From gagsl-py2 at yahoo.com.ar Wed Jul 1 00:15:06 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 01 Jul 2009 01:15:06 -0300 Subject: invoking a method from two superclasses References: Message-ID: En Tue, 30 Jun 2009 21:34:02 -0300, Mitchell L Model escribi?: > Allow me to add to my previous question that certainly the superclass > methods can be called explicitly without resorting to super(), e.g.: > > class C(A, B): > def __init__(self): > A.__init__(self) > B.__init__(self) > > My question is really whether there is any way of getting around the > explicit class names by using super() and if not, shouldn't the > documentation > of super point out that if more than one class on the mro defines a > method > only the first will get called? super returns [a proxy to] the *next* class in the MRO chain; it may or may not be a superclass of C (it may be a sibling class instead). If you know that only A B and C are involved and no other subclass ever exists, yes, you can explicitely invoke A.__init__ and B.__init__. If not (and this covers the vast majority of cases) using super in all places is the only way to get correct results (for details, see the "Harmful" article by James Knight [1] that someone has already referenced) Note that doing it right with __init__ is tricky: usually, cooperative methods share the same signature so the super() call just passes the same arguments to the next class in the chain. But isn't uncommon for __init__ to have different signatures for different subclasses; the article [1] contains a recipe for this case. > What's strange is that it specifically mentions diamond patterns, which > is an important caseto get right, but it doesn't show how. The "typical superclass call" in the 2.6 docs is, uhm, typical :) - in principle you use it everywhere you have cooperative classes. For the sake of completeness I'll copy it here: class C(A, B): def method(self, arg): super(C, self).method(arg) (you may simply use super().method(arg) in Python 3). Every implementation of method() in the class hierarchy should contain a super call like above (well, it gets somewhat more complex due to error handling, but you get the idea...) In addition to the "Harmful" document, consider reading the three-article series by M. Simionato [2] which explains the problem from a different point of view. [1] Python's Super Considered Harmful http://fuhm.net/super-harmful/ [2] http://www.artima.com/weblogs/viewpost.jsp?thread=236275 -- Gabriel Genellina From nmartelaro at gmail.com Wed Jul 1 00:51:37 2009 From: nmartelaro at gmail.com (Nik Martelaro) Date: Wed, 1 Jul 2009 00:51:37 -0400 Subject: Streaming Webcam over network Message-ID: <885d14c40906302151u57d7005n5532093d68d2c786@mail.gmail.com> Hi eveyone, I am working on a project that requires streaming video from a webcam over LAN and displaying the live video on the remote computer. Basically, I am creating a specialized video chat client. I can get really nice video locally using VideoCapture and Pygame, however have only been able to send pixel data and recreate a single image at the other end. Does anyone know of a good video streaming module or any other way of doing this in Python? Thanks for any help! -------------- next part -------------- An HTML attachment was scrubbed... URL: From rylesny at gmail.com Wed Jul 1 00:52:52 2009 From: rylesny at gmail.com (ryles) Date: Tue, 30 Jun 2009 21:52:52 -0700 (PDT) Subject: Making code run in both source tree and installation path References: Message-ID: On Jun 29, 12:20?pm, Javier Collado wrote: > Hello, > > I would like to be able to run the main script in a python project > from both the source tree and the path in which it's installed on > Ubuntu. The script, among other things, imports a package which in > turns makes use of some data files that contains some metadata that is > needed in xml format. > > The source tree has an structure such as this one: > setup.py > debian/ (packaging files) > src/ (source code) > src/lib (package files) > src/data (data files) > src/bin (main script) > > However, when the project is installed using setup.py install, the > directory structure is approximately this way: > /usr/local/bin (main script) > /usr/local/share/ (data files) > /usr/local/lib/python2.x/dist-packages/ (library files) > > And when installing the code through a package, the structure is the > same one, but removing "local". > > Hence, the data files aren't always in the same relative directories > depending on we're executing code from the source tree or from the > installation. To make it possible to run the code from both places, > I've seen different approaches: > - distutils trick in setup.py to modify the installed script (i.e. > changing a global variable value) so that it has a reference to the > data files location. > - Heuristic in the package code to detect when it's being executed > from the source tree and when it has been the installed > - Just using an environment variable that the user must set according > to his needs > > I guess that there are other options, for example, maybe using > buildout. What would you say it's the best/more elegant option to > solve this problem? > > Best regards, > ? ?Javier It's kludgey, but one option may be to try and use __file__ to figure out where the script is installed. Something like os.path.dirname (os.path.abspath(__file__)) could tell you if it's in src/ or in the bin/ directory, and then data files could be found in the appropriate place. I like the distutils/variable option better. Your script is more likely to still behave correctly when copied to another directory. Plus its code definitely remains cleaner. From gagsl-py2 at yahoo.com.ar Wed Jul 1 00:54:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 01 Jul 2009 01:54:45 -0300 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: En Tue, 30 Jun 2009 22:52:18 -0300, Mag Gam escribi?: > I am very new to python and I am in the process of loading a very > large compressed csv file into another format. I was wondering if I > can do this in a multi thread approach. Does the format conversion involve a significant processing time? If not, the total time is dominated by the I/O time (reading and writing the file) so it's doubtful you gain anything from multiple threads. > Here is the pseudo code I was thinking about: > > Let T = Total number of lines in a file, Example 1000000 (1 million > files) > Let B = Total number of lines in a buffer, for example 10000 lines > > > Create a thread to read until buffer > Create another thread to read buffer+buffer ( So we have 2 threads > now. But since the file is zipped I have to wait until the first > thread is completed. Unless someone knows of a clever technique. > Write the content of thread 1 into a numpy array > Write the content of thread 2 into a numpy array Can you process each line independently? Is the record order important? If not (or at least, some local dis-ordering is acceptable) you may use a few worker threads (doing the conversion), feed them thru a Queue object, put the converted lines into another Queue, and let another thread write the results onto the destination file. import Queue, threading, csv def convert(in_queue, out_queue): while True: row = in_queue.get() if row is None: break # ... convert row out_queue.put(converted_line) def write_output(out_queue): while True: line = out_queue.get() if line is None: break # ... write line to output file in_queue = Queue.Queue() out_queue = Queue.Queue() tlist = [] for i in range(4): t = threading.Thread(target=convert, args=(in_queue, out_queue)) t.start() tlist.append(t) output_thread = threading.Thread(target=write_output, args=(out_queue,)) output_thread.start() with open("...") as csvfile: reader = csv.reader(csvfile, ...) for row in reader: in_queue.put(row) for t in tlist: in_queue.put(None) # indicate end-of-work for t in tlist: t.join() # wait until finished out_queue.put(None) output_thread.join() # wait until finished -- Gabriel Genellina From MLMLists at Comcast.net Wed Jul 1 00:57:13 2009 From: MLMLists at Comcast.net (Mitchell L Model) Date: Wed, 1 Jul 2009 00:57:13 -0400 Subject: invoking a method from two superclasses Message-ID: [Continuing the discussion about super() and __init__] The documentation of super points out that good design of diamond patterns require the methods to have the same signature throughout the diamond. That's fine for non-mixin classes where the diamond captures different ways of handling the same data. The classical example is BufferedStram / \ / \ / \ BufInputStrm BufOutputStrm both have buffers, but use them differentlyu \ / \ / \ / RandomAccessStream or something like that The idea of the diamond is to have just one buffer, rather than the two buffers that would result in C++ without making the base classes virtual. All four classes could define __init__ with the argument filename, or whatever, and everything works fine. The problems start with the use of mixins. In essence, mixins intentionally do NOT want to be part of diamond patterns. They are orthogonal to the "true" or "main" class hierarchy and just poke their heads in hear and there in that hierarchy. Moreover, a class could inherit from multiple mixins. Typical simple orthogonal mixins would be NamedObject, TrackedObject, LoggedObject, ColoredWidget, and other such names compounded from an adjective, participle, or gerund and a completely meaningless name such as Object or Thing and which classes typically manage one action or piece of state to factor it out from the many other classes that need it where the pattern of which classes need them does not follow the regular class hierarchy. Suppose I have a class User that includes NamedObject, TrackedObject, and LoggedObject as base classes. (By Tracked I mean instances are automatically registered in a list or dictionary for use by class methods that search, count, or do other operations on them.) The problem is that each mixin's __init__ is likely to have a different signature. User.__init__ would have arguments (self, name, log), but it would need to call each mixin class's __init__ with different arguments. Mixins are different than what the document refers to as cooperative multiple inheritance -- does that make them uncooperative multiple inheritance classes :-)? I think I'd be back to having to call each mixin class's __init__ explicitly: class User(NamedObject, TrackedObject, LoggedObject) def __init__(self, name, log): NamedObject.__init__(self, name) TrackedObject.__init__(self) LoggedObject.__init__(self, log) This is not terrible. It seems somehow appropriate that because mixin use is "orthogonal" to the "real" inheritance hierarchy, they shouldn't have the right to use super() and the full mro (after all, who knows where the mro will take these calls anyway). And in many cases, two mixins will join into another (a NamedTrackedObject) for convenience, and then classes inheriting from that have one less init to worry about. I suspect this problem is largely with __init__. All the other __special__ fns have defined argument lists and so can be used with diamond patterns, mixins, etc. Does this all sound accurate? (Again, not looking for design advice, just trying to ferret out the subtleties and implications of multiple inheritance and super().) From rylesny at gmail.com Wed Jul 1 01:00:10 2009 From: rylesny at gmail.com (ryles) Date: Tue, 30 Jun 2009 22:00:10 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: On Jun 29, 5:43?pm, Scott David Daniels wrote: >?and I personally wouldn't have it any other way. Simulating a shell > with hooks on its I/O should be so complicated that a "script kiddie" > has trouble writing a Trojan. +1 QOTW From ldo at geek-central.gen.new_zealand Wed Jul 1 01:07:47 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 17:07:47 +1200 Subject: Multi thread reading a file References: Message-ID: In message , Mag Gam wrote: > I am very new to python and I am in the process of loading a very > large compressed csv file into another format. I was wondering if I > can do this in a multi thread approach. Why bother? From ldo at geek-central.gen.new_zealand Wed Jul 1 01:19:30 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 17:19:30 +1200 Subject: Idioms and Anti-Idioms Question References: Message-ID: In message , J. Cliff Dyer wrote: > If the lines got separated, a leading + could disappear into its line > without any errors showing up. A trailing + would raise a syntax error. Unless, of course, it was moved onto the previous line as part of whatever caused the separation of the lines. How would you guard against that? From stefan_ml at behnel.de Wed Jul 1 01:30:13 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 01 Jul 2009 07:30:13 +0200 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <4a4af466$0$31328$9b4e6d93@newsspool4.arcor-online.net> Gabriel Genellina wrote: > En Tue, 30 Jun 2009 22:52:18 -0300, Mag Gam escribi?: > >> I am very new to python and I am in the process of loading a very >> large compressed csv file into another format. I was wondering if I >> can do this in a multi thread approach. > > Does the format conversion involve a significant processing time? If > not, the total time is dominated by the I/O time (reading and writing > the file) so it's doubtful you gain anything from multiple threads. Well, the OP didn't say anything about multiple processors, so multiple threads may not help wrt. processing time. However, if the file is large and the OS can schedule the I/O in a way that a seek disaster is avoided (although that's hard to assure with today's hard disk storage density, but SSDs may benefit), multiple threads reading multiple partial streams may still reduce the overall runtime due to increased I/O throughput. That said, the OP was mentioning that the data was compressed, so I doubt that the I/O bandwidth is a problem here. As another poster put it: why bother? Run a few benchmarks first to see where (and if!) things really get slow, and then check what to do about the real problem. Stefan From pavlovevidence at gmail.com Wed Jul 1 01:37:19 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 30 Jun 2009 22:37:19 -0700 (PDT) Subject: invoking a method from two superclasses References: Message-ID: <65fa395e-0ac1-4b64-9792-74323ecbecc8@a36g2000yqc.googlegroups.com> On Jun 30, 9:15?pm, "Gabriel Genellina" wrote: > En Tue, 30 Jun 2009 21:34:02 -0300, Mitchell L Model ? > escribi : > > > Allow me to add to my previous question that certainly the superclass > > methods can be called explicitly without resorting to super(), e.g.: > > > ? ? class C(A, B): > > ? ? ? ? def __init__(self): > > ? ? ? ? ? ? A.__init__(self) > > ? ? ? ? ? ? B.__init__(self) > > > My question is really whether there is any way of getting around the > > explicit class names by using super() and if not, shouldn't the ? > > documentation > > of super point out that if more than one class on the mro defines a ? > > method > > only the first will get called? > > super returns [a proxy to] the *next* class in the MRO chain; it may or ? > may not be a superclass of C (it may be a sibling class instead). It could be even a niece class. Carl Banks From sato.photo at gmail.com Wed Jul 1 01:38:04 2009 From: sato.photo at gmail.com (sato.photo at gmail.com) Date: Tue, 30 Jun 2009 22:38:04 -0700 (PDT) Subject: Basic question from pure beginner Message-ID: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> I have been going through some Python Programming exercises while following the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with the first "If" exercise listed on this page: http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is entered. I know this is really basic, please forgive me. I have no programming experience and have just started going through these tutorials. My code is here: http://python.pastebin.com/m6036b52e -daniel From pavlovevidence at gmail.com Wed Jul 1 02:04:15 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 30 Jun 2009 23:04:15 -0700 (PDT) Subject: Making code run in both source tree and installation path References: Message-ID: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> On Jun 29, 9:20?am, Javier Collado wrote: > I've seen different approaches: > - distutils trick in setup.py to modify the installed script (i.e. > changing a global variable value) so that it has a reference to the > data files location. One of my biggest complaints about distutils is that it doesn't do this, a limitation made worse by the fact that distutils allows you to specify an alternate data file directory, but your scripts have no way to know what that alternate directory is installed. Which really limits the usefulness of that functionality. The most common way I've seen people work around this issue is to throw their data files into the package directories. Yuck. At one point I hacked up a setup.py file to look in the distutils data structure and pull out the data install location, and wrote out a trivial python file listing that location. (The distutils build data structure is helpfully returned by setup function.) I never felt good about it, and it wouldn't work if the install was done in steps (such as build as user, install as superuser). If you care to navigate the murky internals of distutils to do this in a more elegant way, more power to you, and if so I'd recommend doing it that way. > - Heuristic in the package code to detect when it's being executed > from the source tree and when it has been the installed > - Just using an environment variable that the user must set according > to his needs I guess I'd combine these two. Make a sensible guess about where the data files are by checking out the environment, but if the data files aren't there (such as if the user installs to a different data location) then they are responsible for setting an env variable or config option. > I guess that there are other options, for example, maybe using > buildout. What would you say it's the best/more elegant option to > solve this problem? Another option is not to use distutils at all. If you are writing an application, I think that would be a good idea. I don't think applications really need to be located in site-packages. If you have any C-extensions it might complicate this. Carl Banks From __peter__ at web.de Wed Jul 1 03:01:44 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 01 Jul 2009 09:01:44 +0200 Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: sato.photo at gmail.com wrote: > I have been going through some Python Programming exercises while > following the MIT OpenCourseWare Intro to CS syllabus and am having > some trouble with the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password > three times, but can't get it to quit right away after the correct one > is entered. I know this is really basic, please forgive me. I have > no programming experience and have just started going through these > tutorials. > > My code is here: > > http://python.pastebin.com/m6036b52e > > -daniel Some hints: (1) Have the while loop check for both the correct password and the number of tries. Pseudocode: while and : ... (2) After the loop has terminated you can check whether the password was entered correctly and print the appropriate message. Aside: you can't mix if...else with other statements correct: if cond: print "sunny" else: print "cloudy" print "weather" wrong: if cond: print "norwegian" print "blue" else: # syntax error, else must immediately follow the indented if-block: print "moon" you could fix it like so: if cond: print "norwegian" print "blue" if not cond: print "moon" Peter From wuwei23 at gmail.com Wed Jul 1 03:06:38 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 00:06:38 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: On Jul 1, 3:38?pm, "sato.ph... at gmail.com" wrote: > I have been able to make the module quit after entering a password > three times, but can't get it to quit right away after the correct one > is entered. ? Not with the code you pasted, you haven't. There's a missing colon on line 7 & line 9 isn't indented properly. It's always best if we're referring to the same source when trying to help with a problem. The problem you're having, though, has to do with the while loop and the fact that it's only checking one condition: has the 'count' counter been incremented to 3. What you need to do is _also_ check for the success condition: is_confirmed = False while count != 3 or is_confirmed: guess = raw_input("Enter your password: ") guess = str(guess) if guess != password: print "Access Denied" count = count + 1 else: print "Password Confirmed" is_confirmed = True This also provides you with a nice boolean that shows whether or not the password was confirmed, which may be useful for latter code. However, whenever you want to loop a set number of times, it's usually better to use a 'for' loop instead: PASSWORD = 'qwerty' MAXRETRY = 3 for attempt in xrange(MAXRETRY): guess = str(raw_input('Enter your password: ')) is_complete = guess == PASSWORD if is_complete: print 'Password confirmed' break # this exits the for loop else: print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) This saves you from the burden of creating, incrementing & testing your own counter. If there's anything here that isn't clear, please don't hesitate to ask. From sato.photo at gmail.com Wed Jul 1 03:58:27 2009 From: sato.photo at gmail.com (sato.photo at gmail.com) Date: Wed, 1 Jul 2009 00:58:27 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Thank you for all of the help. With your assistance and help from the Python Tutor mailing list I was able to come up with the following code: password = "qwerty" correct_password_given = False guess = "0" count = 0 while count != 3 and not correct_password_given : guess = raw_input("Enter your password: ") guess = str(guess) if guess != password: print "Access Denied" count = count + 1 else: print "Password Confirmed" correct_password_given = True If I understand it correctly, the function will continue to loop until either the count == 3 or until correct_password_give = True, satisfying the two conditions of the assignment, which were to lock a user out after three failed password attempts and to print "Access Granted" and end the module if the correct password is given. Am I understanding this correctly? Thanks! On Jul 1, 12:06?am, alex23 wrote: > On Jul 1, 3:38?pm, "sato.ph... at gmail.com" > wrote: > > > I have been able to make the module quit after entering a password > > three times, but can't get it to quit right away after the correct one > > is entered. ? > > Not with the code you pasted, you haven't. There's a missing colon on > line 7 & line 9 isn't indented properly. It's always best if we're > referring to the same source when trying to help with a problem. > > The problem you're having, though, has to do with the while loop and > the fact that it's only checking one condition: has the 'count' > counter been incremented to 3. What you need to do is _also_ check for > the success condition: > > is_confirmed = False > while count != 3 or is_confirmed: > ? guess = raw_input("Enter your password: ") > ? guess = str(guess) > ? if guess != password: > ? ? print "Access Denied" > ? ? count = count + 1 > ? else: > ? ? print "Password Confirmed" > ? ? is_confirmed = True > > This also provides you with a nice boolean that shows whether or not > the password was confirmed, which may be useful for latter code. > > However, whenever you want to loop a set number of times, it's usually > better to use a 'for' loop instead: > > PASSWORD = 'qwerty' > MAXRETRY = 3 > for attempt in xrange(MAXRETRY): > ? guess = str(raw_input('Enter your password: ')) > ? is_complete = guess == PASSWORD > ? if is_complete: > ? ? print 'Password confirmed' > ? ? break # this exits the for loop > ? else: > ? ? print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) > > This saves you from the burden of creating, incrementing & testing > your own counter. > > If there's anything here that isn't clear, please don't hesitate to > ask. From bruno.42.desthuilliers at websiteburo.invalid Wed Jul 1 04:06:21 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 10:06:21 +0200 Subject: Learning to use decorators with classes In-Reply-To: References: Message-ID: <4a4b18f2$0$15174$426a74cc@news.free.fr> sk8in_zombi at yahoo.com.au a ?crit : > --- On Tue, 30/6/09, Bruno Desthuilliers wrote: > (snip) >> >> This can't work, and it's a FAQ FWIW - but since there's no >> official c.l.py FAQ, we won't hold it against you !-) >> > > Can you please point me to the FAQ related to this snippet. I would be grateful. Well, as I said, there's *no* c.l.py FAQ (I mean, any half-official text document listing frequently asked questions and their canonical answers), so I just can't provide any pointer here :-/ I guess we all here love talking about Python (and showing out our science, knowledge and wisdom) too much to maintain a FAQ !-) (snip) From bruno.42.desthuilliers at websiteburo.invalid Wed Jul 1 04:26:16 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 10:26:16 +0200 Subject: Basic question from pure beginner In-Reply-To: <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <4a4b1d9d$0$1525$426a74cc@news.free.fr> sato.photo at gmail.com a ?crit : > Thank you for all of the help. With your assistance and help from the > Python Tutor mailing list I was able to come up with the following > code: > > password = "qwerty" > correct_password_given = False > guess = "0" You could just use None here: guess=None > count = 0 > while count != 3 and not correct_password_given : > guess = raw_input("Enter your password: ") > guess = str(guess) IIRC, raw_input() already returns a string !-) > if guess != password: > print "Access Denied" > count = count + 1 Or: count += 1 > else: > print "Password Confirmed" > correct_password_given = True > > > If I understand it correctly, the function will continue to loop until > either the count == 3 or until correct_password_give = True, There are many ways to write a same boolean test, and some are easier to understand. Your test could be written: while not (count == 3 or correct_password_given) : # proceed Does it answer your question ?-) As a general rule: * not A and not B <=> not (A or B) * not A or not B <=> not (A and B) > satisfying the two conditions of the assignment, which were to lock a > user out after three failed password attempts and to print "Access > Granted" and end the module if the correct password is given. Well, given your snippet, execution indeed terminates after either successful 'login' or 3 failed attempts, but that's mostly because there's no code to execute after the while loop... From mr.spoon21 at gmail.com Wed Jul 1 04:33:28 2009 From: mr.spoon21 at gmail.com (Mr.SpOOn) Date: Wed, 1 Jul 2009 10:33:28 +0200 Subject: Getting input the scanf way Message-ID: <8f67b6f80907010133t7f6e2d9eg9ffa3b927d540728@mail.gmail.com> Hi, I need to do some kind of interactive command line program. I mean: I run the program, it asks me for input, I type something and then I get the output or other questions. I'm not sure what is the right way to achieve this. Thanks, Carlo From ldo at geek-central.gen.new_zealand Wed Jul 1 04:33:45 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:33:45 +1200 Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: In message , ryles wrote: > On Jun 29, 5:43 pm, Scott David Daniels wrote: > >>and I personally wouldn't have it any other way. Simulating a shell >> with hooks on its I/O should be so complicated that a "script kiddie" >> has trouble writing a Trojan. > > +1 QOTW Trouble is, script kiddies, by definition, don't need to write anything. They just need to download something somebody else has already written. -- Lawrence "I counter your +1 with my -1" D'Oliveiro From ldo at geek-central.gen.new_zealand Wed Jul 1 04:39:06 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:39:06 +1200 Subject: No trees in the stdlib? References: Message-ID: In message , Jo?o Valverde wrote: > Simple example usage case: Insert string into data structure in sorted > order if it doesn't exist, else retrieve it. the_set = set( ... ) if str in the_set : ... "retrieval" case ... else : the_set.add(str) #end if Want sorted order? sorted(tuple(the_set)) What could be simpler? From ldo at geek-central.gen.new_zealand Wed Jul 1 04:40:19 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:40:19 +1200 Subject: No trees in the stdlib? References: <4A4462E6.7080700@netcabo.pt> <50697b2c0906252323y60c237d0i44725e841ad29ed5@mail.gmail.com> Message-ID: In message , Miles Kaufmann wrote: > On Jun 26, 2009, at 2:23 AM, Chris Rebert wrote: > >> That's pretty much the bisect module in a nutshell. It manipulates a >> sorted list using binary search. > > With O(n) insertions and removals, though. A decent self-balancing > binary tree will generally do those in O(log n). For values of n below, say, a million, would you even notice the difference on modern hardware? From http Wed Jul 1 04:42:42 2009 From: http (Paul Rubin) Date: 01 Jul 2009 01:42:42 -0700 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <7xeit0iykd.fsf@ruckus.brouhaha.com> Joachim Str?mbergson writes: > http://docs.python.org/library/hashlib.html > I would suggest to use the SHA-256 in the library. I agree with this. We should even move towards supporting x509 and/or gpg signatures in eggs, similar to signed .jar files. From ldo at geek-central.gen.new_zealand Wed Jul 1 04:43:10 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:43:10 +1200 Subject: No trees in the stdlib? References: <11eb0a6e-4e21-4a61-bf46-4221fd75dc8d@v15g2000prn.googlegroups.com> Message-ID: In message , Jo?o Valverde wrote: > But a dict can't be used to implement a (sorted) table ADT. for key in sorted(the_dict.keys(), cmp = ... whatever ordering criteria you like ...) : ... do something with the_dict[key] ... #end for From R.Brodie at rl.ac.uk Wed Jul 1 04:43:40 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Wed, 1 Jul 2009 09:43:40 +0100 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: "Joachim Str?mbergson" wrote in message news:mailman.2422.1246418400.8015.python-list at python.org... > Even so, choosing md5 in 2009 for something that (hopefully) will be > used in years is a bad design decision. It creates a dependency for to > an algorithm that all sensible recommendations point you to move away > from. Why not write the field as algorithm:value? e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 Installers can fallback to using hashlib.new(), so you can plug in a new algorithm without changing the PEP or the installer code. From http Wed Jul 1 04:43:49 2009 From: http (Paul Rubin) Date: 01 Jul 2009 01:43:49 -0700 Subject: No trees in the stdlib? References: <4A4462E6.7080700@netcabo.pt> <50697b2c0906252323y60c237d0i44725e841ad29ed5@mail.gmail.com> Message-ID: <7xab3oiyii.fsf@ruckus.brouhaha.com> Lawrence D'Oliveiro writes: > > With O(n) insertions and removals, though. A decent self-balancing > > binary tree will generally do those in O(log n). > > For values of n below, say, a million, would you even notice the difference > on modern hardware? Huh? Yes, of course you'd notice, unless you were doing the operation just once or something like that. From http Wed Jul 1 04:47:34 2009 From: http (Paul Rubin) Date: 01 Jul 2009 01:47:34 -0700 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <7x3a9gdc2h.fsf@ruckus.brouhaha.com> "Richard Brodie" writes: > Why not write the field as algorithm:value? > e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 This is reasonable, though I would deprecate md5 and sha1 already, and start with sha256. From iainking at gmail.com Wed Jul 1 05:52:24 2009 From: iainking at gmail.com (Iain King) Date: Wed, 1 Jul 2009 02:52:24 -0700 (PDT) Subject: string character count References: <58ceecd5-6d2c-44a9-8ef9-01feac5792f4@b9g2000yqm.googlegroups.com> Message-ID: On Jun 30, 6:27?pm, noydb wrote: > If I have a string for a file name such that I want to find the number > of characters to the left of the dot, how can that be done? > > I did it this way: > x = "text12345.txt" > dot = x.find('.') > print dot > > Was curious to see what method others would use - helps me learn. ?I > guess I was most curious to see if it could be done in one line. > > And, how would a char count be done with no dot -- like if the string > were "textstringwithoutdot" or "no dot in text string"? import os root, ext = os.path.splitext(x) print len(root) or in one line (assuming you've imported os): print len(os.path.splitext(x)[0]) Iain From pm567426 at gmail.com Wed Jul 1 06:00:32 2009 From: pm567426 at gmail.com (Pedram) Date: Wed, 1 Jul 2009 03:00:32 -0700 (PDT) Subject: A question about fill_free_list(void) function Message-ID: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Hello community, I'm reading the CPython interpreter source code, first, if you have something that I should know for better reading this source code, I would much appreciate that :) second, in intobject.c file, I read the following code in fill_free_list function that I couldn't understand: while (--q > p) Py_TYPE(q) = (struct _typeobject *)(q-1); Py_TYPE(q) = NULL; What does it mean? Thanks. From jerome.fuselier at gmail.com Wed Jul 1 06:14:20 2009 From: jerome.fuselier at gmail.com (=?ISO-8859-1?Q?J=E9r=F4me_Fuselier?=) Date: Wed, 1 Jul 2009 03:14:20 -0700 (PDT) Subject: Problem with uuid package when embedding a python interpreter References: <7d5cfbf0-38d5-4505-a93a-f321d0da74b8@c36g2000yqn.googlegroups.com> Message-ID: On Jun 30, 7:02?pm, "Gabriel Genellina" wrote: > En Tue, 30 Jun 2009 13:05:42 -0300, J?r?me Fuselier > escribi?: > > > > > ? I've tried to import a script in an embedded python intrepreter but > > this script fails when it imports the uuid module. I have a > > segmentation fault in Py_Finalize(). > > > #include "Python.h" > > > void test() { > > ? ? Py_Initialize(); > > ? ? PyImport_Import(PyString_FromString("uuid")); > > ? ? Py_Finalize(); > > } > > > main(int argc, char **argv) > > { > > ? ? for (i=0 ; i < 10; i++) > > ? ? ? ? test(); > > } > > > For my application, I have to call Py_initialize and Py_Finalize > > several times so factorizing them in the main function is not an easy > > solution for me. > > Are you sure you can't do that? Not even using Py_IsInitialized? Try to > avoid repeatedly calling Py_Initialize - won't work. > > Python 2.x does not have a way to "un-initialize" an extension module > (that's a big flaw in Python design). Modules that contain global state > are likely to crash the interpreter when used by the second time. (Python > 3 attempts to fix that) > > -- > Gabriel Genellina Hi Gabriel, Thanks for your response. I can modify my code to call Py_IsInitialized which I didn't know before and this works well. The problem is that I did not own the process which calls Py_Initialize and Py_Finalize. I'm able to call Py_Initialize correctly but I can't call Py_Finalize so this is not perfect. At least I have a temporary solution which is not too bad. Thanks, Jerome From jeanmichel at sequans.com Wed Jul 1 06:33:43 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 01 Jul 2009 12:33:43 +0200 Subject: string character count In-Reply-To: <4A4A51D7.6040602@mrabarnett.plus.com> References: <58ceecd5-6d2c-44a9-8ef9-01feac5792f4@b9g2000yqm.googlegroups.com> <4A4A51D7.6040602@mrabarnett.plus.com> Message-ID: <4A4B3B87.5040703@sequans.com> MRAB wrote: > noydb wrote: >> If I have a string for a file name such that I want to find the number >> of characters to the left of the dot, how can that be done? >> >> I did it this way: >> x = "text12345.txt" >> dot = x.find('.') >> print dot >> >> Was curious to see what method others would use - helps me learn. I >> guess I was most curious to see if it could be done in one line. >> > >>> print "text12345.txt".find('.') > 9 > >> And, how would a char count be done with no dot -- like if the string >> were "textstringwithoutdot" or "no dot in text string"? > > If there's no dot then find() returns -1. > > If you wanted to know the number of characters before the dot, if > present, or in total otherwise, then you could use split(): > > >>> len("text12345.txt".split(".", 1)[0]) > 9 > >>> len("textstringwithoutdot".split(".", 1)[0]) > 20 > You may have problems with files containing multiple dots. the os module provide nice & safe methods to parse a file path (on any system, mac os windows, linux). >>>f = "/dir1/dir2/test.log" >>>os.path.splitext(f) ('/dir1/dir2/test', '.log') >>>os.path.splitext(os.path.basename(f)) ('test', '.log') >>>f2 = 'test.log' >>>os.path.splitext(os.path.basename(f2)) ('test', '.log') one safe way would be then >>> import os >>> x = "text12345.txt" >>> base , ext = os.path.splitext(os.path.basename(x)) >>> print len(base) 9 Jean-Michel From icanbob at gmail.com Wed Jul 1 06:40:22 2009 From: icanbob at gmail.com (bobicanprogram) Date: Wed, 1 Jul 2009 03:40:22 -0700 (PDT) Subject: Passing parameters for a C program in Linux. References: <7fa13d0c-d031-4576-8fee-a1c84af3e265@y7g2000yqa.googlegroups.com> Message-ID: On Jun 30, 6:46 am, "venutaurus... at gmail.com" wrote: > Hi all, > I have to write an automted script which will test my c > program. That program when run will ask for the commands. For example: > > local-host# ./cli > Enter 1 for add > Enter 2 for sub > Enter 3 for mul > 1 -------Our option > Enter two numbers > 44 33 -------- Our option > Result is 77 > > As shown in the above example it lists all the options and waits for > user input and once given, it does some operations and waits for some > more. This has to be automated. > > Can someone give suggestions how to do this in Python (Linux Platform > in particular). > > Thank you, > Venu. Take a look at the examples here: http://www.icanprogram.com/06py/main.html You can probably do what you want using the SIMPL toolkit quite easily. bob From steve at REMOVE-THIS-cybersource.com.au Wed Jul 1 07:41:07 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Jul 2009 11:41:07 GMT Subject: No trees in the stdlib? References: Message-ID: <025b3cfd$0$20654$c3e8da3@news.astraweb.com> On Wed, 01 Jul 2009 20:39:06 +1200, Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > >> Simple example usage case: Insert string into data structure in sorted >> order if it doesn't exist, else retrieve it. > > the_set = set( ... ) > > if str in the_set : > ... "retrieval" case ... Not terribly useful, because there's no way of attaching any data to retrieve to the key. Apart from the case of interning strings (or other keys), you'd probably want a dict rather than a set: if str in the_dict: return the_dict[str] else: the_dict[str] = something_or_other > else : > the_set.add(str) > #end if > Want sorted order? > > sorted(tuple(the_set)) > > What could be simpler? Dropping the entirely superfluous creation of a tuple: sorted(the_set) That's a reasonable approach for small sets of data, but it isn't very useful if the_set contains 4GB of keys, because you have to duplicate the keys, then sort them. More effective would be a data structure that was always sorted. This has the further advantage that it's even simpler than sorted(the_set): the_set -- Steven From magawake at gmail.com Wed Jul 1 07:41:29 2009 From: magawake at gmail.com (Mag Gam) Date: Wed, 1 Jul 2009 07:41:29 -0400 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <1cbd6f830907010441l3f953d13r6dd4e1a55dedc806@mail.gmail.com> Thanks for the response Gabriel. On Wed, Jul 1, 2009 at 12:54 AM, Gabriel Genellina wrote: > En Tue, 30 Jun 2009 22:52:18 -0300, Mag Gam escribi?: > >> I am very new to python and I am in the process of loading a very >> large compressed csv file into another format. ?I was wondering if I >> can do this in a multi thread approach. > > Does the format conversion involve a significant processing time? If not, > the total time is dominated by the I/O time (reading and writing the file) > so it's doubtful you gain anything from multiple threads. The format does inolve significant time processing each line. > >> Here is the pseudo code I was thinking about: >> >> Let T ?= Total number of lines in a file, Example 1000000 (1 million >> files) >> Let B = Total number of lines in a buffer, for example 10000 lines >> >> >> Create a thread to read until buffer >> Create another thread to read buffer+buffer ?( So we have 2 threads >> now. But since the file is zipped I have to wait until the first >> thread is completed. Unless someone knows of a clever technique. >> Write the content of thread 1 into a numpy array >> Write the content of thread 2 into a numpy array > > Can you process each line independently? Is the record order important? If > not (or at least, some local dis-ordering is acceptable) you may use a few > worker threads (doing the conversion), feed them thru a Queue object, put > the converted lines into another Queue, and let another thread write the > results onto the destination file. Yes, each line can be independent. The original file is a time series file which I am placing it into a Numpy array therefore I don't think the record order is important. The writing is actually done when I place a value into a "dset" object. Let me show you what I mean. reader=csv.reader(open("100000.csv")) for s,row in enumerate(reader): if s!=0 and s%bufsize==0: dset[s-bufsize:s] = t #here is where I am writing the data to the data structure. Using a range or broadcasting. #15 columns if len(row) != 15: break t[s%bufsize] = tuple(row) #Do this all the way at the end for flushing. if (s%bufsize != 0): dset[(s//bufsize)*bufsize:s]=t[0:s%bufsize] > > import Queue, threading, csv > > def convert(in_queue, out_queue): > ?while True: > ? ?row = in_queue.get() > ? ?if row is None: break > ? ?# ... convert row > ? ?out_queue.put(converted_line) > > def write_output(out_queue): > ?while True: > ? ?line = out_queue.get() > ? ?if line is None: break > ? ?# ... write line to output file > > in_queue = Queue.Queue() > out_queue = Queue.Queue() > tlist = [] > for i in range(4): > ?t = threading.Thread(target=convert, args=(in_queue, out_queue)) > ?t.start() > ?tlist.append(t) > output_thread = threading.Thread(target=write_output, args=(out_queue,)) > output_thread.start() > > with open("...") as csvfile: > ?reader = csv.reader(csvfile, ...) > ?for row in reader: > ? ?in_queue.put(row) > > for t in tlist: in_queue.put(None) # indicate end-of-work > for t in tlist: t.join() # wait until finished > out_queue.put(None) > output_thread.join() # wait until finished > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > From ganesh.gbg at gmail.com Wed Jul 1 07:48:11 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Wed, 1 Jul 2009 04:48:11 -0700 (PDT) Subject: what will be best framework to select to develop Multi-Level Marketing (network marketing) application in python? Message-ID: <2f8c1642-6cad-4cd1-bc97-60c16aea31fd@g6g2000vbr.googlegroups.com> hi group, I have to build a software for Multi-Level Marketing (network marketing),There are several Frameworks available in python ,i could like to know the best Framework suitable to develop an application on Multi-Level Marketing . Thanks From Ron.Barak at lsi.com Wed Jul 1 07:48:40 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Wed, 1 Jul 2009 12:48:40 +0100 Subject: What are the limitations of cStringIO.StringIO() ? Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Hi, I think I'm up against a limitation in cStringIO.StringIO(), but could not find any clues on the web, especially not in http://docs.python.org/library/stringio.html. What I have is the following function: def concatenate_files(self,filename_array): import types f = cStringIO.StringIO() for filename in filename_array: log_stream = LogStream(filename) if not isinstance(log_stream, types.NoneType): try: string_ = log_stream.input_file.read() except AttributeError, e: sys.stderr.write("AttributeError: "+str(e)+"\n") sys.stderr.write("log_stream: "+str(log_stream)+"\n") else: return(None) f.write(string_) return (f) And, when the list of files - in filename_array - is pointing to long/large enough files, I get the exception: Traceback (most recent call last): File "svm_ts_tool_in_progress.py", line 244, in OnSelectCell self.InspectorViewController(row,col) File "svm_ts_tool_in_progress.py", line 1216, in InspectorViewController self.InspectorePaneGetRecords(self.req_table, rec) File "svm_ts_tool_in_progress.py", line 1315, in InspectorePaneGetRecords log_stream = ConcatenatedLogStream(self.events_dict[req_table]["db_dict"].keys()) File "c:\Documents and Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 31, in __init__ self.input_file = self.concatenate_files(filename_array) File "c:\Documents and Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 47, in concatenate_files string_ = log_stream.input_file.read() MemoryError 1. Anyone knows whet's the limitation on cStringIO.StringIO() objects ? 2. Could you suggest a better way to create a string that contains the concatenation of several big files ? Thanks, Ron. -------------- next part -------------- An HTML attachment was scrubbed... URL: From thomas.bugzilla at gmx.net Wed Jul 1 08:38:36 2009 From: thomas.bugzilla at gmx.net (Thomas) Date: Wed, 01 Jul 2009 14:38:36 +0200 Subject: infinite recursion in pickle.load() Message-ID: we regularly pickle and unpickle data from the same script (mostly dictionaries). sometimes, a file written this way cannot be unpickled with pickle.load(), due to an infinite recursion with __getattr__ in codecs.py. here is a python2.5 stack trace excerpt: /usr/local/lib/python2.5/pickle.py in load(file) 1403 1404 def load(file): -> 1405 return Unpickler(file).load() 1406 1407 def loads(str): /usr/local/lib/python2.5/pickle.py in load(self) 891 while 1: 892 key = read(1) --> 893 dispatch[key](self) 894 except _Stop, stopinst: 895 return stopinst.value /usr/local/lib/python2.5/pickle.py in load_build(self) 1248 state = stack.pop() 1249 inst = stack[-1] -> 1250 setstate = getattr(inst, "__setstate__", None) 1251 if setstate: 1252 setstate(state) /usr/local/lib/python2.5/codecs.py in __getattr__(self, name, getattr) 328 """ Inherit all other methods from the underlying stream. 329 """ --> 330 return getattr(self.stream, name) 331 332 def __enter__(self): /usr/local/lib/python2.5/codecs.py in __getattr__(self, name, getattr) 328 """ Inherit all other methods from the underlying stream. 329 """ --> 330 return getattr(self.stream, name) 331 332 def __enter__(self): ... The last frame repeats ad infinitum. 'inst' in the third frame is a The problem is the same with cPickle. This looks somewhat related to this Python issue, which is only about the exception reporting: http://bugs.python.org/issue5508 (The title of the issue is the error you get when running our code in python2.6). Any idea how to go about that? T. From gil.shinar at gmail.com Wed Jul 1 08:56:12 2009 From: gil.shinar at gmail.com (eranlevi) Date: Wed, 1 Jul 2009 05:56:12 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> Message-ID: <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> On Jun 30, 7:48?pm, s... at pobox.com wrote: > ? ? Gil> I have looked for a timeout parameter to limit the 4 minutes to > ? ? Gil> something more reasonable but couldn't find one. ?Can anyone please > ? ? Gil> help? > > ? ? Gil> BTW, I'm using Sybase.connect(, , , > ? ? Gil> datetime='auto') > > We use the Sybase module where I work, but I've never encountered this > problem (or a timeout parameter of any kind). ?At any rate, you'll probably > have more luck asking on the python-sybase mailing list: > > ? ? python-sybase-m... at lists.sourceforge.net > > -- > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > ? ? when i wake up with a heart rate below 40, i head right for the espresso > ? ? machine. -- chaos @ forums.usms.org Are you saying, that when you trying to connect to a sybase DBS server and the DBS or the server is down, you get an error after a few seconds and not after a few minutes? Which python version are you using? I'll try to ask in the python-sybase mailing list Thanks From gil.shinar at gmail.com Wed Jul 1 09:08:01 2009 From: gil.shinar at gmail.com (eranlevi) Date: Wed, 1 Jul 2009 06:08:01 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> Message-ID: <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> On Jul 1, 3:56?pm, eranlevi wrote: > On Jun 30, 7:48?pm, s... at pobox.com wrote: > > > > > ? ? Gil> I have looked for a timeout parameter to limit the 4 minutes to > > ? ? Gil> something more reasonable but couldn't find one. ?Can anyone please > > ? ? Gil> help? > > > ? ? Gil> BTW, I'm using Sybase.connect(, , , > > ? ? Gil> datetime='auto') > > > We use the Sybase module where I work, but I've never encountered this > > problem (or a timeout parameter of any kind). ?At any rate, you'll probably > > have more luck asking on the python-sybase mailing list: > > > ? ? python-sybase-m... at lists.sourceforge.net > > > -- > > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > > ? ? when i wake up with a heart rate below 40, i head right for the espresso > > ? ? machine. -- chaos @ forums.usms.org > > Are you saying, that when you trying to connect to a sybase DBS server > and the DBS or the server is down, you get an error after a few > seconds and not after a few minutes? Which python version are you > using? > > I'll try to ask in the python-sybase mailing list > > Thanks There's no such group as python-sybase :-( Thanks From pm567426 at gmail.com Wed Jul 1 09:50:31 2009 From: pm567426 at gmail.com (Pedram) Date: Wed, 1 Jul 2009 06:50:31 -0700 (PDT) Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Message-ID: Oh... I got it! :) I found this line in ctypes.h: #define Py_TYPE(q) = ((PyObject *)(q))->ob_next; So those lines are trying to set the blocks type from rear to front. But I still don't know why after the while (when q is equal to p), the Py_TYPE(q) set to NULL! From sebastian.schabe at gmx.de Wed Jul 1 10:51:47 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Wed, 01 Jul 2009 16:51:47 +0200 Subject: deleting certain entries in numpy array Message-ID: <4a4b7763$1@news.fhg.de> Hello everybody, I'm new to python and numpy and have a little/special problem: I have an numpy array which is in fact a gray scale image mask, e.g.: mask = array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 255, 255, 255, 0, 0, 255, 0], [ 0, 0, 255, 255, 255, 0, 0, 255, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) and I have another array of (y, x) positions in that mask (first two values of each row): pos = array([[ 3., 2., 0., 0.], [ 3., 4., 0., 0.], [ 5., 2., 0., 0.], [ 5., 4., 0., 0.], [ 6., 2., 0., 0.], [ 6., 7., 0., 0.], [ 0., 0., 0., 0.], [ 8., 8., 0., 0.]]) and now I only want to keep all lines from 2nd array pos with those indices that are nonzero in the mask, i.e. line 3-6 (pos[2]-pos[5]). F.e. line 4 in pos has the values (5, 4) and mask[5][4] is nonzero, so I want to keep it. While line 2 (pos[1]) has the values (4, 6) and mask[4][6] is zero, so shall be discarded. I want to avoid a for loop (if possible!!!) cause I think (but don't know) numpy array are handled in another way. I think numpy.delete is the right function for discarding the values, but I don't know how to build the indices. Maybe someone can help me with that or suggest which way to do this Sebastian From Frank.Aune at broadpark.no Wed Jul 1 11:02:20 2009 From: Frank.Aune at broadpark.no (Frank Aune) Date: Wed, 1 Jul 2009 17:02:20 +0200 Subject: logging module and binary strings Message-ID: <200907011702.21450.Frank.Aune@broadpark.no> Hello, ---- snip ---- >>> import logging >>> logging.basicConfig(level=logging.DEBUG) >>> x='\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>> x '\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>> print x ????? >>> logging.info(x) Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit stream.write(fs % msg.encode("UTF-8")) UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 10: ordinal not in range(128) >>> logging.info(x, extra={'encoding':'utf-8'}) Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit stream.write(fs % msg.encode("UTF-8")) UnicodeDecodeError: 'utf8' codec can't decode byte 0xfe in position 10: unexpected code byte ---- snip ---- Is there any way to log the above value of x "properly" using the python logging module? By properly I mean logging the value displayed when entering just x in the python shell and pressing enter. Regards, Frank From eric.pruitt at gmail.com Wed Jul 1 11:17:31 2009 From: eric.pruitt at gmail.com (Eric Pruitt) Date: Wed, 1 Jul 2009 10:17:31 -0500 Subject: Bytes, Strings, Encoding Message-ID: <171e8a410907010817j715f1d5ye638ea4b2469afd9@mail.gmail.com> Hello, I am working on the subprocess.Popen module for Python 2.7 and am now moving my changes over to Python 3.1 however I am having trouble with the whole byte situation and I can't quite seem to understand how to go back and forth between bytes and strings. I am also looking for the Python 3k equivalent for the Python 2.X built-in buffer class. One version of the file with my modifications can be found here < http://code.google.com/p/subprocdev/source/browse/subprocess.py?spec=svn5b570f8cbfcaae859091eb01b21b183aa5221af9&r=5b570f8cbfcaae859091eb01b21b183aa5221af9>. Lines 1 - 15 are me attempting to get around certain changes between Python 3.0 and Python 2.7. Further down on line 916, we have the function "send" and "recv" in which I am having the most trouble with bytes and strings. Any help is appreciated. Feel free to comment on my blog http://subdev.blogspot.com/ or reply to the last. Thanks in advance, Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: From Scott.Daniels at Acm.Org Wed Jul 1 11:38:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 01 Jul 2009 08:38:58 -0700 Subject: invoking a method from two superclasses In-Reply-To: References: Message-ID: > Mitchell L Model wrote: Sorry, after looking over some other responses, I went back and re-read your reply. I'm just making sure here, but: > Scott David Daniels wrote: Below compressed for readability in comparison: >> class A: >> def __init__(self): super().__init__(); print('A') >> class B: >> def __init__(self): super().__init__(); print('B') >> class C(A, B): >> def __init__(self): super().__init__(); print('C') >> C() >> And, if you are doing it with a message not available in object: Renamed to disambiguate later discussion >> class root: >> def prints(self): print('root') # or pass if you prefer >> class D(root): >> def prints(self): super().prints(); print('D') >> class E(root): >> def prints(self): super().prints(); print('E') >> class F(D, E): >> def prints(self): super().prints(); print('F') >> F().prints() > What I was missing is that each path up to and including the top of the diamond > must include a definition of the method, along with super() calls to move the method > calling on its way up. Actually, not really true. In the F through root example, any of the "prints" methods except that on root may be deleted and the whole thing works fine. The rootward (closer to object) end must contain the method in question, possibly only doing a pass as the action, and _not_ calling super. The other methods (those in D, E, and F above are all optional (you can freely comment out those methods where you like), but each should call super() in their bodies. Note that you can also add a class: class G(E, D): def prints(self): super().prints(); print('G') G().prints() Also note that the inheritances above can be "eventually inherits from" as well as direct inheritance. > Is this what the documentation means by "cooperative multiple inheritance"? Yes, the phrase is meant to convey "No magic (other than super itelf) is involved in causing the other methods to be invoked. If you want "all" prints methods called, make sure all but the last method do super calls. Of course, any method that doesn't do the super call will be the last by definition (no flow control then), but by putting the non-forwarding version at or below the lower point of the diamond, the mro order guarantees that you will have a "good" place to stop. Think of the mro order as a solution to the partial order constraints that a class must appear before any of its direct superclasses, and (by implication) after any of its subclasses. > If your correction of my example, if you remove super().__init__ from B.__init__ > the results aren't affected, because object.__init__ doesn't do anything and > B comes after A in C's mro. However, if you remove super().__init__ from > A.__init__, it stops the "supering" process dead in its tracks. Removing the super from B.__init__ means that you don't execute object.__init__. It turns out that object does nothing in its __init__, but without knowing that, removing the super from B.__init__ is also a mistake. So, you may well already have it all right, but as long as I'm putting in the effort to get the "operational rules about using super" out, I thought I'd fill in this last little bit. --Scott David Daniels Scott.Daniels at Acm.Org From jenn.duerr at gmail.com Wed Jul 1 11:45:40 2009 From: jenn.duerr at gmail.com (noydb) Date: Wed, 1 Jul 2009 08:45:40 -0700 (PDT) Subject: string character count References: <58ceecd5-6d2c-44a9-8ef9-01feac5792f4@b9g2000yqm.googlegroups.com> Message-ID: <54b554d3-c03e-40de-a02d-a176d0583cea@z28g2000vbl.googlegroups.com> thanks everyone for all the ideas -- simple stuff, I know for you all, but very helpful for me. From jcd at sdf.lonestar.org Wed Jul 1 11:48:23 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Wed, 01 Jul 2009 11:48:23 -0400 Subject: It's ... In-Reply-To: References: Message-ID: <1246463303.11768.0.camel@aalcdl07> On Tue, 2009-06-30 at 13:24 -0700, Beni Cherniavsky wrote: > On Jun 24, 11:40 pm, "J. Cliff Dyer" wrote: > > Also note that you can iterate over a file several times: > > > > f = open('foo.txt') > > for line in f: > > print line[0] # prints the first character of every line > > for line in f: > > print line[1] #prints the second character of every line > > > No, you can't. The second loop prints nothing! > A file by default advances forward. Once you reach the end, you stay > there. You are, of course, absolutely right. Sorry for the misinformation. :( From Scott.Daniels at Acm.Org Wed Jul 1 11:49:31 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 01 Jul 2009 08:49:31 -0700 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: Gabriel Genellina wrote: > ... > def convert(in_queue, out_queue): > while True: > row = in_queue.get() > if row is None: break > # ... convert row > out_queue.put(converted_line) These loops work well with the two-argument version of iter, which is easy to forget, but quite useful to have in your bag of tricks: def convert(in_queue, out_queue): for row in iter(in_queue.get, None): # ... convert row out_queue.put(converted_line) --Scott David Daniels Scott.Daniels at Acm.Org From petshmidt at googlemail.com Wed Jul 1 12:05:37 2009 From: petshmidt at googlemail.com (someone) Date: Wed, 1 Jul 2009 09:05:37 -0700 (PDT) Subject: =?windows-1252?Q?getting_rid_of__=97?= Message-ID: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Hello, how can I replace '?' sign from string? Or do split at that character? Getting unicode error if I try to do it: UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 1: ordinal not in range(128) Thanks, Pet script is # -*- coding: UTF-8 -*- From michele.simionato at gmail.com Wed Jul 1 12:08:55 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Wed, 1 Jul 2009 09:08:55 -0700 (PDT) Subject: invoking a method from two superclasses References: Message-ID: <8bc6afc1-21fc-40dd-ba7d-f0d549a7de93@24g2000yqm.googlegroups.com> On Jul 1, 2:34?am, Mitchell L Model wrote: > I suspect we should have a Multiple Inheritance HOWTO, though details and > recommendations would be controversial. I've accumulated lots of abstract > examples along the lines of my question, using multiple inheritance both to > create combination classes (the kinds that are probably best done with > composition instead of inheritance) and mixins. I like mixins, and I like abstract > classes. And yes I understand the horrors of working with a large component > library that uses mixins heavily, because I've experienced many of them, going > all the way back to Lisp-Machine Lisp's window system with very many combo > classes such as FancyFontScrollingTitledMinimizableWindow, or whatever. > Also, I understand that properties might be better instead of multiple inheritance > for some situations. What I'm trying to do is puzzle out what the reasonable uses > of multiple inheritance are in Python 3 and how classes and methods that follow > them should be written. You may be interest in my "Things to know about super": http://www.artima.com/weblogs/viewpost.jsp?thread=236275 http://www.artima.com/weblogs/viewpost.jsp?thread=236278 http://www.artima.com/weblogs/viewpost.jsp?thread=237121 From robert.kern at gmail.com Wed Jul 1 12:17:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 01 Jul 2009 11:17:31 -0500 Subject: Making code run in both source tree and installation path In-Reply-To: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> References: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> Message-ID: On 2009-07-01 01:04, Carl Banks wrote: > On Jun 29, 9:20 am, Javier Collado wrote: >> I've seen different approaches: >> - distutils trick in setup.py to modify the installed script (i.e. >> changing a global variable value) so that it has a reference to the >> data files location. > > > One of my biggest complaints about distutils is that it doesn't do > this, a limitation made worse by the fact that distutils allows you to > specify an alternate data file directory, but your scripts have no way > to know what that alternate directory is installed. Which really > limits the usefulness of that functionality. > > The most common way I've seen people work around this issue is to > throw their data files into the package directories. Yuck. Huh. I always found that to be a more elegant solution than hardcoding the data location into the program at install-time. -- 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 python at mrabarnett.plus.com Wed Jul 1 12:18:24 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 01 Jul 2009 17:18:24 +0100 Subject: Basic question from pure beginner In-Reply-To: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: <4A4B8C50.4010202@mrabarnett.plus.com> sato.photo at gmail.com wrote: > I have been going through some Python Programming exercises while > following the MIT OpenCourseWare Intro to CS syllabus and am having > some trouble with the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password > three times, but can't get it to quit right away after the correct one > is entered. I know this is really basic, please forgive me. I have > no programming experience and have just started going through these > tutorials. > > My code is here: > > http://python.pastebin.com/m6036b52e > raw_input() returns a string, so you don't need "guess = str(guess)". The 'if' line should end with a colon. Incrementing the count should probably be after the 'if' statement. You can leave a loop prematurely with the 'break' statement. From mail at timgolden.me.uk Wed Jul 1 12:20:38 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 01 Jul 2009 17:20:38 +0100 Subject: No trees in the stdlib? In-Reply-To: References: Message-ID: <4A4B8CD6.6050606@timgolden.me.uk> Lawrence D'Oliveiro wrote: > Want sorted order? > > sorted(tuple(the_set)) Not sure why you want the tuple () there, but you probably knew that... TJG From python at mrabarnett.plus.com Wed Jul 1 12:21:54 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 01 Jul 2009 17:21:54 +0100 Subject: No trees in the stdlib? In-Reply-To: References: Message-ID: <4A4B8D22.3060308@mrabarnett.plus.com> Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > >> Simple example usage case: Insert string into data structure in sorted >> order if it doesn't exist, else retrieve it. > > the_set = set( ... ) > > if str in the_set : > ... "retrieval" case ... > else : > the_set.add(str) > #end if > > Want sorted order? > > sorted(tuple(the_set)) > > What could be simpler? > Why not just "sorted(the_set)"? From charles at declareSub.com Wed Jul 1 12:26:41 2009 From: charles at declareSub.com (Charles Yeomans) Date: Wed, 1 Jul 2009 12:26:41 -0400 Subject: Basic question from pure beginner In-Reply-To: <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <1A63D369-87EF-4429-8C05-C1579C9BE71C@declareSub.com> Let me offer a bit of editing. First, using the condition count != 3 is perhaps risky. A mistake or a change in logic in the loop body might result in an infinite loop. So instead I suggest while count < 3... Second, I'd suggest storing the value 3 in a variable with a name that describes it. MaxAttempts = 3 while count < MaxAttempts This suggests that you change the name 'count' to 'attemptcount'. The variable 'guess' is used only inside the loop, so I suggest removing the assignment outside the loop. Finally, I'd remove correct_password_given from the loop test, and replace it with a break statement when the correct password is entered. password = "qwerty" correct_password_given = False attemptcount = 0 MaxAttempts = 3 while attemptcount < MaxAttempts: guess = raw_input("Enter your password: ") guess = str(guess) if guess != password: print "Access Denied" attemptcount = attemptcount + 1 else: print "Password Confirmed" correct_password_given = True break Charles Yeomans On Jul 1, 2009, at 3:58 AM, sato.photo at gmail.com wrote: > Thank you for all of the help. With your assistance and help from the > Python Tutor mailing list I was able to come up with the following > code: > > password = "qwerty" > correct_password_given = False > guess = "0" > count = 0 > while count != 3 and not correct_password_given : > guess = raw_input("Enter your password: ") > guess = str(guess) > if guess != password: > print "Access Denied" > count = count + 1 > else: > print "Password Confirmed" > correct_password_given = True > > > If I understand it correctly, the function will continue to loop until > either the count == 3 or until correct_password_give = True, > satisfying the two conditions of the assignment, which were to lock a > user out after three failed password attempts and to print "Access > Granted" and end the module if the correct password is given. Am I > understanding this correctly? > > Thanks! > > On Jul 1, 12:06 am, alex23 wrote: >> On Jul 1, 3:38 pm, "sato.ph... at gmail.com" >> wrote: >> >>> I have been able to make the module quit after entering a password >>> three times, but can't get it to quit right away after the correct >>> one >>> is entered. >> >> Not with the code you pasted, you haven't. There's a missing colon on >> line 7 & line 9 isn't indented properly. It's always best if we're >> referring to the same source when trying to help with a problem. >> >> The problem you're having, though, has to do with the while loop and >> the fact that it's only checking one condition: has the 'count' >> counter been incremented to 3. What you need to do is _also_ check >> for >> the success condition: >> >> is_confirmed = False >> while count != 3 or is_confirmed: >> guess = raw_input("Enter your password: ") >> guess = str(guess) >> if guess != password: >> print "Access Denied" >> count = count + 1 >> else: >> print "Password Confirmed" >> is_confirmed = True >> >> This also provides you with a nice boolean that shows whether or not >> the password was confirmed, which may be useful for latter code. >> >> However, whenever you want to loop a set number of times, it's >> usually >> better to use a 'for' loop instead: >> >> PASSWORD = 'qwerty' >> MAXRETRY = 3 >> for attempt in xrange(MAXRETRY): >> guess = str(raw_input('Enter your password: ')) >> is_complete = guess == PASSWORD >> if is_complete: >> print 'Password confirmed' >> break # this exits the for loop >> else: >> print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) >> >> This saves you from the burden of creating, incrementing & testing >> your own counter. >> >> If there's anything here that isn't clear, please don't hesitate to >> ask. > > -- > From bdesth.quelquechose at free.quelquepart.fr Wed Jul 1 12:39:18 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 18:39:18 +0200 Subject: Getting input the scanf way In-Reply-To: References: Message-ID: <4a4bacd3$0$296$426a74cc@news.free.fr> Mr.SpOOn a ?crit : > Hi, > I need to do some kind of interactive command line program. > > I mean: I run the program, it asks me for input, I type something and > then I get the output or other questions. > I'm not sure what is the right way to achieve this. Simplest : use raw_input() Friendlier (at least on *n*x platforms) : use readline. HTH From bdesth.quelquechose at free.quelquepart.fr Wed Jul 1 12:43:44 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 18:43:44 +0200 Subject: Basic question from pure beginner In-Reply-To: References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <4a4badde$0$10226$426a74cc@news.free.fr> Charles Yeomans a ?crit : Please don't top-post (not corrected) > Let me offer a bit of editing. > > First, using the condition count != 3 is perhaps risky. A mistake or a > change in logic in the loop body might result in an infinite loop. So > instead I suggest > > while count < 3... > > Second, I'd suggest storing the value 3 in a variable with a name that > describes it. Pretty sensible suggestion, but then: > MaxAttempts = 3 follow conventions instead : MAX_ATTEMPS = 3 (snip) > Finally, I'd remove correct_password_given from the loop test, and > replace it with a break statement when the correct password is entered. Then using a while loop and manually incrementing a counter is a waste of time - using a for loop would be simplier and less error-prone. From bdesth.quelquechose at free.quelquepart.fr Wed Jul 1 12:45:26 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 18:45:26 +0200 Subject: Basic question from pure beginner In-Reply-To: References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <4a4bae44$0$10226$426a74cc@news.free.fr> Scott David Daniels a ?crit : (snip) > And even simpler: > PASSWORD = "qwerty" > MAXRETRY = 3 > for attempt in range(MAXRETRY): > if raw_input('Enter your password: ') == PASSWORD: > print 'Password confirmed' > break # this exits the for loop > print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) > else: > # The else for a for statement is not executed for breaks, > # So indicates the end of testing without a match > raise SystemExit # Or whatever you'd rather do. Which is probably the most pythonic implementation. From cmpython at gmail.com Wed Jul 1 13:24:27 2009 From: cmpython at gmail.com (Che M) Date: Wed, 1 Jul 2009 10:24:27 -0700 (PDT) Subject: Ubigraph vs Matplotlib (dynamic plotting, event handling) References: <300620092320590209%shaibani@ymail.com> Message-ID: <307b4075-ec5b-4d06-a638-c62f38307e7e@q11g2000yqi.googlegroups.com> On Jun 30, 6:20?pm, Ala wrote: > Hello everyone, > > I intend to use python for some network graph plotting, with event > handling (clicking on network nodes, zooming in/out etc..) and so far I > have come across two good candidates which are Matplotlib and Ubigraph. > > Did anyone have any experience with either of them for dynamic plotting > (a slider bar on a Qt interface causing the graph to be replotted for > each value for example), as well as event handling? (it seems on first > notice that Ubigraph will have an upperhand on that), as well as event > handling such as mouse clicks? (on this one Matplotlib has good > documentation showing it does achieve that while I find ubigraph's > documentation lacking, but I'd preffere to have the opinion of those > who have used them before). > > Thank you. I have used Matplotlib for both uses. It has a built-in toolbar that allows for changing the plot (zoom, pan, etc), and has good support for point picking (clicking on a datapoint to do send an event). You could probably connect the QT slider to the zoom function without much trouble, and Matplotlib is embeddable in QT GUIs. From andrew at acooke.org Wed Jul 1 13:42:06 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Jul 2009 13:42:06 -0400 (CLT) Subject: Suppressing Implicit Chained Exceptions (Python 3.0) Message-ID: <59a8ef2bf270d290bc5ef5ee98ceb717.squirrel@acooke.dyndns.org> I have some (library) code where an exception is caught and, since the underlying cause is rather obscure, a different exception is raised that more clearly explains the issue to the caller. However, when printed via format_exc(), this new exception still has the old exception attached via the mechanism described at http://www.python.org/dev/peps/pep-3134/ (this is Python 3.0). Is there any simple way to stop this? It's rather annoying and misleading, as it exposes a lot of internal detail that the caller does not understand or want. This is marked as an "open issue" in the PEP described above. Thanks, Andrew From __peter__ at web.de Wed Jul 1 13:44:56 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 01 Jul 2009 19:44:56 +0200 Subject: logging module and binary strings References: Message-ID: Frank Aune wrote: >>>> import logging >>>> logging.basicConfig(level=logging.DEBUG) >>>> x='\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> x > '\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> logging.info(x) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 10: > ordinal not in range(128) >>>> logging.info(x, extra={'encoding':'utf-8'}) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'utf8' codec can't decode byte 0xfe in position 10: > unexpected code byte > Is there any way to log the above value of x "properly" using the python > logging module? By properly I mean logging the value displayed when > entering just x in the python shell and pressing enter. How about logging.info(repr(x))? Peter From Scott.Daniels at Acm.Org Wed Jul 1 13:47:47 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 01 Jul 2009 10:47:47 -0700 Subject: Basic question from pure beginner In-Reply-To: References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: Charles Yeomans wrote: > Let me offer a bit of editing.... > Finally, I'd remove correct_password_given from the loop test, and > replace it with a break statement when the correct password is entered. > > password = "qwerty" > correct_password_given = False > attemptcount = 0 > MaxAttempts = 3 > while attemptcount < MaxAttempts: > guess = raw_input("Enter your password: ") > guess = str(guess) > if guess != password: > print "Access Denied" > attemptcount = attemptcount + 1 > else: > print "Password Confirmed" > correct_password_given = True > break And even simpler: PASSWORD = "qwerty" MAXRETRY = 3 for attempt in range(MAXRETRY): if raw_input('Enter your password: ') == PASSWORD: print 'Password confirmed' break # this exits the for loop print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) else: # The else for a for statement is not executed for breaks, # So indicates the end of testing without a match raise SystemExit # Or whatever you'd rather do. --Scott David Daniels Scott.Daniels at Acm.Org From no.email at please.post Wed Jul 1 13:56:55 2009 From: no.email at please.post (kj) Date: Wed, 1 Jul 2009 17:56:55 +0000 (UTC) Subject: Why re.match()? Message-ID: For a recovering Perl-head like me it is difficult to understand why Python's re module offers both match and search. Why not just use search with a beginning-of-string anchor? I find it particularly puzzling because I have this (possibly mistaken) idea that the Python design philosophy tends towards minimalism, a sort of Occam's razor, when it comes to language entities; i.e. having re.match along with re.search seems to me like an "unnecessary multiplication of entities". What am I missing? TIA! kj From timmyt at smarttypes.org Wed Jul 1 13:58:33 2009 From: timmyt at smarttypes.org (timmyt) Date: Wed, 1 Jul 2009 10:58:33 -0700 (PDT) Subject: a little wsgi framework Message-ID: i'm interested in getting opinions on a small wsgi framework i assembled from webob, sqlalchemy, genshi, and various code fragments i found on the inter-tubes here is the interesting glue - any comments / suggestions would be much appreciated ------------------ the wsgi app ------------------ def application(environ, start_response): path = environ.get('PATH_INFO', '').lstrip('/') for regex, callback in urls: match = re.search(regex, path) if match: environ['myapp.url_args'] = match.groups() request = webob.Request(environ) try: return callback(request, start_response) except Exception, ex: start_response('500 Internal Server Error', [('Content- Type', 'text/plain')]) return [traceback.format_exc()] start_response('404 Not Found', [('Content-Type', 'text/plain')]) return ["Couldn't find the URL specified."] ---------------------------------- the controller decorator ---------------------------------- def web_decorator(filename, method='html'): def decorator(target): def wrapper(request, start_response): #genshi TemplateLoader template = loader.load(filename) global config try: return_dict = target(request, start_response) return_string = template.generate(**return_dict).render (method) config['database.Session'].commit() except: config['database.Session'].rollback() raise finally: config['database.Session'].remove() #TODO: alter 'Content-Type' per method being passed start_response('200 OK', [('Content-Type', 'text/html')]) return [return_string] return wrapper return decorator From lists at cheimes.de Wed Jul 1 14:01:55 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 01 Jul 2009 20:01:55 +0200 Subject: A question about fill_free_list(void) function In-Reply-To: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Message-ID: Pedram schrieb: > Hello community, > I'm reading the CPython interpreter source code, > first, if you have something that I should know for better reading > this source code, I would much appreciate that :) > second, in intobject.c file, I read the following code in > fill_free_list function that I couldn't understand: > while (--q > p) > Py_TYPE(q) = (struct _typeobject *)(q-1); > Py_TYPE(q) = NULL; > > What does it mean? The code is abusing the ob_type member to store a single linked, NULL terminated list of free integer objects. It's a tricky optimization you don't have to understand in order to understand the rest of the code. And no, the code in ctypes.h is totally unrelated to the free list in intobject.c. Christian From robert.kern at gmail.com Wed Jul 1 14:33:53 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 01 Jul 2009 13:33:53 -0500 Subject: deleting certain entries in numpy array In-Reply-To: <4a4b7763$1@news.fhg.de> References: <4a4b7763$1@news.fhg.de> Message-ID: On 2009-07-01 09:51, Sebastian Schabe wrote: > Hello everybody, > > I'm new to python and numpy and have a little/special problem: You will want to ask numpy questions on the numpy mailing list. http://www.scipy.org/Mailing_Lists > I have an numpy array which is in fact a gray scale image mask, e.g.: > > mask = > array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 255, 255, 255, 0, 0, 255, 0], > [ 0, 0, 255, 255, 255, 0, 0, 255, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) > > and I have another array of (y, x) positions in that mask (first two > values of each row): > > pos = > array([[ 3., 2., 0., 0.], > [ 3., 4., 0., 0.], > [ 5., 2., 0., 0.], > [ 5., 4., 0., 0.], > [ 6., 2., 0., 0.], > [ 6., 7., 0., 0.], > [ 0., 0., 0., 0.], > [ 8., 8., 0., 0.]]) > > and now I only want to keep all lines from 2nd array pos with those > indices that are nonzero in the mask, i.e. line 3-6 (pos[2]-pos[5]). > > F.e. line 4 in pos has the values (5, 4) and mask[5][4] is nonzero, so I > want to keep it. While line 2 (pos[1]) has the values (4, 6) and > mask[4][6] is zero, so shall be discarded. > > I want to avoid a for loop (if possible!!!) cause I think (but don't > know) numpy array are handled in another way. I think numpy.delete is > the right function for discarding the values, but I don't know how to > build the indices. First, convert the pos array to integers, and just the columns with indices in them: ipos = pos[:,:2].astype(int) Now check the values in the mask corresponding to these positions: mask_values = mask[ipos[:,0], ipos[:,1]] Now extract the rows from the original pos array where mask_values is nonzero: result = pos[mask_values != 0] -- 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 backup95 at netcabo.pt Wed Jul 1 14:45:26 2009 From: backup95 at netcabo.pt (=?UTF-8?B?Sm/Do28gVmFsdmVyZGU=?=) Date: Wed, 01 Jul 2009 19:45:26 +0100 Subject: No trees in the stdlib? In-Reply-To: References: Message-ID: <4A4BAEC6.1060109@netcabo.pt> Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > > >> Simple example usage case: Insert string into data structure in sorted >> order if it doesn't exist, else retrieve it. >> > > the_set = set( ... ) > > if str in the_set : > ... "retrieval" case ... > else : > the_set.add(str) > #end if > > Want sorted order? > > sorted(tuple(the_set)) > > What could be simpler? > > Try putting that inside a loop with thousands of iterations and you'll see what the problem is. From duncan.booth at invalid.invalid Wed Jul 1 14:52:34 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 1 Jul 2009 18:52:34 GMT Subject: Why re.match()? References: Message-ID: kj wrote: > > > For a recovering Perl-head like me it is difficult to understand > why Python's re module offers both match and search. Why not just > use search with a beginning-of-string anchor? I find it particularly > puzzling because I have this (possibly mistaken) idea that the > Python design philosophy tends towards minimalism, a sort of Occam's > razor, when it comes to language entities; i.e. having re.match > along with re.search seems to me like an "unnecessary multiplication > of entities". What am I missing? > RTFM? http://docs.python.org/library/re.html#matching-vs-searching says: > Note that match may differ from search even when using a regular > expression beginning with '^': '^' matches only at the start of the > string, or in MULTILINE mode also immediately following a newline. The > ?match? operation succeeds only if the pattern matches at the start of > the string regardless of mode, or at the starting position given by > the optional pos argument regardless of whether a newline precedes > it. So, for example: >>> re.compile("c").match("abcdef", 2) <_sre.SRE_Match object at 0x0000000002C09B90> >>> re.compile("^c").search("abcdef", 2) >>> The ^ anchors you to the start of the string (or start of line in multiline mode) even if you specify a non-zero position. The match method just tells you if the pattern matches at the specified starting position regardless of whether it is the start of the string. From damienlmoore at gmail.com Wed Jul 1 15:30:51 2009 From: damienlmoore at gmail.com (spillz) Date: Wed, 1 Jul 2009 12:30:51 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: On Jun 29, 3:15?pm, Pascal Chambon wrote: > Hello everyone > > I've had real issues with subprocesses recently : from a python script, > on windows, I wanted to "give control" to a command line utility, i.e > forward user in put to it and display its output on console. It seems > simple, but I ran into walls : If you are willing to have a wxPython dependency, wx.Execute handles non-blocking i/o with processes on all supported platforms more discussion of python's blocking i/o issues here: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a037349e18f99630/60886b8beb55cfbc?q=#60886b8beb55cfbc From melissa at corsairsolutions.com Wed Jul 1 15:57:35 2009 From: melissa at corsairsolutions.com (Melissa Powers) Date: Wed, 1 Jul 2009 15:57:35 -0400 Subject: Python 2.5 incompatible with Fedora Core 6 - packaging problems again Message-ID: <51BDF10DBC00564DBBBE62B537CF53922B9ADD@csi-svr1.csi.local> Hi, This may be a stretch but I came across this blog while searching for a SW Packaging Engineer. Do you know anyone in the Boston, MA area who may be interested in this position? The company is a start up with a lot of funding and this is an opportunity to define the environment and grow with a promising company. Please4= forward to anyone you may know of... SW Packaging Engineer * Python developer * Red Hat Applications expert (Fedora 10) * They want a real Red Hat Linux expert * Custom Build Distribution * Custom RPM's * Some Linux Server administration duties * Virtualization experience a big plus Melissa Frechette Powers Account Manager Corsair Solutions, Inc. 2 New Pasture Rd Unit 1 Newburyport, MA 01950 978-465-0085 x 206 Phone 978-465-0068 Fax http://www.corsairsolutions.com Please visit our NEW website: www.corsairsolutions.com The documents included with this electronic mail transmission contain information from the staffing firm of Corsair Solutions, Inc which is confidential and/or privileged. This information is intended to be for the use of the addressee only. Note that any disclosure, printing, photocopying, distribution or use of the contents of this e-mailed information by persons other than the addressee or an agent of the addressee, is unauthorized and prohibited. If you have received this electronic mail in error, please notify us via electronic mail reply to the sender or by telephone (collect 877-626-7724) immediately. -------------- next part -------------- An HTML attachment was scrubbed... URL: From stef.mientki at gmail.com Wed Jul 1 16:08:13 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Wed, 01 Jul 2009 22:08:13 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? Message-ID: <4A4BC22D.70502@gmail.com> hello, I need to add an object's name to the global namespace. The reason for this is to create an environment, where you can add some kind of math environment, where no need for Python knowledge is needed. The next statement works, but I'm not sure if it will have any dramatical side effects, other than overruling a possible object with the name A def some_function ( ...) : A = object ( ...) sys._getframe(1).f_globals [ Name ] = A thanks, Stef Mientki From ben+python at benfinney.id.au Wed Jul 1 16:16:43 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 02 Jul 2009 06:16:43 +1000 Subject: deleting certain entries in numpy array References: <4a4b7763$1@news.fhg.de> Message-ID: <87fxdgtaz8.fsf@benfinney.id.au> Sebastian Schabe writes: > I want to avoid a for loop (if possible!!!) cause I think (but don't > know) numpy array are handled in another way. Yes, Numpy arrays can be indexed logically by a boolean array. > I think numpy.delete is the right function for discarding the values, > but I don't know how to build the indices. You don't need to discard the values. You can get a new array which is a filtered version of an existing array, by using an array of bool values as the index to the old array:: >>> import numpy >>> mask = numpy.array([ ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 255, 255, 255, 0, 0, 255, 0], ... [ 0, 0, 255, 255, 255, 0, 0, 255, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... ], dtype=numpy.uint8) >>> mask > 0 array([[False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, True, True, True, False, False, True, False], [False, False, True, True, True, False, False, True, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False]], dtype=bool) >>> mask[mask > 0] array([255, 255, 255, 255, 255, 255, 255, 255], dtype=uint8) However, your case is somewhat more tricky: you need to construct the boolean array based on coordinates from a separate array. That doesn't require a for loop statement, but AFAICT it does require manually generating the array of bools. I've done it with a list comprehension:: >>> pos = numpy.array([ ... [ 3., 2., 0., 0.], ... [ 3., 4., 0., 0.], ... [ 5., 2., 0., 0.], ... [ 5., 4., 0., 0.], ... [ 6., 2., 0., 0.], ... [ 6., 7., 0., 0.], ... [ 0., 0., 0., 0.], ... [ 8., 8., 0., 0.], ... ]) >>> pos[numpy.array( ... [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]])] array([[ 5., 2., 0., 0.], [ 5., 4., 0., 0.], [ 6., 2., 0., 0.], [ 6., 7., 0., 0.]]) Here it is again, showing my working steps:: >>> pos[:, 0:2] array([[ 3., 2.], [ 3., 4.], [ 5., 2.], [ 5., 4.], [ 6., 2.], [ 6., 7.], [ 0., 0.], [ 8., 8.]]) >>> [(int(x), int(y)) for (x, y) in pos[:, 0:2]] [(3, 2), (3, 4), (5, 2), (5, 4), (6, 2), (6, 7), (0, 0), (8, 8)] >>> [mask[int(x), int(y)] for (x, y) in pos[:, 0:2]] [0, 0, 255, 255, 255, 255, 0, 0] >>> [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]] [False, False, True, True, True, True, False, False] >>> numpy.array( ... [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]]) array([False, False, True, True, True, True, False, False], dtype=bool) >>> pos[numpy.array( ... [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]])] array([[ 5., 2., 0., 0.], [ 5., 4., 0., 0.], [ 6., 2., 0., 0.], [ 6., 7., 0., 0.]]) -- \ ?Holy knit one purl two, Batman!? ?Robin | `\ | _o__) | Ben Finney From Digvijoy.C at gmail.com Wed Jul 1 16:20:03 2009 From: Digvijoy.C at gmail.com (digz) Date: Wed, 1 Jul 2009 13:20:03 -0700 (PDT) Subject: problems displaying results in ibm_db Message-ID: This simple db connectivity and select test program works in the interactive mode >>> import ibm_db >>> conn = ibm_db.connect( 'DATABASE', 'user', 'passwd') >>> result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST 1 ROWS ONLY') >>> row = ibm_db.fetch_tuple( result ) >>> for r in row: ... print r ... 13 ABC 4 2009-05-11 The same executed from a script using python testdb.py does not yield anything , What am I doing wrong ? import ibm_db conn = ibm_db.connect( 'DATABASE', 'user', 'pwd') result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST 1 ROWS ONLY') row = ibm_db.fetch_tuple( result ) for r in row: print r == >python testdb.py database value after unicode conversion is : N database value sent to CLI is : N > == From psimon at sonic.net Wed Jul 1 17:16:18 2009 From: psimon at sonic.net (Paul Simon) Date: Wed, 1 Jul 2009 14:16:18 -0700 Subject: problem installing python 2.6.2 from tarball Message-ID: <4a4bd226$0$95520$742ec2ed@news.sonic.net> I have just finished going through the usual ./configure, make, etc. and now have a load of stuff in my home directory that I think doesn't belong there. Apparently I did the installation from my home directory (linux) and have a directory in my home directory "Python2.6.2" with subdirectories like "build", "Demo", "Doc", "Include", "Lib", etc. There are many files under /usr/bin/local/ which appear to be duplicates. This looks like a mess to me and would like some help to sort this out. Paul Simon From Mariosky at gmail.com Wed Jul 1 17:34:51 2009 From: Mariosky at gmail.com (Mario Garcia) Date: Wed, 1 Jul 2009 14:34:51 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order Message-ID: Im trying to use sets for doing statistics from a data set. I want to select, 70% random records from a List. I thougth set where a good idea so I tested this way: c = set(range(1000)) for d in range(1000): print c.pop() I was hoping to see a print out of random selected numbers from 1 to 1000 but I got an ordered count from 1 to 1000. I also tried using a dictionary, with keys from 1 to 10, and also got the keys in order. Im using: Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug 4 2008, 13:45:20) [GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin Examples in the documentation seem to work. But I cant make it. Can some one, give me a hint on whats going on? From lance.ellinghaus at hp.com Wed Jul 1 18:02:49 2009 From: lance.ellinghaus at hp.com (ELLINGHAUS, LANCE) Date: Wed, 1 Jul 2009 22:02:49 +0000 Subject: Using Python to set desktop background image under Windows XP In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <197A4D1DF629F64A9260E3B454A218D13403C48D2E@GVW1158EXB.americas.hpqcorp.net> Does anyone have any code that would allow setting the desktop background image under Windows XP? Thank you, lance Lance Ellinghaus mailto:lance.ellinghaus at hp.com From skip at pobox.com Wed Jul 1 18:07:16 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 1 Jul 2009 17:07:16 -0500 Subject: Timeout when connecting to sybase DBS In-Reply-To: <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> Message-ID: <19019.56852.751152.510037@montanaro.dyndns.org> Gil> Are you saying, that when you trying to connect to a sybase DBS Gil> server and the DBS or the server is down, you get an error after a Gil> few seconds and not after a few minutes? Yes, though thankfully our server tends to almost always be up. Gil> Which python version are you using? We're still running 2.4.5 at work and have a slightly hacked very old version of the python-sybase package (0.36). -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ when i wake up with a heart rate below 40, i head right for the espresso machine. -- chaos @ forums.usms.org From rhodri at wildebst.demon.co.uk Wed Jul 1 18:08:33 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 01 Jul 2009 23:08:33 +0100 Subject: Python/Pygame question In-Reply-To: References: Message-ID: On Tue, 30 Jun 2009 19:15:24 +0100, Crip wrote: > I have been experimenting with pygame. I would like to know how to go > about placing an image of my creation as the background of the > generated window. To where should I save the image, and what should it > be saved as? Wherever will be most useful to you (the same directory as where you are developing your game is often good). You can use most of the common formats, so pick whatever's most appropriate for your image. The documentation is at http://www.pygame.org/docs/ref/image.html In particular, you use pygame.image.load() to load your file into a surface, then blit it into your display. Some resizing may be necessary! -- Rhodri James *-* Wildebeest Herder to the Masses From skip at pobox.com Wed Jul 1 18:08:57 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 1 Jul 2009 17:08:57 -0500 Subject: Timeout when connecting to sybase DBS In-Reply-To: <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> Message-ID: <19019.56953.762133.210100@montanaro.dyndns.org> Gil> There's no such group as python-sybase :-( http://sourceforge.net/mailarchive/forum.php?forum_name=python-sybase-misc S From clp2 at rebertia.com Wed Jul 1 18:11:45 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 1 Jul 2009 15:11:45 -0700 Subject: Noob In-Reply-To: References: Message-ID: <50697b2c0907011511r6aac78d9m3f190c19b79ed832@mail.gmail.com> On Tue, Jun 30, 2009 at 11:02 AM, kaffeen wrote: > Hello all, just joined this list and am just beginning to learn Python. > Thanks to everyone for making this a great place to learn and their > contributions. > > As mentioned, I'm new to Python (but have experience with other programming > languages/scripting). I have a couple of questions... > 2) What are the differences between IPython and IDLE, is one better than the > other, why? IDLE is a GUI interactive interpreter and IDE that uses the Tk GUI toolkit. IPython is a command-line interactive interpreter with tab-completion, advanced command history functions, and other handy extras. I would say they have completely different purposes. IPython is a souped-up version of the command-line Python REPL, but doesn't include a text editor for writing a file of code. It is very good for experimenting and exploring with though. IDLE is a quite servicable free IDE. Cheers, Chris -- http://blog.rebertia.com From pavlovevidence at gmail.com Wed Jul 1 18:28:10 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 15:28:10 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> On Jul 1, 2:34?pm, Mario Garcia wrote: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): > ? ? ?print c.pop() > > I was hoping to see a print out of random selected numbers from 1 to > 1000 > but I got an ordered count from 1 to 1000. > I also tried using a dictionary, with keys from 1 to 10, and also got > the keys in order. > > Im using: > ?Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug ?4 2008, 13:45:20) > ?[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin > > Examples in the documentation seem to work. But I cant make it. > Can some one, give me a hint on whats going on? The keys in a dict or set are not in random order, but (more or less) they are in hash key order modulo the size of the hash. This neglects the effect of hash collisions. The hash code of an integer happens to the integer itself, so oftentimes a dict or set storing a sequence of integers will end up with keys in order, although it's not guaranteed to be so. Point it, it's unsafe to rely on *any* ordering behavior in a dict or set, even relatively random order. Instead, call random.shuffle() on the list, and iterate through that to get the elements in random order. Carl Banks From mensanator at aol.com Wed Jul 1 18:29:19 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 1 Jul 2009 15:29:19 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <51a36108-d3dc-4a03-9a85-84a5da9b541a@l31g2000yqb.googlegroups.com> On Jul 1, 4:34?pm, Mario Garcia wrote: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): > ? ? ?print c.pop() > > I was hoping to see a print out of random selected numbers from 1 to > 1000 > but I got an ordered count from 1 to 1000. > I also tried using a dictionary, with keys from 1 to 10, and also got > the keys in order. > > Im using: > ?Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug ?4 2008, 13:45:20) > ?[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin > > Examples in the documentation seem to work. But I cant make it. > Can some one, give me a hint on whats going on? Sets don't help. Try this: >>> import random >>> c = range(100) >>> c [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> random.shuffle(c) >>> c [78, 62, 38, 54, 12, 48, 24, 20, 8, 14, 1, 69, 49, 92, 41, 64, 17, 35, 88, 40, 73, 45, 5, 84, 96, 90, 98, 57, 51, 75, 99, 13, 29, 4, 97, 77, 74, 56, 91, 95, 59, 79, 89, 19, 9, 42, 31, 85, 86, 23, 27, 50, 6, 21, 15, 80, 3, 30, 87, 82, 16, 63, 2, 55, 37, 33, 10, 61, 93, 72, 60, 67, 44, 65, 11, 70, 52, 58, 47, 18, 36, 66, 94, 28, 22, 68, 32, 76, 53, 25, 83, 34, 26, 71, 39, 43, 7, 46, 0, 81] >>> for d in c: print c.pop(), 81 0 46 7 43 39 71 26 34 83 25 53 76 32 68 22 28 94 66 36 18 47 58 52 70 11 65 44 67 60 72 93 61 10 33 37 55 2 63 16 82 87 30 3 80 15 21 6 50 27 Notice that the numbers are coming out in the exact reverse order of the list, because you used .pop() without an index number. But it's ok in _THIS_ example, because the list was randomly shuffled before popping. From pavlovevidence at gmail.com Wed Jul 1 18:33:01 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 15:33:01 -0700 (PDT) Subject: Why re.match()? References: Message-ID: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> On Jul 1, 10:56?am, kj wrote: > For a recovering Perl-head like me it is difficult to understand > why Python's re module offers both match and search. ?Why not just > use search with a beginning-of-string anchor? ?I find it particularly > puzzling because I have this (possibly mistaken) idea that the > Python design philosophy tends towards minimalism, a sort of Occam's > razor, when it comes to language entities; i.e. having re.match > along with re.search seems to me like an "unnecessary multiplication > of entities". ?What am I missing? It always seemed redundant to me also (notwithstanding Duncan Booth's explanation of slight semantic differences). However, I find myself using re.match much more often than re.search, so perhaps in this case a "second obvious way" is justified. Carl Banks From aahz at pythoncraft.com Wed Jul 1 18:39:55 2009 From: aahz at pythoncraft.com (Aahz) Date: 1 Jul 2009 15:39:55 -0700 Subject: Problem with uuid package when embedding a python interpreter References: <7d5cfbf0-38d5-4505-a93a-f321d0da74b8@c36g2000yqn.googlegroups.com> Message-ID: In article <7d5cfbf0-38d5-4505-a93a-f321d0da74b8 at c36g2000yqn.googlegroups.com>, =?ISO-8859-1?Q?J=E9r=F4me_Fuselier?= wrote: > >I've tried to import a script in an embedded python intrepreter but >this script fails when it imports the uuid module. I have a >segmentation fault in Py_Finalize(). You may want to also ask on the capi-sig mailing list. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From mensanator at aol.com Wed Jul 1 18:47:08 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 1 Jul 2009 15:47:08 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <51a36108-d3dc-4a03-9a85-84a5da9b541a@l31g2000yqb.googlegroups.com> Message-ID: <6891d82f-9dc3-4f43-8490-c339edf85ad8@p23g2000vbl.googlegroups.com> On Jul 1, 5:29?pm, Mensanator wrote: > On Jul 1, 4:34?pm, Mario Garcia wrote: > > > > > > > Im trying to use sets for doing statistics from a data set. > > I want to select, 70% random records from a List. I thougth set where > > a good idea so I > > tested this way: > > > c = set(range(1000)) > > for d in range(1000): > > ? ? ?print c.pop() > > > I was hoping to see a print out of random selected numbers from 1 to > > 1000 > > but I got an ordered count from 1 to 1000. > > I also tried using a dictionary, with keys from 1 to 10, and also got > > the keys in order. > > > Im using: > > ?Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug ?4 2008, 13:45:20) > > ?[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin > > > Examples in the documentation seem to work. But I cant make it. > > Can some one, give me a hint on whats going on? > > Sets don't help. Try this: > > >>> import random > >>> c = range(100) > >>> c > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, > 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, > 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, > 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, > 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, > 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] > > >>> random.shuffle(c) > >>> c > > [78, 62, 38, 54, 12, 48, 24, 20, 8, 14, 1, 69, 49, 92, 41, 64, 17, 35, > 88, 40, 73, 45, 5, 84, 96, 90, 98, 57, 51, 75, 99, 13, 29, 4, 97, 77, > 74, 56, 91, 95, 59, 79, 89, 19, 9, 42, 31, 85, 86, 23, 27, 50, 6, 21, > 15, 80, 3, 30, 87, 82, 16, 63, 2, 55, 37, 33, 10, 61, 93, 72, 60, 67, > 44, 65, 11, 70, 52, 58, 47, 18, 36, 66, 94, 28, 22, 68, 32, 76, 53, > 25, 83, 34, 26, 71, 39, 43, 7, 46, 0, 81] > > >>> for d in c: Oops! You don't want to iterate through c while popping it. Your original for statement is what to use. > > ? ? ? ? print c.pop(), > > 81 0 46 7 43 39 71 26 34 83 25 53 76 32 68 22 28 94 66 36 18 47 58 52 > 70 11 65 44 67 60 72 93 61 10 33 37 55 2 63 16 82 87 30 3 80 15 21 6 > 50 27 > > Notice that the numbers are coming out in the exact reverse order > of the list, because you used .pop() without an index number. > But it's ok in _THIS_ example, because the list was randomly > shuffled before popping. From ben+python at benfinney.id.au Wed Jul 1 19:01:10 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 02 Jul 2009 09:01:10 +1000 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <87bpo4t3d5.fsf@benfinney.id.au> Mario Garcia writes: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): > print c.pop() > > I was hoping to see a print out of random selected numbers from 1 to > 1000 The ?set? and ?dict? types are unordered collections, but that doesn't have any implication of randomness. Rather, it means that the order of retrieval is not guaranteed to be predictable. You can't even predict that it'll be a random order; you can't predict (based on the Python code) *anything* about the order. Any appearance of predictable ordering is an unreliable artefact of the implementation and may change at any time, since the language explicitly disclaims any specific ordering to the items. For a random sequence, you want the ?random? module :: >>> import random >>> for n in (random.randint(0, 999) for _ in range(10)): ... print n ... 381 4 471 624 70 979 110 932 105 262 -- \ ?When in doubt tell the truth. It will confound your enemies | `\ and astound your friends.? ?Mark Twain, _Following the Equator_ | _o__) | Ben Finney From alex.lavoro.propio at gmail.com Wed Jul 1 19:18:13 2009 From: alex.lavoro.propio at gmail.com (Alex) Date: Wed, 1 Jul 2009 16:18:13 -0700 (PDT) Subject: Open Source RSS Reader in Python? Message-ID: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> I am looking for an open source RSS reader (desktop, not online) written in Python but in vain. I am not looking for a package but a fully functional software. Google: python "open source" (rss OR feeds) reader Any clue ? From tn.pablo at gmail.com Wed Jul 1 19:26:36 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Wed, 1 Jul 2009 18:26:36 -0500 Subject: problem installing python 2.6.2 from tarball In-Reply-To: <4a4bd226$0$95520$742ec2ed@news.sonic.net> References: <4a4bd226$0$95520$742ec2ed@news.sonic.net> Message-ID: On Wed, Jul 1, 2009 at 16:16, Paul Simon wrote: > I have just finished going through the usual ./configure, make, etc. and now > have a load of stuff in my home directory that I think doesn't belong there. > Apparently I did the installation from my home directory (linux) and have a > directory ?in my home directory "Python2.6.2" with subdirectories like > "build", "Demo", "Doc", "Include", "Lib", etc. ?There are many files under > /usr/bin/local/ ?which appear to be duplicates. > > This looks like a mess to me and would like some help to sort this out. > > Paul Simon Sounds like you untarred directly into your home dir. You're OK if you delete those, they are the sources, although you should do so only after uninstalling, just in case. To unistall, follow this thread from the list from a while ago: http://mail.python.org/pipermail/python-list/2004-December/296269.html I have followed it and it worked. Pablo Torres N. From tn.pablo at gmail.com Wed Jul 1 19:34:18 2009 From: tn.pablo at gmail.com (ptn) Date: Wed, 1 Jul 2009 16:34:18 -0700 (PDT) Subject: problems displaying results in ibm_db References: Message-ID: <052ecdf2-6253-4885-9f16-2f24062f1f07@g6g2000vbr.googlegroups.com> On Jul 1, 3:20?pm, digz wrote: > result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST > 1 ROWS ONLY') You have the same string split into two lines, which means that what you actually have is this: result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST 1 ROWS ONLY') You need to escape that little '\n' with a backslash: result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST\ 1 ROWS ONLY') Better yet, use implicit string concatenation: result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST' '1 ROWS ONLY') Note that now you have two strings: 'SELECT * FROM TEST FETCH FIRST' and '1 ROWS ONLY' Pablo Torres N. From tn.pablo at gmail.com Wed Jul 1 19:36:38 2009 From: tn.pablo at gmail.com (ptn) Date: Wed, 1 Jul 2009 16:36:38 -0700 (PDT) Subject: problem installing python 2.6.2 from tarball References: <4a4bd226$0$95520$742ec2ed@news.sonic.net> Message-ID: <9286d6b5-91bf-4a74-99ba-c7c90329d4aa@f33g2000vbm.googlegroups.com> Sounds like you untarred directly into your home dir. You're OK if you delete those, they are the sources, although you should do so only after uninstalling, just in case. To unistall, follow this thread from the list from a while ago: http://mail.python.org/pipermail/python-list/2004-December/296269.html I have followed it and it worked. From sajmikins at gmail.com Wed Jul 1 19:39:31 2009 From: sajmikins at gmail.com (Simon Forman) Date: Wed, 1 Jul 2009 16:39:31 -0700 (PDT) Subject: Open Source RSS Reader in Python? References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: On Jul 1, 7:18?pm, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? There's Canto: http://codezen.org/canto/ From python at mrabarnett.plus.com Wed Jul 1 19:50:16 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 00:50:16 +0100 Subject: Why re.match()? In-Reply-To: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> References: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> Message-ID: <4A4BF638.5060500@mrabarnett.plus.com> Carl Banks wrote: > On Jul 1, 10:56 am, kj wrote: >> For a recovering Perl-head like me it is difficult to understand >> why Python's re module offers both match and search. Why not just >> use search with a beginning-of-string anchor? I find it particularly >> puzzling because I have this (possibly mistaken) idea that the >> Python design philosophy tends towards minimalism, a sort of Occam's >> razor, when it comes to language entities; i.e. having re.match >> along with re.search seems to me like an "unnecessary multiplication >> of entities". What am I missing? > > It always seemed redundant to me also (notwithstanding Duncan Booth's > explanation of slight semantic differences). However, I find myself > using re.match much more often than re.search, so perhaps in this case > a "second obvious way" is justified. > re.match is anchored at a certain position, whereas re.search isn't. The re module doesn't support Perl's \G anchor. From robert.kern at gmail.com Wed Jul 1 19:51:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 01 Jul 2009 18:51:31 -0500 Subject: logging module and binary strings In-Reply-To: <200907011702.21450.Frank.Aune@broadpark.no> References: <200907011702.21450.Frank.Aune@broadpark.no> Message-ID: On 2009-07-01 10:02, Frank Aune wrote: > Hello, > > ---- snip ---- >>>> import logging >>>> logging.basicConfig(level=logging.DEBUG) >>>> x='\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> x > '\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> print x > ????? >>>> logging.info(x) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 10: > ordinal not in range(128) >>>> logging.info(x, extra={'encoding':'utf-8'}) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'utf8' codec can't decode byte 0xfe in position 10: > unexpected code byte > > ---- snip ---- > > Is there any way to log the above value of x "properly" using the python > logging module? By properly I mean logging the value displayed when entering > just x in the python shell and pressing enter. logging.info(repr(x)) Or, to be more descriptive in your logging message: logging.info("Got a binary value: %r", x) -- 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 benjamin at python.org Wed Jul 1 19:55:23 2009 From: benjamin at python.org (Benjamin Peterson) Date: Wed, 1 Jul 2009 23:55:23 +0000 (UTC) Subject: getting rid of =?utf-8?b?4oCU?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Message-ID: someone googlemail.com> writes: > > Hello, > > how can I replace '?' sign from string? Or do split at that character? > Getting unicode error if I try to do it: > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > 1: ordinal not in range(128) Please paste your code. I suspect that you are mixing unicode and normal strings. From python at mrabarnett.plus.com Wed Jul 1 19:56:22 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 00:56:22 +0100 Subject: getting rid of =?windows-1252?Q?=97?= In-Reply-To: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Message-ID: <4A4BF7A6.9010500@mrabarnett.plus.com> someone wrote: > Hello, > > how can I replace '?' sign from string? Or do split at that character? > Getting unicode error if I try to do it: > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > 1: ordinal not in range(128) > > > Thanks, Pet > > script is # -*- coding: UTF-8 -*- It sounds like you're mixing bytestrings with Unicode strings. I can't be any more helpful because you haven't shown the code. From python at mrabarnett.plus.com Wed Jul 1 20:26:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 01:26:09 +0100 Subject: Getting input the scanf way In-Reply-To: <8f67b6f80907010133t7f6e2d9eg9ffa3b927d540728@mail.gmail.com> References: <8f67b6f80907010133t7f6e2d9eg9ffa3b927d540728@mail.gmail.com> Message-ID: <4A4BFEA1.6020301@mrabarnett.plus.com> Mr.SpOOn wrote: > Hi, > I need to do some kind of interactive command line program. > > I mean: I run the program, it asks me for input, I type something and > then I get the output or other questions. > I'm not sure what is the right way to achieve this. > Use raw_input() and print. From benjamin.kaplan at case.edu Wed Jul 1 20:27:33 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 1 Jul 2009 20:27:33 -0400 Subject: What are the limitations of cStringIO.StringIO() ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: On Wed, Jul 1, 2009 at 7:48 AM, Barak, Ron wrote: > Hi, > > I think I'm up against a limitation in cStringIO.StringIO(), but could not > find any clues on the web, especially not in > http://docs.python.org/library/stringio.html. > > What I have is the following function: > > ??? def concatenate_files(self,filename_array): > ??????? import types > > ??????? f = cStringIO.StringIO() > ??????? for filename in filename_array: > ??????????? log_stream = LogStream(filename) > ??????????? if not isinstance(log_stream, types.NoneType): > ??????????????? try: > ??????????????????? string_ = log_stream.input_file.read() > ??????????????? except AttributeError, e: > ??????????????????????? sys.stderr.write("AttributeError: "+str(e)+"\n") > ??????????????????????? sys.stderr.write("log_stream: > "+str(log_stream)+"\n") > ??????????? else: > ??????????????? return(None) > > ??????????? f.write(string_) > ??????? return (f) > > And, when the list of files?- in filename_array - is pointing to long/large > enough files, I get the exception: > > Traceback (most recent call last): > ? File "svm_ts_tool_in_progress.py", line 244, in OnSelectCell > ??? self.InspectorViewController(row,col) > ? File "svm_ts_tool_in_progress.py", line 1216, in InspectorViewController > ??? self.InspectorePaneGetRecords(self.req_table, rec) > ? File "svm_ts_tool_in_progress.py", line 1315, in InspectorePaneGetRecords > ??? log_stream = > ConcatenatedLogStream(self.events_dict[req_table]["db_dict"].keys()) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 31, in > __init__ > ??? self.input_file = self.concatenate_files(filename_array) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 47, in > concatenate_files > ??? string_ = log_stream.input_file.read() > MemoryError > > Anyone knows whet's the limitation on cStringIO.StringIO() objects ? > Could you suggest a better way to create a string that contains the > concatenation of several big files ? > MemoryErrors are caused because, for whatever reason, the OS won't allocate any more memory for the process. For a 32-bit Windows process, that maximum is 2 GB. No matter what programming language or library you use, you can't bypass that limit. There is a way to up the limit to 3GB but I don't know how to do it. The link below has all the information about Windows memory limits. If you need lots of really big strings in memory simultaneously, you'll have to get a 64-bit system with a ton of RAM. http://msdn.microsoft.com/en-us/library/aa366778(VS.85).aspx > Thanks, > Ron. > > -- > http://mail.python.org/mailman/listinfo/python-list > > From missive at hotmail.com Wed Jul 1 20:37:40 2009 From: missive at hotmail.com (Lee Harr) Date: Thu, 2 Jul 2009 05:07:40 +0430 Subject: [ANNC] acromania-0.4 Message-ID: Acromania is a word game of acronyms. This program is a computer moderator for networked games of acromania. It can connect to a channel on IRC, or start a standalone server which can be accessed much like a MUD. http://acromania.googlecode.com/ Acromania uses Twisted and SQLite. Optionally, it can use NLTK to generate computer opponents. Acromania is released under GPLv3. Changes in acromania-0.4: - autocreate conf.py on first start - initialize database on first start - add contact information - various bug fixes Notes: I have only played the game using the standalone server on a secure network. If you have experience with programming IRC bots securely, please take a look and contact me if you see any problems. If you decide to connect it to IRC, please let me know, because I'd like to play :o) _________________________________________________________________ Drag n? drop?Get easy photo sharing with Windows Live? Photos. http://www.microsoft.com/windows/windowslive/products/photos.aspx From http Wed Jul 1 20:48:46 2009 From: http (Paul Rubin) Date: 01 Jul 2009 17:48:46 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <7xhbxvgb9t.fsf@ruckus.brouhaha.com> Mario Garcia writes: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I No that's not a good idea. When the set/dict documentation says you get the keys in an undetermined order, it doesn't mean the ordering is random. It means there is a fixed ordering controlled by the implementation and not by you. If you want a random sample, use random.sample(). See the docs for the random module. From http Wed Jul 1 20:49:21 2009 From: http (Paul Rubin) Date: 01 Jul 2009 17:49:21 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> Message-ID: <7xd48jgb8u.fsf@ruckus.brouhaha.com> Carl Banks writes: > Instead, call random.shuffle() on the list, and iterate through that > to get the elements in random order. It's better to use random.sample() than random.shuffle(). From sjmachin at lexicon.net Wed Jul 1 21:20:29 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 1 Jul 2009 18:20:29 -0700 (PDT) Subject: Why re.match()? References: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> Message-ID: On Jul 2, 9:50?am, MRAB wrote: > Carl Banks wrote: > > On Jul 1, 10:56 am, kj wrote: > >> For a recovering Perl-head like me it is difficult to understand > >> why Python's re module offers both match and search. ?Why not just > >> use search with a beginning-of-string anchor? ?I find it particularly > >> puzzling because I have this (possibly mistaken) idea that the > >> Python design philosophy tends towards minimalism, a sort of Occam's > >> razor, when it comes to language entities; i.e. having re.match > >> along with re.search seems to me like an "unnecessary multiplication > >> of entities". ?What am I missing? > > > It always seemed redundant to me also (notwithstanding Duncan Booth's > > explanation of slight semantic differences). ?However, I find myself > > using re.match much more often than re.search, so perhaps in this case > > a "second obvious way" is justified. > > re.match is anchored at a certain position, whereas re.search isn't. The > re module doesn't support Perl's \G anchor. re.search is obstinately determined when it comes to flogging dead horses: C:\junk>\python26\python -mtimeit -s"import re" "re.match('xxx', 'y'*100)" 100000 loops, best of 3: 3.37 usec per loop C:\junk>\python26\python -mtimeit -s"import re" "re.search('^xxx', 'y'*100)" 100000 loops, best of 3: 7.01 usec per loop C:\junk>\python26\python -mtimeit -s"import re" "re.match('xxx', 'y'*1000)" 100000 loops, best of 3: 3.85 usec per loop C:\junk>\python26\python -mtimeit -s"import re" "re.search('^xxx', 'y'*1000)" 10000 loops, best of 3: 37.9 usec per loop C:\junk> From magawake at gmail.com Wed Jul 1 21:28:12 2009 From: magawake at gmail.com (Mag Gam) Date: Wed, 1 Jul 2009 21:28:12 -0400 Subject: Multi thread reading a file In-Reply-To: References: Message-ID: <1cbd6f830907011828i789e1f6fk18ce71da7290f6ee@mail.gmail.com> LOL! :-) Why not. I think I will take just do it single thread for now and if performance is really that bad I can re investigate. Either way, thanks everyone for your feedback! I think I like python a lot because of the great user community and wiliness to help! On Wed, Jul 1, 2009 at 1:07 AM, Lawrence D'Oliveiro wrote: > In message , Mag Gam > wrote: > >> I am very new to python and I am in the process of loading a very >> large compressed csv file into another format. ?I was wondering if I >> can do this in a multi thread approach. > > Why bother? > > -- > http://mail.python.org/mailman/listinfo/python-list > From steven at REMOVE.THIS.cybersource.com.au Wed Jul 1 21:28:19 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Jul 2009 01:28:19 GMT Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> Message-ID: On Wed, 01 Jul 2009 17:49:21 -0700, Paul Rubin wrote: > Carl Banks writes: >> Instead, call random.shuffle() on the list, and iterate through that to >> get the elements in random order. > > It's better to use random.sample() than random.shuffle(). Why? -- Steven From pavlovevidence at gmail.com Wed Jul 1 21:29:21 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 18:29:21 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> Message-ID: <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> On Jul 1, 5:49?pm, Paul Rubin wrote: > Carl Banks writes: > > Instead, call random.shuffle() on the list, and iterate through that > > to get the elements in random order. > > It's better to use random.sample() than random.shuffle(). If you're iterating through the whole list and don't need to preserve the original order (as was the case here) random.shuffle() is better. Aren't-absolutist-opinions-cool?-ly yr's, Carl Banks From http Wed Jul 1 21:37:38 2009 From: http (Paul Rubin) Date: 01 Jul 2009 18:37:38 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> Message-ID: <7xr5wzzwyl.fsf@ruckus.brouhaha.com> Carl Banks writes: > If you're iterating through the whole list and don't need to preserve > the original order (as was the case here) random.shuffle() is better. 1. Random.sample avoids iterating through the whole list when it can. 2. Say you want to choose 10 random numbers between 1 and 1000000. random.sample(xrange(1000000), 10) works nicely. Doing the same with shuffle is painful. From pavlovevidence at gmail.com Wed Jul 1 21:46:10 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 18:46:10 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> <7xr5wzzwyl.fsf@ruckus.brouhaha.com> Message-ID: <5572daf2-7149-41a7-97e5-91c4bc1e908f@n16g2000yqm.googlegroups.com> On Jul 1, 6:37?pm, Paul Rubin wrote: > Carl Banks writes: > > If you're iterating through the whole list and don't need to preserve > > the original order (as was the case here) random.shuffle() is better. > > 1. Random.sample avoids iterating through the whole list when it can. > > 2. Say you want to choose 10 random numbers between 1 and 1000000. > > ? ?random.sample(xrange(1000000), 10) > > works nicely. ?Doing the same with shuffle is painful. random.shuffle() is still better when you're iterating through the whole list as the OP was doing. Carl Banks From http Wed Jul 1 22:02:46 2009 From: http (Paul Rubin) Date: 01 Jul 2009 19:02:46 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> <7xr5wzzwyl.fsf@ruckus.brouhaha.com> <5572daf2-7149-41a7-97e5-91c4bc1e908f@n16g2000yqm.googlegroups.com> Message-ID: <7xtz1vu9ix.fsf@ruckus.brouhaha.com> Carl Banks writes: > random.shuffle() is still better when you're iterating through the > whole list as the OP was doing. The OP wrote: I want to select, 70% random records from a List. I thougth set where a good idea so I tested this way: ... That sounds like 70% of the list, not the whole list. Note that in addition to using time proportional to the size of the entire lsit rather than the size of the sample, shuffle() also messes up the order of the list. Why would shuffle ever be better? It is designed for a different purpose. Using a function called "sample" when you want to sample should be a no-brainer, and if using shuffle instead is ever preferable, that should be treated as a misfeature in the "sample" implementation, and fixed. From pavlovevidence at gmail.com Wed Jul 1 22:17:56 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 19:17:56 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> <7xr5wzzwyl.fsf@ruckus.brouhaha.com> <5572daf2-7149-41a7-97e5-91c4bc1e908f@n16g2000yqm.googlegroups.com> <7xtz1vu9ix.fsf@ruckus.brouhaha.com> Message-ID: <263f472b-3555-4d62-b37d-fb134da5a683@r25g2000vbn.googlegroups.com> On Jul 1, 7:02?pm, Paul Rubin wrote: > Carl Banks writes: > > random.shuffle() is still better when you're iterating through the > > whole list as the OP was doing. > The OP wrote: > > ? ? I want to select, 70% random records from a List. I thougth set > ? ? where a good idea so I tested this way: ... I was going by his example which went through all the items in the list. > That sounds like 70% of the list, not the whole list. ?Note that > in addition to using time proportional to the size of the entire > lsit rather than the size of the sample, shuffle() also messes up > the order of the list. ? > > Why would shuffle ever be better? Because we were talking about different things. Carl Banks From rtm74 at yahoo.com Wed Jul 1 22:38:14 2009 From: rtm74 at yahoo.com (rtm74 at yahoo.com) Date: Wed, 1 Jul 2009 19:38:14 -0700 (PDT) Subject: Newby Windows User Message-ID: <447092.99927.qm@web110201.mail.gq1.yahoo.com> I am new to Python and use Windows XP. I would like to know how to run a python program file? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bpytlik at Sun.COM Wed Jul 1 22:48:04 2009 From: bpytlik at Sun.COM (Brock Pytlik) Date: Wed, 01 Jul 2009 19:48:04 -0700 Subject: System default sys.path Message-ID: <4A4C1FE4.1070901@sun.com> Hi, I'm trying to find a way to get the value sys.path would have on a particular system if python was started with an empty python path. I do want it to include the site specific additional paths. I know I can hack this information myself, but I'd rather be able to get this on demand so that if things change in the future, I don't have grovel around looking for which directory string to change. If nothing else, I think the following would satisfy my needs: 1) a variable containing the directories to use as a prefix (I think sys.exec_prefix and sys.prefix are what I want here) 2) a variable containing the list of suffixes that are applies to the prefixes, like lib/python/site-packages 3) a way of handing the *.pth files Thanks in advance for the help, Brock From pavlovevidence at gmail.com Wed Jul 1 23:18:22 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 20:18:22 -0700 (PDT) Subject: invoking a method from two superclasses References: Message-ID: <24ebfdef-0832-4b78-983a-d886e56160b7@f10g2000vbf.googlegroups.com> On Jun 30, 9:57?pm, Mitchell L Model wrote: > [Continuing the discussion about super() and __init__] > > The documentation of super points out that good design of > diamond patterns require the methods to have the same > signature throughout the diamond. That's fine for non-mixin > classes where the diamond captures different ways of handling > the same data. [snip] > The problem is that each mixin's __init__ is likely to have a > different signature. [snip] Here are my recommendations for mixin classes. 1. Try to write mixins so that they don't need initializer arguments. There's several approaches to this. I prefer to burden the subclass to set any attributes the mixin needs before calling super().__init__ (). class A(SomeMixin,SomeBaseClass): def __init__(self,arg1,arg2): self.some_attribute_needed_by_mixin = 10 super().__init__(arg1,arg2) That might seem a little weird to some (possibly because it is impossible in C++ or Java to do that), but I find it to be a far more versatile way for a subclass to send data to its parent. I use it for regular single inheritance, even. Another possibility is to have a separate mixin initializer that is called separately. 2. The __init__ of a mixin, if it has one, should always take *args and **kwargs arguments, and only those arguments, and pass them as-is to the superclass. class SomeMixin: def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) self.use_somehow(self.some_attribute_needed_by_mixin) If your mixin must have its own initializer argument, make it a keyword-only argument, and have __init__ pop it off kwargs. 3. List mixin bases first among base classes. I don't think Python provides an ideal way to do MI, BTW. I believe that's by design: MI is somewhat troublesome specifically to discourage overuse. As a power MI user, I am fine with this. > I think I'd be back to having to call each mixin class's > __init__ explicitly: I'd say never mix super() and direct calls, for any reason. Pick one and go with it. Carl Banks From contact at xavierho.com Wed Jul 1 23:18:42 2009 From: contact at xavierho.com (Xavier Ho) Date: Thu, 2 Jul 2009 13:18:42 +1000 Subject: Newby Windows User In-Reply-To: <447092.99927.qm@web110201.mail.gq1.yahoo.com> References: <447092.99927.qm@web110201.mail.gq1.yahoo.com> Message-ID: <2d56febf0907012018h79a6ca5bt97d3a900a4b409bf@mail.gmail.com> Double click should work. If it flashes up and goes away really quickly, then you need to run it under IDLE or some kind of python shell. Once you have Python installed, go to Start -> Run, and type in cmd to bring up the windows command line or whatever you wanna call it. Go to the folder where your python script is. Command is the same like macs or linux, with cd being change directory, and such. Type e: to switch to the e drive and so on. Note that dir lists the directory and its contents, not ls like in mac terminals. When you're in the same folder as your script, type the command like this: python .py That should do the trick. Alternatively, you can type in python and start the python shell in the same folder. Then you can just do import and whatever you like. :] Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Thu, Jul 2, 2009 at 12:38 PM, wrote: > I am new to Python and use Windows XP. I would like to know how to run a > python program file? > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wuwei23 at gmail.com Wed Jul 1 23:41:19 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:41:19 -0700 (PDT) Subject: Using Python to set desktop background image under Windows XP References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: On Jul 2, 8:02?am, "ELLINGHAUS, LANCE" wrote: > Does anyone have any code that would allow setting the desktop background image under Windows XP? It's amazing what typing "python windows desktop" into Google will find you: http://snippets.dzone.com/posts/show/3348 Any other searches you'd like us to do? (Also: please post a new message instead of replying to an existing one and changing the subject, it screws up threading) From wuwei23 at gmail.com Wed Jul 1 23:47:46 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:47:46 -0700 (PDT) Subject: Getting input the scanf way References: Message-ID: On Jul 1, 6:33?pm, "Mr.SpOOn" wrote: > I need to do some kind of interactive command line program. > > I mean: I run the program, it asks me for input, I type something and > then I get the output or other questions. > I'm not sure what is the right way to achieve this. While the simplest way would be raw_input & print, as suggested, there's also the cmd[1] module in the stdlib, which is what the standard CPython interpreter uses, I believe. [1]: http://docs.python.org/library/cmd.html From no.email at please.post Wed Jul 1 23:49:57 2009 From: no.email at please.post (kj) Date: Thu, 2 Jul 2009 03:49:57 +0000 (UTC) Subject: Why re.match()? References: Message-ID: In Duncan Booth writes: >So, for example: >>>> re.compile("c").match("abcdef", 2) ><_sre.SRE_Match object at 0x0000000002C09B90> >>>> re.compile("^c").search("abcdef", 2) >>>> I find this unconvincing; with re.search alone one could simply do: >>> re.compile("^c").search("abcdef"[2:]) <_sre.SRE_Match object at 0x75918> No need for re.match(), at least as far as your example shows. Maybe there are times when re.match() is more "convenient" in some way, but it is awfully Perlish to "multiply language elements" for the sake of this trifling convenience. kynn From wuwei23 at gmail.com Wed Jul 1 23:53:37 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:53:37 -0700 (PDT) Subject: Open Source RSS Reader in Python? References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: <425d611e-ee56-4def-875e-899b2753eeb4@a7g2000yqk.googlegroups.com> On Jul 2, 9:18?am, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? It's always worth taking a look at PyPI. Some packages work perfectly well as stand alone applications. http://pypi.python.org/pypi?%3Aaction=search&term=rss&submit=search These two look promising: + NewsFeed (Tk-based): http://home.arcor.de/mdoege/newsfeed/ + urssus (QT-based): http://code.google.com/p/urssus/ From wuwei23 at gmail.com Wed Jul 1 23:56:28 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:56:28 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <1e79b6bf-f33e-4267-bc61-dde6093a95e1@r33g2000yqn.googlegroups.com> On Jul 2, 3:47?am, Scott David Daniels wrote: > And even simpler: > ? ? ?PASSWORD = "qwerty" > ? ? ?MAXRETRY = 3 > ? ? ?for attempt in range(MAXRETRY): > ? ? ? ? ?if raw_input('Enter your password: ') == PASSWORD: > ? ? ? ? ? ? ?print 'Password confirmed' > ? ? ? ? ? ? ?break # this exits the for loop > ? ? ? ? ?print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) > ? ? ?else: > ? ? ? ? ?# The else for a for statement is not executed for breaks, > ? ? ? ? ?# So indicates the end of testing without a match > ? ? ? ? ?raise SystemExit # Or whatever you'd rather do. Nice one, I always forget about for-else :) From steven at REMOVE.THIS.cybersource.com.au Thu Jul 2 00:14:36 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Jul 2009 04:14:36 GMT Subject: Why re.match()? References: Message-ID: On Thu, 02 Jul 2009 03:49:57 +0000, kj wrote: > In Duncan Booth > writes: >>So, for example: > >>>>> re.compile("c").match("abcdef", 2) >><_sre.SRE_Match object at 0x0000000002C09B90> >>>>> re.compile("^c").search("abcdef", 2) >>>>> >>>>> > I find this unconvincing; with re.search alone one could simply do: > >>>> re.compile("^c").search("abcdef"[2:]) > <_sre.SRE_Match object at 0x75918> > > No need for re.match(), at least as far as your example shows. Your source string "abcdef" is tiny. Consider the case where the source string is 4GB of data. You want to duplicate the whole lot, minus two characters. Not so easy now. > Maybe there are times when re.match() is more "convenient" in some way, > but it is awfully Perlish to "multiply language elements" for the sake > of this trifling convenience. No, it's absolutely Pythonic. >>> import this ... Although practicality beats purity. -- Steven From rajat.dudeja at gmail.com Thu Jul 2 00:33:55 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Wed, 1 Jul 2009 21:33:55 -0700 (PDT) Subject: Access NtQueryInformationProces() from Python References: <408014003A0DA34BA5287D7A07EC089A1F94F4@EVS1.aeroflex.corp> Message-ID: <5e107810-2378-41fc-8fb5-329a943ab3bd@t13g2000yqt.googlegroups.com> On Jun 30, 2:10?pm, Tim Golden wrote: > Dudeja, Rajat wrote: > > Hi, > > > I'm looking for a way to call the NtQueryInformationProces() and ReadProcessMemory() of WindowsXP from Python. > > > Can anyone direct me to some examples where they have been successfully called through Python? > > You want to look at ctypes: > > http://docs.python.org/library/ctypes.html?highlight=ctypes#module-ct... > > TJG Thanks Tim From david.lyon at preisshare.net Thu Jul 2 00:40:25 2009 From: david.lyon at preisshare.net (David Lyon) Date: Thu, 02 Jul 2009 00:40:25 -0400 Subject: System default sys.path In-Reply-To: <4A4C1FE4.1070901@sun.com> References: <4A4C1FE4.1070901@sun.com> Message-ID: <81a8195ca5ecf44f3bf9ddbfaae37bdc@preisshare.net> On Wed, 01 Jul 2009 19:48:04 -0700, Brock Pytlik wrote: > Hi, I'm trying to find a way to get the value sys.path would have on a > particular system if python was started with an empty python path. I do > want it to include the site specific additional paths. I know I can hack > this information myself, Copy the code out from site.py... > but I'd rather be able to get this on demand so > that if things change in the future, I don't have grovel around looking > for which directory string to change. for index in range(len(sys.path)): del sys.path[0] site.addsitedir(self.p.python_sitepackages_path) > If nothing else, I think the following would satisfy my needs: > 1) a variable containing the directories to use as a prefix (I think > sys.exec_prefix and sys.prefix are what I want here) > 2) a variable containing the list of suffixes that are applies to the > prefixes, like lib/python/site-packages > 3) a way of handing the *.pth files What way do you want to handle them? From stefan_ml at behnel.de Thu Jul 2 00:50:54 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 02 Jul 2009 06:50:54 +0200 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <4a4c3cae$0$31873$9b4e6d93@newsspool3.arcor-online.net> Mag Gam wrote: > On Wed, Jul 1, 2009 at 12:54 AM, Gabriel Genellina wrote: >> Does the format conversion involve a significant processing time? If not, >> the total time is dominated by the I/O time (reading and writing the file) >> so it's doubtful you gain anything from multiple threads. > > The format does inolve significant time processing each line. > >>> Here is the pseudo code I was thinking about: >>> >>> Let T = Total number of lines in a file, Example 1000000 (1 million >>> files) >>> Let B = Total number of lines in a buffer, for example 10000 lines >>> >>> >>> Create a thread to read until buffer >>> Create another thread to read buffer+buffer ( So we have 2 threads >>> now. But since the file is zipped I have to wait until the first >>> thread is completed. Unless someone knows of a clever technique. >>> Write the content of thread 1 into a numpy array >>> Write the content of thread 2 into a numpy array >> Can you process each line independently? Is the record order important? If >> not (or at least, some local dis-ordering is acceptable) you may use a few >> worker threads (doing the conversion), feed them thru a Queue object, put >> the converted lines into another Queue, and let another thread write the >> results onto the destination file. > > Yes, each line can be independent. The original file is a time series > file which I am placing it into a Numpy array therefore I don't think > the record order is important. The writing is actually done when I > place a value into a "dset" object. > > Let me show you what I mean. > reader=csv.reader(open("100000.csv")) Have you tried if using the csv reader here is actually fast enough for you? Maybe you can just .split(',') each line or something (no idea what else the csv reader may do, but anyway...) > for s,row in enumerate(reader): > if s!=0 and s%bufsize==0: > dset[s-bufsize:s] = t #here is where I am writing the data to > the data structure. Using a range or broadcasting. Erm, what's "t"? And where do you think the "significant" processing time comes from in your example? From your code, that's totally non-obvious to me. > #15 columns > if len(row) != 15: > break > > t[s%bufsize] = tuple(row) > > #Do this all the way at the end for flushing. > if (s%bufsize != 0): > dset[(s//bufsize)*bufsize:s]=t[0:s%bufsize] If you *really* think your code is not I/O bound, but that the code for writing the data into the NumPy array is the slow part (which I actually doubt, but anyway), you can try writing your code in Cython, which has direct support for writing to NumPy arrays through their buffer interface. http://docs.cython.org/docs/numpy_tutorial.html Stefan From apt.shansen at gmail.com Thu Jul 2 01:11:10 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Wed, 1 Jul 2009 22:11:10 -0700 Subject: What are the limitations of cStringIO.StringIO() ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: <7a9c25c20907012211m610dac7dr3c18c33ea33f0ed6@mail.gmail.com> > > > 1. Anyone knows whet's the limitation on cStringIO.StringIO() objects ? > 2. Could you suggest a better way to create a string that contains the > concatenation of several big files ? > > This isn't a limit in cStringIO.StringIO, but a limit on the amount of memory a process is able to allocate from the OS. There's no real way around it-- but you can take a different approach. You have to get the memory somewhere, but if what you're combining can't fit into memory (generally, 2 GB, but it depends on OS and platform), you can use memory from a different source-- the hard-drive for instance. I would suggest you create a temporary file somewhere, and instead of reading the contents into memory, read it in chunks and write it to this temporary file, then go on and do the rest. That way you'll never have so much in memory that you hit the limits. When this is done, you can move this temporary file to where you want it, or read from it and pass its output somewhere. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Jul 2 01:25:05 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jul 2009 01:25:05 -0400 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: <4A4BC22D.70502@gmail.com> References: <4A4BC22D.70502@gmail.com> Message-ID: Stef Mientki wrote: > hello, > > I need to add an object's name to the global namespace. > The reason for this is to create an environment, > where you can add some kind of math environment, > where no need for Python knowledge is needed. > > The next statement works, > but I'm not sure if it will have any dramatical side effects, > other than overruling a possible object with the name A > > def some_function ( ...) : > A = object ( ...) > sys._getframe(1).f_globals [ Name ] = A global name name = A or is name is a string var globals()[name] = A From esj at harvee.org Thu Jul 2 01:30:16 2009 From: esj at harvee.org (Eric S. Johansson) Date: Thu, 02 Jul 2009 01:30:16 -0400 Subject: pep 8 constants In-Reply-To: <4A4A3FC1.5000108@tim.thechases.com> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4A2EE5.8040301@harvee.org> <4A4A3FC1.5000108@tim.thechases.com> Message-ID: <4A4C45E8.9070309@harvee.org> Tim Chase wrote: >> I've tried it least two dozen editors and they all fail miserably >> because they're focused on keyboard use (but understandable) > [...snip...] >> I've tried a whole bunch, like I said at least a dozen. They >> all fail for first reasons such as inability to access all >> functionality through keystrokes (easiest interface method >> from speech recognition) > > I'm not sure I follow which you want...you kvetch that > they're too focused on keyboard use, but then that you can't > access all functionality through the keyboard. sorry. I've been living in this world for so long that I sometimes forget to explain what's obvious inside my head. I apologize for the confusion. The apparent conflict arises because the same term (keyboard access) is used to describe two different problems. In the first case, the user interface model (how you access taxes, manipulate light which (language) constructs are designed to be efficient for your hands. speech recognition users need an editor that supports constructs that are efficient for the voice. One such construct is the ability to navigate or operate on language elements by name. I've used some of them before such as predicate, index, first method etc. The second problem is that editors are designed to be driven by keystroke. If I had a cross process boundary APIPaula I can simply call the editor function to perform to give operation. Unfortunately, today my speech recognition environment needs to generate keystrokes to access functionality. for example, in Emacs, when I say "delete word", the macro and marmot emits M-d > > [warning, blatant vim fanaticism follows] > > I use vim and can't remember the last time I used the mouse > with it (save for occasionally lazily scrolling with the > mouse-wheel, but you can scroll with the keyboard too), so > it meets your "must be fully accessible through the > keyboard" constraint. It also has single keystroke commands > for indenting/exdenting the current line as you mention: you are forgiven. :-) try this experiment. Bring up the buffer full Python code code you value them, starting in command mode, close your eyes and type a random word. While your eyes are closed, undo that word. Every time I've tried it the results have been mildly disastrous. If you been paying attention, I've tried to leave speech recognition errors in place whatever didn't impede understanding (too much) but, remember I'm getting one error in 20 errors is on average. That means, I'll be correcting the IE errors, potentially much more dangerous errors are regular basis. Any energy use by speech recognition must be able to cope with a random dictation errors and let the user recover gracefully. Again, until I started using speech recognition, I would never would've thought of this as a failure case. > > > I know this is also counter to the Python way but having > > something marking and outdent would be really useful so > > that a vocal driven command to indent or outdent a block > > would be practical. > > which can be done in insert-mode with control+D, control+T, > and if you want to clear all indentation (all the way back > to the first column), you can use "0" followed by control+D. > Vim also allows for fairly detailed control over > auto-indentation settings so you can match them to your > preferences. I suspect Emacs may be configurable to offer > similar functionality but I'd have to defer to the emacsen > on the list. Yes, Emacs will, a single tab key, tab view to the indentation level specified by the previous line unless it's already there and then it will start inventing further. This really sucks. Sometimes, you just weary and then to (you just want to) go to the right place and stay there > > That said, I'll be the first to admit that the vi/vim > learning curve is more like a brick wall than a curve. > However, I find the efficiency gains abundantly than repaid > the time I invested to learn it well. Yeah, I first encountered the eye in 1985. Or maybe I should say VI (Roman numeral six). > >>> Dasher[1] >> >> I've tried it, it's quite promising but my hands term are sufficiently >> that I can't target accurately. > > Last I tried it, it scaled the target sizes based on > probability. You might also try unconventional pointing > devices for better precision. I've seen web-cam > eye-tracking tools which might make it a bit easier. Or try > a tablet input (where the motion corresponds linearly, as > opposed to accelerated mouse motion). Such input options > are listed on the Dasher website, at least for those they > tested. > I like tablets. I need to buy a new one. We only problem is that most pens for tablets are a little bit too small and hard for my hands (although they are much better than mice). The major downfall is the buttons on the pen shaft. Damn that's awkward to reach. I think it would need a separate tab of three buttons that I can click and double-click independent of the pen controls. >> Oh, another failure point. Have you ever tried to selected beach and >> by voice. I mean I should say have you ever tried to >> select a region by voice. > > yes, not a pleasant experience. Again, vim's modal > interface allows for using text-objects so you can issue > short-hand commands like > > ci" > > to "[c]hange the contents [i]nside the double-quotes I'm > currently on/withing". The operator+motion and > operator+textobject command syntax obviates a lot selection. > While vim does offer a "visual" mode (akin to selection in > other editors), I use it much less because of the power > provided by the operator+[motion/textobject]. that's getting close to what I want to do with voice command except instead of cryptic sequence of characters, I would say something like string_edit = edit [] string which = first | second | third | fourth | fifth | sixth > as you're an emacs user, my vim suggestions may sound like > heresy. ;-) But I suspect a skilled emacs user (or perhaps > asking on an emacs-users list) could mimic much of the vim > functionality. I just can't be of much assistance there. I really don't care what editor I use as long as it gets the job done. I have strong preferences for always insert mode but, if I had an editor I had no keystroke commands and everything I type went into the buffer but I committed a complete of my speech, that would be kind of cool. > > You're welcome to move it off-list...it's kinda drifted from python to > general accessibility issues. CC'ing c.p.l for this one in case any > emacsen care to chime in on their suggested tweaks. well, that you are really joined. For example, if we look at the string edit command above, that implies he had or has the ability to properly distinguish a string in a line of code. Think of how many ways you can express a string triple or single quotes into different forms sometimes with quotes nested inside. then take the whole object instance and know method is possible with its type signature. This is editor work. From Mariosky at gmail.com Thu Jul 2 01:33:25 2009 From: Mariosky at gmail.com (Mario Garcia) Date: Wed, 1 Jul 2009 22:33:25 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <05dde563-a8f1-45d1-8f59-7740c58787ba@e21g2000yqb.googlegroups.com> Thank you all for your input, Yes random is what I need!! I checked the docs, and follow your comments, I will use both random.sample and random.shuffle,and random.choice etc. !! For this particular problem I think ramdom.shuffle() is what I need. I was not very clear or complete in my explanation, and I didn't expect such a discussion on my needs, now I'm embarrassed :) . The list is a data set for training a machine learning algorithm. I want to use 70% of the records (random) for training, but then the remaining 30% are used for validation. This is repeated a few times choose again 70% at random for training and the rest for validation. With random.shuffle() I just iterate for the first 70% of the records for training and I just continue with the remaining 30%. This last 30% we dont care if its random anymore, so shuffle is doing some extra work as it was pointed out. Conceptually something like this: population = range(100) training = random.sample(population,70) validation = set(population).difference(set(training)) But I think this is more costly Thanks Again Mario From rajat.dudeja at gmail.com Thu Jul 2 01:42:43 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Wed, 1 Jul 2009 22:42:43 -0700 (PDT) Subject: Accessing windows structures through ctypes. Message-ID: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Hi, Using ctypes can I access the windows structures like: PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), PEB_LDR_DATA, etc? Regards, Rajat From esj at harvee.org Thu Jul 2 01:47:28 2009 From: esj at harvee.org (Eric S. Johansson) Date: Thu, 02 Jul 2009 01:47:28 -0400 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <025949cb$0$20671$c3e8da3@news.astraweb.com> <4A497A5B.4040503@harvee.org> Message-ID: <4A4C49F0.5060306@harvee.org> Rhodri James wrote: > > Gah. Ignore me. I hit 'send' instead of 'cancel', after my musings > concluded that yes, an editor could be smart enough, but it would have to > embed a hell of a lot of semantic knowledge of Python and it still wouldn't > eliminate the need to speak the keyboard at times. imagine having the same problem except that happens at random while you're speaking and watching something else other than a screen. :-) Yeah, it would take a lot of semantic knowledge about Python. Is it possible to leverage compiler internals? Is it possible to gather as much information as you can and when the system says "I don't know" you tell what you know it remembers that for future reference? And you're right, they're always corner cases where you'll need to speak the keyboard or type. I've mostly resigned to accept on days when I have an absolute hissy hissy fit because my hands hurt so bad I can't get enough painkillers drop the pain and let me function. In the real short term what I really want. More than anything right now is to get back to the point where I can pick away at writing Python again. I'm in this really weird confluence of virtual machine hell, the evolution of naturally speaking under wine and the problems of telling an editor how to do the right thing. There's a gap between the fragile crappy way I and others operate today and the really ideal completely voice driven environment that exists between my ears. I would like to move into that gap but it's sufficiently far away from what my hands can do that I'm dependent on others and that makes me really crazy sometimes. >From my perspective, what I consider a reasonable intermediate step would be an editor that has the following features: The ability to select regions either by NaturallySpeaking Select-and-Say and Emacs mark and point semantics. Very deterministic indentation control. (I.e. indent x tabs or indent to "right" level.) ability to navigate/operate on generic language features it would be nice if one could edit language features in a separate window again, using Select-and-Say type interface. another it would be nice would be the ability to look up type signatures (classes, methods etc. and inject the appropriate template when using a particular method. (Doesn't have to be by voice) this level of functionality, would also make it possible to comply with PEP-8 :-) given proper easy and automatic name generation and dumpling. From ldo at geek-central.gen.new_zealand Thu Jul 2 01:53:09 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 02 Jul 2009 17:53:09 +1200 Subject: Making code run in both source tree and installation path References: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> Message-ID: In message , Robert Kern wrote: > On 2009-07-01 01:04, Carl Banks wrote: >> >> The most common way I've seen people work around this issue is to >> throw their data files into the package directories. Yuck. > > Huh. I always found that to be a more elegant solution than hardcoding the > data location into the program at install-time. Think in terms of a portable OS, written to run on multiple architectures. Read-only data is architecture-dependent, that's why it pays to separate it from the code. Otherwise you end up with a system that needs a reinstall every time the hardware changes. From wuwei23 at gmail.com Thu Jul 2 02:09:13 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 23:09:13 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> <1e79b6bf-f33e-4267-bc61-dde6093a95e1@r33g2000yqn.googlegroups.com> Message-ID: <19cbe70f-d9a0-4c77-b02a-98b943b063b7@o6g2000yqj.googlegroups.com> Dennis Lee Bieber wrote: > ? ? ? ? There is also the getpass module to play with! I don't think I've ever seen getpass, so thanks for pointing that out. Unfortunately, it wouldn't have helped the OP understand why his original code wasn't working ;) From tkjthingone at gmail.com Thu Jul 2 02:10:43 2009 From: tkjthingone at gmail.com (Horace Blegg) Date: Wed, 1 Jul 2009 23:10:43 -0700 Subject: Accessing windows structures through ctypes. In-Reply-To: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Message-ID: http://www.codeproject.com/KB/threads/GetNtProcessInfo.aspx Looks rather to be pretty simple: Acquire the PED base pointer (article explains how) and then just read that information into a struct using ReadProcessMemory(). On Wed, Jul 1, 2009 at 10:42 PM, Rajat wrote: > Hi, > > Using ctypes can I access the windows structures like: > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > PEB_LDR_DATA, etc? > > > Regards, > Rajat > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From shenyute at gmail.com Thu Jul 2 02:11:54 2009 From: shenyute at gmail.com (Shen, Yu-Teh) Date: Wed, 1 Jul 2009 23:11:54 -0700 (PDT) Subject: dealloc function in python extend c module Message-ID: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> I create my extend type something like http://www.python.org/doc/current/extending/newtypes.html. And my type has a member which is a pointer point to my allocate memory ( no ref count). ex: --------------------------------------------------- typedef struct { PyObject_HEAD /* Type-specific fields go here. */ mytype *p; } noddy_NoddyObject; And i write dealloc function like this: --------------------------------------------------- static void Noddy_dealloc(Noddy* self) { delete p; self->ob_type->tp_free((PyObject*)self); } And I found it's strange that it didn't call dealloc when there is no reference. ex: a = Noddy() b = a del a del b # i think there is no ref to the object, and it should call dealloc but it didn't call!!! could anyone tell me what happened? Thanks a lot!! From pm567426 at gmail.com Thu Jul 2 02:13:52 2009 From: pm567426 at gmail.com (Pedram) Date: Wed, 1 Jul 2009 23:13:52 -0700 (PDT) Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Message-ID: <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> On Jul 1, 10:01?pm, Christian Heimes wrote: > Pedram schrieb: > > > Hello community, > > I'm reading the CPython interpreter source code, > > first, if you have something that I should know for better reading > > this source code, I would much appreciate that :) > > second, in intobject.c file, I read the following code in > > fill_free_list function that I couldn't understand: > > while (--q > p) > > ? ? Py_TYPE(q) = (struct _typeobject *)(q-1); > > Py_TYPE(q) = NULL; > > > > What does it mean? > > The code is abusing the ob_type member to store a single linked, NULL > terminated list of free integer objects. It's a tricky optimization you > don't have to understand in order to understand the rest of the code. > > And no, the code in ctypes.h is totally unrelated to the free list in > intobject.c. > > Christian Thanks for reply. I totally understood the fill_free_list() function. Thanks. But I have problems in understanding the PyInt_FromLong(). Here's the code: PyObject * PyInt_FromLong(long ival) { register PyIntObject *v; #if NSMALLNEGINTS + NSMALLPOSINTS > 0 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { v = small_ints[ival + NSMALLNEGINTS]; Py_INCREF(v); #ifdef COUNT_ALLOCS if (ival >= 0) quick_int_allocs++; else quick_neg_int_allocs++; #endif return (PyObject *) v; } #endif if (free_list == NULL) { if ((free_list = fill_free_list()) == NULL) return NULL; } v = free_list; // <-- No problem till here :) free_list = (PyIntObject *)Py_TYPE(v); PyObject_INIT(v, &PyInt_Type); v->ob_ival = ival; return (PyObject *) v; } I understood that small numbers (between -5 and 256) are referenced to small_ints list. Others have to store in PyIntBlock, am I right? OK, no problem till 'v = free_list;' but can't understand the line 'free_list = (PyIntObject *)Py_TYPE(v);' :(. Where free_list referenced to? Don't this correct that free_list have to be referenced to another free block so next time the function called v store in this location? Sorry for my bad English. I hope you could understand me :) From israelu at elbit.co.il Thu Jul 2 02:17:00 2009 From: israelu at elbit.co.il (iu2) Date: Wed, 1 Jul 2009 23:17:00 -0700 (PDT) Subject: wxPython: Plaing widgets in status bar Message-ID: <335b69b5-eabe-443e-842d-fb6ec95a3bd3@x3g2000yqa.googlegroups.com> Hi all, I try to placs widgets (button, static text labels) in a status bar, wxPython. >From the examples I could place them using exact positioning by the status bar methof GetFieldRect. In this case I need to add an EVT_SIZER handler to keep the widgets in their propotional places within the status bar. I try to do it without exact positioning, that is using a sizer. I tried adding a sizer directly to the status bar, but it didn't work, it layed out the widgets at the upper left corner of the status bar, atop of each other. I tried adding a panel to the status bar and then adding the widgets to it., but then the widgets partially cover the status bar. They also don't keep their propotional positions, although I used it in the sizer.Add method. How can I position widgets within the status bar like positioning them in a frame? thanks From wuwei23 at gmail.com Thu Jul 2 02:25:38 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 23:25:38 -0700 (PDT) Subject: Accessing windows structures through ctypes. References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Message-ID: On Jul 2, 3:42?pm, Rajat wrote: > Using ctypes can I access the windows structures like: > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > PEB_LDR_DATA, etc? ctypes.wintypes lists all of the Windows structures included with the module. You should be able to use ctypes.Structure class to roll your own: http://docs.python.org/library/ctypes.html#structured-data-types http://code.activestate.com/recipes/208699/ http://msdn.microsoft.com/en-us/library/ms684855(VS.85).aspx From esj at harvee.org Thu Jul 2 02:33:10 2009 From: esj at harvee.org (Eric S. Johansson) Date: Thu, 02 Jul 2009 02:33:10 -0400 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <025949cb$0$20671$c3e8da3@news.astraweb.com> Message-ID: <4A4C54A6.9030707@harvee.org> Steven D'Aprano wrote: > That assumes that every word is all caps. In practice, for real-life > Python code, I've tripled the vocal load of perhaps one percent of your > utterances, which cuts your productivity by 2%. > > If you have 10000 words in you per day, and one percent get wrapped with > a leading and trailing "capslock", you have: > > 9900 regular words plus 100 constants > > versus > > 9700 regular words plus 100 constants plus 200 by capslock. > > Your productivity goes down by 200 words out of 10,000, or two percent. oh I wish it was so simple. I would absolutely you if it was that simple the real world. Somehow, I fear that you wouldn't understand unless you sat next to me and watch me try to write code to a variety of standards. In practice, my experience says that with a really good speech driven environment for programming, IT, vocal load probably by a quarter to a half and be more compliant with current fashion for naming. > > >>>> Heck, have you ever noticed how most Python smart editors can't even >>>> indent properly according to local contexts. Emacs is the only one and >>>> even that one sometimes fails >>> I use kwrite for editing, and I can't say I've ever noticed a failure. >> okay, it fails from a speech recognition user perspective. Put the >> cursor on the line and hit the tab key. Insert spaces/tabs in the line >> of text. Not good for speech recognition. If mess of indentation and >> hit the tab key, it doesn't automatically indent to the right location. >> I'll be damned if I'm in the sit there speaking >> "tabkeytabkeytabkeytabkeytabkey" because the error should take care of >> it for me and save my voice. Like I said, Emacs does it correctly and >> nobody else does as far as I can tell. > > I don't understand what you're describing. > > If you set kwrite to use "Python style" indent mode, then starting a new > line will automatically indent to either the current indent level, or one > extra indent level if the line ends with a colon. You shouldn't need to > indent forward more than one level at a time when writing Python code, > although you will need to dedent multiple levels on occasion. I recognize your confusion. It's very common when describing this problem. If I had some way of making a movie of what and describing, obviously be more clear. I'll see if I can make a movie with my camera that demonstrates the problem. It probably won't happen fast but, what I tried your editor, it failed with a trivial effort. I think I just hit the tab key multiple times in different places on the line. In all cases it did the wrong thing. For me, the right thing is to force the line to the right indentation based on the previous line to matter how may times you get the tab key or where you are on that line when you hit the tab key. I could be in the middle of the line, the end of line, start a line, the start of the string and if I hit a tab key, that line should go to exactly the right place stayed there no matter how me more times I typed a tab key. But this might be what you're getting confused. What I'm really asking for is the ability to say "force indent" and have the line indent exactly correctly based on context. It doesn't have to be the tab key. The tab key is the method of convenience. > >> You're looking at it from the classical anti-accommodation perspective. >> Don't change the convention, give us the tools so we can comply with >> what the rest of you do. > > Just a minute. You're the one who started this discussion rejecting the > convention. Your first post started off: > > "I reject this convention..." > > and then finished with > > "so I reject pep 8 because I have no voice safe alternative" > > Do you blame us for reading this as a protest against uppercase > identifiers? it's a protest against identifiers that increase vocal or hand load. It's also protest against decisions people make without awareness of the implications, in this case, increasing the barriers against disabled programmers. > > Now you're saying you're happy for us to keep the all-uppercase > convention and just want better tools. Okay, you want better tools. > Great. If you're attempting to raise the profile of disabled programmers, > you've done so, but what exactly are you expecting? I waffle back and forth on that. I recognize my opinion matters as much as a piss hole in the snow. But the unintentional discrimination really gets to me. In case you're wondering, I feel this way about many forms of bad design/exclusionary practices. Some I can even do something about. my expectations are evolving. I wanted to raise awareness, like you said, I've done that. But now, I'd like to turn it into action. As I said in another post, the editor that would let me get back to writing Python relatively easily again doesn't exist. It's not the flashy wonderful no keystrokes whatsoever condition but it's significantly better than the current Emacs/VI/Python editor du jour. it would be able to operate open loop with relation to speech recognition but I also have an idea of how to add closed loop capability to make it more efficient as that capability becomes available. > I've already said it's your right to reject the convention for your own > code. Go right ahead. This is only a problem if you're editing other > people's code which is using uppercase constants. exactly. If I'm unable to comply with local conventions, I cannot work within an organization and be an active member of the team. This is one example of what I mean by unintentional discriminatory effects of a given decision. rhetorical devices aside, I think the right way to deal with accessibility issues and decisions leading to discrimination is to make the appropriate tools if possible for the individual. But I'm sure that would be a very long discussion we would have over a couple of beers. > > You've rejected capslock red capslock because it takes three utterances, > but said you use constant_red instead. That's two utterances, saving you > only one. If you actually have to say the underscore, your convention > takes six syllables versus five for my idea. my opinion comes out of experience. Strictly numerically speaking, you are correct. But if you look at recognition accuracy, pacing of speech, and flow of words through the vocal track, my solution is gentler on the vocal tract, body stress (tension anticipating this recognition errors), and cognition (don't need a spare any mental cycles on anticipation of failure and how to correct). I can just say focused on what I'm dictating. Additionally, making corrections of my style is easier (i.e. you can make the correction) but the form you propose with Locks (see, this is one of those "I'm going to bust a In your ass" type errors) cannot be corrected. He can only be deleted and restated. I apologize for being a "I'm the expert know it all" on this but, I really do have 15 years experience with these kind of failures and I like to think I know something about the topic. >>> Especially when these coding conventions are entirely optional. >> these conventions are religion for some special if you want to >> contribute code. > > Does your editor have search and replace? Before you contribute code, do > a global replacement of constant_red for RED. Yes it does and, it you know as well as I do that global search replace blindly leads to disastrous errors and, increased testing, and the problem doesn't go away because you always modify your code later. > > >> But look at the bigger issue. > > Yes, we get it. It sucks to have a physical disability. The universe > isn't arranged to make it easy for those who do, and even systems built > by people are only indifferently suitable. fine. I'll put down that particular baseball bat stop hitting you over the head with it. I still think you should try living with naturally speaking for a week or two. > > So, let's get down to practical matters: > > Do you wish to register a protest against all-caps constants in PEP 8? only if I had a rational, reasonable alternative. Let me chew on that. > > Do you wish to ask the Python Dev team to rethink their decision? only when I have rational, reasonable alternatives. > > Do you wish to raise the profile of disabled programmers? yes. Not exactly sure of the best way but I would like to do that through providing tools enabling them to participate on equal footing with not yet disabled programmers. I really don't like saying "take pity on the poor programmers with broken hands." I'd rather say "you need talent? Here's a pool you have previously thrown away but they can now generate code faster than the keyboard users. > Do you wish to start a project developing a better, smarter editor, and > are looking for volunteers? yes. This also opens up a great big giant hairball. Specifically, how do you edit on one machine but do all of your testing and debugging on the second (i.e. edit on Windows, do everything else on Linux. Unfortunately, I have quite a bit of scar tissue with this as well and can tell you all about how the different network file systems fail in special and wonderful ways. but let's deal with the editor first. yes, I would like to start a smarter editor project and the only way I could do it is with volunteers. I would not be able to write code for it until it had progressed significantly. What I can do is provide the knowledge of a first-generation system should look like, evolutionary corrections thereafter. I can also help with interfacing Python two to speech recognition and creation of grammars and their code. I'm a strong believer in recycling existing systems. We might get lucky and make it better for them to. if you're willing to help me find volunteers, get this started (I only need some nudges in the right direction), I gladly accept your help and your knowledge. From rajat.dudeja at gmail.com Thu Jul 2 02:50:43 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Wed, 1 Jul 2009 23:50:43 -0700 (PDT) Subject: Accessing windows structures through ctypes. References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Message-ID: <8c8b5cf2-bc77-4633-96ca-e3b908430492@z14g2000yqa.googlegroups.com> > > Using ctypes can I access the windows structures like: > > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > > PEB_LDR_DATA, etc? > > ctypes.wintypes lists all of the Windows structures included with the > module. > > You should be able to use ctypes.Structure class to roll your own: Thanks Alex. As you suggested, I'm trying to implemenet the below structure, windows PEB, in Python: typedef struct _PEB { BYTE Reserved1[2]; BYTE BeingDebugged; BYTE Reserved2[21]; PPEB_LDR_DATA LoaderData; PRTL_USER_PROCESS_PARAMETERS ProcessParameters; BYTE Reserved3[520]; PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine; BYTE Reserved4[136]; ULONG SessionId; } PEB; My equivalent Python structure is: class PEB(Structure): _fields_ = [("Reserved1", wintypes.BYTE * 2), ("BeingDebugged", wintypes.BYTE), ("Reserved2", wintypes.BYTE * 2), ("Reserved3", c_void_p), ("Ldr", pointer(PEB_LDR_DATA)), ("ProcessParameters", pointer (RTL_USER_PROCESS_PARAMETERS)), ("Reserved4", wintypes.BYTE * 104), ("Reserved5", c_void_p), (), ("Reserved6", wintypes.BYTE), ("Reserved7", c_void_p), ("SessionId", c_ulong)] I'm not sure what needs to go in the above empty tuple for "PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine" (in Original PEB). Please suggest. From sgm at objexx.com Thu Jul 2 02:54:25 2009 From: sgm at objexx.com (SK) Date: Wed, 1 Jul 2009 23:54:25 -0700 (PDT) Subject: multiprocessing and freezing on Windows Message-ID: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Is there a method for freezing a Python 2.6 app using multiprocessing on Windows using PyInstaller or py2exe that works? It is trying to call my executable instead of python.exe when the process starts and passes it --multiprocessing-fork . Adding a freeze_support() to my main doesn't help. Do I have to bundle all of Python and use set_executable() to point to the bundled python.exe? I can't find any workable information on the canonical way to do this. Thanks! From clp2 at rebertia.com Thu Jul 2 03:02:41 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Jul 2009 00:02:41 -0700 Subject: Open Source RSS Reader in Python? In-Reply-To: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: <50697b2c0907020002o16536566h948856375ea3289e@mail.gmail.com> On Wed, Jul 1, 2009 at 4:18 PM, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? Straw for GTK/GNOME is written in Python, though it's somewhat inactive. http://projects.gnome.org/straw/ http://strawreader.wordpress.com/ Cheers, Chris -- http://blog.rebertia.com From eckhardt at satorlaser.com Thu Jul 2 03:11:48 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Thu, 02 Jul 2009 09:11:48 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors Message-ID: Hi! I'm currently converting my bioware to handle Python code and I have stumbled across a problem... Simple scenario: I have a handle to a resource. This handle allows me to manipulate the resource in various ways and it also represents ownership. Now, when I put this into a class, instances to that class can be shared, using Python's reference counting. What I'm missing is a way to automatically release the resource, something which I would do in the destructor in C++. Any ideas how to solve this? Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From 4564 at 755189.45 Thu Jul 2 03:42:09 2009 From: 4564 at 755189.45 (Enrico) Date: Thu, 2 Jul 2009 09:42:09 +0200 Subject: Accessing windows structures through ctypes. References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> <8c8b5cf2-bc77-4633-96ca-e3b908430492@z14g2000yqa.googlegroups.com> Message-ID: <4a4c650c$0$1115$4fafbaef@reader3.news.tin.it> "Rajat" ha scritto nel messaggio news:8c8b5cf2-bc77-4633-96ca-e3b908430492 at z14g2000yqa.googlegroups.com... > > > > Using ctypes can I access the windows structures like: > > > > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > > > PEB_LDR_DATA, etc? > > > > ctypes.wintypes lists all of the Windows structures included with the > > module. > > > > You should be able to use ctypes.Structure class to roll your own: > > Thanks Alex. As you suggested, I'm trying to implemenet the below > structure, windows PEB, in Python: > > typedef struct _PEB { > BYTE Reserved1[2]; > BYTE BeingDebugged; > BYTE Reserved2[21]; > PPEB_LDR_DATA LoaderData; > PRTL_USER_PROCESS_PARAMETERS ProcessParameters; > BYTE Reserved3[520]; > PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine; > BYTE Reserved4[136]; > ULONG SessionId; > } PEB; > > My equivalent Python structure is: > class PEB(Structure): > _fields_ = [("Reserved1", wintypes.BYTE * 2), > ("BeingDebugged", wintypes.BYTE), > ("Reserved2", wintypes.BYTE * 2), > ("Reserved3", c_void_p), > ("Ldr", pointer(PEB_LDR_DATA)), > ("ProcessParameters", pointer > (RTL_USER_PROCESS_PARAMETERS)), > ("Reserved4", wintypes.BYTE * 104), > ("Reserved5", c_void_p), > (), > ("Reserved6", wintypes.BYTE), > ("Reserved7", c_void_p), > ("SessionId", c_ulong)] > > I'm not sure what needs to go in the above empty tuple for > "PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine" (in Original > PEB). > > Please suggest. PostProcessInitRoutine should be a callback function or something similar. It should be enough to define a type PostProcessInitRoutine = WINFUNCTYPE(...parameters...) and use this. Regards, Enrico From Joachim at Strombergson.com Thu Jul 2 03:57:14 2009 From: Joachim at Strombergson.com (=?UTF-8?B?Sm9hY2hpbSBTdHLDtm1iZXJnc29u?=) Date: Thu, 02 Jul 2009 09:57:14 +0200 Subject: PEP 376 In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <4A4C685A.8020300@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! Richard Brodie wrote: > "Joachim Str?mbergson" wrote in message > news:mailman.2422.1246418400.8015.python-list at python.org... > >> Even so, choosing md5 in 2009 for something that (hopefully) will be >> used in years is a bad design decision. It creates a dependency for to >> an algorithm that all sensible recommendations point you to move away >> from. > > Why not write the field as algorithm:value? > > e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 > > Installers can fallback to using hashlib.new(), so you can plug in a new > algorithm without changing the PEP or the installer code. +1 Good idea and future proof as well as being simple. - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMaFoACgkQZoPr8HT30QHVxQCfWqKprUaAQxaxTfYJVfIp8+FC 5UIAn2cWNoMe+sdNO4BSZG7e+19qkqvl =Id7s -----END PGP SIGNATURE----- From petshmidt at googlemail.com Thu Jul 2 04:25:29 2009 From: petshmidt at googlemail.com (Tep) Date: Thu, 2 Jul 2009 01:25:29 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Message-ID: <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> On 2 Jul., 01:56, MRAB wrote: > someone wrote: > > Hello, > > > how can I replace '?' sign from string? Or do split at that character? > > Getting unicode error if I try to do it: > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > 1: ordinal not in range(128) > > > Thanks, Pet > > > script is # -*- coding: UTF-8 -*- > > It sounds like you're mixing bytestrings with Unicode strings. I can't > be any more helpful because you haven't shown the code. Oh, I'm sorry. Here it is def cleanInput(input) return input.replace('?', '') From petshmidt at googlemail.com Thu Jul 2 04:31:46 2009 From: petshmidt at googlemail.com (Tep) Date: Thu, 2 Jul 2009 01:31:46 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> Message-ID: <9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> On 2 Jul., 10:25, Tep wrote: > On 2 Jul., 01:56, MRAB wrote: > > > someone wrote: > > > Hello, > > > > how can I replace '?' sign from string? Or do split at that character? > > > Getting unicode error if I try to do it: > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > > 1: ordinal not in range(128) > > > > Thanks, Pet > > > > script is # -*- coding: UTF-8 -*- > > > It sounds like you're mixing bytestrings with Unicode strings. I can't > > be any more helpful because you haven't shown the code. > > Oh, I'm sorry. Here it is > > def cleanInput(input) > ? ? return input.replace('?', '') I also need: #input is html source code, I have problem with only this character #input = 'foo ? bar' #return should be foo def splitInput(input) parts = input.split(' ? ') return parts[0] Thanks! From Joachim at Strombergson.com Thu Jul 2 04:32:04 2009 From: Joachim at Strombergson.com (=?UTF-8?B?Sm9hY2hpbSBTdHLDtm1iZXJnc29u?=) Date: Thu, 02 Jul 2009 10:32:04 +0200 Subject: PEP368 and pixeliterators Message-ID: <4A4C7084.60000@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! I just read the PEP368 and really liked the proposed idea, sound like a great battery addition to include in the std lib: http://www.python.org/dev/peps/pep-0368/ One question/idea though: The proposed iterator will iterate over all pixels in a line, but only one line. The example code looks like this: # iterate over an image for line in rgb_image: for pixel in line: # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, pixel.r But, wouldn't it be more Pythonic and simpler to have an iterator that iterates over all pixels in an image? Starting with upper left corner and moving left-right and (line by line) to lower right. This would change the code above to: for pixel in rgb_image: # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, pixel.r The idea I'm having is that fundamentally the image is made up of a 2D array of pixels, not rows of pixels. And having the iterator focus on pixels instead make then more sense, no? Or if possible have two iterators. Otherwise, big thumbs up for PEP 368 as well as having PEP:s for driving the language development process in an orderly and well documented way. - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMcIQACgkQZoPr8HT30QFkcgCgzxb9JS2l87B/nkpf05FLDjY5 RPQAni2yPxKjCd4lM/qMBNhjp8HHg/PZ =9gB/ -----END PGP SIGNATURE----- From jeremiah.dodds at gmail.com Thu Jul 2 04:44:44 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Thu, 2 Jul 2009 09:44:44 +0100 Subject: Determining if a function is a method of a class within a decorator In-Reply-To: <4A4AB863.1020205@ilm.com> References: <4A4AB863.1020205@ilm.com> Message-ID: <12cbbbfc0907020144s6d7848aci65bab54a726a9f22@mail.gmail.com> On Wed, Jul 1, 2009 at 2:14 AM, David Hirschfield wrote: > Unfortunately that still requires two separate decorators, when I was > hoping there was a way to determine if I was handed a function or method > from within the same decorator. > > Seems like there really isn't, so two decorators is the way to go. > Thanks, > -David > > This is a really horrible idea, but it may work: If you can rely on your code using the word "self", you could use the inspect module to look at the arguments of the function being decorated, and dispatch based on that. The idea itself feels extremely dirty though. -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Jul 2 05:11:34 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 11:11:34 +0200 Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> Message-ID: Pedram wrote: > On Jul 1, 10:01 pm, Christian Heimes wrote: >> Pedram schrieb: >> >> > Hello community, >> > I'm reading the CPython interpreter source code, >> > first, if you have something that I should know for better reading >> > this source code, I would much appreciate that :) >> > second, in intobject.c file, I read the following code in >> > fill_free_list function that I couldn't understand: >> > while (--q > p) >> > Py_TYPE(q) = (struct _typeobject *)(q-1); >> > Py_TYPE(q) = NULL; >> > >> > What does it mean? >> >> The code is abusing the ob_type member to store a single linked, NULL >> terminated list of free integer objects. It's a tricky optimization you >> don't have to understand in order to understand the rest of the code. >> >> And no, the code in ctypes.h is totally unrelated to the free list in >> intobject.c. >> >> Christian > > Thanks for reply. > I totally understood the fill_free_list() function. Thanks. > But I have problems in understanding the PyInt_FromLong(). Here's the > code: > > PyObject * > PyInt_FromLong(long ival) > { > register PyIntObject *v; > #if NSMALLNEGINTS + NSMALLPOSINTS > 0 > if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { > v = small_ints[ival + NSMALLNEGINTS]; > Py_INCREF(v); > #ifdef COUNT_ALLOCS > if (ival >= 0) > quick_int_allocs++; > else > quick_neg_int_allocs++; > #endif > return (PyObject *) v; > } > #endif > if (free_list == NULL) { > if ((free_list = fill_free_list()) == NULL) > return NULL; > } > > v = free_list; // <-- No problem till here :) > free_list = (PyIntObject *)Py_TYPE(v); > PyObject_INIT(v, &PyInt_Type); > v->ob_ival = ival; > return (PyObject *) v; > } > > I understood that small numbers (between -5 and 256) are referenced to > small_ints list. Others have to store in PyIntBlock, am I right? OK, > no problem till 'v = free_list;' but can't understand the line > 'free_list = (PyIntObject *)Py_TYPE(v);' :(. Where free_list > referenced to? Don't this correct that free_list have to be referenced > to another free block so next time the function called v store in this > location? > Sorry for my bad English. I hope you could understand me :) Well, if I understand fill_free_list() correctly, you haven't ;) It only makes sense together with the allocation code in PyInt_FromLong(). Py_TYPE(v) translates to v->ob_type, and in fill_free_list() that field was abused(*) to hold a pointer to the previous item in the array of free integers, thereby temporarily turning it into a linked list. Assuming that free_list pointed to block->objects[10] in the current PyIntBlock it will now point to block->objects[9]. When block->objects[0] is reached free_list will be set to NULL as block- >objects[0]->ob_type was initialized with NULL by the line Py_TYPE(q) = NULL; /* in fill_free_list() */ The next call of PyInt_FromLong() will then trigger the allocation of a new block. Peter (*) It may help if you think of the objects array as a union linked { linked *next; PyInt_Object obj; }; From davitadze.tengiz at gmail.com Thu Jul 2 05:23:00 2009 From: davitadze.tengiz at gmail.com (Tengiz Davitadze) Date: Thu, 2 Jul 2009 13:23:00 +0400 Subject: Need Help Message-ID: <2f0bc6280907020223kf2701au81da279a5f8fabaf@mail.gmail.com> Hello. I can't find a wright mail address. If you can help me I need to get an information about UNICODE. I am georgian and I need to write programs on georgian language . If you can transfer this mail or send me a wright mail about encoding or unicode information. -- Tengiz Davitadze { programing Future } -------------- next part -------------- An HTML attachment was scrubbed... URL: From sebastian.schabe at gmx.de Thu Jul 2 05:40:12 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Thu, 02 Jul 2009 11:40:12 +0200 Subject: deleting certain entries in numpy array In-Reply-To: References: <4a4b7763$1@news.fhg.de> Message-ID: <4a4c7fd9$1@news.fhg.de> Robert Kern schrieb: > First, convert the pos array to integers, and just the columns with > indices in them: > > ipos = pos[:,:2].astype(int) > > Now check the values in the mask corresponding to these positions: > > mask_values = mask[ipos[:,0], ipos[:,1]] > > Now extract the rows from the original pos array where mask_values is > nonzero: > > result = pos[mask_values != 0] > Great!!! That's the way I wanted. After reading the numpy reference guide I supposed the delete function was not the right way, but that it's all about cerrect indexing. But I didn't really know how. So thanks a lot, also to Ben. > > You will want to ask numpy questions on the numpy mailing list. > > http://www.scipy.org/Mailing_Lists > I ever thought news-groups are the right way for questions like this. And the mailing list page just confuses me, but I'm trying to get used to it. Sebastian From bearophileHUGS at lycos.com Thu Jul 2 05:50:17 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 2 Jul 2009 02:50:17 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> Ulrich Eckhardt: > a way to automatically release the resource, something > which I would do in the destructor in C++. Is this helpful? http://effbot.org/pyref/with.htm Bye, bearophile From gagsl-py2 at yahoo.com.ar Thu Jul 2 06:10:17 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 07:10:17 -0300 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: En Wed, 01 Jul 2009 12:49:31 -0300, Scott David Daniels escribi?: > Gabriel Genellina wrote: >> ... >> def convert(in_queue, out_queue): >> while True: >> row = in_queue.get() >> if row is None: break >> # ... convert row >> out_queue.put(converted_line) > > These loops work well with the two-argument version of iter, > which is easy to forget, but quite useful to have in your bag > of tricks: > > def convert(in_queue, out_queue): > for row in iter(in_queue.get, None): > # ... convert row > out_queue.put(converted_line) Yep, I always forget about that variant of iter() -- very handy! -- Gabriel Genellina From eckhardt at satorlaser.com Thu Jul 2 06:12:19 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Thu, 02 Jul 2009 12:12:19 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> Message-ID: <3pduh6-g52.ln1@satorlaser.homedns.org> Bearophile wrote: > Ulrich Eckhardt: >> a way to automatically release the resource, something >> which I would do in the destructor in C++. > > Is this helpful? > http://effbot.org/pyref/with.htm Yes, it aims in the same direction. However, I'm not sure this applies to my case. The point is that the resource handle is not just used locally in a restricted scope but it is allocated and stored. The 'with' is something that makes sense in the context of mutex locking, where you have a well-defined critical section. What I need is something similar to open(), which returs a file. When the last reference to that object goes out of scope, the underlying file object is closed. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From __peter__ at web.de Thu Jul 2 06:46:27 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 12:46:27 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: Ulrich Eckhardt wrote: > Bearophile wrote: >> Ulrich Eckhardt: >>> a way to automatically release the resource, something >>> which I would do in the destructor in C++. >> >> Is this helpful? >> http://effbot.org/pyref/with.htm > > Yes, it aims in the same direction. However, I'm not sure this applies to > my case. The point is that the resource handle is not just used locally in > a restricted scope but it is allocated and stored. The 'with' is something > that makes sense in the context of mutex locking, where you have a > well-defined critical section. Isn't that exactly what RAII does? > What I need is something similar to open(), > which returs a file. When the last reference to that object goes out of > scope, the underlying file object is closed. You can go ahead and implement a __del__() method. It will often work in CPython, but you get no guarantees, especially when you have reference cycles and with other Python implementations that don't use refcounting. Peter From pm567426 at gmail.com Thu Jul 2 06:55:42 2009 From: pm567426 at gmail.com (Pedram) Date: Thu, 2 Jul 2009 03:55:42 -0700 (PDT) Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> Message-ID: <7c8e96a1-cbb2-4036-9aa1-1ecdfd84cca8@j32g2000yqh.googlegroups.com> On Jul 2, 1:11?pm, Peter Otten <__pete... at web.de> wrote: > Pedram wrote: > > On Jul 1, 10:01 pm, Christian Heimes wrote: > >> Pedram schrieb: > > >> > Hello community, > >> > I'm reading the CPython interpreter source code, > >> > first, if you have something that I should know for better reading > >> > this source code, I would much appreciate that :) > >> > second, in intobject.c file, I read the following code in > >> > fill_free_list function that I couldn't understand: > >> > while (--q > p) > >> > Py_TYPE(q) = (struct _typeobject *)(q-1); > >> > Py_TYPE(q) = NULL; > >> > > >> > What does it mean? > > >> The code is abusing the ob_type member to store a single linked, NULL > >> terminated list of free integer objects. It's a tricky optimization you > >> don't have to understand in order to understand the rest of the code. > > >> And no, the code in ctypes.h is totally unrelated to the free list in > >> intobject.c. > > >> Christian > > > Thanks for reply. > > I totally understood the fill_free_list() function. Thanks. > > But I have problems in understanding the PyInt_FromLong(). Here's the > > code: > > > PyObject * > > PyInt_FromLong(long ival) > > { > > register PyIntObject *v; > > #if NSMALLNEGINTS + NSMALLPOSINTS > 0 > > if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { > > v = small_ints[ival + NSMALLNEGINTS]; > > Py_INCREF(v); > > #ifdef COUNT_ALLOCS > > if (ival >= 0) > > quick_int_allocs++; > > else > > quick_neg_int_allocs++; > > #endif > > return (PyObject *) v; > > } > > #endif > > if (free_list == NULL) { > > if ((free_list = fill_free_list()) == NULL) > > return NULL; > > } > > > v = free_list; // <-- No problem till here :) > > free_list = (PyIntObject *)Py_TYPE(v); > > PyObject_INIT(v, &PyInt_Type); > > v->ob_ival = ival; > > return (PyObject *) v; > > } > > > I understood that small numbers (between -5 and 256) are referenced to > > small_ints list. Others have to store in PyIntBlock, am I right? OK, > > no problem till 'v = free_list;' but can't understand the line > > 'free_list = (PyIntObject *)Py_TYPE(v);' :(. Where free_list > > referenced to? Don't this correct that free_list have to be referenced > > to another free block so next time the function called v store in this > > location? > > Sorry for my bad English. I hope you could understand me :) > > Well, if I understand fill_free_list() correctly, you haven't ;) It only > makes sense together with the allocation code in PyInt_FromLong(). > > Py_TYPE(v) translates to v->ob_type, and in fill_free_list() that field was > abused(*) to hold a pointer to the previous item in the array of free > integers, thereby temporarily turning it into a linked list. > > Assuming that free_list pointed to block->objects[10] in the current > PyIntBlock it will now point to block->objects[9]. > > When block->objects[0] is reached free_list will be set to NULL as block- > > >objects[0]->ob_type was initialized with NULL by the line > > Py_TYPE(q) = NULL; /* in fill_free_list() */ > > The next call of PyInt_FromLong() will then trigger the allocation of a new > block. > > Peter > > (*) It may help if you think of the objects array as a > > union linked { > ? ? linked *next; > ? ? PyInt_Object obj; > > > > > > }; > > Oh, I got it! What a wonderful implementation! :o Thanks so much Peter. You made my day :) I didn't read the code carefully. > > ;) > From no.email at please.post Thu Jul 2 07:19:40 2009 From: no.email at please.post (kj) Date: Thu, 2 Jul 2009 11:19:40 +0000 (UTC) Subject: Why re.match()? References: Message-ID: In Steven D'Aprano writes: >On Thu, 02 Jul 2009 03:49:57 +0000, kj wrote: >> In Duncan Booth >> writes: >>>So, for example: >> >>>>>> re.compile("c").match("abcdef", 2) >>><_sre.SRE_Match object at 0x0000000002C09B90> >>>>>> re.compile("^c").search("abcdef", 2) >>>>>> >>>>>> >> I find this unconvincing; with re.search alone one could simply do: >> >>>>> re.compile("^c").search("abcdef"[2:]) >> <_sre.SRE_Match object at 0x75918> >> >> No need for re.match(), at least as far as your example shows. >Your source string "abcdef" is tiny. Consider the case where the source >string is 4GB of data. You want to duplicate the whole lot, minus two >characters. Not so easy now. I'm sure that it is possible to find cases in which the *current* implementation of re.search() would be inefficient, but that's because this implementation is perverse, which, I guess, is ultimately the point of my original post. Why privilege the special case of a start-of-string anchor? What if you wanted to apply an end-anchored pattern to some prefix of your 4GB string? Why not have a special re method for that? And another for every possible special case? If the concern is efficiency for such cases, then simply implement optional offset and length parameters for re.search(), to specify any arbitrary substring to apply the search to. To have a special-case re.match() method in addition to a general re.search() method is antithetical to language minimalism, and plain-old bizarre. Maybe there's a really good reason for it, but it has not been mentioned yet. kj From nils at ccsg.de Thu Jul 2 07:30:44 2009 From: nils at ccsg.de (=?ISO-8859-1?Q?Nils_R=FCttershoff?=) Date: Thu, 02 Jul 2009 13:30:44 +0200 Subject: performance problem with time.strptime() Message-ID: <4A4C9A64.9030507@ccsg.de> Hi everyone, In my free time I translate scripts from open source projects or write my own, to train my python skills. ATM I convert the aplogmerge.pl from awstats. It merges multiple apache logfiles and sort the output by the timestamps of each line. My first version of this script hasn't a good performance, so I started profiling. It turned out that the script spend much time for converting the timestamps of the line into a struct_time object. Here a code example (Python 2.6.2 on Ubuntu 7.10): Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def strptime(): m = Rec.match(Line) if m: date_string = m.group(1) # date_string example: '02/Jul/2009:01:50:26' return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") with timeit this functions takes approximate 125 sec and 29.004.081 function calls (I've configured timeit with 1.000.000 runs). A look at the output of cProfile told me that more than the half time is spent in locale.py: 1000002 11.712 0.000 19.592 0.000 locale.py:316(normalize) 1000002 3.639 0.000 23.231 0.000 locale.py:382(_parse_localename) 1000002 5.162 0.000 30.271 0.000 locale.py:481(getlocale) I studied the time documentation and thought that I had to set TZ in os environ: os.environ['TZ'] = 'Europe/Berlin' time.set() but that had no effect. I don't know why time.strptime() looks every time for my locale. Maybe it's a problem with my OS locale... However. I've introduced a work around, which works perfectly for my problem. For time comparison I could use any sort of time representation and so I convert to epoch: # needed to translate month str to dig repr Shortmonth = {'Jan' : '01', 'Feb' : '02', 'Mar' : '03', 'Apr' : '04', 'May' : '05', 'Jun' : '06', 'Jul' : '07', 'Aug' : '08', 'Sep' : '09', 'Oct' : '10', 'Nov' : '11', 'Dec' : '12'} Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def epoch(): m = Rec.match(Line) if m: result = m.groupdict() date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) date_tuple = map(int,date_tuple) return time.mktime(date_tuple) with this workaround I had a speed up to 4 times; it tooks only 31 sec with only 5.000.009 function calls. Maybe this helps some of you, who had the similar problems with time conversion.... ...But one big question remains: Why time.strptime() checks everytime the locale? had I missed something or could I have a problem with my OS locale? With python 3.1 there is no difference, unless that time.strptime() took approx 12 sec longer... :( regards, Nils Here a complete test script: #!/opt/python/2.6.2/bin/python import time import timeit import cProfile import re # needed to tranlate month str to dig repr Shortmonth = {'Jan' : '01', 'Feb' : '02', 'Mar' : '03', 'Apr' : '04', 'May' : '05', 'Jun' : '06', 'Jul' : '07', 'Aug' : '08', 'Sep' : '09', 'Oct' : '10', 'Nov' : '11', 'Dec' : '12'} Rec1 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") Rec2 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def epoch(): m = Rec1.match(Line) if m: result = m.groupdict() date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) date_tuple = map(int,date_tuple) return time.mktime(date_tuple) def strptime(): m = Rec2.match(Line) if m: date_string = m.group(1) return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") if __name__ == "__main__": t1 = timeit.Timer("epoch()","from __main__ import epoch") t2 = timeit.Timer("strptime()", "from __main__ import strptime") cProfile.run("t1.timeit();print") print "" cProfile.run("t2.timeit();print") From simon at brunningonline.net Thu Jul 2 07:34:10 2009 From: simon at brunningonline.net (Simon Brunning) Date: Thu, 2 Jul 2009 12:34:10 +0100 Subject: Need Help In-Reply-To: <2f0bc6280907020223kf2701au81da279a5f8fabaf@mail.gmail.com> References: <2f0bc6280907020223kf2701au81da279a5f8fabaf@mail.gmail.com> Message-ID: <8c7f10c60907020434u26e0518el6045d23cd89c4bcf@mail.gmail.com> 2009/7/2 Tengiz Davitadze : > Hello. I can't find a wright mail address. If you can help me I need to get > an information about UNICODE. I am georgian and I need to write programs on > georgian language . If you can transfer this mail or send me a wright mail > about encoding or unicode information. Our chief link is . and . Our two links are and . And . Our *three* links are , , and . And . Our *four*...no... *Amongst* our links.... Amongst our linkry are such elements as , .... I'll come in again. -- Cheers, Simon B. From Caseyweb at gmail.com Thu Jul 2 07:45:22 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Thu, 2 Jul 2009 04:45:22 -0700 (PDT) Subject: performance problem with time.strptime() References: Message-ID: <6a2cc3a6-0e5c-4a43-bcf7-416a77253f97@h2g2000yqg.googlegroups.com> On Jul 2, 7:30?am, Nils R?ttershoff wrote: > Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' I'm not sure how much it will help but if you are only using the regex to get the date/time group element, it might be faster to replace the regex with: >>> date_string = Line.split()[3][1:-1] From ziade.tarek at gmail.com Thu Jul 2 07:55:30 2009 From: ziade.tarek at gmail.com (=?ISO-8859-1?Q?Tarek_Ziad=E9?=) Date: Thu, 2 Jul 2009 13:55:30 +0200 Subject: PEP 376 In-Reply-To: <4A4C685A.8020300@Strombergson.com> References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> Message-ID: <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> 2009/7/2 Joachim Str?mbergson : > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Aloha! > > Richard Brodie wrote: >> "Joachim Str?mbergson" wrote in message >> news:mailman.2422.1246418400.8015.python-list at python.org... >> >>> Even so, choosing md5 in 2009 for something that (hopefully) will be >>> used in years is a bad design decision. It creates a dependency for to >>> an algorithm that all sensible recommendations point you to move away >>> from. >> >> Why not write the field as algorithm:value? >> >> e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 >> >> Installers can fallback to using hashlib.new(), so you can plug in a new >> algorithm without changing the PEP or the installer code. > > +1 > > Good idea and future proof as well as being simple. The prefix is a good idea but since it's just a checksum to control that the file hasn't changed what's wrong with using a weak hash algorithm like md5 or now sha1 ? If someone wants to modify a file of a distribution he can recreate the checksum as well, the only secured way to prevent that would be to use gpg keys but isn't that overkill for what we need ? e.g. making sure a file wasn't modified when distutils uninstalls a distribution. Tarek From hniksic at xemacs.org Thu Jul 2 07:55:39 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 02 Jul 2009 13:55:39 +0200 Subject: Why re.match()? References: Message-ID: <87k52rwb7o.fsf@busola.homelinux.net> kj writes: > For a recovering Perl-head like me it is difficult to understand > why Python's re module offers both match and search. Why not just > use search with a beginning-of-string anchor? I need re.match when parsing the whole string. In that case I never want to search through the string, but process the whole string with some regulat expression, for example when tokenizing. For example: pos = 0 while pos != len(s): match = TOKEN_RE.match(s, pos) if match: process_token(match) pos = match.end() else: raise ParseError('invalid syntax at position %d' % pos) From davea at ieee.org Thu Jul 2 08:05:12 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 02 Jul 2009 08:05:12 -0400 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: References: Message-ID: <4A4CA278.4050604@ieee.org> Ulrich Eckhardt wrote: > Hi! > > I'm currently converting my bioware to handle Python code and I have > stumbled across a problem... > > Simple scenario: I have a handle to a resource. This handle allows me to > manipulate the resource in various ways and it also represents ownership. > Now, when I put this into a class, instances to that class can be shared, > using Python's reference counting. What I'm missing is a way to > automatically release the resource, something which I would do in the > destructor in C++. > > Any ideas how to solve this? > > Uli > > As someone else pointed out, 'with' is the first line of defense. It makes the stuff you could already do with try/except/finally much easier to get right. Look also at 'del' a command in the language which explicitly deletes an object. But I'm guessing you want something that automatically deletes objects whenever the last reference disappears. That's an implementation detail, not a language guarantee. In particular CPython does what you want, by using reference counting. That's the only Python I've used, so it's only hearsay when I say that other implementations, (maybe Cython or Jython) do not all work the same way. In CPython, any object whose *last* reference goes away, will get immediately deleted. So if you avoid circular references, it should do just what you want. Orphaned circular references are caught by the garbage collector, which runs periodically, but is non-deterministic. Two more caveats. Exceptions tend to hang onto stuff in their near vicinity, and I can't tell you the algorithm for what happens. But an explicit del in the except clause can probably handle that. Finally, closures can hang onto stuff. So if you have nested functions, or lambda functions, it's possible they're going to keep things longer than you realize. From lists at cheimes.de Thu Jul 2 08:10:55 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 02 Jul 2009 14:10:55 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: <4A4CA278.4050604@ieee.org> References: <4A4CA278.4050604@ieee.org> Message-ID: Dave Angel wrote: > Look also at 'del' a command in the language which explicitly deletes an > object. No, you are either explaining it the wrong way or you have been fallen for a common misinterpretation of the del statement. The del statement only removes the object from the current scope. This means it decreases the reference count by one. It does *not* remove the object. Christian From Caseyweb at gmail.com Thu Jul 2 08:17:28 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Thu, 2 Jul 2009 05:17:28 -0700 (PDT) Subject: PEP368 and pixeliterators References: Message-ID: <2aeeb8de-c8f3-4e9d-98f7-d1fa71e1f3a3@d4g2000yqa.googlegroups.com> On Jul 2, 4:32?am, Joachim Str?mbergson wrote: > But, wouldn't it be more Pythonic and simpler to have an iterator that > iterates over all pixels in an image? Starting with upper left corner > and moving left-right and (line by line) to lower right. This would > change the code above to: Unless I'm totally misreading the PEP, the author does provide both iterators. Quoting the PEP: Non-planar images offer the following additional methods: pixels() -> iterator[pixel] Returns an iterator that iterates over all the pixels in the image, starting from the top line and scanning each line from left to right. See below for a description of the pixel objects. __iter__() -> iterator[line] Returns an iterator that iterates over all the lines in the image, from top to bottom. See below for a description of the line objects. From __peter__ at web.de Thu Jul 2 08:54:50 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 14:54:50 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: Dave Angel wrote: > But I'm guessing you want something that automatically deletes objects > whenever the last reference disappears. That's an implementation > detail, not a language guarantee. In particular CPython does what you > want, by using reference counting. That's the only Python I've used, so > it's only hearsay when I say that other implementations, (maybe Cython > or Jython) do not all work the same way. Here are some examples from Kubuntu 9.04's zoo of python implementations: $ cat del.py import sys print sys.version class A(object): def __init__(self, x): self.x = x def __del__(self): print "releasing A(%r)" % self.x def f(): a = A("local in function") f() a = A("global (one ref)") c = A("global (cycle)") c.a = c del c b = A("global (no refs)") del b print "about to quit" $ python del.py 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) [GCC 4.3.3] releasing A('local in function') releasing A('global (no refs)') about to quit releasing A('global (one ref)') $ jython del.py 2.2.1 about to quit $ ipy del.py 2.4.0 (IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.42) about to quit releasing A('global (no refs)') releasing A('global (cycle)') releasing A('local in function') Unhandled Exception: System.ArgumentException: I/O operation on closed file at IronPython.Runtime.PythonFile.ThrowIfClosed () [0x00000] at IronPython.Runtime.PythonFile.Write (System.String s) [0x00000] $ IronPython sometimes segfaulted. Peter From nils at ccsg.de Thu Jul 2 09:00:11 2009 From: nils at ccsg.de (=?ISO-8859-1?Q?Nils_R=FCttershoff?=) Date: Thu, 02 Jul 2009 15:00:11 +0200 Subject: performance problem with time.strptime() In-Reply-To: <6a2cc3a6-0e5c-4a43-bcf7-416a77253f97@h2g2000yqg.googlegroups.com> References: <6a2cc3a6-0e5c-4a43-bcf7-416a77253f97@h2g2000yqg.googlegroups.com> Message-ID: <4A4CAF5B.2040502@ccsg.de> Hi Casey Casey Webster wrote: > On Jul 2, 7:30 am, Nils R?ttershoff wrote: > > >> Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") >> Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' >> > > I'm not sure how much it will help but if you are only using the regex > to get the date/time group element, it might be faster to replace the > regex with: > > >>>> date_string = Line.split()[3][1:-1] >>>> Indeed this would give a little speed up (by 1000000 iteration approx 3-4 sec). But this would be only a small piece of the cake. Although thx :) The problem is that time.strptime() consult locale.py for each iteration. Here the hole cProfile trace: first with epoch and second with strptime (condensed): 5000009 function calls in 33.084 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 33.084 33.084 :1() 1 2.417 2.417 33.084 33.084 :2(inner) 1000000 9.648 0.000 30.667 0.000 time_test.py:30(epoch) 1 0.000 0.000 33.084 33.084 timeit.py:177(timeit) 1000000 3.711 0.000 3.711 0.000 {built-in method groupdict} 1000000 4.318 0.000 4.318 0.000 {built-in method match} 1 0.000 0.000 0.000 0.000 {gc.disable} 1 0.000 0.000 0.000 0.000 {gc.enable} 1 0.000 0.000 0.000 0.000 {gc.isenabled} 1000000 7.764 0.000 7.764 0.000 {map} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 1000000 5.225 0.000 5.225 0.000 {time.mktime} 2 0.000 0.000 0.000 0.000 {time.time} ################################################################ 29000009 function calls in 124.449 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 124.449 124.449 :1() 1 2.244 2.244 124.449 124.449 :2(inner) 1000000 3.500 0.000 33.559 0.000 _strptime.py:27(_getlang) 1000000 41.814 0.000 100.754 0.000 _strptime.py:295(_strptime) 1000000 4.010 0.000 104.764 0.000 _strptime.py:453(_strptime_time) 1000000 11.647 0.000 19.529 0.000 locale.py:316(normalize) 1000000 3.638 0.000 23.167 0.000 locale.py:382(_parse_localename) 1000000 5.120 0.000 30.059 0.000 locale.py:481(getlocale) 1000000 7.242 0.000 122.205 0.000 time_test.py:37(strptime) 1 0.000 0.000 124.449 124.449 timeit.py:177(timeit) 1000000 1.771 0.000 1.771 0.000 {_locale.setlocale} 1000000 1.735 0.000 1.735 0.000 {built-in method __enter__} 1000000 1.626 0.000 1.626 0.000 {built-in method end} 1000000 3.854 0.000 3.854 0.000 {built-in method groupdict} 1000000 1.646 0.000 1.646 0.000 {built-in method group} 2000000 8.409 0.000 8.409 0.000 {built-in method match} 1 0.000 0.000 0.000 0.000 {gc.disable} 1 0.000 0.000 0.000 0.000 {gc.enable} 1 0.000 0.000 0.000 0.000 {gc.isenabled} 2000000 2.942 0.000 2.942 0.000 {len} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 3000000 4.552 0.000 4.552 0.000 {method 'get' of 'dict' objects} 1000000 2.072 0.000 2.072 0.000 {method 'index' of 'list' objects} 1000000 1.517 0.000 1.517 0.000 {method 'iterkeys' of 'dict' objects} 2000000 3.113 0.000 3.113 0.000 {method 'lower' of 'str' objects} 2000000 3.233 0.000 3.233 0.000 {method 'replace' of 'str' objects} 2000000 2.953 0.000 2.953 0.000 {method 'toordinal' of 'datetime.date' objects} 1000000 1.476 0.000 1.476 0.000 {method 'weekday' of 'datetime.date' objects} 1000000 4.332 0.000 109.097 0.000 {time.strptime} 2 0.000 0.000 0.000 0.000 {time.time} -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Jul 2 09:00:44 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 02 Jul 2009 09:00:44 -0400 Subject: performance problem with time.strptime() In-Reply-To: <4A4C9A64.9030507@ccsg.de> References: <4A4C9A64.9030507@ccsg.de> Message-ID: <4A4CAF7C.9040800@ieee.org> Nils R?ttershoff wrote: > Hi everyone, > > In my free time I translate scripts from open source projects or write > my own, to train my python skills. ATM I convert the aplogmerge.pl from > awstats. It merges multiple apache logfiles and sort the output by the > timestamps of each line. My first version of this script hasn't a good > performance, so I started profiling. It turned out that the script spend > much time for converting the timestamps of the line into a struct_time > object. Here a code example (Python 2.6.2 on Ubuntu 7.10): > > Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' > > def strptime(): > m = Rec.match(Line) > if m: > date_string = m.group(1) > # date_string example: '02/Jul/2009:01:50:26' > return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") > > with timeit this functions takes approximate 125 sec and 29.004.081 > function calls (I've configured timeit with 1.000.000 runs). A look at > the output of cProfile told me that more than the half time is spent in > locale.py: > > 1000002 11.712 0.000 19.592 0.000 locale.py:316(normalize) > > 1000002 3.639 0.000 23.231 0.000 locale.py:382(_parse_localename) > > 1000002 5.162 0.000 30.271 0.000 locale.py:481(getlocale) > > > > I studied the time documentation and thought that I had to set TZ in os > environ: > > os.environ['TZ'] = 'Europe/Berlin' > > time.set() > > > but that had no effect. I don't know why time.strptime() looks every > time for my locale. Maybe it's a problem with my OS locale... However. > I've introduced a work around, which works perfectly for my problem. For > time comparison I could use any sort of time representation and so I > convert to epoch: > > # needed to translate month str to dig repr > > Shortmonth = {'Jan' : '01', > > 'Feb' : '02', > > 'Mar' : '03', > > 'Apr' : '04', > > 'May' : '05', > > 'Jun' : '06', > > 'Jul' : '07', > > 'Aug' : '08', > > 'Sep' : '09', > > 'Oct' : '10', > > 'Nov' : '11', > > 'Dec' : '12'} > > Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") > > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' > > def epoch(): > > m = Rec.match(Line) > > if m: > > result = m.groupdict() > > date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) > > date_tuple = map(int,date_tuple) > > return time.mktime(date_tuple) > > > with this workaround I had a speed up to 4 times; it tooks only 31 sec > with only 5.000.009 function calls. Maybe this helps some of you, who > had the similar problems with time conversion.... > > ...But one big question remains: Why time.strptime() checks everytime > the locale? had I missed something or could I have a problem with my OS > locale? > > With python 3.1 there is no difference, unless that time.strptime() took > approx 12 sec longer... :( > > regards, Nils > > > > Here a complete test script: > > #!/opt/python/2.6.2/bin/python > > import time > > import timeit > > import cProfile > > import re > > # needed to tranlate month str to dig repr > > Shortmonth = {'Jan' : '01', > > 'Feb' : '02', > > 'Mar' : '03', > > 'Apr' : '04', > > 'May' : '05', > > 'Jun' : '06', > > 'Jul' : '07', > > 'Aug' : '08', > > 'Sep' : '09', > > 'Oct' : '10', > > 'Nov' : '11', > > 'Dec' : '12'} > > Rec1 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") > > Rec2 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") > > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' > > def epoch(): > > m = Rec1.match(Line) > > if m: > > result = m.groupdict() > > date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) > > date_tuple = map(int,date_tuple) > > return time.mktime(date_tuple) > > def strptime(): > > m = Rec2.match(Line) > > if m: > > date_string = m.group(1) > > return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") > > if __name__ == "__main__": > > t1 = timeit.Timer("epoch()","from __main__ import epoch") > > t2 = timeit.Timer("strptime()", "from __main__ import strptime") > > cProfile.run("t1.timeit();print") > > print "" > > cProfile.run("t2.timeit();print") > > > > As you say, if you don't actually need the datetime fields, why waste time generating them. You gained a lot more than the time spent in locale(), so your algorithm must be faster than the one in strptime(). That frequently happens when you write a special case for code which is otherwise general. Notice that only a quarter of the time is spent in locale logic (30.271 secs). You can't add up the three terms since they're measuring the same thing. Every call to getlocale() results in a call to _parse_localename(), so the cumulative times of the latter are already included in the cumulative times in getlocale(). Why does it need to call locale() ? How else is it going to know how to spell the 11th month? From Joachim at Strombergson.com Thu Jul 2 09:15:18 2009 From: Joachim at Strombergson.com (=?UTF-8?B?Sm9hY2hpbSBTdHLDtm1iZXJnc29u?=) Date: Thu, 02 Jul 2009 15:15:18 +0200 Subject: PEP 376 In-Reply-To: <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> Message-ID: <4A4CB2E6.8010607@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! Tarek Ziad? wrote: > The prefix is a good idea but since it's just a checksum to control > that the file hasn't changed > what's wrong with using a weak hash algorithm like md5 or now sha1 ? Because it creates a dependency to an old algorithm that should be deprecated. Also using MD5, even for a thing like this might make people belive that it is an ok algorithm to use - "Hey, it is used by the default install in Python, so it must be ok, right?" If we flip the argument around: Why would you want to use MD5 instead of SHA-256? For the specific use case the performance will not (should not) be an issue. As I wrote a few mails ago, it is time to move forward from MD5 and designing something in 2009 that will be around for many years that uses MD5 is (IMHO) a bad design decision. > If someone wants to modify a file of a distribution he can recreate > the checksum as well, > the only secured way to prevent that would be to use gpg keys but > isn't that overkill for what we need ? Actually, adding this type of security would IMHO be a good idea. - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMsuYACgkQZoPr8HT30QELagCghfYyHyK5jnkS8DlaQ2ZX4KR8 W+YAniWSvWRvm47/xGu0thTaYioETY94 =2x3X -----END PGP SIGNATURE----- From Joachim at Strombergson.com Thu Jul 2 09:16:36 2009 From: Joachim at Strombergson.com (=?ISO-8859-1?Q?Joachim_Str=F6mbergson?=) Date: Thu, 02 Jul 2009 15:16:36 +0200 Subject: PEP368 and pixeliterators In-Reply-To: <2aeeb8de-c8f3-4e9d-98f7-d1fa71e1f3a3@d4g2000yqa.googlegroups.com> References: <2aeeb8de-c8f3-4e9d-98f7-d1fa71e1f3a3@d4g2000yqa.googlegroups.com> Message-ID: <4A4CB334.9080505@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! Casey Webster wrote: > Unless I'm totally misreading the PEP, the author does provide both > iterators. Quoting the PEP: > > Non-planar images offer the following additional methods: > > pixels() -> iterator[pixel] > > Returns an iterator that iterates over all the pixels in the image, > starting from the top line and scanning each line from left to > right. > See below for a description of the pixel objects. Ah! I knew there was a reason for me wearing glasses. ;-) Go PEP368! - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMszQACgkQZoPr8HT30QGmVACfezmlZ+K93exuIEoUDJyzNzgM 3dcAn1x4Ghu9AlMKEme2og4sK4TwhXaR =bNbl -----END PGP SIGNATURE----- From davea at dejaviewphoto.com Thu Jul 2 09:21:40 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Thu, 02 Jul 2009 09:21:40 -0400 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: References: <4A4CA278.4050604@ieee.org> Message-ID: <4A4CB464.80607@dejaviewphoto.com> Christian Heimes wrote: > Dave Angel wrote: > >> Look also at 'del' a command in the language which explicitly deletes an >> object. >> > > No, you are either explaining it the wrong way or you have been fallen > for a common misinterpretation of the del statement. The del statement > only removes the object from the current scope. This means it decreases > the reference count by one. It does *not* remove the object. > > Christian > > > You're right of course. What I was trying to say was it deletes the reference to the object. Unlike obj = None, del obj removes the reference (attribute) entirely. Although I don't know what it should be called if it's a local variable. Perhaps it "unbinds" the name. DaveA From Mariosky at gmail.com Thu Jul 2 09:27:36 2009 From: Mariosky at gmail.com (Mario Garcia) Date: Thu, 2 Jul 2009 06:27:36 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <05dde563-a8f1-45d1-8f59-7740c58787ba@e21g2000yqb.googlegroups.com> Message-ID: <32f177b3-f305-4ff7-be64-ed161eb7c38d@h11g2000yqb.googlegroups.com> This could be better: >>> import random >>> population = range(10) >>> choice = random.choice(population) >>> population.remove(choice) >>> print population >>> print population [0, 1, 2, 3, 4, 5, 6, 8, 9] That was my idea with the previous pop(), remove from the population a certain number of elements at random. In the docs pop is defined as: Remove and return an arbitrary element from the set. My mistake: arbitrary is not the same as random :( Mario From lie.1296 at gmail.com Thu Jul 2 09:33:38 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 13:33:38 GMT Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <025949cb$0$20671$c3e8da3@news.astraweb.com> Message-ID: Eric S. Johansson wrote: > Steven D'Aprano wrote: >> That assumes that every word is all caps. In practice, for real-life >> Python code, I've tripled the vocal load of perhaps one percent of your >> utterances, which cuts your productivity by 2%. >> >> If you have 10000 words in you per day, and one percent get wrapped with >> a leading and trailing "capslock", you have: >> >> 9900 regular words plus 100 constants >> >> versus >> >> 9700 regular words plus 100 constants plus 200 by capslock. >> >> Your productivity goes down by 200 words out of 10,000, or two percent. > > oh I wish it was so simple. I would absolutely you if it was that simple the > real world. Somehow, I fear that you wouldn't understand unless you sat next to > me and watch me try to write code to a variety of standards. > > In practice, my experience says that with a really good speech driven > environment for programming, IT, vocal load probably by a quarter to a half and > be more compliant with current fashion for naming. I've tried (Windows') speech recognition some time before, and I know it's one heck of a mess trying to type anything with it. I have a fully working hand, so I don't actually need a voice recognition, but I remember trying to write a single (not code) paragraph solely by recognition. It took me over an hour correcting all those mistakes and it really starts to gets into the nerves after the first sentence. I did a little victory dance, after doing the first paragraph. I know they gets better with time as they learn our speech pattern and we learn how to pronounce the way the voice recognition expects. But one thing I can't imagine is using voice recognition for writing code (heck... that'll be a nightmare inside a nightmare) >>>>> Heck, have you ever noticed how most Python smart editors can't even >>>>> indent properly according to local contexts. Emacs is the only one and >>>>> even that one sometimes fails >>>> I use kwrite for editing, and I can't say I've ever noticed a failure. >>> okay, it fails from a speech recognition user perspective. Put the >>> cursor on the line and hit the tab key. Insert spaces/tabs in the line >>> of text. Not good for speech recognition. If mess of indentation and >>> hit the tab key, it doesn't automatically indent to the right location. >>> I'll be damned if I'm in the sit there speaking >>> "tabkeytabkeytabkeytabkeytabkey" because the error should take care of >>> it for me and save my voice. Like I said, Emacs does it correctly and >>> nobody else does as far as I can tell. >> I don't understand what you're describing. >> >> If you set kwrite to use "Python style" indent mode, then starting a new >> line will automatically indent to either the current indent level, or one >> extra indent level if the line ends with a colon. You shouldn't need to >> indent forward more than one level at a time when writing Python code, >> although you will need to dedent multiple levels on occasion. > > I recognize your confusion. It's very common when describing this problem. If I > had some way of making a movie of what and describing, obviously be more clear. > I'll see if I can make a movie with my camera that demonstrates the problem. It > probably won't happen fast but, what I tried your editor, it failed with a > trivial effort. I think I just hit the tab key multiple times in different > places on the line. In all cases it did the wrong thing. For me, the right thing > is to force the line to the right indentation based on the previous line to > matter how may times you get the tab key or where you are on that line when you > hit the tab key. I could be in the middle of the line, the end of line, start a > line, the start of the string and if I hit a tab key, that line should go to > exactly the right place stayed there no matter how me more times I typed a tab key. > > But this might be what you're getting confused. What I'm really asking for is > the ability to say "force indent" and have the line indent exactly correctly > based on context. It doesn't have to be the tab key. The tab key is the method > of convenience. Maybe we need an editor that could sort of understand something like: "constant red" and apply the appropriate code-sensitive convention transformation (in python it'll be RED) "class foo inheriting object" and typed "class Foo(object):\n\t" "indent", "dedent" put a tab or delete a tab (or softtab) but for all those, we're going to need a specialized text editor or perhaps a context-sensitive voice recognition software. The simplest solution would be a vim plugin or emacs mode that is specially tailored to recognize these commands and apply the appropriate transformations depending on the contexts. >> I've already said it's your right to reject the convention for your own >> code. Go right ahead. This is only a problem if you're editing other >> people's code which is using uppercase constants. > > exactly. If I'm unable to comply with local conventions, I cannot work within an > organization and be an active member of the team. This is one example of what I > mean by unintentional discriminatory effects of a given decision. rhetorical > devices aside, I think the right way to deal with accessibility issues and > decisions leading to discrimination is to make the appropriate tools if possible > for the individual. But I'm sure that would be a very long discussion we would > have over a couple of beers. Personally, I think it'd be very difficult to try to change conventions that were made for a job that requires so much typing. In programming, typing is almost a requirement. I'm aware people will still try to insist on doing things they love the most no matter what happens, but I still think if the disabled want to program, they're the one that need to adjust their work flow and definitely with the current voice recognition technology, they really need better tools. >> Do you wish to raise the profile of disabled programmers? > > yes. Not exactly sure of the best way but I would like to do that through > providing tools enabling them to participate on equal footing with not yet > disabled programmers. I really don't like saying "take pity on the poor > programmers with broken hands." I'd rather say "you need talent? Here's a pool > you have previously thrown away but they can now generate code faster than the > keyboard users. Stephen Hawking didn't stop with his experiments when his whole body can't move. I agree that disabilities shouldn't stop people from programming, which is mostly the head's job. I think if we can make voice recognition faster and easier than typing (like in those sci-fi movies), disabled or not we will all benefit from it. Maybe some day, we all, disabled or not, might code with voice recognition and keyboard will be considered obsolete. From lie.1296 at gmail.com Thu Jul 2 09:40:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 13:40:49 GMT Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> Message-ID: Eric S. Johansson wrote: > I've been working with speech recognition for 15 years. I've written something > on the order of 10,000 lines of Python code both as open source and private > projects. I've tried it least two dozen editors and they all fail miserably > because they're focused on keyboard use (but understandable) I get good > recognition accuracy because I train the system and then I let it train me. >> For coding, you might want to investigate a tool like Dasher[1] which >> offers an alternate form of input. It allows for custom >> vocabularies/keymaps if you need, as well as more precise specification >> of a full keyboard (caps vs. mixed-case, specific punctuation >> characters, etc). The predictive entry should be smart enough to pick >> up previously entered constants/terms saving you entry speed. It can >> also be driven by a wide variety of pointing devices (mouse, trackball, >> touchpad, head-tracker, gyro-input, etc). > > I've tried it, it's quite promising but my hands term are sufficiently that I > can't target accurately. This is a problem for me with mice as well. Maybe these > tiny little spit dot icons and webpages drive me insane because it takes me two > or three tries to put the right spot. You might want to try eye-ball tracking device. Dashers are designed for that as well. You won't need to use your hand to type with it. From nils at ccsg.de Thu Jul 2 09:51:25 2009 From: nils at ccsg.de (=?ISO-8859-1?Q?Nils_R=FCttershoff?=) Date: Thu, 02 Jul 2009 15:51:25 +0200 Subject: Spam? Re: whizBase vs. Python In-Reply-To: <0bc58d9a-37cf-4e09-a28f-5edb0489ea30@h8g2000yqm.googlegroups.com> References: <620ad7d3-6c1a-4869-b844-1951095bb92c@g19g2000yql.googlegroups.com> <0bc58d9a-37cf-4e09-a28f-5edb0489ea30@h8g2000yqm.googlegroups.com> Message-ID: <4A4CBB5D.9070104@ccsg.de> NurAzije wrote: > On Jun 29, 11:04 am, Tim Harig wrote: > >> On 2009-06-29, NurAzije wrote: >> >> >>> Hi, >>> I am working on a study and I need expert opinion, I did not work with >>> Python before, can anyone help me with a comparison betweenWhizBase >>> (www.whizbase.com) and Python please. >>> >> Given posts like:http://groups.google.com/group/Server-side-programing/browse_thread/t... >> is this just thinly veiled spam? The domain and the posting IP address are >> both out of Sarajevo, Bosnia. >> >> Obviously you alreadly know your product is nothing but a database >> macro processor. Python is a dynamic programming language and therefore >> far more capable. >> >> Your product is proprietary with less capability while Python is free and >> powerful. >> > > Hi Tim, > I am not a spammer, WhizBase is from Sarajevo and I am from Sarajevo, > for that it is interesting for me. > > regards, > Ashraf Gheith > Hi Ashraf, hm... but it seems you know WhizBase very well, here a blog (http://whizbase-scripting-language.blogspot.com/) of you with very detailed information about WhizBase. Why you are asking nearly any programming list/group about the difference?: http://www.highdots.com/forums/javascript-discussion-multi-lingual/whizbase-vs-javascript-281242.html http://groups.google.com/group/alt.comp.lang.learn.c-c++/browse_thread/thread/a7a47adb904319ad http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/9dfa76ad94d75168/01a999b7b8638621?lnk=raot http://www.mail-archive.com/dotnetdevelopment at googlegroups.com/msg07381.html Hm here the main blog: http://whizbase.blogspot.com/ There is a second contributer Faik Djikic and Whizbase is from a company called "Djikic Software Development"...... What's the Topic of your study? Whizbase compared to other languages? Why Whizbase? Is your study public and if so were we could read it? ;) Regards, Nils -------------- next part -------------- An HTML attachment was scrubbed... URL: From jcd at sdf.lonestar.org Thu Jul 2 10:15:49 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Thu, 02 Jul 2009 10:15:49 -0400 Subject: Idioms and Anti-Idioms Question In-Reply-To: References: Message-ID: <1246544149.5775.0.camel@aalcdl07> On Wed, 2009-07-01 at 17:19 +1200, Lawrence D'Oliveiro wrote: > In message , J. Cliff > Dyer wrote: > > > If the lines got separated, a leading + could disappear into its line > > without any errors showing up. A trailing + would raise a syntax error. > > Unless, of course, it was moved onto the previous line as part of whatever > caused the separation of the lines. How would you guard against that? > Can you give an example of what you mean? From gil.shinar at gmail.com Thu Jul 2 10:17:58 2009 From: gil.shinar at gmail.com (eranlevi) Date: Thu, 2 Jul 2009 07:17:58 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> Message-ID: <20e625ef-8fa2-4ed5-9ae4-a5b3fa2b1019@o6g2000yqj.googlegroups.com> On Jul 2, 1:08?am, s... at pobox.com wrote: > ? ? Gil> There's no such group as python-sybase :-( > > http://sourceforge.net/mailarchive/forum.php?forum_name=python-sybase... > > S Thanks :-) From gil.shinar at gmail.com Thu Jul 2 10:19:36 2009 From: gil.shinar at gmail.com (eranlevi) Date: Thu, 2 Jul 2009 07:19:36 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> Message-ID: On Jul 2, 1:07?am, s... at pobox.com wrote: > ? ? Gil> Are you saying, that when you trying to connect to a sybase DBS > ? ? Gil> server and the DBS or the server is down, you get an error after a > ? ? Gil> few seconds and not after a few minutes? > > Yes, though thankfully our server tends to almost always be up. > > ? ? Gil> Which python version are you using? > > We're still running 2.4.5 at work and have a slightly hacked very old > version of the python-sybase package (0.36). > > -- > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > ? ? when i wake up with a heart rate below 40, i head right for the espresso > ? ? machine. -- chaos @ forums.usms.org That is really strange :-( I'll ask in the python-sybase group Thanks From nobody at nowhere.com Thu Jul 2 10:23:51 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 02 Jul 2009 15:23:51 +0100 Subject: PEP368 and pixeliterators References: Message-ID: On Thu, 02 Jul 2009 10:32:04 +0200, Joachim Str?mbergson wrote: > I just read the PEP368 and really liked the proposed idea, sound like a > great battery addition to include in the std lib: > > http://www.python.org/dev/peps/pep-0368/ Unfortunately, it's too simplistic, meaning that most of the existing image formats used by the various GUI toolkits (and OpenGL) will need to be retained. Obvious omissions include support for arbitrary numbers of bits per component (e.g. R5G5B5A1), component, pixel and row strides (padding), mixing packed and planar components (e.g. packed RGB with a separate alpha plane), and storage orientation (top-to-bottom or bottom-to-top). Ideally, an Image should be able to use any sane storage format, so that you don't have to keep converting the Image to an application-specific format. NumPy's arrays would probably be a good model here. Essentially, each component would have a base pointer plus horizontal and vertical strides measured in bits. The strides wouldn't necessarily be the same for all components (allowing for an R8G8B8 plane with a 3-byte stride plus a distinct A8 plane with a 1-byte stride), vertical stride wouldn't necessarily be greater than horizontal stride (allowing transposition), strides could be negative (allowing mirroring), and could be less than a byte (allowing e.g. a 1-bpp alpha channel, planar 4-bpp data, 12-bpp greyscale, etc). AFAICT, this would allow all common image storage formats to be represented. The most common exception is 8-bit RGB data using a 6x6x6 colour cube, but that's not common enough to be worth the added complexity. The interface should also be flexible enough to allow an Image to be a "proxy" for a non-local image (e.g. one held in video RAM or in the X server). You wouldn't be able to get a buffer for the image data, but you should at least be able to obtain the dimensions and format of such images, and ideally copy the data (or rectangular subregions) to/from a local image. From hv at tbz-pariv.de Thu Jul 2 10:50:34 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Thu, 02 Jul 2009 16:50:34 +0200 Subject: logging of strings with broken encoding Message-ID: <7b3vpqF20nct6U1@mid.individual.net> Hi, I have bug in my code, which results in the same error has this one: https://bugs.launchpad.net/bzr/+bug/295653 {{{ Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit self.stream.write(fs % msg.encode("UTF-8")) .. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128) }}} I run Python 2.6. In SVN the code is the same (StreamHandler ... def emit...): http://svn.python.org/view/python/branches/release26-maint/Lib/logging/__init__.py?revision=72507&view=markup I think msg.encode("UTF-8", 'backslashreplace') would be better here. What do you think? Should I fill a bugreport? Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From vertespain at gmail.com Thu Jul 2 11:09:43 2009 From: vertespain at gmail.com (masher) Date: Thu, 2 Jul 2009 08:09:43 -0700 (PDT) Subject: multiprocessing: pool with blocking queue Message-ID: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> Hi, I am trying to implement a multiprocessing pool that assigns tasks from a blocking queue. My situation is a pretty classic producer/ consumer conundrum, where the producer can produce much faster than the consumers can consume. The wrinkle in the story is that the producer produces objects that consume large amounts of memory, so I would like the queue to block when it reaches, say, twice the number of tasks as there are processes in the pool, so that I don't devour my RAM. I have been able to implement this one way, but I am somewhat displeased with the result. Code speaks louder than words, so here is a brief example of the technique I am using, followed by an explanation of why I am unhappy with it: === #!/usr/bin/env python3 import time import random from multiprocessing import Pool, Queue, M def procfunc(queue): time.sleep(random.random() * 2) return queue.get()*2 def printrange(n): for i in range(n): print("generated " + str(i)) yield i if __name__ == "__main__": sm = Manager() pool = Pool() queuelen = len(pool._pool) * 2 queue = sm.Queue(queuelen) for i in printrange(100): queue.put(i) pool.apply_async(procfunc, (queue,), callback=print) pool.close() pool.join() === The reason I am unhappy with this trick is that if you examine the source code of pool.py, you will note that the class Pool already uses an internal queue, "_taskqueue" from which the tasks are assigned to processes in the pool. Particularly: def __init__(self, processes=None, initializer=None, initargs=()): self._setup_queues() self._taskqueue = queue.Queue() ...snip It seems to me that if I could only subclass and do queuelen = len(pool._pool) * 2 self._taskqueue = queue.Queue(queuelen) later in the constructor, once the pool length has been established, I would have a much more elegant, transparent solution to the problem. Unfortunately, the design of the Pool class is such that actually implementing this solution would be very hackish and inelegant. If only, say, _setup_queues() were called after the _taskqueue assignment, then I could override it. My questions, then, is: Is there a more elegant/pythonic way of doing what I am trying to do with the current Pool class? If the verdict is no, I'll be happy to file a bug report. From dns4 at cornell.edu Thu Jul 2 11:09:58 2009 From: dns4 at cornell.edu (David Smith) Date: Thu, 02 Jul 2009 11:09:58 -0400 Subject: logging of strings with broken encoding In-Reply-To: <7b3vpqF20nct6U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > Hi, > > I have bug in my code, which results in the same error has this one: > > https://bugs.launchpad.net/bzr/+bug/295653 > {{{ > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit > self.stream.write(fs % msg.encode("UTF-8")) > .. > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128) > }}} > > I run Python 2.6. In SVN the code is the same (StreamHandler ... def emit...): > http://svn.python.org/view/python/branches/release26-maint/Lib/logging/__init__.py?revision=72507&view=markup > > I think msg.encode("UTF-8", 'backslashreplace') would be better here. > > What do you think? > > Should I fill a bugreport? > > Thomas > > I think you have to decode it first using the strings original encoding whether that be cp1252 or mac-roman or any of the other 8-bit encodings. Once that's done, you can encode in UTF-8 --David From __peter__ at web.de Thu Jul 2 11:16:29 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 17:16:29 +0200 Subject: logging of strings with broken encoding References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > I have bug in my code, which results in the same error has this one: > > https://bugs.launchpad.net/bzr/+bug/295653 > {{{ > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit > self.stream.write(fs % msg.encode("UTF-8")) > .. > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: > ordinal not in range(128) }}} > > I run Python 2.6. In SVN the code is the same (StreamHandler ... def > emit...): > http://svn.python.org/view/python/branches/release26- maint/Lib/logging/__init__.py?revision=72507&view=markup > > I think msg.encode("UTF-8", 'backslashreplace') would be better here. > > What do you think? That won't help. It's a *decoding* error. You are feeding it a non-ascii byte string. Peter From wells at submute.net Thu Jul 2 11:32:40 2009 From: wells at submute.net (Wells Oliver) Date: Thu, 2 Jul 2009 10:32:40 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor Message-ID: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> Is there some kind of mysterious logic to how the the columns are ordered when executing the following: sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' GROUP BY player_id" % (start, date) cursor.execute(sql) for row in cursor.fetchall(): print row.keys() What I get is: ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] Neither alphabetical nor the order in which they were specified in the query nor... any seeming order I can suss out. Any ideas? Thanks! (cursor being a MySQLdb.cursors.DictCursor object.) -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Thu Jul 2 11:48:18 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 02 Jul 2009 10:48:18 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> Message-ID: <4A4CD6C2.2030103@tim.thechases.com> > sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) > AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN > Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' > GROUP BY player_id" % (start, date) > cursor.execute(sql) > > for row in cursor.fetchall(): > print row.keys() > > What I get is: > > ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] > > Neither alphabetical nor the order in which they were specified in the query > nor... any seeming order I can suss out. Any ideas? Thanks! > > (cursor being a MySQLdb.cursors.DictCursor object.) My guess is you're experiencing the fact that dicts are unordered by nature which allows it to return in any order it likes (usually per the internal representation/storage). -tkc From lie.1296 at gmail.com Thu Jul 2 11:49:46 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 15:49:46 GMT Subject: logging of strings with broken encoding In-Reply-To: <7b3vpqF20nct6U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > Hi, > > I have bug in my code, which results in the same error has this one: > > https://bugs.launchpad.net/bzr/+bug/295653 > {{{ > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit > self.stream.write(fs % msg.encode("UTF-8")) > .. > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128) > }}} What's the encoding of self.stream? Is it sys.stdout/sys.stderr or a file object? From hv at tbz-pariv.de Thu Jul 2 11:53:14 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Thu, 02 Jul 2009 17:53:14 +0200 Subject: logging of strings with broken encoding In-Reply-To: <7b3vpqF20nct6U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: <7b43faF21l89hU1@mid.individual.net> My quick fix is this: class MyFormatter(logging.Formatter): def format(self, record): msg=logging.Formatter.format(self, record) if isinstance(msg, str): msg=msg.decode('utf8', 'replace') return msg But I still think handling of non-ascii byte strings should be better. A broken logging message is better than none. And, if there is a UnicodeError, handleError() should not send the message to sys.stderr, but it should use emit() of the current handler. In my case sys.stderr gets discarded. Its very hard to debug, if you don't see any logging messages. Thomas Thomas Guettler schrieb: > Hi, > > I have bug in my code, which results in the same error has this one: > ... -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From wells at submute.net Thu Jul 2 11:57:05 2009 From: wells at submute.net (Wells Oliver) Date: Thu, 2 Jul 2009 10:57:05 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <4A4CD6C2.2030103@tim.thechases.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> <4A4CD6C2.2030103@tim.thechases.com> Message-ID: <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> Will this order at least be the same for that same query every time the script is executed? On Thu, Jul 2, 2009 at 10:48 AM, Tim Chase wrote: > sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) >> AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN >> Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' >> GROUP BY player_id" % (start, date) >> cursor.execute(sql) >> >> for row in cursor.fetchall(): >> print row.keys() >> >> What I get is: >> >> ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] >> >> Neither alphabetical nor the order in which they were specified in the >> query >> nor... any seeming order I can suss out. Any ideas? Thanks! >> >> (cursor being a MySQLdb.cursors.DictCursor object.) >> > > My guess is you're experiencing the fact that dicts are unordered by nature > which allows it to return in any order it likes (usually per the internal > representation/storage). > > -tkc > > > > -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From james at agentultra.com Thu Jul 2 12:06:58 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 02 Jul 2009 12:06:58 -0400 Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> Message-ID: <851vozf4rh.fsf@agentultra.com> masher writes: > My questions, then, is: Is there a more elegant/pythonic way of doing > what I am trying to do with the current Pool class? Forgive me, I may not fully understand what you are trying to do here (I've never really used multiprocessing all that much)... But couldn't you just assign your own Queue object to the Pool instance? From robert.kern at gmail.com Thu Jul 2 12:23:40 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 02 Jul 2009 11:23:40 -0500 Subject: deleting certain entries in numpy array In-Reply-To: <4a4c7fd9$1@news.fhg.de> References: <4a4b7763$1@news.fhg.de> <4a4c7fd9$1@news.fhg.de> Message-ID: On 2009-07-02 04:40, Sebastian Schabe wrote: > Robert Kern schrieb: > > You will want to ask numpy questions on the numpy mailing list. > > > > http://www.scipy.org/Mailing_Lists > > > > I ever thought news-groups are the right way for questions like this. > And the mailing list page just confuses me, but I'm trying to get used > to it. You may want to try GMane, then: http://dir.gmane.org/gmane.comp.python.numeric.general It's how I read this list, for instance. -- 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 vertespain at gmail.com Thu Jul 2 12:25:54 2009 From: vertespain at gmail.com (masher) Date: Thu, 2 Jul 2009 09:25:54 -0700 (PDT) Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> <851vozf4rh.fsf@agentultra.com> Message-ID: On Jul 2, 12:06?pm, J Kenneth King wrote: > masher writes: > > My questions, then, is: Is there a more elegant/pythonic way of doing > > what I am trying to do with the current Pool class? > > Forgive me, I may not fully understand what you are trying to do here > (I've never really used multiprocessing all that much)... > > But couldn't you just assign your own Queue object to the Pool instance? That's basically my question. It does not appear as though there is any straightforward way of doing this because of the design of Pool's __init__ method, which passes _taskqueue to several functions. Hence, even if I were to reassign _taskqueue after __init__, that wouldn't change anything. From ethan at stoneleaf.us Thu Jul 2 12:38:56 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 02 Jul 2009 09:38:56 -0700 Subject: regex question on .findall and \b Message-ID: <4A4CE2A0.5040407@stoneleaf.us> Greetings! My closest to successfull attempt: Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.9.1 -- An enhanced Interactive Python. In [161]: re.findall('\d+','this is test a3 attempt 79') Out[161]: ['3', '79'] What I really want in just the 79, as a3 is not a decimal number, but when I add the \b word boundaries I get: In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') Out[162]: [] What am I missing? ~Ethan~ From tim.wintle at teamrubber.com Thu Jul 2 12:49:48 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Thu, 02 Jul 2009 17:49:48 +0100 Subject: Dict ordering [was: Re: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor] In-Reply-To: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> Message-ID: <1246553388.18497.5.camel@tim-laptop> On Thu, 2009-07-02 at 10:32 -0500, Wells Oliver wrote: > for row in cursor.fetchall(): > print row.keys() > > What I get is: > > ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] > > Neither alphabetical nor the order in which they were specified in the > query nor... any seeming order I can suss out. Any ideas? Thanks! keys in a dict are not guaranteed to be in any specific order. It's not specific to the MySQLdb cursor. IIRC it's related to the hashes of the keys (but obviously not in strictly increasing order). You definitely shouldn't rely on the ordering (hence the need for an OrderedDict. e.g. >>> a = ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] >>> for s in a: ... print hash(s) ... -3369365635700083487 8448050754076166 9216055368083080 9344056137084361 9600028874 9216027721 1844482854915224472 8832053061079775 From python.list at tim.thechases.com Thu Jul 2 12:50:09 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 02 Jul 2009 11:50:09 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> <4A4CD6C2.2030103@tim.thechases.com> <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> Message-ID: <4A4CE541.9030308@tim.thechases.com> > Will this order at least be the same for that same query every time the > script is executed? I wouldn't count on it. The order is only defined for the one iteration (result of the keys() call). If the order matters, I'd suggest a double-dispatch with a non-dict (regular/default) query result, something like header_map = dict( (desc[0], i) for i,desc in enumerate(cursor.description) ) for row in cursor.fetchall(): print row[header_map['league']] If you have a lot of fields, I often use a closure to simplify the coding: for row in cursor.fetchall(): item = lambda s: row[headermap[s]] print item('league') print item('BB') print item('player_id') That way, the row stays in order, if you need it that way: league, bb, hr, ip, k, h, id, er = row -tkc From stefan_ml at behnel.de Thu Jul 2 13:01:37 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 02 Jul 2009 19:01:37 +0200 Subject: logging of strings with broken encoding In-Reply-To: <7b43faF21l89hU1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> Message-ID: <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> Thomas Guettler wrote: > My quick fix is this: > > class MyFormatter(logging.Formatter): > def format(self, record): > msg=logging.Formatter.format(self, record) > if isinstance(msg, str): > msg=msg.decode('utf8', 'replace') > return msg > > But I still think handling of non-ascii byte strings should be better. > A broken logging message is better than none. Erm, may I note that this is not a problem in the logging library but in the code that uses it? How should the logging library know what you meant by passing that byte string in the first place? And where is the difference between accidentally passing a byte string and accidentally passing another non-printable object? Handling this "better" may simply hide the bugs in your code, I don't find that's any "better" at all. Anyway, this has been fixed in Py3. Stefan From ethan at stoneleaf.us Thu Jul 2 13:12:32 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 02 Jul 2009 10:12:32 -0700 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A4CEA80.4050803@stoneleaf.us> Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit > (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? > > ~Ethan~ ARGH!! Okay, I need two \\ so I'm not trying to match a backspace. I knew (okay, hoped ;) I would figure it out once I posted the question and moved on. *sheepish grin* From lie.1296 at gmail.com Thu Jul 2 13:14:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 17:14:34 GMT Subject: logging of strings with broken encoding In-Reply-To: <7b43faF21l89hU1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> Message-ID: <_V53m.2227$ze1.167@news-server.bigpond.net.au> Thomas Guettler wrote: > My quick fix is this: > > class MyFormatter(logging.Formatter): > def format(self, record): > msg=logging.Formatter.format(self, record) > if isinstance(msg, str): > msg=msg.decode('utf8', 'replace') > return msg > > But I still think handling of non-ascii byte strings should be better. > A broken logging message is better than none. > The problem is, python 2.x assumed the default encoding of `ascii` whenever you don't explicitly mention the encoding, and your code apparently broke with that assumption. I haven't looked at your code, but others have suggested that you've fed the logging module with non-ascii byte strings. The logging module can only work with 1) unicode string, 2) ascii-encoded byte string If you want a quick fix, you may be able to get away with repr()-ing your log texts. A proper fix, however, is to pass a unicode string to the logging module instead. >>> logging.warn('?') # or logging.warn('\xd1\x8b') Traceback (most recent call last): File "/usr/lib64/python2.6/logging/__init__.py", line 773, in emit stream.write(fs % msg.encode("UTF-8")) UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 13: ordinal not in range(128) >>> logging.warn(repr('?')) WARNING:root:'\xd1\x8b' >>> logging.warn(u'?') WARNING:root:? From python.list at tim.thechases.com Thu Jul 2 13:17:22 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 02 Jul 2009 12:17:22 -0500 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A4CEBA2.9040303@tim.thechases.com> Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? The sneaky detail that the regexp should be in a raw string (always a good practice), not a cooked string: r'\b\d+\b' The "\d" isn't a valid character-expansion, so python leaves it alone. However, I believe the "\b" is a control character, so your actual string ends up something like: >>> print repr('\b\d+\b') '\x08\\d+\x08' >>> print repr(r'\b\d+\b') '\\b\\d+\\b' the first of which doesn't match your target string, as you might imagine. -tkc From lie.1296 at gmail.com Thu Jul 2 13:18:00 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 17:18:00 GMT Subject: invoking a method from two superclasses In-Reply-To: References: <31fe4bef-6738-4de5-b9bb-c0dc42c695a7@o6g2000yqj.googlegroups.com> Message-ID: Carl Banks wrote: > On Jun 30, 6:23 pm, Carl Banks wrote: >> On Jun 30, 5:34 pm, Mitchell L Model wrote: >> >>> Allow me to add to my previous question that certainly the superclass >>> methods can be called explicitly without resorting to super(), e.g.: >>> class C(A, B): >>> def __init__(self): >>> A.__init__(self) >>> B.__init__(self) >>> My question is really whether there is any way of getting around the >>> explicit class names by using super() >> Yes there is: just make sure that all subclasses also call super. > > And by subclasses I mean base classes, of course. ugh > > > Carl Banks super() is short for two things: 1) superharmful 2) superuseful From sjoerd at acm.org Thu Jul 2 13:18:07 2009 From: sjoerd at acm.org (Sjoerd Mullender) Date: Thu, 02 Jul 2009 19:18:07 +0200 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A4CEBCF.9010203@acm.org> On 2009-07-02 18:38, Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit > (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? > > ~Ethan~ Try this: >>> re.findall(r'\b\d+\b','this is test a3 attempt 79') ['79'] The \b is a backspace, by using raw strings you get an actual backslash and b. -- Sjoerd Mullender From philip at semanchuk.com Thu Jul 2 13:22:53 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 2 Jul 2009 13:22:53 -0400 Subject: dealloc function in python extend c module In-Reply-To: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> References: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> Message-ID: <8599A253-D2C9-4EE4-AF4F-AEF658BA2033@semanchuk.com> On Jul 2, 2009, at 2:11 AM, Shen, Yu-Teh wrote: > I create my extend type something like http://www.python.org/doc/current/extending/newtypes.html > . > And my type has a member which is a pointer point to my allocate > memory ( no ref count). > ex: > --------------------------------------------------- > typedef struct { > PyObject_HEAD > /* Type-specific fields go here. */ > mytype *p; > } noddy_NoddyObject; > > And i write dealloc function like this: > --------------------------------------------------- > static void > Noddy_dealloc(Noddy* self) > { > delete p; > self->ob_type->tp_free((PyObject*)self); > } > > And I found it's strange that it didn't call dealloc when there is no > reference. > > ex: > a = Noddy() > b = a > del a > del b > # i think there is no ref to the object, and it should call dealloc > but it didn't call!!! > > could anyone tell me what happened? Hi Shen, I'm no expert on Python memory management, but since no once else has answered your question I'll tell you what I *think* is happening. Python doesn't delete objects as soon as they're dereferenced. It merely marks them as safe for garbage collection (GC). If GC never happens (and it might not in a small test program), your dealloc function won't run. Now some more knowledgeable person will probably correct me. =) You can control the garbage collector manually with Python's gc module. You will probably never want to control the garbage collector manually in ordinary programs, but it can be useful for testing. Hope this helps Philip From sajmikins at gmail.com Thu Jul 2 13:23:55 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 10:23:55 -0700 (PDT) Subject: question of style Message-ID: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Hey I was hoping to get your opinions on a sort of minor stylistic point. These two snippets of code are functionally identical. Which would you use and why? The first one is easier [for me anyway] to read and understand, but slightly less efficient, while the second is [marginally] harder to follow but more efficient. ## First snippet if self.higher is self.lower is None: return if self.lower is None: return self.higher if self.higher is None: return self.lower ## Second snippet if self.higher is None: if self.lower is None: return return self.lower if self.lower is None: return self.higher What do you think? (One minor point: in the first snippet, the "is None" in the first line is superfluous in the context in which it will be used, the only time "self.lower is self.higher" will be true is when they are both None.) From chambon.pascal at gmail.com Thu Jul 2 13:34:00 2009 From: chambon.pascal at gmail.com (Pascal Chambon) Date: Thu, 02 Jul 2009 19:34:00 +0200 Subject: Direct interaction with subprocess - the curse of blocking I/O In-Reply-To: References: Message-ID: <4A4CEF88.80201@gmail.com> Thank you all for the comments >you might want something like Expect. Yes "Expect" deals with such things, unfortunately it's posix only (due to the PTY module requirement...); whereas I'd like to find generic ways (i.e at least windows/linux/mac recipes) > The latter is inherently tricky (which is why C's popen() lets you connect > to stdin or stdout but not both). You have to use either multiple threads, > select/poll, or non-blocking I/O. > > If the child's output is to the console, it should presumably be the > former, i.e. piping stdin but allowing the child to inherit stdout, in > which case, where's the problem? Or are you piping its stdout via Python > for the hell of it It's well a too-way piping that I'd like to get ; but actually, even a single-way piping is error-prone : if I try to send data to the child's stdin, and this child never wants to receive that data (for whatever reason), the parent thread will be forever stuck. I can use multiple threads, but it doesn't fully solve the problem, because having even a single thread stuck might prevent the process from terminating, on some operating systems... I'd need a way to deblock I/O blocking threads (but even closing the pipe isn't sure to do it properly - the closing might be delayed by the I/O operations wating). >I would guess the architectural differences are so great that an attempt >to "do something simple" is going to involve architecture-specific code. >and I personally wouldn't have it any other way. Simulating a shell >with hooks on its I/O should be so complicated that a "script kiddie" >has trouble writing a Trojan. I'm not sure I understand the security hole there - if a script kiddie manages to make you execute his program, he doesn't need complex I/O redirections to harm you, simply destroying files randomly on your hard drive should fit his needs shouldn't it. :? > > I met the issue : select() works only on windows ... > >No it doesn't. It works only on sockets on Windows, on Unix/Linux it works >with all file descriptors . Indeed, I mixed up my words there... >_< >If you are willing to have a wxPython dependency, wx.Execute handles non-blocking i/o with processes on all supported platforms more discussion of python's blocking i/o issues here: > http://groups.google.com/group/comp.lang.python/browse_thread/thread/a037349e18f99630/60886b8beb55cfbc?q=#60886b8beb55cfbc Very interesting link, that's exactly the problem... and it seems no obvious solution comes out of it, except, of course, going down to the OS' internals... File support is really weak in python I feel (stdio is a little outdated...), it'd need a complete blocking/non-blocking, locking/non-locking stream API straight in the stdlib... I'm already working on locking classes, I'll explore the opportunity for a higher level "file" object too whenever possible (a new filesystem api is in progress here at europython2009, it might be the occasion). regards, Pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Thu Jul 2 13:37:01 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 17:37:01 GMT Subject: PEP 376 In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> Message-ID: <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Joachim Str?mbergson wrote: > Aloha! > > Tarek Ziad? wrote: >> The prefix is a good idea but since it's just a checksum to control >> that the file hasn't changed >> what's wrong with using a weak hash algorithm like md5 or now sha1 ? > > Because it creates a dependency to an old algorithm that should be > deprecated. Also using MD5, even for a thing like this might make people > belive that it is an ok algorithm to use - "Hey, it is used by the > default install in Python, so it must be ok, right?" > > If we flip the argument around: Why would you want to use MD5 instead of > SHA-256? For the specific use case the performance will not (should not) > be an issue. > > As I wrote a few mails ago, it is time to move forward from MD5 and > designing something in 2009 that will be around for many years that uses > MD5 is (IMHO) a bad design decision. > >> If someone wants to modify a file of a distribution he can recreate >> the checksum as well, >> the only secured way to prevent that would be to use gpg keys but >> isn't that overkill for what we need ? > > Actually, adding this type of security would IMHO be a good idea. > Now, are we actually talking about security or checksum? It has been known for years that MD5 is weak, weak, weak. Not just in the recent years. But it doesn't matter since MD5 was never designed for security, MD5 was designed to protect against random bits corruption. If you want security, look at least to GPG. For data protection against intentional, malicious forging, definitely MD5 is the wrong choice. But when you just want a simple checksum to ensure that a faulty router somewhere in the internet backbone doesn't destroying your data, MD5 is a fine algorithm. From duncan.booth at invalid.invalid Thu Jul 2 13:44:34 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 2 Jul 2009 17:44:34 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) > I'd write the first one as: if self.lower is None: return self.higher if self.higher is None: return self.lower because the entire first 'if' statement is redundant. As a matter of style however I wouldn't use the shorthand to run two 'is' comparisons together, I'd write that out in full if it was actually needed here. Likewise in the second one: if self.lower is None: return return self.lower is obviously the same as: return self.lower so apart from reversing the order of the comparisons once you've dropped the redundant test it is the same as the first one. From petr.messner at gmail.com Thu Jul 2 13:48:28 2009 From: petr.messner at gmail.com (Petr Messner) Date: Thu, 2 Jul 2009 19:48:28 +0200 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <4A4CE541.9030308@tim.thechases.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> <4A4CD6C2.2030103@tim.thechases.com> <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> <4A4CE541.9030308@tim.thechases.com> Message-ID: <67c97cd90907021048u650ae97dhb6da2f34fabd971b@mail.gmail.com> 2009/7/2 Tim Chase : >> Will this order at least be the same for that same query every time the >> script is executed? > > I wouldn't count on it. The order is only defined for the one iteration > (result of the keys() call). If the order matters, I'd suggest a > double-dispatch with a non-dict (regular/default) query result, something ... Dictionaries are usually designed to have the best performance when accessing certain key, so it is not expected that dictionary items will be in any particular order; this applies not only for Python. But (for Python dict) you can be sure that results of items(), keys(), values(), iteritems(), iterkeys(), and itervalues() will correspond if called with no intervening modifications to the dictionary. If such a functionality is needed, there is a collections.OrderedDict that was introduced in Python 3.1; there is also an implementation for Python 2.4 or later: http://code.activestate.com/recipes/576693/ (this links is from "What?s New In Python 3.1") PM From cbc at unc.edu Thu Jul 2 13:52:48 2009 From: cbc at unc.edu (Chris Calloway) Date: Thu, 02 Jul 2009 13:52:48 -0400 Subject: Deadline for Toronto PyCamp Registraiton Appoaching Message-ID: <4A4CF3F0.4020304@unc.edu> Tomorrow (July 3) by midnight will be the last opportunity for Toronto PyCamp registration before the late registration period ending July 10. PyCamp is the original Python BootCamp developed by a user group for user groups. This year PyCamp is July 13-17 at the University of Toronto, sponsored by the Department of Physics and Scryent. For beginners, PyCamp makes you productive so you can get your work done quickly. PyCamp emphasizes the features which make Python a simpler and more efficient language. Following along by example speeds your learning process in a modern high-tech classroom. Become a self-sufficient Python developer in just five days at PyCamp! http://pycamp.org -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From cbc at unc.edu Thu Jul 2 13:52:48 2009 From: cbc at unc.edu (Chris Calloway) Date: Thu, 02 Jul 2009 13:52:48 -0400 Subject: Deadline for Toronto PyCamp Registraiton Appoaching Message-ID: <4A4CF3F0.4020304@unc.edu> Tomorrow (July 3) by midnight will be the last opportunity for Toronto PyCamp registration before the late registration period ending July 10. PyCamp is the original Python BootCamp developed by a user group for user groups. This year PyCamp is July 13-17 at the University of Toronto, sponsored by the Department of Physics and Scryent. For beginners, PyCamp makes you productive so you can get your work done quickly. PyCamp emphasizes the features which make Python a simpler and more efficient language. Following along by example speeds your learning process in a modern high-tech classroom. Become a self-sufficient Python developer in just five days at PyCamp! http://pycamp.org -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/ . From hobesh at gmail.com Thu Jul 2 13:56:10 2009 From: hobesh at gmail.com (Zach Hobesh) Date: Thu, 2 Jul 2009 10:56:10 -0700 Subject: Config files with different types Message-ID: Hi all, I've written a function that reads a specifically formatted text file and spits out a dictionary. ?Here's an example: config.txt: Destination = C:/Destination Overwrite = True Here's my function that takes 1 argument (text file) the_file = open(textfile,'r') linelist = the_file.read().split('\n') the_file.close() configs = {} for line in linelist: try: key,value = line.split('=') key.strip() value.strip() key.lower() value.lower() configs[key] = value except ValueError: break so I call this on my config file, and then I can refer back to any config in my script like this: shutil.move(your_file,configs['destination']) which I like because it's very clear and readable. So this works great for simple text config files. Here's how I want to improve it: I want to be able to look at the value and determine what type it SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when it might be more useful as a boolean. Is there a quick way to do this? I'd also like to able to read '1' as an in, '1.0' as a float, etc... I remember once I saw a script that took a string and tried int(), float() wrapped in a try except, but I was wondering if there was a more direct way. Thanks in advance, Zach From db3l.net at gmail.com Thu Jul 2 13:58:24 2009 From: db3l.net at gmail.com (David Bolen) Date: Thu, 02 Jul 2009 13:58:24 -0400 Subject: Suppressing Implicit Chained Exceptions (Python 3.0) References: Message-ID: "andrew cooke" writes: > However, when printed via format_exc(), this new exception still has the > old exception attached via the mechanism described at > http://www.python.org/dev/peps/pep-3134/ (this is Python 3.0). If you're in control of the format_exc() call, I think the new chain keyword parameter can disable this and restore the old behavior. If you're not in control of the traceback display, I'm not sure there's an easy way to prevent it, given that displaying chained exceptions is the default mode. -- David From sajmikins at gmail.com Thu Jul 2 13:58:56 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 10:58:56 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On Jul 2, 1:44?pm, Duncan Booth wrote: > Simon Forman wrote: > > Hey I was hoping to get your opinions on a sort of minor stylistic > > point. > > These two snippets of code are functionally identical. Which would you > > use and why? > > The first one is easier [for me anyway] to read and understand, but > > slightly less efficient, while the second is [marginally] harder to > > follow but more efficient. > > > ## First snippet > > > if self.higher is self.lower is None: return > > if self.lower is None: return self.higher > > if self.higher is None: return self.lower > > > ## Second snippet > > > if self.higher is None: > > ? ? if self.lower is None: > > ? ? ? ? return > > ? ? return self.lower > > if self.lower is None: > > ? ? return self.higher > > > What do you think? > > > (One minor point: in the first snippet, the "is None" in the first > > line is superfluous in the context in which it will be used, the only > > time "self.lower is self.higher" will be true is when they are both > > None.) > > I'd write the first one as: > > ? ? ? ? if self.lower is None: > ? ? ? ? ? ? ? ? return self.higher > ? ? ? ? if self.higher is None: > ? ? ? ? ? ? return self.lower > > because the entire first 'if' statement is redundant. > > As a matter of style however I wouldn't use the shorthand to run two 'is' > comparisons together, I'd write that out in full if it was actually needed > here. > > Likewise in the second one: > > ? ? if self.lower is None: > ? ? ? ? return > ? ? return self.lower > > is obviously the same as: > > ? ? ? ? return self.lower > > so apart from reversing the order of the comparisons once you've dropped > the redundant test it is the same as the first one. Wow. The equivalence in the second bit is obvious enough-- in hindsight :] but I totally missed the redundancy in the first bit. I must be getting old. Thank you very much. From charles at declareSub.com Thu Jul 2 14:00:51 2009 From: charles at declareSub.com (Charles Yeomans) Date: Thu, 2 Jul 2009 14:00:51 -0400 Subject: PEP 376 In-Reply-To: <1f63m.2229$ze1.1410@news-server.bigpond.net.au> References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Message-ID: On Jul 2, 2009, at 1:37 PM, Lie Ryan wrote: > Joachim Str?mbergson wrote: >> Aloha! >> >> Tarek Ziad? wrote: >>> The prefix is a good idea but since it's just a checksum to control >>> that the file hasn't changed >>> what's wrong with using a weak hash algorithm like md5 or now sha1 ? >> >> Because it creates a dependency to an old algorithm that should be >> deprecated. Also using MD5, even for a thing like this might make >> people >> belive that it is an ok algorithm to use - "Hey, it is used by the >> default install in Python, so it must be ok, right?" >> >> If we flip the argument around: Why would you want to use MD5 >> instead of >> SHA-256? For the specific use case the performance will not (should >> not) >> be an issue. >> >> As I wrote a few mails ago, it is time to move forward from MD5 and >> designing something in 2009 that will be around for many years that >> uses >> MD5 is (IMHO) a bad design decision. >> >>> If someone wants to modify a file of a distribution he can recreate >>> the checksum as well, >>> the only secured way to prevent that would be to use gpg keys but >>> isn't that overkill for what we need ? >> >> Actually, adding this type of security would IMHO be a good idea. >> > > Now, are we actually talking about security or checksum? > > It has been known for years that MD5 is weak, weak, weak. Not just in > the recent years. But it doesn't matter since MD5 was never designed > for > security, MD5 was designed to protect against random bits > corruption. If > you want security, look at least to GPG. For data protection against > intentional, malicious forging, definitely MD5 is the wrong choice. > But > when you just want a simple checksum to ensure that a faulty router > somewhere in the internet backbone doesn't destroying your data, MD5 > is > a fine algorithm. > -- On the contrary, MD5 was intended to be a cryptographic hash function, not a checksum. Charles Yeomans From python at mrabarnett.plus.com Thu Jul 2 14:12:07 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 19:12:07 +0100 Subject: Config files with different types In-Reply-To: References: Message-ID: <4A4CF877.6030906@mrabarnett.plus.com> Zach Hobesh wrote: > Hi all, > > I've written a function that reads a specifically formatted text file > and spits out a dictionary. Here's an example: > > config.txt: > > Destination = C:/Destination > Overwrite = True > > > Here's my function that takes 1 argument (text file) > > the_file = open(textfile,'r') > linelist = the_file.read().split('\n') You could use .splitlines() or iterate over the file itself. > the_file.close() > configs = {} > for line in linelist: > try: > key,value = line.split('=') > key.strip() > value.strip() > key.lower() > value.lower() Strings are immutable. These methods don't modify the strings, but _return_ the result. > configs[key] = value > > except ValueError: > break > 'break' will leave the loop. Is this intentional? > so I call this on my config file, and then I can refer back to any > config in my script like this: > > shutil.move(your_file,configs['destination']) > > which I like because it's very clear and readable. > > So this works great for simple text config files. Here's how I want > to improve it: > > I want to be able to look at the value and determine what type it > SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when > it might be more useful as a boolean. Is there a quick way to do > this? I'd also like to able to read '1' as an in, '1.0' as a float, > etc... > > I remember once I saw a script that took a string and tried int(), > float() wrapped in a try except, but I was wondering if there was a > more direct way. > The way you saw was the safe, and recommended, way. When checking for Boolean you might want to ignore the case; something like: bool_dict = {"false": False, "true": True} ... try: value = bool_dict[value.strip().lower()] except ValueError: # Not a Boolean. ... From kee at kagi.com Thu Jul 2 14:12:29 2009 From: kee at kagi.com (Kee Nethery) Date: Thu, 2 Jul 2009 11:12:29 -0700 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: the fact that you felt compelled to explain the "one minor point" in the first snippet tells me that the second snippet does not need that explanation and will be easier for someone (like you for example) to maintain in the future. Second snippet would be my choice. Kee Nethery On Jul 2, 2009, at 10:23 AM, Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) > -- > http://mail.python.org/mailman/listinfo/python-list ------------------------------------------------- I check email roughly 2 to 3 times per business day. Kagi main office: +1 (510) 550-1336 From pavlovevidence at gmail.com Thu Jul 2 14:36:38 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 2 Jul 2009 11:36:38 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> On Jul 2, 3:12?am, Ulrich Eckhardt wrote: > Bearophile wrote: > > Ulrich Eckhardt: > >> a way to automatically release the resource, something > >> which I would do in the destructor in C++. > > > Is this helpful? > >http://effbot.org/pyref/with.htm > > Yes, it aims in the same direction. However, I'm not sure this applies to my > case. The point is that the resource handle is not just used locally in a > restricted scope but it is allocated and stored. The 'with' is something > that makes sense in the context of mutex locking, where you have a > well-defined critical section. What I need is something similar to open(), > which returs a file. When the last reference to that object goes out of > scope, the underlying file object is closed. On CPython you can do it with a __del__ attribute. Warning: objects with a __del__ attribute prevent reference cycle detection, which can potentially lead to memory (and resource) leaks. So you must be careful to avoid creating reference loops with that object. Note that file objects have a close method; you can explicitly close it at any time. Your object should follow that example, and define a close (or release, or whatever) method. I'd recommend making an effort to call it and to rely on __del__ as little as possible. Carl Banks From nobody at nowhere.com Thu Jul 2 14:41:54 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 02 Jul 2009 19:41:54 +0100 Subject: regex question on .findall and \b References: Message-ID: On Thu, 02 Jul 2009 09:38:56 -0700, Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? You need to use a raw string (r'...') to prevent \b from being interpreted as a backspace: re.findall(r'\b\d+\b','this is test a3 attempt 79') \d isn't a recognised escape sequence, so it doesn't get interpreted: > print '\b'  ^H > print '\d' \d > print r'\b' \b Try to get into the habit of using raw strings for regexps. From tjreedy at udel.edu Thu Jul 2 14:53:43 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jul 2009 14:53:43 -0400 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher What happen is neither is None? From james at agentultra.com Thu Jul 2 14:54:49 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 02 Jul 2009 14:54:49 -0400 Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> <851vozf4rh.fsf@agentultra.com> Message-ID: <85ws6qewzq.fsf@agentultra.com> masher writes: > On Jul 2, 12:06?pm, J Kenneth King wrote: >> masher writes: >> > My questions, then, is: Is there a more elegant/pythonic way of doing >> > what I am trying to do with the current Pool class? >> >> Forgive me, I may not fully understand what you are trying to do here >> (I've never really used multiprocessing all that much)... >> >> But couldn't you just assign your own Queue object to the Pool instance? > > That's basically my question. It does not appear as though there is > any straightforward way of doing this because of the design of Pool's > __init__ method, which passes _taskqueue to several functions. Hence, > even if I were to reassign _taskqueue after __init__, that wouldn't > change anything. I think I understand. There are ways to modify the class before instantiating it, but even the most clever or elegant solution will still smell funny. I suppose this might be worth submitting as a feature suggestion to the multiprocessing project. Best of luck. From rdrehoff at technotraining.net Thu Jul 2 14:57:40 2009 From: rdrehoff at technotraining.net (Rich Drehoff) Date: Thu, 2 Jul 2009 14:57:40 -0400 Subject: Intro to Python class, 7/21-23, Ft Worth TX Message-ID: <000901c9fb46$faa21ae0$efe650a0$@net> We are looking for someone that can help with the subject class, Intro to Python class, 7/21-23, Ft Worth TX. Please let me know if you can help. Would need your resume and best possible daily rate. Best regards, Rich Drehoff TechnoTraining, Inc. 328 Office Square Lane, Ste. 202, Virginia Beach, VA 23462-3648 (757) 425-0728 x15 / (757) 425-7323 Fax Email: RDrehoff at TechnoTraining.net Website: www.TechnoTraining.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew at acooke.org Thu Jul 2 15:07:20 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Jul 2009 15:07:20 -0400 (CLT) Subject: Suppressing Implicit Chained Exceptions (Python 3.0) Message-ID: <7915bc3d5213848b805f461940e3cbec.squirrel@localhost> David Bolen wrote: > "andrew cooke" writes: > >> However, when printed via format_exc(), this new exception still has the old exception attached via the mechanism described at >> http://www.python.org/dev/peps/pep-3134/ (this is Python 3.0). > > If you're in control of the format_exc() call, I think the new chain keyword parameter can disable this and restore the old behavior. > > If you're not in control of the traceback display, I'm not sure there's an easy way to prevent it, given that displaying chained exceptions is the default mode. Yeah, that's the impression I got too. Just in case it wasn't clear, I'd like the chaining not to exist at all in this case), so that it doesn't appear in format_exc outside my control. Thanks, Andrew From thudfoo at opensuse.us Thu Jul 2 15:10:41 2009 From: thudfoo at opensuse.us (member thudfoo) Date: Thu, 2 Jul 2009 12:10:41 -0700 Subject: Open Source RSS Reader in Python? In-Reply-To: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: <3d881a310907021210haabc998x9e4f678299716462@mail.gmail.com> On Wed, Jul 1, 2009 at 4:18 PM, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? > -- > http://mail.python.org/mailman/listinfo/python-list > rawdog is an RSS Aggregator Without Delusions Of Grandeur. It is a "river of news"-style aggregator: it uses feedparser to download feeds in RSS, Atom and a variety of other formats, and (by default) produces static HTML pages containing the newest articles in date order. For example, rawdog's default configuration produces output that looks like this: http://offog.org/code/rawdog.html From http Thu Jul 2 15:13:48 2009 From: http (Paul Rubin) Date: 02 Jul 2009 12:13:48 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <7x63eahp8z.fsf@ruckus.brouhaha.com> Simon Forman writes: > These two snippets of code are functionally identical. Which would you > use and why? Both are terrible. I can't tell what you're really trying to do. As Terry Reedy points out, the case where self.higher and self.lower are both not None is not handled. If you want to explicitly return None, use "return None" rather than "return". Generally, having a special value like None to denote a missing datum was considered standard practice a few decades ago, but these days in some circles it's treated as something of a code smell. You may want to look for some other representation instead. Most importantly, whatever code you write, put in an explanatory comment saying what the real goal is. From usernet at ilthio.net Thu Jul 2 15:16:25 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 19:16:25 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On 2009-07-02, Duncan Booth wrote: > so apart from reversing the order of the comparisons once you've dropped > the redundant test it is the same as the first one. I try to evaluate what you have given regardless of what Booth pointed out. So, I will only evaluate the first line as it contains most of what I want to illistrate. > Simon Forman wrote: >> if self.higher is self.lower is None: return I *really* don't like the fact that you don't specify a return value here when you have specified a return value for the other conditions. Obviously it is possible that something will be looking at the return value for this function (ie, it would not be a void function() in C). I had to lookup to make sure what Python would return in such a case. If you want the condition to return something then you should specify what you are returning. Specifing 'return None' is much clearer then relying on default behavior. I always prefer to see a: condition: operation structure to my code. It is the way it is almost always see in Python and in other languages. Putting it on one line doesn't save you anything and it makes it easier to miss something. I have no particular problem with running the is statements together as an extended if statement together when it makes it clearer to do so. The fact that you want all of them to be the same object is clear. I do have some wroblem in this instance because you are referencing "is" against an instance of a builtin object. The fact that you used is instead of == for this particular instance throws me a little because of the use of None. I wouldn't think second about it if it was a user defined object. I have to wonder whether all objects that are assigned the value of None are actually re-referenced to the origional None object? Could deep copy style operations create two equivilant but different copys of none? The same problems may be encounted from any builtin objects such as number literals. Had None not been specified I would be fine with stringing the comparison together: # if all three objects are the same instance, return None if object1 is object2 is object3: return None: Because of this pecularity, I think that it would be clearer to break the ifs for simularity and equality: if object1 is object2: if object1 == None: return None else: return object1 else: if object1 == None: return Object2 elif object2 == None:: return Object1 else: # what do we do if neither object == None? raise houstonWeHaveAProblemException From Scott.Daniels at Acm.Org Thu Jul 2 15:57:57 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 02 Jul 2009 12:57:57 -0700 Subject: question of style In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Duncan Booth wrote: > Simon Forman wrote: >> ... >> if self.higher is self.lower is None: return >> ... > As a matter of style however I wouldn't use the shorthand to run two 'is' > comparisons together, I'd write that out in full if it was actually needed > here. Speaking only to the style issue, when I've wanted to do something like that, I find: if self.higher is None is self.lower: ... more readable, by making clear they are both being compared to a constant, rather than compared to each other. More often, I've used code like: if is not None is not : ... since I am usually working on non-defaulting cases in the body. I find the form above simpler to read than: if is not None and is not None: ... I do draw the line at two, though, and with three or more I'll paren-up a list of parallel comparisons: if ( is not None and is not None and is not None): ... --Scott David Daniels Scott.Daniels at Acm.Org From http Thu Jul 2 16:02:58 2009 From: http (Paul Rubin) Date: 02 Jul 2009 13:02:58 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <7xws6qrgy5.fsf@ruckus.brouhaha.com> Simon Forman writes: > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? I'm not sure, but my guess is that what you are really trying to write is something like this: # self.higher and self.lower are each either "available" (i.e. are # not None), or unavailable (are None). Return the highest of the # available values. If no value is available, return None. if self.higher is not None: return self.higher elif self.lower is not None: return self.lower else: return None From sanket.s.patel at gmail.com Thu Jul 2 16:07:54 2009 From: sanket.s.patel at gmail.com (sanket) Date: Thu, 2 Jul 2009 13:07:54 -0700 (PDT) Subject: how to spawn a process under different user Message-ID: Hello All, I am trying to use python's subprocess module to launch a process. but in order to do that I have to change the user. I am not getting any clue how to do that? so can anyone please tell me How can I spawn a process under different user than currently I am logging in as. Thank you, sanket From max at alcyone.com Thu Jul 2 16:08:57 2009 From: max at alcyone.com (Erik Max Francis) Date: Thu, 02 Jul 2009 13:08:57 -0700 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? Explicit is always better, `return None` when that is your intent. `return` shouts to the reader, "I want to get out of this function now, and no one cares about the return result." Personally, I only use the one-liner if/else clauses if it's an extremely trivial condition, and even then, usually not there. (The only place I use it consistently is `if __name__ == '__main__': main()`.) If it's part of something more important that needs inspection -- as this obviously does given the questions you've had about it, doesn't skip space. Newlines are free. Even when expanding about the first test, there's no reason to do chained `if`s. One `if` with an `and` works just fine. Paul Rubin's looked to be the best overall replacement, as the logic here looks strangely turned inside out. You're obviously looking for which one _isn't_ `None`, so write the tests that way. It's much easier for everyone else (including your potential future self) to follow. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis They are ill discoverers that think there is no land when they can see nothing but sea. -- Francis Bacon From usernet at ilthio.net Thu Jul 2 16:27:33 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 20:27:33 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On 2009-07-02, Scott David Daniels wrote: > Duncan Booth wrote: >> Simon Forman wrote: >> As a matter of style however I wouldn't use the shorthand to run two 'is' >> comparisons together, I'd write that out in full if it was actually needed >> here. That part I don't really have a problem with. The chained is statements can actually be much clearer burying the tests into nested ifs. Nested ifs get the same result but they also give an immediate impression that the control structure might be looking for more then a single test result. I have to walk through the ifs to see all of the potential results one is looking for before I know what the entire control struture is doing. Multiple is operators makes it clear that I just want everything to be the same. The same would not be true if the operators where not all equilance operators ( is and == are clear as long as they are not mixed; but <, >=, != would be more confusing). > Speaking only to the style issue, when I've wanted to do something like > that, I find: > if self.higher is None is self.lower: > more readable, by making clear they are both being compared to a > constant, rather than compared to each other. By comparing them to *any* constant with 'is' you end up with the same potential problems as to whether something with a value of None is actually None? Is something with the value of 3 actually the same object as 3? What about if I make a deep copy? Can I have several objects with the value of three, all the same 3 object, or maybe even a combination of 1 separate 3 object with two other objects of the same 3. Is if self.higher == None == self.lower: any more clear then: if self.higher == self.lower == None: I don't personally have a problem with either. > More often, I've used code like: > if is not None is not : > since I am usually working on non-defaulting cases in the body. > I find the form above simpler to read than: > if is not None and is not None: This is a much more compicated expression. With equality operators everything is either the same or it isn't. I can start from the first variable and compare it to the second then to the third then ... In the case of the inequality, I have to start with the first and compare it to all of the rest to make sure that none are the same. Then I have to do the same for each with the remaining. This is much more difficult to do in my head. I requires much more thought, better left to a computer, when evaluating the expression for myself. Therefore, equalites are quick and easy to keep straight whereas any inequalites are more error prone when trying to evaluate how a section of code will react under different circumstances. > I do draw the line at two, though, and with three or more I'll > paren-up a list of parallel comparisons: As long as everything is an equality, then I don't mind comparing to the end of an 70 column line. Mixed types of equalities or inequalities require too many operations mentally evaluate safely. From bpytlik at sun.com Thu Jul 2 16:31:35 2009 From: bpytlik at sun.com (Brock Pytlik) Date: Thu, 02 Jul 2009 13:31:35 -0700 Subject: System default sys.path In-Reply-To: <81a8195ca5ecf44f3bf9ddbfaae37bdc@preisshare.net> References: <4A4C1FE4.1070901@sun.com> <81a8195ca5ecf44f3bf9ddbfaae37bdc@preisshare.net> Message-ID: <4A4D1927.5070305@sun.com> David Lyon wrote: > On Wed, 01 Jul 2009 19:48:04 -0700, Brock Pytlik wrote: > >> Hi, I'm trying to find a way to get the value sys.path would have on a >> particular system if python was started with an empty python path. I do >> want it to include the site specific additional paths. I know I can hack >> this information myself, >> > > Copy the code out from site.py... > > Well, as far as I can tell, site does several things. It calls abs__file__ which, afaict, doesn't effect sys.path. Then it calls removeduppaths, which walks over sys.path. That means that sys.path has already been set by something at that point. So, how/where did sys.path get set then? Or is sys.path always empty there at startup and this code is just to handle other situations? [snip] Brock From usernet at ilthio.net Thu Jul 2 16:37:13 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 20:37:13 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> Message-ID: On 2009-07-02, Paul Rubin wrote: [reformated with emphasis added] > #self.higher and self.lower are each either "available" (i.e. are not > #None), or unavailable (are None). [EMPHASIS] Return the highest of the > #available values. [/EMPHASIS] If no value is available, return None. Your code doesn't do what your comment says (notice EMPHASIS tags): > if self.higher is not None: > return self.higher > elif self.lower is not None: > return self.lower > else: > return None If lower is 5 and higher is 3, then it returns 3 because 3 != None in the first if. From dfstrauss at gmail.com Thu Jul 2 16:49:00 2009 From: dfstrauss at gmail.com (Dawie Strauss) Date: Thu, 2 Jul 2009 13:49:00 -0700 (PDT) Subject: PyTextMagicSMS text messaging library released Message-ID: <2b583f41-78d7-4a7e-a925-04934efe5458@m7g2000prd.googlegroups.com> TextMagic (http://www.textmagic.com) offers a convenient way to send text messages programmatically. This package provides a simple Python API on top of the TextMagic HTTPS API. Project documentation and source code is hosted at http://code.google.com/p/textmagic-sms-api-python/ The package can be dowloaded from http://pypi.python.org/pypi/PyTextMagicSMS/ From user at example.net Thu Jul 2 16:57:49 2009 From: user at example.net (superpollo) Date: Thu, 02 Jul 2009 22:57:49 +0200 Subject: stringio+tarfile Message-ID: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> why the following does not work? can you help me correct (if possible)? 1 import tarfile 2 import StringIO 3 sf1 = StringIO.StringIO("one\n") 4 sf2 = StringIO.StringIO("two\n") 5 tf = StringIO.StringIO() 6 tar = tarfile.open(tf , "w") 7 for name in [sf1 , sf2]: 8 tar.add(name) 9 print tf 10 sf1.close() 11 sf2.close() 12 tf.close() 13 tar.close() tia From mail at timgolden.me.uk Thu Jul 2 16:58:03 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 02 Jul 2009 21:58:03 +0100 Subject: how to spawn a process under different user In-Reply-To: References: Message-ID: <4A4D1F5B.6070605@timgolden.me.uk> sanket wrote: > Hello All, > > I am trying to use python's subprocess module to launch a process. > but in order to do that I have to change the user. > > I am not getting any clue how to do that? > so can anyone please tell me How can I spawn a process under different > user than currently I am logging in as. What platform are you on? If you are on Windows, there was a thread on this subject some time in the last month. (Short answer: switching user before launching subprocess won't work; switching within the subprocess will...) TJG From rschroev_nospam_ml at fastmail.fm Thu Jul 2 17:28:19 2009 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Thu, 02 Jul 2009 23:28:19 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: Peter Otten schreef: > Ulrich Eckhardt wrote: > >> Bearophile wrote: >>> Ulrich Eckhardt: >>>> a way to automatically release the resource, something >>>> which I would do in the destructor in C++. >>> Is this helpful? >>> http://effbot.org/pyref/with.htm >> Yes, it aims in the same direction. However, I'm not sure this applies to >> my case. The point is that the resource handle is not just used locally in >> a restricted scope but it is allocated and stored. The 'with' is something >> that makes sense in the context of mutex locking, where you have a >> well-defined critical section. > > Isn't that exactly what RAII does? RAII also works if the resource handle is stored, for example, in a data member of an object. If that object is destroyed (because it goes out of scope, or because it is deleted), the resource is automatically destroyed too. The way RAII works is actually the one thing from C++ that I miss in Python. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From http Thu Jul 2 17:43:38 2009 From: http (Paul Rubin) Date: 02 Jul 2009 14:43:38 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> Message-ID: <7xocs2222d.fsf@ruckus.brouhaha.com> Tim Harig writes: > If lower is 5 and higher is 3, then it returns 3 because 3 != None in the > first if. Sorry, the presumption was that lower <= higher, i.e. the comparison had already been made and the invariant was enforced by the class constructor. The comment should have been more explicit, I guess. From sanket.s.patel at gmail.com Thu Jul 2 17:56:30 2009 From: sanket.s.patel at gmail.com (sanket) Date: Thu, 2 Jul 2009 14:56:30 -0700 (PDT) Subject: how to spawn a process under different user References: Message-ID: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> On Jul 2, 1:58?pm, Tim Golden wrote: > sanket wrote: > > Hello All, > > > I am trying to use python's subprocess module to launch a process. > > but in order to do that I have to change the user. > > > I am not getting any clue how to do that? > > so can anyone please tell me How can I spawn a process under different > > user than currently I am logging in as. > > What platform are you on? If you are on Windows, > there was a thread on this subject some time in > the last month. (Short answer: switching user before > launching subprocess won't work; switching within > the subprocess will...) > > TJG Hi TJG, Thanks for the reply. I am using python 2.4 on centos. Thanks, sanket From hannarosie at gmail.com Thu Jul 2 18:05:46 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Thu, 2 Jul 2009 15:05:46 -0700 Subject: String to List Question Message-ID: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> Hi, I am brand new to python and I love it, but I've been having some trouble with a file parser that I've been working on. It contains lines that start with a name and then continue with names, nicknames and phone numbers of people associated with that name. I need to create a list of the names of people associated with each singular person (the first name in each line). Each name/phone number is separated by a tab but if someone doesn't have a nickname there are two tabs between their name and number. I've been trying to figure out how to test for two tabs, skip over these people and move onto the next name but I just can't figure out how that will work in python. Any help would be greatly appreciated! Thanks, Hanna -------------- next part -------------- An HTML attachment was scrubbed... URL: From allen.fowler at yahoo.com Thu Jul 2 18:06:17 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Thu, 2 Jul 2009 15:06:17 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? Message-ID: <115250.26948.qm@web45607.mail.sp1.yahoo.com> I have an (in-development) python system that needs to shuttle events / requests around over the network to other parts of itself. It will also need to cooperate with a .net application running on yet a different machine. So, naturally I figured some sort of HTTP event / RPC type of would be a good idea? Are there any modules I should know about, or guidelines I could read, that could aid me in the design of the API? Thank you, :) From usernet at ilthio.net Thu Jul 2 18:14:18 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:14:18 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> Message-ID: <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> On 2009-07-02, Paul Rubin wrote: > Tim Harig writes: >> If lower is 5 and higher is 3, then it returns 3 because 3 != None in the >> first if. > Sorry, the presumption was that lower <= higher, i.e. the comparison > had already been made and the invariant was enforced by the class > constructor. The comment should have been more explicit, I guess. That being the case, it might be a good idea either to handle the situation and raise an exception or add: assert self.lower <= self.higher That way an exception will be raised if there is an error somewhere else in the code rather then silently passing a possibly incorrect value. From philip at semanchuk.com Thu Jul 2 18:15:03 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 2 Jul 2009 18:15:03 -0400 Subject: String to List Question In-Reply-To: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> References: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> Message-ID: <76DF2C44-9A40-4FC9-AA20-CEDC3ED37806@semanchuk.com> On Jul 2, 2009, at 6:05 PM, Hanna Michelsen wrote: > Hi, > > I am brand new to python and I love it, but I've been having some > trouble > with a file parser that I've been working on. It contains lines that > start > with a name and then continue with names, nicknames and phone > numbers of > people associated with that name. I need to create a list of the > names of > people associated with each singular person (the first name in each > line). > Each name/phone number is separated by a tab but if someone doesn't > have a > nickname there are two tabs between their name and number. > > I've been trying to figure out how to test for two tabs, skip over > these > people and move onto the next name but I just can't figure out how > that will > work in python. > > Any help would be greatly appreciated! Hi Hanna, Are you familiar with a string's split() function? It sounds like just what you need. http://docs.python.org/library/stdtypes.html#str.split HTH Philip From allen.fowler at yahoo.com Thu Jul 2 18:17:56 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Thu, 2 Jul 2009 15:17:56 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? Message-ID: <219972.64445.qm@web45607.mail.sp1.yahoo.com> > I have an (in-development) python system that needs to shuttle events / requests > around over the network to other parts of itself. It will also need to > cooperate with a .net application running on yet a different machine. > > So, naturally I figured some sort of HTTP event / RPC type of would be a good > idea? > > Are there any modules I should know about, or guidelines I could read, that > could aid me in the design of the API? > > To clarify: Each message would be <1KB of data total, and consist of some structured object containing strings, numbers, dates, etc. For instance there would be an "add user" request that would contain one or more User objects each having a number of properties like: - Full Name - Username - Password - Email addresses (a variable length array) - Street Address line1 - Street Address line1 - Street Address line1 - City - State - Zip - Sign Up Date .... and so on. Since I need to work with other platforms, pickle is out... what are the alternatives? XML? JSON? How should I formally define each of the valid messages and objects? Thank you, :) From mccdo at iastate.edu Thu Jul 2 18:22:07 2009 From: mccdo at iastate.edu (Doug McCorkle) Date: Thu, 2 Jul 2009 17:22:07 -0500 Subject: Detecting platform architecture with Parallels and Win XP x64 Message-ID: <8B80C75F-1F27-4E65-8BAE-C77441235943@iastate.edu> Hello, I am using Python 2.5.4 on Windows XP x64 with Parallels. When I try to use: distutils.util.get_platform sys.platform I always get win32 but if I use: platform.architecture() with the amd64 installer I get 64bit for the first argument. Is there a way to detect win64 without having to use the python executable to see if the application is on a 64bit architecture? Thanks. Doug From usernet at ilthio.net Thu Jul 2 18:27:05 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:27:05 GMT Subject: how to spawn a process under different user References: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> Message-ID: On 2009-07-02, sanket wrote: >> sanket wrote: >> > I am trying to use python's subprocess module to launch a process. >> > but in order to do that I have to change the user. > I am using python 2.4 on centos. I have never done this in python; but, using the normal system calls in C the process is basically: 1. fork() a new process 2. the child process changes its user id with setreuid() and possibly its group id with setregid() 3. then the child exec()s new process which replaces itself All of the necessary functions should be under the os module on POSIX operating systems. From clp2 at rebertia.com Thu Jul 2 18:27:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Jul 2009 15:27:23 -0700 Subject: XML(JSON?)-over-HTTP: How to define API? In-Reply-To: <115250.26948.qm@web45607.mail.sp1.yahoo.com> References: <115250.26948.qm@web45607.mail.sp1.yahoo.com> Message-ID: <50697b2c0907021527j6cd6cfb6o734c989b71da77b1@mail.gmail.com> On Thu, Jul 2, 2009 at 3:06 PM, Allen Fowler wrote: > > I have an (in-development) python system that needs to shuttle events / requests around over the network to other parts of itself. ? It will also need to cooperate with a .net application running on yet a different machine. > > So, naturally I figured some sort of HTTP event / RPC type of would be a good idea? > > Are there any modules I should know about, or guidelines I could read, that could aid me in the design of the API? http://en.wikipedia.org/wiki/Representational_State_Transfer (aka REST) O'Reilly also has a book on the subject. Cheers, Chris -- http://blog.rebertia.com From http Thu Jul 2 18:29:49 2009 From: http (Paul Rubin) Date: 02 Jul 2009 15:29:49 -0700 Subject: XML(JSON?)-over-HTTP: How to define API? References: Message-ID: <7xprcipvky.fsf@ruckus.brouhaha.com> Allen Fowler writes: > Since I need to work with other platforms, pickle is out... what > are the alternatives? XML? JSON? json is the easiest to prototype with and is less bureaucratic. xml has more serious tools for schema specification and validation etc. You could start with json and switch later. As for the rpc mechanism, using urllib and one of the HTTP server modules is probably simplest. From charles at declareSub.com Thu Jul 2 18:30:43 2009 From: charles at declareSub.com (Charles Yeomans) Date: Thu, 2 Jul 2009 18:30:43 -0400 Subject: XML(JSON?)-over-HTTP: How to define API? In-Reply-To: <115250.26948.qm@web45607.mail.sp1.yahoo.com> References: <115250.26948.qm@web45607.mail.sp1.yahoo.com> Message-ID: <35DA358F-DFDC-425D-BD68-AFEF2024F537@declareSub.com> On Jul 2, 2009, at 6:06 PM, Allen Fowler wrote: > > I have an (in-development) python system that needs to shuttle > events / requests around over the network to other parts of > itself. It will also need to cooperate with a .net application > running on yet a different machine. > > So, naturally I figured some sort of HTTP event / RPC type of would > be a good idea? > > Are there any modules I should know about, or guidelines I could > read, that could aid me in the design of the API? > > > Thank you, > :) I'd suggest the O'Reilly book "RESTful Web Services". Charles Yeomans From http Thu Jul 2 18:32:27 2009 From: http (Paul Rubin) Date: 02 Jul 2009 15:32:27 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> Message-ID: <7xljn6pvgk.fsf@ruckus.brouhaha.com> Tim Harig writes: > That being the case, it might be a good idea either to handle the situation > and raise an exception or add: > > assert self.lower <= self.higher > > That way an exception will be raised if there is an error somewhere else > in the code rather then silently passing a possibly incorrect value. Well, that assert is not right because you have to handle the case where one of the values is None. The general sentiment is reasonable though, if you're concerned that the precondition may not be valid. From rhodri at wildebst.demon.co.uk Thu Jul 2 18:38:49 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 02 Jul 2009 23:38:49 +0100 Subject: String to List Question In-Reply-To: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> References: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> Message-ID: On Thu, 02 Jul 2009 23:05:46 +0100, Hanna Michelsen wrote: > Hi, > > I am brand new to python and I love it, but I've been having some trouble > with a file parser that I've been working on. It contains lines that > start > with a name and then continue with names, nicknames and phone numbers of > people associated with that name. I need to create a list of the names of > people associated with each singular person (the first name in each > line). > Each name/phone number is separated by a tab but if someone doesn't have > a > nickname there are two tabs between their name and number. > > I've been trying to figure out how to test for two tabs, skip over these > people and move onto the next name but I just can't figure out how that > will > work in python. You might find the csv module in the standard library does a lot of the hard work for you: http://docs.python.org/library/csv.html You can define yourself a reader that splits the input on tabs, and then see how long the rows it returns are. Something like this (untested): import csv for row in csv.reader(open("phone_numbers.txt", "rb"), delimiter='\t'): if len(row) > 1: # Do your stuff -- Rhodri James *-* Wildebeest Herder to the Masses From Russ.Paielli at gmail.com Thu Jul 2 18:41:56 2009 From: Russ.Paielli at gmail.com (Russ P.) Date: Thu, 2 Jul 2009 15:41:56 -0700 (PDT) Subject: Is Psyco good for Python 2.6.1? Message-ID: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> I need to speed up some Python code, and I discovered Psyco. However, the Psyco web page has not been updated since December 2007. Before I go to the trouble of installing it, does anyone know if it is still good for Python 2.6.1? Thanks. From usernet at ilthio.net Thu Jul 2 18:49:52 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:49:52 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: On 2009-07-02, Paul Rubin wrote: > Tim Harig writes: >> That being the case, it might be a good idea either to handle the situation >> and raise an exception or add: >> assert self.lower <= self.higher >> That way an exception will be raised if there is an error somewhere else >> in the code rather then silently passing a possibly incorrect value. > Well, that assert is not right because you have to handle the case > where one of the values is None. The general sentiment is reasonable > though, if you're concerned that the precondition may not be valid. Sorry, it worked under 2.5: 2.5 >>> lower = None 2.5 >>> higher = 10 2.5 >>> assert lower <= higher 2.5 >>> assert higher < lower 2.5 Traceback (most recent call last): 2.5 File "", line 1, in 2.5 AssertionError 2.5 >>> higher = -10 2.5 >>> assert lower <= higher 2.5 >>> assert higher < lower 2.5 Traceback (most recent call last): 2.5 File "", line 1, in 2.5 AssertionError None seems to have been evaluated less then any integer. The same isn't true under 3.0: 3.0 >>> lower = None 3.0 >>> higher = 10 3.0 >>> assert lower <= higher 3.0 Traceback (most recent call last): 3.0 File "", line 1, in 3.0 TypeError: unorderable types: NoneType() <= int() Then it is probably easier to just handle it in the conditionals and raise an exception manually. From usernet at ilthio.net Thu Jul 2 18:56:49 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:56:49 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: On 2009-07-02, Tim Harig wrote: > Sorry, it worked under 2.5: [SNIP] > None seems to have been evaluated less then any integer. The same isn't > true under 3.0: None seem to have been evaluated less then any non-negative integer including 0. From sajmikins at gmail.com Thu Jul 2 19:04:31 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 16:04:31 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On Jul 2, 4:08?pm, Erik Max Francis wrote: > Simon Forman wrote: > > Hey I was hoping to get your opinions on a sort of minor stylistic > > point. > > These two snippets of code are functionally identical. Which would you > > use and why? > > The first one is easier [for me anyway] to read and understand, but > > slightly less efficient, while the second is [marginally] harder to > > follow but more efficient. > > > ## First snippet > > > if self.higher is self.lower is None: return > > if self.lower is None: return self.higher > > if self.higher is None: return self.lower > > > ## Second snippet > > > if self.higher is None: > > ? ? if self.lower is None: > > ? ? ? ? return > > ? ? return self.lower > > if self.lower is None: > > ? ? return self.higher > > > What do you think? > > Explicit is always better, `return None` when that is your intent. > `return` shouts to the reader, "I want to get out of this function now, > and no one cares about the return result." > > Personally, I only use the one-liner if/else clauses if it's an > extremely trivial condition, and even then, usually not there. ?(The > only place I use it consistently is `if __name__ == '__main__': > main()`.) ?If it's part of something more important that needs > inspection -- as this obviously does given the questions you've had > about it, doesn't skip space. ?Newlines are free. > > Even when expanding about the first test, there's no reason to do > chained `if`s. ?One `if` with an `and` works just fine. > > Paul Rubin's looked to be the best overall replacement, as the logic > here looks strangely turned inside out. ?You're obviously looking for > which one _isn't_ `None`, so write the tests that way. ?It's much easier > for everyone else (including your potential future self) to follow. > Thanks for all the great feedback! Some notes: This code is part of a delete() method for a binary tree implementation. "None" is used to simulate a NULL pointer. In the case where both children are non-None the code goes on to handle that. None is a "unitary" or "singleton" value, so "is" is the right comparison operator to use with it, at least in this "imitation pointer" usage. The bare return was used both to preclude executing the rest of the function and to return None. (I sometimes use "return # None" with None in a comment to note that the return value is used. Also, I will put "# return" or "# return None" at the end of a function if the returning-of-None is important to the implementation of whatever. I've never used dis.dis() to check the bytecodes, but I wouldn't be surprised to find that the compiler generated the same bytecode whether you explicitly state the return or comment it out.) In any event, as Duncan Booth pointed out, the entire line was redundant so the issue is somewhat moot in this particular case. As for one-line control flow statements in python, I usually refrain but this code wasn't originally meant for public viewing. Last but not least, the logic may seem backwards but it's actually correct. If you check for non-None (NULL) -ness and return the thing you checked, rather than checking for None-ness and returning the other, the case where both are non-None is not handled correctly. Thanks again, ~Simon FWIW, here's the BinaryTree class, the code is in the _delete_me() method. from random import random class BinaryTree: ''' A Binary Tree implementation. Provides: get(key) - Return item for key or None. insert(key, value) - Insert value under key, replacing old ones. delete(key) - Delete value under key if any. keys() - Iterate through the keys in sort order. values() - Iterate through the values in sort order. items() - Iterate through (key, value) pairs in sort order. ''' def __init__(self, key, value): self.key = key self.value = value self.higher = self.lower = None def get(self, key): ''' Return item for key or None. ''' if key == self.key: return self.value if key < self.key and self.lower is not None: return self.lower.get(key) if self.higher is not None: assert key > self.key return self.higher.get(key) def insert(self, key, value): ''' Insert value under key, replacing old ones. ''' if key == self.key: self.value = value elif key < self.key: if self.lower is None: self.lower = BinaryTree(key, value) else: self.lower.insert(key, value) else: assert key > self.key if self.higher is None: self.higher = BinaryTree(key, value) else: self.higher.insert(key, value) def delete(self, key): ''' Delete value under key if any. ''' if key < self.key and self.lower is not None: self.lower = self.lower.delete(key) elif key > self.key and self.higher is not None: self.higher = self.higher.delete(key) elif key == self.key: return self._delete_me() return self def _delete_me(self): if self.lower is None: return self.higher if self.higher is None: return self.lower # Two children... try: side = self._which_side except AttributeError: side = bool(int(random() * 2)) # Alternate sides, should help keep tree balanced. self._which_side = not side method = self._delete_lower if side else self._delete_higher return method() def _delete_lower(self): next = self.lower while next.higher is not None: next = next.higher self.key = next.key self.value = next.value self.lower = self.lower.delete(self.key) return self def _delete_higher(self): next = self.higher while next.lower is not None: next = next.lower self.key = next.key self.value = next.value self.higher = self.higher.delete(self.key) return self def keys(self): ''' Iterate through the keys in sort order. ''' for key, value in self.items(): yield key def values(self): ''' Iterate through the values in sort order. ''' for key, value in self.items(): yield value def items(self): ''' Iterate through (key, value) pairs in sort order. ''' if self.lower is not None: for kv in self.lower.items(): yield kv yield self.key, self.value if self.higher is not None: for kv in self.higher.items(): yield kv From barry at python.org Thu Jul 2 19:10:39 2009 From: barry at python.org (Barry Warsaw) Date: Thu, 2 Jul 2009 19:10:39 -0400 Subject: Python 3.0 (pinin' for the fjords) Message-ID: Now that Python 3.1 is out, it seems there's been some confusion as to whether there will be one last Python 3.0 release, i.e. Python 3.0.2. At the PyCon 2009 language summit it was decided that there will be *no* Python 3.0.2. Python 3.0 is different than all other releases. There will be no last maintenance release and no ongoing security releases. Python 3.1 is the replacement for Python 3.0, and it's release schedule was accelerated specifically so that production users would be able to switch immediately. If you're using Python 3, you are strongly urged to update to Python 3.1. Python 3.1 will be a 'normal' release in that we're making all the guarantees that we make for other releases. It is likely that Python 3.2 is 18-24 months away, there will be Python 3.1.x maintenance releases, and there will be one last Python 3.1.x release after the final release of Python 3.2. Cheers, -Barry -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 849 bytes Desc: This is a digitally signed message part URL: From sajmikins at gmail.com Thu Jul 2 19:13:47 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 16:13:47 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> On Jul 2, 3:57?pm, Scott David Daniels wrote: > Duncan Booth wrote: > > Simon Forman wrote: > >> ... > >> if self.higher is self.lower is None: return > >> ... > > As a matter of style however I wouldn't use the shorthand to run two 'is' > > comparisons together, I'd write that out in full if it was actually needed > > here. > > Speaking only to the style issue, when I've wanted to do something like > that, I find: > > ? ? ? ?if self.higher is None is self.lower: > ? ? ? ? ? ?... > > more readable, by making clear they are both being compared to a > constant, rather than compared to each other. I was going to do it that way for aesthetics if nothing else, but in this particular code "self.higher is self.lower" could only have been True if they were both None, so the final "is None" was redundant and I only included it as a "just-in-case" check in case someone someday used the code in such a way as to assign the same object (not None) to both self.higher and self.lower... Totally pointless, I'm sorry to say. I'm glad the whole line was redundant really. > More often, I've used code like: > > ? ? ? ?if is not None is not : > ? ? ? ? ? ?... > > since I am usually working on non-defaulting cases in the body. > I find the form above simpler to read than: > > ? ? ? ?if is not None and is not None: > ? ? ? ? ? ?... > > I do draw the line at two, though, and with three or more I'll > paren-up a list of parallel comparisons: > > ? ? ? ?if ( is not None > ? ? ? ? ?and is not None > ? ? ? ? ?and is not None): > ? ? ? ? ? ?... > > --Scott David Daniels > Scott.Dani... at Acm.Org From mensanator at aol.com Thu Jul 2 20:34:31 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 2 Jul 2009 17:34:31 -0700 (PDT) Subject: Is Psyco good for Python 2.6.1? References: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> Message-ID: On Jul 2, 5:41?pm, "Russ P." wrote: > I need to speed up some Python code, and I discovered Psyco. However, > the Psyco web page has not been updated since December 2007. Before I > go to the trouble of installing it, does anyone know if it is still > good for Python 2.6.1? Thanks. Go back to the web site and look at the download page. Usually, the download page will list various versions to use with various Python versions. If it lists a 2.4 version, a 2.5 version, but no 2.6 version, most likely the authors haven't yet made a 2.6 version so you're SOL. Even that isn't guaranteed to always work. For instance, the authors of the smpy module didn't think they needed a seperate 2.6 version (and didn't make any effort to actually try it 2.6). Turned out to be dead wrong on that and had to scramble to release a fix. From http Thu Jul 2 20:57:15 2009 From: http (Paul Rubin) Date: 02 Jul 2009 17:57:15 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: <7x8wj61t3o.fsf@ruckus.brouhaha.com> Tim Harig writes: > > Well, that assert is not right because you have to handle the case > > where one of the values is None... > Sorry, it worked under 2.5: Well, it didn't crash under 2.5. Whether the result was correct is a different question. > None seems to have been evaluated less then any integer. The same isn't > true under 3.0: But the original code didn't specify the non-None values to be integers. Really, it's unwise to rely on an ordering relation between None and values of arbitrary other types, unless supportable by a clear specification, but even then, it's a code smell. > 3.0 TypeError: unorderable types: NoneType() <= int() That is far preferable to what 2.x does. From rylesny at gmail.com Thu Jul 2 21:03:59 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 18:03:59 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: <5a6891ba-bb93-41ab-8ba5-acb23bde3ae9@a7g2000yqk.googlegroups.com> > You can go ahead and implement a __del__() method. It will often work in > CPython, but you get no guarantees, especially when you have reference > cycles and with other Python implementations that don't use refcounting. And for resources whose lifetime is greater than your process (e.g. a file), you can also use the atexit module to help ensure they are cleaned/reclaimed at shutdown. One way to do this is to maintain a weak dictionary (from the weakref module) to your objects and install a handler which iterates this. This is useful for not only dealing with reference cycles, but for objects which may exist at the time the interpreter exits. These may not be deleted. http://docs.python.org/reference/datamodel.html#object.__del__ From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:37 -0300 Subject: stringio+tarfile References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: En Thu, 02 Jul 2009 17:57:49 -0300, superpollo escribi?: > why the following does not work? can you help me correct (if possible)? Explain "does not work" please. Does it raise an exception? Which one? Please post the complete traceback. You don't get the expected result? Tell us what did you expect and what you actually get. If you provide this information, it's a lot easier for people to look at your problem and eventually find a solution. Fortunately my crystall ball is back from the repair shop and I can guess that you want to create a tar file in memory, and add some content from a memory buffer too, right? I didn't run your code but I can see two problems: > 5 tf = StringIO.StringIO() > 6 tar = tarfile.open(tf , "w") The first argument to tarfile.open is the *name* of the file to open/create. In this case you don't have a real file, but a StringIO object; use the fileobj argument instead: tarfile.open(fileobj=tf, mode="w") See http://docs.python.org/library/tarfile.html#tarfile.open > 7 for name in [sf1 , sf2]: > 8 tar.add(name) The add method takes the *name* of a file to add to the archive. Again, you don't have a real file to add; try the addfile method instead. See http://docs.python.org/library/tarfile.html#tarfile.TarFile.add -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:42 -0300 Subject: how to spawn a process under different user References: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> Message-ID: En Thu, 02 Jul 2009 19:27:05 -0300, Tim Harig escribi?: > On 2009-07-02, sanket wrote: >>> sanket wrote: >>> > I am trying to use python's subprocess module to launch a process. >>> > but in order to do that I have to change the user. >> I am using python 2.4 on centos. > > I have never done this in python; but, using the normal system calls in C > the process is basically: > > 1. fork() a new process > 2. the child process changes its user id with setreuid() and > possibly its group id with setregid() > 3. then the child exec()s new process which replaces itself > > All of the necessary functions should be under the os module on POSIX > operating systems. How to do that using the subprocess module: write a function for item (2) above and pass it as the preexec_fn argument to Popen. preexec_fn is executed after fork() and before exec() -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:45 -0300 Subject: dealloc function in python extend c module References: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> <8599A253-D2C9-4EE4-AF4F-AEF658BA2033@semanchuk.com> Message-ID: En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk escribi?: > On Jul 2, 2009, at 2:11 AM, Shen, Yu-Teh wrote: > >> I create my extend type something like >> http://www.python.org/doc/current/extending/newtypes.html. >> And my type has a member which is a pointer point to my allocate >> memory ( no ref count). >> ex: >> --------------------------------------------------- >> typedef struct { >> PyObject_HEAD >> /* Type-specific fields go here. */ >> mytype *p; >> } noddy_NoddyObject; >> >> And i write dealloc function like this: >> --------------------------------------------------- >> static void >> Noddy_dealloc(Noddy* self) >> { >> delete p; >> self->ob_type->tp_free((PyObject*)self); >> } >> >> And I found it's strange that it didn't call dealloc when there is no >> reference. And you set the tp_dealloc field in the type structure to Noddy_dealloc, did you? >> ex: >> a = Noddy() >> b = a >> del a >> del b >> # i think there is no ref to the object, and it should call dealloc >> but it didn't call!!! You can use sys.getrefcount(a) to check how many references it has. Remember that it returns one more than the value you expect (because the function itself holds a temporary reference to its argument) > Hi Shen, > I'm no expert on Python memory management, but since no once else has > answered your question I'll tell you what I *think* is happening. > > Python doesn't delete objects as soon as they're dereferenced. Nope. CPython *does* destroy objects as soon as their reference count reaches zero. It does not rely on garbage collection for that. > It merely marks them as safe for garbage collection (GC). If GC never > happens (and it might not in a small test program), your dealloc > function won't run. The garbage collector is only used to recover memory from object cycles (in the CPython implementation; Jython *does* use garbage collection for "normal" object destruction too) > Now some more knowledgeable person will probably correct me. =) And now some more knowledgeable person will probably correct me. =) -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:48 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:48 -0300 Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> <7c8e96a1-cbb2-4036-9aa1-1ecdfd84cca8@j32g2000yqh.googlegroups.com> Message-ID: En Thu, 02 Jul 2009 07:55:42 -0300, Pedram escribi?: > On Jul 2, 1:11?pm, Peter Otten <__pete... at web.de> wrote: >> [snip explanation of strange pointer manipulations in the C code of the >> integer type] > Oh, I got it! What a wonderful implementation! :o Well, I would not say it's "wonderful"... The wonderful part is that all those nasty tricks are only there, hidden from us poor mortals, so we can focus on writing nice and elegant Python code instead :) -- Gabriel Genellina From http Thu Jul 2 21:30:07 2009 From: http (Paul Rubin) Date: 02 Jul 2009 18:30:07 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <7x4otu1rkw.fsf@ruckus.brouhaha.com> Simon Forman writes: > This code is part of a delete() method for a binary tree > implementation. "None" is used to simulate a NULL pointer. In the case > where both children are non-None the code goes on to handle that. > > None is a "unitary" or "singleton" value, so "is" is the right > comparison operator to use with it, at least in this "imitation > pointer" usage. Yes, but the concept of null pointers is considered kludgy these days. One alternative is to represent an empty node as an empty list [], and a node with value x as the 1-element list [x]. That incurs some storage overhead, but all the case by case checking in your comparison function goes away: return (self.higher + self.lower)[:1] > I've never used dis.dis() to check the bytecodes, but I wouldn't be > surprised to find that the compiler generated the same bytecode > whether you explicitly state the return or comment it out.) I really wouldn't worry about this. Python is so slow no matter what you do, that 1 more no-op bytecode won't matter. > Last but not least, the logic may seem backwards but it's actually > correct. If you check for non-None (NULL) -ness and return the thing > you checked, rather than checking for None-ness and returning the > other, the case where both are non-None is not handled correctly. The code you posted didn't return anything if both values were non-None. As for checking for None-ness vs. non-None-ness, sure, the two are logically equivalent, but I think checking for non-None expresses the algorithm more clearly. > FWIW, here's the BinaryTree class, ... > A Binary Tree implementation. Provides: > get(key) - Return item for key or None. I'm sorry but I find that class rather ugly. The code would be a lot smaller and have fewer corner cases with a different data representation. From rylesny at gmail.com Thu Jul 2 22:10:18 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 19:10:18 -0700 (PDT) Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> On Jul 2, 6:10?am, "Gabriel Genellina" wrote: > En Wed, 01 Jul 2009 12:49:31 -0300, Scott David Daniels ? > escribi?: > > These loops work well with the two-argument version of iter, > > which is easy to forget, but quite useful to have in your bag > > of tricks: > > > ? ? ?def convert(in_queue, out_queue): > > ? ? ? ? ?for row in iter(in_queue.get, None): > > ? ? ? ? ? ? ?# ... convert row > > ? ? ? ? ? ? ?out_queue.put(converted_line) > > Yep, I always forget about that variant of iter() -- very handy! Yes, at first glance using iter() here seems quite elegant and clever. You might even pat yourself on the back, or treat yourself to an ice cream cone, as I once did. There is one subtle distinction, however. Please allow me to demonstrate. >>> import Queue >>> >>> queue = Queue.Queue() >>> >>> queue.put(1) >>> queue.put("la la la") >>> queue.put(None) >>> >>> list(iter(queue.get, None)) [1, 'la la la'] >>> >>> # Cool, it really works! I'm going to change all my old code to use this... new and *improved* ... >>> # And then one day your user inevitably does something like this. ... >>> class A(object): ... def __init__(self, value): ... self.value = value ... ... def __eq__(self, other): ... return self.value == other.value ... >>> queue.put(A(1)) >>> queue.put(None) >>> >>> # And then this happens inside your 'generic' code (which probably even passed your unit tests). ... >>> list(iter(queue.get, None)) Traceback (most recent call last): File "", line 1, in File "", line 5, in __eq__ AttributeError: 'NoneType' object has no attribute 'value' >>> >>> # Oh... yeah. I really *did* want 'is None' and not '== None' which iter() will do. Sorry guys! Please don't let this happen to you too ;) From http Thu Jul 2 22:20:03 2009 From: http (Paul Rubin) Date: 02 Jul 2009 19:20:03 -0700 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> Message-ID: <7xfxdev770.fsf@ruckus.brouhaha.com> ryles writes: > >>> # Oh... yeah. I really *did* want 'is None' and not '== None' > >>> which iter() will do. Sorry guys! > > Please don't let this happen to you too ;) None is a perfectly good value to put onto a queue. I prefer using a unique sentinel to mark the end of the stream: sentinel = object() From rylesny at gmail.com Thu Jul 2 22:23:54 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 19:23:54 -0700 (PDT) Subject: What are the limitations of cStringIO.StringIO() ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: <5354b682-1f7e-4274-96e6-b51a3556e931@h11g2000yqb.googlegroups.com> This reminds me. Would anyone object to adding a sentence to http://docs.python.org/library/stringio.html#module-cStringIO just to mention that attributes cannot be added to a cStringIO, like such: % import cStringIO, StringIO % StringIO.StringIO().value = 1 % cStringIO.StringIO().value = 1 Traceback (most recent call last): File "", line 1, in AttributeError: 'cStringIO.StringO' object has no attribute 'value' From bearophileHUGS at lycos.com Thu Jul 2 22:34:50 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 2 Jul 2009 19:34:50 -0700 (PDT) Subject: Is Psyco good for Python 2.6.1? References: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> Message-ID: Russ P.: Python works well to me on Python 2.6+, on Windows. You can find a compiled version too, around. Bye, bearophile From davea at ieee.org Thu Jul 2 22:35:59 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 02 Jul 2009 22:35:59 -0400 Subject: stringio+tarfile In-Reply-To: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4A4D6E8F.1000506@ieee.org> superpollo wrote: >
why the > following does not work? can you help me correct (if possible)? > > 1 import tarfile > 2 import StringIO > 3 sf1 = StringIO.StringIO("one\n") > 4 sf2 = StringIO.StringIO("two\n") > 5 tf = StringIO.StringIO() > 6 tar = tarfile.open(tf , "w") > 7 for name in [sf1 , sf2]: > 8 tar.add(name) > 9 print tf > 10 sf1.close() > 11 sf2.close() > 12 tf.close() > 13 tar.close() > > tia > >
> Couldn't you have given a clue as to what doesn't work? What version of Python are you using, and on what platform? Do you get an error message, or are the results unreasonable? Details please. First problem I see is all those numbers before the lines. That's not valid python. Assuming that was a transcription error, we get to some other problems. tarfile.open() first argument is a filename, and you're passing it a Stringio object. Not the same at all. Fortunately, the Stringio should work as the third parameter, so you can change this line to tarfile.open(None, "w", tf) Similarly tar.add() takes a filename as a parameter, and you're trying to pass another Stringio. That's not a possible argument to the add() method at all. Try tar.addfile() I stopped at this point. Maybe somebody else can help, after you fill in a few details. From sajmikins at gmail.com Thu Jul 2 22:52:10 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 22:52:10 -0400 Subject: What are the limitations of cStringIO.StringIO() ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: <50f98a4c0907021952y50bea866heb69ed4cff52d590@mail.gmail.com> On Wed, Jul 1, 2009 at 7:48 AM, Barak, Ron wrote: > Hi, > > I think I'm up against a limitation in cStringIO.StringIO(), but could not > find any clues on the web, especially not in > http://docs.python.org/library/stringio.html. > > What I have is the following function: > > ??? def concatenate_files(self,filename_array): > ??????? import types > > ??????? f = cStringIO.StringIO() > ??????? for filename in filename_array: > ??????????? log_stream = LogStream(filename) > ??????????? if not isinstance(log_stream, types.NoneType): > ??????????????? try: > ??????????????????? string_ = log_stream.input_file.read() > ??????????????? except AttributeError, e: > ??????????????????????? sys.stderr.write("AttributeError: "+str(e)+"\n") > ??????????????????????? sys.stderr.write("log_stream: > "+str(log_stream)+"\n") > ??????????? else: > ??????????????? return(None) > > ??????????? f.write(string_) > ??????? return (f) > > And, when the list of files?- in filename_array - is pointing to long/large > enough files, I get the exception: > > Traceback (most recent call last): > ? File "svm_ts_tool_in_progress.py", line 244, in OnSelectCell > ??? self.InspectorViewController(row,col) > ? File "svm_ts_tool_in_progress.py", line 1216, in InspectorViewController > ??? self.InspectorePaneGetRecords(self.req_table, rec) > ? File "svm_ts_tool_in_progress.py", line 1315, in InspectorePaneGetRecords > ??? log_stream = > ConcatenatedLogStream(self.events_dict[req_table]["db_dict"].keys()) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 31, in > __init__ > ??? self.input_file = self.concatenate_files(filename_array) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 47, in > concatenate_files > ??? string_ = log_stream.input_file.read() > MemoryError > > Anyone knows whet's the limitation on cStringIO.StringIO() objects ? > Could you suggest a better way to create a string that contains the > concatenation of several big files ? > > Thanks, > Ron. > > -- > http://mail.python.org/mailman/listinfo/python-list > > MemoryError means you're out of memory. Try using the operating system commands for file concatenation, i.e. "cat" on *nix, dunno how on windows. Or just do whatever it is you're doing in a different way that doesn't require having all the data in memory at once. Also, this: not isinstance(log_stream, types.NoneType) should just be this: log_stream is not None and return is not a function, leave out the ()'s around the expression you're returning, and if you're returning None, you can leave out the None as it is the default return value anyway. Generally speaking don't do module imports in functions. From benjamin at python.org Thu Jul 2 22:54:31 2009 From: benjamin at python.org (Benjamin Peterson) Date: Fri, 3 Jul 2009 02:54:31 +0000 (UTC) Subject: What are the limitations of cStringIO.StringIO() ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> <5354b682-1f7e-4274-96e6-b51a3556e931@h11g2000yqb.googlegroups.com> Message-ID: ryles gmail.com> writes: > > This reminds me. Would anyone object to adding a sentence to > http://docs.python.org/library/stringio.html#module-cStringIO just to > mention that attributes cannot be added to a cStringIO, like such: That's true of almost all types that are implemented in C. From rylesny at gmail.com Thu Jul 2 22:55:19 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 19:55:19 -0700 (PDT) Subject: Adding an object to the global namespace through " f_globals" is that allowed ? References: <4A4BC22D.70502@gmail.com> Message-ID: <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> On Jul 2, 1:25 am, Terry Reedy wrote: > > The next statement works, > > but I'm not sure if it will have any dramatical side effects, > > other than overruling a possible object with the name A > > > def some_function ( ...) : > > A = object ( ...) > > sys._getframe(1).f_globals [ Name ] = A > > global name > name = A > > or is name is a string var > globals()[name] = A It wasn't explicit, but I think Stef meant that the global should be added to the caller's environment, which was the reason for sys._getframe(). Is this environment only intended for interactive use? I wonder if you might just set things up in a PYTHONSTARTUP script instead. From schickb at gmail.com Thu Jul 2 22:56:50 2009 From: schickb at gmail.com (schickb) Date: Thu, 2 Jul 2009 19:56:50 -0700 (PDT) Subject: Sequence splitting Message-ID: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> I have fairly often found the need to split a sequence into two groups based on a function result. Much like the existing filter function, but returning a tuple of true, false sequences. In Python, something like: def split(seq, func=None): if func is None: func = bool t, f = [], [] for item in seq: if func(item): t.append(item) else: f.append(item) return (t, f) The discussion linked to below has various approaches for doing this now, but most traverse the sequence twice and many don't apply a function to spit the sequence. http://stackoverflow.com/questions/949098/python-split-a-list-based-on-a-condition Is there any interest in a C implementation of this? Seems too trivial to write a PEP, so I'm just trying to measure interest before diving in. This wouldn't really belong in intertool. Would it be best implemented as a top level built-in? -Brad From alan.isaac at gmail.com Thu Jul 2 22:59:51 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 02:59:51 GMT Subject: Python 3.0 (pinin' for the fjords) In-Reply-To: References: Message-ID: Using the 3.1 Windows installer, I chose that I did not want the extensions registered, and the installer unregistered the .py and .pyw extensions (which I had wanted to keep associated with Python 2.6). Is this intentional? Alan Isaac PS I already fixed the problem. My question is about intent. From rylesny at gmail.com Thu Jul 2 23:09:25 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 20:09:25 -0700 (PDT) Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> Message-ID: On Jul 2, 10:20?pm, Paul Rubin wrote: > ryles writes: > > >>> # Oh... yeah. I really *did* want 'is None' and not '== None' > > >>> which iter() will do. Sorry guys! > > > Please don't let this happen to you too ;) > > None is a perfectly good value to put onto a queue. ?I prefer > using a unique sentinel to mark the end of the stream: > > ? ?sentinel = object() I agree, this is cleaner than None. We're still in the same boat, though, regarding iter(). Either it's 'item == None' or 'item == object ()', and depending on the type, __eq__ can introduce some avoidable risk. FWIW, even object() has its disadvantages. Namely, it doesn't work for multiprocessing.Queue which pickles and unpickles, thus giving you a new object. One way to deal with this is to define a "Stopper" class and type check objects taken from the queue. This is not news to anyone who's worked with multiprocessing.Queue, though. From tn.pablo at gmail.com Thu Jul 2 23:10:14 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 2 Jul 2009 22:10:14 -0500 Subject: Sequence splitting In-Reply-To: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: On Thu, Jul 2, 2009 at 21:56, schickb wrote: > I have fairly often found the need to split a sequence into two groups > based on a function result. Much like the existing filter function, > but returning a tuple of true, false sequences. In Python, something > like: > > def split(seq, func=None): > ? ?if func is None: > ? ? ? ?func = bool > ? ?t, f = [], [] > ? ?for item in seq: > ? ? ? ?if func(item): > ? ? ? ? ? ?t.append(item) > ? ? ? ?else: > ? ? ? ? ? ?f.append(item) > ? ?return (t, f) > > The discussion linked to below has various approaches for doing this > now, but most traverse the sequence twice and many don't apply a > function to spit the sequence. > http://stackoverflow.com/questions/949098/python-split-a-list-based-on-a-condition > > Is there any interest in a C implementation of this? Seems too trivial > to write a PEP, so I'm just trying to measure interest before diving > in. This wouldn't really belong in intertool. Would it be best > implemented as a top level built-in? > > -Brad > -- > http://mail.python.org/mailman/listinfo/python-list > This sounds like it belongs to the python-ideas list. I suggest posting there for better feedback, since the core developers check that list more often than this one. -- Pablo Torres N. From http Thu Jul 2 23:14:24 2009 From: http (Paul Rubin) Date: 02 Jul 2009 20:14:24 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: <7xzlbm5ugf.fsf@ruckus.brouhaha.com> schickb writes: > def split(seq, func=None): > if func is None: > func = bool > t, f = [], [] > for item in seq: > if func(item): > t.append(item) > else: > f.append(item) > return (t, f) untested: def split(seq, func=bool): xs = zip(seq, itertools.imap(func, seq)) t = list(x for (x,y) in xs if y) f = list(x for (x,y) in xs if not y) return (t, f) From http Thu Jul 2 23:15:40 2009 From: http (Paul Rubin) Date: 02 Jul 2009 20:15:40 -0700 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> Message-ID: <7xvdma5ueb.fsf@ruckus.brouhaha.com> ryles writes: > > ? ?sentinel = object() > > I agree, this is cleaner than None. We're still in the same boat, > though, regarding iter(). Either it's 'item == None' or 'item == object ()' Use "item is sentinel". From tn.pablo at gmail.com Thu Jul 2 23:17:32 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 2 Jul 2009 20:17:32 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> On Jul 2, 9:56?pm, schickb wrote: > I have fairly often found the need to split a sequence into two groups > based on a function result. Much like the existing filter function, > but returning a tuple of true, false sequences. In Python, something > like: > > def split(seq, func=None): > ? ? if func is None: > ? ? ? ? func = bool > ? ? t, f = [], [] > ? ? for item in seq: > ? ? ? ? if func(item): > ? ? ? ? ? ? t.append(item) > ? ? ? ? else: > ? ? ? ? ? ? f.append(item) > ? ? return (t, f) > > The discussion linked to below has various approaches for doing this > now, but most traverse the sequence twice and many don't apply a > function to spit the sequence.http://stackoverflow.com/questions/949098/python-split-a-list-based-o... > > Is there any interest in a C implementation of this? Seems too trivial > to write a PEP, so I'm just trying to measure interest before diving > in. This wouldn't really belong in intertool. Would it be best > implemented as a top level built-in? > > -Brad This sounds like it belongs to the python-ideas list. I suggest posting there for better feedback, since the core developers check that list more often than this one. From philip at semanchuk.com Thu Jul 2 23:17:35 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 2 Jul 2009 23:17:35 -0400 Subject: dealloc function in python extend c module In-Reply-To: References: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> <8599A253-D2C9-4EE4-AF4F-AEF658BA2033@semanchuk.com> Message-ID: On Jul 2, 2009, at 9:28 PM, Gabriel Genellina wrote: > En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk > escribi?: >> >> Hi Shen, >> I'm no expert on Python memory management, but since no once else >> has answered your question I'll tell you what I *think* is happening. >> >> Python doesn't delete objects as soon as they're dereferenced. > > Nope. CPython *does* destroy objects as soon as their reference count > reaches zero. It does not rely on garbage collection for that. > >> It merely marks them as safe for garbage collection (GC). If GC >> never happens (and it might not in a small test program), your >> dealloc function won't run. > > The garbage collector is only used to recover memory from object > cycles > (in the CPython implementation; Jython *does* use garbage collection > for > "normal" object destruction too) > >> Now some more knowledgeable person will probably correct me. =) Wow, that last part was the only part I got right. =) Thanks for straightening me out, Gabriel. From gagsl-py2 at yahoo.com.ar Thu Jul 2 23:47:54 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Jul 2009 00:47:54 -0300 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> <7xvdma5ueb.fsf@ruckus.brouhaha.com> Message-ID: En Fri, 03 Jul 2009 00:15:40 -0300, > escribi?: > ryles writes: >> > ? ?sentinel = object() >> >> I agree, this is cleaner than None. We're still in the same boat, >> though, regarding iter(). Either it's 'item == None' or 'item == object >> ()' > > Use "item is sentinel". We're talking about the iter() builtin behavior, and that uses == internally. It could have used an identity test, and that would be better for this specific case. But then iter(somefile.read, '') wouldn't work. A compromise solution is required; since one can customize the equality test but not an identity test, the former has a small advantage. (I don't know if this was the actual reason, or even if this really was a concious decision, but that's why *I* would choose == to test against the sentinel value). -- Gabriel Genellina From sajmikins at gmail.com Thu Jul 2 23:54:18 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 20:54:18 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> Message-ID: On Jul 2, 9:30?pm, Paul Rubin wrote: > Simon Forman writes: > > This code is part of a delete() method for a binary tree > > implementation. "None" is used to simulate a NULL pointer. In the case > > where both children are non-None the code goes on to handle that. > > > None is a "unitary" or "singleton" value, so "is" is the right > > comparison operator to use with it, at least in this "imitation > > pointer" usage. > > Yes, but the concept of null pointers is considered kludgy these days. Seriously? "kludgy"? (I really AM getting old.) Well I wouldn't know, I've been fortunate enough to program mostly in python for over half a decade now and None and 0 are as close as I've gotten to NULL in a long time. > One alternative is to represent an empty node as an empty list [], and > a node with value x as the 1-element list [x]. ?That incurs some > storage overhead, but all the case by case checking in your comparison > function goes away: > > ? ?return (self.higher + self.lower)[:1] Heh, program LISP much? :] That sounds very interesting, but I'm only writing a BTree to then use it to play with "persistent data structures" and the models I'm using are based on pointer code. I wouldn't want to try to re-implement them on top of a different "architecture", at least not at first. > > I've never used dis.dis() to check the bytecodes, but I wouldn't be > > surprised to find that the compiler generated the same bytecode > > whether you explicitly state the return or comment it out.) > > I really wouldn't worry about this. ?Python is so slow no matter what > you do, that 1 more no-op bytecode won't matter. I'm not worried about it at all. In fact, I'm pretty sure the compiler handles it for me as well as I could want or expect it to. "A Computer's Perspective on Moore's Law: Humans are getting more expensive at an exponential rate." - Mark Miller http://www.caplet.com/adages.html Python may be slow to execute but it's fast to write. (And write well.) ;] > > Last but not least, the logic may seem backwards but it's actually > > correct. If you check for non-None (NULL) -ness and return the thing > > you checked, rather than checking for None-ness and returning the > > other, the case where both are non-None is not handled correctly. > > The code you posted didn't return anything if both values were non-None. Yep, it was just a snippet, just the part I wanted feedback on. > As for checking for None-ness vs. non-None-ness, sure, the two are > logically equivalent, but I think checking for non-None expresses the > algorithm more clearly. In this particular case it's somewhat more elegant (IMO) to check "is None". > > FWIW, here's the BinaryTree class, ... > > ? ? A Binary Tree implementation. Provides: > > ? ? ? ? get(key) - Return item for key or None. > > I'm sorry but I find that class rather ugly. ?The code would be a lot > smaller and have fewer corner cases with a different data > representation. Um, thanks? Seriously though, care to put your money where your mouth is? I'd be interested to see a BTree implemented as you indicated above. Pointer code in python is silly in general, since you're only imitating real pointers with attributes (i.e. instance dicts) anyway. As I said above, I'm faking it with python to explore persistent data structures and the models I've found so far are pointer-based. I'm sure there are better (i.e. faster) ways to get the same (or sufficiently similar) behavior in a more "pythonic" fashion. (for example, to maintain a sorted sequence under insertion I'd use a list and the bisect module, and so on.) Reminds me, a few years back I implemented Knuth's "Dancing Links" algorithm in python for a Sudoku solver. It uses a kind of grid of doubly-linked lists to store information about the search space and to traverse that space to resolve an answer. The algorithm takes advantage of the fact that allocated nodes persisted in memory even when completely unlinked from the "surrounding" lists. You unlinked and re-linked the nodes/lists in a certain pattern to search and backtrack the search space. It's more than I can explain here. Really elegant algorithm. The point is in python you had to put ALL the nodes into a storage structure just to prevent them disappearing while unlinked from the "head" of the grid. Regards, ~Simon From schickb at gmail.com Thu Jul 2 23:55:12 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 20:55:12 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> Message-ID: <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> On Jul 2, 8:14?pm, Paul Rubin wrote: > schickb writes: > > def split(seq, func=None): > > ? ? if func is None: > > ? ? ? ? func = bool > > ? ? t, f = [], [] > > ? ? for item in seq: > > ? ? ? ? if func(item): > > ? ? ? ? ? ? t.append(item) > > ? ? ? ? else: > > ? ? ? ? ? ? f.append(item) > > ? ? return (t, f) > > untested: > > ? ?def split(seq, func=bool): > ? ? ? xs = zip(seq, itertools.imap(func, seq)) > ? ? ? t = list(x for (x,y) in xs if y) > ? ? ? f = list(x for (x,y) in xs if not y) > ? ? ? return (t, f) In my testing that is 3.5x slower than the original solution (and less clear imo). I fixed my version to take a bool default. Either way, I'm not really looking for additional ways to do this in Python unless I've totally missed something. What I am considering is writing it in C, much like filter. -Brad From http Thu Jul 2 23:55:12 2009 From: http (Paul Rubin) Date: 02 Jul 2009 20:55:12 -0700 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> <7xvdma5ueb.fsf@ruckus.brouhaha.com> Message-ID: <7xhbxu4dzz.fsf@ruckus.brouhaha.com> "Gabriel Genellina" writes: > We're talking about the iter() builtin behavior, and that uses == > internally. Oh, I see. Drat. > It could have used an identity test, and that would be better for this > specific case. But then iter(somefile.read, '') wouldn't work. Yeah, it should allow supplying a predicate instead of using == on a value. How about (untested): from itertools import * ... for row in takewhile(lambda x: x is sentinel, starmap(in_queue.get, repeat(()))): ... From schickb at gmail.com Thu Jul 2 23:56:03 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 20:56:03 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: On Jul 2, 8:17?pm, "Pablo Torres N." wrote: > On Jul 2, 9:56?pm, schickb wrote: > > > I have fairly often found the need to split a sequence into two groups > > based on a function result. > > This sounds like it belongs to the python-ideas list. ?I suggest > posting there for better feedback, since the core developers check > that list more often than this one. Thanks, I didn't know about that list. From rylesny at gmail.com Thu Jul 2 23:56:52 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 20:56:52 -0700 (PDT) Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> Message-ID: On Jul 2, 11:09?am, masher wrote: > My questions, then, is: Is there a more elegant/pythonic way of doing > what I am trying to do with the current Pool class? Another thing you might try is to subclass Pool and add an apply_async () wrapper which would wait for _taskqueue.qsize() to reach the desired size. You would probably do this wait in a loop with a small sleep. This approach would avoid needing a second Queue, but you would also add some delay to your producer due to the sleep (something you're not currently worried about). The minimum sleep may be something like 1 ms (it's really system dependent), but the time it takes for a thread blocked on a mutex to wake up is often more on the order of microseconds, which you have with your blocking queue. I doubt this offers you much satisfaction, though. > If the verdict is no, I'll be happy to file a bug report. Yeah, I think it's a worth a try. From http Fri Jul 3 00:08:02 2009 From: http (Paul Rubin) Date: 02 Jul 2009 21:08:02 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> Message-ID: <7xhbxu4del.fsf@ruckus.brouhaha.com> Brad writes: > On Jul 2, 8:14?pm, Paul Rubin wrote: > > schickb writes: > > > def split(seq, func=None): > > > ? ? if func is None: > > > ? ? ? ? func = bool > > > ? ? t, f = [], [] > > > ? ? for item in seq: > > > ? ? ? ? if func(item): > > > ? ? ? ? ? ? t.append(item) > > > ? ? ? ? else: > > > ? ? ? ? ? ? f.append(item) > > > ? ? return (t, f) > > > > untested: > > > > ? ?def split(seq, func=bool): > > ? ? ? xs = zip(seq, itertools.imap(func, seq)) > > ? ? ? t = list(x for (x,y) in xs if y) > > ? ? ? f = list(x for (x,y) in xs if not y) > > ? ? ? return (t, f) > > In my testing that is 3.5x slower than the original solution (and less > clear imo). I fixed my version to take a bool default. Either way, I'm > not really looking for additional ways to do this in Python unless > I've totally missed something. What I am considering is writing it in > C, much like filter. I'm a little skeptical that the C version will help much, if it's evaluating a python function at every list element. Here's a variant of your version: def split(seq, func=bool): ? ? t, f = [], [] ta, fa = t.append, f.append ? ? for item in seq: (ta if func(item) else fa)(item) ? ? return (t, f) This avoids some dict lookups and copying. I wonder if that helps significantly. From rylesny at gmail.com Fri Jul 3 00:13:19 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 21:13:19 -0700 (PDT) Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> <7xvdma5ueb.fsf@ruckus.brouhaha.com> <7xhbxu4dzz.fsf@ruckus.brouhaha.com> Message-ID: <34271171-ba63-43bb-b79c-83c036453007@c9g2000yqm.googlegroups.com> On Jul 2, 11:55?pm, Paul Rubin wrote: > Yeah, it should allow supplying a predicate instead of using == on > a value. ?How about (untested): > > ? ?from itertools import * > ? ?... > ? ?for row in takewhile(lambda x: x is sentinel, > ? ? ? ? ? ? ? ? ? ? ? ? ?starmap(in_queue.get, repeat(()))): > ? ? ? ... Yeah, it's a small recipe I'm sure a lot of others have written as well. My old version: def iterwhile(callable_, predicate): """ Like iter() but with a predicate instead of a sentinel. """ return itertools.takewhile(predicate, repeatfunc(callable_)) where repeatfunc is as defined here: http://docs.python.org/library/itertools.html#recipes I wish all of these little recipes made their way into itertools or a like module; itertools seems a bit tightly guarded. From schickb at gmail.com Fri Jul 3 00:34:21 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 21:34:21 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: On Jul 2, 9:08?pm, Paul Rubin wrote: > Brad writes: > > On Jul 2, 8:14?pm, Paul Rubin wrote: > > > schickb writes: > > > > def split(seq, func=None): > > > > ? ? if func is None: > > > > ? ? ? ? func = bool > > > > ? ? t, f = [], [] > > > > ? ? for item in seq: > > > > ? ? ? ? if func(item): > > > > ? ? ? ? ? ? t.append(item) > > > > ? ? ? ? else: > > > > ? ? ? ? ? ? f.append(item) > > > > ? ? return (t, f) > > > > untested: > > > > ? ?def split(seq, func=bool): > > > ? ? ? xs = zip(seq, itertools.imap(func, seq)) > > > ? ? ? t = list(x for (x,y) in xs if y) > > > ? ? ? f = list(x for (x,y) in xs if not y) > > > ? ? ? return (t, f) > > > In my testing that is 3.5x slower than the original solution (and less > > clear imo). I fixed my version to take a bool default. Either way, I'm > > not really looking for additional ways to do this in Python unless > > I've totally missed something. What I am considering is writing it in > > C, much like filter. > > I'm a little skeptical that the C version will help much, if it's > evaluating a python function at every list element. ? Perhaps true, but it would be a nice convenience (for me) as a built- in written in either Python or C. Although the default case of a bool function would surely be faster. > Here's a variant of your version: > > ?def split(seq, func=bool): > ?? ? t, f = [], [] > ? ? ?ta, fa = t.append, f.append > ?? ? for item in seq: > ? ? ? ? ?(ta if func(item) else fa)(item) > ?? ? return (t, f) > > This avoids some dict lookups and copying. ?I wonder if that helps > significantly. Faster, but in tests of a few short sequences only 1% so. -Brad From ldo at geek-central.gen.new_zealand Fri Jul 3 00:39:46 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 03 Jul 2009 16:39:46 +1200 Subject: Config files with different types References: Message-ID: In message , Zach Hobesh wrote: > I want to be able to look at the value and determine what type it > SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when > it might be more useful as a boolean. Typically the type should be what you expect, not what is given. For example, it doesn't make sense for your configs["overwrite"] setting to be "red", does it? A reasonable solution might be to have a table of expected types for each config item keyword, e.g. configs_types = \ { ... "overwrite" : lambda x : x[0] not in set("nNfF"), ... } Then you can just do something like configs[keyword] = configs_types[keyword](configs[keyword]) with appropriate trapping of ValueError exceptions. From tn.pablo at gmail.com Fri Jul 3 00:40:13 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 2 Jul 2009 23:40:13 -0500 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: On Thu, Jul 2, 2009 at 23:34, Brad wrote: > On Jul 2, 9:08?pm, Paul Rubin wrote: >> Brad writes: >> > On Jul 2, 8:14?pm, Paul Rubin wrote: >> > > schickb writes: >> > > > def split(seq, func=None): >> > > > ? ? if func is None: >> > > > ? ? ? ? func = bool >> > > > ? ? t, f = [], [] >> > > > ? ? for item in seq: >> > > > ? ? ? ? if func(item): >> > > > ? ? ? ? ? ? t.append(item) >> > > > ? ? ? ? else: >> > > > ? ? ? ? ? ? f.append(item) >> > > > ? ? return (t, f) >> >> > > untested: >> >> > > ? ?def split(seq, func=bool): >> > > ? ? ? xs = zip(seq, itertools.imap(func, seq)) >> > > ? ? ? t = list(x for (x,y) in xs if y) >> > > ? ? ? f = list(x for (x,y) in xs if not y) >> > > ? ? ? return (t, f) >> >> > In my testing that is 3.5x slower than the original solution (and less >> > clear imo). I fixed my version to take a bool default. Either way, I'm >> > not really looking for additional ways to do this in Python unless >> > I've totally missed something. What I am considering is writing it in >> > C, much like filter. >> >> I'm a little skeptical that the C version will help much, if it's >> evaluating a python function at every list element. > > Perhaps true, but it would be a nice convenience (for me) as a built- > in written in either Python or C. Although the default case of a bool > function would surely be faster. > >> Here's a variant of your version: >> >> ?def split(seq, func=bool): >> ?? ? t, f = [], [] >> ? ? ?ta, fa = t.append, f.append >> ?? ? for item in seq: >> ? ? ? ? ?(ta if func(item) else fa)(item) >> ?? ? return (t, f) >> >> This avoids some dict lookups and copying. ?I wonder if that helps >> significantly. > > Faster, but in tests of a few short sequences only 1% so. > > -Brad > -- > http://mail.python.org/mailman/listinfo/python-list > If it is speed that we are after, it's my understanding that map and filter are faster than iterating with the for statement (and also faster than list comprehensions). So here is a rewrite: def split(seq, func=bool): t = filter(func, seq) f = filter(lambda x: not func(x), seq) return list(t), list(f) The lambda thing is kinda ugly, but I can't think of anything else. Also, is it ok to return lists? Py3k saw a lot of APIs changed to return iterables instead of lists, so maybe my function should have 'return t, f' as it's last statement. -- Pablo Torres N. From sajmikins at gmail.com Fri Jul 3 00:40:42 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 21:40:42 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> <9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> Message-ID: <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> On Jul 2, 4:31?am, Tep wrote: > On 2 Jul., 10:25, Tep wrote: > > > > > On 2 Jul., 01:56, MRAB wrote: > > > > someone wrote: > > > > Hello, > > > > > how can I replace '?' sign from string? Or do split at that character? > > > > Getting unicode error if I try to do it: > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > > > 1: ordinal not in range(128) > > > > > Thanks, Pet > > > > > script is # -*- coding: UTF-8 -*- > > > > It sounds like you're mixing bytestrings with Unicode strings. I can't > > > be any more helpful because you haven't shown the code. > > > Oh, I'm sorry. Here it is > > > def cleanInput(input) > > ? ? return input.replace('?', '') > > I also need: > > #input is html source code, I have problem with only this character > #input = 'foo ? bar' > #return should be foo > def splitInput(input) > ? ? parts = input.split(' ? ') > ? ? return parts[0] > > Thanks! Okay people want to help you but you must make it easy for us. Post again with a small piece of code that is runnable as-is and that causes the traceback you're talking about, AND post the complete traceback too, as-is. I just tried a bit of your code above in my interpreter here and it worked fine: |>>> data = 'foo ? bar' |>>> data.split('?') |['foo ', ' bar'] |>>> data = u'foo ? bar' |>>> data.split(u'?') |[u'foo ', u' bar'] Figure out the smallest piece of "html source code" that causes the problem and include that with your next post. HTH, ~Simon You might also read this: http://catb.org/esr/faqs/smart-questions.html From zookog at gmail.com Fri Jul 3 00:48:10 2009 From: zookog at gmail.com (zooko) Date: Thu, 2 Jul 2009 21:48:10 -0700 (PDT) Subject: No trees in the stdlib? References: <11eb0a6e-4e21-4a61-bf46-4221fd75dc8d@v15g2000prn.googlegroups.com> <006078f0$0$9721$c3e8da3@news.astraweb.com> Message-ID: Try PyJudy: http://www.dalkescientific.com/Python/PyJudy.html From http Fri Jul 3 00:56:40 2009 From: http (Paul Rubin) Date: 02 Jul 2009 21:56:40 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> Message-ID: <7x3a9egy9j.fsf@ruckus.brouhaha.com> Simon Forman writes: > > Yes, but the concept of null pointers is considered kludgy these days. > Seriously? "kludgy"? (I really AM getting old.) http://math.andrej.com/2009/04/11/on-programming-language-design/ discusses the issue (scroll down to "Undefined values" section). > Well I wouldn't know, I've been fortunate enough to program mostly in > python for over half a decade now and None and 0 are as close as I've > gotten to NULL in a long time. Right, and how many times have you had to debug AttributeError: 'NoneType' object has no attribute 'foo' or the equivalent null pointer exceptions in Java, C, or whatever? They are very common. And the basic idea is that if you avoid using null pointers in the first place, you'll get fewer accidental null pointer exceptions. > That sounds very interesting, but I'm only writing a BTree to then use > it to play with "persistent data structures" But you have implemented a mutable tree. If you want to write a persistent one, then write a persistent one! You also use a wishful heuristic in the hope of keeping the tree balanced--if you want a balanced tree, why not use one that's guaranteed to stay in balance? AVL trees are pretty easy to implement; maybe you could try writing a persistent one: http://en.wikipedia.org/wiki/AVL_tree > In this particular case it's somewhat more elegant (IMO) to check "is > None". Well, I can't understand why that might be, but it's surely subjective, so ok. > > I'm sorry but I find that class rather ugly. ?The code would be a lot > > smaller and have fewer corner cases with a different data representation. > > Um, thanks? Seriously though, care to put your money where your mouth > is? I'd be interested to see a BTree implemented as you indicated above. Er, I'm not trying to argue with you. You asked for people's advice and impressions, so I gave you some. I don't feel like rewriting that whole class, but I'll do a method or two, using [] to represent an empty node. (Hmm, actually changing the empty node representation did less to shorten the code than just getting rid of a bunch of duplication. Really, I'd tend to look for a slightly different tree structure which tried to avoid these issues). Untested: class BinaryTree: def __init__(self, key, value): self.key = key self.value = value self.higher = self.lower = [] def get(self, key): if key == self.key: return self.value branch = self.lower if key < self.key else self.higher return branch.get(key) if branch else [] def insert(self, key, value): def xinsert(xtree): # xtree is either a tree or [] if not xtree: xtree = BinaryTree(key, value) else: xtree.insert(key, value) return xtree if key == self.key: self.value = value elif key < self.key: self.lower = xinsert(self.lower) else: self.higher = xinsert(self.higher) and so forth. From http Fri Jul 3 00:58:22 2009 From: http (Paul Rubin) Date: 02 Jul 2009 21:58:22 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: <7xy6r6fjm9.fsf@ruckus.brouhaha.com> "Pablo Torres N." writes: > def split(seq, func=bool): > t = filter(func, seq) > f = filter(lambda x: not func(x), seq) > return list(t), list(f) That is icky--you're calling func (which might be slow) twice instead of once on every element of the seq. From lie.1296 at gmail.com Fri Jul 3 02:05:06 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:05:06 GMT Subject: question of style In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Tim Harig wrote: >> Speaking only to the style issue, when I've wanted to do something like >> that, I find: >> if self.higher is None is self.lower: >> more readable, by making clear they are both being compared to a >> constant, rather than compared to each other. > > By comparing them to *any* constant with 'is' you end up with the same > potential problems as to whether something with a value of None is actually > None? Oh really? None is a Singleton and all None always point to the same None object. And the fact that it is impossible to override `is` operator is the reason for the "is None" idiom. This does not happen with other objects though (except if you make them as Singletons as well), and in case of immutable, you may get bugs due to python's optimizing small integers. > With equality operators > everything is either the same or it isn't. How about: class Fake(object): def __eq__(self, other): return True we uses `is` when we want to make sure an object is really the same object as another, not just an equivalent object. I can start from the first > variable and compare it to the second then to the third then ... In the > case of the inequality, I have to start with the first and compare it to > all of the rest to make sure that none are the same. Then I have to do the > same for each with the remaining. This is much more difficult to do in my > head. I requires much more thought, better left to a computer, when > evaluating the expression for myself. Therefore, equalites are quick and > easy to keep straight whereas any inequalites are more error prone when > trying to evaluate how a section of code will react under different > circumstances. > >> I do draw the line at two, though, and with three or more I'll >> paren-up a list of parallel comparisons: > > As long as everything is an equality, then I don't mind comparing to the > end of an 70 column line. Mixed types of equalities or inequalities > require too many operations mentally evaluate safely. Personally, I'd only chain inequality for something like this: if 0 < x < 10: From lie.1296 at gmail.com Fri Jul 3 02:09:31 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:09:31 GMT Subject: question of style In-Reply-To: <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> Message-ID: Simon Forman wrote: > On Jul 2, 3:57 pm, Scott David Daniels wrote: >> Duncan Booth wrote: >>> Simon Forman wrote: >>>> ... >>>> if self.higher is self.lower is None: return >>>> ... >>> As a matter of style however I wouldn't use the shorthand to run two 'is' >>> comparisons together, I'd write that out in full if it was actually needed >>> here. >> Speaking only to the style issue, when I've wanted to do something like >> that, I find: >> >> if self.higher is None is self.lower: >> ... >> >> more readable, by making clear they are both being compared to a >> constant, rather than compared to each other. > > I was going to do it that way for aesthetics if nothing else, but in > this particular code "self.higher is self.lower" could only have been > True if they were both None, so the final "is None" was redundant and > I only included it as a "just-in-case" check in case someone someday > used the code in such a way as to assign the same object (not None) to > both self.higher and self.lower... Totally pointless, I'm sorry to > say. I'm glad the whole line was redundant really. > I say, you should keep the `is None` for readability and to safe guard against immutable optimization and/or Singleton objects. Due to an implementation detail, python does not guarantee that two immutable object will return different objects; you have: >>> a = 1 >>> b = 1 >>> a is b True From schickb at gmail.com Fri Jul 3 02:31:18 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 23:31:18 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> On Jul 2, 9:40?pm, "Pablo Torres N." wrote: > > If it is speed that we are after, it's my understanding that map and > filter are faster than iterating with the for statement (and also > faster than list comprehensions). ?So here is a rewrite: > > def split(seq, func=bool): > ? ? ? ? t = filter(func, seq) > ? ? ? ? f = filter(lambda x: not func(x), seq) > ? ? ? ? return list(t), list(f) > In my simple tests, that takes 1.8x as long as the original solution. Better than the itertools solution, when "func" is short and fast. I think the solution here would worse if func was more complex. Either way, what I am still wondering is if people would find a built- in implementation useful? -Brad From gagsl-py2 at yahoo.com.ar Fri Jul 3 02:31:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Jul 2009 03:31:42 -0300 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <7xy6r6fjm9.fsf@ruckus.brouhaha.com> Message-ID: En Fri, 03 Jul 2009 01:58:22 -0300, > escribi?: > "Pablo Torres N." writes: >> def split(seq, func=bool): >> t = filter(func, seq) >> f = filter(lambda x: not func(x), seq) >> return list(t), list(f) > > That is icky--you're calling func (which might be slow) twice instead > of once on every element of the seq. In addition, this doesn't work if seq is an iterator instead of a sequence. -- Gabriel Genellina From lie.1296 at gmail.com Fri Jul 3 02:33:03 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:33:03 GMT Subject: question of style In-Reply-To: <7x63eahp8z.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x63eahp8z.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Generally, having a special > value like None to denote a missing datum was considered standard > practice a few decades ago, I guess in python, None as the missing datum idiom is still quite prevalent: def cat_list(a=None, b=None): # poor man's list concatenation if a is None and b is None: return [] if a is None: return b if b is None: return a return a + b From schickb at gmail.com Fri Jul 3 02:34:21 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 23:34:21 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: On Jul 2, 8:17?pm, "Pablo Torres N." wrote: > > This sounds like it belongs to the python-ideas list. ?I suggest > posting there for better feedback, since the core developers check > that list more often than this one. I tried posting on python-ideas and received a "You are not allowed to post to this mailing list" reply. Perhaps because I am posting through Google groups? Or maybe one must be an approved member to post? -Brad From lie.1296 at gmail.com Fri Jul 3 02:37:32 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:37:32 GMT Subject: question of style In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Lie Ryan wrote: > Tim Harig wrote: >>> Speaking only to the style issue, when I've wanted to do something like >>> that, I find: >>> if self.higher is None is self.lower: >>> more readable, by making clear they are both being compared to a >>> constant, rather than compared to each other. >> By comparing them to *any* constant with 'is' you end up with the same >> potential problems as to whether something with a value of None is actually >> None? > > Oh really? None is a Singleton and all None always point to the same > None object. And the fact that it is impossible to override `is` > operator is the reason for the "is None" idiom. > http://docs.python.org/c-api/none.html From aotto1968 at users.sourceforge.net Fri Jul 3 02:39:17 2009 From: aotto1968 at users.sourceforge.net (Andreas Otto) Date: Fri, 03 Jul 2009 08:39:17 +0200 Subject: ANNOUNCE: libmsgque 3.4 Message-ID: Dear Users, I would like to provide a new !! Major Feature Release !! of >>> LibMsgque (3.4) <<< This Release Introduce a new technology called >>> Master / Slave - Link <<< and is used to create, mange and configure a MESH of node's (process or thread) distributed over multiple hosts. But a picture says more then all the words, read more at: -> http://libmsgque.sourceforge.net/master_versa_slave.htm To get the Details and the Downloads read more at: -> http://libmsgque.sourceforge.net/ P.S. sourceforge introduce a new interface yesterday .... and this has still some bugs ... to get the release 3.4 files you have to use the files link. mfg Andreas Otto From lie.1296 at gmail.com Fri Jul 3 02:40:19 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:40:19 GMT Subject: question of style In-Reply-To: <7xljn6pvgk.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Tim Harig writes: >> That being the case, it might be a good idea either to handle the situation >> and raise an exception or add: >> >> assert self.lower <= self.higher >> >> That way an exception will be raised if there is an error somewhere else >> in the code rather then silently passing a possibly incorrect value. > > Well, that assert is not right because you have to handle the case > where one of the values is None. The general sentiment is reasonable > though, if you're concerned that the precondition may not be valid. Well, it depends on where you put the assert statement. If your code is like this: if self.higher is self.lower is None: return if self.lower is None: return self.higher if self.higher is None: return self.lower assert self.lower <= self.higher then the assert statement is correct. Some people might prefer to keep all asserts at the top of function though. From ricli85 at gmail.com Fri Jul 3 02:46:46 2009 From: ricli85 at gmail.com (Rickard Lindberg) Date: Fri, 3 Jul 2009 08:46:46 +0200 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: <890d8ab70907022346r635b00dfr5001e65838a1b994@mail.gmail.com> > I tried posting on python-ideas and received a "You are not allowed to > post to this mailing list" reply. Perhaps because I am posting through > Google groups? Or maybe one must be an approved member to post? If you got an "awaiting moderator approval" message you post might appear on the list soon. The reason for getting those can be that it is a member only list and you posted from another address. I am not sure if that was the message you got. -- Rickard Lindberg From lie.1296 at gmail.com Fri Jul 3 02:47:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:47:49 GMT Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? If self is an object (and it must have been), it would be preferrable to set self.higher and self.lower to a known value inside self.__init__ and avoid having the comparison. I'm guessing you have something like this in your __init__: def __init__(self, lower=None, higher=None): self.lower = lower self.higher = higher that should be changed to: def __init__(self, lower=None, higher=None): self.lower = lower if lower is not None else higher self.higher = higher if lower is not None else lower or whatever else-value you see is appropriate for the context. From javier.collado at gmail.com Fri Jul 3 02:55:28 2009 From: javier.collado at gmail.com (Javier Collado) Date: Fri, 3 Jul 2009 08:55:28 +0200 Subject: Config files with different types In-Reply-To: References: Message-ID: Hello, Have you considered using something that is already developed? You could take a look at this presentation for an overview of what's available: http://us.pycon.org/2009/conference/schedule/event/5/ Anyway, let me explain that, since I "discovered" it, my favourite format for configuration files is yaml (http://yaml.org/, http://pyyaml.org/). It's easy to read, easy to write, available in different programming languagues, etc. In addition to this, type conversion is already in place so I think it covers your requirement. For example: IIn [1]: import yaml In [2]: yaml.load("""name: person name ...: age: 25 ...: is_programmer: true""") Out[2]: {'age': 25, 'is_programmer': True, 'name': 'person name'} Best regards, Javier 2009/7/2 Zach Hobesh : > Hi all, > > I've written a function that reads a specifically formatted text file > and spits out a dictionary. ?Here's an example: > > config.txt: > > Destination = C:/Destination > Overwrite = True > > > Here's my function that takes 1 argument (text file) > > the_file = open(textfile,'r') > linelist = the_file.read().split('\n') > the_file.close() > configs = {} > for line in linelist: > ? ? ? try: > ? ? ? ? ? ? ?key,value = line.split('=') > ? ? ? ? ? ? ?key.strip() > ? ? ? ? ? ? ?value.strip() > ? ? ? ? ? ? ?key.lower() > ? ? ? ? ? ? ?value.lower() > ? ? ? ? ? ? ?configs[key] = value > > ? ? ? except ValueError: > ? ? ? ? ? ? ?break > > so I call this on my config file, and then I can refer back to any > config in my script like this: > > shutil.move(your_file,configs['destination']) > > which I like because it's very clear and readable. > > So this works great for simple text config files. ?Here's how I want > to improve it: > > I want to be able to look at the value and determine what type it > SHOULD be. ?Right now, configs['overwrite'] = 'true' (a string) when > it might be more useful as a boolean. ?Is there a quick way to do > this? ?I'd also like to able to read '1' as an in, '1.0' as a float, > etc... > > I remember once I saw a script that took a string and tried int(), > float() wrapped in a try except, but I was wondering if there was a > more direct way. > > Thanks in advance, > > Zach > -- > http://mail.python.org/mailman/listinfo/python-list > From hv at tbz-pariv.de Fri Jul 3 03:02:06 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Fri, 03 Jul 2009 09:02:06 +0200 Subject: logging of strings with broken encoding In-Reply-To: <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> Message-ID: <7b5oneF223712U1@mid.individual.net> Stefan Behnel schrieb: > Thomas Guettler wrote: >> My quick fix is this: >> >> class MyFormatter(logging.Formatter): >> def format(self, record): >> msg=logging.Formatter.format(self, record) >> if isinstance(msg, str): >> msg=msg.decode('utf8', 'replace') >> return msg >> >> But I still think handling of non-ascii byte strings should be better. >> A broken logging message is better than none. > > Erm, may I note that this is not a problem in the logging library but in > the code that uses it? I know that my code passes the broken string to the logging module. But maybe I get the non-ascii byte string from a third party (psycopg2 sometime passes latin1 byte strings from postgres in error messages). I like Python very much because "it refused to guess". But in this case, "best effort" is a better approach. It worked in 2.5 and will in py3k. I think it is a bug, that it does not in 2.6. Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From lie.1296 at gmail.com Fri Jul 3 03:16:35 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 07:16:35 GMT Subject: Sequence splitting In-Reply-To: <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: Brad wrote: > On Jul 2, 9:40 pm, "Pablo Torres N." wrote: >> If it is speed that we are after, it's my understanding that map and >> filter are faster than iterating with the for statement (and also >> faster than list comprehensions). So here is a rewrite: >> >> def split(seq, func=bool): >> t = filter(func, seq) >> f = filter(lambda x: not func(x), seq) >> return list(t), list(f) >> > > In my simple tests, that takes 1.8x as long as the original solution. > Better than the itertools solution, when "func" is short and fast. I > think the solution here would worse if func was more complex. > > Either way, what I am still wondering is if people would find a built- > in implementation useful? > > -Brad A built-in/itertools should always try to provide the general solution to be as useful as possible, something like this: def group(seq, func=bool): ret = {} for item in seq: fitem = func(item) try: ret[fitem].append(item) except KeyError: ret[fitem] = [item] return ret definitely won't be faster, but it is a much more general solution. Basically, the function allows you to group sequences based on the result of func(item). It is similar to itertools.groupby() except that this also group non-contiguous items. From lie.1296 at gmail.com Fri Jul 3 03:25:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 07:25:28 GMT Subject: logging of strings with broken encoding In-Reply-To: <7b5oneF223712U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> <7b5oneF223712U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > Stefan Behnel schrieb: >> Thomas Guettler wrote: >>> My quick fix is this: >>> >>> class MyFormatter(logging.Formatter): >>> def format(self, record): >>> msg=logging.Formatter.format(self, record) >>> if isinstance(msg, str): >>> msg=msg.decode('utf8', 'replace') >>> return msg >>> >>> But I still think handling of non-ascii byte strings should be better. >>> A broken logging message is better than none. >> Erm, may I note that this is not a problem in the logging library but in >> the code that uses it? > > I know that my code passes the broken string to the logging module. But maybe > I get the non-ascii byte string from a third party (psycopg2 sometime passes > latin1 byte strings from postgres in error messages). If the database contains non-ascii byte string, then you could repr() them before logging (repr also adds some niceties such as quotes). I think that's the best solution, unless you want to decode the byte string (which might be an overkill, depending on the situation). > I like Python very much because "it refused to guess". But in this case, "best effort" > is a better approach. One time it refused to guess, then the next time it tries best effort. I don't think Guido liked such inconsistency. > It worked in 2.5 and will in py3k. I think it is a bug, that it does not in 2.6. In python 3.x, the default string is unicode string. If it works in python 2.5, then it is a bug in 2.5 From http Fri Jul 3 03:27:54 2009 From: http (Paul Rubin) Date: 03 Jul 2009 00:27:54 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x63eahp8z.fsf@ruckus.brouhaha.com> Message-ID: <7x8wj6usxx.fsf@ruckus.brouhaha.com> Lie Ryan writes: > I guess in python, None as the missing datum idiom is still quite prevalent: Well, sometimes there is no way around it, but: > def cat_list(a=None, b=None): > # poor man's list concatenation > if a is None and b is None: return [] > if a is None: return b > if b is None: return a > return a + b def cat_list(a=[], b=[]): return a + b From nillgump at gmail.com Fri Jul 3 03:31:27 2009 From: nillgump at gmail.com (nillgump) Date: Fri, 3 Jul 2009 00:31:27 -0700 (PDT) Subject: use python to edit pdf document Message-ID: <7094d63e-21a6-4b1a-995a-280b0b1b26c4@q40g2000prh.googlegroups.com> hi all. I are looking for some packages which use python to edit pdf format docutment. when I searched the internet,I found the paper " Using Python as PDF Editing and Processing Framework" which is at: *********http://www.python.org/workshops/2002-02/papers/17/ index.htm********* this paper is posted at 2002.it had a long years.But I cann't find the impletation. Does anyone know what happened about this?And I have seen the Reportlab which can generate the pdf .But what I want is not only this,but also can edit the pdf. So does everyone know there are some exsiting python packages about this? With using the exsiting things,I can save much work. Any advice is appropriated! --nillgump --2009/7/3 --China From lie.1296 at gmail.com Fri Jul 3 03:31:45 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 07:31:45 GMT Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: Rickard Lindberg wrote: >> I tried posting on python-ideas and received a "You are not allowed to >> post to this mailing list" reply. Perhaps because I am posting through >> Google groups? Or maybe one must be an approved member to post? > > If you got an "awaiting moderator approval" message you post might appear on > the list soon. The reason for getting those can be that it is a member only > list and you posted from another address. I am not sure if that was the message > you got. > AFAIK, python-ideas is not moderated (I followed python-ideas). I've never used Google Groups to access it though. Try subscribing to the mailing list directly (instead of using Google Group's web-gateway) here: http://mail.python.org/mailman/listinfo/python-ideas From clp2 at rebertia.com Fri Jul 3 03:44:43 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 00:44:43 -0700 Subject: use python to edit pdf document In-Reply-To: <7094d63e-21a6-4b1a-995a-280b0b1b26c4@q40g2000prh.googlegroups.com> References: <7094d63e-21a6-4b1a-995a-280b0b1b26c4@q40g2000prh.googlegroups.com> Message-ID: <50697b2c0907030044p740e0e96h3f1ac9a81b578d57@mail.gmail.com> On Fri, Jul 3, 2009 at 12:31 AM, nillgump wrote: > hi all. > I ?are looking for some packages which use python to edit pdf format > docutment. > when I searched the internet,I found ?the > paper ?" Using Python as PDF Editing and Processing Framework" ?which > is at: > *********http://www.python.org/workshops/2002-02/papers/17/ > index.htm********* > this paper is posted at ?2002.it ?had a ?long ?years.But I cann't > find ?the impletation. > Does anyone know what ?happened ?about > this?And ?I ?have seen the Reportlab ?which can generate the pdf .But > what I ?want is not only this,but also can edit the pdf. See ReportLab's proprietary+commercial PageCatcher library (http://developer.reportlab.com/). Cheers, Chris -- http://blog.rebertia.com From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 03:50:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 07:50:53 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> Message-ID: <025da9f8$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 21:56:40 -0700, Paul Rubin wrote: >> Well I wouldn't know, I've been fortunate enough to program mostly in >> python for over half a decade now and None and 0 are as close as I've >> gotten to NULL in a long time. > > Right, and how many times have you had to debug > > AttributeError: 'NoneType' object has no attribute 'foo' Hardly ever, and mostly when I do something silly like: another_list = alist.sort() Of course, YMMV. > or the equivalent null pointer exceptions in Java, C, or whatever? They > are very common. And the basic idea is that if you avoid using null > pointers in the first place, you'll get fewer accidental null pointer > exceptions. And some other error instead, due to the bugs in your more complicated code *wink* -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 03:57:02 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 07:57:02 GMT Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: <025dab69$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 22:10:14 -0500, Pablo Torres N. wrote: > This sounds like it belongs to the python-ideas list. I suggest posting > there for better feedback, since the core developers check that list > more often than this one. If you post to python-ideas, you'll probably be told to gather feedback here first. The core-developers aren't hugely interested in arbitrary new features unless they have significant community support. I've never needed such a split function, and I don't like the name, and the functionality isn't general enough. I'd prefer something which splits the input sequence into as many sublists as necessary, according to the output of the key function. Something like itertools.groupby(), except it runs through the entire sequence and collates all the elements with identical keys. E.g.: splitby(range(10), lambda n: n%3) => [ (0, [0, 3, 6, 9]), (1, [1, 4, 7]), (2, [2, 5, 8]) ] Your split() would be nearly equivalent to this with a key function that returns a Boolean. -- Steven From http Fri Jul 3 04:02:56 2009 From: http (Paul Rubin) Date: 03 Jul 2009 01:02:56 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7x1voy5h3j.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > I've never needed such a split function, and I don't like the name, and > the functionality isn't general enough. I'd prefer something which splits > the input sequence into as many sublists as necessary, according to the > output of the key function. Something like itertools.groupby(), except it > runs through the entire sequence and collates all the elements with > identical keys. No really, groupby makes iterators, not lists, and it you have to develop quite a delicate sense of when you can use it without having bugs caused by the different iterators it makes getting advanced at the wrong times. The concept of a split function that actually works on lists is useful. I'm neutral about whether it's worth having a C version in the stdlib. From lie.1296 at gmail.com Fri Jul 3 04:06:18 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 08:06:18 GMT Subject: question of style In-Reply-To: <7x8wj6usxx.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x63eahp8z.fsf@ruckus.brouhaha.com> <7x8wj6usxx.fsf@ruckus.brouhaha.com> Message-ID: <_Zi3m.2347$ze1.458@news-server.bigpond.net.au> Paul Rubin wrote: > Lie Ryan writes: >> I guess in python, None as the missing datum idiom is still quite prevalent: > > Well, sometimes there is no way around it, but: > >> def cat_list(a=None, b=None): >> # poor man's list concatenation >> if a is None and b is None: return [] >> if a is None: return b >> if b is None: return a >> return a + b > > def cat_list(a=[], b=[]): > return a + b Being super contrived is why I tagged it as poor man's concat. Generally, there will be a processing: ... if b is None: return a # processing here return retlist ... and it will not be about concatenating two lists, but something more complex. But I thought that was unnecessary since I just want to mention about the None argument idiom. From stef.mientki at gmail.com Fri Jul 3 04:07:50 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Fri, 03 Jul 2009 10:07:50 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> References: <4A4BC22D.70502@gmail.com> <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> Message-ID: <4A4DBC56.9020202@gmail.com> ryles wrote: > On Jul 2, 1:25 am, Terry Reedy wrote: > >>> The next statement works, >>> but I'm not sure if it will have any dramatical side effects, >>> other than overruling a possible object with the name A >>> >>> def some_function ( ...) : >>> A = object ( ...) >>> sys._getframe(1).f_globals [ Name ] = A >>> >> global name >> name = A >> >> or is name is a string var >> globals()[name] = A >> > > It wasn't explicit, but I think Stef meant that the global should be > added to the caller's environment, which was the reason for > sys._getframe(). > > Is this environment only intended for interactive use? I wonder if you > might just set things up > in a PYTHONSTARTUP script instead. > the idea is to get a simple environment where you can do interactive 3D geometry, without knowing anything about Python. So to create a triangle e.g., the whole program will look like this: Point ( 'A', (1,1,0) ) Point ( 'B', (5,5,0) ) Point ( 'C', (1,5,0) ) Line_Segment ( 'AB' ) Line_Segment ( 'BC' ) Line_Segment ( 'AC' ) And now the points A,B,C and the line segments AB,BC,AC also exists as variables in the namespace of the above environment. So now you can add a point "D" in the middle of the line-segment AB, by giving the formula of that point: Point ( 'D', ' ( A + B ) / 2 ' ) Or display the properties of point A, with : Print A which (for the moment) will result in: Point Instance: A Childs : AB(Line-Segment), AC(Line-Segment), Parents: The graphics above is done in VPython, which gives you an easy way to make all objects dragable, and if objects are defined by formulas, the will stick together according to that formula. And now it's obvious I can't use the solution of Terry, because I don't know what names will to become global. thanks, Stef -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: moz-screenshot-3.jpg Type: image/jpeg Size: 3333 bytes Desc: not available URL: From ldo at geek-central.gen.new_zealand Fri Jul 3 04:08:20 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 03 Jul 2009 20:08:20 +1200 Subject: No trees in the stdlib? References: Message-ID: In message , Jo?o Valverde wrote: > Lawrence D'Oliveiro wrote: > >> In message , Jo?o >> Valverde wrote: >> >>> Simple example usage case: Insert string into data structure in sorted >>> order if it doesn't exist, else retrieve it. >> >> the_set = set( ... ) >> >> if str in the_set : >> ... "retrieval" case ... >> else : >> the_set.add(str) >> #end if >> >> Want sorted order? >> >> sorted(tuple(the_set)) >> >> What could be simpler? > > Try putting that inside a loop with thousands of iterations and you'll > see what the problem is. You could apply the same argument to anything. E.g. why create a tree structure with a million elements? Try putting that inside a loop with thousands of iterations and you'll see what the problem is. From deets at nospam.web.de Fri Jul 3 04:11:02 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 03 Jul 2009 10:11:02 +0200 Subject: XML(JSON?)-over-HTTP: How to define API? In-Reply-To: References: Message-ID: <7b5somF21peceU1@mid.uni-berlin.de> Allen Fowler schrieb: > > > >> I have an (in-development) python system that needs to shuttle events / requests >> around over the network to other parts of itself. It will also need to >> cooperate with a .net application running on yet a different machine. >> >> So, naturally I figured some sort of HTTP event / RPC type of would be a good >> idea? >> >> Are there any modules I should know about, or guidelines I could read, that >> could aid me in the design of the API? >> >> > > > To clarify: > > Each message would be <1KB of data total, and consist of some structured object containing strings, numbers, dates, etc. > > For instance there would be an "add user" request that would contain one or more User objects each having a number of properties like: > > - Full Name > - Username > - Password > - Email addresses (a variable length array) > - Street Address line1 > - Street Address line1 > - Street Address line1 > - City > - State > - Zip > - Sign Up Date > > .... and so on. > > > Since I need to work with other platforms, pickle is out... what are the alternatives? XML? JSON? > > How should I formally define each of the valid messages and objects? > > Thank you, Use XMLRPC. Implementations for both languages are available. There is no need for formal spec - which is a good thing. You just call the server, and it works. Diez From clp2 at rebertia.com Fri Jul 3 04:12:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 01:12:20 -0700 Subject: Sequence splitting In-Reply-To: <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: <50697b2c0907030112g38a3c992tb07eea45acc58312@mail.gmail.com> On Thu, Jul 2, 2009 at 11:31 PM, Brad wrote: > On Jul 2, 9:40?pm, "Pablo Torres N." wrote: >> >> If it is speed that we are after, it's my understanding that map and >> filter are faster than iterating with the for statement (and also >> faster than list comprehensions). ?So here is a rewrite: >> >> def split(seq, func=bool): >> ? ? ? ? t = filter(func, seq) >> ? ? ? ? f = filter(lambda x: not func(x), seq) >> ? ? ? ? return list(t), list(f) >> > > In my simple tests, that takes 1.8x as long as the original solution. > Better than the itertools solution, when "func" is short and fast. I > think the solution here would worse if func was more complex. > > Either way, what I am still wondering is if people would find a built- > in implementation useful? FWIW, Ruby has Enumerable#partition, which does the same thing as split() and has a better name IMHO. http://www.ruby-doc.org/core/classes/Enumerable.html#M003130 Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Fri Jul 3 04:15:35 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 01:15:35 -0700 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: <4A4DBC56.9020202@gmail.com> References: <4A4BC22D.70502@gmail.com> <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> <4A4DBC56.9020202@gmail.com> Message-ID: <50697b2c0907030115l5aab4816i159eb22d39785f0f@mail.gmail.com> On Fri, Jul 3, 2009 at 1:07 AM, Stef Mientki wrote: > ryles wrote: > > On Jul 2, 1:25 am, Terry Reedy wrote: > > > The next statement works, > but I'm not sure if it will have any dramatical side effects, > other than overruling a possible object with the name A > > > def some_function ( ...) : > A = object ( ...) > sys._getframe(1).f_globals [ Name ] = A > > > global name > name = A > > or is name is a string var > globals()[name] = A > > > It wasn't explicit, but I think Stef meant that the global should be > added to the caller's environment, which was the reason for > sys._getframe(). > > Is this environment only intended for interactive use? I wonder if you > might just set things up > in a PYTHONSTARTUP script instead. > > > the idea is to get a simple environment where you can do interactive 3D > geometry, > without knowing anything about Python. > So to create a triangle e.g., the whole program will look like this: > > Point ( 'A', (1,1,0) ) > Point ( 'B', (5,5,0) ) > Point ( 'C', (1,5,0) ) > Line_Segment ( 'AB' ) > Line_Segment ( 'BC' ) > Line_Segment ( 'AC' ) > > > > And now the points A,B,C and the line segments AB,BC,AC also exists as > variables in the namespace of the above environment. > So now you can add a point "D" in the middle of the line-segment AB, by > giving the formula of that point: > > Point ( 'D',? ' ( A + B ) / 2 ' ) > > Or display the properties of point A, with : > > Print A > > which (for the moment) will result in: > > Point Instance: A > ? Childs : AB(Line-Segment), AC(Line-Segment), > ? Parents: > > The graphics above is done in VPython, which gives you an easy way to make > all objects dragable, > and if objects are defined by formulas, the will stick together according to > that formula. > > And now it's obvious I can't use the solution of Terry, > because I don't know what names will to become global. Have you considered just using imports instead? Requiring a single opaque: from graphicslibthingy import * at the start of a file using the environment isn't so bad, IMHO. Cheers, Chris -- http://blog.rebertia.com From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:18:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:18:19 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> Message-ID: <025db066$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 22:14:18 +0000, Tim Harig wrote: > On 2009-07-02, Paul Rubin wrote: >> Tim Harig writes: >>> If lower is 5 and higher is 3, then it returns 3 because 3 != None in >>> the first if. >> Sorry, the presumption was that lower <= higher, i.e. the comparison >> had already been made and the invariant was enforced by the class >> constructor. The comment should have been more explicit, I guess. > > That being the case, it might be a good idea either to handle the > situation and raise an exception or add: > > assert self.lower <= self.higher Only if you want strange and mysterious bugs whenever somebody runs your code with the -O flag. > That way an exception will be raised if there is an error somewhere else > in the code rather then silently passing a possibly incorrect value. asserts are disabled when the -O flag is given. You should never use asserts for testing data. That's not what they're for. They're for testing program logic (and unit-testing). This is wrong, because it will sometimes fail when running under -O: def parrot(colour): assert colour.lower() in ('red', 'green', 'blue') return "Norwegian %s" % colour.title() That should be written as test followed by an explicit raise: def parrot(colour): if colour.lower() not in ('red', 'green', 'blue'): raise ValueError return "Norwegian %s" % colour.title() An acceptable use for assert is to verify your program logic by testing something which should never fail. If it does fail, then you know something bizarre and unexpected has happened, and you have a program bug rather than bad data. Here's a silly example: def cheese_available(kind): """Return True if the kind of cheese specified is in stock in the cheeseshop run by Michael Palin. This function always returns False. """ return False def get_cheese(): exchange_amusing_banter() for kind in ('Cheddar', 'Gouda', 'Swiss', 'Camembert'): ask_proprietor("Do you have any %s?" % kind) flag = cheese_available(kind) assert not flag if flag: buy_cheese(kind) return None else: express_disappointment() # We only get here if no cheese is bought. print "Cleese: Does this cheeseshop actually have ANY cheese?" print "Palin: No sir, I'm afraid I've been wasting your time." print "Cleese: Well then, I'm going to have to shoot you." -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:19:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:19:23 GMT Subject: Why re.match()? References: Message-ID: <025db0a6$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: > I'm sure that it is possible to find cases in which the *current* > implementation of re.search() would be inefficient, but that's because > this implementation is perverse, which, I guess, is ultimately the point > of my original post. Why privilege the special case of a > start-of-string anchor? Because wanting to see if a string matches from the beginning is a very important and common special case. > What if you wanted to apply an end-anchored > pattern to some prefix of your 4GB string? Why not have a special re > method for that? And another for every possible special case? Because they're not common special cases. They're rare and not special enough to justify special code. > If the concern is efficiency for such cases, then simply implement > optional offset and length parameters for re.search(), to specify any > arbitrary substring to apply the search to. To have a special-case > re.match() method in addition to a general re.search() method is > antithetical to language minimalism, and plain-old bizarre. Maybe > there's a really good reason for it, but it has not been mentioned yet. There is, and it has. You're welcome to keep your own opinion, but I don't think you'll find many experienced Python coders will agree with it. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:21:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:21:09 GMT Subject: PEP368 and pixeliterators References: Message-ID: <025db110$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 10:32:04 +0200, Joachim Str?mbergson wrote: > for pixel in rgb_image: > # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, > pixel.r > > > The idea I'm having is that fundamentally the image is made up of a 2D > array of pixels, not rows of pixels. A 2D array implies rows (and columns) of pixels. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:26:59 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:26:59 GMT Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> Message-ID: <025db26e$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 01:02:56 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> I've never needed such a split function, and I don't like the name, and >> the functionality isn't general enough. I'd prefer something which >> splits the input sequence into as many sublists as necessary, according >> to the output of the key function. Something like itertools.groupby(), >> except it runs through the entire sequence and collates all the >> elements with identical keys. > > No really, groupby makes iterators, not lists, and it you have to > develop quite a delicate sense of when you can use it without having > bugs caused by the different iterators it makes getting advanced at the > wrong times. The concept of a split function that actually works on > lists is useful. I'm neutral about whether it's worth having a C > version in the stdlib. groupby() works on lists. The difference between what I'm suggesting and what groupby() does is that my suggestion would collate *all* the elements with the same key, not just runs of them. This (as far as I can tell) requires returning lists rather than iterators. The most important difference between my suggestion and that of the OP is that he limited the key function to something which returns a truth value, while I'm looking for something more general which can split the input into an arbitrary number of collated sublists. -- Steven From nick at craig-wood.com Fri Jul 3 04:30:02 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Fri, 03 Jul 2009 03:30:02 -0500 Subject: how to spawn a process under different user References: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> Message-ID: Gabriel Genellina wrote: > En Thu, 02 Jul 2009 19:27:05 -0300, Tim Harig > escribi?: > > On 2009-07-02, sanket wrote: > >>> sanket wrote: > >>> > I am trying to use python's subprocess module to launch a process. > >>> > but in order to do that I have to change the user. > >> I am using python 2.4 on centos. > > > > I have never done this in python; but, using the normal system calls in C > > the process is basically: > > > > 1. fork() a new process > > 2. the child process changes its user id with setreuid() and > > possibly its group id with setregid() > > 3. then the child exec()s new process which replaces itself > > > > All of the necessary functions should be under the os module on POSIX > > operating systems. > > How to do that using the subprocess module: write a function for item (2) > above and pass it as the preexec_fn argument to Popen. preexec_fn is > executed after fork() and before exec() If you are forking 100s of processes a second you want to use the method above, however I think it is easier to use su (assuming you start off as root), so instead of passing ['mycommand', 'my_arg1', 'my_arg2'] to Popen, pass ['su', '-', 'username', '-c', 'mycommand my_arg1 my_arg2'] There is some opportunity for quoting problems there, but it is easy! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From http Fri Jul 3 04:33:27 2009 From: http (Paul Rubin) Date: 03 Jul 2009 01:33:27 -0700 Subject: No trees in the stdlib? References: Message-ID: <7xocs2go88.fsf@ruckus.brouhaha.com> Lawrence D'Oliveiro writes: > >> Want sorted order? > >> sorted(tuple(the_set)) > >> What could be simpler? > > > > Try putting that inside a loop with thousands of iterations and you'll > > see what the problem is. > > You could apply the same argument to anything. E.g. why create a tree > structure with a million elements? Try putting that inside a loop with > thousands of iterations and you'll see what the problem is. The idea is you're going to insert or delete something in the tree structure at each iteration, an O(log n) operation, making the whole loop O(n log n). If you sort a set (which is O(n log n)) inside the loop then you end up with O(n**2 log n) which is impractical. A reason you might sort inside the loop might be to find the nearby neighbors of the new element or traverse some elements in the middle. This is trivial with a tree structure but messy with something like sets. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:39:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:39:19 GMT Subject: No trees in the stdlib? References: Message-ID: <025db552$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 20:08:20 +1200, Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > >> Lawrence D'Oliveiro wrote: [...] >>> Want sorted order? >>> >>> sorted(tuple(the_set)) >>> >>> What could be simpler? >> >> Try putting that inside a loop with thousands of iterations and you'll >> see what the problem is. > > You could apply the same argument to anything. E.g. why create a tree > structure with a million elements? Try putting that inside a loop with > thousands of iterations and you'll see what the problem is. The difference is, it's vanishingly rare to want to build a tree with millions of elements thousands of times over and over again, but it is not unreasonable to want to access sorted data thousands of times. Needing to re-sort it over and over and over again is wasteful, slow and stupid. Binary trees were invented, in part, specifically to solve this use-case. -- Steven From http Fri Jul 3 04:39:27 2009 From: http (Paul Rubin) Date: 03 Jul 2009 01:39:27 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xk52qgny8.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > groupby() works on lists. >>> a = [1,3,4,6,7] >>> from itertools import groupby >>> b = groupby(a, lambda x: x%2==1) # split into even and odd >>> c = list(b) >>> print len(c) 3 >>> d = list(c[1][1]) # should be [4,6] >>> print d # oops. [] > The difference between what I'm suggesting and what groupby() does is > that my suggestion would collate *all* the elements with the same key, > not just runs of them. This (as far as I can tell) requires returning > lists rather than iterators. I guess that is reasonable. > The most important difference between my suggestion and that of the OP is > that he limited the key function to something which returns a truth > value, while I'm looking for something more general which can split the > input into an arbitrary number of collated sublists. Also ok. From pyklass at gmail.com Fri Jul 3 04:55:04 2009 From: pyklass at gmail.com (Goksie Aruna) Date: Fri, 03 Jul 2009 09:55:04 +0100 Subject: is it possible to write USSD / SMS /SS7 apps in python Message-ID: <4A4DC768.40604@gmail.com> Can someone give me an insight into these? developing ss7 or USSD or SMS apps in python. is there any existing ones in this manner? Thanks From tsangpo.newsgroup at gmail.com Fri Jul 3 05:03:51 2009 From: tsangpo.newsgroup at gmail.com (tsangpo) Date: Fri, 3 Jul 2009 17:03:51 +0800 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: Just a shorter implementation: from itertools import groupby def split(lst, func): gs = groupby(lst, func) return list(gs[True]), list(gs[False]) "Lie Ryan" ????????????:nfi3m.2341$ze1.1151 at news-server.bigpond.net.au... > Brad wrote: >> On Jul 2, 9:40 pm, "Pablo Torres N." wrote: >>> If it is speed that we are after, it's my understanding that map and >>> filter are faster than iterating with the for statement (and also >>> faster than list comprehensions). So here is a rewrite: >>> >>> def split(seq, func=bool): >>> t = filter(func, seq) >>> f = filter(lambda x: not func(x), seq) >>> return list(t), list(f) >>> >> >> In my simple tests, that takes 1.8x as long as the original solution. >> Better than the itertools solution, when "func" is short and fast. I >> think the solution here would worse if func was more complex. >> >> Either way, what I am still wondering is if people would find a built- >> in implementation useful? >> >> -Brad > > A built-in/itertools should always try to provide the general solution > to be as useful as possible, something like this: > > def group(seq, func=bool): > ret = {} > for item in seq: > fitem = func(item) > try: > ret[fitem].append(item) > except KeyError: > ret[fitem] = [item] > return ret > > definitely won't be faster, but it is a much more general solution. > Basically, the function allows you to group sequences based on the > result of func(item). It is similar to itertools.groupby() except that > this also group non-contiguous items. From usernet at ilthio.net Fri Jul 3 05:07:30 2009 From: usernet at ilthio.net (Tim Harig) Date: Fri, 03 Jul 2009 09:07:30 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <025db066$0$20657$c3e8da3@news.astraweb.com> Message-ID: On 2009-07-03, Steven D'Aprano wrote: > On Thu, 02 Jul 2009 22:14:18 +0000, Tim Harig wrote: >> On 2009-07-02, Paul Rubin wrote: >>> Tim Harig writes: >>>> If lower is 5 and higher is 3, then it returns 3 because 3 != None in >>>> the first if. >>> Sorry, the presumption was that lower <= higher, i.e. the comparison >>> had already been made and the invariant was enforced by the class >>> constructor. The comment should have been more explicit, I guess. >> That being the case, it might be a good idea either to handle the >> situation and raise an exception or add: >> assert self.lower <= self.higher > Only if you want strange and mysterious bugs whenever somebody runs your > code with the -O flag. The point here is that Rubin presumed that the condition where lower > higher should never exist. Therefore, because the program given segment of program doesn't test it elswhere, it is possible that a bug in the code that sets lower > higher could go unoticed while silently passing the wrong data. Therefore, It is better to test that assumption in a way that *will* crash if something is wrong, for instance, if another method accidently changes one of the values. That would tend to make errors in another method of the class (or even higher up in this method), more likely to be found. > asserts are disabled when the -O flag is given. Right, like defining NDEBUG in C. In _Writing Solid Code_ by Steve Maguire, he likens it to having two pieces of code: one for testing where any errors should cause crashes as early as possible and one for shipping where it may be better to handle errors if possible from within the code. > You should never use asserts for testing data. That's not what they're > for. They're for testing program logic (and unit-testing). What I am suggesting here is exactly that. If lower is always defined such that it should always be equal to or lower then higher, then there is an error in the code somewhere if lower is higher by the time this code is called. Either the constructor didn't didn't reject input properly or another method within the class has inadvertantly changed higher or lower in an uncorrect way. In any event, whether I am using it 100% correctly, I want to find any errors in my code. If there is an error, I want the program to crash during testing rather then silently passing bad data. assert will help here. Unfortunatly, Rubin is right. I did make the mistake that the assert will fail with higher or lower values of None, which is allowed here. The same basic premise is correct but will require more complex assertions to allow that through. From mjohnson at cfi.co.ug Fri Jul 3 05:18:04 2009 From: mjohnson at cfi.co.ug (Johnson Mpeirwe) Date: Fri, 03 Jul 2009 12:18:04 +0300 Subject: PSP Caching Message-ID: <4A4DCCCC.6040605@cfi.co.ug> Hello All, How do I stop caching of Python Server Pages (or whatever causes changes in a page not to be noticed in a web browser)? I am new to developing web applications in Python and after looking at implementations of PSP like Spyce (which I believed introduces new unnecessary non-PSP syntax), I decided to write my own PSP applications from scratch. When I modify a file, I keep getting the old results until I intentionally introduce an error (e.g parse error) and correct it after to have the changes noticed. There's no proxy (I am working on a windows machine unplugged from the network). I have Googled and no documents seem to talk about this. Is there any particular mod_python directive I must set in my Apache configuration to fix this? Any help will be highly appreciated. Johnson From clp2 at rebertia.com Fri Jul 3 05:21:33 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 02:21:33 -0700 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <4A4DC768.40604@gmail.com> References: <4A4DC768.40604@gmail.com> Message-ID: <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> On Fri, Jul 3, 2009 at 1:55 AM, Goksie Aruna wrote: > Can someone give me an insight into these? > > ? developing ss7 or USSD or SMS apps in python. > > is there any existing ones in this manner? Advice for the future: STFW. http://pypi.python.org/pypi?%3Aaction=search&term=sms&submit=search Cheers, Chris -- http://blog.rebertia.com From pyklass at gmail.com Fri Jul 3 05:31:38 2009 From: pyklass at gmail.com (Goke Aruna) Date: Fri, 3 Jul 2009 10:31:38 +0100 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> Message-ID: <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> On Fri, Jul 3, 2009 at 10:21 AM, Chris Rebert wrote: > On Fri, Jul 3, 2009 at 1:55 AM, Goksie Aruna wrote: > > Can someone give me an insight into these? > > > > developing ss7 or USSD or SMS apps in python. > > > > is there any existing ones in this manner? > > Advice for the future: STFW. > > http://pypi.python.org/pypi?%3Aaction=search&term=sms&submit=search > > Cheers, > Chris > -- > http://blog.rebertia.com > thanks Chris, however, i have checked all the packages there are specifics for a particular company or device. what am saying is reading the ITU info on USSD, is it possible to use python to write the application SS7 with support for TCAP/MAP talking to E1 card to do the ss7 signalling. Thanks. goksie -------------- next part -------------- An HTML attachment was scrubbed... URL: From kurt.alfred.mueller at gmail.com Fri Jul 3 05:38:48 2009 From: kurt.alfred.mueller at gmail.com (yam850) Date: Fri, 3 Jul 2009 02:38:48 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> On 1 Jul., 21:30, spillz wrote: > On Jun 29, 3:15 pm, Pascal Chambon wrote: > > I've had real issues with subprocesses recently : from a python script, > > on windows, I wanted to "give control" to a command line utility, i.e > > forward user in put to it and display its output on console. It seems > > simple, but I ran into walls : > If you are willing to have a wxPython dependency, wx.Execute handles > non-blockingi/o with processes on all supported platforms I made a python method/function for non blocking read from a file object. I use it in one of my python programs. When looking at the code bear in mind that I am not an expert and I am happy to see comments. #------------------------------------------------------------------ def non_blocking_readline(f_read=sys.stdin, timeout_select=0.0): """to readline non blocking from the file object 'f_read' for 'timeout_select' see module 'select'""" import select text_lines = '' # empty string while True: # as long as there are bytes to read try: # try select rlist, wlist, xlist = select.select([f_read], [], [], timeout_select) except: # select ERROR print >>sys.stderr, ("non_blocking_read select ERROR") break if DEBUG: print("rlist=%s, wlist=%s, xlist=%s" % (repr(rlist), repr(wlist), repr(xlist))) if len(rlist) > 0: text_read = f_read.readline() # get a line if DEBUG: print("after read/readline text_read:'%s', len= %s" % (text_read, repr(len(text_read)))) if len(text_read) > 0: # there were some bytes text_lines = "".join([text_lines, text_read]) if DEBUG: print("text_lines:'%s'" % (text_lines)) else: break # there was no byte in a line else: break # there was no byte in the f_read if text_lines == '': return None else: return text_lines -- Kurt From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 05:50:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 09:50:09 GMT Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> <7xk52qgny8.fsf@ruckus.brouhaha.com> Message-ID: <025dc5ec$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 01:39:27 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> groupby() works on lists. > >>>> a = [1,3,4,6,7] >>>> from itertools import groupby >>>> b = groupby(a, lambda x: x%2==1) # split into even and odd >>>> c = list(b) >>>> print len(c) > 3 >>>> d = list(c[1][1]) # should be [4,6] print d # oops. > [] I didn't say it worked properly *wink* Seriously, this behaviour caught me out too. The problem isn't that the input data is a list, the same problem occurs for arbitrary iterators. >From the docs: [quote] The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function). That behavior differs from SQL?s GROUP BY which aggregates common elements regardless of their input order. The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list [end quote] http://www.python.org/doc/2.6/library/itertools.html#itertools.groupby -- Steven From petshmidt at googlemail.com Fri Jul 3 06:28:25 2009 From: petshmidt at googlemail.com (Tep) Date: Fri, 3 Jul 2009 03:28:25 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> <9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> Message-ID: <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> On 3 Jul., 06:40, Simon Forman wrote: > On Jul 2, 4:31?am, Tep wrote: > > > > > > > On 2 Jul., 10:25, Tep wrote: > > > > On 2 Jul., 01:56, MRAB wrote: > > > > > someone wrote: > > > > > Hello, > > > > > > how can I replace '?' sign from string? Or do split at that character? > > > > > Getting unicode error if I try to do it: > > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > > > > 1: ordinal not in range(128) > > > > > > Thanks, Pet > > > > > > script is # -*- coding: UTF-8 -*- > > > > > It sounds like you're mixing bytestrings with Unicode strings. I can't > > > > be any more helpful because you haven't shown the code. > > > > Oh, I'm sorry. Here it is > > > > def cleanInput(input) > > > ? ? return input.replace('?', '') > > > I also need: > > > #input is html source code, I have problem with only this character > > #input = 'foo ? bar' > > #return should be foo > > def splitInput(input) > > ? ? parts = input.split(' ? ') > > ? ? return parts[0] > > > Thanks! > > Okay people want to help you but you must make it easy for us. > > Post again with a small piece of code that is runnable as-is and that > causes the traceback you're talking about, AND post the complete > traceback too, as-is. > > I just tried a bit of your code above in my interpreter here and it > worked fine: > > |>>> data = 'foo ? bar' > |>>> data.split('?') > |['foo ', ' bar'] > |>>> data = u'foo ? bar' > |>>> data.split(u'?') > |[u'foo ', u' bar'] > > Figure out the smallest piece of "html source code" that causes the > problem and include that with your next post. The problem was, I've converted "html source code" to unicode object and didn't encoded to utf-8 back, before using split... Thanks for help and sorry for not so smart question Pet > > HTH, > ~Simon > > You might also read this:http://catb.org/esr/faqs/smart-questions.html From jeanmichel at sequans.com Fri Jul 3 06:33:45 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 03 Jul 2009 12:33:45 +0200 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <4A4DDE89.1080003@sequans.com> Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > Easier for you but not for those who are familiar with the standard way of python coding. (by standard I don't mean the "only Godly way of coding", there's room for custom rules) But I think most of the people will write: if self.higher is None: return self.lower if self.lower is None: return self.higher # here neither are None > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) > From hkmshb at gmail.com Fri Jul 3 06:46:32 2009 From: hkmshb at gmail.com (Klone) Date: Fri, 3 Jul 2009 03:46:32 -0700 (PDT) Subject: Is code duplication allowed in this instance? Message-ID: Hi all. I believe in programming there is a common consensus to avoid code duplication, I suppose such terms like 'DRY' are meant to back this idea. Anyways, I'm working on a little project and I'm using TDD (still trying to get a hang of the process) and am trying to test the functionality within a method. Whoever it so happens to verify the output from the method I have to employ the same algorithm within the method to do the verification since there is no way I can determine the output before hand. So in this scenario is it OK to duplicate the algorithm to be tested within the test codes or refactor the method such that it can be used within test codes to verify itself(??). From bthate at gmail.com Fri Jul 3 06:50:36 2009 From: bthate at gmail.com (Bart Thate) Date: Fri, 3 Jul 2009 03:50:36 -0700 (PDT) Subject: GOZERBOT 0.9.1 BETA2 released Message-ID: <67b84335-211d-4f7f-b417-71faeb70fbf5@o6g2000yqj.googlegroups.com> GOZERBOT has a new website !! check it out at http://gozerbot.org. This is all in preparation for the 0.9.1 release and the latest GOZERBOT beta has been released as well. Please try this version and let me know how goes. Install is as simple as .. easy_install gozerbot gozerplugs, see README. This release will be used to move GOZERBOT 0.9 into the debian repositories and freebsd ports. we can be contacted on #dunkbots EFNET/IRCNET or use http://dev.gozerbot.org for any bugs you might find. NEW IN THIS RELEASE: * GOZERBOT is now truely free as it no longer depends on GPL licensed xmpppy, a own xmpp package has been implemented for this. * GOZERBOT now depends on setuptools to install the proper packages * gozerbot-nest script can be used to install all dependacies and bot code in 1 directory that can be run by the user (no root required) * morphs are added that allow for encryption of input and output streams (not used yet) ABOUT GOZERBOT: GOZERBOT is a channel bot that aids with conversation in irc channels and jabber conference rooms. its mainly used to serve rss feeds and to have custom commands made for the channel. More then just a channel bot GOZERBOT aims to provide a platform for the user to program his own bot and make it into something thats usefull. This is done with a plugin structure that makes it easy to program your own. But GOZERBOT comes with some batteries included, there are now over 100 plugins already written and ready for use. groet, Bart From p.f.moore at gmail.com Fri Jul 3 07:01:04 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Fri, 3 Jul 2009 12:01:04 +0100 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: <79990c6b0907030401w2e668ac3qd315c751d9e3e924@mail.gmail.com> 2009/7/3 Brad : > Perhaps true, but it would be a nice convenience (for me) as a built- > in written in either Python or C. Although the default case of a bool > function would surely be faster. The chance of getting this accepted as a builtin is essentially zero. To be a builtin, as opposed to being in the standard library, something has to have a very strong justification. This suggestion may find a home in the standard library, although it's not entirely clear where (maybe itertools, although it's not entirely obvious that it's a fit there). You'd have to justify this against the argument "not every 2-3 line function needs to be built in". Personally, I'm not sure it passes that test - sure, it's a useful function, but it's not that hard to write when you need it. It may be better as a recipe in the cookbook. Or if it's close enough to the spirit of the itertools, it may be suitable as a sample in the itertools documentation (the "recipes" section). Paul. From user at example.net Fri Jul 3 07:21:12 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 13:21:12 +0200 Subject: stringio+tarfile In-Reply-To: References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4a4de9a8$0$1105$4fafbaef@reader3.news.tin.it> > First problem I see is all those numbers before the lines. That's not > valid python. > > Assuming that was a transcription error not so. i intentionally add linenumbers to facilitare reference to the code, but if it is a nuisance i will not include them anymore. thanks From tkjthingone at gmail.com Fri Jul 3 07:28:47 2009 From: tkjthingone at gmail.com (Horace Blegg) Date: Fri, 3 Jul 2009 04:28:47 -0700 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> Message-ID: I've been kinda following this. I have a cousin who is permanently wheel chair bound and doesn't have perfect control of her hands, but still manages to use a computer and interact with society. However, the idea/thought of disabled programmers was new to me/hadn't ever occurred to me. You say that using your hands is painful, but what about your feet? Wouldn't it be possible to rig up some kind of foot peddle for shift/caps lock? Kinda like the power peddle used with sowing machines, so the hands are free to hold fabric. I don't mean this in a condescending manor, and I apologize if you take it as such. I'm genuinely curious if you think something like this could work. The way I was envisioning it working last night (and I haven't the faintest clue how SR works, nor have I ever used SR) was that you would hit the foot peddle, which would tell the SR program to capitalize the first letter of the next word (a smart shift, basically, so you don't end up doing something like ... WONderland -or- "stocks are up 1,0))% TOday".) Possible? Stupid? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ricli85 at gmail.com Fri Jul 3 07:29:44 2009 From: ricli85 at gmail.com (Rickard Lindberg) Date: Fri, 3 Jul 2009 13:29:44 +0200 Subject: Is code duplication allowed in this instance? In-Reply-To: References: Message-ID: <890d8ab70907030429i615c4bdat2263ac9555bd8c4e@mail.gmail.com> > Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine > the output before hand. Can you clarify this scenario a bit? If you are performing black-box testing I don't see why you need to use the same algorithm in the test code. But maybe your case is special. On the other hand you can not perform black-box testing if the output is not known for a given input. -- Rickard Lindberg From user at example.net Fri Jul 3 07:29:52 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 13:29:52 +0200 Subject: stringio+tarfile In-Reply-To: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4a4debb1$0$1106$4fafbaef@reader3.news.tin.it> thanks to the guys who bothered to answer me even using their chrystal ball ;-) i'll try to be more specific. yes: i want to create a tar file in memory, and add some content from a memory buffer... my platform: $ uname -a Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux $ python -V Python 2.3.4 following some suggestions i modified the code: $ cat tar001.py import tarfile import StringIO sfo1 = StringIO.StringIO("one\n") sfo2 = StringIO.StringIO("two\n") tfo = StringIO.StringIO() tar = tarfile.open(fileobj=tfo , mode="w") ti = tar.gettarinfo(fileobj=tfo) for sfo in [sfo1 , sfo2]: tar.addfile(fileobj=sfo , tarinfo=ti) print tfo and that's what i get: $ python tar001.py > tar001.out Traceback (most recent call last): File "tar001.py", line 7, in ? ti = tar.gettarinfo(fileobj=tfo) File "/usr/lib/python2.3/tarfile.py", line 1060, in gettarinfo name = fileobj.name AttributeError: StringIO instance has no attribute 'name' can you help? TIA ps: i'd also like that the tar file has names for the buffers, so than once output the file can be untarred with /bin/tar into regular files... From eckhardt at satorlaser.com Fri Jul 3 07:58:44 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Fri, 03 Jul 2009 13:58:44 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: Thanks to all that answered, in particular I wasn't aware of the existence of the __del__ function. For completeness' sake, I think I have found another way to not really solve but at least circumvent the problem: weak references. If I understand correctly, those would allow me to pass out handles to the resources and, if some code decides it is time, release the resources and render all the weak references invalid. At least I don't suffer resource leaks but rather get meaningful errors that way, which is enough for my case. cheers! Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From __peter__ at web.de Fri Jul 3 08:12:38 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 03 Jul 2009 14:12:38 +0200 Subject: stringio+tarfile References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4debb1$0$1106$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > thanks to the guys who bothered to answer me even using their chrystal > ball ;-) > > i'll try to be more specific. > > yes: i want to create a tar file in memory, and add some content from a > memory buffer... > > my platform: > > $ uname -a > Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux > $ python -V > Python 2.3.4 > > following some suggestions i modified the code: > > $ cat tar001.py > import tarfile > import StringIO > sfo1 = StringIO.StringIO("one\n") > sfo2 = StringIO.StringIO("two\n") > tfo = StringIO.StringIO() > tar = tarfile.open(fileobj=tfo , mode="w") > ti = tar.gettarinfo(fileobj=tfo) gettarinfo() expects a real file, not a file-like object. You have to create your TarInfo manually. > for sfo in [sfo1 , sfo2]: > tar.addfile(fileobj=sfo , tarinfo=ti) > print tfo > > and that's what i get: > > $ python tar001.py > tar001.out > Traceback (most recent call last): > File "tar001.py", line 7, in ? > ti = tar.gettarinfo(fileobj=tfo) > File "/usr/lib/python2.3/tarfile.py", line 1060, in gettarinfo > name = fileobj.name > AttributeError: StringIO instance has no attribute 'name' > > can you help? I recommend that you have a look into the tarfile module's source code. The following seems to work: import sys import time import tarfile import StringIO sf1 = "first.txt", StringIO.StringIO("one one\n") sf2 = "second.txt", StringIO.StringIO("two\n") tf = StringIO.StringIO() tar = tarfile.open(fileobj=tf , mode="w") mtime = time.time() for name, f in [sf1 , sf2]: ti = tarfile.TarInfo(name) ti.size = f.len ti.mtime = mtime # add more attributes as needed tar.addfile(ti, f) sys.stdout.write(tf.getvalue()) Peter From sri_annauni at yahoo.co.in Fri Jul 3 08:15:47 2009 From: sri_annauni at yahoo.co.in (srinivasan srinivas) Date: Fri, 3 Jul 2009 17:45:47 +0530 (IST) Subject: Python debugger Message-ID: <740981.83964.qm@web7901.mail.in.yahoo.com> Hi, Could you suggest some python debuggers? Thanks, Srini Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com From jackdied at gmail.com Fri Jul 3 08:34:01 2009 From: jackdied at gmail.com (Jack Diederich) Date: Fri, 3 Jul 2009 08:34:01 -0400 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> Message-ID: On Thu, Jul 2, 2009 at 2:36 PM, Carl Banks wrote: > On Jul 2, 3:12?am, Ulrich Eckhardt wrote: >> Bearophile wrote: >> > Ulrich Eckhardt: >> >> a way to automatically release the resource, something >> >> which I would do in the destructor in C++. >> >> > Is this helpful? >> >http://effbot.org/pyref/with.htm >> >> Yes, it aims in the same direction. However, I'm not sure this applies to my >> case. The point is that the resource handle is not just used locally in a >> restricted scope but it is allocated and stored. The 'with' is something >> that makes sense in the context of mutex locking, where you have a >> well-defined critical section. What I need is something similar to open(), >> which returs a file. When the last reference to that object goes out of >> scope, the underlying file object is closed. > > On CPython you can do it with a __del__ attribute. > > Warning: objects with a __del__ attribute prevent reference cycle > detection, which can potentially lead to memory (and resource) leaks. > So you must be careful to avoid creating reference loops with that > object. WARNING-er: As Carl points out, adding a __del__ method actually makes it /less/ likely that your object will be cleaned up. __del__ is only useful if you have a complicated tear-down procedure. Since you come from C++ RAII land (my old haunt) your objects probably only allocate one resource each, or worse: a bunch of objects that allocate one resource each. In that case __del__ will always hurt you. The C++ temptation is to match every __init__ with a __del__. A better rule of thumb is to only add a __del__ method after confirming with someone else that it would be useful. Better still is to ask them by postal courier. For best results that someone should be your grandmother. > Note that file objects have a close method; you can explicitly close > it at any time. ?Your object should follow that example, and define a > close (or release, or whatever) method. ?I'd recommend making an > effort to call it and to rely on __del__ as little as possible. This. If you care enough about a resource to write a __del__ method you care enough to clean it up explicitly. 'with' blocks are very nice for that. -jack From tn.pablo at gmail.com Fri Jul 3 08:34:20 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Fri, 3 Jul 2009 07:34:20 -0500 Subject: Sequence splitting In-Reply-To: <79990c6b0907030401w2e668ac3qd315c751d9e3e924@mail.gmail.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <79990c6b0907030401w2e668ac3qd315c751d9e3e924@mail.gmail.com> Message-ID: On Fri, Jul 3, 2009 at 06:01, Paul Moore wrote: > 2009/7/3 Brad : >> Perhaps true, but it would be a nice convenience (for me) as a built- >> in written in either Python or C. Although the default case of a bool >> function would surely be faster. > > The chance of getting this accepted as a builtin is essentially zero. > To be a builtin, as opposed to being in the standard library, > something has to have a very strong justification. That's right. Mr. schickb, I think what you need is a few concrete examples as to where this function would be beneficial, so it can be judged objectively. -- Pablo Torres N. From bieffe62 at gmail.com Fri Jul 3 08:34:27 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 3 Jul 2009 05:34:27 -0700 (PDT) Subject: Is code duplication allowed in this instance? References: Message-ID: On Jul 3, 12:46?pm, Klone wrote: > Hi all. I believe in programming there is a common consensus to avoid > code duplication, I suppose such terms like 'DRY' are meant to back > this idea. Anyways, I'm working on a little project and I'm using TDD > (still trying to get a hang of the process) and am trying to test the > functionality within a method. Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine > the output before hand. > > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). If the purpose of the test is to verify the algorithm, you obviously should not use the algorithm to verify itself ... you should use a set of pairs (input data, exoected output data) data that you know is well representative of the data your algorithm will process. Possibly to prepare the test data set you might need a different - and already proven - implementation of the algorithm. Another thing I sometime do when testing mathematics function is use counter-proof: for instance, if my function computes the roots of a quadratic equation, the test verifies that the roots applied to the equation actually give (almost) zero as result. This kind of test might not be as rigorous as preparing the data set with the known answers, but it is easier to setup and could give you a first idea if your code is "correct enough" to stand more formal proof. Ciao ---- FB From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 08:46:08 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 14:46:08 +0200 Subject: Python debugger In-Reply-To: References: Message-ID: <4a4dfd78$0$404$426a74cc@news.free.fr> srinivasan srinivas a ?crit : > Hi, > Could you suggest some python debuggers? http://docs.python.org/library/pdb.html#module-pdb HTH From lie.1296 at gmail.com Fri Jul 3 08:49:21 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 12:49:21 GMT Subject: Is code duplication allowed in this instance? In-Reply-To: References: Message-ID: Klone wrote: > Hi all. I believe in programming there is a common consensus to avoid > code duplication, I suppose such terms like 'DRY' are meant to back > this idea. Anyways, I'm working on a little project and I'm using TDD > (still trying to get a hang of the process) and am trying to test the > functionality within a method. Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine > the output before hand. > > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). When unittesting a complex output, usually the first generation code would generate the output and then you (manually) verify this output and copy the output to the test code. The testing code should be as dumb as possible to avoid bugs in the testing code itself. The most important thing is not to use a possibly buggy algorithm implementation to check the algorithm itself. If your test code looks like this: # this the function to be tested def func(a, b): return a + b class TC(unittest.TestCase): def test_foo(self): a, b = 10, 20 # worse: use random module result = a + b self.assertEqual(func(a, b), result) then something is definitely wrong. Instead the testing code should be simple and stupid like: class TC(unittest.TestCase): def test_foo(self): self.assertEqual(func(10, 20), 30) There are exceptions though, such as if you're 100% sure your first-generation program is correct and you simply want to replicate the same function with different algorithm, or probably optimizing the function. For that case, you could do something like this: def func(a, b): # some fancy new algo pass class TC(unittest.TestCase): def original_func(self, a, b): return a + b def test_foo(self): self.assertEquals(func(a, b), self.original_func(a, b)) From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 08:50:10 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 14:50:10 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: References: Message-ID: <4a4dfe6a$0$404$426a74cc@news.free.fr> Stef Mientki a ?crit : > hello, > > I need to add an object's name to the global namespace. > The reason for this is to create an environment, > where you can add some kind of math environment, > where no need for Python knowledge is needed. > > The next statement works, > but I'm not sure if it will have any dramatical side effects, > other than overruling a possible object with the name A > > def some_function ( ...) : > A = object ( ...) > sys._getframe(1).f_globals [ Name ] = A > > Anything wrong with the 'global' statement ? Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. pythonrc start pythonrc done >>> def foo(): ... global a ... a = 42 ... >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> foo() >>> a 42 >>> From ziade.tarek at gmail.com Fri Jul 3 08:57:19 2009 From: ziade.tarek at gmail.com (=?ISO-8859-1?Q?Tarek_Ziad=E9?=) Date: Fri, 3 Jul 2009 14:57:19 +0200 Subject: PEP 376 In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Message-ID: <94bdd2610907030557q79cf7018t77c39beb6f45437b@mail.gmail.com> Ok here's my proposal for the checksum : - I'll add the "hash_type:" suffix in the record file - install will get a new option to define what hash should be used when writing the RECORD file it will default to SHA1 for 2.7/3.2 - pkgutil, that reads the RECORD files, will pick the right hash function by looking at the suffix now for the security it's another story that goes beyond the scope of this PEP notice though, that the PEP provides a place-holder for distributions metadata, so it could host a key later on. On Thu, Jul 2, 2009 at 8:00 PM, Charles Yeomans wrote: > > On Jul 2, 2009, at 1:37 PM, Lie Ryan wrote: > >> Joachim Str?mbergson wrote: >>> >>> Aloha! >>> >>> Tarek Ziad? wrote: >>>> >>>> The prefix is a good idea but since it's just a checksum to control >>>> that the file hasn't changed >>>> what's wrong with using a weak hash algorithm like md5 or now sha1 ? >>> >>> Because it creates a dependency to an old algorithm that should be >>> deprecated. Also using MD5, even for a thing like this might make people >>> belive that it is an ok algorithm to use - "Hey, it is used by the >>> default install in Python, so it must be ok, right?" >>> >>> If we flip the argument around: Why would you want to use MD5 instead of >>> SHA-256? For the specific use case the performance will not (should not) >>> be an issue. >>> >>> As I wrote a few mails ago, it is time to move forward from MD5 and >>> designing something in 2009 that will be around for many years that uses >>> MD5 is (IMHO) a bad design decision. >>> >>>> If someone wants to modify a file of a distribution he can recreate >>>> the checksum as well, >>>> the only secured way to prevent that would be to use gpg keys but >>>> isn't that overkill for what we need ? >>> >>> Actually, adding this type of security would IMHO be a good idea. >>> >> >> Now, are we actually talking about security or checksum? >> >> It has been known for years that MD5 is weak, weak, weak. Not just in >> the recent years. But it doesn't matter since MD5 was never designed for >> security, MD5 was designed to protect against random bits corruption. If >> you want security, look at least to GPG. For data protection against >> intentional, malicious forging, definitely MD5 is the wrong choice. But >> when you just want a simple checksum to ensure that a faulty router >> somewhere in the internet backbone doesn't destroying your data, MD5 is >> a fine algorithm. >> -- > > On the contrary, MD5 was intended to be a cryptographic hash function, not a > checksum. > > Charles Yeomans > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Tarek Ziad? | http://ziade.org From nosklo at gmail.com Fri Jul 3 08:57:35 2009 From: nosklo at gmail.com (Clovis Fabricio) Date: Fri, 3 Jul 2009 09:57:35 -0300 Subject: Python debugger In-Reply-To: <740981.83964.qm@web7901.mail.in.yahoo.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> Message-ID: <9187a60d0907030557q69a2429cn509e3792ed7708be@mail.gmail.com> 2009/7/3 srinivasan srinivas : > Could you suggest some python debuggers? Two graphical debugger frontends: http://www.gnu.org/software/emacs/ http://winpdb.org/ From lie.1296 at gmail.com Fri Jul 3 09:10:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 13:10:34 GMT Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: tsangpo wrote: > Just a shorter implementation: > > from itertools import groupby > def split(lst, func): > gs = groupby(lst, func) > return list(gs[True]), list(gs[False]) > As you're replying to my post, I assume you meant a shorter implementation my function. But it doesn't do the same thing. The idea with my group() is similar to what Steven D'Aprano is describing in another branch of this thread (i.e. splitting not only based on True and False, but arbitrary groupings, e.g. 'tru', 'flash' or perhaps -1, 0, 1). For example: >>> def group(seq, func=bool): ... ret = {} ... for item in seq: ... fitem = func(item) ... try: ... ret[fitem].append(item) ... except KeyError: ... ret[fitem] = [item] ... return ret ... >>> group(range(10), lambda x: x%3) {0: [0, 3, 6, 9], 1: [1, 4, 7], 2: [2, 5, 8]} >>> # the next one is the OP's split >>> group(['true', '', [], [''], 'false'], bool) {False: ['', []], True: ['true', [''], 'false']} From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 09:11:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 13:11:00 GMT Subject: Is code duplication allowed in this instance? References: Message-ID: <025df4fd$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 03:46:32 -0700, Klone wrote: > Hi all. I believe in programming there is a common consensus to avoid > code duplication, I suppose such terms like 'DRY' are meant to back this > idea. Anyways, I'm working on a little project and I'm using TDD (still > trying to get a hang of the process) and am trying to test the > functionality within a method. Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine the > output before hand. > > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). Neither -- that's essentially a pointless test. The only way to *correctly* test a function is to compare the result of that function to an *independent* test. If you test a function against itself, of course it will always pass: def plus_one(x): """Return x plus 1.""" return x-1 # Oops, a bug. # Test it is correct: assert plus_one(5) == plus_one(5) The only general advice I can give is: (1) Think very hard about finding an alternative algorithm to calculate the same result. There usually will be one. (2) If there's not, at least come up with an alternative implementation. It doesn't need to be particularly efficient, because it will only be called for testing. A rather silly example: def plus_one_testing(x): """Return x plus 1 using a different algorithm for testing.""" if type(x) in (int, long): temp = 1 for i in range(x-1): temp += 1 return temp else: floating_part = x - int(x) return floating_part + plus_one_testing(int(x)) (The only problem is, if a test fails, you may not be sure whether it's because your test function is wrong or your production function.) (3) Often you can check a few results by hand. Even if it takes you fifteen minutes, at least that gives you one good test. If need be, get a colleague to check your results. (4) Test failure modes. It might be really hard to calculate func(arg) independently for all possible arguments, but if you know that func(obj) should fail, at least you can test that. E.g. it's hard to test whether or not you've downloaded the contents of a URL correctly without actually downloading it, but you know that http://example.com/ should fail because that domain doesn't exist. (5) Test the consequences of your function rather than the exact results. E.g. if it's too difficult to calculate plus_one(x) independently: assert plus_one(x) > x # except for x = inf or -inf assert plus_one( -plus_one(x) ) == x # -(x+1)+1 = x (6) While complete test coverage is the ideal you aspire to, any tests are better than no tests. But they have to be good tests to be useful. Even *one* test is better than no tests. Hope this helps. -- Steven From lie.1296 at gmail.com Fri Jul 3 09:16:00 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 13:16:00 GMT Subject: stringio+tarfile In-Reply-To: <4a4de9a8$0$1105$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4de9a8$0$1105$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: >> First problem I see is all those numbers before the lines. That's not >> valid python. >> >> Assuming that was a transcription error > > not so. i intentionally add linenumbers to facilitare reference to the > code, but if it is a nuisance i will not include them anymore. The line numbering makes it impossible for others to copy and paste to the interactive interpreter to see the problem code running, and makes it hard to copy and paste into a script file. For code reference, quoting the problematic code is usually much easier. From ronn.ross at gmail.com Fri Jul 3 09:26:52 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Fri, 3 Jul 2009 09:26:52 -0400 Subject: VirtualEnv Message-ID: <9c8c445f0907030626i62d912e6k6d92247a428145e0@mail.gmail.com> I'm attempting to write a bootstrap script for virtualenv. I just want to do a couple of easy_install's after the environment is created. It was fairly easy to create the script, but I can't figure out how to implement it. The documentation was not of much help. Can someone please point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From no.email at please.post Fri Jul 3 09:38:20 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 13:38:20 +0000 (UTC) Subject: Why re.match()? References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: In <025db0a6$0$20657$c3e8da3 at news.astraweb.com> Steven D'Aprano writes: >On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: >> If the concern is efficiency for such cases, then simply implement >> optional offset and length parameters for re.search(), to specify any >> arbitrary substring to apply the search to. To have a special-case >> re.match() method in addition to a general re.search() method is >> antithetical to language minimalism, and plain-old bizarre. Maybe >> there's a really good reason for it, but it has not been mentioned yet. >There is, and it has. I "misspoke" earlier. I should have written "I'm *sure* there's a really good reason for it." And I think no one in this thread (myself included, of course) has a clue of what it is. I miss the days when Guido still posted to comp.lang.python. He'd know. Regarding the "practicality beats purity" line, it's hard to think of a better motto for *Perl*, with all its practicality-oriented special doodads. (And yes, I know where the "practicality beats purity" line comes from.) Even *Perl* does not have a special syntax for the task that re.match is supposedly tailor-made for, according to the replies I've received. Given that it is so trivial to implement all of re.match's functionality with only one additional optional parameter for re.search (i.e. pos), it is absurd to claim that re.match is necessary for the sake of this special functionality. The justification for re.match must be elsewhere. But thanks for letting me know that I'm entitled to my opinion. That's a huge relief. kj From python at mrabarnett.plus.com Fri Jul 3 09:53:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 14:53:42 +0100 Subject: Why re.match()? In-Reply-To: References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4A4E0D66.6070704@mrabarnett.plus.com> kj wrote: > In <025db0a6$0$20657$c3e8da3 at news.astraweb.com> Steven D'Aprano writes: > >> On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: > >>> If the concern is efficiency for such cases, then simply implement >>> optional offset and length parameters for re.search(), to specify any >>> arbitrary substring to apply the search to. To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, and plain-old bizarre. Maybe >>> there's a really good reason for it, but it has not been mentioned yet. > >> There is, and it has. > > I "misspoke" earlier. I should have written "I'm *sure* there's > a really good reason for it." And I think no one in this thread > (myself included, of course) has a clue of what it is. I miss the > days when Guido still posted to comp.lang.python. He'd know. > > Regarding the "practicality beats purity" line, it's hard to think > of a better motto for *Perl*, with all its practicality-oriented > special doodads. (And yes, I know where the "practicality beats > purity" line comes from.) Even *Perl* does not have a special > syntax for the task that re.match is supposedly tailor-made for, > according to the replies I've received. Given that it is so trivial > to implement all of re.match's functionality with only one additional > optional parameter for re.search (i.e. pos), it is absurd to claim > that re.match is necessary for the sake of this special functionality. > The justification for re.match must be elsewhere. > > But thanks for letting me know that I'm entitled to my opinion. > That's a huge relief. > As I wrote, re.match anchors the match whereas re.search doesn't. An alternative would have been to implement Perl's \G anchor, but I believe that that was invented after the re module was written. From no.email at please.post Fri Jul 3 10:05:08 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 14:05:08 +0000 (UTC) Subject: Clarity vs. code reuse/generality Message-ID: I'm will be teaching a programming class to novices, and I've run into a clear conflict between two of the principles I'd like to teach: code clarity vs. code reuse. I'd love your opinion about it. The context is the concept of a binary search. In one of their homeworks, my students will have two occasions to use a binary search. This seemed like a perfect opportunity to illustrate the idea of abstracting commonalities of code into a re-usable function. So I thought that I'd code a helper function, called _binary_search, that took five parameters: a lower limit, an upper limit, a one-parameter function, a target value, and a tolerance (epsilon). It returns the value of the parameter for which the value of the passed function is within the tolerance of the target value. This seemed straightforward enough, until I realized that, to be useful to my students in their homework, this _binary_search function had to handle the case in which the passed function was monotonically decreasing in the specified interval... The implementation is still very simple, but maybe not very clear, particularly to programming novices (docstring omitted): def _binary_search(lo, hi, func, target, epsilon): assert lo < hi assert epsilon > 0 sense = cmp(func(hi), func(lo)) if sense == 0: return None target_plus = sense * target + epsilon target_minus = sense * target - epsilon while True: param = (lo + hi) * 0.5 value = sense * func(param) if value > target_plus: hi = param elif value < target_minus: lo = param else: return param if lo == hi: return None My question is: is the business with sense and cmp too "clever"? Here's the rub: the code above is more general (hence more reusable) by virtue of this trick with the sense parameter, but it is also a bit harder to understand. This not an unusual situation. I find that the processing of abstracting out common logic often results in code that is harder to read, at least for the uninitiated... I'd love to know your opinions on this. TIA! kj From esj at harvee.org Fri Jul 3 10:19:10 2009 From: esj at harvee.org (Eric S. Johansson) Date: Fri, 03 Jul 2009 10:19:10 -0400 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> Message-ID: <4A4E135E.5090400@harvee.org> Horace Blegg wrote: > I've been kinda following this. I have a cousin who is permanently wheel > chair bound and doesn't have perfect control of her hands, but still > manages to use a computer and interact with society. However, the > idea/thought of disabled programmers was new to me/hadn't ever occurred > to me. > > You say that using your hands is painful, but what about your feet? > Wouldn't it be possible to rig up some kind of foot peddle for > shift/caps lock? Kinda like the power peddle used with sowing machines, > so the hands are free to hold fabric. > > I don't mean this in a condescending manor, and I apologize if you take > it as such. I'm genuinely curious if you think something like this could > work. > > The way I was envisioning it working last night (and I haven't the > faintest clue how SR works, nor have I ever used SR) was that you would > hit the foot peddle, which would tell the SR program to capitalize the > first letter of the next word (a smart shift, basically, so you don't > end up doing something like ... WONderland -or- "stocks are up 1,0))% > TOday".) > > Possible? Stupid? > it's not stupid. People have used foot pedals for decades for a variety of controls. I don't think foot pedals would work for me because when I am dictating, I pace. Standing, sitting, I pace. With a cord headset, I'm forced to stay within about 4 feet of the computer. But what I'm using a Bluetooth headset, I will sometimes ramble as far as 10 or 15 feet from the computer. It helps if I make the font larger so I can glance over and see what kind of errors I've gotten. I really love a Bluetooth headset with speech recognition. It's so liberating. Your question about foot pedals makes me think of alternative. would it make sense to have a handheld keyboard which would be used for command-and-control functionality or as an adjunct to speech recognition use? It would have to be designed in such a way that it doesn't aggravate a hand injury which may not be possible. Anyway, just thinking out loud. From aahz at pythoncraft.com Fri Jul 3 10:31:36 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 07:31:36 -0700 Subject: Why re.match()? References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: In article , kj wrote: >In <025db0a6$0$20657$c3e8da3 at news.astraweb.com> Steven D'Aprano writes: >>On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: >>> >>> If the concern is efficiency for such cases, then simply implement >>> optional offset and length parameters for re.search(), to specify any >>> arbitrary substring to apply the search to. To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, and plain-old bizarre. Maybe >>> there's a really good reason for it, but it has not been mentioned yet. >> >>There is, and it has. > >I "misspoke" earlier. I should have written "I'm *sure* there's >a really good reason for it." And I think no one in this thread >(myself included, of course) has a clue of what it is. I miss the >days when Guido still posted to comp.lang.python. He'd know. You may find this enlightening: http://www.python.org/doc/1.4/lib/node52.html -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 10:33:12 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 14:33:12 GMT Subject: Clarity vs. code reuse/generality References: Message-ID: <025e0841$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 14:05:08 +0000, kj wrote: > ... I find that the processing of > abstracting out common logic often results in code that is harder to > read ... Yes. There is often a conflict between increasing abstraction and ease of understanding. [...] > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 You're using assert for data sanitation. That is a very bad habit for you to be teaching novices. Your code will fail to behave as expected whenever the caller runs Python with the -O switch. [...] > My question is: is the business with sense and cmp too "clever"? For production code? Maybe -- you would need to profile the code, and compare it to a less general, more simple-minded function, and see which performs better. If there is an excessive performance penalty, that's a good sign that perhaps you're being too clever, too general, or too abstract -- or all three. Too clever for novices? That depends on how inexperienced they are -- are they new to Python or new to programming in general? Are they children or adults? Do they have a PhD degree or are they still at primary school? It depends on their experience and their intelligence -- dare I say it, some people will *never* get programming, let alone code-reuse. It also depends on how you lead up to it -- are you dropping them straight into the abstract code, or giving them two pieces of nearly the same code and showing them how to pull out the common functionality? -- Steven From bearophileHUGS at lycos.com Fri Jul 3 10:34:23 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 3 Jul 2009 07:34:23 -0700 (PDT) Subject: Is code duplication allowed in this instance? References: Message-ID: <4d99b710-45f1-42dc-b80c-1894af26db42@p29g2000yqh.googlegroups.com> Francesco Bochicchio: > Possibly to prepare the test data set you might need a > different - and already proven - implementation of > the algorithm. Usually a brute force or slow but short algorithm is OK (beside some hard-coded input-output pairs). Sometimes you may use the first implementation of your code that's usually simpler, before becoming complex because of successive optimizations. But you have to be careful, because the first implementation too may be buggy. Some other times there are simpler algorithms to test if the output of another algorithm is correct (think of exponential algorithms with a polinomial test of the correct result), for example to test a fancy Introsort implementation you can use a very small and simple O(n^2) sorter, or better a simple linear loop that tests the result to be sorted. Here, beside unit tests, are also useful languages (or tools) that allow to add pre- and post-conditions, class invariants, etc. Bye, bearophile From alan.isaac at gmail.com Fri Jul 3 10:34:58 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 14:34:58 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On 7/3/2009 10:05 AM kj apparently wrote: > The context is the concept of a binary search. In one of their > homeworks, my students will have two occasions to use a binary > search. This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. > > This seemed straightforward enough, until I realized that, to be > useful to my students in their homework, this _binary_search function > had to handle the case in which the passed function was monotonically > decreasing in the specified interval... > > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > while True: > param = (lo + hi) * 0.5 > value = sense * func(param) > if value > target_plus: > hi = param > elif value < target_minus: > lo = param > else: > return param > > if lo == hi: > return None 1. Don't use assertions to test argument values! 2. from scipy.optimize import bisect def _binary_search(lo, hi, func, target, epsilon): def f(x): return func(x) - target return bisect(f, lo, high, xtol=epsilon) 3. If you don't want to use SciPy (why?), have them implement http://en.wikipedia.org/wiki/Bisection_method#Pseudo-code to produce their own `bisect` function. hth, Alan Isaac From aahz at pythoncraft.com Fri Jul 3 10:36:38 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 07:36:38 -0700 Subject: Clarity vs. code reuse/generality References: Message-ID: In article , kj wrote: > >This seemed straightforward enough, until I realized that, to be >useful to my students in their homework, this _binary_search function >had to handle the case in which the passed function was monotonically >decreasing in the specified interval... > >def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > while True: > param = (lo + hi) * 0.5 > value = sense * func(param) > if value > target_plus: > hi = param > elif value < target_minus: > lo = param > else: > return param > > if lo == hi: > return None > >My question is: is the business with sense and cmp too "clever"? First of all, cmp() is gone in Python 3, unfortunately, so I'd avoid using it. Second, assuming I understand your code correctly, I'd change "sense" to "direction" or "order". -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From timinganalyzer at gmail.com Fri Jul 3 10:42:12 2009 From: timinganalyzer at gmail.com (chewie) Date: Fri, 3 Jul 2009 07:42:12 -0700 (PDT) Subject: The TimingAnalyzer -- project to convert from Java to Python Message-ID: <368b6501-5f01-4a96-9e31-274601878464@c36g2000yqn.googlegroups.com> ?Hello, This a project related to the development of an EDA CAD tool program called the TimingAnalyzer. Digital engineers could use this kind of program to analyze and document inteface timing diagrams for IC, ASIC, FPGA, and board level hardware projects. The TimingAnalyzer is licensed as freeware. I don't have the time needed to make a high quality commercial product but I do want to keep the development moving forward and continue to fix problems and add new features as time permits. www.timing-diagrams.com Recently, I have become very interested in Python and using it to develop similar type cad programs. My plan is to convert the TimingAnalyzer Java to Python with mostly a scripting interface for building complex timing diagrams, doing timing analysis, creating testbenches and testvectors from waveform diagrams, and creating timing diagrams from simulation VCD files. Most all of this is text based work anyway. Developing professional GUIs is very time consuming for me. This has been my bottleneck with the program all along. With a command line interface, you will execute a script and in one window, and view and edit and print the timing diagram shown in another window. Like the Matlab interface. For example: micro = m68000() micro.write(add, data, wait_states) micro.read(add, wait_states). or add_clock(......) add_signal(.....) add_delay(......) add_constraint(.....) add_or_gate(....) add_and_gate(....) add_counter(....) add_clock_jitter(.....) analyze_clock_domains(.....) analyze_worst_case_timings(....) analyze_best_case_timings. read_vcd(....) vcd_2_timing_diagram(.....) create_testvectors(.....) create_testbench(....) A lot of these functions are built into the program now so its a matter of converting them java to python. I won't have to spend most of the time getting the user interface to look good and be friendly. If this is made an open source project, I would hope that others would help with the development and new features and bug fixes will so progress will be made quickly. If anyone is interested in helping with the development, I will make this an open source project. Just let me know if your interested. Thank you, Dan Fabrizio From bearophileHUGS at lycos.com Fri Jul 3 10:48:57 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 3 Jul 2009 07:48:57 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: Message-ID: <5461f308-3b3b-4baf-9ef0-c1a94d059119@y17g2000yqn.googlegroups.com> On 3 Lug, 16:05, kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. They are both important principles, but clarity is usually more important because short code that can't be read can't be fixed and modified, while long code that can be read is alive still. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). Five arguments are a bit too much, even for non-novices. 2-3 arguments are more than enough for novices. Where are doctests'? Novices have to learn to think of tests as part of the normal code :-) I think the main problem in all this discussion is that generally to learn to program you have to write code yourself, so your students have to invent and write their own binary search. Reading your code they are going to learn much less. Creating a binary search from almost scratch can turn out to be too much hard for them, it means you have to ask them to invent something simpler :-) Programming is (among other things) problem solving, so they have to learn to solve not easy problems from the beginning. Showing them famous algorithms (and very good code to copy from) can be useful, but it's less important than learning basic problem solving skills, and much less important than learning why solving problems has to became their purpose and pleasure :-) Bye, bearophile From python at mrabarnett.plus.com Fri Jul 3 10:52:31 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 15:52:31 +0100 Subject: pep 8 constants In-Reply-To: <4A4E135E.5090400@harvee.org> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> Message-ID: <4A4E1B2F.2000700@mrabarnett.plus.com> Eric S. Johansson wrote: > Horace Blegg wrote: >> I've been kinda following this. I have a cousin who is permanently wheel >> chair bound and doesn't have perfect control of her hands, but still >> manages to use a computer and interact with society. However, the >> idea/thought of disabled programmers was new to me/hadn't ever occurred >> to me. >> >> You say that using your hands is painful, but what about your feet? >> Wouldn't it be possible to rig up some kind of foot peddle for >> shift/caps lock? Kinda like the power peddle used with sowing machines, >> so the hands are free to hold fabric. >> >> I don't mean this in a condescending manor, and I apologize if you take >> it as such. I'm genuinely curious if you think something like this could >> work. >> >> The way I was envisioning it working last night (and I haven't the >> faintest clue how SR works, nor have I ever used SR) was that you would >> hit the foot peddle, which would tell the SR program to capitalize the >> first letter of the next word (a smart shift, basically, so you don't >> end up doing something like ... WONderland -or- "stocks are up 1,0))% >> TOday".) >> >> Possible? Stupid? >> > it's not stupid. > > People have used foot pedals for decades for a variety of controls. I don't > think foot pedals would work for me because when I am dictating, I pace. > Standing, sitting, I pace. With a cord headset, I'm forced to stay within about > 4 feet of the computer. But what I'm using a Bluetooth headset, I will sometimes > ramble as far as 10 or 15 feet from the computer. It helps if I make the font > larger so I can glance over and see what kind of errors I've gotten. > > I really love a Bluetooth headset with speech recognition. It's so liberating. > > Your question about foot pedals makes me think of alternative. would it make > sense to have a handheld keyboard which would be used for command-and-control > functionality or as an adjunct to speech recognition use? It would have to be > designed in such a way that it doesn't aggravate a hand injury which may not be > possible. Anyway, just thinking out loud. > You can get giant piano keyboards that you step on, so how about a giant computer keyboard? "I wrote 5 miles of code before lunch!" :-) From pdmef at gmx.net Fri Jul 3 10:54:26 2009 From: pdmef at gmx.net (Rocco Rutte) Date: Fri, 3 Jul 2009 16:54:26 +0200 Subject: Custom CFLAGS with distutils Message-ID: <20090703145426.GJ316@andreas.daprodeges.fqdn.th-h.de> Hi, I've been trying to make distutils build mercurial with custom cflags. The only way this works is to change Makefile because I don't want to put my changed CFLAGS into the environment and I tend to forget to run "make" with a CFLAGS=... option. Google brings up a special "Setup" file which should solve my problem, but it somehow doesn't. I've tried: mercurial mercurial/base85.c -Xcompiler -arch x86_64 mercurial.base85 mercurial/base85.c -Xcompiler -arch x86_64 for base85.c in the mercurial/ subdirectory. Hacking Makefile does the trick, but having a Setup file working would never produce merge conflicts. What am I doing wrong? Rocco From metolone+gmane at gmail.com Fri Jul 3 10:58:35 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 3 Jul 2009 07:58:35 -0700 Subject: =?utf-8?Q?Re:_getting_rid_of_=E2=80=94?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> Message-ID: "Tep" wrote in message news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... > On 3 Jul., 06:40, Simon Forman wrote: > > On Jul 2, 4:31 am, Tep wrote: [snip] > > > > > > how can I replace '?' sign from string? Or do split at that > > > > > > character? > > > > > > Getting unicode error if I try to do it: > > > > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in > > > > > > position > > > > > > 1: ordinal not in range(128) > > > > > > > > Thanks, Pet > > > > > > > > script is # -*- coding: UTF-8 -*- [snip] > > I just tried a bit of your code above in my interpreter here and it > > worked fine: > > > > |>>> data = 'foo ? bar' > > |>>> data.split('?') > > |['foo ', ' bar'] > > |>>> data = u'foo ? bar' > |>>> data.split(u'?') > > |[u'foo ', u' bar'] > > > > Figure out the smallest piece of "html source code" that causes the > > problem and include that with your next post. > > The problem was, I've converted "html source code" to unicode object > and didn't encoded to utf-8 back, before using split... > Thanks for help and sorry for not so smart question > Pet You'd still benefit from posting some code. You shouldn't be converting back to utf-8 to do a split, you should be using a Unicode string with split on the Unicode version of the "html source code". Also make sure your file is actually saved in the encoding you declare. I print the encoding of your symbol in two encodings to illustrate why I suspect this. Below, assume "data" is your "html source code" as a Unicode string: # -*- coding: UTF-8 -*- data = u'foo ? bar' print repr(u'?'.encode('utf-8')) print repr(u'?'.encode('windows-1252')) print data.split(u'?') print data.split('?') OUTPUT: '\xe2\x80\x94' '\x97' [u'foo ', u' bar'] Traceback (most recent call last): File "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 427, in ImportFile exec codeObj in __main__.__dict__ File "", line 1, in File "x.py", line 6, in print data.split('?') UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) Note that using the Unicode string in split() works. Also note the decode byte in the error message when using a non-Unicode string to split the Unicode data. In your original error message the decode byte that caused an error was 0x97, which is 'EM DASH' in Windows-1252 encoding. Make sure to save your source code in the encoding you declare. If I save the above script in windows-1252 encoding and change the coding line to windows-1252 I get the same results, but the decode byte is 0x97. # coding: windows-1252 data = u'foo ? bar' print repr(u'?'.encode('utf-8')) print repr(u'?'.encode('windows-1252')) print data.split(u'?') print data.split('?') '\xe2\x80\x94' '\x97' [u'foo ', u' bar'] Traceback (most recent call last): File "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 427, in ImportFile exec codeObj in __main__.__dict__ File "", line 1, in File "x.py", line 6, in print data.split('?) UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: ordinal not in range(128) -Mark From nhuthuy.huynh at gmail.com Fri Jul 3 11:18:58 2009 From: nhuthuy.huynh at gmail.com (Huynh Nhu Thuy) Date: Fri, 3 Jul 2009 08:18:58 -0700 (PDT) Subject: Video shows vigorous Jackson before death??? Message-ID: <333e0920-4dd9-4ab3-8e92-8dcfb207fc65@x5g2000prf.googlegroups.com> Video shows vigorous Jackson before death??? http://hd-family.blogspot.com/2009/07/video-shows-vigorous-jackson-before.html LOS ANGELES (AFP) ? A video released Thursday showed Michael Jackson vigorously practicing a song-and-dance routine days before his death, supporting accounts he had been in good health. In footage obtained by AFP, the pop legend performed at the Staples Center in Los Angeles on June 23, two days before he died, as he prepared for a 50-date set in London starting in July. http://hd-family.blogspot.com/2009/07/video-shows-vigorous-jackson-before.html From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 11:19:34 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 17:19:34 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <4a4e216e$0$7801$426a74cc@news.free.fr> kj a ?crit : > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. I'd love your opinion about > it. (snip - others already commented on this code) > Here's the rub: the code above is more general (hence more reusable) > by virtue of this trick with the sense parameter, but it is also > a bit harder to understand. Perhaps better naming (s/sense/direction/g ?) and a small comment could help ? > This not an unusual situation. I find that the processing of > abstracting out common logic often results in code that is harder > to read, at least for the uninitiated... IOW : the notion of "clarity" depends on the context (including the reader). Anyway, there are algorithms (or implementations of...) that are definitly and inherently on the 'hard to read' side - well, complexity is something you have to learn to live with, period. The key is to try and make it as simple and readable *as possible*. Also, factoring out common code - or even slightly complex code - often makes _client_ code (much) more readable. From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 11:22:40 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 17:22:40 +0200 Subject: Why re.match()? In-Reply-To: References: Message-ID: <4a4e2227$0$7801$426a74cc@news.free.fr> kj a ?crit : (snipo > To have a special-case > re.match() method in addition to a general re.search() method is > antithetical to language minimalism, FWIW, Python has no pretention to minimalism. From Scott.Daniels at Acm.Org Fri Jul 3 11:43:47 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 03 Jul 2009 08:43:47 -0700 Subject: Direct interaction with subprocess - the curse of blocking I/O In-Reply-To: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> References: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> Message-ID: yam850 wrote: > I made a python method/function for non blocking read from a file > object.... I am happy to see comments. OK, here's a fairly careful set of comments with a few caveats: Does this work on windows? The top comment should say where you know it works. Does this code correctly read a single line? perhaps you need to check for a final '\n' whenever you actually get characters, and break out there. The rest below simply is style-changing suggestions; take what you like and leave the rest. > > def non_blocking_readline(f_read=sys.stdin, timeout_select=0.0): > """to readline non blocking from the file object 'f_read' > for 'timeout_select' see module 'select'""" > import select Typically put imports at the top of the source XXX text_lines = '' # empty string Either no comment here or say _why_ it is empty. (A) text_lines = [] # for result accumulation if collecting pieces (see below) (B) text_lines = '' # for result accumulation if collecting a single string > while True: # as long as there are bytes to read > try: # try select > rlist, wlist, xlist = select.select([f_read], [], [], > timeout_select) XXX except: # select ERROR XXX print >>sys.stderr, ("non_blocking_read select ERROR") I'd not hide the details of the exception like that. Don't do empty excepts. Either don't catch it at all here, (I prefer that) or do something like the following to capture the error: except Exception, why: print >>sys.stderr, "non_blocking_read select: %s" % why > break XXX if DEBUG: print("rlist=%s, wlist=%s, xlist=%s" % (repr(rlist), XXX repr(wlist), repr(xlist))) Don't be scared of vertical space; if you must, define a function DPRINT to ignore args; use %r to get repr So, either: if DEBUG: print("rlist=%r, wlist=%r, xlist=%r" % ( rlist, wlist, xlist)) or: elsewhere: def DPRINT(format_str, *args, **kwargs): '''Conditionally print formatted output based on DEBUG''' if DEBUG: print(format_str % (args or kwargs)) and then here: DPRINT("rlist=%r, wlist=%r, xlist=%r", rlist, wlist, xlist) XXX if len(rlist) > 0: Idiomatic Python: use the fact that empty containers evaluate false: if rlist: > text_read = f_read.readline() # get a line XXX if DEBUG: print("after read/readline text_read:'%s', len= > %s" % (text_read, repr(len(text_read)))) Similar comment to above: if DEBUG: print("after read/readline text_read:%r, len=%s" % ( text_read, len(text_read)) or: DPRINT("after read/readline text_read:%r, len=%s", text_read, len(text_read)) XXX if len(text_read) > 0: # there were some bytes if text_read: # there were some bytes XXX text_lines = "".join([text_lines, text_read]) Note the join is a good idea only if you have several elements. For a single concatenation, "=" is just fine. The (A) case combines at the end, the (B) case if you expect multiple concatenates are rare. (A) text_lines.append(text_read) (B) text_lines += text_read XXX if DEBUG: print("text_lines:'%s'" % (text_lines)) Similar to above if DEBUG: print("text_lines:%r" % text_lines) or: DPRINT("text_lines:%r", text_lines) XXX else: XXX break # there was no byte in a line XXX else: XXX break # there was no byte in the f_read To make the flow of control above more clear (one case continues, others get out), I'd also replace the above with: continue # In one case we keep going break # No new chars found, let's get out. XXX if text_lines == '': XXX return None XXX else: XXX return text_lines Simplify using 'or's semantics: (A) return ''.join(text_lines) or None (B) return text_lines or None So, with everything applied: import select def DPRINT(format_str, *args, **kwargs): '''Conditionally print formatted output based on DEBUG''' if DEBUG: print(format_str % (args or kwargs)) def non_blocking_readline(f_read=sys.stdin, timeout_select=0.0): """to readline non blocking from the file object 'f_read' for 'timeout_select' see module 'select' """ text_lines = '' # for result accumulation while True: # as long as there are bytes to read rlist, wlist, xlist = select.select([f_read], [], [], timeout_select) DPRINT("rlist=%r, wlist=%r, xlist=%r", rlist, wlist, xlist) if rlist: text_read = f_read.readline() # get a line DPRINT("after read/readline text_read:%r, len=%s", text_read, len(text_read)) if text_read: # there were some bytes text_lines += text_read DPRINT("text_lines:%r", text_lines) continue # Got some chars, keep going. break # Nothing new found, let's get out. return text_lines or None --Scott David Daniels Scott.Daniels at Acm.Org From jeanmichel at sequans.com Fri Jul 3 11:58:48 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 03 Jul 2009 17:58:48 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <4A4E2AB8.6060808@sequans.com> kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. I'd love your opinion about > it. > [...] > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None My suggestion on how to improve this part for python novices: # assuming func is monotonous if func(high) > func(low): direction = 1 # aka sign of the func derivative elif func(low) > func(high): direction = -1 else: return None Avoid using cmp, this will prevent the "what the hell is cmp doing ?" , unless you want to teach your students how to search for inline python documentation. Some other list members have suggested to improve the variable naming. I couldn't agree more, in your case, I think clarity can be achieved as well with the abstraction (these notions does not necessarily collide). Here's a link on my how-to-name bible : http://tottinge.blogsome.com/meaningfulnames/ Jean-Michel From kurt.alfred.mueller at gmail.com Fri Jul 3 11:59:24 2009 From: kurt.alfred.mueller at gmail.com (yam850) Date: Fri, 3 Jul 2009 08:59:24 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> Message-ID: <8a974010-e0c8-470d-b6a8-9934cf61a377@18g2000yqa.googlegroups.com> On 3 Jul., 17:43, Scott David Daniels wrote: > yam850 wrote: > > I made a python method/function for nonblockingread from a file > > object.... I am happy to see comments. > OK, here's a fairly careful set of comments with a few caveats: [snip] valuable comments > --Scott David Daniels > Scott.Dani... at Acm.Org Wow, thats a GREAT answer!!! Thanks!!! I will learn a lot while "digesting" this mail. G -- yam850 From lie.1296 at gmail.com Fri Jul 3 12:10:42 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 16:10:42 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <64q3m.2408$ze1.1228@news-server.bigpond.net.au> kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. I'd love your opinion about > it. Sometimes when the decision between clarity and generality becomes too hard; you might fare better to save the code, go out for a walk to forget the code, and start a blank text editor. Being in a fresh mind, you may found an alternative approach, e.g.: from __future__ import division def binary_search(lo, hi, func, target, epsilon): # reverses hi and lo if monotonically decreasing lo, hi = (lo, hi) if func(hi) > func(lo) else (hi, lo) param = (lo + hi) / 2 # loop while not precise enough while abs(func(param) - target) > epsilon: param = (lo + hi) / 2 if target < func(param): hi = param else: lo = param return param > The context is the concept of a binary search. In one of their > homeworks, my students will have two occasions to use a binary > search. This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. > > This seemed straightforward enough, until I realized that, to be > useful to my students in their homework, this _binary_search function > had to handle the case in which the passed function was monotonically > decreasing in the specified interval... > > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > while True: > param = (lo + hi) * 0.5 > value = sense * func(param) > if value > target_plus: > hi = param > elif value < target_minus: > lo = param > else: > return param > > if lo == hi: > return None > > My question is: is the business with sense and cmp too "clever"? > > Here's the rub: the code above is more general (hence more reusable) > by virtue of this trick with the sense parameter, but it is also > a bit harder to understand. > > This not an unusual situation. I find that the processing of > abstracting out common logic often results in code that is harder > to read, at least for the uninitiated... > > I'd love to know your opinions on this. > > TIA! > > kj > From no.email at please.post Fri Jul 3 12:19:22 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 16:19:22 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Alan G Isaac writes: >1. Don't use assertions to test argument values! Out of curiosity, where does this come from? Thanks, kj From no.email at please.post Fri Jul 3 12:20:33 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 16:20:33 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In aahz at pythoncraft.com (Aahz) writes: >First of all, cmp() is gone in Python 3, unfortunately, so I'd avoid >using it. Good to know. >Second, assuming I understand your code correctly, I'd change >"sense" to "direction" or "order". Thanks, kj From petshmidt at googlemail.com Fri Jul 3 12:24:43 2009 From: petshmidt at googlemail.com (Tep) Date: Fri, 3 Jul 2009 09:24:43 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> Message-ID: <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> On 3 Jul., 16:58, "Mark Tolonen" wrote: > "Tep" wrote in message > > news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... > > > > > > > On 3 Jul., 06:40, Simon Forman wrote: > > > On Jul 2, 4:31 am, Tep wrote: > [snip] > > > > > > > how can I replace '?' sign from string? Or do split at that > > > > > > > character? > > > > > > > Getting unicode error if I try to do it: > > > > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in > > > > > > > position > > > > > > > 1: ordinal not in range(128) > > > > > > > > Thanks, Pet > > > > > > > > script is # -*- coding: UTF-8 -*- > [snip] > > > I just tried a bit of your code above in my interpreter here and it > > > worked fine: > > > > |>>> data = 'foo ? bar' > > > |>>> data.split('?') > > > |['foo ', ' bar'] > > > |>>> data = u'foo ? bar' > > |>>> data.split(u'?') > > > |[u'foo ', u' bar'] > > > > Figure out the smallest piece of "html source code" that causes the > > > problem and include that with your next post. > > > The problem was, I've converted "html source code" to unicode object > > and didn't encoded to utf-8 back, before using split... > > Thanks for help and sorry for not so smart question > > Pet > > You'd still benefit from posting some code. ?You shouldn't be converting I've posted code below > back to utf-8 to do a split, you should be using a Unicode string with split > on the Unicode version of the "html source code". ?Also make sure your file > is actually saved in the encoding you declare. ?I print the encoding of your > symbol in two encodings to illustrate why I suspect this. File was indeed in windows-1252, I've changed this. For errors see below > > Below, assume "data" is your "html source code" as a Unicode string: > > # -*- coding: UTF-8 -*- > data = u'foo ? bar' > print repr(u'?'.encode('utf-8')) > print repr(u'?'.encode('windows-1252')) > print data.split(u'?') > print data.split('?') > > OUTPUT: > > '\xe2\x80\x94' > '\x97' > [u'foo ', u' bar'] > Traceback (most recent call last): > ? File > "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > line 427, in ImportFile > ? ? exec codeObj in __main__.__dict__ > ? File "", line 1, in > ? File "x.py", line 6, in > ? ? print data.split('?') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: > ordinal not in range(128) > > Note that using the Unicode string in split() works. ?Also note the decode > byte in the error message when using a non-Unicode string to split the > Unicode data. ?In your original error message the decode byte that caused an > error was 0x97, which is 'EM DASH' in Windows-1252 encoding. ?Make sure to > save your source code in the encoding you declare. ?If I save the above > script in windows-1252 encoding and change the coding line to windows-1252 I > get the same results, but the decode byte is 0x97. > > # coding: windows-1252 > data = u'foo ? bar' > print repr(u'?'.encode('utf-8')) > print repr(u'?'.encode('windows-1252')) > print data.split(u'?') > print data.split('?') > > '\xe2\x80\x94' > '\x97' > [u'foo ', u' bar'] > Traceback (most recent call last): > ? File > "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > line 427, in ImportFile > ? ? exec codeObj in __main__.__dict__ > ? File "", line 1, in > ? File "x.py", line 6, in > ? ? print data.split('?) > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: > ordinal not in range(128) > > -Mark #! /usr/bin/python # -*- coding: UTF-8 -*- import urllib2 import re def getTitle(input): title = re.search('(.*?)', input) title = title.group(1) print "FULL TITLE", title.encode('UTF-8') parts = title.split(' ? ') return parts[0] def getWebPage(url): user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } req = urllib2.Request(url, '', headers) response = urllib2.urlopen(req) the_page = unicode(response.read(), 'UTF-8') return the_page def main(): url = "http://bg.wikipedia.org/wiki/ %D0%91%D0%B0%D1%85%D1%80%D0%B5%D0%B9%D0%BD" title = getTitle(getWebPage(url)) print title[0] if __name__ == "__main__": main() Traceback (most recent call last): File "C:\user\Projects\test\src\new_main.py", line 29, in main() File "C:\user\Projects\test\src\new_main.py", line 24, in main title = getTitle(getWebPage(url)) FULL TITLE ?????????????? ??? ?????????????????? File "C:\user\Projects\test\src\new_main.py", line 9, in getTitle parts = title.split(' ??? ') UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1: ordinal not in range(128) From keflavich at gmail.com Fri Jul 3 12:30:21 2009 From: keflavich at gmail.com (Keflavich) Date: Fri, 3 Jul 2009 09:30:21 -0700 (PDT) Subject: Compiling 64 bit python on a mac - cannot compute sizeof (int) Message-ID: I'm trying to compile a 64 bit version of python 2.6.2 on my mac (OS X 10.5.7), and am running into a problem during the configure stage. I configure with: ./configure --enable-framework=/Library/Frameworks --enable- universalsdk MACOSX_DEPLOYMENT_TARGET=10.5 --with-universal-archs=all - with-readline-dir=/usr/local because I want 64 and 32 bit, and I needed to install a 64 bit readline as a prerequisite. configure fails at: checking size of int... configure: error: cannot compute sizeof (int) I'm not reporting this as a bug because I know it's a problem with my path somewhere (a friend with an identical computer but slightly different setup was able to compile without a problem), but I don't know what paths to change. Any tips? Thanks, Adam From no.email at please.post Fri Jul 3 12:30:33 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 16:30:33 +0000 (UTC) Subject: Why re.match()? References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: In aahz at pythoncraft.com (Aahz) writes: >In article , kj wrote: >You may find this enlightening: >http://www.python.org/doc/1.4/lib/node52.html Indeed. Thank you. kj From http Fri Jul 3 12:33:35 2009 From: http (Paul Rubin) Date: 03 Jul 2009 09:33:35 -0700 Subject: Clarity vs. code reuse/generality References: Message-ID: <7xk52p4tgg.fsf@ruckus.brouhaha.com> kj writes: > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > ... The code looks confusing to me and in some sense incorrect. Suppose func(hi)==func(lo)==target. In this case the solver gives up and returns None even though it already has found a root. Also, the stuff with the sense parameter, and target_minus and target_plus looks messy. I do like to use cmp. I'd just write something like (untested): def _binary_search(lo, hi, func, target, epsilon): y_hi, y_lo = func(hi), func(lo) while True: x_new = (lo + hi) * 0.5 y_new = func(x_new) if abs(y_new - target) < epsilon: return x_new elif cmp(y_new, target) == cmp(y_hi, target): hi = x_new else: lo = x_new if lo == hi: return None This uses an extra couple function calls in that trivial case, of course. From lie.1296 at gmail.com Fri Jul 3 12:34:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 16:34:49 GMT Subject: Why re.match()? In-Reply-To: <025db0a6$0$20657$c3e8da3@news.astraweb.com> References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: > >> I'm sure that it is possible to find cases in which the *current* >> implementation of re.search() would be inefficient, but that's because >> this implementation is perverse, which, I guess, is ultimately the point >> of my original post. Why privilege the special case of a >> start-of-string anchor? > > Because wanting to see if a string matches from the beginning is a very > important and common special case. > I find the most oddest thing about re.match is that it have an implicit beginning anchor, but not implicit end anchor. I thought it was much more common to ensure that a string matches a certain pattern, than just matching the beginning. But everyone's mileages vary. From buzzard at urubu.freeserve.co.uk Fri Jul 3 12:35:45 2009 From: buzzard at urubu.freeserve.co.uk (duncan smith) Date: Fri, 03 Jul 2009 17:35:45 +0100 Subject: Is Psyco good for Python 2.6.1? In-Reply-To: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> References: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> Message-ID: Russ P. wrote: > I need to speed up some Python code, and I discovered Psyco. However, > the Psyco web page has not been updated since December 2007. Before I > go to the trouble of installing it, does anyone know if it is still > good for Python 2.6.1? Thanks. If you look at http://psyco.sourceforge.net/ it would seem so (a Windows binary for 2.6 is available). You might want to wait a little while because Psyco 2 is likely to be released very soon (tomorrow, the last I heard). Duncan From hobesh at gmail.com Fri Jul 3 12:53:19 2009 From: hobesh at gmail.com (Zach Hobesh) Date: Fri, 3 Jul 2009 09:53:19 -0700 Subject: Config files with different types In-Reply-To: References: Message-ID: yaml looks pretty interesting. Also, I wouldn't have to change much, I would still use the same function, and still output a dict. Thanks! -Zach On Thu, Jul 2, 2009 at 11:55 PM, Javier Collado wrote: > Hello, > > Have you considered using something that is already developed? > > You could take a look at this presentation for an overview of what's available: > http://us.pycon.org/2009/conference/schedule/event/5/ > > Anyway, let me explain that, since I "discovered" it, my favourite > format for configuration files is yaml (http://yaml.org/, > http://pyyaml.org/). It's easy to read, easy to write, available in > different programming languagues, etc. In addition to this, type > conversion is already in place so I think it covers your requirement. > For example: > > IIn [1]: import yaml > > In [2]: yaml.load("""name: person name > ? ...: age: 25 > ? ...: is_programmer: true""") > Out[2]: {'age': 25, 'is_programmer': True, 'name': 'person name'} > > Best regards, > ? ?Javier > > 2009/7/2 Zach Hobesh : >> Hi all, >> >> I've written a function that reads a specifically formatted text file >> and spits out a dictionary. ?Here's an example: >> >> config.txt: >> >> Destination = C:/Destination >> Overwrite = True >> >> >> Here's my function that takes 1 argument (text file) >> >> the_file = open(textfile,'r') >> linelist = the_file.read().split('\n') >> the_file.close() >> configs = {} >> for line in linelist: >> ? ? ? try: >> ? ? ? ? ? ? ?key,value = line.split('=') >> ? ? ? ? ? ? ?key.strip() >> ? ? ? ? ? ? ?value.strip() >> ? ? ? ? ? ? ?key.lower() >> ? ? ? ? ? ? ?value.lower() >> ? ? ? ? ? ? ?configs[key] = value >> >> ? ? ? except ValueError: >> ? ? ? ? ? ? ?break >> >> so I call this on my config file, and then I can refer back to any >> config in my script like this: >> >> shutil.move(your_file,configs['destination']) >> >> which I like because it's very clear and readable. >> >> So this works great for simple text config files. ?Here's how I want >> to improve it: >> >> I want to be able to look at the value and determine what type it >> SHOULD be. ?Right now, configs['overwrite'] = 'true' (a string) when >> it might be more useful as a boolean. ?Is there a quick way to do >> this? ?I'd also like to able to read '1' as an in, '1.0' as a float, >> etc... >> >> I remember once I saw a script that took a string and tried int(), >> float() wrapped in a try except, but I was wondering if there was a >> more direct way. >> >> Thanks in advance, >> >> Zach >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > From python at mrabarnett.plus.com Fri Jul 3 12:54:10 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 17:54:10 +0100 Subject: getting rid of =?UTF-8?B?4oCU?= In-Reply-To: <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> Message-ID: <4A4E37B2.10300@mrabarnett.plus.com> Tep wrote: > On 3 Jul., 16:58, "Mark Tolonen" wrote: >> "Tep" wrote in message >> >> news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... >> >> >> >> >> >>> On 3 Jul., 06:40, Simon Forman wrote: >>>> On Jul 2, 4:31 am, Tep wrote: >> [snip] >>>>>>>> how can I replace '?' sign from string? Or do split at that >>>>>>>> character? >>>>>>>> Getting unicode error if I try to do it: >>>>>>>> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in >>>>>>>> position >>>>>>>> 1: ordinal not in range(128) >>>>>>>> Thanks, Pet >>>>>>>> script is # -*- coding: UTF-8 -*- >> [snip] >>>> I just tried a bit of your code above in my interpreter here and it >>>> worked fine: >>>> |>>> data = 'foo ? bar' >>>> |>>> data.split('?') >>>> |['foo ', ' bar'] >>>> |>>> data = u'foo ? bar' >>> |>>> data.split(u'?') >>>> |[u'foo ', u' bar'] >>>> Figure out the smallest piece of "html source code" that causes the >>>> problem and include that with your next post. >>> The problem was, I've converted "html source code" to unicode object >>> and didn't encoded to utf-8 back, before using split... >>> Thanks for help and sorry for not so smart question >>> Pet >> You'd still benefit from posting some code. You shouldn't be converting > > I've posted code below > >> back to utf-8 to do a split, you should be using a Unicode string with split >> on the Unicode version of the "html source code". Also make sure your file >> is actually saved in the encoding you declare. I print the encoding of your >> symbol in two encodings to illustrate why I suspect this. > > File was indeed in windows-1252, I've changed this. For errors see > below > >> Below, assume "data" is your "html source code" as a Unicode string: >> >> # -*- coding: UTF-8 -*- >> data = u'foo ? bar' >> print repr(u'?'.encode('utf-8')) >> print repr(u'?'.encode('windows-1252')) >> print data.split(u'?') >> print data.split('?') >> >> OUTPUT: >> >> '\xe2\x80\x94' >> '\x97' >> [u'foo ', u' bar'] >> Traceback (most recent call last): >> File >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", >> line 427, in ImportFile >> exec codeObj in __main__.__dict__ >> File "", line 1, in >> File "x.py", line 6, in >> print data.split('?') >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: >> ordinal not in range(128) >> >> Note that using the Unicode string in split() works. Also note the decode >> byte in the error message when using a non-Unicode string to split the >> Unicode data. In your original error message the decode byte that caused an >> error was 0x97, which is 'EM DASH' in Windows-1252 encoding. Make sure to >> save your source code in the encoding you declare. If I save the above >> script in windows-1252 encoding and change the coding line to windows-1252 I >> get the same results, but the decode byte is 0x97. >> >> # coding: windows-1252 >> data = u'foo ? bar' >> print repr(u'?'.encode('utf-8')) >> print repr(u'?'.encode('windows-1252')) >> print data.split(u'?') >> print data.split('?') >> >> '\xe2\x80\x94' >> '\x97' >> [u'foo ', u' bar'] >> Traceback (most recent call last): >> File >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", >> line 427, in ImportFile >> exec codeObj in __main__.__dict__ >> File "", line 1, in >> File "x.py", line 6, in >> print data.split('?) >> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: >> ordinal not in range(128) >> >> -Mark > > #! /usr/bin/python > # -*- coding: UTF-8 -*- > import urllib2 > import re > def getTitle(input): > title = re.search('(.*?)', input) The input is Unicode, so it's probably better for the regular expression to also be Unicode: title = re.search(u'(.*?)', input) (In the current implementation it actually doesn't matter.) > title = title.group(1) > print "FULL TITLE", title.encode('UTF-8') > parts = title.split(' ? ') The title is Unicode, so the string with which you're splitting should also be Unicode: parts = title.split(u' ? ') > return parts[0] > > > def getWebPage(url): > user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' > headers = { 'User-Agent' : user_agent } > req = urllib2.Request(url, '', headers) > response = urllib2.urlopen(req) > the_page = unicode(response.read(), 'UTF-8') > return the_page > > > def main(): > url = "http://bg.wikipedia.org/wiki/ > %D0%91%D0%B0%D1%85%D1%80%D0%B5%D0%B9%D0%BD" > title = getTitle(getWebPage(url)) > print title[0] > > > if __name__ == "__main__": > main() > > > Traceback (most recent call last): > File "C:\user\Projects\test\src\new_main.py", line 29, in > main() > File "C:\user\Projects\test\src\new_main.py", line 24, in main > title = getTitle(getWebPage(url)) > FULL TITLE ?????????????? ??? ?????????????????? > File "C:\user\Projects\test\src\new_main.py", line 9, in getTitle > parts = title.split(' ??? ') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position > 1: ordinal not in range(128) From kd at kendyck.com Fri Jul 3 13:00:31 2009 From: kd at kendyck.com (Ken Dyck) Date: Fri, 3 Jul 2009 10:00:31 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? References: Message-ID: <08bb0349-4bc7-448c-9ae5-49eca6d7f3ae@g7g2000prg.googlegroups.com> On Jul 2, 6:17?pm, Allen Fowler wrote: > Since I need to work with other platforms, pickle is out... ?what are the alternatives? ?XML? JSON? Don't forget YAML (http://yaml.org). Libraries available for Python and .NET, among others. -Ken From schickb at gmail.com Fri Jul 3 13:03:32 2009 From: schickb at gmail.com (Brad) Date: Fri, 3 Jul 2009 10:03:32 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: On Jul 3, 12:57?am, Steven D'Aprano wrote: > I've never needed such a split function, and I don't like the name, and > the functionality isn't general enough. I'd prefer something which splits > the input sequence into as many sublists as necessary, according to the > output of the key function. That's not a bad idea, I'll have to experiment with the alternatives. My thought process for this, however, was that filter itself already splits the sequence and it would have been more useful had it not thrown away "half" of what it discovers. It could have been written to returned two sequences with very litter perf hit for all but very large input sequences, and been useful in more situations. What I *really* wanted was a way to make filter itself more useful, since it seems a bit silly to have two very similar functions. Maybe this would be difficult to get into the core, but how about this idea: Rename the current filter function to something like "split" or "partition" (which I agree may be a better name) and modify it to return the desired true and false sequences. Then recreate the existing "filter" function with a wrapper that throws away the false sequence. Here are two simplified re-creations of situations where I could have used partition (aka split): words = ['this', 'is', 'a', 'bunch', 'of', 'words'] short, long = partition(words, lambda w: len(w) < 3) d = {1 : 'w', 2 : 'x' ,3 : 'y' ,4 : 'z'} keys = [1, 3, 4, 9] found, missing = partition(keys, d.has_key) There are probably a dozen other approaches, but the existing "filter" is fast, clear, and *almost* good enough. So when is this useful in general: Whenever filter itself is useful, but you want to use both sides of the partitioning work it already does. -Brad From http Fri Jul 3 13:14:00 2009 From: http (Paul Rubin) Date: 03 Jul 2009 10:14:00 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xr5wx8zaf.fsf@ruckus.brouhaha.com> Brad writes: > Maybe this would be difficult to get into the core, but how about this > idea: Rename the current filter function to something like "split" or > "partition" (which I agree may be a better name) and modify it to > return the desired true and false sequences. Then recreate the > existing "filter" function with a wrapper that throws away the false > sequence. This isn't so attractive, since filter takes a sequence input but returns a list. So if I have an iterator that produces a billion elements of which I expect three to satisfy some predicate, then xs = filter(func, seq) as currently implemented will build a 3-element list and return it. Under your suggestion, it would also build and throw away an (almost) billion element list. From lie.1296 at gmail.com Fri Jul 3 13:27:59 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 17:27:59 GMT Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: Brad wrote: > On Jul 3, 12:57 am, Steven D'Aprano cybersource.com.au> wrote: >> I've never needed such a split function, and I don't like the name, and >> the functionality isn't general enough. I'd prefer something which splits >> the input sequence into as many sublists as necessary, according to the >> output of the key function. > > That's not a bad idea, I'll have to experiment with the alternatives. > My thought process for this, however, was that filter itself already > splits the sequence and it would have been more useful had it not > thrown away "half" of what it discovers. It could have been written to > returned two sequences with very litter perf hit for all but very > large input sequences, and been useful in more situations. What I > *really* wanted was a way to make filter itself more useful, since it > seems a bit silly to have two very similar functions. It's not that easy. filter is nearly by definition a lazy function. The various split/group functions is impossible to be made an efficient iterator, since they must traverse the whole list before being sure that they have finished the group/part of the split (or even to be sure that they have all the group keys). > Maybe this would be difficult to get into the core, but how about this > idea: Rename the current filter function to something like "split" or > "partition" (which I agree may be a better name) and modify it to > return the desired true and false sequences. Then recreate the > existing "filter" function with a wrapper that throws away the false > sequence. filter has a very deep root in functional programming, I doubt it will change any time soon. Also, consider infinite iterators. With the "filter as wrapper to partition" scheme, it is impossible for split/group/partition to use infinite iterator. From fiafulli at hotmail.it Fri Jul 3 13:39:27 2009 From: fiafulli at hotmail.it (fiafulli) Date: Fri, 3 Jul 2009 19:39:27 +0200 Subject: PyGtkSourceView / PyScintilla Message-ID: <4a4e41d5$0$836$4fafbaef@reader5.news.tin.it> Hello to all. I would have need of the recent version (possibly most recent) than one of the two controls in subject. I think that wrapper for scintilla it would be better but creed that the development for pygtk is stopped. therefore I'm more inclined for GtkSourceView (than today it is to the version 2.6.0 http://projects.gnome.org/gtksourceview/). In the specific I need the version for windows (for that for linux I have not still tried but I must not have problems) of which a setup exists for python 2.5 but it is stop to version 2.2 http://ftp.gnome.org/pub/gnome/binaries/win32/pygtksourceview/2.2/). Someone would be to post the compiled in issue (if it were last version would be fantastic!)? or to explain step to me step as to compile it to me alone on windows (I have tried installing mingw, msys, autoconf, automake, libtools, and all the other ones of time in time, I have begun to compile but they are stopped to me on reference lacking pygobject 2.15 or something of the sort. I lost all morning!)? The version of python that I use is the 2.5.4. on windows xp sp3 (and ubuntu 9.04) Thanks in advance, Francesco p.s.: sorry for my bad english From user at example.net Fri Jul 3 13:42:39 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 19:42:39 +0200 Subject: stringio+tarfile In-Reply-To: References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4debb1$0$1106$4fafbaef@reader3.news.tin.it> Message-ID: <4a4e4311$0$47538$4fafbaef@reader1.news.tin.it> Peter Otten wrote: > gettarinfo() expects a real file, not a file-like object. > You have to create your TarInfo manually. ok... which attributes are mandatory, and which optional? > I recommend that you have a look into the tarfile module's source code. > i will try... but: $ cat /usr/lib/python2.3/tarfile.py | wc -l 1938 wow! it'll take some time ;-) > The following seems to work: > > import sys > import time > import tarfile > import StringIO > > sf1 = "first.txt", StringIO.StringIO("one one\n") > sf2 = "second.txt", StringIO.StringIO("two\n") > tf = StringIO.StringIO() > > tar = tarfile.open(fileobj=tf , mode="w") > > mtime = time.time() > for name, f in [sf1 , sf2]: > ti = tarfile.TarInfo(name) > ti.size = f.len > ti.mtime = mtime > # add more attributes as needed > tar.addfile(ti, f) > > sys.stdout.write(tf.getvalue()) > > Peter > much obliged mr otten bye From sajmikins at gmail.com Fri Jul 3 13:44:52 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 10:44:52 -0700 (PDT) Subject: PSP Caching References: Message-ID: <3e1c0979-fb6b-4d6e-80e5-a4af8d5fd96a@a36g2000yqc.googlegroups.com> On Jul 3, 5:18?am, Johnson Mpeirwe wrote: > Hello All, > > How do I stop caching of Python Server Pages (or whatever causes changes > in a page not to be noticed in a web browser)? I am new to developing > web applications in Python and after looking at implementations of PSP > like Spyce (which I believed introduces new unnecessary non-PSP syntax), > I decided to write my own PSP applications from scratch. When I modify a > file, I keep getting the old results until I intentionally introduce an > error (e.g parse error) and correct it after to have the changes > noticed. There's no proxy (I am working on a windows machine unplugged > from the network). I have Googled and no documents seem to talk about > this. Is there any particular mod_python directive I must set in my > Apache configuration to fix this? > > Any help will be highly appreciated. > > Johnson I don't know much about caching with apache, but the answer mght be on this page: http://httpd.apache.org/docs/2.2/caching.html Meanwhile, couldn't you just send apache a restart signal when you modify your code? HTH, ~Simon From Scott.Daniels at Acm.Org Fri Jul 3 13:47:15 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 03 Jul 2009 10:47:15 -0700 Subject: Sequence splitting In-Reply-To: <025dab69$0$20657$c3e8da3@news.astraweb.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I've never needed such a split function, and I don't like the name, and > the functionality isn't general enough. I'd prefer something which splits > the input sequence into as many sublists as necessary, according to the > output of the key function. Something like itertools.groupby(), except it > runs through the entire sequence and collates all the elements with > identical keys. > > splitby(range(10), lambda n: n%3) > => [ (0, [0, 3, 6, 9]), > (1, [1, 4, 7]), > (2, [2, 5, 8]) ] > > Your split() would be nearly equivalent to this with a key function that > returns a Boolean. Well, here is my go at doing the original with iterators: def splitter(source, test=bool): a, b = itertools.tee((x, test(x)) for x in source) return (data for data, decision in a if decision), ( data for data, decision in b if not decision) This has the advantage that it can operate on infinite lists. For something like splitby for grouping, I seem to need to know the cases up front: def _make_gen(particular, src): return (x for x, c in src if c == particular) def splitby(source, cases, case): '''Produce a dict of generators for case(el) for el in source''' decided = itertools.tee(((x, case(x)) for x in source), len(cases)) return dict((c, _make_gen(c, src)) for c, src in zip(cases, decided)) example: def classify(n): '''Least prime factor of a few''' for prime in [2, 3, 5, 7]: if n % prime == 0: return prime return 0 for k,g in splitby(range(50), (2, 3, 5, 7, 0), classify).items(): print('%s: %s' % (k, list(g))) 0: [1, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] 2: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48] 3: [3, 9, 15, 21, 27, 33, 39, 45] 5: [5, 25, 35] 7: [7, 49] --Scott David Daniels Scott.Daniels at Acm.Org From alan.isaac at gmail.com Fri Jul 3 13:50:23 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 17:50:23 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: > In Alan G Isaac writes: >> 1. Don't use assertions to test argument values! On 7/3/2009 12:19 PM kj apparently wrote: > Out of curiosity, where does this come from? http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_stmt "The current code generator emits no code for an assert statement when optimization is requested at compile time." Alan Isaac From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 14:03:07 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 18:03:07 GMT Subject: Clarity vs. code reuse/generality References: Message-ID: <025e3974$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 16:19:22 +0000, kj wrote: > In Alan G Isaac > writes: > >>1. Don't use assertions to test argument values! > > Out of curiosity, where does this come from? Assertions are disabled when you run Python with the -O (optimise) flag. If you rely on assertions to verify data, then any time the user runs python with -O your code will be running without error checking. assert should be used to verify program logic, not to sanitize data. -- Steven From kee at kagi.com Fri Jul 3 14:08:19 2009 From: kee at kagi.com (Kee Nethery) Date: Fri, 3 Jul 2009 11:08:19 -0700 Subject: Python debugger In-Reply-To: <740981.83964.qm@web7901.mail.in.yahoo.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> Message-ID: <4A19FA5C-C135-480F-BFD1-82B7AF1500A8@kagi.com> It's not free but I like the debugger in Komodo IDE. Lets me simulate a web connection, lets me step through the code and examine the variables as it executes, can be run remotely (have not played with that aspect yet). Does variable inspection of the variables so you can dive into the parts of arrays and dictionaries to see what the (for example) 5th item of the 4th item named "blah" is set to and what type of data element it is (int, unicode, etc). I find it tremendously useful as a newbie to Python. Kee Nethery From patrick.just4fun at gmail.com Fri Jul 3 14:18:51 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Fri, 03 Jul 2009 20:18:51 +0200 Subject: Reversible Debugging Message-ID: <4A4E4B8B.7090104@gmail.com> Hello, I am interested if there are any python modules, that supports reversible debugging aka stepping backwards. Any links or ideas would be helpful, because I am thinking of implementing something like that. Thanks in advance, Patrick From petshmidt at googlemail.com Fri Jul 3 14:34:21 2009 From: petshmidt at googlemail.com (Tep) Date: Fri, 3 Jul 2009 11:34:21 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> Message-ID: On 3 Jul., 18:54, MRAB wrote: > Tep wrote: > > On 3 Jul., 16:58, "Mark Tolonen" wrote: > >> "Tep" wrote in message > > >>news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... > > >>> On 3 Jul., 06:40, Simon Forman wrote: > >>>> On Jul 2, 4:31 am, Tep wrote: > >> [snip] > >>>>>>>> how can I replace '?' sign from string? Or do split at that > >>>>>>>> character? > >>>>>>>> Getting unicode error if I try to do it: > >>>>>>>> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in > >>>>>>>> position > >>>>>>>> 1: ordinal not in range(128) > >>>>>>>> Thanks, Pet > >>>>>>>> script is # -*- coding: UTF-8 -*- > >> [snip] > >>>> I just tried a bit of your code above in my interpreter here and it > >>>> worked fine: > >>>> |>>> data = 'foo ? bar' > >>>> |>>> data.split('?') > >>>> |['foo ', ' bar'] > >>>> |>>> data = u'foo ? bar' > >>> |>>> data.split(u'?') > >>>> |[u'foo ', u' bar'] > >>>> Figure out the smallest piece of "html source code" that causes the > >>>> problem and include that with your next post. > >>> The problem was, I've converted "html source code" to unicode object > >>> and didn't encoded to utf-8 back, before using split... > >>> Thanks for help and sorry for not so smart question > >>> Pet > >> You'd still benefit from posting some code. ?You shouldn't be converting > > > I've posted code below > > >> back to utf-8 to do a split, you should be using a Unicode string with split > >> on the Unicode version of the "html source code". ?Also make sure your file > >> is actually saved in the encoding you declare. ?I print the encoding of your > >> symbol in two encodings to illustrate why I suspect this. > > > File was indeed in windows-1252, I've changed this. For errors see > > below > > >> Below, assume "data" is your "html source code" as a Unicode string: > > >> # -*- coding: UTF-8 -*- > >> data = u'foo ? bar' > >> print repr(u'?'.encode('utf-8')) > >> print repr(u'?'.encode('windows-1252')) > >> print data.split(u'?') > >> print data.split('?') > > >> OUTPUT: > > >> '\xe2\x80\x94' > >> '\x97' > >> [u'foo ', u' bar'] > >> Traceback (most recent call last): > >> ? File > >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > >> line 427, in ImportFile > >> ? ? exec codeObj in __main__.__dict__ > >> ? File "", line 1, in > >> ? File "x.py", line 6, in > >> ? ? print data.split('?') > >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: > >> ordinal not in range(128) > > >> Note that using the Unicode string in split() works. ?Also note the decode > >> byte in the error message when using a non-Unicode string to split the > >> Unicode data. ?In your original error message the decode byte that caused an > >> error was 0x97, which is 'EM DASH' in Windows-1252 encoding. ?Make sure to > >> save your source code in the encoding you declare. ?If I save the above > >> script in windows-1252 encoding and change the coding line to windows-1252 I > >> get the same results, but the decode byte is 0x97. > > >> # coding: windows-1252 > >> data = u'foo ? bar' > >> print repr(u'?'.encode('utf-8')) > >> print repr(u'?'.encode('windows-1252')) > >> print data.split(u'?') > >> print data.split('?') > > >> '\xe2\x80\x94' > >> '\x97' > >> [u'foo ', u' bar'] > >> Traceback (most recent call last): > >> ? File > >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > >> line 427, in ImportFile > >> ? ? exec codeObj in __main__.__dict__ > >> ? File "", line 1, in > >> ? File "x.py", line 6, in > >> ? ? print data.split('?) > >> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: > >> ordinal not in range(128) > > >> -Mark > > > #! /usr/bin/python > > # -*- coding: UTF-8 -*- > > import urllib2 > > import re > > def getTitle(input): > > ? ? title = re.search('(.*?)', input) > > The input is Unicode, so it's probably better for the regular expression > to also be Unicode: > > ? ? ?title = re.search(u'(.*?)', input) > > (In the current implementation it actually doesn't matter.) > > > ? ? title = title.group(1) > > ? ? print "FULL TITLE", title.encode('UTF-8') > > ? ? parts = title.split(' ? ') > > The title is Unicode, so the string with which you're splitting should > also be Unicode: > > ? ? ?parts = title.split(u' ? ') > Oh, so simple. I'm new to python and still feel uncomfortable with unicode stuff. Thanks to all for help! > > > > ? ? return parts[0] > > > def getWebPage(url): > > ? ? user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' > > ? ? headers = { 'User-Agent' : user_agent } > > ? ? req = urllib2.Request(url, '', headers) > > ? ? response = urllib2.urlopen(req) > > ? ? the_page = unicode(response.read(), 'UTF-8') > > ? ? return the_page > > > def main(): > > ? ? url = "http://bg.wikipedia.org/wiki/ > > %D0%91%D0%B0%D1%85%D1%80%D0%B5%D0%B9%D0%BD" > > ? ? title = getTitle(getWebPage(url)) > > ? ? print title[0] > > > if __name__ == "__main__": > > ? ? main() > > > Traceback (most recent call last): > > ? File "C:\user\Projects\test\src\new_main.py", line 29, in > > ? ? main() > > ? File "C:\user\Projects\test\src\new_main.py", line 24, in main > > ? ? title = getTitle(getWebPage(url)) > > FULL TITLE ?????????????? ??? ????????????????? > > ? File "C:\user\Projects\test\src\new_main.py", line 9, in getTitle > > ? ? parts = title.split(' ??? ') > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position > > 1: ordinal not in range(128) From charles at declareSub.com Fri Jul 3 14:50:48 2009 From: charles at declareSub.com (Charles Yeomans) Date: Fri, 3 Jul 2009 14:50:48 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <025e3974$0$20657$c3e8da3@news.astraweb.com> References: <025e3974$0$20657$c3e8da3@news.astraweb.com> Message-ID: <50230590-3F00-45ED-A1B2-DCA85366C416@declareSub.com> On Jul 3, 2009, at 2:03 PM, Steven D'Aprano wrote: > On Fri, 03 Jul 2009 16:19:22 +0000, kj wrote: > >> In Alan G Isaac >> writes: >> >>> 1. Don't use assertions to test argument values! >> >> Out of curiosity, where does this come from? > > Assertions are disabled when you run Python with the -O (optimise) > flag. > If you rely on assertions to verify data, then any time the user runs > python with -O your code will be running without error checking. > > assert should be used to verify program logic, not to sanitize data. I wouldn't describe the use of an assert statement in the original code as data sanitizing, but rather as a precondition. And with that description, the use of an assert statement that might be compiled away is not unreasonable; indeed, it certainly is so in the context of design by contract. Charles Yeomans From tjreedy at udel.edu Fri Jul 3 14:57:20 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 14:57:20 -0400 Subject: Python 3.0 (pinin' for the fjords) In-Reply-To: References: Message-ID: Alan G Isaac wrote: > Using the 3.1 Windows installer, I chose that I did not > want the extensions registered, and the installer > unregistered the .py and .pyw extensions (which I had wanted > to keep associated with Python 2.6). > > Is this intentional? I suspect not, but Martin is the one to answer. If you had started a new thread for this new topic "3.1 Windows Installer trashes extension registrations" this would have been seen more easily ;-) From mwilson at the-wire.com Fri Jul 3 15:08:35 2009 From: mwilson at the-wire.com (Mel) Date: Fri, 03 Jul 2009 15:08:35 -0400 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > The most important difference between my suggestion and that of the OP is > that he limited the key function to something which returns a truth > value, while I'm looking for something more general which can split the > input into an arbitrary number of collated sublists. Generally along the lines of def scatter (seq, criterion): lists = {} for s in seq: lists.setdefault (criterion (s), []).append (s) return lists modulo a defaultdict or some such ? Mel. > > > From dudeja.rajat at gmail.com Fri Jul 3 15:10:06 2009 From: dudeja.rajat at gmail.com (dudeja.rajat at gmail.com) Date: Sat, 4 Jul 2009 00:40:06 +0530 Subject: [Help] Python implmenentation of finding process's command line parameters. Message-ID: I'm looking for some python implementation of finding the command line parameters of a process on Windows XP SP3 machine. I found some links but they all point me to the direction of c/c++ implememtations. I've seen WMI, but this takes time to load. So I just though if some one has done implementation and could pointing me in some right direction. Regards, Rajat -------------- next part -------------- An HTML attachment was scrubbed... URL: From nosklo at gmail.com Fri Jul 3 15:12:39 2009 From: nosklo at gmail.com (Clovis Fabricio) Date: Fri, 3 Jul 2009 16:12:39 -0300 Subject: Python debugger In-Reply-To: <4A19FA5C-C135-480F-BFD1-82B7AF1500A8@kagi.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> <4A19FA5C-C135-480F-BFD1-82B7AF1500A8@kagi.com> Message-ID: <9187a60d0907031212m7f91024m46b7d440e0815f29@mail.gmail.com> 2009/7/3 Kee Nethery : > It's not free but I like the debugger in Komodo IDE. > Lets me simulate a web connection, lets me step through the code and examine > the variables as it executes, can be run remotely (have not played with that > aspect yet). > Does variable inspection of the variables so you can dive into the parts of > arrays and dictionaries to see what the (for example) 5th item of the 4th > item named "blah" is set to and what type of data element it is (int, > unicode, etc). I find it tremendously useful as a newbie to Python. > Kee Nethery Winpdb is free and offers all those features. From mail at timgolden.me.uk Fri Jul 3 15:15:37 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 03 Jul 2009 20:15:37 +0100 Subject: [Help] Python implmenentation of finding process's command line parameters. In-Reply-To: References: Message-ID: <4A4E58D9.6050806@timgolden.me.uk> dudeja.rajat at gmail.com wrote: > I'm looking for some python implementation of finding the command line > parameters of a process on Windows XP SP3 machine. I found some links but > they all point me to the direction of c/c++ implememtations. > > I've seen WMI, but this takes time to load. So I just though if some one has > done implementation and could pointing me in some right direction. Someone recently posted an example using PSI [1] which seemed quite fast. TJG [1] http://www.psychofx.com/psi/trac/wiki/ From mr.spoon21 at gmail.com Fri Jul 3 15:23:20 2009 From: mr.spoon21 at gmail.com (Mr.SpOOn) Date: Fri, 3 Jul 2009 21:23:20 +0200 Subject: Getting input the scanf way In-Reply-To: References: Message-ID: <8f67b6f80907031223u767044ara169ddf137b5207a@mail.gmail.com> Thanks, I think raw_input() will work just fine for my needs. From tjreedy at udel.edu Fri Jul 3 15:23:47 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 15:23:47 -0400 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: Brad wrote: > On Jul 2, 8:17 pm, "Pablo Torres N." wrote: >> This sounds like it belongs to the python-ideas list. I suggest >> posting there for better feedback, since the core developers check >> that list more often than this one. > > I tried posting on python-ideas and received a "You are not allowed to > post to this mailing list" reply. Perhaps because I am posting through > Google groups? Or maybe one must be an approved member to post? Spammers post thru Google groups Either subscribe or access via news.gmane.org as gmane.comp.python.ideas. As to your main question: this was discuss some years ago. There did not seem enough use cases for 'bifilter' or 'distributor' to warrent inclusion in the stdlib. Easy enough to do on one's own for those few cases. Just filter twice. Usually, one wants to do one thing or another to each item with the original scan for item in collection: if pred(item): proc_true(item) else: proc_false(item) Having proc_true and proc_false both be append(item) is a special case. Note that filter has been changed from returning a list to returning an iterator. So a proposal for a function to return two lists would seem backwards. A function that returns two iterators would be possible. Something like itertools.tee except that each item would go on one tee or the other instead of both. It would have two queues (deques) instead of one. And it should be lazy -- items should only be pulled from the original iterator when an item is requested from an empty queue. I am not sure it is worth it. In the worst case, all items are say 'True' and you request 'False' items and all items get copied to the True queue before the False iterator raised StopIteration. The way to avoid calling keyfunc(item) twice on each item is to make a new list first: newlist = list(map(keyfunc,iterable)). Then scan newlist twice. If you write such a function, first in Python, then C, you can register code on PyPI and see how much interest it gets. Terry Jan Reedy From tjreedy at udel.edu Fri Jul 3 15:27:25 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 15:27:25 -0400 Subject: Sequence splitting In-Reply-To: <7xr5wx8zaf.fsf@ruckus.brouhaha.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7xr5wx8zaf.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Brad writes: >> Maybe this would be difficult to get into the core, but how about this >> idea: Rename the current filter function to something like "split" or >> "partition" (which I agree may be a better name) and modify it to >> return the desired true and false sequences. Then recreate the >> existing "filter" function with a wrapper that throws away the false >> sequence. > > This isn't so attractive, since filter takes a sequence input but > returns a list. In modern Python (py3), it returns an iterator. From gnewsg at gmail.com Fri Jul 3 15:33:48 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Fri, 3 Jul 2009 12:33:48 -0700 (PDT) Subject: Python implmenentation of finding process's command line parameters. References: Message-ID: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> On 3 Lug, 21:15, Tim Golden wrote: > dudeja.ra... at gmail.com wrote: > > I'm looking for some python implementation of finding the command line > > parameters of a process on Windows XP SP3 machine. I found some links but > > they all point me to the direction of c/c++ implememtations. > > > I've seen WMI, but this takes time to load. So I just though if some one has > > done implementation and could pointing me in some right direction. > > Someone recently posted an example using PSI [1] which seemed > quite fast. > > TJG > > [1]http://www.psychofx.com/psi/trac/wiki/ If I'm not mistaken psi shouldn't support Windows. You can use psutil: http://code.google.com/p/psutil/ >>> import psutil >>> p = psutil.Process(7055) >>> p.cmdline ['/usr/bin/python', '-O'] >>> --- Giampaolo http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil/ From http Fri Jul 3 15:41:20 2009 From: http (Paul Rubin) Date: 03 Jul 2009 12:41:20 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7xr5wx8zaf.fsf@ruckus.brouhaha.com> Message-ID: <7xr5wximfz.fsf@ruckus.brouhaha.com> Terry Reedy writes: > > This isn't so attractive, since filter takes a sequence input but > > returns a list. > > In modern Python (py3), it returns an iterator. Still not much help in the proposed version that would return two iterators. From dudeja.rajat at gmail.com Fri Jul 3 15:41:30 2009 From: dudeja.rajat at gmail.com (dudeja.rajat at gmail.com) Date: Sat, 4 Jul 2009 01:11:30 +0530 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: I have had a look at psutil, however, this only works with Python 2.6.x and above. We are using Python 2.5.2 and updating python could pose certain problems. So, I'm not keep on using psutils. -- Regrads, Rajat -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Fri Jul 3 15:48:20 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 03 Jul 2009 20:48:20 +0100 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: <4A4E6084.6030001@timgolden.me.uk> Giampaolo Rodola' wrote: > On 3 Lug, 21:15, Tim Golden wrote: >> dudeja.ra... at gmail.com wrote: >>> I'm looking for some python implementation of finding the command line >>> parameters of a process on Windows XP SP3 machine. I found some links but >>> they all point me to the direction of c/c++ implememtations. >>> I've seen WMI, but this takes time to load. So I just though if some one has >>> done implementation and could pointing me in some right direction. >> Someone recently posted an example using PSI [1] which seemed >> quite fast. >> >> TJG >> >> [1]http://www.psychofx.com/psi/trac/wiki/ > > If I'm not mistaken psi shouldn't support Windows. > You can use psutil: > http://code.google.com/p/psutil/ Sorry. Exactly what I meant. (Should have checked for the exact post rather than trusting to my memory). TKG From mail at timgolden.me.uk Fri Jul 3 15:48:32 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 03 Jul 2009 20:48:32 +0100 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: <4A4E6090.4060102@timgolden.me.uk> Giampaolo Rodola' wrote: > On 3 Lug, 21:15, Tim Golden wrote: >> dudeja.ra... at gmail.com wrote: >>> I'm looking for some python implementation of finding the command line >>> parameters of a process on Windows XP SP3 machine. I found some links but >>> they all point me to the direction of c/c++ implememtations. >>> I've seen WMI, but this takes time to load. So I just though if some one has >>> done implementation and could pointing me in some right direction. >> Someone recently posted an example using PSI [1] which seemed >> quite fast. >> >> TJG >> >> [1]http://www.psychofx.com/psi/trac/wiki/ > > If I'm not mistaken psi shouldn't support Windows. > You can use psutil: > http://code.google.com/p/psutil/ Sorry. Exactly what I meant. (Should have checked for the exact post rather than trusting to my memory). TJG From tjgolden at gmail.com Fri Jul 3 15:50:04 2009 From: tjgolden at gmail.com (Tim Golden) Date: Fri, 03 Jul 2009 20:50:04 +0100 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: <4A4E60EC.7030400@gmail.com> dudeja.rajat at gmail.com wrote: > I have had a look at psutil, however, this only works with Python 2.6.x and > above. We are using Python 2.5.2 and updating python could pose certain > problems. So, I'm not keep on using psutils. Doesn't look as if there's anything 2.6-specific there. Maybe the maintainer can comment, but I suspect you'd just need to recompile and link against 2.5 TJG From tjreedy at udel.edu Fri Jul 3 15:51:56 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 15:51:56 -0400 Subject: pep 8 constants In-Reply-To: <4A4E135E.5090400@harvee.org> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> Message-ID: Eric S. Johansson wrote: > Horace Blegg wrote: >> I've been kinda following this. I have a cousin who is permanently wheel >> chair bound and doesn't have perfect control of her hands, but still >> manages to use a computer and interact with society. However, the >> idea/thought of disabled programmers was new to me/hadn't ever occurred >> to me. >> >> You say that using your hands is painful, but what about your feet? >> Wouldn't it be possible to rig up some kind of foot peddle for >> shift/caps lock? Kinda like the power peddle used with sowing machines, >> so the hands are free to hold fabric. >> >> I don't mean this in a condescending manor, and I apologize if you take >> it as such. I'm genuinely curious if you think something like this could >> work. >> >> The way I was envisioning it working last night (and I haven't the >> faintest clue how SR works, nor have I ever used SR) was that you would >> hit the foot peddle, which would tell the SR program to capitalize the >> first letter of the next word (a smart shift, basically, so you don't >> end up doing something like ... WONderland -or- "stocks are up 1,0))% >> TOday".) >> >> Possible? Stupid? >> > it's not stupid. > > People have used foot pedals for decades for a variety of controls. I don't > think foot pedals would work for me because when I am dictating, I pace. > Standing, sitting, I pace. With a cord headset, I'm forced to stay within about > 4 feet of the computer. But what I'm using a Bluetooth headset, I will sometimes > ramble as far as 10 or 15 feet from the computer. It helps if I make the font > larger so I can glance over and see what kind of errors I've gotten. > > I really love a Bluetooth headset with speech recognition. It's so liberating. > > Your question about foot pedals makes me think of alternative. would it make > sense to have a handheld keyboard which would be used for command-and-control > functionality or as an adjunct to speech recognition use? It would have to be > designed in such a way that it doesn't aggravate a hand injury which may not be > possible. Anyway, just thinking out loud. As long as we are thinking wildly, how about a pair of bluetooth-connected switches on a belt or chest band that you could press with your arms or elbows -- no hands involved. Sort of become a walking mouse ;-). Terry From gnewsg at gmail.com Fri Jul 3 15:57:44 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Fri, 3 Jul 2009 12:57:44 -0700 (PDT) Subject: Python implmenentation of finding process's command line parameters. References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: On 3 Lug, 21:50, Tim Golden wrote: > Sorry. Exactly what I meant. (Should have checked for > the exact post rather than trusting to my memory) No prolem. > dudeja.ra... at gmail.com wrote: > > I have had a look at psutil, however, this only works with Python 2.6.x and > > above. We are using Python 2.5.2 and updating python could pose certain > > problems. So, I'm not keep on using psutils. > > Doesn't look as if there's anything 2.6-specific there. > Maybe the maintainer can comment, but I suspect you'd > just need to recompile and link against 2.5 > > TJG psutil works on Python 2.4, 2.5 and 2.6. If you are on Windows, you can get the precompiled binary for Python 2.5 from here: http://code.google.com/p/psutil/downloads/list --- Giampaolo http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil/ From sajmikins at gmail.com Fri Jul 3 16:04:21 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 13:04:21 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> Message-ID: On Jul 3, 2:09?am, Lie Ryan wrote: > Simon Forman wrote: > > On Jul 2, 3:57 pm, Scott David Daniels wrote: > >> Duncan Booth wrote: > >>> Simon Forman wrote: > >>>> ... > >>>> if self.higher is self.lower is None: return > >>>> ... > >>> As a matter of style however I wouldn't use the shorthand to run two 'is' > >>> comparisons together, I'd write that out in full if it was actually needed > >>> here. > >> Speaking only to the style issue, when I've wanted to do something like > >> that, I find: > > >> ? ? ? ?if self.higher is None is self.lower: > >> ? ? ? ? ? ?... > > >> more readable, by making clear they are both being compared to a > >> constant, rather than compared to each other. > > > I was going to do it that way for aesthetics if nothing else, but in > > this particular code "self.higher is self.lower" could only have been > > True if they were both None, so the final "is None" was redundant and > > I only included it as a "just-in-case" check in case someone someday > > used the code in such a way as to assign the same object (not None) to > > both self.higher and self.lower... ?Totally pointless, I'm sorry to > > say. ?I'm glad the whole line was redundant really. > > I say, you should keep the `is None` for readability and to safe guard > against immutable optimization and/or Singleton objects. Due to an > implementation detail, python does not guarantee that two immutable > object will return different objects; you have: > > >>> a = 1 > >>> b = 1 > >>> a is b > > True I probably would have kept the 'is None' for those reasons except that this code was never meant to be used by anyone but me, and it turned out the entire line was redundant anyway. :] From python at mrabarnett.plus.com Fri Jul 3 16:07:50 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 21:07:50 +0100 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4A4E6516.8090207@mrabarnett.plus.com> Mel wrote: > Steven D'Aprano wrote: > >> The most important difference between my suggestion and that of the OP is >> that he limited the key function to something which returns a truth >> value, while I'm looking for something more general which can split the >> input into an arbitrary number of collated sublists. > > Generally along the lines of > > def scatter (seq, criterion): > lists = {} > for s in seq: > lists.setdefault (criterion (s), []).append (s) > return lists > > modulo a defaultdict or some such ? > Somehow this reminds me of the new Counter class, except that it's making lists instead of counts. From user at example.net Fri Jul 3 16:14:22 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 22:14:22 +0200 Subject: stringio+tarfile (or better... zipfile) In-Reply-To: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4a4e66a0$0$1115$4fafbaef@reader4.news.tin.it> following the excellent suggestions received, i tried to adapt the problem to zipfile, and i wrote this: $ cat zip001.py import sys import zipfile import StringIO nb1 = "first.txt", StringIO.StringIO("one one\n") nb2 = "second.txt", StringIO.StringIO("two\n") zb = StringIO.StringIO() zip = zipfile.ZipFile(zb , "w" , zipfile.ZIP_DEFLATED) for name , buffer in [nb1 , nb2]: zip.writestr(name, buffer.getvalue()*1000) zip.close() sys.stdout.write(zb.getvalue()) $ python zip001.py > zip001.out $ unzip -l zip001.out Archive: zip001.out Length Date Time Name -------- ---- ---- ---- 8000 07-03-09 22:07 first.txt 4000 07-03-09 22:07 second.txt -------- ------- 12000 2 files $ it seems to me thaz zipfile has a simpler usability... any comments? thanks again and bye bye From sajmikins at gmail.com Fri Jul 3 16:17:16 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 13:17:16 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> Message-ID: <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> On Jul 3, 12:56?am, Paul Rubin wrote: > Simon Forman writes: > > > Yes, but the concept of null pointers is considered kludgy these days. > > Seriously? "kludgy"? ?(I really AM getting old.) > > ?http://math.andrej.com/2009/04/11/on-programming-language-design/ > > discusses the issue (scroll down to "Undefined values" section). Hmm, I'm not convinced. I read that post with interest, but his argument against None et. al. was just this: "...stick to the principle: NULL is wrong because it causes horrible and tricky mistakes which appear even after the program was tested thoroughly. You cannot introduce NULL into the language and tell the programmer to be careful about it. The programmer is not capable of being careful!" To me that seems weak and too general, you could say /anything/ "...causes horrible and tricky mistakes which appear even after the program was tested thoroughly." He does go on to say, in a reply to a comment, "In my opinion, at the end of the day the quality of the code depends more on the quality of its author than on the quality of the language he uses. But this does not mean that the quality of the programming language does not matter." Which I agree with wholeheartedly. I just don't see that he made a strong case that the inclusion of NULLs directly implies poor quality of language. (I like Haskell's Maybe, but saying A is better than B doesn't imply that B is therefore terrible.) > > Well I wouldn't know, I've been fortunate enough to program mostly in > > python for over half a decade now and None and 0 are as close as I've > > gotten to NULL in a long time. > > Right, and how many times have you had to debug > > ? ?AttributeError: 'NoneType' object has no attribute 'foo' > > or the equivalent null pointer exceptions in Java, C, or whatever? > They are very common. ?And the basic idea is that if you avoid using > null pointers in the first place, you'll get fewer accidental null > pointer exceptions. I have encountered exceptions like that, but usually as a result of e.g. omitting a return statement (so the caller gets None instead of whatever should have been returned.) I don't usually write "pointer" style code in python. In fact, other than this BTree and the Dancing Links algorithm I mentioned earlier I can't recall ever having done it. Indeed, python's omission of pointers is one of the reasons I'm so fond of it. In my experience with python exceptions like that one are indicators of something bad wrong with my code, not of misuse of None (as NULL pointer.) > > That sounds very interesting, but I'm only writing a BTree to then use > > it to play with "persistent data structures" > > But you have implemented a mutable tree. ?If you want to write a > persistent one, then write a persistent one! ?You also use a wishful The paper I'm working from is about general techniques for making persistent data structures from mutable forms, thus I wanted a mutable BTree to then "overwrite" with a persistent form. > heuristic in the hope of keeping the tree balanced--if you want a > balanced tree, why not use one that's guaranteed to stay in balance? > AVL trees are pretty easy to implement; maybe you could try writing a > persistent one: > > ?http://en.wikipedia.org/wiki/AVL_tree The balance (or lack thereof) of the tree is not important (in this use.) I threw in that heuristic in the delete function because it was easy enough and it was mentioned in the wikipedia article on BTrees. AVL trees are easy too, but still more complicated than I need. > > In this particular case it's somewhat more elegant (IMO) to check "is > > None". > > Well, I can't understand why that might be, but it's surely > subjective, so ok. It's a matter of this: if a is None: return b if b is None: return a # handle both non-None here... vs. this: if a is not None: if b is not None: # handle both non-None here... else: return a return b Everything is subjective, but I think the first one is more elegant IMO. > > > I'm sorry but I find that class rather ugly. ?The code would be a lot > > > smaller and have fewer corner cases with a different data representation. > > > Um, thanks? ?Seriously though, care to put your money where your mouth > > is? I'd be interested to see a BTree implemented as you indicated above. > > Er, I'm not trying to argue with you. ?You asked for people's advice > and impressions, so I gave you some. ?I don't feel like rewriting that I know, I asked for it. :] But it still stings a little to hear someone call my code "rather ugly". No hard feelings. I'm not trying to argue with you either. :] > whole class, but I'll do a method or two, using [] to represent an > empty node. ?(Hmm, actually changing the empty node representation did > less to shorten the code than just getting rid of a bunch of > duplication. ?Really, I'd tend to look for a slightly different tree > structure which tried to avoid these issues). Hmm, I really don't see that using an empty list rather than None as a stand-in for NULL pointer bought you anything there. In fact, this code would work identically if you replaced [] with None. I was hoping to see some clever code-fu involving list manipulation or something. When would you ever store a value in the empty list "nodes" anyway? > Untested: > > class BinaryTree: > ? ? def __init__(self, key, value): > ? ? ? ? self.key = key > ? ? ? ? self.value = value > ? ? ? ? self.higher = self.lower = [] > > ? ? def get(self, key): > ? ? ? ? if key == self.key: > ? ? ? ? ? ? return self.value > ? ? ? ? branch = self.lower if key < self.key else self.higher > ? ? ? ? return branch.get(key) if branch else [] > > ? ? def insert(self, key, value): > ? ? ? ? def xinsert(xtree): ?# xtree is either a tree or [] > ? ? ? ? ? ?if not xtree: xtree = BinaryTree(key, value) > ? ? ? ? ? ?else: xtree.insert(key, value) > ? ? ? ? ? ?return xtree > > ? ? ? ? if key == self.key: > ? ? ? ? ? ? self.value = value > ? ? ? ? elif key < self.key: > ? ? ? ? ? ? self.lower = xinsert(self.lower) > ? ? ? ? else: > ? ? ? ? ? ? self.higher = xinsert(self.higher) > > and so forth. From dickinsm at gmail.com Fri Jul 3 16:18:09 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Fri, 3 Jul 2009 13:18:09 -0700 (PDT) Subject: Compiling 64 bit python on a mac - cannot compute sizeof (int) References: Message-ID: <6c0287d8-c9f5-44e2-8159-fe5c88494a62@g1g2000yqh.googlegroups.com> On Jul 3, 5:30?pm, Keflavich wrote: > I'm trying to compile a 64 bit version of python 2.6.2 on my mac (OS X > 10.5.7), and am running into a problem during the configure stage. > > I configure with: > ./configure --enable-framework=/Library/Frameworks --enable- > universalsdk MACOSX_DEPLOYMENT_TARGET=10.5 --with-universal-archs=all - > with-readline-dir=/usr/local > > because I want 64 and 32 bit, and I needed to install a 64 bit > readline as a prerequisite. > > configure fails at: > checking size of int... configure: error: cannot compute sizeof (int) I confess that I don't fully understand the intricacies of the various OS X autoconf options, but I think the problem is that the --enable- universalsdk option takes a directory. If that directory isn't given, it appears to default to /Developer/SDKs/MacOSX10.4u.sdk (at least on my OS X 10.5.7 machine), which would likely conflict with your MACOSX_DEPLOYMENT_TARGET=10.5 setting. Try either changing MACOSX_DEPLOYMENT_TARGET to 10.4, or using -- enable-universalsdk=/Developer/SDKs/MacOSX10.5.sdk (or whatever the appropriate directory is on your system). For some reason, I think using --enable-universalsdk=/ also works on my system. If none of that helps, you might try asking this question over on the pythonmac- sig mailing list. (http://mail.python.org/mailman/listinfo/pythonmac- sig) Mark From timr at probo.com Fri Jul 3 16:20:18 2009 From: timr at probo.com (Tim Roberts) Date: Fri, 03 Jul 2009 13:20:18 -0700 Subject: Searching equivalent to C++ RAII or deterministic destructors References: <4A4CA278.4050604@ieee.org> Message-ID: Dave Angel wrote: > >You're right of course. What I was trying to say was it deletes the >reference to the object. Unlike obj = None, del obj removes the >reference (attribute) entirely. Although I don't know what it should be >called if it's a local variable. Perhaps it "unbinds" the name. Yes. As far as the object is concerned, "obj = None" and "del obj" are exactly identical. In both cases, there is one less binding to the name. The difference between the two is only whether the name lives on in the namespace. A local variable is (usually) just a name in the local() namespace. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From tjreedy at udel.edu Fri Jul 3 16:36:50 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 16:36:50 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <025e3974$0$20657$c3e8da3@news.astraweb.com> References: <025e3974$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 03 Jul 2009 16:19:22 +0000, kj wrote: > >> In Alan G Isaac >> writes: >> >>> 1. Don't use assertions to test argument values! >> Out of curiosity, where does this come from? > > Assertions are disabled when you run Python with the -O (optimise) flag. > If you rely on assertions to verify data, then any time the user runs > python with -O your code will be running without error checking. > > assert should be used to verify program logic, not to sanitize data. In other words, assertions should never happen, whereas your assertions could easily happen with 'bad' input. An example of program logic assertion would be 'assert ' at the top of the loop. That would be useful for development, but would not be needed for production use of tested code. tjr From sajmikins at gmail.com Fri Jul 3 16:46:28 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 13:46:28 -0700 (PDT) Subject: Python debugger References: Message-ID: On Jul 3, 8:15?am, srinivasan srinivas wrote: > Hi, > Could you suggest some python debuggers? > > Thanks, > Srini > > ? ? ? Love Cricket? Check out live scores, photos, video highlights and more. Click herehttp://cricket.yahoo.com Ipython has good debugger integration. From http Fri Jul 3 16:50:12 2009 From: http (Paul Rubin) Date: 03 Jul 2009 13:50:12 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> Message-ID: <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Simon Forman writes: > "...stick to the principle: NULL is wrong because it causes horrible > and tricky mistakes which appear even after the program was tested > thoroughly. You cannot introduce NULL into the language and tell the > programmer to be careful about it. The programmer is not capable of > being careful!" > > To me that seems weak and too general, you could say /anything/ > "...causes horrible and tricky mistakes which appear even after the > program was tested thoroughly." On the contrary, general means powerful :). For example, you can say that malloc/free in C causes horrible and tricky mistakes etc. Python avoids the mistakes by having automatic storage management. > Which I agree with wholeheartedly. I just don't see that he made a > strong case that the inclusion of NULLs directly implies poor quality > of language. > > (I like Haskell's Maybe, but saying A is better than B doesn't imply > that B is therefore terrible.) I wouldn't say Python's None is terrible, but the programming style in which None is used as a marker for "absent value" is genuinely a source of bugs, requiring care when used. Often it's easy to just avoid it and all the bugs that come with it. Haskell's Maybe type, ML's Option type, and various similar constructs in other recently designed languages all are responses to the shortcomings of None-like constructs in older languages. I'm not going purely by that one guy's blog post, though I do think that post was pretty good. > Hmm, I really don't see that using an empty list rather than None as a > stand-in for NULL pointer bought you anything there. In fact, this > code would work identically if you replaced [] with None. Yes I noticed that too (and mentioned it) after writing the code. I think the real problem is that the class has unnatural semantics and Python's OOP system (well, OOP in general) is also sort of kludgy. I will think about a possible rewrite. From keflavich at gmail.com Fri Jul 3 19:01:42 2009 From: keflavich at gmail.com (Adam) Date: Fri, 3 Jul 2009 16:01:42 -0700 (PDT) Subject: Compiling 64 bit python on a mac - cannot compute sizeof (int) References: <6c0287d8-c9f5-44e2-8159-fe5c88494a62@g1g2000yqh.googlegroups.com> Message-ID: <73186719-e5a6-4661-a06c-cf6f2570996b@2g2000prl.googlegroups.com> On Jul 3, 2:18?pm, Mark Dickinson wrote: > On Jul 3, 5:30?pm, Keflavich wrote: > > > I'm trying to compile a 64 bit version of python 2.6.2 on my mac (OS X > > 10.5.7), and am running into a problem during the configure stage. > > > I configure with: > > ./configure --enable-framework=/Library/Frameworks --enable- > > universalsdk MACOSX_DEPLOYMENT_TARGET=10.5 --with-universal-archs=all - > > with-readline-dir=/usr/local > > > because I want 64 and 32 bit, and I needed to install a 64 bit > > readline as a prerequisite. > > > configure fails at: > > checking size of int... configure: error: cannot compute sizeof (int) > > I confess that I don't fully understand the intricacies of the various > OS X autoconf options, but I think the problem is that the --enable- > universalsdk option takes a directory. ?If that directory isn't > given, it appears to default to /Developer/SDKs/MacOSX10.4u.sdk (at > least on my OS X 10.5.7 machine), which would likely conflict with > your MACOSX_DEPLOYMENT_TARGET=10.5 setting. > > Try either changing MACOSX_DEPLOYMENT_TARGET to 10.4, or using -- > enable-universalsdk=/Developer/SDKs/MacOSX10.5.sdk ?(or whatever the > appropriate directory is on your system). ?For some reason, I think > using --enable-universalsdk=/ also works on my system. ?If none of > that helps, you might try asking this question over on the pythonmac- > sig mailing list. (http://mail.python.org/mailman/listinfo/pythonmac- > sig) > > Mark Thanks. I also determined after the fact that universalsdk was the problem, but I didn't know how to fix it. Unfortunately, it turns out what you identified was a transcription error on my part - I had been using --enable-universalsdk instead of --enable-universalsdk=/. Thanks for the help, I appreciate it! Adam From alan.isaac at gmail.com Fri Jul 3 19:18:15 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 23:18:15 GMT Subject: 3.1 Windows Installer trashes extension registrations In-Reply-To: References: Message-ID: > Alan G Isaac wrote: >> Using the 3.1 Windows installer, I chose that I did not >> want the extensions registered, and the installer >> unregistered the .py and .pyw extensions (which I had >> wanted to keep associated with Python 2.6). >> Is this intentional? On 7/3/2009 2:57 PM Terry Reedy apparently wrote: > I suspect not, but Martin is the one to answer. > If you had started a new thread for this new topic > "3.1 Windows Installer trashes extension registrations" > this would have been seen more easily ;-) Done. Cheers, Alan Isaac From gallium.arsenide at gmail.com Fri Jul 3 19:26:39 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 3 Jul 2009 16:26:39 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: <9d99c5ca-f497-473f-a22f-a5219597bcd7@h11g2000yqb.googlegroups.com> On Jul 3, 4:50?pm, Paul Rubin wrote: > I wouldn't say Python's None is terrible, but the > programming style in which None is used as a marker > for "absent value" is genuinely a source of bugs, > requiring care when used. ?Often it's easy to just > avoid it and all the bugs that come with it. While other languages may make it easier to avoid these types of bugs, I think it's pretty natural to use Python's None as a marker for "absent value", if you're coding in Python. Python seems to encourage this by having functions return None when there is no explicit return value, and by having the interactive command line print nothing at all when the expression evaluates to None. I won't argue with anyone who wants to call these design flaws, but it seems to me that using None this way in one's own Python code is reasonably Pythonic. John From tn.pablo at gmail.com Fri Jul 3 19:40:03 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Fri, 3 Jul 2009 18:40:03 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On Fri, Jul 3, 2009 at 09:05, kj wrote: > The context is the concept of a binary search. ?In one of their > homeworks, my students will have two occasions to use a binary > search. ?This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. Five params for a novice function is overkill. I'd drop the epsilon thing and, assuming you are searching in a list, tuple or something similar, make the lower limit default to the first index and the upper default to the last one. In fact, I'd let them to realize that a function is convenient, and base some of the grading in whether they wrote it or not. Just a thought. -- Pablo Torres N. From ldo at geek-central.gen.new_zealand Fri Jul 3 20:08:17 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 04 Jul 2009 12:08:17 +1200 Subject: Idioms and Anti-Idioms Question References: Message-ID: In message , J. Cliff Dyer wrote: > On Wed, 2009-07-01 at 17:19 +1200, Lawrence D'Oliveiro wrote: >> >> In message , J. >> Cliff Dyer wrote: >> >> > If the lines got separated, a leading + could disappear into its line >> > without any errors showing up. A trailing + would raise a syntax >> > error. >> >> Unless, of course, it was moved onto the previous line as part of >> whatever caused the separation of the lines. How would you guard against >> that? > > Can you give an example of what you mean? Sure. Just start with an example of what you mean. From asm198 at gmail.com Fri Jul 3 21:14:23 2009 From: asm198 at gmail.com (Icarus) Date: Fri, 3 Jul 2009 18:14:23 -0700 (PDT) Subject: Problems with using queue in Tkinter application Message-ID: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> I'm working on a serial protocol analyzer in python. We have an application written by someone else in MFC but we need something that is cross platform. I intended to implement the GUI portion in Tkinter but am having trouble. The idea is that I will read messages from the serial port and output them to a Tkinter Text object initially. Eventually it will have other functionality but that's it for the short term. I've written this little test app to experiment with putting things on the GUI via a Queue which is polled by the Tkinter loop. On some machines this code works fine and I get whatever I type in displayed in the Text widget. On others I get errors like this as soon as I start it running. error in background error handler: out of stack space (infinite loop?) while executing "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) while execu..." I don't understand why on some machines it works exactly as expected and on others it acts the same way Tkinter does when I call functions directly from outside the Tkinter thread. Does anyone have any suggestions? The full code as appended below. Thanks in advance. [code] import Queue class functionQueue: def __init__(self, root = None, timeout = 250): self.functionQueue = Queue.Queue() self.root = root self.timeout = timeout if(self.root): self.pop_function(root) def pop_function(self, root = None): try: funcArgList = self.functionQueue.get(block = False) except Queue.Empty: pass else: try: funcArgList[0](*funcArgList[1]) except: try: print "Failed to call function", funcArgList[0] except: print "Failed to call function" if(root): root.after(self.timeout, lambda: self.pop_function (self.root)) def add_function(self, function, argList): try: self.functionQueue.put([function, argList]) except: pass if( __name__ == '__main__'): import Tkinter import thread text = Tkinter.Text() text.pack() myQueue = functionQueue(text, 50) def gui_loop(): try: text.mainloop() except: import os os._exit(1) thread.start_new_thread(text.mainloop, ()) while(True): usrInput = raw_input() if(usrInput == "-1"): import os os._exit(0) myQueue.add_function(text.insert, ['end', usrInput + "\n"]) myQueue.add_function(text.see, ['end']) [/code] From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 22:04:42 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 02:04:42 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: <025eaa4f$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 13:50:12 -0700, Paul Rubin wrote: > I wouldn't say Python's None is terrible, but the programming style in > which None is used as a marker for "absent value" is genuinely a source > of bugs, requiring care when used. Often it's easy to just avoid it and > all the bugs that come with it. So you keep saying, but I don't believe it. I've never found this to be a problem in my own code: a few silly mistakes where I accidentally assign the result of a function that operates by side-effect: blist = alist.sort() or similar, and I haven't done that for years now. No, wait, I tell I lie... re.search() sometimes bites me, because sometimes it returns None and sometimes it returns a matchobject and I don't use re often enough to have good habits with it yet. But that's a good example of why removing NULL (None, nul, nil, whatever you want to call it) doesn't lower the number of bugs, it just changes their nature: if re.search didn't return None, I'd still have trouble with it. There are three natural approaches to (say) re.search() for dealing with failure: (1) return a sentinel value like None; (2) return a matchobject which tests False; (3) raise an exception. Here's how you would use them correctly: # 1 or 2 are handled identically o = re.search(s) if o: process_match(o) else: print "Not found" # 3 is different try: process_match(re.search(s)) except Exception: print "Not found" There's essentially little or no difference in the handling. In all three cases, if you assume search will always succeed or forget to handle the failure case, you will write incorrect code: process_match(re.search(s)) and get an unexpected exception. The only difference is that the specific exception will differ between the cases. Remove None from the scenario doesn't reduce the number of bugs, it just changes their nature. -- Steven From gagsl-py2 at yahoo.com.ar Fri Jul 3 23:50:12 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 04 Jul 2009 00:50:12 -0300 Subject: stringio+tarfile (or better... zipfile) References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4e66a0$0$1115$4fafbaef@reader4.news.tin.it> Message-ID: En Fri, 03 Jul 2009 17:14:22 -0300, superpollo escribi?: > following the excellent suggestions received, i tried to adapt the > problem to zipfile, and i wrote this: > > zip = zipfile.ZipFile(zb , "w" , zipfile.ZIP_DEFLATED) > for name , buffer in [nb1 , nb2]: > zip.writestr(name, buffer.getvalue()*1000) > zip.close() > > it seems to me thaz zipfile has a simpler usability... any comments? Maybe - but a zip and a tar file are not the same thing. Now, if you want to say that there should be a common interfase to handle all those file formats like tar,zip,bz2: Yes, I agree. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Sat Jul 4 00:04:29 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 04 Jul 2009 01:04:29 -0300 Subject: Reversible Debugging References: <4A4E4B8B.7090104@gmail.com> Message-ID: En Fri, 03 Jul 2009 15:18:51 -0300, Patrick Sabin escribi?: > I am interested if there are any python modules, that supports > reversible debugging aka stepping backwards. Any links or ideas would be > helpful, because I am thinking of implementing something like that. Do you want reverse execution, like an undo function? Undo all changes made by executing some piece of code? There are many cases where that's hard to do, or impossible. How to undo object destruction? How to undo external effects, like writing to files? You should think carefully what can and cannot be done - good luck! -- Gabriel Genellina From aahz at pythoncraft.com Sat Jul 4 00:47:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 21:47:30 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: In article <7xhbxtjxtn.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: >Simon Forman writes: >> >> (I like Haskell's Maybe, but saying A is better than B doesn't imply >> that B is therefore terrible.) > >I wouldn't say Python's None is terrible, but the programming style in >which None is used as a marker for "absent value" is genuinely a >source of bugs, requiring care when used. Often it's easy to just >avoid it and all the bugs that come with it. > >Haskell's Maybe type, ML's Option type, and various similar constructs >in other recently designed languages all are responses to the >shortcomings of None-like constructs in older languages. I'm not >going purely by that one guy's blog post, though I do think that post >was pretty good. AFAICT, in order for Maybe and Option to work, you need to have some kind of static typing system. Am I missing something? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From aahz at pythoncraft.com Sat Jul 4 00:50:47 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 21:50:47 -0700 Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: In article , Tim Roberts wrote: >Dave Angel wrote: >> >>You're right of course. What I was trying to say was it deletes the >>reference to the object. Unlike obj = None, del obj removes the >>reference (attribute) entirely. Although I don't know what it should be >>called if it's a local variable. Perhaps it "unbinds" the name. > >Yes. As far as the object is concerned, "obj = None" and "del obj" are >exactly identical. In both cases, there is one less binding to the name. > >The difference between the two is only whether the name lives on in the >namespace. > >A local variable is (usually) just a name in the local() namespace. OTOH, Python's ``del`` applies to targets generally, not just names: >>> L = [1,2,3] >>> del L[1] >>> L [1, 3] -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From upwestdon at gmail.com Sat Jul 4 02:03:39 2009 From: upwestdon at gmail.com (upwestdon) Date: Fri, 3 Jul 2009 23:03:39 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> On Jul 2, 1:23?pm, Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > ? ? if self.lower is None: > ? ? ? ? return > ? ? return self.lower > if self.lower is None: > ? ? return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) How about just: if not (self.higher and self.lower): return self.higher or self.lower From tkjthingone at gmail.com Sat Jul 4 02:46:13 2009 From: tkjthingone at gmail.com (Horace Blegg) Date: Fri, 3 Jul 2009 23:46:13 -0700 Subject: Reversible Debugging In-Reply-To: <4A4E4B8B.7090104@gmail.com> References: <4A4E4B8B.7090104@gmail.com> Message-ID: You might consider using a VM with 'save-points'. You run the program (in a debugger/ida/what have you) to a certain point (logical point would be if/ifelse/else statements, etc) and save the VM state. Once you've saved, you continue. If you find the path you've taken isn't what you are after, you can reload a previous save point and start over, trying a different path the next time. This is also somewhat useful if you're trying to debug an error that happens deep inside of a program, so you can continually jump to the point right before the error happens, instead of needing to run through the entire program each time it crashes. Just beware of tunnel vision. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hanooter at gmail.com Sat Jul 4 03:33:52 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 00:33:52 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. Message-ID: Currently I need to find the most common elements in thousands of arrays within one large array (arround 2 million instances with ~70k unique elements) so I set up a dictionary to handle the counting so when I am iterating I up the count on the corrosponding dictionary element. I then iterate through the dictionary and find the 25 most common elements. the elements are initially held in a array within an array. so I am am just trying to find the most common elements between all the arrays contained in one large array. my current code looks something like this: d = {} for arr in my_array: -----for i in arr: #elements are numpy integers and thus are not accepted as dictionary keys -----------d[int(i)]=d.get(int(i),0)+1 then I filter things down. but with my algorithm that only takes about 1 sec so I dont need to show it here since that isnt the problem. But there has to be something better. I have to do this many many times and it seems silly to iterate through 2 million things just to get 25. The element IDs are integers and are currently being held in numpy arrays in a larger array. this ID is what makes up the key to the dictionary. It currently takes about 5 seconds to accomplish this with my current algorithm. So does anyone know the best solution or algorithm? I think the trick lies in matrix intersections but I do not know. From clp2 at rebertia.com Sat Jul 4 03:45:32 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 00:45:32 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: Message-ID: <50697b2c0907040045i5316c803ma199bfab10324735@mail.gmail.com> On Sat, Jul 4, 2009 at 12:33 AM, mclovin wrote: > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating ?I up the count on the corrosponding dictionary element. I > then iterate through the dictionary and find the 25 most common > elements. > > the elements are initially held in a array within an array. so I am am > just trying to find the most common elements between all the arrays > contained in one large array. > my current code looks something like this: > d = {} > for arr in my_array: > -----for i in arr: > #elements are numpy integers and thus are not accepted as dictionary > keys > -----------d[int(i)]=d.get(int(i),0)+1 > > then I filter things down. but with my algorithm that only takes about > 1 sec so I dont need to show it here since that isnt the problem. > > > But there has to be something better. I have to do this many many > times and it seems silly to iterate through 2 million things just to > get 25. The element IDs are integers and are currently being held in > numpy arrays in a larger array. this ID is what makes up the key to > the dictionary. > > ?It currently takes about 5 seconds to accomplish this with my current > algorithm. > > So does anyone know the best solution or algorithm? I think the trick > lies in matrix intersections but I do not know. Using a defaultdict (http://docs.python.org/library/collections.html#collections.defaultdict) would speed it up (but only slightly) and make it clearer to read. Cheers, Chris -- http://blog.rebertia.com From patrick.just4fun at gmail.com Sat Jul 4 04:04:08 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Sat, 04 Jul 2009 10:04:08 +0200 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F0CF8.5090101@gmail.com> Gabriel Genellina schrieb: > Do you want reverse execution, like an undo function? Undo all changes > made by executing some piece of code? I am not completly sure, if I really want to make exact undo, i.e. undoing commands by reversing all their effects, or just restoring the program state to an arbitrary state of its history, which would not effect things like files. > There are many cases where that's hard to do, or impossible. How to > undo object destruction? How to undo external effects, like writing to > files? Object destruction should not be a big problem to restore. But writing files or other resources is because it is not guaranted that it is even possible or wanted. I considered either ignoring undoing external commands completly or adding some plugin system to do it. > You should think carefully what can and cannot be done - good luck! > From andreengels at gmail.com Sat Jul 4 04:04:42 2009 From: andreengels at gmail.com (Andre Engels) Date: Sat, 4 Jul 2009 10:04:42 +0200 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: Message-ID: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating ?I up the count on the corrosponding dictionary element. I > then iterate through the dictionary and find the 25 most common > elements. > > the elements are initially held in a array within an array. so I am am > just trying to find the most common elements between all the arrays > contained in one large array. > my current code looks something like this: > d = {} > for arr in my_array: > -----for i in arr: > #elements are numpy integers and thus are not accepted as dictionary > keys > -----------d[int(i)]=d.get(int(i),0)+1 > > then I filter things down. but with my algorithm that only takes about > 1 sec so I dont need to show it here since that isnt the problem. > > > But there has to be something better. I have to do this many many > times and it seems silly to iterate through 2 million things just to > get 25. The element IDs are integers and are currently being held in > numpy arrays in a larger array. this ID is what makes up the key to > the dictionary. > > ?It currently takes about 5 seconds to accomplish this with my current > algorithm. > > So does anyone know the best solution or algorithm? I think the trick > lies in matrix intersections but I do not know. There's no better algorithm for the general case. No method of checking the matrices using less than 2000000-x look-ups will ensure you that there's not a new value with x occurences lurking somewhere. However, if you need this information more often, and if the number of values that changes in between is small compared to the total size of the matrices, you could make a gain in subsequent calculations by remembering part results. Depending on the situation you could for example keep the dictionary with the counts and update it each time, or keep such a dictionary for each "big" matrix, and set a flag when that dictionary is not up-to-date any more . -- Andr? Engels, andreengels at gmail.com From Lacrima.Maxim at gmail.com Sat Jul 4 04:12:10 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Sat, 4 Jul 2009 01:12:10 -0700 (PDT) Subject: urllib with x509 certs Message-ID: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Hello! I am trying to use urllib to fetch some internet resources, using my client x509 certificate. I have divided my .p12 file into mykey.key and mycert.cer files. Then I use following approach: >>> import urllib >>> url = 'https://example.com' >>> xml = ''' ... somexml ''' >>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') >>> f = opener.open(url, xml) This works Ok! But every time I am asked to enter PEM pass phrase, which I specified during dividing my .p12 file. So my question... What should I do to make my code fetch any url automatically (without asking me every time to enter pass phrase)? As I understand there is impossible to specify pass phrase while constructing URLopener. So what should I do? With regards, Max (sorry if my English isn't very proper) From __peter__ at web.de Sat Jul 4 04:21:13 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 04 Jul 2009 10:21:13 +0200 Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: Icarus wrote: > I'm working on a serial protocol analyzer in python. We have an > application written by someone else in MFC but we need something that > is cross platform. I intended to implement the GUI portion in Tkinter > but am having trouble. > > The idea is that I will read messages from the serial port and output > them to a Tkinter Text object initially. Eventually it will have > other functionality but that's it for the short term. I've written > this little test app to experiment with putting things on the GUI via > a Queue which is polled by the Tkinter loop. > > On some machines this code works fine and I get whatever I type in > displayed in the Text widget. On others I get errors like this as > soon as I start it running. > > error in background error handler: > out of stack space (infinite loop?) > while executing > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > while execu..." > > > I don't understand why on some machines it works exactly as expected > and on others it acts the same way Tkinter does when I call functions > directly from outside the Tkinter thread. Does anyone have any > suggestions? The full code as appended below. Thanks in advance. > > [code] > > import Queue > > class functionQueue: > > def __init__(self, root = None, timeout = 250): > > self.functionQueue = Queue.Queue() > self.root = root > self.timeout = timeout > > if(self.root): > self.pop_function(root) > > > def pop_function(self, root = None): > > try: > funcArgList = self.functionQueue.get(block = False) > except Queue.Empty: > pass > else: > try: > funcArgList[0](*funcArgList[1]) > except: > try: > print "Failed to call function", funcArgList[0] > except: > print "Failed to call function" > > if(root): > root.after(self.timeout, lambda: self.pop_function > (self.root)) > > def add_function(self, function, argList): > > try: > self.functionQueue.put([function, argList]) > except: > pass > > > if( __name__ == '__main__'): > > import Tkinter > import thread > > text = Tkinter.Text() > text.pack() > > myQueue = functionQueue(text, 50) > > def gui_loop(): > try: > text.mainloop() > except: > import os > os._exit(1) > > thread.start_new_thread(text.mainloop, ()) > > while(True): > usrInput = raw_input() > > if(usrInput == "-1"): > import os > os._exit(0) > > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > myQueue.add_function(text.see, ['end']) > > [/code] I can make it work over here by putting the UI into the main thread, as suggested by http://effbot.org/zone/tkinter-threads.htm: import Queue import Tkinter import threading class FunctionQueue: # unchanged def input_loop(): while True: try: usrInput = raw_input() except EOFError: break myQueue.add_function(text.insert, ['end', usrInput + "\n"]) myQueue.add_function(text.see, ['end']) myQueue.add_function(text.quit, []) if __name__ == '__main__': text = Tkinter.Text() text.pack() myQueue = FunctionQueue(text, 50) threading.Thread(target=input_loop).start() text.mainloop() Peter From clp2 at rebertia.com Sat Jul 4 04:24:36 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 01:24:36 -0700 Subject: urllib with x509 certs In-Reply-To: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: <50697b2c0907040124j4e309c25j77e01c8127711f80@mail.gmail.com> On Sat, Jul 4, 2009 at 1:12 AM, Lacrima wrote: > Hello! > > I am trying to use urllib to fetch some internet resources, using my > client x509 certificate. > I have divided my .p12 file into mykey.key and mycert.cer files. > Then I use following approach: >>>> import urllib >>>> url = 'https://example.com' >>>> xml = ''' > ... somexml > ''' >>>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') >>>> f = opener.open(url, xml) > > This works Ok! But every time I am asked to enter PEM pass phrase, > which I specified during dividing my .p12 file. > So my question... What should I do to make my code fetch any url > automatically (without asking me every time to enter pass phrase)? > As I understand there is impossible to specify pass phrase while > constructing URLopener. > So what should I do? Subclass FancyURLopener [http://docs.python.org/library/urllib.html#urllib.FancyURLopener], overriding the prompt_user_passwd() method [http://docs.python.org/library/urllib.html#urllib.FancyURLopener.prompt_user_passwd]. Then use an instance of your subclass instead of URLopener. Cheers, Chris -- http://blog.rebertia.com From patrick.just4fun at gmail.com Sat Jul 4 04:33:03 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Sat, 04 Jul 2009 10:33:03 +0200 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F13BF.9050605@gmail.com> Horace Blegg schrieb: > You might consider using a VM with 'save-points'. You run the program > (in a debugger/ida/what have you) to a certain point (logical point > would be if/ifelse/else statements, etc) and save the VM state. Once > you've saved, you continue. If you find the path you've taken isn't > what you are after, you can reload a previous save point and start > over, trying a different path the next time. That was my idea to implement it. I thought of taking snapshots of the current state every time a "unredoable instruction", e.g random number generation, is done. For every other instruction I count the number of instructions done since the last snapshot. So I can go back one instruction by restoring to the previous state and go the number of instructions minus one forward. > This is also somewhat useful if you're trying to debug an error that > happens deep inside of a program, so you can continually jump to the > point right before the error happens, instead of needing to run > through the entire program each time it crashes. Just beware of tunnel > vision. I think of implementing some snapshot/restore mechanism first. That may help in many other situations. From stef.mientki at gmail.com Sat Jul 4 05:25:38 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sat, 04 Jul 2009 11:25:38 +0200 Subject: Reversible Debugging In-Reply-To: <4A4E4B8B.7090104@gmail.com> References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F2012.2020206@gmail.com> Patrick Sabin wrote: > Hello, > > I am interested if there are any python modules, that supports > reversible debugging aka stepping backwards. Any links or ideas would > be helpful, because I am thinking of implementing something like that. > In some cases it would be nice to have something like that, specially in combination with a "normal" debugger, which allows you to change values on the flight, like winpdb. cheers, Stef > Thanks in advance, > Patrick From martin at v.loewis.de Sat Jul 4 05:38:15 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 04 Jul 2009 11:38:15 +0200 Subject: urllib with x509 certs In-Reply-To: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: <4a4f2307$0$8384$9b622d9e@news.freenet.de> > This works Ok! But every time I am asked to enter PEM pass phrase, > which I specified during dividing my .p12 file. > So my question... What should I do to make my code fetch any url > automatically (without asking me every time to enter pass phrase)? > As I understand there is impossible to specify pass phrase while > constructing URLopener. > So what should I do? You can remove the passphrase on the private key, e.g. with the openssl rsa utility. Regards, Martin From banibrata.dutta at gmail.com Sat Jul 4 05:47:18 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 4 Jul 2009 15:17:18 +0530 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> Message-ID: <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> QuoteGoke Aruna what am saying is reading the ITU info on USSD, is it possible to use python to write the application SS7 with support for TCAP/MAP talking to E1 card to do the ss7 signalling. As Chris mentioned "possible, but probably not easy". AFAIK, not much of what you might need exists in form of existing Python modules, if that's what you were thinking -- and especially in the Open-source form. The only open-source initiative in the SS7 domain that I am aware of, is the openss7 (http://www.openss7.org/), which has 'some' software available. No personal experience though, API's are C/C++ AFAIR, and last I'd checked, the status wasn't 'practically usable' in functionality terms. If they offer functionality of the ITU-T SS7 stack upto the TCAP layer, exposing a C/C++ API to create dialogs, create components, populate and send them etc. (usual TCAP Primitives), then as Chris mentions, you could have Python wrappers (ctypes, swig...). Then, for the MAP layer, which is largely ASN.1 BER encoding/decoding, you'd need such functionality in Python. There are some ASN.1 BER codec implementations having Python API/bindings, but AFAIK, they are largely domain/application specific (s.a. for SNMP), and not generic or robust enough. Given the API's however, writing any application s.a. for performing some "business-logic" on receiving or before sending USSD / SMS message in Python, that's a no-brainer ! :-) If however, you want to implement the whole SS7 stack in Python -- everything that Chris wrote is accurate, and that's potentially (and the most obvious) way you'd have to implement it. -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Sat Jul 4 05:50:18 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 02:50:18 -0700 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> Message-ID: <50697b2c0907040250o8e6d9baqc1b368c2d0c547ff@mail.gmail.com> On Sat, Jul 4, 2009 at 2:47 AM, Banibrata Dutta wrote: > QuoteGoke Aruna >> >> what am saying is reading the ITU info on USSD, is it possible to use >> python >> >> to write the application SS7 with support for TCAP/MAP talking to E1 card >> to do the ss7 signalling. > > As Chris mentioned "possible, but probably not easy". AFAIK, not much of s/Chris/Dennis/ Credit where credit is due. I merely gave a PyPI link. Cheers, Chris -- http://blog.rebertia.com From banibrata.dutta at gmail.com Sat Jul 4 05:53:19 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 4 Jul 2009 15:23:19 +0530 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> Message-ID: <3de8e1f70907040253r60211297o8dbea679d3f5273a@mail.gmail.com> BTW, if you just want to be able to send/receive SMS's in a Python application, there are several alternatives. 1. Most operators, or 3rd party bulk-SMS vendors (who resell SMS capacity from operators at a premium but in smaller bundles than what an operator would sell), offer -- a. HTTP based mechanism to send/receive messages b. SMPP protocol. OpenSMPP has a C API, and you could create Python wrappers for same. 2. For very low volume application, you could use the Serial (or Serial emulation over USB/Bluetooth) way, i.e. using "AT" commandset of a tethered GSM mobile (with inbuilt GSM modem), or a separate GSM-modem (PCMCIA, USB,...) via Python, to send / receive SMS's. On Sat, Jul 4, 2009 at 3:17 PM, Banibrata Dutta wrote: > QuoteGoke Aruna > > what am saying is reading the ITU info on USSD, is it possible to use python > > to write the application SS7 with support for TCAP/MAP talking to E1 card to do the ss7 signalling. > > As Chris mentioned "possible, but probably not easy". AFAIK, not much of > what you might need exists in form of existing Python modules, if that's > what you were thinking -- and especially in the Open-source form. The only > open-source initiative in the SS7 domain that I am aware of, is the openss7 > (http://www.openss7.org/), which has 'some' software available. No > personal experience though, API's are C/C++ AFAIR, and last I'd checked, the > status wasn't 'practically usable' in functionality terms. If they offer > functionality of the ITU-T SS7 stack upto the TCAP layer, exposing a C/C++ > API to create dialogs, create components, populate and send them etc. (usual > TCAP Primitives), then as Chris mentions, you could have Python wrappers > (ctypes, swig...). Then, for the MAP layer, which is largely ASN.1 BER > encoding/decoding, you'd need such functionality in Python. There are some > ASN.1 BER codec implementations having Python API/bindings, but AFAIK, they > are largely domain/application specific (s.a. for SNMP), and not generic or > robust enough. Given the API's however, writing any application s.a. for > performing some "business-logic" on receiving or before sending USSD / SMS > message in Python, that's a no-brainer ! :-) > If however, you want to implement the whole SS7 stack in Python -- > everything that Chris wrote is accurate, and that's potentially (and the > most obvious) way you'd have to implement it. > > -- > regards, > Banibrata > http://www.linkedin.com/in/bdutta > -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From banibrata.dutta at gmail.com Sat Jul 4 05:54:08 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 4 Jul 2009 15:24:08 +0530 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <50697b2c0907040250o8e6d9baqc1b368c2d0c547ff@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> <50697b2c0907040250o8e6d9baqc1b368c2d0c547ff@mail.gmail.com> Message-ID: <3de8e1f70907040254ucc68ff3l65e791b0457678aa@mail.gmail.com> On Sat, Jul 4, 2009 at 3:20 PM, Chris Rebert wrote: > On Sat, Jul 4, 2009 at 2:47 AM, Banibrata > Dutta wrote: > > QuoteGoke Aruna > >> > >> what am saying is reading the ITU info on USSD, is it possible to use > >> python > >> > >> to write the application SS7 with support for TCAP/MAP talking to E1 > card > >> to do the ss7 signalling. > > > > As Chris mentioned "possible, but probably not easy". AFAIK, not much of > > s/Chris/Dennis/ > > Credit where credit is due. I merely gave a PyPI link. > Indeed. Thanks Chris, sorry Dennis. -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From vilya.harvey at gmail.com Sat Jul 4 05:55:44 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sat, 4 Jul 2009 10:55:44 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> Message-ID: <6aef848f0907040255y296c5ea1n2fff44c859cf12ab@mail.gmail.com> 2009/7/4 Andre Engels : > On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >> Currently I need to find the most common elements in thousands of >> arrays within one large array (arround 2 million instances with ~70k >> unique elements) >> >> so I set up a dictionary to handle the counting so when I am >> iterating ?I up the count on the corrosponding dictionary element. I >> then iterate through the dictionary and find the 25 most common >> elements. >> >> the elements are initially held in a array within an array. so I am am >> just trying to find the most common elements between all the arrays >> contained in one large array. >> my current code looks something like this: >> d = {} >> for arr in my_array: >> -----for i in arr: >> #elements are numpy integers and thus are not accepted as dictionary >> keys >> -----------d[int(i)]=d.get(int(i),0)+1 >> >> then I filter things down. but with my algorithm that only takes about >> 1 sec so I dont need to show it here since that isnt the problem. >> >> >> But there has to be something better. I have to do this many many >> times and it seems silly to iterate through 2 million things just to >> get 25. The element IDs are integers and are currently being held in >> numpy arrays in a larger array. this ID is what makes up the key to >> the dictionary. >> >> ?It currently takes about 5 seconds to accomplish this with my current >> algorithm. >> >> So does anyone know the best solution or algorithm? I think the trick >> lies in matrix intersections but I do not know. > > There's no better algorithm for the general case. No method of > checking the matrices using less than 2000000-x look-ups will ensure > you that there's not a new value with x occurences lurking somewhere. Try flattening the arrays into a single large array & sorting it. Then you can just iterate over the large array counting as you go; you only ever have to insert into the dict once for each value and there's no lookups in the dict. I don't know numpy, so there's probably a more efficient way to write this, but this should show what I'm talking about: big_arr = sorted(reduce(list.__add__, my_array, [])) counts = {} last_val = big_arr[0] count = 0 for val in big_arr: if val == last_val: count += 1 else: counts[last_val] = count count = 0 last_val = val counts[last_val] = count # to get the count for the last value. If flattening the arrays isn't practical, you may still get some improvements by sorting them individually and applying the same principle to each of them: counts = {} for arr in my_array: sorted_arr = sorted(arr) last_val = sorted_arr[0] count = 0 for val in sorted_arr: if val == last_val: count += 1 else: counts[last_val] = counts.get(last_val, 0) + count count = 0 last_val = val counts[last_val] = counts.get(last_val, 0) + count Hope that helps... Vil. From parikshatdubey at gmail.com Sat Jul 4 06:06:11 2009 From: parikshatdubey at gmail.com (Parikshat Dubey) Date: Sat, 4 Jul 2009 15:36:11 +0530 Subject: Calling C or C++ Functions inside Python script Message-ID: Hi, Is this possible to call C functions(inbuilt or user defined) inside python script.If yes, please illustrate how it can be done. Thanks, Parikshat Dubey -- -----Fear not the path of truth, for the lack of people walking on it. ----Practice yourself, for heaven's sake, in little things; and thence proceed to greater. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Lacrima.Maxim at gmail.com Sat Jul 4 06:08:40 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Sat, 4 Jul 2009 03:08:40 -0700 (PDT) Subject: urllib with x509 certs References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: On Jul 4, 11:24?am, Chris Rebert wrote: > On Sat, Jul 4, 2009 at 1:12 AM, Lacrima wrote: > > Hello! > > > I am trying to use urllib to fetch some internet resources, using my > > client x509 certificate. > > I have divided my .p12 file into mykey.key and mycert.cer files. > > Then I use following approach: > >>>> import urllib > >>>> url = 'https://example.com' > >>>> xml = ''' > > ... somexml > > ''' > >>>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') > >>>> f = opener.open(url, xml) > > > This works Ok! But every time I am asked to enter PEM pass phrase, > > which I specified during dividing my .p12 file. > > So my question... What should I do to make my code fetch any url > > automatically (without asking me every time to enter pass phrase)? > > As I understand there is impossible to specify pass phrase while > > constructing URLopener. > > So what should I do? > > Subclass FancyURLopener > [http://docs.python.org/library/urllib.html#urllib.FancyURLopener], > overriding the prompt_user_passwd() method > [http://docs.python.org/library/urllib.html#urllib.FancyURLopener.prom...]. > Then use an instance of your subclass instead of URLopener. > > Cheers, > Chris > --http://blog.rebertia.com Hi Chris, Thanks for your quick reply. According to docs the return value of prompt_user_passwd() method should be a tuple (user, password), but there is no user when authenticating with certificate. So how should I use this method? This doesn't work: >>> import urllib >>> class MyOpener(urllib.FancyURLopener): ... def prompt_user_passwd(self, host, realm): ... return ('password') ... With regards, Max From Lacrima.Maxim at gmail.com Sat Jul 4 06:14:17 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Sat, 4 Jul 2009 03:14:17 -0700 (PDT) Subject: urllib with x509 certs References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> <4a4f2307$0$8384$9b622d9e@news.freenet.de> Message-ID: On Jul 4, 12:38?pm, "Martin v. L?wis" wrote: > > This works Ok! But every time I am asked to enter PEM pass phrase, > > which I specified during dividing my .p12 file. > > So my question... What should I do to make my code fetch any url > > automatically (without asking me every time to enter pass phrase)? > > As I understand there is impossible to specify pass phrase while > > constructing URLopener. > > So what should I do? > > You can remove the passphrase on the private key, e.g. with the > openssl rsa utility. > > Regards, > Martin Hi Martin! Thanks for the reply. I want my key to be as secure as possible. So I will remove pass phrase if only there is no other possibility to go through authentication. With regards, Max From clp2 at rebertia.com Sat Jul 4 06:27:46 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 03:27:46 -0700 Subject: urllib with x509 certs In-Reply-To: References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: <50697b2c0907040327w23b7f0eal13934bc8d33c7231@mail.gmail.com> 2009/7/4 Lacrima : > On Jul 4, 11:24?am, Chris Rebert wrote: >> On Sat, Jul 4, 2009 at 1:12 AM, Lacrima wrote: >> > Hello! >> >> > I am trying to use urllib to fetch some internet resources, using my >> > client x509 certificate. >> > I have divided my .p12 file into mykey.key and mycert.cer files. >> > Then I use following approach: >> >>>> import urllib >> >>>> url = 'https://example.com' >> >>>> xml = ''' >> > ... somexml >> > ''' >> >>>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') >> >>>> f = opener.open(url, xml) >> >> > This works Ok! But every time I am asked to enter PEM pass phrase, >> > which I specified during dividing my .p12 file. >> > So my question... What should I do to make my code fetch any url >> > automatically (without asking me every time to enter pass phrase)? >> > As I understand there is impossible to specify pass phrase while >> > constructing URLopener. >> > So what should I do? >> >> Subclass FancyURLopener >> [http://docs.python.org/library/urllib.html#urllib.FancyURLopener], >> overriding the prompt_user_passwd() method >> [http://docs.python.org/library/urllib.html#urllib.FancyURLopener.prom...]. >> Then use an instance of your subclass instead of URLopener. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > Hi Chris, > Thanks for your quick reply. > According to docs the return value of prompt_user_passwd() method > should be a tuple (user, password), but there is no user when > authenticating with certificate. So how should I use this method? This > doesn't work: >>>> import urllib >>>> class MyOpener(urllib.FancyURLopener): > ... ? ? ?def prompt_user_passwd(self, host, realm): > ... ? ? ? ? ?return ('password') Only a guess: def prompt_user_passwd(self, host, realm): return ('', 'password') Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Sat Jul 4 06:29:30 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 03:29:30 -0700 Subject: Calling C or C++ Functions inside Python script In-Reply-To: References: Message-ID: <50697b2c0907040329w5085b9c9xa8d6a2aaae9667de@mail.gmail.com> On Sat, Jul 4, 2009 at 3:06 AM, Parikshat Dubey wrote: > Hi, > > Is this possible to call C functions(inbuilt or user defined) inside python > script.If yes, please illustrate how it can be done. The `ctypes` module: http://docs.python.org/library/ctypes.html Note: does not work w/ C++ directly. Cheers, Chris -- http://blog.rebertia.com From martin at v.loewis.de Sat Jul 4 07:09:51 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 04 Jul 2009 13:09:51 +0200 Subject: urllib with x509 certs In-Reply-To: References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> <4a4f2307$0$8384$9b622d9e@news.freenet.de> Message-ID: <4A4F387F.7050804@v.loewis.de> > Thanks for the reply. I want my key to be as secure as possible. So I > will remove pass phrase if only there is no other possibility to go > through authentication. And you put the passphrase into the source code instead? How does it make that more secure? Regards, Martin From larudwer at freenet.de Sat Jul 4 08:26:27 2009 From: larudwer at freenet.de (larudwer) Date: Sat, 4 Jul 2009 14:26:27 +0200 Subject: psyco V2 beta2 benchmark Message-ID: just out of curiosity i've downloaded the latest Version of Psyco V2 Beta 2 and run the benchmarks against the old Version of psyco 1.6 Because it might be of common interest, i am posting the results here. My machine is a Pentium D 3.2 Ghz running Windows XP SP 3 and Python 2.6.2. Psyco V2 was built with 4.3.3-tdm-1 mingw32 with optimisation flags changed to -O3 Benchmark | avg. Base time | psyco 1.6 time | psyco 2.0 time | ratio | possible error +- time_anyall all_bool_genexp | 2.270 | 2.250 | 2.420 | 0.930 | 8.3 % time_anyall all_bool_listcomp | 3.450 | 1.900 | 1.910 | 0.995 | 0.0 % time_anyall all_genexp | 1.970 | 1.940 | 2.160 | 0.898 | 9.6 % time_anyall all_listcomp | 3.485 | 1.660 | 1.660 | 1.000 | 1.4 % time_anyall all_loop | 0.665 | 0.090 | 0.090 | 1.000 | 4.4 % time_anyall any_bool_genexp | 2.215 | 2.130 | 2.340 | 0.910 | 10.0 % time_anyall any_bool_listcomp | 3.620 | 1.930 | 1.940 | 0.995 | 9.0 % time_anyall any_genexp | 1.985 | 1.920 | 2.180 | 0.881 | 10.1 % time_anyall any_listcomp | 3.360 | 1.680 | 1.680 | 1.000 | 8.0 % time_anyall any_loop | 0.660 | 0.090 | 0.090 | 1.000 | 3.0 % time_builtins chr(i) | 2.420 | 0.010 | 0.010 | 1.000 | 0.0 % time_builtins hash(i) | 1.280 | 0.370 | 0.080 | 4.625 | 8.1 % time_builtins int(round(f)) | 2.635 | 1.510 | 1.120 | 1.348 | 0.4 % time_builtins min | 0.535 | 0.520 | 0.120 | 4.333 | 1.9 % time_builtins min_kw | 4.430 | 4.400 | 0.160 | 27.500 | 0.5 % time_builtins ord(i) | 0.320 | 0.000 | 0.000 | 1.000 | 6.5 % time_builtins pow | 0.345 | 0.230 | 0.150 | 1.533 | 2.9 % time_builtins reduce | 0.735 | 0.710 | 0.020 | 35.498 | 1.4 % time_builtins round(f) | 1.720 | 0.890 | 0.400 | 2.225 | 1.2 % time_builtins sums | 0.180 | 0.180 | 0.100 | 1.800 | 0.0 % time_fib matrix | 0.425 | 0.360 | 0.360 | 1.000 | 2.3 % time_fib recursive | 0.000 | 0.000 | 0.000 | 1.000 | 0.0 % time_fib takahashi | 0.410 | 0.320 | 0.330 | 0.970 | 0.0 % time_generators call next just many times | 0.900 | 0.630 | 0.970 | 0.649 | 4.3 % time_generators iterate just many times | 0.660 | 0.550 | 0.950 | 0.579 | 3.1 % time_generators send and loop 1000 | 2.805 | 2.540 | 0.060 | 42.333 | 9.8 % time_generators send call loop 1000 | 2.505 | 2.940 | 0.060 | 48.999 | 10.9 % time_generators send just many times | 1.280 | 0.590 | 0.980 | 0.602 | 3.1 % time_iter iter | 1.490 | 0.590 | 0.440 | 1.341 | 5.5 % time_math floats | 2.910 | 1.500 | 1.630 | 0.920 | 0.7 % time_properties method_get | 0.935 | 0.120 | 0.130 | 0.923 | 1.1 % time_properties method_set | 1.005 | 0.170 | 0.180 | 0.944 | 1.0 % time_properties property_get | 0.960 | 0.740 | 0.100 | 7.400 | 2.1 % time_properties property_set | 1.020 | 0.920 | 0.930 | 0.989 | 0.0 % time_properties pyproperty_get | 1.535 | 1.310 | 0.140 | 9.357 | 0.7 % time_properties pyproperty_set | 1.030 | 0.920 | 0.930 | 0.989 | 2.0 % time_subdist subdist(i) | 3.665 | 1.640 | 6.140 | 0.267 | 0.8 % time_sums rounding | 0.800 | 0.790 | 0.810 | 0.975 | 2.5 % Running new timings with C:\Programme\GNU\python26\lib\site-packages\psyco\_psyco.pyd Sat Jul 04 12:09:07 2009 time_anyall : all_bool_genexp plain: 2.36 psyco: 2.42 ratio: 0.97 time_anyall : all_bool_listcomp plain: 3.45 psyco: 1.91 ratio: 1.80 time_anyall : all_genexp plain: 2.06 psyco: 2.16 ratio: 0.95 time_anyall : all_listcomp plain: 3.51 psyco: 1.66 ratio: 2.11 time_anyall : all_loop plain: 0.65 psyco: 0.09 ratio: 7.03 time_anyall : any_bool_genexp plain: 2.32 psyco: 2.34 ratio: 0.99 time_anyall : any_bool_listcomp plain: 3.45 psyco: 1.94 ratio: 1.78 time_anyall : any_genexp plain: 2.08 psyco: 2.18 ratio: 0.96 time_anyall : any_listcomp plain: 3.49 psyco: 1.68 ratio: 2.08 time_anyall : any_loop plain: 0.65 psyco: 0.09 ratio: 7.05 time_builtins : chr(i) plain: 2.42 psyco: 0.01 ratio: 197.40 time_builtins : hash(i) plain: 1.33 psyco: 0.08 ratio: 17.69 time_builtins : int(round(f)) plain: 2.63 psyco: 1.12 ratio: 2.36 time_builtins : min plain: 0.53 psyco: 0.12 ratio: 4.28 time_builtins : min_kw plain: 4.44 psyco: 0.16 ratio: 28.58 time_builtins : ord(i) plain: 0.33 psyco: 0.00 ratio: 123.57 time_builtins : pow plain: 0.34 psyco: 0.15 ratio: 2.24 time_builtins : reduce plain: 0.74 psyco: 0.02 ratio: 40.93 time_builtins : round(f) plain: 1.73 psyco: 0.40 ratio: 4.38 time_builtins : sums plain: 0.18 psyco: 0.10 ratio: 1.80 time_fib : matrix plain: 0.42 psyco: 0.36 ratio: 1.18 time_fib : recursive plain: 0.00 psyco: 0.00 ratio: 19.45 time_fib : takahashi plain: 0.41 psyco: 0.33 ratio: 1.25 time_generators: send call loop 1000 plain: 2.36 psyco: 0.06 ratio: 41.30 time_generators: send and loop 1000 plain: 2.66 psyco: 0.06 ratio: 46.69 time_generators: send just many times plain: 1.26 psyco: 0.98 ratio: 1.29 time_generators: iterate just many times plain: 0.67 psyco: 0.95 ratio: 0.70 time_generators: call next just many times plain: 0.88 psyco: 0.97 ratio: 0.91 time_iter : iter plain: 1.53 psyco: 0.44 ratio: 3.47 time_math : floats plain: 2.90 psyco: 1.63 ratio: 1.78 time_properties: method_get plain: 0.93 psyco: 0.13 ratio: 7.22 time_properties: method_set plain: 1.00 psyco: 0.18 ratio: 5.61 time_properties: property_get plain: 0.95 psyco: 0.10 ratio: 9.26 time_properties: property_set plain: 1.02 psyco: 0.93 ratio: 1.09 time_properties: pyproperty_get plain: 1.54 psyco: 0.14 ratio: 10.80 time_properties: pyproperty_set plain: 1.04 psyco: 0.93 ratio: 1.12 time_subdist : subdist(i) plain: 3.68 psyco: 6.14 ratio: 0.60 time_sums : rounding plain: 0.79 psyco: 0.81 ratio: 0.98 Running new timings with original psyco time_anyall : all_bool_genexp plain: 2.18 psyco: 2.25 ratio: 0.97 time_anyall : all_bool_listcomp plain: 3.45 psyco: 1.90 ratio: 1.82 time_anyall : all_genexp plain: 1.88 psyco: 1.94 ratio: 0.97 time_anyall : all_listcomp plain: 3.46 psyco: 1.66 ratio: 2.09 time_anyall : all_loop plain: 0.68 psyco: 0.09 ratio: 7.34 time_anyall : any_bool_genexp plain: 2.11 psyco: 2.13 ratio: 0.99 time_anyall : any_bool_listcomp plain: 3.79 psyco: 1.93 ratio: 1.96 time_anyall : any_genexp plain: 1.89 psyco: 1.92 ratio: 0.98 time_anyall : any_listcomp plain: 3.23 psyco: 1.68 ratio: 1.92 time_anyall : any_loop plain: 0.67 psyco: 0.09 ratio: 7.26 time_builtins : chr(i) plain: 2.42 psyco: 0.01 ratio: 197.10 time_builtins : hash(i) plain: 1.23 psyco: 0.37 ratio: 3.30 time_builtins : int(round(f)) plain: 2.64 psyco: 1.51 ratio: 1.75 time_builtins : min plain: 0.54 psyco: 0.52 ratio: 1.03 time_builtins : min_kw plain: 4.42 psyco: 4.40 ratio: 1.00 time_builtins : ord(i) plain: 0.31 psyco: 0.00 ratio: 116.56 time_builtins : pow plain: 0.35 psyco: 0.23 ratio: 1.50 time_builtins : reduce plain: 0.73 psyco: 0.71 ratio: 1.03 time_builtins : round(f) plain: 1.71 psyco: 0.89 ratio: 1.91 time_builtins : sums plain: 0.18 psyco: 0.18 ratio: 1.03 time_fib : matrix plain: 0.43 psyco: 0.36 ratio: 1.19 time_fib : recursive plain: 0.00 psyco: 0.00 ratio: 19.28 time_fib : takahashi plain: 0.41 psyco: 0.32 ratio: 1.27 time_generators: send call loop 1000 plain: 2.65 psyco: 2.94 ratio: 0.90 time_generators: send and loop 1000 plain: 2.95 psyco: 2.54 ratio: 1.16 time_generators: send just many times plain: 1.30 psyco: 0.59 ratio: 2.23 time_generators: iterate just many times plain: 0.65 psyco: 0.55 ratio: 1.17 time_generators: call next just many times plain: 0.92 psyco: 0.63 ratio: 1.45 time_iter : iter plain: 1.45 psyco: 0.59 ratio: 2.47 time_math : floats plain: 2.92 psyco: 1.50 ratio: 1.95 time_properties: method_get plain: 0.94 psyco: 0.12 ratio: 7.88 time_properties: method_set plain: 1.01 psyco: 0.17 ratio: 6.08 time_properties: property_get plain: 0.97 psyco: 0.74 ratio: 1.31 time_properties: property_set plain: 1.02 psyco: 0.92 ratio: 1.12 time_properties: pyproperty_get plain: 1.53 psyco: 1.31 ratio: 1.17 time_properties: pyproperty_set plain: 1.02 psyco: 0.92 ratio: 1.11 time_subdist : subdist(i) plain: 3.65 psyco: 1.64 ratio: 2.23 time_sums : rounding plain: 0.81 psyco: 0.79 ratio: 1.03 From Scott.Daniels at Acm.Org Sat Jul 4 08:28:01 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 05:28:01 -0700 Subject: question of style In-Reply-To: <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> Message-ID: upwestdon wrote: > if not (self.higher and self.lower): > return self.higher or self.lower self.lower = 0 self.higher = 123 ??? More than just None is False From Scott.Daniels at Acm.Org Sat Jul 4 08:31:55 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 05:31:55 -0700 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: Patrick Sabin wrote: > Horace Blegg schrieb: >> You might consider using a VM with 'save-points'. You run the program >> (in a debugger/ida/what have you) to a certain point (logical point >> would be if/ifelse/else statements, etc) and save the VM state. Once >> you've saved, you continue. If you find the path you've taken isn't >> what you are after, you can reload a previous save point and start >> over, trying a different path the next time. > That was my idea to implement it. I thought of taking snapshots of the > current state every time a "unredoable instruction", e.g random number > generation, is done. Remember, storing into a location is destruction. Go over a list of VM instructions and see how many of them are undoable. From tn.pablo at gmail.com Sat Jul 4 09:25:39 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sat, 4 Jul 2009 08:25:39 -0500 Subject: Calling C or C++ Functions inside Python script In-Reply-To: References: Message-ID: On Sat, Jul 4, 2009 at 05:06, Parikshat Dubey wrote: > Hi, > > Is this possible to call C functions(inbuilt or user defined) inside python > script.If yes, please illustrate how it can be done. > Thanks, > Parikshat Dubey >From http://www.swig.org : "SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby. " --- Pablo Torres N. From http Sat Jul 4 09:29:09 2009 From: http (Paul Rubin) Date: 04 Jul 2009 06:29:09 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: <7xws6oa862.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > AFAICT, in order for Maybe and Option to work, you need to have some > kind of static typing system. Am I missing something? I'm not sure. Maybe there could be some kind of syntactic support in a dynamic language (not that I'm suggesting adding that to Python). I've been meaning to look up how Erlang does it. For the limited purpose of regexps that might or might not match, Perl has some special support. A kludgy Python counterpart to Option might be to use a pair (present, x) where present is a bool saying whether the value is available. If present is True, then x is the value. That could make it harder to forget to test the flag before using the value. In the stuff I'm doing, I often find it best to just use a list value, so instead of: y = f(x) if x is not None else None I can just write y = map(f, x) In many cases it later turns out that list really was the natural representation and it ends up growing additional potential elements. I saw some article about database design recently that claimed as a program evolves, all relationships end up becoming many-to-many. It gave as an example, if your program deals with names and addresses, it will eventually have to handle the case where someone has more than one residence address. From davea at ieee.org Sat Jul 4 09:31:44 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 09:31:44 -0400 Subject: question of style In-Reply-To: <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> Message-ID: <4A4F59C0.8030201@ieee.org> upwestdon wrote: > On Jul 2, 1:23 pm, Simon Forman wrote: > >> Hey I was hoping to get your opinions on a sort of minor stylistic >> point. >> These two snippets of code are functionally identical. Which would you >> use and why? >> The first one is easier [for me anyway] to read and understand, but >> slightly less efficient, while the second is [marginally] harder to >> follow but more efficient. >> >> ## First snippet >> >> if self.higher is self.lower is None: return >> if self.lower is None: return self.higher >> if self.higher is None: return self.lower >> >> ## Second snippet >> >> if self.higher is None: >> if self.lower is None: >> return >> return self.lower >> if self.lower is None: >> return self.higher >> >> What do you think? >> >> (One minor point: in the first snippet, the "is None" in the first >> line is superfluous in the context in which it will be used, the only >> time "self.lower is self.higher" will be true is when they are both >> None.) >> > > How about just: > > if not (self.higher and self.lower): > return self.higher or self.lower > > But if self.higher is 0 and self.lower is None, this'll return None, which wasn't the original intent. Without having some documented constraints on the self.higher and lower types and values, it's difficult to simplify further. From neilcrighton at gmail.com Sat Jul 4 09:39:41 2009 From: neilcrighton at gmail.com (Neil Crighton) Date: Sat, 4 Jul 2009 13:39:41 +0000 (UTC) Subject: finding most common elements between thousands of multiple arrays. References: Message-ID: You can join all your arrays into a single big array with concatenate. >>> import numpy as np >>> a = np.concatenate(array_of_arrays) Then count the number of occurrances of each unique element using this trick with searchsorted. This should be pretty fast. >>> a.sort() >>> unique_a = np.unique(a) >>> count = [] >>> for val in unique_a: ... count.append(a.searchsorted(val,side='right') - a.searchsorted(val, side='left')) >>> mostcommonvals = unique_a[np.argsort(count)[-25:]] Neil From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 09:42:06 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 13:42:06 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> Message-ID: <025f4dbf$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: > 2009/7/4 Andre Engels : >> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>> Currently I need to find the most common elements in thousands of >>> arrays within one large array (arround 2 million instances with ~70k >>> unique elements) ... >> There's no better algorithm for the general case. No method of checking >> the matrices using less than 2000000-x look-ups will ensure you that >> there's not a new value with x occurences lurking somewhere. > > Try flattening the arrays into a single large array & sorting it. Then > you can just iterate over the large array counting as you go; you only > ever have to insert into the dict once for each value and there's no > lookups in the dict. You're suggesting to do a whole bunch of work copying 2,000,000 pointers into a single array, then a whole bunch of more work sorting that second array (which is O(N*log N) on average), and then finally iterate over the second array. Sure, that last step will on average involve fewer than O(N) steps, but to get to that point you've already done more work than just iterating over the array-of-arrays in the first place. Now, if you're really lucky, your strategy can be done in fast C code instead of slow Python code, and you might see a speed-up for values of N which aren't too big. But I wouldn't put money on it. Another strategy might be to pre-count elements in each array, as you build or modify them. This will marginally slow down each modification you make to the array, but the payback will be that finding the frequency of any element will be almost instantaneous. -- Steven From davea at ieee.org Sat Jul 4 09:58:39 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 09:58:39 -0400 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F600F.8040402@ieee.org> Scott David Daniels wrote: >
Patrick > Sabin wrote: >> Horace Blegg schrieb: >>> You might consider using a VM with 'save-points'. You run the >>> program (in a debugger/ida/what have you) to a certain point >>> (logical point would be if/ifelse/else statements, etc) and save the >>> VM state. Once you've saved, you continue. If you find the path >>> you've taken isn't what you are after, you can reload a previous >>> save point and start over, trying a different path the next time. >> That was my idea to implement it. I thought of taking snapshots of >> the current state every time a "unredoable instruction", e.g random >> number generation, is done. > Remember, storing into a location is destruction. > Go over a list of VM instructions and see how many of them are undoable. > >
> > Read his suggested approach more carefully. He's not "undoing" anything. He's rolling back to the save-point, and then stepping forward to the desired spot. Except for influences outside his control (eg. file system operations), this approach has to work. Even random will work the same, if the generator uses only data that was restored from the save-point. A typical pseudo-random generator works from a seed, and if the seed is restored as part of rolling back, he's fine. If the snapshot is done via VMWare (for example), he's even okay for file operations, except for network and VM-shared files. "I thought of taking snapshots of the current state every time a "unredoable instruction", e.g random number generation, is done. For every other instruction I count the number of instructions done since the last snapshot. So I can go back one instruction by restoring to the previous state and go the number of instructions minus one forward." DaveA From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 10:10:01 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 14:10:01 GMT Subject: Reversible Debugging References: <4A4E4B8B.7090104@gmail.com> Message-ID: <025f544a$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 09:58:39 -0400, Dave Angel wrote: > Read his suggested approach more carefully. He's not "undoing" > anything. He's rolling back to the save-point, and then stepping > forward to the desired spot. Except for influences outside his control > (eg. file system operations), this approach has to work. Is that anything like saying "Except for all the diseases this drug won't cure, it cures everything"? *wink* -- Steven From Scott.Daniels at Acm.Org Sat Jul 4 10:19:48 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 07:19:48 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> Message-ID: <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> Vilya Harvey wrote: > 2009/7/4 Andre Engels : >> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>> Currently I need to find the most common elements in thousands of >>> arrays within one large array (arround 2 million instances with ~70k >>> unique elements)... > Try flattening the arrays into a single large array & sorting it. Then > you can just iterate over the large array counting as you go; you only > ever have to insert into the dict once for each value and there's no > lookups in the dict.... Actually the next step is to maintain a min-heap as you run down the sorted array. Something like: import numpy as np import heapq def frequency(arr): '''Generate frequency-value pairs from a numpy array''' clustered = arr.flatten() # copy (so can safely sort) clustered.sort() # Bring identical values together scanner = iter(clustered) last = scanner.next() count = 1 for el in scanner: if el == last: count += 1 else: yield count, last last = el count = 1 yield count, last def most_frequent(arr, N): '''Return the top N (freq, val) elements in arr''' counted = frequency(arr) # get an iterator for freq-val pairs heap = [] # First, just fill up the array with the first N distinct for i in range(N): try: heap.append(counted.next()) except StopIteration: break # If we run out here, no need for a heap else: # more to go, switch to a min-heap, and replace the least # element every time we find something better heapq.heapify(heap) for pair in counted: if pair > heap[0]: heapq.heapreplace(heap, pair) return sorted(heap, reverse=True) # put most frequent first. --Scott David Daniels Scott.Daniels at Acm.Org From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 10:38:52 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 14:38:52 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> Message-ID: <025f5b0d$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 13:42:06 +0000, Steven D'Aprano wrote: > On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: > >> 2009/7/4 Andre Engels : >>> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>>> Currently I need to find the most common elements in thousands of >>>> arrays within one large array (arround 2 million instances with ~70k >>>> unique elements) > ... >>> There's no better algorithm for the general case. No method of >>> checking the matrices using less than 2000000-x look-ups will ensure >>> you that there's not a new value with x occurences lurking somewhere. >> >> Try flattening the arrays into a single large array & sorting it. Then >> you can just iterate over the large array counting as you go; you only >> ever have to insert into the dict once for each value and there's no >> lookups in the dict. > > You're suggesting to do a whole bunch of work copying 2,000,000 pointers > into a single array, then a whole bunch of more work sorting that second > array (which is O(N*log N) on average), and then finally iterate over > the second array. Sure, that last step will on average involve fewer > than O(N) steps, Er what? Ignore that last comment -- I don't know what I was thinking. You still have to iterate over all N elements, sorted or not. > but to get to that point you've already done more work > than just iterating over the array-of-arrays in the first place. What it does buy you though, as you pointed out, is reducing the number of explicit dict lookups and writes. However, dict lookups and writes are very fast, fast enough that they're used throughout Python. A line like: count += 1 actually is a dict lookup and write. -- Steven From sajmikins at gmail.com Sat Jul 4 10:54:38 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sat, 4 Jul 2009 07:54:38 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> Message-ID: <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> On Jul 4, 2:03?am, upwestdon wrote: > On Jul 2, 1:23?pm, Simon Forman wrote: > > > > > Hey I was hoping to get your opinions on a sort of minor stylistic > > point. > > These two snippets of code are functionally identical. Which would you > > use and why? > > The first one is easier [for me anyway] to read and understand, but > > slightly less efficient, while the second is [marginally] harder to > > follow but more efficient. > > > ## First snippet > > > if self.higher is self.lower is None: return > > if self.lower is None: return self.higher > > if self.higher is None: return self.lower > > > ## Second snippet > > > if self.higher is None: > > ? ? if self.lower is None: > > ? ? ? ? return > > ? ? return self.lower > > if self.lower is None: > > ? ? return self.higher > > > What do you think? > > > (One minor point: in the first snippet, the "is None" in the first > > line is superfluous in the context in which it will be used, the only > > time "self.lower is self.higher" will be true is when they are both > > None.) > > How about just: > > if not (self.higher and self.lower): > ? ? return self.higher or self.lower That would work too in this case, both higher and lower are expected to be either None or an object (that doesn't override the default all- objects-are-true rule.) Thanks, ~Simon From no.email at please.post Sat Jul 4 11:00:00 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 15:00:00 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: In <7xk52p4tgg.fsf at ruckus.brouhaha.com> Paul Rubin writes: >kj writes: >> sense = cmp(func(hi), func(lo)) >> if sense == 0: >> return None >> target_plus = sense * target + epsilon >> target_minus = sense * target - epsilon >> ... >The code looks confusing to me and in some sense incorrect. Suppose >func(hi)==func(lo)==target. In hindsight, I too think that it is incorrect, but for a different reason. I've rewritten it like this: sense = cmp(func(hi), func(lo)) assert sense != 0, "func is not strictly monotonic in [lo, hi]" I regard the very special case of func(hi)==func(lo)==target as pathological (analogous to the fact that a stopped watch is "exactly right" twice a day), and not one I care to support. Thanks for your feedback! kj From Scott.Daniels at Acm.Org Sat Jul 4 11:03:39 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 08:03:39 -0700 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: Dave Angel wrote: > Scott David Daniels wrote: >> Patrick Sabin wrote: >>> Horace Blegg schrieb: >>>> You might consider using a VM with 'save-points'. You run the >>>> program (in a debugger/ida/what have you) to a certain point >>>> (logical point would be if/ifelse/else statements, etc) and save the >>>> VM state. Once you've saved, you continue. If you find the path >>>> you've taken isn't what you are after, you can reload a previous >>>> save point and start over, trying a different path the next time. >>> That was my idea to implement it. I thought of taking snapshots of >>> the current state every time a "unredoable instruction", e.g random >>> number generation, is done. >> Remember, storing into a location is destruction. >> Go over a list of VM instructions and see how many of them are undoable. > Read his suggested approach more carefully. He's not "undoing" > anything. He's rolling back to the save-point, and then stepping > forward to the desired spot. Right, I did misread "unredoable" as "undoable." However, I suspect a surprising amount of stuff is "unredoable" -- iff the random number generator counts as one of those things. The random number seeder is unredoable with empty args, but running the generator once seeded is predictable (by design). If you don't capture the random number state as part of your "snapshot," _lots_ of C space storage will be in the same class, and you are stuck finding the exceptional "safe to use" cases, rather than the exceptional "unsafe to use." Similarly, system calls about time or _any_ callback (when and where executed) create snapshot points, and I suspect roll forwards will be relatively short. In fact, in some sense the _lack_ of a callback is unredoable. --Scott David Daniels Scott.Daniels at Acm.Org From no.email at please.post Sat Jul 4 11:05:48 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 15:05:48 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Alan G Isaac writes: >> In Alan G Isaac writes: >>> 1. Don't use assertions to test argument values! >On 7/3/2009 12:19 PM kj apparently wrote: >> Out of curiosity, where does this come from? >http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_stmt >"The current code generator emits no code for an assert statement when optimization is requested at compile time." Sorry, this doesn't say anything like "don't use assertions to test argument values". I'm aware of the fact that assertions are silenced under certain circumstances. But where does the principle 1. in your first reply come from? I've never seen it before, and would like to understand the reasoning behind it. kj From vinay_sajip at yahoo.co.uk Sat Jul 4 11:12:36 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Sat, 4 Jul 2009 08:12:36 -0700 (PDT) Subject: ANN: A new version of the Python module which wraps GnuPG has been released. Message-ID: A new version of the Python module which wraps GnuPG has been released. What Does It Do? ================ The gnupg module allows Python programs to make use of the functionality provided by the Gnu Privacy Guard (abbreviated GPG or GnuPG). Using this module, Python programs can encrypt and decrypt data, digitally sign documents and verify digital signatures, manage (generate, list and delete) encryption keys, using proven Public Key Infrastructure (PKI) encryption technology based on OpenPGP. This module is expected to be used with Python versions >= 2.4, as it makes use of the subprocess module which appeared in that version of Python. Development and testing has been carried out on Windows and Ubuntu. This module is a newer version derived from earlier work by Andrew Kuchling, Richard Jones and Steve Traugott. A test suite using unittest is included with the source distribution. Simple usage: >>> import gnupg >>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory') >>> gpg.list_keys() [{ ... 'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2', 'keyid': '197D5DAC68F1AAB2', 'length': '1024', 'type': 'pub', 'uids': ['', 'Gary Gross (A test user) ']}, { ... 'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A', 'keyid': '0C5FEFA7A921FC4A', 'length': '1024', ... 'uids': ['', 'Danny Davis (A test user) ']}] >>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A']) >>> str(encrypted) '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n \nhQIOA/6NHMDTXUwcEAf ... -----END PGP MESSAGE-----\n' >>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret') >>> str(decrypted) 'Hello, world!' >>> signed = gpg.sign("Goodbye, world!", passphrase='secret') >>> verified = verified = gpg.verify(str(signed)) >>> print "Verified" if verified else "Not verified" 'Verified' For more information, visit http://code.google.com/p/python-gnupg/ - as always, your feedback is most welcome (especially bug reports, patches and suggestions for improvement). Enjoy! Cheers Vinay Sajip Red Dove Consultants Ltd. From asm198 at gmail.com Sat Jul 4 11:23:33 2009 From: asm198 at gmail.com (Icarus) Date: Sat, 4 Jul 2009 08:23:33 -0700 (PDT) Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: On Jul 4, 3:21?am, Peter Otten <__pete... at web.de> wrote: > Icarus wrote: > > I'm working on a serial protocol analyzer in python. ?We have an > > application written by someone else in MFC but we need something that > > is cross platform. ?I intended to implement the GUI portion in Tkinter > > but am having trouble. > > > The idea is that I will read messages from the serial port and output > > them to a Tkinter Text object initially. ?Eventually it will have > > other functionality but that's it for the short term. ?I've written > > this little test app to experiment with putting things on the GUI via > > a Queue which is polled by the Tkinter loop. > > > On some machines this code works fine and I get whatever I type in > > displayed in the Text widget. ?On others I get errors like this as > > soon as I start it running. > > > error in background error handler: > > out of stack space (infinite loop?) > > ? ? while executing > > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > > ? ? while execu..." > > > I don't understand why on some machines it works exactly as expected > > and on others it acts the same way Tkinter does when I call functions > > directly from outside the Tkinter thread. ?Does anyone have any > > suggestions? ?The full code as appended below. ?Thanks in advance. > > > [code] > > > import Queue > > > class functionQueue: > > > ? ? def __init__(self, root = None, timeout = 250): > > > ? ? ? ? self.functionQueue = Queue.Queue() > > ? ? ? ? self.root = root > > ? ? ? ? self.timeout = timeout > > > ? ? ? ? if(self.root): > > ? ? ? ? ? ? self.pop_function(root) > > > ? ? def pop_function(self, root = None): > > > ? ? ? ? try: > > ? ? ? ? ? ? funcArgList = self.functionQueue.get(block = False) > > ? ? ? ? except Queue.Empty: > > ? ? ? ? ? ? pass > > ? ? ? ? else: > > ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? funcArgList[0](*funcArgList[1]) > > ? ? ? ? ? ? except: > > ? ? ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? ? ? print "Failed to call function", funcArgList[0] > > ? ? ? ? ? ? ? ? except: > > ? ? ? ? ? ? ? ? ? ? print "Failed to call function" > > > ? ? ? ? if(root): > > ? ? ? ? ? ? root.after(self.timeout, lambda: self.pop_function > > (self.root)) > > > ? ? def add_function(self, function, argList): > > > ? ? ? ? try: > > ? ? ? ? ? ? self.functionQueue.put([function, argList]) > > ? ? ? ? except: > > ? ? ? ? ? ? pass > > > if( __name__ == '__main__'): > > > ? ? import Tkinter > > ? ? import thread > > > ? ? text = Tkinter.Text() > > ? ? text.pack() > > > ? ? myQueue = functionQueue(text, 50) > > > ? ? def gui_loop(): > > ? ? ? ? try: > > ? ? ? ? ? ? text.mainloop() > > ? ? ? ? except: > > ? ? ? ? ? ? import os > > ? ? ? ? ? ? os._exit(1) > > > ? ? thread.start_new_thread(text.mainloop, ()) > > > ? ? while(True): > > ? ? ? ? usrInput = raw_input() > > > ? ? ? ? if(usrInput == "-1"): > > ? ? ? ? ? ? import os > > ? ? ? ? ? ? os._exit(0) > > > ? ? ? ? myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > > ? ? ? ? myQueue.add_function(text.see, ['end']) > > > [/code] > > I can make it work over here by putting the UI into the main thread, as > suggested byhttp://effbot.org/zone/tkinter-threads.htm: > > import Queue > import Tkinter > import threading > > class FunctionQueue: > ? ? # unchanged > > def input_loop(): > ? ? while True: > ? ? ? ? try: > ? ? ? ? ? ? usrInput = raw_input() > ? ? ? ? except EOFError: > ? ? ? ? ? ? break > ? ? ? ? myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > ? ? ? ? myQueue.add_function(text.see, ['end']) > ? ? myQueue.add_function(text.quit, []) > > if __name__ == '__main__': > ? ? text = Tkinter.Text() > ? ? text.pack() > > ? ? myQueue = FunctionQueue(text, 50) > ? ? threading.Thread(target=input_loop).start() > ? ? text.mainloop() > > Peter Peter, thanks for the suggestion. I tried your code exactly on my box and I still get the same results. As soon as I run the script and every time I click on the Text box I get tcl::Bgerror ... just like I mentioned above. I'm fairly certain that I'm not calling Tkinter functions from any other thread but it's acting as though I am as soon as I create the input thread. If I comment out the input loop thread everything is fine but of course that's not terribly useful as a logging box. From tn.pablo at gmail.com Sat Jul 4 11:26:15 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sat, 4 Jul 2009 10:26:15 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On Sat, Jul 4, 2009 at 10:05, kj wrote: >>http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_stmt >>"The current code generator emits no code for an assert statement when optimization is requested at compile time." > > Sorry, this doesn't say anything like "don't use assertions to test > argument values". ?I'm aware of the fact that assertions are silenced > under certain circumstances. ?But where does the principle 1. in > your first reply come from? ?I've never seen it before, and would > like to understand the reasoning behind it. > > kj > -- > http://mail.python.org/mailman/listinfo/python-list > But...if no code is generated for assertions on some occasions, then the parameters would go unchecked, potentially breaking your code in said occasions. -- Pablo Torres N. From aahz at pythoncraft.com Sat Jul 4 11:48:21 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 Jul 2009 08:48:21 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> <7xws6oa862.fsf@ruckus.brouhaha.com> Message-ID: In article <7xws6oa862.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: > >In many cases it later turns out that list really was the natural >representation and it ends up growing additional potential elements. >I saw some article about database design recently that claimed as a >program evolves, all relationships end up becoming many-to-many. It >gave as an example, if your program deals with names and addresses, it >will eventually have to handle the case where someone has more than >one residence address. That's definitely a critical point, and I absolutely agree that this should get rubbed into people's faces. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From dw at botanicus.net Sat Jul 4 11:50:56 2009 From: dw at botanicus.net (David Wilson) Date: Sat, 4 Jul 2009 08:50:56 -0700 (PDT) Subject: Where does setuptools live? Message-ID: I'm trying to create a patch for a diabolical issue I keep running into, but I can't seem to find the setuptools repository. Is it this one? http://svn.python.org/view/sandbox/trunk/setuptools/ It's seen no changes in 9 months. The issue in question is its (ab)use of .svn to directly read working copy information rather than going via the well designed 'svn info -- xml' route, which if done, wouldn't result in setuptools busting every few years, nor require me to downgrade my Subversion installation (OS X packages don't support multiple versions installed at a time), checkout a source tree, use setuptools, then re-upgrade so I can go back to using my other trees. David From davea at ieee.org Sat Jul 4 12:09:34 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 12:09:34 -0400 Subject: Reversible Debugging In-Reply-To: <025f544a$0$20657$c3e8da3@news.astraweb.com> References: <4A4E4B8B.7090104@gmail.com> <025f544a$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4A4F7EBE.4030004@ieee.org> Steven D'Aprano wrote: > On Sat, 04 Jul 2009 09:58:39 -0400, Dave Angel wrote: > > >> Read his suggested approach more carefully. He's not "undoing" >> anything. He's rolling back to the save-point, and then stepping >> forward to the desired spot. Except for influences outside his control >> (eg. file system operations), this approach has to work. >> > > Is that anything like saying "Except for all the diseases this drug won't > cure, it cures everything"? > > *wink* > > > Yeah, pretty close. That's the problem with speaking in concepts, rather than something concrete. Somebody else brought up VM, but that has two meanings these days. The sense I took it (with regard to save-point) was a program like VMWare, where it virtualizes the entire machine. So any program that deals only with the local machine will fit that approach. And yes, I know there are subtleties beyond networks and virtual networks, like time. When you roll the VM back, the time magically stays current. But for a program that deals only with its own process, that tends to just look like some other process suddenly hogged the CPU. Now, if the snapshot is a feature of the Python VM, that's another matter entirely. From amrita at iisermohali.ac.in Sat Jul 4 12:21:12 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 4 Jul 2009 21:51:12 +0530 (IST) Subject: mail Message-ID: <22154.210.212.36.65.1246724472.squirrel@www.iisermohali.ac.in> Hi, I want to know that whether using python programming is it possible to extract chemical shift information about some amino acids of some protein from BMRB(BioMagResBank) or Ref-DB(referenced databank) or not. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From hanooter at gmail.com Sat Jul 4 12:22:48 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 09:22:48 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> Message-ID: <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> OK then. I will try some of the strategies here but I guess things arent looking too good. I need to run this over a dataset that someone pickled. I need to run this 480,000 times so you can see my frustration. So it doesn't need to be "real time" but it would be nice it was done sorting this month. Is there a "bet guess" strategy where it is not 100% accurate but much faster? On Jul 4, 7:38?am, Steven D'Aprano wrote: > On Sat, 04 Jul 2009 13:42:06 +0000, Steven D'Aprano wrote: > > On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: > > >> 2009/7/4 Andre Engels : > >>> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: > >>>> Currently I need to find the most common elements in thousands of > >>>> arrays within one large array (arround 2 million instances with ~70k > >>>> unique elements) > > ... > >>> There's no better algorithm for the general case. No method of > >>> checking the matrices using less than 2000000-x look-ups will ensure > >>> you that there's not a new value with x occurences lurking somewhere. > > >> Try flattening the arrays into a single large array & sorting it. Then > >> you can just iterate over the large array counting as you go; you only > >> ever have to insert into the dict once for each value and there's no > >> lookups in the dict. > > > You're suggesting to do a whole bunch of work copying 2,000,000 pointers > > into a single array, then a whole bunch of more work sorting that second > > array (which is O(N*log N) on average), and then finally iterate over > > the second array. Sure, that last step will on average involve fewer > > than O(N) steps, > > Er what? > > Ignore that last comment -- I don't know what I was thinking. You still > have to iterate over all N elements, sorted or not. > > > but to get to that point you've already done more work > > than just iterating over the array-of-arrays in the first place. > > What it does buy you though, as you pointed out, is reducing the number > of explicit dict lookups and writes. However, dict lookups and writes are > very fast, fast enough that they're used throughout Python. A line like: > > count += 1 > > actually is a dict lookup and write. > > -- > Steven From __peter__ at web.de Sat Jul 4 12:24:06 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 04 Jul 2009 18:24:06 +0200 Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: Icarus wrote: > On Jul 4, 3:21 am, Peter Otten <__pete... at web.de> wrote: >> Icarus wrote: >> > I'm working on a serial protocol analyzer in python. We have an >> > application written by someone else in MFC but we need something that >> > is cross platform. I intended to implement the GUI portion in Tkinter >> > but am having trouble. >> >> > The idea is that I will read messages from the serial port and output >> > them to a Tkinter Text object initially. Eventually it will have >> > other functionality but that's it for the short term. I've written >> > this little test app to experiment with putting things on the GUI via >> > a Queue which is polled by the Tkinter loop. >> >> > On some machines this code works fine and I get whatever I type in >> > displayed in the Text widget. On others I get errors like this as >> > soon as I start it running. >> >> > error in background error handler: >> > out of stack space (infinite loop?) >> > while executing >> > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level >> > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) >> > while execu..." >> >> > I don't understand why on some machines it works exactly as expected >> > and on others it acts the same way Tkinter does when I call functions >> > directly from outside the Tkinter thread. Does anyone have any >> > suggestions? The full code as appended below. Thanks in advance. >> >> > [code] >> >> > import Queue >> >> > class functionQueue: >> >> > def __init__(self, root = None, timeout = 250): >> >> > self.functionQueue = Queue.Queue() >> > self.root = root >> > self.timeout = timeout >> >> > if(self.root): >> > self.pop_function(root) >> >> > def pop_function(self, root = None): >> >> > try: >> > funcArgList = self.functionQueue.get(block = False) >> > except Queue.Empty: >> > pass >> > else: >> > try: >> > funcArgList[0](*funcArgList[1]) >> > except: >> > try: >> > print "Failed to call function", funcArgList[0] >> > except: >> > print "Failed to call function" >> >> > if(root): >> > root.after(self.timeout, lambda: self.pop_function >> > (self.root)) >> >> > def add_function(self, function, argList): >> >> > try: >> > self.functionQueue.put([function, argList]) >> > except: >> > pass >> >> > if( __name__ == '__main__'): >> >> > import Tkinter >> > import thread >> >> > text = Tkinter.Text() >> > text.pack() >> >> > myQueue = functionQueue(text, 50) >> >> > def gui_loop(): >> > try: >> > text.mainloop() >> > except: >> > import os >> > os._exit(1) >> >> > thread.start_new_thread(text.mainloop, ()) >> >> > while(True): >> > usrInput = raw_input() >> >> > if(usrInput == "-1"): >> > import os >> > os._exit(0) >> >> > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) >> > myQueue.add_function(text.see, ['end']) >> >> > [/code] >> >> I can make it work over here by putting the UI into the main thread, as >> suggested byhttp://effbot.org/zone/tkinter-threads.htm: >> >> import Queue >> import Tkinter >> import threading >> >> class FunctionQueue: >> # unchanged >> >> def input_loop(): >> while True: >> try: >> usrInput = raw_input() >> except EOFError: >> break >> myQueue.add_function(text.insert, ['end', usrInput + "\n"]) >> myQueue.add_function(text.see, ['end']) >> myQueue.add_function(text.quit, []) >> >> if __name__ == '__main__': >> text = Tkinter.Text() >> text.pack() >> >> myQueue = FunctionQueue(text, 50) >> threading.Thread(target=input_loop).start() >> text.mainloop() >> >> Peter > > Peter, thanks for the suggestion. I tried your code exactly on my box > and I still get the same results. As soon as I run the script and > every time I click on the Text box I get tcl::Bgerror ... just like I > mentioned above. I'm fairly certain that I'm not calling Tkinter > functions from any other thread but it's acting as though I am as soon > as I create the input thread. > If I comment out the input loop thread everything is fine but of > course that's not terribly useful as a logging box. http://bugs.python.org/issue3835 Could tcl have been built without thread support on the failing machines? Peter From no.email at please.post Sat Jul 4 12:30:10 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 16:30:10 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In "Pablo Torres N." writes: >On Sat, Jul 4, 2009 at 10:05, kj wrote: >>>http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_s= >tmt >>>"The current code generator emits no code for an assert statement when op= >timization is requested at compile time." >> >> Sorry, this doesn't say anything like "don't use assertions to test >> argument values". =C2=A0I'm aware of the fact that assertions are silence= >d >> under certain circumstances. =C2=A0But where does the principle 1. in >> your first reply come from? =C2=A0I've never seen it before, and would >> like to understand the reasoning behind it. >> >> kj >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >But...if no code is generated for assertions on some occasions, then the >parameters would go unchecked, potentially breaking your code in said >occasions. This implies that code that uses *any* assert statement (other than perhaps the trivial and meaningless ones like "assert True") is liable to break, because whatever it is that these assert statements are checking "on some occasions, ... would go unchecked, potentially breaking your code." I'm beginning to think that the original "precept" was simply "cargo cult," i.e. one of those rules that are parroted without being fully understood. As I wrote in my original post, the function I posted was an internal ("helper") function, not to be used outside of the file. This fact was further (ahem) underscored by the leading underscore in the function's name. Under these circumstances, the assert statements seem to me perfectly in keeping with the intended use for them. kj From floris.bruynooghe at gmail.com Sat Jul 4 12:35:47 2009 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Sat, 4 Jul 2009 09:35:47 -0700 (PDT) Subject: Where does setuptools live? References: Message-ID: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> On Jul 4, 4:50?pm, David Wilson wrote: > I'm trying to create a patch for a diabolical issue I keep running > into, but I can't seem to find the setuptools repository. Is it this > one? > > ? ?http://svn.python.org/view/sandbox/trunk/setuptools/ It is, see http://mail.python.org/pipermail/distutils-sig/2009-July/012374.html > It's seen no changes in 9 months. It's setuptools... I'm sure you can find many flamefests on distutils- sig about this. Regards Floris From python at mrabarnett.plus.com Sat Jul 4 12:40:48 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 Jul 2009 17:40:48 +0100 Subject: Problems with using queue in Tkinter application In-Reply-To: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: <4A4F8610.1080202@mrabarnett.plus.com> Icarus wrote: > I'm working on a serial protocol analyzer in python. We have an > application written by someone else in MFC but we need something that > is cross platform. I intended to implement the GUI portion in Tkinter > but am having trouble. > > The idea is that I will read messages from the serial port and output > them to a Tkinter Text object initially. Eventually it will have > other functionality but that's it for the short term. I've written > this little test app to experiment with putting things on the GUI via > a Queue which is polled by the Tkinter loop. > > On some machines this code works fine and I get whatever I type in > displayed in the Text widget. On others I get errors like this as > soon as I start it running. > > error in background error handler: > out of stack space (infinite loop?) > while executing > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > while execu..." > > > I don't understand why on some machines it works exactly as expected > and on others it acts the same way Tkinter does when I call functions > directly from outside the Tkinter thread. Does anyone have any > suggestions? The full code as appended below. Thanks in advance. > > [code] > > import Queue > > class functionQueue: > > def __init__(self, root = None, timeout = 250): > > self.functionQueue = Queue.Queue() > self.root = root > self.timeout = timeout > > if(self.root): > self.pop_function(root) > > > def pop_function(self, root = None): > > try: > funcArgList = self.functionQueue.get(block = False) > except Queue.Empty: > pass > else: > try: > funcArgList[0](*funcArgList[1]) > except: > try: > print "Failed to call function", funcArgList[0] > except: > print "Failed to call function" > > if(root): > root.after(self.timeout, lambda: self.pop_function > (self.root)) > > def add_function(self, function, argList): > > try: > self.functionQueue.put([function, argList]) > except: > pass > > > if( __name__ == '__main__'): > > import Tkinter > import thread > > text = Tkinter.Text() > text.pack() > > myQueue = functionQueue(text, 50) > > def gui_loop(): > try: > text.mainloop() > except: > import os > os._exit(1) > > thread.start_new_thread(text.mainloop, ()) > > while(True): > usrInput = raw_input() > > if(usrInput == "-1"): > import os > os._exit(0) > > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > myQueue.add_function(text.see, ['end']) > > [/code] > Only an idea, but: myQueue = functionQueue(text, 50) runs in the main thread, so the pop_function method is calling Tkinter methods in the main thread every 50ms. But: thread.start_new_thread(text.mainloop, ()) is running the Tkinter main loop in another thread. You might be experiencing a race condition, so different timings on different machines might cause a problem to appear sooner on one than another. From fabiofz at gmail.com Sat Jul 4 13:17:31 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Sat, 4 Jul 2009 14:17:31 -0300 Subject: Python debugger In-Reply-To: <740981.83964.qm@web7901.mail.in.yahoo.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> Message-ID: > Hi, > Could you suggest some python debuggers? > > Thanks, > Srini Pydev has a debugger that supports the common debugger features (watches, multiple threads, breakpoints, conditional breakpoints, step in, out, etc -- http://fabioz.com/pydev/manual_adv_debugger.html ), and pydev extensions adds to that a remote debugger ( http://fabioz.com/pydev/manual_adv_remote_debugger.html ) and a console ( http://fabioz.com/pydev/manual_adv_debug_console.html ). Cheers, Fabio From patrick.just4fun at gmail.com Sat Jul 4 13:21:14 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Sat, 04 Jul 2009 19:21:14 +0200 Subject: Reversible Debugging In-Reply-To: <4A4F7EBE.4030004@ieee.org> References: <4A4E4B8B.7090104@gmail.com> <025f544a$0$20657$c3e8da3@news.astraweb.com> <4A4F7EBE.4030004@ieee.org> Message-ID: <4A4F8F8A.4080702@gmail.com> > Now, if the snapshot is a feature of the Python VM, that's another > matter entirely. I thought of taking a snapshot using fork, which creates a copy of the process. It may not be the most performant, but it should be quite portable. Of course there are some issues with multithreading/multiprocessing. If someone has another idea of taking a snapshot let me know. Using VMWare is not a very elegant way in my opinion. From nagle at animats.com Sat Jul 4 13:33:15 2009 From: nagle at animats.com (John Nagle) Date: Sat, 04 Jul 2009 10:33:15 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. Message-ID: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> As an example of code that really needs to run fast, but is speed-limited by Python's limitations, see "tokenizer.py" in http://code.google.com/p/html5lib/ This is a parser for HTML 5, a piece of code that will be needed in many places and will process large amounts of data. It's written entirely in Python. Take a look at how much work has to be performed per character. This is a good test for Python implementation bottlenecks. Run that tokenizer on HTML, and see where the time goes. ("It should be written in C" is not an acceptable answer.) Python doesn't have a "switch" or "case" statement, and when you need a state machine with many states, that makes for painful, slow code. There's a comment in the code that it would be useful to run a few billion lines of HTML through an instrumented version of the parser to decide in which order the IF statements should be executed. You shouldn't have to do that. Yes, I've read PEP 3103. The big problem is the difficulty of figuring out what's a constant and what might change. If all the cases are constants, case statements are easy. But in Python, the compiler can't tell. Parsers have many named compile-time constants. Python doesn't support named compile-time constants, and this is one of the places where we have to pay the bill for that limitation. Something to think about when you need three more racks of servers because the HTML parser is slow. John Nagle From jeremy.cowles at gmail.com Sat Jul 4 13:44:57 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Sat, 4 Jul 2009 10:44:57 -0700 Subject: Zipped Python? Message-ID: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> Hi all, I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. no install). My intentions are to use it for a distributed computing project (PyMW) where there is no guarantee that Python is installed on the worker machines and an interactive install is out of the question. I've been looking around, but I haven't been able to turn anything useful up so far. Any help would be appreciated. -- Jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: From vilya.harvey at gmail.com Sat Jul 4 13:53:58 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sat, 4 Jul 2009 18:53:58 +0100 Subject: Reversible Debugging In-Reply-To: <4A4F8F8A.4080702@gmail.com> References: <4A4E4B8B.7090104@gmail.com> <025f544a$0$20657$c3e8da3@news.astraweb.com> <4A4F7EBE.4030004@ieee.org> <4A4F8F8A.4080702@gmail.com> Message-ID: <6aef848f0907041053j24d646a4rd6eca49e44a4b0d7@mail.gmail.com> 2009/7/4 Patrick Sabin : > If someone has another idea of taking a snapshot let me know. Using VMWare > is not a > very elegant way in my opinion. Someone implemented the same idea for Java a while ago. They called it "omniscient debugging"; you can find details at http://www.lambdacs.com/debugger/ and a paper about it at http://www.lambdacs.com/debugger/AADEBUG_Mar_03.pdf Another more recent paper on the topic is http://scg.unibe.ch/archive/papers/Lien08bBackInTimeDebugging.pdf I haven't read either of these papers myself, but maybe they'll give you some ideas. Vil. From vilya.harvey at gmail.com Sat Jul 4 13:59:24 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sat, 4 Jul 2009 18:59:24 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <025f5b0d$0$20657$c3e8da3@news.astraweb.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> Message-ID: <6aef848f0907041059n27f88449v16d5fdb4af5c22d2@mail.gmail.com> 2009/7/4 Steven D'Aprano : > On Sat, 04 Jul 2009 13:42:06 +0000, Steven D'Aprano wrote: > >> On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: >> >>> 2009/7/4 Andre Engels : >>>> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>>>> Currently I need to find the most common elements in thousands of >>>>> arrays within one large array (arround 2 million instances with ~70k >>>>> unique elements) >> ... >>>> There's no better algorithm for the general case. No method of >>>> checking the matrices using less than 2000000-x look-ups will ensure >>>> you that there's not a new value with x occurences lurking somewhere. >>> >>> Try flattening the arrays into a single large array & sorting it. Then >>> you can just iterate over the large array counting as you go; you only >>> ever have to insert into the dict once for each value and there's no >>> lookups in the dict. >> >> You're suggesting to do a whole bunch of work copying 2,000,000 pointers >> into a single array, then a whole bunch of more work sorting that second >> array (which is O(N*log N) on average), and then finally iterate over >> the second array. Sure, that last step will on average involve fewer >> than O(N) steps, > > Er what? > > Ignore that last comment -- I don't know what I was thinking. You still > have to iterate over all N elements, sorted or not. > >> but to get to that point you've already done more work >> than just iterating over the array-of-arrays in the first place. > > What it does buy you though, as you pointed out, is reducing the number > of explicit dict lookups and writes. However, dict lookups and writes are > very fast, fast enough that they're used throughout Python. A line like: > > count += 1 > > actually is a dict lookup and write. I did some tests, just to be sure, and you're absolutely right: just creating the flattened list took several hundred (!) times as long as iterating through all the lists in place. Live and learn... Vil. From benjamin.kaplan at case.edu Sat Jul 4 14:03:52 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 4 Jul 2009 14:03:52 -0400 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: On Sat, Jul 4, 2009 at 1:33 PM, John Nagle wrote: > ? As an example of code that really needs to run fast, but is > speed-limited by Python's limitations, see "tokenizer.py" in > > ? ? ? ?http://code.google.com/p/html5lib/ > > This is a parser for HTML 5, a piece of code that will be needed > in many places and will process large amounts of data. It's written > entirely in Python. ?Take a look at how much work has to be performed > per character. > > This is a good test for Python implementation bottlenecks. ?Run > that tokenizer on HTML, and see where the time goes. > > ("It should be written in C" is not an acceptable answer.) > > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. ?There's a comment in the code that it would be useful > to run a few billion lines of HTML through an instrumented version > of the parser to decide in which order the IF statements should be > executed. ?You shouldn't have to do that. > If you're cases are hashable, just use a dict instead of the if chain. Then you get the constant time access to it. def func_a() : ... def func_b(): ... def func_c(): .... case = {"a":func_a, "b":func_b, "c":func_c} case[value]() > Yes, I've read PEP 3103. ?The big problem is the difficulty of figuring > out what's a constant and what might change. ?If all the cases are > constants, > case statements are easy. ?But in Python, the compiler can't tell. > > Parsers have many named compile-time constants. ?Python doesn't support > named compile-time constants, and this is one of the places where we > have to pay the bill for that limitation. > > Something to think about when you need three more racks of servers > because the HTML parser is slow. > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?John Nagle > -- > http://mail.python.org/mailman/listinfo/python-list > From http Sat Jul 4 14:10:48 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:10:48 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> Message-ID: <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Simon Forman writes: > > if not (self.higher and self.lower): > > ? ? return self.higher or self.lower > > That would work too in this case, both higher and lower are expected > to be either None or an object (that doesn't override the default all- > objects-are-true rule.) -1. Objects can support methods like __bool__, __len__ (a tree might use this to compute the number of nodes or something like that), and __nonzero__. If len(object)==0 then the object is treated as false. So that test can fail if a __len__ method gets added to the tree class sometime. Or you might decide you want to support empty trees which would test as false. Anyway, Python's overloading of bool(...) is yet another misfeature and although it's convenient, the "explicit is better than implicit" principle indicates to avoid that sort of trick. From mwilson at the-wire.com Sat Jul 4 14:20:09 2009 From: mwilson at the-wire.com (Mel) Date: Sat, 04 Jul 2009 14:20:09 -0400 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: [ ... ] > Parsers have many named compile-time constants. Python doesn't support > named compile-time constants, and this is one of the places where we > have to pay the bill for that limitation. > > Something to think about when you need three more racks of servers > because the HTML parser is slow. One technique used in such a case is to dispatch different case-handling functions via a dictionary lookup. Mel. From http Sat Jul 4 14:20:20 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:20:20 -0700 Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: <7x4otsux7f.fsf@ruckus.brouhaha.com> kj writes: > sense = cmp(func(hi), func(lo)) > assert sense != 0, "func is not strictly monotonic in [lo, hi]" bisection search usually just requires the function to be continuous and to have its value cross the target somewhere between the endpoints, not be monotonic. > I regard the very special case of func(hi)==func(lo)==target as > pathological (analogous to the fact that a stopped watch is "exactly > right" twice a day), and not one I care to support. I do think you should support that case, under the "do 'nothing' gracefully" principle. Having your equation solver crash if the equation is already solved is like having your sorting function crash if the input is already sorted. E.g. the function should do something reasonable if you end up calling it twice. From http Sat Jul 4 14:29:20 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:29:20 -0700 Subject: Clarity vs. code reuse/generality References: Message-ID: <7xzlbkti7z.fsf@ruckus.brouhaha.com> kj writes: > This implies that code that uses *any* assert statement (other than > perhaps the trivial and meaningless ones like "assert True") is > liable to break, because whatever it is that these assert statements > are checking "on some occasions, ... would go unchecked, potentially > breaking your code." Yes, that implication is absolutely valid. The purpose of assert statements is to debug the code, by checking for conditions that are supposed to be impossible. Unless the program is broken (i.e. the impossible happened), no assert statement should ever trigger. Invalid input data is not considered impossible and doesn't imply a broken program, so assert statements are not the appropriate way to check for it. I like to use a function like def check(condition, msg="data error"): if not condition: raise ValueError, msg ... check (x >= 0, "invalid x") # raises ValueError if x is negative y = sqrt(x) instead. From lie.1296 at gmail.com Sat Jul 4 14:36:38 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 04 Jul 2009 18:36:38 GMT Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: mclovin wrote: > OK then. I will try some of the strategies here but I guess things > arent looking too good. I need to run this over a dataset that someone > pickled. I need to run this 480,000 times so you can see my > frustration. So it doesn't need to be "real time" but it would be nice > it was done sorting this month. > > Is there a "bet guess" strategy where it is not 100% accurate but much > faster? Heuristics? If you don't need 100% accuraccy, you can simply sample 10000 or so element and find the most common element in this small sample space. It should be much faster, though you'll probably need to determine the best cutoff number (too small and you're risking biases, too large and it would be slower). random.sample() might be useful here. From http Sat Jul 4 14:40:19 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:40:19 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <7xvdm8thpo.fsf@ruckus.brouhaha.com> John Nagle writes: > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. ... > There's a comment in the code that it would be useful > to run a few billion lines of HTML through an instrumented version > of the parser to decide in which order the IF statements should be > executed. You shouldn't have to do that. In that particular program it would probably be better to change those if/elif/elif/else constructs to dictionary lookups. I see the program already does that for some large tables. From python at mrabarnett.plus.com Sat Jul 4 14:43:24 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 Jul 2009 19:43:24 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: <7xzlbkti7z.fsf@ruckus.brouhaha.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <4A4FA2CC.1080105@mrabarnett.plus.com> Paul Rubin wrote: > kj writes: >> This implies that code that uses *any* assert statement (other than >> perhaps the trivial and meaningless ones like "assert True") is >> liable to break, because whatever it is that these assert statements >> are checking "on some occasions, ... would go unchecked, potentially >> breaking your code." > > Yes, that implication is absolutely valid. The purpose of assert > statements is to debug the code, by checking for conditions that are > supposed to be impossible. Unless the program is broken (i.e. the > impossible happened), no assert statement should ever trigger. > Technically these are known as "invariants". An assertion will always be True if the program is bug-free, no matter what the user might throw at it; it's not the same as validation. > Invalid input data is not considered impossible and doesn't imply a > broken program, so assert statements are not the appropriate way to > check for it. I like to use a function like > > def check(condition, msg="data error"): > if not condition: raise ValueError, msg > > ... > check (x >= 0, "invalid x") # raises ValueError if x is negative > y = sqrt(x) > > instead. From alan.isaac at gmail.com Sat Jul 4 14:54:02 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sat, 04 Jul 2009 18:54:02 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On 7/4/2009 12:30 PM kj apparently wrote: > I'm beginning to think that the original "precept" was simply "cargo > cult," i.e. one of those rules that are parroted without being > fully understood. Adopting such a view is of course an alternative to attempting to understand, but perhaps less useful. Alan Isaac PS For additional explanation: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html From grante at visi.com Sat Jul 4 15:15:31 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 04 Jul 2009 14:15:31 -0500 Subject: mail References: Message-ID: On 2009-07-04, amrita at iisermohali.ac.in wrote: > I want to know that whether using python programming is it > possible to extract chemical shift information about some > amino acids of some protein from BMRB(BioMagResBank) or > Ref-DB(referenced databank) or not. Yes. I don't know how easy it would be, but I'm pretty confident it is possible. I'm assuming it is currently possible to do so using some sort of software. If it's currently impossible to do so using other methods (e.g. such information doesn't exist or you don't have access to the dabases), then using Python isn't going cause any miracles to happen. -- Grant From Scott.Daniels at Acm.Org Sat Jul 4 15:51:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 12:51:58 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: mclovin wrote: > OK then. I will try some of the strategies here but I guess things > arent looking too good. I need to run this over a dataset that someone > pickled. I need to run this 480,000 times so you can see my > frustration. So it doesn't need to be "real time" but it would be nice > it was done sorting this month. > > Is there a "bet guess" strategy where it is not 100% accurate but much > faster? Well, I timed a run of a version of mine, and the scan is approx 5X longer than the copy-and-sort. Time arr_of_arr.flatten().sort() to see how quickly the copy and sort happens.So you could try a variant exploiting the following property: If you know the minimum length of a run that will be in the top 25, then the value for each of the most-frequent run entries must show up at positions n * stride and (n + 1) * stride (for some n). That should drastically reduce the scan cost, as long as stride is reasonably large. For my uniformly distributed 0..1024 values in 5M x 5M array, About 2.5 sec to flatten and sort. About 15 sec to run one of my heapish thingies. the least frequency encountered: 24716 so, with stride at sum(flattened[:-stride:stride] == flattened[stride::stride]) == 1000 So there are only 1000 points to investigate. With any distribution other than uniform, that should go _way_ down. So just pull out those points, use bisect to get their frequencies, and feed those results into the heap accumulation. --Scott David Daniels From Scott.Daniels at Acm.Org Sat Jul 4 16:01:00 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 13:01:00 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: <7xzlbkti7z.fsf@ruckus.brouhaha.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Invalid input data is not considered impossible and doesn't imply a > broken program, so assert statements are not the appropriate way to > check for it. I like to use a function like > > def check(condition, msg="data error"): > if not condition: raise ValueError, msg > > ... > check (x >= 0, "invalid x") # raises ValueError if x is negative > y = sqrt(x) And I curse such uses, since I don't get to see the troublesome value, or why it is troublesome. In the above case, y = sqrt(x) at least raises ValueError('math domain error'), which is more information than you are providing. How about: ... if x >= 0: raise ValueError('x = %r not allowed (negative)?' % x) ... --Scott David Daniels Scott.Daniels at Acm.Org From sajmikins at gmail.com Sat Jul 4 16:05:36 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sat, 4 Jul 2009 13:05:36 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: <0a175b39-f084-4814-92e0-80b6cfce6130@c36g2000yqn.googlegroups.com> On Jul 4, 2:10?pm, Paul Rubin wrote: > Simon Forman writes: > > > if not (self.higher and self.lower): > > > ? ? return self.higher or self.lower > > > That would work too in this case, both higher and lower are expected > > to be either None or an object (that doesn't override the default all- > > objects-are-true rule.) > > -1. Heh heh heh, I figured someone might call me on this, but I stopped short of adding further elucidation to that parenthetical clause. What I meant (of course, *wink*) was "...an instance of a user-defined class that doesn't override the special double-underscore methods in a way that causes it (the instance) to be considered False in a 'boolean context', as they say in perl." In [1]: class bar: pass ...: In [2]: b = bar() In [3]: bool(b) Out[3]: True > Objects can support methods like __bool__, __len__ (a tree might use > this to compute the number of nodes or something like that), and > __nonzero__. ?If len(object)==0 then the object is treated as false. > So that test can fail if a __len__ method gets added to the tree class > sometime. ?Or you might decide you want to support empty trees which > would test as false. Ya, that's exactly why I stuck to checking explicitly against None in that tree code. :] > Anyway, Python's overloading of bool(...) is yet another misfeature > and although it's convenient, the "explicit is better than implicit" > principle indicates to avoid that sort of trick. ? I really like the "misfeature" myself. Python (to me) seems to relieve you of gory details that get in the way (compare equivalent C+ + and Python code) but still exposes you to gory details that, with care in coding, can make your code easier to grok and more elegant in general. BTW, Paul, kind of a tangent: I reimplemented the same algorithm but using tuples instead of instances (and empty tuples for "NULL" values.) I was trying to mess around in the space you seemed to indicate existed, i.e. a better implementation using other datatypes, but I didn't have a clear idea what I was doing and, as I said, I started by simply re-implementing with a different datatype. Much to my surprise and delight, I discovered the tuple-based BTree was /already/ a "persistent data type"! It was both awesome and a bit of an anti-climax. :] From http Sat Jul 4 16:55:04 2009 From: http (Paul Rubin) Date: 04 Jul 2009 13:55:04 -0700 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <7xbpo0f9sn.fsf@ruckus.brouhaha.com> Scott David Daniels writes: > And I curse such uses, since I don't get to see the troublesome value, > or why it is troublesome. In the above case, y = sqrt(x) at least > raises ValueError('math domain error'), which is more information than > you are providing. > > How about: > if x >= 0: raise ValueError('x = %r not allowed (negative)?' % x) Better still in these situations is to throw to pdb. But yes, you can put a formatted string in a check message. From no.email at please.post Sat Jul 4 17:10:44 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 21:10:44 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> <7x4otsux7f.fsf@ruckus.brouhaha.com> Message-ID: In <7x4otsux7f.fsf at ruckus.brouhaha.com> Paul Rubin writes: >kj writes: >> sense = cmp(func(hi), func(lo)) >> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >bisection search usually just requires the function to be continuous >and to have its value cross the target somewhere between the endpoints, >not be monotonic. Try the algorithm I posted with lo = -pi/4, hi = 2*pi, func = cos, target = -1, and see what you get... >> I regard the very special case of func(hi)==func(lo)==target as >> pathological (analogous to the fact that a stopped watch is "exactly >> right" twice a day), and not one I care to support. >I do think you should support that case, under the "do 'nothing' >gracefully" principle. You keep missing the point that this is an *internal* *helper* *convenience* function, meant to abstract away common logic from a handful of places and thus eliminate some code repetition within a module. It is *not* a library function intended to be called from elsewhere. So talk of "supporting" anything is besides the point. Any internal use of this function that applies it to a non-strictly-monotonic function is, by assumption, an error. kj From no.email at please.post Sat Jul 4 17:13:23 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 21:13:23 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: In <7xzlbkti7z.fsf at ruckus.brouhaha.com> Paul Rubin writes: >kj writes: >> This implies that code that uses *any* assert statement (other than >> perhaps the trivial and meaningless ones like "assert True") is >> liable to break, because whatever it is that these assert statements >> are checking "on some occasions, ... would go unchecked, potentially >> breaking your code." >Yes, that implication is absolutely valid. The purpose of assert >statements is to debug the code, by checking for conditions that are >supposed to be impossible. Precisely. As I've stated elsewhere, this is an internal helper function, to be called only a few times under very well-specified conditions. The assert statements checks that these conditions are as intended. I.e. they are checks against the module writer's programming errors. From no.email at please.post Sat Jul 4 17:14:45 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 21:14:45 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: In MRAB writes: >Paul Rubin wrote: >> kj writes: >>> This implies that code that uses *any* assert statement (other than >>> perhaps the trivial and meaningless ones like "assert True") is >>> liable to break, because whatever it is that these assert statements >>> are checking "on some occasions, ... would go unchecked, potentially >>> breaking your code." >> >> Yes, that implication is absolutely valid. The purpose of assert >> statements is to debug the code, by checking for conditions that are >> supposed to be impossible. Unless the program is broken (i.e. the >> impossible happened), no assert statement should ever trigger. >> >Technically these are known as "invariants". An assertion will always be >True if the program is bug-free, no matter what the user might throw at >it; it's not the same as validation. What *user* are you talking about??? I've stated a bazillion times that this function is meant to be called only from within this module. From hanooter at gmail.com Sat Jul 4 18:06:29 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 15:06:29 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: On Jul 4, 12:51?pm, Scott David Daniels wrote: > mclovin wrote: > > OK then. I will try some of the strategies here but I guess things > > arent looking too good. I need to run this over a dataset that someone > > pickled. I need to run this 480,000 times so you can see my > > frustration. So it doesn't need to be "real time" but it would be nice > > it was done sorting this month. > > > Is there a "bet guess" strategy where it is not 100% accurate but much > > faster? > > Well, I timed a run of a version of mine, and the scan is approx 5X > longer than the copy-and-sort. ?Time arr_of_arr.flatten().sort() to > see how quickly the copy and sort happens.So you could try a variant > exploiting the following property: > ? ? ?If you know the minimum length of a run that will be in the top 25, > then the value for each of the most-frequent run entries must show up at > positions n * stride and (n + 1) * stride (for some n). ?That should > drastically reduce the scan cost, as long as stride is reasonably large. > > For my uniformly distributed 0..1024 values in 5M x 5M array, > About 2.5 sec to flatten and sort. > About 15 sec to run one of my heapish thingies. > the least frequency encountered: 24716 > so, with stride at > > sum(flattened[:-stride:stride] == flattened[stride::stride]) == 1000 > So there are only 1000 points to investigate. > With any distribution other than uniform, that should go _way_ down. > So just pull out those points, use bisect to get their frequencies, and > feed those results into the heap accumulation. > > --Scott David Daniels I dont quite understand what you are saying but I know this: the times the most common element appears varies greatly. Sometimes it appears over 1000 times, and some times it appears less than 50. It all depends on the density of the arrays I am analyzing. like I said I need to do this 480,000 times so to get this done realistically I need to analyse about 5 a second. It appears that the average matrix size contains about 15 million elements. I threaded my program using your code and I did about 1,000 in an hour so it is still much too slow. When I selected 1 million random elements to count, 8 out of the top 10 of those were in the top 25 of the precise way and 18 of the 25 were in the top 25 of the precise way. so I suppose that could be an option. From sajmikins at gmail.com Sat Jul 4 18:22:47 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sat, 4 Jul 2009 15:22:47 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: Message-ID: On Jul 4, 12:30?pm, kj wrote: > In "Pablo Torres N." writes: > > > > >On Sat, Jul 4, 2009 at 10:05, kj wrote: > >>>http://docs.python.org/reference/simple_stmts.html#grammar-token-asse... > >tmt > >>>"The current code generator emits no code for an assert statement when op= > >timization is requested at compile time." > > >> Sorry, this doesn't say anything like "don't use assertions to test > >> argument values". =C2=A0I'm aware of the fact that assertions are silence= > >d > >> under certain circumstances. =C2=A0But where does the principle 1. in > >> your first reply come from? =C2=A0I've never seen it before, and would > >> like to understand the reasoning behind it. > > >> kj > >> -- > >>http://mail.python.org/mailman/listinfo/python-list > > >But...if no code is generated for assertions on some occasions, then the > >parameters would go unchecked, potentially breaking your code in said > >occasions. > > This implies that code that uses *any* assert statement (other than > perhaps the trivial and meaningless ones like "assert True") is > liable to break, because whatever it is that these assert statements > are checking "on some occasions, ... would go unchecked, potentially > breaking your code." > > I'm beginning to think that the original "precept" was simply "cargo > cult," i.e. one of those rules that are parroted without being > fully understood. > > As I wrote in my original post, the function I posted was an internal > ("helper") function, not to be used outside of the file. ?This fact > was further (ahem) underscored by the leading underscore in the > function's name. ?Under these circumstances, the assert statements > seem to me perfectly in keeping with the intended use for them. > > kj Assertions are for you, the programmer, to check that your understanding of the code is correct (or not, i.e. if the assertion fails.) It's unfortunately not uncommon to see code where the assert statement is used as an integral part of the processing, or, in other words, where running the code with '-O' will cause changes in the way the it runs. In the code you posted, the assert statements seem to be checking that other code/algorithms aren't (will never) pass that function "out of bounds" arguments. Perfectly valid IMO. Since it's an internal helper function you can (should) know that client code will /always/ call it with valid values, the asserts simply make your intuition or knowledge explicit in a way that will show up dramatically if you're wrong about that (i.e. if there's some problem elsewhere in the code feeding that function.) Assertions should never fail, which is why you can (or should be able to without breaking your code) remove them entirely and know that your program will run identically. That's why folks are quick to jump on cases where assertions are used as control-flow constructs. (In your function, however, they aren't.) In a sense, assertions /are/ meaningless, except to the programmer. FWIW, you can use "if __debug__:" and put anything you like in the statement suite and the whole if statement goes away when run with '- O'. Sort of a "super-assert". Regards, ~Simon From http Sat Jul 4 18:24:01 2009 From: http (Paul Rubin) Date: 04 Jul 2009 15:24:01 -0700 Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> <7x4otsux7f.fsf@ruckus.brouhaha.com> Message-ID: <7xljn484u6.fsf@ruckus.brouhaha.com> kj writes: > >bisection search usually just requires the function to be continuous > >and to have its value cross the target somewhere between the endpoints, > >not be monotonic. > > Try the algorithm I posted with lo = -pi/4, hi = 2*pi, func = cos, > target = -1, and see what you get... Sorry, my comment was poorly phrase. Bisection search usually is phrased in terms of solving f(x)=0 where f is continuous and if the initial endpoints are a and b, then f(a) and f(b) have opposite sign. > You keep missing the point that this is an *internal* *helper* > *convenience* function, ... Any internal use of this function that > applies it to a non-strictly-monotonic function is, by assumption, > an error. In that case there are better algorithms you can use. But didn't this thread start out asking what version of the code was best suited for presentation to students? In that case, the function's preconditions should be stated explicitly and not be narrower than necessary. From python at mrabarnett.plus.com Sat Jul 4 18:29:41 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 Jul 2009 23:29:41 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <4A4FD7D5.6050201@mrabarnett.plus.com> mclovin wrote: [snip] > like I said I need to do this 480,000 times so to get this done > realistically I need to analyse about 5 a second. It appears that the > average matrix size contains about 15 million elements. > > I threaded my program using your code and I did about 1,000 in an hour > so it is still much too slow. > > When I selected 1 million random elements to count, 8 out of the top > 10 of those were in the top 25 of the precise way and 18 of the 25 > were in the top 25 of the precise way. so I suppose that could be an > option. The values are integers, aren't they? What is the range of values? From hanooter at gmail.com Sat Jul 4 18:33:38 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 15:33:38 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: On Jul 4, 3:29?pm, MRAB wrote: > mclovin wrote: > > [snip] > > > like I said I need to do this 480,000 times so to get this done > > realistically I need to analyse about 5 a second. It appears that the > > average matrix size contains about 15 million elements. > > > I threaded my program using your code and I did about 1,000 in an hour > > so it is still much too slow. > > > When I selected 1 million random elements to count, 8 out of the top > > 10 of those were in the top 25 of the precise way and 18 of the 25 > > were in the top 25 of the precise way. so I suppose that could be an > > option. > > The values are integers, aren't they? What is the range of values? There are appox 550k unique values with a range of 0-2million with gaps. From pavlovevidence at gmail.com Sat Jul 4 18:47:57 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 4 Jul 2009 15:47:57 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> Message-ID: <94eaeb9d-fe90-45fb-99ee-8846eb8cb19e@r33g2000yqn.googlegroups.com> On Jul 3, 5:34?am, Jack Diederich wrote: > On Thu, Jul 2, 2009 at 2:36 PM, Carl Banks wrote: > > Warning: objects with a __del__ attribute prevent reference cycle > > detection, which can potentially lead to memory (and resource) leaks. > > So you must be careful to avoid creating reference loops with that > > object. > > WARNING-er: As Carl points out, adding a __del__ method actually makes > it /less/ likely that your object will be cleaned up. No I wasn't pointing that out, and I don't really agree with it. (You can't really say it is "more likely" or "less likely" without considering the skill and patience of the programmer, and there are many programmers who are up to the task.) __del__ can succeed with care, and if you are willing to give up a certain amount of flexibility and portability I don't recommend going that way. For one thing RAII isn't quite so wonderful in Python's dynamic environment as it is in C++'s static universe, and people seem to expect more from it than they get. Second, relying on implementation-dependent behavior is not a good thing and can set you up for problems later, something few people expect to happen to them but it does more often than they think. (Search comp.lang.python for a thread started by Warren DeLano for an example of someone who got bit hard by such a change he should have seen coming.) But still I leave that for them to risk for themselves. [snip] >?If you care enough about a resource to write a __del__ method > you care enough to clean it up explicitly. ?'with' blocks are very > nice for that. The OP already said with blocks won't suffice since the resources are long-lived objects that aren't limited to a single scope. Carl Banks From Scott.Daniels at Acm.Org Sat Jul 4 18:50:23 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 15:50:23 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <4A4FDCAF.8010209@Acm.Org> mclovin wrote: > On Jul 4, 12:51 pm, Scott David Daniels wrote: >> mclovin wrote: >>> OK then. I will try some of the strategies here but I guess things >>> arent looking too good. I need to run this over a dataset that someone >>> pickled. I need to run this 480,000 times so you can see my >>> frustration. So it doesn't need to be "real time" but it would be nice >>> it was done sorting this month. >>> Is there a "bet guess" strategy where it is not 100% accurate but much >>> faster? >> >> Well, I timed a run of a version of mine, and the scan is approx 5X >> longer than the copy-and-sort. Time arr_of_arr.flatten().sort() to >> see how quickly the copy and sort happens.So you could try a variant >> exploiting the following property: >> If you know the minimum length of a run that will be in the top 25, >> then the value for each of the most-frequent run entries must show up at >> positions n * stride and (n + 1) * stride (for some n). That should >> drastically reduce the scan cost, as long as stride is reasonably large.... >> >> sum(flattened[:-stride:stride] == flattened[stride::stride]) == 1000 >> So there are only 1000 points to investigate. >> With any distribution other than uniform, that should go _way_ down. >> So just pull out those points, use bisect to get their frequencies, and >> feed those results into the heap accumulation. >> >> --Scott David Daniels > > I dont quite understand what you are saying but I know this: the times > the most common element appears varies greatly. Sometimes it appears > over 1000 times, and some times it appears less than 50. It all > depends on the density of the arrays I am analyzing. Here's a heuristic replacement for my previous frequency code: I've tried to mark where you could fudge numbers if the run time is at all close. def frequency(arr_of_arr, N, stride=100) '''produce (freq, value) pairs for data in arr_of_arr. Tries to produce > N pairs. stride is a guess at half the length of the shortest run in the top N runs. ''' # if the next two lines are too slow, this whole approach is toast data = arr_of_arr.flatten() # big allocation data.sort() # a couple of seconds for 25 million ints # stride is a length forcing examination of a run. sampled = data[::stride] # Note this is a view into data, and is still sorted. # We know that any run of length 2 * stride - 1 in data _must_ have # consecutive entries in sampled. Compare them "in parallel" matches = sampled[:-1] == sampled[1:] # matches is True or False for stride-separated values from sampled candidates = sum(matches) # count identified matches # while candidates is huge, keep trying with a larger stride while candidates > N *10: # 10 -- heuristic stride *= 2 # # heuristic increase sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = sum(matches) # if we got too few, move stride down: while candidates < N * 3: # heuristic slop for long runs stride //= 2 # heuristic decrease sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = sum(matches) # Here we have a "nice" list of candidates that is likely # to include every run we actually want. sampled[matches] is # the sorted list of candidate values. It may have duplicates former = None past = 0 # In the loop here we only use sampled to the pick values we # then go find in data. We avoid checking for same value twice for value in sampled[matches]: if value == former: continue # A long run: multiple matches in sampled former = value # Make sure we only try this one once # find the beginning of the run start = bisect.bisect_left(data, value, past) # find the end of the run (we know it is at least stride long) past = bisect.bisect_right(data, value, start + stride) yield past - start, value # produce frequency, value data --Scott David Daniels Scott.Daniels at Acm.Org From python at mrabarnett.plus.com Sat Jul 4 19:05:50 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 05 Jul 2009 00:05:50 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <4A4FE04E.1050202@mrabarnett.plus.com> mclovin wrote: > On Jul 4, 3:29 pm, MRAB wrote: >> mclovin wrote: >> >> [snip] >> >>> like I said I need to do this 480,000 times so to get this done >>> realistically I need to analyse about 5 a second. It appears that the >>> average matrix size contains about 15 million elements. >>> I threaded my program using your code and I did about 1,000 in an hour >>> so it is still much too slow. >>> When I selected 1 million random elements to count, 8 out of the top >>> 10 of those were in the top 25 of the precise way and 18 of the 25 >>> were in the top 25 of the precise way. so I suppose that could be an >>> option. >> The values are integers, aren't they? What is the range of values? > > There are appox 550k unique values with a range of 0-2million with > gaps. I've done a little experimentation with lists (no numpy involved) and found that I got a x2 speed increase if I did the counting using a list, something like this: counts = [0] * 2000000 for x in values: counts[x] += 1 counts = dict(e for e in enumerate(values) if e[1] != 0) From hkmshb at gmail.com Sat Jul 4 19:18:50 2009 From: hkmshb at gmail.com (Klone) Date: Sat, 4 Jul 2009 16:18:50 -0700 (PDT) Subject: Is code duplication allowed in this instance? References: <4d99b710-45f1-42dc-b80c-1894af26db42@p29g2000yqh.googlegroups.com> Message-ID: <46ce50ea-0272-4285-ab06-65c4e53a1e99@k8g2000yqn.googlegroups.com> Thank you all. I think I get the gist and am about trying your suggestions. From nagle at animats.com Sat Jul 4 19:35:08 2009 From: nagle at animats.com (John Nagle) Date: Sat, 04 Jul 2009 16:35:08 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xvdm8thpo.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> Message-ID: <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Paul Rubin wrote: > John Nagle writes: >> Python doesn't have a "switch" or "case" statement, and when >> you need a state machine with many states, that makes for painful, >> slow code. ... >> There's a comment in the code that it would be useful >> to run a few billion lines of HTML through an instrumented version >> of the parser to decide in which order the IF statements should be >> executed. You shouldn't have to do that. > > In that particular program it would probably be better to change those > if/elif/elif/else constructs to dictionary lookups. I see the program > already does that for some large tables. A dictionary lookup (actually, several of them) for every input character is rather expensive. Tokenizers usually index into a table of character classes, then use the character class index in a switch statement. This is an issue that comes up whenever you have to parse some formal structure, from XML/HTML to Pickle to JPEG images to program source. If Python could figure out what's a constant and what isn't during compilation, this sort of thing could be much more efficient. In fact, you don't even need a switch statement at the source level, if the language is such that the compiler can figure out when "elif" clauses are mutually exclusive. The temptation is to write tokenizers in C, but that's an admission of language design failure. (A general problem with Python is "hidden dynamism". That is, changes to variables that can't be found by examining the source. This is a killer for optimizations. One could take the position that any module variable with exactly one visible assignment to it is, in fact, only assigned in one place, and if the right hand side is a constant, the variable is a constant. This would break some programs doing funny stuff with "eval", or using some of the backdoor ways to modify variables, but that's very rare in practice. In return, you get the ability to hard-compile more of Python into fast code. I'm thinking Shed Skin here, not yet another attempt at a JIT system.) On the other hand, trying to do this in Perl, where you can't even index strings, is far worse. John Nagle From http Sat Jul 4 19:48:19 2009 From: http (Paul Rubin) Date: 04 Jul 2009 16:48:19 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <7xfxdcqabg.fsf@ruckus.brouhaha.com> John Nagle writes: > A dictionary lookup (actually, several of them) for every > input character is rather expensive. Tokenizers usually index into > a table of character classes, then use the character class index in > a switch statement. Maybe you could use a regexp (and then have -two- problems...) to find the token boundaries, then a dict to identify the actual token. Tables of character classes seem a bit less attractive in the Unicode era than in the old days. From davea at ieee.org Sat Jul 4 19:53:48 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 19:53:48 -0400 Subject: Zipped Python? In-Reply-To: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> References: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> Message-ID: <4A4FEB8C.4040302@ieee.org> Jeremy Cowles wrote: > Hi all, > > I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. no > install). My intentions are to use it for a distributed computing project > (PyMW) where there is no guarantee that Python is installed on the worker > machines and an interactive install is out of the question. > > I've been looking around, but I haven't been able to turn anything useful up > so far. Any help would be appreciated. > > -- > Jeremy > > > How about portable python: http://www.portablepython.com/ or Movable Python: http://www.voidspace.org.uk/python/movpy/ No experience with either, but they bear looking at. From matt at tplus1.com Sat Jul 4 20:03:38 2009 From: matt at tplus1.com (Matthew Wilson) Date: Sun, 05 Jul 2009 00:03:38 GMT Subject: Does cProfile include IO wait time? Message-ID: I have a command-line script that loads about 100 yaml files. It takes 2 or 3 seconds. I profiled my code and I'm using pstats to find what is the bottleneck. Here's the top 10 functions, sorted by internal time: In [5]: _3.sort_stats('time').print_stats(10) Sat Jul 4 13:25:40 2009 pitz_prof 756872 function calls (739759 primitive calls) in 8.621 CPU seconds Ordered by: internal time List reduced from 1700 to 10 due to restriction <10> ncalls tottime percall cumtime percall filename:lineno(function) 15153 0.446 0.000 0.503 0.000 build/bdist.linux-i686/egg/yaml/reader.py:134(forward) 30530 0.424 0.000 0.842 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:142(need_more_tokens) 98037 0.423 0.000 0.423 0.000 build/bdist.linux-i686/egg/yaml/reader.py:122(peek) 1955 0.415 0.000 1.265 0.001 build/bdist.linux-i686/egg/yaml/scanner.py:1275(scan_plain) 69935 0.381 0.000 0.381 0.000 {isinstance} 18901 0.329 0.000 3.908 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:113(check_token) 5414 0.277 0.000 0.794 0.000 /home/matt/projects/pitz/pitz/__init__.py:34(f) 30935 0.258 0.000 0.364 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:276(stale_possible_simple_keys) 18945 0.192 0.000 0.314 0.000 /usr/local/lib/python2.6/uuid.py:180(__cmp__) 2368 0.172 0.000 1.345 0.001 build/bdist.linux-i686/egg/yaml/parser.py:268(parse_node) I expected to see a bunch of my IO file-reading code in there, but I don't. So this makes me think that the profiler uses CPU time, not clock-on-the-wall time. I'm not an expert on python profiling, and the docs seem sparse. Can I rule out IO as the bottleneck here? How do I see the IO consequences? TIA Matt From jeremy.cowles at gmail.com Sat Jul 4 20:03:44 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Sat, 4 Jul 2009 17:03:44 -0700 Subject: Zipped Python? In-Reply-To: <4A4FEB8C.4040302@ieee.org> References: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> <4A4FEB8C.4040302@ieee.org> Message-ID: <373cf0740907041703g5250fc9cg346c2c6f2b6d992c@mail.gmail.com> > > I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. no >> install). My intentions are to use it for a distributed computing project >> (PyMW) where there is no guarantee that Python is installed on the worker >> machines and an interactive install is out of the question. >> >> I've been looking around, but I haven't been able to turn anything useful >> up >> so far. Any help would be appreciated. >> >> >> How about portable python: > http://www.portablepython.com/ > > or Movable Python: > http://www.voidspace.org.uk/python/movpy/ > Sorry, forgot - I need something that will work for Windows, Linux and Mac. It could be 3 different distros (zips), but I need something for all three. Both portable python and movable Python are Windows only. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Sat Jul 4 20:12:22 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Jul 2009 12:12:22 +1200 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> Message-ID: In message <1beffd94-cfe6-4cf6- bd48-2ccac8637796 at j32g2000yqh.googlegroups.com>, ryles wrote: >>>> # Oh... yeah. I really *did* want 'is None' and not '== None' which >>>> # iter() will do. Sorry guys! > > Please don't let this happen to you too ;) Strange. others have got told off for using "== None" instead of "is None" , and yet it turns out Python itself does exactly the same thing. From emile at fenx.com Sat Jul 4 21:00:29 2009 From: emile at fenx.com (Emile van Sebille) Date: Sat, 04 Jul 2009 18:00:29 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: Message-ID: On 7/4/2009 12:33 AM mclovin said... > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating I ** up the count on the corrosponding dictionary element ** Right at this point, instead of or in addition to counting, why not save the large array index in a list? Then when you've identified the 25 most common elements you'll already have a list of pointer to the instances to work from. Emile From sgm at objexx.com Sat Jul 4 21:15:43 2009 From: sgm at objexx.com (SK) Date: Sat, 4 Jul 2009 18:15:43 -0700 (PDT) Subject: multiprocessing and freezing on Windows References: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Message-ID: To add a bit more information, I found that I needed to patch get_command_line in multiprocessing/forking.py replacing: if getattr(sys, 'frozen', False): return [sys.executable, '--multiprocessing-fork'] else: prog = 'from multiprocessing.forking import main; main()' return [_python_exe, '-c', prog, '--multiprocessing-fork'] with: if WINEXE: prog = 'from multiprocessing.forking import main; main()' return [_python_exe, '-c', prog, '--multiprocessing-fork'] elif getattr(sys, 'frozen', False): return [sys.executable, '--multiprocessing-fork'] else: prog = 'from multiprocessing.forking import main; main()' return [_python_exe, '-c', prog, '--multiprocessing-fork'] and then using py2exe I can get a working "frozen" package if I include pythonw.exe and a subset of Python's Lib directory in my package and call multiprocessing.set_executable to point to that pythonw.exe. Does the above patch address a real bug? Is packaging with multiprocessing supposed to be this hard? If so, some documentation is needed. From nobody at nowhere.com Sat Jul 4 21:25:38 2009 From: nobody at nowhere.com (Nobody) Date: Sun, 05 Jul 2009 02:25:38 +0100 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: On Sat, 04 Jul 2009 16:35:08 -0700, John Nagle wrote: > The temptation is to write tokenizers in C, but that's an admission > of language design failure. The only part that really needs to be written in C is the DFA loop. The code to construct the state table from regexps could be written entirely in Python, but I don't see any advantage to doing so. From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:27:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:27:30 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <025ff310$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 21:14:45 +0000, kj wrote: >>Technically these are known as "invariants". An assertion will always be >>True if the program is bug-free, no matter what the user might throw at >>it; it's not the same as validation. > > What *user* are you talking about??? I've stated a bazillion times that > this function is meant to be called only from within this module. In that case, you would be the user, and like any user, you might accidentally call the function with invalid data. I believe this discussion started because you are presenting this as code for novices. In that case, it is absolutely important that you start off by teaching them the Right Way to do things. As a general rule, the Right Way is to do an explicit test and raise rather than use assert. In production code, "internal use only" code is a grey area where assert is sometimes justified (although I'll point out that in practice, code written for internal use only has a habit of being called by others). But you're not writing production code, you're writing *teaching code*, where even more important than code correctness is user education. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:31:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:31:23 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <025ff3f9$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 15:06:29 -0700, mclovin wrote: > like I said I need to do this 480,000 times so to get this done > realistically I need to analyse about 5 a second. It appears that the > average matrix size contains about 15 million elements. Have you considered recording the element counts as you construct the arrays? This will marginally increase the time it takes to build the array, but turn finding the most frequent elements into a very quick operation. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:35:31 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:35:31 GMT Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> Message-ID: <025ff4f1$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 12:12:22 +1200, Lawrence D'Oliveiro wrote: > In message <1beffd94-cfe6-4cf6- > bd48-2ccac8637796 at j32g2000yqh.googlegroups.com>, ryles wrote: > >>>>> # Oh... yeah. I really *did* want 'is None' and not '== None' which >>>>> # iter() will do. Sorry guys! >> >> Please don't let this happen to you too ;) > > Strange. others have got told off for using "== None" instead of "is > None" > , > and yet it turns out Python itself does exactly the same thing. That's not "strange", that's a bug. Did you report it to the tracker? -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:36:13 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:36:13 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: <025ff51b$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 11:10:48 -0700, Paul Rubin wrote: > Anyway, Python's overloading of bool(...) is yet another misfeature and > although it's convenient, the "explicit is better than implicit" > principle indicates to avoid that sort of trick. "Overloading of bool()"? I don't understand what that means -- you mean you dislike the ability to overload __bool__ (__nonzero__ in older Pythons)? That seems strange. Python allows you to overload __int__ and __str__ and __float__, why is __bool__ a misfeature? If that's what you mean, then I'm perplexed. Or do you mean that all Python objects are interpretable in a truth context? If that's what you mean, then I'm not perplexed, I'm sure you're utterly wrong. Certain people -- a tiny minority -- keep trying to argue that the ability to say "if obj" for arbitrary objects is somehow a bad thing, and their arguments seem to always boil down to: "If you write code that assumes that only bools have a truth value, then surprising things will happen because all objects have a truth value." Well duh. If you think you have a better reason for calling the truth value of arbitrary objects a "misfeature", I'd like to hear it. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:39:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:39:33 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> Message-ID: <025ff5e2$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 07:19:48 -0700, Scott David Daniels wrote: > Actually the next step is to maintain a min-heap as you run down the > sorted array. Something like: Not bad. I did some tests on it, using the following sample data: arr = np.array([xrange(i, i+7000) for i in xrange(143)] + [[750]*7000] + [xrange(3*i, 3*i+7000) for i in xrange(142)]) and compared your code against the following simple function: def count(arr, N): D = {} for v in arr: for x in v: D[x] = D.get(x, 0) + 1 freq = [] for el, f in D.iteritems(): freq.append((f, el)) return sorted(freq, reverse=True)[:N] As a rough figure, your min-heap code is approximately twice as fast as mine. To the OP: I think you should start profiling your code and find out exactly *where* it is slow and concentrate on that. I think that trying a heuristic to estimate the most frequent elements by taking a random sample is likely to be a mistake -- whatever you're trying to accomplish with the frequency counts, the use of such a heuristic will mean that you're only approximately accomplishing it. -- Steven From ben+python at benfinney.id.au Sat Jul 4 22:09:12 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 05 Jul 2009 12:09:12 +1000 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <87tz1rrid3.fsf@benfinney.id.au> John Nagle writes: > A dictionary lookup (actually, several of them) for every input > character is rather expensive. Tokenizers usually index into a table > of character classes, then use the character class index in a switch > statement. > > This is an issue that comes up whenever you have to parse some > formal structure, from XML/HTML to Pickle to JPEG images to program > source. > [?] > The temptation is to write tokenizers in C, but that's an admission > of language design failure. This sounds like a job for Pyparsing. -- \ ?Better not take a dog on the space shuttle, because if he | `\ sticks his head out when you're coming home his face might burn | _o__) up.? ?Jack Handey | Ben Finney From benjamin.kaplan at case.edu Sat Jul 4 22:28:23 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 4 Jul 2009 22:28:23 -0400 Subject: Zipped Python? In-Reply-To: <373cf0740907041703g5250fc9cg346c2c6f2b6d992c@mail.gmail.com> References: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> <4A4FEB8C.4040302@ieee.org> <373cf0740907041703g5250fc9cg346c2c6f2b6d992c@mail.gmail.com> Message-ID: On Sat, Jul 4, 2009 at 8:03 PM, Jeremy Cowles wrote: >>> I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. >>> no >>> install). My intentions are to use it for a distributed computing project >>> (PyMW) where there is no guarantee that Python is installed on the worker >>> machines and an interactive install is out of the question. >>> >>> I've been looking around, but I haven't been able to turn anything useful >>> up >>> so far. Any help would be appreciated. >>> >>> >> How about portable python: >> ? ? http://www.portablepython.com/ >> >> or Movable Python: >> ? ? ?http://www.voidspace.org.uk/python/movpy/ > > Sorry, forgot - I need something that will work for Windows, Linux and Mac. > It could be 3 different distros (zips), but I need something for all three. > Both portable python and movable Python are Windows only. OS X and most, if not all, Linux distributions come with Python preinstalled (they use them for system features) so no one bothered to make something like Portable Python for them. > -- > http://mail.python.org/mailman/listinfo/python-list > > From nagle at animats.com Sat Jul 4 23:15:11 2009 From: nagle at animats.com (John Nagle) Date: Sat, 04 Jul 2009 20:15:11 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xfxdcqabg.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> Message-ID: <4a501a5e$0$1640$742ec2ed@news.sonic.net> Paul Rubin wrote: > John Nagle writes: >> A dictionary lookup (actually, several of them) for every >> input character is rather expensive. Tokenizers usually index into >> a table of character classes, then use the character class index in >> a switch statement. > > Maybe you could use a regexp (and then have -two- problems...) to > find the token boundaries, then a dict to identify the actual token. > Tables of character classes seem a bit less attractive in the Unicode > era than in the old days. I want to see a regular expression that expresses the HTML 5 token parsing rules, including all the explicitly specified error handling. Here's some actual code, from "tokenizer.py". This is called once for each character in an HTML document, when in "data" state (outside a tag). It's straightforward code, but look at all those dictionary lookups. def dataState(self): data = self.stream.char() # Keep a charbuffer to handle the escapeFlag if self.contentModelFlag in\ (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): if len(self.lastFourChars) == 4: self.lastFourChars.pop(0) self.lastFourChars.append(data) # The rest of the logic if data == "&" and self.contentModelFlag in\ (contentModelFlags["PCDATA"], contentModelFlags["RCDATA"]) and not\ self.escapeFlag: self.state = self.states["entityData"] elif data == "-" and self.contentModelFlag in\ (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) and not\ self.escapeFlag and "".join(self.lastFourChars) == "": self.escapeFlag = False self.tokenQueue.append({"type": "Characters", "data":data}) elif data == EOF: # Tokenization ends. return False elif data in spaceCharacters: # Directly after emitting a token you switch back to the "data # state". At that point spaceCharacters are important so they are # emitted separately. self.tokenQueue.append({"type": "SpaceCharacters", "data": data + self.stream.charsUntil(spaceCharacters, True)}) # No need to update lastFourChars here, since the first space will # have already broken any sequences else: chars = self.stream.charsUntil(("&", "<", ">", "-")) self.tokenQueue.append({"type": "Characters", "data": data + chars}) self.lastFourChars += chars[-4:] self.lastFourChars = self.lastFourChars[-4:] return True John Nagle From kyosohma at gmail.com Sat Jul 4 23:17:07 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Sat, 4 Jul 2009 20:17:07 -0700 (PDT) Subject: Pyowa is this Monday! Message-ID: <7224400c-cadc-403f-b090-74cb49373dee@h11g2000yqb.googlegroups.com> Hi, The next Pyowa meeting is on Monday, July 6th from 7-9 p.m. We will be at the Marshall County Sheriff's Office (directions on website). Currently, the plan is to have a talk about Python at Fisher/Emerson that will segue into a general discussion of what we do with our favorite programming language. Be prepared to share! We may have another presentation in our Standard Library series as well or just something random. Let me know if you think you'll be there. Pop & water will be provided. We hope to see you there! -------------------------- Mike Driscoll http://www.pyowa.org http://pyowa.blip.tv/ http://twitter.com/pyowa http://upcoming.yahoo.com/event/2853430/ Mailing list: http://pyowalist.pythonlibrary.org/listinfo.cgi/pyowa-pythonlibrary.org From aahz at pythoncraft.com Sat Jul 4 23:37:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 Jul 2009 20:37:16 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: In article <4a501a5e$0$1640$742ec2ed at news.sonic.net>, John Nagle wrote: > > Here's some actual code, from "tokenizer.py". This is called once >for each character in an HTML document, when in "data" state (outside >a tag). It's straightforward code, but look at all those >dictionary lookups. > > def dataState(self): > data = self.stream.char() > > # Keep a charbuffer to handle the escapeFlag > if self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > if len(self.lastFourChars) == 4: > self.lastFourChars.pop(0) > self.lastFourChars.append(data) > > # The rest of the logic > if data == "&" and self.contentModelFlag in\ > (contentModelFlags["PCDATA"], contentModelFlags["RCDATA"]) and not\ > self.escapeFlag: > self.state = self.states["entityData"] > elif data == "-" and self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) and not\ > self.escapeFlag and "".join(self.lastFourChars) == "": > self.escapeFlag = False > self.tokenQueue.append({"type": "Characters", "data":data}) > elif data == EOF: > # Tokenization ends. > return False > elif data in spaceCharacters: > # Directly after emitting a token you switch back to the "data > # state". At that point spaceCharacters are important so they are > # emitted separately. > self.tokenQueue.append({"type": "SpaceCharacters", "data": > data + self.stream.charsUntil(spaceCharacters, True)}) > # No need to update lastFourChars here, since the first space will > # have already broken any sequences > else: > chars = self.stream.charsUntil(("&", "<", ">", "-")) > self.tokenQueue.append({"type": "Characters", "data": > data + chars}) > self.lastFourChars += chars[-4:] > self.lastFourChars = self.lastFourChars[-4:] > return True Every single "self." is a dictionary lookup. Were you referring to those? If not, I don't see your point. If yes, well, that's kind of the whole point of using Python. You do pay a performance penalty. You can optimize out some lookups, but you need to switch to C for some kinds of computationally intensive algorithms. In this case, you can probably get a large boost out of Pysco or Cython or Pyrex. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From tn.pablo at gmail.com Sun Jul 5 00:26:39 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sat, 4 Jul 2009 23:26:39 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Sat, Jul 4, 2009 at 18:01, Dennis Lee Bieber wrote: > ? ? ? ?Do you really want to inflict novices with the subtleties of when > "assert" is a proper structure to use? I'll second that. This very thread is proof that assertions are polemic, so maybe you (kj) should just save your student's sanity and stick to good old conditionals :-) As for your initial question, I think, thirty four emails after, that yes, your function is a bit too clever and you should sacrifice some generality in order to make it more readable. -- Pablo Torres N. From pavlovevidence at gmail.com Sun Jul 5 01:51:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 4 Jul 2009 22:51:52 -0700 (PDT) Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <4c2c7b44-23cd-43b4-8086-1b7cf6a14954@n11g2000yqb.googlegroups.com> On Jul 4, 4:35?pm, John Nagle wrote: > ? ? The temptation is to write tokenizers in C, but that's an admission > of language design failure. No it isn't. It's only a failure of Python to be the language that does everything *you* want. Carl Banks From http Sun Jul 5 02:17:21 2009 From: http (Paul Rubin) Date: 04 Jul 2009 23:17:21 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xfxdbk61a.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Certain people -- a tiny minority -- keep trying to argue > that the ability to say "if obj" for arbitrary objects is somehow a bad > thing, and their arguments seem to always boil down to: > "If you write code that assumes that only bools have a truth value, then > surprising things will happen because all objects have a truth value." I'd put it under the general rubric of "explicit is better than implicit". The language shouldn't do silly automatic typecasts all over the place. Yes, it saves a few keystrokes to say "if x:" instead of "if len(x)==0:" or even "if bool(x):", but if I program in a style where I like to think I know the type of something when I use it, I'd like the interpreter to let me know when I'm wrong instead of proceeding silently. From nick at craig-wood.com Sun Jul 5 03:30:04 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Sun, 05 Jul 2009 02:30:04 -0500 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > As an example of code that really needs to run fast, but is > speed-limited by Python's limitations, see "tokenizer.py" in > > http://code.google.com/p/html5lib/ > > This is a parser for HTML 5, a piece of code that will be needed > in many places and will process large amounts of data. It's written > entirely in Python. Take a look at how much work has to be performed > per character. > > This is a good test for Python implementation bottlenecks. Run > that tokenizer on HTML, and see where the time goes. > > ("It should be written in C" is not an acceptable answer.) You could compile it with Cython though. lxml took this route... -- Nick Craig-Wood -- http://www.craig-wood.com/nick From pm567426 at gmail.com Sun Jul 5 03:38:30 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 00:38:30 -0700 (PDT) Subject: How Python Implements "long integer"? Message-ID: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Hello, I'm reading about implementation of long ints in Python. I downloaded the source code of CPython and will read the longobject.c, but from where I should start reading this file? I mean which function is the first? Anyone can help? Thanks Pedram From mail at microcorp.co.za Sun Jul 5 04:12:54 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 5 Jul 2009 10:12:54 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <006201c9fd48$666d94e0$0d00a8c0@Hendrik> "John Nagle" wrote: > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. There's a comment in the code that it would be useful > to run a few billion lines of HTML through an instrumented version > of the parser to decide in which order the IF statements should be > executed. You shouldn't have to do that. You do not have to implement a state machine in a case statement, or in a series of if... elifs. Python is not C. Use a dispatch dict, and have each state return the next state. Then you can use strings representing state names, and everybody will be able to understand the code. toy example, not tested, nor completed: protocol = {"start":initialiser,"hunt":hunter,"classify":classifier,....other states} def state_machine(): next_step = protocol["start"]() while True: next_step = protocol[next_step]() Simple, and almost as fast as if you did the same thing in assembler using pointers. And each state will have a finite set of reasons to either stay where its at, or move on. Not a lot you can do about that, but test for them one at a time. But at least you will have split the problem up, and you won't be doing irrelevant tests. You can even do away with the dict, by having each state return the actual next state routine: next_state = protocol_initialiser() while True: next_state = next_state() time.sleep(0.001) # this prevents thrashing when monitoring real events If you are using a gui, and you have access to an after callback, then you can make a ticking stutter thread to run some monitoring machine in the background, using the same "tell me what to do next" technique. To take the timing thing further, you can do: wait_time, next_state = protocol_initialiser() while True: if wait_time: time.sleep(wait_time) wait_time, next_state = next_state() This gives you control over busy-wait loops, and lets you speed up when it is needed. Python really is not C. - Hendrik From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 04:28:01 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 08:28:01 GMT Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: <0260559c$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 20:15:11 -0700, John Nagle wrote: > Paul Rubin wrote: >> John Nagle writes: >>> A dictionary lookup (actually, several of them) for every >>> input character is rather expensive. Tokenizers usually index into a >>> table of character classes, then use the character class index in a >>> switch statement. >> >> Maybe you could use a regexp (and then have -two- problems...) to find >> the token boundaries, then a dict to identify the actual token. Tables >> of character classes seem a bit less attractive in the Unicode era than >> in the old days. > > I want to see a regular expression that expresses the HTML 5 token > parsing rules, including all the explicitly specified error handling. Obviously the regex can't do the error handling. Nor should you expect a single regex to parse an entire HTML document. But you could (perhaps) use regexes to parse pieces of the document, as needed. Have you investigated the pyparsing module? Unless you have some reason for avoiding it, for any complicated parsing job I'd turn to that before trying to roll your own. > Here's some actual code, from "tokenizer.py". This is called once > for each character in an HTML document, when in "data" state (outside a > tag). It's straightforward code, but look at all those dictionary > lookups. Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't see how a case statement would help you here: you're not dispatching on a value, but running through a series of tests until one passes. There are languages where you can write something like: case: x > 0: process_positive(x) x < 0: process_negative(x) x == 0: process_zero(x) but that's generally just syntactic sugar for the obvious if...elif... block. (Although clever compilers might recognise that it's the same x in each expression, and do something clever to optimize the code.) Nor do I see why you were complaining about Python not having true constants. I don't see how that would help you... most of your explicit dict lookups are against string literals e.g. contentModelFlags["RCDATA"]. So while I feel your pain, I'm not sure I understand why you're blaming this on *Python*. -- Steven From stefan_ml at behnel.de Sun Jul 5 04:41:58 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 10:41:58 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <4a506755$0$31333$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. Cython has a built-in optimisation that maps if-elif-else chains to C's switch statement if they only test a single int/char variable, even when you write things like "elif x in [1,5,9,12]". This works in Cython, because we know that the comparison to a C int/char is side-effect free. It may not always be side-effect free in Python, so this won't work in general. It would be perfect for your case, though. Stefan From http Sun Jul 5 04:58:13 2009 From: http (Paul Rubin) Date: 05 Jul 2009 01:58:13 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xws6nik0q.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't > see how a case statement would help you here: you're not dispatching on a > value, but running through a series of tests until one passes. A case statement switch(x):... into a bunch of constant case labels would be able to use x as an index into a jump vector, and/or do an unrolled logarithmic (bisection-like) search through the tests, instead of a linear search. From stefan_ml at behnel.de Sun Jul 5 04:58:27 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 10:58:27 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a501a5e$0$1640$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Here's some actual code, from "tokenizer.py". This is called once > for each character in an HTML document, when in "data" state (outside > a tag). It's straightforward code, but look at all those > dictionary lookups. > > def dataState(self): > data = self.stream.char() > > # Keep a charbuffer to handle the escapeFlag > if self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): Is the tuple (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) constant? If that is the case, I'd cut it out into a class member (or module-local variable) first thing in the morning. And I'd definitely keep the result of the "in" test in a local variable for reuse, seeing how many times it's used in the rest of the code. Writing inefficient code is not something to blame the language for. Stefan From stefan_ml at behnel.de Sun Jul 5 05:04:33 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 11:04:33 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <4a506ca1$0$31344$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Paul Rubin wrote: >> John Nagle writes: >>> Python doesn't have a "switch" or "case" statement, and when >>> you need a state machine with many states, that makes for painful, >>> slow code. ... >>> There's a comment in the code that it would be useful >>> to run a few billion lines of HTML through an instrumented version >>> of the parser to decide in which order the IF statements should be >>> executed. You shouldn't have to do that. >> >> In that particular program it would probably be better to change those >> if/elif/elif/else constructs to dictionary lookups. I see the program >> already does that for some large tables. > > A dictionary lookup (actually, several of them) for every > input character is rather expensive. Did you implement this and prove your claim in benchmarks? Taking a look at the current implementation, I'm pretty sure a dict-based implementation would outrun it in your first try. Stefan From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 05:04:58 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 09:04:58 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> Message-ID: <02605e46$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 23:17:21 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> Certain people -- a tiny minority -- keep trying to argue that the >> ability to say "if obj" for arbitrary objects is somehow a bad thing, >> and their arguments seem to always boil down to: "If you write code >> that assumes that only bools have a truth value, then surprising things >> will happen because all objects have a truth value." > > I'd put it under the general rubric of "explicit is better than > implicit". "if x" is explicit. It's difficult to see how a branch could be anything other than explicit, but never mind. > The language shouldn't do silly automatic typecasts all over > the place. "if x" doesn't involve a typecast. Python doesn't have typecasts, except possibly for the special case of myobject.__class__ = Another_Class. If you mean Python shouldn't do silly automatic type conversions all over the place, I absolutely agree with you! Fortunately, testing the truth value of an object isn't a silly automatic type conversion. > Yes, it saves a few keystrokes to say "if x:" instead of "if > len(x)==0:" or even "if bool(x):", It's not about saving keystrokes -- that's a furphy. It's about encapsulation. Objects are in a better position to recognise when they are "something" (true) or "nothing" (false) than you are. Given an arbitrary object x, how do you know if it's something or nothing? In general, you can't tell -- but the object can, provided it's well written. (The conspicuous exception is iterators, but that's a special case.) "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like iterable instead of a list, and calculating the actual length is O(N). Why spend all that effort to find out the length of the object is 59,872,819 only to throw that away? My object knows when it's empty, but instead you make foolish assumptions about the object and consequently write wastefully slow code. > but if I program in a style where I > like to think I know the type of something when I use it, I'd like the > interpreter to let me know when I'm wrong instead of proceeding > silently. Oh come on now... that's a silly objection. If you want strongly-typed variables in Python, say so, don't pretend this is a failure of truth- testing. If you write len(x)==0 Python doesn't complain if x is a dict instead of the list you were expecting. Why is it acceptable to duck-type len(x) but not truth-testing? -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 05:07:50 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 09:07:50 GMT Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <02605ef2$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 10:12:54 +0200, Hendrik van Rooyen wrote: > Python is not C. John Nagle is an old hand at Python. He's perfectly aware of this, and I'm sure he's not trying to program C in Python. I'm not entirely sure *what* he is doing, and hopefully he'll speak up and say, but whatever the problem is it's not going to be as simple as that. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 05:13:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 09:13:23 GMT Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> Message-ID: <0260603f$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 01:58:13 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't >> see how a case statement would help you here: you're not dispatching on >> a value, but running through a series of tests until one passes. > > A case statement switch(x):... into a bunch of constant case labels > would be able to use x as an index into a jump vector, and/or do an > unrolled logarithmic (bisection-like) search through the tests, instead > of a linear search. Yes, I'm aware of that, but that's not what John's code is doing -- he's doing a series of if expr ... elif expr tests. I don't think a case statement can do much to optimize that. -- Steven From stef.mientki at gmail.com Sun Jul 5 05:35:14 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 05 Jul 2009 11:35:14 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: References: <4A4BC22D.70502@gmail.com> Message-ID: <4A5073D2.4010000@gmail.com> Terry Reedy wrote: > Stef Mientki wrote: >> hello, >> >> I need to add an object's name to the global namespace. >> The reason for this is to create an environment, >> where you can add some kind of math environment, >> where no need for Python knowledge is needed. >> >> The next statement works, >> but I'm not sure if it will have any dramatical side effects, >> other than overruling a possible object with the name A >> >> def some_function ( ...) : >> A = object ( ...) >> sys._getframe(1).f_globals [ Name ] = A > > global name > name = A > > or is name is a string var > globals()[name] = A great, the last 2 lines works in most cases. Now to get everything working correctly, I have to use both globals() and f_globals() but everything seems to work perfect now. thanks for all the hints. cheers, Stef From http Sun Jul 5 05:38:54 2009 From: http (Paul Rubin) Date: 05 Jul 2009 02:38:54 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Yes, I'm aware of that, but that's not what John's code is doing -- he's > doing a series of if expr ... elif expr tests. I don't think a case > statement can do much to optimize that. The series of tests is written that way because there is no case statement available. It is essentially switching on a bunch of character constants and then doing some additional tests in each branch. It could be that using ord(c) as an index into a list of functions might be faster than a dict lookup on c to get a function. I think John is hoping to avoid a function call and instead get an indexed jump within the Python bytecode for the big function. From stefan_ml at behnel.de Sun Jul 5 05:41:18 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 11:41:18 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <4a50753e$0$31331$9b4e6d93@newsspool4.arcor-online.net> Stefan Behnel wrote: > John Nagle wrote: >> Here's some actual code, from "tokenizer.py". This is called once >> for each character in an HTML document, when in "data" state (outside >> a tag). It's straightforward code, but look at all those >> dictionary lookups. >> >> def dataState(self): >> data = self.stream.char() >> >> # Keep a charbuffer to handle the escapeFlag >> if self.contentModelFlag in\ >> (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > > Is the tuple > > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) > > constant? If that is the case, I'd cut it out into a class member (or > module-local variable) first thing in the morning. Ah, and there's also this little trick to make it a (fast) local variable in that method: def some_method(self, some_const=(1,2,3,4)): ... Stefan From stefan_ml at behnel.de Sun Jul 5 05:48:38 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 11:48:38 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xr5wvtqoh.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: <4a5076f8$0$31341$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Steven D'Aprano writes: >> Yes, I'm aware of that, but that's not what John's code is doing -- he's >> doing a series of if expr ... elif expr tests. I don't think a case >> statement can do much to optimize that. > > The series of tests is written that way because there is no case > statement available. It is essentially switching on a bunch of > character constants and then doing some additional tests in each > branch. Although doing some of the tests first and then checking the input conditionally might be faster here. Another idea: You could exchange the methods whenever self.contentModelFlag changes, i.e. you'd have a "dataState_CDATA", a "dataState_PCDATA" etc. > It could be that using ord(c) as an index into a list of functions > might be faster than a dict lookup on c to get a function. Rather unlikely, given that calling "ord(c)" involves a dict lookup for "ord". You might get away with the pre-initialised keywords trick, though. > I think > John is hoping to avoid a function call and instead get an indexed > jump within the Python bytecode for the big function. Hmm, yes, the actual code inside the conditionals is pretty short, so the call overhead might hurt here. Stefan From http Sun Jul 5 05:52:17 2009 From: http (Paul Rubin) Date: 05 Jul 2009 02:52:17 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7x7hyn5uem.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > > # Keep a charbuffer to handle the escapeFlag > > if self.contentModelFlag in\ > > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > Is the tuple > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) > constant? If that is the case, I'd cut it out into a class member ... I think the main issue for that function comes after that if statement. There is a multi-way switch on a bunch of different possible character values. I do agree with you that the first "if" can also be optimized. From ldo at geek-central.gen.new_zealand Sun Jul 5 05:52:22 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Jul 2009 21:52:22 +1200 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <025ff4f1$0$20657$c3e8da3@news.astraweb.com> Message-ID: In message <025ff4f1$0$20657$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > On Sun, 05 Jul 2009 12:12:22 +1200, Lawrence D'Oliveiro wrote: > >> In message <1beffd94-cfe6-4cf6-bd48-2ccac8637796 at j32g2000yqh.googlegroups.com>, ryles wrote: >> >>>>>> # Oh... yeah. I really *did* want 'is None' and not '== None' which >>>>>> # iter() will do. Sorry guys! >>> >>> Please don't let this happen to you too ;) >> >> Strange. others have got told off for using "== None" instead of "is >> None" , >> and yet it turns out Python itself does exactly the same thing. > > That's not "strange", that's a bug. It's not a bug, as Gabriel Genellina has pointed out. From http Sun Jul 5 05:54:23 2009 From: http (Paul Rubin) Date: 05 Jul 2009 02:54:23 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> <4a5076f8$0$31341$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7x3a9b5ub4.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > > The series of tests is written that way because there is no case > > statement available. It is essentially switching on a bunch of > > character constants and then doing some additional tests in each > > branch. > Although doing some of the tests first and then checking the input > conditionally might be faster here. That is essentially what happens. There are a bunch of tests of the form if data=='<' and [some other stuff]: ... Because of short-circuit evaluation of "and", the additional tests only happen once the character has been matched. From ldo at geek-central.gen.new_zealand Sun Jul 5 05:54:37 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Jul 2009 21:54:37 +1200 Subject: Is code duplication allowed in this instance? References: Message-ID: In message , Klone wrote: > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). I think you should be put on the management fast-track. From dickinsm at gmail.com Sun Jul 5 05:57:02 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sun, 5 Jul 2009 02:57:02 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: On Jul 5, 8:38?am, Pedram wrote: > Hello, > I'm reading about implementation of long ints in Python. I downloaded > the source code of CPython and will read the longobject.c, but from > where I should start reading this file? I mean which function is the > first? I don't really understand the question: what do you mean by 'first'? It might help if you tell us what your aims are. In any case, you probably also want to look at the Include/ longintrepr.h and Include/longobject.h files. Mark From stefan_ml at behnel.de Sun Jul 5 06:04:43 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 12:04:43 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7x3a9b5ub4.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> <4a5076f8$0$31341$9b4e6d93@newsspool4.arcor-online.net> <7x3a9b5ub4.fsf@ruckus.brouhaha.com> Message-ID: <4a507abb$0$31327$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Stefan Behnel writes: >>> The series of tests is written that way because there is no case >>> statement available. It is essentially switching on a bunch of >>> character constants and then doing some additional tests in each >>> branch. >> Although doing some of the tests first and then checking the input >> conditionally might be faster here. > > That is essentially what happens. There are a bunch of tests of the > form > if data=='<' and [some other stuff]: ... That's what I meant. Some of the "other stuff" is redundant enough to do it once at the beginning of the function (or even before entering the function, by writing specialised methods), i.e. I'd (partially) reverse the order of the "and" operands. Stefan From stefan_ml at behnel.de Sun Jul 5 06:06:49 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 12:06:49 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7x7hyn5uem.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> <7x7hyn5uem.fsf@ruckus.brouhaha.com> Message-ID: <4a507b39$0$31327$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Stefan Behnel writes: >>> # Keep a charbuffer to handle the escapeFlag >>> if self.contentModelFlag in\ >>> (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): >> Is the tuple >> (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) >> constant? If that is the case, I'd cut it out into a class member ... > > I think the main issue for that function comes after that if statement. > There is a multi-way switch on a bunch of different possible character > values. I do agree with you that the first "if" can also be optimized. You may notice that the creation of this exact tuple appears in almost all if the conditionals of this method. So it is part of the bottleneck. Stefan From http Sun Jul 5 06:08:16 2009 From: http (Paul Rubin) Date: 05 Jul 2009 03:08:16 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xy6r34f3j.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > > Yes, it saves a few keystrokes to say "if x:" instead of "if > > len(x)==0:" or even "if bool(x):", > > It's not about saving keystrokes -- that's a furphy. It's about > encapsulation. Objects are in a better position to recognise when they > are "something" (true) or "nothing" (false) than you are. I don't know what a furphy is, but I don't accept that "somethingness" vs. "nothingness" is the same distinction as truth vs falsehood. True and False are values in a specific datatype (namely bool), not abstract qualities of arbitrary data structures. The idea that the "if" statement selects between "somethingness" and "nothingness" rather than between True and False is a bogus re-imagining of the traditional function of an "if" statement and has been an endless source of bugs in Python code. Look how much confusion it causes here in the newsgroup all the time. > "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like > iterable instead of a list, and calculating the actual length is O(N). That doesn't happen in any of Python's built-in container types. I could see some value to having a generic "is_empty" predicate on containers though, to deal with this situation. Your iterable could support that predicate. In fact maybe all iterables should support that predicate. They don't (and can't) all support "len". > If you write len(x)==0 Python doesn't complain if x is a dict > instead of the list you were expecting. Why is it acceptable to > duck-type len(x) but not truth-testing? I haven't seen the amount of bugs coming from generic "len" as from something-vs-nothing confusion. From http Sun Jul 5 07:09:26 2009 From: http (Paul Rubin) Date: 05 Jul 2009 04:09:26 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> <7x7hyn5uem.fsf@ruckus.brouhaha.com> <4a507b39$0$31327$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7xljn3bd3t.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > You may notice that the creation of this exact tuple appears in almost all > if the conditionals of this method. So it is part of the bottleneck. I don't think so. The tuple is only created when the character has already matched, and for the vast majority of the chars in the input stream (ordinary text chars rather than html delimiters) none of them match. From stefan_ml at behnel.de Sun Jul 5 07:23:23 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 13:23:23 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xljn3bd3t.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> <7x7hyn5uem.fsf@ruckus.brouhaha.com> <4a507b39$0$31327$9b4e6d93@newsspool4.arcor-online.net> <7xljn3bd3t.fsf@ruckus.brouhaha.com> Message-ID: <4a508d2c$0$31332$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Stefan Behnel writes: >> You may notice that the creation of this exact tuple appears in almost all >> if the conditionals of this method. So it is part of the bottleneck. > > I don't think so. The tuple is only created when the character has > already matched, and for the vast majority of the chars in the input > stream (ordinary text chars rather than html delimiters) none of them > match. Well, it's the second thing that happens when entering the method, it happens several times later on when specific characters are matched, and it also happens at the end when none of the special characters did match. So it /is/ part of the bottleneck because the dict lookups, the tuple creation, the "in" test and the tuple deallocation happen *twice* for almost all characters in the stream. Stefan From 13139781969 at mymetropcs.com Sun Jul 5 07:36:00 2009 From: 13139781969 at mymetropcs.com (13139781969 at mymetropcs.com) Date: Sun, 5 Jul 2009 06:36:00 -0500 Subject: No subject Message-ID: <1295065.145352441246808164305.JavaMail.mms@mms12.mms.metropcs.net> An HTML attachment was scrubbed... URL: -------------- next part -------------- (Homer & Marge having sex with Bart Simpson) From lie.1296 at gmail.com Sun Jul 5 07:37:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 05 Jul 2009 11:37:49 GMT Subject: question of style In-Reply-To: <7xy6r34f3j.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Steven D'Aprano writes: >> "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like >> iterable instead of a list, and calculating the actual length is O(N). > > That doesn't happen in any of Python's built-in container types. I > could see some value to having a generic "is_empty" predicate on > containers though, to deal with this situation. Your iterable could > support that predicate. In fact maybe all iterables should support > that predicate. They don't (and can't) all support "len". That doesn't happen because all python's built-in container keep track of its own length and are able to quickly determine its own length. But outside builtins, certain iterables cannot determine its own length quickly, e.g. iterators, but may have alternative ways to determine whether itself is empty (through a "private" attributes). If you peek through this private attribute, you're breaking encapsulation. Although python is a language of consenting adults and doesn't really have a real private, codes that breaks encapsulation is prone to bugs. >>> Yes, it saves a few keystrokes to say "if x:" instead of "if >>> len(x)==0:" or even "if bool(x):", >> It's not about saving keystrokes -- that's a furphy. It's about >> encapsulation. Objects are in a better position to recognise when >> they are "something" (true) or "nothing" (false) than you are. > > I don't know what a furphy is, but I don't accept that "somethingness" > vs. "nothingness" is the same distinction as truth vs falsehood. True > and False are values in a specific datatype (namely bool), not > abstract qualities of arbitrary data structures. The idea that the > "if" statement selects between "somethingness" and "nothingness" > rather than between True and False is a bogus re-imagining of the > traditional function of an "if" statement and has been an endless > source of bugs in Python code. Look how much confusion it causes here > in the newsgroup all the time. Neither python's `if` nor `if` in formal logic is about testing True vs. False. `if` in python and formal logic receives a statement. The statement must be evaluatable to True or False, but does not have to be True or False themselves. It just happens that True evaluates to True and False evaluates to False. For example, the statement: P := `e = 2.7182818284590451` Q := `e = m*c**2` ---------------------------------- P -> Q P -> Q evaluates to: `e = 2.7182818284590451` -> `e = m*c**2` Note that `e = 2.7182818284590451` is a statement, not a boolean value. The truth value of `e = 2.7182818284590451` is determined by "calling" (note the double quotes) `e = 2.7182818284590451`.statement_is_true(), which when written in python syntax becomes: (e == 2.7182818284590451).__bool__() >> If you write len(x)==0 Python doesn't complain if x is a dict >> instead of the list you were expecting. Why is it acceptable to >> duck-type len(x) but not truth-testing? > > I haven't seen the amount of bugs coming from generic "len" as from > something-vs-nothing confusion. From ptmcg at austin.rr.com Sun Jul 5 07:41:27 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sun, 5 Jul 2009 04:41:27 -0700 (PDT) Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <7061fac4-954d-49e2-a03b-3ed8653a99f3@o6g2000yqj.googlegroups.com> On Jul 5, 3:12 am, "Hendrik van Rooyen" wrote: > > Use a dispatch dict, and have each state return the next state. > Then you can use strings representing state names, and > everybody will be able to understand the code. > > toy example, not tested, nor completed: > > protocol = {"start":initialiser,"hunt":hunter,"classify":classifier,....other > states} > > def state_machine(): > next_step = protocol["start"]() > while True: > next_step = protocol[next_step]() > I've just spent about an hour looking over this code, with a few comments to inject to the thread here: - To all those suggesting the OP convert to a dispatch table, be assured that this code is well aware of this idiom. It is used HEAVILY at a macro level, picking through the various HTML states (starting a tag, reading attributes, reading body, etc.). There still are a number of cascading if-elif's within some of these states, and some of them *may* be candidates for further optimization. - There is an underlying HTMLInputStream that seems to be doing some unnecessary position bookkeeping (positionLine and positionCol). Commenting this out increases my test speed by about 13%. In my ignorance, I may be removing some important behavior, but this does not seem to be critical as I tested against a few megs of HTML source. Before blaming the tokenizer for everything, there may be more performance to be wrung from the input stream processor. For that matter, I would guess that about 90% of all HTML files that this code would process would easily fit in memory - in that case, the stream processing (and all of the attendant "if I'm not at the end of the current chunk" code) could be skipped/removed entirely. - The HTMLInputStream's charsUntil code is an already-identified bottleneck, and some re enhancements have been applied here to help out. - Run-time construction of tuple literals where the tuple members are constants can be lifted out. emitCurrentToken rebuilds this tuple every time it is called (which is a lot!): if (token["type"] in (tokenTypes["StartTag"], tokenTypes ["EndTag"], tokenTypes["EmptyTag"])): Move this tuple literal into a class constant (or if you can tolerate it, a default method argument to gain LOAD_FAST benefits - sometimes optimization isn't pretty). - These kinds of optimizations are pretty small, and only make sense if they are called frequently. Tallying which states are called in my test gives the following list in decreasing frequency. Such a list would help guide your further tuning efforts: tagNameState 194848 dataState 182179 attributeNameState 116507 attributeValueDoubleQuotedState 114931 tagOpenState 105556 beforeAttributeNameState 58612 beforeAttributeValueState 58216 afterAttributeValueState 58083 closeTagOpenState 50547 entityDataState 1673 attributeValueSingleQuotedState 1098 commentEndDashState 372 markupDeclarationOpenState 370 commentEndState 364 commentStartState 362 commentState 362 selfClosingStartTagState 359 doctypePublicIdentifierDoubleQuotedState 291 doctypeSystemIdentifierDoubleQuotedState 247 attributeValueUnQuotedState 191 doctypeNameState 32 beforeDoctypePublicIdentifierState 16 afterDoctypePublicIdentifierState 14 afterDoctypeNameState 9 doctypeState 8 beforeDoctypeNameState 8 afterDoctypeSystemIdentifierState 6 afterAttributeNameState 5 commentStartDashState 2 bogusCommentState 2 For instance, I wouldn't bother doing much tuning of the bogusCommentState. Anything called fewer than 50,000 times in this test doesn't look like it would be worth the trouble. -- Paul (Thanks to those who suggested pyparsing as an alternative, but I think this code is already beyond pyparsing in a few respects. For one thing, this code works with an input stream, in order to process large HTML files; pyparsing *only* works with an in-memory string. This code can also take advantage of some performance short cuts, knowing that it is parsing HTML; pyparsing's generic classes can't do that.) From stefan_ml at behnel.de Sun Jul 5 07:52:07 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 13:52:07 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a501a5e$0$1640$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: <4a5093e7$0$31338$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Here's some actual code, from "tokenizer.py". This is called once > for each character in an HTML document, when in "data" state (outside > a tag). It's straightforward code, but look at all those > dictionary lookups. > > def dataState(self): > data = self.stream.char() > > # Keep a charbuffer to handle the escapeFlag > if self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > if len(self.lastFourChars) == 4: > self.lastFourChars.pop(0) > self.lastFourChars.append(data) > > # The rest of the logic > if data == "&" and self.contentModelFlag in\ > (contentModelFlags["PCDATA"], contentModelFlags["RCDATA"]) and > not\ > self.escapeFlag: > self.state = self.states["entityData"] > elif data == "-" and self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) and > not\ > self.escapeFlag and "".join(self.lastFourChars) == "": > self.escapeFlag = False > self.tokenQueue.append({"type": "Characters", "data":data}) > elif data == EOF: > # Tokenization ends. > return False > elif data in spaceCharacters: > # Directly after emitting a token you switch back to the "data > # state". At that point spaceCharacters are important so > they are > # emitted separately. > self.tokenQueue.append({"type": "SpaceCharacters", "data": > data + self.stream.charsUntil(spaceCharacters, True)}) > # No need to update lastFourChars here, since the first > space will > # have already broken any sequences > else: > chars = self.stream.charsUntil(("&", "<", ">", "-")) > self.tokenQueue.append({"type": "Characters", "data": > data + chars}) > self.lastFourChars += chars[-4:] > self.lastFourChars = self.lastFourChars[-4:] > return True Giving this some more thought, I'd also try is to split the huge if-elif-else block like this: if data in string_with_all_special_characters: if data == '&' ...: ... else: ... So there are three things to improve: - eliminate common subexpressions which you know are constant - split the large conditional sequence as shown above - use separate dataState() methods when inside and outside of CDATA/RCDATA blocks and (maybe) escaped blocks Stefan From pm567426 at gmail.com Sun Jul 5 08:09:49 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 05:09:49 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> On Jul 5, 1:57?pm, Mark Dickinson wrote: > On Jul 5, 8:38?am, Pedram wrote: > > > Hello, > > I'm reading about implementation of long ints in Python. I downloaded > > the source code of CPython and will read the longobject.c, but from > > where I should start reading this file? I mean which function is the > > first? > > I don't really understand the question: ?what do you mean by 'first'? > It might help if you tell us what your aims are. > > In any case, you probably also want to look at the Include/ > longintrepr.h and Include/longobject.h files. > > Mark Thanks for reply, Sorry I can't explain too clear! I'm not English ;) But I want to understand the implementation of long int object in Python. How Python allocates memory and how it implements operations for this object? Although, I'm reading the source code (longobject.c and as you said, longintrepr.h and longobject.h) but if you can help me, I really appreciate that. Pedram From drobinow at gmail.com Sun Jul 5 08:21:33 2009 From: drobinow at gmail.com (David Robinow) Date: Sun, 5 Jul 2009 08:21:33 -0400 Subject: Is code duplication allowed in this instance? In-Reply-To: References: Message-ID: <4eb0089f0907050521n8d0f6eo271ec18c10040290@mail.gmail.com> On Sun, Jul 5, 2009 at 5:54 AM, Lawrence D'Oliveiro wrote: > In message c1d1c62d69fb at y17g2000yqn.googlegroups.com>, Klone wrote: > >> So in this scenario is it OK to duplicate the algorithm to be tested >> within the test codes or refactor the method such that it can be used >> within test codes to verify itself(??). > > I think you should be put on the management fast-track. Heavens, no. He's too valuable as a managee. From mjohnson at cfi.co.ug Sun Jul 5 08:27:12 2009 From: mjohnson at cfi.co.ug (Johnson Mpeirwe) Date: Sun, 5 Jul 2009 16:27:12 +0400 Subject: PSP Caching In-Reply-To: <3e1c0979-fb6b-4d6e-80e5-a4af8d5fd96a@a36g2000yqc.googlegroups.com> References: <3e1c0979-fb6b-4d6e-80e5-a4af8d5fd96a@a36g2000yqc.googlegroups.com> Message-ID: <20090705122217.M97570@cfi.co.ug> Thanks Simon, I got around this behavior by adding "MaxRequestsPerChild 1" (default value of this is 0) to my httpd.conf to limit the number of requests a child server process will handle before it dies but I think it is important to keep it 0 in production environment. Regards, Johnson On Fri, 3 Jul 2009 10:44:52 -0700 (PDT), Simon Forman wrote > On Jul 3, 5:18?am, Johnson Mpeirwe wrote: > > Hello All, > > > > How do I stop caching of Python Server Pages (or whatever causes changes > > in a page not to be noticed in a web browser)? I am new to developing > > web applications in Python and after looking at implementations of PSP > > like Spyce (which I believed introduces new unnecessary non-PSP syntax), > > I decided to write my own PSP applications from scratch. When I modify a > > file, I keep getting the old results until I intentionally introduce an > > error (e.g parse error) and correct it after to have the changes > > noticed. There's no proxy (I am working on a windows machine unplugged > > from the network). I have Googled and no documents seem to talk about > > this. Is there any particular mod_python directive I must set in my > > Apache configuration to fix this? > > > > Any help will be highly appreciated. > > > > Johnson > > I don't know much about caching with apache, but the answer mght be > on this page: http://httpd.apache.org/docs/2.2/caching.html > > Meanwhile, couldn't you just send apache a restart signal when you > modify your code? > > HTH, > ~Simon > -- > http://mail.python.org/mailman/listinfo/python-list From dickinsm at gmail.com Sun Jul 5 09:04:11 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sun, 5 Jul 2009 06:04:11 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> Message-ID: <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> On Jul 5, 1:09?pm, Pedram wrote: > Thanks for reply, > Sorry I can't explain too clear! I'm not English ;) That's shocking. Everyone should be English. :-) > But I want to understand the implementation of long int object in > Python. How Python allocates memory and how it implements operations > for this object? I'd pick one operation (e.g., addition), and trace through the relevant functions in longobject.c. Look at the long_as_number table to see where to get started. In the case of addition, that table shows that the nb_add slot is given by long_add. long_add does any necessary type conversions (CONVERT_BINOP) and then calls either x_sub or x_add to do the real work. x_add calls _PyLong_New to allocate space for a new PyLongObject, then does the usual digit-by-digit-with-carry addition. Finally, it normalizes the result (removes any unnecessary zeros) and returns. As far as memory allocation goes: almost all operations call _PyLong_New at some point. (Except in py3k, where it's a bit more complicated because small integers are cached.) If you have more specific questions I'll have a go at answering them. Mark From http Sun Jul 5 09:12:25 2009 From: http (Paul Rubin) Date: 05 Jul 2009 06:12:25 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> <025eaa4f$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7x1vovxohy.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > > I wouldn't say Python's None is terrible,... > No, wait, I tell I lie... re.search() sometimes bites me, because > sometimes it returns None and sometimes it returns a matchobject and I > don't use re often enough to have good habits with it yet. re is a common source of this type of bug but there are others. > There are three natural approaches to (say) re.search() for dealing with > failure: > > (1) return a sentinel value like None; > (2) return a matchobject which tests False; > (3) raise an exception. 4. Have re.search return a bool and possible matchobject separately: put_match_here = [] if re.search(pat, s, target=put_match_here): do_something_with(put_match_here[0]) or alternatively (cleaner), have a new type of object which supports search operations while self-updating with the match object: mt = re.match_target() ... if mt.search(pat, s): do_something_with(mt.match) if mt.search(pat2, s): do_another_thing_with(mt.match) ... This is sort of inspired by what Perl does. I often do something like this because it makes it cleaner to chain a series of matches. From mail at microcorp.co.za Sun Jul 5 09:45:45 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 5 Jul 2009 15:45:45 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> Message-ID: <00f201c9fd76$e61565a0$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >On Sun, 05 Jul 2009 10:12:54 +0200, Hendrik van Rooyen wrote: > >> Python is not C. > >John Nagle is an old hand at Python. He's perfectly aware of this, and >I'm sure he's not trying to program C in Python. > >I'm not entirely sure *what* he is doing, and hopefully he'll speak up >and say, but whatever the problem is it's not going to be as simple as >that. I am well aware that John is not a newbie. He was complaining about Python's lack of a case statement in the context of a state machine. The point I was trying to make is that, if any state machine is examined, then, if you examine any one state, the reasons for leaving it ("state transitions") is always a subset of the choices that _can_ be made. So that drawing a circle round each state in a state diagram, and making a routine to examine the arrows leaving that circle, and returning the destination point of the chosen arrow, is a way of splitting the job up, and results in making only the relevant decisions at the time of their relevance. This is in contrast to the classic C way of making one big case statement to implement a finite state machine, which gets its efficiency (if any) out of compiler optimisations such as replacing a skip chain with a jump table. I understand that it leads to a lot of what looks like boilerplate code, but he was looking for speed... - Hendrik From mail at microcorp.co.za Sun Jul 5 10:03:05 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 5 Jul 2009 16:03:05 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net><7xvdm8thpo.fsf@ruckus.brouhaha.com><4a4fe6cb$0$1591$742ec2ed@news.sonic.net><7xfxdcqabg.fsf@ruckus.brouhaha.com><4a501a5e$0$1640$742ec2ed@news.sonic.net><0260559c$0$20657$c3e8da3@news.astraweb.com><7xws6nik0q.fsf@ruckus.brouhaha.com><0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: <00f901c9fd79$51d12160$0d00a8c0@Hendrik> "Paul Rubin" wrote: > The series of tests is written that way because there is no case > statement available. It is essentially switching on a bunch of > character constants and then doing some additional tests in each > branch. > > It could be that using ord(c) as an index into a list of functions > might be faster than a dict lookup on c to get a function. I think > John is hoping to avoid a function call and instead get an indexed > jump within the Python bytecode for the big function. I agree about the ord(c). However, avoiding the function call is, I think, right now, in the realms of wishful thinking. I cannot see how you could avoid a python function call - even if he bites the bullet and implements my laborious scheme, he would still have to fetch the next character to test against, inside the current state. So if it is the function calls that is slowing him down, I cannot imagine a solution using less than one per character, in which case he is screwed no matter what he does. But wait - maybe if he passes an iterator around - the equivalent of for char in input_stream... Still no good though, unless the next call to the iterator is faster than an ordinary python call. - Hendrik From pm567426 at gmail.com Sun Jul 5 10:15:11 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 07:15:11 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> Message-ID: On Jul 5, 5:04?pm, Mark Dickinson wrote: > That's shocking. ?Everyone should be English. :-) Yes, I'm trying :) > I'd pick one operation (e.g., addition), and trace through the > relevant functions in longobject.c. ?Look at the long_as_number > table to see where to get started. > > In the case of addition, that table shows that the nb_add slot is > given by long_add. ?long_add does any necessary type conversions > (CONVERT_BINOP) and then calls either x_sub or x_add to do the real > work. > x_add calls _PyLong_New to allocate space for a new PyLongObject, then > does the usual digit-by-digit-with-carry addition. ?Finally, it > normalizes > the result (removes any unnecessary zeros) and returns. > > As far as memory allocation goes: almost all operations call > _PyLong_New at some point. ?(Except in py3k, where it's a bit more > complicated because small integers are cached.) Oh, I didn't see long_as_number before. I'm reading it. That was very helpful, thanks. > If you have more specific questions I'll have a go at answering them. > > Mark Thank you a million. I will write your name in my "Specially thanks to" section of my article (In font size 72) ;) Pedram From l.mastrodomenico at gmail.com Sun Jul 5 10:25:31 2009 From: l.mastrodomenico at gmail.com (Lino Mastrodomenico) Date: Sun, 5 Jul 2009 16:25:31 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <00f901c9fd79$51d12160$0d00a8c0@Hendrik> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> <00f901c9fd79$51d12160$0d00a8c0@Hendrik> Message-ID: 2009/7/5 Hendrik van Rooyen : > I cannot see how you could avoid a python function call - even if he > bites the bullet and implements my laborious scheme, he would still > have to fetch the next character to test against, inside the current state. > > So if it is the function calls that is slowing him down, I cannot > imagine a solution using less than one per character, in which > case he is screwed no matter what he does. A simple solution may be to read the whole input HTML file in a string. This potentially requires lots of memory but I suspect that the use case by far most common for this parser is to build a DOM (or DOM-like) tree of the whole document. This tree usually requires much more memory that the HTML source itself. So, if the code duplication is acceptable, I suggest keeping this implementation for cases where the input is extremely big *AND* the whole program will work on it in "streaming", not just the parser itself. Then write a simpler and faster parser for the more common case when the data is not huge *OR* the user will keep the whole document in memory anyway (e.g. on a tree). Also: profile, profile a lot. HTML pages are very strange beasts and the bottlenecks may be in innocent-looking places! -- Lino Mastrodomenico From tn.pablo at gmail.com Sun Jul 5 10:30:47 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sun, 5 Jul 2009 09:30:47 -0500 Subject: How Python Implements "long integer"? In-Reply-To: References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: On Sun, Jul 5, 2009 at 04:57, Mark Dickinson wrote: > On Jul 5, 8:38?am, Pedram wrote: >> Hello, >> I'm reading about implementation of long ints in Python. I downloaded >> the source code of CPython and will read the longobject.c, but from >> where I should start reading this file? I mean which function is the >> first? > > I don't really understand the question: ?what do you mean by 'first'? > It might help if you tell us what your aims are. I think he means the entry point, problem is that libraries have many. -- Pablo Torres N. From asm198 at gmail.com Sun Jul 5 10:56:26 2009 From: asm198 at gmail.com (Icarus) Date: Sun, 5 Jul 2009 07:56:26 -0700 (PDT) Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: <39985616-d6b4-42f5-9db6-e7827f97c21d@24g2000yqm.googlegroups.com> On Jul 4, 11:24?am, Peter Otten <__pete... at web.de> wrote: > Icarus wrote: > > On Jul 4, 3:21 am, Peter Otten <__pete... at web.de> wrote: > >> Icarus wrote: > >> > I'm working on a serial protocol analyzer in python. ?We have an > >> > application written by someone else in MFC but we need something that > >> > is cross platform. ?I intended to implement the GUI portion in Tkinter > >> > but am having trouble. > > >> > The idea is that I will read messages from the serial port and output > >> > them to a Tkinter Text object initially. ?Eventually it will have > >> > other functionality but that's it for the short term. ?I've written > >> > this little test app to experiment with putting things on the GUI via > >> > a Queue which is polled by the Tkinter loop. > > >> > On some machines this code works fine and I get whatever I type in > >> > displayed in the Text widget. ?On others I get errors like this as > >> > soon as I start it running. > > >> > error in background error handler: > >> > out of stack space (infinite loop?) > >> > while executing > >> > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > >> > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > >> > while execu..." > > >> > I don't understand why on some machines it works exactly as expected > >> > and on others it acts the same way Tkinter does when I call functions > >> > directly from outside the Tkinter thread. ?Does anyone have any > >> > suggestions? ?The full code as appended below. ?Thanks in advance. > > >> > [code] > > >> > import Queue > > >> > class functionQueue: > > >> > def __init__(self, root = None, timeout = 250): > > >> > self.functionQueue = Queue.Queue() > >> > self.root = root > >> > self.timeout = timeout > > >> > if(self.root): > >> > self.pop_function(root) > > >> > def pop_function(self, root = None): > > >> > try: > >> > funcArgList = self.functionQueue.get(block = False) > >> > except Queue.Empty: > >> > pass > >> > else: > >> > try: > >> > funcArgList[0](*funcArgList[1]) > >> > except: > >> > try: > >> > print "Failed to call function", funcArgList[0] > >> > except: > >> > print "Failed to call function" > > >> > if(root): > >> > root.after(self.timeout, lambda: self.pop_function > >> > (self.root)) > > >> > def add_function(self, function, argList): > > >> > try: > >> > self.functionQueue.put([function, argList]) > >> > except: > >> > pass > > >> > if( __name__ == '__main__'): > > >> > import Tkinter > >> > import thread > > >> > text = Tkinter.Text() > >> > text.pack() > > >> > myQueue = functionQueue(text, 50) > > >> > def gui_loop(): > >> > try: > >> > text.mainloop() > >> > except: > >> > import os > >> > os._exit(1) > > >> > thread.start_new_thread(text.mainloop, ()) > > >> > while(True): > >> > usrInput = raw_input() > > >> > if(usrInput == "-1"): > >> > import os > >> > os._exit(0) > > >> > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > >> > myQueue.add_function(text.see, ['end']) > > >> > [/code] > > >> I can make it work over here by putting the UI into the main thread, as > >> suggested byhttp://effbot.org/zone/tkinter-threads.htm: > > >> import Queue > >> import Tkinter > >> import threading > > >> class FunctionQueue: > >> # unchanged > > >> def input_loop(): > >> while True: > >> try: > >> usrInput = raw_input() > >> except EOFError: > >> break > >> myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > >> myQueue.add_function(text.see, ['end']) > >> myQueue.add_function(text.quit, []) > > >> if __name__ == '__main__': > >> text = Tkinter.Text() > >> text.pack() > > >> myQueue = FunctionQueue(text, 50) > >> threading.Thread(target=input_loop).start() > >> text.mainloop() > > >> Peter > > > Peter, thanks for the suggestion. ?I tried your code exactly on my box > > and I still get the same results. ?As soon as I run the script and > > every time I click on the Text box I get tcl::Bgerror ... just like I > > mentioned above. ?I'm fairly certain that I'm not calling Tkinter > > functions from any other thread but it's acting as though I am as soon > > as I create the input thread. > > If I comment out the input loop thread everything is fine but of > > course that's not terribly useful as a logging box. > > http://bugs.python.org/issue3835 > > Could tcl have been built without thread support on the failing machines? > > Peter You had it Peter. I tried the "import pydoc pydoc.gui()" in the bug report you referenced and the same thing described there occurred. After recompiling tcl/tk with --threads-enabled and replacing the slackware default packages with those everything is working as I expected. Thanks for the help. From aahz at pythoncraft.com Sun Jul 5 11:08:23 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Jul 2009 08:08:23 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: In article , Hendrik van Rooyen wrote: > >But wait - maybe if he passes an iterator around - the equivalent of >for char in input_stream... Still no good though, unless the next call >to the iterator is faster than an ordinary python call. Calls to iterators created by generators are indeed faster than an ordinary Python call, because the stack frame is already mostly set up. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From nomail at thank.you Sun Jul 5 11:26:21 2009 From: nomail at thank.you (jack catcher (nick)) Date: Sun, 05 Jul 2009 18:26:21 +0300 Subject: Python and webcam capture delay? Message-ID: Hi, I'm thinking of using Python for capturing and showing live webcam stream simultaneously between two computers via local area network. Operating system is Windows. I'm going to begin with VideoCapture extension, no ideas about other implementation yet. Do you have any suggestions on how short delay I should hope to achieve in showing the video? This would be part of a psychological experiment, so I would need to deliver the video stream with a reasonable delay (say, below 100ms). From serverin2000 at yahoo.com Sun Jul 5 11:56:12 2009 From: serverin2000 at yahoo.com (RAM) Date: Sun, 5 Jul 2009 08:56:12 -0700 (PDT) Subject: generation of keyboard events Message-ID: Hi, I need to start an external program and pass the keyboard events like F1,Right arrow key etc to the program..I am trying to use the subprocess module to invoke the external program. I am able to invoke but not able to generate the keyboard events and pass them on to the external progam. Please help me in this because I am a beginner. regards Sreerama V From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 12:00:03 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 16:00:03 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: <0260bf8d$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 03:08:16 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> > Yes, it saves a few keystrokes to say "if x:" instead of "if >> > len(x)==0:" or even "if bool(x):", >> >> It's not about saving keystrokes -- that's a furphy. It's about >> encapsulation. Objects are in a better position to recognise when they >> are "something" (true) or "nothing" (false) than you are. > > I don't know what a furphy is, Is your Google broken? *wink* http://en.wikipedia.org/wiki/Furphy > but I don't accept that "somethingness" > vs. "nothingness" is the same distinction as truth vs falsehood. It's the distinction used by Python since the dawn of time. Python only grew a bool type a few versions back. > True > and False are values in a specific datatype (namely bool), not abstract > qualities of arbitrary data structures. I'm not talking about the constants True and False (nouns), but about true and false values (adjectives). > The idea that the "if" > statement selects between "somethingness" and "nothingness" rather than > between True and False is a bogus re-imagining of the traditional > function of an "if" statement There's nothing bogus about it. Some languages such as Pascal and Java require a special Boolean type for if-branches. Other languages, like Forth, C, Lisp and Ruby do not. http://en.wikipedia.org/wiki/Boolean_data_type > and has been an endless source of bugs in Python code. I wonder why these "endless" bugs weren't important enough to be mentioned in the rationale to PEP 285: http://www.python.org/dev/peps/pep-0285/ You'd think something as vital as "if x Considered Harmful" would have made it into the PEP, but no. Instead Guido *explicitly* stated that he had no intention of forcing `if` to require a bool, describing `if x` as the "correct form" and calling scrapping such a feature as "crippling the language". > Look how much confusion it causes here in the newsgroup all the time. The only confusion is that you're causing me. Would you care to link to some? >> "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like >> iterable instead of a list, and calculating the actual length is O(N). > > That doesn't happen in any of Python's built-in container types. And if they were the only types possible in Python, that might be relevant. > I > could see some value to having a generic "is_empty" predicate on > containers though, to deal with this situation. We have that already. It's spelled __bool__ or __nonzero__, and it applies to any object, not just containers. > Your iterable could > support that predicate. In fact maybe all iterables should support that > predicate. They don't (and can't) all support "len". Iterators are a special case, because in general they can't tell if they're exhausted until they try to yield a value. >> If you write len(x)==0 Python doesn't complain if x is a dict instead >> of the list you were expecting. Why is it acceptable to duck-type >> len(x) but not truth-testing? > > I haven't seen the amount of bugs coming from generic "len" as from > something-vs-nothing confusion. Again with these alleged bugs. -- Steven From pm567426 at gmail.com Sun Jul 5 12:01:30 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 09:01:30 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> Hello again, This time I have a simple C question! As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I found PyObject_NEW_VAR in objimpl.h header file. But I can't understand the last line :( Here's the code: #define PyObject_NEW_VAR(type, typeobj, n) \ ( (type *) PyObject_InitVar( \ (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), (n)) ),\ (typeobj), (n)) ) I know this will replace the PyObject_New_VAR(type, typeobj, n) everywhere in the code and but I can't understand the last line, which is just 'typeobj' and 'n'! What do they do? Are they make any sense in allocation process? From aahz at pythoncraft.com Sun Jul 5 12:12:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Jul 2009 09:12:19 -0700 Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> Message-ID: In article <6f6be2b9-49f4-4db0-9c21-52062d8ea3df at l31g2000yqb.googlegroups.com>, Pedram wrote: > >This time I have a simple C question! >As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I >found PyObject_NEW_VAR in objimpl.h header file. But I can't >understand the last line :( Here's the code: > >#define PyObject_NEW_VAR(type, typeobj, n) \ >( (type *) PyObject_InitVar( \ > (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), >(n)) ),\ > (typeobj), (n)) ) > >I know this will replace the PyObject_New_VAR(type, typeobj, n) >everywhere in the code and but I can't understand the last line, which >is just 'typeobj' and 'n'! What do they do? Are they make any sense in >allocation process? Look in the code to find out what PyObject_InitVar() does -- and, more importantly, what its signature is. The clue you're missing is the trailing backslash on the third line, but that should not be required if you're using an editor that shows you matching parentheses. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From usernet at ilthio.net Sun Jul 5 12:12:37 2009 From: usernet at ilthio.net (Tim Harig) Date: Sun, 05 Jul 2009 16:12:37 GMT Subject: generation of keyboard events References: Message-ID: On 2009-07-05, RAM wrote: > I need to start an external program and pass the keyboard events like > F1,Right arrow key etc to the program..I am trying to use the > subprocess module to invoke the external program. I am able to invoke > but not able to generate the keyboard events and pass them on to the catb.org/esr/faqs/smart-questions.html You have told us nothing about the environment where you are trying to accomplish this. GUI, CLI, Unix, Windows, etc? So I suggest that you checkout the curses getch functions. You can find them in the standard library documentation at http://docs.python.org. You should also reference documentation for the C version in your systems man pages. From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 12:13:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 16:13:30 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: <0260c2b4$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 11:37:49 +0000, Lie Ryan wrote: > Neither python's `if` nor `if` in formal logic is about testing True vs. > False. `if` in python and formal logic receives a statement. The > statement must be evaluatable to True or False, but does not have to be > True or False themselves. It just happens that True evaluates to True > and False evaluates to False. I think your explanation is a little confused, or at least confusing. `if` implements a two-way branch. Some languages, like Pascal and Java, requires the switch value to take one of two specific enumerable values conventionally spelled TRUE and FALSE (modulo variations in case). Other languages don't require specific enumerable values, and instead accept (e.g.) any integer, or any object, with rules for how to interpret such values in such a context. Forth, for example, branches according to whether the word on the stack is zero or non-zero: "nothing" or "something". Lisp branches according to empty list or non-empty list: "nothing" or "something" again. Other languages, like Ruby, have less intuitive rules. That's their problem. -- Steven From emile at fenx.com Sun Jul 5 12:19:48 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 05 Jul 2009 09:19:48 -0700 Subject: generation of keyboard events In-Reply-To: References: Message-ID: On 7/5/2009 8:56 AM RAM said... > Hi, > > I need to start an external program and pass the keyboard events like > F1,Right arrow key etc to the program..I am trying to use the > subprocess module to invoke the external program. I am able to invoke > but not able to generate the keyboard events and pass them on to the > external progam. If you're on *nix, search for python and expect and you'll find something based apps -- I'm not sure what you do for GUIs. On windows, I'm sure there are ways of doing this with the win32 extensions, but I commonly use msched from www.mjtnet.com. I create msched scripts from within python and use subprocess to invoke msched and execute the python generated script. HTH, Emile > Please help me in this because I am a beginner. > > regards > Sreerama V From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 12:20:32 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 16:20:32 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> <025eaa4f$0$20657$c3e8da3@news.astraweb.com> <7x1vovxohy.fsf@ruckus.brouhaha.com> Message-ID: <0260c45a$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 06:12:25 -0700, Paul Rubin wrote: >> There are three natural approaches to (say) re.search() for dealing >> with failure: >> >> (1) return a sentinel value like None; (2) return a matchobject which >> tests False; (3) raise an exception. > > 4. Have re.search return a bool and possible matchobject separately: > > put_match_here = [] > if re.search(pat, s, target=put_match_here): > do_something_with(put_match_here[0]) Wow. I never for the life of me thought I'd see an experienced Python programmer re-implement Pascal's VAR parameters. That is... hideous. Returning a (flag, matchobject) tuple is the height of beauty in comparison. -- Steven From pm567426 at gmail.com Sun Jul 5 12:32:41 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 09:32:41 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> Message-ID: <179f64f8-6528-4953-88b1-4096d5907cbe@p29g2000yqh.googlegroups.com> On Jul 5, 8:12?pm, a... at pythoncraft.com (Aahz) wrote: > In article <6f6be2b9-49f4-4db0-9c21-52062d8ea... at l31g2000yqb.googlegroups.com>, > > > > Pedram ? wrote: > > >This time I have a simple C question! > >As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I > >found PyObject_NEW_VAR in objimpl.h header file. But I can't > >understand the last line :( Here's the code: > > >#define PyObject_NEW_VAR(type, typeobj, n) \ > >( (type *) PyObject_InitVar( \ > > ? ? ?(PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), > >(n)) ),\ > > ? ? ?(typeobj), (n)) ) > > >I know this will replace the PyObject_New_VAR(type, typeobj, n) > >everywhere in the code and but I can't understand the last line, which > >is just 'typeobj' and 'n'! What do they do? Are they make any sense in > >allocation process? > > Look in the code to find out what PyObject_InitVar() does -- and, more > importantly, what its signature is. ?The clue you're missing is the > trailing backslash on the third line, but that should not be required if > you're using an editor that shows you matching parentheses. > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "as long as we like the same operating system, things are cool." --piranha No, they wrapped the 3rd line! I'll show you the code in picture below: http://lh3.ggpht.com/_35nHfALLgC4/SlDVMEl6oOI/AAAAAAAAAKg/vPWA1gttvHM/s640/Screenshot.png As you can see the PyObject_MALLOC has nothing to do with typeobj and n in line 4. From pm567426 at gmail.com Sun Jul 5 12:34:37 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 09:34:37 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> <179f64f8-6528-4953-88b1-4096d5907cbe@p29g2000yqh.googlegroups.com> Message-ID: <5a0b1063-ae2b-4229-b040-4904b4d01077@b15g2000yqd.googlegroups.com> On Jul 5, 8:32?pm, Pedram wrote: > On Jul 5, 8:12?pm, a... at pythoncraft.com (Aahz) wrote: > > > > > In article <6f6be2b9-49f4-4db0-9c21-52062d8ea... at l31g2000yqb.googlegroups.com>, > > > Pedram ? wrote: > > > >This time I have a simple C question! > > >As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I > > >found PyObject_NEW_VAR in objimpl.h header file. But I can't > > >understand the last line :( Here's the code: > > > >#define PyObject_NEW_VAR(type, typeobj, n) \ > > >( (type *) PyObject_InitVar( \ > > > ? ? ?(PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), > > >(n)) ),\ > > > ? ? ?(typeobj), (n)) ) > > > >I know this will replace the PyObject_New_VAR(type, typeobj, n) > > >everywhere in the code and but I can't understand the last line, which > > >is just 'typeobj' and 'n'! What do they do? Are they make any sense in > > >allocation process? > > > Look in the code to find out what PyObject_InitVar() does -- and, more > > importantly, what its signature is. ?The clue you're missing is the > > trailing backslash on the third line, but that should not be required if > > you're using an editor that shows you matching parentheses. > > -- > > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > > "as long as we like the same operating system, things are cool." --piranha > > No, they wrapped the 3rd line! > > I'll show you the code in picture below:http://lh3.ggpht.com/_35nHfALLgC4/SlDVMEl6oOI/AAAAAAAAAKg/vPWA1gttvHM... > > As you can see the PyObject_MALLOC has nothing to do with typeobj and > n in line 4. Oooooh! What a mistake! I got it! they're Py_Object_InitVar parameters. Sorry and Thanks! From lie.1296 at gmail.com Sun Jul 5 12:43:35 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 05 Jul 2009 16:43:35 GMT Subject: question of style In-Reply-To: <0260c2b4$0$20657$c3e8da3@news.astraweb.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> <0260c2b4$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 05 Jul 2009 11:37:49 +0000, Lie Ryan wrote: > >> Neither python's `if` nor `if` in formal logic is about testing True vs. >> False. `if` in python and formal logic receives a statement. The >> statement must be evaluatable to True or False, but does not have to be >> True or False themselves. It just happens that True evaluates to True >> and False evaluates to False. > > I think your explanation is a little confused, or at least confusing. Indeed, partially because I said "statement" when I really meant "expression". > Other languages don't require specific enumerable values, and instead > accept (e.g.) any integer, or any object, with rules for how to interpret > such values in such a context. That was what I was wanting to say, except that I stretched that to formal logic (mathematical logic). Even in formal logic `if` receives any arbitrary expression that can be -- according to certain rules -- interpreted as True or False (i.e. the expressions themselves are not required to be a boolean value). The conclusion is python's `if` does not deviate from `if`'s semantic in mathematical sense. From piet at cs.uu.nl Sun Jul 5 13:28:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 05 Jul 2009 19:28:35 +0200 Subject: Determining if a function is a method of a class within a decorator References: <4A4963E9.30202@ilm.com> Message-ID: >>>>> David Hirschfield (DH) wrote: >DH> Yeah, it definitely seems like having two separate decorators is the >DH> solution. But the strange thing is that I found this snippet after some >DH> deep googling, that seems to do something *like* what I want, though I >DH> don't understand the descriptor stuff nearly well enough to get what's >DH> happening: >DH> http://stackoverflow.com/questions/306130/python-decorator-makes-function-forget-that-it-belongs-to-a-class >DH> answer number 3, by ianb. It seems to indicate there's a way to introspect >DH> and determine the class that the function is going to be bound to...but I >DH> don't get it, and I'm not sure it's applicable to my case. >DH> I'd love an explanation of what is going on in that setup, and if it isn't >DH> usable for my situation, why not? What that example does is not getting the name of the class in the decorator, but in the bound method that is the result of the decorator when that method is called. This is just done by asking for the class of the self parameter. Actually they even don't do that in that example but it could have been done. Note also that there is an error in the code: keyargs should be kw. There is also something special in that code: it uses the descriptor protocol. This is necessary for a method. The descriptor protocol for methods defines a __get__ method that transforms the unbound method into a bound method. That code uses this to decorate the generated bound method object instead of decorating the unbound method. A side effect of doing the class detection at call time is that you get the name of the subclass if you use the method on an instance of the subclass, not the name of the class that the method was defined in: class D(C): pass D().f(1, 2) will talk about class D, not class C. So if you would like to do something special for bound methods the __get__ might be the proper place to do it. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From a0046088 at airmail.net Sun Jul 5 14:20:26 2009 From: a0046088 at airmail.net (wwwayne) Date: Sun, 05 Jul 2009 12:20:26 -0600 Subject: Clarity vs. code reuse/generality References: Message-ID: <3in15557eu33n494hh3lvmcnssujl2ueds@4ax.com> On Fri, 03 Jul 2009 14:34:58 GMT, Alan G Isaac wrote: >On 7/3/2009 10:05 AM kj apparently wrote: === 8< === >2. >from scipy.optimize import bisect >def _binary_search(lo, hi, func, target, epsilon): > def f(x): return func(x) - target > return bisect(f, lo, high, xtol=epsilon) > >3. If you don't want to use SciPy (why?), have them >implement http://en.wikipedia.org/wiki/Bisection_method#Pseudo-code >to produce their own `bisect` function. Of course this isn't really pseudo-code, it's VB code with quite poor comments: 'Bisection Method 'Start loop Do While (abs(right - left) > 2*epsilon) 'Calculate midpoint of domain midpoint = (right + left) / 2 'Find f(midpoint) If ((f(left) * f(midpoint)) > 0) Then 'Throw away left half left = midpoint Else 'Throw away right half right = midpoint End If Loop Return (right + left) / 2 and even just throwing away the VB code and leaving the comments does not give a good algorithm: 'Bisection Method 'Start loop 'Calculate midpoint of domain 'Find f(midpoint) 'Throw away left half 'Throw away right half A much better approach to teaching introductory programming in any language at almost any level is to incorporate some "top down problem solving", including writing a method of solution (algorithm) in some reasonably well-defined pseudo-code that can be easily elaborated and translated into one's target language (and, peferably, some reasonable-sized subset of related languages). This pseudo-code should then become comments (or equivalent) for the students to convert to real code, as in: Algorithm bisect (f, left, right, epsilon): # Bisection Method to find a root of a real continuous function f(x): # Assume f(x) changes sign between f(left) and f(right) and # we want a value not further than epsilon from a real root. # Begin with the domain left...right. # While the absolute value of (left - right) exceeds 2*epsilon: # Calculate the midpoint, mid, of the domain. # If the product of f(left) and f(mid) is positive: # Set left to mid; # Otherwise: # Set right to mid. # Return the midpoint of left...right. === And adapting this approach to kj's case is straightforward. Of course, what consitutes a suitable vocabulary and syntax for an algorithm pseudo-code language depends upon the target language(s), the tastes of the instructor, and the point in the lesson or course. My choice is for python+COBOL (as above) initially, soon incorporating the usual arithmetic and relational operators (and how soon and how many at once depends upon the level of the students: for an introductory university/college course in Computer Science or equivalent, where everyone should have a reasonable background in mathemtics notation as a prerequisite, this should be very soon and quite fast), arrays and subscripting, etc. But if we were to write this algorithm or kj's in python-like pseudo-code it would already *be* python codeor very close to it--which is why we should teach intorductory programming in python. Very soon students would be writing algorithms that required very little elaboration to be programs. But without including suitable problem solving and psudo-code algorithm writing there will soon come a time or an example where students are trying to think in code instead of in their natural language and don't have the experience and repertoire to be able to do that well. I hope that's not too pedantic or AR? wayne >hth, >Alan Isaac From dotancohen at gmail.com Sun Jul 5 14:32:56 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sun, 5 Jul 2009 21:32:56 +0300 Subject: A C++ user's introduction to Python: a really good read Message-ID: <880dece00907051132p117ca7e0sf5774d1eb5014c64@mail.gmail.com> Here is a C++ KDE programer's take on Python: http://majewsky.wordpress.com/2009/07/04/python-experiences-or-why-i-like-c-more/ Good read. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From nick.m.daly at gmail.com Sun Jul 5 14:48:06 2009 From: nick.m.daly at gmail.com (Nick Daly) Date: Sun, 5 Jul 2009 13:48:06 -0500 Subject: Method to separate unit-test methods and data? In-Reply-To: References: Message-ID: Hi, I was wondering if it's possible / if there are any simple methods known of storing unit-test functions and their data in separate files? Perhaps this is a strange request, but it does an excellent job of modularizing code. ?As far as revision control goes, it makes it easier to discern between new or changed test cases, and changes in the test data. I've worked through this idea a bit and actually have a nearly working model. ?For example, when testing a class, I have a test module, which contains a class for testing each method of the class. ?This allows me to generalize each method's parameters into each class, which can then be overridden by the config file's data, something like as follows (with a really arbitrary example, Python 2.5 code): Demo code (midpoint.py): ======================= class Midpoint(object): ? ?def __init__(self, a, b): ? ? ? ?self.a = a ? ? ? ?self.b = b ? ?def mid(): ? ? ? ?return (self.a + self.b) / 2.0 ? ?def sum() ? ? ? ?return (self.a + self.b) Testing Code (test_Midpoint.py): =============================== import unittest import midpoint import sys, ConfigParser as configparser # set up the config file that overrides each class's data config = configparser.SafeConfigParser() config.read(sys.argv[0]) class test_Midpoint_mid(unittest.TestCase): ? ?def __init__(self): ? ? ? ?# default testing values ? ? ? ?self.none_values = ((None, 1), ? ? ? ? ? ? ? ? ? ? ? ? ? ?(0, None)) ? ? ? ?# override the default values with the config file values ? ? ? ?for key, value in config.items(self.__class__.__name__): ? ? ? ? ? ?if value: ? ? ? ? ? ? ? ?setattr(self, key, value) ? ?# a few tests of the method ? ?def test_rejectNone(self): ? ? ? ?for tests in self.none_values: ? ? ? ? ? ?self.assertRaises(TypeError, ? ? ? ? ? ? ? ?midpoint.Midpoint(tests[0], tests[1]).mid) # and repeat the concept for class test_Midpoint_sum Config Code (test_Midpoint.cfg): ============================== # override the default values of the test class's members [test_Midpoint_mid] none_values = ((-1, None), ? ? ? ? ? ? ? (None, -12.8)) What I haven't yet figured out how to do though, is properly override the default class member values with values from the config file. ?The config file's data is loaded as a string instead of as a list, as I'd want. ?This causes all the tests to fail, as while none_values needs to be interpreted as a list, it is instead understood as: " ((-1, None),\n ? ? ? ? ? ? ? (None, -12.8))" Does anyone have any solutions for these problems? ?First, is there a known and simple way to separate unit-test data and methods into separate files? ?Secondly, if not, is there a simple way to convert strings into other base types, like lists, dictionaries, and so forth? Or, am I going to have to write my own list-parsing methods? ?Would pickling help? ?I haven't yet had a chance to look into if or how that would work... ?If there's anything else I can clarify about this request, feel free to let me know. Thanks for any help you can provide, Nick From maymunbeyin at gmail.com Sun Jul 5 14:59:01 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 11:59:01 -0700 (PDT) Subject: Creating alot of class instances? Message-ID: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Hi I am new to Python classes and trying to figure out this particular issue here. I will need to create instances of a class. But at the moment I do not know how many instances I will end up having, in every case it might be different. Most of the documents I read makes this simpl class-student analogy to explain python classes which is fine. But in those examples the number and the names of the instances were known and limited. I will be querying some data and create class instances based on the data I gather. But the problem as I mentioned is that I do not know the names and the number of the end class instances. They will be based on the content of the data. So how can I create class instances within a loop and when the loop is done how can I figure out the list of instances via class membership? I can track the names by introducing another list but I want to understand the class side of things. The solution might be dead simple but I just cannot figure out at the moment. For example this is what I need in the simplest form class myclass(): def __init__(self,name): self.name=name for count,data in enumerate(some list): instance_count=myclass() instance_count.name=data print instances thanks From tjreedy at udel.edu Sun Jul 5 15:09:33 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 05 Jul 2009 15:09:33 -0400 Subject: question of style In-Reply-To: <7xy6r34f3j.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > I don't know what a furphy is, but I don't accept that "somethingness" > vs. "nothingness" is the same distinction as truth vs falsehood. True > and False are values in a specific datatype (namely bool), not > abstract qualities of arbitrary data structures. The idea that the > "if" statement selects between "somethingness" and "nothingness" > rather than between True and False is a bogus re-imagining of the > traditional function of an "if" statement and has been an endless > source of bugs in Python code. Look how much confusion it causes here > in the newsgroup all the time. You appear to be confusing a specific interpretation of an abstraction with the abstraction itself. Or perhaps better, you seem to be confusing a specific example of a general process with the general process. A boolean variable is a variable that is in one of two states -- a binary variable -- a variable that carries one bit of information. The two states are the marked state and the unmarked or default state. Which is to say, when we draw a distinction to distinguish two states, we mark one of them to distinguish one from the other. The if statement tests whether an object is in the marked state (or not). Truth and falsity of propositions are one possible interpretation of marked and unmarked, giving us propositional logic. But they are only one of many. So are in and out of a particular class and member or not of a set or subclass or not of a set, giving us class and set logic. So are closed and open, said of gates or switches, or on and off, giving us switching logic. So are non-zero and zero, said of numbers. Or done and not done, said of an algorithmic process. Counts 0 and 1, and their representations '0' and '1', taken in themselves, are as good as any distinct pair as a pair of labels for the two distinct states. They have some computational advantages, including making it easy to count the number of objects in a collection in the marked state (and, given the total number, the number in the unmarked state). They have one disadvantage, though. If I say 'x = 1', do I mean 1 versus 0 or 1 versus all possible ints? Similarly, If 'print(x)' prints 1, does it mean 1 versus 0 or 1 versus all other ints? Recognizing this, Guido decided to subclass them and give them alternate names. He could have chosen 'Marked' and 'Unmarked', or any of several other pairs, but did choose the conventional 'True' and 'False', referring to the common propositional interpretation. However, he specifically disclaimed any intention to restrict 'if' to testing specific logic propositions, as opposed to the general proposition 'object is in the marked state'. Terry Jan Reedy From andreengels at gmail.com Sun Jul 5 15:20:38 2009 From: andreengels at gmail.com (Andre Engels) Date: Sun, 5 Jul 2009 21:20:38 +0200 Subject: Creating alot of class instances? In-Reply-To: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <6faf39c90907051220o5b2adebbnae1136d987209a5a@mail.gmail.com> On 7/5/09, kk wrote: > I am new to Python classes and trying to figure out this particular > issue here. I will need to create instances of a class. But at the > moment I do not know how many instances I will end up having, in every > case it might be different. Most of the documents I read makes this > simpl class-student analogy to explain python classes which is fine. > But in those examples the number and the names of the instances were > known and limited That's no problem. The only limit to the number of instances of a class you can create is your memory - and not even that if you don't need to 'keep' the instances. > I will be querying some data and create class instances based on the > data I gather. But the problem as I mentioned is that I do not know > the names and the number of the end class instances. They will be > based on the content of the data. So how can I create class instances > within a loop and when the loop is done how can I figure out the list > of instances via class membership? I can track the names by > introducing another list but I want to understand the class side of > things. > > The solution might be dead simple but I just cannot figure out at the > moment. > > For example this is what I need in the simplest form > > class myclass(): > def __init__(self,name): > self.name=name > > for count,data in enumerate(some list): > instance_count=myclass() > instance_count.name=data > > print instances Okay, to solve your problem, we add a list containing all the instances: class myclass(): def __init__(self,name): self.name=name instances = [] for count,data in enumerate(some list): instance_count=myclass() instance_count.name=data instances.append(instance_count) print instances ============================================= However, that won't work because myclass has an __init__ with 2 attributes, so you will have to call it using an attribute: class myclass(): def __init__(self,name): self.name=name instances = [] for count,data in enumerate(some list): instance_count=myclass(data) instances.append(instance_count) print instances ============================================= This works, but it can be done better: First we notice that count is not used at all, so why create it? class myclass(): def __init__(self,name): self.name=name instances = [] for data in some list: instance_count=myclass(data) instances.append(instance_count) print instances ============================================= Then, the variable instance_count is created once, then used in the next line. We can do that at once: class myclass(): def __init__(self,name): self.name=name instances = [] for data in some list: instances.append(myclass(data)) print instances ==================== Finally, "print instances" does not give very nice looking information, so I would change this to: class myclass(): def __init__(self,name): self.name=name instances = [] for data in some list: instances.append(myclass(data)) print (instance.name for instance in instances) -- Andr? Engels, andreengels at gmail.com From python.list at tim.thechases.com Sun Jul 5 15:21:35 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 05 Jul 2009 14:21:35 -0500 Subject: Creating alot of class instances? In-Reply-To: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <4A50FD3F.7080105@tim.thechases.com> > The solution might be dead simple but I just cannot figure out at the > moment. > > For example this is what I need in the simplest form > > class myclass(): > def __init__(self,name): > self.name=name > > for count,data in enumerate(some list): > instance_count=myclass() > instance_count.name=data > > print instances Sounds like a use for a list: instances = [] for count, data in enumerate(some_list): # 1) camel-case is preferred for classnames # 2) since your __init__() expects the name # pass it in, instead of setting it later instance = MyClass(data) instances.append(instance) This can be written in a slightly more condensed-yet-readable list-comprehension form as: instances = [MyClass(data) for data in some_list] You then have a list/array of instances you can print: print instances or pick off certain items from it: print instances[42] -tkc From martin at v.loewis.de Sun Jul 5 15:23:50 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 05 Jul 2009 21:23:50 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <4A50FDC6.1040604@v.loewis.de> > This is a good test for Python implementation bottlenecks. Run > that tokenizer on HTML, and see where the time goes. I looked at it with cProfile, and the top function that comes up for a larger document (52k) is ...validator.HTMLConformanceChecker.__iter__. This method dispatches various validation routines, and it computes the method names from the input over and over again, doing lots of redundant string concatenations. It also capitalizes the element names, even though the spelling in the original document is probably not capitalized (but either upper-case or lower case). In my patch below, I create a dictionary of bound methods, indexed by (syntax) type and name, following the logic of falling back to just type-based validation if no type/name routine exists. However, in order to reduce the number of dictionary lookups, it will also cache type/name pairs (both in the original spelling, and the capitalized spelling), so that subsequent occurrences of the same element will hit the method cache. With this simple optimization, I get a 20% speedup on my test case. In my document, there are no attributes - the same changes should be made to attribute validation routines. I don't think this has anything to do with the case statement. Regards, Martin -------------- next part -------------- A non-text attachment was scrubbed... Name: methodlookup.diff Type: text/x-patch Size: 2837 bytes Desc: not available URL: From maymunbeyin at gmail.com Sun Jul 5 15:54:58 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 12:54:58 -0700 (PDT) Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> Hi Thank you soo much for speedy and in detailed help. Your replies really cleared out most of the cloud for me. I have one last issue to resolve which is something I did not articulate properly, I realize now. The last issue is actually automatically naming the instances. The reason I was using the "instance_count" is for enumerating the actual name of an instance. For example lets say I have class MyMaterials: and my instances might need to look like material_01 material_02 or light_01 light_02 or Mesh_01 Mesh_02 etc I will need to get the base names from the "some list" and create the names accordingly from the list array. Basically I also need to generate the instance names based on the some list. For example some_list[2] might denote a name for the instance. I will study the sampled codes in depth now. Maybe you have answered the naming issue as well, if so please frgove the noise. thanks From vasudevram at gmail.com Sun Jul 5 16:14:31 2009 From: vasudevram at gmail.com (vasudevram) Date: Sun, 5 Jul 2009 13:14:31 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? References: <7b5somF21peceU1@mid.uni-berlin.de> Message-ID: <5328d134-5fb0-4a0f-b24b-86deeeaf97aa@i4g2000prm.googlegroups.com> On Jul 3, 1:11?pm, "Diez B. Roggisch" wrote: > Allen Fowler schrieb: > > > > > > > > >> I have an (in-development) python system that needs to shuttle events / requests > >> around over the network to other parts of itself. ? It will also need to > >> cooperate with a .net application running on yet a different machine. > > >> So, naturally I figured some sort of HTTP event / RPC type of would be a good > >> idea? > > >> Are there any modules I should know about, or guidelines I could read, that > >> could aid me in the design of the API? ? ? > > > To clarify: > > > Each message would be <1KB of data total, and consist of some structured object containing strings, numbers, dates, etc. > > > For instance there would be an "add user" request that would contain one or more User objects each having a number of properties like: > > > - Full Name > > - Username > > - Password > > - Email addresses (a variable length array) > > - Street Address line1 > > - Street Address line1 > > - Street Address line1 > > - City > > - State > > - Zip > > - Sign Up Date > > > .... and so on. > > > Since I need to work with other platforms, pickle is out... ?what are the alternatives? ?XML? JSON? > > > How should I formally define each of the valid messages and objects? > > > Thank you, > > Use XMLRPC. Implementations for both languages are available. There is > no need for formal spec - which is a good thing. You just call the > server, and it works. > > Diez I second the suggestion of Diez to use XML-RPC. Very simple to learn and use. Supports structs (as method arguments and method return values) which can consist of other data types bundled together, also supports arrays. Just check whether .NET supports XML-RPC. From ricli85 at gmail.com Sun Jul 5 16:38:50 2009 From: ricli85 at gmail.com (Rickard Lindberg) Date: Sun, 5 Jul 2009 22:38:50 +0200 Subject: Creating alot of class instances? In-Reply-To: <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> Message-ID: <890d8ab70907051338p2eaac716qe96f3a1953d4696c@mail.gmail.com> > Thank you soo much for speedy and in detailed help. Your replies > really cleared out most of the cloud for me. I have one last issue to > resolve which is something I did not articulate properly, I realize > now. The last issue is actually automatically naming the instances. > The reason I was using the "instance_count" is for enumerating the > actual name of an instance. > > For example lets say I have > > class MyMaterials: > > and my instances might need to look like > > material_01 > material_02 > or > light_01 > light_02 > or > Mesh_01 > Mesh_02 etc If you do not know how many instances you are going to create from the beginning, there is no way for you to know which of the instances you mentioned above will get created. So having names for all of the instances will not help you since you will never know what names are "safe" to use. On the other hand, if you have all instances in a list, you can refer to them by index and you know exactly how many of them you have. If you would like to get instances by some name you gave to them, maybe something like this will work: def get_instance(name): for instance in instance_list: if instance.name == name: return instance return None Note that this might very well return None if no instance with that particular name was found. -- Rickard Lindberg From no.email at please.post Sun Jul 5 16:42:41 2009 From: no.email at please.post (kj) Date: Sun, 5 Jul 2009 20:42:41 +0000 (UTC) Subject: memoization module? Message-ID: Is there a memoization module for Python? I'm looking for something like Mark Jason Dominus' handy Memoize module for Perl. TIA! kj From l.mastrodomenico at gmail.com Sun Jul 5 16:50:30 2009 From: l.mastrodomenico at gmail.com (Lino Mastrodomenico) Date: Sun, 5 Jul 2009 22:50:30 +0200 Subject: memoization module? In-Reply-To: References: Message-ID: 2009/7/5 kj : > Is there a memoization module for Python? ?I'm looking for something > like Mark Jason Dominus' handy Memoize module for Perl. Check out the "memoized" class example here: -- Lino Mastrodomenico From python.list at tim.thechases.com Sun Jul 5 17:07:45 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 05 Jul 2009 16:07:45 -0500 Subject: Creating alot of class instances? In-Reply-To: <890d8ab70907051338p2eaac716qe96f3a1953d4696c@mail.gmail.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> <890d8ab70907051338p2eaac716qe96f3a1953d4696c@mail.gmail.com> Message-ID: <4A511621.9050101@tim.thechases.com> >> For example lets say I have >> >> class MyMaterials: >> >> and my instances might need to look like >> >> material_01 >> material_02 >> or >> light_01 >> light_02 >> or >> Mesh_01 >> Mesh_02 etc > > If you do not know how many instances you are going to create from the > beginning, there is no way for you to know which of the instances you mentioned > above will get created. So having names for all of the instances will not help > you since you will never know what names are "safe" to use. > > On the other hand, if you have all instances in a list, you can refer to them > by index and you know exactly how many of them you have. > > If you would like to get instances by some name you gave to them, maybe > something like this will work: > > def get_instance(name): > for instance in instance_list: > if instance.name == name: > return instance > return None Another option might be to use a counter in the class that keeps track of the number of instances: class MyMaterial: instances = 0 def __init__(self, name): self.name = name self.instance = MyMaterial.instances MyMaterial.instances += 1 def __str__(self): return "%s_%02i" % (self.name, self.instance) m = MyMaterial("Brick") print m # "Brick_00" print repr([MyMaterial("Stone") for _ in range(5)]) # "[Stone_01, Stone_02, Stone_03, Stone_04, Stone_05]" It's not thread-safe, but it may do the trick. -tkc From lists at cheimes.de Sun Jul 5 17:42:41 2009 From: lists at cheimes.de (Christian Heimes) Date: Sun, 05 Jul 2009 23:42:41 +0200 Subject: Creating alot of class instances? In-Reply-To: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <4A511E51.9030408@cheimes.de> kk wrote: > I will be querying some data and create class instances based on the > data I gather. But the problem as I mentioned is that I do not know > the names and the number of the end class instances. They will be > based on the content of the data. So how can I create class instances > within a loop and when the loop is done how can I figure out the list > of instances via class membership? I can track the names by > introducing another list but I want to understand the class side of > things. Do you need an exact number or just a rough statistic? In order to estimate the number of instances you can query the reference count of the class. Since every instance usually increases the reference count by one it provides a good overview. Note that lots of other things like imports increase the reference count, too. >>> import sys >>> class Example(object): ... pass ... >>> sys.getrefcount(Example) 5 >>> examples = list(Example() for i in range(10)) >>> examples [<__main__.Example object at 0x7f2e5cd61110>, <__main__.Example object at 0x7f2e5cd61150>, <__main__.Example object at 0x7f2e5cd61190>, <__main__.Example object at 0x7f2e5cd611d0>, <__main__.Example object at 0x7f2e5cd61210>, <__main__.Example object at 0x7f2e5cd61250>, <__main__.Example object at 0x7f2e5cd61390>, <__main__.Example object at 0x7f2e5cd613d0>, <__main__.Example object at 0x7f2e5cd61410>, <__main__.Example object at 0x7f2e5cd61450>] >>> sys.getrefcount(Example) 15 From rhodri at wildebst.demon.co.uk Sun Jul 5 17:47:25 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 05 Jul 2009 22:47:25 +0100 Subject: pep 8 constants In-Reply-To: <4A4E1B2F.2000700@mrabarnett.plus.com> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> <4A4E1B2F.2000700@mrabarnett.plus.com> Message-ID: On Fri, 03 Jul 2009 15:52:31 +0100, MRAB wrote: > Eric S. Johansson wrote: >> Horace Blegg wrote: >>> I've been kinda following this. I have a cousin who is permanently >>> wheel >>> chair bound and doesn't have perfect control of her hands, but still >>> manages to use a computer and interact with society. However, the >>> idea/thought of disabled programmers was new to me/hadn't ever occurred >>> to me. >>> >>> You say that using your hands is painful, but what about your feet? >>> Wouldn't it be possible to rig up some kind of foot peddle for >>> shift/caps lock? Kinda like the power peddle used with sowing machines, >>> so the hands are free to hold fabric. >>> >>> I don't mean this in a condescending manor, and I apologize if you take >>> it as such. I'm genuinely curious if you think something like this >>> could >>> work. >>> >>> The way I was envisioning it working last night (and I haven't the >>> faintest clue how SR works, nor have I ever used SR) was that you would >>> hit the foot peddle, which would tell the SR program to capitalize the >>> first letter of the next word (a smart shift, basically, so you don't >>> end up doing something like ... WONderland -or- "stocks are up 1,0))% >>> TOday".) >>> >>> Possible? Stupid? >>> >> it's not stupid. >> People have used foot pedals for decades for a variety of controls. I >> don't >> think foot pedals would work for me because when I am dictating, I pace. >> Standing, sitting, I pace. With a cord headset, I'm forced to stay >> within about >> 4 feet of the computer. But what I'm using a Bluetooth headset, I will >> sometimes >> ramble as far as 10 or 15 feet from the computer. It helps if I make >> the font >> larger so I can glance over and see what kind of errors I've gotten. >> I really love a Bluetooth headset with speech recognition. It's so >> liberating. >> Your question about foot pedals makes me think of alternative. would >> it make >> sense to have a handheld keyboard which would be used for >> command-and-control >> functionality or as an adjunct to speech recognition use? It would have >> to be >> designed in such a way that it doesn't aggravate a hand injury which >> may not be >> possible. Anyway, just thinking out loud. >> > You can get giant piano keyboards that you step on, so how about a giant > computer keyboard? "I wrote 5 miles of code before lunch!" :-) You can get/make MIDI organ pedal-boards (a friend of mine has two). From there it's just one small step... :-) -- Rhodri James *-* Wildebeest Herder to the Masses From python.list at tim.thechases.com Sun Jul 5 17:56:07 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 05 Jul 2009 16:56:07 -0500 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> <4A4E1B2F.2000700@mrabarnett.plus.com> Message-ID: <4A512177.8070106@tim.thechases.com> >> You can get giant piano keyboards that you step on, so how about a giant >> computer keyboard? "I wrote 5 miles of code before lunch!" :-) > > You can get/make MIDI organ pedal-boards (a friend of mine has two). From > there it's just one small step... Is that a two-step? a box-step? Count it off...slow, slow, quick, quick. I think I just coded up Conway's Game of Life... -tkc From rhodri at wildebst.demon.co.uk Sun Jul 5 18:03:13 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 05 Jul 2009 23:03:13 +0100 Subject: PEP368 and pixeliterators In-Reply-To: <025db110$0$20657$c3e8da3@news.astraweb.com> References: <025db110$0$20657$c3e8da3@news.astraweb.com> Message-ID: On Fri, 03 Jul 2009 09:21:09 +0100, Steven D'Aprano wrote: > On Thu, 02 Jul 2009 10:32:04 +0200, Joachim Str?mbergson wrote: > >> for pixel in rgb_image: >> # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, >> pixel.r >> >> >> The idea I'm having is that fundamentally the image is made up of a 2D >> array of pixels, not rows of pixels. > > A 2D array implies rows (and columns) of pixels. But not necessarily an internal representation in which those rows or columns are contiguous. An efficient internal storage format might well include margins to make transform edge cases easier, or store the pixel components in separate arrays, or both. I'd imagine that quite frequently, the iterator across all pixels will in fact just be hiding from the programmer the fact that it's really iterating by row and then by pixel. At Python's level of abstraction, that's just fine, but the assumption that an image is made up of a 2D array of pixels is not safe. -- Rhodri James *-* Wildebeest Herder to the Masses From torriem at gmail.com Sun Jul 5 18:27:40 2009 From: torriem at gmail.com (Michael Torrie) Date: Sun, 05 Jul 2009 16:27:40 -0600 Subject: Wrapping comments In-Reply-To: References: Message-ID: <4A5128DC.5060305@gmail.com> Lawrence D'Oliveiro wrote: > I tried using Emacs via SSH from a Mac once. Made me run screaming for the > nearest Windows box > . Interesting rant, but the problem is with the key bindings they chose to use in Terminal.app program. Fortunately in leopard most of the problems are now fixed, or can be configured to work in a non-broken fashion. The rest of us, in the meantime, all switched to iTerm which, although had some performance issues, behaved like we all expected terminals to behave. As far as terminal hell goes, I regularly find that when ssh-ing to remote boxes that backspace doesn't work. Or does in bash (because it's smart enough to play games) but not in vim. Somehow sometimes over ssh the key bindings for backspace get lost of messed up. From http Sun Jul 5 18:51:09 2009 From: http (Paul Rubin) Date: 05 Jul 2009 15:51:09 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> <0260bf8d$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7x8wj2agma.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > > but I don't accept that "somethingness" > > vs. "nothingness" is the same distinction as truth vs falsehood. > > It's the distinction used by Python since the dawn of time. Python only > grew a bool type a few versions back. That's true, part of the situation we have now is an artifact of that history. > I'm not talking about the constants True and False (nouns), but about > true and false values (adjectives). But, it seems to me, the constants True and False are the only values to which the adjectives "true" and "false" should be applicable to. > > > The idea that the "if" > > statement selects between "somethingness" and "nothingness" rather than > > between True and False is a bogus re-imagining of the traditional > > function of an "if" statement > > There's nothing bogus about it. > > > and has been an endless source of bugs in Python code. > I wonder why these "endless" bugs weren't important enough to be > mentioned in the rationale to PEP 285: Because adding the bool type doesn't really fix those bugs. > describing `if x` as the "correct form" and calling scrapping such a > feature as "crippling the language". Certainly, changing "if" would have broken an immense amount of code and been a completely unworkable approach. We are using a fairly mature language by now; it has a culture and history that carries certain baggage, as one should expect. > > Look how much confusion it causes here in the newsgroup all the time. > The only confusion is that you're causing me. Would you care to link to > some? This current discussion (about bools) came from such confusion just a few posts up in this very thread: From: upwestdon Date: Fri, 3 Jul 2009 23:03:39 -0700 (PDT) How about just: if not (self.higher and self.lower): return self.higher or self.lower That test was designed to treat None as a boolean False, without noticing that numeric 0 is also treated as False and could make the test do the wrong thing. This is an extremely common type of error. > > could see some value to having a generic "is_empty" predicate > We have that already. It's spelled __bool__ or __nonzero__ That's fine, but under the "explicit is better than implicit" principle, it's preferable to call that predicate explicitly: "if bool(x): ..." rather than "if x:". Also, after many years of fixing bugs caused by the mushing together of None and False, it seems to me that we'd have been better off if bool(None) raised an exception rather than returning false. None is supposed to denote a nonexistent value, not a false or empty value. The only valid way to check whether something is None should have been "if x is None". However, it is of course way too late to do this differently. > Iterators are a special case, because in general they can't tell if > they're exhausted until they try to yield a value. Right, it would be nice if they supported a lookahead slot, though that would complicate a lot of __iter__ methods. There's been various kludgy attempts to wrap them in ways that support this, though. From http Sun Jul 5 18:53:34 2009 From: http (Paul Rubin) Date: 05 Jul 2009 15:53:34 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <0a175b39-f084-4814-92e0-80b6cfce6130@c36g2000yqn.googlegroups.com> Message-ID: <7x4otqagi9.fsf@ruckus.brouhaha.com> Simon Forman writes: > BTW, Paul, kind of a tangent: I reimplemented the same algorithm but > using tuples instead of instances (and empty tuples for "NULL" > values.) I was trying to mess around in the space you seemed to > indicate existed, i.e. a better implementation using other datatypes, > but I didn't have a clear idea what I was doing and, as I said, I > started by simply re-implementing with a different datatype. > > Much to my surprise and delight, I discovered the tuple-based BTree > was /already/ a "persistent data type"! It was both awesome and a bit > of an anti-climax. :] Cool ;-). It also seems to me a bit irregular to require every tree to have a node with optional children, rather than allowing trees to be compleely empty. I think the irregularity complicated the code somewhat. From ldo at geek-central.gen.new_zealand Sun Jul 5 18:54:43 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 10:54:43 +1200 Subject: Wrapping comments References: Message-ID: In message , Michael Torrie wrote: > Lawrence D'Oliveiro wrote: > >> I tried using Emacs via SSH from a Mac once. Made me run screaming for >> the nearest Windows box >> . > > Interesting rant, but the problem is with the key bindings they chose to > use in Terminal.app program. Except for control-space, which was defined systemwide to bring up Spotlight. So you can't blame the Terminal app for that. The brain damage was more widespread than just one program. From contact at xavierho.com Sun Jul 5 20:20:39 2009 From: contact at xavierho.com (Xavier Ho) Date: Mon, 6 Jul 2009 10:20:39 +1000 Subject: Why is my code faster with append() in a loop than with a large list? Message-ID: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> (Here's a short version of the long version below if you don't want to read:) Why is version B of the code faster than version A? (Only three lines different) Version A: http://pastebin.com/f14561243 Version B: http://pastebin.com/f1f657afc ------------------------------------------------ I was doing the problems on Project Euler for practice with Python last night. Problem 12 was to find the value of the first triangular number that has over 500 divisors. ========================================================================================= The sequence of triangle numbers is generated by adding the natural numbers. So the 7[image: ^(]th[image: )] triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: * 1*: 1 * 3*: 1,3 * 6*: 1,2,3,6 *10*: 1,2,5,10 *15*: 1,3,5,15 *21*: 1,3,7,21 *28*: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? ========================================================================================= My initial code was to loop through from 1 to half the number and see which were divisors, and as I find them I store them in a list. That would have taken days. My second try was factorising the number each time, and count the divisors using the powers of each factor, plus 1, and multiply together. The code is here (Version A): http://pastebin.com/f14561243 This worked, but it took overnight to compute. Before I went to bed a friend of mine caught me online, and apparently left me a working version under 8 seconds with only 3 line difference. The code is here (Version B): http://pastebin.com/f1f657afc That was amazing. But I have no idea why his edit makes it so much faster. I did a test to see whether if append() was faster (which I doubted) than defining a list with a large size to begin with, and I was right: http://pastebin.com/f4b46d0db Which shows that appending is 40x slower, and was expected. But I still can't puzzle out why his use of appending in Version B was so much faster than mine. Any insights would be welcome. I'm going on a family trip, though, so my replies may delay. Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From fetchinson at googlemail.com Sun Jul 5 20:23:49 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Sun, 5 Jul 2009 17:23:49 -0700 Subject: memoization module? In-Reply-To: References: Message-ID: > Is there a memoization module for Python? I'm looking for something > like Mark Jason Dominus' handy Memoize module for Perl. The Python Cookbook has several examples: http://www.google.com/search?q=python+memoize&sitesearch=code.activestate.com HTH, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From ldo at geek-central.gen.new_zealand Sun Jul 5 20:30:30 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 12:30:30 +1200 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Message-ID: In message , Charles Yeomans wrote: > On the contrary, MD5 was intended to be a cryptographic hash function, > not a checksum. Just like MD4 and MD2 before it. They have long since been considered worthless, and now MD5 has joined them. From Scott.Daniels at Acm.Org Sun Jul 5 20:30:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 05 Jul 2009 17:30:58 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <4A4FDCAF.8010209@Acm.Org> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: Scott David Daniels wrote: > ... Here's a heuristic replacement for my previous frequency code: > I've tried to mark where you could fudge numbers if the run time > is at all close. Boy, I cannot let go. I did a bit of a test checking for cost to calculated number of discovered samples, and found after: import timeit import numpy original = numpy.random.random(0, 100, (1000, 1000)).astype(int) data = original.flatten() data.sort() part = data[::100] t = timeit.Timer('sum(part[:-1]==part[1:])', 'from __main__ import part') v = timeit.Timer('len(part[part[:-1]==part[1:]])', 'from __main__ import part') I got: >>> t.repeat(3, 10) [0.58319842326318394, 0.57617574300638807, 0.57831819407238072] >>> v.repeat(3, 1000) [0.93933027801040225, 0.93704535073584339, 0.94096260837613954] So, len(part[mask]) is almost 50X faster! I checked: >>> sum(part[:-1]==part[1:]) 9393 >>> len(part[part[:-1]==part[1:]]) 9393 That's an awful lot of matches, so I with high selectivity: data = original.flatten() # no sorting, so runs missing part = data[::100] >>> t.repeat(3, 10) [0.58641335700485797, 0.58458854407490435, 0.58872594142576418] >>> v.repeat(3, 1000) [0.27352554584422251, 0.27375686015921019, 0.27433291102624935] about 200X faster >>> len(part[part[:-1]==part[1:]]) 39 >>> sum(part[:-1]==part[1:]) 39 So my new version of this (compressed) code: > ... > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) # count identified matches > while candidates > N * 10: # 10 -- heuristic > stride *= 2 # # heuristic increase > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > while candidates < N * 3: # heuristic slop for long runs > stride //= 2 # heuristic decrease > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > former = None > past = 0 > for value in sampled[matches]: > ... is: ... sampled = data[::stride] candidates = sampled[sampled[:-1] == sampled[1:]] while len(candidates) > N * 10: # 10 -- heuristic stride *= 2 # # heuristic increase sampled = data[::stride] candidates = sampled[sampled[:-1] == sampled[1:]] while len(candidates) < N * 3: # heuristic slop for long runs stride //= 2 # heuristic decrease sampled = data[::stride] candidates = sampled[sampled[:-1] == sampled[1:]] former = None past = 0 for value in candidates: ... This change is important, for we try several strides before settling on a choice, meaning the optimization can be valuable. This also means we could be pickier at choosing strides (try more values), since checking is cheaper than before. Summary: when dealing with numpy, (or any bulk <-> individual values transitions), try several ways that you think are equivalent and _measure_. In the OODB work I did we called this "impedance mismatch," and it is likely some boundary transitions are _much_ faster than others. The sum case is one of them; I am getting numpy booleans back, rather than numpy booleans, so conversions aren't going fastpath. --Scott David Daniels Scott.Daniels at Acm.Org From timr at probo.com Sun Jul 5 20:36:11 2009 From: timr at probo.com (Tim Roberts) Date: Sun, 05 Jul 2009 17:36:11 -0700 Subject: Python and webcam capture delay? References: Message-ID: "jack catcher (nick)" wrote: > >I'm thinking of using Python for capturing and showing live webcam >stream simultaneously between two computers via local area network. >Operating system is Windows. I'm going to begin with VideoCapture >extension, no ideas about other implementation yet. Do you have any >suggestions on how short delay I should hope to achieve in showing the >video? This would be part of a psychological experiment, so I would need >to deliver the video stream with a reasonable delay (say, below 100ms). You need to do the math on this. Remember that a full 640x480 RGB stream at 30 frames per second runs 28 megabytes per second. That's more than twice what a 100 megabit network can pump. You can probably use Python to oversee this, but you might want to consider using lower-level code to control the actual hardware. If you are targeting Windows, for example, you could write a DirectShow graph to pump into a renderer that transmits out to a network, then another graph to receive from the network and display it. You can manage the network latency by adding a delays in the local graph. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From david at pythontoo.com Sun Jul 5 20:48:39 2009 From: david at pythontoo.com (David) Date: Sun, 05 Jul 2009 20:48:39 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A5149E7.6030809@pythontoo.com> Xavier Ho wrote: > (Here's a short version of the long version below if you don't want to > read:) > > Why is version B of the code faster than version A? (Only three lines > different) > > Version A: http://pastebin.com/f14561243 > Version B: http://pastebin.com/f1f657afc I don't know but here is the diff for someone that may; 1c1,2 < # This one only took 8 seconds on my machine. Wow? --- > > # This one took hours to compute, overnight. 28c29 < powers = [0, 0] --- > powers = [0] * (num + 1) 32c33 < powers[factor-1] += 1 --- > powers[factor] += 1 35d35 < powers.append(0) 55c55 < n += 1 \ No newline at end of file --- > n += 1 -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From python at mrabarnett.plus.com Sun Jul 5 20:54:39 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 01:54:39 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A514B4F.6010408@mrabarnett.plus.com> Xavier Ho wrote: > (Here's a short version of the long version below if you don't want to > read:) > > Why is version B of the code faster than version A? (Only three lines > different) > > Version A: http://pastebin.com/f14561243 > Version B: http://pastebin.com/f1f657afc > > ------------------------------------------------ > > I was doing the problems on Project Euler for practice with Python last > night. Problem 12 was to find the value of the first triangular number > that has over 500 divisors. > ========================================================================================= > > The sequence of triangle numbers is generated by adding the natural > numbers. So the 7^(^th ) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 > + 7 = 28. The first ten terms would be: > > 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... > > Let us list the factors of the first seven triangle numbers: > > * 1*: 1 > * 3*: 1,3 > * 6*: 1,2,3,6 > *10*: 1,2,5,10 > *15*: 1,3,5,15 > *21*: 1,3,7,21 > *28*: 1,2,4,7,14,28 > > We can see that 28 is the first triangle number to have over five divisors. > > What is the value of the first triangle number to have over five hundred > divisors? > > ========================================================================================= > > My initial code was to loop through from 1 to half the number and see > which were divisors, and as I find them I store them in a list. That > would have taken days. > > My second try was factorising the number each time, and count the > divisors using the powers of each factor, plus 1, and multiply together. > The code is here (Version A): http://pastebin.com/f14561243 > > This worked, but it took overnight to compute. Before I went to bed a > friend of mine caught me online, and apparently left me a working > version under 8 seconds with only 3 line difference. > The code is here (Version B): http://pastebin.com/f1f657afc > > That was amazing. But I have no idea why his edit makes it so much > faster. I did a test to see whether if append() was faster (which I > doubted) than defining a list with a large size to begin with, and I was > right: > http://pastebin.com/f4b46d0db > Which shows that appending is 40x slower, and was expected. But I still > can't puzzle out why his use of appending in Version B was so much > faster than mine. > > Any insights would be welcome. I'm going on a family trip, though, so my > replies may delay. > In your version you're creating a list of (num + 1) elements, but in the other version the list is only as long as the largest factor. For example, for num=28, your version creates a list 29 elements long, but the other version creates one only 7 elements long. Also, 'the time needed to append an item to the list is "amortized constant"' (quoted from http://effbot.org/zone/python-list.htm). This means that your little speed test isn't representation of what's actually happening. From notvalid2 at sbcglobal.net Sun Jul 5 21:48:46 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Sun, 05 Jul 2009 18:48:46 -0700 Subject: Adding the Copy Property to a Simple Histogram Message-ID: The code below produces a text window 60hx20w with a scroll bar. The contents are something of a histogram of values from 0 to 255. If one tries to copy the contents, Windows doesn't allow it. What needs to be done to allow a copy and paste? def ShowHistogram(self): if not self.current_image: return if self.histogram: self.histogram.destroy() t = Toplevel( self.master ) t.title("Histogram") t.bind( '', self.DestroyHistogram ) text = Text( t, height=60, width=20 ) scroll = Scrollbar(t, command=text.yview) text.configure(yscrollcommand=scroll.set) text.pack(side=LEFT, fill='both', expand=1) scroll.pack(side=RIGHT, fill=Y) self.histogram = t hist = self.current_image.histogram() for i in range(len(hist)): msg = "%5d %6d\n" % (i,hist[i]) text.insert( END, msg ) -- W. eWatson (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Web Page: From tanner989 at hotmail.com Sun Jul 5 22:06:30 2009 From: tanner989 at hotmail.com (tanner barnes) Date: Sun, 5 Jul 2009 21:06:30 -0500 Subject: Help with Sockets. Message-ID: I am writing a program and in one section there is going to be a lobby with (for testing purposes) about 4 people in it. in the lobby there are two txtctrl's the first for entering your message and the second for displaying the message you and the other people in the lobby type. i am trying to figure out how to get all the text entrys from the users and display them in the second one. For clarification and example of what i want would be like yahoo or windows live messanger but for more than 2 people. Python version: 2.6 GUI toolkit: WxPython _________________________________________________________________ Hotmail? has ever-growing storage! Don?t worry about storage limits. http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage_062009 -------------- next part -------------- An HTML attachment was scrubbed... URL: From maymunbeyin at gmail.com Sun Jul 5 22:27:25 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 19:27:25 -0700 (PDT) Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> Hi Thank you so much for wonderful tips and suggestions. I also found a solution to dynamic naming of the instances(I think). It does not sound like a very secure method but since my application will be just processing data one way I think it might be alright. I will compare to the list and dictionary methods. globals()["Some_Instance_Name"] From ldo at geek-central.gen.new_zealand Sun Jul 5 22:32:46 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 14:32:46 +1200 Subject: A Bug By Any Other Name ... Message-ID: I wonder how many people have been tripped up by the fact that ++n and --n fail silently for numeric-valued n. From clp2 at rebertia.com Sun Jul 5 22:45:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 5 Jul 2009 19:45:39 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: <50697b2c0907051945x67f97094s370449fa6d05b449@mail.gmail.com> On Sun, Jul 5, 2009 at 7:32 PM, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ? ?++n > > and > > ? ?--n > > fail silently for numeric-valued n. Given that C-style for-loops are relatively infrequent in Python and are usually written using range() when they are needed, it's probably not that prevalent a problem. I suppose the lexer could be changed to make ++ and -- illegal... Cheers, Chris -- http://blog.rebertia.com From sajmikins at gmail.com Sun Jul 5 23:11:51 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sun, 5 Jul 2009 20:11:51 -0700 (PDT) Subject: Adding the Copy Property to a Simple Histogram References: Message-ID: On Jul 5, 9:48?pm, "W. eWatson" wrote: > The code below produces a text window 60hx20w with a scroll bar. The > contents are something of a histogram of values from 0 to 255. If one tries > to copy the contents, Windows doesn't allow it. What needs to be done to > allow a copy and paste? > > ? ? ?def ShowHistogram(self): > ? ? ? ? ?if not self.current_image: > ? ? ? ? ? ? ?return > > ? ? ? ? ?if self.histogram: > ? ? ? ? ? ? ?self.histogram.destroy() > > ? ? ? ? ?t = Toplevel( self.master ) > ? ? ? ? ?t.title("Histogram") > ? ? ? ? ?t.bind( '', self.DestroyHistogram ) > ? ? ? ? ?text = Text( t, height=60, width=20 ) > ? ? ? ? ?scroll = Scrollbar(t, command=text.yview) > ? ? ? ? ?text.configure(yscrollcommand=scroll.set) > ? ? ? ? ?text.pack(side=LEFT, fill='both', expand=1) > ? ? ? ? ?scroll.pack(side=RIGHT, fill=Y) > ? ? ? ? ?self.histogram = t > > ? ? ? ? ?hist = self.current_image.histogram() > ? ? ? ? ?for i in range(len(hist)): > ? ? ? ? ? ? ?msg = "%5d %6d\n" % (i,hist[i]) > ? ? ? ? ? ? ?text.insert( END, msg ) > -- > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? W. eWatson > > ? ? ? ? ? ? ? (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) > ? ? ? ? ? ? ? ?Obz Site: ?39? 15' 7" N, 121? 2' 32" W, 2700 feet > > ? ? ? ? ? ? ? ? ? ? ?Web Page: Do you mean that the Text widget doesn't let you copy-and-paste copy its contents using selection and ? That shouldn't have anything to do with the contents of the Text widget. From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 23:28:43 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 03:28:43 GMT Subject: A Bug By Any Other Name ... References: Message-ID: <006d4a50$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ++n > > and > > --n > > fail silently for numeric-valued n. What do you mean, "fail silently"? They do exactly what you should expect: >>> ++5 # positive of a positive number is positive 5 >>> --5 # negative of a negative number is positive 5 >>> -+5 # negative of a positive number is negative -5 So does the bitwise-not unary operator: >>> ~~5 5 I'm not sure what "bug" you're seeing. Perhaps it's your expectations that are buggy, not Python. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 23:28:55 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 03:28:55 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: <006d4a5c$0$9711$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 17:30:58 -0700, Scott David Daniels wrote: > Summary: when dealing with numpy, (or any bulk <-> individual values > transitions), try several ways that you think are equivalent and > _measure_. This advice is *much* more general than numpy -- it applies to any optimization exercise. People's intuitions about what's fast and what's slow are often very wrong. -- Steven From notvalid2 at sbcglobal.net Sun Jul 5 23:33:48 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Sun, 05 Jul 2009 20:33:48 -0700 Subject: Adding the Copy Property to a Simple Histogram In-Reply-To: References: Message-ID: Simon Forman wrote: > On Jul 5, 9:48 pm, "W. eWatson" wrote: >> The code below produces a text window 60hx20w with a scroll bar. The >> contents are something of a histogram of values from 0 to 255. If one tries >> to copy the contents, Windows doesn't allow it. What needs to be done to >> allow a copy and paste? >> >> def ShowHistogram(self): >> if not self.current_image: >> return >> >> if self.histogram: >> self.histogram.destroy() >> >> t = Toplevel( self.master ) >> t.title("Histogram") >> t.bind( '', self.DestroyHistogram ) >> text = Text( t, height=60, width=20 ) >> scroll = Scrollbar(t, command=text.yview) >> text.configure(yscrollcommand=scroll.set) >> text.pack(side=LEFT, fill='both', expand=1) >> scroll.pack(side=RIGHT, fill=Y) >> self.histogram = t >> >> hist = self.current_image.histogram() >> for i in range(len(hist)): >> msg = "%5d %6d\n" % (i,hist[i]) >> text.insert( END, msg ) >> -- >> W. eWatson >> >> (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) >> Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet >> >> Web Page: > > Do you mean that the Text widget doesn't let you copy-and-paste copy > its contents using selection and ? That shouldn't have > anything to do with the contents of the Text widget. Whoops, I missed it. I'm not quite sure what I did to make it seem like it doesn't copy, but it does. I fooled myself. All is well. -- W. eWatson (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Web Page: From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 23:46:41 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 03:46:41 GMT Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> Message-ID: <006d4e86$0$9711$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 19:27:25 -0700, kk wrote: > Hi > > Thank you so much for wonderful tips and suggestions. > > I also found a solution to dynamic naming of the instances(I think). It > does not sound like a very secure method but since my application will > be just processing data one way I think it might be alright. I will > compare to the list and dictionary methods. > > globals()["Some_Instance_Name"] You're fighting the computer instead of working with it. That's the Wrong Way to solve the problem -- you're doing more work than needed, for little or no benefit. My bet is, you have code that looks something like this: for i in range(N): # N comes from somewhere else # Create a new variable globals()["Some_Instance_Name%s" % i] = instance() # Do something with the variables for i in range(N): # Look up the variable x = globals()["Some_Instance_Name%s" % i] process(x) Am I close? That's the Wrong Way to do it -- you're using a screwdriver to hammer a nail. The right way to work with an unknown number of data elements is to put them in a list, and process each element in the list, not to try giving them all unique names. The only reason for using named variables is so you can use the name in source code: my_value = Some_Instance87 + Some_Instance126 But you can't do that, because you don't know how many instances there are, you don't know whether to write Some_Instance87 or Some_Instance125 or Some_Instance19. So instead, do something like this: instances = [] for i in range(N): # Create a new instance and store it for later instances.append( instance() ) # Later on: for x in instances(): process(x) -- Steven From dns4 at cornell.edu Sun Jul 5 23:53:56 2009 From: dns4 at cornell.edu (David Smith) Date: Sun, 05 Jul 2009 23:53:56 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> <7x4otsux7f.fsf@ruckus.brouhaha.com> Message-ID: kj wrote: > In <7x4otsux7f.fsf at ruckus.brouhaha.com> Paul Rubin writes: > >> kj writes: >>> sense = cmp(func(hi), func(lo)) >>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" > >> bisection search usually just requires the function to be continuous >> and to have its value cross the target somewhere between the endpoints, >> not be monotonic. > > Try the algorithm I posted with lo = -pi/4, hi = 2*pi, func = cos, > target = -1, and see what you get... > >>> I regard the very special case of func(hi)==func(lo)==target as >>> pathological (analogous to the fact that a stopped watch is "exactly >>> right" twice a day), and not one I care to support. > >> I do think you should support that case, under the "do 'nothing' >> gracefully" principle. > > You keep missing the point that this is an *internal* *helper* > *convenience* function, meant to abstract away common logic from > a handful of places and thus eliminate some code repetition within > a module. It is *not* a library function intended to be called > from elsewhere. So talk of "supporting" anything is besides the > point. Any internal use of this function that applies it to a > non-strictly-monotonic function is, by assumption, an error. > > kj First, let me say *I got the point*. I use asserts, but only in unit testing where I want to test the result of some action for correctness. In the course of programming product code, I personally don't think they should ever be used exactly for the reasons everyone else is pointing out. They can be disabled with the -O option and that changes the program's behavior in ways that could break in production. If you insist on teaching the assert statement, teach it in the context of writing unit testing code. Its an extremely valuable skill. --David From casevh at gmail.com Mon Jul 6 01:04:36 2009 From: casevh at gmail.com (casevh) Date: Sun, 5 Jul 2009 22:04:36 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 Message-ID: An alpha release of GMPY that supports Python 2 and 3 is available. GMPY is a wrapper for the GMP multiple-precision arithmetic library. The MPIR multiple-precision arithmetic library is also supported. GMPY is available for download from http://code.google.com/p/gmpy/ Support for Python 3 required many changes to the logic used to convert between different numerical types. The result type of some combinations has changed. For example, 'mpz' + 'float' now returns an 'mpf' instead of a 'float'. See the file "changes.txt" for more information. In addition to support for Python 3, there are several other changes and bug fixes: - Bug fixes in mpz.binary() and mpq.binary(). - Unicode strings are accepted as input on Python 2. (Known bug: works for mpz, fails for mpq and mpf) - The overhead for calling GMPY routines has been reduced. If one operand in a small integer, it is not converted to mpz. - 'mpf' and 'mpq' now support % and divmod. Comments on provided binaries The 32-bit Windows installers were compiled using MPIR 1.2.1 and will automatically recognize the CPU type and use code optimized for that CPU. Please test with your applications and report any issues found! casevh From gagsl-py2 at yahoo.com.ar Mon Jul 6 01:19:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 02:19:51 -0300 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano escribi?: > On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: > >> I wonder how many people have been tripped up by the fact that >> >> ++n >> >> and >> >> --n >> >> fail silently for numeric-valued n. > > What do you mean, "fail silently"? They do exactly what you should > expect: >>>> ++5 # positive of a positive number is positive > > I'm not sure what "bug" you're seeing. Perhaps it's your expectations > that are buggy, not Python. Well, those expectations are taken seriously when new features are introduced into the language - and sometimes the feature is dismissed just because it would be confusing for some. If a += 1 works, expecting ++a to have the same meaning is very reasonable (for those coming from languages with a ++ operator, like C or Java) - more when ++a is a perfectly valid expression. If this issue isn't listed under the various "Python gotchas" articles, it should... -- Gabriel Genellina From banibrata.dutta at gmail.com Mon Jul 6 01:37:19 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Mon, 6 Jul 2009 11:07:19 +0530 Subject: mail In-Reply-To: <22154.210.212.36.65.1246724472.squirrel@www.iisermohali.ac.in> References: <22154.210.212.36.65.1246724472.squirrel@www.iisermohali.ac.in> Message-ID: <3de8e1f70907052237m442e563bj4e040eec12d63f0d@mail.gmail.com> On Sat, Jul 4, 2009 at 9:51 PM, wrote: > Hi, > > I want to know that whether using python programming is it possible to > extract chemical shift information about some amino acids of some protein > from BMRB(BioMagResBank) or Ref-DB(referenced databank) or not. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > Without any real knowledge of the problem domain, but with keyword level google search, here's one one finds -- http://www.ccpn.ac.uk/api-documentation/ccpnmr/ccpnmr2.0/python/doc/api.html http://biopython.org/wiki/Main_Page http://chempython.org/ http://www.sschwarzer.net/ Also as Grant says, if you can create a software program in any language, you can potentially do it with Python as well. As Python is a "batteries included" language, chances are, the included-batteries would make your life easier. Of course mileage may vary. If you find existing modules that do much of what you want, you have a very good starting point. -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From maymunbeyin at gmail.com Mon Jul 6 01:50:36 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 22:50:36 -0700 (PDT) Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven, Before your post I was contemplating about the merits of using the globals(). After reading your post I am totally convinced that your suggestion that was also suggested by previous posters is the way to go. At first I thought it would be limiting to not to have the instance names properly setup, but now I understand it better. thank you all again. From gagsl-py2 at yahoo.com.ar Mon Jul 6 01:58:59 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 02:58:59 -0300 Subject: Does cProfile include IO wait time? References: Message-ID: En Sat, 04 Jul 2009 21:03:38 -0300, Matthew Wilson escribi?: > I expected to see a bunch of my IO file-reading code in there, but I > don't. So > this makes me think that the profiler uses CPU time, not > clock-on-the-wall time. > I'm not an expert on python profiling, and the docs seem sparse. Can I > rule out IO as the bottleneck here? How do I see the IO consequences? I don't know either - but it's easy to check. Write a program that just reads a lot from /dev/zero, and look at its profile. You should be able to tell whether I/O time is included or not. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 6 02:10:25 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 03:10:25 -0300 Subject: Help with Sockets. References: Message-ID: En Sun, 05 Jul 2009 23:06:30 -0300, tanner barnes escribi?: > I am writing a program and in one section there is going to be a lobby > with (for testing purposes) about 4 people in it. in the lobby there are > two txtctrl's the first for entering your message and the second for > displaying the message you and the other people in the lobby type. i am > trying to figure out how to get all the text entrys from the users and > display them in the second one. For clarification and example of what i > want would be like yahoo or windows live messanger but for more than 2 > people. Like a chat room, IRC? It's easy to do using a client-server architecture. Make all the clients connect to a central server. Any time someone writes some text, the client sends it to the server (but does not display it). The server just receives text from any client, and sends the received messages to all connected clients (including the one that sent it originally). Any book on socket programming will help; there are a few specific for Python. You may start with the "echo" example in the Python documentation. Make the networking part work first, then add the wx GUI if you want. -- Gabriel Genellina From nomail at thank.you Mon Jul 6 02:10:38 2009 From: nomail at thank.you (jack catcher (nick)) Date: Mon, 06 Jul 2009 09:10:38 +0300 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: Tim Roberts kirjoitti: > "jack catcher (nick)" wrote: >> I'm thinking of using Python for capturing and showing live webcam >> stream simultaneously between two computers via local area network. >> Operating system is Windows. I'm going to begin with VideoCapture >> extension, no ideas about other implementation yet. Do you have any >> suggestions on how short delay I should hope to achieve in showing the >> video? This would be part of a psychological experiment, so I would need >> to deliver the video stream with a reasonable delay (say, below 100ms). > > You need to do the math on this. Remember that a full 640x480 RGB stream > at 30 frames per second runs 28 megabytes per second. That's more than > twice what a 100 megabit network can pump. > > You can probably use Python to oversee this, but you might want to consider > using lower-level code to control the actual hardware. If you are > targeting Windows, for example, you could write a DirectShow graph to pump > into a renderer that transmits out to a network, then another graph to > receive from the network and display it. > > You can manage the network latency by adding a delays in the local graph. Thanks Tim, you're correct about the math. What is your main point about DirectShow: that it is generally faster and more reliable than doing the job high-level, or that one could use coding/decoding in DirectShow to speed up the transmission? I think the latter would be a great idea if the latency were tolerable. On the other hand, I'd like to keep things simple and do all the programming in Python. I've got no experience with DirectShow, but I guess the filters need to be programmed in C++ and called from Python? Another option might be to use resolution 320x240 at 15fps. From gherron at islandtraining.com Mon Jul 6 02:33:36 2009 From: gherron at islandtraining.com (Gary Herron) Date: Sun, 05 Jul 2009 23:33:36 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A519AC0.1050905@islandtraining.com> Gabriel Genellina wrote: > En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano > escribi?: >> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >> >>> I wonder how many people have been tripped up by the fact that >>> >>> ++n >>> >>> and >>> >>> --n >>> >>> fail silently for numeric-valued n. >> >> What do you mean, "fail silently"? They do exactly what you should >> expect: >>>>> ++5 # positive of a positive number is positive >> >> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >> that are buggy, not Python. > > Well, those expectations are taken seriously when new features are > introduced into the language - and sometimes the feature is dismissed > just because it would be confusing for some. > If a += 1 works, expecting ++a to have the same meaning is very > reasonable (for those coming from languages with a ++ operator, like C > or Java) - more when ++a is a perfectly valid expression. > If this issue isn't listed under the various "Python gotchas" > articles, it should... Well sure, it's not unreasonable to expect ++n and --n to behave as in other languages, and since they don't, perhaps they should be listed as a "Python gotcha". But even so, it's quite arrogant of the OP to flaunt his ignorance of the language by claiming this is a bug and a failure. It shouldn't have been all that hard for him to figure out what was really happening. Gary Herron From __peter__ at web.de Mon Jul 6 02:33:53 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 06 Jul 2009 08:33:53 +0200 Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: Scott David Daniels wrote: > Scott David Daniels wrote: > t = timeit.Timer('sum(part[:-1]==part[1:])', > 'from __main__ import part') What happens if you calculate the sum in numpy? Try t = timeit.Timer('(part[:-1]==part[1:]).sum()', 'from __main__ import part') Peter From gagsl-py2 at yahoo.com.ar Mon Jul 6 02:46:33 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 03:46:33 -0300 Subject: Method to separate unit-test methods and data? References: Message-ID: En Sun, 05 Jul 2009 15:48:06 -0300, Nick Daly escribi?: > [test_Midpoint_mid] > none_values = ((-1, None), > ? ? ? ? ? ? ? (None, -12.8)) > > What I haven't yet figured out how to do though, is properly override > the default class member values with values from the config file. ?The > config file's data is loaded as a string instead of as a list, as I'd > want. ?This causes all the tests to fail, as while none_values needs > to be interpreted as a list, it is instead understood as: > > " ((-1, None),\n ? ? ? ? ? ? ? (None, -12.8))" > > Does anyone have any solutions for these problems? ? You may use a .py file to configure it, instead of your .cfg > First, is there a > known and simple way to separate unit-test data and methods into > separate files? ? Just write them in separate files? I didn't quite understand the question... > Secondly, if not, is there a simple way to convert > strings into other base types, like lists, dictionaries, and so forth? eval(), but I'd just use a .py file People usually warns against using eval on arbitrary strings, or on user-supplied data, but in this case it is not worse than importing/executing the module. > Or, am I going to have to write my own list-parsing methods? ?Would > pickling help? ?I haven't yet had a chance to look into if or how that > would work... ?If there's anything else I can clarify about this > request, feel free to let me know. Another alternative would be a .csv file; I use them when the test data comes from other parties, like a manufacturer's data sheet. I've never used a pickle to store test data - how do you generate the pickle contents in the first place? Usually I'd just use *that* code directly, but I think pickling the resulting objects would be OK is the process to regenerate them takes a lot of time. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 6 02:53:57 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 03:53:57 -0300 Subject: multiprocessing and freezing on Windows References: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Message-ID: En Sat, 04 Jul 2009 22:15:43 -0300, SK escribi?: > To add a bit more information, I found that I needed to patch > get_command_line in multiprocessing/forking.py [...] > > Is packaging with multiprocessing supposed to be this hard? If so, > some documentation is needed. Shouldn't be so hard, I presume. You may want to post your comments and upload the patch to http://bugs.python.org -- Gabriel Genellina From martin at librador.com Mon Jul 6 03:44:33 2009 From: martin at librador.com (Martin Vilcans) Date: Mon, 6 Jul 2009 09:44:33 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. ?I'd love your opinion about > it. In general, code clarity is more important than reusability. Unfortunately, many novice programmers have the opposite impression. I have seen too much convoluted code written by beginners who try to make the code generic. Writing simple, clear, to-the-point code is hard enough as it is, even when not aiming at making it reusable. If in the future you see an opportunity to reuse the code, then and only then is the time to make it generic. YAGNI is a wonderful principle. -- martin at librador.com http://www.librador.com From gagsl-py2 at yahoo.com.ar Mon Jul 6 03:49:28 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 04:49:28 -0300 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: En Mon, 06 Jul 2009 03:33:36 -0300, Gary Herron escribi?: > Gabriel Genellina wrote: >> En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano >> escribi?: >>> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >>> >>>> I wonder how many people have been tripped up by the fact that >>>> >>>> ++n >>>> >>>> and >>>> >>>> --n >>>> >>>> fail silently for numeric-valued n. >>> >>> What do you mean, "fail silently"? They do exactly what you should >>> expect: >>>>>> ++5 # positive of a positive number is positive >>> >>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>> that are buggy, not Python. >> >> Well, those expectations are taken seriously when new features are >> introduced into the language - and sometimes the feature is dismissed >> just because it would be confusing for some. >> If a += 1 works, expecting ++a to have the same meaning is very >> reasonable (for those coming from languages with a ++ operator, like C >> or Java) - more when ++a is a perfectly valid expression. >> If this issue isn't listed under the various "Python gotchas" articles, >> it should... > > Well sure, it's not unreasonable to expect ++n and --n to behave as in > other languages, and since they don't, perhaps they should be listed as > a "Python gotcha". But even so, it's quite arrogant of the OP to flaunt > his ignorance of the language by claiming this is a bug and a failure. > It shouldn't have been all that hard for him to figure out what was > really happening. That depends on what you call a "bug". In his classical book "The art of software testing", Myers says that a program has a bug when it doesn't perform as the user expects reasonably it to do (not an exact quote, I don't have the book at hand). That's a lot broader than developers like to accept. In this case, a note in the documentation warning about the potential confusion would be fine. -- Gabriel Genellina From andreengels at gmail.com Mon Jul 6 03:51:04 2009 From: andreengels at gmail.com (Andre Engels) Date: Mon, 6 Jul 2009 09:51:04 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <6faf39c90907060051n65089c88od45c75c80f3a7ccf@mail.gmail.com> On Mon, Jul 6, 2009 at 9:44 AM, Martin Vilcans wrote: > On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: >> I'm will be teaching a programming class to novices, and I've run >> into a clear conflict between two of the principles I'd like to >> teach: code clarity vs. code reuse. ?I'd love your opinion about >> it. > > In general, code clarity is more important than reusability. > Unfortunately, many novice programmers have the opposite impression. I > have seen too much convoluted code written by beginners who try to > make the code generic. Writing simple, clear, to-the-point code is > hard enough as it is, even when not aiming at making it reusable. > > If in the future you see an opportunity to reuse the code, then and > only then is the time to make it generic. Not just that, when you actually get to that point, making simple and clear code generic is often easier than making complicated-and-supposedly-generic code that little bit more generic that you need. -- Andr? Engels, andreengels at gmail.com From mail at timgolden.me.uk Mon Jul 6 03:56:20 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 06 Jul 2009 08:56:20 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <4A51AE24.9080608@timgolden.me.uk> Gabriel Genellina wrote: [... re confusion over ++n etc ...] > In this case, a note in the documentation warning about the potential > confusion would be fine. The difficulty here is knowing where to put such a warning. You obviously can't put it against the "++" operator as such because... there isn't one. You could put it against the unary plus operator, but who's going to look there? :) I've wondered for a while whether it wouldn't be a good move to include the official (or any other) Python FAQ into the standard docs set. If we did, that would be the obvious place for this piece of documentation, seems to me. TJG From stef.mientki at gmail.com Mon Jul 6 04:05:32 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Mon, 06 Jul 2009 10:05:32 +0200 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: <4A51B04C.2080207@gmail.com> jack catcher (nick) wrote: > Hi, > > I'm thinking of using Python for capturing and showing live webcam > stream simultaneously between two computers via local area network. > Operating system is Windows. I'm going to begin with VideoCapture > extension, no ideas about other implementation yet. Do you have any > suggestions on how short delay I should hope to achieve in showing the > video? This would be part of a psychological experiment, so I would > need to deliver the video stream with a reasonable delay (say, below > 100ms). I would first check if video capture extension works anyway. I couldn't get it working, ... ... it seems to start ok ... but moving the captured window, ... or sometimes even just moving the mouse hangs the program :-( So I'm still looking for a good / open source workiing video capture in Python. I can make a good working capture in delphi, make a DLL or ActiveX from that and use it in Python, but I'm not allowed to ditribute these. cheers, Stef From serverin2000 at yahoo.com Mon Jul 6 04:11:27 2009 From: serverin2000 at yahoo.com (RAM) Date: Mon, 6 Jul 2009 01:11:27 -0700 (PDT) Subject: generation of keyboard events References: Message-ID: On 5 July, 17:12, Tim Harig wrote: > On 2009-07-05, RAM wrote: > > > I need to start an external program and pass the keyboard events like > > F1,Right arrow key etc to the program..I am trying to use the > > subprocess module to invoke the external program. I am able to invoke > > but not able to generate the keyboard events and pass them on to the > > catb.org/esr/faqs/smart-questions.html > > You have told us nothing about the environment where you are trying to > accomplish this. ?GUI, CLI, Unix, Windows, etc? So I suggest that you > checkout the curses getch functions. ?You can find them in the standard > library documentation athttp://docs.python.org. ?You should also reference > documentation for the C version in your systems man pages. Hi Tim, I am trying to do this on windows. My program(executable) has been written in VC++ and when I run this program, I need to click on one button on the program GUI i,e just I am entering "Enter key" on the key board. But this needs manual process. So i need to write a python script which invokes my program and pass "Enter key" event to my program so that it runs without manual intervention. Thank in advance for the help. regards Sreerama V From david.m.cooke at gmail.com Mon Jul 6 04:16:44 2009 From: david.m.cooke at gmail.com (David M. Cooke) Date: Mon, 6 Jul 2009 08:16:44 +0000 (UTC) Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <4A50FDC6.1040604@v.loewis.de> Message-ID: Martin v. L?wis v.loewis.de> writes: > > This is a good test for Python implementation bottlenecks. Run > > that tokenizer on HTML, and see where the time goes. > > I looked at it with cProfile, and the top function that comes up > for a larger document (52k) is > ...validator.HTMLConformanceChecker.__iter__. [...] > With this simple optimization, I get a 20% speedup on my test > case. In my document, there are no attributes - the same changes > should be made to attribute validation routines. > > I don't think this has anything to do with the case statement. I agree. I ran cProfile over just the tokenizer step; essentially tokenizer = html5lib.tokenizer.HTMLStream(htmldata) for tok in tokenizer: pass It mostly *isn't* tokenizer.py that's taking the most time, it's inputstream.py. (There is one exception: tokenizer.py:HTMLStream.__init__ constructs a dictionary of states each time -- this is unnecessary, replace all expressions like self.states["attributeName"] with self.attributeNameState.) I've done several optimisations -- I'll upload the patch to the html5lib issue tracker. In particular, * The .position property of EncodingBytes is used a lot. Every self.position +=1 calls getPosition() and setPosition(). Another getPosition() call is done in the self.currentByte property. Most of these can be optimised away by using methods that move the position and return the current byte. * In HTMLInputStream, the current line number and column are updated every time a new character is read with .char(). The current position is *only* used in error reporting, so I reworked it to only calculate the position when .position() is called, by keeping track of the number of lines in previous read chunks, and computing the number of lines to the current offset in the current chunk. These give me about a 20% speedup. This just illustrates that the first step in optimisation is profiling :D As other posters have said, slurping the whole document into memory and using a regexp-based parser (such as pyparsing) would likely give you the largest speedups. If you want to keep the chunk- based approach, you can still use regexp's, but you'd have to think about matching on chunk boundaries. One way would be to guarantee a minimum number of characters available, say 10 or 50 (unless end-of-file, of course) -- long enough such that any *constant* string you'd want to match like From ldo at geek-central.gen.new_zealand Mon Jul 6 04:29:04 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 20:29:04 +1200 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: In message , Tim Golden wrote: > The difficulty here is knowing where to put such a warning. > You obviously can't put it against the "++" operator as such > because... there isn't one. This bug is an epiphenomenon. :) From wuwei23 at gmail.com Mon Jul 6 04:30:03 2009 From: wuwei23 at gmail.com (alex23) Date: Mon, 6 Jul 2009 01:30:03 -0700 (PDT) Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <19f3d19d-c4bc-4f94-8387-febe624245a2@p18g2000pra.googlegroups.com> On Jul 6, 5:56?pm, Tim Golden wrote: > Gabriel Genellina wrote: > > In this case, a note in the documentation warning about the potential > > confusion would be fine. > > The difficulty here is knowing where to put such a warning. > You obviously can't put it against the "++" operator as such > because... there isn't one. You could put it against the unary > plus operator, but who's going to look there? :) The problem is: where do you stop? If you're going to add something to the documentation to address every expectation someone might hold coming from another language, the docs are going to get pretty big. I think a language should be intuitive within itself, but not be required to be intuitable based on _other_ languages (unless, of course, that's an objective of the language). If I expect something in language-A to operate the same way as completely-unrelated-language-B, I'd see that as a failing on my behalf, especially if I hadn't read language-A's documentation first. I'm not adverse to one language being _explained_ in terms of another, but would much prefer to see those relegated to "Python for programmers" articles rather than in the main docs themselves. From ldo at geek-central.gen.new_zealand Mon Jul 6 04:32:34 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 20:32:34 +1200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: In message <4a4f91f9$0$1587$742ec2ed at news.sonic.net>, John Nagle wrote: > ("It should be written in C" is not an acceptable answer.) I don't see why not. State machines that have to process input byte by byte are well known to be impossible to implement efficiently in high-level languages. That's why lex/flex isn't worth using. Even GCC has been moving to hand-coded parsers, first the lexical analyzer, and more recently even for the syntax parser (getting rid of yacc/bison), certainly for C. From clp2 at rebertia.com Mon Jul 6 04:32:51 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 01:32:51 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <50697b2c0907060132w592ce54dt3c9392b764639bb2@mail.gmail.com> On Mon, Jul 6, 2009 at 1:29 AM, Lawrence D'Oliveiro wrote: > In message , Tim Golden > wrote: > >> The difficulty here is knowing where to put such a warning. >> You obviously can't put it against the "++" operator as such >> because... there isn't one. > > This bug is an epiphenomenon. :) Well, like I suggested, it /could/ be made an operator (or rather, a lexer token) which just causes a compile/parse error. Cheers, Chris -- http://blog.rebertia.com From simon at brunningonline.net Mon Jul 6 05:02:08 2009 From: simon at brunningonline.net (Simon Brunning) Date: Mon, 6 Jul 2009 10:02:08 +0100 Subject: generation of keyboard events In-Reply-To: References: Message-ID: <8c7f10c60907060202v60c7ea0ct51749bbaf0f81bb6@mail.gmail.com> 2009/7/6 RAM : > I am trying to do this on windows. My program(executable) has been > written in VC++ and when I run this program, I need to click on one > button on the program GUI i,e just I am entering "Enter key" on the > key board. But this needs manual process. So i need to write a python > script which invokes my program and pass "Enter key" event to my > program so that it runs without manual intervention. Try . -- Cheers, Simon B. From sjmachin at lexicon.net Mon Jul 6 05:15:57 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 6 Jul 2009 02:15:57 -0700 (PDT) Subject: A Bug By Any Other Name ... References: Message-ID: <2e02da2c-28d5-4ce5-9aa0-2827ba7110d3@12g2000pri.googlegroups.com> On Jul 6, 12:32?pm, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ? ? ++n > > and > > ? ? --n > > fail silently for numeric-valued n. What fail? In Python, ++n and --n are fatuous expressions which SUCCEED silently except for rare circiumstances e.g. --n will cause an overflow exception on older CPython versions if isinstance(n, int) and n == -sys.maxint - 1. From vilya.harvey at gmail.com Mon Jul 6 05:21:07 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Mon, 6 Jul 2009 10:21:07 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <6aef848f0907060221t6a44e10bga5a7798855779b5@mail.gmail.com> 2009/7/6 Xavier Ho : > Why is version B of the code faster than version A? (Only three lines > different) Here's a guess: As the number you're testing gets larger, version A is creating very big list. I'm not sure exactly how much overhead each list entry has in python, but I guess it's at least 8 bytes: a 32-bit reference for each list entry, and 32 bits to hold the int value (assuming a 32-bit version of python). The solution you're looking for is a large 8 digit number; let's say 80,000,000, for the sake of easy calculation. That means, as you get close to the solution, you'll be trying to allocate almost 640 Mb of memory for every number you're checking. That's going to make the garbage collector work extremely hard. Also, depending on how much memory your computer has free, you'll probably start hitting virtual memory too, which will slow you down even further. Finally, the reduce step has to process all 80,000,000 elements which is clearly going to take a while. Version b creates a list which is only as long as the largest prime factor, so at worst the list size will be approx. sqrt(80,000,000), which is approx. 8900 elements or approx. 72 Kb or memory - a much more manageable size. Hope that helps, Vil. From tjreedy at udel.edu Mon Jul 6 05:36:27 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 05:36:27 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: Gabriel Genellina wrote: > > In this case, a note in the documentation warning about the potential > confusion would be fine. How would that help someone who does not read the doc? From mooniitk at gmail.com Mon Jul 6 05:55:26 2009 From: mooniitk at gmail.com (mayank gupta) Date: Mon, 6 Jul 2009 11:55:26 +0200 Subject: Tree structure consuming lot of memory Message-ID: Hi, I am creating a tree data-structure in python; with nodes of the tree created by a simple class : class Node : def __init__(self , .... other attributes): # initialise the attributes here!! But the problem is I am working with a huge tree (millions of nodes); and each node is consuming much more memory than it should. After a little analysis, I found out that in general it uses about 1.4 kb of memory for each node!! I will be grateful if someone could help me optimize the memory usage. Thanks. Regards, Mayank -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 05:58:21 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 09:58:21 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006da5a2$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 02:19:51 -0300, Gabriel Genellina wrote: > En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano > escribi?: >> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >> >>> I wonder how many people have been tripped up by the fact that >>> >>> ++n >>> >>> and >>> >>> --n >>> >>> fail silently for numeric-valued n. >> >> What do you mean, "fail silently"? They do exactly what you should >> expect: >>>>> ++5 # positive of a positive number is positive >> >> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >> that are buggy, not Python. > > Well, those expectations are taken seriously when new features are > introduced into the language - and sometimes the feature is dismissed > just because it would be confusing for some. If a += 1 works, expecting > ++a to have the same meaning is very reasonable (for those coming from > languages with a ++ operator, like C or Java) - more when ++a is a > perfectly valid expression. If this issue isn't listed under the various > "Python gotchas" articles, it should... The fact that it isn't suggests strongly to me that it isn't that common a surprise even for Java and C programmers. This is the first time I've seen anyone raise it as an issue. There are plenty of other languages other than Java and C. If we start listing every feature of Python that's different from some other language, we'll never end. For what it's worth, Ruby appears to behave the same as Python: $ irb irb(main):001:0> n = 5 => 5 irb(main):002:0> ++n => 5 irb(main):003:0> --n => 5 irb(main):004:0> -+n => -5 -- Steven From clp2 at rebertia.com Mon Jul 6 06:03:00 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 03:03:00 -0700 Subject: Tree structure consuming lot of memory In-Reply-To: References: Message-ID: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> On Mon, Jul 6, 2009 at 2:55 AM, mayank gupta wrote: > Hi, > > I am creating a tree data-structure in python; with nodes of the tree > created by a simple class : > > class Node : > ?????? def __init__(self , .... other attributes): > ????????????? # initialise the attributes here!! > > But the problem is I am working with a huge tree (millions of nodes); and > each node is consuming much more memory than it should. After a little > analysis, I found out that in general it uses about 1.4 kb of memory for > each node!! > I will be grateful if someone could help me optimize the memory usage. (1) Use __slots__ (see http://docs.python.org/reference/datamodel.html#slots) (2) Use some data structure other than a tree (3) Rewrite your Node/Tree implementation in C Cheers, Chris -- http://blog.rebertia.com From davea at dejaviewphoto.com Mon Jul 6 06:09:46 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Mon, 06 Jul 2009 06:09:46 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A51CD6A.2010106@dejaviewphoto.com> Xavier Ho wrote: > (Here's a short version of the long version below if you don't want to > read:) > > Why is version B of the code faster than version A? (Only three lines > different) > > Version A: http://pastebin.com/f14561243 > Version B: http://pastebin.com/f1f657afc > > ------------------------------------------------ > > I was doing the problems on Project Euler for practice with Python last > night. Problem 12 was to find the value of the first triangular number that > has over 500 divisors. > ========================================================================================= > > The sequence of triangle numbers is generated by adding the natural numbers. > So the 7[image: ^(]th[image: )] triangle number would be 1 + 2 + 3 + 4 + 5 + > 6 + 7 = 28. The first ten terms would be: > > 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... > > Let us list the factors of the first seven triangle numbers: > > * 1*: 1 > * 3*: 1,3 > * 6*: 1,2,3,6 > *10*: 1,2,5,10 > *15*: 1,3,5,15 > *21*: 1,3,7,21 > *28*: 1,2,4,7,14,28 > > We can see that 28 is the first triangle number to have over five divisors. > > What is the value of the first triangle number to have over five hundred > divisors? > ========================================================================================= > > My initial code was to loop through from 1 to half the number and see which > were divisors, and as I find them I store them in a list. That would have > taken days. > > My second try was factorising the number each time, and count the divisors > using the powers of each factor, plus 1, and multiply together. > The code is here (Version A): http://pastebin.com/f14561243 > > This worked, but it took overnight to compute. Before I went to bed a friend > of mine caught me online, and apparently left me a working version under 8 > seconds with only 3 line difference. > The code is here (Version B): http://pastebin.com/f1f657afc > > That was amazing. But I have no idea why his edit makes it so much faster. I > did a test to see whether if append() was faster (which I doubted) than > defining a list with a large size to begin with, and I was right: > http://pastebin.com/f4b46d0db > Which shows that appending is 40x slower, and was expected. But I still > can't puzzle out why his use of appending in Version B was so much faster > than mine. > > Any insights would be welcome. I'm going on a family trip, though, so my > replies may delay. > > Best regards, > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > Just by inspection, it would seem the bottleneck in your first version is that you return a huge list of nearly all zeroes, from factorize(). This slows down countDivisors() a great deal. It would probably save some time to not bother storing the zeroes in the list at all. And it should help if you were to step through a list of primes, rather than trying every possible int. Or at least constrain yourself to odd numbers (after the initial case of 2). DaveA From mooniitk at gmail.com Mon Jul 6 06:12:06 2009 From: mooniitk at gmail.com (mayank gupta) Date: Mon, 6 Jul 2009 12:12:06 +0200 Subject: Tree structure consuming lot of memory In-Reply-To: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> References: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> Message-ID: Thanks for the other possibilites. I would consider option (2) and (3) to improve my code. But out of curiosity, I would still like to know why does an object of a Python-class consume "so" much of memory (1.4 kb), and this memory usage has nothing to do with its attributes. Thanks Regards. On Mon, Jul 6, 2009 at 12:03 PM, Chris Rebert wrote: > On Mon, Jul 6, 2009 at 2:55 AM, mayank gupta wrote: > > Hi, > > > > I am creating a tree data-structure in python; with nodes of the tree > > created by a simple class : > > > > class Node : > > def __init__(self , .... other attributes): > > # initialise the attributes here!! > > > > But the problem is I am working with a huge tree (millions of nodes); and > > each node is consuming much more memory than it should. After a little > > analysis, I found out that in general it uses about 1.4 kb of memory for > > each node!! > > I will be grateful if someone could help me optimize the memory usage. > > (1) Use __slots__ (see > http://docs.python.org/reference/datamodel.html#slots) > (2) Use some data structure other than a tree > (3) Rewrite your Node/Tree implementation in C > > Cheers, > Chris > -- > http://blog.rebertia.com > -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.f.moore at gmail.com Mon Jul 6 08:05:02 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 6 Jul 2009 13:05:02 +0100 Subject: Opening a SQLite database in readonly mode Message-ID: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> The SQLite documentation mentions a flag, SQLITE_OPEN_READONLY, to open a database read only. I can't find any equivalent documented in the Python standard library documentation for the sqlite3 module (or, for that matter, on the pysqlite library's website). Is it possible to open a sqlite database in readonly mode, in Python? Thanks, Paul. From mail at microcorp.co.za Mon Jul 6 08:12:42 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 6 Jul 2009 14:12:42 +0200 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <00df01c9fe33$10c36d80$0d00a8c0@Hendrik> "Terry Reedy" wrote: > Gabriel Genellina wrote: > > > > In this case, a note in the documentation warning about the potential > > confusion would be fine. > > How would that help someone who does not read the doc? It obviously won't. All it will do, is that it will enable people on this group, who may read the manual, to tell people who complain, to RTFM. I agree that it would be a good idea to make it an error, or a warning - "this might not do what you think it does", or an "are you sure?" exception. :-) - Hendrik From contact at xavierho.com Mon Jul 6 08:12:58 2009 From: contact at xavierho.com (Xavier Ho) Date: Mon, 6 Jul 2009 20:12:58 +0800 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A51CD6A.2010106@dejaviewphoto.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> Message-ID: <2d56febf0907060512m794c2468sfa01cf332df3167a@mail.gmail.com> Thanks for the response all, I finally got my 'net working on the mountains, and I think your reasons are quite sound. I'll keep that in mind for the future. Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Mon, Jul 6, 2009 at 6:09 PM, Dave Angel wrote: > Xavier Ho wrote: > >> (Here's a short version of the long version below if you don't want to >> read:) >> >> Why is version B of the code faster than version A? (Only three lines >> different) >> >> Version A: http://pastebin.com/f14561243 >> Version B: http://pastebin.com/f1f657afc >> >> ------------------------------------------------ >> >> I was doing the problems on Project Euler for practice with Python last >> night. Problem 12 was to find the value of the first triangular number >> that >> has over 500 divisors. >> >> ========================================================================================= >> >> The sequence of triangle numbers is generated by adding the natural >> numbers. >> So the 7[image: ^(]th[image: )] triangle number would be 1 + 2 + 3 + 4 + 5 >> + >> >> 6 + 7 = 28. The first ten terms would be: >> >> 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... >> >> Let us list the factors of the first seven triangle numbers: >> >> * 1*: 1 >> * 3*: 1,3 >> * 6*: 1,2,3,6 >> *10*: 1,2,5,10 >> *15*: 1,3,5,15 >> *21*: 1,3,7,21 >> *28*: 1,2,4,7,14,28 >> >> We can see that 28 is the first triangle number to have over five >> divisors. >> >> What is the value of the first triangle number to have over five hundred >> divisors? >> >> ========================================================================================= >> >> My initial code was to loop through from 1 to half the number and see >> which >> were divisors, and as I find them I store them in a list. That would have >> taken days. >> >> My second try was factorising the number each time, and count the divisors >> using the powers of each factor, plus 1, and multiply together. >> The code is here (Version A): http://pastebin.com/f14561243 >> >> This worked, but it took overnight to compute. Before I went to bed a >> friend >> of mine caught me online, and apparently left me a working version under 8 >> seconds with only 3 line difference. >> The code is here (Version B): http://pastebin.com/f1f657afc >> >> That was amazing. But I have no idea why his edit makes it so much faster. >> I >> did a test to see whether if append() was faster (which I doubted) than >> defining a list with a large size to begin with, and I was right: >> http://pastebin.com/f4b46d0db >> Which shows that appending is 40x slower, and was expected. But I still >> can't puzzle out why his use of appending in Version B was so much faster >> than mine. >> >> Any insights would be welcome. I'm going on a family trip, though, so my >> replies may delay. >> >> Best regards, >> >> Ching-Yun "Xavier" Ho, Technical Artist >> >> Contact Information >> Mobile: (+61) 04 3335 4748 >> Skype ID: SpaXe85 >> Email: contact at xavierho.com >> Website: http://xavierho.com/ >> >> >> > Just by inspection, it would seem the bottleneck in your first version is > that you return a huge list of nearly all zeroes, from factorize(). This > slows down countDivisors() a great deal. > > It would probably save some time to not bother storing the zeroes in the > list at all. And it should help if you were to step through a list of > primes, rather than trying every possible int. Or at least constrain > yourself to odd numbers (after the initial case of 2). > > DaveA > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pm567426 at gmail.com Mon Jul 6 08:24:43 2009 From: pm567426 at gmail.com (Pedram) Date: Mon, 6 Jul 2009 05:24:43 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: OK, fine, I read longobject.c at last! :) I found that longobject is a structure like this: struct _longobject { struct _object *_ob_next; struct _object *_ob_prev; Py_ssize_t ob_refcnt; struct _typeobject *ob_type; digit ob_digit[1]; } And a digit is a 15-item array of C's unsigned short integers. Am I right? Or I missed something! Is this structure is constant in all environments (Linux, Windows, Mobiles, etc.)? From jeanmichel at sequans.com Mon Jul 6 08:32:10 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 06 Jul 2009 14:32:10 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: <4A51EECA.10509@sequans.com> kj wrote: > I've rewritten it like this: > > sense = cmp(func(hi), func(lo)) > assert sense != 0, "func is not strictly monotonic in [lo, hi]" > > Thanks for your feedback! > > kj > As already said before, unlike other languages, sense in english does **not** mean direction. You should rewrite this part using a better name. Wrong informations are far worse than no information at all. JM From Scott.Daniels at Acm.Org Mon Jul 6 08:33:02 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 05:33:02 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: Andre Engels wrote: > On Mon, Jul 6, 2009 at 9:44 AM, Martin Vilcans wrote: >> On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: >>> I'm will be teaching a programming class to novices, and I've run >>> into a clear conflict between two of the principles I'd like to >>> teach: code clarity vs. code reuse. I'd love your opinion about >>> it. >> In general, code clarity is more important than reusability. >> Unfortunately, many novice programmers have the opposite impression. I >> have seen too much convoluted code written by beginners who try to >> make the code generic. Writing simple, clear, to-the-point code is >> hard enough as it is, even when not aiming at making it reusable. >> >> If in the future you see an opportunity to reuse the code, then and >> only then is the time to make it generic. > > Not just that, when you actually get to that point, making simple and > clear code generic is often easier than making > complicated-and-supposedly-generic code that little bit more generic > that you need. First, a quote which took me a bit to find: Thomas William K?rner paraphrasing Polya and Svego in A Companion to Analysis: Recalling that 'once is a trick, twice is a method, thrice is a theorem, and four times a theory,' we seek to codify this insight. Let us apply this insight: Suppose in writing code, we pretty much go with that. A method is something you notice, a theorem is a function, and a theory is a generalized function. Even though we like DRY ("don't repeat yourself") as a maxim, let it go the first time and wait until you see the pattern (a possible function). I'd go with a function first, a pair of functions, and only then look to abstracting the function. --Scott David Daniels Scott.Daniels at Acm.Org From Scott.Daniels at Acm.Org Mon Jul 6 08:47:18 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 05:47:18 -0700 Subject: Creating alot of class instances? In-Reply-To: <006d4e86$0$9711$c3e8da3@news.astraweb.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > ... That's the Wrong Way to do it -- > you're using a screwdriver to hammer a nail.... Don't knock tool abuse (though I agree with you here). Sometimes tool abuse can produce good results. For example, using hammers to drive screws for temporary strong holds led to making better nails. --Scott David Daniels Scott.Daniels at Acm.Org From ronn.ross at gmail.com Mon Jul 6 08:47:30 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Mon, 6 Jul 2009 08:47:30 -0400 Subject: VirtualEnv Message-ID: <9c8c445f0907060547g5a279821n4cd31efe9231887b@mail.gmail.com> I'm attempting to write a bootstrap script for virtualenv. I just want to do a couple of easy_install's after the environment is created. It was fairly easy to create the script, but I can't figure out how to implement it. The documentation was not of much help. Can someone please point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeanmichel at sequans.com Mon Jul 6 08:54:03 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 06 Jul 2009 14:54:03 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <006201c9fd48$666d94e0$0d00a8c0@Hendrik> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <006201c9fd48$666d94e0$0d00a8c0@Hendrik> Message-ID: <4A51F3EB.1080509@sequans.com> > protocol = {"start":initialiser,"hunt":hunter,"classify":classifier,....other > states} > > def state_machine(): > next_step = protocol["start"]() > while True: > next_step = protocol[next_step]() > > Woot ! I'll keep this one in my mind, while I may not be that concerned by speed unlike the OP, I still find this way of doing very simple and so intuitive (one will successfully argue how I was not figuring this out by myself if it was so intuitive). Anyway I wanted to participated to this thread, as soon as I saw 'due to python limitations' in the title, I foretold a hell of a thread ! This is just provocation ! :-) JM From pdpinheiro at gmail.com Mon Jul 6 09:38:13 2009 From: pdpinheiro at gmail.com (pdpi) Date: Mon, 6 Jul 2009 06:38:13 -0700 (PDT) Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: On Jul 6, 1:12?pm, "Hendrik van Rooyen" wrote: > "Terry Reedy" wrote: > > Gabriel Genellina wrote: > > > > In this case, a note in the documentation warning about the potential > > > confusion would be fine. > > > How would that help someone who does not read the doc? > > It obviously won't. > > All it will do, is that it will enable people on this group, > who may read the manual, to tell people who complain, > to RTFM. > > ?I agree that it would be a good idea to make it an > error, or a warning - "this might not do what you > think it does", or an "are you sure?" exception. > > ? :-) > > - Hendrik I dunno. Specifically recognizing (and emitting code code for) a token that's not actually part of the language because people coming from other languages think it exists seems like the start of a fustercluck. From dickinsm at gmail.com Mon Jul 6 09:46:05 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Mon, 6 Jul 2009 06:46:05 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> On Jul 6, 1:24?pm, Pedram wrote: > OK, fine, I read longobject.c at last! :) > I found that longobject is a structure like this: > > struct _longobject { > ? ? struct _object *_ob_next; > ? ? struct _object *_ob_prev; For current CPython, these two fields are only present in debug builds; for a normal build they won't exist. > ? ? Py_ssize_t ob_refcnt; > ? ? struct _typeobject *ob_type; You're missing an important field here (see the definition of PyObject_VAR_HEAD): Py_ssize_t ob_size; /* Number of items in variable part */ For the current implementation of Python longs, the absolute value of this field gives the number of digits in the long; the sign gives the sign of the long (0L is represented with zero digits). > ? ? digit ob_digit[1]; Right. This is an example of the so-called 'struct hack' in C; it looks as though there's just a single digit, but what's intended here is that there's an array of digits tacked onto the end of the struct; for any given PyLongObject, the size of this array is determined at runtime. (C99 allows you to write this as simply ob_digit[], but not all compilers support this yet.) > } > And a digit is a 15-item array of C's unsigned short integers. No: a digit is a single unsigned short, which is used to store 15 bits of the Python long. Python longs are stored in sign-magnitude format, in base 2**15. So each of the base 2**15 'digits' is an integer in the range [0, 32767). The unsigned short type is used to store those digits. Exception: for Python 2.7+ or Python 3.1+, on 64-bit machines, Python longs are stored in base 2**30 instead of base 2**15, using a 32-bit unsigned integer type in place of unsigned short. > Is this structure is constant in > all environments (Linux, Windows, Mobiles, etc.)? I think it would be dangerous to rely on this struct staying constant, even just for CPython. It's entirely possible that the representation of Python longs could change in Python 2.8 or 3.2. You should use the public, documented C-API whenever possible. Mark From philip at semanchuk.com Mon Jul 6 09:51:58 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 09:51:58 -0400 Subject: How to map size_t using ctypes? Message-ID: Hi all, I can't figure out how to map a C variable of size_t via Python's ctypes module. Let's say I have a C function like this: void populate_big_array(double *the_array, size_t element_count) {...} How would I pass parameter 2? A long (or ulong) will (probably) work (on most platforms), but I like my code to be more robust than that. Furthermore, this is scientific code and it's entirely possible that someone will want to pass a huge array with more elements than can be described by a 32-bit long. Suggestions appreciated. Thanks Philip From Scott.Daniels at Acm.Org Mon Jul 6 09:59:44 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 06:59:44 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: <4A520350.9000806@Acm.Org> Peter Otten wrote: > Scott David Daniels wrote: > >> Scott David Daniels wrote: > >> t = timeit.Timer('sum(part[:-1]==part[1:])', >> 'from __main__ import part') > > What happens if you calculate the sum in numpy? Try > > t = timeit.Timer('(part[:-1]==part[1:]).sum()', > 'from __main__ import part') Good idea, I hadn't thought of adding numpy bools. (part[:-1]==part[1:]).sum() is only a slight improvement over len(part[part[:-1]==part[1:]]) when there are few elements, but it is almost twice as fast when there are a lot (reflecting the work of allocating and copying). >>> import numpy >>> import timeit >>> original = numpy.random.normal(0, 100, (1000, 1000)).astype(int) >>> data = original.flatten() >>> data.sort() >>> t = timeit.Timer('sum(part[:-1]==part[1:])', 'from __main__ import part') >>> u = timeit.Timer('len(part[part[:-1]==part[1:]])', 'from __main__ import part') >>> v = timeit.Timer('(part[:-1]==part[1:]).sum()', 'from __main__ import part') >>> part = data[::100] >>> (part[:-1]==part[1:]).sum() 9390 >>> t.repeat(3, 10) [0.56368281443587875, 0.55615057220961717, 0.55465764503594528] >>> u.repeat(3, 1000) [0.89576580263690175, 0.89276374511291579, 0.8937328626963108] >>> v.repeat(3, 1000) [0.24798598704592223, 0.24715431709898894, 0.24498979618920202] >>> >>> part = original.flatten()[::100] >>> (part[:-1]==part[1:]).sum() 27 >>> t.repeat(3, 10) [0.57576898739921489, 0.56410158274297828, 0.56988248506445416] >>> u.repeat(3, 1000) [0.27312186325366383, 0.27315007913011868, 0.27214492344683094] >>> v.repeat(3, 1000) [0.28410342655297427, 0.28374053126867693, 0.28318990262732768] >>> Net result: go back to former definition of candidates (a number, not the actual entries), but calculate that number as matches.sum(), not len(part[matches]). Now the latest version of this (compressed) code: > ... > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) # count identified matches > while candidates > N * 10: # 10 -- heuristic > stride *= 2 # # heuristic increase > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > while candidates < N * 3: # heuristic slop for long runs > stride //= 2 # heuristic decrease > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > former = None > past = 0 > for value in sampled[matches]: > ... is: ... sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = matches.sum() # count identified matches while candidates > N * 10: # 10 -- heuristic stride *= 2 # # heuristic increase sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = matches.sum() while candidates < N * 3: # heuristic slop for long runs stride //= 2 # heuristic decrease sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = matches.sum() former = None past = 0 for value in sampled[matches]: ... Now I think I can let this problem go, esp. since it was mclovin's problem in the first place. --Scott David Daniels Scott.Daniels at Acm.Org From dickinsm at gmail.com Mon Jul 6 10:01:42 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Mon, 6 Jul 2009 07:01:42 -0700 (PDT) Subject: A Bug By Any Other Name ... References: Message-ID: On Jul 6, 3:32?am, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ? ? ++n > > and > > ? ? --n > > fail silently for numeric-valued n. Recent python-ideas discussion on this subject: http://mail.python.org/pipermail/python-ideas/2009-March/003741.html Mark From mail at microcorp.co.za Mon Jul 6 10:04:30 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 6 Jul 2009 16:04:30 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <006201c9fd48$666d94e0$0d00a8c0@Hendrik> <4A51F3EB.1080509@sequans.com> Message-ID: <000901c9fe42$b026cfc0$0d00a8c0@Hendrik> "Jean-Michel Pichavant" wrote: > Woot ! I'll keep this one in my mind, while I may not be that concerned > by speed unlike the OP, I still find this way of doing very simple and > so intuitive (one will successfully argue how I was not figuring this > out by myself if it was so intuitive). > Anyway I wanted to participated to this thread, as soon as I saw 'due to > python limitations' in the title, I foretold a hell of a thread ! This > is just provocation ! :-) The OP was not being provocative - he has a real problem, and the code he is complaining about already does more or less what my snippet showed, as I rushed in where angels fear to tread... The bit that was not clearly shown in what I proposed, is that you should stay in the individual states, testing for the reasons for the state transitions, until it is time to change - so there is a while loop in each of the individual states too. It becomes a terribly big structure if you have a lot of states, it duplicates a lot of tests across the different states, and it is very awkward if the states nest. Have a look at the one without the dict too - it is even faster as it avoids the dict lookup. That, however, is a bit like assembler code, as it kind of "jumps" from state to state, and there is no central thing to show what does, and what does not, belong together, as there is no dict. Not an easy beast to fix if it's big and it's wrong. - Hendrik From "sandrodll[remove]" at googlemail.com Mon Jul 6 10:21:01 2009 From: "sandrodll[remove]" at googlemail.com (gialloporpora) Date: Mon, 06 Jul 2009 16:21:01 +0200 Subject: Help to find a regular expression to parse po file Message-ID: <4A52084D.1010900@googlemail.com> Hi all, I would like to extract string from a PO file. To do this I have created a little python function to parse po file and extract string: import re regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") m=r.findall(s) where s is a po file like this: msgctxt "write ubiquity commands.description" msgid "Takes you to the Ubiquity command editor page." msgstr "Apre l'editor dei comandi di Ubiquity." #. list ubiquity commands command: #. use | to separate multiple name values: msgctxt "list ubiquity commands.names" msgid "list ubiquity commands" msgstr "elenco comandi disponibili" msgctxt "list ubiquity commands.description" msgid "Opens the list\n" " of all Ubiquity commands available and what they all do." msgstr "Apre una pagina\n" " in cui sono elencati tutti i comandi disponibili e per ognuno viene spiegato in breve a cosa serve." #. change ubiquity settings command: #. use | to separate multiple name values: msgctxt "change ubiquity settings.names" msgid "change ubiquity settings|change ubiquity preferences|change ubiquity skin" msgstr "modifica impostazioni di ubiquity|modifica preferenze di ubiquity|modifica tema di ubiquity" msgctxt "change ubiquity settings.description" msgid "Takes you to the settings page,\n" " where you can change your skin, key combinations, etc." msgstr "Apre la pagina delle impostazioni di Ubiquity,\n" " dalla quale ?? possibile modificare la combinazione da tastiera utilizzata per richiamare Ubiquity, il tema, ecc." but, obviusly, with the code above the last string is not matched. If I use re.DOTALL to match also new line character it not works because it match the entire file, I would like to stop the matching when "msgstr" is found. regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n\\n",re.DOTALL) is it possible or not ? From python at mrabarnett.plus.com Mon Jul 6 10:22:23 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 15:22:23 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A51CD6A.2010106@dejaviewphoto.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> Message-ID: <4A52089F.7020301@mrabarnett.plus.com> Dave Angel wrote: [snip] > It would probably save some time to not bother storing the zeroes in the > list at all. And it should help if you were to step through a list of > primes, rather than trying every possible int. Or at least constrain > yourself to odd numbers (after the initial case of 2). > Or stop looking for more factors when you've passed the square root of num. I don't know what effect there'll be on the time if you recalculate the square root when num changes (expensive calculation vs smaller search space). From rhodri at wildebst.demon.co.uk Mon Jul 6 11:00:59 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 06 Jul 2009 16:00:59 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: <006da5a2$0$9711$c3e8da3@news.astraweb.com> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: On Mon, 06 Jul 2009 10:58:21 +0100, Steven D'Aprano wrote: > On Mon, 06 Jul 2009 02:19:51 -0300, Gabriel Genellina wrote: > >> En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano >> escribi?: >>> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >>> >>>> I wonder how many people have been tripped up by the fact that >>>> >>>> ++n >>>> >>>> and >>>> >>>> --n >>>> >>>> fail silently for numeric-valued n. >>> >>> What do you mean, "fail silently"? They do exactly what you should >>> expect: >>>>>> ++5 # positive of a positive number is positive >>> >>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>> that are buggy, not Python. >> >> Well, those expectations are taken seriously when new features are >> introduced into the language - and sometimes the feature is dismissed >> just because it would be confusing for some. If a += 1 works, expecting >> ++a to have the same meaning is very reasonable (for those coming from >> languages with a ++ operator, like C or Java) - more when ++a is a >> perfectly valid expression. If this issue isn't listed under the various >> "Python gotchas" articles, it should... > > The fact that it isn't suggests strongly to me that it isn't that common > a surprise even for Java and C programmers. This is the first time I've > seen anyone raise it as an issue. Indeed, arguably it's a bug for C compilers to fail to find the valid parsing of "++5" as "+(+5)". All I can say is that I've never even accidentally typed that in twenty years of C programming. -- Rhodri James *-* Wildebeest Herder to the Masses From h.b.furuseth at usit.uio.no Mon Jul 6 11:04:06 2009 From: h.b.furuseth at usit.uio.no (Hallvard B Furuseth) Date: Mon, 06 Jul 2009 17:04:06 +0200 Subject: Help to find a regular expression to parse po file References: <4A52084D.1010900@googlemail.com> Message-ID: gialloporpora writes: > I would like to extract string from a PO file. To do this I have created > a little python function to parse po file and extract string: > > import re > regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") > m=r.findall(s) I don't know the syntax of a po file, but this works for the snippet you posted: arg_re = r'"[^\\\"]*(?:\\.[^\\\"]*)*"' arg_re = '%s(?:\s+%s)*' % (arg_re, arg_re) find_re = re.compile( r'^msgid\s+(' + arg_re + ')\s*\nmsgstr\s+(' + arg_re + ')\s*\n', re.M) However, can \ quote a newline? If so, replace \\. with \\[\s\S] or something. Can there be other keywords between msgid and msgstr? If so, add something like (?:\w+\s+\s*\n)*? between them. Can msgstr come before msgid? If so, forget using a single regexp. Anything else to the syntax to look out for? Single quotes, maybe? Is it a problem if the regexp isn't quite right and doesn't match all cases, yet doesn't report an error when that happens? All in all, it may be a bad idea to sqeeze this into a single regexp. It gets ugly real fast. Might be better to parse the file in a more regular way, maybe using regexps just to extract each (keyword, "value") pair. -- Hallvard From james at agentultra.com Mon Jul 6 11:06:50 2009 From: james at agentultra.com (J Kenneth King) Date: Mon, 06 Jul 2009 11:06:50 -0400 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: <85prcdetpx.fsf@agentultra.com> aahz at pythoncraft.com (Aahz) writes: > In article , > Hendrik van Rooyen wrote: >> >>But wait - maybe if he passes an iterator around - the equivalent of >>for char in input_stream... Still no good though, unless the next call >>to the iterator is faster than an ordinary python call. > > Calls to iterators created by generators are indeed faster than an > ordinary Python call, because the stack frame is already mostly set up. I think Beazely demonstrated this in his talk on using the python 2.5 co-routines to setup an xml parser. I believe he benchmarked it roughly and the initial results were rather impressive. http://www.dabeaz.com/coroutines/ From python at mrabarnett.plus.com Mon Jul 6 11:12:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 16:12:18 +0100 Subject: Help to find a regular expression to parse po file In-Reply-To: <4A52084D.1010900@googlemail.com> References: <4A52084D.1010900@googlemail.com> Message-ID: <4A521452.4090405@mrabarnett.plus.com> gialloporpora wrote: > Hi all, > I would like to extract string from a PO file. To do this I have created > a little python function to parse po file and extract string: > > import re > regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") > m=r.findall(s) > > where s is a po file like this: > > msgctxt "write ubiquity commands.description" > msgid "Takes you to the Ubiquity href=\"chrome://ubiquity/content/editor.html\">command editor page." > msgstr "Apre l'editor > dei comandi di Ubiquity." > > > #. list ubiquity commands command: > #. use | to separate multiple name values: > msgctxt "list ubiquity commands.names" > msgid "list ubiquity commands" > msgstr "elenco comandi disponibili" > > msgctxt "list ubiquity commands.description" > msgid "Opens the > list\n" > " of all Ubiquity commands available and what they all do." > msgstr "Apre una href=\"chrome://ubiquity/content/cmdlist.html\">pagina\n" > " in cui sono elencati tutti i comandi disponibili e per ognuno > viene spiegato in breve a cosa serve." > > > > #. change ubiquity settings command: > #. use | to separate multiple name values: > msgctxt "change ubiquity settings.names" > msgid "change ubiquity settings|change ubiquity preferences|change > ubiquity skin" > msgstr "modifica impostazioni di ubiquity|modifica preferenze di > ubiquity|modifica tema di ubiquity" > > msgctxt "change ubiquity settings.description" > msgid "Takes you to the href=\"chrome://ubiquity/content/settings.html\">settings page,\n" > " where you can change your skin, key combinations, etc." > msgstr "Apre la pagina href=\"chrome://ubiquity/content/settings.html\">delle impostazioni > di Ubiquity,\n" > " dalla quale ?? possibile modificare la combinazione da tastiera > utilizzata per richiamare Ubiquity, il tema, ecc." > > > > but, obviusly, with the code above the last string is not matched. If > I use re.DOTALL to match also new line character it not works because it > match the entire file, I would like to stop the matching when "msgstr" > is found. > > regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n\\n",re.DOTALL) > > is it possible or not ? > You could try: regex = re.compile(r"msgid (.*(?:\n".*")*)\nmsgstr (.*(?:\n".*")*)$") and then, if necessary, tidy what you get. From pm567426 at gmail.com Mon Jul 6 11:13:00 2009 From: pm567426 at gmail.com (Pedram) Date: Mon, 6 Jul 2009 08:13:00 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> Message-ID: <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> Hello Mr. Dickinson. Glad to see you again :) On Jul 6, 5:46?pm, Mark Dickinson wrote: > On Jul 6, 1:24?pm, Pedram wrote: > > > OK, fine, I read longobject.c at last! :) > > I found that longobject is a structure like this: > > > struct _longobject { > > ? ? struct _object *_ob_next; > > ? ? struct _object *_ob_prev; > > For current CPython, these two fields are only present in debug > builds; ?for a normal build they won't exist. I couldn't understand the difference between them. What are debug build and normal build themselves? And You mean in debug build PyLongObject is a doubly-linked-list but in normal build it is just an array (Or if not how it'll store in this mode)? > > ? ? Py_ssize_t ob_refcnt; > > ? ? struct _typeobject *ob_type; > > You're missing an important field here (see the definition of > PyObject_VAR_HEAD): > > ? ? Py_ssize_t ob_size; /* Number of items in variable part */ > > For the current implementation of Python longs, the absolute value of > this field gives the number of digits in the long; ?the sign gives the > sign of the long (0L is represented with zero digits). Oh, you're right. I missed that. Thanks :) > > ? ? digit ob_digit[1]; > > Right. ?This is an example of the so-called 'struct hack' in C; it > looks as though there's just a single digit, but what's intended here > is that there's an array of digits tacked onto the end of the struct; > for any given PyLongObject, the size of this array is determined at > runtime. ?(C99 allows you to write this as simply ob_digit[], but not > all compilers support this yet.) WOW! I didn't know anything about 'struct hacks'! I read about them and they were very wonderful. Thanks for your point. :) > > } > > And a digit is a 15-item array of C's unsigned short integers. > > No: a digit is a single unsigned short, which is used to store 15 bits > of the Python long. ?Python longs are stored in sign-magnitude format, > in base 2**15. ?So each of the base 2**15 'digits' is an integer in > the range [0, 32767). ?The unsigned short type is used to store those > digits. > > Exception: for Python 2.7+ or Python 3.1+, on 64-bit machines, Python > longs are stored in base 2**30 instead of base 2**15, using a 32-bit > unsigned integer type in place of unsigned short. > > > Is this structure is constant in > > all environments (Linux, Windows, Mobiles, etc.)? > > I think it would be dangerous to rely on this struct staying constant, > even just for CPython. ?It's entirely possible that the representation > of Python longs could change in Python 2.8 or 3.2. ?You should use the > public, documented C-API whenever possible. > > Mark Thank you a lot Mark :) From piet at cs.uu.nl Mon Jul 6 11:15:47 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Jul 2009 17:15:47 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: >>>>> Dave Angel (DA) wrote: >DA> It would probably save some time to not bother storing the zeroes in the >DA> list at all. And it should help if you were to step through a list of >DA> primes, rather than trying every possible int. Or at least constrain >DA> yourself to odd numbers (after the initial case of 2). The first and the last save a constant factor (slightly less than 2): def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] factor = 2 while num > 1: power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: powers.append(power+1) factor += 1 return powers ... return reduce(mul, powers) or to skip the odd factors: def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] factor = 2 while num > 1: power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: powers.append(power+1) factor = 3 if factor == 2 else factor + 2 return powers This can be slightly optimised by taking factor 2 out of the loop. def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] power = 0 while num % 2 == 0: power += 1 num /= 2 if power > 0: powers.append(power+1) factor = 3 while num > 1: power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: powers.append(power+1) factor += 2 return powers To restrict the search to primes you would have to use a sieve of Eratosthenes or something similar. My first attempt (with a sieve from http://code.activestate.com/recipes/117119/) only gave a speed decrease!! But this had the sieve recreated for every triangle number. A global sieve that is reused at each triangle number is better. But the speed increase relative to the odd factors only is not dramatical. # Based upon http://code.activestate.com/recipes/117119/ D = {9: 6} # contains composite numbers Dlist = [2, 3] # list of already generated primes def sieve(): '''generator that yields all prime numbers''' global D global Dlist for p in Dlist: yield p q = Dlist[-1]+2 while True: if q in D: p = D[q] x = q + p while x in D: x += p D[x] = p else: Dlist.append(q) yield q D[q*q] = 2*q q += 2 def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] power = 0 for factor in sieve(): power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: # if you really want the factors then append((factor, power)) powers.append(power+1) if num == 1: break return powers -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rhodri at wildebst.demon.co.uk Mon Jul 6 11:22:43 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 06 Jul 2009 16:22:43 +0100 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: On Mon, 06 Jul 2009 07:10:38 +0100, jack catcher (nick) wrote: > Tim Roberts kirjoitti: >> "jack catcher (nick)" wrote: >>> I'm thinking of using Python for capturing and showing live webcam >>> stream simultaneously between two computers via local area network. >>> Operating system is Windows. I'm going to begin with VideoCapture >>> extension, no ideas about other implementation yet. Do you have any >>> suggestions on how short delay I should hope to achieve in showing the >>> video? This would be part of a psychological experiment, so I would >>> need to deliver the video stream with a reasonable delay (say, below >>> 100ms). >> You need to do the math on this. Remember that a full 640x480 RGB >> stream >> at 30 frames per second runs 28 megabytes per second. That's more than >> twice what a 100 megabit network can pump. >> You can probably use Python to oversee this, but you might want to >> consider >> using lower-level code to control the actual hardware. If you are >> targeting Windows, for example, you could write a DirectShow graph to >> pump >> into a renderer that transmits out to a network, then another graph to >> receive from the network and display it. >> You can manage the network latency by adding a delays in the local >> graph. > > Thanks Tim, you're correct about the math. What is your main point about > DirectShow: that it is generally faster and more reliable than doing the > job high-level, or that one could use coding/decoding in DirectShow to > speed up the transmission? I think the latter would be a great idea if > the latency were tolerable. On the other hand, I'd like to keep things > simple and do all the programming in Python. I've got no experience with > DirectShow, but I guess the filters need to be programmed in C++ and > called from Python? > > Another option might be to use resolution 320x240 at 15fps. Does the webcam just deliver frames, or are you getting frames out of a decoder layer? If it's the latter, you want to distribute the encoded video, which should be much lower bandwidth. Exactly how you do that depends a bit on what format the webcam claims to deliver. -- Rhodri James *-* Wildebeest Herder to the Masses From jeanmichel at sequans.com Mon Jul 6 11:24:53 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 06 Jul 2009 17:24:53 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <000901c9fe42$b026cfc0$0d00a8c0@Hendrik> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <006201c9fd48$666d94e0$0d00a8c0@Hendrik> <4A51F3EB.1080509@sequans.com> <000901c9fe42$b026cfc0$0d00a8c0@Hendrik> Message-ID: <4A521745.8040307@sequans.com> Hendrik van Rooyen wrote: > "Jean-Michel Pichavant" wrote: > > >> Woot ! I'll keep this one in my mind, while I may not be that concerned >> by speed unlike the OP, I still find this way of doing very simple and >> so intuitive (one will successfully argue how I was not figuring this >> out by myself if it was so intuitive). >> Anyway I wanted to participated to this thread, as soon as I saw 'due to >> python limitations' in the title, I foretold a hell of a thread ! This >> is just provocation ! :-) >> > > The OP was not being provocative - he has a real problem, I was just kidding, asserting for python limitations in this list guarantees that the thread will last for several days, whether or not the assertion is right. JM From python-url at phaseit.net Mon Jul 6 11:33:47 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Mon, 6 Jul 2009 15:33:47 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 6) Message-ID: QOTW: "Simulating a shell with hooks on its I/O should be so complicated that a 'script kiddie' has trouble writing a Trojan." - Scott David Daniels http://groups.google.com/group/comp.lang.python/msg/1c0f70d5fc69b5aa Python 3.1 final was released last week - congratulations! http://groups.google.com/group/comp.lang.python/browse_thread/thread/b37a041ce01d4168/ Clarity of code vs. reusability in an introductory course: http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1a6ca84d3bfc483/ A piece of code analyzed, looking for ways to improve speed: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a420417a1e215cf0/ Beginner question: How to define an indeterminate number of variables? How to create objects, and later reference them, when one doesn't know how many of them will be needed? http://groups.google.com/group/comp.lang.python/browse_thread/thread/c40e3c0c843ce6fb/ Simple things made simple: correct use of Unicode helps a lot in this case: http://groups.google.com/group/comp.lang.python/browse_thread/thread/9cbb01aacf23fa8f/ super() and multiple inheritance: __init__ is hard to get right. Also, Carl Banks explains how mixin classes are best used: http://groups.google.com/group/comp.lang.python/browse_thread/thread/c934715ac83dfbd/ iter(function, sentinel) is a handy way to iterate in some cases, but beware of the fine print: http://groups.google.com/group/comp.lang.python/browse_thread/thread/b977c3b39abd98b0 The testing code should not merely repeat the code being tested (this fact isn't obvious to everyone): http://groups.google.com/group/comp.lang.python/browse_thread/thread/8978c54b83f80bd6/ Organize code so it can be run both in the source tree and after being installed: http://groups.google.com/group/comp.lang.python/browse_thread/thread/62284434d2ca5e1f/ Mark Dickinson explains the C implementation of long integers: http://groups.google.com/group/comp.lang.python/browse_thread/thread/aa1d06d371658135/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From digitig at gmail.com Mon Jul 6 11:43:43 2009 From: digitig at gmail.com (Tim Rowe) Date: Mon, 6 Jul 2009 16:43:43 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: 2009/7/4 kj : > Precisely. ?As I've stated elsewhere, this is an internal helper > function, to be called only a few times under very well-specified > conditions. ?The assert statements checks that these conditions > are as intended. ?I.e. they are checks against the module writer's > programming errors. Good for you. I'm convinced that you have used the assertion appropriately, and the fact that so many here are unable to see that looks to me like a good case for teaching the right use of assertions. For what it's worth, I read assertions at the beginning of a procedure as part of the specification of the procedure, and I use them there in order to document the procedure. An assertion in that position is for me a statement to the user of the procedure "it's your responsibility to make sure that you never call this procedure in such a way as to violate these conditions". They're part of a contract, as somebody (maybe you) pointed out. As somebody who works in the safety-critical domain, it's refreshing to see somebody teaching students to think about the circumstances in which a procedure can legitimately be called. The hostility you've received to that idea is saddening, and indicative of why there's so much buggy software out there. -- Tim Rowe From dmhouse at gmail.com Mon Jul 6 11:46:45 2009 From: dmhouse at gmail.com (David House) Date: Mon, 6 Jul 2009 16:46:45 +0100 Subject: try -> except -> else -> except? Message-ID: Hi all, I'm looking for some structure advice. I'm writing something that currently looks like the following: try: except KeyError: else: This is working fine. However, I now want to add a call to a function in the `else' part that may raise an exception, say a ValueError. So I was hoping to do something like the following: try: except KeyError: else: except ValueError: However, this isn't allowed in Python. An obvious way round this is to move the `else' clause into the `try', i.e., try: except KeyError: except ValueError: However, I am loath to do this, for two reasons: (i) if I modify the block at some point in the future so that it may raise a KeyError, I have to somehow tell this exception from the one that may be generated from the line. (ii) it moves the error handler for the bit miles away from the line that might generate the error, making it unclear which code the KeyError error handler is an error handler for. What would be the best way to structure this? -- -David From piet at cs.uu.nl Mon Jul 6 11:51:02 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Jul 2009 17:51:02 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: Sorry, there was an error in the sieve in my last example. Here is a corrected version: D = {9: 6} # contains composite numbers Dlist = [2, 3] # list of already generated primes def sieve(): '''generator that yields all prime numbers''' global D global Dlist for q in Dlist: yield q while True: q += 2 p = D.pop(q, 0) if p: x = q + p while x in D: x += p D[x] = p else: Dlist.append(q) D[q*q] = 2*q yield q -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 6 11:56:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Jul 2009 17:56:21 +0200 Subject: try -> except -> else -> except? References: Message-ID: >>>>> David House (DH) wrote: >DH> Hi all, >DH> I'm looking for some structure advice. I'm writing something that >DH> currently looks like the following: >DH> try: >DH> >DH> except KeyError: >DH> >DH> else: >DH> >DH> This is working fine. However, I now want to add a call to a function >DH> in the `else' part that may raise an exception, say a ValueError. So I >DH> was hoping to do something like the following: >DH> try: >DH> >DH> except KeyError: >DH> >DH> else: >DH> >DH> except ValueError: >DH> >DH> However, this isn't allowed in Python. >DH> An obvious way round this is to move the `else' clause into the `try', i.e., >DH> try: >DH> >DH> >DH> except KeyError: >DH> >DH> except ValueError: >DH> >DH> However, I am loath to do this, for two reasons: >DH> (i) if I modify the block at some point in >DH> the future so that it may raise a KeyError, I have to somehow tell >DH> this exception from the one that may be generated from the DH> amount of code that may raise a KeyError> line. >DH> (ii) it moves the error handler for the DH> raise a KeyError> bit miles away from the line that might generate the >DH> error, making it unclear which code the KeyError error handler is an >DH> error handler for. >DH> What would be the best way to structure this? try: except KeyError: else: try: except ValueError: -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at rgbaz.eu Mon Jul 6 12:02:18 2009 From: python at rgbaz.eu (Python) Date: Mon, 6 Jul 2009 18:02:18 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: On 6 jul 2009, at 17:46, David House wrote: > Hi all, > > I'm looking for some structure advice. I'm writing something that > currently looks like the following: > > try: > > except KeyError: > > else: > > > This is working fine. However, I now want to add a call to a function > in the `else' part that may raise an exception, say a ValueError. So I > was hoping to do something like the following: > > try: > > except KeyError: > > else: > > except ValueError: > > > However, this isn't allowed in Python. > > An obvious way round this is to move the `else' clause into the > `try', i.e., > > try: > > > except KeyError: > > except ValueError: > > > However, I am loath to do this, for two reasons: > > (i) if I modify the block at some point in > the future so that it may raise a KeyError, I have to somehow tell > this exception from the one that may be generated from the amount of code that may raise a KeyError> line. > (ii) it moves the error handler for the raise a KeyError> bit miles away from the line that might generate the > error, making it unclear which code the KeyError error handler is an > error handler for. > > What would be the best way to structure this? > > -- > -David > as far as I know try has no 'else' it's 'finally' try: a except: b except: c finally: d gr Arno From deets at nospam.web.de Mon Jul 6 12:10:36 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Jul 2009 18:10:36 +0200 Subject: How to map size_t using ctypes? References: Message-ID: <7belo1F21fs3vU1@mid.uni-berlin.de> Philip Semanchuk wrote: > Hi all, > I can't figure out how to map a C variable of size_t via Python's > ctypes module. Let's say I have a C function like this: > > void populate_big_array(double *the_array, size_t element_count) {...} > > How would I pass parameter 2? A long (or ulong) will (probably) work > (on most platforms), but I like my code to be more robust than that. > Furthermore, this is scientific code and it's entirely possible that > someone will want to pass a huge array with more elements than can be > described by a 32-bit long. from ctypes import c_size_t doesn't work for you? On my system, it's aliased to c_ulong, but I guess that's depending on the platfrom of course. Diez From dmhouse at gmail.com Mon Jul 6 12:14:23 2009 From: dmhouse at gmail.com (David House) Date: Mon, 6 Jul 2009 17:14:23 +0100 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: 2009/7/6 Python : > as far as I know try has no 'else' It does: http://docs.python.org/reference/compound_stmts.html#the-try-statement > it's 'finally' There is a `finally', too, but they are semantically different. See the above link. -- -David From python at rgbaz.eu Mon Jul 6 12:17:24 2009 From: python at rgbaz.eu (Python) Date: Mon, 6 Jul 2009 18:17:24 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: <1661ED1F-8AC9-48A7-8C87-0090942664B1@rgbaz.eu> On 6 jul 2009, at 18:14, David House wrote: > 2009/7/6 Python : >> as far as I know try has no 'else' > > It does: > http://docs.python.org/reference/compound_stmts.html#the-try-statement > >> it's 'finally' > > There is a `finally', too, but they are semantically different. See > the above link. > > -- > -David > > ah yeah you;re right, sry shouldn't the else statement come after the except ones maybe? From usernet at ilthio.net Mon Jul 6 12:25:33 2009 From: usernet at ilthio.net (Tim Harig) Date: Mon, 06 Jul 2009 16:25:33 GMT Subject: generation of keyboard events References: Message-ID: <1Ap4m.2113$8r.633@nlpi064.nbdc.sbc.com> On 2009-07-06, RAM wrote: > I am trying to do this on windows. My program(executable) has been > written in VC++ and when I run this program, I need to click on one > button on the program GUI i,e just I am entering "Enter key" on the > key board. But this needs manual process. So i need to write a python > script which invokes my program and pass "Enter key" event to my > program so that it runs without manual intervention. This can be done using the WScript.WshShell.SendKeys() method when running Python with Windows Scripting Host by using the .pyw extension: http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx From philip at semanchuk.com Mon Jul 6 12:29:21 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 12:29:21 -0400 Subject: How to map size_t using ctypes? In-Reply-To: <7belo1F21fs3vU1@mid.uni-berlin.de> References: <7belo1F21fs3vU1@mid.uni-berlin.de> Message-ID: On Jul 6, 2009, at 12:10 PM, Diez B. Roggisch wrote: > Philip Semanchuk wrote: > >> Hi all, >> I can't figure out how to map a C variable of size_t via Python's >> ctypes module. Let's say I have a C function like this: >> >> void populate_big_array(double *the_array, size_t element_count) >> {...} >> >> How would I pass parameter 2? A long (or ulong) will (probably) work >> (on most platforms), but I like my code to be more robust than that. >> Furthermore, this is scientific code and it's entirely possible that >> someone will want to pass a huge array with more elements than can be >> described by a 32-bit long. > > from ctypes import c_size_t > > doesn't work for you? On my system, it's aliased to c_ulong, but I > guess > that's depending on the platfrom of course. D'oh! [slaps forehead] That will teach me to RTFM. In my 2.5 doc, it's not listed in the "Fundamental data types" section in the tutorial, but it is mentioned in "Fundamental data types" in the ctypes reference. You'd be surprised at the amount of Googling I did without learning this on my own. Thanks Philip From "sandrodll[remove]" at googlemail.com Mon Jul 6 12:32:47 2009 From: "sandrodll[remove]" at googlemail.com (gialloporpora) Date: Mon, 06 Jul 2009 18:32:47 +0200 Subject: Help to find a regular expression to parse po file In-Reply-To: References: <4A52084D.1010900@googlemail.com> Message-ID: <4A52272F.10209@googlemail.com> Risposta al messaggio di Hallvard B Furuseth : > > I don't know the syntax of a po file, but this works for the > snippet you posted: > > arg_re = r'"[^\\\"]*(?:\\.[^\\\"]*)*"' > arg_re = '%s(?:\s+%s)*' % (arg_re, arg_re) > find_re = re.compile( > r'^msgid\s+(' + arg_re + ')\s*\nmsgstr\s+(' + arg_re + ')\s*\n', re.M) > > However, can \ quote a newline? If so, replace \\. with \\[\s\S] or > something. > Can there be other keywords between msgid and msgstr? If so, > add something like (?:\w+\s+\s*\n)*? between them. > Can msgstr come before msgid? If so, forget using a single regexp. > Anything else to the syntax to look out for? Single quotes, maybe? > > Is it a problem if the regexp isn't quite right and doesn't match all > cases, yet doesn't report an error when that happens? > > All in all, it may be a bad idea to sqeeze this into a single regexp. > It gets ugly real fast. Might be better to parse the file in a more > regular way, maybe using regexps just to extract each (keyword, "value") > pair. > Thank you very much, Haldvard, it seem to works, there is a strange match in the file header but I could skip the first match. The po files have this structure: http://bit.ly/18qbVc msgid "string to translate" " second string to match" " n string to match" msgstr "translated sting" " second translated string" " n translated string" One or more new line before the next group. In past I have created a Python script to parse PO files where msgid and msgstr are in two sequential lines, for example: msgid "string to translate" msgstr "translated string" now the problem is how to match also (optional) string between msgid and msgstr. Sandro From davea at ieee.org Mon Jul 6 12:49:49 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 12:49:49 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A52089F.7020301@mrabarnett.plus.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> Message-ID: <4A522B2D.60407@ieee.org> MRAB wrote: >
Dave > Angel wrote: > [snip] >> It would probably save some time to not bother storing the zeroes in >> the list at all. And it should help if you were to step through a >> list of primes, rather than trying every possible int. Or at least >> constrain yourself to odd numbers (after the initial case of 2). >> > Or stop looking for more factors when you've passed the square root of > num. I don't know what effect there'll be on the time if you recalculate > the square root when num changes (expensive calculation vs smaller > search space). > >
> But if I remember the code, it stopped when the quotient is one, which is usually sooner than the square root. And no need to precalculate the square root. From Scott.Daniels at Acm.Org Mon Jul 6 12:53:34 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 09:53:34 -0700 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: Piet van Oostrum wrote: >>>>>> Dave Angel (DA) wrote: > >> DA> It would probably save some time to not bother storing the zeroes in the >> DA> list at all. And it should help if you were to step through a list of >> DA> primes, rather than trying every possible int. Or at least constrain >> DA> yourself to odd numbers (after the initial case of 2). > > ... > # Based upon http://code.activestate.com/recipes/117119/ > > D = {9: 6} # contains composite numbers XXX Dlist = [2, 3] # list of already generated primes Elist = [(2, 4), (3, 9)] # list of primes and their squares > XXX def sieve(): XXX '''generator that yields all prime numbers''' XXX global D XXX global Dlist def sieve2(): '''generator that yields all primes and their squares''' # No need for global declarations, we alter, not replace XXX for p in Dlist: XXX yield p XXX q = Dlist[-1]+2 for pair in Elist: yield pair q = pair[0] + 2 > while True: > if q in D: > p = D[q] > x = q + p > while x in D: x += p > D[x] = p > else: XXX Dlist.append(q) XXX yield q XXX D[q*q] = 2*q square = q * q pair = q, square Elist.append(pair) yield pair D[square] = 2 * q > q += 2 > > def factorise(num): > """Returns a list of prime factor powers. For example: > factorise(6) will return > [2, 2] (the powers are returned one higher than the actual value) > as in, 2^1 * 3^1 = 6.""" > powers = [] > power = 0 XXX for factor in sieve(): for factor, limit in sieve2(): > power = 0 > while num % factor == 0: > power += 1 > num /= factor XXX if power > 0: if power: # good enough here, and faster > # if you really want the factors then append((factor, power)) > powers.append(power+1) XXX if num == 1: XXX break XXX return powers if num < limit: if num > 1: # if you really want the factors then append((num, 1)) powers.append(2) return powers OK, that's a straightforward speedup, _but_: factorize(6) == [2, 2] == factorize(10) == factorize(15) So I am not sure exactly what you are calculating. --Scott David Daniels Scott.Daniels at Acm.Org From davea at ieee.org Mon Jul 6 12:54:35 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 12:54:35 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A522C4B.20102@ieee.org> Rhodri James wrote: >
On Mon, > 06 Jul 2009 10:58:21 +0100, Steven D'Aprano > wrote: > >> On Mon, 06 Jul 2009 02:19:51 -0300, Gabriel Genellina wrote: >> >>> En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano >>> escribi?: >>>> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >>>> >>>>> I wonder how many people have been tripped up by the fact that >>>>> >>>>> ++n >>>>> >>>>> and >>>>> >>>>> --n >>>>> >>>>> fail silently for numeric-valued n. >>>> >>>> What do you mean, "fail silently"? They do exactly what you should >>>> expect: >>>>>>> ++5 # positive of a positive number is positive >>>> >>>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>>> that are buggy, not Python. >>> >>> Well, those expectations are taken seriously when new features are >>> introduced into the language - and sometimes the feature is dismissed >>> just because it would be confusing for some. If a += 1 works, expecting >>> ++a to have the same meaning is very reasonable (for those coming from >>> languages with a ++ operator, like C or Java) - more when ++a is a >>> perfectly valid expression. If this issue isn't listed under the >>> various >>> "Python gotchas" articles, it should... >> >> The fact that it isn't suggests strongly to me that it isn't that common >> a surprise even for Java and C programmers. This is the first time I've >> seen anyone raise it as an issue. > > Indeed, arguably it's a bug for C compilers to fail to find the valid > parsing of "++5" as "+(+5)". All I can say is that I've never even > accidentally typed that in twenty years of C programming. > But the C language specifically defines the tokenizer as doing a max-match, finding the longest legal token at any point. That's how many things that would otherwise be ambiguous are well-defined. For example, if you want to divide two integers, given pointers to them, you need a space between the slash and the start. *p1/*p2 begins a comment, while *p1/ *p2 does a division From davea at ieee.org Mon Jul 6 13:13:48 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 13:13:48 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A5230CC.2020801@ieee.org> Scott David Daniels wrote: >
Piet van > Oostrum wrote: >>>>>>> Dave Angel (DA) wrote: >> >>> DA> It would probably save some time to not bother storing the >>> zeroes in the >>> DA> list at all. And it should help if you were to step through a >>> list of >>> DA> primes, rather than trying every possible int. Or at least >>> constrain >>> DA> yourself to odd numbers (after the initial case of 2). >> >> ... >> # Based upon http://code.activestate.com/recipes/117119/ >> >> D = {9: 6} # contains composite numbers > XXX Dlist = [2, 3] # list of already generated primes > Elist = [(2, 4), (3, 9)] # list of primes and their squares >> > XXX def sieve(): > XXX '''generator that yields all prime numbers''' > XXX global D > XXX global Dlist > def sieve2(): > '''generator that yields all primes and their squares''' > # No need for global declarations, we alter, not replace > XXX for p in Dlist: > XXX yield p > XXX q = Dlist[-1]+2 > > for pair in Elist: > yield pair > q = pair[0] + 2 > >> while True: >> if q in D: >> p = D[q] >> x = q + p >> while x in D: x += p >> D[x] = p >> else: > XXX Dlist.append(q) > XXX yield q > XXX D[q*q] = 2*q > square = q * q > pair = q, square > Elist.append(pair) > yield pair > D[square] = 2 * q >> q += 2 >> >> def factorise(num): >> """Returns a list of prime factor powers. For example: >> factorise(6) will return >> [2, 2] (the powers are returned one higher than the actual >> value) >> as in, 2^1 * 3^1 = 6.""" >> powers = [] >> power = 0 > XXX for factor in sieve(): > for factor, limit in sieve2(): >> power = 0 >> while num % factor == 0: >> power += 1 >> num /= factor > XXX if power > 0: > if power: # good enough here, and faster >> # if you really want the factors then append((factor, >> power)) >> powers.append(power+1) > XXX if num == 1: > XXX break > XXX return powers > if num < limit: > if num > 1: > # if you really want the factors then append((num, 1)) > powers.append(2) > return powers > > OK, that's a straightforward speedup, _but_: > factorize(6) == [2, 2] == factorize(10) == factorize(15) > So I am not sure exactly what you are calculating. > > > --Scott David Daniels > Scott.Daniels at Acm.Org > >
> The OP only needed the number of factors, not the actual factors. So the zeroes in the list are unneeded. 6, 10, and 15 each have 4 factors. From sajmikins at gmail.com Mon Jul 6 13:15:22 2009 From: sajmikins at gmail.com (Simon Forman) Date: Mon, 6 Jul 2009 13:15:22 -0400 Subject: Tree structure consuming lot of memory In-Reply-To: References: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> Message-ID: <50f98a4c0907061015n68191f9bj868b8e1b8908223c@mail.gmail.com> On Mon, Jul 6, 2009 at 6:12 AM, mayank gupta wrote: > Thanks for the other possibilites. I would consider option (2) and (3) to > improve my code. > > But out of curiosity, I would still like to know why does an object of a > Python-class consume "so" much of memory (1.4 kb), and this memory usage has > nothing to do with its attributes. > > Thanks > > Regards. > > On Mon, Jul 6, 2009 at 12:03 PM, Chris Rebert wrote: >> >> On Mon, Jul 6, 2009 at 2:55 AM, mayank gupta wrote: >> > Hi, >> > >> > I am creating a tree data-structure in python; with nodes of the tree >> > created by a simple class : >> > >> > class Node : >> > ?????? def __init__(self , .... other attributes): >> > ????????????? # initialise the attributes here!! >> > >> > But the problem is I am working with a huge tree (millions of nodes); >> > and >> > each node is consuming much more memory than it should. After a little >> > analysis, I found out that in general it uses about 1.4 kb of memory for >> > each node!! >> > I will be grateful if someone could help me optimize the memory usage. >> >> (1) Use __slots__ (see >> http://docs.python.org/reference/datamodel.html#slots) >> (2) Use some data structure other than a tree >> (3) Rewrite your Node/Tree implementation in C >> >> Cheers, >> Chris >> -- >> http://blog.rebertia.com > For option 2 you should try using the built in types, list tuple or dict. You might get better results. I'm curious too as to why the class/instance code should take so much memory, could you mention more about the code? From python at mrabarnett.plus.com Mon Jul 6 13:20:59 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 18:20:59 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A522B2D.60407@ieee.org> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> <4A522B2D.60407@ieee.org> Message-ID: <4A52327B.3000403@mrabarnett.plus.com> Dave Angel wrote: > MRAB wrote: >>
Dave >> Angel wrote: >> [snip] >>> It would probably save some time to not bother storing the zeroes in >>> the list at all. And it should help if you were to step through a >>> list of primes, rather than trying every possible int. Or at least >>> constrain yourself to odd numbers (after the initial case of 2). >>> >> Or stop looking for more factors when you've passed the square root of >> num. I don't know what effect there'll be on the time if you recalculate >> the square root when num changes (expensive calculation vs smaller >> search space). >> >>
>> > But if I remember the code, it stopped when the quotient is one, which > is usually sooner than the square root. And no need to precalculate the > square root. > If the number is a large prime then the code will try all the numbers up to that, eg if num == 1000003 then it'll try 2..1000003 even though it could've given up after 1000. From pescadero10 at gmail.com Mon Jul 6 13:30:41 2009 From: pescadero10 at gmail.com (pescadero10) Date: Mon, 6 Jul 2009 10:30:41 -0700 (PDT) Subject: updating, adding new pages to confluence remotely, using python Message-ID: Hello, I am new to python and have been trying to figure out how to remotely add new pages to my confluence wiki space. I'm running my python script from a linux rhel4 machine and using confluence version 2.10. As a test I tried to read from stdin and write a page but it fails- that is, the script runs without errors but nothing is added. Does anyone have an example of how this is done? Here is my script: --- begin script ----------- #!/usr/local/bin/python # # Reads from standard input, dumps it onto a Confluence page # You'll need to modify the URL/username/password/spacekey/page title # below, because I'm too lazy to bother with argv. import sys from xmlrpclib import Server # Read the text of the page from standard input content = sys.stdin.read() s = Server("http://confluence.slac.stanford.edu/display/GO/Home") token = s.confluence1.login("chee", "******") page = s.confluence1.getPage(token, "SPACEKEY", "TEST Python-2- Confluence") page["content"] = content s.confluence1.storePage(token, page) newpagedata = {"title":"New Page","content":"new content","space":"spaceKey"} newpage = s.confluence1.storePage(token, newpagedata); ------------ end script ------------------------ Any help would be greatly appreciated. thanks, Pescadero10 From nomail at thank.you Mon Jul 6 13:41:03 2009 From: nomail at thank.you (jack catcher (nick)) Date: Mon, 06 Jul 2009 20:41:03 +0300 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: Rhodri James kirjoitti: > On Mon, 06 Jul 2009 07:10:38 +0100, jack catcher (nick) > wrote: > >> Tim Roberts kirjoitti: >>> "jack catcher (nick)" wrote: >>>> I'm thinking of using Python for capturing and showing live webcam >>>> stream simultaneously between two computers via local area network. >>>> Operating system is Windows. I'm going to begin with VideoCapture >>>> extension, no ideas about other implementation yet. Do you have any >>>> suggestions on how short delay I should hope to achieve in showing >>>> the video? This would be part of a psychological experiment, so I >>>> would need to deliver the video stream with a reasonable delay (say, >>>> below 100ms). >>> You need to do the math on this. Remember that a full 640x480 RGB >>> stream >>> at 30 frames per second runs 28 megabytes per second. That's more than >>> twice what a 100 megabit network can pump. >>> You can probably use Python to oversee this, but you might want to >>> consider >>> using lower-level code to control the actual hardware. If you are >>> targeting Windows, for example, you could write a DirectShow graph to >>> pump >>> into a renderer that transmits out to a network, then another graph to >>> receive from the network and display it. >>> You can manage the network latency by adding a delays in the local >>> graph. >> >> Thanks Tim, you're correct about the math. What is your main point >> about DirectShow: that it is generally faster and more reliable than >> doing the job high-level, or that one could use coding/decoding in >> DirectShow to speed up the transmission? I think the latter would be a >> great idea if the latency were tolerable. On the other hand, I'd like >> to keep things simple and do all the programming in Python. I've got >> no experience with DirectShow, but I guess the filters need to be >> programmed in C++ and called from Python? >> >> Another option might be to use resolution 320x240 at 15fps. > > Does the webcam just deliver frames, or are you getting frames out of > a decoder layer? If it's the latter, you want to distribute the encoded > video, which should be much lower bandwidth. Exactly how you do that > depends a bit on what format the webcam claims to deliver. Well, getting already encoded video from the webcam sounds almost like a free lunch (which it probably is not). At least I wouldn't want to get too long a delay because of the encoding. I haven't got the webcam(s) yet, and I guess I can basically purchase any ones I find suitable for getting the job done. Any recommendations? From "sandrodll[remove]" at googlemail.com Mon Jul 6 13:42:46 2009 From: "sandrodll[remove]" at googlemail.com (gialloporpora) Date: Mon, 06 Jul 2009 19:42:46 +0200 Subject: Help to find a regular expression to parse po file In-Reply-To: References: <4A52084D.1010900@googlemail.com> Message-ID: <4A523796.3050509@googlemail.com> Risposta al messaggio di MRAB : > gialloporpora wrote: >> Hi all, >> I would like to extract string from a PO file. To do this I have created >> a little python function to parse po file and extract string: >> >> import re >> regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") >> m=r.findall(s) >> >> where s is a po file like this: >> >> msgctxt "write ubiquity commands.description" >> msgid "Takes you to the Ubiquity> href=\"chrome://ubiquity/content/editor.html\">command editor page." >> msgstr "Apre l'editor >> dei comandi di Ubiquity." >> >> >> #. list ubiquity commands command: >> #. use | to separate multiple name values: >> msgctxt "list ubiquity commands.names" >> msgid "list ubiquity commands" >> msgstr "elenco comandi disponibili" >> >> msgctxt "list ubiquity commands.description" >> msgid "Opensthe >> list\n" >> " of all Ubiquity commands available and what they all do." >> msgstr "Apre una> href=\"chrome://ubiquity/content/cmdlist.html\">pagina\n" >> " in cui sono elencati tutti i comandi disponibili e per ognuno >> viene spiegato in breve a cosa serve." >> >> >> >> #. change ubiquity settings command: >> #. use | to separate multiple name values: >> msgctxt "change ubiquity settings.names" >> msgid "change ubiquity settings|change ubiquity preferences|change >> ubiquity skin" >> msgstr "modifica impostazioni di ubiquity|modifica preferenze di >> ubiquity|modifica tema di ubiquity" >> >> msgctxt "change ubiquity settings.description" >> msgid "Takes you to the> href=\"chrome://ubiquity/content/settings.html\">settings page,\n" >> " where you can change your skin, key combinations, etc." >> msgstr "Apre la pagina> href=\"chrome://ubiquity/content/settings.html\">delle impostazioni >> di Ubiquity,\n" >> " dalla quale ?? possibile modificare la combinazione da tastiera >> utilizzata per richiamare Ubiquity, il tema, ecc." >> >> >> >> but, obviusly, with the code above the last string is not matched. If >> I use re.DOTALL to match also new line character it not works because it >> match the entire file, I would like to stop the matching when "msgstr" >> is found. >> >> regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n\\n",re.DOTALL) >> >> is it possible or not ? >> > You could try: > > regex = re.compile(r"msgid (.*(?:\n".*")*)\nmsgstr (.*(?:\n".*")*)$") > > and then, if necessary, tidy what you get. MRAB, thank you for your help, I have tried the code posted by Hallvard because I have seen it before and it works. Now I'll check also your suggestions. Sandro -- *Pink Floyd ? The Great Gig in the Sky* - http://sn.im/kggo7 * FAQ* di /it-alt.comp.software.mozilla/: http://bit.ly/1MZ04d From bdesth.quelquechose at free.quelquepart.fr Mon Jul 6 13:48:29 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Mon, 06 Jul 2009 19:48:29 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: <4a52548c$0$31414$426a74cc@news.free.fr> David House a ?crit : > Hi all, > > I'm looking for some structure advice. I'm writing something that > currently looks like the following: > > try: > > except KeyError: > > else: > > > This is working fine. However, I now want to add a call to a function > in the `else' part that may raise an exception, say a ValueError. If your error handler terminates the function (which is usually the case when using the else clause), you can just skip the else statement, ie: try: except KeyError: Then adding one or more try/except is just trivial. > So I > was hoping to do something like the following: > > try: > > except KeyError: > > else: > > except ValueError: > > > However, this isn't allowed in Python. Nope. But this is legal: try: except KeyError: else: try: except ValueError: > An obvious way round this is to move the `else' clause into the `try' "obvious" but not necessarily the best thing to do. (snip - cf above for simple answers) From bdesth.quelquechose at free.quelquepart.fr Mon Jul 6 13:49:49 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Mon, 06 Jul 2009 19:49:49 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: <4a5254db$0$31414$426a74cc@news.free.fr> Python a ?crit : (snip whole OP) > as far as I know try has no 'else' Then you may want to RTFM. From ethan at stoneleaf.us Mon Jul 6 13:53:31 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 06 Jul 2009 10:53:31 -0700 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A523A1B.70901@stoneleaf.us> Many thanks to all who replied! And, yes, I will *definitely* use raw strings from now on. :) ~Ethan~ From bdesth.quelquechose at free.quelquepart.fr Mon Jul 6 13:56:00 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Mon, 06 Jul 2009 19:56:00 +0200 Subject: How Python Implements "long integer"? In-Reply-To: <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> Message-ID: <4a52564e$0$31414$426a74cc@news.free.fr> Mark Dickinson a ?crit : > On Jul 5, 1:09 pm, Pedram wrote: >> Thanks for reply, >> Sorry I can't explain too clear! I'm not English ;) > > That's shocking. Everyone should be English. :-) > Mark, tu sors ! From davea at ieee.org Mon Jul 6 14:28:05 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 14:28:05 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A52327B.3000403@mrabarnett.plus.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> <4A522B2D.60407@ieee.org> <4A52327B.3000403@mrabarnett.plus.com> Message-ID: <4A524235.1010002@ieee.org> MRAB wrote: >
Dave > Angel wrote: >> MRAB wrote: >>>
Dave >>> Angel wrote: >>> [snip] >>>> It would probably save some time to not bother storing the zeroes >>>> in the list at all. And it should help if you were to step through >>>> a list of primes, rather than trying every possible int. Or at >>>> least constrain yourself to odd numbers (after the initial case of 2). >>>> >>> Or stop looking for more factors when you've passed the square root of >>> num. I don't know what effect there'll be on the time if you >>> recalculate >>> the square root when num changes (expensive calculation vs smaller >>> search space). >>> >>>
>>> >> But if I remember the code, it stopped when the quotient is one, >> which is usually sooner than the square root. And no need to >> precalculate the square root. >> > If the number is a large prime then the code will try all the numbers up > to that, eg if num == 1000003 then it'll try 2..1000003 even though it > could've given up after 1000. > >
> That's one reason I suggested (and Piet implemented) a sieve. You can stop dividing when the square of the next prime exceeds your quotient. From tn.pablo at gmail.com Mon Jul 6 14:49:56 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Mon, 6 Jul 2009 13:49:56 -0500 Subject: A Bug By Any Other Name ... In-Reply-To: <00df01c9fe33$10c36d80$0d00a8c0@Hendrik> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <00df01c9fe33$10c36d80$0d00a8c0@Hendrik> Message-ID: On Mon, Jul 6, 2009 at 07:12, Hendrik van Rooyen wrote: > "Terry Reedy" wrote: > >> Gabriel Genellina wrote: >> > >> > In this case, a note in the documentation warning about the potential >> > confusion would be fine. >> >> How would that help someone who does not read the doc? > > It obviously won't. > > All it will do, is that it will enable people on this group, > who may read the manual, to tell people who complain, > to RTFM. Yes, it's their problem if they don't read the docs. > > ?I agree that it would be a good idea to make it an > error, or a warning - "this might not do what you > think it does", or an "are you sure?" exception. > > ?:-) > > - Hendrik That would be even harder than adding a line to the docs. Besides, the problem that Mr. alex23 pointed: "where do you stop?" would really get out of hand. -- Pablo Torres N. From jdnier at gmail.com Mon Jul 6 14:52:26 2009 From: jdnier at gmail.com (David Niergarth) Date: Mon, 6 Jul 2009 11:52:26 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: Message-ID: I remember in college taking an intro programming class (C++) where the professor started us off writing a program to factor polynomials; he probably also incorporated binary search into an assignment. But people don't generally use Python to implement binary search or factor polynomials so maybe you should start with a problem more germane to typical novice users (and less algorithm-y). Wouldn't starting them off with string processing or simple calculations be a practical way to get comfortable with the language? --David On Jul 3, 9:05?am, kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. ?I'd love your opinion about > it. > > The context is the concept of a binary search. ?In one of their > homeworks, my students will have two occasions to use a binary > search. ?This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. > > This seemed straightforward enough, until I realized that, to be > useful to my students in their homework, this _binary_search function > had to handle the case in which the passed function was monotonically > decreasing in the specified interval... > > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > ? ? assert lo < hi > ? ? assert epsilon > 0 > ? ? sense = cmp(func(hi), func(lo)) > ? ? if sense == 0: > ? ? ? ? return None > ? ? target_plus = sense * target + epsilon > ? ? target_minus = sense * target - epsilon > ? ? while True: > ? ? ? ? param = (lo + hi) * 0.5 > ? ? ? ? value = sense * func(param) > ? ? ? ? if value > target_plus: > ? ? ? ? ? ? hi = param > ? ? ? ? elif value < target_minus: > ? ? ? ? ? ? lo = param > ? ? ? ? else: > ? ? ? ? ? ? return param > > ? ? ? ? if lo == hi: > ? ? ? ? ? ? return None > > My question is: is the business with sense and cmp too "clever"? > > Here's the rub: the code above is more general (hence more reusable) > by virtue of this trick with the sense parameter, but it is also > a bit harder to understand. > > This not an unusual situation. ?I find that the processing of > abstracting out common logic often results in code that is harder > to read, at least for the uninitiated... > > I'd love to know your opinions on this. > > TIA! > > kj From nobody at nowhere.com Mon Jul 6 15:41:53 2009 From: nobody at nowhere.com (Nobody) Date: Mon, 06 Jul 2009 20:41:53 +0100 Subject: Python and webcam capture delay? References: Message-ID: On Mon, 06 Jul 2009 20:41:03 +0300, jack catcher (nick) wrote: >> Does the webcam just deliver frames, or are you getting frames out of >> a decoder layer? If it's the latter, you want to distribute the encoded >> video, which should be much lower bandwidth. Exactly how you do that >> depends a bit on what format the webcam claims to deliver. > > Well, getting already encoded video from the webcam sounds almost like a > free lunch (which it probably is not). At least I wouldn't want to get > too long a delay because of the encoding. > > I haven't got the webcam(s) yet, and I guess I can basically purchase > any ones I find suitable for getting the job done. Any recommendations? The webcam is bound to do some encoding; most of them use USB "full speed" (12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. Chroma subsampling and JPEG compression will both reduce the bandwidth without introducing noticable latency (the compression time will be no more than one frame). Temporal compression will reduce the bandwidth further. Using B-frames (frames which contain the differences from a predicted frame which is based upon both previous and subsequent frames) will provide more compression, but increases the latency significantly. For this reason, the compression built into video cameras normally only uses P-frames (frames which contain the differences from a predicted frame which is based only upon previous frames). The biggest issue is likely to be finding latency specifications, followed by finding a camera which meets your latency requirement. Also, any "read frame, write frame" loops will add an additional frame of latency, as you can't start writing the first byte of the frame until after you've read the last byte of the frame. Video APIs which let you get rows as they're decoded are rare. From sisibley at gmail.com Mon Jul 6 15:43:40 2009 From: sisibley at gmail.com (Scott Sibley) Date: Mon, 6 Jul 2009 14:43:40 -0500 Subject: Ctypes to wrap libusb-1.0 Message-ID: I have been having issues trying to wrap libusb-1.0 with ctypes. Actually, there's not much of an issue if I keep everything synchronous, but I need this to be asynchronous and that is where my problem arises. Please refer to the following link on Stackoverflow for a full overview of the issue. http://stackoverflow.com/questions/1060305/usb-sync-vs-async-vs-semi-async-partially-answered-now As mentioned in the question on Stackoverflow, synchronous transfers work great. I wrote an asynchronous C version that works fine. usbmon's output suggests the transfers are making it through. libusb's debug output shows nothing out of the ordinary. I've also asked about this on the libusb mailing list. I've hesitated asking on the Python mailing list up till now. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Mon Jul 6 15:55:02 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 15:55:02 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: Mark Dickinson wrote: > On Jul 6, 3:32 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> I wonder how many people have been tripped up by the fact that >> >> ++n >> >> and >> >> --n >> >> fail silently for numeric-valued n. Rather few, it seems. > Recent python-ideas discussion on this subject: > > http://mail.python.org/pipermail/python-ideas/2009-March/003741.html To add to what I wrote in that thread: it is C, not Python, that is out of step with standard usage in math and most languages. --x = x; 1/1/x = x; non not a = a; inverse(inverse(f)) = f; etc. And ++x= +x = x corresponded to I(I(x)) = I(x) = x, where I is identity function. In C as high-level assembler, the inc and dec functions are written as -- and ++ operators for some mix of the following reasons. 1) They correspond to machine language / assembler functions. 2) They need to be efficient since they are used in inner loops. Function calls have overhead. Original C did not allow inlining of function calls as best I remember. 3) They save keystrokes; 4) 2 versions that return pre and post change values are needed. My impression is that some opcode sets (and hence assemblers) have only one, and efficient code requires allowing direct access to whichever one a particular processor supports. Basic routines can usually be written either way. These reasons do not apply to Python or do not fit Python's style. Anyone who spends 15 minutes skimming the chapter on expressions could notice that 5.5. Unary arithmetic and bitwise operations has only +,-, and ~ or that the Summary operator table at the end has no -- or ++. Terry Jan Reedy From solipsis at pitrou.net Mon Jul 6 15:58:26 2009 From: solipsis at pitrou.net (Antoine Pitrou) Date: Mon, 6 Jul 2009 19:58:26 +0000 (UTC) Subject: Tree structure consuming lot of memory References: Message-ID: mayank gupta gmail.com> writes: > > After a little analysis, I found out that in general it uses about > 1.4 kb of memory for each node!! How did you measure memory use? Python objects are not very compact, but 1.4KB per object seems a bit too much (I would expect more about 150-200 bytes/object in 32-bit mode, or 300-400 bytes/object in 64-bit mode). One of the solutions is to use __slots__ as already suggested. Another, which will have similar benefits, is to use a namedtuple. Both suppress the instance dictionnary (`instance`.__dict__), which is a major contributor to memory consumption. Illustration (64-bit mode, by the way): >>> import sys >>> from collections import namedtuple # First a normal class >>> class Node(object): pass ... >>> o = Node() >>> o.value = 1 >>> o.children = () >>> >>> sys.getsizeof(o) 64 >>> sys.getsizeof(o.__dict__) 280 # The object seems to take a mere 64 bytes, but the attribute dictionnary # adds a whoppy 280 bytes and bumps actual size to 344 bytes! # Now a namedtuple (a tuple subclass with property accessors for the various # tuple items) >>> Node = namedtuple("Node", "value children") >>> >>> o = Node(value=1, children=()) >>> sys.getsizeof(o) 72 >>> sys.getsizeof(o.__dict__) Traceback (most recent call last): File "", line 1, in AttributeError: 'Node' object has no attribute '__dict__' # The object doesn't have a __dict__, so 72 bytes is its real total size. From tjreedy at udel.edu Mon Jul 6 16:27:25 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 16:27:25 -0400 Subject: updating, adding new pages to confluence remotely, using python In-Reply-To: References: Message-ID: pescadero10 wrote: > > I am new to python and have been trying to figure out how to remotely > add new pages to my confluence > wiki space. I'm running my python script from a linux rhel4 machine > and using confluence version 2.10. As a test I tried to read from > stdin and write a page but it fails- that is, the script runs without > errors but nothing is added. Does anyone have an example of how this > is done? Here is my script: > > --- begin script ----------- > #!/usr/local/bin/python > # > # Reads from standard input, dumps it onto a Confluence page > # You'll need to modify the URL/username/password/spacekey/page title > # below, because I'm too lazy to bother with argv. > > import sys > from xmlrpclib import Server > > # Read the text of the page from standard input > content = sys.stdin.read() > > s = Server("http://confluence.slac.stanford.edu/display/GO/Home") > token = s.confluence1.login("chee", "******") > page = s.confluence1.getPage(token, "SPACEKEY", "TEST Python-2- > Confluence") > page["content"] = content > s.confluence1.storePage(token, page) > > newpagedata = {"title":"New Page","content":"new > content","space":"spaceKey"} > newpage = s.confluence1.storePage(token, newpagedata); > ------------ end script ------------------------ > > Any help would be greatly appreciated. You neglected to specify Python version. As near as I can tell from 2.x docs, xmlrpclib has ServerProxy class but no Server class. Whoops, just saw "Server is retained as an alias for ServerProxy for backwards compatibility. New code should use ServerProxy". Good idea -- calling a client 'server' is confusing. Do you have access to logs on remote machine to see what was received? Or to a sysadmin? tjr From mooniitk at gmail.com Mon Jul 6 16:30:28 2009 From: mooniitk at gmail.com (mayank gupta) Date: Tue, 7 Jul 2009 02:00:28 +0530 Subject: Tree structure consuming lot of memory In-Reply-To: References: Message-ID: I worked out a small code which initializes about 1,000,000 nodes with some attributes, and saw the memory usage on my linux machine (using 'top' command). Then just later I averaged out the memory usage per node. I know this is not the most accurate way but just for estimated value. The kind of Node class I am working on in my original code is like : class Node: def __init__(self, #attributes ): self.coordinates = coordinates self.index = index self.sibNum = sibNum self.branchNum - branchNum #here 'coordinates' and 'index' are LISTS with length = "dimension", where "dimension" is a user-input. The most shocking part of it after the memory-analysis was that, the memory usage was never dependent on the "dimension". Yeah it varied a bit, but there wasnt any significant changes in the memory usage even when the "dimension" was doubled -- Any clues? Thank you for all your suggestions till this point. Regards. On Tue, Jul 7, 2009 at 1:28 AM, Antoine Pitrou wrote: > mayank gupta gmail.com> writes: > > > > After a little analysis, I found out that in general it uses about > > 1.4 kb of memory for each node!! > > How did you measure memory use? Python objects are not very compact, but > 1.4KB > per object seems a bit too much (I would expect more about 150-200 > bytes/object > in 32-bit mode, or 300-400 bytes/object in 64-bit mode). > > One of the solutions is to use __slots__ as already suggested. Another, > which > will have similar benefits, is to use a namedtuple. Both suppress the > instance > dictionnary (`instance`.__dict__), which is a major contributor to memory > consumption. Illustration (64-bit mode, by the way): > > >>> import sys > >>> from collections import namedtuple > > # First a normal class > >>> class Node(object): pass > ... > >>> o = Node() > >>> o.value = 1 > >>> o.children = () > >>> > >>> sys.getsizeof(o) > 64 > >>> sys.getsizeof(o.__dict__) > 280 > # The object seems to take a mere 64 bytes, but the attribute dictionnary > # adds a whoppy 280 bytes and bumps actual size to 344 bytes! > > # Now a namedtuple (a tuple subclass with property accessors for the > various > # tuple items) > >>> Node = namedtuple("Node", "value children") > >>> > >>> o = Node(value=1, children=()) > >>> sys.getsizeof(o) > 72 > >>> sys.getsizeof(o.__dict__) > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'Node' object has no attribute '__dict__' > > # The object doesn't have a __dict__, so 72 bytes is its real total size. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From thebrasse at gmail.com Mon Jul 6 16:36:59 2009 From: thebrasse at gmail.com (brasse) Date: Mon, 6 Jul 2009 13:36:59 -0700 (PDT) Subject: Cleaning up after failing to contructing objects Message-ID: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Hello! I have been thinking about how write exception safe constructors in Python. By exception safe I mean a constructor that does not leak resources when an exception is raised within it. The following is an example of one possible way to do it: class Foo(object): def __init__(self, name, fail=False): self.name = name if not fail: print '%s.__init__(%s)' % (self.__class__.__name__, self.name) else: print '%s.__init__(%s), FAIL' % (self.__class__.__name__, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) class Bar(object): def __init__(self): try: self.a = Foo('a') self.b = Foo('b', fail=True) except: self.close() def close(self): if hasattr(self, 'a'): self.a.close() if hasattr(self, 'b'): self.b.close() bar = Bar() As you can see this is less than straight forward. Is there some kind of best practice that I'm not aware of? :.:: mattias From no.email at please.post Mon Jul 6 16:40:49 2009 From: no.email at please.post (kj) Date: Mon, 6 Jul 2009 20:40:49 +0000 (UTC) Subject: Why re.match()? References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: >kj a ?crit : >(snipo >> To have a special-case >> re.match() method in addition to a general re.search() method is >> antithetical to language minimalism, >FWIW, Python has no pretention to minimalism. Assuming that you mean by this that Python's authors have no such pretensions: "There is real value in having a small language." Guido van Rossum, 2007.07.03 http://mail.python.org/pipermail/python-3000/2007-July/008663.html So there. BTW, that's just one example. I've seen similar sentiments expressed by Guido over and over and over: any new proposed enhancement to Python must be good enough in his mind to justify cluttering the language. That attitude counts as minimalism in my book. The best explanation I have found so far for re.match is that it is an unfortunate bit of legacy, something that would not be there if the design of Python did not have to be mindful of keeping old code chugging along... kj From joshua at joshuakugler.com Mon Jul 6 16:45:45 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Mon, 06 Jul 2009 12:45:45 -0800 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: Paul Moore wrote: > The SQLite documentation mentions a flag, SQLITE_OPEN_READONLY, to > open a database read only. I can't find any equivalent documented in > the Python standard library documentation for the sqlite3 module (or, > for that matter, on the pysqlite library's website). > > Is it possible to open a sqlite database in readonly mode, in Python? Yes, but most likely not with pysqlite. The python sqlite3 module in the standard library, and the pysqlite module are both DB-API compliant, which means they probably do not have a method to open the DB read only (as that is usually enforced at the user permission level). If you want to use that flag, take a look at APSW. It is a very thin layer on top of the SQLite C API, and thus supports everything that the C API supports. It is not, however, DB API compliant, but is very close so not hard to pick up. BTW, APSW is written by the same author as pysqlite. j From clp2 at rebertia.com Mon Jul 6 17:03:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 14:03:48 -0700 Subject: Tree structure consuming lot of memory In-Reply-To: References: Message-ID: <50697b2c0907061403s16c0602cp5d3767772e6e8ba@mail.gmail.com> > On Tue, Jul 7, 2009 at 1:28 AM, Antoine Pitrou wrote: >> >> mayank gupta gmail.com> writes: >> > >> > After a little analysis, I found out that in general it uses about >> > 1.4 kb of memory for each node!! >> >> How did you measure memory use? Python objects are not very compact, but >> 1.4KB >> per object seems a bit too much (I would expect more about 150-200 >> bytes/object >> in 32-bit mode, or 300-400 bytes/object in 64-bit mode). >> >> One of the solutions is to use __slots__ as already suggested. Another, >> which >> will have similar benefits, is to use a namedtuple. Both suppress the >> instance >> dictionnary (`instance`.__dict__), which is a major contributor to memory >> consumption. Illustration (64-bit mode, by the way): >> >> >>> import sys >> >>> from collections import namedtuple >> >> # First a normal class >> >>> class Node(object): pass >> ... >> >>> o = Node() >> >>> o.value = 1 >> >>> o.children = () >> >>> >> >>> sys.getsizeof(o) >> 64 >> >>> sys.getsizeof(o.__dict__) >> 280 >> # The object seems to take a mere 64 bytes, but the attribute dictionnary >> # adds a whoppy 280 bytes and bumps actual size to 344 bytes! >> >> # Now a namedtuple (a tuple subclass with property accessors for the >> various >> # tuple items) >> >>> Node = namedtuple("Node", "value children") >> >>> >> >>> o = Node(value=1, children=()) >> >>> sys.getsizeof(o) >> 72 >> >>> sys.getsizeof(o.__dict__) >> Traceback (most recent call last): >> ?File "", line 1, in >> AttributeError: 'Node' object has no attribute '__dict__' >> >> # The object doesn't have a __dict__, so 72 bytes is its real total size. On Mon, Jul 6, 2009 at 1:30 PM, mayank gupta wrote: > I worked out a small code which initializes about 1,000,000 nodes with some > attributes, and saw the memory usage on my linux machine (using 'top' > command). Then just later I averaged out the memory usage per node. I know > this is not the most accurate way but just for estimated value. You should try the more accurate sys.getsizeof() function: http://docs.python.org/library/sys.html#sys.getsizeof Cheers, Chris P.S. Please don't top-post in the future. -- http://blog.rebertia.com From deets at nospam.web.de Mon Jul 6 17:10:28 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Jul 2009 23:10:28 +0200 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: <7bf7i4F2350o5U1@mid.uni-berlin.de> kj schrieb: > In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: > >> kj a ?crit : >> (snipo >>> To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, > >> FWIW, Python has no pretention to minimalism. > > Assuming that you mean by this that Python's authors have no such > pretensions: > > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html > > So there. > > BTW, that's just one example. I've seen similar sentiments expressed > by Guido over and over and over: any new proposed enhancement to > Python must be good enough in his mind to justify cluttering the > language. That attitude counts as minimalism in my book. > > The best explanation I have found so far for re.match is that it > is an unfortunate bit of legacy, something that would not be there > if the design of Python did not have to be mindful of keeping old > code chugging along... language != libraries. Diez From Scott.Daniels at Acm.Org Mon Jul 6 17:15:44 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 14:15:44 -0700 Subject: Cleaning up after failing to contructing objects In-Reply-To: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: brasse wrote: > I have been thinking about how write exception safe constructors in > Python. By exception safe I mean a constructor that does not leak > resources when an exception is raised within it. ... > As you can see this is less than straight forward. Is there some kind > of best practice that I'm not aware of? Not so tough. Something like this tweaked version of your example: class Foo(object): def __init__(self, name, fail=False): self.name = name if not fail: print '%s.__init__(%s)' % (type(self).__name__, name) else: print '%s.__init__(%s), FAIL' % (type(self).__name__, name) raise ValueError('Asked to fail: %r' % fail) def close(self): print '%s.close(%s)' % (type(self).__name__, self.name) class Bar(object): def __init__(self): unwind = [] try: self.a = Foo('a') unwind.append(a) self.b = Foo('b', fail=True) unwind.append(b) ... except Exception, why: while unwind): unwind.pop().close() raise bar = Bar() --Scott David Daniels Scott.Daniels at Acm.Org From macaodha at gmail.com Mon Jul 6 17:36:37 2009 From: macaodha at gmail.com (Oisin) Date: Mon, 6 Jul 2009 14:36:37 -0700 (PDT) Subject: getting text from webpage that has embedded flash Message-ID: <8aaca352-e115-4aa8-b76d-a37a0edf7a74@h31g2000yqd.googlegroups.com> HI, Im trying to parse a bands myspace page and get the total number of plays for their songs. e.g. http://www.myspace.com/mybloodyvalentine The problem is that I cannot use urllib2 as the "Total plays" string does not appear in the page source. Any idea of ways around this? Thanks, O From michaelmossey at gmail.com Mon Jul 6 17:37:53 2009 From: michaelmossey at gmail.com (Michael Mossey) Date: Mon, 6 Jul 2009 14:37:53 -0700 (PDT) Subject: Catching control-C Message-ID: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> What is required in a python program to make sure it catches a control- c on the command-line? Do some i/o? The OS here is Linux. Thanks, Mike From clp2 at rebertia.com Mon Jul 6 17:44:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 14:44:20 -0700 Subject: Catching control-C In-Reply-To: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <50697b2c0907061444x1a361d47j7c3eeb610431a8ba@mail.gmail.com> On Mon, Jul 6, 2009 at 2:37 PM, Michael Mossey wrote: > What is required in a python program to make sure it catches a control- > c on the command-line? Do some i/o? The OS here is Linux. try: #code that reads input except KeyboardInterrupt: #Ctrl-C was pressed Cheers, Chris -- http://blog.rebertia.com From philip at semanchuk.com Mon Jul 6 17:47:57 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 17:47:57 -0400 Subject: Catching control-C In-Reply-To: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <928186A7-E76E-4BF3-8696-DC61ED025B99@semanchuk.com> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > What is required in a python program to make sure it catches a > control- > c on the command-line? Do some i/o? The OS here is Linux. You can use a try/except to catch a KeyboardInterrupt exception, or you can trap it using the signal module: http://docs.python.org/library/signal.html You want to trap SIGINT. HTH Philip From michaelmossey at gmail.com Mon Jul 6 18:02:26 2009 From: michaelmossey at gmail.com (Michael Mossey) Date: Mon, 6 Jul 2009 15:02:26 -0700 (PDT) Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 6, 2:47?pm, Philip Semanchuk wrote: > On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > > > What is required in a python program to make sure it catches a ? > > control- > > c on the command-line? Do some i/o? The OS here is Linux. > > You can use a try/except to catch a KeyboardInterrupt exception, or ? > you can trap it using the signal module:http://docs.python.org/library/signal.html > > You want to trap SIGINT. > > HTH > Philip Thanks to both of you. However, my question is also about whether I need to be doing i/o or some similar operation for my program to notice in any shape or form that Control-C has been pressed. In the past, I've written Python programs that go about their business ignoring Ctrl-C. Other programs respond to it immediately by exiting. I think the difference is that the latter programs are doing i/o. But I want to understand better what the "secret" is to responding to a ctrl-C in any shape or form. For example, does trapping SIGINT always work, regardless of what my process is doing? Thanks, Mike From nile_mcadams at yahoo.com Mon Jul 6 18:02:45 2009 From: nile_mcadams at yahoo.com (Nile) Date: Mon, 6 Jul 2009 15:02:45 -0700 (PDT) Subject: Semi-Newbie needs a little help Message-ID: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> I am trying to write a simple little program to do some elementary stock market analysis. I read lines, send each line to a function and then the function returns a date which serves as a key to a dictionary. Each time a date is returned I want to increment the value associated with that date. The function seems to be working properly. By means of a print statement I have inserted just before the return value I can see there are three dates that are returned which is correct. The dictionary only seems to capture the last date. My test data consists of five stocks, each stock with five days. The correct answer would be a count of 5 for the second day, the third day, and the last day -- 11/14/2008. Here is the a code, followed by a portion of the output. I know enough to write simple little programs like this with no problems up until now but I don't know enough to figure out what I am doing wrong. Code for x in range(len(file_list)): d = open(file_list[x] , "r") data = d.readlines() k = above_or_below(data) # This function seems to work correctly print "here is the value that was returned " , k dict[k] = dict.get(k,0) + 1 dict_list = dict.values() print "here is a list of the dictionary values ", dict_list print "the length of the dictionary is ", len(dict) And here is some output Function will return k which = 11/11/2008 # These 3 lines are printed from the function just before the return Function will return k which = 11/12/2008 # This sample shows stocks 4 and 5 but 1,2,3 are the same. Function will return k which = 11/14/2008 here is the value that was returned 11/14/2008 # printed from code above - only the last day seems to be Function will return k which = 11/11/2008 # recognized. Function will return k which = 11/12/2008 Function will return k which = 11/14/2008 here is the value that was returned 11/14/2008 here is a list of the dictionary values [5] # dict has counted only the last day for 5 stocks the length of the dictionary is 1 >Exit code: 0 From philip at semanchuk.com Mon Jul 6 18:16:06 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 18:16:06 -0400 Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 6, 2009, at 6:02 PM, Michael Mossey wrote: > On Jul 6, 2:47 pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >>> What is required in a python program to make sure it catches a >>> control- >>> c on the command-line? Do some i/o? The OS here is Linux. >> >> You can use a try/except to catch a KeyboardInterrupt exception, or >> you can trap it using the signal module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH >> Philip > > Thanks to both of you. However, my question is also about whether I > need to be doing i/o or some similar operation for my program to > notice in any shape or form that Control-C has been pressed. In the > past, I've written Python programs that go about their business > ignoring Ctrl-C. Other programs respond to it immediately by exiting. > I think the difference is that the latter programs are doing i/o. But > I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. > > For example, does trapping SIGINT always work, regardless of what my > process is doing? Hi Mike, Sorry, I don't know the Python internals well enough to answer your question. Good luck Philip From clp2 at rebertia.com Mon Jul 6 18:22:54 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 15:22:54 -0700 Subject: Semi-Newbie needs a little help In-Reply-To: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> On Mon, Jul 6, 2009 at 3:02 PM, Nile wrote: > I am trying to write a simple little program to do some elementary > stock market analysis. ?I read lines, send each line to a function and > then the function returns a date which serves as a key to a > dictionary. Each time a date is returned I want to increment the value > associated with that date. The function seems to be working properly. > By means of a print statement I have inserted just before the return > value I can see there are three dates that are returned which is > correct. ?The dictionary only seems to capture the last date. My test > data consists of five stocks, each stock with five days. The correct > answer would be a count of 5 for the second day, the third day, and > the last day -- 11/14/2008. > > Here is the a code, followed by a portion of the output. ?I know > enough to write simple little programs like this with no problems up > until now but I don't know enough to figure out what I am doing > wrong. > ? ?for x in range(len(file_list)): for filename in file_list: #I'm assuming the lack of indentation on the subsequent lines is a mere transcription error... > ? ?d = open(file_list[x] , "r") d = open(filename , "r") > ? ?data = d.readlines() > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > function seems to work correctly > ? ?print "here is the value that was returned " , k > ? ?dict[k] = dict.get(k,0) + 1 `dict` is the name of a builtin type. Please rename this variable to avoid shadowing the type. Also, where is this variable even initialized? It's not in this code snippet you gave. Further, I would recommend using a defaultdict (http://docs.python.org/dev/library/collections.html#collections.defaultdict) rather than a regular dictionary; this would make the count-incrementing part nicer. Taking these changes into account, your code becomes: from collections import defaultdict counts = defaultdict(lambda: 0) for filename in file_list: d = open(filename , "r") data = d.readlines() k = above_or_below(data) # This function seems to work correctly print "here is the value that was returned " , k counts[k] += 1 values = counts.values() print "here is a list of the dictionary values ", values print "the length of the dictionary is ", len(counts) I don't immediately see what's causing your problem, but guess that it might've be related to the initialization of the `dict` variable. Cheers, Chris -- http://blog.rebertia.com From tn.pablo at gmail.com Mon Jul 6 18:30:21 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Mon, 6 Jul 2009 17:30:21 -0500 Subject: Semi-Newbie needs a little help In-Reply-To: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Mon, Jul 6, 2009 at 17:02, Nile wrote: > Code > > ? ?for x in range(len(file_list)): > ? ?d = open(file_list[x] , "r") > ? ?data = d.readlines() > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > function seems to work correctly > ? ?print "here is the value that was returned " , k > ? ?dict[k] = dict.get(k,0) + 1 > > ? ?dict_list = dict.values() > ? ?print "here is a list of the dictionary values ", dict_list > ? ?print "the length of the dictionary is ", len(dict) Correcting your indentation errors and moving your comments above the line they reference will attract more help from others in this list ;-) Also, I'd recommend limiting your line length to 80 chars, since lines are wrapped anyway. -- Pablo Torres N. From bcharrow at csail.mit.edu Mon Jul 6 18:38:12 2009 From: bcharrow at csail.mit.edu (Ben Charrow) Date: Mon, 06 Jul 2009 18:38:12 -0400 Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <4A527CD4.3010406@csail.mit.edu> Michael Mossey wrote: > On Jul 6, 2:47 pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >>> What is required in a python program to make sure it catches a control- >>> c on the command-line? Do some i/o? The OS here is Linux. >> You can use a try/except to catch a KeyboardInterrupt exception, or you >> can trap it using the signal >> module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH Philip > > Thanks to both of you. However, my question is also about whether I need to > be doing i/o or some similar operation for my program to notice in any shape > or form that Control-C has been pressed. You don't need to be doing I/O in order to raise a KeyboardIneterrupt. For example, the following program should use up a lot of your CPU until you hit Ctrl-C. >>> while True: ... pass ... ^CTraceback (most recent call last): File "", line 1, in KeyboardInterrupt > In the past, I've written Python programs that go about their business > ignoring Ctrl-C. Can you be more specific? Can you share the relevant sections of these programs? Were these programs multi-threaded? > But I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. Strictly speaking, I don't think you can always respond to a Ctrl-C in any shape or form. Quoting from the signal module: Although Python signal handlers are called asynchronously as far as the Python user is concerned, they can only occur between the "atomic" instructions of the Python interpreter. This means that signals arriving during long calculations implemented purely in C (such as regular expression matches on large bodies of text) may be delayed for an arbitrary amount of time. HTH, Ben From python at mrabarnett.plus.com Mon Jul 6 18:49:41 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 23:49:41 +0100 Subject: Semi-Newbie needs a little help In-Reply-To: <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> Message-ID: <4A527F85.7030400@mrabarnett.plus.com> Chris Rebert wrote: > On Mon, Jul 6, 2009 at 3:02 PM, Nile wrote: >> I am trying to write a simple little program to do some elementary >> stock market analysis. I read lines, send each line to a function and >> then the function returns a date which serves as a key to a >> dictionary. Each time a date is returned I want to increment the value >> associated with that date. The function seems to be working properly. >> By means of a print statement I have inserted just before the return >> value I can see there are three dates that are returned which is >> correct. The dictionary only seems to capture the last date. My test >> data consists of five stocks, each stock with five days. The correct >> answer would be a count of 5 for the second day, the third day, and >> the last day -- 11/14/2008. >> >> Here is the a code, followed by a portion of the output. I know >> enough to write simple little programs like this with no problems up >> until now but I don't know enough to figure out what I am doing >> wrong. > >> for x in range(len(file_list)): > > for filename in file_list: > #I'm assuming the lack of indentation on the subsequent lines is a > mere transcription error... > >> d = open(file_list[x] , "r") > > d = open(filename , "r") > >> data = d.readlines() >> k = above_or_below(data) # This >> function seems to work correctly >> print "here is the value that was returned " , k >> dict[k] = dict.get(k,0) + 1 > > `dict` is the name of a builtin type. Please rename this variable to > avoid shadowing the type. > Also, where is this variable even initialized? It's not in this code > snippet you gave. > Further, I would recommend using a defaultdict > (http://docs.python.org/dev/library/collections.html#collections.defaultdict) > rather than a regular dictionary; this would make the > count-incrementing part nicer. > > Taking these changes into account, your code becomes: > > from collections import defaultdict > > counts = defaultdict(lambda: 0) > Better is: counts = defaultdict(int) > for filename in file_list: > d = open(filename , "r") > data = d.readlines() > k = above_or_below(data) # This function seems to work correctly > print "here is the value that was returned " , k > counts[k] += 1 > > values = counts.values() > print "here is a list of the dictionary values ", values > print "the length of the dictionary is ", len(counts) > > > I don't immediately see what's causing your problem, but guess that it > might've be related to the initialization of the `dict` variable. > It might be that the indentation was wrong where the count is incremented, but I can't tell because none of the lines were shown indented. From p.f.moore at gmail.com Mon Jul 6 18:50:08 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 6 Jul 2009 23:50:08 +0100 Subject: Opening a SQLite database in readonly mode In-Reply-To: References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: <79990c6b0907061550o43df92c0qec1fe2c7e49b916@mail.gmail.com> 2009/7/6 Joshua Kugler : > Paul Moore wrote: >> The SQLite documentation mentions a flag, SQLITE_OPEN_READONLY, to >> open a database read only. I can't find any equivalent documented in >> the Python standard library documentation for the sqlite3 module (or, >> for that matter, on the pysqlite library's website). >> >> Is it possible to open a sqlite database in readonly mode, in Python? > > Yes, but most likely not with pysqlite. ?The python sqlite3 module in the > standard library, and the pysqlite module are both DB-API compliant, which > means they probably do not have a method to open the DB read only (as that > is usually enforced at the user permission level). > > If you want to use that flag, take a look at APSW. It is a very thin layer > on top of the SQLite C API, and thus supports everything that the C API > supports. ?It is not, however, DB API compliant, but is very close so not > hard to pick up. > > BTW, APSW is written by the same author as pysqlite. Excellent, thanks. I'll have to think whether I want the extra dependency but at least I know it's possible now. Thanks for the pointer. Paul. From nile_mcadams at yahoo.com Mon Jul 6 18:50:59 2009 From: nile_mcadams at yahoo.com (Nile) Date: Mon, 6 Jul 2009 15:50:59 -0700 (PDT) Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Jul 6, 5:30?pm, "Pablo Torres N." wrote: > On Mon, Jul 6, 2009 at 17:02, Nile wrote: > > Code > > > ? ?for x in range(len(file_list)): > > ? ?d = open(file_list[x] , "r") > > ? ?data = d.readlines() > > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > > function seems to work correctly > > ? ?print "here is the value that was returned " , k > > ? ?dict[k] = dict.get(k,0) + 1 > > > ? ?dict_list = dict.values() > > ? ?print "here is a list of the dictionary values ", dict_list > > ? ?print "the length of the dictionary is ", len(dict) > > Correcting your indentation errors and moving your comments above the > line they reference will attract more help from others in this list > ;-) > > Also, I'd recommend limiting your line length to 80 chars, since lines > are wrapped anyway. > > -- > Pablo Torres N. Yup - Sorry, first post ever - next ones will be better formatted From piet at cs.uu.nl Mon Jul 6 18:51:24 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 00:51:24 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: >>>>> Scott David Daniels (SDD) wrote: >SDD> # No need for global declarations, we alter, not replace Yes, I know, but I find it neater to do the global declarations, if only for documentation. And they don't affect the run time, only the compile time. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From matt0177 at GMAIL.COM Mon Jul 6 18:56:40 2009 From: matt0177 at GMAIL.COM (matt0177) Date: Mon, 6 Jul 2009 15:56:40 -0700 (PDT) Subject: Python Error from Apress book Message-ID: <24364269.post@talk.nabble.com> http://www.nabble.com/file/p24364269/simple_markup2.py simple_markup2.py http://www.nabble.com/file/p24364269/util2.py util2.py The two above files are from chapter 20 of the Apress book "Beginning Python >From Novice to Professional". When I try to run the command as outlined in the book "simple_markup2.py < test_input.txt > test_output.html i get the following error every time. File "C:\Python25\Scripts\simple_markup2.py", line 7, in for block in blocks(sys.stdin): File "C:\Python25\Scripts\util2.py", line 7, in blocks for line in lines(file): File "C:\Python25\Scripts\util2.py", line 2, in lines for line in file: yield line IOError: [Errno 9] Bad file descriptor I've tried this code on two different computers (both winxp, python 2.5.1) with the same results. when simple_markup2.py is run with only output, it will correctly write the headers. When you try to add an input file, you get the error every time, and I have tried every text file known to man, including the downloadable sample. This has been frustrating me for several days, any advice would be greatly appreciated. I'm just starting to learn python so troubleshooting is a sorely lacked skill atm :) Thanks, Matt -- View this message in context: http://www.nabble.com/Python-Error-from-Apress-book-tp24364269p24364269.html Sent from the Python - python-list mailing list archive at Nabble.com. From hannarosie at gmail.com Mon Jul 6 18:57:20 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Mon, 6 Jul 2009 15:57:20 -0700 Subject: Write matrix to text file Message-ID: <564970db0907061557s2c559b3cxaf52859abd2050a3@mail.gmail.com> Hi, I'm working with both python and matlab at the moment and I was wondering if there is an efficient way to take a 2-D array (of 1s and 0s) in python and write it to a text file such that matlab will be able to create a sparse matrix from it later. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Mon Jul 6 18:58:00 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 00:58:00 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> Message-ID: >>>>> Dave Angel (DA) wrote: >DA> MRAB wrote: >>>
Dave Angel >>> wrote: >>> [snip] >>>> It would probably save some time to not bother storing the zeroes in the >>>> list at all. And it should help if you were to step through a list of >>>> primes, rather than trying every possible int. Or at least constrain >>>> yourself to odd numbers (after the initial case of 2). >>>> >>> Or stop looking for more factors when you've passed the square root of >>> num. I don't know what effect there'll be on the time if you recalculate >>> the square root when num changes (expensive calculation vs smaller >>> search space). >>> >>>
>>> >DA> But if I remember the code, it stopped when the quotient is one, which is >DA> usually sooner than the square root. And no need to precalculate the >DA> square root. That's right. I thought of doing the sqrt trick by testing for num < factor, i.e." if num < factor: break but found out that it is useless, as for each factor found, num is divided by that factor as many times as possible. Therefore when the largest prime factor of num has been found, num ends up being 1 and the loop stops. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nacim_bravo at agilent.com Mon Jul 6 19:00:39 2009 From: nacim_bravo at agilent.com (nacim_bravo at agilent.com) Date: Mon, 6 Jul 2009 17:00:39 -0600 Subject: Newbie needs help In-Reply-To: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Dear Python gurus, If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? Please let me know, nacim From rhodri at wildebst.demon.co.uk Mon Jul 6 19:03:00 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:03:00 +0100 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: On Mon, 06 Jul 2009 18:41:03 +0100, jack catcher (nick) wrote: > Rhodri James kirjoitti: >> Does the webcam just deliver frames, or are you getting frames out of >> a decoder layer? If it's the latter, you want to distribute the encoded >> video, which should be much lower bandwidth. Exactly how you do that >> depends a bit on what format the webcam claims to deliver. > > Well, getting already encoded video from the webcam sounds almost like a > free lunch (which it probably is not). At least I wouldn't want to get > too long a delay because of the encoding. Not so unlikely as you might think, since there are very basic M-JPEG and MPEG-2 on-chip encoders available, and the webcam will have to do some compression to get the video data into the computer. USB is not as fast as it would like to pretend. > I haven't got the webcam(s) yet, and I guess I can basically purchase > any ones I find suitable for getting the job done. Any recommendations? Sorry, no. I'm used to getting my video feeds down fibre-optic cables :-) -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Mon Jul 6 19:05:44 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 01:05:44 +0200 Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: >>>>> Philip Semanchuk (PS) wrote: >PS> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >>> What is required in a python program to make sure it catches a control- >>> c on the command-line? Do some i/o? The OS here is Linux. >PS> You can use a try/except to catch a KeyboardInterrupt exception, or you >PS> can trap it using the signal module: >PS> http://docs.python.org/library/signal.html >PS> You want to trap SIGINT. And make sure threads don't mess up the signal handling. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rhodri at wildebst.demon.co.uk Mon Jul 6 19:16:00 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:16:00 +0100 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: On Mon, 06 Jul 2009 21:40:49 +0100, kj wrote: > In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers > writes: > >> kj a ?crit : >> (snipo >>> To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, > >> FWIW, Python has no pretention to minimalism. > > Assuming that you mean by this that Python's authors have no such > pretensions: > > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html re.match() is part of the library, not the language. The standard library is in no sense of the word small. It has a mild tendency to avoid repeating itself, but presumably the stonkingly obvious optimisation possibilities of re.match() over re.search() are considered worth the (small) increase in size. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Mon Jul 6 19:20:48 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:20:48 +0100 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Message-ID: On Tue, 07 Jul 2009 00:00:39 +0100, wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it > possible to do such in Python environment? If yes, how to do or what > syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible > to get this value in Python environment? Quite possibly, however you're going to have to give us a *lot* more information before the answers you get will be worth anything at all. How is "the certain material" represented? What simulator are you using? Which Python environment? Which Python version for that matter? We may appear to be mind-readers, but we usually need a bit more than this to work on. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Mon Jul 6 19:23:57 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:23:57 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: <4A522C4B.20102@ieee.org> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4A522C4B.20102@ieee.org> Message-ID: On Mon, 06 Jul 2009 17:54:35 +0100, Dave Angel wrote: > Rhodri James wrote: >> Indeed, arguably it's a bug for C compilers to fail to find the valid >> parsing of "++5" as "+(+5)". All I can say is that I've never even >> accidentally typed that in twenty years of C programming. >> > But the C language specifically defines the tokenizer as doing a > max-match, finding the longest legal token at any point. That's how > many things that would otherwise be ambiguous are well-defined. For > example, if you want to divide two integers, given pointers to them, you > need a space between the slash and the start. > *p1/*p2 begins a comment, while *p1/ *p2 does a division You know, I've never had that come up either! My habit of sticking spaces around binary operators purely for legibility probably helped, but I'm still a bit boggled. -- Rhodri James *-* Wildebeest Herder to the Masses From nile_mcadams at yahoo.com Mon Jul 6 19:29:36 2009 From: nile_mcadams at yahoo.com (Nile) Date: Mon, 6 Jul 2009 16:29:36 -0700 (PDT) Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Jul 6, 5:22?pm, Chris Rebert wrote: > On Mon, Jul 6, 2009 at 3:02 PM, Nile wrote: > > I am trying to write a simple little program to do some elementary > > stock market analysis. ?I read lines, send each line to a function and > > then the function returns a date which serves as a key to a > > dictionary. Each time a date is returned I want to increment the value > > associated with that date. The function seems to be working properly. > > By means of a print statement I have inserted just before the return > > value I can see there are three dates that are returned which is > > correct. ?The dictionary only seems to capture the last date. My test > > data consists of five stocks, each stock with five days. The correct > > answer would be a count of 5 for the second day, the third day, and > > the last day -- 11/14/2008. > > > Here is the a code, followed by a portion of the output. ?I know > > enough to write simple little programs like this with no problems up > > until now but I don't know enough to figure out what I am doing > > wrong. > > ? ?for x in range(len(file_list)): > > for filename in file_list: > #I'm assuming the lack of indentation on the subsequent lines is a > mere transcription error... > > > ? ?d = open(file_list[x] , "r") > > ? ? d = open(filename , "r") > > > ? ?data = d.readlines() > > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > > function seems to work correctly > > ? ?print "here is the value that was returned " , k > > ? ?dict[k] = dict.get(k,0) + 1 > > `dict` is the name of a builtin type. Please rename this variable to > avoid shadowing the type. > Also, where is this variable even initialized? It's not in this code > snippet you gave. > Further, I would recommend using a defaultdict > (http://docs.python.org/dev/library/collections.html#collections.defau...) > rather than a regular dictionary; this would make the > count-incrementing part nicer. > > Taking these changes into account, your code becomes: > > from collections import defaultdict > > counts = defaultdict(lambda: 0) > > for filename in file_list: > ? ? d = open(filename , "r") > ? ? data = d.readlines() > ? ? k = above_or_below(data) # This function seems to work correctly > ? ? print "here is the value that was returned " , k > ? ? counts[k] += 1 > > ? ? values = counts.values() > ? ? print "here is a list of the dictionary values ", values > ? ? print "the length of the dictionary is ", len(counts) > > I don't immediately see what's causing your problem, but guess that it > might've be related to the initialization of the `dict` variable. > > Cheers, > Chris > --http://blog.rebertia.com- Hide quoted text - > > - Show quoted text - I initialized the dictionary earlier in the program like this - hashtable = {} I changed the "dict" to hashtable but I still get the same result I will try to learn about the defaultdict but I'm just trying to keep it as simple as I can for now Revised code for x in range(len(file_list)): d = open(file_list[x] , "r") data = d.readlines() k = 0 k = above_or_below(data) print "here is the value that was returned ",k hashtable[k] = hashtable.get(k,0) + 1 hashtable_list = hashtable.values() print "here is a list of the dictionary values ", hashtable_list print "the length of the dictionary is ", len(hashtable) Output # The first 3 lines are printed from the function # right before the return statement. This output # snippet shows the last two stocks. The function # SAYS it is returning the correct value but only # the last date seems to make it to the hashtable Function will return k which = 11/11/2008 Function will return k which = 11/12/2008 Function will return k which = 11/14/2008 # this line is printed from the code above # I don't understand why all three dates don't # seem to make it to the main program. Only # the last date seems to be recognized here is the value that was returned 11/14/2008 Function will return k which = 11/11/2008 Function will return k which = 11/12/2008 Function will return k which = 11/14/2008 here is the value that was returned 11/14/2008 here is a list of the dictionary values [5] the length of the dictionary is 1 >Exit code: 0 From python at mrabarnett.plus.com Mon Jul 6 19:41:53 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 00:41:53 +0100 Subject: Semi-Newbie needs a little help In-Reply-To: References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <4A528BC1.4040400@mrabarnett.plus.com> Nile wrote: [snip] > I initialized the dictionary earlier in the program like this - > > hashtable = {} > > I changed the "dict" to hashtable but I still get the same result > I will try to learn about the defaultdict but I'm just trying to keep > it as simple as I can for now > > Revised code > > for x in range(len(file_list)): > d = open(file_list[x] , "r") > data = d.readlines() What's the point of the following line? > k = 0 > k = above_or_below(data) > print "here is the value that was returned ",k > hashtable[k] = hashtable.get(k,0) + 1 > > > hashtable_list = hashtable.values() > print "here is a list of the dictionary values ", hashtable_list > print "the length of the dictionary is ", len(hashtable) > > Output > # The first 3 lines are printed from the function > # right before the return statement. This output > # snippet shows the last two stocks. The function > # SAYS it is returning the correct value but only > # the last date seems to make it to the hashtable > Function will return k which = 11/11/2008 > Function will return k which = 11/12/2008 > Function will return k which = 11/14/2008 > > # this line is printed from the code above > # I don't understand why all three dates don't > # seem to make it to the main program. Only > # the last date seems to be recognized > here is the value that was returned 11/14/2008 > > Function will return k which = 11/11/2008 > Function will return k which = 11/12/2008 > Function will return k which = 11/14/2008 > here is the value that was returned 11/14/2008 > here is a list of the dictionary values [5] > the length of the dictionary is 1 >> Exit code: 0 I think there's a bug in 'above_or_below' which you haven't noticed. From gagsl-py2 at yahoo.com.ar Mon Jul 6 19:49:40 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 20:49:40 -0300 Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: En Mon, 06 Jul 2009 18:15:44 -0300, Scott David Daniels escribi?: > brasse wrote: >> I have been thinking about how write exception safe constructors in >> Python. By exception safe I mean a constructor that does not leak >> resources when an exception is raised within it. > ... > > As you can see this is less than straight forward. Is there some kind > > of best practice that I'm not aware of? > > Not so tough. Something like this tweaked version of your example: Another variant: Presumably, there is already a method in Bar responsible for "normal" cleanup; just make sure it gets called (and write it in a robust way): > class Foo(object): > def __init__(self, name, fail=False): > self.name = name > if not fail: > print '%s.__init__(%s)' % (type(self).__name__, name) > else: > print '%s.__init__(%s), FAIL' % (type(self).__name__, name) > raise ValueError('Asked to fail: %r' % fail) > > def close(self): > print '%s.close(%s)' % (type(self).__name__, self.name) class Bar(object): a = None # default values b = None def __init__(self): try: self.a = Foo('a') self.b = Foo('b', fail=True) except Exception, why: self.cleanup() raise def cleanup(self): if self.a is not None: self.a.close() if self.b is not None: self.b.close() bar = Bar() -- Gabriel Genellina From mh at pixar.com Mon Jul 6 20:21:32 2009 From: mh at pixar.com (mh at pixar.com) Date: Tue, 07 Jul 2009 00:21:32 GMT Subject: parsing times like "5 minutes ago"? Message-ID: I'm looking for something like Tcl's [clock scan] command which parses human-readable time strings such as: % clock scan "5 minutes ago" 1246925569 % clock scan "tomorrow 12:00" 1246993200 % clock scan "today + 1 fortnight" 1248135628 Does any such package exist for Python? Many TIA! Mark -- Mark Harrison Pixar Animation Studios From pavlovevidence at gmail.com Mon Jul 6 20:43:09 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 6 Jul 2009 17:43:09 -0700 (PDT) Subject: parsing times like "5 minutes ago"? References: Message-ID: On Jul 6, 5:21?pm, m... at pixar.com wrote: > I'm looking for something like Tcl's [clock scan] command which parses > human-readable time strings such as: > > ? ? % clock scan "5 minutes ago" > ? ? 1246925569 > ? ? % clock scan "tomorrow 12:00" > ? ? 1246993200 > ? ? % clock scan "today + 1 fortnight" > ? ? 1248135628 > > Does any such package exist for Python? If you're on a machine with GNU datethe simplest solution is to use it to parse the string. Q&D: def clock_scan(datestring): stdout,stderr = subprocess.Popen( ['date','+%s','--date=%s' % datestring ], stdout=subprocess.PIPE).communicate() return float(stdout) clock_scan('1 hour ago') clock_scan('next week') Carl Banks From rhodri at wildebst.demon.co.uk Mon Jul 6 20:58:07 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 01:58:07 +0100 Subject: Semi-Newbie needs a little help In-Reply-To: References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Tue, 07 Jul 2009 00:29:36 +0100, Nile wrote: > Revised code > > for x in range(len(file_list)): > d = open(file_list[x] , "r") > data = d.readlines() > k = 0 > k = above_or_below(data) > print "here is the value that was returned ",k > hashtable[k] = hashtable.get(k,0) + 1 > > > hashtable_list = hashtable.values() > print "here is a list of the dictionary values ", hashtable_list > print "the length of the dictionary is ", len(hashtable) > > Output > # The first 3 lines are printed from the function > # right before the return statement. This output > # snippet shows the last two stocks. The function > # SAYS it is returning the correct value but only > # the last date seems to make it to the hashtable > Function will return k which = 11/11/2008 > Function will return k which = 11/12/2008 > Function will return k which = 11/14/2008 Have you checked the indentation of the print statement that produces this? Is it perhaps inside a loop still? -- Rhodri James *-* Wildebeest Herder to the Masses From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:01:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:01:44 GMT Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: <006e795f$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: > kj wrote: >> I've rewritten it like this: >> >> sense = cmp(func(hi), func(lo)) >> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >> >> Thanks for your feedback! >> >> kj >> >> > As already said before, unlike other languages, sense in english does > **not** mean direction. You should rewrite this part using a better > name. Wrong informations are far worse than no information at all. Absolutely. >From Webster's Dictionary: 8. (Geom.) One of two opposite directions in which a line, surface, or volume, may be supposed to be described by the motion of a point, line, or surface. [1913 Webster] And from WordNet: 2: the meaning of a word or expression; the way in which a word or expression or situation can be interpreted Both meanings are relevant to the way KJ is using the word. Please take your own advice and stop giving wrong information. As a native English speaker, I had no difficulty understanding the meaning of "sense" in the sense intended by KJ. -- Steven From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:04:02 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:04:02 GMT Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006e79e9$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 05:47:18 -0700, Scott David Daniels wrote: > Steven D'Aprano wrote: >> ... That's the Wrong Way to do it -- >> you're using a screwdriver to hammer a nail.... > > Don't knock tool abuse (though I agree with you here). Sometimes tool > abuse can produce good results. For example, using hammers to drive > screws for temporary strong holds led to making better nails. Historically, nails were invented a long time before screws. Screws as fasteners weren't invented until the 1400s, nails were used thousands of years ago. And hammering screws makes temporary *weak* holds, not strong, because the screw only touches the sides of the hole lightly. Unless the screw has been specifically designed to be hammered, hammering screws is pretty much the definition of incompetence and/or laziness! -- Steven From chris at simplistix.co.uk Mon Jul 6 21:09:41 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 02:09:41 +0100 Subject: Where does setuptools live? In-Reply-To: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> Message-ID: <4A52A055.8080108@simplistix.co.uk> Floris Bruynooghe wrote: > It is, see > http://mail.python.org/pipermail/distutils-sig/2009-July/012374.html > > >> It's seen no changes in 9 months. > > It's setuptools... I'm sure you can find many flamefests on distutils- > sig about this. Yeah, distutils-sig is the right place to discuss. I wonder how close setuptools is to being forked because of Phil Eby's unwillingness to apply patches and/or clean up the horrible setuptools code? Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:16:41 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:16:41 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <006e7ce0$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 16:43:43 +0100, Tim Rowe wrote: > 2009/7/4 kj : > >> Precisely. ?As I've stated elsewhere, this is an internal helper >> function, to be called only a few times under very well-specified >> conditions. ?The assert statements checks that these conditions are as >> intended. ?I.e. they are checks against the module writer's programming >> errors. > > Good for you. I'm convinced that you have used the assertion > appropriately, and the fact that so many here are unable to see that > looks to me like a good case for teaching the right use of assertions. > For what it's worth, I read assertions at the beginning of a procedure > as part of the specification of the procedure, and I use them there in > order to document the procedure. An assertion in that position is for me > a statement to the user of the procedure "it's your responsibility to > make sure that you never call this procedure in such a way as to violate > these conditions". They're part of a contract, as somebody (maybe you) > pointed out. > > As somebody who works in the safety-critical domain, it's refreshing to > see somebody teaching students to think about the circumstances in which > a procedure can legitimately be called. The hostility you've received to > that idea is saddening, and indicative of why there's so much buggy > software out there. LOL. Maybe the reason for "so much buggy software" is that people inappropriately use assert, thus changing the behaviour of code depending on whether it is run with the -O flag or not. I don't know what "hostility" you're seeing. The only hostility I'm seeing is from the OP, which is bizarre considering that he asked for advice and we gave it. What I see is a bunch of people concerned that the OP is teaching novices a bad habit, namely, using assert for error checking. He claims -- angrily and over and over again -- that in his code, the assertions should never fail. Great. Good for him for knowing when to use assert. But are the novices going to learn that lesson, or will they simply learn "use assert for error checking"? -- Steven From davea at ieee.org Mon Jul 6 21:18:57 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 21:18:57 -0400 Subject: Semi-Newbie needs a little help In-Reply-To: <4A528BC1.4040400@mrabarnett.plus.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <4A528BC1.4040400@mrabarnett.plus.com> Message-ID: <4A52A281.3040101@ieee.org> MRAB wrote: >
Nile wrote: > [snip] >> I initialized the dictionary earlier in the program like this - >> >> hashtable = {} >> >> I changed the "dict" to hashtable but I still get the same result >> I will try to learn about the defaultdict but I'm just trying to keep >> it as simple as I can for now >> >> Revised code >> >> for x in range(len(file_list)): >> d = open(file_list[x] , "r") >> data = d.readlines() > > What's the point of the following line? > >> k = 0 >> k = above_or_below(data) >> print "here is the value that was returned ",k >> hashtable[k] = hashtable.get(k,0) + 1 >> >> >> hashtable_list = hashtable.values() >> print "here is a list of the dictionary values ", hashtable_list >> print "the length of the dictionary is ", len(hashtable) >> >> Output >> # The first 3 lines are printed from the function >> # right before the return statement. This output >> # snippet shows the last two stocks. The function >> # SAYS it is returning the correct value but only >> # the last date seems to make it to the hashtable > >> Function will return k which = 11/11/2008 >> Function will return k which = 11/12/2008 >> Function will return k which = 11/14/2008 >> >> # this line is printed from the code above >> # I don't understand why all three dates don't >> # seem to make it to the main program. Only >> # the last date seems to be recognized >> here is the value that was returned 11/14/2008 >> >> Function will return k which = 11/11/2008 >> Function will return k which = 11/12/2008 >> Function will return k which = 11/14/2008 >> here is the value that was returned 11/14/2008 >> here is a list of the dictionary values [5] >> the length of the dictionary is 1 >>> Exit code: 0 > > I think there's a bug in 'above_or_below' which you haven't noticed. > >
> The code supplied doesn't match the output supplied. It'd probably help if the output was actually pasted from the command window, instead of retyped with more comments than data. And of course it'd help if you actually showed us the function above_or_below(), which is probably where the bug is. If it prints three values, but returns a string, then why would you be surprised? Maybe you intended it to return a list? Each time above_or_below() it's called, it prints three lines before returning, but only returns the final value. How big is file_list? I suspect it's of length 5. And the output is shown as repeated twice, but it probably was actually five sets of data. You do know you can print hashtable, don't you? You're extracting and printing the values, but not bothering with the keys. I suggest you add a print to the entry point of above_or_below(), to match the one you have for its return. And all of these print lines should be indented. That might make it easier to interpret the output, without lots of inserted comments. DaveA From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:20:04 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:20:04 GMT Subject: Newbie needs help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <006e7dac$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 17:00:39 -0600, nacim_bravo wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it > possible to do such in Python environment? If yes, how to do or what > syntax can be used? certain_material.dielectric_constant = 1.234 > Also, I'd like to get a simulation result, like voltage, is it possible > to get this value in Python environment? Yes. -- Steven From ben.welsh at gmail.com Mon Jul 6 21:25:13 2009 From: ben.welsh at gmail.com (palewire) Date: Mon, 6 Jul 2009 18:25:13 -0700 (PDT) Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else Message-ID: <24365710.post@talk.nabble.com> In my application, I'd like to have a function that compares two values, either of which may be null, and then classify the result depending on whether one is higher or lower than the other. I find myself writing something clunky like this, and I'm curious whether anyone might know a more elegant way for parsing this out. def compare(a, b): if not a and not b: return "No data" else: if a == 0 and b == 0: return "Never had" else: if a == b: return "Stayed the same" elif a < b: return "Gained" elif a > b and b > 0: return "Lost Some" elif a > b and b == 0: return "Lost All" If there's some obvious way to search for this solution online, feel free to slap me with that. I tried digging around on Google and couldn't come up with much. Thanks much. -- View this message in context: http://www.nabble.com/Looking-for-a-slick-way-to-classify-relationship-between-two-numbers%2C-without-tons-of-if-else-tp24365710p24365710.html Sent from the Python - python-list mailing list archive at Nabble.com. From gherron at islandtraining.com Mon Jul 6 21:29:51 2009 From: gherron at islandtraining.com (Gary Herron) Date: Mon, 06 Jul 2009 18:29:51 -0700 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Message-ID: <4A52A50F.1060208@islandtraining.com> nacim_bravo at agilent.com wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? > > Please let me know, > nacim > This would be a good place for you to start: http://www.catb.org/~esr/faqs/smart-questions.html From ldo at geek-central.gen.new_zealand Mon Jul 6 21:33:18 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 07 Jul 2009 13:33:18 +1200 Subject: A Bug By Any Other Name ... References: Message-ID: In message , Terry Reedy wrote: > ... it is C, not Python, that is out of step with standard usage in math > and most languages ... And it is C that introduced "==" for equality, versus "=" for assignment, which Python slavishly followed instead of keeping "=" with its mathematical meaning and using ":=" for assignment. From david.lyon at preisshare.net Mon Jul 6 21:55:03 2009 From: david.lyon at preisshare.net (David Lyon) Date: Mon, 06 Jul 2009 21:55:03 -0400 Subject: Where does setuptools =?UTF-8?Q?live=3F?= In-Reply-To: <4A52A055.8080108@simplistix.co.uk> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> Message-ID: On Tue, 07 Jul 2009 02:09:41 +0100, Chris Withers wrote: > I wonder how close setuptools is to being forked because of Phil Eby's > unwillingness to apply patches and/or clean up the horrible setuptools > code? setuptools... as far as I can see isn't actually installed until you install easyinstall... Pip (http://pypi.python.org/pypi/pip) and enstall (http://pypi.python.org/pypi/Enstaller/3.1.0) seem to be forks of setuptools already... So it looks like it's already been forked to some degree.. What hasn't happened is enough testing of pypi packages and installing with setuptools/pip/enstall from pypi. If the xmlrpc links actually "worked" on pypi... http://wiki.python.org/moin/PyPiXmlRpc?action=show&redirect=CheeseShopXmlRpc ie.. that they would allow a developer to download a packages via rpc.... then this whole issue could probably be fixed more easily... ok - the process isn't perfect... but there's room for improvement... David From rhodri at wildebst.demon.co.uk Mon Jul 6 22:11:43 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 03:11:43 +0100 Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else In-Reply-To: <24365710.post@talk.nabble.com> References: <24365710.post@talk.nabble.com> Message-ID: On Tue, 07 Jul 2009 02:25:13 +0100, palewire wrote: > In my application, I'd like to have a function that compares two values, > either of which may be null, and then classify the result depending on > whether one is higher or lower than the other. > > I find myself writing something clunky like this, and I'm curious whether > anyone might know a more elegant way for parsing this out. It depends on what you want to do with the information. If you're doing different things on each case, you might as well do it with an if-chain, though you could make it a bit tidier: > def compare(a, b): > if not a and not b: Did you mean "if a is None or b is None:" here? > return "No data" > else: > if a == 0 and b == 0: This will never fire, because it will always be caught by the test above. > return "Never had" > else: > if a == b: > return "Stayed the same" > elif a < b: > return "Gained" > elif a > b and b > 0: > return "Lost Some" > elif a > b and b == 0: > return "Lost All" def compare(a, b): if a is not None and b is not None: return "No data" elif a == 0 and b == 0: return "Never had" elif a == b: return "Stayed the same" elif a < b: return "Gained" elif b > 0: return "Lost Some" return "Lost All" Alternatively you can do something like the (deprecated) cmp and a dispatch table: CMP_TABLE = [ "Gained", "Stayed the same", "Lost Some", "Lost All"] def compare(a, b): if a is not None or b is not None: return "No data" c = cmp(a, b) + 1 if c == 2 and b == 0: c = 3 return CMP_TABLE[c] There's no cmp() in Python 3.x so you'd have to roll your own. At that point you might as well revert to the if-chain, since it'll be a lot easier to understand when you come back to it in six months time. -- Rhodri James *-* Wildebeest Herder to the Masses From gagsl-py2 at yahoo.com.ar Mon Jul 6 22:18:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 23:18:37 -0300 Subject: Python Error from Apress book References: <24364269.post@talk.nabble.com> Message-ID: En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 escribi?: > When I try to run the command as outlined in > the book "simple_markup2.py < test_input.txt > test_output.html i get the > following error every time. > > IOError: [Errno 9] Bad file descriptor That's a Windows problem. When you execute the script as itself (either as you do in the command line, or by double-clicking on it), it doesn't have valid standard handles. You have to invoke Python explicitely: python simple_markup2.py < test_input.txt > test_output.html (you may need to specify the full path to python.exe, or add the directory where Python is installed to your system PATH). -- Gabriel Genellina From fetchinson at googlemail.com Mon Jul 6 22:48:39 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Mon, 6 Jul 2009 19:48:39 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <006da5a2$0$9711$c3e8da3@news.astraweb.com> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: >>>> I wonder how many people have been tripped up by the fact that >>>> >>>> ++n >>>> >>>> and >>>> >>>> --n >>>> >>>> fail silently for numeric-valued n. >>> >>> What do you mean, "fail silently"? They do exactly what you should >>> expect: >>>>>> ++5 # positive of a positive number is positive >>> >>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>> that are buggy, not Python. >> >> Well, those expectations are taken seriously when new features are >> introduced into the language - and sometimes the feature is dismissed >> just because it would be confusing for some. If a += 1 works, expecting >> ++a to have the same meaning is very reasonable (for those coming from >> languages with a ++ operator, like C or Java) - more when ++a is a >> perfectly valid expression. If this issue isn't listed under the various >> "Python gotchas" articles, it should... > > The fact that it isn't suggests strongly to me that it isn't that common > a surprise even for Java and C programmers. This is the first time I've > seen anyone raise it as an issue. > > There are plenty of other languages other than Java and C. If we start > listing every feature of Python that's different from some other > language, we'll never end. Yes, there are plenty of languages other than Java and C, but the influence of C is admittedly huge in Python. Why do you think loops are called "for", conditionals "if" or "while", functions return via "return", loops terminate via "break" and keep going via "continue" and why is comparison written as "==", etc, etc? All of these are coming from C (or an even earlier language) and my point is that users are most of time correct when they assume that something will work the same way as in C. So I'd think that putting a warning in a FAQ or a Python Gotchas list about ++n would be very useful for many users. And it does not imply that the differences from every other language should be documented in a similar fashion. Only C :) Cheers, Daniel > For what it's worth, Ruby appears to behave the same as Python: > > $ irb > irb(main):001:0> n = 5 > => 5 > irb(main):002:0> ++n > => 5 > irb(main):003:0> --n > => 5 > irb(main):004:0> -+n > => -5 > -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From astan.chee at al.com.au Mon Jul 6 22:58:24 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 7 Jul 2009 12:58:24 +1000 Subject: copytree with timeout in windows Message-ID: <4A52B9D0.9040608@al.com.au> Hi, I'm trying to modify the copytree function in shutil so that any file being copied does not take more than 5 minutes (if it does, skip to the next one). This is what I have so far: import shutil import signal, os def handler(signum, frame): print 'Signal handler called with signal', signum raise IOError, "Couldn't open device!" signal.signal(signal.SIGALRM, handler) def copytree(src, dst): names = os.listdir(src) os.makedirs(dst) signal.alarm(0) for name in names: if name in ignored_names: continue srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if os.path.isdir(srcname): copytree(srcname, dstname, symlinks, ignore) else: signal.alarm(300) shutil.copy2(srcname, dstname) except (IOError, os.error), why: print str(why) # catch the Error from the recursive copytree so that we can # continue with other files except Error, err: print str(err.args[0]) source = "c:\\testwa\\" destination = "d:\\testwa\\" copytree(source,destination) Then I re-read the documentation which said that signal.SIGALRM is unix/linux only. How do I go about doing this in windows? Thanks for any help. Cheers Astan From rocky at gnu.org Mon Jul 6 23:13:09 2009 From: rocky at gnu.org (rocky) Date: Mon, 6 Jul 2009 20:13:09 -0700 (PDT) Subject: module name versus function name resolution conflict. Message-ID: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Someone recently reported a problem in pydb where a function defined in his program was conflicting with a module name that pydb uses. I think I understand what's wrong, but I don't have any elegant solutions to the problem. Suggestions would be appreciated. In a nutshell, here's the problem: In file fns: def foo(): print "foo" In file pdebug.py: import fns, sys def trace_dispatch(frame, event, arg): fns.foo() print frame, event, arg return trace_dispatch sys.settrace(trace_dispatch) execfile('myprogram.py') Finally file myprogram.py: def fns(): print "This is the *other* fns" When you run pdebug.py you get: $ python pdebug.py foo call None foo line None Traceback (most recent call last): File "pdebug.py", line 7, in execfile('myprogram.py') File "myprogram.py", line 1, in def fns(): print "This is the *other* fns" File "pdebug.py", line 3, in trace_dispatch fns.foo() AttributeError: 'function' object has no attribute 'foo' Basically inside the trace hook, local functions are visible and take precedence over (global) module names. I could move "import fns" inside trace_dispatch(), but I'd have to do that in all of the methods that module "fns" is used. Also note that using the form: from fns import foo would eliminate the conflict on "fns", but I'd still have potential conflicts on "foo". Using more obscure names (e.g. pydb_fns) would reduce the chance of a conflict but not eliminate it. Suggestions? From davea at ieee.org Mon Jul 6 23:28:39 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 23:28:39 -0400 Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else In-Reply-To: <24365710.post@talk.nabble.com> References: <24365710.post@talk.nabble.com> Message-ID: <4A52C0E7.9060502@ieee.org> palewire wrote: > In my application, I'd like to have a function that compares two values, > either of which may be null, and then classify the result depending on > whether one is higher or lower than the other. > > I find myself writing something clunky like this, and I'm curious whether > anyone might know a more elegant way for parsing this out. > > def compare(a, b): > if not a and not b: > return "No data" > else: > if a == 0 and b == 0: > return "Never had" > else: > if a == b: > return "Stayed the same" > elif a < b: > return "Gained" > elif a > b and b > 0: > return "Lost Some" > elif a > b and b == 0: > return "Lost All" > > If there's some obvious way to search for this solution online, feel free to > slap me with that. I tried digging around on Google and couldn't come up > with much. > > Thanks much. > Before one can "optimize" a function, one needs to get the function correct. Looks to me that if a and b are both zero, it'll say "No data" and never get to the "Never had" test. And if a is positive and b is None, it'll return None, rather than any string. Anyway, any "optimized" construct is going to be nearly as big as this. You'd have to define a list of strings, and then compute an index value that gave you the appropriate value. While I could probably come up with such, I hardly think it's worthwhile. The cases are confusing enough to me that I'd not want to make the code any less readable by some formula & lookup. That brings us to the question of what is more elegant. To me the elegant code is readable first, and efficient (space & time) second. I suspect efficiency would be nearly optimal by careful ordering of the nested ifs. Find the most likely cases, and test for them first. If this is a programming puzzle, for entertainment purposes, here's the way I'd tackle making it "efficient" obfuscated code. Start by checking if either value is None. Something like: table = ("Lost Some","Lost All","Gained","Gained","Stayed the same","Never had, never will") def compare2(a,b): global table if a is None or b is None: return "Invalid data" return table[4*(a==b)+2*(a Message-ID: On Jul 6, 8:43?pm, Carl Banks wrote: > On Jul 6, 5:21?pm, m... at pixar.com wrote: > > > I'm looking for something like Tcl's [clock scan] command which parses > > human-readable time strings such as: > > > ? ? % clock scan "5 minutes ago" > > ? ? 1246925569 > > ? ? % clock scan "tomorrow 12:00" > > ? ? 1246993200 > > ? ? % clock scan "today + 1 fortnight" > > ? ? 1248135628 > > > Does any such package exist for Python? > > If you're on a machine with GNU datethe simplest solution is to use it > to parse the string. > > Q&D: > > def clock_scan(datestring): > ? ? stdout,stderr = subprocess.Popen( > ? ? ? ? ['date','+%s','--date=%s' % datestring ], > ? ? ? ? stdout=subprocess.PIPE).communicate() > ? ? return float(stdout) > > clock_scan('1 hour ago') > clock_scan('next week') > > Carl Banks The timelib module might do what you want http://pypi.python.org/pypi/timelib/0.2 From tjreedy at udel.edu Mon Jul 6 23:50:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 23:50:08 -0400 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: kj wrote: > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html > > So there. small != minimal > > BTW, that's just one example. I've seen similar sentiments expressed > by Guido over and over and over: any new proposed enhancement to > Python must be good enough in his mind to justify cluttering the > language. That attitude counts as minimalism in my book. > > The best explanation I have found so far for re.match is that it > is an unfortunate bit of legacy, something that would not be there > if the design of Python did not have to be mindful of keeping old > code chugging along... It is possible that someone proposed removing re.match for 3.0, but I do not remember any such discussion. Some things were dropped when that contraint was (teporarily) dropped. tjr From gagsl-py2 at yahoo.com.ar Mon Jul 6 23:51:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 00:51:07 -0300 Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> <4A527F85.7030400@mrabarnett.plus.com> Message-ID: En Mon, 06 Jul 2009 19:49:41 -0300, MRAB escribi?: > Chris Rebert wrote: >> from collections import defaultdict >> counts = defaultdict(lambda: 0) >> > Better is: > counts = defaultdict(int) For speed? This is even faster: zerogen = itertools.repeat(0).next counts = defaultdict(zerogen) -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 6 23:51:09 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 00:51:09 -0300 Subject: How to map size_t using ctypes? References: <7belo1F21fs3vU1@mid.uni-berlin.de> Message-ID: En Mon, 06 Jul 2009 13:29:21 -0300, Philip Semanchuk escribi?: > On Jul 6, 2009, at 12:10 PM, Diez B. Roggisch wrote: >> Philip Semanchuk wrote: >> >>> I can't figure out how to map a C variable of size_t via Python's >>> ctypes module. >> >> from ctypes import c_size_t > > D'oh! [slaps forehead] > > That will teach me to RTFM. [...] You'd be surprised at the amount of > Googling I did without learning this on my own. Yep, seems like these terms in particular are hard to find. Searching for +ctypes +size_t, the first "good" result comes only at page 3. Bad luck... :( -- Gabriel Genellina From aahz at pythoncraft.com Tue Jul 7 00:02:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 6 Jul 2009 21:02:19 -0700 Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: >On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >> kj wrote: >>> >>> sense = cmp(func(hi), func(lo)) >>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >> >> As already said before, unlike other languages, sense in english does >> **not** mean direction. You should rewrite this part using a better >> name. Wrong informations are far worse than no information at all. > >Absolutely. > >From Webster's Dictionary: > > 8. (Geom.) One of two opposite directions in which a line, > surface, or volume, may be supposed to be described by the > motion of a point, line, or surface. > [1913 Webster] > > >And from WordNet: > > 2: the meaning of a word or expression; the way in which a word > or expression or situation can be interpreted > >Both meanings are relevant to the way KJ is using the word. Please take >your own advice and stop giving wrong information. As a native English >speaker, I had no difficulty understanding the meaning of "sense" in the >sense intended by KJ. As another native English speaker, I agree with Jean-Michel; this is the first time I've seen "sense" used to mean direction. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From mail at timgolden.me.uk Tue Jul 7 00:11:30 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Jul 2009 05:11:30 +0100 Subject: copytree with timeout in windows In-Reply-To: <4A52B9D0.9040608@al.com.au> References: <4A52B9D0.9040608@al.com.au> Message-ID: <4A52CAF2.7010707@timgolden.me.uk> Astan Chee wrote: > Hi, > I'm trying to modify the copytree function in shutil so that any file > being copied does not take more than 5 minutes (if it does, skip to the > next one). One suggestion is to use the CopyFileEx API exposed in the win32file module from pywin32. That allows for a callback function which can abort the copy (or issue a progress meter, etc.). Obviously, you'd have to roll your own copytree, but that's not beyond the wit of man. TJG From me at haolian.org Tue Jul 7 00:38:10 2009 From: me at haolian.org (Hao Lian) Date: Tue, 07 Jul 2009 00:38:10 -0400 Subject: a little wsgi framework In-Reply-To: References: Message-ID: <6a368$4a52d120$a652c80b$16349@ALLTEL.NET> timmyt wrote: > i'm interested in getting opinions on a small wsgi framework i > assembled from webob, sqlalchemy, genshi, and various code fragments i > found on the inter-tubes > > here is the interesting glue - any comments / suggestions would be > much appreciated Fun! Since you're already using WebOb, you should give webob.Response a try, which will handle your headers in a sexier way. (If you start using paste.httpexceptions and the middleware, you can avoid manually sending headers altogether.) http://pythonpaste.org/webob/reference.html#response-as-a-wsgi-application http://pythonpaste.org/modules/httpexceptions.html#paste.httpexceptions.HTTPExceptionHandler timmyt wrote: > global config > > try: > return_dict = target(request, start_response) > return_string = template.generate(**return_dict).render > (method) > config['database.Session'].commit() > except: > config['database.Session'].rollback() > raise > finally: > config['database.Session'].remove() The config global probably isn't thread-safe depending on what you're doing with the application. A safer way to go would be use Paste Registry like Pylons does with its globals or just attach it to Request. http://pythonpaste.org/webob/reference.html#ad-hoc-attributes From lie.1296 at gmail.com Tue Jul 7 00:51:51 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 04:51:51 GMT Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: Chris Rebert wrote: > On Mon, Jul 6, 2009 at 1:29 AM, Lawrence > D'Oliveiro wrote: >> In message , Tim Golden >> wrote: >> >>> The difficulty here is knowing where to put such a warning. >>> You obviously can't put it against the "++" operator as such >>> because... there isn't one. >> This bug is an epiphenomenon. :) > > Well, like I suggested, it /could/ be made an operator (or rather, a > lexer token) which just causes a compile/parse error. > > Cheers, > Chris There are edge cases (level: very rare) where you legitimately want to actually do that, e.g.: class A(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return B(self) def __neg__(self): return D(self) class B(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return C(self) def __neg__(self): return A(self) class C(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return D(self) def __neg__(self): return B(self) class D(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return A(self) def __neg__(self): return C(self) def cons(val): class E(object): value = val return E() >>> a = A(cons(10)) >>> +a <__main__.B object at 0x7fbf723c8690> >>> ++a <__main__.C object at 0x7fbf723c8710> >>> +++a <__main__.D object at 0x7fbf723c8710> >>> --a <__main__.C object at 0x7fbf723c8710> From fetchinson at googlemail.com Tue Jul 7 00:52:40 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Mon, 6 Jul 2009 21:52:40 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: >> Yes, there are plenty of languages other than Java and C, but the >> influence of C is admittedly huge in Python. Why do you think loops >> are called "for", conditionals "if" or "while", functions return via >> "return", loops terminate via "break" and keep going via "continue" >> and why is comparison written as "==", etc, etc? All of these are >> coming from C (or an even earlier language) and my point is that users > > for, if, and return were common keywords in FORTRAN. > > Not to mention BASIC > > Both of which predate C Yes, hence my comment above, ".... coming from C (or an even earlier language) ......". Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:05:12 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:05:12 GMT Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006eb26f$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 21:02:19 -0700, Aahz wrote: > In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, Steven D'Aprano > wrote: >>On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >>> kj wrote: >>>> >>>> sense = cmp(func(hi), func(lo)) >>>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >>> >>> As already said before, unlike other languages, sense in english does >>> **not** mean direction. You should rewrite this part using a better >>> name. Wrong informations are far worse than no information at all. >> >>Absolutely. >> >>From Webster's Dictionary: >> >> 8. (Geom.) One of two opposite directions in which a line, >> surface, or volume, may be supposed to be described by the motion >> of a point, line, or surface. >> [1913 Webster] >> >> >>And from WordNet: >> >> 2: the meaning of a word or expression; the way in which a word >> or expression or situation can be interpreted >> >>Both meanings are relevant to the way KJ is using the word. Please take >>your own advice and stop giving wrong information. As a native English >>speaker, I had no difficulty understanding the meaning of "sense" in the >>sense intended by KJ. > > As another native English speaker, I agree with Jean-Michel; this is the > first time I've seen "sense" used to mean direction. Just goes to show you learn something new all the time. http://www.merriam-webster.com/dictionary/sense 7: one of two opposite directions especially of motion (as of a point, line, or surface) http://dictionary.reference.com/browse/sense 18. Mathematics. one of two opposite directions in which a vector may point. Paraphrasing the Collins Dictionary of Mathematics: The sense of a vector is the sign of the measure, contrasted with the magnitude. Thus the vectors AB and BA have the same direction but opposite sense. Sense is also used to distinguish clockwise and anti- clockwise. Sense is, if you like, a "signed direction". "Towards north" (say) as opposed to "along the north-south axis". -- Steven From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:07:08 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:07:08 GMT Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <006eb2e3$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 15:02:26 -0700, Michael Mossey wrote: > On Jul 6, 2:47?pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >> > What is required in a python program to make sure it catches a >> > control- >> > c on the command-line? Do some i/o? The OS here is Linux. >> >> You can use a try/except to catch a KeyboardInterrupt exception, or you >> can trap it using the signal >> module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH >> Philip > > Thanks to both of you. However, my question is also about whether I need > to be doing i/o or some similar operation for my program to notice in > any shape or form that Control-C has been pressed. In the past, I've > written Python programs that go about their business ignoring Ctrl-C. I bet that somewhere in your code you have something like: for x in really_big_list: try: long_running_process(x) except: continue If that's what you're doing, stop! The correct way is: for x in really_big_list: try: long_running_process(x) except Exception: # Let KeyboardInterrupt and SystemExit through. continue or even: for x in really_big_list: try: long_running_process(x) except (KeyboardInterrupt, SystemExit): print "User requested exit... shutting down now" cleanup() raise except Exception: continue -- Steven From lie.1296 at gmail.com Tue Jul 7 01:13:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 05:13:28 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: Aahz wrote: > In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, > Steven D'Aprano wrote: >> On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >>> kj wrote: >>>> sense = cmp(func(hi), func(lo)) >>>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >>> As already said before, unlike other languages, sense in english does >>> **not** mean direction. You should rewrite this part using a better >>> name. Wrong informations are far worse than no information at all. >> Absolutely. >> >>From Webster's Dictionary: >> 8. (Geom.) One of two opposite directions in which a line, >> surface, or volume, may be supposed to be described by the >> motion of a point, line, or surface. >> [1913 Webster] >> >> >> And from WordNet: >> >> 2: the meaning of a word or expression; the way in which a word >> or expression or situation can be interpreted >> >> Both meanings are relevant to the way KJ is using the word. Please take >> your own advice and stop giving wrong information. As a native English >> speaker, I had no difficulty understanding the meaning of "sense" in the >> sense intended by KJ. > > As another native English speaker, I agree with Jean-Michel; this is the > first time I've seen "sense" used to mean direction. When people are fighting over things like `sense`, although sense may not be strictly wrong dictionary-wise, it smells of something burning... From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:13:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:13:30 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <006eb461$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 04:51:51 +0000, Lie Ryan wrote: > Chris Rebert wrote: >> On Mon, Jul 6, 2009 at 1:29 AM, Lawrence >> D'Oliveiro wrote: >>> In message , Tim >>> Golden wrote: >>> >>>> The difficulty here is knowing where to put such a warning. You >>>> obviously can't put it against the "++" operator as such because... >>>> there isn't one. >>> This bug is an epiphenomenon. :) >> >> Well, like I suggested, it /could/ be made an operator (or rather, a >> lexer token) which just causes a compile/parse error. >> >> Cheers, >> Chris > > There are edge cases (level: very rare) where you legitimately want to > actually do that, e.g.: Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. Help on method __pos__ in module decimal: __pos__(self, context=None) unbound decimal.Decimal method Returns a copy, unless it is a sNaN. Rounds the number (if more then precision digits) -- Steven From casevh at gmail.com Tue Jul 7 01:16:39 2009 From: casevh at gmail.com (casevh) Date: Mon, 6 Jul 2009 22:16:39 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: Message-ID: I discovered a serious bug with comparisons and have posted alpha2 which fixes that bug and adds Unicode support for Python 2.x casevh From clp2 at rebertia.com Tue Jul 7 01:18:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 22:18:20 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <006eb461$0$9711$c3e8da3@news.astraweb.com> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <006eb461$0$9711$c3e8da3@news.astraweb.com> Message-ID: <50697b2c0907062218t34c011ecna831de8aa441d8e1@mail.gmail.com> On Mon, Jul 6, 2009 at 10:13 PM, Steven D'Aprano wrote: > On Tue, 07 Jul 2009 04:51:51 +0000, Lie Ryan wrote: > >> Chris Rebert wrote: >>> On Mon, Jul 6, 2009 at 1:29 AM, Lawrence >>> D'Oliveiro wrote: >>>> In message , Tim >>>> Golden wrote: >>>> >>>>> The difficulty here is knowing where to put such a warning. You >>>>> obviously can't put it against the "++" operator as such because... >>>>> there isn't one. >>>> This bug is an epiphenomenon. :) >>> >>> Well, like I suggested, it /could/ be made an operator (or rather, a >>> lexer token) which just causes a compile/parse error. >>> >>> Cheers, >>> Chris >> >> There are edge cases (level: very rare) where you legitimately want to >> actually do that, e.g.: > > Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. > > > Help on method __pos__ in module decimal: > > __pos__(self, context=None) unbound decimal.Decimal method > ? ?Returns a copy, unless it is a sNaN. > > ? ?Rounds the number (if more then precision digits) Well, yes, but when would you apply it twice in a row? (Not that I strongly support the prohibition idea, just thought it should be brought up) Cheers, Chris -- http://blog.rebertia.com From mensanator at aol.com Tue Jul 7 01:47:21 2009 From: mensanator at aol.com (Mensanator) Date: Mon, 6 Jul 2009 22:47:21 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: Message-ID: On Jul 7, 12:16?am, casevh wrote: > I discovered a serious bug with comparisons and have posted alpha2 > which fixes that bug and adds Unicode support for Python 2.x > > casevh Damn! I was just congatulating myself for pulling off a hat trick (there had been no point in downloading 3.x without gmpy so I have been putting it off): - installing Python 3.1 - installing gmpy 1.10 - converting my Collatz Function library to 3.1 syntax And it all worked smoothly, just had to add parentheses to my print statements, change xrange to range and all my / to // (the library is exclusively integer). I had gmpy running in my library on 3.1 in about 10 minutes. So I'll have to re-do the gmpy install. Shouldn't be any big deal. I started doing some real world tests. Generally, things look good (nothing crashes, timing looks not bad) but I'm getting some funny results on one of my tests, so I'll report back when I have more information. From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:57:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:57:14 GMT Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006ebea2$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: > When people are fighting over things like `sense`, although sense may > not be strictly wrong dictionary-wise, it smells of something burning... That would be my patience. I can't believe the direction this discussion has taken. Anybody sensible would be saying "Oh wow, I've just learned a new meaning to the word, that's great, I'm now less ignorant than I was a minute ago". But oh no, we mustn't use a standard meaning to a word, heaven forbid we disturb people's ignorance by teaching them something new. It's as simple as this: using `sense` as a variable name to record the sense of a function is not a code smell, any more than using `flag` to record a flag would be, or `sign` to record the sign of an object. If you don't know the appropriate meanings of the words sense, flag or sign, learn them, don't dumb down my language. -- Steven From nomail at thank.you Tue Jul 7 02:01:39 2009 From: nomail at thank.you (jack catcher (nick)) Date: Tue, 07 Jul 2009 09:01:39 +0300 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: <6xB4m.21104$vi5.8567@uutiset.elisa.fi> Nobody kirjoitti: > On Mon, 06 Jul 2009 20:41:03 +0300, jack catcher (nick) wrote: > >>> Does the webcam just deliver frames, or are you getting frames out of >>> a decoder layer? If it's the latter, you want to distribute the encoded >>> video, which should be much lower bandwidth. Exactly how you do that >>> depends a bit on what format the webcam claims to deliver. >> Well, getting already encoded video from the webcam sounds almost like a >> free lunch (which it probably is not). At least I wouldn't want to get >> too long a delay because of the encoding. >> >> I haven't got the webcam(s) yet, and I guess I can basically purchase >> any ones I find suitable for getting the job done. Any recommendations? > > The webcam is bound to do some encoding; most of them use USB "full speed" > (12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. > > Chroma subsampling and JPEG compression will both reduce the bandwidth > without introducing noticable latency (the compression time will be no > more than one frame). > > Temporal compression will reduce the bandwidth further. Using B-frames > (frames which contain the differences from a predicted frame which is > based upon both previous and subsequent frames) will provide more > compression, but increases the latency significantly. For this reason, the > compression built into video cameras normally only uses P-frames (frames > which contain the differences from a predicted frame which is based only > upon previous frames). > > The biggest issue is likely to be finding latency specifications, followed > by finding a camera which meets your latency requirement. Thanks for the comments. Unfortunately, such specifications aren't easy to find, even in reviews. Fortunately several newer webcams seem at least to use usb2. Regarding Python, I'll have to check whether the ImageCapture extension works at all, as someone suggested earlier, and the possibility of using DirectShow. I'm still considering Matlab as an option for Python here. From ptmcg at austin.rr.com Tue Jul 7 02:02:44 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Mon, 6 Jul 2009 23:02:44 -0700 (PDT) Subject: parsing times like "5 minutes ago"? References: Message-ID: <90911ef7-ff17-4aa0-9816-4579a638db92@j32g2000yqh.googlegroups.com> On Jul 6, 7:21?pm, m... at pixar.com wrote: > I'm looking for something like Tcl's [clock scan] command which parses > human-readable time strings such as: > > ? ? % clock scan "5 minutes ago" > ? ? 1246925569 > ? ? % clock scan "tomorrow 12:00" > ? ? 1246993200 > ? ? % clock scan "today + 1 fortnight" > ? ? 1248135628 > > Does any such package exist for Python? > > Many TIA! > Mark > > -- > Mark Harrison > Pixar Animation Studios I've been dabbling with such a parser with pyparsing - here is my progress so far: http://pyparsing.wikispaces.com/UnderDevelopment It parses these test cases: today tomorrow yesterday in a couple of days a couple of days from now a couple of days from today in a day 3 days ago 3 days from now a day ago now 10 minutes ago 10 minutes from now in 10 minutes in a minute in a couple of minutes 20 seconds ago in 30 seconds 20 seconds before noon 20 seconds before noon tomorrow noon midnight noon tomorrow -- Paul From wsysdu at gmail.com Tue Jul 7 02:06:13 2009 From: wsysdu at gmail.com (Eric Wong) Date: Tue, 07 Jul 2009 14:06:13 +0800 Subject: why PyObject_VAR_HEAD? References: Message-ID: kio wrote: > Hi, > > I'm studying the CPython source code. I don?t quite understand why > they?re using PyObject_VAR_HEAD to define struct like PyListObject. To > define such kind of struct, could I use _PyObject_HEAD_EXTRA as a > header and add "items" pointer and "allocated" count explicity? Is > there any difference? > > Thanks. > > Eric first, _PyObject_HEAD_EXTRA is only useful for debug and defined as: #ifdef Py_TRACE_REFS #define _PyObject_HEAD_EXTRA \ struct _object *_ob_next; \ struct _object *_ob_prev; #define _PyObject_EXTRA_INIT 0, 0, #else #define _PyObject_HEAD_EXTRA #define _PyObject_EXTRA_INIT #endif and PyObject_HEAD is defined as #define PyObject_HEAD \ _PyObject_HEAD_EXTRA \ int ob_refcnt; \ struct _typeobject *ob_type; PyObject_VAR_HEAD defined as #define PyObject_VAR_HEAD \ PyObject_HEAD \ int ob_size; so you can see the differences between them here. PyListObject is defined as typedef struct{ PyObject_VAR_HEAD PyObject **ob_item; int allocated; }PyListObject; After unfolding these macros we can get typedef struct{ _PyObject_HEAD_EXTRA int ob_refcnt; struct _typeobject *ob_type; int ob_size; PyObject **ob_item; int allocated; }PyListObject; So, if we don't use macros, we have to specify not only the "item" and "allocated", but also the ref count and type information of the object and the size info for a variable object. These information is not only used by PyListObject, but also PyStringObject and PyDictObject, so macros make it convenient and consize. From rogerb at rogerbinns.com Tue Jul 7 02:16:10 2009 From: rogerb at rogerbinns.com (Roger Binns) Date: Mon, 06 Jul 2009 23:16:10 -0700 Subject: Opening a SQLite database in readonly mode In-Reply-To: References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Joshua Kugler wrote: > BTW, APSW is written by the same author as pysqlite. Not even remotely true :-) pysqlite was written by various people, with the maintainer of the last several years being Gerhard H?ring. I am the (sole) author of APSW and have not contributed any code to pysqlite although ideas have flowed freely between the projects and we share a mailing list. I started APSW in late 2004 because I wanted to use SQLite from Python rather than using a layer that pretended SQLite was like other databases. There were various quirks of pysqlite I also didn't like (many since corrected) and so scratched my itch. If you are just doing simple queries then there isn't much apparent difference. If you want to be a "power user" of SQLite then APSW is for you. SQLite has many cool features such as virtual tables (you provide the underlying data for the SQL queries to work on) and VFS (you provide the file access). See this link for more details: http://apsw.googlecode.com/svn/publish/pysqlite.html Roger -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkpS6CUACgkQmOOfHg372QS0/gCgiHD9ukUlQYJGCIMWb9hNMLCM Y/cAnid4dAeFHIdLBmKzGsXrvANkvhR5 =U0wg -----END PGP SIGNATURE----- From nagle at animats.com Tue Jul 7 02:21:02 2009 From: nagle at animats.com (John Nagle) Date: Mon, 06 Jul 2009 23:21:02 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <0260603f$0$20657$c3e8da3@news.astraweb.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4a52e8ef$0$1633$742ec2ed@news.sonic.net> Steven D'Aprano wrote: > On Sun, 05 Jul 2009 01:58:13 -0700, Paul Rubin wrote: > >> Steven D'Aprano writes: >>> Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't >>> see how a case statement would help you here: you're not dispatching on >>> a value, but running through a series of tests until one passes. >> A case statement switch(x):... into a bunch of constant case labels >> would be able to use x as an index into a jump vector, and/or do an >> unrolled logarithmic (bisection-like) search through the tests, instead >> of a linear search. > > Yes, I'm aware of that, but that's not what John's code is doing -- he's > doing a series of if expr ... elif expr tests. I don't think a case > statement can do much to optimize that. (I didn't write that code; it's from "http://code.google.com/p/html5lib/", which is a general purpose HTML 5 parser written in Python. It's compatible with ElementTree and/or BeautifulSoup. I currently use a modified BeautifulSoup for parsing real-world HTML in a small-scale crawler, and I'm looking at this as an HTML 5 compatible replacement.) John Nagle From nagle at animats.com Tue Jul 7 02:31:26 2009 From: nagle at animats.com (John Nagle) Date: Mon, 06 Jul 2009 23:31:26 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <02605ef2$0$20657$c3e8da3@news.astraweb.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4a52eb5f$0$1663$742ec2ed@news.sonic.net> Steven D'Aprano wrote: > On Sun, 05 Jul 2009 10:12:54 +0200, Hendrik van Rooyen wrote: > >> Python is not C. > > John Nagle is an old hand at Python. He's perfectly aware of this, and > I'm sure he's not trying to program C in Python. > > I'm not entirely sure *what* he is doing, and hopefully he'll speak up > and say, but whatever the problem is it's not going to be as simple as > that. I didn't write this code; I'm just using it. As I said in the original posting, it's from "http://code.google.com/p/html5lib". It's from an effort to write a clean HTML 5 parser in Python for general-purpose use. HTML 5 parsing is well-defined for the awful cases that make older browsers incompatible, but quite complicated. The Python implementation here is intended partly as a reference implementation, so browser writers have something to compare with. I have a small web crawler robust enough to parse real-world HTML, which can be appallingly bad. I currently use an extra-robust version of BeautifulSoup, and even that sometimes blows up. So I'm very interested in a new Python parser which supposedly handles bad HTML in the same way browsers do. But if it's slower than BeautifulSoup, there's a problem. John Nagle From __peter__ at web.de Tue Jul 7 02:33:48 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Jul 2009 08:33:48 +0200 Subject: module name versus function name resolution conflict. References: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Message-ID: rocky wrote: > Someone recently reported a problem in pydb where a function defined > in his program was conflicting with a module name that pydb uses. I > think I understand what's wrong, but I don't have any elegant > solutions to the problem. Suggestions would be appreciated. > > In a nutshell, here's the problem: > > In file fns: > > def foo(): print "foo" > > In file pdebug.py: > > import fns, sys > > def trace_dispatch(frame, event, arg): > fns.foo() > print frame, event, arg > return trace_dispatch > > sys.settrace(trace_dispatch) > execfile('myprogram.py') > > Finally file myprogram.py: > > def fns(): print "This is the *other* fns" > > When you run pdebug.py you get: > > $ python pdebug.py > foo > call None > foo > line None > Traceback (most recent call last): > File "pdebug.py", line 7, in > execfile('myprogram.py') > File "myprogram.py", line 1, in > def fns(): print "This is the *other* fns" > File "pdebug.py", line 3, in trace_dispatch > fns.foo() > AttributeError: 'function' object has no attribute 'foo' > > > Basically inside the trace hook, local functions are visible and take > precedence over (global) module names. I could move "import fns" > inside trace_dispatch(), but I'd have to do that in all of the methods > that module "fns" is used. Also note that using the form: > from fns import foo > > would eliminate the conflict on "fns", but I'd still have potential > conflicts on "foo". Using more obscure names (e.g. pydb_fns) would > reduce the chance of a conflict but not eliminate it. > > Suggestions? Can you run the program in another namespace? execfile("myprogram.py", {"__name__":"__main__"}) Alternatively move trace_dispatch into yet another module and import it into pdebug. from elsewhere import trace_dispatch Peter From wsysdu at gmail.com Tue Jul 7 02:56:40 2009 From: wsysdu at gmail.com (Eric Wong) Date: Tue, 07 Jul 2009 14:56:40 +0800 Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> Message-ID: Pedram wrote: > Hello Mr. Dickinson. Glad to see you again :) > > On Jul 6, 5:46 pm, Mark Dickinson wrote: >> On Jul 6, 1:24 pm, Pedram wrote: >> >> > OK, fine, I read longobject.c at last! :) >> > I found that longobject is a structure like this: >> >> > struct _longobject { >> > struct _object *_ob_next; >> > struct _object *_ob_prev; >> >> For current CPython, these two fields are only present in debug >> builds; for a normal build they won't exist. > > I couldn't understand the difference between them. What are debug > build and normal build themselves? And You mean in debug build > PyLongObject is a doubly-linked-list but in normal build it is just an > array (Or if not how it'll store in this mode)? > we use the macro Py_TRACE_REFS to differ the code for debug build and normal build, that's to say, in debug build and normal build the codes are actually *different*. In debug build, not only PyLongObject but all Objects are linked by a doubly-linked-list and it can make the debug process less painful. But in normal build, objects are seperated! After an object is created, it will never be moved, so we can and should refer to an object only by it's address(pointer). There's no one-big-container like a list or an array for objects. From astan.chee at al.com.au Tue Jul 7 02:56:55 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 7 Jul 2009 16:56:55 +1000 Subject: copytree with timeout in windows In-Reply-To: <4A52CAF2.7010707@timgolden.me.uk> References: <4A52B9D0.9040608@al.com.au> <4A52CAF2.7010707@timgolden.me.uk> Message-ID: <4A52F1B7.2070507@al.com.au> Tim Golden wrote: > Astan Chee wrote: > >> Hi, >> I'm trying to modify the copytree function in shutil so that any file >> being copied does not take more than 5 minutes (if it does, skip to the >> next one). >> > > One suggestion is to use the CopyFileEx API > exposed in the win32file module from pywin32. That > allows for a callback function which can abort > the copy (or issue a progress meter, etc.). > Thanks, but looking in the documentation there is no callback function. Did you mean */the Progress Routine? Thanks again /* > Obviously, you'd have to roll your own copytree, but > that's not beyond the wit of man. > > TJG > -------------- next part -------------- An HTML attachment was scrubbed... URL: From http Tue Jul 7 03:38:50 2009 From: http (Paul Rubin) Date: 07 Jul 2009 00:38:50 -0700 Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else References: Message-ID: <7x3a99gcxh.fsf@ruckus.brouhaha.com> palewire writes: > In my application, I'd like to have a function that compares two values, > either of which may be null, and then classify the result depending on > whether one is higher or lower than the other. Didn't we just have a huge thread about that? Using a special null value is often (not always) a code smell. But anyway (untested): def compare(a,b): if a is None and b is None: # I'm guessing you meant this return "No data" if a == b: return "Stayed the same" if a != 0 else "Never had" elif a < b: return "gained" else: # we know here that a > b return "Lost some" if b > 0 else "Lost all" I don't know what you intended to do in the case where exactly one of (a,b) is None, so I'll leave that to you. From mail at timgolden.me.uk Tue Jul 7 03:42:55 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Jul 2009 08:42:55 +0100 Subject: copytree with timeout in windows In-Reply-To: <4A52F1B7.2070507@al.com.au> References: <4A52B9D0.9040608@al.com.au> <4A52CAF2.7010707@timgolden.me.uk> <4A52F1B7.2070507@al.com.au> Message-ID: <4A52FC7F.8050308@timgolden.me.uk> Astan Chee wrote: > Tim Golden wrote: >> Astan Chee wrote: >> >>> Hi, >>> I'm trying to modify the copytree function in shutil so that any file >>> being copied does not take more than 5 minutes (if it does, skip to >>> the next one). >> >> One suggestion is to use the CopyFileEx API >> exposed in the win32file module from pywin32. That >> allows for a callback function which can abort >> the copy (or issue a progress meter, etc.). >> > Thanks, but looking in the documentation there is no callback function. > Did you mean */the Progress Routine? Well, depending on which hairs you're splitting, I would argue that the Progress Routine *is* a callback function: it is a function which is called back. But, yes, I did mean that. :) TJG From piet at cs.uu.nl Tue Jul 7 04:01:40 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 10:01:40 +0200 Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Nile (N) wrote: >N> I initialized the dictionary earlier in the program like this - >N> hashtable = {} >N> I changed the "dict" to hashtable but I still get the same result >N> I will try to learn about the defaultdict but I'm just trying to keep >N> it as simple as I can for now >N> Revised code >N> for x in range(len(file_list)): >N> d = open(file_list[x] , "r") >N> data = d.readlines() >N> k = 0 >N> k = above_or_below(data) >N> print "here is the value that was returned ",k >N> hashtable[k] = hashtable.get(k,0) + 1 >N> hashtable_list = hashtable.values() >N> print "here is a list of the dictionary values ", hashtable_list >N> print "the length of the dictionary is ", len(hashtable) >N> Output >N> # The first 3 lines are printed from the function >N> # right before the return statement. This output >N> # snippet shows the last two stocks. The function >N> # SAYS it is returning the correct value but only >N> # the last date seems to make it to the hashtable >N> Function will return k which = 11/11/2008 >N> Function will return k which = 11/12/2008 >N> Function will return k which = 11/14/2008 >N> # this line is printed from the code above >N> # I don't understand why all three dates don't >N> # seem to make it to the main program. Only >N> # the last date seems to be recognized >N> here is the value that was returned 11/14/2008 >N> Function will return k which = 11/11/2008 >N> Function will return k which = 11/12/2008 >N> Function will return k which = 11/14/2008 >N> here is the value that was returned 11/14/2008 >N> here is a list of the dictionary values [5] >N> the length of the dictionary is 1 >>> Exit code: 0 Now in your code there is a 1-1 relation between printing "here is the value that was returned" and incrementing the hashtable entry. In your log there are only 2 prints of "here is the value that was returned" so how can the count be 5? Are you hiding something from us? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From chris at simplistix.co.uk Tue Jul 7 04:06:49 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 09:06:49 +0100 Subject: Where does setuptools live? In-Reply-To: References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> Message-ID: <4A530219.6040007@simplistix.co.uk> David Lyon wrote: > setuptools... as far as I can see isn't actually installed until you > install easyinstall... That depends... I exclusively use buildout to manage my python packages, and sadly that relies on setuptools... > Pip (http://pypi.python.org/pypi/pip) and enstall > (http://pypi.python.org/pypi/Enstaller/3.1.0) seem to be forks of > setuptools already... > > So it looks like it's already been forked to some degree.. > > What hasn't happened is enough testing of pypi packages and installing > with setuptools/pip/enstall from pypi. What needs testing? More important for me is which of these has the most active development community. How do we judge that? > If the xmlrpc links actually "worked" on pypi... What about it doesn't work? Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From duncan.booth at invalid.invalid Tue Jul 7 04:19:47 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 7 Jul 2009 08:19:47 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: Dennis Lee Bieber wrote: > for, if, and return were common keywords in FORTRAN. Really? What does 'for' do in FORTRAN? P.S. Does FORTRAN actually have keywords these days? Back when I learned it there was no such thing as a reserved word though for all I know they may have since added them. -- Duncan Booth http://kupuguy.blogspot.com From afriere at yahoo.co.uk Tue Jul 7 04:48:30 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Tue, 7 Jul 2009 01:48:30 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006eb26f$0$9711$c3e8da3@news.astraweb.com> Message-ID: On Jul 7, 3:05?pm, Steven D'Aprano wrote: [snip] > Sense is, if you like, a "signed direction". Or to put it another way, in the graphical representation of a vector, 'direction' is the line, 'sense' is the arrowhead. From stefan_ml at behnel.de Tue Jul 7 05:09:59 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 11:09:59 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a52eb5f$0$1663$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> <4a52eb5f$0$1663$742ec2ed@news.sonic.net> Message-ID: <4a5310e7$0$31327$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > I have a small web crawler robust enough to parse > real-world HTML, which can be appallingly bad. I currently use > an extra-robust version of BeautifulSoup, and even that sometimes > blows up. So I'm very interested in a new Python parser which supposedly > handles bad HTML in the same way browsers do. But if it's slower > than BeautifulSoup, there's a problem. Well, if performance matters in any way, you can always use lxml's blazingly fast parser first, possibly trying a couple of different configurations, and only if all fail, fall back to running html5lib over the same input. That should give you a tremendous speed-up over your current code in most cases, while keeping things robust in the hard cases. Note the numbers that Ian Bicking has for HTML parser performance: http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ You should be able to run lxml's parser ten times in different configurations (e.g. different charset overrides) before it even reaches the time that BeautifulSoup would need to parse a document once. Given that undeclared character set detection is something where BS is a lot better than lxml, you can also mix the best of both worlds and use BS's character set detection to configure lxml's parser if you notice that the first parsing attempts fail. And yes, html5lib performs pretty badly in comparison (or did, at the time). But the numbers seem to indicate that if you can drop the ratio of documents that require a run of html5lib below 30% and use lxml's parser for the rest, you will still be faster than with BeautifulSoup alone. Stefan From dickinsm at gmail.com Tue Jul 7 05:10:31 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 7 Jul 2009 02:10:31 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> Message-ID: <2169e268-3fa7-4320-bef0-2619b8d1d040@h8g2000yqm.googlegroups.com> On Jul 6, 4:13?pm, Pedram wrote: > On Jul 6, 5:46?pm, Mark Dickinson wrote: > > On Jul 6, 1:24?pm, Pedram wrote: > > > > OK, fine, I read longobject.c at last! :) > > > I found that longobject is a structure like this: > > > > struct _longobject { > > > ? ? struct _object *_ob_next; > > > ? ? struct _object *_ob_prev; > > > For current CPython, these two fields are only present in debug > > builds; ?for a normal build they won't exist. > > I couldn't understand the difference between them. What are debug > build and normal build themselves? And You mean in debug build > PyLongObject is a doubly-linked-list but in normal build it is just an > array (Or if not how it'll store in this mode)? No: a PyLongObject is stored the same way (ob_size giving sign and number of digits, ob_digit giving the digits themselves) whether or not a debug build is in use. A debug build does various things (extra checks, extra information) to make it easier to track down problems. On Unix-like systems, you can get a debug build by configuring with the --with-pydebug flag. The _ob_next and _ob_prev fields have nothing particularly to do with Python longs; for a debug build, these two fields are added to *all* Python objects, and provide a doubly-linked list that links all 'live' Python objects together. I'm not really sure what, if anything, the extra information is used for within Python---it might be used by some external tools, I guess. Have you looked at the C-API documentation? http://docs.python.org/c-api/index.html _ob_next and _ob_prev are described here: http://docs.python.org/c-api/typeobj.html#_ob_next (These docs are for Python 2.6; I'm not sure what version you're working with.) Mark From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 05:19:46 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 11:19:46 +0200 Subject: a little wsgi framework In-Reply-To: References: Message-ID: <4a53132c$0$10839$426a34cc@news.free.fr> timmyt a ?crit : > i'm interested in getting opinions on a small wsgi framework i > assembled from webob, sqlalchemy, genshi, and various code fragments i > found on the inter-tubes > > here is the interesting glue - any comments / suggestions would be > much appreciated Well... My first comment would be about the usefulness of yet another Python web framework, but let's not worry about this... > ------------------ > the wsgi app > ------------------ > def application(environ, start_response): > path = environ.get('PATH_INFO', '').lstrip('/') > > for regex, callback in urls: > match = re.search(regex, path) I don't know where these "urls" come from. But anyway : if they live in some sort of long-running process, you may want to precompile them. > if match: > environ['myapp.url_args'] = match.groups() > request = webob.Request(environ) > > try: > return callback(request, start_response) > except Exception, ex: How are redirect etc handled ? > start_response('500 Internal Server Error', [('Content- > Type', 'text/plain')]) > return [traceback.format_exc()] A configuration option controlling the display of the traceback would be fine - I surely don't want the traceback being displayed to anyone when the app goes into production. Also, logging the error and traceback might be useful. > start_response('404 Not Found', [('Content-Type', 'text/plain')]) > return ["Couldn't find the URL specified."] > And in both cases, having the ability to use "custom" 500 and 404 pages might be nice too. > ---------------------------------- > the controller decorator > ---------------------------------- > def web_decorator(filename, method='html'): > > def decorator(target): May I suggest some renaming here ? filename => path (or 'template_path' ?) method => content_type target => controller or function or callback or.... > def wrapper(request, start_response): > > #genshi TemplateLoader > template = loader.load(filename) > > global config Probably not thread-safe.... > try: > return_dict = target(request, start_response) > return_string = template.generate(**return_dict).render > (method) Renaming again: return_dict => context return_string => data or text or ??? > config['database.Session'].commit() > except: > config['database.Session'].rollback() > raise > finally: > config['database.Session'].remove() This doesn't leave much control on transactions... Sometimes you want to handle it manually one way or another. > #TODO: alter 'Content-Type' per method being passed > start_response('200 OK', [('Content-Type', 'text/html')]) > return [return_string] Ok, so here again, I don't understand how redirects can work. Also, if you do use start_response here, I don't get why you still pass it to the callback function. > return wrapper > > return decorator slightly OT, but preserving infos on the decorated function might help too (introspection, debugging etc). My 2 cents... From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 05:27:52 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 11:27:52 +0200 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4a531512$0$9355$426a74cc@news.free.fr> Daniel Fetchinson a ?crit : (snip) > and my point is that users > are most of time correct when they assume that something will work the > same way as in C. Oh, really ? They would surely be wrong if they'd expect the for loop to have any similarity with a C for loop, or - a *very* common error - if they'd expect assignment to work the same way as in C. > So I'd think that putting a warning in a FAQ or a Python Gotchas list > about ++n would be very useful for many users. Might eventually be useful, but I can't hardly recall of more than a couple threads on this topic in 8+ years. From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 05:29:10 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 11:29:10 +0200 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4a531560$0$9355$426a74cc@news.free.fr> Daniel Fetchinson a ?crit : >>> Yes, there are plenty of languages other than Java and C, but the >>> influence of C is admittedly huge in Python. Why do you think loops >>> are called "for", conditionals "if" or "while", functions return via >>> "return", loops terminate via "break" and keep going via "continue" >>> and why is comparison written as "==", etc, etc? All of these are >>> coming from C (or an even earlier language) and my point is that users >> for, if, and return were common keywords in FORTRAN. >> >> Not to mention BASIC >> >> Both of which predate C > > Yes, hence my comment above, ".... coming from C (or an even earlier > language) ......". Mmm... Should we then claim that "the influence of FORTRAN is admittedly huge in Python" ?-) From rocky at gnu.org Tue Jul 7 05:44:07 2009 From: rocky at gnu.org (rocky) Date: Tue, 7 Jul 2009 02:44:07 -0700 (PDT) Subject: module name versus function name resolution conflict. References: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Message-ID: On Jul 7, 2:33?am, Peter Otten <__pete... at web.de> wrote: > rocky wrote: > > Someone recently reported a problem in pydb where a function defined > > in his program was conflicting with amodulenamethat pydb uses. I > > think I understand what's wrong, but I don't have any elegant > > solutions to the problem. Suggestions would be appreciated. > > > In a nutshell, here's the problem: > > > In file fns: > > > ? def foo(): print "foo" > > > In file pdebug.py: > > > ? import fns, sys > > > ? def trace_dispatch(frame, event, arg): > > ? ? ? fns.foo() > > ? ? ? print frame, event, arg > > ? ? ? return trace_dispatch > > > ? sys.settrace(trace_dispatch) > > ? execfile('myprogram.py') > > > Finally file myprogram.py: > > > ? def fns(): print "This is the *other* fns" > > > When you run pdebug.py you get: > > > $ python pdebug.py > > foo > > call None > > foo > > line None > > Traceback (most recent call last): > > ? File "pdebug.py", line 7, in > > ? ? execfile('myprogram.py') > > ? File "myprogram.py", line 1, in > > ? ? def fns(): print "This is the *other* fns" > > ? File "pdebug.py", line 3, in trace_dispatch > > ? ? fns.foo() > > AttributeError: 'function' object has no attribute 'foo' > > > Basically inside the trace hook, local functions are visible and take > > precedence over (global)modulenames. I was sloppy here. The "local" is the wrong word as suggested by the first remedy proposed. "entry in the global namespace dictionary" is probably closer to the truth. > > I could move "import fns" > > inside trace_dispatch(), but I'd have to do that in all of the methods > > thatmodule"fns" is used. ?Also note that using the form: > > ? from fns import foo > > > would eliminate the conflict on "fns", but I'd still have potential > > conflicts on "foo". Using more obscure names (e.g. pydb_fns) would > > reduce the chance of a conflict but not eliminate it. > > > Suggestions? > > Can you run the program in another namespace? > > execfile("myprogram.py", {"__name__":"__main__"}) > > Alternatively move trace_dispatch into yet anothermoduleand import it into > pdebug. > > from elsewhere import trace_dispatch > > Peter Both of these work. Thanks! (I haven't figured out how to adapt it to the messier context of the program yet). The second suggestion interesting/weird: by putting trace_dispatch in another module, compilation and name/ function resolution of fns.foo is done before myprogram has a chance to redefine fns. From lie.1296 at gmail.com Tue Jul 7 06:08:14 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 10:08:14 GMT Subject: Method to separate unit-test methods and data? In-Reply-To: References: Message-ID: Nick Daly wrote: > Hi, > > I was wondering if it's possible / if there are any simple methods > known of storing unit-test functions and their data in separate files? > > Perhaps this is a strange request, but it does an excellent job of > modularizing code. As far as revision control goes, it makes it > easier to discern between new or changed test cases, and changes in > the test data. > > Does anyone have any solutions for these problems? First, is there a > known and simple way to separate unit-test data and methods into > separate files? Secondly, if not, is there a simple way to convert > strings into other base types, like lists, dictionaries, and so forth? > Or, am I going to have to write my own list-parsing methods? Would > pickling help? I haven't yet had a chance to look into if or how that > would work... If there's anything else I can clarify about this > request, feel free to let me know. > It is usually not a very good idea to complicate the testing framework with configuration files (as then you will have two things to be tested). Unless the testing data is very huge (so huge that it interferes with reading and writing the testing code), and huge data is necessary for the test, it is much simpler and better to just put the data into the test code. In cases where you want to have fairly complex object (list, etc) you can "import" the test data. Pickling is usually unnecessary unless you want to store instance data (classes, etc). -- code.py -- def add(a, b): return a + b -- test.py -- import unittest # import the file we're testing import code # imports the data from separate file from testdata import data class AddCase(unittest.TestCase): def test_add(): for case in data: self.assertEqual(code.add(case[0], case[1]), case[2]) -- testdata.py -- data = [(0, 0, 0), (1, 0, 1), (0, 1, 1), (1, -1, 0), ] From pm567426 at gmail.com Tue Jul 7 07:06:07 2009 From: pm567426 at gmail.com (Pedram) Date: Tue, 7 Jul 2009 04:06:07 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> <2169e268-3fa7-4320-bef0-2619b8d1d040@h8g2000yqm.googlegroups.com> Message-ID: <06ec69cc-2183-41b6-92ac-eafaa3b5cd02@n11g2000yqb.googlegroups.com> On Jul 7, 1:10?pm, Mark Dickinson wrote: > On Jul 6, 4:13?pm, Pedram wrote: > > > > > On Jul 6, 5:46?pm, Mark Dickinson wrote: > > > On Jul 6, 1:24?pm, Pedram wrote: > > > > > OK, fine, I read longobject.c at last! :) > > > > I found that longobject is a structure like this: > > > > > struct _longobject { > > > > ? ? struct _object *_ob_next; > > > > ? ? struct _object *_ob_prev; > > > > For current CPython, these two fields are only present in debug > > > builds; ?for a normal build they won't exist. > > > I couldn't understand the difference between them. What are debug > > build and normal build themselves? And You mean in debug build > > PyLongObject is a doubly-linked-list but in normal build it is just an > > array (Or if not how it'll store in this mode)? > > No: ?a PyLongObject is stored the same way (ob_size giving sign and > number of digits, ob_digit giving the digits themselves) whether or > not a debug build is in use. > > A debug build does various things (extra checks, extra information) to > make it easier to track down problems. ?On Unix-like systems, you can > get a debug build by configuring with the --with-pydebug flag. > > The _ob_next and _ob_prev fields have nothing particularly to do with > Python longs; for a debug build, these two fields are added to *all* > Python objects, and provide a doubly-linked list that links all 'live' > Python objects together. ?I'm not really sure what, if anything, the > extra information is used for within Python---it might be used by some > external tools, I guess. > > Have you looked at the C-API documentation? > > http://docs.python.org/c-api/index.html > > _ob_next and _ob_prev are described here: > > http://docs.python.org/c-api/typeobj.html#_ob_next > > (These docs are for Python 2.6; ?I'm not sure what version you're > working with.) > > Mark It seems there's an island named Python! Thanks for links, I'm on reading them. From serverin2000 at yahoo.com Tue Jul 7 07:07:46 2009 From: serverin2000 at yahoo.com (RAM) Date: Tue, 7 Jul 2009 04:07:46 -0700 (PDT) Subject: generation of keyboard events References: Message-ID: On 6 July, 10:02, Simon Brunning wrote: > 2009/7/6 RAM : > > > I am trying to do this on windows. My program(executable) has been > > written in VC++ and when I run this program, I need to click on one > > button on the program GUI i,e just I am entering "Enter key" on the > > key board. But this needs manual process. So i need to write a python > > script which invokes my program and pass "Enter key" event to my > > program so that it runs without manual intervention. > > Try . > > -- > Cheers, > Simon B. Thank you all for the help the below url helped me in accomplishing my task http://www.rutherfurd.net/python/sendkeys/index.html regards Sreerama V From no.email at please.post Tue Jul 7 07:50:01 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 11:50:01 +0000 (UTC) Subject: ISO library ref in printed form Message-ID: Does anyone know where I can buy the Python library reference in printed form? (I'd rather not print the whole 1200+-page tome myself.) I'm interested in both/either 2.6 and 3.0. TIA! kj From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 07:51:11 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 13:51:11 +0200 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: <4a5336a8$0$11315$426a74cc@news.free.fr> kj a ?crit : > In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: > >> kj a ?crit : >> (snipo >>> To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, > >> FWIW, Python has no pretention to minimalism. > > Assuming that you mean by this that Python's authors have no such > pretensions: > > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html There are some differences between "small" and "minimal"... > So there. > > BTW, that's just one example. I've seen similar sentiments expressed > by Guido over and over and over: any new proposed enhancement to > Python must be good enough in his mind to justify cluttering the > language. That attitude counts as minimalism in my book. And in mine, it counts as "keeping the language's evolution under control" - which is still not the same thing as being "minimalist". If Python really was on the "minimalist" side, you wouldn't even have "class" or "def" statements - both being mostly syntactic sugar. And let's not even talk about @decorator syntax... From dnhkng at googlemail.com Tue Jul 7 08:00:09 2009 From: dnhkng at googlemail.com (Dr Mephesto) Date: Tue, 7 Jul 2009 05:00:09 -0700 (PDT) Subject: Python/pyobjC Apps on iPhone now a possibility? Message-ID: I have been following the discussion about python and pyobjc on the iphone, and it seemed to me that the app-store rules prohibited embedded interpreters; so, python apps are a no-no. But now it seems that the Rubyists have the option that we don't. It seems there is a company, http://rhomobile.com/home, that has an SDK that allows ruby programs to be embedded together with an interpreter in an app! More interesting is the fact that several of these hybrid apps seem to have been accepted on the itunes app store. Here's a quote from a representative, found on this blog: http://www.rubyinside.com/rhodes-develop-full-iphone-rim-and-symbian-apps-using-ruby-1475.html "...First of all, to clarify, we precompile all framework and app code down to Ruby 1.9 VM bytecode. This yields great performance advantages. We also disable eval and other dynamic execution aspects of Ruby. In the end, on all platforms your app gets compiled with our framework all into one single executable, indistinguishable from any other executable. But even if we were shipping a fullon Ruby interpreter without compiling to bytecode and leaving dynamic evaluation enabled (as has been well remarked in the blogosphere by now) App Store rule 3.3.2 does not disallow interpreters but only downloading code to be executed by the interpreter." So, the question is, can the same thing be done for Python apps? From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 08:00:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 12:00:33 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <006eb461$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006f13c9$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 22:18:20 -0700, Chris Rebert wrote: >> Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. [...] > Well, yes, but when would you apply it twice in a row? My point was that unary + isn't a no-op, and therefore neither is ++. For Decimal, I can't think why you'd want to apply ++x, but for other objects, who knows? Here's a toy example: >>> class Spam(str): ... def __pos__(self): ... return self.__class__("spam " + self) ... >>> s = Spam("") >>> ++++s 'spam spam spam spam ' -- Steven From david.lyon at preisshare.net Tue Jul 7 08:02:07 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 07 Jul 2009 08:02:07 -0400 Subject: Where does setuptools =?UTF-8?Q?live=3F?= In-Reply-To: <4A530219.6040007@simplistix.co.uk> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> Message-ID: On Tue, 07 Jul 2009 09:06:49 +0100, Chris Withers wrote: >> What hasn't happened is enough testing of pypi packages and installing >> with setuptools/pip/enstall from pypi. > > What needs testing? It's software... therefore it needs testing... David From paul.nospam at rudin.co.uk Tue Jul 7 08:03:54 2009 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Tue, 07 Jul 2009 13:03:54 +0100 Subject: Why re.match()? References: <4a4e2227$0$7801$426a74cc@news.free.fr> <4a5336a8$0$11315$426a74cc@news.free.fr> Message-ID: <87prccsnrp.fsf@rudin.co.uk> Bruno Desthuilliers writes: > kj a ?crit : >> In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: >> >>> kj a ?crit : >>> (snipo >>>> To have a special-case >>>> re.match() method in addition to a general re.search() method is >>>> antithetical to language minimalism, >> >>> FWIW, Python has no pretention to minimalism. >> >> Assuming that you mean by this that Python's authors have no such >> pretensions: >> >> "There is real value in having a small language." >> >> Guido van Rossum, 2007.07.03 >> http://mail.python.org/pipermail/python-3000/2007-July/008663.html > > There are some differences between "small" and "minimal"... > There's also a difference between the language and its standard library. From chris at simplistix.co.uk Tue Jul 7 08:07:48 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 13:07:48 +0100 Subject: Where does setuptools live? In-Reply-To: References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> Message-ID: <4A533A94.7030503@simplistix.co.uk> David Lyon wrote: > On Tue, 07 Jul 2009 09:06:49 +0100, Chris Withers > wrote: >>> What hasn't happened is enough testing of pypi packages and installing >>> with setuptools/pip/enstall from pypi. >> What needs testing? > > It's software... therefore it needs testing... Yes, which is why I asked WHAT needs testing? :-) Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From david.lyon at preisshare.net Tue Jul 7 08:21:50 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 07 Jul 2009 08:21:50 -0400 Subject: Where does setuptools =?UTF-8?Q?live=3F?= In-Reply-To: <4A533A94.7030503@simplistix.co.uk> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> <4A533A94.7030503@simplistix.co.uk> Message-ID: On Tue, 07 Jul 2009 13:07:48 +0100, Chris Withers wrote: >>>> What hasn't happened is enough testing of pypi packages and installing >>>> with setuptools/pip/enstall from pypi. >>> What needs testing? >> >> It's software... therefore it needs testing... > > Yes, which is why I asked WHAT needs testing? :-) I've written a package manager gui. I think it is orderly to comprehensively ascertain which packages will and won't install from pypi with the tool. I'll run the same install test for pip, easyinstall and enstall. And come up with a preferred installer. Which I will then suggest as the preferred tool. David From chris at simplistix.co.uk Tue Jul 7 08:28:14 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 13:28:14 +0100 Subject: Where does setuptools live? In-Reply-To: References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> <4A533A94.7030503@simplistix.co.uk> Message-ID: <4A533F5E.1080004@simplistix.co.uk> David Lyon wrote: > I've written a package manager gui. I think it is > orderly to comprehensively ascertain which packages will > and won't install from pypi with the tool. > > I'll run the same install test for pip, easyinstall > and enstall. And come up with a preferred installer. > > Which I will then suggest as the preferred tool. Cool :-) If you can publish your results or make the raw data downloadable somewhere (especially which packages fail with which installers) I'm sure a lot of people would find that *very* interesting... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From zdenekmaxa at yahoo.co.uk Tue Jul 7 08:38:16 2009 From: zdenekmaxa at yahoo.co.uk (Zdenek Maxa) Date: Tue, 07 Jul 2009 14:38:16 +0200 Subject: Tkinter.Canvas thread safety problem? Message-ID: <4A5341B8.5000502@yahoo.co.uk> Hello, I have started a project using Tkinter. The application performs some regular checks in a thread and updates Canvas components. I have observed that sometimes the application hangs when it is about to call canvas.itemconfig() when the thread is about to terminate in the next loop. Experimenting with this problem for a while, I have compiled a little example which always reproduces the problem. Commenting out the line 52 (before canvas.itemconfig()), the example always finishes all right, having the delay there, it hangs. I would like to ask if you could have a look at the snippet in the attachment and tell me if that is actually me doing something wrong or indeed Tkinter thread safety problem and what the workaround could be. Could you please also comment on wxPython thread safety? I am using: Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45) [GCC 4.3.3] 2.6.26-1-amd64 #1 SMP Fri Mar 13 19:34:38 UTC 2009 x86_64 GNU/Linux Thanks in advance, Zdenek -------------- next part -------------- A non-text attachment was scrubbed... Name: tkinter_thread_safety.py Type: text/x-python Size: 2278 bytes Desc: not available URL: From lie.1296 at gmail.com Tue Jul 7 08:48:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 12:48:34 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: <006ebea2$0$9711$c3e8da3@news.astraweb.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: > >> When people are fighting over things like `sense`, although sense may >> not be strictly wrong dictionary-wise, it smells of something burning... > > That would be my patience. > > I can't believe the direction this discussion has taken. Me neither. > Anybody sensible > would be saying "Oh wow, I've just learned a new meaning to the word, > that's great, I'm now less ignorant than I was a minute ago". But oh no, > we mustn't use a standard meaning to a word, heaven forbid we disturb > people's ignorance by teaching them something new. A meaning of a word is meaningless if nobody apart the writer understands it. The purpose of code is 1) to communicate with the computer, 2) to communicate with fellow programmer. The second point is especially important if the code are written for pedantic purpose. Teaching is largely one-way communication and often students that does not understand about a slight point could not or would not communicate their feelings because they think it is too silly. If the use of word is criticized on a two-way communication channel (e.g. newsgroup), it should raise a question of whether the word should be described first or whether a synonym would be more suitable for the purpose. Most of these do not apply on practical, non-pedantic purpose though, since in non-pedantic settings you are expected to know and use the jargons however (in)sensible they may be at first sight. > It's as simple as this: using `sense` as a variable name to record the > sense of a function is not a code smell, any more than using `flag` to > record a flag would be, or `sign` to record the sign of an object. Nobody said code smell... linguistic smell is more appropriate. > If you > don't know the appropriate meanings of the words sense, flag or sign, > learn them, don't dumb down my language. From jeanmichel at sequans.com Tue Jul 7 08:51:10 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 07 Jul 2009 14:51:10 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: <006ebea2$0$9711$c3e8da3@news.astraweb.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A5344BE.2080006@sequans.com> Steven D'Aprano wrote: > On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: > > >> When people are fighting over things like `sense`, although sense may >> not be strictly wrong dictionary-wise, it smells of something burning... >> > > That would be my patience. > > I can't believe the direction this discussion has taken. Anybody sensible > would be saying "Oh wow, I've just learned a new meaning to the word, > that's great, I'm now less ignorant than I was a minute ago". But oh no, > we mustn't use a standard meaning to a word, heaven forbid we disturb > people's ignorance by teaching them something new. > > It's as simple as this: using `sense` as a variable name to record the > sense of a function is not a code smell, any more than using `flag` to > record a flag would be, or `sign` to record the sign of an object. If you > don't know the appropriate meanings of the words sense, flag or sign, > learn them, don't dumb down my language. > > Can't we just calm down ? I'm really sorry my ignorance started this thread, and my apologies go to Kj who's obviously more fluent in english than me. I've never used sense in that way before, nor I've seen used by others until now. However Kj is right, and my dictionary seems wrong (wordreference.com). I've searched through others dictionaries and find out this is actually applicable to functions. My bad. JM From davea at ieee.org Tue Jul 7 08:55:13 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 08:55:13 -0400 Subject: Python Error from Apress book In-Reply-To: References: <24364269.post@talk.nabble.com> Message-ID: <4A5345B1.8050306@ieee.org> Gabriel Genellina wrote: >
En Mon, > 06 Jul 2009 19:56:40 -0300, matt0177 escribi?: > >> When I try to run the command as outlined in >> the book "simple_markup2.py < test_input.txt > test_output.html i get >> the >> following error every time. >> >> IOError: [Errno 9] Bad file descriptor > > That's a Windows problem. When you execute the script as itself > (either as you do in the command line, or by double-clicking on it), > it doesn't have valid standard handles. > You have to invoke Python explicitely: > > python simple_markup2.py < test_input.txt > test_output.html > > (you may need to specify the full path to python.exe, or add the > directory where Python is installed to your system PATH). > I use stdout this way all the time, with no problem (python 2.6, Windows XP). But as you point out, stdin redirection doesn't seem to work using the file associations. I do get a different error though. When I look at sys.stdin, it shows an open file, with handle of zero, as expected. But when I do a raw_input(), it gets: EOFError: EOF when reading a line From stefan_ml at behnel.de Tue Jul 7 09:01:07 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 15:01:07 +0200 Subject: parsing times like "5 minutes ago"? In-Reply-To: References: Message-ID: <4a534713$0$31332$9b4e6d93@newsspool4.arcor-online.net> mh at pixar.com wrote: > I'm looking for something like Tcl's [clock scan] command which parses > human-readable time strings such as: > > % clock scan "5 minutes ago" > 1246925569 > % clock scan "tomorrow 12:00" > 1246993200 > % clock scan "today + 1 fortnight" > 1248135628 > > Does any such package exist for Python? Is this only for English times or is I18N a concern? Stefan From python at mrabarnett.plus.com Tue Jul 7 09:07:00 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 14:07:00 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A534874.8050503@mrabarnett.plus.com> Dennis Lee Bieber wrote: > On Mon, 6 Jul 2009 19:48:39 -0700, Daniel Fetchinson > declaimed the following in > gmane.comp.python.general: > >> Yes, there are plenty of languages other than Java and C, but the >> influence of C is admittedly huge in Python. Why do you think loops >> are called "for", conditionals "if" or "while", functions return via >> "return", loops terminate via "break" and keep going via "continue" >> and why is comparison written as "==", etc, etc? All of these are >> coming from C (or an even earlier language) and my point is that users > > for, if, and return were common keywords in FORTRAN. > > Not to mention BASIC > > Both of which predate C > FORTRAN also used "=" for assignment (and ".EQ." for comparison). C was derived from BCPL which used ":=" for assignment and "=" for comparison. From james at agentultra.com Tue Jul 7 09:07:58 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 07 Jul 2009 09:07:58 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? References: Message-ID: <85ljn0ej4h.fsf@agentultra.com> Dr Mephesto writes: > I have been following the discussion about python and pyobjc on the > iphone, and it seemed to me that the app-store rules prohibited > embedded interpreters; so, python apps are a no-no. > > But now it seems that the Rubyists have the option that we don't. It > seems there is a company, http://rhomobile.com/home, that has an SDK > that allows ruby programs to be embedded together with an interpreter > in an app! More interesting is the fact that several of these hybrid > apps seem to have been accepted on the itunes app store. > > Here's a quote from a representative, found on this blog: > http://www.rubyinside.com/rhodes-develop-full-iphone-rim-and-symbian-apps-using-ruby-1475.html > > "...First of all, to clarify, we precompile all framework and app code > down to Ruby 1.9 VM bytecode. This yields great performance > advantages. We also disable eval and other dynamic execution aspects > of Ruby. In the end, on all platforms your app gets compiled with our > framework all into one single executable, indistinguishable from any > other executable. > > But even if we were shipping a fullon Ruby interpreter without > compiling to bytecode and leaving dynamic evaluation enabled (as has > been well remarked in the blogosphere by now) App Store rule 3.3.2 > does not disallow interpreters but only downloading code to be > executed by the interpreter." > > So, the question is, can the same thing be done for Python apps? I love Python and all, but it'd be apt to ask, what's the point? The iPhone is running on what? A 400Mhz ARM processor? Resources on the device are already limited; running your program on top of an embedded Python interpreter would only be adding pressure to the constraints; even if it was an optimized interpreter. Might as well just suck it up and learn C/Objective-C .. it's really not that hard. It took me about a day to pick up the language and another two or three to finagle with the libraries. From davea at ieee.org Tue Jul 7 09:10:14 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 09:10:14 -0400 Subject: module name versus function name resolution conflict. In-Reply-To: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> References: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Message-ID: <4A534936.7060300@ieee.org> rocky wrote: > Someone recently reported a problem in pydb where a function defined > in his program was conflicting with a module name that pydb uses. I > think I understand what's wrong, but I don't have any elegant > solutions to the problem. Suggestions would be appreciated. > > In a nutshell, here's the problem: > > In file fns: > > def foo(): print "foo" > > In file pdebug.py: > > import fns, sys > > def trace_dispatch(frame, event, arg): > fns.foo() > print frame, event, arg > return trace_dispatch > > sys.settrace(trace_dispatch) > execfile('myprogram.py') > > Finally file myprogram.py: > > def fns(): print "This is the *other* fns" > > When you run pdebug.py you get: > > $ python pdebug.py > foo > call None > foo > line None > Traceback (most recent call last): > File "pdebug.py", line 7, in > execfile('myprogram.py') > File "myprogram.py", line 1, in > def fns(): print "This is the *other* fns" > File "pdebug.py", line 3, in trace_dispatch > fns.foo() > AttributeError: 'function' object has no attribute 'foo' > > > Basically inside the trace hook, local functions are visible and take > precedence over (global) module names. I could move "import fns" > inside trace_dispatch(), but I'd have to do that in all of the methods > that module "fns" is used. Also note that using the form: > from fns import foo > > would eliminate the conflict on "fns", but I'd still have potential > conflicts on "foo". Using more obscure names (e.g. pydb_fns) would > reduce the chance of a conflict but not eliminate it. > > Suggestions? > > > Some context would be useful. For example, you may be writing pdebug.py as a library to be used by other developers, and you want to minimize the likelihood that arbitrary code they write will conflict. If you own all the code, I'd just say to rename one or the other. My preference is also for using import of myprogram, as I've never needed execfile(). But if you need one of the features of execfile(), and really want myprogram in the same namespace, then you probably ought to obfuscate the other import. Instead of import fns, try: import fns as _module_fns And of course any references to the module will use the longer name. The leading underscore is a clue that the name is not to be used outside of the module. And the prefix 'module' helps to make it unlikely they'll make a data or function or class with that name. From philip at semanchuk.com Tue Jul 7 09:18:32 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Tue, 7 Jul 2009 09:18:32 -0400 Subject: How to map size_t using ctypes? In-Reply-To: References: <7belo1F21fs3vU1@mid.uni-berlin.de> Message-ID: <9DD9E3DD-1C6E-4B12-A9DD-9F768832DF60@semanchuk.com> On Jul 6, 2009, at 11:51 PM, Gabriel Genellina wrote: > En Mon, 06 Jul 2009 13:29:21 -0300, Philip Semanchuk > escribi?: >> On Jul 6, 2009, at 12:10 PM, Diez B. Roggisch wrote: >>> Philip Semanchuk wrote: >>> >>>> I can't figure out how to map a C variable of size_t via Python's >>>> ctypes module. >>> >>> from ctypes import c_size_t >> >> D'oh! [slaps forehead] >> >> That will teach me to RTFM. [...] You'd be surprised at the amount >> of Googling I did without learning this on my own. > > Yep, seems like these terms in particular are hard to find. > Searching for > +ctypes +size_t, the first "good" result comes only at page 3. Bad > luck... > :( Fortunately, through my ignorance I may provide enlightenment...this conversation is now hit #3 on page 1 =) If I paste the URL here, will I create an infinite loop? http://www.google.com/search?q=%2Bctypes+%2Bsize_t From dnhkng at googlemail.com Tue Jul 7 09:23:25 2009 From: dnhkng at googlemail.com (Dr Mephesto) Date: Tue, 7 Jul 2009 06:23:25 -0700 (PDT) Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: Sure, I am learning Objective C already, but the syntax is really unfriendly after python. I think it really depends on the type of app you want to write. Anything held back by network delays or that sits around waiting for user input are perfectly acceptable target apps. If you need speed for something really intensive, falling back to C is still much nicer than coding in Objective C. I agree that having a certain basic understanding of objective C is a must, but having the option to use a coder-friendly language like Ruby or Python can cut development time dramatically. If Ruby can do it (and it generally slower than python), why can Python also get a legal iPhone interpreter? From deets at nospam.web.de Tue Jul 7 09:23:34 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 07 Jul 2009 15:23:34 +0200 Subject: Tkinter.Canvas thread safety problem? References: Message-ID: <7bh0anF22udtoU1@mid.uni-berlin.de> Zdenek Maxa wrote: > Hello, > > I have started a project using Tkinter. The application performs some > regular checks in a thread and updates Canvas components. I have > observed that sometimes the application hangs when it is about to call > canvas.itemconfig() when the thread is about to terminate in the next > loop. > > Experimenting with this problem for a while, I have compiled a little > example which always reproduces the problem. Commenting out the line 52 > (before canvas.itemconfig()), the example always finishes all right, > having the delay there, it hangs. > > I would like to ask if you could have a look at the snippet in the > attachment and tell me if that is actually me doing something wrong or > indeed Tkinter thread safety problem and what the workaround could be. > > Could you please also comment on wxPython thread safety? > As a rule of thumb, GUI-toolkits aren't threadsafe. Qt being a notable exception to this rule. There are various ways to cope with this, mostly through injecting events into the event-queue of the main gui thread, or using timers. This is also true for wx (google wx python threading for a plethora of information on this) For Tx, I dimly remember an "after" function that can be used to invoke timer-based code. Use that to update aggregated data from the events. Diez From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 09:23:49 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 15:23:49 +0200 Subject: Why re.match()? In-Reply-To: <87prccsnrp.fsf@rudin.co.uk> References: <4a4e2227$0$7801$426a74cc@news.free.fr> <4a5336a8$0$11315$426a74cc@news.free.fr> <87prccsnrp.fsf@rudin.co.uk> Message-ID: <4a534c5e$0$414$426a74cc@news.free.fr> Paul Rudin a ?crit : > Bruno Desthuilliers writes: > >> kj a ?crit : >>> In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: >>> >>>> kj a ?crit : >>>> (snipo >>>>> To have a special-case >>>>> re.match() method in addition to a general re.search() method is >>>>> antithetical to language minimalism, >>>> FWIW, Python has no pretention to minimalism. >>> Assuming that you mean by this that Python's authors have no such >>> pretensions: >>> >>> "There is real value in having a small language." >>> >>> Guido van Rossum, 2007.07.03 >>> http://mail.python.org/pipermail/python-3000/2007-July/008663.html >> There are some differences between "small" and "minimal"... >> > > There's also a difference between the language and its standard > library. Indeed. From pdpinheiro at gmail.com Tue Jul 7 09:37:02 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 06:37:02 -0700 (PDT) Subject: Why re.match()? References: Message-ID: On Jul 2, 4:49?am, kj wrote: > In Duncan Booth writes: > > >So, for example: > >>>> re.compile("c").match("abcdef", 2) > ><_sre.SRE_Match object at 0x0000000002C09B90> > >>>> re.compile("^c").search("abcdef", 2) > > I find this unconvincing; with re.search alone one could simply > do: > > >>> re.compile("^c").search("abcdef"[2:]) given large enough values of "abcdef", you just allocated several megs for no good reason, when re.compile("c").match("abcdef", 2) would process "abcdef" in-place. From davea at ieee.org Tue Jul 7 09:38:37 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 09:38:37 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A534FDD.8030105@ieee.org> Duncan Booth wrote: > Dennis Lee Bieber wrote: > > >> for, if, and return were common keywords in FORTRAN. >> > > Really? What does 'for' do in FORTRAN? > > P.S. Does FORTRAN actually have keywords these days? Back when I learned it > there was no such thing as a reserved word though for all I know they may > have since added them. > > The way I remember it (last used Fortran in 1970), DO was the looping construct, not FOR. Naturally it was uppercase, as keypunches of the time didn't have an easy way to do lowercase. (It was possible, you just had to use multi-punch, and a good memory for the appropriate 0-12-11 prefixes). From piet at cs.uu.nl Tue Jul 7 09:41:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 15:41:46 +0200 Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> <006e79e9$0$9711$c3e8da3@news.astraweb.com> Message-ID: >>>>> Steven D'Aprano (SD) wrote: >SD> On Mon, 06 Jul 2009 05:47:18 -0700, Scott David Daniels wrote: >>> Steven D'Aprano wrote: >>>> ... That's the Wrong Way to do it -- >>>> you're using a screwdriver to hammer a nail.... >>> >>> Don't knock tool abuse (though I agree with you here). Sometimes tool >>> abuse can produce good results. For example, using hammers to drive >>> screws for temporary strong holds led to making better nails. >SD> Historically, nails were invented a long time before screws. Screws as >SD> fasteners weren't invented until the 1400s, nails were used thousands of >SD> years ago. >SD> And hammering screws makes temporary *weak* holds, not strong, because >SD> the screw only touches the sides of the hole lightly. Unless the screw >SD> has been specifically designed to be hammered, hammering screws is pretty >SD> much the definition of incompetence and/or laziness! In our language (Dutch, i.e. Guido's mother tongue) `American screwdriver' is an expression meaning `hammer' :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nile_mcadams at yahoo.com Tue Jul 7 09:45:12 2009 From: nile_mcadams at yahoo.com (Nile) Date: Tue, 7 Jul 2009 06:45:12 -0700 (PDT) Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <97697cdc-e6b3-4277-b26e-d859ee4cd499@h31g2000yqd.googlegroups.com> Thanks all for your help. I appreciate it. The problem was in the function. A simple bug which I should have caught but I had my mental blinders on and was sure the problem was outside the function. The answers have given me a lot to learn so thanks for that as well. From __peter__ at web.de Tue Jul 7 09:50:35 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Jul 2009 15:50:35 +0200 Subject: Tkinter.Canvas thread safety problem? References: Message-ID: Zdenek Maxa wrote: > I would like to ask if you could have a look at the snippet in the > attachment and tell me if that is actually me doing something wrong or > indeed Tkinter thread safety problem and what the workaround could be. http://effbot.org/zone/tkinter-threads.htm From CHausberger at gmx.de Tue Jul 7 09:59:49 2009 From: CHausberger at gmx.de (Claus Hausberger) Date: Tue, 07 Jul 2009 15:59:49 +0200 Subject: Problem reading file with umlauts Message-ID: <20090707135949.100950@gmx.net> Hello I have a text file with is encoding in Latin1 (ISO-8859-1). I can't change that as I do not create those files myself. I have to read those files and convert the umlauts like ? to stuff like &oumol; as the text files should become html files. I have this code: #!/usr/bin/python # -*- coding: latin1 -*- import codecs f = codecs.open('abc.txt', encoding='latin1') for line in f: print line for c in line: if c == "?": print "oe" else: print c and I get this error message: $ ./read.py Abc ./read.py:11: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if c == "?": A b c Traceback (most recent call last): File "./read.py", line 9, in print line UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) I checked the web and tried several approaches but I also get some strange encoding errors. Has anyone ever done this before? I am currently using Python 2.5 and may be able to use 2.6 but I cannot yet move to 3.1 as many libs we use don't yet work with Python 3. any help more than welcome. This has been driving me crazy for two days now. best wishes Claus -- Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate f?r nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02 From stefan_ml at behnel.de Tue Jul 7 10:04:09 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 16:04:09 +0200 Subject: Problem reading file with umlauts In-Reply-To: References: Message-ID: <4a5355d9$0$31339$9b4e6d93@newsspool4.arcor-online.net> Claus Hausberger wrote: > Hello > > I have a text file with is encoding in Latin1 (ISO-8859-1). I can't change that as I do not create those files myself. > > I have to read those files and convert the umlauts like ? to stuff like &oumol; as the text files should become html files. > > I have this code: > > > #!/usr/bin/python > # -*- coding: latin1 -*- > > import codecs > > f = codecs.open('abc.txt', encoding='latin1') > > for line in f: > print line > for c in line: > if c == "?": You are reading Unicode strings, so you have to compare it to a unicode string as in if c == u"?": > print "oe" > else: > print c Note that printing non-ASCII characters may not always work, depending on your terminal. Stefan From sajmikins at gmail.com Tue Jul 7 10:18:30 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 10:18:30 -0400 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Message-ID: <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> On Mon, Jul 6, 2009 at 7:00 PM, wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? > > Please let me know, > nacim > > > -- > http://mail.python.org/mailman/listinfo/python-list > The answers to your first and third questions are, "yes" and "yes". :] (Generally speaking if something can be done by a computer it can be done with python.) As for your second question check out the "magnitude" package: http://pypi.python.org/pypi/magnitude/ and http://juanreyero.com/magnitude/ (That second link also has links to three other packages that deal with units of measurement.) Ii has units for the SI measurements, including volts and coulombs, so you should be able to accomplish your goals with it. The tricky thing is, as far as I can tell from the wikipedia entry (http://en.wikipedia.org/wiki/Relative_static_permittivity), "dielectric constant" seems to be a dimensionless number, i.e. C/C... I could be totally daft though. HTH, ~Simon From sajmikins at gmail.com Tue Jul 7 10:22:48 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 07:22:48 -0700 (PDT) Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 6, 6:02?pm, Michael Mossey wrote: > On Jul 6, 2:47?pm, Philip Semanchuk wrote: > > > On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > > > > What is required in a python program to make sure it catches a ? > > > control- > > > c on the command-line? Do some i/o? The OS here is Linux. > > > You can use a try/except to catch a KeyboardInterrupt exception, or ? > > you can trap it using the signal module:http://docs.python.org/library/signal.html > > > You want to trap SIGINT. > > > HTH > > Philip > > Thanks to both of you. However, my question is also about whether I > need to be doing i/o or some similar operation for my program to > notice in any shape or form that Control-C has been pressed. In the > past, I've written Python programs that go about their business > ignoring Ctrl-C. Other programs respond to it immediately by exiting. > I think the difference is that the latter programs are doing i/o. But > I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. > > For example, does trapping SIGINT always work, regardless of what my > process is doing? > > Thanks, > Mike Try some experiments. ;] From aahz at pythoncraft.com Tue Jul 7 10:41:59 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Jul 2009 07:41:59 -0700 Subject: Clarity vs. code reuse/generality References: <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Jean-Michel Pichavant wrote: > >Can't we just calm down ? I'm really sorry my ignorance started this >thread, and my apologies go to Kj who's obviously more fluent in english >than me. >I've never used sense in that way before, nor I've seen used by others >until now. However Kj is right, and my dictionary seems wrong >(wordreference.com). I've searched through others dictionaries and find >out this is actually applicable to functions. My bad. You were not the first person to point out that "sense" is a poor variable name. Unless KJ is specifically using this code in the context of a math class, I still think that "sense" is completely inappropriate. Your English fluency is just fine. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From hubaghdadi at gmail.com Tue Jul 7 10:46:18 2009 From: hubaghdadi at gmail.com (Hussein B) Date: Tue, 7 Jul 2009 07:46:18 -0700 (PDT) Subject: Remoting over SSH Message-ID: Hey, I want to perform commands on a remote server over SSH. What do I need? Thanks. From aahz at pythoncraft.com Tue Jul 7 10:47:49 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Jul 2009 07:47:49 -0700 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: In article <85ljn0ej4h.fsf at agentultra.com>, J Kenneth King wrote: > >The iPhone is running on what? A 400Mhz ARM processor? Resources on the >device are already limited; running your program on top of an embedded >Python interpreter would only be adding pressure to the constraints; >even if it was an optimized interpreter. Ten years ago, a 400MHz ARM processor would have been fast, and it would have been running Python 1.5.2. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From sajmikins at gmail.com Tue Jul 7 10:48:16 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 10:48:16 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <4A5344BE.2080006@sequans.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> <4A5344BE.2080006@sequans.com> Message-ID: <50f98a4c0907070748w39481ad9sed8ffba64b299dbc@mail.gmail.com> On Tue, Jul 7, 2009 at 8:51 AM, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> >> On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: >> >> >>> >>> When people are fighting over things like `sense`, although sense may >>> not be strictly wrong dictionary-wise, it smells of something burning... >>> >> >> That would be my patience. >> >> I can't believe the direction this discussion has taken. Anybody sensible >> would be saying "Oh wow, I've just learned a new meaning to the word, that's >> great, I'm now less ignorant than I was a minute ago". But oh no, we mustn't >> use a standard meaning to a word, heaven forbid we disturb people's >> ignorance by teaching them something new. >> >> It's as simple as this: using `sense` as a variable name to record the >> sense of a function is not a code smell, any more than using `flag` to >> record a flag would be, or `sign` to record the sign of an object. If you >> don't know the appropriate meanings of the words sense, flag or sign, learn >> them, don't dumb down my language. >> >> > > Can't we just calm down ? I'm really sorry my ignorance started this thread, > and my apologies go to Kj who's obviously more fluent in english than me. > I've never used sense in that way before, nor I've seen used by others until > now. However Kj is right, and my dictionary seems wrong (wordreference.com). > I've searched through others dictionaries and find out this is actually > applicable to functions. My bad. > > JM Well met, sir. From wuwei23 at gmail.com Tue Jul 7 10:52:56 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 7 Jul 2009 07:52:56 -0700 (PDT) Subject: Remoting over SSH References: Message-ID: On Jul 8, 12:46?am, Hussein B wrote: > I want to perform commands on a remote server over SSH. > What do I need? Take a look at pexpect: http://pexpect.sourceforge.net/pexpect.html From usernet at ilthio.net Tue Jul 7 10:58:34 2009 From: usernet at ilthio.net (Tim Harig) Date: Tue, 07 Jul 2009 14:58:34 GMT Subject: Remoting over SSH References: Message-ID: On 2009-07-07, Hussein B wrote: > I want to perform commands on a remote server over SSH. > What do I need? catb.org/esr/faqs/smart-questions.html There are many ways to remote using ssh. If we know what you are trying to do, maybe we could give you a better answer. If you just want to open the python interpreter interactively on another host you can do something like: ssh -t user at host python That will allow you send "commands" to the python interpeter. You could write a script and pipe it into the interpreter: cat script.py | ssh -t user at host python Maybe you want to open an ssh connection and send shell commands from a python script. You can do that using one of the popen2 functions: http://docs.python.org/library/popen2.html From nacim_bravo at agilent.com Tue Jul 7 11:02:05 2009 From: nacim_bravo at agilent.com (nacim_bravo at agilent.com) Date: Tue, 7 Jul 2009 09:02:05 -0600 Subject: Newbie needs help In-Reply-To: <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> Message-ID: <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> Hello Gurus, Thank you for trying to help to my initial and not well written questions. I will compile more detailed information and ask again. Btw, I am giving a glimpse to: "How To Ask Questions The Smart Way". nacim -----Original Message----- From: Simon Forman [mailto:sajmikins at gmail.com] Sent: Tuesday, July 07, 2009 7:19 AM To: BRAVO,NACIM (A-Sonoma,ex1) Cc: python-list at python.org Subject: Re: Newbie needs help On Mon, Jul 6, 2009 at 7:00 PM, wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? > > Please let me know, > nacim > > > -- > http://mail.python.org/mailman/listinfo/python-list > The answers to your first and third questions are, "yes" and "yes". :] (Generally speaking if something can be done by a computer it can be done with python.) As for your second question check out the "magnitude" package: http://pypi.python.org/pypi/magnitude/ and http://juanreyero.com/magnitude/ (That second link also has links to three other packages that deal with units of measurement.) Ii has units for the SI measurements, including volts and coulombs, so you should be able to accomplish your goals with it. The tricky thing is, as far as I can tell from the wikipedia entry (http://en.wikipedia.org/wiki/Relative_static_permittivity), "dielectric constant" seems to be a dimensionless number, i.e. C/C... I could be totally daft though. HTH, ~Simon From matt0177 at GMAIL.COM Tue Jul 7 11:02:22 2009 From: matt0177 at GMAIL.COM (matt0177) Date: Tue, 7 Jul 2009 08:02:22 -0700 (PDT) Subject: Python Error from Apress book In-Reply-To: <24364269.post@talk.nabble.com> References: <24364269.post@talk.nabble.com> Message-ID: <24374988.post@talk.nabble.com> Adding the python before the command line didn't work at first, but upon moving the files to the c:\python25 it worked like a champ. Thank you both for the help. Very frustrating to run into stuff like this when you're first trying to learn a knew language, it really throws off your momentum! -- View this message in context: http://www.nabble.com/Python-Error-from-Apress-book-tp24364269p24374988.html Sent from the Python - python-list mailing list archive at Nabble.com. From motoom at xs4all.nl Tue Jul 7 11:05:32 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Tue, 07 Jul 2009 17:05:32 +0200 Subject: Problem reading file with umlauts In-Reply-To: <20090707135949.100950@gmx.net> References: <20090707135949.100950@gmx.net> Message-ID: <4A53643C.4020905@xs4all.nl> Claus Hausberger wrote: > I have a text file with is encoding in Latin1 (ISO-8859-1). I can't > change that as I do not create those files myself. I have to read > those files and convert the umlauts like ? to stuff like &oumol; as > the text files should become html files. umlaut-in.txt: ---- This file is contains data in the unicode character set and is encoded with utf-8. Viele R?hre. Macht spa?! Ts?sch! umlaut-in.txt hexdump: ---- 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e unicode..chara 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and is 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with utf 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele R..hr 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht spa..! 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 Ts..sch!....... umlaut.py: ---- # -*- coding: utf-8 -*- import codecs text=codecs.open("umlaut-in.txt",encoding="utf-8").read() text=text.replace(u"?",u"oe") text=text.replace(u"?",u"ss") text=text.replace(u"?",u"ue") of=open("umlaut-out.txt","w") of.write(text) of.close() umlaut-out.txt: ---- This file is contains data in the unicode character set and is encoded with utf-8. Viele Roehre. Macht spass! Tsuesch! umlaut-out.txt hexdump: ---- 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e unicode...char 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and is 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with ut 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele Roe 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht spass 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! Tsuesch!..... -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From chengsoon.ong at inf.ethz.ch Tue Jul 7 11:08:20 2009 From: chengsoon.ong at inf.ethz.ch (Cheng Soon Ong) Date: Tue, 07 Jul 2009 17:08:20 +0200 Subject: automatic multiprocessing Message-ID: <4A5364E4.4040607@inf.ethz.ch> Hi all, I'm trying to automate the use of multiprocessing when it is available. The setting I have is quite simple, with a for loop where the operations inside are independent of each other. Here's a bit of code. function_inputs is a list of dictionaries, each of which match the signature of function_handle. if multiprocessing_present: # Passing keyword arguments to map still doesn't work cpus = multiprocessing.Pool() function_outputs = cpus.map(function_handle, function_inputs) else: function_outputs = [] for kwargs in function_inputs: cur_out = function_handle(**kwargs) function_outputs.append(cur_out) Am I missing something here? I cannot seem to get map to pass on keyword arguments. Thanks in advance, Cheng Soon From Scott.Daniels at Acm.Org Tue Jul 7 11:11:27 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 07 Jul 2009 08:11:27 -0700 Subject: ISO library ref in printed form In-Reply-To: References: Message-ID: kj wrote: > Does anyone know where I can buy the Python library reference in > printed form? (I'd rather not print the whole 1200+-page tome > myself.) I'm interested in both/either 2.6 and 3.0. Personally, I'd get the new Beazley's Python Essential Reference, which is due out "real soon now," and then use the provided docs as a addon. Also consider grabbing Gruet's "Python Quick Reference" page. When I was working in a printer site I printed the color version of Gruet's page two-sided; it was neither too bulky nor too sketchy for my needs (and he uses color to distinguish version-to-version changes). http://rgruet.free.fr/ Sadly, I no longer work there, so my copy is gone. :-( --Scott David Daniels Scott.Daniels at Acm.Org From tn.pablo at gmail.com Tue Jul 7 11:12:38 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Tue, 7 Jul 2009 10:12:38 -0500 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> Message-ID: On Tue, Jul 7, 2009 at 10:02, wrote: > Hello Gurus, > > Thank you for trying to help to my initial and not well written questions. ?I will compile more detailed information and ask again. ?Btw, I am giving a glimpse to: "How To Ask Questions The Smart Way". > > nacim Give this one a try too: http://www.mikeash.com/getting_answers.html It doesn't talk down to you...as much :P -- Pablo Torres N. From aahz at pythoncraft.com Tue Jul 7 11:29:28 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Jul 2009 08:29:28 -0700 Subject: ISO library ref in printed form References: Message-ID: In article , kj wrote: > >Does anyone know where I can buy the Python library reference in >printed form? (I'd rather not print the whole 1200+-page tome >myself.) I'm interested in both/either 2.6 and 3.0. There used to be one for Python 2.1, but I can't tell whether it's ever been updated because the website isn't responding: http://wiki.python.org/moin/ReferenceBooks -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From davea at ieee.org Tue Jul 7 11:44:44 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 11:44:44 -0400 Subject: Python Error from Apress book In-Reply-To: <24374988.post@talk.nabble.com> References: <24364269.post@talk.nabble.com> <24374988.post@talk.nabble.com> Message-ID: <4A536D6C.4010308@ieee.org> matt0177 wrote: > Adding the python before the command line didn't work at first, but upon > moving the files to the c:\python25 it worked like a champ. Thank you both > for the help. Very frustrating to run into stuff like this when you're first > trying to learn a knew language, it really throws off your momentum! > I'm glad you got things working. But you really shouldn't need to add your own files to the installation directory. What you need is straightforward, but the details are Windows specific The following is all at the command prompt, not inside Python. You have a PATH environment variable, with various directories on it. Type it out using the PATH command. Pick a location on it, and add a batch file we'll write below. It's also possible to add a new directory to the PATH, using the control panel. Normally, the first thing I do on a new machine is add two directories to the PATH, perhaps c:\bin and c:\bat. The former is for the simple one-file utilities you accumulate over the years, and the latter is for batch files. If you want to pursue this, let me know, and I'll give details. Now that you've picked a location for your batch file, let's create it, calling it Python25.bat You could just call it Python.bat, but this way, you can have more than one Python installed, and choose between them. The contents of Python25.bat are a single line: @c:\python25\python.exe %* The leading @ says we don't wan the line echoed to the screen. If you're unsure of yourself, you can leave it off till everything works well, then add it in. The %* says to copy all the arguments you pass the batch file into the executable. Once this batch file is stored in your PATH, you can just type something like: python25 myscript.py arg1 arg2 or, to run the interpreter itself, python25 DaveA From fetchinson at googlemail.com Tue Jul 7 12:14:48 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 7 Jul 2009 09:14:48 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <4a531512$0$9355$426a74cc@news.free.fr> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: > (snip) >> and my point is that users >> are most of time correct when they assume that something will work the >> same way as in C. > > Oh, really ? They would surely be wrong if they'd expect the for loop to > have any similarity with a C for loop, or - a *very* common error - if > they'd expect assignment to work the same way as in C. Yes, really. I wrote ".... most of the time ....." and not "all the time". >> So I'd think that putting a warning in a FAQ or a Python Gotchas list >> about ++n would be very useful for many users. > > Might eventually be useful, but I can't hardly recall of more than a > couple threads on this topic in 8+ years. I'm happy we agree. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From sajmikins at gmail.com Tue Jul 7 12:16:18 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 09:16:18 -0700 (PDT) Subject: automatic multiprocessing References: mailman.2782.1246980030.8015.python-list@python.org Message-ID: On Jul 7, 11:08?am, Cheng Soon Ong wrote: > Hi all, > > I'm trying to automate the use of multiprocessing when it is available. The > setting I have is quite simple, with a for loop where the operations inside are > independent of each other. Here's a bit of code. function_inputs is a list of > dictionaries, each of which match the signature of function_handle. > > ? ? ?if multiprocessing_present: > ? ? ? ? ?# Passing keyword arguments to map still doesn't work > ? ? ? ? ?cpus = multiprocessing.Pool() > ? ? ? ? ?function_outputs = cpus.map(function_handle, function_inputs) > ? ? ?else: > ? ? ? ? ?function_outputs = [] > ? ? ? ? ?for kwargs in function_inputs: > ? ? ? ? ? ? ?cur_out = function_handle(**kwargs) > ? ? ? ? ? ? ?function_outputs.append(cur_out) > > Am I missing something here? I cannot seem to get map to pass on keyword arguments. > > Thanks in advance, > Cheng Soon Pool.map() doesn't handle "**dict" keyword argument notation automatically. You could use a wrapper function like so: cpus = multiprocessing.Pool() def f(kwargs): return function_handle(**kwargs) function_outputs = cpus.map(f, function_inputs) (Note that f() could be defined outside the if statement if you're going to use it often.) HTH, ~Simon From fetchinson at googlemail.com Tue Jul 7 12:23:30 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 7 Jul 2009 09:23:30 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <4a531560$0$9355$426a74cc@news.free.fr> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531560$0$9355$426a74cc@news.free.fr> Message-ID: >>>> Yes, there are plenty of languages other than Java and C, but the >>>> influence of C is admittedly huge in Python. Why do you think loops >>>> are called "for", conditionals "if" or "while", functions return via >>>> "return", loops terminate via "break" and keep going via "continue" >>>> and why is comparison written as "==", etc, etc? All of these are >>>> coming from C (or an even earlier language) and my point is that users >>> for, if, and return were common keywords in FORTRAN. >>> >>> Not to mention BASIC >>> >>> Both of which predate C >> >> Yes, hence my comment above, ".... coming from C (or an even earlier >> language) ......". > > > Mmm... Should we then claim that "the influence of FORTRAN is admittedly > huge in Python" ?-) Hmmmm, your comments reached a level of pedanticism beyond which I can not follow :) Seriously, ask Guido about the influence of C vs. fortran. Somewhere you can find him quoted as saying that python was originally intended to "bridge the gap between the shell and C". I've never heard him talk about fortran. But this academic discussion is honestly a little pointless. The OP was referring to a expectation, coming from C, that is not fulfilled in python. What's wrong with mentioning it somewhere for the sake of helping C programmers? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From fetchinson at googlemail.com Tue Jul 7 12:29:51 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 7 Jul 2009 09:29:51 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <4a531512$0$9355$426a74cc@news.free.fr> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: >> and my point is that users >> are most of time correct when they assume that something will work the >> same way as in C. > > Oh, really ? They would surely be wrong if they'd expect the for loop to > have any similarity with a C for loop, or - a *very* common error - if > they'd expect assignment to work the same way as in C. By the way, assignments in conditionals. Guido explicitly referred to C when he forbade assignment in conditionals, citing common typos/errors in C code such as if( x = 5 ){ .... } instead of if( x == 5 ){ ..... }. So even he realized that warning people about different usage in python and C is a good thing. Expectations from C work sometimes, and sometimes they don't. In latter case a little warning is useful. Cheers, Daniel >> So I'd think that putting a warning in a FAQ or a Python Gotchas list >> about ++n would be very useful for many users. > > Might eventually be useful, but I can't hardly recall of more than a > couple threads on this topic in 8+ years. -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From nagle at animats.com Tue Jul 7 12:40:15 2009 From: nagle at animats.com (John Nagle) Date: Tue, 07 Jul 2009 09:40:15 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a5310e7$0$31327$9b4e6d93@newsspool4.arcor-online.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> <4a52eb5f$0$1663$742ec2ed@news.sonic.net> <4a5310e7$0$31327$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <4a537a11$0$1672$742ec2ed@news.sonic.net> Stefan Behnel wrote: > John Nagle wrote: >> I have a small web crawler robust enough to parse >> real-world HTML, which can be appallingly bad. I currently use >> an extra-robust version of BeautifulSoup, and even that sometimes >> blows up. So I'm very interested in a new Python parser which supposedly >> handles bad HTML in the same way browsers do. But if it's slower >> than BeautifulSoup, there's a problem. > > Well, if performance matters in any way, you can always use lxml's > blazingly fast parser first, possibly trying a couple of different > configurations, and only if all fail, fall back to running html5lib over > the same input. Detecting "fail" is difficult. A common problem is badly terminated comments which eat most of the document if you follow the spec. The document seems to parse correctly, but most of it is missing. The HTML 5 spec actually covers things like and treats it as a bogus comment. (That's because HTML 5 doesn't include general SGML; the only directive recognized is DOCTYPE. Anything else after " References: <20090707135949.100950@gmx.net> Message-ID: <4a537ce8$0$30220$9b4e6d93@newsspool1.arcor-online.net> Michiel Overtoom schrob: > Viele R?hre. Macht spa?! Ts?sch! LOL! :) Stefan From dana_at_work at yahoo.com Tue Jul 7 13:05:07 2009 From: dana_at_work at yahoo.com (dana) Date: Tue, 7 Jul 2009 10:05:07 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? Message-ID: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC modules together. Although I don't have Python 2.5, I've been informed the DBI module has been deprecated at 2.5. A few questions: 1) Although deprecated, will it work at all in 2.5? Does the fact that it is deprecrated mean it has been removed entirely, or does Python 2.5 simply issuing a warning? 2) What do I use in place of DBI for my Python 2.4. scripts that import modules DBI and ODBC together. I don't use DBI directly. It was simply a dependency for the ODBC module as best I knew. Thanks. Dana From python at mrabarnett.plus.com Tue Jul 7 13:10:27 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 18:10:27 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: <4A538183.9020009@mrabarnett.plus.com> Daniel Fetchinson wrote: >>> and my point is that users >>> are most of time correct when they assume that something will work the >>> same way as in C. >> Oh, really ? They would surely be wrong if they'd expect the for loop to >> have any similarity with a C for loop, or - a *very* common error - if >> they'd expect assignment to work the same way as in C. > > By the way, assignments in conditionals. Guido explicitly referred to > C when he forbade assignment in conditionals, citing common > typos/errors in C code such as if( x = 5 ){ .... } instead of if( x == > 5 ){ ..... }. So even he realized that warning people about different > usage in python and C is a good thing. Expectations from C work > sometimes, and sometimes they don't. In latter case a little warning > is useful. > I wonder whether the problem with assignment in conditionals in C is due to the assignment operator being "=". If it was ":=", would the error still occur? From douglas.packard at gmail.com Tue Jul 7 13:34:17 2009 From: douglas.packard at gmail.com (weforgottenuno) Date: Tue, 7 Jul 2009 10:34:17 -0700 (PDT) Subject: Unable to get Tkinter menubar to appear under OS X References: <3737fd18-4973-4cc3-914d-fbdefdbbb8a7@t10g2000vbg.googlegroups.com> Message-ID: <6a9b0831-ce2e-417b-ab4b-419629d0a25d@u16g2000pru.googlegroups.com> I'm having the same problem, though I am just using the pre-installed python and tkinter versions that are on my OS X 10.5 computer, I did not install them on my own. Any advice? -Doug On Jun 24, 9:22?am, Eric Winter wrote: > Hi all. I've googled this issue several times and come up dry. Here's > the situation: > > I have a X-windows build of Tkinter for Python built on an OS X > machine (10.4 or 10.5, same issue). This is not the Aqua version of > Tk, but Tk built from source using X-Window support. I also use a from- > source build of Python, not the system Python. I don't like this > arrangement, but project requirements (allegedly) dictate this > approach. > > When I run any application using that copy of Tkinter and that copy of > Python, everything seems to work fine except menus - the code which > creates the menus runs, but no menus appear, either in the window or > at the top of the screen. > > Am I required to use the Aqua version of Tk on OS X? If not, do I need > to do something special on OS X when I built Tk and/or Python? Any > hints here would be greatly appreciated. > > Thanks, > Eric Winter > Fermi Science Support Center > NASA Goddard Space Flight Center From piet at cs.uu.nl Tue Jul 7 13:38:59 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 19:38:59 +0200 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: >>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >A> In article <85ljn0ej4h.fsf at agentultra.com>, >A> J Kenneth King wrote: >>> >>> The iPhone is running on what? A 400Mhz ARM processor? Resources on the >>> device are already limited; running your program on top of an embedded >>> Python interpreter would only be adding pressure to the constraints; >>> even if it was an optimized interpreter. >A> Ten years ago, a 400MHz ARM processor would have been >A> fast, and it would have been running Python 1.5.2. My first Python experience at home was on a 40MHz 80486 (Python 1.5.2 I think). It was a bit slow :=( -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From hannarosie at gmail.com Tue Jul 7 13:46:47 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Tue, 7 Jul 2009 10:46:47 -0700 Subject: Write matrix to text file Message-ID: <564970db0907071046o60e927dao21cc0557f1fa2903@mail.gmail.com> Hi, I'm working with both python and matlab at the moment and I was wondering if there is an efficient way to take a 2-D array (of 1s and 0s) in python and write it to a text file such that matlab will be able to create a sparse matrix from it later. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Tue Jul 7 13:48:32 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 19:48:32 +0200 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: <4a538a71$0$30236$9b4e6d93@newsspool1.arcor-online.net> Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ++n > > and > > --n > > fail silently for numeric-valued n. I doubt that there are many. Plus, you misspelled them from the more obvious n++ and n-- which *do* fail with a SyntaxError. I think I faintly remember trying those in my early Python days and immediately went for "+=" when I saw them fail (as I had expected). Stefan From robert.oschler at gmail.com Tue Jul 7 14:01:17 2009 From: robert.oschler at gmail.com (roschler) Date: Tue, 7 Jul 2009 11:01:17 -0700 (PDT) Subject: Embedded Python : Why does thread lock here? Message-ID: <00e8b4e8-5386-4f3d-b900-ab1ba41294f2@q40g2000prh.googlegroups.com> I have the Python Intepreter embedded in a Delphi (Object Pascal) program. In the Python script shown below, I have a module that creates a thread object and starts it. The thread's run() call calls a function called deleteOutputVariables() declared at the module level. In my code's first incarnation the thread's run() call would deadlock when trying to call the deleteOutputVariables() function declared at Module level. I would never see the statement "(deleteOutputVariables) Top of Call" printed to the screen. I now know that I must periodically call join() with a very fast time-out to keep Python threads happy, and that solved the problem. However I am curious as to why it deadlocked at the deleteOutputVariables() call? Is it because deleteOutputVariables() is declared at the module level or because that function deletes module level variables? If so why? The code with the relevant parts excerpted is shown below, hopefully the indents hold up. Thanks, Robert ---------------------------- # MODULE: PythonThreadTest.py # FUNCTION: Delete the variables given in the list. def deleteOutputVariables(theModule, theOutputVariablesListOfNames): try: print "(deleteOutputVariables) Top of call." for theOutputVariableName in theOutputVariablesListOfNames: if theModule.__dict__.has_key(theOutputVariableName): print "(Python::deleteOutputVariables) Deleting the Output Variable named " + theOutputVariableName del theModule.__dict__[theOutputVariableName] except: print "(deleteOutputVariables) Exception occurred." # Import needed system modules import sys, os from threading import Thread # ----------------- BEGIN: THREAD class to execute robodanceCommand() class threadRun(Thread): def __init__ (self, theJobID = None): Thread.__init__(self) self.jobCompleted = False # def: __init__ def run(self): try: # NOTE ---> This is where the thread locks if I don't call join(0.001) in # my DELPHI (not Python) loop that waits on the thread to complete. Once # theNewThread.join() is called, execution resumes in # deleteOutputVariables() and the thread completes. deleteOutputVariables(Test_PYTHON, ["PyOut1"]) # Let others know we are done. self.jobCompleted = True except Exception, exc: self.exceptionCaught = exc # Let others know we are done. self.jobCompleted = True print("(Python::threadRun) Exception occurred.") # end: try # def: run() # ----------------- END: THREAD to execute robodanceCommand() theNewThread = None theNewThread = threadRun("TestThread") theNewThread.start() From pdpinheiro at gmail.com Tue Jul 7 14:01:33 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:01:33 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> Message-ID: <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> On Jul 7, 2:16?am, Steven D'Aprano wrote: > On Mon, 06 Jul 2009 16:43:43 +0100, Tim Rowe wrote: > > 2009/7/4 kj : > > >> Precisely. ?As I've stated elsewhere, this is an internal helper > >> function, to be called only a few times under very well-specified > >> conditions. ?The assert statements checks that these conditions are as > >> intended. ?I.e. they are checks against the module writer's programming > >> errors. > > > Good for you. I'm convinced that you have used the assertion > > appropriately, and the fact that so many here are unable to see that > > looks to me like a good case for teaching the right use of assertions. > > For what it's worth, I read assertions at the beginning of a procedure > > as part of the specification of the procedure, and I use them there in > > order to document the procedure. An assertion in that position is for me > > a statement to the user of the procedure "it's your responsibility to > > make sure that you never call this procedure in such a way as to violate > > these conditions". They're part of a contract, as somebody (maybe you) > > pointed out. > > > As somebody who works in the safety-critical domain, it's refreshing to > > see somebody teaching students to think about the circumstances in which > > a procedure can legitimately be called. The hostility you've received to > > that idea is saddening, and indicative of why there's so much buggy > > software out there. > > LOL. > > Maybe the reason for "so much buggy software" is that people > inappropriately use assert, thus changing the behaviour of code depending > on whether it is run with the -O flag or not. > > I don't know what "hostility" you're seeing. The only hostility I'm > seeing is from the OP, which is bizarre considering that he asked for > advice and we gave it. What I see is a bunch of people concerned that the > OP is teaching novices a bad habit, namely, using assert for error > checking. He claims -- angrily and over and over again -- that in his > code, the assertions should never fail. Great. Good for him for knowing > when to use assert. (...) But he doesn't. He asserts: assert lo < hi but then compares: sense = cmp(func(hi), func(lo)) sense can't ever be anything other than 1. I actually find it amusing that this threat got to 50 posts of raving discussion about assertions without anyone spotting that. Personally, I think the code is an unreadable mess, but that's mostly because of all the micro optimizations, not the generality of it. Here's my unoptimized, but still equally generic, version: def _binary_search(lo, hi, func, target, epsilon): sense = cmp(func(hi), func(lo)) if sense == 0: return None guess = (lo + hi) / 2. while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if func(guess) > target: hi = guess elif func(guess) < target: lo = guess elif lo == hi: return None return guess This is a newbie course, right? A while True loop might be faster, but it's all sorts of wrong for teaching newbies. Likewise, calculating a midpoint as mid = (hi + lo) * .5 is an aberration in a beginner environment. You want your students asking why you're calculating an average, not asking why you're multiplying by 0.5. In the same vein, I have no words for your target_plus/target_minus cleverness. The right way to go about it, IMO, is to give them my version, let them digest it, make all the necessary changes to it to turn it into your (faster) version. Present benchmarks for both, then let the readability/performance trade-off sink in. What you achieve with this exercise is that, instead of making your students think "I have to read that crud!?", they'll respect that ugly code is often the result of eking out every last drop of performance from a program as you possibly can. From inky788 at gmail.com Tue Jul 7 14:01:45 2009 From: inky788 at gmail.com (Inky 788) Date: Tue, 7 Jul 2009 11:01:45 -0700 (PDT) Subject: Where does setuptools live? References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> Message-ID: <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> On Jul 7, 4:06?am, Chris Withers wrote: > David Lyon wrote: > > > What hasn't happened is enough testing of pypi packages and installing > > with setuptools/pip/enstall from pypi. > > What needs testing? > > More important for me is which of these has the most active development > community. How do we judge that? Currently, distutils itself is being actively developed. More info about this here: http://tarekziade.wordpress.com/ My (albeit anonymous) advice is: use distutils. Manually download packages as-needed from PyPI and install manually using standard distutils. Read more about distutils here http://wiki.python.org/moin/Distutils and of course in the Python docs. If you want to contribute, my first guess would be that Tarek could use help writing tests (but I don't know what distutils coverage looks like at the moment). When Tarek says, "For package installation that takes care of dependencies, uninstall, etc., use $tool", then I'll start using $tool. Until then, it's just straight distutils for me. From http Tue Jul 7 14:06:59 2009 From: http (Paul Rubin) Date: 07 Jul 2009 11:06:59 -0700 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <7xab3gz7ss.fsf@ruckus.brouhaha.com> pdpi writes: > Personally, I think the code is an unreadable mess, but that's mostly > because of all the micro optimizations, not the generality of it. > Here's my unoptimized, but still equally generic, version: That version doesn't use "sense" inside the binary search, i.e. it relies on the function being monotonically increasing. From andreengels at gmail.com Tue Jul 7 14:26:45 2009 From: andreengels at gmail.com (Andre Engels) Date: Tue, 7 Jul 2009 20:26:45 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <6faf39c90907071126o5c774a79h4de2921fb3af205a@mail.gmail.com> On Tue, Jul 7, 2009 at 8:01 PM, pdpi wrote: > He asserts: > ? ?assert lo < hi > but then compares: > ? ?sense = cmp(func(hi), func(lo)) > > sense can't ever be anything other than 1. It can - there is no necessity that func is monotonically increasing. -- Andr? Engels, andreengels at gmail.com From pdpinheiro at gmail.com Tue Jul 7 14:30:42 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:30:42 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: On Jul 7, 7:26?pm, Andre Engels wrote: > On Tue, Jul 7, 2009 at 8:01 PM, pdpi wrote: > > He asserts: > > ? ?assert lo < hi > > but then compares: > > ? ?sense = cmp(func(hi), func(lo)) > > > sense can't ever be anything other than 1. > > It can - there is no necessity that func is monotonically increasing. > > -- > Andr? Engels, andreeng... at gmail.com Yes, I realized that as I was walking home. In other news, some of you may be interested in my seminar in advanced foot-in-mouth placement. From pdpinheiro at gmail.com Tue Jul 7 14:31:39 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:31:39 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> Message-ID: <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> On Jul 7, 7:06?pm, Paul Rubin wrote: > pdpi writes: > > Personally, I think the code is an unreadable mess, but that's mostly > > because of all the micro optimizations, not the generality of it. > > Here's my unoptimized, but still equally generic, version: > > That version doesn't use "sense" inside the binary search, i.e. it > relies on the function being monotonically increasing. You're right, make that: def _binary_search(lo, hi, func, target, epsilon): sense = cmp(func(hi), func(lo)) if sense == 0: return None guess = (lo + hi) / 2. while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if sense * func(guess) > target: hi = guess elif sense * func(guess) < target: lo = guess elif lo == hi: return None return guess Seems I had a serious brain cramp while posting that... From pdpinheiro at gmail.com Tue Jul 7 14:51:59 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:51:59 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> Message-ID: <1f09e856-bd04-4d27-bf51-5d67953a7e13@y7g2000yqa.googlegroups.com> On Jul 7, 7:31?pm, pdpi wrote: > On Jul 7, 7:06?pm, Paul Rubin wrote: > > > pdpi writes: > > > Personally, I think the code is an unreadable mess, but that's mostly > > > because of all the micro optimizations, not the generality of it. > > > Here's my unoptimized, but still equally generic, version: > > > That version doesn't use "sense" inside the binary search, i.e. it > > relies on the function being monotonically increasing. > > You're right, make that: > > def _binary_search(lo, hi, func, target, epsilon): > ? ? sense = cmp(func(hi), func(lo)) > ? ? if sense == 0: > ? ? ? ? return None > ? ? guess = (lo + hi) / 2. > ? ? while abs(func(guess) - target) > epsilon: > ? ? ? ? guess = (lo + hi) / 2. > ? ? ? ? if sense * func(guess) > target: > ? ? ? ? ? ? hi = guess > ? ? ? ? elif sense * func(guess) < target: > ? ? ? ? ? ? lo = guess > ? ? ? ? elif lo == hi: > ? ? ? ? ? ? return None > ? ? return guess > > Seems I had a serious brain cramp while posting that... Actually, scratch that. def _binary_search(lo, hi, func, target, epsilon): sense = cmp(func(hi), func(lo)) if sense == 0: return None guess = (lo + hi) / 2. while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if sense * func(guess) > sense * target: hi = guess elif sense * func(guess) < sense * target: lo = guess elif lo == hi: return None return guess From CHausberger at gmx.de Tue Jul 7 15:00:46 2009 From: CHausberger at gmx.de (Claus Hausberger) Date: Tue, 07 Jul 2009 21:00:46 +0200 Subject: Problem reading file with umlauts In-Reply-To: <4A53643C.4020905@xs4all.nl> References: <20090707135949.100950@gmx.net> <4A53643C.4020905@xs4all.nl> Message-ID: <20090707190046.152740@gmx.net> Thanks a lot. Now I am one step further but I get another strange error: Traceback (most recent call last): File "./read.py", line 12, in of.write(text) UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 0: ordinal not in range(128) according to google ufeff has something to do with byte order. I use an Linux system, maybe this helps to find the error. Claus > Claus Hausberger wrote: > > > I have a text file with is encoding in Latin1 (ISO-8859-1). I can't > > change that as I do not create those files myself. I have to read > > those files and convert the umlauts like ? to stuff like &oumol; as > > the text files should become html files. > > umlaut-in.txt: > ---- > This file is contains data in the unicode > character set and is encoded with utf-8. > Viele R?hre. Macht spa?! Ts?sch! > > > umlaut-in.txt hexdump: > ---- > 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con > 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th > 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e unicode..chara > 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and is > 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with utf > 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele R..hr > 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht spa..! > 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 Ts..sch!....... > > > umlaut.py: > ---- > # -*- coding: utf-8 -*- > import codecs > text=codecs.open("umlaut-in.txt",encoding="utf-8").read() > text=text.replace(u"?",u"oe") > text=text.replace(u"?",u"ss") > text=text.replace(u"?",u"ue") > of=open("umlaut-out.txt","w") > of.write(text) > of.close() > > > umlaut-out.txt: > ---- > This file is contains data in the unicode > character set and is encoded with utf-8. > Viele Roehre. Macht spass! Tsuesch! > > > umlaut-out.txt hexdump: > ---- > 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con > 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th > 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e unicode...char > 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and is > 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with ut > 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele Roe > 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht spass > 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! Tsuesch!..... > > > > > > -- > "The ability of the OSS process to collect and harness > the collective IQ of thousands of individuals across > the Internet is simply amazing." - Vinod Valloppillil > http://www.catb.org/~esr/halloween/halloween4.html -- Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate f?r nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02 From davea at ieee.org Tue Jul 7 15:04:27 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 15:04:27 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <4A539C3B.3040508@ieee.org> pdpi wrote: > On Jul 7, 2:16 am, Steven D'Aprano cybersource.com.au> wrote: > >> On Mon, 06 Jul 2009 16:43:43 +0100, Tim Rowe wrote: >> >>> 2009/7/4 kj : >>> >>>> Precisely. As I've stated elsewhere, this is an internal helper >>>> function, to be called only a few times under very well-specified >>>> conditions. The assert statements checks that these conditions are as >>>> intended. I.e. they are checks against the module writer's programming >>>> errors. >>>> >>> Good for you. I'm convinced that you have used the assertion >>> appropriately, and the fact that so many here are unable to see that >>> looks to me like a good case for teaching the right use of assertions. >>> For what it's worth, I read assertions at the beginning of a procedure >>> as part of the specification of the procedure, and I use them there in >>> order to document the procedure. An assertion in that position is for me >>> a statement to the user of the procedure "it's your responsibility to >>> make sure that you never call this procedure in such a way as to violate >>> these conditions". They're part of a contract, as somebody (maybe you) >>> pointed out. >>> >>> As somebody who works in the safety-critical domain, it's refreshing to >>> see somebody teaching students to think about the circumstances in which >>> a procedure can legitimately be called. The hostility you've received to >>> that idea is saddening, and indicative of why there's so much buggy >>> software out there. >>> >> LOL. >> >> Maybe the reason for "so much buggy software" is that people >> inappropriately use assert, thus changing the behaviour of code depending >> on whether it is run with the -O flag or not. >> >> I don't know what "hostility" you're seeing. The only hostility I'm >> seeing is from the OP, which is bizarre considering that he asked for >> advice and we gave it. What I see is a bunch of people concerned that the >> OP is teaching novices a bad habit, namely, using assert for error >> checking. He claims -- angrily and over and over again -- that in his >> code, the assertions should never fail. Great. Good for him for knowing >> when to use assert. (...) >> > > But he doesn't. > > He asserts: > assert lo < hi > but then compares: > sense =mp(func(hi), func(lo)) > > sense can't ever be anything other than 1. I actually find it amusing > that this threat got to 50 posts of raving discussion about assertions > without anyone spotting that. > > That's because the assert and the comparison are unrelated to each other. If the function is monotonically decreasing, then by definition lo= func(hi), which would yield a sense of 0 or -1. Trivial example of monotonically decreasing: def func(inp): return 53.0 - inp > Personally, I think the code is an unreadable mess, but that's mostly > because of all the micro optimizations, not the generality of it. > Here's my unoptimized, but still equally generic, version: > > def _binary_search(lo, hi, func, target, epsilon): > sense =mp(func(hi), func(lo)) > if sense =0: > return None > guess =lo + hi) / 2. > while abs(func(guess) - target) > epsilon: > guess =lo + hi) / 2. > if func(guess) > target: > hi =uess > elif func(guess) < target: > lo =uess > elif lo =hi: > return None > return guess > > And of course your clarified function will fail if the func is monotonically decreasing. I still think that rather than using sense in the loop, one should simply swap lo and hi, and continue. > This is a newbie course, right? A while True loop might be faster, but > it's all sorts of wrong for teaching newbies. Likewise, calculating a > midpoint as mid =hi + lo) * .5 is an aberration in a beginner > environment. You want your students asking why you're calculating an > average, not asking why you're multiplying by 0.5. In the same vein, I > have no words for your target_plus/target_minus cleverness. > > The right way to go about it, IMO, is to give them my version, let > them digest it, make all the necessary changes to it to turn it into > your (faster) version. Present benchmarks for both, then let the > readability/performance trade-off sink in. What you achieve with this > exercise is that, instead of making your students think "I have to > read that crud!?", they'll respect that ugly code is often the result > of eking out every last drop of performance from a program as you > possibly can. > > (untested) def _binary_search(lo, hi, func, target, epsilon): """ lo, hi are floats representing the desired range of input values to func() func() is a function that takes a float argument, and returns a float result target is the desired result value of func() epsilon is the allowable error compared to target. If set too small, this function will fail by returning None precondition: func is monotonic over the range of floating point inputs from lo to hi """ return a float value between lo and hi (inclusive) which yields approximately target if func(lo) > func(hi): lo, hi = hi, lo if not (func(lo) <= target <= func(hi)): return None #function doesn't have target value within the input range guess = lo while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if func(guess) > target: hi = guess elif func(guess) < target: lo = guess elif lo == hi: return None #function is too steep to have a value within epsilon return guess The reason for the "too steep" condition is that with a finite resolution for 'guess' epsilon could be enough smaller than target as to make a result impossible. For example, if the slope is 45 degrees, this happens when epsilon is about 2**51 smaller. From pdpinheiro at gmail.com Tue Jul 7 15:10:59 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 12:10:59 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <51f115a9-3e03-49f8-a340-4969133488cf@t33g2000yqe.googlegroups.com> On Jul 7, 8:04?pm, Dave Angel wrote: > And of course your clarified function will fail if the func is > monotonically decreasing. yeah, I eventually realized that and corrected it... And the assert()/ cmp() confusion too. I blame lack of sleep. The whole sign/sense thing left a really bad taste in my mouth, though, and the swapping lo and hi suggestion of yours seems like the neatest solution presented. From davea at ieee.org Tue Jul 7 15:17:42 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 15:17:42 -0400 Subject: Python Error from Apress book In-Reply-To: <8e2cbd660907071048p3a7c3bf9r22026712bf9bc3e4@mail.gmail.com> References: <24364269.post@talk.nabble.com> <24374988.post@talk.nabble.com> <4A536D6C.4010308@ieee.org> <8e2cbd660907071048p3a7c3bf9r22026712bf9bc3e4@mail.gmail.com> Message-ID: <4A539F56.9070109@ieee.org> Matthew Edmondson wrote: > Thanks a ton for the help. At first adding the path didn't work, but after > restarting my computer, ran like a champ :) > > Hopefully I can get decent with this language one day! > > All you needed was to restart the DOS-box (Command Prompt), after you did the control-panel thing. Those changes don't affect currently running processes. . By the way, those "standalone executables" could very well be python scripts. Except for input redirection, all my single-file scripts work fine stored there. And if you add .py and .pyw to the PATHEXT environment variable, you can run them without extension, so they're pretty much interchangeable with .exe files. So I have a script called digest.py, and I run it from a directory I want to analyze, by just typing digest . From dudeja.rajat at gmail.com Tue Jul 7 15:31:07 2009 From: dudeja.rajat at gmail.com (dudeja.rajat at gmail.com) Date: Wed, 8 Jul 2009 01:01:07 +0530 Subject: Check file is locked? Message-ID: How to check if a particular file is locked by some application? (i.e. the file is opened by some application)? -- Regrads, Rajat -------------- next part -------------- An HTML attachment was scrubbed... URL: From no.email at please.post Tue Jul 7 15:37:20 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 19:37:20 +0000 (UTC) Subject: ISO library ref in printed form References: Message-ID: In aahz at pythoncraft.com (Aahz) writes: >In article , kj wrote: >> >>Does anyone know where I can buy the Python library reference in >>printed form? (I'd rather not print the whole 1200+-page tome >>myself.) I'm interested in both/either 2.6 and 3.0. >There used to be one for Python 2.1, but I can't tell whether it's ever >been updated because the website isn't responding: >http://wiki.python.org/moin/ReferenceBooks Hmmm. That's a shame. How is one supposed to keep it under one's pillow??? kj From no.email at please.post Tue Jul 7 15:38:32 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 19:38:32 +0000 (UTC) Subject: ISO library ref in printed form References: Message-ID: In Scott David Daniels writes: >Also consider grabbing Gruet's "Python Quick Reference" page. Not quite what I had in mind, but handy all the same. Thanks. kj From pruebauno at latinmail.com Tue Jul 7 15:51:26 2009 From: pruebauno at latinmail.com (nn) Date: Tue, 7 Jul 2009 12:51:26 -0700 (PDT) Subject: library search path when compiling python Message-ID: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> I am trying to compile python with ssl support but the libraries are not in /usr/lib but in /opt/freeware/lib. How do I add that folder to the default library search path? It looks like configure --libdir=DIR might do the job but I don't want to replace the default lib search path, just add an additional folder to it. From no.email at please.post Tue Jul 7 16:04:46 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 20:04:46 +0000 (UTC) Subject: tough-to-explain Python Message-ID: I'm having a hard time coming up with a reasonable way to explain certain things to programming novices. Consider the following interaction sequence: >>> def eggs(some_int, some_list, some_tuple): ... some_int += 2 ... some_list += [2] ... some_tuple += (2,) ... >>> x = 42 >>> y = (42,) >>> z = [42] >>> eggs(x, y, z) >>> x 42 >>> y (42,) >>> z [42, 2] >>> How do I explain to rank beginners (no programming experience at all) why x and y remain unchanged above, but not z? Or consider this one: >>> ham = [1, 2, 3, 4] >>> spam = (ham,) >>> spam ([1, 2, 3, 4],) >>> spam[0] is ham True >>> spam[0] += [5] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> ham += [5] >>> spam ([1, 2, 3, 4, 5, 5],) >>> What do you say to that? I can come up with much mumbling about pointers and stacks and heaps and much hand-waving about the underlying this-and-that, but nothing that sounds even remotely illuminating. Your suggestions would be much appreciated! TIA! kj From lists at cheimes.de Tue Jul 7 16:06:01 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 07 Jul 2009 22:06:01 +0200 Subject: library search path when compiling python In-Reply-To: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> References: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> Message-ID: <4A53AAA9.9060307@cheimes.de> nn wrote: > I am trying to compile python with ssl support but the libraries are > not in /usr/lib but in /opt/freeware/lib. How do I add that folder to > the default library search path? > > It looks like configure --libdir=DIR might do the job but I don't want > to replace the default lib search path, just add an additional folder > to it. You didn't mention your OS. On Linux you can set the environment variable LD_RUN_PATH prior to compiling Python. The env var sets the rpath for the linker. See man ld(1) for details. Christian From lists at cheimes.de Tue Jul 7 16:07:32 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 07 Jul 2009 22:07:32 +0200 Subject: Check file is locked? In-Reply-To: References: Message-ID: dudeja.rajat at gmail.com wrote: > How to check if a particular file is locked by some application? (i.e. the > file is opened by some application)? It depends on your operating system. By the way most operating systems don't lock a file when it's opened for reading or writing or even executed. Christian From clp2 at rebertia.com Tue Jul 7 16:08:50 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 7 Jul 2009 13:08:50 -0700 Subject: tough-to-explain Python In-Reply-To: References: Message-ID: <50697b2c0907071308u375c81edt49fd2d1a2f6c7c34@mail.gmail.com> On Tue, Jul 7, 2009 at 1:04 PM, kj wrote: > > > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > > Consider the following interaction sequence: > >>>> def eggs(some_int, some_list, some_tuple): > ... ? ? some_int += 2 > ... ? ? some_list += [2] > ... ? ? some_tuple += (2,) > ... >>>> x = 42 >>>> y = (42,) >>>> z = [42] >>>> eggs(x, y, z) >>>> x > 42 >>>> y > (42,) >>>> z > [42, 2] >>>> > > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? > > Or consider this one: > >>>> ham = [1, 2, 3, 4] >>>> spam = (ham,) >>>> spam > ([1, 2, 3, 4],) >>>> spam[0] is ham > True >>>> spam[0] += [5] > Traceback (most recent call last): > ?File "", line 1, in > TypeError: 'tuple' object does not support item assignment >>>> ham += [5] >>>> spam > ([1, 2, 3, 4, 5, 5],) >>>> > > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! You might find the following helpful (partially): http://effbot.org/zone/call-by-object.htm Cheers, Chris -- http://blog.rebertia.com From kevin.p.dwyer at gmail.com Tue Jul 7 16:11:17 2009 From: kevin.p.dwyer at gmail.com (Kevin Dwyer) Date: Tue, 7 Jul 2009 20:11:17 +0000 (UTC) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: Hello, I think this is discussed PEP 249 - see the "major changes" section. http://www.python.org/dev/peps/pep-0249/ Kev On Tue, 07 Jul 2009 10:05:07 -0700, dana wrote: > I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC > modules together. Although I don't have Python 2.5, I've been informed > the DBI module has been deprecated at 2.5. A few questions: > > 1) Although deprecated, will it work at all in 2.5? Does the fact that > it is deprecrated mean it has been removed entirely, or does Python 2.5 > simply issuing a warning? > > 2) What do I use in place of DBI for my Python 2.4. scripts that import > modules DBI and ODBC together. I don't use DBI directly. It was simply a > dependency for the ODBC module as best I knew. > > Thanks. > > Dana From deets at nospam.web.de Tue Jul 7 16:15:40 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 07 Jul 2009 22:15:40 +0200 Subject: ISO library ref in printed form In-Reply-To: References: Message-ID: <7bhoncF23eocqU1@mid.uni-berlin.de> kj schrieb: > In aahz at pythoncraft.com (Aahz) writes: > >> In article , kj wrote: >>> Does anyone know where I can buy the Python library reference in >>> printed form? (I'd rather not print the whole 1200+-page tome >>> myself.) I'm interested in both/either 2.6 and 3.0. > >> There used to be one for Python 2.1, but I can't tell whether it's ever >> been updated because the website isn't responding: > >> http://wiki.python.org/moin/ReferenceBooks > > Hmmm. That's a shame. How is one supposed to keep it under one's > pillow??? That's what netbooks were created for... Diez From piet at cs.uu.nl Tue Jul 7 16:37:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 22:37:56 +0200 Subject: ISO library ref in printed form References: Message-ID: >>>>> kj (kj) wrote: >kj> Does anyone know where I can buy the Python library reference in >kj> printed form? (I'd rather not print the whole 1200+-page tome >kj> myself.) I'm interested in both/either 2.6 and 3.0. Maybe you can have a copy printed at lulu.com. It would even be nicer if the PSF would offer them at lulu. They could even make some money from it if enough copies would be sold.. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at mrabarnett.plus.com Tue Jul 7 16:40:20 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 21:40:20 +0100 Subject: Problem reading file with umlauts In-Reply-To: <20090707190046.152740@gmx.net> References: <20090707135949.100950@gmx.net> <4A53643C.4020905@xs4all.nl> <20090707190046.152740@gmx.net> Message-ID: <4A53B2B4.9090409@mrabarnett.plus.com> Claus Hausberger wrote: > Thanks a lot. Now I am one step further but I get another strange error: > > Traceback (most recent call last): > File "./read.py", line 12, in > of.write(text) > UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 0: ordinal not in range(128) > > according to google ufeff has something to do with byte order. > > I use an Linux system, maybe this helps to find the error. > 'text' contains Unicode, but you're writing it to a file that's not opened for Unicode. Either open the output file for Unicode: of = codecs.open("umlaut-out.txt", "w", encoding="latin1") or encode the text before writing: text = text.encode("latin1") (I'm assuming you want the output file to be in Latin1.) > >> Claus Hausberger wrote: >> >>> I have a text file with is encoding in Latin1 (ISO-8859-1). I can't >>> change that as I do not create those files myself. I have to read >>> those files and convert the umlauts like ? to stuff like &oumol; as >>> the text files should become html files. >> umlaut-in.txt: >> ---- >> This file is contains data in the unicode >> character set and is encoded with utf-8. >> Viele R?hre. Macht spa?! Ts?sch! >> >> >> umlaut-in.txt hexdump: >> ---- >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e unicode..chara >> 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and is >> 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with utf >> 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele R..hr >> 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht spa..! >> 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 Ts..sch!....... >> >> >> umlaut.py: >> ---- >> # -*- coding: utf-8 -*- >> import codecs >> text=codecs.open("umlaut-in.txt",encoding="utf-8").read() >> text=text.replace(u"?",u"oe") >> text=text.replace(u"?",u"ss") >> text=text.replace(u"?",u"ue") >> of=open("umlaut-out.txt","w") >> of.write(text) >> of.close() >> >> >> umlaut-out.txt: >> ---- >> This file is contains data in the unicode >> character set and is encoded with utf-8. >> Viele Roehre. Macht spass! Tsuesch! >> >> >> umlaut-out.txt hexdump: >> ---- >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e unicode...char >> 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and is >> 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with ut >> 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele Roe >> 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht spass >> 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! Tsuesch!..... >> >> >> >> >> >> -- >> "The ability of the OSS process to collect and harness >> the collective IQ of thousands of individuals across >> the Internet is simply amazing." - Vinod Valloppillil >> http://www.catb.org/~esr/halloween/halloween4.html > From piet at cs.uu.nl Tue Jul 7 16:53:13 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 22:53:13 +0200 Subject: tough-to-explain Python References: Message-ID: >>>>> kj (k) wrote: >k> I'm having a hard time coming up with a reasonable way to explain >k> certain things to programming novices. >k> Consider the following interaction sequence: >>>>> def eggs(some_int, some_list, some_tuple): >k> ... some_int += 2 >k> ... some_list += [2] >k> ... some_tuple += (2,) >k> ... >>>>> x = 42 >>>>> y = (42,) >>>>> z = [42] >>>>> eggs(x, y, z) >>>>> x >k> 42 >>>>> y >k> (42,) >>>>> z >k> [42, 2] >>>>> >k> How do I explain to rank beginners (no programming experience at >k> all) why x and y remain unchanged above, but not z? You shouldn't. That's not for beginners. Leave it waiing until you get to the advanced level. >k> Or consider this one: >>>>> ham = [1, 2, 3, 4] >>>>> spam = (ham,) >>>>> spam >k> ([1, 2, 3, 4],) >>>>> spam[0] is ham >k> True >>>>> spam[0] += [5] >k> Traceback (most recent call last): >k> File "", line 1, in >k> TypeError: 'tuple' object does not support item assignment >>>>> ham += [5] >>>>> spam >k> ([1, 2, 3, 4, 5, 5],) >>>>> >k> What do you say to that? Mutable and immutable. But use different examples. Like ham = [1, 2, 3, 4] spam = (1, 2, 3, 4) spam[0] += 1 will give the same error message. You can't change the components of a tuple. Your example above is similar. The spam[0] += [5] appends the 5 to the list in spam[0] (so it appends to ham), and then tries to assign the result of it to spam[0], which is not allowed. That the item it tries to assign is the same as the item that was already there doesn't matter. So dont't forget += is a real assignment, even when it is an in-place modification. Your example just proves that. The language ref manual says: With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. But I think that your example isn't for beginners either. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From joshua at joshuakugler.com Tue Jul 7 17:07:55 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Tue, 07 Jul 2009 13:07:55 -0800 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: Roger Binns wrote: > Joshua Kugler wrote: >> BTW, APSW is written by the same author as pysqlite. > Not even remotely true :-) Sorry about that...since pysqlite and APSW are both discusses on the pysqlite list, I had made an incorrect assumption. Oops. j From no.email at please.post Tue Jul 7 17:11:29 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 21:11:29 +0000 (UTC) Subject: tough-to-explain Python References: Message-ID: In Piet van Oostrum writes: >>>>>> kj (k) wrote: >>k> I'm having a hard time coming up with a reasonable way to explain >>k> certain things to programming novices. >>k> Consider the following interaction sequence: >>>>>> def eggs(some_int, some_list, some_tuple): >>k> ... some_int += 2 >>k> ... some_list += [2] >>k> ... some_tuple += (2,) >>k> ... >>>>>> x = 42 >>>>>> y = (42,) >>>>>> z = [42] >>>>>> eggs(x, y, z) >>>>>> x >>k> 42 >>>>>> y >>k> (42,) >>>>>> z >>k> [42, 2] >>>>>> >>k> How do I explain to rank beginners (no programming experience at >>k> all) why x and y remain unchanged above, but not z? >You shouldn't. That's not for beginners. No, of course not. And I don't plan to present these examples to them. But beginners have a way of bumping into such conundrums all on their own, and, as a former beginner myself, I can tell you that they find them, at best, extremely frustrating, and at worst, extremely discouraging. I doubt that an answer of the form "don't worry your pretty little head over this now; wait until your second course" will do the trick. Thanks for your comments! kj From no.email at please.post Tue Jul 7 17:18:53 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 21:18:53 +0000 (UTC) Subject: tough-to-explain Python References: Message-ID: In Chris Rebert writes: >You might find the following helpful (partially): >http://effbot.org/zone/call-by-object.htm Extremely helpful. Thanks! (I learned more from it than someone who will teach the stuff would care to admit...) I had not realized how *profoundly* different the meaning of the "=" in Python's spam = ham is from the "=" in its spam[3] = ham[3] So much for "explicit is better than implicit"... And it confirmed Paul Graham's often-made assertion that all of programming language design is still catching up to Lisp... kj From james at agentultra.com Tue Jul 7 17:21:26 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 07 Jul 2009 17:21:26 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: <85hbxodwa1.fsf@agentultra.com> Dr Mephesto writes: > Sure, I am learning Objective C already, but the syntax is really > unfriendly after python. > > I think it really depends on the type of app you want to write. > Anything held back by network delays or that sits around waiting for > user input are perfectly acceptable target apps. If you need speed for > something really intensive, falling back to C is still much nicer than > coding in Objective C. I agree that having a certain basic > understanding of objective C is a must, but having the option to use a > coder-friendly language like Ruby or Python can cut development time > dramatically. > > If Ruby can do it (and it generally slower than python), why can > Python also get a legal iPhone interpreter? It can if you want to write the interpreter. I just don't see the point. I can understand wanting a higher level language than assembler or maybe even C, but that's precisely what Objective-C is. Unfortunately, while the hardware for these mobile platforms is getting better these days, it's still not where it needs to be to run programs written in interpreted languages, IMO. Users typically only interact with an app for around two minutes at a time. Your app needs to be as fast as it can be. It's one of the few areas of software development today where optimizations can be justified (the other are games and scientific computing). Trust me, if there were an SMS app for the iPhone that loaded faster than the one that ships with it, guaranteed it would sell like hot-cakes (if Apple would let it in the store of course). If you do write the interpreter, let me know. I would certainly experiment with it. From bearophileHUGS at lycos.com Tue Jul 7 17:46:01 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 7 Jul 2009 14:46:01 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: kj, as Piet van Oostrum as said, that's the difference between mutable an immutable. It comes from the procedural nature of Python, and probably an explanation of such topic can't be avoided if you want to learn/teach Python. Lot of people think that a language where everything is immutable is simpler for newbies to understand (even if they have to learn what higher-order functions are). That's why some people think Scheme or other languages (even Clojure, I guess) are better for novices (Scheme has mutability, but you can avoid showing it to newbies). Some people say that languages that mostly encourage the use of immutable data structures (like F#, Clojure, Scala and many other less modern ones) help avoid bugs, maybe for novices too. On the other hand, it's hard or impossible to actually remove complexity from a system, and you usually just move it elsewhere. So other things will become harder to do for those novices. I have no idea if for the average novice it's simpler to learn to use a immutables-based language instead of a mutables-based one (it can also be possible that some novices prefer the first group, and other novices prefer the second group). >From what I have seen lot of students seem able to learn Python, so it's not a bad choice. Python isn't perfect, and in *many* situations it is pragmatic, for example to increase its performance. Generally for a novice programmer running speed is not important, but it's important to have a really coherent and clean language. I've personally seen that for such people even Python looks very "dirty" (even if it's one of the less dirty ones). For example a novice wants to see 124 / 38 to return the 62/19 fraction and not 3 or 3.263157894736842 :-) People may be able to invent a clean and orthogonal language that's easy to use and has very few compromises, fit for newbies. But this language may be very slow, not much useful in practice (who knows? Maybe there's a practical niche even for such very high level language), and it doesn't teach how to use lower level languages like C :-) Today I think there are no languages really fit for teaching. Python is one of the few fit ones, but it's getting more and more complex as time passes because it's getting used in more and more complex real world situations (a language fit for newbies must not have abstract base classes, decorators, etc). D language is too much complex for a newbie. Java is not too much bad, but it's requires to write too much code, it's too much fussy (semicolons at the end of lines? Newbies say that the computer is an idiot when it refuses code just because there's a missing useless semicolon!), and it misses some necessary things (first class functions! Damn). A nice language like Boo running on the Mono VM seems another option :-) In the past Pascal was good enough, but I think it's not good enough anymore. The problem is that teaching is a niche activity (even if a very important one). PLT Scheme is one of the few environments (beside Squeak, Python itself, and little more) that look refined and implemented well enough for such purpose. See you later, bearophile From andrew.henshaw at gtri.gatech.edu Tue Jul 7 17:48:37 2009 From: andrew.henshaw at gtri.gatech.edu (Andrew Henshaw) Date: Tue, 7 Jul 2009 17:48:37 -0400 Subject: finding most common elements between thousands of multiple arrays. References: Message-ID: "mclovin" wrote in message news:c5332c9b-2348-4194-bfa0-d70c7710765d at x3g2000yqa.googlegroups.com... > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating I up the count on the corrosponding dictionary element. I > then iterate through the dictionary and find the 25 most common > elements. > > the elements are initially held in a array within an array. so I am am > just trying to find the most common elements between all the arrays > contained in one large array. > my current code looks something like this: > d = {} > for arr in my_array: > -----for i in arr: > #elements are numpy integers and thus are not accepted as dictionary > keys > -----------d[int(i)]=d.get(int(i),0)+1 > > then I filter things down. but with my algorithm that only takes about > 1 sec so I dont need to show it here since that isnt the problem. > > > But there has to be something better. I have to do this many many > times and it seems silly to iterate through 2 million things just to > get 25. The element IDs are integers and are currently being held in > numpy arrays in a larger array. this ID is what makes up the key to > the dictionary. > > It currently takes about 5 seconds to accomplish this with my current > algorithm. > > So does anyone know the best solution or algorithm? I think the trick > lies in matrix intersections but I do not know. Would the following work for you, or am I missing something? For a 5Kx5K array, this takes about a tenth of a second on my machine. This code doesn't deal with the sub-array issue. ##################### import numpy import time LOWER = 0 UPPER = 1024 SIZE = 5000 NUM_BEST = 4 # sample data data = numpy.random.randint(LOWER, UPPER, (SIZE,SIZE)).astype(int) time.clock() count = numpy.bincount(data.flat) best = sorted(zip(count, range(len(count))))[-NUM_BEST:] print 'time=', time.clock() print best From python at bdurham.com Tue Jul 7 17:54:14 2009 From: python at bdurham.com (python at bdurham.com) Date: Tue, 07 Jul 2009 17:54:14 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? In-Reply-To: <85hbxodwa1.fsf@agentultra.com> References: <85ljn0ej4h.fsf@agentultra.com> <85hbxodwa1.fsf@agentultra.com> Message-ID: <1247003654.9346.1323970563@webmail.messagingengine.com> > If you do write the interpreter, let me know. I would certainly experiment with it. +2 over here! Malcolm From nobody at nowhere.com Tue Jul 7 18:37:43 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 07 Jul 2009 23:37:43 +0100 Subject: Python and webcam capture delay? References: <6xB4m.21104$vi5.8567@uutiset.elisa.fi> Message-ID: On Tue, 07 Jul 2009 09:01:39 +0300, jack catcher (nick) wrote: > Thanks for the comments. Unfortunately, such specifications aren't easy > to find, even in reviews. Fortunately several newer webcams seem at > least to use usb2. Supporting USB-2 doesn't mean that the camera necessarily uses high-speed (480Mbit/sec). AFAIK, the only real difference between "USB-1 conformant" and "USB-2 conformant" is that the latter actually passed a test of its ability to say "no, I can't do high-speed", while the former didn't. The check for high-speed capability was designed such that it shouldn't cause problems for USB-1 devices, but individual USB-1 devices weren't actually tested for this. From gagsl-py2 at yahoo.com.ar Tue Jul 7 18:50:57 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 19:50:57 -0300 Subject: Python Error from Apress book References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> Message-ID: En Tue, 07 Jul 2009 09:55:13 -0300, Dave Angel escribi?: > Gabriel Genellina wrote: >> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >> escribi?: >>> When I try to run the command as outlined in >>> the book "simple_markup2.py < test_input.txt > test_output.html i get >>> the >>> following error every time. >>> >>> IOError: [Errno 9] Bad file descriptor >> >> That's a Windows problem. When you execute the script as itself (either >> as you do in the command line, or by double-clicking on it), it doesn't >> have valid standard handles. >> You have to invoke Python explicitely: >> >> python simple_markup2.py < test_input.txt > test_output.html >> >> (you may need to specify the full path to python.exe, or add the >> directory where Python is installed to your system PATH). >> > I use stdout this way all the time, with no problem (python 2.6, Windows > XP). But as you point out, stdin redirection doesn't seem to work using > the file associations. I do get a different error though. When I look > at sys.stdin, it shows an open file, with handle of zero, as expected. > But when I do a raw_input(), it gets: > EOFError: EOF when reading a line I think the error depends on the specific OS version/service pack. But at least on XP this appears to fix it: http://support.microsoft.com/kb/321788/en-us -- Gabriel Genellina From philr at aspexconsulting.co.nz Tue Jul 7 19:04:25 2009 From: philr at aspexconsulting.co.nz (Phil Runciman) Date: Wed, 8 Jul 2009 11:04:25 +1200 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: -----Original Message----- From: Dennis Lee Bieber [mailto:wlfraed at ix.netcom.com] Sent: Tuesday, 7 July 2009 4:45 p.m. To: python-list at python.org Subject: Re: A Bug By Any Other Name ... On Mon, 6 Jul 2009 19:48:39 -0700, Daniel Fetchinson declaimed the following in gmane.comp.python.general: > Yes, there are plenty of languages other than Java and C, but the > influence of C is admittedly huge in Python. Why do you think loops > are called "for", conditionals "if" or "while", functions return via > "return", loops terminate via "break" and keep going via "continue" > and why is comparison written as "==", etc, etc? All of these are > coming from C (or an even earlier language) and my point is that users for, if, and return were common keywords in FORTRAN. Not to mention BASIC Both of which predate C -- __________________________________________________________________________ Guido was probably influenced by the ALGOL language stream, which used "for" and "if". ALGOL 60 was a joint American and European effort and was significant in the late 50s and 60's. Guido's fellow countryman, Edsgar Dijkstra, took this publication language (the ALGOL60 version) and produced a compiler for it. (It was not the first, but was very early on). Then B. Randell and L.J. Russell visited him, learnt from his experiences and returned to the UK to produce the KDF9 Whetstone ALGOL60 Compiler. Their book "ALGOL 60 Implementation" was a very early and influential book in the field and occupies a significant place in the history of computing. Computer language designers, including Nicholas Wirth (Pascal, Modula, Oberon), have been influenced by ALGOL. Sadly, "C" and its ilk, come from a totally different stream of language development beginning with the likes of "CPL", "BCPL", "B" and developing into "C" and "C++". This stream was originally focussed on language portability and performance. This stream was much closer to the assembler language end of the language spectrum, whereas the other stream was heavily biased towards publication and later teaching. I could say more but will restrain myself. My 2c Phil FWIW "++" reeks of assembler language notation. The KDF9 Whetstone ALGOL60 Compiler was the one I used at Whetstone for 1-Dimensional Transient Heat-Flow calculations and Steam-Table generation. Our course on it was 2.5 days long. We had wonderful training by English Electric, Kidsgrove staff. I hate to think how long the same course would be now, a month? From norseman at hughes.net Tue Jul 7 19:07:04 2009 From: norseman at hughes.net (norseman) Date: Tue, 07 Jul 2009 16:07:04 -0700 Subject: tough-to-explain Python In-Reply-To: References: Message-ID: <4A53D518.6080306@hughes.net> Bearophile wrote: > kj, as Piet van Oostrum as said, that's the difference between mutable > an immutable. It comes from the procedural nature of Python, and > probably an explanation of such topic can't be avoided if you want to > learn/teach Python. ...(snip) > > See you later, > bearophile ==========================Perhaps because it is the way I learned or the way I "naturally" approach programming, or maybe the way one influences the other, but at any rate I think "newbies" to programming should first learn a few basic concepts (if/then, for, while, do and other loops and other control constructs) and then be forced to deal with the computer on it's own terms. Assembly. Once the student learns what the computer already knows, that the whole thing is just bits and the combination of them determines it's responses, it then becomes easier to present 'idealistic' concepts and their implementations. With the knowledge and 'mental picture' of the computer's nature the rest of the ideas for programming have a tendency to drift in the direction of reality and the underlying needs to fulfill the 'better' and/or 'easier' languages. Having both the knowledge of the 'full capabilities' of a computer and the experience of a formalized language the student can, or should, learn/compare the trade offs of each. By understanding the computer (I at least) grasp the basics of a new language quite quickly. No, I don't become a guru overnight, if at all, but I do have the advantage in deciding if a given language is appropriate for a given job with very little research. The more I delve into OOP the more I liken an 'object' to a box. A box with a shipping manifest. There are big boxes, little boxes, squat boxes and so on. A box can contain corn flakes, bullets, raisins, rice, burlap, silk, motorcycle(s), soap and more. The manifest describes contents. The manifest is there but the description(s) change with content (type). The descriptions always use one or more of the basics like: color, count, dimension and so forth. Just like an OOP object. A box can contain things of all sorts, including references to the contents of other box(es). A box can even be a virtual of another (the global concept). The return statement, in this context, means hauling the contents of the box (and/or its manifest) back to (wherever) and disposing of the current box (a local). Just like an OOP object. It is easier to visualize a box and it's use than a non described blob. Abstracts are never precise - hence the evolution of the word. The one thing a teacher will always fail is the same as anyone else who tries to adequately describe a pretty sunset to a person born totally blind. No point(s) of reference. Time for me to sign off. To all those that helped me when I needed it - I thank you very much. Food for thought: your watch (clock) does not tell time. The watch (clock) only mimics one movement of the earth. ie... 3 dimensions are still static, the 4th is movement. Steve From stef.mientki at gmail.com Tue Jul 7 19:12:08 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Wed, 08 Jul 2009 01:12:08 +0200 Subject: Python/pyobjC Apps on iPhone now a possibility? In-Reply-To: <85ljn0ej4h.fsf@agentultra.com> References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: <4A53D648.60005@gmail.com> >> So, the question is, can the same thing be done for Python apps? >> > > I love Python and all, but it'd be apt to ask, what's the point? > > The iPhone is running on what? A 400Mhz ARM processor? Resources on the > device are already limited; running your program on top of an embedded > Python interpreter would only be adding pressure to the constraints; > even if it was an optimized interpreter. > I don't know iPhone, but I've done some experiments with 400 MHz arm, running Windows Mobile, and found PocketPyGUI running very very well on these devices. cheers, Stef Mientki From james at agentultra.com Tue Jul 7 19:29:25 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 07 Jul 2009 19:29:25 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: <85d48cdqcq.fsf@agentultra.com> Stef Mientki writes: >>> So, the question is, can the same thing be done for Python apps? >>> >> >> I love Python and all, but it'd be apt to ask, what's the point? >> >> The iPhone is running on what? A 400Mhz ARM processor? Resources on the >> device are already limited; running your program on top of an embedded >> Python interpreter would only be adding pressure to the constraints; >> even if it was an optimized interpreter. >> > I don't know iPhone, > but I've done some experiments with 400 MHz arm, running Windows Mobile, > and found PocketPyGUI running very very well on these devices. > > cheers, > Stef Mientki Sure, but it's pretty relative in the sense that it might be fast enough if I'm sitting around but too slow if I want to enter some information in the app before the next train comes. As a programmer, I don't really see the benefit of using an embedded interpreter on the iPhone. Objective-C isn't the greatest language, but it's easy to learn and well supported. It also compiles into some pretty speedy executables. If you can sacrifice a little run-time speed for your users in exchange for ease of development on your part, all the more to you. My original point was that I don't see the benefit in that decision. From gagsl-py2 at yahoo.com.ar Tue Jul 7 19:44:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 20:44:45 -0300 Subject: Embedded Python : Why does thread lock here? References: <00e8b4e8-5386-4f3d-b900-ab1ba41294f2@q40g2000prh.googlegroups.com> Message-ID: En Tue, 07 Jul 2009 15:01:17 -0300, roschler escribi?: > I have the Python Intepreter embedded in a Delphi (Object Pascal) > program. In the Python script shown below, I have a module that > creates a thread object and starts it. Do you *execute* the module or do you *import* it? Isn't a good idea to spawn a thread by side effect of importing a module. > The thread's run() call calls > a function called deleteOutputVariables() declared at the module > level. In my code's first incarnation the thread's run() call would > deadlock when trying to call the deleteOutputVariables() function > declared at Module level. I would never see the statement > "(deleteOutputVariables) Top of Call" printed to the screen. I now > know that I must periodically call join() with a very fast time-out to > keep Python threads happy, and that solved the problem. What does the Delphi code? Isn't a join with a long timeout enough? > However I am > curious as to why it deadlocked at the deleteOutputVariables() call? > Is it because deleteOutputVariables() is declared at the module level > or because that function deletes module level variables? If so why? I don't know, but you can make some experiments - move te deleteOutputVariables as a method, or don't delete module level variables at all, and see what happens. I'd say both factors are irrelevant. > # FUNCTION: Delete the variables given in the list. > def deleteOutputVariables(theModule, theOutputVariablesListOfNames): > try: > print "(deleteOutputVariables) Top of call." > for theOutputVariableName in theOutputVariablesListOfNames: > if theModule.__dict__.has_key(theOutputVariableName): > print "(Python::deleteOutputVariables) Deleting the > Output Variable named " + theOutputVariableName > del theModule.__dict__[theOutputVariableName] > except: > print "(deleteOutputVariables) Exception occurred." As a rule, avoid using __special__ names. There is almost never need of using them (__init__ is a notable exception) unless you want to change Python behaviour. In this case: for theOutputVariableName in theOutputVariablesListOfNames: if hasattr(theModule, theOutputVariableName): delattr(theModule, theOutputVariableName) (a name like theOutputVariablesListOfNames is too long for my taste, but that's a matter of style...) > theNewThread = None > theNewThread = threadRun("TestThread") That first line is useless... -- Gabriel Genellina From stef.mientki at gmail.com Tue Jul 7 19:46:51 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Wed, 08 Jul 2009 01:46:51 +0200 Subject: Python/pyobjC Apps on iPhone now a possibility? In-Reply-To: <85d48cdqcq.fsf@agentultra.com> References: <85ljn0ej4h.fsf@agentultra.com> <85d48cdqcq.fsf@agentultra.com> Message-ID: <4A53DE6B.8040706@gmail.com> J Kenneth King wrote: > Stef Mientki writes: > > >>>> So, the question is, can the same thing be done for Python apps? >>>> >>>> >>> I love Python and all, but it'd be apt to ask, what's the point? >>> >>> The iPhone is running on what? A 400Mhz ARM processor? Resources on the >>> device are already limited; running your program on top of an embedded >>> Python interpreter would only be adding pressure to the constraints; >>> even if it was an optimized interpreter. >>> >>> >> I don't know iPhone, >> but I've done some experiments with 400 MHz arm, running Windows Mobile, >> and found PocketPyGUI running very very well on these devices. >> >> cheers, >> Stef Mientki >> > > Sure, but it's pretty relative in the sense that it might be fast enough > if I'm sitting around but too slow if I want to enter some information > in the app before the next train comes. > > As a programmer, I don't really see the benefit of using an embedded > interpreter on the iPhone. Objective-C isn't the greatest language, but > it's easy to learn and well supported. It also compiles into some > pretty speedy executables. > > If you can sacrifice a little run-time speed for your users in exchange > for ease of development on your part, all the more to you. > > My original point was that I don't see the benefit in that decision. > > > ok 20 years ago I might have agreed with you, but now a days, speed of development is a much more important decision maker than speed of execution ;-) cheers, Stef From freelancer317 at gmail.com Tue Jul 7 19:49:54 2009 From: freelancer317 at gmail.com (Bret Fledderjohn) Date: Tue, 7 Jul 2009 19:49:54 -0400 Subject: tough-to-explain Python In-Reply-To: <4A53D518.6080306@hughes.net> References: <4A53D518.6080306@hughes.net> Message-ID: I really enjoyed your boxes analogy, from a guy with a trucking background, it makes a lot of sense! -Bret > ... The more I delve into OOP the more I liken an 'object' to a box. A box > with a shipping manifest. > > There are big boxes, > little boxes, > squat boxes and so on. > > A box can contain corn flakes, > bullets, raisins, rice, burlap, silk, motorcycle(s), soap and more. > > The manifest describes contents. > The manifest is there but the description(s) change with content (type). > The descriptions always use one or more of the basics like: color, count, > dimension and so forth. > > Just like an OOP object. > > A box can contain things of all sorts, including references to the contents > of other box(es). A box can even be a virtual of another (the global > concept). The return statement, in this context, means hauling the contents > of the box (and/or its manifest) back to (wherever) and disposing of the > current box (a local). > > Just like an OOP object. > > > It is easier to visualize a box and it's use than a non described blob. > Abstracts are never precise - hence the evolution of the word. > > > The one thing a teacher will always fail is the same as anyone else who > tries to adequately describe a pretty sunset to a person born totally blind. > No point(s) of reference. > > > > Time for me to sign off. To all those that helped me when I needed it - > > I thank you very much. > > Food for thought: your watch (clock) does not tell time. > The watch (clock) only mimics one movement of the earth. > ie... 3 dimensions are still static, the 4th is movement. > > > Steve -- - Bret -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Tue Jul 7 19:57:35 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Jul 2009 11:57:35 +1200 Subject: Check file is locked? References: Message-ID: In message , Christian Heimes wrote: > By the way most operating systems don't lock a file when it's opened for > reading or writing or even executed. The general conclusion seems to be that mandatory locking is more trouble than it's worth. From ben+python at benfinney.id.au Tue Jul 7 20:06:07 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 08 Jul 2009 10:06:07 +1000 Subject: tough-to-explain Python References: Message-ID: <8763e4ox74.fsf@benfinney.id.au> kj writes: > In Chris Rebert writes: > > >You might find the following helpful (partially): > >http://effbot.org/zone/call-by-object.htm > > Extremely helpful. Thanks! (I learned more from it than someone > who will teach the stuff would care to admit...) I have got very good results from teaching using the analogy of ?paper tags tied to physical objects? to describe Python's references to values. The analogy allows illustration of many otherwise confusing facts: * an assignment is the act of putting a tag onto an object * a tag leads to exactly one object, and it can be re-tied to a different object * an object can have arbitrarily many tags on it * containers (e.g. lists, dicts, sets, etc.) don't contain objects, they contain tags leading to objects * passing arguments to a function always makes new tags leading to the objects passed in, and throws those new tags away once the function returns * etc. The analogy can then be contrasted to how Python *doesn't* do it: named boxes with one value per box. You can point out that many other languages do use this model, so they should be aware of it, but Python doesn't use it. > I had not realized how *profoundly* different the meaning of the > "=" in Python's > > spam = ham Access the object referenced by ?ham?, and assigns the reference ?spam? to that object. > is from the "=" in its > > spam[3] = ham[3] Access the object referenced by ?ham[3]?, and assigns the reference ?spam[3]? to that object. No, they're exactly the same. The only thing that's different is the references you use; the assignment operates *exactly* the same in both cases. -- \ ?We are no more free to believe whatever we want about God than | `\ we are free to adopt unjustified beliefs about science or | _o__) history [?].? ?Sam Harris, _The End of Faith_, 2004 | Ben Finney From gagsl-py2 at yahoo.com.ar Tue Jul 7 20:45:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 21:45:15 -0300 Subject: Write matrix to text file References: <564970db0907071046o60e927dao21cc0557f1fa2903@mail.gmail.com> Message-ID: En Tue, 07 Jul 2009 14:46:47 -0300, Hanna Michelsen escribi?: > I'm working with both python and matlab at the moment and I was > wondering if > there is an efficient way to take a 2-D array (of 1s and 0s) in python > and > write it to a text file such that matlab will be able to create a sparse > matrix from it later. Your yesterday post appeared on this list successfully. Posting the very same text again isn't going to help - those that are reading it now are likely the same that read it before. If nobody replied, assume that something is wrong with the question itself. Not the grammar, looks clear to me. Maybe the meaning: you say "a 2-D array" but you don't say what that means (a list? an array.array object? a Numpy array?). Consider *who* might be able to answer: someone that knows both Python and matlab, and knows how to use a sparse matrix, and knows how to read data from files, and knows how to generate such files from Python. The last part is the easy part, because people here is supposed to know how to do things in Python; but not how matlab works. So, please help people help you: tell us what file format would be fine for matlab to read, and people here surely will suggest the best way to write that in Python. Reading http://www.mikeash.com/getting_answers.html may help too. -- Gabriel Genellina From gallium.arsenide at gmail.com Tue Jul 7 20:55:13 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 7 Jul 2009 17:55:13 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <8fa4563b-9de7-44cb-a8a2-a6d7c87a0ce4@q11g2000yqi.googlegroups.com> On Jul 7, 5:11?pm, kj wrote: > I don't plan to present these examples to them. > But beginners have a way of bumping into such > conundrums all on their own [...]. ?I doubt that > an answer of the form "don't worry your pretty > little head over this now; wait until your second > course" will do the trick. I agree that beginners are bound to come across difficult issues on their own, and when they do, you have to at least try to explain, rather than dismiss them. I believe that the beginners which are most curious, and thus most likely to run into things you didn't plan for them, are also most likely to be ready and able to receive your explanation. That said, I think it's worth planning a fairly controlled flow of information. For example, you can go a LONG way without touching tuples or the augmented assignment operators. For your function example, I suppose the key ideas to understand are binding and containers (which may or may not be mutable). The oversimplified version I think I would attempt to explain is that, as far as the "outside world" is concerned, a function cannot rebind the arguments passed to it. However, the contents of a mutable container may be altered without rebinding. That is, you can hold a bucket, pass that bucket to a function, and the function can put stuff in the bucket or take stuff out without you ever letting go of the bucket. When the function returns, you are still holding the bucket. Nested containers, especially with "outside" bindings to inner containers, may be tougher to visualize with real-world objects. Hopefully by then the students can grasp a more abstract (pointerlike) notion of binding! Good luck! John From usernet at ilthio.net Tue Jul 7 20:59:05 2009 From: usernet at ilthio.net (Tim Harig) Date: Wed, 08 Jul 2009 00:59:05 GMT Subject: IP Address Function References: Message-ID: On 2009-07-08, Fred Atkinson wrote: > Is there a Python function I can use to get the user's IP > address so I can display it on his browser? If you are using CGI you can get it from the REMOTE_ADDR environmental variable. From clp2 at rebertia.com Tue Jul 7 21:09:09 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 7 Jul 2009 18:09:09 -0700 Subject: IP Address Function In-Reply-To: References: Message-ID: <50697b2c0907071809y57b78162odd06b94cfff54d01@mail.gmail.com> On Tue, Jul 7, 2009 at 6:45 PM, Fred Atkinson wrote: > ? ? ? ?Is there a Python function I can use to get the user's IP > address so I can display it on his browser? from socket import gethostname, gethostbyname ip = gethostbyname(gethostname()) Cheers, Chris -- http://blog.rebertia.com From http Tue Jul 7 21:24:28 2009 From: http (Paul Rubin) Date: 07 Jul 2009 18:24:28 -0700 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> <1f09e856-bd04-4d27-bf51-5d67953a7e13@y7g2000yqa.googlegroups.com> Message-ID: <7xskh83r1v.fsf@ruckus.brouhaha.com> pdpi writes: > while abs(func(guess) - target) > epsilon: > guess = (lo + hi) / 2. > if sense * func(guess) > sense * target: > hi = guess > elif sense * func(guess) < sense * target: > lo = guess > elif lo == hi: > return None > return guess That is completely confusing. I get the, er, impression that "sense" is supposed to change during the loop, and it takes much head scratching to tell whether what you have there is right or not. Also, it calls func 3 times on each loop, which could be extremely slow. You don't know what func does, so eliminating 2/3 of the calls to it is not a micro-optimization--it could be a major time saving. Yet another version: def _binary_search(x0, x1, func, target, epsilon): y0,y1 = func(x0), func(x1) while abs(y1 - target) > epsilon: if x0 == x1 or cmp(y0,target) == cmp(y1,target): return None xn = (x0 + x1) / 2. yn = func(xn) if cmp(yn,target) == cmp(y0,target): x0,y0 = xn,yn else: x1,y1 = xn,yn return x1 From gallium.arsenide at gmail.com Tue Jul 7 21:26:13 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 7 Jul 2009 18:26:13 -0700 (PDT) Subject: tough-to-explain Python References: <8763e4ox74.fsf@benfinney.id.au> Message-ID: <197bfc81-05a6-4328-acec-3ddd5b733655@d32g2000yqh.googlegroups.com> On Jul 7, 8:06?pm, Ben Finney wrote: > I have got very good results from teaching using > the analogy of ?paper tags tied to physical objects? > to describe Python's references to values. Ah, I like that! I think it's better than what I used in my post (which I composed and submitted before yours showed up in my reader). I am not formally a teacher, but I do try to help "nonprogrammers" learn Python from time to time, and this will be a good one to remember. John From gagsl-py2 at yahoo.com.ar Tue Jul 7 21:30:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 22:30:07 -0300 Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> <4A5344BE.2080006@sequans.com> Message-ID: En Tue, 07 Jul 2009 09:51:10 -0300, Jean-Michel Pichavant escribi?: > I've never used sense in that way before, nor I've seen used by others > until now. However Kj is right, and my dictionary seems wrong > (wordreference.com). I've searched through others dictionaries and find > out this is actually applicable to functions. My bad. Using a common word with its common meaning is important too in order to understand the code. It's hard enough for students to grasp the algorithm itself, why make it artificially harder by using strange variable names. Some years ago I had to endure using an in-house framework with names like bring_XXX and fix_XXX instead of the usual names get_XXX and set_XXX (that was C++, emulating properties; I'm not sure of the actual verbs used, perhaps "obtain" and "establish", but certainly not get/set/put). Add some undecipherable comments in spanglish, profuse usage of macros that alter the lexical appearance of the language, and even reading code was a torture. -- Gabriel Genellina From fatkinson at mishmash.com Tue Jul 7 21:45:24 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Tue, 07 Jul 2009 18:45:24 -0700 Subject: IP Address Function Message-ID: Is there a Python function I can use to get the user's IP address so I can display it on his browser? Regards, Fred From gagsl-py2 at yahoo.com.ar Tue Jul 7 21:54:03 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 22:54:03 -0300 Subject: IP Address Function References: Message-ID: En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson escribi?: > Is there a Python function I can use to get the user's IP > address so I can display it on his browser? There is a long distance between "Python" and "browser" - you'll have to tell us what is in between the two. By example, do you have a server and the user connects to it? is it running Python? how do you run the Python application? And why do you want to do that on the server side? Isn't easier to do that on the client side? What about proxies? NAT? If using CGI, look at the REMOTE_ADDR environment variable. -- Gabriel Genellina From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 22:03:16 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 02:03:16 GMT Subject: tough-to-explain Python References: Message-ID: <006fd94e$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 21:18:53 +0000, kj wrote: > I had not realized how *profoundly* different the meaning of the "=" in > Python's > > spam = ham > > is from the "=" in its > > spam[3] = ham[3] > > So much for "explicit is better than implicit"... I'm sorry, I don't get it. Can you explain please? I don't see why it's so "profoundly" different. Apart from spam[3] = x not being permitted if spam is an immutable type. I suppose though they are *fundamentally* different, in that spam=ham is dealt with by the compiler while spam[3]=ham is handled by the object spam using the __setitem__ method. Is that the difference you're talking about? -- Steven From python at bdurham.com Tue Jul 7 22:40:05 2009 From: python at bdurham.com (python at bdurham.com) Date: Tue, 07 Jul 2009 22:40:05 -0400 Subject: tough-to-explain Python In-Reply-To: <197bfc81-05a6-4328-acec-3ddd5b733655@d32g2000yqh.googlegroups.com> References: <8763e4ox74.fsf@benfinney.id.au> <197bfc81-05a6-4328-acec-3ddd5b733655@d32g2000yqh.googlegroups.com> Message-ID: <1247020805.1725.1324003095@webmail.messagingengine.com> Ben, > I have got very good results from teaching using the analogy of "paper tags tied to physical objects" to describe Python's references to values. Great analogy!! And an excellent analogy for newcomers to Python. (this would have saved me some personal pain in my early days). Regards, Malcolm From gagsl-py2 at yahoo.com.ar Tue Jul 7 23:02:53 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Jul 2009 00:02:53 -0300 Subject: tough-to-explain Python References: Message-ID: En Tue, 07 Jul 2009 17:04:46 -0300, kj escribi?: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > >>>> ham = [1, 2, 3, 4] >>>> spam = (ham,) >>>> spam > ([1, 2, 3, 4],) >>>> spam[0] is ham > True >>>> spam[0] += [5] > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment >>>> ham += [5] >>>> spam > ([1, 2, 3, 4, 5, 5],) > > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. This article is based on an old thread in this list that is very enlightening: How To Think Like A Pythonista http://python.net/crew/mwh/hacks/objectthink.html and I'll quote just a paragraph from Alex Martelli: ?There is [...] a huge difference between changing an object, and changing (mutating) some OTHER object to which the first refers. In Bologna over 100 years ago we had a statue of a local hero depicted pointing forwards with his finger -- presumably to the future, but given where exactly it was placed, the locals soon identified it as "the statue that points to Hotel Belfiore". Then one day some enterprising developer bought the hotel's building and restructured it -- in particular, where the hotel used to be was now a restaurant, Da Carlo. So, "the statue that points to Hotel Belfiore" had suddenly become "the statue that points to Da Carlo"...! Amazing isn't it? Considering that marble isn't very fluid and the statue had not been moved or disturbed in any way...? This is a real anecdote, by the way (except that I'm not sure of the names of the hotel and restaurant involved -- I could be wrong on those), but I think it can still help here. The dictionary, or statue, has not changed at all, even though the objects it refers/points to may have been mutated beyond recognition, and the name people know it by (the dictionary's string-representation) may therefore change. That name or representation was and is referring to a non-intrinsic, non-persistent, "happenstance" characteristic of the statue, or dictionary...? -- Gabriel Genellina From peter.milliken at gmail.com Tue Jul 7 23:18:20 2009 From: peter.milliken at gmail.com (Peter) Date: Tue, 7 Jul 2009 20:18:20 -0700 (PDT) Subject: How to use Python to interface with Web pages? Message-ID: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Any help would be appreciated :-) I want to write an auction sniping tool in Python. I know Python, but I know absolutely nothing about web pages, javascript etc i.e. I want the program to automatically log me into my eBay account, access the appropriate item, locate how many mins/seconds until the bid time ends and then automatically place a bid at the last possible moment. I can see the web-page source - it looks to be javascript (to my untutored eye :-)). But how do I enter data and "simulated" mouse presses on a web-page that I have accessed via a Python program? So where can I start to learn how to do this? Any books available? web resources that show examples or offer tutorials? I have (in the past) written some python to scoop data off web-sites but I have never written anything that interactively interacts with the web-page contents and don't know where to even start on this one. Thanks for any help/pointers Peter From ralf at schoenian-online.de Wed Jul 8 00:05:53 2009 From: ralf at schoenian-online.de (Ralf Schoenian) Date: Wed, 08 Jul 2009 06:05:53 +0200 Subject: How to use Python to interface with Web pages? In-Reply-To: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> References: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Message-ID: <4A541B21.3030203@schoenian-online.de> Peter wrote: > Any help would be appreciated :-) > > I want to write an auction sniping tool in Python. I know Python, but > I know absolutely nothing about web pages, javascript etc i.e. I want > the program to automatically log me into my eBay account, access the > appropriate item, locate how many mins/seconds until the bid time ends > and then automatically place a bid at the last possible moment. > > I can see the web-page source - it looks to be javascript (to my > untutored eye :-)). > > But how do I enter data and "simulated" mouse presses on a web-page > that I have accessed via a Python program? > > So where can I start to learn how to do this? Any books available? web > resources that show examples or offer tutorials? > > I have (in the past) written some python to scoop data off web-sites > but I have never written anything that interactively interacts with > the web-page contents and don't know where to even start on this one. > > Thanks for any help/pointers > > Peter Hi, a good starting point possibly is http://wwwsearch.sourceforge.net/mechanize/ Regards, Ralf From sajmikins at gmail.com Wed Jul 8 00:10:38 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 21:10:38 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> On Jul 7, 4:04 pm, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > > Consider the following interaction sequence: > > >>> def eggs(some_int, some_list, some_tuple): > > ... some_int += 2 > ... some_list += [2] > ... some_tuple += (2,) > ... > > >>> x = 42 > >>> y = (42,) > >>> z = [42] > >>> eggs(x, y, z) > >>> x > 42 > >>> y > (42,) > >>> z > [42, 2] You have transposed some_list and some some_tuple. I.e. you should be calling eggs(x, z, y). > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? You don't. Rank beginners don't have enough background knowledge to grok that code. Why would you even tell the poor bastards about "+=" before they were comfortable with (python's style of) function calls, immutable integers, mutable lists and immutable tuples? Let them use "x = x + y" until they have enough knowledge to understand "augmented" assignment. Syntax matters (I mean general order of things, not linguistic syntax.) Making an omelette requires putting eggs in a pan and cracking them, but not in that order. > Or consider this one: > > >>> ham = [1, 2, 3, 4] > >>> spam = (ham,) > >>> spam > ([1, 2, 3, 4],) > >>> spam[0] is ham > True > >>> spam[0] += [5] > > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment > >>> ham += [5] > >>> spam > > ([1, 2, 3, 4, 5, 5],) > > > > What do you say to that? I say, "Don't use augmented assignment with indexed tuples." Seriously, python's augmented assignment is almost magical. I think you're just making trouble for yourself and your students if you introduce it too early. I get python pretty well (if I say so myself) but I wouldn't know how to explain: In [1]: def foo(a_list): ...: a_list = a_list + [5] ...: ...: In [2]: n = [] In [3]: foo(n) In [4]: n Out[4]: [] In [5]: def bar(a_list): ...: a_list += [5] ...: ...: In [6]: bar(n) In [7]: n Out[7]: [5] It's "Just The Way It Is". Freakin' magic, man. > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! Frankly, I'm of the impression that it's a mistake not to start teaching programming with /the bit/ and work your way up from there. I'm not kidding. I wrote a (draft) article about this: "Computer Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 I really think the only good way to teach computers and programming is to start with a bit, and build up from there. "Ontology recapitulates phylogeny" I realize that doesn't help you teach python, but I wanted to put it out there. From sjmachin at lexicon.net Wed Jul 8 00:30:26 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 7 Jul 2009 21:30:26 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: On Jul 8, 3:05?am, dana wrote: > I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC > modules together. Although I don't have Python 2.5, I've been informed > the DBI module has been deprecated at 2.5. > > A few questions: > > 1) Although deprecated, will it work at all in 2.5? Does the fact that > it is deprecrated mean it has been removed entirely, or does Python > 2.5 simply issuing a warning? Deprecated certainly doesn't mean removed. > > 2) What do I use in place of DBI for my Python 2.4. scripts that > import modules DBI and ODBC together. I don't use DBI directly. It was > simply a dependency for the ODBC module as best I knew. For a start, none of (DBI, ODBC, dbi, odbc) are standard Python- supplied modules. Perhaps you are referring to the odbc (and dbi) from the pywin32 package? Where did you get them from? If you can't remember, try this: |Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32 | Type "help", "copyright", "credits" or "license" for more information. | >>> import odbc | >>> odbc.__file__ | 'C:\\python24\\lib\\site-packages\\win32\\odbc.pyd' | >>> If this is what you're talking about, you should be asking on the pywin32 dicussion list (http://mail.python.org/mailman/listinfo/python- win32). General advice: if you are thinking of upgrading your Python version, go straight to 2.6. odbc is AFAIK stuck at version 1.0 of the Python DB API; consider switching to pyodbc (http://code.google.com/p/ pyodbc/) HTH, John From rajat.dudeja at gmail.com Wed Jul 8 00:31:12 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Tue, 7 Jul 2009 21:31:12 -0700 (PDT) Subject: Check file is locked? References: Message-ID: On Jul 8, 4:57?am, Lawrence D'Oliveiro wrote: > In message , Christian > > Heimes wrote: > > By the way most operating systems don't lock a file when it's opened for > > reading or writing or even executed. > > The general conclusion seems to be that mandatory locking is more trouble > than it's worth. My OS is a windows XP sp3. All I'm trying to achieve is to close an application ( which could be a notepad, a wordpad or some other text editor) that have opened my file. Once the file is closed I can easily delete that file. I guess, if file is opened by one of that application, the file must be locked and so is the reason I cannot delete the file. Please suggest if this is fine. From timr at probo.com Wed Jul 8 01:11:12 2009 From: timr at probo.com (Tim Roberts) Date: Tue, 07 Jul 2009 22:11:12 -0700 Subject: Python and webcam capture delay? References: Message-ID: <0ca855t29jklb0o0gi31ourfk3lstdiepf@4ax.com> Nobody wrote: > >The webcam is bound to do some encoding; most of them use USB "full speed" >(12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. That's not true. Most of the web cams made in the last 5 years or so run at high speed, 480 Mbps. Full speed only gets you 1 fps at 640x480 uncompressed, so it's really only useful for the most primitive video conference cams. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 02:09:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 06:09:19 GMT Subject: tough-to-explain Python References: Message-ID: <007012f9$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 20:04:46 +0000, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. [...] > Or consider this one: > >>>> ham = [1, 2, 3, 4] >>>> spam = (ham,) >>>> spam > ([1, 2, 3, 4],) >>>> spam[0] is ham > True >>>> spam[0] += [5] > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment >>>> ham += [5] >>>> spam > ([1, 2, 3, 4, 5, 5],) >>>> >>>> > What do you say to that? That one surely is very straight forward. Just like the exception says, tuples don't support item assignment, so spam[0] += [5] is not allowed. But just because you have put a list inside a tuple doesn't mean the list stops being a list -- you can still append to the list, which is what ham += [5] does. So spam is unchanged: it is still the one-item tuple containing a list. It is just that the list has now been modified. This is only troublesome (in my opinion) if you imagine that tuples are somehow magical "frozen-lists", where the contents can't be modified once created. That's not the case -- the tuple itself can't be modified, but the objects inside it remain ordinary objects, and the mutable ones can be modified. The thing to remember is that the tuple spam doesn't know anything about the *name* ham -- it knows the object referred to by the name ham. You can modify the name, and nothing happens to the tuple: >>> spam ([1, 2, 3, 4, 5],) >>> ham = [5] >>> spam ([1, 2, 3, 4, 5],) Or if you prefer: >>> ham = spam[0] # label the list inside spam as 'ham' >>> ham += [6] # modify the list labelled as 'ham' >>> spam ([1, 2, 3, 4, 5, 6],) >>> pork = ham # create a new label, 'pork', and bind it to the same list >>> del ham # throw away the label 'ham' >>> pork += [7] # modify the list labelled as 'pork' >>> spam ([1, 2, 3, 4, 5, 6, 7],) It's all about the objects, not the names. -- Steven From dhananjay.c.joshi at gmail.com Wed Jul 8 02:10:59 2009 From: dhananjay.c.joshi at gmail.com (Dhananjay) Date: Wed, 8 Jul 2009 11:40:59 +0530 Subject: count Message-ID: Dear all, I have file as follows,however, tab seperated (not shown in following file): 6 3 4.309726 7 65 93.377388 8 47 50.111952 9 270 253.045923 10 184 182.684670 11 76 121.853455 12 85 136.283470 13 114 145.910662 14 45 80.703013 15 44 47.154646 16 41 66.461339 17 16 33.819488 18 127 136.105455 19 70 88.798681 20 29 61.297823 I wanted to sort column 2 in assending order and I read whole file in array "data" and did the following: data.sort(key = lambda fields:(fields[2])) I have sorted column 2, however I want to count the numbers in the column 2. i.e. I want to know, for example, how many repeates of say '3' (first row, 2nd column in above data) are there in column 2. I could write seperate programme to get the result.s. However, is there any way to count the numbers there itself while sorting in column 2 ? Thanking you in advance, -- Dhananjay -------------- next part -------------- An HTML attachment was scrubbed... URL: From romeoamp at gmail.com Wed Jul 8 02:13:42 2009 From: romeoamp at gmail.com (romeoamp) Date: Tue, 7 Jul 2009 23:13:42 -0700 (PDT) Subject: SSH utility In-Reply-To: References: Message-ID: <24385961.post@talk.nabble.com> Please look at on : http://www.lag.net/paramiko/ http://www.lag.net/paramiko/ Sample Code: Find Attachment Thanks, S.V.RAJKUMAR, XOU Solutions India Private Limited No. 37, PM Towers, Greams Road, Thousand Lights, Chennai - 6 . Mobile No : +91 - 9940632275. half.italian wrote: > > On Aug 11, 5:17?am, Edwin.Mad... at VerizonWireless.com wrote: >> for similar tasks, I use pexpecthttp://pypi.python.org/pypi/pexpect. >> >> spawning bash process and simulate an interactive session. Here sending >> ls command, retrieving results and exiting. In the spawned process ssh or >> any other command, is just another command. >> >> ------------actual session-------------------------- >> $ python >> Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) >> [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin >> Type "help", "copyright", "credits" or "license" for more information.>>> >> import pexpect >> >>> c = pexpect.spawn('/bin/bash') >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('\x1b[?1034hmadared at NJWARHQD0IT696A:~\r\n', '$ ')>>> c.sendline('ls') >> 3 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('ls\r\x.txt ?xx.txt ?xy.txt ?y.txt\r\nmadared at NJWARHQD0IT696A:~\r\n', '$ >> ')>>> c.sendline('exit') >> 5 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 1 >> >>> c.before, c.after >> >> ('exit\r\nexit\r\n', )>>> exit() >> >> madared at NJWARHQD0IT696A:~ >> $ >> --------------------------------------------------------------- >> >> hope that helps. >> >> regards. >> Edwin >> >> -----Original Message----- >> From: python-list-bounces+edwin.madari=verizonwireless.... at python.org >> >> [mailto:python-list-bounces+edwin.madari=verizonwireless.... at python.org] >> On Behalf Of James Brady >> Sent: Monday, August 11, 2008 12:26 AM >> To: python-l... at python.org >> Subject: SSH utility >> >> Hi all, >> I'm looking for a python library that lets me execute shell commands >> on remote machines. >> >> I've tried a few SSH utilities so far: paramiko, PySSH and pssh; >> unfortunately all been unreliable, and repeated questions on their >> respective mailing lists haven't been answered... >> >> It seems like the sort of commodity task that there should be a pretty >> robust library for. Are there any suggestions for alternative >> libraries or approaches? >> >> Thanks! >> James >> --http://mail.python.org/mailman/listinfo/python-list >> >> The information contained in this message and any attachment may be >> proprietary, confidential, and privileged or subject to the work >> product doctrine and thus protected from disclosure. ?If the reader >> of this message is not the intended recipient, or an employee or >> agent responsible for delivering this message to the intended >> recipient, you are hereby notified that any dissemination, >> distribution or copying of this communication is strictly prohibited. >> If you have received this communication in error, please notify me >> immediately by replying to this message and deleting it and all >> copies and backups thereof. ?Thank you. > > I second pexpect and the nice little module that comes with it > ssh_session.py. > > Been using it for ages now! > > ~Sean > -- > http://mail.python.org/mailman/listinfo/python-list > > http://www.nabble.com/file/p24385961/sshclient.py sshclient.py -- View this message in context: http://www.nabble.com/SSH-utility-tp18920030p24385961.html Sent from the Python - python-list mailing list archive at Nabble.com. -------------- next part -------------- An HTML attachment was scrubbed... URL: From romeoamp at gmail.com Wed Jul 8 02:15:08 2009 From: romeoamp at gmail.com (romeoamp) Date: Tue, 7 Jul 2009 23:15:08 -0700 (PDT) Subject: SSH utility Message-ID: <24385961.post@talk.nabble.com> Please look at on : http://www.lag.net/paramiko/ Sample Code: Find Attachment Thanks, S.V.RAJKUMAR, XOU Solutions India Private Limited No. 37, PM Towers, Greams Road, Thousand Lights, Chennai - 6 . Mobile No : +91 - 9940632275. half.italian wrote: > > On Aug 11, 5:17?am, Edwin.Mad... at VerizonWireless.com wrote: >> for similar tasks, I use pexpecthttp://pypi.python.org/pypi/pexpect. >> >> spawning bash process and simulate an interactive session. Here sending >> ls command, retrieving results and exiting. In the spawned process ssh or >> any other command, is just another command. >> >> ------------actual session-------------------------- >> $ python >> Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) >> [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin >> Type "help", "copyright", "credits" or "license" for more information.>>> >> import pexpect >> >>> c = pexpect.spawn('/bin/bash') >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('\x1b[?1034hmadared at NJWARHQD0IT696A:~\r\n', '$ ')>>> c.sendline('ls') >> 3 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('ls\r\x.txt ?xx.txt ?xy.txt ?y.txt\r\nmadared at NJWARHQD0IT696A:~\r\n', '$ >> ')>>> c.sendline('exit') >> 5 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 1 >> >>> c.before, c.after >> >> ('exit\r\nexit\r\n', )>>> exit() >> >> madared at NJWARHQD0IT696A:~ >> $ >> --------------------------------------------------------------- >> >> hope that helps. >> >> regards. >> Edwin >> >> -----Original Message----- >> From: python-list-bounces+edwin.madari=verizonwireless.... at python.org >> >> [mailto:python-list-bounces+edwin.madari=verizonwireless.... at python.org] >> On Behalf Of James Brady >> Sent: Monday, August 11, 2008 12:26 AM >> To: python-l... at python.org >> Subject: SSH utility >> >> Hi all, >> I'm looking for a python library that lets me execute shell commands >> on remote machines. >> >> I've tried a few SSH utilities so far: paramiko, PySSH and pssh; >> unfortunately all been unreliable, and repeated questions on their >> respective mailing lists haven't been answered... >> >> It seems like the sort of commodity task that there should be a pretty >> robust library for. Are there any suggestions for alternative >> libraries or approaches? >> >> Thanks! >> James >> --http://mail.python.org/mailman/listinfo/python-list >> >> The information contained in this message and any attachment may be >> proprietary, confidential, and privileged or subject to the work >> product doctrine and thus protected from disclosure. ?If the reader >> of this message is not the intended recipient, or an employee or >> agent responsible for delivering this message to the intended >> recipient, you are hereby notified that any dissemination, >> distribution or copying of this communication is strictly prohibited. >> If you have received this communication in error, please notify me >> immediately by replying to this message and deleting it and all >> copies and backups thereof. ?Thank you. > > I second pexpect and the nice little module that comes with it > ssh_session.py. > > Been using it for ages now! > > ~Sean > -- > http://mail.python.org/mailman/listinfo/python-list > > http://www.nabble.com/file/p24385961/sshclient.py sshclient.py -- View this message in context: http://www.nabble.com/SSH-utility-tp18920030p24385961.html Sent from the Python - python-list mailing list archive at Nabble.com. From python at rcn.com Wed Jul 8 03:11:50 2009 From: python at rcn.com (Raymond Hettinger) Date: Wed, 8 Jul 2009 00:11:50 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> Message-ID: <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> [Scott David Daniels] > def most_frequent(arr, N): > ? ? ?'''Return the top N (freq, val) elements in arr''' > ? ? ?counted = frequency(arr) # get an iterator for freq-val pairs > ? ? ?heap = [] > ? ? ?# First, just fill up the array with the first N distinct > ? ? ?for i in range(N): > ? ? ? ? ?try: > ? ? ? ? ? ? ?heap.append(counted.next()) > ? ? ? ? ?except StopIteration: > ? ? ? ? ? ? ?break # If we run out here, no need for a heap > ? ? ?else: > ? ? ? ? ?# more to go, switch to a min-heap, and replace the least > ? ? ? ? ?# element every time we find something better > ? ? ? ? ?heapq.heapify(heap) > ? ? ? ? ?for pair in counted: > ? ? ? ? ? ? ?if pair > heap[0]: > ? ? ? ? ? ? ? ? ?heapq.heapreplace(heap, pair) > ? ? ?return sorted(heap, reverse=True) # put most frequent first. In Py2.4 and later, see heapq.nlargest(). In Py3.1, see collections.Counter(data).most_common(n) Raymond From wuwei23 at gmail.com Wed Jul 8 03:29:32 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 8 Jul 2009 00:29:32 -0700 (PDT) Subject: How to use Python to interface with Web pages? References: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Message-ID: <127e0a78-89fc-4c09-bda7-95836c57ac58@v37g2000prg.googlegroups.com> On Jul 8, 1:18?pm, Peter wrote: > I can see the web-page source - it looks to be javascript (to my > untutored eye :-)). > > But how do I enter data and "simulated" mouse presses on a web-page > that I have accessed via a Python program? > [...] > I have (in the past) written some python to scoop data off web-sites > but I have never written anything that interactively interacts with > the web-page contents and don't know where to even start on this one. I don't have much experience with it, but for interacting with javascript-heavy pages you might want to take a look at Selenium: http://seleniumhq.org/ http://pypi.python.org/pypi/selenium http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html http://joker.linuxstuff.pl/documentation/make_selenium It's primarily aimed at testing web app UIs, but if it allows for values to be passed into tests then 'test' == 'action' in terms of your requirements. Hope this helps. - alex23 From bieffe62 at gmail.com Wed Jul 8 03:32:07 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Wed, 8 Jul 2009 00:32:07 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <70654e24-aa1b-4a78-87a3-d36873da6109@i6g2000yqj.googlegroups.com> On Jul 7, 10:04?pm, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > > Consider the following interaction sequence: > > >>> def eggs(some_int, some_list, some_tuple): > > ... ? ? some_int += 2 > ... ? ? some_list += [2] > ... ? ? some_tuple += (2,) > ... > > >>> x = 42 > >>> y = (42,) > >>> z = [42] > >>> eggs(x, y, z) > >>> x > 42 > >>> y > (42,) > >>> z > [42, 2] > > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? > > Or consider this one: > > >>> ham = [1, 2, 3, 4] > >>> spam = (ham,) > >>> spam > ([1, 2, 3, 4],) > >>> spam[0] is ham > True > >>> spam[0] += [5] > > Traceback (most recent call last): > ? File "", line 1, in > TypeError: 'tuple' object does not support item assignment>>> ham += [5] > >>> spam > > ([1, 2, 3, 4, 5, 5],) > > > > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! > > TIA! > > kj I would go with something like this: """ In object oriented programming, the same function or operator can be used to represent different things. This is called overloading. To understand what the operator/function do, we have to look at the kind of object it is applied to. In this case, the operator "+=" means two different things: - for strings and numbers it means : "create a new object by merging the two operands". This is why the original object is left the same. - for lists, it means : "increase the left operand with the contents of the right operand". This is why the original object is changed """ You couuld also add: """ You see, in python everithing is an object. Some object can be changed (mutable objects), others cannot. """ but this is another story. P:S : Sometime I think they should not have allowed += on immutables and forced everybody to write s = s + "some more". Ciao ---- FB From mail at timgolden.me.uk Wed Jul 8 03:45:58 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 08 Jul 2009 08:45:58 +0100 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A544EB6.5090404@timgolden.me.uk> Rajat wrote: > On Jul 8, 4:57 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> In message , Christian >> >> Heimes wrote: >>> By the way most operating systems don't lock a file when it's opened for >>> reading or writing or even executed. >> The general conclusion seems to be that mandatory locking is more trouble >> than it's worth. > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > application ( which could be a notepad, a wordpad or some other text > editor) that have opened my file. Once the file is closed I can easily > delete that file. > > I guess, if file is opened by one of that application, the file must > be locked and so is the reason I cannot delete the file. I assume that your real requirement is: I can't open/close/delete this file; it must be locked by something else; what is that something else? The simplest thing is probably to run sysinternals' handle util: http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx and use the subprocess module to parse the output. That will give you the pid, and you can then use, eg, psutil: http://code.google.com/p/psutil/ to get the details of the process. TJG From piet at cs.uu.nl Wed Jul 8 04:11:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 10:11:35 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Simon Forman (SF) wrote: >SF> Why would you even tell the poor bastards about "+=" before they were >SF> comfortable with (python's style of) function calls, immutable >SF> integers, mutable lists and immutable tuples? >SF> Let them use "x = x + y" until they have enough knowledge to >SF> understand "augmented" assignment. And *then* you can tell them that "x += y" can be subtly different from "x = x + y", which is what happened in the example that the OP gave. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From gagsl-py2 at yahoo.com.ar Wed Jul 8 04:13:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Jul 2009 05:13:42 -0300 Subject: tough-to-explain Python References: <70654e24-aa1b-4a78-87a3-d36873da6109@i6g2000yqj.googlegroups.com> Message-ID: En Wed, 08 Jul 2009 04:32:07 -0300, Francesco Bochicchio escribi?: > I would go with something like this: > > """ > In object oriented programming, the same function or operator can be > used to represent > different things. This is called overloading. To understand what the > operator/function do, we have to look at > the kind of object it is applied to. > In this case, the operator "+=" means two different things: > - for strings and numbers it means : "create a new object by merging > the two operands". This is why the original object is left the same. > - for lists, it means : "increase the left operand with the contents > of the right operand". This is why the original object is changed > """ Mmm, but this isn't quite exact. += isn't an operator, but a statement (augmented assignment). a += b translates to: [*] a = a.__iadd__(b) [*] It's up to the __iadd__ implementation to return the same object or a new one, and the assignment occurs even if the same object is returned. If you think of an assignments as binding names to objects (well, that's the documented behavior, btw) things become more clear. > P:S : Sometime I think they should not have allowed += on immutables > and forced everybody to write s = s + "some more". I think not everyone agreed when += was introduced. But now, having s += "some more" allows the operation to be optimized. [*] except 'a' is evaluated only once -- Gabriel Genellina From helvinlui at gmail.com Wed Jul 8 04:51:07 2009 From: helvinlui at gmail.com (Helvin) Date: Wed, 8 Jul 2009 01:51:07 -0700 (PDT) Subject: PyQt GUI Message-ID: Hi experts! I'm developing a GUI for a software using PyQT, and need 3D visualization. Should I use PyOpenGL or VTK? I understand that the PyQt package comes with a opengl module. What else would I need? I think I need to download opengl. but how? where? I have VTK and pyVTK installed, but I don't know how to use it in my code, as when I run it, an error says it can't find the vtk module. Help would be sooooo appreciated! Helvin From fatkinson at mishmash.com Wed Jul 8 04:55:29 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Wed, 08 Jul 2009 01:55:29 -0700 Subject: IP Address Function References: Message-ID: On Tue, 07 Jul 2009 22:54:03 -0300, "Gabriel Genellina" wrote: >En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson >escribi?: > >> Is there a Python function I can use to get the user's IP >> address so I can display it on his browser? > >There is a long distance between "Python" and "browser" - you'll have to >tell us what is in between the two. I want to have a Web page come up (written in Python, cgi) that returns the IP address of the browser (user's PC's IP address). >By example, do you have a server and the user connects to it? is it >running Python? how do you run the Python application? >And why do you want to do that on the server side? Isn't easier to do that >on the client side? What about proxies? NAT? Yes. By CGI. >If using CGI, look at the REMOTE_ADDR environment variable. I did look at REMOTE_ADDR but I've been unable to figure out the correct way to code it. I've tried a number of ways but I've been unsuccessful. Ideally, I'd like to store the brower's IP address in a string and then print the string on the Web page from a Python CGI script. Regards, Fred From vilya.harvey at gmail.com Wed Jul 8 04:56:55 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Wed, 8 Jul 2009 09:56:55 +0100 Subject: count In-Reply-To: References: Message-ID: <6aef848f0907080156k114b0bb0me13b26904683d411@mail.gmail.com> 2009/7/8 Dhananjay : > I wanted to sort column 2 in assending order? and I read whole file in array > "data" and did the following: > > data.sort(key = lambda fields:(fields[2])) > > I have sorted column 2, however I want to count the numbers in the column 2. > i.e. I want to know, for example, how many repeates of say '3' (first row, > 2nd column in above data) are there in column 2. One thing: indexes in Python start from 0, so the second column has an index of 1 not 2. In other words, it should be data.sort(key = lambda fields: fields[1]) instead. With that out of the way, the following will print out a count of each unique item in the second column: from itertools import groupby for x, g in groupby([fields[1] for fields in data]): print x, len(tuple(g)) Hope that helps, Vil. From deets at nospam.web.de Wed Jul 8 05:11:51 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Jul 2009 11:11:51 +0200 Subject: PyQt GUI References: Message-ID: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Helvin wrote: > Hi experts! > > I'm developing a GUI for a software using PyQT, and need 3D > visualization. Should I use PyOpenGL or VTK? > I understand that the PyQt package comes with a opengl module. What > else would I need? I think I need to download opengl. but how? where? > I have VTK and pyVTK installed, but I don't know how to use it in my > code, as when I run it, an error says it can't find the vtk module. VTK won't mix with Qt. And I don't think you need to download opengl - it either comes with your system (or gfx-drivers), or not. I'm not sure if Qt actually wraps the OpenGL api itself, AFAIK it doesn't, so you need PyOpenGL I guess. Diez From phil at riverbankcomputing.com Wed Jul 8 05:23:10 2009 From: phil at riverbankcomputing.com (Phil Thompson) Date: Wed, 08 Jul 2009 10:23:10 +0100 Subject: PyQt GUI In-Reply-To: <7bj5ulF22b4ktU1@mid.uni-berlin.de> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Message-ID: <66fe05cfa1805aef3a0d4aadc4e7c9c1@localhost> On Wed, 08 Jul 2009 11:11:51 +0200, "Diez B. Roggisch" wrote: > Helvin wrote: > >> Hi experts! >> >> I'm developing a GUI for a software using PyQT, and need 3D >> visualization. Should I use PyOpenGL or VTK? >> I understand that the PyQt package comes with a opengl module. What >> else would I need? I think I need to download opengl. but how? where? >> I have VTK and pyVTK installed, but I don't know how to use it in my >> code, as when I run it, an error says it can't find the vtk module. > > VTK won't mix with Qt. And I don't think you need to download opengl - it > either comes with your system (or gfx-drivers), or not. I'm not sure if Qt > actually wraps the OpenGL api itself, AFAIK it doesn't, so you need > PyOpenGL I guess. VTK has explicit support for both Qt (ie. via C++) and PyQt. Phil From eckhardt at satorlaser.com Wed Jul 8 05:24:28 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Wed, 08 Jul 2009 11:24:28 +0200 Subject: Fractions as result from divisions (was: Re: tough-to-explain Python) References: Message-ID: Bearophile wrote: > For example a novice wants to see 124 / 38 to return the 62/19 > fraction and not 3 or 3.263157894736842 :-) Python has adopted the latter of the three for operator / and the the second one for operator //. I wonder if it was considered to just return a fraction from that operation. x = 3/5 -> x = fraction(3, 5) x = x*7 -> x = fraction(21, 5) x = x/2 -> x = fraction(21, 10) range(x) -> error, x not an integer range(int(x)) -> [0, 1,] y = float(x) -> y = 2.1 This would have allowed keeping the operator what people are used to. On the other hand, implicit conversion to either of float or int would have been avoided, which is usually the #1 cause for subtle problems. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From davea at ieee.org Wed Jul 8 05:35:54 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Jul 2009 05:35:54 -0400 Subject: Python Error from Apress book In-Reply-To: References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> Message-ID: <4A54687A.6010004@ieee.org> Gabriel Genellina wrote: >
En Tue, > 07 Jul 2009 09:55:13 -0300, Dave Angel escribi?: >> Gabriel Genellina wrote: >>> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >>> escribi?: > >>>> When I try to run the command as outlined in >>>> the book "simple_markup2.py < test_input.txt > test_output.html i >>>> get the >>>> following error every time. >>>> >>>> IOError: [Errno 9] Bad file descriptor >>> >>> That's a Windows problem. When you execute the script as itself >>> (either as you do in the command line, or by double-clicking on it), >>> it doesn't have valid standard handles. >>> You have to invoke Python explicitely: >>> >>> python simple_markup2.py < test_input.txt > test_output.html >>> >>> (you may need to specify the full path to python.exe, or add the >>> directory where Python is installed to your system PATH). >>> >> I use stdout this way all the time, with no problem (python 2.6, >> Windows XP). But as you point out, stdin redirection doesn't seem to >> work using the file associations. I do get a different error though. >> When I look at sys.stdin, it shows an open file, with handle of zero, >> as expected. But when I do a raw_input(), it gets: >> EOFError: EOF when reading a line > > I think the error depends on the specific OS version/service pack. But > at least on XP this appears to fix it: > > http://support.microsoft.com/kb/321788/en-us > Thanks for the link. Looking at that one, it indicates that Windows 2000 fixed it in SP4, and XP fixed it in Sp1. But I'm already running XP SP3, so I wonder if it's something new. Matt, what OS version are you running, and exactly what is happening when the error occurs? (you should be able to figure that out from the stack trace) DaveA From deets at nospam.web.de Wed Jul 8 05:44:15 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Jul 2009 11:44:15 +0200 Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Message-ID: <7bj7reF23oparU1@mid.uni-berlin.de> Phil Thompson wrote: > On Wed, 08 Jul 2009 11:11:51 +0200, "Diez B. Roggisch" > > wrote: >> Helvin wrote: >> >>> Hi experts! >>> >>> I'm developing a GUI for a software using PyQT, and need 3D >>> visualization. Should I use PyOpenGL or VTK? >>> I understand that the PyQt package comes with a opengl module. What >>> else would I need? I think I need to download opengl. but how? where? >>> I have VTK and pyVTK installed, but I don't know how to use it in my >>> code, as when I run it, an error says it can't find the vtk module. >> >> VTK won't mix with Qt. And I don't think you need to download opengl - it >> either comes with your system (or gfx-drivers), or not. I'm not sure if > Qt >> actually wraps the OpenGL api itself, AFAIK it doesn't, so you need >> PyOpenGL I guess. > > VTK has explicit support for both Qt (ie. via C++) and PyQt. Oh, I'm sorry for the FUD - they talk about Tk on their homepage. Diez From python.list at tim.thechases.com Wed Jul 8 05:53:02 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 08 Jul 2009 04:53:02 -0500 Subject: count In-Reply-To: References: Message-ID: <4A546C7E.503@tim.thechases.com> > I wanted to sort column 2 in assending order and I read whole file in array > "data" and did the following: > > data.sort(key = lambda fields:(fields[2])) > > I have sorted column 2, however I want to count the numbers in the column 2. > i.e. I want to know, for example, how many repeates of say '3' (first row, > 2nd column in above data) are there in column 2. I think you're trying to count in the wrong step -- I'd do the counting while you read the file in; not while you're trying to sort. Maybe something like this untested file-wrapper: class Histo: def __init__(self, data, column): self.data = data self.column = column self.stats = defaultdict(int) self.exhausted = False def __iter__(self): if self.exhausted: raise StopIteration("Input exhausted") for line in self.data: self.stats[line[self.column]] += 1 yield line self.exhausted = True def stats_so_far(self): return self.stats with file('in.txt') as f: r = csv.reader(f, delimiter='\t') h = Histo(r, 2) data = list(h) # slurp up the elements print repr(h.stats_so_far()) data.sort(key = lambda fields: (fields[2])) -tkc From davea at ieee.org Wed Jul 8 06:18:47 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Jul 2009 06:18:47 -0400 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A547287.8020507@ieee.org> Rajat wrote: > On Jul 8, 4:57 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: > >> In message , Christian >> >> Heimes wrote: >> >>> By the way most operating systems don't lock a file when it's opened for >>> reading or writing or even executed. >>> >> The general conclusion seems to be that mandatory locking is more trouble >> than it's worth. >> > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > application ( which could be a notepad, a wordpad or some other text > editor) that have opened my file. Once the file is closed I can easily > delete that file. > > I guess, if file is opened by one of that application, the file must > be locked and so is the reason I cannot delete the file. > > Please suggest if this is fine. > > > Neither Wordpad nor Notepad lock the files they have open, at least in my experience. Specifically, I just tried it in XP Sp3. I created a file, opened the file in one of the apps, then deleted the file before closing the app. Word for Windows, on the other hand, has locked files for me in the past. When I tried that one just now (on a text file, because that was easy), results were rather confusing. It let me delete the file, but then when I tried to "save" it, it gave me a permissions error. My experience with Winword has been that it's in a world of its own, best not messed with. And this latest version (Office 2007 or somesuch) is so modern that it has removed the Help->About menu to make sure I can't even tell what version it is. Next time I rebuild my system, I think I'll go back to Office 97. But perhaps your question is intended to cover the general case. You can get a program from sysinternals called handle, which will list all the files (and other handles) opened by all the processes in the system. You may be able to run that (with subprocess, or just from the commandline), and filter its output for the information you want. DaveA From dana_at_work at yahoo.com Wed Jul 8 06:25:33 2009 From: dana_at_work at yahoo.com (dana) Date: Wed, 8 Jul 2009 03:25:33 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: <084fac41-364f-431c-9b8f-b57c3279f91d@x3g2000yqa.googlegroups.com> On Jul 8, 12:30?am, John Machin wrote: > Deprecated certainly doesn't mean removed. > For a start, none of (DBI, ODBC, dbi, odbc) are standard Python- > supplied modules. Perhaps you are referring to the odbc (and dbi) from > the pywin32 package? Where did you get them from? If you can't > remember, try this: Thanks John. I mean the lower-case dbi and odbc modules from pywin32. Sorry for being vague. I assumed incorrectly they were part of the standard library because they came with pywin32. > If this is what you're talking about, you should be asking on the > pywin32 dicussion list (http://mail.python.org/mailman/listinfo/python- > win32). Thanks again. > General advice: if you are thinking of upgrading your Python version, > go straight to 2.6. odbc is AFAIK stuck at version 1.0 of the Python > DB API; consider switching to pyodbc (http://code.google.com/p/ > pyodbc/) Thanks thrice. We "have" to use the version of Python our software vendor supports. Presently, that's pywin32 version 2.5. Dana From piet at cs.uu.nl Wed Jul 8 06:29:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 12:29:54 +0200 Subject: IP Address Function References: Message-ID: >>>>> Fred Atkinson (FA) wrote: >FA> On Tue, 07 Jul 2009 22:54:03 -0300, "Gabriel Genellina" >FA> wrote: >>> En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson >>> escribi?: >>> >>>> Is there a Python function I can use to get the user's IP >>>> address so I can display it on his browser? >>> >>> There is a long distance between "Python" and "browser" - you'll have to >>> tell us what is in between the two. >FA> I want to have a Web page come up (written in Python, cgi) >FA> that returns the IP address of the browser (user's PC's IP address). >>> By example, do you have a server and the user connects to it? is it >>> running Python? how do you run the Python application? >>> And why do you want to do that on the server side? Isn't easier to do that >>> on the client side? What about proxies? NAT? >FA> Yes. By CGI. >>> If using CGI, look at the REMOTE_ADDR environment variable. >FA> I did look at REMOTE_ADDR but I've been unable to figure out >FA> the correct way to code it. I've tried a number of ways but I've been >FA> unsuccessful. >FA> Ideally, I'd like to store the brower's IP address in a string >FA> and then print the string on the Web page from a Python CGI script. Something like: #! /usr/bin/env python import cgi from os import getenv print "Content-type: text/html" print ipaddr = (getenv("HTTP_CLIENT_IP") or getenv("HTTP_X_FORWARDED_FOR") or getenv("HTTP_X_FORWARDED_FOR") or getenv("REMOTE_ADDR") or "UNKNOWN") print ipaddr -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nick at craig-wood.com Wed Jul 8 06:30:01 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 08 Jul 2009 05:30:01 -0500 Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> <006eb2e3$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 06 Jul 2009 15:02:26 -0700, Michael Mossey wrote: > > > On Jul 6, 2:47?pm, Philip Semanchuk wrote: > >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > >> > >> > What is required in a python program to make sure it catches a > >> > control- > >> > c on the command-line? Do some i/o? The OS here is Linux. > >> > >> You can use a try/except to catch a KeyboardInterrupt exception, or you > >> can trap it using the signal > >> module:http://docs.python.org/library/signal.html > >> > >> You want to trap SIGINT. > >> > >> HTH > >> Philip > > > > Thanks to both of you. However, my question is also about whether I need > > to be doing i/o or some similar operation for my program to notice in > > any shape or form that Control-C has been pressed. In the past, I've > > written Python programs that go about their business ignoring Ctrl-C. > > I bet that somewhere in your code you have something like: > > > for x in really_big_list: > try: > long_running_process(x) > except: > continue > > > If that's what you're doing, stop! The correct way is: > > > for x in really_big_list: > try: > long_running_process(x) > except Exception: > # Let KeyboardInterrupt and SystemExit through. > continue Note that it is a relatively recent change (in python 2.5) which made KeyboardInterrupt not a child of Exception ncw at dogger:~$ python2.4 Python 2.4.6 (#2, Feb 17 2009, 20:01:48) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. Loaded customisations from '/home/ncw/.pystartup' >>> isinstance(KeyboardInterrupt(), Exception) True >>> ncw at dogger:~$ python2.5 Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. Loaded customisations from '/home/ncw/.pystartup' >>> isinstance(KeyboardInterrupt(), Exception) False >>> > for x in really_big_list: > try: > long_running_process(x) > except (KeyboardInterrupt, SystemExit): > print "User requested exit... shutting down now" > cleanup() > raise > except Exception: > continue That is the backwards compatible way -- Nick Craig-Wood -- http://www.craig-wood.com/nick From bearophileHUGS at lycos.com Wed Jul 8 07:20:56 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Wed, 8 Jul 2009 04:20:56 -0700 (PDT) Subject: count References: Message-ID: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Vilya Harvey: > from itertools import groupby > for x, g in groupby([fields[1] for fields in data]): > ? ? print x, len(tuple(g)) Avoid that len(tuple(g)), use something like the following, it's lazy and saves some memory. def leniter(iterator): """leniter(iterator): return the length of a given iterator, consuming it, without creating a list. Never use it with infinite iterators. >>> leniter() Traceback (most recent call last): ... TypeError: leniter() takes exactly 1 argument (0 given) >>> leniter([]) 0 >>> leniter([1]) 1 >>> leniter(iter([1])) 1 >>> leniter(x for x in xrange(100) if x%2) 50 >>> from itertools import groupby >>> [(leniter(g), h) for h,g in groupby("aaaabccaadeeee")] [(4, 'a'), (1, 'b'), (2, 'c'), (2, 'a'), (1, 'd'), (4, 'e')] >>> def foo0(): ... if False: yield 1 >>> leniter(foo0()) 0 >>> def foo1(): yield 1 >>> leniter(foo1()) 1 """ # This code is faster than: sum(1 for _ in iterator) if hasattr(iterator, "__len__"): return len(iterator) nelements = 0 for _ in iterator: nelements += 1 return nelements Bye, bearophile From no.email at please.post Wed Jul 8 07:59:19 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 11:59:19 +0000 (UTC) Subject: Fractions as result from divisions (was: Re: tough-to-explain Python) References: Message-ID: In Ulrich Eckhardt writes: >Bearophile wrote: >> For example a novice wants to see 124 / 38 to return the 62/19 >> fraction and not 3 or 3.263157894736842 :-) >Python has adopted the latter of the three for operator / and the the second >one for operator //. I wonder if it was considered to just return a >fraction from that operation. http://www.python.org/dev/peps/pep-0239/ kj From mail at microcorp.co.za Wed Jul 8 08:15:08 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 8 Jul 2009 14:15:08 +0200 Subject: Remoting over SSH References: Message-ID: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> "Hussein B" wrote: > Hey, > I want to perform commands on a remote server over SSH. > What do I need? > Thanks. Access privileges for the remote machine. - Hendrik From no.email at please.post Wed Jul 8 08:23:50 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 12:23:50 +0000 (UTC) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> Simon Forman writes: >Frankly, I'm of the impression that it's a mistake not to start >teaching programming with /the bit/ and work your way up from there. >I'm not kidding. I wrote a (draft) article about this: "Computer >Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 >I really think the only good way to teach computers and programming is >to start with a bit, and build up from there. "Ontology recapitulates >phylogeny" I happen to be very receptive to this point of view. I had the benefit of that sort of training (one of the first computer courses I took started, believe it or not, with Turing machines, through coding in machine language, and compiler theory, and all the way up to dabbling with Unix!), and I suspect that the reason it is sometimes difficult for me to explain even relatively simple-looking things to others is that I have this background that I unconsciously, and incorrectly, take for granted in others... There is this persistent idea "out there" that programming is a very accessible skill, like cooking or gardening, anyone can do it, and even profit from it, monetarily or otherwise, etc., and to some extent I am actively contributing to this perception by teaching this course to non-programmers (experimental biologists to be more precise), but maybe this idea is not entirely true... Maybe, to get past the most amateurish level, one has to, one way or another, come face-to-face with bits, compilers, algorithms, and all the rest that real computer scientists learn about in their formal training... kj From p.f.moore at gmail.com Wed Jul 8 08:33:50 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Wed, 8 Jul 2009 13:33:50 +0100 Subject: DBI module deprecated at Python 2.5--what to use in its place? In-Reply-To: <084fac41-364f-431c-9b8f-b57c3279f91d@x3g2000yqa.googlegroups.com> References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> <084fac41-364f-431c-9b8f-b57c3279f91d@x3g2000yqa.googlegroups.com> Message-ID: <79990c6b0907080533v54f97099oabc77a8d146fe5e7@mail.gmail.com> 2009/7/8 dana : > On Jul 8, 12:30?am, John Machin wrote: >> Deprecated certainly doesn't mean removed. >> For a start, none of (DBI, ODBC, dbi, odbc) are standard Python- >> supplied modules. Perhaps you are referring to the odbc (and dbi) from >> the pywin32 package? Where did you get them from? If you can't >> remember, try this: > > Thanks John. I mean the lower-case dbi and odbc modules from pywin32. > Sorry for being vague. I assumed incorrectly they were part of the > standard library because they came with pywin32. pywin32 isn't part of the standard library, either - it just feels like it if you're on Windows :-) As you've already seen, the Python-Win32 list is probably of more use to you: http://mail.python.org/mailman/listinfo/python-win32 Paul. From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 08:43:55 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 12:43:55 GMT Subject: Fractions as result from divisions (was: Re: tough-to-explain Python) References: Message-ID: <00706f76$0$9711$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 11:24:28 +0200, Ulrich Eckhardt wrote: > Bearophile wrote: >> For example a novice wants to see 124 / 38 to return the 62/19 fraction >> and not 3 or 3.263157894736842 :-) > > Python has adopted the latter of the three for operator / and the the > second one for operator //. Up until a few years ago, / would return the integer division if both arguments where ints or longs, and // didn't even exist. Even in Python 2.6, you still need to do from __future__ import division to get the behaviour you describe. > I wonder if it was considered to just return > a fraction from that operation. Because of his experience with ABC, Guido dislikes using fractions for standard arithmetic. He was resistant to even allowing the rational module be added to the standard library. The problem with fractions is that, unless you're very careful, repeated arithmetic operations on numbers ends up giving you fractions like 498655847957/569892397664 instead of the 7/8 you were expecting. Not only is that ugly, but it becomes slower and slower as the denominator and numerator become larger. At least, that was Guido's experience. -- Steven From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 08:46:28 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 12:46:28 GMT Subject: Check file is locked? References: Message-ID: <0070700f$0$9711$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 00:06:11 -0700, Dennis Lee Bieber wrote: > Also, some applications may still have the file open, but Windows > allows one to make copies of that file... Not always though... some applications open files for exclusive read access. -- Steven From zephyrfalcon!NO_SPAM! at gmail.com Wed Jul 8 08:51:30 2009 From: zephyrfalcon!NO_SPAM! at gmail.com (Hans Nowak) Date: Wed, 08 Jul 2009 08:51:30 -0400 Subject: Fractions as result from divisions In-Reply-To: References: Message-ID: <4A549652.9070408@gmail.com> Ulrich Eckhardt wrote: > Bearophile wrote: >> For example a novice wants to see 124 / 38 to return the 62/19 >> fraction and not 3 or 3.263157894736842 :-) > > Python has adopted the latter of the three for operator / and the the second > one for operator //. I wonder if it was considered to just return a > fraction from that operation. http://python-history.blogspot.com/2009/03/problem-with-integer-division.html "For example, in ABC, when you divided two integers, the result was an exact rational number representing the result. In Python however, integer division truncated the result to an integer. In my experience, rational numbers didn't pan out as ABC's designers had hoped. A typical experience would be to write a simple program for some business application (say, doing one?s taxes), and find that it was running much slower than expected. After some debugging, the cause would be that internally the program was using rational numbers with thousands of digits of precision to represent values that would be truncated to two or three digits of precision upon printing. This could be easily fixed by starting an addition with an inexact zero, but this was often non-intuitive and hard to debug for beginners." -- Hans Nowak (zephyrfalcon at gmail dot com) http://4.flowsnake.org/ From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 09:10:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 13:10:23 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <007075aa$0$9711$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 12:23:50 +0000, kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> > Simon Forman writes: > >>Frankly, I'm of the impression that it's a mistake not to start teaching >>programming with /the bit/ and work your way up from there. I'm not >>kidding. I wrote a (draft) article about this: "Computer Curriculum" >>http://docs.google.com/View?id=dgwr777r_31g4572gp4 > >>I really think the only good way to teach computers and programming is >>to start with a bit, and build up from there. "Ontology recapitulates >>phylogeny" > > > I happen to be very receptive to this point of view. [...] > There is this persistent idea "out there" that > programming is a very accessible skill, like cooking or gardening, > anyone can do it, and even profit from it, monetarily or otherwise, > etc., and to some extent I am actively contributing to this perception > by teaching this course to non-programmers (experimental biologists to > be more precise), but maybe this idea is not entirely true... There is some evidence that 30-60% of people simply cannot learn to program, no matter how you teach them: http://www.codinghorror.com/blog/archives/000635.html http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ I'm sympathetic to the idea, but not entirely convinced. Perhaps the problem isn't with the students, but with the teachers, and the languages: http://www.csse.monash.edu.au/~damian/papers/PDF/SevenDeadlySins.pdf (My money is that it's a little of both.) > Maybe, to > get past the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest that > real computer scientists learn about in their formal training... The "No True Scotsman" fallacy. There's nothing amateurish about building software applications that work, with well-designed interfaces and a minimum of bugs, even if you've never heard of Turing Machines. -- Steven From lucascarvalho at gmail.com Wed Jul 8 09:11:35 2009 From: lucascarvalho at gmail.com (Lucas Carvalho) Date: Wed, 08 Jul 2009 15:11:35 +0200 Subject: Remoting over SSH In-Reply-To: References: Message-ID: <4A549B07.2090402@gmail.com> Hussein B wrote: > Hey, > I want to perform commands on a remote server over SSH. > What do I need? > Thanks. > Hi, If you want to use the SSH2 protocol into a python code, you should take a look at this module: paramiko [1]. [1] http://www.lag.net/paramiko/ Regards, Lucas. From pruebauno at latinmail.com Wed Jul 8 09:23:57 2009 From: pruebauno at latinmail.com (nn) Date: Wed, 8 Jul 2009 06:23:57 -0700 (PDT) Subject: library search path when compiling python References: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> Message-ID: <11e4f2f2-a32f-4671-a457-7cdd928d29fb@a7g2000yqk.googlegroups.com> On Jul 7, 4:06?pm, Christian Heimes wrote: > nn wrote: > > I am trying to compile python with ssl support but the libraries are > > not in /usr/lib but in /opt/freeware/lib. How do I add that folder to > > the default library search path? > > > It looks like configure --libdir=DIR might do the job but I don't want > > to replace the default lib search path, just add an additional folder > > to it. > > You didn't mention your OS. On Linux you can set the environment > variable LD_RUN_PATH prior to compiling Python. The env var sets the > rpath for the linker. See man ld(1) for details. > > Christian Sorry I didn't realize that was OS specific. I am on AIX 5.3. From no.email at please.post Wed Jul 8 09:27:34 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 13:27:34 +0000 (UTC) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> Simon Forman writes: >I'm not kidding. I wrote a (draft) article about this: "Computer >Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 Very cool. kj From aahz at pythoncraft.com Wed Jul 8 09:40:34 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 06:40:34 -0700 Subject: Newbie needs help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> Message-ID: In article , Pablo Torres N. wrote: > >Give this one a try too: http://www.mikeash.com/getting_answers.html >It doesn't talk down to you...as much :P Nice! I'll try remembering that one. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From p.f.moore at gmail.com Wed Jul 8 09:47:48 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Wed, 8 Jul 2009 14:47:48 +0100 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <79990c6b0907080647l22dce38epf03709b1119b2529@mail.gmail.com> 2009/7/8 kj : > There is this > persistent idea "out there" that programming is a very accessible > skill, like cooking or gardening, anyone can do it, and even profit > from it, monetarily or otherwise, etc., and to some extent I am > actively contributing to this perception by teaching this course > to non-programmers (experimental biologists to be more precise), > but maybe this idea is not entirely true... ?Maybe, to get past > the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest > that real computer scientists learn about in their formal training... Look at it another way. Experimental biologists don't want to program, they want to use computers to do experimental biology. It's a tool, and they (quite reasonably) don't *care* about robustness, portability, etc. Or even about programming, to be honest. In the context of the original question, it's entirely reasonable (in my view) to tell this audience "if the code does something weird you don't understand, either ignore it and find another way or dig into the manuals and experiment if you care". They'd very quickly find a = a + b as a less confusing alternative to a += b. (As has been pointed out earlier, to some extent a += b is quite an advanced construct - after all, it's essentially an optimisation of a = a + b). Biologists don't expect me to understand their discipline before I can plant seeds in my garden, after all. (And when I do plant seeds, I usually get far more surprising results than I could get from a += b :-)) Paul. From pruebauno at latinmail.com Wed Jul 8 09:55:18 2009 From: pruebauno at latinmail.com (nn) Date: Wed, 8 Jul 2009 06:55:18 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: On Jul 7, 5:18?pm, kj wrote: > In Chris Rebert writes: > > >You might find the following helpful (partially): > >http://effbot.org/zone/call-by-object.htm > > Extremely helpful. ?Thanks! ?(I learned more from it than someone > who will teach the stuff would care to admit...) > > And it confirmed Paul Graham's often-made assertion that all of > programming language design is still catching up to Lisp... > > kj One possibility is to avoid teaching mutable datatypes at the beginning (i.e. no lists,dicts and sets), then you would have something more lispish. You would have to use tuples instead of lists for everything. That avoids the gotchas of mutable types at the cost of efficiency. Python is not a purist language but rather walks a fine line between high flexible abstractions and simple fast implementation. From no.email at please.post Wed Jul 8 10:04:35 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 14:04:35 +0000 (UTC) Subject: The meaning of "=" (Was: tough-to-explain Python) Message-ID: In kj writes: >I had not realized how *profoundly* different the meaning of the >"=" in Python's > spam = ham >is from the "=" in its > spam[3] = ham[3] To clarify, this comes from my reading of Fredrik Lundh's pages "Python Objects" (http://effbot.org/zone/python-objects.htm) and "Call By Object" (http://effbot.org/zone/call-by-object.htm). (Thanks to Chris Rebert for the pointer!) In the first one of these pages, Lundh writes: [START OF LENGTHY QUOTE] Assignment statements modify namespaces, not objects. In other words, name = 10 means that you're adding the name 'name' to your local namespace, and making it refer to an integer object containing the value 10. If the name is already present, the assignment replaces the original name: name = 10 name = 20 means that you're first adding the name 'name' to the local namespace, and making it refer to an integer object containing the value 10. You're then replacing the name, making it point to an integer object containing the value 20. The original '10' object isn't affected by this operation, and it doesn't care. In contrast, if you do: name = [] name.append(1) you're first adding the name 'name' to the local namespace, making it refer to an empty list object. This modifies the namespace. You're then calling a method on that object, telling it to append an integer object to itself. This modifies the content of the list object, but it doesn't touch the namespace, and it doesn't touch the integer object. Things like name.attr and name[index] are just syntactic sugar for method calls. The first corresponds to __setattr__/__getattr__, the second to __setitem__/__getitem__ (depending on which side of the assignment they appear). [END OF LENGTHY QUOTE] Therefore, extending just a bit beyond Lundh's explanation, if we did: name = [] name.append(1) name[0] = 3 ...the second assignment would amount to a method call on the object called 'name', an operation of a very different nature (according to Lundh) from the first assignment, which is a modification of a namespace. In the second one of these pages, Lundh makes a very similar point (third post from the bottom). But note that Lundh appears to be contradicting himself somewhat when he writes "Assignment statements modify namespaces, not objects." If by "assignment statements" he means ones consisting of a left operand, a "=", and a right operand, then according to the rest of what he writes on this subject, this assertion applies only to *some* assignment statements, namely those of the form = and not to those like, for example, [] = or . = The former are syntatic sugar for certain namespace modifications that leave objects unchanged. The latter are syntactic sugar for certain object-modifying method calls that leave namespaces unchanged. At least this is how I interpret what Lundh writes. kj From pdpinheiro at gmail.com Wed Jul 8 10:06:09 2009 From: pdpinheiro at gmail.com (pdpi) Date: Wed, 8 Jul 2009 07:06:09 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> <1f09e856-bd04-4d27-bf51-5d67953a7e13@y7g2000yqa.googlegroups.com> <7xskh83r1v.fsf@ruckus.brouhaha.com> Message-ID: <9b819fa8-92f0-4b73-866f-95097d0a5b03@c1g2000yqi.googlegroups.com> On Jul 8, 2:24?am, Paul Rubin wrote: > pdpi writes: > > ? ? while abs(func(guess) - target) > epsilon: > > ? ? ? ? guess = (lo + hi) / 2. > > ? ? ? ? if sense * func(guess) > sense * target: > > ? ? ? ? ? ? hi = guess > > ? ? ? ? elif sense * func(guess) < sense * target: > > ? ? ? ? ? ? lo = guess > > ? ? ? ? elif lo == hi: > > ? ? ? ? ? ? return None > > ? ? return guess > > That is completely confusing. ?I get the, er, impression that "sense" > is supposed to change during the loop, and it takes much head > scratching to tell whether what you have there is right or not. ?Also, > it calls func 3 times on each loop, which could be extremely slow. > You don't know what func does, so eliminating 2/3 of the calls to it > is not a micro-optimization--it could be a major time saving. > > Yet another version: > > ? ? def _binary_search(x0, x1, func, target, epsilon): > ? ? ? ? y0,y1 = func(x0), func(x1) > ? ? ? ? while abs(y1 - target) > epsilon: > ? ? ? ? ? ? if x0 == x1 or cmp(y0,target) == cmp(y1,target): > ? ? ? ? ? ? ? ? return None > ? ? ? ? ? ? xn = (x0 + x1) / 2. > ? ? ? ? ? ? yn = func(xn) > ? ? ? ? ? ? if cmp(yn,target) == cmp(y0,target): > ? ? ? ? ? ? ? ?x0,y0 = xn,yn > ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ?x1,y1 = xn,yn > ? ? ? ? return x1 micro-optimization was perhaps too harsh, but it is an optimization that's not obvious for the newbie, and one best learnt from experience rather than having it spoon fed. I'll restate: you should start with the cleanest code possible and mangle it for performance. Then again, that implicitly assumes that f(x) is more readable than y, which is really a matter of taste... From futurebase at gmx.at Wed Jul 8 10:44:37 2009 From: futurebase at gmx.at (Daniel Austria) Date: Wed, 8 Jul 2009 07:44:37 -0700 (PDT) Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values Message-ID: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Hi python - hackers, just one question. How can i remove all 0 values in a list? Sure - i can loop over it, but that s not a neat style. list.remove() will only remove the first occurence. Doing that while no exception is raised is also uncool, right? Some suggestions? Best, Dan From paul at boddie.org.uk Wed Jul 8 10:53:44 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Wed, 8 Jul 2009 07:53:44 -0700 (PDT) Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> On 8 Jul, 16:04, kj wrote: > > ? = > > and not to those like, for example, > > ? [] = > > or > > ? . = > > The former are syntatic sugar for certain namespace modifications > that leave objects unchanged. ?The latter are syntactic sugar for > certain object-modifying method calls that leave namespaces unchanged. Almost. The latter can modify namespaces - the objects themselves - but through properties or dynamic attribute access, they may choose not to modify such a namespace. Really, we can phrase assignment (=) as follows: = # make refer to the result of Here, has to provide something that can be made to refer to something else, such as a name within a namespace - the first and last of your cases - or an item or slice within a sequence - the special second case which is actually handled differently from the other cases. Meanwhile, the will always provide an object to refer to, never anything of the nature of referring to something else. In other words, if you have this... x[1] = y[2] ...then the which is y[2] will yield an object which is then assigned to x[1]. The concept of y[2] is not assignable - it must be fully evaluated and produce the object at location #2 in the sequence for assignment. I suppose you could say that the left-hand side is like a sign on a signpost which always points to a real place, not another sign on a signpost. You could stretch this analogy by treating sequences as signposts holding many signs, each adjustable to point to something different. Since signposts (not the individual signs) are located in real places, they would naturally be acceptable as targets of assignments: where the signs are allowed to point to. Indeed, this would be a world of signposts with the occasional primitive value mixed in to keep weary travellers interested. ;-) Paul From mabdelkader at gmail.com Wed Jul 8 10:54:09 2009 From: mabdelkader at gmail.com (ma) Date: Wed, 8 Jul 2009 10:54:09 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> filter(lambda x: x, your_list) On Wed, Jul 8, 2009 at 10:44 AM, Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > > > Best, > Dan > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bruno.42.desthuilliers at websiteburo.invalid Wed Jul 8 10:57:16 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Wed, 08 Jul 2009 16:57:16 +0200 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <4a54b3bf$0$405$426a74cc@news.free.fr> Daniel Austria a ?crit : > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? the_list = [0, 0, 0, 1, 1, 1, 0] # Simple solution print [x for x in the_list if x != 0] # if you want to mutate the list 'in place': the_list[:] = [x for x in the_list if x != 0] print the_list HTH From djames.suhanko at gmail.com Wed Jul 8 10:59:38 2009 From: djames.suhanko at gmail.com (Djames Suhanko) Date: Wed, 8 Jul 2009 11:59:38 -0300 Subject: Remoting over SSH In-Reply-To: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> References: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> Message-ID: Hello! I wrote a litle program that send commands to many cluster nodes: #!/usr/bin/env python #By: Djames Suhanko #servers list sincroniza =["server1.domain","server2.domain", "server3.domain"] import pexpect import sys from threading import Thread #the user and pass can be in ini file. #Test parameters list try: if sys.argv[3]: pass except: print "Use: " + "script" + " " sys.exit() #This function send the command in argument def executor(command,username,password,server): a = 'ssh ' + username + '@' + server foo = pexpect.spawn(a) foo.expect('.*ssword:') foo.sendline(password) foo.sendline('su') foo.expect('.*sword:') foo.sendline('root_password_here') foo.sendline(command + '&& exit') print "command to: " + server + "..........[OK]" foo.sendline('exit') foo.expect('.*osed.') foo.interact() #make a list tasks = [] #theading loop for i in sincroniza: t = Thread(target=executor,args=(sys.argv[1],sys.argv[2],sys.argv[3],i)) t.start() tasks.append(t) #wait the end for t in tasks: t.join() it's all! On Wed, Jul 8, 2009 at 9:15 AM, Hendrik van Rooyen wrote: > "Hussein B" wrote: > >> Hey, >> I want to perform commands on a remote server over SSH. >> What do I need? >> Thanks. > > Access privileges for the remote machine. > > - Hendrik > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Djames Suhanko LinuxUser 158.760 From charles at declareSub.com Wed Jul 8 11:02:16 2009 From: charles at declareSub.com (Charles Yeomans) Date: Wed, 8 Jul 2009 11:02:16 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: On Jul 8, 2009, at 10:54 AM, ma wrote: > filter(lambda x: x, your_list) > > On Wed, Jul 8, 2009 at 10:44 AM, Daniel Austria > wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > L = [0, 0, 0, 1, 1, 1, 0] M = [x for x in L if x !=0] Charles Yeomans -------------- next part -------------- An HTML attachment was scrubbed... URL: From darcy at druid.net Wed Jul 8 11:03:15 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Wed, 8 Jul 2009 11:03:15 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: <20090708110315.634de5d2.darcy@druid.net> On Wed, 8 Jul 2009 10:54:09 -0400 ma wrote: > filter(lambda x: x, your_list) Or... [x for x in your_list if x] I'm not sure which one is more efficient but I like the syntax of the latter. A smart person could probably figure it out even without knowing Python syntax. Clarity is trump. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From fridrik at pyth.net Wed Jul 8 11:03:56 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Wed, 8 Jul 2009 15:03:56 +0000 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: ma wrote: > filter(lambda x: x, your_list) Good call! Equivalent but more efficient: filter(None, your_list) Regards, Fri?rik M?r From deets at nospam.web.de Wed Jul 8 11:06:23 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Jul 2009 17:06:23 +0200 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <7bjqndF22l519U1@mid.uni-berlin.de> Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. Why not? If you need to potentially look at *all* elements of a list, nothing but a loop will take you there. OTOH, your proposed "remove until nothing is found"-thingy will become quadratic in behavior, as remove also loops over the list - so if you have list with say 10 ones followed by 10 zeros, you will loop ten times for 11 elements, which is in the order of (n/2)**2. Diez From nachogomez at gmail.com Wed Jul 8 11:14:36 2009 From: nachogomez at gmail.com (=?UTF-8?B?UmHDumwgR8OzbWV6IEMu?=) Date: Wed, 8 Jul 2009 10:44:36 -0430 Subject: Remoting over SSH In-Reply-To: References: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> Message-ID: <684b0a740907080814t4f4911e1u9b417aaaba76e44a@mail.gmail.com> You also could use TwistedConch, which is an implementation of the SSH2 protocol for Python. I've done something with it, you can read my first questions on the TwistedConch list here . Hope that helps... -- Nacho Linux Counter #156439 -------------- next part -------------- An HTML attachment was scrubbed... URL: From no.email at please.post Wed Jul 8 11:42:09 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 15:42:09 +0000 (UTC) Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In <0778f257-d36c-4e13-93ea-bf8d448c82e2 at b15g2000yqd.googlegroups.com> Paul Boddie writes: >On 8 Jul, 16:04, kj wrote: >> >> =A0 =3D >> >> and not to those like, for example, >> >> =A0 [] =3D >> >> or >> >> =A0 . =3D >> >> The former are syntatic sugar for certain namespace modifications >> that leave objects unchanged. =A0The latter are syntactic sugar for >> certain object-modifying method calls that leave namespaces unchanged. >Almost. The latter can modify namespaces - the objects themselves - >but through properties or dynamic attribute access, they may choose >not to modify such a namespace. Really, we can phrase assignment (=3D) >as follows: > =3D # make refer to the result of > >Here, has to provide something that can be made to refer to >something else, such as a name within a namespace - the first and last >of your cases - or an item or slice within a sequence - the special >second case which is actually handled differently from the other >cases. Thanks for this correction. OK, so, scratching from my original post the case . = (as being a special case of = ), still, to the extent that I understand your post, the "=" in x = 1 means something fundamentally different (in terms of Python's underlying implementation) from the "=" in y[0] = 1 No? >You could stretch this analogy by treating sequences as >signposts holding many signs, each adjustable to point to something >different. Notionally, yes, I can see that, but there's no counterpart of this analogy at the level of Python's implementation. The "x" above is a sign, as you put it, i.e. an entry in a namespace, but "y[0]" is, in essence, a method call. kj From aahz at pythoncraft.com Wed Jul 8 11:44:32 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 08:44:32 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In article <0778f257-d36c-4e13-93ea-bf8d448c82e2 at b15g2000yqd.googlegroups.com>, Paul Boddie wrote: > >Almost. The latter can modify namespaces - the objects themselves - >but through properties or dynamic attribute access, they may choose >not to modify such a namespace. Really, we can phrase assignment (=) >as follows: > > = # make refer to the result of Right, except s/thing/target/ http://docs.python.org/reference/simple_stmts.html#assignment-statements -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From matt0177 at gmail.com Wed Jul 8 11:54:36 2009 From: matt0177 at gmail.com (Matthew Edmondson) Date: Wed, 8 Jul 2009 08:54:36 -0700 Subject: Python Error from Apress book In-Reply-To: <4A54687A.6010004@ieee.org> References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> <4A54687A.6010004@ieee.org> Message-ID: <8e2cbd660907080854p65bad365p54ccdad4771786a6@mail.gmail.com> I'm running XP SP3. The program now works great from either of the directories, as long as include 'python' before it. As far as looking at the error with stack trace, I really don't know enough yet to know how to do that. I'm running the file from command line, because I'm not sure how to run it referencing other files from idle. Lot to learn..... Once again, thanks a ton for all of the help. Matt On Wed, Jul 8, 2009 at 2:35 AM, Dave Angel wrote: > > > Gabriel Genellina wrote: > >>
En Tue, 07 >> Jul 2009 09:55:13 -0300, Dave Angel escribi?: >> >>> Gabriel Genellina wrote: >>> >>>> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >>>> escribi?: >>>> >>> >> When I try to run the command as outlined in >>>>> the book "simple_markup2.py < test_input.txt > test_output.html i get >>>>> the >>>>> following error every time. >>>>> >>>>> IOError: [Errno 9] Bad file descriptor >>>>> >>>> >>>> That's a Windows problem. When you execute the script as itself (either >>>> as you do in the command line, or by double-clicking on it), it doesn't have >>>> valid standard handles. >>>> You have to invoke Python explicitely: >>>> >>>> python simple_markup2.py < test_input.txt > test_output.html >>>> >>>> (you may need to specify the full path to python.exe, or add the >>>> directory where Python is installed to your system PATH). >>>> >>>> I use stdout this way all the time, with no problem (python 2.6, >>> Windows XP). But as you point out, stdin redirection doesn't seem to work >>> using the file associations. I do get a different error though. When I look >>> at sys.stdin, it shows an open file, with handle of zero, as expected. But >>> when I do a raw_input(), it gets: >>> EOFError: EOF when reading a line >>> >> >> I think the error depends on the specific OS version/service pack. But at >> least on XP this appears to fix it: >> >> http://support.microsoft.com/kb/321788/en-us >> >> Thanks for the link. Looking at that one, it indicates that Windows 2000 > fixed it in SP4, and XP fixed it in Sp1. But I'm already running XP SP3, so > I wonder if it's something new. > > Matt, what OS version are you running, and exactly what is happening when > the error occurs? (you should be able to figure that out from the stack > trace) > > > DaveA > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Jul 8 11:59:14 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Jul 2009 11:59:14 -0400 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <4A54C252.2090304@ieee.org> kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> Simon Forman writes: > > >> Frankly, I'm of the impression that it's a mistake not to start >> teaching programming with /the bit/ and work your way up from there. >> I'm not kidding. I wrote a (draft) article about this: "Computer >> Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 >> > > >> I really think the only good way to teach computers and programming is >> to start with a bit, and build up from there. "Ontology recapitulates >> phylogeny" >> > > > I happen to be very receptive to this point of view. I had the > benefit of that sort of training (one of the first computer courses > I took started, believe it or not, with Turing machines, through > coding in machine language, and compiler theory, and all the way > up to dabbling with Unix!), and I suspect that the reason it is > sometimes difficult for me to explain even relatively simple-looking > things to others is that I have this background that I unconsciously, > and incorrectly, take for granted in others... There is this > persistent idea "out there" that programming is a very accessible > skill, like cooking or gardening, anyone can do it, and even profit > from it, monetarily or otherwise, etc., and to some extent I am > actively contributing to this perception by teaching this course > to non-programmers (experimental biologists to be more precise), > but maybe this idea is not entirely true... Maybe, to get past > the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest > that real computer scientists learn about in their formal training... > > kj > > > And I learned to program Analog Computers, along with APL, in an early course. In my case assembly language came later, but by then we had covered the electronics behind transistors, and Karnough maps, logic diagrams, and so on. By the time I graduated, I had five six-level languages and two assembly languages under my belt. .DaveA From aahz at pythoncraft.com Wed Jul 8 11:59:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 08:59:19 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In article , kj wrote: > >OK, so, scratching from my original post the case > >. = > >(as being a special case of = ), still, >to the extent that I understand your post, the "=" in > > x = 1 > >means something fundamentally different (in terms of Python's >underlying implementation) from the "=" in > > y[0] = 1 > >No? No. ;-) What's different is not the ``=`` but the construction of the assignment target before ``=`` gets executed. Consider also this: x, y = y, x -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From xahlee at gmail.com Wed Jul 8 12:19:23 2009 From: xahlee at gmail.com (Xah Lee) Date: Wed, 8 Jul 2009 09:19:23 -0700 (PDT) Subject: OT: unix to Windows technology Message-ID: Dear unixers & lispers, i've been using Mac for the past 19 years, and been a professional sys admin or web app developers on the unix platform, since 1998 (maily Solaris, Apache, Perl, Java, SQL, PHP). In june, i bought a PC (not for the first time though), and made a switch to Windows, for the first time, in the sense as a developer instead of just a casual PC user i've been. In the past month, i've spend about 5 hours a day digging into MS Windows tech, in particluar, read over 200 Wikipedia articles in detail related to Windows technology. (192 of them linked) Here's a write up of the whole story, my experiences, including some tech introduction to MS Windows from a sys admin or programer point of view. ? Switching from Mac/Unix To PC/Windows http://xahlee.org/mswin/switch_to_windows.html Some slightly noteworthy subsections are: ? Removing HP/Compaq Software http://xahlee.org/mswin/hp_bundled_apps.html ? Installing Cygwin Tutorial http://xahlee.org/mswin/installing_cygwin.html ? Mac and Windows File Conversion http://xahlee.org/mswin/mac_windows_file_conv.html ? Unix And Windows File Permission Systems http://xahlee.org/mswin/file_perm_systems.html ? Introduction to Windows Scripting http://xahlee.org/mswin/windows_scripting.html Some articles (not shown above) are still work in progress, such as VBScript tutorial and PowerShell tutorial. Hoping to complete in the coming months or years. comment & feedback welcome, esp if you are a Windows expert and answer some of my unanswered questions on the page. Xah ? http://xahlee.org/ ? From james at agentultra.com Wed Jul 8 12:31:40 2009 From: james at agentultra.com (J Kenneth King) Date: Wed, 08 Jul 2009 12:31:40 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: <858wizdtlf.fsf@agentultra.com> Fri?rik M?r J?nsson writes: > ma wrote: >> filter(lambda x: x, your_list) > > Good call! Equivalent but more efficient: > > filter(None, your_list) > > Regards, > Fri?rik M?r I was wondering when someone would mention filter() From robert.kern at gmail.com Wed Jul 8 12:38:33 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 08 Jul 2009 11:38:33 -0500 Subject: Does cProfile include IO wait time? In-Reply-To: References: Message-ID: On 2009-07-04 19:03, Matthew Wilson wrote: > I have a command-line script that loads about 100 yaml files. It takes > 2 or 3 seconds. I profiled my code and I'm using pstats to find what is > the bottleneck. > > Here's the top 10 functions, sorted by internal time: > > In [5]: _3.sort_stats('time').print_stats(10) > Sat Jul 4 13:25:40 2009 pitz_prof > > 756872 function calls (739759 primitive calls) in 8.621 CPU seconds > > Ordered by: internal time > List reduced from 1700 to 10 due to restriction<10> > > ncalls tottime percall cumtime percall filename:lineno(function) > 15153 0.446 0.000 0.503 0.000 build/bdist.linux-i686/egg/yaml/reader.py:134(forward) > 30530 0.424 0.000 0.842 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:142(need_more_tokens) > 98037 0.423 0.000 0.423 0.000 build/bdist.linux-i686/egg/yaml/reader.py:122(peek) > 1955 0.415 0.000 1.265 0.001 build/bdist.linux-i686/egg/yaml/scanner.py:1275(scan_plain) > 69935 0.381 0.000 0.381 0.000 {isinstance} > 18901 0.329 0.000 3.908 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:113(check_token) > 5414 0.277 0.000 0.794 0.000 /home/matt/projects/pitz/pitz/__init__.py:34(f) > 30935 0.258 0.000 0.364 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:276(stale_possible_simple_keys) > 18945 0.192 0.000 0.314 0.000 /usr/local/lib/python2.6/uuid.py:180(__cmp__) > 2368 0.172 0.000 1.345 0.001 build/bdist.linux-i686/egg/yaml/parser.py:268(parse_node) > > I expected to see a bunch of my IO file-reading code in there, but I don't. So > this makes me think that the profiler uses CPU time, not > clock-on-the-wall time. It should be basically wall-clock time on Linux. The timer function underneath is gettimeofday(2). Look in Modules/_lsprof.c of the Python sources for the function hpTimer(). -- 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 no.email at please.post Wed Jul 8 12:38:48 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 16:38:48 +0000 (UTC) Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In aahz at pythoncraft.com (Aahz) writes: >In article , kj wrote: >> >>OK, so, scratching from my original post the case >> >>. = >> >>(as being a special case of = ), still, >>to the extent that I understand your post, the "=" in >> >> x = 1 >> >>means something fundamentally different (in terms of Python's >>underlying implementation) from the "=" in >> >> y[0] = 1 >> >>No? >No. ;-) No??? Just when I thought I finally understood all this! >What's different is not the ``=`` but the construction of the >assignment target before ``=`` gets executed. Hmm. OK, I went to the link you posted in your other message (http://docs.python.org/reference/simple_stmts.html#assignment-statements) and I find this (my emphasis): Assignment of an object to a single target is recursively defined as follows. * If the target is an identifier (name): o If the name does not occur in a global statement in the current code block: the name is bound to the object ^^^^^^^^^^^^^^^^^ in the current local namespace. o Otherwise: the name is bound to the object in the ^^^^^^^^^^^^^^^^^ current global namespace. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. * If the target is a target list enclosed in parentheses or in square brackets... (I'LL IGNORE THIS FOR NOW) * If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case, TypeError is raised. That object is then asked to assign the assigned ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object to the given attribute; if it cannot perform the ^^^^^^ assignment, it raises an exception (usually but not necessarily AttributeError). * If the target is a subscription: The primary expression in the reference is evaluated. It should yield either a mutable sequence object (such as a list) or a mapping object (such as a dictionary). Next, the subscript expression is evaluated. If the primary is a mutable sequence object (such as a list),... [CONDITIONS ON THE INDEX EXPRESSION OMITTED]... the sequence is asked to assign the assigned object to its ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ item with that index.... If the primary is a mapping object (such as a dictionary),... [CONDITIONS ON THE SUBSCRIPT EXPRESSION OMITTED]... the ^^^ mapping is then asked to create a key/datum pair which maps ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the subscript to the assigned object. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * If the target is a slicing: [INDEX STUFF OMITTED]... the ^^^ sequence object is asked to replace the slice with the items ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ of the assigned sequence... ^^^^^^^^^^^^^^^^^^^^^^^^ OK, I originally interpreted what Lundh wrote in his two articles that the "binding" described at the very beginning (i.e. when the target is an identifier), which I take to make an entry or modify such an entry in a namespace, is a *categorically different* operation from the remaining operations underlined above. I interpreted Paul Boddie's correction in his response to me as saying that the "assignment" mentioned for the case when the target is an attribute reference is actually a special case of the "assignment" to simple identifiers (i.e. it also means "binding"). But that still leaves all the other "assignments" (or the like) underlined above. I don't think that the full definitions of these remaining cases are covered by the same rule, even though the rule is described as "recursive." I think that the writer has something else in mind, and in particular, something *other* than binding, but the author remains vague on exactly what this means. Clearly, both Lundh and the documentation draw some distinction between "binding" and some other forms of "assignment" (which remain ill-defined throughout). This distinction is what I was referring to when I said that "=" means different things in different contexts. kj From fridrik at pyth.net Wed Jul 8 12:44:47 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Wed, 8 Jul 2009 16:44:47 +0000 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <858wizdtlf.fsf@agentultra.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> <858wizdtlf.fsf@agentultra.com> Message-ID: <7D21C328-8320-4DF8-9E8B-08093441D270@pyth.net> J Kenneth King wrote: > I was wondering when someone would mention filter() I was happy to see that too. It's clean, faster than list comprehension and in terms of clarity it's only to be expected that the developer is familiar with, or at least willing to look up, the available built-in methods. Regards, Fri?rik M?r From kentilton at gmail.com Wed Jul 8 12:51:57 2009 From: kentilton at gmail.com (Kenneth Tilton) Date: Wed, 08 Jul 2009 12:51:57 -0400 Subject: OT: unix to Windows technology In-Reply-To: References: Message-ID: <4a54ceb0$0$5898$607ed4bc@cv.net> Xah Lee wrote: > Dear unixers & lispers, > > i've been using Mac for the past 19 years, and been a professional sys > admin or web app developers on the unix platform, since 1998 (maily > Solaris, Apache, Perl, Java, SQL, PHP). In june, i bought a PC (not > for the first time though), and made a switch to Windows, for the > first time, in the sense as a developer instead of just a casual PC > user i've been. > > In the past month, i've spend about 5 hours a day digging into MS > Windows tech, in particluar, read over 200 Wikipedia articles in > detail related to Windows technology. (192 of them linked) > > Here's a write up of the whole story, my experiences, including some > tech introduction to MS Windows from a sys admin or programer point of > view. > > ? Switching from Mac/Unix To PC/Windows > http://xahlee.org/mswin/switch_to_windows.html > > Some slightly noteworthy subsections are: > > ? Removing HP/Compaq Software > http://xahlee.org/mswin/hp_bundled_apps.html > > ? Installing Cygwin Tutorial > http://xahlee.org/mswin/installing_cygwin.html > > ? Mac and Windows File Conversion > http://xahlee.org/mswin/mac_windows_file_conv.html > > ? Unix And Windows File Permission Systems > http://xahlee.org/mswin/file_perm_systems.html > > ? Introduction to Windows Scripting > http://xahlee.org/mswin/windows_scripting.html > > Some articles (not shown above) are still work in progress, such as > VBScript tutorial and PowerShell tutorial. Hoping to complete in the > coming months or years. > > comment & feedback welcome, esp if you are a Windows expert and answer > some of my unanswered questions on the page. > > Xah > ? http://xahlee.org/ > > ? You just discovered PCs are cheaper? The funny thing is that that is Microsoft's answer to the Apple Mac-PC ads, they show people shopping for computers and just comparing hardware and price as if this is some kind of breakthrough. But I understand: they have no answer to Windows being such a nightmare and the Mac being such a joy. kt From tjreedy at udel.edu Wed Jul 8 12:57:31 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 08 Jul 2009 12:57:31 -0400 Subject: The meaning of "=" (Was: tough-to-explain Python) In-Reply-To: References: Message-ID: kj wrote: > To clarify, this comes from my reading of Fredrik Lundh's pages > "Python Objects" (http://effbot.org/zone/python-objects.htm) and > "Call By Object" (http://effbot.org/zone/call-by-object.htm). [snip] > [END OF LENGTHY QUOTE] > > Therefore, extending just a bit beyond Lundh's explanation, if we > did: > > name = [] > name.append(1) > name[0] = 3 > > ...the second assignment would amount to a method call on the object > called 'name', an operation of a very different nature (according > to Lundh) from the first assignment, which is a modification of a > namespace. I disagree. Assignment creates an association. Modification of a namespace, when implemented, amounts to a method call on the concrete object, whether a Python object or not, that implements the abstraction of a namespace. At module scope, name = ob is the same as globals()['name']=ob Within a class statement, substitute '' for 'globals' Within functions, CPython uses an internal array, so name = ob becomes [name-number] = ob Or, to put it another way, Python dicts and lists are, considered abstractly, associations also, just like namespaces. Dicts are more general than namespaces, sequences are 'number-spaces' instead of name-spaces. Terry Jan Reedy From nobody at nowhere.com Wed Jul 8 13:03:35 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 08 Jul 2009 18:03:35 +0100 Subject: Python and webcam capture delay? References: <6xB4m.21104$vi5.8567@uutiset.elisa.fi> Message-ID: On Tue, 07 Jul 2009 22:49:22 -0700, Dennis Lee Bieber wrote: > On Tue, 07 Jul 2009 23:37:43 +0100, Nobody > declaimed the following in gmane.comp.python.general: > >> AFAIK, the only real difference between "USB-1 conformant" and "USB-2 >> conformant" is that the latter actually passed a test of its ability to >> say "no, I can't do high-speed", while the former didn't. The check for >> high-speed capability was designed such that it shouldn't cause problems >> for USB-1 devices, but individual USB-1 devices weren't actually tested >> for this. > > USB-1 or USB-1.1? As I recall the latter is capable of 11Mbps > whereas the original USB spec was much lower... (of course, USB 2 at top > is 480Mbps) USB 1.0 supports 1.5Mbps (low-speed) and 12MBps (full-speed). Hosts and hubs must support both, functions (devices) can use either. USB 1.1 was a bug-fix release which solved some interoperability issues arising from ambiguities in the USB 1.0 standard. From lucas_junqueira at yahoo.com Wed Jul 8 13:07:33 2009 From: lucas_junqueira at yahoo.com (Lucas Junqueira) Date: Wed, 8 Jul 2009 10:07:33 -0700 (PDT) Subject: windows command-line Message-ID: <376017.28322.qm@web30601.mail.mud.yahoo.com> Hi, I'd like to run a simple windows command-line program from within my python script and agt all the returt it generates. Is this possible? How can I do it? Thank you! ____________________________________________________________________________________ Veja quais s?o os assuntos do momento no Yahoo! +Buscados http://br.maisbuscados.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From cameron.pulsford at gmail.com Wed Jul 8 13:09:43 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Wed, 8 Jul 2009 13:09:43 -0400 Subject: Problem with list of dicts and copying Message-ID: <5700df6b0907081009p5ec4167cqec36e915e0f6c4@mail.gmail.com> Hello all, I'm redoing a sudoku solver of mine and I ran into an issue with lists of dicts. Bear with me for a second before I get to the actual problem... I'm representing the board as a dictionary, where the keys are (x, y) positions, and the values are candidates. So my program goes along picking numbers from the list of candidates and then propagating the constraints. So it might place a 6, which would then remove 6 as a candidate from that row column and grid. However, that might leave a spot with no candidates, which means we placed a legal but not correct number. This is where my problem arises. I keep a running list of the boards which is updated every time I find a legal number that doesn't invalidate the boards. I add onto this list by doing "boards.append(self.board.copy())". When I place a legal but invalid number I do "self.board = boards[-1].copy()" to return the board to the last known good state. And when I completely exhaust the candidates for a spot and must backtrack to a different spot (instead of simply trying a different candidate of the current spot) I do "self.board = boards[-1].copy() and then del boards[-1]" Basically my boards list should be copies of good boards, and when I need to refresh the current board, I want to pull off a copy of the correct one. Essentially (after watching the debugging output) it looks like this isn't happening. I am always modifying the same board. The algo follows, and I can post the rest of the code if necessary. So am I using the dict.copy() wrong? Am I guessing I have a tricky problem with the "=" in "self.board = boards[-1].copy()" def solve(self): previousValue = dict((blank, 0) for blank in self.blanks()) self.fillInCandidates() continuing, boards, guesses = False, [self.board.copy()], 0 while len(self.blanks()) > 0: if continuing == False: cp = self.mrv() continuing = False nl = self.nextLegal(cp, previousValue[cp]) previousValue[cp] = nl if nl != 0: self.board[cp] = nl guesses += 1 self.update(nl, cp[0], cp[1]) if self.isNotLegal(): continuing = True self.board = boards[-1].copy() else: boards.append(self.board.copy()) else: previousValue[cp] = 0 self.board = boards[-1].copy() del boards[-1] -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.com Wed Jul 8 13:19:16 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 08 Jul 2009 18:19:16 +0100 Subject: Python and webcam capture delay? References: <0ca855t29jklb0o0gi31ourfk3lstdiepf@4ax.com> Message-ID: On Tue, 07 Jul 2009 22:11:12 -0700, Tim Roberts wrote: >>The webcam is bound to do some encoding; most of them use USB "full speed" >>(12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. > > That's not true. Most of the web cams made in the last 5 years or so run > at high speed, 480 Mbps. Full speed only gets you 1 fps at 640x480 > uncompressed, so it's really only useful for the most primitive video > conference cams. The very earliest models typically only did 320x200, while later USB-1 webcams used onboard compression to get a decent framerate. For internet use, 12Mbps isn't that much of an obstacle; the internet connection's upload speed is more likely to be the limiting factor. Faster speeds are more useful for things like LAN-based CCTV systems. From emile at fenx.com Wed Jul 8 13:23:54 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 08 Jul 2009 10:23:54 -0700 Subject: windows command-line In-Reply-To: <376017.28322.qm@web30601.mail.mud.yahoo.com> References: <376017.28322.qm@web30601.mail.mud.yahoo.com> Message-ID: On 7/8/2009 10:07 AM Lucas Junqueira said... > Hi, I'd like to run a simple windows command-line program from within my > python script and agt all the returt it generates. Is this possible? How > can I do it? Depending on python version, look into subprocess, commands or os.pipe and related. Emile From nobody at nowhere.com Wed Jul 8 13:25:49 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 08 Jul 2009 18:25:49 +0100 Subject: Check file is locked? References: Message-ID: On Tue, 07 Jul 2009 21:31:12 -0700, Rajat wrote: >> > By the way most operating systems don't lock a file when it's opened for >> > reading or writing or even executed. >> >> The general conclusion seems to be that mandatory locking is more trouble >> than it's worth. > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > application ( which could be a notepad, a wordpad or some other text > editor) that have opened my file. Once the file is closed I can easily > delete that file. > > I guess, if file is opened by one of that application, the file must > be locked and so is the reason I cannot delete the file. Windows doesn't allow you to delete (or rename) open files. It's up to the application as to whether it keeps the file open or closes it once it has been read. > Please suggest if this is fine. If you can't delete the file, then you can't delete it. You shouldn't be trying to kill off applications so that you can delete the file. Just do what other Windows programs (including those which are part of Windows) do: display a dialog telling the user to close the application then try again. From xahlee at gmail.com Wed Jul 8 13:34:50 2009 From: xahlee at gmail.com (Xah Lee) Date: Wed, 8 Jul 2009 10:34:50 -0700 (PDT) Subject: OT: unix to Windows technology References: <4a54ceb0$0$5898$607ed4bc@cv.net> Message-ID: <16430ee7-888e-42ef-9c84-14df4b78ee04@p36g2000prn.googlegroups.com> Xah Lee wrote: > ? Switching from Mac/Unix To PC/Windows > http://xahlee.org/mswin/switch_to_windows.html Kenneth Tilton wrote: > You just discovered PCs are cheaper? > > The funny thing is that that is Microsoft's answer to the Apple Mac-PC > ads, they show people shopping for computers and just comparing hardware > and price as if this is some kind of breakthrough. But I understand: > they have no answer to Windows being such a nightmare and the Mac being > such a joy. well, i've been owning Macs for over the past 19 years. From a dedicated fan thru-out the 1990s, to still fan in early of 2000s with debut of OS X. Mac prices in comparison to PC has gone up and downs. In the early 1990s for example, it is maybe 4 times more. Lowest is probably in the mid 1990s, where 3rd party companies are licensed to producing clones, and that happens to also be a period that Mac OS is the worst, crashing few times per day, similar to Win 95 and 98. I think in the early 2000s the price gap came closer, and since maybe 2005 it start to increase again. for someone like me, i've read all the pros and cons of Mac vs PC. I've read, for instance, basically all MacWorld and MacUser mags in the early 1990s until they become defunct. (and often MacWeek too) Not to mention the huge amount of websites, especially in the late 1990s where there are a number of high profile dedicated mac fan sites. I also have often attended the yearly Mac World Expo, eagerly antipating Steve Job's ?O, one more thing...? etc... and etc and etc. As a hyperbole, i've prob read more Mac vs PC argument from newsgroup users combined. LOL as to the price comparison... a better metric is value/price ratio. I think, over the past 20 years, the value/price ratio of Mac compared to PC, namely: (MacValue/MacPrice)/(PC_value/PC_price) is going down, starting with Windows NT 4, and going downhill quickly with Windows XP, and Vista, .NET. as far as SOFTWARE technology goes, Apple's falling lamer and lamer from its 1990s mountain top of lisps and hypercards and desktop publishing stuff. Microsoft Windows, starting from MS-DOS moronicity, today, with its .NET, F#, PowerShell, is quite beyond Apple's perpetual diddling with its prettification of OS X. Xah ? http://xahlee.org/ ? From paul.lafollette at gmail.com Wed Jul 8 13:35:44 2009 From: paul.lafollette at gmail.com (Paul LaFollette) Date: Wed, 8 Jul 2009 13:35:44 -0400 Subject: function local namespace question Message-ID: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Kind people, Using Python 3.1 under FreeBSD and WinXP. I've been tearing my hair out trying to solve this myself, but I need to ask for help. I want (for obscure reasons) to be able to log transactions in the namespace(s) of a script. Specifically I would like to log creation of identifiers, changes in the binding of identifiers ("assignment") and lookups. This turns out to be pretty easy in the global and local namespaces... I simply subclass dict, override the appropriate operations to include the logging operations I want, and then exec the code using my dictionaries as the global and local namespaces. All of this works just dandy until I try to extend it to functions. I cannot figure out any way to get a hook into the local namespace of a user defined function. I have tried making a wrapper class that grabs the function call and then uses exec to invoke myfunction.__code__ with my own dictionaries. This runs the (no argument) function properly (losing the return value, but I can deal with that) but never accesses the local logging dictionary that I specify in the exec() call. Perhaps the local namespace of a function is not a dict at all? Anyway, is there any way (however clumsy) to do what I want to do? Thank you for your help. Paul --------------------------- Paul LaFollette CIS Department Temple University paul.lafollette(at)temple.edu www.cis.temple.edu/~lafollet From Lacrima.Maxim at gmail.com Wed Jul 8 13:52:44 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Wed, 8 Jul 2009 10:52:44 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. Message-ID: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> Hello! I have just started using Emacs to write python scripts. I installed python-mode.el Then I just tried this code: print 'hello world' When I press C-c RET, new blank window is opened and emacs says: (Shell command succeeded with no output) So where is my 'hello world'? When I do C-c C-c, it prints 'hello world' successfully. Why in the first case I get no output? Thanks in advance! With regards, Max (sorry if my English isn't very proper) From sajmikins at gmail.com Wed Jul 8 14:01:28 2009 From: sajmikins at gmail.com (Simon Forman) Date: Wed, 8 Jul 2009 11:01:28 -0700 (PDT) Subject: ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: On Jul 8, 10:44?am, Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. ?list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > > Best, > Dan If you are doing something like this: L = [0, 0, 0, 1, 1, 1, 0] removeZeros(L) number_of_ones = len(L) you can just use sum() like so: number_of_ones = sum(L) HTH From no.email at please.post Wed Jul 8 14:10:08 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 18:10:08 +0000 (UTC) Subject: ISO library ref in printed form References: Message-ID: In Piet van Oostrum writes: >>>>>> kj (kj) wrote: >>kj> Does anyone know where I can buy the Python library reference in >>kj> printed form? (I'd rather not print the whole 1200+-page tome >>kj> myself.) I'm interested in both/either 2.6 and 3.0. >Maybe you can have a copy printed at lulu.com. Interesting idea... Doesn't look like they offer such service. >It would even be nicer if >the PSF would offer them at lulu. That would be great. kj From http Wed Jul 8 14:43:19 2009 From: http (Paul Rubin) Date: 08 Jul 2009 11:43:19 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Message-ID: <7xiqi3dni0.fsf@ruckus.brouhaha.com> Bearophile writes: > > ? ? print x, len(tuple(g)) > > Avoid that len(tuple(g)), use something like the following print x, sum(1 for _ in g) From http Wed Jul 8 14:49:52 2009 From: http (Paul Rubin) Date: 08 Jul 2009 11:49:52 -0700 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <7xd48bdn73.fsf@ruckus.brouhaha.com> Daniel Austria writes: > just one question. How can i remove all 0 values in a list? I prefer: newlist = list(x for x in oldlist if x != 0) to the square bracket list comprehension that a few people have suggested. This is because in python 2.x, the listcomp "leaks" its index variable into the surrounding scope, but the generator expression above doesn't. Usually not a big deal, but an extra bit of hygiene never(?) hurts. From fetchinson at googlemail.com Wed Jul 8 14:56:39 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Wed, 8 Jul 2009 11:56:39 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531560$0$9355$426a74cc@news.free.fr> Message-ID: >> But this academic discussion is honestly a little pointless. The OP >> was referring to a expectation, coming from C, that is not fulfilled >> in python. What's wrong with mentioning it somewhere for the sake of >> helping C programmers? >> > And where does one stop? After all, my primary work language at the > time I first encountered Python was FORTRAN77; and my home system at the > time was an Amiga with ARexx... (granted, I did have a C compiler on it > -- which I do not have on this WinXP machine)... And programming, even > in C, on the Amiga still inflicted BCPL concepts upon one ("AmigaDOS", > the "user" view, was a port of Tripos user view on top of the Amiga > executive libraries). > > Do we mention how Python differs from F77, F90, Ada, Rexx, LISP, > RPG, APL, Pascal, BASIC, and COBOL (I've done all except RPG since > graduating high school). > > In my mind... Anyone whose only experience with programming language > concepts is C/C++ deserves the shock of finding that other languages ARE > DIFFERENT! Even if they never use other languages they should at least > have been exposed to language design options and rationales... > > Okay, I don't know what current curricula consist of, but in the > late 70s, a CS degree at my college required two sessions of FORTRAN, > two of COBOL, Assembly (numerically, it followed advanced FORTRAN), > database (followed COBOL)... and then diverged into Business Programming > vs Systems Programming (Business required lots of accounting/statistics > courses [stat-II was SPSS], Systems was operating system and language > design). Electives included BASIC, Pascal, APL (The professor for the > data structures course made the mistake of once allowing an assignment > -- hashed head multiple-linked list -- to be done in any language he > could read ; I did it in a BASIC that only supported 4 open files at > a time... I think someone did it in SNOBOL) > > C wasn't available on the XEROX Sigma-7; I did have a first edition > K&R. By graduation I'd also been exposed to the initial Ada > reference/rational (a working compiler -- Ada/Ed -- didn't come out > until after I'd graduated). Okay, so where does one stop? I'd say C deserves special treatment as opposed to all the other languages you mentioned because Guido himself admits to influences from C (and ABC but I hope you won't assume that ABC is a widely used language). Maybe you didn't read these messages from me in this thread: By the way, assignments in conditionals. Guido explicitly referred to C when he forbade assignment in conditionals, citing common typos/errors in C code such as if( x = 5 ){ .... } instead of if( x == 5 ){ ..... }. So even he realized that warning people about different usage in python and C is a good thing. Expectations from C work sometimes, and sometimes they don't. In latter case a little warning is useful. And also, Seriously, ask Guido about the influence of C vs. fortran (or cobol, ada, pascal, etc). Somewhere you can find him quoted as saying that python was originally intended to "bridge the gap between the shell and C". I've never heard him talk about fortran (or cobol, ada, pascal, etc). So don't get me wrong, I'm sufficiently impressed by your knowledge of various computer languages, I admit to only knowing C and basic and some fortran (and of course python :)), but if Guido himself thinks the influence of C on python is more important than the others, then let's not doubt him. And yes, I shamelessly admit to arguing based on a higher authority and not based on merit, but in this case it's appropriate, I think :) Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From backup95 at netcabo.pt Wed Jul 8 15:32:36 2009 From: backup95 at netcabo.pt (=?ISO-8859-1?Q?Jo=E3o_Valverde?=) Date: Wed, 08 Jul 2009 20:32:36 +0100 Subject: ISO library ref in printed form In-Reply-To: References: Message-ID: <4A54F454.9060007@netcabo.pt> kj wrote: > Does anyone know where I can buy the Python library reference in > printed form? (I'd rather not print the whole 1200+-page tome > myself.) I'm interested in both/either 2.6 and 3.0. > > TIA! > > kj > Why not download the documentation, take it to a local copy shop and have it printed and bound there? http://docs.python.org/download.html http://docs.python.org/3.1/download.html From aahz at pythoncraft.com Wed Jul 8 15:33:57 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 12:33:57 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Message-ID: In article <050094ea-faf4-4e03-875d-9c2c63090a89 at y17g2000yqn.googlegroups.com>, Bearophile wrote: >Vilya Harvey: >> >> from itertools import groupby >> for x, g in groupby([fields[1] for fields in data]): >> =A0 =A0 print x, len(tuple(g)) > >Avoid that len(tuple(g)), use something like the following, it's lazy >and saves some memory. The question is whether it saves time, have you tested it? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From piet at cs.uu.nl Wed Jul 8 15:54:23 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 21:54:23 +0200 Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> Message-ID: >>>>> Lacrima (L) wrote: >L> Hello! >L> I have just started using Emacs to write python scripts. >L> I installed python-mode.el >L> Then I just tried this code: >L> print 'hello world' >L> When I press C-c RET, new blank window is opened and emacs says: >L> (Shell command succeeded with no output) >L> So where is my 'hello world'? >L> When I do C-c C-c, it prints 'hello world' successfully. >L> Why in the first case I get no output? Can you check in the buffer where you have the python code what command C-c RET is bound to? With C-h k C-c RET Shell command succeeded with no output suggests that is has a different binding than the standard one in python-mode. Did you happen to name your file 'test' or 'test.py? C-c RET does an import and 'import test' imports a standard module test. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 8 15:56:01 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 21:56:01 +0200 Subject: ISO library ref in printed form References: Message-ID: >>>>> kj (k) wrote: >k> In Piet van Oostrum writes: >>>>>>>> kj (kj) wrote: >kj> Does anyone know where I can buy the Python library reference in >kj> printed form? (I'd rather not print the whole 1200+-page tome >kj> myself.) I'm interested in both/either 2.6 and 3.0. >>> Maybe you can have a copy printed at lulu.com. >k> Interesting idea... Doesn't look like they offer such service. You can upload your own PDF file and have them print a single copy. You would have to split the file yourself in manageble pieces (< 600 something pages) >>> It would even be nicer if >>> the PSF would offer them at lulu. >k> That would be great. >k> kj -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From milesck at umich.edu Wed Jul 8 16:50:44 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 8 Jul 2009 16:50:44 -0400 Subject: function local namespace question In-Reply-To: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> References: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Message-ID: On Jul 8, 2009, at 1:35 PM, Paul LaFollette wrote: > I cannot figure out any way to get a hook into the local namespace of > a user defined function. I have tried making a wrapper class that > grabs the function call and then uses exec to invoke > myfunction.__code__ with my own dictionaries. This runs the (no > argument) function properly (losing the return value, but I can deal > with that) but never accesses the local logging dictionary that I > specify in the exec() call. Perhaps the local namespace of a function > is not a dict at all? Right. Within functions, local variable name accesses and assignments are compiled into bytecode instructions (LOAD_FAST, STORE_FAST) that manipulate pointers to objects by indexing directly into a C array in the frame. The "locals" dictionary of a frame is generated on-demand from that array when accessed. There is no way that I'm aware of to directly hook into function namespace access and manipulation. -Miles From http Wed Jul 8 17:45:53 2009 From: http (Paul Rubin) Date: 08 Jul 2009 14:45:53 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Message-ID: <7xbpnuzw4u.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > >Avoid that len(tuple(g)), use something like the following, it's lazy > >and saves some memory. > The question is whether it saves time, have you tested it? len(tuple(xrange(100000000))) ... hmm. From david.bramer at googlemail.com Wed Jul 8 18:06:22 2009 From: david.bramer at googlemail.com (David) Date: Wed, 8 Jul 2009 15:06:22 -0700 (PDT) Subject: regex help Message-ID: Hi I have a few regexs I need to do, but im struggling to come up with a nice way of doing them, and more than anything am here to learn some tricks and some neat code rather than getting an answer - although thats obviously what i would like to get to. Problem 1 - (25.47%)
I want to extract 25.47 from here - so far I've tried - xPer = re.search('(.*?)%', content) and xPer = re.search('\((\d*)%\)
', content) neither of these seem to do what I want - am I not doing this correctly? (obviously!) Problem 2 -   Open: 5.50   Mkt Cap: 6.92M   P/E: 21.99 I want to extract the open, mkt cap and P/E values - but apart from doing loads of indivdual REs which I think would look messy, I can't think of a better and neater looking way. Any ideas? Cheers David From aahz at pythoncraft.com Wed Jul 8 18:22:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 15:22:19 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: In article <7xbpnuzw4u.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: >aahz at pythoncraft.com (Aahz) writes: >>Paul Rubin deleted an attribution: >>> >>>Avoid that len(tuple(g)), use something like the following, it's lazy >>>and saves some memory. >> >> The question is whether it saves time, have you tested it? > >len(tuple(xrange(100000000))) ... hmm. When dealing with small N, O() can get easily swamped by the constant factors. How often do you deal with more than a hundred fields? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From digitig at gmail.com Wed Jul 8 18:25:21 2009 From: digitig at gmail.com (Tim Rowe) Date: Wed, 8 Jul 2009 23:25:21 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: <006e7ce0$0$9711$c3e8da3@news.astraweb.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> Message-ID: 2009/7/7 Steven D'Aprano : > Maybe the reason for "so much buggy software" is that people > inappropriately use assert, thus changing the behaviour of code depending > on whether it is run with the -O flag or not. I've done my share of code review and process audits, and assertions seem *far* to rare for that distinction. > I don't know what "hostility" you're seeing. The only hostility I'm > seeing is from the OP, which is bizarre considering that he asked for > advice and we gave it. What I see is a bunch of people concerned that the > OP is teaching novices a bad habit, namely, using assert for error > checking. He claims -- angrily and over and over again -- that in his > code, the assertions should never fail. Great. Good for him for knowing > when to use assert. But are the novices going to learn that lesson, or > will they simply learn "use assert for error checking"? They are rather more likely to learn if they are taught, aren't they? Or do you think it's better to keep them in the dark? "There are these things called assertions -- work them out for yourselves". I am convinced that the time to teach programmers to consider under what circumstances a routine can be called and who is responsible for ensuring that those conditions are met is as soon as they hit the concept of calling routines. And assertions provide an excellent way of doing that, fostering good habits for the rest of their careers. Any hostility from the OP seems to be a response to the persistent refusal to accept his assurances that he is not using the assertions for run-time error checking, nor teaching the students to do that, -- Tim Rowe From http Wed Jul 8 18:28:17 2009 From: http (Paul Rubin) Date: 08 Jul 2009 15:28:17 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: <7xvdm2n726.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > When dealing with small N, O() can get easily swamped by the constant > factors. How often do you deal with more than a hundred fields? The number of fields in the OP's post was not stated. Expecting it to be less than 100 seems like an ill-advised presumption. If N is unknown, speed-tuning the case where N is small at the expense of consuming monstrous amounts of memory when N is large sounds somewhere between a premature optimization and a nasty bug. From clp2 at rebertia.com Wed Jul 8 18:37:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 8 Jul 2009 15:37:48 -0700 Subject: regex help In-Reply-To: References: Message-ID: <50697b2c0907081537v44c45510kf8e9f143e2213a0@mail.gmail.com> On Wed, Jul 8, 2009 at 3:06 PM, David wrote: > Hi > > I have a few regexs I need to do, but im struggling to come up with a > nice way of doing them, and more than anything am here to learn some > tricks and some neat code rather than getting an answer - although > thats obviously what i would like to get to. > > Problem 1 - > > ? ? ? ? ? ? ? ?id="ref_678774_cp">(25.47%)
> > I want to extract 25.47 from here - so far I've tried - > > xPer = re.search(' \">(.*?)%', content) > > and > > xPer = re.search(' \">\((\d*)%\)
', content) > > neither of these seem to do what I want - am I not doing this > correctly? (obviously!) > > Problem 2 - > >   > > Open: > > 5.50 > >   > Mkt Cap: > > 6.92M > >   > P/E: > > 21.99 > > > > I want to extract the open, mkt cap and P/E values - but apart from > doing loads of indivdual REs which I think would look messy, I can't > think of a better and neater looking way. Any ideas? Use an actual HTML parser? Like BeautifulSoup (http://www.crummy.com/software/BeautifulSoup/), for instance. I will never understand why so many people try to parse/scrape HTML/XML with regexes... Cheers, Chris -- http://blog.rebertia.com From usernet at ilthio.net Wed Jul 8 18:43:41 2009 From: usernet at ilthio.net (Tim Harig) Date: Wed, 08 Jul 2009 22:43:41 GMT Subject: regex help References: Message-ID: On 2009-07-08, Chris Rebert wrote: > On Wed, Jul 8, 2009 at 3:06 PM, David wrote: >> I want to extract the open, mkt cap and P/E values - but apart from >> doing loads of indivdual REs which I think would look messy, I can't >> think of a better and neater looking way. Any ideas? You are downloading market data? Yahoo offers its stats in CSV format that is easier to parse without a dedicated parser. > Use an actual HTML parser? Like BeautifulSoup > (http://www.crummy.com/software/BeautifulSoup/), for instance. I agree with your sentiment exactly. If the regex he is trying to get is difficult enough that he has to ask; then, yes, he should be using a parser. > I will never understand why so many people try to parse/scrape > HTML/XML with regexes... Why? Because some times it is good enough to get the job done easily. From thebrasse at gmail.com Wed Jul 8 18:43:59 2009 From: thebrasse at gmail.com (=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=) Date: Wed, 8 Jul 2009 15:43:59 -0700 (PDT) Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: <48bcf21c-4c40-4d49-9966-e0cedfdf206e@d4g2000yqa.googlegroups.com> On Jul 6, 11:15?pm, Scott David Daniels wrote: > brasse wrote: > > I have been thinking about how write exception safe constructors in > > Python. By exception safe I mean a constructor that does not leak > > resources when an exception is raised within it. > > ... > ?> As you can see this is less than straight forward. Is there some kind > ?> of best practice that I'm not aware of? > > Not so tough. ?Something like this tweaked version of your example: > > class Foo(object): > ? ? ?def __init__(self, name, fail=False): > ? ? ? ? ?self.name = name > ? ? ? ? ?if not fail: > ? ? ? ? ? ? ?print '%s.__init__(%s)' % (type(self).__name__, name) > ? ? ? ? ?else: > ? ? ? ? ? ? ?print '%s.__init__(%s), FAIL' % (type(self).__name__, name) > ? ? ? ? ? ? ?raise ValueError('Asked to fail: %r' % fail) > > ? ? ?def close(self): > ? ? ? ? ?print '%s.close(%s)' % (type(self).__name__, self.name) > > class Bar(object): > ? ? ?def __init__(self): > ? ? ? ? ?unwind = [] > ? ? ? ? ?try: > ? ? ? ? ? ? ?self.a = Foo('a') > ? ? ? ? ? ? ?unwind.append(a) > ? ? ? ? ? ? ?self.b = Foo('b', fail=True) > ? ? ? ? ? ? ?unwind.append(b) > ? ? ? ? ? ? ?... > ? ? ? ? ?except Exception, why: > ? ? ? ? ? ? ?while unwind): > ? ? ? ? ? ? ? ? ?unwind.pop().close() > ? ? ? ? ? ? ?raise > > bar = Bar() > OK. That's another way. I have been thinking about this some more and right now I am experimenting with a decorator that could help me do these kinds of things. This is what I have right now: import functools class Foo(object): def __init__(self, name, fail=False): self.name = name self.closed = False if not fail: print '%s.__init__(%s)' % (self.__class__.__name__, self.name) else: print '%s.__init__(%s), FAIL' % (self.__class__.__name__, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) def safe(f): @functools.wraps(f) def wrapper(self, *args, **kwargs): variables = [] def recorder(self, name, value): if name != '__class__': variables.append(name) object.__setattr__(self, name, value) class Customized(object): pass setattr(Customized, '__setattr__', recorder) old_class = self.__class__ self.__class__ = Customized try: f(self, *args, **kwargs) self.__class__ = old_class except: # clean up objects created in __init__ (i.e. the call to f ()) for name in reversed(variables): o = getattr(self, name) if hasattr(o, 'close'): o.close() raise return wrapper class Bar(object): @safe def __init__(self): self.a = Foo('a') self.b = Foo('b') self.c = Foo('c', fail=True) bar = Bar() The safe decorator will record all attributes created on self. If an exception is raised during the execution of the decorated constructor it will call close on all the recorded objects. I realize that this decorator still needs a lot of work before it is usable, but I think it illustrates the concept. It might even be possible for the decorator to create a close/cleanup method dynamically. To have a decorator like this that actually worked would be great since it would remove the need to write error prone cleanup code. Any thoughts? :.:: mattias > --Scott David Daniels > Scott.Dani... at Acm.Org From fabiofz at gmail.com Wed Jul 8 19:01:40 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Wed, 8 Jul 2009 20:01:40 -0300 Subject: Pydev 1.4.7 Released Message-ID: Hi All, Pydev and Pydev Extensions 1.4.7 have been released Details on Pydev Extensions: http://www.fabioz.com/pydev Details on Pydev: http://pydev.sf.net Details on its development: http://pydev.blogspot.com Release Highlights in Pydev Extensions: ----------------------------------------------------------------- * The interactive console can be used on the remote debugger * Remote debugger properly redirects contents from the server on multiple runs. * Providing context-independent completions when other completions are not available (until now, it only gave those completions for method parameters) * The number of chars required to show the context-insensitive completion can now be customized. * Fixed problem with Eclipse 3.5: "Invalid Thread Access" when trying to rename a class Release Highlights in Pydev: ---------------------------------------------- * Iron Python support * Fixed issue when configuring interpreter on Eclipse 3.3 and 3.2 (was using API only available in 3.4) * Google App Engine o Popup menus for google app engine are now working with eclipse 3.2 o Fixed issues when google app engine project has spaces in path * Launching o Ctrl+F9 can be used to run as unit-test and select which tests will be run o F9 will now run the current editor based on the project type o Changed run icons o Run configurations can be created for the project o Run as unit-test can have --filter and --tests as a parameter set in the run configuration * Shift left can now shift even when there are less chars than the required indent string * Top-level modules on .egg files are now properly recognized * Auto-config fixed * Fixed problem when .pydevproject was not a parseable xml file (the pydev package explorer wouldn't work because of that) * When a new interpreter is created, it's properly selected in the tree * Code-completion better heuristic when analyzing function return that's called on self. * Code-completion in the interactive console now handles import sections correctly * Code formatter: Spaces after square and curly braces are no longer changed when an unary operator is found afterwards * Fixed problem when recognizing encodings (regexp was not correct) What is PyDev? --------------------------- PyDev is a plugin that enables users to use Eclipse for Python, Jython and Iron Python 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 Aptana http://aptana.com/python Pydev Extensions http://www.fabioz.com/pydev Pydev - Python Development Environment for Eclipse http://pydev.sf.net http://pydev.blogspot.com From helvinlui at gmail.com Wed Jul 8 19:10:37 2009 From: helvinlui at gmail.com (Helvin) Date: Wed, 8 Jul 2009 16:10:37 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Message-ID: <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> On Jul 8, 9:23?pm, Phil Thompson wrote: > On Wed, 08 Jul 2009 11:11:51 +0200, "Diez B. Roggisch" > > wrote: > > > > > > > Helvin wrote: > > >> Hi experts! > > >> I'm developing a GUI for a software using PyQT, and need 3D > >> visualization. Should I use PyOpenGL or VTK? > >> I understand that the PyQt package comes with a opengl module. What > >> else would I need? I think I need to download opengl. but how? where? > >> I have VTK and pyVTK installed, but I don't know how to use it in my > >> code, as when I run it, an error says it can't find the vtk module. > > > VTK won't mix with Qt. And I don't think you need to download opengl - it > > either comes with your system (or gfx-drivers), or not. I'm not sure if > Qt > > actually wraps the OpenGL api itself, AFAIK it doesn't, so you need > > PyOpenGL I guess. > > VTK has explicit support for both Qt (ie. via C++) and PyQt. > > Phil Thanks for the fast replies! I will look into how to use VTK now. Where would I find VTK's explicit support for PyQt? Because I have installed VTK (using its installer) and pyVTK (using its setup.py file), but how do I actually use it in my code? According to: http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521455.html, I have tried 'import vtk', but python can't find the vtk module. Thanks a million! Helvin From psimon at sonic.net Wed Jul 8 19:18:02 2009 From: psimon at sonic.net (Paul Simon) Date: Wed, 8 Jul 2009 16:18:02 -0700 Subject: tkinter problem Message-ID: <4a55292f$0$95538$742ec2ed@news.sonic.net> I have the "tkinter" problem and need some assistance to straighten it out. >From the web page "http://wiki.python.org/moin/TkInter" I tested as in "step 1" and cannot import "_tkinter." I do not have that file on my computer, but do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. This python stuff is great, but the documentation frequently feels like it is just a bit out of my grasp. I realize that all of this is free but I understand the instructions on the web page to repair only to the point of confusion. I'm not an expert. How do I modify my python configuration? Is there a file that needs to be edited? Which setup.py file do I use? Make? or python setup.py build and python setup.py install? Thanks. I appreciate your help. Paul Simon From rhodri at wildebst.demon.co.uk Wed Jul 8 19:20:59 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 09 Jul 2009 00:20:59 +0100 Subject: regex help In-Reply-To: References: Message-ID: On Wed, 08 Jul 2009 23:06:22 +0100, David wrote: > Hi > > I have a few regexs I need to do, but im struggling to come up with a > nice way of doing them, and more than anything am here to learn some > tricks and some neat code rather than getting an answer - although > thats obviously what i would like to get to. > > Problem 1 - > > id="ref_678774_cp">(25.47%)
> > I want to extract 25.47 from here - so far I've tried - > > xPer = re.search(' \">(.*?)%', content) Supposing that str(xID.group(1)) == "678774", let's see how that string concatenation turns out: (.*?)% The obvious problems here are the spurious double quotes, the spurious (but harmless) escaping of a double quote, and the lack of (escaped) backslash and (escaped) open parenthesis. The latter you can always strip off later, but the first sink the match rather thoroughly. > > and > > xPer = re.search(' \">\((\d*)%\)
', content) With only two single quotes present, the biggest problem should be obvious. Unfortunately if you just fix the obvious in either of the two regular expressions, you're setting yourself up for a fall later on. As The Fine Manual says right at the top of the page on the re module (http://docs.python.org/library/re.html), you want to be using raw string literals when you're dealing with regular expressions, because you want the backslashes getting through without being interpreted specially by Python's own parser. As it happens you get away with it in this case, since neither '\d' nor '\(' have a special meaning to Python, so aren't changed, and '\"' is interpreted as '"', which happens to be the right thing anyway. > Problem 2 - > >   > > Open: > > 5.50 > >   > Mkt Cap: > > 6.92M > >   > P/E: > > 21.99 > > > > I want to extract the open, mkt cap and P/E values - but apart from > doing loads of indivdual REs which I think would look messy, I can't > think of a better and neater looking way. Any ideas? What you're trying to do is inherently messy. You might want to use something like BeautifulSoup to hide the mess, but never having had cause to use it myself I couldn't say for sure. -- Rhodri James *-* Wildebeest Herder to the Masses From clp2 at rebertia.com Wed Jul 8 19:22:14 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 8 Jul 2009 16:22:14 -0700 Subject: tkinter problem In-Reply-To: <4a55292f$0$95538$742ec2ed@news.sonic.net> References: <4a55292f$0$95538$742ec2ed@news.sonic.net> Message-ID: <50697b2c0907081622w11ed9275mcce5c0377cafd273@mail.gmail.com> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: > I have the "tkinter" problem and need some assistance to straighten it out. > >From the web page "http://wiki.python.org/moin/TkInter" I tested as in "step > 1" and cannot import "_tkinter." I do not have that file on my computer, but > do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the > directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. > This ?python stuff is great, but the documentation frequently > feels like it is just a bit out of my grasp. I realize that all of this is > free but I understand the instructions on the web page to repair only to the > point of confusion. I'm not an expert. How do I modify my python > configuration? Is there a file that needs to be edited? Which setup.py file > do I use? Make? or python setup.py build and python setup.py install? > Thanks. I appreciate your help. - How did you install Python? - What Linux distro are you using? Cheers, Chris -- http://blog.rebertia.com From robert.kern at gmail.com Wed Jul 8 19:29:26 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 08 Jul 2009 18:29:26 -0500 Subject: PyQt GUI In-Reply-To: <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> Message-ID: On 2009-07-08 18:10, Helvin wrote: > Thanks for the fast replies! I will look into how to use VTK now. > Where would I find VTK's explicit support for PyQt? Wrapping/Python/vtk/qt4/ in the VTK sources. > Because I have installed VTK (using its installer) and pyVTK (using > its setup.py file), but how do I actually use it in my code? According > to: http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521455.html, > I have tried 'import vtk', but python can't find the vtk module. Then you have not installed VTK's Python bindings correctly. Note that pyVTK is just a library for manipulating VTK files. The VTK Python bindings are part of VTK's distribution itself. Exactly how did you install VTK? Did you compile it yourself? -- 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 ldo at geek-central.gen.new_zealand Wed Jul 8 19:49:54 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 09 Jul 2009 11:49:54 +1200 Subject: walking a directory with very many files References: <5f51bc62-16fb-4e24-ac6c-1e5ea3ee596c@d38g2000prn.googlegroups.com> <234b19ac-7baf-4356-9fe5-37d00146d982@z9g2000yqi.googlegroups.com> <20090617091858.432f89ca@malediction> <20090617110705.7e7c423f@malediction> <%Zv_l.19493$y61.5958@news-server.bigpond.net.au> <0058d36a$0$9759$c3e8da3@news.astraweb.com> Message-ID: In message , Lie Ryan wrote: > Lawrence D'Oliveiro wrote: > >> ... certainly it is characteristic of GUIs to show you all 400,000 files >> in a directory, or at least try to do so, and either hang for half an >> hour or run out of memory and crash, rather than give you some >> intelligent way of prefiltering the file display up front. > > In many debugging cases, you don't even know what to filter ... So pick a random sample to start with. Do you know of a GUI tool that can do that? From mensanator at aol.com Wed Jul 8 20:03:42 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 8 Jul 2009 17:03:42 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: Message-ID: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> On Jul 7, 12:47?am, Mensanator wrote: > On Jul 7, 12:16 am, casevh wrote: > > > I discovered a serious bug with comparisons and have posted alpha2 > > which fixes that bug and adds Unicode support for Python 2.x > > > casevh > > Damn! I was just congatulating myself for pulling off > a hat trick (there had been no point in downloading > 3.x without gmpy so I have been putting it off): > > - installing Python 3.1 > - installing gmpy 1.10 > - converting my Collatz Function library to 3.1 syntax > > And it all worked smoothly, just had to add parentheses > to my print statements, change xrange to range and all > my / to // (the library is exclusively integer). I had > gmpy running in my library on 3.1 in about 10 minutes. > > So I'll have to re-do the gmpy install. Shouldn't be > any big deal. > > I started doing some real world tests. Generally, things > look good (nothing crashes, timing looks not bad) but > I'm getting some funny results on one of my tests, so > I'll report back when I have more information. As I said, I was getting funny results from one of my tests. It seemed to work ok, but timing varied from 2 to 88 seconds, which seemed odd. The other tests were generally consistent for a given environment (cpu speed, OS, Python version, gmpy version). At some point I watched the memory usage profile from Windows. On the same machine I have both Python 2.6 and 3.1 installed, with the appropriate gmpy 1.10 version loaded for each. In Python 2.6, it looks like this: memory usage profile Python 2.6 gmpy 1.1 Vista /--------------\ /-------\ .....RESTART SHELL / .\/ \ ____/ . \________ . . . . start of RUN start of RUN The first "start of RUN" is the first time the test is run (from IDLE). That change in usage represents about 700 MB (I'm testing really BIG numbers, up to 500 million digits). The memory remains allocated after the program terminates (the flat plateau). When I run a second time, we see the allocation dip, then climb back up to the plateau, so it appears that the allocation never climbs above 1.1 GB. Finally, doing a RESTART SHELL seems to completely free the allocated memory. I assume this is normal behaviour. With Python 3.1, it get this profile: memory usage profile Python 3.1 gmpy 1.1 Vista /- / | /----------------/ \------\ .....RESTART SHELL / . \ ____/ . \___________ . . . . start of RUN start of RUN Here, at the start of the second RUN, it appears that new memory is allocated BEFORE the previous is cleared. Is this a quirk in the way 3.1 behaves? Here, the peak usage climbs to 1.8 GB which I think causes VM thrashing accounting for the increased execution times. My guess is that gmpy is provoking, but not causing this behaviour. The actual test is: t0 = time.time() n=10 for k in range(1,n): for i in range(1,n-2): print((str(cf.gmpy.numdigits(cf.Type12MH(k,i))).zfill(n)),end=' ') print() print() t1 = time.time() The library function Type12MH is: def Type12MH(k,i): """Find ith, kth Generation Type [1,2] Mersenne Hailstone using the closed form equation Type12MH(k,i) k: generation i: member of generation returns Hailstone (a) """ ONE = gmpy.mpz(1) TWO = gmpy.mpz(2) SIX = gmpy.mpz(6) NIN = gmpy.mpz(9) if (k<1) or (i<1): return 0 i = gmpy.mpz(i) k = gmpy.mpz(k) # a = (i-1)*9**(k-1) + (9**(k-1) - 1)//2 + 1 # return 2**(6*a - 1) - 1 a = (i-ONE)*NIN**(k-ONE) + (NIN**(k-ONE) - ONE)//TWO + ONE return TWO**(SIX*a - ONE) - ONE ## Sample runs ## ## Test 1 - create numbers up to 500 million digits ## ## 0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ## 0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ## 0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ## 0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ## 0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ## 0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ## 0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ## 0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ## 0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ## 15.5460000038 ## >>> ================================ RESTART ================================ ## >>> ## 0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ## 0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ## 0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ## 0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ## 0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ## 0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ## 0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ## 0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ## 0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ## 3.06299996376 From psimon at sonic.net Wed Jul 8 20:16:15 2009 From: psimon at sonic.net (Paul Simon) Date: Wed, 8 Jul 2009 17:16:15 -0700 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> Message-ID: <4a5536d4$0$95520$742ec2ed@news.sonic.net> "Chris Rebert" wrote in message news:mailman.2863.1247095339.8015.python-list at python.org... On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: > I have the "tkinter" problem and need some assistance to straighten it > out. > >From the web page "http://wiki.python.org/moin/TkInter" I tested as in > >"step > 1" and cannot import "_tkinter." I do not have that file on my computer, > but > do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the > directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. > This python stuff is great, but the documentation frequently > feels like it is just a bit out of my grasp. I realize that all of this is > free but I understand the instructions on the web page to repair only to > the > point of confusion. I'm not an expert. How do I modify my python > configuration? Is there a file that needs to be edited? Which setup.py > file > do I use? Make? or python setup.py build and python setup.py install? > Thanks. I appreciate your help. - How did you install Python? - What Linux distro are you using? Cheers, Chris -- http://blog.rebertia.com I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure exactly how I installed Python. Originally I had installed 2.5 from RPM but 2.6 was not available for my distro (2008.1) in RPM. I downloaded something from python.org and installed. Not sure if it was tarball or zip file. Paul From casevh at gmail.com Wed Jul 8 20:38:06 2009 From: casevh at gmail.com (casevh) Date: Wed, 8 Jul 2009 17:38:06 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> Message-ID: <4ae1e16f-124b-49c8-8364-36c59419be9b@d32g2000yqh.googlegroups.com> On Jul 8, 5:03?pm, Mensanator wrote: > On Jul 7, 12:47?am, Mensanator wrote: > > > > > On Jul 7, 12:16 am, casevh wrote: > > > > I discovered a serious bug with comparisons and have posted alpha2 > > > which fixes that bug and adds Unicode support for Python 2.x > > > > casevh > > > Damn! I was just congatulating myself for pulling off > > a hat trick (there had been no point in downloading > > 3.x without gmpy so I have been putting it off): > > > - installing Python 3.1 > > - installing gmpy 1.10 > > - converting my Collatz Function library to 3.1 syntax > > > And it all worked smoothly, just had to add parentheses > > to my print statements, change xrange to range and all > > my / to // (the library is exclusively integer). I had > > gmpy running in my library on 3.1 in about 10 minutes. > > > So I'll have to re-do the gmpy install. Shouldn't be > > any big deal. > > > I started doing some real world tests. Generally, things > > look good (nothing crashes, timing looks not bad) but > > I'm getting some funny results on one of my tests, so > > I'll report back when I have more information. > > As I said, I was getting funny results from one of my tests. > > It seemed to work ok, but timing varied from 2 to 88 seconds, > which seemed odd. The other tests were generally consistent > for a given environment (cpu speed, OS, Python version, gmpy > version). > > At some point I watched the memory usage profile from Windows. > On the same machine I have both Python 2.6 and 3.1 installed, > with the appropriate gmpy 1.10 version loaded for each. > > In Python 2.6, it looks like this: > > memory usage profile Python 2.6 gmpy 1.1 Vista > > ? ? ? /--------------\ ?/-------\ .....RESTART SHELL > ? ? ?/ ? ? ? ? ? ? ? .\/ ? ? ? ? \ > ____/ ? ? ? ? ? ? ? ?. ? ? ? ? ? ?\________ > ? ?. ? ? ? ? ? ? ? ? . > ? ?. ? ? ? ? ? ? ? ? . > ? ?start of RUN ? ? ?start of RUN > > The first "start of RUN" is the first time the test is run > (from IDLE). That change in usage represents about 700 MB > (I'm testing really BIG numbers, up to 500 million digits). > > The memory remains allocated after the program terminates > (the flat plateau). When I run a second time, we see the > allocation dip, then climb back up to the plateau, so it > appears that the allocation never climbs above 1.1 GB. > > Finally, doing a RESTART SHELL seems to completely free > the allocated memory. I assume this is normal behaviour. > > With Python 3.1, it get this profile: > > memory usage profile Python 3.1 gmpy 1.1 Vista > > ? ? ? ? ? ? ? ? ? ? ? ? ?/- > ? ? ? ? ? ? ? ? ? ? ? ? / | > ? ? ? /----------------/ ? \------\ .....RESTART SHELL > ? ? ?/ ? ? ? ? ? ? ? ? . ? ? ? ? ? \ > ____/ ? ? ? ? ? ? ? ? ?. ? ? ? ? ? ?\___________ > ? ?. ? ? ? ? ? ? ? ? ? . > ? ?. ? ? ? ? ? ? ? ? ? . > ? ?start of RUN ? ? ? ?start of RUN > > Here, at the start of the second RUN, it appears that new > memory is allocated BEFORE the previous is cleared. Is this > a quirk in the way 3.1 behaves? Here, the peak usage climbs > to 1.8 GB which I think causes VM thrashing accounting for > the increased execution times. > Hmmm. It looks like memory is not being release properly. I don't see that behavior under Linux. The running time is a very consistent 1.35 seconds. I'm traveling at the moment so it will be at least a week before I can test under Windows. Thanks for the report. I'll try to see if I can figure out what is going on. casevh > My guess is that gmpy is provoking, but not causing this > behaviour. > > The actual test is: > > t0 = time.time() > n=10 > for k in range(1,n): > ? for i in range(1,n-2): > ? ? print((str(cf.gmpy.numdigits(cf.Type12MH(k,i))).zfill(n)),end=' ') > ? print() > print() > t1 = time.time() > > The library function Type12MH is: > > def Type12MH(k,i): > ? ? """Find ith, kth Generation Type [1,2] Mersenne Hailstone using > the closed form equation > > ? ? Type12MH(k,i) > ? ? k: generation > ? ? i: member of generation > ? ? returns Hailstone (a) > ? ? """ > ? ? ONE = gmpy.mpz(1) > ? ? TWO = gmpy.mpz(2) > ? ? SIX = gmpy.mpz(6) > ? ? NIN = gmpy.mpz(9) > > ? ? if (k<1) or (i<1): return 0 > > ? ? i = gmpy.mpz(i) > ? ? k = gmpy.mpz(k) > > ? ? # a = (i-1)*9**(k-1) + (9**(k-1) - 1)//2 + 1 > ? ? # return 2**(6*a - 1) - 1 > > ? ? a = (i-ONE)*NIN**(k-ONE) + (NIN**(k-ONE) - ONE)//TWO + ONE > ? ? return TWO**(SIX*a - ONE) - ONE > > ## ?Sample runs > ## > ## ?Test 1 - create numbers up to 500 million digits > ## > ## ?0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 > 0000000013 > ## ?0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 > 0000000107 > ## ?0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 > 0000000952 > ## ?0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 > 0000008560 > ## ?0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 > 0000077028 > ## ?0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 > 0000693246 > ## ?0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 > 0006239209 > ## ?0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 > 0056152869 > ## ?0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 > 0505375816 > ## > ## ?15.5460000038 > ## ?>>> ================================ RESTART > ================================ > ## ?>>> > ## ?0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 > 0000000013 > ## ?0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 > 0000000107 > ## ?0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 > 0000000952 > ## ?0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 > 0000008560 > ## ?0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 > 0000077028 > ## ?0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 > 0000693246 > ## ?0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 > 0006239209 > ## ?0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 > 0056152869 > ## ?0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 > 0505375816 > ## > ## ?3.06299996376 From no.email at please.post Wed Jul 8 21:20:10 2009 From: no.email at please.post (kj) Date: Thu, 9 Jul 2009 01:20:10 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Martin Vilcans writes: >On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: >> I'm will be teaching a programming class to novices, and I've run >> into a clear conflict between two of the principles I'd like to >> teach: code clarity vs. code reuse. =A0I'd love your opinion about >> it. >In general, code clarity is more important than reusability. >Unfortunately, many novice programmers have the opposite impression. I >have seen too much convoluted code written by beginners who try to >make the code generic. Writing simple, clear, to-the-point code is >hard enough as it is, even when not aiming at making it reusable. >If in the future you see an opportunity to reuse the code, then and >only then is the time to make it generic. >YAGNI is a wonderful principle. Thanks! kynn From no.email at please.post Wed Jul 8 21:21:22 2009 From: no.email at please.post (kj) Date: Thu, 9 Jul 2009 01:21:22 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Scott David Daniels writes: >First, a quote which took me a bit to find: > Thomas William K??rner paraphrasing Polya and Svego > in A Companion to Analysis: > Recalling that 'once is a trick, twice is a method, > thrice is a theorem, and four times a theory,' we > seek to codify this insight. Good stuff. >Let us apply this insight: > Suppose in writing code, we pretty much go with that. >A method is something you notice, a theorem is a function, and >a theory is a generalized function. >Even though we like DRY ("don't repeat yourself") as a maxim, let >it go the first time and wait until you see the pattern (a possible >function). I'd go with a function first, a pair of functions, and >only then look to abstracting the function. Thanks! kynn From jcd at sdf.lonestar.org Wed Jul 8 22:10:47 2009 From: jcd at sdf.lonestar.org (J. Clifford Dyer) Date: Wed, 08 Jul 2009 22:10:47 -0400 Subject: count In-Reply-To: <7xbpnuzw4u.fsf@ruckus.brouhaha.com> References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: <1247105447.4532.8.camel@mctell> On Wed, 2009-07-08 at 14:45 -0700, Paul Rubin wrote: > aahz at pythoncraft.com (Aahz) writes: > > >Avoid that len(tuple(g)), use something like the following, it's lazy > > >and saves some memory. > > The question is whether it saves time, have you tested it? > > len(tuple(xrange(100000000))) ... hmm. timer.py -------- from datetime import datetime def tupler(n): return len(tuple(xrange(n))) def summer(n): return sum(1 for x in xrange(n)) def test_func(f, n): print f.__name__, start = datetime.now() print f(n) end = datetime.now() print "Start: %s" % start print "End: %s" % end print "Duration: %s" % (end - start,) if __name__ == '__main__': test_func(summer, 10000000) test_func(tupler, 10000000) test_func(summer, 100000000) test_func(tupler, 100000000) $ python timer.py summer 10000000 Start: 2009-07-08 22:02:13.216689 End: 2009-07-08 22:02:15.855931 Duration: 0:00:02.639242 tupler 10000000 Start: 2009-07-08 22:02:15.856122 End: 2009-07-08 22:02:16.743153 Duration: 0:00:00.887031 summer 100000000 Start: 2009-07-08 22:02:16.743863 End: 2009-07-08 22:02:49.372756 Duration: 0:00:32.628893 Killed $ Note that "Killed" did not come from anything I did. The tupler just bombed out when the tuple got too big for it to handle. Tupler was faster for as large an input as it could handle, as well as for small inputs (test not shown). From no.email at please.post Wed Jul 8 22:19:25 2009 From: no.email at please.post (kj) Date: Thu, 9 Jul 2009 02:19:25 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: In Tim Rowe writes: >2009/7/4 kj : >> Precisely. =A0As I've stated elsewhere, this is an internal helper >> function, to be called only a few times under very well-specified >> conditions. =A0The assert statements checks that these conditions >> are as intended. =A0I.e. they are checks against the module writer's >> programming errors. >Good for you. I'm convinced that you have used the assertion >appropriately, and the fact that so many here are unable to see that >looks to me like a good case for teaching the right use of assertions. >For what it's worth, I read assertions at the beginning of a procedure >as part of the specification of the procedure, and I use them there in >order to document the procedure. An assertion in that position is for >me a statement to the user of the procedure "it's your responsibility >to make sure that you never call this procedure in such a way as to >violate these conditions". They're part of a contract, as somebody >(maybe you) pointed out. >As somebody who works in the safety-critical domain, it's refreshing >to see somebody teaching students to think about the circumstances in >which a procedure can legitimately be called. The hostility you've >received to that idea is saddening, and indicative of why there's so >much buggy software out there. Thanks for the encouragement. When I teach programming, the students are scientists. For the stuff they do, correctness of the code trumps everything else. I teach them to view assertions as way of translating their assumptions into code. And by this I mean not only assumptions about the correctness of their code (the typical scope of assertions), but also, more broadly, assumptions about the data that they are dealing with (which often comes from external sources with abysmal quality control). My scientific code is jam-packed with assertions. I can't count the number of times that one such lowly assertion saved me from a silent but potentially disastrous bug. And yes I find it distressing that so so few programmers in my line of work use assertions at all. kynn From vel.accel at gmail.com Wed Jul 8 22:23:53 2009 From: vel.accel at gmail.com (dieter) Date: Wed, 8 Jul 2009 19:23:53 -0700 (PDT) Subject: PyGtk Depends on Numeric Message-ID: <1ebe9314-9434-459a-bd3e-2b2386a35f1b@n11g2000yqb.googlegroups.com> Get with the times people and port to numpy. :P Don't you think its about time? ~Pete From jpablo.romero at gmail.com Wed Jul 8 22:55:53 2009 From: jpablo.romero at gmail.com (=?ISO-8859-1?Q?Juan_Pablo_Romero_M=E9ndez?=) Date: Wed, 8 Jul 2009 21:55:53 -0500 Subject: PyQt GUI In-Reply-To: References: Message-ID: I use OpenInventor (Coin3d) which have a python binding called "pivy". It works great. http://pivy.coin3d.org/ Juan Pablo 2009/7/8 Helvin : > Hi experts! > > I'm developing a GUI for a software using PyQT, and need 3D > visualization. Should I use PyOpenGL or VTK? > I understand that the PyQt package comes with a opengl module. What > else would I need? I think I need to download opengl. but how? where? > I have VTK and pyVTK installed, but I don't know how to use it in my > code, as when I run it, an error says it can't find the vtk module. > > Help would be sooooo appreciated! > Helvin > -- > http://mail.python.org/mailman/listinfo/python-list > From fatkinson at mishmash.com Wed Jul 8 23:53:12 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Wed, 08 Jul 2009 20:53:12 -0700 Subject: IP Address Function References: Message-ID: On Wed, 08 Jul 2009 12:29:54 +0200, Piet van Oostrum wrote: >Something like: > >#! /usr/bin/env python > >import cgi >from os import getenv > >print "Content-type: text/html" >print > >ipaddr = (getenv("HTTP_CLIENT_IP") or > getenv("HTTP_X_FORWARDED_FOR") or > getenv("HTTP_X_FORWARDED_FOR") or > getenv("REMOTE_ADDR") or > "UNKNOWN") > >print ipaddr That did it. I wonder why they don't just have a function to return it instead of putting you through all of that? At any rate, it works. Regards, Fred From nobody at nowhere.com Thu Jul 9 00:38:32 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 09 Jul 2009 05:38:32 +0100 Subject: IP Address Function References: Message-ID: On Wed, 08 Jul 2009 20:53:12 -0700, Fred Atkinson wrote: >>ipaddr = (getenv("HTTP_CLIENT_IP") or >> getenv("HTTP_X_FORWARDED_FOR") or >> getenv("HTTP_X_FORWARDED_FOR") or >> getenv("REMOTE_ADDR") or >> "UNKNOWN") >> >>print ipaddr > > That did it. > > I wonder why they don't just have a function to return it instead of > putting you through all of that? There's no unambiguous definition of "client IP", so you have to choose which definition you want. REMOTE_ADDR is set to the actual IP address from which the connection originated (from getpeername()). If the connection was made via a proxy, REMOTE_ADDR will contain the IP address of the proxy. The others are set from HTTP headers. Proxies often add Client-IP or X-Forwarded-For headers to indicate the originating IP address. OTOH, there's no way to know if the headers are accurate; nuisance users may add these headers to confuse poorly-conceived banning mechanisms. From sajmikins at gmail.com Thu Jul 9 01:05:57 2009 From: sajmikins at gmail.com (Simon Forman) Date: Wed, 8 Jul 2009 22:05:57 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: (I wanted to reply to a few messages in one post so I quoted them all below. Let me know if this is bad etiquette.) On Jul 8, 8:23 am, kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5... at q11g2000yqi.googlegroups.com> Simon Forman writes: > > >Frankly, I'm of the impression that it's a mistake not to start > >teaching programming with /the bit/ and work your way up from there. > >I'm not kidding. I wrote a (draft) article about this: "Computer > >Curriculum"http://docs.google.com/View?id=dgwr777r_31g4572gp4 > >I really think the only good way to teach computers and programming is > >to start with a bit, and build up from there. "Ontology recapitulates > >phylogeny" > > I happen to be very receptive to this point of view. I had the > benefit of that sort of training (one of the first computer courses > I took started, believe it or not, with Turing machines, through > coding in machine language, and compiler theory, and all the way > up to dabbling with Unix!), and I suspect that the reason it is > sometimes difficult for me to explain even relatively simple-looking > things to others is that I have this background that I unconsciously, > and incorrectly, take for granted in others... There is this Yes! Once the concepts become so familiar that you call them "intuitive" it seems to be very difficult to remember what they were like before. Something like "a = b" becomes "obvious" only after you've internalized the preceding concepts. > persistent idea "out there" that programming is a very accessible > skill, like cooking or gardening, anyone can do it, and even profit > from it, monetarily or otherwise, etc., and to some extent I am Programming is not like any other human activity. I've been reading some of Prof. Dijkstra's EWDs in the last few days. In one [1] he says, "automatic computers embody not only one radical novelty but two of them", to wit: First, the huge scales that must be understood, "from a bit to a few hundred megabytes, from a microsecond to a half an hour of computing"; and second, "that the automatic computer is our first large-scale digital device" which our until-now overwhelmingly analog experience does not prepare us to deal with well. He talks about how "when all is said and done, the only thing computers can do for us is to manipulate symbols and produce results of such manipulations" and he emphasises the "uninterpreted" nature of mechanical symbol manipulation, i.e. that the machine is doing it mindlessly. Dijkstra[1]: "It is true that the student that has never manipulated uninterpreted formulae quickly realizes that he is confronted with something totally unlike anything he has ever seen before. But fortunately, the rules of manipulation are in this case so few and simple that very soon thereafter he makes the exciting discovery that he is beginning to master the use of a tool that, in all its simplicity, gives him a power that far surpasses his wildest dreams." [1] > actively contributing to this perception by teaching this course > to non-programmers (experimental biologists to be more precise), Experimental biologists? Well that's probably harmless. Mostly harmless. > but maybe this idea is not entirely true... Maybe, to get past > the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest > that real computer scientists learn about in their formal training... > > kj If you're never exposed to that constellation of concepts that underpins "mechanical symbol manipulation" you are adrift in a sea ("c", ha ha) of abstractions. However, if you /are/ exposed to the "so few and simple" rules of manipulation the gates (no pun intended) to the kingdom are thrown wide. On Jul 8, 9:10 am, Steven D'Aprano wrote: > On Wed, 08 Jul 2009 12:23:50 +0000, kj wrote: > > I happen to be very receptive to this point of view. > [...] > > There is this persistent idea "out there" that > > programming is a very accessible skill, like cooking or gardening, > > anyone can do it, and even profit from it, monetarily or otherwise, > > etc., and to some extent I am actively contributing to this perception > > by teaching this course to non-programmers (experimental biologists to > > be more precise), but maybe this idea is not entirely true... > > There is some evidence that 30-60% of people simply cannot learn to > program, no matter how you teach them: > > http://www.codinghorror.com/blog/archives/000635.html > http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ Thank you! That's exactly the paper that prompted me to write the article I mentioned. (Now I don't have to go find the link myself. Win!) I don't buy it: I believe strongly that any normal person can learn to program, to manipulate symbols to create formulae that guide the machine in its uninterpreted symbol manipulation. I find it significant that in the paper [2] they say, "Formal logical proofs, and therefore programs ? formal logical proofs that particular computations are possible, expressed in a formal system called a programming language ? are utterly meaningless. To write a computer program you have to come to terms with this, to accept that whatever you might want the program to mean, the machine will blindly follow its meaningless rules and come to some meaningless conclusion. In the test the consistent group showed a pre-acceptance of this fact: they are capable of seeing mathematical calculation problems in terms of rules, and can follow those rules wheresoever they may lead. The inconsistent group, on the other hand, looks for meaning where it is not." In other words the people who don't understand computers, don't understand computers. I think that "first hump" people can become "second hump" people but that it requires teaching them the foundations first, not confronting them with such incredible novelties as "a = b" and saying in effect, "here you go buddy, sink or swim." Quoting Dijkstra again [1]: "Before we part, I would like to invite you to consider the following way of doing justice to computing's radical novelty in an introductory programming course. "On the one hand, we teach what looks like the predicate calculus, but we do it very differently from the philosophers. In order to train the novice programmer in the manipulation of uninterpreted formulae, we teach it more as boolean algebra, familiarizing the student with all algebraic properties of the logical connectives. To further sever the links to intuition, we rename the values {true, false} of the boolean domain as {black, white}. "On the other hand, we teach a simple, clean, imperative programming language, with a skip and a multiple assignment as basic statements, with a block structure for local variables, the semicolon as operator for statement composition, a nice alternative construct, a nice repetition and, if so desired, a procedure call. To this we add a minimum of data types, say booleans, integers, characters and strings. The essential thing is that, for whatever we introduce, the corresponding semantics is defined by the proof rules that go with it." Imagine my surprise: he's saying (with immensely greater brilliance and eloquence) much what I said in my little article. The major difference from what he's outlined is that I think the students should implement the imperative programming language themselves in Forth, but the gist is the same. > I'm sympathetic to the idea, but not entirely convinced. Perhaps the > problem isn't with the students, but with the teachers, and the > languages: > > http://www.csse.monash.edu.au/~damian/papers/PDF/SevenDeadlySins.pdf > > (My money is that it's a little of both.) Hmm, that paper contains some good insights IMO, but I think they're still missing the big picture, so to speak. Really I suspect it's a case of "Programming languages considered harmful." The core abstractions of [mechanical] computation are just not that complicated. You can teach them to anybody in about a half an hour, drunk. I have. After that, if they're interested, there is a smooth easy path to "higher" abstractions: parsing, compiling, tree traversal and transformation. (It is said that possession is 9/10s of the law, in the same vein I would claim parsing is 9/10s of computer programming.) I am beginning to suspect that concrete static (in the sense of "standard" language specifications) languages are part of the problem. Everyone gets so caught up in programming via languages that you get, well, people trying to teach "Computer Programming" as if it were only necessary to grok a language, rather than grokking /symbol manipulation/ itself. (Did you read that last paragraph and think, "Well how the heck else are you supposed to program a computer if not in a computer language?"? If so, well, that is kind of my point.) > > Maybe, to > > get past the most amateurish level, one has to, one way or another, come > > face-to-face with bits, compilers, algorithms, and all the rest that > > real computer scientists learn about in their formal training... > > The "No True Scotsman" fallacy. > > There's nothing amateurish about building software applications that > work, with well-designed interfaces and a minimum of bugs, even if you've > never heard of Turing Machines. > > -- > Steven I beg to differ. I recall a conversation with a co-worker who had "learned" to program using PHP. Another co-worker and I were trying to convince him that there was a good reason to differentiate between hash tables and arrays. He didn't even know that they were different "things". I remember telling him, "between the metal and the desktop there is nothing but layers of abstraction. We use different names precisely because of different behaviours." He made "well-designed interfaces", but "amateurish" is about the nicest thing I would have called him. As for "a minimum of bugs"... sigh. The "minimum of bugs" is zero, if you derive your "uninterpreted formulae" /correctly/. Deriving provably correct "programs" should be what computer science and computer education are all about (not "java vocational training" as Alan Kay once decried.) Again with Dijkstra[3]: "The prime paradigma of the pragmatic designer is known as "poor man's induction", i.e. he believes in his design as long as "it works", i.e. until faced with evidence to the contrary. (He will then "fix the design".) The scientific designer, however, believes in his design because he understands why it will work under all circumstances. The transition from pragmatic to scientific design would indeed be a drastic change within the computer industry." "Obviously no errors" is the goal to strive for, and I am comfortable calling anyone an amateur who prefers "no obvious errors." (Actually that's a little harsh on the amateurs, "ama" meaning love, "amateur" is one who does something for love of it.) On Jul 8, 9:27 am, kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5... at q11g2000yqi.googlegroups.com> Simon Forman writes: > > >I'm not kidding. I wrote a (draft) article about this: "Computer > >Curriculum"http://docs.google.com/View?id=dgwr777r_31g4572gp4 > > Very cool. > > kj Hey, thank you! On Jul 8, 9:47 am, Paul Moore wrote: > 2009/7/8 kj : > > > There is this > > persistent idea "out there" that programming is a very accessible > > that real computer scientists learn about in their formal training... > > Look at it another way. Experimental biologists don't want to program, > they want to use computers to do experimental biology. It's a tool, > and they (quite reasonably) don't *care* about robustness, > portability, etc. Or even about programming, to be honest. I'd say it's just the opposite: to "use computers to do experimental biology" they want to instruct that machine to manipulate their (meaningful to them but meaningless to it) symbols in useful ways. This is nothing more or less than programming. The fact that they need to learn all sorts of details of a programming language to do that is NOT because they can't grok programming. It's because computer scientists have put too many layers of abstraction on top of the "pure" symbol manipulation and then forgotten what they have done. I have a very nice book "Introduction to Programming and Problem Solving with Pascal" that I picked up for $0.50 at a used bookstore not long ago. It says, right on page 201, in the chapter on "Running, Debugging, and Testing Programs": "One of the nice features of programming in a high-level language like Pascal is that it can be done with almost a total lack of understanding of what a computer is and how it actually operates. [...] There is no reason why someone who wants to write a computer program should have to understand the electronic circuitry of a computer, any more than someone learning to drive a car should have to understand how the internal combustion engine works." I think that's exactly wrong. What you're doing with computers doesn't change from the bit to the high-level language. It's all symbol manipulation according to the same set of rules, all the way up. The elaboration becomes involved as you go up but the process does not change qualitatively. > In the context of the original question, it's entirely reasonable (in > my view) to tell this audience "if the code does something weird you > don't understand, either ignore it and find another way or dig into > the manuals and experiment if you care". They'd very quickly find a = > a + b as a less confusing alternative to a += b. (As has been pointed > out earlier, to some extent a += b is quite an advanced construct - > after all, it's essentially an optimisation of a = a + b). On that point I completely agree. The language is getting in the way of the programming. > Biologists don't expect me to understand their discipline before I can > plant seeds in my garden, after all. (And when I do plant seeds, I > usually get far more surprising results than I could get from a += b > :-)) > > Paul. The discipline of programming is different than biology. It's incredibly simple yet profound if it's taught as what it is, i.e. automatic symbol manipulation. No scientist is a stranger to logic and reasoned argument. They shouldn't be strangers to telling their mechanical brains what to "reason" about. Learning to program should be /easy/ for someone who basically already gets it. Wow, long post. (Oh, and, er, it's ONTOGENY not Ontology that recapitulates phylogeny. Heh. My bad.) [1] "On the cruelty of really teaching computing science" http://www.cs.utexas.edu/~EWD/transcriptions/EWD10xx/EWD1036.html [2] "The camel has two humps" http://www.cs.mdx.ac.uk/research/PhDArea/saeed/paper1.pdf [3] "Can computing science save the computer industry?" http://www.cs.utexas.edu/users/EWD/transcriptions/EWD09xx/EWD920.html From gagsl-py2 at yahoo.com.ar Thu Jul 9 02:14:18 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 03:14:18 -0300 Subject: Python Error from Apress book References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> <4A54687A.6010004@ieee.org> Message-ID: En Wed, 08 Jul 2009 06:35:54 -0300, Dave Angel escribi?: > Gabriel Genellina wrote: >> Jul 2009 09:55:13 -0300, Dave Angel escribi?: >>> Gabriel Genellina wrote: >>>> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >>>> escribi?: >> >>>>> When I try to run the command as outlined in >>>>> the book "simple_markup2.py < test_input.txt > test_output.html i >>>>> get the >>>>> following error every time. >>>>> >>>>> IOError: [Errno 9] Bad file descriptor >> >> I think the error depends on the specific OS version/service pack. But >> at least on XP this appears to fix it: >> >> http://support.microsoft.com/kb/321788/en-us >> > Thanks for the link. Looking at that one, it indicates that Windows > 2000 fixed it in SP4, and XP fixed it in Sp1. But I'm already running > XP SP3, so I wonder if it's something new. I'm using XP SP3 too. Before applying the registry fix, stdout and stderr were working fine, but not stdin. After making the registry change, stdin works too. I didn't notice the issue with stdin until now (I don't use stdin very often) -- Gabriel Genellina From helvinlui at gmail.com Thu Jul 9 02:27:53 2009 From: helvinlui at gmail.com (Helvin) Date: Wed, 8 Jul 2009 23:27:53 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> Message-ID: <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> On Jul 9, 11:29?am, Robert Kern wrote: > On 2009-07-08 18:10, Helvin wrote: > > > Thanks for the fast replies! I will look into how to use VTK now. > > Where would I find VTK's explicit support for PyQt? > > Wrapping/Python/vtk/qt4/ in the VTK sources. > > > Because I have installed VTK (using its installer) and pyVTK (using > > its setup.py file), but how do I actually use it in my code? According > > to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., > > I have tried 'import vtk', but python can't find the vtk module. > > Then you have not installed VTK's Python bindings correctly. Note that pyVTK is > just a library for manipulating VTK files. The VTK Python bindings are part of > VTK's distribution itself. Exactly how did you install VTK? Did you compile it > yourself? > > -- > 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 You mean, when I download VTK, the VTK Python bindings come with it? I downloaded VTK from:http://vtk.org/files/release/5.4/ I downloaded pyVTK from: http://pypi.python.org/pypi/PyVTK/0.4.67 And then installed it by the command 'python setup.py install' according to: http://cens.ioc.ee/projects/pyvtk/ I understand that this command runs the file called 'setup.py' that came with the pyVTK download. Thanks so much for your help! I am very desperate now... Helvin From greg at cosc.canterbury.ac.nz Thu Jul 9 02:29:26 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Thu, 09 Jul 2009 18:29:26 +1200 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <7blh0eF23r79rU1@mid.individual.net> Dave Angel wrote: > By the time I graduated, I had five six-level languages ^^^ Are they languages that you have to edit using vi? :-) -- Greg From morten.aasnes at gmail.com Thu Jul 9 02:32:56 2009 From: morten.aasnes at gmail.com (Morten Nygaard Aasnes) Date: 9 Jul 2009 06:32:56 GMT Subject: Pydev 1.4.7 Released References: Message-ID: <7blh8oF2367ioU2@mid.individual.net> On 2009-07-08, Fabio Zadrozny wrote: > Hi All, > > Pydev and Pydev Extensions 1.4.7 have been released > > Details on Pydev Extensions: http://www.fabioz.com/pydev > Details on Pydev: http://pydev.sf.net > Details on its development: http://pydev.blogspot.com Nice! Will this work in Eclipse 3.5 as well? -- Morten Nygaard ?snes http://twitter.com/mortenaa http://identi.ca/mortenaa From stefan_ml at behnel.de Thu Jul 9 02:42:06 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 09 Jul 2009 08:42:06 +0200 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <7xd48bdn73.fsf@ruckus.brouhaha.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <7xd48bdn73.fsf@ruckus.brouhaha.com> Message-ID: <4a55913f$0$31874$9b4e6d93@newsspool3.arcor-online.net> Paul Rubin wrote: > Daniel Austria writes: >> just one question. How can i remove all 0 values in a list? > > I prefer: > > newlist = list(x for x in oldlist if x != 0) > > to the square bracket list comprehension that a few people have > suggested. This is because in python 2.x, the listcomp "leaks" its > index variable into the surrounding scope, but the generator > expression above doesn't. As you indicated, that's been fixed in Py3, though. So if your code works in Py3.x, you can be somewhat sure that the leak doesn't have side effects. Plus, it's pretty easy to ignore those leaks as long as you use a suitable name for the loop variable. Also note that the performance characteristics may not be identical in both cases, depending on where you run your code. Cython, for example, will write a list comprehension out as a rather straight C loop, but when we implement generator expressions in Cython, it may have to get turned into a generator function instead of a loop, so that you'd get a much larger overhead than for the plain list comprehension (although in the simple case above it would likely get optimised away). CPython shows a similar difference: $ python3.1 -m timeit '[x for x in range(1000) if x]' 10000 loops, best of 3: 170 usec per loop $ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \ '[x for x in r if x]' 1000 loops, best of 3: 222 usec per loop $ python3.1 -m timeit 'list(x for x in range(1000) if x)' 1000 loops, best of 3: 227 usec per loop $ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \ 'list(x for x in r if x)' 1000 loops, best of 3: 280 usec per loop Not that I'd consider those numbers worth bothering... Stefan From Lacrima.Maxim at gmail.com Thu Jul 9 02:50:37 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Wed, 8 Jul 2009 23:50:37 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> Message-ID: <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> On Jul 8, 10:54?pm, Piet van Oostrum wrote: > >>>>> Lacrima (L) wrote: > >L> Hello! > >L> I have just started using Emacs to write python scripts. > >L> I installed python-mode.el > >L> Then I just tried this code: > >L> print 'hello world' > >L> When I press C-c RET, new blank window is opened and emacs says: > >L> (Shell command succeeded with no output) > >L> So where is my 'hello world'? > >L> When I do C-c C-c, it prints 'hello world' successfully. > >L> Why in the first case I get no output? > > Can you check in the buffer where you have the python code what command > C-c RET is bound to? > With C-h k C-c RET > > Shell command succeeded with no output suggests that is has a different > binding than the standard one in python-mode. > > Did you happen to name your file 'test' or 'test.py? > > C-c RET does an import and 'import test' imports a standard module test. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Hi, Piet! Thanks for your reply! My file name is 'trains.py'. When I do C-h k C-c RET, it shows me help from manual: "C-c RET runs the command py-execute-import-or-reload which is an interactive Lisp function in `python-mode'..." and so on. And still when I do C-c RET, I receive "(Shell command succeeded with no output)" Any more help would be really appreciated, because I am newbie with Emacs. With regards, Max From reddy.mrp at gmail.com Thu Jul 9 03:17:52 2009 From: reddy.mrp at gmail.com (m.reddy prasad reddy) Date: Thu, 9 Jul 2009 12:47:52 +0530 Subject: can i write a assemly language programs in python Message-ID: can any one tell me how to write assembly language programs in python...if no is there any other way to write the programs in python Reddi prasad reddy ph.no:09958083797 -------------- next part -------------- An HTML attachment was scrubbed... URL: From futurebase at gmx.at Thu Jul 9 03:22:43 2009 From: futurebase at gmx.at (Daniel Austria) Date: Thu, 9 Jul 2009 00:22:43 -0700 (PDT) Subject: ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <7xd48bdn73.fsf@ruckus.brouhaha.com> <4a55913f$0$31874$9b4e6d93@newsspool3.arcor-online.net> Message-ID: <4dcaab15-2d2d-4d7c-bd93-5e7f931cb369@j32g2000yqh.googlegroups.com> Thanks a lot for your advices, i decided to use the filter() method to sort out the 0. i can ?t use the sum() function cause i need the list afterwards .... best, Dan From reddy.mrp at gmail.com Thu Jul 9 03:25:17 2009 From: reddy.mrp at gmail.com (m.reddy prasad reddy) Date: Thu, 9 Jul 2009 12:55:17 +0530 Subject: i need immediate help from u Message-ID: i m new this community .; can any one send me the solution for writing the assembly language programs in python my mail ID:redd.mrp at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Jul 9 03:29:04 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Jul 2009 09:29:04 +0200 Subject: regex help References: Message-ID: David wrote: >   > > Open: > > 5.50 > >   > Mkt Cap: > > 6.92M > >   > P/E: > > 21.99 > > > > I want to extract the open, mkt cap and P/E values - but apart from > doing loads of indivdual REs which I think would look messy, I can't > think of a better and neater looking way. Any ideas? >>> from BeautifulSoup import BeautifulSoup >>> bs = BeautifulSoup("""  ... ... Open: ... ... 5.50 ... ...   ... Mkt Cap: ... ... 6.92M ... ...   ... P/E: ... ... 21.99 ... ... """) >>> for key in bs.findAll(attrs={"class": "key"}): ... value = key.findNext(attrs={"class": "val"}) ... print key.string.strip(), "-->", value.string.strip() ... Open: --> 5.50 Mkt Cap: --> 6.92M P/E: --> 21.99 From clp2 at rebertia.com Thu Jul 9 03:29:33 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 00:29:33 -0700 Subject: i need immediate help from u In-Reply-To: References: Message-ID: <50697b2c0907090029n66002f7cy68a5c51082c2b232@mail.gmail.com> On Thu, Jul 9, 2009 at 12:25 AM, m.reddy prasad reddy wrote: > i m new this community .; > can any one send me the solution for writing the assembly language programs > in python > my mail ID:redd.mrp at gmail.com Please read http://catb.org/esr/faqs/smart-questions.html Cheers, Chris -- http://blog.rebertia.com From CHausberger at gmx.de Thu Jul 9 03:41:20 2009 From: CHausberger at gmx.de (Claus Hausberger) Date: Thu, 09 Jul 2009 09:41:20 +0200 Subject: Problem reading file with umlauts In-Reply-To: <4A53B2B4.9090409@mrabarnett.plus.com> References: <20090707135949.100950@gmx.net> <4A53643C.4020905@xs4all.nl> <20090707190046.152740@gmx.net> <4A53B2B4.9090409@mrabarnett.plus.com> Message-ID: <20090709074120.198030@gmx.net> Thanks a lot. I will try that on the weekend. Claus > Claus Hausberger wrote: > > Thanks a lot. Now I am one step further but I get another strange error: > > > > Traceback (most recent call last): > > File "./read.py", line 12, in > > of.write(text) > > UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in > position 0: ordinal not in range(128) > > > > according to google ufeff has something to do with byte order. > > > > I use an Linux system, maybe this helps to find the error. > > > 'text' contains Unicode, but you're writing it to a file that's not > opened for Unicode. Either open the output file for Unicode: > > of = codecs.open("umlaut-out.txt", "w", encoding="latin1") > > or encode the text before writing: > > text = text.encode("latin1") > > (I'm assuming you want the output file to be in Latin1.) > > > > >> Claus Hausberger wrote: > >> > >>> I have a text file with is encoding in Latin1 (ISO-8859-1). I can't > >>> change that as I do not create those files myself. I have to read > >>> those files and convert the umlauts like ? to stuff like &oumol; as > >>> the text files should become html files. > >> umlaut-in.txt: > >> ---- > >> This file is contains data in the unicode > >> character set and is encoded with utf-8. > >> Viele R?hre. Macht spa?! Ts?sch! > >> > >> > >> umlaut-in.txt hexdump: > >> ---- > >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is > con > >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in > th > >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e > unicode..chara > >> 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and > is > >> 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with > utf > >> 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele > R..hr > >> 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht > spa..! > >> 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 > Ts..sch!....... > >> > >> > >> umlaut.py: > >> ---- > >> # -*- coding: utf-8 -*- > >> import codecs > >> text=codecs.open("umlaut-in.txt",encoding="utf-8").read() > >> text=text.replace(u"?",u"oe") > >> text=text.replace(u"?",u"ss") > >> text=text.replace(u"?",u"ue") > >> of=open("umlaut-out.txt","w") > >> of.write(text) > >> of.close() > >> > >> > >> umlaut-out.txt: > >> ---- > >> This file is contains data in the unicode > >> character set and is encoded with utf-8. > >> Viele Roehre. Macht spass! Tsuesch! > >> > >> > >> umlaut-out.txt hexdump: > >> ---- > >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is > con > >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in > th > >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e > unicode...char > >> 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and > is > >> 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with > ut > >> 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele > Roe > >> 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht > spass > >> 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! > Tsuesch!..... > >> > >> > >> > >> > >> > >> -- > >> "The ability of the OSS process to collect and harness > >> the collective IQ of thousands of individuals across > >> the Internet is simply amazing." - Vinod Valloppillil > >> http://www.catb.org/~esr/halloween/halloween4.html > > > > -- > http://mail.python.org/mailman/listinfo/python-list -- Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate f?r nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02 From rajat.dudeja at gmail.com Thu Jul 9 03:55:37 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Thu, 9 Jul 2009 00:55:37 -0700 (PDT) Subject: Check file is locked? References: Message-ID: On Jul 8, 12:45?pm, Tim Golden wrote: > Rajat wrote: > > On Jul 8, 4:57 am, Lawrence D'Oliveiro > central.gen.new_zealand> wrote: > >> In message , Christian > > >> Heimes wrote: > >>> By the way most operating systems don't lock a file when it's opened for > >>> reading or writing or even executed. > >> The general conclusion seems to be that mandatory locking is more trouble > >> than it's worth. > > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > > application ( which could be a notepad, a wordpad or some other text > > editor) that have opened my file. Once the file is closed I can easily > > delete that file. > > > I guess, if file is opened by one of that application, the file must > > be locked and so is the reason I cannot delete the file. > > I assume that your real requirement is: I can't open/close/delete > this file; it must be locked by something else; what is that > something else? > > The simplest thing is probably to run sysinternals' handle util: > > ?http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx > > and use the subprocess module to parse the output. > That will give you the pid, and you can then use, eg, psutil: > > ?http://code.google.com/p/psutil/ > > to get the details of the process. > > TJG- Hide quoted text - > > - Show quoted text - I've used the Handle.exe and got the following results: ------------------------------------------------------------------------------ notepad.exe pid: 3540 COMP\rajatd C: File (RW-) C:\Documents and Settings\rajatd\Desktop 10: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 44: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 7C: Section \BaseNamedObjects\ShimSharedMemory ------------------------------------------------------------------------------ wordpad.exe pid: 2212 COMP\rajatd 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 74: Section \BaseNamedObjects\ShimSharedMemory F8: Section \BaseNamedObjects \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 170: Section \BaseNamedObjects\RotHintTable 184: File (RW-) C:\Documents and Settings\rajatd\My Documents I've also parsed this output for the PIDS. But no where in the result I got to know what file has been associated with a PID. Does for this I need to use pustil? From gagsl-py2 at yahoo.com.ar Thu Jul 9 03:57:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 04:57:15 -0300 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: En Wed, 08 Jul 2009 23:19:25 -0300, kj escribi?: > In Tim Rowe > writes: > >> 2009/7/4 kj : > >>> Precisely. =A0As I've stated elsewhere, this is an internal helper >>> function, to be called only a few times under very well-specified >>> conditions. =A0The assert statements checks that these conditions >>> are as intended. =A0I.e. they are checks against the module writer's >>> programming errors. > >> Good for you. I'm convinced that you have used the assertion >> appropriately, and the fact that so many here are unable to see that >> looks to me like a good case for teaching the right use of assertions. >> For what it's worth, I read assertions at the beginning of a procedure >> as part of the specification of the procedure, and I use them there in >> order to document the procedure. An assertion in that position is for >> me a statement to the user of the procedure "it's your responsibility >> to make sure that you never call this procedure in such a way as to >> violate these conditions". They're part of a contract, as somebody >> (maybe you) pointed out. > >> As somebody who works in the safety-critical domain, it's refreshing >> to see somebody teaching students to think about the circumstances in >> which a procedure can legitimately be called. The hostility you've >> received to that idea is saddening, and indicative of why there's so >> much buggy software out there. > > Thanks for the encouragement. > > When I teach programming, the students are scientists. For the > stuff they do, correctness of the code trumps everything else. I > teach them to view assertions as way of translating their assumptions > into code. And by this I mean not only assumptions about the > correctness of their code (the typical scope of assertions), but > also, more broadly, assumptions about the data that they are dealing > with (which often comes from external sources with abysmal quality > control). Nobody says you shouldn't check your data. Only that "assert" is not the right way to do that. Ok, it's easier to write: assert x>0 and y>0 and x0 and y>0 and x My scientific code is jam-packed with assertions. I can't count > the number of times that one such lowly assertion saved me from a > silent but potentially disastrous bug. And yes I find it distressing > that so so few programmers in my line of work use assertions at > all. I'm just saying that some of those assertions probably should be real "if ... raise" statements instead - not that you remove the checks. You may use this short function, if you prefer: def fassert(condition, message='', exc_type=AssertionError): if not condition: raise exc_type(message) -- Gabriel Genellina From __peter__ at web.de Thu Jul 9 03:59:54 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Jul 2009 09:59:54 +0200 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > "Chris Rebert" wrote in message > news:mailman.2863.1247095339.8015.python-list at python.org... > On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >> I have the "tkinter" problem and need some assistance to straighten it >> out. >> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >> >"step >> 1" and cannot import "_tkinter." I do not have that file on my computer, >> but >> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >> This python stuff is great, but the documentation frequently >> feels like it is just a bit out of my grasp. I realize that all of this >> is free but I understand the instructions on the web page to repair only >> to the >> point of confusion. I'm not an expert. How do I modify my python >> configuration? Is there a file that needs to be edited? Which setup.py >> file >> do I use? Make? or python setup.py build and python setup.py install? >> Thanks. I appreciate your help. > > - How did you install Python? > - What Linux distro are you using? > > Cheers, > Chris > http://blog.rebertia.com > I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure > exactly how I installed Python. Originally I had installed 2.5 from RPM > but 2.6 was not available for my distro (2008.1) in RPM. I downloaded > something from python.org and installed. Not sure if it was tarball or > zip file. Zip or tar doesn't matter, you are installing "from source". Python has to find the necessary include files for tcl/tk. These are in separate packages that you have to install before you invoke Python's configure script. I don't know what they are called on your system -- look for tk-dev.rpm, tcl-dev.rpm or similar. You may run into the same problem with other modules like readline. Peter From mukherjee.tanmoy at gmail.com Thu Jul 9 04:18:30 2009 From: mukherjee.tanmoy at gmail.com (Tanmoy) Date: Thu, 9 Jul 2009 13:48:30 +0530 Subject: Hello Message-ID: Hi ... I have been trying to set this 2-D array of this sort. 0 10 20 ........... 1000 1 11 21............... 1000 Here is the code i tried ... arr=[] for i in range(0,1010,10): arr.append([]) for j in range(0,1001,1): arr[i].append(i+j) print arr I am getting the following error list index out of range Please let me know where am i getting wrong. Sincerely tank -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Thu Jul 9 04:19:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 05:19:37 -0300 Subject: Problem with list of dicts and copying References: <5700df6b0907081009p5ec4167cqec36e915e0f6c4@mail.gmail.com> Message-ID: En Wed, 08 Jul 2009 14:09:43 -0300, Cameron Pulsford escribi?: > I'm representing the board as a dictionary, where the keys are > (x, y) positions, and the values are candidates. So my program goes along > picking numbers from the list of candidates and then propagating the > constraints. [...]Basically my boards list should be copies of good > boards, and when I need to > refresh the current board, I want to pull off a copy of the correct one. > [...] this isn't happening. I am always modifying the same board. You have a dictionary whose values are lists, ok? The copy() method doesn't make a copy of those lists - only of the dictionary itself. Try using the deepcopy function in the copy module: http://docs.python.org/library/copy.html -- Gabriel Genellina From mail at timgolden.me.uk Thu Jul 9 04:21:17 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 09 Jul 2009 09:21:17 +0100 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A55A87D.6060802@timgolden.me.uk> Rajat wrote: > I've used the Handle.exe and got the following results: > > ------------------------------------------------------------------------------ > notepad.exe pid: 3540 COMP\rajatd > C: File (RW-) C:\Documents and Settings\rajatd\Desktop > 10: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 44: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 7C: Section \BaseNamedObjects\ShimSharedMemory > > ------------------------------------------------------------------------------ > wordpad.exe pid: 2212 COMP\rajatd > 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 74: Section \BaseNamedObjects\ShimSharedMemory > F8: Section \BaseNamedObjects > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > 170: Section \BaseNamedObjects\RotHintTable > 184: File (RW-) C:\Documents and Settings\rajatd\My Documents > > I've also parsed this output for the PIDS. But no where in the result > I got to know what file has been associated with a PID. > > Does for this I need to use pustil? Well unless I'm missing your point considerably, the output tells you all you need to know: notepad.exe (pid 3540) has some kind of handle open on the desktop, the common controls DLL and an area of shared memory. As has been pointed out elsewhere, notepad doesn't hold the file open which it's editing: it opens it, reads the contents, and closes it again. For demonstration purposes: import os, sys import subprocess f = open (sys.executable) subprocess.call (["handle", sys.executable]) f.close () TJG From gagsl-py2 at yahoo.com.ar Thu Jul 9 04:23:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 05:23:51 -0300 Subject: windows command-line References: <376017.28322.qm@web30601.mail.mud.yahoo.com> Message-ID: En Wed, 08 Jul 2009 14:23:54 -0300, Emile van Sebille escribi?: > On 7/8/2009 10:07 AM Lucas Junqueira said... >> Hi, I'd like to run a simple windows command-line program from within >> my python script and agt all the returt it generates. Is this possible? >> How can I do it? > > Depending on python version, look into subprocess, commands or os.pipe > and related. commands is Unix only, and os.pipe by itself doesn't solve the problem - so we are left with subprocess, and that would be my reccomendation too. -- Gabriel Genellina From dickinsm at gmail.com Thu Jul 9 04:29:02 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Thu, 9 Jul 2009 01:29:02 -0700 (PDT) Subject: python3 fail to start References: <788ec223-9894-4168-b1f8-758cd51a47ad@r16g2000vbn.googlegroups.com> Message-ID: <06650e06-5073-4420-972c-1add7c9bbbd6@h8g2000yqm.googlegroups.com> On Jun 30, 3:43?pm, ts wrote: > i just install the python 3.1 dmg onto my mac. when i run python3, it > fail with : > > Fatal Python error: Py_Initialize: can't initialize sys standard > streamsLookupError: unknown encoding: > Abort trap > > couldnt understand the problem. anyone can help? Any chance you could give us some feedback about your system? The Python devs are working to fix the problem: http://bugs.python.org/issue6393 but it would be really helpful to know: - what version of OS X you're using - what output you get when you type 'locale' at a Terminal prompt - what the International settings are in your Terminal preferences (Terminal preferences -> Settings -> Advanced tab): what's the setting for the character encoding, and do you have the 'Set LANG environment variable at startup' checkbox checked? I managed to reproduce the crash by starting python3 (again at a Terminal prompt) with: LANG=UTF-8 python3 So I suspect that your locale settings are part of the problem. As a temporary workaround, something like LANG=en_US.UTF-8 python3 might work. (Substitute whatever language setting is appropriate for you in place of en_US.) Thanks! Mark From python at mrabarnett.plus.com Thu Jul 9 04:41:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 Jul 2009 09:41:42 +0100 Subject: Hello In-Reply-To: References: Message-ID: <4A55AD46.8010407@mrabarnett.plus.com> Tanmoy wrote: T Hi ... > I have been trying to set this 2-D array of this sort. > > 0 10 20 ........... 1000 > 1 11 21............... > > 1000 > > > Here is the code i tried ... > > arr=[] > for i in range(0,1010,10): > arr.append([]) > for j in range(0,1001,1): > arr[i].append(i+j) > > print arr > > I am getting the following error > list index out of range > > Please let me know where am i getting wrong. > The value of 'i' goes 0, 10, ..., etc. When i==0 you append an empty list to arr, so arr[i] is arr[0]. No problem. When i==10 you append another empty list to arr, so arr[i] is arr[10]. Index error because there's no arr[10], only arr[0] and arr[1]. From gagsl-py2 at yahoo.com.ar Thu Jul 9 05:03:21 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 06:03:21 -0300 Subject: can i write a assemly language programs in python References: Message-ID: En Thu, 09 Jul 2009 04:17:52 -0300, m.reddy prasad reddy escribi?: > can any one tell me how to write assembly language programs in > python...if > no is there any other way to write the programs in python You write Python programs using Python, not assembly. Perhaps if you provide more info on what you want to do, someone can suggest a different way... -- Gabriel Genellina From andy2.0 at gmail.com Thu Jul 9 05:26:10 2009 From: andy2.0 at gmail.com (Andy Clegg) Date: Thu, 9 Jul 2009 02:26:10 -0700 (PDT) Subject: subprocess + python-daemon - bug/problem? Message-ID: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> Hi all, I'm trying to daemonize a python program, and occasionally have it run subprocesses, however I'm running into a nasty error, as follows: "File "/users/rsg/ancl/devcocast/devcocast-svn/scripts/DaemonSpawnTes t.py", line 5, in subprocess.Popen('echo 1').wait() File "/usr/lib64/python2.5/subprocess.py", line 594, in __init__ errread, errwrite) File "/usr/lib64/python2.5/subprocess.py", line 1089, in _execute_ch ild os.waitpid(self.pid, 0) OSError: [Errno 10] No child processes" The minimal testcase showing this problem is as follows: "import daemon import subprocess daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() subprocess.Popen('echo 1').wait()" So there is no threading going on (I've found some bugs relating to subprocess and threading). I'm using Fedora 10, Python 2.5.2, and python-daemon 1.4.6 from here http://pypi.python.org/pypi/python-daemon/ . If anyone can shed some light on the situation, I'd be extremely grateful! Yours, Andy From user at example.net Thu Jul 9 05:36:13 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 11:36:13 +0200 Subject: older pythons Message-ID: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> hi everybody. i have a certain set of old python scripts and data used by said scripts which were written using python 2.3 in particular i used some features which were later modified or deprecated by the language newer versions (e.g.: cmp(), print as keyword, and such...) for reasons i find hard to explain briefly, it is unfeasible at the moment to modify scripts and data to adapt to the changes now: i ask if it is possible to have different versions of python installed on my system (linux debian) so that i can continue to use python 2.3 (as "python" from the shell) but i can also use - say - python 3.1 (maybe as "python3.1" from the shell) if yes: is there a known recommended way to do so? thanks a lot bye From andy2.0 at gmail.com Thu Jul 9 05:42:54 2009 From: andy2.0 at gmail.com (Andy Clegg) Date: Thu, 9 Jul 2009 02:42:54 -0700 (PDT) Subject: subprocess + python-daemon - bug/problem? References: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> Message-ID: <828acd8d-a9bd-4506-b695-e537af4587d8@l31g2000yqb.googlegroups.com> My apologies, the python code should have been: "import daemon import subprocess daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() subprocess.Popen(['echo','1']).wait()" However the error remains the same. Yours, Andy On Jul 9, 10:26?am, Andy Clegg wrote: > Hi all, > > I'm trying to daemonize a python program, and occasionally have it run > subprocesses, however I'm running into a nasty error, as follows: > > "File "/users/rsg/ancl/devcocast/devcocast-svn/scripts/DaemonSpawnTes > t.py", line 5, in > ? ? subprocess.Popen('echo 1').wait() > ? File "/usr/lib64/python2.5/subprocess.py", line 594, in __init__ > ? ? errread, errwrite) > ? File "/usr/lib64/python2.5/subprocess.py", line 1089, in _execute_ch > ild > ? ? os.waitpid(self.pid, 0) > OSError: [Errno 10] No child processes" > > The minimal testcase showing this problem is as follows: > > "import daemon > import subprocess > > daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() > subprocess.Popen('echo 1').wait()" > > So there is no threading going on (I've found some bugs relating to > subprocess and threading). I'm using Fedora 10, Python 2.5.2, and > python-daemon 1.4.6 from herehttp://pypi.python.org/pypi/python-daemon/ > . > > If anyone can shed some light on the situation, I'd be extremely > grateful! > > Yours, > > Andy From adrian.dziubek at gmail.com Thu Jul 9 05:53:40 2009 From: adrian.dziubek at gmail.com (Adrian Dziubek) Date: Thu, 9 Jul 2009 02:53:40 -0700 (PDT) Subject: older pythons References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> Message-ID: The recommended Debian way is update-alternatives. I find it a bit unintuitive, so I have to read through the documentation every time I use it, but it should be able link a chosen version of python to /usr/ bin/python. I don't know if it's set up by default, I have only one version installed. -- Adrian From rajat.dudeja at gmail.com Thu Jul 9 06:03:00 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Thu, 9 Jul 2009 03:03:00 -0700 (PDT) Subject: Check file is locked? References: Message-ID: On Jul 9, 1:21?pm, Tim Golden wrote: > Rajat wrote: > > I've used the Handle.exe and got the following results: > > > ---------------------------------------------------------------------------?--- > > notepad.exe pid: 3540 COMP\rajatd > > ? ? C: File ?(RW-) ? C:\Documents and Settings\rajatd\Desktop > > ? ?10: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?44: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?7C: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > > ---------------------------------------------------------------------------?--- > > wordpad.exe pid: 2212 COMP\rajatd > > ? ?1C: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?40: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?74: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > ? ?F8: Section ? ? ? \BaseNamedObjects > > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > > ? 170: Section ? ? ? \BaseNamedObjects\RotHintTable > > ? 184: File ?(RW-) ? C:\Documents and Settings\rajatd\My Documents > > > I've also parsed this output for the PIDS. But no where in the result > > I got to know what file has been associated with a PID. > > > Does for this I need to use pustil? > > Well unless I'm missing your point considerably, the output tells > you all you need to know: notepad.exe (pid 3540) has some kind > of handle open on the desktop, the common controls DLL and an > area of shared memory. As has been pointed out elsewhere, notepad > doesn't hold the file open which it's editing: it opens it, reads > the contents, and closes it again. > > For demonstration purposes: > > > import os, sys > import subprocess > > f = open (sys.executable) > subprocess.call (["handle", sys.executable]) > f.close () > > > > TJG- Hide quoted text - > > - Show quoted text - The Notepad process information is fine here. However, with wordpad the results are not much differentiating: ------------------------------------------------------------------------------ wordpad.exe pid: 2832 COMP\rajatd 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 74: Section \BaseNamedObjects\ShimSharedMemory F8: Section \BaseNamedObjects \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 170: Section \BaseNamedObjects\RotHintTable 184: File (RW-) C:\Documents and Settings\rajatd\My Documents ------------------------------------------------------------------------------ wordpad.exe pid: 844 COMP\rajatd 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 74: Section \BaseNamedObjects\ShimSharedMemory F8: Section \BaseNamedObjects \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 170: Section \BaseNamedObjects\RotHintTable 184: File (RW-) C:\Documents and Settings\rajatd\My Documents Both the wordpad applications opened 2 totally different files kept at different locations on the system. So, on the basis of above results one can not say out of these 2 wordpad apps which is the right one that could be closed. The only different thing among the two is the PIDs. From luislupeXXX at gmailXXX.com Thu Jul 9 06:06:16 2009 From: luislupeXXX at gmailXXX.com (Luis P. Mendes) Date: 09 Jul 2009 10:06:16 GMT Subject: Psyco 64 bits Message-ID: <4a55c118$0$48237$14726298@news.sunsite.dk> Hi, I used Psyco to speed up my Python code. Due to the great amount of data I have to proccess, I moved my Linux system to a 64 bits version with more RAM. It seems that Psyco cannot be used in such platforms. Or is there another version of Psyco for 64 bits platform? I googled and arrived to PyPy. But I don't know how to use it. With psyco, I used to include two statements roughly at the beginning of the script: import psyco psyco.full() With PyPy, is there a similar way to try to speed up my script? Luis From tkermode at gmail.com Thu Jul 9 06:13:43 2009 From: tkermode at gmail.com (Tom Kermode) Date: Thu, 9 Jul 2009 11:13:43 +0100 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: I wonder if the OP is trying to find out whether python programmes can be compiled and run as stand alone executables. (I know assembly and machine code are not the same, but it might be what they're after.) On windows you can use http://www.py2exe.org/ to bundle python programs into stand alone executables. They're not compiled, but your code and all the modules it relies on are brought together, meaning you can deliver a single exe than can be run like any other windows program. Maybe that helps... 2009/7/9 Gabriel Genellina : > En Thu, 09 Jul 2009 04:17:52 -0300, m.reddy prasad reddy > escribi?: > >> can any one tell me how to write assembly language programs in python...if >> no is there any other way to write the programs in python > > You write Python programs using Python, not assembly. > > Perhaps if you provide more info on what you want to do, someone can suggest > a different way... > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.kiloday.com http://www.fourstopspast.com From mail at timgolden.me.uk Thu Jul 9 06:21:51 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 09 Jul 2009 11:21:51 +0100 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A55C4BF.6080200@timgolden.me.uk> Rajat wrote: > The Notepad process information is fine here. However, with wordpad > the results are not much differentiating: > > ------------------------------------------------------------------------------ > wordpad.exe pid: 2832 COMP\rajatd > 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 74: Section \BaseNamedObjects\ShimSharedMemory > F8: Section \BaseNamedObjects > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > 170: Section \BaseNamedObjects\RotHintTable > 184: File (RW-) C:\Documents and Settings\rajatd\My Documents > ------------------------------------------------------------------------------ > wordpad.exe pid: 844 COMP\rajatd > 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 74: Section \BaseNamedObjects\ShimSharedMemory > F8: Section \BaseNamedObjects > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > 170: Section \BaseNamedObjects\RotHintTable > 184: File (RW-) C:\Documents and Settings\rajatd\My Documents > > Both the wordpad applications opened 2 totally different files kept at > different locations on the system. > > So, on the basis of above results one can not say out of these 2 > wordpad apps which is the right one that could be closed. The only > different thing among the two is the PIDs. Rajat: are you trying to find out which app is holding a file open? If so -- run handle.exe *for that filename*, as my code does: handle.exe This will only show which apps are holding that file. I suspect you're running handle.exe on its own which will show everything which is holding handles on anything. 1) Use wordpad.exe to open c:\temp\blah.txt 2) Run "handle.exe c:\temp\blah.txt" 3) Observe (at least on my Win XP Sp3 machine) that *no* process has a handle open on c:\temp\blah.txt, including wordpad, which presumably opens the file, reads it, and closes it again. The items you're seeing above are system-level handles which wordpad is holding for reasons of its own, but none of them is a user filename. TJG From mukherjee.tanmoy at gmail.com Thu Jul 9 06:31:56 2009 From: mukherjee.tanmoy at gmail.com (Tanmoy) Date: Thu, 9 Jul 2009 16:01:56 +0530 Subject: Python-list Digest, Vol 70, Issue 123 In-Reply-To: References: Message-ID: Hello , When i==0 you append an empty list to arr, so arr[i] is arr[0]. No problem. When i==10 you append another empty list to arr, so arr[i] is arr[10]. Index error because there's no arr[10], only arr[0] and arr[1]. Thanks for your prompt reply... How can i pass this problem Sincerely Tanmoy -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Jul 9 06:39:34 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 Jul 2009 11:39:34 +0100 Subject: Python-list Digest, Vol 70, Issue 123 In-Reply-To: References: Message-ID: <4A55C8E6.3040803@mrabarnett.plus.com> Tanmoy wrote: > Hello , > When i==0 you append an empty list to arr, so arr[i] is arr[0]. No > problem. > > When i==10 you append another empty list to arr, so arr[i] is arr[10]. > Index error because there's no arr[10], only arr[0] and arr[1]. > > > Thanks for your prompt reply... > > How can i pass this problem > You could try appending to arr[-1] instead. Better still: ... row = [] for j in range(0,1001,1): row.append(i+j) arr.append(row) ... From piet at cs.uu.nl Thu Jul 9 06:43:43 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 09 Jul 2009 12:43:43 +0200 Subject: IP Address Function References: Message-ID: >>>>> Fred Atkinson (FA) wrote: >FA> On Wed, 08 Jul 2009 12:29:54 +0200, Piet van Oostrum >FA> wrote: >>> Something like: >>> >>> #! /usr/bin/env python >>> >>> import cgi >>> from os import getenv >>> >>> print "Content-type: text/html" >>> print >>> >>> ipaddr = (getenv("HTTP_CLIENT_IP") or >>> getenv("HTTP_X_FORWARDED_FOR") or >>> getenv("HTTP_X_FORWARDED_FOR") or >>> getenv("REMOTE_ADDR") or >>> "UNKNOWN") >>> >>> print ipaddr >FA> That did it. >FA> I wonder why they don't just have a function to return it instead of >FA> putting you through all of that? I see now that I had getenv("HTTP_X_FORWARDED_FOR") or duplicated. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From user at example.net Thu Jul 9 06:49:59 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 12:49:59 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> Message-ID: <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Adrian Dziubek wrote: > The recommended Debian way is update-alternatives. I find it a bit > unintuitive, so I have to read through the documentation every time I > use it, but it should be able link a chosen version of python to /usr/ > bin/python. I don't know if it's set up by default, I have only one > version installed. > -- > Adrian what i was asking for is about a way to *INSTALL* and mantain different python versions, a task i think is not unusal for developers. thanks anyway for reply bye From reddy.mrp at gmail.com Thu Jul 9 06:52:44 2009 From: reddy.mrp at gmail.com (m.reddy prasad reddy) Date: Thu, 9 Jul 2009 16:22:44 +0530 Subject: need to write a assembly progrm Message-ID: my aim is to run the assembly programs in python.by that we can use that in the any labs.because we can run the python in the mobiles also.if we write assembly programs in the mobile ,the mobile act as a tool kit for the lab.tell me any other solutions for this -------------- next part -------------- An HTML attachment was scrubbed... URL: From rajat.dudeja at gmail.com Thu Jul 9 06:53:29 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Thu, 9 Jul 2009 03:53:29 -0700 (PDT) Subject: Check file is locked? References: Message-ID: <652cca82-44a3-473f-b640-c2336a9cf929@v15g2000prn.googlegroups.com> On Jul 9, 3:21?pm, Tim Golden wrote: > Rajat wrote: > > The Notepad process information is fine here. However, with wordpad > > the results are not much differentiating: > > > ---------------------------------------------------------------------------?--- > > wordpad.exe pid: 2832 COMP\rajatd > > ? ?1C: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?40: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?74: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > ? ?F8: Section ? ? ? \BaseNamedObjects > > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > > ? 170: Section ? ? ? \BaseNamedObjects\RotHintTable > > ? 184: File ?(RW-) ? C:\Documents and Settings\rajatd\My Documents > > ---------------------------------------------------------------------------?--- > > wordpad.exe pid: 844 COMP\rajatd > > ? ?1C: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?40: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?74: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > ? ?F8: Section ? ? ? \BaseNamedObjects > > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > > ? 170: Section ? ? ? \BaseNamedObjects\RotHintTable > > ? 184: File ?(RW-) ? C:\Documents and Settings\rajatd\My Documents > > > Both the wordpad applications opened 2 totally different files kept at > > different locations on the system. > > > So, on the basis of above results one can not say out of these 2 > > wordpad apps which is the right one that could be closed. The only > > different thing among the two is the PIDs. > > Rajat: are you trying to find out which app is holding a file open? > > If so -- run handle.exe *for that filename*, as my code does: > > handle.exe > > This will only show which apps are holding that file. I suspect > you're running handle.exe on its own which will show everything > which is holding handles on anything. > > 1) Use wordpad.exe to open c:\temp\blah.txt > > 2) Run "handle.exe c:\temp\blah.txt" > > 3) Observe (at least on my Win XP Sp3 machine) that *no* process > has a handle open on c:\temp\blah.txt, including wordpad, which > presumably opens the file, reads it, and closes it again. > > The items you're seeing above are system-level handles which > wordpad is holding for reasons of its own, but none of them > is a user filename. > > TJG- Hide quoted text - > > - Show quoted text - Thanks Tim for the details. Just further on this, my whole idea is to close the wordpad / notepad application so that I can delete the file and the directory where this file resides. With notepad it is no more a problem. But I'm concerned about the wordpad now. From manu3d at gmail.com Thu Jul 9 06:59:45 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Thu, 9 Jul 2009 03:59:45 -0700 (PDT) Subject: property using a classmethod Message-ID: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Greetings, today I did something like this: class MyClass(object): @classmethod def myClassMethod(self): print "ham" myProperty = property(myClassMethod, None, None) As many of you know this doesn't work and returns a TypeError: the object passed to the property is not a callable function but a classmethod object, which isn't callable at all. So, how do I do this? Ultimately all I want is a non-callable class-level attribute MyClass.myProperty that gives the result of MyClass.myClassMethod(). Can it be done? Manu From bearophileHUGS at lycos.com Thu Jul 9 07:04:10 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 9 Jul 2009 04:04:10 -0700 (PDT) Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin: > print x, sum(1 for _ in g) Don't use that, use my function :-) If g has a __len__ you are wasting time. And sum(1 ...) is (on my PC) slower. J. Clifford Dyer: > if __name__ == '__main__': > ? ? test_func(summer, 10000000) > ? ? test_func(tupler, 10000000) > ? ? test_func(summer, 100000000) > ? ? test_func(tupler, 100000000) Have you forgotten my function? Bye, bearophile From larrykavanagh at examiner.ie Thu Jul 9 07:05:46 2009 From: larrykavanagh at examiner.ie (Larry kavanagh) Date: Thu, 09 Jul 2009 12:05:46 +0100 Subject: python-ldap Message-ID: <4A55CF0A.50707@examiner.ie> Hi, I need a PYTHON-LDAP package. I'm trying to install ploneldap and it tells me I need python-ldap package first .. but I can't find one to match my versions. I'm using plone 3.2.2, Python 2.4.4 and Zope 2.10.7 on a Win32 environment. Preferable I'd like an EXE as not too familiar with whole plone configuration. The ones I've tried have all told me I need Python 2.4 installed and it can't find in the registry. My plone site tells me I'm python 2.4.4 so maybe I just need to configure some environment variables or PATHs or something prior to running an install ?? Can anyone help me Thanks Larry Please consider the environment before printing this email. Examiner Publications (Cork) Ltd Directors: G. A. Crosbie (Chairman), Thomas J. Murphy (Chief Executive), A.W. Dinan (Secretary), T.P. Crosbie. Registered In Dublin, Ireland. Registered number: 73385. Registered Office: City Quarter, Lapps Quay, Cork. From bearophileHUGS at lycos.com Thu Jul 9 07:10:53 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 9 Jul 2009 04:10:53 -0700 (PDT) Subject: Psyco 64 bits References: <4a55c118$0$48237$14726298@news.sunsite.dk> Message-ID: <51ae217e-729d-4eac-957e-b01f054e4057@t13g2000yqt.googlegroups.com> Luis P. Mendes: > It seems that Psyco cannot be used in such platforms. > Or is there another version of Psyco for 64 bits platform? > I googled and arrived to PyPy. ?But I don't know how to use it. Psyco will be updated, but probably not for 64 bit CPUs. At the moment PyPy doesn't give you much speed up. So you can try IronPython, but I don't think it will help. There's also the Boo language, that's often fast enough. If your code has few hot spots, then there are many solutions that can be used. For example Cython, Weave, ShedSkin. Or writing external code in C/C++/D/Fortran and interfacing with it with a plethora of means, like ctypes, swig, PIL, Pyd, Boost.Python, Inline, and many other. Running speed is a big problem in many Python programs, so they have invented an army of possible solutions. Bye, bearophile From lie.1296 at gmail.com Thu Jul 9 07:15:45 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 11:15:45 GMT Subject: older pythons In-Reply-To: <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > Adrian Dziubek wrote: >> The recommended Debian way is update-alternatives. I find it a bit >> unintuitive, so I have to read through the documentation every time I >> use it, but it should be able link a chosen version of python to /usr/ >> bin/python. I don't know if it's set up by default, I have only one >> version installed. >> -- >> Adrian > > what i was asking for is about a way to *INSTALL* and mantain different > python versions, a task i think is not unusal for developers. > > thanks anyway for reply > > bye Installing several minor versions (e.g. 2.3, 2.4, 2.5, 3.0) in parallel has always been supported. Just install them and it should work "automagically". However changing the system's default /usr/bin/python is NOT recommended, especially in linux systems that rely heavily on python for many of the system tools. If you install from source, you need to check for some settings to ensure the installer does not install the /usr/bin/python's symlink. If you install from a package manager, they usually doesn't change the symlink and you'll need to run a separate program to change the symlinks (e.g. python-updater in Gentoo, which will also updates all installed python packages to the appropriate version. Neat.). The easiest way to make a specific script use a specific version of python is by changing its hashbang (#!) line: instead of: #!/usr/bin/env python use #!/usr/bin/env python2.3 or whatever version it should run on. AFAIK, no major linux distributions have officially ported to python 3.x. This means switching /usr/bin/python to python3.1 will definitely break your system. As an alternative, you can always explicitly specify the version number of python you want to use: $ python2.5 Python 2.5.4 (r254:67916, Jul 5 2009, 04:12:16) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit() $ python2.6 Python 2.6.2 (r262:71600, Jul 5 2009, 04:08:11) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit() $ python3.0 Python 3.0.1 (r301:69556, Jul 5 2009, 04:03:20) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit() $ python3.0 need3.py happily running with python 3.0 $ python2 need3.py SillyError: not running with python 3.0 From piet at cs.uu.nl Thu Jul 9 07:31:44 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 09 Jul 2009 13:31:44 +0200 Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Lacrima (L) wrote: >L> Thanks for your reply! >L> My file name is 'trains.py'. >L> When I do C-h k C-c RET, it shows me help from manual: "C-c RET runs >L> the command py-execute-import-or-reload >L> which is an interactive Lisp function in `python-mode'..." and so >L> on. >L> And still when I do C-c RET, I receive "(Shell command succeeded with >L> no output)" >L> Any more help would be really appreciated, because I am newbie with >L> Emacs. I tested it and I can get this message also. It happens when you have no Python shell running (*Python* buffer). The python code is then executed by calling the python shell command and catching the output. This output is displayed in the *Python Output* buffer. It happens also with C-c C-c and it will give the same message. There is a difference, however between C-c C-x and C-c RET: The latter will import your module, and most modules are written such that importing them doesn't print anything. If you add something like print "test" and the end of file, then "test" will appear in the *Python Output* buffer. If your file has the print "hello world" statement it should display in the *Python Output* buffer. I checked that. On the other hand, if you have a python shell running you must make sure (e.g. with the cd command) that its working directory is the directory where your python file is located, otherwise the import statement will not find your file. Unless it is somewhere in the python path. Also the *Python* buffer will not automatically pop up if it is not visible, in contrast with the *Python Output* buffer. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From fabiofz at gmail.com Thu Jul 9 07:35:32 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Thu, 9 Jul 2009 08:35:32 -0300 Subject: Pydev 1.4.7 Released In-Reply-To: <7blh8oF2367ioU2@mid.individual.net> References: <7blh8oF2367ioU2@mid.individual.net> Message-ID: Yes. On Thu, Jul 9, 2009 at 3:32 AM, Morten Nygaard Aasnes wrote: > On 2009-07-08, Fabio Zadrozny wrote: >> Hi All, >> >> Pydev and Pydev Extensions 1.4.7 have been released >> >> Details on Pydev Extensions: http://www.fabioz.com/pydev >> Details on Pydev: http://pydev.sf.net >> Details on its development: http://pydev.blogspot.com > > Nice! Will this work in Eclipse 3.5 as well? > > -- > Morten Nygaard ?snes ? http://twitter.com/mortenaa ? http://identi.ca/mortenaa > -- > http://mail.python.org/mailman/listinfo/python-list > From user at example.net Thu Jul 9 07:39:37 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 13:39:37 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Lie Ryan wrote: > AFAIK, no major linux distributions have officially ported to python > 3.x. http://packages.debian.org/experimental/python3.1 thanks for help From helvinlui at gmail.com Thu Jul 9 07:42:37 2009 From: helvinlui at gmail.com (Helvin) Date: Thu, 9 Jul 2009 04:42:37 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> Message-ID: <7a641820-7e7e-44a8-aae5-9cf9822aab6d@p36g2000prn.googlegroups.com> On Jul 9, 6:27?pm, Helvin wrote: > On Jul 9, 11:29?am, Robert Kern wrote: > > > > > > > On 2009-07-08 18:10, Helvin wrote: > > > > Thanks for the fast replies! I will look into how to use VTK now. > > > Where would I find VTK's explicit support for PyQt? > > > Wrapping/Python/vtk/qt4/ in the VTK sources. > > > > Because I have installed VTK (using its installer) and pyVTK (using > > > its setup.py file), but how do I actually use it in my code? According > > > to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., > > > I have tried 'import vtk', but python can't find the vtk module. > > > Then you have not installed VTK's Python bindings correctly. Note that pyVTK is > > just a library for manipulating VTK files. The VTK Python bindings are part of > > VTK's distribution itself. Exactly how did you install VTK? Did you compile it > > yourself? > > > -- > > 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 > > You mean, when I download VTK, the VTK Python bindings come with it? > I downloaded VTK from:http://vtk.org/files/release/5.4/ > > I downloaded pyVTK from:http://pypi.python.org/pypi/PyVTK/0.4.67 > And then installed it by the command 'python setup.py install' > according to:http://cens.ioc.ee/projects/pyvtk/ > I understand that this command runs the file called 'setup.py' that > came with the pyVTK download. > > Thanks so much for your help! I am very desperate now... > > Helvin I googled: vtk for beginners python. >From there, I found this: http://www.nabble.com/pls-help-how-to-install-VTK-and-run-td14977428.html And from there, this: http://www-viz.tamu.edu/courses/viza658/08spring/tutorials/WinVTKInstall.html This last one has very detailed info about how to get started with VTK. I am now redoing the installing VTK thing. Hope it will work. Helvin From mondi at cs.unibo.it Thu Jul 9 07:50:20 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Thu, 09 Jul 2009 11:50:20 +0000 Subject: hoe to build a patched socketmodule.c Message-ID: Hi all, I need to patch socketmodule.c (the _socket module) in order to add support to an experimental socket family. I'm guessing about the patched module build process: does a stand alone build script using distutils makes sense, or have I to patch sockmodule.c, eventualy rename it, modify setup.py inside python sources to let it know about the new module, and then rebuild whole python? I think the latter makes more sense, because I suppose socketmodule could not be build stand alone, but need the whole python build system, in order to have right definition specified by a Makefile generated by autotools from inside python sources. Or maybe distutils is able to retreive the whole build system? I don't think so, because there are no requirement about having python source tree installed. If I have to go for the second way, then how could I distribuite the patched _socket module? I could not use distutils, have I to distribuite the already built .so with only a script to install it, and the source code in another directory? Thanks a lot jacopo PS correct my english please, is the only way I have to improve it! From rashidsdpk at gmail.com Thu Jul 9 07:59:28 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Thu, 9 Jul 2009 04:59:28 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10. for more details on http://0nanotechnology0.blogspot.com From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 9 07:59:44 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 09 Jul 2009 13:59:44 +0200 Subject: property using a classmethod In-Reply-To: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: <4a55db9e$0$10243$426a74cc@news.free.fr> Emanuele D'Arrigo a ?crit : > Greetings, > > today I did something like this: > > class MyClass(object): > > @classmethod > def myClassMethod(self): Usually, the first argument of classmethods is named 'cls' > print "ham" > > myProperty = property(myClassMethod, None, None) > > As many of you know this doesn't work and returns a TypeError: the > object passed to the property is not a callable function but a > classmethod object, which isn't callable at all. So, how do I do this? > Ultimately all I want is a non-callable class-level attribute > MyClass.myProperty properties *are* class attributes. > that gives the result of MyClass.myClassMethod(). > > Can it be done? You could write your own custom descriptor. Or just use an additional level of indirection, ie: myProperty = property(lambda self: self.myClassMethod()) but since you already have myClassMethod available, I don't see the point. What problem are you trying to solve exactly ? From lie.1296 at gmail.com Thu Jul 9 08:10:33 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 12:10:33 GMT Subject: older pythons In-Reply-To: <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Lie Ryan wrote: > >> AFAIK, no major linux distributions have officially ported to python >> 3.x. > > http://packages.debian.org/experimental/python3.1 > > thanks for help Note the word "experimental" From michael at stroeder.com Thu Jul 9 08:17:49 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Thu, 09 Jul 2009 14:17:49 +0200 Subject: python-ldap In-Reply-To: References: Message-ID: Larry kavanagh wrote: > I need a PYTHON-LDAP package. > > I'm trying to install ploneldap and it tells me I need python-ldap > package first .. but I can't find one to match my versions. > > I'm using plone 3.2.2, Python 2.4.4 and Zope 2.10.7 on a Win32 environment. Did you check all the links on this page? http://www.python-ldap.org/download.shtml There's a MSI installer for Python 2.4 on Win32: http://pypi.python.org/packages/2.4/p/python-ldap/python-ldap-2.3.8.win32-py2.4.exe#md5=35da547711280c18bd4ccd6e637cdf9b There has been an update of the .egg files recently (also link on the download page above). Ciao, Michael. -- Michael Str?der E-Mail: michael at stroeder.com http://www.stroeder.com From xahlee at gmail.com Thu Jul 9 08:19:29 2009 From: xahlee at gmail.com (Xah Lee) Date: Thu, 9 Jul 2009 05:19:29 -0700 (PDT) Subject: OT: unix to Windows technology References: <4a54ceb0$0$5898$607ed4bc@cv.net> <16430ee7-888e-42ef-9c84-14df4b78ee04@p36g2000prn.googlegroups.com> Message-ID: <0de99585-6a35-498d-b5d1-6eb8724c41b3@m3g2000pri.googlegroups.com> a fried showed me this today: http://xahlee.org/funny/Microsoft_eula.html not sure which site originally reported it. Xah ? http://xahlee.org/ ? From user at example.net Thu Jul 9 08:26:37 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 14:26:37 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Message-ID: <4a55e1fb$0$1116$4fafbaef@reader3.news.tin.it> Lie Ryan wrote: > superpollo wrote: > >>Lie Ryan wrote: >> >> >>>AFAIK, no major linux distributions have officially ported to python >>>3.x. >> >>http://packages.debian.org/experimental/python3.1 >> >>thanks for help > > > Note the word "experimental" i noticed. isn't experimental official? i thought it was... thanks for your reply bye and god bless you From marco at sferacarta.com Thu Jul 9 08:37:11 2009 From: marco at sferacarta.com (Marco Mariani) Date: Thu, 09 Jul 2009 14:37:11 +0200 Subject: older pythons In-Reply-To: <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > what i was asking for is about a way to *INSTALL* and mantain different > python versions, a task i think is not unusal for developers. Check out virtualenv, I ask myself how I could work without it. http://pypi.python.org/pypi/virtualenv From lie.1296 at gmail.com Thu Jul 9 08:37:30 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 12:37:30 GMT Subject: property using a classmethod In-Reply-To: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: Emanuele D'Arrigo wrote: > Greetings, > > today I did something like this: > > class MyClass(object): > > @classmethod > def myClassMethod(self): > print "ham" > > myProperty = property(myClassMethod, None, None) > > As many of you know this doesn't work and returns a TypeError: the > object passed to the property is not a callable function but a > classmethod object, which isn't callable at all. That code runs fine for me. Although I doubt the behavior is what you wanted to do. > So, how do I do this? > Ultimately all I want is a non-callable class-level attribute > MyClass.myProperty that gives the result of MyClass.myClassMethod(). This works like what you seem to want (it's ugly): class MyClass(object): class _MyClass(object): @classmethod def myClassMethod(cls): return 'ham' @property def myProperty(self): return MyClass._MyClass.myClassMethod() @classmethod def myClassMethod(cls): return MyClass._MyClass.myClassMethod() @property def myProperty(self): return MyClass._MyClass.myClassMethod() def __call__(self, *args, **kargs): # this is __init__ return MyClass._MyClass(*args, **kargs) # note this is NOT a real MyClass instantiation MyClass = MyClass() $ python -i ./strangeclass.py >>> MyClass.myClassMethod() 'ham' >>> MyClass.myProperty 'ham' >>> mc = MyClass() >>> mc.myProperty 'ham' >>> mc.myClassMethod() 'ham' From mukherjee.tanmoy at gmail.com Thu Jul 9 08:43:57 2009 From: mukherjee.tanmoy at gmail.com (Tanmoy) Date: Thu, 9 Jul 2009 18:13:57 +0530 Subject: Hello Message-ID: > > Hello , > When i==0 you append an empty list to arr, so arr[i] is arr[0]. No > problem. > > When i==10 you append another empty list to arr, so arr[i] is arr[10]. > Index error because there's no arr[10], only arr[0] and arr[1]. > > Thanks for your prompt reply... > > How can i pass this problem > > You could try appending to arr[-1] instead. Better still: ... row = [] for j in range(0,1001,1): row.append(i+j) arr.append(row) ... I tried both the ways... the only thing is its not coming like 0 10 20.......1000 . Its coming like 0,1,2,........1000.....Any answer to the problem. Thanks Tanmoy Mukherjee -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Jul 9 08:44:18 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 08:44:18 -0400 Subject: function local namespace question In-Reply-To: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> References: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Message-ID: <4A55E622.3050906@ieee.org> Paul LaFollette wrote: > Kind people, > > Using Python 3.1 under FreeBSD and WinXP. > > I've been tearing my hair out trying to solve this myself, but I need > to ask for help. I want (for obscure reasons) to be able to log > transactions in the namespace(s) of a script. Specifically I would > like to log creation of identifiers, changes in the binding of > identifiers ("assignment") and lookups. This turns out to be pretty > easy in the global and local namespaces... I simply subclass dict, > override the appropriate operations to include the logging operations > I want, and then exec the code using my dictionaries as the global and > local namespaces. All of this works just dandy until I try to > extend it to functions. > > I cannot figure out any way to get a hook into the local namespace of > a user defined function. I have tried making a wrapper class that > grabs the function call and then uses exec to invoke > myfunction.__code__ with my own dictionaries. This runs the (no > argument) function properly (losing the return value, but I can deal > with that) but never accesses the local logging dictionary that I > specify in the exec() call. Perhaps the local namespace of a function > is not a dict at all? > > Anyway, is there any way (however clumsy) to do what I want to do? > Thank you for your help. > Paul > > --------------------------- > Paul LaFollette > CIS Department > Temple University > paul.lafollette(at)temple.edu > www.cis.temple.edu/~lafollet > > Is there a way? Undoubtedly. The simplest way that occurs to me is to have a pre-pass that rewrites the .pyc files, adding instrumentation to the byte-code. You could also recompile the Python interpreter, with some extra hooks in it. That could get tricky, as you don't want to instrument the code that's doing the tracking. Fnally, you could hook into the byte-code loader, doing the changes at that time. As always, if the code is for commercial use, beware of infringing on patents. I have a few in the 3rd area (though the ownership is with other companies; I was just the inventor.) DaveA From deets at nospam.web.de Thu Jul 9 08:54:25 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 09 Jul 2009 14:54:25 +0200 Subject: function local namespace question References: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Message-ID: <7bm7bsF22aoa9U1@mid.uni-berlin.de> Dave Angel wrote: > Paul LaFollette wrote: >> Kind people, >> >> Using Python 3.1 under FreeBSD and WinXP. >> >> I've been tearing my hair out trying to solve this myself, but I need >> to ask for help. I want (for obscure reasons) to be able to log >> transactions in the namespace(s) of a script. Specifically I would >> like to log creation of identifiers, changes in the binding of >> identifiers ("assignment") and lookups. This turns out to be pretty >> easy in the global and local namespaces... I simply subclass dict, >> override the appropriate operations to include the logging operations >> I want, and then exec the code using my dictionaries as the global and >> local namespaces. All of this works just dandy until I try to >> extend it to functions. >> >> I cannot figure out any way to get a hook into the local namespace of >> a user defined function. I have tried making a wrapper class that >> grabs the function call and then uses exec to invoke >> myfunction.__code__ with my own dictionaries. This runs the (no >> argument) function properly (losing the return value, but I can deal >> with that) but never accesses the local logging dictionary that I >> specify in the exec() call. Perhaps the local namespace of a function >> is not a dict at all? >> >> Anyway, is there any way (however clumsy) to do what I want to do? You might consider using the trace-functionality of python, exposed throug sys.settrace It's very expensive of course to do that. Diez From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 9 08:56:18 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 09 Jul 2009 14:56:18 +0200 Subject: property using a classmethod In-Reply-To: References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: <4a55e8e0$0$8041$426a34cc@news.free.fr> Lie Ryan a ?crit : > Emanuele D'Arrigo wrote: (snip) >> Ultimately all I want is a non-callable class-level attribute >> MyClass.myProperty that gives the result of MyClass.myClassMethod(). > > This works like what you seem to want (it's ugly): Ugly, indeed. And an extreme case of arbitrary overcomplexification too :-/ (snip rube goldberg code) From user at example.net Thu Jul 9 09:04:30 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 15:04:30 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: <4a55eadd$0$1108$4fafbaef@reader3.news.tin.it> Marco Mariani wrote: > superpollo wrote: > >> what i was asking for is about a way to *INSTALL* and mantain >> different python versions, a task i think is not unusal for developers. > > > Check out virtualenv, I ask myself how I could work without it. > > http://pypi.python.org/pypi/virtualenv > much obliged! From lie.1296 at gmail.com Thu Jul 9 09:20:19 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 13:20:19 GMT Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: Michael Mossey wrote: > On Jul 6, 2:47 pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >>> What is required in a python program to make sure it catches a >>> control- >>> c on the command-line? Do some i/o? The OS here is Linux. >> You can use a try/except to catch a KeyboardInterrupt exception, or >> you can trap it using the signal module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH >> Philip > > Thanks to both of you. However, my question is also about whether I > need to be doing i/o or some similar operation for my program to > notice in any shape or form that Control-C has been pressed. In the > past, I've written Python programs that go about their business > ignoring Ctrl-C. Other programs respond to it immediately by exiting. > I think the difference is that the latter programs are doing i/o. But > I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. > > For example, does trapping SIGINT always work, regardless of what my > process is doing? > > Thanks, > Mike Are you asking: "when would the python interpreter process KeyboardInterrupt?" In a multi threaded python program (where KeyboardInterrupt doesn't always work), only the main thread can process KeyboardInterrupt; thus the main thread must regain control before the interrupt is raised. Normally, python will context switch (i.e. thread switch) every 100 interpreter "ticks", but when the interpreter received a SIGINT, the interpreter will try to context switch as fast as it can (every tick) to allow the main thread to regain control. So the answer is in multithreaded python program: "when the main thread regains control" In single threaded python program, the currently running thread is always the main thread (which can handle KeyboardInterrupt). I believe SIGINT is checked at every ticks. But SIGINT cannot interrupt atomic operations (i.e. it cannot interrupt long operations that takes a single tick). An example, where python have difficulties processing KeyboardInterrupt: >>> print 'foo'*10000000 ...foofoofoo... because printing a string is an atomic operation I believe a tick in python is equivalent to a single bytecode, but please correct me if I'm wrong. From python at mrabarnett.plus.com Thu Jul 9 09:25:36 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 Jul 2009 14:25:36 +0100 Subject: Hello In-Reply-To: References: Message-ID: <4A55EFD0.7080408@mrabarnett.plus.com> Tanmoy wrote: > Hello , > When i==0 you append an empty list to arr, so arr[i] is > arr[0]. No > problem. > > When i==10 you append another empty list to arr, so arr[i] is arr[10]. > Index error because there's no arr[10], only arr[0] and arr[1]. > > Thanks for your prompt reply... > > How can i pass this problem > > You could try appending to arr[-1] instead. > > Better still: > > ... > row = [] > for j in range(0,1001,1): > row.append(i+j) > arr.append(row) > ... > I tried both the ways... the only thing is its not coming like 0 10 > 20.......1000 . Its coming like 0,1,2,........1000.....Any answer to the > problem. > Each row should be 1 more than the previous row and each column should be 10 more than the previous column. Just swap your ranges. From Lacrima.Maxim at gmail.com Thu Jul 9 09:28:07 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Thu, 9 Jul 2009 06:28:07 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: On Jul 9, 2:31?pm, Piet van Oostrum wrote: > >>>>> Lacrima (L) wrote: > >L> Thanks for your reply! > >L> My file name is 'trains.py'. > >L> When I do C-h k C-c RET, it shows me help from manual: "C-c RET runs > >L> the command py-execute-import-or-reload > >L> ? ?which is an interactive Lisp function in `python-mode'..." and so > >L> on. > >L> And still when I do C-c RET, I receive "(Shell command succeeded with > >L> no output)" > >L> Any more help would be really appreciated, because I am newbie with > >L> Emacs. > > I tested it and I can get this message also. It happens when you have no > Python shell running (*Python* buffer). The python code is then executed > by calling the python shell command and catching the output. This output > is displayed in the *Python Output* buffer. It happens also with C-c C-c > and it will give the same message. > > There is a difference, however between C-c C-x and C-c RET: The latter > will import your module, and most modules are written such that > importing them doesn't print anything. If you add something like > print "test" and the end of file, then "test" will appear in the *Python > Output* ?buffer. > > If your file has the print "hello world" statement it should display in > the *Python Output* buffer. I checked that. > > On the other hand, if you have a python shell running you must make sure > (e.g. with the cd command) that its working directory is the directory > where your python file is located, otherwise the import statement will > not find your file. Unless it is somewhere in the python path. Also the > *Python* buffer will not automatically pop up if it is not visible, in > contrast with the *Python Output* buffer. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Thank you for really useful and detailed explanation. Now I can test my code using Emacs. But I think it works for me in a little bit different way. My file contains only the print 'hello world'. If I have no python shell running, then: a) C-c RET opens *Python Output* buffer without any output and with 'Shell command succeeded with no output' message at the bottom b) C-c C-c opens *Python Output* with appropriate 'hello world' message. Then I run python shell with C-c ! command. And: a) C-c RET opens *Python* buffer and prints 'hello world' in the python shell b) C-c C-c gives the same result. With regards, Max From davea at ieee.org Thu Jul 9 09:28:29 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 09:28:29 -0400 Subject: tough-to-explain Python In-Reply-To: <7blh0eF23r79rU1@mid.individual.net> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <7blh0eF23r79rU1@mid.individual.net> Message-ID: <4A55F07D.7040400@ieee.org> greg wrote: >
Dave > Angel wrote: >> By the time I graduated, I had five six-level languages > ^^^ > > Are they languages that you have to edit using vi? :-) > Back then I didn't need glasses. That was of course intended to be "six high-level languages" From shinejose22 at gmail.com Thu Jul 9 09:40:18 2009 From: shinejose22 at gmail.com (Shine Jose) Date: Thu, 9 Jul 2009 08:40:18 -0500 Subject: Problem with two instances of PySerial Message-ID: Hello friends,I am developing an application to read data from serial port and display it on the screen using GUI developed in pyQT. For this I have instantiated the GUI as well as pySerial objects in the main thread. I have provided a button 'Read' which when clicked by the user reads from the serial port using read() method of pySerial. However, for debugging purposes I also want to poll the serial port continuously for arrival of data and display on screen. I achieve this by creating a separate worker thread to poll the serial port for arrival of data and then updating the required widget in main thread of program. For polling the serial port, I create a separate instance of pySerial object. However, I am unable to read any data in the worker thread. Can the reason for this be 2 instances of pySerial objects being connected to serial port. The reason I had this doubt was because Serial.Connect() in the worker thread did not throw any exception & isOpen() method returns true. I request you to help me in figuring out the problem. -- Regards, Shine Jose -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Jul 9 09:41:38 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 09:41:38 -0400 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: <4A55F392.3080404@ieee.org> m.reddy prasad reddy wrote: > can any one tell me how to write assembly language programs in python...if > no is there any other way to write the programs in python > > Reddi prasad reddy > ph.no:09958083797 > > Assembly language is a different programming language than Python. You can use both in the same process, much in the same way you can use C or C++ with Python. In fact, some of the system DLL's are written (partly) in assembler, though mostly in C or C++. You'll need to tell us what your real goal is. Are you a python programmer trying to optimize some loop? Are you taking a course in assembler, and hope that somehow Python will help? Do you have Python installed on your system? What system is it? As far as I know, Python has no "inline assembler" mode, as C does. So anything written in assembler would be in a separate DLL, not mixed in your .py files. It's not hard to mix assembly files with C or C++ files (or Pascal, or dozens of other compiled languages), and link them into a single DLL. But Python's access to that DLL will be done through a module like ctypes, or SWIG. DaveA From nleioatt at gmail.com Thu Jul 9 09:42:06 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 9 Jul 2009 06:42:06 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" Message-ID: I've seen a lot of posts on this problem, but none seems to help. Here is the code: /code file = open(prefix1) text = file.readlines() len = len(text) fields = text[1].split() num_rows = int(fields[1]) num_cols = int(fields[2]) U1_matrix = [] print fields print repr(fields) print len(fields) for line in text[2: num_rows+2]: fields = line.split() # print "fields", fields, line for i in range(len(fields)): fields[i] = float(fields[i]) U1_matrix.append(fields) /*code prefix is a space/line delimited ascii file that represents a 2D matrix. i'm trying to read in 2 matrices from different files, strip away the header stuff and then take the dot product of the 2 matrices. any help is much appreciated. thanks, nick From gabriel.rossetti at arimaz.com Thu Jul 9 09:43:01 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Thu, 09 Jul 2009 15:43:01 +0200 Subject: Blocking XMPP API? Message-ID: <4A55F3E5.8040800@arimaz.com> Hello everyone, I am looking for blocking XMPP API. I'm wanting to make a webservice that uses XMPP, but all the XMPP libs I find are non-blocking (i.e. w/ callbacks). I'd like to be able to do something like : cl = Client("test at domain.com/res01", "password",) msg = " References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: On Thu, Jul 9, 2009 at 1:05 AM, Simon Forman wrote: > Everyone gets so caught up in programming via languages that > you get, well, people trying to teach "Computer Programming" as if it > were only necessary to grok a language, rather than grokking /symbol > manipulation/ itself. > > +1 QOTW. I'm a CS major and this is exactly what I see happening in the intro to CS courses. In class, the "Intro to Programming" students are showed how to write Java code. Then they try their homework and are completely lost because they were never taught how to think like a programmer so they don't understand how to approach the question. From lutz.horn at fastmail.fm Thu Jul 9 09:50:02 2009 From: lutz.horn at fastmail.fm (Lutz Horn) Date: Thu, 09 Jul 2009 15:50:02 +0200 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <4A55F58A.9060003@fastmail.fm> Hi, Nick schrieb: > I've seen a lot of posts on this problem, but none seems to help. Could you please post a sample input file and the exact error message? Thanks Lutz -- Strike Out ? http://www.fourmilab.ch/documents/strikeout From benjamin.kaplan at case.edu Thu Jul 9 09:51:49 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 9 Jul 2009 09:51:49 -0400 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Message-ID: On Thu, Jul 9, 2009 at 8:10 AM, Lie Ryan wrote: > superpollo wrote: >> Lie Ryan wrote: >> >>> AFAIK, no major linux distributions have officially ported to python >>> 3.x. >> >> http://packages.debian.org/experimental/python3.1 >> >> thanks for help > > Note the word "experimental" Assuming that Debian does the same thing as Ubuntu (which is highly likely), Python3.1 won't install itself as the default. Too many system features depend on Python 2.x. > -- > http://mail.python.org/mailman/listinfo/python-list > From fridrik at pyth.net Thu Jul 9 09:53:29 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 13:53:29 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <3A693537-927D-44B1-840A-D97FACFB6809@pyth.net> Look at: len = len(text) You're overriding `len` (a built-in method), with an integer (`len(text)`). You then call: for i in range(len(fields)): But `len` is no longer a callable, but merely an integer. Regards, Fri?rik M?r P.S. While this is a fairly obvious problem it's usually a good idea to post working code and a traceback when requesting help. From gagsl-py2 at yahoo.com.ar Thu Jul 9 09:53:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 10:53:42 -0300 Subject: Check file is locked? References: <652cca82-44a3-473f-b640-c2336a9cf929@v15g2000prn.googlegroups.com> Message-ID: En Thu, 09 Jul 2009 07:53:29 -0300, Rajat escribi?: > Thanks Tim for the details. Just further on this, my whole idea is to > close the wordpad / notepad application so that I can delete the file > and the directory where this file resides. > > With notepad it is no more a problem. But I'm concerned about the > wordpad now. I don't see Wordpad keeping the file open either. If you can't remove the file, use "handle.exe filename.txt" to discover which process is holding it open, as Tim already explained. -- Gabriel Genellina From R.Brodie at rl.ac.uk Thu Jul 9 10:02:27 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Thu, 9 Jul 2009 15:02:27 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: "Nick" wrote in message news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f at h18g2000yqj.googlegroups.com... > file = open(prefix1) > text = file.readlines() > len = len(text) You have redefined two built-in functions "file" and "len" in the first three lines. This is usually considered poor practice. Stick to meaningless variable names, it's safer (only joking). TypeError: 'int' object is not callable". This means that something you thought was a function is in fact an integer. It's helpful to post/look at the line number of the error; "how is this line failing", is much easier to answer than "how is my program failing". print len(fields) Here len is an integer, because you redefined it in line 3. I'm guessing this is the problem. From davea at ieee.org Thu Jul 9 10:04:23 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 10:04:23 -0400 Subject: Hello In-Reply-To: References: Message-ID: <4A55F8E7.6090304@ieee.org> You could pick a much better title. And apparently you started a new thread with this one. When responding to an existing message, you ought to reply-all to the existing message, rather than starting a new thread. Tanmoy wrote: >> Hello , >> When i==0 you append an empty list to arr, so arr[i] is arr[0]. No >> problem. >> >> When i==10 you append another empty list to arr, so arr[i] is arr[10]. >> Index error because there's no arr[10], only arr[0] and arr[1]. >> >> Thanks for your prompt reply... >> >> How can i pass this problem >> >> You could try appending to arr[-1] instead. >> > > Better still: > > ... > row = [] > for j in range(0,1001,1): > row.append(i+j) > arr.append(row) > ... > I tried both the ways... the only thing is its not coming like 0 10 > 20.......1000 . Its coming like 0,1,2,........1000.....Any answer to the > problem. > Thanks > Tanmoy Mukherjee > > If you want the row to count by 10's you have to build it that way. You have the arguments to the ranges backwards. Try: arr=[] for i in range(1001): row = [] for j in range(0,1010,10): row.append(i+j) arr.append(row) Coincidentally, once you reverse them, the original problem would go away. But this is better anyway, as it makes it clear what's happening. You are building 1001 rows, each containing non-contiguous numbers. DaveA From timmyt at smarttypes.org Thu Jul 9 10:07:52 2009 From: timmyt at smarttypes.org (timmyt) Date: Thu, 9 Jul 2009 07:07:52 -0700 (PDT) Subject: a little wsgi framework References: <4a53132c$0$10839$426a34cc@news.free.fr> Message-ID: <103cb80e-5a5a-4c41-ae7c-c0ae122cfebf@b25g2000prb.googlegroups.com> On Jul 7, 5:19?am, Bruno Desthuilliers wrote: > timmyt a ?crit : > > > i'm interested in getting opinions on a smallwsgiframework i > > assembled from webob, sqlalchemy, genshi, and various code fragments i > > found on the inter-tubes > > > here is the interesting glue - any comments / suggestions would be > > much appreciated > > > Well... My first comment would be about the usefulness of yet another > Python web framework, but let's not worry about this... > > > > ------------------ > > thewsgiapp > > ------------------ > > def application(environ, start_response): > > ? ? path = environ.get('PATH_INFO', '').lstrip('/') > > > ? ? for regex, callback in urls: > > ? ? ? ? match = re.search(regex, path) > > I don't know where these "urls" come from. But anyway : if they live in > some sort of long-running process, you may want to precompile them. > > > ? ? ? ? if match: > > ? ? ? ? ? ? environ['myapp.url_args'] = match.groups() > > ? ? ? ? ? ? request = webob.Request(environ) > > > ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? return callback(request, start_response) > > ? ? ? ? ? ? except Exception, ex: > > How are redirect etc handled ? > > > ? ? ? ? ? ? ? ? start_response('500 Internal Server Error', [('Content- > > Type', 'text/plain')]) > > ? ? ? ? ? ? ? ? return [traceback.format_exc()] > > A configuration option controlling the display of the traceback would be > fine - I surely don't want the traceback being displayed to anyone when > ? the app goes into production. > > Also, logging the error and traceback might be useful. > > > ? ? start_response('404 Not Found', [('Content-Type', 'text/plain')]) > > ? ? return ["Couldn't find the URL specified."] > > And in both cases, having the ability to use "custom" 500 and 404 pages > might be nice too. > > > > > ---------------------------------- > > the controller decorator > > ---------------------------------- > > def web_decorator(filename, method='html'): > > > ? ? def decorator(target): > > May I suggest some renaming here ? > > filename => path (or 'template_path' ?) > method ? => content_type > target ? => controller or function or callback or.... > > > ? ? ? ? def wrapper(request, start_response): > > > ? ? ? ? ? ? #genshi TemplateLoader > > ? ? ? ? ? ? template = loader.load(filename) > > > ? ? ? ? ? ? global config > > Probably not thread-safe.... > > > ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? return_dict = target(request, start_response) > > ? ? ? ? ? ? ? ? return_string = template.generate(**return_dict).render > > (method) > > Renaming again: > return_dict => context > return_string => data or text or ??? > > > ? ? ? ? ? ? ? ? config['database.Session'].commit() > > ? ? ? ? ? ? except: > > ? ? ? ? ? ? ? ? config['database.Session'].rollback() > > ? ? ? ? ? ? ? ? raise > > ? ? ? ? ? ? finally: > > ? ? ? ? ? ? ? ? config['database.Session'].remove() > > This doesn't leave much control on transactions... Sometimes you want to > handle it manually one way or another. > > > ? ? ? ? ? ? #TODO: alter 'Content-Type' per method being passed > > ? ? ? ? ? ? start_response('200 OK', [('Content-Type', 'text/html')]) > > ? ? ? ? ? ? return [return_string] > > Ok, so here again, I don't understand how redirects can work. Also, if > you do use start_response here, I don't get why you still pass it to the > callback function. > > > ? ? ? ? return wrapper > > > ? ? return decorator > > slightly OT, but preserving infos on the decorated function might help > too (introspection, debugging etc). > > My 2 cents... thank you gentlemen i'm now looking for Redirect exceptions as well, and returning the appropriate response code (stole this idea from turbogears) i like doing the response codes and exceptions myself which is why i'm not using the webob response - i think it's more explicit and straightforward the conditional display of errors goes without say - that will be trivial to add the 'global config' line was an error - config is a module level variable set before the application definition i assume as long as i don't write to the config variable within the application it is thread-safe - is that correct the underlying assumption is the wsgi application setup does not have to be thread safe - this is only run once per process From nleioatt at gmail.com Thu Jul 9 10:08:05 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 9 Jul 2009 07:08:05 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> On Jul 9, 10:02 am, "Richard Brodie" wrote: > "Nick" wrote in message > > news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f at h18g2000yqj.googlegroups.com... > > > file = open(prefix1) > > text = file.readlines() > > len = len(text) > > You have redefined two built-in functions "file" and "len" in the first three lines. > This is usually considered poor practice. Stick to meaningless variable names, > it's safer (only joking). > > TypeError: 'int' object is not callable". This means that something you thought > was a function is in fact an integer. It's helpful to post/look at the line number of > the error; "how is this line failing", is much easier to answer than > "how is my program failing". > > print len(fields) > > Here len is an integer, because you redefined it in line 3. I'm guessing this is the > problem. thanks for spotting the obvious errors, its my 2nd day programming python in about 3 years. fridrick, code should be workable with the exception of the errors...thats the whole program Thanks again for all the help problem fixed From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 9 10:24:19 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 09 Jul 2009 16:24:19 +0200 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <4a55fd80$0$9943$426a74cc@news.free.fr> Nick a ?crit : > I've seen a lot of posts on this problem, but none seems to help. > Here is the code: > /code > > file = open(prefix1) shadows the builtin 'file' type. > text = file.readlines() > len = len(text) shadows the builtin 'len' function. > fields = text[1].split() > num_rows = int(fields[1]) > num_cols = int(fields[2]) > > U1_matrix = [] > > print fields > print repr(fields) > print len(fields) And here's your problem - 'len' is now bound to the result of the previous call to len(text). Hint : Python's functions, classes and modules are objects too, and don't live in a distinct namespace. So _don't_ use builtin's types / functions / etc names as identifiers. HTH From davea at ieee.org Thu Jul 9 10:52:14 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 10:52:14 -0400 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <4A56041E.6020206@ieee.org> Nick wrote: > I've seen a lot of posts on this problem, but none seems to help. > Here is the code: > /code > > file = open(prefix1) > text = file.readlines() > len = len(text) > fields = text[1].split() > num_rows = int(fields[1]) > num_cols = int(fields[2]) > > U1_matrix = [] > > print fields > print repr(fields) > print len(fields) > > for line in text[2: num_rows+2]: > fields = line.split() > # print "fields", fields, line > for i in range(len(fields)): > fields[i] = float(fields[i]) > U1_matrix.append(fields) > > /*code > > prefix is a space/line delimited ascii file that represents a 2D > matrix. i'm trying to read in 2 matrices from different files, strip > away the header stuff and then take the dot product of the 2 > matrices. any help is much appreciated. > > thanks, > nick > > You have at least two problems with that code, one of which is causing your symptom. Both 'file' and 'len' are defined in the standard library, and shouldn't be redefined in your code. And your problem is that after you redefined 'len', you then tried to use it in its original meaning. Rename those two and you'll get further. And it would have saved lots of time for lots of people if you included sample data and the actual error message, marking where in your code it occurs. Once you know it's complaining about the len() call, it's not too hard to figure out that the problem was you rebound the len attribute from a function to an integer. Traceback (most recent call last): File "M:\Programming\Python\sources\dummy\echo2.py", line 21, in print len(fields) TypeError: 'int' object is not callable DaveA From jcd at sdf.lonestar.org Thu Jul 9 11:15:41 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Thu, 09 Jul 2009 11:15:41 -0400 Subject: count In-Reply-To: References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: <1247152541.3694.45.camel@aalcdl07> Bearophile wins! (This only times the loop itself. It doesn't check for __len__) summer:5 0:00:00.000051 bearophile:5 0:00:00.000009 summer:50 0:00:00.000030 bearophile:50 0:00:00.000013 summer:500 0:00:00.000077 bearophile:500 0:00:00.000053 summer:5000 0:00:00.000575 bearophile:5000 0:00:00.000473 summer:50000 0:00:00.005583 bearophile:50000 0:00:00.004625 summer:500000 0:00:00.055834 bearophile:500000 0:00:00.046137 summer:5000000 0:00:00.426734 bearophile:5000000 0:00:00.349573 summer:50000000 0:00:04.180920 bearophile:50000000 0:00:03.652311 summer:500000000 0:00:42.647885 bearophile: 500000000 0:00:35.190550 On Thu, 2009-07-09 at 04:04 -0700, Bearophile wrote: > Paul Rubin: > > print x, sum(1 for _ in g) > > Don't use that, use my function :-) If g has a __len__ you are wasting > time. And sum(1 ...) is (on my PC) slower. > > > J. Clifford Dyer: > > if __name__ == '__main__': > > test_func(summer, 10000000) > > test_func(tupler, 10000000) > > test_func(summer, 100000000) > > test_func(tupler, 100000000) > > Have you forgotten my function? > > Bye, > bearophile From ethan at stoneleaf.us Thu Jul 9 11:16:30 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 09 Jul 2009 08:16:30 -0700 Subject: tough-to-explain Python In-Reply-To: <007075aa$0$9711$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <007075aa$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A5609CE.70807@stoneleaf.us> Steven D'Aprano wrote: > > There is some evidence that 30-60% of people simply cannot learn to > program, no matter how you teach them: > > http://www.codinghorror.com/blog/archives/000635.html > http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ > > I'm sympathetic to the idea, but not entirely convinced. Perhaps the > problem isn't with the students, but with the teachers, and the > languages: > > http://www.csse.monash.edu.au/~damian/papers/PDF/SevenDeadlySins.pdf > > (My money is that it's a little of both.) > > Very interesting articles. Not suprising (to me, at least) -- everybody has their strengths and weaknesses. I will never be, and could never have been, any kind of sports person; I don't have the right personality to be a good people person; I don't have the knack for writing stories. Not everyone can do anything, or even many things. ~Ethan~ From fridrik at pyth.net Thu Jul 9 11:30:35 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 15:30:35 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> References: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> Message-ID: <391EDF07-7FCE-4DD2-B510-1EBA7A70D36A@pyth.net> Previously, I wrote: >> P.S. While this is a fairly obvious problem it's usually a good >> idea to post working code and a traceback when requesting help. Nick wrote: > thanks for spotting the obvious errors, its my 2nd day programming > python in about 3 years. I'm sorry, my saying it was obvious may have come off a little arrogant. My clumsily delivered point was that because it was a small snippet of code it didn't take much time to run through for someone who codes daily with Python. What you did there was a perfectly ordinary thing to do until experience teaches you how Python does things. :) > fridrick, code should be workable with the exception of the > errors...thats the whole program You're right, I failed to say it explicitely but I was referring to the input file. In some cases, albeit not this one, problems can exist in parsing gotchas. Regards, Fri?rik M?r From robert.kern at gmail.com Thu Jul 9 11:54:37 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 09 Jul 2009 10:54:37 -0500 Subject: PyQt GUI In-Reply-To: <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> Message-ID: On 2009-07-09 01:27, Helvin wrote: > On Jul 9, 11:29 am, Robert Kern wrote: >> On 2009-07-08 18:10, Helvin wrote: >> >>> Thanks for the fast replies! I will look into how to use VTK now. >>> Where would I find VTK's explicit support for PyQt? >> Wrapping/Python/vtk/qt4/ in the VTK sources. >> >>> Because I have installed VTK (using its installer) and pyVTK (using >>> its setup.py file), but how do I actually use it in my code? According >>> to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., >>> I have tried 'import vtk', but python can't find the vtk module. >> Then you have not installed VTK's Python bindings correctly. Note that pyVTK is >> just a library for manipulating VTK files. The VTK Python bindings are part of >> VTK's distribution itself. Exactly how did you install VTK? Did you compile it >> yourself? > > You mean, when I download VTK, the VTK Python bindings come with it? > I downloaded VTK from:http://vtk.org/files/release/5.4/ Exactly which file did you download? I don't think the vtk-5.4.x-win32.exe installers have the Python bindings. You will need to build VTK from sources. -- 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 psimon at sonic.net Thu Jul 9 12:02:34 2009 From: psimon at sonic.net (Paul Simon) Date: Thu, 9 Jul 2009 09:02:34 -0700 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> Message-ID: <4a5615ba$0$95523$742ec2ed@news.sonic.net> "Peter Otten" <__peter__ at web.de> wrote in message news:h3481q$d95$00$1 at news.t-online.com... > Paul Simon wrote: > >> "Chris Rebert" wrote in message >> news:mailman.2863.1247095339.8015.python-list at python.org... >> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>> I have the "tkinter" problem and need some assistance to straighten it >>> out. >>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >>> >"step >>> 1" and cannot import "_tkinter." I do not have that file on my computer, >>> but >>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>> This python stuff is great, but the documentation frequently >>> feels like it is just a bit out of my grasp. I realize that all of this >>> is free but I understand the instructions on the web page to repair only >>> to the >>> point of confusion. I'm not an expert. How do I modify my python >>> configuration? Is there a file that needs to be edited? Which setup.py >>> file >>> do I use? Make? or python setup.py build and python setup.py install? >>> Thanks. I appreciate your help. >> >> - How did you install Python? >> - What Linux distro are you using? >> >> Cheers, >> Chris >> http://blog.rebertia.com >> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure >> exactly how I installed Python. Originally I had installed 2.5 from RPM >> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >> something from python.org and installed. Not sure if it was tarball or >> zip file. > > Zip or tar doesn't matter, you are installing "from source". > > Python has to find the necessary include files for tcl/tk. These are in > separate packages that you have to install before you invoke Python's > configure script. > > I don't know what they are called on your system -- look for tk-dev.rpm, > tcl-dev.rpm or similar. > > You may run into the same problem with other modules like readline. > > Peter > Thank you Peter. I understand what you are saying but don't know how to do it. Although I installed from source, I followed a "cookbook" recipe. Could you tell me what files to execute, where they might be, and file arguments? I'm just ignorant, not stupid. ;-). Paul From nleioatt at gmail.com Thu Jul 9 12:04:58 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 9 Jul 2009 09:04:58 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" References: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> Message-ID: no problem, i understand, i haven't coded anything in literally 2 years, but it was a simple and pretty obvious mistake. thanks for all your help, nick On Jul 9, 11:30 am, Fri?rik M?r J?nsson wrote: > Previously, I wrote: > >> P.S. While this is a fairly obvious problem it's usually a good > >> idea to post working code and a traceback when requesting help. > Nick wrote: > > thanks for spotting the obvious errors, its my 2nd day programming > > python in about 3 years. > > I'm sorry, my saying it was obvious may have come off a little > arrogant. My clumsily delivered point was that because it was a small > snippet of code it didn't take much time to run through for someone > who codes daily with Python. What you did there was a perfectly > ordinary thing to do until experience teaches you how Python does > things. :) > > > fridrick, code should be workable with the exception of the > > errors...thats the whole program > > You're right, I failed to say it explicitely but I was referring to > the input file. In some cases, albeit not this one, problems can > exist in parsing gotchas. > > Regards, > Fri?rik M?r From tkermode at gmail.com Thu Jul 9 12:06:45 2009 From: tkermode at gmail.com (Tom Kermode) Date: Thu, 9 Jul 2009 17:06:45 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Hi, Do you know a good way to avoid running into this problem? It makes sense to suggest not calling variables the same names as built-in functions, but that's hard for a new python programmer who doesn't already know what all the built-in functions are. Over time a programmer will learn which names to avoid, but it's a bit of a pitfall early on. Cheers, Tom 2009/7/9 Richard Brodie : > > "Nick" wrote in message > news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f at h18g2000yqj.googlegroups.com... > >> file = open(prefix1) >> text = file.readlines() >> len = len(text) > > You have redefined two built-in functions "file" and "len" in the first three lines. > This is usually considered poor practice. Stick to meaningless variable names, > it's safer (only joking). > > TypeError: 'int' object is not callable". This means that something you thought > was a function is in fact an integer. It's helpful to post/look at the line number of > the error; "how is this line failing", is much easier to answer than > "how is my program failing". > > print len(fields) > > Here len is an integer, because you redefined it in line 3. I'm guessing this is the > problem. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.kiloday.com http://www.fourstopspast.com From R.Brodie at rl.ac.uk Thu Jul 9 12:30:42 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Thu, 9 Jul 2009 17:30:42 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: "Tom Kermode" wrote in message news:mailman.2903.1247155607.8015.python-list at python.org... > Do you know a good way to avoid running into this problem? It > makes sense to suggest not calling variables the same names as > built-in functions, but that's hard for a new python programmer who > doesn't already know what all the built-in functions are. No, but not redefining the ones you actually use is a good start. Learning to understand the traceback is the more important lesson, IMHO. It takes a while to tune into what error messages are trying to tell you; even when you stop making newbie mistakes, you're going to have to deal with runtime errors from time to time. From m at n.d Thu Jul 9 12:32:41 2009 From: m at n.d (Broken) Date: Thu, 9 Jul 2009 18:32:41 +0200 Subject: send keys to focused window Message-ID: Hi, I am new to Python, and I'm miserably failing to send specific keys to (say) notepad. Here is the scenario: I have notepad open. My python script is running in the background. When I press ALT+a I want to intercept the keys and send "?"(ASCII code: ALT+0228) instead. OS: Windows 7 Libraries used: pyHook, SendKeys, pyWin32 Here is my code: ############################### # -*- coding: cp1252 -*- import pythoncom, pyHook from SendKeys import SendKeys def OnKeyboardEvent(event): if event.Alt == 32: #alt pressed if event.Ascii == 97: #a pressed SendKeys("?") return False #don't pass to other handlers # return True to pass the event to other handlers return True # create a hook manager hm = pyHook.HookManager() # watch for all mouse events hm.KeyDown = OnKeyboardEvent # set the hook hm.HookKeyboard() # wait forever pythoncom.PumpMessages() ############################### It's largely copied from the pyhook example. My problem is, I need to somehow disable ALT while sonding the key. Please help :) From h at realh.co.uk Thu Jul 9 12:33:43 2009 From: h at realh.co.uk (Tony Houghton) Date: Thu, 9 Jul 2009 17:33:43 +0100 Subject: Running a script to build docs from setup.py Message-ID: <20090709173343.1784710d@realh.co.uk> I want to write a setup.py script, using distutils, for a python library called ROX-Lib2 (package name "rox"). The library includes a script to build HTML documentation from the pydoc strings. I'd like to run that script from setup.py but I don't know the best way to do that. I've looked through the manual but I can't find any hooks in distutils for generating files at install time other than extension modules and .pyc files. Should I just run the script from somewhere in my setup.py before calling distutils' setup function? -- TH * http://www.realh.co.uk From fridrik at pyth.net Thu Jul 9 12:40:47 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 16:40:47 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Tom Kermode wrote: > Do you know a good way to avoid running into this problem? It > makes sense to suggest not calling variables the same names as > built-in functions, but that's hard for a new python programmer who > doesn't already know what all the built-in functions are. One way is using a code checker like PyChecker[1]. This neat software for finding bugs will check for lots of other pitfalls too, but you can filter it down to what you need if you're only interested in this one. I don't use an IDE, but this would seem like something for an IDE[2] to support if you're into that kind of magic. Regards, Fri?rik M?r [1] http://pychecker.sourceforge.net/ [2] http://wiki.python.org/moin/IntegratedDevelopmentEnvironments From __peter__ at web.de Thu Jul 9 13:00:37 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Jul 2009 19:00:37 +0200 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > > "Peter Otten" <__peter__ at web.de> wrote in message > news:h3481q$d95$00$1 at news.t-online.com... >> Paul Simon wrote: >> >>> "Chris Rebert" wrote in message >>> news:mailman.2863.1247095339.8015.python-list at python.org... >>> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>>> I have the "tkinter" problem and need some assistance to straighten it >>>> out. >>>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >>>> >"step >>>> 1" and cannot import "_tkinter." I do not have that file on my >>>> computer, but >>>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>>> This python stuff is great, but the documentation frequently >>>> feels like it is just a bit out of my grasp. I realize that all of this >>>> is free but I understand the instructions on the web page to repair >>>> only to the >>>> point of confusion. I'm not an expert. How do I modify my python >>>> configuration? Is there a file that needs to be edited? Which setup.py >>>> file >>>> do I use? Make? or python setup.py build and python setup.py install? >>>> Thanks. I appreciate your help. >>> >>> - How did you install Python? >>> - What Linux distro are you using? >>> >>> Cheers, >>> Chris >>> http://blog.rebertia.com >>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not >>> sure >>> exactly how I installed Python. Originally I had installed 2.5 from RPM >>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>> something from python.org and installed. Not sure if it was tarball or >>> zip file. >> >> Zip or tar doesn't matter, you are installing "from source". >> >> Python has to find the necessary include files for tcl/tk. These are in >> separate packages that you have to install before you invoke Python's >> configure script. >> >> I don't know what they are called on your system -- look for tk-dev.rpm, >> tcl-dev.rpm or similar. >> >> You may run into the same problem with other modules like readline. >> >> Peter >> > > Thank you Peter. I understand what you are saying but don't know how to > do > it. Although I installed from source, I followed a "cookbook" recipe. > Could you tell me what files to execute, where they might be, and file > arguments? I'm just ignorant, not stupid. ;-). > > Paul Once you have the necessary development packages for tcl/tk just go into the directory where you unzipped the source and ./configure make sudo make altinstall Unfortunately I don't know the names of these packages nor how to install them, and Google didn't turn up anything useful. If you don't get any additional answers here you may try a Mandriva forum since this is not a question that requires python knowlegde. Peter From duane.kaufman at gmail.com Thu Jul 9 13:14:29 2009 From: duane.kaufman at gmail.com (TheSeeker) Date: Thu, 9 Jul 2009 10:14:29 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted Message-ID: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Hi, I am embarking on teaching myself Microsoft UI Automation using Python as the scripting language. I have asked some questions in the IronPython users group, but have yet to get a response, so I thought I would broaden the audience by asking here. Specifically, I have a WinForms application I will be wanting to automate. Does anyone have some Python examples of driving Microsoft UI Automation they could share with me to get me started? The structure of the UI automation classes etc. seem quite convoluted, and I am having difficulty getting my brain wrapped around it. Alternatives to Microsoft's UI Automation are welcome too, but I have tried using winguiauto and watsup (along with AutoIt), and there seems to be severe limitations when using these tools with WinForm applications. Thanks in advance, Duane From joshua at joshuakugler.com Thu Jul 9 13:17:25 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Thu, 09 Jul 2009 09:17:25 -0800 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: Joshua Kugler wrote: > Sorry about that...since pysqlite and APSW are both discusses on the > pysqlite list, I had made an incorrect assumption. Oops. "are both discusses?" Yeef, I must have been out of it. Discussed, thank you. :) j From usernet at ilthio.net Thu Jul 9 13:18:49 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 09 Jul 2009 17:18:49 GMT Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: On 2009-07-09, TheSeeker wrote: > Specifically, I have a WinForms application I will be wanting to > automate. Does anyone have some Python examples of driving Microsoft > UI Automation they could share with me to get me started? The > structure of the UI automation classes etc. seem quite convoluted, and > I am having difficulty getting my brain wrapped around it. If you find a way to work through the UI using the keyboard (tabs, etc); then, you can send it keyboard commands using WScript.WshShell.SendKeys(): http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx From steve at REMOVE-THIS-cybersource.com.au Thu Jul 9 13:20:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 Jul 2009 17:20:14 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <007201bc$0$9710$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > The core abstractions of [mechanical] computation are just not that > complicated. You can teach them to anybody in about a half an hour, > drunk. I have. That's *easy*. Anyone can teach the most complicated and abstract principles of any topic at all drunk. What's hard is doing it sober. http://stackoverflow.com/questions/63668/confessions-of-your-worst-wtf- moment-what-not-to-do/350267#350267 or http://tinyurl.com/lur784 You'll excuse my skepticism about all these claims about how anyone can program, how easy it is to teach the fundamentals of Turing Machines and functional programming to anybody at all. Prove it. Where are your peer- reviewed studies demonstrating such successes on randomly chosen people, not self-selected motivated, higher-than-average intelligence students? In the real world, human beings are prone to serious cognitive biases and errors. Our brains are buggy. Take any list of reasoning fallacies, and you'll find the majority of people have difficulty avoid them. The first struggle is to get them to even accept that they *are* fallacies, but even once they have intellectually accepted it, getting them to live up to that knowledge in practice is much, much harder. In fact I'd go further and say that *every single person*, no matter how intelligent and well-educated, will fall for cognitive biases and fallacious reasoning on a regular basis. http://en.wikipedia.org/wiki/Cognitive_bias http://en.wikipedia.org/wiki/Fallacy -- Steven From lie.1296 at gmail.com Thu Jul 9 13:30:02 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 17:30:02 GMT Subject: Cleaning up after failing to contructing objects In-Reply-To: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: brasse wrote: > Hello! > I have been thinking about how write exception safe constructors in > Python. By exception safe I mean a constructor that does not leak > resources when an exception is raised within it. The following is an > example of one possible way to do it: First, your automatic cleaning Bar() silently pass an unitialized Bar() into the calling code. When a code fails, it should fail as loud as possible. Bar() should raises an Exception and the calling code should turn into something like: try: bar = Bar() except Exception, e: print 'bar failed to construct' And about the cleaning up, how about: class CleanWrap(object): def __init__(self, cls, cleanup): self.cls = cls self.cleanup = cleanup def __call__(self, *args, **kargs): try: return self.cls(*args, **kargs) except: self.cleanup() raise class Bar(object): def __init__(self): CleanWrappedFoo = CleanWrap(Foo, self.close) self.a = CleanWrappedFoo('a') self.b = CleanWrappedFoo('b', fail=True) def close(self): if hasattr(self, 'a'): self.a.close() if hasattr(self, 'b'): self.b.close() try: bar = Bar() except Exception, e: print 'Bar() failed to construct' ========== My next attempt is pretty neat, using a totally different approach, see the docstring for details: class Foo1(object): def __init__(self, name, fail=False): self.name = name cls_name = self.__class__.__name__ if not fail: print '%s.__init__(%s)' % (cls_name, self.name) else: print '%s.__init__(%s), FAIL' % (cls_name, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) class Foo2(object): def __init__(self, name, fail=False): self.name = name cls_name = self.__class__.__name__ if not fail: print '%s.__init__(%s)' % (cls_name, self.name) else: print '%s.__init__(%s), FAIL' % (cls_name, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) class CleanList(object): ''' Each CleanList() instance is rigged so that if exceptions happen in the same CleanList() instance's wrapped class, all objects created from the wrapped classes in the same CleanList() instance will be cleaned (see "Usage" for much better explanation). Usage: >>> cleaner = CleanList() >>> othercleaner = CleanList() >>> F = cleaner(F, F.close) >>> G = cleaner(G, G.close) >>> H = othercleaner(H, H.close) >>> a = F() >>> b = F() >>> c = G() >>> d = H() >>> cleaner.cleanall() cleaner.cleanall() will clean a, b, and c but not d exceptions in (F|G|H).__init__ will trigger cleaner.cleanall() Can be subclassed if you want to override the conditions that determines the triggering of cleanups ''' def wrap(self, cls): ''' Wrapper factory that customizes Wrapper's subclass ''' class Wrapper(cls): ''' Wraps the class' __init__ with cleanup guard. Subclasses cls to simulate cls as close as possible ''' # change __class__ to make printing prettier # e.g. Foo1.__init__ instead of Wrapper.__init__ # probably should be removed # I'm not sure of the side effects of changing __class__ __class__ = cls def __init__(self_in, *args, **kargs): try: sup = super(Wrapper, self_in) ret = sup.__init__(*args, **kargs) except: self.cleanall() raise else: self.add_to_list(cls, self_in) return ret return Wrapper def __init__(self): self.cleaners = {} def __call__(self, cls, cleanup): ''' wraps the class constructor ''' # cleanup, []: # cleanup is the function called to clean # [] is the object list for that `cleanup` function # may not be the best data structure, but it works... self.cleaners[cls] = cleanup, [] return self.wrap(cls) def cleanall(self): ''' clean all objects ''' for cleaner, insts in self.cleaners.values(): for inst in insts: cleaner(inst) def add_to_list(self, cls, inst): ''' add objects to the cleanup list ''' self.cleaners[cls][1].append(inst) class Bar(object): def __init__(self): clist = CleanList() otherclist = CleanList() CleanFoo1 = clist(Foo1, Foo1.close) CleanFoo2 = clist(Foo2, Foo2.close) OtherCleanFoo1 = otherclist(Foo1, Foo1.close) self.a = CleanFoo1('a') self.b = CleanFoo2('b') # self.c should not be close()ed self.c = OtherCleanFoo1('c') self.d = CleanFoo1('d', fail=True) self.e = CleanFoo2('e') class Car(object): def __init__(self): Clean = CleanList() CleanFoo1 = Clean(Foo1, Foo1.close) CleanFoo2 = Clean(Foo2, Foo2.close) self.a = CleanFoo1('a') self.b = CleanFoo2('b') self.c = CleanFoo1('c') self.d = CleanFoo2('d') try: bar = Car() except Exception, e: print e print 'Car() failed to construct' print try: bar = Bar() except Exception, e: print e print 'Bar() failed to construct' From sebastian.schabe at gmx.de Thu Jul 9 13:34:59 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Thu, 09 Jul 2009 19:34:59 +0200 Subject: Concatenating images (numpy arrays), but they look like HSV images Message-ID: <4a5629ba$1@news.fhg.de> Hello everybody, I want to concatenate 2 numpy array which in fact are RGB images: def concat_images(im1,im2): rows1 = im1.shape[0] rows2 = im2.shape[0] if rows1 < rows2: im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1],3), int)), axis=0) elif rows1 > rows2: im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1],3), int)), axis=0) return concatenate((im1,im2), axis=1) It's all working fine, except that the images when showing with pylab are somewhat interpreted as HSV images as it looks. The function zeros() must be responsible for that circumstance, because when the arrays have the same shape and are concatenated they appear as horizontally concatenated images as I expected. Can someone help me with that? Thanks a lot, Basti From lie.1296 at gmail.com Thu Jul 9 13:35:19 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 17:35:19 GMT Subject: property using a classmethod In-Reply-To: <4a55e8e0$0$8041$426a34cc@news.free.fr> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> <4a55e8e0$0$8041$426a34cc@news.free.fr> Message-ID: Bruno Desthuilliers wrote: > Lie Ryan a ?crit : >> Emanuele D'Arrigo wrote: > (snip) >>> Ultimately all I want is a non-callable class-level attribute >>> MyClass.myProperty that gives the result of MyClass.myClassMethod(). >> >> This works like what you seem to want (it's ugly): > > Ugly, indeed. And an extreme case of arbitrary overcomplexification too :-/ > > (snip rube goldberg code) > Can't think of anything simpler than that without meddling with descriptor. I'm not even sure descriptor can help here as it seems descriptor needs an instance? (I've just skimmed it, so I may be wrong) The ugliness of the code hints to two possible reasons: - there should be a better way - if there isn't an easier way, then something is wrong the class' design From duane.kaufman at gmail.com Thu Jul 9 13:36:50 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 10:36:50 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> On Jul 9, 12:18?pm, Tim Harig wrote: > On 2009-07-09, TheSeeker wrote: > > > Specifically, I have a WinForms application I will be wanting to > > automate. Does anyone have some Python examples of driving Microsoft > > UI Automation they could share with me to get me started? The > > structure of the UI automation classes etc. seem quite convoluted, and > > I am having difficulty getting my brain wrapped around it. > > If you find a way to work through the UI using the keyboard (tabs, etc); > then, you can send it keyboard commands using WScript.WshShell.SendKeys(): > > http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx Thanks for the link. Unfortunately, I need to be able to find out the contents of a few text-boxes as well, so SendKeys isn't all I need. Thanks again, Duane From tjreedy at udel.edu Thu Jul 9 13:40:01 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jul 2009 13:40:01 -0400 Subject: can i write a assemly language programs in python In-Reply-To: <4A55F392.3080404@ieee.org> References: <4A55F392.3080404@ieee.org> Message-ID: Dave Angel wrote: > m.reddy prasad reddy wrote: >> can any one tell me how to write assembly language programs in >> python...if >> no is there any other way to write the programs in python >> >> Reddi prasad reddy >> ph.no:09958083797 >> >> > Assembly language is a different programming language than Python. You > can use both in the same process, much in the same way you can use C or > C++ with Python. In fact, some of the system DLL's are written (partly) > in assembler, though mostly in C or C++. It is possible that he meant how to write assembly *with* python. Or how to translate python to assembly. As it turns out, CPython translates Python to byte code and has a dis (assembly) module that produces very nice assembly code ;-) So that is one answer to his question. > You'll need to tell us what your real goal is. Definitely. tjr From lie.1296 at gmail.com Thu Jul 9 13:40:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 17:40:28 GMT Subject: older pythons In-Reply-To: <4a55e1fb$0$1116$4fafbaef@reader3.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> <4a55e1fb$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Lie Ryan wrote: >> superpollo wrote: >> >>> Lie Ryan wrote: >>> >>> >>>> AFAIK, no major linux distributions have officially ported to python >>>> 3.x. >>> >>> http://packages.debian.org/experimental/python3.1 >>> >> Note the word "experimental" > > i noticed. isn't experimental official? i thought it was... Official as in "official release"? From Scott.Daniels at Acm.Org Thu Jul 9 13:40:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 09 Jul 2009 10:40:58 -0700 Subject: property using a classmethod In-Reply-To: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: <3dKdndiWOZRutMvXnZ2dnUVZ_gFi4p2d@pdx.net> Emanuele D'Arrigo wrote: > class MyClass(object): > @classmethod > def myClassMethod(self): > print "ham" > myProperty = property(myClassMethod, None, None) > > ... doesn't work and returns a TypeError: .... So, how do I do this? > Ultimately all I want is a non-callable class-level attribute > MyClass.myProperty that gives the result of MyClass.myClassMethod(). properties affect instances, and classes are instances of types. What you want is a new metaclass: class MyType(type): @property def demo(class_): return class_.a + 3 class MyClass(object): __metaclass__ = MyType a = 5 print MyClass.a, MyClass.demo --Scott David Daniels Scott.Daniels at Acm.Org From rhodri at wildebst.demon.co.uk Thu Jul 9 13:41:59 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 09 Jul 2009 18:41:59 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: On Thu, 09 Jul 2009 17:06:45 +0100, Tom Kermode wrote: > Hi, > > Do you know a good way to avoid running into this problem? It > makes sense to suggest not calling variables the same names as > built-in functions, but that's hard for a new python programmer who > doesn't already know what all the built-in functions are. Over time a > programmer will learn which names to avoid, but it's a bit of a > pitfall early on. It's only really a pitfall if you try to use the built-in after you've redefined it. That's the thing to keep an eye open for. -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Thu Jul 9 13:42:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 09 Jul 2009 19:42:56 +0200 Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Lacrima (L) wrote: >L> Thank you for really useful and detailed explanation. Now I can test >L> my code using Emacs. >L> But I think it works for me in a little bit different way. >L> My file contains only the print 'hello world'. >L> If I have no python shell running, then: >L> a) C-c RET opens *Python Output* buffer without any output and with >L> 'Shell command succeeded with no output' message at the bottom >L> b) C-c C-c opens *Python Output* with appropriate 'hello world' >L> message. >L> Then I run python shell with C-c ! command. And: >L> a) C-c RET opens *Python* buffer and prints 'hello world' in the >L> python shell >L> b) C-c C-c gives the same result. Which version of python-mode.el do you have? (Variable py-version) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From usernet at ilthio.net Thu Jul 9 13:45:13 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 09 Jul 2009 17:45:13 GMT Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On 2009-07-09, DuaneKaufman wrote: > On Jul 9, 12:18?pm, Tim Harig wrote: >> On 2009-07-09, TheSeeker wrote: >> > Specifically, I have a WinForms application I will be wanting to >> > automate. Does anyone have some Python examples of driving Microsoft >> > UI Automation they could share with me to get me started? The >> > structure of the UI automation classes etc. seem quite convoluted, and >> > I am having difficulty getting my brain wrapped around it. >> If you find a way to work through the UI using the keyboard (tabs, etc); >> then, you can send it keyboard commands using WScript.WshShell.SendKeys(): >> http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx > Unfortunately, I need to be able to find out the contents of a few > text-boxes as well, > so SendKeys isn't all I need. Then you will need some way to import your application as an object so that you can gain access to the methods and properties directly. Using IronPython you should be able to access anyting in the GAC. If you register your UI componets then you should be able to access them. If not, or using cpython, you might be able to do something like: http://msdn.microsoft.com/en-us/library/fh1h056h(VS.71).aspx to get a mashalled COM object. From black.linen99 at gmail.com Thu Jul 9 13:46:47 2009 From: black.linen99 at gmail.com (Alex Rosslyn) Date: Thu, 9 Jul 2009 10:46:47 -0700 (PDT) Subject: Colour of output text Message-ID: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Hi, I would like to learn a way of changing the colour of a particular part of the output text. I've tried the following: import os os.system("color 17") print "This should be white on blue" But that command changes the colour of ALL the text and the whole background. What i'm trying to do is simple: Change a single part (A letter, a word, a phrase) of the output text. Is there a way of doing this? Thanks in advance, ~~A.Rosslyn From tjreedy at udel.edu Thu Jul 9 13:49:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jul 2009 13:49:36 -0400 Subject: tkinter problem In-Reply-To: <4a5615ba$0$95523$742ec2ed@news.sonic.net> References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > "Peter Otten" <__peter__ at web.de> wrote in message > news:h3481q$d95$00$1 at news.t-online.com... >> Paul Simon wrote: >> >>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure >>> exactly how I installed Python. Originally I had installed 2.5 from RPM >>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>> something from python.org and installed. Not sure if it was tarball or >>> zip file. >> Zip or tar doesn't matter, you are installing "from source". >> >> Python has to find the necessary include files for tcl/tk. These are in >> separate packages that you have to install before you invoke Python's >> configure script. >> >> I don't know what they are called on your system -- look for tk-dev.rpm, >> tcl-dev.rpm or similar. >> >> You may run into the same problem with other modules like readline. >> >> Peter >> > > Thank you Peter. I understand what you are saying but don't know how to do > it. Although I installed from source, I followed a "cookbook" recipe. > Could you tell me what files to execute, where they might be, and file > arguments? I'm just ignorant, not stupid. ;-). Is there a Mandriva list where you can ask such distribution-specific questions? From usernet at ilthio.net Thu Jul 9 13:53:12 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 09 Jul 2009 17:53:12 GMT Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On 2009-07-09, Alex Rosslyn wrote: > I would like to learn a way of changing the colour of a particular > part of the output text. I've tried the following http://catb.org/esr/faqs/smart-questions.html > import os > os.system("color 17") > print "This should be white on blue" I assume that you are on Windows? > But that command changes the colour of ALL the text and the whole > background. What i'm trying to do is simple: Change a single part (A > letter, a word, a phrase) of the output text. If you are on Windows, then there is an API accessing the Windows Console: http://msdn.microsoft.com/en-us/library/ms682010(VS.85).aspx On Unix operating systems this would be done through the curses interface: http://docs.python.org/library/curses.html From robert.kern at gmail.com Thu Jul 9 14:08:08 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 09 Jul 2009 13:08:08 -0500 Subject: Concatenating images (numpy arrays), but they look like HSV images In-Reply-To: <4a5629ba$1@news.fhg.de> References: <4a5629ba$1@news.fhg.de> Message-ID: On 2009-07-09 12:34, Sebastian Schabe wrote: > Hello everybody, > > I want to concatenate 2 numpy array which in fact are RGB images: > > def concat_images(im1,im2): > rows1 = im1.shape[0] > rows2 = im2.shape[0] > > if rows1 < rows2: > im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1],3), int)), axis=0) > elif rows1 > rows2: > im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1],3), int)), axis=0) > > return concatenate((im1,im2), axis=1) > > It's all working fine, except that the images when showing with pylab > are somewhat interpreted as HSV images as it looks. The function zeros() > must be responsible for that circumstance, because when the arrays have > the same shape and are concatenated they appear as horizontally > concatenated images as I expected. > > Can someone help me with that? Ask on the matplotlib mailing list. https://lists.sourceforge.net/lists/listinfo/matplotlib-users Probably, you need to use zeros(..., dtype=uint8). When you use dtype=int, that will result in dtype=int arrays. I suspect that matplotlib is then interpreting that to mean that you want it to treat the input as scalar data (which it will pass through a colormap) rather than an RGB image. -- 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 duane.kaufman at gmail.com Thu Jul 9 14:09:03 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 11:09:03 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 12:45?pm, Tim Harig wrote: > On 2009-07-09, DuaneKaufman wrote: > > > On Jul 9, 12:18?pm, Tim Harig wrote: > >> On 2009-07-09, TheSeeker wrote: > >> > Specifically, I have a WinForms application I will be wanting to > >> > automate. Does anyone have some Python examples of driving Microsoft > >> > UI Automation they could share with me to get me started? The > >> > structure of the UI automation classes etc. seem quite convoluted, and > >> > I am having difficulty getting my brain wrapped around it. > >> If you find a way to work through the UI using the keyboard (tabs, etc); > >> then, you can send it keyboard commands using WScript.WshShell.SendKeys(): > >>http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx > > Unfortunately, I need to be able to find out the contents of a few > > text-boxes as well, > > so SendKeys isn't all I need. > > Then you will need some way to import your application as an object so that > you can gain access to the methods and properties directly. ?Using > IronPython you should be able to access anyting in the GAC. ?If you > register your UI componets then you should be able to access them. ?If not, > or using cpython, you might be able to do something like: > > http://msdn.microsoft.com/en-us/library/fh1h056h(VS.71).aspx > > to get a mashalled COM object. Thanks again Tim. I believe I probably mis-communicated my requirements (or at least, was not explicit enough) The application I wish to interact with is not my own, but an ERP system GUI front-end. With MS utilities like UISpy and the like, I can 'see' the controls in the application, but I do not seem to be able to manipulate them programatically, and I believe it us simply due to my not understanding of the UI Automation API. Hence, my request for example code to boot-strap myself. Thanks, Duane From steve at REMOVE-THIS-cybersource.com.au Thu Jul 9 14:10:16 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 Jul 2009 18:10:16 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <00720d76$0$9710$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: >> persistent idea "out there" that programming is a very accessible >> skill, like cooking or gardening, anyone can do it, and even profit >> from it, monetarily or otherwise, etc., and to some extent I am > > Programming is not like any other human activity. In practice? In principle? Programming in principle is not the same as it is performed in practice. But in either case, programming requires both the logical reasoning of mathematics and the creativity of the arts. Funnily enough, mathematicians will tell you that mathematics requires the same, and so will the best artists. I think mathematicians, engineers, artists, even great chefs, will pour scorn on your claim that programming is not like any other human activity. [...] > He talks about how "when all is said and done, the only thing computers > can do for us is to manipulate symbols and produce results of such > manipulations" and he emphasises the "uninterpreted" nature of > mechanical symbol manipulation, i.e. that the machine is doing it > mindlessly. "Manipulate symbols" is so abstract as to be pointless. By that reasoning, I can build a "computer" consisting of a box open at the top. I represent a symbol by an object (say, a helium-filled balloon, or a stone), instead of a pattern of bits. I manipulate the symbol by holding the object over the box and letting go. If it flies up into the sky, that represents the symbol "Love is War", if it falls into the box, it represents the symbol "Strength is Blue", and if it just floats there, it represents "Cheddar Cheese". This is a deterministic, analog computer which manipulates symbols. Great. And utterly, utterly useless. So what is my computer lacking that real computers have? When you have answered that question, you'll see why Dijkstra's claim is under-specified. > Dijkstra[1]: "It is true that the student that has never manipulated > uninterpreted formulae quickly realizes that he is confronted with > something totally unlike anything he has ever seen before. But > fortunately, the rules of manipulation are in this case so few and > simple that very soon thereafter he makes the exciting discovery that he > is beginning to master the use of a tool that, in all its simplicity, > gives him a power that far surpasses his wildest dreams." [1] What is an uninterpreted formula? If you don't interpret it, how can you distinguish it from random noise? >> but maybe this idea is not entirely true... Maybe, to get past the >> most amateurish level, one has to, one way or another, come >> face-to-face with bits, compilers, algorithms, and all the rest that >> real computer scientists learn about in their formal training... >> >> kj > > If you're never exposed to that constellation of concepts that underpins > "mechanical symbol manipulation" you are adrift in a sea ("c", ha ha) of > abstractions. > > However, if you /are/ exposed to the "so few and simple" rules of > manipulation the gates (no pun intended) to the kingdom are thrown wide. What nonsense. The keys to the kingdom are the abstractions. Here's an exercise for you, if you dare. It's not that difficult to remove the abstraction from integer addition, to explain it in terms of bit manipulation. If you prefer, you can write a Turing Machine instead. Now try doing the same thing for Quicksort. Don't worry, we'll wait. Once you've done that, let's see you implement a practical, scalable, non- toy webserver as a Turing Machine. No, it's not the fundamental operations that open the doors to the kingdom. It's the abstractions. The history of computing is to gain more and more power by leveraging better and better abstractions. > On Jul 8, 9:10 am, Steven D'Aprano cybersource.com.au> wrote: >> There is some evidence that 30-60% of people simply cannot learn to >> program, no matter how you teach them: >> >> http://www.codinghorror.com/blog/archives/000635.html >> http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ ... > I don't buy it: I'm not entirely convinced myself. It's plausible -- not everyone has got what it takes to be musically talented, or a great athlete, or a skilled surgeon, or a charismatic leader. Why should programming be the one high- level intellectual skill that everybody is good at? Seems mighty implausible to me. But I'm not sure that the distribution of skill is *fundamentally* bi- modal. Maybe it is, maybe it isn't. > I believe strongly that any normal person can learn to > program, to manipulate symbols to create formulae that guide the machine > in its uninterpreted symbol manipulation. Most people don't manipulate abstract symbols *at all* -- even something as simple as: "if x is to z as half y is to twice z, then x is the same as ____ y" (fill in the blank) will perplex most people. > I think that "first hump" people can become "second hump" people but > that it requires teaching them the foundations first, not confronting > them with such incredible novelties as "a = b" and saying in effect, > "here you go buddy, sink or swim." The weakness of the paper is not that "a=b" is a novelty, but that it's dangerously familiar. All their students will have already seen "a=b" in the context of many years of mathematics. But that's not what it means in this context. So the risk is, they will be interpreting the symbols "a=b" in terms of mathematical equality, and getting confused. > Quoting Dijkstra again [1]: "Before we part, I would like to invite you > to consider the following way of doing justice to computing's radical > novelty in an introductory programming course. > > "On the one hand, we teach what looks like the predicate calculus, but > we do it very differently from the philosophers. In order to train the > novice programmer in the manipulation of uninterpreted formulae, we > teach it more as boolean algebra, familiarizing the student with all > algebraic properties of the logical connectives. To further sever the > links to intuition, we rename the values {true, false} of the boolean > domain as {black, white}. This is supposed to be a good thing? Oh my. It must be nice in that ivory tower, never having to deal with anyone who hasn't already been winnowed by years of study and self- selection before taking on a comp sci course. I don't think Dijkstra would have taken that suggestion seriously if he had to teach school kids instead of college adults. ... > (Did you read that last paragraph and think, "Well how the heck else are > you supposed to program a computer if not in a computer language?"? If > so, well, that is kind of my point.) No. What I thought was, how the hell are you supposed to manipulate symbols without a language to describe what sort of manipulations you can do? >> > Maybe, to >> > get past the most amateurish level, one has to, one way or another, >> > come face-to-face with bits, compilers, algorithms, and all the rest >> > that real computer scientists learn about in their formal training... >> >> The "No True Scotsman" fallacy. >> >> There's nothing amateurish about building software applications that >> work, with well-designed interfaces and a minimum of bugs, even if >> you've never heard of Turing Machines. >> >> -- >> Steven > > I beg to differ. I recall a conversation with a co-worker who had > "learned" to program using PHP. Another co-worker and I were trying to > convince him that there was a good reason to differentiate between hash > tables and arrays. He didn't even know that they were different > "things". I shall manfully resist the urge to make sarcastic comments about ignorant PHP developers, and instead refer you to my earlier reply to you about cognitive biases. For extra points, can you identify all the fallacious reasoning in that paragraph of yours? > I remember telling him, "between the metal and the desktop there is > nothing but layers of abstraction. We use different names precisely > because of different behaviours." But those abstractions just get in the way, according to you. He should be programming in the bare metal, doing pure symbol manipulation without language. > He made "well-designed interfaces", but "amateurish" is about the nicest > thing I would have called him. Well, perhaps this is a failure of his profession, in that there isn't enough specialisation. If he is skilled with making interfaces, and unskilled with programming the backend, then he should work where his strength lies, instead of being insulted by somebody who has an entirely unjustified opinion about what "real programming" is. If programming is symbol manipulation, then you should remember that the user interface is also symbol manipulation, and it is a MUCH harder problem than databases, sorting, searching, and all the other problems you learn about in academia. The user interface has to communicate over a rich but noisy channel using multiple under-specified protocols to a couple of pounds of meat which processes information using buggy heuristics evolved over millions of years to find the ripe fruit, avoid being eaten, and have sex. If you think getting XML was hard, that's *nothing* compared to user interfaces. The fact that even bad UIs work at all is a credit to the heuristics, bugs and all, in the meat. > As for "a minimum of bugs"... sigh. The "minimum of bugs" is zero, if > you derive your "uninterpreted formulae" /correctly/. And the secret of getting rich on the stock market is to buy low, sell high. Very true, and very pointless. How do you derive the formula correctly? Do you have an algorithm to do so? If so, how do you know that algorithm is correct? > Deriving provably > correct "programs" should be what computer science and computer > education are all about That's impossible. Not just impractical, or hard, or subject to hardware failures, but impossible. I really shouldn't need to raise this to anyone with pretensions of being a Computer Scientist and not just a programmer, but... try proving an arbitrary program will *halt*. If you can't do that, then what makes you think you can prove all useful, necessary programs are correct? > Again with Dijkstra[3]: "The prime paradigma of the pragmatic designer > is known as "poor man's induction", i.e. he believes in his design as > long as "it works", i.e. until faced with evidence to the contrary. (He > will then "fix the design".) Yes, that is the only way it can be. > The scientific designer, however, believes > in his design because he understands why it will work under all > circumstances. Really? Under *all* circumstances? Memory failures? Stray cosmic radiation flipping bits? Noise in the power supply? What's that you say? "That's cheating, the software designer can't be expected to deal with the actual reality of computers!!! We work in an abstract realm where computers never run out of memory, swapping never occurs, and the floating point unit is always correct!" Fail enough. I don't want to make it too hard for you. Okay, so let's compare the mere "pragmatic designer" with his provisional expectation that his code is correct, and Dijkstra's "scientific designer". He understands his code is correct. Wonderful! Er, *how exactly* does he reach that understanding? He reasons about the logic of the program. Great! I can do that. See, here's a proof that binary search is correct. How easy this is. Now try it for some non-trivial piece of code. Say, the Linux kernel. This may take a while... What guarantee do we have that the scientific designer's reasoning was correct? I mean, sure he is convinced he has reasoned correctly, but he would, wouldn't he? If he was unsure, he'd keep reasoning (or ask for help), until he was sure. But being sure doesn't make him correct. It just makes him sure. So how do you verify the verification? And speaking of binary search: [quote] I was shocked to learn that the binary search program that Bentley PROVED CORRECT and SUBSEQUENTLY TESTED [emphasis added] in Chapter 5 of "Programming Pearls" contains a bug. Once I tell you what the it is, you will understand why it escaped detection for two decades. [end quote] http://northernplanets.blogspot.com/2006/07/nearly-all-binary-searches- are-broken.html or http://tinyurl.com/nco6yv Perhaps the "scientific designer" should be a little less arrogant, and take note of the lesson of real science: all knowledge is provisional and subject to correction and falsification. Which really makes the "scientific designer" just a slightly more clever and effective "pragmatic designer", doesn't it? > The transition from pragmatic to scientific design would > indeed be a drastic change within the computer industry." Given Dijkstra's definition of "scientific design", it certainly would. It would be as drastic a change as the discovery of Immovable Objects and Irresistible Forces would be to engineering, or the invention of a Universal Solvent for chemistry, or any other impossibility. > "Obviously no errors" is the goal to strive for, and I am comfortable > calling anyone an amateur who prefers "no obvious errors." It's not a matter of preferring no obvious errors, as understanding the limitations of reality. You simply can't do better than no obvious errors (although of course you can do worse) -- the only question is what you call "obvious". -- Steven From Lacrima.Maxim at gmail.com Thu Jul 9 14:22:46 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Thu, 9 Jul 2009 11:22:46 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: <53aaa579-e69a-43c3-b297-10c6ea6ebf35@b15g2000yqd.googlegroups.com> On Jul 9, 8:42?pm, Piet van Oostrum wrote: > >>>>> Lacrima (L) wrote: > >L> Thank you for really useful and detailed explanation. Now I can test > >L> my code usingEmacs. > >L> But I think it works for me in a little bit different way. > >L> My file contains only the print 'hello world'. > >L> If I have no python shell running, then: > >L> a) C-c RET opens *Python Output* buffer without any output and with > >L> 'Shell command succeeded with no output' message at the bottom > >L> b) C-c C-c opens *Python Output* with appropriate 'hello world' > >L> message. > >L> Then I run python shell with C-c ! command. And: > >L> a) C-c RET opens *Python* buffer and prints 'hello world' in the > >L> python shell > >L> b) C-c C-c gives the same result. > > Which version of python-mode.el do you have? (Variable py-version) > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org py-version's value is "5.1.0" Also maybe the problem is because I use Emacs version 21.4.1. But I can't change it to 22, because I access Emacs via SSH on some shared hosting service. With regards, Max. From ptmcg at austin.rr.com Thu Jul 9 14:29:12 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 9 Jul 2009 11:29:12 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 1:09?pm, DuaneKaufman wrote: > The application I wish to interact with is not my own, but an ERP > system GUI front-end. > I have used pywinauto to drive a Flash game running inside of an Internet Explorer browser - that's pretty GUI! -- Paul From duane.kaufman at gmail.com Thu Jul 9 14:50:45 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 11:50:45 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 1:29?pm, Paul McGuire wrote: > On Jul 9, 1:09?pm, DuaneKaufman wrote: > > > The application I wish to interact with is not my own, but an ERP > > system GUI front-end. > > I have used pywinauto to drive a Flash game running inside of an > Internet Explorer browser - that's pretty GUI! > > -- Paul Hi, Could you share some code examples? Thanks, Duane (duanek (at) chorus (dot) net) From fridrik at pyth.net Thu Jul 9 14:57:47 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 18:57:47 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <9DBED98B-8B7A-45DC-9177-D3E97B878AAF@pyth.net> Hi Rhodri, > It's only really a pitfall if you try to use the built-in after you've > redefined it. That's the thing to keep an eye open for. You're right, but in cases where you're editing a codebase which you didn't author entirely by yourself you may not be aware of that. That said, if the codebase you're working on is structured (short, concise methods) you should be working with small, consumable scopes you can inhale in entirety before modifying. Regards, Fri?rik M?r From dns4 at cornell.edu Thu Jul 9 15:08:29 2009 From: dns4 at cornell.edu (David Smith) Date: Thu, 09 Jul 2009 15:08:29 -0400 Subject: tkinter problem In-Reply-To: <4a5615ba$0$95523$742ec2ed@news.sonic.net> References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > "Peter Otten" <__peter__ at web.de> wrote in message > news:h3481q$d95$00$1 at news.t-online.com... >> Paul Simon wrote: >> >>> "Chris Rebert" wrote in message >>> news:mailman.2863.1247095339.8015.python-list at python.org... >>> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>>> I have the "tkinter" problem and need some assistance to straighten it >>>> out. >>>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >>>>> "step >>>> 1" and cannot import "_tkinter." I do not have that file on my computer, >>>> but >>>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>>> This python stuff is great, but the documentation frequently >>>> feels like it is just a bit out of my grasp. I realize that all of this >>>> is free but I understand the instructions on the web page to repair only >>>> to the >>>> point of confusion. I'm not an expert. How do I modify my python >>>> configuration? Is there a file that needs to be edited? Which setup.py >>>> file >>>> do I use? Make? or python setup.py build and python setup.py install? >>>> Thanks. I appreciate your help. >>> - How did you install Python? >>> - What Linux distro are you using? >>> >>> Cheers, >>> Chris >>> http://blog.rebertia.com >>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure >>> exactly how I installed Python. Originally I had installed 2.5 from RPM >>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>> something from python.org and installed. Not sure if it was tarball or >>> zip file. >> Zip or tar doesn't matter, you are installing "from source". >> >> Python has to find the necessary include files for tcl/tk. These are in >> separate packages that you have to install before you invoke Python's >> configure script. >> >> I don't know what they are called on your system -- look for tk-dev.rpm, >> tcl-dev.rpm or similar. >> >> You may run into the same problem with other modules like readline. >> >> Peter >> > > Thank you Peter. I understand what you are saying but don't know how to do > it. Although I installed from source, I followed a "cookbook" recipe. > Could you tell me what files to execute, where they might be, and file > arguments? I'm just ignorant, not stupid. ;-). > > Paul > > Just install the tkinter package from the Mandriva Linux Control Center's Software Management system. I just did it, doing a search for tkinter brought it right up. All done. --David From ethan at stoneleaf.us Thu Jul 9 16:06:16 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 09 Jul 2009 13:06:16 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <4A564DB8.3050209@stoneleaf.us> kj wrote: > > My scientific code is jam-packed with assertions. I can't count > the number of times that one such lowly assertion saved me from a > silent but potentially disastrous bug. Now imagine that asserts had been disabled for that run... The issue is not "should you validate your inputs", the issue is "do you want your validation able to be turned off?" ~Ethan~ From sberry2a at gmail.com Thu Jul 9 16:16:38 2009 From: sberry2a at gmail.com (Sean) Date: Thu, 9 Jul 2009 13:16:38 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list Message-ID: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> I have a huge list, 10,000,000+ items. Each item is a dictionary with fields used to sort the list. When I have completed sorting I want to grab a page of items, say 1,000 of them which I do easily by using list_data[x:x+1000] Now I want to add an additional key/value pair to each dictionary in the list, incrementing them by 1 each time. So, if I grabbed page 2 of the list I would get: [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': 1002}, ...] Any way to do that with list comprehension? Any other good way to do it besides iterating over the list? Thanks From sberry2a at gmail.com Thu Jul 9 16:33:01 2009 From: sberry2a at gmail.com (Sean) Date: Thu, 9 Jul 2009 13:33:01 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: On Jul 9, 1:16?pm, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? > > Thanks I was able to do this by doing the following: page_data = [[start_position + 1 + n, x] for n, x in enumerate (page_data)] However, this creates a list of lists, each containing an int and a dictionary. I wanted to add it directly to the dictionary. I can add the key position to the dictionary when the data is created, but how would I assign the value in a list comprehension? From tn.pablo at gmail.com Thu Jul 9 16:38:16 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 9 Jul 2009 15:38:16 -0500 Subject: Best way to add a "position" value to each item in a list In-Reply-To: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: Howdy, On Thu, Jul 9, 2009 at 15:16, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > I don't get it, what do you increment by one, the value of a given key or the number of key/value pairs? Also, if the 'position' key is the index of the item in the list, then I don't understand what you mean by 'page'. Could you tell us about the structure of these dictionaries? > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? > > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > -- Pablo Torres N. From ptmcg at austin.rr.com Thu Jul 9 16:41:17 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 9 Jul 2009 13:41:17 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 1:50?pm, DuaneKaufman wrote: > > I have used pywinauto to drive a Flash game running inside of an > > Internet Explorer browser - that's pretty GUI! > > > -- Paul > > Hi, > > Could you share some code examples? > > Thanks, > Duane (duanek (at) chorus (dot) net) I just went on a brief fishing expedition through two disk backups, and no luck. I guess it's been a while since I worked on this. The work I did was entirely graphical, which is to say, my script interacted with the Flash program by using PIL to take image snapshots of the window, and then sifting through the bitmap looking for the status of a "time remaining" thermometer-style gauge in the game. Then the script could click on X-Y coordinates within the window, which would get picked up by the Flash game, and the script would monitor the "time remaining" gauge some more, and so on. I'm not sure how well pywinauto would work in allowing you to access controls such as textboxes within a form. I remember that I had to access the IE window using a caption name, and then found the embedded Flash program as an embedded control of some sort, again, I probably needed to indicate that it was some sort of "Adobe.FlashWidget1" object or something. I may have another, older disk backup at home, I can look for it later this evening. -- Paul From malaclypse2 at gmail.com Thu Jul 9 16:49:22 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 9 Jul 2009 16:49:22 -0400 Subject: Best way to add a "position" value to each item in a list In-Reply-To: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: <16651e80907091349x409158b7sf62f7bbd0fb7c1a6@mail.gmail.com> On Thu, Jul 9, 2009 at 4:16 PM, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? Normally you wouldn't mutate the items in a list with a list comprehension. Instead, you would use a for loop, like this: for idx,item in enumerate(my_list_of_dicts): item['position'] = idx Is there a particular reason you want to do this with a list comprehension? Using a list comp means you're going to create an extra copy of your 10 million item list, just so you can add a key to each member, then (probably) throw away the original list. It doesn't seem like the right tool for the job here. -- Jerry From jcd at sdf.lonestar.org Thu Jul 9 16:56:44 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Thu, 09 Jul 2009 16:56:44 -0400 Subject: tough-to-explain Python In-Reply-To: <00720d76$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <1247173004.20204.0.camel@aalcdl07> On Thu, 2009-07-09 at 18:10 +0000, Steven D'Aprano wrote: > If programming is symbol manipulation, then you should remember that > the > user interface is also symbol manipulation, and it is a MUCH harder > problem than databases, sorting, searching, and all the other > problems > you learn about in academia. The user interface has to communicate > over a > rich but noisy channel using multiple under-specified protocols to a > couple of pounds of meat which processes information using buggy > heuristics evolved over millions of years to find the ripe fruit, > avoid > being eaten, and have sex. If you think getting XML was hard, that's > *nothing* compared to user interfaces. +1 QOTW! QOTM, even! From sberry2a at gmail.com Thu Jul 9 17:00:27 2009 From: sberry2a at gmail.com (Sean) Date: Thu, 9 Jul 2009 14:00:27 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: <58d94d82-3482-4429-9632-3255d2a85295@12g2000pri.googlegroups.com> On Jul 9, 1:16?pm, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? > > Thanks I was able to do this by doing the following: page_data = [[start_position + 1 + n, x] for n, x in enumerate (page_data)] However, this creates a list of lists, each containing an int and a dictionary. I wanted to add it directly to the dictionary. I can add the key position to the dictionary when the data is created, but how would I assign the value in a list comprehension? From pavlovevidence at gmail.com Thu Jul 9 17:03:13 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Jul 2009 14:03:13 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: <1bf2ab02-2a84-41a2-8156-9ec3ac7301c4@t13g2000yqt.googlegroups.com> On Jul 9, 1:16?pm, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? Not really, but if you want to avoid the delay in setting the dictionary elements (I'm guessing that is why you don't want to iterate over the list--and parenthetically it's not iterating over the list but adding dictionary items that is driving the time, and that cost is unavoidable), you should consider adding the position elements on demand. That is, instead of running a big loop once to add position to all ten million elements, just run the loop on individual pages. def get_page(start,end): page = list_data[start:end] for i,item in enumerate(page): page['position'] = start+i return page That might not work depending on what you are doing but you should consider it. Carl Banks From torriem at gmail.com Thu Jul 9 17:09:13 2009 From: torriem at gmail.com (Michael Torrie) Date: Thu, 09 Jul 2009 15:09:13 -0600 Subject: Examples of Python driven Microsoft UI Automation wanted In-Reply-To: References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: <4A565C79.4020501@gmail.com> DuaneKaufman wrote: > With MS utilities like UISpy and the like, I can 'see' the controls in > the application, but I > do not seem to be able to manipulate them programatically, and I > believe it us simply due > to my not understanding of the UI Automation API. You're probably better off using a free app like AutoIt3 rather than roll your own. AutoIt's scripting language isn't as nice as Python, but it does the job and it turing-complete. http://www.autoitscript.com/autoit3/ From thebrasse at gmail.com Thu Jul 9 17:10:02 2009 From: thebrasse at gmail.com (=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=) Date: Thu, 9 Jul 2009 14:10:02 -0700 (PDT) Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: <42fbcee1-61b2-4ba8-a50a-39c9c0d2fe9c@k19g2000yqn.googlegroups.com> On Jul 9, 7:30?pm, Lie Ryan wrote: > brasse wrote: > > Hello! > > I have been thinking about how write exception safe constructors in > > Python. By exception safe I mean a constructor that does not leak > > resources when an exception is raised within it. The following is an > > example of one possible way to do it: > > First, your automatic cleaning Bar() silently pass an unitialized Bar() > into the calling code. When a code fails, it should fail as loud as > possible. Bar() should raises an Exception and the calling code should > turn into something like: > > try: > ? ? bar = Bar() > except Exception, e: > ? ? print 'bar failed to construct' > I know, I missed that little detail. :-) > And about the cleaning up, how about: > > class CleanWrap(object): > ? ? def __init__(self, cls, cleanup): > ? ? ? ? self.cls = cls > ? ? ? ? self.cleanup = cleanup > ? ? def __call__(self, *args, **kargs): > ? ? ? ? try: > ? ? ? ? ? ? return self.cls(*args, **kargs) > ? ? ? ? except: > ? ? ? ? ? ? self.cleanup() > ? ? ? ? ? ? raise > > class Bar(object): > ? ? def __init__(self): > ? ? ? ? CleanWrappedFoo = CleanWrap(Foo, self.close) > ? ? ? ? self.a = CleanWrappedFoo('a') > ? ? ? ? self.b = CleanWrappedFoo('b', fail=True) > ? ? def close(self): > ? ? ? ? if hasattr(self, 'a'): > ? ? ? ? ? ? self.a.close() > ? ? ? ? if hasattr(self, 'b'): > ? ? ? ? ? ? self.b.close() > I think this example adds about as much overhead as my original example with the try block. Ideally I would like to be able to write my classes in the most straight forward manner possible. In my mind that would be something like a naive constructor and a close/dispose method: def __init__(self): self.a = Foo() self.b = Bar() def close(self): self.a.close() self.b.close() See my other post (the one with the decorator named safe) in this thread for an experiment I thought was promising for a while. However there are some some use cases it has to cover to be useful: (1) References to objects owned by someone else def __init__(self, some_resource): self.not_my_responsibility = some_resource self.a = Foo() # If Foo.__init__ raises an exception self.not_my responsibility should not be closed. (2) Local objects def __init__(self): x = Bar() self.a = Foo(x) # If Foo.__init__ raises an exception x should also be closed. So far I have not been able to find any good solutions for (at least) case 1. I'll give it a few more days, but I'm not that hopeful. I think that I will end up writing my classes very much like Bar in my original post. If someone has any ideas for how to solve my two use cases above with my decorator based approach I would really like to hear them! :.:: mattias From torriem at gmail.com Thu Jul 9 17:12:40 2009 From: torriem at gmail.com (Michael Torrie) Date: Thu, 09 Jul 2009 15:12:40 -0600 Subject: Examples of Python driven Microsoft UI Automation wanted In-Reply-To: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: <4A565D48.2060608@gmail.com> TheSeeker wrote: > Alternatives to Microsoft's UI Automation are welcome too, but I have > tried using winguiauto and watsup (along with AutoIt), and there seems > to be severe limitations when using these tools with WinForm > applications. http://www.autoitscript.com/forum/index.php?showtopic=96752 -- looks like recent betas have much better support for WinForms, at least from the change list. From helvinlui at gmail.com Thu Jul 9 17:19:23 2009 From: helvinlui at gmail.com (Helvin) Date: Thu, 9 Jul 2009 14:19:23 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> Message-ID: <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> On Jul 10, 3:54?am, Robert Kern wrote: > On 2009-07-09 01:27, Helvin wrote: > > > > > > > On Jul 9, 11:29 am, Robert Kern ?wrote: > >> On 2009-07-08 18:10, Helvin wrote: > > >>> Thanks for the fast replies! I will look into how to use VTK now. > >>> Where would I find VTK's explicit support for PyQt? > >> Wrapping/Python/vtk/qt4/ in the VTK sources. > > >>> Because I have installed VTK (using its installer) and pyVTK (using > >>> its setup.py file), but how do I actually use it in my code? According > >>> to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., > >>> I have tried 'import vtk', but python can't find the vtk module. > >> Then you have not installed VTK's Python bindings correctly. Note that pyVTK is > >> just a library for manipulating VTK files. The VTK Python bindings are part of > >> VTK's distribution itself. Exactly how did you install VTK? Did you compile it > >> yourself? > > > You mean, when I download VTK, the VTK Python bindings come with it? > > I downloaded VTK from:http://vtk.org/files/release/5.4/ > > Exactly which file did you download? I don't think the vtk-5.4.x-win32.exe > installers have the Python bindings. You will need to build VTK from sources. > > -- > 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 Yes, I think you are right. I did initially use the windows installer. I didn't know that it did't have python bindings. I thought that if I had pyVTK, then it would work. But it didn't. I am now building VTK with sources according to this EXCELLENT tutorial that I have somehow come across, at: http://www-viz.tamu.edu/courses/viza658/08spring/tutorials/WinVTKInstall.html It's for Windows XP, and I am on Vista, but I hope it will work. I have worked my way through most of it now. Thanks Robert! Helvin From mail.to.daniel.platz at googlemail.com Thu Jul 9 18:03:36 2009 From: mail.to.daniel.platz at googlemail.com (tt-industries) Date: Thu, 9 Jul 2009 15:03:36 -0700 (PDT) Subject: Package for fast plotting of many data points in Python? Message-ID: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Hi, I am programming a oscilloscope module in Python. For this reason, I want to plot very many data points as fast as possible. This can be more than 100 000 at once. So far I have been using the ploting module of wxPython. However, it becomes unstable for more than 25000 points. Can someone recommend me a faster plotting library? It would be really cool if one could embed this in wxPython. If someone has an idea I would be very glad about answer. With kind regards, Daniel From duane.kaufman at gmail.com Thu Jul 9 18:10:10 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 15:10:10 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: <4be34b72-0961-4031-b943-82c9e7c95577@24g2000yqm.googlegroups.com> On Jul 9, 4:12?pm, Michael Torrie wrote: > TheSeeker wrote: > > Alternatives to Microsoft's UI Automation are welcome too, but I have > > tried using winguiauto and watsup (along with AutoIt), and there seems > > to be severe limitations when using these tools with WinForm > > applications. > > http://www.autoitscript.com/forum/index.php?showtopic=96752-- looks > like recent betas have much better support for WinForms, at least from > the change list. Hi, Thanks for the information. Boy, you are away from a utility for a couple of weeks, and it starts growing things one needs! Unfortunately, while it looks like AutoIt is gaining the ability to 'see' WinForms controls, this beta doesn't seem to be able to retrieve the text say, in a TextBox. Darn. Thanks, Duane From stef.mientki at gmail.com Thu Jul 9 18:26:09 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Fri, 10 Jul 2009 00:26:09 +0200 Subject: Package for fast plotting of many data points in Python? In-Reply-To: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <4A566E81.5040503@gmail.com> tt-industries wrote: > Hi, > > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. At once is impossible ;-) > So far I have been using the ploting module > of wxPython. which plotting module ? > However, it becomes unstable for more than 25000 points. > again what do you mean: 25000 points per ? 25000 points on a 1600 pixel width screen ? Please try to be somewhat more specific. cheers, Stef > Can someone recommend me a faster plotting library? It would be really > cool if one could embed this in wxPython. If someone has an idea I > would be very glad about answer. > > With kind regards, > > Daniel > From tn.pablo at gmail.com Thu Jul 9 18:49:48 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 9 Jul 2009 17:49:48 -0500 Subject: can i write a assemly language programs in python In-Reply-To: References: <4A55F392.3080404@ieee.org> Message-ID: Or maybe he meant if you can have both Python and assembler code in the same source file, but I haven't heard of it. If what you are trying to do is write a faster version of some part of your code, try SWIG: http://www.swig.org/ Playing the guessing game is a time sink, and since nobody has done it so far, I will: OP, please read http://www.mikeash.com/getting_answers.html and http://catb.org/esr/faqs/smart-questions.html On Thu, Jul 9, 2009 at 12:40, Terry Reedy wrote: > Dave Angel wrote: >> >> m.reddy prasad reddy wrote: >>> >>> can any one tell me how to write assembly language programs in >>> python...if >>> no is there any other way to write the programs in python >>> >>> Reddi prasad reddy >>> ph.no:09958083797 >>> >>> >> >> Assembly language is a different programming language than Python. ?You >> can use both in the same process, much in the same way you can use C or C++ >> with Python. ?In fact, some of the system DLL's are written (partly) in >> assembler, though mostly in C or C++. > > It is possible that he meant how to write assembly *with* python. > Or how to translate python to assembly. > > As it turns out, CPython translates Python to byte code and has a dis > (assembly) module that produces very nice assembly code ;-) > So that is one answer to his question. > >> You'll need to tell us what your real goal is. > > Definitely. > > tjr > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Pablo Torres N. From milesck at umich.edu Thu Jul 9 19:28:11 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Thu, 9 Jul 2009 19:28:11 -0400 Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <4F69EE56-6911-400D-B324-4F6CE3E789CF@umich.edu> On Jul 9, 2009, at 9:20 AM, Lie Ryan wrote: > Michael Mossey wrote: >> I want to understand better what the "secret" is to responding to a >> ctrl-C in any shape or form. > > Are you asking: "when would the python interpreter process > KeyboardInterrupt?" > ... > In single threaded python program, the currently running thread is > always the main thread (which can handle KeyboardInterrupt). I believe > SIGINT is checked at every ticks. But SIGINT cannot interrupt atomic > operations (i.e. it cannot interrupt long operations that takes a > single > tick). Some otherwise atomic single-bytecode operations (like large integer arithmetic) do manual checks for whether signals were raised (though that won't help at all if the operation isn't on the main thread). > I believe a tick in python is equivalent to a single bytecode, but > please correct me if I'm wrong. Not all opcodes qualify as a tick. In general, those opcodes that cause control to remain in the eval loop (and not make calls to other Python or C functions) don't qualify as ticks (but there are exceptions, e.g. so that while True: pass is interruptible). In Python/ceval.c: PyEval_EvalFrameEx(), those opcodes that don't end in goto fast_next_opcode are ticks. Please correct me if _I'm_ wrong! :) -Miles From sajmikins at gmail.com Thu Jul 9 19:38:18 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 9 Jul 2009 19:38:18 -0400 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: <50f98a4c0907091638u6ad23eaeo779a333a46860dc8@mail.gmail.com> On Thu, Jul 9, 2009 at 3:17 AM, m.reddy prasad reddy wrote: > > can any one tell me how to write assembly language programs in python...if > no is there any other way to write the programs in python > > Reddi prasad reddy > ph.no:09958083797 > > -- > http://mail.python.org/mailman/listinfo/python-list > > Examine CorePy http://www.corepy.org "CorePy is a complete system for developing machine-level programs in Python. CorePy lets developers build and execute assembly-level programs interactively from the Python command prompt, embed them directly in Python applications, or export them to standard assembly languages." HTH, ~Simon From xavier.benech at t-immersion.com Thu Jul 9 19:39:07 2009 From: xavier.benech at t-immersion.com (=?ISO-8859-1?Q?Xavier_B=E9nech?=) Date: Fri, 10 Jul 2009 01:39:07 +0200 Subject: sys.exit() and PyRun_SimpleFileExFlags() Message-ID: <4A567F9B.80108@t-immersion.com> Hi, There is a behaviour I do not understand of PyRun_SimpleFileExFlags(), normally when it executes a python script containing a sys.exit(), it results by ending the calling application. I have got this behaviour with PyRun_SimpleFileExFlags() when I call it from the main thread of a GUI application (so I use PyRun_FileExFlags() in this case). But in another application when it is called (PyRun_SimpleFileExFlags())) from a working thread (not the main one) of a console application, everything just go fine without exiting the application nor the thread. It returns the exit code specified in sys.exit() just fine. Last things, I am working on Windows. And for the story, I started to make a small function to wrap some embedded python script call using the simple way with PyRun_SimpleFileExFlags() for the second application, but when I integrate it in the first one (with GUI) got some problems (:)) so I redo things with a cleaner PyRun_FileExFlags() call. So, my question is: why do I have different behaviour of PyRun_SimpleFileExFlags() in this two cases? Regards, Xavier Benech. From bearophileHUGS at lycos.com Thu Jul 9 19:49:10 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 9 Jul 2009 16:49:10 -0700 (PDT) Subject: can i write a assemly language programs in python References: Message-ID: <4d8af709-e4c1-431e-9a43-7f0f0dd8f1a6@j32g2000yqh.googlegroups.com> Simon Forman: > Examine CorePyhttp://www.corepy.org > > "CorePy is a complete system for developing machine-level programs in > Python. CorePy lets developers build and execute assembly-level > programs interactively from the Python command prompt, embed them > directly in Python applications, or export them to standard assembly > languages." I have never used CorePy yet, but it's an awesome project ^_^ With some care and work even Python programs can get fast enough :-) Bye, bearophile From clp2 at rebertia.com Thu Jul 9 19:54:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 16:54:23 -0700 Subject: Package for fast plotting of many data points in Python? In-Reply-To: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <50697b2c0907091654l188ea9c1v6b568fdf1dc5e89@mail.gmail.com> On Thu, Jul 9, 2009 at 3:03 PM, tt-industries wrote: > Hi, > > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. So far I have been using the ploting module > of wxPython. However, it becomes unstable for more than 25000 points. > Can someone recommend me a faster plotting library? It would be really > cool if one could embed this in wxPython. If someone has an idea I > would be very glad about answer. Don't know how fast it is or how well it integrates, but matplotlib is probably worth considering: http://matplotlib.sourceforge.net/ Cheers, Chris -- http://blog.rebertia.com From sajmikins at gmail.com Thu Jul 9 20:01:47 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 9 Jul 2009 20:01:47 -0400 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <50f98a4c0907091701q660fec7cxfe75315a4e72575b@mail.gmail.com> On Thu, Jul 9, 2009 at 9:42 AM, Nick wrote: > ? ?fields = line.split() > ? ?for i in range(len(fields)): > ? ? ? ?fields[i] = float(fields[i]) instead of the above code you could say: fields = [float(n) for n in in line.split()] Have fun getting back into python! :] (A lot has changed in the last few years) HTH, ~Simon From http Thu Jul 9 20:22:06 2009 From: http (Paul Rubin) Date: 09 Jul 2009 17:22:06 -0700 Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: <7xab3d9ykx.fsf@ruckus.brouhaha.com> Nick writes: > text = file.readlines() > len = len(text) > fields = text[1].split() Is that intended to split the first line of the file? Remember that arrays in python begin at index 0. From marty.musatov at gmail.com Thu Jul 9 21:03:31 2009 From: marty.musatov at gmail.com (Musatov) Date: Thu, 9 Jul 2009 18:03:31 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google Message-ID: Los Angeles (AP) --MeAmI.org now has users in 50 countries following its adopted use in Pakistan. The search engine has grown in popularity 10,000 fold following its Beta test launch three months ago in April, 2009. Supporters of the site claim it is better than rival Google upon which platform it is based. Controversy arose after MeAmI.org search code allowed users to search other users Google results with no advertising. "It is truly an innovative thing we are doing," said Founder and CEO, Martin Musatov. "Letting users search the results of other Google users immediately results in a level of accuracy and relevance above and beyond Google." Google changed their API following the launch or MeAmI.org and explored the possibility of blocking site access from MeAmI.org to Google search results but was unable to do so. Critics of MeAmI.org say what it is doing is tantamount to intellectual theft. When asked about this topper Musatov exclaimed, "The Internet was made for people, not companies." An analyst at Goldman Sachs says, requesting to remain anonymous, "MeAmI.org has a strong presence in promoting itself as a vehicle for global activism and to tell you the truth, this makes it much more likely an acquisition target than potential intellectual property violator." Google could not be reached for comment. From ldo at geek-central.gen.new_zealand Thu Jul 9 21:05:01 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 10 Jul 2009 13:05:01 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , kj wrote: > .., Lundh writes: > > Assignment statements modify namespaces, not objects. >>> a = [3] >>> b = a These may indeed modify a namespace, not any object. However: >>> a[:] = [4] >>> a [4] >>> b [4] What change has happened to the namespace in this case? None. Yet an object has changed. From ben+python at benfinney.id.au Thu Jul 9 21:06:34 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jul 2009 11:06:34 +1000 Subject: Running a script to build docs from setup.py References: <20090709173343.1784710d@realh.co.uk> Message-ID: <87fxd5ny79.fsf@benfinney.id.au> Tony Houghton writes: > I've looked through the manual but I can't find any hooks in distutils > for generating files at install time other than extension modules and > .pyc files. Should I just run the script from somewhere in my setup.py > before calling distutils' setup function? Indirectly related: Ian Bicking's article on using Python's ?setup.py? as a ?Makefile? replacement: If one is writing a ?setup.py? anyway, I think it makes sense to use that as the build program for the whole project if possible. Good hunting! -- \ ?I have an answering machine in my car. It says, ?I'm home now. | `\ But leave a message and I'll call when I'm out.?? ?Steven Wright | _o__) | Ben Finney From scriber17 at aol.com Thu Jul 9 21:07:55 2009 From: scriber17 at aol.com (The Masked Logician) Date: Thu, 9 Jul 2009 18:07:55 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <8bbed30b-53e5-4558-8e50-fbc395221915@y10g2000prf.googlegroups.com> On Jul 9, 6:03?pm, Musatov wrote: > Los Angeles (AP) -- MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search > the results of other Google users immediately results in a level of > accuracy and relevance above and beyond Google." Google changed their > API following the launch or MeAmI.org and explored the possibility of > blocking site access from MeAmI.org to Google search results but was > unable to do so. Critics of MeAmI.org say what it is doing is > tantamount to intellectual theft. When asked about this topper Musatov > exclaimed, "The Internet was made for people, not companies." An > analyst at Goldman Sachs says, requesting to remain anonymous, > "MeAmI.org has a strong presence in promoting itself as a vehicle for > global activism and to tell you the truth, this makes it much more > likely an acquisition target than potential intellectual property > violator." Google could not be reached for comment. Wow. From ben+python at benfinney.id.au Thu Jul 9 21:11:26 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jul 2009 11:11:26 +1000 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <87bpntnxz5.fsf@benfinney.id.au> tt-industries writes: > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. I think you will find good results using Numpy for your arrays of data and Matplotlib to plot those arrays . -- \ ?Ubi dubium, ibi libertas.? (?Where there is doubt, there is | `\ freedom.?) | _o__) | Ben Finney From roy at panix.com Thu Jul 9 21:16:09 2009 From: roy at panix.com (Roy Smith) Date: Thu, 09 Jul 2009 21:16:09 -0400 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: In article <0734dc45-d8a0-4f28-b945-f9e179f30f5c at h11g2000yqb.googlegroups.com>, tt-industries wrote: > I am programming a oscilloscope module in Python. Sigh. I guess I'm showing my age, but I still can't get used to the idea that the right tool to build an oscilloscope is no longer a soldering iron. From bcb at undisclosedlocation.net Thu Jul 9 21:18:23 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Thu, 9 Jul 2009 20:18:23 -0500 Subject: AP -- MeAmI.org Paces Google References: Message-ID: "Musatov" wrote in message news:fe9969b0-3c6a-49f7-8f38-0ed89dc9ad42 at x6g2000prc.googlegroups.com... [snip-a-rooney] > violator." Google could not be reached for comment. Possibly because they were laughing so hard they couldn't hear their cell phones ringing ...? From scriber17 at aol.com Thu Jul 9 21:29:58 2009 From: scriber17 at aol.com (The Masked Logician) Date: Thu, 9 Jul 2009 18:29:58 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <7657db2e-71b3-4a49-b70f-127e362824a2@x5g2000prf.googlegroups.com> On Jul 9, 6:18?pm, "Bruce C. Baker" wrote: > "Musatov" wrote in message > > news:fe9969b0-3c6a-49f7-8f38-0ed89dc9ad42 at x6g2000prc.googlegroups.com... > > [snip-a-rooney] > > > violator." Google could not be reached for comment. > > Possibly because they were laughing so hard they couldn't hear their cell > phones ringing ...? Thank you for your comment Bruce C. Baker. Are you aware you're dead? http://wcc.dli.mt.gov/B/BAKER_BRUCE_SJ_DECISION.htm http://MeAmI.org "The Best Search Engine for Fat Dead Guys named Bruce" From matt.dubins at sympatico.ca Thu Jul 9 21:36:05 2009 From: matt.dubins at sympatico.ca (inkhorn) Date: Thu, 9 Jul 2009 18:36:05 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? Message-ID: Hi all, For one of my projects, I came across the need to check if one of many items from a list of strings could be found in a long string. I came up with a pretty quick helper function to check this, but I just want to find out if there's something a little more elegant than what I've cooked up. The helper function follows: def list_items_in_string(list_items, string): for item in list_items: if item in string: return True return False So if you define a list x = ['Blah','Yadda','Hoohoo'] and a string y = 'Yip yip yippee Blah' and you run list_items_in_string(x, y), it should return True. Any ideas how to make that function look nicer? :) Matt Dubins From david250 at videotron.ca Thu Jul 9 21:41:53 2009 From: david250 at videotron.ca (David Bernier) Date: Thu, 09 Jul 2009 21:41:53 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search [ snip ] I wonder what _AP_ means ... David Bernier From clp2 at rebertia.com Thu Jul 9 21:43:31 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 18:43:31 -0700 Subject: How to check if any item from a list of strings is in a big string? In-Reply-To: References: Message-ID: <50697b2c0907091843m174a065dx86e5ca6c21776a63@mail.gmail.com> On Thu, Jul 9, 2009 at 6:36 PM, inkhorn wrote: > Hi all, > > For one of my projects, I came across the need to check if one of many > items from a list of strings could be found in a long string. ?I came > up with a pretty quick helper function to check this, but I just want > to find out if there's something a little more elegant than what I've > cooked up. ?The helper function follows: > > def list_items_in_string(list_items, string): > ? ?for item in list_items: > ? ? ? ?if item in string: > ? ? ? ? ? ?return True > ? ?return False > > So if you define a list x = ['Blah','Yadda','Hoohoo'] and a string y = > 'Yip yip yippee Blah' and you run list_items_in_string(x, y), it > should return True. > > Any ideas how to make that function look nicer? :) any(substr in y for substr in x) Note that any() was added in Python 2.5 Cheers, Chris -- http://blog.rebertia.com From fridrik at pyth.net Thu Jul 9 21:45:01 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Fri, 10 Jul 2009 01:45:01 +0000 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: <8BCC0DC4-7ADB-496C-AE87-5C419656CD73@pyth.net> I'll be the first to admit it. The point of writing a fake story by Associated Press and publishing it on a programming mailing list is totally beyond me. Confoundedly yours, Fri?rik M?r From scriber17 at aol.com Thu Jul 9 21:48:17 2009 From: scriber17 at aol.com (The Masked Logician) Date: Thu, 9 Jul 2009 18:48:17 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <90319430-9fdf-4286-b19a-8b1a852f75a9@g7g2000prg.googlegroups.com> On Jul 9, 6:41?pm, David Bernier wrote: > Musatov wrote: > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > its adopted use in Pakistan. ?The search engine has grown in > > popularity 10,000 fold following its Beta test launch three months ago > > in April, 2009. Supporters of the site claim it is better than rival > > Google upon which platform it is based. Controversy arose after > > MeAmI.org search code allowed users to search other users Google > > results with no advertising. "It is truly an innovative thing we are > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > [ snip ] > > I wonder what _AP_ means ... > > David Bernier 1. Dec 16, 2008 ... "Staffers recognize the tough times, but they also understand that quality journalism at AP means attracting and retaining the best ... www.google.com/.../afp/.../ALeqM5gSCzjMBFqxtXCVC-am3ve59koBYQ 2. Business Accounting and Bookkeeping question: What does AP mean? many things inc. associated press. wiki.answers.com/Q/What_does_AP_mean Found: http://www.meami.org/?cx=000961116824240632825%3A5n3yth9xwbo&cof=FORID%3A9%3B+NB%3A1&ie=UTF-8&q=AP+means#943 3. David Bernier - Wikipedia, the free encyclopedia David Enrique Bernier also known as "Kike" (born January 21, 1977) former Secretary of Sports and Recreation of Puerto Rico as well as Executive Director of ... en.wikipedia.org/wiki/David_Bernier Found: http://www.meami.org/?cx=000961116824240632825%3A5n3yth9xwbo&cof=FORID%3A9%3B+NB%3A1&ie=UTF-8&q=David+Bernier#913 http://MeAmI.org "Use it to fend off assholes." From aahz at pythoncraft.com Thu Jul 9 22:00:21 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 Jul 2009 19:00:21 -0700 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: In article , Joshua Kugler wrote: >Joshua Kugler wrote: >> >> Sorry about that...since pysqlite and APSW are both discusses on the >> pysqlite list, I had made an incorrect assumption. Oops. > >"are both discusses?" Yeef, I must have been out of it. Discussed, thank >you. :) Who's Yeef? ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From jason at powerpull.net Thu Jul 9 22:06:35 2009 From: jason at powerpull.net (Jason S. Friedman) Date: Fri, 10 Jul 2009 02:06:35 +0000 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: <4A56A22B.5050204@powerpull.net> Hello, I administer the Informatica ETL tool at my company. Part of that role involves creating and enforcing standards. I want the Informatica developers to add comments to certain key objects and I want to be able to verify (in an automated fashion) that they have done so. I cannot merely check for non-emptiness; that is trivial to circumvent. On the other hand, I probably do not need to be able to catch developers who are determined to not create comments. There are not too many of them and perhaps they will find it is easier to write a (useful) comment than to game the system. Any thoughts on how I might proceed? Stated plainly, how can I tell when a string more-or-less forms at least one phrase? From marty.musatov at gmail.com Thu Jul 9 22:14:08 2009 From: marty.musatov at gmail.com (Martin Musatov) Date: Thu, 9 Jul 2009 19:14:08 -0700 Subject: AP -- MeAmI.org Paces Google In-Reply-To: <8BCC0DC4-7ADB-496C-AE87-5C419656CD73@pyth.net> References: <8BCC0DC4-7ADB-496C-AE87-5C419656CD73@pyth.net> Message-ID: Thank you. Martin Musatov 2009/7/9 Fri?rik M?r J?nsson > I'll be the first to admit it. The point of writing a fake story by > Associated Press and publishing it on a programming mailing list is totally > beyond me. > > Confoundedly yours, > Fri?rik M?r -------------- next part -------------- An HTML attachment was scrubbed... URL: From rogerb at rogerbinns.com Thu Jul 9 22:15:00 2009 From: rogerb at rogerbinns.com (Roger Binns) Date: Thu, 09 Jul 2009 19:15:00 -0700 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 jacopo mondi wrote: > Hi all, I need to patch socketmodule.c (the _socket module) in order to > add support to an experimental socket family. You may find it considerably easier to use ctypes since that will avoid the need for any patching. You'll also be able to control how read and write are done (eg read vs recvfrom vs recvmsg vs readv). You can use os.fdopen to convert your raw file descriptor into a Python file object if appropriate. If you do use ctypes then you'll only need to distribute pure Python source code. Roger -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkpWpCAACgkQmOOfHg372QQq0QCbB+rslUqK/AuUu0gA6N4m01Jo C8IAn3EkzFKCyt/K5WiuCnw3jzORBQh5 =JCv+ -----END PGP SIGNATURE----- From nobody at nowhere.com Thu Jul 9 22:28:04 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 10 Jul 2009 03:28:04 +0100 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > Nobody says you shouldn't check your data. Only that "assert" is not the > right way to do that. "assert" is not the right way to check your *inputs*. It's a perfectly reasonable way to check data which "should" be valid, as well as a way to document what variables are supposed to contain. From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 22:42:04 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 02:42:04 GMT Subject: language analysis to enforce code standards References: Message-ID: On Fri, 10 Jul 2009 02:06:35 +0000, Jason S. Friedman wrote: > Hello, I administer the Informatica ETL tool at my company. Part of > that role involves creating and enforcing standards. I want the > Informatica developers to add comments to certain key objects and I want > to be able to verify (in an automated fashion) that they have done so. > > I cannot merely check for non-emptiness; that is trivial to circumvent. > On the other hand, I probably do not need to be able to catch > developers who are determined to not create comments. There are not too > many of them and perhaps they will find it is easier to write a (useful) > comment than to game the system. > > Any thoughts on how I might proceed? Stated plainly, how can I tell > when a string more-or-less forms at least one phrase? Define "phrase". if len(s) > 0: print "at least one character" if len(s.split()) > 0: print "at least one word" if len(s.split('\n') > 0: print "at least one line" -- Steven From timr at probo.com Thu Jul 9 22:52:58 2009 From: timr at probo.com (Tim Roberts) Date: Thu, 09 Jul 2009 19:52:58 -0700 Subject: IP Address Function References: Message-ID: Fred Atkinson wrote: > >I wonder why they don't just have a function to return it instead of >putting you through all of that? In CGI, EVERYTHING gets communicated through environment variables. That (and the stdin stream) is really the only option, since you get a new process for every request. The Python CGI module doesn't provide a wrapper function because this information is just not useful. Most corporate users sit behind proxies, so everyone at the company appears to come from the same IP. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From nobody at nowhere.com Thu Jul 9 22:53:57 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 10 Jul 2009 03:53:57 +0100 Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > For one of my projects, I came across the need to check if one of many > items from a list of strings could be found in a long string. If you need to match many strings or very long strings against the same list of items, the following should (theoretically) be optimal: r = re.compile('|'.join(map(re.escape,list_items))) ... result = r.search(string) From david250 at videotron.ca Thu Jul 9 22:54:16 2009 From: david250 at videotron.ca (David Bernier) Date: Thu, 09 Jul 2009 22:54:16 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search > the results of other Google users immediately results in a level of > accuracy and relevance above and beyond Google." Google changed their > API following the launch or MeAmI.org and explored the possibility of > blocking site access from MeAmI.org to Google search results but was > unable to do so. Critics of MeAmI.org say what it is doing is > tantamount to intellectual theft. When asked about this topper Musatov > exclaimed, "The Internet was made for people, not companies." An > analyst at Goldman Sachs says, requesting to remain anonymous, > "MeAmI.org has a strong presence in promoting itself as a vehicle for > global activism and to tell you the truth, this makes it much more > likely an acquisition target than potential intellectual property > violator." Google could not be reached for comment. Mr. Musatov, do you know who originally wrote the article above? Thank you. David Bernier From zac256 at gmail.com Thu Jul 9 22:54:47 2009 From: zac256 at gmail.com (Zac Burns) Date: Thu, 9 Jul 2009 19:54:47 -0700 Subject: psyco V2 beta2 benchmark In-Reply-To: References: Message-ID: <333edbe80907091954u7451e11fhf9600d1e2549137c@mail.gmail.com> Where do you get this beta? I heard that Psyco V2 is coming out but can't find anything on their site to support this. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games On Sat, Jul 4, 2009 at 5:26 AM, larudwer wrote: > just out of curiosity i've downloaded the latest Version of Psyco V2 Beta 2 > and run the benchmarks against the old Version of psyco 1.6 > > Because it might be of common interest, i am posting the results here. > > My machine is a Pentium D 3.2 Ghz running Windows XP SP 3 and Python 2.6.2. > Psyco V2 was built with 4.3.3-tdm-1 mingw32 with optimisation flags changed > to -O3 > > > Benchmark ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | avg. Base time | psyco 1.6 time > | psyco 2.0 time | ratio ? ? | possible error +- > time_anyall all_bool_genexp ? ? ? ? ? ? ? | 2.270 ? ? ? ? ?| 2.250 > | 2.420 ? ? ? ? ?| 0.930 ? ? | 8.3 % > time_anyall all_bool_listcomp ? ? ? ? ? ? | 3.450 ? ? ? ? ?| 1.900 > | 1.910 ? ? ? ? ?| 0.995 ? ? | 0.0 % > time_anyall all_genexp ? ? ? ? ? ? ? ? ? ?| 1.970 ? ? ? ? ?| 1.940 > | 2.160 ? ? ? ? ?| 0.898 ? ? | 9.6 % > time_anyall all_listcomp ? ? ? ? ? ? ? ? ?| 3.485 ? ? ? ? ?| 1.660 > | 1.660 ? ? ? ? ?| 1.000 ? ? | 1.4 % > time_anyall all_loop ? ? ? ? ? ? ? ? ? ? ?| 0.665 ? ? ? ? ?| 0.090 > | 0.090 ? ? ? ? ?| 1.000 ? ? | 4.4 % > time_anyall any_bool_genexp ? ? ? ? ? ? ? | 2.215 ? ? ? ? ?| 2.130 > | 2.340 ? ? ? ? ?| 0.910 ? ? | 10.0 % > time_anyall any_bool_listcomp ? ? ? ? ? ? | 3.620 ? ? ? ? ?| 1.930 > | 1.940 ? ? ? ? ?| 0.995 ? ? | 9.0 % > time_anyall any_genexp ? ? ? ? ? ? ? ? ? ?| 1.985 ? ? ? ? ?| 1.920 > | 2.180 ? ? ? ? ?| 0.881 ? ? | 10.1 % > time_anyall any_listcomp ? ? ? ? ? ? ? ? ?| 3.360 ? ? ? ? ?| 1.680 > | 1.680 ? ? ? ? ?| 1.000 ? ? | 8.0 % > time_anyall any_loop ? ? ? ? ? ? ? ? ? ? ?| 0.660 ? ? ? ? ?| 0.090 > | 0.090 ? ? ? ? ?| 1.000 ? ? | 3.0 % > time_builtins chr(i) ? ? ? ? ? ? ? ? ? ? ?| 2.420 ? ? ? ? ?| 0.010 > | 0.010 ? ? ? ? ?| 1.000 ? ? | 0.0 % > time_builtins hash(i) ? ? ? ? ? ? ? ? ? ? | 1.280 ? ? ? ? ?| 0.370 > | 0.080 ? ? ? ? ?| 4.625 ? ? | 8.1 % > time_builtins int(round(f)) ? ? ? ? ? ? ? | 2.635 ? ? ? ? ?| 1.510 > | 1.120 ? ? ? ? ?| 1.348 ? ? | 0.4 % > time_builtins min ? ? ? ? ? ? ? ? ? ? ? ? | 0.535 ? ? ? ? ?| 0.520 > | 0.120 ? ? ? ? ?| 4.333 ? ? | 1.9 % > time_builtins min_kw ? ? ? ? ? ? ? ? ? ? ?| 4.430 ? ? ? ? ?| 4.400 > | 0.160 ? ? ? ? ?| 27.500 ? ?| 0.5 % > time_builtins ord(i) ? ? ? ? ? ? ? ? ? ? ?| 0.320 ? ? ? ? ?| 0.000 > | 0.000 ? ? ? ? ?| 1.000 ? ? | 6.5 % > time_builtins pow ? ? ? ? ? ? ? ? ? ? ? ? | 0.345 ? ? ? ? ?| 0.230 > | 0.150 ? ? ? ? ?| 1.533 ? ? | 2.9 % > time_builtins reduce ? ? ? ? ? ? ? ? ? ? ?| 0.735 ? ? ? ? ?| 0.710 > | 0.020 ? ? ? ? ?| 35.498 ? ?| 1.4 % > time_builtins round(f) ? ? ? ? ? ? ? ? ? ?| 1.720 ? ? ? ? ?| 0.890 > | 0.400 ? ? ? ? ?| 2.225 ? ? | 1.2 % > time_builtins sums ? ? ? ? ? ? ? ? ? ? ? ?| 0.180 ? ? ? ? ?| 0.180 > | 0.100 ? ? ? ? ?| 1.800 ? ? | 0.0 % > time_fib matrix ? ? ? ? ? ? ? ? ? ? ? ? ? | 0.425 ? ? ? ? ?| 0.360 > | 0.360 ? ? ? ? ?| 1.000 ? ? | 2.3 % > time_fib recursive ? ? ? ? ? ? ? ? ? ? ? ?| 0.000 ? ? ? ? ?| 0.000 > | 0.000 ? ? ? ? ?| 1.000 ? ? | 0.0 % > time_fib takahashi ? ? ? ? ? ? ? ? ? ? ? ?| 0.410 ? ? ? ? ?| 0.320 > | 0.330 ? ? ? ? ?| 0.970 ? ? | 0.0 % > time_generators call next just many times | 0.900 ? ? ? ? ?| 0.630 > | 0.970 ? ? ? ? ?| 0.649 ? ? | 4.3 % > time_generators iterate just many times ? | 0.660 ? ? ? ? ?| 0.550 > | 0.950 ? ? ? ? ?| 0.579 ? ? | 3.1 % > time_generators send and loop 1000 ? ? ? ?| 2.805 ? ? ? ? ?| 2.540 > | 0.060 ? ? ? ? ?| 42.333 ? ?| 9.8 % > time_generators send call loop 1000 ? ? ? | 2.505 ? ? ? ? ?| 2.940 > | 0.060 ? ? ? ? ?| 48.999 ? ?| 10.9 % > time_generators send just many times ? ? ?| 1.280 ? ? ? ? ?| 0.590 > | 0.980 ? ? ? ? ?| 0.602 ? ? | 3.1 % > time_iter iter ? ? ? ? ? ? ? ? ? ? ? ? ? ?| 1.490 ? ? ? ? ?| 0.590 > | 0.440 ? ? ? ? ?| 1.341 ? ? | 5.5 % > time_math floats ? ? ? ? ? ? ? ? ? ? ? ? ?| 2.910 ? ? ? ? ?| 1.500 > | 1.630 ? ? ? ? ?| 0.920 ? ? | 0.7 % > time_properties method_get ? ? ? ? ? ? ? ?| 0.935 ? ? ? ? ?| 0.120 > | 0.130 ? ? ? ? ?| 0.923 ? ? | 1.1 % > time_properties method_set ? ? ? ? ? ? ? ?| 1.005 ? ? ? ? ?| 0.170 > | 0.180 ? ? ? ? ?| 0.944 ? ? | 1.0 % > time_properties property_get ? ? ? ? ? ? ?| 0.960 ? ? ? ? ?| 0.740 > | 0.100 ? ? ? ? ?| 7.400 ? ? | 2.1 % > time_properties property_set ? ? ? ? ? ? ?| 1.020 ? ? ? ? ?| 0.920 > | 0.930 ? ? ? ? ?| 0.989 ? ? | 0.0 % > time_properties pyproperty_get ? ? ? ? ? ?| 1.535 ? ? ? ? ?| 1.310 > | 0.140 ? ? ? ? ?| 9.357 ? ? | 0.7 % > time_properties pyproperty_set ? ? ? ? ? ?| 1.030 ? ? ? ? ?| 0.920 > | 0.930 ? ? ? ? ?| 0.989 ? ? | 2.0 % > time_subdist subdist(i) ? ? ? ? ? ? ? ? ? | 3.665 ? ? ? ? ?| 1.640 > | 6.140 ? ? ? ? ?| 0.267 ? ? | 0.8 % > time_sums rounding ? ? ? ? ? ? ? ? ? ? ? ?| 0.800 ? ? ? ? ?| 0.790 > | 0.810 ? ? ? ? ?| 0.975 ? ? | 2.5 % > > > Running new timings with > C:\Programme\GNU\python26\lib\site-packages\psyco\_psyco.pyd Sat Jul 04 > 12:09:07 2009 > > time_anyall ? ?: all_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.36 ? ? psyco: > 2.42 ? ? ratio: ?0.97 > time_anyall ? ?: all_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.45 ? ? psyco: > 1.91 ? ? ratio: ?1.80 > time_anyall ? ?: all_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 2.06 ? ? psyco: > 2.16 ? ? ratio: ?0.95 > time_anyall ? ?: all_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.51 ? ? psyco: > 1.66 ? ? ratio: ?2.11 > time_anyall ? ?: all_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.65 ? ? psyco: > 0.09 ? ? ratio: ?7.03 > time_anyall ? ?: any_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.32 ? ? psyco: > 2.34 ? ? ratio: ?0.99 > time_anyall ? ?: any_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.45 ? ? psyco: > 1.94 ? ? ratio: ?1.78 > time_anyall ? ?: any_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 2.08 ? ? psyco: > 2.18 ? ? ratio: ?0.96 > time_anyall ? ?: any_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.49 ? ? psyco: > 1.68 ? ? ratio: ?2.08 > time_anyall ? ?: any_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.65 ? ? psyco: > 0.09 ? ? ratio: ?7.05 > time_builtins ?: chr(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.42 ? ? psyco: > 0.01 ? ? ratio: 197.40 > time_builtins ?: hash(i) ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 1.33 ? ? psyco: > 0.08 ? ? ratio: 17.69 > time_builtins ?: int(round(f)) ? ? ? ? ? ? ? ? ?plain: ? 2.63 ? ? psyco: > 1.12 ? ? ratio: ?2.36 > time_builtins ?: min ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.53 ? ? psyco: > 0.12 ? ? ratio: ?4.28 > time_builtins ?: min_kw ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 4.44 ? ? psyco: > 0.16 ? ? ratio: 28.58 > time_builtins ?: ord(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.33 ? ? psyco: > 0.00 ? ? ratio: 123.57 > time_builtins ?: pow ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.34 ? ? psyco: > 0.15 ? ? ratio: ?2.24 > time_builtins ?: reduce ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.74 ? ? psyco: > 0.02 ? ? ratio: 40.93 > time_builtins ?: round(f) ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.73 ? ? psyco: > 0.40 ? ? ratio: ?4.38 > time_builtins ?: sums ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.18 ? ? psyco: > 0.10 ? ? ratio: ?1.80 > time_fib ? ? ? : matrix ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.42 ? ? psyco: > 0.36 ? ? ratio: ?1.18 > time_fib ? ? ? : recursive ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.00 ? ? psyco: > 0.00 ? ? ratio: 19.45 > time_fib ? ? ? : takahashi ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.41 ? ? psyco: > 0.33 ? ? ratio: ?1.25 > time_generators: send call loop 1000 ? ? ? ? ? ?plain: ? 2.36 ? ? psyco: > 0.06 ? ? ratio: 41.30 > time_generators: send and loop 1000 ? ? ? ? ? ? plain: ? 2.66 ? ? psyco: > 0.06 ? ? ratio: 46.69 > time_generators: send just many times ? ? ? ? ? plain: ? 1.26 ? ? psyco: > 0.98 ? ? ratio: ?1.29 > time_generators: iterate just many times ? ? ? ?plain: ? 0.67 ? ? psyco: > 0.95 ? ? ratio: ?0.70 > time_generators: call next just many times ? ? ?plain: ? 0.88 ? ? psyco: > 0.97 ? ? ratio: ?0.91 > time_iter ? ? ?: iter ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.53 ? ? psyco: > 0.44 ? ? ratio: ?3.47 > time_math ? ? ?: floats ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.90 ? ? psyco: > 1.63 ? ? ratio: ?1.78 > time_properties: method_get ? ? ? ? ? ? ? ? ? ? plain: ? 0.93 ? ? psyco: > 0.13 ? ? ratio: ?7.22 > time_properties: method_set ? ? ? ? ? ? ? ? ? ? plain: ? 1.00 ? ? psyco: > 0.18 ? ? ratio: ?5.61 > time_properties: property_get ? ? ? ? ? ? ? ? ? plain: ? 0.95 ? ? psyco: > 0.10 ? ? ratio: ?9.26 > time_properties: property_set ? ? ? ? ? ? ? ? ? plain: ? 1.02 ? ? psyco: > 0.93 ? ? ratio: ?1.09 > time_properties: pyproperty_get ? ? ? ? ? ? ? ? plain: ? 1.54 ? ? psyco: > 0.14 ? ? ratio: 10.80 > time_properties: pyproperty_set ? ? ? ? ? ? ? ? plain: ? 1.04 ? ? psyco: > 0.93 ? ? ratio: ?1.12 > time_subdist ? : subdist(i) ? ? ? ? ? ? ? ? ? ? plain: ? 3.68 ? ? psyco: > 6.14 ? ? ratio: ?0.60 > time_sums ? ? ?: rounding ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.79 ? ? psyco: > 0.81 ? ? ratio: ?0.98 > > > Running new timings with original psyco > > time_anyall ? ?: all_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.18 ? ? psyco: > 2.25 ? ? ratio: ?0.97 > time_anyall ? ?: all_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.45 ? ? psyco: > 1.90 ? ? ratio: ?1.82 > time_anyall ? ?: all_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 1.88 ? ? psyco: > 1.94 ? ? ratio: ?0.97 > time_anyall ? ?: all_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.46 ? ? psyco: > 1.66 ? ? ratio: ?2.09 > time_anyall ? ?: all_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.68 ? ? psyco: > 0.09 ? ? ratio: ?7.34 > time_anyall ? ?: any_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.11 ? ? psyco: > 2.13 ? ? ratio: ?0.99 > time_anyall ? ?: any_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.79 ? ? psyco: > 1.93 ? ? ratio: ?1.96 > time_anyall ? ?: any_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 1.89 ? ? psyco: > 1.92 ? ? ratio: ?0.98 > time_anyall ? ?: any_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.23 ? ? psyco: > 1.68 ? ? ratio: ?1.92 > time_anyall ? ?: any_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.67 ? ? psyco: > 0.09 ? ? ratio: ?7.26 > time_builtins ?: chr(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.42 ? ? psyco: > 0.01 ? ? ratio: 197.10 > time_builtins ?: hash(i) ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 1.23 ? ? psyco: > 0.37 ? ? ratio: ?3.30 > time_builtins ?: int(round(f)) ? ? ? ? ? ? ? ? ?plain: ? 2.64 ? ? psyco: > 1.51 ? ? ratio: ?1.75 > time_builtins ?: min ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.54 ? ? psyco: > 0.52 ? ? ratio: ?1.03 > time_builtins ?: min_kw ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 4.42 ? ? psyco: > 4.40 ? ? ratio: ?1.00 > time_builtins ?: ord(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.31 ? ? psyco: > 0.00 ? ? ratio: 116.56 > time_builtins ?: pow ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.35 ? ? psyco: > 0.23 ? ? ratio: ?1.50 > time_builtins ?: reduce ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.73 ? ? psyco: > 0.71 ? ? ratio: ?1.03 > time_builtins ?: round(f) ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.71 ? ? psyco: > 0.89 ? ? ratio: ?1.91 > time_builtins ?: sums ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.18 ? ? psyco: > 0.18 ? ? ratio: ?1.03 > time_fib ? ? ? : matrix ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.43 ? ? psyco: > 0.36 ? ? ratio: ?1.19 > time_fib ? ? ? : recursive ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.00 ? ? psyco: > 0.00 ? ? ratio: 19.28 > time_fib ? ? ? : takahashi ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.41 ? ? psyco: > 0.32 ? ? ratio: ?1.27 > time_generators: send call loop 1000 ? ? ? ? ? ?plain: ? 2.65 ? ? psyco: > 2.94 ? ? ratio: ?0.90 > time_generators: send and loop 1000 ? ? ? ? ? ? plain: ? 2.95 ? ? psyco: > 2.54 ? ? ratio: ?1.16 > time_generators: send just many times ? ? ? ? ? plain: ? 1.30 ? ? psyco: > 0.59 ? ? ratio: ?2.23 > time_generators: iterate just many times ? ? ? ?plain: ? 0.65 ? ? psyco: > 0.55 ? ? ratio: ?1.17 > time_generators: call next just many times ? ? ?plain: ? 0.92 ? ? psyco: > 0.63 ? ? ratio: ?1.45 > time_iter ? ? ?: iter ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.45 ? ? psyco: > 0.59 ? ? ratio: ?2.47 > time_math ? ? ?: floats ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.92 ? ? psyco: > 1.50 ? ? ratio: ?1.95 > time_properties: method_get ? ? ? ? ? ? ? ? ? ? plain: ? 0.94 ? ? psyco: > 0.12 ? ? ratio: ?7.88 > time_properties: method_set ? ? ? ? ? ? ? ? ? ? plain: ? 1.01 ? ? psyco: > 0.17 ? ? ratio: ?6.08 > time_properties: property_get ? ? ? ? ? ? ? ? ? plain: ? 0.97 ? ? psyco: > 0.74 ? ? ratio: ?1.31 > time_properties: property_set ? ? ? ? ? ? ? ? ? plain: ? 1.02 ? ? psyco: > 0.92 ? ? ratio: ?1.12 > time_properties: pyproperty_get ? ? ? ? ? ? ? ? plain: ? 1.53 ? ? psyco: > 1.31 ? ? ratio: ?1.17 > time_properties: pyproperty_set ? ? ? ? ? ? ? ? plain: ? 1.02 ? ? psyco: > 0.92 ? ? ratio: ?1.11 > time_subdist ? : subdist(i) ? ? ? ? ? ? ? ? ? ? plain: ? 3.65 ? ? psyco: > 1.64 ? ? ratio: ?2.23 > time_sums ? ? ?: rounding ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.81 ? ? psyco: > 0.79 ? ? ratio: ?1.03 > > > > -- > http://mail.python.org/mailman/listinfo/python-list > From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 22:57:44 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 02:57:44 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > >> Nobody says you shouldn't check your data. Only that "assert" is not >> the right way to do that. > > "assert" is not the right way to check your *inputs*. It's a perfectly > reasonable way to check data which "should" be valid, as well as a way > to document what variables are supposed to contain. Where are those variables coming from? The distinction really boils down to this: * asserts should never fail. If there is any chance that an assertion might fail outside of test suites, then don't use assert. You can't control what input the caller provides to a function, so unless the data is generated inside the function, a caller might provide something unexpected. Therefore, assert is inappropriate for checking function parameters, even if that function is "private" and only called by your own functions. (1) So-called "private" or "internal" functions have a habit of becoming public, and then you have asserts in public functions. (2) If public() calls _private(x), and _private uses assert to check the value of x, there is a risk that if public() is buggy and supplies an invalid x, the assertion will never be checked and _private() will return incorrect results. (3) assert is absolutely unsuitable for enforcing pre-conditions and post- conditions, unless such conditions are mere "guidelines", because assert can be switched off at runtime. It's a fine line to draw. Obviously, if we think something *truly* can never fail, we don't bother checking it: def add(x, y): result = x+y assert result-y == x assert result-x == y return result is probably overkill for most programs, and yet surprisingly those assertions will commonly fail for float arguments! -- Steven From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 23:07:09 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 03:07:09 GMT Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > def list_items_in_string(list_items, string): > for item in list_items: > if item in string: > return True > return False ... > Any ideas how to make that function look nicer? :) Change the names. Reverse the order of the arguments. Add a docstring. Otherwise looks pretty nice to me. Simple, straightforward, and correct. If you're running Python 2.5 or better, then this is even shorter (and probably faster): def contains(s, targets): """Return True if any item of targets is in string s.""" return any(target in s for target in targets) -- Steven From tjreedy at udel.edu Thu Jul 9 23:07:34 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jul 2009 23:07:34 -0400 Subject: tough-to-explain Python In-Reply-To: <00720d76$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > And speaking of binary search: > > [quote] > I was shocked to learn that the binary search program that Bentley PROVED > CORRECT and SUBSEQUENTLY TESTED [emphasis added] in Chapter 5 of > "Programming Pearls" contains a bug. Once I tell you what the it is, you > will understand why it escaped detection for two decades. > [end quote] > > http://northernplanets.blogspot.com/2006/07/nearly-all-binary-searches-are-broken.html > > or http://tinyurl.com/nco6yv The actual report is at http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html The is the so called bug: "In Programming Pearls Bentley says that the analogous line "sets m to the average of l and u, truncated down to the nearest integer." On the face of it, this assertion might appear correct, but it fails for large values of the int variables low and high. Specifically, it fails if the sum of low and high is greater than the maximum positive int value (231 - 1). The sum overflows to a negative value, and the value stays negative when divided by two. In C this causes an array index out of bounds with unpredictable results. In Java, it throws ArrayIndexOutOfBoundsException." The is *not* a bug is Bentley program. It is a bug in bad, buggy, insane integer arithmetic implementations. low + high should either return the right answer, as it will in Python, or raise an overflow error. tjr From sjmachin at lexicon.net Thu Jul 9 23:07:58 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 9 Jul 2009 20:07:58 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: <347b30b2-48a9-49cd-8e33-172325d34a34@v15g2000prn.googlegroups.com> On Jul 10, 12:53?pm, Nobody wrote: > On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > > For one of my projects, I came across the need to check if one of many > > items from a list of strings could be found in a long string. > > If you need to match many strings or very long strings against the same > list of items, the following should (theoretically) be optimal: > > ? ? ? ? r = re.compile('|'.join(map(re.escape,list_items))) > ? ? ? ? ... > ? ? ? ? result = r.search(string) "theoretically optimal" happens only if the search mechanism builds a DFA or similar out of the list of strings. AFAIK Python's re module doesn't. Try this: http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/ From miki.tebeka at gmail.com Thu Jul 9 23:14:30 2009 From: miki.tebeka at gmail.com (lazy1) Date: Thu, 9 Jul 2009 20:14:30 -0700 (PDT) Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <128ab32f-3609-44a5-9fb3-1a7cb64f04e9@y10g2000prg.googlegroups.com> Hello Daniel, > Can someone recommend me a faster plotting library? I found out gnuplot to be blazing fast for many many points. I usually just call it using subprocess but there are Python bindings to it somewhere as well. HTH, -- Miki http://pythonwise.blogspot.com From http Thu Jul 9 23:24:54 2009 From: http (Paul Rubin) Date: 09 Jul 2009 20:24:54 -0700 Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: <7x63e1kynt.fsf@ruckus.brouhaha.com> inkhorn writes: > def list_items_in_string(list_items, string): > for item in list_items: > if item in string: > return True > return False You could write that as (untested): def list_items_in_string(list_items, string): return any(item in string for item in list_items) but there are faster algorithms you could use if the list is large and you want to do the test on lots of long strings, etc. From scriber77 at yahoo.com Thu Jul 9 23:25:13 2009 From: scriber77 at yahoo.com (scriber77 at yahoo.com) Date: Thu, 9 Jul 2009 20:25:13 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: On Jul 9, 7:54?pm, David Bernier wrote: > Musatov wrote: > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > its adopted use in Pakistan. ?The search engine has grown in > > popularity 10,000 fold following its Beta test launch three months ago > > in April, 2009. Supporters of the site claim it is better than rival > > Google upon which platform it is based. Controversy arose after > > MeAmI.org search code allowed users to search other users Google > > results with no advertising. "It is truly an innovative thing we are > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > the results of other Google users immediately results in a level of > > accuracy and relevance above and beyond Google." Google changed their > > API following the launch or MeAmI.org and explored the possibility of > > blocking site access from MeAmI.org to Google search results but was > > unable to do so. Critics of MeAmI.org say what it is doing is > > tantamount to intellectual theft. When asked about this topper Musatov > > exclaimed, "The Internet was made for people, not companies." An > > analyst at Goldman Sachs says, requesting to remain anonymous, > > "MeAmI.org has a strong presence in promoting itself as a vehicle for > > global activism and to tell you the truth, this makes it much more > > likely an acquisition target than potential intellectual property > > violator." Google could not be reached for comment. > > Mr. Musatov, ?do you know who originally wrote the > article above? > > Thank you. > > David Bernier- Hide quoted text - > > - Show quoted text - Hello, I am an anonymous Professor of Mathematics at a University in the Midwest. You see, I have been contacted by this "Musatov" fellow as well and posted a link he sent me to gather a second opinion. I was immediately attacked by a Mr. Tim Chow from M.I.T. as though I was him. I explained to him Mr. Musatov had contacted me, that I had read his simple and very light 3 page paper and said I thought the work in the equations was elegant and the mending of the binary with the traditional mathematics symbols inventive. Listen, I understand this "Musatov" character has annoyed a lot of you and apparently rightfully so, at least to some extent, has gained a bit of contempt from the community. But perhaps he is trying to see things a bit differently and is just not getting the feedback he needs, so he is throwing tantrums apparently across USENET. Like I said before, I am just trying to do right by this person who contacted me, and seemed to be a decent person with a genuine interest in mathematics. He was very respectful. Alas, I am at a loss on what specific feedback to give him on his paper as though I am a professor of Mathematics, Computer Science is not my specialty. I told him I would try to get him some feedback on the equations on page 3. Would any of you be so kind to help me? Here is the paper: http://MeAmI.org/pversusnp.pdf I do thank you for your time. Good day, Professor X From msjmb at tpg.com.au Thu Jul 9 23:28:15 2009 From: msjmb at tpg.com.au (MichaelW) Date: Thu, 9 Jul 2009 20:28:15 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <109bd130-12d6-458b-879b-fa26afab678d@l5g2000pra.googlegroups.com> On Jul 10, 1:25?pm, scribe... at yahoo.com wrote: > On Jul 9, 7:54?pm, David Bernier wrote: > > > > > > > Musatov wrote: > > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > > its adopted use in Pakistan. ?The search engine has grown in > > > popularity 10,000 fold following its Beta test launch three months ago > > > in April, 2009. Supporters of the site claim it is better than rival > > > Google upon which platform it is based. Controversy arose after > > > MeAmI.org search code allowed users to search other users Google > > > results with no advertising. "It is truly an innovative thing we are > > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > > the results of other Google users immediately results in a level of > > > accuracy and relevance above and beyond Google." Google changed their > > > API following the launch or MeAmI.org and explored the possibility of > > > blocking site access from MeAmI.org to Google search results but was > > > unable to do so. Critics of MeAmI.org say what it is doing is > > > tantamount to intellectual theft. When asked about this topper Musatov > > > exclaimed, "The Internet was made for people, not companies." An > > > analyst at Goldman Sachs says, requesting to remain anonymous, > > > "MeAmI.org has a strong presence in promoting itself as a vehicle for > > > global activism and to tell you the truth, this makes it much more > > > likely an acquisition target than potential intellectual property > > > violator." Google could not be reached for comment. > > > Mr. Musatov, ?do you know who originally wrote the > > article above? > > > Thank you. > > > David Bernier- Hide quoted text - > > > - Show quoted text - > > Hello, I am an anonymous Professor of Mathematics at a University in > the Midwest. You see, I have been contacted by this "Musatov" fellow > as well and posted a link he sent me to gather a second opinion. I was > immediately attacked by a Mr. Tim Chow from M.I.T. as though I was > him. I explained to him Mr. Musatov had contacted me, that I had read > his simple and very light 3 page paper and said I thought the work in > the equations was elegant and the mending of the binary with the > traditional mathematics symbols inventive. Listen, I understand this > "Musatov" character has annoyed a lot of you and apparently rightfully > so, at least to some extent, has gained a bit of contempt from the > community. But perhaps he is trying to see things a bit differently > and is just not getting the feedback he needs, so he is throwing > tantrums apparently across USENET. > > Like I said before, I am just trying to do right by this person who > contacted me, and seemed to be a decent person with a genuine interest > in mathematics. He was very respectful. > > Alas, I am at a loss on what specific feedback to give him on his > paper as though I am a professor of Mathematics, Computer Science is > not my specialty. > > I told him I would try to get him some feedback on the equations on > page 3. ?Would any of you be so kind to help me? > > Here is the paper:http://MeAmI.org/pversusnp.pdf > > I do thank you for your time. > > Good day, > > Professor X- Hide quoted text - > > - Show quoted text - Please see reply in the "Breathtaking..." thread. It will come as no surprise to anyone that "Professor X" and "The Masked Logician" share the same computer. MichaelW. From bcb at undisclosedlocation.net Thu Jul 9 23:28:46 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Thu, 9 Jul 2009 22:28:46 -0500 Subject: language analysis to enforce code standards References: Message-ID: "Jason S. Friedman" wrote in message news:mailman.2927.1247192026.8015.python-list at python.org... > Hello, I administer the Informatica ETL tool at my company. Part of that > role involves creating and enforcing standards. I want the Informatica > developers to add comments to certain key objects and I want to be able to > verify (in an automated fashion) that they have done so. > > I cannot merely check for non-emptiness; that is trivial to circumvent. On > the other hand, I probably do not need to be able to catch developers who > are determined to not create comments. There are not too many of them and > perhaps they will find it is easier to write a (useful) comment than to > game the system. > > Any thoughts on how I might proceed? Stated plainly, how can I tell when > a string more-or-less forms at least one phrase? Well, you *could* try analyzing the comment text using NLTK: http://www.nltk.org/ From bcb at undisclosedlocation.net Thu Jul 9 23:33:25 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Thu, 9 Jul 2009 22:33:25 -0500 Subject: AP -- MeAmI.org Paces Google References: Message-ID: <5Cy5m.121$Ly1.67@newsfe06.iad> wrote in message news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 23:41:43 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 03:41:43 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: On Thu, 09 Jul 2009 23:07:34 -0400, Terry Reedy wrote: > Steven D'Aprano wrote: > >> And speaking of binary search: >> >> [quote] >> I was shocked to learn that the binary search program that Bentley >> PROVED CORRECT and SUBSEQUENTLY TESTED [emphasis added] in Chapter 5 of >> "Programming Pearls" contains a bug. Once I tell you what the it is, >> you will understand why it escaped detection for two decades. [end >> quote] >> >> http://northernplanets.blogspot.com/2006/07/nearly-all-binary-searches- are-broken.html >> >> or http://tinyurl.com/nco6yv > > The actual report is at > http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about- it-nearly.html > > The is the so called bug: > "In Programming Pearls Bentley says that the analogous line "sets m to > the average of l and u, truncated down to the nearest integer." On the > face of it, this assertion might appear correct, but it fails for large > values of the int variables low and high. Specifically, it fails if the > sum of low and high is greater than the maximum positive int value (231 > - 1). The sum overflows to a negative value, and the value stays > negative when divided by two. In C this causes an array index out of > bounds with unpredictable results. In Java, it throws > ArrayIndexOutOfBoundsException." > > The is *not* a bug is Bentley program. Wow. That's an impressive set of typos :) > It is a bug in bad, buggy, insane > integer arithmetic implementations. low + high should either return the > right answer, as it will in Python, or raise an overflow error. Is binary search *supposed* to raise an overflow error if given more than 2**30 elements? If not, then you're solving a different problem: it's just like binary search, but it only works up to a maximum number of elements. Now perhaps that's a legitimate problem to solve, but unless you make the change in behaviour clear up front, your code has failed to live up to specifications and therefore is buggy. Is there something special about OverflowError that is "better" than ArrayIndexOutOfBoundsException? I don't have "Programming Pearls", and unfortunately the section on binary search is not online: http://netlib.bell-labs.com/cm/cs/pearls/sketch04.html but there's no reason to imagine that the book will assume -- or even state as a requirement for binary search -- that addition will never overflow. Far from it: it seems to me that the author is very aware of real world constraints, and in the early 1980s, BigInt types were not something you took as a given. Of course *binary search* as an algorithm is not broken. The bug was that "Programming Pearls" neglected to take into account overflow when you have more than 2**30 items. One way to take that into account is to insist on using a language like Python where addition can never overflow. But that's not the code presented in "Programming Pearls". -- Steven From marty.musatov at gmail.com Thu Jul 9 23:43:08 2009 From: marty.musatov at gmail.com (Musatov) Date: Thu, 9 Jul 2009 20:43:08 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: On Jul 9, 7:54?pm, David Bernier wrote: > Musatov wrote: > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > its adopted use in Pakistan. ?The search engine has grown in > > popularity 10,000 fold following its Beta test launch three months ago > > in April, 2009. Supporters of the site claim it is better than rival > > Google upon which platform it is based. Controversy arose after > > MeAmI.org search code allowed users to search other users Google > > results with no advertising. "It is truly an innovative thing we are > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > the results of other Google users immediately results in a level of > > accuracy and relevance above and beyond Google." Google changed their > > API following the launch or MeAmI.org and explored the possibility of > > blocking site access from MeAmI.org to Google search results but was > > unable to do so. Critics of MeAmI.org say what it is doing is > > tantamount to intellectual theft. When asked about this topper Musatov > > exclaimed, "The Internet was made for people, not companies." An > > analyst at Goldman Sachs says, requesting to remain anonymous, > > "MeAmI.org has a strong presence in promoting itself as a vehicle for > > global activism and to tell you the truth, this makes it much more > > likely an acquisition target than potential intellectual property > > violator." Google could not be reached for comment. > > Mr. Musatov, ?do you know who originally wrote the > article above? > > Thank you. > > David Bernier- Hide quoted text - > > - Show quoted text - Yes. From timr at probo.com Thu Jul 9 23:44:50 2009 From: timr at probo.com (Tim Roberts) Date: Thu, 09 Jul 2009 20:44:50 -0700 Subject: How to use Python to interface with Web pages? References: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Message-ID: Peter wrote: > >Any help would be appreciated :-) > >I want to write an auction sniping tool in Python. Please don't. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From timr at probo.com Thu Jul 9 23:50:30 2009 From: timr at probo.com (Tim Roberts) Date: Thu, 09 Jul 2009 20:50:30 -0700 Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: TheSeeker wrote: > >I am embarking on teaching myself Microsoft UI Automation using Python >as the scripting language. The University of North Carolina at Chapel Hill has a great Python package called pyAA that does exactly this: http://www.cs.unc.edu/Research/assist/developer.shtml http://mindtrove.info/articles/gui-automation-with-pyaa/ -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From scriptlearner at gmail.com Fri Jul 10 01:29:15 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Thu, 9 Jul 2009 22:29:15 -0700 (PDT) Subject: Looking for the right library for a simple HTTP client Message-ID: I am trying to implement a simple client that can do the following: 1)to send the following kinds of HTTP requests and validate responses 1.1 GET 1.2 POST with application/x-www-form-urlencoded encoding 1.3 POST with multipart/form-data encoding 2)to set any number of (even duplicate) headers. For example, I may intentionally add the following Cookie: headers to a request: Cookie: id_1=v1;Domain=sample.com;Path=/ Cookie: id_1=v1;Domain=sample.com;Path=/ <--same as the one above Cookie: id_2=v1;Domain=sample.com;Path=/ 3)to set proxy cfg so requests can go through a proxy server to reach the target server. 4)to send multiple requests simultaneously. I have Python 2.4.1 on Solaris 9 and 10, and I don't have any plan to upgrade to latest version of Python. I looked around and I found httplib and urllib. Are they sufficient for my tasks 1 to 3 above? Any good sample codes will be great. Thanks. From david250 at videotron.ca Fri Jul 10 01:45:36 2009 From: david250 at videotron.ca (David Bernier) Date: Fri, 10 Jul 2009 01:45:36 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > On Jul 9, 7:54 pm, David Bernier wrote: >> Musatov wrote: >>> Los Angeles (AP) --MeAmI.org now has users in 50 countries following >>> its adopted use in Pakistan. The search engine has grown in >>> popularity 10,000 fold following its Beta test launch three months ago >>> in April, 2009. Supporters of the site claim it is better than rival >>> Google upon which platform it is based. Controversy arose after >>> MeAmI.org search code allowed users to search other users Google >>> results with no advertising. "It is truly an innovative thing we are >>> doing," said Founder and CEO, Martin Musatov. "Letting users search >>> the results of other Google users immediately results in a level of >>> accuracy and relevance above and beyond Google." Google changed their >>> API following the launch or MeAmI.org and explored the possibility of >>> blocking site access from MeAmI.org to Google search results but was >>> unable to do so. Critics of MeAmI.org say what it is doing is >>> tantamount to intellectual theft. When asked about this topper Musatov >>> exclaimed, "The Internet was made for people, not companies." An >>> analyst at Goldman Sachs says, requesting to remain anonymous, >>> "MeAmI.org has a strong presence in promoting itself as a vehicle for >>> global activism and to tell you the truth, this makes it much more >>> likely an acquisition target than potential intellectual property >>> violator." Google could not be reached for comment. >> Mr. Musatov, do you know who originally wrote the >> article above? >> >> Thank you. >> >> David Bernier- Hide quoted text - >> >> - Show quoted text - > > Yes. Mr. Musatov, do you know the name of the person who originally wrote the article above? Thank you. David Bernier From scriber77 at yahoo.com Fri Jul 10 01:52:14 2009 From: scriber77 at yahoo.com (Floetry) Date: Thu, 9 Jul 2009 22:52:14 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: <5Cy5m.121$Ly1.67@newsfe06.iad> Message-ID: <803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3@2g2000prl.googlegroups.com> On Jul 9, 8:33?pm, "Bruce C. Baker" wrote: > wrote in message > > news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... > > > > Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! Any reason "ka"-snip? Binomial Theorem: page 3. http://MeAmI.org/pversusnp.pdf From s.deshe at gmail.com Fri Jul 10 02:18:03 2009 From: s.deshe at gmail.com (slamdunk) Date: Thu, 9 Jul 2009 23:18:03 -0700 (PDT) Subject: way for a function to understand whether it's being run through a OnCreate callback or not Message-ID: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> is there a way for a function to understand whether it's being run through a OnCreate callback or not? I have working functions that I want to recycle through the OnCreate but need to catch the "nuke.thisNode()" bit inside them so they can still function when called manually through other scripts functions. From bcb at undisclosedlocation.net Fri Jul 10 02:21:46 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Fri, 10 Jul 2009 01:21:46 -0500 Subject: AP -- MeAmI.org Paces Google References: <5Cy5m.121$Ly1.67@newsfe06.iad> <803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3@2g2000prl.googlegroups.com> Message-ID: "Floetry" wrote in message news:803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3 at 2g2000prl.googlegroups.com... On Jul 9, 8:33 pm, "Bruce C. Baker" wrote: > wrote in message > > news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... > > > > Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! Any reason "ka"-snip? Binomial Theorem: page 3. http://MeAmI.org/pversusnp.pdf _________________________________________________ Well, *Martin*, since you think I'm dead, it could only have been my "ka" that did the snipping. Surely a polymath of your vast accomplishments will have no problem with the religio-historical reference, eh, *Martin*? (I'm predicting that not only will you *not* recognize it, you'll find it beneath your notice. No fair googling, *Martin*!) From clp2 at rebertia.com Fri Jul 10 02:23:17 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 23:23:17 -0700 Subject: way for a function to understand whether it's being run through a OnCreate callback or not In-Reply-To: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> References: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> Message-ID: <50697b2c0907092323n6da421f1o9b3faec654bd88e7@mail.gmail.com> On Thu, Jul 9, 2009 at 11:18 PM, slamdunk wrote: > is there a way for a function to understand whether it's being run > through a OnCreate callback or not? > I have working functions that I want to recycle through the OnCreate > but need to catch the "nuke.thisNode()" bit inside them so they can > still function when called manually through other scripts functions. Your question lacks context. What's an OnCreate callback? Cheers, Chris -- http://blog.rebertia.com From gherron at islandtraining.com Fri Jul 10 02:59:22 2009 From: gherron at islandtraining.com (Gary Herron) Date: Thu, 09 Jul 2009 23:59:22 -0700 Subject: way for a function to understand whether it's being run through a OnCreate callback or not In-Reply-To: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> References: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> Message-ID: <4A56E6CA.5090802@islandtraining.com> slamdunk wrote: > is there a way for a function to understand whether it's being run > through a OnCreate callback or not? > I have working functions that I want to recycle through the OnCreate > but need to catch the "nuke.thisNode()" bit inside them so they can > still function when called manually through other scripts functions. > You've given us almost *NO* clue what you are talking about. I suppose I could Google "onCreate", and perhaps "nuke" and "thisNode", and see what falls out. But why would I? If you want our help, *you* take the time to tell is what you are asking about. You may want to start here: http://www.catb.org/~esr/faqs/smart-questions.html#intro Gary Herron From kf9150 at gmail.com Fri Jul 10 03:13:54 2009 From: kf9150 at gmail.com (Kelie) Date: Fri, 10 Jul 2009 00:13:54 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: On Jul 9, 5:50?pm, Tim Roberts wrote: > The University of North Carolina at Chapel Hill has a great Python package > called pyAA that does exactly this: > > http://www.cs.unc.edu/Research/assist/developer.shtmlhttp://mindtrove.info/articles/gui-automation-with-pyaa/ > -- > Tim Roberts, t... at probo.com Thanks Tim. From this page http://sourceforge.net/projects/uncassist/files/, there isn't a installer for Python2.5 or higher. Didn't find the source code there either. From marty.musatov at gmail.com Fri Jul 10 03:19:40 2009 From: marty.musatov at gmail.com (Musatov) Date: Fri, 10 Jul 2009 00:19:40 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: <5Cy5m.121$Ly1.67@newsfe06.iad> <803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3@2g2000prl.googlegroups.com> Message-ID: <1d58ce00-9436-4220-a7ad-fc586df6625c@h18g2000yqj.googlegroups.com> On Jul 9, 11:21?pm, "Bruce C. Baker" wrote: > "Floetry" wrote in message > > news:803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3 at 2g2000prl.googlegroups.com... > On Jul 9, 8:33 pm, "Bruce C. Baker" > wrote: > > > wrote in message > > >news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... > > > > > > Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! > > Any reason "ka"-snip? > > Binomial Theorem: page 3.http://MeAmI.org/pversusnp.pdf > > _________________________________________________ > > Well, *Martin*, since you think I'm dead, it could only have been my "ka" > that did the snipping. Surely a polymath of your vast accomplishments will > have no problem with the religio-historical reference, eh, *Martin*? (I'm > predicting that not only will you *not* recognize it, you'll find it beneath > your notice. No fair googling, *Martin*!) I did not Google. ka in ancient Egypt, the supposed spiritual part of an individual human being or god, which survived (with the soul) after death and could reside in a statue of the person. ELIZABETH KNOWLES. "ka." The Oxford Dictionary of Phrase and Fable. Oxford University Press. 2006. Encyclopedia.com. 10 Jul. 2009 . In a third class of religion?usually heavily interlaced with fetishism? magic, momentary and special deities, nature gods, and deities personifying natural functions (such as the Egyptian solar god Ra, the Babylonian goddess of fertility Ishtar, the Greek sea-god Poseidon, and the Hindu goddess of death and destruction *Ka*li) emerge and are incorporated into a system of mythology and ritual. Sometimes they take on distinctively human characteristics (see anthropomorphism). The axis of reason - equinox, mass, and time - the orbits of ...Et a basso profundo hashi-hashi-brek-a-time todo ka tiempo. > Vozacka dozvola macarana lambada ... (&?&@?&?&?@&?&)) P. -- Musatov http://MeAmI.org ... From martin at librador.com Fri Jul 10 03:30:48 2009 From: martin at librador.com (Martin Vilcans) Date: Fri, 10 Jul 2009 09:30:48 +0200 Subject: way for a function to understand whether it's being run through a OnCreate callback or not In-Reply-To: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> References: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> Message-ID: On Fri, Jul 10, 2009 at 8:18 AM, slamdunk wrote: > is there a way for a function to understand whether it's being run > through a OnCreate callback or not? > I have working functions that I want to recycle through the OnCreate > but need to catch the "nuke.thisNode()" bit inside them so they can > still function when called manually through other scripts functions. I suppose you're programming for The Foundry's Nuke. Python is used in lots of different contexts, and most people on this list use Python for something totally unrelated to video. As an attempt at answering your question, you can add a parameter to your function that tells from where it's called. I.e. you put True in the parameter when it is called from OnCreate, False when it is not. But that isn't a very good design. A function should not need to care from where it is called. You probably want to have the node as a parameter to the function instead of calling nuke.thisNode inside of it. I can't find documentation for the OnCreate function online. Do you mean that you can only call nuke.thisNode() from inside OnCreate? Here's my guess of what I think you want to do: def OnCreate(): # call your function with the current node as argument your_function(nuke.thisNode()) def your_function(node): # The function takes a node as an argument. # Do whatever you want here For more Nuke-specific questions, you'd probably get better results by asking on the Nuke-python mailing list: http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python -- martin at librador.com http://www.librador.com From dstanek at dstanek.com Fri Jul 10 03:34:05 2009 From: dstanek at dstanek.com (David Stanek) Date: Fri, 10 Jul 2009 03:34:05 -0400 Subject: Looking for the right library for a simple HTTP client In-Reply-To: References: Message-ID: On Fri, Jul 10, 2009 at 1:29 AM, scriptlearner at gmail.com wrote: > I am trying to implement a simple client that can do the following: > 1)to send the following kinds of HTTP requests and validate responses > 1.1 GET > 1.2 POST with application/x-www-form-urlencoded encoding > 1.3 POST with multipart/form-data encoding > > 2)to set any number of (even duplicate) headers. ?For example, I may > intentionally add the following Cookie: headers to a request: > Cookie: id_1=v1;Domain=sample.com;Path=/ > Cookie: id_1=v1;Domain=sample.com;Path=/ <--same as the one above > Cookie: id_2=v1;Domain=sample.com;Path=/ > > 3)to set proxy cfg so requests can go through a proxy server to reach > the target server. > > 4)to send multiple requests simultaneously. > > > I have Python 2.4.1 on Solaris 9 and 10, and I don't have any plan to > upgrade to latest version of Python. > > I looked around and I found httplib and urllib. ?Are they sufficient > for my tasks 1 to 3 above? ?Any good sample codes will be great. > Thanks. > -- > http://mail.python.org/mailman/listinfo/python-list > I like urllib2. The examples[1] are pretty good. [1] http://www.python.org/doc/2.4.1/lib/urllib2-examples.html -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From bhardwajjayesh at gmail.com Fri Jul 10 03:54:52 2009 From: bhardwajjayesh at gmail.com (jayesh bhardwaj) Date: Fri, 10 Jul 2009 00:54:52 -0700 (PDT) Subject: Httplib issues Message-ID: <017b8d15-6a4c-4a6c-8d92-1f8e8cbf4c6d@t33g2000yqe.googlegroups.com> Hi, i was trying to download file frm a terminal having apache with httplib manipulation. It worked, now i want to upload file to the terminal. Can this b done by httplib too? From lie.1296 at gmail.com Fri Jul 10 04:03:05 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 10 Jul 2009 08:03:05 GMT Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Fri?rik M?r J?nsson wrote: > Hi Rhodri, > >> It's only really a pitfall if you try to use the built-in after you've >> redefined it. That's the thing to keep an eye open for. > > You're right, but in cases where you're editing a codebase which you > didn't author entirely by yourself you may not be aware of that. > > That said, if the codebase you're working on is structured (short, > concise methods) you should be working with small, consumable scopes you > can inhale in entirety before modifying. > > Regards, > Fri?rik M?r But if you are responsible for a large codebase that you don't write yourself, it is doubtful that you're a real newbie. From lie.1296 at gmail.com Fri Jul 10 04:47:51 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 10 Jul 2009 08:47:51 GMT Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Simon Forman wrote: > On Thu, Jul 9, 2009 at 9:42 AM, Nick wrote: > >> fields = line.split() >> for i in range(len(fields)): >> fields[i] = float(fields[i]) > > > instead of the above code you could say: > > fields = [float(n) for n in in line.split()] > > Have fun getting back into python! :] (A lot has changed in the last few years) >>> fields = [float(n) for n in in line.split()] File "", line 1 fields = [float(n) for n in in line.split()] ^ SyntaxError: invalid syntax s/in in/in/ From jeremy+complangpython at jeremysanders.net Fri Jul 10 04:51:29 2009 From: jeremy+complangpython at jeremysanders.net (Jeremy Sanders) Date: Fri, 10 Jul 2009 09:51:29 +0100 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: tt-industries wrote: > Hi, > > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. So far I have been using the ploting module > of wxPython. However, it becomes unstable for more than 25000 points. > Can someone recommend me a faster plotting library? It would be really > cool if one could embed this in wxPython. If someone has an idea I > would be very glad about answer. Veusz can plot a line with that many points with that many points in a couple of seconds on my system. It's a bit faster without antialiasing, slower if you want to actually plot markers at each position. Jeremy -- Jeremy Sanders http://www.jeremysanders.net/ From deets at nospam.web.de Fri Jul 10 04:59:08 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 10 Jul 2009 10:59:08 +0200 Subject: Httplib issues In-Reply-To: <017b8d15-6a4c-4a6c-8d92-1f8e8cbf4c6d@t33g2000yqe.googlegroups.com> References: <017b8d15-6a4c-4a6c-8d92-1f8e8cbf4c6d@t33g2000yqe.googlegroups.com> Message-ID: <7boe6sF249o1gU1@mid.uni-berlin.de> jayesh bhardwaj schrieb: > Hi, > i was trying to download file frm a terminal having apache with > httplib manipulation. It worked, now i want to upload file to the > terminal. Can this b done by httplib too? "Upload file to the terminal" makes no sense to me. You can upload something to a server. You can run a terminal. You can run a ssh-session to a server in a terminal. You can't upload to a terminal. But you can use httplib (or better urllib2) to make http-post-requests to webservers. And last but not least the ubiquious http://catb.org/esr/faqs/smart-questions.html Diez From jeanmichel at sequans.com Fri Jul 10 05:11:33 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 10 Jul 2009 11:11:33 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <4A5705C5.6010301@sequans.com> Nobody wrote: > On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > > >> Nobody says you shouldn't check your data. Only that "assert" is not the >> right way to do that. >> > > "assert" is not the right way to check your *inputs*. It's a perfectly > reasonable way to check data which "should" be valid, as well as a way to > document what variables are supposed to contain. > > Maybe, one of the assert problem is a semantic issue. Assert is a convenient way to write in the code "This is a requirement, get lost if you don't fit in". However it seems we often forget that builtin asserts can be disabled. One possible solution for those who prefer to write assertions instead a 'if A then Exception' is to write their own assertion statement and **raise an exception** that will not be disabled at runtime. Jea-Michel From garabik-news-2005-05 at kassiopeia.juls.savba.sk Fri Jul 10 05:23:54 2009 From: garabik-news-2005-05 at kassiopeia.juls.savba.sk (garabik-news-2005-05 at kassiopeia.juls.savba.sk) Date: Fri, 10 Jul 2009 09:23:54 +0000 (UTC) Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: Tim Harig wrote: > On 2009-07-09, Alex Rosslyn wrote: >> I would like to learn a way of changing the colour of a particular >> part of the output text. I've tried the following > On Unix operating systems this would be done through the curses interface: > > http://docs.python.org/library/curses.html Or using ANSI colour codes: colours = { 'none' : "", 'default' : "\033[0m", 'bold' : "\033[1m", 'underline' : "\033[4m", 'blink' : "\033[5m", 'reverse' : "\033[7m", 'concealed' : "\033[8m", 'black' : "\033[30m", 'red' : "\033[31m", 'green' : "\033[32m", 'yellow' : "\033[33m", 'blue' : "\033[34m", 'magenta' : "\033[35m", 'cyan' : "\033[36m", 'white' : "\033[37m", 'on_black' : "\033[40m", 'on_red' : "\033[41m", 'on_green' : "\033[42m", 'on_yellow' : "\033[43m", 'on_blue' : "\033[44m", 'on_magenta' : "\033[45m", 'on_cyan' : "\033[46m", 'on_white' : "\033[47m", 'beep' : "\007", # non-standard attributes, supported by some terminals 'dark' : "\033[2m", 'italic' : "\033[3m", 'rapidblink' : "\033[6m", 'strikethrough': "\033[9m", } print colours['red'], 'this is red', colours['blue'], 'blue', colours['on_green'], 'and with a green background', colours['default'] -- ----------------------------------------------------------- | Radovan Garab?k http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk | ----------------------------------------------------------- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread! From roland at catalogix.se Fri Jul 10 05:33:25 2009 From: roland at catalogix.se (Roland Hedberg) Date: Fri, 10 Jul 2009 11:33:25 +0200 Subject: A zlib question Message-ID: <80F42625-5FB2-438F-AFF2-913236A6D336@catalogix.se> Hi! I have a problem with zlib and compressing/decompressing according to RFC 1951. It seems like I can decompress, something compressed according to RFC 1951 by someone else, provided I set wbits to something negative (used -8 but I guess any negative number would work?). But how can I compress using zlib so it doesn't add a gzip header ? -- Roland From jeanmichel at sequans.com Fri Jul 10 05:39:54 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 10 Jul 2009 11:39:54 +0200 Subject: the ultimate logging facility for debugging code Message-ID: <4A570C6A.2060904@sequans.com> Greetings, Sorry for the dubious title :o). I was wandering if there is a standard (or reliable) python module that implements the following feature: http://code.activestate.com/recipes/198078/ >>>Recipe 198078: Wrapping method calls (meta-class example) >>> >>>A metaclass is used to wrap all (or just some) methods for logging purposes. The underlying mechanism can be used as well to check pre/post conditions, attribute access,... The basic point is, that the actual class must not be changed in any way to achive the desired effect. I had to adapt the code to some of my needs, but now it's almost working, by simply defining a metaclass for any of my class, I get the class methods calls logged with the instance that called the method, the method name, the call line in the file, the parameter **value** passed to the method and the returned value. It just saved me from hours of ipython interactive debugging with the old print way, and to beauty of it: I don't have to write any "self._log('Please log me')" line. As it is so useful, I bet there is a module that is doing this in a more reliable way that I did. Anyone has heard of it ? Jean-Michel From jhinak.sen at gmail.com Fri Jul 10 05:41:03 2009 From: jhinak.sen at gmail.com (jhinak sen) Date: Fri, 10 Jul 2009 15:11:03 +0530 Subject: help me to find the error Message-ID: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> hi, i am a beginner in python language, i am trying with this programme : to find the addition and mean from a data set in a file and writing the mean and sum in some other file : "" *#! /usr/bin/env python import re import cPickle as p import math from numpy import * f0= open("temp9","r+").readlines() f2= open("out1","r+") add_1=[ ]; for i in range(0, len(f0)): f1=f0[i].split() add= float(f1[1])+float(f1[2]) mean= float(add)/2 print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), repr(mean).ljust(7) add_1.append(add) add_1.append(mean) f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % repr(mean).ljust(7)) print "printing from file" for i in range(0, len(add_1),2): print add_1[i]," ", add_1[i+1] f0.close() f2.close()* "" and this programme is givving me this error : "" *Traceback (most recent call last): File "./temporary1.py", line 24, in f0.close() AttributeError: 'list' object has no attribute 'close'* "" please help to to find the error. or suggest some simpler or better way note: 1)file "temp9" is already exist 2)this programme is giving me all my outputs, but at the end of the out ..its giving me that error. -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 10 05:53:15 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 10 Jul 2009 17:53:15 +0800 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: <2d56febf0907100253m55cca65eo1889431de5b9835d@mail.gmail.com> I'm new to Python myself, but I think it's because the method readlist() returns a list. Hence, f0 is typed 'list'. And lists can't be closed. If you leave it to: *f0= open("temp9","r+") *and then assign another variable to the readlist, such as: *lines = f0.readlines()* Then in the for loop, change f1 to *f1=lines[i].split()* That may just stop it from complaining. Untested, but logically it's sound. Hope that works, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Fri, Jul 10, 2009 at 5:41 PM, jhinak sen wrote: > hi, > i am a beginner in python language, > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the > mean and sum in some other file : > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * > > f0= open("temp9","r+").readlines() > f2= open("out1","r+") > add_1=[ ]; > for i in range(0, len(f0)): > f1=f0[i].split() > add= float(f1[1])+float(f1[2]) > mean= float(add)/2 > print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), > repr(mean).ljust(7) > add_1.append(add) > add_1.append(mean) > f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % > repr(mean).ljust(7)) > print "printing from file" > for i in range(0, len(add_1),2): > print add_1[i]," ", add_1[i+1] > > f0.close() > f2.close()* > > > "" > > and this programme is givving me this error : > > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" > > please help to to find the error. > or suggest some simpler or better way > > note: > 1)file "temp9" is already exist > 2)this programme is giving me all my outputs, but at the end of the out > ..its giving me that error. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gabriel.rossetti at arimaz.com Fri Jul 10 05:57:23 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 10 Jul 2009 11:57:23 +0200 Subject: Threading.Condition problem Message-ID: <4A571083.8030403@arimaz.com> Hello everyone, I wrote a small example that listens for xmpp msgs in a thread. The main program calls a function that blocks (using Condition.wait) until a msg has been received and then returns the msg. When a msg arrives, it is put in a variable in the thread's object, it then calls the notify() attr on the Condition object. For some reason, this doesn't work, the thread gets the msg, tries to notify the Condition object, fails because the lock has not been acquired yet and blocks. I tried ignoring the failure, thinking that since it has not been acquired yet then when it is, it will get the msg right away and never call Condition.wait, thus not causing any problems, but this does not work either. Does someone know what I am doing wrong? I attached the code to this msg. Thank you, Gabriel -------------- next part -------------- A non-text attachment was scrubbed... Name: xmpp_client.py Type: text/x-python Size: 6127 bytes Desc: not available URL: From bearophileHUGS at lycos.com Fri Jul 10 05:58:33 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 10 Jul 2009 02:58:33 -0700 (PDT) Subject: psyco V2 beta2 benchmark References: Message-ID: larudwer, is that time_subdist subdist(i) a bad worsening? Something to be fixed in Psyco2? Bye, bearophile From sebastian.schabe at gmx.de Fri Jul 10 06:01:51 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Fri, 10 Jul 2009 12:01:51 +0200 Subject: Concatenating images (numpy arrays), but they look like HSV images In-Reply-To: References: <4a5629ba$1@news.fhg.de> Message-ID: <4a5710ea$1@news.fhg.de> Robert Kern schrieb: > Probably, you need to use zeros(..., dtype=uint8). When you use > dtype=int, that will result in dtype=int arrays. I suspect that > matplotlib is then interpreting that to mean that you want it to treat > the input as scalar data (which it will pass through a colormap) rather > than an RGB image. > Thanks Robert, that was exactly the problem. Now I'am really wondering how one can know such details. Well, with your answer I searched again in the mathplotlib documentation and under the function imshow(X, ...) I indeed found the hint, that X has to be an uint8 or float array or PIL image, but before I hadn't known where to search. So again, thank you Sebastian From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 10 06:14:26 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 10 Jul 2009 12:14:26 +0200 Subject: property using a classmethod In-Reply-To: References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> <4a55e8e0$0$8041$426a34cc@news.free.fr> Message-ID: <4a571483$0$19592$426a74cc@news.free.fr> Lie Ryan a ?crit : > Bruno Desthuilliers wrote: >> Lie Ryan a ?crit : >>> Emanuele D'Arrigo wrote: >> (snip) >>>> Ultimately all I want is a non-callable class-level attribute >>>> MyClass.myProperty that gives the result of MyClass.myClassMethod(). >>> This works like what you seem to want (it's ugly): >> Ugly, indeed. And an extreme case of arbitrary overcomplexification too :-/ >> >> (snip rube goldberg code) >> > > Can't think of anything simpler than that without meddling with > descriptor. Hmmm... Rereading the OP's spec, I guess you understood it better than I did - seems the OP wants to be able to call the "property" on the class object itself - which won't work with the builtin property type. So my own proposed solution won't do :-/ But still, "meddling with descriptor" is *way* simpler than your proposed solution. Here's a simple non-binding descriptor that do the job: class ClsProperty(object): def __init__(self, fget): if not isinstance(fget, (classmethod, staticmethod)): raise ValueError( "fget must be a classmethod or staticmethod" ) self.fget = fget def __get__(self, obj, cls=None): if cls is None: assert obj is not None cls = type(obj) return self.fget.__get__(obj, cls)() # example use class Foo(object): @classmethod def bar(cls): return "%s.bar" % cls.__name__ quux = ClsProperty(bar) > I'm not even sure descriptor can help here as it seems > descriptor needs an instance? wrt/ descriptors, no, they don't "need" an instance, at least for non-binding descriptors. Else, MyClass.MyMethod would return the MyClass.__dict__['MyMethod'] function, not an unbound method object !-) From jhinak.sen at gmail.com Fri Jul 10 06:20:47 2009 From: jhinak.sen at gmail.com (jhinak sen) Date: Fri, 10 Jul 2009 15:50:47 +0530 Subject: Python-list Digest, Vol 70, Issue 142 In-Reply-To: References: Message-ID: <3d4ff96c0907100320s346403baga8795d0c61cd54d7@mail.gmail.com> hey Xavier thnx a lot :) its working and ya.. i got your pont regarding typecasting . jhinak On Fri, Jul 10, 2009 at 3:30 PM, wrote: > 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. A zlib question (Roland Hedberg) > 2. Re: help me to find the error (Xavier Ho) > 3. Re: Concatenating images (numpy arrays), but they look like > HSV images (Sebastian Schabe) > > > ---------- Forwarded message ---------- > From: Roland Hedberg > To: python-list at python.org > Date: Fri, 10 Jul 2009 11:33:25 +0200 > Subject: A zlib question > Hi! > > I have a problem with zlib and compressing/decompressing according to RFC > 1951. > > It seems like I can decompress, something compressed according to RFC 1951 > by someone else, provided I set wbits to something negative (used -8 but I > guess any negative number would work?). > > But how can I compress using zlib so it doesn't add a gzip header ? > > -- Roland > > > ---------- Forwarded message ---------- > From: Xavier Ho > To: python-list at python.org > Date: Fri, 10 Jul 2009 17:53:15 +0800 > Subject: Re: help me to find the error > I'm new to Python myself, but I think it's because the method readlist() > returns a list. Hence, f0 is typed 'list'. And lists can't be closed. > > If you leave it to: > > *f0= open("temp9","r+") > > *and then assign another variable to the readlist, such as: > > *lines = f0.readlines()* > > Then in the for loop, change f1 to > > *f1=lines[i].split()* > > That may just stop it from complaining. > > Untested, but logically it's sound. > > Hope that works, > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > > On Fri, Jul 10, 2009 at 5:41 PM, jhinak sen wrote: > >> hi, >> i am a beginner in python language, >> >> i am trying with this programme : >> to find the addition and mean from a data set in a file and writing the >> mean and sum in some other file : >> "" >> *#! /usr/bin/env python >> >> import re >> import cPickle as p >> import math >> from numpy import * >> >> f0= open("temp9","r+").readlines() >> f2= open("out1","r+") >> add_1=[ ]; >> for i in range(0, len(f0)): >> f1=f0[i].split() >> add= float(f1[1])+float(f1[2]) >> mean= float(add)/2 >> print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), >> repr(mean).ljust(7) >> add_1.append(add) >> add_1.append(mean) >> f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % >> repr(mean).ljust(7)) >> print "printing from file" >> for i in range(0, len(add_1),2): >> print add_1[i]," ", add_1[i+1] >> >> f0.close() >> f2.close()* >> >> >> "" >> >> and this programme is givving me this error : >> >> "" *Traceback (most recent call last): >> File "./temporary1.py", line 24, in >> f0.close() >> AttributeError: 'list' object has no attribute 'close'* >> "" >> >> please help to to find the error. >> or suggest some simpler or better way >> >> note: >> 1)file "temp9" is already exist >> 2)this programme is giving me all my outputs, but at the end of the out >> ..its giving me that error. >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > > ---------- Forwarded message ---------- > From: Sebastian Schabe > To: python-list at python.org > Date: Fri, 10 Jul 2009 12:01:51 +0200 > Subject: Re: Concatenating images (numpy arrays), but they look like HSV > images > Robert Kern schrieb: > >> Probably, you need to use zeros(..., dtype=uint8). When you use dtype=int, >> that will result in dtype=int arrays. I suspect that matplotlib is then >> interpreting that to mean that you want it to treat the input as scalar data >> (which it will pass through a colormap) rather than an RGB image. >> >> > Thanks Robert, that was exactly the problem. Now I'am really wondering how > one can know such details. Well, with your answer I searched again in the > mathplotlib documentation and under the function imshow(X, ...) I indeed > found the hint, that X has to be an uint8 or float array or PIL image, but > before I hadn't known where to search. > > So again, thank you > > Sebastian > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 10 06:38:49 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 10 Jul 2009 12:38:49 +0200 Subject: property using a classmethod In-Reply-To: <4a55db9e$0$10243$426a74cc@news.free.fr> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> <4a55db9e$0$10243$426a74cc@news.free.fr> Message-ID: <4a571a39$0$29561$426a74cc@news.free.fr> Bruno Desthuilliers a ?crit : (snip) > You could write your own custom descriptor. Or just use an additional > level of indirection, ie: > > myProperty = property(lambda self: self.myClassMethod()) > Sorry, looks like I didn't read carefully enough. The above code won't work if you intend to lookup the property directly on the class object, ie "MyClass.myProperty". If that was your intention, you'll need a custom descriptor. The following code should do the job, or at least get you started: # python 2.5.x # the custom (non binding) descriptor class ClsProperty(object): def __init__(self, fget): if not isinstance(fget, (classmethod, staticmethod)): # XXX better error message raise ValueError( "fget must be a classmethod or staticmethod" ) self.fget = fget def __get__(self, obj, cls=None): if cls is None: assert obj is not None cls = type(obj) return self.fget.__get__(obj, cls)() # helper -> a simple decorator def classproperty(func): if not isinstance(func, (classmethod, staticmethod)): func = classmethod(func) return ClsProperty(func) # example use class Foo(object): # the hard way @classmethod def bar(cls): return "%s.bar" % cls.__name__ quux = ClsProperty(bar) # the simple way @classproperty def baaz(cls): return "%s.baaz" % cls Given your example, this should be enough. If you need a binding descriptor (one with a setter), you'll have to implement the __set__ method (and possibly __del__). Google for "python descriptor" to find more doc about the descriptor protocol. HTH From mail at microcorp.co.za Fri Jul 10 06:54:21 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 10 Jul 2009 12:54:21 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <001401ca014c$ca414d60$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > >>> persistent idea "out there" that programming is a very accessible >>> skill, like cooking or gardening, anyone can do it, and even profit >>> from it, monetarily or otherwise, etc., and to some extent I am >> >> Programming is not like any other human activity. > >In practice? In principle? Programming in principle is not the same as it >is performed in practice. > >But in either case, programming requires both the logical reasoning of >mathematics and the creativity of the arts. Funnily enough, I do not buy this arty creativity stuff. - or are you talking about making a website look pretty? >mathematicians will tell you that mathematics requires the same, and so >will the best artists. I think mathematicians, engineers, artists, even >great chefs, will pour scorn on your claim that programming is not like >any other human activity. So a chef is now an authority on programming? Programming is actually kind of different - almost everything else is just done, at the time that you do it. Programming is creating stuff that is completely useless until it is fed into something that uses it, to do something else, in conjuction with the thing it is fed into, at a later time. This is a highly significant difference, IMHO. > > >[...] >> He talks about how "when all is said and done, the only thing computers >> can do for us is to manipulate symbols and produce results of such >> manipulations" and he emphasises the "uninterpreted" nature of >> mechanical symbol manipulation, i.e. that the machine is doing it >> mindlessly. > >"Manipulate symbols" is so abstract as to be pointless. By that >reasoning, I can build a "computer" consisting of a box open at the top. >I represent a symbol by an object (say, a helium-filled balloon, or a >stone), instead of a pattern of bits. I manipulate the symbol by holding >the object over the box and letting go. If it flies up into the sky, that >represents the symbol "Love is War", if it falls into the box, it >represents the symbol "Strength is Blue", and if it just floats there, it >represents "Cheddar Cheese". This is a deterministic, analog computer >which manipulates symbols. Great. > >And utterly, utterly useless. So what is my computer lacking that real >computers have? When you have answered that question, you'll see why >Dijkstra's claim is under-specified. > So if computers do not manipulate symbols, what is it that they do? They sure cannot think, or drink, or reason, or almost any verb you can think of. "Manipulating symbols" is actually an elegant definition. Try coming up with a better one and you will see. And calling an abstraction pointless kind of contradicts what you say later... 8<------- camel humps and other stuff ------------------- - Hendrik From gabriel.rossetti at arimaz.com Fri Jul 10 07:09:28 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 10 Jul 2009 13:09:28 +0200 Subject: Threading.Condition problem In-Reply-To: <4A571083.8030403@arimaz.com> References: <4A571083.8030403@arimaz.com> Message-ID: <4A572168.90201@arimaz.com> The previous msg w/ attached code is the wrong code, please use the code attached to this msg, thank you and sorry for this. Gabriel Gabriel Rossetti wrote: > Hello everyone, > > I wrote a small example that listens for xmpp msgs in a thread. The > main program calls a function that blocks (using Condition.wait) until > a msg has been received and then returns the msg. When a msg arrives, > it is put in a variable in the thread's object, it then calls the > notify() attr on the Condition object. For some reason, this doesn't > work, the thread gets the msg, tries to notify the Condition object, > fails because the lock has not been acquired yet and blocks. I tried > ignoring the failure, thinking that since it has not been acquired yet > then when it is, it will get the msg right away and never call > Condition.wait, thus not causing any problems, but this does not work > either. Does someone know what I am doing wrong? I attached the code > to this msg. > > Thank you, > Gabriel -------------- next part -------------- A non-text attachment was scrubbed... Name: test_xmpp.py Type: text/x-python Size: 1878 bytes Desc: not available URL: From gabriel.rossetti at arimaz.com Fri Jul 10 07:09:42 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 10 Jul 2009 13:09:42 +0200 Subject: Threading.Condition problem Message-ID: <4A572176.9070100@arimaz.com> Sorry if this appears twice, I sent it once with an attachment and it never arrived so maybe the attachment is posing problems. I inlined the code this time (at the bottom), thank you, Gabriel ########################## Original message ############################ Hello everyone, I wrote a small example that listens for xmpp msgs in a thread. The main program calls a function that blocks (using Condition.wait) until a msg has been received and then returns the msg. When a msg arrives, it is put in a variable in the thread's object, it then calls the notify() attr on the Condition object. For some reason, this doesn't work, the thread gets the msg, tries to notify the Condition object, fails because the lock has not been acquired yet and blocks. I tried ignoring the failure, thinking that since it has not been acquired yet then when it is, it will get the msg right away and never call Condition.wait, thus not causing any problems, but this does not work either. Does someone know what I am doing wrong? I attached the code to this msg. Thank you, Gabriel ############################ Example code ############################ from __future__ import with_statement import xmpp, sys from threading import Thread, Condition, Event class Listener(Thread): def __init__(self, ws): Thread.__init__(self) self.interrupt = Event() self.message = None self._cv = ws._cv self.client = ws._client self.client.RegisterHandler('message', self.onMessage) def onMessage(self, conn, msg): self.message = msg try: self._cv.notify() except RuntimeError: print "self._cv has not acquired the lock yet" def getMsg(self): return self.message def run(self): try: while(not self.interrupt.isSet()): self.client.Process(1) except KeyboardInterrupt: return 0 class WS(object): def __init__(self, username, password, res): self._jid = xmpp.protocol.JID(username) self._client = xmpp.Client(self._jid.getDomain()) self._cv = Condition() if(self._client.connect(server=("localhost", 5222)) == ""): raise Exception("Error while connecting!") if(self._client.auth(self._jid.getNode(), password, res) is None): raise Exception("Authentication failed!") self._client.sendInitPresence() self._listener = Listener(self) self._listener.start() def getMsg(self, mid=None): """ """ with self._cv: res = self._listener.getMsg() while not res: self._cv.wait() res = self._listener.getMsg() return res if(__name__ == "__main__"): ws = WS("test at localhost", "123", "test") res = ws.getMsg() print "I just received : %s" % str(res) sys.exit(0) From ben+python at benfinney.id.au Fri Jul 10 07:30:41 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jul 2009 21:30:41 +1000 Subject: subprocess + python-daemon - bug/problem? References: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> <828acd8d-a9bd-4506-b695-e537af4587d8@l31g2000yqb.googlegroups.com> Message-ID: <873a94ojvi.fsf@benfinney.id.au> Andy Clegg writes: > "import daemon > import subprocess > > daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() > subprocess.Popen(['echo','1']).wait()" > > However the error remains the same. The error appears in the file specified for the stderr output of the DaemonContext. Here it is without the unwanted extra line-wrapping that seems to plague all Google Mail users (seriously, folks: get a real mail provider that won't mangle your messages):: Traceback (most recent call last): File "/home/bignose/Projects/python/python-daemon/bin/andy-clegg-test", line 7, in subprocess.Popen(['echo', '1']).wait() File "/usr/lib/python2.5/subprocess.py", line 1184, in wait pid, sts = self._waitpid_no_intr(self.pid, 0) File "/usr/lib/python2.5/subprocess.py", line 1014, in _waitpid_no_intr return os.waitpid(pid, options) OSError: [Errno 10] No child processes I confirm that I'm getting the same error; Python 2.5.4. My first thought was perhaps it's related to the fact that the process has no stdout? But setting ?stdout? and ?stderr? to the same file:: #! /usr/bin/python import daemon import subprocess fake_console = open("fake_console.txt", "w+") daemon.DaemonContext(stdout=fake_console, stderr=fake_console).open() subprocess.Popen(['echo', '1']).wait() still gives the same error:: 1 Traceback (most recent call last): File "/home/bignose/Projects/python/python-daemon/bin/andy-clegg-test", line 8, in subprocess.Popen(['echo', '1']).wait() File "/usr/lib/python2.5/subprocess.py", line 1184, in wait pid, sts = self._waitpid_no_intr(self.pid, 0) File "/usr/lib/python2.5/subprocess.py", line 1014, in _waitpid_no_intr return os.waitpid(pid, options) OSError: [Errno 10] No child processes I'm not familiar enough with the nuances of the ?subprocess? module to know what might be going wrong here. I'd like to know whether it might be a problem in the ?python-daemon? library. -- \ ?Whatever a man prays for, he prays for a miracle. Every prayer | `\ reduces itself to this: ?Great God, grant that twice two be not | _o__) four.?? ?Ivan Turgenev | Ben Finney From vox2000 at gmail.com Fri Jul 10 07:46:52 2009 From: vox2000 at gmail.com (vox) Date: Fri, 10 Jul 2009 04:46:52 -0700 (PDT) Subject: Query regarding set([])? Message-ID: Hi, I'm contsructing a simple compare-script and thought I would use set ([]) to generate the difference output. But I'm obviosly doing something wrong. file1 contains 410 rows. file2 contains 386 rows. I want to know what rows are in file1 but not in file2. This is my script: s1 = set(open("file1")) s2 = set(open("file2")) s3 = set([]) s1temp = set([]) s2temp = set([]) s1temp = set(i.strip() for i in s1) s2temp = set(i.strip() for i in s2) s3 = s1temp-s2temp print len(s3) Output is 119. AFAIK 410-386=24. What am I doing wrong here? BR, Andy From davea at ieee.org Fri Jul 10 07:48:49 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 07:48:49 -0400 Subject: can i write a assemly language programs in python In-Reply-To: References: <4A55F392.3080404@ieee.org> Message-ID: <4A572AA1.7010207@ieee.org> Terry Reedy wrote: >
Dave > Angel wrote: >> m.reddy prasad reddy wrote: >>> can any one tell me how to write assembly language programs in >>> python...if >>> no is there any other way to write the programs in python >>> >>> Reddi prasad reddy >>> ph.no:09958083797 >>> >>> >> Assembly language is a different programming language than Python. >> You can use both in the same process, much in the same way you can >> use C or C++ with Python. In fact, some of the system DLL's are >> written (partly) in assembler, though mostly in C or C++. > > It is possible that he meant how to write assembly *with* python. > Or how to translate python to assembly. > > As it turns out, CPython translates Python to byte code and has a dis > (assembly) module that produces very nice assembly code ;-) > So that is one answer to his question. > >> You'll need to tell us what your real goal is. > > Definitely. > > tjr > > >
> If you mean dis.dis() that only gives you byte code. I assumed he was referring to native code assembly. I guess I'd better stop guessing . DaveA From __peter__ at web.de Fri Jul 10 08:04:35 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 14:04:35 +0200 Subject: Query regarding set([])? References: Message-ID: vox wrote: > I'm contsructing a simple compare-script and thought I would use set > ([]) to generate the difference output. But I'm obviosly doing > something wrong. > > file1 contains 410 rows. > file2 contains 386 rows. > I want to know what rows are in file1 but not in file2. > > This is my script: > s1 = set(open("file1")) > s2 = set(open("file2")) Remove the following three lines: > s3 = set([]) > s1temp = set([]) > s2temp = set([]) > s1temp = set(i.strip() for i in s1) > s2temp = set(i.strip() for i in s2) > s3 = s1temp-s2temp > > print len(s3) > > Output is 119. AFAIK 410-386=24. What am I doing wrong here? You are probably misinterpreting len(s3). s3 contains lines occuring in "file1" but not in "file2". Duplicate lines are only counted once, and the order doesn't matter. So there are 119 lines that occur at least once in "file2", but not in "file1". If that is not what you want you have to tell us what exactly you are looking for. Peter From paul at boddie.org.uk Fri Jul 10 08:07:21 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Fri, 10 Jul 2009 05:07:21 -0700 (PDT) Subject: psyco V2 beta2 benchmark References: Message-ID: <35dcc79b-4f9d-4f16-942e-7cee2a470844@t13g2000yqt.googlegroups.com> On 10 Jul, 04:54, Zac Burns wrote: > Where do you get this beta? I heard that Psyco V2 is coming out but > can't find anything on their site to support this. I found the Subversion repository from the Psyco site: http://psyco.sourceforge.net/ -> http://codespeak.net/svn/psyco/dist/ -> http://codespeak.net/svn/psyco/v2/ It's not widely advertised, but I imagine that this is the correct repository. Navigating around on the codespeak.net site took me to the ViewVC instance which gives some date/time information that would confirm my suspicions: https://codespeak.net/viewvc/psyco/ A log of the development can be viewed here: https://codespeak.net/viewvc/psyco/v2/dist/?view=log Paul From mondi at cs.unibo.it Fri Jul 10 08:10:04 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Fri, 10 Jul 2009 12:10:04 +0000 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: Roger Binns wrote: > jacopo mondi wrote: >> Hi all, I need to patch socketmodule.c (the _socket module) in order to >> add support to an experimental socket family. > > You may find it considerably easier to use ctypes since that will avoid > the need for any patching. You'll also be able to control how read and > write are done (eg read vs recvfrom vs recvmsg vs readv). You can use > os.fdopen to convert your raw file descriptor into a Python file object > if appropriate. > > If you do use ctypes then you'll only need to distribute pure Python > source code. > > Roger Thanks a lot, I'll give it a try.. cheers jacopo From rhodri at wildebst.demon.co.uk Fri Jul 10 08:22:31 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 10 Jul 2009 13:22:31 +0100 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: On Fri, 10 Jul 2009 10:41:03 +0100, jhinak sen wrote: > hi, > i am a beginner in python language, > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the > mean > and sum in some other file : > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * > > f0= open("temp9","r+").readlines() > f2= open("out1","r+") > add_1=[ ]; > for i in range(0, len(f0)): > f1=f0[i].split() > add= float(f1[1])+float(f1[2]) > mean= float(add)/2 > print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), > repr(mean).ljust(7) > add_1.append(add) > add_1.append(mean) > f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % > repr(mean).ljust(7)) > print "printing from file" > for i in range(0, len(add_1),2): > print add_1[i]," ", add_1[i+1] > > f0.close() > f2.close()* > > > "" > > and this programme is givving me this error : > > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" As the error message tells you, 'f0' is a list. readlines() returns a list of the lines in the file object, rather than the file object itself. Since you promptly (and entirely reasonably) discard the file object, you haven't got any way to close() it. You don't actually need to do that, though. You can use a file object as something you use a 'for' loop over, and be given one line at a time. So instead you can write your code like this: f0 = open("temp9", "r") for line in f0: f1 = line.split() # ... and so on Notice that I only opened the file as "r" -- you are only reading it, not updating it, and giving yourself more permission than you need is a bad habit to get into. Opening the file this way means that if you accidentally write f0.write("something") instead of "f2.write(something)", Python will stop you with an exception rather than silently trash your data. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Fri Jul 10 08:25:49 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 10 Jul 2009 13:25:49 +0100 Subject: need to write a assembly progrm In-Reply-To: References: Message-ID: On Thu, 09 Jul 2009 11:52:44 +0100, m.reddy prasad reddy wrote: > my aim is to run the assembly programs in python.by that we can use that > in > the any labs.because we can run the python in the mobiles also.if we > write > assembly programs in the mobile ,the mobile act as a tool kit for the > lab.tell me any other solutions for this Please define what you mean by "assembly". -- Rhodri James *-* Wildebeest Herder to the Masses From mtnbikingmark at gmail.com Fri Jul 10 08:30:46 2009 From: mtnbikingmark at gmail.com (Mark) Date: Fri, 10 Jul 2009 05:30:46 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: <3aa3398a-b944-40fc-bb3b-2c2650d41454@26g2000yqk.googlegroups.com> Hi, I am the author of pywinauto (http://pywinauto.openqa.org/). It doesn't have specific functionality built in for winforms - but many winforms controls are built on top of win32 functionality and pywinauto can work with them for that. On Jul 9, 4:41?pm, Paul McGuire wrote: > On Jul 9, 1:50?pm, DuaneKaufman wrote: > > > > I have used pywinauto to drive a Flash game running inside of an > > > Internet Explorer browser - that's pretty GUI! > Wow - cool :) > > > -- Paul > > > Hi, > > > Could you share some code examples? > You might have a look at http://pywinauto.pbworks.com/ (the site is no longer used - but there is a cool movie giving an example of using pywinauto at the interactive prompt) Other examples (Notepad, mspaint) are included with the install. PyPi - http://pypi.python.org/pypi/pywinauto/0.3.8 > > Thanks, > > Duane (duanek (at) chorus (dot) net) > Good luck! :) Mark > I just went on a brief fishing expedition through two disk backups, > and no luck. ?I guess it's been a while since I worked on this. > > The work I did was entirely graphical, which is to say, my script > interacted with the Flash program by using PIL to take image snapshots > of the window, and then sifting through the bitmap looking for the > status of a "time remaining" thermometer-style gauge in the game. > Then the script could click on X-Y coordinates within the window, > which would get picked up by the Flash game, and the script would > monitor the "time remaining" gauge some more, and so on. > > I'm not sure how well pywinauto would work in allowing you to access > controls such as textboxes within a form. ?I remember that I had to > access the IE window using a caption name, and then found the embedded > Flash program as an embedded control of some sort, again, I probably > needed to indicate that it was some sort of "Adobe.FlashWidget1" > object or something. > > I may have another, older disk backup at home, I can look for it later > this evening. > > -- Paul From jeanmichel at sequans.com Fri Jul 10 08:32:52 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 10 Jul 2009 14:32:52 +0200 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: <4A5734F4.7010105@sequans.com> Steven D'Aprano wrote: > On Fri, 10 Jul 2009 02:06:35 +0000, Jason S. Friedman wrote: > > >> Hello, I administer the Informatica ETL tool at my company. Part of >> that role involves creating and enforcing standards. I want the >> Informatica developers to add comments to certain key objects and I want >> to be able to verify (in an automated fashion) that they have done so. >> >> I cannot merely check for non-emptiness; that is trivial to circumvent. >> On the other hand, I probably do not need to be able to catch >> developers who are determined to not create comments. There are not too >> many of them and perhaps they will find it is easier to write a (useful) >> comment than to game the system. >> >> Any thoughts on how I might proceed? Stated plainly, how can I tell >> when a string more-or-less forms at least one phrase? >> > > Define "phrase". > > > if len(s) > 0: > print "at least one character" > if len(s.split()) > 0: > print "at least one word" > if len(s.split('\n') > 0: > print "at least one line" > > > > You could also verify there are at least N different characters used in the sentence: N = 5 # must contains at least 5 different characters record = [] for c in s: if c not in record: record += [c] if len(record) >= N: print "at least %s different characters" % N Jean-Michel From python.list at tim.thechases.com Fri Jul 10 08:37:59 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jul 2009 07:37:59 -0500 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: <4A573627.8090308@tim.thechases.com> m.reddy prasad reddy wrote: > can any one tell me how to write assembly language programs in python...if > no is there any other way to write the programs in python Bah, writing assembly language is easy in Python: print("MOV EAX, [EBX]") print("XOR EBX, EBX") Just adjust the strings for your favorite processor architecture and Python will produce the assembly code you want. Now compiling assembly to *machine* code...or calling between Python and assembly...that's another matter. But writing assembly language programs in Python? Easy. ;-) -tkc From __peter__ at web.de Fri Jul 10 08:48:18 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 14:48:18 +0200 Subject: language analysis to enforce code standards References: Message-ID: Jason S. Friedman wrote: > Hello, I administer the Informatica ETL tool at my company. Part of > that role involves creating and enforcing standards. I want the > Informatica developers to add comments to certain key objects and I want > to be able to verify (in an automated fashion) that they have done so. > > I cannot merely check for non-emptiness; that is trivial to circumvent. > On the other hand, I probably do not need to be able to catch > developers who are determined to not create comments. There are not too > many of them and perhaps they will find it is easier to write a (useful) > comment than to game the system. > > Any thoughts on how I might proceed? Stated plainly, how can I tell > when a string more-or-less forms at least one phrase? Don't be a fool. Have someone other than the author read the comment. Peter From vox2000 at gmail.com Fri Jul 10 08:52:10 2009 From: vox2000 at gmail.com (vox) Date: Fri, 10 Jul 2009 05:52:10 -0700 (PDT) Subject: Query regarding set([])? References: Message-ID: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> On Jul 10, 2:04?pm, Peter Otten <__pete... at web.de> wrote: > You are probably misinterpreting len(s3). s3 contains lines occuring in > "file1" but not in "file2". Duplicate lines are only counted once, and the > order doesn't matter. > > So there are 119 lines that occur at least once in "file2", but not in > "file1". > > If that is not what you want you have to tell us what exactly you are > looking for. > > Peter Hi, Thanks for the answer. I am looking for a script that compares file1 and file2, for each line in file1, check if line is present in file2. If the line from file1 is not present in file2, print that line/write it to file3, because I have to know what lines to add to file2. BR, Andy From drobinow at gmail.com Fri Jul 10 09:10:14 2009 From: drobinow at gmail.com (David Robinow) Date: Fri, 10 Jul 2009 09:10:14 -0400 Subject: Query regarding set([])? In-Reply-To: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: <4eb0089f0907100610j3c53f4efjae119c9ec21f3992@mail.gmail.com> On Fri, Jul 10, 2009 at 8:52 AM, vox wrote: > I am looking for a script that compares file1 and file2, for each line > in file1, check if line is present in file2. If the line from file1 is > not present in file2, print that line/write it to file3, because I > have to know what lines to add to file2. Just copy file1 to file2. (I'm pretty sure that's not what you want, but in explaining why it should become clearer what you're trying to do.) From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 09:11:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 13:11:09 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <007318de$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 12:54:21 +0200, Hendrik van Rooyen wrote: > "Steven D'Aprano" wrote: > >>On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: >> >>>> persistent idea "out there" that programming is a very accessible >>>> skill, like cooking or gardening, anyone can do it, and even profit >>>> from it, monetarily or otherwise, etc., and to some extent I am >>> >>> Programming is not like any other human activity. >> >>In practice? In principle? Programming in principle is not the same as >>it is performed in practice. >> >>But in either case, programming requires both the logical reasoning of >>mathematics and the creativity of the arts. Funnily enough, > > I do not buy this arty creativity stuff. - or are you talking about > making a website look pretty? I must admit, it never crossed my mind that anyone here would claim that there was no creativity involved in programming, that it was all a mindless, algorithmic process capable of being done by a simple mechanical device. This is certainly the accusation made against *bad* programmers -- that they can't actually solve new, unique problems, but just apply recipes they learned without any insight or intelligence. The sort of people who program so poorly that "a trained monkey could do what they do". Do you really think that applies to good programmers too? If so, then a good code generator should be able to replace any programmer. Is that what you believe? >>mathematicians will tell you that mathematics requires the same, and so >>will the best artists. I think mathematicians, engineers, artists, even >>great chefs, will pour scorn on your claim that programming is not like >>any other human activity. > > So a chef is now an authority on programming? Did I say that? Chefs are authorities on OTHER HUMAN ACTIVITIES. > Programming is actually kind of different - almost everything else is > just done, at the time that you do it. > > Programming is creating stuff that is completely useless until it is fed > into something that uses it, to do something else, in conjuction with > the thing it is fed into, at a later time. Somebody should teach Hendrik that human beings have been creating TOOLS for hundreds of thousands of years. People have been creating tools to build tools for thousands of years. Software is just more of the same. Even *soup stock* fits the same profile as what Hendrik claims is almost unique to programming. On its own, soup stock is totally useless. But you make it, now, so you can you feed it into something else later on. Or instant coffee. No, Henrik, if that's the best you can do, it's not very good. It is rather sad, but also hilarious, that the most different thing you have noticed about software is that it's just like instant coffee. > This is a highly significant difference, IMHO. >>[...] >>> He talks about how "when all is said and done, the only thing >>> computers can do for us is to manipulate symbols and produce results >>> of such manipulations" and he emphasises the "uninterpreted" nature of >>> mechanical symbol manipulation, i.e. that the machine is doing it >>> mindlessly. >> >>"Manipulate symbols" is so abstract as to be pointless. By that >>reasoning, I can build a "computer" consisting of a box open at the top. >>I represent a symbol by an object (say, a helium-filled balloon, or a >>stone), instead of a pattern of bits. I manipulate the symbol by holding >>the object over the box and letting go. If it flies up into the sky, >>that represents the symbol "Love is War", if it falls into the box, it >>represents the symbol "Strength is Blue", and if it just floats there, >>it represents "Cheddar Cheese". This is a deterministic, analog computer >>which manipulates symbols. Great. >> >>And utterly, utterly useless. So what is my computer lacking that real >>computers have? When you have answered that question, you'll see why >>Dijkstra's claim is under-specified. >> >> > So if computers do not manipulate symbols, what is it that they do? Did I say they don't manipulate symbols? > They > sure cannot think, > or drink, > or reason, They can't reason? Then what are they doing when they manipulate symbols? Yet again, it didn't even cross my mind that somebody would make this claim. My entire point is that it's not enough to just "manipulate symbols", you have to manipulate symbols the correct way, following laws of logic, so that the computer can *mindlessly* reason. > or almost any verb you can think of. If you're going to take that argument, then I'll remind you that there are no symbols inside a computer. There are only bits. And in fact, there aren't even any bits -- there are only analog voltages, and analog magnetic fields. > "Manipulating symbols" is actually an elegant definition. Try coming up > with a better one and you will see. As I said above, it's not enough to just manipulate symbols. Here's a set of rules to manipulate symbols: X => 0 or in English, "Any symbol becomes the zero symbol". That's symbol manipulation. Utterly useless. This is why it's not enough to just manipulate symbols, you have to manipulate them THE RIGHT WAY. -- Steven From david250 at videotron.ca Fri Jul 10 09:14:50 2009 From: david250 at videotron.ca (David Bernier) Date: Fri, 10 Jul 2009 09:14:50 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search > the results of other Google users immediately results in a level of > accuracy and relevance above and beyond Google." Google changed their > API following the launch or MeAmI.org and explored the possibility of > blocking site access from MeAmI.org to Google search results but was > unable to do so. Critics of MeAmI.org say what it is doing is > tantamount to intellectual theft. When asked about this topper Musatov > exclaimed, "The Internet was made for people, not companies." An > analyst at Goldman Sachs says, requesting to remain anonymous, > "MeAmI.org has a strong presence in promoting itself as a vehicle for > global activism and to tell you the truth, this makes it much more > likely an acquisition target than potential intellectual property > violator." Google could not be reached for comment. Mr. Musatov, is it you who removed the post quoted above from the Google Groups archive? Thank you. David Bernier From Nikolaus at rath.org Fri Jul 10 09:22:29 2009 From: Nikolaus at rath.org (Nikolaus Rath) Date: Fri, 10 Jul 2009 09:22:29 -0400 Subject: Implementing a cache Message-ID: <87ab3cwu3u.fsf@vostro.rath.org> Hello, I want to implement a caching data structure in Python that allows me to: 1. Quickly look up objects using a key 2. Keep track of the order in which the objects are accessed (most recently and least recently accessed one, not a complete history) 3. Quickly retrieve and remove the least recently accessed object. Here's my idea for the implementation: The objects in the cache are encapsulated in wrapper objects: class OrderedDictElement(object): __slots__ = [ "next", "prev", "key", "value" ] These wrapper objects are then kept in a linked lists and in an ordinary dict (self.data) in parallel. Object access then works as follows: def __setitem__(self, key, value): if key in self.data: # Key already exists, just overwrite value self.data[key].value = value else: # New key, attach at head of list with self.lock: el = OrderedDictElement(key, value, next=self.head.next, prev=self.head) self.head.next.prev = el self.head.next = el self.data[key] = el def __getitem__(self, key): return self.data[key].value To 'update the access time' of an object, I use def to_head(self, key): with self.lock: el = self.data[key] # Splice out el.prev.next = el.next el.next.prev = el.prev # Insert back at front el.next = self.head.next el.prev = self.head self.head.next.prev = el self.head.next = el self.head and self.tail are special sentinel objects that only have a .next and .prev attribute respectively. While this is probably going to work, I'm not sure if its the best solution, so I'd appreciate any comments. Can it be done more elegantly? Or is there an entirely different way to construct the data structure that also fulfills my requirements? I already looked at the new OrderedDict class in Python 3.1, but apparently it does not allow me to change the ordering and is therefore not suitable for my purpose. (I can move something to one end by deleting and reinserting it, but I'd like to keep at least the option of also moving objects to the opposite end). Best, -Nikolaus -- ?Time flies like an arrow, fruit flies like a Banana.? PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C From aahz at pythoncraft.com Fri Jul 10 09:50:29 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 06:50:29 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: [excessive quoting ahead, I'm too tired to trim] In article , kj wrote: >In aahz at pythoncraft.com (Aahz) writes: >>In article , kj wrote: >>> >>>OK, so, scratching from my original post the case >>> >>>. = >>> >>>(as being a special case of = ), still, >>>to the extent that I understand your post, the "=" in >>> >>> x = 1 >>> >>>means something fundamentally different (in terms of Python's >>>underlying implementation) from the "=" in >>> >>> y[0] = 1 >>> >>>No? >> >>No. ;-) > >No??? Just when I thought I finally understood all this! > >>What's different is not the ``=`` but the construction of the >>assignment target before ``=`` gets executed. > >Hmm. OK, I went to the link you posted in your other message >(http://docs.python.org/reference/simple_stmts.html#assignment-statements) >and I find this (my emphasis): > > Assignment of an object to a single target is recursively defined as follows. > > * If the target is an identifier (name): > > o If the name does not occur in a global statement in > the current code block: the name is bound to the object > ^^^^^^^^^^^^^^^^^ > in the current local namespace. > > o Otherwise: the name is bound to the object in the > ^^^^^^^^^^^^^^^^^ > current global namespace. > > The name is rebound if it was already bound. This may cause > the reference count for the object previously bound to the > name to reach zero, causing the object to be deallocated and > its destructor (if it has one) to be called. > > * If the target is a target list enclosed in parentheses or in > square brackets... (I'LL IGNORE THIS FOR NOW) > > * If the target is an attribute reference: The primary expression > in the reference is evaluated. It should yield an object with > assignable attributes; if this is not the case, TypeError is > raised. That object is then asked to assign the assigned > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > object to the given attribute; if it cannot perform the > ^^^^^^ > assignment, it raises an exception (usually but not necessarily > AttributeError). > > * If the target is a subscription: The primary expression in > the reference is evaluated. It should yield either a mutable > sequence object (such as a list) or a mapping object (such > as a dictionary). Next, the subscript expression is evaluated. > > If the primary is a mutable sequence object (such as a > list),... [CONDITIONS ON THE INDEX EXPRESSION OMITTED]... > the sequence is asked to assign the assigned object to its > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > item with that index.... > > If the primary is a mapping object (such as a dictionary),... > [CONDITIONS ON THE SUBSCRIPT EXPRESSION OMITTED]... the > ^^^ > mapping is then asked to create a key/datum pair which maps > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > the subscript to the assigned object. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > * If the target is a slicing: [INDEX STUFF OMITTED]... the > ^^^ > sequence object is asked to replace the slice with the items > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > of the assigned sequence... > ^^^^^^^^^^^^^^^^^^^^^^^^ > >OK, I originally interpreted what Lundh wrote in his two articles >that the "binding" described at the very beginning (i.e. when the >target is an identifier), which I take to make an entry or modify >such an entry in a namespace, is a *categorically different* >operation from the remaining operations underlined above. > >I interpreted Paul Boddie's correction in his response to me as >saying that the "assignment" mentioned for the case when the target >is an attribute reference is actually a special case of the >"assignment" to simple identifiers (i.e. it also means "binding"). > >But that still leaves all the other "assignments" (or the like) >underlined above. I don't think that the full definitions of these >remaining cases are covered by the same rule, even though the rule >is described as "recursive." I think that the writer has something >else in mind, and in particular, something *other* than binding, >but the author remains vague on exactly what this means. > >Clearly, both Lundh and the documentation draw some distinction >between "binding" and some other forms of "assignment" (which remain >ill-defined throughout). This distinction is what I was referring >to when I said that "=" means different things in different contexts. Consider this: x = 1 globals()['x'] = 1 locals()[1] = 1 What's the difference between the three? Although there's a lot of machinery amenable to manipulation, with the corresponding potential for breaking the standard model, fundamentally it all comes down to the intention that *some* piece of code establishes a target on the left-hand side and binds it to the object returned by the right-hand side. IME, except when going down to the very lowest-level details, Python is easiest to understand when you treat all assignment statements as working the same. It helps to remember that names and namespaces are in many ways syntactic sugar for dicts or lists. (Obviously, the example with locals() doesn't do what it purports to do, but it shows how function/method namespaces work under the covers.) Slicing is the one real exception, but again I think that it's easier to understand by wedging your mental model into conformance with the simple target/binding metaphor. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From davea at ieee.org Fri Jul 10 09:55:58 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 09:55:58 -0400 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: <4A57486E.9070900@ieee.org> jhinak sen wrote: > hi, > i am a beginner in python language, > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the mean > and sum in some other file : > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * > > f0= open("temp9","r+").readlines() > f2= open("out1","r+") > add_1=[ ]; > for i in range(0, len(f0)): > f1=f0[i].split() > add= float(f1[1])+float(f1[2]) > mean= float(add)/2 > print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), > repr(mean).ljust(7) > add_1.append(add) > add_1.append(mean) > f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % > repr(mean).ljust(7)) > print "printing from file" > for i in range(0, len(add_1),2): > print add_1[i]," ", add_1[i+1] > > f0.close() > f2.close()* > > > "" > > and this programme is givving me this error : > > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" > > please help to to find the error. > or suggest some simpler or better way > > note: > 1)file "temp9" is already exist > 2)this programme is giving me all my outputs, but at the end of the out > ..its giving me that error. > > Others have pointed out the specific problem that gives you this error. But I'd like to point out a few other things to consider: 1) Don't mix tabs and spaces. Best practice is to bind tab to (4) spaces in your editor, and never have a tab in a Python source file. 2) Think about your variable names. As it stands, f0 is a list of lines, f1 is a list of "word" within a line, and f2 is a file. No wonder you accidentally tried to close the list. I'd suggest things like: infile = open(....) lines = infile.readlines() outfile = open(....) for line in lines: words = line.split(" ") or even val1, val2 = lines.split(" ") Then of course the last two lines become infile.close() outfile.close() 3) Learn to use the for statement directly on a list, rather than using len() on the list to make an index, then using the index to find the value 4) On the open() calls, get your modes right. Looks like you really want infile = open(infilename, "r") outfile = open(outfilename, "w") 5) Consider using tuples in your add_1 list, rather than separate elements. That way, each element of the list would contain both sum and mean. add_1.append((add, mean)) and the final print would become for item in add_1: print item[0]," ", item[1] 6) Put anything over three lines into a function, instead of doing it at module scope. That way, you'll be clearer about what things are local to this code, and what might be useful to other code in the same module. In this case, infilename, and outfilename might be arguments to that function. There are lots of other refinements, but these are all within your reach, and would make the program much clearer. From usernet at ilthio.net Fri Jul 10 10:05:13 2009 From: usernet at ilthio.net (Tim Harig) Date: Fri, 10 Jul 2009 14:05:13 GMT Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On 2009-07-10, garabik-news-2005-05 at kassiopeia.juls.savba.sk wrote: > Tim Harig wrote: >> On 2009-07-09, Alex Rosslyn wrote: >>> I would like to learn a way of changing the colour of a particular >>> part of the output text. I've tried the following >> On Unix operating systems this would be done through the curses interface: >> http://docs.python.org/library/curses.html > Or using ANSI colour codes: Which will only work for ANSI terminals. From t.lehmann at rtsgroup.net Fri Jul 10 10:07:22 2009 From: t.lehmann at rtsgroup.net (Thomas Lehmann) Date: Fri, 10 Jul 2009 07:07:22 -0700 (PDT) Subject: Tkinter only: table widget with canvas... Message-ID: My intention is to keep dependencies low that means using python and tkinter as base package is quite easy because it's available on nearly every system. There is good documentation on Tkinter but going into the depth I'm missing many details. As an example... Using the Tkinter.Canvas class I should be able to create a nice table. The missing informations are: a) Assume I would have some different widgets to add per row. How do I get the maximum row height? b) Assume something like a label in one column. The length of all texts in a column will differ. How do I choose the maxium column width? c) Placing headers in a canvas does not look like a good idea because I don't want to scroll the headers. Am I right? c.1) How do I place a none scrollable header in a canvas? or c.2) How do I place all headers outside the canvas correctly above the relating column? best regards Thomas From davea at ieee.org Fri Jul 10 10:17:55 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 10:17:55 -0400 Subject: Query regarding set([])? In-Reply-To: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: <4A574D93.30103@ieee.org> vox wrote: > On Jul 10, 2:04 pm, Peter Otten <__pete... at web.de> wrote: > >> You are probably misinterpreting len(s3). s3 contains lines occuring in >> "file1" but not in "file2". Duplicate lines are only counted once, and the >> order doesn't matter. >> >> So there are 119 lines that occur at least once in "file2", but not in >> "file1". >> >> If that is not what you want you have to tell us what exactly you are >> looking for. >> >> Peter >> > > Hi, > Thanks for the answer. > > I am looking for a script that compares file1 and file2, for each line > in file1, check if line is present in file2. If the line from file1 is > not present in file2, print that line/write it to file3, because I > have to know what lines to add to file2. > > BR, > Andy > > > There's no more detail in that response. To the level of detail you provide, the program works perfectly. Just loop through the set and write the members to the file. But you have some unspecified assumptions: 1) order doesn't matter 2) duplicates are impossible in the input file, or at least not meaningful. So the correct output file could very well be smaller than either of the input files. And a few others that might matter: 3) the two files are both text files, with identical line endings matching your OS default 4) the two files are ASCII, or at least 8 bit encoded, using the same encoding (such as both UTF-8) 5) the last line of each file DOES have a trailing newline sequence From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 10:22:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 14:22:11 GMT Subject: Implementing a cache References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: <00732984$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 09:22:29 -0400, Nikolaus Rath wrote: > Hello, > > I want to implement a caching data structure in Python that allows me > to: > > 1. Quickly look up objects using a key 2. Keep track of the order in > which the objects are accessed (most > recently and least recently accessed one, not a complete history) > 3. Quickly retrieve and remove the least recently accessed object. Google for "python LRU cache". Here are the first three hits: http://code.activestate.com/recipes/498245/ http://code.activestate.com/recipes/252524/ http://www.algorithm.co.il/blogs/index.php/programming/python/small-python-challenge-no-2-lru-cache/ -- Steven From chris at simplistix.co.uk Fri Jul 10 10:26:00 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Fri, 10 Jul 2009 15:26:00 +0100 Subject: Where does setuptools live? In-Reply-To: <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> Message-ID: <4A574F78.9000801@simplistix.co.uk> Inky 788 wrote: > Currently, distutils itself is being actively developed. More info > about this here: http://tarekziade.wordpress.com/ > > My (albeit anonymous) advice is: use distutils. Manually download > packages as-needed from PyPI and install manually using standard > distutils. No thanks. I'm a big fan of buildout. Making it possible for packages to specify their dependencies is a big win... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From vox2000 at gmail.com Fri Jul 10 10:28:04 2009 From: vox2000 at gmail.com (vox) Date: Fri, 10 Jul 2009 07:28:04 -0700 (PDT) Subject: Query regarding set([])? References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: On Jul 10, 4:17?pm, Dave Angel wrote: > vox wrote: > > On Jul 10, 2:04 pm, Peter Otten <__pete... at web.de> wrote: > > >> You are probably misinterpreting len(s3). s3 contains lines occuring in > >> "file1" but not in "file2". Duplicate lines are only counted once, and the > >> order doesn't matter. > > >> So there are 119 lines that occur at least once in "file2", but not in > >> "file1". > > >> If that is not what you want you have to tell us what exactly you are > >> looking for. > > >> Peter > > > Hi, > > Thanks for the answer. > > > I am looking for a script that compares file1 and file2, for each line > > in file1, check if line is present in file2. If the line from file1 is > > not present in file2, print that line/write it to file3, because I > > have to know what lines to add to file2. > > > BR, > > Andy > > There's no more detail in that response. ?To the level of detail you > provide, the program works perfectly. ?Just loop through the set and > write the members to the file. > > But you have some unspecified assumptions: > ? ? 1) order doesn't matter > ? ? 2) duplicates are impossible in the input file, or at least not > meaningful. ?So the correct output file could very well be smaller than > either of the input files. > > And a few others that might matter: > ? ? 3) the two files are both text files, with identical line endings > matching your OS default > ? ? 4) the two files are ASCII, or at least 8 bit encoded, using the > same encoding ?(such as both UTF-8) > ? ? 5) the last line of each file DOES have a trailing newline sequence Thanks all for the input! I have guess I have to think it through a couple times more. :) BR, Andy From digitig at gmail.com Fri Jul 10 10:38:54 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 10 Jul 2009 15:38:54 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: 2009/7/9 kj : > Thanks for the encouragement. [snip] > into code. ?And by this I mean not only assumptions about the > correctness of their code (the typical scope of assertions), but > also, more broadly, assumptions about the data that they are dealing > with (which often comes from external sources with abysmal quality > control). There we diverge. A lot. If "correctness of the code trumps everything else" (in fact, if it matters at all) and the external data has "abysmal quality control" then it *must* be checked for correctness before it is used. If it is not, you have no idea whether your output is correct or not. And assertions *will* *not* reliably provide that checking (because they may not be executed). You *must* actively check the data, using good old-fasioned "if" statements and so on, because not to do so is to declare that you *don't* care about correctness. You *know* the input is often wrong, but you're not bothering to check it? -- Tim Rowe From __peter__ at web.de Fri Jul 10 10:59:53 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 16:59:53 +0200 Subject: Query regarding set([])? References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: vox wrote: > On Jul 10, 4:17 pm, Dave Angel wrote: >> vox wrote: >> > On Jul 10, 2:04 pm, Peter Otten <__pete... at web.de> wrote: >> >> >> You are probably misinterpreting len(s3). s3 contains lines occuring >> >> in "file1" but not in "file2". Duplicate lines are only counted once, >> >> and the order doesn't matter. >> >> >> So there are 119 lines that occur at least once in "file2", but not in >> >> "file1". >> >> >> If that is not what you want you have to tell us what exactly you are >> >> looking for. >> >> >> Peter >> >> > Hi, >> > Thanks for the answer. >> >> > I am looking for a script that compares file1 and file2, for each line >> > in file1, check if line is present in file2. If the line from file1 is >> > not present in file2, print that line/write it to file3, because I >> > have to know what lines to add to file2. >> >> > BR, >> > Andy >> >> There's no more detail in that response. To the level of detail you >> provide, the program works perfectly. Just loop through the set and >> write the members to the file. >> >> But you have some unspecified assumptions: >> 1) order doesn't matter >> 2) duplicates are impossible in the input file, or at least not >> meaningful. So the correct output file could very well be smaller than >> either of the input files. >> >> And a few others that might matter: >> 3) the two files are both text files, with identical line endings >> matching your OS default >> 4) the two files are ASCII, or at least 8 bit encoded, using the >> same encoding (such as both UTF-8) >> 5) the last line of each file DOES have a trailing newline sequence > > Thanks all for the input! > I have guess I have to think it through a couple times more. :) Indeed. Note that others thinking through related problems have come up with http://docs.python.org/library/difflib.html Peter From mondi at cs.unibo.it Fri Jul 10 11:16:14 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Fri, 10 Jul 2009 15:16:14 +0000 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: Roger Binns wrote: > jacopo mondi wrote: >> Hi all, I need to patch socketmodule.c (the _socket module) in order to >> add support to an experimental socket family. > > You may find it considerably easier to use ctypes since that will avoid > the need for any patching. You'll also be able to control how read and > write are done (eg read vs recvfrom vs recvmsg vs readv). You can use > os.fdopen to convert your raw file descriptor into a Python file object > if appropriate. > > If you do use ctypes then you'll only need to distribute pure Python > source code. > > Roger Wait a minute, am I confused or ctypes seems to be unusless for my needs? Ctypes wraps a system shared object (as libc is) and gives you the possibility to access its functions. What have I to wrap? the fuctionalities I need to use are those exposed by the kernel via sys/socket.h haeder (socket, bind, connect etc.). Maybe I could wrapp _socket.so (I think it's possible) but seems to be unuseless because I need to modify, for example, the parameters that _socket.so passes to the socket() function in order to create a socket owned by the new socket family I'm actualy experimenting. Hope it's clear what I'm trying to explain... Thanks anyway, but ctypes doen't seem to fit my needs.. cheers jacopo From aahz at pythoncraft.com Fri Jul 10 11:19:48 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 08:19:48 -0700 Subject: PyGtk Depends on Numeric References: <1ebe9314-9434-459a-bd3e-2b2386a35f1b@n11g2000yqb.googlegroups.com> Message-ID: In article <1ebe9314-9434-459a-bd3e-2b2386a35f1b at n11g2000yqb.googlegroups.com>, dieter wrote: > >Get with the times people and port to numpy. :P >Don't you think its about time? Are you trying to get something to happen or just posting a random drive-by? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From francois.dot.grondin at bpr-cso.dot.com Fri Jul 10 11:20:48 2009 From: francois.dot.grondin at bpr-cso.dot.com (François Grondin) Date: Fri, 10 Jul 2009 15:20:48 GMT Subject: AP -- MeAmI.org Paces Google References: Message-ID: "David Bernier" a ?crit dans le message de news: h36ki102d7m at news5.newsguy.com... > Musatov wrote: >> On Jul 9, 7:54 pm, David Bernier wrote: >>> Musatov wrote: >>>> Los Angeles (AP) --MeAmI.org now has users in 50 countries following >>>> its adopted use in Pakistan. The search engine has grown in >>>> popularity 10,000 fold following its Beta test launch three months ago >>>> in April, 2009. Supporters of the site claim it is better than rival >>>> Google upon which platform it is based. Controversy arose after >>>> MeAmI.org search code allowed users to search other users Google >>>> results with no advertising. "It is truly an innovative thing we are >>>> doing," said Founder and CEO, Martin Musatov. "Letting users search >>>> the results of other Google users immediately results in a level of >>>> accuracy and relevance above and beyond Google." Google changed their >>>> API following the launch or MeAmI.org and explored the possibility of >>>> blocking site access from MeAmI.org to Google search results but was >>>> unable to do so. Critics of MeAmI.org say what it is doing is >>>> tantamount to intellectual theft. When asked about this topper Musatov >>>> exclaimed, "The Internet was made for people, not companies." An >>>> analyst at Goldman Sachs says, requesting to remain anonymous, >>>> "MeAmI.org has a strong presence in promoting itself as a vehicle for >>>> global activism and to tell you the truth, this makes it much more >>>> likely an acquisition target than potential intellectual property >>>> violator." Google could not be reached for comment. >>> Mr. Musatov, do you know who originally wrote the >>> article above? >>> >>> Thank you. >>> >>> David Bernier- Hide quoted text - >>> >>> - Show quoted text - >> >> Yes. > > Mr. Musatov, do you know the name of the person who > originally wrote the article above? > > Thank you. > > David Bernier Maybe Mr. Musatov should answer the following questions instead : 1. Did you write the article above? 2. If not, who did? And I don't want AP as the answer, but the name of the journalist. David, don't take it bad, but he answered your question with an accurate answer (yes), You gave him the opportunity to avoid the real answer and he took it. Based on Musatov's strange behavior and logic, don't expect more from him. Ask anyone else the same question and you'd get a real answer. BTW, my guess for question 2 would be the Masked Logician, Floetry, scriber77, or Professor X. Francois From aahz at pythoncraft.com Fri Jul 10 11:23:29 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 08:23:29 -0700 Subject: language analysis to enforce code standards References: Message-ID: In article , Jean-Michel Pichavant wrote: > >You could also verify there are at least N different characters used in >the sentence: > >N = 5 # must contains at least 5 different characters >record = [] >for c in s: > if c not in record: > record += [c] >if len(record) >= N: > print "at least %s different characters" % N Much simpler and *way* more efficient with a set: if len(set(s)) < N: print "Must have at least %s different characters" % N -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From Scott.Daniels at Acm.Org Fri Jul 10 11:28:29 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 08:28:29 -0700 Subject: tough-to-explain Python In-Reply-To: <007318de$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > Even *soup stock* fits the same profile as what Hendrik claims is almost > unique to programming. On its own, soup stock is totally useless. But you > make it, now, so you can you feed it into something else later on. > > Or instant coffee. I think I'll avoid coming to your house for a cup of coffee. :-) --Scott David Daniels Scott.Daniels at Acm.Org From magobin at gmail.com Fri Jul 10 11:33:18 2009 From: magobin at gmail.com (Alex) Date: Fri, 10 Jul 2009 08:33:18 -0700 (PDT) Subject: problem with keys combination! Message-ID: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Hi at all, I made a simple program that make a screenshot of Desktop and use it as fullscreen background and then a ball erase image making illusion that erase Desktop. The program working fine and I succesfully blocked all keys but I have a problem with hotkey combination Ctrl-Alt- Del...that bypass my FullScreen Application. Reading in google I understood that if I want disable this keys I have to operate in more low level. But I'm a newbe and I don't know how to make it(operate with api is very hard 4 me).I tried several days to find a workaround like replace one of that keys but doesn't work :-(( Plus..I can't install pyHook because there isn't a 2.6 version :-(( Can someone help me ?? thanks in advance Alex This is a piece of my code: while True: pg.event.pump() keyinput = pg.key.get_pressed() # press ESC to exit if keyinput[pg.K_ESCAPE] raise SystemExit if keyinput[pygame.K_LALT] and keyinput[pygame.K_LCTRL] and keyinput[pygame.K_DELETE]: win32api.keybd_event(win32con.VK_ESCAPE,0) #shell = win32com.client.Dispatch("WScript.Shell") #shell.SendKeys("{ESC}") #ignore keyboard input def IgnoreKeyboardInterrupt(): return signal.signal(signal.SIGINT,signal.SIG_IGN) From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 11:48:47 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 15:48:47 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <00733dd0$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 08:28:29 -0700, Scott David Daniels wrote: > Steven D'Aprano wrote: >> Even *soup stock* fits the same profile as what Hendrik claims is >> almost unique to programming. On its own, soup stock is totally >> useless. But you make it, now, so you can you feed it into something >> else later on. >> >> Or instant coffee. > > I think I'll avoid coming to your house for a cup of coffee. :-) I meant the instant coffee powder is prepared in advance. It's useless on it's own, but later on you feed it into boiling water, add sugar and milk, and it's slightly less useless. -- Steven From davea at ieee.org Fri Jul 10 11:57:21 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 11:57:21 -0400 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100718p5e48506cg2d133842972d6328@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> <4A57486E.9070900@ieee.org> <3d4ff96c0907100718p5e48506cg2d133842972d6328@mail.gmail.com> Message-ID: <4A5764E1.7010102@ieee.org> jhinak sen wrote: > hey, > thanx a lot :) > i got ur points .. and it really helps.. > > and please also tell me ... > where i can get more basic and detail knowledge of python.. as i am > beginners in this , i need more examples of python programmes so that i can > understand better. > also if you know of any gud pdf file or book please let me know > > thnx a lot > jhinak > > On Fri, Jul 10, 2009 at 7:25 PM, Dave Angel wrote: > > >> jhinak sen wrote: >> >> >>> hi, >>> i am a beginner in python language, >>> >>> i am trying with this programme : >>> to find the addition and mean from a data set in a file and writing the >>> mean >>> and sum in some other file : >>> "" >>> *#! /usr/bin/env python >>> >>> import re >>> import cPickle as p >>> import math >>> from numpy import * >>> >>> f0= open("temp9","r+").readlines() >>> f2= open("out1","r+") >>> add_1=[ ]; >>> for i in range(0, len(f0)): >>> f1=f0[i].split() >>> add= float(f1[1])+float(f1[2]) >>> mean= float(add)/2 >>> print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), >>> repr(mean).ljust(7) >>> add_1.append(add) >>> add_1.append(mean) >>> f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % >>> repr(mean).ljust(7)) >>> print "printing from file" >>> for i in range(0, len(add_1),2): >>> print add_1[i]," ", add_1[i+1] >>> >>> f0.close() >>> f2.close()* >>> >>> >>> "" >>> >>> and this programme is givving me this error : >>> >>> "" *Traceback (most recent call last): >>> File "./temporary1.py", line 24, in >>> f0.close() >>> AttributeError: 'list' object has no attribute 'close'* >>> "" >>> >>> please help to to find the error. >>> or suggest some simpler or better way >>> >>> note: >>> 1)file "temp9" is already exist >>> 2)this programme is giving me all my outputs, but at the end of the out >>> ..its giving me that error. >>> >>> >>> >>> >> Others have pointed out the specific problem that gives you this error. >> But I'd like to point out a few other things to consider: >> >> 1) Don't mix tabs and spaces. Best practice is to bind tab to (4) spaces >> in your editor, and never have a tab in a Python source file. >> 2) Think about your variable names. As it stands, f0 is a list of lines, >> f1 is a list of "word" within a line, and f2 is a file. No wonder you >> accidentally tried to close the list. I'd suggest things like: >> infile = open(....) >> lines = infile.readlines() >> outfile = open(....) >> >> for line in lines: >> words = line.split(" ") or even val1, val2 = >> lines.split(" ") >> >> Then of course the last two lines become >> infile.close() >> outfile.close() >> >> 3) Learn to use the for statement directly on a list, rather than using >> len() on the list to make an index, then using the index to find the value >> 4) On the open() calls, get your modes right. Looks like you really want >> infile = open(infilename, "r") >> outfile = open(outfilename, "w") >> 5) Consider using tuples in your add_1 list, rather than separate elements. >> That way, each element of the list would contain both sum and mean. >> add_1.append((add, mean)) >> >> and the final print would become >> >> for item in add_1: >> print item[0]," ", item[1] >> >> 6) Put anything over three lines into a function, instead of doing it at >> module scope. That way, you'll be clearer about what things are local to >> this code, and what might be useful to other code in the same module. >> In this case, infilename, and outfilename might be arguments to that >> function. >> >> There are lots of other refinements, but these are all within your reach, >> and would make the program much clearer. >> >> >> > > Please don' t top-post. Putting your reply out of order makes it harder for others to see the sequences of things. Some people top-post everything, but on this mailing list (and maybe most), the standard is to add to bottom, or inline where appropriate. Anyway, http://docs.python.org/tutorial/ http://diveintopython.org/ http://www.openbookproject.net/thinkCSpy/ are all good, depending on your experience with other languages, and with your computer's OS. You could also check out http://code.activestate.com/recipes/ or http://code.activestate.com/recipes/langs/python/ for Python specifically which has a large set of relatively small modules of code. For a examples that might stretch your thought process: http://code.activestate.com/recipes/576755/ http://code.activestate.com/recipes/576647/ From darcy at druid.net Fri Jul 10 12:09:28 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 10 Jul 2009 12:09:28 -0400 Subject: tough-to-explain Python In-Reply-To: <00733dd0$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> <00733dd0$0$9710$c3e8da3@news.astraweb.com> Message-ID: <20090710120928.075451c8.darcy@druid.net> On 10 Jul 2009 15:48:47 GMT Steven D'Aprano wrote: > I meant the instant coffee powder is prepared in advance. It's useless on > it's own, but later on you feed it into boiling water, add sugar and > milk, and it's slightly less useless. I don't know about that. I find instant coffee pretty useless no matter what it is fed to. :-) -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From bryanvick at gmail.com Fri Jul 10 12:13:41 2009 From: bryanvick at gmail.com (Bryan) Date: Fri, 10 Jul 2009 09:13:41 -0700 (PDT) Subject: Automate rsync w/ authentication Message-ID: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> I am trying to automate rsync to backup server A from server B. I have set up a private/public key between the two servers so I don't have to enter a password when using rsync. Running rsync manually with the following command works fine: rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp But when I try to do it with python, the subprocess simply returns the ssh -h output on stderr like I am passing some invalid syntax. What is wrong in my translation of rsync's -e command from shell to pythyon? #! /usr/bin/python from subprocess import Popen, PIPE rsyncExec = '/usr/bin/ssh' source = 'root at 10.0.45.67:/home/bry/jquery.lookup' dest = '/home/bry/tmp' rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] try: p = Popen(args, stdout=PIPE, stderr=PIPE) print 'rsync running with pid %s' % p.pid out, err = p.communicate() print 'Errors: %s' % err print 'Output: %s' % out except Exception: print 'Error running rsync' From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 12:14:04 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 16:14:04 GMT Subject: problem with keys combination! References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Message-ID: <007343bd$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 08:33:18 -0700, Alex wrote: > Hi at all, > I made a simple program that make a screenshot of Desktop and use it > as fullscreen background and then a ball erase image making illusion > that erase Desktop. The program working fine and I succesfully blocked > all keys but I have a problem with hotkey combination Ctrl-Alt- > Del...that bypass my FullScreen Application. What operating system are you using? Windows? As I understand it, you can't block, modify, or otherwise access Ctrl-Alt- Del while running under Windows: it is the "Secure Attention Key", and is designed to be virtually impossible to interfere with. It's not *quite* impossible, but it is the deepest, darkest black magic. Microsoft makes it close enough to impossible as makes no difference even for experienced developers. As a newbie, well, put it this way: it's like somebody saying "Hi guys, I have a shiny new Swiss Army Knife, the one with the screwdriver and the corkscrew. I'd like to build my own Space Shuttle -- what do I do?" http://stackoverflow.com/questions/886076/how-can-i-intercept-all-key-events-including-ctrlaltdel-and-ctrltab http://en.wikipedia.org/wiki/Control-Alt-Delete You should also read this: http://blogs.msdn.com/oldnewthing/archive/2004/02/16/73780.aspx -- Steven From mal at egenix.com Fri Jul 10 12:15:31 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Fri, 10 Jul 2009 18:15:31 +0200 Subject: DBI module deprecated at Python 2.5--what to use in its place? In-Reply-To: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: <4A576923.6050507@egenix.com> dana wrote: > I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC > modules together. Although I don't have Python 2.5, I've been informed > the DBI module has been deprecated at 2.5. A few questions: > > 1) Although deprecated, will it work at all in 2.5? Does the fact that > it is deprecrated mean it has been removed entirely, or does Python > 2.5 simply issuing a warning? > > 2) What do I use in place of DBI for my Python 2.4. scripts that > import modules DBI and ODBC together. I don't use DBI directly. It was > simply a dependency for the ODBC module as best I knew. If you're looking for a stable and maintained ODBC for Python, have a look at our mxODBC extension or mxODBC Connect package: http://www.egenix.com/products/python/mxODBC/ http://www.egenix.com/products/python/mxODBCConnect/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 10 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 12:17:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 16:17:09 GMT Subject: help me to find the error References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> <4A57486E.9070900@ieee.org> <3d4ff96c0907100718p5e48506cg2d133842972d6328@mail.gmail.com> Message-ID: <00734476$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 11:57:21 -0400, Dave Angel wrote: [...] > Please don' t top-post. Putting your reply out of order makes it harder > for others to see the sequences of things. Some people top-post > everything, but on this mailing list (and maybe most), the standard is > to add to bottom, or inline where appropriate. Inline is nearly always appropriate. Please trim your replies, leaving only what you need for context and what you are replying to directly, not the entire every-growing collection of quoted-quoted-quoted-quoted-quotes. Thank you. -- Steven From mal at egenix.com Fri Jul 10 12:18:28 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Fri, 10 Jul 2009 18:18:28 +0200 Subject: Remoting over SSH In-Reply-To: <4A549B07.2090402@gmail.com> References: <4A549B07.2090402@gmail.com> Message-ID: <4A5769D4.8080106@egenix.com> Lucas Carvalho wrote: > Hussein B wrote: >> Hey, >> I want to perform commands on a remote server over SSH. >> What do I need? >> Thanks. >> > Hi, > If you want to use the SSH2 protocol into a python code, you should > take a look at this module: paramiko [1]. > > [1] http://www.lag.net/paramiko/ If you're looking for remote Python execution over SSH have a look at http://codespeak.net/py/dist/execnet.html -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 10 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From pdpinheiro at gmail.com Fri Jul 10 12:23:55 2009 From: pdpinheiro at gmail.com (pdpi) Date: Fri, 10 Jul 2009 09:23:55 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: On Jul 10, 2:11?pm, Steven D'Aprano wrote: > On Fri, 10 Jul 2009 12:54:21 +0200, Hendrik van Rooyen wrote: > > "Steven D'Aprano" wrote: > > >>On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > > >>>> persistent idea "out there" that programming is a very accessible > >>>> skill, like cooking or gardening, anyone can do it, and even profit > >>>> from it, monetarily or otherwise, etc., and to some extent I am > > >>> Programming is not like any other human activity. > > >>In practice? In principle? Programming in principle is not the same as > >>it is performed in practice. > > >>But in either case, programming requires both the logical reasoning of > >>mathematics and the creativity of the arts. Funnily enough, > > > I do not buy this arty creativity stuff. - or are you talking about > > making a website look pretty? > > I must admit, it never crossed my mind that anyone here would claim that > there was no creativity involved in programming, that it was all a > mindless, algorithmic process capable of being done by a simple > mechanical device. > > This is certainly the accusation made against *bad* programmers -- that > they can't actually solve new, unique problems, but just apply recipes > they learned without any insight or intelligence. The sort of people who > program so poorly that "a trained monkey could do what they do". I wholeheartedly agree. Coming up with Duff's device is nothing if not creative. My mind still reels at trying to grok it. http://www.lysator.liu.se/c/duffs-device.html > Even *soup stock* fits the same profile as what Hendrik claims is almost > unique to programming. On its own, soup stock is totally useless. But you > make it, now, so you can you feed it into something else later on. > > Or instant coffee. I've always found cooking an apt metaphor for programming. You've got your well-limited for loops (cook for x minutes), your less straightforward while/until loops (roast until golden), you have your subprocedures (prepare sauce in advance/in parallel), you have some conditionals (tenderize the steak if the meat isn't really that tender), etc etc. The complexities of assignment can be easily visualized in terms of containers and mixing stuff together. Nothing makes a += b more obvious than having a bowl of cream (a), an egg (b), and adding the egg to the bowl of cream (a += b). Well, except for the part where that in that case evaluating b is destructive ;) > They can't reason? Then what are they doing when they manipulate symbols? "Computers aren't intelligent. They only think they are." Or, more to the point: the typical definition of "reasoning" tends to involve more of what defines humans as self-aware, animate beings than what is usually ascribed to computers. From charles at declareSub.com Fri Jul 10 12:27:25 2009 From: charles at declareSub.com (Charles Yeomans) Date: Fri, 10 Jul 2009 12:27:25 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Jul 9, 2009, at 10:57 PM, Steven D'Aprano wrote: > On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > >> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: >> >>> Nobody says you shouldn't check your data. Only that "assert" is not >>> the right way to do that. >> >> "assert" is not the right way to check your *inputs*. It's a >> perfectly >> reasonable way to check data which "should" be valid, as well as a >> way >> to document what variables are supposed to contain. > > Where are those variables coming from? > > The distinction really boils down to this: > > * asserts should never fail. If there is any chance that an assertion > might fail outside of test suites, then don't use assert. > > > You can't control what input the caller provides to a function, so > unless > the data is generated inside the function, a caller might provide > something unexpected. Therefore, assert is inappropriate for checking > function parameters, even if that function is "private" and only > called > by your own functions. > > (1) So-called "private" or "internal" functions have a habit of > becoming > public, and then you have asserts in public functions. > > (2) If public() calls _private(x), and _private uses assert to check > the > value of x, there is a risk that if public() is buggy and supplies an > invalid x, the assertion will never be checked and _private() will > return > incorrect results. > > (3) assert is absolutely unsuitable for enforcing pre-conditions and > post- > conditions, unless such conditions are mere "guidelines", because > assert > can be switched off at runtime. Unless, of course, you want to switch off such checking at runtime, as you might when using a design-by-contract approach. Charles Yeomans From aahz at pythoncraft.com Fri Jul 10 12:33:06 2009 From: aahz at pythoncraft.com (Aahz) Date: Fri, 10 Jul 2009 09:33:06 -0700 Subject: BayPIGgies at OSCON: 7/23 8-9:30pm Message-ID: <20090710163306.GA10586@panix.com> NOTE: time change AND location change The July BayPIGgies meeting will be held at OSCON in the San Jose Convention Center as one of the BoF (Birds of a Feather) sessions from 8pm to 9:30pm Thursday July 23. Everyone is welcome: you do NOT need to be an OSCON member to attend a BoF. Wesley Chun will have a newbie-oriented "What is Python?" BoF from 7-8pm in the same room as BayPIGgies (we don't know which room yet). The July meeting is supposed to have a Django focus, but the program hasn't been settled yet, either. For more information, see http://baypiggies.net/ Discussion of details will take place on the BayPIGgies list: http://mail.python.org/mailman/listinfo/baypiggies -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From gabrielmonnerat at gmail.com Fri Jul 10 12:36:22 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Fri, 10 Jul 2009 13:36:22 -0300 Subject: problem with subprocess Message-ID: <4A576E06.5080006@gmail.com> Hi all, I need start a openoffice in Xvfb, but when I call with the DISPLAY occurs this error: [root at localhost oood]# Xvfb :99 -screen 0 1024x768x24 & In [9]: subprocess.call('/opt/ooo-dev3/program/soffice.bin') Out[9]: 0 In [10]: subprocess.call('DISPLAY=:99 /opt/ooo-dev3/program/soffice.bin') --------------------------------------------------------------------------- OSError Traceback (most recent call last) /home/gabriel/ in () /usr/lib/python2.6/subprocess.pyc in call(*popenargs, **kwargs) 442 retcode = call(["ls", "-l"]) 443 """ --> 444 return Popen(*popenargs, **kwargs).wait() 445 446 /usr/lib/python2.6/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags) 593 p2cread, p2cwrite, 594 c2pread, c2pwrite, --> 595 errread, errwrite) 596 597 # On Windows, you cannot just redirect one or two handles: You /usr/lib/python2.6/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) 1104 os.waitpid(self.pid, 0) 1105 child_exception = pickle.loads(data) -> 1106 raise child_exception 1107 1108 OSError: [Errno 2] No such file or directory I am using subprocess because I need store the pid. Any suggestions? thanks in advance, Gabriel M. Monnerat From inky788 at gmail.com Fri Jul 10 12:44:34 2009 From: inky788 at gmail.com (Inky 788) Date: Fri, 10 Jul 2009 09:44:34 -0700 (PDT) Subject: Where does setuptools live? References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> Message-ID: On Jul 10, 10:26?am, Chris Withers wrote: > Inky 788 wrote: > > Currently, distutils itself is being actively developed. More info > > about this here:http://tarekziade.wordpress.com/ > > > My (albeit anonymous) advice is: use distutils. Manually download > > packages as-needed from PyPI and install manually using standard > > distutils. > > No thanks. I'm a big fan of buildout. Making it possible for packages to > specify their dependencies is a big win... Yup, it's a big win. But package installation for Python is a bit of a mess right now. Neither setuptools nor buildout (nor pip for that matter) are a standard part of Python. It's rather silly that although Python is a batteries-included language, and it's mid-2009, and Python 3.1 has been released, that Python *still* doesn't have a standard built-in way to handle package installation (including dependencies and uninstallation). My guess is that once distutils finishes getting spruced up, some intrepid hacker is going to: * take the best parts of pip and the best parts of setuptools (I don't know anything about buildout), * stir vigorously, * ruthlessly remove the excess pieces, * write good documentation for it, * throw the result up on github/launchpad/bitbucket/whatever, and then *that's* what everyone's going to start using and which will eventually make it into the Python std lib. But that's just my anon 2 cents. From psimon at sonic.net Fri Jul 10 12:47:42 2009 From: psimon at sonic.net (Paul Simon) Date: Fri, 10 Jul 2009 09:47:42 -0700 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: <4a5770bc$0$95549$742ec2ed@news.sonic.net> "David Smith" wrote in message news:h35f78$pts$1 at ruby.cit.cornell.edu... > Paul Simon wrote: >> "Peter Otten" <__peter__ at web.de> wrote in message >> news:h3481q$d95$00$1 at news.t-online.com... >>> Paul Simon wrote: >>> >>>> "Chris Rebert" wrote in message >>>> news:mailman.2863.1247095339.8015.python-list at python.org... >>>> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>>>> I have the "tkinter" problem and need some assistance to straighten it >>>>> out. >>>>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as >>>>> >in >>>>>> "step >>>>> 1" and cannot import "_tkinter." I do not have that file on my >>>>> computer, >>>>> but >>>>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>>>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>>>> This python stuff is great, but the documentation frequently >>>>> feels like it is just a bit out of my grasp. I realize that all of >>>>> this >>>>> is free but I understand the instructions on the web page to repair >>>>> only >>>>> to the >>>>> point of confusion. I'm not an expert. How do I modify my python >>>>> configuration? Is there a file that needs to be edited? Which setup.py >>>>> file >>>>> do I use? Make? or python setup.py build and python setup.py install? >>>>> Thanks. I appreciate your help. >>>> - How did you install Python? >>>> - What Linux distro are you using? >>>> >>>> Cheers, >>>> Chris >>>> http://blog.rebertia.com >>>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not >>>> sure >>>> exactly how I installed Python. Originally I had installed 2.5 from >>>> RPM >>>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>>> something from python.org and installed. Not sure if it was tarball or >>>> zip file. >>> Zip or tar doesn't matter, you are installing "from source". >>> >>> Python has to find the necessary include files for tcl/tk. These are in >>> separate packages that you have to install before you invoke Python's >>> configure script. >>> >>> I don't know what they are called on your system -- look for tk-dev.rpm, >>> tcl-dev.rpm or similar. >>> >>> You may run into the same problem with other modules like readline. >>> >>> Peter >>> >> >> Thank you Peter. I understand what you are saying but don't know how to >> do >> it. Although I installed from source, I followed a "cookbook" recipe. >> Could you tell me what files to execute, where they might be, and file >> arguments? I'm just ignorant, not stupid. ;-). >> >> Paul >> >> > > Just install the tkinter package from the Mandriva Linux Control > Center's Software Management system. I just did it, doing a search for > tkinter brought it right up. All done. > > --David Thanks to all for your patient help. I have made some progress, but still no success. I installed Active Tcl-8.5.7 and corrected the PATH accordingly. However I still get a "missing" message on building Python. "Failed to find the necessary bits (!) to build these modules: .... _tkinter (among others) To find the necessary bits, look in setup.py in detect_modules() for teh module's name." Not sure what bits are, euphemism? but am about to wipe the disk and reinstall linux, etc. Paul From gabrielmonnerat at gmail.com Fri Jul 10 12:48:50 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Fri, 10 Jul 2009 13:48:50 -0300 Subject: problem with subprocess In-Reply-To: <4A576E06.5080006@gmail.com> References: <4A576E06.5080006@gmail.com> Message-ID: <4A5770F2.7050308@gmail.com> gabrielmonnerat wrote: > Hi all, > > I need start a openoffice in Xvfb, but when I call with the DISPLAY > occurs this error: > > [root at localhost oood]# Xvfb :99 -screen 0 1024x768x24 & > > In [9]: subprocess.call('/opt/ooo-dev3/program/soffice.bin') > Out[9]: 0 > > In [10]: subprocess.call('DISPLAY=:99 /opt/ooo-dev3/program/soffice.bin') > --------------------------------------------------------------------------- > > OSError Traceback (most recent call > last) > > /home/gabriel/ in () > > /usr/lib/python2.6/subprocess.pyc in call(*popenargs, **kwargs) > 442 retcode = call(["ls", "-l"]) > 443 """ > --> 444 return Popen(*popenargs, **kwargs).wait() > 445 > 446 > > /usr/lib/python2.6/subprocess.pyc in __init__(self, args, bufsize, > executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, > env, universal_newlines, startupinfo, creationflags) > 593 p2cread, p2cwrite, > 594 c2pread, c2pwrite, > --> 595 errread, errwrite) > 596 > 597 # On Windows, you cannot just redirect one or two > handles: You > > > /usr/lib/python2.6/subprocess.pyc in _execute_child(self, args, > executable, preexec_fn, close_fds, cwd, env, universal_newlines, > startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, > c2pwrite, errread, errwrite) > 1104 os.waitpid(self.pid, 0) > 1105 child_exception = pickle.loads(data) > -> 1106 raise child_exception > 1107 > 1108 > > OSError: [Errno 2] No such file or directory > > I am using subprocess because I need store the pid. Any suggestions? Sorry, I was forgot the parameter shell=True. i.e In [20]: subprocess.call('DISPLAY=:99 /opt/ooo-dev3/program/soffice.bin',shell=True) lack of attention mine > > thanks in advance, > > Gabriel M. Monnerat > > Gabriel M. Monnerat From jcd at sdf.lonestar.org Fri Jul 10 12:50:27 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 12:50:27 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <1247244628.8029.15.camel@aalcdl07> On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: > On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > > > On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > > > >> Nobody says you shouldn't check your data. Only that "assert" is not > >> the right way to do that. > > > > "assert" is not the right way to check your *inputs*. It's a perfectly > > reasonable way to check data which "should" be valid, as well as a way > > to document what variables are supposed to contain. > > Where are those variables coming from? > > The distinction really boils down to this: > > * asserts should never fail. If there is any chance that an assertion > might fail outside of test suites, then don't use assert. > I'm no expert, but the more I read this thread, and the more I think on it, the more I believe that asserts don't really need to exist outside of test suites. The function that assertions provide is handled in a far more robust and maintainable way by unit tests and doctests. Anything else should be handled by more descriptive exceptions. The use of assertions in regular code may just be a historical baby step on the way to real code testing. To play devils advocate for a moment, one possible use case for assert statements is if you need to test something that you can't easily get under a proper unittest or doctest. Maybe you need to know the value of a variable halfway through a method. A judicious assertion can help supplement your test suite A better solution would be to refactor so you can get the needed value under test, but if time constraints won't allow it, then make your assertion and move on, but only to help you debug the code, not to protect your code at runtime. Even then, why not use a proper Exception (unless speed is a major issue)? Even if it is only used internally in a module, I would still prefer a ValueError to an AssertionError. It tells you what's happening more clearly. And it protects you if another caller (even one internal to the class) calls it with a different set of assumptions. At minimum, I think there's a heavy burden on an author to justify the use of AssertionErrors rather than other kinds of Exceptions. Cheers, Cliff From magobin at gmail.com Fri Jul 10 12:52:47 2009 From: magobin at gmail.com (Alex) Date: Fri, 10 Jul 2009 09:52:47 -0700 (PDT) Subject: problem with keys combination! References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> <007343bd$0$9710$c3e8da3@news.astraweb.com> Message-ID: <03eeae5c-fe58-41d1-91ea-f3e5dba602dc@h11g2000yqb.googlegroups.com> Hi Steven, > As I understand it, you can't block, modify, or otherwise access Ctrl-Alt- > Del while running under Windows: it is the "Secure Attention Key", and is > designed to be virtually impossible to interfere with. It's not *quite* > impossible, but it is the deepest, darkest black magic. Microsoft makes > it close enough to impossible as makes no difference even for experienced > developers. No, is possible but for my level is quite impossible: http://www.codeproject.com/KB/winsdk/AntonioWinLock.aspx ...in this article the author move a lot a functions in a dll and make a demonstration with a VB and C code to use it...plus...there is his program that make it :-)))) I thought that python can read dll via ctypes...but it's very hard for me :-(( > > As a newbie, well, put it this way: it's like somebody saying "Hi guys, I > have a shiny new Swiss Army Knife, the one with the screwdriver and the > corkscrew. I'd like to build my own Space Shuttle -- what do I do?" > :-))))))))))....but yes We caaaaannnnnnn Alex From robert.kern at gmail.com Fri Jul 10 12:57:16 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 Jul 2009 11:57:16 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: <1247244628.8029.15.camel@aalcdl07> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <1247244628.8029.15.camel@aalcdl07> Message-ID: On 2009-07-10 11:50, J. Cliff Dyer wrote: > On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: >> On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: >> >>> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: >>> >>>> Nobody says you shouldn't check your data. Only that "assert" is not >>>> the right way to do that. >>> "assert" is not the right way to check your *inputs*. It's a perfectly >>> reasonable way to check data which "should" be valid, as well as a way >>> to document what variables are supposed to contain. >> Where are those variables coming from? >> >> The distinction really boils down to this: >> >> * asserts should never fail. If there is any chance that an assertion >> might fail outside of test suites, then don't use assert. >> > > I'm no expert, but the more I read this thread, and the more I think on > it, the more I believe that asserts don't really need to exist outside > of test suites. Actually, there is a good argument that one shouldn't use an assert statement in test suites: code can have bugs that only show up under -O so you want to be able to run your test suite under -O. -- 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 inky788 at gmail.com Fri Jul 10 12:57:46 2009 From: inky788 at gmail.com (Inky 788) Date: Fri, 10 Jul 2009 09:57:46 -0700 (PDT) Subject: Where does setuptools live? References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> Message-ID: On Jul 10, 10:26?am, Chris Withers wrote: > Inky 788 wrote: > > Currently, distutils itself is being actively developed. More info > > about this here:http://tarekziade.wordpress.com/ > > > My (albeit anonymous) advice is: use distutils. Manually download > > packages as-needed from PyPI and install manually using standard > > distutils. > > No thanks. I'm a big fan of buildout. Making it possible for packages to > specify their dependencies is a big win... Read this: http://tarekziade.wordpress.com/2009/07/03/dropping-pep-386-versions-comparison/ So, again, I don't know anything about buildout, and it might be a nice interim solution, but there are some new and exciting developments in distutils coming down the pike, and whatever becomes the standard Python package management system will very likely be based on those new developments. I just hope it all happens sooner than later. :) From manuel.graune at koeln.de Fri Jul 10 13:00:11 2009 From: manuel.graune at koeln.de (Manuel Graune) Date: Fri, 10 Jul 2009 19:00:11 +0200 Subject: Nested Classes and Instances Message-ID: Hello, as an example of what I would like to achieve, think of a street where each house has a door and a sign with a unique (per house) number on it. I tried to model this like this: class House(object): class Door(object): def __init__(self,color): self.color=color class Sign(object): def __init__(self,text): self.text=text def __init__(self, doorcolor,housenumber): self.housenumber=housenumber self.door=House.Door(doorcolor) self.sign=House.Sign(housenumber) house1=House("red","1") house2=House("blue","2") Well, so far, so good. Now, what I'd like to achive is that the text of the "sign" changes whenever the variable "housenumber" of the "parent-instance" changes or that "house1.sign.text" is a reference/pointer to "house1.housenumber" Thanks in advance, Manuel -- A hundred men did the rational thing. The sum of those rational choices was called panic. Neal Stephenson -- System of the world http://www.graune.org/GnuPG_pubkey.asc Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99 From lists at cheimes.de Fri Jul 10 13:21:43 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 10 Jul 2009 19:21:43 +0200 Subject: problem with subprocess In-Reply-To: <4A5770F2.7050308@gmail.com> References: <4A576E06.5080006@gmail.com> <4A5770F2.7050308@gmail.com> Message-ID: gabrielmonnerat wrote: >> I am using subprocess because I need store the pid. Any suggestions? > Sorry, I was forgot the parameter shell=True. > i.e > In [20]: subprocess.call('DISPLAY=:99 > /opt/ooo-dev3/program/soffice.bin',shell=True) You should avoid using the shell=True parameter. It may result in security issues and it consumes more resources. Try this: env = os.environ.copy() env["DISPLAY"] = ":99" subprocess.call(['opt/ooo-dev3/program/soffice.bin', env=env) Christian From thudfoo at opensuse.us Fri Jul 10 13:24:22 2009 From: thudfoo at opensuse.us (member thudfoo) Date: Fri, 10 Jul 2009 10:24:22 -0700 Subject: can i write a assemly language programs in python In-Reply-To: <4A573627.8090308@tim.thechases.com> References: <4A573627.8090308@tim.thechases.com> Message-ID: <3d881a310907101024o7c03448cyc45103d54e7cc4ef@mail.gmail.com> On Fri, Jul 10, 2009 at 5:37 AM, Tim Chase wrote: > m.reddy prasad reddy wrote: >> >> can any one tell me how to write assembly language programs in python...if >> no is there any other way to write the programs in python > > Bah, writing assembly language is easy in Python: > > ?print("MOV EAX, [EBX]") > ?print("XOR EBX, EBX") > > Just adjust the strings for your favorite processor architecture and Python > will produce the assembly code you want. > > Now compiling assembly to *machine* code...or calling between Python and > assembly...that's another matter. ?But writing assembly language programs in > Python? ?Easy. ;-) > "PyASM is a full-featured dynamic assembler written entirely in Python. By dynamic, I mean that it can be used to generate and execute machine code in python at runtime without requiring the generation of object files and linkage. It essentially allow 'inline' assembly in python modules on x86 platforms. "PyASM can also generate object files (for windows) like a traditional standalone assembler, although you're probably better off using one of the many freely available assemblers if this is you primary goal." http://members.verizon.net/~olsongt/usersGuide.html From nleioatt at gmail.com Fri Jul 10 13:26:07 2009 From: nleioatt at gmail.com (Nick) Date: Fri, 10 Jul 2009 10:26:07 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" References: <7xab3d9ykx.fsf@ruckus.brouhaha.com> Message-ID: <04f36636-bf25-423e-8deb-d28d81511678@f16g2000vbf.googlegroups.com> On Jul 9, 8:22 pm, Paul Rubin wrote: > Nick writes: > > text = file.readlines() > > len = len(text) > > fields = text[1].split() > > Is that intended to split the first line of the file? Remember > that arrays in python begin at index 0. no the '1st line' is garbled meta data, but thanks man From __peter__ at web.de Fri Jul 10 13:37:15 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 19:37:15 +0200 Subject: Nested Classes and Instances References: Message-ID: Manuel Graune wrote: > as an example of what I would like to achieve, think of a street > where each house has a door and a sign with a unique (per house) > number on it. I tried to model this like this: > > class House(object): > class Door(object): > def __init__(self,color): > self.color=color > class Sign(object): > def __init__(self,text): > self.text=text > def __init__(self, doorcolor,housenumber): > self.housenumber=housenumber > self.door=House.Door(doorcolor) > self.sign=House.Sign(housenumber) > > house1=House("red","1") > house2=House("blue","2") > > Well, so far, so good. Now, what I'd like to achive is that the text of > the "sign" changes whenever the variable "housenumber" of the > "parent-instance" changes or that > "house1.sign.text" is a reference/pointer to "house1.housenumber" Python doesn't support C-style pointers, but you can work around it to some degree: >>> class House(object): ... def __init__(self, housenumber): ... self.housenumber = housenumber ... self.sign = Sign(self) ... >>> class Sign(object): ... def __init__(self, house): ... self.house = house ... @property ... def text(self): return self.house.housenumber ... >>> house = House(42) >>> house.sign.text 42 >>> house.housenumber = "42b" >>> house.sign.text '42b' If you are concerned about the tight coupling between House and Sign you can modify Sign to accept a function that gets the housenumber: >>> class House(object): ... def __init__(self, n): self.housenumber = n ... >>> class Sign(object): ... def __init__(self, gettext): ... self._gettext = gettext ... @property ... def text(self): return self._gettext() ... >>> house = House(7) >>> house.sign = Sign(lambda house=house: house.housenumber) >>> house.sign.text 7 Peter From h at realh.co.uk Fri Jul 10 13:43:54 2009 From: h at realh.co.uk (Tony Houghton) Date: Fri, 10 Jul 2009 18:43:54 +0100 Subject: Running a script to build docs from setup.py References: <20090709173343.1784710d@realh.co.uk> <87fxd5ny79.fsf@benfinney.id.au> Message-ID: <20090710184354.406c26ae@realh.co.uk> On Fri, 10 Jul 2009 11:06:34 +1000 Ben Finney wrote: > Tony Houghton writes: > > > I've looked through the manual but I can't find any hooks in distutils > > for generating files at install time other than extension modules and > > .pyc files. Should I just run the script from somewhere in my setup.py > > before calling distutils' setup function? > > Indirectly related: Ian Bicking's article on using Python's ?setup.py? > as a ?Makefile? replacement: > > Thanks, but I don't think that adds much to the distutils manual. > If one is writing a ?setup.py? anyway, I think it makes sense to use > that as the build program for the whole project if possible. Good > hunting! Yes. Really I only want to write a setup.py because it makes it easier to make a debian package, but the more of the installation that's done by setup.py the better I suppose, then other people might find it useful. -- TH * http://www.realh.co.uk From wescpy at gmail.com Fri Jul 10 13:48:43 2009 From: wescpy at gmail.com (Wesley Chun) Date: Fri, 10 Jul 2009 10:48:43 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <89727cd5-a039-4295-8641-c5fe60e8b50f@m18g2000vbi.googlegroups.com> On Jul 7, 1:04?pm, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > : > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? > : > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! kj, i don't have too much to add to everyone else's response except to describe how i deal with this. i teach Python courses several times a year and realized long ago that conveying the concept of mutable vs. immutable is a key to getting up-to-speed quickly with Python as well as helping beginners. so, although techically, this is more of an intermediate topic rather than "beginner" material, i still teach it anyway, with the hopes of producing better Python programmers out of the gate, and hopefully, less frustrated ones. in fact, i dedicated an entire chapter (4) in Core Python Programming just to address this important issue. to top it all off, i end this module in the class by giving 2 quizzes, just to make sure they understood what i just told them. i put the 1st one online, so if you're curious, the PDF is at http://roadkill.com/~wesc/cyberweb/introQuiz.pdf ... the 2nd quiz is harder and involves the discussion of the differences between shallow and deep copies. so yes, not very beginner- ish stuff, hence the reason why i (re)named my course "Intro +Intermediate Python". finally, rather than the "paper tag" or alex's hotel statue analogy, i just say that variables are like Post-It® or sticky notes on objects. i can tag objects anytime, tag objects more than once, remove tags, or switch them to another object, etc. just my $0.02, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From tony.lay at lmco.com Fri Jul 10 14:03:05 2009 From: tony.lay at lmco.com (Lay, Tony) Date: Fri, 10 Jul 2009 14:03:05 -0400 Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory Message-ID: <71417B877DBC6946AFC52F0C1576C7D6474D7B524B@HVXMSP1.us.lmco.com> Trying to build python-2.6.2 ./configure --prefix=/usr/local --exec-prefix=/usr/local LDFLAGS="-L/usr/local" (runs through happily, had to make some libs local) make runs most of the way until. building '_tkinter' extension gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c /tmp/meld/Python-2.6.2/Modules/_tkinter.c -o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/_tkinter.o gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c /tmp/meld/Python-2.6.2/Modules/tkappinit.c -o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/tkappinit.o gcc -pthread -shared build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/_tkinter.o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/tkappinit.o -L/usr/X11R6/lib64 -L/usr/X11R6/lib -L/usr/local/lib -ltk8.5 -ltcl8.5 -lX11 -o build/lib.linux-i686-2.6/_tkinter.so *** WARNING: renaming "_tkinter" since importing it failed: libtk8.5.so: cannot open shared object file: No such file or directory Failed to find the necessary bits to build these modules: _sqlite3 bsddb185 sunaudiodev To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: _tkinter running build_scripts # cd /usr/local/lib # ls -la | grep libtk8.5.so -r-xr-xr-x 1 root root 1112606 Jul 10 13:28 libtk8.5.so Am I missing something, it's there? Regards, Tony Lay UNIX Administration 407-306-6559 Lockheed Martin - EBS One Company, One Team -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Anthony (Tony) Lay (tony.lay at lmco.com).vcf Type: text/x-vcard Size: 430 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 7647 bytes Desc: not available URL: From digitig at gmail.com Fri Jul 10 14:29:26 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 10 Jul 2009 19:29:26 +0100 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: 2009/7/10 Peter Otten <__peter__ at web.de>: > Don't be a fool. Have someone other than the author read the comment. That's the winning answer as far as I'm concerned. Automated tools are good for picking up some types of accidental mistakes, but for checking that comments are meaningful (and variable names, for that matter) you can't do without peer review. Think about it. What's the purpose of "enforcing standards". Just a tick in some assurance box to say "we meet these standards"? Ot to ensure something about the product quality? No automated tool -- not for a while yet, anyway -- is going to pick up comments such as: # increment x x += 1 or # You are not expected to understand this. The former is the sort of thing that any programmer might produce when against a deadline and forced to comment their code. The latter is a classic from a programming guru of old. An automatic checker that just checks that the comment exists without understanding its contents simply is not adding value but is rather petty bureaucracy that will annoy the programmers. -- Tim Rowe From clp2 at rebertia.com Fri Jul 10 14:53:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 11:53:39 -0700 Subject: Automate rsync w/ authentication In-Reply-To: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: <50697b2c0907101153k69a6b5ej88fbd3a9993ed5d2@mail.gmail.com> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: > I am trying to automate rsync to backup server A from server B. ?I > have set up a private/public key between the two servers so I don't > have to enter a password when using rsync. ?Running rsync manually > with the following command works fine: > rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp > > But when I try to do it with python, the subprocess simply returns the > ssh -h output on stderr like I am passing some invalid syntax. ?What > is wrong in my translation of rsync's -e command from shell to > pythyon? > > #! /usr/bin/python > from subprocess import Popen, PIPE > rsyncExec = '/usr/bin/ssh' > source = 'root at 10.0.45.67:/home/bry/jquery.lookup' > dest = '/home/bry/tmp' > rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] Like many problems involving the subprocess module, I think you've tokenized the arguments incorrectly. Try: rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] Note that the -e switch and its operand are separate arguments for the purposes of POSIX shell tokenization. Cheers, Chris -- http://blog.rebertia.com From edreamleo at charter.net Fri Jul 10 14:54:50 2009 From: edreamleo at charter.net (Edward K Ream) Date: Fri, 10 Jul 2009 13:54:50 -0500 Subject: ANN: Leo 4.6 rc1 released Message-ID: Leo 4.6 rc1 is now available at: http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106 Leo is a text editor, data organizer, project manager and much more. See: http://webpages.charter.net/edreamleo/intro.html The highlights of Leo 4.6: -------------------------- - Cached external files *greatly* reduces the time to load .leo files. - Leo now features a modern Qt interface by default. Leo's legacy Tk interface can also be used. - New --config, --file and --gui command-line options. - Leo tests syntax of .py files when saving them. - Leo can now open any kind of file into @edit nodes. - @auto-rst nodes allow easy editing of reStructuredText files. - Properties of commanders, positions and nodes simplify programming. - Improved Leo's unit testing framework. - Leo now requires Python 2.5 or later. - Dozens of small improvements and bug fixes. Links: ------ Leo: http://webpages.charter.net/edreamleo/front.html Forum: http://groups.google.com/group/leo-editor Download: http://sourceforge.net/project/showfiles.php?group_id=3458 Bzr: http://code.launchpad.net/leo-editor/ Quotes: http://webpages.charter.net/edreamleo/testimonials.html -------------------------------------------------------------------- Edward K. Ream email: edreamleo at yahoo.com Leo: http://webpages.charter.net/edreamleo/front.html -------------------------------------------------------------------- From jcd at sdf.lonestar.org Fri Jul 10 14:56:48 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 14:56:48 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <1247244628.8029.15.camel@aalcdl07> Message-ID: <1247252208.11457.6.camel@aalcdl07> On Fri, 2009-07-10 at 11:57 -0500, Robert Kern wrote: > On 2009-07-10 11:50, J. Cliff Dyer wrote: > > On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: > >> On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > >> > >>> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > >>> > >>>> Nobody says you shouldn't check your data. Only that "assert" is not > >>>> the right way to do that. > >>> "assert" is not the right way to check your *inputs*. It's a perfectly > >>> reasonable way to check data which "should" be valid, as well as a way > >>> to document what variables are supposed to contain. > >> Where are those variables coming from? > >> > >> The distinction really boils down to this: > >> > >> * asserts should never fail. If there is any chance that an assertion > >> might fail outside of test suites, then don't use assert. > >> > > > > I'm no expert, but the more I read this thread, and the more I think on > > it, the more I believe that asserts don't really need to exist outside > > of test suites. > > Actually, there is a good argument that one shouldn't use an assert statement in > test suites: code can have bugs that only show up under -O so you want to be > able to run your test suite under -O. > That's an interesting point. Presumably TestCase.assert_() doesn't suffer from this defect? Otherwise the entire unittest suite is essentially broken by your argument. I suppose I should have said one should only raise AssertionErrors in test suites. Practically speaking, that's what a test suite is: The place where you assert what the code does. From jcd at sdf.lonestar.org Fri Jul 10 14:59:29 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 14:59:29 -0400 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: <3A693537-927D-44B1-840A-D97FACFB6809@pyth.net> References: <3A693537-927D-44B1-840A-D97FACFB6809@pyth.net> Message-ID: <1247252369.11457.8.camel@aalcdl07> On Thu, 2009-07-09 at 13:53 +0000, Fri?rik M?r J?nsson wrote: > Look at: > > len = len(text) > > You're overriding `len` (a built-in method), with an integer > (`len(text)`). You then call: > > for i in range(len(fields)): > > But `len` is no longer a callable, but merely an integer. > > Regards, > Fri?rik M?r > > P.S. While this is a fairly obvious problem it's usually a good idea > to post working code and a traceback when requesting help. While we're on the subject of good question posting form: The body of your post should contain all relevant information. Please don't make readers look back at the subject heading for the statement of the problem. Duplicating the statement of the problem in the subject and the body is ok, but it really ought to be in the body. From Scott.Daniels at Acm.Org Fri Jul 10 14:59:50 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 11:59:50 -0700 Subject: tough-to-explain Python In-Reply-To: <00733dd0$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> <00733dd0$0$9710$c3e8da3@news.astraweb.com> Message-ID: <4-CdnSJso5pwEMrXnZ2dnUVZ_sVi4p2d@pdx.net> Steven D'Aprano wrote: > On Fri, 10 Jul 2009 08:28:29 -0700, Scott David Daniels wrote: >> Steven D'Aprano wrote: >>> Even *soup stock* fits the same profile as what Hendrik claims is >>> almost unique to programming. On its own, soup stock is totally >>> useless. But you make it, now, so you can you feed it into something >>> else later on. >>> Or instant coffee. >> I think I'll avoid coming to your house for a cup of coffee. :-) > I meant the instant coffee powder is prepared in advance. It's useless on > it's own, but later on you feed it into boiling water, add sugar and > milk, and it's slightly less useless. I know, but the image of even a _great_ soup stock with instant coffee poured in, both appalled me and made me giggle. So, I thought I'd share. --Scott David Daniels Scott.Daniels at Acm.Org From robert.kern at gmail.com Fri Jul 10 15:02:58 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 Jul 2009 14:02:58 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: <1247252208.11457.6.camel@aalcdl07> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <1247244628.8029.15.camel@aalcdl07> <1247252208.11457.6.camel@aalcdl07> Message-ID: On 2009-07-10 13:56, J. Cliff Dyer wrote: > On Fri, 2009-07-10 at 11:57 -0500, Robert Kern wrote: >> On 2009-07-10 11:50, J. Cliff Dyer wrote: >>> On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: >>>> On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: >>>> >>>>> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: >>>>> >>>>>> Nobody says you shouldn't check your data. Only that "assert" is not >>>>>> the right way to do that. >>>>> "assert" is not the right way to check your *inputs*. It's a perfectly >>>>> reasonable way to check data which "should" be valid, as well as a way >>>>> to document what variables are supposed to contain. >>>> Where are those variables coming from? >>>> >>>> The distinction really boils down to this: >>>> >>>> * asserts should never fail. If there is any chance that an assertion >>>> might fail outside of test suites, then don't use assert. >>>> >>> I'm no expert, but the more I read this thread, and the more I think on >>> it, the more I believe that asserts don't really need to exist outside >>> of test suites. >> Actually, there is a good argument that one shouldn't use an assert statement in >> test suites: code can have bugs that only show up under -O so you want to be >> able to run your test suite under -O. >> > > That's an interesting point. Presumably TestCase.assert_() doesn't > suffer from this defect? Otherwise the entire unittest suite is > essentially broken by your argument. It explicitly raises AssertionErrors. It does not use the assert statement. > I suppose I should have said one > should only raise AssertionErrors in test suites. Practically speaking, > that's what a test suite is: The place where you assert what the code > does. Yup. -- 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 mgi820 at motorola.com Fri Jul 10 15:03:49 2009 From: mgi820 at motorola.com (Gary Duzan) Date: Fri, 10 Jul 2009 19:03:49 +0000 (UTC) Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: In article <3af970b1-b454-4d56-a33f-889ecfacacc5 at l28g2000vba.googlegroups.com>, Bryan wrote: > >rsyncExec = '/usr/bin/ssh' >source = 'root at 10.0.45.67:/home/bry/jquery.lookup' >dest = '/home/bry/tmp' >rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] I think you want -e and the ssh command to be separate args. Something like: rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] or: rshArgs = [ '-e', '/usr/bin/ssh -i /home/bry/keys/brybackup.key' ] args = [rsyncExec, '-a', '-v', '--dry-run'] + rshArgs + [ source, dest] Gary Duzan Motorola H&NM From clp2 at rebertia.com Fri Jul 10 15:11:12 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 12:11:12 -0700 Subject: Colour of output text In-Reply-To: References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: <50697b2c0907101211u5d6ddc80ra85857075c986217@mail.gmail.com> On Fri, Jul 10, 2009 at 2:23 AM, wrote: > Tim Harig wrote: >> On 2009-07-09, Alex Rosslyn wrote: >>> I would like to learn a way of changing the colour of a particular >>> part of the output text. I've tried the following > >> On Unix operating systems this would be done through the curses interface: >> >> http://docs.python.org/library/curses.html > > Or using ANSI colour codes: > > colours = { > ? ? ? ? ? ?'beep' ? ? ? : ? ?"\007", Sound is a color? Maybe if you have synaesthesia... Cheers, Chris -- http://blog.rebertia.com From walterbyrd at iname.com Fri Jul 10 15:22:15 2009 From: walterbyrd at iname.com (walterbyrd) Date: Fri, 10 Jul 2009 12:22:15 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? Message-ID: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> I believe Guido himself has said that all indentions should be four spaces - no tabs. Since backward compatibility is being thrown away anyway, why not enforce the four space rule? At least that way, when I get python code from somebody else, I would know what I am looking at, without having to do a hex dump, or something. From marty.musatov at gmail.com Fri Jul 10 15:24:27 2009 From: marty.musatov at gmail.com (Musatov) Date: Fri, 10 Jul 2009 12:24:27 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: Fran?ois Grondin wrote: > "David Bernier" a ?crit dans le message de news: > h36ki102d7m at news5.newsguy.com... > > Musatov wrote: > >> On Jul 9, 7:54 pm, David Bernier wrote: > >>> Musatov wrote: > >>>> Los Angeles (AP) --MeAmI.org now has users in 50 countries following > >>>> its adopted use in Pakistan. The search engine has grown in > >>>> popularity 10,000 fold following its Beta test launch three months ago > >>>> in April, 2009. Supporters of the site claim it is better than rival > >>>> Google upon which platform it is based. Controversy arose after > >>>> MeAmI.org search code allowed users to search other users Google > >>>> results with no advertising. "It is truly an innovative thing we are > >>>> doing," said Founder and CEO, Martin Musatov. "Letting users search > >>>> the results of other Google users immediately results in a level of > >>>> accuracy and relevance above and beyond Google." Google changed their > >>>> API following the launch or MeAmI.org and explored the possibility of > >>>> blocking site access from MeAmI.org to Google search results but was > >>>> unable to do so. Critics of MeAmI.org say what it is doing is > >>>> tantamount to intellectual theft. When asked about this topper Musatov > >>>> exclaimed, "The Internet was made for people, not companies." An > >>>> analyst at Goldman Sachs says, requesting to remain anonymous, > >>>> "MeAmI.org has a strong presence in promoting itself as a vehicle for > >>>> global activism and to tell you the truth, this makes it much more > >>>> likely an acquisition target than potential intellectual property > >>>> violator." Google could not be reached for comment. > >>> Mr. Musatov, do you know who originally wrote the > >>> article above? > >>> > >>> Thank you. > >>> > >>> David Bernier- Hide quoted text - > >>> > >>> - Show quoted text - > >> > >> Yes. > > > > Mr. Musatov, do you know the name of the person who > > originally wrote the article above? > > > > Thank you. > > > > David Bernier > > Maybe Mr. Musatov should answer the following questions instead : > 1. Did you write the article above? > 2. If not, who did? And I don't want AP as the answer, but the name of the > journalist. > > David, don't take it bad, but he answered your question with an accurate > answer (yes), You gave him the opportunity to avoid the real answer and he > took it. Based on Musatov's strange behavior and logic, don't expect more > from him. Ask anyone else the same question and you'd get a real answer. > > BTW, my guess for question 2 would be the Masked Logician, Floetry, > scriber77, or Professor X. > > Francois 1. Yes. From aahz at pythoncraft.com Fri Jul 10 15:26:14 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 12:26:14 -0700 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: In article <260f0f1f-1115-4db8-a955-74c9f459ecc3 at h30g2000vbr.googlegroups.com>, walterbyrd wrote: > >I believe Guido himself has said that all indentions should be four >spaces - no tabs. > >Since backward compatibility is being thrown away anyway, why not >enforce the four space rule? Probably the main reason is that Guido also hates non-functional code changes because it messes up version history. Not sure, though. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From kwmsmith at gmail.com Fri Jul 10 15:29:09 2009 From: kwmsmith at gmail.com (Kurt Smith) Date: Fri, 10 Jul 2009 14:29:09 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: On Fri, Jul 10, 2009 at 2:22 PM, walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. > > Since backward compatibility is being thrown away anyway, why not > enforce the four space rule? > > At least that way, when I get python code from somebody else, I would > know what I am looking at, without having to do a hex dump, or > something. What you propose has already been (forcefully) rejected: http://www.python.org/dev/peps/pep-0666/ From clp2 at rebertia.com Fri Jul 10 15:29:59 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 12:29:59 -0700 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: <50697b2c0907101229l720e9c92y94a921dc5c58de52@mail.gmail.com> On Fri, Jul 10, 2009 at 12:22 PM, walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. That's a (very good) recommendation at most. http://www.python.org/dev/peps/pep-0666/ Cheers, Chris -- http://blog.rebertia.com From tjreedy at udel.edu Fri Jul 10 15:30:04 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 15:30:04 -0400 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: jhinak sen wrote: > hi, > i am a beginner in python language, Welcome to Python. > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the > mean and sum in some other file : This is three things: input data from, perform calculation, output data to file. I would start simpler. See below. You did two things right that too many people do not do. 1. You posted a complete chunk of code. > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * You never use these. > > f0= open("temp9","r+").readlines() [snip] > f0.close() > f2.close()* 2. You posted the complete traceback (instead of the annoying 'program didn't work ;-). > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" > > please help to to find the error. Seeing that, any of us could tell you thought f0 was a file but it was actually a list. Looking back up through the code, people found the definition -- the output of file.readlines, which is a list. > or suggest some simpler or better way Develop more incrementally. If you edit with IDLE, for instance, and hit RUN (F5), it takes about a second to see the result for a small program like this. I would have suggested starting with data = [(1,2), (3,4)] ... print "input: ", a, b, "output: ", tot, ave and fill in ... until the output was correct. Then change data to ['1 2', '3 4'] and revise until correct. At that point, change data to open(....) and the program should otherwise work without change because a list of strings and a file are both iterators that produce a sequence of strings. Now, if you want, worry about formating the output, removing the echo of the input. Very last, send to output to a disk file instead of the screen. For development, that is a nuisance because it takes time to open the file to check results. So only do that when you already know the results are correct. Note that real programs used repeatedly ofter do not hard code an output file. If run from a command line in a comman window, screen output can be redirected to a file with '>': "myprog > outfile". Terry Jan Reedy From piet at cs.uu.nl Fri Jul 10 15:43:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 10 Jul 2009 21:43:21 +0200 Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: >>>>> Chris Rebert (CR) wrote: >CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: >>> I am trying to automate rsync to backup server A from server B. ?I >>> have set up a private/public key between the two servers so I don't >>> have to enter a password when using rsync. ?Running rsync manually >>> with the following command works fine: >>> rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" >>> root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp >>> >>> But when I try to do it with python, the subprocess simply returns the >>> ssh -h output on stderr like I am passing some invalid syntax. ?What >>> is wrong in my translation of rsync's -e command from shell to >>> pythyon? >>> >>> #! /usr/bin/python >>> from subprocess import Popen, PIPE >>> rsyncExec = '/usr/bin/ssh' >>> source = 'root at 10.0.45.67:/home/bry/jquery.lookup' >>> dest = '/home/bry/tmp' >>> rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>> args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] >CR> Like many problems involving the subprocess module, I think you've >CR> tokenized the arguments incorrectly. Try: >CR> rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >CR> args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] >CR> Note that the -e switch and its operand are separate arguments for the >CR> purposes of POSIX shell tokenization. I think you should have only one kind of quotes in rshArg: rshArg = "/usr/bin/ssh -i /home/bry/keys/brybackup.key" I haven't tried it, however, but this is just how Unix works. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From clp2 at rebertia.com Fri Jul 10 15:47:59 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 12:47:59 -0700 Subject: Automate rsync w/ authentication In-Reply-To: References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: <50697b2c0907101247g274c270bocfda4e82d3fb6617@mail.gmail.com> On Fri, Jul 10, 2009 at 12:43 PM, Piet van Oostrum wrote: >>>>>> Chris Rebert (CR) wrote: > >>CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: >>>> I am trying to automate rsync to backup server A from server B. ?I >>>> have set up a private/public key between the two servers so I don't >>>> have to enter a password when using rsync. ?Running rsync manually >>>> with the following command works fine: >>>> rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" >>>> root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp >>>> >>>> But when I try to do it with python, the subprocess simply returns the >>>> ssh -h output on stderr like I am passing some invalid syntax. ?What >>>> is wrong in my translation of rsync's -e command from shell to >>>> pythyon? >>>> >>>> #! /usr/bin/python >>>> from subprocess import Popen, PIPE >>>> rsyncExec = '/usr/bin/ssh' >>>> source = 'root at 10.0.45.67:/home/bry/jquery.lookup' >>>> dest = '/home/bry/tmp' >>>> rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>>> args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] > >>CR> Like many problems involving the subprocess module, I think you've >>CR> tokenized the arguments incorrectly. Try: > >>CR> rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>CR> args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] > >>CR> Note that the -e switch and its operand are separate arguments for the >>CR> purposes of POSIX shell tokenization. > > I think you should have only one kind of quotes in rshArg: > rshArg = "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > > I haven't tried it, however, but this is just how Unix works. Ah, indeed, I think you're probably right. Just goes to show it's not always easy to get exactly right. Cheers, Chris -- http://blog.rebertia.com From jcd at sdf.lonestar.org Fri Jul 10 16:12:11 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 16:12:11 -0400 Subject: Nested Classes and Instances In-Reply-To: References: Message-ID: <1247256731.11457.76.camel@aalcdl07> On Fri, 2009-07-10 at 19:00 +0200, Manuel Graune wrote: > Hello, > > as an example of what I would like to achieve, think of a street > where each house has a door and a sign with a unique (per house) > number on it. I tried to model this like this: > > class House(object): > class Door(object): > def __init__(self,color): > self.color=color > class Sign(object): > def __init__(self,text): > self.text=text > def __init__(self, doorcolor,housenumber): > self.housenumber=housenumber > self.door=House.Door(doorcolor) > self.sign=House.Sign(housenumber) > > house1=House("red","1") > house2=House("blue","2") > Don't do it like that. Keep your classes independent. Many houses can have doors, and there's no reason to redefine the concept of Doors for each one. Same goes for Signs. class House(object): def __init__(self, doorcolor, housenumber): self.door = Door(doorcolor) self.sign = Sign(housenumber) self.number = housenumber class Door(object): def __init__(self, color): self.color = color class Sign(object): def __init__(self, inscription): self.inscription = str(housenumber) (Something like that) Cheers, Cliff > Well, so far, so good. Now, what I'd like to achive is that the text of > the "sign" changes whenever the variable "housenumber" of the > "parent-instance" changes or that > "house1.sign.text" is a reference/pointer to "house1.housenumber" > > Thanks in advance, > > Manuel > > > > -- > A hundred men did the rational thing. The sum of those rational choices was > called panic. Neal Stephenson -- System of the world > http://www.graune.org/GnuPG_pubkey.asc > Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99 From bryanvick at gmail.com Fri Jul 10 16:16:02 2009 From: bryanvick at gmail.com (Bryan) Date: Fri, 10 Jul 2009 13:16:02 -0700 (PDT) Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: On Jul 10, 12:43?pm, Piet van Oostrum wrote: > >>>>> Chris Rebert (CR) wrote: > >CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: > >>> I am trying to automate rsync to backup server A from server B. ?I > >>> have set up a private/public key between the two servers so I don't > >>> have to enter a password when using rsync. ?Running rsync manually > >>> with the following command works fine: > >>> rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > >>> r... at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp > > >>> But when I try to do it with python, the subprocess simply returns the > >>> ssh -h output on stderr like I am passing some invalid syntax. ?What > >>> is wrong in my translation of rsync's -e command from shell to > >>> pythyon? > > >>> #! /usr/bin/python > >>> from subprocess import Popen, PIPE > >>> rsyncExec = '/usr/bin/ssh' > >>> source = 'r... at 10.0.45.67:/home/bry/jquery.lookup' > >>> dest = '/home/bry/tmp' > >>> rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > >>> args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] > >CR> Like many problems involving the subprocess module, I think you've > >CR> tokenized the arguments incorrectly. Try: > >CR> rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > >CR> args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] > >CR> Note that the -e switch and its operand are separate arguments for the > >CR> purposes of POSIX shell tokenization. > > I think you should have only one kind of quotes in rshArg: > rshArg = "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > > I haven't tried it, however, but this is just how Unix works. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org I tried removing the internal quotes, and splitting '-e' from the other arguments. I still get the same ssh -h output however: rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] p = Popen(args, stdout=PIPE, stderr=PIPE) From tjreedy at udel.edu Fri Jul 10 16:27:12 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:27:12 -0400 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Thu, 09 Jul 2009 23:07:34 -0400, Terry Reedy wrote: >> >> The is *not* a bug is Bentley program. This is *not* a bug in Bentley's program. > Wow. That's an impressive set of typos :) 3. Way beneath my usual standards ;-) >> It is a bug in bad, buggy, insane >> integer arithmetic implementations. low + high should either return the >> right answer, as it will in Python, or raise an overflow error. > > Is binary search *supposed* to raise an overflow error if given more than > 2**30 elements? No. it is supposed to work, and Bentley's program will work if lo and hi are actually integers, as his program presupposes, and not residue classes mislabeled 'int'. > Is there something special about OverflowError that is "better" than > ArrayIndexOutOfBoundsException? Wrong comparison. The actual alternative to OverflowError is a negative number as the sum of two positive numbers. If one claims that a and b are positive ints, then returning a negative is a bug. The index exception was a side effect, in Java, of using the negative as an index. In C, the side effect was a segmentation fault. Do you seriously question that OverflowError is better than seg faulting? A different program could go on to silently return a wrong answer. Perhaps it would say to go west instead of east, or to reduce a medical dosage instead of raising it. Note that negative ints are legal indexes in Python, so that if any Python version ever had ints that wrapped instead of overflowing or returning a long, then the program would not necessarily stop. > but there's no reason to imagine that the book will assume -- or even > state as a requirement for binary search -- that addition will never > overflow. Bentley assumed that if (lo+hi)/2 were successfully calculated, then it would be a positive number between lo and hi, and therefore a legal index. The dirty secret of computing is that some languages do not have integer types. They do not even have restricted-range integer types, which would noisily fail when they cannot perform an operation. They only have residue class types, which are something else, and which can give bizarre results if one mindlessly uses them as if they really were restricted-range integers. When one wants integer operations, every operation with two residue-class variables has a potential *silent* bug. Being relieved of the burden of constantly keeping this in mind is one reason I use Python. Float types are usually restricted range types. A huge_float + huge_float may raise overflow, but never returns a negative float. Bentley assumed that indexes hi and lo are integers. If one uses restricted range integers that are too large, then lo+hi could fail gracefully with an appropriate effor message. I would not consider that a bug, bug a limitation due to using limited-range numbers. If one uses residue classes instead of integers, and makes no adjustment, I consider it wrong to blame Bentley. Terry Jan Reedy From vivainio at gmail.com Fri Jul 10 16:30:37 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Fri, 10 Jul 2009 13:30:37 -0700 (PDT) Subject: ANN: Leo 4.6 rc1 released References: Message-ID: <56d676ba-b2fc-4288-a37e-0825291369e0@n11g2000yqb.googlegroups.com> On Jul 10, 9:54?pm, "Edward K Ream" wrote: > The highlights of Leo 4.6: > -------------------------- > - Leo now features a modern Qt interface by default. > ? Leo's legacy Tk interface can also be used. And to drive home this point (Qt ui), some screenshots for the visually oriented: http://imgbin.org/images/625.png http://imgbin.org/images/626.png http://imgbin.org/images/627.png From tjreedy at udel.edu Fri Jul 10 16:32:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:32:17 -0400 Subject: can i write a assemly language programs in python In-Reply-To: <4A572AA1.7010207@ieee.org> References: <4A55F392.3080404@ieee.org> <4A572AA1.7010207@ieee.org> Message-ID: Dave Angel wrote: > Terry Reedy wrote: > If you mean dis.dis() that only gives you byte code. No, dis is a disassembler and it gives readable assembly code, not the raw numerical bytes. Its purpose is to make byte code + other parts of the code object humanly readable. As far as I know, there is no assembler that would turn the exact output of dis back to its input. tjr From Scott.Daniels at Acm.Org Fri Jul 10 16:38:06 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 13:38:06 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> Message-ID: Raymond Hettinger wrote: > [Scott David Daniels] >> def most_frequent(arr, N): ... > In Py2.4 and later, see heapq.nlargest(). I should have remembered this one > In Py3.1, see collections.Counter(data).most_common(n) This one is from Py3.2, I think. --Scott David Daniels Scott.Daniels at Acm.Org From tjreedy at udel.edu Fri Jul 10 16:47:06 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:47:06 -0400 Subject: Query regarding set([])? In-Reply-To: References: Message-ID: vox wrote: > Hi, > I'm contsructing a simple compare-script and thought I would use set > ([]) to generate the difference output. But I'm obviosly doing > something wrong. > > file1 contains 410 rows. > file2 contains 386 rows. > I want to know what rows are in file1 but not in file2. > > This is my script: > s1 = set(open("file1")) > s2 = set(open("file2")) > s3 = set([]) > s1temp = set([]) > s2temp = set([]) > > s1temp = set(i.strip() for i in s1) > s2temp = set(i.strip() for i in s2) > s3 = s1temp-s2temp > > print len(s3) > > Output is 119. AFAIK 410-386=24. What am I doing wrong here? Assuming that every line in s2 is in s1. If there are lines in s2 that are not in s1, then the number of lines in s1 not in s2 will be larger than 24. s1 - s2 subtracts the intersection of s1 and s2. From tjreedy at udel.edu Fri Jul 10 16:53:53 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:53:53 -0400 Subject: problem with keys combination! In-Reply-To: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Message-ID: Alex wrote: > Hi at all, > I made a simple program that make a screenshot of Desktop and use it > as fullscreen background and then a ball erase image making illusion > that erase Desktop. The program working fine and I succesfully blocked > all keys but I have a problem with hotkey combination Ctrl-Alt- > Del...that bypass my FullScreen Application. Blocking Ctrl-Alt-Del leaves the power switch or maybe the plug as the only way for the user to regain control. Why would you want to do that? From ivlenin at gmail.com Fri Jul 10 16:54:08 2009 From: ivlenin at gmail.com (I V) Date: 10 Jul 2009 22:54:08 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <4a57aa70@news.x-privat.org> On Fri, 10 Jul 2009 16:27:12 -0400, Terry Reedy wrote: > a bug, bug a limitation due to using limited-range numbers. If one uses > residue classes instead of integers, and makes no adjustment, I consider > it wrong to blame Bentley. But it was Bentley himself who used the C int type, so it hardly seems unreasonable to blame him. From Scott.Daniels at Acm.Org Fri Jul 10 17:07:28 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 14:07:28 -0700 Subject: Sorry about that, the Counter class is there. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> Message-ID: Scott David Daniels wrote: > Raymond Hettinger wrote: >> [Scott David Daniels] >>> def most_frequent(arr, N): ... >> In Py2.4 and later, see heapq.nlargest(). > I should have remembered this one > >> In Py3.1, see collections.Counter(data).most_common(n) > This one is from Py3.2, I think. Oops -- egg all over my face. I thought I was checking with 3.1, and it was 2.6.2. I _did_ make an explicit check, just poorly. Again, apologies. --Scott David Daniels Scott.Daniels at Acm.Org From noen Fri Jul 10 17:16:13 2009 From: noen (earthlink) Date: Fri, 10 Jul 2009 17:16:13 -0400 Subject: 2.4 VS 3.1 for simple print Message-ID: Why does print "GarbageCollector: collected %d objects." % (gc.collect()) work in 2.4 but not in 3.1? From Scott.Daniels at Acm.Org Fri Jul 10 17:24:42 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 14:24:42 -0700 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: jacopo mondi wrote: > Roger Binns wrote: >> jacopo mondi wrote: >>> Hi all, I need to patch socketmodule.c (the _socket module) in order to >>> add support to an experimental socket family. >> You may find it considerably easier to use ctypes since that will avoid >> the need for any patching. You'll also be able to control how read and >> write are done (eg read vs recvfrom vs recvmsg vs readv). You can use >> os.fdopen to convert your raw file descriptor into a Python file object >> if appropriate. The typical Python way of dealing with this is an additional module, not a modified module placed back in the library. So, take the sources and edit, but change the module name. Even better is figure out how to use _socket.pyd, to create a smaller _socketexpmodule.c and use that. --Scott David Daniels Scott.Daniels at Acm.Org From clp2 at rebertia.com Fri Jul 10 17:28:07 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 14:28:07 -0700 Subject: 2.4 VS 3.1 for simple print In-Reply-To: References: Message-ID: <50697b2c0907101428q76fcef47me9e389c5aeee66f3@mail.gmail.com> On Fri, Jul 10, 2009 at 2:16 PM, earthlink wrote: > Why does > print "GarbageCollector: collected %d objects." % (gc.collect()) > > work in 2.4 but not in 3.1? Define "works". What error are you getting? Include the exact message and full error traceback. Or if no exception is being raised, explain exactly how is it behaving differently from your expectations. Cheers, Chris -- http://blog.rebertia.com From martin at v.loewis.de Fri Jul 10 17:30:53 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 10 Jul 2009 23:30:53 +0200 Subject: 2.4 VS 3.1 for simple print In-Reply-To: References: Message-ID: <4a57b30e$0$15912$9b622d9e@news.freenet.de> > Why does > print "GarbageCollector: collected %d objects." % (gc.collect()) > > work in 2.4 but not in 3.1? Because print is a function in 3.1, so you have to follow it with a parenthesis. Regards, Martin From lkcl at lkcl.net Fri Jul 10 17:37:46 2009 From: lkcl at lkcl.net (Luke Kenneth Casson Leighton) Date: Fri, 10 Jul 2009 21:37:46 +0000 Subject: [ANN] Pyjamas 0.6pre1 ALPHA release of Pyjamas Widget Set Message-ID: http://pyjs.org - Pyjamas is a port of GWT to Python that can run applications both on the Desktop (like python-gtk2) and in all major Web Browsers (as javascript). This is an alpha release - 0.6pre1 - of the Pyjamas Web Widget Set. It is a significant upgrade, incorporating Pyjamas Desktop which can now use Mozilla XULRunner as well as PyWebKitGtk as the browser engine. Significant enhancements have been made to the javascript compiler, which bring python strict features as well as a relaxed (and faster) compile-time option. The reason for the 0.6 pre-release is due to the number of features and improvements added. Many thanks to Kees, Lovely Systems, and all the people from EuroPython 2009 who have helped contribute and generally make Pyjamas fun to work with. Downloads are available from: http://code.google.com/p/pyjamas http://sourceforge.net/projects/pyjamas http://pypi.python.org/pypi/Pyjamas From matt.dubins at sympatico.ca Fri Jul 10 17:39:14 2009 From: matt.dubins at sympatico.ca (inkhorn) Date: Fri, 10 Jul 2009 14:39:14 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> Message-ID: Thanks all!! I found the following to be most helpful: any(substr in long_string for substr in list_of_strings) This bang-for-your-buck is one of the many many reasons why I love Python programming :) Matt Dubins From piet at cs.uu.nl Fri Jul 10 17:46:23 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 10 Jul 2009 23:46:23 +0200 Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: >>>>> Bryan (B) wrote: >B> I tried removing the internal quotes, and splitting '-e' from the >B> other arguments. I still get the same ssh -h output however: >B> rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' >B> args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, >B> dest] >B> p = Popen(args, stdout=PIPE, stderr=PIPE) For me it works. Of course I substituted local server names, file names etc. I run this on Mac OS X 10.4.11. The server is some Linux system. ------------------------------------------------------------------------ #! /usr/bin/env python from subprocess import Popen, PIPE rsyncExec = '/usr/local/bin/rsync' source = 'xxx.cs.uu.nl:/users/piet/yyyyyy' dest = '/Users/piet/TEMP/test.rsync' rshArg = '/usr/bin/ssh -i /Users/piet/.ssh/id_rsa' args = [rsyncExec, '-a', '-v', '-e', rshArg, source, dest] try: p = Popen(args, stdout=PIPE, stderr=PIPE) print 'rsync running with pid %s' % p.pid out, err = p.communicate() print 'Errors: %s' % err print 'Output: %s' % out except Exception: print 'Error running rsync' ------------------------------------------------------------------------ It just copies the file. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From timlee126 at yahoo.com Fri Jul 10 17:52:09 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 10 Jul 2009 14:52:09 -0700 (PDT) Subject: main in Python Message-ID: <159869.41728.qm@web63104.mail.re1.yahoo.com> Hi, I learned that a Python script is written in this way: def main(): ... if __name__ == "__main__": main() Today, when I read a script, I found it has a different way: def main(): ... main() It can run as well. Can someone explain why and the rules that Python scripts get run? Thanks and regards, Tim From jjposner at optimum.net Fri Jul 10 18:01:06 2009 From: jjposner at optimum.net (John Posner) Date: Fri, 10 Jul 2009 18:01:06 -0400 Subject: Tkinter only: table widget with canvas... In-Reply-To: References: Message-ID: <4A57BA22.5030201@optimum.net> Hi -- > > a) Assume I would have some different widgets to add per row. How > do I get the maximum row height? If you are placing widgets in a table like this: w.grid(row=a, column=b) ... where the *master* of widget "w" is a Tkinter.Frame named "table_frm", then you can determine the height of row N like this: table_frm.grid_bbox(column=0, row=N) Quoth the grid_bbox() documentation (with the object name changed for clarity): Returns a 4-tuple describing the bounding box of some or all of the grid system in widget |/|table_frm|/|. The first two numbers returned are the /|x|/ and /|y|/ coordinates of the upper left corner of the area, and the second two numbers are the width and height. So the final member of the 4-tuple is the height you want. > > b) Assume something like a label in one column. The length of all > texts > in a column will differ. How do I choose the maxium column width? The third member of grid_bbox() 4-tuple is the cell-width. So just find the maximum of these values: table_frm.grid_bbox(column=0, row=0)[2] table_frm.grid_bbox(column=1, row=0)[2] table_frm.grid_bbox(column=2, row=0)[2] ... > > c) Placing headers in a canvas does not look like a good idea because > I don't want to scroll the headers. Am I right? > c.1) How do I place a none scrollable header in a canvas? or > c.2) How do I place all headers outside the canvas correctly above > the relating column? > "c.2" is what you want. Again, if the table is in a Tkinter.Frame named "frm", you want to create an outer frame, then pack a header frame and the table frame into it, vertically: rt = Tk() outer_frm = Frame(rt) outer_frm.pack(expand=True, fill=BOTH) header_frm = Frame(outer_frm, height=25) header_frm.pack(side=TOP) table_frm = Frame(outer_frm) table_frm.pack(side=TOP) (For scrolling, consider making "table_frm" a Pmw.ScrolledFrame instead of a Tkinter.Frame.) As for getting the heading labels correctly placed above the columns, pack correctly-sized subframes (Tkinter.Frame's) into "header_frm". To determine the size of a column, use grid_bbox() again: BBOX_WIDTH_COMPONENT = 2 header_subfrm = Frame(header_frm, relief=GROOVE, bd=2) header_subfrm['width'] = \ table_frm.grid_bbox(column=N, row=0)[BBOX_WIDTH_COMPONENT] Then, use place() to center a label within the subframe: header_label = Label(header_subfrm, text="Heading") header_label.place(relx=0.5, rely=0.5, anchor=CENTER) There's a working app at http://cl1p.net/tkinter_table_headers/ -John From ethan at stoneleaf.us Fri Jul 10 18:11:08 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 10 Jul 2009 15:11:08 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: <006eb26f$0$9711$c3e8da3@news.astraweb.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006eb26f$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A57BC7C.5030705@stoneleaf.us> Steven D'Aprano wrote: > On Mon, 06 Jul 2009 21:02:19 -0700, Aahz wrote: > > >>In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, Steven D'Aprano >> wrote: >> >>>On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >>> >>>>kj wrote: >>>> >>>>> sense = cmp(func(hi), func(lo)) >>>>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >>>> >>>>As already said before, unlike other languages, sense in english does >>>>**not** mean direction. You should rewrite this part using a better >>>>name. Wrong informations are far worse than no information at all. >>> >>>Absolutely. >>> >> >>>From Webster's Dictionary: >> >>> 8. (Geom.) One of two opposite directions in which a line, >>> surface, or volume, may be supposed to be described by the motion >>> of a point, line, or surface. >>> [1913 Webster] >>> >>> >>>And from WordNet: >>> >>> 2: the meaning of a word or expression; the way in which a word >>> or expression or situation can be interpreted >>> >>>Both meanings are relevant to the way KJ is using the word. Please take >>>your own advice and stop giving wrong information. As a native English >>>speaker, I had no difficulty understanding the meaning of "sense" in the >>>sense intended by KJ. >> >>As another native English speaker, I agree with Jean-Michel; this is the >>first time I've seen "sense" used to mean direction. > > > > Just goes to show you learn something new all the time. > > http://www.merriam-webster.com/dictionary/sense > > 7: one of two opposite directions especially of motion (as > of a point, line, or surface) > > > http://dictionary.reference.com/browse/sense > > 18. Mathematics. one of two opposite directions in which > a vector may point. > > > > Paraphrasing the Collins Dictionary of Mathematics: > > The sense of a vector is the sign of the measure, contrasted with the > magnitude. Thus the vectors AB and BA have the same direction but > opposite sense. Sense is also used to distinguish clockwise and anti- > clockwise. > > Sense is, if you like, a "signed direction". "Towards north" (say) as > opposed to "along the north-south axis". > This also illustrates the importance of knowing your target audience. I have also not seen "sense" used this way before, and from the placement in the dictionaries I would venture to say it's not common usage outside of mathematics and the sciences. Of course, since kj is teaching biologists, odds are decent they know what he's talking about. ~Ethan~ From usernet at ilthio.net Fri Jul 10 18:13:00 2009 From: usernet at ilthio.net (Tim Harig) Date: Fri, 10 Jul 2009 22:13:00 GMT Subject: main in Python References: Message-ID: On 2009-07-10, Tim wrote: [RE-ORDERED] > It can run as well. Can someone explain why and the rules that Python > scripts get run? Everything gets run by default. The def syntax defines functions but does not run them -- they are only run when they are called > Today, when I read a script, I found it has a different way: > def main(): > ... > > main() This define main and then runs it. > def main(): > ... > if __name__ == "__main__": > main() This defines main but it only runs it if __name__ == "__main" which will only happen if the script is called directly: ./sample.py or python sample.py etc. This form is often preferred because it allows you to import definitions the module without running the code within the main funciton module. That way the same file can be used as a standalone program or as an importable module. From david250 at videotron.ca Fri Jul 10 18:26:36 2009 From: david250 at videotron.ca (David Bernier) Date: Fri, 10 Jul 2009 18:26:36 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: scriber77 at yahoo.com wrote: [...] > community. But perhaps he is trying to see things a bit differently > and is just not getting the feedback he needs, so he is throwing > tantrums apparently across USENET. > > Like I said before, I am just trying to do right by this person who > contacted me, and seemed to be a decent person with a genuine interest > in mathematics. He was very respectful. > > Alas, I am at a loss on what specific feedback to give him on his > paper as though I am a professor of Mathematics, Computer Science is > not my specialty. > > I told him I would try to get him some feedback on the equations on > page 3. Would any of you be so kind to help me? > > Here is the paper: http://MeAmI.org/pversusnp.pdf > > I do thank you for your time. > > Good day, > > Professor X This is what there is at the bottom of page 3: << Conclusion: Binary revisions are allowed given the above formulas. >> So I don't understand the proposed solution for the "P = NP" problem. David Bernier From liukis at usc.edu Fri Jul 10 19:28:06 2009 From: liukis at usc.edu (Maria Liukis) Date: Fri, 10 Jul 2009 16:28:06 -0700 Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception Message-ID: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> Hello, Has anybody seen an exception "OSError: [Errno 39] Directory not empty" raised by shutil.rmtree? The code that raised an exception creates a lot of directories with files, and then removes them. I got an exception when one of such directories was removed. Here is the backtrace: shutil.rmtree(filename) File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree onerror(os.rmdir, path, sys.exc_info()) File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree os.rmdir(path) OSError: [Errno 39] Directory not empty: /path/to/my/dir According to the documentation, shutil.rmtree should not care about directory being not empty. Thanks for the help, Masha -------------------- liukis at usc.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben+python at benfinney.id.au Fri Jul 10 19:35:13 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 11 Jul 2009 09:35:13 +1000 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: <87ljmwm7ri.fsf@benfinney.id.au> walterbyrd writes: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. Yes. That's a ?should? and not a ?must?, even though PEP 8 says it with a simple imperative:: Use 4 spaces per indentation level. it soon afterward takes a more nuanced tone:: The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended! For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do. > Since backward compatibility is being thrown away anyway That implies a level of abandon that was (AIUI) never the intent. Rather, backward compatibility was critically examined for whether it was justified in Python 3. > why not enforce the four space rule? Guido has used his time machine to answer your question a few years ago . (Be sure to read the discussion in the comments on the article.) > At least that way, when I get python code from somebody else, I would > know what I am looking at, without having to do a hex dump, or > something. Any text editor that is good for programming (yes, I'm aware this is perilously close to a ?no true Scotsman? fallacy) will have an option to visibly differentiate whitespace characters, for exactly the reason you point out. -- \ ?Special cocktails for the ladies with nuts.? ?bar, Tokyo | `\ | _o__) | Ben Finney From ben+python at benfinney.id.au Fri Jul 10 19:37:53 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 11 Jul 2009 09:37:53 +1000 Subject: 2.4 VS 3.1 for simple print References: Message-ID: <87hbxkm7n2.fsf@benfinney.id.au> "earthlink" writes: > Why does > > print "GarbageCollector: collected %d objects." % (gc.collect()) > > work in 2.4 but not in 3.1? For this and other differences introduced in the Python 3.x series, see . -- \ ?Please do not feed the animals. If you have any suitable food, | `\ give it to the guard on duty.? ?zoo, Budapest | _o__) | Ben Finney From tjreedy at udel.edu Fri Jul 10 19:48:58 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 19:48:58 -0400 Subject: tough-to-explain Python In-Reply-To: <4a57aa70@news.x-privat.org> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <4a57aa70@news.x-privat.org> Message-ID: I V wrote: > On Fri, 10 Jul 2009 16:27:12 -0400, Terry Reedy wrote: >> a bug, bug a limitation due to using limited-range numbers. If one uses >> residue classes instead of integers, and makes no adjustment, I consider >> it wrong to blame Bentley. > > But it was Bentley himself who used the C int type, so it hardly seems > unreasonable to blame him. The original program that he verified was in pseudocode equivalent to the following (not tested) Python. The odd spec is due to Bentley using 1-based arrays. From the 1986 book Programming Pearls def binsearch(x, t): "If t is in sorted x[1:n], return its index; otherwise 0" # L,U = 1, len(x)-1 while True: if L > U: return 0 m = (L+U)/2 if x[m] < t: L = m+1 elif x[m] == t: return m elif x[m] > t: U = m-1 He then translated into an unspecified dialect of BASIC, but it is consistent with Microsoft GW-BASIC. If so, L and U were float variables, while the largest possible array size for x was 32767. So as near as I can tell with that dialect, there was no possible problem with L+U overflowing or wrapping. Other dialects, I am not sure. So I revise my statement to "I consider it wrong to blame the Bentley that wrote the original program" ;-). If he later translated to C and fell into the residue class trap, then that reinforces my contention that using residue classes as ints is error prone. Terry Jan Reedy From r1chardj0n3s at gmail.com Fri Jul 10 19:52:18 2009 From: r1chardj0n3s at gmail.com (Richard Jones) Date: Sat, 11 Jul 2009 09:52:18 +1000 Subject: Announcing the 9th Pyweek game programming challenge! Message-ID: <7CFED2D8-4C06-4E6F-B25D-C39A510C7686@gmail.com> The date for the ninth PyWeek challenge has been set: Sunday 30th August to Sunday 6th September (00:00UTC to 00:00UTC) The PyWeek challenge invites entrants to write a game in one week from scratch either as an individual or in a team. Entries must be developed in Python, during the challenge, and must incorporate some theme chosen at the start of the challenge. REGISTRATION IS NOT YET OPEN -- Registration will open one month before the start date. See the competition timetable and rules: http://www.pyweek.org/9/ PLANNING FOR THE CHALLENGE -- Make sure you have working versions of the libraries you're going to use. The rules page has a list of libraries and other resources. Make sure you can build packages to submit as your final submission (if you're going to use py2exe, make sure you know how to use it and that it works). If you don't have access to Linux, Windows or a Mac to test on, contact friends, family or other competitors to find someone who is able to test for you. From ben+python at benfinney.id.au Fri Jul 10 19:55:10 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 11 Jul 2009 09:55:10 +1000 Subject: Email provider recommendations for those tired of mangled messages (was: subprocess + python-daemon - bug/problem?) References: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> <828acd8d-a9bd-4506-b695-e537af4587d8@l31g2000yqb.googlegroups.com> <873a94ojvi.fsf@benfinney.id.au> Message-ID: <87d488m6u9.fsf_-_@benfinney.id.au> Ben Finney writes: > Here it is without the unwanted extra line-wrapping that seems to > plague all Google Mail users (seriously, folks: get a real mail > provider that won't mangle your messages) I've been asked off-list whether I have any suggestions for those who want a better email provider. Handling email (transport and mailbox services) is certainly a service that's worth getting someone else to handle, provided you can trust they'll do so reliably and well. For me, ?well? includes ?don't mangle my messages?. My personal solution to this is to manage my own email server machine, but that's an ongoing investment of time that is not something I would recommend to everyone. Nancy McGough maintained (past tense? present tense?) a comprehensive index of email service providers with IMAP access, hence allowing you to use whatever IMAP mail client you like to communicate with your mailboxes remotely. There are feature lists and price indications for each provider. Several of my colleagues swear by Tuffmail as providing excellent uptime, fine control filtering, responsive service, flexible customisation, and good overall value. -- \ ?Visitors are expected to complain at the office between the | `\ hours of 9 and 11 a.m. daily.? ?hotel, Athens | _o__) | Ben Finney From nagle at animats.com Fri Jul 10 20:02:19 2009 From: nagle at animats.com (John Nagle) Date: Fri, 10 Jul 2009 17:02:19 -0700 Subject: tough-to-explain Python In-Reply-To: References: Message-ID: <4a57d630$0$1646$742ec2ed@news.sonic.net> Bearophile wrote: > kj, as Piet van Oostrum as said, that's the difference between mutable > an immutable. It comes from the procedural nature of Python, and > probably an explanation of such topic can't be avoided if you want to > learn/teach Python. The problem with Python's mutable/immutable distinction is that it's not very visible. In a language with no declarations, no named constants, and no parameter attributes like "value", "in", "out", or "const", it's not at all obvious which functions can modify what. Which is usually the place where this causes a problem. The classic, of course, is def additemtolist(item, lst = []) : lst.append(item) return(lst) lista = additemtolist(1) listb = additemtolist(2) print(listb) The general problem is inherent in Python's design. This particular problem, though, results in the occasional suggestion that parameters shouldn't be allowed to have mutable defaults. John Nagle From python.list at tim.thechases.com Fri Jul 10 20:10:03 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jul 2009 19:10:03 -0500 Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception In-Reply-To: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> References: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> Message-ID: <4A57D85B.8040305@tim.thechases.com> > shutil.rmtree(filename) > File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree > onerror(os.rmdir, path, sys.exc_info()) > File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree > os.rmdir(path) > OSError: [Errno 39] Directory not empty: /path/to/my/dir > > According to the documentation, shutil.rmtree should not care about > directory being not empty. This sounds suspiciously like a permission issue. rmtree() *should* walk the tree removing items *if it can*. If a file can't be deleted, it treats it as an error. rmtree() takes parameters for ignore_errors and an onerror callback function, so you can catch these error conditions. -tkc From liukis at usc.edu Fri Jul 10 20:30:56 2009 From: liukis at usc.edu (Maria Liukis) Date: Fri, 10 Jul 2009 17:30:56 -0700 Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception In-Reply-To: <4A57D85B.8040305@tim.thechases.com> References: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> <4A57D85B.8040305@tim.thechases.com> Message-ID: Thanks for pointing out the parameters for ignore_errors and an onerror callback function. I have been running this code for more than an year, and this is the very first time I see the problem. We switched to the NFS mounted storage though, but there is nothing unusual logged by the system in the /var/log/messages. I checked the permissions - the directory is still there and has exactly the same permissions as other successfully copied and removed directories (the very same process creates the directory and later removes it). The directory is empty though - so the process removed all the entries in it, then some kind of an error occured that triggered the exception. There are lots of similar directories that the process creates and removes, but only one of them raised an exception. Thanks, Masha -------------------- liukis at usc.edu On Jul 10, 2009, at 5:10 PM, Tim Chase wrote: >> shutil.rmtree(filename) >> File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree >> onerror(os.rmdir, path, sys.exc_info()) >> File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree >> os.rmdir(path) >> OSError: [Errno 39] Directory not empty: /path/to/my/dir >> According to the documentation, shutil.rmtree should not care >> about directory being not empty. > > This sounds suspiciously like a permission issue. rmtree() > *should* walk the tree removing items *if it can*. If a file can't > be deleted, it treats it as an error. rmtree() takes parameters > for ignore_errors and an onerror callback function, so you can > catch these error conditions. > > -tkc > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Fri Jul 10 20:44:48 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 20:44:48 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: <4A57E080.20008@ieee.org> David Bernier wrote: >> >> >> Good day, >> >> Professor X > > This is what there is at the bottom of page 3: > > << Conclusion: Binary revisions are allowed given the above formulas. >> > > So I don't understand the proposed solution for the "P = NP" > problem. > > David Bernier > > "Professor X" is yet another alias for the hoaxter who started this thread, and presumably who created the website. From benjamin.kaplan at case.edu Fri Jul 10 21:25:19 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Jul 2009 21:25:19 -0400 Subject: main in Python In-Reply-To: <159869.41728.qm@web63104.mail.re1.yahoo.com> References: <159869.41728.qm@web63104.mail.re1.yahoo.com> Message-ID: On Fri, Jul 10, 2009 at 5:52 PM, Tim wrote: > > Hi, > I learned that a Python script is written in this way: > def main(): > ? ?... > if __name__ == "__main__": > ? ?main() > > Today, when I read a script, I found it has a different way: > > def main(): > ? ?... > > main() > > It can run as well. Can someone explain why and the rules that Python scripts get run? > You're thinking like a C or Java programmer. Python scripts are just that - scripts. They aren't compiled with a main() method for an entry point- it's interpreted. When you run a python script, everything that isn't indented is run, from top to bottom, in order. So for instance, when the interpreter sees the line def main() : pass it creates a function called main. If you were to have the statement x= 5 the value 5 would be assigned to x at that time. There are two ways to execute these scripts- by running them directly and by importing them into another module. The second way, with the main method, the script will be run any time it is imported. The first relies on the fact that any module run as a script has a __name__ attribute of "__main__" (usually it's the name of the module). The first program acts more like the C code would- the "main" part of it will only be run if that script is executed. > Thanks and regards, > Tim > > > > -- > http://mail.python.org/mailman/listinfo/python-list > From laurentluce49 at yahoo.com Fri Jul 10 21:52:24 2009 From: laurentluce49 at yahoo.com (Laurent Luce) Date: Fri, 10 Jul 2009 18:52:24 -0700 (PDT) Subject: windows explorer integration Message-ID: <44278.3184.qm@web54207.mail.re2.yahoo.com> Hello, Do you know if it is possible to write a plugin for windows explorer using win32 module ? The idea is to modify the way the folders/files are displayed in the explorer window and also to provide some controls. Laurent From ryankaskel at gmail.com Fri Jul 10 21:57:43 2009 From: ryankaskel at gmail.com (Ryan K) Date: Fri, 10 Jul 2009 18:57:43 -0700 (PDT) Subject: Addind imports to a class namespace Message-ID: Greetings, In order to avoid circular imports, I have a class that I want to improve upon: Class GenerateMenuXhtml(threading.Thread): """ Subclasses a threading.Thread class to generate a menu's XHTML in a separate thread. All Link objects that have this menu associated with it are gathered and combined in an XHTML unordered list. If the sender is of type Link, then all menus associated with that link are iterated through and rebuilt. """ def __init__(self, instance): from asqcom.apps.staticpages.models import Menu, Link self.Link = Link self.Menu = Menu As you can see I just expose these imports by attaching them to members of the class. There must be "prettier" option though where I can just add these imoprts to the class's namespace so all methods of any instance will have access to the imported modules. How would I go about doing this? How can I access the namespace of any class? Through Class.__dict__? Thanks, Ryan From greg at cosc.canterbury.ac.nz Fri Jul 10 22:25:11 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sat, 11 Jul 2009 14:25:11 +1200 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: <7bqbeiF1rue32U1@mid.individual.net> Aahz wrote: > Much simpler and *way* more efficient with a set: > > if len(set(s)) < N: > print "Must have at least %s different characters" % N Or you could do a dictionary lookup on the words. I can just see the error message: "Your comment must include at least one verb, one noun, one non-cliched adjective and one Monty Python reference." -- Greg From aahz at pythoncraft.com Fri Jul 10 23:34:25 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 20:34:25 -0700 Subject: sys.exit() and PyRun_SimpleFileExFlags() References: Message-ID: In article , =?ISO-8859-1?Q?Xavier_B=E9nech?= wrote: > >There is a behaviour I do not understand of PyRun_SimpleFileExFlags(), >normally when it executes a python script containing a sys.exit(), it >results by ending the calling application. If nobody responds on c.l.py, I suggest trying capi-sig. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From marty.musatov at gmail.com Fri Jul 10 23:34:25 2009 From: marty.musatov at gmail.com (Musatov) Date: Fri, 10 Jul 2009 20:34:25 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: Martin Musatov wrote: David Bernier wrote: > scriber77 at yahoo.com wrote: > [...] > > > community. But perhaps he is trying to see things a bit differently > > and is just not getting the feedback he needs, so he is throwing > > tantrums apparently across USENET. > > > > Like I said before, I am just trying to do right by this person who > > contacted me, and seemed to be a decent person with a genuine interest > > in mathematics. He was very respectful. > > > > Alas, I am at a loss on what specific feedback to give him on his > > paper as though I am a professor of Mathematics, Computer Science is > > not my specialty. > > > > I told him I would try to get him some feedback on the equations on > > page 3. Would any of you be so kind to help me? > > > > Here is the paper: http://MeAmI.org/pversusnp.pdf > > > > I do thank you for your time. > > > > Good day, > > > > Professor X > > This is what there is at the bottom of page 3: > > << Conclusion: Binary revisions are allowed given the above formulas. >> > > So I don't understand the proposed solution for the "P = NP" > problem. > > David Bernier P can be equal to N and not P when it is in brackets vs. parantheses. The dots ib the equation in "P = [dots] N (dots) P. The difference between the brackets and the paranthesis represents a margin of time in computation. The dots present in the paper (I am not able to render them here, but they are in the .pdf file) represent a non-deterministic symbolic means of representing language. Thank you for your respone. The conclusion means traditional characters and symbols are not ideal for binary computation. From steve at REMOVE-THIS-cybersource.com.au Sat Jul 11 00:01:03 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Jul 2009 04:01:03 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <0267ffdf$0$18215$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 12:27:25 -0400, Charles Yeomans wrote: >> (3) assert is absolutely unsuitable for enforcing pre-conditions and >> post- >> conditions, unless such conditions are mere "guidelines", because >> assert >> can be switched off at runtime. > > > Unless, of course, you want to switch off such checking at runtime, as > you might when using a design-by-contract approach. So is design-by-contract just another way of saying "let's hope the data is valid, because if it's not, we're screwed"? Perhaps Python should have a new statement, `assume`, used just like `assert` except it never actually performs the test or raises an error. -- Steven From tjreedy at udel.edu Sat Jul 11 01:04:45 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Jul 2009 01:04:45 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <0267ffdf$0$18215$c3e8da3@news.astraweb.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <0267ffdf$0$18215$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 10 Jul 2009 12:27:25 -0400, Charles Yeomans wrote: > >>> (3) assert is absolutely unsuitable for enforcing pre-conditions and >>> post- >>> conditions, unless such conditions are mere "guidelines", because >>> assert >>> can be switched off at runtime. >> >> Unless, of course, you want to switch off such checking at runtime, as >> you might when using a design-by-contract approach. > > So is design-by-contract just another way of saying "let's hope the data > is valid, because if it's not, we're screwed"? > > Perhaps Python should have a new statement, `assume`, used just like > `assert` except it never actually performs the test or raises an error. The manual says quite clearly "The simple form, assert expression, is equivalent to if __debug__: if not expression: raise AssertionError" It should be used when and only when one actually wants the double condition check, and not as an abbreviation of the second conditional only. tjr From haematopus at gmail.com Sat Jul 11 01:52:40 2009 From: haematopus at gmail.com (oystercatcher) Date: Fri, 10 Jul 2009 22:52:40 -0700 (PDT) Subject: fedora 3.1 python install Message-ID: <69d90aab-eee3-4fa3-8960-4548e4e706d7@h2g2000yqg.googlegroups.com> had installed python 3.0. on fc11 x64 the only anomalies were a couple of tests failed during the make test. make altinstall was fine and have been using it for a simple math exercise. attempting to install python 3.1 the make test hangs at test_httpservers and I had to kill the task after waiting quite awhile. now probably this is no big deal and I should just go ahead and run the make altinstall but it would be nice to know a bit more about what is going on here. thanks From nobody at nowhere.com Sat Jul 11 02:24:02 2009 From: nobody at nowhere.com (Nobody) Date: Sat, 11 Jul 2009 07:24:02 +0100 Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On Fri, 10 Jul 2009 09:23:54 +0000, garabik-news-2005-05 wrote: >>> I would like to learn a way of changing the colour of a particular >>> part of the output text. I've tried the following > >> On Unix operating systems this would be done through the curses interface: >> >> http://docs.python.org/library/curses.html > > Or using ANSI colour codes: > > colours = { > 'none' : "", > 'default' : "\033[0m", > 'bold' : "\033[1m", [snip] > # non-standard attributes, supported by some terminals This comment should have appeared immediately after "none" ;) Hard-coding control/escape sequences is just lame. Use the curses modules to obtain the correct sequences for the terminal. From research at johnohagan.com Sat Jul 11 02:30:24 2009 From: research at johnohagan.com (John O'Hagan) Date: Sat, 11 Jul 2009 06:30:24 +0000 Subject: tough-to-explain Python In-Reply-To: <001401ca014c$ca414d60$0d00a8c0@Hendrik> References: <00720d76$0$9710$c3e8da3@news.astraweb.com> <001401ca014c$ca414d60$0d00a8c0@Hendrik> Message-ID: <200907110630.24557.research@johnohagan.com> On Fri, 10 Jul 2009, Hendrik van Rooyen wrote: > "Steven D'Aprano" wrote: > >On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: [...] > >> Programming is not like any other human activity. > > > >In practice? In principle? Programming in principle is not the same as it > >is performed in practice. > > > >But in either case, programming requires both the logical reasoning of > >mathematics and the creativity of the arts. Funnily enough, > > I do not buy this arty creativity stuff. - or are you talking about > making a website look pretty? > > >mathematicians will tell you that mathematics requires the same, and so > >will the best artists. I think mathematicians, engineers, artists, even > >great chefs, will pour scorn on your claim that programming is not like > >any other human activity. > > So a chef is now an authority on programming? > > Programming is actually kind of different - almost everything else is > just done, at the time that you do it. > > Programming is creating stuff that is completely useless until it is > fed into something that uses it, to do something else, in conjuction > with the thing it is fed into, at a later time. > > This is a highly significant difference, IMHO. [...] The drawings produced by an architect, the script of a play, the score of a piece of music, and the draft of a piece of legislation are all examples of other things which are "useless" until they are interpreted in some way. There are countless human activities which require a program, i.e. a conscious plan or strategy, formed at least partly by a creative process, and a computer program is just a special case of this. I use Python as a tool for writing music, but I find I need both logical reasoning and creativity to do either. In fact, I find programming very similar to writing music in a rigorous contrapuntal style, where each set of choices constrains each other, and there is a deep aesthetic satisfaction in getting it right. Regards, John -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sat Jul 11 02:51:48 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Jul 2009 08:51:48 +0200 Subject: Addind imports to a class namespace References: Message-ID: Ryan K wrote: > In order to avoid circular imports, I have a class that I want to > improve upon: > > Class GenerateMenuXhtml(threading.Thread): > """ > Subclasses a threading.Thread class to generate a menu's XHTML in > a separate > thread. All Link objects that have this menu associated with it > are gathered > and combined in an XHTML unordered list. > > If the sender is of type Link, then all menus associated with that > link are > iterated through and rebuilt. > """ > def __init__(self, instance): > from asqcom.apps.staticpages.models import Menu, Link > self.Link = Link > self.Menu = Menu > > As you can see I just expose these imports by attaching them to > members of the class. There must be "prettier" option though where I > can just add these imoprts to the class's namespace so all methods of > any instance will have access to the imported modules. > > How would I go about doing this? How can I access the namespace of any > class? Through Class.__dict__? You can write either class A(object): from some.module import Menu, Link or class A(object): @classmethod do_imports(cls): from some.module import Menu, Link cls.Menu = Menu cls.Link = Link A.do_imports() to put the names into the class. The first form performs the import while the containing module is imported and therefore won't help with breaking circles. In the second form you have to defer the A.do_imports() method call until the import from some.module is safe. But I still recommend that you have another look at your package organization to find a way to avoid circles. Peter From piet at cs.uu.nl Sat Jul 11 03:54:19 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 11 Jul 2009 09:54:19 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <4a57aa70@news.x-privat.org> Message-ID: >>>>> I V (IV) wrote: >IV> On Fri, 10 Jul 2009 16:27:12 -0400, Terry Reedy wrote: >>> a bug, bug a limitation due to using limited-range numbers. If one uses >>> residue classes instead of integers, and makes no adjustment, I consider >>> it wrong to blame Bentley. >IV> But it was Bentley himself who used the C int type, so it hardly seems >IV> unreasonable to blame him. If you are on a 32-bit machine, and the array to be searched contains ints, floats or doubles, the the array must be < 2^32 bytes in size, and as each element is at least 4 bytes, the indices are less than 2^30, so l+u < 2^31. Therefore there is no overflow at all. I think the Bentley programs were formulated in terms of arrays of ints. So his implementations were safe. If you are on a 64-bit machine you shouldn't use int for the indices anyway (supposing int is 32 bits) but longs and then the same reasoning shows that there are no overflows. Only when you have an array of shorts or bytes (chars) you get the problem. In that case the alternative formulation l + (u-l)/2 is more robust and therefore preferable. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From Iris-und-Thomas-Lehmann at T-Online.de Sat Jul 11 04:12:27 2009 From: Iris-und-Thomas-Lehmann at T-Online.de (Thomas Lehmann) Date: Sat, 11 Jul 2009 01:12:27 -0700 (PDT) Subject: Tkinter only: table widget with canvas... References: Message-ID: <5db86de3-d0a0-42c7-8924-7ba8948cfd99@32g2000yqj.googlegroups.com> > > There's a working app at http://cl1p.net/tkinter_table_headers/ > > -John Thank you for this example. However, one issue to that... When resizing the window (vertical) then the header moves away from the table. How can I avoid this with the grid? With "pack" I now this... From p.f.moore at gmail.com Sat Jul 11 05:25:26 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Sat, 11 Jul 2009 10:25:26 +0100 Subject: windows explorer integration In-Reply-To: <44278.3184.qm@web54207.mail.re2.yahoo.com> References: <44278.3184.qm@web54207.mail.re2.yahoo.com> Message-ID: <79990c6b0907110225p14dd718bv26a36e5b13d5e51@mail.gmail.com> 2009/7/11 Laurent Luce : > Do you know if it is possible to write a plugin for windows explorer using win32 module ? Yes, I believe it is. There are a number of Python projects (I believe TortoiseHg is one) that do this sort of thing. However, I don't know anything about how to do it - you should check MSDN, the PyWin32 samples, and maybe something like TortoiseHg, for sample code. Paul. From magobin at gmail.com Sat Jul 11 06:39:25 2009 From: magobin at gmail.com (Alex) Date: Sat, 11 Jul 2009 03:39:25 -0700 (PDT) Subject: problem with keys combination! References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Message-ID: > Blocking Ctrl-Alt-Del leaves the power switch or maybe the plug as the > only way for the user to regain control. Why would you want to do that? ONly for the reason that I explaine above...is for my little joke application ! And I want disable all keys for about 30 seconds (time to erase background) I read about getasynckey() to intercept a key...but I don't know how to change key pressed with another...maybe a solution ? Alex From gslindstrom at gmail.com Sat Jul 11 07:10:17 2009 From: gslindstrom at gmail.com (gslindstrom) Date: Sat, 11 Jul 2009 04:10:17 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: On Jul 10, 2:22?pm, walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. > > Since backward compatibility is being thrown away anyway, why not > enforce the four space rule? > There is a routine in the Scripts directory, reindent.py, that will take your source file(s) and remove tabs, unify the indents to 4- spaces, remove needless characters at the end of lines, etc. IIRC, it was written by Tim Peters. We run all of our source files though before checking them into svn. --greg From mail at microcorp.co.za Sat Jul 11 08:01:25 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 14:01:25 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com><00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <000f01ca021f$5132b5c0$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >On Fri, 10 Jul 2009 12:54:21 +0200, Hendrik van Rooyen wrote: > >> "Steven D'Aprano" wrote: >> >>>On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: >>> >>>>> persistent idea "out there" that programming is a very accessible >>>>> skill, like cooking or gardening, anyone can do it, and even profit >>>>> from it, monetarily or otherwise, etc., and to some extent I am >>>> >>>> Programming is not like any other human activity. >>> >>>In practice? In principle? Programming in principle is not the same as >>>it is performed in practice. >>> >>>But in either case, programming requires both the logical reasoning of >>>mathematics and the creativity of the arts. Funnily enough, >> >> I do not buy this arty creativity stuff. - or are you talking about >> making a website look pretty? > >I must admit, it never crossed my mind that anyone here would claim that >there was no creativity involved in programming, that it was all a >mindless, algorithmic process capable of being done by a simple >mechanical device. "Programming" is the step of going from the "design" to something that tells the machine how to implement the design. The creativity could, arguably, be in the "Design". Not in the translation to python, or assembler. No way. That is just coding. > >This is certainly the accusation made against *bad* programmers -- that >they can't actually solve new, unique problems, but just apply recipes >they learned without any insight or intelligence. The sort of people who >program so poorly that "a trained monkey could do what they do". > >Do you really think that applies to good programmers too? If so, then a >good code generator should be able to replace any programmer. Is that >what you believe? > Should eventually be possible, with sufficient restrictions to start off. UML wants to go this route... But may my eyes be stopped and my bones be heaped with dust ere I see the day... > > >>>mathematicians will tell you that mathematics requires the same, and so >>>will the best artists. I think mathematicians, engineers, artists, even >>>great chefs, will pour scorn on your claim that programming is not like >>>any other human activity. >> >> So a chef is now an authority on programming? > >Did I say that? No. I just read it like that to irritate you. > >Chefs are authorities on OTHER HUMAN ACTIVITIES. > > > >> Programming is actually kind of different - almost everything else is >> just done, at the time that you do it. >> >> Programming is creating stuff that is completely useless until it is fed >> into something that uses it, to do something else, in conjuction with >> the thing it is fed into, at a later time. > >Somebody should teach Hendrik that human beings have been creating TOOLS >for hundreds of thousands of years. People have been creating tools to >build tools for thousands of years. Software is just more of the same. I disagree - I actually own some machine tools, so I am a little bit acquainted with what they can do, and it is not at all like computing at any level I can think of - they are merely extensions of the hand, making the transformation of materials more accurate and faster. The line only becomes blurred when a processor is added, and a STORED PROGRAM is brought into the equation. > >Even *soup stock* fits the same profile as what Hendrik claims is almost >unique to programming. On its own, soup stock is totally useless. But you >make it, now, so you can you feed it into something else later on. > >Or instant coffee. > >No, Henrik, if that's the best you can do, it's not very good. It is >rather sad, but also hilarious, that the most different thing you have >noticed about software is that it's just like instant coffee. > You have a wonderful ability to grab hold of part of a definition and to ignore the rest, just like I can misread what you write. Coffee and soup stay coffee and soup on re hydration. Mixing it in with something else is not at all the same - it does not "DO" anything else in conjunction with the thing it is fed into - how is that like programming, and executing a program? I am sorry if you are confusing the drinking of coffee, which is an ancilliary activity to programming, with the actual programming itself. > > >> This is a highly significant difference, IMHO. > > >>>[...] >>>> He talks about how "when all is said and done, the only thing >>>> computers can do for us is to manipulate symbols and produce results >>>> of such manipulations" and he emphasises the "uninterpreted" nature of >>>> mechanical symbol manipulation, i.e. that the machine is doing it >>>> mindlessly. >>> >>>"Manipulate symbols" is so abstract as to be pointless. By that >>>reasoning, I can build a "computer" consisting of a box open at the top. >>>I represent a symbol by an object (say, a helium-filled balloon, or a >>>stone), instead of a pattern of bits. I manipulate the symbol by holding >>>the object over the box and letting go. If it flies up into the sky, >>>that represents the symbol "Love is War", if it falls into the box, it >>>represents the symbol "Strength is Blue", and if it just floats there, >>>it represents "Cheddar Cheese". This is a deterministic, analog computer >>>which manipulates symbols. Great. >>> >>>And utterly, utterly useless. So what is my computer lacking that real >>>computers have? When you have answered that question, you'll see why >>>Dijkstra's claim is under-specified. >>> >>> >> So if computers do not manipulate symbols, what is it that they do? > >Did I say they don't manipulate symbols? No you wanted someone to tell you how to make your box machine useful, and that was too difficult, so I went this way. > > >> They >> sure cannot think, >> or drink, >> or reason, > >They can't reason? Then what are they doing when they manipulate symbols? They simply manipulate symbols. They really do. There is no reasoning involved. Any reasoning that is done, was done in the mind of the human who designed the system, and the the machine simply manipulates the implementation of the abstract symbols, following the implementation of the abstract rules that were laid down. No reasoning at run time at all. Lots of decision making though - and it is this ability to do this now, and something else later, that tricks the casual observer into thinking that there is somebody at home - a reasoning ghost in the machine. > >Yet again, it didn't even cross my mind that somebody would make this >claim. My entire point is that it's not enough to just "manipulate >symbols", you have to manipulate symbols the correct way, following laws >of logic, so that the computer can *mindlessly* reason. No. There is no requirement for dragging in things like "the laws of logic" or any arbitrarily chosen subset. You can build a machine to do essentially "anything" - and people like Turing and his ilk have spent a lot of skull sweat to try to define what is "general purpose", to enable you to do "anything" > > >> or almost any verb you can think of. > >If you're going to take that argument, then I'll remind you that there >are no symbols inside a computer. There are only bits. And in fact, there >aren't even any bits -- there are only analog voltages, and analog >magnetic fields. > Duh - sorry to hear that - I have only been doing digital electronics now for more years than I care to remember - but maybe there is a difference in how you define "analogue". - maybe something like: "if I can measure it with a multimeter, it is an analogue signal, because a multimeter is capable of measuring analogue signals" - a bit fallacious. More seriously, I again differ on the symbol bit - I am, right now, working on a little processor that mindlessly does I/O, by manipulating bits from one representation to another. In this case, the symbols and the bits are mostly identical - but please do not try to tell me that there are no symbols inside the little processors' memory - I know they are there, because I have arranged for them to be put there, via an RS-232 feed. And my stored program actually arranges for the incoming symbols to do things like activating output relays, and changes on input lines actually cause other symbols to be transmitted out of the RS-232 port. The whole thing is simply rotten with symbology - in fact all it does, is manipulating the implementation of the abstract symbols in my mind. It does so mindlessly. This specific thing is so simple, that it hardly uses the "logical ability" of the processor. And please remember that for a function with two input bits and a single output bit, there can at most be eight possible outcomes, not all of which are useful, for some definition of useful. But they exist, and you can use them, if you want. And you do not even have to be consistent, if you do not want to be, or if it is convenient not to be. I think it is this horrendous freedom that Dijkstra was talking about when he was referring to the "power beyond your wildest dreams", or some such. > >> "Manipulating symbols" is actually an elegant definition. Try coming up >> with a better one and you will see. > >As I said above, it's not enough to just manipulate symbols. Here's a set >of rules to manipulate symbols: > >X => 0 > >or in English, "Any symbol becomes the zero symbol". > >That's symbol manipulation. Utterly useless. This is why it's not enough >to just manipulate symbols, you have to manipulate them THE RIGHT WAY. > No. There is no RIGHT WAY. Really. There are only ways that are useful, or convenient. In my little processor, I can cause any result to occur, and the rules can be as silly as your example - if it is useful for you, under some circumstances, for all symbols to become the symbol for zero, then go for it - The point is that there is nothing prohibiting it to be adopted as a rule for a machine to implement. /dev/null anyone? - Hendrik From mail at microcorp.co.za Sat Jul 11 08:14:55 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 14:14:55 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <001001ca0221$33a04c00$0d00a8c0@Hendrik> "pdpi" wrote; >I've always found cooking an apt metaphor for programming. No this is wrong. Writing a recipe or a cookbook is like programming. Cooking, following a recipe, is like running a program. - Hendrik From jjposner at optimum.net Sat Jul 11 09:31:58 2009 From: jjposner at optimum.net (John Posner) Date: Sat, 11 Jul 2009 09:31:58 -0400 Subject: Tkinter only: table widget with canvas... In-Reply-To: References: Message-ID: <4A58944E.2030009@optimum.net> >> There's a working app at http://cl1p.net/tkinter_table_headers/ > Thank you for this example. However, one issue to that... > When resizing the window (vertical) then the header moves away > from the table. How can I avoid this with the grid? With "pack" > I now this... Oops ... packing can be tricky. Try these modifications: header_frm.pack(side=TOP, anchor=W, expand=False) ... table_frm.pack(side=TOP, expand=True, fill=BOTH) You also might try constraining the top-level (root) window, using these methods: rt.geometry(XXX, YYY) rt.resizable(False, True) -John From ryankaskel at gmail.com Sat Jul 11 09:55:34 2009 From: ryankaskel at gmail.com (Ryan K) Date: Sat, 11 Jul 2009 06:55:34 -0700 (PDT) Subject: Addind imports to a class namespace References: Message-ID: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Thanks for your help Peter. I'm thinking that perhaps this isn't a circular import and that I don't understand importing. Here is a better explanation of my case (I am using Django): I have file x.py that declares classes A, B, C. There is also a file y.py that contains two methods T, U and the class that we are talking about above. x.py uses a dispatcher system to connect a signal to methods T and U in y.py so it does: from y import T, U. y.py needs to use classes A, B, C which is basically Menu and Link (and some other class) above so I am thinking that if in y.py I have from x import A, B, C that will cause a circular import? Is this not correct and if it isn't can you explain why? Does using from ... import X, Y, Z, i.e. explicit imports avoid this problem or does it exacerbate it? Thanks, Ryan From XX.XmcX at XX.XmclaveauX.com Sat Jul 11 10:13:20 2009 From: XX.XmcX at XX.XmclaveauX.com (MC) Date: Sat, 11 Jul 2009 16:13:20 +0200 Subject: windows explorer integration References: Message-ID: Hi! Possible! With Pywin32. I see two ways: - add a "toolBar" ; see the exemple for Internet-Explorer (it run for both, IE & explorer) - add an entry in the context menu (right click) @-salutations -- Michel Claveau -- @-salutations Michel Claveau From __peter__ at web.de Sat Jul 11 10:39:13 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Jul 2009 16:39:13 +0200 Subject: Addind imports to a class namespace References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: Ryan K wrote: > I'm thinking that perhaps this isn't a circular import and that I > don't understand importing. Here is a better explanation of my case (I > am using Django): > > I have file x.py that declares classes A, B, C. Classes in Python are executable code, just like import-statements. That's why there is no Python equivalent to forward declarations in other languages; every name has to be known at the point where it occurs in the module. > There is also a file y.py that contains two methods T, U and the class > that we are talking about above. > > x.py uses a dispatcher system to connect a signal to methods T and U > in y.py so it does: from y import T, U. > > y.py needs to use classes A, B, C which is basically Menu and Link > (and some other class) above so I am thinking that if in y.py I have > from x import A, B, C that will cause a circular import? Yes. Having x import y and y import x creates a cycle. If you cannot avoid this by moving to a simpler design you can always introduce a third module z that imports x and y, and then explicitly resolves the circular references. > Is this not correct and if it isn't can you explain why? Does using > from ... import X, Y, Z, i.e. explicit imports avoid this problem or > does it exacerbate it? It has no effect. from module import X is equivalent to import module X = module.X del module # just the name, not the module itself Peter From mail at microcorp.co.za Sat Jul 11 11:10:04 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 17:10:04 +0200 Subject: tough-to-explain Python References: <00720d76$0$9710$c3e8da3@news.astraweb.com><001401ca014c$ca414d60$0d00a8c0@Hendrik> <200907110630.24557.research@johnohagan.com> Message-ID: <007701ca0240$77f9c2e0$0d00a8c0@Hendrik> John O'Hagan wrote: >The drawings produced by an architect, the script of a play, the score of a piece of music, and the draft of a piece of >legislation are all examples of other things which are "useless" until they are interpreted in some way. Granted. But... >There are countless human activities which require a program, i.e. a conscious plan or strategy, formed at least partly >by a creative process, and a computer program is just a special case of this. The difference is that for a piece of music, or a recipe for "bobotie", there is room for artistry also in the performance or preparation. If the piece of music is reduced to a scroll with holes in it and played on a pianola, then nobody in his right mind would call the mechanically produced sounds "a great performance". And this is the essential difference that sets programming apart from cookbook writing or drawing architectural plans - the one thing that makes the real difference is the mindlessness of the performer. (maybe I should conceed that writing legislation is a form of programming, as the constabulary does not have a reputation for anything other than a plodding consistency) >I use Python as a tool for writing music, but I find I need both logical reasoning and creativity to do either. In fact, I >find programming very similar to writing music in a rigorous contrapuntal style, where each set of choices constrains >each other, and there is a deep aesthetic satisfaction in getting it right. Getting it right has to do with the design, not the programming - Have you ever struggled to get something right, and then one day you take a different tack, and suddenly it is as if you cannot do anything wrong - everything just falls into place? - That is the time that you get the good feeling. The difference between the one and the other is the difference between bad or indifferent design and good design. When you have a good design, the rest follows. If your design is crud, no matter what you do, you struggle and nothing comes out just right, despite heroic effort. Now this stuff is easy to talk about, and immensely difficult to do - it takes experience, some skill, some cunning, and a willingness to experiment in an egoless fashion, amongst other things. And then the stupid compiler completely screws up your intent... :-) - Hendrik From darcy at druid.net Sat Jul 11 11:22:42 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Sat, 11 Jul 2009 11:22:42 -0400 Subject: tough-to-explain Python In-Reply-To: <000f01ca021f$5132b5c0$0d00a8c0@Hendrik> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> <000f01ca021f$5132b5c0$0d00a8c0@Hendrik> Message-ID: <20090711112242.ec01ae8b.darcy@druid.net> On Sat, 11 Jul 2009 14:01:25 +0200 "Hendrik van Rooyen" wrote: > "Programming" is the step of going from the "design" to something > that tells the machine how to implement the design. > > The creativity could, arguably, be in the "Design". > Not in the translation to python, or assembler. > No way. That is just coding. One might also argue that divorcing the design from the code is the problem in a lot of legacy code. See Agile Programming methods. Now you could say that there is a design step still in talking to the client and making a plan in your head or in some notes but that's like saying that Michelangelo was done creating after discussing the Sistine Chapel with Pope Sixtus and that the rest was just a house painting job. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From mail at microcorp.co.za Sat Jul 11 11:58:32 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 17:58:32 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com><00720d76$0$9710$c3e8da3@news.astraweb.com><007318de$0$9710$c3e8da3@news.astraweb.com><000f01ca021f$5132b5c0$0d00a8c0@Hendrik> <20090711112242.ec01ae8b.darcy@druid.net> Message-ID: <007801ca0240$78a01500$0d00a8c0@Hendrik> "D'Arcy J.M. Cain" > One might also argue that divorcing the design from the code is the > problem in a lot of legacy code. See Agile Programming methods. Now > you could say that there is a design step still in talking to the > client and making a plan in your head or in some notes but that's like > saying that Michelangelo was done creating after discussing the Sistine > Chapel with Pope Sixtus and that the rest was just a house painting job. How do you know that it was not exactly like that - he did, after all, take a much longer time than expected to complete the job - just like a house painter that gets paid by the hour. :-) It is also unreasonable to assume the opposite fallacy - that he was in a creative frenzy from the start to the time at the end when he was cleaning his brush after applying the last spot of paint. But it is a valid point - it is often difficult to draw the line between the design and the implementation - and one of the reasons that we all like to program in python, is that it is almost a language that is a design language that can also be run directly. - I have lately been doing stuff like writing rough prototypes using python syntax to serve as designs for some of the assembler thingies I do. If you muck around a bit at the lower level, you will also be more aware of the dichotomy between the design and the implementation - in python, it is not obvious at all. In assembler, it is glaring. But in either place, if you get it wrong, you suffer. - your programs cripple along, or they hardly do what you thought they would, and they bear no relationship with what the customer wanted. - Hendrik From 2.b7e15163 at googlemail.com Sat Jul 11 13:33:10 2009 From: 2.b7e15163 at googlemail.com (Henry 'Pi' James) Date: Sat, 11 Jul 2009 10:33:10 -0700 (PDT) Subject: asyncore's lack of sendto(), recvfrom() Message-ID: <3cbf56cf-1362-44af-9d9f-4f5f2f298c0a@a26g2000yqn.googlegroups.com> Is there any good reason why sendto() and recvfrom() aren't wrapped by asyncore? Obviously, recvfrom() cannot be replaced by recv(), but even sendto() cannot be replace by connect() and send(), either: I'm writing a traceroute module, and I found out that under the current firewall configuration of my OS, sending an ICMP packet via sendto() doesn't trigger a connection confirmation (the packet is silently and successfully sent), while connect() does (the firewall reports an attempted UDP connection, I'm sure why). I know Python merely wraps send() and sendto() from of the OS, obviously there are some important subtle differences between the two. Now, if I'd want to add sendto() and recvfrom() to asyncore, would it be sufficient to simply copying its send() and recv() wrapper functions and change the names and arguments, or are there more modifications required? From mondi at cs.unibo.it Sat Jul 11 13:55:25 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Sat, 11 Jul 2009 17:55:25 +0000 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: Scott David Daniels wrote: > jacopo mondi wrote: >> Roger Binns wrote: >>> jacopo mondi wrote: >>>> Hi all, I need to patch socketmodule.c (the _socket module) in order to >>>> add support to an experimental socket family. >>> You may find it considerably easier to use ctypes since that will avoid >>> the need for any patching. You'll also be able to control how read and >>> write are done (eg read vs recvfrom vs recvmsg vs readv). You can use >>> os.fdopen to convert your raw file descriptor into a Python file object >>> if appropriate. > > The typical Python way of dealing with this is an additional module, not > a modified module placed back in the library. So, take the sources and > edit, but change the module name. Even better is figure out how to > use _socket.pyd, to create a smaller _socketexpmodule.c and use that. > > --Scott David Daniels > Scott.Daniels at Acm.Org Thanks a lot Scott. If I'll write a new module, with a new name, based on socketmodule.c I will not be able to ditribuite the source code and make users compile it using distutils, instead I have to compile it using the whole python build system (changing setup.py in python sources to let it know about my new module) and distribuite the compiled .so with a script to install it, I suppose. Anyway, that's not too bad... Reusing _socket.so, or the main wrapper socket.pyc I think is not possible, because I need to modify low level functions that are not exposed thanks again jacopo From davea at ieee.org Sat Jul 11 13:56:04 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 11 Jul 2009 13:56:04 -0400 Subject: Addind imports to a class namespace In-Reply-To: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: <4A58D234.4050607@ieee.org> Ryan K wrote: > Thanks for your help Peter. > > I'm thinking that perhaps this isn't a circular import and that I > don't understand importing. Here is a better explanation of my case (I > am using Django): > > I have file x.py that declares classes A, B, C. > > There is also a file y.py that contains two methods T, U and the class > that we are talking about above. > > x.py uses a dispatcher system to connect a signal to methods T and U > in y.py so it does: from y import T, U. > > y.py needs to use classes A, B, C which is basically Menu and Link > (and some other class) above so I am thinking that if in y.py I have > from x import A, B, C that will cause a circular import? > > Is this not correct and if it isn't can you explain why? Does using > from ... import X, Y, Z, i.e. explicit imports avoid this problem or > does it exacerbate it? > > Thanks, > Ryan > > You indeed have described a circular import. In this case, a mutual dependency exists between x.py and y.py. This can and nearly always should be avoided. That's independent of the language involved, in any language, circular dependencies are a code smell Now, in Python in particular, first question is whether either x.py or y.py is your main script module (the one where you do the if __name__ == "__main__" and the answer is yes). If so, you're in big trouble, and you really need to rework it. I could elaborate if needed, but trust me, you don't want to do this, even if it means writing one more module containing a single import line and just two or three more, and letting that be your script. If that's not the case, next question is whether either module refers to the other one at import time, or only from inside def and class definitions. If you have any top-level code going on, other than imports, and this code actually uses a symbol from the other module, you're probably in trouble. Not as bad as first case, but still worth avoiding. Sometimes this can be kludged past, by reordering things a little bit, maybe by moving one of the imports further down in the file. Now back to the code smell. If the modules really depend on each other, you should consider factoring out those dependencies and move them to a third module that both import. Figure out why the modules are broken up in the particular way they are, and how you can eliminate circular references by refactoring. It's hard for me to come up with general principles, but perhaps an example or three will help. If a callee needs configuration information stored in globals of the caller, then you probably need to add that config information as parameters. If an object needs a reference to functions in another module, add them when constructing the object, not statically. Or maybe even have the one module explicitly set global variables in the other module, so they're in one place, instead of being only in the namespace of the code initializing them. That's the best I can do without something concrete to adjust. DaveA From ryankaskel at gmail.com Sat Jul 11 13:56:48 2009 From: ryankaskel at gmail.com (Ryan K) Date: Sat, 11 Jul 2009 10:56:48 -0700 (PDT) Subject: Addind imports to a class namespace References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: Okay so below is the acutal code. I am starting to think there is no reason why I can't install the post_save signal in signals.py itself and thereby avoid this issue entirely. models.py: class Link(CommonAbstractModel): ... class Menu(CommonAbstractModel): .... class StaticPage(CommonAbstractModel): ,,, class CachedMenuXhtml(CommonAbstractModel): ... post_save.connect(signals.build_menu, sender=Link) post_save.connect(signals.build_menu, sender=Menu) -------------------- # Signlas for caching of menu XHTML class GenerateMenuXhtml(threading.Thread): def __init__(self, instance): from asqcom.apps.staticpages.models import Menu, Link, CachedMenuXhtml self.Link = Link self.Menu = Menu self.CachedMenuXhtml = CachedMenuXhtml # Function to run on post_save signal def build_menu(sender, instance, **kwargs): GenerateMenuXhtml(instance).start() From digitig at gmail.com Sat Jul 11 14:33:19 2009 From: digitig at gmail.com (Tim Rowe) Date: Sat, 11 Jul 2009 19:33:19 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: <0267ffdf$0$18215$c3e8da3@news.astraweb.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <0267ffdf$0$18215$c3e8da3@news.astraweb.com> Message-ID: 2009/7/11 Steven D'Aprano : > So is design-by-contract just another way of saying "let's hope the data > is valid, because if it's not, we're screwed"? Not at all. Design By Contract is about assigning responsibility for checking. If you don't assign responsibility then a pile of things end up getting checked over and over again, because everybody is assuming responsibility, and some things don't get checked at all because everyone assumes that somebody else is checking. In DbC, the pre-condition on data coming in to a program from outside will usually simply be "true" -- nothing at all is assumed about it. But when you pass it from an input conditioning routine to a processing routine, the input conditioning routine should be able to make guarantees about what it passes, and the processing routine should be able to assume that those guarantees are met without having to check it again (after all, what was the conditioning routine there for?). Assertions might be useful in testing (because they save having to repeat the same set of test cases on absolutely every test run) and they're certainly useful in documenting, but if they're any use at all in the production run-time then there's something wrong with your development and testing processes. -- Tim Rowe From piet at cs.uu.nl Sat Jul 11 17:21:53 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 11 Jul 2009 23:21:53 +0200 Subject: Threading.Condition problem References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Sorry if this appears twice, I sent it once with an attachment and it never >GR> arrived so maybe the attachment is posing problems. I inlined the code this >GR> time (at the bottom), thank you, >GR> Gabriel >GR> ########################## Original message ############################ >GR> Hello everyone, >GR> I wrote a small example that listens for xmpp msgs in a thread. The main >GR> program calls a function that blocks (using Condition.wait) until a msg >GR> has been received and then returns the msg. When a msg arrives, it is >GR> put in a variable in the thread's object, it then calls the notify() >GR> attr on the Condition object. For some reason, this doesn't work, the >GR> thread gets the msg, tries to notify the Condition object, fails because >GR> the lock has not been acquired yet and blocks. I tried ignoring the >GR> failure, thinking that since it has not been acquired yet then when it >GR> is, it will get the msg right away and never call Condition.wait, thus >GR> not causing any problems, but this does not work either. Does someone >GR> know what I am doing wrong? I attached the code to this msg. The code that puts the message in the variable should also acquire the lock: def onMessage(self, conn, msg): with self._cv: self.message = msg self._cv.notify() A couple of remarks: 1. I think the code is neater if all manipulation with the condition is done in the same class (actually in the same instance -- making this instance into a monitor). class Listener(Thread): def __init__(self, ws): Thread.__init__(self) self.interrupt = Event() self.message = None self._cv = Condition() self.client = ws._client self.client.RegisterHandler('message', self.onMessage) def onMessage(self, conn, msg): with self._cv: self.message = msg try: self._cv.notify() except RuntimeError: print "self._cv has not acquired the lock yet" def getMsg(self): with self._cv: while !self.message self._cv.wait() return self.message class WS(object): def __init__(self, username, password, res): self._jid = xmpp.protocol.JID(username) self._client = xmpp.Client(self._jid.getDomain()) # self._cv = Condition() def getMsg(self, mid=None): """ """ return self._listener.getMsg() Of course I haven't tested this code as I don't have the context modules. 2. I don't know if more than one message can be delivered in the same instance. If yes, than your code will not work, and neither will the code above as, the message instance variable is never cleared. So the next getMsg will be happy to deliver the previous one. You would have to clear it when returning this one. def getMsg(self): with self._cv: while !self.message self._cv.wait() msg = self.message self.message = None return msg 3. If the messages come in faster than they can be processed some will be lost as they will overwrite the previous one in the self.message variable. The solution is to use a threading.Queue to transfer the messages from one thread to the other. This also saves you the hassle of doing your own synchronisation like above. If you are not familiar with synchronising multithreaded applications it is very easy to make errors and even if you are it is quite easy to do them wrong. I have been involved in distributed programming courses at university level and I have seen many errors in this area. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mail.to.daniel.platz at googlemail.com Sat Jul 11 18:33:32 2009 From: mail.to.daniel.platz at googlemail.com (Daniel Platz) Date: Sat, 11 Jul 2009 15:33:32 -0700 (PDT) Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: Hi, thanks for your repleys. I have tried matplotlib but it is extremely slow. I think it is more optimized for good looking plots instead of speed. I do not know the Python bindings of gnuplot and Veusz. To clarify the issue again, by 25000 data points I mean 25000 pixels, i.e. corresponding (x,y) pairs. Thus the mapping to one pixel on the screen is not unique. Now, that I have to implement this problem on my own I am very impressed by the plotting capabilities of LabView which brings thounds of data points on screen in an extremely short time. Thanks for your help. Best regards, Daniel From ben+python at benfinney.id.au Sat Jul 11 19:27:23 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 12 Jul 2009 09:27:23 +1000 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <87vdlyls10.fsf@benfinney.id.au> Daniel Platz writes: > I do not know the Python bindings of gnuplot and Veusz. A web search does, though. -- \ ?The problem with television is that the people must sit and | `\ keep their eyes glued on a screen: the average American family | _o__) hasn't time for it.? ?_The New York Times_, 1939 | Ben Finney From ryankaskel at gmail.com Sat Jul 11 19:43:10 2009 From: ryankaskel at gmail.com (Ryan K) Date: Sat, 11 Jul 2009 16:43:10 -0700 (PDT) Subject: Addind imports to a class namespace References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: <3a6a9820-ab17-4ddc-a75b-a6b49b919af3@p15g2000vbl.googlegroups.com> Thank you for your replies. I have factored out the dependency and everything is solved. Cheers, Ryan From ldo at geek-central.gen.new_zealand Sat Jul 11 20:21:12 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 12 Jul 2009 12:21:12 +1200 Subject: 2.4 VS 3.1 for simple print References: <87hbxkm7n2.fsf@benfinney.id.au> Message-ID: In message <87hbxkm7n2.fsf at benfinney.id.au>, Ben Finney wrote: > For this and other differences introduced in the Python 3.x series, see > . People never thank you for an "RTFM" response. From ldo at geek-central.gen.new_zealand Sat Jul 11 20:25:02 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 12 Jul 2009 12:25:02 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , Aahz wrote: > It helps to remember that names and namespaces are in many > ways syntactic sugar for dicts or lists. Interesting, though, that Python insists on maintaining a distinction between c["x"] and c.x, whereas JavaScript doesn't bother. From greg at cosc.canterbury.ac.nz Sat Jul 11 21:28:17 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sun, 12 Jul 2009 13:28:17 +1200 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com><00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <7bssfuF24o01hU1@mid.individual.net> Hendrik van Rooyen wrote: > The creativity could, arguably, be in the "Design". > Not in the translation to python, or assembler. > No way. That is just coding. No, the mechanical part of the process is called compiling, and we have programs to do it for us. By the time you've specified the design so rigorously that not the slightest spark of creativity is needed to implement it, you *have* coded it. -- Greg From greg at cosc.canterbury.ac.nz Sat Jul 11 21:34:27 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sun, 12 Jul 2009 13:34:27 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) In-Reply-To: References: Message-ID: <7bssrgF251u00U1@mid.individual.net> Lawrence D'Oliveiro wrote: > Interesting, though, that Python insists on maintaining a distinction > between c["x"] and c.x, whereas JavaScript doesn't bother. And that distinction is a good thing. It means, for example, that dictionaries can have methods without colliding with the key space of the items put into them. -- Greg From clp2 at rebertia.com Sat Jul 11 21:50:28 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 11 Jul 2009 18:50:28 -0700 Subject: explode() In-Reply-To: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: <50697b2c0907111850sda1fd84k65f017f2553b639a@mail.gmail.com> On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: > ? ? ? ?What is the Python equivalent of the PHP explode() function? some_string.split("separator") Cheers, Chris -- http://blog.rebertia.com From fatkinson at mishmash.com Sat Jul 11 22:41:12 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Sat, 11 Jul 2009 19:41:12 -0700 Subject: explode() Message-ID: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> What is the Python equivalent of the PHP explode() function? Fred From sjmachin at lexicon.net Sat Jul 11 23:01:35 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 12 Jul 2009 03:01:35 +0000 (UTC) Subject: A zlib question References: <80F42625-5FB2-438F-AFF2-913236A6D336@catalogix.se> Message-ID: Roland Hedberg catalogix.se> writes: > I have a problem with zlib and compressing/decompressing according to > RFC 1951. > > It seems like I can decompress, something compressed according to RFC > 1951 by someone else, provided I set wbits to something negative (used > -8 but I guess any negative number would work?). -15 will get you a 32KB window which is the best and is the default in APIs where a default is possible. original_data = zlib.decompress(deflated_data, -15) > But how can I compress using zlib so it doesn't add a gzip header ? You don't need to, because zlib doesn't add a gzip header (RFC 1952) -- it adds a zlib header (RFC 1950) ... without out any frills (i.e. default case) a zlib stream is a 2-byte header plus the RFC 1951 deflate stream plus a 4-byte checksum. deflated_data = zlib.compress(uncompressed_data)[2:-4] Coincidentally this question arose elsewhere very recently: see http://stackoverflow.com/questions/1089662/ python-inflate-and-deflate-implementations concatenate the above two lines -- gmane <= 80-byte line limit 8-P BTW FWIW, one can evidently force zlib to do a gzip wrapper instead of a zlib wrapper by setting wbits to positive > 15 [brilliant API design], but one might be better off using the gzip module directly ... HTH, John From fatkinson at mishmash.com Sat Jul 11 23:20:41 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Sat, 11 Jul 2009 20:20:41 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert wrote: >On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >> ? ? ? ?What is the Python equivalent of the PHP explode() function? > >some_string.split("separator") > >Cheers, >Chris Thanks, Fred From aahz at pythoncraft.com Sun Jul 12 00:23:43 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 Jul 2009 21:23:43 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In article , Lawrence D'Oliveiro wrote: >In message , Aahz wrote: >> >> It helps to remember that names and namespaces are in many >> ways syntactic sugar for dicts or lists. > >Interesting, though, that Python insists on maintaining a distinction >between c["x"] and c.x, whereas JavaScript doesn't bother. Why do you say "insists"? class AttrDict: def __getitem__(self, key): return getattr(self, key) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From blepctc at free.fr Sun Jul 12 03:47:32 2009 From: blepctc at free.fr (Baptiste Lepilleur) Date: Sun, 12 Jul 2009 09:47:32 +0200 Subject: Looking for a tool to checkfor python script backward compatibility Message-ID: <4a59951a$0$10853$426a74cc@news.free.fr> I'm looking for a tool that could be used in a pre-commit step to check that only features available in a "old" python version are used, say python 2.3 for example. Does any one know of one ? From vs at it.uu.se Sun Jul 12 05:21:51 2009 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 12 Jul 2009 11:21:51 +0200 Subject: PyODE Message-ID: <4A59AB2F.4060708@it.uu.se> Does anyone have PyODE running on Python 2.6.2? --V From ben+python at benfinney.id.au Sun Jul 12 05:34:56 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 12 Jul 2009 19:34:56 +1000 Subject: 2.4 VS 3.1 for simple print References: <87hbxkm7n2.fsf@benfinney.id.au> Message-ID: <87r5wmkzwf.fsf@benfinney.id.au> Lawrence D'Oliveiro writes: > In message <87hbxkm7n2.fsf at benfinney.id.au>, Ben Finney wrote: > > > For this and other differences introduced in the Python 3.x series, see > > . > > People never thank you for an "RTFM" response. It's a good thing I avoid giving such responses, then. (Pointing someone to the specific document isn't ?RTFM?.) -- \ ?Leave nothing to chance. Overlook nothing. Combine | `\ contradictory observations. Allow yourself enough time.? | _o__) ?Hippocrates | Ben Finney From skip at pobox.com Sun Jul 12 09:01:23 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 12 Jul 2009 08:01:23 -0500 Subject: Implementing a cache In-Reply-To: <87ab3cwu3u.fsf@vostro.rath.org> References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: <19033.56995.644542.486528@montanaro.dyndns.org> Nikolaus> I want to implement a caching data structure in Python that Nikolaus> allows me to: Nikolaus> 1. Quickly look up objects using a key Nikolaus> 2. Keep track of the order in which the objects are accessed Nikolaus> (most recently and least recently accessed one, not a Nikolaus> complete history) Nikolaus> 3. Quickly retrieve and remove the least recently accessed Nikolaus> object. My Cache module does #1 and #3. I'm not sure if you want #2 for internal cache maintenance or for as part of the API. http://www.smontanaro.net/python/Cache.py Skip From pdpinheiro at gmail.com Sun Jul 12 09:27:23 2009 From: pdpinheiro at gmail.com (pdpi) Date: Sun, 12 Jul 2009 06:27:23 -0700 (PDT) Subject: Implementing a cache References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: On Jul 12, 2:01?pm, s... at pobox.com wrote: > ? ? Nikolaus> I want to implement a caching data structure in Python that > ? ? Nikolaus> allows me to: > > ? ? Nikolaus> ?1. Quickly look up objects using a key > ? ? Nikolaus> ?2. Keep track of the order in which the objects are accessed > ? ? Nikolaus> ? ? (most recently and least recently accessed one, not a > ? ? Nikolaus> ? ? complete history) > ? ? Nikolaus> ?3. Quickly retrieve and remove the least recently accessed > ? ? Nikolaus> ? ? object. > > My Cache module does #1 and #3. ?I'm not sure if you want #2 for internal > cache maintenance or for as part of the API. > > ? ?http://www.smontanaro.net/python/Cache.py > > Skip I'm not sure whether #2 is doable at all, as written. You _need_ a complete history (at least the full ordering of the items in the cache) to be able to tell what the least recently used item is. From exarkun at divmod.com Sun Jul 12 09:39:43 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Sun, 12 Jul 2009 09:39:43 -0400 Subject: Looking for a tool to checkfor python script backward compatibility In-Reply-To: <4a59951a$0$10853$426a74cc@news.free.fr> Message-ID: <20090712133943.2543.62461196.divmod.quotient.1539@henry.divmod.com> On Sun, 12 Jul 2009 09:47:32 +0200, Baptiste Lepilleur wrote: >I'm looking for a tool that could be used in a pre-commit step to check that >only features available in a "old" python version are used, say python 2.3 >for example. > >Does any one know of one ? > Run your test suite with whichever versions of Python you want to support. Tools like Hudson () and BuildBot () can help with this. Jean-Paul From stef.mientki at gmail.com Sun Jul 12 09:40:27 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 12 Jul 2009 15:40:27 +0200 Subject: how to run the "main" section of another module ? Message-ID: <4A59E7CB.5020908@gmail.com> hello, when I''m working in a library, and want to test some of the library functions, I need to switch to a main application, (which has a normal main-section) and run that. If the library is simply, I add a main section to the library, so no problem. But if the library requires a lot of overhead, I want to run some main program, that has a main section, and uses my library under construction. So how do I call from within the library module, in a main section in that library module, a main in another module ? thanks, Stef Mientki From skip at pobox.com Sun Jul 12 09:40:45 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 12 Jul 2009 08:40:45 -0500 Subject: Implementing a cache In-Reply-To: References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: <19033.59357.223527.248051@montanaro.dyndns.org> >> My Cache module does #1 and #3. ?I'm not sure if you want #2 for >> internal cache maintenance or for as part of the API. >> >> ? ?http://www.smontanaro.net/python/Cache.py pdpi> I'm not sure whether #2 is doable at all, as written. You _need_ a pdpi> complete history (at least the full ordering of the items in the pdpi> cache) to be able to tell what the least recently used item is. I should have added that my Cache module does maintain a history. (Clearly it needs that to determine the LRU item.) It is exposed as part of the API via the ftimes method. S From stef.mientki at gmail.com Sun Jul 12 09:47:27 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 12 Jul 2009 15:47:27 +0200 Subject: how to run the "main" section of another module ? In-Reply-To: <4A59E7CB.5020908@gmail.com> References: <4A59E7CB.5020908@gmail.com> Message-ID: <4A59E96F.6080709@gmail.com> Stef Mientki wrote: > hello, > > when I''m working in a library, > and want to test some of the library functions, > I need to switch to a main application, > (which has a normal main-section) > and run that. > > If the library is simply, > I add a main section to the library, > so no problem. > > But if the library requires a lot of overhead, > I want to run some main program, > that has a main section, > and uses my library under construction. > > So how do I call from within the library module, > in a main section in that library module, > a main in another module ? > > thanks, > Stef Mientki btw, I just found this in one of my programs (which works but isn't very beautiful) In the library fill under construction I put this main section: (sb_test.py is the main program, that has a main section and uses the library under construction) if __name__ == '__main__': import db_test new_globals = {} new_globals [ '__name__' ] = '__main__' new_globals [ '__file__' ] = 'not really valuable' execfile ( 'db_test.py', new_globals ) From aahz at pythoncraft.com Sun Jul 12 10:21:55 2009 From: aahz at pythoncraft.com (Aahz) Date: 12 Jul 2009 07:21:55 -0700 Subject: how to run the "main" section of another module ? References: Message-ID: In article , Stef Mientki wrote: > >when I''m working in a library, and want to test some of the library >functions, I need to switch to a main application, (which has a normal >main-section) and run that. if __name__ == '__main__': main() Then you can call main() from another module for testing purposes. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From piet at cs.uu.nl Sun Jul 12 11:54:30 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 12 Jul 2009 17:54:30 +0200 Subject: how to run the "main" section of another module ? References: <4A59E7CB.5020908@gmail.com> Message-ID: >>>>> Stef Mientki (SM) wrote: >SM> Stef Mientki wrote: >>> hello, >>> >>> when I''m working in a library, >>> and want to test some of the library functions, >>> I need to switch to a main application, >>> (which has a normal main-section) >>> and run that. >>> >>> If the library is simply, >>> I add a main section to the library, >>> so no problem. >>> >>> But if the library requires a lot of overhead, >>> I want to run some main program, >>> that has a main section, >>> and uses my library under construction. >>> >>> So how do I call from within the library module, >>> in a main section in that library module, >>> a main in another module ? >>> >>> thanks, >>> Stef Mientki >SM> btw, >SM> I just found this in one of my programs (which works but isn't very >SM> beautiful) >SM> In the library fill under construction I put this main section: >SM> (sb_test.py is the main program, that has a main section and uses the >SM> library under construction) >SM> if __name__ == '__main__': >SM> import db_test >SM> new_globals = {} >SM> new_globals [ '__name__' ] = '__main__' >SM> new_globals [ '__file__' ] = 'not really valuable' >SM> execfile ( 'db_test.py', new_globals ) Why not: import db_test db_test.main() I think that is what Aahz meant. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From bjorn.m.meyer at gmail.com Sun Jul 12 13:16:13 2009 From: bjorn.m.meyer at gmail.com (Bjorn Meyer) Date: Sun, 12 Jul 2009 11:16:13 -0600 Subject: multiprocessing and dictionaries Message-ID: <200907121116.13828.bjorn.m.meyer@gmail.com> I am trying to convert a piece of code that I am using the thread module with to the multiprocessing module. The way that I have it set up is a chunk of code reads a text file and assigns a dictionary key multiple values from the text file. I am using locks to write the values to the dictionary. The way that the values are written is as follows: mydict.setdefault(key, []).append(value) The problem that I have run into is that using multiprocessing, the key gets set, but the values don't get appended. I've even tried the Manager().dict() option, but it doesn't seem to work. Is this not supported at this time or am I missing something? Thanks in advance. Bjorn From stef.mientki at gmail.com Sun Jul 12 14:13:53 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 12 Jul 2009 20:13:53 +0200 Subject: how to run the "main" section of another module ? In-Reply-To: References: <4A59E7CB.5020908@gmail.com> Message-ID: <4A5A27E1.1090502@gmail.com> >> SM> if __name__ == '__main__': >> SM> import db_test >> SM> new_globals = {} >> SM> new_globals [ '__name__' ] = '__main__' >> SM> new_globals [ '__file__' ] = 'not really valuable' >> SM> execfile ( 'db_test.py', new_globals ) >> > > Why not: > > import db_test > db_test.main() > > I think that is what Aahz meant. > > Yes I tried that too, but it gives the following error: "module object not callable" db_text.__main__() gives error: 'module' object has no attribute '__main__' cheers, Stef From aahz at pythoncraft.com Sun Jul 12 14:44:10 2009 From: aahz at pythoncraft.com (Aahz) Date: 12 Jul 2009 11:44:10 -0700 Subject: how to run the "main" section of another module ? References: <4A59E7CB.5020908@gmail.com> Message-ID: In article , Stef Mientki wrote: >Stef deleted an attribution: >> >> Why not: >> >> import db_test >> db_test.main() > >Yes I tried that too, but it gives the following error: "module object >not callable" You need to create a main() function in db_test first. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From emile at fenx.com Sun Jul 12 14:48:25 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 12 Jul 2009 11:48:25 -0700 Subject: how to run the "main" section of another module ? In-Reply-To: <4A5A27E1.1090502@gmail.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: On 7/12/2009 11:13 AM Stef Mientki said... > >>> SM> if __name__ == '__main__': >>> SM> import db_test >>> SM> new_globals = {} >>> SM> new_globals [ '__name__' ] = '__main__' >>> SM> new_globals [ '__file__' ] = 'not really valuable' >>> SM> execfile ( 'db_test.py', new_globals ) >>> >> >> Why not: implied here is that you restructure... >> >> import db_test >> db_test.main() >> >> I think that is what Aahz meant. >> >> > Yes I tried that too, > but it gives the following error: > "module object not callable" > > > db_text.__main__() > gives error: > 'module' object has no attribute '__main__' > You see, we're further assuming you've structured the module for this purpose. IOW, your module should end with: if __name__ == '__main__': main() and nothing more. Then, you can do: import db_test db_test.main() Emile From cameron.pulsford at gmail.com Sun Jul 12 15:11:55 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Sun, 12 Jul 2009 15:11:55 -0400 Subject: Question about generators In-Reply-To: References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Hey everyone, I have this small piece of code that simply finds the factors of a number. import sys def factor(n): primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! = 0) factors = [] for i in [2, 3, 5]: while n % i == 0: n /= i factors.append(i) for i in primes: while n % i == 0: n /= i factors.append(i) print factors factor(int(sys.argv[1])) My question is, is it possible to combine those two loops? The primes generator I wrote finds all primes up to n, except for 2, 3 and 5, so I must check those explicitly. Is there anyway to concatenate the hard coded list of [2,3,5] and the generator I wrote so that I don't need two for loops that do the same thing? I tried writing a primes function using yield statements, but it didn't work like I thought it would. From alan.kesselmann at gmail.com Sun Jul 12 15:16:29 2009 From: alan.kesselmann at gmail.com (zayatzz) Date: Sun, 12 Jul 2009 12:16:29 -0700 (PDT) Subject: Sockets and threading Message-ID: <0b77e7b9-85cd-4a23-b975-5cb8ac59fe7e@j19g2000vbp.googlegroups.com> Im trying to get aquinted to python on bit more basic level and am following socket and threading programming tutorials from these 2 addresses : http://heather.cs.ucdavis.edu/~matloff/Python/PyNet.pdf http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf in this PyThreads file he sets up threaded server app which listens to what clients send to it and echos it back to clients while adding sent info into one string. The problem is that the code does not work for me. class srvr(threading.Thread): v = "" vlock = threading.Lock() id = 0 def __init__(self,clntsock): threading.Thread.__init__(self) self.myid = srvr.id srvr.id += 1 self.myclntsock = clntsock def run(self): while 1: k = self.myclntsock.recv(1) if k == "": break srvr.vlock.acquire() srvr.v += k srvr.vlock.release() self.myclntsock.send(srvr.v) self.myclntsock.close() Instead of sendint back i get this error : File "server3.py", line 31, in serveclient k = self.myclntsock.recv(1) File "/usr/lib/python2.6/socket.py", line 165, in _dummy raise error(EBADF, 'Bad file descriptor') socket.error: [Errno 9] Bad file descriptor As much as i understand this line 31 is supposed to recieve stuff from clients and has buffer size 1. I dont understand what this has to do with file descriptor.. whatever that is anyway. Alan From vodkalove at gmail.com Sun Jul 12 15:18:04 2009 From: vodkalove at gmail.com (Riley Crane) Date: Sun, 12 Jul 2009 12:18:04 -0700 (PDT) Subject: MySQLdb + SSH Tunnel Message-ID: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> OVERVIEW: I am running a script on one machine that connects to a MySQL database on another machine that is outside of our university's domain. According to the administrator, network policies do not allow the compute nodes to access machines outside of our university's domain. COMPUTERS: A = compute node within university (I do not have shell access) B = 2nd machine within university that does not block outside connections (I have root access) C = machine outside of university (I have root access) mysqldb on A cannot connect to C ....but..... mysqldb on A can connect to B WHAT I WOULD LIKE TO DO: Is it possible to set something up where A talks to a port on B, and that port is actually nothing more than 3306 on C? Can I do this with an SSH tunnel? Can anyone please give precise instructions? From emile at fenx.com Sun Jul 12 15:41:31 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 12 Jul 2009 12:41:31 -0700 Subject: MySQLdb + SSH Tunnel In-Reply-To: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: On 7/12/2009 12:18 PM Riley Crane said... > OVERVIEW: > I am running a script on one machine that connects to a MySQL database > on another machine that is outside of our university's domain. > According to the administrator, network policies do not allow the > compute nodes to access machines outside of our university's domain. > > COMPUTERS: > A = compute node within university (I do not have shell access) > B = 2nd machine within university that does not block outside > connections (I have root access) ...so B can talk to C presumably... > C = machine outside of university (I have root access) > mysqldb on A cannot connect to C ....but..... > mysqldb on A can connect to B > > WHAT I WOULD LIKE TO DO: > Is it possible to set something up where A talks to a port on B, and > that port is actually nothing more than 3306 on C? Can I do this with > an SSH tunnel? > > Can anyone please give precise instructions? ssh with something like... ssh -lroot -L3306:C:3306 B Watch out for other instances of mysql on A or B... Emile From mensanator at aol.com Sun Jul 12 15:47:46 2009 From: mensanator at aol.com (Mensanator) Date: Sun, 12 Jul 2009 12:47:46 -0700 (PDT) Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: <173f9127-2846-46a4-8392-6a7f03fde6e1@j19g2000vbp.googlegroups.com> On Jul 12, 2:11?pm, Cameron Pulsford wrote: > Hey everyone, I have this small piece of code that simply finds the ? > factors of a number. > > import sys > > def factor(n): > ? ? ?primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! > = 0) > > ? ? ?factors = [] > > ? ? ?for i in [2, 3, 5]: > ? ? ? ? ?while n % i == 0: > ? ? ? ? ? ? ?n /= i > ? ? ? ? ? ? ?factors.append(i) > ? ? ?for i in primes: > ? ? ? ? ?while n % i == 0: > ? ? ? ? ? ? ?n /= i > ? ? ? ? ? ? ?factors.append(i) > ? ? ?print factors > > factor(int(sys.argv[1])) > > My question is, is it possible to combine those two loops? Yeah, get rid of the first loop. > The primes ? > generator I wrote finds all primes up to n, except for 2, 3 and 5, so ? > I must check those explicitly. Is there anyway to concatenate the hard ? > coded list of [2,3,5] and the generator I wrote so that I don't need ? > two for loops that do the same thing? primes.extend([2,3,5]) > > I tried writing a primes function using yield statements, but it ? > didn't work like I thought it would. From vilya.harvey at gmail.com Sun Jul 12 15:55:49 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sun, 12 Jul 2009 20:55:49 +0100 Subject: Question about generators In-Reply-To: <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: <6aef848f0907121255n7d5cff37r6b01828cfb0ba6db@mail.gmail.com> 2009/7/12 Cameron Pulsford : > My question is, is it possible to combine those two loops? The primes > generator I wrote finds all primes up to n, except for 2, 3 and 5, so I must > check those explicitly. Is there anyway to concatenate the hard coded list > of [2,3,5] and the generator I wrote so that I don't need two for loops that > do the same thing? itertools.chain([2, 3, 5], primes) is what you're looking for, I think. Vil. From rhodri at wildebst.demon.co.uk Sun Jul 12 16:13:58 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 12 Jul 2009 21:13:58 +0100 Subject: need to write a assembly progrm In-Reply-To: References: Message-ID: On Sat, 11 Jul 2009 05:17:18 +0100, Dennis Lee Bieber wrote: > On Fri, 10 Jul 2009 13:25:49 +0100, "Rhodri James" > declaimed the following in > gmane.comp.python.general: > >> On Thu, 09 Jul 2009 11:52:44 +0100, m.reddy prasad reddy >> wrote: >> >> > my aim is to run the assembly programs in python.by that we can use >> that >> > in >> > the any labs.because we can run the python in the mobiles also.if we >> > write >> > assembly programs in the mobile ,the mobile act as a tool kit for the >> > lab.tell me any other solutions for this >> >> Please define what you mean by "assembly". >> > The poorly punctuated paragraph sounds, to me, as if they mean they > want (Windows assumed) EXE files that can be dropped onto remote/laptop > machines without performing a full installation of Python... > > But yes... "assembly programs" does, to me, also mean things > containing mnemonics for machine level opcodes. Given that to me, "assembly programs" does not mean .EXE files at all, not even a little bit, I'm prefering to refuse to guess :-) -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Sun Jul 12 17:02:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 12 Jul 2009 23:02:46 +0200 Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: >>>>> Cameron Pulsford (CP) wrote: >CP> Hey everyone, I have this small piece of code that simply finds the >CP> factors of a number. Others have already given you advice to add the [2, 3, 5] to the iterator (of which the primes.extend([2,3,5]) will not work). Please allow me to make some other remarks. >CP> import sys >CP> def factor(n): >CP> primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! = 0) It is a bit misleading to call this `primes' as it will also contain non-primes. Of course they don't harm as they will never be considered a factor because all their prime factors will have been successfully removed before this non prime will be considered. >CP> factors = [] >CP> for i in [2, 3, 5]: >CP> while n % i == 0: >CP> n /= i >CP> factors.append(i) >CP> for i in primes: >CP> while n % i == 0: >CP> n /= i >CP> factors.append(i) You can stop the loop when n == 1. Like: if n == 1: break >CP> print factors >CP> factor(int(sys.argv[1])) >CP> My question is, is it possible to combine those two loops? The primes >CP> generator I wrote finds all primes up to n, except for 2, 3 and 5, so I >CP> must check those explicitly. Is there anyway to concatenate the hard coded >CP> list of [2,3,5] and the generator I wrote so that I don't need two for >CP> loops that do the same thing? >CP> I tried writing a primes function using yield statements, but it didn't >CP> work like I thought it would. See the recent thread `Why is my code faster with append() in a loop than with a large list?' http://mail.python.org/pipermail/python-list/2009-July/718931.html -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From gagsl-py2 at yahoo.com.ar Sun Jul 12 17:34:41 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 12 Jul 2009 18:34:41 -0300 Subject: Sockets and threading References: <0b77e7b9-85cd-4a23-b975-5cb8ac59fe7e@j19g2000vbp.googlegroups.com> Message-ID: En Sun, 12 Jul 2009 16:16:29 -0300, zayatzz escribi?: > while 1: > k = self.myclntsock.recv(1) > if k == "": break > srvr.vlock.acquire() > srvr.v += k > srvr.vlock.release() > self.myclntsock.send(srvr.v) > self.myclntsock.close() > > Instead of sendint back i get this error : > File "server3.py", line 31, in serveclient > k = self.myclntsock.recv(1) > File "/usr/lib/python2.6/socket.py", line 165, in _dummy > raise error(EBADF, 'Bad file descriptor') > socket.error: [Errno 9] Bad file descriptor > > As much as i understand this line 31 is supposed to recieve stuff from > clients and has buffer size 1. I dont understand what this has to do > with file descriptor.. whatever that is anyway. That means the clntsock variable isn't an open, available socket. Note that you close the socket right after sending the response - are you sure you don't have an indentation error there? -- Gabriel Genellina From Samnsparky at gmail.com Sun Jul 12 17:50:01 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 14:50:01 -0700 (PDT) Subject: Webcam + GStreamer Message-ID: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> Hello! I need to stream from a webcam in Linux and I need to be able to analyze the video feed frame by frame with PIL. Currently my web- cam (Quickcam Chat) only seems to work with GStreamer so a solution using pygst would be preferred. Thanks for your help, Sam From piet at cs.uu.nl Sun Jul 12 17:52:50 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 12 Jul 2009 23:52:50 +0200 Subject: Sockets and threading References: <0b77e7b9-85cd-4a23-b975-5cb8ac59fe7e@j19g2000vbp.googlegroups.com> Message-ID: >>>>> zayatzz (z) wrote: >z> Im trying to get aquinted to python on bit more basic level and am >z> following socket and threading programming tutorials from these 2 >z> addresses : >z> http://heather.cs.ucdavis.edu/~matloff/Python/PyNet.pdf >z> http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf >z> in this PyThreads file he sets up threaded server app which listens to >z> what clients send to it and echos it back to clients while adding sent >z> info into one string. >z> The problem is that the code does not work for me. >z> class srvr(threading.Thread): >z> v = "" >z> vlock = threading.Lock() >z> id = 0 >z> def __init__(self,clntsock): >z> threading.Thread.__init__(self) >z> self.myid = srvr.id >z> srvr.id += 1 >z> self.myclntsock = clntsock >z> def run(self): >z> while 1: >z> k = self.myclntsock.recv(1) >z> if k == "": break >z> srvr.vlock.acquire() >z> srvr.v += k >z> srvr.vlock.release() >z> self.myclntsock.send(srvr.v) >z> self.myclntsock.close() The last line is wrongly indented. It should be outside the loop. Shift it left so that it lines up with the while. What happens now is that the socket is closed after the first recv(). Then the next round in the loop fails because the myclntsock is closed. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From Samnsparky at gmail.com Sun Jul 12 17:54:13 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 14:54:13 -0700 (PDT) Subject: Webcam + GStreamer References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> Message-ID: <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> On Jul 12, 3:50?pm, Sparky wrote: > Hello! I need to stream from a webcam in Linux and I need to be able > to analyze the video feed frame by frame with PIL. Currently my web- > cam (Quickcam Chat) only seems to work with GStreamer so a solution > using pygst would be preferred. > > Thanks for your help, > Sam Sorry, to clarify I am just having a hard time capturing frames in a way that I can access with PIL. From davoodvahdati2009 at gmail.com Sun Jul 12 18:24:57 2009 From: davoodvahdati2009 at gmail.com (Davood Vahdati) Date: Sun, 12 Jul 2009 15:24:57 -0700 (PDT) Subject: c++ Source Code for acm 2004-2005 problems Message-ID: <03d25e16-97d2-4b09-8113-5011c0e0ded9@p28g2000vbn.googlegroups.com> Dear Sirs And Madams : it is an Acm programming competition Questions in year 2004-2005 . could you please solve problems is question ? I Wan't C++ Source Code program About this questions OR Problems . thank you for your prompt attention to this matter your faithfully. ----------------------------------------- 29th ACM International Collegiate Sponsored by Programming Contest, 2004-2005 Sharif Preliminary Contest Sharif University of Technology, 28 Oct. 2004 Problem B (Program filename: B.CPP, B.DPR, B.PAS or B.java) Parallelogram Counting There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4- element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line. Input (filename: B.IN) The first line of the input file contains a single integer t (1 ? t ? 10), the number of test cases. It is followed by the input data for each test case. The first line of each test case contains an integer n (1 ? n ? 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000. Output (Standard Output) Output should contain t lines. Line i contains an integer showing the number of the parallelograms as described above for test case i. Sample Input 2 6 0 0 2 0 4 0 1 1 3 1 5 1 7 -2 -1 8 9 5 7 1 1 4 8 2 0 9 8 Sample Output 5 6 ---------------------------------------------------------------------------------------------------------------------- Problem B-Page 1 of 1 28th ACM International Collegiate Sponsored by Programming Contest, 2003-2004 Sharif Preliminary Contest Sharif University of Technology, 14 Nov. 2003 Problem C (Program filename: C.CPP or C.PAS or C.DPR or C.java) Toy Storage Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for Reza to find his favorite toys anymore. Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top: We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys. Input (filename: C.IN) The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0< n ? 1000) and the number of toys is given in m (0 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box. Sample Input 4 10 0 10 100 0 20 20 80 80 60 60 40 40 5 10 15 10 95 10 25 10 65 10 75 10 35 10 45 10 55 10 85 10 5 6 0 10 60 0 4 3 15 30 3 1 6 8 10 10 2 1 2 8 1 5 5 5 40 10 7 9 0 Sample Output Box 2: 5 Box 1: 4 2: 1 ---------------------------------------------------------------------- 29th ACM International Collegiate Sponsored by Programming Contest, 2004-2005 Sharif Preliminary Contest Sharif University of Technology, 28 Oct. 2004 Problem D (Program filename: D.CPP, D.DPR, or D.java) Software Company A software developing company has been assigned two programming projects. As both projects are within the same contract, both must be handed in at the same time. It does not help if one is finished earlier. This company has n employees to do the jobs. To manage the two projects more easily, each is divided into m independent subprojects. Only one employee can work on a single subproject at one time, but it is possible for two employees to work on different subprojects of the same project simultaneously. Our goal is to finish the projects as soon as possible. Input (filename: D.IN) The first line of the input file contains a single integer t (1 ? t ? 11), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers n (1 ? n ? 100), and m (1 ? m ? 100). The input for this test case will be followed by n lines. Each line contains two integers which specify how much time in seconds it will take for the specified employee to complete one subproject of each project. So if the line contains x and y, it means that it takes the employee x seconds to complete a subproject from the first project, and y seconds to complete a subproject from the second project. Output (Standard Output) There should be one line per test case containing the minimum amount of time in seconds after which both projects can be completed. Sample Input 1 3 20 1 1 2 4 1 6 Sample Output 18 Problem D -Page 1 of 1 29th ACM International Collegiate Sponsored by Programming Contest, 2004-2005 Sharif Preliminary Contest Sharif University of Technology, 28 Oct. 2004 ---------------------------------------------------------------------- Problem F (Program filename: F.CPP, F.DPR, or F.java) Median Weight Bead There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads: A scale is given to compare the weights of beads. We can determine which one is heavier than the other between two beads. As the result, we now know that some beads are heavier than others. We are going to remove some beads which cannot have the medium weight. For example, the following results show which bead is heavier after M comparisons where M=4 and N=5. 1. Bead 2 is heavier than Bead 1. 2. Bead 4 is heavier than Bead 3. 3. Bead 5 is heavier than Bead 1. 4. Bead 4 is heavier than Bead 2. >From the above results, though we cannot determine exactly which is the median bead, we know that Bead 1 and Bead 4 can never have the median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3 are lighter than Bead 4. Therefore, we can remove these two beads. Write a program to count the number of beads which cannot have the median weight. Input (filename: F.IN) The first line of the input file contains a single integer t (1 ? t ? 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows: The first line of input data contains an integer N (1=N=99) denoting the number of beads, and M denoting the number of pairs of beads compared. In each of the next M lines, two numbers are given where the first bead is heavier than the second bead. Output (Standard Output) There should be one line per test case. Print the number of beads which can never have the medium weight. Sample Input Sample Output 1 2 5 4 2 1 4 3 5 1 4 2 Problem F -Page 1 of 2 From david at pythontoo.com Sun Jul 12 18:30:12 2009 From: david at pythontoo.com (David) Date: Sun, 12 Jul 2009 18:30:12 -0400 Subject: Webcam + GStreamer In-Reply-To: <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> Message-ID: <4A5A63F4.6010301@pythontoo.com> Sparky wrote: > On Jul 12, 3:50 pm, Sparky wrote: >> Hello! I need to stream from a webcam in Linux and I need to be able >> to analyze the video feed frame by frame with PIL. Currently my web- >> cam (Quickcam Chat) only seems to work with GStreamer so a solution >> using pygst would be preferred. >> >> Thanks for your help, >> Sam > > Sorry, to clarify I am just having a hard time capturing frames in a > way that I can access with PIL. Most web cams produce jpeg images so read the note at the bottom here; http://effbot.org/imagingbook/format-jpeg.htm What exactly are you trying to do? What have you tried so far and what happened may help. -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From ptmcg at austin.rr.com Sun Jul 12 18:46:05 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sun, 12 Jul 2009 15:46:05 -0700 (PDT) Subject: c++ Source Code for acm 2004-2005 problems References: <03d25e16-97d2-4b09-8113-5011c0e0ded9@p28g2000vbn.googlegroups.com> Message-ID: On Jul 12, 5:24?pm, Davood Vahdati wrote: > Dear Sirs And Madams : > > it is an Acm programming competition Questions in year 2004-2005 . > could you please solve problems is question ? I ?Wan't C++ Source Code > program About this questions OR Problems . thank you for your prompt > attention to this matter > > 2 1 > 4 3 > 5 1 > 4 2 > looking for the Python content in this post... mmmmm, nope, didn't find any... I guess the OP tried on a C++ newsgroup and got told to do his own homework, so he came here instead? From gagsl-py2 at yahoo.com.ar Sun Jul 12 19:13:43 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 12 Jul 2009 20:13:43 -0300 Subject: c++ Source Code for acm 2004-2005 problems References: <03d25e16-97d2-4b09-8113-5011c0e0ded9@p28g2000vbn.googlegroups.com> Message-ID: En Sun, 12 Jul 2009 19:24:57 -0300, Davood Vahdati escribi?: > it is an Acm programming competition Questions in year 2004-2005 . > could you please solve problems is question ? I Wan't C++ Source Code > program About this questions OR Problems . thank you for your prompt > attention to this matter Not C++ code but Python: A brute force approach to the first problem. > Parallelogram Counting > There are n distinct points in the plane, given by their integer > coordinates. Find the number of parallelograms whose > vertices lie on these points. class Point: def __init__(self, x, y): self.x, self.y = x, y def __repr__(self): return 'Point(%d,%d)' % (self.x, self.y) class Segment: def __init__(self, p0, p1): self.p0, self.p1 = p0, p1 def is_parallel(self, other): return ((self.p1.x-self.p0.x) * (other.p1.y-other.p0.y) - (self.p1.y-self.p0.y) * (other.p1.x-other.p0.x) == 0) points = [ Point(-2,-1), Point(8,9), Point(5,7), Point(1,1), Point(4,8), Point(2,0), Point(9,8)] n = 0 for i,A in enumerate(points): for B in points[i+1:]: AB = Segment(A,B) for C in points: if C in (A,B): continue BC = Segment(B,C) for D in points: if D in (A,B,C): continue CD = Segment(C,D) if AB.is_parallel(CD) and BC.is_parallel(Segment(A,D)): print A,B,C,D n += 1 n /= 4 # ABCD,BCDA,CDAB,DABC ## BACD etc already removed print n -- Gabriel Genellina From clp2 at rebertia.com Sun Jul 12 19:52:06 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 12 Jul 2009 16:52:06 -0700 Subject: multiprocessing and dictionaries In-Reply-To: <200907121116.13828.bjorn.m.meyer@gmail.com> References: <200907121116.13828.bjorn.m.meyer@gmail.com> Message-ID: <50697b2c0907121652j6cf27749xffd6057a3e01f7c@mail.gmail.com> On Sun, Jul 12, 2009 at 10:16 AM, Bjorn Meyer wrote: > I am trying to convert a piece of code that I am using the thread module with > to the multiprocessing module. > > The way that I have it set up is a chunk of code reads a text file and assigns > a dictionary key multiple values from the text file. I am using locks to write > the values to the dictionary. > The way that the values are written is as follows: > ? ? ? ?mydict.setdefault(key, []).append(value) > > The problem that I have run into is that using multiprocessing, the key gets > set, but the values don't get appended. Don't have much concurrency experience, but have you tried using a defaultdict instead? It's possible its implementation might solve the problem. http://docs.python.org/dev/library/collections.html#collections.defaultdict Cheers, Chris -- http://blog.rebertia.com From Samnsparky at gmail.com Sun Jul 12 20:01:55 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 17:01:55 -0700 (PDT) Subject: Webcam + GStreamer References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> Message-ID: <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> On Jul 12, 4:30?pm, David wrote: > Sparky wrote: > > On Jul 12, 3:50 pm, Sparky wrote: > >> Hello! I need to stream from a webcam in Linux and I need to be able > >> to analyze the video feed frame by frame with PIL. Currently my web- > >> cam (Quickcam Chat) only seems to work with GStreamer so a solution > >> using pygst would be preferred. > > >> Thanks for your help, > >> Sam > > > Sorry, to clarify I am just having a hard time capturing frames in a > > way that I can access with PIL. > > Most web cams produce jpeg images so read the note at the bottom here;http://effbot.org/imagingbook/format-jpeg.htm > What exactly are you trying to do? What have you tried so far and what > happened may help. > > -- > Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com Dear David, Thank you for your quick response. I have tried a few things. First of all, I have tried gst-launch-0.10 v4l2src ! ffmpegcolorspace ! pngenc ! filesink location=foo.png. Foo.png comes out sharp enough but it takes around 2 seconds to complete. I have also tried CVTypes but it does not run without LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so and, when it does run, it only displays colored "snow". Here is that code: import pygame import Image from pygame.locals import * import sys import opencv #this is important for capturing/displaying images from opencv import highgui camera = highgui.cvCreateCameraCapture(-1) print "cam:" + str(camera) def get_image(): print "here" im = highgui.cvQueryFrame(camera) #convert Ipl image to PIL image return opencv.adaptors.Ipl2PIL(im) fps = 30.0 pygame.init() window = pygame.display.set_mode((320,240)) pygame.display.set_caption("WebCam Demo") screen = pygame.display.get_surface() while True: events = pygame.event.get() im = get_image() print im.mode pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) screen.blit(pg_img, (0,0)) pygame.display.flip() pygame.time.delay(int(1000 * 1.0/fps)) Finally, I have gotten pygst to stream video with the example at http://pygstdocs.berlios.de/pygst-tutorial/webcam-viewer.html but of course I do not know how to get a hold of that data. Just so you know, I am trying a primitive type of object tracking. I would use some of the libraries already available but the two more popular implementations on Linux (tbeta/ccv and reacTIVision) dont seem to work with my web cam. I have more info on those non-python attempts at http://ubuntuforums.org/showthread.php?p=7596908. Unfortunately no one seemed to respond to that post. Thanks again, Sam From half.italian at gmail.com Sun Jul 12 20:03:32 2009 From: half.italian at gmail.com (Sean DiZazzo) Date: Sun, 12 Jul 2009 17:03:32 -0700 (PDT) Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception References: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> Message-ID: On Jul 10, 5:10?pm, Tim Chase wrote: > > ? ? ?shutil.rmtree(filename) > > ? ?File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree > > ? ? ?onerror(os.rmdir, path, sys.exc_info()) > > ? ?File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree > > ? ? ?os.rmdir(path) > > OSError: [Errno 39] Directory not empty: /path/to/my/dir > > > According to the documentation, shutil.rmtree should not care about ? > > directory being not empty. > > This sounds suspiciously like a permission issue. ?rmtree() > *should* walk the tree removing items *if it can*. ?If a file > can't be deleted, it treats it as an error. ?rmtree() takes > parameters for ignore_errors and an onerror callback function, so > you can catch these error conditions. > > -tkc This one took me a long time to find a solution for. Check this page, and see comment #3: http://code.activestate.com/recipes/193736/ I guess if the file is marked as "Read Only" or "Archive", or whatever, it cannot be deleted with shutil.rmtree() The key: win32api.SetFileAttributes(path, win32con.FILE_ATTRIBUTE_NORMAL) It will work! ~Sean From tjreedy at udel.edu Sun Jul 12 20:15:00 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 12 Jul 2009 20:15:00 -0400 Subject: Question about generators In-Reply-To: <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: Cameron Pulsford wrote: When you start a new thread, you should start a new thread and not piggyback on an existing thread. From cameron.pulsford at gmail.com Sun Jul 12 21:24:29 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Sun, 12 Jul 2009 21:24:29 -0400 Subject: Question about generators In-Reply-To: References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: itertools.chain() did it, thanks! As far as the primes generator, it does not generate any non-primes. All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) where is x is [1, 2, ..., n]. The only time it doesn't generate a prime is when x + (1 or 5) % 5 == 0. Which is what that last part is making sure doesn't happen. I'm not a mathematician or anything so correct me if I'm wrong, but that's what I've read. Also sorry if this was piggy backed, I started the thread as a fresh e- mail to python-list at python.org, sorry if I messed something up! On Jul 12, 2009, at 8:15 PM, Terry Reedy wrote: > Cameron Pulsford wrote: > > When you start a new thread, you should start a new thread and not > piggyback on an existing thread. > > -- > http://mail.python.org/mailman/listinfo/python-list From david at pythontoo.com Sun Jul 12 21:38:43 2009 From: david at pythontoo.com (David) Date: Sun, 12 Jul 2009 21:38:43 -0400 Subject: Webcam + GStreamer In-Reply-To: <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> Message-ID: <4A5A9023.8070600@pythontoo.com> Sparky wrote: > On Jul 12, 4:30 pm, David wrote: >> Sparky wrote: >>> On Jul 12, 3:50 pm, Sparky wrote: >>>> Hello! I need to stream from a webcam in Linux and I need to be able >>>> to analyze the video feed frame by frame with PIL. Currently my web- >>>> cam (Quickcam Chat) only seems to work with GStreamer so a solution >>>> using pygst would be preferred. >>>> Thanks for your help, >>>> Sam >>> Sorry, to clarify I am just having a hard time capturing frames in a >>> way that I can access with PIL. >> Most web cams produce jpeg images so read the note at the bottom here;http://effbot.org/imagingbook/format-jpeg.htm >> What exactly are you trying to do? What have you tried so far and what >> happened may help. >> >> -- >> Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com > > Dear David, > > Thank you for your quick response. I have tried a few things. First of > all, I have tried gst-launch-0.10 v4l2src ! ffmpegcolorspace ! > pngenc ! filesink location=foo.png. Foo.png comes out sharp enough but > it takes around 2 seconds to complete. I have also tried CVTypes but > it does not run without LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so and, > when it does run, it only displays colored "snow". Here is that code: > > import pygame > import Image > from pygame.locals import * > import sys > > import opencv > #this is important for capturing/displaying images > from opencv import highgui > > camera = highgui.cvCreateCameraCapture(-1) > print "cam:" + str(camera) > def get_image(): > print "here" > im = highgui.cvQueryFrame(camera) > #convert Ipl image to PIL image > return opencv.adaptors.Ipl2PIL(im) > > fps = 30.0 > pygame.init() > window = pygame.display.set_mode((320,240)) > pygame.display.set_caption("WebCam Demo") > screen = pygame.display.get_surface() > > while True: > events = pygame.event.get() > im = get_image() > print im.mode > pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) > screen.blit(pg_img, (0,0)) > pygame.display.flip() > pygame.time.delay(int(1000 * 1.0/fps)) > > Finally, I have gotten pygst to stream video with the example at > http://pygstdocs.berlios.de/pygst-tutorial/webcam-viewer.html but of > course I do not know how to get a hold of that data. Just so you know, > I am trying a primitive type of object tracking. I would use some of > the libraries already available but the two more popular > implementations on Linux (tbeta/ccv and reacTIVision) dont seem to > work with my web cam. I have more info on those non-python attempts at > http://ubuntuforums.org/showthread.php?p=7596908. Unfortunately no one > seemed to respond to that post. > > Thanks again, > Sam See if this gets you started, I got it working here with a video4linux2 cam. http://code.google.com/p/python-video4linux2/ here is what I have, I think it is the same; http://dwabbott.com/python-video4linux2-read-only/ let me know how you make out. -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From drobinow at gmail.com Sun Jul 12 22:00:15 2009 From: drobinow at gmail.com (David Robinow) Date: Sun, 12 Jul 2009 22:00:15 -0400 Subject: Question about generators In-Reply-To: References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: <4eb0089f0907121900o3f8f3416vd519c1487465fb1a@mail.gmail.com> On Sun, Jul 12, 2009 at 9:24 PM, Cameron Pulsford wrote: > As far as the primes generator, it does not generate any non-primes. All > primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) where is x is > [1, 2, ..., n]. The only time it doesn't generate a prime is when x + (1 or > 5) % 5 == 0. Which is what that last part is making sure doesn't happen. I'm > not a mathematician or anything so correct me if I'm wrong, but that's what > I've read. All you're doing is eliminating numbers divisible by 2,3, and 5. Your generator includes 49 (7 * 7), 77 (7*11), 91 (7*13), 121 (11*11), etc. From sjmachin at lexicon.net Sun Jul 12 22:26:20 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 12 Jul 2009 19:26:20 -0700 (PDT) Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> On Jul 13, 11:24?am, Cameron Pulsford wrote: > As far as the primes generator, it does not generate any non-primes. ? > All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) ? > where is x is [1, 2, ..., n]. The only time it doesn't generate a ? > prime is when x + (1 or 5) % 5 == 0. Which is what that last part is ? > making sure doesn't happen. I'm not a mathematician or anything so > correct me if I'm wrong, but that's what I've read. Where did you read that? Have you tried to verify it, like this: | >>> [6*i+j for i in range(1, 21) for j in (1, 5) if (i+j) % 5] | [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 77, 79, 83, 89, 91, 97, 101, 103, 107, 109, 113, 119, 121] 49, 77, 91, and 121 are not prime. From Samnsparky at gmail.com Sun Jul 12 22:54:21 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 19:54:21 -0700 (PDT) Subject: Webcam + GStreamer References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> Message-ID: <8aac1115-3dcc-4e91-94d6-ac4204879420@d9g2000prh.googlegroups.com> On Jul 12, 7:38?pm, David wrote: > Sparky wrote: > > On Jul 12, 4:30 pm, David wrote: > >> Sparky wrote: > >>> On Jul 12, 3:50 pm, Sparky wrote: > >>>> Hello! I need to stream from a webcam in Linux and I need to be able > >>>> to analyze the video feed frame by frame with PIL. Currently my web- > >>>> cam (Quickcam Chat) only seems to work with GStreamer so a solution > >>>> using pygst would be preferred. > >>>> Thanks for your help, > >>>> Sam > >>> Sorry, to clarify I am just having a hard time capturing frames in a > >>> way that I can access with PIL. > >> Most web cams produce jpeg images so read the note at the bottom here;http://effbot.org/imagingbook/format-jpeg.htm > >> What exactly are you trying to do? What have you tried so far and what > >> happened may help. > > >> -- > >> Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com > > > Dear David, > > > Thank you for your quick response. I have tried a few things. First of > > all, I have tried gst-launch-0.10 v4l2src ! ffmpegcolorspace ! > > pngenc ! filesink location=foo.png. Foo.png comes out sharp enough but > > it takes around 2 seconds to complete. I have also tried CVTypes but > > it does not run without LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so and, > > when it does run, it only displays colored "snow". Here is that code: > > > import pygame > > import Image > > from pygame.locals import * > > import sys > > > import opencv > > #this is important for capturing/displaying images > > from opencv import highgui > > > camera = highgui.cvCreateCameraCapture(-1) > > print "cam:" + str(camera) > > def get_image(): > > ? ?print "here" > > ? ?im = highgui.cvQueryFrame(camera) > > ? ?#convert Ipl image to PIL image > > ? ?return opencv.adaptors.Ipl2PIL(im) > > > fps = 30.0 > > pygame.init() > > window = pygame.display.set_mode((320,240)) > > pygame.display.set_caption("WebCam Demo") > > screen = pygame.display.get_surface() > > > while True: > > ? ?events = pygame.event.get() > > ? ?im = get_image() > > ? ?print im.mode > > ? ?pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) > > ? ?screen.blit(pg_img, (0,0)) > > ? ?pygame.display.flip() > > ? ?pygame.time.delay(int(1000 * 1.0/fps)) > > > Finally, I have gotten pygst to stream video with the example at > >http://pygstdocs.berlios.de/pygst-tutorial/webcam-viewer.htmlbut of > > course I do not know how to get a hold of that data. Just so you know, > > I am trying a primitive type of object tracking. I would use some of > > the libraries already available but the two more popular > > implementations on Linux (tbeta/ccv and reacTIVision) dont seem to > > work with my web cam. I have more info on those non-python attempts at > >http://ubuntuforums.org/showthread.php?p=7596908. Unfortunately no one > > seemed to respond to that post. > > > Thanks again, > > Sam > > See if this gets you started, I got it working here with a video4linux2 cam.http://code.google.com/p/python-video4linux2/ > here is what I have, I think it is the same;http://dwabbott.com/python-video4linux2-read-only/ > let me know how you make out. > -david > > -- > Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com I gave it a shot and here is what I got: sam at sam-laptop:~/python-video4linux2-read-only$ ./pyv4l2.py Available devices: ['/dev/video0'] /dev/video0 Capabilities: Capture ReadWrite Streaming Input 0: Name: zc3xx Type: camera Standards: [] Pixel formats: JPEG JPEG Resolutions: Segmentation fault ------------------------------------- sam at sam-laptop:~/python-video4linux2-read-only$ ./streampics.py /dev/ video0 0 BGR3 640 480 testpics Trying to create directory pics Recording /dev/video0:0 with format JPEG at (640, 480) Traceback (most recent call last): File "./streampics.py", line 94, in Run() File "./streampics.py", line 78, in Run d.SetupStreaming(5, StreamCallback) File "/home/sam/python-video4linux2-read-only/pyv4l2.py", line 682, in SetupStreaming self.StreamOn() File "/home/sam/python-video4linux2-read-only/pyv4l2.py", line 636, in StreamOn lib.Error() Exception: Could not start streaming: 22: Invalid argument *** glibc detected *** python: free(): invalid next size (fast): 0x0a2aeff0 *** Any suggestions? Thanks, Sam From cameron.pulsford at gmail.com Sun Jul 12 23:17:31 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Sun, 12 Jul 2009 23:17:31 -0400 Subject: Question about generators In-Reply-To: <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> Message-ID: I read it on the haskell site in their sieves/prime wheel section, I guess I misunderstood something. (east to do over there...) I did verify it against established list of primes and other generators I've written that use more normal methods, but I only hand verified it. It is at least interesting though, I can probably extend it to check for primality by using a more normal sieve method. It might be pretty fast too because generally it does only generate primes, and the few non primes it does generate could be caught quickly using a scratching out technique. When I was initially looking at it there are some interesting patterns I might be able to extend into a generator that would yield only correct sets of numbers for the 6x + n pattern. On Jul 12, 2009, at 10:26 PM, John Machin wrote: > On Jul 13, 11:24 am, Cameron Pulsford > wrote: > >> As far as the primes generator, it does not generate any non-primes. >> All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) >> where is x is [1, 2, ..., n]. The only time it doesn't generate a >> prime is when x + (1 or 5) % 5 == 0. Which is what that last part is >> making sure doesn't happen. I'm not a mathematician or anything so >> correct me if I'm wrong, but that's what I've read. > > Where did you read that? Have you tried to verify it, like this: > > | >>> [6*i+j for i in range(1, 21) for j in (1, 5) if (i+j) % 5] > | [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, > 71, 73, 77, 79, 83, 89, 91, 97, 101, 103, 107, 109, 113, 119, 121] > > 49, 77, 91, and 121 are not prime. > > -- > http://mail.python.org/mailman/listinfo/python-list From vincent.gulinao at gmail.com Mon Jul 13 01:06:11 2009 From: vincent.gulinao at gmail.com (Vincent Gulinao) Date: Mon, 13 Jul 2009 13:06:11 +0800 Subject: Infinite loops and synchronization Message-ID: lst = list() (lst populated by async twisted deferred callbacks) while True: if len(lst) == SOME_NUMBER: return lst Q1: is this a common OK practice? I'm worried infinite loops hogs memory. Q2: operating on list from threads (mostly appends) must be safe, right (synchronization)? From sjmachin at lexicon.net Mon Jul 13 01:08:30 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 12 Jul 2009 22:08:30 -0700 (PDT) Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> Message-ID: On Jul 13, 1:17?pm, Cameron Pulsford wrote: > I read it on the haskell site in their sieves/prime wheel section, I ? > guess I misunderstood something. (east to do over there...) I did ? > verify it against established list of primes and other generators I've ? > written that use more normal methods, but I only hand verified it. "to verify" something means to ensure the truth or correctness, NOT attempt to ensure ;-) From amit.kumar.iitr at gmail.com Mon Jul 13 02:42:02 2009 From: amit.kumar.iitr at gmail.com (Amit) Date: Sun, 12 Jul 2009 23:42:02 -0700 (PDT) Subject: compilation problem of python on AIX 6.1 Message-ID: <51c556a8-7277-474d-821f-190adf155c75@f16g2000vbf.googlegroups.com> Hello I want to intsall python on my AIX 6.1 Box. I couldn't get any rpm for python 2.5.x for AIX 6.1. THen I decided to compile python 2.5.4 on my AIX box. I downloaded the python source code from www.python.org and tried to compile on my AIX both using gcc. step 1 ./configure --with-gcc configuration runs successfully step 2 make it gives me error case $MAKEFLAGS in *-s*) CC='gcc -pthread' LDSHARED='./Modules/ ld_so_aix gcc -pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g - fwrapv -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py -q build;; *) CC='gcc -pthread' LDSHARED='./Modules/ld_so_aix gcc - pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes' ./python -E ./setup.py build;; esac running build running build_ext building 'bz2' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes -I. -I/avre/Python-2.5.4/./Include -I. -IInclude - I./Include -I/avre/Python-2.5.4/Include -I/avre/Python-2.5.4 -c /avre/ Python-2.5.4/Modules/bz2module.c -o build/temp.aix-6.1-2.5/avre/ Python-2.5.4/Modules/bz2module.o building '_tkinter' extension ./Modules/ld_so_aix gcc -pthread -bI:Modules/python.exp build/ temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/_tkinter.o build/ temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/tkappinit.o -L/usr/X11/lib - ltk8.4 -ltcl8.4 -lX11 -o build/lib.aix-6.1-2.5/_tkinter.so *** WARNING: renaming "_tkinter" since importing it failed: 0509-022 Cannot load module build/lib.aix-6.1-2.5. 0509-026 System error: A file or directory in the path name does not exist. plz suggest From piet at cs.uu.nl Mon Jul 13 03:56:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 09:56:08 +0200 Subject: multiprocessing and dictionaries References: Message-ID: >>>>> Bjorn Meyer (BM) wrote: >BM> I am trying to convert a piece of code that I am using the thread module with >BM> to the multiprocessing module. >BM> The way that I have it set up is a chunk of code reads a text file and assigns >BM> a dictionary key multiple values from the text file. I am using locks to write >BM> the values to the dictionary. >BM> The way that the values are written is as follows: >BM> mydict.setdefault(key, []).append(value) >BM> The problem that I have run into is that using multiprocessing, the key gets >BM> set, but the values don't get appended. >BM> I've even tried the Manager().dict() option, but it doesn't seem to work. >BM> Is this not supported at this time or am I missing something? I think you should give more information. Try to make a *minimal* program that shows the problem and include it in your posting or supply a download link. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 04:01:53 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 10:01:53 +0200 Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> Message-ID: >>>>> Cameron Pulsford (CP) wrote: >CP> I read it on the haskell site in their sieves/prime wheel section, >CP> I guess I misunderstood something. (east to do over there...) I did >CP> verify it against established list of primes and other generators >CP> I've written that use more normal methods, but I only hand verified >CP> it. If it is used in a sieve then the non-primes will be sieved out. >CP> It is at least interesting though, I can probably extend it to >CP> check for primality by using a more normal sieve method. It might >CP> be pretty fast too because generally it does only generate primes, >CP> and the few non primes it does generate could be caught quickly >CP> using a scratching out technique. it does only generate primes => it does generate all primes. >CP> When I was initially looking at it there are some interesting >CP> patterns I might be able to extend into a generator that would >CP> yield only correct sets of numbers for the 6x + n pattern. As I wrote in my previous reply, in your use case the non-primes are harmless. But it is useful to reflect that in the naming of your identifiers. *And please, don't top post.* -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 04:53:36 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 10:53:36 +0200 Subject: Infinite loops and synchronization References: Message-ID: >>>>> Vincent Gulinao (VG) wrote: >VG> lst = list() >VG> (lst populated by async twisted deferred callbacks) >VG> while True: >VG> if len(lst) == SOME_NUMBER: >VG> return lst >VG> Q1: is this a common OK practice? I'm worried infinite loops hogs memory. >VG> Q2: operating on list from threads (mostly appends) must be safe, >VG> right (synchronization)? I am not familiar enough with twisted, but I think the principle is independent from twisted. This loop will not hog memory but it will hog CPU time. You should use a synchronisation construct like threading.Condition or a Semaphore. Here is my suggestion with a Condition: Global somewhere: lst_cond = Condition() In your loop: lst = list() # Why not lst = []? while True: # strange while/if combo if len(lst) == SOME_NUMBER: return lst Make that: with lst_cond: while len(lst) < SOME_NUMBER: lst_cond.wait() return lst In the callback: with lst_cond: lst.append(new_value) lst_cond.notify() In case you don't have a python that supports the with statement (not even `from future') you should use: lst_cond.acquire() try: ..... finally: lst_cond.release() I think the solution with a semaphore is less elegant. global: sem = Semaphore() loop: for i in range(SOME_NUMBER): sem.acquire() return lst In callback: lst.append(new_value) sem.release() *Be careful: I haven't tested this code (not even syntax checked). So consider it pseudo code.* -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From vinay_sajip at yahoo.co.uk Mon Jul 13 05:39:40 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Mon, 13 Jul 2009 02:39:40 -0700 (PDT) Subject: the ultimate logging facility for debugging code References: Message-ID: <69032a2b-0f8e-4174-b996-6203387c4058@l5g2000vbp.googlegroups.com> On Jul 10, 10:39?am, Jean-Michel Pichavant wrote: > Greetings, > > Sorry for the dubious title :o). I was wandering if there is a standard > (or reliable) python module that implements the following feature: > > http://code.activestate.com/recipes/198078/ > ?>>>Recipe 198078: Wrapping method calls (meta-class example) > ?>>> > ?>>>A metaclass is used to wrap all (or just some) methods forlogging > purposes. The underlying mechanism can be used as well to check pre/post > conditions, attribute access,... The basic point is, that the actual > class must not be changed in any way to achive the desired effect. > > I had to adapt the code to some of my needs, but now it's almost > working, by simply defining a metaclass for any of my class, I get the > class methods calls logged with the instance that called the method, the > method name, the call line in the file, the parameter **value** ?passed > to the method and the returned value. > It just saved me from hours of ipython interactive debugging with the > old print way, and to beauty of it: I don't have to write any > "self._log('Please log me')" line. > > As it is so useful, I bet there is a module that is doing this in a more > reliable way that I did. Anyone has heard of it ? > > Jean-Michel Check out the Python Decorator Library: http://wiki.python.org/moin/PythonDecoratorLibrary and you should be able to adapt what you find there to your needs. Regards, Vinay Sajip From pdpinheiro at gmail.com Mon Jul 13 06:12:03 2009 From: pdpinheiro at gmail.com (pdpi) Date: Mon, 13 Jul 2009 03:12:03 -0700 (PDT) Subject: Infinite loops and synchronization References: Message-ID: On Jul 13, 6:06?am, Vincent Gulinao wrote: > lst = list() > > (lst populated by async twisted deferred callbacks) > > while True: > ? ? ? ? if len(lst) == SOME_NUMBER: > ? ? ? ? ? ? ? ? return lst > > Q1: is this a common OK practice? I'm worried infinite loops hogs memory. > Q2: operating on list from threads (mostly appends) must be safe, > right (synchronization)? Q1: I'll answer your question with another. What's so fundamentally different between your infinite loop and this one: while len(lst) != SOME_NUMBER: pass return lst which is not an "infinite loop"[1]. Why would yours be any worse in terms of memory than mine? Are you allocating anything that would hog memory? Of course, like Piet said, it *will* hog your CPU, so you want a time.sleep(.1) in there, at the least. Of course, the question is: why aren't you using a semaphore to let you know you can proceed, and make the provider increment the semaphore? [1] -- well, it can be, if len(lst) == SOME_NUMBER never comes about, and I'd hazard a guess that that's pretty much where your fear of memory hogging comes from: it's easy to allocate stuff and not deallocate it within a cycle, only to find the bounds on that cycle going awry. From ganesh.gbg at gmail.com Mon Jul 13 06:34:19 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Mon, 13 Jul 2009 03:34:19 -0700 (PDT) Subject: simple question about Dictionary type containing List objects Message-ID: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> Hi group, I have a dict object like emails={'mycontacts': [ 'xxx at gmail.com, 'yy at gmail.com', 'zz at gmail.com'], 'myname':['gganesh']} I need to get the lenght of the list mycontacts ,like mycontacts_numbers=3 help me to solve Thanks From contact at xavierho.com Mon Jul 13 06:36:55 2009 From: contact at xavierho.com (Xavier Ho) Date: Mon, 13 Jul 2009 18:36:55 +0800 Subject: simple question about Dictionary type containing List objects In-Reply-To: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> References: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> Message-ID: <2d56febf0907130336x6950051em7903f66b5dfc5349@mail.gmail.com> On Mon, Jul 13, 2009 at 6:34 PM, gganesh wrote: > Hi group, > I have a dict object like > emails={'mycontacts': [ 'xxx at gmail.com, 'yy at gmail.com', > 'zz at gmail.com'], 'myname':['gganesh']} > I need to get the lenght of the list mycontacts ,like > mycontacts_numbers=3 > >>> len(emails['mycontacts']) 3 HTH, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Mon Jul 13 07:22:36 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:22:36 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , Aahz wrote: > In article , > Lawrence D'Oliveiro wrote: >>In message , Aahz wrote: >>> >>> It helps to remember that names and namespaces are in many >>> ways syntactic sugar for dicts or lists. >> >>Interesting, though, that Python insists on maintaining a distinction >>between c["x"] and c.x, whereas JavaScript doesn't bother. > > Why do you say "insists"? > > class AttrDict: > def __getitem__(self, key): > return getattr(self, key) OK, let's try it: >>> c = {} >>> c["x"] = 3 >>> c.x = 4 Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'x' >>> class AttrDict: ... def __getitem__(self, key): ... return getattr(self, key) ... >>> c.x = 4 Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'x' Nope, still doesn't work... From ldo at geek-central.gen.new_zealand Mon Jul 13 07:24:03 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:24:03 +1200 Subject: Infinite loops and synchronization References: Message-ID: In message , Vincent Gulinao wrote: > Q1: is this a common OK practice? I'm worried infinite loops hogs memory. The problem is not that the loop is infinite, but that it busy-waits, hogging CPU. From eckhardt at satorlaser.com Mon Jul 13 07:25:29 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Mon, 13 Jul 2009 13:25:29 +0200 Subject: simple question about Dictionary type containing List objects References: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> Message-ID: <96iri6-s13.ln1@satorlaser.homedns.org> gganesh wrote: > I have a dict object like > emails={'mycontacts': [ 'xxx at gmail.com, 'yy at gmail.com', > 'zz at gmail.com'], 'myname':['gganesh']} > I need to get the lenght of the list mycontacts ,like > mycontacts_numbers=3 mycontacts = emails['mycontacts'] mycontacts_number = len(mycontacts) A list doesn't change in behaviour when it is contained in a dictionary. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From ldo at geek-central.gen.new_zealand Mon Jul 13 07:27:33 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:27:33 +1200 Subject: MySQLdb + SSH Tunnel References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: In message , Emile van Sebille wrote: > ssh with something like... > > ssh -lroot -L3306:C:3306 B > > Watch out for other instances of mysql on A or B... You can use a non-default local port and specify that in your local connection parameters. Similarly you can tell the remote server to use a non-default port in its /etc/my.cnf. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:29:39 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:29:39 +1200 Subject: Looking for a tool to checkfor python script backward compatibility References: <4a59951a$0$10853$426a74cc@news.free.fr> Message-ID: In message <4a59951a$0$10853$426a74cc at news.free.fr>, Baptiste Lepilleur wrote: > I'm looking for a tool that could be used in a pre-commit step to check > that only features available in a "old" python version are used, say > python 2.3 for example. The only sure way would be to run the script under an appropriately old version of Python. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:35:43 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:35:43 +1200 Subject: send keys to focused window References: Message-ID: In message , Broken wrote: > I am new to Python, and I'm miserably failing to send specific keys to > (say) notepad. I don't understand why you need to automate a GUI front-end, meant for human use, to a function that can be directly performed without that front-end anyway. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:37:09 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:37:09 +1200 Subject: Check file is locked? References: <652cca82-44a3-473f-b640-c2336a9cf929@v15g2000prn.googlegroups.com> Message-ID: In message <652cca82-44a3-473f-b640- c2336a9cf929 at v15g2000prn.googlegroups.com>, Rajat wrote: > ... my whole idea is to close the wordpad / notepad application so that I > can delete the file and the directory where this file resides. Don't you think the user might have that application open for a reason? From ldo at geek-central.gen.new_zealand Mon Jul 13 07:39:34 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:39:34 +1200 Subject: A Bug By Any Other Name ... References: <4a538a71$0$30236$9b4e6d93@newsspool1.arcor-online.net> Message-ID: In message <4a538a71$0$30236$9b4e6d93 at newsspool1.arcor-online.net>, Stefan Behnel wrote: > Lawrence D'Oliveiro wrote: >> >> I wonder how many people have been tripped up by the fact that >> >> ++n >> >> and >> >> --n >> >> fail silently for numeric-valued n. > > I doubt that there are many. Plus, you misspelled them from the more > obvious > > n++ > > and > > n-- > > which *do* fail with a SyntaxError. Funny, you accuse me of "misspelling", when it's _your_ version that fails with errors! From ldo at geek-central.gen.new_zealand Mon Jul 13 07:44:32 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:44:32 +1200 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: In message , MRAB wrote: > I wonder whether the problem with assignment in conditionals in C is due > to the assignment operator being "=". If it was ":=", would the error > still occur? One of the original C designers, Kernighan or Ritchie, admitted that he made the assignment operator "=" instead of ":=" just to save typing one character, because he thought assignments were more common than tests for equality. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:47:10 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:47:10 +1200 Subject: Blocking XMPP API? References: Message-ID: In message , Gabriel Rossetti wrote: > I am looking for blocking XMPP API. I'm wanting to make a webservice > that uses XMPP, but all the XMPP libs I find are non-blocking (i.e. w/ > callbacks). So why not have the caller sleep or something until the callback is invoked? It's easier to turn an async API into a synchronous one than the other way round. From piet at cs.uu.nl Mon Jul 13 08:15:07 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 14:15:07 +0200 Subject: The meaning of "=" References: Message-ID: >>>>> Lawrence D'Oliveiro (LD) wrote: >LD> In message , Aahz wrote: >Aahz> class AttrDict: >Aahz> def __getitem__(self, key): >Aahz> return getattr(self, key) >LD> OK, let's try it: >LD> >>> c = {} >LD> >>> c["x"] = 3 >LD> >>> c.x = 4 >LD> Traceback (most recent call last): >LD> File "", line 1, in >LD> AttributeError: 'dict' object has no attribute 'x' >LD> >>> class AttrDict: >LD> ... def __getitem__(self, key): >LD> ... return getattr(self, key) >LD> ... >LD> >>> c.x = 4 >LD> Traceback (most recent call last): >LD> File "", line 1, in >LD> AttributeError: 'dict' object has no attribute 'x' >LD> Nope, still doesn't work... Of course you need c = AttrDict() And to get c.x = 4 working you also need a __setitem__. And to get c["x"] working AtrrDict should subclass dict: >>> class AttrDict(dict): but these are only minor details :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From dzizes451 at gmail.com Mon Jul 13 09:07:52 2009 From: dzizes451 at gmail.com (dzizes) Date: Mon, 13 Jul 2009 06:07:52 -0700 (PDT) Subject: how to set timeout while colling a soap method? Message-ID: <24461403.post@talk.nabble.com> Hello! I wrote some some python code for executing a soap method: import SOAPpy from SOAPpy import WSDL _server = WSDL.Proxy(some wsdl) r=_server.generuj(some parameters...) print r.encode('cp1250') It works fine. However, the execution time of this soap method might be long. Therefore, I would like to set a timeout like 1 minute, after which I would post a timeoute message. Any ideas? Greats, -- View this message in context: http://www.nabble.com/how-to-set-timeout-while-colling-a-soap-method--tp24461403p24461403.html Sent from the Python - python-list mailing list archive at Nabble.com. From denis-bz-gg at t-online.de Mon Jul 13 09:11:09 2009 From: denis-bz-gg at t-online.de (denis) Date: Mon, 13 Jul 2009 06:11:09 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> Message-ID: <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Matt, how many words are you looking for, in how long a string ? Were you able to time any( substr in long_string ) against re.compile ( "|".join( list_items )) ? (REs are my method of choice, but different inputs of course give different times -- see google regex speed site:groups.google.com / site:stackoverflow.com .) cheers -- denis From seldan24 at gmail.com Mon Jul 13 09:26:19 2009 From: seldan24 at gmail.com (seldan24) Date: Mon, 13 Jul 2009 06:26:19 -0700 (PDT) Subject: Best Way to Handle All Exceptions Message-ID: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Hello, I'm fairly new at Python so hopefully this question won't be too awful. I am writing some code that will FTP to a host, and want to catch any exception that may occur, take that and print it out (eventually put it into a log file and perform some alerting action). I've figured out two different ways to do this, and am wondering which is the best (i.e. cleanest, 'right' way to proceed). I'm also trying to understand exactly what occurs for each one. The first example: from ftplib import FTP try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except Exception, err: print err This works fine. I read through the documentation, and my understanding is that there is a built-in exceptions module in python, that is automatically available in a built-in namespace. Within that module is an 'Exception' class which would contain whatever exception is thrown. So, I'm passing that to the except, along with err to hold the value and then print it out. The second example: from ftplib import FTP import sys try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except: print sys.exc_info() Here I, for the most part, get the same thing. I'm not passing anything to except and just printing out the exception using a method defined in the sys module. So, I'm new to Python... I've made it this far and am happy, but want to make sure I'm coding correctly from the start. Which method is the better/cleaner/more standard way to continue? Thanks for any help. From dsdale24 at gmail.com Mon Jul 13 09:38:17 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Mon, 13 Jul 2009 06:38:17 -0700 (PDT) Subject: Is it possible to use keyword arguments from C? Message-ID: <94892b60-b4cb-40fc-8c9d-7cfb7d1c00ed@q35g2000vbi.googlegroups.com> I am learning about the python C-api in order to contribute a feature to numpy. I see a discussion (http://docs.python.org/extending/ extending.html#keyword-parameters-for-extension-functions) on how to create a function or method in C that accepts kwargs, but is it possible to call such a method from C using kwargs rather than positional arguments? From dsdale24 at gmail.com Mon Jul 13 09:47:41 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Mon, 13 Jul 2009 06:47:41 -0700 (PDT) Subject: Is it possible to use keyword arguments from C? References: <94892b60-b4cb-40fc-8c9d-7cfb7d1c00ed@q35g2000vbi.googlegroups.com> Message-ID: <29a66e7b-1263-4c06-ab01-017b471d53d1@p36g2000vbn.googlegroups.com> On Jul 13, 9:38?am, Darren Dale wrote: > I am learning about the python C-api in order to contribute a feature > to numpy. I see a discussion (http://docs.python.org/extending/ > extending.html#keyword-parameters-for-extension-functions) on how to > create a function or method ?in C that accepts kwargs, but is it > possible to call such a method from C using kwargs rather than > positional arguments? The answer was yes, this can be done. I found a short discussion at the bottom of section 1.6: http://docs.python.org/extending/extending.html#calling-python-functions-from-c From tonylay at gmail.com Mon Jul 13 09:57:29 2009 From: tonylay at gmail.com (Tony Lay) Date: Mon, 13 Jul 2009 06:57:29 -0700 (PDT) Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory Message-ID: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> Trying to build python-2.6.2 ./configure --prefix=/usr/local --exec-prefix=/usr/local LDFLAGS="-L/ usr/local" (runs through happily, had to make some libs local) make runs most of the way until? building '_tkinter' extension gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/ meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/ include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c / tmp/meld/Python-2.6.2/Modules/_tkinter.c -o build/temp.linux-i686-2.6/ tmp/meld/Python-2.6.2/Modules/_tkinter.o gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/ meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/ include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c / tmp/meld/Python-2.6.2/Modules/tkappinit.c -o build/temp.linux-i686-2.6/ tmp/meld/Python-2.6.2/Modules/tkappinit.o gcc -pthread -shared build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/ Modules/_tkinter.o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/ Modules/tkappinit.o -L/usr/X11R6/lib64 -L/usr/X11R6/lib -L/usr/local/ lib -ltk8.5 -ltcl8.5 -lX11 -o build/lib.linux-i686-2.6/_tkinter.so *** WARNING: renaming "_tkinter" since importing it failed: libtk8.5.so: cannot open shared object file: No such file or directory Failed to find the necessary bits to build these modules: _sqlite3 bsddb185 sunaudiodev To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: _tkinter running build_scripts # cd /usr/local/lib # ls -la | grep libtk8.5.so -r-xr-xr-x 1 root root 1112606 Jul 10 13:28 libtk8.5.so Am I missing something, it?s there? Regards, -Tony From lists at cheimes.de Mon Jul 13 10:09:47 2009 From: lists at cheimes.de (Christian Heimes) Date: Mon, 13 Jul 2009 16:09:47 +0200 Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory In-Reply-To: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> References: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> Message-ID: Tony Lay wrote: > # cd /usr/local/lib > > # ls -la | grep libtk8.5.so > > -r-xr-xr-x 1 root root 1112606 Jul 10 13:28 libtk8.5.so > > Am I missing something, it?s there? Is /usr/local/lib in your library search path? It looks like it isn't. Check /etc/ld.so.conf and /etc/ld.so.conf.d/. Christian From invalid at invalid Mon Jul 13 10:16:12 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 13 Jul 2009 09:16:12 -0500 Subject: Problem with two instances of PySerial References: Message-ID: > On Thu, 9 Jul 2009 08:40:18 -0500, Shine Jose wrote: > I achieve this by creating a separate worker thread to poll > the serial port for arrival of data and then updating the > required widget in main thread of program. For polling the > serial port, I create a separate instance of pySerial object. > However, I am unable to read any data in the worker thread. > Can the reason for this be 2 instances of pySerial objects > being connected to serial port. Yes. > The reason I had this doubt was because Serial.Connect() in > the worker thread did not throw any exception & isOpen() > method returns true. Under Unix, you are allowed to open a device or file as many times as you want (there are some common cases where there is a very useful thing to do). However, for something like a serial port, there's only one copy of each received data byte. Depending on exactly how you structure your code, the incoming data may be divided up amongst multiple readers, or one particular reader may get all of it. -- Grant Edwards grante Yow! I joined scientology at at a garage sale!! visi.com From deets at nospam.web.de Mon Jul 13 10:17:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 13 Jul 2009 16:17:53 +0200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <7c0toaF25751bU1@mid.uni-berlin.de> seldan24 wrote: > Hello, > > I'm fairly new at Python so hopefully this question won't be too > awful. I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). > I've figured out two different ways to do this, and am wondering which > is the best (i.e. cleanest, 'right' way to proceed). I'm also trying > to understand exactly what occurs for each one. > > The first example: > > from ftplib import FTP > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except Exception, err: > print err > > This works fine. I read through the documentation, and my > understanding is that there is a built-in exceptions module in python, > that is automatically available in a built-in namespace. Within that > module is an 'Exception' class which would contain whatever exception > is thrown. So, I'm passing that to the except, along with err to hold > the value and then print it out. > > The second example: > > from ftplib import FTP > import sys > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except: > print sys.exc_info() > > Here I, for the most part, get the same thing. I'm not passing > anything to except and just printing out the exception using a method > defined in the sys module. > > So, I'm new to Python... I've made it this far and am happy, but want > to make sure I'm coding correctly from the start. Which method is the > better/cleaner/more standard way to continue? Thanks for any help. The latter is - unfortunately - the better. This comes from python allowing all kinds of objects being thrown as exceptions, not only those extending from a common ancestor like Exception. You seem to have a sensible take on this, but anyway I'd like to mention that using these kinds of catch-all code is rarely justified, as it imposes the danger of not handling errors at all. So make sure the program spits out a sensible error-message, and then dies. Unless it's a server-process of course. Diez From invalid at invalid Mon Jul 13 10:19:29 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 13 Jul 2009 09:19:29 -0500 Subject: need to write a assembly progrm References: Message-ID: On 2009-07-12, Rhodri James wrote: >> The poorly punctuated paragraph sounds, to me, as if they mean >> they want (Windows assumed) EXE files that can be dropped onto >> remote/laptop machines without performing a full installation >> of Python... >> >> But yes... "assembly programs" does, to me, also mean things >> containing mnemonics for machine level opcodes. > > Given that to me, "assembly programs" does not mean .EXE files > at all, not even a little bit, I'm prefering to refuse to > guess :-) Ah, but guessing what a poster meant to ask and then answering the "guessed" question is a pretty popular sport here in c.l.p. The discussions resulting from "guessed" questions are often more informative than the answer to the intended question (if the OP ever returns to clarify his intent). -- Grant Edwards grante Yow! Bo Derek ruined at my life! visi.com From tonylay at gmail.com Mon Jul 13 10:24:08 2009 From: tonylay at gmail.com (Tony Lay) Date: Mon, 13 Jul 2009 07:24:08 -0700 (PDT) Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory References: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> Message-ID: On Jul 13, 10:09?am, Christian Heimes wrote: > Tony Lay wrote: > > # cd /usr/local/lib > > > # ls -la | grep libtk8.5.so > > > -r-xr-xr-x ? 1 root root ?1112606 Jul 10 13:28 libtk8.5.so > > > Am I missing something, it?s there? > > Is /usr/local/lib in your library search path? It looks like it isn't. > Check /etc/ld.so.conf and /etc/ld.so.conf.d/. > > Christian I added /usr/local/lib to ld.so.conf.d/python26.conf and ran ldconfig (thanks Chrisitan) Ran a make distclean (had to move the files and got a ?file not found? from my previous working directory). Everything compiled and installed like a charm. Thanks! From python at mrabarnett.plus.com Mon Jul 13 10:33:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 Jul 2009 15:33:42 +0100 Subject: Best Way to Handle All Exceptions In-Reply-To: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <4A5B45C6.5060404@mrabarnett.plus.com> seldan24 wrote: > Hello, > > I'm fairly new at Python so hopefully this question won't be too > awful. I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). > I've figured out two different ways to do this, and am wondering which > is the best (i.e. cleanest, 'right' way to proceed). I'm also trying > to understand exactly what occurs for each one. > > The first example: > > from ftplib import FTP > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except Exception, err: > print err > > This works fine. I read through the documentation, and my > understanding is that there is a built-in exceptions module in python, > that is automatically available in a built-in namespace. Within that > module is an 'Exception' class which would contain whatever exception > is thrown. So, I'm passing that to the except, along with err to hold > the value and then print it out. > There isn't an "exceptions module"; exceptions are part of the language. > The second example: > > from ftplib import FTP > import sys > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except: > print sys.exc_info() > This is called a "bare exception handler". It's virtually never the right way to do it. > Here I, for the most part, get the same thing. I'm not passing > anything to except and just printing out the exception using a method > defined in the sys module. > > So, I'm new to Python... I've made it this far and am happy, but want > to make sure I'm coding correctly from the start. Which method is the > better/cleaner/more standard way to continue? Thanks for any help. You should use the most specific exception possible because at lot of things could raise an exception: >>> foo Traceback (most recent call last): File "", line 1, in foo NameError: name 'foo' is not defined >>> try: foo except Exception, e: print "*** Caught an exception ***" print e *** Caught an exception *** name 'foo' is not defined From bryanvick at gmail.com Mon Jul 13 10:52:51 2009 From: bryanvick at gmail.com (Bryan) Date: Mon, 13 Jul 2009 07:52:51 -0700 (PDT) Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: On Jul 10, 12:03?pm, mgi... at motorola.com (Gary Duzan) wrote: > In article <3af970b1-b454-4d56-a33f-889ecfaca... at l28g2000vba.googlegroups.com>, > > Bryan ? wrote: > > >rsyncExec = '/usr/bin/ssh' > >source = 'r... at 10.0.45.67:/home/bry/jquery.lookup' > >dest = '/home/bry/tmp' > >rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > >args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] > > ? ?I think you want -e and the ssh command to be separate args. > Something like: > > rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' > args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] > > or: > > rshArgs = [ '-e', '/usr/bin/ssh -i /home/bry/keys/brybackup.key' ] > args = [rsyncExec, '-a', '-v', '--dry-run'] + ?rshArgs + [ source, dest] > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Gary Duzan > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Motorola H&NM Separating the argument parts worked. Strangely though, I don't need to do that for arguments such as --files-from='/path/to/file'. Also, in this example code I had the rsync executable path pointing to the ssh program, so no wonder I was getting the output of ssh! From seldan24 at gmail.com Mon Jul 13 11:03:27 2009 From: seldan24 at gmail.com (seldan24) Date: Mon, 13 Jul 2009 08:03:27 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: On Jul 13, 10:33?am, MRAB wrote: > seldan24 wrote: > > Hello, > > > I'm fairly new at Python so hopefully this question won't be too > > awful. ?I am writing some code that will FTP to a host, and want to > > catch any exception that may occur, take that and print it out > > (eventually put it into a log file and perform some alerting action). > > I've figured out two different ways to do this, and am wondering which > > is the best (i.e. cleanest, 'right' way to proceed). ?I'm also trying > > to understand exactly what occurs for each one. > > > The first example: > > > from ftplib import FTP > > try: > > ? ? ftp = FTP(ftp_host) > > ? ? ftp.login(ftp_user, ftp_pass) > > except Exception, err: > > ? ? print err > > > This works fine. ?I read through the documentation, and my > > understanding is that there is a built-in exceptions module in python, > > that is automatically available in a built-in namespace. ?Within that > > module is an 'Exception' class which would contain whatever exception > > is thrown. ?So, I'm passing that to the except, along with err to hold > > the value and then print it out. > > There isn't an "exceptions module"; exceptions are part of the language. > > > The second example: > > > from ftplib import FTP > > import sys > > try: > > ? ? ftp = FTP(ftp_host) > > ? ? ftp.login(ftp_user, ftp_pass) > > except: > > ? ? print sys.exc_info() > > This is called a "bare exception handler". It's virtually never the > right way to do it. > > > Here I, for the most part, get the same thing. ?I'm not passing > > anything to except and just printing out the exception using a method > > defined in the sys module. > > > So, I'm new to Python... I've made it this far and am happy, but want > > to make sure I'm coding correctly from the start. ?Which method is the > > better/cleaner/more standard way to continue? ?Thanks for any help. > > You should use the most specific exception possible because at lot of > things could raise an exception: > > ?>>> foo > > Traceback (most recent call last): > ? ?File "", line 1, in > ? ? ?foo > NameError: name 'foo' is not defined > ?>>> try: > ? ? ?foo > except Exception, e: > ? ? ?print "*** Caught an exception ***" > ? ? ?print e > > *** Caught an exception *** > name 'foo' is not defined- Hide quoted text - > > - Show quoted text - Thank you both for your input. I want to make sure I get started on the right track. For this particular script, I should have included that I would take the exception contents, and pass those to the logging module. For this particular script, all exceptions are fatal and I would want them to be. I just wanted a way to catch them and log them prior to program termination. I can understand why folks should always specify exact exceptions where possible if they plan on doing something with them, but I just want to log and terminate (and alarm) when any exception, irregardless of what it is, occurs; that's why I'm using the catch-all approach. I most likely should have put in an exit() in my snippet; sorry about that. I did notice that Python allows different objects to be thrown. This threw me off a bit because, at first, I was expecting everything to come through as nice, easy tuples. It turns out that ftplib throws some class from the sockets module (my terminology may be off here). As for terminology, sorry about the 'exceptions' misuse. The Python documentation refers to 'exceptions' as a module, albeit built-in, so I used that language accordingly (link to doc: http://docs.python.org/library/exceptions.html?highlight=exceptions#module-exceptions). Anyway, thanks again for the help and advice, it is greatly appreciated. From floris.bruynooghe at gmail.com Mon Jul 13 11:13:34 2009 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Mon, 13 Jul 2009 08:13:34 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: On Jul 13, 2:26?pm, seldan24 wrote: > The first example: > > from ftplib import FTP > try: > ? ? ftp = FTP(ftp_host) > ? ? ftp.login(ftp_user, ftp_pass) > except Exception, err: > ? ? print err *If* you really do want to catch *all* exceptions (as mentioned already it is usually better to catch specific exceptions) this is the way to do it. To know why you should look at the class hierarchy on http://docs.python.org/library/exceptions.html. The reason is that you almost never want to be catching SystemExit, KeyboardInterrupt etc. catching them will give you trouble at some point (unless you really know what you're doing but then I would suggest you list them explicitly instead of using the bare except statement). While it is true that you could raise an object that is not a subclass from Exception it is very bad practice, you should never do that. And I've haven't seen an external module in the wild that does that in years and the stdlib will always play nice. Regards Floris From hartley79 at gmail.com Mon Jul 13 11:47:03 2009 From: hartley79 at gmail.com (hartley) Date: Mon, 13 Jul 2009 08:47:03 -0700 (PDT) Subject: Passing python list from C to python Message-ID: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> I'm very new at wrapping Python/C, and I have run into some problems. I have one python module that provides me with a list (provideBuffer in provideBuff.py): Py_Initialize(); pName = PyString_FromString("provideBuff"); pModule = PyImport_Import(pName); pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); pValue = PyObject_CallObject(pFunc,NULL); pValue is now a PyList - i've even verified this with: int a = PyList_Check(pValue); printf("%d\n", a); However, I want to send this PyList to another python module, but I don't know how to do this. Initially I though I could just do like above, only swapping NULL with pValue, but that is not working. pName2 = PyString_FromString("C-embedding"); pModule2 = PyImport_Import(pName2); pFunc2 = PyObject_GetAttrString(pModule2,"buff"); pValue2 = PyObject_CallObject(pFunc2,pValue); pValue2 is now False! So i guess i cannot pass pValue as an argument to PyObject_CallObject when i want to pass an python list as an argument. But how must a go about to make this work? From degibb at gmail.com Mon Jul 13 12:21:37 2009 From: degibb at gmail.com (David Gibb) Date: Mon, 13 Jul 2009 12:21:37 -0400 Subject: list of all possible values Message-ID: Hi guys. I was thinking about a problem I had: suppose I have a list of possible values. I want to to have a list of all possible lists of length n whose values are in that original list. For example: if my values are ['a', 'b', 'c'], then all possible lists of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. I created a recursive program to do it, but I was wondering if there was a better way of doing it (possibly with list comprehensions). Here's my recursive version: vals = ['a', 'b', 'c'] def foo(length): if length <=0: return [] if length == 1: return [[x] for x in vals] else: return [x + [y] for x in foo(length - 1) for y in vals] print foo(3) Thanks, David From bjorn.m.meyer at gmail.com Mon Jul 13 12:29:56 2009 From: bjorn.m.meyer at gmail.com (Bjorn Meyer) Date: Mon, 13 Jul 2009 10:29:56 -0600 Subject: multiprocessing and dictionaries In-Reply-To: References: Message-ID: <200907131029.56346.bjorn.m.meyer@gmail.com> On Monday 13 July 2009 01:56:08 Piet van Oostrum wrote: > >>>>> Bjorn Meyer (BM) wrote: > > > >BM> I am trying to convert a piece of code that I am using the thread > > module with BM> to the multiprocessing module. > > > >BM> The way that I have it set up is a chunk of code reads a text file and > > assigns BM> a dictionary key multiple values from the text file. I am > > using locks to write BM> the values to the dictionary. > >BM> The way that the values are written is as follows: > >BM> mydict.setdefault(key, []).append(value) > > > >BM> The problem that I have run into is that using multiprocessing, the > > key gets BM> set, but the values don't get appended. > >BM> I've even tried the Manager().dict() option, but it doesn't seem to > > work. > > > >BM> Is this not supported at this time or am I missing something? > > I think you should give more information. Try to make a *minimal* program > that shows the problem and include it in your posting or supply a > download link. > -- > Piet van Oostrum > URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] > Private email: piet at vanoostrum.org Here is what I have been using as a test. This pretty much mimics what I am trying to do. I put both threading and multiprocessing in the example which shows the output that I am looking for. #!/usr/bin/env python import threading from multiprocessing import Manager, Process name = ('test1','test2','test3') data1 = ('dat1','dat2','dat3') data2 = ('datA','datB','datC') def thread_test(name,data1,data2, d): for nam in name: for num in range(0,3): d.setdefault(nam, []).append(data1[num]) d.setdefault(nam, []).append(data2[num]) print 'Thread test dict:',d def multiprocess_test(name,data1,data2, mydict): for nam in name: for num in range(0,3): mydict.setdefault(nam, []).append(data1[num]) mydict.setdefault(nam, []).append(data2[num]) print 'Multiprocess test dic:',mydict if __name__ == '__main__': mgr = Manager() md = mgr.dict() d = {} m = Process(target=multiprocess_test, args=(name,data1,data2,md)) m.start() t = threading.Thread(target=thread_test, args=(name,data1,data2,d)) t.start() m.join() t.join() print 'Thread test:',d print 'Multiprocess test:',md Thanks Bjorn From python.list at tim.thechases.com Mon Jul 13 12:33:20 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 13 Jul 2009 11:33:20 -0500 Subject: list of all possible values In-Reply-To: References: Message-ID: <4A5B61D0.5070403@tim.thechases.com> > For example: if my values are ['a', 'b', 'c'], then all possible lists > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. > > I created a recursive program to do it, but I was wondering if there > was a better way of doing it (possibly with list comprehensions). > > Here's my recursive version: > > vals = ['a', 'b', 'c'] > > def foo(length): > if length <=0: > return [] > if length == 1: > return [[x] for x in vals] > else: > return [x + [y] for x in foo(length - 1) for y in vals] Sounds like you want one of the combinitoric generators found in itertools[1] -- in this case, the itertools.product() does what you describe. According to the docs, it was added in 2.6 so if you're running an older version, you'd have to back-port it. -tkc [1] http://docs.python.org/library/itertools.html#itertools.product From sjmachin at lexicon.net Mon Jul 13 12:35:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 13 Jul 2009 09:35:16 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> Message-ID: <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> On Jul 14, 1:47?am, hartley wrote: > I'm very new at wrapping Python/C, and I have run into some problems. > > I have one python module that provides me with a list (provideBuffer > in provideBuff.py): > > ?Py_Initialize(); > ? ? ? ? pName = PyString_FromString("provideBuff"); > ? ? ? ? pModule = PyImport_Import(pName); > > ? ? ? ? pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > pValue is now a PyList - i've even verified this with: > > int a = PyList_Check(pValue); > ? ? ? ? printf("%d\n", a); > > However, I want to send this PyList to another python module, Please explain "send" ... do you mean the C equivalent of the Python statement C_embedding.buff = the_pylist ? BTW C-embedding would trigger a syntax error in Python source; best to avoid ... > but I > don't know how to do this. Initially I though I could just do like > above, only swapping NULL with pValue, but that is not working. > > pName2 = PyString_FromString("C-embedding"); > pModule2 = PyImport_Import(pName2); > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); Get?? Do you want Set? Is buff a Python function? Or is it the destination of the "sending"? Any good reason for not checking the return value for an error? [Rhetorical question; answer == "No"] > pValue2 = PyObject_CallObject(pFunc2,pValue); CallObject?? You used this before because you had a function and wanted to call it because it returned you a value .... now you want to do one of (in Python terms) value = amodule.anattr value = getattr(amodule, "anattr") or amodule.anattr = value setattr(amodule, "anattr", value) > pValue2 is now False! False?? Do you mean the C NULL? > So i guess i cannot pass pValue as an argument > to PyObject_CallObject when i want to pass an python list as an > argument. But how must a go about to make this work? It's mainly just a matter of (1) knowing what you want to do (2) picking the API that does what you want (3) checking the returned value for error after every call. HTH, John From bearophileHUGS at lycos.com Mon Jul 13 12:35:49 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Mon, 13 Jul 2009 09:35:49 -0700 (PDT) Subject: list of all possible values References: Message-ID: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> David Gibb: > For example: if my values are ['a', 'b', 'c'], then all possible lists > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. >>> from itertools import product >>> list(product("abc", repeat=2)) [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')] Bye, bearophile From whatnextur at gmail.com Mon Jul 13 12:50:57 2009 From: whatnextur at gmail.com (whatnext@gmail.com) Date: Mon, 13 Jul 2009 09:50:57 -0700 (PDT) Subject: Germany Ups Terrorism Alert Before Election Message-ID: With Germany going to the polls in a general election in three months, authorities are on high alert after detecting an increase in online warnings of terrorist attacks targeting the country. The German government held high-level talks with top security and intelligence chiefs in Berlin on Thursday to discuss the growing threat posed by Islamic extremists and to coordinate counterterrorism measures. Intelligence officials are alarmed by the rising number of videos posted online by militant Islamists who say they are specifically targeting Germany. Up to 13 videos are reported to have appeared on the Web since January, many of them referring to the deployment of German troops in Afghanistan. (See pictures of U.S. Marines opening a new offensive in Afghanistan.) for more http://www.terrorismsearch.blogspot.com/ From andreas.tawn at ubisoft.com Mon Jul 13 12:51:07 2009 From: andreas.tawn at ubisoft.com (Andreas Tawn) Date: Mon, 13 Jul 2009 18:51:07 +0200 Subject: list of all possible values In-Reply-To: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> Message-ID: <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> > David Gibb: > > For example: if my values are ['a', 'b', 'c'], then all possible lists > > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. > > >>> from itertools import product > >>> list(product("abc", repeat=2)) > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', > 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')] > > Bye, > bearophile Certainly possible with list comprehensions. >>> a = "abc" >>> [(x, y) for x in a for y in a] [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')] But I like bearophile's version better. Cheers, Drea From gabriel.rossetti at arimaz.com Mon Jul 13 13:08:12 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Mon, 13 Jul 2009 19:08:12 +0200 Subject: Blocking XMPP API? In-Reply-To: References: Message-ID: <4A5B69FC.5010109@arimaz.com> Lawrence D'Oliveiro wrote: > In message , Gabriel > Rossetti wrote: > > >> I am looking for blocking XMPP API. I'm wanting to make a webservice >> that uses XMPP, but all the XMPP libs I find are non-blocking (i.e. w/ >> callbacks). >> > > So why not have the caller sleep or something until the callback is invoked? > > It's easier to turn an async API into a synchronous one than the other way > round. > > Yes, that is what I ended up doing, thanks for the response though :-) Gabriel From skong1 at gmail.com Mon Jul 13 13:10:00 2009 From: skong1 at gmail.com (sityee kong) Date: Mon, 13 Jul 2009 10:10:00 -0700 Subject: Memory error due to big input file Message-ID: Hi All, I have a similar problem that many new python users might encounter. I would really appreciate if you could help me fix the error. I have a big text file with size more than 2GB. It turned out memory error when reading in this file. Here is my python script, the error occurred at line -- self.fh.readlines(). import math import time class textfile: def __init__(self,fname): self.name=fname self.fh=open(fname) self.fh.readline() self.lines=self.fh.readlines() a=textfile("/home/sservice/nfbc/GenoData/CompareCalls3.diff") lfile=len(a.lines) def myfun(snp,start,end): subdata=a.lines[start:end+1] NEWmiss=0 OLDmiss=0 DIFF=0 for row in subdata: k=row.split() if (k[3]=="0/0") & (k[4]!="0/0"): NEWmiss=NEWmiss+1 elif (k[3]!="0/0") & (k[4]=="0/0"): OLDmiss=OLDmiss+1 elif (k[3]!="0/0") & (k[4]!="0/0"): DIFF=DIFF+1 result.write(snp+" "+str(NEWmiss)+" "+str(OLDmiss)+" "+str(DIFF)+"\n") result=open("Summary_noLoop_diff3.txt","w") result.write("SNP NEWmiss OLDmiss DIFF\n") start=0 snp=0 for i in range(lfile): if (i==0): continue after=a.lines[i].split() before=a.lines[i-1].split() if (before[0]==after[0]): if (i!=(lfile-1)): continue else: end=lfile-1 myfun(before[0],start,end) snp=snp+1 else: end=i-1 myfun(before[0],start,end) snp=snp+1 start=i if (i ==(lfile-1)): myfun(after[0],start,start) snp=snp+1 result.close() sincerely, phoebe -------------- next part -------------- An HTML attachment was scrubbed... URL: From skip at pobox.com Mon Jul 13 13:18:25 2009 From: skip at pobox.com (skip at pobox.com) Date: Mon, 13 Jul 2009 12:18:25 -0500 Subject: Memory error due to big input file In-Reply-To: References: Message-ID: <19035.27745.515643.604146@montanaro.dyndns.org> phoebe> I have a big text file with size more than 2GB. It turned out phoebe> memory error when reading in this file. Here is my python phoebe> script, the error occurred at line -- self.fh.readlines(). phoebe> import math phoebe> import time phoebe> class textfile: phoebe> def __init__(self,fname): phoebe> self.name=fname phoebe> self.fh=open(fname) phoebe> self.fh.readline() phoebe> self.lines=self.fh.readlines() Don't do that. The problem is that you are trying to read the entire file into memory. Learn to operate a line (or a few lines) at a time. Try something like: a = open("/home/sservice/nfbc/GenoData/CompareCalls3.diff") for line in a: do your per-line work here -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ when i wake up with a heart rate below 40, i head right for the espresso machine. -- chaos @ forums.usms.org From python at mrabarnett.plus.com Mon Jul 13 13:20:43 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 Jul 2009 18:20:43 +0100 Subject: Memory error due to big input file In-Reply-To: References: Message-ID: <4A5B6CEB.1010507@mrabarnett.plus.com> sityee kong wrote: > Hi All, > > I have a similar problem that many new python users might encounter. I > would really appreciate if you could help me fix the error. > I have a big text file with size more than 2GB. It turned out memory > error when reading in this file. Here is my python script, the error > occurred at line -- self.fh.readlines(). > [snip code] Your 'error' is that you're running it on a computer with insufficient memory. From gabriel.rossetti at arimaz.com Mon Jul 13 13:28:27 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Mon, 13 Jul 2009 19:28:27 +0200 Subject: Threading.Condition problem In-Reply-To: References: Message-ID: <4A5B6EBB.30906@arimaz.com> Piet van Oostrum wrote: >>>>>> Gabriel Rossetti (GR) wrote: >>>>>> > > >> GR> Sorry if this appears twice, I sent it once with an attachment and it never >> GR> arrived so maybe the attachment is posing problems. I inlined the code this >> GR> time (at the bottom), thank you, >> > > >> GR> Gabriel >> > > >> GR> ########################## Original message ############################ >> > > >> GR> Hello everyone, >> > > >> GR> I wrote a small example that listens for xmpp msgs in a thread. The main >> GR> program calls a function that blocks (using Condition.wait) until a msg >> GR> has been received and then returns the msg. When a msg arrives, it is >> GR> put in a variable in the thread's object, it then calls the notify() >> GR> attr on the Condition object. For some reason, this doesn't work, the >> GR> thread gets the msg, tries to notify the Condition object, fails because >> GR> the lock has not been acquired yet and blocks. I tried ignoring the >> GR> failure, thinking that since it has not been acquired yet then when it >> GR> is, it will get the msg right away and never call Condition.wait, thus >> GR> not causing any problems, but this does not work either. Does someone >> GR> know what I am doing wrong? I attached the code to this msg. >> > > The code that puts the message in the variable should also acquire the > lock: > > > def onMessage(self, conn, msg): > with self._cv: > self.message = msg > self._cv.notify() > Thank you, that was the problem, I eventually found that > A couple of remarks: > > 1. I think the code is neater if all manipulation with the condition is > done in the same class (actually in the same instance -- making this > instance into a monitor). > The reason I didn't do that is that I don' t want the Listener to sleep, I maybe over simplified the example, I actually put them in a dictionary as they come in, so in your example, if I have several threads waiting on msgs it wouldn't work. I'm trying to make a webservice api thay will also be turned into a java .jar for people that need java. Now that I think about it, each session will have an instance of the object so msgs shouldn' t get mixed up (one connection per user), so I could block in the thread. I'll try your suggestion as I think it is cleaner. > class Listener(Thread): > def __init__(self, ws): > Thread.__init__(self) > self.interrupt = Event() > self.message = None > self._cv = Condition() > self.client = ws._client > self.client.RegisterHandler('message', self.onMessage) > > def onMessage(self, conn, msg): > with self._cv: > self.message = msg > try: > self._cv.notify() > except RuntimeError: > print "self._cv has not acquired the lock yet" > > def getMsg(self): > with self._cv: > while !self.message > self._cv.wait() > return self.message > > class WS(object): > def __init__(self, username, password, res): > self._jid = xmpp.protocol.JID(username) > self._client = xmpp.Client(self._jid.getDomain()) > # self._cv = Condition() > > def getMsg(self, mid=None): > """ > """ > return self._listener.getMsg() > > Of course I haven't tested this code as I don't have the context > modules. > > 2. I don't know if more than one message can be delivered in the same > instance. If yes, than your code will not work, and neither will the > code above as, the message instance variable is never cleared. So the > next getMsg will be happy to deliver the previous one. > You would have to clear it when returning this one. > > Like I said above, in reality I have a dict not just a simple variable. > def getMsg(self): > with self._cv: > while !self.message > self._cv.wait() > msg = self.message > self.message = None > return msg > > 3. If the messages come in faster than they can be processed some will > be lost as they will overwrite the previous one in the self.message > variable. The solution is to use a threading.Queue to transfer the > messages from one thread to the other. This also saves you the hassle > of doing your own synchronisation like above. If you are not familiar > with synchronising multithreaded applications it is very easy to make > errors and even if you are it is quite easy to do them wrong. I have > been involved in distributed programming courses at university level > and I have seen many errors in this area. > I used a dict because the API can also be setup to be async and use callbacks, so I had to be able to access the msgs directly and quickly. Gabriel From vilya.harvey at gmail.com Mon Jul 13 13:55:50 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Mon, 13 Jul 2009 18:55:50 +0100 Subject: Best Way to Handle All Exceptions In-Reply-To: References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <6aef848f0907131055p76072a24h68ef2636b9676f50@mail.gmail.com> 2009/7/13 seldan24 : > Thank you both for your input. ?I want to make sure I get started on > the right track. ?For this particular script, I should have included > that I would take the exception contents, and pass those to the > logging module. ?For this particular script, all exceptions are fatal > and I would want them to be. ?I just wanted a way to catch them and > log them prior to program termination. The logging module has a function specifically to handle this case: try: # do something except: logging.exception("Uh oh...") The exception() method will automatically add details of the exception to the log message it creates; as per the docs, you should only call it from within an exception handler. Hope that helps, Vil. From emile at fenx.com Mon Jul 13 13:58:25 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 13 Jul 2009 10:58:25 -0700 Subject: list of all possible values In-Reply-To: <4A5B61D0.5070403@tim.thechases.com> References: <4A5B61D0.5070403@tim.thechases.com> Message-ID: On 7/13/2009 9:33 AM Tim Chase said... >> For example: if my values are ['a', 'b', 'c'], then all possible lists >> of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. >> >> I created a recursive program to do it, but I was wondering if there >> was a better way of doing it (possibly with list comprehensions). >> >> Here's my recursive version: >> >> vals = ['a', 'b', 'c'] >> >> def foo(length): >> if length <=0: >> return [] >> if length == 1: >> return [[x] for x in vals] >> else: >> return [x + [y] for x in foo(length - 1) for y in vals] > > Sounds like you want one of the combinitoric generators found in > itertools[1] -- in this case, the itertools.product() does what you > describe. According to the docs, it was added in 2.6 so if you're > running an older version, you'd have to back-port it Or on systems with list comps try: >>> V='abc' >>> ['%s%s'%(ii,jj) for ii in V for jj in V] ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] >>> Emile From mantihor at gmail.com Mon Jul 13 14:08:39 2009 From: mantihor at gmail.com (Bogdan Opanchuk) Date: Mon, 13 Jul 2009 11:08:39 -0700 (PDT) Subject: How to override Marshaller/Unmarshaller in xmlrpc? Message-ID: Is there a way to override marshaller in xmlrpc.client properly? For example, I want to hide bytes -> Binary transformation inside marshaller (i.e., just let user pass a bytes value to function and marshaller dumps it in base64 autmatically). Unfortunately, I cannot just make a derived class with necessary dump_* function and pass it to client and server; marshaller initialization seems to be hidden inside xmlrpc. So, here's the question - is there a nice way to do it (because there are some dirty ways like rewriting getparser() ans so on, but I don't like the idea of copypasting code from library)? Or should I reconsider my design instead? Thanks in advance. From degibb at gmail.com Mon Jul 13 14:15:24 2009 From: degibb at gmail.com (David Gibb) Date: Mon, 13 Jul 2009 14:15:24 -0400 Subject: list of all possible values In-Reply-To: References: <4A5B61D0.5070403@tim.thechases.com> Message-ID: > Or on systems with list comps try: > >>>> V='abc' >>>> ['%s%s'%(ii,jj) for ii in V for jj in V] > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] >>>> Yeah, except that the length here is hard-coded. There's no way (as far as I can tell, at least), to make this generic with respect to list length. Thanks for the itertools suggestion, guys. I was hoping to improve my list-comprehension-fu, but that module was the next best thing. David On Mon, Jul 13, 2009 at 1:58 PM, Emile van Sebille wrote: > On 7/13/2009 9:33 AM Tim Chase said... >>> >>> For example: if my values are ['a', 'b', 'c'], then all possible lists >>> of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. >>> >>> I created a recursive program to do it, but I was wondering if there >>> was a better way of doing it (possibly with list comprehensions). >>> >>> Here's my recursive version: >>> >>> vals = ['a', 'b', 'c'] >>> >>> def foo(length): >>> ? ?if length <=0: >>> ? ? ? ?return [] >>> ? ?if length == 1: >>> ? ? ? ?return [[x] for x in vals] >>> ? ?else: >>> ? ? ? ?return [x + [y] for x in foo(length - 1) for y in vals] >> >> Sounds like you want one of the combinitoric generators found in >> itertools[1] -- in this case, the itertools.product() does what you >> describe. ?According to the docs, it was added in 2.6 so if you're running >> an older version, you'd have to back-port it > > > Emile > > -- > http://mail.python.org/mailman/listinfo/python-list > From darkwater42 at gmail.com Mon Jul 13 14:19:59 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Mon, 13 Jul 2009 11:19:59 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? Message-ID: I couldn't find a good algorithms forum on the Internet, so I guess I'll ask this question here instead: Is it possible to efficiently maintain a binary search tree in a flat array (i.e., without using pointers), as is typically done for a binary heap? It *is* possible, of course, to keep an ordered list in a flat array, and then do a binary search on the ordered array, but then insertion and deletion are O(n), rather than O(log n). It would also clearly be possible to store a balanced binary tree in a flat array, storing the children of the node at index i at indices 2*i and 2*i + 1, just as one does for a binary heap. But if you do that, I don't know if you could then do insertions and deletions in O(log n) time. One idea that came to mind, is that maybe it is possible using a "treap", which is a combination of a binary heap and a binary search tree. Insertions and deletions in binary heaps can be done in O(log n) in a flat array, but I don't know if this is also true for a treap, since you also have the binary search tree invariants to maintain, in addition to the binary heap invariants. For all I know, this might cause rotations to no longer be O(log n). |>ouglas From piet at cs.uu.nl Mon Jul 13 14:25:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 20:25:58 +0200 Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: >>>>> Bryan (B) wrote: >B> On Jul 10, 12:03?pm, mgi... at motorola.com (Gary Duzan) wrote: >>> In article <3af970b1-b454-4d56-a33f-889ecfaca... at l28g2000vba.googlegroups.com>, >>> >>> Bryan ? wrote: >>> >>> >rsyncExec = '/usr/bin/ssh' >>> >source = 'r... at 10.0.45.67:/home/bry/jquery.lookup' >>> >dest = '/home/bry/tmp' >>> >rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>> >args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] >>> >>> ? ?I think you want -e and the ssh command to be separate args. >>> Something like: >>> >>> rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' >>> args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] >>> >>> or: >>> >>> rshArgs = [ '-e', '/usr/bin/ssh -i /home/bry/keys/brybackup.key' ] >>> args = [rsyncExec, '-a', '-v', '--dry-run'] + ?rshArgs + [ source, dest] >>> >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Gary Duzan >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Motorola H&NM >B> Separating the argument parts worked. Strangely though, I don't need >B> to do that for arguments such as --files-from='/path/to/file'. Also, >B> in this example code I had the rsync executable path pointing to the >B> ssh program, so no wonder I was getting the output of ssh! I should have seen that because I changed it in my own copy!!! --files-from='/path/to/file *is* one argument, in contrast to -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" which is two arguments. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 14:33:17 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 20:33:17 +0200 Subject: Threading.Condition problem References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Piet van Oostrum wrote: ... >GR> I wrote a small example that listens for xmpp msgs in a thread. The main >GR> program calls a function that blocks (using Condition.wait) until a msg >GR> has been received and then returns the msg. When a msg arrives, it is >GR> put in a variable in the thread's object, it then calls the notify() >GR> attr on the Condition object. For some reason, this doesn't work, the >GR> thread gets the msg, tries to notify the Condition object, fails because >GR> the lock has not been acquired yet and blocks. I tried ignoring the >GR> failure, thinking that since it has not been acquired yet then when it >GR> is, it will get the msg right away and never call Condition.wait, thus >GR> not causing any problems, but this does not work either. Does someone >GR> know what I am doing wrong? I attached the code to this msg. >>>> >>> >>> The code that puts the message in the variable should also acquire the >>> lock: >>> >>> >>> def onMessage(self, conn, msg): >>> with self._cv: >>> self.message = msg >>> self._cv.notify() >>> >GR> Thank you, that was the problem, I eventually found that >>> A couple of remarks: >>> >>> 1. I think the code is neater if all manipulation with the condition is >>> done in the same class (actually in the same instance -- making this >>> instance into a monitor). >>> >GR> The reason I didn't do that is that I don' t want the Listener to sleep, I >GR> maybe over simplified the example, I actually put them in a dictionary as >GR> they come in, so in your example, if I have several threads waiting on msgs >GR> it wouldn't work. I'm trying to make a webservice api thay will also be >GR> turned into a java .jar for people that need java. Now that I think about >GR> it, each session will have an instance of the object so msgs shouldn' t get >GR> mixed up (one connection per user), so I could block in the thread. I'll >GR> try your suggestion as I think it is cleaner. Sleeping as you call it is better than busy waiting. You must have some synchronisation to make it efficient. If you put the messages in a dictionary access to the dictionary must be protected. Having several threads waiting for the messages doesn't prevent you from using proper synchronisation. Maybe you must use notify_all instead of notify. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From http Mon Jul 13 14:55:34 2009 From: http (Paul Rubin) Date: 13 Jul 2009 11:55:34 -0700 Subject: Efficient binary search tree stored in a flat array? References: Message-ID: <7x7hycmmzd.fsf@ruckus.brouhaha.com> Douglas Alan writes: > It would also clearly be > possible to store a balanced binary tree in a flat array, storing the > children of the node at index i at indices 2*i and 2*i + 1, just as > one does for a binary heap. But if you do that, I don't know if you > could then do insertions and deletions in O(log n) time. Probably not. Maybe you could organize a 2-3 tree like that, at the expense of some space. From piet at cs.uu.nl Mon Jul 13 15:12:18 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 21:12:18 +0200 Subject: multiprocessing and dictionaries References: Message-ID: >>>>> Bjorn Meyer (BM) wrote: >BM> Here is what I have been using as a test. >BM> This pretty much mimics what I am trying to do. >BM> I put both threading and multiprocessing in the example which shows >BM> the output that I am looking for. >BM> #!/usr/bin/env python >BM> import threading >BM> from multiprocessing import Manager, Process >BM> name = ('test1','test2','test3') >BM> data1 = ('dat1','dat2','dat3') >BM> data2 = ('datA','datB','datC') [snip] >BM> def multiprocess_test(name,data1,data2, mydict): >BM> for nam in name: >BM> for num in range(0,3): >BM> mydict.setdefault(nam, []).append(data1[num]) >BM> mydict.setdefault(nam, []).append(data2[num]) >BM> print 'Multiprocess test dic:',mydict I guess what's happening is this: d.setdefault(nam, []) returns a list, initially an empty list ([]). This list gets appended to. However, this list is a local list in the multi-process_test Process, therefore the result is not reflected in the original list inside the manager. Therefore all your updates get lost. You will have to do operations directly on the dictionary itself, not on any intermediary objects. Of course with the threading the situation is different as all operations are local. This works: def multiprocess_test(name,data1,data2, mydict): print name, data1, data2 for nam in name: for num in range(0,3): mydict.setdefault(nam, []) mydict[nam] += [data1[num]] mydict[nam] += [data2[num]] print 'Multiprocess test dic:',mydict If you have more than one process operating on the dictionary simultaneously you have to beware of race conditions!! -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 15:31:28 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 21:31:28 +0200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: >>>>> seldan24 (s) wrote: >s> Hello, >s> I'm fairly new at Python so hopefully this question won't be too >s> awful. I am writing some code that will FTP to a host, and want to >s> catch any exception that may occur, take that and print it out >s> (eventually put it into a log file and perform some alerting action). >s> I've figured out two different ways to do this, and am wondering which >s> is the best (i.e. cleanest, 'right' way to proceed). I'm also trying >s> to understand exactly what occurs for each one. >s> The first example: >s> from ftplib import FTP >s> try: >s> ftp = FTP(ftp_host) >s> ftp.login(ftp_user, ftp_pass) >s> except Exception, err: >s> print err I think you should restrict yourself to those exceptions that are related to ftp. Do you want to catch an exception like a misspelling in one of the variables? from ftplib import FTP, all_errors try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except all_errors, err: print err -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Mon Jul 13 15:57:38 2009 From: aahz at pythoncraft.com (Aahz) Date: 13 Jul 2009 12:57:38 -0700 Subject: Efficient binary search tree stored in a flat array? References: Message-ID: In article , Douglas Alan wrote: > >I couldn't find a good algorithms forum on the Internet, so I guess >I'll ask this question here instead: Is it possible to efficiently >maintain a binary search tree in a flat array (i.e., without using >pointers), as is typically done for a binary heap? > >It *is* possible, of course, to keep an ordered list in a flat array, >and then do a binary search on the ordered array, but then insertion >and deletion are O(n), rather than O(log n). Still, unless your list is large (more than thousands of elements), that's the way you should go. See the bisect module. Thing is, the speed difference between C and Python means the constant for insertion and deletion is very very small relative to bytecode speed. Keep in mind that Python's object/binding model means that you're shuffling pointers in the list rather than items. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From newport at freemail.hu Mon Jul 13 16:17:31 2009 From: newport at freemail.hu (Andras Szabo) Date: Mon, 13 Jul 2009 23:17:31 +0300 Subject: Tkinter / Entry widget problem Message-ID: Hello. I searched the archives but couldn't find a solution to a problem related to the Entry widget in Tkinter. When creating a pop-up window in an app, which contains an Entry widget, I want this widget to contain some default string, to have all this default string selected (as if the user had manually selected everything), and to have the focus transferred to this widget. (The idea is then that if the window pops up, the user won't have to click or press Tab any more before being able to type what is needed in the textbox, overwriting what is written there already.) I thought this might be the way to go: entrybox=Entry(toplevel_parent_window) entrybox.insert(0,"Some default string") entrybox.select_range(0,END) entrybox.focus_set() entrybox.pack() But it doesn't seem to work - the focus is not transferred to the Entry widget, and the text does not appear to be selected (even though after this entrybox.selection_present() returns True). What am I doing wrong? andras From tjreedy at udel.edu Mon Jul 13 16:19:14 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 13 Jul 2009 16:19:14 -0400 Subject: Best Way to Handle All Exceptions In-Reply-To: <7c0toaF25751bU1@mid.uni-berlin.de> References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <7c0toaF25751bU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > The latter is - unfortunately - the better. This comes from python allowing > all kinds of objects being thrown as exceptions, not only those extending > from a common ancestor like Exception. Fixed in Python 3. exceptions, without exceptions, are instances of BaseException, either directly or as instances of subcleasses of BaseException. One of many reasons to switch when possible. tjr From darkwater42 at gmail.com Mon Jul 13 16:47:19 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Mon, 13 Jul 2009 13:47:19 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: Message-ID: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> On Jul 13, 3:57?pm, a... at pythoncraft.com (Aahz) wrote: > Still, unless your list is large (more than thousands of elements), > that's the way you should go. ?See the bisect module. ?Thing is, the > speed difference between C and Python means the constant for insertion > and deletion is very very small relative to bytecode speed. ?Keep in > mind that Python's object/binding model means that you're shuffling > pointers in the list rather than items. Thank you. My question wasn't intended to be Python specific, though. I am just curious for purely academic reasons about whether there is such an algorithm. All the sources I've skimmed only seem to the answer the question via omission. Which is kind of strange, since it seems to me like an obvious question to ask. If I find the free time, I might try to work out myself whether it can be done with a treap. |>ouglas From davea at ieee.org Mon Jul 13 17:01:17 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 13 Jul 2009 17:01:17 -0400 Subject: Memory error due to big input file In-Reply-To: References: Message-ID: <4A5BA09D.9070402@ieee.org> sityee kong wrote: > Hi All, > > I have a similar problem that many new python users might encounter. I would > really appreciate if you could help me fix the error. > I have a big text file with size more than 2GB. It turned out memory error > when reading in this file. Here is my python script, the error occurred at > line -- self.fh.readlines(). > > import math > import time > > class textfile: > def __init__(self,fname): > self.name=fname > self.fh=open(fname) > self.fh.readline() > self.lines=self.fh.readlines() > > a=textfile("/home/sservice/nfbc/GenoData/CompareCalls3.diff") > > lfile=len(a.lines) > > def myfun(snp,start,end): > subdata=a.lines[start:end+1] > NEWmiss=0 > OLDmiss=0 > DIFF=0 > for row in subdata: > k=row.split() > if (k[3]=="0/0") & (k[4]!="0/0"): > NEWmiss=NEWmiss+1 > elif (k[3]!="0/0") & (k[4]=="0/0"): > OLDmiss=OLDmiss+1 > elif (k[3]!="0/0") & (k[4]!="0/0"): > DIFF=DIFF+1 > result.write(snp+" "+str(NEWmiss)+" "+str(OLDmiss)+" "+str(DIFF)+"\n") > > result=open("Summary_noLoop_diff3.txt","w") > result.write("SNP NEWmiss OLDmiss DIFF\n") > > start=0 > snp=0 > for i in range(lfile): > if (i==0): continue > after=a.lines[i].split() > before=a.lines[i-1].split() > if (before[0]==after[0]): > if (i!=(lfile-1)): continue > else: > end=lfile-1 > myfun(before[0],start,end) > snp=snp+1 > else: > end=i-1 > myfun(before[0],start,end) > snp=snp+1 > start=i > if (i ==(lfile-1)): > myfun(after[0],start,start) > snp=snp+1 > > result.close() > > sincerely, phoebe > > Others have pointed out that you have too little memory for a 2gig data structure. If you're running on a 32bit system, chances are it won't matter how much memory you add, a process is limited to 4gb, and the OS typically takes about half of it, your code and other data takes some, and you don't have 2gig left. A 64 bit version of Python, running on a 64bit OS, might be able to "just work." Anyway, loading the whole file into a list is seldom the best answer, except for files under a meg or so. It's usually better to process the file in sequence. It looks like you're also making slices of that data, so they could potentially be pretty big as well. If you can be assured that you only need the current line and the previous two (for example), then you can use a list of just those three, and delete the oldest one, and add a new one to that list each time through the loop. Or, you can add some methods to that 'textfile' class that fetch a line by index. Brute force, you could pre-scan the file, and record all the file offsets for the lines you find, rather than storing the actual line. So you still have just as big a list, but it's a list of integers. Then when somebody calls your method, he passes an integer, and you return the particular line. A little caching for performance, and you're good to go. Anyway, if you organize it that way, you can code the rest of the module to not care whether the whole file is really in memory or not. BTW, you should derive all your classes from something. If nothing else, use object. class textfile(object): From aaron.hildebrandt at gmail.com Mon Jul 13 17:14:27 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Mon, 13 Jul 2009 14:14:27 -0700 (PDT) Subject: Pickling classes -- disappearing lists? Message-ID: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> I'm trying to pickle an instance of a class. It mostly works just fine -- I can save the pickle to a file, restore it from that file, and it's mostly okay. The problem is, some lists seem to disappear. For example (snipped and crunched from the code giving me trouble): --- class InitGame: value = False journal = [] game.InitGame() def Save(): global game import cPickle, gzip, os # Change some data game.journal.append("A value") game.value = True pickles = {'game': game} jar = gzip.open("pickefile", 'wb') cPickle.dump(pickles, jar, 2) jar.close() def Load(): global game import gzip, os, cPickle jar = gzip.open("picklefile", 'r') loaded = cPickle.load(jar) jar.close() game = loaded["game"] --- Now, if I save a pickle, then load it back in, I'll get an instance of InitGame called "game", and game.value will be true, but the list "journal" will be empty. Am I missing something obvious about the pickle spec? Am I doing something wrong? Or should I be hunting for a deeper bug in the code? I've noticed that pretty much anything that's a list isn't saving to the pickle (or loading from the pickle... it's hard to tell which). From aaron.hildebrandt at gmail.com Mon Jul 13 17:20:13 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Mon, 13 Jul 2009 14:20:13 -0700 (PDT) Subject: Memory error due to big input file References: Message-ID: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> > BTW, you should derive all your classes from something. ?If nothing > else, use object. > ? class textfile(object): Just out of curiousity... why is that? I've been coding in Python for a long time, and I never derive my base classes. What's the advantage to deriving them? From l.lensgraf at gmail.com Mon Jul 13 17:44:44 2009 From: l.lensgraf at gmail.com (DrLeif) Date: Mon, 13 Jul 2009 14:44:44 -0700 (PDT) Subject: PDF: finding a blank image Message-ID: I have about 6000 PDF files which have been produced using a scanner with more being produced each day. The PDF files contain old paper records which have been taking up space. The scanner is set to detect when there is information on the backside of the page (duplex scan). The problem of course is it's not the always reliable and we wind up with a number of PDF files containing blank pages. What I would like to do is have python detect a "blank" pages in a PDF file and remove it. Any suggestions? Thanks, DrL From clp2 at rebertia.com Mon Jul 13 17:50:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 14:50:23 -0700 Subject: Pickling classes -- disappearing lists? In-Reply-To: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> References: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> Message-ID: <50697b2c0907131450j5d3f9799l2e9c206b3c749f39@mail.gmail.com> On Mon, Jul 13, 2009 at 2:14 PM, Aaron Scott wrote: > I'm trying to pickle an instance of a class. It mostly works just fine > -- I can save the pickle to a file, restore it from that file, and > it's mostly okay. The problem is, some lists seem to disappear. For > example (snipped and crunched from the code giving me trouble): > > --- > > > class InitGame: > ? ? ? ?value = False > ? ? ? ?journal = [] > > > game.InitGame() That line doesn't make sense with the code you've given... > def Save(): > ? ? ? ?global game > ? ? ? ?import cPickle, gzip, os > > ? ? ? ?# Change some data > ? ? ? ?game.journal.append("A value") > ? ? ? ?game.value = True > > ? ? ? ?pickles = {'game': game} > ? ? ? ?jar = gzip.open("pickefile", 'wb') > ? ? ? ?cPickle.dump(pickles, jar, 2) > ? ? ? ?jar.close() > > > def Load(): > ? ? ? ?global game > ? ? ? ?import gzip, os, cPickle > ? ? ? ?jar = gzip.open("picklefile", 'r') > ? ? ? ?loaded = cPickle.load(jar) > ? ? ? ?jar.close() > ? ? ? ?game = loaded["game"] > > > --- > > Now, if I save a pickle, then load it back in, I'll get an instance of > InitGame called "game", and game.value will be true, but the list > "journal" will be empty. > > Am I missing something obvious about the pickle spec? Am I doing > something wrong? Or should I be hunting for a deeper bug in the code? Your class definition isn't right. It makes 'value' and 'journal' class variables (Java lingo: "static variables") as opposed to the instance variables they should be. Here's a corrected version: class InitGame(object): def __init__(self): #instance variables are created through self.foo assignments in __init__ self.value = False self.journal = [] Cheers, Chris -- http://blog.rebertia.com From vilya.harvey at gmail.com Mon Jul 13 17:51:01 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Mon, 13 Jul 2009 22:51:01 +0100 Subject: Memory error due to big input file In-Reply-To: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> References: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> Message-ID: <6aef848f0907131451h2ae1b83cs1eda1651e6db13@mail.gmail.com> 2009/7/13 Aaron Scott : >> BTW, you should derive all your classes from something. ?If nothing >> else, use object. >> ? class textfile(object): > > Just out of curiousity... why is that? I've been coding in Python for > a long time, and I never derive my base classes. What's the advantage > to deriving them? class Foo: uses the old object model. class Foo(object): uses the new object model. See http://docs.python.org/reference/datamodel.html (specifically section 3.3) for details of the differences. Vil. From clp2 at rebertia.com Mon Jul 13 18:02:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 15:02:37 -0700 Subject: Memory error due to big input file In-Reply-To: <6aef848f0907131451h2ae1b83cs1eda1651e6db13@mail.gmail.com> References: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> <6aef848f0907131451h2ae1b83cs1eda1651e6db13@mail.gmail.com> Message-ID: <50697b2c0907131502p2e262a22p4a6af5a31bfee677@mail.gmail.com> On Mon, Jul 13, 2009 at 2:51 PM, Vilya Harvey wrote: > 2009/7/13 Aaron Scott : >>> BTW, you should derive all your classes from something. ?If nothing >>> else, use object. >>> ? class textfile(object): >> >> Just out of curiousity... why is that? I've been coding in Python for >> a long time, and I never derive my base classes. What's the advantage >> to deriving them? > > ? ?class Foo: > > uses the old object model. > > ? ?class Foo(object): > > uses the new object model. > > See http://docs.python.org/reference/datamodel.html (specifically > section 3.3) for details of the differences. Note that Python 3.0 makes explicitly subclassing `object` unnecessary since it removes old-style classes; a class that doesn't explicitly subclass anything will implicitly subclass `object`. Cheers, Chris -- http://blog.rebertia.com From aaron.hildebrandt at gmail.com Mon Jul 13 19:00:18 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Mon, 13 Jul 2009 16:00:18 -0700 (PDT) Subject: Pickling classes -- disappearing lists? References: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> Message-ID: <60e9b0e3-25c7-4d01-a120-b15b33a06b57@m18g2000vbi.googlegroups.com> > Your class definition isn't right. It makes 'value' and 'journal' > class variables (Java lingo: "static variables") as opposed to the > instance variables they should be. Here's a corrected version: > Woah, thanks. I can't believe I made such a stupid mistake. It's not like I'm a newcomer to Python, either. I'm can't believe I never noticed what I was doing. No more 2am coding for me. Thanks, Aaron From db3l.net at gmail.com Mon Jul 13 19:05:05 2009 From: db3l.net at gmail.com (David Bolen) Date: Mon, 13 Jul 2009 19:05:05 -0400 Subject: PDF: finding a blank image References: Message-ID: DrLeif writes: > What I would like to do is have python detect a "blank" pages in a PDF > file and remove it. Any suggestions? The odds are good that even a blank page is being "rendered" within the PDF as having some small bits of data due to scanner resolution, imperfections on the page, etc.. So I suspect you won't be able to just look for a well-defined pattern in the resulting PDF or anything. Unless you're using OCR, the odds are good that the scanner is rendering the PDF as an embedded image. What I'd probably do is extract the image of the page, and then use image processing on it to try to identify blank pages. I haven't had the need to do this myself, and tool availability would depend on platform, but for example, I'd probably try ImageMagick's convert operation to turn the PDF into images (like PNGs). I think Gimp can also do a similar conversion, but you'd probably have to script it yourself. Once you have an image of a page, you could then use something like OpenCV to process the page (perhaps a morphology operation to remove small noise areas, then a threshold or non-zero counter to judge "blankness"), or probably just something like PIL depending on complexity of the processing needed. Once you identify a blank page, removing it could either be with pure Python (there have been other posts recently about PDF libraries) or with external tools (such as pdftk under Linux for example). -- David From Scott.Daniels at Acm.Org Mon Jul 13 19:22:23 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 13 Jul 2009 16:22:23 -0700 Subject: PDF: finding a blank image In-Reply-To: References: Message-ID: DrLeif wrote: > I have about 6000 PDF files which have been produced using a scanner > with more being produced each day. The PDF files contain old paper > records which have been taking up space. The scanner is set to > detect when there is information on the backside of the page (duplex > scan). The problem of course is it's not the always reliable and we > wind up with a number of PDF files containing blank pages. > > What I would like to do is have python detect a "blank" pages in a PDF > file and remove it. Any suggestions? I'd check into ReportLab's commercial product, it may well be easily capable of that. If no success, you might contact PJ at Groklaw, she has dealt with a _lot_ of PDFs (and knows people who deal with PDFs in bulk). --Scott David Daniels Scott.Daniels at Acm.Org From Brian.Mingus at colorado.edu Mon Jul 13 19:26:26 2009 From: Brian.Mingus at colorado.edu (Brian) Date: Mon, 13 Jul 2009 17:26:26 -0600 Subject: PDF: finding a blank image In-Reply-To: References: Message-ID: <9839a05c0907131626w77489e62raf1582a9ff429a8f@mail.gmail.com> Perhaps your blank pages have a characteristic size. Or perhaps if you trim them with `convert' (ImageMagick) there is nothing left. On Mon, Jul 13, 2009 at 3:44 PM, DrLeif wrote: > I have about 6000 PDF files which have been produced using a scanner > with more being produced each day. The PDF files contain old paper > records which have been taking up space. The scanner is set to > detect when there is information on the backside of the page (duplex > scan). The problem of course is it's not the always reliable and we > wind up with a number of PDF files containing blank pages. > > What I would like to do is have python detect a "blank" pages in a PDF > file and remove it. Any suggestions? > > > Thanks, > DrL > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bjorn.m.meyer at gmail.com Mon Jul 13 19:35:00 2009 From: bjorn.m.meyer at gmail.com (Bjorn Meyer) Date: Mon, 13 Jul 2009 17:35:00 -0600 Subject: multiprocessing and dictionaries In-Reply-To: References: Message-ID: <200907131735.00191.bjorn.m.meyer@gmail.com> On Monday 13 July 2009 13:12:18 Piet van Oostrum wrote: > >>>>> Bjorn Meyer (BM) wrote: > > > >BM> Here is what I have been using as a test. > >BM> This pretty much mimics what I am trying to do. > >BM> I put both threading and multiprocessing in the example which shows > >BM> the output that I am looking for. > > > >BM> #!/usr/bin/env python > > > >BM> import threading > >BM> from multiprocessing import Manager, Process > > > >BM> name = ('test1','test2','test3') > >BM> data1 = ('dat1','dat2','dat3') > >BM> data2 = ('datA','datB','datC') > > [snip] > > >BM> def multiprocess_test(name,data1,data2, mydict): > >BM> for nam in name: > >BM> for num in range(0,3): > >BM> mydict.setdefault(nam, []).append(data1[num]) > >BM> mydict.setdefault(nam, []).append(data2[num]) > >BM> print 'Multiprocess test dic:',mydict > > I guess what's happening is this: > > d.setdefault(nam, []) returns a list, initially an empty list ([]). This > list gets appended to. However, this list is a local list in the > multi-process_test Process, therefore the result is not reflected in the > original list inside the manager. Therefore all your updates get lost. > You will have to do operations directly on the dictionary itself, not on > any intermediary objects. Of course with the threading the situation is > different as all operations are local. > > This works: > > def multiprocess_test(name,data1,data2, mydict): > print name, data1, data2 > for nam in name: > for num in range(0,3): > mydict.setdefault(nam, []) > mydict[nam] += [data1[num]] > mydict[nam] += [data2[num]] > print 'Multiprocess test dic:',mydict > > If you have more than one process operating on the dictionary > simultaneously you have to beware of race conditions!! > -- > Piet van Oostrum > URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] > Private email: piet at vanoostrum.org Excellent. That works perfectly. Thank you for your response Piet. Bjorn From ben+python at benfinney.id.au Mon Jul 13 20:09:06 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 14 Jul 2009 10:09:06 +1000 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <87eiskktwd.fsf@benfinney.id.au> seldan24 writes: > I'm fairly new at Python so hopefully this question won't be too > awful. I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). You have been given a lot of advice so far about catching exceptions with the ?try ? except? syntax. But reading your request, I think perhaps you don't want to catch exceptions at all. Instead of catching *all* exceptions at a specific point in your code, and then having nothing to do but end the program (possibly after some pre-exit action like logging), I think you would be better served by installing a custom top-level exception handler. When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook. With a custom top-level exception handler in place, you should then restrict your use of ?try: ? except FooException: ?? to be as precise as possible, so that the ?try? block is as small as possible and the ?FooException? is as specific as possible. Any other exceptions that you don't specifically catch will then go through to your default handle-it-just-before-we-exit ?sys.excepthook? exception handler. -- \ ?Nature hath given men one tongue but two ears, that we may | `\ hear from others twice as much as we speak.? ?Epictetus, | _o__) _Fragments_ | Ben Finney From pavlovevidence at gmail.com Mon Jul 13 20:49:23 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 13 Jul 2009 17:49:23 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> On Jul 13, 12:31?pm, Piet van Oostrum wrote: > >>>>> seldan24 (s) wrote: > >s> Hello, > >s> I'm fairly new at Python so hopefully this question won't be too > >s> awful. ?I am writing some code that will FTP to a host, and want to > >s> catch any exception that may occur, take that and print it out > >s> (eventually put it into a log file and perform some alerting action). > >s> I've figured out two different ways to do this, and am wondering which > >s> is the best (i.e. cleanest, 'right' way to proceed). ?I'm also trying > >s> to understand exactly what occurs for each one. > >s> The first example: > >s> from ftplib import FTP > >s> try: > >s> ? ? ftp = FTP(ftp_host) > >s> ? ? ftp.login(ftp_user, ftp_pass) > >s> except Exception, err: > >s> ? ? print err > > I think you should restrict yourself to those exceptions that are > related to ftp. Do you want to catch an exception like a misspelling in > one of the variables? He quite reasonably could want that, such as if the program is designed to be run from a cron job, or in some other non-interactive way. Just because something is usually a bad idea doesn't mean it always is. Carl Banks From albert at spenarnc.xs4all.nl Mon Jul 13 22:28:06 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 14 Jul 2009 02:28:06 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: In article <7x8wj4uxnb.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: > >Anyway, Python's overloading of bool(...) is yet another misfeature >and although it's convenient, the "explicit is better than implicit" >principle indicates to avoid that sort of trick. Well, trading "tricks" (I would call it idiom) for explicitness, made some people (you know ..) hate Pascal. Einstein introduced the summation convention for indices that are used twice. Leaving out summation signs is absolutely hideous, but it has saved generations of physicists of loosing track (and their minds.) Also ... For me there is nothing more clear than a regular expression search that returns None if nothing matches. Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From albert at spenarnc.xs4all.nl Mon Jul 13 22:46:19 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 14 Jul 2009 02:46:19 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> <0260bf8d$0$20657$c3e8da3@news.astraweb.com> <7x8wj2agma.fsf@ruckus.brouhaha.com> Message-ID: In article <7x8wj2agma.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: >Steven D'Aprano writes: >> > but I don't accept that "somethingness" >> > vs. "nothingness" is the same distinction as truth vs falsehood. >> >> It's the distinction used by Python since the dawn of time. Python only >> grew a bool type a few versions back. > >That's true, part of the situation we have now is an artifact of that >history. > >> I'm not talking about the constants True and False (nouns), but about >> true and false values (adjectives). > >But, it seems to me, the constants True and False are the only values >to which the adjectives "true" and "false" should be applicable to. > >> >> > The idea that the "if" >> > statement selects between "somethingness" and "nothingness" rather than >> > between True and False is a bogus re-imagining of the traditional >> > function of an "if" statement >> >> There's nothing bogus about it. >> >> > and has been an endless source of bugs in Python code. >> I wonder why these "endless" bugs weren't important enough to be >> mentioned in the rationale to PEP 285: > >Because adding the bool type doesn't really fix those bugs. > >> describing `if x` as the "correct form" and calling scrapping such a >> feature as "crippling the language". > >Certainly, changing "if" would have broken an immense amount of code >and been a completely unworkable approach. We are using a fairly >mature language by now; it has a culture and history that carries >certain baggage, as one should expect. > >> > Look how much confusion it causes here in the newsgroup all the time. >> The only confusion is that you're causing me. Would you care to link to >> some? > >This current discussion (about bools) came from such confusion just a >few posts up in this very thread: > > From: upwestdon > Date: Fri, 3 Jul 2009 23:03:39 -0700 (PDT) > How about just: > > if not (self.higher and self.lower): > return self.higher or self.lower > >That test was designed to treat None as a boolean False, without >noticing that numeric 0 is also treated as False and could make the >test do the wrong thing. This is an extremely common type of error. > >> > could see some value to having a generic "is_empty" predicate >> We have that already. It's spelled __bool__ or __nonzero__ > >That's fine, but under the "explicit is better than implicit" >principle, it's preferable to call that predicate explicitly: >"if bool(x): ..." rather than "if x:". Also, after many years of Maybe I'm missing something here, but if self.higher contains 0, wouldn't bool(self.higher) evaluate to False? So how does this help? Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:25:52 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:25:52 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> Message-ID: On Mon, 13 Jul 2009 17:49:23 -0700, Carl Banks wrote: > On Jul 13, 12:31?pm, Piet van Oostrum wrote: >> >>>>> seldan24 (s) wrote: >> >s> Hello, >> >s> I'm fairly new at Python so hopefully this question won't be too s> >> >awful. ?I am writing some code that will FTP to a host, and want to s> >> >catch any exception that may occur, take that and print it out s> >> >(eventually put it into a log file and perform some alerting action). >> >s> I've figured out two different ways to do this, and am wondering >> >which s> is the best (i.e. cleanest, 'right' way to proceed). ?I'm >> >also trying s> to understand exactly what occurs for each one. s> The >> >first example: >> >s> from ftplib import FTP >> >s> try: >> >s> ? ? ftp = FTP(ftp_host) >> >s> ? ? ftp.login(ftp_user, ftp_pass) s> except Exception, err: >> >s> ? ? print err >> >> I think you should restrict yourself to those exceptions that are >> related to ftp. Do you want to catch an exception like a misspelling in >> one of the variables? > > He quite reasonably could want that, such as if the program is designed > to be run from a cron job, or in some other non-interactive way. Why is it okay for non-interactive programs to silently have incorrect behaviour? What's the point of a cron job that doesn't do what it is supposed to, because it has a bug which is silently swallowed? "I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn't crash is a horrible nightmare." http://www.pphsg.org/cdsmith/types.html -- Steven From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:27:30 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:27:30 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <87eiskktwd.fsf@benfinney.id.au> Message-ID: On Tue, 14 Jul 2009 10:09:06 +1000, Ben Finney wrote: > Instead of catching *all* exceptions at a specific point in your code, > and then having nothing to do but end the program (possibly after some > pre-exit action like logging), I think you would be better served by > installing a custom top-level exception handler. ... > Any other exceptions that you don't specifically catch will then go > through to your default handle-it-just-before-we-exit ?sys.excepthook? > exception handler. Isn't that risky though? Won't that potentially change the exception- handling behaviour of functions and classes he imports from other modules? -- Steven From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:45:02 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:45:02 GMT Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: On Mon, 13 Jul 2009 23:22:36 +1200, Lawrence D'Oliveiro wrote: [stupidity omitted] > Nope, still doesn't work... Are we supposed to interpret that post as Dumb Insolence or just Dumb? -- Steven From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:49:30 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:49:30 GMT Subject: Memory error due to big input file References: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> Message-ID: On Mon, 13 Jul 2009 14:20:13 -0700, Aaron Scott wrote: >> BTW, you should derive all your classes from something. ?If nothing >> else, use object. >> ? class textfile(object): > > Just out of curiousity... why is that? I've been coding in Python for a > long time, and I never derive my base classes. What's the advantage to > deriving them? "Old style" classes (those whose base classes aren't derived from anything) have a few disadvantages: (1) Properties don't work correctly: >>> class Parrot: # old-style class ... def __init__(self): ... self._x = 3 ... def _setter(self, value): ... self._x = value ... def _getter(self): ... print "Processing ..." ... return self._x + 1 ... x = property(_getter, _setter) ... >>> p = Parrot() >>> p.x Processing ... 4 >>> p.x = 2 >>> p.x 2 In general, anything that uses the descriptor protocol, not just property, will fail to work correctly with old-style classes. (2) Classes using multiple inheritance with diamond-shaped inheritance will be broken. (3) __slots__ is just an attribute. (4) super() doesn't work. And, depending on whether you consider this a disadvantage or an advantage: (5) Special methods like __len__ can be over-ridden on the instance, not just the class: >>> class K: ... def __len__(self): ... return 0 ... >>> k = K() >>> len(k) 0 >>> k.__len__ = lambda : 42 >>> len(k) 42 In their favour: (1) Less typing. (2) If you're not using descriptors, including property(), or multiple inheritance with diamond diagrams, they work fine. (3) They're (apparently) a tiny bit faster. -- Steven From Lily.Gao at autodesk.com Tue Jul 14 00:30:03 2009 From: Lily.Gao at autodesk.com (Lily Gao) Date: Mon, 13 Jul 2009 21:30:03 -0700 Subject: How to unbuffer Python's output Message-ID: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> Hi, All I am calling a python program in perl and use redirection, Like : `python x.py > 1.log 2>&1` When x.py crash, I get nothing from 1.log, and if I don?t use redirection, I can get useful log from the screen. How can I do to make x.py ?s output un-buffered when redirection log to files ,just exactly same with print to the screen? When I use perl?s $|=1 to unbuffer output, it take no effect. So I think it may be caused by python. Thanks! Thanks, Lily Gao(??) ACRD PSEB Catapult TD +86-21-38664379 -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Tue Jul 14 01:06:04 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 14 Jul 2009 02:06:04 -0300 Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: En Mon, 13 Jul 2009 10:11:09 -0300, denis escribi?: > Matt, how many words are you looking for, in how long a string ? > Were you able to time any( substr in long_string ) against re.compile > ( "|".join( list_items )) ? There is a known algorithm to solve specifically this problem (Aho-Corasick), a good implementation should perform better than R.E. (and better than the gen.expr. with the advantage of returning WHICH string matched) There is a C extension somewhere implementing Aho-Corasick. -- Gabriel Genellina From clp2 at rebertia.com Tue Jul 14 01:32:55 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 22:32:55 -0700 Subject: explode() In-Reply-To: References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: <50697b2c0907132232g40dce2c3o776a2f07ef28acf0@mail.gmail.com> On Mon, Jul 13, 2009 at 11:28 PM, Fred Atkinson wrote: > On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert > wrote: > >>On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >>> ? ? ? ?What is the Python equivalent of the PHP explode() function? >> >>some_string.split("separator") >> >>Cheers, >>Chris > > ? ? What would the equivalent of the PHP function implode be? "separator".join(a_list_of_strings) I would recommend you read the Python manual's section on string methods: http://docs.python.org/library/stdtypes.html#string-methods Cheers, Chris -- http://blog.rebertia.com From usernet at ilthio.net Tue Jul 14 01:41:40 2009 From: usernet at ilthio.net (Tim Harig) Date: Tue, 14 Jul 2009 05:41:40 GMT Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: On 2009-07-14, Fred Atkinson wrote: > The one thing I really dislike about Python over PHP is that > Python can usually only appear in the cgi directory (unless other > arragements are made with your hosting provider or if you reconfigure > Apache on your own server if you have your own). With PHP, I can put > them in any folder on my Web site without any problem. That is a server configuration and has nothing to do with Python directly. From sanu1267 at gmail.com Tue Jul 14 01:41:52 2009 From: sanu1267 at gmail.com (sanju ps) Date: Tue, 14 Jul 2009 11:11:52 +0530 Subject: python _binary_ code Message-ID: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> Hi Can anyone give me solution to create a python binary file (bytecode) other than pyc file .So my source code be secure.. I am working on ubuntu 9.04 with python2.6.. I Regards Sanju -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Tue Jul 14 02:00:00 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 23:00:00 -0700 Subject: python _binary_ code In-Reply-To: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> References: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> Message-ID: <50697b2c0907132300xd593feepbaf5a2be078c93f4@mail.gmail.com> On Mon, Jul 13, 2009 at 10:41 PM, sanju ps wrote: > Hi > > ?Can anyone give me solution to create a python binary file (bytecode) other > than pyc file .So my source code be secure.. I am working on ubuntu 9.04 > with python2.6.. I PyInstaller (http://www.pyinstaller.org/) will generate a self-contained executable, but it won't hide the bytecode (which can be decompiled w/ decompyle). It's generally accepted to be impossible (or at least more trouble than it's worth) to protect Python source. See http://stackoverflow.com/questions/261638/how-do-i-protect-python-code for excellent and more detailed answers. Cheers, Chris -- http://blog.rebertia.com From fatkinson at mishmash.com Tue Jul 14 02:28:32 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Mon, 13 Jul 2009 23:28:32 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert wrote: >On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >> ? ? ? ?What is the Python equivalent of the PHP explode() function? > >some_string.split("separator") > >Cheers, >Chris What would the equivalent of the PHP function implode be? Thanks, Fred From fatkinson at mishmash.com Tue Jul 14 02:36:26 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Mon, 13 Jul 2009 23:36:26 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> On Mon, 13 Jul 2009 22:32:55 -0700, Chris Rebert wrote: >On Mon, Jul 13, 2009 at 11:28 PM, Fred Atkinson wrote: >> On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert >> wrote: >> >>>On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >>>> ? ? ? ?What is the Python equivalent of the PHP explode() function? >>> >>>some_string.split("separator") >>> >>>Cheers, >>>Chris >> >> ? ? What would the equivalent of the PHP function implode be? > >"separator".join(a_list_of_strings) > >I would recommend you read the Python manual's section on string methods: >http://docs.python.org/library/stdtypes.html#string-methods > >Cheers, >Chris Chris, You always seem to be waiting when I post a question. Do you ever sleep? I wish the Python site was as well written as the PHP site. On the PHP site, I can look up a command and they show not only the docs on that command but a list of all other commands associated with it. Thanks. Python is a whole new animal to me. I'm taking a course in PHP and Python online right now. I've done PHP scripting before though I don't claim to be a whiz on it. But I'd barely heard of Python before I took this course. The one thing I really dislike about Python over PHP is that Python can usually only appear in the cgi directory (unless other arragements are made with your hosting provider or if you reconfigure Apache on your own server if you have your own). With PHP, I can put them in any folder on my Web site without any problem. Regards, Fred From lists at cheimes.de Tue Jul 14 02:50:16 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 14 Jul 2009 08:50:16 +0200 Subject: python _binary_ code In-Reply-To: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> References: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> Message-ID: sanju ps schrieb: > Hi > > Can anyone give me solution to create a python binary file (bytecode) other > than pyc file .So my source code be secure.. I am working on ubuntu 9.04 > with python2.6.. I It's impossible to secure your code if it runs on an untrusted computer. This is true for all programming languages but it's even easier to decompile code of byte interpreted languages like Python, Java or .NET. Microsoft has tried to protect its programs, the film industry has tried it with the movies and RIAA, too. All have failed. You won't have more luck, though. :) Christian From wuwei23 at gmail.com Tue Jul 14 02:54:11 2009 From: wuwei23 at gmail.com (alex23) Date: Mon, 13 Jul 2009 23:54:11 -0700 (PDT) Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: Fred Atkinson wrote: > ? ? ? ? I wish the Python site was as well written as the PHP site. On > the PHP site, I can look up a command and they show not only the docs > on that command but a list of all other commands associated with it. ? Hey Fred, My problem is the complete opposite, I wish I could as easily interrogate objects in PHP about their behaviour as I can Python :) In case you're not familiar with this, you can see what methods are attached to an object using the intepreter via dir() and see the associated docstring for them via help(). Try looking at 'dir(str)' for a list of methods that strings recognise, then 'help(str.join)' to see the help on join. This is such a handy ability to have that I miss this casual introspection in everything else in which I'm required to develop. Hope this helps. From amrita at iisermohali.ac.in Tue Jul 14 02:57:29 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Tue, 14 Jul 2009 12:27:29 +0530 (IST) Subject: copy a file Message-ID: <14186.210.212.36.65.1247554649.squirrel@www.iisermohali.ac.in> Dear all, Can anyone tell me that suppose i want to copy few lines from one text file to another then how can i do that.Looking forward for soon reply. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From ben+python at benfinney.id.au Tue Jul 14 02:59:07 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 14 Jul 2009 16:59:07 +1000 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <87eiskktwd.fsf@benfinney.id.au> Message-ID: <873a8zlphg.fsf@benfinney.id.au> Steven D'Aprano writes: > Isn't that risky though? Won't that potentially change the exception- > handling behaviour of functions and classes he imports from other > modules? No, any existing ?except? clause will be unaffected by re-binding ?sys.excepthook?. As I understand the documentation, that function is called only if the exception is uncaught by anything else. >>> 1 / 0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: integer division or modulo by zero >>> import sys >>> def reverse_handler(exc_type, exc_instance, exc_traceback): ... print exc_type.__name__[::-1], str(exc_instance)[::-1] ... >>> sys.excepthook = reverse_handler >>> try: ... 1 / 0 ... except ZeroDivisionError: ... print "You created a black hole." ... You created a black hole. >>> 1 / 0 rorrEnoisiviDoreZ orez yb oludom ro noisivid regetni In other words, it is the function that (unless re-bound) prints the traceback we're all familiar with when an exception is uncaught. It is exposed via the name ?sys.excepthook? precisely for the purpose of changing the default handler for uncaught exceptions -- \ ?I have yet to see any problem, however complicated, which, | `\ when you looked at it in the right way, did not become still | _o__) more complicated.? ?Paul Anderson | Ben Finney From hackingkk at gmail.com Tue Jul 14 03:07:13 2009 From: hackingkk at gmail.com (Krishnakant) Date: Tue, 14 Jul 2009 12:37:13 +0530 Subject: copy a file In-Reply-To: <14186.210.212.36.65.1247554649.squirrel@www.iisermohali.ac.in> References: <14186.210.212.36.65.1247554649.squirrel@www.iisermohali.ac.in> Message-ID: <1247555233.3329.18.camel@krishna-laptop> On Tue, 2009-07-14 at 12:27 +0530, amrita at iisermohali.ac.in wrote: > Dear all, > > Can anyone tell me that suppose i want to copy few lines from one text > file to another then how can i do that.Looking forward for soon reply. > very simple. open one file and open the source file. seek till to the point where u want to start copying and loop till the end with readline function. then write it to the destination file. To find the starting point, keep looping through the content and come out of that loop when you find a match to the word you are looking for. happy hacking. Krishnakant. From pavlovevidence at gmail.com Tue Jul 14 04:30:48 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 01:30:48 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> Message-ID: <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> On Jul 13, 8:25?pm, Steven D'Aprano wrote: > On Mon, 13 Jul 2009 17:49:23 -0700, Carl Banks wrote: > > On Jul 13, 12:31?pm, Piet van Oostrum wrote: > >> >>>>> seldan24 (s) wrote: > >> >s> Hello, > >> >s> I'm fairly new at Python so hopefully this question won't be too s> > >> >awful. ?I am writing some code that will FTP to a host, and want to s> > >> >catch any exception that may occur, take that and print it out s> > >> >(eventually put it into a log file and perform some alerting action). > >> >s> I've figured out two different ways to do this, and am wondering > >> >which s> is the best (i.e. cleanest, 'right' way to proceed). ?I'm > >> >also trying s> to understand exactly what occurs for each one. s> The > >> >first example: > >> >s> from ftplib import FTP > >> >s> try: > >> >s> ? ? ftp = FTP(ftp_host) > >> >s> ? ? ftp.login(ftp_user, ftp_pass) s> except Exception, err: > >> >s> ? ? print err > > >> I think you should restrict yourself to those exceptions that are > >> related to ftp. Do you want to catch an exception like a misspelling in > >> one of the variables? > > > He quite reasonably could want that, such as if the program is designed > > to be run from a cron job, or in some other non-interactive way. > > Why is it okay for non-interactive programs to silently have incorrect > behaviour? > > What's the point of a cron job that doesn't do what it is supposed to, > because it has a bug which is silently swallowed? Seriously, do you *ever* take more than 2 seconds to consider whether you might be missing something obvious before following up with these indignant knee-jerk responses? I never said a thing about swallowing errors. I was talking about catching exceptions, whence you might do all kinds of things besides swallowing (like logging). I was pointing out there are reasons why you might want to catch variable misspelling errors when you're running non-interactively (like for instance, to log the error somewhere). Or would you rather let all unexpected exceptions print to standard error, which is often a black hole in non-interactive sitations? Carl Banks From nelson1977 at gmail.com Tue Jul 14 04:34:10 2009 From: nelson1977 at gmail.com (nelson -) Date: Tue, 14 Jul 2009 10:34:10 +0200 Subject: Remote audio recording in a multiuser environment Message-ID: Hi all! I'm developing an application that will be used in a school. It will allow client connected over ssh to liste to a multimedia file on the server. The server componente will record the audio taken from a specified client microphone, or it would pair two client, allowing them to communicate each other registering the conversation. I think to use pygst to record the microphone, but i can' figure out how to register remote sound and how to create the channel between two client. Do you think i have to use twisted (i don't know this framework at all), or there is a simple solution? I will surely use wxpython for the UI on the client and on the server. Thank for any advice, nelson -- Consulenze Linux e Windows http://nelsonenterprise.net From tedwards at it.dcs.ch Tue Jul 14 05:11:13 2009 From: tedwards at it.dcs.ch (Tim Edwards) Date: Tue, 14 Jul 2009 11:11:13 +0200 Subject: 'unable to execute cc: No such file or directory' with distutils on Solaris Message-ID: <4A5C4BB1.4050204@it.dcs.ch> Hi, I'm trying to compile http://sourceforge.net/projects/astlib/ 0.17.1 on a Solaris 10 SPARC system. This python module uses distutils.ccompiler. The problem seems to be that although I have gcc installed in /usr/sfw/bin/gcc it keeps trying to execute the command 'cc', which doesn't work: cc -c actread.c -o actread.o unable to execute cc: No such file or directory error: command 'cc' failed with exit status 1 I've tried modifying the setup.py file to specify "cc.compiler = '/usr/sfw/bin/gcc'" and setting the $CC envrionment var. But I get the same error everytime. Can someone suggest how to change where distutils is looking for the compiler? Thanks Tim Edwards From rileycrane at gmail.com Tue Jul 14 05:12:15 2009 From: rileycrane at gmail.com (R C) Date: Tue, 14 Jul 2009 02:12:15 -0700 (PDT) Subject: MySQLdb + SSH Tunnel References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: Got it working. Thanks for your help 1) login to B 2) setup a tunnel in the shell machine-B> ssh -L B_ip_address:B_port:C_ip_address:C_port user at C_ip_address for example: machine-B has ip 1.1.1.1 machine-C has ip 2.2.2.2 then I would type: machine-B> ssh -L 1.1.1.1:3307:2.2.2.2:3306 user at 2.2.2.2 now the code that is running on machine-A would use MySQLdb in the following way import MySQLdb connection = MySQLdb.connect (user='myname',passwd='mypass',db='mydb',host='1.1.1.1',port=3307) NOTE: the port is an integer, NOT a string On Jul 12, 9:18?pm, Riley Crane wrote: > OVERVIEW: > I am running a script on one machine that connects to a MySQL database > on another machine that is outside of our university's domain. > According to the administrator, network policies do not allow the > compute nodes to access machines outside of our university's domain. > > COMPUTERS: > A = compute node within university (I do not have shell access) > B = 2nd machine within university that does not block outside > connections (I have root access) > C = machine outside of university (I have root access) > mysqldb on A cannot connect to C ....but..... > mysqldb on A can connect to B > > WHAT I WOULD LIKE TO DO: > Is it possible to set something up where A talks to a port on B, and > that port is actually nothing more than 3306 on C? ?Can I do this with > an SSH tunnel? > > Can anyone please give precise instructions? From vodkalove at gmail.com Tue Jul 14 05:14:19 2009 From: vodkalove at gmail.com (Riley Crane) Date: Tue, 14 Jul 2009 02:14:19 -0700 (PDT) Subject: MySQLdb + SSH Tunnel References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: <03687615-f8b2-427f-af2d-4cfcb23604f8@f33g2000vbm.googlegroups.com> Got it working. Thanks for your help! 1) login to B 2) setup a tunnel in the shell machine-B> ssh -L B_ip_address:B_port:C_ip_address:C_port user at C_ip_address for example: machine-B has ip 1.1.1.1 machine-C has ip 2.2.2.2 then I would type: machine-B> ssh -L 1.1.1.1:3307:2.2.2.2:3306 user at 2.2.2.2 now the code that is running on machine-A would use MySQLdb in the following way import MySQLdb connection = MySQLdb.connect (user='myname',passwd='mypass',db='mydb',host='1.1.1.1',port=3307) NOTE: the port is an integer, NOT a string On Jul 12, 9:18?pm, Riley Crane wrote: > OVERVIEW: > I am running a script on one machine that connects to a MySQL database > on another machine that is outside of our university's domain. > According to the administrator, network policies do not allow the > compute nodes to access machines outside of our university's domain. > > COMPUTERS: > A = compute node within university (I do not have shell access) > B = 2nd machine within university that does not block outside > connections (I have root access) > C = machine outside of university (I have root access) > mysqldb on A cannot connect to C ....but..... > mysqldb on A can connect to B > > WHAT I WOULD LIKE TO DO: > Is it possible to set something up where A talks to a port on B, and > that port is actually nothing more than 3306 on C? ?Can I do this with > an SSH tunnel? > > Can anyone please give precise instructions? From steven at REMOVE.THIS.cybersource.com.au Tue Jul 14 05:14:52 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 09:14:52 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 01:30:48 -0700, Carl Banks wrote: > Seriously, do you *ever* take more than 2 seconds to consider whether > you might be missing something obvious before following up with these > indignant knee-jerk responses? Obviously not. [...] > Or would you rather let all unexpected exceptions print to standard > error, which is often a black hole in non-interactive sitations? Fair point. Of course you're right. -- Steven From hartley79 at gmail.com Tue Jul 14 05:22:24 2009 From: hartley79 at gmail.com (hartley) Date: Tue, 14 Jul 2009 02:22:24 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> Message-ID: <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> > > I'm very new at wrapping Python/C, and I have run into some problems. > > > I have one python module that provides me with a list (provideBuffer > > in provideBuff.py): > > > ?Py_Initialize(); > > ? ? ? ? pName = PyString_FromString("provideBuff"); > > ? ? ? ? pModule = PyImport_Import(pName); > > > ? ? ? ? pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > pValue is now a PyList - i've even verified this with: > > > int a = PyList_Check(pValue); > > ? ? ? ? printf("%d\n", a); > > > However, I want to send this PyList to another python module, > > Please explain "send" ... do you mean the C equivalent of the Python > statement C_embedding.buff = the_pylist ? > > BTW C-embedding would trigger a syntax error in Python source; best to > avoid ... I'm sorry I'm not using the proper terms when trying to describe this - i've never really learnt those terms, I guess i should do that one day. Imagine that the python function C-embedding.buff looks like this: def buff(a): if isinstance(a,list): print "success" I want to send, pass, call, whatever a list argument from the C code onto this function. As simple as this - how can you call a function in python from C, and providing a python list as an argument? As I wrote earlier - I already have a PyList, called pValue. But I have not been able to use this as an argument for C-embedding.buff using the code I described last time. > > but I > > don't know how to do this. Initially I though I could just do like > > above, only swapping NULL with pValue, but that is not working. > > > pName2 = PyString_FromString("C-embedding"); > > pModule2 = PyImport_Import(pName2); > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > Get?? Do you want Set? Is buff a Python function? Or is it the > destination of the "sending"? Any good reason for not checking the > return value for an error? [Rhetorical question; answer == "No"] > > > pValue2 = PyObject_CallObject(pFunc2,pValue); > > CallObject?? You used this before because you had a function and > wanted to call it because it returned you a value .... now you want to > do one of (in Python terms) > > value = amodule.anattr > value = getattr(amodule, "anattr") > > or > > amodule.anattr = value > setattr(amodule, "anattr", value) > > > pValue2 is now False! > > False?? Do you mean the C NULL? > > > So i guess i cannot pass pValue as an argument > > to PyObject_CallObject when i want to pass an python list as an > > argument. But how must a go about to make this work? > > It's mainly just a matter of (1) knowing what you want to do (2) > picking the API that does what you want (3) checking the returned > value for error after every call. > > HTH, > John From clp2 at rebertia.com Tue Jul 14 05:25:15 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 02:25:15 -0700 Subject: How to unbuffer Python's output In-Reply-To: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> References: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> Message-ID: <50697b2c0907140225s59df742fpdfb2673d837e4f3d@mail.gmail.com> 2009/7/13 Lily Gao : > Hi, All > > I am calling a python program in perl and use redirection, > > Like : > > `python x.py > 1.log 2>&1` > > When x.py crash, I get nothing from 1.log, and if I don?t use redirection, I > can get useful log from the screen. > > How can I do to make x.py ?s? output un-buffered when redirection log to > files ,just exactly same with print to the screen? Use the -u flag to python: ?u Force stdin, stdout and stderr to be totally unbuffered. python -u x.py > 1.log 2>&1 Cheers, Chris -- http://blog.rebertia.com From amrita at iisermohali.ac.in Tue Jul 14 05:43:17 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Tue, 14 Jul 2009 15:13:17 +0530 (IST) Subject: copy a few lines Message-ID: <23461.210.212.36.65.1247564597.squirrel@www.iisermohali.ac.in> Dear all, I want to know if i want to copy one paragraph from one text file to another then what command i have to use in python.Looking forwaard for soon reply. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From clp2 at rebertia.com Tue Jul 14 05:50:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 02:50:13 -0700 Subject: copy a few lines In-Reply-To: <23461.210.212.36.65.1247564597.squirrel@www.iisermohali.ac.in> References: <23461.210.212.36.65.1247564597.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907140250lb2bdc63y4872c522cf216953@mail.gmail.com> On Tue, Jul 14, 2009 at 2:43 AM, wrote: > > > Dear all, > > I want to know if i want to copy one paragraph from one text file to > another then what command i have to use in python.Looking forwaard for > soon reply. Please don't double-post. It wastes everyone's time, especially when (a) your previous post was less than 3 hours ago, and (b) your previous post got a response. If you're refining your question, it's best to just reply to your own prior post. Also, you're going to need to define what precisely qualifies as a "paragraph" for your purposes. - Chris -- http://blog.rebertia.com From jon at peirce.org.uk Tue Jul 14 06:28:19 2009 From: jon at peirce.org.uk (JonPeirce) Date: Tue, 14 Jul 2009 03:28:19 -0700 (PDT) Subject: renice In-Reply-To: <1193438701.979110.114360@v3g2000hsg.googlegroups.com> References: <13422771.post@talk.nabble.com> <1193417206.734440.268840@o38g2000hse.googlegroups.com> <1193438701.979110.114360@v3g2000hsg.googlegroups.com> Message-ID: <24477000.post@talk.nabble.com> Note that with os.nice you can only *decrease* priority. For increasing priority you need to run; os.system("sudo renice -n %s %s" % (new_nice, os.getpid())) or simply set the nice level when you run python in the first place (you do also need sudo for this): sudo nice -n -15 python myScript.py Jon -- View this message in context: http://www.nabble.com/renice-tp13422771p24477000.html Sent from the Python - python-list mailing list archive at Nabble.com. From hartley79 at gmail.com Tue Jul 14 06:32:08 2009 From: hartley79 at gmail.com (hartley) Date: Tue, 14 Jul 2009 03:32:08 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> Message-ID: <3d3d9f7e-5c57-414b-9363-f1ebfca0f9e2@r25g2000vbn.googlegroups.com> On Jul 13, 6:35?pm, John Machin wrote: > On Jul 14, 1:47?am, hartley wrote: > > > > > I'm very new at wrapping Python/C, and I have run into some problems. > > > I have one python module that provides me with a list (provideBuffer > > in provideBuff.py): > > > ?Py_Initialize(); > > ? ? ? ? pName = PyString_FromString("provideBuff"); > > ? ? ? ? pModule = PyImport_Import(pName); > > > ? ? ? ? pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > pValue is now a PyList - i've even verified this with: > > > int a = PyList_Check(pValue); > > ? ? ? ? printf("%d\n", a); > > > However, I want to send this PyList to another python module, > > Please explain "send" ... do you mean the C equivalent of the Python > statement C_embedding.buff = the_pylist ? > > BTW C-embedding would trigger a syntax error in Python source; best to > avoid ... > > > but I > > don't know how to do this. Initially I though I could just do like > > above, only swapping NULL with pValue, but that is not working. > > > pName2 = PyString_FromString("C-embedding"); > > pModule2 = PyImport_Import(pName2); > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > Get?? Do you want Set? Is buff a Python function? Or is it the > destination of the "sending"? Any good reason for not checking the > return value for an error? [Rhetorical question; answer == "No"] > > > pValue2 = PyObject_CallObject(pFunc2,pValue); > > CallObject?? You used this before because you had a function and > wanted to call it because it returned you a value .... now you want to > do one of (in Python terms) > > value = amodule.anattr > value = getattr(amodule, "anattr") > > or > > amodule.anattr = value > setattr(amodule, "anattr", value) > > > pValue2 is now False! > > False?? Do you mean the C NULL? > > > So i guess i cannot pass pValue as an argument > > to PyObject_CallObject when i want to pass an python list as an > > argument. But how must a go about to make this work? > > It's mainly just a matter of (1) knowing what you want to do (2) > picking the API that does what you want (3) checking the returned > value for error after every call. > > HTH, > John I tried to keep this simple, but I realise that since I'm not using the terminology correctly, it may be hard understand what I mean. I'll do something even simpler: Say you have the function: C-embedding.buff def buff(s): if isinstance(s,list): return s how can i call (use,invoke,?? - sorry, about the terminology again) this function from C so that I call this function with an argument that is (accepted as) a python list? foo.bar def bar(): pName = PyString_FromString("foo"); pModule = PyImport_Import(pName); pFunc = PyObject_GetAttrString(pModule,"bar"); pValue = PyObject_CallObject(pFunc,NULL); From dana_at_work at yahoo.com Tue Jul 14 07:32:35 2009 From: dana_at_work at yahoo.com (dana) Date: Tue, 14 Jul 2009 04:32:35 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: <9e63df1c-9757-4e94-98a0-f49b383a3ad6@h30g2000vbr.googlegroups.com> On Jul 10, 12:15?pm, "M.-A. Lemburg" wrote: > If you're looking for a stable and maintained ODBC for Python, > have a look at our mxODBC extension or mxODBC Connect package: > http://www.egenix.com/products/python/mxODBC/http://www.egenix.com/products/python/mxODBCConnect/ I'm looking for a free module. Is yours a commercial product? Thanks. Dana From torf at torfbold.com Tue Jul 14 07:38:45 2009 From: torf at torfbold.com (Florian Brucker) Date: Tue, 14 Jul 2009 13:38:45 +0200 Subject: Efficient binary search tree stored in a flat array? In-Reply-To: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: <4a5c5e7f$0$30235$9b4e6d93@newsspool1.arcor-online.net> Douglas Alan wrote: > Thank you. My question wasn't intended to be Python specific, though. > I am just curious for purely academic reasons about whether there is > such an algorithm. All the sources I've skimmed only seem to the > answer the question via omission. Which is kind of strange, since it > seems to me like an obvious question to ask. IIRC comp.programming would be the place to ask such questions. HTH, Florian From __peter__ at web.de Tue Jul 14 07:45:47 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Jul 2009 13:45:47 +0200 Subject: Tkinter / Entry widget problem References: Message-ID: Andras Szabo wrote: > Hello. I searched the archives but couldn't find a solution to a > problem related to the Entry widget in Tkinter. > > When creating a pop-up window in an app, which contains an Entry > widget, I want this widget to contain some default string, to have all > this default string selected (as if the user had manually selected > everything), and to have the focus transferred to this widget. > > (The idea is then that if the window pops up, the user won't have to > click or press Tab any more before being able to type what is needed > in the textbox, overwriting what is written there already.) > > I thought this might be the way to go: > > entrybox=Entry(toplevel_parent_window) > entrybox.insert(0,"Some default string") > entrybox.select_range(0,END) > entrybox.focus_set() > entrybox.pack() > > But it doesn't seem to work - the focus is not transferred to the > Entry widget, and the text does not appear to be selected (even though > after this entrybox.selection_present() returns True). > > What am I doing wrong? Nothing, I would think. Can you post a minimal runnable example? Peter From ldo at geek-central.gen.new_zealand Tue Jul 14 07:48:57 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 14 Jul 2009 23:48:57 +1200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: In message <93f6a517-63d8-4c80- bf19-4614b70999db at m7g2000prd.googlegroups.com>, Carl Banks wrote: > Or would you rather let all unexpected exceptions print to standard > error, which is often a black hole in non-interactive sitations? Since when? Cron, for example, collects standard error and mails it to you. From ldo at geek-central.gen.new_zealand Tue Jul 14 07:50:34 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 14 Jul 2009 23:50:34 +1200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: In message , seldan24 wrote: > For this particular script, all exceptions are fatal > and I would want them to be. I just wanted a way to catch them and > log them prior to program termination. You don't need to. They will be written to standard error anyway. From ldo at geek-central.gen.new_zealand Tue Jul 14 07:52:40 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 14 Jul 2009 23:52:40 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , Steven D'Aprano wrote: > Are we supposed to interpret that post as Dumb Insolence or just Dumb? "Insolence" indeed ... another wanker to plonk, I think. From piet at cs.uu.nl Tue Jul 14 08:10:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 14:10:20 +0200 Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: >>>>> Douglas Alan (DA) wrote: >DA> On Jul 13, 3:57?pm, a... at pythoncraft.com (Aahz) wrote: >>> Still, unless your list is large (more than thousands of elements), >>> that's the way you should go. ?See the bisect module. ?Thing is, the >>> speed difference between C and Python means the constant for insertion >>> and deletion is very very small relative to bytecode speed. ?Keep in >>> mind that Python's object/binding model means that you're shuffling >>> pointers in the list rather than items. >DA> Thank you. My question wasn't intended to be Python specific, though. >DA> I am just curious for purely academic reasons about whether there is >DA> such an algorithm. All the sources I've skimmed only seem to the >DA> answer the question via omission. Which is kind of strange, since it >DA> seems to me like an obvious question to ask. Of course you can take any BST algorithm and replace pointers by indices in the array and allocate new elements in the array. But then you need array elements to contain the indices for the children explicitely. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From sjmachin at lexicon.net Tue Jul 14 08:21:30 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 05:21:30 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> Message-ID: On Jul 14, 7:22?pm, hartley wrote: > > > I'm very new at wrapping Python/C, and I have run into some problems. [snip] > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > > pValue is now a PyList - i've even verified this with: > > > > int a = PyList_Check(pValue); > > > ? ? ? ? printf("%d\n", a); > > > > However, I want to send this PyList to another python module, > > > Please explain "send" ... do you mean the C equivalent of the Python > > statement C_embedding.buff = the_pylist ? > > > BTW C-embedding would trigger a syntax error in Python source; best to > > avoid ... > > I'm sorry I'm not using the proper terms when trying to describe this > - i've never really learnt those terms, I guess i should do that one > day. Today would be a good day to start :-) > > Imagine that the python function C-embedding.buff looks like this: > > def buff(a): > ? ? if isinstance(a,list): > ? ? ? ? print "success" > > I want to send, pass, call, whatever a list argument from the C code > onto this function. As simple as this - how can you call a function in > python from C, and providing a python list as an argument? > > As I wrote earlier - I already have a PyList, called pValue. But I > have not been able to use this as an argument for C-embedding.buff > using the code I described last time. > > > > but I > > > don't know how to do this. Initially I though I could just do like > > > above, only swapping NULL with pValue, but that is not working. > > > > pName2 = PyString_FromString("C-embedding"); > > > pModule2 = PyImport_Import(pName2); > > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > Any good reason for not checking the > > return value for an error? [Rhetorical question; answer == "No"] > > > > pValue2 = PyObject_CallObject(pFunc2,pValue); Your problem is here, in the second arg of PyObject_CallObject. It should be either NULL if no args are being supplied, or a tuple containing the args if 1 or more args are being supplied. pValue is the arg itself; you need to wrap it in a tuple. See http://docs.python.org/c-api/object.html#PyObject_CallObject Actually you might like to use this instead (no tuple hassles): http://docs.python.org/c-api/object.html#PyObject_CallFunctionObjArgs What are you using as a source of information? Have you read this: http://docs.python.org/extending/embedding.html#pure-embedding ? It contains a very thorough example of calling a Python function from C, with all the reference-counting and error-checking stuff and it's quite runnable -- I've just now compiled it and run it (1) as-is (2) changed to pass a list instead of multiple ints (3) changed again to emulate you trying to pass the list directly instead of wrapped in a tuple ... here's the result: TypeError: argument list must be a tuple Call failed Oh by the way, you can get away with C-embedding as a module name if you are importing it from C, but importing it from Python [so that you could test it independently of your C program] presents a difficulty (syntax error) ... better to change it to c_embedding. > > It's mainly just a matter of (1) knowing what you want to do (2) > > picking the API that does what you want (3) checking the returned > > value for error after every call. HTH, John From candide at free.invalid Tue Jul 14 08:37:24 2009 From: candide at free.invalid (candide) Date: Tue, 14 Jul 2009 14:37:24 +0200 Subject: Python code for testing well parenthesized expression Message-ID: <4a5c7c04$0$16808$426a34cc@news.free.fr> Hi, I'm trying to implement in Python a function testing if an expression is well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. My code follows at the end. If you have a better algorithm or a better Python code (I'm a beginner in the Python world), don't hesitate ... #---------------------------------- # The obvious iterative version def i(s): op = 0 # op : open parenthesis for k in range(len(s)): op += (s[k] == '(') - (s[k] == ')') if op < 0: break return op # Recursive implementation of the preceding version def r(s,op=0): # op : open parenthesis if len(s)==0: return op elif s[0]=='(': return r(s[1:],op+1) elif s[0]==')': if op==0: return -1 else : return r(s[1:],op-1) else : return r(s[1:],op) # "functional" style version def f(s): if (len(s)!=0 and s[0]=='('): z=f(s[1:]) if len(z)!=0: return f(z[1:]) else: return None elif len(s)==0 or s[0] == ')': return s else: return f(s[1:]) def test(t,f): for z in t: print (z,f(z)==0) # True True True True True False False t=["zx4er(1(er(Yy)ol)ol)ik", "x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)", "a(ty(y(y(bn)))lokl)kl", "xc(er(tgy(rf(yh)()uj)ki))", "e", "rf(tgt)juj)jkik(jun)", "zx(4er(1(er(Yy)ol)ol)ik"] test(t,i) print test(t,r) print def test(t): for s in t: print (s,f(s)=="") test(t) #------------------------------------------- and the corresponding output : ('zx4er(1(er(Yy)ol)ol)ik', True) ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True) ('a(ty(y(y(bn)))lokl)kl', True) ('xc(er(tgy(rf(yh)()uj)ki))', True) ('e', True) ('rf(tgt)juj)jkik(jun)', False) ('zx(4er(1(er(Yy)ol)ol)ik', False) ('zx4er(1(er(Yy)ol)ol)ik', True) ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True) ('a(ty(y(y(bn)))lokl)kl', True) ('xc(er(tgy(rf(yh)()uj)ki))', True) ('e', True) ('rf(tgt)juj)jkik(jun)', False) ('zx(4er(1(er(Yy)ol)ol)ik', False) ('zx4er(1(er(Yy)ol)ol)ik', True) ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True) ('a(ty(y(y(bn)))lokl)kl', True) ('xc(er(tgy(rf(yh)()uj)ki))', True) ('e', True) ('rf(tgt)juj)jkik(jun)', False) ('zx(4er(1(er(Yy)ol)ol)ik', False) From jeremy+complangpython at jeremysanders.net Tue Jul 14 08:57:21 2009 From: jeremy+complangpython at jeremysanders.net (Jeremy Sanders) Date: Tue, 14 Jul 2009 13:57:21 +0100 Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: candide wrote: > I'm trying to implement in Python a function testing if an expression is > well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" > is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. > > My code follows at the end. > > If you have a better algorithm or a better Python code (I'm a beginner in > the Python world), don't hesitate ... Don't you want to just test that the number of "("s equals the number of ")"s or am I missing the point? >>> a='aAAA(bbb(cc)))' >>> a.count('(') == a.count(')') Jeremy -- Jeremy Sanders http://www.jeremysanders.net/ From adrian.dziubek at gmail.com Tue Jul 14 08:59:51 2009 From: adrian.dziubek at gmail.com (Adrian Dziubek) Date: Tue, 14 Jul 2009 05:59:51 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Strings are immutable, so your method of slicing one letter at time will be building lots of them. That shouldn't hurt you here, but it will when you hit a bigger problem. In the i() there should be "return op == 0" on the end. def well(expr): mapping = {'(':1, ')':-1} count = 0 for s in expr: if s in mapping: count += mapping[s] if s < 0: return False return count == 0 def test_well(): examples = [ ('zx4er(1(er(Yy)ol)ol)ik', True), ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True), ('a(ty(y(y(bn)))lokl)kl', True), ('xc(er(tgy(rf(yh)()uj)ki))', True), ('e', True), ('rf(tgt)juj)jkik(jun)', False), ('zx(4er(1(er(Yy)ol)ol)ik', False), ] for expr, expected in examples: assert well(expr) == expected From adrian.dziubek at gmail.com Tue Jul 14 09:04:06 2009 From: adrian.dziubek at gmail.com (Adrian Dziubek) Date: Tue, 14 Jul 2009 06:04:06 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: <89cdf891-04e2-4c21-9043-cfdf91dd136a@j9g2000vbp.googlegroups.com> > Don't you want to just test that the number of "("s equals the number of > ")"s or am I missing the point? I had this idea too, but there is additional requirement that any beginning must have greater or equal number of '(' than ')'. -- Adrian From martin.hellwig at dcuktec.org Tue Jul 14 09:05:38 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 14 Jul 2009 14:05:38 +0100 Subject: Python code for testing well parenthesized expression In-Reply-To: <4a5c7c04$0$16808$426a34cc@news.free.fr> References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: candide wrote: To add to your implementations; a readable version: +++file parantheses.py+++ """Parentheses Module Test""" def parentheses_are_paired(input_string): "Check if 'input_string' contains paired parentheses, if so return True." parenthesis_count = 0 parenthesis_open = '(' parenthesis_close = ')' for character in input_string: if character == parenthesis_open: parenthesis_count += 1 elif character == parenthesis_close: parenthesis_count -= 1 if parenthesis_count == 0: return(True) else: if parenthesis_count < 0: error_text = 'More closing parantheses than opening ones' elif parenthesis_count > 0: error_text = 'More opening parantheses than closing ones' raise(ValueError(error_text)) if __name__ == '__main__': TEST_TRUE = 'zx4er(1(er(Yy)ol)ol)ik' TEST_ERROR = 'zx(4er(1(er(Yy)ol)ol)ik' print(parentheses_are_paired(TEST_TRUE)) try: print(parentheses_are_paired(TEST_ERROR)) except(ValueError): print(False) ---file parantheses.py--- -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From jeremy+complangpython at jeremysanders.net Tue Jul 14 09:06:42 2009 From: jeremy+complangpython at jeremysanders.net (Jeremy Sanders) Date: Tue, 14 Jul 2009 14:06:42 +0100 Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> <7c3dvuF25o0t6U1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > Yep, you are: > > "())))(((" > > is certainly not "well parenthized". Thanks for that! -- Jeremy Sanders http://www.jeremysanders.net/ From deets at nospam.web.de Tue Jul 14 09:07:20 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 14 Jul 2009 15:07:20 +0200 Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: <7c3dvuF25o0t6U1@mid.uni-berlin.de> Jeremy Sanders wrote: > candide wrote: > >> I'm trying to implement in Python a function testing if an expression is >> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" >> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. >> >> My code follows at the end. >> >> If you have a better algorithm or a better Python code (I'm a beginner in >> the Python world), don't hesitate ... > > Don't you want to just test that the number of "("s equals the number of > ")"s or am I missing the point? Yep, you are: "())))(((" is certainly not "well parenthized". Diez From dns4 at cornell.edu Tue Jul 14 09:08:12 2009 From: dns4 at cornell.edu (David Smith) Date: Tue, 14 Jul 2009 09:08:12 -0400 Subject: Python code for testing well parenthesized expression In-Reply-To: References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Jeremy Sanders wrote: > candide wrote: > >> I'm trying to implement in Python a function testing if an expression is >> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" >> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. >> >> My code follows at the end. >> >> If you have a better algorithm or a better Python code (I'm a beginner in >> the Python world), don't hesitate ... > > Don't you want to just test that the number of "("s equals the number of > ")"s or am I missing the point? > >>>> a='aAAA(bbb(cc)))' >>>> a.count('(') == a.count(')') > > Jeremy > Using straight counting, )z))ab(c(ew( would be well parenthesized. I suspect a better way would be to iterate over the string using a balance counter that increases 1 for every open, decreases 1 for every close. A negative balance at any moment would indicate an error as well as an ending balance greater than 0. --David From martin.hellwig at dcuktec.org Tue Jul 14 09:14:48 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 14 Jul 2009 14:14:48 +0100 Subject: Python code for testing well parenthesized expression In-Reply-To: References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Martin P. Hellwig wrote: > candide wrote: > To add to your implementations; a readable version: > > +++file parantheses.py+++ > """Parentheses Module Test""" > > def parentheses_are_paired(input_string): > "Check if 'input_string' contains paired parentheses, if so return > True." > parenthesis_count = 0 > parenthesis_open = '(' > parenthesis_close = ')' > > for character in input_string: > if character == parenthesis_open: > parenthesis_count += 1 > elif character == parenthesis_close: > parenthesis_count -= 1 > > > if parenthesis_count == 0: > return(True) > else: > if parenthesis_count < 0: > error_text = 'More closing parantheses than opening ones' > > elif parenthesis_count > 0: > error_text = 'More opening parantheses than closing ones' > > raise(ValueError(error_text)) > > if __name__ == '__main__': > TEST_TRUE = 'zx4er(1(er(Yy)ol)ol)ik' > TEST_ERROR = 'zx(4er(1(er(Yy)ol)ol)ik' > > print(parentheses_are_paired(TEST_TRUE)) > > try: > print(parentheses_are_paired(TEST_ERROR)) > except(ValueError): > print(False) > > ---file parantheses.py--- > However as Mr. Roggisch commented this wouldn't check if it is on the right spot, by moving the check that at any time you can not have more closing ones than opening ones within the loop you will get those exceptions too. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From sjmachin at lexicon.net Tue Jul 14 09:14:50 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 13:14:50 +0000 (UTC) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: candide free.invalid> writes: > # The obvious iterative version > def i(s): > op = 0 # op : open parenthesis > for k in range(len(s)): > op += (s[k] == '(') - (s[k] == ')') > if op < 0: break > return op > E: H c, w t P. F: A c, b ? P. Suggested better code: def iterative(string): paren_count = 0 for char in string: paren_count += (char == '(') - (char == ')') if paren_count < 0: break return paren_count You don't need the other versions :-) Try an iterative version of checking that () [] and {} are balanced and nested appropriately. Cheers, John From sjmachin at lexicon.net Tue Jul 14 09:17:48 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 06:17:48 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: On Jul 14, 11:08?pm, David Smith wrote: > Jeremy Sanders wrote: > > candide wrote: > > >> I'm trying to implement in Python a function testing if an expression is > >> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" > >> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. > > >> My code follows at the end. > > >> If you have a better algorithm or a better Python code (I'm a beginner in > >> the Python world), don't hesitate ... > > > Don't you want to just test that the number of "("s equals the number of > > ")"s or am I missing the point? > > >>>> a='aAAA(bbb(cc)))' > >>>> a.count('(') == a.count(')') > > > Jeremy > > Using straight counting, )z))ab(c(ew( would be well parenthesized. ?I > suspect a better way would be to iterate over the string using a balance > counter that increases 1 for every open, decreases 1 for every close. ?A > negative balance at any moment would indicate an error as well as an > ending balance greater than 0. > > --David Ummm isn't that the OP's first function? From Scott.Daniels at Acm.Org Tue Jul 14 09:19:37 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 06:19:37 -0700 Subject: Efficient binary search tree stored in a flat array? In-Reply-To: References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: Piet van Oostrum wrote: >>>>>> Douglas Alan (DA) wrote: > >> DA> On Jul 13, 3:57 pm, a... at pythoncraft.com (Aahz) wrote: >>>> Still, unless your list is large (more than thousands of elements), >>>> that's the way you should go. See the bisect module. Thing is, the >>>> speed difference between C and Python means the constant for insertion >>>> and deletion is very very small relative to bytecode speed. Keep in >>>> mind that Python's object/binding model means that you're shuffling >>>> pointers in the list rather than items. > >> DA> Thank you. My question wasn't intended to be Python specific, though. >> DA> I am just curious for purely academic reasons about whether there is >> DA> such an algorithm. All the sources I've skimmed only seem to the >> DA> answer the question via omission. Which is kind of strange, since it >> DA> seems to me like an obvious question to ask. It may well be that there is no good simple solution, and people avoid writing about non-existent algorithms. I certainly cannot imagine trying to write an article that carefully covered ideas which don't have well-studied data structures available, and calling them out only to say, "we don't know how to do this well." If such an algorithm were simple and obvious, I dare say you'd be taught about it around the time you learn binary search. > Of course you can take any BST algorithm and replace pointers by indices > in the array and allocate new elements in the array. But then you need > array elements to contain the indices for the children explicitely. And you loower your locality of reference (cache-friendliness). Note the insert in Python, for example, is quite cache-friendly. --Scott David Daniels Scott.Daniels at Acm.Org From aaron.watters at gmail.com Tue Jul 14 09:23:23 2009 From: aaron.watters at gmail.com (Aaron Watters) Date: Tue, 14 Jul 2009 06:23:23 -0700 (PDT) Subject: WHIFF 0.4 += repoze.who authentication + menus + calculator + docs. Message-ID: <1209aa77-c58c-4daf-a0f7-74f479ca59c8@f30g2000vbf.googlegroups.com> WHIFF (WSGI HTTP Integrated Filesystem Frames) 0.4 released. The new WHIFF 0.4 release linked from http://whiff.sourceforge.net includes the following enhancements: Built in support for repoze.who based authentication (from http://static.repoze.org/whodocs/ ) + tutorial see: http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1500.who . AJAX calculator demo and tutorial: see: http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1400.calc . Built in drop-down menu support and tutorial: see: http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1300.menus . Some subtle but important bug fixes, including a thread race condition bug fix. Auto-reload support. Also documentation improvements: particularly for standard middleware see: http://aaron.oirt.rutgers.edu/myapp/docs/W1200_1400.stdMiddleware What is WHIFF? WHIFF is a collection of support services for Python WSGI applications which allows applications to be composed by "dropping" dynamic pages into container directories. This mode of development will be familiar to developers who have created PHP applications, vanilla CGI scripts, Apache/modpy Publisher applications, JSP pages, or static web content. The WHIFF implementation significantly generalizes the "drop in" paradigm to support WSGI middleware components and application fragments as well as stand-alone pages. WHIFF provides other services in addition to supporting "drop in" components, such as managed application resources. I created WHIFF to address complexity issues I encounter when creating and fixing sophisticated Web applications which include complex database interactions and dynamic features such as AJAX (Asynchronous Javascript And XML). Project home page: http://whiff.sourceforge.net . Documentation index: http://aaron.oirt.rutgers.edu/myapp/docs/W.intro . I hope you like it! -- Aaron Watters === % ping elvis elvis is alive From sjmachin at lexicon.net Tue Jul 14 09:26:38 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 13:26:38 +0000 (UTC) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Adrian Dziubek gmail.com> writes: > In the i() there should be "return > op == 0" on the end. Hard to tell. In the absence of any docs, we have to resort to divination on the function name :-P ... is "i" short for "iterative" or "imbalanced"? From l.lensgraf at gmail.com Tue Jul 14 09:49:38 2009 From: l.lensgraf at gmail.com (DrLeif) Date: Tue, 14 Jul 2009 06:49:38 -0700 (PDT) Subject: PDF: finding a blank image References: Message-ID: <736570b5-c315-4eb2-99d1-5de7f05c26d0@l28g2000vba.googlegroups.com> On Jul 13, 6:22?pm, Scott David Daniels wrote: > DrLeif wrote: > > I have about 6000 PDF files which have been produced using a scanner > > with more being produced each day. ?The PDF files contain old paper > > records which have been taking up space. ? The scanner is set to > > detect when there is information on the backside of the page (duplex > > scan). ?The problem of course is it's not the always reliable and we > > wind up with a number of PDF files containingblankpages. > > > What I would like to do is have python detect a "blank" pages in a PDF > > file and remove it. ?Any suggestions? > > I'd check into ReportLab's commercial product, it may well be easily > capable of that. ?If no success, you might contact PJ at Groklaw, she > has dealt with a _lot_ of PDFs (and knows people who deal with PDFs > in bulk). > > --Scott David Daniels > Scott.Dani... at Acm.Org From aahz at pythoncraft.com Tue Jul 14 09:54:07 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 06:54:07 -0700 Subject: Infinite loops and synchronization References: Message-ID: In article , Vincent Gulinao wrote: > >lst = list() > >while True: > if len(lst) == SOME_NUMBER: > return lst > >Q2: operating on list from threads (mostly appends) must be safe, >right (synchronization)? What do you mean by "safe"? Python certainly won't crash, but there's no guarantee that the list will be consistent from *your* perspective. Consider what happens if len(lst) == SOME_NUMBER - 1 and some other part of your code adds two elements to lst. You'll skip right over your if condition. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From aahz at pythoncraft.com Tue Jul 14 09:55:37 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 06:55:37 -0700 Subject: compilation problem of python on AIX 6.1 References: <51c556a8-7277-474d-821f-190adf155c75@f16g2000vbf.googlegroups.com> Message-ID: In article <51c556a8-7277-474d-821f-190adf155c75 at f16g2000vbf.googlegroups.com>, Amit wrote: > >THen I decided to compile python 2.5.4 on my AIX box. I downloaded the >python source code from www.python.org and tried to compile on my AIX >both using gcc. > > case $MAKEFLAGS in *-s*) CC='gcc -pthread' LDSHARED='./Modules/ >ld_so_aix gcc -pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g - >fwrapv -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py -q >build;; *) CC='gcc -pthread' LDSHARED='./Modules/ld_so_aix gcc - >pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g -fwrapv -O3 -Wall - >Wstrict-prototypes' ./python -E ./setup.py build;; esac >running build >running build_ext >building 'bz2' extension >gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - >Wstrict-prototypes -I. -I/avre/Python-2.5.4/./Include -I. -IInclude - >I./Include -I/avre/Python-2.5.4/Include -I/avre/Python-2.5.4 -c /avre/ >Python-2.5.4/Modules/bz2module.c -o build/temp.aix-6.1-2.5/avre/ >Python-2.5.4/Modules/bz2module.o >building '_tkinter' extension >./Modules/ld_so_aix gcc -pthread -bI:Modules/python.exp build/ >temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/_tkinter.o build/ >temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/tkappinit.o -L/usr/X11/lib - >ltk8.4 -ltcl8.4 -lX11 -o build/lib.aix-6.1-2.5/_tkinter.so >*** WARNING: renaming "_tkinter" since importing it failed: 0509-022 >Cannot load module build/lib.aix-6.1-2.5. > 0509-026 System error: A file or directory in the path name does not >exist. This looks like a generic AIX compilation error finding system libraries; you probably will get better results from asking on an AIX forum. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From fatkinson at mishmash.com Tue Jul 14 09:56:01 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Tue, 14 Jul 2009 06:56:01 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: On Tue, 14 Jul 2009 05:41:40 GMT, Tim Harig wrote: >On 2009-07-14, Fred Atkinson wrote: >> The one thing I really dislike about Python over PHP is that >> Python can usually only appear in the cgi directory (unless other >> arragements are made with your hosting provider or if you reconfigure >> Apache on your own server if you have your own). With PHP, I can put >> them in any folder on my Web site without any problem. > >That is a server configuration and has nothing to do with Python directly. Agreed, it doesn't. But if my hosting provider won't change it, I'm stuck with it. Regards, Fred From fatkinson at mishmash.com Tue Jul 14 09:58:58 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Tue, 14 Jul 2009 06:58:58 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: <5k3p5514u11kldk56vjgpop4jp90hf3o89@4ax.com> On Mon, 13 Jul 2009 23:54:11 -0700 (PDT), alex23 wrote: >Fred Atkinson wrote: >> ? ? ? ? I wish the Python site was as well written as the PHP site. On >> the PHP site, I can look up a command and they show not only the docs >> on that command but a list of all other commands associated with it. ? > >Hey Fred, > >My problem is the complete opposite, I wish I could as easily >interrogate objects in PHP about their behaviour as I can Python :) > >In case you're not familiar with this, you can see what methods are >attached to an object using the intepreter via dir() and see the >associated docstring for them via help(). Try looking at 'dir(str)' >for a list of methods that strings recognise, then 'help(str.join)' to >see the help on join. > >This is such a handy ability to have that I miss this casual >introspection in everything else in which I'm required to develop. > >Hope this helps. I appreciate the information. I'm halfway through a course in PHP and Python. We are given assignments and we have to write the assignment in both languages. I'll take a look at that as I work on this week's assignment (I've only got a month to finish the course but I can probably take some time to look at this. Regards, Fred From phonky at europe.com Tue Jul 14 10:03:16 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 16:03:16 +0200 Subject: one-time factory in python for an experienced java guy Message-ID: Hi I have searched all over and haven't found the solution for my problem yet. I am new to python, and all the time realize I do program python in java, which is not great. Besides being a real-life problem, I want to solve it as elegant as I can, using it to also learn about python (I know I could just hack something easy for now). That's what I want to do. I have an Account class. An Account instance has to get an account number. On instance creation, I want the account number to be generated (I don't want callers to pass the account # to ensure uniqueness): class Account(object): def __init__(self, holder): self.__accountnumber = self.__generate_account_number() Now, I do not know yet how the account number scheme looks like. For now, I just want to have an incremental number; later, when going to production, we'll need the proper one. Furthermore, as we plan to distribute the package, we want to allow custom account numbering schemes. Thus, customers should be able to plug in their own AccountNumberGenerator implementation. For now, I have a generators.py module. I have an AccountNumberGenerator base class, all subclasses should implement "generate". I have an IncrementalGenerator subclass. So for now, I need to instantiate IncrementalGenerator, allowing for a future module to plugin their own generator. Any suggestions on how to do this? Thanks so much!!!! From l.lensgraf at gmail.com Tue Jul 14 10:04:16 2009 From: l.lensgraf at gmail.com (DrLeif) Date: Tue, 14 Jul 2009 07:04:16 -0700 (PDT) Subject: PDF: finding a blank image References: Message-ID: <6ba293b7-bba4-4d7b-ba3b-fb7901a2ea7d@e18g2000vbe.googlegroups.com> On Jul 13, 6:22?pm, Scott David Daniels wrote: > DrLeif wrote: > > I have about 6000 PDF files which have been produced using a scanner > > with more being produced each day. ?The PDF files contain old paper > > records which have been taking up space. ? The scanner is set to > > detect when there is information on the backside of the page (duplex > > scan). ?The problem of course is it's not the always reliable and we > > wind up with a number of PDF files containingblankpages. > > > What I would like to do is have python detect a "blank" pages in a PDF > > file and remove it. ?Any suggestions? > > I'd check into ReportLab's commercial product, it may well be easily > capable of that. ?If no success, you might contact PJ at Groklaw, she > has dealt with a _lot_ of PDFs (and knows people who deal with PDFs > in bulk). > > --Scott David Daniels > Scott.Dani... at Acm.Org Thanks everyone for the quick reply. I had considered using ReportLab however, was uncertain about it's ability to detect a blank page. Scott, I'll drop an email to ReportLab and PJ.... Thanks again, DrLeif From jjposner at optimum.net Tue Jul 14 10:07:52 2009 From: jjposner at optimum.net (John Posner) Date: Tue, 14 Jul 2009 10:07:52 -0400 Subject: Tkinter / Entry widget problem In-Reply-To: References: Message-ID: <4A5C9138.2020502@optimum.net> > Andras Szabo wrote: > > >> Hello. I searched the archives but couldn't find a solution to a >> problem related to the Entry widget in Tkinter. >> >> When creating a pop-up window in an app, which contains an Entry >> widget, I want this widget to contain some default string, to have all >> this default string selected (as if the user had manually selected >> everything), and to have the focus transferred to this widget. >> >> (The idea is then that if the window pops up, the user won't have to >> click or press Tab any more before being able to type what is needed >> in the textbox, overwriting what is written there already.) >> >> I thought this might be the way to go: >> >> entrybox=Entry(toplevel_parent_window) >> entrybox.insert(0,"Some default string") >> entrybox.select_range(0,END) >> entrybox.focus_set() >> entrybox.pack() >> >> But it doesn't seem to work - the focus is not transferred to the >> Entry widget, and the text does not appear to be selected (even though >> after this entrybox.selection_present() returns True). >> >> What am I doing wrong? >> > > Nothing, I would think. Can you post a minimal runnable example? > > Peter This works for me, on Windows and Linux: from Tkinter import * rt = Tk() rt.title("root window") pop = Toplevel() pop.title("second window") entrybox=Entry(pop) entrybox.insert(0,"Some default string") entrybox.select_range(0,END) entrybox.focus_set() entrybox.pack() rt.withdraw() rt.mainloop() On Win/XP SP3: > python Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter >>> Tkinter.TkVersion 8.5 >>> Tkinter.TclVersion 8.5 On Ubuntu Linux (Ubu-SL 2.6.27.14-generic): $ python Python 2.6.1 (r261:67515, Jan 9 2009, 19:06:23) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter >>> Tkinter.TkVersion 8.4000000000000004 >>> Tkinter.TclVersion 8.4000000000000004 -John From piet at cs.uu.nl Tue Jul 14 10:10:06 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 16:10:06 +0200 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: >>>>> Fred Atkinson (FA) wrote: >FA> On Tue, 14 Jul 2009 05:41:40 GMT, Tim Harig >FA> wrote: >>> On 2009-07-14, Fred Atkinson wrote: >>>> The one thing I really dislike about Python over PHP is that >>>> Python can usually only appear in the cgi directory (unless other >>>> arragements are made with your hosting provider or if you reconfigure >>>> Apache on your own server if you have your own). With PHP, I can put >>>> them in any folder on my Web site without any problem. >>> >>> That is a server configuration and has nothing to do with Python directly. >FA> Agreed, it doesn't. But if my hosting provider won't change it, I'm >FA> stuck with it. That's a good reason to dislike your hosting provider, not a good reason to dislike Python. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From stefan_ml at behnel.de Tue Jul 14 10:27:48 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 14 Jul 2009 16:27:48 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: References: Message-ID: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> phonky wrote: > class Account(object): > def __init__(self, holder): > self.__accountnumber = self.__generate_account_number() > > Now, I do not know yet how the account number scheme looks like. > For now, I just want to have an incremental number; later, > when going to production, we'll need the proper one. Use a global variable in the module. Stefan From http Tue Jul 14 10:33:26 2009 From: http (Paul Rubin) Date: 14 Jul 2009 07:33:26 -0700 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7xzlb7wczt.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > phonky wrote: > > class Account(object): > > def __init__(self, holder): > > self.__accountnumber = self.__generate_account_number() > > > > Now, I do not know yet how the account number scheme looks like. > > For now, I just want to have an incremental number; later, > > when going to production, we'll need the proper one. > > Use a global variable in the module. Yuch! Don't use a global. Try something like: import itertools class Account(object): def __init__(self, holder, gen=itertools.count()): self.__accountnumber = gen.next() From phonky at europe.com Tue Jul 14 10:34:30 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 16:34:30 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> Message-ID: Stefan, thanks first of all > Use a global variable in the module. I have an account_number_generator variable in the module, I got that hint from searching the web. But where my stubborn java mind doesn't release me: what does the variable contain? Do I create the actual IncrementalGenerator object there? Or the super class? Or just a string, which a factory method takes to create the actual object? What I especially don't get: How will an external module overwrite that variable? I fail to see, python being interpreted, how I can ensure that a future module is being executed later, thus overwriting the variable. Thanks again. From newport at freemail.hu Tue Jul 14 10:38:42 2009 From: newport at freemail.hu (Andras Szabo) Date: Tue, 14 Jul 2009 17:38:42 +0300 Subject: Tkinter / Entry widget problem In-Reply-To: <4A5C9138.2020502@optimum.net> References: <4A5C9138.2020502@optimum.net> Message-ID: So it's either that I use Python 2.5.1, or that I use it on a Mac. (John, your code still doesn't work the way it's supposed to here.) I guess I'll upgrade to 2.6.1 and see if it makes a difference. (The Tkinter/Tcl versions are the same for me.) Thanks for your help. andras On Jul 14, 2009, at 5:07 PM, John Posner wrote: > >> Andras Szabo wrote: >> >> >>> Hello. I searched the archives but couldn't find a solution to a >>> problem related to the Entry widget in Tkinter. >>> >>> When creating a pop-up window in an app, which contains an Entry >>> widget, I want this widget to contain some default string, to have >>> all >>> this default string selected (as if the user had manually selected >>> everything), and to have the focus transferred to this widget. >>> >>> (The idea is then that if the window pops up, the user won't have to >>> click or press Tab any more before being able to type what is needed >>> in the textbox, overwriting what is written there already.) >>> >>> I thought this might be the way to go: >>> >>> entrybox=Entry(toplevel_parent_window) >>> entrybox.insert(0,"Some default string") >>> entrybox.select_range(0,END) >>> entrybox.focus_set() >>> entrybox.pack() >>> >>> But it doesn't seem to work - the focus is not transferred to the >>> Entry widget, and the text does not appear to be selected (even >>> though >>> after this entrybox.selection_present() returns True). >>> >>> What am I doing wrong? >>> >> >> Nothing, I would think. Can you post a minimal runnable example? >> >> Peter > > This works for me, on Windows and Linux: > > from Tkinter import * > > rt = Tk() > rt.title("root window") > pop = Toplevel() > pop.title("second window") > > entrybox=Entry(pop) > entrybox.insert(0,"Some default string") > entrybox.select_range(0,END) > entrybox.focus_set() > entrybox.pack() > > rt.withdraw() > rt.mainloop() > > > On Win/XP SP3: > > > python > Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> import Tkinter > >>> Tkinter.TkVersion > 8.5 > >>> Tkinter.TclVersion > 8.5 > > On Ubuntu Linux (Ubu-SL 2.6.27.14-generic): > > $ python > Python 2.6.1 (r261:67515, Jan 9 2009, 19:06:23) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import Tkinter > >>> Tkinter.TkVersion > 8.4000000000000004 > >>> Tkinter.TclVersion > 8.4000000000000004 > > > -John > > From http Tue Jul 14 10:40:52 2009 From: http (Paul Rubin) Date: 14 Jul 2009 07:40:52 -0700 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7xab37l43v.fsf@ruckus.brouhaha.com> phonky writes: > But where my stubborn java mind doesn't release me: what does the > variable contain? Do I create the actual IncrementalGenerator object > there? Or the super class? Or just a string, which a factory method > takes to create the actual object? Ugh, just forget everything you ever knew about java. Do some Zen exercises to erase your mind. Then read a Python tutorial as if you're starting from nothing. From contact at xavierho.com Tue Jul 14 10:52:55 2009 From: contact at xavierho.com (Xavier Ho) Date: Tue, 14 Jul 2009 22:52:55 +0800 Subject: Why does extend() fail in this case, and what am I doing wrong? Message-ID: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? The code is run at: http://codepad.org/wgLU4JZh class A(): def __init__(self): self.n = [1, 2, 3, 4, 5] a = A() print a.n print a.n.extend([6, 7, 8, 9]) #Output: #[1, 2, 3, 4, 5] #None I really don't know, but I'm probably missing something. Any pointers would be great. Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From phonky at europe.com Tue Jul 14 10:55:56 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 16:55:56 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: <7xab37l43v.fsf@ruckus.brouhaha.com> References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> Message-ID: <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Thanks Paul, > Ugh, just forget everything you ever knew about java. Do some Zen > exercises to erase your mind. Then read a Python tutorial as if > you're starting from nothing. Yeah, surely right, but easier said than done... I'm working on it. Taking your example. import itertools class Account(object): def __init__(self, holder, gen=itertools.count()): self.__accountnumber = gen.next() If you consider my python illiteracy, "itertools.count(): Make an iterator that returns consecutive integers starting with n" to me that sounds like that solves the increment issue, but what about future modules wanting to plug in a different numbering format, e.g. 205434.1234 or whatever? From tn.pablo at gmail.com Tue Jul 14 11:07:29 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Tue, 14 Jul 2009 10:07:29 -0500 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Message-ID: This has been asked extensively before, here and elsewhere. On Tue, Jul 14, 2009 at 09:52, Xavier Ho wrote: > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? > The code is run at: http://codepad.org/wgLU4JZh > > class A(): > ??? def __init__(self): > ??????? self.n = [1, 2, 3, 4, 5] > > a = A() > print a.n > print a.n.extend([6, 7, 8, 9]) > > #Output: > #[1, 2, 3, 4, 5] > #None > > I really don't know, but I'm probably missing something. Any pointers would > be great. > > Best regards, > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Pablo Torres N. From mabdelkader at gmail.com Tue Jul 14 11:07:59 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Tue, 14 Jul 2009 11:07:59 -0400 Subject: one-time factory in python for an experienced java guy In-Reply-To: <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: <148918f0907140807i3b31f9b0u94fecf309eec5cdc@mail.gmail.com> What Paul was trying to elaborate on is that have your customers or whomever will use this implement their own generator protocol to generate whatever number format they need. Paul just gave you an example with itertools.count(), where it is an infinite generator that yields count+1 every time. Read up on the iterator protocol, have your customers pass in a generator to establish their own formats. http://docs.python.org/library/stdtypes.html#iterator-types This is much more elegant than AbstractCustomerRandomFormatGenerator..etc which is what the reference to "erase everything from Java" is attempting to relate. -- mahmoud mack abdelkader python powered http://blog.mahmoudimus.com/ mahmoud at linux.com On Tue, Jul 14, 2009 at 10:55 AM, phonky wrote: > Thanks Paul, > > Ugh, just forget everything you ever knew about java. Do some Zen >> exercises to erase your mind. Then read a Python tutorial as if >> you're starting from nothing. >> > > Yeah, surely right, but easier said than done... > I'm working on it. > > Taking your example. > > import itertools > > class Account(object): > def __init__(self, holder, gen=itertools.count()): > self.__accountnumber = gen.next() > > If you consider my python illiteracy, > > "itertools.count(): Make an iterator that returns consecutive integers > starting with n" > > to me that sounds like that solves the increment issue, but what about > future modules wanting to plug in a different > numbering format, e.g. 205434.1234 or whatever? > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Tue Jul 14 11:13:43 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 16:13:43 +0100 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Message-ID: <4A5CA0A7.7090800@mrabarnett.plus.com> Xavier Ho wrote: > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? > The code is run at: http://codepad.org/wgLU4JZh > > class A(): > def __init__(self): > self.n = [1, 2, 3, 4, 5] > > a = A() > print a.n > print a.n.extend([6, 7, 8, 9]) > > #Output: > #[1, 2, 3, 4, 5] > #None > > I really don't know, but I'm probably missing something. Any pointers > would be great. > The extend() method modifies the list in-place and returns None. Try printing a.n after extending. From http Tue Jul 14 11:14:18 2009 From: http (Paul Rubin) Date: 14 Jul 2009 08:14:18 -0700 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: <7xzlb7fgad.fsf@ruckus.brouhaha.com> phonky writes: > "itertools.count(): Make an iterator that returns consecutive integers > starting with n" > > to me that sounds like that solves the increment issue, but what about > future modules wanting to plug in a different > numbering format, e.g. 205434.1234 or whatever? You'd write a different generator for that, and use it instead of itertools.count. E.g. (untested): def different_gen(): a = 201593.0768 # initial number while True: yield '%.04f'% a a += 123.4567 # increase in increments of this much From __peter__ at web.de Tue Jul 14 11:20:03 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Jul 2009 17:20:03 +0200 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: phonky wrote: > Thanks Paul, > >> Ugh, just forget everything you ever knew about java. Do some Zen >> exercises to erase your mind. Then read a Python tutorial as if >> you're starting from nothing. > > Yeah, surely right, but easier said than done... > I'm working on it. > > Taking your example. > > import itertools > > class Account(object): > def __init__(self, holder, gen=itertools.count()): > self.__accountnumber = gen.next() > > If you consider my python illiteracy, > > "itertools.count(): Make an iterator that returns consecutive integers > starting with n" > > to me that sounds like that solves the increment issue, but what about > future modules wanting to plug in a different > numbering format, e.g. 205434.1234 or whatever? In that case you may want to stick with the class attribute: >>> class Account(object): ... def __init__(self): ... self.account = self.next_account() ... def __str__(self): ... return "Account(number=%r)" % self.account ... __repr__ = __str__ ... >>> from itertools import count >>> Account.next_account = count(42).next >>> a = Account() >>> b = Account() >>> a, b (Account(number=42), Account(number=43)) >>> from uuid import uuid1 >>> Account.next_account = staticmethod(uuid1) >>> c = Account() >>> d = Account() >>> c, d (Account(number=UUID('b0f8dfc6-7087-11de-be16-001d923f29c5')), Account(number=UUID('b310c90e-7087-11de-be16-001d923f29c5'))) You can plug in arbitrary callables at runtime. The only complication I can see is that you may have to wrap them into a staticmethod to prevent python from passing the self reference. You can avoid that if you just use a global instead: # account.py next_account = ... class Account(object): def __init__(self): self.number = next_account() You can then set the factory elsewhere # main.py import account account.next_account = ... a = Account() In general in python we like to keep simple things simple rather than creating a huge bureaucracy. Peter From ml at well-adjusted.de Tue Jul 14 11:20:13 2009 From: ml at well-adjusted.de (Jochen Schulz) Date: Tue, 14 Jul 2009 17:20:13 +0200 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Message-ID: <20090714152012.GY31378@wasteland.homelinux.net> Xavier Ho: > > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? -- snip > print a.n.extend([6, 7, 8, 9]) extend doesn't fail. It just returns None and extends the list in place. In [1]: l = [1, 2, 3] In [2]: l.extend([4, 5, 6]) In [3]: l Out[3]: [1, 2, 3, 4, 5, 6] J. -- When I get home from the supermarket I don't know what to do with all the plastic. [Agree] [Disagree] -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From aahz at pythoncraft.com Tue Jul 14 11:24:14 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 08:24:14 -0700 Subject: one-time factory in python for an experienced java guy References: <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: In article <23406$4a5c9c7d$d9a2f023$27926 at news.hispeed.ch>, phonky wrote: > >import itertools > > class Account(object): > def __init__(self, holder, gen=itertools.count()): > self.__accountnumber = gen.next() > >If you consider my python illiteracy, > >"itertools.count(): Make an iterator that returns consecutive integers >starting with n" > >to me that sounds like that solves the increment issue, but what about >future modules wanting to plug in a different >numbering format, e.g. 205434.1234 or whatever? Here's what I would do: class Account: gen = itertools.count gen_instance = None def __init__(self, holder): if self.gen_instance is None: self.__class__.gen_instance = self.gen() self._account = self.gen_instance() Notice that I'm using only a single underscore for ``_account`` to make inheritance simpler, and I'm using ``self`` to access class attributes *except* when I need to *set* the class attribute, which requires ``self.__class__``. Now anyone who wants to change the generator can simply do module.Account.gen = other_gen and it will work as long as no Account() instances have been created (you don't want the generator to change mid-stream, right?). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From pdpinheiro at gmail.com Tue Jul 14 11:35:40 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 14 Jul 2009 08:35:40 -0700 (PDT) Subject: one-time factory in python for an experienced java guy References: Message-ID: On Jul 14, 3:03?pm, phonky wrote: > Hi > > I have searched all over and haven't found the solution > for my problem yet. I am new to python, and all the time realize I > do program python in java, which is not great. > > Besides being a real-life problem, I want to > solve it as elegant as I can, using it to > also learn about python (I know I could just > hack something easy for now). > > That's what I want to do. > > I have an Account class. > An Account instance has to get an account number. > On instance creation, I want the account number to > be generated (I don't want callers to pass > the account # to ensure uniqueness): > > class Account(object): > ? ? ? ? def __init__(self, holder): > ? ? ? ? ? ? ? ? self.__accountnumber = self.__generate_account_number() > > Now, I do not know yet how the account number scheme looks like. > For now, I just want to have an incremental number; later, > when going to production, we'll need the proper one. > > Furthermore, as we plan to distribute the package, we want > to allow custom account numbering schemes. Thus, customers > should be able to plug in their own AccountNumberGenerator implementation. > > For now, I have a generators.py module. > > I have an AccountNumberGenerator base class, > all subclasses should implement "generate". > > I have an IncrementalGenerator subclass. > > So for now, I need to instantiate IncrementalGenerator, > allowing for a future module to plugin their own generator. > > Any suggestions on how to do this? > Thanks so much!!!! You don't want an AccountNumberGenerator class and subclassing, all you need is an iterator/generator of some form. These might help: http://docs.python.org/tutorial/classes.html#iterators http://docs.python.org/tutorial/classes.html#generators (in fact, that whole page is pretty relevant) Once your code expects one of those, it's trivially easy to plug something else in there. From Scott.Daniels at Acm.Org Tue Jul 14 11:44:04 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 08:44:04 -0700 Subject: Proposal: Decimal literals in Python. In-Reply-To: References: <4721ab28$0$1238$e4fe514c@dreader28.news.xs4all.nl> <1193438763.190428.134710@q5g2000prf.googlegroups.com> <87tzodii6i.fsf@benfinney.id.au> <1193494184.618613.81090@50g2000hsm.googlegroups.com> Message-ID: Tim Roberts wrote: > My favorite notation for this comes from Ada, which allows arbitrary bases > from 2 to 16, and allows for underscores within numeric literals: > > x23_bin : constant := 2#0001_0111#; > x23_oct : constant := 8#27#; > x23_dec : constant := 10#23#; > x23_hex : constant := 16#17#; And mine is one w/o the base 10 bias: .f.123 == 0x123 .7.123 == 0o123 .1.1101 == 0b1101 That is, .. -- show the base by showing base-1 in the base. I actually built this into "OZ," an interpretter. --Scott David Daniels Scott.Daniels at Acm.Org From pavlovevidence at gmail.com Tue Jul 14 11:53:33 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 08:53:33 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: On Jul 14, 2:14?am, Steven D'Aprano wrote: > On Tue, 14 Jul 2009 01:30:48 -0700, Carl Banks wrote: > > Seriously, do you *ever* take more than 2 seconds to consider whether > > you might be missing something obvious before following up with these > > indignant knee-jerk responses? > > Obviously not. > > [...] > > > Or would you rather let all unexpected exceptions print to standard > > error, which is often a black hole in non-interactive sitations? > > Fair point. Of course you're right. I don't want to be mean or anything. I agree with you on 95% of issues probably, of course I only follow up to disagree.... Carl Banks From contact at xavierho.com Tue Jul 14 11:54:05 2009 From: contact at xavierho.com (Xavier Ho) Date: Tue, 14 Jul 2009 23:54:05 +0800 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <20090714152012.GY31378@wasteland.homelinux.net> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> <20090714152012.GY31378@wasteland.homelinux.net> Message-ID: <2d56febf0907140854s52d20904y90c7ccb6a0262cb9@mail.gmail.com> I see. Thanks! Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Tue, Jul 14, 2009 at 11:20 PM, Jochen Schulz wrote: > Xavier Ho: > > > > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? > -- snip > > print a.n.extend([6, 7, 8, 9]) > > extend doesn't fail. It just returns None and extends the list in place. > > In [1]: l = [1, 2, 3] > > In [2]: l.extend([4, 5, 6]) > > In [3]: l > Out[3]: [1, 2, 3, 4, 5, 6] > > > J. > -- > When I get home from the supermarket I don't know what to do with all the > plastic. > [Agree] [Disagree] > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.9 (GNU/Linux) > > iEYEARECAAYFAkpcoiwACgkQ+AfZydWK2zmchgCfZjRCOGTa0OZ1Q045sCLZfpvD > EIEAnjJ8/uNwPYFfCsGNbQIDd5+LnkbA > =fCdV > -----END PGP SIGNATURE----- > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Tue Jul 14 11:55:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 16:55:18 +0100 Subject: Proposal: Decimal literals in Python. In-Reply-To: References: <4721ab28$0$1238$e4fe514c@dreader28.news.xs4all.nl> <1193438763.190428.134710@q5g2000prf.googlegroups.com> <87tzodii6i.fsf@benfinney.id.au> <1193494184.618613.81090@50g2000hsm.googlegroups.com> Message-ID: <4A5CAA66.3080608@mrabarnett.plus.com> Scott David Daniels wrote: > Tim Roberts wrote: >> My favorite notation for this comes from Ada, which allows arbitrary >> bases >> from 2 to 16, and allows for underscores within numeric literals: >> >> x23_bin : constant := 2#0001_0111#; >> x23_oct : constant := 8#27#; >> x23_dec : constant := 10#23#; >> x23_hex : constant := 16#17#; > And mine is one w/o the base 10 bias: > .f.123 == 0x123 > .7.123 == 0o123 > .1.1101 == 0b1101 > That is, .. > -- show the base by showing base-1 in the base. > I actually built this into "OZ," an interpretter. > Smalltalk uses "r" (for "radix"). If we also permit underscores: x23_bin = 2r0001_0111 x23_oct = 8r27 x23_dec = 10r23 x23_hex = 16r17 From doctoresam at gmail.com Tue Jul 14 11:55:36 2009 From: doctoresam at gmail.com (Deep_Feelings) Date: Tue, 14 Jul 2009 08:55:36 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? Message-ID: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> So you have chosen programming language "x" so shall you tell us why you did so , and what negatives or positives it has ? From aahz at pythoncraft.com Tue Jul 14 11:58:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 08:58:30 -0700 Subject: The meaning of "=" References: Message-ID: In article , Piet van Oostrum wrote: >>>>>> Lawrence D'Oliveiro (LD) wrote: > >>LD> In message , Aahz wrote: > >>Aahz> class AttrDict: >>Aahz> def __getitem__(self, key): >>Aahz> return getattr(self, key) > >>LD> OK, let's try it: > >>LD> >>> c = {} >>LD> >>> c["x"] = 3 >>LD> >>> c.x = 4 >>LD> Traceback (most recent call last): >>LD> File "", line 1, in >>LD> AttributeError: 'dict' object has no attribute 'x' >>LD> >>> class AttrDict: >>LD> ... def __getitem__(self, key): >>LD> ... return getattr(self, key) >>LD> ... >>LD> >>> c.x = 4 >>LD> Traceback (most recent call last): >>LD> File "", line 1, in >>LD> AttributeError: 'dict' object has no attribute 'x' > >>LD> Nope, still doesn't work... > >Of course you need c = AttrDict() Absolutely -- Lawrence really needs to learn to do his own debugging. >And to get c.x = 4 working you also need a __setitem__. Nope. You do need __setitem__ so that this works: c['x'] = 4 >And to get c["x"] working AtrrDict should subclass dict: Definitely not. There's a perfectly good dict inside a regular class instance already. The only reason to subclass from dict is so that you get all the dict methods for free; however, the cost is that you get ugly bugs because of e.g. c['update'] = 'foo' Overally, I think it's much better/safer to explicitly pull the dict methods you want to use. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From jfrancis4970 at mailinator.com Tue Jul 14 11:58:44 2009 From: jfrancis4970 at mailinator.com (jfrancis4970 at mailinator.com) Date: Tue, 14 Jul 2009 08:58:44 -0700 (PDT) Subject: how to determine if python is run interactively? (-i) Message-ID: <17280e94-6c15-4225-9d28-3dff4e0b61a5@p15g2000vbl.googlegroups.com> how do you determine, from within a python program, whether the python interpreter was launched in interactive mode? in other words, if i have a program called "test.py", i want to ensure that the program was launched with this command line: python -i test.py (and not just with "python test.py"). the reason is that i'm doing some very expensive and lengthy initialization in my test.py program that will be useless unless the program exits to the python prompt when it is done, where the user can run further python commands. obviously i can't use sys.argv since that is only for parameters passed to the test.py program, not for parameters passed to the python interpreter itself. i haven't found a way to access these options... any help would be greatly appreciated! From pavlovevidence at gmail.com Tue Jul 14 12:01:37 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 09:01:37 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: <7d161f1f-2c09-42ca-9bbd-b99a777044ea@j21g2000vbn.googlegroups.com> On Jul 14, 4:48?am, Lawrence D'Oliveiro wrote: > In message <93f6a517-63d8-4c80- > > bf19-4614b7099... at m7g2000prd.googlegroups.com>, Carl Banks wrote: > > Or would you rather let all unexpected exceptions print to standard > > error, which is often a black hole in non-interactive sitations? > > Since when? > > Cron, for example, collects standard error and mails it to you. 1. Cron is only one way to run programs non-interactively. (Would that work with, say, Windows services?) 2. Many systems don't run MTAs these days 3. In my experience the pipeline from cron to mail delivery is error prone, mainly due to the complexity of configuring MTAs 4. Even if all this works, you might just want to do your logging directly in Python anyway Carl Banks From rylesny at gmail.com Tue Jul 14 12:12:01 2009 From: rylesny at gmail.com (ryles) Date: Tue, 14 Jul 2009 09:12:01 -0700 (PDT) Subject: how to set timeout while colling a soap method? References: Message-ID: On Jul 13, 9:07?am, dzizes wrote: > Hello! > > I wrote some some python code for executing a soap method: > > import SOAPpy > from SOAPpy import WSDL > > _server = WSDL.Proxy(some wsdl) > r=_server.generuj(some parameters...) > print r.encode('cp1250') > > It works fine. However, the execution time of this soap method might be > long. Therefore, I would like to set a timeout like 1 minute, after which I > would post a timeoute message. > > Any ideas? > Greats, > > -- > View this message in context:http://www.nabble.com/how-to-set-timeout-while-colling-a-soap-method-... > Sent from the Python - python-list mailing list archive at Nabble.com. I don't believe SOAPpy supports this directly. You can set the timeout globally before you make your SOAP call: import socket socket.setdefaulttimeout(60) http://docs.python.org/library/socket.html#socket.setdefaulttimeout From python at mrabarnett.plus.com Tue Jul 14 12:14:03 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 17:14:03 +0100 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <4A5CAECB.8080604@mrabarnett.plus.com> Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and what negatives or positives it has ? I've heard of "C" and "D", but not "x", unless you mean XPL (X Programming Language) or PLAN-X. :-) From duncan.booth at invalid.invalid Tue Jul 14 12:26:28 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 14 Jul 2009 16:26:28 GMT Subject: how to determine if python is run interactively? (-i) References: <17280e94-6c15-4225-9d28-3dff4e0b61a5@p15g2000vbl.googlegroups.com> Message-ID: jfrancis4970 at mailinator.com wrote: > how do you determine, from within a python program, whether the python > interpreter was launched in interactive mode? sys.flags.interactive or sys.flags.inspect From duncan.booth at invalid.invalid Tue Jul 14 13:10:17 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 14 Jul 2009 17:10:17 GMT Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: John Machin wrote: > Try an iterative version of checking that () [] and {} > are balanced and nested appropriately. Here's how I might approach the more general case: def balanced(s, parens=("()",)): ''' Example: >>> balanced('aAAA(b[bb(c]c))') True >>> balanced('aAAA(b[bb(c]c))', parens=("()", "[]")) False ''' s = re.sub("[^%s]+" % re.escape("".join(parens)), "", s) for i in range(len(s)/2): for p in parens: s = s.replace(p, "") return not s For short strings this is obviously slower than your 'iterative' function, but the run time mostly depends on the number of pairs of parentheses rather than the total length of the string, so for example it starts winning on a string with 2 pairs of parentheses about 75 characters long. From amrita at iisermohali.ac.in Tue Jul 14 13:33:59 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Tue, 14 Jul 2009 23:03:59 +0530 (IST) Subject: select lines in python Message-ID: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Dear all, Can anyone tell me that suppose i have a file having content like: _Atom_name _Atom_type _Chem_shift_value _Chem_shift_value_error _Chem_shift_ambiguity_code 1 1 PHE H H 8.49 0.02 1 2 1 PHE HA H 4.60 0.02 1 3 1 PHE CA C 57.83 0.3 1 4 2 LEU H H 8.23 0.02 1 5 2 LEU HA H 4.25 0.02 1 6 2 LEU HB2 H 1.54 0.02 1 7 3 ASP H H 8.10 0.02 1 8 3 ASP HA H 4.52 0.02 1 9 3 ASP HB2 H 2.65 0.02 1 stop now what i want that instead of copying all the lines it will just write the information about PHE and ASP then how i acn do that using python programming.Kindly tell me the command for that. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From degibb at gmail.com Tue Jul 14 13:48:06 2009 From: degibb at gmail.com (David Gibb) Date: Tue, 14 Jul 2009 13:48:06 -0400 Subject: select lines in python In-Reply-To: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> References: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Message-ID: try something like: for line in open("filename").readlines(): if (re.search("PHE|ASP",line): print line On Tue, Jul 14, 2009 at 1:33 PM, wrote: > Dear all, > > Can anyone tell me that suppose i have a file having content like: > > ? ?_Atom_name > ? ? ?_Atom_type > ? ? ?_Chem_shift_value > ? ? ?_Chem_shift_value_error > ? ? ?_Chem_shift_ambiguity_code > ? ? 1 ? ? 1 ? PHE ? ? ? H ? ? H ? ? ?8.49 ? ? 0.02 ? ? 1 > ? ? 2 ? ? 1 ? PHE ? ? ? HA ? ?H ? ? ?4.60 ? ? 0.02 ? ? 1 > ? ? 3 ? ? 1 ? PHE ? ? ? CA ? ?C ? ? 57.83 ? ? ?0.3 ? ? 1 > ? ? 4 ? ? 2 ? LEU ? ? ? H ? ? H ? ? ?8.23 ? ? 0.02 ? ? 1 > ? ? 5 ? ? 2 ? LEU ? ? ? HA ? ?H ? ? ?4.25 ? ? 0.02 ? ? 1 > ? ? 6 ? ? 2 ? LEU ? ? ? HB2 ? H ? ? ?1.54 ? ? 0.02 ? ? 1 > ? ? 7 ? ? 3 ? ASP ? ? ? H ? ? H ? ? ?8.10 ? ? 0.02 ? ? 1 > ? ? 8 ? ? 3 ? ASP ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > ? ? 9 ? ? 3 ? ASP ? ? ? HB2 ? H ? ? ?2.65 ? ? 0.02 ? ? 1 > stop > > > now what i want that instead of copying all the lines it will just write > the information about PHE and ASP then how i acn do that using python > programming.Kindly tell me the command for that. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > > -- > http://mail.python.org/mailman/listinfo/python-list > From phonky at europe.com Tue Jul 14 13:50:54 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 19:50:54 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: References: Message-ID: Thanks for all replies. I need to practice much more pythonese.... In fact I don't think to understand all of your suggestions, so I'll need to go through them and decide what approach I am going to take. Thanks a lot! From mensanator at aol.com Tue Jul 14 13:52:39 2009 From: mensanator at aol.com (Mensanator) Date: Tue, 14 Jul 2009 10:52:39 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> On Jul 14, 10:55?am, Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and ?what negatives or positives it has ? language must have - unlimited precision integers - easy to program - IDE not required - reasonable speed - math library needs to include number theoretic functions like GCD, LCM, Modular Inverse, etc. - not fucking retarded like F# That leaves Python (along with gympy, the Python wrapper for the GMP library, which could be used with C or C++ if it weren't for the ease of use issue) as the obvious choice. As for negatives, the GMP library doesn't factor. As for positives, you can call a factoring program from Python and capture the output (and have it correct the bug in the factoring program without having to fix the factoring program). From tonylay at gmail.com Tue Jul 14 13:59:24 2009 From: tonylay at gmail.com (Tony Lay) Date: Tue, 14 Jul 2009 10:59:24 -0700 (PDT) Subject: ImportError: No module named _functools Message-ID: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> I'm sooo close to getting this meld program running....had to install a lot of things in /usr/local to get to pygtk2 functional and I'm trying to run the meld program and get... RHEL4 server Traceback (most recent call last): File "/usr/local/bin/meld", line 35, in import gettext File "/usr/local/lib/python2.6/gettext.py", line 49, in import locale, copy, os, re, struct, sys File "/usr/local/lib/python2.6/locale.py", line 15, in import functools File "/usr/local/lib/python2.6/functools.py", line 10, in from _functools import partial, reduce ImportError: No module named _functools This is with me building all of the dependencies as well as python with ./configure --prefix=/usr/local --exec-prefix=/usr/local make distclean make make -i install ldconfig export PYTHONHOME=/usr/local/lib/python2.6 export PYTHONPATH=/usr/local/lib/python2.6:/usr/local/lib/python2.6/ site-packages I thought it might have been an err of the application, but I ran python and received the same error: [root at orlstscts1 meld-1.0.0]# python Python 2.6.2 (r262:71600, Jul 14 2009, 11:47:04) [GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import functools Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.6/functools.py", line 10, in from _functools import partial, reduce ImportError: No module named _functools Kind of a python n00b, but the functools.py is there, it's in the path...everything seems to be lined up right. Problem is that the server this is on is stable (read older), and I am to not touch the older software unless it's as a last resort. For this reason everything is in /usr/local and built from the ground up. I'm hoping that there's some silly dependency I'm missing. I've installed the following: atk-1.26.0 pkg-config-0.23 cairo-1.8.8 libglade-2.6.4 pycairo-1.8.6 fontconfig-2.7.0 libpng-1.2.37 pygobject-2.18.0 freetype-2.1.10 libxml2-2.7.3 pygtk-2.15.2 gettext-0.17 meld-1.0.0 pygtk-2.8.6 glade3-3.6.7 meld-1.1.5 Python-2.6.2 glib-2.20.4 glibc-2.9 meld-1.3.0 (no install) tcl8.5.7 tk8.5.7 gtk+-2.16.4 pango-1.20.5 intltool-0.35.5 pixman-0.15.14 Took me a while to get by fontconfig/freetype junk because (usr/local/ lib/libfontconfig.so: undefined reference to `FT_Select_Size') but that's a little OT. If there are any recommendations of what to attack or further information would help let me know! Regards, -Tony From weafon at lbl.gov Tue Jul 14 14:03:22 2009 From: weafon at lbl.gov (weafon) Date: Tue, 14 Jul 2009 11:03:22 -0700 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? Message-ID: <4A5CC86A.9070907@lbl.gov> Hi guys, I have a question about the usage of yield. As shown in the below example, in general, if there is a code segment commonly used by two or more functions, we may isolate the segment into a function and then call it from other functions if necessary. def func1(): .... while(cond): ..... commoncode() ... def func2(): .... while(cond): ..... commoncode() ... def commoncode() AAAA BBBB CCCC However, if there is a 'yield' operation in the common code segment, the isolation causes that func1 and func2 become a non-generator function!! Although I can prevent such an isolation by just duplicating the segment in func1 and func2 to keep both of them being generator functions, the code may become ugly and hard to maintain particularly when coomoncode() is long. The problem may be resolved if I can define the commoncode() as an inline function or marco. Unfortunately, inline and marco do not seems to be implemented in python. Thus, how can I isolate a common segment into a function when there are yield operations in the common segment? Thanks, Weafon From nagle at animats.com Tue Jul 14 14:06:17 2009 From: nagle at animats.com (John Nagle) Date: Tue, 14 Jul 2009 11:06:17 -0700 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. > > Since backward compatibility is being thrown away anyway, why not > enforce the four space rule? > > At least that way, when I get python code from somebody else, I would > know what I am looking at, without having to do a hex dump, or > something. Python 3 enforces the rule that you can't mix tabs and spaces for indentation in the same file. That (finally) guarantees that the indentation you see is what the Python parser sees. That's enough to prevent non-visible indentation errors. It also means that the Python parser no longer has to have any concept of how many spaces equal a tab. So the problem is now essentially solved. John Nagle From rhodri at wildebst.demon.co.uk Tue Jul 14 14:21:47 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 14 Jul 2009 19:21:47 +0100 Subject: select lines in python In-Reply-To: References: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Message-ID: On Tue, 14 Jul 2009 18:48:06 +0100, David Gibb wrote: [Something top-posted, which I've shuffled down] > On Tue, Jul 14, 2009 at 1:33 PM, wrote: >> Dear all, >> >> Can anyone tell me that suppose i have a file having content like: >> >> ? ?_Atom_name >> ? ? ?_Atom_type >> ? ? ?_Chem_shift_value >> ? ? ?_Chem_shift_value_error >> ? ? ?_Chem_shift_ambiguity_code >> ? ? 1 ? ? 1 ? PHE ? ? ? H ? ? H ? ? ?8.49 ? ? 0.02 ? ? 1 >> ? ? 2 ? ? 1 ? PHE ? ? ? HA ? ?H ? ? ?4.60 ? ? 0.02 ? ? 1 >> ? ? 3 ? ? 1 ? PHE ? ? ? CA ? ?C ? ? 57.83 ? ? ?0.3 ? ? 1 >> ? ? 4 ? ? 2 ? LEU ? ? ? H ? ? H ? ? ?8.23 ? ? 0.02 ? ? 1 >> ? ? 5 ? ? 2 ? LEU ? ? ? HA ? ?H ? ? ?4.25 ? ? 0.02 ? ? 1 >> ? ? 6 ? ? 2 ? LEU ? ? ? HB2 ? H ? ? ?1.54 ? ? 0.02 ? ? 1 >> ? ? 7 ? ? 3 ? ASP ? ? ? H ? ? H ? ? ?8.10 ? ? 0.02 ? ? 1 >> ? ? 8 ? ? 3 ? ASP ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 >> ? ? 9 ? ? 3 ? ASP ? ? ? HB2 ? H ? ? ?2.65 ? ? 0.02 ? ? 1 >> stop >> >> >> now what i want that instead of copying all the lines it will just write >> the information about PHE and ASP then how i acn do that using python >> programming.Kindly tell me the command for that. > try something like: > > for line in open("filename").readlines(): > if (re.search("PHE|ASP",line): > print line > The readlines() is unnecessary, and if your file is at all long you'd be better off precompiling the regular expression. import re expr = re.compile("PHE|ASP") with open("filename") as f: for line in f: if expr.search(line): print line -- Rhodri James *-* Wildebeest Herder to the Masses From milesck at umich.edu Tue Jul 14 14:24:06 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Tue, 14 Jul 2009 14:24:06 -0400 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: <4A5CC86A.9070907@lbl.gov> References: <4A5CC86A.9070907@lbl.gov> Message-ID: <388B397B-40B2-4A4E-857E-49C842708F7F@umich.edu> On Jul 14, 2009, at 2:03 PM, weafon wrote: > Hi guys, > > I have a question about the usage of yield. As shown in the below > example, in general, if there is a code segment commonly used by two > or more functions, we may isolate the segment into a function and > then call it from other functions if necessary. > > def func1(): > .... > while(cond): > ..... > commoncode() > ... > > > def func2(): > .... > while(cond): > ..... > commoncode() > ... > > def commoncode() > AAAA > BBBB > CCCC > > However, if there is a 'yield' operation in the common code segment, > the isolation causes that func1 and func2 become a non-generator > function!! Although I can prevent such an isolation by just > duplicating the segment in func1 and func2 to keep both of them > being generator functions, the code may become ugly and hard to > maintain particularly when coomoncode() is long. > > The problem may be resolved if I can define the commoncode() as an > inline function or marco. Unfortunately, inline and marco do not > seems to be implemented in python. Thus, how can I isolate a common > segment into a function when there are yield operations in the > common segment? def func1(): ... while cond: ... for x in commoncode(): yield x ... See also: - PEP 380: http://www.python.org/dev/peps/pep-0380/ - Stackless: http://www.stackless.com/ -Miles From pfeldman at verizon.net Tue Jul 14 14:25:08 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Tue, 14 Jul 2009 11:25:08 -0700 (PDT) Subject: missing 'xor' Boolean operator Message-ID: <24485116.post@talk.nabble.com> Current Boolean operators are 'and', 'or', and 'not'. It would be nice to have an 'xor' operator as well. -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24485116.html Sent from the Python - python-list mailing list archive at Nabble.com. From stefan_ml at behnel.de Tue Jul 14 14:26:30 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 14 Jul 2009 20:26:30 +0200 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and what negatives or positives it has ? Java, pays a living. *duck* Stefan From http Tue Jul 14 14:47:08 2009 From: http (Paul Rubin) Date: 14 Jul 2009 11:47:08 -0700 Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> Message-ID: <7xab37t843.fsf@ruckus.brouhaha.com> Mensanator writes: > - unlimited precision integers > - easy to program > - IDE not required > - reasonable speed > - math library needs to include number theoretic functions > like GCD, LCM, Modular Inverse, etc. > - not fucking retarded like F# Have you looked at Haskell? > As for negatives, the GMP library doesn't factor. Maybe with some MIRACL bindings... From dickinsm at gmail.com Tue Jul 14 14:47:41 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 14 Jul 2009 11:47:41 -0700 (PDT) Subject: missing 'xor' Boolean operator References: Message-ID: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" wrote: > Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to > have an 'xor' operator as well. Hmm. I don't think 'nice' is sufficient. You'd need to make the case that it's sufficiently useful to justify adding a new keyword 'xor' to the language; I suspect that would be an uphill struggle. :) I'll just note that: (1) It's easy to emulate xor: 'x xor y' <-> bool(x) != bool(y) (2) 'and' and 'or' are special in that they have useful short- circuiting behaviour; xor doesn't have this property (that is, you always need to evaluate *both* operands to determine the result). I'd also guess that 'xor' would be much less used than 'and' or 'or', but maybe that's just a reflection of the sort of code that I tend to write. -- Mark From amrita at iisermohali.ac.in Tue Jul 14 15:10:32 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Wed, 15 Jul 2009 00:40:32 +0530 (IST) Subject: select lines in python Message-ID: <29449.210.212.36.65.1247598632.squirrel@www.iisermohali.ac.in> Can i become more precise like instead of printing all lines for PHE and ASP is it possible that for PHE python will print only those lines which will have information about H and HA and for ASP it will print those lines which will have information about HA and HB. Thanks > On Tue, 14 Jul 2009 18:48:06 +0100, David Gibb wrote: > > [Something top-posted, which I've shuffled down] > >> On Tue, Jul 14, 2009 at 1:33 PM, wrote: >>> Dear all, >>> >>> Can anyone tell me that suppose i have a file having content like: >>> >>> ?? ??_Atom_name >>> ?? ?? ??_Atom_type >>> ?? ?? ??_Chem_shift_value >>> ?? ?? ??_Chem_shift_value_error >>> ?? ?? ??_Chem_shift_ambiguity_code >>> ?? ?? 1 ?? ?? 1 ?? PHE ?? ?? ?? H ?? ?? H ?? ?? ??8.49 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 2 ?? ?? 1 ?? PHE ?? ?? ?? HA ?? ??H ?? ?? ??4.60 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 3 ?? ?? 1 ?? PHE ?? ?? ?? CA ?? ??C ?? ?? 57.83 ?? ?? ??0.3 ?? ?? >>> 1 >>> ?? ?? 4 ?? ?? 2 ?? LEU ?? ?? ?? H ?? ?? H ?? ?? ??8.23 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 5 ?? ?? 2 ?? LEU ?? ?? ?? HA ?? ??H ?? ?? ??4.25 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 6 ?? ?? 2 ?? LEU ?? ?? ?? HB2 ?? H ?? ?? ??1.54 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 7 ?? ?? 3 ?? ASP ?? ?? ?? H ?? ?? H ?? ?? ??8.10 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 8 ?? ?? 3 ?? ASP ?? ?? ?? HA ?? ??H ?? ?? ??4.52 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 9 ?? ?? 3 ?? ASP ?? ?? ?? HB2 ?? H ?? ?? ??2.65 ?? ?? 0.02 ?? ?? >>> 1 >>> stop >>> >>> >>> now what i want that instead of copying all the lines it will just >>> write >>> the information about PHE and ASP then how i acn do that using python >>> programming.Kindly tell me the command for that. > >> try something like: >> >> for line in open("filename").readlines(): >> if (re.search("PHE|ASP",line): >> print line >> > > The readlines() is unnecessary, and if your file is at all long you'd > be better off precompiling the regular expression. > > import re > > expr = re.compile("PHE|ASP") > with open("filename") as f: > for line in f: > if expr.search(line): > print line > > -- > Rhodri James *-* Wildebeest Herder to the Masses > -- > http://mail.python.org/mailman/listinfo/python-list > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From invalid at invalid Tue Jul 14 15:12:53 2009 From: invalid at invalid (Grant Edwards) Date: Tue, 14 Jul 2009 14:12:53 -0500 Subject: select lines in python References: Message-ID: On 2009-07-14, amrita at iisermohali.ac.in wrote: > Can i become more precise like instead of printing all lines > for PHE and ASP is it possible that for PHE python will print > only those lines which will have information about H and HA > and for ASP it will print those lines which will have > information about HA and HB? Yes. -- Grant Edwards grante Yow! I KAISER ROLL?! at What good is a Kaiser Roll visi.com without a little COLE SLAW on the SIDE? From knny.myer at gmail.com Tue Jul 14 15:15:57 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 12:15:57 -0700 (PDT) Subject: http://tinypic.com/view.php?pic=m78cgp&s=3 Message-ID: Hi, I'm experiencing a strange behaviour of the Python prompt when using the four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of getting the previous and next command respectively I get ugly characters. See it yourself: http://tinypic.com/view.php?pic=m78cgp&s=3 This is not directly Python-specific, but you feel quite handicapped if you must rewrite each command again... so my obvious question is: How can I fix this? Please, let me know if you can tell me something. Additional information: I'm running Ubuntu Linux I've tried the python prompt in several shell environments and got the same issue in csh, dash... all negative. Cheers, Kenny From knny.myer at gmail.com Tue Jul 14 15:20:11 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 12:20:11 -0700 (PDT) Subject: bad behaviour in interactive Python prompt Message-ID: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> Hi, I'm experiencing a strange behaviour of the Python prompt when using the four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of getting the previous and next command respectively I get ugly characters. See it yourself: http://tinypic.com/view.php?pic=m78cgp&s=3 This is not directly Python-specific, but you feel quite handicapped if you must rewrite each command again... so my obvious question is: How can I fix this? Please, let me know if you can tell me something. Additional information: I'm running Ubuntu Linux I've tried the python prompt in several shell environments and got the same issue in csh, dash... all negative. Cheers, Kenny From dickinsm at gmail.com Tue Jul 14 15:26:19 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 14 Jul 2009 12:26:19 -0700 (PDT) Subject: bad behaviour in interactive Python prompt References: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> Message-ID: <722961de-8416-4aff-a1e2-ce0e18c65503@k1g2000yqf.googlegroups.com> On Jul 14, 8:20?pm, "~km" wrote: > I'm experiencing a strange behaviour of the Python prompt when using > the > four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of > getting > the previous and next command respectively I get ugly characters. See > it > yourself:http://tinypic.com/view.php?pic=m78cgp&s=3 > > This is not directly Python-specific, but you feel quite handicapped > if > you must rewrite each command again... so my obvious question is: > > How can I fix this? Please, let me know if you can tell me something. > > Additional information: > I'm running Ubuntu Linux > I've tried the python prompt in several shell environments and got the > same issue in csh, dash... all negative. Where did your version of Python 2.6 come from? If you built your copy of Python 2.6 from source, then the problem is probably that either the readline library is missing, or (much more likely) the include files for the readline library are missing. Look for a package called something like libreadline5-dev or readline-devel and install it, and then try rebuilding Python. Mark From degibb at gmail.com Tue Jul 14 15:26:29 2009 From: degibb at gmail.com (David Gibb) Date: Tue, 14 Jul 2009 15:26:29 -0400 Subject: select lines in python In-Reply-To: References: Message-ID: I think what Grant is saying is that you should read the documentation for the re module. David On Tue, Jul 14, 2009 at 3:12 PM, Grant Edwards wrote: > On 2009-07-14, amrita at iisermohali.ac.in wrote: > >> Can i become more precise like instead of printing all lines >> for PHE and ASP is it possible that for PHE python will print >> only those lines which will have information about H and HA >> and for ASP it will print those lines which will have >> information about HA and HB? > > Yes. > > -- > Grant Edwards ? ? ? ? ? ? ? ? ? grante ? ? ? ? ? ? Yow! I KAISER ROLL?! > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?at ? ? ? ? ? ? ? What good is a Kaiser Roll > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? visi.com ? ? ? ? ? ?without a little COLE SLAW > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? on the SIDE? > -- > http://mail.python.org/mailman/listinfo/python-list > From clp2 at rebertia.com Tue Jul 14 15:43:42 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 12:43:42 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> On Tue, Jul 14, 2009 at 11:47 AM, Mark Dickinson wrote: > On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" > wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to >> have an 'xor' operator as well. > > Hmm. ?I don't think 'nice' is sufficient. ?You'd need to make the case > that it's sufficiently useful to justify adding a new keyword 'xor' to > the language; ?I suspect that would be an uphill struggle. :) > > I'll just note that: > > (1) It's easy to emulate xor: ?'x xor y' <-> bool(x) != bool(y) Using the xor bitwise operator is also an option: bool(x) ^ bool(y) Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Tue Jul 14 15:50:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 12:50:13 -0700 Subject: http://tinypic.com/view.php?pic=m78cgp&s=3 In-Reply-To: References: Message-ID: <50697b2c0907141250k6f44fc8buda78c22a0a1989b@mail.gmail.com> On Tue, Jul 14, 2009 at 12:15 PM, ~km wrote: > Hi, > > I'm experiencing a strange behaviour of the Python prompt when using > the > four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of > getting > the previous and next command respectively I get ugly characters. See > it > yourself: > http://tinypic.com/view.php?pic=m78cgp&s=3 > > This is not directly Python-specific, but you feel quite handicapped > if > you must rewrite each command again... so my obvious question is: > > How can I fix this? Please, let me know if you can tell me something. I would guess that your Python wasn't compiled with GNU readline support. Do you get an error if you `import readline`? Cheers, Chris -- http://blog.rebertia.com From dickinsm at gmail.com Tue Jul 14 15:52:34 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 14 Jul 2009 12:52:34 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Jul 14, 8:43?pm, Chris Rebert wrote: > On Tue, Jul 14, 2009 at 11:47 AM, Mark Dickinson wrote: > > (1) It's easy to emulate xor: ?'x xor y' <-> bool(x) != bool(y) > > Using the xor bitwise operator is also an option: > bool(x) ^ bool(y) Good point. For some reason I expected bitwise operations on bools to return ints rather than bools. Now I know better. :-) Thanks, Mark From pfeldman at verizon.net Tue Jul 14 15:56:02 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Tue, 14 Jul 2009 12:56:02 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: <24486661.post@talk.nabble.com> != does do what I want, except that it doesn't indicate to someone reading the code that the operands are being treated as logicals. (Readability is supposed to be one of the major selling points of Python). But, this is probably good enough. Here's a related issue: I would like to see an option for type checking on operands of logical operators, so that attempting to apply a logical operator to non-Boolean entities generates a warning message. With operand type checking, 'xor' and != would be different. Mark Dickinson wrote: > > On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" > wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice >> to >> have an 'xor' operator as well. > > Hmm. I don't think 'nice' is sufficient. You'd need to make the case > that it's sufficiently useful to justify adding a new keyword 'xor' to > the language; I suspect that would be an uphill struggle. :) > > I'll just note that: > > (1) It's easy to emulate xor: 'x xor y' <-> bool(x) != bool(y) > > (2) 'and' and 'or' are special in that they have useful short- > circuiting behaviour; xor doesn't have this property (that is, you > always need to evaluate *both* operands to determine the result). > > I'd also guess that 'xor' would be much less used than 'and' or 'or', > but maybe that's just a reflection of the sort of code that I tend to > write. > > -- > Mark > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24486661.html Sent from the Python - python-list mailing list archive at Nabble.com. From jgardner at jonathangardner.net Tue Jul 14 16:05:48 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Tue, 14 Jul 2009 13:05:48 -0700 (PDT) Subject: one-time factory in python for an experienced java guy References: Message-ID: On Jul 14, 7:03?am, phonky wrote: > > Now, I do not know yet how the account number scheme looks like. Exactly. The data store knows a lot more than the client (your program) will ever know. The correct answer is to do nothing. Use your data store to generate the IDs for you. The implementations discussed here will not generate unique IDs across invocations, but the data store will persist the sequence for you appropriately. The more correct answer is to abstract away the client to your data store as well. See SQLAlchemy. If you're writing a data store, you're doing it wrong. See PostgreSQL, MySQL, or any other data store out there that are perfectly fine for development and production use. I like to use UUIDs for the IDs. Others like big ints that are a sequence. I've seen people use encrypted big ints, basically random strings that aren't really random. In the end, you only have to change the code that talks to the data store, and the rest of your program only cares about the equality of the id. From clp2 at rebertia.com Tue Jul 14 16:22:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 13:22:20 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <24486661.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: <50697b2c0907141322y3ec27688g4e6bc29d8fc4b7bf@mail.gmail.com> > Mark Dickinson wrote: >> >> On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" >> wrote: >>> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice >>> to >>> have an 'xor' operator as well. >> >> Hmm. ?I don't think 'nice' is sufficient. ?You'd need to make the case >> that it's sufficiently useful to justify adding a new keyword 'xor' to >> the language; ?I suspect that would be an uphill struggle. :) >> >> I'll just note that: >> >> (1) It's easy to emulate xor: ?'x xor y' <-> bool(x) != bool(y) >> >> (2) 'and' and 'or' are special in that they have useful short- >> circuiting behaviour; xor doesn't have this property (that is, you >> always need to evaluate *both* operands to determine the result). >> >> I'd also guess that 'xor' would be much less used than 'and' or 'or', >> but maybe that's just a reflection of the sort of code that I tend to >> write. On Tue, Jul 14, 2009 at 12:56 PM, Dr. Phillip M. Feldman wrote: > Here's a related issue: I would like to see an option for type checking on > operands of logical operators, so that attempting to apply a logical > operator to non-Boolean entities generates a warning message. With operand > type checking, 'xor' and != would be different. That's probably not gonna happen. Python is dynamically and duck typed, and has a sweet coercion system (__bool__ or __nonzero__ depending on your version) to coerce non-booleans to booleans in a sensible and useful way. So, it's not gonna change any time soon. Some illustrative examples: >>> #empty containers considered false >>> bool([] or {} or set() or "") >>> False >>> #zero and null considered false >>> bool(0 or None) >>> False >>> #anything else is, by default, true >>> bool(object()) >>> True And like I said, a class can override a special method to provide whatever boolean semantics are desired. It's surprisingly handy. Cheers, Chris -- http://blog.rebertia.com From jgardner at jonathangardner.net Tue Jul 14 16:24:29 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Tue, 14 Jul 2009 13:24:29 -0700 (PDT) Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: <681fe49e-7063-47c5-b0c5-d5088ab216c1@g31g2000yqc.googlegroups.com> On Jul 14, 6:56?am, Fred Atkinson wrote: > > Agreed, it doesn't. ?But if my hosting provider won't change it, I'm > stuck with it. ? > Nowadays you can find hosts that allow you to run FastCGI scripts in any language for dirt cheap. (Hostmonster for instance.) If your host doesn't allow it, it's time to upgrade. Any hosting service that supports Ruby on Rails supports Python through the exact same mechanism. From aahz at pythoncraft.com Tue Jul 14 16:30:43 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 13:30:43 -0700 Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, Stefan Behnel wrote: >Deep_Feelings wrote: >> >> So you have chosen programming language "x" so shall you tell us why >> you did so , and what negatives or positives it has ? > >*duck* Where do you get the duck programming language? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From jakub.piotr.nowak at gmail.com Tue Jul 14 16:39:28 2009 From: jakub.piotr.nowak at gmail.com (Jakub P. Nowak) Date: Tue, 14 Jul 2009 13:39:28 -0700 (PDT) Subject: ANN: RuPy '09 Conference Message-ID: RuPy 2009 :: Strongly Dynamic Conference Poznan, Poland November 7-8, 2009 :: Call for speakers RuPy is a conference about dynamically typed programming languages. Held for the first time in April 2007 it gathered enthusiasts from Poland and other countries. The idea behind the conference is to liven up the community and make these technologies more popular in Europe. The first conference was a middle-sized one but came out as a huge success. We believe it was a great experience for both attendees and speakers. RuPy 2009 committee is looking for speakers willing to present a Python, Ruby, Groovy or any other related subject. If you have an interesting talk in mind go ahead and submit a talk proposal. The talk proposal should include a talk title, brief introduction and your short resume/biography. To our invited speakers we offer free accommodation, full board and possibility to interact with a very lively IT community. You are also free to participate in all the extra events - 'Geek party' always turns out great! You can also support us by linking to our site and informing all your friends about the event and the possibility to submit a talk proposal. Potential speakers should submit an abstract using the following form: https://spreadsheets.google.com/viewform?formkey=dEFEYndsQWR2TGFzRFpzcU9IbFlCT2c6MA.. by September 30th, 2009 for consideration. If you have any special presentation needs, let us know. For more details check our site: http://www.rupy.eu Best regards, -- Jakub P. Nowak RuPy Committee From suruti94 at gmail.com Tue Jul 14 16:40:38 2009 From: suruti94 at gmail.com (Mohan Parthasarathy) Date: Tue, 14 Jul 2009 13:40:38 -0700 Subject: Calling functions: Why this complicated ? Message-ID: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> Hi, I am a newbie. I am reading http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html Defining a function with "N" arguments and calling them in "M" different ways. Why does it have to be this complicated ? I like the idea of calling the function by explicitly naming the arguments, but there are so many other ways here that is very confusing. Do people really use all these features ? Perhaps, there is a equivalent book to "Javascript: Good Parts" for Python ? -thanks mohan -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Tue Jul 14 16:45:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 22:45:46 +0200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> <7d161f1f-2c09-42ca-9bbd-b99a777044ea@j21g2000vbn.googlegroups.com> Message-ID: >>>>> Carl Banks (CB) wrote: >CB> On Jul 14, 4:48?am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >>> In message <93f6a517-63d8-4c80- >>> >>> bf19-4614b7099... at m7g2000prd.googlegroups.com>, Carl Banks wrote: >>> > Or would you rather let all unexpected exceptions print to standard >>> > error, which is often a black hole in non-interactive sitations? >>> >>> Since when? >>> >>> Cron, for example, collects standard error and mails it to you. >CB> 1. Cron is only one way to run programs non-interactively. (Would that >CB> work with, say, Windows services?) >CB> 2. Many systems don't run MTAs these days >CB> 3. In my experience the pipeline from cron to mail delivery is error >CB> prone, mainly due to the complexity of configuring MTAs >CB> 4. Even if all this works, you might just want to do your logging >CB> directly in Python anyway Even then, I think the program would get structured better if the FTP code would only catch FTP-specific errors, including socket errors (which is what all_errors does) and to catch programming errors etc on another level, e.g by the mentioned sys.excepthook. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From deets at nospam.web.de Tue Jul 14 16:59:04 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 14 Jul 2009 22:59:04 +0200 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: References: Message-ID: <7c49soF25pgh4U1@mid.uni-berlin.de> weafon schrieb: > Hi guys, > > I have a question about the usage of yield. As shown in the below > example, in general, if there is a code segment commonly used by two or > more functions, we may isolate the segment into a function and then call > it from other functions if necessary. > > def func1(): > .... > while(cond): > ..... > commoncode() > ... > > > def func2(): > .... > while(cond): > ..... > commoncode() > ... > > def commoncode() > AAAA > BBBB > CCCC > > However, if there is a 'yield' operation in the common code segment, the > isolation causes that func1 and func2 become a non-generator function!! No. Not writing them as generators makes them a non-generator-function. You are way to unspecific with your examples. But if func1 and func2 are themselves supposed to be generators, there is nothing preventing you from using them as such. Diez From robert.kern at gmail.com Tue Jul 14 16:59:32 2009 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 14 Jul 2009 15:59:32 -0500 Subject: missing 'xor' Boolean operator In-Reply-To: <24486661.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: > != does do what I want, except that it doesn't indicate to someone reading > the code that the operands are being treated as logicals. (Readability is > supposed to be one of the major selling points of Python). But, this is > probably good enough. In the words of those greater than myself, "Not every one-liner needs to be in the standard library." def xor(a, b): return bool(a) != bool(b) -- 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 piet at cs.uu.nl Tue Jul 14 16:59:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 22:59:34 +0200 Subject: The meaning of "=" References: Message-ID: >>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >A> In article , Piet van Oostrum >A> wrote: >>> And to get c.x = 4 working you also need a __setitem__. >A> Nope. You do need __setitem__ so that this works: >A> c['x'] = 4 Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because that was what the OP wanted (I think). -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From db3l.net at gmail.com Tue Jul 14 17:06:03 2009 From: db3l.net at gmail.com (David Bolen) Date: Tue, 14 Jul 2009 17:06:03 -0400 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: John Nagle writes: > Python 3 enforces the rule that you can't mix tabs and spaces > for indentation in the same file. That (finally) guarantees that > the indentation you see is what the Python parser sees. That's > enough to prevent non-visible indentation errors. Are you sure? It seems to restrict them in the same block, but not in the entire file. At least I was able to use both space and tab indented blocks in the same file with Python 3.0 and 3.1. I suspect precluding any mixture at all at the file level would be more intrusive, for example, when trying to combine multiple code sources in a single file. Not that this really changes your final point, since the major risk of a mismatch between the parser vs. visual display is within a single block. > It also means that the Python parser no longer has to have > any concept of how many spaces equal a tab. So the problem > is now essentially solved. "has to have" being a future possibility at this point, since I'm fairly sure the 3.x parser does technically still have the concept of a tab size of 8, though now it can be an internal implementation detail. -- David From ethan at stoneleaf.us Tue Jul 14 17:38:12 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 14:38:12 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: <4A5CFAC4.7020300@stoneleaf.us> Robert Kern wrote: > On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: > >> != does do what I want, except that it doesn't indicate to someone >> reading >> the code that the operands are being treated as logicals. >> (Readability is >> supposed to be one of the major selling points of Python). But, this is >> probably good enough. > > > In the words of those greater than myself, "Not every one-liner needs to > be in the standard library." > > def xor(a, b): > return bool(a) != bool(b) > Let's see... and returns the last object that is "true" or returns the first object that is "true" so should xor return the only object that is "true", else False/None? def xor(a, b) if a and b: return None elif a: return a elif b: return b else: return None ~Ethan~ From nobody at nowhere.com Tue Jul 14 17:40:27 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 14 Jul 2009 22:40:27 +0100 Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 02:06:04 -0300, Gabriel Genellina wrote: >> Matt, how many words are you looking for, in how long a string ? >> Were you able to time any( substr in long_string ) against re.compile >> ( "|".join( list_items )) ? > > There is a known algorithm to solve specifically this problem > (Aho-Corasick), a good implementation should perform better than R.E. (and > better than the gen.expr. with the advantage of returning WHICH string > matched) Aho-Corasick has the advantage of being linear in the length of the patterns, so the setup may be faster than re.compile(). The actual searching won't necessarily be any faster (assuming optimal implementations; I don't know how safe that assumption is). From ken at seehart.com Tue Jul 14 17:44:51 2009 From: ken at seehart.com (Ken Seehart) Date: Tue, 14 Jul 2009 14:44:51 -0700 Subject: decorators - would be nice if... Message-ID: <4A5CFC53.7050902@seehart.com> Almost every time I use decorators, I find myself wishing I had access to the local namespace of the context from which the decorator is executed. In practice, decorator is being applied to a method, so the namespace in question would be the dictionary of the class being created. Similarly, before decorators were around, I often found myself lamenting the fact that a class is not created until after it's scope is completed, since this makes certain kinds of metaprogramming more difficult. But after digging deeper, I realized why it is the way it is, so I don't complain about that. But it would have been a simple thing to add an argument 'locals' to the decorator specification. It's somewhat more difficult to add that now because of compatibility issues, but perhaps there is a way to get the same effect. I can work around all of this by making my decorators just store data to be processed by a metaclass, but that solution is less elegant. Also, it has the slight disadvantage of requiring the use of a metaclass which would otherwise not be necessary. I prefer to save metaclass implementation as a last resort for various reasons (e.g. there does not seem to be a good way to use multiple metaclasses in a class hierarchy, and more generally you can't stack them on top of each other (you can only have one metaclass per class)). Thoughts? - Ken From knny.myer at gmail.com Tue Jul 14 17:47:04 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 14:47:04 -0700 (PDT) Subject: bad behaviour in interactive Python prompt References: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> <722961de-8416-4aff-a1e2-ce0e18c65503@k1g2000yqf.googlegroups.com> Message-ID: <21a14a9e-2837-4ae1-a55b-c6b3d50c0513@k19g2000yqn.googlegroups.com> On 14 Jul., 15:26, Mark Dickinson wrote: > Where did your version of Python 2.6 come from? > > If you built your copy of Python 2.6 from source, then the problem is > probably that either the readline library is missing, or (much more > likely) the include files for the readline library are missing. ?Look > for a package called something like libreadline5-dev or readline-devel > and install it, and then try rebuilding Python. > > Mark Yes, I built it from source and never thought that there might be such a library. Now I've re-compiled and my problem is solved! Have many thanks Mark, and Chris Rebert, too, who responded to my deleted post, which I've received per mail. Sorry for that incidence. Cheers, Kenny From knny.myer at gmail.com Tue Jul 14 17:47:17 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 14:47:17 -0700 (PDT) Subject: bad behaviour in interactive Python prompt References: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> <722961de-8416-4aff-a1e2-ce0e18c65503@k1g2000yqf.googlegroups.com> Message-ID: On 14 Jul., 15:26, Mark Dickinson wrote: > Where did your version of Python 2.6 come from? > > If you built your copy of Python 2.6 from source, then the problem is > probably that either the readline library is missing, or (much more > likely) the include files for the readline library are missing. ?Look > for a package called something like libreadline5-dev or readline-devel > and install it, and then try rebuilding Python. > > Mark Yes, I built it from source and never thought that there might be such a library. Now I've re-compiled and my problem is solved! Have many thanks Mark, and Chris Rebert, too, who responded to my deleted post, which I've received per mail. Sorry for that incidence. Cheers, Kenny From python at mrabarnett.plus.com Tue Jul 14 17:58:27 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 22:58:27 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5CFAC4.7020300@stoneleaf.us> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> Message-ID: <4A5CFF83.9070301@mrabarnett.plus.com> Ethan Furman wrote: > Robert Kern wrote: >> On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: >> >>> != does do what I want, except that it doesn't indicate to someone >>> reading >>> the code that the operands are being treated as logicals. >>> (Readability is >>> supposed to be one of the major selling points of Python). But, this is >>> probably good enough. >> >> >> In the words of those greater than myself, "Not every one-liner needs >> to be in the standard library." >> >> def xor(a, b): >> return bool(a) != bool(b) >> > > Let's see... > > and returns the last object that is "true" > or returns the first object that is "true" > > so should xor return the only object that is "true", else False/None? > > def xor(a, b) > if a and b: > return None > elif a: > return a > elif b: > return b > else: > return None > How about: def xor(a, b): return not b and a or not a and b From nobody at nowhere.com Tue Jul 14 17:58:28 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 14 Jul 2009 22:58:28 +0100 Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> <7xab37t843.fsf@ruckus.brouhaha.com> Message-ID: On Tue, 14 Jul 2009 11:47:08 -0700, Paul Rubin wrote: >> - unlimited precision integers >> - easy to program >> - IDE not required >> - reasonable speed >> - math library needs to include number theoretic functions >> like GCD, LCM, Modular Inverse, etc. >> - not fucking retarded like F# > > Have you looked at Haskell? Given his comment about F#, I have a suspicion that he might be dogmatically opposed to functional languages generally. From deets at nospam.web.de Tue Jul 14 18:03:03 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 00:03:03 +0200 Subject: decorators - would be nice if... In-Reply-To: References: Message-ID: <7c4dknF253b3tU1@mid.uni-berlin.de> Ken Seehart schrieb: > Almost every time I use decorators, I find myself wishing I had access > to the local namespace of the context from which the decorator is > executed. In practice, decorator is being applied to a method, so the > namespace in question would be the dictionary of the class being created. You can access the instance. def decorator(method): def _d(self, *args, **kwargs): print self.__dict__ return method(self, *args, **kwargs) return _d class Foo(object): @decorator def bar(self, a, b): print "bar" f = Foo() f.bar(1, 2) So what exactly it is you are missing? The method's locals()? And could you explain *why* you are missing this? Diez From ethan at stoneleaf.us Tue Jul 14 18:11:40 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 15:11:40 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5CFF83.9070301@mrabarnett.plus.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> Message-ID: <4A5D029C.5030700@stoneleaf.us> MRAB wrote: > Ethan Furman wrote: > >> Robert Kern wrote: >> >>> On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: >>> >>>> != does do what I want, except that it doesn't indicate to someone >>>> reading >>>> the code that the operands are being treated as logicals. >>>> (Readability is >>>> supposed to be one of the major selling points of Python). But, >>>> this is >>>> probably good enough. >>> >>> >>> >>> In the words of those greater than myself, "Not every one-liner needs >>> to be in the standard library." >>> >>> def xor(a, b): >>> return bool(a) != bool(b) >>> >> >> Let's see... >> >> and returns the last object that is "true" >> or returns the first object that is "true" >> >> so should xor return the only object that is "true", else False/None? >> >> def xor(a, b) >> if a and b: >> return None >> elif a: >> return a >> elif b: >> return b >> else: >> return None >> > How about: > > def xor(a, b): > return not b and a or not a and b In [12]: not 1 and 0 or 1 and not 0 Out[12]: True In [13]: not 0 and 0 or 0 and not 0 Out[13]: 0 In [14]: not 1 and 1 or 1 and not 1 Out[14]: False In [15]: not [] and [] or [] and not [] Out[15]: [] Doesn't produce consistent objects, sometimes bool, sometimes something else. 'Course, it all depends on what you need. ~Ethan~ From pavlovevidence at gmail.com Tue Jul 14 18:26:06 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 15:26:06 -0700 (PDT) Subject: decorators - would be nice if... References: Message-ID: On Jul 14, 2:44?pm, Ken Seehart wrote: [snip] (e.g. there does not > seem to be a good way to use multiple metaclasses in a class hierarchy, > and more generally you can't stack them on top of each other (you can > only have one metaclass per class)). > > Thoughts? If "stacking" metaclasses (whatever that means) is a possible approach to your problem, you might want to ask yourself if you're trying to be too clever. Would you like to give us an example of a decorator you think would benefit from having access to local namespace? We might be able to suggest other possibilities. Carl Banks From darkwater42 at gmail.com Tue Jul 14 18:54:31 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 15:54:31 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <4a5c5e7f$0$30235$9b4e6d93@newsspool1.arcor-online.net> Message-ID: On Jul 14, 7:38?am, Florian Brucker wrote: > Douglas Alan wrote: > > Thank you. My question wasn't intended to be Python specific, though. > > I am just curious for purely academic reasons about whether there is > > such an algorithm. All the sources I've skimmed only seem to the > > answer the question via omission. Which is kind of strange, since it > > seems to me like an obvious question to ask. > IIRC comp.programming would be the place to ask such questions. Thanks, yes that does look like a good place to post such questions. Unfortunately, it also looks to be overrun with stories on "hot girls top and bottom sexy movie", though I'm sure I can ignore those. I'm scared to look at the posting on "tricky bit twiddling", though. |>ouglas From aahz at pythoncraft.com Tue Jul 14 18:58:06 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 15:58:06 -0700 Subject: The meaning of "=" References: Message-ID: In article , Piet van Oostrum wrote: >>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: > >>A> In article , Piet van Oostrum >>A> wrote: > >>>> And to get c.x = 4 working you also need a __setitem__. > >>A> Nope. You do need __setitem__ so that this works: > >>A> c['x'] = 4 > >Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because >that was what the OP wanted (I think). c.x = 4 already updates the instance dict, so there's no need to change any class methods to support it. That is, IME it's much better to add methods to a regular class to make it more dict-like using the built-in instance dict rather than changing any of the attribute mechanisms. If you're really curious, I recommend trying several approaches yourself to see what works better. ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From Scott.Daniels at Acm.Org Tue Jul 14 19:00:03 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 16:00:03 -0700 Subject: How to check if any item from a list of strings is in a big string? In-Reply-To: References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: <3cCdna9IneyskcDXnZ2dnUVZ_g6dnZ2d@pdx.net> Nobody wrote: > On Tue, 14 Jul 2009 02:06:04 -0300, Gabriel Genellina wrote: > >>> Matt, how many words are you looking for, in how long a string ? >>> Were you able to time any( substr in long_string ) against re.compile >>> ( "|".join( list_items )) ? >> There is a known algorithm to solve specifically this problem >> (Aho-Corasick), a good implementation should perform better than R.E. (and >> better than the gen.expr. with the advantage of returning WHICH string >> matched) > > Aho-Corasick has the advantage of being linear in the length of the > patterns, so the setup may be faster than re.compile(). The actual > searching won't necessarily be any faster (assuming optimal > implementations; I don't know how safe that assumption is). > Having done a fast Aho-Corasick implementation myself, I can assure you that the actual searching can be incredibly fast. RE conversion usually goes to a slightly more general machine than the Aho-Corasick processing requires. --Scott David Daniels Scott.Daniels at Acm.Org From dstanek at dstanek.com Tue Jul 14 19:03:02 2009 From: dstanek at dstanek.com (David Stanek) Date: Tue, 14 Jul 2009 19:03:02 -0400 Subject: How to unbuffer Python's output In-Reply-To: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> References: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> Message-ID: 2009/7/14 Lily Gao : > Hi, All > > I am calling a python program in perl and use redirection, > > Like : > > `python x.py > 1.log 2>&1` Try tihs instead: python x.py 2>&1 > 1.log -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From freyr.magnusson at gmail.com Tue Jul 14 19:07:09 2009 From: freyr.magnusson at gmail.com (Freyr) Date: Tue, 14 Jul 2009 16:07:09 -0700 (PDT) Subject: Passing handlers between bound c++ libs Message-ID: <3e7b0717-a665-4f42-b7bd-7a99d59258c4@m11g2000yqh.googlegroups.com> I have a python bound physics library that uses handler to process events. The passing of handlers between c++ and python causes a huge overhead slowing down the process. Can I implement a handler in my custom python bound c++ lib B and pass it to blackbox python bound c++ lib A so that A would call B directly without passing through the python layer? Or to phrase the question differently: Can I pass a handler from B through python to A so that A will invoke B with out calling into python code? From lists at cheimes.de Tue Jul 14 19:08:24 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 15 Jul 2009 01:08:24 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: Chris Rebert wrote: > Using the xor bitwise operator is also an option: > bool(x) ^ bool(y) I prefer something like: bool(a) + bool(b) == 1 It works even for multiple tests (super xor): if bool(a) + bool(b) + bool(c) + bool(d) != 1: raise ValueError("Exactly one of a, b, c and d must be true") Christian From darkwater42 at gmail.com Tue Jul 14 19:08:36 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 16:08:36 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: <95f25380-d71f-41b6-82b8-f991551e8a1a@32g2000yqj.googlegroups.com> On Jul 14, 8:10?am, Piet van Oostrum wrote: > Of course you can take any BST algorithm and replace pointers by indices > in the array and allocate new elements in the array. But then you need > array elements to contain the indices for the children explicitely. And why is this a problem? This is how binary heaps are typically implemented, and it all works swimmingly. The node rotations for keeping a binary heap balanced turn out to be suitable for representation in a flat array. I.e., when you do the node rotations, you only ever have to copy log n array elements. In general, however, you can't move nodes around so easily when represented in a flat array. A node movement in a tree represented with pointers, might involves changing just two pointers, while when represented as a flat array, might involve copying most of the array to maintain the tree invariants. It just so turns out that maintaining the invariants for a binary heap does not have this issue. This is why I was curious about treaps, which are a type of binary heap. The CLRS textbook on algorithms discusses treaps, but doesn't ever mention whether they are as fortunate as less constrained binary heaps. I'm sure I could work out for myself whether the treap rotations are suitable for storage in a flat array, but I might make a mistake somewhere in my reasoning, and then never know the true answer! |>ouglas From Scott.Daniels at Acm.Org Tue Jul 14 19:17:35 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 16:17:35 -0700 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: Aahz wrote: > In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, > Stefan Behnel wrote: >> Deep_Feelings wrote: >>> So you have chosen programming language "x" so shall you tell us why >>> you did so , and what negatives or positives it has ? >> *duck* > > Where do you get the duck programming language? It shares a type system with Python, of course. :-) --Scott David Daniels Scott.Daniels at Acm.Org From jackdied at gmail.com Tue Jul 14 19:23:31 2009 From: jackdied at gmail.com (Jack Diederich) Date: Tue, 14 Jul 2009 19:23:31 -0400 Subject: decorators - would be nice if... In-Reply-To: <4A5CFC53.7050902@seehart.com> References: <4A5CFC53.7050902@seehart.com> Message-ID: On Tue, Jul 14, 2009 at 5:44 PM, Ken Seehart wrote: > Almost every time I use decorators, I find myself wishing I had access > to the local namespace of the context from which the decorator is > executed. ?In practice, decorator is being applied to a method, so the > namespace in question would be the dictionary of the class being created. As other mentioned, an example would make it clearer what you are trying to do. > Similarly, before decorators were around, I often found myself lamenting > the fact that a class is not created until after it's scope is > completed, since this makes certain kinds of metaprogramming more > difficult. ?But after digging deeper, I realized why it is the way it > is, so I don't complain about that. Py3k to the rescue! Because the metaclass is defined outside of the class body it is possible to pass in a metaclass that uses a custom dictionary. This was added to make some common cases easier (like knowing in which order members were defined). This is not backportable to 2.6 because the __metaclass__ is defined inside the class body and possibly anywhere in the class body. -Jack From darkwater42 at gmail.com Tue Jul 14 19:23:58 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 16:23:58 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: <5e8d271a-e3e7-451b-92ca-b5321da9b706@o6g2000yqj.googlegroups.com> On Jul 14, 9:19?am, Scott David Daniels wrote: > It may well be that there is no good simple solution, and people avoid > writing about non-existent algorithms. I can't imagine why that should be the case. The CLRS textbook on algorithms, for instance, goes to some pains to mathematically prove that there is no comparison sort that can operate in faster than O(n log n) time. And any decent discussion of rabies would be sure to mention that there is no known cure. CLRS talks about binary heaps, binary search trees, and treaps, and it shows how to maintain a binary heap in a flat array efficiently (n log n time overall), but it never even seems to bring up the subject as to whether a binary search tree or a treap can also be efficiently maintained in a flat array. Though it may be the case that these questions are left as exercises for the student, and therefore buried in the exercises and problem sets that I didn't read carefully. > Piet van Oostrum wrote: > > Of course you can take any BST algorithm and replace pointers by indices > > in the array and allocate new elements in the array. But then you need > > array elements to contain the indices for the children explicitely. > And you loower your locality of reference (cache-friendliness). > Note the insert in Python, for example, is quite cache-friendly. I can't see that a binary search tree would typically have particularly good cache-friendliness, so I can't see why a flat-array representation, such as is done for a binary heap, would have particularly worse cache-reference. |>ouglas From clp2 at rebertia.com Tue Jul 14 19:31:25 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 16:31:25 -0700 Subject: Calling functions: Why this complicated ? In-Reply-To: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> Message-ID: <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> On Tue, Jul 14, 2009 at 1:40 PM, Mohan Parthasarathy wrote: > Hi, > I am a newbie. I am reading > http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html > Defining a function with "N" arguments and calling them in "M" different > ways. Why does it have to be this complicated ? I like the idea of calling > the function by explicitly naming the arguments, but there are so many other > ways here that is very confusing. Do people really use all these features ? > Perhaps, there is a equivalent book to "Javascript: Good Parts" for Python ? Oh $DEITY, don't even compare Python to JavaScript. At least in Python, when you try to access a non-existent attribute, a proper NameError exception is thrown rather than silently getting back "undefined"... (*has traumatic horror story flashback*) The calling syntax is not so much complicated as it is liberating. Are you a C junkie who has never heard of named arguments? Just use the call sequence like you've always done. Are you a exacting Smalltalk or Objective-C person who likes to name all the arguments all the time for clarity? You can do that. Do you want to call a function with lots of default arguments but only want to override a couple of them? Specifying them by name lets you do that succinctly. Do you not want to have to worry about the order of the parameters because it seems kinda arbitrary? Then specify the arguments by name. etc... And if you try and specify an argument twice, Python will throw an error, so anything truly confusing will get caught right away. And there's only one definition syntax, so while the call could be complicated, figuring out what it means by looking to the definition is fairly easy. There really aren't that many ways it's done in practice. In practice, the following styles cover 90% of cases: - All named arguments: foo(bar=a, baz=b, qux=c) - All sequential arguments: foo(a, b, c) - All sequential arguments, with a few optional arguments given by name: foo(a, b, c, flag=True, style=qux) - Simple pass-through: foo(*args, **kwargs) Granted, there's 4 of them, but each taken by itself seems pretty easy to read, IMHO. Cheers, Chris -- http://blog.rebertia.com From darkwater42 at gmail.com Tue Jul 14 19:33:33 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 16:33:33 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <95f25380-d71f-41b6-82b8-f991551e8a1a@32g2000yqj.googlegroups.com> Message-ID: <01ea9a41-5885-4df3-91ae-24cfa5bfbec4@y17g2000yqn.googlegroups.com> I wrote: > On Jul 14, 8:10?am, Piet van Oostrum wrote: > > > Of course you can take any BST algorithm and replace pointers by indices > > in the array and allocate new elements in the array. But then you need > > array elements to contain the indices for the children explicitely. > And why is this a problem? Oh, I'm sorry -- I see what you are saying now. You're saying you can just implement a normal binary search tree, but store the tree nodes in an array, as if it were a chunk of memory, and use array indices as pointers, rather than using memory addresses as pointers. Fair enough, but I'm not sure what that would buy you. Other than, perhaps some improved locality of reference, and the potential to perhaps get the pointers take up less space if you know the array is never going to grow to be very large. |>ouglas From Scott.Daniels at Acm.Org Tue Jul 14 19:36:35 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 16:36:35 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: <06edna4zh5xcicDXnZ2dnUVZ_tKdnZ2d@pdx.net> Ethan Furman wrote: > and returns the last object that is "true" A little suspect this. _and_ returns the first object that is not "true," or the last object. > or returns the first object that is "true" Similarly: _or_ returns the first object that is "true," or the last object. > so should xor return the only object that is "true", else False/None? Xor has the problem that in two cases it can return neither of its args. Not has behavior similar in those cases, and we see it returns False or True. The Pythonic solution is therefore to use False. > def xor(a, b) > if a and b: > return None > elif a: > return a > elif b: > return b > else: > return None def xor(a, b): if bool(a) == bool(b): return False else: return a or b Side-effect counting in applications of bool(x) is ignored here. If minimizing side-effects is needed: def xor(a, b): if a: if not b: return a elif b: return b return False --Scott David Daniels Scott.Daniels at Acm.Org From ethan at stoneleaf.us Tue Jul 14 19:48:26 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 16:48:26 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <4A5D194A.6050301@stoneleaf.us> Christian Heimes wrote: > Chris Rebert wrote: > >>Using the xor bitwise operator is also an option: >>bool(x) ^ bool(y) > > > I prefer something like: > > bool(a) + bool(b) == 1 > > It works even for multiple tests (super xor): > > if bool(a) + bool(b) + bool(c) + bool(d) != 1: > raise ValueError("Exactly one of a, b, c and d must be true") > > Christian > super_xor! I like it! In [23]: def super_xor(args, failure=False): ....: found_one = False ....: for item in args: ....: if item: ....: if found_one: ....: return failure ....: else: ....: found_one = item ....: return found_one or failure In [25]: super_xor((0, 1, 0, [])) Out[25]: 1 In [26]: super_xor((0, 1, 0, [],('this',))) Out[26]: False In [27]: super_xor((0, {}, 0, [],())) Out[27]: False In [16]: def super_or(args, failure=False): ....: for item in args: ....: if item: ....: return item ....: else: ....: return failure ....: In [17]: super_or((None, [], 0, 3, {})) Out[17]: 3 In [18]: super_or((None, [], 0, (), {})) Out[18]: False In [19]: def super_and(args, failure=False): ....: for item in args: ....: if not item: ....: return failure ....: else: ....: return item ....: In [20]: super_and((1, 2, 3)) Out[20]: 3 In [21]: super_and((1, 2, 3, 4)) Out[21]: 4 In [22]: super_and((1, 0, 3, 4)) Out[22]: False A whole family of supers. :) ~Ethan~ From ethan at stoneleaf.us Tue Jul 14 19:53:37 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 16:53:37 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <06edna4zh5xcicDXnZ2dnUVZ_tKdnZ2d@pdx.net> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <06edna4zh5xcicDXnZ2dnUVZ_tKdnZ2d@pdx.net> Message-ID: <4A5D1A81.5000406@stoneleaf.us> Scott David Daniels wrote: > Ethan Furman wrote: > >> and returns the last object that is "true" > > A little suspect this. > _and_ returns the first object that is not "true," or the last object. > >> or returns the first object that is "true" > > Similarly: > _or_ returns the first object that is "true," or the last object. > Thanks for the correction. ~Ethan~ > > --Scott David Daniels > Scott.Daniels at Acm.Org From magawake at gmail.com Tue Jul 14 20:11:49 2009 From: magawake at gmail.com (Mag Gam) Date: Tue, 14 Jul 2009 20:11:49 -0400 Subject: compiling python Message-ID: <1cbd6f830907141711s4b6dbbe6gd563a3e63fc53b4@mail.gmail.com> At my university we are trying to compile python with --enable-shared however when I do a make many things fail. Is it a good idea to compile python with shared libraries? It seems mod-python needs it this way. TIA From keith at nekotaku.com Tue Jul 14 20:29:43 2009 From: keith at nekotaku.com (Alagalah) Date: Tue, 14 Jul 2009 17:29:43 -0700 (PDT) Subject: Reading floats from Excel using COM Message-ID: <04a50999-3934-4235-b047-5135d8c71c94@m11g2000yqh.googlegroups.com> Hi, I am using the Range function to return a tuple from Excel. The data in the range (nREVENUE) in the excel file is 100.0, 101.0, 102.0, 103.0, 104.0 I can successfully iterate across the tuple and list, but when I try and cast to a float to do some math, I get: File "C:\Python25\lib\site-packages\win32com\client\__init__.py", line 454, in __getattr__ raise AttributeError, "'%s' object has no attribute '%s'" % (repr (self), attr) AttributeError: '' object has no attribute '__float__' **** CODE ***** import win32com.client import os excel=win32com.client.Dispatch("Excel.Application") excel.Visible=0 excel.DisplayAlerts=False #This is the file that contains the macro xlsname=os.path.join(os.getcwd(),"nametest.xlsx") nametest=excel.Workbooks.Open(xlsname) revenue_list=excel.Range("nREVENUE") revenue=[] revenue.extend(revenue_list) for i in range(len(revenue)): rev=revenue[i] print float(rev) excel.Quit() del excel **************** I need to use COM as I will eventually need formula support, otherwise I would use xlutils. Any advice on how to cast the elements to a float would be greatly appreciated! From greg at cosc.canterbury.ac.nz Tue Jul 14 20:32:25 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 15 Jul 2009 12:32:25 +1200 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <7c4mb4F261slsU1@mid.individual.net> Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and what negatives or positives it has ? This summarises my reasons for choosing Python fairly well: http://www1.american.edu/cas/econ/faculty/isaac/choose_python.pdf -- Greg From suruti94 at gmail.com Tue Jul 14 20:42:55 2009 From: suruti94 at gmail.com (Mohan Parthasarathy) Date: Tue, 14 Jul 2009 17:42:55 -0700 Subject: Calling functions: Why this complicated ? In-Reply-To: <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> Message-ID: <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> Chris, Thanks for your clarifications > > I am a newbie. I am reading > > http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html > > Defining a function with "N" arguments and calling them in "M" different > > ways. Why does it have to be this complicated ? I like the idea of > calling > > the function by explicitly naming the arguments, but there are so many > other > > ways here that is very confusing. Do people really use all these features > ? > > Perhaps, there is a equivalent book to "Javascript: Good Parts" for > Python ? > > Oh $DEITY, don't even compare Python to JavaScript. At least in > Python, when you try to access a non-existent attribute, a proper > NameError exception is thrown rather than silently getting back > "undefined"... (*has traumatic horror story flashback*) I did not try to compare python to Javascript. Just because there are ten different ways of doing certain things, not all of them may be used. Over a period of time, people tend to use certain features more and more. Javascript is a different beast where some of the features need to be avoided for writing good programs. I don't know anything about python. But it is possible that there is a subset that people use frequently which may be sufficient to write good programs. Sure, that would not help me the python interview test :-) > > The calling syntax is not so much complicated as it is liberating. > Are you a C junkie who has never heard of named arguments? > Just use the call sequence like you've always done. > Are you a exacting Smalltalk or Objective-C person who likes to name > all the arguments all the time for clarity? > You can do that. That's where I fit. I did not expect to see more than that :-) > Do you want to call a function with lots of default arguments but only > want to override a couple of them? > Specifying them by name lets you do that succinctly. > Do you not want to have to worry about the order of the parameters > because it seems kinda arbitrary? > Then specify the arguments by name. > etc... > > And if you try and specify an argument twice, Python will throw an > error, so anything truly confusing will get caught right away. > And there's only one definition syntax, so while the call could be > complicated, figuring out what it means by looking to the definition > is fairly easy. > > There really aren't that many ways it's done in practice. In practice, > the following styles cover 90% of cases: > - All named arguments: foo(bar=a, baz=b, qux=c) > - All sequential arguments: foo(a, b, c) > - All sequential arguments, with a few optional arguments given by > name: foo(a, b, c, flag=True, style=qux) - Simple pass-through: foo(*args, **kwargs) > > Granted, there's 4 of them, but each taken by itself seems pretty easy > to read, IMHO. So, all four of them above has its use cases in practice i guess. thanks mohan > > Cheers, > Chris > -- > http://blog.rebertia.com > -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Tue Jul 14 20:44:12 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 17:44:12 -0700 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <7c4mb4F261slsU1@mid.individual.net> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <7c4mb4F261slsU1@mid.individual.net> Message-ID: <50697b2c0907141744q61489a81t111d4545828d2b09@mail.gmail.com> On Tue, Jul 14, 2009 at 5:32 PM, greg wrote: > Deep_Feelings wrote: >> >> So you have chosen programming language "x" so shall you tell us why >> you did so , and ?what negatives or positives it has ? > > This summarises my reasons for choosing Python > fairly well: > > http://www1.american.edu/cas/econ/faculty/isaac/choose_python.pdf +1 PDF Of The Week - Chris -- http://blog.rebertia.com From martin.hellwig at dcuktec.org Tue Jul 14 20:50:44 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Wed, 15 Jul 2009 01:50:44 +0100 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: Aahz wrote: > In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, > Stefan Behnel wrote: >> Deep_Feelings wrote: >>> So you have chosen programming language "x" so shall you tell us why >>> you did so , and what negatives or positives it has ? >> *duck* > > Where do you get the duck programming language? Probably floated around in the same C. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From sjmachin at lexicon.net Tue Jul 14 21:05:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 15 Jul 2009 01:05:16 +0000 (UTC) Subject: Reading floats from Excel using COM References: <04a50999-3934-4235-b047-5135d8c71c94@m11g2000yqh.googlegroups.com> Message-ID: Alagalah nekotaku.com> writes: > > Hi, I am using the Range function to return a tuple from Excel. [big snip] Exact duplicate of question asked by "KB" on http://groups.google.com/group/python-excel ... see my answer there. From lawson89 at gmail.com Tue Jul 14 21:34:05 2009 From: lawson89 at gmail.com (Rick Lawson) Date: Tue, 14 Jul 2009 18:34:05 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java Message-ID: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> Appreciate any help on this. I am porting an app from Java to python and need generic object pooling with hooks for object initialization / cleanup and be able to specify an object timeout. Thanks ! Rick From cmpython at gmail.com Tue Jul 14 21:52:32 2009 From: cmpython at gmail.com (Che M) Date: Tue, 14 Jul 2009 18:52:32 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <7ba1ea2b-0886-4972-abb3-5dd77a95aa1e@d4g2000yqa.googlegroups.com> On Jul 14, 11:55?am, Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and ?what negatives or positives it has ? As a hobbyist--and not a real programmer*--I think I chose Python (I don't really recall now) because of things like: - There is a good community, including tutor list, etc. - I wanted something that would run cross-platform (so no Visual Basic). - Preferred something free (so no REALBasic). - I wanted a high level language. - It seemed easiest to read and learn. I like mandatory indents, for example. - There are a lot of libraries for it with good support. - I liked the Zen of Python philosophy (to the extent I understood it). - Some high profile entities were using it, so it must have something going for it. - It was new enough to be a possible improvement over older languages while old enough to be established. - Who can't like a computer language named Python? Drawbacks: - AFAIK, running multiple Python apps all show up in the Windows task manager as "python.exe" (common to all non-compiled languages?) instead of the app's name. - Distributing as an executable is not as straightforward as I might hope. - I wish Python 3 wouldn't break my 2.5 code. - If it could be quicker (compiled), that would be better. (But haven't tried psyco or Shed Skin, etc.) - I've found understanding web programming hard, but that might be just the nature of web programming and not a Python thing. - I wish wxPython had a more complete rich text editor (but overall it is great). CM From http Tue Jul 14 22:36:22 2009 From: http (Paul Rubin) Date: 14 Jul 2009 19:36:22 -0700 Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <5e8d271a-e3e7-451b-92ca-b5321da9b706@o6g2000yqj.googlegroups.com> Message-ID: <7xab36oeop.fsf@ruckus.brouhaha.com> Douglas Alan writes: > I can't see that a binary search tree would typically have > particularly good cache-friendliness, so I can't see why a flat-array > representation, such as is done for a binary heap, would have > particularly worse cache-reference. That is a good point. Maybe we should be paying more attention to cache-oblivious algorithms. http://en.wikipedia.org/wiki/Cache-oblivious_algorithm H. Prokop's masters' thesis cited in the wiki article explains the subject very well. A fair amount of work has been done on it since then, but not as much as one might expect. From davea at dejaviewphoto.com Tue Jul 14 22:51:25 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Tue, 14 Jul 2009 22:51:25 -0400 Subject: explode() In-Reply-To: <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: <4A5D442D.2040600@dejaviewphoto.com> > The one thing I really dislike about Python over PHP is that > Python can usually only appear in the cgi directory (unless other > arragements are made with your hosting provider or if you reconfigure > Apache on your own server if you have your own). With PHP, I can put > them in any folder on my Web site without any problem. > > Regards, > > > > > Fred > > You should talk to your hosting provider. With mine, all you need do differently for directories other than cgi-bin is to do a chmod on the file to make it executable. Have you tried that? Is it hosted on Unix or Windows? DaveA From jmcmonagle at NO.SPAM.velseis.com.au Tue Jul 14 23:06:18 2009 From: jmcmonagle at NO.SPAM.velseis.com.au (John McMonagle) Date: Wed, 15 Jul 2009 13:06:18 +1000 Subject: Tkinter / Entry widget problem In-Reply-To: References: Message-ID: <4A5D47AA.7090007@NO.SPAM.velseis.com.au> Andras Szabo wrote: > Hello. I searched the archives but couldn't find a solution to a problem > related to the Entry widget in Tkinter. > > When creating a pop-up window in an app, which contains an Entry widget, > I want this widget to contain some default string, to have all this > default string selected (as if the user had manually selected > everything), and to have the focus transferred to this widget. > > (The idea is then that if the window pops up, the user won't have to > click or press Tab any more before being able to type what is needed in > the textbox, overwriting what is written there already.) > > I thought this might be the way to go: > > entrybox=Entry(toplevel_parent_window) > entrybox.insert(0,"Some default string") > entrybox.select_range(0,END) > entrybox.focus_set() > entrybox.pack() > > But it doesn't seem to work - the focus is not transferred to the Entry > widget, and the text does not appear to be selected (even though after > this entrybox.selection_present() returns True). > > What am I doing wrong? > > andras You're probably not updating after the focus_set. Try the following: from Tkinter import * r = Tk() def click(): t = Toplevel(r) e = Entry(t) e.pack() b = Button(t, text='Close', command=t.destroy) b.pack() e.insert(0, 'Default') e.select_range(0, END) e.focus_set() r.update() b = Button(r, text='Press', command=click) b.pack() r.mainloop() Regards, John From davea at ieee.org Tue Jul 14 23:32:21 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 14 Jul 2009 23:32:21 -0400 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: <4A5CC86A.9070907@lbl.gov> References: <4A5CC86A.9070907@lbl.gov> Message-ID: <4A5D4DC5.1090202@ieee.org> weafon wrote: >
Hi guys, > > I have a question about the usage of yield. As shown in the below > example, in general, if there is a code segment commonly used by two > or more functions, we may isolate the segment into a function and then > call it from other functions if necessary. > > def func1(): > .... > while(cond): > ..... > commoncode() > ... > > > def func2(): > .... > while(cond): > ..... > commoncode() > ... > > def commoncode() > AAAA > BBBB > CCCC > > However, if there is a 'yield' operation in the common code segment, > the isolation causes that func1 and func2 become a non-generator > function!! Although I can prevent such an isolation by just > duplicating the segment in func1 and func2 to keep both of them being > generator functions, the code may become ugly and hard to maintain > particularly when coomoncode() is long. > > The problem may be resolved if I can define the commoncode() as an > inline function or marco. Unfortunately, inline and marco do not seems > to be implemented in python. Thus, how can I isolate a common segment > into a function when there are yield operations in the common segment? > > Thanks, > Weafon > You are implying there's something special or unique about yield in this. Return has the same problem, and many other flow control constructs. Also, variable definitions and scoping. So you can't just copy any old bunch of adjacent lines out of two functions, put it into a third, and call it factoring. Give us a specific example you're puzzled about, and we can try to solve it. DaveA From pfeldman at verizon.net Wed Jul 15 00:07:14 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Tue, 14 Jul 2009 21:07:14 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <4A5CFF83.9070301@mrabarnett.plus.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> Message-ID: <24491580.post@talk.nabble.com> I appreciate the effort that people have made, but I'm not impressed with any of the answers. For one thing, xor should be able to accept an arbitrary number of input arguments (not just two), and should return True if and only if the number of input arguments that evaluate to True is odd (see www.mathworld.com article on xor). Here's my code: def xor(*args): """xor accepts an arbitrary number of input arguments, returning True if and only if bool() evaluates to True for an odd number of the input arguments.""" result= False for arg in args: if bool(arg): result= not result return result MRAB-2 wrote: > > Ethan Furman wrote: >> Robert Kern wrote: >>> On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: >>> >>>> != does do what I want, except that it doesn't indicate to someone >>>> reading >>>> the code that the operands are being treated as logicals. >>>> (Readability is >>>> supposed to be one of the major selling points of Python). But, this >>>> is >>>> probably good enough. >>> >>> >>> In the words of those greater than myself, "Not every one-liner needs >>> to be in the standard library." >>> >>> def xor(a, b): >>> return bool(a) != bool(b) >>> >> >> Let's see... >> >> and returns the last object that is "true" >> or returns the first object that is "true" >> >> so should xor return the only object that is "true", else False/None? >> >> def xor(a, b) >> if a and b: >> return None >> elif a: >> return a >> elif b: >> return b >> else: >> return None >> > How about: > > def xor(a, b): > return not b and a or not a and b > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24491580.html Sent from the Python - python-list mailing list archive at Nabble.com. From milesck at umich.edu Wed Jul 15 01:08:42 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 01:08:42 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <24491580.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> <24491580.post@talk.nabble.com> Message-ID: <9F28E417-53B9-4CFE-A880-965B85903B34@umich.edu> On Jul 15, 2009, at 12:07 AM, Dr. Phillip M. Feldman wrote: > I appreciate the effort that people have made, but I'm not impressed > with any > of the answers. For one thing, xor should be able to accept an > arbitrary > number of input arguments (not just two) You originally proposed this in the context of the existing short- circuit boolean operators. Those operators (being infix) take only two operands. > and should return True if and only > if the number of input arguments that evaluate to True is odd The existing and/or operators always return the value of one of the operands--not necessarily True or False--which is another important property, but one that can't be translated fully to xor. Given the lack of context in your original post, hopefully you'll forgive me being unimpressed by your not being impressed. :) > Here's my code: > > def xor(*args): > """xor accepts an arbitrary number of input arguments, returning > True > if and only if bool() evaluates to True for an odd number of the > input > arguments.""" > > result= False > > for arg in args: > if bool(arg): result= not result > > return result If all you want is a True or False result, I'd write it like this: import operator def xor(*args): return reduce(operator.xor, map(bool, args)) # or imap In order to make it act more like the other logical operators, I'd use MRAB's 2-argument xor as the reducer--though I can't really see the utility. def xor2(a, b): return (not b and a) or (not a and b) def xor(*args): return reduce(xor2, args) You may also find this question interesting: http://stackoverflow.com/questions/432842/ -Miles From amrita at iisermohali.ac.in Wed Jul 15 02:13:06 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Wed, 15 Jul 2009 11:43:06 +0530 (IST) Subject: one more question Message-ID: <20300.210.212.36.65.1247638386.squirrel@www.iisermohali.ac.in> Dear all, Just one more thing i want to ask that suppose i have a file like:--- 47 8 ALA H H 7.85 0.02 1 48 8 ALA HA H 2.98 0.02 1 49 8 ALA HB H 1.05 0.02 1 50 8 ALA C C 179.39 0.3 1 51 8 ALA CA C 54.67 0.3 1 52 8 ALA CB C 18.85 0.3 1 53 8 ALA N N 123.95 0.3 1 107 15 ALA H H 8.05 0.02 1 108 15 ALA HA H 4.52 0.02 1 now what i want that i will make another file in which first it will write the position of ALA lets say 8 then its name ALA and then the chemical shift value for only three atoms C,CA and CB. Means it will be someting like: 8 ALA C = 179.39 CA = 54.67 CB = 18.85 I tried but its not coming. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From mensanator at aol.com Wed Jul 15 02:21:21 2009 From: mensanator at aol.com (Mensanator) Date: Tue, 14 Jul 2009 23:21:21 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> <7xab37t843.fsf@ruckus.brouhaha.com> Message-ID: On Jul 14, 1:47?pm, Paul Rubin wrote: > Mensanator writes: > > - unlimited precision integers > > - easy to program > > - IDE not required > > - reasonable speed > > - math library needs to include number theoretic functions > > ? like GCD, LCM, Modular Inverse, etc. > > - not fucking retarded like F# > > Have you looked at Haskell? > > > As for negatives, the GMP library doesn't factor. > > Maybe with some MIRACL bindings... That's the factoring program (factor.exe from the MIRACL package) I made reference to. I don't know how to fix the bug nor how to bind it to Python. What I do (or did at one time) know is how figure out how to recompile it, change the output to be database compatible, consistent messages rather than such useless messages as "this number is prime". The Python program, as it captures the StdOut, can watch for the bug. The bug is that factor.exe occasionally gets stuck on a composite while deep into the factoring process. Sometimes, however, this getting stuck can be resolved if you send the composite back to the beginning and start over. The factor.exe program isn't smart enough to try this, it simply returns COMPOSITE amongst the PRIME_FACTORS. The calling Python program can then collect the unfactored composites and call factor.exe again with each of them sending them back to the start of the factoring. If they successfully factor on the second pass, Python then appends them to the factors from the first pass to achieve a complete factorization that factor.exe can produce in theory but not in practice. From mensanator at aol.com Wed Jul 15 02:35:15 2009 From: mensanator at aol.com (Mensanator) Date: Tue, 14 Jul 2009 23:35:15 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> <7xab37t843.fsf@ruckus.brouhaha.com> Message-ID: <4b2e00a3-d09c-4c9a-bcfb-8c89af262963@i6g2000yqj.googlegroups.com> On Jul 14, 4:58?pm, Nobody wrote: > On Tue, 14 Jul 2009 11:47:08 -0700, Paul Rubin wrote: > >> - unlimited precision integers > >> - easy to program > >> - IDE not required > >> - reasonable speed > >> - math library needs to include number theoretic functions > >> ? like GCD, LCM, Modular Inverse, etc. > >> - not fucking retarded like F# > > > Have you looked at Haskell? > > Given his comment about F#, I have a suspicion that he might be > dogmatically opposed to functional languages generally. Not dogmatically opposed, I installed it because I actually wanted to try functional progrsmming. My mistake was listening to that Harrop dude who didn't bother to explain that F# is part of a government program that provides jobs for retards. To wit: F# has a rational data type, that's cool. But wait, F# has TWO rational data types. Huh? Well, after they made the first, some jackass came up with a second type that stores data more efficiently. And, of course, the second type doesn't have the same functionality as the first, so you need both. And of the myriad data type converion methods, guess which possible conversion methods are conspicuous by their absense? That's right, type1 <==> type2 rational conversions don't exist. At that point, I decided I wasn't going to put up crap like that and threw it away. From timr at probo.com Wed Jul 15 02:43:10 2009 From: timr at probo.com (Tim Roberts) Date: Tue, 14 Jul 2009 23:43:10 -0700 Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: "Dr. Phillip M. Feldman" wrote: > >Here's a related issue: I would like to see an option for type checking on >operands of logical operators, so that attempting to apply a logical >operator to non-Boolean entities generates a warning message. With operand >type checking, 'xor' and != would be different. How would you define "Boolean entities"? Do you mean the True and False values? Such a change would break virtually every Python program ever written. In any case, this idea is dead in the water. It would break a whole bunch of existing code from before the conditional operator: xxx = testme and truevalue or falsevalue -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From wuwei23 at gmail.com Wed Jul 15 02:50:08 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 14 Jul 2009 23:50:08 -0700 (PDT) Subject: one more question References: Message-ID: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> amr... at iisermohali.ac.in wrote: > I tried but its not coming. How much are you prepared to pay for help with this? Or are you just asking us to do all the work for you? From wuwei23 at gmail.com Wed Jul 15 02:56:57 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 14 Jul 2009 23:56:57 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> Message-ID: On Jul 15, 4:50?pm, alex23 wrote: > amr... at iisermohali.ac.in wrote: > > I tried but its not coming. > > How much are you prepared to pay for help with this? Or are you just > asking us to do all the work for you? Or to be a _little_ less blunt: if you want people here to _assist_ you with _your_ code, then post what you've tried here, along with tracebacks if errors are occurring, or an explanation as to what it isn't doing that you require. Spawning a new thread restating your question every time someone suggests you RTFM isn't the best way to show that you're actually trying to solve this issue yourself. From clp2 at rebertia.com Wed Jul 15 03:05:14 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 00:05:14 -0700 Subject: one more question In-Reply-To: <20300.210.212.36.65.1247638386.squirrel@www.iisermohali.ac.in> References: <20300.210.212.36.65.1247638386.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907150005q30917bf1i88cde982b54a3bdf@mail.gmail.com> On Tue, Jul 14, 2009 at 11:13 PM, wrote: > > Dear all, > > Just one more thing i want to ask that suppose i have a file like:--- > > > ?47 ? ? 8 ? ALA ? ? ? H ? ? H ? ? ?7.85 ? ? 0.02 ? ? 1 > ?48 ? ? 8 ? ALA ? ? ? HA ? ?H ? ? ?2.98 ? ? 0.02 ? ? 1 > ?49 ? ? 8 ? ALA ? ? ? HB ? ?H ? ? ?1.05 ? ? 0.02 ? ? 1 > ?50 ? ? 8 ? ALA ? ? ? C ? ? C ? ?179.39 ? ? ?0.3 ? ? 1 > ?51 ? ? 8 ? ALA ? ? ? CA ? ?C ? ? 54.67 ? ? ?0.3 ? ? 1 > ?52 ? ? 8 ? ALA ? ? ? CB ? ?C ? ? 18.85 ? ? ?0.3 ? ? 1 > ?53 ? ? 8 ? ALA ? ? ? N ? ? N ? ?123.95 ? ? ?0.3 ? ? 1 > 107 ? ?15 ? ALA ? ? ? H ? ? H ? ? ?8.05 ? ? 0.02 ? ? 1 > 108 ? ?15 ? ALA ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > > now what i want that i will make another file in which first it will write > the position of ALA lets say 8 then its name ALA and then the chemical > shift value for only three atoms C,CA and CB. > > Means it will be someting like: > > 8 ?ALA ?C = 179.39 ?CA = 54.67 ?CB = 18.85 > > I tried but its not coming. Surely a colleague at IISER Mohali's Computer Science department should be able to help you? Cheers, Chris -- http://blog.rebertia.com From koranthala at gmail.com Wed Jul 15 03:24:52 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 00:24:52 -0700 (PDT) Subject: one more question References: Message-ID: <19b3cb31-643c-47f4-9153-52e15289e933@24g2000yqm.googlegroups.com> On Jul 15, 11:13?am, amr... at iisermohali.ac.in wrote: > Dear all, > > Just one more thing i want to ask that suppose i have a file like:--- > > ?47 ? ? 8 ? ALA ? ? ? H ? ? H ? ? ?7.85 ? ? 0.02 ? ? 1 > ?48 ? ? 8 ? ALA ? ? ? HA ? ?H ? ? ?2.98 ? ? 0.02 ? ? 1 > ?49 ? ? 8 ? ALA ? ? ? HB ? ?H ? ? ?1.05 ? ? 0.02 ? ? 1 > ?50 ? ? 8 ? ALA ? ? ? C ? ? C ? ?179.39 ? ? ?0.3 ? ? 1 > ?51 ? ? 8 ? ALA ? ? ? CA ? ?C ? ? 54.67 ? ? ?0.3 ? ? 1 > ?52 ? ? 8 ? ALA ? ? ? CB ? ?C ? ? 18.85 ? ? ?0.3 ? ? 1 > ?53 ? ? 8 ? ALA ? ? ? N ? ? N ? ?123.95 ? ? ?0.3 ? ? 1 > 107 ? ?15 ? ALA ? ? ? H ? ? H ? ? ?8.05 ? ? 0.02 ? ? 1 > 108 ? ?15 ? ALA ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > > now what i want that i will make another file in which first it will write > the position of ALA lets say 8 then its name ALA and then the chemical > shift value for only three atoms C,CA and CB. > > Means it will be someting like: > > 8 ?ALA ?C = 179.39 ?CA = 54.67 ?CB = 18.85 > > I tried but its not coming. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA This is indeed possible and should be quite easy. One problem is that I do not exactly understand the problem - how do you decide which all to join and print? Is it ALA with 4th field as C or 6th field as 0.3? The issue here is that I am not getting the context of your problem. And I have *no* idea about the Chemical shift etc which you are talking about. If you can explain it a little more, I will try something out. Just for the scenario you explained, this code will suffice - f = open('abcd') d = {} for line in f: fields = line.split() if fields[2] == 'ALA': d.setdefault('ALA', {'position':fields[1], 'values':[]}) if fields[4] == 'C': d['ALA']['values'].append({fields[3]:fields[5]}) print d But i dont think this is what you want From piet at cs.uu.nl Wed Jul 15 03:42:15 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 09:42:15 +0200 Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <95f25380-d71f-41b6-82b8-f991551e8a1a@32g2000yqj.googlegroups.com> <01ea9a41-5885-4df3-91ae-24cfa5bfbec4@y17g2000yqn.googlegroups.com> Message-ID: >>>>> Douglas Alan (DA) wrote: >DA> I wrote: >>> On Jul 14, 8:10?am, Piet van Oostrum wrote: >>> >>> > Of course you can take any BST algorithm and replace pointers by indices >>> > in the array and allocate new elements in the array. But then you need >>> > array elements to contain the indices for the children explicitely. >>> And why is this a problem? >DA> Oh, I'm sorry -- I see what you are saying now. You're saying you can >DA> just implement a normal binary search tree, but store the tree nodes >DA> in an array, as if it were a chunk of memory, and use array indices as >DA> pointers, rather than using memory addresses as pointers. >DA> Fair enough, but I'm not sure what that would buy you. Other than, >DA> perhaps some improved locality of reference, and the potential to >DA> perhaps get the pointers take up less space if you know the array is >DA> never going to grow to be very large. My second sentence that you quoted more or less means `it doesn't buy you much'. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From dickinsm at gmail.com Wed Jul 15 03:44:48 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Wed, 15 Jul 2009 00:44:48 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> Message-ID: <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> On Jul 15, 5:07?am, "Dr. Phillip M. Feldman" wrote: > I appreciate the effort that people have made, but I'm not impressed with any > of the answers. ?For one thing, xor should be able to accept an arbitrary > number of input arguments (not just two), and should return True if and only > if the number of input arguments that evaluate to True is odd. Well, that's not exactly what you originally asked for. But it's still a one-liner: def xor(*args): return bool(sum(map(bool, args)) % 2) or perhaps def xor(*args): return bool(len(filter(None, args)) & 1) > Here's my code: > > def xor(*args): > ? ?"""xor accepts an arbitrary number of input arguments, returning True > ? ?if and only if bool() evaluates to True for an odd number of the input > ? ?arguments.""" > > ? ?result= False > > ? ?for arg in args: > ? ? ? if bool(arg): result= not result It's more idiomatic to say "if arg: ..." rather than "if bool (arg): ...". > > ? ?return result Mark From koranthala at gmail.com Wed Jul 15 03:51:23 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 00:51:23 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> Message-ID: <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> On Jul 15, 11:56?am, alex23 wrote: > On Jul 15, 4:50?pm, alex23 wrote: > > > amr... at iisermohali.ac.in wrote: > > > I tried but its not coming. > > > How much are you prepared to pay for help with this? Or are you just > > asking us to do all the work for you? > > Or to be a _little_ less blunt: if you want people here to _assist_ > you with _your_ code, then post what you've tried here, along with > tracebacks if errors are occurring, or an explanation as to what it > isn't doing that you require. > > Spawning a new thread restating your question every time someone > suggests you RTFM isn't the best way to show that you're actually > trying to solve this issue yourself. Alex, I am not sure about it. It doesnt look like she(I guess) is a programmer. In the last thread, she asked the question and the reply was to check up the regular expressions. Now, for a non-programmer, re might be a real confusing and tough subject to grasp. I am not saying that what you said was wrong, only that I felt that she got tense looking up regular expressions. So a python reply which basically does the basic checking without going to re etc might be more helpful for her to start her own coding. From piet at cs.uu.nl Wed Jul 15 04:14:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 10:14:20 +0200 Subject: The meaning of "=" References: Message-ID: >>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >A> In article , Piet van Oostrum wrote: >>>>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >>> >A> In article , Piet van Oostrum >A> wrote: >>> >>>>>> And to get c.x = 4 working you also need a __setitem__. >>> >A> Nope. You do need __setitem__ so that this works: >>> >A> c['x'] = 4 >>> >>> Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because >>> that was what the OP wanted (I think). >A> c.x = 4 >A> already updates the instance dict, so there's no need to change any class >A> methods to support it. That is, IME it's much better to add methods to >A> a regular class to make it more dict-like using the built-in instance >A> dict rather than changing any of the attribute mechanisms. If you're >A> really curious, I recommend trying several approaches yourself to see >A> what works better. ;-) Yes, that's why I mentioned __setitem__. I just mixed up the motivation. In [28]: class AttrDict: ....: def __getitem__(self, key): ....: return getattr(self, key) ....: ....: def __setitem__(self, key, value): ....: setattr(self, key, value) ....: ....: In [29]: c = AttrDict() In [30]: c["y"] = 3 In [31]: c.y Out[31]: 3 In [32]: c.x = 4 In [33]: c['x'] Out[33]: 4 -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From hackingkk at gmail.com Wed Jul 15 04:22:24 2009 From: hackingkk at gmail.com (Krishnakant) Date: Wed, 15 Jul 2009 13:52:24 +0530 Subject: select lines in python In-Reply-To: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> References: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Message-ID: <1247646144.3355.26.camel@krishna-laptop> On Tue, 2009-07-14 at 23:03 +0530, amrita at iisermohali.ac.in wrote: > Dear all, > > Can anyone tell me that suppose i have a file having content like: > > _Atom_name > _Atom_type > _Chem_shift_value > _Chem_shift_value_error > _Chem_shift_ambiguity_code > 1 1 PHE H H 8.49 0.02 1 > 2 1 PHE HA H 4.60 0.02 1 > 3 1 PHE CA C 57.83 0.3 1 > 4 2 LEU H H 8.23 0.02 1 > 5 2 LEU HA H 4.25 0.02 1 > 6 2 LEU HB2 H 1.54 0.02 1 > 7 3 ASP H H 8.10 0.02 1 > 8 3 ASP HA H 4.52 0.02 1 > 9 3 ASP HB2 H 2.65 0.02 1 > stop > > > now what i want that instead of copying all the lines it will just write > the information about PHE and ASP then how i acn do that using python > programming.Kindly tell me the command for that. Dear Amrita, No one will tell you commands for your program. We can give you logic and the way to implement it. It is recommended that you write the code your self. For our problem you keep a list and do a readlines() function and get the content in your list variable. then start looping through that list like for line in lines: where the lines variable is your list. Now keep comparing each line against your expected phrase and the moment you find one, do the actions you need such as writing to a file. happy hacking. Krishnakant. From andi at mozarellasalat.homelinux.net Wed Jul 15 04:24:22 2009 From: andi at mozarellasalat.homelinux.net (Andreas Grommek) Date: Wed, 15 Jul 2009 10:24:22 +0200 Subject: promlems with threading and print Message-ID: <4A5D9236.9060008@mozarellasalat.homelinux.net> Hi Newsgroup, I'm new to python and I am familiarizing myself with threads (haven't done any threading in any other language before...). I was playing around and discovered some weird behavior. Here is my code: import threading from time import sleep from random import random import sys class MyThread(threading.Thread): def __init__(self, t, s): self.threadmarker = t self.sleeptime = s threading.Thread.__init__(self) def run(self): print("Tread", self.threadmarker, "is going to sleep for a while...") sys.stdout.flush() #flush I/O sleep(self.sleeptime) #go to sleep print("Tread", self.threadmarker, "is waking up and terminating") a = 1 b = 20 for n in range(a, b): x = MyThread(n,random()*10.0) x.start() This should create some threads which print messages, go to sleep for a random amount of time (max 10 seconds) and return with a message. When I run the code I get something like this (always different): Tread 1 is going to sleep for a while... Tread 2 is going to sleep for a while... Tread 3 is going to sleep for a while... Tread 4 is going to sleep for a while... Tread 5 is going to sleep for a while... Tread 6 is going to sleep for a while... Tread 6 is going to sleep for a while... Tread 7 is going to sleep for a while... Tread 7 is going to sleep for a while... Tread 7 is going to sleep for a while... Tread 8 is going to sleep for a while... (...) Some "going to sleep" messages appear more than once. If I increase the number of thread the problem gets worse and threads are even started out of order (but this alone would not worry me...). Are some threads startet more than once or is this an issue with print? What else can I do in addition to sys.stdout.flush() after the first print statement? Are print and sleep thread-safe? Or is this a bug (I use python 3.1) Any hints and help for an newbie would be appreciated. Thanks, Andi From p.f.moore at gmail.com Wed Jul 15 04:31:13 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Wed, 15 Jul 2009 09:31:13 +0100 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: <79990c6b0907150131x73370fa3iaa095564fe729a51@mail.gmail.com> 2009/7/15 Scott David Daniels : > Aahz wrote: >> >> In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, >> Stefan Behnel ? wrote: >>> >>> Deep_Feelings wrote: >>>> >>>> So you have chosen programming language "x" so shall you tell us why >>>> you did so , and ?what negatives or positives it has ? >>> >>> *duck* >> >> Where do you get the duck programming language? > > It shares a type system with Python, of course. ?:-) No, it just has a type system with all the same operations as Python, which all do the same things. :-) Paul. From alessiogiovanni.baroni at gmail.com Wed Jul 15 04:41:49 2009 From: alessiogiovanni.baroni at gmail.com (tuxagb) Date: Wed, 15 Jul 2009 01:41:49 -0700 (PDT) Subject: A question of style ..... Message-ID: <430dc862-7c94-4616-a66c-5ed2c033442f@n11g2000yqb.googlegroups.com> If I have to write an extension module with many objects, how can I organizing the code? I think: every object in a separate file, and a last file with the PyInit_..... function. But is unmenageable ..... Solutions? Thanks! From jgardner at jonathangardner.net Wed Jul 15 04:53:51 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Wed, 15 Jul 2009 01:53:51 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> Message-ID: <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> On Jul 14, 6:34?pm, Rick Lawson wrote: > Appreciate any help on this. I am porting an app from Java to python > and need generic object pooling with hooks for object initialization / > cleanup and be able to specify an object timeout. > Are you looking for something like a thread pool or a connection pool? Such a thing is easy to code in Python. Might as well write one from scratch for your particular need. By the way, a tip for writing Python: Forget Java. If you're porting an app, consider rewriting it from the architecture on up. You'll save yourself time and get a better result. From afriere at yahoo.co.uk Wed Jul 15 04:55:33 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Wed, 15 Jul 2009 01:55:33 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> Message-ID: On Jul 15, 5:44?pm, Mark Dickinson wrote: > On Jul 15, 5:07?am, "Dr. Phillip M. Feldman" > wrote: [snip] > > ? ?for arg in args: > > ? ? ? if bool(arg): result= not result > > It's more idiomatic to say "if arg: ..." rather than "if bool > (arg): ...". > Ah yes, but not once conditional tests, (just like logical operators), type-check to ensure they have been supplied with Boolean entities. ;) From steven at REMOVE.THIS.cybersource.com.au Wed Jul 15 04:58:36 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 15 Jul 2009 08:58:36 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 08:53:33 -0700, Carl Banks wrote: > On Jul 14, 2:14?am, Steven D'Aprano > wrote: >> On Tue, 14 Jul 2009 01:30:48 -0700, Carl Banks wrote: >> > Seriously, do you *ever* take more than 2 seconds to consider whether >> > you might be missing something obvious before following up with these >> > indignant knee-jerk responses? >> >> Obviously not. >> >> [...] >> >> > Or would you rather let all unexpected exceptions print to standard >> > error, which is often a black hole in non-interactive sitations? >> >> Fair point. Of course you're right. > > I don't want to be mean or anything. I agree with you on 95% of issues > probably, of course I only follow up to disagree.... No problem Carl. You were right, in this instance I was being a smartarse and ended up being too clever for my own good. There are cases where you want to catch all exceptions, not to hide them, but to process them in some way (possibly logging the error and then exiting). -- Steven From jeremiah.dodds at gmail.com Wed Jul 15 05:01:55 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 15 Jul 2009 10:01:55 +0100 Subject: Calling functions: Why this complicated ? In-Reply-To: <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> Message-ID: <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> On Wed, Jul 15, 2009 at 1:42 AM, Mohan Parthasarathy wrote: > So, all four of them above has its use cases in practice i guess. > > thanks > mohan > As a hopefully semi-informative aside, I've been writing python code for a few years now, and I regularly use all four forms of argument passing listed above. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jgardner at jonathangardner.net Wed Jul 15 05:02:04 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Wed, 15 Jul 2009 02:02:04 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <4c01f2c4-314d-4690-8904-d19dec9f6241@k1g2000yqf.googlegroups.com> On Jul 14, 4:48?pm, Ethan Furman wrote: > > A whole family of supers. ?:) > You should pick up Lisp. The whole concept of a binary operator doesn't exist over there. All the things binary operators can do, Lisp does with 0, 1, 2, or more arguments. [1]> (+) 0 [2]> (+ 1) 1 [3]> (+ 1 2) 3 [4]> (+ 1 2 3) 6 Once you get used to that, binary operators don't seem so useful anymore. The equivalent in Python is dropping the operators and replacing them with built-in functions that take 0, 1, 2, or more arguments. From steven at REMOVE.THIS.cybersource.com.au Wed Jul 15 05:11:44 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 15 Jul 2009 09:11:44 GMT Subject: missing 'xor' Boolean operator References: Message-ID: On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > Current Boolean operators are 'and', 'or', and 'not'. It would be nice > to have an 'xor' operator as well. I've often wished there was too, for the sake of completeness and aesthetics, I'd love to be able to write: a xor b instead of defining a function xor(a, b). Unfortunately, outside of boolean algebra and simulating electrical circuits, I can't think of any use-cases for an xor operator. Do you have any? -- Steven From pdpinheiro at gmail.com Wed Jul 15 05:17:09 2009 From: pdpinheiro at gmail.com (pdpi) Date: Wed, 15 Jul 2009 02:17:09 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> On Jul 15, 12:08?am, Christian Heimes wrote: > Chris Rebert wrote: > > Using the xor bitwise operator is also an option: > > bool(x) ^ bool(y) > > I prefer something like: > > ? ? bool(a) + bool(b) == 1 > > It works even for multiple tests (super xor): > > ? if bool(a) + bool(b) + bool(c) + bool(d) != 1: > ? ? ? raise ValueError("Exactly one of a, b, c and d must be true") > > Christian "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor 1 = 0 xor 1 = 1 if you assicate to the left) From bearophileHUGS at lycos.com Wed Jul 15 05:25:20 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Wed, 15 Jul 2009 02:25:20 -0700 (PDT) Subject: A question of style ..... References: <430dc862-7c94-4616-a66c-5ed2c033442f@n11g2000yqb.googlegroups.com> Message-ID: <07945ef8-769d-4ca5-8142-0112a3a7befc@k6g2000yqn.googlegroups.com> tuxagb: > If I have to write an extension module with many objects, how can I > organizing the code? > I think: every object in a separate file, and a last file with the > PyInit_..... function. But is unmenageable ..... > > Solutions? What do you think about using Cython? Bye, bearophile From tim.wintle at teamrubber.com Wed Jul 15 05:28:34 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Wed, 15 Jul 2009 10:28:34 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: <4c01f2c4-314d-4690-8904-d19dec9f6241@k1g2000yqf.googlegroups.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <4c01f2c4-314d-4690-8904-d19dec9f6241@k1g2000yqf.googlegroups.com> Message-ID: <1247650114.7759.13.camel@tim-laptop> On Wed, 2009-07-15 at 02:02 -0700, Jonathan Gardner wrote: > On Jul 14, 4:48 pm, Ethan Furman wrote: > > > > A whole family of supers. :) > > > All the things binary operators can do, Lisp > does with 0, 1, 2, or more arguments. +1 n-ary operators are great, but binary operators are just syntactic sugar for functions (which are obviously a generalisation of an n-ary operator in python). The fact that lisp-like languages don't expose this sugar is good for making you think about what you're actually doing, but just like mathematicians use binary (and unary) operators in equations rather than working in the lambda calculus, having that sugar is useful to python. > > [1]> (+) > 0 > [2]> (+ 1) > 1 > [3]> (+ 1 2) > 3 > [4]> (+ 1 2 3) > 6 c.f. : >>> sum([]) 0 >>> sum([1]) 1 >>> sum([1,2]) 3 >>> sum([1,2,3]) 6 > Once you get used to that, binary operators don't seem so useful > anymore. > > The equivalent in Python is dropping the operators and replacing them > with built-in functions that take 0, 1, 2, or more arguments. From mail at timgolden.me.uk Wed Jul 15 05:32:55 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 15 Jul 2009 10:32:55 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: References: Message-ID: <4A5DA247.3040606@timgolden.me.uk> Steven D'Aprano wrote: > On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > >> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >> to have an 'xor' operator as well. > > I've often wished there was too, for the sake of completeness and > aesthetics, I'd love to be able to write: > > a xor b > > instead of defining a function xor(a, b). > > Unfortunately, outside of boolean algebra and simulating electrical > circuits, I can't think of any use-cases for an xor operator. Do you have > any? I was pondering on this yesterday, and the only case I've come across in my code -- and it's reasonably common -- is checking that one and only one of two params has been passed. I have code which wants, say, an id or a name but doesn't want both. It's hardly difficult to write the check even now, but an built-in xor would make it fractionally cleaner. TJG From jarausch at igpm.rwth-aachen.de Wed Jul 15 05:53:28 2009 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: Wed, 15 Jul 2009 11:53:28 +0200 Subject: convert Dbase (.dbf) files to SQLite databases Message-ID: <7c5n8kF25pc8bU1@mid.dfncis.de> Hi, I have a lot of old Dbase files (.dbf) and I'll like to convert these to SQLite databases as automatically as possible. Does anybody know a tool/Python script to do so? I know, I could use dbfpy and create the SQLite table and import all data. But is there something easier? Many thanks for a hint, Helmut. -- Helmut Jarausch Lehrstuhl fuer Numerische Mathematik RWTH - Aachen University D 52056 Aachen, Germany From hartley79 at gmail.com Wed Jul 15 05:54:52 2009 From: hartley79 at gmail.com (hartley) Date: Wed, 15 Jul 2009 02:54:52 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> Message-ID: <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> On Jul 14, 2:21?pm, John Machin wrote: > On Jul 14, 7:22?pm, hartley wrote:> > > I'm very new at wrapping Python/C, and I have run into some problems. > > [snip] > > > > > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > > > pValue is now a PyList - i've even verified this with: > > > > > int a = PyList_Check(pValue); > > > > ? ? ? ? printf("%d\n", a); > > > > > However, I want to send this PyList to another python module, > > > > Please explain "send" ... do you mean the C equivalent of the Python > > > statement C_embedding.buff = the_pylist ? > > > > BTW C-embedding would trigger a syntax error in Python source; best to > > > avoid ... > > > I'm sorry I'm not using the proper terms when trying to describe this > > - i've never really learnt those terms, I guess i should do that one > > day. > > Today would be a good day to start :-) > > > > > > > Imagine that the python function C-embedding.buff looks like this: > > > def buff(a): > > ? ? if isinstance(a,list): > > ? ? ? ? print "success" > > > I want to send, pass, call, whatever a list argument from the C code > > onto this function. As simple as this - how can you call a function in > > python from C, and providing a python list as an argument? > > > As I wrote earlier - I already have a PyList, called pValue. But I > > have not been able to use this as an argument for C-embedding.buff > > using the code I described last time. > > > > > but I > > > > don't know how to do this. Initially I though I could just do like > > > > above, only swapping NULL with pValue, but that is not working. > > > > > pName2 = PyString_FromString("C-embedding"); > > > > pModule2 = PyImport_Import(pName2); > > > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > > ?Any good reason for not checking the > > > return value for an error? [Rhetorical question; answer == "No"] > > > > > pValue2 = PyObject_CallObject(pFunc2,pValue); > > Your problem is here, in the second arg of PyObject_CallObject. It > should be either NULL if no args are being supplied, or a tuple > containing the args if 1 or more args are being supplied. pValue is > the arg itself; you need to wrap it in a tuple. > > Seehttp://docs.python.org/c-api/object.html#PyObject_CallObject > > Actually you might like to use this instead (no tuple hassles): > > http://docs.python.org/c-api/object.html#PyObject_CallFunctionObjArgs > > What are you using as a source of information? Have you read this: > > http://docs.python.org/extending/embedding.html#pure-embedding? > > It contains a very thorough example of calling a Python function from > C, with all the reference-counting and error-checking stuff and it's > quite runnable -- I've just now compiled it and run it (1) as-is (2) > changed to pass a list instead of multiple ints (3) changed again to > emulate you trying to pass the list directly instead of wrapped in a > tuple ... here's the result: > > TypeError: argument list must be a tuple > Call failed > > Oh by the way, you can get away with C-embedding as a module name if > you are importing it from C, but importing it from Python [so that you > could test it independently of your C program] presents a difficulty > (syntax error) ... better to change it to c_embedding. > > > > It's mainly just a matter of (1) knowing what you want to do (2) > > > picking the API that does what you want (3) checking the returned > > > value for error after every call. > > HTH, > John Thank you, that works! You're a genius :) As for what I'm reading, I'm familiar with both http://docs.python.org/c-api/ and http://docs.python.org/extending/embedding.html#pure-embedding but i still find it hard to read this and find the proper API. The Python/C API reference is 162 pages! I have a second issue that I've also struggled with, namely using class object (don't shoot me if my terminology is off again). With the little toy class below, i want to instansiate it, boost it, and return it, similar to: s = ObjectTest() s.boostList() return s.dispList() but in C, of course. so far I have: pName = PyString_FromString("ob-test"); pModule = PyImport_Import(pName); pFunc = PyObject_GetAttrString(pModule,"ObjectTest"); if (pFunc && PyCallable_Check(pFunc)) { printf("Callable\n"); } pObj = PyObject_CallObject(pFunc,NULL); if (pObj != NULL) { printf("WAS NOT NULL!\n"); } At least this seems to work. However, as to how to actually instantiate and use this class the way I described, I have no idea which API to use. Even though I've spent lots of time reading it! Do you have any clue here, as well? (ob-test.py) class ObjectTest: def __init__(self): self.list = [] def boostList(self): self.list.append(1) def dispList(self): return self.list From quentel.pierre at wanadoo.fr Wed Jul 15 06:04:01 2009 From: quentel.pierre at wanadoo.fr (Pierre Quentel) Date: Wed, 15 Jul 2009 03:04:01 -0700 (PDT) Subject: select lines in python References: Message-ID: On 14 juil, 19:33, amr... at iisermohali.ac.in wrote: > Dear all, > > Can anyone tell me that suppose i have a file having content like: > > ? ? _Atom_name > ? ? ? _Atom_type > ? ? ? _Chem_shift_value > ? ? ? _Chem_shift_value_error > ? ? ? _Chem_shift_ambiguity_code > ? ? ?1 ? ? 1 ? PHE ? ? ? H ? ? H ? ? ?8.49 ? ? 0.02 ? ? 1 > ? ? ?2 ? ? 1 ? PHE ? ? ? HA ? ?H ? ? ?4.60 ? ? 0.02 ? ? 1 > ? ? ?3 ? ? 1 ? PHE ? ? ? CA ? ?C ? ? 57.83 ? ? ?0.3 ? ? 1 > ? ? ?4 ? ? 2 ? LEU ? ? ? H ? ? H ? ? ?8.23 ? ? 0.02 ? ? 1 > ? ? ?5 ? ? 2 ? LEU ? ? ? HA ? ?H ? ? ?4.25 ? ? 0.02 ? ? 1 > ? ? ?6 ? ? 2 ? LEU ? ? ? HB2 ? H ? ? ?1.54 ? ? 0.02 ? ? 1 > ? ? ?7 ? ? 3 ? ASP ? ? ? H ? ? H ? ? ?8.10 ? ? 0.02 ? ? 1 > ? ? ?8 ? ? 3 ? ASP ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > ? ? ?9 ? ? 3 ? ASP ? ? ? HB2 ? H ? ? ?2.65 ? ? 0.02 ? ? 1 > stop > > now what i want that instead of copying all the lines it will just write > the information about PHE and ASP then how i acn do that using python > programming.Kindly tell me the command for that. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA Hi, The problem here is to test the values at specific colums in the file. So I'd rather split each line into a list of values and do a test on relevant items, like : for line in open(filename): items = line.strip().split() if items[1] in ["ASP","PHE"]: (... handle this case ...) Using re here is probably not the best way. It may happen that a sequence appearing in a column also appears in another one ; and when the tests become more complex the regular expression itself might become complex, or even impossible (imagine the OP wants to test that the numeric values are below a certain threshold) - Pierre From ben+python at benfinney.id.au Wed Jul 15 06:08:13 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 15 Jul 2009 20:08:13 +1000 Subject: Teaching pylint about keyword placeholders in string formatting Message-ID: <87bpnmjm2a.fsf@benfinney.id.au> Howdy all, A common idiom I use is:: def frobnicate(warble): foo = complex_computation() bar = long.access.path.leading.to.useful.value baz = (lengthy + expression * with_several_parts) spangulate("%(warble)s: %(foo)s%(bar)s [%(baz)d]" % vars()) This allows the format of the string to be clear, and allows the separate parts of it to have meaningful names. However, this is causing pylint to complain:: W:218:frobnicate: Unused argument 'warble' W:219:frobnicate: Unused variable 'foo' W:220:frobnicate: Unused variable 'bar' W:221:frobnicate: Unused variable 'baz' That is, pylint is not aware that the names used by accessing the values from the dictionary returned by ?vars()?. This warning, when it actually *does* detect unused name bindings, is very useful; I don't want to disable it. Nor do I want to pepper my code with hints to pylint about each one of these, detracting significantly from the readability which is the main point of the above idiom. How can I make pylint aware in the general case that the above idiom does, in fact, constitute a use of the names ?warble?, ?foo?, ?bar?, and ?baz? in the code? -- \ ?Pinky, are you pondering what I'm pondering?? ?I think so, | `\ Brain, but if we give peas a chance, won't the lima beans feel | _o__) left out?? ?_Pinky and The Brain_ | Ben Finney From david.lyon at preisshare.net Wed Jul 15 06:39:21 2009 From: david.lyon at preisshare.net (David Lyon) Date: Wed, 15 Jul 2009 06:39:21 -0400 Subject: convert Dbase (.dbf) files to SQLite databases In-Reply-To: <7c5n8kF25pc8bU1@mid.dfncis.de> References: <7c5n8kF25pc8bU1@mid.dfncis.de> Message-ID: <942d96482771b3b7a0e4294381c5f3dd@preisshare.net> On Wed, 15 Jul 2009 11:53:28 +0200, Helmut Jarausch wrote: > Hi, > > I have a lot of old Dbase files (.dbf) and I'll like to convert these > to SQLite databases as automatically as possible. > Does anybody know a tool/Python script to do so? > > I know, I could use dbfpy and create the SQLite table and import all > data. But is there something easier? yes... Use OpenOffice-Scalc or MS-Office-Excel to open the table... Export to csv.... Use SQLite Manager (https://addons.mozilla.org/en-US/firefox/addon/5817) and use the import wizard to import your data.... It shouldn't take too long... David From piet at cs.uu.nl Wed Jul 15 06:39:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 12:39:26 +0200 Subject: promlems with threading and print References: Message-ID: >>>>> Andreas Grommek (AG) wrote: >AG> Hi Newsgroup, >AG> I'm new to python and I am familiarizing myself with threads >AG> (haven't done any threading in any other language before...). I was >AG> playing around and discovered some weird behavior. Here is my code: When you start programming with threads you should familiarize yourself with (or better: study) the subject of parallel programming. Without that you will make hard to find and debug errors. Fortunately this one you did find. Many of these have to do with 'shared resources', i.e. resources (like objects) that are used in more than one thread at the same time, while at least one thread modifies it/them. For a start read this: http://effbot.org/zone/thread-synchronization.htm and then read some more elaborate literature. >AG> import threading >AG> from time import sleep >AG> from random import random >AG> import sys >AG> class MyThread(threading.Thread): >AG> def __init__(self, t, s): >AG> self.threadmarker = t >AG> self.sleeptime = s >AG> threading.Thread.__init__(self) >AG> def run(self): >AG> print("Tread", self.threadmarker, "is going to sleep for a while...") >AG> sys.stdout.flush() #flush I/O >AG> sleep(self.sleeptime) #go to sleep >AG> print("Tread", self.threadmarker, "is waking up and terminating") What happens here is that stdout is a shared resource that you are modifying in different threads. So you have to protect it. >AG> a = 1 >AG> b = 20 >AG> for n in range(a, b): >AG> x = MyThread(n,random()*10.0) >AG> x.start() >AG> This should create some threads which print messages, go to sleep for a random amount of time (max >AG> 10 seconds) and return with a message. When I run the code I get something like this (always different): You can get even weirder stuff, like: ('Tread', 1, 'is going to sleep for a while...') ('Tread', 2, 'is going to sleep for a while...') ('Tread', 3, 'is going to sleep for a while...') ('Tread', 4, 'is going to sleep for a while...') ('Tread', 5, 'is going to sleep for a while...') ('Tread', 6, 'is going to sleep for a while...') ('Tread', 7, 'is going to sleep for a while...') ('Tread', 8('Tread', , 'is going to sleep for a while...'9, )'is going to sleep for a while...' ('Tread'), 10, 'is going to sleep for a while...') ('Tread'('Tread', 12, 'is going to sleep for a while...') , 11, 'is going to sleep for a while...') ('Tread', 13, 'is going to sleep for a while...') ('Tread', 14, 'is going to sleep for a while...') ('Tread', 15, 'is going to sleep for a while...') ('Tread', 16, 'is going to sleep for a while...') ('Tread', 17, 'is going to sleep for a while...') ('Tread'('Tread', 19, 'is going to sleep for a while...') , 18, 'is going to sleep for a while...') ('Tread', 10, 'is waking up and terminating') ... Here is a solution using locks (and at the same time I corrected Tread->Thread class MyThread(threading.Thread): def __init__(self, t, s, lock): self.lock = lock self.threadmarker = t self.sleeptime = s threading.Thread.__init__(self) def run(self): with lock: print("Thread", self.threadmarker, "is going to sleep for a while...") sys.stdout.flush() #flush I/O sleep(self.sleeptime) #go to sleep with lock: print("Thread", self.threadmarker, "is waking up and terminating") a = 1 b = 20 lock = threading.Lock() for n in range(a, b): x = MyThread(n,random()*10.0, lock) x.start() If you have an older version of Python you have to replace with lock: statement by lock.acquire() try: statement finally: lock.release() or if you want to risk deadlocks: lock.acquire() statement lock.release() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nicolas.chauvat at logilab.fr Wed Jul 15 06:39:48 2009 From: nicolas.chauvat at logilab.fr (Nicolas Chauvat) Date: Wed, 15 Jul 2009 12:39:48 +0200 Subject: [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <87bpnmjm2a.fsf@benfinney.id.au> References: <87bpnmjm2a.fsf@benfinney.id.au> Message-ID: <20090715103948.GA6248@volans.logilab.fr> Hi, On Wed, Jul 15, 2009 at 08:08:13PM +1000, Ben Finney wrote: > def frobnicate(warble): > foo = complex_computation() > bar = long.access.path.leading.to.useful.value > baz = (lengthy + expression * with_several_parts) > spangulate("%(warble)s: %(foo)s%(bar)s [%(baz)d]" % vars()) > > This allows the format of the string to be clear, and allows the > separate parts of it to have meaningful names. > > However, this is causing pylint to complain:: > ... > That is, pylint is not aware that the names used by accessing the values > from the dictionary returned by ?vars()?. > ... You are not the only one: http://lists.logilab.org/pipermail/python-projects/2009-July/thread.html Here is the ticket: https://www.logilab.net/elo/ticket/9634 Thanks for the report. As usual, help and patches are welcome. -- Nicolas Chauvat logilab.fr - services en informatique scientifique et gestion de connaissances From denis-bz-gg at t-online.de Wed Jul 15 06:46:23 2009 From: denis-bz-gg at t-online.de (denis) Date: Wed, 15 Jul 2009 03:46:23 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: <22fa2fe9-899b-407a-8e3c-49f29c86fa59@r33g2000yqn.googlegroups.com> Sure, Aho-Corasick is fast for fixed strings; but without real numbers / a concrete goal > > Matt, how many words are you looking for, in how long a string ? a simple solution is good enough, satisficing. Matt asked "how to make that function look nicer?" but "nice" has many dimensions -- bicycles are nice for some tasks, Ferraris for others. Bytheway http://en.wikipedia.org/wiki/Aho-Corasick_algorithm has a link to a Python implementation, also http://en.wikipedia.org/wiki/Worse_is_Better is fun. From fabiodib at email.it Wed Jul 15 06:49:11 2009 From: fabiodib at email.it (fdb) Date: Wed, 15 Jul 2009 12:49:11 +0200 Subject: dictionary inherit and method overriding Message-ID: Hi all, I need to extend and not replace the __getitem__ method of a dict class. Here is sample the code: >>> class myDict(dict): ... def __getitem__(self, y): ... print("Doing something") ... dict.__getitem__(self, y) ... >>> a=myDict() >>> a["value"] = 1 >>> print a["value"] None As you see i get None instead of 1. Any solutions? Bye -- FabioBD From digitig at gmail.com Wed Jul 15 06:54:06 2009 From: digitig at gmail.com (Tim Rowe) Date: Wed, 15 Jul 2009 11:54:06 +0100 Subject: Calling functions: Why this complicated ? In-Reply-To: <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> Message-ID: 2009/7/15 Jeremiah Dodds : > As a hopefully semi-informative aside, I've been writing python code for a > few years now, and I regularly use all four forms of argument passing listed > above. Curiously, I never use the all-named style in Python, whereas it's my normal style in Ada. I shall now enter a period of self-refelection to try to work out why I am so inconsistent :-) -- Tim Rowe From ben+python at benfinney.id.au Wed Jul 15 07:01:07 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 15 Jul 2009 21:01:07 +1000 Subject: Teaching pylint about keyword placeholders in string formatting References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> Message-ID: <877hyajjm4.fsf@benfinney.id.au> Nicolas Chauvat writes: > On Wed, Jul 15, 2009 at 08:08:13PM +1000, Ben Finney wrote: > > That is, pylint is not aware that the names used by accessing the values > > from the dictionary returned by ?vars()?. > > ... > > You are not the only one: > http://lists.logilab.org/pipermail/python-projects/2009-July/thread.html Thank you. I think the thread Nicolas wanted me to see was this one: . -- \ ?I put contact lenses in my dog's eyes. They had little | `\ pictures of cats on them. Then I took one out and he ran around | _o__) in circles.? ?Steven Wright | Ben Finney From nicolas.chauvat at logilab.fr Wed Jul 15 07:21:26 2009 From: nicolas.chauvat at logilab.fr (Nicolas Chauvat) Date: Wed, 15 Jul 2009 13:21:26 +0200 Subject: [Python-projects] [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <20090715103948.GA6248@volans.logilab.fr> References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> Message-ID: <20090715112126.GA7138@volans.logilab.fr> On Wed, Jul 15, 2009 at 12:39:48PM +0200, Nicolas Chauvat wrote: > Here is the ticket: > https://www.logilab.net/elo/ticket/9634 Apologies: http://www.logilab.org/ticket/9634 -- Nicolas Chauvat logilab.fr - services en informatique scientifique et gestion de connaissances From lists at cheimes.de Wed Jul 15 07:31:44 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 15 Jul 2009 13:31:44 +0200 Subject: dictionary inherit and method overriding In-Reply-To: References: Message-ID: fdb wrote: > Hi all, > > I need to extend and not replace the __getitem__ method of a dict class. > > Here is sample the code: > >>>> class myDict(dict): > .... def __getitem__(self, y): > .... print("Doing something") > .... dict.__getitem__(self, y) > .... >>>> a=myDict() >>>> a["value"] = 1 >>>> print a["value"] > None > > As you see i get None instead of 1. > > Any solutions? How about returning the value? :] Christian From lists at cheimes.de Wed Jul 15 07:37:22 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 15 Jul 2009 13:37:22 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> Message-ID: pdpi wrote: > On Jul 15, 12:08 am, Christian Heimes wrote: >> Chris Rebert wrote: >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >> raise ValueError("Exactly one of a, b, c and d must be true") >> >> Christian > > "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to > xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor > 1 = 0 xor 1 = 1 if you assicate to the left) I'm well aware of the fact that I've described something differently. 'xor' can be explained as 'check if exactly one element of two elements is true'. My algorithms describes a super xor, aka 'check if exactly one element of n elements is true'. Christian From dzizes451 at gmail.com Wed Jul 15 08:07:20 2009 From: dzizes451 at gmail.com (dzizes) Date: Wed, 15 Jul 2009 05:07:20 -0700 (PDT) Subject: how to set timeout while colling a soap method? In-Reply-To: References: <24461403.post@talk.nabble.com> Message-ID: <24496577.post@talk.nabble.com> Yes, that is what I was looking for. Greets!! -- View this message in context: http://www.nabble.com/how-to-set-timeout-while-colling-a-soap-method--tp24461403p24496577.html Sent from the Python - python-list mailing list archive at Nabble.com. From lawson89 at gmail.com Wed Jul 15 08:09:54 2009 From: lawson89 at gmail.com (Rick Lawson) Date: Wed, 15 Jul 2009 05:09:54 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> Message-ID: <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> On Jul 15, 4:53?am, Jonathan Gardner wrote: > On Jul 14, 6:34?pm, Rick Lawson wrote: > > > Appreciate any help on this. I am porting an app from Java to python > > and need generic object pooling with hooks for object initialization / > > cleanup and be able to specify an object timeout. > > Are you looking for something like a thread pool or a connection pool? > Such a thing is easy to code in Python. Might as well write one from > scratch for your particular need. > > By the way, a tip for writing Python: Forget Java. If you're porting > an app, consider rewriting it from the architecture on up. You'll save > yourself time and get a better result. Jonathan, Thanks for the advice but would like to pick your brain a little more. The pool I need is not a thread pool or db pool. I need to pool objects which are a proxy for an external C process that communicates via sockets. The pool is necessary because the initial connection/ authentication is expensive, plus the connection needs to be recycled every so often due to instability on the C side. The existing Java code uses commons-pool which incidentally is the base object pool for a db connection pooling library (DBCP). Anyway, maybe this is just so easy to do in Python that everyone rolls their own - but I have done a lot of googling with no luck. I just hate to re-invent the wheel here - I guess I should look at a db connection pooling library and see if they have a generic object pool somewhere in there. Any you can recommend that are more or less standalone db connection pool libraries ? Thanks, Rick From sorry.no at can.do Wed Jul 15 08:15:53 2009 From: sorry.no at can.do (Borivoj) Date: Wed, 15 Jul 2009 14:15:53 +0200 Subject: Need help with oo design while using TreeWidget Message-ID: I'm looking for objective oriented and pythonic way to solve my problem. I'm using IDLE's TreeWidget, by inheriting TreeItem and overriding some of it's methods. My tree structure is defined inside treedata, which is xml.dom.minidom object. MenuTreeFrame is responsible for displaying the widget inside it's own Frame. As you can see in the source, MyTreeItem is one child in tree, and it's method GetSubList is responsible for creating it's children TreeItems. When creating each child, I have to pass original tree structure (treedata) to each child. Once I implement event firing, I will have to pass Event reference to each child as well. There has to be a better way to make these references available to each child in tree. What would you recommend? My code looks something like this (not executable, because treedata definition is lacking): import Tkinter as tk from TreeWidget import TreeItem, TreeNode, ScrolledCanvas class MenuTreeFrame(tk.Frame): def __init__(self, parent, treedata): tk.Frame.__init__(self, parent) self.treedata = treedata sc = ScrolledCanvas(self, bg='white', width=150, highlightthickness=0, takefocus=1) self.canvas = sc.canvas sc.frame.pack(fill="both", expand=1) def update(self, dom): self.create(dom) # TODO: implement something smarter than recreating whole tree def create(self, dom): self.rootitem = DomTreeItem(dom.documentElement, self.treedata) self.node = TreeNode(self.canvas, None, self.rootitem) self.node.update() self.node.select() self.rootitem.OnSelect() self.node.expand() class MyTreeItem(TreeItem): def __init__(self, treenode, treedata): self.node = treenode self.previously_selected = None self.treedata = treedata def GetText(self): return self.node.firstChild.nodeValue def IsExpandable(self): node = self.node return len(self.node.getElementsByTagName('node')) > 0 def OnSelect(self): self.treedata.current = self.node # TODO: fire off the event def GetSubList(self): parent = self.node children = parent.childNodes itemlist = [MyTreeItem(node, self.treedata) for node in children if node.nodeName == 'node'] return itemlist Thanks for reading Josip From fabiodib at email.it Wed Jul 15 08:19:15 2009 From: fabiodib at email.it (fdb) Date: Wed, 15 Jul 2009 14:19:15 +0200 Subject: dictionary inherit and method overriding In-Reply-To: References: Message-ID: Only this! I'm going crazy! Than you! Code: class myDict(dict): def __getitem__(self, y): print("Doing something") return dict.__getitem__(self, y) a=myDict() a["value"] = 1 print a["value"] Christian Heimes ha scritto: > How about returning the value? :] -- FabioBD From sjmachin at lexicon.net Wed Jul 15 08:34:31 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 15 Jul 2009 05:34:31 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> Message-ID: <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> On Jul 15, 7:54?pm, hartley wrote: > On Jul 14, 2:21?pm, John Machin wrote: > > On Jul 14, 7:22?pm, hartley wrote:> > > I'm very new at wrapping Python/C, and I have run into some problems. [snip] /* the first telling */ > > > > statement C_embedding.buff = the_pylist ? > > > > > BTW C-embedding would trigger a syntax error in Python source; best to > > > > avoid ... /* the second telling */ > > Oh by the way, you can get away with C-embedding as a module name if > > you are importing it from C, but importing it from Python [so that you > > could test it independently of your C program] presents a difficulty > > (syntax error) ... better to change it to c_embedding. > > > As for what I'm reading, I'm familiar with both > > http://docs.python.org/c-api/ > andhttp://docs.python.org/extending/embedding.html#pure-embedding You must have a very strange definition of familiarity! That section has as I pointed out a complete working example of a C program that calls a Python function with 1 or more arguments, complete with error checking and reference counting. To me, familiarity would include reading that program, understanding what it is doing (there are copious explanations), compiling it, running it, then start making changes (like I did) to do things like what you want to do. > but i still find it hard to read this and find the proper API. The > Python/C API reference is 162 pages! It's a reference, not a novel. You needed no API that was not in the sample program. What you should do is when you are going through the example programs, look up each API in the reference manual to see what the arguments are supposed to be, what other possibilities there are (e.g. PyObject_CallObject 2nd arg is NULL or a tuple of your args), what the return value is, what if anything is said about reference counts, what similar APIs are there (e.g. a few called PyObject_Callxxxxxxx) > I have a second issue that I've also struggled with, namely using > class object (don't shoot me if my terminology is off again). With the > little toy class below, i want to instansiate it, boost it, and return > it, similar to: > > s = ObjectTest() > s.boostList() > return s.dispList() Let's break that down: alist = s.dispList() return alist s.dispList (despite its name indicating "display") returns a reference to a list. Your C program is going to return that to whom? The shell? > but in C, of course. > > so far I have: > > pName = PyString_FromString("ob-test"); /* the third telling */ s/-/_/ "What I tell you three times is true". Just do it. > pModule = PyImport_Import(pName); You've imported the module. So far, I'm making the charitable assumption that the lack of error checking is for brevity in your posting, but is there in your actual code. > pFunc = PyObject_GetAttrString(pModule,"ObjectTest"); You've obtained a reference to the class. > if (pFunc && PyCallable_Check(pFunc)) { > > ? ? ? ? ? ? printf("Callable\n"); > ? ? ? ? ? ? ? ? } And if there's a problem, you'll fall through and use the dud reference to the class?? Get into the habit NOW of doing the error checking properly, after each API call; otherwise debugging will become a nightmare. Follow the examples in the sample program. In this case, it is necessary only to test for NULL, and get the correct error message displayed for that. Checking if the returned object is callable in the same if-test clouds the issue. Checking for callability separately is quite unnecessary -- just call the object and you'll find out then if it's not callable. > pObj = PyObject_CallObject(pFunc,NULL); > ? ? ? ? if (pObj != NULL) { > ? ? ? ? printf("WAS NOT NULL!\n"); Again instead of printing congratulatory messages on success, put in proper checking for failure, get the error message printed and bale out. > } > > At least this seems to work. However, as to how to actually > instantiate and use this class the way I described, I have no idea > which API to use. You don't know how to instantiate the class? What do you think was the purpose of the PyObject_CallObject() that you just did?? > Even though I've spent lots of time reading it! Do > you have any clue here, as well? Let's trawl through the Python equivalent of what you want to do: s = amodule.ObjectTest() (1) import the module (2) get the "objectTest" attribute of the module (3) call it, park the result in s s.boostList() (1) get the "boostList" attribute of s (2) call it ... but it's a method, maybe we need to use a different PyObject_Callxxxxxx ... no result to park. x = s.dispList() (1) get the "displist" attribute of s (2) call it (see above), park result in x (3) do something sensible with x > > (ob-test.py) > class ObjectTest: > > ? ? ? ? def __init__(self): > ? ? ? ? ? ? ? ? self.list = [] > > ? ? ? ? def boostList(self): > ? ? ? ? ? ? ? ? self.list.append(1) > > ? ? ? ? def dispList(self): > ? ? ? ? ? ? ? ? return self.list HTH, John From deets at nospam.web.de Wed Jul 15 08:40:05 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 14:40:05 +0200 Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> Message-ID: <7c60ooF25j7lsU1@mid.uni-berlin.de> Rick Lawson wrote: > On Jul 15, 4:53?am, Jonathan Gardner > wrote: >> On Jul 14, 6:34?pm, Rick Lawson wrote: >> >> > Appreciate any help on this. I am porting an app from Java to python >> > and need generic object pooling with hooks for object initialization / >> > cleanup and be able to specify an object timeout. >> >> Are you looking for something like a thread pool or a connection pool? >> Such a thing is easy to code in Python. Might as well write one from >> scratch for your particular need. >> >> By the way, a tip for writing Python: Forget Java. If you're porting >> an app, consider rewriting it from the architecture on up. You'll save >> yourself time and get a better result. > > Jonathan, > > Thanks for the advice but would like to pick your brain a little more. > The pool I need is not a thread pool or db pool. I need to pool > objects which are a proxy for an external C process that communicates > via sockets. The pool is necessary because the initial connection/ > authentication is expensive, plus the connection needs to be recycled > every so often due to instability on the C side. The existing Java > code uses commons-pool which incidentally is the base object pool for > a db connection pooling library (DBCP). Anyway, maybe this is just so > easy to do in Python that everyone rolls their own - but I have done a > lot of googling with no luck. > > I just hate to re-invent the wheel here - I guess I should look at a > db connection pooling library and see if they have a generic object > pool somewhere in there. Any you can recommend that are more or less > standalone db connection pool libraries ? I don't know enough about your domain, e.g. if your pooled objects have state or not. Assuming that - they are objects - they don't have state (like database connections) - you want to recycle them after so many calls one of the simplest solutions would be something like this: class AutoDestructingWrapper(object): MAX_CALL_COUNT = 1000 def __init__(self, object_factory): self._object_factory = object_factory self._call_count = self.MAX_CALL_COUNT def __getattr__(self, name): if self._call_count >= self.MAX_CALL_COUNT: self._delegate = self._object_factory() return getattr(self._delegate, name) Diez From jeremiah.dodds at gmail.com Wed Jul 15 09:04:12 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 15 Jul 2009 14:04:12 +0100 Subject: Calling functions: Why this complicated ? In-Reply-To: References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> Message-ID: <12cbbbfc0907150604k33a6d468l387742ff0aa0a999@mail.gmail.com> On Wed, Jul 15, 2009 at 11:54 AM, Tim Rowe wrote: > > Curiously, I never use the all-named style in Python, whereas it's my > normal style in Ada. I shall now enter a period of self-refelection to > try to work out why I am so inconsistent :-) > > > I use it for functions that only (or mostly) have behaviour-modifying flags and for functions that take more than 3 or so parameters. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Wed Jul 15 09:05:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 15 Jul 2009 06:05:30 -0700 Subject: The meaning of "=" References: Message-ID: In article , Piet van Oostrum wrote: >>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: > >>A> In article , Piet van Oostrum wrote: >>>>>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >>>> >>A> In article , Piet van Oostrum >>A> wrote: >>>> >>>>>>> And to get c.x = 4 working you also need a __setitem__. >>>> >>A> Nope. You do need __setitem__ so that this works: >>>> >>A> c['x'] = 4 >>>> >>>> Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because >>>> that was what the OP wanted (I think). > >>A> c.x = 4 >>A> already updates the instance dict, so there's no need to change any class >>A> methods to support it. That is, IME it's much better to add methods to >>A> a regular class to make it more dict-like using the built-in instance >>A> dict rather than changing any of the attribute mechanisms. If you're >>A> really curious, I recommend trying several approaches yourself to see >>A> what works better. ;-) > >Yes, that's why I mentioned __setitem__. I just mixed up the motivation. Gotcha. Unfortunately, I can only respond to what you've written, not what you intended to write. ;-) (There was enough misinformation earlier in this thread that I felt being really really clear was important.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From pdpinheiro at gmail.com Wed Jul 15 09:12:03 2009 From: pdpinheiro at gmail.com (pdpi) Date: Wed, 15 Jul 2009 06:12:03 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> Message-ID: On Jul 15, 12:37?pm, Christian Heimes wrote: > pdpi wrote: > > On Jul 15, 12:08 am, Christian Heimes wrote: > >> Chris Rebert wrote: > >>> Using the xor bitwise operator is also an option: > >>> bool(x) ^ bool(y) > >> I prefer something like: > > >> ? ? bool(a) + bool(b) == 1 > > >> It works even for multiple tests (super xor): > > >> ? if bool(a) + bool(b) + bool(c) + bool(d) != 1: > >> ? ? ? raise ValueError("Exactly one of a, b, c and d must be true") > > >> Christian > > > "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to > > xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor > > 1 = 0 xor 1 = 1 if you assicate to the left) > > I'm well aware of the fact that I've described something differently. > 'xor' can be explained as 'check if exactly one element of two elements > is true'. My algorithms describes a super xor, aka 'check if exactly one > element of n elements is true'. > > Christian Well, I just wouldn't call it a "super xor" then, when "unicity test" works much better -- especially as an alternative to an actual xor without any specification to the actual intended functionality except the exception text. From peter.fodrek at stuba.sk Wed Jul 15 09:12:52 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Wed, 15 Jul 2009 15:12:52 +0200 Subject: import module unbelieveable behaviour Message-ID: <200907151512.52607.peter.fodrek@stuba.sk> Dear conference! I have test Why python based script for HeeksCNC post-processing does not work... And I've got unbelievable behavior When importing module module manually it works, but same opertaion from script does not work as seen /opt/HeeksCAD8/HeeksCNC> python Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import nc.rez >>> /opt/HeeksCAD8/HeeksCNC> python test.py Traceback (most recent call last): File "test.py", line 7, in import nc.rez ImportError: No module named rez /opt/HeeksCAD8/HeeksCNC> python ./test.py Traceback (most recent call last): File "./test.py", line 7, in import nc.rez ImportError: No module named rez Would anyone be helpful for me to get more information about this problem because pydb does not show anything usable for me,please? I look forward hearing form you Yours faithfully Peter Fodrek From mail at microcorp.co.za Wed Jul 15 09:37:30 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 15 Jul 2009 15:37:30 +0200 Subject: missing 'xor' Boolean operator References: Message-ID: <002101ca0551$66d1dd40$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >Unfortunately, outside of boolean algebra and simulating electrical >circuits, I can't think of any use-cases for an xor operator. Do you have >any? A bitwise xor is a poor man's comparator - if the result is binary zero, the operands were equal, no matter what they represented. Not much use in python, though. - Hendrik From deets at nospam.web.de Wed Jul 15 09:43:06 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 15:43:06 +0200 Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <7c60ooF25j7lsU1@mid.uni-berlin.de> Message-ID: <7c64etF26e7lsU1@mid.uni-berlin.de> Diez B. Roggisch wrote: > Rick Lawson wrote: > >> On Jul 15, 4:53?am, Jonathan Gardner >> wrote: >>> On Jul 14, 6:34?pm, Rick Lawson wrote: >>> >>> > Appreciate any help on this. I am porting an app from Java to python >>> > and need generic object pooling with hooks for object initialization / >>> > cleanup and be able to specify an object timeout. >>> >>> Are you looking for something like a thread pool or a connection pool? >>> Such a thing is easy to code in Python. Might as well write one from >>> scratch for your particular need. >>> >>> By the way, a tip for writing Python: Forget Java. If you're porting >>> an app, consider rewriting it from the architecture on up. You'll save >>> yourself time and get a better result. >> >> Jonathan, >> >> Thanks for the advice but would like to pick your brain a little more. >> The pool I need is not a thread pool or db pool. I need to pool >> objects which are a proxy for an external C process that communicates >> via sockets. The pool is necessary because the initial connection/ >> authentication is expensive, plus the connection needs to be recycled >> every so often due to instability on the C side. The existing Java >> code uses commons-pool which incidentally is the base object pool for >> a db connection pooling library (DBCP). Anyway, maybe this is just so >> easy to do in Python that everyone rolls their own - but I have done a >> lot of googling with no luck. >> >> I just hate to re-invent the wheel here - I guess I should look at a >> db connection pooling library and see if they have a generic object >> pool somewhere in there. Any you can recommend that are more or less >> standalone db connection pool libraries ? > > I don't know enough about your domain, e.g. if your pooled objects have > state or not. > > Assuming that > > - they are objects > - they don't have state (like database connections) > - you want to recycle them after so many calls > > one of the simplest solutions would be something like this: > > class AutoDestructingWrapper(object): > > MAX_CALL_COUNT = 1000 > > def __init__(self, object_factory): > self._object_factory = object_factory > self._call_count = self.MAX_CALL_COUNT > > This must be def __getattr__(self, name): self._call_count += 1 if self._call_count >= self.MAX_CALL_COUNT: self._delegate = self._object_factory() self._call_count = 0 return getattr(self._delegate, name) of course. Diez From python at mrabarnett.plus.com Wed Jul 15 10:21:27 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 15 Jul 2009 15:21:27 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: References: Message-ID: <4A5DE5E7.5070003@mrabarnett.plus.com> Steven D'Aprano wrote: > On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > >> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >> to have an 'xor' operator as well. > > I've often wished there was too, for the sake of completeness and > aesthetics, I'd love to be able to write: > > a xor b > > instead of defining a function xor(a, b). > > Unfortunately, outside of boolean algebra and simulating electrical > circuits, I can't think of any use-cases for an xor operator. Do you have > any? > The problem is that 'and' and 'or' are not limited to Boolean values: 'and' returns the first false value or the last true value. 'or' returns the first true value or the last false value. What values should 'xor' return? IMHO, if only one of the values is true then it should return that value, otherwise it should return False. 1 xor 0 => 1 0 xor 2 => 2 1 xor 2 => False 0 xor 0 => False This is because it's a Boolean operator, so it should fall back to Boolean values when necessary, like 'not': not 0 => True not 1 => False Also: x and y and z => (x and y) and z x or y or z => (x or y) or z therefore: x xor y xor z => (x xor y) xor z From skip at pobox.com Wed Jul 15 10:22:11 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 15 Jul 2009 09:22:11 -0500 Subject: [Python-projects] [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <20090715103948.GA6248@volans.logilab.fr> References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> Message-ID: <19037.58899.828799.617599@montanaro.dyndns.org> Nicolas> Here is the ticket: Nicolas> https://www.logilab.net/elo/ticket/9634 Is it possible to get read-only access to the tracker? It's prompting me for a login which I don't have. Thx, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ when i wake up with a heart rate below 40, i head right for the espresso machine. -- chaos @ forums.usms.org From piet at cs.uu.nl Wed Jul 15 10:25:14 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 16:25:14 +0200 Subject: promlems with threading and print References: Message-ID: >>>>> Piet van Oostrum (PvO) wrote: >PvO> def run(self): >PvO> with lock: All the 'with lock:' lines should have been 'with self.lock:' but as lock is also a global variable, it did work. Of course you can decide to use only the global variable and get rid of the self.lock altogether. I am not very fond of global variables myself, however, that's why I used the self.lock. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aurelien.campeas at logilab.fr Wed Jul 15 10:33:04 2009 From: aurelien.campeas at logilab.fr (=?utf-8?B?QXVyw6lsaWVuIENhbXDDqWFz?=) Date: Wed, 15 Jul 2009 16:33:04 +0200 Subject: [Python-projects] [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <19037.58899.828799.617599@montanaro.dyndns.org> References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> <19037.58899.828799.617599@montanaro.dyndns.org> Message-ID: <20090715143304.GJ10884@crater.logilab.fr> On Wed, Jul 15, 2009 at 09:22:11AM -0500, skip at pobox.com wrote: > > Nicolas> Here is the ticket: > Nicolas> https://www.logilab.net/elo/ticket/9634 > > Is it possible to get read-only access to the tracker? It's prompting me > for a login which I don't have. > > Thx, that should be http://www.logilab.org/ticket/9634 From Bill at SynectixLtd.com Wed Jul 15 10:37:54 2009 From: Bill at SynectixLtd.com (Bill Davy) Date: Wed, 15 Jul 2009 15:37:54 +0100 Subject: missing 'xor' Boolean operator References: Message-ID: <6Yidnc7fb6QvdMDXnZ2dnUVZ8h6dnZ2d@bt.com> "MRAB" wrote in message news:mailman.3158.1247667680.8015.python-list at python.org... > Steven D'Aprano wrote: >> On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: >> >>> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >>> to have an 'xor' operator as well. >> >> I've often wished there was too, for the sake of completeness and >> aesthetics, I'd love to be able to write: >> >> a xor b >> >> instead of defining a function xor(a, b). >> >> Unfortunately, outside of boolean algebra and simulating electrical >> circuits, I can't think of any use-cases for an xor operator. Do you have >> any? >> > The problem is that 'and' and 'or' are not limited to Boolean values: > > 'and' returns the first false value or the last true value. > > 'or' returns the first true value or the last false value. > > What values should 'xor' return? IMHO, if only one of the values is true > then it should return that value, otherwise it should return False. > > 1 xor 0 => 1 > 0 xor 2 => 2 > 1 xor 2 => False > 0 xor 0 => False > > This is because it's a Boolean operator, so it should fall back to > Boolean values when necessary, like 'not': > > not 0 => True > not 1 => False > > Also: > > x and y and z => (x and y) and z > x or y or z => (x or y) or z > > therefore: > > x xor y xor z => (x xor y) xor z Gosh, let's all discuss commutation and distribution. And surely in quantum merchanics there is something about non-commuting operatiomns letting in Planck's constant. From tycho at tycho.ws Wed Jul 15 10:58:03 2009 From: tycho at tycho.ws (Tycho Andersen) Date: Wed, 15 Jul 2009 09:58:03 -0500 Subject: import module unbelieveable behaviour In-Reply-To: <200907151512.52607.peter.fodrek@stuba.sk> References: <200907151512.52607.peter.fodrek@stuba.sk> Message-ID: <49b3a7400907150758j77e97e95ra9b806e763eb22da@mail.gmail.com> On Wed, Jul 15, 2009 at 8:12 AM, Peter Fodrek wrote: > > Would anyone be helpful for me to get more information about this problem > because ?pydb does not show anything usable for me,please? What is the directory structure for the HeeksCNC module? Although I'm no expert, I suspect it looks something like: nc/ nc/rez.py nc/foo.py In order for python to look in the nc/ directory for modules, there needs to be a file called __init__.py (even if it's empty). I suspect the nc/ directory is missing this file (although, I'm confused as to why it would work in the interpreter then). Tycho From jeanmichel at sequans.com Wed Jul 15 11:00:39 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 17:00:39 +0200 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: <4A5DEF17.3050107@sequans.com> John Nagle wrote: > walterbyrd wrote: >> I believe Guido himself has said that all indentions should be four >> spaces - no tabs. >> >> Since backward compatibility is being thrown away anyway, why not >> enforce the four space rule? >> >> At least that way, when I get python code from somebody else, I would >> know what I am looking at, without having to do a hex dump, or >> something. > > Python 3 enforces the rule that you can't mix tabs and spaces > for indentation in the same file. That (finally) guarantees that > the indentation you see is what the Python parser sees. That's > enough to prevent non-visible indentation errors. > > It also means that the Python parser no longer has to have > any concept of how many spaces equal a tab. So the problem > is now essentially solved. > > John Nagle By the way why would you prevent us from using tabs for indenting ? If I'm not wrong, from a semantic point of view, that's what tabs are for: indenting. Spaces are meant to separate tokens, aren't they ? I love my tabs, don't take them away from me ! JM From jeanmichel at sequans.com Wed Jul 15 11:03:30 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 17:03:30 +0200 Subject: Colour of output text In-Reply-To: References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: <4A5DEFC2.4030602@sequans.com> Nobody wrote: > On Fri, 10 Jul 2009 09:23:54 +0000, garabik-news-2005-05 wrote: > > >>>> I would like to learn a way of changing the colour of a particular >>>> part of the output text. I've tried the following >>>> >>> On Unix operating systems this would be done through the curses interface: >>> >>> http://docs.python.org/library/curses.html >>> >> Or using ANSI colour codes: >> >> colours = { >> 'none' : "", >> 'default' : "\033[0m", >> 'bold' : "\033[1m", >> > > [snip] > > >> # non-standard attributes, supported by some terminals >> > > This comment should have appeared immediately after "none" ;) > > Hard-coding control/escape sequences is just lame. Use the curses modules > to obtain the correct sequences for the terminal. > > As the OP I'm really interested in doing so. I currently have all my colors hard-coded. Now It may be lame but as soon as I call curses.initscr(), it's just messing up with my terminal, moreover I didn't figure out how to "print 'hello'" using curses color codes. Anyone has an example ? I'm pretty sure it may fit in one line. JM From jeanmichel at sequans.com Wed Jul 15 11:15:47 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 17:15:47 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <4A5DF2A3.2080101@sequans.com> Christian Heimes wrote: > Chris Rebert wrote: > >> Using the xor bitwise operator is also an option: >> bool(x) ^ bool(y) >> > > I prefer something like: > > bool(a) + bool(b) == 1 > > It works even for multiple tests (super xor): > > if bool(a) + bool(b) + bool(c) + bool(d) != 1: > raise ValueError("Exactly one of a, b, c and d must be true") > > Christian > > While everyone's trying to tell the OP how to workaround the missing xor operator, nobody answered the question "why is there no xor operator ?". If the question was "Why is there no 'or' operator ?", would "because A or B <=> not(not A and not B)" be a proper answer ? JM From MrJean1 at gmail.com Wed Jul 15 11:15:55 2009 From: MrJean1 at gmail.com (Jean) Date: Wed, 15 Jul 2009 08:15:55 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: On Jul 13, 6:26?am, seldan24 wrote: > Hello, > > I'm fairly new at Python so hopefully this question won't be too > awful. ?I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). > I've figured out two different ways to do this, and am wondering which > is the best (i.e. cleanest, 'right' way to proceed). ?I'm also trying > to understand exactly what occurs for each one. > > The first example: > > from ftplib import FTP > try: > ? ? ftp = FTP(ftp_host) > ? ? ftp.login(ftp_user, ftp_pass) > except Exception, err: > ? ? print err > > This works fine. ?I read through the documentation, and my > understanding is that there is a built-in exceptions module in python, > that is automatically available in a built-in namespace. ?Within that > module is an 'Exception' class which would contain whatever exception > is thrown. ?So, I'm passing that to the except, along with err to hold > the value and then print it out. > > The second example: > > from ftplib import FTP > import sys > try: > ? ? ftp = FTP(ftp_host) > ? ? ftp.login(ftp_user, ftp_pass) > except: > ? ? print sys.exc_info() > > Here I, for the most part, get the same thing. ?I'm not passing > anything to except and just printing out the exception using a method > defined in the sys module. > > So, I'm new to Python... I've made it this far and am happy, but want > to make sure I'm coding correctly from the start. ?Which method is the > better/cleaner/more standard way to continue? ?Thanks for any help. The second example is "better" if you need your code to work in Python 3.0 *and* in 2.X. For Python 3.0, the first example has to be written as: .... except Exception as err: .... /Jean Brouwers From skip at pobox.com Wed Jul 15 11:23:22 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 15 Jul 2009 10:23:22 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <4A5DEF17.3050107@sequans.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> <4A5DEF17.3050107@sequans.com> Message-ID: <19037.62570.370235.287125@montanaro.dyndns.org> JM> By the way why would you prevent us from using tabs for indenting ? JM> If I'm not wrong, from a semantic point of view, that's what tabs JM> are for: indenting. Spaces are meant to separate tokens, aren't they JM> ? I love my tabs, don't take them away from me ! I don't think your tabs have been taken away, you just can't mix them with spaces. Skip From python-url at phaseit.net Wed Jul 15 11:36:06 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Wed, 15 Jul 2009 15:36:06 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 15) Message-ID: QOTW: "Everyone gets so caught up in programming via languages that you get, well, people trying to teach 'Computer Programming' as if it were only necessary to grok a language, rather than grokking /symbol manipulation/ itself." - Simon Forman http://groups.google.com/group/comp.lang.python/msg/4bc886a5d5feda14 A C programmer may completely fail to guess the actual meaning of ++n and --n in Python: http://groups.google.com/group/comp.lang.python/browse_thread/thread/fa7281ce6249dec5/ Building the argument list for a subprocess call *right* wasn't obvious: http://groups.google.com/group/comp.lang.python/browse_thread/thread/f02150fbc732f9dc/ Robust way to handle all possible exceptions: http://groups.google.com/group/comp.lang.python/browse_thread/thread/db858ef8dfb51a47/ A related question: how to safely handle Ctrl-C: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a3ef46f47a0eac39/ Started two weeks ago, this thread is now discussing about the correct usage of "assert": http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1a6ca84d3bfc483/ Looking for a robust and safe way of writing a constructor when any step may fail: http://groups.google.com/group/comp.lang.python/browse_thread/thread/1dc1f1cb5fa8177a/ How to define a "class" property (as opposed to normal, "instance" properties): http://groups.google.com/group/comp.lang.python/browse_thread/thread/8a8b8138eada2be5/ How to explain basic concepts like pass-by-object, assignment and mutability to beginners: http://groups.google.com/group/comp.lang.python/browse_thread/thread/4b5c5aa1d8b42b9c/ later: the meaning of '=' in assignment http://groups.google.com/group/comp.lang.python/browse_thread/thread/3c13fd526a4d35df/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From robert.kern at gmail.com Wed Jul 15 11:38:55 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 15 Jul 2009 10:38:55 -0500 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5DF2A3.2080101@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <4A5DF2A3.2080101@sequans.com> Message-ID: On 2009-07-15 10:15, Jean-Michel Pichavant wrote: > Christian Heimes wrote: >> Chris Rebert wrote: >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >> >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >> raise ValueError("Exactly one of a, b, c and d must be true") >> >> Christian >> > While everyone's trying to tell the OP how to workaround the missing xor > operator, nobody answered the question "why is there no xor operator ?". > > If the question was "Why is there no 'or' operator ?", would "because A > or B <=> not(not A and not B)" be a proper answer ? That's not the only answer the OP has been given. The most compelling answer is that an "xor" keyword cannot be implemented with similar semantics to "and" and "or", in particular short-circuiting and returning one of the actual inputs instead of a coerced bool. -- 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 deets at nospam.web.de Wed Jul 15 11:41:54 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 17:41:54 +0200 Subject: import module unbelieveable behaviour References: Message-ID: <7c6bdkF26j00sU1@mid.uni-berlin.de> Peter Fodrek wrote: > Dear conference! > > I have test Why python based script for HeeksCNC post-processing does not > work... And I've got unbelievable behavior When importing module module > manually it works, but same opertaion from script does not > work as seen > > /opt/HeeksCAD8/HeeksCNC> python > Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import nc.rez >>>> > /opt/HeeksCAD8/HeeksCNC> python test.py > Traceback (most recent call last): > File "test.py", line 7, in > import nc.rez > ImportError: No module named rez What does import nc print nc.__file__ tell you, and is that what you'd expect it to be? Diez From hniksic at xemacs.org Wed Jul 15 12:14:10 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 15 Jul 2009 18:14:10 +0200 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <87fxcxj54d.fsf@busola.homelinux.net> Jean-Michel Pichavant writes: > While everyone's trying to tell the OP how to workaround the missing > xor operator, nobody answered the question "why is there no [boolean] > xor operator ?". Probably because there isn't one in C. The bitwise XOR operator, on the other hand, exists in both C and Python. > If the question was "Why is there no 'or' operator ?", would "because > A or B <=> not(not A and not B)" be a proper answer ? Note that in Python A or B is in fact not equivalent to not(not A and not B). From ethan at stoneleaf.us Wed Jul 15 12:25:08 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 15 Jul 2009 09:25:08 -0700 Subject: convert Dbase (.dbf) files to SQLite databases In-Reply-To: <7c5n8kF25pc8bU1@mid.dfncis.de> References: <7c5n8kF25pc8bU1@mid.dfncis.de> Message-ID: <4A5E02E4.9090303@stoneleaf.us> Helmut Jarausch wrote: > Hi, > > I have a lot of old Dbase files (.dbf) and I'll like to convert these > to SQLite databases as automatically as possible. > Does anybody know a tool/Python script to do so? > > I know, I could use dbfpy and create the SQLite table and import all > data. But is there something easier? > > Many thanks for a hint, > > Helmut. > Greetings! If your tables are either dBase III or Visual Foxpro 6 (may work with other VFP versions...) you can try http://groups.google.com/group/python-dbase #open table import dbf table = dbf.Table('/path/to/old/dbf') #generate .csv file table.export() # defaults to same name with csv extension #access records for rec in table: tups = tuple(rec) lst = list(rec) dct = rec.scatter_fields() I haven't used SQlite, so I'm unable to provide samples for that portion of the conversion. Hope this helps. ~Ethan~ From sjmachin at lexicon.net Wed Jul 15 12:30:29 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 15 Jul 2009 09:30:29 -0700 (PDT) Subject: convert Dbase (.dbf) files to SQLite databases References: <7c5n8kF25pc8bU1@mid.dfncis.de> Message-ID: <419cc0e0-3a78-4906-8420-c12ce2684bef@c36g2000yqn.googlegroups.com> On Jul 15, 8:39?pm, David Lyon wrote: > On Wed, 15 Jul 2009 11:53:28 +0200, Helmut Jarausch > > wrote: > > Hi, > > > I have a lot of old Dbase files (.dbf) and I'll like to convert these > > to SQLite databases as automatically as possible. > > Does anybody know a tool/Python script to do so? > > > I know, I could use dbfpy and create the SQLite table and import all > > data. But is there something easier? > > yes... > > Use OpenOffice-Scalc or MS-Office-Excel to open the table... Max 64K rows for Scalc and Excel 2003; 2007 can take 2**20 rows. Only old dBase (not dBase 7). Memo fields not handled. Visual FoxPro DBFs not supported by Excel even tho' VFP is an MS product. > Export to csv.... Yuk. > > Use SQLite Manager (https://addons.mozilla.org/en-US/firefox/addon/5817) > > and use the import wizard to import your data.... > > It shouldn't take too long... ... before you get sick of the error-prone manual tasks. I'd write a script that took a DBF file, analysed the field descriptions, made a CREATE TABLE statement, executed it, and then started doing inserts. Fairly easy to write. Scripts have the great benefit that you can fix them and re-run a whole lot easier than redoing manual steps. If dbfpy can't handle any new-fangled stuff you may have in your files, drop me a line ... I have a soon-to-be released DBF module that should be able to read the "new" stuff up to dBase7 and VFP9, including memo files, conversion from whatever to Unicode if needed, ... Cheers, John From seldan24 at gmail.com Wed Jul 15 12:36:13 2009 From: seldan24 at gmail.com (seldan24) Date: Wed, 15 Jul 2009 09:36:13 -0700 (PDT) Subject: Python Equivalent for dd & fold Message-ID: Hello, I have a shell script, that I'm attempting to convert to Python. It FTP's files down from an AS/400 machine. That part is working fine. Once the files arrive, the script converts them from EBCDIC to ASCII and then formats their line width based on a pre-determined size. For example, if I have the file TESTFILE and I know it should be formatted to 32 characters/line, I run: dd if=TESTFILE,EBCDIC conv=ASCII | fold -w 32 > TESTFILE.ASCII Luckily, the files have the packed decimal format common in COBOL programs converted already prior to reaching me, so all I have to do is the conversion and line width formatting. The above works fine, and I know I could (and may) just embed it using subprocess.Popen. This, I've figured out how to do. But, ideally, I'd like to convert as much shell to native Python as possible, so that I can learn more. I think I can figure out the first part by using the codecs module... found some information on Google related to that. My question is, what can I use as the equivalent for the Unix 'fold' command? I don't really need to know how to do it, just a push in the right direction. Thanks. From rhodri at wildebst.demon.co.uk Wed Jul 15 12:40:20 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 15 Jul 2009 17:40:20 +0100 Subject: select lines in python In-Reply-To: <29449.210.212.36.65.1247598632.squirrel@www.iisermohali.ac.in> References: <29449.210.212.36.65.1247598632.squirrel@www.iisermohali.ac.in> Message-ID: On Tue, 14 Jul 2009 20:10:32 +0100, wrote: > Can i become more precise like instead of printing all lines for PHE and > ASP is it possible that for PHE python will print only those lines which > will have information about H and HA and for ASP it will print those > lines > which will have information about HA and HB. 1) Top-posting is considered unhelpful, since it breaks the flow of information badly. 2) To answer question: yes, you can. However I can sense the start of an unending flood of small changes, so I'm going to direct you back to the Python tutorial at www.python.org rather than tell you exactly how. -- Rhodri James *-* Wildebeest Herder to the Masses From ethan at stoneleaf.us Wed Jul 15 12:44:53 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 15 Jul 2009 09:44:53 -0700 Subject: convert Dbase (.dbf) files to SQLite databases In-Reply-To: <419cc0e0-3a78-4906-8420-c12ce2684bef@c36g2000yqn.googlegroups.com> References: <7c5n8kF25pc8bU1@mid.dfncis.de> <419cc0e0-3a78-4906-8420-c12ce2684bef@c36g2000yqn.googlegroups.com> Message-ID: <4A5E0785.8060307@stoneleaf.us> John Machin wrote: > If dbfpy can't handle any new-fangled stuff you may have in your > files, drop me a line ... I have a soon-to-be released DBF module that > should be able to read the "new" stuff up to dBase7 and VFP9, > including memo files, conversion from whatever to Unicode if > needed, ... > > Cheers, > John Cool! I'm looking forward to it! ~Ethan~ From motoom at xs4all.nl Wed Jul 15 12:47:13 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Wed, 15 Jul 2009 18:47:13 +0200 Subject: Python Equivalent for dd & fold In-Reply-To: References: Message-ID: <4A5E0811.2050800@xs4all.nl> seldan24 wrote: > what can I use as the equivalent for the Unix 'fold' command? def fold(s,len): while s: print s[:len] s=s[len:] s="A very long string indeed. Really that long? Indeed." fold(s,10) Output: A very lon g string i ndeed. Rea lly that l ong? Indee d. Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From weafon at lbl.gov Wed Jul 15 12:48:49 2009 From: weafon at lbl.gov (weafon) Date: Wed, 15 Jul 2009 09:48:49 -0700 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: <4A5D4DC5.1090202@ieee.org> References: <4A5CC86A.9070907@lbl.gov> <4A5D4DC5.1090202@ieee.org> Message-ID: <4A5E0871.90105@lbl.gov> Hi DaveA, Thank for your responses even though my problem has been solved based on Miles' suggestion. I am writing programs by using the SimPy library, which is a discrete-event simulator library. Below is my actual code segment class RAID(Process): def ReqServ(self): while(now() weafon wrote: >>
Hi guys, >> >> I have a question about the usage of yield. As shown in the below >> example, in general, if there is a code segment commonly used by two >> or more functions, we may isolate the segment into a function and >> then call it from other functions if necessary. >> >> def func1(): >> .... >> while(cond): >> ..... >> commoncode() >> ... >> >> >> def func2(): >> .... >> while(cond): >> ..... >> commoncode() >> ... >> >> def commoncode() >> AAAA >> BBBB >> CCCC >> >> However, if there is a 'yield' operation in the common code segment, >> the isolation causes that func1 and func2 become a non-generator >> function!! Although I can prevent such an isolation by just >> duplicating the segment in func1 and func2 to keep both of them being >> generator functions, the code may become ugly and hard to maintain >> particularly when coomoncode() is long. >> >> The problem may be resolved if I can define the commoncode() as an >> inline function or marco. Unfortunately, inline and marco do not >> seems to be implemented in python. Thus, how can I isolate a common >> segment into a function when there are yield operations in the common >> segment? >> >> Thanks, >> Weafon >> > You are implying there's something special or unique about yield in > this. Return has the same problem, and many other flow control > constructs. Also, variable definitions and scoping. > > So you can't just copy any old bunch of adjacent lines out of two > functions, put it into a third, and call it factoring. Give us a > specific example you're puzzled about, and we can try to solve it. > > DaveA > From seldan24 at gmail.com Wed Jul 15 13:14:54 2009 From: seldan24 at gmail.com (seldan24) Date: Wed, 15 Jul 2009 10:14:54 -0700 (PDT) Subject: Python Equivalent for dd & fold References: Message-ID: On Jul 15, 12:47?pm, Michiel Overtoom wrote: > seldan24 wrote: > > what can I use as the equivalent for the Unix 'fold' command? > > def fold(s,len): > ? ? ?while s: > ? ? ? ? ?print s[:len] > ? ? ? ? ?s=s[len:] > > s="A very long string indeed. Really that long? Indeed." > fold(s,10) > > Output: > > A very lon > g string i > ndeed. Rea > lly that l > ong? Indee > d. > > Greetings, > > -- > "The ability of the OSS process to collect and harness > the collective IQ of thousands of individuals across > the Internet is simply amazing." - Vinod Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html Wow, I feel like a dork. I should have done more research prior to posting. Anyway, thanks for the advice. The trouble with Python is that things make 'too much' sense. Loving this language. From python at mrabarnett.plus.com Wed Jul 15 13:23:14 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 15 Jul 2009 18:23:14 +0100 Subject: Python Equivalent for dd & fold In-Reply-To: References: Message-ID: <4A5E1082.8090901@mrabarnett.plus.com> seldan24 wrote: > On Jul 15, 12:47 pm, Michiel Overtoom wrote: >> seldan24 wrote: >>> what can I use as the equivalent for the Unix 'fold' command? >> def fold(s,len): >> while s: >> print s[:len] >> s=s[len:] >> >> s="A very long string indeed. Really that long? Indeed." >> fold(s,10) >> >> Output: >> >> A very lon >> g string i >> ndeed. Rea >> lly that l >> ong? Indee >> d. >> > > Wow, I feel like a dork. I should have done more research prior to > posting. Anyway, thanks for the advice. The trouble with Python is > that things make 'too much' sense. Loving this language. You might still need to tweak the above code as regards how line endings are handled. From http Wed Jul 15 13:25:25 2009 From: http (Paul Rubin) Date: 15 Jul 2009 10:25:25 -0700 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <7x4otdeu4a.fsf@ruckus.brouhaha.com> Hrvoje Niksic writes: > > While everyone's trying to tell the OP how to workaround the missing > > xor operator, nobody answered the question "why is there no [boolean] > > xor operator ?". > > Probably because there isn't one in C. The bitwise XOR operator, on the > other hand, exists in both C and Python. A non-bitwise XOR really sounds almost useless. From wbrehaut at mcsnet.ca Wed Jul 15 13:36:21 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 11:36:21 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> Message-ID: On Wed, 15 Jul 2009 13:37:22 +0200, Christian Heimes wrote: >pdpi wrote: >> On Jul 15, 12:08 am, Christian Heimes wrote: >>> Chris Rebert wrote: >>>> Using the xor bitwise operator is also an option: >>>> bool(x) ^ bool(y) >>> I prefer something like: >>> >>> bool(a) + bool(b) == 1 >>> >>> It works even for multiple tests (super xor): >>> >>> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >>> raise ValueError("Exactly one of a, b, c and d must be true") >>> >>> Christian >> >> "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to >> xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor >> 1 = 0 xor 1 = 1 if you assicate to the left) > >I'm well aware of the fact that I've described something differently. >'xor' can be explained as 'check if exactly one element of two elements >is true'. My algorithms describes a super xor, aka 'check if exactly one >element of n elements is true'. But that's not a "super xor" (commonly known as XOR). Rather than describing xor as: check if exactly one element of two elements is true describe it as: check if an odd number of two elements is true then you'll get the correct definition of "super xor": check if an odd number of n elements is true I.e., XOR determines parity, and: XOR = 0 if v has an even number of 1s and 1 if v has an odd number of 1s wayne >Christian From jeanmichel at sequans.com Wed Jul 15 13:43:20 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 19:43:20 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <87fxcxj54d.fsf@busola.homelinux.net> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <4A5E1538.7030107@sequans.com> Hrvoje Niksic wrote: [snip] > Note that in Python A or B is in fact not equivalent to not(not A and > not B). > >>> l = [(True, True), (True, False), (False, True), (False, False)] >>> for p in l: ... p[0] or p[1] ... True True True False >>> for p in l: ... not(not p[0] and not p[1]) ... True True True False >>> Did I make twice the same obvious error ? JM From emile at fenx.com Wed Jul 15 13:48:42 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Jul 2009 10:48:42 -0700 Subject: Python Equivalent for dd & fold In-Reply-To: <4A5E1082.8090901@mrabarnett.plus.com> References: <4A5E1082.8090901@mrabarnett.plus.com> Message-ID: On 7/15/2009 10:23 AM MRAB said... >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: >>> seldan24 wrote: >>>> what can I use as the equivalent for the Unix 'fold' command? >>> def fold(s,len): >>> while s: >>> print s[:len] >>> s=s[len:] >>> > You might still need to tweak the above code as regards how line endings > are handled. You might also want to tweak it if the strings are _really_ long to simply slice out the substrings as opposed to reassigning the balance to a newly created s on each iteration. Emile From emile at fenx.com Wed Jul 15 13:55:22 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Jul 2009 10:55:22 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5E1538.7030107@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On 7/15/2009 10:43 AM Jean-Michel Pichavant said... > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> > >>> l = [(True, True), (True, False), (False, True), (False, False)] > >>> for p in l: > ... p[0] or p[1] > ... > True > True > True > False > >>> for p in l: > ... not(not p[0] and not p[1]) > ... > True > True > True > False > >>> > Did I make twice the same obvious error ? No -- but in the not(not... example it doesn't short-circuit. Emile From rodriguealcazar at gmail.com Wed Jul 15 13:55:34 2009 From: rodriguealcazar at gmail.com (Rodrigue) Date: Wed, 15 Jul 2009 10:55:34 -0700 (PDT) Subject: python first assignment of a global variable Message-ID: Hi all, I came accross a strange behaviour in python today. Here is a simple example to describe my situation: MY_GLOBAL = '' def a(): print 'global is: ', MY_GLOBAL def b(): try: MY_GLOBAL += 'bla' except Exception, e: print 'b: ', e def c(): try: global MY_GLOBAL MY_GLOBAL += 'bla' except Exception, e: print 'c: ', e def d(): try: if not MY_GLOBAL: print 'not my global' except Exception, e: print 'd: ', e def e(): try: if not MY_GLOBAL: MY_GLOBAL = '' except Exception, e: print 'e: ', e def f(): global MY_GLOBAL if not MY_GLOBAL: MY_GLOBAL = '' def e_raise(): if not MY_GLOBAL: MY_GLOBAL = 'bla' if __name__ == "__main__": a() b() c() d() e() f() e_raise() And here is the output I get: global is: b: local variable 'MY_GLOBAL' referenced before assignment e: local variable 'MY_GLOBAL' referenced before assignment Traceback (most recent call last): File "glo.py", line 49, in e_raise() File "glo.py", line 39, in e_raise if not MY_GLOBAL: UnboundLocalError: local variable 'MY_GLOBAL' referenced before assignment Now, I was reading that page http://stackoverflow.com/questions/370357/python-variable-scope-question and found (understood) only part of the behaviour that could explain the output. Basically, I was very surprised to discover that e() raises an exception, but even more that e_raise() points to if not MY_GLOBAL Is the problem not really when I assign? My assumption is that some reordering is happening behind the scenes that creates a situation similar to the += which assigns hence expects to be at the local level. I would love some enlightenment here Rodrigue From milesck at umich.edu Wed Jul 15 13:57:20 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 13:57:20 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5E1538.7030107@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Jul 15, 2009, at 1:43 PM, Jean-Michel Pichavant wrote: > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> > >>> l = [(True, True), (True, False), (False, True), (False, False)] > >>> for p in l: > ... p[0] or p[1] > [snip] > Did I make twice the same obvious error ? Try again with: l = [('foo','bar'), ('foo', ''), ('', 'bar'), ('', '')] -Miles From milesck at umich.edu Wed Jul 15 14:10:50 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 14:10:50 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Jul 15, 2009, at 1:55 PM, Emile van Sebille wrote: > On 7/15/2009 10:43 AM Jean-Michel Pichavant said... >> Hrvoje Niksic wrote: >> [snip] >>> Note that in Python A or B is in fact not equivalent to not(not A >>> and >>> not B). >>> >> Did I make twice the same obvious error ? > > No -- but in the not(not... example it doesn't short-circuit. No; like 'A or B', 'not (not A and not B)' does in fact short-circuit if A is True. (The 'and' condition does not have to evaluate the right operand when 'not A' is False.) -Miles From tjreedy at udel.edu Wed Jul 15 14:14:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 15 Jul 2009 14:14:55 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5DA247.3040606@timgolden.me.uk> References: <4A5DA247.3040606@timgolden.me.uk> Message-ID: Tim Golden wrote: > I was pondering on this yesterday, and the only case I've > come across in my code -- and it's reasonably common -- > is checking that one and only one of two params has been > passed. I have code which wants, say, an id or a name > but doesn't want both. It's hardly difficult to write the > check even now, but an built-in xor would make it fractionally > cleaner. I think I would just have one parameter id_name or identifier so no check is needed. This is a common idiom in Python where names are not typed. Example: param 'source' is a string (with a file name to be opened) or an already opened file. If you want two local vars inside the function, that should be kept private to the function. tjr From emile at fenx.com Wed Jul 15 14:14:57 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Jul 2009 11:14:57 -0700 Subject: python first assignment of a global variable In-Reply-To: References: Message-ID: On 7/15/2009 10:55 AM Rodrigue said... > I came accross a strange behaviour in python today. Here is a simple > example to describe my situation: > > MY_GLOBAL = '' > def e_raise(): > if not MY_GLOBAL: > MY_GLOBAL = 'bla' > > Traceback (most recent call last): > File "glo.py", line 49, in > e_raise() > File "glo.py", line 39, in e_raise > if not MY_GLOBAL: > UnboundLocalError: local variable 'MY_GLOBAL' referenced before > assignment the def prepares the function for subsequent use. At this prep time, MY_GLOBAL, by virtue of being assigned to later in the function, and the absence of a global statement, is identified as a local variable. Later, at execution time, testing MY_GLOBAL fails as it's not yet been assigned to. HTH, Emile From milesck at umich.edu Wed Jul 15 14:17:27 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 14:17:27 -0400 Subject: python first assignment of a global variable In-Reply-To: References: Message-ID: <4C9A140E-7BAB-49F5-97B7-FAD86DD0E2CF@umich.edu> On Jul 15, 2009, at 1:55 PM, Rodrigue wrote: > Basically, I was very surprised to discover that e() raises an > exception, but even more that e_raise() points to > if not MY_GLOBAL Is the problem not really when I assign? > > My assumption is that some reordering is happening behind the scenes > that creates a situation similar to the += which assigns hence expects > to be at the local level. The determination of whether a name is a reference to a local or global variable is made at compile time. When a function contains a single assignment (or augmented assignment) to a name, the compiler generates bytecode such that all references to that name within the function will be looked up in the local scope only, including those before the assignment statement. -Miles From pfeldman at verizon.net Wed Jul 15 14:19:18 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Wed, 15 Jul 2009 11:19:18 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> Message-ID: <24503248.post@talk.nabble.com> I did initially ask for an infix xor operator, but eventually gave up on this. I like the first of your two one-line solutions below; this is clean and easy to understand. Thanks! I'd still like to be able to write an expression like '(a and b) xor (c and d) xor (e and f)', but it looks as though that can't be done. Well, that's not exactly what you originally asked for. But it's still a one-liner: def xor(*args): return bool(sum(map(bool, args)) % 2) or perhaps def xor(*args): return bool(len(filter(None, args)) & 1) -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24503248.html Sent from the Python - python-list mailing list archive at Nabble.com. From milesck at umich.edu Wed Jul 15 14:24:23 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 14:24:23 -0400 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: On Jul 14, 2009, at 5:06 PM, David Bolen wrote: > Are you sure? It seems to restrict them in the same block, but not in > the entire file. At least I was able to use both space and tab > indented blocks in the same file with Python 3.0 and 3.1. It seems to me that, within an indented block, Python 3.1 requires that you are consistent in your use of indentation characters *for that indentation level*. For example, the following code seems to be allowed: def foo(): if True: x = 1 else: x = 2 return x But replacing any of the first tabs on each line with 8 spaces (without replacing them all), which previously would have been allowed, is now an error. -Miles From wbrehaut at mcsnet.ca Wed Jul 15 14:29:54 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 12:29:54 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" >wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to >> have an 'xor' operator as well. > >Hmm. I don't think 'nice' is sufficient. You'd need to make the case >that it's sufficiently useful to justify adding a new keyword 'xor' to >the language; I suspect that would be an uphill struggle. :) > >I'll just note that: > >(1) It's easy to emulate xor: 'x xor y' <-> bool(x) != bool(y) > >(2) 'and' and 'or' are special in that they have useful short- >circuiting behaviour; xor doesn't have this property (that is, you >always need to evaluate *both* operands to determine the result). > >I'd also guess that 'xor' would be much less used than 'and' or 'or', >but maybe that's just a reflection of the sort of code that I tend to >write. You're right about that!. It's used everywhere in: - coding and encryption theory (and practice) (e.g., http://www.mathcs.emory.edu/~whalen/Hash/Hash_Articles/IEEE/XOR-based%20hash%20functions.pdf) - emulation and simulation of hardware (since all but the most trivial logic circuits are likely to include XOR-gates) - hence, for design of new architectures or simulators or virtual machines and simplification of existing ones--(e.g., http://www.date-conference.com/archive/conference/proceedings/PAPERS/1999/DATE99/PDFFILES/05A_6.PDF) and http://bochs.sourceforge.net/Virtualization_Without_Hardware_Final.pdf which includes: "The answer relies on the observation that subtracting an integer value from 0xFFFFFFFF gives the same result as XOR-ing that same value to 0xFFFFFFFF." And, perhaps the most useful use of all, for Bouton's solution of the game of Nim--both for the proof that his strategy "solves" the game and for an easy implementation of a Nim-playing program--and the only operator needed is XOR (e.g., http://www.wordiq.com/definition/Nim). wayne From gerard.blais at gmail.com Wed Jul 15 14:40:32 2009 From: gerard.blais at gmail.com (MCIPERF) Date: Wed, 15 Jul 2009 11:40:32 -0700 (PDT) Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 9, 7:28?pm, Miles Kaufmann wrote: > On Jul 9, 2009, at 9:20 AM, Lie Ryan wrote: > > > Michael Mossey wrote: > >> I want to understand better what the "secret" is to responding to a > >> ctrl-C in any shape or form. > > > Are you asking: "when would the python interpreter process > > KeyboardInterrupt?" > > ... > > In single threaded python program, the currently running thread is > > always the main thread (which can handle KeyboardInterrupt). I believe > > SIGINT is checked at every ticks. But SIGINT cannot interrupt atomic > > operations (i.e. it cannot interrupt long operations that takes a ? > > single > > tick). > > Some otherwise atomic single-bytecode operations (like large integer ? > arithmetic) do manual checks for whether signals were raised (though ? > that won't help at all if the operation isn't on the main thread). > > > I believe a tick in python is equivalent to a single bytecode, but > > please correct me if I'm wrong. > > Not all opcodes qualify as a tick. ?In general, those opcodes that ? > cause control to remain in the eval loop (and not make calls to other ? > Python or C functions) don't qualify as ticks (but there are ? > exceptions, e.g. so that while True: pass is interruptible). ?In ? > Python/ceval.c: PyEval_EvalFrameEx(), those opcodes that don't end in ? > goto fast_next_opcode are ticks. > > Please correct me if _I'm_ wrong! :) > -Miles You don't need to do I/O. This works: try: process_forever() except KeyboardInterrupt: save critical stuff write nice messages I often wrap a large computational task like this, with the idea that the exception can let me exit safely, in my case by writing restart parameters and printing a summary pf progress to date. Gerry From amrita at iisermohali.ac.in Wed Jul 15 14:46:29 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Thu, 16 Jul 2009 00:16:29 +0530 (IST) Subject: mail Message-ID: <7683.210.212.36.65.1247683589.squirrel@www.iisermohali.ac.in> Dear all, Sorry that I am disturbing you all again and again but this is the way I am trying to solve my problem:--- >>> import re >>> exp = re.compile("CA") >>> infile = open("file1.txt") >>> for line in infile: ... values = re.split("\s+", line) ... if exp.search(line): ... print ("%s %s CA = %s" %(values[2], values[3], values[6])) ... with this it is giving the output like:---- 8 ALA CA = 54.67 15 ALA CA = 52.18 21 ALA CA = 54.33 23 ALA CA = 55.84 33 ALA CA = 55.58 38 ALA CA = 54.33 which is all right but i want CB and C value also in each row and it should take value from 5th column infront of them, file is something lookin like:----- 47 8 ALA H H 7.85 0.02 1 48 8 ALA HA H 2.98 0.02 1 49 8 ALA HB H 1.05 0.02 1 50 8 ALA C C 179.39 0.3 1 51 8 ALA CA C 54.67 0.3 1 52 8 ALA CB C 18.85 0.3 1 53 8 ALA N N 123.95 0.3 1 107 15 ALA H H 8.05 0.02 1 108 15 ALA HA H 4.52 0.02 1 109 15 ALA HB H 1.29 0.02 1 110 15 ALA C C 177.18 0.3 1 111 15 ALA CA C 52.18 0.3 1 112 15 ALA CB C 20.64 0.3 1 113 15 ALA N N 119.31 0.3 1 154 21 ALA H H 7.66 0.02 1 155 21 ALA HA H 4.05 0.02 1 156 21 ALA HB H 1.39 0.02 1 157 21 ALA C C 179.35 0.3 1 158 21 ALA CA C 54.33 0.3 1 159 21 ALA CB C 17.87 0.3 1 160 21 ALA N N 123.58 0.3 1 169 23 ALA H H 8.78 0.02 1 170 23 ALA HA H 4.14 0.02 1 171 23 ALA HB H 1.62 0.02 1 172 23 ALA C C 179.93 0.3 1 173 23 ALA CA C 55.84 0.3 1 174 23 ALA CB C 17.55 0.3 1 175 23 ALA N N 120.16 0.3 1 232 33 ALA H H 7.57 0.02 1 233 33 ALA HA H 3.89 0.02 1 234 33 ALA HB H 1.78 0.02 1 235 33 ALA C C 179.24 0.3 1 236 33 ALA CA C 55.58 0.3 1 237 33 ALA CB C 19.75 0.3 1 238 33 ALA N N 121.52 0.3 1 269 38 ALA H H 8.29 0.02 1 270 38 ALA HA H 4.04 0.02 1 271 38 ALA HB H 1.35 0.02 1 272 38 ALA C C 178.95 0.3 1 273 38 ALA CA C 54.33 0.3 1 274 38 ALA CB C 18.30 0.3 1 275 38 ALA N N 120.62 0.3 1 I just want that it will give output something like:----- 8 ALA C = 179.39 CA = 54.67 CB = 18.85 15 ALA C = 177.18 CA = 52.18 CB = 20.64 21 ALA C = 179.35 CA = 54.33 CB = 17.87..... so first it will write the position of the amino acid(given by second column)then amino acid here it is ALA and then the corresponding value of C, CA and CB from 5th colum for each position of ALA. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From robert.kern at gmail.com Wed Jul 15 14:47:28 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 15 Jul 2009 13:47:28 -0500 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On 2009-07-15 13:29, Wayne Brehaut wrote: > On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson > wrote: > >> On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" >> wrote: >>> Current Boolean operators are 'and', 'or', and 'not'. It would be nice to >>> have an 'xor' operator as well. >> Hmm. I don't think 'nice' is sufficient. You'd need to make the case >> that it's sufficiently useful to justify adding a new keyword 'xor' to >> the language; I suspect that would be an uphill struggle. :) >> >> I'll just note that: >> >> (1) It's easy to emulate xor: 'x xor y'<-> bool(x) != bool(y) >> >> (2) 'and' and 'or' are special in that they have useful short- >> circuiting behaviour; xor doesn't have this property (that is, you >> always need to evaluate *both* operands to determine the result). >> >> I'd also guess that 'xor' would be much less used than 'and' or 'or', >> but maybe that's just a reflection of the sort of code that I tend to >> write. > > You're right about that!. It's used everywhere in: > > - coding and encryption theory (and practice) (e.g., > http://www.mathcs.emory.edu/~whalen/Hash/Hash_Articles/IEEE/XOR-based%20hash%20functions.pdf) > - emulation and simulation of hardware (since all but the most trivial > logic circuits are likely to include XOR-gates) > - hence, for design of new architectures or simulators or virtual > machines and simplification of existing ones--(e.g., > http://www.date-conference.com/archive/conference/proceedings/PAPERS/1999/DATE99/PDFFILES/05A_6.PDF) > and > http://bochs.sourceforge.net/Virtualization_Without_Hardware_Final.pdf > which includes: > > "The answer relies on the observation that subtracting an integer > value from 0xFFFFFFFF gives the same result as XOR-ing that same value > to 0xFFFFFFFF." > > And, perhaps the most useful use of all, for Bouton's solution of the > game of Nim--both for the proof that his strategy "solves" the game > and for an easy implementation of a Nim-playing program--and the only > operator needed is XOR (e.g., http://www.wordiq.com/definition/Nim). All of those can use the bitwise XOR operator, not the boolean XOR. Python already has the ^ operator for those purposes. -- 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 dickinsm at gmail.com Wed Jul 15 14:51:44 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Wed, 15 Jul 2009 11:51:44 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Jul 15, 7:29?pm, Wayne Brehaut wrote: > On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: > >I'd also guess that 'xor' would be much less used than 'and' or 'or', > >but maybe that's just a reflection of the sort of code that I tend to > >write. > > You're right about that!. It's used everywhere in: > [snip examples and references] Those examples are (almost) all about the *bitwise* xor operator, which exists in Python ('^') and, as you point out, has no shortage of good uses. The discussion was about a *logical* xor, to parallel the existing 'and' and 'or' operators. -- Mark From anthony.tolle at gmail.com Wed Jul 15 14:53:23 2009 From: anthony.tolle at gmail.com (Anthony Tolle) Date: Wed, 15 Jul 2009 11:53:23 -0700 (PDT) Subject: missing 'xor' Boolean operator References: Message-ID: On Jul 14, 2:25?pm, "Dr. Phillip M. Feldman" wrote: > Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to > have an 'xor' operator as well. My $0.02 on this discussion: There would be nothing gained by having non-bitwise XOR operator. You can't short-circuit XOR, because you must evaluate all operands to produce a result. Consequently, returning the "true" item also doesn't make much sense. XOR is closer to the the logical NOT operator than it is to AND or OR. Here's my own take on a function that can handle any number of arguments (it should probably raise an exception for 0 or 1 arguments, but I'm lazy): def xor(*operands): if operands: operands = list(operands) a = bool(operands.pop(0)) while operands: b = bool(operands.pop(0)) if a: if b: a = False elif b: a = True return a return False From jeanmichel at sequans.com Wed Jul 15 15:05:16 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 21:05:16 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: <4A5E286C.5060607@sequans.com> Miles Kaufmann wrote: > > On Jul 15, 2009, at 1:43 PM, Jean-Michel Pichavant wrote: > >> Hrvoje Niksic wrote: >> [snip] >>> Note that in Python A or B is in fact not equivalent to not(not A and >>> not B). >>> >> >>> l = [(True, True), (True, False), (False, True), (False, False)] >> >>> for p in l: >> ... p[0] or p[1] >> [snip] >> Did I make twice the same obvious error ? > > > Try again with: > > l = [('foo','bar'), ('foo', ''), ('', 'bar'), ('', '')] > > -Miles > Didn't know that. So if I resume: - not 'foo' => False - 'foo' or 'foo' => 'foo' I may be missing something, but honestly, Guido must have smoked some heavy stuff to write such logic, has he ? Let's play again False or 'foo' => 'foo' False and 'foo' => False So funny. I would have expected boolean operators to return a boolean value. I should have read the f*** manual (this is an anticipated RTFM counter-attack). Anyway Miles, thanks for the update. JM From invalid at invalid Wed Jul 15 15:05:30 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 15 Jul 2009 14:05:30 -0500 Subject: mail References: Message-ID: On 2009-07-15, amrita at iisermohali.ac.in wrote: > Sorry that I am disturbing you all again and again but this is the way I > am trying to solve my problem:--- We could probably be a lot more helpful if you would keep these postings all in a single thread so that people who didn't read the first postings knew what you were talking about in subsequent postings. >>>> import re >>>> exp = re.compile("CA") > [...] > > file is something lookin like:----- > > 47 8 ALA H H 7.85 0.02 1 [...] IMO you're making a mistake using the "re" module for this task. I think you'd be a lot better off if you just used the string type's split method to split each line up into fields and processed the individual fields: for line in inputfile: field = line.split() if field[2] == "CA": <> elif field[2] == "soemthing-else" <> <> -- Grant Edwards grante Yow! If I felt any more at SOPHISTICATED I would DIE visi.com of EMBARRASSMENT! From jcd at sdf.lonestar.org Wed Jul 15 15:40:36 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Wed, 15 Jul 2009 15:40:36 -0400 Subject: mail In-Reply-To: <7683.210.212.36.65.1247683589.squirrel@www.iisermohali.ac.in> References: <7683.210.212.36.65.1247683589.squirrel@www.iisermohali.ac.in> Message-ID: <1247686836.11677.196.camel@aalcdl07> On Thu, 2009-07-16 at 00:16 +0530, amrita at iisermohali.ac.in wrote: > Dear all, > > Sorry that I am disturbing you all again and again but this is the way I > am trying to solve my problem:--- > > >>> import re > >>> exp = re.compile("CA") > >>> infile = open("file1.txt") > >>> for line in infile: > ... values = re.split("\s+", line) > ... if exp.search(line): > ... print ("%s %s CA = %s" %(values[2], values[3], values[6])) > ... > with this it is giving the output like:---- > > 8 ALA CA = 54.67 > 15 ALA CA = 52.18 > 21 ALA CA = 54.33 > 23 ALA CA = 55.84 > 33 ALA CA = 55.58 > 38 ALA CA = 54.33 > > which is all right but i want CB and C value also in each row and it > should take value from 5th column infront of them, file is something > lookin like:----- > > 47 8 ALA H H 7.85 0.02 1 > 48 8 ALA HA H 2.98 0.02 1 > 49 8 ALA HB H 1.05 0.02 1 > 50 8 ALA C C 179.39 0.3 1 > 51 8 ALA CA C 54.67 0.3 1 > 52 8 ALA CB C 18.85 0.3 1 > 53 8 ALA N N 123.95 0.3 1 > 107 15 ALA H H 8.05 0.02 1 > 108 15 ALA HA H 4.52 0.02 1 > 109 15 ALA HB H 1.29 0.02 1 > 110 15 ALA C C 177.18 0.3 1 > 111 15 ALA CA C 52.18 0.3 1 > 112 15 ALA CB C 20.64 0.3 1 > 113 15 ALA N N 119.31 0.3 1 > 154 21 ALA H H 7.66 0.02 1 > 155 21 ALA HA H 4.05 0.02 1 > 156 21 ALA HB H 1.39 0.02 1 > 157 21 ALA C C 179.35 0.3 1 > 158 21 ALA CA C 54.33 0.3 1 > 159 21 ALA CB C 17.87 0.3 1 > 160 21 ALA N N 123.58 0.3 1 > 169 23 ALA H H 8.78 0.02 1 > 170 23 ALA HA H 4.14 0.02 1 > 171 23 ALA HB H 1.62 0.02 1 > 172 23 ALA C C 179.93 0.3 1 > 173 23 ALA CA C 55.84 0.3 1 > 174 23 ALA CB C 17.55 0.3 1 > 175 23 ALA N N 120.16 0.3 1 > 232 33 ALA H H 7.57 0.02 1 > 233 33 ALA HA H 3.89 0.02 1 > 234 33 ALA HB H 1.78 0.02 1 > 235 33 ALA C C 179.24 0.3 1 > 236 33 ALA CA C 55.58 0.3 1 > 237 33 ALA CB C 19.75 0.3 1 > 238 33 ALA N N 121.52 0.3 1 > 269 38 ALA H H 8.29 0.02 1 > 270 38 ALA HA H 4.04 0.02 1 > 271 38 ALA HB H 1.35 0.02 1 > 272 38 ALA C C 178.95 0.3 1 > 273 38 ALA CA C 54.33 0.3 1 > 274 38 ALA CB C 18.30 0.3 1 > 275 38 ALA N N 120.62 0.3 1 > > I just want that it will give output something like:----- > > 8 ALA C = 179.39 CA = 54.67 CB = 18.85 > 15 ALA C = 177.18 CA = 52.18 CB = 20.64 > 21 ALA C = 179.35 CA = 54.33 CB = 17.87..... > > so first it will write the position of the amino acid(given by second > column)then amino acid here it is ALA and then the corresponding value of > C, CA and CB from 5th colum for each position of ALA. > Your program is structured wrong for doing what you want. Right now you are essentially saying, "for each line in the input file, print something", but each line of the input file only has part of the information you want. Instead, you should create a data structure that gathers the pieces you want, and only print once you have all of those pieces in place. Step one, create your data structure, so the data is grouped the way you want it, not by line: >>> thingies = {} # create a dictionary >>> for line in infile: >>> data = line.split() >>> >>> if data[1] not in thingies: >>> # group data by data[1] >>> thingies[data[1]] = {} >>> >>> thingies[data[1]][data[3]] = data[5] Step two, extract the data from the list: >>> for key, data in thingies.items(): >>> print key, >>> for entry in data >>> print '%s = %s' % (entry, data[entry]), This should do what you want, minus some formatting issues. Cheers, Cliff From stefan_ml at behnel.de Wed Jul 15 15:48:13 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 15 Jul 2009 21:48:13 +0200 Subject: compiling python In-Reply-To: References: Message-ID: <4a5e327d$0$31339$9b4e6d93@newsspool4.arcor-online.net> Mag Gam wrote: > At my university we are trying to compile python with --enable-shared Is there a reason why you need to compile the CPython interpreter yourself? > however when I do a make many things fail. Is it a good idea to > compile python with shared libraries? Perfectly fine, Linux distros do that by default, for example. "many things fail" is not a very detailed problem description, though. Could you state more exactly what you do and provide the error messages that you see? Stefan From wbrehaut at mcsnet.ca Wed Jul 15 15:55:08 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 13:55:08 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Wed, 15 Jul 2009 11:51:44 -0700 (PDT), Mark Dickinson wrote: >On Jul 15, 7:29?pm, Wayne Brehaut wrote: >> On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >> >I'd also guess that 'xor' would be much less used than 'and' or 'or', >> >but maybe that's just a reflection of the sort of code that I tend to >> >write. >> >> You're right about that!. It's used everywhere in: >> [snip examples and references] > >Those examples are (almost) all about the *bitwise* xor operator, >which exists in Python ('^') and, as you point out, has no shortage of >good uses. The discussion was about a *logical* xor, to parallel the >existing 'and' and 'or' operators. I thought you were saying your program domains didn't include a lot of requirements for xor in general, rather than just no uses for Boolean xor--and I'm used to thinking of xor on binary vectors as "Boolean" anyway so would still have been confused. The most common non-binary-bit-wise xor is the general "symmetric difference" of sets, most likely to be useful in list or dictionary processing or database-like contexts. Please see my suggested use-case for Steven below. wayne From miheavner at gmail.com Wed Jul 15 15:55:56 2009 From: miheavner at gmail.com (mheavner) Date: Wed, 15 Jul 2009 12:55:56 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? Message-ID: I'm using multiprocessing to spawn several subprocesses, each of which uses a very large data structure (making it impractical to pass it via pipes / pickling). I need to allocate this structure once when the process is created and have it remain in memory for the duration of the process. The way the multiprocessing module is set up, only the 'run' method runs within the subprocess - so creating a wrapper class with a constructor that allocates the structure in __init__ will not work, as far as I know, as this will still be within the parent process. If I were working in C/C++, I would declare the variable "static" within the function body - is there any way with the multiprocessing module to have persistent data members within subprocesses? Any ideas?? Thanks, Matt From drozzy at gmail.com Wed Jul 15 16:00:06 2009 From: drozzy at gmail.com (drozzy) Date: Wed, 15 Jul 2009 13:00:06 -0700 (PDT) Subject: Reading a large csv file References: <1cbd6f830906222017m694bc738m40a46e2851381e@mail.gmail.com> <1cbd6f830906222227q7ec2a89ke3d4805c9ace80e8@mail.gmail.com> <4A41D1FD.7080004@simplistix.co.uk> Message-ID: On Jun 26, 6:47?am, Mag Gam wrote: > Thankyou everyone for the responses! I took some of your suggestions > and my loading sped up by 25% what a useless post... From hniksic at xemacs.org Wed Jul 15 16:00:52 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 15 Jul 2009 22:00:52 +0200 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <87bpnliumj.fsf@busola.homelinux.net> Jean-Michel Pichavant writes: > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> >>>> l = [(True, True), (True, False), (False, True), (False, False)] >>>> for p in l: > ... p[0] or p[1] [...] Try with a different data set, for example: >>> 10 or 20 10 >>> not(not 10 and not 20) True From deets at nospam.web.de Wed Jul 15 16:22:46 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 22:22:46 +0200 Subject: Persistent variable in subprocess using multiprocessing? In-Reply-To: References: Message-ID: <7c6s4nF25vs08U1@mid.uni-berlin.de> mheavner schrieb: > I'm using multiprocessing to spawn several subprocesses, each of which > uses a very large data structure (making it impractical to pass it via > pipes / pickling). I need to allocate this structure once when the > process is created and have it remain in memory for the duration of > the process. The way the multiprocessing module is set up, only the > 'run' method runs within the subprocess - so creating a wrapper class > with a constructor that allocates the structure in __init__ will not > work, as far as I know, as this will still be within the parent > process. > > If I were working in C/C++, I would declare the variable "static" > within the function body - is there any way with the multiprocessing > module to have persistent data members within subprocesses? Works for me, at least under OSX (and I presume *nixes in general work.) No idea about Windows. The thing to keep in mind is that forking is used, and that interpreter-state up to the moment of the fork is the same for all subprocesses. from multiprocessing import Process class MyProcess(Process): def __init__(self, huge_shared_state): self.huge_shared_state = huge_shared_state super(MyProcess, self).__init__() def run(self): print self.name, len(self.huge_shared_state) shared_state = range(1000000) processes = [] for i in xrange(10): p = MyProcess(shared_state) p.start() processes.append(p) for p in processes: p.join() Diez From db3l.net at gmail.com Wed Jul 15 16:26:17 2009 From: db3l.net at gmail.com (David Bolen) Date: Wed, 15 Jul 2009 16:26:17 -0400 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: Miles Kaufmann writes: > On Jul 14, 2009, at 5:06 PM, David Bolen wrote: >> Are you sure? It seems to restrict them in the same block, but not in >> the entire file. At least I was able to use both space and tab >> indented blocks in the same file with Python 3.0 and 3.1. > > It seems to me that, within an indented block, Python 3.1 requires > that you are consistent in your use of indentation characters *for > that indentation level*. For example, the following code seems to be > allowed: Um, right - in other words, what I said :-) -- David From rodriguealcazar at gmail.com Wed Jul 15 16:27:11 2009 From: rodriguealcazar at gmail.com (Rodrigue) Date: Wed, 15 Jul 2009 13:27:11 -0700 (PDT) Subject: python first assignment of a global variable References: Message-ID: <47c62f6f-cf9a-4f74-ad71-5f2eccc8c797@h2g2000yqg.googlegroups.com> > MY_GLOBAL, by virtue of being assigned to later in the function, and the > absence of a global statement, is identified as a local variable. > When a function contains a > single assignment (or augmented assignment) to a name, the compiler > generates bytecode such that all references to that name within the > function will be looked up in the local scope only Alright. I didn't know that. I thought the entire scope (local, then global) was considered in every situation. It does explain the observed behaviour then. I'm surprised I never bumped into that before, but I'm glad I learnt something new about python today. Thanks Emile and Miles for the explanation! From georgeoliverGO at gmail.com Wed Jul 15 16:39:37 2009 From: georgeoliverGO at gmail.com (George Oliver) Date: Wed, 15 Jul 2009 13:39:37 -0700 (PDT) Subject: interactive fiction in Python? Message-ID: <5c97224b-99d4-469b-90cb-1c04b1e1e9a0@m11g2000yqh.googlegroups.com> hi, I'm just curious who might be working on interactive fiction modules in the style of Inform or TADS for Python. I've seen a few threads on this list [1] (among many that mention IF tangentially), and there are old projects like PUB and PAWS. There are some newer potential projects such as Curveship as well. In any case I'd like to see what's out there. thanks, George [1]: http://bit.ly/Python_Text_Adventure_Authoring_System http://bit.ly/simple_interactive_fiction_engine http://bit.ly/adventure_engines_in_Python From perfreem at gmail.com Wed Jul 15 16:54:02 2009 From: perfreem at gmail.com (per) Date: Wed, 15 Jul 2009 13:54:02 -0700 (PDT) Subject: allowing output of code that is unittested? Message-ID: hi all, i am using the standard unittest module to unit test my code. my code contains several print statements which i noticed are repressed when i call my unit tests using: if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(TestMyCode) unittest.TextTestRunner(verbosity=2).run(suite) is there a way to allow all the print statements in the code that is being run by the unit test functions to be printed to stdio? i want to be able to see the output of the tested code, in addition to the output of the unit testing framework. thank you. From jkn_gg at nicorp.f9.co.uk Wed Jul 15 17:25:24 2009 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Wed, 15 Jul 2009 14:25:24 -0700 (PDT) Subject: Ann: Google releases Python-based open-source NX server Message-ID: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> Google quietly releases open-source NX server ...written in Python, apparently Neatx can be downloaded from Google's code repository: Regards J^n From magobin at gmail.com Wed Jul 15 17:26:53 2009 From: magobin at gmail.com (Alex) Date: Wed, 15 Jul 2009 14:26:53 -0700 (PDT) Subject: How to Force exiting from program/script Message-ID: hi at all, I have made a script with a while loop and I want that after 30 seconds the program stop and exit . But the code like this doesn't run: In the Console I can see work so that function is correctly called... #Function to exit def exit(): print "work" raise SystemExit() t = threading.Timer(30.0, exit) t.start() # Loop while True: ...many lines.... From deets at nospam.web.de Wed Jul 15 17:39:01 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 23:39:01 +0200 Subject: How to Force exiting from program/script In-Reply-To: References: Message-ID: <7c70jlF25pt2sU1@mid.uni-berlin.de> Alex schrieb: > hi at all, > I have made a script with a while loop and I want that after 30 > seconds the program stop and exit . But the code like this doesn't > run: > In the Console I can see work so that function is correctly called... > > #Function to exit > def exit(): > print "work" > raise SystemExit() > t = threading.Timer(30.0, exit) > t.start() > > # Loop > while True: > ...many lines.... > This works for me: import threading import time def work(): while True: pass t = threading.Thread(target=work) t.setDaemon(True) t.start() time.sleep(10) raise SystemExit The trick is to raise the SystemExit in the main-thread. Diez From deets at nospam.web.de Wed Jul 15 17:46:52 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 23:46:52 +0200 Subject: allowing output of code that is unittested? In-Reply-To: References: Message-ID: <7c712cF26q447U1@mid.uni-berlin.de> per schrieb: > hi all, > > i am using the standard unittest module to unit test my code. my code > contains several print statements which i noticed are repressed when i > call my unit tests using: > > if __name__ == '__main__': > suite = unittest.TestLoader().loadTestsFromTestCase(TestMyCode) > unittest.TextTestRunner(verbosity=2).run(suite) > > is there a way to allow all the print statements in the code that is > being run by the unit test functions to be printed to stdio? i want > to be able to see the output of the tested code, in addition to the > output of the unit testing framework. I use nosetests to discover & run tests, and that has an "-s"-option that does exactly that. Diez From mobiledreamers at gmail.com Wed Jul 15 18:01:15 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Wed, 15 Jul 2009 15:01:15 -0700 Subject: Ann: Google releases Python-based open-source NX server In-Reply-To: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> Message-ID: On Wed, Jul 15, 2009 at 2:25 PM, jkn wrote: > Google quietly releases open-source NX server ...written in Python, > apparently > > Google_quietly_releases_open_source_NX_server?taxonomyId=88> > > Neatx can be downloaded from Google's code repository: > > > > Regards > J^n > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.com Wed Jul 15 18:31:38 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 15 Jul 2009 23:31:38 +0100 Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On Wed, 15 Jul 2009 17:03:30 +0200, Jean-Michel Pichavant wrote: >> Hard-coding control/escape sequences is just lame. Use the curses modules >> to obtain the correct sequences for the terminal. >> >> > As the OP I'm really interested in doing so. I currently have all my > colors hard-coded. > Now It may be lame but as soon as I call curses.initscr(), it's just > messing up with my terminal, Use curses.setupterm() to locate and parse the terminfo/termcap entry without entering "curses mode". Most curses functions won't work without calling initscr(), but the terminfo functions will. > moreover I didn't figure out how to "print > 'hello'" using curses color codes. > Anyone has an example ? I'm pretty sure it may fit in one line. #!/usr/bin/env python import curses curses.setupterm() setaf = curses.tigetstr('setaf') if not setaf: setaf = '' print (curses.tparm(setaf,1) + "hello, " + curses.tparm(setaf,2) + "world" + curses.tparm(setaf,0)) From mmanns at gmx.net Wed Jul 15 18:36:51 2009 From: mmanns at gmx.net (mmanns at gmx.net) Date: Thu, 16 Jul 2009 00:36:51 +0200 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <20090716003651.09bca73c@Knock> On Sat, 11 Jul 2009 15:33:32 -0700 (PDT) Daniel Platz wrote: > thanks for your repleys. I have tried matplotlib but it is extremely > slow. I think it is more optimized for good looking plots instead of > speed. I do not know the Python bindings of gnuplot and Veusz. To > clarify the issue again, by 25000 data points I mean 25000 pixels, > i.e. corresponding (x,y) pairs. Thus the mapping to one pixel on the > screen is not unique. Have you already tried out rpy? Martin From shai at platonix.com Wed Jul 15 18:45:57 2009 From: shai at platonix.com (Shai) Date: Wed, 15 Jul 2009 15:45:57 -0700 (PDT) Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> <42fbcee1-61b2-4ba8-a50a-39c9c0d2fe9c@k19g2000yqn.googlegroups.com> Message-ID: Since nobody else mentioned this... Python classes have a magic method called __del__ which is usually called just before an object is garbage-collected. Further, Python uses reference-counting to tell when an object is no longer accessible. This means that if your resource classes define __del__ methods, these will be called properly when the object using them is destroyed, and you don't need to write an explicit close() method for this. class Resource(object): def __init__(self, param): # acquire resource def __del__(self): # release resource not_my_responsibility = Resource(1) class Foo(object): def __init__(self): self.ref = not_my_responsibility # self.ref.__del__() will not be called as long as the module exists local = Resource(2) # local.__del__() will be called as soon as __init__ is exited self.held = Resource(3) # self.held.__del__() will be called when the object dies z = 1/0 # The exception in the initializer will kill the object, triggering some Resource.__del__() calls There are two caveats: 1) __del__ methods prevent instances of your class from being collected when they are involved in cyclical structures; this means if your structures start to get complex (sometimes a doubly-linked list is complex enough), you may find yourself leaking memory. 2) The bit about reference counting, which allows predictable destruction, holds for CPython, but not for Jython, and IIRC also not for IronPython (I don't know about PyPy or other implementations). It is a feature of the reference implementation, not the language definition. From wbrehaut at mcsnet.ca Wed Jul 15 18:54:00 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 16:54:00 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: <7bns55dch1pler31ai2ivdp1u6nk60lnfo@4ax.com> On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" >wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to >> have an 'xor' operator as well. > >Hmm. I don't think 'nice' is sufficient. You'd need to make the case >that it's sufficiently useful to justify adding a new keyword 'xor' to >the language; I suspect that would be an uphill struggle. :) === 8< === And for the objects for which it *is* sufficiently useful (sets) the xor operator ^ is available: >>> cheese = set(['cheddar', 'limburger', 'stilton']) >>> stinky = set(['skunk', 'limburger', 'stilton', 'polecat', 'doggy-doo', 'civet']) >>> nasty = set(['doggy-doo', 'polecat', 'limburger', 'Perl']) >>> cheese & stinky # stinky cheese set(['limburger', 'stilton']) >>> cheese ^ stinky # either cheese or stinky but not both set(['doggy-doo', 'civet', 'polecat', 'skunk', 'cheddar']) >>> cheese ^ stinky ^ nasty # in an odd number of these sets (1 or 3) set(['civet', 'cheddar', 'Perl', 'limburger', 'skunk']) wayne From wbrehaut at mcsnet.ca Wed Jul 15 18:57:59 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 16:57:59 -0600 Subject: missing 'xor' Boolean operator References: Message-ID: On 15 Jul 2009 09:11:44 GMT, Steven D'Aprano wrote: >On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > >> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >> to have an 'xor' operator as well. > >I've often wished there was too, for the sake of completeness and >aesthetics, I'd love to be able to write: > >a xor b > >instead of defining a function xor(a, b). > >Unfortunately, outside of boolean algebra and simulating electrical >circuits, I can't think of any use-cases for an xor operator. Do you have >any? Since xor in set theory is the symmetric difference, perhaps we'd like to know all items in exactly one of two word lists or dictionaries, or anything else we could easily set-ize: >>> cheese = set(['cheddar', 'limburger', 'stilton']) >>> stinky = set(['skunk', 'limburger', 'stilton', 'polecat', 'doggy-doo', 'civet']) >>> nasty = set(['doggy-doo', 'polecat', 'limburger', 'Perl']) >>> cheese & stinky # stinky cheese set(['limburger', 'stilton']) >>> cheese ^ stinky # either cheese or stinky but not both set(['doggy-doo', 'civet', 'polecat', 'skunk', 'cheddar']) >>> cheese ^ stinky ^ nasty # in an odd number of these sets (1 or 3) set(['civet', 'cheddar', 'Perl', 'limburger', 'skunk']) Who hasn't needed that occasionally? wayne From nobody at nowhere.com Wed Jul 15 19:01:37 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 00:01:37 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > So if I resume: > - not 'foo' => False > - 'foo' or 'foo' => 'foo' > > I may be missing something, but honestly, Guido must have smoked some > heavy stuff to write such logic, has he ? Several languages (e.g. Lisp, Bourne shell) behave the same way, i.e. "or" returns the first element which is considered true while "and" returns the last element provided that all preceding elements are considered true. > Let's play again > False or 'foo' => 'foo' > False and 'foo' => False > > So funny. I would have expected boolean operators to return a boolean > value. In Python, almost everything is a boolean value. Compare with Lisp, where everything is a boolean value: nil (the empty list) is false and everything else (including integer zero) is true. From clp2 at rebertia.com Wed Jul 15 19:02:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 16:02:13 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: Message-ID: <50697b2c0907151602j393935eas4d5e18394baaa639@mail.gmail.com> On Wed, Jul 15, 2009 at 3:57 PM, Wayne Brehaut wrote: > On 15 Jul 2009 09:11:44 GMT, Steven D'Aprano > wrote: > >>On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: >> >>> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice >>> to have an 'xor' operator as well. >> >>I've often wished there was too, for the sake of completeness and >>aesthetics, I'd love to be able to write: >> >>a xor b >> >>instead of defining a function xor(a, b). >> >>Unfortunately, outside of boolean algebra and simulating electrical >>circuits, I can't think of any use-cases for an xor operator. Do you have >>any? > > Since xor in set theory is the symmetric difference, ?perhaps we'd > like to know all items in exactly one of two word lists or > dictionaries, or anything else we could easily set-ize: > >>>> cheese = set(['cheddar', 'limburger', 'stilton']) >>>> stinky = set(['skunk', 'limburger', 'stilton', 'polecat', 'doggy-doo', 'civet']) >>>> nasty = set(['doggy-doo', 'polecat', 'limburger', 'Perl']) >>>> cheese & stinky # stinky cheese > set(['limburger', 'stilton']) >>>> cheese ^ stinky # either cheese or stinky but not both > set(['doggy-doo', 'civet', 'polecat', 'skunk', 'cheddar']) >>>> cheese ^ stinky ^ nasty # in an odd number of these sets (1 or 3) > set(['civet', 'cheddar', 'Perl', 'limburger', 'skunk']) > > Who hasn't needed that occasionally? This discussion is about adding a *logical* operator for use in boolean expressions. We obviously already have ^ for non-boolean use. Cheers, Chris -- http://blog.rebertia.com From pdlemper at earthlink.net Wed Jul 15 20:07:18 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Wed, 15 Jul 2009 19:07:18 -0500 Subject: Can module tell if running from interpreter vs Windows command line ? Message-ID: The WConio console module produces different colors, sometimes quite different, when run from Windows command line vs from Python interpreter >>> . A good foregnd/backgnd combination under one may be unreadable under the other : ( I'm using Python 3.0 with the corresponding WConio on XP. Is there any way for the module to detect under which it has been started ? Thanks From pavlovevidence at gmail.com Wed Jul 15 20:18:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 15 Jul 2009 17:18:52 -0700 (PDT) Subject: import module unbelieveable behaviour References: Message-ID: On Jul 15, 6:12?am, Peter Fodrek wrote: > Dear conference! > > I have test Why python based script for HeeksCNC post-processing does not > work... ?And I've got unbelievable behavior ?When importing module module > manually it works, but same opertaion from script does not > ?work as seen > > /opt/HeeksCAD8/HeeksCNC> python > Python 2.6 (r26:66714, Feb ?3 2009, 20:49:49) > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > Type "help", "copyright", "credits" or "license" for more information.>>> import nc.rez > > /opt/HeeksCAD8/HeeksCNC> python test.py > Traceback (most recent call last): > ? File "test.py", line 7, in > ? ? import nc.rez > ImportError: No module named rez > > /opt/HeeksCAD8/HeeksCNC> python ./test.py > Traceback (most recent call last): > ? File "./test.py", line 7, in > ? ? import nc.rez > ImportError: No module named rez > > Would anyone be helpful for me to get more information about this problem > because ?pydb does not show anything usable for me,please? That's a tricky one, indeed. Here's my guess: test.py is a symlink to a file in another directory. A major difference between interactive and script mode is the value of sys.path[0], which is the directory Python uses for local modules and packages. Python uses the current directory when running in interactive mode, but it uses the directory where the script is located when it is called on a script. Normally, when you run python on a script in the current directory, the behavior is the same as interactive mode, because the directory where the script is located is the current directory. But there's the gotcha: when you run python on symlinked file, Python sets sys.path[0] to the directory containing the actual file, not the directory containing the symlink. Thus even if the symlink is in the current directory, sys.path[0] is set to a different one. As a workaround, test.py should explicitly add '' to sys.path: sys.path.insert(0,'') There is another notable difference in behavior between the two modes. In interactive mode, sys.path[0] is ''. If you were to change the current directory from within Python, using os.chdir(), then future imports will occur relative to the new current directory. However, if you run Python from a script, sys.path[0] is an actual directory name, so that even if you chdir, any future imports will still occur relative to the original directory. Pretty confusing if you ask me. Unfortunately Python is very unPythonic when it comes to importing. :( Carl Banks From http Wed Jul 15 20:32:34 2009 From: http (Paul Rubin) Date: 15 Jul 2009 17:32:34 -0700 Subject: missing 'xor' Boolean operator References: Message-ID: <7x3a8xwjq5.fsf@ruckus.brouhaha.com> Anthony Tolle writes: > def xor(*operands): > if operands: > operands = list(operands) > a = bool(operands.pop(0)) > while operands: > b = bool(operands.pop(0)) > if a: > if b: > a = False > elif b: > a = True > return a > return False Among other things, that uses quadratic time! Why do you want to keep popping items from that list instead of iterating through it anyway? Anyway, I think you wrote something close to this: def xor(*operands): r = False for x in operands: r = (r != bool(x)) return r or in map-reduce style: from operator import ne def xor(*operands): return reduce(ne, map(bool, operands), False) From wuwei23 at gmail.com Wed Jul 15 20:41:41 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 17:41:41 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: Message-ID: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> On Jul 16, 10:07?am, pdlem... at earthlink.net wrote: > The WConio console module produces different colors, sometimes quite > different, when run from Windows command line vs from Python > interpreter >>> . ?A good foregnd/backgnd combination under one > may be unreadable under the other ?: ( > I'm using Python 3.0 with the corresponding WConio on XP. > Is there any way for the module to detect under which it has been > started ? ? ?Thanks It's recommended that you search through the list for similar questions before posting. This was covered just yesterday: http://groups.google.com/group/comp.lang.python/browse_frm/thread/fb2f075ccc796b63# From wuwei23 at gmail.com Wed Jul 15 20:47:39 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 17:47:39 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> Message-ID: <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> On Jul 16, 10:41?am, alex23 wrote: > It's recommended that you search through the list for similar > questions before posting. Of course, it's even MORE important that one actually ensures they're responding to the _same_ question before pointing at an answer... This older post should help: http://groups.google.com/group/comp.lang.python/browse_frm/thread/6c587ba377ae045a/0df10b077f5ee5d4 But the quick answer is to import sys into your program, and do a test on hasattr(sys, 'ps1'), which is only created when running the interactive prompt. From pdlemper at earthlink.net Wed Jul 15 21:36:57 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Wed, 15 Jul 2009 20:36:57 -0500 Subject: Can module tell if running from interpreter vs Windows command line ? References: Message-ID: On Wed, 15 Jul 2009 19:07:18 -0500, pdlemper at earthlink.net wrote: >The WConio console module produces different colors, sometimes quite >different, when run from Windows command line vs from Python >interpreter >>> . A good foregnd/backgnd combination under one >may be unreadable under the other : ( >I'm using Python 3.0 with the corresponding WConio on XP. >Is there any way for the module to detect under which it has been >started ? Thanks Thanks Alex It worked : import sys hasattr(sys, 'ps1') yields True when started from interpreter >>> and False run from Windows command line Dave WB3DWE From wuwei23 at gmail.com Wed Jul 15 22:30:11 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 19:30:11 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> Message-ID: <30a6179e-f08a-4809-8c3d-50e083114d50@y10g2000prg.googlegroups.com> On Jul 15, 5:51?pm, koranthala wrote: > ? ?I am not saying that what you said was wrong, only that I felt that > she got tense looking up regular expressions. So a python reply which > basically does the basic checking without going to re etc might be > more helpful for her to start her own coding. Using regexps wasn't the only advice given, and string manipulation is covered well in pretty much every tutorial I've taken a look at. The issue is that no evidence was shown that the OP was even trying to resolve this themself, and the new posting of the question every time an unsatisfactory answer was provided doesn't give me any faith they were endeavouring to do this at all. It's annoying seeing these "stone soup" attempts at coding come through here, whether its to answer someone's homework or to do someone's job for them. If the end result has value to the poster, I expect to see either effort on their behalf or at least a nominal attempt to reward others for their time. But if you want to do their work for them, I don't think anyone here would object, especially if you both took it to private correspondence. From afriere at yahoo.co.uk Wed Jul 15 22:56:38 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Wed, 15 Jul 2009 19:56:38 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> Message-ID: <0fc81395-7787-48a9-93de-cf7a274b691e@u38g2000pro.googlegroups.com> On Jul 16, 10:47?am, alex23 wrote: ... > This older post should help:http://groups.google.com/group/comp.lang.python/browse_frm/thread/6c5... > > But the quick answer is to import sys into your program, and do a test > on hasattr(sys, 'ps1'), which is only created when running the > interactive prompt. As you note there, this will work when running the vanilla shell (ie running it from the command line), but not (potentially) in other interactive environments (IronPython being the example you give). Another instance: there is not sys.ps1 when running a python shell under idle. Since this solution differs whether the interactive session is taking place from the cmd line, idle, IronPython etc. it seems to me not terribly robust. Depending on the use case, it is of course easy to tell whether the module was executed on the command line, or imported (from an interactive shell or another script) using the __name__ trick. (eg. is_imported = __name__ == '__main__') From koranthala at gmail.com Wed Jul 15 23:42:51 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 20:42:51 -0700 (PDT) Subject: mail References: Message-ID: <55d3cd26-44a2-4b87-8bb4-988e8c397833@v15g2000prn.googlegroups.com> On Jul 15, 11:46?pm, amr... at iisermohali.ac.in wrote: > Dear all, > > Sorry that I am disturbing you all again and again but this is the way I > am trying to solve my problem:--- > > >>> import re > >>> exp = re.compile("CA") > >>> infile = open("file1.txt") > >>> for line in infile: > > ... ? ? values = re.split("\s+", line) > ... ? ? if exp.search(line): > ... ? ? ? ?print ("%s %s CA = %s" %(values[2], values[3], values[6])) > ... > ?with this it is giving the output like:---- > > 8 ALA CA = 54.67 > 15 ALA CA = 52.18 > 21 ALA CA = 54.33 > 23 ALA CA = 55.84 > 33 ALA CA = 55.58 > 38 ALA CA = 54.33 > > which is all right but i want CB and C value also in each row and it > should take value from 5th column infront of them, file is something > lookin like:----- > > ?47 ? ? 8 ? ALA ? ? ? H ? ? H ? ? ?7.85 ? ? 0.02 ? ? 1 > ?48 ? ? 8 ? ALA ? ? ? HA ? ?H ? ? ?2.98 ? ? 0.02 ? ? 1 > ?49 ? ? 8 ? ALA ? ? ? HB ? ?H ? ? ?1.05 ? ? 0.02 ? ? 1 > ?50 ? ? 8 ? ALA ? ? ? C ? ? C ? ?179.39 ? ? ?0.3 ? ? 1 > ?51 ? ? 8 ? ALA ? ? ? CA ? ?C ? ? 54.67 ? ? ?0.3 ? ? 1 > ?52 ? ? 8 ? ALA ? ? ? CB ? ?C ? ? 18.85 ? ? ?0.3 ? ? 1 > ?53 ? ? 8 ? ALA ? ? ? N ? ? N ? ?123.95 ? ? ?0.3 ? ? 1 > 107 ? ?15 ? ALA ? ? ? H ? ? H ? ? ?8.05 ? ? 0.02 ? ? 1 > 108 ? ?15 ? ALA ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > 109 ? ?15 ? ALA ? ? ? HB ? ?H ? ? ?1.29 ? ? 0.02 ? ? 1 > 110 ? ?15 ? ALA ? ? ? C ? ? C ? ?177.18 ? ? ?0.3 ? ? 1 > 111 ? ?15 ? ALA ? ? ? CA ? ?C ? ? 52.18 ? ? ?0.3 ? ? 1 > 112 ? ?15 ? ALA ? ? ? CB ? ?C ? ? 20.64 ? ? ?0.3 ? ? 1 > 113 ? ?15 ? ALA ? ? ? N ? ? N ? ?119.31 ? ? ?0.3 ? ? 1 > 154 ? ?21 ? ALA ? ? ? H ? ? H ? ? ?7.66 ? ? 0.02 ? ? 1 > 155 ? ?21 ? ALA ? ? ? HA ? ?H ? ? ?4.05 ? ? 0.02 ? ? 1 > 156 ? ?21 ? ALA ? ? ? HB ? ?H ? ? ?1.39 ? ? 0.02 ? ? 1 > 157 ? ?21 ? ALA ? ? ? C ? ? C ? ?179.35 ? ? ?0.3 ? ? 1 > 158 ? ?21 ? ALA ? ? ? CA ? ?C ? ? 54.33 ? ? ?0.3 ? ? 1 > 159 ? ?21 ? ALA ? ? ? CB ? ?C ? ? 17.87 ? ? ?0.3 ? ? 1 > 160 ? ?21 ? ALA ? ? ? N ? ? N ? ?123.58 ? ? ?0.3 ? ? 1 > 169 ? ?23 ? ALA ? ? ? H ? ? H ? ? ?8.78 ? ? 0.02 ? ? 1 > 170 ? ?23 ? ALA ? ? ? HA ? ?H ? ? ?4.14 ? ? 0.02 ? ? 1 > 171 ? ?23 ? ALA ? ? ? HB ? ?H ? ? ?1.62 ? ? 0.02 ? ? 1 > 172 ? ?23 ? ALA ? ? ? C ? ? C ? ?179.93 ? ? ?0.3 ? ? 1 > 173 ? ?23 ? ALA ? ? ? CA ? ?C ? ? 55.84 ? ? ?0.3 ? ? 1 > 174 ? ?23 ? ALA ? ? ? CB ? ?C ? ? 17.55 ? ? ?0.3 ? ? 1 > 175 ? ?23 ? ALA ? ? ? N ? ? N ? ?120.16 ? ? ?0.3 ? ? 1 > 232 ? ?33 ? ALA ? ? ? H ? ? H ? ? ?7.57 ? ? 0.02 ? ? 1 > 233 ? ?33 ? ALA ? ? ? HA ? ?H ? ? ?3.89 ? ? 0.02 ? ? 1 > 234 ? ?33 ? ALA ? ? ? HB ? ?H ? ? ?1.78 ? ? 0.02 ? ? 1 > 235 ? ?33 ? ALA ? ? ? C ? ? C ? ?179.24 ? ? ?0.3 ? ? 1 > 236 ? ?33 ? ALA ? ? ? CA ? ?C ? ? 55.58 ? ? ?0.3 ? ? 1 > 237 ? ?33 ? ALA ? ? ? CB ? ?C ? ? 19.75 ? ? ?0.3 ? ? 1 > 238 ? ?33 ? ALA ? ? ? N ? ? N ? ?121.52 ? ? ?0.3 ? ? 1 > 269 ? ?38 ? ALA ? ? ? H ? ? H ? ? ?8.29 ? ? 0.02 ? ? 1 > 270 ? ?38 ? ALA ? ? ? HA ? ?H ? ? ?4.04 ? ? 0.02 ? ? 1 > 271 ? ?38 ? ALA ? ? ? HB ? ?H ? ? ?1.35 ? ? 0.02 ? ? 1 > 272 ? ?38 ? ALA ? ? ? C ? ? C ? ?178.95 ? ? ?0.3 ? ? 1 > 273 ? ?38 ? ALA ? ? ? CA ? ?C ? ? 54.33 ? ? ?0.3 ? ? 1 > 274 ? ?38 ? ALA ? ? ? CB ? ?C ? ? 18.30 ? ? ?0.3 ? ? 1 > 275 ? ?38 ? ALA ? ? ? N ? ? N ? ?120.62 ? ? ?0.3 ? ? 1 > > I just want that it will give output something like:----- > > 8 ?ALA ?C = 179.39 ?CA = 54.67 ?CB = 18.85 > 15 ALA ?C = 177.18 ?CA = 52.18 ?CB = 20.64 > 21 ALA ?C = 179.35 ?CA = 54.33 ?CB = 17.87..... > > so first it will write the position of the amino acid(given by second > column)then amino acid here it is ALA and then the corresponding value of > C, CA and CB from 5th colum for each position of ALA. > > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA Hi, Can you try using the code which I suggested in the earlier thread? If you cannot use it to get the output, just point it out here, and I will try to solve the issue. From nagle at animats.com Thu Jul 16 00:03:58 2009 From: nagle at animats.com (John Nagle) Date: Wed, 15 Jul 2009 21:03:58 -0700 Subject: does python have a generic object pool like commons-pool in Java In-Reply-To: <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> Message-ID: <4a5ea61b$0$1682$742ec2ed@news.sonic.net> Rick Lawson wrote: > On Jul 15, 4:53 am, Jonathan Gardner > wrote: >> On Jul 14, 6:34 pm, Rick Lawson wrote: >> >>> Appreciate any help on this. I am porting an app from Java to python >>> and need generic object pooling with hooks for object initialization / >>> cleanup and be able to specify an object timeout. >> Are you looking for something like a thread pool or a connection pool? >> Such a thing is easy to code in Python. Might as well write one from >> scratch for your particular need. >> >> By the way, a tip for writing Python: Forget Java. If you're porting >> an app, consider rewriting it from the architecture on up. You'll save >> yourself time and get a better result. > > Jonathan, > > Thanks for the advice but would like to pick your brain a little more. > The pool I need is not a thread pool or db pool. I need to pool > objects which are a proxy for an external C process that communicates > via sockets. "fcgi" is an option for this sort of thing. With "mod_fcgi" installed in Apache, and "fcgi.py" used to manage the Python side of the problem, you can have semi-persistent programs started up and shut down for you on the server side. I use this for "sitetruth.com". It's useful when CGI is too slow because the cost of launching Python and loading all the modules is far greater than the cost of processing the request. Once you find and install a working Apache "mod_fcgi" module, and a working "fcgi.py" module (I suggest "http://svn.saddi.com/py-lib/trunk/fcgi.py") writing the server side app is no harder than with CGI. But the performance is far better. John Nagle From wuwei23 at gmail.com Thu Jul 16 00:14:37 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 21:14:37 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> <0fc81395-7787-48a9-93de-cf7a274b691e@u38g2000pro.googlegroups.com> Message-ID: <35b0b936-6bc1-4acf-aa27-6cc0db84292b@y10g2000prf.googlegroups.com> On Jul 16, 12:56?pm, Asun Friere wrote: > As you note there, this will work when running the vanilla shell (ie > running ?it from the command line), but not (potentially) in other > interactive environments (IronPython being the example you give). Actually, that was IPython, which is an enhanced interactive prompt, and a totally different beastie altogether :) > Another instance: ?there is not sys.ps1 when running a python shell > under idle. ?Since this solution differs whether the interactive > session is taking place from the cmd line, idle, IronPython etc. it > seems to me not terribly robust. Well, none of {idle, IronPython, IPython} were specified by the OP AND you're citing back what I myself wrote in the link to which I also referred the OP - whom has subsequently posted his success with this technique - so I'm not really sure what the point is here other than "if you have a different use case, you'll need a different solution"... > Depending on the use case, it is of course easy to tell whether the > module was executed on the command line, or imported (from an > interactive shell or another script) using the __name__ trick. (eg. > is_imported = __name__ == '__main__') That should be: is_imported = __name__ != '__main__' And such a test is all well and good if the main code body is the one that needs to know about the execution mode, but if you need to know under which conditions the program is being run within a module imported by the main body, well, that check is _always_ going to be true... From phostu at gmail.com Thu Jul 16 00:18:39 2009 From: phostu at gmail.com (Vincent) Date: Wed, 15 Jul 2009 21:18:39 -0700 (PDT) Subject: problem about cx_Oracle Message-ID: <957ab843-92ac-4072-ace4-441a2da0d130@m3g2000pri.googlegroups.com> hi, all: i am using cx_oracle now. i write code as below: def __getfields_by_tbname(self,tbname): cursor = self.ora_db.cursor() print tbname sql = 'select * from %s where rownum <=2' % tbname print sql cursor = cursor.execute(sql) return self.getfields(cursor) and i got a error, it's message is : JRYZCFZB_X_ZQY select * from JRYZCFZB_X_ZQY where rownum <=2 Traceback (most recent call last): File "", line 1, in File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools \data_convert.py", line 107, in convert self.convert_table(tbname) File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools \data_convert.py", line 94, in convert_table field_list = self.__getfields_by_tbname(tbname) File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools \data_convert.py", line 38, in __getfields_by_tbname cursor = cursor.execute(sql) TypeError: expecting None or a string i'm sure the cursor instance is not None. could anybody give me sussgestion? i will apreciate it. vincent From koranthala at gmail.com Thu Jul 16 00:29:04 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 21:29:04 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> <30a6179e-f08a-4809-8c3d-50e083114d50@y10g2000prg.googlegroups.com> Message-ID: On Jul 16, 7:30?am, alex23 wrote: > On Jul 15, 5:51?pm, koranthala wrote: > > > ? ?I am not saying that what you said was wrong, only that I felt that > > she got tense looking up regular expressions. So a python reply which > > basically does the basic checking without going to re etc might be > > more helpful for her to start her own coding. > > Using regexps wasn't the only advice given, and string manipulation is > covered well in pretty much every tutorial I've taken a look at. The > issue is that no evidence was shown that the OP was even trying to > resolve this themself, and the new posting of the question every time > an unsatisfactory answer was provided doesn't give me any faith they > were endeavouring to do this at all. > > It's annoying seeing these "stone soup" attempts at coding come > through here, whether its to answer someone's homework or to do > someone's job for them. If the end result has value to the poster, I > expect to see either effort on their behalf or at least a nominal > attempt to reward others for their time. > > But if you want to do their work for them, I don't think anyone here > would object, especially if you both took it to private correspondence. It is not that I do want to do the work for them. It is just that I was in the same position just 6 months back. My first pieces of code were very poor - full of errors and quite horrible indeed. I was even afraid to post it anywhere. I came here, and I found so much helpful people who helped me out so much, that I think my coding has improved a little bit. So, I just thought that we should always give people the benefit of doubt. From wuwei23 at gmail.com Thu Jul 16 00:30:40 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 21:30:40 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <4a5ea61b$0$1682$742ec2ed@news.sonic.net> Message-ID: <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> On Jul 16, 2:03?pm, John Nagle wrote: > ? ? ?"fcgi" is an option for this sort of thing. ?With "mod_fcgi" installed > in Apache, and "fcgi.py" used to manage the Python side of the problem, you > can have semi-persistent programs started up and shut down for you on the > server side. Hey John, The environments in which I've been asked to develop webs apps using Python have all utilised mod_wsgi. Do you have any experience with mod_wsgi vs mod_fcgi, and if so, can you comment on any relevant performance / capability / ease-of-use differences? Cheers, alex23 From akhilanger at gmail.com Thu Jul 16 00:34:06 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 21:34:06 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) Message-ID: <24509879.post@talk.nabble.com> Hi! Can anyone please help me getting rid of this error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) I am not a python programmer (though intend to start learning this wonderful language), I am just using a python script. After doing some search, I found that 0xb7 is a 'middle dot character' that is not interpreted by the python. Even after inserting text = text.replace('\u00b7', '') in the script, the problem still persists. Can anyone please tell me the easiest way to get rid of this? --Thanks, Akhil -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24509879.html Sent from the Python - python-list mailing list archive at Nabble.com. From clp2 at rebertia.com Thu Jul 16 00:39:19 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 21:39:19 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24509879.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> Message-ID: <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: > > Hi! > > Can anyone please help me getting rid of this error: > UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position > 13: ordinal not in range(128) > > I am not a python programmer (though intend to start learning this wonderful > language), I am just using a python script. > > After doing some search, I found that 0xb7 is a 'middle dot character' that > is not interpreted by the python. > Even after inserting text = text.replace('\u00b7', '') in the script, the > problem still persists. > > Can anyone please tell me the easiest way to get rid of this? We'll need the full error traceback. The error message at the end is just not enough information. As to fixing it, google for "UnicodeEncodeError". You should find about a million mailinglist threads on it. Cheers, Chris -- http://blog.rebertia.com From wuwei23 at gmail.com Thu Jul 16 00:41:31 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 21:41:31 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> <30a6179e-f08a-4809-8c3d-50e083114d50@y10g2000prg.googlegroups.com> Message-ID: <0f28ec7c-f0cc-4cfb-a518-e65dba6e094c@12g2000pri.googlegroups.com> On Jul 16, 2:29?pm, koranthala wrote: > It is not that I do want to do the work for them. It is just that I > was in the same position just 6 months back. My first pieces of code > were very poor - full of errors and quite horrible indeed. I was even > afraid to post it anywhere. I came here, and I found so much helpful > people who helped me out so much, that I think my coding has improved > a little bit. So, I just thought that we should always give people the > benefit of doubt. Which is why I waited until the 5th repeated post on the topic before commenting. From nagle at animats.com Thu Jul 16 01:05:01 2009 From: nagle at animats.com (John Nagle) Date: Wed, 15 Jul 2009 22:05:01 -0700 Subject: does python have a generic object pool like commons-pool in Java In-Reply-To: <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <4a5ea61b$0$1682$742ec2ed@news.sonic.net> <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> Message-ID: <4a5eb46a$0$1673$742ec2ed@news.sonic.net> alex23 wrote: > On Jul 16, 2:03 pm, John Nagle wrote: >> "fcgi" is an option for this sort of thing. With "mod_fcgi" installed >> in Apache, and "fcgi.py" used to manage the Python side of the problem, you >> can have semi-persistent programs started up and shut down for you on the >> server side. > > Hey John, > > The environments in which I've been asked to develop webs apps using > Python have all utilised mod_wsgi. Do you have any experience with > mod_wsgi vs mod_fcgi, and if so, can you comment on any relevant > performance / capability / ease-of-use differences? > > Cheers, > alex23 FCGI seems to be somewhat out of favor, perhaps because it isn't complicated enough. It's a mature technology and works reasonably well. It's been a puzzle to me that FCGI was taken out of the main Apache code base, because it gives production-level performance with CGI-type simplicity. WSGI has a mode for running Python inside the Apache process, which is less secure and doesn't allow multiple Python processes. That complicates mod_wsgi considerably, and ties it very closely to specific versions of Python and Python modules. As a result, the WSGI developers are patching at a great rate. I think the thing has too many "l33t features". I'd avoid "embedded mode". "Daemon mode" is the way to go if you use WSGI. I haven't tried WSGI; I don't need the grief of a package under heavy development. John Nagle From akhilanger at gmail.com Thu Jul 16 01:05:20 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 22:05:20 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> Message-ID: <24510222.post@talk.nabble.com> Well, All I get is this traceback: File "./customWikiExtractor.py", line 492, in ? main() File "./customWikiExtractor.py", line 480, in main print >> sys.stdout, 'line: %s' % line UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) I am giving a string to the python code as input, and python processes it like this: line = line.decode('utf-8').strip() After this when I do, print >> sys.stdout, 'line: %s' % line I get this Unicode error. I tried a few repairs, but they did not work like changing: in sgmmlib.py (/usr/lib64/python2.4/sgmmlib.py) if not 0 < n <= 255 to if not 0 < n <= 127 But since this did not work, I have changed it back to it's original form. --Thanks, Akhil Chris Rebert-6 wrote: > > On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >> >> Hi! >> >> Can anyone please help me getting rid of this error: >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >> position >> 13: ordinal not in range(128) >> >> I am not a python programmer (though intend to start learning this >> wonderful >> language), I am just using a python script. >> >> After doing some search, I found that 0xb7 is a 'middle dot character' >> that >> is not interpreted by the python. >> Even after inserting text = text.replace('\u00b7', '') in the script, the >> problem still persists. >> >> Can anyone please tell me the easiest way to get rid of this? > > We'll need the full error traceback. The error message at the end is > just not enough information. > As to fixing it, google for "UnicodeEncodeError". You should find > about a million mailinglist threads on it. > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24510222.html Sent from the Python - python-list mailing list archive at Nabble.com. From akhilanger at gmail.com Thu Jul 16 01:09:38 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 22:09:38 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24510222.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> Message-ID: <24510252.post@talk.nabble.com> Sorry, it is sgmllib.py and not sgmmlib.py -- Akhil akhil1988 wrote: > > Well, > All I get is this traceback: > > File "./customWikiExtractor.py", line 492, in ? > main() > File "./customWikiExtractor.py", line 480, in main > print >> sys.stdout, 'line: %s' % line > UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in > position 13: ordinal not in range(128) > > I am giving a string to the python code as input, and python processes it > like this: > > line = line.decode('utf-8').strip() > > After this when I do, > print >> sys.stdout, 'line: %s' % line > I get this Unicode error. > > I tried a few repairs, but they did not work like > changing: in sgmmlib.py (/usr/lib64/python2.4/sgmmlib.py) > if not 0 < n <= 255 > to > if not 0 < n <= 127 > > But since this did not work, I have changed it back to it's original form. > > --Thanks, > Akhil > > > Chris Rebert-6 wrote: >> >> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >>> >>> Hi! >>> >>> Can anyone please help me getting rid of this error: >>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >>> position >>> 13: ordinal not in range(128) >>> >>> I am not a python programmer (though intend to start learning this >>> wonderful >>> language), I am just using a python script. >>> >>> After doing some search, I found that 0xb7 is a 'middle dot character' >>> that >>> is not interpreted by the python. >>> Even after inserting text = text.replace('\u00b7', '') in the script, >>> the >>> problem still persists. >>> >>> Can anyone please tell me the easiest way to get rid of this? >> >> We'll need the full error traceback. The error message at the end is >> just not enough information. >> As to fixing it, google for "UnicodeEncodeError". You should find >> about a million mailinglist threads on it. >> >> Cheers, >> Chris >> -- >> http://blog.rebertia.com >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24510252.html Sent from the Python - python-list mailing list archive at Nabble.com. From clp2 at rebertia.com Thu Jul 16 01:12:08 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 22:12:08 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24510222.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> Message-ID: <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> > Chris Rebert-6 wrote: >> >> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >>> >>> Hi! >>> >>> Can anyone please help me getting rid of this error: >>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >>> position >>> 13: ordinal not in range(128) >>> >>> I am not a python programmer (though intend to start learning this >>> wonderful >>> language), I am just using a python script. >>> >>> After doing some search, I found that 0xb7 is a 'middle dot character' >>> that >>> is not interpreted by the python. >>> Even after inserting text = text.replace('\u00b7', '') in the script, the >>> problem still persists. >>> >>> Can anyone please tell me the easiest way to get rid of this? >> >> We'll need the full error traceback. The error message at the end is >> just not enough information. >> As to fixing it, google for "UnicodeEncodeError". You should find >> about a million mailinglist threads on it. On Wed, Jul 15, 2009 at 10:05 PM, akhil1988 wrote: > > Well, > All I get is this traceback: > > File "./customWikiExtractor.py", line 492, in ? > main() > File "./customWikiExtractor.py", line 480, in main > print >> sys.stdout, 'line: %s' % line > UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position > 13: ordinal not in range(128) > > I am giving a string to the python code as input, and python processes it > like this: > > line = line.decode('utf-8').strip() > > After this when I do, > print >> sys.stdout, 'line: %s' % line > I get this Unicode error. Try this instead (the ">> sys.stdout" part is redundant): print (u'line: %s' % line).encode('utf8') #if your system doesn't use UTF-8, change as necessary Cheers, Chris -- http://blog.rebertia.com From peter.fodrek at stuba.sk Thu Jul 16 01:16:57 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 07:16:57 +0200 Subject: import module unbelieveable behaviour In-Reply-To: <7c6bdkF26j00sU1@mid.uni-berlin.de> References: <7c6bdkF26j00sU1@mid.uni-berlin.de> Message-ID: <200907160716.57648.peter.fodrek@stuba.sk> On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > Peter Fodrek wrote: ...... > What does > > import nc > print nc.__file__ python Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import nc >>> print nc.__file__ nc/__init__.pyc and as well ls nc attach.py hpgl2dv_read.py iso_codes.pyc nc_read.py emc2.py hpgl3d.py iso.py nc_read.pyc rez.pyc emc2_read.py hpgl3d_read.py iso_read.py num_reader.py rez_read.py hpgl2d.py __init__.py machines.txt rez_codes.py siegkx1.py hpgl2d_read.py __init__.pyc nc.py siegkx1_read.py hpgl2dv.py iso_codes.py nc.pyc rez.py and module is compiled well ls -la nc/rez* -rwxr-xr-x 1 peto users 1391 2009-07-15 13:18 nc/rez_codes.py -rwxrwxrwx 1 peto users 255 2009-07-15 14:28 nc/rez.py -rwxr-xr-x 1 peto users 222 2009-07-15 14:28 nc/rez.pyc -rwxr-xr-x 1 peto users 5920 2009-07-15 13:17 nc/rez_read.py Thank you Peter From peter.fodrek at stuba.sk Thu Jul 16 01:20:53 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 07:20:53 +0200 Subject: import module unbelieveable behaviour In-Reply-To: References: Message-ID: <200907160720.53656.peter.fodrek@stuba.sk> On Thursday 16 July 2009 02:18:52 Carl Banks wrote: > On Jul 15, 6:12 am, Peter Fodrek wrote: > That's a tricky one, indeed. > > Here's my guess: test.py is a symlink to a file in another directory. It is not true guess ls -la test.py -rw-r--r-- 1 peto users 1990 2009-07-15 14:19 test.py But maybe access rights needed to be corrected... Thank you Peter From mensanator at aol.com Thu Jul 16 01:28:31 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 15 Jul 2009 22:28:31 -0700 (PDT) Subject: Why does Python not stop when it's supposed to? Message-ID: <539781e9-1096-499e-851a-8d3e8247e52d@f10g2000vbf.googlegroups.com> So, I'm playing around with Python (running 2.6 and 3.1 versions of IDLE). After I shut everything down and close the windows, I notice the Task Manager's activity light is still glowing. In looking at the Process window I see this: Image... User Name CPU Memory(... Description pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 216 K pythonw.exe pythonw.exe mensanator 00 220 K pythonw.exe pythonw.exe mensanator 00 220 K pythonw.exe pythonw.exe mensanator 00 408 K pythonw.exe pythonw.exe mensanator 00 180 K pythonw.exe pythonw.exe mensanator 50 66,156 K pythonw.exe pythonw.exe mensanator 00 600 K pythonw.exe pythonw.exe mensanator 00 228 K pythonw.exe pythonw.exe mensanator 00 208 K pythonw.exe pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 192 K pythonw.exe pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 180 K pythonw.exe pythonw.exe mensanator 00 8,672 K pythonw.exe I can't seem to reproduce this. Opening IDLE causes two copies to appear. When RUN, a third appears for a few moments and drops back to two. Doing a RESTART SHELL also launches another copy which also goes away momentarily. So, with a 2.6 and a 3.1 IDLE running simultaneously, there could be 6 copies at once. And they eventually all go away when the windows are closed. How is it I end up with 16 copies, one of them still running after all the windows are closed? I had to manually do an [end process] on all of them. If I was doing something wrong, i sure would like to know what it is. From steven at REMOVE.THIS.cybersource.com.au Thu Jul 16 01:37:02 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 16 Jul 2009 05:37:02 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > Didn't know that. > So if I resume: > - not 'foo' => False > - 'foo' or 'foo' => 'foo' > > I may be missing something, but honestly, Guido must have smoked some > heavy stuff to write such logic, has he ? No, it's perfectly reasonable, and not at all the product of mind- altering drugs. Other languages do the same thing. for x in alist or blist or clist: print x will select the first non-empty list and iterate over that. Every object in Python has a truth value. By convention, we say that objects are Something or Nothing. 0 None [] '' are examples of Nothing, or false values. 1 2.5 [x, y, z] 'foo' are examples of Something, or true values. Note the distinction between lower-case "false" and "true" (adjectives), and title-case False and True (nouns). `if x: ... else:` branches according to whether x is Something or Nothing, not whether it is True or False. The operators `and` and `or` also return Something or Nothing, short- circuiting as appropriate. -- Steven From phostu at gmail.com Thu Jul 16 01:38:10 2009 From: phostu at gmail.com (Vincent) Date: Wed, 15 Jul 2009 22:38:10 -0700 (PDT) Subject: problem about cx_Oracle References: <957ab843-92ac-4072-ace4-441a2da0d130@m3g2000pri.googlegroups.com> Message-ID: <717d5451-ef6d-45df-9e77-2135c75f7a12@u38g2000pro.googlegroups.com> On Jul 16, 12:18?pm, Vincent wrote: > hi, all: > > i am using cx_oracle now. > > i write code as below: > > def __getfields_by_tbname(self,tbname): > ? ? ? ? cursor = self.ora_db.cursor() > ? ? ? ? print tbname > ? ? ? ? sql = 'select * from %s where rownum <=2' % tbname > ? ? ? ? print sql > ? ? ? ? cursor = cursor.execute(sql) > ? ? ? ? return self.getfields(cursor) > > and i got a error, it's message is : > JRYZCFZB_X_ZQY > select * from JRYZCFZB_X_ZQY where rownum <=2 > Traceback (most recent call last): > ? File "", line 1, in > ? File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools > \data_convert.py", line 107, in convert > ? ? self.convert_table(tbname) > ? File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools > \data_convert.py", line 94, in convert_table > ? ? field_list = self.__getfields_by_tbname(tbname) > ? File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools > \data_convert.py", line 38, in __getfields_by_tbname > ? ? cursor = cursor.execute(sql) > TypeError: expecting None or a string > > i'm sure the cursor instance is not None. > could anybody give me sussgestion? i will apreciate it. > > vincent i have the answer now. the variant sql is unicode. i neet to convert it to string. From pavlovevidence at gmail.com Thu Jul 16 01:38:23 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 15 Jul 2009 22:38:23 -0700 (PDT) Subject: import module unbelieveable behaviour References: <7c6bdkF26j00sU1@mid.uni-berlin.de> Message-ID: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> On Jul 15, 10:16?pm, Peter Fodrek wrote: > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > > > Peter Fodrek wrote: > ...... > > What does > > > import nc > > print nc.__file__ > > python > Python 2.6 (r26:66714, Feb ?3 2009, 20:49:49) > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > Type "help", "copyright", "credits" or "license" for more information.>>> import nc > >>> print nc.__file__ > > nc/__init__.pyc What does it do if you run this from a script? Also, what is sys.path[0] in both interactive and script? Inside test.py, right before the line that imports nc.rez, add the code "import sys; print sys.modules.get('nc')"; what does that output? Is there an old nc.py or nc.pyc file anywhere? Carl Banks Carl Banks From afriere at yahoo.co.uk Thu Jul 16 01:44:40 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Wed, 15 Jul 2009 22:44:40 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> <0fc81395-7787-48a9-93de-cf7a274b691e@u38g2000pro.googlegroups.com> <35b0b936-6bc1-4acf-aa27-6cc0db84292b@y10g2000prf.googlegroups.com> Message-ID: <6c572443-cf8e-403c-aa66-086007dca540@d15g2000prc.googlegroups.com> On Jul 16, 2:14?pm, alex23 wrote: ... > AND > you're citing back what I myself wrote in the link to which I also > referred the OP - whom (sic) has subsequently posted his success with this > technique - so I'm not really sure what the point is here other than > "if you have a different use case, you'll need a different > solution"... > Please don't take offence where none was intended. I know that I was citing you and that I was highlighting the caveat you raised, explicitly so. No criticism of your post was intended nor implied. Moreover the fact that it worked for OP on a single occasion does not speak for its robustness. > > Depending on the use case, it is of course easy to tell whether the > > module was executed on the command line, or imported (from an > > interactive shell or another script) using the __name__ trick. ?(eg. > > is_imported = __name__ == '__main__') > > That should be: > > is_imported = __name__ != '__main__' > Doh! ... Yup I actually used that when I tried it out, my bad. > And such a test is all well and good if the main code body is the one > that needs to know about the execution mode, but if you need to know > under which conditions the program is being run within a module > imported by the main body, well, that check is _always_ going to be > true... Which is what the paragraph you just quoted says. Hence the attribute is called 'is_imported' rather that 'running_non_interactively'. From akhilanger at gmail.com Thu Jul 16 01:52:24 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 22:52:24 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> Message-ID: <24510519.post@talk.nabble.com> Chris, Using print (u'line: %s' % line).encode('utf-8') the 'line' gets printed, but actually this print statement I was using just for testing, actually my code operates on 'line', on which I use line = line.decode('utf-8') as 'line' is read as bytes from a stream. And if I use line = line.encode('utf-8'), I start getting other error like UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4561: ordinal not in range(128) at line = line.replace('<<', u'?').replace('>>', u'?') --Akhil Chris Rebert-6 wrote: > >> Chris Rebert-6 wrote: >>> >>> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >>>> >>>> Hi! >>>> >>>> Can anyone please help me getting rid of this error: >>>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >>>> position >>>> 13: ordinal not in range(128) >>>> >>>> I am not a python programmer (though intend to start learning this >>>> wonderful >>>> language), I am just using a python script. >>>> >>>> After doing some search, I found that 0xb7 is a 'middle dot character' >>>> that >>>> is not interpreted by the python. >>>> Even after inserting text = text.replace('\u00b7', '') in the script, >>>> the >>>> problem still persists. >>>> >>>> Can anyone please tell me the easiest way to get rid of this? >>> >>> We'll need the full error traceback. The error message at the end is >>> just not enough information. >>> As to fixing it, google for "UnicodeEncodeError". You should find >>> about a million mailinglist threads on it. > On Wed, Jul 15, 2009 at 10:05 PM, akhil1988 wrote: >> >> Well, >> All I get is this traceback: >> >> File "./customWikiExtractor.py", line 492, in ? >> main() >> File "./customWikiExtractor.py", line 480, in main >> print >> sys.stdout, 'line: %s' % line >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >> position >> 13: ordinal not in range(128) >> >> I am giving a string to the python code as input, and python processes it >> like this: >> >> line = line.decode('utf-8').strip() >> >> After this when I do, >> print >> sys.stdout, 'line: %s' % line >> I get this Unicode error. > > Try this instead (the ">> sys.stdout" part is redundant): > print (u'line: %s' % line).encode('utf8') > #if your system doesn't use UTF-8, change as necessary > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24510519.html Sent from the Python - python-list mailing list archive at Nabble.com. From milesck at umich.edu Thu Jul 16 01:59:54 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Thu, 16 Jul 2009 01:59:54 -0400 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: <1EAE7F84-0FA1-4688-8F2B-B5E6869E69B8@umich.edu> On Jul 15, 2009, at 4:26 PM, David Bolen wrote: > Miles Kaufmann writes: > >> On Jul 14, 2009, at 5:06 PM, David Bolen wrote: >>> Are you sure? It seems to restrict them in the same block, but >>> not in >>> the entire file. At least I was able to use both space and tab >>> indented blocks in the same file with Python 3.0 and 3.1. >> >> It seems to me that, within an indented block, Python 3.1 requires >> that you are consistent in your use of indentation characters *for >> that indentation level*. For example, the following code seems to be >> allowed: > > Um, right - in other words, what I said :-) I wasn't trying to correct you, just being more explicit. :) After reading your post, I still wasn't sure if the restriction on mixing spaces and tabs applied to nested blocks--I was surprised that the code sample I included was allowed. -Miles From list at qtrac.plus.com Thu Jul 16 02:21:48 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Wed, 15 Jul 2009 23:21:48 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? Message-ID: Hi, I'm just wondering why <, <=, >=, and > are not supported by collections.OrderedDict: >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) >>> d2 = d1.copy() >>> d2["z"] = 4 >>> d1 == d2 False >>> d1 < d2 Traceback (most recent call last): File "", line 1, in d1 < d2 TypeError: unorderable types: OrderedDict() < OrderedDict() It just seems to me that since the items in ordered dictionaries are ordered, it would make sense to do an item by item comparison from first to last item in exactly the same way that Python compares lists or tuples? From peter.fodrek at stuba.sk Thu Jul 16 02:38:57 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 08:38:57 +0200 Subject: import module unbelieveable behaviour In-Reply-To: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> References: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> Message-ID: <200907160838.57658.peter.fodrek@stuba.sk> On Thursday 16 July 2009 07:38:23 Carl Banks wrote: > On Jul 15, 10:16 pm, Peter Fodrek wrote: > > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > > > Peter Fodrek wrote: > > > > ...... > > > > > What does > > > > > > import nc > > > print nc.__file__ > > > > python > > Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) > > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > > Type "help", "copyright", "credits" or "license" for more information.>>> > > import nc > > > > >>> print nc.__file__ > > > > nc/__init__.pyc > > What does it do if you run this from a script? Ouch, I am and python rookie, that was needed to try to edit undocumented script from German author this answers everything /home/opt/HeeksCAD8/HeeksCNC> python ./test.py /usr/local/lib/heekscnc/nc/__init__.py and it is because begining of the script # -*- coding: utf-8 -*- import sys sys.path.insert(0,'/usr/local/lib/heekscnc/') import kurve import kurve_funcs from nc.nc import * import nc print nc.__file__ import nc.rez and ls -a /usr/local/lib/heekscnc/nc . emc2_read.py hpgl2dv_read.py iso_codes.py nc_read.py .. hpgl2d.py hpgl3d.py iso.py num_reader.py attach.py hpgl2d_read.py hpgl3d_read.py iso_read.py siegkx1.py emc2.py hpgl2dv.py __init__.py nc.py siegkx1_read.py Thank you for your help, once more. Peter From peter.fodrek at stuba.sk Thu Jul 16 02:48:55 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 08:48:55 +0200 Subject: import module unbelieveable behaviour In-Reply-To: <200907160838.57658.peter.fodrek@stuba.sk> References: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> <200907160838.57658.peter.fodrek@stuba.sk> Message-ID: <200907160848.55069.peter.fodrek@stuba.sk> On Thursday 16 July 2009 08:38:57 Peter Fodrek wrote: > On Thursday 16 July 2009 07:38:23 Carl Banks wrote: > > On Jul 15, 10:16 pm, Peter Fodrek wrote: > > > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > sys.path.insert(0,'/usr/local/lib/heekscnc/') I was mentioned that this adds system path to multipath list(As $PATH) and it replaces path instead. That was problem.. Peter From jackdied at gmail.com Thu Jul 16 03:12:20 2009 From: jackdied at gmail.com (Jack Diederich) Date: Thu, 16 Jul 2009 03:12:20 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: Message-ID: On Thu, Jul 16, 2009 at 2:21 AM, Mark Summerfield wrote: > Hi, > > I'm just wondering why <, <=, >=, and > are not supported by > collections.OrderedDict: > > ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) > ? ?>>> d2 = d1.copy() > ? ?>>> d2["z"] = 4 > ? ?>>> d1 == d2 > ? ?False > ? ?>>> d1 < d2 > ? ?Traceback (most recent call last): > ? ?File "", line 1, in > ? ? ? ?d1 < d2 > ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > It just seems to me that since the items in ordered dictionaries are > ordered, it would make sense to do an item by item comparison from > first to last item in exactly the same way that Python compares lists > or tuples? >>> import this In the face of ambiguity, refuse the temptation to guess. It isn't an OrderedDict thing, it is a comparison thing. Two regular dicts also raise an error if you try to LT them. What does it mean for a dict to be greater than or less than its peer? Nothing, so we refuse to guess. -Jack From list at qtrac.plus.com Thu Jul 16 03:22:20 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 00:22:20 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> On 16 July, 08:12, Jack Diederich wrote: > On Thu, Jul 16, 2009 at 2:21 AM, Mark Summerfield wrote: > > Hi, > > > I'm just wondering why <, <=, >=, and > are not supported by > > collections.OrderedDict: > > > ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) > > ? ?>>> d2 = d1.copy() > > ? ?>>> d2["z"] = 4 > > ? ?>>> d1 == d2 > > ? ?False > > ? ?>>> d1 < d2 > > ? ?Traceback (most recent call last): > > ? ?File "", line 1, in > > ? ? ? ?d1 < d2 > > ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > > It just seems to me that since the items in ordered dictionaries are > > ordered, it would make sense to do an item by item comparison from > > first to last item in exactly the same way that Python compares lists > > or tuples? > >>> import this > > In the face of ambiguity, refuse the temptation to guess. > > It isn't an OrderedDict thing, it is a comparison thing. ?Two regular > dicts also raise an error if you try to LT them. ?What does it mean > for a dict to be greater than or less than its peer? ?Nothing, so we > refuse to guess. > > -Jack You are right that it doesn't make sense to compare two dicts. But OrderedDicts can be viewed logically as lists of (key,value) tuples so they are much more like lists or tuples when it comes to comparisons. For example: >>> l = [("a", 1), ("z", 2), ("k", 3)] >>> l1 = l[:] >>> l1[1] = ("z", 5) >>> l < l1 True But if you do: >>> d = collections.OrderedDict(l) >>> d1 = collections.OrderedDict(l1) You can't use <, <=, =>, or >, even though ordered dictionaries preserve the order and their items are just as comparable as those in a list or tuple of tuples. From nagle at animats.com Thu Jul 16 03:26:18 2009 From: nagle at animats.com (John Nagle) Date: Thu, 16 Jul 2009 00:26:18 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> Message-ID: <4a5ed587$0$1627$742ec2ed@news.sonic.net> akhil1988 wrote: > Sorry, it is sgmllib.py and not sgmmlib.py Oh, that bug again. See http://bugs.python.org/issue1651995 It's a bug in SGMLParser. When Python 2.5 restricted ASCII to 0..127, SGMLParser needed to be modified, but wasn't. I reported that bug in February 2007. It was fixed in Python 2.6 and 3.0 on March 31, 2009. John Nagle From clp2 at rebertia.com Thu Jul 16 03:30:26 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 00:30:26 -0700 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: Message-ID: <50697b2c0907160030x5abbf7a4i99d397236297c850@mail.gmail.com> On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield wrote: > Hi, > > I'm just wondering why <, <=, >=, and > are not supported by > collections.OrderedDict: > > ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) > ? ?>>> d2 = d1.copy() > ? ?>>> d2["z"] = 4 > ? ?>>> d1 == d2 > ? ?False > ? ?>>> d1 < d2 > ? ?Traceback (most recent call last): > ? ?File "", line 1, in > ? ? ? ?d1 < d2 > ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > It just seems to me that since the items in ordered dictionaries are > ordered, it would make sense to do an item by item comparison from > first to last item in exactly the same way that Python compares lists > or tuples? Use case? I'm curious. Cheers, Chris -- http://blog.rebertia.com From mail at microcorp.co.za Thu Jul 16 03:43:53 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 16 Jul 2009 09:43:53 +0200 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com><50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <001201ca05e9$2b0a61e0$0d00a8c0@Hendrik> "Hrvoje Niksic" wrote: > Note that in Python A or B is in fact not equivalent to not(not A and > not B). De Morgan would turn in his grave. - Hendrik From steven at REMOVE.THIS.cybersource.com.au Thu Jul 16 03:51:29 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 16 Jul 2009 07:51:29 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote: > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield > wrote: >> Hi, >> >> I'm just wondering why <, <=, >=, and > are not supported by >> collections.OrderedDict: >> >> ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 = >> ? ?>>> d1.copy() >> ? ?>>> d2["z"] = 4 >> ? ?>>> d1 == d2 >> ? ?False >> ? ?>>> d1 < d2 >> ? ?Traceback (most recent call last): >> ? ?File "", line 1, in >> ? ? ? ?d1 < d2 >> ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() >> >> It just seems to me that since the items in ordered dictionaries are >> ordered, it would make sense to do an item by item comparison from >> first to last item in exactly the same way that Python compares lists >> or tuples? > > Use case? I'm curious. Surely it would be the same use case as for comparing two lists? There doesn't need to be a special "OrderedDict use case" beyond "an OrderedDict is just like a list of (key,value) tuples, but searches are faster". Or maybe not. If OrderedDicts are sequences as well as mappings, then we should be able to sort them. And that seems a bit much even for me. -- Steven From eckhardt at satorlaser.com Thu Jul 16 03:56:13 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Thu, 16 Jul 2009 09:56:13 +0200 Subject: allowing output of code that is unittested? References: Message-ID: per wrote: > i am using the standard unittest module to unit test my code. my code > contains several print statements which i noticed are repressed when i > call my unit tests using: > > if __name__ == '__main__': > suite = unittest.TestLoader().loadTestsFromTestCase(TestMyCode) > unittest.TextTestRunner(verbosity=2).run(suite) I have here if __name__ == '__main__': unittest.main() preceded by classes which derive from unittest.TestCase. Running this allows me to write to stdout using print as usual, even though it's ugly because it clutters the output of the testcases. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From steven at REMOVE.THIS.cybersource.com.au Thu Jul 16 04:19:21 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 16 Jul 2009 08:19:21 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On Thu, 16 Jul 2009 09:43:53 +0200, Hendrik van Rooyen wrote: > "Hrvoje Niksic" wrote: > > >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > De Morgan would turn in his grave. No he wouldn't. Python isn't Boolean algebra, and there is no requirement to limit Python's operators to the semantics of Boolean algebra. -- Steven From info at egenix.com Thu Jul 16 04:28:10 2009 From: info at egenix.com (eGenix Team: M.-A. Lemburg) Date: Thu, 16 Jul 2009 10:28:10 +0200 Subject: EuroPython 2009: Making 50 Mio. EUR per year using Python Message-ID: <4A5EE49A.8000207@egenix.com> Now available as video... http://www.egenix.com/company/news/EuroPython-2009-Lightning-Talk.html Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 16 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From vmalloc at gmail.com Thu Jul 16 04:43:32 2009 From: vmalloc at gmail.com (Rotem) Date: Thu, 16 Jul 2009 01:43:32 -0700 (PDT) Subject: Memory leak involving traceback objects Message-ID: Hi, I'm debugging a nasty memory leak in a framework written in Python (v2.6.2). After much digging around I found that the entire object group that is leaking is held by a frame object which is subsequently held by a traceback object. Traversing the get_referrers() of each traceback frame leads eventually to a root traceback frame which has no referrers (gc.get_referrers returns an empty list). However, this traceback object seems not to be picked by the garbage collector, and is still there even after many iterations and calls to gc.collect(). The code location to which the traceback frame points doesn't do anything special - it just catches an exception, without saving the exception itself and/or traceback anywhere. Before I dive into the Python code itself - does anyone have any idea what can cause this? Thanks, Rotem From piet at cs.uu.nl Thu Jul 16 04:48:57 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 10:48:57 +0200 Subject: How to Force exiting from program/script References: Message-ID: >>>>> Alex (A) a ?crit: >A> hi at all, >A> I have made a script with a while loop and I want that after 30 >A> seconds the program stop and exit . But the code like this doesn't >A> run: >A> In the Console I can see work so that function is correctly called... >A> #Function to exit >A> def exit(): >A> print "work" >A> raise SystemExit() >A> t = threading.Timer(30.0, exit) >A> t.start() >A> # Loop >A> while True: >A> ...many lines.... This code gives you a bit more control as it doesn't just force a system exit, but allows you to continue some other work after aborting the loop: import signal, os from threading import Timer signalcode = signal.SIGALRM class AlarmError(Exception): pass def handler(signum, frame): raise AlarmError, "command lasts too long" signal.signal(signalcode, handler) def interrupt(): os.kill(os.getpid(), signalcode) def execute(function, timeout): Timer(timeout, interrupt).start() try: function() except AlarmError, e: print e print 'Execution aborted' def long_function(): while True: pass print "The everlasting command" execute(long_function, 10) print "The End" -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From l.mastrodomenico at gmail.com Thu Jul 16 05:02:55 2009 From: l.mastrodomenico at gmail.com (Lino Mastrodomenico) Date: Thu, 16 Jul 2009 11:02:55 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <001201ca05e9$2b0a61e0$0d00a8c0@Hendrik> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <001201ca05e9$2b0a61e0$0d00a8c0@Hendrik> Message-ID: 2009/7/16 Hendrik van Rooyen : > "Hrvoje Niksic" wrote: > > >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > De Morgan would turn in his grave. If this can make him happier, in Python (not (not a and not b)) *is* equivalent to bool(a or b). (Modulo crazy things like redefining "bool" or having a __bool__ with side effects.) In the first expression you implicitly request a bool because you use "not", in the second one you do this with an explicit "bool". -- Lino Mastrodomenico From jeanmichel at sequans.com Thu Jul 16 05:06:54 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 16 Jul 2009 11:06:54 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: <4A5EEDAE.3040605@sequans.com> Nobody wrote: > On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > > >> So if I resume: >> - not 'foo' => False >> - 'foo' or 'foo' => 'foo' >> >> I may be missing something, but honestly, Guido must have smoked some >> heavy stuff to write such logic, has he ? >> > > Several languages (e.g. Lisp, Bourne shell) behave the same way, i.e. "or" > returns the first element which is considered true while "and" returns the > last element provided that all preceding elements are considered true. > > [snip] > Ok then, why "or" does not return True, if the first element is considered True ? Why returning the element itself. Any reason for that ? Because it's confusing, maybe people used to that logic find it obvious, but I really don't. JM From piet at cs.uu.nl Thu Jul 16 05:09:51 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 11:09:51 +0200 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> Message-ID: >>>>> akhil1988 (a) wrote: >a> Chris, >a> Using >a> print (u'line: %s' % line).encode('utf-8') >a> the 'line' gets printed, but actually this print statement I was using just >a> for testing, actually my code operates on 'line', on which I use line = >a> line.decode('utf-8') as 'line' is read as bytes from a stream. >a> And if I use line = line.encode('utf-8'), >a> I start getting other error like >a> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4561: >a> ordinal not in range(128) >a> at line = line.replace('<<', u'?').replace('>>', u'?') You do a Unicode replace here, so line should be a unicode string. Therefore you have to do this before the line.encode('utf-8'), but after the decode('utf-8'). It might be better to use different variables for Unicode strings and byte code strings to prevent confusion, like: 'line' is read as bytes from a stream uline = line.decode('utf-8') uline = uline.replace('<<', u'?').replace('>>', u'?') line = uline.encode('utf-8') -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jarausch at igpm.rwth-aachen.de Thu Jul 16 05:12:25 2009 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: Thu, 16 Jul 2009 11:12:25 +0200 Subject: How to search this newsgroup by a python script. Message-ID: <7c897lF26dea4U1@mid.dfncis.de> Hi, I haven't found anything with Google's group search, so let me ask it (again?). How can I search this newsgroup from within a Python script. (Perhaps by searching Google Groups or Gmane by some Python code.) Many thanks for a hint, Helmut. -- Helmut Jarausch Lehrstuhl fuer Numerische Mathematik RWTH - Aachen University D 52056 Aachen, Germany From piet at cs.uu.nl Thu Jul 16 05:21:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 11:21:54 +0200 Subject: Why aren't OrderedDicts comparable with < etc? References: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> Message-ID: >>>>> Mark (M) wrote: >M> You are right that it doesn't make sense to compare two dicts. >M> But OrderedDicts can be viewed logically as lists of (key,value) >M> tuples so they are much more like lists or tuples when it comes to >M> comparisons. >M> For example: >>>>> l = [("a", 1), ("z", 2), ("k", 3)] >>>>> l1 = l[:] >>>>> l1[1] = ("z", 5) >>>>> l < l1 >M> True >M> But if you do: >>>>> d = collections.OrderedDict(l) >>>>> d1 = collections.OrderedDict(l1) >M> You can't use <, <=, =>, or >, even though ordered dictionaries >M> preserve the order and their items are just as comparable as those in >M> a list or tuple of tuples. But why should the order be as if the OrderedDict was a list of tuples. A dict can be considered as a mapping and then you might want to treat either the key or the value as contravariant (the key I guess). So there is ambiguity. Why would the view as a list of tuples for the ordering be the `natural' view? Maybe you may expect some kind of monotonicity such that d1 URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rob.hooft at gmail.com Thu Jul 16 05:25:27 2009 From: rob.hooft at gmail.com (rwwh) Date: Thu, 16 Jul 2009 02:25:27 -0700 (PDT) Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <006eb461$0$9711$c3e8da3@news.astraweb.com> <006f13c9$0$9711$c3e8da3@news.astraweb.com> Message-ID: <1712a4da-fb9f-416e-9190-4da55fcc40ea@b14g2000yqd.googlegroups.com> On Jul 7, 2:00?pm, Steven D'Aprano wrote: > On Mon, 06 Jul 2009 22:18:20 -0700, Chris Rebert wrote: > >> Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. > [...] > > Well, yes, but when would you apply it twice in a row? > > My point was that unary + isn't a no-op, and therefore neither is ++. For > Decimal, I can't think why you'd want to apply ++x, but for other > objects, who knows? > > Here's a toy example: > > >>> class Spam(str): > > ... ? ? def __pos__(self): > ... ? ? ? ? return self.__class__("spam " + self) > ...>>> s = Spam("") > >>> ++++s > > 'spam spam spam spam ' Here's another toy example: class Toy(int): def __init__(self, value): self._incrd = False int.__init__(self, value) def incrHalf(self): self._incrd = True def __pos__(self): if self._incrd: return self.__class__(self+1) else: p = self.__class__(self) p.incrHalf() return p def __add__(self, other): return self.__class__(int(self)+other) nows122[126]~% python -i toy.py >>> i=Toy(5) >>> +i 5 >>> ++i 6 >>> +++i 6 >>> +i++i 10 >>> +(+i++i) 10 >>> (++i)++i 11 From clp2 at rebertia.com Thu Jul 16 05:33:24 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 02:33:24 -0700 Subject: How to search this newsgroup by a python script. In-Reply-To: <7c897lF26dea4U1@mid.dfncis.de> References: <7c897lF26dea4U1@mid.dfncis.de> Message-ID: <50697b2c0907160233j70ba360fv69f6f48e509b55aa@mail.gmail.com> On Thu, Jul 16, 2009 at 2:12 AM, Helmut Jarausch wrote: > Hi, > > I haven't found anything with Google's group search, so let me > ask it (again?). > > How can I search this newsgroup from within a Python script. > (Perhaps by searching Google Groups or Gmane by some Python code.) 1. Generate URL of the form: http://search.gmane.org/?query=foo&group=gmane.comp.python.general where "foo" is the search terms, with proper URL escaping applied. 2. Fetch URL using urllib - http://docs.python.org/library/urllib.html 3. Parse resulting HTML page (e.g. using BeautifulSoup) 4. Extract desired information from search results using the parse tree. 5. ??? 6. Profit! Cheers, Chris -- http://blog.rebertia.com From mail at timgolden.me.uk Thu Jul 16 05:38:05 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 16 Jul 2009 10:38:05 +0100 Subject: How to search this newsgroup by a python script. In-Reply-To: <50697b2c0907160233j70ba360fv69f6f48e509b55aa@mail.gmail.com> References: <7c897lF26dea4U1@mid.dfncis.de> <50697b2c0907160233j70ba360fv69f6f48e509b55aa@mail.gmail.com> Message-ID: <4A5EF4FD.7080708@timgolden.me.uk> Chris Rebert wrote: > On Thu, Jul 16, 2009 at 2:12 AM, Helmut > Jarausch wrote: >> Hi, >> >> I haven't found anything with Google's group search, so let me >> ask it (again?). >> >> How can I search this newsgroup from within a Python script. >> (Perhaps by searching Google Groups or Gmane by some Python code.) > > 1. Generate URL of the form: > http://search.gmane.org/?query=foo&group=gmane.comp.python.general > where "foo" is the search terms, with proper URL escaping applied. > 2. Fetch URL using urllib - http://docs.python.org/library/urllib.html > 3. Parse resulting HTML page (e.g. using BeautifulSoup) > 4. Extract desired information from search results using the parse tree. > 5. ??? > 6. Profit! Alternatively, you could do something with the mailing list archive: http://mail.python.org/pipermail/python-list/ The .gz files are gzipped mbox format so can be dropped into, eg, Thunderbird for offline browsing, or searched with the email package from the stdlib. TJG From user at example.net Thu Jul 16 06:34:20 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 12:34:20 +0200 Subject: using timers to force an execution time Message-ID: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> hello. based upon previuos suggestions, i tried to write a program in which i would like to have a certain code fragment to execute only for a specified amount of time (say 3 seconds), then bail out. but i get the following: $ cat tmr004.py #!/usr/bin/python -u import threading , time e = threading.Event() t = threading.Timer(3.0, e.set) t.start() print time.asctime(time.localtime(time.time())) while not e.isSet(): for repeat in range(10): print time.time() time.sleep(0.66) print time.asctime(time.localtime(time.time())) $ ./tmr004.py Thu Jul 16 12:31:27 2009 1247740287.44 1247740288.1 1247740288.76 1247740289.42 1247740290.08 1247740290.74 1247740291.4 1247740292.06 1247740292.72 1247740293.38 Thu Jul 16 12:31:34 2009 $ see? the while body ran for about 7 seconds... i bet it has to do with the fact that the timer does not control inner loops... any suggestion? $ python -V Python 2.3.4 $ uname -a Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux $ bye From contact at xavierho.com Thu Jul 16 06:49:07 2009 From: contact at xavierho.com (Xavier Ho) Date: Thu, 16 Jul 2009 18:49:07 +0800 Subject: using timers to force an execution time In-Reply-To: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <2d56febf0907160349k7c4eaa50t42216ce85bcb1689@mail.gmail.com> On Thu, Jul 16, 2009 at 6:34 PM, superpollo wrote: > hello. > > based upon previuos suggestions, i tried to write a program in which i > would like to have a certain code fragment to execute only for a specified > amount of time (say 3 seconds), then bail out. > > > see? the while body ran for about 7 seconds... i bet it has to do with the > fact that the timer does not control inner loops... any suggestion? > > I'm guessing it has to do with a sleep of 0.66 seconds x 10 times = sleep for 6.6 seconds, and then it checks for e.isSet(). Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From list at qtrac.plus.com Thu Jul 16 06:50:25 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 03:50:25 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> Message-ID: On 16 July, 10:21, Piet van Oostrum wrote: > >>>>> Mark (M) wrote: > >M> You are right that it doesn't make sense to compare two dicts. > >M> But OrderedDicts can be viewed logically as lists of (key,value) > >M> tuples so they are much more like lists or tuples when it comes to > >M> comparisons. > >M> For example: > >>>>> l = [("a", 1), ("z", 2), ("k", 3)] > >>>>> l1 = l[:] > >>>>> l1[1] = ("z", 5) > >>>>> l < l1 > >M> True > >M> But if you do: > >>>>> d = collections.OrderedDict(l) > >>>>> d1 = collections.OrderedDict(l1) > >M> You can't use <, <=, =>, or >, even though ordered dictionaries > >M> preserve the order and their items are just as comparable as those in > >M> a list or tuple of tuples. > > But why should the order be as if the OrderedDict was a list of tuples. > A dict can be considered as a mapping and then you might want to treat > either the key or the value as contravariant (the key I guess). So there > is ambiguity. Why would the view as a list of tuples for the ordering be > the `natural' view? > > Maybe you may expect some kind of monotonicity such that d1 d1[x] 2:5}. So maybe there is only a partial ordering? OK, that seems to me to be a convincing argument against supporting ordering. From user at example.net Thu Jul 16 06:56:50 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 12:56:50 +0200 Subject: turtle dump Message-ID: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> hi there. is there a way to dump the content of a turtle window to a file or a file object? bye From list at qtrac.plus.com Thu Jul 16 06:58:08 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 03:58:08 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: <401967fb-c12e-4f63-8b05-6dffa0688028@a26g2000yqn.googlegroups.com> On 16 July, 08:51, Steven D'Aprano wrote: > On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote: > > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield > > wrote: > >> Hi, > > >> I'm just wondering why <, <=, >=, and > are not supported by > >> collections.OrderedDict: > > >> ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 = > >> ? ?>>> d1.copy() > >> ? ?>>> d2["z"] = 4 > >> ? ?>>> d1 == d2 > >> ? ?False > >> ? ?>>> d1 < d2 > >> ? ?Traceback (most recent call last): > >> ? ?File "", line 1, in > >> ? ? ? ?d1 < d2 > >> ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > >> It just seems to me that since the items in ordered dictionaries are > >> ordered, it would make sense to do an item by item comparison from > >> first to last item in exactly the same way that Python compares lists > >> or tuples? > > > Use case? I'm curious. > > Surely it would be the same use case as for comparing two lists? There > doesn't need to be a special "OrderedDict use case" beyond "an > OrderedDict is just like a list of (key,value) tuples, but searches are > faster". > > Or maybe not. If OrderedDicts are sequences as well as mappings, then we > should be able to sort them. And that seems a bit much even for me. > > -- > Steven One thing that I've just noticed is that you can use <, <=, >=, and > with sets: >>> s1 = {1,2,3} >>> s2 = {1,2,4} >>> s1 == s2 False >>> s1 < s2 False >>> s1 <= s2 False >>> s2 < s1 False >>> s2 <= s1 False >>> s1 != s2 True It seems a bit inconsistent that with sets you always get False when using an ordering operator but with an ordered dict you get an exception? From list at qtrac.plus.com Thu Jul 16 06:59:47 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 03:59:47 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: <401967fb-c12e-4f63-8b05-6dffa0688028@a26g2000yqn.googlegroups.com> Message-ID: <028e48c2-4a3a-4920-9a67-d9bb95714b4f@c36g2000yqn.googlegroups.com> On 16 July, 11:58, Mark wrote: > On 16 July, 08:51, Steven D'Aprano > > > > wrote: > > On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote: > > > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield > > > wrote: > > >> Hi, > > > >> I'm just wondering why <, <=, >=, and > are not supported by > > >> collections.OrderedDict: > > > >> ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 = > > >> ? ?>>> d1.copy() > > >> ? ?>>> d2["z"] = 4 > > >> ? ?>>> d1 == d2 > > >> ? ?False > > >> ? ?>>> d1 < d2 > > >> ? ?Traceback (most recent call last): > > >> ? ?File "", line 1, in > > >> ? ? ? ?d1 < d2 > > >> ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > > >> It just seems to me that since the items in ordered dictionaries are > > >> ordered, it would make sense to do an item by item comparison from > > >> first to last item in exactly the same way that Python compares lists > > >> or tuples? > > > > Use case? I'm curious. > > > Surely it would be the same use case as for comparing two lists? There > > doesn't need to be a special "OrderedDict use case" beyond "an > > OrderedDict is just like a list of (key,value) tuples, but searches are > > faster". > > > Or maybe not. If OrderedDicts are sequences as well as mappings, then we > > should be able to sort them. And that seems a bit much even for me. > > > -- > > Steven > > One thing that I've just noticed is that you can use <, <=, >=, and > > with sets: > > >>> s1 = {1,2,3} > >>> s2 = {1,2,4} > >>> s1 == s2 > False > >>> s1 < s2 > False > >>> s1 <= s2 > False > >>> s2 < s1 > False > >>> s2 <= s1 > False > >>> s1 != s2 > > True > > It seems a bit inconsistent that with sets you always get False when > using an ordering operator but with an ordered dict you get an > exception? Ooops---disregard the above---I forgot that these do subset and superset comparisions! From akhilanger at gmail.com Thu Jul 16 07:00:00 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 04:00:00 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <4a5ed587$0$1627$742ec2ed@news.sonic.net> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> Message-ID: <24514309.post@talk.nabble.com> I have switched to python 3.1 , but now I am getting some syntax errors in the code: File "./customWikiExtractor.py", line 81 __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', '¢' :u'\u00A2', ^ SyntaxError: invalid syntax line 81 is: __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', '¢' :u'\u00A2', '£' :u'\u00A3', '¤':u'\u00A4', '¥' :u'\u00A5', '¦' :u'\u00A6', '§' :u'\u00A7', '¨' :u'\u00A8', '©' :u'\u00A9', 'ª' :u'\u00AA', '«' :u'\u00AB', '¬' :u'\u00AC', '­' :u'\u00AD', '®' :u'\u00AE', '¯' :u'\u00AF', '°' :u'\u00B0', '±' :u'\u00B1', '²' :u'\u00B2', '³' :u'\u00B3', '´' :u'\u00B4', 'µ' :u'\u00B5', '¶' :u'\u00B6', '·' :u'\u00B7', '¸' :u'\u00B8', '¹' :u'\u00B9', 'º' :u'\u00BA',} --Akhil John Nagle-2 wrote: > > akhil1988 wrote: >> Sorry, it is sgmllib.py and not sgmmlib.py > > Oh, that bug again. See > > http://bugs.python.org/issue1651995 > > It's a bug in SGMLParser. When Python 2.5 restricted ASCII to 0..127, > SGMLParser needed to be modified, but wasn't. > > I reported that bug in February 2007. It was fixed in > Python 2.6 and 3.0 on March 31, 2009. > > John Nagle > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24514309.html Sent from the Python - python-list mailing list archive at Nabble.com. From user at example.net Thu Jul 16 07:01:34 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:01:34 +0200 Subject: using timers to force an execution time In-Reply-To: <7c8fgbF26vgl2U1@mid.uni-berlin.de> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> Message-ID: <4a5f088f$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > What you can't achieve in python without major black magic hackery that is > very dangerous is to terminate a thread while it is executing. Termination > has to be co-operative. i see. thanks a lot. bye From deets at nospam.web.de Thu Jul 16 07:03:55 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:03:55 +0200 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <7c8fgbF26vgl2U1@mid.uni-berlin.de> superpollo wrote: > hello. > > based upon previuos suggestions, i tried to write a program in which i > would like to have a certain code fragment to execute only for a > specified amount of time (say 3 seconds), then bail out. > > but i get the following: > > $ cat tmr004.py > #!/usr/bin/python -u > > import threading , time > > e = threading.Event() > t = threading.Timer(3.0, e.set) > t.start() > print time.asctime(time.localtime(time.time())) > while not e.isSet(): > for repeat in range(10): > print time.time() > time.sleep(0.66) > print time.asctime(time.localtime(time.time())) > $ ./tmr004.py > Thu Jul 16 12:31:27 2009 > 1247740287.44 > 1247740288.1 > 1247740288.76 > 1247740289.42 > 1247740290.08 > 1247740290.74 > 1247740291.4 > 1247740292.06 > 1247740292.72 > 1247740293.38 > Thu Jul 16 12:31:34 2009 > $ > > see? the while body ran for about 7 seconds... i bet it has to do with > the fact that the timer does not control inner loops... any suggestion? Of course the inner loop isn't affected by the set event - how should it be, if you don't check it. if you rewrite it as this: while True: for repeat in range(10): if e.isSet(): break print time.time() time.sleep(.66) it should terminate earlier. What you can't achieve in python without major black magic hackery that is very dangerous is to terminate a thread while it is executing. Termination has to be co-operative. Diez From user at example.net Thu Jul 16 07:04:08 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:04:08 +0200 Subject: using timers to force an execution time In-Reply-To: <7c8fgbF26vgl2U1@mid.uni-berlin.de> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> Message-ID: <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: >>see? the while body ran for about 7 seconds... i bet it has to do with >>the fact that the timer does not control inner loops... any suggestion? > > > Of course the inner loop isn't affected by the set event - how should it be, > if you don't check it. > > if you rewrite it as this: > > while True: > for repeat in range(10): > if e.isSet(): > break > print time.time() > time.sleep(.66) > > it should terminate earlier. so it seems almost impossible to allocate a certain amount of time to a code fragment *without* somehow rewriting its logic? am i wrong? bye From akhilanger at gmail.com Thu Jul 16 07:04:24 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 04:04:24 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24514309.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <24514309.post@talk.nabble.com> Message-ID: <24514367.post@talk.nabble.com> Please click reply on the post and then read this reply in the editor. Actually, some sequences have been replaced to their graphical form when this post is published. So the python code is being displayed, what actually it is not. --Akhil akhil1988 wrote: > > I have switched to python 3.1 , but now I am getting some syntax errors in > the code: > > File "./customWikiExtractor.py", line 81 > __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', > '¢' :u'\u00A2', > ^ > SyntaxError: invalid syntax > > line 81 is: > __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', '¢' > :u'\u00A2', > '£' :u'\u00A3', '¤':u'\u00A4', > '¥' :u'\u00A5', > '¦' :u'\u00A6', '§' :u'\u00A7', > '¨' :u'\u00A8', > '©' :u'\u00A9', 'ª' :u'\u00AA', > '«' :u'\u00AB', > '¬' :u'\u00AC', '­' :u'\u00AD', > '®' :u'\u00AE', > '¯' :u'\u00AF', '°' :u'\u00B0', > '±' :u'\u00B1', > '²' :u'\u00B2', '³' :u'\u00B3', > '´' :u'\u00B4', > 'µ' :u'\u00B5', '¶' :u'\u00B6', > '·' :u'\u00B7', > '¸' :u'\u00B8', '¹' :u'\u00B9', > 'º' :u'\u00BA',} > > --Akhil > > > John Nagle-2 wrote: >> >> akhil1988 wrote: >>> Sorry, it is sgmllib.py and not sgmmlib.py >> >> Oh, that bug again. See >> >> http://bugs.python.org/issue1651995 >> >> It's a bug in SGMLParser. When Python 2.5 restricted ASCII to 0..127, >> SGMLParser needed to be modified, but wasn't. >> >> I reported that bug in February 2007. It was fixed in >> Python 2.6 and 3.0 on March 31, 2009. >> >> John Nagle >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24514367.html Sent from the Python - python-list mailing list archive at Nabble.com. From deets at nospam.web.de Thu Jul 16 07:08:40 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:08:40 +0200 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> Message-ID: <7c8fp8F26vgl2U2@mid.uni-berlin.de> Diez B. Roggisch wrote: > superpollo wrote: > >> hello. >> >> based upon previuos suggestions, i tried to write a program in which i >> would like to have a certain code fragment to execute only for a >> specified amount of time (say 3 seconds), then bail out. >> >> but i get the following: >> >> $ cat tmr004.py >> #!/usr/bin/python -u >> >> import threading , time >> >> e = threading.Event() >> t = threading.Timer(3.0, e.set) >> t.start() >> print time.asctime(time.localtime(time.time())) >> while not e.isSet(): >> for repeat in range(10): >> print time.time() >> time.sleep(0.66) >> print time.asctime(time.localtime(time.time())) >> $ ./tmr004.py >> Thu Jul 16 12:31:27 2009 >> 1247740287.44 >> 1247740288.1 >> 1247740288.76 >> 1247740289.42 >> 1247740290.08 >> 1247740290.74 >> 1247740291.4 >> 1247740292.06 >> 1247740292.72 >> 1247740293.38 >> Thu Jul 16 12:31:34 2009 >> $ >> >> see? the while body ran for about 7 seconds... i bet it has to do with >> the fact that the timer does not control inner loops... any suggestion? > > Of course the inner loop isn't affected by the set event - how should it > be, if you don't check it. > > if you rewrite it as this: > > while True: > for repeat in range(10): > if e.isSet(): > break > print time.time() > time.sleep(.66) > > it should terminate earlier. This is of course wrong, remove the outer "while" Diez From user at example.net Thu Jul 16 07:09:24 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:09:24 +0200 Subject: using timers to force an execution time In-Reply-To: <7c8futF26vgl2U3@mid.uni-berlin.de> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> Message-ID: <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: >>am i wrong? > > > No, you are right, for threads that is. You can try & trick around with the > trace-functionality of python, and some ctypes-based > system-thread-module-tricks that are, as mentioned before, black-magic & a > spinning gatling gun pointed to your very own lower extremities. > OUCH. > The only reliable way of terminating an asynchronous computation is to use > processes, since python2.6 that's rather convenient with the > multiprocessing-module. However, that imposes some limits to what you can > do. fair enough. very helpful indeed. bye From deets at nospam.web.de Thu Jul 16 07:11:41 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:11:41 +0200 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <7c8futF26vgl2U3@mid.uni-berlin.de> superpollo wrote: > Diez B. Roggisch wrote: >> superpollo wrote: >>>see? the while body ran for about 7 seconds... i bet it has to do with >>>the fact that the timer does not control inner loops... any suggestion? >> >> >> Of course the inner loop isn't affected by the set event - how should it >> be, if you don't check it. >> >> if you rewrite it as this: >> >> while True: >> for repeat in range(10): >> if e.isSet(): >> break >> print time.time() >> time.sleep(.66) >> >> it should terminate earlier. > > so it seems almost impossible to allocate a certain amount of time to a > code fragment *without* somehow rewriting its logic? > > am i wrong? No, you are right, for threads that is. You can try & trick around with the trace-functionality of python, and some ctypes-based system-thread-module-tricks that are, as mentioned before, black-magic & a spinning gatling gun pointed to your very own lower extremities. The only reliable way of terminating an asynchronous computation is to use processes, since python2.6 that's rather convenient with the multiprocessing-module. However, that imposes some limits to what you can do. Diez From deets at nospam.web.de Thu Jul 16 07:15:04 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:15:04 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <7c8g58F26vgl2U4@mid.uni-berlin.de> superpollo wrote: > hi there. > > is there a way to dump the content of a turtle window to a file or a > file object? Why should I want to dump a turtle? They are very benign creatures, dumping them is going to hurt them needlessly. Without a cheek-in-tongue: how are we supposed to know what a"turtle window" is, how you create it, what features it uses? You need to give more details, such as the module you are using, on what system that runs and so forth. Diez From user at example.net Thu Jul 16 07:18:51 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:18:51 +0200 Subject: turtle dump In-Reply-To: <7c8g58F26vgl2U4@mid.uni-berlin.de> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> Message-ID: <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: > > >>hi there. >> >>is there a way to dump the content of a turtle window to a file or a >>file object? > > > Why should I want to dump a turtle? They are very benign creatures, dumping > them is going to hurt them needlessly. > lol. ;-) the title was indeed supposed to stir a bit of curiosity upon the reader... > Without a cheek-in-tongue: how are we supposed to know what a"turtle window" > is, how you create it, what features it uses? You need to give more > details, such as the module you are using, on what system that runs and so > forth. in fact i was looking for a *platform independent* way to draw into a graphics file (say, a postscript or a png) using the turtle module. so i understand that "dumping a window" is not a good expression... maybe "redirecting graphics commands to a file instead of a window"? bye From wuwei23 at gmail.com Thu Jul 16 07:24:30 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 04:24:30 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> Message-ID: <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> On Jul 16, 9:00?pm, akhil1988 wrote: > I have switched to python 3.1 , but now I am getting some syntax errors in > the code: Python 3.x was a major release that endeavoured to clean up a number of lingering issues with the language, the upshot being that it isn't entirely backwards compatible with past versions. Unicode became the default string type, which is what is causing the error here: the u- prefix is no longer required (or even allowed). However, Py3.x _does_ come with a handy tool for automatically converting Python 2.x code to 3.x, called 2to3. One of the things it should do is convert Py2.x unicode values into their correct representation in 3.x. With any luck, it should be able to convert the code you're using entirely. Let us know how it goes. From wuwei23 at gmail.com Thu Jul 16 07:50:29 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 04:50:29 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> On Jul 16, 9:18?pm, superpollo wrote: > lol. ;-) the title was indeed supposed to stir a bit of curiosity upon > the reader... Which isn't really useful when trying to obtain assistance... you want certainty, not curiosity. > in fact i was looking for a *platform independent* way to draw into a > graphics file (say, a postscript or a png) using the turtle module. so i > understand that "dumping a window" is not a good expression... maybe > "redirecting graphics commands to a file instead of a window"? You didn't actually answer Diez question. The turtle module is only a recent (2.6/3.0) addition to Python and is probably obscure enough not to have tracked across everyone's radar. So _if_ you're talking about the standard lib turtle module, you're not looking to 'dump a window', you're looking to write the contents of turtle.Canvas to file. Canvas only supports writing to postscript, so you'll have to do something like this (untested): import turtle screen = turtle.Screen() # go go gadget turtle... screen._canvas.postscript(file='turtle.ps') Or it may be open('turtle.ps','w').write(screen._canvas.postscript ())... I couldn't find a definitive answer. Try looking through the Tk docs, it should be covered there. Note that this is a postscript _text_ file, so you'll also need to find something to render it with. Hope this helps point you in the right direction. From user at example.net Thu Jul 16 08:11:07 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 14:11:07 +0200 Subject: turtle dump In-Reply-To: <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> Message-ID: <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> alex23 wrote: > On Jul 16, 9:18 pm, superpollo wrote: > >>lol. ;-) the title was indeed supposed to stir a bit of curiosity upon >>the reader... > > > Which isn't really useful when trying to obtain assistance... you want > certainty, not curiosity. > ok. my bad. > >>in fact i was looking for a *platform independent* way to draw into a >>graphics file (say, a postscript or a png) using the turtle module. so i >>understand that "dumping a window" is not a good expression... maybe >>"redirecting graphics commands to a file instead of a window"? > > > You didn't actually answer Diez question. The turtle module is only a > recent (2.6/3.0) addition to Python and is probably obscure enough not > to have tracked across everyone's radar. > actually i am still using 2.3.4, which means that... > screen = turtle.Screen() ... is not possible > > Hope this helps point you in the right direction. it certainly did, tahnks. bye From bearophileHUGS at lycos.com Thu Jul 16 08:22:22 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 16 Jul 2009 05:22:22 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <267d74e2-97ce-477c-a39b-022fc4f82bd3@i6g2000yqj.googlegroups.com> Superchicken: > is there a way to dump the content of a turtle window to a file or a file object? A possible low-tech solution is to append to a list the sequence of your plotting commands (using a decorator too, even, something like the logging decorator), and then save all of them at the end into a text file :-) Bye, bearophile From piet at cs.uu.nl Thu Jul 16 08:39:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 14:39:54 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: Message-ID: >>>>> mheavner (m) wrote: >m> I'm using multiprocessing to spawn several subprocesses, each of which >m> uses a very large data structure (making it impractical to pass it via >m> pipes / pickling). I need to allocate this structure once when the >m> process is created and have it remain in memory for the duration of >m> the process. The way the multiprocessing module is set up, only the >m> 'run' method runs within the subprocess - so creating a wrapper class >m> with a constructor that allocates the structure in __init__ will not >m> work, as far as I know, as this will still be within the parent >m> process. >m> If I were working in C/C++, I would declare the variable "static" >m> within the function body - is there any way with the multiprocessing >m> module to have persistent data members within subprocesses? >m> Any ideas?? Your post is not entirely clear. Is `the process' the same as `the subprocess'? Assuming it is, what is the problem? You can create the datastructure first thing in the run method can't you? Like this: from multiprocessing import Process from time import sleep from random import random class MyProcess(Process): def __init__(self, number): self.number = number Process.__init__(self) def run(self): print "Process %s started" % self.number self.data = range(self.number * 100000, (self.number + 1) * 100000) self.doit() def doit(self): for i in range(5): sleep(3 * random()) self.data[i] += i print self.data[i] processes = [] for k in range(10): p = MyProcess(k) p.start() processes.append(p) for p in processes: p.join() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From albert at spenarnc.xs4all.nl Thu Jul 16 08:45:27 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 16 Jul 2009 12:45:27 GMT Subject: Clarity vs. code reuse/generality References: Message-ID: In article , kj wrote: > > >I'm will be teaching a programming class to novices, and I've run >into a clear conflict between two of the principles I'd like to >teach: code clarity vs. code reuse. I'd love your opinion about >it. > > >This seemed straightforward enough, until I realized that, to be >useful to my students in their homework, this _binary_search function >had to handle the case in which the passed function was monotonically >decreasing in the specified interval... > > >Here's the rub: the code above is more general (hence more reusable) >by virtue of this trick with the sense parameter, but it is also >a bit harder to understand. > >This not an unusual situation. I find that the processing of >abstracting out common logic often results in code that is harder >to read, at least for the uninitiated... Yes, of course. You're teaching, say, green belt programmers. Good reusable code requires a fifth dan. You may not be up to it yourself (no offense intended), let alone your students. Writing a reusable binary search is a very different assignment from being able to code/use it in a concrete example. Their choice must be between *writing* a one-off binary search, versus *using* a brilliantly code, brilliantly documented binary search that has been given beforehand. Even assuming the reusable code is perfect in all sense, reusing may be more effort than writing from scratch. Then *you* have to explain them about the benefits of reuse. (The other benefits, of course.) > >I'd love to know your opinions on this. You're welcome. >kj Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From __peter__ at web.de Thu Jul 16 08:46:40 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 16 Jul 2009 14:46:40 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: superpollo wrote: > alex23 wrote: >> On Jul 16, 9:18 pm, superpollo wrote: >> >>>lol. ;-) the title was indeed supposed to stir a bit of curiosity upon >>>the reader... >> >> >> Which isn't really useful when trying to obtain assistance... you want >> certainty, not curiosity. >> > > ok. my bad. > >> >>>in fact i was looking for a *platform independent* way to draw into a >>>graphics file (say, a postscript or a png) using the turtle module. so i >>>understand that "dumping a window" is not a good expression... maybe >>>"redirecting graphics commands to a file instead of a window"? >> >> >> You didn't actually answer Diez question. The turtle module is only a >> recent (2.6/3.0) addition to Python and is probably obscure enough not >> to have tracked across everyone's radar. >> > > actually i am still using 2.3.4, which means that... > >> screen = turtle.Screen() > > ... is not possible Tested on 2.4: >>> import turtle >>> turtle.reset() >>> for i in range(4): ... turtle.forward(50) ... turtle.right(90) ... >>> turtle._canvas.postscript(file="tmp.ps") '' I think the big rewrite has happened in 2.6, so the above should also work in 2.3. Peter From user at example.net Thu Jul 16 09:07:26 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 15:07:26 +0200 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: <4a5f260f$0$18931$4fafbaef@reader2.news.tin.it> Peter Otten wrote: > Tested on 2.4: > > >>>>import turtle >>>>turtle.reset() >>>>for i in range(4): > > ... turtle.forward(50) > ... turtle.right(90) > ... > >>>>turtle._canvas.postscript(file="tmp.ps") > > '' > > I think the big rewrite has happened in 2.6, so the above should also work > in 2.3. > > Peter > mr otten, you are great. bye From hartley79 at gmail.com Thu Jul 16 09:10:35 2009 From: hartley79 at gmail.com (hartley) Date: Thu, 16 Jul 2009 06:10:35 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> Message-ID: /* the first telling */ (...) /* the third telling */ (...) /* the third telling */ - If you had loosened up on the sarcasm I would probably have read what you wrote more thoroughly instead of just skimming through it. Thanks for the help, but you should seriously consider doing something with your patronizing attitude. HTH, Hartley From hartley79 at gmail.com Thu Jul 16 09:13:09 2009 From: hartley79 at gmail.com (hartley) Date: Thu, 16 Jul 2009 06:13:09 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> Message-ID: <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> /* the first telling */ (...) /* the second telling */ (...) /* the third telling */ - If you had loosened up on the sarcasm I would probably have read what you wrote more thoroughly instead of just skimming through it. Thanks for the help, but you should seriously consider doing something with your patronizing attitude. HTH, Hartley From albert at spenarnc.xs4all.nl Thu Jul 16 09:15:59 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 16 Jul 2009 13:15:59 GMT Subject: Clarity vs. code reuse/generality References: <006eb26f$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Dennis Lee Bieber wrote: >On 07 Jul 2009 05:05:12 GMT, Steven D'Aprano > declaimed the following in >gmane.comp.python.general: > >> Paraphrasing the Collins Dictionary of Mathematics: >> >> opposite sense. Sense is also used to distinguish clockwise and anti- >> clockwise. >> > Which, I believe, is the only usage I've encountered of it... In >regards to quantum spin states in such things as Scientific American >(though that magazine has either gone down hill in the last 30 years, or >my expectations have gone up... Current issues read like the first years >of Discover magazine) I dropped my subscription when power was expressed in multiples of US hair dryers. (I could calculate that back, and was appalled by the energy wastage of US hair dryers. ;-) ) >-- > Wulfraed Dennis Lee Bieber KD6MOG Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From matt.dubins at sympatico.ca Thu Jul 16 09:17:27 2009 From: matt.dubins at sympatico.ca (inkhorn) Date: Thu, 16 Jul 2009 06:17:27 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> <22fa2fe9-899b-407a-8e3c-49f29c86fa59@r33g2000yqn.googlegroups.com> Message-ID: Hi all, This was more a question of programming aesthetics for me than one of great practical significance. I was looking to perform a certain function on files in a directory so long as those files weren't found in certain standard directories. In other words, I was using os.walk () to get multiple root directory strings, and the lists of files in each directory. The function was to be performed on those files, so long as certain terms weren't in the root directory string. In actuality, I could have stuck with the helper function I created, but I'm always curious to see how well multiple lines of code can turn into fewer lines of code in python and retain the same functional value :) Matt On Jul 15, 6:46?am, denis wrote: > Sure, Aho-Corasick is fast for fixedstrings; but without real > numbers / a concrete goal > > > > Matt, how many words are you looking for, in how long a string ? > > a simple solution is good enough, satisficing. ?Matt asked "how to > make that function look nicer?" > but "nice" has many dimensions -- bicycles are nice for some tasks, > Ferraris for others. > > Bythewayhttp://en.wikipedia.org/wiki/Aho-Corasick_algorithmhas a > link to a Python implementation, > alsohttp://en.wikipedia.org/wiki/Worse_is_Betteris fun. From motoom at xs4all.nl Thu Jul 16 09:17:47 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Thu, 16 Jul 2009 15:17:47 +0200 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: <4A5F287B.2000503@xs4all.nl> I got success with the following code (python 2.6.2): import turtle turtle.reset() for i in range(4): turtle.forward(50) turtle.right(90) can=turtle.getscreen().getcanvas() can.postscript(file="tmp.ps") -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From miheavner at gmail.com Thu Jul 16 09:18:43 2009 From: miheavner at gmail.com (mheavner) Date: Thu, 16 Jul 2009 06:18:43 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: Message-ID: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> On Jul 16, 8:39?am, Piet van Oostrum wrote: > >>>>> mheavner (m) wrote: > >m> I'm using multiprocessing to spawn several subprocesses, each of which > >m> uses a very large data structure (making it impractical to pass it via > >m> pipes / pickling). I need to allocate this structure once when the > >m> process is created and have it remain in memory for the duration of > >m> the process. The way the multiprocessing module is set up, only the > >m> 'run' method runs within the subprocess - so creating a wrapper class > >m> with a constructor that allocates the structure in __init__ will not > >m> work, as far as I know, as this will still be within the parent > >m> process. > >m> If I were working in C/C++, I would declare the variable "static" > >m> within the function body - is there any way with the multiprocessing > >m> module to have persistent data members within subprocesses? > >m> Any ideas?? > > Your post is not entirely clear. Is `the process' the same as `the > subprocess'? > > Assuming it is, what is the problem? You can create the datastructure > first thing in the run method can't you? > > Like this: > > from multiprocessing import Process > from time import sleep > from random import random > > class MyProcess(Process): > > ? ? def __init__(self, number): > ? ? ? ? self.number = number > ? ? ? ? Process.__init__(self) > > ? ? def run(self): > ? ? ? ? print "Process %s started" % self.number > ? ? ? ? self.data = range(self.number * 100000, (self.number + 1) * 100000) > ? ? ? ? self.doit() > > ? ? def doit(self): > ? ? ? ? for i in range(5): > ? ? ? ? ? ? sleep(3 * random()) > ? ? ? ? ? ? self.data[i] += i > ? ? ? ? ? ? print self.data[i] > > processes = [] > for k in range(10): > ? ? p = MyProcess(k) > ? ? p.start() > ? ? processes.append(p) > > for p in processes: > ? ? p.join() > > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org 'The process' refers to the subprocess. I could do as you say, load the data structure each time, but the problem is that takes a considerable amount of time compared to the the actual computation with the data it contains. I'm using these processes within a loop as follows: # Don't recreate processes or Queues pop1 = Queue() pop2 = Queue() pop_out = Queue() p1 = CudaProcess(0, args=(costf,pop1,pop_out)) p2 = CudaProcess(1, args=(costf,pop2,pop_out)) # Main loop for i in range(maxiter): print 'ITERATION: '+str(i) if log != None: l = open(log,'a') l.write('Iteration: '+str(i)+'\n') l.close() # Split population in two pop1.putmany(pop[0:len(pop)/2]) pop2.putmany(pop[len(pop)/2:len(pop)]) # Start two processes if not p1.isAlive(): p1.start() print 'started %s'%str(p1.getPid()) else: p1.run() if not p2.isAlive(): p2.start() print 'started %s'%str(p2.getPid()) else: p2.run() . . . So I'd like to load that data into memory once and keep there as long as the process is alive (ideally when the subprocess is created, storing some sort of pointer to it), rather than loading it each time run is called for a process within the loop. Could be my CudaProcess class - I'll check out what Diez suggested and post back. From albert at spenarnc.xs4all.nl Thu Jul 16 09:19:08 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 16 Jul 2009 13:19:08 GMT Subject: Clarity vs. code reuse/generality References: <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Lie Ryan wrote: >Steven D'Aprano wrote: >> On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: >> >>> When people are fighting over things like `sense`, although sense may >>> not be strictly wrong dictionary-wise, it smells of something burning... >> >> That would be my patience. >> >> I can't believe the direction this discussion has taken. > >Me neither. > >> Anybody sensible >> would be saying "Oh wow, I've just learned a new meaning to the word, >> that's great, I'm now less ignorant than I was a minute ago". But oh no, >> we mustn't use a standard meaning to a word, heaven forbid we disturb >> people's ignorance by teaching them something new. > >A meaning of a word is meaningless if nobody apart the writer >understands it. The purpose of code is 1) to communicate with the Exactly. And the OP teaches to scientist. They know sense in that meaning. Maybe you don't, but that is irrelevant. Groetjes Albert. -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From anthony.tolle at gmail.com Thu Jul 16 09:23:57 2009 From: anthony.tolle at gmail.com (Anthony Tolle) Date: Thu, 16 Jul 2009 06:23:57 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <7x3a8xwjq5.fsf@ruckus.brouhaha.com> Message-ID: On Jul 15, 8:32?pm, Paul Rubin wrote: > Among other things, that uses quadratic time! ?Why do you want to keep > popping items from that list instead of iterating through it anyway? > > Anyway, I think you wrote something close to this: > ... Very true! I didn't think about the problems with pop(). I was using it as a shortcut for pulling off the first operand. I forgot that if you start with an initial operand of "False", the result will be the same (0 xor X = X) While I'm not sure how useful it would be, here's a version of the first function that returns one of the operands (ala AND and OR), except in the case where there is an even number of True elements, where it returns False: def xor(*operands): r, rprime = False, False for x in operands: xprime = bool(x) if rprime: if xprime: r, rprime = False, False else: r, rprime = x, xprime return r >>> xor(0, 0) 0 >>> xor(0, 1) 1 >>> xor(1, 0) 1 >>> xor(1, 1) False >>> xor(0, 1, 2) False >>> xor(0, 1, 2, 3) 3 >>> xor(None, []) [] From nick at craig-wood.com Thu Jul 16 09:29:59 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 16 Jul 2009 08:29:59 -0500 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: superpollo wrote: > Diez B. Roggisch wrote: > > superpollo wrote: > >>am i wrong? > > > > > > No, you are right, for threads that is. You can try & trick around with the > > trace-functionality of python, and some ctypes-based > > system-thread-module-tricks that are, as mentioned before, black-magic & a > > spinning gatling gun pointed to your very own lower extremities. > > > > OUCH. > > > The only reliable way of terminating an asynchronous computation is to use > > processes, since python2.6 that's rather convenient with the > > multiprocessing-module. However, that imposes some limits to what you can > > do. > > fair enough. Or if you are on unix you can use signals... It is probably just about acceptable to raise an exception in a signal handler like this code does. import signal, os, time class TimeOut(Exception): """Thrown on alarm""" pass def sig_alarm(signum, frame): raise TimeOut() def time_out(t, fn, *args, **kwargs): """Calls fn with the args and kwargs returning its result or raising a TimeOut exception if it doesn't complete within t seconds""" # Turn alarm off and read old value old_alarm = signal.alarm(0) # Install new handler remembering old old_handler = signal.signal(signal.SIGALRM, sig_alarm) # Set the timer going signal.alarm(t) try: rc = fn(*args, **kwargs) finally: # Restore the old handler signal.signal(signal.SIGALRM, old_handler) signal.alarm(0) def test(): for repeat in range(10): print time.time() time.sleep(0.66) if __name__ == "__main__": try: time_out(3, test) except TimeOut: print "timed out" -- Nick Craig-Wood -- http://www.craig-wood.com/nick From inky788 at gmail.com Thu Jul 16 09:32:34 2009 From: inky788 at gmail.com (Inky 788) Date: Thu, 16 Jul 2009 06:32:34 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> Message-ID: <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> On Jul 10, 7:35?pm, Ben Finney wrote: > walterbyrd writes: > > I believe Guido himself has said that all indentions should be four > > spaces - no tabs. > > Yes. That's a ?should? and not a ?must?, even though PEP 8 says it > with a simple imperative:: > > ? ? Use 4 spaces per indentation level. That actually sounds pretty weird. "Don't do {foo}. Really, I mean *really* don't do {foo}. Oh, also, the interpreter allows you to do {foo}. But don't do it! I mean it!". Very ... perlish, if you ask me. I realize that a small portion of the community likes the tabs. They're sold on the tabs. They like the tabs. But tabs are an archaic holdover from an era when typewriters had physical tabstops on them. Actually, after *that* they're a holdover from a program called `make` whose designers unfortunately decided to use tab "characters" to prefix commands. The tab is vestigial. Your text editor can treat 4 consecutive spaces as a unit if you wish. Let tabs go away. Stop using them. If your editor keeps irrationally inserting them, make it stop. From miheavner at gmail.com Thu Jul 16 09:32:47 2009 From: miheavner at gmail.com (mheavner) Date: Thu, 16 Jul 2009 06:32:47 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: On Jul 16, 9:18?am, mheavner wrote: > On Jul 16, 8:39?am, Piet van Oostrum wrote: > > > > > >>>>> mheavner (m) wrote: > > >m> I'm using multiprocessing to spawn several subprocesses, each of which > > >m> uses a very large data structure (making it impractical to pass it via > > >m> pipes / pickling). I need to allocate this structure once when the > > >m> process is created and have it remain in memory for the duration of > > >m> the process. The way the multiprocessing module is set up, only the > > >m> 'run' method runs within the subprocess - so creating a wrapper class > > >m> with a constructor that allocates the structure in __init__ will not > > >m> work, as far as I know, as this will still be within the parent > > >m> process. > > >m> If I were working in C/C++, I would declare the variable "static" > > >m> within the function body - is there any way with the multiprocessing > > >m> module to have persistent data members within subprocesses? > > >m> Any ideas?? > > > Your post is not entirely clear. Is `the process' the same as `the > > subprocess'? > > > Assuming it is, what is the problem? You can create the datastructure > > first thing in the run method can't you? > > > Like this: > > > from multiprocessing import Process > > from time import sleep > > from random import random > > > class MyProcess(Process): > > > ? ? def __init__(self, number): > > ? ? ? ? self.number = number > > ? ? ? ? Process.__init__(self) > > > ? ? def run(self): > > ? ? ? ? print "Process %s started" % self.number > > ? ? ? ? self.data = range(self.number * 100000, (self.number + 1) * 100000) > > ? ? ? ? self.doit() > > > ? ? def doit(self): > > ? ? ? ? for i in range(5): > > ? ? ? ? ? ? sleep(3 * random()) > > ? ? ? ? ? ? self.data[i] += i > > ? ? ? ? ? ? print self.data[i] > > > processes = [] > > for k in range(10): > > ? ? p = MyProcess(k) > > ? ? p.start() > > ? ? processes.append(p) > > > for p in processes: > > ? ? p.join() > > > -- > > Piet van Oostrum > > URL:http://pietvanoostrum.com[PGP8DAE142BE17999C4] > > Private email: p... at vanoostrum.org > > 'The process' refers to the subprocess. I could do as you say, load > the data structure each time, but the problem is that takes a > considerable amount of time compared to the the actual computation > with the data it contains. I'm using these processes within a loop as > follows: > > ? ? ? ? ?# Don't recreate processes or Queues > ? ? ? ? ?pop1 = Queue() > ? ? ? ? ?pop2 = Queue() > ? ? ? ? ?pop_out = Queue() > ? ? ? ? ?p1 = CudaProcess(0, args=(costf,pop1,pop_out)) > ? ? ? ? ?p2 = CudaProcess(1, args=(costf,pop2,pop_out)) > > ? ? ? ? ?# Main loop > ? ? ? ? ?for i in range(maxiter): > ? ? ? ? ? ? ? ? ?print 'ITERATION: '+str(i) > ? ? ? ? ? ? ? ? ?if log != None: > ? ? ? ? ? ? ? ? ? ? ? ? ?l = open(log,'a') > ? ? ? ? ? ? ? ? ?l.write('Iteration: '+str(i)+'\n') > ? ? ? ? ? ? ? ? ?l.close() > > ? ? ? ? ? ? ? ? ?# Split population in two > ? ? ? ? ? ? ? ? ?pop1.putmany(pop[0:len(pop)/2]) > ? ? ? ? ? ? ? ? ?pop2.putmany(pop[len(pop)/2:len(pop)]) > > ? ? ? ? ? ? ? ? ?# Start two processes > ? ? ? ? ? ? ? ? ?if not p1.isAlive(): > ? ? ? ? ? ? ? ? ? ? ? ? ?p1.start() > ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p1.getPid()) > ? ? ? ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ? ? ? ? ? ?p1.run() > ? ? ? ? ? ? ? ? ?if not p2.isAlive(): > ? ? ? ? ? ? ? ? ? ? ? ? ?p2.start() > ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p2.getPid()) > ? ? ? ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ? ? ? ? ? ?p2.run() > ? ? ? ? ? ? ? ? ?. > ? ? ? ? ? ? ? ? ?. > ? ? ? ? ? ? ? ? ?. > > So I'd like to load that data into memory once and keep there as long > as the process is alive (ideally when the subprocess is created, > storing some sort of pointer to it), rather than loading it each time > run is called for a process within the loop. Could be my CudaProcess > class - I'll check out what Diez suggested and post back. Essentially, I'd like to "sneak" that allocation in somewhere after the fork is done (in start()) in the context of the subprocess, holding a pointer to that structure, but before all of the run() calls are done From emile at fenx.com Thu Jul 16 09:34:34 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 06:34:34 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5EEDAE.3040605@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: On 7/16/2009 2:06 AM Jean-Michel Pichavant said... > Ok then, why "or" does not return True, if the first element is > considered True ? Why returning the element itself. Any reason for that > ? Because it's confusing, maybe people used to that logic find it > obvious, but I really don't. For example, I sometimes use it to set defaults: daysInAdvance = int(inputVar) or 25 Emile From wuwei23 at gmail.com Thu Jul 16 09:43:20 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 06:43:20 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: On Jul 16, 10:11?pm, superpollo wrote: > actually i am still using 2.3.4, which means that... > > > screen = turtle.Screen() > > ... is not possible Ah, sorry about that. My belief that turtle was a new module was based on a line from http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle-PyCon2007.pdf Since Python 2.6/3.0, Python has had a new turtle module. At which point I stopped reading and missed the following line: Its development was based entirely on the previous one. In my defence, I _had_ been drinking. Thankfully Peter stepped up with a more appropriate solution, and Michiel pointed out the more suitable API calls over dealing directly with the underlying implementation :) Good work guys! From p.f.moore at gmail.com Thu Jul 16 09:46:13 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Thu, 16 Jul 2009 14:46:13 +0100 Subject: using timers to force an execution time In-Reply-To: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <79990c6b0907160646n58163a3fx1577951dab64215d@mail.gmail.com> 2009/7/16 superpollo : > hello. > > based upon previuos suggestions, i tried to write a program in which i would > like to have a certain code fragment to execute only for a specified amount > of time (say 3 seconds), then bail out. > > but i get the following: > > $ cat tmr004.py > #!/usr/bin/python -u > > import threading , time > > e = threading.Event() > t = threading.Timer(3.0, e.set) > t.start() > print time.asctime(time.localtime(time.time())) > while not e.isSet(): > ? ?for repeat in range(10): > ? ? ? ?print time.time() > ? ? ? ?time.sleep(0.66) > print time.asctime(time.localtime(time.time())) > $ ./tmr004.py > Thu Jul 16 12:31:27 2009 > 1247740287.44 > 1247740288.1 > 1247740288.76 > 1247740289.42 > 1247740290.08 > 1247740290.74 > 1247740291.4 > 1247740292.06 > 1247740292.72 > 1247740293.38 > Thu Jul 16 12:31:34 2009 > $ > > see? the while body ran for about 7 seconds... i bet it has to do with the > fact that the timer does not control inner loops... any suggestion? Clearly, this isn't what you are actually trying to do. For this case, your event is getting set when you expect, but your main thread does not check the event often enough to let it stop in a timely manner. To fix this, remove the inner "for repeat in range(10)" loop. But I assume that your "real" code isn't something that you can fix this easily. If you describe your real requirement, it may be possible to give you a better answer. (But that answer will almost certainly not be a way of interrupting one thread from another - that's not really possible with Python - but rather a way of achieving your goal without *needing* to interrupt one thread from another). Paul. PS If you really must interrupt one thread from another, you can use C code (or ctypes) to call the PyThreadState_SetAsyncExc API. But I'm not going to tell you how, as it's almost certainly *not* what you want to do in practice :-) From user at example.net Thu Jul 16 09:46:41 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 15:46:41 +0200 Subject: using timers to force an execution time In-Reply-To: References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <4a5f2f43$0$18933$4fafbaef@reader2.news.tin.it> Nick Craig-Wood wrote: > superpollo wrote: > ... > Or if you are on unix you can use signals... > > It is probably just about acceptable to raise an exception in a signal > handler like this code does. > > ... neat! From icanbob at gmail.com Thu Jul 16 09:50:52 2009 From: icanbob at gmail.com (bobicanprogram) Date: Thu, 16 Jul 2009 06:50:52 -0700 (PDT) Subject: Passing handlers between bound c++ libs References: <3e7b0717-a665-4f42-b7bd-7a99d59258c4@m11g2000yqh.googlegroups.com> Message-ID: On Jul 14, 7:07 pm, Freyr wrote: > I have a python bound physics library that uses handler to process > events. The passing of handlers between c++ and python causes a huge > overhead slowing down the process. Can I implement a handler in my > custom python bound c++ lib B and pass it to blackbox python bound c++ > lib A so that A would call B directly without passing through the > python layer? Or to phrase the question differently: Can I pass a > handler from B through python to A so that A will invoke B with out > calling into python code? Depending on your exact configuration you may be able to wrap that C++ library using the SIMPL toolkit and get at your data with a message. There is some "hello world" demo code here: http://www.icanprogram.com/06py/main.html bob From user at example.net Thu Jul 16 09:51:59 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 15:51:59 +0200 Subject: using timers to force an execution time In-Reply-To: References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <4a5f3080$0$18933$4fafbaef@reader2.news.tin.it> Nick Craig-Wood wrote: > ... > import signal, os, time > ... importing os is useless of course... From jeanmichel at sequans.com Thu Jul 16 09:53:45 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 16 Jul 2009 15:53:45 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: <4A5F30E9.1000404@sequans.com> Emile van Sebille wrote: > On 7/16/2009 2:06 AM Jean-Michel Pichavant said... >> Ok then, why "or" does not return True, if the first element is >> considered True ? Why returning the element itself. Any reason for >> that ? Because it's confusing, maybe people used to that logic find >> it obvious, but I really don't. > > For example, I sometimes use it to set defaults: > > daysInAdvance = int(inputVar) or 25 > > Emile > Sure this looks like an elegant way to set default values and I will use this form , but I'm not sure this justifies by itself the trickery. Python has extended the algebra definition of "or" and "and" top any type, but it is so unintuitive (I'm no LISP programmer). I think than using the short-circuiting mechanism of bool operators along with the python tricks is just error prone and may result in bug difficult to spot, unless you are very aware of all python boolean mechanisms. JM From invalid at invalid Thu Jul 16 10:04:34 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 16 Jul 2009 09:04:34 -0500 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: On 2009-07-16, Emile van Sebille wrote: > On 7/16/2009 2:06 AM Jean-Michel Pichavant said... >> Ok then, why "or" does not return True, if the first element is >> considered True ? Why returning the element itself. Any reason for that >> ? Because it's confusing, maybe people used to that logic find it >> obvious, but I really don't. > > For example, I sometimes use it to set defaults: > > daysInAdvance = int(inputVar) or 25 I don't get it. That doesn't work right when inputVar == "0". -- Grant Edwards grante Yow! What I want to find at out is -- do parrots know visi.com much about Astro-Turf? From invalid at invalid Thu Jul 16 10:05:22 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 16 Jul 2009 09:05:22 -0500 Subject: How to search this newsgroup by a python script. References: <7c897lF26dea4U1@mid.dfncis.de> Message-ID: On 2009-07-16, Chris Rebert wrote: > On Thu, Jul 16, 2009 at 2:12 AM, Helmut > Jarausch wrote: >> Hi, >> >> I haven't found anything with Google's group search, so let me >> ask it (again?). >> >> How can I search this newsgroup from within a Python script. >> (Perhaps by searching Google Groups or Gmane by some Python code.) > > 1. Generate URL of the form: > http://search.gmane.org/?query=foo&group=gmane.comp.python.general > where "foo" is the search terms, with proper URL escaping applied. > 2. Fetch URL using urllib - http://docs.python.org/library/urllib.html > 3. Parse resulting HTML page (e.g. using BeautifulSoup) > 4. Extract desired information from search results using the parse tree. > 5. ??? > 6. Profit! The underpants gnomes would be proud. -- Grant Edwards grante Yow! I'm encased in the at lining of a pure pork visi.com sausage!! From nleioatt at gmail.com Thu Jul 16 10:07:58 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 16 Jul 2009 07:07:58 -0700 (PDT) Subject: This is a mess... Message-ID: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> I've been coding python for about a week now, and i'm trying to make an object oriented version of a program i just wrote. in this post is the original program. the next post will include the new programs and the traceback. i'm sure there are many underlying problems, but have been stuck on this for 2 days now... email me for input files since they are big and i don't see a way to upload a file here(~5 mB) this program works, and inputing the same file twice you should get a "Q" values of ~1 (.99...) ##############CODE##################### #9July09 #Compute the covariance overlap of two results #Read in _U.asc files for both ensembles to find e-vectors #Read in _ .asc files for both ensembles to find sqrt e-values #strip out matrix info #compute corvariance import sys import math from math import sqrt if len(sys.argv) != 3: print " " print "The corrent usage is 'python covariance.py file1 file2'" print "where the _U.asc and _s.asc will be appended when needed" print " " exit(1) ####Input the first prefix1_U.asc file: ####this is the first eigen vector matrix prefix1 = sys.argv[1] + "_U.asc" fileholder = open(prefix1) text = fileholder.readlines() fields = text[1].split() num_rows = int(fields[1]) num_cols = int(fields[2]) U1_matrix = [] for line in text[2: num_rows+2]: fields = line.split() for i in range(len(fields)): fields[i] = float(fields[i]) U1_matrix.append(fields) ####Input the second prefix2_U.asc file: ####this is the 2nd eigen vector matrix prefix2 = sys.argv[2] + "_U.asc" fileholder = open(prefix2) text = fileholder.readlines() fields = text[1].split() num_rows = int(fields[1]) num_cols = int(fields[2]) #print "look here nick:", fields U2_matrix = [] for line in text[2: num_rows+2]: fields = line.split() for i in range(len(fields)): fields[i] = float(fields[i]) U2_matrix.append(fields) ####Then Read in the first eigen values ####1st 2 lines are header and need to be stripped away prefix3 = sys.argv[1] + "_s.asc" fileholder = open(prefix3) text = fileholder.readlines() fields = text[1].split() num_vals1 = int(fields[1]) #add check to see if correct # of values added print "square if", len(U1_matrix), "=", len(U2_matrix), "=", len (U1_matrix[1]), "=", len(U2_matrix[1]) #the list of sqrt e vals sqrt_s1 = [float(line) for line in text[2: num_vals1+2]] s1 = [float(line) * float(line) for line in text[2: num_vals1+2]] ####Finally, read in the 2nd set of eigen vals prefix4 = sys.argv[2] + "_s.asc" fileholder = open(prefix4) text = fileholder.readlines() fields = text[1].split() num_vals2 = int(fields[1]) #add check to see if correct # of values added #the list of sqrt e vals sqrt_s2 = [float(line) for line in text[2: num_vals1+2]] s2 = [float(line) * float(line) for line in text[2: num_vals1+2]] #============================================================= ####double summation (the 2nd term) doublesum = 0.0 for i in range(len(U1_matrix[1])): #print "i = ", i v1 = U1_matrix[i] sum = 0 for j in range(len(U2_matrix)): v2 = U2_matrix[j] dot = 0 for k in range(len(U1_matrix)): dot += v1[k] * v2[k] root = sqrt_s1[i] * sqrt_s2[j] sum += root * dot * dot doublesum += sum print "double sum: ", doublesum ####single summation (the 1st term and denominator) singsum = 0.0 for k in range(len(s1)): singsum += s1[k] + s2[k] print "sing sum:", singsum ####Put it all together Q = 1 - sqrt(abs((singsum - (2.0*doublesum)) / singsum)) print "your Q:" print Q ############################# end of code covariance.py From seldan24 at gmail.com Thu Jul 16 10:12:24 2009 From: seldan24 at gmail.com (seldan24) Date: Thu, 16 Jul 2009 07:12:24 -0700 (PDT) Subject: Python Equivalent for dd & fold References: <4A5E1082.8090901@mrabarnett.plus.com> Message-ID: <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> On Jul 15, 1:48?pm, Emile van Sebille wrote: > On 7/15/2009 10:23 AM MRAB said... > > >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: > >>> seldan24 wrote: > >>>> what can I use as the equivalent for the Unix 'fold' command? > >>> def fold(s,len): > >>> ? ? ?while s: > >>> ? ? ? ? ?print s[:len] > >>> ? ? ? ? ?s=s[len:] > > > > You might still need to tweak the above code as regards how line endings > > are handled. > > You might also want to tweak it if the strings are _really_ long to > simply slice out the substrings as opposed to reassigning the balance to > a newly created s on each iteration. > > Emile Thanks for all of the help. I'm almost there. I have it working now, but the 'fold' piece is very slow. When I use the 'fold' command in shell it is almost instantaneous. I was able to do the EBCDIC->ASCII conversion usng the decode method in the built-in str type. I didn't have to import the codecs module. I just decoded the data to cp037 which works fine. So now, I'm left with a large file, consisting of one extremely long line of ASCII data that needs to be sliced up into 35 character lines. I did the following, which works but takes a very long time: f = open(ascii_file, 'w') while ascii_data: f.write(ascii_data[:len]) ascii_data = ascii_data[len:] f.close() I know that Emile suggested that I can slice out the substrings rather than do the gradual trimming of the string variable as is being done by moving around the length. So, I'm going to give that a try... I'm a bit confused by what that means, am guessing that slice can break up a string based on characters; will research. Thanks for the help thus far. I'll post again when all is working fine. From aahz at pythoncraft.com Thu Jul 16 10:14:40 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 07:14:40 -0700 Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: In article <45228cf4-0b37-4c52-bf6f-d7bd124b0f82 at l32g2000vbp.googlegroups.com>, Tony Lay wrote: > >Traceback (most recent call last): > File "/usr/local/bin/meld", line 35, in > import gettext > File "/usr/local/lib/python2.6/gettext.py", line 49, in > import locale, copy, os, re, struct, sys > File "/usr/local/lib/python2.6/locale.py", line 15, in > import functools > File "/usr/local/lib/python2.6/functools.py", line 10, in > from _functools import partial, reduce >ImportError: No module named _functools Where is _functools.so? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From nleioatt at gmail.com Thu Jul 16 10:15:52 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 16 Jul 2009 07:15:52 -0700 (PDT) Subject: This is a mess... References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: this is the new oop version, its pretty messy currently, and i do understand it is a simple routine, but i'm using it as an exercise to learn oop python... first the (current) traceback: [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test Traceback (most recent call last): File "oop_covariance.py", line 24, in cov = set1.covariance(set2, Eigen_vect.dot) File "/home/nleioatts/python/Eigen.py", line 66, in covariance print self.vectors[i][i] AttributeError: Eigen_vect instance has no attribute '__getitem__' and a quick explaination: the file structures aare a 2d list of lists of numbers corresponding to eginvectors and a list of numbers corresponding to eigenvalues #########################33##Here is the main body #!/usr/bin/env python import sys import Eigen from Eigen import * if len(sys.argv) != 3: print " " print "The corrent usage is 'python covariance.py file1 file2'" print "where the _U.asc and _s.asc will be appended when needed" print " " exit(1) file1 = sys.argv[1] file2 = sys.argv[2] set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc") set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc") cov = set1.covariance(set2, Eigen_vect.dot) print cov ###############and here are the classes: #!/usr/bin/env python import sys import math from math import sqrt class Eigen_vect: def __init__(self, e_val, e_vect): self.e_val = e_val self.e_vect = e_vect def length(self): return len(self.e_vect) def dot(self, other): d = 0.0 if other.length() != self.length(): raise ValueError, "Eigen Vectors not same Length" for k in range(self.length()): # print "HI NICK", self.e_vect[k], other.e_vect[k] d += float(self.e_vect[k]) * float(other.e_vect[k]) return d class Eigen_set: def __init__(self, vec_filename, val_filename): self.vec_filename = vec_filename self.val_filename = val_filename # open two files # loop through them, skipping lines that begin with # # for each row, extract eigen vector and eigen values fileholder = open(self.vec_filename) text = fileholder.readlines() fields = text[2].split() # print "len of fields", len(fields) self.vectors = [] for line in text[2: len(fields)]: fields = line.split() # print "len of fields", len(fields) for i in range(len(fields)): fields[i] = float(fields[i]) e_vect = fields fileholder = open(self.val_filename) text = fileholder.readlines() e_val = [float(line) for line in text[2: self.length()]] self.vectors.append(Eigen_vect(e_val, e_vect)) # print "this is self.vectors" # print self.vectors(e_val) def length(self): return len(self.vectors) def covariance(self, other, dot): newdot = 0.0 # do a length check to make sure we're consistent if other.length() != self.length(): raise ValueError, "Eigen Vectors not same Length" #double loop over all my vectors and all of other's vectors doublesum = 0.0 for i in range(self.length()): sum = 0.0 v1 = self.vectors[i] for j in range(self.length()): newdot += v1.dot(self.vectors[j]) # root = self.e_val[i] * other.e_val[j] print self.vectors[i] print self.vectors[j] print self.vectors[i][i] #####################<<------------------------This is line 66, I'm trying to figure out how to call "e_val" from the Eigen_set class root = self.vectors[i][i] * other.vectors[i][j] sum += newdot * newdot * root doublesum += sum ######### singsum = 0.0 for k in range(self.length()): singsum += self.e_val[k] * self.e_val[k] + other.e_val[k] * other.e_val[k] Q = 1 - sqrt(abs((singsum - (2.0*doublesum)) / singsum)) print "your Q:" print Q and any additional help is great. thanks in advance, like the title says this is really a mess.... From python.list at tim.thechases.com Thu Jul 16 10:18:47 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 16 Jul 2009 09:18:47 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: <4A5F36C7.5010307@tim.thechases.com> > I realize that a small portion of the community likes the tabs. > They're sold on the tabs. They like the tabs. But tabs are an archaic > holdover from an era when typewriters had physical tabstops on them. However, they are a single logical level of indentation -- I come down fairly solidly on the "tabs" side of the "tabs vs. spaces" argument. I can set my editor (vim in this case) to show tabs as as many spaces as I want. I usually have this set to 4, but sometimes 1 or 2. In Vim, using tabs I can just switch up my tab-stop setting (":set ts=2") and the presentation of my code reindents the way I expect. If I wanted to switch from 4-real-spaces to 2-real-spaces, I'd have to concoct a perlish regexp to handle the munging. The same "separation of content and presentation" that is all the rage with N-tier programmers and web developers, my content is "logical levels of indentation indicated by a tab character in the source-code" and my presentation is "N spaces as presented by my editor". Yes, the dictatorial "a tab always equals 8 spaces" is a vestigial holdover for which I hold no love. I'll code on other people's projects with spaces if that's the project convention (as Vim lets me switch around fairly easily). But as for my own code, it's tabs all the way. -tkc From sjmachin at lexicon.net Thu Jul 16 10:20:25 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 16 Jul 2009 07:20:25 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> Message-ID: On Jul 16, 11:13?pm, hartley wrote: > /* the first telling */ > (...) > /* the second telling */ > (...) > /* the third telling */ > > - > If you had loosened up on the sarcasm That wasn't sarcasm, it was an attempt at humourous watering-down the expression of exasperation at continued ignoring of advice and wonder that I was continuing to bother ... > HTH, It didn't. From aahz at pythoncraft.com Thu Jul 16 10:22:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 07:22:16 -0700 Subject: Memory leak involving traceback objects References: Message-ID: In article , Rotem wrote: > >I'm debugging a nasty memory leak in a framework written in Python >(v2.6.2). >After much digging around I found that the entire object group that is >leaking is held by a frame object which is subsequently held by a >traceback object. > >Traversing the get_referrers() of each traceback frame leads >eventually to a root traceback frame which has no referrers >(gc.get_referrers returns an empty list). > >However, this traceback object seems not to be picked by the garbage >collector, and is still there even after many iterations and calls to >gc.collect(). The code location to which the traceback frame points >doesn't do anything special - it just catches an exception, without >saving the exception itself and/or traceback anywhere. What *does* it do? Does it re-raise? This sounds like you're still in block scope of an exception. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From ifl2009 at shu.edu Thu Jul 16 10:28:44 2009 From: ifl2009 at shu.edu (IFL 2009) Date: Thu, 16 Jul 2009 10:28:44 -0400 Subject: IFL 2009: Third Call for Papers Message-ID: An HTML attachment was scrubbed... URL: From james at agentultra.com Thu Jul 16 10:49:54 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 16 Jul 2009 10:49:54 -0400 Subject: interactive fiction in Python? References: <5c97224b-99d4-469b-90cb-1c04b1e1e9a0@m11g2000yqh.googlegroups.com> Message-ID: <853a8wlm25.fsf@agentultra.com> George Oliver writes: > hi, I'm just curious who might be working on interactive fiction > modules in the style of Inform or TADS for Python. I've seen a few > threads on this list [1] (among many that mention IF tangentially), > and there are old projects like PUB and PAWS. There are some newer > potential projects such as Curveship as well. In any case I'd like to > see what's out there. I'd be interested in checking out such a project, but I don't really see the point in starting one. There are already several great IF tools out there that are already freely available! The only reason I would see to start one is either for pure intellectual curiosity or because you have some feature ideas that no one else has implemented (and you'd rather cash in on the potential glory than submit a patch!). Best of luck finding what you're looking for... but maybe you should start writing one yourself and post here about it! :) Cheers > > > thanks, George > > > [1]: > > http://bit.ly/Python_Text_Adventure_Authoring_System > > http://bit.ly/simple_interactive_fiction_engine > > http://bit.ly/adventure_engines_in_Python From james at agentultra.com Thu Jul 16 10:51:47 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 16 Jul 2009 10:51:47 -0400 Subject: Ann: Google releases Python-based open-source NX server References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> Message-ID: <85y6qok7ek.fsf@agentultra.com> jkn writes: > Google quietly releases open-source NX server ...written in Python, > apparently > > Google_quietly_releases_open_source_NX_server?taxonomyId=88> > > Neatx can be downloaded from Google's code repository: > > > > Regards > J^n It's pretty cool, but not PEP8! Probably because they just bought the source off of another smaller proprietary project. Makes me sad seeing Google, proud supporter of all things Python, release non-PEP8 code. Either way, I did check out the project and it looks pretty exciting. Hopefully I'll get some time to contribute a few patches. From gabriel.rossetti at arimaz.com Thu Jul 16 11:12:23 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Thu, 16 Jul 2009 17:12:23 +0200 Subject: no return value for threading.Condition.wait(timeout)? Message-ID: <4A5F4357.5060402@arimaz.com> Hello everyone, I am using threading.Condition.wait(timeout) and was surprised to see that there is no return value nor an exception when wait() is used w/ a timeout. How am I supposed to know if it was notified or if it timed out? cheers, Gabriel From maxerickson at gmail.com Thu Jul 16 11:31:36 2009 From: maxerickson at gmail.com (Max Erickson) Date: Thu, 16 Jul 2009 15:31:36 +0000 (UTC) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <24514309.post@talk.nabble.com> <24514367.post@talk.nabble.com> Message-ID: akhil1988 wrote: > > akhil1988 wrote: >> >> I have switched to python 3.1 , but now I am getting some syntax >> errors in the code: >> >> File "./customWikiExtractor.py", line 81 >> __char_entities = {' ' :u'\u00A0', '¡' >> :u'\u00A1', >> '¢' :u'\u00A2', >> ^ You may want to try 2.6. Python 3.1 is not syntax compatible with 2.5 (so the u'' stuff won't work in 3.1): http://docs.python.org/dev/py3k/whatsnew/3.0.html#removed-syntax max From motoom at xs4all.nl Thu Jul 16 11:34:25 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Thu, 16 Jul 2009 17:34:25 +0200 Subject: Python Equivalent for dd & fold In-Reply-To: <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <4A5F4881.9090501@xs4all.nl> seldan24 wrote: > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. An excellent idea. def fold(s,chunklength): offset=0 while offset <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <24514309.post@talk.nabble.com> Message-ID: On Jul 16, 9:04?pm, akhil1988 wrote: > Please click reply on the post and then read this reply in the editor. > Actually, some sequences have been replaced to their graphical form when > this post is published. So the python code is being displayed, what actually > it is not. What editor? I guess you mean that somebody's browser may be interpreting the & blah ; thingies .... but this is not relevant; your syntax error problem is that 2.x unicode literals have a u in front but 3.x str literals don't IOW you need to lose the first u in u'\u00A0' From Caseyweb at gmail.com Thu Jul 16 11:44:18 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Thu, 16 Jul 2009 08:44:18 -0700 (PDT) Subject: Python Equivalent for dd & fold References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <26e55904-58b6-482f-a2b6-c6edfb5bf32e@y4g2000prf.googlegroups.com> On Jul 16, 10:12?am, seldan24 wrote: > On Jul 15, 1:48?pm, Emile van Sebille wrote: > > > > > On 7/15/2009 10:23 AM MRAB said... > > > >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: > > >>> seldan24 wrote: > > >>>> what can I use as the equivalent for the Unix 'fold' command? > > >>> def fold(s,len): > > >>> ? ? ?while s: > > >>> ? ? ? ? ?print s[:len] > > >>> ? ? ? ? ?s=s[len:] > > > > > > You might still need to tweak the above code as regards how line endings > > > are handled. > > > You might also want to tweak it if the strings are _really_ long to > > simply slice out the substrings as opposed to reassigning the balance to > > a newly created s on each iteration. > > > Emile > > Thanks for all of the help. ?I'm almost there. ?I have it working now, > but the 'fold' piece is very slow. ?When I use the 'fold' command in > shell it is almost instantaneous. ?I was able to do the EBCDIC->ASCII > conversion usng the decode method in the built-in str type. ?I didn't > have to import the codecs module. ?I just decoded the data to cp037 > which works fine. > > So now, I'm left with a large file, consisting of one extremely long > line of ASCII data that needs to be sliced up into 35 character > lines. ?I did the following, which works but takes a very long time: > > f = open(ascii_file, 'w') > while ascii_data: > ? ? f.write(ascii_data[:len]) > ? ? ascii_data = ascii_data[len:] > f.close() > > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. ?So, I'm going to give that a try... I'm > a bit confused by what that means, am guessing that slice can break up > a string based on characters; will research. ?Thanks for the help thus > far. ?I'll post again when all is working fine. The problem is that it creates a new string every time you iterate through the "ascii_data = ascii_data[len:]". I believe Emile was suggesting that you just keep moving the starting index through the same string, something like (warning - untested code!): >>> i = 0 >>> str_len = len(ascii_data) >>> while i < str_len: >>> j = min(i + length, str_len) >>> print ascii_data[i:j] >>> i = j From pdpinheiro at gmail.com Thu Jul 16 12:02:25 2009 From: pdpinheiro at gmail.com (pdpi) Date: Thu, 16 Jul 2009 09:02:25 -0700 (PDT) Subject: Python Equivalent for dd & fold References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <4592d512-592b-4e58-829e-e643be34bcec@r33g2000yqn.googlegroups.com> On Jul 16, 3:12?pm, seldan24 wrote: > On Jul 15, 1:48?pm, Emile van Sebille wrote: > > > > > > > On 7/15/2009 10:23 AM MRAB said... > > > >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: > > >>> seldan24 wrote: > > >>>> what can I use as the equivalent for the Unix 'fold' command? > > >>> def fold(s,len): > > >>> ? ? ?while s: > > >>> ? ? ? ? ?print s[:len] > > >>> ? ? ? ? ?s=s[len:] > > > > > > You might still need to tweak the above code as regards how line endings > > > are handled. > > > You might also want to tweak it if the strings are _really_ long to > > simply slice out the substrings as opposed to reassigning the balance to > > a newly created s on each iteration. > > > Emile > > Thanks for all of the help. ?I'm almost there. ?I have it working now, > but the 'fold' piece is very slow. ?When I use the 'fold' command in > shell it is almost instantaneous. ?I was able to do the EBCDIC->ASCII > conversion usng the decode method in the built-in str type. ?I didn't > have to import the codecs module. ?I just decoded the data to cp037 > which works fine. > > So now, I'm left with a large file, consisting of one extremely long > line of ASCII data that needs to be sliced up into 35 character > lines. ?I did the following, which works but takes a very long time: > > f = open(ascii_file, 'w') > while ascii_data: > ? ? f.write(ascii_data[:len]) > ? ? ascii_data = ascii_data[len:] > f.close() > > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. ?So, I'm going to give that a try... I'm > a bit confused by what that means, am guessing that slice can break up > a string based on characters; will research. ?Thanks for the help thus > far. ?I'll post again when all is working fine. Assuming your rather large text file is 1 meg long, you have 1 million characters in there. 1000000/35 = ~29k lines. The size remaining string decreases linearly, so the average size is (1000000 + 0) / 2 or 500k. All said and done, you're allocating and copying a 500K string -- not once, but 29 thousand times. That's where your slowdown resides. From tn.pablo at gmail.com Thu Jul 16 12:02:57 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 16 Jul 2009 11:02:57 -0500 Subject: How to check if any item from a list of strings is in a big string? In-Reply-To: References: Message-ID: On Thu, Jul 9, 2009 at 22:07, Steven D'Aprano wrote: > On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > >> def list_items_in_string(list_items, string): >> ? ? for item in list_items: >> ? ? ? ? if item in string: >> ? ? ? ? ? ? return True >> ? ? return False > ... >> Any ideas how to make that function look nicer? :) > > Change the names. Reverse the order of the arguments. Add a docstring. > Why reverse the order of the arguments? Is there a design principle there? I always make a mess out of the order of my arguments... -- Pablo Torres N. From degibb at gmail.com Thu Jul 16 12:15:46 2009 From: degibb at gmail.com (David Gibb) Date: Thu, 16 Jul 2009 12:15:46 -0400 Subject: list of all possible values In-Reply-To: <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> Message-ID: > Certainly possible with list comprehensions. > >>>> a = "abc" >>>> [(x, y) for x in a for y in a] > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), > ('c', 'a'), ('c', 'b'), ('c', 'c')] > > But I like bearophile's version better. > Andreas, Thanks, but I think you were missing my point. I should have explained better. The advantage that bearophile's version is generic with respect to the number of elements in each combination. To go from 2 element pairs (e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e')) requires only a change in a parameter passed to itertools. I don't see how you would do that with list comprehensions. You're example works nicely with 2 element pairs, but it seems to me like you'd need to recode it if you wanted to change it to 5 element pairs. Am I wrong about that? Can you think of a way to write a function that, using list comprehensions, takes a list of values and the size of each combination, and returns the len(list)**(combination size) possible combinations using those values? Thanks again, David From rylesny at gmail.com Thu Jul 16 12:23:25 2009 From: rylesny at gmail.com (ryles) Date: Thu, 16 Jul 2009 09:23:25 -0700 (PDT) Subject: Python Equivalent for dd & fold References: Message-ID: On Jul 15, 1:14?pm, seldan24 wrote: > On Jul 15, 12:47?pm, Michiel Overtoom wrote: > > > > > seldan24 wrote: > > > what can I use as the equivalent for the Unix 'fold' command? > > > def fold(s,len): > > ? ? ?while s: > > ? ? ? ? ?print s[:len] > > ? ? ? ? ?s=s[len:] > > > s="A very long string indeed. Really that long? Indeed." > > fold(s,10) > > > Output: > > > A very lon > > g string i > > ndeed. Rea > > lly that l > > ong? Indee > > d. > > > Greetings, > > > -- > > "The ability of the OSS process to collect and harness > > the collective IQ of thousands of individuals across > > the Internet is simply amazing." - Vinod Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html > > Wow, I feel like a dork. ?I should have done more research prior to > posting. ?Anyway, thanks for the advice. ?The trouble with Python is > that things make 'too much' sense. ?Loving this language. You might also find the textwrap module useful: http://docs.python.org/library/textwrap.html From emile at fenx.com Thu Jul 16 12:23:43 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 09:23:43 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: On 7/16/2009 7:04 AM Unknown said... > On 2009-07-16, Emile van Sebille wrote: >> daysInAdvance = int(inputVar) or 25 > > I don't get it. That doesn't work right when inputVar == "0". > Aah, but you didn't get to define right. :) For that particular example 0 is not a valid response. Emile From jeanmichel at sequans.com Thu Jul 16 12:37:45 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 16 Jul 2009 18:37:45 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: <4A5F5759.4010600@sequans.com> Emile van Sebille wrote: > On 7/16/2009 7:04 AM Unknown said... >> On 2009-07-16, Emile van Sebille wrote: >>> daysInAdvance = int(inputVar) or 25 >> >> I don't get it. That doesn't work right when inputVar == "0". >> > Aah, but you didn't get to define right. :) For that particular > example 0 is not a valid response. > > Emile > When I was talking about such error prone form of boolean operations, I didn't expect to be right so quickly :p Steven explained the truth notion with the Something/Nothing. "0" is Something, 0 would be Nothing. I'm not sure it makes sens anyway. I mean, I could easily argue that the number 0 is something. In the end I wonder if I shouldn't just acknowledge the python mechanism without trying to find any intuitive/natural/obvious logic in it, knowing that sometimes the Truth lies far away from the Evidence. JM From andreas.tawn at ubisoft.com Thu Jul 16 12:49:11 2009 From: andreas.tawn at ubisoft.com (Andreas Tawn) Date: Thu, 16 Jul 2009 18:49:11 +0200 Subject: list of all possible values In-Reply-To: References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> Message-ID: <8AEDA5E3386EA742B8C24C95FF0C7580079FCD7C@PDC-MAIL3.ubisoft.org> > > Certainly possible with list comprehensions. > > > >>>> a = "abc" > >>>> [(x, y) for x in a for y in a] > > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), > > ('c', 'a'), ('c', 'b'), ('c', 'c')] > > > > But I like bearophile's version better. > > > > Andreas, > > Thanks, but I think you were missing my point. I should have explained better. > > The advantage that bearophile's version is generic with respect to the > number of elements in each combination. To go from 2 element pairs > (e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e')) > requires only a change in a parameter passed to itertools. > > I don't see how you would do that with list comprehensions. You're > example works nicely with 2 element pairs, but it seems to me like > you'd need to recode it if you wanted to change it to 5 element pairs. > > Am I wrong about that? Can you think of a way to write a function > that, using list comprehensions, takes a list of values and the size > of each combination, and returns the len(list)**(combination size) > possible combinations using those values? > > Thanks again, > David David, I think my post got caught in the nebulous email eddys and seems to have taken 16 hours to arrive on the list. It was meant to be a reply to your first post, not your second. Having said that, I think I missed the point of that post too ;o) Maybe someone smarter than me can come up with a way to dynamically nest the fors in a list comprehension, but I certainly can't do it. Sorry for the confusion. Cheers, Drea From python at mrabarnett.plus.com Thu Jul 16 13:06:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 16 Jul 2009 18:06:09 +0100 Subject: Python Equivalent for dd & fold In-Reply-To: <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <4A5F5E01.1060505@mrabarnett.plus.com> seldan24 wrote: > On Jul 15, 1:48 pm, Emile van Sebille wrote: >> On 7/15/2009 10:23 AM MRAB said... >> >>>> On Jul 15, 12:47 pm, Michiel Overtoom wrote: >>>>> seldan24 wrote: >>>>>> what can I use as the equivalent for the Unix 'fold' command? >>>>> def fold(s,len): >>>>> while s: >>>>> print s[:len] >>>>> s=s[len:] >> >>> You might still need to tweak the above code as regards how line endings >>> are handled. >> You might also want to tweak it if the strings are _really_ long to >> simply slice out the substrings as opposed to reassigning the balance to >> a newly created s on each iteration. >> >> Emile > > Thanks for all of the help. I'm almost there. I have it working now, > but the 'fold' piece is very slow. When I use the 'fold' command in > shell it is almost instantaneous. I was able to do the EBCDIC->ASCII > conversion usng the decode method in the built-in str type. I didn't > have to import the codecs module. I just decoded the data to cp037 > which works fine. > > So now, I'm left with a large file, consisting of one extremely long > line of ASCII data that needs to be sliced up into 35 character > lines. I did the following, which works but takes a very long time: > > f = open(ascii_file, 'w') > while ascii_data: > f.write(ascii_data[:len]) > ascii_data = ascii_data[len:] > f.close() > The 'write' method doesn't append any line ending, so that code gives the same output as f.write(ascii_data). > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. So, I'm going to give that a try... I'm > a bit confused by what that means, am guessing that slice can break up > a string based on characters; will research. Thanks for the help thus > far. I'll post again when all is working fine. From python at mrabarnett.plus.com Thu Jul 16 13:07:54 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 16 Jul 2009 18:07:54 +0100 Subject: Python Equivalent for dd & fold In-Reply-To: <4A5F4881.9090501@xs4all.nl> References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> <4A5F4881.9090501@xs4all.nl> Message-ID: <4A5F5E6A.60104@mrabarnett.plus.com> Michiel Overtoom wrote: > seldan24 wrote: > >> I know that Emile suggested that I can slice out the substrings rather >> than do the gradual trimming of the string variable as is being done >> by moving around the length. > > An excellent idea. > > def fold(s,chunklength): > offset=0 > while offset print s[offset:offset+chunklength] > offset+=chunklength > More Pythonic: for offset in range(0, len(s), chunklength): print s[offset : offset + chunklength] > s="A very long string indeed. Really that long? Indeed." > fold(s,10) > From piet at cs.uu.nl Thu Jul 16 13:18:04 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 19:18:04 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: >>>>> mheavner (m) wrote: >m> 'The process' refers to the subprocess. I could do as you say, load >m> the data structure each time, but the problem is that takes a >m> considerable amount of time compared to the the actual computation >m> with the data it contains. I'm using these processes within a loop as >m> follows: >m> # Don't recreate processes or Queues >m> pop1 = Queue() >m> pop2 = Queue() >m> pop_out = Queue() >m> p1 = CudaProcess(0, args=(costf,pop1,pop_out)) >m> p2 = CudaProcess(1, args=(costf,pop2,pop_out)) >m> # Main loop >m> for i in range(maxiter): >m> print 'ITERATION: '+str(i) >m> if log != None: >m> l = open(log,'a') >m> l.write('Iteration: '+str(i)+'\n') >m> l.close() >m> # Split population in two >m> pop1.putmany(pop[0:len(pop)/2]) >m> pop2.putmany(pop[len(pop)/2:len(pop)]) >m> # Start two processes >m> if not p1.isAlive(): >m> p1.start() >m> print 'started %s'%str(p1.getPid()) >m> else: >m> p1.run() That won't work. p1.run() will execute the run method in the Master process, not in the subprocess. And if it would your could would have a race condition: between the p1.isAlive() (which must be is_alive btw), and the p1.run() the process can have stopped. The proper way to do is to put the work in a Queue and let the processes get work out of the Queue. The datastructure will remain in the process then. >m> if not p2.isAlive(): >m> p2.start() >m> print 'started %s'%str(p2.getPid()) >m> else: >m> p2.run() >m> . >m> . >m> . >m> So I'd like to load that data into memory once and keep there as long >m> as the process is alive (ideally when the subprocess is created, >m> storing some sort of pointer to it), rather than loading it each time >m> run is called for a process within the loop. Could be my CudaProcess >m> class - I'll check out what Diez suggested and post back. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From miheavner at gmail.com Thu Jul 16 13:37:45 2009 From: miheavner at gmail.com (mheavner) Date: Thu, 16 Jul 2009 10:37:45 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: <2b61ef2d-4bf6-47ba-824d-6b490a342edb@n11g2000yqb.googlegroups.com> I realize that the Queue would be the best way of doing this, however that involves transferring the huge amount of data for each call - my hope was to transfer it once and have it remain in memory for the subprocess across run() calls. On Jul 16, 1:18?pm, Piet van Oostrum wrote: > >>>>> mheavner (m) wrote: > >m> 'The process' refers to the subprocess. I could do as you say, load > >m> the data structure each time, but the problem is that takes a > >m> considerable amount of time compared to the the actual computation > >m> with the data it contains. I'm using these processes within a loop as > >m> follows: > >m> ? ? ? ? ?# Don't recreate processes or Queues > >m> ? ? ? ? ?pop1 = Queue() > >m> ? ? ? ? ?pop2 = Queue() > >m> ? ? ? ? ?pop_out = Queue() > >m> ? ? ? ? ?p1 = CudaProcess(0, args=(costf,pop1,pop_out)) > >m> ? ? ? ? ?p2 = CudaProcess(1, args=(costf,pop2,pop_out)) > >m> ? ? ? ? ?# Main loop > >m> ? ? ? ? ?for i in range(maxiter): > >m> ? ? ? ? ? ? ? ? ?print 'ITERATION: '+str(i) > >m> ? ? ? ? ? ? ? ? ?if log != None: > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?l = open(log,'a') > >m> ? ? ? ? ? ? ? ? ?l.write('Iteration: '+str(i)+'\n') > >m> ? ? ? ? ? ? ? ? ?l.close() > >m> ? ? ? ? ? ? ? ? ?# Split population in two > >m> ? ? ? ? ? ? ? ? ?pop1.putmany(pop[0:len(pop)/2]) > >m> ? ? ? ? ? ? ? ? ?pop2.putmany(pop[len(pop)/2:len(pop)]) > >m> ? ? ? ? ? ? ? ? ?# Start two processes > >m> ? ? ? ? ? ? ? ? ?if not p1.isAlive(): > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p1.start() > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p1.getPid()) > >m> ? ? ? ? ? ? ? ? ?else: > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p1.run() > > That won't work. p1.run() will execute the run method in the Master > process, not in the subprocess. And if it would your could would have a > race condition: between the p1.isAlive() (which must be is_alive btw), and > the p1.run() the process can have stopped. > > The proper way to do is to put the work in a Queue and let the processes > get work out of the Queue. The datastructure will remain in the process > then. > > >m> ? ? ? ? ? ? ? ? ?if not p2.isAlive(): > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p2.start() > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p2.getPid()) > >m> ? ? ? ? ? ? ? ? ?else: > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p2.run() > >m> ? ? ? ? ? ? ? ? ?. > >m> ? ? ? ? ? ? ? ? ?. > >m> ? ? ? ? ? ? ? ? ?. > >m> So I'd like to load that data into memory once and keep there as long > >m> as the process is alive (ideally when the subprocess is created, > >m> storing some sort of pointer to it), rather than loading it each time > >m> run is called for a process within the loop. Could be my CudaProcess > >m> class - I'll check out what Diez suggested and post back. > > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org From inky788 at gmail.com Thu Jul 16 13:47:07 2009 From: inky788 at gmail.com (Inky 788) Date: Thu, 16 Jul 2009 10:47:07 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: <68dbe005-64bc-42f8-99b7-e73c41d0dbd4@r33g2000yqn.googlegroups.com> On Jul 16, 10:18?am, Tim Chase wrote: > > I realize that a small portion of the community likes the tabs. > > They're sold on the tabs. They like the tabs. But tabs are an archaic > > holdover from an era when typewriters had physical tabstops on them. > > However, they are a single logical level of indentation -- I come > down fairly solidly on the "tabs" side of the "tabs vs. spaces" > argument. My bet is that the problem is this: some people like to format their code in ways that don't work well when you're using tabs. For example, they might want to call a function like this (note spaces): some_func(foo=1, bar=2, baz=3) instead of: some_func( foo=1, bar=2, baz=3) The first one requires 10 spaces in front of bar and baz. If you're using tabs, that means one or two tabs plus some space characters. So, people who do that would probably never use tabs. The 2nd example is fine for tabs or spaces. I'm sure there are a bunch of similar examples for things besides function calls. Don't you ever find cases where you'd like to add in an extra space or two to make things line up nicely? >?I can set my editor (vim in this case) to show tabs as > as many spaces as I want. ?I usually have this set to 4, but > sometimes 1 or 2. Just curious: why would you want to do that? In my experience, once my eyes got used to 4-space indents, everything else looks either too little or too much. :) From jkn_gg at nicorp.f9.co.uk Thu Jul 16 13:59:12 2009 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Thu, 16 Jul 2009 10:59:12 -0700 (PDT) Subject: Ann: Google releases Python-based open-source NX server References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> Message-ID: <4086976f-11da-4da2-869a-7720510f2bae@m11g2000yqh.googlegroups.com> On Jul 16, 3:51?pm, J Kenneth King wrote: > It's pretty cool, but not PEP8! Probably because they just bought the > source off of another smaller proprietary project. ?Makes me sad seeing > Google, proud supporter of all things Python, release non-PEP8 code. Personally, I don't follow PEP8 either. Note: PEP-8 gives 'coding conventions for the Python code **comprising the standard library in the main Python distribution**' (my emphasis). My coding conventions are similar to, but not exactly the same as, PEP-8. It would be ridiculous IMO (and PEP-8 acknowledges this) for such a permissive language as Python to put a 'coding style' straitjacket around itself. YMMV, of course. J^n From emile at fenx.com Thu Jul 16 14:27:55 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 11:27:55 -0700 Subject: Ann: Google releases Python-based open-source NX server In-Reply-To: <4086976f-11da-4da2-869a-7720510f2bae@m11g2000yqh.googlegroups.com> References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> <4086976f-11da-4da2-869a-7720510f2bae@m11g2000yqh.googlegroups.com> Message-ID: On 7/16/2009 10:59 AM jkn said... > Personally, I don't follow PEP8 either. Note: PEP-8 gives 'coding > conventions for the Python code **comprising the standard library in > the main Python distribution**' (my emphasis). My coding conventions > are similar to, but not exactly the same as, PEP-8. Mine too, and my objection to PEP-8 is that the coding style doesn't allow for copy-n-paste into the interpreter, and I like to be able to test that way. Perhaps if I configured my editor not to strip trailing whitespace from lines it would work, but then I don't like trailing whitespace. Of course, I _like_ leading whitespace. :) Emile From robert.kern at gmail.com Thu Jul 16 14:29:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 16 Jul 2009 13:29:31 -0500 Subject: Ann: Google releases Python-based open-source NX server In-Reply-To: <85y6qok7ek.fsf@agentultra.com> References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> Message-ID: On 2009-07-16 09:51, J Kenneth King wrote: > jkn writes: > >> Google quietly releases open-source NX server ...written in Python, >> apparently >> >> > Google_quietly_releases_open_source_NX_server?taxonomyId=88> >> >> Neatx can be downloaded from Google's code repository: >> >> >> >> Regards >> J^n > > It's pretty cool, but not PEP8! Probably because they just bought the > source off of another smaller proprietary project. No, it' just that Google does not use PEP8 for its internal style standard. -- 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 python.list at tim.thechases.com Thu Jul 16 14:40:09 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 16 Jul 2009 13:40:09 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <68dbe005-64bc-42f8-99b7-e73c41d0dbd4@r33g2000yqn.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> <68dbe005-64bc-42f8-99b7-e73c41d0dbd4@r33g2000yqn.googlegroups.com> Message-ID: <4A5F7409.8000502@tim.thechases.com> >> However, they are a single logical level of indentation -- I come >> down fairly solidly on the "tabs" side of the "tabs vs. spaces" >> argument. > > My bet is that the problem is this: some people like to format their > code in ways that don't work well when you're using tabs. For example, > they might want to call a function like this (note spaces): > > some_func(foo=1, > bar=2, > baz=3) > > instead of: > > some_func( > foo=1, > bar=2, > baz=3) For continued indenting statements such as this, I tend to use the coding convention used at my first job out of college (Computer Sciences Corp...for better or worse) which just indents two levels: def some_func(foo=1, bar=2, baz=3): do_something(foo) do_other_stuff(bar) > examples for things besides function calls. Don't you ever find cases > where you'd like to add in an extra space or two to make things line > up nicely? I have occasionally (okay, "very rarely") use the "mixed tab+space" method of indentation for continued lines if I want them lined up nicely: if (foo == bar and baz > frob and fred != barney): do_something() do_more() This scheme insures that the visual alignment for the continued-line matches up, even if the tab-stop is changed. The positioning of the indented block (the do_* bit) floats inconveniently with relation to the continued text, with pessimal cases being indistinguishable from the continued lines (which is why I generally opt not to use this unless it has great benefits in code clarity). By regularly indenting continued lines for containing blocks (if, while, etc) by two tabs, the continuation stands out from the contained code regardless of my tab stops. >> I can set my editor (vim in this case) to show tabs as >> as many spaces as I want. I usually have this set to 4, but >> sometimes 1 or 2. > > Just curious: why would you want to do that? In my experience, once my > eyes got used to 4-space indents, everything else looks either too > little or too much. :) It totally depends on the project -- I like the condensed nature of 2sp/tab for code sharing on mailing lists (and tend to copy out of vim with tabs expanded to 2 spaces for pasting into emails) and for my own visual preference. If it's code that I'd expect anybody else to view, I tend to use 4sp/tab to keep my lines below 80 chars per line with the tabs taken into consideration. I guess I change up my indent enough that sometimes 2 seems just right or too small, and sometimes 4 seems just right or too large. -tkc From pavlovevidence at gmail.com Thu Jul 16 14:45:38 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 16 Jul 2009 11:45:38 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: On Jul 16, 6:32?am, Inky 788 wrote: > On Jul 10, 7:35?pm, Ben Finney wrote: > > > walterbyrd writes: > > > I believe Guido himself has said that all indentions should be four > > > spaces - no tabs. > > > Yes. That's a ?should? and not a ?must?, even though PEP 8 says it > > with a simple imperative:: > > > ? ? Use 4 spaces per indentation level. > > That actually sounds pretty weird. "Don't do {foo}. Really, I mean > *really* don't do {foo}. Oh, also, the interpreter allows you to do > {foo}. But don't do it! I mean it!". > > Very ... perlish, if you ask me. Not Perlish at all. (Perl would never want you not to use something.) Carl Banks From tjreedy at udel.edu Thu Jul 16 14:50:41 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 14:50:41 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> Message-ID: Mark wrote: > On 16 July, 10:21, Piet van Oostrum wrote: >> But why should the order be as if the OrderedDict was a list of tuples. >> A dict can be considered as a mapping and then you might want to treat >> either the key or the value as contravariant (the key I guess). So there >> is ambiguity. Why would the view as a list of tuples for the ordering be >> the `natural' view? >> >> Maybe you may expect some kind of monotonicity such that d1> d1[x]> 2:5}. So maybe there is only a partial ordering? > > OK, that seems to me to be a convincing argument against supporting > ordering. To put the above in a slightly different way. OrderedDicts are a recently added niche class that Raymond added to what is mostly his collections module because there are enough few use cases. There was pydev discussion which included the idea, I believe, that they should fundamentally be dicts, not lists. Regardless, the justifying use cases did not include a need to compare OrderedDicts. The small fraction of the few use cases for OrderedDicts that do require comparision can be met by extracting the needed sequences and comparing *them* in the appropriate manner. The 'appropriate manner' is not likely to always be the same. This point may have come up in the discussion, but I would let you check for sure if curious. 'Consistency' is a Python design principle, but it is balanced with others, so that it is not sufficient reason to add nearly useless features. There is a cost to every addition. Terry Jan Reedy From akhilanger at gmail.com Thu Jul 16 14:57:09 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 11:57:09 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> Message-ID: <24522412.post@talk.nabble.com> Hi, Thanks all for the replies. I am working on a cluster of 15 nodes and I have now installed python 3.1 on all of them. I tried installing python2.6 but there was some make error. So, I do not want to give more time in installing 2.4 and rather use 3.1 but for that I need to convert my 2.4 code to 3.1. I used 2to3 tool, and it did make many changes in the 2.4 code, but still there are some indentation errors that I am unable to resolve being new to python. I have attached my python code, can anyone please fix the indentation error in the code. I am using vi editor. --Thanks a lot, Akhil http://www.nabble.com/file/p24522412/temp.py temp.py alex23 wrote: > > On Jul 16, 9:00?pm, akhil1988 wrote: >> I have switched to python 3.1 , but now I am getting some syntax errors >> in >> the code: > > Python 3.x was a major release that endeavoured to clean up a number > of lingering issues with the language, the upshot being that it isn't > entirely backwards compatible with past versions. Unicode became the > default string type, which is what is causing the error here: the u- > prefix is no longer required (or even allowed). > > However, Py3.x _does_ come with a handy tool for automatically > converting Python 2.x code to 3.x, called 2to3. One of the things it > should do is convert Py2.x unicode values into their correct > representation in 3.x. > > With any luck, it should be able to convert the code you're using > entirely. Let us know how it goes. > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24522412.html Sent from the Python - python-list mailing list archive at Nabble.com. From 1x7y2z9 at gmail.com Thu Jul 16 15:02:52 2009 From: 1x7y2z9 at gmail.com (1x7y2z9) Date: Thu, 16 Jul 2009 12:02:52 -0700 (PDT) Subject: URLError errno and strerror not set Message-ID: <5bbbf0b7-743d-48bc-bd0d-67a6626a58cd@z4g2000prh.googlegroups.com> python 2.5.2 errno, strerror and message do not appear to be set in the following two cases (at least). Is this to be expected? (as an aside, arg[0] is set) # case 1 > print exception, exception.errno, exception.strerror, exception.message == '' None None True # case 2 > print exception, exception.errno, exception.strerror, exception.message == '' None None True Thanks. From piet at cs.uu.nl Thu Jul 16 15:10:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 21:10:08 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> <2b61ef2d-4bf6-47ba-824d-6b490a342edb@n11g2000yqb.googlegroups.com> Message-ID: >>>>> mheavner (m) wrote: >m> I realize that the Queue would be the best way of doing this, however >m> that involves transferring the huge amount of data for each call - my >m> hope was to transfer it once and have it remain in memory for the >m> subprocess across run() calls. Which huge amount of data? The datastructure you talked about can remain in the process. You only have to transfer the input for your calculation in the queue but you have to do that anyway. And there is only one run call per process. When run has terminated the process exits, so you would have a loop in the run(0 method getting work from the queue. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Thu Jul 16 15:26:02 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 12:26:02 -0700 Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> Message-ID: In article <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4 at m11g2000yqh.googlegroups.com>, hartley wrote: > >If you had loosened up on the sarcasm I would probably have read what >you wrote more thoroughly instead of just skimming through it. Thanks >for the help, but you should seriously consider doing something with >your patronizing attitude. http://www.mikeash.com/getting_answers.html http://www.catb.org/~esr/faqs/smart-questions.html -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From james at agentultra.com Thu Jul 16 15:36:11 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 16 Jul 2009 15:36:11 -0400 Subject: Ann: Google releases Python-based open-source NX server References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> Message-ID: <85r5wgju8k.fsf@agentultra.com> Robert Kern writes: > On 2009-07-16 09:51, J Kenneth King wrote: >> jkn writes: >> >>> Google quietly releases open-source NX server ...written in Python, >>> apparently >>> >>> >> Google_quietly_releases_open_source_NX_server?taxonomyId=88> >>> >>> Neatx can be downloaded from Google's code repository: >>> >>> >>> >>> Regards >>> J^n >> >> It's pretty cool, but not PEP8! Probably because they just bought the >> source off of another smaller proprietary project. > > No, it' just that Google does not use PEP8 for its internal style standard. Yeah probably. I'm just a little OCD sometimes. From akhilanger at gmail.com Thu Jul 16 15:41:40 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 12:41:40 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24522412.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24523113.post@talk.nabble.com> ok! I got the indentation errors fixed. Bu I get another error: Traceback (most recent call last): File "./temp.py", line 484, in main() File "./temp.py", line 476, in main line.decode('utf-8').strip() AttributeError: 'str' object has no attribute 'decode' I am using Python3.1 Thanks Akhil akhil1988 wrote: > > Hi, > > Thanks all for the replies. > > I am working on a cluster of 15 nodes and I have now installed python 3.1 > on all of them. I tried installing python2.6 but there was some make > error. So, I do not want to give more time in installing 2.4 and rather > use 3.1 but for that I need to convert my 2.4 code to 3.1. > > I used 2to3 tool, and it did make many changes in the 2.4 code, but still > there are some indentation errors that I am unable to resolve being new to > python. I have attached my python code, can anyone please fix the > indentation error in the code. I am using vi editor. > > --Thanks a lot, > Akhil http://www.nabble.com/file/p24522412/temp.py temp.py > > > alex23 wrote: >> >> On Jul 16, 9:00?pm, akhil1988 wrote: >>> I have switched to python 3.1 , but now I am getting some syntax errors >>> in >>> the code: >> >> Python 3.x was a major release that endeavoured to clean up a number >> of lingering issues with the language, the upshot being that it isn't >> entirely backwards compatible with past versions. Unicode became the >> default string type, which is what is causing the error here: the u- >> prefix is no longer required (or even allowed). >> >> However, Py3.x _does_ come with a handy tool for automatically >> converting Python 2.x code to 3.x, called 2to3. One of the things it >> should do is convert Py2.x unicode values into their correct >> representation in 3.x. >> >> With any luck, it should be able to convert the code you're using >> entirely. Let us know how it goes. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24523113.html Sent from the Python - python-list mailing list archive at Nabble.com. From tonylay at gmail.com Thu Jul 16 15:45:27 2009 From: tonylay at gmail.com (Tony Lay) Date: Thu, 16 Jul 2009 12:45:27 -0700 (PDT) Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: On Jul 16, 10:14?am, a... at pythoncraft.com (Aahz) wrote: > In article <45228cf4-0b37-4c52-bf6f-d7bd124b0... at l32g2000vbp.googlegroups.com>, > Tony ?Lay ? wrote: > > > > >Traceback (most recent call last): > > ?File "/usr/local/bin/meld", line 35, in > > ? ?import gettext > > ?File "/usr/local/lib/python2.6/gettext.py", line 49, in > > ? ?import locale, copy, os, re, struct, sys > > ?File "/usr/local/lib/python2.6/locale.py", line 15, in > > ? ?import functools > > ?File "/usr/local/lib/python2.6/functools.py", line 10, in > > ? ?from _functools import partial, reduce > >ImportError: No module named _functools > > Where is _functools.so? > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "If you think it's expensive to hire a professional to do the job, wait > until you hire an amateur." ?--Red Adair /usr/local/lib/python2.6/lib-dynload/ It's in the path... export PYTHONHOME=/usr/local/lib/python2.6 export PYTHONPATH=/usr/local/lib/python2.6:/usr/local/lib/python2.6/ site-packages:/usr/local/lib/python2.6/lib-dynload:/usr/local/etc/ pango From tonylay at gmail.com Thu Jul 16 15:56:31 2009 From: tonylay at gmail.com (Tony Lay) Date: Thu, 16 Jul 2009 12:56:31 -0700 (PDT) Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: On Jul 16, 10:14?am, a... at pythoncraft.com (Aahz) wrote: > In article <45228cf4-0b37-4c52-bf6f-d7bd124b0... at l32g2000vbp.googlegroups.com>, > Tony ?Lay ? wrote: > > > > >Traceback (most recent call last): > > ?File "/usr/local/bin/meld", line 35, in > > ? ?import gettext > > ?File "/usr/local/lib/python2.6/gettext.py", line 49, in > > ? ?import locale, copy, os, re, struct, sys > > ?File "/usr/local/lib/python2.6/locale.py", line 15, in > > ? ?import functools > > ?File "/usr/local/lib/python2.6/functools.py", line 10, in > > ? ?from _functools import partial, reduce > >ImportError: No module named _functools > > Where is _functools.so? > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "If you think it's expensive to hire a professional to do the job, wait > until you hire an amateur." ?--Red Adair /usr/local/lib/python2.6/lib-dynload/ It's in the path... export PYTHONHOME=/usr/local/lib/python2.6 export PYTHONPATH=/usr/local/lib/python2.6:/usr/local/lib/python2.6/ site-packages:/usr/local/lib/python2.6/lib-dynload:/usr/local/etc/ pango From kyrie at uh.cu Thu Jul 16 15:57:02 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Thu, 16 Jul 2009 15:57:02 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5F5759.4010600@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <4A5F5759.4010600@sequans.com> Message-ID: <1247774222.4a5f860ec3cfa@mail.uh.cu> Quoting Jean-Michel Pichavant : > Emile van Sebille wrote: > > On 7/16/2009 7:04 AM Unknown said... > >> On 2009-07-16, Emile van Sebille wrote: > >>> daysInAdvance = int(inputVar) or 25 > >> > >> I don't get it. That doesn't work right when inputVar == "0". > >> > > Aah, but you didn't get to define right. :) For that particular > > example 0 is not a valid response. > > When I was talking about such error prone form of boolean operations, I > didn't expect to be right so quickly :p What do you mean by being "right so quickly", and "error prone" in this context? I would also ask "Unknown" why he believes that "int(intputVar) or 25" doesn't work right when inputVar == "0". The only false value that int() may return is zero, so the "or 25" clause is there only for that case. I can't see then how you think that is an error. > I'm not sure it makes sens anyway. I > mean, I could easily argue that the number 0 is something. In the end I > wonder if I shouldn't just acknowledge the python mechanism Then look it another way. The "Empty/Nothing" is just standard practice, there is nothing in python that forces you to be "false" if you are empty, or true otherwise. Instead, answer this: why do you need a /boolean/ value? Is there any case where you need to be certain that the object's type is 'bool'? If you think the answer is "yes", you may want to get more familiar with the "duck typing" concept. (That doesn't mean that there are no legitimate cases where duck typing is inappropriate, but that there are many cases where people, specially if they come from statically typed languages, may believe that it is inappropriate when it isn't). In the python world, one should care more about how an object /behaves/ than from what clase it came. If something quacks like a duck, then assume that it is a duck, at least for the quacking part. Most python objects carry a truth value. Sometimes it feels natural (None is "false", boolean True and False are "true" and "false", empty containers are expected to be false, 0 and '' are "false"). Sometimes, it is everything but natural, but that's a problem with the object's implementation (datetime.time comes to mind). So, you shouldn't care if you have a bool instance - it should be enough that it behaves like a bool (though, if you need a bool, you can always construct one). The "True or False" expression could return Giraffes, as long as Giraffes behave like the bool "True" in boolean context. If you care about the class of the result, you can ask for its truth value, and if you don't care about it, you can just ignore it, and use it as you would use a bool. And then, if you can return any object as long as it behaves properly, what would be better to return? A new bool? Why not new Giraffe, if they will have the same behaviour? Guido chose to return the a value that will say more about the result of the operation than just a boolean. It acts as a boolean - if you don't need anything else, treat it as such -, but it will be, whenever is possible, one of the objects in the sequence, in case you need more info. > without > trying to find any intuitive/natural/obvious logic in it, knowing that > sometimes the Truth lies far away from the Evidence. Don't do that. Many of python's decisions are very well thought. You may disagree with them, as I do with some, but they are rarely taken lightly. And this is one that I find very agreeable and in line with the rest of python's philosophy. -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From piet at cs.uu.nl Thu Jul 16 16:01:22 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 22:01:22 +0200 Subject: This is a mess... References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: >>>>> Nick (N) wrote: >N> this is the new oop version, its pretty messy currently, and i do >N> understand it is a simple routine, but i'm using it as an exercise to >N> learn oop python... >N> first the (current) traceback: >N> [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test >N> >N> >N> Traceback (most recent call last): >N> File "oop_covariance.py", line 24, in >N> cov = set1.covariance(set2, Eigen_vect.dot) >N> File "/home/nleioatts/python/Eigen.py", line 66, in covariance >N> print self.vectors[i][i] >N> AttributeError: Eigen_vect instance has no attribute '__getitem__' self.vectors is a list of Eigen_vect objects. So self.vectors[i] is an Eigen_vect object. Now you do a subscript on this Eigen_vect object but you have not defined what that means. Presumably you want it to do the subscript on the self.e_vect in the Eigen_vect instance. Therefore you have to define the __getitem__ method in the Eigen_vect class. class Eigen_vect: def __init__(self, e_val, e_vect): self.e_val = e_val self.e_vect = e_vect def length(self): return len(self.e_vect) def __getitem__(self, indx): return self.e_vect[indx] I hope I did not make a mistake, I didn't check it because I don't want to make test input files. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nobody at nowhere.com Thu Jul 16 16:17:05 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 21:17:05 +0100 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: On Thu, 16 Jul 2009 09:18:47 -0500, Tim Chase wrote: > Yes, the dictatorial "a tab always equals 8 spaces" Saying "always" is incorrect; it is more accurate to say that tab stops are every 8 columns unless proven otherwise, with the burden of proof falling on whoever wants to use something different. From mensanator at aol.com Thu Jul 16 16:23:38 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 16 Jul 2009 13:23:38 -0700 (PDT) Subject: list of all possible values References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> Message-ID: On Jul 16, 11:49 am, "Andreas Tawn" wrote: > > > Certainly possible with list comprehensions. > > > >>>> a = "abc" > > >>>> [(x, y) for x in a for y in a] > > > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', > 'c'), > > > ('c', 'a'), ('c', 'b'), ('c', 'c')] > > > > But I like bearophile's version better. > > > Andreas, > > > Thanks, but I think you were missing my point. I should have explained > better. > > > The advantage that bearophile's version is generic with respect to the > > number of elements in each combination. To go from 2 element pairs > > (e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e')) > > requires only a change in a parameter passed to itertools. > > > I don't see how you would do that with list comprehensions. You're > > example works nicely with 2 element pairs, but it seems to me like > > you'd need to recode it if you wanted to change it to 5 element pairs. > > > Am I wrong about that? Can you think of a way to write a function > > that, using list comprehensions, takes a list of values and the size > > of each combination, and returns the len(list)**(combination size) > > possible combinations using those values? > > > Thanks again, > > David > > David, > > I think my post got caught in the nebulous email eddys and seems to have > taken 16 hours to arrive on the list. It was meant to be a reply to your > first post, not your second. > > Having said that, I think I missed the point of that post too ;o) > > Maybe someone smarter than me can come up with a way to dynamically nest > the fors in a list comprehension, but I certainly can't do it. Well, I don't use list comprehension, but you can certainly make dynamic for loops. Not that this is a replacement for itertools (I wrote it before itertools had the ability to do all this). def ooloop6(a, n, perm=True, repl=True): if (not repl) and (n>len(a)): return r0 = range(n) r1 = r0[1:] if perm and repl: # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) e = ''.join(["p = [''.join((",v,")) ",f,"]"]) exec e return p if (not perm) and repl: # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) i = ' and '.join(['(c%s>=c%s)' % (j,j-1) for j in r1]) e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"]) exec e return p if perm and (not repl): # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) i = ' and '.join([' and '.join(['(c%s!=c%s)' % (j,k) for k in range(j)]) for j in r1]) e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"]) exec e return p if (not perm) and (not repl): # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) i = ' and '.join(['(c%s>c%s)' % (j,j-1) for j in r1]) e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"]) exec e return p print '0123456789 taken 2 at a time' p = ooloop6('0123456789',2,True,True) print print 'Permutations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',2,True,False) print print 'Permutations without Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',2,False,True) print print 'Combinations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',2,False,False) print print 'Combinations without Replacement: %6d' % (len(p)) print p print print '0123456789 taken 3 at a time' p = ooloop6('0123456789',3,True,True) print print 'Permutations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',3,True,False) print print 'Permutations without Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',3,False,True) print print 'Combinations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',3,False,False) print print 'Combinations without Replacement: %6d' % (len(p)) print p 0123456789 taken 2 at a time Permutations with Replacement: 100 ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99'] Permutations without Replacement: 90 ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98'] Combinations with Replacement: 55 ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '11', '12', '13', '14', '15', '16', '17', '18', '19', '22', '23', '24', '25', '26', '27', '28', '29', '33', '34', '35', '36', '37', '38', '39', '44', '45', '46', '47', '48', '49', '55', '56', '57', '58', '59', '66', '67', '68', '69', '77', '78', '79', '88', '89', '99'] Combinations without Replacement: 45 ['01', '02', '03', '04', '05', '06', '07', '08', '09', '12', '13', '14', '15', '16', '17', '18', '19', '23', '24', '25', '26', '27', '28', '29', '34', '35', '36', '37', '38', '39', '45', '46', '47', '48', '49', '56', '57', '58', '59', '67', '68', '69', '78', '79', '89'] 0123456789 taken 3 at a time Permutations with Replacement: 1000 ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012', '013', '014', '015', '016', '017', '018', '019', '020', '021', '022', '023', '024', '025', '026', '027', '028', '029', '030', '031', '032', '033', '034', '035', '036', '037', '038', '039', '040', '041', '042', '043', '044', '045', '046', '047', '048', '049', '050', '051', '052', '053', '054', '055', '056', '057', '058', '059', '060', '061', '062', '063', '064', '065', '066', '067', '068', '069', '070', '071', '072', '073', '074', '075', '076', '077', '078', '079', '080', '081', '082', '083', '084', '085', '086', '087', '088', '089', '090', '091', '092', '093', '094', '095', '096', '097', '098', '099', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '220', '221', '222', '223', '224', '225', '226', '227', '228', '229', '230', '231', '232', '233', '234', '235', '236', '237', '238', '239', '240', '241', '242', '243', '244', '245', '246', '247', '248', '249', '250', '251', '252', '253', '254', '255', '256', '257', '258', '259', '260', '261', '262', '263', '264', '265', '266', '267', '268', '269', '270', '271', '272', '273', '274', '275', '276', '277', '278', '279', '280', '281', '282', '283', '284', '285', '286', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '302', '303', '304', '305', '306', '307', '308', '309', '310', '311', '312', '313', '314', '315', '316', '317', '318', '319', '320', '321', '322', '323', '324', '325', '326', '327', '328', '329', '330', '331', '332', '333', '334', '335', '336', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '357', '358', '359', '360', '361', '362', '363', '364', '365', '366', '367', '368', '369', '370', '371', '372', '373', '374', '375', '376', '377', '378', '379', '380', '381', '382', '383', '384', '385', '386', '387', '388', '389', '390', '391', '392', '393', '394', '395', '396', '397', '398', '399', '400', '401', '402', '403', '404', '405', '406', '407', '408', '409', '410', '411', '412', '413', '414', '415', '416', '417', '418', '419', '420', '421', '422', '423', '424', '425', '426', '427', '428', '429', '430', '431', '432', '433', '434', '435', '436', '437', '438', '439', '440', '441', '442', '443', '444', '445', '446', '447', '448', '449', '450', '451', '452', '453', '454', '455', '456', '457', '458', '459', '460', '461', '462', '463', '464', '465', '466', '467', '468', '469', '470', '471', '472', '473', '474', '475', '476', '477', '478', '479', '480', '481', '482', '483', '484', '485', '486', '487', '488', '489', '490', '491', '492', '493', '494', '495', '496', '497', '498', '499', '500', '501', '502', '503', '504', '505', '506', '507', '508', '509', '510', '511', '512', '513', '514', '515', '516', '517', '518', '519', '520', '521', '522', '523', '524', '525', '526', '527', '528', '529', '530', '531', '532', '533', '534', '535', '536', '537', '538', '539', '540', '541', '542', '543', '544', '545', '546', '547', '548', '549', '550', '551', '552', '553', '554', '555', '556', '557', '558', '559', '560', '561', '562', '563', '564', '565', '566', '567', '568', '569', '570', '571', '572', '573', '574', '575', '576', '577', '578', '579', '580', '581', '582', '583', '584', '585', '586', '587', '588', '589', '590', '591', '592', '593', '594', '595', '596', '597', '598', '599', '600', '601', '602', '603', '604', '605', '606', '607', '608', '609', '610', '611', '612', '613', '614', '615', '616', '617', '618', '619', '620', '621', '622', '623', '624', '625', '626', '627', '628', '629', '630', '631', '632', '633', '634', '635', '636', '637', '638', '639', '640', '641', '642', '643', '644', '645', '646', '647', '648', '649', '650', '651', '652', '653', '654', '655', '656', '657', '658', '659', '660', '661', '662', '663', '664', '665', '666', '667', '668', '669', '670', '671', '672', '673', '674', '675', '676', '677', '678', '679', '680', '681', '682', '683', '684', '685', '686', '687', '688', '689', '690', '691', '692', '693', '694', '695', '696', '697', '698', '699', '700', '701', '702', '703', '704', '705', '706', '707', '708', '709', '710', '711', '712', '713', '714', '715', '716', '717', '718', '719', '720', '721', '722', '723', '724', '725', '726', '727', '728', '729', '730', '731', '732', '733', '734', '735', '736', '737', '738', '739', '740', '741', '742', '743', '744', '745', '746', '747', '748', '749', '750', '751', '752', '753', '754', '755', '756', '757', '758', '759', '760', '761', '762', '763', '764', '765', '766', '767', '768', '769', '770', '771', '772', '773', '774', '775', '776', '777', '778', '779', '780', '781', '782', '783', '784', '785', '786', '787', '788', '789', '790', '791', '792', '793', '794', '795', '796', '797', '798', '799', '800', '801', '802', '803', '804', '805', '806', '807', '808', '809', '810', '811', '812', '813', '814', '815', '816', '817', '818', '819', '820', '821', '822', '823', '824', '825', '826', '827', '828', '829', '830', '831', '832', '833', '834', '835', '836', '837', '838', '839', '840', '841', '842', '843', '844', '845', '846', '847', '848', '849', '850', '851', '852', '853', '854', '855', '856', '857', '858', '859', '860', '861', '862', '863', '864', '865', '866', '867', '868', '869', '870', '871', '872', '873', '874', '875', '876', '877', '878', '879', '880', '881', '882', '883', '884', '885', '886', '887', '888', '889', '890', '891', '892', '893', '894', '895', '896', '897', '898', '899', '900', '901', '902', '903', '904', '905', '906', '907', '908', '909', '910', '911', '912', '913', '914', '915', '916', '917', '918', '919', '920', '921', '922', '923', '924', '925', '926', '927', '928', '929', '930', '931', '932', '933', '934', '935', '936', '937', '938', '939', '940', '941', '942', '943', '944', '945', '946', '947', '948', '949', '950', '951', '952', '953', '954', '955', '956', '957', '958', '959', '960', '961', '962', '963', '964', '965', '966', '967', '968', '969', '970', '971', '972', '973', '974', '975', '976', '977', '978', '979', '980', '981', '982', '983', '984', '985', '986', '987', '988', '989', '990', '991', '992', '993', '994', '995', '996', '997', '998', '999'] Permutations without Replacement: 720 ['012', '013', '014', '015', '016', '017', '018', '019', '021', '023', '024', '025', '026', '027', '028', '029', '031', '032', '034', '035', '036', '037', '038', '039', '041', '042', '043', '045', '046', '047', '048', '049', '051', '052', '053', '054', '056', '057', '058', '059', '061', '062', '063', '064', '065', '067', '068', '069', '071', '072', '073', '074', '075', '076', '078', '079', '081', '082', '083', '084', '085', '086', '087', '089', '091', '092', '093', '094', '095', '096', '097', '098', '102', '103', '104', '105', '106', '107', '108', '109', '120', '123', '124', '125', '126', '127', '128', '129', '130', '132', '134', '135', '136', '137', '138', '139', '140', '142', '143', '145', '146', '147', '148', '149', '150', '152', '153', '154', '156', '157', '158', '159', '160', '162', '163', '164', '165', '167', '168', '169', '170', '172', '173', '174', '175', '176', '178', '179', '180', '182', '183', '184', '185', '186', '187', '189', '190', '192', '193', '194', '195', '196', '197', '198', '201', '203', '204', '205', '206', '207', '208', '209', '210', '213', '214', '215', '216', '217', '218', '219', '230', '231', '234', '235', '236', '237', '238', '239', '240', '241', '243', '245', '246', '247', '248', '249', '250', '251', '253', '254', '256', '257', '258', '259', '260', '261', '263', '264', '265', '267', '268', '269', '270', '271', '273', '274', '275', '276', '278', '279', '280', '281', '283', '284', '285', '286', '287', '289', '290', '291', '293', '294', '295', '296', '297', '298', '301', '302', '304', '305', '306', '307', '308', '309', '310', '312', '314', '315', '316', '317', '318', '319', '320', '321', '324', '325', '326', '327', '328', '329', '340', '341', '342', '345', '346', '347', '348', '349', '350', '351', '352', '354', '356', '357', '358', '359', '360', '361', '362', '364', '365', '367', '368', '369', '370', '371', '372', '374', '375', '376', '378', '379', '380', '381', '382', '384', '385', '386', '387', '389', '390', '391', '392', '394', '395', '396', '397', '398', '401', '402', '403', '405', '406', '407', '408', '409', '410', '412', '413', '415', '416', '417', '418', '419', '420', '421', '423', '425', '426', '427', '428', '429', '430', '431', '432', '435', '436', '437', '438', '439', '450', '451', '452', '453', '456', '457', '458', '459', '460', '461', '462', '463', '465', '467', '468', '469', '470', '471', '472', '473', '475', '476', '478', '479', '480', '481', '482', '483', '485', '486', '487', '489', '490', '491', '492', '493', '495', '496', '497', '498', '501', '502', '503', '504', '506', '507', '508', '509', '510', '512', '513', '514', '516', '517', '518', '519', '520', '521', '523', '524', '526', '527', '528', '529', '530', '531', '532', '534', '536', '537', '538', '539', '540', '541', '542', '543', '546', '547', '548', '549', '560', '561', '562', '563', '564', '567', '568', '569', '570', '571', '572', '573', '574', '576', '578', '579', '580', '581', '582', '583', '584', '586', '587', '589', '590', '591', '592', '593', '594', '596', '597', '598', '601', '602', '603', '604', '605', '607', '608', '609', '610', '612', '613', '614', '615', '617', '618', '619', '620', '621', '623', '624', '625', '627', '628', '629', '630', '631', '632', '634', '635', '637', '638', '639', '640', '641', '642', '643', '645', '647', '648', '649', '650', '651', '652', '653', '654', '657', '658', '659', '670', '671', '672', '673', '674', '675', '678', '679', '680', '681', '682', '683', '684', '685', '687', '689', '690', '691', '692', '693', '694', '695', '697', '698', '701', '702', '703', '704', '705', '706', '708', '709', '710', '712', '713', '714', '715', '716', '718', '719', '720', '721', '723', '724', '725', '726', '728', '729', '730', '731', '732', '734', '735', '736', '738', '739', '740', '741', '742', '743', '745', '746', '748', '749', '750', '751', '752', '753', '754', '756', '758', '759', '760', '761', '762', '763', '764', '765', '768', '769', '780', '781', '782', '783', '784', '785', '786', '789', '790', '791', '792', '793', '794', '795', '796', '798', '801', '802', '803', '804', '805', '806', '807', '809', '810', '812', '813', '814', '815', '816', '817', '819', '820', '821', '823', '824', '825', '826', '827', '829', '830', '831', '832', '834', '835', '836', '837', '839', '840', '841', '842', '843', '845', '846', '847', '849', '850', '851', '852', '853', '854', '856', '857', '859', '860', '861', '862', '863', '864', '865', '867', '869', '870', '871', '872', '873', '874', '875', '876', '879', '890', '891', '892', '893', '894', '895', '896', '897', '901', '902', '903', '904', '905', '906', '907', '908', '910', '912', '913', '914', '915', '916', '917', '918', '920', '921', '923', '924', '925', '926', '927', '928', '930', '931', '932', '934', '935', '936', '937', '938', '940', '941', '942', '943', '945', '946', '947', '948', '950', '951', '952', '953', '954', '956', '957', '958', '960', '961', '962', '963', '964', '965', '967', '968', '970', '971', '972', '973', '974', '975', '976', '978', '980', '981', '982', '983', '984', '985', '986', '987'] Combinations with Replacement: 220 ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '011', '012', '013', '014', '015', '016', '017', '018', '019', '022', '023', '024', '025', '026', '027', '028', '029', '033', '034', '035', '036', '037', '038', '039', '044', '045', '046', '047', '048', '049', '055', '056', '057', '058', '059', '066', '067', '068', '069', '077', '078', '079', '088', '089', '099', '111', '112', '113', '114', '115', '116', '117', '118', '119', '122', '123', '124', '125', '126', '127', '128', '129', '133', '134', '135', '136', '137', '138', '139', '144', '145', '146', '147', '148', '149', '155', '156', '157', '158', '159', '166', '167', '168', '169', '177', '178', '179', '188', '189', '199', '222', '223', '224', '225', '226', '227', '228', '229', '233', '234', '235', '236', '237', '238', '239', '244', '245', '246', '247', '248', '249', '255', '256', '257', '258', '259', '266', '267', '268', '269', '277', '278', '279', '288', '289', '299', '333', '334', '335', '336', '337', '338', '339', '344', '345', '346', '347', '348', '349', '355', '356', '357', '358', '359', '366', '367', '368', '369', '377', '378', '379', '388', '389', '399', '444', '445', '446', '447', '448', '449', '455', '456', '457', '458', '459', '466', '467', '468', '469', '477', '478', '479', '488', '489', '499', '555', '556', '557', '558', '559', '566', '567', '568', '569', '577', '578', '579', '588', '589', '599', '666', '667', '668', '669', '677', '678', '679', '688', '689', '699', '777', '778', '779', '788', '789', '799', '888', '889', '899', '999'] Combinations without Replacement: 120 ['012', '013', '014', '015', '016', '017', '018', '019', '023', '024', '025', '026', '027', '028', '029', '034', '035', '036', '037', '038', '039', '045', '046', '047', '048', '049', '056', '057', '058', '059', '067', '068', '069', '078', '079', '089', '123', '124', '125', '126', '127', '128', '129', '134', '135', '136', '137', '138', '139', '145', '146', '147', '148', '149', '156', '157', '158', '159', '167', '168', '169', '178', '179', '189', '234', '235', '236', '237', '238', '239', '245', '246', '247', '248', '249', '256', '257', '258', '259', '267', '268', '269', '278', '279', '289', '345', '346', '347', '348', '349', '356', '357', '358', '359', '367', '368', '369', '378', '379', '389', '456', '457', '458', '459', '467', '468', '469', '478', '479', '489', '567', '568', '569', '578', '579', '589', '678', '679', '689', '789'] >>> > > Sorry for the confusion. > > Cheers, > > Drea From nobody at nowhere.com Thu Jul 16 16:25:50 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 21:25:50 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Thu, 16 Jul 2009 11:06:54 +0200, Jean-Michel Pichavant wrote: >>> So if I resume: >>> - not 'foo' => False >>> - 'foo' or 'foo' => 'foo' >>> >>> I may be missing something, but honestly, Guido must have smoked some >>> heavy stuff to write such logic, has he ? >> >> Several languages (e.g. Lisp, Bourne shell) behave the same way, i.e. "or" >> returns the first element which is considered true while "and" returns the >> last element provided that all preceding elements are considered true. >> >> [snip] >> > > Ok then, why "or" does not return True, if the first element is > considered True ? If the first element is true, returning the first element is returning true. > Why returning the element itself. Any reason for that ? Why not? Where is the benefit in collapsing all true values to True? You can convert values to True/False with bool(), but the conversion cannot be reversed. It only makes a difference if you are interested in the representation rather than the value. Do you normally test for equality with "is" or "=="? From nobody at nowhere.com Thu Jul 16 16:29:07 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 21:29:07 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On Wed, 15 Jul 2009 18:14:10 +0200, Hrvoje Niksic wrote: >> If the question was "Why is there no 'or' operator ?", would "because >> A or B <=> not(not A and not B)" be a proper answer ? > > Note that in Python A or B is in fact not equivalent to not(not A and > not B). Ah, but it *is* "equivalent"; it isn't "identical", but that's not the point. From nick at craig-wood.com Thu Jul 16 16:29:59 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 16 Jul 2009 15:29:59 -0500 Subject: This is a mess... References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: Nick wrote: > this is the new oop version, its pretty messy currently, and i do > understand it is a simple routine, but i'm using it as an exercise to > learn oop python... > > first the (current) traceback: > [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test > > > Traceback (most recent call last): > File "oop_covariance.py", line 24, in > cov = set1.covariance(set2, Eigen_vect.dot) > File "/home/nleioatts/python/Eigen.py", line 66, in covariance > print self.vectors[i][i] > AttributeError: Eigen_vect instance has no attribute '__getitem__' You are trying to use your Eigen_vect class as an array. In order for an object to act as an array it needs a __getitem__ method. > class Eigen_vect: > def __init__(self, e_val, e_vect): > self.e_val = e_val > self.e_vect = e_vect > def length(self): > return len(self.e_vect) > > def dot(self, other): > d = 0.0 > if other.length() != self.length(): > raise ValueError, "Eigen Vectors not same Length" > for k in range(self.length()): > # print "HI NICK", self.e_vect[k], other.e_vect[k] > d += float(self.e_vect[k]) * float(other.e_vect[k]) > return d Either add a __getitem__ method like this def __getitem__(self, n): return self.e_vect[n] Or you might want to subclass list if you are going to do a lot of list like operations on Eigen_vects, eg class Eigen_vect(list): def __init__(self, e_val, e_vect): self.e_val = e_val self[:] = e_vect def dot(self, other): d = 0.0 if len(other) != len(self): raise ValueError("Eigen Vectors not same Length") for a, b in zip(self, other): d += float(a) * float(b) return d Notice that now these things are lists, you can use len, zip etc on them which makes the code much neater. e = Eigen_vect(3, range(10)) f = Eigen_vect(4, range(1,11)) print e print f print e[2] print e.dot(f) Which prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 2 330.0 -- Nick Craig-Wood -- http://www.craig-wood.com/nick From robertrobert93 at yahoo.com Thu Jul 16 16:41:39 2009 From: robertrobert93 at yahoo.com (Robert Robert) Date: Thu, 16 Jul 2009 13:41:39 -0700 (PDT) Subject: file write IOError Invalid argument Message-ID: <505868.24184.qm@web59308.mail.re1.yahoo.com> Hello All, I am trying to write a binary string to file on a windows network share. I get an IOError. I've read that it is because the file size is too large. I did a type( binaryString) and saw that it was type "str". So I loop through it one by one element and use f.write to write to file, and it worked without any error. However it took a long while to write to file. I was wondering how I could split this binary string into N number of chunks efficiently and write them to file. thanks, rh -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Jul 16 16:42:48 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 16:42:48 -0400 Subject: turtle dump In-Reply-To: <4A5F287B.2000503@xs4all.nl> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Michiel Overtoom wrote: > > I got success with the following code (python 2.6.2): > > import turtle > turtle.reset() > for i in range(4): > turtle.forward(50) > turtle.right(90) > can=turtle.getscreen().getcanvas() > can.postscript(file="tmp.ps") Is raw postscript (.ps) the only thing tk can write from canvas? I would like to be able to import into OpenOffice document (drawing object) and it can import encapsulated postscript (.eps) and about 20 other things but not, apparently, .ps. Help on method postscript: postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance is spectacularly useless. I did not see anything similar in the list of about 100 attributes. The image_type of the canvas is 'photo bitmap' but I see no method to write that. From tjreedy at udel.edu Thu Jul 16 16:55:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 16:55:08 -0400 Subject: This is a mess... In-Reply-To: References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: Nick wrote: > this is the new oop version, its pretty messy currently, and i do > understand it is a simple routine, but i'm using it as an exercise to > learn oop python... You neglected to specify Python version. > > first the (current) traceback: > [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test > > > Traceback (most recent call last): > File "oop_covariance.py", line 24, in > cov = set1.covariance(set2, Eigen_vect.dot) > File "/home/nleioatts/python/Eigen.py", line 66, in covariance > print self.vectors[i][i] > AttributeError: Eigen_vect instance has no attribute '__getitem__' self.vectors[i] is apparently an Eigen_vect instance and you try to subscript it, but, as the message said, there is no __getitem__ method. So add one. > > and a quick explaination: the file structures aare a 2d list of lists > of numbers corresponding to eginvectors and a list of numbers > corresponding to eigenvalues > > #########################33##Here is the main body > #!/usr/bin/env python > import sys > import Eigen > from Eigen import * > > if len(sys.argv) != 3: > print " " > print "The corrent usage is 'python covariance.py file1 file2'" > print "where the _U.asc and _s.asc will be appended when needed" > print " " > exit(1) > file1 = sys.argv[1] > file2 = sys.argv[2] > set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc") > set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc") > cov = set1.covariance(set2, Eigen_vect.dot) > print cov > > > ###############and here are the classes: > > #!/usr/bin/env python > import sys > import math > from math import sqrt > > class Eigen_vect: > def __init__(self, e_val, e_vect): > self.e_val = e_val > self.e_vect = e_vect > def length(self): > return len(self.e_vect) You should really call this __len__ so len(Eigen_vect()) will work. I presume you need something like def __getitem__(self, i): return self.e_vect[i] Before continuing, read the Language Reference / Data Model / Special method names to understand how to write classes to integrate into syntax. Terry Jan Reedy From pavlovevidence at gmail.com Thu Jul 16 17:00:43 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 16 Jul 2009 14:00:43 -0700 (PDT) Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: On Jul 16, 8:12?am, Gabriel Rossetti wrote: > Hello everyone, > > I am using threading.Condition.wait(timeout) and was surprised to see > that there is no return value nor an exception when wait() is used w/ a > timeout. How am I supposed to know if it was notified or if it timed out? That's a good question. Condition.wait seems to violate an invariant if it can return without having acquired the underlying lock. And if it does the least it could do is to return a status so you know if you have to skeddadle. Workaround is to use the _is_owned() method of Condition to see if you are the owner of the condition, but this method is undocumented. cond = Condition() ... cond.acquire() while not ok_to_proceed(): cond.wait() if not cond._is_owned(): # must've timed out raise TimeOutException operate() cond.release() Carl Banks From tjreedy at udel.edu Thu Jul 16 17:10:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 17:10:15 -0400 Subject: interactive fiction in Python? In-Reply-To: <853a8wlm25.fsf@agentultra.com> References: <5c97224b-99d4-469b-90cb-1c04b1e1e9a0@m11g2000yqh.googlegroups.com> <853a8wlm25.fsf@agentultra.com> Message-ID: J Kenneth King wrote: > George Oliver writes: > >> hi, I'm just curious who might be working on interactive fiction >> modules in the style of Inform or TADS for Python. I've seen a few >> threads on this list [1] (among many that mention IF tangentially), >> and there are old projects like PUB and PAWS. There are some newer >> potential projects such as Curveship as well. In any case I'd like to >> see what's out there. www.renpy.org Visual novel engine written in Python, with full access to Python as needed. Download includes a tutorial written with renpy. You can look as source code in a separate window as you go (at least earlier version I used did). Also has 150+ free games/novels to download. "Ren'Py is a free and cross-platform engine that helps you use words, pictures, and sounds to tell stories with the computer." Terry Jan Reedy From nobody at nowhere.com Thu Jul 16 17:12:32 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 22:12:32 +0100 Subject: Why aren't OrderedDicts comparable with < etc? References: <401967fb-c12e-4f63-8b05-6dffa0688028@a26g2000yqn.googlegroups.com> <028e48c2-4a3a-4920-9a67-d9bb95714b4f@c36g2000yqn.googlegroups.com> Message-ID: On Thu, 16 Jul 2009 03:59:47 -0700, Mark wrote: >> > Or maybe not. If OrderedDicts are sequences as well as mappings, then we >> > should be able to sort them. And that seems a bit much even for me. >> One thing that I've just noticed is that you can use <, <=, >=, and > >> with sets: >> It seems a bit inconsistent that with sets you always get False when >> using an ordering operator but with an ordered dict you get an >> exception? > > Ooops---disregard the above---I forgot that these do subset and > superset comparisions! Which is an argument for dictionaries (ordered or not) doing likewise, except that the comparison would be subfunction rather than subset, i.e. d1 References: Message-ID: <26ddd1750907161422n5b640a48pbbf9325dd05a699d@mail.gmail.com> On Thu, Jul 16, 2009 at 5:00 PM, Carl Banks wrote: > On Jul 16, 8:12?am, Gabriel Rossetti > wrote: >> Hello everyone, >> >> I am using threading.Condition.wait(timeout) and was surprised to see >> that there is no return value nor an exception when wait() is used w/ a >> timeout. How am I supposed to know if it was notified or if it timed out? > > That's a good question. ?Condition.wait seems to violate an invariant > if it can return without having acquired the underlying lock. ?And if > it does the least it could do is to return a status so you know if you > have to skeddadle. > > Workaround is to use the _is_owned() method of Condition to see if you > are the owner of the condition, but this method is undocumented. > > cond = Condition() > ... > cond.acquire() > while not ok_to_proceed(): > ? ?cond.wait() > ? ?if not cond._is_owned(): > ? ? ? ?# must've timed out > ? ? ? ?raise TimeOutException > operate() > cond.release() You will always be the owner of the condition whether it times out or your thread is notified. This doesn't solve the problem. As an aside, the implementation of wait() is rather poor in any case; the sleep function should not be used for the purpose of waiting for an event. Depending on what platform you are using, I can offer two solutions. The simplest one is to subclass Condition and override the wait method. Just copy the code as it is, but modify the end of the function to look like this: if not gotit: if __debug__: self._note("%s.wait(%s): timed out", self, timeout) try: self.__waiters.remove(waiter) except ValueError: pass return False else: if __debug__: self._note("%s.wait(%s): got it", self, timeout) return True The two return statements will tell you whether you timed out ("if not gotit" condition), or were notified ("else"). The better way to solve this and the other problem that I mentioned is to use native OS events. This is what I had to do for a recent project. I used pywin32 extension to completely rewrite Event, Condition, and Timer classes to use a true event-based approach. In the process of the rewrite I also modified some functionality, such as returning True of False from wait(). Unfortunately, I'm not able to post this code right now, though I can try and get permission to do so a bit later. If you are writing software for Windows, then simply take a look at win32event module. CreateEvent, SetEvent, ResetEvent, WaitForSingleObject are all the Windows functions that you need to reimplement threading.Event. Using the new event class you can implement a proper Condition.wait() method (hint: use new events rather than locks to wait for notification). If you are on Linux or BSD, I can't help you. I'm sure that there is some equivalent functionality that you may be able to access using ctypes. Exactly how to do it, however, is not something that I can help you with right now. - Max From __peter__ at web.de Thu Jul 16 17:37:06 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 16 Jul 2009 23:37:06 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Terry Reedy wrote: > Michiel Overtoom wrote: >> >> I got success with the following code (python 2.6.2): >> >> import turtle >> turtle.reset() >> for i in range(4): >> turtle.forward(50) >> turtle.right(90) >> can=turtle.getscreen().getcanvas() >> can.postscript(file="tmp.ps") > > Is raw postscript (.ps) the only thing tk can write from canvas? > I would like to be able to import into OpenOffice document (drawing > object) and it can import encapsulated postscript (.eps) and about 20 > other things but not, apparently, .ps. > > Help on method postscript: > postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance > > is spectacularly useless. http://docs.python.org/library/turtle.html#turtle.getcanvas The module is a bit messy, but the accompanying documentation seems OK to me. > I did not see anything similar in the list of > about 100 attributes. The image_type of the canvas is 'photo bitmap' but > I see no method to write that. That's probably a bitmap that you can draw on the canvas. The Canvas is actually that of Tkinter, and according to http://www.tcl.tk/man/tcl8.5/TkCmd/canvas.htm#M59 it writes Encapsulated Postscript 3.0. Also: $ file tmp.ps tmp.ps: PostScript document text conforming DSC level 3.0, type EPS Try changing the file extension from .ps to .eps. Peter From emile at fenx.com Thu Jul 16 17:59:22 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 14:59:22 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On 7/16/2009 1:29 PM Nobody said... > On Wed, 15 Jul 2009 18:14:10 +0200, Hrvoje Niksic wrote: > >>> If the question was "Why is there no 'or' operator ?", would "because >>> A or B <=> not(not A and not B)" be a proper answer ? >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > Ah, but it *is* "equivalent"; it isn't "identical", but that's not the > point. > I'm not sure I'd call it equivalent. A or B returns either unaltered, and not(not A and not B) always returns a boolean. The equivalent would be not(not( A or B )). Emile From piet at cs.uu.nl Thu Jul 16 18:03:52 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 00:03:52 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Hello everyone, >GR> I am using threading.Condition.wait(timeout) and was surprised to see that >GR> there is no return value nor an exception when wait() is used w/ a timeout. >GR> How am I supposed to know if it was notified or if it timed out? Normally you wouldn't have to know. The logic of your program should be such that you wait until a certain condition is satisfied. After each wait you usually check that condition anyway, like: while (some_condition_satisfied): cond_var.wait() The only exception could be if there is a 1-1 relation between a single waiter and a single notifier. I that case you can usually use if instead of while (but it does depend of the logic of the notifier). So in case of using a timeout you can check the desired condition after the wait and decide what to do. If the condition was not satisfied you probably want to do something special, like error handling, or do something else first and then retry the wait. There could be situations where this doesn't work, however. For example if you wait until a queue is not empty, and the notify is done when something is inserted in the queue, another thread may sneak in while you are waiting and snatch the inserted item just before your thread continues after the wait. In that case you can't distinguish between a timeout and a snatcher. So it all depends on what your use case is. (Java's wait doesn't return anything either.) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From thelists at optusnet.com.au Thu Jul 16 18:07:30 2009 From: thelists at optusnet.com.au (Leo List) Date: 16 Jul 2009 22:07:30 GMT Subject: Python 3 xmlrpc client HTTPS problem Message-ID: <4a5fa4a2$0$3321$afc38c87@news.optusnet.com.au> I have just built and installed version 3.1 and all's good, but when I try to connect to a server via HTTPS I get the following: File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1029, in __call__ return self.__send(self.__name, args) File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1271, in __request verbose=self.__verbose File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1058, in request http_conn = self.send_request(host, handler, request_body, verbose) File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1183, in send_request "your version of http.client doesn't support HTTPS") NotImplementedError: your version of http.client doesn't support HTTPS I am on Ubuntu and have followed the instructions on how to build a release with SSL support: ./configure CPPFLAGS=-I/usr/include/openssl LDFLAGS=-L/usr/lib/ssl Any idea what I'm missing? Thanks From piet at cs.uu.nl Thu Jul 16 18:08:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 00:08:08 +0200 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: >>>>> akhil1988 (a) wrote: >a> ok! >a> I got the indentation errors fixed. Bu I get another error: >a> Traceback (most recent call last): >a> File "./temp.py", line 484, in >a> main() >a> File "./temp.py", line 476, in main >a> line.decode('utf-8').strip() >a> AttributeError: 'str' object has no attribute 'decode' >a> I am using Python3.1 In Python 3 you can't decode strings because they are Unicode strings and it doesn't make sense to decode a Unicode string. You can only decode encoded things which are byte strings. So you are mixing up byte strings and Unicode strings. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pavlovevidence at gmail.com Thu Jul 16 18:15:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 16 Jul 2009 15:15:52 -0700 (PDT) Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: On Jul 16, 2:22?pm, Maxim Khitrov wrote: > On Thu, Jul 16, 2009 at 5:00 PM, Carl Banks wrote: > > On Jul 16, 8:12?am, Gabriel Rossetti > > wrote: > >> Hello everyone, > > >> I am using threading.Condition.wait(timeout) and was surprised to see > >> that there is no return value nor an exception when wait() is used w/ a > >> timeout. How am I supposed to know if it was notified or if it timed out? > > > That's a good question. ?Condition.wait seems to violate an invariant > > if it can return without having acquired the underlying lock. ?And if > > it does the least it could do is to return a status so you know if you > > have to skeddadle. > > > Workaround is to use the _is_owned() method of Condition to see if you > > are the owner of the condition, but this method is undocumented. > > > cond = Condition() > > ... > > cond.acquire() > > while not ok_to_proceed(): > > ? ?cond.wait() > > ? ?if not cond._is_owned(): > > ? ? ? ?# must've timed out > > ? ? ? ?raise TimeOutException > > operate() > > cond.release() > > You will always be the owner of the condition whether it times out or > your thread is notified. This doesn't solve the problem. You are correct, I misread the code. So scratch what I said. Carl Banks From akhilanger at gmail.com Thu Jul 16 18:43:37 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 15:43:37 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24525761.post@talk.nabble.com> Then, how should I do it? I read a byte string from sys.stdin which needs to converted to unicode string for further processing. I cannot just remove the decode statement and proceed? This is it what it looks like: for line in sys.stdin: line = line.decode('utf-8').strip() if line == '': #do something here elsif #do something here If I remove the decode statement, line == '' never gets true. --Akhil Piet van Oostrum wrote: > >>>>>> akhil1988 (a) wrote: > >>a> ok! >>a> I got the indentation errors fixed. Bu I get another error: > >>a> Traceback (most recent call last): >>a> File "./temp.py", line 484, in >>a> main() >>a> File "./temp.py", line 476, in main >>a> line.decode('utf-8').strip() >>a> AttributeError: 'str' object has no attribute 'decode' > >>a> I am using Python3.1 > > In Python 3 you can't decode strings because they are Unicode strings > and it doesn't make sense to decode a Unicode string. You can only > decode encoded things which are byte strings. So you are mixing up byte > strings and Unicode strings. > -- > Piet van Oostrum > URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] > Private email: piet at vanoostrum.org > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24525761.html Sent from the Python - python-list mailing list archive at Nabble.com. From tjreedy at udel.edu Thu Jul 16 19:06:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 19:06:55 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Peter Otten wrote: > Terry Reedy wrote: >> Help on method postscript: >> postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance >> >> is spectacularly useless. This is from interactive help. > http://docs.python.org/library/turtle.html#turtle.getcanvas That method, following your lead, is how I got to .postscript. The doc says " Return the Canvas of this TurtleScreen. Useful for insiders who know what to do with a Tkinter Canvas." > The module is a bit messy, but the accompanying documentation seems OK to > me. ?? I am not an 'insider' ;-). >> I did not see anything similar in the list of >> about 100 attributes. Of Canvas, mostly methods. I can understand that no one who could write decent doc strings for the 100 methods has actually volunteered to do so. >The image_type of the canvas is 'photo bitmap' but >> I see no method to write that. > > That's probably a bitmap that you can draw on the canvas. The Canvas is > actually that of Tkinter, and according to > > http://www.tcl.tk/man/tcl8.5/TkCmd/canvas.htm#M59 > > it writes Encapsulated Postscript 3.0. Also: > > $ file tmp.ps > tmp.ps: PostScript document text conforming DSC level 3.0, type EPS > > Try changing the file extension from .ps to .eps. I will. Thank you. tjr From graham.dumpleton at gmail.com Thu Jul 16 19:25:37 2009 From: graham.dumpleton at gmail.com (Graham Dumpleton) Date: Thu, 16 Jul 2009 16:25:37 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <4a5ea61b$0$1682$742ec2ed@news.sonic.net> <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> <4a5eb46a$0$1673$742ec2ed@news.sonic.net> Message-ID: On Jul 16, 3:05?pm, John Nagle wrote: > alex23 wrote: > > On Jul 16, 2:03 pm, John Nagle wrote: > >> ? ? ?"fcgi" is an option for this sort of thing. ?With "mod_fcgi" installed > >> in Apache, and "fcgi.py" used to manage the Python side of the problem, you > >> can have semi-persistent programs started up and shut down for you on the > >> server side. > > > Hey John, > > > The environments in which I've been asked to develop webs apps using > > Python have all utilisedmod_wsgi. Do you have any experience with > >mod_wsgivs mod_fcgi, and if so, can you comment on any relevant > > performance / capability / ease-of-use differences? > > > Cheers, > > alex23 > > ? ? FCGI seems to be somewhat out of favor, perhaps because it isn't > complicated enough. I doubt that is the reason. It is out of favour for Python web hosting at least, because web hosting companies provide really crappy support for using Python with it and also don't like long lived processes. Most FASTCGI deployments are configured in a way more appropriate for PHP because that is what the default settings favour. This works to the detriment of Python. Even when people setup FASTCGI themselves, they still don't play with the configuration to optimise it for their specific Python application. Thus it can still run poorly. > It's a mature technology and works reasonably well. But is showing its age. It really needs a refresh. Various of its design decisions were from when network speeds and bandwidth were much lower and this complicates the design. Making the protocol format simpler in some areas would make it easier to implement. The protocol could also be built on to make process management more flexible, rather than relying on each implementation to come up with adhoc ways of managing it. Part of the problem with FASTCGI, or hosting of dynamic applications in general, is that web servers primary focus is serving of static files. Any support for dynamic web applications is more of a bolt on feature. Thus, web applications using Python will always be at a disadvantage. PHP manages okay because their model of operation is closer to the one shot processing of static files. Python requires persistent to work adequately with modern fat applications. > It's been a puzzle to me that FCGI was taken out of the > main Apache code base, because it gives production-level performance > with CGI-type simplicity. As far as I know FASTCGI support has in the past never been a part of the Apache code base. Both mod_fastcgi and mod_fcgid were developed by independent people and not under the ASF. In Apache 2.3 development versions (to become 2.4 when released), there will however be a mod_proxy_fcgi. The ASF has also taken over management of mod_fcgid and working out how that may be incorporated into future Apache versions. > ? ? WSGI has a mode for running Python inside the Apache process, > which is less secure and doesn't allow multiple Python processes. Depending on the requirements it is more than adequate and if configured correctly gives better performance and scalability. Not everyone runs in a shared hosting environment. The different modes of mod_wsgi therefore give choice. > That complicates mod_wsgi considerably, No it doesn't. It is actual daemon mode that complicates things, not embedded mode. > and ties it very closely > to specific versions of Python and Python modules. ?As a result, > the WSGI developers are patching at a great rate. What are you talking about when you say 'As a result, the WSGI developers are patching at a great rate'? As with some of your other comments, this is leaning towards being FUD and nothing more. There is no 'patching at a great rate' going on. >?I think the thing has too many "l33t features". It is all about choice and providing flexibility. You may not see a need for certain features, but it doesn't mean other people don't have a need for it. > I'd avoid "embedded mode". ?"Daemon mode" is the way to go if you use WSGI. I'd agree, but not sure you really understand the reasons for why it would be preferred. > I haven't tried WSGI; Then why comment on it as if you are knowledgeable on it. > I don't need the grief of a package under heavy development. More FUD. Graham From ben+python at benfinney.id.au Thu Jul 16 19:56:33 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 09:56:33 +1000 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: <87zlb4gp1q.fsf@benfinney.id.au> Inky 788 writes: > On Jul 10, 7:35?pm, Ben Finney wrote: > > Yes. That's a ?should? and not a ?must?, even though PEP 8 says it > > with a simple imperative:: > > > > ? ? Use 4 spaces per indentation level. > > That actually sounds pretty weird. "Don't do {foo}. Really, I mean > *really* don't do {foo}. Oh, also, the interpreter allows you to do > {foo}. But don't do it! I mean it!". Not weird at all, especially when you realise that your interpretation isn't there in the document. The allowance for both types of indentation is a compromise made for backward compatibility with code written before this convention was quite so strong. All *new* code is strongly advised to follow the convention, while the interpreter will still allow old code that doesn't follow the convention. > Very ... perlish, if you ask me. Hah! Perl doesn't have the ?don't do {foo}? part, so I don't see what you're seeing. -- \ ?I like to skate on the other side of the ice.? ?Steven Wright | `\ | _o__) | Ben Finney From lhughes42 at gmail.com Thu Jul 16 19:58:36 2009 From: lhughes42 at gmail.com (lh) Date: Thu, 16 Jul 2009 16:58:36 -0700 (PDT) Subject: Visualization of Python Class Hierarchy Message-ID: I would like to automatically generate this for my program. I am running python in an eclipse context (pydev). I am not familiar with the best current tools. Thank you From wuwei23 at gmail.com Thu Jul 16 20:37:00 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 17:37:00 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> > >> Help on method postscript: > >> postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance > >> is spectacularly useless. > > This is from interactive help. The help in iPython says the same, but also mentions that it's a dynamically generated function, so it may not be picking up the docstring that way. turtle.ScrolledCanvas.postscript is similarly terse, but you can find more info in turtle.Canvas.postscript: Print the contents of the canvas to a postscript file. Valid options: colormap, colormode, file, fontmap, height, pageanchor, pageheight, pagewidth, pagex, pagey, rotate, witdh, x, y. > The doc says > > " Return the Canvas of this TurtleScreen. Useful for insiders who know > what to do with a Tkinter Canvas." > > > The module is a bit messy, but the accompanying documentation seems OK to > > me. > > ?? I am not an 'insider' ;-). I don't think the intent of the turtle module is to be a primer in Tk, but more of an educational tool. The lack of image export niceties has a lot to do with the limitations of Tk, but was probably (and I'm conjecturing here) felt to be outside the scope of what the module aims to achieve. > Of Canvas, mostly methods. I can understand that no one who could write > decent doc strings for the 100 methods has actually volunteered to do so. I think they all _have_ doc strings, or at least the code they eventually call does, but I'd have to look into the module itself to see if they could be brought over dynamically. You may be interested in the final page of Greg Lingl's PyCon talk, Seven Ways to use Turtle[1] which states: The turtle module is designed in a way so that essentially all of the turtle graphics machinery is based on a class TurtleScreenBase, which provides the interface to the underlying graphics toolkit Tkinter. So it?s easy to port turtle.py to different graphics toolkits/ libraries, simply by replacing this Tkinter base class with an appropriate different one. I?ve done two ports: Pygame & Jython If you're after bitmap, I'd suggest contacting Greg and asking about the Pygame port. His contact info can be found at the original site for the revised turtle module[2], hopefully it's still up to date. 1: us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle- PyCon2007.pdf 2: http://xturtle.rg16.at/download.html From tjreedy at udel.edu Thu Jul 16 20:55:54 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 20:55:54 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Terry Reedy wrote: >> $ file tmp.ps >> tmp.ps: PostScript document text conforming DSC level 3.0, type EPS >> >> Try changing the file extension from .ps to .eps. > > I will. Thank you. I tried it. Unfortunately, OOo does not open it correctly. It just displays the first three lines of metadate - Title, Creator, Date -- as image text. Photoshop does read the image, and does an ok job of conversion once anti-aliasing is turned off. Hmmm. Crazy idea... in http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle-PyCon2007.pdf Gregor Lingl says that turtle.py has been ported to pygame and jython as back ends. It should be possible to instead send output to a file instead of an on-screen canvas. Run a program to screen. When output looks right, return with a different parameter to send to file. For instance, an OpenDocumentDrawing file (.odd) using odfpy. This assumes that there are elements corresponding to each turtle command. Or at least that the fit is close enough. Terry Jan Reedy From ronn.ross at gmail.com Thu Jul 16 20:57:03 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Thu, 16 Jul 2009 20:57:03 -0400 Subject: TypeError: unbound method Message-ID: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> Hello all, Created a python file that has a class and three methods. When I jump into interactive mode I import like so: from import Now when I try to use a method from the class: var1 = class.method() It give me this error: TypeError: unbound method must be called with instance as first argument (got int instance instead) I'm not sure what is going on event after consulting with google -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben+python at benfinney.id.au Thu Jul 16 21:01:49 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 11:01:49 +1000 Subject: Override a method but inherit the docstring Message-ID: <87r5wggm0y.fsf@benfinney.id.au> Howdy all, The following is a common idiom:: class FooGonk(object): def frobnicate(self): """ Frobnicate this gonk. """ basic_implementation(self.wobble) class BarGonk(FooGonk): def frobnicate(self): special_implementation(self.warble) The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly applicable to the ?BarGonk.frobnicate? method also. Yet in overriding the method, the original docstring is not associated with it. Ideally there would be a way to specify that the docstring should be inherited. The best I can come up with is:: class BarGonk(FooGonk): def frobnicate(self): special_implementation(self.warble) frobnicate.__doc__ = FooGonk.frobnicate.__doc__ but that violates DRY (the association between BarGonk and FooGonk is being repeated), puts the docstring assignment awkwardly after the end of the method instead of at the beginning where docstrings normally go, and reads poorly besides. What is the most Pythonic, DRY-adherent, and preferably least-ugly approach to override a method, but have the same docstring on both methods? -- \ ?Why, I'd horse-whip you if I had a horse.? ?Groucho Marx | `\ | _o__) | Ben Finney From clp2 at rebertia.com Thu Jul 16 21:08:45 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 18:08:45 -0700 Subject: TypeError: unbound method In-Reply-To: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> References: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> Message-ID: <50697b2c0907161808i3bcc55eej333ad83660897313@mail.gmail.com> On Thu, Jul 16, 2009 at 5:57 PM, Ronn Ross wrote: > Hello all, > > Created a python file that has a class and three methods. When I jump into > interactive mode I import like so: > > from import > > Now when I try to use a method from the class: > > var1 = class.method() > > It give me this error: > TypeError: unbound method must be called with > instance as first argument (got int instance instead) > > I'm not sure what is going on event after consulting with google You're trying to call an instance method on the class itself, which doesn't make sense. You need to first create an instance of the class to invoke the method on. i.e.: instance = TheClass(constuctor_arguments_here) var1 = instance.method() Cheers, Chris -- http://blog.rebertia.com From exarkun at divmod.com Thu Jul 16 21:13:34 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Thu, 16 Jul 2009 21:13:34 -0400 Subject: Override a method but inherit the docstring In-Reply-To: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <20090717011334.2543.775231474.divmod.quotient.2982@henry.divmod.com> On Fri, 17 Jul 2009 11:01:49 +1000, Ben Finney wrote: >Howdy all, > >The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > >The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >the method, the original docstring is not associated with it. > >Ideally there would be a way to specify that the docstring should be >inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > >but that violates DRY (the association between BarGonk and FooGonk is >being repeated), puts the docstring assignment awkwardly after the end >of the method instead of at the beginning where docstrings normally go, >and reads poorly besides. > >What is the most Pythonic, DRY-adherent, and preferably least-ugly >approach to override a method, but have the same docstring on both >methods? > How about this? class BarGonk(FooGonk): @inherit_docstring def frobnicate(self): special_implementation(self.warble) The implementation of "inherit_docstring" is left as an exercise for the reader (it's not utterly trivial, I admit, as "FooGonk" will not readily be at hand, but it is still possible). By the way, I don't think this is a particularly good idea. Presumably there is a reason your implementation is special. It would probably be better if this were reflected in the docstring somehow. Perhaps this idea is a better one: class BarGonk(FooGonk): @append_to_docstring def frobnicate(self): """ This implementation takes the warble into consideration. """ special_implementation(self.warble) With the result of BarGonk.frobnicate.__doc__ being set to: Frobnicate this gonk. This implementation takes the warble into consideration. Jean-Paul From nobody at nowhere.com Thu Jul 16 21:16:53 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 17 Jul 2009 02:16:53 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On Thu, 16 Jul 2009 14:59:22 -0700, Emile van Sebille wrote: >>>> If the question was "Why is there no 'or' operator ?", would "because >>>> A or B <=> not(not A and not B)" be a proper answer ? >>> Note that in Python A or B is in fact not equivalent to not(not A and >>> not B). >> >> Ah, but it *is* "equivalent"; it isn't "identical", but that's not the >> point. > > I'm not sure I'd call it equivalent. That depends upon what definition of "equivalent" you are using. Apart from all manner of vague, informal definitions, there is a formal definition: A relation "=" is an equivalence relation if the following hold for all x, y, and z: x = x x = y => y = x x = y and y = z => x = z An equivalence relation partitions its domain into equivalence classes, such that an element is "=" (equivalent) to every element in the same class, and not "=" to every element in every other class. This is a lesser notion of equality to "identity", which also requires that x = y => f(x) = f(y) for all f. Python's notion of boolean values treats x and y as equivalent if bool(x) == bool(y). On this basis, the result of "A or B" is *equivalent* to the result of "not(not A and not B)". If you use either result as a boolean value (e.g. the test of an "if" or "while" statement, as an operand to "and" or "or", etc), the effect is the same. The results aren't *identical*, as there exist ways to distinguish the two. As for the utility of this behaviour, returning an actual value rather than True/False allows the operators to be used as "gates". The term "gate" in digital logic follows from the axioms: 0 and x = 0 1 and x = x 0 or x = x 1 or x = 1 [0 = False, 1 = True] If you consider the left operand as the "control" and the right operand as the "data", the control determines whether or not the data can pass through the gate to the output (i.e. whether the gate is open or closed). In Python: and: False and 7 => False False and 0 => False True and 7 => 7 True and 0 => 0 or: False or 7 => 7 False or 0 => 0 True or 7 => True True or 0 => True From max at alcyone.com Thu Jul 16 21:18:56 2009 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jul 2009 18:18:56 -0700 Subject: Einstein summation notation (was: question of style) In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: Albert van der Horst wrote: > Einstein introduced the summation convention for indices that > are used twice. Leaving out summation signs is absolutely hideous, > but it has saved generations of physicists of loosing track (and > their minds.) There is a joke among mathematicians that if Einstein hadn't been remembered for a couple of other fairly contributions things in physics :-), he would have been remembered in mathematics for that convention, since it has saved everyone -- mathematicians included -- a lot of time and anguish. When using tensor calculus, there's even very easy ways to verify you have a valid tensor equation using it. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis If the sky should fall, hold up your hands. -- (a Spanish proverb) From mkhitrov at gmail.com Thu Jul 16 21:21:16 2009 From: mkhitrov at gmail.com (Maxim Khitrov) Date: Thu, 16 Jul 2009 21:21:16 -0400 Subject: Override a method but inherit the docstring In-Reply-To: <20090717011334.2543.775231474.divmod.quotient.2982@henry.divmod.com> References: <87r5wggm0y.fsf@benfinney.id.au> <20090717011334.2543.775231474.divmod.quotient.2982@henry.divmod.com> Message-ID: <26ddd1750907161821r1a5feeebr1c7c63e0130bf6e7@mail.gmail.com> On Thu, Jul 16, 2009 at 9:13 PM, Jean-Paul Calderone wrote: > On Fri, 17 Jul 2009 11:01:49 +1000, Ben Finney > wrote: >> >> Howdy all, >> >> The following is a common idiom:: >> >> ? class FooGonk(object): >> ? ? ? def frobnicate(self): >> ? ? ? ? ? """ Frobnicate this gonk. """ >> ? ? ? ? ? basic_implementation(self.wobble) >> >> ? class BarGonk(FooGonk): >> ? ? ? def frobnicate(self): >> ? ? ? ? ? special_implementation(self.warble) >> >> The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >> applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >> the method, the original docstring is not associated with it. >> >> Ideally there would be a way to specify that the docstring should be >> inherited. The best I can come up with is:: >> >> ? class BarGonk(FooGonk): >> ? ? ? def frobnicate(self): >> ? ? ? ? ? special_implementation(self.warble) >> ? ? ? frobnicate.__doc__ = FooGonk.frobnicate.__doc__ >> >> but that violates DRY (the association between BarGonk and FooGonk is >> being repeated), puts the docstring assignment awkwardly after the end >> of the method instead of at the beginning where docstrings normally go, >> and reads poorly besides. >> >> What is the most Pythonic, DRY-adherent, and preferably least-ugly >> approach to override a method, but have the same docstring on both >> methods? >> > > How about this? > > ? class BarGonk(FooGonk): > ? ? ? @inherit_docstring > ? ? ? def frobnicate(self): > ? ? ? ? ? special_implementation(self.warble) > > The implementation of "inherit_docstring" is left as an exercise for the > reader (it's not utterly trivial, I admit, as "FooGonk" will not readily > be at hand, but it is still possible). > > By the way, I don't think this is a particularly good idea. ?Presumably > there is a reason your implementation is special. ?It would probably be > better if this were reflected in the docstring somehow. ?Perhaps this > idea is a better one: > > ? class BarGonk(FooGonk): > ? ? ? @append_to_docstring > ? ? ? def frobnicate(self): > ? ? ? ? ? """ > ? ? ? ? ? This implementation takes the warble into consideration. > ? ? ? ? ? """ > ? ? ? ? ? special_implementation(self.warble) > > With the result of BarGonk.frobnicate.__doc__ being set to: > > > ? ?Frobnicate this gonk. > > ? ?This implementation takes the warble into consideration. Another way is to use a metaclass. Have its __new__ method loop through all attributes and compare those with what is already defined in bases. If you find a match, copy the __doc__ attribute. The advantage here is that it will work for all methods without any additional code, not counting the "__metaclass__ = ..." line. If you define a metaclass for the base, then no modifications are required for any subclasses. I do agree, however, that the best thing to do is to write a very short explanation for what the override is for. - Max From nobody at nowhere.com Thu Jul 16 21:23:41 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 17 Jul 2009 02:23:41 +0100 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: >> In Python 3 you can't decode strings because they are Unicode strings >> and it doesn't make sense to decode a Unicode string. You can only >> decode encoded things which are byte strings. So you are mixing up byte >> strings and Unicode strings. > > Then, how should I do it? > I read a byte string from sys.stdin which needs to converted to unicode > string for further processing. In 3.x, sys.stdin (stdout, stderr) are text streams, which means that they read and write Unicode strings, not byte strings. > I cannot just remove the decode statement and > proceed? > > This is it what it looks like: > > for line in sys.stdin: > line = line.decode('utf-8').strip() > if line == '': #do something here > elsif #do something here > > If I remove the decode statement, line == '' never gets true. Did you inadvertently remove the strip() as well? From rhodri at wildebst.demon.co.uk Thu Jul 16 21:31:43 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 17 Jul 2009 02:31:43 +0100 Subject: Override a method but inherit the docstring In-Reply-To: <87r5wggm0y.fsf@benfinney.id.au> References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: On Fri, 17 Jul 2009 02:01:49 +0100, Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > Ideally there would be a way to specify that the docstring should be > inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > > but that violates DRY (the association between BarGonk and FooGonk is > being repeated), Not really. Consider the case of BarGonk being a subclass of FooGonk and BazGonk; which docstring would you wish to inherit? > puts the docstring assignment awkwardly after the end > of the method instead of at the beginning where docstrings normally go, > and reads poorly besides. Sounds like a job for a decorator! (This is probably unbelievably ugly and unwise, since I don't use decorators at all often.) def copydoc(cls): def _fn(fn): if fn.__name__ in cls.__dict__: fn.__doc__ = cls.__dict__[fn.__name__].__doc__ return fn return _fn class BarGonk(FooGonk): @copydoc(FooGonk) def frobnicate(self): special_implementation(self.warble) -- Rhodri James *-* Wildebeest Herder to the Masses From ptmcg at austin.rr.com Thu Jul 16 21:35:05 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 16 Jul 2009 18:35:05 -0700 (PDT) Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> On Jul 16, 8:01?pm, Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > ? ? class FooGonk(object): > ? ? ? ? def frobnicate(self): > ? ? ? ? ? ? """ Frobnicate this gonk. """ > ? ? ? ? ? ? basic_implementation(self.wobble) > > ? ? class BarGonk(FooGonk): > ? ? ? ? def frobnicate(self): > ? ? ? ? ? ? special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > What is the most Pythonic, DRY-adherent, and preferably least-ugly > approach to override a method, but have the same docstring on both > methods? > Two ideas come to mind, the decorator way and the metaclass way. I am not a guru at either, but these two examples work: # the decorator way def inherit_docstring_from(cls): def docstring_inheriting_decorator(fn): fn.__doc__ = getattr(cls,fn.__name__).__doc__ return fn return docstring_inheriting_decorator class FooGonk(object): def frobnicate(self): """ Frobnicate this gonk. """ basic_implementation(self.wobble) class BarGonk(FooGonk): @inherit_docstring_from(FooGonk) def frobnicate(self): special_implementation(self.warble) bg = BarGonk() help(bg.frobnicate) Prints: Help on method frobnicate in module __main__: frobnicate(self) method of __main__.BarGonk instance Frobnicate this gonk. Using a decorator in this manner requires repeating the super class name. Perhaps there is a way to get the bases of BarGonk, but I don't think so, because at the time that the decorator is called, BarGonk is not yet fully defined. # The metaclass way from types import FunctionType class DocStringInheritor(type): def __new__(meta, classname, bases, classDict): newClassDict = {} for attributeName, attribute in classDict.items(): if type(attribute) == FunctionType: # look through bases for matching function by name for baseclass in bases: if hasattr(baseclass, attributeName): basefn = getattr(baseclass,attributeName) if basefn.__doc__: attribute.__doc__ = basefn.__doc__ break newClassDict[attributeName] = attribute return type.__new__(meta, classname, bases, newClassDict) class FooGonk2(object): def frobnicate(self): """ Frobnicate this gonk. """ basic_implementation(self.wobble) class BarGonk2(FooGonk2): __metaclass__ = DocStringInheritor def frobnicate(self): special_implementation(self.warble) bg = BarGonk2() help(bg.frobnicate) Prints: Help on method frobnicate in module __main__: frobnicate(self) method of __main__.BarGonk2 instance Frobnicate this gonk. This metaclass will walk the list of bases until the desired superclass method is found AND if that method has a docstring and only THEN does it attach the superdocstring to the derived class method. Please use carefully, I just did the metaclass thing by following Michael Foord's Metaclass tutorial (http://www.voidspace.org.uk/python/ articles/metaclasses.shtml), I may have missed a step or two. -- Paul From vinay_sajip at yahoo.co.uk Thu Jul 16 21:56:51 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Thu, 16 Jul 2009 18:56:51 -0700 (PDT) Subject: ANN: gnupg v0.2.0 released Message-ID: A new version of the Python module which wraps GnuPG has been released. What Changed? ============= The module was refactored slightly to support Python 3.0. The current version now passes all tests on Windows (Python 2.4, 2.5) and Ubuntu (Python 2.4, 2.5, 2.6 and 3.0). What Does It Do? ================ The gnupg module allows Python programs to make use of the functionality provided by the Gnu Privacy Guard (abbreviated GPG or GnuPG). Using this module, Python programs can encrypt and decrypt data, digitally sign documents and verify digital signatures, manage (generate, list and delete) encryption keys, using proven Public Key Infrastructure (PKI) encryption technology based on OpenPGP. This module is expected to be used with Python versions >= 2.4, as it makes use of the subprocess module which appeared in that version of Python. This module is a newer version derived from earlier work by Andrew Kuchling, Richard Jones and Steve Traugott. A test suite using unittest is included with the source distribution. Simple usage: >>> import gnupg >>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory') >>> gpg.list_keys() [{ ... 'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2', 'keyid': '197D5DAC68F1AAB2', 'length': '1024', 'type': 'pub', 'uids': ['', 'Gary Gross (A test user) ']}, { ... 'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A', 'keyid': '0C5FEFA7A921FC4A', 'length': '1024', ... 'uids': ['', 'Danny Davis (A test user) ']}] >>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A']) >>> str(encrypted) '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n \nhQIOA/6NHMDTXUwcEAf ... -----END PGP MESSAGE-----\n' >>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret') >>> str(decrypted) 'Hello, world!' >>> signed = gpg.sign("Goodbye, world!", passphrase='secret') >>> verified = verified = gpg.verify(str(signed)) >>> print "Verified" if verified else "Not verified" 'Verified' For more information, visit http://code.google.com/p/python-gnupg/ - as always, your feedback is most welcome (especially bug reports, patches and suggestions for improvement). Enjoy! Cheers Vinay Sajip Red Dove Consultants Ltd. From tismer at stackless.com Thu Jul 16 22:26:02 2009 From: tismer at stackless.com (Christian Tismer) Date: Fri, 17 Jul 2009 04:26:02 +0200 Subject: ANN: psyco V2 Message-ID: <4A5FE13A.3080708@stackless.com> Announcing Psyco V2 source release ---------------------------------- This is the long awaited announcement of Psyco V2. Psyco V2 is a continuation of the well-known psyco project, which was called finished and was dis-continued by its author Armin Rigo in 2005, in favor of the PyPy project. This is a new project, using Psyco's code base with permission of Armin. Questions aqnd complaints should go to me (tismer at stackless.com) or the mailing list (psyco-devel at lists.sourceforge.net); Armin is explicitly not in charge of (t)his project any longer! As one of the founders and an active member of the PyPy project, I was very happy to be invited to work on Psyco V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco approach "an extension module that just makes Python faster". Psyco is a just-in-time compiler that accelerates arbitrary Python code by specialization. We believe that Psyco's approach can be carried out much further than it was tried so far, when it's first version was abandoned. This first V2 release is source-only. There is no web-site, yet, and there are no binaries for download. These will be available in a few days on http://www.psyco.org . For the time being, please stick with subversion access, building the extension module from source code. The repository is here: http://codespeak.net/svn/psyco/v2/dist Check-out the repository, and run the setup.py script, given that you have access to a C compiler. Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, and Mac OS X. Psyco is not supporting 64 bit, yet. But it is well being considered. The current improvements are, shortly: - Support for Python 2.4, 2.5 and 2.6 - a lot of new builtins - generators, fast and fully supported. More information is coming soon on http://www.psyco.org . This is the beginning of a series of new Psyco versions. Many more improvements are prepared and about to be published, soon, starting with the current version 2.0.0 . Stay tuned, this is just the beginning of psyco's re-birth! For questions about Psyco V2, please join the mailing list psyco-devel at lists.sourceforge.net or contact me on IRC: #psyco on irc.freenode.net . Psyco V2 is fundamentally supported by FATTOC, LLC. See http://www.fattoc.com . Without their continuous support, this work would not have been possible at all. I wish to express my deepest thanks to FATTOC, for allowing me to continue on Psyco with all the energy that this ambitious project needs, and will need. Further special thanks are going to Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, Raymond Hettinger, Fabrizio Milo, Michael Foord, Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, for all the support and discussions. Looking forward to a great future of Psyco! July 17, 2009 -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From tismer at stackless.com Thu Jul 16 22:48:18 2009 From: tismer at stackless.com (Christian Tismer) Date: Fri, 17 Jul 2009 04:48:18 +0200 Subject: ANN: psyco V2 Message-ID: <4A5FE672.3000401@stackless.com> Announcing Psyco V2 source release ---------------------------------- This is the long awaited announcement of Psyco V2. Psyco V2 is a continuation of the well-known psyco project, which was called finished and was dis-continued by its author Armin Rigo in 2005, in favor of the PyPy project. This is a new project, using Psyco's code base with permission of Armin. Questions and complaints should go to me (tismer at stackless.com) or the mailing list (psyco-devel at lists.sourceforge.net); Armin is explicitly not in charge of (t)his project any longer! As one of the founders and an active member of the PyPy project, I was very happy to be invited to work on Psyco V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco approach "an extension module that just makes Python faster". Psyco is a just-in-time compiler that accelerates arbitrary Python code by specialization. We believe that Psyco's approach can be carried out much further than it was tried so far, when it's first version was abandoned. This first V2 release is source-only. There is no web-site, yet, and there are no binaries for download. These will be available in a few days on http://www.psyco.org . For the time being, please stick with subversion access, building the extension module from source code. The repository is here: http://codespeak.net/svn/psyco/v2/dist Check-out the repository, and run the setup.py script, given that you have access to a C compiler. Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, and Mac OS X. Psyco is not supporting 64 bit, yet. But it is well being considered. The current improvements are, shortly: - Support for Python 2.4, 2.5 and 2.6 - a lot of new builtins - generators, fast and fully supported. More information is coming soon on http://www.psyco.org . This is the beginning of a series of new Psyco versions. Many more improvements are prepared and about to be published, soon, starting with the current version 2.0.0 . Stay tuned, this is just the beginning of psyco's re-birth! For questions about Psyco V2, please join the mailing list psyco-devel at lists.sourceforge.net or contact me on IRC: #psyco on irc.freenode.net . Psyco V2 is fundamentally supported by FATTOC, LLC. See http://www.fattoc.com . Without their continuous support, this work would not have been possible at all. I wish to express my deepest thanks to FATTOC, for allowing me to continue on Psyco with all the energy that this ambitious project needs, and will need. Further special thanks are going to Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, Raymond Hettinger, Fabrizio Milo, Michael Foord, Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, for all the support and discussions. Looking forward to a great future of Psyco! July 17, 2009 -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From ben+python at benfinney.id.au Thu Jul 16 22:58:48 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 12:58:48 +1000 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> Message-ID: <87ljmogglz.fsf@benfinney.id.au> Paul McGuire writes: > Two ideas come to mind, the decorator way and the metaclass way. I am > not a guru at either, but these two examples work: I think the decorator idea is most attractive to me, since it can be applied per method. > # the decorator way > def inherit_docstring_from(cls): > def docstring_inheriting_decorator(fn): > fn.__doc__ = getattr(cls,fn.__name__).__doc__ > return fn > return docstring_inheriting_decorator That works, thank you. > Using a decorator in this manner requires repeating the super class > name. Perhaps there is a way to get the bases of BarGonk, but I don't > think so, because at the time that the decorator is called, BarGonk is > not yet fully defined. Yes, I tried a few different ways, but within the decorator it seems the function object is quite unaware of what class it is destined for. -- \ ?We are human only to the extent that our ideas remain humane.? | `\ ?_Breakfast of Champions_, Kurt Vonnegut | _o__) | Ben Finney From akhilanger at gmail.com Thu Jul 16 23:26:39 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 20:26:39 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24528030.post@talk.nabble.com> Well, you were write: unintentionally I removed strip(). But the problem does not ends here: I get this error now: File "./temp.py", line 488, in main() File "./temp.py", line 475, in main for line in sys.stdin: File "/usr/local/lib/python3.1/codecs.py", line 300, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid data for this line: ? --Akhil Nobody-38 wrote: > > On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: > >>> In Python 3 you can't decode strings because they are Unicode strings >>> and it doesn't make sense to decode a Unicode string. You can only >>> decode encoded things which are byte strings. So you are mixing up byte >>> strings and Unicode strings. >> >> Then, how should I do it? >> I read a byte string from sys.stdin which needs to converted to unicode >> string for further processing. > > In 3.x, sys.stdin (stdout, stderr) are text streams, which means that they > read and write Unicode strings, not byte strings. > >> I cannot just remove the decode statement and >> proceed? >> >> This is it what it looks like: >> >> for line in sys.stdin: >> line = line.decode('utf-8').strip() >> if line == '': #do something here >> elsif #do something here >> >> If I remove the decode statement, line == '' never gets true. > > Did you inadvertently remove the strip() as well? > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24528030.html Sent from the Python - python-list mailing list archive at Nabble.com. From aahz at pythoncraft.com Thu Jul 16 23:39:39 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 20:39:39 -0700 Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: In article , Tony Lay wrote: >On Jul 16, 10:14=A0am, a... at pythoncraft.com (Aahz) wrote: >> In article <45228cf4-0b37-4c52-bf6f-d7bd124b0... at l32g2000vbp.googlegroups= >.com>, >> Tony =A0Lay =A0 wrote: >>> >>>Traceback (most recent call last): >>> =A0File "/usr/local/bin/meld", line 35, in >>> =A0 =A0import gettext >>> =A0File "/usr/local/lib/python2.6/gettext.py", line 49, in >>> =A0 =A0import locale, copy, os, re, struct, sys >>> =A0File "/usr/local/lib/python2.6/locale.py", line 15, in >>> =A0 =A0import functools >>> =A0File "/usr/local/lib/python2.6/functools.py", line 10, in >>> =A0 =A0from _functools import partial, reduce >>>ImportError: No module named _functools >> >> Where is _functools.so? > >/usr/local/lib/python2.6/lib-dynload/ > >It's in the path... >export PYTHONHOME=3D/usr/local/lib/python2.6 >export PYTHONPATH=3D/usr/local/lib/python2.6:/usr/local/lib/python2.6/ >site-packages:/usr/local/lib/python2.6/lib-dynload:/usr/local/etc/ What's in sys.path? You might also get somewhere with "python -vv". -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From aurora00 at gmail.com Thu Jul 16 23:55:11 2009 From: aurora00 at gmail.com (aurora00 at gmail.com) Date: Thu, 16 Jul 2009 20:55:11 -0700 (PDT) Subject: ctype performance benchmark Message-ID: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> I have done some performance benchmarking for Python's ctypes library. I think you may find it interesting. I am planning to use ctypes as an alternative to writing C extension module for performance enhancement. Therefore my use case is slight different from the typical use case for accessing existing third party C libraries. In this case I am both the user and the implementer of the C library. In order to determine what is the right granularity for context switching between Python and C, I have done some benchmarking. I mainly want to measure the function call overhead. So the test functions are trivial function like returning the first character of a string. I compare a pure Python function versus C module function versus ctypes function. The tests are ran under Python 2.6 on Windows XP with Intel 2.33Ghz Core Duo. First of all I want to compare the function to get the first character of a string. The most basic case is to reference it as the 0th element of a sequence without calling any function. The produce the fastest result at 0.0659 usec per loop. $ timeit "'abc'[0]" 10000000 loops, best of 3: 0.0659 usec per loop As soon as I build a function around it, the cost goes up substantially. Both pure Python and C extension method shows similar performance at around 0.5 usec. ctypes function takes about 2.5 times as long at 1.37 usec. $ timeit -s "f=lambda s: s[0]" "f('abc')" 1000000 loops, best of 3: 0.506 usec per loop $ timeit -s "import mylib" "mylib.py_first('abc')" 1000000 loops, best of 3: 0.545 usec per loop $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')" "dll.first('abc')" 1000000 loops, best of 3: 1.37 usec per loop I repeated the test with a long string (1MB). There are not much difference in performance. So I can be quite confident that the parameter is passed by reference (of the internal buffer). $ timeit -s "f=lambda s: s[0]; lstr='abcde'*200000" "f(lstr)" 1000000 loops, best of 3: 0.465 usec per loop $ timeit -s "import mylib; lstr='abcde'*200000" "mylib.py_first(lstr)" 1000000 loops, best of 3: 0.539 usec per loop $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')" -s "lstr='abcde'*200000" "dll.first(lstr)" 1000000 loops, best of 3: 1.4 usec per loop Next I have make some attempts to speed up ctypes performance. A measurable improvement can be attained by eliminating the attribute look up for the function. Curiously this shows no improvement in the similar case for C extension. $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd'); -s "f=dll.first" "f('abcde')" 1000000 loops, best of 3: 1.18 usec per loop Secondary I have tried to specify the ctypes function prototype. This actually decrease the performance significantly. $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')" -s "f=dll.first" -s "f.argtypes=[ctypes.c_char_p]" -s "f.restype=ctypes.c_int" "f('abcde')" 1000000 loops, best of 3: 1.57 usec per loop Finally I have tested passing multiple parameters into the function. One of the parameter is passed by reference in order to return a value. Performance decrease as the number of parameter increase. $ timeit -s "charAt = lambda s, size, pos: s[pos]" -s "s='this is a test'" "charAt(s, len(s), 1)" 1000000 loops, best of 3: 0.758 usec per loop $ timeit -s "import mylib; s='this is a test'" "mylib.py_charAt(s, len(s), 1)" 1000000 loops, best of 3: 0.929 usec per loop $ timeit -s "import ctypes" -s "dll = ctypes.CDLL('mylib.pyd')" -s "s='this is a test'" -s "ch = ctypes.c_char()" "dll.charAt(s, len(s), 1, ctypes.byref(ch))" 100000 loops, best of 3: 2.5 usec per loop One style of coding that improve the performance somewhat is to build a C struct to hold all the parameters. $ timeit -s "from test_mylib import dll, charAt_param" -s "s='this is a test'" -s "obj = charAt_param(s=s, size=len(s), pos=3, ch='')" "dll.charAt_struct(obj)" 1000000 loops, best of 3: 1.71 usec per loop This may work because most of the fields in the charAt_param struct are invariant in the loop. Having them in the same struct object save them from getting rebuilt each time. My overall observation is that ctypes function has an overhead that is 2 to 3 times to a similar C extension function. This may become a limiting factor if the function calls are fine grained. Using ctypes for performance enhancement is a lot more productive if the interface can be made to medium or coarse grained. A snapshot of the source code used for testing is available for download http://tungwaiyip.info/blog/2009/07/16/ctype_performance_benchmark Wai Yip Tung From aurora00 at gmail.com Fri Jul 17 00:03:14 2009 From: aurora00 at gmail.com (Wai Yip) Date: Thu, 16 Jul 2009 21:03:14 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: I think Diez' example show this work automatically in Unix. In my case I use Windows. I use the multiprocessing.Array to share data in shared memory. multiprocessing.Array has a limitation that it can only reference simple C data types, not Python objects though. Wai Yip Tung From crazypangolin at gmail.com Fri Jul 17 00:44:26 2009 From: crazypangolin at gmail.com (aj) Date: Thu, 16 Jul 2009 21:44:26 -0700 (PDT) Subject: error when compiling source on linux Message-ID: <1c8d7530-873b-4f52-8d0f-21a6b79de339@p9g2000vbl.googlegroups.com> when I try to compile Python 2.6 from source code on ubuntu, I get the message /usr/bin/ld: cannot open output file python: Is a directory collect2: ld returned 1 exit status make: *** [python] Error 1 PLEASE HELP! From aahz at pythoncraft.com Fri Jul 17 01:08:31 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 22:08:31 -0700 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: In article , Gabriel Rossetti wrote: > >I am using threading.Condition.wait(timeout) and was surprised to see >that there is no return value nor an exception when wait() is used w/ a >timeout. How am I supposed to know if it was notified or if it timed out? What are you trying to do? Maybe your purpose would be better suited to using a Queue? (Yes, Queue is a hammer in search of a nail, but most threading problems are easier if you treat them as nails.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From alan.isaac at gmail.com Fri Jul 17 01:19:50 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 17 Jul 2009 05:19:50 GMT Subject: invoke method on many instances Message-ID: As a recurrent situation, I need to invoke the same method on many instances. Speed matters, but the solution should be pure Python. Is the following convenience function a reasonable approach? def apply2(itr, methodname, *args, **kwargs): f = operator.methodcaller(methodname, *args, **kwargs) for item in itr: f(item) (Comment: in the case at hand, the methods return None.) Thank you, Alan Isaac From contact at xavierho.com Fri Jul 17 01:31:28 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 13:31:28 +0800 Subject: Try... except....Try again? Message-ID: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> I have a simple class that generates prime numbers, using memorisation and iteration to generate the next prime number. In the class, I have defined a function that gives, say, the 5th prime number. Or the 1000th, it's still very fast. But the code isn't what I really like. def nPrime(self, n): "Returns nth prime number, the first one being 2, where n = 0. When n = 1, it returns 3." while True: try: return self.primes[n] except: self.next() The next() function generates the next prime, and appends it into primes. This way, it keeps trying to append until the nth prime requested exist, and returns it. My problem is that it's a "While True" loop, which I get a lot of "Don't do it!" In the light of making the code better, what could I do? Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From gabriel.rossetti at arimaz.com Fri Jul 17 02:12:38 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 17 Jul 2009 08:12:38 +0200 Subject: no return value for threading.Condition.wait(timeout)? In-Reply-To: <26ddd1750907161422n5b640a48pbbf9325dd05a699d@mail.gmail.com> References: <26ddd1750907161422n5b640a48pbbf9325dd05a699d@mail.gmail.com> Message-ID: <4A601656.1070802@arimaz.com> Maxim Khitrov wrote: > On Thu, Jul 16, 2009 at 5:00 PM, Carl Banks wrote: > >> On Jul 16, 8:12 am, Gabriel Rossetti >> wrote: >> >>> Hello everyone, >>> >>> I am using threading.Condition.wait(timeout) and was surprised to see >>> that there is no return value nor an exception when wait() is used w/ a >>> timeout. How am I supposed to know if it was notified or if it timed out? >>> >> That's a good question. Condition.wait seems to violate an invariant >> if it can return without having acquired the underlying lock. And if >> it does the least it could do is to return a status so you know if you >> have to skeddadle. >> >> Workaround is to use the _is_owned() method of Condition to see if you >> are the owner of the condition, but this method is undocumented. >> >> cond = Condition() >> ... >> cond.acquire() >> while not ok_to_proceed(): >> cond.wait() >> if not cond._is_owned(): >> # must've timed out >> raise TimeOutException >> operate() >> cond.release() >> > > You will always be the owner of the condition whether it times out or > your thread is notified. This doesn't solve the problem. As an aside, > the implementation of wait() is rather poor in any case; the sleep > function should not be used for the purpose of waiting for an event. > > Depending on what platform you are using, I can offer two solutions. > The simplest one is to subclass Condition and override the wait > method. Just copy the code as it is, but modify the end of the > function to look like this: > > if not gotit: > if __debug__: > self._note("%s.wait(%s): timed out", self, timeout) > try: > self.__waiters.remove(waiter) > except ValueError: > pass > return False > else: > if __debug__: > self._note("%s.wait(%s): got it", self, timeout) > return True > > The two return statements will tell you whether you timed out ("if not > gotit" condition), or were notified ("else"). > > The better way to solve this and the other problem that I mentioned is > to use native OS events. This is what I had to do for a recent > project. I used pywin32 extension to completely rewrite Event, > Condition, and Timer classes to use a true event-based approach. In > the process of the rewrite I also modified some functionality, such as > returning True of False from wait(). > > Unfortunately, I'm not able to post this code right now, though I can > try and get permission to do so a bit later. If you are writing > software for Windows, then simply take a look at win32event module. > CreateEvent, SetEvent, ResetEvent, WaitForSingleObject are all the > Windows functions that you need to reimplement threading.Event. Using > the new event class you can implement a proper Condition.wait() method > (hint: use new events rather than locks to wait for notification). > > If you are on Linux or BSD, I can't help you. I'm sure that there is > some equivalent functionality that you may be able to access using > ctypes. Exactly how to do it, however, is not something that I can > help you with right now. > > - Max > Thank you, I will try that. I am coding for Linux, Mac & Windows (and any POSIX platform, but right now I support only those three). Gabriel From koranthala at gmail.com Fri Jul 17 02:13:51 2009 From: koranthala at gmail.com (koranthala) Date: Thu, 16 Jul 2009 23:13:51 -0700 (PDT) Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> > That test was designed to treat None as a boolean False, without > noticing that numeric 0 is also treated as False and could make the > test do the wrong thing. This is an extremely common type of error. Actually, I felt that 0 not being considered False would be a better option. I had lot of instances where 0 is a valid value and always I had to put checks specifically for that. For me, None and False should be equal to False in if checks, every thing else being considered True. Can someone let me know why 0 is considered equal to False? There should be real good reasons for it, only that I cannot think of one. From gabriel.rossetti at arimaz.com Fri Jul 17 02:25:23 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 17 Jul 2009 08:25:23 +0200 Subject: no return value for threading.Condition.wait(timeout)? In-Reply-To: References: Message-ID: <4A601953.5000603@arimaz.com> Piet van Oostrum wrote: >>>>>> Gabriel Rossetti (GR) wrote: >>>>>> > > >> GR> Hello everyone, >> GR> I am using threading.Condition.wait(timeout) and was surprised to see that >> GR> there is no return value nor an exception when wait() is used w/ a timeout. >> GR> How am I supposed to know if it was notified or if it timed out? >> > > Normally you wouldn't have to know. The logic of your program should be > such that you wait until a certain condition is satisfied. After each > wait you usually check that condition anyway, like: > > while (some_condition_satisfied): > cond_var.wait() > > The only exception could be if there is a 1-1 relation between a single > waiter and a single notifier. I that case you can usually use if instead > of while (but it does depend of the logic of the notifier). > I have a 1-1 relation, I have a thread reading msgs from a network socket and a method waiting for an answer to arrive (see my thread "Threading.Condition problem"). I would like to be able to have a timeout as to not block forever, so the idea is to check if I returned because of a timeout or not. > So in case of using a timeout you can check the desired condition after > the wait and decide what to do. If the condition was not satisfied you > probably want to do something special, like error handling, or do > something else first and then retry the wait. > > There could be situations where this doesn't work, however. For example > if you wait until a queue is not empty, and the notify is done when > something is inserted in the queue, another thread may sneak in while > you are waiting and snatch the inserted item just before your thread > continues after the wait. In that case you can't distinguish between a > timeout and a snatcher. > > So it all depends on what your use case is. > > (Java's wait doesn't return anything either.) > From __peter__ at web.de Fri Jul 17 02:28:46 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 17 Jul 2009 08:28:46 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Terry Reedy wrote: > Terry Reedy wrote: > >>> $ file tmp.ps >>> tmp.ps: PostScript document text conforming DSC level 3.0, type EPS >>> >>> Try changing the file extension from .ps to .eps. >> >> I will. Thank you. > > I tried it. Unfortunately, OOo does not open it correctly. It just > displays the first three lines of metadate - Title, Creator, Date -- as > image text. Photoshop does read the image, and does an ok job of > conversion once anti-aliasing is turned off. I snatched some code from the module and modified it to save a sample image: from turtle import * def switchpen(): if isdown(): pu() else: pd() def demo2(): """Demo of some new features.""" speed(1) st() pensize(3) setheading(towards(0, 0)) radius = distance(0, 0)/2.0 rt(90) for _ in range(18): switchpen() circle(radius, 10) write("wait a moment...") while undobufferentries(): undo() reset() lt(90) colormode(255) laenge = 10 pencolor("green") pensize(3) lt(180) for i in range(-2, 16): if i > 0: begin_fill() fillcolor(255-15*i, 0, 15*i) for _ in range(3): fd(laenge) lt(120) laenge += 10 lt(15) speed((speed()+1)%12) end_fill() lt(120) pu() fd(70) rt(30) pd() color("red","yellow") speed(0) fill(1) for _ in range(4): circle(50, 90) rt(90) fd(30) rt(90) fill(0) lt(90) pu() fd(30) pd() shape("turtle") tri = getturtle() tri.resizemode("auto") turtle = Turtle() turtle.resizemode("auto") turtle.shape("turtle") turtle.reset() turtle.left(90) turtle.speed(0) turtle.up() turtle.goto(280, 40) turtle.lt(30) turtle.down() turtle.speed(6) turtle.color("blue","orange") turtle.pensize(2) tri.speed(6) setheading(towards(turtle)) count = 1 while tri.distance(turtle) > 4: turtle.fd(3.5) turtle.lt(0.6) tri.setheading(tri.towards(turtle)) tri.fd(4) if count % 20 == 0: turtle.stamp() tri.stamp() switchpen() count += 1 tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right") if __name__ == "__main__": demo2() Screen().getcanvas().postscript(file="demo2.eps") I could successfully insert the picture into Writer. I'm on Kubuntu 9.04 with Python 2.6.2 and OpenOffice 3.0.1. > Hmmm. Crazy idea... in > http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle- PyCon2007.pdf > Gregor Lingl says that turtle.py has been ported to pygame and jython as > back ends. It should be possible to instead send output to a file > instead of an on-screen canvas. Run a program to screen. When output > looks right, return with a different parameter to send to file. > > For instance, an OpenDocumentDrawing file (.odd) using odfpy. > This assumes that there are elements corresponding to each turtle > command. Or at least that the fit is close enough. I can see nothing crazy about that. But still, turtle is an educational tool, its main advantage is that it can show beginners how the image is generated. If you want to generate high-quality graphics easily you need different primitives. http://cairographics.org has Python bindings, but I don't know if it's available on Windows. Peter From clp2 at rebertia.com Fri Jul 17 02:30:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 23:30:39 -0700 Subject: Einstein summation notation (was: question of style) In-Reply-To: <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <50697b2c0907162330m60327179j6f7fcf0427beac3c@mail.gmail.com> On Thu, Jul 16, 2009 at 11:13 PM, koranthala wrote: >> That test was designed to treat None as a boolean False, without >> noticing that numeric 0 is also treated as False and could make the >> test do the wrong thing. ?This is an extremely common type of error. > > Actually, I felt that 0 not being considered False would be a better > option. > I had lot of instances where 0 is a valid value and always I had to > put checks specifically for that. > For me, None and False should be equal to False in if checks, every > thing else being considered True. > > Can someone let me know why 0 is considered equal to False? > There should be real good reasons for it, only that I cannot think of > one. * Because that's how C does/did it (primary motivation) - Because if Boolean algebra is implemented using numbers, False is 0 - Because philosophically, 0 is the "empty" or null value for numbers, and empty values are by convention considered false in Python - Because it's the behavior people want 75% of the time. FWIW, your paradigm of true/false is used by Lisp and Lisp wannabes such as Ruby. Cheers, Chris -- http://blog.rebertia.com From __peter__ at web.de Fri Jul 17 02:58:40 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 17 Jul 2009 08:58:40 +0200 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > Ideally there would be a way to specify that the docstring should be > inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > > but that violates DRY (the association between BarGonk and FooGonk is > being repeated), puts the docstring assignment awkwardly after the end > of the method instead of at the beginning where docstrings normally go, > and reads poorly besides. > > What is the most Pythonic, DRY-adherent, and preferably least-ugly > approach to override a method, but have the same docstring on both > methods? Just thinking aloud: Write a patch for pydoc that looks up the base-class documentation. B.f.__doc__ will continue to return None, but help(B.f) will show something like No documentation available for B.f. Help for A.f: yadda yadda Of course that might be misleading when A.f and B.f are up to something completely different... Peter From nobody at nowhere.com Fri Jul 17 03:20:15 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 17 Jul 2009 08:20:15 +0100 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: On Thu, 16 Jul 2009 20:26:39 -0700, akhil1988 wrote: > Well, you were write: unintentionally I removed strip(). But the problem does > not ends here: > > I get this error now: > > File "./temp.py", line 488, in > main() > File "./temp.py", line 475, in main > for line in sys.stdin: > File "/usr/local/lib/python3.1/codecs.py", line 300, in decode > (result, consumed) = self._buffer_decode(data, self.errors, final) > UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid > data > > for this line: > ? Right. You're running in a locale whose encoding is UTF-8, but feeding data which isn't valid UTF-8 to stdin. If you want to use data with a different encoding, you need to replace sys.stdin, e.g.: import sys import io sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding = 'iso-8859-1') From Lacrima.Maxim at gmail.com Fri Jul 17 03:24:48 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Fri, 17 Jul 2009 00:24:48 -0700 (PDT) Subject: urllib with x509 certs References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> <4a4f2307$0$8384$9b622d9e@news.freenet.de> <4A4F387F.7050804@v.loewis.de> Message-ID: Hello! I've solved this problem, using pyCurl. Here is sample code. import pycurl import StringIO b = StringIO.StringIO() c = pycurl.Curl() url = 'https://example.com/' c.setopt(pycurl.URL, url) c.setopt(pycurl.WRITEFUNCTION, b.write) c.setopt(pycurl.CAINFO, 'cert.crt') c.setopt(pycurl.SSLKEY, 'mykey.key') c.setopt(pycurl.SSLCERT, 'mycert.cer') c.setopt(pycurl.SSLKEYPASSWD , 'pass phrase') c.perform() This also allow to specify CA, so your requests are more secure then with urllib. With regards, Max. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:28:32 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:28:32 GMT Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <02701951$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 23:13:51 -0700, koranthala wrote: >> That test was designed to treat None as a boolean False, without >> noticing that numeric 0 is also treated as False and could make the >> test do the wrong thing. This is an extremely common type of error. > > Actually, I felt that 0 not being considered False would be a better > option. > I had lot of instances where 0 is a valid value and always I had to put > checks specifically for that. > For me, None and False should be equal to False in if checks, every > thing else being considered True. I believe it would be a mistake for None == False to return True. As far as having only None and False to be considered equivalent in truth contexts, that's awfully limiting. It is very useful to be able to write e.g.: if header or body or footer: print assemble_page(header, body, footer) and have empty strings to be equivalent to False. Lisp and (I think) Ruby behaves as you want. Python doesn't. We think it's Lisp and Ruby that got it wrong :) > Can someone let me know why 0 is considered equal to False? There should > be real good reasons for it, only that I cannot think of one. Having 0 considered false goes back to branching in machine language, which I understand are typically. The Null value (e.g. nil in Pascal, nul in C) are usually implemented as pointer values equal to an address of zero. It essentially boils down to this: the behaviour of if...else is a convention. The language designer might choose to have an explicit Boolean type, and prohibit "if None" (e.g. Pascal does this). It might choose to treat flags as integers, and branch according to the integer being zero (false) or non-zero (true) (e.g. Forth does this). Python allows any object to be tested by if, and branches according to whether it is Something or Nothing. It's a logical dichotomy, and treating 0 and [] as Something instead of Nothing would make it arbitrary and strange. -- Steven From http Fri Jul 17 03:34:00 2009 From: http (Paul Rubin) Date: 17 Jul 2009 00:34:00 -0700 Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> Message-ID: <7xljmnahlj.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > It is very useful to be able to write e.g.: > if header or body or footer: > print assemble_page(header, body, footer) > and have empty strings to be equivalent to False. Why doesn't assemble_page properly handle the case where header, body, and footer are all empty? That would let you eliminate the if. "Make sure your code 'does nothing' gracefully" (Kernighan and Plauger). From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:45:08 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:45:08 GMT Subject: invoke method on many instances References: Message-ID: <02701d35$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: > As a recurrent situation, I need to invoke the same method on many > instances. Speed matters, but the solution should be pure Python. Is > the following convenience function a reasonable approach? > > def apply2(itr, methodname, *args, **kwargs): > f = operator.methodcaller(methodname, *args, **kwargs) > for item in itr: > f(item) I don't particularly like your naming conventions, but never mind. You could also try this: for obj in objects: getattr(obj, methodname)(*args, **kwargs) See also these recipes from the "Python Cookbook": http://code.activestate.com/recipes/52289/ http://code.activestate.com/recipes/87370/ -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:52:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:52:19 GMT Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> <87ljmogglz.fsf@benfinney.id.au> Message-ID: <02701ee5$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 12:58:48 +1000, Ben Finney wrote: >> Using a decorator in this manner requires repeating the super class >> name. Perhaps there is a way to get the bases of BarGonk, but I don't >> think so, because at the time that the decorator is called, BarGonk is >> not yet fully defined. > > Yes, I tried a few different ways, but within the decorator it seems the > function object is quite unaware of what class it is destined for. When the decorator is called, the function object is just a function object, not a method, so there is no concept of "what class it is destined for". >>> def dec(func): ... print type(func) ... try: ... print func.im_class ... except: ... print "no im_class" ... return func ... >>> class Test(object): ... @dec ... def spam(self): ... pass ... no im_class >>> type(Test.spam) >>> Test.spam.im_class I suppose you could try to determine what namespace you're currently when the class is created, but that's surely going to be fragile and messy. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:56:07 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:56:07 GMT Subject: TypeError: unbound method References: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> Message-ID: <02701fc9$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 18:08:45 -0700, Chris Rebert wrote: > You're trying to call an instance method on the class itself, which > doesn't make sense. Oh I don't know, people do it all the time -- just not the way the OP did :) > You need to first create an instance of the class to invoke the method > on. i.e.: > > instance = TheClass(constuctor_arguments_here) > var1 = instance.method() Or explicitly pass an instance to the method when calling from the class: TheClass.method(TheClass(constuctor_arguments_here)) Which strangely enough, is useful at times. class K(parent): def method(self, args): x = parent.method(self, args) return x + 1 -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 04:06:22 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 08:06:22 GMT Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: <0270222f$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 11:02:57 -0500, Pablo Torres N. wrote: > On Thu, Jul 9, 2009 at 22:07, Steven > D'Aprano wrote: >> On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: >> >>> def list_items_in_string(list_items, string): >>> ? ? for item in list_items: >>> ? ? ? ? if item in string: >>> ? ? ? ? ? ? return True >>> ? ? return False >> ... >>> Any ideas how to make that function look nicer? :) >> >> Change the names. Reverse the order of the arguments. Add a docstring. >> >> > Why reverse the order of the arguments? Is there a design principle > there? It's just a convention. Before strings had methods, you used the string module, e.g.: string.find(source, target) => find target in source This became source.find(target). In your function: list_items_in_string(list_items, string) "list_items" is equivalent to target, and "string" is equivalent to source. It's conventional to write the source first. -- Steven From piet at cs.uu.nl Fri Jul 17 04:12:52 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:12:52 +0200 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: >>>>> koranthala (k) wrote: >>> That test was designed to treat None as a boolean False, without >>> noticing that numeric 0 is also treated as False and could make the >>> test do the wrong thing. This is an extremely common type of error. >k> Actually, I felt that 0 not being considered False would be a better >k> option. >k> I had lot of instances where 0 is a valid value and always I had to >k> put checks specifically for that. >k> For me, None and False should be equal to False in if checks, every >k> thing else being considered True. >k> Can someone let me know why 0 is considered equal to False? >k> There should be real good reasons for it, only that I cannot think of >k> one. Originally Python didn't have booleans. Integers were used instead (but not exclusively). 0 was False, everything else True (with a slight preference for 1 as True). Same as in C. Nowadays this is reflected in bool being a subtype of int with False == 0 and True == 1. Actually it is even closer: False and True are just 0 and 1 cast to bool, so to say. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From oisin.mulvihill at gmail.com Fri Jul 17 04:14:45 2009 From: oisin.mulvihill at gmail.com (Oisin Mulvihill) Date: Fri, 17 Jul 2009 09:14:45 +0100 Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... Message-ID: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> Hi There, I gave a lightning talk about my ServiceStation project at Europython 2009. While it isn't written in Python (C++/Windows), I did write it with python in mind. Its released under the CDDL license. I'm hoping it will be of use to other Python users who will need to run their services on windows, but don't want to learn much/anything about windows service programming. The ServiceStation blurb is: ServiceStation allows you to run arbitrary programs as a service on the Windows platform. The program you wish to run does not need to be changed to allow it to work with ServiceStation or windows services. This project was developed with an eye to running Python web services on Windows, without the need to use and include Pywin32. This meant we could take services running on Linux/Mac and run them unmodified on Windows." If your interested in this check out the projects trac page for docs and download http://www.foldingsoftware.com/servicestation All the best, Oisin From piet at cs.uu.nl Fri Jul 17 04:26:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:26:27 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: There is stil something not clear in your description. >m> I'm using multiprocessing to spawn several subprocesses, each of which >m> uses a very large data structure (making it impractical to pass it via >m> pipes / pickling). I need to allocate this structure once when the >m> process is created and have it remain in memory for the duration of >m> the process. I have read this as that every subprocess has its own large data structure and that there is no connection between these. But seeing where the discussion is going I guess there might be different interpretations. So can you enlighten us how the situation is? 1. Each subprocess has a copy of a data structure that is prepared by the master process. Therefore you want it to be passed by the fork 1a. the data structure is constant i.e. the subprocess doesn't change it 1b. the subprocess makes changes in its copy 2. Each subprocess has a seperate data structure not equal to the others 3. Something else. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mail at timgolden.me.uk Fri Jul 17 04:34:27 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 17 Jul 2009 09:34:27 +0100 Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... In-Reply-To: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> References: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> Message-ID: <4A603793.3070803@timgolden.me.uk> Oisin Mulvihill wrote: > Hi There, > > I gave a lightning talk about my ServiceStation project at Europython > 2009. While it > isn't written in Python (C++/Windows), I did write it with python in > mind. Its > released under the CDDL license. > > I'm hoping it will be of use to other Python users who will need to run > their services > on windows, but don't want to learn much/anything about windows service > programming. > If your interested in this check out the projects trac page for docs and download > http://www.foldingsoftware.com/servicestation It's not clear at a glance how this differs from, say, the venerable SRVANY.EXE and a slew of other "Run-things-as-a-service" programs. Absolutely not wishing to be disparaging, but what is servicestation offering to distinguish itself? TJG From stefan_ml at behnel.de Fri Jul 17 04:35:54 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 17 Jul 2009 10:35:54 +0200 Subject: ctype performance benchmark In-Reply-To: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> Message-ID: <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> aurora00 at gmail.com wrote: > My overall observation is that ctypes function has an overhead that is > 2 to 3 times to a similar C extension function. This may become a > limiting factor if the function calls are fine grained. Using ctypes > for performance enhancement is a lot more productive if the interface > can be made to medium or coarse grained. I think ctypes is ok for its niche: easy interfacing with C stuff from straight Python code, without further dependencies. There will always (and necessarily) be a call overhead involved. Anyone who needs to care about per-call performance would use something else anyway (do I need to mention Cython here?) Stefan From piet at cs.uu.nl Fri Jul 17 04:37:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:37:27 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> I have a 1-1 relation, I have a thread reading msgs from a network socket >GR> and a method waiting for an answer to arrive (see my thread >GR> "Threading.Condition problem"). I would like to be able to have a timeout >GR> as to not block forever, so the idea is to check if I returned because of a >GR> timeout or not. So that case is easy I think. After the wait just check if the answer has arrived. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Fri Jul 17 04:47:36 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:47:36 +0200 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: >>>>> akhil1988 (a) wrote: >a> Well, you were write: unintentionally I removed strip(). But the problem does >a> not ends here: >a> I get this error now: >a> File "./temp.py", line 488, in >a> main() >a> File "./temp.py", line 475, in main >a> for line in sys.stdin: >a> File "/usr/local/lib/python3.1/codecs.py", line 300, in decode >a> (result, consumed) = self._buffer_decode(data, self.errors, final) >a> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid >a> data >a> for this line: >a> ? Your Python assumes stdin uses utf-8 encoding, probably because your locale says so. But it seems the input is not really utf-8 but some other encoding. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From oisin.mulvihill at gmail.com Fri Jul 17 05:13:49 2009 From: oisin.mulvihill at gmail.com (Oisin Mulvihill) Date: Fri, 17 Jul 2009 02:13:49 -0700 (PDT) Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... References: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> Message-ID: <42b910db-7e71-4f26-81ed-ee6a2f29ca56@26g2000yqk.googlegroups.com> On Jul 17, 9:34?am, Tim Golden wrote: > Oisin Mulvihill wrote: > > Hi There, > > > I gave a lightning talk about my ServiceStation project at Europython > > 2009. While it > > isn't written in Python (C++/Windows), I did write it with python in > > mind. Its > > released under the CDDL license. > > > I'm hoping it will be of use to other Python users who will need to run > > their services > > on windows, but don't want to learn much/anything about windows service > > programming. > > If your interested in this check out the projects trac page for docs and download > > ?http://www.foldingsoftware.com/servicestation > > It's not clear at a glance how this differs from, say, the > venerable SRVANY.EXE and a slew of other "Run-things-as-a-service" > programs. Absolutely not wishing to be disparaging, but what > is servicestation offering to distinguish itself? > > TJG Hi There, I have to admit I didn't come across srvany before. I may not have used the right google fu the day I decided to do this ;) So maybe it does all the following, however servicestation distingusihes itself by: * Properly tracking all child processes launched by the command it runs and closes them on stop/restart. * Monitoring the command its running and keeping it alive. * Allows you to set the description / name from the config file. * It logs useful information to the event viewer so you can see why it couldn't run the command under its care. * Can interact with the desktop or not so you can run programs with a gui but not actually see it. * Does not disconnect the service if you log-out as some runners do. Thats all I can think of. I should probably add this to this wiki too, Thanks for prompting me on this. Oisin From peter.fodrek at stuba.sk Fri Jul 17 05:24:59 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Fri, 17 Jul 2009 11:24:59 +0200 Subject: error when compiling source on linux In-Reply-To: <1c8d7530-873b-4f52-8d0f-21a6b79de339@p9g2000vbl.googlegroups.com> References: <1c8d7530-873b-4f52-8d0f-21a6b79de339@p9g2000vbl.googlegroups.com> Message-ID: <200907171124.59610.peter.fodrek@stuba.sk> On Friday 17 July 2009 06:44:26 aj wrote: > when I try to compile Python 2.6 from source code on ubuntu, I get the > message > /usr/bin/ld: cannot open output file python: Is a directory This says that there is directory in the sources with same name as will be named output It can only occur when you are not compiling software correctly via INSTALL or README file I think you made ./configure make as compiling process if this is true try mkdir bulid cd build ../configure make make install Kind regards Peter From mail at timgolden.me.uk Fri Jul 17 05:26:09 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 17 Jul 2009 10:26:09 +0100 Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... In-Reply-To: <42b910db-7e71-4f26-81ed-ee6a2f29ca56@26g2000yqk.googlegroups.com> References: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> <42b910db-7e71-4f26-81ed-ee6a2f29ca56@26g2000yqk.googlegroups.com> Message-ID: <4A6043B1.8010500@timgolden.me.uk> Oisin Mulvihill wrote: > I have to admit I didn't come across srvany before. It's been part of the Windows Resource Kit since way back, but like many such things is probably known more by word-of-mouth than by Internet Searchability > * Properly tracking all child processes launched by the command it > runs and closes them on stop/restart. > > * Monitoring the command its running and keeping it alive. > > * Allows you to set the description / name from the config file. > > * It logs useful information to the event viewer so you can see why > it couldn't run the command under its care. > > * Can interact with the desktop or not so you can run programs with a > gui but not actually see it. > > * Does not disconnect the service if you log-out as some runners do. Looks very interesting. Definitely put this on the front page, otherwise people may think as I did that this is just another srvany clone. TJG From hartley79 at gmail.com Fri Jul 17 05:59:46 2009 From: hartley79 at gmail.com (hartley) Date: Fri, 17 Jul 2009 02:59:46 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> Message-ID: <94c1adf3-a25d-4ae5-9f38-7ca8680d022d@b14g2000yqd.googlegroups.com> On Jul 16, 9:26?pm, a... at pythoncraft.com (Aahz) wrote: > In article <0afc5c4d-1af5-4d0e-9442-26b51b12e... at m11g2000yqh.googlegroups.com>, > > hartley ? wrote: > > >If you had loosened up on the sarcasm I would probably have read what > >you wrote more thoroughly instead of just skimming through it. Thanks > >for the help, but you should seriously consider doing something with > >your patronizing attitude. > > http://www.mikeash.com/getting_answers.htmlhttp://www.catb.org/~esr/faqs/smart-questions.html >From "http://www.mikeash.com/getting_answers.html": How To Ask Questions The Smart Way is pretty condescending, especially if you already feel insulted by somebody telling you that you need help asking questions. If you're pointed at a guide with a filename of smart-questions, that means this person thinks you have stupid questions, and who needs that? Cheers mate :) -Hartley From ben+python at benfinney.id.au Fri Jul 17 06:03:29 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 20:03:29 +1000 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <87zlb3fwy6.fsf@benfinney.id.au> Peter Otten <__peter__ at web.de> writes: > Just thinking aloud: Write a patch for pydoc that looks up the > base-class documentation. That doesn't scale; I want the docstring to be discovered by the normal interface (the ?__doc__? attribute) by *every* tool that gathers docstrings from methods. Also, I want to have this behaviour not for every method missing a docstring, but only for selected methods. > Of course that might be misleading when A.f and B.f are up to > something completely different... Exactly. -- \ ?Facts do not cease to exist because they are ignored.? ?Aldous | `\ Huxley | _o__) | Ben Finney From sion at viridian.paintbox Fri Jul 17 06:20:34 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Fri, 17 Jul 2009 10:20:34 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: Jack Diederich wrote: >It isn't an OrderedDict thing, it is a comparison thing. Two regular >dicts also raise an error if you try to LT them. Since when? Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = dict((str(i), i) for i in range (10)) >>> d2 = dict((str(i), i) for i in range (20)) >>> d1 < d2 True >>> (Don't have a 2.6 or 3 to hand.) Mind you, it makes even less sense for a regular dict than an OrderedDict. And it's not like d1.items() < d2.items() is a huge burden, if that's what you want dictionary comparison to mean. -- \S under construction From jeanmichel at sequans.com Fri Jul 17 07:06:26 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 17 Jul 2009 13:06:26 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <1247774222.4a5f860ec3cfa@mail.uh.cu> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <4A5F5759.4010600@sequans.com> <1247774222.4a5f860ec3cfa@mail.uh.cu> Message-ID: <4A605B32.5080306@sequans.com> Luis Alberto Zarrabeitia Gomez wrote: > Quoting Jean-Michel Pichavant : > > >> Emile van Sebille wrote: >> >>> On 7/16/2009 7:04 AM Unknown said... >>> >>>> On 2009-07-16, Emile van Sebille wrote: >>>> >>>>> daysInAdvance = int(inputVar) or 25 >>>>> >>>> I don't get it. That doesn't work right when inputVar == "0". >>>> >>>> >>> Aah, but you didn't get to define right. :) For that particular >>> example 0 is not a valid response. >>> >> When I was talking about such error prone form of boolean operations, I >> didn't expect to be right so quickly :p >> > > What do you mean by being "right so quickly", and "error prone" in this context? > I would also ask "Unknown" why he believes that "int(intputVar) or 25" doesn't > work right when inputVar == "0". The only false value that int() may return is > zero, so the "or 25" clause is there only for that case. I can't see then how > you think that is an error. > I was saying that using boolean operators with object instead of boolean values is error prone, cause no language behaves he same way, and all behaviors are conventions difficult to figure out without diving deeply into the documentation (or being explained as it happened to me). I think the initialization trick is an error, because I don't want foo(0) to set daysInAdvance to 25. I'll want it to set the attribute to 0, cause 0 is a valid integer. 0 is a valid integer content, None wouldn't be a valid integer content. JM From bearophileHUGS at lycos.com Fri Jul 17 07:11:10 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 17 Jul 2009 04:11:10 -0700 (PDT) Subject: ANN: psyco V2 References: Message-ID: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> Very good, thank you. I'll try it when I can. Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow? The problem I have with Psyco1.6 is that you can't use the normal profilers to know how much seconds of running time is taken by each function/method of your code. Psyco1.6 has a profile() function, but I am not much able to use it yet. Can you tell me how to find a sorted list of the running time of all functions/methods of a Psyco-digested program? Bye and thank you, bearophile From andreengels at gmail.com Fri Jul 17 07:33:03 2009 From: andreengels at gmail.com (Andre Engels) Date: Fri, 17 Jul 2009 13:33:03 +0200 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> Message-ID: <6faf39c90907170433y7258eebcyeab03ff3021aaa8a@mail.gmail.com> On Fri, Jul 17, 2009 at 7:31 AM, Xavier Ho wrote: > I have a simple class that generates prime numbers, using memorisation and > iteration to generate the next prime number. > > In the class, I have defined a function that gives, say, the 5th prime > number. Or the 1000th, it's still very fast. But the code isn't what I > really like. [...] > My problem is that it's a "While True" loop, which I get a lot of "Don't do > it!" In the light of making the code better, what could I do? Where do you get these "Don't do it!"s? As far as I know, "while true" loops are a very acceptable idiom for programming in Python (and several other languages). -- Andr? Engels, andreengels at gmail.com From contact at xavierho.com Fri Jul 17 07:38:55 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 19:38:55 +0800 Subject: Try... except....Try again? In-Reply-To: <6faf39c90907170433y7258eebcyeab03ff3021aaa8a@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <6faf39c90907170433y7258eebcyeab03ff3021aaa8a@mail.gmail.com> Message-ID: <2d56febf0907170438l6e38a503g74c3e0256404bb58@mail.gmail.com> On Fri, Jul 17, 2009 at 7:33 PM, Andre Engels wrote: > > Where do you get these "Don't do it!"s? As far as I know, "while true" > loops are a very acceptable idiom for programming in Python (and > several other languages). > Hey Andre, Friends, internet, common sense. Also... "while True" has a bad reputation. But mostly outside influences. But if it's okay, it's okay by me.. I can't think of an easier way to code the "try loop" above, anyhow. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From Larry.Martell at gmail.com Fri Jul 17 08:07:04 2009 From: Larry.Martell at gmail.com (Larry.Martell at gmail.com) Date: Fri, 17 Jul 2009 05:07:04 -0700 (PDT) Subject: setup.py not found Message-ID: <3be2bdce-680d-4b32-ad0c-ef46caf556e9@f10g2000vbf.googlegroups.com> I'm trying to install a package (cx_Oracle) on a mac running 10.5.7. I've done this on other platforms, but never on a mac. I followed the instructions given, but when I try and run setup I get: Apollo:instantclient_10_2 user$ python setup.py build /System/Library/Frameworks/Python.framework/Versions/2.5/Resources/ Python.app/Contents/MacOS/Python: can't open file 'setup.py': [Errno 2] No such file or directory Is there something else I have to install first to have this? Thanks! From dstanek at dstanek.com Fri Jul 17 08:08:48 2009 From: dstanek at dstanek.com (David Stanek) Date: Fri, 17 Jul 2009 08:08:48 -0400 Subject: Override a method but inherit the docstring In-Reply-To: References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: On Fri, Jul 17, 2009 at 2:58 AM, Peter Otten<__peter__ at web.de> wrote: > Ben Finney wrote: > >> Howdy all, >> >> The following is a common idiom:: >> >> ? ? class FooGonk(object): >> ? ? ? ? def frobnicate(self): >> ? ? ? ? ? ? """ Frobnicate this gonk. """ >> ? ? ? ? ? ? basic_implementation(self.wobble) >> >> ? ? class BarGonk(FooGonk): >> ? ? ? ? def frobnicate(self): >> ? ? ? ? ? ? special_implementation(self.warble) >> >> The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >> applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >> the method, the original docstring is not associated with it. >> >> Ideally there would be a way to specify that the docstring should be >> inherited. The best I can come up with is:: >> >> ? ? class BarGonk(FooGonk): >> ? ? ? ? def frobnicate(self): >> ? ? ? ? ? ? special_implementation(self.warble) >> ? ? ? ? frobnicate.__doc__ = FooGonk.frobnicate.__doc__ >> >> but that violates DRY (the association between BarGonk and FooGonk is >> being repeated), puts the docstring assignment awkwardly after the end >> of the method instead of at the beginning where docstrings normally go, >> and reads poorly besides. >> >> What is the most Pythonic, DRY-adherent, and preferably least-ugly >> approach to override a method, but have the same docstring on both >> methods? > > Just thinking aloud: Write a patch for pydoc that looks up the base-class > documentation. > > B.f.__doc__ will continue to return None, but > > help(B.f) will show something like > > ? ?No documentation available for B.f. > > ? ?Help for A.f: > ? ?yadda yadda > > > Of course that might be misleading when A.f and B.f are up to something > completely different... > This should never be the case. It violates LSP and would be very confusing to readers of the code. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From dstanek at dstanek.com Fri Jul 17 08:21:39 2009 From: dstanek at dstanek.com (David Stanek) Date: Fri, 17 Jul 2009 08:21:39 -0400 Subject: Override a method but inherit the docstring In-Reply-To: <02701ee5$0$5185$c3e8da3@news.astraweb.com> References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> <87ljmogglz.fsf@benfinney.id.au> <02701ee5$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Fri, Jul 17, 2009 at 3:52 AM, Steven D'Aprano wrote: > On Fri, 17 Jul 2009 12:58:48 +1000, Ben Finney wrote: > >>> Using a decorator in this manner requires repeating the super class >>> name. ?Perhaps there is a way to get the bases of BarGonk, but I don't >>> think so, because at the time that the decorator is called, BarGonk is >>> not yet fully defined. >> >> Yes, I tried a few different ways, but within the decorator it seems the >> function object is quite unaware of what class it is destined for. > > > When the decorator is called, the function object is just a function > object, not a method, so there is no concept of "what class it is > destined for". > >>>> def dec(func): > ... ? ? print type(func) > ... ? ? try: > ... ? ? ? ? ? ? print func.im_class > ... ? ? except: > ... ? ? ? ? ? ? print "no im_class" > ... ? ? return func > ... >>>> class Test(object): > ... ? ? @dec > ... ? ? def spam(self): > ... ? ? ? ? ? ? pass > ... > > no im_class >>>> type(Test.spam) > >>>> Test.spam.im_class > > > > I suppose you could try to determine what namespace you're currently when > the class is created, but that's surely going to be fragile and messy. > It isn't too bad. I got the idea to use the method's enclosing scope[1] in my decorator[2] from DecoratorTools. I am working to remove it because it make me sad, but it does work. [1] http://code.google.com/p/snake-guice/source/browse/snakeguice/decorators.py#51 [2] http://code.google.com/p/snake-guice/source/browse/snakeguice/decorators.py#58 -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From seldon at katamail.it Fri Jul 17 08:23:45 2009 From: seldon at katamail.it (Seldon) Date: Fri, 17 Jul 2009 14:23:45 +0200 Subject: guessing file type Message-ID: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Hello, I need to determine programmatically a file type from its content/extension (much like the "file" UNIX command line utility) I searched for a suitable Python library module, with little luck. Do you know something useful ? Thanks in advance. -- Seldon From ben+python at benfinney.id.au Fri Jul 17 08:28:41 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 22:28:41 +1000 Subject: guessing file type References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: <87iqhrfq86.fsf@benfinney.id.au> Seldon writes: > Hello, I need to determine programmatically a file type from its > content/extension (much like the "file" UNIX command line utility) The Unix ?file(1)? program does its magic with a library called ?magic? and a corresponding database. > I searched for a suitable Python library module, with little luck. Do > you know something useful ? This package provides Python bindings to libmagic. -- \ ?Unix is an operating system, OS/2 is half an operating system, | `\ Windows is a shell, and DOS is a boot partition virus.? ?Peter | _o__) H. Coffin | Ben Finney From python.list at tim.thechases.com Fri Jul 17 08:36:29 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 17 Jul 2009 07:36:29 -0500 Subject: guessing file type In-Reply-To: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: <4A60704D.8080803@tim.thechases.com> > Hello, I need to determine programmatically a file type from its > content/extension (much like the "file" UNIX command line utility) > > I searched for a suitable Python library module, with little luck. Do > you know something useful ? Are you looking for something comprehensive? Or are you just looking for particular file-types that your application can handle? I'd start with the python mimetypes library[1] which does detection based on extensions (to which you can add your own mappings). For sniffing by content, there are a wide variety of document types, I don't know of any pre-existing library. The first couple bytes can often tell you something, but you'd have to go digging into the source for "file" to see what it does. -tkc [1] http://docs.python.org/library/mimetypes.html From van.lindberg at gmail.com Fri Jul 17 08:39:22 2009 From: van.lindberg at gmail.com (VanL) Date: Fri, 17 Jul 2009 07:39:22 -0500 Subject: guessing file type In-Reply-To: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: Seldon wrote: > Hello, I need to determine programmatically a file type from its > content/extension (much like the "file" UNIX command line utility) > > I searched for a suitable Python library module, with little luck. Do > you know something useful ? Python-magic (http://pypi.python.org/pypi/python-magic/0.1) wraps libmagic -- the same thing that "file" uses -- but getting it on windows is a pain. I have never been able to do it, anyway. A more cross-platform library with a similar scope is hachoir. Still seems to be available from PyPI, but the website seems to have fallen off the 'net. From victorsubervi at gmail.com Fri Jul 17 09:02:25 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Fri, 17 Jul 2009 10:02:25 -0300 Subject: Auto Send URL Message-ID: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> Hi; I am trying to script code that automatically sends a Web site visitor to an URL. Specifically, when they enter a value in a search box, I have that form sent to a script that writes an URL acceptable to Google, then I want to send the visitor off without him having to click another button. How do I do this? TIA, Victor -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Fri Jul 17 09:17:20 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 14:17:20 +0100 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> Message-ID: <4A6079E0.8040003@mrabarnett.plus.com> Xavier Ho wrote: > I have a simple class that generates prime numbers, using memorisation > and iteration to generate the next prime number. > > In the class, I have defined a function that gives, say, the 5th prime > number. Or the 1000th, it's still very fast. But the code isn't what I > really like. > > def nPrime(self, n): > "Returns nth prime number, the first one being 2, where n = 0. > When n = 1, it returns 3." > while True: > try: > return self.primes[n] > except: > self.next() > > The next() function generates the next prime, and appends it into > primes. This way, it keeps trying to append until the nth prime > requested exist, and returns it. > > My problem is that it's a "While True" loop, which I get a lot of "Don't > do it!" In the light of making the code better, what could I do? > I wouldn't use a bare "except". What if you had written "prims" instead of "primes"? It would catch AttributeError and produce the next prime, repeating until it ran out of memory. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 09:28:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 13:28:00 GMT Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> Message-ID: <02706d8f$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 00:34:00 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> It is very useful to be able to write e.g.: >> >> if header or body or footer: >> print assemble_page(header, body, footer) >> >> and have empty strings to be equivalent to False. > > Why doesn't assemble_page properly handle the case where header, body, > and footer are all empty? That would let you eliminate the if. "Make > sure your code 'does nothing' gracefully" (Kernighan and Plauger). You mean something like this? def assemble_page(header, body, footer): if header or body or footer: do_lots_of_expensive_processing() else: do_nothing_gracefully() Sure, why not? Whatever way you do it, my point is that it is very useful to perform branching tests on non-Boolean objects. Wherever you put the test, being able to write: if header or body or footer: is a huge improvement over: if (header != '') or (body != '') or (footer != ''): -- Steven From edreamleo at charter.net Fri Jul 17 09:36:59 2009 From: edreamleo at charter.net (Edward K Ream) Date: Fri, 17 Jul 2009 08:36:59 -0500 Subject: ANN: Leo 4.6 final released Message-ID: Leo 4.6 final is now available at: http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106 Leo is a text editor, data organizer, project manager and much more. See: http://webpages.charter.net/edreamleo/intro.html The highlights of Leo 4.6: -------------------------- - Cached external files *greatly* reduces the time to load .leo files. - Leo now features a modern Qt interface by default. Leo's legacy Tk interface can also be used. - New --config, --file and --gui command-line options. - Leo tests syntax of .py files when saving them. - Leo can now open any kind of file into @edit nodes. - @auto-rst nodes allow easy editing of reStructuredText files. - Properties of commanders, positions and nodes simplify programming. - Improved Leo's unit testing framework. - Leo now requires Python 2.5 or later. - Dozens of small improvements and bug fixes. Links: ------ Leo: http://webpages.charter.net/edreamleo/front.html Forum: http://groups.google.com/group/leo-editor Download: http://sourceforge.net/project/showfiles.php?group_id=3458 Bzr: http://code.launchpad.net/leo-editor/ Quotes: http://webpages.charter.net/edreamleo/testimonials.html -------------------------------------------------------------------- Edward K. Ream email: edreamleo at yahoo.com Leo: http://webpages.charter.net/edreamleo/front.html -------------------------------------------------------------------- From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 09:42:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 13:42:44 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: <02707103$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: > Python has extended the algebra definition of "or" and "and" top any > type, but it is so unintuitive (I'm no LISP programmer). I disagree. The Something/Nothing dichotomy is so intuitive to me that I would hate to go back to a language that only accepted booleans as arguments to `if`. > I think than > using the short-circuiting mechanism of bool operators along with the > python tricks is just error prone and may result in bug difficult to > spot, unless you are very aware of all python boolean mechanisms. In other words, if you don't know how Python behaves, you will make mistakes. Of course you will. That applies to *anything* -- if you don't know how it works, you will make mistakes. Given three result codes, where 0 means "no error" and an arbitrary non- zero integer means some error, it is simple and easy to write: failed = result_1 or result_2 or result_3 The equivalent: failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) # or if you prefer: succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) are longer and more difficult to read and easier to get wrong. Even worse are tricks like this: failed = (result_1 + result_2 + result_3) != 0 This obscures the fact that the result codes are flags and makes it seem like (flag + flag) is meaningful. -- Steven From justin.mailinglists at gmail.com Fri Jul 17 09:58:13 2009 From: justin.mailinglists at gmail.com (Justin Ezequiel) Date: Fri, 17 Jul 2009 06:58:13 -0700 (PDT) Subject: guessing file type References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: <7c7ab0ce-ffea-46da-b495-8ebf8e63ecf6@32g2000yqj.googlegroups.com> On Jul 17, 8:39?pm, VanL wrote: > Seldon wrote: > > Hello, ?I need to determine programmatically a file type from its > > content/extension (much like the "file" UNIX command line utility) > > > I searched for a suitable Python library module, with little luck. Do > > you know something useful ? > I've used the magic.py module by "Jason Petrone" before. http://tinyurl.com/ldho8m From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 10:12:37 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 14:12:37 GMT Subject: Try... except....Try again? References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> Message-ID: <02707804$0$5185$c3e8da3@news.astraweb.com> Apologies for breaking threading, but the original post hasn't showed up on my ISP's news server. Xavier Ho wrote: > I have a simple class that generates prime numbers, using memorisation > and iteration to generate the next prime number. [...] > The next() function generates the next prime, and appends it into > primes. This way, it keeps trying to append until the nth prime > requested exist, and returns it. > > My problem is that it's a "While True" loop, which I get a lot of >> "Don't do it!" There's nothing wrong with "while True" loops. They are useful in two situations: (1) Where you want to loop forever. (2) Where you want to loop until some complex event takes place, which can't easily be written using "while condition". Neither of these cases holds for your code, so although there's nothing wrong with "while True", in your case I'd avoid it. > In the light of making the code better, what could I do? (1) Explicitly loop the correct number of times. (2) Only catch IndexError. def nPrime(self, n): for i in xrange(len(self.primes), n+1): self.next() return self.primes[n] -- Steven From http Fri Jul 17 10:12:51 2009 From: http (Paul Rubin) Date: 17 Jul 2009 07:12:51 -0700 Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> Message-ID: <7xab33s8ik.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > def assemble_page(header, body, footer): > if header or body or footer: > do_lots_of_expensive_processing() > else: > do_nothing_gracefully() Why should the processing be expensive if all three fields are empty? > if header or body or footer: > is a huge improvement over: > if (header != '') or (body != '') or (footer != ''): Doesn't really seem any better. There's always something like if any(p != '' for p in [header, body, footer, otherthing1, ...]) if the number of components gets larger. From kyrie at uh.cu Fri Jul 17 10:19:51 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Fri, 17 Jul 2009 10:19:51 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A605B32.5080306@sequans.com> References: <1247774222.4a5f860ec3cfa@mail.uh.cu> <4A605B32.5080306@sequans.com> Message-ID: <200907171019.52471.kyrie@uh.cu> On Friday 17 July 2009 07:06:26 am Jean-Michel Pichavant wrote: > > I was saying that using boolean operators with object instead of boolean > values is error prone, cause no language behaves he same way, I don't know of many languages that actively promote the duck typing concept, are as highly dynamic as python, have the "metaclass" concept, treats almost all language concepts as first class citizens (including functions and classes), and so on. And don't get me started on assignment (which I consider very natural, by the way, but apparently most of the popular languages have pretty unnatural assignments). It is error prone if you are expecting the result to be a bool instead of just behaving like one (it is certainly unexpected, but you shouldn't be expecting to get an instance of certain class anyway, for most of python's operations). And it is confusing if you are reading the "int(inputVar) or 25" line and have no idea of what it means (but once you know it, it becomes very readable, almost plain english). > and all > behaviors are conventions difficult to figure out without diving deeply > into the documentation (or being explained as it happened to me). That happens with many new concepts. Welcome to python, I guess... if you are willing to shake some of your expectations from previous programming languages, you will enjoy it. My [little] experience teaching python tells me that "duck typing", "non-enforced encapsulation" and "everything is an object" are the hardest to accept for the C# folk at my faculty, but once past it, they (the few of them who don't leave the course after that) really enjoy the language. > I think the initialization trick is an error, because I don't want > foo(0) to set daysInAdvance to 25. I'll want it to set the attribute to > 0, cause 0 is a valid integer. 0 is a valid integer content, None > wouldn't be a valid integer content. Well, that wouldn't be a problem with "or", but with the programmer. The exact same behaviour could be obtained with if int(inputValue) == 0: inputValue = 25 and no "or" involved. However, using only inputValue = inputValue or 25 could have been an error if you only wanted 25 in case inputValue is None. (the "or trick" implies certain trust in that the object you have in hand has a reasonable definition of truth value). -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From jeanmichel at sequans.com Fri Jul 17 10:34:57 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 17 Jul 2009 16:34:57 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <02707103$0$5185$c3e8da3@news.astraweb.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A608C11.1020901@sequans.com> Steven D'Aprano wrote: > On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: > > > Given three result codes, where 0 means "no error" and an arbitrary non- > zero integer means some error, it is simple and easy to write: > > failed = result_1 or result_2 or result_3 > > The equivalent: > > failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) > # or if you prefer: > succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) > > [snip] This is, I guess, where we disagree. I find the second proposal less error prone, and universally understandable unlike the first one. It may be verbose, it may look even lame to some people, but in the end this is perfectly reliable, because you manipulate only False or True within the boolean operations. The first form does not clearly show what is the failed criteria. It just happens by coincidence that in this case the failed criteria matches the Nothingness of result_1, result_2, result_3. What if results may be 'OK' or 'KO'. failed = result_1 or result_2 or result_3 won't work. failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is lame but reliable. JM From contact at xavierho.com Fri Jul 17 10:35:24 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 22:35:24 +0800 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> Message-ID: <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> oops, wrong address. When will reply-to tag appear on the Python mailing list? =/ Good idea, Steven and MRAB. I changed my code to the following: def nPrime(self, n): "Returns nth prime number, the first one being 2, where n = 0. When n = 1, it returns 3." for x in range(n+2): try: return self.primes[n] except IndexError: self.next() And it's definitely better. Thanks a ton guys. (n+2 because after looping the maximum number of times, it needs to try returning the value again.) It works just as well as the previous version, but slightly safer on my part... and I can adapt this in my future coding habits. Thanks again. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Fri Jul 17 10:51:05 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 17 Jul 2009 07:51:05 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A608C11.1020901@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> <4A608C11.1020901@sequans.com> Message-ID: <4A608FD9.908@stoneleaf.us> Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: > >> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: >> >> >> Given three result codes, where 0 means "no error" and an arbitrary non- >> zero integer means some error, it is simple and easy to write: >> >> failed = result_1 or result_2 or result_3 >> >> The equivalent: >> >> failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) >> # or if you prefer: >> succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) >> >> > > [snip] > > This is, I guess, where we disagree. I find the second proposal less > error prone, and universally understandable unlike the first one. Careful! The very few (if any) things in this world that can be considered "universally understandable" do *not* include any programming language! :) > It may > be verbose, it may look even lame to some people, but in the end this is > perfectly reliable, because you manipulate only False or True within the > boolean operations. > > The first form does not clearly show what is the failed criteria. It > just happens by coincidence that in this case the failed criteria > matches the Nothingness of result_1, result_2, result_3. What if results > may be 'OK' or 'KO'. As the programmer, particularly a Python programmer, you should be taking advantage of Python's strengths, of which this is one. If, as you say, some function uses a "something" value to indicate an undesirable result, then you have to use something akin to your last statement. ~Ethan~ > > failed = result_1 or result_2 or result_3 > won't work. > > failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is > lame but reliable. > > > JM From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 10:54:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 14:54:53 GMT Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> Message-ID: <027081ec$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 07:12:51 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> def assemble_page(header, body, footer): >> if header or body or footer: >> do_lots_of_expensive_processing() >> else: >> do_nothing_gracefully() > > Why should the processing be expensive if all three fields are empty? Since I haven't specified an implementation for assemble_page, it could be doing *anything*. Perhaps it has to talk to a remote database over a slow link, perhaps it generates 300 lines of really inefficient HTML code with no content, perhaps it sends a print job to a printer which then warms up, cleans the print heads, and ejects a blank piece of paper. Why does it matter? The example is about the `if` test, not about the function assemble_page(). -- Steven From jackdied at gmail.com Fri Jul 17 11:02:09 2009 From: jackdied at gmail.com (Jack Diederich) Date: Fri, 17 Jul 2009 11:02:09 -0400 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: On Fri, Jul 17, 2009 at 10:35 AM, Xavier Ho wrote: > I changed my code to the following: > > ??? def nPrime(self, n): > ??????? "Returns nth prime number, the first one being 2, where n = 0. When > n = 1, it returns 3." > ??????? for x in range(n+2): > ??????????? try: > ??????????????? return self.primes[n] > ??????????? except IndexError: > ??????????????? self.next() > > And it's definitely better. Thanks a ton guys. > > (n+2 because after looping the maximum number of times, it needs to try > returning the value again.) def nPrime(self, n): while len(self.primes) < n: self.next() return self.primes[n] If you use an off-the-shelf prime number generator fucntion[1] that returns consecutive primes the method collapses into a simple function. def nPrime(n, primes=[], next_prime=eratosthenes().next): while len(primes) < n: primes.append(next_prime()) return primes[n] -Jack [1] http://www.catonmat.net/blog/solving-google-treasure-hunt-prime-number-problem-four/#comment-3075 There is a deque implementation that is faster still but I can't find a link. From python at mrabarnett.plus.com Fri Jul 17 11:09:03 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 16:09:03 +0100 Subject: Einstein summation notation In-Reply-To: <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <4A60940F.6090604@mrabarnett.plus.com> koranthala wrote: >> That test was designed to treat None as a boolean False, without >> noticing that numeric 0 is also treated as False and could make the >> test do the wrong thing. This is an extremely common type of error. > > Actually, I felt that 0 not being considered False would be a better > option. > I had lot of instances where 0 is a valid value and always I had to > put checks specifically for that. > For me, None and False should be equal to False in if checks, every > thing else being considered True. > > Can someone let me know why 0 is considered equal to False? > There should be real good reasons for it, only that I cannot think of > one. Python did always have True and False. From gabriel.rossetti at arimaz.com Fri Jul 17 11:19:36 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 17 Jul 2009 17:19:36 +0200 Subject: no return value for threading.Condition.wait(timeout)? In-Reply-To: References: Message-ID: <4A609688.9080201@arimaz.com> Piet van Oostrum wrote: >>>>>> Gabriel Rossetti (GR) wrote: >>>>>> > > >> GR> I have a 1-1 relation, I have a thread reading msgs from a network socket >> GR> and a method waiting for an answer to arrive (see my thread >> GR> "Threading.Condition problem"). I would like to be able to have a timeout >> GR> as to not block forever, so the idea is to check if I returned because of a >> GR> timeout or not. >> > > So that case is easy I think. After the wait just check if the answer > has arrived. > The problem is other answers could arrive that are not the one I want. Maybe I could check if I got the answer, if not then check how much time has elapsed, and if it's grater or equal to the timeout consider it a timeout. I still think a return value would be better though. From ethan at stoneleaf.us Fri Jul 17 11:20:41 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 17 Jul 2009 08:20:41 -0700 Subject: Einstein summation notation In-Reply-To: <7xab33s8ik.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> Message-ID: <4A6096C9.4070703@stoneleaf.us> Paul Rubin wrote: > Steven D'Aprano writes: > >>def assemble_page(header, body, footer): >> if header or body or footer: >> do_lots_of_expensive_processing() >> else: >> do_nothing_gracefully() > > > Why should the processing be expensive if all three fields are empty? > > >> if header or body or footer: >>is a huge improvement over: >> if (header != '') or (body != '') or (footer != ''): > > > Doesn't really seem any better. There's always something like > > if any(p != '' for p in [header, body, footer, otherthing1, ...]) > > if the number of components gets larger. Or if any(p for p in [header, body, footer, whatever, ...]) Which is even nicer! :) Particularly if header, et al, are some more complicated objects which don't easily generate strings, but do easily know whether they are Something or Nothing. I suppose some of these discussions are based more on one's personal requirements (using several languages, just a couple, or just Python), and whether one wants to achieve Mastery of Python, or just be Proficient. Neither choice is wrong, and wanting Mastery, of course, does not mean not wanting improvements, etc. It does, however, require a deep understanding of not just the mechanics, but the philosophy of the language. Mind you, I am nowhere near Mastery just yet (okay, okay, I have a _long_ way to go! ;), but I love the philosophy of Python, it's simplicity, and the *entirety* of the Zen. ~Ethan~ From malaclypse2 at gmail.com Fri Jul 17 11:26:18 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 17 Jul 2009 11:26:18 -0400 Subject: Einstein summation notation In-Reply-To: <4A60940F.6090604@mrabarnett.plus.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <4A60940F.6090604@mrabarnett.plus.com> Message-ID: <16651e80907170826r13856d5ag48eb299b70574450@mail.gmail.com> On Fri, Jul 17, 2009 at 11:09 AM, MRAB wrote: > Python did always have True and False. Only if "always" means "since python 2.2.1". See: http://www.python.org/doc/2.3/whatsnew/section-bool.html and http://www.python.org/dev/peps/pep-0285/ for details. -- Jerry From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 11:30:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 15:30:11 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> Message-ID: <02708a32$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 16:34:57 +0200, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: >> >> >> Given three result codes, where 0 means "no error" and an arbitrary >> non- zero integer means some error, it is simple and easy to write: >> >> failed = result_1 or result_2 or result_3 >> >> The equivalent: >> >> failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) # or if >> you prefer: >> succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) >> >> > [snip] > > This is, I guess, where we disagree. I find the second proposal less > error prone, and universally understandable unlike the first one. It may > be verbose, it may look even lame to some people, but in the end this is > perfectly reliable, because you manipulate only False or True within the > boolean operations. Because it is verbose, it is more error-prone. The more code you have to write, the more opportunities you have to make mistakes. (This holds up to a point, beyond which being more and more terse also leads to more errors.) Boolean algebra is notorious for programmer errors. The more complicated the boolean expression, the more often programmers get it wrong. The more book-keeping they have to do, the easier it is to get it wrong. All those (result != 0) comparisons are mere book-keeping, they don't add anything to the code except to force the flag to be True or False. > The first form does not clearly show what is the failed criteria. Of course it does: at least one of the three result codes is non-zero. As a bonus, failed will be assigned the first non-zero result code (if any). Your preferred form, on the other hand, folds all the possible error codes into True, throwing away useful information: >>> result_1 = 0 # success >>> result_2 = 0 >>> result_3 = 15 # failure >>> failure = result_1 or result_2 or result_3 >>> failure 15 >>> failure = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) >>> failure True > It > just happens by coincidence that in this case the failed criteria > matches the Nothingness of result_1, result_2, result_3. What if results > may be 'OK' or 'KO'. Obviously the test you write depends on the data you have to work with. Do you think that's surprising? > failed = result_1 or result_2 or result_3 won't work. Not in the example you gave, no. Although if I had to work with lots and lots of strings of the form 'OK' and 'KO', I'd consider sub-classing string: >>> class OKString(str): ... def __nonzero__(self): ... if self == 'OK': return True ... elif self == 'KO': return False ... else: ... raise ValueError('invalid result code') ... >>> >>> a = OKString('OK') >>> b = OKString('KO') >>> c = OKString('KO') >>> if a and b and c: ... print "Success!" ... else: print "Failed!" ... Failed! (I wouldn't bother if I only needed one or two such tests, but if I had lots of them, I'd consider it.) > failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is > lame but reliable. Yes, it's reliably complicated, reliably easy to get wrong, reliably harder to read, and it reliably throws away information. But apart from those disadvantages, it does the job you want it to do. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 11:32:41 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 15:32:41 GMT Subject: Einstein summation notation References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <02708ac8$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 16:09:03 +0100, MRAB wrote: > Python did always have True and False. $ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> True, False Traceback (innermost last): File "", line 1, in ? NameError: True -- Steven From mooniitk at gmail.com Fri Jul 17 11:42:19 2009 From: mooniitk at gmail.com (mayank gupta) Date: Fri, 17 Jul 2009 17:42:19 +0200 Subject: 'del' function for Dictionary Message-ID: Hi all, I wanted to know whether there is a more efficient way to delete an entry from a dictionary (instead of using the 'del' function), because after analyzing the time taken by the code, it seems to me that the 'del' function takes most of the time. I might be incorrect as well. Kindly help me in this regard. Cheers, Mayank -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 17 11:50:14 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 23:50:14 +0800 Subject: Try... except....Try again? In-Reply-To: References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: <2d56febf0907170850w5941170fpa527571f7fc241ef@mail.gmail.com> On Fri, Jul 17, 2009 at 11:02 PM, Jack Diederich wrote: > > If you use an off-the-shelf prime number generator fucntion[1] that > returns consecutive primes the method collapses into a simple > function. > > def nPrime(n, primes=[], next_prime=eratosthenes().next): > while len(primes) < n: > primes.append(next_prime()) > return primes[n] > > -Jack > > [1] > http://www.catonmat.net/blog/solving-google-treasure-hunt-prime-number-problem-four/#comment-3075 > There is a deque implementation that is faster still but I can't > find a link. Jack, thanks for the link, I'll definitely look into it. The function is currently implemented in a class I wrote myself, which is a generator class that does exactly the same thing in the next() function. I didn't post the code here because it wasn't in the scope of this thread, but I'm sure others have done a lot better and much more efficient algorithms. The nPrime method uses the nature of the iteration and checks if a value already exists (ie generated before) and return that value; otherwise it iterates again. This way, I don't have to iterate from n=2 and on every time, which makes generation time linear. However, I just gave the link's code a quick try. It was at least 10x times faster than my code to generate 100000 prime numbers - I'll have a closer look. Thanks a great deal. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From motoom at xs4all.nl Fri Jul 17 11:54:47 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Fri, 17 Jul 2009 17:54:47 +0200 Subject: 'del' function for Dictionary In-Reply-To: References: Message-ID: <4A609EC7.6060007@xs4all.nl> mayank gupta wrote: > after analyzing the time taken by the code, What code? From dan.eloff at gmail.com Fri Jul 17 12:11:18 2009 From: dan.eloff at gmail.com (Eloff) Date: Fri, 17 Jul 2009 09:11:18 -0700 (PDT) Subject: Find first matching substring Message-ID: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> Almost every time I've had to do parsing of text over the last 5 years I've needed this function: def find_first(s, subs, start=None, end=None): results = [s.find(sub, start, end) for sub in subs] results = [r for r in results if r != -1] if results: return min(results) return -1 It finds the first matching substring in the target string, if there is more than one match, it returns the position of the match that occurs at the lowest index in the string. Has anyone else had problems where they could have applied this function? It seems to me that python's find (and rfind, index, rindex) could be modified (the same way that startswith and endswith have been) to behave this way if a tuple were passed. Do other's agree that this would be desirable? Thanks, Dan From http Fri Jul 17 12:15:17 2009 From: http (Paul Rubin) Date: 17 Jul 2009 09:15:17 -0700 Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> <027081ec$0$5185$c3e8da3@news.astraweb.com> Message-ID: <7xljmn5lre.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Since I haven't specified an implementation for assemble_page, it could > be doing *anything*. Perhaps it has to talk to a remote database over a > slow link, perhaps it generates 300 lines of really inefficient HTML code > with no content, perhaps it sends a print job to a printer which then > warms up, cleans the print heads, and ejects a blank piece of paper. That sounds like a code smell to me. If the function is supposed to print a piece of paper when any of those strings are nonempty, it should also print one when they are all empty. Of the different values a string can have, 'foo', 'bar', and '' are all perfectly good values and should all be treated the same way. If you want an absent string to work differently than a present one, that's separate from empty vs nonempty, and overloading the empty string to mean "absent" is another antipattern. Maybe what you really want is a list of page constituents that are present: page_parts = [header, body, footer] If all the constituents are absent, then page_parts is the empty list, so you would just test for page_parts being empty. > Why does it matter? The example is about the `if` test, not about the > function assemble_page(). Because of the notion that the code should "do nothing" gracefully. The presence of that 'if' test is ungraceful, so its better to look for ways to eliminate it than slickify it. From wells at submute.net Fri Jul 17 12:31:10 2009 From: wells at submute.net (Wells Oliver) Date: Fri, 17 Jul 2009 11:31:10 -0500 Subject: Question regarding style/design.. Message-ID: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> Sometimes I see relatively small application, generally task scripts, written as essentially a list of statements. Other times, I see them neatly divided into functions and then the "if __name__ == '__main__':" convention. Is there a preference? Is there an... application scope such that the preference shifts from the former to the latter? I understand the use of the __name__ == 'main' convention for building unit tests, but I'm mixed on using it in scripts/small applications. Thanks for any thoughts! -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From mabdelkader at gmail.com Fri Jul 17 12:32:12 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Fri, 17 Jul 2009 12:32:12 -0400 Subject: A multi-threaded file searcher for processing large text files Message-ID: <148918f0907170932w7f1a7f3br78a70845e9050566@mail.gmail.com> I'm building a threaded file searcher that uses some of Fredrik Lundh's ( http://effbot.org/zone/wide-finder.htm) suggestions for parsing text very quickly in pure python, as I have about a 10GB log file to parse every day. A naiive approach would be to just parse the 1MB chunks, add the results into a list, and just traverse that list. I want to take this idea a bit further. I want to process results as they're being found. A great way to implement this is to use the Queue class that python provides. My idea is to exploit the iterator protocol to have it block until a result is found, if any, and return the result until we're finished parsing the file then we can raise StopIteration. My idea is sort of similar to a producer / consumer, but it follows something of this idiom: producer produces the file chunks consumer consumes the file chunks -> consumer parsers the file chunks and produces results class waits on the production of the original consumer and processes it as they come. I am having a bit of trouble with the concurrency, but I'm using this as an exercise to understand how concurrency works from a broader scale. I am not trying to get into a debate of whether this is really needed or a python-concurrency debate:) Without further ado, my class is as follows: class ThreadedFileSearcher(object): def __init__(self, filename, rx_pat, blocking_timeout = 10): self.name = filename self.pattern = rx_pat self.blocking_timeout = blocking_timeout #need to find a better way to do this with more threads that can return #stable results (aka chunks are in order) self._thread_count = 1 #the queues self._results = Queue.Queue() self._queue = Queue.Queue() #returns the get_chunk() implementation self._engine = LogParsingEngine(filename) #start acquiring file offsets for the file #as daemon threads self._initialize_worker_threads(self._prime_queue) #experimental...should probably be some type of conditional variable self._stop_processing = False def __iter__(self): #start the worker threads self._initialize_worker_threads(self._target_worker) return self.next() def _initialize_worker_threads(self, callback): #should really use just one thread for i in xrange(self._thread_count): t = threading.Thread(target=callback) t.setDaemon(True) t.start() def _prime_queue(self): """put code chunk offsets on the queue""" #get_chunks() just returns 1MB offsets in the file for chunk in self._engine.get_chunks(): self._queue.put(chunk) def _target_worker(self): """code chunk to parse queue""" #loop infinitely while True: try: #get next chunk offset from the queue start_pos, bytes_to_read = self._queue.get( timeout=self.blocking_timeout ) except (TypeError, Queue.Empty): #None was returned from the .get() #this will throw a TypeError as it tries to unpack None #or the Queue was empty self._stop_processing = True #exit loop break #process the cunk here f = open(self.name, 'r') f.seek(start_pos) #find all matching lines in the chunk for chunk in self.pattern.findall(f.read(bytes_to_read)): #an non-overlapping matches of self.pattern #placed on the queue as a string self._results.put(chunk) f.close() #done! self._queue.task_done() def next(self): while True: try: #wait for the results to be put on matchedlist = self._results.get(timeout=self.blocking_timeout) except Queue.Empty: #if the worker thread finished if self._stop_processing: raise StopIteration else: self._results.task_done() yield matchedlist To use the following class, I wanted to have some kind of interface like this: regex = re.compile("-{3}Processing..-{3}") #---Processing..--- f = ThreadedFileSearcher("LogFile.log", regex) for line in f: #guaranteed to be a line that matches regex #process something... print line I am looking for some suggestions, comments, and better ways to modify this. One thing someone will realize when using this class is that the initial parsing will be incredibly quick, but if the blocking_timeout is set to 10, then there will be a 10second wait at the end to test if the worker threads should set the stop conditions. A glaring hole leading from this is if the blocking_timeout is set to something like 1second and by the time a user attempts to iterate over the results, the worker threads will prematurely stop processing. Speaking of stop processing, should self._stop_processing be a conditional variable. Right now it's a boolean, and I think that's pretty hacky. I don't like the StopIteration stop condition, maybe someone has a better suggestion? A future modification I'm looking for is to launch multiple threads that process different parts of the file (different chunks) and return their results, probably indexed by their chunk offset. Then I can iterate over that sequentially. I think that would be a trivial parallel optimization. Thoughts? Comments? Thanks very much, Mahmoud Abdelkader mahmoud at linux.com http://blog.mahmoudimus.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Jul 17 12:55:39 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 17 Jul 2009 09:55:39 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A608C11.1020901@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> <4A608C11.1020901@sequans.com> Message-ID: On 7/17/2009 7:34 AM Jean-Michel Pichavant said... > Steven D'Aprano wrote: >> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: >> >> >> Given three result codes, where 0 means "no error" and an arbitrary non- >> zero integer means some error, it is simple and easy to write: >> >> failed = result_1 or result_2 or result_3 >> >> The equivalent: >> >> failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) >> # or if you prefer: >> succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) >> >> > [snip] > > This is, I guess, where we disagree. I find the second proposal less > error prone, and universally understandable unlike the first one. It may > be verbose, it may look even lame to some people, but in the end this is > perfectly reliable, because you manipulate only False or True within the > boolean operations. > > The first form does not clearly show what is the failed criteria. It > just happens by coincidence No -- it happens by design because the premise is 'where 0 means "no error" and an arbitrary non-zero integer means some error'. > that in this case the failed criteria > matches the Nothingness of result_1, result_2, result_3. What if results > may be 'OK' or 'KO'. Which by definition won't happen for the example cited... > > failed = result_1 or result_2 or result_3 > won't work. ... so you certainly wouldn't write a test that couldn't properly determine success or failure. > > failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is > lame but reliable. In this case I'd write something that meets the specs of the problem your addressing... failed = 'KO' in (result_1,result_2,result_3) Emile From nohics at gmail.com Fri Jul 17 12:59:47 2009 From: nohics at gmail.com (nohics nohics) Date: Fri, 17 Jul 2009 17:59:47 +0100 Subject: Regular exprssion for IRC messages Message-ID: Hello, When an IRC client receive a messages, this message in in a special format defined here: Message format in pseudo BNF( http://www.irchelp.org/irchelp/rfc/chapter2.html#c2_3_1 ). For example we can have: :MyNickName!n=MyUserName at 41.238.129.121 JOIN :#AnIrcChannel :MyNickName!n=MyUserName at 41.238.129.121 PRIVMSG #AnIrcChannel :Hello here :MyNickName!n=MyUserName at 41.238.129.121 NICK :TheNewNickName :holmes.freenode.net 372 UnNickName :- a message here ... I want to transforme the message format defined in the irc protocole in the last link, to a regular expression to get the nickname, the username, the host, the commad, it's arguments if exist, and the message after ":" I'm programming in python and I have tried this regular expression: ^(:(\w+)(![^ \r\n]+)?(\@[^ \r\n]+)? )?([a-zA-Z]+|\d\d\d)(( [^: \r\n][^ \r\n]*)+)( :[^\r\n]*)? Python code: nicknamePattern = re.compile(r'^(:(\w+)(![^ \r\n]+)?(\@[^ \r\n]+)? )?([a-zA-Z]+|\d\d\d)(( [^: \r\n][^ \r\n]*)+)( :[^\r\n]*)?') matches = nicknamePattern.search(data) if matches != None: print matches.groups() But it doesn't seems to work all the time. So can you help me to make the correct regular expression please ? You can see some example of messages working or not with this pattren (when I try to connect to an irc server): http://www.developpez.net/forums/d779736/php/langage/regex/format-message-pseudo-bnf-expression-reguliere/#post4494010 Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Fri Jul 17 13:11:49 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 17 Jul 2009 12:11:49 -0500 Subject: Question regarding style/design.. In-Reply-To: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> Message-ID: <4A60B0D5.6030806@tim.thechases.com> > Sometimes I see relatively small application, generally task scripts, > written as essentially a list of statements. Other times, I see them neatly > divided into functions and then the "if __name__ == '__main__':" convention. > Is there a preference? Is there an... application scope such that the > preference shifts from the former to the latter? I understand the use of the > __name__ == 'main' convention for building unit tests, but I'm mixed on > using it in scripts/small applications. This may circle around to the "is Python a scripting language" bruhaha a while back. I tend to skip the __name__ == '__main__' in what I'd consider scripts (one-file hacks to solve an immediate problem, often disposable or one-use wonders). However for non-scripts (which I'd define as code that has more than one source-code file I've written or that I'd have to maintain) and for modules, I tend to put in the __name__ guard. -tkc From aurora00 at gmail.com Fri Jul 17 13:15:59 2009 From: aurora00 at gmail.com (Wai Yip) Date: Fri, 17 Jul 2009 10:15:59 -0700 (PDT) Subject: ctype performance benchmark References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> Message-ID: <48c15bac-8af2-472d-9bc0-1f19c4bf29cb@g7g2000prg.googlegroups.com> I started with ctypes because it is the battery included with the Python standard library. My code is very fluid and I'm looking for easy opportunity to optimize it. One example is to find the longest common prefix among two strings. Right now I am comparing it character by character with pure Python. It seems like an ideal low hanging fruit. With ctype I can rewriting a few lines of Python into a few lines of C. All the tools are available and no third party library is needed. It turned out the performance fail to match pure Python. This function is called million of times in an inner loop. The overhead overwhelm any performance gain with C. Eventually I have found success grouping the data into larger chunk for each API call. This is what I originally planned to do anyway. I am only sharing my experience here that doing fine grained ctype function call has its limitation. I have looked into Pyrex before and I was quite confused by the fusion of C and Python langauage. Perhaps it is time for me to give it a second look. I just heard of Cython now and it also look interesting. I think it is helpful for both project to articulate more clearly what they are, how they achieve the performance gain and to give more guidance on good use cases. On the other hand, perhaps it is just me are confused because I don't know enough of the Python internal. Wai Yip From mydevforums at gmail.com Fri Jul 17 13:17:19 2009 From: mydevforums at gmail.com (IronyOfLife) Date: Fri, 17 Jul 2009 10:17:19 -0700 (PDT) Subject: Installing python-gnutls on Windows Message-ID: <58825f8b-9665-4a3d-9f0a-7bb559ae195e@p23g2000vbl.googlegroups.com> Hi, I'm new to python and would like to request your help installing python-gnutls on windows. Are there binaries available which can be installed ? Looks like the setup.py file which came with the package is for Debian. I tried modifying it for windows but am still getting errors. get_options() is not getting the include directories and libraries so I harcoded the include directories. I'm getting the following error now: //----------------------------------------------------------------------------------- C:\Development\python\python-gnutls-1.1.8>python setup.py build -- compiler=mingw32 running build running build_py running build_ext building 'gnutls.library._gnutls_init' extension Traceback (most recent call last): File "setup.py", line 86, in libraries='-lgnutls -lgnutls-extra')]) File "C:\Python26\lib\distutils\core.py", line 152, in setup dist.run_commands() File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands self.run_command(cmd) .... File "C:\Python26\lib\distutils\command\build_ext.py", line 471, in build_exte nsions self.build_extension(ext) ... depends=ext.depends) File "C:\Python26\lib\distutils\ccompiler.py", line 689, in compile depends, extra_postargs) File "C:\Python26\lib\distutils\ccompiler.py", line 363, in _setup_compile "'include_dirs' (if supplied) must be a list of strings" TypeError: 'include_dirs' (if supplied) must be a list of strings //------------------------------------------------------------------------------------- I would really appreciate your help Thanks Ramg From lodmod.dod at gmail.com Fri Jul 17 13:20:43 2009 From: lodmod.dod at gmail.com (LoD MoD) Date: Fri, 17 Jul 2009 10:20:43 -0700 Subject: getopt code NameError exception on logTail.py Message-ID: I am having trouble extending my option parsing. Any help is appreciated: import sys import getopt import time def tail(file): while 1: where = file.tell() line = file.readline() if not line: time.sleep(1) file.seek(where) else: print line, # already has newline def main(): # parse command line options try: opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", "filename=" ]) except getopt.error, msg: print msg print "for help use --help" sys.exit(2) # process options for o, a in opts: if o in ("-h", "--help"): print __doc__ sys.exit(0) if o in ("-f", "--filename"): print "Parsing F argument" file = open(filename, 'r') print file # process arguments for arg in args: process(arg) # process() is defined elsewhere if __name__ == "__main__": main() Yields this error: localhost:src gsery$ python logTail.py /var/log/system.log Traceback (most recent call last): File "logTail.py", line 52, in main() File "logTail.py", line 49, in main process(arg) # process() is defined elsewhere NameError: global name 'process' is not defined -------------- next part -------------- An HTML attachment was scrubbed... URL: From mrabarnett at mrabarnett.plus.com Fri Jul 17 13:24:42 2009 From: mrabarnett at mrabarnett.plus.com (Matthew Barnett) Date: Fri, 17 Jul 2009 18:24:42 +0100 Subject: Einstein summation notation In-Reply-To: <02708ac8$0$5185$c3e8da3@news.astraweb.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02708ac8$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A60B3DA.7010506@mrabarnett.plus.com> Steven D'Aprano wrote: > On Fri, 17 Jul 2009 16:09:03 +0100, MRAB wrote: > >> Python did always have True and False. Oops! I meant "didn't", of course. > > $ python1.5 > Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat > 4.1.2-27)] on linux2 > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>> True, False > Traceback (innermost last): > File "", line 1, in ? > NameError: True > From phily05 at gmail.com Fri Jul 17 13:28:13 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 10:28:13 -0700 (PDT) Subject: Propagate import for all modules in a package. Message-ID: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> I'm really new to Python and I am absolutely stumped trying to figure this out. I have searched plenty, but I am either searching for the wrong keywords or this isn't possible. What I want to do is have one import be global for the entire package. Here is an example... __init__.py module1.py module2.py ... moduleN.py I was thinking that I could just, for example, 'from datetime import datetime' in __init__.py and have the ability to use 'datetime' anywhere in any of the modules in 'package'. This didn't work for me. Did I just do something wrong? Is what I am trying to do possible? Thanks in advance. From Scott.Daniels at Acm.Org Fri Jul 17 13:37:25 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 17 Jul 2009 10:37:25 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: akhil1988 wrote: > > Nobody-38 wrote: >> On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: ... >>>> In Python 3 you can't decode strings because they are Unicode strings >>>> and it doesn't make sense to decode a Unicode string. You can only >>>> decode encoded things which are byte strings. So you are mixing up byte >>>> strings and Unicode strings. >>> ... I read a byte string from sys.stdin which needs to converted to unicode >>> string for further processing. >> In 3.x, sys.stdin (stdout, stderr) are text streams, which means that they >> read and write Unicode strings, not byte strings. >> >>> I cannot just remove the decode statement and proceed? >>> This is it what it looks like: >>> for line in sys.stdin: >>> line = line.decode('utf-8').strip() >>> if line == '': #do something here >>> .... >>> If I remove the decode statement, line == '' never gets true. >> Did you inadvertently remove the strip() as well? > ... unintentionally I removed strip().... > I get this error now: > File "./temp.py", line 488, in > main() > File "./temp.py", line 475, in main > for line in sys.stdin: > File "/usr/local/lib/python3.1/codecs.py", line 300, in decode > (result, consumed) = self._buffer_decode(data, self.errors, final) > UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid > data (1) Do not top post. (2) Try to fully understand the problem and proposed solution, rather than trying to get people to tell you just enough to get your code going. (3) The only way sys.stdin can possibly return unicode is to do some decoding of its own. your job is to make sure it uses the correct decoding. So, if you know your source is always utf-8, try something like: import sys import io sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf8') for line in sys.stdin: line = line.strip() if line == '': #do something here .... --Scott David Daniels Scott.Daniels at Acm.Org From robert.kern at gmail.com Fri Jul 17 13:45:44 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 17 Jul 2009 12:45:44 -0500 Subject: Question regarding style/design.. In-Reply-To: <4A60B0D5.6030806@tim.thechases.com> References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> <4A60B0D5.6030806@tim.thechases.com> Message-ID: On 2009-07-17 12:11, Tim Chase wrote: >> Sometimes I see relatively small application, generally task scripts, >> written as essentially a list of statements. Other times, I see them >> neatly >> divided into functions and then the "if __name__ == '__main__':" >> convention. >> Is there a preference? Is there an... application scope such that the >> preference shifts from the former to the latter? I understand the use >> of the >> __name__ == 'main' convention for building unit tests, but I'm mixed on >> using it in scripts/small applications. > > This may circle around to the "is Python a scripting language" bruhaha a > while back. I tend to skip the __name__ == '__main__' in what I'd > consider scripts (one-file hacks to solve an immediate problem, often > disposable or one-use wonders). However for non-scripts (which I'd > define as code that has more than one source-code file I've written or > that I'd have to maintain) and for modules, I tend to put in the > __name__ guard. I definitely suggest always using the __name__ guard no matter what. Habits are important, and it's usually more of a waste of time trying to determine if you are writing a "script" or a "program" than it is to just write the clause all the time. One of the ex-DivMod guys (Glyph?) has a good blog post on the subject of writing docstrings and the Suzuki Method of violin; however, I cannot find it. With a good programmer's editor that can do macros or "snippets", this particular habit can be basically cost-free. There are also technical limitations to be aware of. Even if you don't think you will ever import the script, other tools might. For example, on Windows, multiprocessing needs to import the main module of the parent process in order to do some of its operations. If you do not have a __name__ guard, you will go into an infinite regress as your subprocesses execute the main code and create subsubprocesses until you finally manage to kill them all. Extra fun if it is a GUI and you have to click all of the windows closed. Don't waste half a day trying to figure out why your script mysteriously doesn't work. Learn from my mistakes. :-) -- 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 deets at nospam.web.de Fri Jul 17 13:52:30 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 17 Jul 2009 19:52:30 +0200 Subject: Propagate import for all modules in a package. In-Reply-To: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <7cbs2uF275m7mU1@mid.uni-berlin.de> Phil schrieb: > I'm really new to Python and I am absolutely stumped trying to figure > this out. I have searched plenty, but I am either searching for the > wrong keywords or this isn't possible. > > What I want to do is have one import be global for the entire package. > Here is an example... > > > __init__.py > module1.py > module2.py > ... > moduleN.py > > I was thinking that I could just, for example, 'from datetime import > datetime' in __init__.py and have the ability to use 'datetime' > anywhere in any of the modules in 'package'. > > This didn't work for me. Did I just do something wrong? Is what I am > trying to do possible? Each module has it's own namespace, there is no real global one. So there is no obvious and recommended way to do what you want. You can do it through, by stuffing names into the __builtins__-module, which acts as "namespace of last resort" from datetime import datetime __builtins__['datetime'] = datetime However, this is strongly discouraged - you can easily get name-conflicts, and undefined behavior in your modules if you rely on this. Diez From zaki.rahaman at gmail.com Fri Jul 17 13:58:20 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 10:58:20 -0700 (PDT) Subject: Generator Expressions and CSV Message-ID: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Hey all, I'm really new to Python and this may seem like a really dumb question, but basically, I wrote a script to do the following, however the processing time/memory usage is not what I'd like it to be. Any suggestions? Outline: 1. Read tab delim files from a directory, files are of 3 types: install, update, and q. All 3 types contain ID values that are the only part of interest. 2. Using set() and set.add(), generate a list of unique IDs from install and update files. 3. Using the set created in (2), check the q files to see if there are matches for IDs. Keep all matches, and add any non matches (which only occur once in the q file) to a queue of lines to be removed from teh q files. 4. Remove the lines in the q for each file. (I haven't quite written the code for this, but I was going to implement this using csv.writer and rewriting all the lines in the file except for the ones in the removal queue). Now, I've tried running this and it takes much longer than I'd like. I was wondering if there might be a better way to do things (I thought generator expressions might be a good way to attack this problem, as you could generate the set, and then check to see if there's a match, and write each line that way). From python at mrabarnett.plus.com Fri Jul 17 13:59:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 18:59:09 +0100 Subject: Find first matching substring In-Reply-To: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> References: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> Message-ID: <4A60BBED.7040501@mrabarnett.plus.com> Eloff wrote: > Almost every time I've had to do parsing of text over the last 5 years > I've needed this function: > > def find_first(s, subs, start=None, end=None): > results = [s.find(sub, start, end) for sub in subs] > results = [r for r in results if r != -1] > if results: > return min(results) > > return -1 > > It finds the first matching substring in the target string, if there > is more than one match, it returns the position of the match that > occurs at the lowest index in the string. > One possible optimisation for your code is to note that if you find that one of the substrings starts at a certain position then you're not interested in any subsequent substring which might start at or after that position, so you could reduce the search space for each substring found. > Has anyone else had problems where they could have applied this > function? > > It seems to me that python's find (and rfind, index, rindex) could be > modified (the same way that startswith and endswith have been) to > behave this way if a tuple were passed. Do other's agree that this > would be desirable? > Possibly. I think that allowing a tuple in the partition and rpartition methods might also be useful. From marduk at letterboxes.org Fri Jul 17 13:59:39 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Fri, 17 Jul 2009 13:59:39 -0400 Subject: Propagate import for all modules in a package. In-Reply-To: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <1247853579.17425.10.camel@centar> On Fri, 2009-07-17 at 10:28 -0700, Phil wrote: > I'm really new to Python and I am absolutely stumped trying to figure > this out. I have searched plenty, but I am either searching for the > wrong keywords or this isn't possible. > > What I want to do is have one import be global for the entire package. > Here is an example... > > > __init__.py > module1.py > module2.py > ... > moduleN.py > > I was thinking that I could just, for example, 'from datetime import > datetime' in __init__.py and have the ability to use 'datetime' > anywhere in any of the modules in 'package'. > > This didn't work for me. Did I just do something wrong? Is what I am > trying to do possible? > That's not how packages (were designed to) work. A package is basically a namespace. It doesn't do anything special outside of providing a namespace for common modules. There are some special features (e.g. __init__.py which is basically the "body" of the package and '__all__' which handles wildcard imports of a package) but other than that it's just a namespace. From python.list at tim.thechases.com Fri Jul 17 14:01:44 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 17 Jul 2009 13:01:44 -0500 Subject: Question regarding style/design.. In-Reply-To: References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> <4A60B0D5.6030806@tim.thechases.com> Message-ID: <4A60BC88.2070703@tim.thechases.com> Robert Kern wrote: [sage advice snipped] > Don't waste half a day trying to figure out why your script > mysteriously doesn't work. Learn from my mistakes. :-) I find that's the best type of mistake to learn from: other people's ;-) -tkc From hannarosie at gmail.com Fri Jul 17 14:06:27 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Fri, 17 Jul 2009 11:06:27 -0700 Subject: Writing a single list of numbers to a .m (matlab) file Message-ID: <564970db0907171106q2be0852dg7209a4177f0b265a@mail.gmail.com> Hi, I was wondering if I could get some suggestions on how to write a single list of numbers to a .m file (for matlab) so that I can create a matlab vector out of the list of numbers from my python program. I have been using a csv writer to create .m files from lists of lists, but I'm not sure how to write just a single list to the file. (if I use the same code I've been using, I get an error: csv.Error sequence expected) Thanks for the help! -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Fri Jul 17 14:08:16 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 19:08:16 +0100 Subject: getopt code NameError exception on logTail.py In-Reply-To: References: Message-ID: <4A60BE10.5050905@mrabarnett.plus.com> LoD MoD wrote: > I am having trouble extending my option parsing. > Any help is appreciated: > > import sys > import getopt > import time > > def tail(file): > while 1: > where = file.tell() > line = file.readline() > if not line: > time.sleep(1) > file.seek(where) > else: > print line, # already has newline > > def main(): > > # parse command line options > try: > opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", > "filename="]) > except getopt.error, msg: > print msg > print "for help use --help" > sys.exit(2) > # process options > for o, a in opts: > if o in ("-h", "--help"): > print __doc__ > sys.exit(0) > if o in ("-f", "--filename"): > print "Parsing F argument" > file = open(filename, 'r') > print file > > > > # process arguments > for arg in args: > process(arg) # process() is defined elsewhere > > if __name__ == "__main__": > main() > > Yields this error: > > localhost:src gsery$ python logTail.py /var/log/system.log > Traceback (most recent call last): > File "logTail.py", line 52, in > main() > File "logTail.py", line 49, in main > process(arg) # process() is defined elsewhere > NameError: global name 'process' is not defined > The trackback tells you what's wrong: you haven't defined 'process'. The comment says it's defined elsewhere, but neither I nor Python can see it! :-) From emile at fenx.com Fri Jul 17 14:26:22 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 17 Jul 2009 11:26:22 -0700 Subject: Generator Expressions and CSV In-Reply-To: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Message-ID: On 7/17/2009 10:58 AM Zaki said... > Now, I've tried running this and it takes much longer than I'd like. I > was wondering if there might be a better way to do things Suppose, for the sake of argument, that you've written highly efficient code. Then the processing time would already be entirely optimized and no improvements possible. It's running as fast as it can. We can't help. On the other hand, maybe you didn't. In that case, you'll need to profile your code to determine where the time is consumed. At a minimum, you'll need to post the slow parts so we can see the implementation and suggest improvements. Emile From nick at craig-wood.com Fri Jul 17 14:29:59 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Fri, 17 Jul 2009 13:29:59 -0500 Subject: ctype performance benchmark References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> <48c15bac-8af2-472d-9bc0-1f19c4bf29cb@g7g2000prg.googlegroups.com> Message-ID: Wai Yip wrote: > I started with ctypes because it is the battery included with the > Python standard library. My code is very fluid and I'm looking for > easy opportunity to optimize it. One example is to find the longest > common prefix among two strings. Right now I am comparing it character > by character with pure Python. It seems like an ideal low hanging > fruit. With ctype I can rewriting a few lines of Python into a few > lines of C. All the tools are available and no third party library is > needed. Yes ctypes is very easy for this and I've used it like this in the past too. It makes interfacing C code very easy. You might consider just writing a plain bit of C code. If you make a setup.py too (not hard) then it is very easy to distribute and use on other platforms. The Python C API is very easy to use - documentation is extensive. You need to be very careful with references and the error checking is boring and tedious! > It turned out the performance fail to match pure Python. This function > is called million of times in an inner loop. The overhead overwhelm > any performance gain with C. Eventually I have found success grouping > the data into larger chunk for each API call. This is what I > originally planned to do anyway. I am only sharing my experience here > that doing fine grained ctype function call has its limitation. Interesting but not too unexpected - that translation of types is non-trivial. > I have looked into Pyrex before and I was quite confused by the fusion > of C and Python langauage. Perhaps it is time for me to give it a > second look. I just heard of Cython now and it also look interesting. > I think it is helpful for both project to articulate more clearly what > they are, how they achieve the performance gain and to give more > guidance on good use cases. On the other hand, perhaps it is just me > are confused because I don't know enough of the Python internal. Pyrex evolved/was forked into Cython. Cython allows you to write a subset of python code that is compiled into C. You can also optionally provide some type annotations to get it to produce better C code at the cost of making your code look less like python. I find Cython alternatively brilliant and frustrating! Sometimes it works really well, and sometimes I spend hours working out why my program doesn't compile, or to find out the exact syntax I need to interface with such and such a library. I did a C library interfacing project with both ctypes and cython recently as a comparison. It came out to be about the same number of lines of code for both. I decided to run with the ctypes version as it was part python. Here is a short Cython example which interfaces with opendir / closedir / readdir which python doesn't do natively. Note the way you interface with C structures (the real ones are used in the C code - the annotations just tell cython how to use them). Note also that Directory is a class defined in C which I don't think you can't do with ctypes without a wrapper class. This compiles into 1320 lines of C, which in turn compile into 11380 bytes of shared object (when stripped). import cython cdef extern from "dirent.h": struct dirent: char d_name[0] ctypedef struct DIR DIR *opendir(char *name) int closedir(DIR *dirp) dirent *readdir(DIR *dirp) cdef extern from "errno.h": int errno cdef extern from "string.h": char *strerror(int errnum) cdef class Directory: """Represents an open directory""" cdef DIR *handle def __init__(self, path): self.handle = opendir(path) if self.handle is NULL: raise OSError(errno, "Failed to open directory: %s" % strerror(errno)) def readdir(self): """Read the next name in the directory""" cdef dirent *p p = readdir(self.handle) if p is NULL: return None return p.d_name def close(self): """Close the directory""" if self.handle is not NULL: closedir(self.handle) self.handle = NULL def __dealloc__(self): self.close() -- Nick Craig-Wood -- http://www.craig-wood.com/nick From lodmod.dod at gmail.com Fri Jul 17 14:32:50 2009 From: lodmod.dod at gmail.com (LoD MoD) Date: Fri, 17 Jul 2009 11:32:50 -0700 Subject: getopt code NameError exception on logTail.py In-Reply-To: <4A60BE10.5050905@mrabarnett.plus.com> References: <4A60BE10.5050905@mrabarnett.plus.com> Message-ID: In this instance the trackback was somewhat unhelpful.There problem was here: file = open(filename, 'r') should be file = open(a, 'r') args should be passed within the getopt riff On Fri, Jul 17, 2009 at 11:08 AM, MRAB wrote: > LoD MoD wrote: > >> I am having trouble extending my option parsing. >> Any help is appreciated: >> >> import sys >> import getopt >> import time >> >> def tail(file): >> while 1: >> where = file.tell() >> line = file.readline() >> if not line: >> time.sleep(1) >> file.seek(where) >> else: >> print line, # already has newline >> >> def main(): >> >> # parse command line options >> try: >> opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", >> "filename="]) >> except getopt.error, msg: >> print msg >> print "for help use --help" >> sys.exit(2) >> # process options >> for o, a in opts: >> if o in ("-h", "--help"): >> print __doc__ >> sys.exit(0) >> if o in ("-f", "--filename"): >> print "Parsing F argument" >> file = open(filename, 'r') >> print file >> >> # process arguments >> for arg in args: >> process(arg) # process() is defined elsewhere >> >> if __name__ == "__main__": >> main() >> >> Yields this error: >> >> localhost:src gsery$ python logTail.py /var/log/system.log >> Traceback (most recent call last): >> File "logTail.py", line 52, in >> main() >> File "logTail.py", line 49, in main >> process(arg) # process() is defined elsewhere >> NameError: global name 'process' is not defined >> >> The trackback tells you what's wrong: you haven't defined 'process'. The > comment says it's defined elsewhere, but neither I nor Python can see > it! :-) > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From phily05 at gmail.com Fri Jul 17 14:42:28 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 11:42:28 -0700 (PDT) Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <663ab878-2ba2-45f1-a38e-2cff94a6bb00@k30g2000yqf.googlegroups.com> Thanks to both of you for the fast and detailed responses. I will just treat the package for what it is, a namespace. From phily05 at gmail.com Fri Jul 17 14:43:49 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 11:43:49 -0700 (PDT) Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <698e45d2-204b-46c0-ab80-8023e4b8ff15@y19g2000yqy.googlegroups.com> Thanks to both of you for the fast and detailed responses. I will just use the package for what it is, a namespace. From phily05 at gmail.com Fri Jul 17 14:44:42 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 11:44:42 -0700 (PDT) Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <9314b787-558b-4891-b519-23228c6d8fb7@m11g2000yqh.googlegroups.com> Thanks to both of you for the fast and detailed responses. I will just use the package for what it is, a namespace. From python at mrabarnett.plus.com Fri Jul 17 14:49:32 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 19:49:32 +0100 Subject: Generator Expressions and CSV In-Reply-To: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Message-ID: <4A60C7BC.4080904@mrabarnett.plus.com> Zaki wrote: > Hey all, > > I'm really new to Python and this may seem like a really dumb > question, but basically, I wrote a script to do the following, however > the processing time/memory usage is not what I'd like it to be. Any > suggestions? > > > Outline: > 1. Read tab delim files from a directory, files are of 3 types: > install, update, and q. All 3 types contain ID values that are the > only part of interest. > 2. Using set() and set.add(), generate a list of unique IDs from > install and update files. > 3. Using the set created in (2), check the q files to see if there are > matches for IDs. Keep all matches, and add any non matches (which only > occur once in the q file) to a queue of lines to be removed from teh q > files. > 4. Remove the lines in the q for each file. (I haven't quite written > the code for this, but I was going to implement this using csv.writer > and rewriting all the lines in the file except for the ones in the > removal queue). > > Now, I've tried running this and it takes much longer than I'd like. I > was wondering if there might be a better way to do things (I thought > generator expressions might be a good way to attack this problem, as > you could generate the set, and then check to see if there's a match, > and write each line that way). > Why are you checking and removing lines in 2 steps? Why not copy the matching lines to a new q file and then replace the old file with the new one (or, maybe, delete the new q file if no lines were removed)? From smolds at comcast.net Fri Jul 17 14:51:11 2009 From: smolds at comcast.net (Stephen M. Olds) Date: Fri, 17 Jul 2009 11:51:11 -0700 Subject: Mechanize not recognized by py2exe Message-ID: <4A60C81F.7080707@comcast.net> An HTML attachment was scrubbed... URL: From duncan.booth at invalid.invalid Fri Jul 17 15:49:54 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 17 Jul 2009 19:49:54 GMT Subject: ctype performance benchmark References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> <48c15bac-8af2-472d-9bc0-1f19c4bf29cb@g7g2000prg.googlegroups.com> Message-ID: Wai Yip wrote: > I started with ctypes because it is the battery included with the > Python standard library. My code is very fluid and I'm looking for > easy opportunity to optimize it. One example is to find the longest > common prefix among two strings. Right now I am comparing it character > by character with pure Python. It seems like an ideal low hanging > fruit. With ctype I can rewriting a few lines of Python into a few > lines of C. All the tools are available and no third party library is > needed. Alternatively you could just use os.path.commonprefix(). From travis+ml-python at subspacefield.org Fri Jul 17 16:01:41 2009 From: travis+ml-python at subspacefield.org (travis+ml-python at subspacefield.org) Date: Fri, 17 Jul 2009 15:01:41 -0500 Subject: proposal: add setresuid() system call to python Message-ID: <20090717200141.GI6065@subspacefield.org> Hello, Historically, I have used scripting languages like python for typical uses, but they tend to not fare very well at system programming; for close interfacing with the operating system, I'm often forced to use a language like C. This is undesirable to me. I do not think this has to be the case; I see no reason why a scripting language can't implement more of the system call API, at the risk of having some OS-dependent modules. I would actually like to see more network servers written in scripting languages, as they neatly avoid buffer overflow and integer overflow issues with no extra effort. One BIG roadblock to doing this is when they can't manage to drop permissions properly. I am suggesting that the setresuid function be added to python, perhaps in the OS module, because it has the clearest semantics for manipulating user ids. The reason why is best described in the following paper: http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf One argument against this is that it is not specified by POSIX, and thus might be dismissed as "implementation dependent". However, as the paper above demonstrates, even though the setuid system call is defined by POSIX, it already has system-dependent behavior. POSIX provides for at least two different behaviors of the setuid call, and even more if you consider that it leaves what constitutes "appropriate privileges" up to the OS kernel. I humbly propose that python implement all the routines necessary to securely drop privileges, to enable construction of network daemons that might need to drop privileges from root to some non-root userid (e.g. mail transfer agents, or POP/IMAP servers). Furthermore, where there are multiple system calls to achieve this effect, it should implement the ones with the clearest semantics, and setresuid fits that bill. To see what an utter mess the uid-manipulation routines are in, I refer you once again to this paper, as the situation is too complicated to describe in this email: http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf Opinions? Best, Travis -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john at subspacefield.org to get blacklisted. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 850 bytes Desc: not available URL: From zaki.rahaman at gmail.com Fri Jul 17 16:08:05 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 13:08:05 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Message-ID: <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> On Jul 17, 2:49?pm, MRAB wrote: > Zaki wrote: > > Hey all, > > > I'm really new to Python and this may seem like a really dumb > > question, but basically, I wrote a script to do the following, however > > the processing time/memory usage is not what I'd like it to be. Any > > suggestions? > > > Outline: > > 1. Read tab delim files from a directory, files are of 3 types: > > install, update, and q. All 3 types contain ID values that are the > > only part of interest. > > 2. Using set() and set.add(), generate a list of unique IDs from > > install and update files. > > 3. Using the set created in (2), check the q files to see if there are > > matches for IDs. Keep all matches, and add any non matches (which only > > occur once in the q file) to a queue of lines to be removed from teh q > > files. > > 4. Remove the lines in the q for each file. (I haven't quite written > > the code for this, but I was going to implement this using csv.writer > > and rewriting all the lines in the file except for the ones in the > > removal queue). > > > Now, I've tried running this and it takes much longer than I'd like. I > > was wondering if there might be a better way to do things (I thought > > generator expressions might be a good way to attack this problem, as > > you could generate the set, and then check to see if there's a match, > > and write each line that way). > > Why are you checking and removing lines in 2 steps? Why not copy the > matching lines to a new q file and then replace the old file with the > new one (or, maybe, delete the new q file if no lines were removed)? That's what I've done now. Here is the final code that I have running. It's very much 'hack' type code and not at all efficient or optimized and any help in optimizing it would be greatly appreciated. import csv import sys import os import time begin = time.time() #Check minutes elapsed def timeElapsed(): current = time.time() elapsed = current-begin return round(elapsed/60) #USAGE: python logcleaner.py inputdir = sys.argv[1] outputdir = sys.argv[2] logfilenames = os.listdir(inputdir) IDs = set() #IDs from update and install logs foundOnceInQuery = set() #foundTwiceInQuery = set() #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; Queue of IDs to remove from query logs (IDs found only once in query logs) #Generate Filename Queues For Install/Update Logs, Query Logs iNuQ = [] queryQ = [] for filename in logfilenames: if filename.startswith("par1.install") or filename.startswith ("par1.update"): iNuQ.append(filename) elif filename.startswith("par1.query"): queryQ.append(filename) totalfiles = len(iNuQ) + len(queryQ) print "Total # of Files to be Processed:" , totalfiles print "Install/Update Logs to be processed:" , len(iNuQ) print "Query logs to be processed:" , len(queryQ) #Process install/update queue to generate list of valid IDs currentfile = 1 for file in iNuQ: print "Processing", currentfile, "install/update log out of", len (iNuQ) print timeElapsed() reader = csv.reader(open(inputdir+file),delimiter = '\t') for row in reader: IDs.add(row[2]) currentfile+=1 print "Finished processing install/update logs" print "Unique IDs found:" , len(IDs) print "Total Time Elapsed:", timeElapsed() currentfile = 1 for file in queryQ: print "Processing", currentfile, "query log out of", len(queryQ) print timeElapsed() reader = csv.reader(open(inputdir+file), delimiter = '\t') outputfile = csv.writer(open(outputdir+file), 'w') for row in reader: if row[2] in IDs: ouputfile.writerow(row) else: if row[2] in foundOnceInQuery: foundOnceInQuery.remove(row[2]) outputfile.writerow(row) #IDremovalQ.remove(row[2]) #foundTwiceInQuery.add(row[2]) else: foundOnceInQuery.add(row[2]) #IDremovalQ.add(row[2]) currentfile+=1 print "Finished processing query logs and writing new files" print "# of Query log entries removed:" , len(foundOnceInQuery) print "Total Time Elapsed:", timeElapsed() From jcd at sdf.lonestar.org Fri Jul 17 16:08:26 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 17 Jul 2009 16:08:26 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: <1247861306.6362.6.camel@aalcdl07> On Fri, 2009-07-17 at 20:53 +0000, Albert van der Horst wrote: > Because unlike in algol 68 in python whitespace is relevant, > we could get by with requiring whitespace: > x= -q # okay > a 8 ** -2 # okay This is actually quite thoroughly untrue. In python, *indentation* is significant. Whitespace (internal to a line) is not. You can even call methods like this if you want: >>> s = 'abc' >>> s . upper() ABC Obviously, that's A Bad Idea(tm), but python's parser won't stop you. The ++ operator gotcha is so minor that I can't remember anyone actually asking about it on the list (who was actually facing it as a problem--this thread was started by idle speculation). Can we not change the language syntax to address non-issues? Practicality beats purity, a.k.a. don't you have something better to do? Cheers, Cliff From mabdelkader at gmail.com Fri Jul 17 16:15:00 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Fri, 17 Jul 2009 16:15:00 -0400 Subject: proposal: add setresuid() system call to python In-Reply-To: <20090717200141.GI6065@subspacefield.org> References: <20090717200141.GI6065@subspacefield.org> Message-ID: <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> Why don't you write a python extension module? This is a perfect opportunity for that. -- mahmoud mack abdelkader http://blog.mahmoudimus.com/ On Fri, Jul 17, 2009 at 4:01 PM, > wrote: > Hello, > > Historically, I have used scripting languages like python for typical > uses, but they tend to not fare very well at system programming; for > close interfacing with the operating system, I'm often forced to use a > language like C. This is undesirable to me. > > I do not think this has to be the case; I see no reason why a > scripting language can't implement more of the system call API, at the > risk of having some OS-dependent modules. I would actually like to > see more network servers written in scripting languages, as they > neatly avoid buffer overflow and integer overflow issues with no extra > effort. > > One BIG roadblock to doing this is when they can't manage to drop > permissions properly. > > I am suggesting that the setresuid function be added to python, > perhaps in the OS module, because it has the clearest semantics for > manipulating user ids. The reason why is best described in the > following paper: > > http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf > > One argument against this is that it is not specified by POSIX, and > thus might be dismissed as "implementation dependent". > > However, as the paper above demonstrates, even though the setuid > system call is defined by POSIX, it already has system-dependent > behavior. POSIX provides for at least two different behaviors of the > setuid call, and even more if you consider that it leaves what > constitutes "appropriate privileges" up to the OS kernel. > > I humbly propose that python implement all the routines necessary to > securely drop privileges, to enable construction of network daemons > that might need to drop privileges from root to some non-root userid > (e.g. mail transfer agents, or POP/IMAP servers). > > Furthermore, where there are multiple system calls to achieve this > effect, it should implement the ones with the clearest semantics, and > setresuid fits that bill. To see what an utter mess the uid-manipulation > routines are in, I refer you once again to this paper, as the situation > is too complicated to describe in this email: > > http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf > > Opinions? > > Best, > Travis > -- > Obama Nation | My emails do not have attachments; it's a digital signature > that your mail program doesn't understand. | > http://www.subspacefield.org/~travis/ > If you are a spammer, please email john at subspacefield.org to get > blacklisted. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.kern at gmail.com Fri Jul 17 16:15:35 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 17 Jul 2009 15:15:35 -0500 Subject: proposal: add setresuid() system call to python In-Reply-To: <20090717200141.GI6065@subspacefield.org> References: <20090717200141.GI6065@subspacefield.org> Message-ID: On 2009-07-17 15:01, travis+ml-python at subspacefield.org wrote: > Hello, > > Historically, I have used scripting languages like python for typical > uses, but they tend to not fare very well at system programming; for > close interfacing with the operating system, I'm often forced to use a > language like C. This is undesirable to me. You can use ctypes for most of these use cases. -- 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 tjreedy at udel.edu Fri Jul 17 16:32:51 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 16:32:51 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Peter Otten wrote: > Terry Reedy wrote: > >> I tried it. Unfortunately, OOo does not open it correctly. It just >> displays the first three lines of metadate - Title, Creator, Date -- as >> image text. Photoshop does read the image, and does an ok job of >> conversion once anti-aliasing is turned off. > > I snatched some code from the module and modified it to save a sample image: Thank you for the example. > > from turtle import * > > def switchpen(): > if isdown(): > pu() > else: > pd() > > def demo2(): > """Demo of some new features.""" > speed(1) > st() > pensize(3) > setheading(towards(0, 0)) > radius = distance(0, 0)/2.0 > rt(90) > for _ in range(18): > switchpen() > circle(radius, 10) > write("wait a moment...") > while undobufferentries(): > undo() > reset() > lt(90) > colormode(255) > laenge = 10 > pencolor("green") > pensize(3) > lt(180) > for i in range(-2, 16): > if i > 0: > begin_fill() > fillcolor(255-15*i, 0, 15*i) > for _ in range(3): > fd(laenge) > lt(120) > laenge += 10 > lt(15) > speed((speed()+1)%12) > end_fill() > > lt(120) > pu() > fd(70) > rt(30) > pd() > color("red","yellow") > speed(0) > fill(1) begin_fill() in 3.x > for _ in range(4): > circle(50, 90) > rt(90) > fd(30) > rt(90) > fill(0) end_fill() in 3.x > lt(90) > pu() > fd(30) > pd() > shape("turtle") > > tri = getturtle() > tri.resizemode("auto") > turtle = Turtle() > turtle.resizemode("auto") > turtle.shape("turtle") > turtle.reset() > turtle.left(90) > turtle.speed(0) > turtle.up() > turtle.goto(280, 40) > turtle.lt(30) > turtle.down() > turtle.speed(6) > turtle.color("blue","orange") > turtle.pensize(2) > tri.speed(6) > setheading(towards(turtle)) > count = 1 > while tri.distance(turtle) > 4: > turtle.fd(3.5) > turtle.lt(0.6) > tri.setheading(tri.towards(turtle)) > tri.fd(4) > if count % 20 == 0: > turtle.stamp() > tri.stamp() > switchpen() > count += 1 > tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right") > > if __name__ == "__main__": > demo2() > Screen().getcanvas().postscript(file="demo2.eps") > > I could successfully insert the picture into Writer. Whereas I still get just three lines of metadata. The size of my demo2.eps is given as 65,628 bytes. 'Size on disk is given as a bit more but I presume that counts to the end of the last 4k block. > I'm on Kubuntu 9.04 with Python 2.6.2 and OpenOffice 3.0.1. WinXP with updates, Python 3.1, and OO 3.1.0 So either tk 8.5(?) on windows is producing something different or OO3.1 on windows is reading differently. If your file size is different, could you email it so I could try to import it here? > But still, turtle is an educational tool, its main advantage is that it can > show beginners how the image is generated. It is also available with Python as installed. And I want to do simple animations (of algoritms) as well as static pictures. > If you want to generate high-quality graphics easily you need different > primitives. > > http://cairographics.org > > has Python bindings, but I don't know if it's available on Windows. Effectively not, as far as I can tell. PyCairo appears to be *nix and require later binaries than those available from gtk site referenced from cairo site. Terry Jan Reedy From tjreedy at udel.edu Fri Jul 17 16:36:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 16:36:17 -0400 Subject: turtle dump In-Reply-To: <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> Message-ID: alex23 wrote: > The help in iPython says the same, but also mentions that it's a > dynamically generated function, so it may not be picking up the > docstring that way. turtle.ScrolledCanvas.postscript is similarly > terse, but you can find more info in turtle.Canvas.postscript: > > Print the contents of the canvas to a postscript > file. Valid options: colormap, colormode, file, fontmap, > height, pageanchor, pageheight, pagewidth, pagex, pagey, > rotate, witdh, x, y. How, exactly, did you get that list? tjr From piet at cs.uu.nl Fri Jul 17 16:48:37 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 22:48:37 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Piet van Oostrum wrote: >>>>>>>> Gabriel Rossetti (GR) wrote: >>>>>>>> >>> >>> >GR> I have a 1-1 relation, I have a thread reading msgs from a network socket >GR> and a method waiting for an answer to arrive (see my thread >GR> "Threading.Condition problem"). I would like to be able to have a timeout >GR> as to not block forever, so the idea is to check if I returned because of a >GR> timeout or not. >>>> >>> >>> So that case is easy I think. After the wait just check if the answer >>> has arrived. >>> >GR> The problem is other answers could arrive that are not the one I >GR> want. Maybe I could check if I got the answer, if not then check >GR> how much time has elapsed, and if it's grater or equal to the >GR> timeout consider it a timeout. I still think a return value would >GR> be better though. The wait will not get a return value in Python. There was an issue posted in the tracker to request this in 2005. Recently (after 4 years) it was rejected by the BDFL. See http://bugs.python.org/issue1175933 OK, your use case might be a bit more difficult, but I still think it can be solved without a return value. I can't tell exactly how because of lack of information. So (as I already mentioned in one of my previous messages) being notified doesn't imply that everything is OK. Now suppose that your CV is notified one microsecond before it would time out but the required answer did not arrive. What's the difference with a real timeout except for that microsecond? Reimplementing Condition yourself is not to be recommended I think. Using these synchronisation primitives is hard enough let alone implementing them. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From albert at spenarnc.xs4all.nl Fri Jul 17 16:53:36 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 17 Jul 2009 20:53:36 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Gabriel Genellina wrote: >En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano > escribi?: >> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >> >>> I wonder how many people have been tripped up by the fact that >>> >>> ++n >>> >>> and >>> >>> --n >>> >>> fail silently for numeric-valued n. >> >> What do you mean, "fail silently"? They do exactly what you should >> expect: >>>>> ++5 # positive of a positive number is positive >> >> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >> that are buggy, not Python. > >Well, those expectations are taken seriously when new features are >introduced into the language - and sometimes the feature is dismissed just >because it would be confusing for some. >If a += 1 works, expecting ++a to have the same meaning is very reasonable >(for those coming from languages with a ++ operator, like C or Java) - >more when ++a is a perfectly valid expression. >If this issue isn't listed under the various "Python gotchas" articles, it >should... In algol 68 there was one thing strictly forbidden: putting two operators behind each other: x := y** -b .comment must be x := y**(-b) .comment This is quite a sensible rule, especially when, like in Python, two special characters can be run together to denote a different operator. Examples abound : +:= ** A consequence is that 'a*-b' would be illegal. It would become 'a*(-b)' Worse is that x=-q would be illegal. Because unlike in algol 68 in python whitespace is relevant, we could get by with requiring whitespace: x= -q # okay a-- >Gabriel Genellina Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From clp2 at rebertia.com Fri Jul 17 16:55:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 17 Jul 2009 13:55:20 -0700 Subject: Writing a single list of numbers to a .m (matlab) file In-Reply-To: <564970db0907171106q2be0852dg7209a4177f0b265a@mail.gmail.com> References: <564970db0907171106q2be0852dg7209a4177f0b265a@mail.gmail.com> Message-ID: <50697b2c0907171355q76f7c538le319b2240bb4210e@mail.gmail.com> On Fri, Jul 17, 2009 at 11:06 AM, Hanna Michelsen wrote: > Hi, > > I was wondering if I could get some suggestions on how to write a single > list of numbers to a .m file (for matlab) so that I can create a matlab > vector out of the list of numbers from my python program. I have been using > a csv writer to create .m files from lists of lists, but I'm not sure how to > write just a single list to the file. (if I use the same code I've been > using, I get an error: csv.Error sequence expected) You didn't provide any code, but since csv is probably overkill anyway: m_file.write("[" + " ".join(str(num) for num in numbers) + "]\n") Cheers, Chris -- http://blog.rebertia.com From python at mrabarnett.plus.com Fri Jul 17 16:57:46 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 21:57:46 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: <1247861306.6362.6.camel@aalcdl07> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <1247861306.6362.6.camel@aalcdl07> Message-ID: <4A60E5CA.1000106@mrabarnett.plus.com> J. Cliff Dyer wrote: > On Fri, 2009-07-17 at 20:53 +0000, Albert van der Horst wrote: >> Because unlike in algol 68 in python whitespace is relevant, >> we could get by with requiring whitespace: >> x= -q # okay >> a> 8 ** -2 # okay > > This is actually quite thoroughly untrue. In python, *indentation* is > significant. Whitespace (internal to a line) is not. [snip] Whitespace (internal to a line) _mostly_ is not. It's not allowed within names, and it is needed in the second example above, before the 'and's. From exarkun at divmod.com Fri Jul 17 16:59:53 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Fri, 17 Jul 2009 16:59:53 -0400 Subject: proposal: add setresuid() system call to python In-Reply-To: <20090717200141.GI6065@subspacefield.org> Message-ID: <20090717205953.2543.1223422516.divmod.quotient.3129@henry.divmod.com> On Fri, 17 Jul 2009 15:01:41 -0500, travis+ml-python at subspacefield.org wrote: >Hello, > > [snip] > >I am suggesting that the setresuid function be added to python, >perhaps in the OS module, because it has the clearest semantics for >manipulating user ids. The reason why is best described in the >following paper: Yes, it'd be good for Python to expose setresuid. The best course of action is to file a ticket in the issue tracker. Things will be sped along if you also attach a patch implementing the change. :) Jean-Paul From piet at cs.uu.nl Fri Jul 17 16:59:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 22:59:54 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: I should add that *if the wait times out* it still could be the case that your answer has arrived but that the notify occured a microsecond after the timeout. So knowing if the timeout occurred really doesn't tell you much. It's just a way to prevent infinite waiting. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From tjreedy at udel.edu Fri Jul 17 16:59:59 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 16:59:59 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: Message-ID: Sion Arrowsmith wrote: > Jack Diederich wrote: >> It isn't an OrderedDict thing, it is a comparison thing. Two regular >> dicts also raise an error if you try to LT them. > > Since when? > > Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> d1 = dict((str(i), i) for i in range (10)) >>>> d2 = dict((str(i), i) for i in range (20)) >>>> d1 < d2 > True Try reversing the definitions of d1 and d2. The dicts are probably being compared by id (address), which is the 2.x CPython default. > (Don't have a 2.6 or 3 to hand.) 2.6 same. 3.1 >>> d1,d2 = {},{} >>> d1 < d2 Traceback (most recent call last): File "", line 1, in d1 < d2 TypeError: unorderable types: dict() < dict() From aahz at pythoncraft.com Fri Jul 17 17:03:59 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Jul 2009 14:03:59 -0700 Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> <94c1adf3-a25d-4ae5-9f38-7ca8680d022d@b14g2000yqd.googlegroups.com> Message-ID: In article <94c1adf3-a25d-4ae5-9f38-7ca8680d022d at b14g2000yqd.googlegroups.com>, hartley wrote: >On Jul 16, 9:26=A0pm, a... at pythoncraft.com (Aahz) wrote: >> In article <0afc5c4d-1af5-4d0e-9442-26b51b12e... at m11g2000yqh.googlegroups= >.com>, >> hartley =A0 wrote: >>> >>>If you had loosened up on the sarcasm I would probably have read what >>>you wrote more thoroughly instead of just skimming through it. Thanks >>>for the help, but you should seriously consider doing something with >>>your patronizing attitude. >> >> http://www.mikeash.com/getting_answers.htmlhttp://www.catb.org/~esr/faqs/= >smart-questions.html > >From "http://www.mikeash.com/getting_answers.html": > >How To Ask Questions The Smart Way is pretty condescending, especially >if you already feel insulted by somebody telling you that you need >help asking questions. If you're pointed at a guide with a filename of >smart-questions, that means this person thinks you have stupid >questions, and who needs that? > >Cheers mate :) Not really. Feel free to chide people trying to help you, eventually people won't care. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From tjreedy at udel.edu Fri Jul 17 17:09:40 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 17:09:40 -0400 Subject: 'del' function for Dictionary In-Reply-To: References: Message-ID: mayank gupta wrote: > Hi all, > > I wanted to know whether there is a more efficient way to delete an > entry from a dictionary (instead of using the 'del' function), because > after analyzing the time taken by the code, it seems to me that the > 'del' function takes most of the time. That is hard to imagine. del ob[sub] has the same effect as ob.__delitem__(sub) but I suspect this would take longer since it requires a runtime attribute lookup and Python level function call. So I am pretty sure the answer is 'no'. tjr From python at mrabarnett.plus.com Fri Jul 17 17:31:30 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 22:31:30 +0100 Subject: Generator Expressions and CSV In-Reply-To: <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: <4A60EDB2.5050202@mrabarnett.plus.com> Zaki wrote: > On Jul 17, 2:49 pm, MRAB wrote: >> Zaki wrote: >>> Hey all, >>> I'm really new to Python and this may seem like a really dumb >>> question, but basically, I wrote a script to do the following, however >>> the processing time/memory usage is not what I'd like it to be. Any >>> suggestions? >>> Outline: >>> 1. Read tab delim files from a directory, files are of 3 types: >>> install, update, and q. All 3 types contain ID values that are the >>> only part of interest. >>> 2. Using set() and set.add(), generate a list of unique IDs from >>> install and update files. >>> 3. Using the set created in (2), check the q files to see if there are >>> matches for IDs. Keep all matches, and add any non matches (which only >>> occur once in the q file) to a queue of lines to be removed from teh q >>> files. >>> 4. Remove the lines in the q for each file. (I haven't quite written >>> the code for this, but I was going to implement this using csv.writer >>> and rewriting all the lines in the file except for the ones in the >>> removal queue). >>> Now, I've tried running this and it takes much longer than I'd like. I >>> was wondering if there might be a better way to do things (I thought >>> generator expressions might be a good way to attack this problem, as >>> you could generate the set, and then check to see if there's a match, >>> and write each line that way). >> Why are you checking and removing lines in 2 steps? Why not copy the >> matching lines to a new q file and then replace the old file with the >> new one (or, maybe, delete the new q file if no lines were removed)? > > That's what I've done now. > > Here is the final code that I have running. It's very much 'hack' type > code and not at all efficient or optimized and any help in optimizing > it would be greatly appreciated. > > import csv > import sys > import os > import time > > begin = time.time() > > #Check minutes elapsed > def timeElapsed(): > current = time.time() > elapsed = current-begin > return round(elapsed/60) > > > #USAGE: python logcleaner.py > > inputdir = sys.argv[1] > outputdir = sys.argv[2] > > logfilenames = os.listdir(inputdir) > > > > IDs = set() #IDs from update and install logs > foundOnceInQuery = set() > #foundTwiceInQuery = set() > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > Queue of IDs to remove from query logs (IDs found only once in query > logs) > > #Generate Filename Queues For Install/Update Logs, Query Logs > iNuQ = [] > queryQ = [] > > for filename in logfilenames: > if filename.startswith("par1.install") or filename.startswith > ("par1.update"): if filename.startswith(("par1.install", "par1.update")): > iNuQ.append(filename) > elif filename.startswith("par1.query"): > queryQ.append(filename) > > totalfiles = len(iNuQ) + len(queryQ) > print "Total # of Files to be Processed:" , totalfiles > print "Install/Update Logs to be processed:" , len(iNuQ) > print "Query logs to be processed:" , len(queryQ) > > #Process install/update queue to generate list of valid IDs > currentfile = 1 > for file in iNuQ: > print "Processing", currentfile, "install/update log out of", len > (iNuQ) > print timeElapsed() > reader = csv.reader(open(inputdir+file),delimiter = '\t') > for row in reader: > IDs.add(row[2]) > currentfile+=1 Best not to call it 'file'; that's a built-in name. Also you could use 'enumerate', and joining filepaths is safer with os.path.join(). for currentfile, filename in enumerate(iNuQ, start=1): print "Processing", currentfile, "install/update log out of", len(iNuQ) print timeElapsed() current_path = os.path.join(inputdir, filename) reader = csv.reader(open(current_path), delimiter = '\t') for row in reader: IDs.add(row[2]) > > print "Finished processing install/update logs" > print "Unique IDs found:" , len(IDs) > print "Total Time Elapsed:", timeElapsed() > > currentfile = 1 > for file in queryQ: Similar remarks to above ... > print "Processing", currentfile, "query log out of", len(queryQ) > print timeElapsed() > reader = csv.reader(open(inputdir+file), delimiter = '\t') > outputfile = csv.writer(open(outputdir+file), 'w') ... and also here. > for row in reader: > if row[2] in IDs: > ouputfile.writerow(row) Should be 'outputfile'. > else: > if row[2] in foundOnceInQuery: > foundOnceInQuery.remove(row[2]) You're removing the ID here ... > outputfile.writerow(row) > #IDremovalQ.remove(row[2]) > #foundTwiceInQuery.add(row[2]) > > else: > foundOnceInQuery.add(row[2]) ... and adding it again here! > #IDremovalQ.add(row[2]) > > > currentfile+=1 > For safety you should close the files after use. > print "Finished processing query logs and writing new files" > print "# of Query log entries removed:" , len(foundOnceInQuery) > print "Total Time Elapsed:", timeElapsed() > Apart from that, it looks OK. How big are the q files? If they're not too big and most of the time you're not removing rows, you could put the output rows into a list and then create the output file only if rows have been removed, otherwise just copy the input file, which might be faster. From emile at fenx.com Fri Jul 17 17:56:12 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 17 Jul 2009 14:56:12 -0700 Subject: Generator Expressions and CSV In-Reply-To: <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: On 7/17/2009 1:08 PM Zaki said... > Here is the final code that I have running. It's very much 'hack' type > code and not at all efficient or optimized and any help in optimizing > it would be greatly appreciated. There are some things I'd approach differently , eg I might prefer glob to build iNuQ and queryQ [1], and although glob is generally fast, I'm not sure it'd be faster. But overall it looks like most of the time is spent in your 'for row in' loops, and as you're reading each file only once, and would have to anyway, there's not much that'll improve overall timing. I don't know what csvreader is doing under the covers, but if your files are reasonably sized for your system you might try timing something that reads in the full file and splits: for each in filelist: for row in open(filelist).readlines(): if row.split()[2] in .... -----[1]----- import glob iNuQ = glob.glob(os.sep.join(inputdir,"par1.install*") queryQ = glob.glob(os.sep.join(inputdir,"par1.query*") Emile From lie.1296 at gmail.com Fri Jul 17 17:58:21 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 18 Jul 2009 07:58:21 +1000 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: Xavier Ho wrote: > oops, wrong address. > > When will reply-to tag appear on the Python mailing list? =/ > > Good idea, Steven and MRAB. > > I changed my code to the following: > > > def nPrime(self, n): > "Returns nth prime number, the first one being 2, where n = 0. > When n = 1, it returns 3." > for x in range(n+2): > > try: > return self.primes[n] > except IndexError: > self.next() If you're looking for speed, you should be aware that a failed try-clause is quite expensive in python and often (as in this case) codes using exception can be less readable than the one without since a program's flow in the case of exception is not linear (the non-linear execution is useful for quickly breaking out from nests of clauses, but not necessary in this case). Semantically, it is also wrong. Not finding the n'th prime in the cache is not really an exceptional case. If you insists on using a try-except clause, use it this way: # untested def nPrime(self, n): try: return self.primes[n] except IndexError: for _ in xrange(len(self.primes), n+1): self.next() return self.primes[n] From lodmod.dod at gmail.com Fri Jul 17 17:58:46 2009 From: lodmod.dod at gmail.com (LoD MoD) Date: Fri, 17 Jul 2009 14:58:46 -0700 Subject: creating my own logwatch in python Message-ID: I am trying to fabricate a logwatch-type script in Python and I'm having some trouble.What happens with the following code is that it prints to the screen but the if condition on line 22 never gets executed. http://pastie.org/549975 Any suggestions are appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 17 18:07:13 2009 From: contact at xavierho.com (Xavier Ho) Date: Sat, 18 Jul 2009 06:07:13 +0800 Subject: Try... except....Try again? In-Reply-To: References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: <2d56febf0907171507y75f6436ete75cb3f748ee838d@mail.gmail.com> On Sat, Jul 18, 2009 at 5:58 AM, Lie Ryan wrote: > > > If you're looking for speed, you should be aware that a failed > try-clause is quite expensive in python and often (as in this case) > codes using exception can be less readable than the one without since a > program's flow in the case of exception is not linear (the non-linear > execution is useful for quickly breaking out from nests of clauses, but > not necessary in this case). Was not aware that myself. Noted. > Semantically, it is also wrong. Not finding the n'th prime in the cache > is not really an exceptional case. > > If you insists on using a try-except clause, use it this way: > > Well, I don't have to use try-except. I think I got another idea though. I could use an internal counter to keep track of how many prime numbers have generated, and if not enough, generate more. Thanks. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From mabdelkader at gmail.com Fri Jul 17 18:33:55 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Fri, 17 Jul 2009 18:33:55 -0400 Subject: creating my own logwatch in python In-Reply-To: References: Message-ID: <148918f0907171533y4044b171h54e91c142a21b6cf@mail.gmail.com> A few code critiques: - Your code is not very help friendly because it can not be run, so I have to deadlist debug it. - There are various syntax errors in the code: - for example: file is not defined when you invoke tail(file) - You are overshadowing a built-in type 'file' by that name, which is confusing. PEP 8 states that if you want to use a keyword, the practice is to put an underscore after it. So, it should become file_ - Getopt is very old and should not be used. You should use the better, readable, optparse - That while loop will consume 99% CPU because of the infinite loop - line = file.readline() is guaranteed to return a newline OR an empty string ONLY when an EOF is encountered since you're not specifying the optional maximum read byte size. This means that all your characters are strings. So, why do you explicitly convert each character to a string and separate each character by a space? (line 18) - Line 19 will always return 0 because of line 18 - Line 19->22 are completely redundant, since readline is guaranteed to give you ONE line. You should just do if "uccess" in logString: ... Hope this helps, mahmoud mack abdelkader http://blog.mahmoudimus.com/ On Fri, Jul 17, 2009 at 5:58 PM, LoD MoD wrote: > I am trying to fabricate a logwatch-type script in Python and I'm having > some trouble.What happens with the following code is that it prints to the > screen but the if condition on line 22 never gets executed. > > http://pastie.org/549975 > > Any suggestions are appreciated. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zaki.rahaman at gmail.com Fri Jul 17 18:39:33 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 15:39:33 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: <1e8faacc-f4bc-420a-8028-88e405abb81f@d4g2000yqa.googlegroups.com> On Jul 17, 5:31?pm, MRAB wrote: > Zaki wrote: > > On Jul 17, 2:49 pm, MRAB wrote: > >> Zaki wrote: > >>> Hey all, > >>> I'm really new to Python and this may seem like a really dumb > >>> question, but basically, I wrote a script to do the following, however > >>> the processing time/memory usage is not what I'd like it to be. Any > >>> suggestions? > >>> Outline: > >>> 1. Read tab delim files from a directory, files are of 3 types: > >>> install, update, and q. All 3 types contain ID values that are the > >>> only part of interest. > >>> 2. Using set() and set.add(), generate a list of unique IDs from > >>> install and update files. > >>> 3. Using the set created in (2), check the q files to see if there are > >>> matches for IDs. Keep all matches, and add any non matches (which only > >>> occur once in the q file) to a queue of lines to be removed from teh q > >>> files. > >>> 4. Remove the lines in the q for each file. (I haven't quite written > >>> the code for this, but I was going to implement this using csv.writer > >>> and rewriting all the lines in the file except for the ones in the > >>> removal queue). > >>> Now, I've tried running this and it takes much longer than I'd like. I > >>> was wondering if there might be a better way to do things (I thought > >>> generator expressions might be a good way to attack this problem, as > >>> you could generate the set, and then check to see if there's a match, > >>> and write each line that way). > >> Why are you checking and removing lines in 2 steps? Why not copy the > >> matching lines to a new q file and then replace the old file with the > >> new one (or, maybe, delete the new q file if no lines were removed)? > > > That's what I've done now. > > > Here is the final code that I have running. It's very much 'hack' type > > code and not at all efficient or optimized and any help in optimizing > > it would be greatly appreciated. > > > import csv > > import sys > > import os > > import time > > > begin = time.time() > > > #Check minutes elapsed > > def timeElapsed(): > > ? ? current = time.time() > > ? ? elapsed = current-begin > > ? ? return round(elapsed/60) > > > #USAGE: python logcleaner.py > > > inputdir = sys.argv[1] > > outputdir = sys.argv[2] > > > logfilenames = os.listdir(inputdir) > > > IDs = set() #IDs from update and install logs > > foundOnceInQuery = set() > > #foundTwiceInQuery = set() > > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > > Queue of IDs to remove from query logs (IDs found only once in query > > logs) > > > #Generate Filename Queues For Install/Update Logs, Query Logs > > iNuQ = [] > > queryQ = [] > > > for filename in logfilenames: > > ? ? if filename.startswith("par1.install") or filename.startswith > > ("par1.update"): > > ? ? ? if filename.startswith(("par1.install", "par1.update")): > > > ? ? ? ? iNuQ.append(filename) > > ? ? elif filename.startswith("par1.query"): > > ? ? ? ? queryQ.append(filename) > > > totalfiles = len(iNuQ) + len(queryQ) > > print "Total # of Files to be Processed:" , totalfiles > > print "Install/Update Logs to be processed:" , len(iNuQ) > > print "Query logs to be processed:" , len(queryQ) > > > #Process install/update queue to generate list of valid IDs > > currentfile = 1 > > for file in iNuQ: > > ?> ? ? print "Processing", currentfile, "install/update log out of", len > ?> (iNuQ) > ?> ? ? print timeElapsed() > ?> ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > ?> ? ? for row in reader: > ?> ? ? ? ? IDs.add(row[2]) > ?> ? ? currentfile+=1 > > Best not to call it 'file'; that's a built-in name. > > Also you could use 'enumerate', and joining filepaths is safer with > os.path.join(). > > for currentfile, filename in enumerate(iNuQ, start=1): > ? ? ?print "Processing", currentfile, "install/update log out of", len(iNuQ) > ? ? ?print timeElapsed() > ? ? ?current_path = os.path.join(inputdir, filename) > ? ? ?reader = csv.reader(open(current_path), delimiter = '\t') > ? ? ?for row in reader: > ? ? ? ? ?IDs.add(row[2]) > > > > > print "Finished processing install/update logs" > > print "Unique IDs found:" , len(IDs) > > print "Total Time Elapsed:", timeElapsed() > > > currentfile = 1 > > for file in queryQ: > > Similar remarks to above ... > > > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > > ? ? print timeElapsed() > > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > > ... and also here. > > > ? ? for row in reader: > > ? ? ? ? if row[2] in IDs: > > ? ? ? ? ? ? ouputfile.writerow(row) > > Should be 'outputfile'. > > > ? ? ? ? else: > > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > > You're removing the ID here ... > > > ? ? ? ? ? ? ? ? outputfile.writerow(row) > > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > > ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > > ... and adding it again here! > > > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > > ? ? currentfile+=1 > > For safety you should close the files after use. > > > print "Finished processing query logs and writing new files" > > print "# of Query log entries removed:" , len(foundOnceInQuery) > > print "Total Time Elapsed:", timeElapsed() > > Apart from that, it looks OK. > > How big are the q files? If they're not too big and most of the time > you're not removing rows, you could put the output rows into a list and > then create the output file only if rows have been removed, otherwise > just copy the input file, which might be faster. MRAB, could you please repost what I sent to you here as I meant to post it in the main discussion. From joncle at googlemail.com Fri Jul 17 18:40:27 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 17 Jul 2009 15:40:27 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: <51ce01d3-4620-4e5f-b5b5-bd0e9fd6fd2c@w41g2000yqb.googlegroups.com> On 17 July, 21:08, Zaki wrote: > On Jul 17, 2:49?pm, MRAB wrote: > > > > > Zaki wrote: > > > Hey all, > > > > I'm really new to Python and this may seem like a really dumb > > > question, but basically, I wrote a script to do the following, however > > > the processing time/memory usage is not what I'd like it to be. Any > > > suggestions? > > > > Outline: > > > 1. Read tab delim files from a directory, files are of 3 types: > > > install, update, and q. All 3 types contain ID values that are the > > > only part of interest. > > > 2. Using set() and set.add(), generate a list of unique IDs from > > > install and update files. > > > 3. Using the set created in (2), check the q files to see if there are > > > matches for IDs. Keep all matches, and add any non matches (which only > > > occur once in the q file) to a queue of lines to be removed from teh q > > > files. > > > 4. Remove the lines in the q for each file. (I haven't quite written > > > the code for this, but I was going to implement this using csv.writer > > > and rewriting all the lines in the file except for the ones in the > > > removal queue). > > > > Now, I've tried running this and it takes much longer than I'd like. I > > > was wondering if there might be a better way to do things (I thought > > > generator expressions might be a good way to attack this problem, as > > > you could generate the set, and then check to see if there's a match, > > > and write each line that way). > > > Why are you checking and removing lines in 2 steps? Why not copy the > > matching lines to a new q file and then replace the old file with the > > new one (or, maybe, delete the new q file if no lines were removed)? > > That's what I've done now. > > Here is the final code that I have running. It's very much 'hack' type > code and not at all efficient or optimized and any help in optimizing > it would be greatly appreciated. > > import csv > import sys > import os > import time > > begin = time.time() > > #Check minutes elapsed > def timeElapsed(): > ? ? current = time.time() > ? ? elapsed = current-begin > ? ? return round(elapsed/60) > > #USAGE: python logcleaner.py > > inputdir = sys.argv[1] > outputdir = sys.argv[2] > > logfilenames = os.listdir(inputdir) > > IDs = set() #IDs from update and install logs > foundOnceInQuery = set() > #foundTwiceInQuery = set() > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > Queue of IDs to remove from query logs (IDs found only once in query > logs) > > #Generate Filename Queues For Install/Update Logs, Query Logs > iNuQ = [] > queryQ = [] > > for filename in logfilenames: > ? ? if filename.startswith("par1.install") or filename.startswith > ("par1.update"): > ? ? ? ? iNuQ.append(filename) > ? ? elif filename.startswith("par1.query"): > ? ? ? ? queryQ.append(filename) > > totalfiles = len(iNuQ) + len(queryQ) > print "Total # of Files to be Processed:" , totalfiles > print "Install/Update Logs to be processed:" , len(iNuQ) > print "Query logs to be processed:" , len(queryQ) > > #Process install/update queue to generate list of valid IDs > currentfile = 1 > for file in iNuQ: > ? ? print "Processing", currentfile, "install/update log out of", len > (iNuQ) > ? ? print timeElapsed() > ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > ? ? for row in reader: > ? ? ? ? IDs.add(row[2]) > ? ? currentfile+=1 > > print "Finished processing install/update logs" > print "Unique IDs found:" , len(IDs) > print "Total Time Elapsed:", timeElapsed() > > currentfile = 1 > for file in queryQ: > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > ? ? print timeElapsed() > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > ? ? for row in reader: > ? ? ? ? if row[2] in IDs: > ? ? ? ? ? ? ouputfile.writerow(row) > ? ? ? ? else: > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > ? ? ? ? ? ? ? ? outputfile.writerow(row) > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > ? ? currentfile+=1 > > print "Finished processing query logs and writing new files" > print "# of Query log entries removed:" , len(foundOnceInQuery) > print "Total Time Elapsed:", timeElapsed() Just a couple of ideas: 1) load the data into a sqlite3 database and use an SQL query to extract your results (has the potential of doing what you want without you coding it, plus if your requirements change, maybe somewhat more flexible) 2) Pre-sort your input files via ID, then match-merge (may add some time/space required to sort, but then the merge should be fairly quick, plus you'll have access to the entire row in the process, not just the ID) Jon. From zaki.rahaman at gmail.com Fri Jul 17 18:57:48 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 15:57:48 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> <51ce01d3-4620-4e5f-b5b5-bd0e9fd6fd2c@w41g2000yqb.googlegroups.com> Message-ID: <8298385a-6f4b-44a5-b171-4871833f8dd2@c36g2000yqn.googlegroups.com> On Jul 17, 6:40?pm, Jon Clements wrote: > On 17 July, 21:08, Zaki wrote: > > > > > On Jul 17, 2:49?pm, MRAB wrote: > > > > Zaki wrote: > > > > Hey all, > > > > > I'm really new to Python and this may seem like a really dumb > > > > question, but basically, I wrote a script to do the following, however > > > > the processing time/memory usage is not what I'd like it to be. Any > > > > suggestions? > > > > > Outline: > > > > 1. Read tab delim files from a directory, files are of 3 types: > > > > install, update, and q. All 3 types contain ID values that are the > > > > only part of interest. > > > > 2. Using set() and set.add(), generate a list of unique IDs from > > > > install and update files. > > > > 3. Using the set created in (2), check the q files to see if there are > > > > matches for IDs. Keep all matches, and add any non matches (which only > > > > occur once in the q file) to a queue of lines to be removed from teh q > > > > files. > > > > 4. Remove the lines in the q for each file. (I haven't quite written > > > > the code for this, but I was going to implement this using csv.writer > > > > and rewriting all the lines in the file except for the ones in the > > > > removal queue). > > > > > Now, I've tried running this and it takes much longer than I'd like. I > > > > was wondering if there might be a better way to do things (I thought > > > > generator expressions might be a good way to attack this problem, as > > > > you could generate the set, and then check to see if there's a match, > > > > and write each line that way). > > > > Why are you checking and removing lines in 2 steps? Why not copy the > > > matching lines to a new q file and then replace the old file with the > > > new one (or, maybe, delete the new q file if no lines were removed)? > > > That's what I've done now. > > > Here is the final code that I have running. It's very much 'hack' type > > code and not at all efficient or optimized and any help in optimizing > > it would be greatly appreciated. > > > import csv > > import sys > > import os > > import time > > > begin = time.time() > > > #Check minutes elapsed > > def timeElapsed(): > > ? ? current = time.time() > > ? ? elapsed = current-begin > > ? ? return round(elapsed/60) > > > #USAGE: python logcleaner.py > > > inputdir = sys.argv[1] > > outputdir = sys.argv[2] > > > logfilenames = os.listdir(inputdir) > > > IDs = set() #IDs from update and install logs > > foundOnceInQuery = set() > > #foundTwiceInQuery = set() > > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > > Queue of IDs to remove from query logs (IDs found only once in query > > logs) > > > #Generate Filename Queues For Install/Update Logs, Query Logs > > iNuQ = [] > > queryQ = [] > > > for filename in logfilenames: > > ? ? if filename.startswith("par1.install") or filename.startswith > > ("par1.update"): > > ? ? ? ? iNuQ.append(filename) > > ? ? elif filename.startswith("par1.query"): > > ? ? ? ? queryQ.append(filename) > > > totalfiles = len(iNuQ) + len(queryQ) > > print "Total # of Files to be Processed:" , totalfiles > > print "Install/Update Logs to be processed:" , len(iNuQ) > > print "Query logs to be processed:" , len(queryQ) > > > #Process install/update queue to generate list of valid IDs > > currentfile = 1 > > for file in iNuQ: > > ? ? print "Processing", currentfile, "install/update log out of", len > > (iNuQ) > > ? ? print timeElapsed() > > ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > > ? ? for row in reader: > > ? ? ? ? IDs.add(row[2]) > > ? ? currentfile+=1 > > > print "Finished processing install/update logs" > > print "Unique IDs found:" , len(IDs) > > print "Total Time Elapsed:", timeElapsed() > > > currentfile = 1 > > for file in queryQ: > > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > > ? ? print timeElapsed() > > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > > ? ? for row in reader: > > ? ? ? ? if row[2] in IDs: > > ? ? ? ? ? ? ouputfile.writerow(row) > > ? ? ? ? else: > > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > > ? ? ? ? ? ? ? ? outputfile.writerow(row) > > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > > ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > > ? ? currentfile+=1 > > > print "Finished processing query logs and writing new files" > > print "# of Query log entries removed:" , len(foundOnceInQuery) > > print "Total Time Elapsed:", timeElapsed() > > Just a couple of ideas: > > 1) load the data into a sqlite3 database and use an SQL query to > extract your results (has the potential of doing what you want without > you coding it, plus if your requirements change, maybe somewhat more > flexible) > > 2) Pre-sort your input files via ID, then match-merge (may add some > time/space required to sort, but then the merge should be fairly > quick, plus you'll have access to the entire row in the process, not > just the ID) > > Jon. Thanks Jon for the ideas, and yeah I might look into the SQLite solution especially since I might need to do other sub selections. I was also considering constructing a set for both install/update logs and then another set for query logs and then do an intersection/other set manipulations. What I was really interested in was seeing if I could use generators to try and accomplish what I'm doing especially since I'm uisng a lot of for loops with conditionals and at this point it wouldn't matter if the data is consumed in the intermediate steps before producing output. Any help with getting this code in generator form would be greatly appreciated. From db3l.net at gmail.com Fri Jul 17 19:09:35 2009 From: db3l.net at gmail.com (David Bolen) Date: Fri, 17 Jul 2009 19:09:35 -0400 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: Nobody writes: > On Thu, 16 Jul 2009 09:18:47 -0500, Tim Chase wrote: > >> Yes, the dictatorial "a tab always equals 8 spaces" > > Saying "always" is incorrect; it is more accurate to say that tab stops > are every 8 columns unless proven otherwise, with the burden of proof > falling on whoever wants to use something different. I suspect Tim was referring to the Python tokenizer. Internally, barring the existence of one of a few Emacs/vi tab setting commands in the file, Python always assigns the logical indentation level for a tab to align with the next multiple-of-8 column. This is unrelated to how someone might choose to display such a file. So mixing tabs and spaces and using a visual display setting of something other than 8 for the tab size (other than one consistent with an instruction embedded in the file) can yield a discrepancy between what is shown on the screen and how the same code is perceived by the Python compiler. This in turn may cause errors or code to execute at different indent levels than expected. Thus, in general, such a mixture is a bad idea, and as per this thread, no longer permitted in a single block in Python 3.x. -- David From dresdek at gmail.com Fri Jul 17 19:13:16 2009 From: dresdek at gmail.com (Matt) Date: Fri, 17 Jul 2009 16:13:16 -0700 (PDT) Subject: Unable to get Tkinter menubar to appear under OS X References: <3737fd18-4973-4cc3-914d-fbdefdbbb8a7@t10g2000vbg.googlegroups.com> <6a9b0831-ce2e-417b-ab4b-419629d0a25d@u16g2000pru.googlegroups.com> Message-ID: same problem on OS 10.4, menu doesn't show up on either from tkinter or wxpython, continuing search.. From ben+python at benfinney.id.au Fri Jul 17 19:25:47 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 18 Jul 2009 09:25:47 +1000 Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <87ws66evt0.fsf@benfinney.id.au> Phil writes: > I was thinking that I could just, for example, 'from datetime import > datetime' in __init__.py and have the ability to use 'datetime' > anywhere in any of the modules in 'package'. That would break one of the very nice features of Python's namespace system: that, because nothing[0] comes into a module without an explicit ?import foo?, you can always trace where ?foo? came from in the source code. > This didn't work for me. Did I just do something wrong? Is what I am > trying to do possible? If it were possible, we'd have the nightmare of not knowing where the names in a module were coming from, without having to search all over the package just to find where these implicit imports were taking place. No, thanks; I'm glad it's not possible. > Thanks in advance. [0] Two exceptions: There are a certain number of built-in names, strictly limited and well documented in the standard Python documentation. There are also those who use a namespace-clobbering ?from foo import *?, to which the solution is a clue-bat applied to the back of the skull. -- \ ?Pinky, are you pondering what I'm pondering?? ?Wuh, I think | `\ so, Brain, but how will we get three pink flamingos into one | _o__) pair of Capri pants?? ?_Pinky and The Brain_ | Ben Finney From joncle at googlemail.com Fri Jul 17 19:31:43 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 17 Jul 2009 16:31:43 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> <51ce01d3-4620-4e5f-b5b5-bd0e9fd6fd2c@w41g2000yqb.googlegroups.com> <8298385a-6f4b-44a5-b171-4871833f8dd2@c36g2000yqn.googlegroups.com> Message-ID: <3ba0cc18-2cd0-416c-ad69-c7be138607e0@k1g2000yqf.googlegroups.com> On 17 July, 23:57, Zaki wrote: > On Jul 17, 6:40?pm, Jon Clements wrote: > > > > > On 17 July, 21:08, Zaki wrote: > > > > On Jul 17, 2:49?pm, MRAB wrote: > > > > > Zaki wrote: > > > > > Hey all, > > > > > > I'm really new to Python and this may seem like a really dumb > > > > > question, but basically, I wrote a script to do the following, however > > > > > the processing time/memory usage is not what I'd like it to be. Any > > > > > suggestions? > > > > > > Outline: > > > > > 1. Read tab delim files from a directory, files are of 3 types: > > > > > install, update, and q. All 3 types contain ID values that are the > > > > > only part of interest. > > > > > 2. Using set() and set.add(), generate a list of unique IDs from > > > > > install and update files. > > > > > 3. Using the set created in (2), check the q files to see if there are > > > > > matches for IDs. Keep all matches, and add any non matches (which only > > > > > occur once in the q file) to a queue of lines to be removed from teh q > > > > > files. > > > > > 4. Remove the lines in the q for each file. (I haven't quite written > > > > > the code for this, but I was going to implement this using csv.writer > > > > > and rewriting all the lines in the file except for the ones in the > > > > > removal queue). > > > > > > Now, I've tried running this and it takes much longer than I'd like. I > > > > > was wondering if there might be a better way to do things (I thought > > > > > generator expressions might be a good way to attack this problem, as > > > > > you could generate the set, and then check to see if there's a match, > > > > > and write each line that way). > > > > > Why are you checking and removing lines in 2 steps? Why not copy the > > > > matching lines to a new q file and then replace the old file with the > > > > new one (or, maybe, delete the new q file if no lines were removed)? > > > > That's what I've done now. > > > > Here is the final code that I have running. It's very much 'hack' type > > > code and not at all efficient or optimized and any help in optimizing > > > it would be greatly appreciated. > > > > import csv > > > import sys > > > import os > > > import time > > > > begin = time.time() > > > > #Check minutes elapsed > > > def timeElapsed(): > > > ? ? current = time.time() > > > ? ? elapsed = current-begin > > > ? ? return round(elapsed/60) > > > > #USAGE: python logcleaner.py > > > > inputdir = sys.argv[1] > > > outputdir = sys.argv[2] > > > > logfilenames = os.listdir(inputdir) > > > > IDs = set() #IDs from update and install logs > > > foundOnceInQuery = set() > > > #foundTwiceInQuery = set() > > > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > > > Queue of IDs to remove from query logs (IDs found only once in query > > > logs) > > > > #Generate Filename Queues For Install/Update Logs, Query Logs > > > iNuQ = [] > > > queryQ = [] > > > > for filename in logfilenames: > > > ? ? if filename.startswith("par1.install") or filename.startswith > > > ("par1.update"): > > > ? ? ? ? iNuQ.append(filename) > > > ? ? elif filename.startswith("par1.query"): > > > ? ? ? ? queryQ.append(filename) > > > > totalfiles = len(iNuQ) + len(queryQ) > > > print "Total # of Files to be Processed:" , totalfiles > > > print "Install/Update Logs to be processed:" , len(iNuQ) > > > print "Query logs to be processed:" , len(queryQ) > > > > #Process install/update queue to generate list of valid IDs > > > currentfile = 1 > > > for file in iNuQ: > > > ? ? print "Processing", currentfile, "install/update log out of", len > > > (iNuQ) > > > ? ? print timeElapsed() > > > ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > > > ? ? for row in reader: > > > ? ? ? ? IDs.add(row[2]) > > > ? ? currentfile+=1 > > > > print "Finished processing install/update logs" > > > print "Unique IDs found:" , len(IDs) > > > print "Total Time Elapsed:", timeElapsed() > > > > currentfile = 1 > > > for file in queryQ: > > > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > > > ? ? print timeElapsed() > > > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > > > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > > > ? ? for row in reader: > > > ? ? ? ? if row[2] in IDs: > > > ? ? ? ? ? ? ouputfile.writerow(row) > > > ? ? ? ? else: > > > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > > > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > > > ? ? ? ? ? ? ? ? outputfile.writerow(row) > > > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > > > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > > > ? ? ? ? ? ? else: > > > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > > > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > > > ? ? currentfile+=1 > > > > print "Finished processing query logs and writing new files" > > > print "# of Query log entries removed:" , len(foundOnceInQuery) > > > print "Total Time Elapsed:", timeElapsed() > > > Just a couple of ideas: > > > 1) load the data into a sqlite3 database and use an SQL query to > > extract your results (has the potential of doing what you want without > > you coding it, plus if your requirements change, maybe somewhat more > > flexible) > > > 2) Pre-sort your input files via ID, then match-merge (may add some > > time/space required to sort, but then the merge should be fairly > > quick, plus you'll have access to the entire row in the process, not > > just the ID) > > > Jon. > > Thanks Jon for the ideas, and yeah I might look into the SQLite > solution especially since I might need to do other sub selections. I > was also considering constructing a set for both install/update logs > and then another set for query logs and then do an intersection/other > set manipulations. > > What I was really interested in was seeing if I could use generators > to try and accomplish what I'm doing especially since I'm uisng a lot > of for loops with conditionals and at this point it wouldn't matter if > the data is consumed in the intermediate steps before producing > output. Any help with getting this code in generator form would be > greatly appreciated. Well effectively, you are using generators. A very small optimisation might be to load the smallest set of data first (if you are already, I apologise!). Read the others and see if it's a matching ID, where it is, remove it from the smallest set, and those are the records to be added on the next parse. Posting when tired, so hope that makes some sense! Otherwise, I'd be tempted to pre-sort or use sqlite. From zac256 at gmail.com Fri Jul 17 20:31:44 2009 From: zac256 at gmail.com (Zac Burns) Date: Fri, 17 Jul 2009 17:31:44 -0700 Subject: Self optimizing iterable Message-ID: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> Greetings, I would like a set like object that when iterated maintains a count of where iteration stopped and then re-orders itself based on that count so that the iteration stopped on the most bubble to the top. An example use case for this would be for something like a large table of regular expressions that would be iterated over trying to match in some string. If some regular expressions are more statistically more successful then the iteration will generally be short. Does anyone know of a pre-existing recipe for this or feel like taking on the challenge? Bonus points for: Best possible BigO notation on switching order and iteration Threadsafety Extend to also include a mapping version -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games From http Fri Jul 17 20:40:32 2009 From: http (Paul Rubin) Date: 17 Jul 2009 17:40:32 -0700 Subject: Self optimizing iterable References: Message-ID: <7xbpniombj.fsf@ruckus.brouhaha.com> Zac Burns writes: > An example use case for this would be for something like a large table > of regular expressions that would be iterated over trying to match in > some string. If some regular expressions are more statistically more > successful then the iteration will generally be short. Generally if you are matching against a bunch of regexps, they will tend to match overlapping sets, so you want to control precisely what order they are tested in. Having stuff bubble around based on hit counts doesn't sound good. From zac256 at gmail.com Fri Jul 17 20:43:21 2009 From: zac256 at gmail.com (Zac Burns) Date: Fri, 17 Jul 2009 17:43:21 -0700 Subject: Retrieving a partial pickle Message-ID: <333edbe80907171743n6e72f5b8p7c1c6ab668cff7df@mail.gmail.com> I have a large pickle file, which happens to be a list with lots of objects in it. What sort of things can I do without unpickling the whole object? I would particularly like to retrieve one of the elements in the list without unpicking the whole object. If the answer is not specific to lists that would be useful (works on getting items in a dictionary, or doing introspection on the objects or whatever) -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games From python at mrabarnett.plus.com Fri Jul 17 20:54:22 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 18 Jul 2009 01:54:22 +0100 Subject: Self optimizing iterable In-Reply-To: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> References: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> Message-ID: <4A611D3E.90806@mrabarnett.plus.com> Zac Burns wrote: > Greetings, > > I would like a set like object that when iterated maintains a count of > where iteration stopped and then re-orders itself based on that count > so that the iteration stopped on the most bubble to the top. > > An example use case for this would be for something like a large table > of regular expressions that would be iterated over trying to match in > some string. If some regular expressions are more statistically more > successful then the iteration will generally be short. > > Does anyone know of a pre-existing recipe for this or feel like taking > on the challenge? > > Bonus points for: > Best possible BigO notation on switching order and iteration > Threadsafety > Extend to also include a mapping version > That's not a set, but a Most Recently Used list. From gagsl-py2 at yahoo.com.ar Fri Jul 17 21:09:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 17 Jul 2009 22:09:22 -0300 Subject: Self optimizing iterable References: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> Message-ID: En Fri, 17 Jul 2009 21:31:44 -0300, Zac Burns escribi?: > I would like a set like object that when iterated maintains a count of > where iteration stopped and then re-orders itself based on that count > so that the iteration stopped on the most bubble to the top. > > An example use case for this would be for something like a large table > of regular expressions that would be iterated over trying to match in > some string. If some regular expressions are more statistically more > successful then the iteration will generally be short. If you require a certain order, it's not a set; better to use a list. The code below extends the builtin list type to add a "promote" method; lst.promote(3) "bubbles" lst[3] up (exchanging lst[3] with lst[2]). It's thread safe, and O(1). import threading class XList(list): # @property def lock(self): lock = getattr(self, '_lock', None) if lock is None: lock = self._lock = threading.Lock() return lock # def promote(self, index): if index<0: index += len(self) if index>0: with self.lock: self[index], self[index-1] = self[index-1], self[index] py> a = XList('python') py> a ['p', 'y', 't', 'h', 'o', 'n'] py> a.promote(3) py> a ['p', 'y', 'h', 't', 'o', 'n'] py> a.promote(2) py> a ['p', 'h', 'y', 't', 'o', 'n'] py> a.promote(-1) py> a ['p', 'h', 'y', 't', 'n', 'o'] py> a.promote(0) py> a ['p', 'h', 'y', 't', 'n', 'o'] An example, looking for a matching regular expression: for index,expr in enumerate(exprlist): m = expr.match(txt) if m: dosomethingwith(m) exprlist.promote(index) break After many calls, the most frequently matched expressions appear towards the front of the list. > Extend to also include a mapping version I'm unsure if this point is still applicable. I'm using a list here, not a set as in your original proposal. -- Gabriel Genellina From pfeldman at verizon.net Fri Jul 17 21:09:37 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 17 Jul 2009 18:09:37 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <4A5DE5E7.5070003@mrabarnett.plus.com> References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> Message-ID: <24543805.post@talk.nabble.com> Suppose that 'xor' returns the value that is true when only one value is true, and False otherwise. This definition of xor doesn't have the standard associative property, that is, (a xor b) xor c will not necessarily equal a xor (b xor c) To see that this is the case, let a= 1, b= 2, and c= 3. (a xor b) xor c yields 3, while a xor (b xor c) yields 1. So, I'd prefer an xor operator that simply returns True or False. Phillip MRAB-2 wrote: > > > > > What values should 'xor' return? IMHO, if only one of the values is true > then it should return that value, otherwise it should return False. > > 1 xor 0 => 1 > 0 xor 2 => 2 > 1 xor 2 => False > 0 xor 0 => False > > This is because it's a Boolean operator, so it should fall back to > Boolean values when necessary, like 'not': > > not 0 => True > not 1 => False > > Also: > > x and y and z => (x and y) and z > x or y or z => (x or y) or z > > therefore: > > x xor y xor z => (x xor y) xor z > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24543805.html Sent from the Python - python-list mailing list archive at Nabble.com. From pfeldman at verizon.net Fri Jul 17 21:12:31 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 17 Jul 2009 18:12:31 -0700 (PDT) Subject: PDF version of Python Tutorial? Message-ID: <24543817.post@talk.nabble.com> Does anyone know if there is a PDF version of the Python Tutorial (URL= http://www.python.org/doc/current/tutorial/)? -- View this message in context: http://www.nabble.com/PDF-version-of-Python-Tutorial--tp24543817p24543817.html Sent from the Python - python-list mailing list archive at Nabble.com. From pavlovevidence at gmail.com Fri Jul 17 21:35:33 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 17 Jul 2009 18:35:33 -0700 (PDT) Subject: Self optimizing iterable References: <7xbpniombj.fsf@ruckus.brouhaha.com> Message-ID: <97c6fdf0-0364-4669-bdd5-35e8b567a38c@o36g2000vbl.googlegroups.com> On Jul 17, 5:40?pm, Paul Rubin wrote: > Zac Burns writes: > > An example use case for this would be for something like a large table > > of regular expressions that would be iterated over trying to match in > > some string. If some regular expressions are more statistically more > > successful then the iteration will generally be short. > > Generally if you are matching against a bunch of regexps, they will > tend to match overlapping sets, so you want to control precisely what > order they are tested in. ?Having stuff bubble around based on hit > counts doesn't sound good. As a corrollary, if you happen to be matching non-overlapping sets, then it is a red flag that there could be some other (better) way to dispatch than a linear regexp search. For instance, if all your regexps start with a different keyword, then you should dispatch on that keyword using a dictionary. A regexp can be used after dispatching to extract parameters if necessary. It's possible to have disjoint regexps without a simple dispatch criterion, but I'd guess that's less common. Carl Banks From ronn.ross at gmail.com Fri Jul 17 21:42:43 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Fri, 17 Jul 2009 21:42:43 -0400 Subject: Beginners question Message-ID: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> How do you define a global variable in a class. I tried this with do success: class ClassName: global_var = 1 def some_methos(): print global_var This doesn't work. What am I doing wrong? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjmachin at lexicon.net Fri Jul 17 21:54:11 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 17 Jul 2009 18:54:11 -0700 (PDT) Subject: Find first matching substring References: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> Message-ID: <470ebc06-3e84-41c8-bd8e-0a342bb4ea28@y10g2000prg.googlegroups.com> On Jul 18, 2:11?am, Eloff wrote: > Almost every time I've had to do parsing of text over the last 5 years > I've needed this function: [snip] > It finds the first matching substring in the target string, if there > is more than one match, it returns the position of the match that > occurs at the lowest index in the string. Alternatives: (1) re.search(r"sub0|sub1|...", ...) (2) google "Aho Corasick Python" (one result should be a thread in this newsgroup within the last week) From gabrielmonnerat at gmail.com Fri Jul 17 22:03:32 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Fri, 17 Jul 2009 23:03:32 -0300 Subject: Beginners question In-Reply-To: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: <4A612D74.80908@gmail.com> Ronn Ross wrote: > How do you define a global variable in a class. I tried this with do > success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? You need pass "self" to the function and "global_var" needs be called with self. class ClassName: global_var = 1 def some_methos(self): print self.global_var Gabriel M. Monnerat From marduk at letterboxes.org Fri Jul 17 22:13:24 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Fri, 17 Jul 2009 22:13:24 -0400 Subject: Beginners question In-Reply-To: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: <1247883209.28511.8.camel@centar> On Fri, 2009-07-17 at 21:42 -0400, Ronn Ross wrote: > How do you define a global variable in a class. I bit of a mix-up with words here. A variable can be a class variable or a global variable (wrt the module).. not both. > I tried this with do success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? You could have posted the error message... Two things are wrong here. The first, if some_methos() is a method of ClassName then it should have at least one parameter, which is the instance of the class (i.e. "self"), so def some_methos(self): The second problem is that global_var is not local so you need to specify the scope. You can either use print ClassName.global_var or print self.global_var I'm not sure which one is better though I prefer the former because it makes it clearer to the reader that it's a class variable and not an instance variable. HTH, -a From nohics at gmail.com Fri Jul 17 22:40:32 2009 From: nohics at gmail.com (nohics nohics) Date: Sat, 18 Jul 2009 03:40:32 +0100 Subject: Beginners question In-Reply-To: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: When defining your class methods, you *must* explicitly list self as the first argument for each method, including __init__. When you call a method of an ancestor class from within your class, you *must* include the selfargument. But when you call your class method from outside, you do not specify anything for the self argument; you skip it entirely, and Pythonautomatically adds the instance reference for you. I am aware that this is confusing at first; it's not really inconsistent, but it may appear inconsistent because it relies on a distinction (between bound and unbound methods) that you don't know about yet. So, you have to do: class ClassName: self.global_var = 1 def some_methos(self): print self.global_var 2009/7/18 Ronn Ross > How do you define a global variable in a class. I tried this with do > success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 17 22:48:28 2009 From: contact at xavierho.com (Xavier Ho) Date: Sat, 18 Jul 2009 10:48:28 +0800 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907171947s6a4a010fp124146d54d560bf4@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> <2d56febf0907171507y75f6436ete75cb3f748ee838d@mail.gmail.com> <4A611D89.7040603@ieee.org> <2d56febf0907171947s6a4a010fp124146d54d560bf4@mail.gmail.com> Message-ID: <2d56febf0907171948l35e73c14gf02bc3c6ef11d493@mail.gmail.com> Darn it. On Sat, Jul 18, 2009 at 8:55 AM, Dave Angel wrote: > > You don't need a counter. len() will tell you the size of the list of > primes. Does len() go through and count by itself, or does it actually keep track of the size and just return the memory? I always thought it would go through and count, which may take some time. I could be wrong. > > And, just in case you're still interested, that > for i in range() > > earlier in the thread has the overhead of building a (possibly) large list > of integers, just in case we might need to loop. Right, so for i in xrange() would be a better choice? Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From gabrielmonnerat at gmail.com Fri Jul 17 23:06:31 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Sat, 18 Jul 2009 00:06:31 -0300 Subject: Beginners question In-Reply-To: References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: <4A613C37.8090808@gmail.com> nohics nohics wrote: > When defining your class methods, you /must/ explicitly list self as > the first argument for each method, including __init__. When you call > a method of an ancestor class from within your class, you /must/ > include the self argument. But when you call your class method from > outside, you do not specify anything for the self argument; you skip > it entirely, and Python automatically adds the instance reference for > you. I am aware that this is confusing at first; it's not really > inconsistent, but it may appear inconsistent because it relies on a > distinction (between bound and unbound methods) that you don't know > about yet. > > So, you have to do: > > class ClassName: > self.global_var = 1 self isn't exists in this context. >>> class ClassName: ... self.global_var = 1 ... def some_methods(self): ... print self.global_var ... Traceback (most recent call last): File "", line 1, in File "", line 2, in ClassName NameError: name 'self' is not defined If you want a variable of instance you can use __init__ >>> class ClassName: ... def __init__(self): ... self.global_var = 1 ... def some_methods(self): ... print self.global_var Now "global_var" is created when ClassName is instantiated >>> ClassName.global_var Traceback (most recent call last): File "", line 1, in AttributeError: class ClassName has no attribute 'global_var' >>> c = ClassName() >>> c.global_var 1 > def some_methos(self): > print self.global_var > > 2009/7/18 Ronn Ross > > > How do you define a global variable in a class. I tried this with > do success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? > > -- > http://mail.python.org/mailman/listinfo/python-list > > From drobinow at gmail.com Fri Jul 17 23:09:44 2009 From: drobinow at gmail.com (David Robinow) Date: Fri, 17 Jul 2009 23:09:44 -0400 Subject: PDF version of Python Tutorial? In-Reply-To: <24543817.post@talk.nabble.com> References: <24543817.post@talk.nabble.com> Message-ID: <4eb0089f0907172009t1ba9331bx24042c497b1d2b96@mail.gmail.com> On Fri, Jul 17, 2009 at 9:12 PM, Dr. Phillip M. Feldman wrote: > > Does anyone know if there is a PDF version of the Python Tutorial (URL= > http://www.python.org/doc/current/tutorial/)? http://docs.python.org/download.html From gagsl-py2 at yahoo.com.ar Fri Jul 17 23:34:35 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 18 Jul 2009 00:34:35 -0300 Subject: PDF version of Python Tutorial? References: <24543817.post@talk.nabble.com> Message-ID: En Fri, 17 Jul 2009 22:12:31 -0300, Dr. Phillip M. Feldman escribi?: > Does anyone know if there is a PDF version of the Python Tutorial (URL= > http://www.python.org/doc/current/tutorial/)? From that page, click on the top left corner to go up one level, and you'll see a download link. Or, starting on http://www.python.org/doc/ you can navigate to the desired document. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 17 23:34:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 18 Jul 2009 00:34:39 -0300 Subject: Beginners question References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: En Fri, 17 Jul 2009 22:42:43 -0300, Ronn Ross escribi?: > How do you define a global variable in a class. I tried this with do > success: > class ClassName: > global_var = 1 > > def some_method(self): > print global_var > > This doesn't work. What am I doing wrong? [some typos fixed] In Python, a "variable" is just a name in a namespace that references another object. There are five possible namespaces here: - The local namespace, that is, the namespace inside some_method. All assigned-to names that appear inside the function (there is none in this case) are in the local namespace. But you're not interested on these local names, I presume, because you said "global" - The global namespace: the namespace of the module containing the code (NOT a "super-global" namespace shared by all modules). Names in this namespace are "global" in the sense that all code in the same module can reference them. But they're not restricted to any class, as you requested. To use a global name in an expression, just write it. To assign a new value to a global name, you must use the "global" statement if you're inside a function (else, it's considered a local name, see above). - The built-in namespace: like a super-global namespace, it is searched last when looking for an unqualified name; by example, the "len" function. For most purposes, you should consider the built-in namespace read-only. - The instance namespace: each object has its own namespace; you access its names using dotted attributes: obj.name -- or self.attribute when inside a method. The instance namespace is not determined by the object type or class; any object may contain any attribute name (in general). - The class namespace: classes may contain attributes too (like any other object, because classes are objects too). In your code above, you may use ClassName.global_var to refer to such class attribute. To resolve a dotted attribute name, Python searches the instance first -- and if no match is found, then the class namespace is searched too: this way, class attributes effectively are "default values" for instance attributes. Since all existing instances of a certain class share the same class, this is like a "shared attribute" between all instances (like "static members" in other languages). Inside some_method above, you may use self.global_var or ClassName.global_var -- but once you *assign* something to the instance attribute (self.global_var = xxx), the class attribute is shadowed and you may reference it using ClassName.global_var only. -- Gabriel Genellina From drobinow at gmail.com Fri Jul 17 23:51:46 2009 From: drobinow at gmail.com (David Robinow) Date: Fri, 17 Jul 2009 23:51:46 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: <4eb0089f0907172051v1b206a47r4326e7447f3d0637@mail.gmail.com> >> If you want to generate high-quality graphics easily you need different >> primitives. >> >> http://cairographics.org >> >> has Python bindings, but I don't know if it's available on Windows. > > Effectively not, as far as I can tell. PyCairo appears to be *nix and > require later binaries than those available from gtk site referenced from > cairo site. http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/1.4/ Works for me. From gagsl-py2 at yahoo.com.ar Sat Jul 18 00:13:18 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 18 Jul 2009 01:13:18 -0300 Subject: Retrieving a partial pickle References: <333edbe80907171743n6e72f5b8p7c1c6ab668cff7df@mail.gmail.com> Message-ID: En Fri, 17 Jul 2009 21:43:21 -0300, Zac Burns escribi?: > I have a large pickle file, which happens to be a list with lots of > objects in it. > > What sort of things can I do without unpickling the whole object? > > I would particularly like to retrieve one of the elements in the list > without unpicking the whole object. I'm afraid you can't do that, in general. A pickle is actually *code* for a stack-based machine; you have to execute all the previous instructions to have a sane stack contents; it's not possible (at least, not easily) just to jump in the middle of the pickle and execute just a section. > If the answer is not specific to lists that would be useful (works on > getting items in a dictionary, or doing introspection on the objects > or whatever) The pickletools module might be a starting point: http://docs.python.org/library/pickletools.html -- Try the Python Cookbook too http://code.activestate.com/recipes/langs/python/ -- Gabriel Genellina From http Sat Jul 18 00:47:03 2009 From: http (Paul Rubin) Date: 17 Jul 2009 21:47:03 -0700 Subject: Einstein summation notation References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> Message-ID: <7xprbybnso.fsf@ruckus.brouhaha.com> Ethan Furman writes: > Or if any(p for p in [header, body, footer, whatever, ...]) No need for the genexp: if any([header, body, footer, whatever, ...]) But, you are using the built-in bool cast either way. From http Sat Jul 18 00:49:02 2009 From: http (Paul Rubin) Date: 17 Jul 2009 21:49:02 -0700 Subject: Retrieving a partial pickle References: Message-ID: <7xljmmbnpd.fsf@ruckus.brouhaha.com> Zac Burns writes: > I have a large pickle file, which happens to be a list with lots of > objects in it. > What sort of things can I do without unpickling the whole object? If it's for some kind of data recovery of a busted file, you could code some awful hack to pull stuff out of the middle of the pickle, but that's not the idea. You may want the shelve module. From torriem at gmail.com Sat Jul 18 00:49:52 2009 From: torriem at gmail.com (Michael Torrie) Date: Fri, 17 Jul 2009 22:49:52 -0600 Subject: getopt code NameError exception on logTail.py In-Reply-To: References: <4A60BE10.5050905@mrabarnett.plus.com> Message-ID: <4A615470.9080503@gmail.com> LoD MoD wrote: > In this instance the trackback was somewhat unhelpful.There problem was > here: > > file = open(filename, 'r') > > should be > > file = open(a, 'r') > > args should be passed within the getopt riff Well given the code you posted, this little "problem" you mention has nothing to do with the traceback. The code (as you posted it) still does not define "process" anywhere. Seems helpful enough to me. If you're not importing it from somewhere, then saying "it's defined elsewhere" is still an error! From torriem at gmail.com Sat Jul 18 00:55:26 2009 From: torriem at gmail.com (Michael Torrie) Date: Fri, 17 Jul 2009 22:55:26 -0600 Subject: Question regarding style/design.. In-Reply-To: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> Message-ID: <4A6155BE.80500@gmail.com> Wells Oliver wrote: > Sometimes I see relatively small application, generally task scripts, > written as essentially a list of statements. Other times, I see them neatly > divided into functions and then the "if __name__ == '__main__':" convention. > Is there a preference? Is there an... application scope such that the > preference shifts from the former to the latter? I understand the use of the > __name__ == 'main' convention for building unit tests, but I'm mixed on > using it in scripts/small applications. > Thanks for any thoughts! I always use the name guard thing for the simple reason that it lets me reuse code by importing it as a module into other things. My programs often serve double purposes. One is to be a standalone utility. The other is to be able to be used as a library from other code. So the name guard is essential. For code that is intended to be in modules, the name guard thing is indispensable for running unit tests. For very short, one-off things, I may leave it off. In any case I much prefer having a name guard, then running various functions than the Java style of writing a stupid static class with a static main method. Always seemed like a paradigm hack to me. From electronixtar at gmail.com Sat Jul 18 01:03:05 2009 From: electronixtar at gmail.com (est) Date: Fri, 17 Jul 2009 22:03:05 -0700 (PDT) Subject: ANN: psyco V2 References: Message-ID: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> On Jul 17, 10:48?am, Christian Tismer wrote: > Announcing Psyco V2 source release > ---------------------------------- > > This is the long awaited announcement of Psyco V2. > > Psyco V2 is a continuation of the well-known psyco project, > which was called finished and was dis-continued by its author > Armin Rigo in 2005, in favor of the PyPy project. > > This is a new project, using Psyco's code base with permission > of Armin. Questions and complaints should go to me > (tis... at stackless.com) or the mailing list > (psyco-de... at lists.sourceforge.net); > Armin is explicitly not in charge of (t)his project any longer! > > As one of the founders and an active member of the PyPy > project, I was very happy to be invited to work on Psyco > V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco > approach "an extension module that just makes Python faster". > > Psyco is a just-in-time compiler that accelerates arbitrary > Python code by specialization. We believe that Psyco's approach > can be carried out much further than it was tried so far, when > it's first version was abandoned. > > This first V2 release is source-only. There is no web-site, yet, > and there are no binaries for download. These will be available > in a few days onhttp://www.psyco.org. > > For the time being, please stick with subversion access, > building the extension module from source code. The repository > is here: > > ? ? ?http://codespeak.net/svn/psyco/v2/dist > > Check-out the repository, and run the setup.py script, > given that you have access to a C compiler. > > Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, > and Mac OS X. Psyco is not supporting 64 bit, yet. But it > is well being considered. > > The current improvements are, shortly: > > ? ?- Support for Python 2.4, 2.5 and 2.6 > ? ?- a lot of new builtins > ? ?- generators, fast and fully supported. > > More information is coming soon onhttp://www.psyco.org. > > This is the beginning of a series of new Psyco versions. > Many more improvements are prepared and about to be published, > soon, starting with the current version 2.0.0 . > > Stay tuned, this is just the beginning of psyco's re-birth! > > For questions about Psyco V2, please join the mailing list > > ? ? ?psyco-de... at lists.sourceforge.net > > or contact me on IRC: > > ? ? ?#psyco on irc.freenode.net . > > Psyco V2 is fundamentally supported by FATTOC, LLC. > Seehttp://www.fattoc.com. > > Without their continuous support, this work would not have > been possible at all. I wish to express my deepest thanks > to FATTOC, for allowing me to continue on Psyco with all the > energy that this ambitious project needs, and will need. > > Further special thanks are going to > Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, > Raymond Hettinger, Fabrizio Milo, Michael Foord, > Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, > for all the support and discussions. > > Looking forward to a great future of Psyco! > > July 17, 2009 > -- > Christian Tismer ? ? ? ? ? ? :^) ? > tismerysoft GmbH ? ? ? ? ? ? : ? ? Have a break! Take a ride on Python's > Johannes-Niemeyer-Weg 9A ? ? : ? ?*Starship*http://starship.python.net/ > 14109 Berlin ? ? ? ? ? ? ? ? : ? ? PGP key ->http://wwwkeys.pgp.net/ > work +49 30 802 86 56 ?mobile +49 173 24 18 776 ?fax +49 30 80 90 57 05 > PGP 0x57F3BF04 ? ? ? 9064 F4E1 D754 C2FF 1619 ?305B C09C 5A3B 57F3 BF04 > ? ? ? ?whom do you want to sponsor today? ?http://www.stackless.com/ congrats! btw is psyco.org owned? From tjreedy at udel.edu Sat Jul 18 01:44:58 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 18 Jul 2009 01:44:58 -0400 Subject: turtle dump In-Reply-To: <4eb0089f0907172051v1b206a47r4326e7447f3d0637@mail.gmail.com> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> <4eb0089f0907172051v1b206a47r4326e7447f3d0637@mail.gmail.com> Message-ID: David Robinow wrote: >>> If you want to generate high-quality graphics easily you need different >>> primitives. >>> >>> http://cairographics.org >>> >>> has Python bindings, but I don't know if it's available on Windows. >> Effectively not, as far as I can tell. PyCairo appears to be *nix and >> require later binaries than those available from gtk site referenced from >> cairo site. > > http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/1.4/ > Works for me. Thanks. I did not see that referenced from the cairo site. Now if only there were a 3.1 version.... tjr From __peter__ at web.de Sat Jul 18 02:42:15 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 08:42:15 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> Message-ID: Terry Reedy wrote: > alex23 wrote: > >> The help in iPython says the same, but also mentions that it's a >> dynamically generated function, so it may not be picking up the >> docstring that way. turtle.ScrolledCanvas.postscript is similarly >> terse, but you can find more info in turtle.Canvas.postscript: >> >> Print the contents of the canvas to a postscript >> file. Valid options: colormap, colormode, file, fontmap, >> height, pageanchor, pageheight, pagewidth, pagex, pagey, >> rotate, witdh, x, y. > > How, exactly, did you get that list? > > tjr [Python 3.1] >>> import tkinter >>> print(tkinter.Canvas.postscript.__doc__) Print the contents of the canvas to a postscript file. Valid options: colormap, colormode, file, fontmap, height, pageanchor, pageheight, pagewidth, pagex, pagey, rotate, witdh, x, y. Peter From tismer at stackless.com Sat Jul 18 02:50:07 2009 From: tismer at stackless.com (Christian Tismer) Date: Sat, 18 Jul 2009 08:50:07 +0200 Subject: ANN: psyco V2 In-Reply-To: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> References: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> Message-ID: <5661F6B6-371B-46C3-BAE6-FFE280DC78A3@stackless.com> It is just being transferred Von meinem iTouch gesendet On Jul 18, 2009, at 7:03, est wrote: > On Jul 17, 10:48 am, Christian Tismer wrote: >> Announcing Psyco V2 source release >> ---------------------------------- >> >> This is the long awaited announcement of Psyco V2. >> >> Psyco V2 is a continuation of the well-known psyco project, >> which was called finished and was dis-continued by its author >> Armin Rigo in 2005, in favor of the PyPy project. >> >> This is a new project, using Psyco's code base with permission >> of Armin. Questions and complaints should go to me >> (tis... at stackless.com) or the mailing list >> (psyco-de... at lists.sourceforge.net); >> Armin is explicitly not in charge of (t)his project any longer! >> >> As one of the founders and an active member of the PyPy >> project, I was very happy to be invited to work on Psyco >> V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco >> approach "an extension module that just makes Python faster". >> >> Psyco is a just-in-time compiler that accelerates arbitrary >> Python code by specialization. We believe that Psyco's approach >> can be carried out much further than it was tried so far, when >> it's first version was abandoned. >> >> This first V2 release is source-only. There is no web-site, yet, >> and there are no binaries for download. These will be available >> in a few days onhttp://www.psyco.org. >> >> For the time being, please stick with subversion access, >> building the extension module from source code. The repository >> is here: >> >> http://codespeak.net/svn/psyco/v2/dist >> >> Check-out the repository, and run the setup.py script, >> given that you have access to a C compiler. >> >> Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, >> and Mac OS X. Psyco is not supporting 64 bit, yet. But it >> is well being considered. >> >> The current improvements are, shortly: >> >> - Support for Python 2.4, 2.5 and 2.6 >> - a lot of new builtins >> - generators, fast and fully supported. >> >> More information is coming soon onhttp://www.psyco.org. >> >> This is the beginning of a series of new Psyco versions. >> Many more improvements are prepared and about to be published, >> soon, starting with the current version 2.0.0 . >> >> Stay tuned, this is just the beginning of psyco's re-birth! >> >> For questions about Psyco V2, please join the mailing list >> >> psyco-de... at lists.sourceforge.net >> >> or contact me on IRC: >> >> #psyco on irc.freenode.net . >> >> Psyco V2 is fundamentally supported by FATTOC, LLC. >> Seehttp://www.fattoc.com. >> >> Without their continuous support, this work would not have >> been possible at all. I wish to express my deepest thanks >> to FATTOC, for allowing me to continue on Psyco with all the >> energy that this ambitious project needs, and will need. >> >> Further special thanks are going to >> Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, >> Raymond Hettinger, Fabrizio Milo, Michael Foord, >> Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, >> for all the support and discussions. >> >> Looking forward to a great future of Psyco! >> >> July 17, 2009 >> -- >> Christian Tismer :^) >> tismerysoft GmbH : Have a break! Take a ride on >> Python's >> Johannes-Niemeyer-Weg 9A : *Starship*http://starship.python.net/ >> 14109 Berlin : PGP key ->http://wwwkeys.pgp.net/ >> work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 >> PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 >> BF04 >> whom do you want to sponsor today? http://www.stackless.com/ > > congrats! > > btw is psyco.org owned? > -- > http://mail.python.org/mailman/listinfo/python-list From amrita at iisermohali.ac.in Sat Jul 18 03:09:21 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 18 Jul 2009 12:39:21 +0530 (IST) Subject: how two join and arrange two files together Message-ID: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> Hi, I have two files having entries like:-- fileA 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 and fileB ChainA: ALA8 -67.217297 -37.131330 ChainA: ALA21 -69.822977 -48.871282 ChainA: ALA23 -59.148095 -46.540043 ChainA: ALA33 -65.459303 -43.269718 i want to join thses two files in such a way that the output file will contain column of both the files and the enties of similar position of ALA will be together.so the output file should look something like:---- fileC 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA: ALA8 -67.217297 -37.131330 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 ChainA: ALA21 -69.822977 -48.871282 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA: ALA23 -59.148095 -46.540043 ChainA: ALA33 -65.459303 -43.269718 Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From kalyanchakravarthy at hyit.com Sat Jul 18 03:14:23 2009 From: kalyanchakravarthy at hyit.com (Kalyan Chakravarthy) Date: Sat, 18 Jul 2009 12:44:23 +0530 Subject: Python import Error Message-ID: <8ca278430907180014k460f3798mafef6fc59379677b@mail.gmail.com> Hi All, I am using* Python 2.6, MySQL 4.0* , I have successfully Instaled MySQLdb (*MySQL-python-1.2.3c1.win32-py2.6*) in my system. I tested through command prompt with "import MySQLdb ", its not shwing any errors (means its instaled successfully), I set Eneceranment variable for Python I am running simple application with import "MySQLdb" to get the connection to MySQL, But its throwing "No module named MySQLdb", Please any one tell me what couled be the reasion is there any version miss match with python and MySQL ? Since last 3days I am strugling on this -- Regards Kalyan -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Sat Jul 18 03:19:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 00:19:23 -0700 Subject: how two join and arrange two files together In-Reply-To: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> On Sat, Jul 18, 2009 at 12:09 AM, wrote: > > Hi, > > I have two files having entries like:-- > fileA > 8 ?ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 > and > fileB > ChainA: ALA8 ? ?-67.217297 ? ? ?-37.131330 > ChainA: ALA21 ? -69.822977 ? ? ?-48.871282 > ChainA: ALA23 ? -59.148095 ? ? ?-46.540043 > ChainA: ALA33 ? -65.459303 ? ? ?-43.269718 > > i want to join thses two files in such a way that the output file will > contain column of both the files and the enties of similar position of ALA > will be together.so the output file should look something like:---- > fileC > 8 ?ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA: > ALA8 ? ?-67.217297 ? ? ?-37.131330 > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ChainA: > ALA21 ? -69.822977 ? ? ?-48.871282 > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA: > ALA23 ? -59.148095 ? ? ?-46.540043 > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ChainA: > ALA33 ? -65.459303 ? ? ?-43.269718 This mailinglist is not a collection of free code monkeys. Show us you've at least /tried/ to write this yourself, and tell us where you're running into problems or what error you're getting. See also http://catb.org/esr/faqs/smart-questions.html Additionally, you might consider asking on the Indian Python mailinglist instead: http://mail.python.org/mailman/listinfo/bangpypers - Chris From amrita at iisermohali.ac.in Sat Jul 18 03:39:09 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 18 Jul 2009 13:09:09 +0530 (IST) Subject: how two join and arrange two files together In-Reply-To: <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: <23421.210.212.36.65.1247902749.squirrel@www.iisermohali.ac.in> I tried to join these two files together using command....... from itertools import izip from os.path import exists def parafiles(*files): vec = (open(f) for f in files if exists(f)) data = izip(*vec) [f.close() for f in vec] return data for data in parafiles('/home/amrita/alachems/chem1.txt', '/home/amrita/secstr/secstr.txt'): print ' '.join(d.strip() for d in data) it just joined the column of two files. > On Sat, Jul 18, 2009 at 12:09 AM, wrote: >> >> Hi, >> >> I have two files having entries like:-- >> fileA >> 8 ??ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 >> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 >> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 >> and >> fileB >> ChainA: ALA8 ?? ??-67.217297 ?? ?? ??-37.131330 >> ChainA: ALA21 ?? -69.822977 ?? ?? ??-48.871282 >> ChainA: ALA23 ?? -59.148095 ?? ?? ??-46.540043 >> ChainA: ALA33 ?? -65.459303 ?? ?? ??-43.269718 >> >> i want to join thses two files in such a way that the output file will >> contain column of both the files and the enties of similar position of >> ALA >> will be together.so the output file should look something like:---- >> fileC >> 8 ??ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA: >> ALA8 ?? ??-67.217297 ?? ?? ??-37.131330 >> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 >> ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? >> ?? ?? ?? ?? ?? ChainA: >> ALA21 ?? -69.822977 ?? ?? ??-48.871282 >> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA: >> ALA23 ?? -59.148095 ?? ?? ??-46.540043 >> ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? >> ?? ?? ?? ?? ?? ChainA: >> ALA33 ?? -65.459303 ?? ?? ??-43.269718 > > This mailinglist is not a collection of free code monkeys. Show us > you've at least /tried/ to write this yourself, and tell us where > you're running into problems or what error you're getting. > > See also http://catb.org/esr/faqs/smart-questions.html > > Additionally, you might consider asking on the Indian Python > mailinglist instead: > http://mail.python.org/mailman/listinfo/bangpypers > > - Chris > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From __peter__ at web.de Sat Jul 18 05:17:33 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 11:17:33 +0200 Subject: how two join and arrange two files together References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: amrita at iisermohali.ac.in wrote: > I tried to join these two files together using command....... > > from itertools import izip > from os.path import exists > > def parafiles(*files): > vec = (open(f) for f in files if exists(f)) > data = izip(*vec) > [f.close() for f in vec] > return data > > for data in parafiles('/home/amrita/alachems/chem1.txt', > '/home/amrita/secstr/secstr.txt'): > print ' '.join(d.strip() for d in data) parafiles has a bug: vec is a generator and hence cannot be run twice. Therefore the odd [f.close()...] list comprehension (don't use a list comp if you don't care about the result!) has no effect. If you change vec into a list you will be hit by another problem -- the for loop trying to operate on closed files. While the correct approach probably involves a contextlib.contextmanager I recommend that you concentrate on your real problem and keep parafiles() simple: def parafiles(*files): open_files = (open(f) for f in files if exits(f)) return izip(*open_files) > it just joined the column of two files. Can you make an effort to express clearly what you want, preferrably with a simple and unambiguous example? Please keep the line widths below the threshold of 78 characters to avoid messing it up on its way to the reader. Peter From amrita at iisermohali.ac.in Sat Jul 18 05:29:28 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 18 Jul 2009 14:59:28 +0530 (IST) Subject: how two join and arrange two files together In-Reply-To: References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: <2569.210.212.36.65.1247909368.squirrel@www.iisermohali.ac.in> I want to join column of two different data file but i want that the entries will match (example i mentioned in my first mail, the position of ALA eill match) if its not matching then it will get printed as such. > amrita at iisermohali.ac.in wrote: > >> I tried to join these two files together using command....... >> >> from itertools import izip >> from os.path import exists >> >> def parafiles(*files): >> vec = (open(f) for f in files if exists(f)) >> data = izip(*vec) >> [f.close() for f in vec] >> return data >> >> for data in parafiles('/home/amrita/alachems/chem1.txt', >> '/home/amrita/secstr/secstr.txt'): >> print ' '.join(d.strip() for d in data) > > parafiles has a bug: vec is a generator and hence cannot be run twice. > Therefore the odd [f.close()...] list comprehension (don't use a list comp > if you don't care about the result!) has no effect. > > If you change vec into a list you will be hit by another problem -- the > for > loop trying to operate on closed files. While the correct approach > probably > involves a contextlib.contextmanager I recommend that you concentrate on > your real problem and keep parafiles() simple: > > def parafiles(*files): > open_files = (open(f) for f in files if exits(f)) > return izip(*open_files) > >> it just joined the column of two files. > > Can you make an effort to express clearly what you want, preferrably with > a > simple and unambiguous example? > > Please keep the line widths below the threshold of 78 characters to avoid > messing it up on its way to the reader. > > Peter > > -- > http://mail.python.org/mailman/listinfo/python-list > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From dickinsm at gmail.com Sat Jul 18 05:50:06 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sat, 18 Jul 2009 02:50:06 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <4A5F5759.4010600@sequans.com> <1247774222.4a5f860ec3cfa@mail.uh.cu> Message-ID: <9a623957-aece-413f-817a-88c76b9e25f4@y17g2000yqn.googlegroups.com> On Jul 17, 12:06?pm, Jean-Michel Pichavant wrote: > I was saying that using boolean operators with object instead of boolean > values is error prone, I agree with this to some extent. After all, Python conditional expressions were eventually introduced in response to buggy uses of the 'a and b or c' idiom. See PEP 308, and: http://mail.python.org/pipermail/python-dev/2005-September/056546.html In my own code, I'm finding myself increasingly using conditional expressions where I would once have used 'and' or 'or': daysInAdvance = int(inputVar) if inputVar is not None else 0 -- Mark From markbak at gmail.com Sat Jul 18 06:21:57 2009 From: markbak at gmail.com (Mark Bakker) Date: Sat, 18 Jul 2009 12:21:57 +0200 Subject: difference in printing to screen Mac / Windows Message-ID: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> Hello list I notice a difference between running the following script on my Mac and on a PC: from time import sleep for i in range(10): print i, sleep(2) On my PC this prints a number every 2 seconds. This is the behavior I want. On my Mac Python waits 10*2 = 20 seconds, and then prints 0 1 2 3 4 5 6 7 8 9 Any thoughts on how I can make Python behave to get the Python behavior of my PC on my Mac? Thanks, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sat Jul 18 06:40:01 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 12:40:01 +0200 Subject: how two join and arrange two files together References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: amrita at iisermohali.ac.in wrote: >> Can you make an effort to express clearly what you want, preferrably with >> a simple and unambiguous example? > I want to join column of two different data file but i want that the > entries will match (example i mentioned in my first mail, the position of > ALA eill match) if its not matching then it will get printed as such. Just say "No" if you mean it. My best guess: from collections import defaultdict def merge(sources): blanks = [blank for items, blank, keyfunc in sources] d = defaultdict(lambda: blanks[:]) for index, (items, blank, keyfunc) in enumerate(sources): for item in items: d[keyfunc(item)][index] = item for key in sorted(d): yield d[key] if __name__ == "__main__": from StringIO import StringIO a = StringIO("""\ a alpha c beta d gamma """) b = StringIO("""\ a one b two d three e four """) c = StringIO("""\ a 111 b 222 f 333 """) def key(line): return line[:1] def source(stream, blank="", key=key): return (line.strip() for line in stream), blank, key for m in merge([source(x) for x in [a,b,c]]): print "|".join(c.ljust(10) for c in m) From python.list at tim.thechases.com Sat Jul 18 07:03:11 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 18 Jul 2009 06:03:11 -0500 Subject: difference in printing to screen Mac / Windows In-Reply-To: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> References: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> Message-ID: <4A61ABEF.6020002@tim.thechases.com> > I notice a difference between running the following script on my Mac and on > a PC: > > from time import sleep > for i in range(10): > print i, > sleep(2) > > On my PC this prints a number every 2 seconds. This is the behavior I want. > > On my Mac Python waits 10*2 = 20 seconds, and then prints 0 1 2 3 4 5 6 7 8 > 9 This sounds like a buffered vs. non-buffered output issue. My guess would be that if you increased 10 to something larger, the output buffer would flush at various intervals. The solution on the Mac (or other *nix-like OSes) would be to either start python in unbuffered mode: python -u mycode.py Alternatively, you can force a flush of the output buffer at each iteration: import sys for i in range(10): print i sys.stdout.flush() sleep(2) Lastly, you can force all standard-output in your program to be unbuffered without the "-u" parameter: sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) Hope this helps, -tkc From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 07:21:32 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 12:21:32 +0100 Subject: Python graphics / imaging library Message-ID: Chaps, what's the most appropriate (maintained) graphics library to use? PIL seems to have last been updated in 2006 http://www.pythonware.com/products/pil/ and GD seems to be even older. Don't want to go down a dead end. Pete -- http://www.petezilla.co.uk From motoom at xs4all.nl Sat Jul 18 07:38:14 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Sat, 18 Jul 2009 13:38:14 +0200 Subject: Python graphics / imaging library In-Reply-To: References: Message-ID: <4A61B426.2090404@xs4all.nl> Peter Chant wrote: > what's the most appropriate (maintained) graphics library to use? PIL seems > to have last been updated in 2006 http://www.pythonware.com/products/pil/ > and GD seems to be even older. Don't want to go down a dead end. Contrary to organic material, software doesn't rot when it gets older. PIL is pretty complete for the task it was designed to do, pretty debugged during the past years, and pretty much 'finished' -- it doesn't need frequent updates anymore. Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 07:50:01 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 12:50:01 +0100 Subject: Python graphics / imaging library References: Message-ID: <9gp8j6xuh6.ln2@phoenix.fire> Michiel Overtoom wrote: > Peter Chant wrote: > >> what's the most appropriate (maintained) graphics library to use? PIL >> seems to have last been updated in 2006 >> http://www.pythonware.com/products/pil/ >> and GD seems to be even older. Don't want to go down a dead end. > > Contrary to organic material, software doesn't rot when it gets older. > > PIL is pretty complete for the task it was designed to do, pretty > debugged during the past years, and pretty much 'finished' -- it doesn't > need frequent updates anymore. > > Greetings, > No, it does not. However, if PIL was updated last in 2006. Python in 2009 has gone to version 3.1. If PIL is compatible with 3.1 then I'm fine. But I don't want to have to stick with Python 2.5 as the rest of the world moves on. Pete -- http://www.petezilla.co.uk From benjamin.kaplan at case.edu Sat Jul 18 08:26:22 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 18 Jul 2009 08:26:22 -0400 Subject: Python graphics / imaging library In-Reply-To: <9gp8j6xuh6.ln2@phoenix.fire> References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: On Sat, Jul 18, 2009 at 7:50 AM, Peter Chant < REMpeteOVE at cappetezilla.italsco.uk> wrote: > Michiel Overtoom wrote: > > > Peter Chant wrote: > > > >> what's the most appropriate (maintained) graphics library to use? PIL > >> seems to have last been updated in 2006 > >> http://www.pythonware.com/products/pil/ > >> and GD seems to be even older. Don't want to go down a dead end. > > > > Contrary to organic material, software doesn't rot when it gets older. > > > > PIL is pretty complete for the task it was designed to do, pretty > > debugged during the past years, and pretty much 'finished' -- it doesn't > > need frequent updates anymore. > > > > Greetings, > > > > No, it does not. However, if PIL was updated last in 2006. Python in 2009 > has gone to version 3.1. If PIL is compatible with 3.1 then I'm fine. But > I don't want to have to stick with Python 2.5 as the rest of the world > moves on. > The rest of the world hasn't moved on yet. Most people are still using Python 2.6 and the 2.x series will continue to be actively developed for another couple years. > Pete > > > -- > http://www.petezilla.co.uk > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 08:30:02 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 13:30:02 +0100 Subject: Python graphics / imaging library References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: Peter Chant wrote: > > No, it does not. However, if PIL was updated last in 2006. Python in > 2009 > has gone to version 3.1. If PIL is compatible with 3.1 then I'm fine. > But I don't want to have to stick with Python 2.5 as the rest of the world > moves on. BTW, this was not a critisism of PIL or GD, rather what do people generally use now? -- http://www.petezilla.co.uk From motoom at xs4all.nl Sat Jul 18 08:41:36 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Sat, 18 Jul 2009 14:41:36 +0200 Subject: Python graphics / imaging library In-Reply-To: References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: <4A61C300.9070503@xs4all.nl> Peter Chant wrote: > what do people generally use now? I can only speak for myself... I use PIL ;-) Greetings, From maxerickson at gmail.com Sat Jul 18 08:54:50 2009 From: maxerickson at gmail.com (Max Erickson) Date: Sat, 18 Jul 2009 12:54:50 +0000 (UTC) Subject: Python graphics / imaging library References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: Peter Chant wrote: > No, it does not. However, if PIL was updated last in 2006. > Python in 2009 has gone to version 3.1. If PIL is compatible > with 3.1 then I'm fine. But I don't want to have to stick with > Python 2.5 as the rest of the world moves on. > > Pete > > Various messages to the Image-SIG mailing list indicate that support for 3.x is coming after a release of 1.1.7: http://mail.python.org/pipermail/image-sig/2009-March/005498.html More here: http://mail.python.org/pipermail/image-sig/2009-March/thread.html More recent months contain updates to the status of 1.1.7, it is headed towards a release. Preliminary tarballs and binaries are available on effbot.org: http://effbot.org/downloads/#imaging http://effbot.org/downloads/#pil max From rhodri at wildebst.demon.co.uk Sat Jul 18 09:17:12 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sat, 18 Jul 2009 14:17:12 +0100 Subject: Beginners question In-Reply-To: <4A612D74.80908@gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> <4A612D74.80908@gmail.com> Message-ID: On Sat, 18 Jul 2009 03:03:32 +0100, gabrielmonnerat wrote: > Ronn Ross wrote: >> How do you define a global variable in a class. I tried this with do >> success: >> class ClassName: >> global_var = 1 >> def some_methos(): >> print global_var >> >> This doesn't work. What am I doing wrong? > You need pass "self" to the function and "global_var" needs be called > with self. > > class ClassName: > global_var = 1 > def some_methos(self): > print self.global_var If the OP really wants global_var to be global to the class, this approach will only work as long as he never assigns to self.global_var. The terminology is a bit confusing here. "Global variables" refers to variables which are global to an entire module, ones that you would use "global" to declare inside a function. Something like this: x = 5 def set_x(y): global x x = y set_x(3) print x ...gives the result "3" The idea of defining a global variable in a class is a bit of a nonsense, since globals exist for the entire module. There are, however, variables that exist and are the same for every instance of a class, or "class attributes" as they are normally called. You use them either by assignment in the class definition, or in a method by prefixing them with the class name. class ClassName: global_var = 1 def some_method(self): print ClassName.global_var ClassName.global_var = 2 "Instance attributes", the ones prefixed by "self", are what get used for most purposes. These are separate for each different instance of the class. class ClassName: def __init__(self, val): self.var = val a = ClassName(1) b = ClassName(2) print a.var, b.var ...gives the result "1 2" The wrinkle is that if you read from an instance attribute that doesn't exist, Python will use a class attribute of the same name if there is one. This is often exploited for setting default values. Beware, though: the moment you assign, an instance attribute is created -- the underlying class attribute is *not* changed. class ClassName: global_var = 1 def some_method(self): self.global_var = 2 a = ClassName() b = ClassName() b.some_method() print a.global_var, b.global_var ...gives the result "1 2" again! -- Rhodri James *-* Wildebeest Herder to the Masses From dstanek at dstanek.com Sat Jul 18 09:41:13 2009 From: dstanek at dstanek.com (David Stanek) Date: Sat, 18 Jul 2009 09:41:13 -0400 Subject: Python import Error In-Reply-To: <8ca278430907180014k460f3798mafef6fc59379677b@mail.gmail.com> References: <8ca278430907180014k460f3798mafef6fc59379677b@mail.gmail.com> Message-ID: On Sat, Jul 18, 2009 at 3:14 AM, Kalyan Chakravarthy wrote: > Hi All, > ?????????????????? I am using Python 2.6, MySQL 4.0 , I have successfully > Instaled MySQLdb (MySQL-python-1.2.3c1.win32-py2.6) in my system. I tested > through command prompt with "import MySQLdb ", its not shwing any errors > (means its instaled successfully), I set Eneceranment variable for Python > > I? am running simple application with import "MySQLdb" to get the connection > to MySQL, But its throwing "No module named MySQLdb", Please any one tell me > what couled be the reasion > > is there any version miss match with python and MySQL ? > > Since last 3days I am strugling on this > Off the top of my head I would say that maybe you are using one python binary from the command-line and another in your script or possibly your python path is foobar. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From tjcrone at gmail.com Sat Jul 18 10:34:11 2009 From: tjcrone at gmail.com (Timothy Crone) Date: Sat, 18 Jul 2009 10:34:11 -0400 Subject: Unpack Expects the Wrong Number of Bytes Message-ID: <78926b920907180734p7363af24v7f81f425cdd6d972@mail.gmail.com> Hello, I have noticed that for certain format strings, struct.unpack expects the wrong number of bytes. For example, this works fine header = "4si4s4si2h2i3h4s" data = list(unpack(header,f.read(42))) however, this header = "4si4s4si2h2i3h4si" data = list(unpack(header,f.read(46))) returns the following error: struct.error: unpack requires a string argument of length 48 So even though my format string includes an additional 4-byte integer, unpack is expecting 6 additional bytes. Here's a simpler example: This works: header = "s" data = list(unpack(header,f.read(1))) however this: header = "si" data = list(unpack(header,f.read(5))) throws struct.error: unpack requires a string argument of length 8 So unpack expects 7 additional bytes when an integer is added to the format string. Does anyone know what is going on? I am using Debian stable, so my Python version is 2.5.2. But I have replicated this with 2.6.2. Here's my proc/version: Linux version 2.6.30-bpo.1-amd64 (Debian 2.6.30-1~bpo50+1) (nobse at debian.org) (gcc version 4.3.2 (Debian 4.3.2-1.1) ) #1 SMP Fri Jun 26 09:41:55 UTC 2009 Any help would be greatly appreciated. Cheers, Tim From aahz at pythoncraft.com Sat Jul 18 10:44:29 2009 From: aahz at pythoncraft.com (Aahz) Date: 18 Jul 2009 07:44:29 -0700 Subject: setup.py not found References: <3be2bdce-680d-4b32-ad0c-ef46caf556e9@f10g2000vbf.googlegroups.com> Message-ID: In article <3be2bdce-680d-4b32-ad0c-ef46caf556e9 at f10g2000vbf.googlegroups.com>, Larry.Martell at gmail.com wrote: > >I'm trying to install a package (cx_Oracle) on a mac running 10.5.7. >I've done this on other platforms, but never on a mac. I followed the >instructions given, but when I try and run setup I get: > >Apollo:instantclient_10_2 user$ python setup.py build >/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/ >Python.app/Contents/MacOS/Python: can't open file 'setup.py': [Errno >2] No such file or directory > >Is there something else I have to install first to have this? Do you have a setup.py in the current working directory? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From sjmachin at lexicon.net Sat Jul 18 10:47:15 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 18 Jul 2009 07:47:15 -0700 (PDT) Subject: Unpack Expects the Wrong Number of Bytes References: Message-ID: <34a7b901-4d33-469a-8b35-5ac1f30304fa@i18g2000pro.googlegroups.com> On Jul 19, 12:34?am, Timothy Crone wrote: > Hello, > > I have noticed that for certain format strings, struct.unpack expects > the wrong number of bytes. [snip] > header = "si" > data = list(unpack(header,f.read(5))) > > throws > > struct.error: unpack requires a string argument of length 8 > > So unpack expects 7 additional bytes when an integer is added to the > format string. Does anyone know what is going on? Alignment. 1 byte for the "s", 3 pad bytes to get to the next "i" boundary, 4 bytes for the "i". http://docs.python.org/library/struct.html Then Ctrl-F search for "align" Choose a prefix character whose MO matches the struct that you need to work with. Cheers, John From python at mrabarnett.plus.com Sat Jul 18 10:53:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 18 Jul 2009 15:53:18 +0100 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907171948l35e73c14gf02bc3c6ef11d493@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> <2d56febf0907171507y75f6436ete75cb3f748ee838d@mail.gmail.com> <4A611D89.7040603@ieee.org> <2d56febf0907171947s6a4a010fp124146d54d560bf4@mail.gmail.com> <2d56febf0907171948l35e73c14gf02bc3c6ef11d493@mail.gmail.com> Message-ID: <4A61E1DE.8010300@mrabarnett.plus.com> Xavier Ho wrote: > Darn it. > > On Sat, Jul 18, 2009 at 8:55 AM, Dave Angel > wrote: > > > You don't need a counter. len() will tell you the size of the list > of primes. > > > Does len() go through and count by itself, or does it actually keep > track of the size and just return the memory? I always thought it would > go through and count, which may take some time. I could be wrong. > [snip] Lists (and strings, tuples, dicts and sets) know how many items they contain. From rridge at csclub.uwaterloo.ca Sat Jul 18 10:55:53 2009 From: rridge at csclub.uwaterloo.ca (Ross Ridge) Date: Sat, 18 Jul 2009 10:55:53 -0400 Subject: Unpack Expects the Wrong Number of Bytes References: Message-ID: Timothy Crone wrote: >header = "s" >data = list(unpack(header,f.read(1))) > >however this: > >header = "si" >data = list(unpack(header,f.read(5))) > >throws > >struct.error: unpack requires a string argument of length 8 > >So unpack expects 7 additional bytes when an integer is added to the >format string. Does anyone know what is going on? It's adding pad bytes so that the format matches the equivilent C structure on your machine. You should use either the "<" little-endian or the ">" big-endian prefix depending on the byte order used in the file you're trying to unpack. You can also use the "=" native byte-order flag if endianness of the file format changes according to the machine your Python program runs on. If you use any of these flags, no extra padding will be inserted. Ross Ridge -- l/ // Ross Ridge -- The Great HTMU [oo][oo] rridge at csclub.uwaterloo.ca -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/ db // From alan.isaac at gmail.com Sat Jul 18 11:31:46 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sat, 18 Jul 2009 15:31:46 GMT Subject: invoke method on many instances In-Reply-To: <02701d35$0$5185$c3e8da3@news.astraweb.com> References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: > On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >> def apply2(itr, methodname, *args, **kwargs): >> f = operator.methodcaller(methodname, *args, **kwargs) >> for item in itr: >> f(item) On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: > for obj in objects: > getattr(obj, methodname)(*args, **kwargs) Are there any obvious considerations in choosing between those two? > See also these recipes from the "Python Cookbook": > http://code.activestate.com/recipes/52289/ > http://code.activestate.com/recipes/87370/ Interesting. Thanks, Alan From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 11:41:33 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 16:41:33 +0100 Subject: Python graphics / imaging library References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: Max Erickson wrote: > More recent months contain updates to the status of 1.1.7, it is > headed towards a release. Preliminary tarballs and binaries are > available on effbot.org: > > http://effbot.org/downloads/#imaging > http://effbot.org/downloads/#pil Excellent. From a very brief look it seems like it will be quite simple to use. Pete -- http://www.petezilla.co.uk From tkermode at gmail.com Sat Jul 18 11:50:44 2009 From: tkermode at gmail.com (Tom Kermode) Date: Sat, 18 Jul 2009 16:50:44 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: Maybe the IDE is the best place to warn you of something like that. You could have an IDE where you specify which language you're more familiar with and then have it display warnings likely to be relevant to you. People could collaborate to add support for gradually more niche languages. Python could continue to work the way it was designed to and people documenting python wouldn't have to worry about this kind of issue. :) 2009/7/6 Lawrence D'Oliveiro : > I wonder how many people have been tripped up by the fact that > > ? ?++n > > and > > ? ?--n > > fail silently for numeric-valued n. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.kiloday.com http://www.fourstopspast.com From marcusw at cox.net Sat Jul 18 11:58:47 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 18 Jul 2009 11:58:47 -0400 Subject: Python graphics / imaging library In-Reply-To: References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: On 7/18/2009 11:41 AM, Peter Chant wrote: > Max Erickson wrote: > >> More recent months contain updates to the status of 1.1.7, it is >> headed towards a release. Preliminary tarballs and binaries are >> available on effbot.org: >> >> http://effbot.org/downloads/#imaging >> http://effbot.org/downloads/#pil > > Excellent. From a very brief look it seems like it will be quite simple to > use. > > Pete > > Yes, it was fun to work with (for me at least). Marcus From gnarlodious at gmail.com Sat Jul 18 12:32:18 2009 From: gnarlodious at gmail.com (Gnarlodious) Date: Sat, 18 Jul 2009 09:32:18 -0700 (PDT) Subject: Rus Python script interactively Message-ID: In an interactive session (I am using iPython), what is the most elegant way to run a Python script from Terminal? Right now I am saying: import subprocess subprocess.call("python /path/to/scriptname.py", shell=True) But I am calling a shell process and I'd rather not. The script just runs, no inputs are needed. Is there a more Pythonesque method? -- Gnarlie http://Gnarlodious.com/ From deets at nospam.web.de Sat Jul 18 12:39:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 18 Jul 2009 18:39:53 +0200 Subject: Rus Python script interactively In-Reply-To: References: Message-ID: <7cec6pF26munvU1@mid.uni-berlin.de> Gnarlodious schrieb: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? If it's in the path, you can just import it. That will of course only be possible once per ipython-session. Diez From lie.1296 at gmail.com Sat Jul 18 12:55:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 19 Jul 2009 02:55:49 +1000 Subject: Rus Python script interactively In-Reply-To: References: Message-ID: Gnarlodious wrote: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? > > -- Gnarlie > http://Gnarlodious.com/ IMHO, starting a new terminal and runs the script from there. Or quitting/pausing the current interactive session and run it like normal python scripts. Alternatively, you may also want to look at `execfile` built-in function. >>> execfile('myscript.py', {}, {}) From marcusw at cox.net Sat Jul 18 13:04:07 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 18 Jul 2009 13:04:07 -0400 Subject: Rus Python script interactively In-Reply-To: References: Message-ID: On 7/18/2009 12:32 PM, Gnarlodious wrote: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? > > -- Gnarlie > http://Gnarlodious.com/ You could put the code of the script in a main() function and have an if __name__ == '__main__': around the internal call. That way, it will still run the code if you run it normally, and you can also run it several times from the interactive session, ie: file you want to run: {{{ def somefunction(): print 'hello world' somefunction() }}} file after modifications: {{{ def somefunction(): print 'hello world' def main(): somefunction() if __name__ == '__main__': #only true if the file is being run normally, not imported. main() }}} Both of those, if run normally, will print "hello world". If the first one is imported, it will run once and not me runnable in that session again. If the second one is imported, it will not do anything until you call the main() function, and then you can call it again as many times as you want: {{{ >>> import thefile >>> for i in range(5): ... thefile.main() ... hello world hello world hello world hello world hello world >>> exit() }}} Hope this helps you! Marcus From __peter__ at web.de Sat Jul 18 13:13:17 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 19:13:17 +0200 Subject: Rus Python script interactively References: Message-ID: Gnarlodious wrote: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? After a quick glance over ipython's %quickref: In [1]: !mkdir -p path/to In [2]: !echo 'print "works!"' > path/to/scriptname.py In [3]: %run path/to/scriptname.py works! From timphotoman at gmail.com Sat Jul 18 13:25:11 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 13:25:11 -0400 Subject: python command running old version Message-ID: My brain is running in n00b mode this morning...must find coffee. I upgraded python this morning and entering python from the command line runs the old version. Just looked and it appears the old version is in /usr/bin while the new one is in /usr/local/bin/ Besides changing the path order, how do I ensure it runs the new version? I'm sure I'll bang my head on the desk in shame as soon as I'm reminded. Thanks. From clp2 at rebertia.com Sat Jul 18 13:26:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 10:26:40 -0700 Subject: python command running old version In-Reply-To: References: Message-ID: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> On Sat, Jul 18, 2009 at 10:25 AM, Tim Edwards wrote: > My brain is running in n00b mode this morning...must find coffee. > > I upgraded python this morning and entering python from the command > line runs the old version. Which OS? How did you install it? Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Sat Jul 18 13:32:25 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 10:32:25 -0700 Subject: Auto Send URL In-Reply-To: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> References: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> Message-ID: <50697b2c0907181032x6f0029dfs304a345ca2835690@mail.gmail.com> On Fri, Jul 17, 2009 at 6:02 AM, Victor Subervi wrote: > Hi; > I am trying to script code that automatically sends a Web site visitor to an > URL. Specifically, when they enter a value in a search box, I have that form > sent to a script that writes an URL acceptable to Google, then I want to > send the visitor off without him having to click another button. How do I do > this? Send back a webpage with a Refresh meta tag: Cheers, Chris -- http://blog.rebertia.com From lists at cheimes.de Sat Jul 18 13:33:48 2009 From: lists at cheimes.de (Christian Heimes) Date: Sat, 18 Jul 2009 19:33:48 +0200 Subject: python command running old version In-Reply-To: References: Message-ID: Tim Edwards wrote: > Besides changing the path order, how do I ensure it runs the new > version? I'm sure I'll bang my head on the desk in shame as soon as > I'm reminded. hash -r Christian From timphotoman at gmail.com Sat Jul 18 13:37:50 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 13:37:50 -0400 Subject: python command running old version In-Reply-To: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> References: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> Message-ID: > Which OS? How did you install it? Sorry (see I need that coffee) Installed on redhat from source From timphotoman at gmail.com Sat Jul 18 13:54:31 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 13:54:31 -0400 Subject: python command running old version In-Reply-To: References: Message-ID: >> Besides changing the path order, how do I ensure it runs the new >> version? I'm sure I'll bang my head on the desk in shame as soon as >> I'm reminded. > > hash -r Thanks. From lie.1296 at gmail.com Sat Jul 18 13:58:13 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 19 Jul 2009 03:58:13 +1000 Subject: python command running old version In-Reply-To: References: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> Message-ID: Tim Edwards wrote: >> Which OS? How did you install it? > > Sorry (see I need that coffee) > > Installed on redhat from source You should consult the distro's (i.e. RedHat's) documentation/mailing list about changing the default python interpreter. In most cases, you will need to make sure all python-related packages are compatible with the new python version. In popular distros, there should be a script that will do that for you. From akhilanger at gmail.com Sat Jul 18 14:20:20 2009 From: akhilanger at gmail.com (akhil1988) Date: Sat, 18 Jul 2009 11:20:20 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24550497.post@talk.nabble.com> Thanks Nobody-38, it solved my problem immediately. --Thanks Again, Akhil Nobody-38 wrote: > > On Thu, 16 Jul 2009 20:26:39 -0700, akhil1988 wrote: > >> Well, you were write: unintentionally I removed strip(). But the problem >> does >> not ends here: >> >> I get this error now: >> >> File "./temp.py", line 488, in >> main() >> File "./temp.py", line 475, in main >> for line in sys.stdin: >> File "/usr/local/lib/python3.1/codecs.py", line 300, in decode >> (result, consumed) = self._buffer_decode(data, self.errors, final) >> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: >> invalid >> data >> >> for this line: >> ? > > Right. You're running in a locale whose encoding is UTF-8, but feeding > data which isn't valid UTF-8 to stdin. If you want to use data with a > different encoding, you need to replace sys.stdin, e.g.: > > import sys > import io > sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding = 'iso-8859-1') > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24550497.html Sent from the Python - python-list mailing list archive at Nabble.com. From akhilanger at gmail.com Sat Jul 18 14:25:53 2009 From: akhilanger at gmail.com (akhil1988) Date: Sat, 18 Jul 2009 11:25:53 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24550540.post@talk.nabble.com> Thanks David, it solved my problem immediately. I will follow your advise from next time but honestly I am new to python with not much knowledge about text formats. And the main portion of my project was not to deal with these, so I just wanted to get this solved as I was already struck at this for 2 days. If you think I am wrong in my approach to getting problems solved, please let me know. Your advise would be helpful in future for me. --Thanks Again, Akhil Scott David Daniels wrote: > > akhil1988 wrote: > > >> Nobody-38 wrote: >>> On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: > ... >>>>> In Python 3 you can't decode strings because they are Unicode strings >>>>> and it doesn't make sense to decode a Unicode string. You can only >>>>> decode encoded things which are byte strings. So you are mixing up >>>>> byte >>>>> strings and Unicode strings. >>>> ... I read a byte string from sys.stdin which needs to converted to >>>> unicode >>>> string for further processing. >>> In 3.x, sys.stdin (stdout, stderr) are text streams, which means that >>> they >>> read and write Unicode strings, not byte strings. >>> >>>> I cannot just remove the decode statement and proceed? >>>> This is it what it looks like: >>>> for line in sys.stdin: >>>> line = line.decode('utf-8').strip() >>>> if line == '': #do something here >>>> .... >>>> If I remove the decode statement, line == '' never gets true. >>> Did you inadvertently remove the strip() as well? >> ... unintentionally I removed strip().... >> I get this error now: >> File "./temp.py", line 488, in >> main() >> File "./temp.py", line 475, in main >> for line in sys.stdin: >> File "/usr/local/lib/python3.1/codecs.py", line 300, in decode >> (result, consumed) = self._buffer_decode(data, self.errors, final) >> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: >> invalid >> data > > (1) Do not top post. > (2) Try to fully understand the problem and proposed solution, rather > than trying to get people to tell you just enough to get your code > going. > (3) The only way sys.stdin can possibly return unicode is to do some > decoding of its own. your job is to make sure it uses the correct > decoding. So, if you know your source is always utf-8, try > something like: > > import sys > import io > > sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf8') > > for line in sys.stdin: > line = line.strip() > if line == '': > #do something here > .... > > --Scott David Daniels > Scott.Daniels at Acm.Org > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24550540.html Sent from the Python - python-list mailing list archive at Nabble.com. From timphotoman at gmail.com Sat Jul 18 15:35:19 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 15:35:19 -0400 Subject: python command running old version In-Reply-To: References: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> Message-ID: > You should consult the distro's (i.e. RedHat's) documentation/mailing > list about changing the default python interpreter. In most cases, you > will need to make sure all python-related packages are compatible with > the new python version. In popular distros, there should be a script > that will do that for you. Thanks, I'll look into that. From sgm at objexx.com Sat Jul 18 16:49:42 2009 From: sgm at objexx.com (SK) Date: Sat, 18 Jul 2009 13:49:42 -0700 (PDT) Subject: multiprocessing and freezing on Windows References: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Message-ID: <015ab41f-7433-4136-8b9c-809f96ae6384@o13g2000vbl.googlegroups.com> Thanks Gabriel. Posted as: http://bugs.python.org/issue6461 The multiprocessing author has tentatively confirmed the bug. From twgray2007 at gmail.com Sat Jul 18 17:33:48 2009 From: twgray2007 at gmail.com (twgray) Date: Sat, 18 Jul 2009 14:33:48 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? Message-ID: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> I am attempting to send a jpeg image file created on an embedded device over a wifi socket to a Python client running on a Linux pc (Ubuntu). All works well, except I don't know, on the pc client side, what the file size is? The following is a snippet: [code] f = open("frame.jpg",mode = 'wb') while True: data = self.s.recv(MAXPACKETLEN) if len(data) == 0: break recvd += len(data) f.write(data) f.close() [end] It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on the final packet, which will always be less than MAXPACKETLEN. I guess my question is, how do I detect end of data on the client side? From irmen.NOSPAM at xs4all.nl Sat Jul 18 17:43:47 2009 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sat, 18 Jul 2009 23:43:47 +0200 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> Message-ID: <4a624251$0$191$e4fe514c@news.xs4all.nl> twgray wrote: > I am attempting to send a jpeg image file created on an embedded > device over a wifi socket to a Python client running on a Linux pc > (Ubuntu). All works well, except I don't know, on the pc client side, > what the file size is? You don't. Sockets are just endless streams of bytes. You will have to design some form of 'wire protocol' that includes the length of the message that is to be read. For instance a minimalistic protocol could be the following: Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 bytes, decodes it into the integer that tells it the length, and then reads the correct amount of bytes from the socket. --irmen From gnarlodious at gmail.com Sat Jul 18 17:48:24 2009 From: gnarlodious at gmail.com (Gnarlodious) Date: Sat, 18 Jul 2009 14:48:24 -0700 (PDT) Subject: Rus Python script interactively References: Message-ID: <0814d92c-46bf-4421-88ea-499cb82a0be8@w41g2000yqb.googlegroups.com> Thanks for all the suggestions! The last solution is the one I was ooking for, I am really starting to like iPython. Learning all kinds of new tricks!. -- Gnarlie From tycho at tycho.ws Sat Jul 18 17:52:41 2009 From: tycho at tycho.ws (Tycho Andersen) Date: Sat, 18 Jul 2009 16:52:41 -0500 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <4a624251$0$191$e4fe514c@news.xs4all.nl> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> Message-ID: <49b3a7400907181452t3f87ee39n17b3dc15c3426d34@mail.gmail.com> On Sat, Jul 18, 2009 at 4:43 PM, Irmen de Jong wrote: > twgray wrote: >> >> I am attempting to send a jpeg image file created on an embedded >> device over a wifi socket to a Python client running on a Linux pc >> (Ubuntu). ?All works well, except I don't know, on the pc client side, >> what the file size is? > > You don't. Sockets are just endless streams of bytes. You will have to > design some form of 'wire protocol' that includes the length of the message > that is to be read. > For instance a minimalistic protocol could be the following: > Send 4 bytes that contain the length (an int) then the data itself. The > client reads 4 bytes, decodes it into the integer that tells it the length, > and then reads the correct amount of bytes from the socket. Exactly, sending the length first is the only way to know ahead of time. Alternatively, if you know what the end of the data looks like, you can look for that 'flag' as well, and stop trying to recv() after that. Some things that may be useful, though, are socket.settimeout() and socket.setblocking(). More information is availible in the docs: http://docs.python.org/library/socket.html. You need to be careful with this, though, since network latency may cause problems. Using these methods will keep your program from sitting in recv() forever, though. \t -- http://tycho.ws From twgray2007 at gmail.com Sat Jul 18 18:04:41 2009 From: twgray2007 at gmail.com (twgray) Date: Sat, 18 Jul 2009 15:04:41 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> Message-ID: <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> On Jul 18, 4:43?pm, Irmen de Jong wrote: > twgray wrote: > > I am attempting to send a jpeg image file created on an embedded > > device over a wifi socket to a Python client running on a Linux pc > > (Ubuntu). ?All works well, except I don't know, on the pc client side, > > what the file size is? ? > > You don't. Sockets are just endless streams of bytes. You will have to design some form > of 'wire protocol' that includes the length of the message that is to be read. > For instance a minimalistic protocol could be the following: > Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 > bytes, decodes it into the integer that tells it the length, and then reads the correct > amount of bytes from the socket. > > --irmen Thanks for the reply. But, now I have a newbie Python question. If I send a 4 byte address from the embedded device, how do I convert that, in Python, to a 4 byte, or long, number? From tjreedy at udel.edu Sat Jul 18 18:30:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 18 Jul 2009 18:30:08 -0400 Subject: invoke method on many instances In-Reply-To: References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: Alan G Isaac wrote: >> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>> def apply2(itr, methodname, *args, **kwargs): >>> f = operator.methodcaller(methodname, *args, **kwargs) >>> for item in itr: >>> f(item) > > > On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >> for obj in objects: >> getattr(obj, methodname)(*args, **kwargs) > > > Are there any obvious considerations in choosing > between those two? I would use the straightforward getattr idiom. From rickbking at comcast.net Sat Jul 18 18:30:52 2009 From: rickbking at comcast.net (Rick King) Date: Sat, 18 Jul 2009 18:30:52 -0400 Subject: uniicode and executing a process with subprocess.call, or os.system Message-ID: <4A624D1C.5010503@comcast.net> Hello, I want to copy files using subprocess.call or os.system where the file names are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. Windows stores all the file names in unicode so they are displayed ok in explorer, and I can read them into my program with listdir(u'.'), etc. and work with the names in the program. os.rename() can be used to rename such files successfully. But I want to be able to copy files using: cmdstr = u'copy' +u' /Y "'+pair[0]+u'" "'+pair[1]+u'"\n' cmdstr = cmdstr.encode(sys.getfilesystemencoding()) try: retcode = sp.call(cmdstr, shell=True) #SP=SUBPROCESS but the encoding can't handle all the characters and so the file isn't found to be copied. sp.call() returns 1. 'mbcs' encoding doesn't work either. 'utf-8' doesn't work. I am very confused about unicode. Can someone point me in the right direction? windows xp sp2 python 2.6.2 unicode Thanks! Rick King Southfield MI From python at mrabarnett.plus.com Sat Jul 18 19:05:48 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 00:05:48 +0100 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> Message-ID: <4A62554C.3040100@mrabarnett.plus.com> twgray wrote: > On Jul 18, 4:43 pm, Irmen de Jong wrote: >> twgray wrote: >>> I am attempting to send a jpeg image file created on an embedded >>> device over a wifi socket to a Python client running on a Linux pc >>> (Ubuntu). All works well, except I don't know, on the pc client side, >>> what the file size is? >> You don't. Sockets are just endless streams of bytes. You will have to design some form >> of 'wire protocol' that includes the length of the message that is to be read. >> For instance a minimalistic protocol could be the following: >> Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 >> bytes, decodes it into the integer that tells it the length, and then reads the correct >> amount of bytes from the socket. >> > Thanks for the reply. But, now I have a newbie Python question. If I > send a 4 byte address from the embedded device, how do I convert that, > in Python, to a 4 byte, or long, number? If you send the length as 4 bytes then you'll have to decide whether it's big-endian or little-endian. An alternative is to send the length as characters, terminated by, say, '\n' or chr(0). From clp2 at rebertia.com Sat Jul 18 19:06:51 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 16:06:51 -0700 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: <4A624D1C.5010503@comcast.net> References: <4A624D1C.5010503@comcast.net> Message-ID: <50697b2c0907181606n2af8577ew3ad178f0463fa2d8@mail.gmail.com> On Sat, Jul 18, 2009 at 3:30 PM, Rick King wrote: > Hello, > I want to copy files using subprocess.call or os.system where the file names > are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. Windows > stores all the file names in unicode so they are displayed ok in explorer, > and I can read them into my program with listdir(u'.'), etc. and work with > the names in the program. You should try one of the copying functions in the shutil module instead, it'll be much simpler than using subprocess: http://docs.python.org/library/shutil.html Cheers, Chris -- http://blog.rebertia.com From python at mrabarnett.plus.com Sat Jul 18 19:07:12 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 00:07:12 +0100 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: <4A624D1C.5010503@comcast.net> References: <4A624D1C.5010503@comcast.net> Message-ID: <4A6255A0.20804@mrabarnett.plus.com> Rick King wrote: > Hello, > I want to copy files using subprocess.call or os.system where the file > names are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. > Windows stores all the file names in unicode so they are displayed ok in > explorer, and I can read them into my program with listdir(u'.'), etc. > and work with the names in the program. > > os.rename() > > can be used to rename such files successfully. > > But I want to be able to copy files using: > > cmdstr = u'copy' +u' /Y "'+pair[0]+u'" "'+pair[1]+u'"\n' > cmdstr = cmdstr.encode(sys.getfilesystemencoding()) > try: retcode = sp.call(cmdstr, shell=True) #SP=SUBPROCESS > > but the encoding can't handle all the characters and so the file isn't > found to be copied. sp.call() returns 1. 'mbcs' encoding doesn't work > either. 'utf-8' doesn't work. > > I am very confused about unicode. Can someone point me in the right > direction? > > windows xp sp2 > python 2.6.2 unicode > Use the shutil module. From aahz at pythoncraft.com Sat Jul 18 19:18:22 2009 From: aahz at pythoncraft.com (Aahz) Date: 18 Jul 2009 16:18:22 -0700 Subject: python command running old version References: Message-ID: In article , Tim Edwards wrote: > >I upgraded python this morning and entering python from the command >line runs the old version. > >Just looked and it appears the old version is in /usr/bin while the >new one is in /usr/local/bin/ > >Besides changing the path order, how do I ensure it runs the new >version? I'm sure I'll bang my head on the desk in shame as soon as >I'm reminded. You probably want to create an alias for command-line usage; scripts should use the full path to the interpreter you want. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From sjmachin at lexicon.net Sat Jul 18 19:26:21 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 18 Jul 2009 16:26:21 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> Message-ID: On Jul 19, 8:04?am, twgray wrote: > send a 4 byte address from the embedded device, how do I convert that, > in Python, to a 4 byte, or long, number? struct.unpack() is your friend. Presuming the embedded device is little-endian, you do: the_int = struct.unpack(' Message-ID: On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote: > It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on > the final packet, which will always be less than MAXPACKETLEN. > > I guess my question is, how do I detect end of data on the client side? recv() should return zero when the sender closes its end of the connection. Is the sender actually closing its end? If you are unsure, use a packet sniffer such as tcpdump to look for a packet with the FIN flag. If you need to keep the connection open for further transfers, you need to incorporate some mechanism for identifying the end of the data into the protocol. As others have suggested, prefixing the data by its length is one option. Another is to use an end-of-data marker, but then you need a mechanism to "escape" the marker if it occurs in the data. A length prefix is probably simpler to implement, but has the disadvantage that you can't start sending the data until you know how long it is going to be. From foo at coo.com Sat Jul 18 19:59:45 2009 From: foo at coo.com (Dave) Date: Sun, 19 Jul 2009 00:59:45 +0100 Subject: Newbie question - running a command and looking at output Message-ID: <4a6261ff@212.67.96.135> I'm trying to run a command (arch -k) and check if the value returned is 'sun4v' or not. kirkby at t2:[~] $ arch -k sun4v In fact, I want to do 3 three things 1) Check if the system is Solaris. 2) If it is Solaris, check if 'arch -k' prints 'sun4v' 3) If both 1 and 2 are true, copy a file. Since arch -k is not supported on all systems, the test for Solaris must be made first. I have a test for Solaris (aka SunOS), which copies a file if the systems is running Solaris: import os, shutil if os.uname()[0] == 'SunOS': shutil.copy2('foo.c','bar.c') How can I change that, so the copy is only performed if 'arch -k' prints 'sun4v'? -- I respectfully request that this message is not archived by companies as unscrupulous as 'Experts Exchange' . In case you are unaware, 'Experts Exchange' take questions posted on the web and try to find idiots stupid enough to pay for the answers, which were posted freely by others. They are leeches. From foo at coo.com Sat Jul 18 20:08:05 2009 From: foo at coo.com (Dave) Date: Sun, 19 Jul 2009 01:08:05 +0100 Subject: Newbie question - running a command and looking at output In-Reply-To: <4a6261ff@212.67.96.135> References: <4a6261ff@212.67.96.135> Message-ID: <4a6263f3@212.67.96.135> Dave wrote: > I'm trying to run a command (arch -k) and check if the value returned is > 'sun4v' or not. > > > kirkby at t2:[~] $ arch -k > sun4v > > In fact, I want to do 3 three things > > 1) Check if the system is Solaris. > 2) If it is Solaris, check if 'arch -k' prints 'sun4v' > 3) If both 1 and 2 are true, copy a file. > > > Since arch -k is not supported on all systems, the test for Solaris must > be made first. > > I have a test for Solaris (aka SunOS), which copies a file if the > systems is running Solaris: > > import os, shutil > > if os.uname()[0] == 'SunOS': > shutil.copy2('foo.c','bar.c') > > How can I change that, so the copy is only performed if 'arch -k' prints > 'sun4v'? > > > Actually, when I look at oluname, it does print sunnv: >>> import os >>> os.uname() ('SunOS', 't2', '5.10', 'Generic_141414-02', 'sun4v') I guess all I need to do is check for the value of os.uname()[4] too. -- I respectfully request that this message is not archived by companies as unscrupulous as 'Experts Exchange' . In case you are unaware, 'Experts Exchange' take questions posted on the web and try to find idiots stupid enough to pay for the answers, which were posted freely by others. They are leeches. From sjmachin at lexicon.net Sat Jul 18 20:12:32 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 18 Jul 2009 17:12:32 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> Message-ID: <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> On Jul 19, 7:43?am, Irmen de Jong wrote: > twgray wrote: > > I am attempting to send a jpeg image file created on an embedded > > device over a wifi socket to a Python client running on a Linux pc > > (Ubuntu). ?All works well, except I don't know, on the pc client side, > > what the file size is? ? > > You don't. Sockets are just endless streams of bytes. You will have to design some form > of 'wire protocol' that includes the length of the message that is to be read. Apologies in advance for my ignorance -- the last time I dipped my toe in that kind of water, protocols like zmodem and Kermit were all the rage -- but I would have thought there would have been an off-the- shelf library for peer-to-peer file transfer over a socket interface ... not so? From python at mrabarnett.plus.com Sat Jul 18 20:33:32 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 01:33:32 +0100 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> Message-ID: <4A6269DC.1010409@mrabarnett.plus.com> Nobody wrote: > On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote: > >> It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on >> the final packet, which will always be less than MAXPACKETLEN. >> >> I guess my question is, how do I detect end of data on the client side? > > recv() should return zero when the sender closes its end of the connection. > > Is the sender actually closing its end? If you are unsure, use a packet > sniffer such as tcpdump to look for a packet with the FIN flag. > > If you need to keep the connection open for further transfers, you need to > incorporate some mechanism for identifying the end of the data into the > protocol. As others have suggested, prefixing the data by its length is > one option. Another is to use an end-of-data marker, but then you need a > mechanism to "escape" the marker if it occurs in the data. A length prefix > is probably simpler to implement, but has the disadvantage that you can't > start sending the data until you know how long it is going to be. > You could send it in chunks, ending with a chunk length of zero. From twgray2007 at gmail.com Sat Jul 18 22:02:05 2009 From: twgray2007 at gmail.com (twgray) Date: Sat, 18 Jul 2009 19:02:05 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> Message-ID: <2b0e3c3c-2beb-46c3-acf6-86aee8caa2ed@b15g2000yqd.googlegroups.com> On Jul 18, 7:33?pm, MRAB wrote: > Nobody wrote: > > On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote: > > >> It appears to be locking up in ?'data=self.s.recv(MAXPACKETLEN)' on > >> the final packet, which will always be less than MAXPACKETLEN. > > >> I guess my question is, how do I detect end of data on the client side? > > > recv() should return zero when the sender closes its end of the connection. > > > Is the sender actually closing its end? If you are unsure, use a packet > > sniffer such as tcpdump to look for a packet with the FIN flag. > > > If you need to keep the connection open for further transfers, you need to > > incorporate some mechanism for identifying the end of the data into the > > protocol. As others have suggested, prefixing the data by its length is > > one option. Another is to use an end-of-data marker, but then you need a > > mechanism to "escape" the marker if it occurs in the data. A length prefix > > is probably simpler to implement, but has the disadvantage that you can't > > start sending the data until you know how long it is going to be. > > You could send it in chunks, ending with a chunk length of zero. Thanks for the help! From aahz at pythoncraft.com Sat Jul 18 22:46:01 2009 From: aahz at pythoncraft.com (Aahz) Date: 18 Jul 2009 19:46:01 -0700 Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> Message-ID: In article , MRAB wrote: > >If you send the length as 4 bytes then you'll have to decide whether >it's big-endian or little-endian. An alternative is to send the length >as characters, terminated by, say, '\n' or chr(0). Alternatively, make it a fixed-length string of bytes, zero-padded in front. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From piet at cs.uu.nl Sun Jul 19 02:55:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 08:55:20 +0200 Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> Message-ID: >>>>> John Machin (JM) wrote: >JM> On Jul 19, 7:43?am, Irmen de Jong wrote: >>> twgray wrote: >>> > I am attempting to send a jpeg image file created on an embedded >>> > device over a wifi socket to a Python client running on a Linux pc >>> > (Ubuntu). ?All works well, except I don't know, on the pc client side, >>> > what the file size is? ? >>> >>> You don't. Sockets are just endless streams of bytes. You will have to design some form >>> of 'wire protocol' that includes the length of the message that is to be read. >JM> Apologies in advance for my ignorance -- the last time I dipped my toe >JM> in that kind of water, protocols like zmodem and Kermit were all the >JM> rage -- but I would have thought there would have been an off-the- >JM> shelf library for peer-to-peer file transfer over a socket >JM> interface ... not so? Yes, many of them, for example HTTP or FTP. But I suppose they are overkill in this situation. There are also remote procedure call protocols which can do much more, like XMLRPC. By the way if the image file is the only thing you send, the client should close the socket after sending and then the receiver will detect end of file which will be detected by your `if len(data) == 0:' -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jeremy.cowles at gmail.com Sun Jul 19 03:12:47 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Sun, 19 Jul 2009 00:12:47 -0700 Subject: Import hashlib fails, embedded Message-ID: <373cf0740907190012q74da1d22le6b1bd03930acd78@mail.gmail.com> I'm working on an embeddded Python interpreter (using the c-api) where we are loading a custom, zipped copy of the standard Python library (./lib/python25.zip). Everything is working fine, but when I try to "import hashlib", i get the following error: Traceback (most recent call last): File "tryrun.py", line 2, in import hashlib File "~/workspace/pyboinc/lib/python25.zip/hashlib.py", line 133, in File "~/workspace/pyboinc/lib/python25.zip/hashlib.py", line 60, in __get_builtin_constructor ImportError: No module named _md5 I assume this is related to a builtin or dynamically loaded C module, but I'm not sure exactly what the problem is. Can anyone explain to me what is going wrong? Other modules (like sys, os) are working fine, which may be because they are pure-Python modules. Here is the main function: int main(int argc, const char* argv[]) { if (argc < 2) { cerr << "Module name missing\n"; cerr << "usage: " << argv[0] << " module" << endl; return 1; } // get the python library name char* libname = get_lib_name(); // try to copy the library from ./libname to ./lib/libname if (!copy_lib(libname)) { return 1; } Py_SetProgramName((char*)argv[0]); Py_SetPythonHome("./"); Py_Initialize(); if (argc > 0){ PySys_SetArgv(argc-1, (char**)(argv+1)); } { PyObject* syspath = PySys_GetObject("path"); if (!syspath) { cerr << "Couldn't get sys.path" << endl; return 1; } if (!PyList_Check(syspath)) { cerr << "sys.path not a list" << endl; return 1; } PyObject* str = PyString_FromString("."); PyList_Append(syspath, str); Py_DECREF(str); } Py_InitModule("boinc", BoincMethods); PyObject* name = PyString_FromString(argv[1]); PyObject* mod = PyImport_Import(name); Py_DECREF(name); if (!mod) { cerr << "Error loading module" << endl; PyErr_Print(); return 1; } Py_DECREF(mod); Py_Finalize(); } -- Jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Jul 19 03:21:15 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 19 Jul 2009 09:21:15 +0200 Subject: how two join and arrange two files together References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: amrita at iisermohali.ac.in wrote: [please keep the correspondence on the mailing list/newsgroup] > It is working sir, but my datas are on file when i replaced StringIO("") > with open("filename.txt") then it is not printing the result properly, > like in one file i have data like:--- > 33 ALA H = 7.57 N = 121.52 CA = 55.58 HA = 3.89 C = 179.24 > 38 ALA H = 8.29 N = 120.62 CA = 54.33 HA = 4.04 C = 178.95 > 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = 4.05 C = 179.35 > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 > in other:--- > 8 ALA helix (helix_alpha, helix1) > 21 ALA helix (helix_alpha, helix2) > 23 ALA helix (helix_alpha, helix2) > 33 ALA helix (helix_alpha, helix3) > 38 ALA helix (helix_alpha, helix3) > 49 ALA bend > and it is giving the result:- > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18| > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93|23 ALA helix > (helix_alpha, helix2) > 38 ALA H = 8.29 N = 120.62 CA = 54.33 HA = 4.04 C = 178.95|38 ALA helix > (helix_alpha, helix3) > |49 ALA bend > 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39|8 ALA helix > (helix_alpha, helix1) > it is not printing the result for 33 and 21. Hint: you have to adapt the key() function from def key(line): return line[:1] to something that returns the same key for lines in the two files that belong together, and different keys for lines that don't. Peter From vs at it.uu.se Sun Jul 19 04:04:10 2009 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 19 Jul 2009 10:04:10 +0200 Subject: On out-of-date Python Applications Message-ID: <4A62D37A.5080105@it.uu.se> I am not a heavy user of Python; but, I do work with it and some of its application packages (e.g. PyODE), in an academic setting. Many of these applications packages have a Windows installer which usually works fine. However, I also try to keep up with the latest release of Python, and this is where I often have problems. That is, the latest Windows installer provided for some of these applications will not install on the latest version of Python. I do understand that there can be a time lag between the release of Python applications and the latest Python. I also appreciate the work of the people that are responsible for these applications. My question is --- Is there anything I, as a user of an application package that is out-of-date with respect to the latest Python, can do to help in this process of bringing an application up-to-date? --V. Stokes From motoom at xs4all.nl Sun Jul 19 04:38:46 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Sun, 19 Jul 2009 10:38:46 +0200 Subject: On out-of-date Python Applications In-Reply-To: <4A62D37A.5080105@it.uu.se> References: <4A62D37A.5080105@it.uu.se> Message-ID: <4A62DB96.8000302@xs4all.nl> Virgil Stokes wrote: > some of these applications will > not install on the latest version of Python. Which version of Python precisely? -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From mail at microcorp.co.za Sun Jul 19 05:47:07 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 19 Jul 2009 11:47:07 +0200 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> Message-ID: <200907191147.07949.mail@microcorp.co.za> On Sunday 19 July 2009 02:12:32 John Machin wrote: > > Apologies in advance for my ignorance -- the last time I dipped my toe > in that kind of water, protocols like zmodem and Kermit were all the > rage -- but I would have thought there would have been an off-the- > shelf library for peer-to-peer file transfer over a socket > interface ... not so? *Grins at the references to Kermit and zmodem, and remembers Laplink and PC Anywhere* If there is such a transfer beast in Python, I have not found it. (There is an FTP module but that is not quite the same thing) I think it is because the network stuff is all done in the OS or NFS and SAMBA now - with drag and drop support and other nice goodies. I have ended up writing a netstring thingy, that addresses the string transfer problem by having a start sentinel, a four byte ASCII length (so you can see it with a packet sniffer/displayer) and the rest of the data escaped to take out the start sentinel and the escape character. It works, but the four byte ASCII limits the size of what can be sent and received. It guarantees to deliver either the whole string, or fail, or timeout. If anybody is interested I will attach the code here. It is not a big module. This question seems to come up periodically in different guises. To the OP: There are really very few valid ways of solving the string transfer problem, given a featureless stream of bytes like a socket. The first thing that must be addressed is to sync up - you have to somehow find the start of the thing as it comes past. And the second is to find the end of the slug of data that you are transferring. So the simplest way is to designate a byte as a start and end sentinel, and to make sure that such a byte does not occur in the data stream, other than as a start and end marker. This process is called escaping, and the reverse is called unescaping. (SDLC/HDLC does this at a bit pattern level) Another way is to use time, namely to rely on there being some minimum time between slugs of data. This does not work well on TCP/IP sockets, as retries at the lower protocol levels can give you false breaks in the stream. It works well on direct connections like RS-232 or RS-485/422 lines. Classic netstrings send length, then data. They rely on the lower level protocols and the length sent for demarcation of the slug, and work well if you connect, send a slug or two, and disconnect. They are not so hot for long running processes, where processors can drop out while sending - there is no reliable way for a stable receiver to sync up again if it is waiting for a slug that will not finish. Adapting the netstring by adding a sync character and time out is a compromise that I have found works well in practice. - Hendrik From sjmachin at lexicon.net Sun Jul 19 06:17:49 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 19 Jul 2009 03:17:49 -0700 (PDT) Subject: On out-of-date Python Applications References: Message-ID: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> On Jul 19, 6:04?pm, Virgil Stokes wrote: > I am not a heavy user of Python; but, I do work with it and some of its > application packages (e.g. PyODE), in an academic setting. > Many of these applications packages have a Windows installer which > usually works fine. However, I also try to keep up with the latest > release of Python, and this is where I often have problems. That is, the > latest Windows installer provided for some of these applications will > not install on the latest version of Python. If the package was written for 2.x and "the latest version of Python" means 3.X, this is not surprising; your options are (1) wait until the package maintainer releases a 3.x-compatible version (2) port it to 3.x yourself (3) use 2.6 Otherwise: What does "will not install" mean? Are these pure Python packages or do they include C extensions (binary (pyd) or source?)? At what stage of the installation does the installation fail? With what error message(s)? With what versions of Python? > > I do understand that there can be a time lag between the release of > Python applications and the latest Python. I also appreciate the work of > the people that are responsible for these applications. > > My question is --- Is there anything I, as a user of an application > package that is out-of-date with respect to the latest Python, can do to > help in this process of bringing an application ?up-to-date? > > --V. Stokes From r.grimm at science-computing.de Sun Jul 19 07:08:00 2009 From: r.grimm at science-computing.de (Rainer Grimm) Date: Sun, 19 Jul 2009 04:08:00 -0700 (PDT) Subject: invoke method on many instances References: Message-ID: Hallo Alan, > def apply2(itr, methodname, *args, **kwargs): > ? ? f = operator.methodcaller(methodname, *args, **kwargs) > ? ? for item in itr: > ? ? ? ? f(item) you can do it in a functional way. >>> class A(object): ... def hello(self): return "hello: " + str ( self.__class__.__name__ ) ... >>> class B(A):pass ... >>> class C(A):pass ... >>> a=A() >>> b=B() >>> c=C() >>> a.hello() 'hello: A' >>> b.hello() 'hello: B' >>> c.hello() 'hello: C' >>> >>> map( (lambda obj : getattr(obj,"hello")()),(a,b,c)) ['hello: A', 'hello: B', 'hello: C'] >>> [ getattr(obj,"hello")() for obj in (a,b,c)] ['hello: A', 'hello: B', 'hello: C'] Greetings from Rottenburg, Rainer From hubaghdadi at gmail.com Sun Jul 19 07:22:57 2009 From: hubaghdadi at gmail.com (Hussein B) Date: Sun, 19 Jul 2009 04:22:57 -0700 (PDT) Subject: A little help with pexpect Message-ID: Hey, I'm trying to execute a command over a remore server using pexpect +++++++++++++++++ url = 'ssh internalserver' res = pexpect.spawn(url) print '1' res.expect('.*ssword:') print '2' res.sendline('mypasswd') print '3' res.sendline('ls -aslh') +++++++++++++++++ What I want to do is to send a couple of commands and get the response. How to do this? Thanks. From python at bdurham.com Sun Jul 19 09:18:21 2009 From: python at bdurham.com (python at bdurham.com) Date: Sun, 19 Jul 2009 09:18:21 -0400 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <200907191147.07949.mail@microcorp.co.za> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> <200907191147.07949.mail@microcorp.co.za> Message-ID: <1248009501.29158.1325749595@webmail.messagingengine.com> Hi Hendrik, > I have ended up writing a netstring thingy, that addresses the string transfer problem by having a start sentinel, a four byte ASCII length (so you can see it with a packet sniffer/displayer) and the rest of the data escaped to take out the start sentinel and the escape character. It works, but the four byte ASCII limits the size of what can be sent and received. It guarantees to deliver either the whole string, or fail, or timeout. > If anybody is interested I will attach the code here. It is not a big module. I am interested in seeing your code and would be grateful if you shared it with this list. Thank you, Malcolm From piet at cs.uu.nl Sun Jul 19 09:24:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 15:24:26 +0200 Subject: invoke method on many instances References: Message-ID: >>>>> Rainer Grimm (RG) a ?crit: >RG> Hallo Alan, >>> def apply2(itr, methodname, *args, **kwargs): >>> ? ? f = operator.methodcaller(methodname, *args, **kwargs) >>> ? ? for item in itr: >>> ? ? ? ? f(item) >RG> you can do it in a functional way. >>>>> class A(object): >RG> ... def hello(self): return "hello: " + str >RG> ( self.__class__.__name__ ) >RG> ... >>>>> class B(A):pass >RG> ... >>>>> class C(A):pass >RG> ... >>>>> a=A() >>>>> b=B() >>>>> c=C() >>>>> a.hello() >RG> 'hello: A' >>>>> b.hello() >RG> 'hello: B' >>>>> c.hello() >RG> 'hello: C' >>>>> >>>>> map( (lambda obj : getattr(obj,"hello")()),(a,b,c)) >RG> ['hello: A', 'hello: B', 'hello: C'] >>>>> [ getattr(obj,"hello")() for obj in (a,b,c)] >RG> ['hello: A', 'hello: B', 'hello: C'] But that creates an unnecessary list. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From victorsubervi at gmail.com Sun Jul 19 09:24:43 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 19 Jul 2009 15:24:43 +0200 Subject: Auto Send URL In-Reply-To: <50697b2c0907181032x6f0029dfs304a345ca2835690@mail.gmail.com> References: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> <50697b2c0907181032x6f0029dfs304a345ca2835690@mail.gmail.com> Message-ID: <4dc0cfea0907190624g691688cdlef20acd77b88746b@mail.gmail.com> Ah. How easy! Thank you. On Sat, Jul 18, 2009 at 7:32 PM, Chris Rebert wrote: > On Fri, Jul 17, 2009 at 6:02 AM, Victor Subervi > wrote: > > Hi; > > I am trying to script code that automatically sends a Web site visitor to > an > > URL. Specifically, when they enter a value in a search box, I have that > form > > sent to a script that writes an URL acceptable to Google, then I want to > > send the visitor off without him having to click another button. How do I > do > > this? > > Send back a webpage with a Refresh meta tag: > > > > > > > > Cheers, > Chris > -- > http://blog.rebertia.com > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.isaac at gmail.com Sun Jul 19 09:48:16 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sun, 19 Jul 2009 13:48:16 GMT Subject: are user defined classes hashable? Message-ID: Are user defined classes hashable? (The classes; *not* the instances!) I want to use some classes as dictionary keys. Python is not objecting, but I'm not sure how to think about whether this could be dangerous. I'm inclined to guess it will be hashed by id and this is OK. Thanks for any insights, Alan Isaac From Nicolas.Dandrimont at crans.org Sun Jul 19 10:07:57 2009 From: Nicolas.Dandrimont at crans.org (Nicolas Dandrimont) Date: Sun, 19 Jul 2009 10:07:57 -0400 Subject: are user defined classes hashable? In-Reply-To: References: Message-ID: <20090719140757.GB11029@dirichlet.crans.org> * Alan G Isaac [2009-07-19 13:48:16 +0000]: > Are user defined classes hashable? > (The classes; *not* the instances!) > > I want to use some classes as dictionary keys. > Python is not objecting, > but I'm not sure how to think about > whether this could be dangerous. > I'm inclined to guess it will be hashed by id > and this is OK. You can check for yourself: In [1]: class Foo(object): ...: pass ...: In [2]: foo = Foo() In [3]: hash hash In [3]: hash(foo) Out[3]: 15294992 In [4]: id(foo) Out[4]: 15294992 So yes, by default, user-defined classes are hashable, by id. You can override this behaviour by defining the __hash__ special method on your object. HTH, -- Nicolas Dandrimont -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From mark.dufour at gmail.com Sun Jul 19 10:30:57 2009 From: mark.dufour at gmail.com (Mark Dufour) Date: Sun, 19 Jul 2009 16:30:57 +0200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler Message-ID: <8180ef690907190730t50ccc2f4sb7f489e7c9bb9fb8@mail.gmail.com> Hi all, I have just released version 0.2 of Shed Skin, an experimental (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). It comes with 7 new example programs (for a total of 40 example programs, at over 12,000 lines) and several important improvements/bug fixes. See http://code.google.com/p/shedskin/wiki/ReleaseNotes for the full changelog. The new example programs consist of Disco, an elegant go player (see http://shed-skin.blogspot.com/2009/07/disco-elegant-python-go-player.html), a larger Voronoi implementation at 800 lines, a TSP algorithm simulating ant colonies, a nicer neural network algorithm and three compressors (Lempel-Ziv, huffman block, and arithmetic). Other than bug fixes for these programs, this release adds some important optimizations. First and foremost, inlining was greatly improved, resulting in potential speedups across the board. Second, loops such as 'for a, b in enumerate/zip(sequence[, sequence])' should now be dramatically faster (also inside list comprehensions), by avoiding allocation of intermediate tuples. Finally, basic list slicing should now be much faster. Please try it out! Mark Dufour. -- "One of my most productive days was throwing away 1000 lines of code" - Ken Thompson From alan.isaac at gmail.com Sun Jul 19 10:46:12 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sun, 19 Jul 2009 14:46:12 GMT Subject: are user defined classes hashable? In-Reply-To: References: Message-ID: > * Alan G Isaac [2009-07-19 13:48:16 +0000]: >> Are user defined classes hashable? >> (The classes; *not* the instances!) >> I'm inclined to guess it will be hashed by id and this is >> OK. On 7/19/2009 10:07 AM Nicolas Dandrimont apparently wrote: > You can check for yourself: > In [1]: class Foo(object): > ...: pass > ...: > In [2]: foo = Foo() > In [3]: hash(foo) > Out[3]: 15294992 > In [4]: id(foo) > Out[4]: 15294992 Again, my question is about the class not its instances, but still, checking as you suggest gives the same answer. Thanks, Alan From piet at cs.uu.nl Sun Jul 19 10:49:09 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 16:49:09 +0200 Subject: A little help with pexpect References: Message-ID: >>>>> Hussein B (HB) wrote: >HB> Hey, >HB> I'm trying to execute a command over a remore server using pexpect >HB> +++++++++++++++++ >HB> url = 'ssh internalserver' >HB> res = pexpect.spawn(url) >HB> print '1' >HB> res.expect('.*ssword:') >HB> print '2' >HB> res.sendline('mypasswd') >HB> print '3' >HB> res.sendline('ls -aslh') >HB> +++++++++++++++++ >HB> What I want to do is to send a couple of commands and get the >HB> response. >HB> How to do this? >HB> Thanks. You can read the output with res.readline() in a loop or similar. The problem is when to stop. You could look in the returned string for the prompt and hope the prompt doesn't accidentally occur in the output. You can also do res.expect(prompt) and get res.before() Same problem with accidentally occurrence of the prompt. Another way is to use a timeout, e.g. with read_nonblocking or specifying a timeout at the spawn call. You can also consider using paramiko instead of pexpect. It gives you considerably more control. For each command you van open a `channel' and that acts more or less similar to a socket, i.e. you get a decent EOF at the end of the command's output. This implies that you have to create a new channel for each command, but it stays within the same SSH connection (SSH allows subconnections). Example: >>> import paramiko ...Mumbles about deprecated modules... >>> hostname='server.example.net' >>> port = 22 >>> t = paramiko.Transport((hostname, port)) >>> username = 'user' >>> password = '********' >>> t.connect(username=username, password=password) Open a channel for a command >>> chan = t.open_session() >>> chan.exec_command('ls -l') >>> chan.recv(999999) 'total 0\ndrwxr-xr-x 2 user group 60 Apr 2 2009 Mail\ndrwx------ 2 user group 6 Dec 27 2008 tmp\n' >>> chan.recv(999999) '' That was end of file. Open a new channel for a new command >>> chan = t.open_session() >>> chan.exec_command('cat') >>> chan.send('abcdefghijklmn\n') 15 >>> chan.recv(99) 'abcdefghijklmn\n' >>> -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From resurtm at gmail.com Sun Jul 19 11:03:21 2009 From: resurtm at gmail.com (resurtm) Date: Sun, 19 Jul 2009 08:03:21 -0700 (PDT) Subject: python, ctypes, callbacks -- access violation when calling callback Message-ID: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> Hello. I'm trying to pass to the C function pointer to callback function from python. But when i'm trying to do this i get access violation within the DLL file when calling python callback. Here is the python side code: from ctypes import * # ... class NewtonBody(Structure): def __init__(self, pointer = 0): self.pointer = pointer # ... class Newton: def __init__(self): self._cdll = CDLL('newton.dll') self.world = NewtonWorld() # ... # NewtonBodySetForceAndTorqueCallback def bodySetForceAndTorqueCallback(self, body): CALLBACK = CFUNCTYPE(c_int, POINTER(NewtonBody), c_float, c_int) def callback(a, b, c): print '1' return 0 self._cdll.NewtonBodySetForceAndTorqueCallback(body.pointer, CALLBACK(callback)) return None Traceback: Traceback (most recent call last): File "Newton.py", line 119, in newton.update(10.5) File "Newton.py", line 42, in update self._cdll.NewtonUpdate(self.world.pointer, c_float(timestep)) WindowsError: exception: access violation reading 0x3C888899 And finally prototype of function which i'm trying to call and callback function type declarations: typedef void (*NewtonApplyForceAndTorque) (const NewtonBody* body, dFloat timestep, int threadIndex); // ... NEWTON_API void NewtonBodySetForceAndTorqueCallback (const NewtonBody* body, NewtonApplyForceAndTorque callback); Can anybody explain my errors when trying to pass callback to DLL function? Thanks for advices and solutions! From lists at cheimes.de Sun Jul 19 11:09:37 2009 From: lists at cheimes.de (Christian Heimes) Date: Sun, 19 Jul 2009 17:09:37 +0200 Subject: python, ctypes, callbacks -- access violation when calling callback In-Reply-To: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> References: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> Message-ID: resurtm wrote: > Can anybody explain my errors when trying to pass callback to DLL > function? > > Thanks for advices and solutions! You have to keep a reference to the callback alive yourself. ctypes doesn't increase the refernece counter of the function when you define a callback. As soon as the ref counter reaches 0, the function object is collected and the pointer to the callback is invalid. You could assign it to a module global or instance variable. Christian From resurtm at gmail.com Sun Jul 19 11:15:33 2009 From: resurtm at gmail.com (resurtm) Date: Sun, 19 Jul 2009 08:15:33 -0700 (PDT) Subject: python, ctypes, callbacks -- access violation when calling callback References: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> Message-ID: <8df80a1c-ada2-47a8-86ba-a9ed804c57f6@q11g2000yqi.googlegroups.com> On 19 ???, 21:09, Christian Heimes wrote: > resurtm wrote: > > Can anybody explain my errors when trying to pass callback to DLL > > function? > > > Thanks for advices and solutions! > > You have to keep a reference to the callback alive yourself. ctypes > doesn't increase the refernece counter of the function when you define a > callback. As soon as the ref counter reaches 0, the function object is > collected and the pointer to the callback is invalid. > > You could assign it to a module global or instance variable. > > Christian Thanks for help Christian! It's working fine now! ;-) From forman.simon at gmail.com Sun Jul 19 11:21:01 2009 From: forman.simon at gmail.com (Calroc) Date: Sun, 19 Jul 2009 08:21:01 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> On Jul 9, 1:20 pm, Steven D'Aprano wrote: [...] > You'll excuse my skepticism about all these claims about how anyone can > program, how easy it is to teach the fundamentals of Turing Machines and > functional programming to anybody at all. Prove it. Where are your peer- > reviewed studies demonstrating such successes on randomly chosen people, > not self-selected motivated, higher-than-average intelligence students? I'm engaged presently in starting a school to teach programming from the ground up, based roughly on the curriculum outlined in the article I mentioned. I can't randomly select students but I do intend to gather data about who does how well from what starting conditions. I'm excited about formal methods because one, I just heard about them, and two, they provide me with a much more well-fleshed-out way to bridge rigorously from "raw" Turing machine models to higher order abstractions. I'm confident that anyone who can play Sudoku can learn programming and formal methods. > In the real world, human beings are prone to serious cognitive biases and > errors. Our brains are buggy. Take any list of reasoning fallacies, and > you'll find the majority of people have difficulty avoid them. The first > struggle is to get them to even accept that they *are* fallacies, but > even once they have intellectually accepted it, getting them to live up > to that knowledge in practice is much, much harder. > > In fact I'd go further and say that *every single person*, no matter how > intelligent and well-educated, will fall for cognitive biases and > fallacious reasoning on a regular basis. I agree completely. One of my chief goals is to "debug" human reasoning to whatever extent possible. This is precisely where I expect training in computer programming to come in handy. The machine symbol manipulator is not going to be fooled by reasoning fallacies. Fluency in rigorous symbol manipulation can only help us cope with the vagaries of our too-often sub-rational wetware. I think the real win starts when computers can be used as a communication medium, not for blogs and tweets and SMS txt and IM and VoIP, but for reasoned symbolically coded discussion. On Jul 9, 2:10 pm, Steven D'Aprano wrote: > On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > >> persistent idea "out there" that programming is a very accessible > >> skill, like cooking or gardening, anyone can do it, and even profit > >> from it, monetarily or otherwise, etc., and to some extent I am > > > Programming is not like any other human activity. > > In practice? In principle? Programming in principle is not the same as it > is performed in practice. > > But in either case, programming requires both the logical reasoning of > mathematics and the creativity of the arts. Funnily enough, > mathematicians will tell you that mathematics requires the same, and so > will the best artists. I think mathematicians, engineers, artists, even > great chefs, will pour scorn on your claim that programming is not like > any other human activity. Well it's actually Dijkstra's claim, and I find it reasonable, that programming has two novel or unique aspects not present in other areas of human endeavor: vast scale and digital mechanism. http://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1036.html > [...] > > > He talks about how "when all is said and done, the only thing computers > > can do for us is to manipulate symbols and produce results of such > > manipulations" and he emphasises the "uninterpreted" nature of > > mechanical symbol manipulation, i.e. that the machine is doing it > > mindlessly. > > "Manipulate symbols" is so abstract as to be pointless. By that That's the "koan form", longhand he means the Boolean domain {true, false}, the logical operations and their algebraic relationships, and the means of using same to deduce new formulae. > reasoning, I can build a "computer" consisting of a box open at the top. > I represent a symbol by an object (say, a helium-filled balloon, or a > stone), instead of a pattern of bits. I manipulate the symbol by holding > the object over the box and letting go. If it flies up into the sky, that > represents the symbol "Love is War", if it falls into the box, it > represents the symbol "Strength is Blue", and if it just floats there, it > represents "Cheddar Cheese". This is a deterministic, analog computer > which manipulates symbols. Great. > > And utterly, utterly useless. So what is my computer lacking that real > computers have? When you have answered that question, you'll see why > Dijkstra's claim is under-specified. Well, you said it yourself: your computer is lacking utility. [...] > > What is an uninterpreted formula? If you don't interpret it, how can you > distinguish it from random noise? Uninterpreted formula means that you manipulate the symbols without thinking about what they mean. You just use the rules on the formulae as such. To be useful, once you're "done" with them you can consider their meaning. You can distinguish it at any time, but Dijktra's saying that we should pay attention to the [mathematical, logical] reasoning process as it's own thing, without reference to the meaning of the symbols, at least temporarily. [...] > > > If you're never exposed to that constellation of concepts that underpins > > "mechanical symbol manipulation" you are adrift in a sea ("c", ha ha) of > > abstractions. > > > However, if you /are/ exposed to the "so few and simple" rules of > > manipulation the gates (no pun intended) to the kingdom are thrown wide. > > What nonsense. The keys to the kingdom are the abstractions. > > Here's an exercise for you, if you dare. It's not that difficult to > remove the abstraction from integer addition, to explain it in terms of > bit manipulation. If you prefer, you can write a Turing Machine instead. > > Now try doing the same thing for Quicksort. Don't worry, we'll wait. > > Once you've done that, let's see you implement a practical, scalable, non- > toy webserver as a Turing Machine. > > No, it's not the fundamental operations that open the doors to the > kingdom. It's the abstractions. The history of computing is to gain more > and more power by leveraging better and better abstractions. I agree that "better and better abstractions" are the key(s), but I strongly disagree that the fundamental operations are not, uh, key to those keys. There are two intertwined phenomenon: 1) formulae, written in symbols 2) deriving new formulae by applying [in]formal methods to existing formulae The rules, and the application of those rules, are two separate but compatible phenomenon. (Compatible in that you can encode the latter in terms of the former. This is the essential "trick" of mechanized thought.) Now (1) is constantly being elaborated, these elaborations are the "higher" abstractions we're talking about. But (2) stays the same(-ish) no matter how high you go, and (2) is the process by which all the higher (1)'s come from, so if we get it right (and by right I mean of course by using rigorous proofs, formal methods et. al.) from the get go and keep it right, we're golden. I do literally intend to teach e.g. "integer addition ... in terms of bit manipulation" and a bit of parsing too. But I intend to rapidly bring students to a Forth or Forth-like stage, and then immediately create simple parsers that implement, for example, C for loops, and other "higher" abstractions. Highly abridged, this is the curriculum: Enough about bits to bring them to bytes, enough bytes to bring them to RAM, enough RAM to implement a crude CPU, enough CPU to implement a bit of Forth, enough Forth to implement parsers, and enough parsing (and compilation and interpreting, etc.) to let them grok languages in general. Formal methods gives me a wide straight road from each stage to the next. [...] > > Quoting Dijkstra again [1]: "Before we part, I would like to invite you > > to consider the following way of doing justice to computing's radical > > novelty in an introductory programming course. > > > "On the one hand, we teach what looks like the predicate calculus, but > > we do it very differently from the philosophers. In order to train the > > novice programmer in the manipulation of uninterpreted formulae, we > > teach it more as boolean algebra, familiarizing the student with all > > algebraic properties of the logical connectives. To further sever the > > links to intuition, we rename the values {true, false} of the boolean > > domain as {black, white}. > > This is supposed to be a good thing? > > Oh my. It must be nice in that ivory tower, never having to deal with > anyone who hasn't already been winnowed by years of study and self- > selection before taking on a comp sci course. > > I don't think Dijkstra would have taken that suggestion seriously if he > had to teach school kids instead of college adults. I'm not going to teach it exactly like he outlines there, but I really am going to try the curriculum I outlined. I'll post videos. :] > ... > > > (Did you read that last paragraph and think, "Well how the heck else are > > you supposed to program a computer if not in a computer language?"? If > > so, well, that is kind of my point.) > > No. What I thought was, how the hell are you supposed to manipulate > symbols without a language to describe what sort of manipulations you can > do? Direct-manipulation graphical Abstract Syntax Trees. [...] > > tables and arrays. He didn't even know that they were different > > "things". > > I shall manfully resist the urge to make sarcastic comments about > ignorant PHP developers, and instead refer you to my earlier reply to you > about cognitive biases. For extra points, can you identify all the > fallacious reasoning in that paragraph of yours? I'll pass. I should have resisted the urge to whinge about him in the first place. > > But those abstractions just get in the way, according to you. He should > be programming in the bare metal, doing pure symbol manipulation without > language. No, the abstractions are useful, but their utility is tied to their reliability, which in turn derives from the formal rigour of the proofs of the abstractions' correctness. My issues with modern art are, that too many of these abstractions are not /proven/, and they are both frozen and fractured by the existing milieu of "static" mutually incompatible programming languages. I see a possibility for something better. > If programming is symbol manipulation, then you should remember that the > user interface is also symbol manipulation, and it is a MUCH harder > problem than databases, sorting, searching, and all the other problems > you learn about in academia. The user interface has to communicate over a > rich but noisy channel using multiple under-specified protocols to a > couple of pounds of meat which processes information using buggy > heuristics evolved over millions of years to find the ripe fruit, avoid > being eaten, and have sex. If you think getting XML was hard, that's > *nothing* compared to user interfaces. > > The fact that even bad UIs work at all is a credit to the heuristics, > bugs and all, in the meat. Ok, yes, but have you read Jef Raskin's "the Humane Interface"? > > As for "a minimum of bugs"... sigh. The "minimum of bugs" is zero, if > > you derive your "uninterpreted formulae" /correctly/. Sorry. I can't believe that I was so melodramatic. > And the secret of getting rich on the stock market is to buy low, sell > high. Very true, and very pointless. How do you derive the formula > correctly? > > Do you have an algorithm to do so? If so, how do you know that algorithm > is correct? Well we know logic works, we can trust that. The algorithm of proving the formula is logic. > > Deriving provably > > correct "programs" should be what computer science and computer > > education are all about > > That's impossible. Not just impractical, or hard, or subject to hardware > failures, but impossible. > > I really shouldn't need to raise this to anyone with pretensions of being > a Computer Scientist and not just a programmer, but... try proving an > arbitrary program will *halt*. > > If you can't do that, then what makes you think you can prove all useful, > necessary programs are correct? It may well be that there are useful, necessary programs that are impossible to prove correct. I'm not trying to claim there aren't such. I think that wherever that's not the case, wherever useful necessary programs can be proven, they should be. It may be that flawless software is an unreachable asymptote, like the speed of light for matter, but I'm (recently!) convinced that it's a goal worthy of exploration and effort. Just because it's unattainable doesn't make it undesirable. And who knows? Maybe the horse will learn to sing. > > Again with Dijkstra[3]: "The prime paradigma of the pragmatic designer > > is known as "poor man's induction", i.e. he believes in his design as > > long as "it works", i.e. until faced with evidence to the contrary. (He > > will then "fix the design".) > > Yes, that is the only way it can be. > > > The scientific designer, however, believes > > in his design because he understands why it will work under all > > circumstances. > > Really? Under *all* circumstances? > > Memory failures? Stray cosmic radiation flipping bits? Noise in the power > supply? > > What's that you say? "That's cheating, the software designer can't be > expected to deal with the actual reality of computers!!! We work in an > abstract realm where computers never run out of memory, swapping never > occurs, and the floating point unit is always correct!" First, yes, that could be considered cheating. We don't expect automotive engineers to design for resistance to meteor strikes. Second, those issues plague all programs, formally derived or not. Formal methods certainly can't hurt. Third, in cases where you do want reliability in the face of things like cosmic rays you're going to want to use formal methods to achieve it. And last but not least, it may be possible to "parameterize" the abstractions we derive with information about their susceptibility to stability of their underlying abstractions. > Fail enough. I don't want to make it too hard for you. > > Okay, so let's compare the mere "pragmatic designer" with his provisional > expectation that his code is correct, and Dijkstra's "scientific > designer". He understands his code is correct. Wonderful! > > Er, *how exactly* does he reach that understanding? > > He reasons about the logic of the program. Great! I can do that. See, > here's a proof that binary search is correct. How easy this is. > > Now try it for some non-trivial piece of code. Say, the Linux kernel. > This may take a while... Well, I'd start with Minix 3... But really the method would be to build up provably correct compounds out of proven sub-assemblies. Even if it takes a long time is it time wasted? A provably correct OS is kind of a neat idea IMO. > > What guarantee do we have that the scientific designer's reasoning was > correct? I mean, sure he is convinced he has reasoned correctly, but he > would, wouldn't he? If he was unsure, he'd keep reasoning (or ask for > help), until he was sure. But being sure doesn't make him correct. It > just makes him sure. > > So how do you verify the verification? Peer review and run it through a mechanical verifier. There is a self- reflexive loop there but it's not infinite. If it was we couldn't implement any computer. > And speaking of binary search: [Bentley's binary search bug] This is a perfect example of leaky abstraction. The C "integer" doesn't behave like a "real" integer but part of the algorithm's implementation relied on the tacit overlap between C ints and integers up to a certain value. This is the sort of thing that could be annotated onto the symbolic representation of the algorithm and used to compile provably correct machine code (for various platforms.) If the same proven algorithm had been implemented on top of a correct integer abstraction (like python's long int) it wouldn't have had that bug. (And yes, there would still be limits, truly vast arrays could conceivably result in memory exhaustion during execution of the sort. I think the answer there lies in being able to represent or calculate the limits of an algorithm explicitly, the better to use it correctly in other code.) > Perhaps the "scientific designer" should be a little less arrogant, and > take note of the lesson of real science: all knowledge is provisional and > subject to correction and falsification. (I apologize for my arrogance on behalf of formal methods, I just found out about them and I'm all giddy and irritating.) > Which really makes the "scientific designer" just a slightly more clever > and effective "pragmatic designer", doesn't it? Um, er, well, scientists are ultimate pragmatists, and Dijkstra's description of "pragmatic designer" above could well be applied to "scientific designers". The difference turns on /why/ each person has confidence is his design: one can prove mathematically that his design is correct while the other is ... sure. Whether or not the delivered software is actually flawless, the difference seems to me more than just "slightly more clever and effective". > > > The transition from pragmatic to scientific design would > > indeed be a drastic change within the computer industry." > > Given Dijkstra's definition of "scientific design", it certainly would. > It would be as drastic a change as the discovery of Immovable Objects and > Irresistible Forces would be to engineering, or the invention of a > Universal Solvent for chemistry, or any other impossibility. > > > "Obviously no errors" is the goal to strive for, and I am comfortable > > calling anyone an amateur who prefers "no obvious errors." > > It's not a matter of preferring no obvious errors, as understanding the > limitations of reality. You simply can't do better than no obvious errors > (although of course you can do worse) -- the only question is what you > call "obvious". It may be expensive, it may be difficult, and it may certainly be impossible, but I feel the shift from ad hoc software creation to rigorous scientific software production is obvious. To me it's like the shift from astrology to astronomy, or from alchemy to chemistry. ~Simon From victorsubervi at gmail.com Sun Jul 19 11:35:58 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 19 Jul 2009 17:35:58 +0200 Subject: Getting a Form To Work Message-ID: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> Hi; I have the following in a *.py page for the Web: from primeNumbers import primeNumbers try: num = form.getfirst('num') except: num = '' msg = "Oops" print "Content-Type: text/html" print print """ """ if num != '': try: num = round(float(num)) roots = primeNumbers(num) print roots except: msg += "We're sorry! You entered something that is not a number. Please try again.
\n" num = '' if num == '': print msg print "Num:", num print """
""" print '\n' Now, "Oops" never gets printed, nor does the variable num (although a blank space appears to print). I am calling the very page to which the form sends its values. Assuming the imported module works, why don?t I get anything printed? TIA, Victor -------------- next part -------------- An HTML attachment was scrubbed... URL: From Nicolas.Dandrimont at crans.org Sun Jul 19 11:39:31 2009 From: Nicolas.Dandrimont at crans.org (Nicolas Dandrimont) Date: Sun, 19 Jul 2009 11:39:31 -0400 Subject: are user defined classes hashable? In-Reply-To: References: Message-ID: <20090719153931.GA14960@dirichlet.crans.org> * Alan G Isaac [2009-07-19 14:46:12 +0000]: > Again, my question is about the class not its instances, > but still, checking as you suggest gives the same answer. That's what I get for answering before my coffee! Cheers, -- Nicolas Dandrimont "Linux poses a real challenge for those with a taste for late-night hacking (and/or conversations with God)." (By Matt Welsh) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From sajmikins at gmail.com Sun Jul 19 11:45:20 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sun, 19 Jul 2009 08:45:20 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: On Jul 14, 1:10?pm, Duncan Booth wrote: > John Machin wrote: > > Try an iterative version of checking that () [] and {} > > are balanced and nested appropriately. > > Here's how I might approach the more general case: > > def balanced(s, parens=("()",)): > ? ? ''' > ? ? Example: > ? ? >>> balanced('aAAA(b[bb(c]c))') > ? ? True > ? ? >>> balanced('aAAA(b[bb(c]c))', parens=("()", "[]")) > ? ? False > ? ? ''' > ? ? s = re.sub("[^%s]+" % re.escape("".join(parens)), "", s) > ? ? for i in range(len(s)/2): > ? ? ? ? for p in parens: > ? ? ? ? ? ? s = s.replace(p, "") > ? ? return not s > > For short strings this is obviously slower than your 'iterative' function, > but the run time mostly depends on the number of pairs of parentheses > rather than the total length of the string, so for example it starts > winning on a string with 2 pairs of parentheses about 75 characters long. def balanced(s, pairs=('()', '[]', '{}')): opening = {} closing = {} for open_, close in pairs: opening[open_] = closing[close] = object() stack = [] for char in s: marker = opening.get(char) if marker: stack.append(marker) continue marker = closing.get(char) if marker: try: if stack.pop() is not marker: return False except IndexError: return False # All markers should be popped. return not stack Can parse sequences other than strings. :] From sjmachin at lexicon.net Sun Jul 19 11:47:17 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 01:47:17 +1000 Subject: On out-of-date Python Applications In-Reply-To: <4A632C9D.2010303@it.uu.se> References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> Message-ID: <4A634005.6080505@lexicon.net> On 20/07/2009 12:24 AM, Virgil Stokes wrote: > John Machin wrote: >> On Jul 19, 6:04 pm, Virgil Stokes wrote: >> >>> I am not a heavy user of Python; but, I do work with it and some of its >>> application packages (e.g. PyODE), in an academic setting. >>> Many of these applications packages have a Windows installer which >>> usually works fine. However, I also try to keep up with the latest >>> release of Python, and this is where I often have problems. That is, the >>> latest Windows installer provided for some of these applications will >>> not install on the latest version of Python. >>> >> >> If the package was written for 2.x and "the latest version of Python" >> means 3.X, this is not surprising; your options are (1) wait until the >> package maintainer releases a 3.x-compatible version (2) port it to >> 3.x yourself (3) use 2.6 >> > Actually John, > My question was about the more general case --- how to help with > upgrading applications; but, I will try to address your email. >> Otherwise: What does "will not install" mean? > This means that when the Windows installer is executed it informs you > that you do not have a match between the application and the Python that > it finds on your Windows PC. You can easily find this out for yourself > by trying to install PyODE on a Windows platform with a Python version > later than 2.5.x. >> Are these pure Python >> packages or do they include C extensions (binary (pyd) or source?)? > Obviously, this depends on the application. In the particular case that > I mentioned (PyODE) I believe that there is a mix of C binaries and > Python code. >> At >> what stage of the installation does the installation fail? > In the initial stages. >> With what >> error message(s)? > In one specific case, the message is (paraphrasing) "You do not have > Python 2.5 installed" --- the installation is then aborted. >> With what versions of Python? >> > I have only Python 2.6.2 on my home Windows Vista PC and PyODE does not > have a Windows Installer for Python 2.6. >> >> >> >>> I do understand that there can be a time lag between the release of >>> Python applications and the latest Python. I also appreciate the work of >>> the people that are responsible for these applications. >>> >>> My question is --- Is there anything I, as a user of an application >>> package that is out-of-date with respect to the latest Python, can do to >>> help in this process of bringing an application up-to-date? >>> >>> --V. Stokes >>> >> >> > Note, again John, my question was about how I might assist (help) with > the updating of applications; Your answers have been very helpful in determining exactly what your problem is; thank you. > but, thank you for your interest in this. > Perhaps, only the owner/responsible programmer for the application can > create a Windows installer, Given a source distribution, a suitable C++ compiler, and the suspension of Murphy's Law, anyone should be able to (a) install it on their own machine (b) make a Windows installer. > or perhaps others could assist with this --- Perhaps indeed ... > this is what my question was about. I would be glad to assist if > possible :-) Suggestions: (1) contact the pyODE maintainer and ask, or check out the project's forums on sourceforge (2) if you have a C++ compiler on your Windows box: get the source distribution, unpack it, ensure that the ODE_BASE in the setup.py points to your (existing) ODE 0.7 installation, do \python26\python setup.py install and stand well back. I have just tried this and got a ton of warnings from the compile and 3 errors from the link: build\lib.win32-2.6\ode.pyd : fatal error LNK1120: 3 unresolved externals error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"' failed with exit status 1120 C++ is not my bag and it's way past bedtime here, so I'll just paste in the link output in the hope that someone can spot the problem: C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\ode\ode-0.7\lib\releaselib /LIBPATH:C:\python26\libs /LIBPATH:C:\python26\PCbuild ode.lib user32.lib /EXPORT:initode build\temp.win32-2.6\Release\ode_trimesh.obj /OUT:build\lib.win32-2.6\ode.pyd /IMPLIB:build\temp.win32-2.6\Release\ode.lib /MANIFESTFILE:build\temp.win32-2.6\Release\ode.pyd.manifest /NODEFAULTLIB:LIBCMT Creating library build\temp.win32-2.6\Release\ode.lib and object build\temp.win32-2.6\Release\ode.exp ode.lib(error.obj) : error LNK2019: unresolved external symbol __iob referenced in function "void __cdecl printMessage(int,char const *,char const *,char *)" (?printMessage@@YAXHPBD0PAD at Z) ode.lib(convex.obj) : error LNK2001: unresolved external symbol __iob ode.lib(convex.obj) : error LNK2019: unresolved external symbol "public: void __thiscall std::_String_base::_Xran(void)const " (?_Xran at _String_base@std@@QBEXXZ) referenced in function "public: class std::basic_string,class std::allocator > & __thiscall std::basic_string,class std::allocator >::erase(unsigned int,unsigned int)" (?erase@?$basic_string at DU?$char_traits at D@std@@V?$allocator at D@2@@std@@QAEAAV12 at II@Z) ode.lib(convex.obj) : error LNK2019: unresolved external symbol "public: void __thiscall std::_String_base::_Xlen(void)const " (?_Xlen at _String_base@std@@QBEXXZ) referenced in function "protected: bool __thiscall std::basic_string,class std::allocator >::_Grow(unsigned int,bool)" (?_Grow@?$basic_string at DU?$char_traits at D@std@@V?$allocator at D@2@@std@@IAE_NI_N at Z) build\lib.win32-2.6\ode.pyd : fatal error LNK1120: 3 unresolved externals Cheers, John From python at mrabarnett.plus.com Sun Jul 19 11:52:45 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 16:52:45 +0100 Subject: Getting a Form To Work In-Reply-To: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> References: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> Message-ID: <4A63414D.20408@mrabarnett.plus.com> Victor Subervi wrote: > Hi; > I have the following in a *.py page for the Web: > > from primeNumbers import primeNumbers > > try: > num = form.getfirst('num') > except: > num = '' > msg = "Oops" > > print "Content-Type: text/html" > print > print """ > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> > > > """ > if num != '': > try: > num = round(float(num)) > roots = primeNumbers(num) > print roots > except: > msg += "We're sorry! You entered something that is not a number. > Please try again.
\n" > num = '' > if num == '': > print msg > print "Num:", num You get here if num == '', so you're printing out an empty string! BTW, try not to use bare excepts. > print """ >
> > name="search" id="search" /> >
> """ > print '\n' > > Now, "Oops" never gets printed, nor does the variable num (although a > blank space appears to print). I am calling the very page to which the > form sends its values. Assuming the imported module works, why don?t I > get anything printed? > From hendrik at microcorp.co.za Sun Jul 19 12:07:54 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 19 Jul 2009 18:07:54 +0200 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <1248009501.29158.1325749595@webmail.messagingengine.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <200907191147.07949.mail@microcorp.co.za> <1248009501.29158.1325749595@webmail.messagingengine.com> Message-ID: <200907191807.55071.hendrik@microcorp.co.za> On Sunday 19 July 2009 15:18:21 python at bdurham.com wrote: > Hi Hendrik, > > If anybody is interested I will attach the code here. It is not a big > > module. > > I am interested in seeing your code and would be grateful if you shared > it with this list. All right here it is. Hope it helps - Hendrik -------------- next part -------------- A non-text attachment was scrubbed... Name: netstring.py Type: application/x-python Size: 3474 bytes Desc: not available URL: From python at bdurham.com Sun Jul 19 12:09:12 2009 From: python at bdurham.com (python at bdurham.com) Date: Sun, 19 Jul 2009 12:09:12 -0400 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <200907191807.55071.hendrik@microcorp.co.za> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <200907191147.07949.mail@microcorp.co.za> <1248009501.29158.1325749595@webmail.messagingengine.com> <200907191807.55071.hendrik@microcorp.co.za> Message-ID: <1248019752.28310.1325763043@webmail.messagingengine.com> >> I am interested in seeing your code and would be grateful if you shared it with this list. > All right here it is. Hope it helps. Hendrik, Thank you very much!! (I'm not the OP, but found this thread interesting) Best regards, Malcolm From piet at cs.uu.nl Sun Jul 19 13:08:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 19:08:58 +0200 Subject: A little help with pexpect References: Message-ID: >>>>> Piet van Oostrum (PvO) wrote: [snip] >PvO> You can also consider using paramiko instead of pexpect. [snip] >>>>> chan = t.open_session() >>>>> chan.exec_command('cat') >>>>> chan.send('abcdefghijklmn\n') In a real program it is better to use sendall here, as send may decide to send only part of its argument (the result will tell how many bytes have been sent). -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From fft1976 at gmail.com Sun Jul 19 13:33:39 2009 From: fft1976 at gmail.com (fft1976) Date: Sun, 19 Jul 2009 10:33:39 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> Message-ID: <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> On Jul 19, 9:55?am, Frank Buss wrote: > E.g. the number system: In many Lisp > implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > it returns "0.6666666666", which will result in lots of fun for porting > applications written for Python <= 2.6. How do you explain that something as inferior as Python beat Lisp in the market place despite starting 40 years later. From python at mrabarnett.plus.com Sun Jul 19 14:06:35 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 19:06:35 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <4A6360AB.4060003@mrabarnett.plus.com> fft1976 wrote: > On Jul 19, 9:55 am, Frank Buss wrote: > >> E.g. the number system: In many Lisp >> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 >> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now >> it returns "0.6666666666", which will result in lots of fun for porting >> applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. It's all part of a Dutch conspiracy to take over the world! :-) From python at mrabarnett.plus.com Sun Jul 19 14:13:13 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 19:13:13 +0100 Subject: Getting a Form To Work In-Reply-To: <4dc0cfea0907191034h2146a185o5691b0ab705f3c8d@mail.gmail.com> References: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> <4A63414D.20408@mrabarnett.plus.com> <4dc0cfea0907191034h2146a185o5691b0ab705f3c8d@mail.gmail.com> Message-ID: <4A636239.2010208@mrabarnett.plus.com> Victor Subervi wrote: > When the form comes up the first time, there is the default value for > num. When I fill in a number in the form and press send, even though the > form sends to itself (same page name), I would think it would read the > number sent. Here again is the code: > > from primeNumbers import primeNumbers > > try: > lang = form.getfirst('lang', 'en') > browser = form.getfirst('browser', 'all') > site = form.getfirst('site', 'bridge') > num = form.getfirst('num','') > except: > pass > > ourFile = string.split(__file__, "/") > p = ourFile[len(ourFile) - 1] > p = p[: - 9] > site = ourFile[4][:-10] > if site != '': > site = site[:-1] > > print "Content-Type: text/html" > print > print """ > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> > > > """ > if num != '': > num = round(float(num)) > roots = primeNumbers(num) > print roots > if num == '': > print """ >
> > name="search" id="search" /> >
> """ > print '\n' > > And why not use bare excepts? What is better? > [snip] An exception could be raised for a number of reasons, including misspelling a name. You should catch only those exceptions you expect so that others you don't expect, caused by bugs, will still be shown; you'll then know there's a bug and can fix it. From egarrulo at gmail.com Sun Jul 19 14:22:30 2009 From: egarrulo at gmail.com (Elena) Date: Sun, 19 Jul 2009 11:22:30 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <1542e10e-6ff0-43db-ab31-b73c0942ecf6@d32g2000yqh.googlegroups.com> On Jul 19, 7:33?pm, fft1976 wrote: > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. To be mainstream a language has to fit in most programmers' minds. From victorsubervi at gmail.com Sun Jul 19 14:28:10 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 19 Jul 2009 20:28:10 +0200 Subject: Trouble With a Form Message-ID: <4dc0cfea0907191128j4ed4e8det11d244769222b57e@mail.gmail.com> Hi: When the form comes up the first time, there is the default value for num. When I fill in a number in the form and press send, even though the form sends to itself (same page name), I would think it would read the number sent. Here again is the code: from primeNumbers import primeNumbers try: lang = form.getfirst('lang', 'en') browser = form.getfirst('browser', 'all') site = form.getfirst('site', 'bridge') num = form.getfirst('num','') except: pass ourFile = string.split(__file__, "/") p = ourFile[len(ourFile) - 1] p = p[: - 9] site = ourFile[4][:-10] if site != '': site = site[:-1] print "Content-Type: text/html" print print """ """ if num != '': num = round(float(num)) roots = primeNumbers(num) print roots if num == '': print """
""" print '\n' -------------- next part -------------- An HTML attachment was scrubbed... URL: From fb at frank-buss.de Sun Jul 19 14:31:36 2009 From: fb at frank-buss.de (Frank Buss) Date: Sun, 19 Jul 2009 20:31:36 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> fft1976 wrote: > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. Python is not that bad. Unlike Lisp, there is much less undefined behavior, there is one free unique implementation on the 3 major platforms Linux, Windows and MacOS X, which is stable, support multithreading and has a default GUI library binding, which is difficult to find for Lisp (e.g. I don't know of a free modern and stable Lisp implemenation with mulithreading support for Windows, with a licence with which you can use it in closed source commercial programs, like you can do with Python). Many problems in the Lispbuilder mailing list are related to problems due to different operating systems and Lisp implementations. But maybe the most important point: The syntax looks simple compared to Common Lisp (much less parentheses) and if you program in Python, it feels easier for programmer newbies. As Sussman says: "undergraduate?s initial experiences maximally productive". And this holds even for more experienced programmers. If you know already a bit of C, it is easy to use Python, but without the ability to do silly errors like writing out of array bounds (of course, you can do this in Lisp, too, if you remember to set the safe mode and if you use the right implementation). GC helps, too, to make the programming task easier than in C. Some more arguments, e.g. 5 times less program size than Java or C and more productive programmers: http://www.artima.com/intv/speedP.html (of course, an interview with Van Rossum might be a bit biased :-) -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From http Sun Jul 19 14:51:17 2009 From: http (Paul Rubin) Date: 19 Jul 2009 11:51:17 -0700 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> Message-ID: <7xljmk8q1m.fsf@ruckus.brouhaha.com> Calroc writes: > I'm engaged presently in starting a school to teach programming from > the ground up, based roughly on the curriculum outlined in the article > I mentioned. ... > I'm excited about formal methods because one, I just heard about them, Formal methods are a big and complicated subject. Right after you heard about them is probably not the best time to start teaching them. Better get to understand them yourself first. From roy at panix.com Sun Jul 19 15:09:28 2009 From: roy at panix.com (Roy Smith) Date: Sun, 19 Jul 2009 15:09:28 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, Frank Buss wrote: > there is one free unique implementation on the 3 major platforms Linux, > Windows and MacOS X Most people would still consider Solaris to be a "major platform". From rickbking at comcast.net Sun Jul 19 15:32:16 2009 From: rickbking at comcast.net (Rick King) Date: Sun, 19 Jul 2009 15:32:16 -0400 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: <50697b2c0907181606n2af8577ew3ad178f0463fa2d8@mail.gmail.com> References: <4A624D1C.5010503@comcast.net> <50697b2c0907181606n2af8577ew3ad178f0463fa2d8@mail.gmail.com> Message-ID: <4A6374C0.3000701@comcast.net> Thanks. I looked around for alternatives but didn't find this one. Rick Chris Rebert wrote: > On Sat, Jul 18, 2009 at 3:30 PM, Rick King wrote: > >> Hello, >> I want to copy files using subprocess.call or os.system where the file names >> are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. Windows >> stores all the file names in unicode so they are displayed ok in explorer, >> and I can read them into my program with listdir(u'.'), etc. and work with >> the names in the program. >> > > You should try one of the copying functions in the shutil module > instead, it'll be much simpler than using subprocess: > http://docs.python.org/library/shutil.html > > Cheers, > Chris > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Sun Jul 19 16:01:26 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 19 Jul 2009 16:01:26 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: Roy Smith wrote: > In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, > Frank Buss wrote: > >> there is one free unique implementation on the 3 major platforms Linux, >> Windows and MacOS X > > Most people would still consider Solaris to be a "major platform". ?? I do not, but I have no idea what comes in 4th after the other three by whatever metric. From stef.mientki at gmail.com Sun Jul 19 16:15:01 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 19 Jul 2009 22:15:01 +0200 Subject: any suggestions to synchronize typed text and speech ? Message-ID: <4A637EC5.4010701@gmail.com> hello, I'm using Scintilla as a wxPython widget with great pleasure. I now have an application where I want to make notes during a conversation, but also want to record the speech during that conversation. I'm using Scintilla as a wxPython widget for editing and PyAudio for the speech recording, until so far everything works fine. Here the problem part: I need to synchronize the typed text with the sound during playback. So if I click somewhere in the sound recording, the line of text, typed that moment should be highlighted. And vise versa, if the cursor in the text is moved and some special key is pressed, the sound starting 10 or 20seconds earlier should be playbacked. I though of adding bookmarks (because these are fixed in the text), and keep a list of bookmarks and sound pointers. This idea should work, but there are only 31 bookmarks. Any other suggestions ? thanks, Stef Mientki From emile at fenx.com Sun Jul 19 17:16:38 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 19 Jul 2009 14:16:38 -0700 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: On 7/19/2009 1:01 PM Terry Reedy said... > Roy Smith wrote: >> In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, >> Frank Buss wrote: >> >>> there is one free unique implementation on the 3 major platforms Linux, >>> Windows and MacOS X >> >> Most people would still consider Solaris to be a "major platform". > > ?? I do not, but I have no idea what comes in 4th after the other three > by whatever metric. > one metric calls fourth as the iPhone OS... http://marketshare.hitslink.com/report.aspx?qprid=8&qptimeframe=Y&qpsp=2009&qpmr=100&qpdt=1&qpct=0&qpob=UV%20DESC Emile From pavlovevidence at gmail.com Sun Jul 19 17:46:42 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sun, 19 Jul 2009 14:46:42 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: On Jul 19, 10:33?am, fft1976 wrote: > On Jul 19, 9:55?am, Frank Buss wrote: > > > E.g. the number system: In many Lisp > > implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > > "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > > it returns "0.6666666666", which will result in lots of fun for porting > > applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. There was no reason to crosspost this here--looking at the original thread on comp.lang.lisp it seems they were doing a surprisingly good job discussing the issue. I'm guessing it's because the fanboy Lispers like Ken Tifton were busy with a flamewar in another thread (LISP vs PROLOG vs HASKELL). Carl Banks From casevh at gmail.com Sun Jul 19 18:05:33 2009 From: casevh at gmail.com (casevh) Date: Sun, 19 Jul 2009 15:05:33 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> <4ae1e16f-124b-49c8-8364-36c59419be9b@d32g2000yqh.googlegroups.com> Message-ID: GMPY 1.10 beta is now available. This version fixes an issue where very large objects would be cached for reuse instead of being freed. Source code and Windows installers may be found at http://code.google.com/p/gmpy/downloads/list casevh From vippstar at gmail.com Sun Jul 19 18:54:17 2009 From: vippstar at gmail.com (vippstar) Date: Sun, 19 Jul 2009 15:54:17 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: On Jul 19, 9:31?pm, Frank Buss wrote: > fft1976 wrote: > > How do you explain that something as inferior as Python beat Lisp in > > the market place despite starting 40 years later. > But maybe the most important point: The syntax looks simple compared to > Common Lisp (much less parentheses) hahaha. From tundra at tundraware.com Sun Jul 19 19:29:07 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Sun, 19 Jul 2009 18:29:07 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: Carl Banks wrote: > On Jul 19, 10:33 am, fft1976 wrote: >> On Jul 19, 9:55 am, Frank Buss wrote: >> >>> E.g. the number system: In many Lisp >>> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 >>> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now >>> it returns "0.6666666666", which will result in lots of fun for porting >>> applications written for Python <= 2.6. >> How do you explain that something as inferior as Python beat Lisp in >> the market place despite starting 40 years later. > > There was no reason to crosspost this here--looking at the original > thread on comp.lang.lisp it seems they were doing a surprisingly good > job discussing the issue. > > I'm guessing it's because the fanboy Lispers like Ken Tifton were busy > with a flamewar in another thread (LISP vs PROLOG vs HASKELL). > > > Carl Banks This is an incredibly important discussion and is much weaker because it does not also include Pascal, BASIC, Ada, Oberon and Forth. In fact, picking a computer language is the most important discussion in Computer Science and eclipses even P=NP? in significance. I sure hope we can keep this thread going for a few months. For guidance, see: http://www.tundraware.com/Technology/How-To-Pick-A-Programming-Language/ -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From marcusw at cox.net Sun Jul 19 19:32:02 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sun, 19 Jul 2009 19:32:02 -0400 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: On 7/19/2009 4:15 PM, Stef Mientki wrote: > hello, > > I'm using Scintilla as a wxPython widget with great pleasure. > I now have an application where I want to make notes during a conversation, > but also want to record the speech during that conversation. > I'm using Scintilla as a wxPython widget for editing and PyAudio for the > speech recording, > until so far everything works fine. > > Here the problem part: > I need to synchronize the typed text with the sound during playback. > So if I click somewhere in the sound recording, > the line of text, typed that moment should be highlighted. > And vise versa, if the cursor in the text is moved and some special key > is pressed, > the sound starting 10 or 20seconds earlier should be playbacked. > > I though of adding bookmarks (because these are fixed in the text), and > keep a list of bookmarks and sound pointers. > This idea should work, but there are only 31 bookmarks. > > Any other suggestions ? > > thanks, > Stef Mientki That sounds like a very specialized type of thing, which only the few people with experience with wxPython, PyAudio, and Scintilla could help you with... But you might try having a dictionary with notes and associated times, or just give each note a four-digit ID number at the beginning of it when it's entered and use that in the dictionary (to keep keys shorter). Or you could just do a little hack and increase the number of bookmarks allowed (seeing as source is available) :p Marcus From http Sun Jul 19 19:35:01 2009 From: http (Paul Rubin) Date: 19 Jul 2009 16:35:01 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <7xiqho1c2i.fsf@ruckus.brouhaha.com> Emile van Sebille writes: > >> Most people would still consider Solaris to be a "major platform". > > ?? I do not, but I have no idea what comes in 4th after the other > > three by whatever metric. > one metric calls fourth as the iPhone OS... > http://marketshare.hitslink.com/report.aspx?qprid=8... That metric is clearly wrong. The #1 platform OS platform in terms of number of units shipped is Javacard, which is in the SIM cards of billions of GSM phones. From pavlovevidence at gmail.com Sun Jul 19 20:45:01 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sun, 19 Jul 2009 17:45:01 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> On Jul 19, 4:29?pm, Tim Daneliuk wrote: > Carl Banks wrote: > > On Jul 19, 10:33 am, fft1976 wrote: > >> On Jul 19, 9:55 am, Frank Buss wrote: > > >>> E.g. the number system: In many Lisp > >>> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > >>> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > >>> it returns "0.6666666666", which will result in lots of fun for porting > >>> applications written for Python <= 2.6. > >> How do you explain that something as inferior as Python beat Lisp in > >> the market place despite starting 40 years later. > > > There was no reason to crosspost this here--looking at the original > > thread on comp.lang.lisp it seems they were doing a surprisingly good > > job discussing the issue. > > > I'm guessing it's because the fanboy Lispers like Ken Tifton were busy > > with a flamewar in another thread (LISP vs PROLOG vs HASKELL). > > > Carl Banks > > This is an incredibly important discussion It might be an important question but a discussion on Usenet about it is utterly useless. > and is much weaker because > it does not also include Pascal, BASIC, Ada, Oberon and Forth. In the same way that a movie is weaker because the director edited out the bad scenes. > In fact, > picking a computer language is the most important discussion in > Computer Science and eclipses even P=NP? in significance. I sure hope > we can keep this thread going for a few months. Please feel free to extend this flame-war along for a few months on comp.lang.lisp. Not here. Carl Banks From marek at xivilization.net Sun Jul 19 20:51:49 2009 From: marek at xivilization.net (Marek Kubica) Date: Mon, 20 Jul 2009 02:51:49 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <20090720025149.326f0cfc@halmanfloyd.lan.local> On Sun, 19 Jul 2009 15:09:28 -0400 Roy Smith wrote: > In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, > Frank Buss wrote: > > > there is one free unique implementation on the 3 major platforms > > Linux, Windows and MacOS X > > Most people would still consider Solaris to be a "major platform". Depends on who you ask. On the desktop it doesn't matter at all (fortunately, since everytime I work on Solaris I'm entering a world of pain which is just slowly getting better with OpenSolaris), on the server it (and other propietary Unices) is losing ground compared to the free Unices. But ok, let's say 3 major platforms: Unix, Windows and Mac OS X. Mac OS X is formally a Unix but everything with GUI is non-Unix'y so it can be considered a separate platform. regards, Marek From marek at xivilization.net Sun Jul 19 20:54:13 2009 From: marek at xivilization.net (Marek Kubica) Date: Mon, 20 Jul 2009 02:54:13 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1542e10e-6ff0-43db-ab31-b73c0942ecf6@d32g2000yqh.googlegroups.com> Message-ID: <20090720025413.12eb7fbf@halmanfloyd.lan.local> On Sun, 19 Jul 2009 11:22:30 -0700 (PDT) Elena wrote: > On Jul 19, 7:33?pm, fft1976 wrote: > > How do you explain that something as inferior as Python beat Lisp in > > the market place despite starting 40 years later. > > To be mainstream a language has to fit in most programmers' minds. Sometimes I wonder whether Python is not too complex for "most programmers", since "most programmers" use Java, which is heavily mainstream. Interesting whether it is in spite of the ridiculous limitations or because of these. Probably a mix of both. regards, Marek From research at johnohagan.com Sun Jul 19 21:44:44 2009 From: research at johnohagan.com (John O'Hagan) Date: Mon, 20 Jul 2009 01:44:44 +0000 Subject: question on input function In-Reply-To: <346922.15108.qm@web56908.mail.re3.yahoo.com> References: <346922.15108.qm@web56908.mail.re3.yahoo.com> Message-ID: <200907200144.44417.research@johnohagan.com> On Mon, 20 Jul 2009, Richel Satumbaga wrote: > I am just learning python then I encountered an certain point in terms of > using the input function of python. the source code: > eq = input("enter an equation:"); > print " the result is : "; > > > the output seen in the command window: > > enter an equation:[m*x+b for m in (0,10,1),for x in (1,11,1), for b in > (2,12,1)] > > Traceback (most recent call last): > File "C:/Python26/try", line 1, in > eq = input("enter an equation:"); > File "", line 1 > [m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] > ^ > SyntaxError: invalid syntax [...] It's the extra commas in your list comprehension.This should work: [m*x+b for m in (0,10,1) for x in (1,11,1) for b in (2,12,1)] HTH, John -------------- next part -------------- An HTML attachment was scrubbed... URL: From rlsatumbaga at yahoo.com Sun Jul 19 22:07:07 2009 From: rlsatumbaga at yahoo.com (Richel Satumbaga) Date: Sun, 19 Jul 2009 19:07:07 -0700 (PDT) Subject: question on input function Message-ID: <346922.15108.qm@web56908.mail.re3.yahoo.com> I am just learning python then I encountered an certain point in terms of using the input function of python. the source code: ?????????????????????? eq = input("enter an equation:"); ?????????????????????? print " the result is : "; the output seen in the command window: >>> enter an equation:[m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] Traceback (most recent call last): ? File "C:/Python26/try", line 1, in ??? eq = input("enter an equation:"); ? File "", line 1 ??? [m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] ?????????????????????????????? ^ SyntaxError: invalid syntax ????????????????? Is there any other way to enter those? or it must be coded in the source code? If in source code is it like in matlab where one can type: x = [0:1:10]; to define the range of x which is form 0 to 10 with the increment of? 1. Thanks Ann -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Sun Jul 19 22:07:34 2009 From: roy at panix.com (Roy Smith) Date: Sun, 19 Jul 2009 22:07:34 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <20090720025149.326f0cfc@halmanfloyd.lan.local> Message-ID: In article <20090720025149.326f0cfc at halmanfloyd.lan.local>, Marek Kubica wrote: > > > there is one free unique implementation on the 3 major platforms > > > Linux, Windows and MacOS X > > > > Most people would still consider Solaris to be a "major platform". > > Depends on who you ask. On the desktop it doesn't matter at all > (fortunately, since everytime I work on Solaris I'm entering a world of > pain which is just slowly getting better with OpenSolaris), on the > server it (and other propietary Unices) is losing ground compared to > the free Unices. Clearly, there's not much (if any) Solaris on the desktop. But it's sure alive and kicking in the server world. One of the things I like about Python is not just how portable is it is, but how easy it is to build and deploy. We build our product on Windows, N different Linux distros, Solaris, HPUX, AIX, and OSX. I've built Python for all of those platforms. Then, I checked the install areas into Perforce. Deploying is as simple as checking out the right sandbox. We've gone through the same exercise for Perl. That was a lot more painful. From fatkinson at mishmash.com Sun Jul 19 22:22:37 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Sun, 19 Jul 2009 19:22:37 -0700 Subject: Final Project Message-ID: I'm looking for some ideas here. I think I've mentioned I am taking a course in Python and PHP. The professor wants each of us to pick a project to write in both languages. It has to be something fairly complex and I'd like for it to be something that would be useful on my Web site(s). I would be grateful for any and all suggestions. Regards, Fred From clp2 at rebertia.com Sun Jul 19 22:44:45 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 19 Jul 2009 19:44:45 -0700 Subject: question on input function In-Reply-To: <346922.15108.qm@web56908.mail.re3.yahoo.com> References: <346922.15108.qm@web56908.mail.re3.yahoo.com> Message-ID: <50697b2c0907191944i43d14c85m270bd391e366ec21@mail.gmail.com> On Sun, Jul 19, 2009 at 7:07 PM, Richel Satumbaga wrote: > I am just learning python then I encountered an certain point in terms of > using the input function of python. > the source code: > ?????????????????????? eq = input("enter an equation:"); > ?????????????????????? print " the result is : "; > > > the output seen in the command window: >>>> > enter an equation:[m*x+b for m in (0,10,1),for x in (1,11,1), for b in > (2,12,1)] > > Traceback (most recent call last): > ? File "C:/Python26/try", line 1, in > ??? eq = input("enter an equation:"); > ? File "", line 1 > ??? [m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] > ?????????????????????????????? ^ > SyntaxError: invalid syntax > > ????????????????? Is there any other way to enter those? or it must be coded > in the source code? > If in source code is it like in matlab where one can type: > > x = [0:1:10]; > > to define the range of x which is form 0 to 10 with the increment of? 1. First, commas between the "for"s are not permitted, which gives us: [m*x+b for m in (0,10,1) for x in (1,11,1) for b in (2,12,1)] However, in Python, (i, j, k) is just a tuple (MATLAB: 1D matrix) of the items i, j, and k. What you want is a range, which is understandably created using the range() function. A step of 1 and a start of 0 are the defaults, and the stop value is always excluded, so the code becomes: [m*x+b for m in range(11) for x in range(1,12) for b in range(2,13)] Which should be what you want. For further explanation of the range() function: range(b) - the numbers from 0 to b-1 with a step of 1 range(a,b) - the numbers from a to b-1 with a step of 1 range(a,b,c) - the numbers from a to b-1 with a step of c Hope that helps. Cheers, Chris -- http://blog.rebertia.com From denize.paul at gmail.com Sun Jul 19 22:57:50 2009 From: denize.paul at gmail.com (pdenize) Date: Sun, 19 Jul 2009 19:57:50 -0700 (PDT) Subject: Managing non-ascii filenames in python Message-ID: <60a305de-0552-4f77-9051-d87149d308c7@y10g2000prg.googlegroups.com> I created the following filename in windows just as a test - ?D?n?ld?s? N?ph?ws? deg?.txt The quotes are non -ascii, many non english characters, long hyphen etc. Now in DOS I can do a directory and it translates them all to something close. "D?n?ld'sT N?ph?ws" deg?.txt I thought the correct way to do this in python would be to scan the dir files=os.listdir(os.path.dirname( os.path.realpath( __file__ ) )) then print the filenames for filename in files: print filename but as expected teh filename is not correct - so correct it using the file sysytems encoding print filename.decode(sys.getfilesystemencoding()) But I get UnicodeEncodeError: 'charmap' codec can't encode character u'\u2014' in position 6: character maps to All was working well till these characters came along I need to be able to write (a representation) to the screen (and I don't see why I should not get something as good as DOS shows). Write it to an XML file in UTF-8 and write it to a text file and be able to read it back in. Again I was supprised that this was also difficult - it appears that the file also wanted ascii. Should I have to open the file in binary for write (I expect so) but then what encoding should I write in? I have been beating myself up with this for weeks as I get it working then come across some outher character that causes it all to stop again. Please help. From mensanator at aol.com Mon Jul 20 00:12:35 2009 From: mensanator at aol.com (Mensanator) Date: Sun, 19 Jul 2009 21:12:35 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> <4ae1e16f-124b-49c8-8364-36c59419be9b@d32g2000yqh.googlegroups.com> Message-ID: <2c3e8fc0-e814-4020-bcfd-d3d02cc62dea@t13g2000yqt.googlegroups.com> On Jul 19, 5:05?pm, casevh wrote: > GMPY 1.10 beta is now available. This version fixes an issue where > very large objects would be cached for reuse instead of being freed. Excellent! That explains the funny memory usage graph. > > Source code and Windows installers may be found athttp://code.google.com/p/gmpy/downloads/list > Test results: import collatz_functions as cf # imports gmpy # the cf.Type12MH function does the equivalent # of the pure Python version of the function # using gmpy import time #3.1 gmpy 1.10 beta # t0 = time.time() n=10 for k in range(1,n): for i in range(1,n-2): print((str(cf.gmpy.numdigits(cf.Type12MH(k,i))).zfill(n)),end=' ') print() print() t1 = time.time() print(t1-t0) # 3.1 pure Python # ##t0 = time.time() ##n=10 ##for k in range(1,n): ## for i in range(1,n-2): ## print((str(cf.gmpy.numdigits( \ ## 2**(6*((i-1)*9**(k-1)+(9**(k-1)-1)//2+1)-1)-1 \ ## )).zfill(n)),end=' ') ## print() ##print() ##t1 = time.time() ## ##print(t1-t0) ## 3.1 gmpy 1.10 alpha ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##3.32299995422 ## funny memory usage display, different between 2.6 & 3.1, but funny regardless, ## memory remains allocated (~700 MB) when program halts ## 3.1 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##338.832000017 ## memory usage seems normal, runs slow enough to observe memory being allocated ## and freed and remains freed when program halts ## 2.6 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##aborted after ONE FULL WEEK of processing (only 8 of 9 rows completed) ## Something funny happened here. By the time I ran this test, there ## were 14 orphaned copies of pythonw.exe present, at least one of which ## was still running along with the real pair executing the program. Yet ## the Performance only registered ~50%. Normally, this would be 100% if ## two programs were running. Pure Python is a LOT slower than gmpy, ## but not THAT slow. These orphaned processes may be a Python 3.1 issue. ##=================================================================== ## 3.1 gmpy 1.10 beta ## ## 0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ## 0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ## 0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ## 0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ## 0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ## 0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ## 0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ## 0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ## 0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ## 2.41799998283 ## memory usage now appears normal, no more allocated memory when program halts ## and it's so fast all we see on Performance graph is a small blip, much better ## than the 700-1400 MB it was allocating before. ## 3.1 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##339.611999989 ## looks like gmpy about a 100 times faster. I knew there was a reason I ## won't use Python without gmpy. ## 2.6 gmpy 1.10 beta ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##2.68400001526 ## 2.6 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##338.987999916 ## ah, that's more like it. no more pythonw.exe thrashing. I was almost ready ## to drop version 2.6 like a live grenade, but that was based on some other ## fault apparently. it may be 3.1 I have to drop. I still got a single instance of an orphaned pythonw.exe, but this happened during a quick attempt at provoking it during a 3.1 pure Python test, so I have no reason to suspect gmpy (previously, I thought it may be related to the memory leak). I've got an idea on how to reliably provoke this which I'll try to work up later (and won't even load gmpy to make sure it's not involved). > casevh From steven at REMOVE.THIS.cybersource.com.au Mon Jul 20 01:11:17 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 20 Jul 2009 05:11:17 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: On Sun, 19 Jul 2009 10:33:39 -0700, fft1976 wrote: > On Jul 19, 9:55?am, Frank Buss wrote: > >> E.g. the number system: In many Lisp >> implementations (/ 2 3) results in the fractional object 2/3. In Python >> 2.6 "2 / 3" results in "0". Looks like with Python 3.1 they have fixed >> it, now it returns "0.6666666666", which will result in lots of fun for >> porting applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in the > market place despite starting 40 years later. http://www.jwz.org/doc/worse-is-better.html Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing to do. It's the right thing to do if you're doing integer maths. -- Steven From fb at frank-buss.de Mon Jul 20 01:18:01 2009 From: fb at frank-buss.de (Frank Buss) Date: Mon, 20 Jul 2009 07:18:01 +0200 Subject: Final Project References: Message-ID: <1ctywmarsqqzd.y7fg69mkbshy$.dlg@40tude.net> Fred Atkinson wrote: > I'm looking for some ideas here. > > I think I've mentioned I am taking a course in Python and PHP. > The professor wants each of us to pick a project to write in both > languages. It has to be something fairly complex and I'd like for it > to be something that would be useful on my Web site(s). > > I would be grateful for any and all suggestions. Useful for your webpage depends very much on what you think what you need and it is difficult to answer by others who don't know your needs. E.g. useful for me was a Python Blender script to create wireframe and solid models of platonic solids and a program for geodesic domes of parametrized detail level: http://www.frank-buss.de/shapeways/dome.py http://www.shapeways.com/mydesign?user_id=19157 It is my first bigger Python program, so ideas how it could be improved are welcome. Would be an interesting project to create a renderer for it, which can calculate this and other geometric figures as a CGI script to an image and display the result in the browser. Try this in PHP and Python and write about the experiences you made. Maybe don't forget to limit the access to the webpage or the size of the output image, to avoid trouble with your hosting provider, if you don't have your own server and anything slows down because of your rendering task :-) -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From tundra at tundraware.com Mon Jul 20 01:18:58 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Mon, 20 Jul 2009 00:18:58 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> Message-ID: Carl Banks wrote: > On Jul 19, 4:29 pm, Tim Daneliuk wrote: >> Carl Banks wrote: >>> On Jul 19, 10:33 am, fft1976 wrote: >>>> On Jul 19, 9:55 am, Frank Buss wrote: >>>>> E.g. the number system: In many Lisp >>>>> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 >>>>> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now >>>>> it returns "0.6666666666", which will result in lots of fun for porting >>>>> applications written for Python <= 2.6. >>>> How do you explain that something as inferior as Python beat Lisp in >>>> the market place despite starting 40 years later. >>> There was no reason to crosspost this here--looking at the original >>> thread on comp.lang.lisp it seems they were doing a surprisingly good >>> job discussing the issue. >>> I'm guessing it's because the fanboy Lispers like Ken Tifton were busy >>> with a flamewar in another thread (LISP vs PROLOG vs HASKELL). >>> Carl Banks >> This is an incredibly important discussion > > It might be an important question but a discussion on Usenet about it > is utterly useless. > > >> and is much weaker because >> it does not also include Pascal, BASIC, Ada, Oberon and Forth. > > In the same way that a movie is weaker because the director edited out > the bad scenes. > > >> In fact, >> picking a computer language is the most important discussion in >> Computer Science and eclipses even P=NP? in significance. I sure hope >> we can keep this thread going for a few months. > > Please feel free to extend this flame-war along for a few months on > comp.lang.lisp. Not here. > > > Carl Banks Uh Carl ... are you familiar with the concept of mocking humor? -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From gagsl-py2 at yahoo.com.ar Mon Jul 20 01:54:57 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 02:54:57 -0300 Subject: file write IOError Invalid argument References: <505868.24184.qm@web59308.mail.re1.yahoo.com> Message-ID: En Thu, 16 Jul 2009 17:41:39 -0300, Robert Robert escribi?: > I am trying to write a binary string to file on a windows network share. > I get an IOError. I've read that it is because the file size is too > large. I did a type( binaryString) and saw that it was type "str". So I > loop through it one by one element and use f.write to write to file, and > it worked without any error. However it took a long while to write to > file. I was wondering how I could split this binary string into N number > of chunks efficiently and write them to file. some_string[i:i+BLOCK_SIZE] returns a slice of the original string, starting at position i, with length BLOCK_SIZE. -- Gabriel Genellina From bob.martin at excite.com Mon Jul 20 02:12:45 2009 From: bob.martin at excite.com (Bob Martin) Date: Mon, 20 Jul 2009 06:12:45 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: in 121683 20090719 210126 Terry Reedy wrote: >Roy Smith wrote: >> In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, >> Frank Buss wrote: >> >>> there is one free unique implementation on the 3 major platforms Linux, >>> Windows and MacOS X >> >> Most people would still consider Solaris to be a "major platform". > >?? I do not, but I have no idea what comes in 4th after the other three >by whatever metric. I think the OP means "major PC operating systems". Those with a wider knowledge of the computer world would consider IBM's mainframe operating systems to be deserving of the description "major". From http Mon Jul 20 02:13:15 2009 From: http (Paul Rubin) Date: 19 Jul 2009 23:13:15 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <7x4ot7rif8.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing > to do. It's the right thing to do if you're doing integer maths. I wonder whether 2/3 => ValueError is preferable. From martin at v.loewis.de Mon Jul 20 02:27:20 2009 From: martin at v.loewis.de (=?windows-1252?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Mon, 20 Jul 2009 08:27:20 +0200 Subject: Managing non-ascii filenames in python In-Reply-To: <60a305de-0552-4f77-9051-d87149d308c7@y10g2000prg.googlegroups.com> References: <60a305de-0552-4f77-9051-d87149d308c7@y10g2000prg.googlegroups.com> Message-ID: <4a640e48$0$7445$9b622d9e@news.freenet.de> > I thought the correct way to do this in python would be to scan the > dir > files=os.listdir(os.path.dirname( os.path.realpath( __file__ ) )) > > then print the filenames > for filename in files: > print filename > > but as expected teh filename is not correct - so correct it using the > file sysytems encoding > > print filename.decode(sys.getfilesystemencoding()) > > But I get > UnicodeEncodeError: 'charmap' codec can't encode character u'\u2014' > in position 6: character maps to As a starting point, you shouldn't be using byte-oriented APIs to access files on Windows; the specific byte-oriented API is os.listdir, when passed a directory represented as a byte string. So try: dirname = os.path.dirname(os.path.realpath(__file__)) dirname = dirname.decode(sys.getfilesystemencoding() files = os.listdir(dirname) This should give you the files as Unicode strings. > I need to be able to write (a representation) to the screen (and I > don't see why I should not get something as good as DOS shows). The command window (it's not really DOS anymore) uses the CP_OEMCP encoding, which is not available in Python. This does all the transliteration also, so you would have to write an extension module if you want to get the same transliteration (or try to get to the OEMCP encoding through ctypes). If you can live with a simpler transliteration, try print filename.encode(sys.stdout.encoding, "replace") > Write it to an XML file in UTF-8 > > and write it to a text file and be able to read it back in. > Again I was supprised that this was also difficult - it appears that > the file also wanted ascii. Should I have to open the file in binary > for write (I expect so) but then what encoding should I write in? You need to tell us how precisely you tried to do this. My guess is: if you now try again, with the filenames being Unicode strings, it will work fairly well. Regards, Martin From fb at frank-buss.de Mon Jul 20 02:28:58 2009 From: fb at frank-buss.de (Frank Buss) Date: Mon, 20 Jul 2009 08:28:58 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <1pk5i4u29maii.vcsqz0mmsyy5$.dlg@40tude.net> Bob Martin wrote: > I think the OP means "major PC operating systems". Those with a wider > knowledge of the computer world would consider IBM's mainframe operating > systems to be deserving of the description "major". Maybe you are right, if you mean big machines. I know mainframes a bit and there are interesting concepts, like hot-swapping of CPU modules and mainframes are very reliable. But expensive, too. I know at least one client, who wants to change it to some cheap Linux boxes, like Google demonstrates it. If you take care (e.g. Xen virtualization for easier computer changing and RAID harddisks, if a downtime of some hours might be ok), it doesn't matter if one PC goes out of order. But even on IBM mainframes you can install Linux or other Unix systems in parallel to the usual operating systems for this machines, so except for special cases, like embedded systems, the most installed and used operating systems might be Unix-like systems and Windows. But looks like Python even runs on more native operating systems for mainframes. -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From bob.martin at excite.com Mon Jul 20 02:40:21 2009 From: bob.martin at excite.com (Bob Martin) Date: Mon, 20 Jul 2009 06:40:21 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <1pk5i4u29maii.vcsqz0mmsyy5$.dlg@40tude.net> Message-ID: in 121708 20090720 072858 Frank Buss wrote: >Bob Martin wrote: > >> I think the OP means "major PC operating systems". Those with a wider >> knowledge of the computer world would consider IBM's mainframe operating >> systems to be deserving of the description "major". > >Maybe you are right, if you mean big machines. I know mainframes a bit and >there are interesting concepts, like hot-swapping of CPU modules and >mainframes are very reliable. But expensive, too. I know at least one >client, who wants to change it to some cheap Linux boxes, like Google >demonstrates it. If you take care (e.g. Xen virtualization for easier >computer changing and RAID harddisks, if a downtime of some hours might be >ok), it doesn't matter if one PC goes out of order. > >But even on IBM mainframes you can install Linux or other Unix systems in >parallel to the usual operating systems for this machines, so except for >special cases, like embedded systems, the most installed and used operating >systems might be Unix-like systems and Windows. But looks like Python even >runs on more native operating systems for mainframes. Yes, a "platform" is really the combination of hardware architecture and operating system, so Linux on Intel and Linux on 390 are different platforms. From pavlovevidence at gmail.com Mon Jul 20 03:08:31 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 20 Jul 2009 00:08:31 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> Message-ID: <77e91c9e-5487-427d-9300-3ad9015adf51@2g2000prl.googlegroups.com> On Jul 19, 10:18?pm, Tim Daneliuk wrote: > Uh Carl ... are you familiar with the concept of mocking humor? You got me, lip hurts bad. :) Carl Banks From gagsl-py2 at yahoo.com.ar Mon Jul 20 03:29:17 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 04:29:17 -0300 Subject: invoke method on many instances References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac escribi?: >> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>> def apply2(itr, methodname, *args, **kwargs): >>> f = operator.methodcaller(methodname, *args, **kwargs) >>> for item in itr: >>> f(item) > > > On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >> for obj in objects: >> getattr(obj, methodname)(*args, **kwargs) > > > Are there any obvious considerations in choosing > between those two? The operator.methodcaller version is faster in my tests for large collections, but slightly slower when you have very few elements. import operator import timeit class X: def method(self, x, y, **kw): pass def apply1(itr, methodname, *args, **kwargs): for item in itr: getattr(item, methodname)(*args, **kwargs) def apply2(itr, methodname, *args, **kwargs): f = operator.methodcaller(methodname, *args, **kwargs) for item in itr: f(item) L=[X() for _ in range(3000)] apply1(L,'method', 1, 2, foo=3) apply2(L,'method', 1, 2, foo=3) timeit.timeit(setup="from __main__ import apply1,apply2,L", stmt="apply1(L,'method', 1, 2, foo=3)", number=1000) timeit.timeit(setup="from __main__ import apply1,apply2,L", stmt="apply2(L,'method', 1, 2, foo=3)", number=1000) -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 20 03:50:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 04:50:15 -0300 Subject: Mechanize not recognized by py2exe References: <4A60C81F.7080707@comcast.net> Message-ID: En Fri, 17 Jul 2009 15:51:11 -0300, Stephen M. Olds escribi?: > I have a Python script getData.py that uses Mechanize, and runs fine > under the > interpreter. It was installed using easy_install - and the install > seemed to > indicate it was completed. > > The problem is, when I try to compile it using py2exe while in the > folder of the > script, and using the run line command: > > python getData.py py2exe > > I get the warning: "Import error: No Module named mechanize"... > > I checked the environmental path and find the following: > %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program > Files (x86)\Python;C:\Python25\Lib\site-packages;C:\Program > Files (x86)\Common Files\Adobe\AGL The system PATH is irrelevant here - it's only used by Windows to locate the Python executable. I don't see a reason to list C:\Python25\Lib\site-packages here. You're mostly interested in sys.path instead (that is, the 'path' attribute in the 'sys' Python module) > I did a search for mechanize and find an egg file in > C:\Python25\Lib\site-packages > named mechanize-0.1.7b-py2.5. > > Not really understanding the "egg" thing, what do I have here that needs > to be > done? I'm not an egg expert either, and I try to avoid them as a plague. But I *think* you can delete the .egg and re-install it using easy_install with the -Z option. -- Gabriel Genellina From greg at cosc.canterbury.ac.nz Mon Jul 20 04:00:14 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Mon, 20 Jul 2009 20:00:14 +1200 Subject: tough-to-explain Python In-Reply-To: <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> Message-ID: <7cimf0F27u23eU1@mid.individual.net> Calroc wrote: > It may be that flawless software is an unreachable asymptote, like the > speed of light for matter, but I'm (recently!) convinced that it's a > goal worthy of exploration and effort. Seems to me that once you get beyond the toy program stage and try to apply correctness proving to real world programming problems, you run up against the problem of what exactly you mean by "correct". Once the requirements get beyond a certain level of complexity, deciding whether the specifications themselves are correct becomes just as difficult as deciding whether the program meets them. Then there's the fact that very often we don't even know what the exact requirements are, and it's only by trying to write and use the program that we discover what they really are -- or at least, get a better idea of what they are, because the process is usually iterative, with no clear end point. So in theory, correctness proofs are a fine idea, and might even be doble on a small scale, but the real world is huge and messy! > Just because it's unattainable doesn't make it undesirable. And who > knows? Maybe the horse will learn to sing. Striving to find ways of writing less bugs is a worthy goal, but I think of it more in terms of adopting patterns of thought and programming that make it more likely you will write code that does what you had in mind, rather than a separate "proof" process that you go through afterwards. -- Greg From stef.mientki at gmail.com Mon Jul 20 05:34:09 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Mon, 20 Jul 2009 11:34:09 +0200 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: <4A643A11.6030109@gmail.com> thanks Marcus, Marcus Wanner wrote: > On 7/19/2009 4:15 PM, Stef Mientki wrote: >> hello, >> >> I'm using Scintilla as a wxPython widget with great pleasure. >> I now have an application where I want to make notes during a >> conversation, >> but also want to record the speech during that conversation. >> I'm using Scintilla as a wxPython widget for editing and PyAudio for >> the speech recording, >> until so far everything works fine. >> >> Here the problem part: >> I need to synchronize the typed text with the sound during playback. >> So if I click somewhere in the sound recording, >> the line of text, typed that moment should be highlighted. >> And vise versa, if the cursor in the text is moved and some special >> key is pressed, >> the sound starting 10 or 20seconds earlier should be playbacked. >> >> I though of adding bookmarks (because these are fixed in the text), >> and keep a list of bookmarks and sound pointers. >> This idea should work, but there are only 31 bookmarks. >> >> Any other suggestions ? >> >> thanks, >> Stef Mientki > That sounds like a very specialized type of thing, Well from an application point of view, with the current netbooks, this looks like a perfect tool for any conversation or meeting. > which only the few people with experience with wxPython, PyAudio, and > Scintilla could help you with... > I was afraid of that too, so I dropped the question in several places, and the writer of Scintilla himself came with the perfect answer. cheers,Stef > But you might try having a dictionary with notes and associated times, > or just give each note a four-digit ID number at the beginning of it > when it's entered and use that in the dictionary (to keep keys > shorter). Or you could just do a little hack and increase the number > of bookmarks allowed (seeing as source is available) :p > > Marcus From sion at viridian.paintbox Mon Jul 20 05:34:24 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Mon, 20 Jul 2009 09:34:24 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: Terry Reedy wrote: >Sion Arrowsmith wrote: >> Jack Diederich wrote: >>> It isn't an OrderedDict thing, it is a comparison thing. Two regular >>> dicts also raise an error if you try to LT them. >> Python 2.5.2 >>>>> d1 = dict((str(i), i) for i in range (10)) >>>>> d2 = dict((str(i), i) for i in range (20)) >>>>> d1 < d2 >> True >Try reversing the definitions of d1 and d2. The dicts are probably being >compared by id (address), which is the 2.x CPython default. Like this? >>> d1 = dict((str(i), i) for i in range (20)) >>> d2 = dict((str(i), i) for i in range (10)) >>> d1 < d2 False >>> id(d1) < id(d2) True I didn't know that comparison for anything other than equality defaulted to using id. That really is rather broken, and I'm glad 3.0 fixed it. -- \S under construction From walkraft at gmail.com Mon Jul 20 05:57:36 2009 From: walkraft at gmail.com (casebash) Date: Mon, 20 Jul 2009 02:57:36 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? Message-ID: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Hi, I have searched this list and found out that Python doesn't have a mutable string class (it had an inefficient one, but this was removed in 3.0). Are there any libraries outside the core that offer this? Thanks, Chris From vivainio at gmail.com Mon Jul 20 06:12:25 2009 From: vivainio at gmail.com (Ville Vainio) Date: Mon, 20 Jul 2009 03:12:25 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops Message-ID: Has anyone implementing something like what the subject line indicates? The idea: To run functions that execute a series of system commands without blocking the ui, *and* without adding state machine logic. The syntax would be something like: def work(): showstatus("building") r = yield runshell("make") showstatus("installing") r = yield runshell("make install") showstatus("Success") mygui.startwork(work) # returns immediately, runs work() gradually in the background. The catch is that showstatus() would need to be run in the mainloop, so running the whole thing in a thread is a no-go. I imagine runshell() would be implemented in terms of QProcess, or subprocess.Popen/os.system and a worker thread. Anyone done this already, or do I have to roll my own? From vivainio at gmail.com Mon Jul 20 06:14:35 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Mon, 20 Jul 2009 03:14:35 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: <9600897a-100c-4907-826d-0d700cef6880@r2g2000yqm.googlegroups.com> On Jul 20, 1:12?pm, Ville Vainio wrote: > Has anyone implementing something like what the subject line ImplentED. I don't think this is that hard to do in the first place, but a "generic" solution that can be easily tuned for different gui mainloops would be nice. From pavlovevidence at gmail.com Mon Jul 20 06:26:09 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 20 Jul 2009 03:26:09 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: On Jul 20, 2:57?am, casebash wrote: > Hi, > > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? I just did a brief search on Python Packaging Index (pypi.python.org) and saw nothing. You might have better luck. I suspect there is not a mutable string class partly because there is little demand, partly because it'd be hard to make an object that is usable everywhere a string is. For instance, a third-party string object might not work with the re module. The core does have some classes that are kind of like strings but mutable. The array module can create mutable arrays of characters which are somewhat like strings. Depending on your use case some other things might suffice (such as mmap, io.StringIO, or even a list of characters). Carl Banks From vivainio at gmail.com Mon Jul 20 06:28:33 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Mon, 20 Jul 2009 03:28:33 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: On Jul 20, 1:12?pm, Ville Vainio wrote: > I imagine runshell() would be implemented in terms of QProcess, or > subprocess.Popen/os.system and a worker thread. Actually, the problem is that of general serialization of worker thread operations. That is, it could be something akin to: res = yield run_in_thread(lambda : os.system('make')) run_in_thread would basically grab a worked thread, execute the callable, and allow the generator to proceed after the thread is done. Now, the only thing to do is the "generator dispatcher" in gui mainloop that finds the correct generator and makes it proceed... From ben+python at benfinney.id.au Mon Jul 20 07:08:22 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 20 Jul 2009 21:08:22 +1000 Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: <87r5wba9y1.fsf@benfinney.id.au> casebash writes: > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? A mutable string would not (AFAICT) be usefully implementable as a subclass of the built-in string types. So even if such a type existed, it would not be useable with all the functionality that works with strings. What is it you're trying to do that makes you search for a mutable string type? It's likely that a better approach can be found. -- \ ?As scarce as truth is, the supply has always been in excess of | `\ the demand.? ?Josh Billings | _o__) | Ben Finney From sjmachin at lexicon.net Mon Jul 20 07:24:06 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 04:24:06 -0700 (PDT) Subject: On out-of-date Python Applications References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> Message-ID: On Jul 20, 1:47?am, John Machin wrote: > On 20/07/2009 12:24 AM, Virgil Stokes wrote: > > John Machin wrote: > >> On Jul 19, 6:04 pm, Virgil Stokes wrote: > >>> I am not a heavy user of Python; but, I do work with it and some of its > >>> application packages (e.g. PyODE), in an academic setting. > >>> Many of these applications packages have a Windows installer which > >>> usually works fine. However, I also try to keep up with the latest > >>> release of Python, and this is where I often have problems. That is, the > >>> latest Windows installer provided for some of these applications will > >>> not install on the latest version of Python. > > Actually John, > > My question was about the more general case --- how to help with > > upgrading applications; but, I will try to address your email. > >> Otherwise: What does "will not install" mean? > > This means that when the Windows installer is executed it informs you > > that you do not have a match between the application and the Python that > > it finds on your Windows PC. You can easily find this out for yourself > > by trying to install PyODE on a Windows platform with a Python version > > later than 2.5.x. > >> Are these pure Python > >> packages or do they include C extensions (binary (pyd) or source?)? > > Obviously, this depends on the application. In the particular case that > > I mentioned (PyODE) I believe that there is a mix of C binaries and > > Python code. > >> At > >> what stage of the installation does the installation fail? > > In the initial stages. > >> With what > >> error message(s)? > > In one specific case, the message is (paraphrasing) "You do not have > > Python 2.5 installed" --- the installation is then aborted. > >> With what versions of Python? > > > I have only Python 2.6.2 on my home Windows Vista PC and PyODE does not > > have a Windows Installer for Python 2.6. > > >>> I do understand that there can be a time lag between the release of > >>> Python applications and the latest Python. I also appreciate the work of > >>> the people that are responsible for these applications. > > >>> My question is --- Is there anything I, as a user of an application > >>> package that is out-of-date with respect to the latest Python, can do to > >>> help in this process of bringing an application ?up-to-date? > > Note, again John, my question was about how I might assist (help) with > > the updating of applications; > > Your answers have been very helpful in determining exactly what your > problem is; thank you. > > > but, thank you for your interest in this. > > Perhaps, only the owner/responsible programmer for the application can > > create a Windows installer, > > Given a source distribution, a suitable C++ compiler, and the suspension > of Murphy's Law, anyone should be able to (a) install it on their own > machine (b) make a Windows installer. > > > or perhaps others could assist with this --- > > Perhaps indeed ... > > > this is what my question was about. I would be glad to assist if > > possible :-) > > Suggestions: > > (1) contact the pyODE maintainer and ask, or check out the project's > forums on sourceforge > > (2) if you have a C++ compiler on your Windows box: get the source > distribution, unpack it, ensure that the ODE_BASE in the setup.py points > to your (existing) ODE 0.7 installation, do > ? ? ?\python26\python setup.py install > and stand well back. > > I have just tried this and got a ton of warnings from the compile and 3 > errors from the link: > > build\lib.win32-2.6\ode.pyd : fatal error LNK1120: 3 unresolved externals > error: command '"C:\Program Files\Microsoft Visual Studio > 9.0\VC\BIN\link.exe"' failed with exit status 1120 Update: The problem is evidently with the lib(s) available with ODE 0.7 (or 0.8 -- there are conflicting stories about which one to use with PyODE); they were compiled with VS2003 AFAICT and the complained- of symbols are not supplied by VS2009 which is what I'm using to try and build PyODE for Python 2.6 . The next step would be to try to compile ODE 0.7 or 0.8 with VS9 -- however this would require "project files" for ODE for VS9, and there aren't any on the ODE website; it has only those for VS3 and VS5. As all I know about VS9 is that somewhere inside the ferschlugginer 100 Mb download there's a C compiler that does the right thing when distutils pushes its button, I'm going to have to punt this back to you to follow up on suggestion (1) ... skip asking the pyODE maintainer (last heard saying little time available and he wasn't even /using/ pyODE any more); try asking on the pyODE and ODE mailing- lists has anyone contemplated a Python 2.6 / win32 / VS 2009 build of pyODE. Cheers, John From sjmachin at lexicon.net Mon Jul 20 07:30:12 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 04:30:12 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> Message-ID: <47ab4ab6-d8c8-4997-8963-ac41b00dd10f@y10g2000prg.googlegroups.com> On Jul 20, 9:08?pm, Ben Finney wrote: > casebash writes: > > I have searched this list and found out that Python doesn't have a > > mutable string class (it had an inefficient one, but this was removed > > in 3.0). Are there any libraries outside the core that offer this? > > A mutable string would not (AFAICT) be usefully implementable as a > subclass of the built-in string types. So even if such a type existed, > it would not be useable with all the functionality that works with > strings. > > What is it you're trying to do that makes you search for a mutable > string type? It's likely that a better approach can be found. > OK, I'll bite: where does the Python 3.x bytearray type fit into your taxonomy? At first glance it appears to be mutable and have a fair swag of functionality. From drobinow at gmail.com Mon Jul 20 07:46:12 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 20 Jul 2009 07:46:12 -0400 Subject: On out-of-date Python Applications In-Reply-To: References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> Message-ID: <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> On Mon, Jul 20, 2009 at 7:24 AM, John Machin wrote: ... > The next step would be to try to compile ODE 0.7 or 0.8 with VS9 -- > however this would require "project files" for ODE for VS9, and there > aren't any on the ODE website; it has only those for VS3 and VS5. > The ODE site is a mess. Go to http://www.ode.org/svn.html and click on: Instructions for accessing the repository Scroll down to the section "Building with Premake" (Note that there is no directory "ode/build" -- you want the "build" directory, which contains premake4.exe) I used "premake4 --with-demos --with-tests vs2008" I have successfully compiled ode-0.11.1 using these instructions. I have not yet run the tests or demos or tried to compile PyODE. From wilk at flibuste.net Mon Jul 20 07:56:49 2009 From: wilk at flibuste.net (William Dode) Date: 20 Jul 2009 11:56:49 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: <4a645b81$0$12974$426a74cc@news.free.fr> On 19-07-2009, Mark Dufour wrote: > Hi all, > > I have just released version 0.2 of Shed Skin, an experimental > (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). I just tested it with a litle game, to find the places of horse on a board 5x5. The result is : c 5s gcj 7s java 7s shedskin 8s python + psyco 18s cython avec malloc *int 18s cython 55s avec [] python python 303s (5m3s) -- William Dod? - http://flibuste.net Informaticien Ind?pendant From Lacrima.Maxim at gmail.com Mon Jul 20 08:20:26 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Mon, 20 Jul 2009 05:20:26 -0700 (PDT) Subject: Design question. Message-ID: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Hello! I am newbie in python and I have really simple question, but I need your advice to know how to do best. I need to store a number of dictionaries in certain place. I've decided to store them in a separate module. Like this: dicts.py ----------------------- dict1 = {....} dict2 = {....} dict3 = {....} Then, when I need any dictionary, I can access it: import dicts dicts.dict1 Is it a good practice? Or should I store them as class attributes or anything else? Thanks in advance. With regards, Max (sorry if my English isn't very proper) From magobin at gmail.com Mon Jul 20 08:28:27 2009 From: magobin at gmail.com (Alex) Date: Mon, 20 Jul 2009 05:28:27 -0700 (PDT) Subject: A lot of problem with pygame.mixer module! Message-ID: hi at all, As email object I 'm not be able to run my program when compiled with py2exe. Everythink works fine until I try to make an exe. After that, it crash and in the log: C:\dist\sponge.exe:97: RuntimeWarning: use mixer: DLL load failed: The specified module could not be found. Traceback (most recent call last): File "sponge.py", line 97, in File "pygame\__init__.pyo", line 70, in __getattr__ NotImplementedError: mixer module not available The code about pygame.mixer is only: pygame.mixer.init() music = pygame.mixer.Sound("popcorn.ogg") music.play() in the dist folder there is SDL_mixer.dll so...HOW can I solve this problem ?? thanks in advance Alex Below my setup.py # py2exe setup program from distutils.core import setup import py2exe import pygame from modulefinder import Module #from pygame import mixer import ImageGrab import sys import os import Tkinter import glob, shutil sys.argv.append("py2exe") VERSION = '1.0' AUTHOR_NAME = 'Your Name' AUTHOR_EMAIL = 'your_email at somewhere.com' AUTHOR_URL = "http://www.urlofyourgamesite.com/" PRODUCT_NAME = "Sponge" SCRIPT_MAIN = 'sponge.py' VERSIONSTRING = PRODUCT_NAME + " ALPHA " + VERSION ICONFILE = 'icon.ico' # Remove the build tree on exit automatically REMOVE_BUILD_ON_EXIT = True if os.path.exists('dist/'): shutil.rmtree('dist/') extra_files = [ ("",[ICONFILE,'WinLockDll.dll','popcorn.ogg']), #("data",glob.glob(os.path.join('data','*.dat'))), #("gfx",glob.glob(os.path.join('gfx','*.jpg'))), #("gfx",glob.glob(os.path.join('gfx','*.png'))), ("fonts",glob.glob(os.path.join('fonts','*.ttf'))), ("music",glob.glob(os.path.join('music','*.ogg')))] #("snd",glob.glob(os.path.join('snd','*.wav')))] # List of all modules to automatically exclude from distribution build # This gets rid of extra modules that aren't necessary for proper functioning of app # You should only put things in this list if you know exactly what you DON'T need # This has the benefit of drastically reducing the size of your dist MODULE_EXCLUDES =[ 'email', 'AppKit', 'Foundation', 'bdb', 'difflib', 'tcl', #'Tkinter', #'Tkconstants', 'curses', 'distutils', 'setuptools', 'urllib', 'urllib2', 'urlparse', 'BaseHTTPServer', '_LWPCookieJar', '_MozillaCookieJar', 'ftplib', 'gopherlib', '_ssl', 'htmllib', 'httplib', 'mimetools', 'mimetypes', 'rfc822', 'tty', 'webbrowser', 'socket', 'hashlib', #'base64', 'compiler', 'pydoc' ] INCLUDE_STUFF = ['encodings',"encodings.latin_1"] setup(windows=[ {'script': SCRIPT_MAIN, 'other_resources': [(u"VERSIONTAG",1,VERSIONSTRING)], 'icon_resources': [(1,ICONFILE)]}], options = {"py2exe": { "optimize": 2, "includes": INCLUDE_STUFF, "compressed": 1, "ascii": 1, #"bundle_files": 1, "ignores": ['tcl','AppKit','Numeric','Foundation'], "excludes": MODULE_EXCLUDES} }, name = PRODUCT_NAME, version = VERSION, data_files = extra_files, #zipfile = None, author = AUTHOR_NAME, author_email = AUTHOR_EMAIL, url = AUTHOR_URL) # Create the /save folder for inclusion with the installer #shutil.copytree('save','dist/save') #if os.path.exists('dist/tcl'): shutil.rmtree('dist/tcl') # Remove the build tree if REMOVE_BUILD_ON_EXIT: shutil.rmtree('build/') if os.path.exists('dist/tcl84.dll'): os.unlink('dist/tcl84.dll') if os.path.exists('dist/tk84.dll'): os.unlink('dist/tk84.dll') From vippstar at gmail.com Mon Jul 20 08:29:40 2009 From: vippstar at gmail.com (vippstar) Date: Mon, 20 Jul 2009 05:29:40 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: On Jul 20, 9:13?am, Paul Rubin wrote: > Steven D'Aprano writes: > > Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing > > to do. It's the right thing to do if you're doing integer maths. > > I wonder whether 2/3 => ValueError is preferable. Not all software wants this. It shouldn't be part of the language but rather part of your code if you need such a feature. (for instance, to distinguish between 2/3 and divisions with 0 dividend). From deets at nospam.web.de Mon Jul 20 08:31:15 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 20 Jul 2009 14:31:15 +0200 Subject: Design question. References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Message-ID: <7cj63mF285spnU1@mid.uni-berlin.de> Lacrima wrote: > Hello! > > I am newbie in python and I have really simple question, but I need > your advice to know how to do best. > I need to store a number of dictionaries in certain place. I've > decided to store them in a separate module. > Like this: > dicts.py > ----------------------- > dict1 = {....} > dict2 = {....} > dict3 = {....} > > Then, when I need any dictionary, I can access it: > import dicts > dicts.dict1 > > Is it a good practice? Or should I store them as class attributes or > anything else? It's perfectly fine to use dictionaries as module-level objects for storing e.g. configuration-data. OTOH it's also fine to do so as class-attributes. Choosing one over the other has a lot to do with the actual problem you are working on. Diez From ndbecker2 at gmail.com Mon Jul 20 08:39:28 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 20 Jul 2009 08:39:28 -0400 Subject: proposal: add setresuid() system call to python References: <20090717200141.GI6065@subspacefield.org> <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> Message-ID: Mahmoud Abdelkader wrote: > Why don't you write a python extension module? This is a perfect > opportunity for that. > I think having a module just for one system call is a bit silly. Why not add to os module? From miheavner at gmail.com Mon Jul 20 08:41:02 2009 From: miheavner at gmail.com (mheavner) Date: Mon, 20 Jul 2009 05:41:02 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: Piet, The situation is 1a of your listed options, however my issue was solved. I was stopping the subprocesses from consuming more data at each iteration which led to the data being lost since the subprocess worker function would then end - I now keep them alive across iterations. Thanks for your help, I'm new to the multiprocessing module and this was very helpful! On Jul 17, 4:26?am, Piet van Oostrum wrote: > There is stil something not clear in your description. > > >m> I'm using multiprocessing to spawn several subprocesses, each of which > >m> uses a very large data structure (making it impractical to pass it via > >m> pipes / pickling). I need to allocate this structure once when the > >m> process is created and have it remain in memory for the duration of > >m> the process. > > I have read this as that every subprocess has its own large > data structure and that there is no connection between these. > > But seeing where the discussion is going I guess there might be > different interpretations. So can you enlighten us how the situation is? > > 1. Each subprocess has a copy of a data structure that is prepared by the > ? ?master process. Therefore you want it to be passed by the fork > ? ?1a. the data structure is constant i.e. the subprocess doesn't change it > ? ?1b. the subprocess makes changes in its copy > 2. Each subprocess has a seperate data structure not equal to the others > 3. Something else. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org From Lacrima.Maxim at gmail.com Mon Jul 20 08:44:22 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Mon, 20 Jul 2009 05:44:22 -0700 (PDT) Subject: Design question. References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> <7cj63mF285spnU1@mid.uni-berlin.de> Message-ID: <825d0aae-3147-4f81-8c82-fa86e4b777df@c29g2000yqd.googlegroups.com> On Jul 20, 3:31?pm, "Diez B. Roggisch" wrote: > Lacrima wrote: > > Hello! > > > I am newbie in python and I have really simple question, but I need > > your advice to know how to do best. > > I need to store a number of dictionaries in certain place. I've > > decided to store them in a separate module. > > Like this: > > dicts.py > > ----------------------- > > dict1 = {....} > > dict2 = {....} > > dict3 = {....} > > > Then, when I need any dictionary, I can access it: > > import dicts > > dicts.dict1 > > > Is it a good practice? Or should I store them as class attributes or > > anything else? > > It's perfectly fine to use dictionaries as module-level objects for storing > e.g. configuration-data. > > OTOH it's also fine to do so as class-attributes. Choosing one over the > other has a lot to do with the actual problem you are working on. > > Diez Hi! Thank you very much for your so soon reply! With regards, Max From jeanmichel at sequans.com Mon Jul 20 09:05:13 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 20 Jul 2009 15:05:13 +0200 Subject: Design question. In-Reply-To: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Message-ID: <4A646B89.7060103@sequans.com> Lacrima wrote: > Hello! > > I am newbie in python and I have really simple question, but I need > your advice to know how to do best. > I need to store a number of dictionaries in certain place. I've > decided to store them in a separate module. > Like this: > dicts.py > ----------------------- > dict1 = {....} > dict2 = {....} > dict3 = {....} > > Then, when I need any dictionary, I can access it: > import dicts > dicts.dict1 > > Is it a good practice? Or should I store them as class attributes or > anything else? > > Thanks in advance. > > With regards, Max > (sorry if my English isn't very proper) > Defining dict as a module attribute ic correct, but try to answer the following question: Is dict1 an attribute/property/declension of the object/entity defined by the module dicts ? If yes, then your design is correct. An correct example: fruits.py ------------ apple = {} banana = {} An incorrect one: fruit.py ----------- apple={} bicycle={} Basically, the rule is very straightforward, set your dict as a module attribute only if its one of its attribute (very nice paraphrase !) Most of people (including me) tend to have a module, where they put everything they have no idea about their location. This is a bad habit and result from a uncontrolled/undocumented design. Usually documenting objects in such modules is really painful. Your proposal is fine from a python syntax point of view. I can't tell of your design with names like (dicts.py and dict1,dict2) hoping you do not intend to name them that way. JM From deets at nospam.web.de Mon Jul 20 09:10:17 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 20 Jul 2009 15:10:17 +0200 Subject: proposal: add setresuid() system call to python References: <20090717200141.GI6065@subspacefield.org> <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> Message-ID: <7cj89qF27mfrjU1@mid.uni-berlin.de> Neal Becker wrote: again with his notorious gmane.comp.python.general group that doesn't work for any decent news-reader..... > Mahmoud Abdelkader wrote: > >> Why don't you write a python extension module? This is a perfect >> opportunity for that. >> > I think having a module just for one system call is a bit silly. Why not > add to os module? While I do agree that it's an omission, it is easy enough to get it yourself using ctypes: from ctypes import * from ctypes.util import find_library clib = CDLL(find_library("c")) # this might be different on 64bit __uid_t = c_uint _setresuid = clib.setresuid _setresuid.argtypes = [__uid_t, __uid_t, __uid_t] _setresuid.restype = c_int def setresuid(ruid, euid, suid): return _setresuid(__uid_t(ruid), __uid_t(euid), __uid_t(suid)) print setresuid(1000, 1000, 1000) # returns 0 for me print setresuid(1001, 1001, 1001) # fails. Diez From stefan_ml at behnel.de Mon Jul 20 09:22:02 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 20 Jul 2009 15:22:02 +0200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a645b81$0$12974$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <4a646f7b$0$31869$9b4e6d93@newsspool3.arcor-online.net> William Dode wrote: > On 19-07-2009, Mark Dufour wrote: >> I have just released version 0.2 of Shed Skin, an experimental >> (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). > > I just tested it with a litle game, to find the places of horse on > a board 5x5. The result is : > > [...] > shedskin 8s > python + psyco 18s > cython avec malloc *int 18s > cython 55s avec [] python > python 303s (5m3s) Note that both Psyco and Cython make a lot less assumptions about Python code than Shed Skin does. Psyco has the advantage of just needing to jump in when it finds out that it can help, so it's the most broadly compatible of the three. But Cython also supports quite a large corpus of dynamic Python code by now. Shed Skin has a lot of restrictions, many of which are by design. It's not intended to compile dynamic code, and I think that's a good thing, because that's what makes it fast for the code that it supports. Getting the same speed in Cython requires a bit more explicit typing, simply because Cython does not assume these restrictions. I think that all three have their raison d'?tre, and it currently looks like all three are there to stay and to keep growing better. And I'm also happy to read that some optimisations jump from one to the other. ;) Stefan From Lacrima.Maxim at gmail.com Mon Jul 20 09:42:23 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Mon, 20 Jul 2009 06:42:23 -0700 (PDT) Subject: Design question. References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Message-ID: <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> On Jul 20, 4:05?pm, Jean-Michel Pichavant wrote: > Lacrima wrote: > > Hello! > > > I am newbie in python and I have really simple question, but I need > > your advice to know how to do best. > > I need to store a number of dictionaries in certain place. I've > > decided to store them in a separate module. > > Like this: > > dicts.py > > ----------------------- > > dict1 = {....} > > dict2 = {....} > > dict3 = {....} > > > Then, when I need any dictionary, I can access it: > > import dicts > > dicts.dict1 > > > Is it a good practice? Or should I store them as class attributes or > > anything else? > > > Thanks in advance. > > > With regards, Max > > (sorry if my English isn't very proper) > > Defining dict as a module attribute ic correct, but try to answer the > following question: > > Is dict1 an attribute/property/declension of the object/entity defined > by the module dicts ? > If yes, then your design is correct. > > An correct example: > fruits.py > ------------ > apple = {} > banana = {} > > An incorrect one: > fruit.py > ----------- > apple={} > bicycle={} > > Basically, the rule is very straightforward, set your dict as a module > attribute only if its one of its attribute (very nice paraphrase !) > Most of people (including me) tend to have a ?module, where they put > everything they have no idea about their location. This is a bad habit > and result from a uncontrolled/undocumented design. Usually documenting > objects in such modules is really painful. > > Your proposal is fine from a python syntax point of view. I can't tell > of your design with names like (dicts.py and dict1,dict2) hoping you do > not intend to name them that way. > > JM Hi, Jean-Michel! Thanks for your answer. I am not going to have names like dicts.py and dict1,dict2. That was just example. I wanted to know if it is correct to have dictionaries on a module level. Now I know that this is correct. I want to collect in one module a number of dictionaries, every of which describes a separate web service. And my function in another module will import required dictionary, depending on what web service should be used. Thanks again for the help. With regards, Max. (sorry if my English isn't very proper) From gerard.blais at gmail.com Mon Jul 20 09:47:54 2009 From: gerard.blais at gmail.com (Gerry) Date: Mon, 20 Jul 2009 06:47:54 -0700 (PDT) Subject: win32api install problem Message-ID: I'm running Python 2.6 under XP. I've installed Windows 32 extensions for Python 2.6 version 1.4 (pywin32-214.win32-py2.6.exe). But If I try to import win32api, I get: File "C:\python_projects\euler\driveletters.py", line 1, in import win32api ImportError: DLL load failed: The specified module could not be found. \Python26\Lib\site-packages has: 03/23/2009 08:35 AM win32 07/20/2009 09:08 AM win32com 02/18/2009 01:21 PM win32comext Can anyone offer a suggestion? Thanks, Gerry From mail at timgolden.me.uk Mon Jul 20 09:57:56 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 20 Jul 2009 14:57:56 +0100 Subject: win32api install problem In-Reply-To: References: Message-ID: <4A6477E4.3030808@timgolden.me.uk> Gerry wrote: > I'm running Python 2.6 under XP. > > I've installed Windows 32 extensions for Python 2.6 version 1.4 > (pywin32-214.win32-py2.6.exe). > > But If I try to import win32api, I get: > > File "C:\python_projects\euler\driveletters.py", line 1, in > import win32api > ImportError: DLL load failed: The specified module could not be found. Used to be you'd get this error if you installed as a non-admin user. Don't know if that's still an issue. Possibility? TJG From dtgeadamo at yahoo.com Mon Jul 20 09:57:57 2009 From: dtgeadamo at yahoo.com (mistersexy) Date: Mon, 20 Jul 2009 06:57:57 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account Message-ID: I am trying to create a Windows service in Python using pywin32. I do not want this service to run under a user account. I want this service to be able to run as a LocalService, NetworkService and the like. How do I specify this using the win32 library? Thanks, everyone. From steve at REMOVE-THIS-cybersource.com.au Mon Jul 20 10:00:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 20 Jul 2009 14:00:30 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: <02746997$0$5185$c3e8da3@news.astraweb.com> On Mon, 20 Jul 2009 09:34:24 +0000, Sion Arrowsmith wrote: > Terry Reedy wrote: >>Sion Arrowsmith wrote: >>> Jack Diederich wrote: >>>> It isn't an OrderedDict thing, it is a comparison thing. Two regular >>>> dicts also raise an error if you try to LT them. >>> Python 2.5.2 >>>>>> d1 = dict((str(i), i) for i in range (10)) d2 = dict((str(i), i) >>>>>> for i in range (20)) d1 < d2 >>> True >>Try reversing the definitions of d1 and d2. The dicts are probably being >>compared by id (address), which is the 2.x CPython default. > > Like this? > >>>> d1 = dict((str(i), i) for i in range (20)) >>>> d2 = dict((str(i), i) for i in range (10)) >>>> d1 < d2 > False >>>> id(d1) < id(d2) > True > > I didn't know that comparison for anything other than equality defaulted > to using id. That really is rather broken, and I'm glad 3.0 fixed it. I don't think comparisons other than equality use id. That would be rather insane. If anyone can demonstrate such comparisons in a built-in or standard library class, I'd like to see it. For the record, dicts have been comparable with < and > since at least Python 1.5: $ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> >>> {1: 'a', 2: 'b'} < {2: 'b', 3: 'a'} 1 Reading the docs: http://docs.python.org/library/stdtypes.html#comparisons http://docs.python.org/library/stdtypes.html#mapping-types-dict I can only suggest that dicts compare in an arbitrary but consistent fashion. What that is based on, I don't know, but based on some very limited testing, I'd guess it is like this: If the two dicts have unequal length, the longer dict compares greater than the shorter dict. If the two dicts are equal in length, (key, item) pairs are compared, probably in lexicographic order, because the order of insertion of keys doesn't appear to matter. -- Steven From mail at timgolden.me.uk Mon Jul 20 10:03:14 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 20 Jul 2009 15:03:14 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: References: Message-ID: <4A647922.4080908@timgolden.me.uk> mistersexy wrote: > I am trying to create a Windows service in Python using pywin32. I do > not want this service to run under a user account. I want this service > to be able to run as a LocalService, NetworkService and the like. How > do I specify this using the win32 library? Thanks, everyone. When you "install" the service, using the HandleCommandLine option, specify --username= and --password options. TJG From nyamatongwe+thunder at gmail.com Mon Jul 20 10:05:33 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Mon, 20 Jul 2009 14:05:33 GMT Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: casebash: > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? I wrote a gap buffer implementation for Python 2.5 allowing character, unicode character and integer elements. http://code.google.com/p/gapbuffer/ Its not seen much use or any maintenance so is unlikely to work with Python 3.x. Neil From hniksic at xemacs.org Mon Jul 20 10:10:35 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Mon, 20 Jul 2009 16:10:35 +0200 Subject: proposal: add setresuid() system call to python References: <20090717200141.GI6065@subspacefield.org> <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> <7cj89qF27mfrjU1@mid.uni-berlin.de> Message-ID: <877hy38mxw.fsf@busola.homelinux.net> "Diez B. Roggisch" writes: To emulate the os-module-type calls, it's better to raise exceptions than return negative values: > def setresuid(ruid, euid, suid): > return _setresuid(__uid_t(ruid), __uid_t(euid), __uid_t(suid)) def setresuid(ruid, euid, suid): res = _setresuid(__uid_t(ruid), __uid_t(euid), __uid_t(suid)) if res < 0: raise OSError('[Errno %d] %s' % (os.errno, errno.strerror(os.errno))) From steve at REMOVE-THIS-cybersource.com.au Mon Jul 20 10:21:15 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 20 Jul 2009 14:21:15 GMT Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> Message-ID: <02746e73$0$5185$c3e8da3@news.astraweb.com> On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote: > casebash writes: > >> I have searched this list and found out that Python doesn't have a >> mutable string class (it had an inefficient one, but this was removed >> in 3.0). Are there any libraries outside the core that offer this? > > A mutable string would not (AFAICT) be usefully implementable as a > subclass of the built-in string types. So even if such a type existed, > it would not be useable with all the functionality that works with > strings. If applications ignore duck-typing and do isinstance(value, str), it's arguably the application and not the value that is broken. Besides, with the new __isinstance__ method, surely such a mutable string could claim to be an instance of string without needing to inherit from string? > What is it you're trying to do that makes you search for a mutable > string type? It's likely that a better approach can be found. When dealing with very large strings, it is wasteful to have to duplicate the entire string just to mutate a single character. However, when dealing with very large strings, it's arguably better to use the rope data structure instead. http://en.wikipedia.org/wiki/Rope_(computer_science) -- Steven From invalid at invalid Mon Jul 20 10:59:17 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 20 Jul 2009 09:59:17 -0500 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> Message-ID: On 2009-07-20, Tim Daneliuk wrote: >>> In fact, picking a computer language is the most important >>> discussion in Computer Science and eclipses even P=NP? in >>> significance. I sure hope we can keep this thread going for a >>> few months. >> >> Please feel free to extend this flame-war along for a few >> months on comp.lang.lisp. Not here. > > Uh Carl ... are you familiar with the concept of mocking humor? Irony on Usenet: always a bit of a gamble... -- Grant Edwards grante Yow! Vote for ME -- I'm at well-tapered, half-cocked, visi.com ill-conceived and TAX-DEFERRED! From craig1.boyd at citi.com Mon Jul 20 11:09:47 2009 From: craig1.boyd at citi.com (Boyd, Craig1 ) Date: Mon, 20 Jul 2009 10:09:47 -0500 Subject: Auto-generate GUI from a database table structure Message-ID: <55660F9659966544BBA3AE14CE5D278D01243B8E04@exgtmb08.nam.nsroot.net> Hello All, I am REALLY new to python and still trying to figure a lot of this stuff out. I am trying to write a couple screens to update three or four database tables on Oracle 10g and I was wondering if there was a way to automatically generate some kind of GUI shell based on the tables that I could then fill in with the logic / functionality I need. I have coded and run some of the python / tcl examples that I could find so I kind of get that, but it appears to be a bit labor intensive. Is there an easier way? Thanks, Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: From tvashtar at gmail.com Mon Jul 20 11:11:34 2009 From: tvashtar at gmail.com (tvashtar) Date: Mon, 20 Jul 2009 08:11:34 -0700 (PDT) Subject: Problem when applying Patch from issue1424152 to get https over authenticating proxies working with urllib2 in Python 2.5 Message-ID: <8fe2ad78-0bd4-40d8-909c-1d2784441e3c@s31g2000yqs.googlegroups.com> Hi, I'm trying to get https requests working through an authenticating proxy with urllib2 in Python 2.5, I'm aware that this isn't supported "out of the box", so applied the patch http://bugs.python.org/file9753/http-tunnel-urllib linked from http://bugs.python.org/issue1424152 , my baseline test program is the following: p = "user:pass at proxy:port" proxy_handler = urllib2.ProxyHandler({"http": p, "https": p}) urllib2.install_opener( urllib2.build_opener( urllib2.HTTPHandler, urllib2.HTTPSHandler, proxy_handler)) request = urllib2.Request( "https://groups.google.com") response = urllib2.urlopen(request) Unfortunately this doesn't work, the call stack being: Traceback (most recent call last): File "", line 1, in File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 121, in urlopen return _opener.open(url, data) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 374, in open response = self._open(req, data) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 392, in _open '_open', req) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 353, in _call_chain result = func(*args) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 1108, in https_open return self.do_open(httplib.HTTPSConnection, req) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 1075, in do_open raise URLError(err) urllib2.URLError: Does anyone know why I might be hitting this issue? Any help is greatly appreciated, thanks From nikekoo at gmail.com Mon Jul 20 11:42:15 2009 From: nikekoo at gmail.com (Nike) Date: Mon, 20 Jul 2009 08:42:15 -0700 (PDT) Subject: Problem when applying Patch from issue1424152 to get https over authenticating proxies working with urllib2 in Python 2.5 References: <8fe2ad78-0bd4-40d8-909c-1d2784441e3c@s31g2000yqs.googlegroups.com> Message-ID: <58f2558c-33fe-4ee0-9a0a-2563374be47c@x6g2000prc.googlegroups.com> On Jul 20, 11:11?pm, tvashtar wrote: > Hi, > I'm trying to get https requests working through an authenticating > proxy with urllib2 in Python 2.5, I'm aware that this isn't supported > "out of the box", so applied the patchhttp://bugs.python.org/file9753/http-tunnel-urllib > linked fromhttp://bugs.python.org/issue1424152, my baseline test > program is the following: > > p = "user:pass at proxy:port" > proxy_handler = urllib2.ProxyHandler({"http": p, "https": p}) > urllib2.install_opener( urllib2.build_opener( urllib2.HTTPHandler, > > urllib2.HTTPSHandler, > > proxy_handler)) > > request = urllib2.Request( "https://groups.google.com") > response = urllib2.urlopen(request) > > Unfortunately this doesn't work, the call stack being: > > Traceback (most recent call last): > ? File "", line 1, in > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 121, in urlopen > ? ? return _opener.open(url, data) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 374, in open > ? ? response = self._open(req, data) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 392, in _open > ? ? '_open', req) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 353, in _call_chain > ? ? result = func(*args) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 1108, in https_open > ? ? return self.do_open(httplib.HTTPSConnection, req) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 1075, in do_open > ? ? raise URLError(err) > urllib2.URLError: routines:SSL23_GET_SERVER_HELLO:unknown protocol')> > > Does anyone know why I might be hitting this issue? > Any help is greatly appreciated, > thanks hi! It's looks like a ssl error . Under the following step to help u : 1. takes a simple code to confirm your pupose without ssl protocol. 2. to confirm python version and extended libs work well 3. to confirm ssl work well. goog luck! nikekoo From hyugaricdeau at gmail.com Mon Jul 20 11:52:39 2009 From: hyugaricdeau at gmail.com (Hyuga) Date: Mon, 20 Jul 2009 08:52:39 -0700 (PDT) Subject: are user defined classes hashable? References: Message-ID: <373d6c69-6965-4a88-bdd2-8028ef850bf8@k6g2000yqn.googlegroups.com> On Jul 19, 11:39?am, Nicolas Dandrimont wrote: > * Alan G Isaac [2009-07-19 14:46:12 +0000]: > > > Again, my question is about the class not its instances, > > but still, checking as you suggest gives the same answer. > > That's what I get for answering before my coffee! Regardless, Nicolas's example can be applied to the class too: >>> class Foo(object): pass >>> hash(Foo) 11443104 >>> id(Foo) 11443104 class objects are just objects of type 'type'. From dtgeadamo at yahoo.com Mon Jul 20 11:55:35 2009 From: dtgeadamo at yahoo.com (mistersexy) Date: Mon, 20 Jul 2009 08:55:35 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: Message-ID: On Jul 20, 3:03?pm, Tim Golden wrote: > mistersexy wrote: > > I am trying to create a Windows service in Python using pywin32. I do > > not want this service to run under a user account. I want this service > > to be able to run as a LocalService, NetworkService and the like. How > > do I specify this using the win32 library? Thanks, everyone. > > When you "install" the service, using the HandleCommandLine > option, specify --username= and --password options. > > TJG That's exactly my point. I do not want to have to specify username and password options. For instance, when creating a Windows Service in .NET, it is possible to specify that the service should run using the LocalService or NetworkService account. Doing this, you would not need to specify username and password options. Is there a way to achieve this in Python? From aahz at pythoncraft.com Mon Jul 20 12:00:33 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 09:00:33 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: In article , tkouts wrote: > >I'm pleased to announce the new version of Porcupine Web Application >Server, a Python based framework that provides front-end and back-end >technologies for building modern data-centric Web 2.0 applications. > > [...] > >QuiX, the server's integrated JavaScript toolkit, has reached the >major milestone of supporting all the popular browsers including >Opera, Safari 4 and IE8. [...] Out of curiosity, are there any JavaScript toolkits that generate code that degrades gracefully when JavaScript is disabled? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From vs at it.uu.se Mon Jul 20 12:09:42 2009 From: vs at it.uu.se (Virgil Stokes) Date: Mon, 20 Jul 2009 18:09:42 +0200 Subject: On out-of-date Python Applications In-Reply-To: <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> Message-ID: <4A6496C6.4040606@it.uu.se> An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Mon Jul 20 12:14:31 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 20 Jul 2009 17:14:31 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: References: Message-ID: <4A6497E7.7090908@timgolden.me.uk> mistersexy wrote: > On Jul 20, 3:03 pm, Tim Golden wrote: >> mistersexy wrote: >>> I am trying to create a Windows service in Python using pywin32. I do >>> not want this service to run under a user account. I want this service >>> to be able to run as a LocalService, NetworkService and the like. How >>> do I specify this using the win32 library? Thanks, everyone. >> When you "install" the service, using the HandleCommandLine >> option, specify --username= and --password options. >> >> TJG > > That's exactly my point. I do not want to have to specify username and > password options. For instance, when creating a Windows Service > in .NET, it is possible to specify that the service should run using > the LocalService or NetworkService account. Doing this, you would not > need to specify username and password options. Is there a way to > achieve this in Python? Sorry, I misread: I mentally removed the "not" in your 'I do not want this service to run under a user account' and reinserted it further on! By default, the service will run as LocalSystem: you only specify a username to override that default. The value in Username is passed straight through to the CreateService Win32 API, and the docs for that: http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx say: """ lpServiceStartName [in, optional] The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName. If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account. If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account. A shared process can run as any user. If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system. A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide. Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2. """ So, although I haven't tried it, it looks as though you can pass "LocalService" or "NetworkService" and so on if you want to override the default LocalSystem but don't want to specify a username/password. TJG From ebonak at hotmail.com Mon Jul 20 12:22:47 2009 From: ebonak at hotmail.com (Esmail) Date: Mon, 20 Jul 2009 12:22:47 -0400 Subject: comments? storing a function in an object Message-ID: Hello all, I am trying to store a function and some associated information in an object so that I can later have series of functions in a list that I can evaluate one at a time. Right now I am only storing the function itself, the number of arguments it expects and its string representation. I may add other information such as the acceptable range for the parameters or other characteristics such as maximum or minimum. I wonder if anyone could comment on my implementation and offer suggestions or constructive criticisms? The 'loading' of the functions is a bit tedious and of course care has to be taken to make sure the string representation corresponds to the actual function computed. It would be nice if there was an automatic way to convert the function to its string representation. Comments or problems with the approach I have taken? Thanks, Esmail -- #!/usr/bin/env python # # store and represent functions # import math class FunctionException(Exception): """ custom exception """ def __init__(self, value): self.parameter = value def __str__(self): return repr(self.parameter) class Function(object): """ class to represent a function """ def __init__(self, fn, fn_str, num_vars): """ the function, its string representation, and the number of variables """ self.fn = fn self.fn_str = fn_str self.num_vars = num_vars def eval_fn(self, variables): """ size of variables should equal num_vars .. else problem """ if len(variables) == self.num_vars: result = self.fn(*variables) return result else: raise FunctionException('invalid number of args provided - '+ 'received %d, expected %d' %(len(variables), self.num_vars)) def str(self): """ return string representation of function """ return self.fn_str def funct1(x): ''' small test function ''' return x * x def funct2(x, y): ''' small test function ''' return x + y def funct3(x): ''' small test function ''' return 1000 + (x*x + x) * math.cos(x) def main(): """ main method """ print 'in main' fn1 = Function(funct1, 'x * x', 1) fn2 = Function(funct2, 'x + y', 2) fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) for i in range(-10, 10): try: print 'f(', [i],') =', print fn3.str(), ' => ', print fn3.eval_fn([i]) except FunctionException, (ex): print ex.parameter if __name__ == '__main__': main() From dtgeadamo at yahoo.com Mon Jul 20 12:25:44 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Mon, 20 Jul 2009 09:25:44 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: Message-ID: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> On Jul 20, 5:14?pm, Tim Golden wrote: > mistersexy wrote: > > On Jul 20, 3:03 pm, Tim Golden wrote: > >> mistersexy wrote: > >>> I am trying to create a Windows service in Python using pywin32. I do > >>> not want this service to run under a user account. I want this service > >>> to be able to run as a LocalService, NetworkService and the like. How > >>> do I specify this using the win32 library? Thanks, everyone. > >> When you "install" the service, using the HandleCommandLine > >> option, specify --username= and --password options. > > >> TJG > > > That's exactly my point. I do not want to have to specify username and > > password options. For instance, when creating a Windows Service > > in .NET, it is possible to specify that the service should run using > > the LocalService or NetworkService account. Doing this, you would not > > need to specify username and password options. Is there a way to > > achieve this in Python? > > Sorry, I misread: I mentally removed the "not" in your 'I do not want > this service to run under a user account' and reinserted it > further on! > > By default, the service will run as LocalSystem: you > only specify a username to override that default. The value > in Username is passed straight through to the CreateService > Win32 API, and the docs for that: > > ?http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx > > say: > > """ > lpServiceStartName [in, optional] > > ? ? The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName. > > ? ? If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account. > > ? ? If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account. > > ? ? A shared process can run as any user. > > ? ? If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system. > > ? ? A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide. > > ? ? ? ? Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: ?Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2. > """ > > So, although I haven't tried it, it looks as though you can pass > "LocalService" or "NetworkService" and so on if you want to > override the default LocalSystem but don't want to specify a > username/password. > > TJG I'll try this stuff. Thanks a million...I'll let everyone know how it goes. From bearophileHUGS at lycos.com Mon Jul 20 12:26:08 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Mon, 20 Jul 2009 09:26:08 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> William Dode': > I just tested it with a litle game, to find the places of horse on > a board 5x5. The result is : > > c 5s > gcj 7s > java 7s > shedskin 8s > python + psyco 18s > cython avec malloc *int 18s > cython 55s avec [] python > python 303s (5m3s) Nice timings, can you please show me the Python, Java and C code versions? I may do more tests. The purpose of all those "example programs" in ShedSkin is to find bugs and to find details to speedup. Bye, bearophile From phillip.oldham at gmail.com Mon Jul 20 12:27:05 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Mon, 20 Jul 2009 17:27:05 +0100 Subject: Help understanding the decisions *behind* python? Message-ID: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> My colleagues and I have been working with python for around 6 months now, and while we love a lot of what python has done for us and what it enables us to do some of the decisions behind such certain data-types and their related methods baffle us slightly (when compared to the decisions made in other, similarly powerful languages). Specifically the "differences" between lists and tuples have us confused and have caused many "discussions" in the office. We understand that lists are mutable and tuples are not, but we're a little lost as to why the two were kept separate from the start. They both perform a very similar job as far as we can tell. Consider the following: >>> x = [2,1,3] >>> x.sort() >>> print x [1, 2, 3] Now, if the sort operations were unable to affect the original structure of the list (as in JavaScript) you'd effectively have a tuple which you could add/remove from, and the example above would look more like: >>> x = [2,1,3] >>> print x.sort() [1, 2, 3] >>> print x [2,1,3] This make a lot more sense to us, and follows the convention from other languages. It would also mean chaining methods to manipulate lists would be easier: >>> x = [2,1,3] >>> print x.sort()[0] 3 >>> print x [2,1,3] We often find we need to do manipulations like the above without changing the order of the original list, and languages like JS allow this. We can't work out how to do this in python though, other than duplicating the list, sorting, reversing, then discarding. We're not looking to start any arguments or religious wars and we're not asking that python be changed into something its not. We'd simply like to understand the decision behind the lists and tuple structures. We feel that in not "getting" the difference between the two types we may be missing out on using these data structures to their full potential. From tycho at tycho.ws Mon Jul 20 12:43:16 2009 From: tycho at tycho.ws (Tycho Andersen) Date: Mon, 20 Jul 2009 11:43:16 -0500 Subject: Help understanding the decisions *behind* python? In-Reply-To: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: <49b3a7400907200943s2ed382c7qa0a38aa67db79885@mail.gmail.com> On Mon, Jul 20, 2009 at 11:27 AM, Phillip B Oldham wrote: > > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. > I have no idea about why the design decisions were made. You might take a look at the sorted() function: http://docs.python.org/library/functions.html#sorted It will do what you want. \t -- http://tycho.ws From http Mon Jul 20 12:50:56 2009 From: http (Paul Rubin) Date: 20 Jul 2009 09:50:56 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: <7xljmjguxb.fsf@ruckus.brouhaha.com> vippstar writes: > > I wonder whether 2/3 => ValueError is preferable. > > Not all software wants this. It shouldn't be part of the language but > rather part of your code if you need such a feature. (for instance, to > distinguish between 2/3 and divisions with 0 dividend). I don't see how to implement such a thing in my code, if I believe that the ring of integers doesn't have any concept of division and so attempts to divide integers should be treated as errors. Yes of course the present / operator is useful, but I could do just as well with the divmod function which I think is more explicit. From marcusw at cox.net Mon Jul 20 13:06:39 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 13:06:39 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <7x4ot7rif8.fsf@ruckus.brouhaha.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: On 7/20/2009 2:13 AM, Paul Rubin wrote: > Steven D'Aprano writes: >> Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing >> to do. It's the right thing to do if you're doing integer maths. > > I wonder whether 2/3 => ValueError is preferable. Not for me :( From duncan.booth at invalid.invalid Mon Jul 20 13:08:38 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 20 Jul 2009 17:08:38 GMT Subject: Help understanding the decisions *behind* python? References: Message-ID: Phillip B Oldham wrote: > This make a lot more sense to us, and follows the convention from > other languages. It would also mean chaining methods to manipulate > lists would be easier: > >>>> x = [2,1,3] >>>> print x.sort()[0] > 3 >>>> print x > [2,1,3] You already have a way to do what you want: >>> x = [2,1,3] >>> print sorted(x)[0] 3 >>> print x [2,1,3] as a bonus you can use 'sorted' to sort any sequence including generators or tuples, but the result will always be a list: if it was a list method then you would have to convert the sequence to a list first. The main reason why you need both lists and tuples is that because a tuple of immutable objects is itself immutable you can use it as a dictionary key. You can't use a list as a dictionary key because if something were to mutate a key the dictionary structure would behave very strangely indeed. The types 'set' and 'frozenset' both exist for the same reason. From darcy at druid.net Mon Jul 20 13:08:56 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Mon, 20 Jul 2009 13:08:56 -0400 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) In-Reply-To: References: Message-ID: <20090720130856.fdee4c6d.darcy@druid.net> On 20 Jul 2009 09:00:33 -0700 aahz at pythoncraft.com (Aahz) wrote: > Out of curiosity, are there any JavaScript toolkits that generate code > that degrades gracefully when JavaScript is disabled? I understand what you want but I can't see how a toolkit can do that. How do you program "graceful?" It seems pretty application specific. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From marcusw at cox.net Mon Jul 20 13:11:20 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 13:11:20 -0400 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: On 7/20/2009 5:34 AM, Stef Mientki wrote: > thanks Marcus, > > Marcus Wanner wrote: >> On 7/19/2009 4:15 PM, Stef Mientki wrote: >>> hello, >>> >>> I'm using Scintilla as a wxPython widget with great pleasure. >>> I now have an application where I want to make notes during a >>> conversation, >>> but also want to record the speech during that conversation. >>> I'm using Scintilla as a wxPython widget for editing and PyAudio for >>> the speech recording, >>> until so far everything works fine. >>> >>> Here the problem part: >>> I need to synchronize the typed text with the sound during playback. >>> So if I click somewhere in the sound recording, >>> the line of text, typed that moment should be highlighted. >>> And vise versa, if the cursor in the text is moved and some special >>> key is pressed, >>> the sound starting 10 or 20seconds earlier should be playbacked. >>> >>> I though of adding bookmarks (because these are fixed in the text), >>> and keep a list of bookmarks and sound pointers. >>> This idea should work, but there are only 31 bookmarks. >>> >>> Any other suggestions ? >>> >>> thanks, >>> Stef Mientki >> That sounds like a very specialized type of thing, > Well from an application point of view, > with the current netbooks, > this looks like a perfect tool for any conversation or meeting. >> which only the few people with experience with wxPython, PyAudio, and >> Scintilla could help you with... >> > I was afraid of that too, so I dropped the question in several places, > and the writer of Scintilla himself came with the perfect answer. > > cheers,Stef >> But you might try having a dictionary with notes and associated times, >> or just give each note a four-digit ID number at the beginning of it >> when it's entered and use that in the dictionary (to keep keys >> shorter). Or you could just do a little hack and increase the number >> of bookmarks allowed (seeing as source is available) :p >> >> Marcus > Glad you got a good answer from somebody. Sounds like an interesting project. About when would this be headed for a release? Could you post a link to a googlecode or sourceforge project or something so I can follow and/or help with development? Marcus From motoom at xs4all.nl Mon Jul 20 13:17:19 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Mon, 20 Jul 2009 19:17:19 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: <4A64A69F.2030400@xs4all.nl> Phillip wrote: > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. Yes, but because of their immutability you can use tuples as dictionary keys (only if they contain immutable objects). Lists can't be used as dictionary keys. Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From gerard.blais at gmail.com Mon Jul 20 13:28:09 2009 From: gerard.blais at gmail.com (MCIPERF) Date: Mon, 20 Jul 2009 10:28:09 -0700 (PDT) Subject: win32api install problem References: Message-ID: On Jul 20, 9:57?am, Tim Golden wrote: > Gerry wrote: > > I'm running Python 2.6 under XP. > > > I've installed Windows 32 extensions for Python 2.6 version 1.4 > > (pywin32-214.win32-py2.6.exe). > > > But If I try to import win32api, I get: > > > ? File "C:\python_projects\euler\driveletters.py", line 1, in > > ? ? import win32api > > ImportError: DLL load failed: The specified module could not be found. > > Used to be you'd get this error if you installed as a > non-admin user. Don't know if that's still an issue. > Possibility? > > TJG Not a possibility (afaict) -- I am an administrator, according to the control panel. From marcusw at cox.net Mon Jul 20 13:42:55 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 13:42:55 -0400 Subject: Design question. In-Reply-To: <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> Message-ID: On 7/20/2009 9:42 AM, Lacrima wrote: > On Jul 20, 4:05 pm, Jean-Michel Pichavant > wrote: >> Lacrima wrote: >>> Hello! >>> I am newbie in python and I have really simple question, but I need >>> your advice to know how to do best. >>> I need to store a number of dictionaries in certain place. I've >>> decided to store them in a separate module. >>> Like this: >>> dicts.py >>> ----------------------- >>> dict1 = {....} >>> dict2 = {....} >>> dict3 = {....} >>> Then, when I need any dictionary, I can access it: >>> import dicts >>> dicts.dict1 >>> Is it a good practice? Or should I store them as class attributes or >>> anything else? >>> Thanks in advance. >>> With regards, Max >>> (sorry if my English isn't very proper) >> Defining dict as a module attribute ic correct, but try to answer the >> following question: >> >> Is dict1 an attribute/property/declension of the object/entity defined >> by the module dicts ? >> If yes, then your design is correct. >> >> An correct example: >> fruits.py >> ------------ >> apple = {} >> banana = {} >> >> An incorrect one: >> fruit.py >> ----------- >> apple={} >> bicycle={} >> >> Basically, the rule is very straightforward, set your dict as a module >> attribute only if its one of its attribute (very nice paraphrase !) >> Most of people (including me) tend to have a module, where they put >> everything they have no idea about their location. This is a bad habit >> and result from a uncontrolled/undocumented design. Usually documenting >> objects in such modules is really painful. >> >> Your proposal is fine from a python syntax point of view. I can't tell >> of your design with names like (dicts.py and dict1,dict2) hoping you do >> not intend to name them that way. >> >> JM > > Hi, Jean-Michel! > > Thanks for your answer. > I am not going to have names like dicts.py and dict1,dict2. That was > just example. I wanted to know if it is correct to have dictionaries > on a module level. Now I know that this is correct. I want to collect > in one module a number of dictionaries, every of which describes a > separate web service. And my function in another module will import > required dictionary, depending on what web service should be used. > Thanks again for the help. > > With regards, > Max. > (sorry if my English isn't very proper) Yeah, you can put just about anything in a separate module/file, but putting unrelated things in the same module is bad... For example, if you have apple and banana in a module, and had a function called slicefruit() to do something to those variables in there with them, then that would be good. But if you put bicycle and car and adjustbrakes() in the module with the fruits, or mixed the two groups in several modules, that would be bad. Marcus From bieffe62 at gmail.com Mon Jul 20 13:44:16 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Mon, 20 Jul 2009 10:44:16 -0700 (PDT) Subject: comments? storing a function in an object References: Message-ID: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> On Jul 20, 6:22?pm, Esmail wrote: > Hello all, > > I am trying to store a function and some associated information in an > object so that I can later have series of functions in a list that I can > evaluate one at a time. > > Right now I am only storing the function itself, the number of > arguments it expects and its string representation. I may add other > information such as the acceptable range for the parameters or other > characteristics such as maximum or minimum. > > I wonder if anyone could comment on my implementation and offer > suggestions or constructive criticisms? > > The 'loading' of the functions is a bit tedious and of course care > has to be taken to make sure the string representation corresponds to > the actual function computed. It would be nice if there was an > automatic way to convert the function to its string representation. > > Comments or problems with the approach I have taken? > > Thanks, > Esmail > > -- > > #!/usr/bin/env python > > # > # store and represent functions > # > > import math > > class FunctionException(Exception): > ? ? ?""" custom exception """ > ? ? ?def __init__(self, value): > ? ? ? ? ?self.parameter = value > > ? ? ?def __str__(self): > ? ? ? ? ?return repr(self.parameter) > > class Function(object): > ? ? ?""" class to represent a function """ > > ? ? ?def __init__(self, fn, fn_str, num_vars): > ? ? ? ? ?""" > ? ? ? ? ?the function, its string representation, and the number of variables > ? ? ? ? ?""" > ? ? ? ? ?self.fn = fn > ? ? ? ? ?self.fn_str = fn_str > ? ? ? ? ?self.num_vars = num_vars > > ? ? ?def eval_fn(self, variables): > ? ? ? ? ?""" size of variables should equal num_vars .. else problem """ > ? ? ? ? ?if len(variables) == self.num_vars: > ? ? ? ? ? ? ?result = self.fn(*variables) > ? ? ? ? ? ? ?return result > ? ? ? ? ?else: > ? ? ? ? ? ? ?raise FunctionException('invalid number of args provided - '+ > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'received %d, expected %d' > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?%(len(variables), self.num_vars)) > > ? ? ?def str(self): > ? ? ? ? ?""" return string representation of function """ > ? ? ? ? ?return self.fn_str > > def funct1(x): > ? ? ?''' small test function ''' > ? ? ?return x * x > > def funct2(x, y): > ? ? ?''' small test function ''' > ? ? ?return x + y > > def funct3(x): > ? ? ?''' small test function ''' > ? ? ?return 1000 + (x*x + x) * math.cos(x) > > def main(): > ? ? ?""" main method """ > ? ? ?print 'in main' > > ? ? ?fn1 = Function(funct1, 'x * x', 1) > ? ? ?fn2 = Function(funct2, 'x + y', 2) > ? ? ?fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) > > ? ? ?for i in range(-10, 10): > ? ? ? ? ?try: > > ? ? ? ? ? ? ?print 'f(', [i],') =', > ? ? ? ? ? ? ?print fn3.str(), ' => ', > ? ? ? ? ? ? ?print fn3.eval_fn([i]) > > ? ? ? ? ?except FunctionException, (ex): > ? ? ? ? ? ? ?print ex.parameter > > if __name__ == '__main__': > ? ? ?main() I can offer some small suggestions: it is up to you to evaluate if they make sense for your app: 1. use __doc__ function standard attribute for function description (your fn_str); this meanst that you do not need fm_str in the constructor and you have to do e.g. : def funct2(x, y): ''' x + y ''' return x + y then funct2.__doc__ becomes the string you want to associate to the function 2. Use __call__ instead of eval_fn : this way, the instance of your Function became a 'callable' and can be used everywhere a function is needed. You can do: f = Function(...) f( some_args ) 3. If you call a python function with wrong number of arguments, it already raises a TypeError exception which contains - with different wording - the same information of your FunctionException : consider removing the check and using the python error instead HTH Ciao ------ FB From drobinow at gmail.com Mon Jul 20 13:46:26 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 20 Jul 2009 13:46:26 -0400 Subject: On out-of-date Python Applications In-Reply-To: <4A6496C6.4040606@it.uu.se> References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> <4A6496C6.4040606@it.uu.se> Message-ID: <4eb0089f0907201046x4bdc3b10t3ad56a888526eff2@mail.gmail.com> On Mon, Jul 20, 2009 at 12:09 PM, Virgil Stokes wrote: > David Robinow wrote: > > On Mon, Jul 20, 2009 at 7:24 AM, John Machin wrote: > ... > > > The next step would be to try to compile ODE 0.7 or 0.8 with VS9 -- > however this would require "project files" for ODE for VS9, and there > aren't any on the ODE website; it has only those for VS3 and VS5. > > > > The ODE site is a mess. > Go to http://www.ode.org/svn.html and click on: Instructions for > accessing the repository > Scroll down to the section "Building with Premake" > (Note that there is no directory "ode/build" -- you want the "build" > directory, which contains premake4.exe) > I used "premake4 --with-demos --with-tests vs2008" > I have successfully compiled ode-0.11.1 using these instructions. I > have not yet run the tests or demos or tried to compile PyODE. > > > Thanks for this information David. > Now that you have successfully compiled ode-0.11.1, how can one finish this > process --- compile PyODE? > > --V > Edit setup.py Windows specific settings: ODE_BASE to location of your ode distribution create releaselib directory under lib and copy the .lib file that you built in Visual Studio. This will be: ReleaseSingleLib\ode_single.lib or ReleaseDoubleLib\ode_double.lib or one of the Debug versions Change the name to ode.lib. i.e. in CMD-speak: %ODE_BASE%\lib\releaselib\ode.lib I was then able to build PyODE-1.2.0 However, all the examples died with: ODE Message 2: mass must be > 0 <..\..\ode\src\mass.cpp:49) I don't have time right now to debug this. Maybe somebody else can take it a bit further. From tvashtar at gmail.com Mon Jul 20 13:58:53 2009 From: tvashtar at gmail.com (tvashtar) Date: Mon, 20 Jul 2009 10:58:53 -0700 (PDT) Subject: Problem when applying Patch from issue1424152 to get https over authenticating proxies working with urllib2 in Python 2.5 References: <8fe2ad78-0bd4-40d8-909c-1d2784441e3c@s31g2000yqs.googlegroups.com> <58f2558c-33fe-4ee0-9a0a-2563374be47c@x6g2000prc.googlegroups.com> Message-ID: <446a3c42-6d86-4003-85be-3e5d8a23b268@c14g2000yqm.googlegroups.com> On Jul 20, 4:42?pm, Nike wrote: > hi! > ?It's looks like a ssl error . Under the following step to help u : > ? 1. takes a simple code to confirm your pupose without ssl protocol. > ? 2. to confirm python version and extended libs work well > ? 3. to confirm ssl work well. > > goog luck! > > nikekoo I've reduced my code to the following: import urllib2 p = "https://user:pass at myproxy:port" proxy_handler = urllib2.ProxyHandler({"https": p}) urllib2.install_opener(urllib2.build_opener(proxy_handler)) request = urllib2.Request( "https://groups.google.com") response = urllib2.urlopen(request) and it is now failing with: Traceback (most recent call last): File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \test.py", line 12, in response = urllib2.urlopen(request) File "C:\Python25\lib\urllib2.py", line 121, in urlopen return _opener.open(url, data) File "C:\Python25\lib\urllib2.py", line 379, in open response = self._open(req, data) File "C:\Python25\lib\urllib2.py", line 397, in _open '_open', req) File "C:\Python25\lib\urllib2.py", line 358, in _call_chain result = func(*args) File "C:\Python25\lib\urllib2.py", line 1115, in https_open return self.do_open(httplib.HTTPSConnection, req) File "C:\Python25\lib\urllib2.py", line 1082, in do_open raise URLError(err) urllib2.URLError: I thought the proxy_handler should take care of the authentication? Thanks for your help From kiorky at cryptelium.net Mon Jul 20 14:19:43 2009 From: kiorky at cryptelium.net (kiorky) Date: Mon, 20 Jul 2009 20:19:43 +0200 Subject: Compilation problem with Python < 2.6 and db >= 4.7 Message-ID: <4A64B53F.8060502@cryptelium.net> Is there a way to make the bsddb module compile against db>=4.7 for python < 2.6 (2.4.6, 2.5.4)? A patch ? A tip ? For the moment, i have a known failure as: gcc -pthread -fno-strict-aliasing -DNDEBUG -I/usr/home/kiorky/minitage/dependencies/readline-5.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/bzip2-1.0/parts/part/include -I/usr/home/kiorky/minitage/dependencies/zlib-1.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/db-4.7/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/expat-2.0/parts/part/include -I. -IInclude -I./Include -I/usr/home/kiorky/minitage/dependencies/readline-5.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/bzip2-1.0/parts/part/include -I/usr/home/kiorky/minitage/dependencies/zlib-1.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/db-4.7/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/expat-2.0/parts/part/include -fPIC -DPy_BUILD_CORE -I/usr/home/kiorky/minitage/dependencies/db-4.7/parts/part/include -c ./Modules/_bsddb.c -o Modules/_bsddb.o ./Modules/_bsddb.c: In function 'DBEnv_getattr': ./Modules/_bsddb.c:5337: error: 'DB_ENV' has no member named 'db_home' ./Modules/_bsddb.c:5340: error: 'DB_ENV' has no member named 'db_home' ./Modules/_bsddb.c: In function 'init_bsddb': ./Modules/_bsddb.c:5962: error: 'DB_LOG_AUTOREMOVE' undeclared (first use in this function) ./Modules/_bsddb.c:5962: error: (Each undeclared identifier is reported only once ./Modules/_bsddb.c:5962: error: for each function it appears in.) ./Modules/_bsddb.c:5963: error: 'DB_DIRECT_LOG' undeclared (first use in this function) ./Modules/_bsddb.c:5971: error: 'DB_LOG_INMEMORY' undeclared (first use in this function) *** Error code 1 I looked for the one inside python >= 2.6 and it seems to have changed a lot so backporting it seems difficult and maybe break the old API. I didn't find something on it. -- -- Cordialement, KiOrKY GPG Key FingerPrint: 0x1A1194B7681112AF From anthony.tolle at gmail.com Mon Jul 20 14:22:52 2009 From: anthony.tolle at gmail.com (Anthony Tolle) Date: Mon, 20 Jul 2009 11:22:52 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: On Jul 20, 12:27?pm, Phillip B Oldham wrote: > ... > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > ... There's no hard-set rules, but tuples are typically used to hold collections of heterogeneous data, e.g. (10, "spam", 3.21). As has been mentioned, such a tuple can be used as a dictionary key, whereas a list cannot be used as a dictionary key, because it is mutable. Lists, on the other hand, typically hold collections of homogeneous data, e.g. [1, 2, 5] or ["spam", "eggs", "sausage"]. Of course, you'll also see plenty of examples of tuples containing homogeneous data and lists containing heterogeneous data :) From lenz at joinville.udesc.br Mon Jul 20 14:26:50 2009 From: lenz at joinville.udesc.br (Eduardo Lenz) Date: Mon, 20 Jul 2009 11:26:50 -0700 Subject: ANN: psyco V2 In-Reply-To: <4A5FE672.3000401@stackless.com> References: <4A5FE672.3000401@stackless.com> Message-ID: <200907201126.51878.lenz@joinville.udesc.br> Em Qui 16 Jul 2009, ??s 19:48:18, Christian Tismer escreveu: > Announcing Psyco V2 source release > ---------------------------------- > > This is the long awaited announcement of Psyco V2. > > Psyco V2 is a continuation of the well-known psyco project, > which was called finished and was dis-continued by its author > Armin Rigo in 2005, in favor of the PyPy project. > > This is a new project, using Psyco's code base with permission > of Armin. Questions and complaints should go to me > (tismer at stackless.com) or the mailing list > (psyco-devel at lists.sourceforge.net); > Armin is explicitly not in charge of (t)his project any longer! > > As one of the founders and an active member of the PyPy > project, I was very happy to be invited to work on Psyco > V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco > approach "an extension module that just makes Python faster". > > Psyco is a just-in-time compiler that accelerates arbitrary > Python code by specialization. We believe that Psyco's approach > can be carried out much further than it was tried so far, when > it's first version was abandoned. > > This first V2 release is source-only. There is no web-site, yet, > and there are no binaries for download. These will be available > in a few days on http://www.psyco.org . > > For the time being, please stick with subversion access, > building the extension module from source code. The repository > is here: > > http://codespeak.net/svn/psyco/v2/dist > > Check-out the repository, and run the setup.py script, > given that you have access to a C compiler. > > Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, > and Mac OS X. Psyco is not supporting 64 bit, yet. But it > is well being considered. > > The current improvements are, shortly: > > - Support for Python 2.4, 2.5 and 2.6 > - a lot of new builtins > - generators, fast and fully supported. > > More information is coming soon on http://www.psyco.org . > > This is the beginning of a series of new Psyco versions. > Many more improvements are prepared and about to be published, > soon, starting with the current version 2.0.0 . > > Stay tuned, this is just the beginning of psyco's re-birth! > > For questions about Psyco V2, please join the mailing list > > psyco-devel at lists.sourceforge.net > > or contact me on IRC: > > #psyco on irc.freenode.net . > > Psyco V2 is fundamentally supported by FATTOC, LLC. > See http://www.fattoc.com . > > Without their continuous support, this work would not have > been possible at all. I wish to express my deepest thanks > to FATTOC, for allowing me to continue on Psyco with all the > energy that this ambitious project needs, and will need. > > Further special thanks are going to > Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, > Raymond Hettinger, Fabrizio Milo, Michael Foord, > Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, > for all the support and discussions. > > Looking forward to a great future of Psyco! > > July 17, 2009 I know its not the proper list but.... gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -DNDEBUG - g -O3 -fPIC -Ic/i386 -Ic -I/usr/include/python2.6 -c c/mergepoints.c -o build/temp.linux-i686-2.6/c/mergepoints.o c/mergepoints.c:250: error: ???RAISE_VARARGS??? undeclared here (not in a function) c/mergepoints.c:250: error: ???JUMP_IF_FALSE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???JUMP_IF_TRUE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???DUP_TOPX??? undeclared here (not in a function) c/mergepoints.c:250: error: ???IMPORT_STAR??? undeclared here (not in a function) c/mergepoints.c:250: error: ???SLICE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???STORE_SLICE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???DELETE_SLICE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_EXPR??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_ITEM??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_ITEM_TO??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_NEWLINE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_NEWLINE_TO??? undeclared here (not in a function) c/mergepoints.c:250: error: ???BUILD_CLASS??? undeclared here (not in a function) c/mergepoints.c:250: error: ???IMPORT_NAME??? undeclared here (not in a function) c/mergepoints.c:250: error: ???IMPORT_FROM??? undeclared here (not in a function) c/mergepoints.c:250: error: ???MAKE_FUNCTION??? undeclared here (not in a function) c/mergepoints.c:250: error: ???BUILD_SLICE??? undeclared here (not in a function) error: command 'gcc' failed with exit status 1 any clue ? []'s Eduardo Tel: +55 47 4009-7971 - Fax: +55 47 4009-7940 E-mail: lenz at Joinville.udesc.br --------------------------------------------- -- Esta mensagem foi verificada pelo sistema de antiv?rus e acredita-se estar livre de perigo. From skip at pobox.com Mon Jul 20 14:26:56 2009 From: skip at pobox.com (skip at pobox.com) Date: Mon, 20 Jul 2009 13:26:56 -0500 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a645b81$0$12974$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <19044.46832.524925.930667@montanaro.dyndns.org> William> c 5s William> gcj 7s William> java 7s William> shedskin 8s William> python + psyco 18s William> cython avec malloc *int 18s William> cython 55s avec [] python William> python 303s (5m3s) I read just enough French to know that "avec" means "with", but I don't understand the difference between "avec malloc *int" and "avec []". Can you explain please? Thx, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire From vippstar at gmail.com Mon Jul 20 14:31:08 2009 From: vippstar at gmail.com (vippstar) Date: Mon, 20 Jul 2009 11:31:08 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> <7xljmjguxb.fsf@ruckus.brouhaha.com> Message-ID: <7a04b07d-2dea-455f-bafd-33461ce25a1a@q11g2000yqi.googlegroups.com> On Jul 20, 7:50?pm, Paul Rubin wrote: > vippstar writes: > > > I wonder whether 2/3 => ValueError is preferable. > > > Not all software wants this. It shouldn't be part of the language but > > rather part of your code if you need such a feature. (for instance, to > > distinguish between 2/3 and divisions with 0 dividend). > > I don't see how to implement such a thing in my code, Write a function: (if (< x y) ValueError (/ x y)) This will return 0 only if the dividend = 0, not in integer division x/ y with y > x, which will return ValueError. Of course, ValueError must not be an integer, because that could be the result of an integer division. If it's not possible to return multiple types, then the function can make use of some error handling mechanism. > if I believe that the ring of integers doesn't have any concept > of division and so attempts to divide integers should be treated > as errors. Wouldn't that mean 3/2 would also evaluate to ValueError? But 3/2 = 1 in integer division, not 0, like 2/3. Regardless, it's a specialized requirement, and thus should either be implemented by the programmer or the language could provide it if it's specialized, (for instance, I wouldn't require a language to provide text manipulation features, but I expect them from perl because it's not a general all purpose language [the name designates that, however it can be used as one - like lisp]) > course the present / operator is useful, but I could do just as well > with the divmod function which I think is more explicit. What? Python? Aww. From bearophileHUGS at lycos.com Mon Jul 20 14:51:18 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Mon, 20 Jul 2009 11:51:18 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <04f03d96-46b7-4761-8498-9149f19ecba6@k6g2000yqn.googlegroups.com> Skip Montanaro: > I read just enough French to know that "avec" means "with", but I don't > understand the difference between "avec malloc *int" and "avec []". ?Can you > explain please? Maybe it's the time difference between using a Python list from Cython and using a C "array" allocated with a malloc from Cython. Bye, bearophile From mark.dufour at gmail.com Mon Jul 20 15:20:55 2009 From: mark.dufour at gmail.com (srepmub) Date: Mon, 20 Jul 2009 12:20:55 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> Message-ID: > Nice timings, can you please show me the Python, Java and C code > versions? I may do more tests. also, which shedskin options did you use? did you use -bw, to disable bounds and wrap-around checking? this can make quite a difference for code that does a lot of indexing. if the code uses random numbers, then -r can also make a big difference, to use C rand(), instead of Python compatible random numbers. and which C++ compiler flags did you use? the default -O2, or something like -O3 -fomit-frame-pointer -msse2..? thanks, mark. From phillip.oldham at gmail.com Mon Jul 20 15:26:07 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Mon, 20 Jul 2009 12:26:07 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> On Jul 20, 6:08?pm, Duncan Booth wrote: > The main reason why you need both lists and tuples is that because a tuple > of immutable objects is itself immutable you can use it as a dictionary > key. Really? That sounds interesting, although I can't think of any real- world cases where you'd use something like that. From duncan.booth at invalid.invalid Mon Jul 20 15:54:02 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 20 Jul 2009 19:54:02 GMT Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: Phillip B Oldham wrote: > On Jul 20, 6:08?pm, Duncan Booth wrote: >> The main reason why you need both lists and tuples is that because a >> tuple of immutable objects is itself immutable you can use it as a >> dictionary key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. > How about a multi-dimensional sparse array? >>> d = {} >>> d[1,2] = 'a' >>> d[5,7] = 'b' From marcusw at cox.net Mon Jul 20 16:12:45 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 16:12:45 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: On 7/20/2009 3:26 PM, Phillip B Oldham wrote: > On Jul 20, 6:08 pm, Duncan Booth wrote: >> The main reason why you need both lists and tuples is that because a tuple >> of immutable objects is itself immutable you can use it as a dictionary >> key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. Actually, that would be very useful in the program from "any suggestions to synchronize typed text and speech ?"...i.e. have a dictionary key of (hour, minute, second). Marcus From jcd at sdf.lonestar.org Mon Jul 20 16:15:16 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Mon, 20 Jul 2009 16:15:16 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: <1248120917.5967.5.camel@aalcdl07> On Mon, 2009-07-20 at 12:26 -0700, Phillip B Oldham wrote: > On Jul 20, 6:08 pm, Duncan Booth wrote: > > The main reason why you need both lists and tuples is that because a tuple > > of immutable objects is itself immutable you can use it as a dictionary > > key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. Well, if you wanted to index a dictionary by coordinates, you might do something like this: fleet = {} fleet[9,4] = 'destroyer' fleet[8,4] = 'destroyer' fleet[3,5] = 'aircraftcarrier' fleet[4,5] = 'aircraftcarrier' fleet[5,5] = 'aircraftcarrier' fleet[6,5] = 'aircraftcarrier' fleet[8,0] = 'battleship' fleet[8,1] = 'battleship' fleet[8,2] = 'battleship' def checkattack(x, y, fleet): if x,y in fleet: return "You hit my %s' % fleet[x,y] Maybe not the best implementation of Battleship, but you get the idea. From mrstevegross at gmail.com Mon Jul 20 16:38:57 2009 From: mrstevegross at gmail.com (mrstevegross) Date: Mon, 20 Jul 2009 13:38:57 -0700 (PDT) Subject: How to import pydoc and then use it? Message-ID: I know how to use pydoc from the command line. However, because of complicated environmental setup, it would be preferable to run it within a python script as a native API call. That is, my python runner looks a bit like this: import pydoc pydoc.generate_html_docs_for(someFile) However, it's not clear to me from the pydoc documentation which function calls I need to use to make this behavior work. Any ideas? Thanks, --Steve From nytrokiss at gmail.com Mon Jul 20 17:01:13 2009 From: nytrokiss at gmail.com (James Matthews) Date: Tue, 21 Jul 2009 00:01:13 +0300 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> Message-ID: <8a6b8e350907201401k383b0982ma7aeaaf8bf7d7f8a@mail.gmail.com> I like this, I am going to run this as a test. I also want to see the source code on how they compile the dynamic variables. On Mon, Jul 20, 2009 at 10:20 PM, srepmub wrote: > > > Nice timings, can you please show me the Python, Java and C code > > versions? I may do more tests. > > also, which shedskin options did you use? did you use -bw, to disable > bounds and wrap-around checking? this can make quite a difference for > code that does a lot of indexing. if the code uses random numbers, > then -r can also make a big difference, to use C rand(), instead of > Python compatible random numbers. > > and which C++ compiler flags did you use? the default -O2, or > something like -O3 -fomit-frame-pointer -msse2..? > > > thanks, > mark. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.jewelerslounge.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From egon.frerich at nord-com.net Mon Jul 20 17:07:38 2009 From: egon.frerich at nord-com.net (Egon Frerich) Date: Mon, 20 Jul 2009 23:07:38 +0200 Subject: locale doesn' format Message-ID: <4A64DC9A.3060606@nord-com.net> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I want to format values to the german form eg. 1.034,56 but locale.format() doesn't work for me. Here is a little test program: import decimal, locale print locale.getdefaultlocale() print locale.localeconv() locale.setlocale(locale.LC_ALL, ('de_DE', 'UTF-8')) print locale.localeconv() k = decimal.Decimal('1034.56') #k = 1034.56 print 'k', k, type(k) k1 = locale.format_string('%12s',k,True) print 'k1', k1, type(k1) k1a = locale.format('%12s',k,True,True) print 'k1a', k1a, type(k1a) k2 = unicode(k1) print 'k2',k2,type(k2) print k1.find(',') print k1a.find(',') print k2.find(',') ab = locale.localeconv()['decimal_point'] print 'ab', ab print locale.localeconv()['decimal_point'].find(',') print locale.localeconv()['mon_decimal_point'].find(',') ac = locale.localeconv()['mon_decimal_point'] print 'ac', ac, len(ac) print locale.localeconv() k1, k1a, k2 are printed as 1034.56 Egon -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFKZNyaZRiDo9Iq4qIRAuqhAKCW3x7iDbrxUDp3M/qHabwbw92osQCgi6N2 mmgb54LACmrd/Wi4BRi2iZo= =psal -----END PGP SIGNATURE----- From piet at cs.uu.nl Mon Jul 20 17:23:25 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 20 Jul 2009 23:23:25 +0200 Subject: Help understanding the decisions *behind* python? References: Message-ID: >>>>> Duncan Booth (DB) wrote: >DB> Phillip B Oldham wrote: >>> This make a lot more sense to us, and follows the convention from >>> other languages. It would also mean chaining methods to manipulate >>> lists would be easier: >>> >>>>>> x = [2,1,3] >>>>>> print x.sort()[0] >>> 3 >>>>>> print x >>> [2,1,3] >DB> You already have a way to do what you want: >>>>> x = [2,1,3] >>>>> print sorted(x)[0] >DB> 3 What kind of Python produces that? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From clp2 at rebertia.com Mon Jul 20 17:30:16 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 20 Jul 2009 14:30:16 -0700 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: Message-ID: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum wrote: >>>>>> Duncan Booth (DB) wrote: > >>DB> Phillip B Oldham wrote: >>>> This make a lot more sense to us, and follows the convention from >>>> other languages. It would also mean chaining methods to manipulate >>>> lists would be easier: >>>> >>>>>>> x = [2,1,3] >>>>>>> print x.sort()[0] >>>> 3 >>>>>>> print x >>>> [2,1,3] > >>DB> You already have a way to do what you want: > >>>>>> x = [2,1,3] >>>>>> print sorted(x)[0] >>DB> 3 > > What kind of Python produces that? Assuming you're referring to the latter example, it was added in version 2.4 If you meant the former example, I think that's purely pseudo-Python. Cheers, Chris -- http://blog.rebertia.com From gagsl-py2 at yahoo.com.ar Mon Jul 20 17:38:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 18:38:22 -0300 Subject: comments? storing a function in an object References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: En Mon, 20 Jul 2009 14:44:16 -0300, Francesco Bochicchio escribi?: > On Jul 20, 6:22?pm, Esmail wrote: >> Hello all, >> >> I am trying to store a function and some associated information in an >> object so that I can later have series of functions in a list that I can >> evaluate one at a time. >> >> Right now I am only storing the function itself, the number of >> arguments it expects and its string representation. I may add other >> information such as the acceptable range for the parameters or other >> characteristics such as maximum or minimum. >> >> I wonder if anyone could comment on my implementation and offer >> suggestions or constructive criticisms? >> >> The 'loading' of the functions is a bit tedious and of course care >> has to be taken to make sure the string representation corresponds to >> the actual function computed. It would be nice if there was an >> automatic way to convert the function to its string representation. >> >> Comments or problems with the approach I have taken? >> >> Thanks, >> Esmail >> >> -- >> >> #!/usr/bin/env python >> >> # >> # store and represent functions >> # >> >> import math >> >> class FunctionException(Exception): >> ? ? ?""" custom exception """ >> ? ? ?def __init__(self, value): >> ? ? ? ? ?self.parameter = value >> >> ? ? ?def __str__(self): >> ? ? ? ? ?return repr(self.parameter) >> >> class Function(object): >> ? ? ?""" class to represent a function """ >> >> ? ? ?def __init__(self, fn, fn_str, num_vars): >> ? ? ? ? ?""" >> ? ? ? ? ?the function, its string representation, and the number of >> variables >> ? ? ? ? ?""" >> ? ? ? ? ?self.fn = fn >> ? ? ? ? ?self.fn_str = fn_str >> ? ? ? ? ?self.num_vars = num_vars >> >> ? ? ?def eval_fn(self, variables): >> ? ? ? ? ?""" size of variables should equal num_vars .. else problem """ >> ? ? ? ? ?if len(variables) == self.num_vars: >> ? ? ? ? ? ? ?result = self.fn(*variables) >> ? ? ? ? ? ? ?return result >> ? ? ? ? ?else: >> ? ? ? ? ? ? ?raise FunctionException('invalid number of args provided - >> '+ >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'received %d, expected %d' >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?%(len(variables), self.num_vars)) >> >> ? ? ?def str(self): >> ? ? ? ? ?""" return string representation of function """ >> ? ? ? ? ?return self.fn_str >> >> def funct1(x): >> ? ? ?''' small test function ''' >> ? ? ?return x * x >> >> def funct2(x, y): >> ? ? ?''' small test function ''' >> ? ? ?return x + y >> >> def funct3(x): >> ? ? ?''' small test function ''' >> ? ? ?return 1000 + (x*x + x) * math.cos(x) >> >> def main(): >> ? ? ?""" main method """ >> ? ? ?print 'in main' >> >> ? ? ?fn1 = Function(funct1, 'x * x', 1) >> ? ? ?fn2 = Function(funct2, 'x + y', 2) >> ? ? ?fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) >> >> ? ? ?for i in range(-10, 10): >> ? ? ? ? ?try: >> >> ? ? ? ? ? ? ?print 'f(', [i],') =', >> ? ? ? ? ? ? ?print fn3.str(), ' => ', >> ? ? ? ? ? ? ?print fn3.eval_fn([i]) >> >> ? ? ? ? ?except FunctionException, (ex): >> ? ? ? ? ? ? ?print ex.parameter >> >> if __name__ == '__main__': >> ? ? ?main() > > > I can offer some small suggestions: it is up to you to evaluate if > they make sense for your app: > > 1. use __doc__ function standard attribute for function description > (your fn_str); this meanst that > you do not need fm_str in the constructor and you have to do e.g. : > > def funct2(x, y): > ''' x + y ''' > return x + y > > then funct2.__doc__ becomes the string you want to associate to the > function > > 2. Use __call__ instead of eval_fn : this way, the instance of your > Function became a 'callable' and can be used > everywhere a function is needed. You can do: > > f = Function(...) > f( some_args ) > > 3. If you call a python function with wrong number of arguments, it > already raises a TypeError exception which contains > - with different wording - the same information of your > FunctionException : consider removing the check and using the > python error instead If you follow the above suggestions, you'll see that your Function class becomes almost useless: a normal function already IS an object, so you don't have to wrap it inside ANOTHER object unless you need very special features. -- Gabriel Genellina From aahz at pythoncraft.com Mon Jul 20 17:40:05 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 14:40:05 -0700 Subject: any suggestions to synchronize typed text and speech ? References: Message-ID: In article , Stef Mientki wrote: > >I was afraid of that too, so I dropped the question in several places, >and the writer of Scintilla himself came with the perfect answer. ...which was?... -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From gagsl-py2 at yahoo.com.ar Mon Jul 20 17:40:14 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 18:40:14 -0300 Subject: Auto-generate GUI from a database table structure References: <55660F9659966544BBA3AE14CE5D278D01243B8E04@exgtmb08.nam.nsroot.net> Message-ID: En Mon, 20 Jul 2009 12:09:47 -0300, Boyd, Craig1 escribi?: > I am trying to write a couple screens to update three or four database > tables on Oracle 10g and I was wondering if there was a way to > automatically generate some kind of GUI shell based on the tables that I > could then fill in with the logic / functionality I need. I've never actually used it, but the DABO project seems to provide what you want: http://dabodev.com/ -- Gabriel Genellina From marduk at letterboxes.org Mon Jul 20 17:40:19 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Mon, 20 Jul 2009 17:40:19 -0400 Subject: How to import pydoc and then use it? In-Reply-To: References: Message-ID: <1248126024.9281.3.camel@centar> On Mon, 2009-07-20 at 13:38 -0700, mrstevegross wrote: > I know how to use pydoc from the command line. However, because of > complicated environmental setup, it would be preferable to run it > within a python script as a native API call. That is, my python runner > looks a bit like this: > > import pydoc > pydoc.generate_html_docs_for(someFile) > > However, it's not clear to me from the pydoc documentation which > function calls I need to use to make this behavior work. Any ideas? Did you try 'pydoc pydoc'? ;) >>> import pydoc >>> htmldoc = pydoc.HTMLDoc() >>> htmldoc_for_pydoc = htmldoc(pydoc) >>> print htmldoc_for_pydoc From p.f.moore at gmail.com Mon Jul 20 17:40:58 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 20 Jul 2009 22:40:58 +0100 Subject: Help understanding the decisions *behind* python? In-Reply-To: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> References: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> Message-ID: <79990c6b0907201440r5470c486j207440183514ba9@mail.gmail.com> 2009/7/20 Chris Rebert : > On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum wrote: >>>>>>> x = [2,1,3] >>>>>>> print sorted(x)[0] >>>DB> 3 >> >> What kind of Python produces that? > > Assuming you're referring to the latter example, it was added in version 2.4 > If you meant the former example, I think that's purely pseudo-Python. I think he was referring to the fact that the result should be 1, not 3. Paul. From malaclypse2 at gmail.com Mon Jul 20 17:59:08 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Mon, 20 Jul 2009 17:59:08 -0400 Subject: locale doesn' format In-Reply-To: <4A64DC9A.3060606@nord-com.net> References: <4A64DC9A.3060606@nord-com.net> Message-ID: <16651e80907201459w5060ad3ex9706a1f4ec12c@mail.gmail.com> On Mon, Jul 20, 2009 at 5:07 PM, Egon Frerich wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I want to format values to the german form eg. 1.034,56 but > locale.format() doesn't work for me. > > Here is a little test program: ... > k1 = locale.format_string('%12s',k,True) I don't think you want to be using '%s' to format numbers in your format strings. Instead, use '%f' to format floating point values. As I understand it, a %s in a format string actually calls str() on the object that you pass into the formatting function, and I don't think that floats (or Decimal.Decimal objects) take locale into account when you do that. -- Jerry From FSet.SLB at gmail.com Mon Jul 20 18:07:58 2009 From: FSet.SLB at gmail.com (Scott Burson) Date: Mon, 20 Jul 2009 15:07:58 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> On Jul 19, 11:31?am, Frank Buss wrote: > I don't know of a free modern and stable Lisp implementation with > mulithreading support for Windows, with a licence with which you can use it > in closed source commercial programs Have you looked at ECL? http://ecls.sourceforge.net/ I've used it only a little, so I can't vouch for its stability, but it fits the threading and license requirements (well, some corporate lawyers have trouble with the LGPL, but I think it's usable). -- Scott From corbet at lwn.net Mon Jul 20 18:16:59 2009 From: corbet at lwn.net (Jonathan Corbet) Date: Mon, 20 Jul 2009 16:16:59 -0600 Subject: Python Essential Reference, 4th Edition - Now Available! In-Reply-To: References: Message-ID: <20090720161659.78cbf90a@bike.lwn.net> On Sun, 19 Jul 2009 11:03:05 -0500 David Beazley wrote: > I'm pleased to announce the release of the Python Essential Reference, > 4th edition, now available at a bookstore near you. More than a year > in development, this edition covers Python 2.6, Python 3, and a wide > variety of new library modules. And here I am still using my old, dog-eared second edition... Any chance of getting a copy of the 4th for an LWN review? Thanks, jon -- Jonathan Corbet / LWN.net / corbet at lwn.net From hniksic at xemacs.org Mon Jul 20 18:19:36 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 21 Jul 2009 00:19:36 +0200 Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: <87ljmjyp3b.fsf@busola.homelinux.net> Phillip B Oldham writes: > On Jul 20, 6:08?pm, Duncan Booth wrote: >> The main reason why you need both lists and tuples is that because a tuple >> of immutable objects is itself immutable you can use it as a dictionary >> key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. An application visiting files on a filesystem recursively needs a dictionary or set keyed by (st_dev, st_ino) to make sure it doesn't visit the same file twice. Memoization implementation (of a cache for results of function application) might need to use a dictionary to map function arguments, a tuple, to the function result. From hniksic at xemacs.org Mon Jul 20 18:20:53 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 21 Jul 2009 00:20:53 +0200 Subject: Help understanding the decisions *behind* python? References: Message-ID: <87hbx7yp16.fsf@busola.homelinux.net> Chris Rebert writes: >>>>>>> x = [2,1,3] >>>>>>> print sorted(x)[0] >>>DB> 3 >> >> What kind of Python produces that? > > Assuming you're referring to the latter example, it was added in version 2.4 > If you meant the former example, I think that's purely pseudo-Python. sorted([2, 1, 3])[0] evaluates to 1, not 3. From http Mon Jul 20 18:22:20 2009 From: http (Paul Rubin) Date: 20 Jul 2009 15:22:20 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> <7xljmjguxb.fsf@ruckus.brouhaha.com> <7a04b07d-2dea-455f-bafd-33461ce25a1a@q11g2000yqi.googlegroups.com> Message-ID: <7xiqhn0zc3.fsf@ruckus.brouhaha.com> vippstar writes: > > I don't see how to implement such a thing in my code, > Write a function: > > (if (< x y) > ValueError > (/ x y)) I meant changing the behavior of integer division in python. > Wouldn't that mean 3/2 would also evaluate to ValueError? Yes, the idea was that one can take the view that integer division should not be allowed except through a 'div' function or some such. From ethan at stoneleaf.us Mon Jul 20 18:34:28 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 20 Jul 2009 15:34:28 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <24543805.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> <24543805.post@talk.nabble.com> Message-ID: <4A64F0F4.9060201@stoneleaf.us> [fixed for bottom-posting] Dr. Phillip M. Feldman wrote: > MRAB-2 wrote: > >> >> >> >>What values should 'xor' return? IMHO, if only one of the values is true >>then it should return that value, otherwise it should return False. >> >> 1 xor 0 => 1 >> 0 xor 2 => 2 >> 1 xor 2 => False >> 0 xor 0 => False >> >>This is because it's a Boolean operator, so it should fall back to >>Boolean values when necessary, like 'not': >> >> not 0 => True >> not 1 => False >> >>Also: >> >> x and y and z => (x and y) and z >> x or y or z => (x or y) or z >> >>therefore: >> >> x xor y xor z => (x xor y) xor z >> > Suppose that 'xor' returns the value that is true when only one value is > true, and False otherwise. This definition of xor doesn't have the standard > associative property, that is, > > (a xor b) xor c > > will not necessarily equal > > a xor (b xor c) > > To see that this is the case, let a= 1, b= 2, and c= 3. > > (a xor b) xor c > > yields 3, while > > a xor (b xor c) > > yields 1. So, I'd prefer an xor operator that simply returns True or False. > > Phillip > You are, of course, free to write your version however it makes sense to you and your team. :) Since the 'and' and 'or' already return objects (and objects evaluate to true or false), then 'xor' should behave likewise, IMO. I expect that would be the case if it were ever added to the language. ~Ethan~ From martin at v.loewis.de Mon Jul 20 18:39:11 2009 From: martin at v.loewis.de (=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=) Date: Tue, 21 Jul 2009 00:39:11 +0200 Subject: Compilation problem with Python < 2.6 and db >= 4.7 In-Reply-To: References: Message-ID: <4a64f210$0$8379$9b622d9e@news.freenet.de> > Is there a way to make the bsddb module compile against db>=4.7 for python < 2.6 > (2.4.6, 2.5.4)? I don't think so, no. > I didn't find something on it. If you don't want to use pybsddb either, for fear of incompatible API, your only choice is to port _bsddb.c to the newer db versions. BSDDB is in the tradition of both breaking the data format and the API frequently, so we (python-dev) have given up on it. Regards, Martin From niels.ellegaard at gmail.com Mon Jul 20 18:39:24 2009 From: niels.ellegaard at gmail.com (Niels L. Ellegaard) Date: Tue, 21 Jul 2009 00:39:24 +0200 Subject: Help understanding the decisions *behind* python? References: Message-ID: <86skgr0yjn.fsf@gmail.com> Phillip B Oldham writes: > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. If you just want a one-liner, and you don't care about speed you can do the following (but I don't think this is considered best practice) >>> x = [2,1,3] >>> print list(sorted(x)) [1, 2, 3] >>> print x [2, 1, 3] Niels From vippstar at gmail.com Mon Jul 20 18:48:51 2009 From: vippstar at gmail.com (vippstar) Date: Mon, 20 Jul 2009 15:48:51 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> <7xljmjguxb.fsf@ruckus.brouhaha.com> <7a04b07d-2dea-455f-bafd-33461ce25a1a@q11g2000yqi.googlegroups.com> <7xiqhn0zc3.fsf@ruckus.brouhaha.com> Message-ID: On Jul 21, 1:22?am, Paul Rubin wrote: > vippstar writes: > > > I don't see how to implement such a thing in my code, > > Write a function: > > > ? (if (< x y) > > ? ? ? ValueError > > ? ? ? (/ x y)) > > I meant changing the behavior of integer division in python. You'd either have to hack an implementation or change the standard (I just noticed python doesn't have one). > > Wouldn't that mean 3/2 would also evaluate to ValueError? > > Yes, the idea was that one can take the view that integer division > should not be allowed except through a 'div' function or some such. You brought up 3/2 == ValueError as a more appropriate value for the integer division to evaluate, rather than 0. I thought you meant specifically those kinds of divisions. From kiorky at cryptelium.net Mon Jul 20 18:51:42 2009 From: kiorky at cryptelium.net (kiorky) Date: Tue, 21 Jul 2009 00:51:42 +0200 Subject: Compilation problem with Python < 2.6 and db >= 4.7 In-Reply-To: <4a64f210$0$8379$9b622d9e@news.freenet.de> References: <4a64f210$0$8379$9b622d9e@news.freenet.de> Message-ID: <4A64F4FE.805@cryptelium.net> Martin v. L?wis a ?crit : >> Is there a way to make the bsddb module compile against db>=4.7 for python < 2.6 >> (2.4.6, 2.5.4)? > > I don't think so, no. > >> I didn't find something on it. > > If you don't want to use pybsddb either, for fear of incompatible API, > your only choice is to port _bsddb.c to the newer db versions. BSDDB > is in the tradition of both breaking the data format and the API > frequently, so we (python-dev) have given up on it. Yup, i understand. I will be lazy and fall back as all packaging systems do, and keep in the minitage packages tree both db versions. Thanks for confirmation. > > Regards, > Martin -- -- Cordialement, KiOrKY GPG Key FingerPrint: 0x1A1194B7681112AF From lists at cheimes.de Mon Jul 20 18:56:12 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 21 Jul 2009 00:56:12 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <86skgr0yjn.fsf@gmail.com> References: <86skgr0yjn.fsf@gmail.com> Message-ID: Niels L. Ellegaard wrote: > Phillip B Oldham writes: > >> We often find we need to do manipulations like the above without >> changing the order of the original list, and languages like JS allow >> this. We can't work out how to do this in python though, other than >> duplicating the list, sorting, reversing, then discarding. > > If you just want a one-liner, and you don't care about speed you can > do the following (but I don't think this is considered best practice) sorted() already returns a new list not a generator as you might assume. Christian From piet at cs.uu.nl Mon Jul 20 19:41:16 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 01:41:16 +0200 Subject: Help understanding the decisions *behind* python? References: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> Message-ID: >>>>> Paul Moore (PM) wrote: >PM> 2009/7/20 Chris Rebert : >>> On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum wrote: >>>>>>>>> x = [2,1,3] >>>>>>>>> print sorted(x)[0] >DB> 3 >>>> >>>> What kind of Python produces that? >>> >>> Assuming you're referring to the latter example, it was added in version 2.4 >>> If you meant the former example, I think that's purely pseudo-Python. >PM> I think he was referring to the fact that the result should be 1, not 3. The underlying thought was that you should copy and paste examples from a real Python interpreter. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Mon Jul 20 20:10:55 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 17:10:55 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: In article , D'Arcy J.M. Cain wrote: >On 20 Jul 2009 09:00:33 -0700 >aahz at pythoncraft.com (Aahz) wrote: >> >> Out of curiosity, are there any JavaScript toolkits that generate code >> that degrades gracefully when JavaScript is disabled? > >I understand what you want but I can't see how a toolkit can do that. >How do you program "graceful?" It seems pretty application specific. Presumably the JS toolkit generates both code and HTML. Actions that normally get executed by JS should degrade to plain HTML links and form submits (or possibly not display at all if it's a pure convenience). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From gdamjan at gmail.com Mon Jul 20 20:33:39 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Tue, 21 Jul 2009 02:33:39 +0200 Subject: locale doesn' format References: Message-ID: <30ffj6-qd6.ln1@archaeopteryx.softver.org.mk> > I want to format values to the german form eg. 1.034,56 but > locale.format() doesn't work for me. seems to work here >>> import decimal, locale >>> locale.setlocale(locale.LC_ALL, 'mk_MK.UTF-8') 'mk_MK.UTF-8' >>> locale.localeconv()['grouping'] [3, 3, 0] >>> locale.localeconv()['thousands_sep'] ' ' >>> locale.localeconv()['decimal_point'] ',' >>> locale.format('%f', 1034.56,True) '1 034,560000' >>> locale.format('%d', 1034.56,True) '1 034' ps. you can copy/paste the above in .txt file and then run/test it with: python -m doctest -v check.txt -- ?????? ( http://softver.org.mk/damjan/ ) Begin...the rest is easy. From gdamjan at gmail.com Mon Jul 20 20:44:38 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Tue, 21 Jul 2009 02:44:38 +0200 Subject: locale doesn' format References: <30ffj6-qd6.ln1@archaeopteryx.softver.org.mk> Message-ID: >> I want to format values to the german form eg. 1.034,56 but >> locale.format() doesn't work for me. > > seems to work here In 2.6 this is good too: >>> import decimal, locale >>> locale.setlocale(locale.LC_ALL, 'mk_MK.UTF-8') >>> '{0:n}'.format(1234.56) '1 234,56' -- ?????? ( http://softver.org.mk/damjan/ ) A: Because it reverses the logical flow of conversation. Q: Why is top posting frowned upon? From davea at ieee.org Mon Jul 20 20:47:56 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 20 Jul 2009 20:47:56 -0400 Subject: Design question. In-Reply-To: <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> Message-ID: <4A65103C.4010401@ieee.org> Lacrima wrote: > On Jul 20, 4:05 pm, Jean-Michel Pichavant > wrote: > >> Lacrima wrote: >> >>> Hello! >>> >>> I am newbie in python and I have really simple question, but I need >>> your advice to know how to do best. >>> I need to store a number of dictionaries in certain place. I've >>> decided to store them in a separate module. >>> Like this: >>> dicts.py >>> ----------------------- >>> dict1 =....} >>> dict2 =....} >>> dict3 =....} >>> >>> Then, when I need any dictionary, I can access it: >>> import dicts >>> dicts.dict1 >>> >> > Hi, Jean-Michel! > > Thanks for your answer. > I am not going to have names like dicts.py and dict1,dict2. That was > just example. I wanted to know if it is correct to have dictionaries > on a module level. Now I know that this is correct. I want to collect > in one module a number of dictionaries, every of which describes a > separate web service. And my function in another module will import > required dictionary, depending on what web service should be used. > Thanks again for the help. > > With regards, > Max. > (sorry if my English isn't very proper) > > It makes a lot of sense to store several dictionaries in a module, if they describe parallel things, or alternate ways of accessing something (like web services). However, you might want to consider how they'll be accessed. If another module is going to "import required dictionary," then you have some means of selection. Perhaps that selection should be in the same module, or at least the data structure to select it should be in the same module. Let's say you have four choices right now. And you add a fifth later. Do you want to have to edit both the "function in the other module" as well as this module? This is similar to a feature that some programs have, "plug-ins." There a customer can add an additional runtime just by dropping a new .py file in the appropriate directory, and the program will find it and add it to its "list of features." So I'm just suggesting you figure out how the program might evolve, and set up the data so changes may be localized, if practical. This turns out usually to be parallel to the notion of naming things and grouping them by similarity of functionality. DaveA From sajmikins at gmail.com Mon Jul 20 20:56:26 2009 From: sajmikins at gmail.com (Simon Forman) Date: Mon, 20 Jul 2009 17:56:26 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7xljmk8q1m.fsf@ruckus.brouhaha.com> Message-ID: <6348dcb8-bfc1-446e-b7bf-28657e2b607e@j32g2000yqh.googlegroups.com> On Jul 19, 2:51 pm, Paul Rubin wrote: > Calroc writes: > > I'm engaged presently in starting a school to teach programming from > > the ground up, based roughly on the curriculum outlined in the article > > I mentioned. ... > > I'm excited about formal methods because one, I just heard about them, > > Formal methods are a big and complicated subject. Right after you > heard about them is probably not the best time to start teaching them. > Better get to understand them yourself first. Very good point. But I'm glad it's there to study, these are wheels I don't have to invent for myself. ~Simon From sajmikins at gmail.com Mon Jul 20 20:57:05 2009 From: sajmikins at gmail.com (Simon Forman) Date: Mon, 20 Jul 2009 17:57:05 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7cimf0F27u23eU1@mid.individual.net> Message-ID: <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> On Jul 20, 4:00 am, greg wrote: > Calroc wrote: > > It may be that flawless software is an unreachable asymptote, like the > > speed of light for matter, but I'm (recently!) convinced that it's a > > goal worthy of exploration and effort. > > Seems to me that once you get beyond the toy program > stage and try to apply correctness proving to real > world programming problems, you run up against the > problem of what exactly you mean by "correct". > > Once the requirements get beyond a certain level of > complexity, deciding whether the specifications > themselves are correct becomes just as difficult as > deciding whether the program meets them. I agree. You could prove that a game renders geometry without crashing, but not that it's fun. Or in an accounting package you could prove that it never loses a cent in tracking accounts and payments, or that its graph-rendering code does correctly render data (without introducing artifacts, for instance), but not that it will let you notice trends. > Then there's the fact that very often we don't > even know what the exact requirements are, and it's > only by trying to write and use the program that > we discover what they really are -- or at least, > get a better idea of what they are, because the > process is usually iterative, with no clear end > point. Aye, if you're still exploring your solution space your requirements are perforce somewhat nebulous. > So in theory, correctness proofs are a fine idea, > and might even be doble on a small scale, but the > real world is huge and messy! Very true, but I think it is still worthy to build larger systems from proven components combined in provably correct ways, at least to the extent possible. > > Just because it's unattainable doesn't make it undesirable. And who > > knows? Maybe the horse will learn to sing. > > Striving to find ways of writing less bugs is a > worthy goal, but I think of it more in terms of > adopting patterns of thought and programming that > make it more likely you will write code that does > what you had in mind, rather than a separate > "proof" process that you go through afterwards. I would consider formal methods exactly "patterns of thought and programming that make it more likely you will write code that does what you had in mind", in fact that's why I'm excited about them. My understanding (so far) is that you (hope to) /derive/ correct code using formal logic, rather than writing code and then proving its soundness. The real win, IMO, is teaching computers as a logical, mathematical activity (that can certainly depart for realms less rigorously defined.) I think anyone who can spontaneously solve a Sudoku puzzle has enough native grasp of "formal" methods of reasoning to learn how to derive working programs logically. Making the formal methods explicit gives you the strongest available confidence that you are reasoning, and programming, correctly. This is not to say that "incorrect" (or unproven or unprovable) programs are never useful or necessary. However, I do feel that it's better to learn the "correct" way, and then introduce ambiguity, than to simply throw e.g. Java at some poor student and let them find solid ground "catch as catch can". ~Simon From darcy at druid.net Mon Jul 20 21:18:28 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Mon, 20 Jul 2009 21:18:28 -0400 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) In-Reply-To: References: Message-ID: <20090720211828.88876711.darcy@druid.net> On 20 Jul 2009 17:10:55 -0700 aahz at pythoncraft.com (Aahz) wrote: > >I understand what you want but I can't see how a toolkit can do that. > >How do you program "graceful?" It seems pretty application specific. > > Presumably the JS toolkit generates both code and HTML. Actions that > normally get executed by JS should degrade to plain HTML links and form > submits (or possibly not display at all if it's a pure convenience). As I said, I do understand what you are after but I still think it is a judgement call. What defines "convenience?" How does the library know what is essential and what is pretty? -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From ebonak at hotmail.com Mon Jul 20 21:53:59 2009 From: ebonak at hotmail.com (Esmail) Date: Mon, 20 Jul 2009 21:53:59 -0400 Subject: comments? storing a function in an object In-Reply-To: References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: <4A651FB7.1030602@hotmail.com> Gabriel Genellina wrote: > > If you follow the above suggestions, you'll see that your Function class > becomes almost useless: a normal function already IS an object, so you > don't have to wrap it inside ANOTHER object unless you need very special > features. Hello Gabriel, In general I would agree with you, but in my specific case I want so store some additional meta-data with each function, such as the valid range for input values, where the max or minimum are located, the name/source for the function etc. I am creating list of functions for use in testing optimization code, so it seems best to store this data along with the function I am going to optimize in order to verify the results for a given range (for instance). Esmail From http Mon Jul 20 21:54:54 2009 From: http (Paul Rubin) Date: 20 Jul 2009 18:54:54 -0700 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7xljmk8q1m.fsf@ruckus.brouhaha.com> <6348dcb8-bfc1-446e-b7bf-28657e2b607e@j32g2000yqh.googlegroups.com> Message-ID: <7xws627qc1.fsf@ruckus.brouhaha.com> Simon Forman writes: > But I'm glad it's there to study, these are wheels I don't have to > invent for myself. http://dwheeler.com/essays/high-assurance-floss.html might be an interesting place to start. From ebonak at hotmail.com Mon Jul 20 22:01:45 2009 From: ebonak at hotmail.com (Esmail) Date: Mon, 20 Jul 2009 22:01:45 -0400 Subject: comments? storing a function in an object In-Reply-To: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: <4A652189.2000607@hotmail.com> Hi Francesco, Those were great suggestions! Re 1: I used the __doc__ attribute to eliminate the parameter in the constructor as you suggested. Also, much easier to specify the character string with the actual function than later to match it up like I was. class Function(object): """ class to represent a function """ def __init__(self, fn, num_vars): """ the function, its string representation, and the number of variables """ self.fn = fn self.fn_str = fn.__doc__ self.num_vars = num_vars Re 2: I will need to read up on __call__ to see how to use it here .. Re 3: I think I still need to have my own exception handler as I am using the 'extended call syntax' now (I just learned about this yesterday) and I will always just supply one argument, the list. Or perhaps my custom exception will be superfluous once I figure out __call__ ..? Thank you for taking the time to help, always good to learn new things. Esmail From aahz at pythoncraft.com Mon Jul 20 22:53:08 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 19:53:08 -0700 Subject: are user defined classes hashable? References: <373d6c69-6965-4a88-bdd2-8028ef850bf8@k6g2000yqn.googlegroups.com> Message-ID: In article <373d6c69-6965-4a88-bdd2-8028ef850bf8 at k6g2000yqn.googlegroups.com>, Hyuga wrote: > >Regardless, Nicolas's example can be applied to the class too: > >>>> class Foo(object): > pass > >>>> hash(Foo) >11443104 >>>> id(Foo) >11443104 > >class objects are just objects of type 'type'. Not quite. They certainly default that way, but changing the metaclass changes a class's type:: class M(type): pass class C(object): pass class C2(object): __metaclass__ = M print type(C) print type(C2) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From pavlovevidence at gmail.com Mon Jul 20 23:53:53 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 20 Jul 2009 20:53:53 -0700 (PDT) Subject: comments? storing a function in an object References: Message-ID: On Jul 20, 9:22?am, Esmail wrote: > def funct1(x): > ? ? ?''' small test function ''' > ? ? ?return x * x > > def funct2(x, y): > ? ? ?''' small test function ''' > ? ? ?return x + y > > def funct3(x): > ? ? ?''' small test function ''' > ? ? ?return 1000 + (x*x + x) * math.cos(x) > > def main(): > ? ? ?""" main method """ > ? ? ?print 'in main' > > ? ? ?fn1 = Function(funct1, 'x * x', 1) > ? ? ?fn2 = Function(funct2, 'x + y', 2) > ? ? ?fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) If you are defining all of the functions are in-line like this (and I assume you are because you seem to need a function object), I'd just exec the string representation. This is to support the DRY principle. You could do something like this: class Function(object): def __init__(self,args,body): ns = {} exec '''def func(%s): return %s''' in ns self.fn = ns['func'] self.fn_str = body self.num_vars = args.count(',')+1 You have to be REALLY REALLY careful not to pass any user-supplied data to it if this is a server running on your computer, of course. (If it's an application running on the user's computer it doesn't matter.) Still wouldn't be a bad idea to pass it through some kind of validator for extra protection. Carl Banks From boyd.blackwell at gmail.com Mon Jul 20 23:56:51 2009 From: boyd.blackwell at gmail.com (bdb112) Date: Mon, 20 Jul 2009 20:56:51 -0700 (PDT) Subject: clean way prepend an element to a numpy array Message-ID: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> If I want to add an element at the beginning of an array, it seems like I must make a list, insert in place, then make an array again. Of course, lists can be efficient too, and the insert() funtion works nicely in that case, but sometimes arrays are the best choice e.g. x=array([1,2,3]) # to put the element 0 at the head of the array listx=list(x) listx.insert(0,0) x=array(listx) # this extra effort distracts from the clarity of the code, and must slow things a little. # While I appreciate that array operations that cause memory re- allocation and copying are to be # avoided if at all possible, sometimes this is the best compromise between clarity and efficiency # A nicer piece of code would be x.insert(0,0) #or x.prepend(0) # It is a little easier to grow an array at the end (if it is not referenced) x.resize(4) I saw this interest syntax on this site x[:0]=0 I guess that is cute, but could be confusing....(and doesn't work) From ben+python at benfinney.id.au Tue Jul 21 00:13:55 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 21 Jul 2009 14:13:55 +1000 Subject: clean way prepend an element to a numpy array References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> Message-ID: <87eisaad18.fsf@benfinney.id.au> bdb112 writes: > If I want to add an element at the beginning of an array, it seems > like I must make a list, insert in place, then make an array again. The NumPy ?ndarray? type (which is what you get by default from the ?array? factory function) is a far more complex type than (and is not derived from) the Python list. For manipulating them, you'll want to study the NumPy documentation . -- \ ?Are you pondering what I'm pondering?? ?I think so, Brain, but | `\ I don't think Kay Ballard's in the union.? ?_Pinky and The | _o__) Brain_ | Ben Finney From gagsl-py2 at yahoo.com.ar Tue Jul 21 00:31:04 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 01:31:04 -0300 Subject: comments? storing a function in an object References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> <4A651FB7.1030602@hotmail.com> Message-ID: En Mon, 20 Jul 2009 22:53:59 -0300, Esmail escribi?: > Gabriel Genellina wrote: > > >> If you follow the above suggestions, you'll see that your Function >> class becomes almost useless: a normal function already IS an object, >> so you don't have to wrap it inside ANOTHER object unless you need very >> special features. > > Hello Gabriel, > > In general I would agree with you, but in my specific case > I want so store some additional meta-data with each function, such > as the valid range for input values, where the max or minimum are > located, > the name/source for the function etc. I am creating list of functions > for use in testing optimization code, so it seems best to store this > data along with the function I am going to optimize in order to verify > the results for a given range (for instance). You can store all meta-data in the function itself: py> def triangle(b, h): ... "area of triangle: b*h/2" ... return b*h/2 ... py> triangle.foo = "some value" py> triangle.foo 'some value' Python already knows some properties: py> triangle.func_code.co_argcount 2 py> triangle.func_doc 'area of triangle: b*h/2' py> triangle.__doc__ 'area of triangle: b*h/2' You may use this variant of Carl Banks proposal - this is a factory function that returns new function objects: def inline_function_factory(name, args, expr): ns = {} exec '''def %s(%s): %r return %s''' % ( name, args, expr, expr) in ns function = ns[name] return function py> sqr = inline_function_factory("square", "x", "x*x") py> sqr(3) 9 py> sqr.__doc__ 'x*x' py> sqr Wrapping a function object isn't necesarily bad, but perhaps you're doing things more complicated than should be. -- Gabriel Genellina From smolds at orcasoul.com Tue Jul 21 00:33:25 2009 From: smolds at orcasoul.com (OrcaSoul) Date: Mon, 20 Jul 2009 21:33:25 -0700 (PDT) Subject: Mechanize not recognized by py2exe In-Reply-To: References: <4A60C81F.7080707@comcast.net> Message-ID: <24581545.post@talk.nabble.com> Gabriel Genellina-7 wrote: > > En Fri, 17 Jul 2009 15:51:11 -0300, Stephen M. Olds > escribi?: > >> I have a Python script getData.py that uses Mechanize, and runs fine >> under the >> interpreter. It was installed using easy_install - and the install >> seemed to >> indicate it was completed. >> >> The problem is, when I try to compile it using py2exe while in the >> folder of the >> script, and using the run line command: >> >> python getData.py py2exe >> >> I get the warning: "Import error: No Module named mechanize"... >> >> I checked the environmental path and find the following: >> %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program >> Files (x86)\Python;C:\Python25\Lib\site-packages;C:\Program >> Files (x86)\Common Files\Adobe\AGL > > The system PATH is irrelevant here - it's only used by Windows to locate > the Python executable. I don't see a reason to list > C:\Python25\Lib\site-packages here. > You're mostly interested in sys.path instead (that is, the 'path' > attribute in the 'sys' Python module) > >> I did a search for mechanize and find an egg file in >> C:\Python25\Lib\site-packages >> named mechanize-0.1.7b-py2.5. >> >> Not really understanding the "egg" thing, what do I have here that needs >> to be >> done? > > I'm not an egg expert either, and I try to avoid them as a plague. But I > *think* you can delete the .egg and re-install it using easy_install with > the -Z option. > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > > Gabriel, that was the kick in the pants I needed! Thanks - after over a week searching through the same 15 web pages these old eyes simply blurred over and I missed that one...I had tried the -a option, but that's what got me the eggs... So, thanks for the help...it's too late to name my first born after you, but I did have a cat named Gabriel - she was a great cat! Stephen -- View this message in context: http://www.nabble.com/Mechanize-not-recognized-by-py2exe-tp24540012p24581545.html Sent from the Python - python-list mailing list archive at Nabble.com. From fb at frank-buss.de Tue Jul 21 00:57:39 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 06:57:39 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> Message-ID: <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Scott Burson wrote: > Have you looked at ECL? > > http://ecls.sourceforge.net/ > > I've used it only a little, so I can't vouch for its stability, but it > fits the threading and license requirements (well, some corporate > lawyers have trouble with the LGPL, but I think it's usable). I didn't tried it myself, but looks like it is not very stable: http://www.mail-archive.com/application-builder at lispniks.com/msg01069.html -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From astan.chee at al.com.au Tue Jul 21 01:02:57 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 21 Jul 2009 15:02:57 +1000 Subject: ignore special characters in python regex Message-ID: <4A654C01.2000508@al.com.au> Hi, I'm reading text from a file (per line) and I want to do a regex using these lines but I want the regex to ignore any special characters and treat them like normal strings. Is there a regex function that can do this? Here is what I have so far: fp = open('file.txt','r') notes = fp.readlines() fp.close() strin = "this is what I want" for note in notes: if re.search(r""+ str(note) + "",strin): print "Found " + str(note) + " in " + strin Thanks for any help From fb at frank-buss.de Tue Jul 21 01:08:41 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 07:08:41 +0200 Subject: any suggestions to synchronize typed text and speech ? References: Message-ID: Stef Mientki wrote: > Here the problem part: > I need to synchronize the typed text with the sound during playback. > So if I click somewhere in the sound recording, > the line of text, typed that moment should be highlighted. > And vise versa, if the cursor in the text is moved and some special key > is pressed, > the sound starting 10 or 20seconds earlier should be playbacked. You could use some screen recording tools, e.g. http://www.techsmith.com/camtasia.asp on Windows. Most tools allows you to record speech while recording the screen. -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From doomster at knuut.de Tue Jul 21 01:16:10 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Tue, 21 Jul 2009 07:16:10 +0200 Subject: C-API, tp_dictoffset vs tp_members Message-ID: <7cl194F28ojoqU1@mid.uni-berlin.de> Hi! When would I use PyObject_SetAttrString/tp_dictoffset instead of tp_members? I have a predefined set of members, some of which are optional. The problem I had with an embedded dictionary was that I can't see its elements using "dir()". Now I just converted to using tp_members, and it still seems to work correctly for the cases I tested. Even better, I can declare the fields as read-only then and add doc-strings. So, I wonder, what made the original author[2] use tp_dictoffset instead? In case you wonder, I'm looking at the Python bindings for GDB[1], file gdb/python/python-type.c, struct field_object. Thanks! Uli [1] http://sourceware.org/gdb/wiki/PythonGdb [2] Yes I could ask them, but I'd like to get an understanding of the pros and cons. From fb at frank-buss.de Tue Jul 21 01:18:01 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 07:18:01 +0200 Subject: ignore special characters in python regex References: Message-ID: <13459wwzhbzoo$.jei131n3o527.dlg@40tude.net> Astan Chee wrote: > I'm reading text from a file (per line) and I want to do a regex using > these lines but I want the regex to ignore any special characters and > treat them like normal strings. > Is there a regex function that can do this? Maybe re.escape helps? -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From boyd.blackwell at gmail.com Tue Jul 21 01:41:13 2009 From: boyd.blackwell at gmail.com (bdb112) Date: Mon, 20 Jul 2009 22:41:13 -0700 (PDT) Subject: clean way prepend an element to a numpy array References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <87eisaad18.fsf@benfinney.id.au> Message-ID: <608f857f-3925-44db-9e5e-8969b6a116af@b25g2000prb.googlegroups.com> On Jul 21, 2:13?pm, Ben Finney wrote: > bdb112 writes: > > If I want to add an element at the beginning of an array, it seems > > like I must make a list, insert in place, then make an array again. > > The NumPy ?ndarray? type (which is what you get by default from the > ?array? factory function) is a far more complex type than (and is not > derived from) the Python list. > > For manipulating them, you'll want to study the NumPy documentation > . > Yes, I had had a look through that - nothing there that allows writing a succint clear statement (except for my .resize example in the original post), although if efficiency was a concern, then maybe resize and shift right, but that would really obscure the code. From sjmachin at lexicon.net Tue Jul 21 02:01:38 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 23:01:38 -0700 (PDT) Subject: ignore special characters in python regex References: Message-ID: On Jul 21, 3:02?pm, Astan Chee wrote: > Hi, > I'm reading text from a file (per line) and I want to do a regex using > these lines but I want the regex to ignore any special characters and > treat them like normal strings. > Is there a regex function that can do this? It would help if you were to say (1) what "ignore ... characters" means -- pretend they don't exist? (2) what are "special chararacters" -- non-alphanumeric? (3) what "treat them like normal strings" means (4) how you expect these special characters to be (a) ignored and (b) "treated like normal strings" /at the same time/. Cheers, John From cmpython at gmail.com Tue Jul 21 02:11:33 2009 From: cmpython at gmail.com (Che M) Date: Mon, 20 Jul 2009 23:11:33 -0700 (PDT) Subject: any suggestions to synchronize typed text and speech ? References: Message-ID: <9eb00b5c-f51b-45ab-9e1a-1b1691a7e538@h18g2000yqj.googlegroups.com> On Jul 19, 4:15?pm, Stef Mientki wrote: > hello, > > I'm using Scintilla as a wxPython widget with great pleasure. > I now have an application where I want to make notes during a conversation, > but also want to record the speech during that conversation. > I'm using Scintilla as a wxPython widget for editing and PyAudio for the > speech recording, > until so far everything works fine. > > Here the problem part: > I need to synchronize the typed text with the sound during playback. > So if I click somewhere in the sound recording, > the line of text, typed that moment should be highlighted. > And vise versa, if the cursor in the text is moved and some special key > is pressed, > the sound starting 10 or 20seconds earlier should be playbacked. > > I though of adding bookmarks (because these are fixed in the text), and > keep a list of bookmarks and sound pointers. > This idea should work, but there are only 31 bookmarks. > > Any other suggestions ? > > thanks, > Stef Mientki Stef, it occurs to me now that you might be useful to look at Transana: http://www.transana.org/ >From their site: Transana is software for professional researchers who want to analyze digital video or audio data. Transana lets you analyze and manage your data in very sophisticated ways. Transcribe it, identify analytically interesting clips, assign keywords to clips, arrange and rearrange clips, create complex collections of interrelated clips, explore relationships between applied keywords, and share your analysis with colleagues. It is written in Python, uses Scintilla, and is open source (GPL): http://www.transana.org/developers/index.htm Che From ivlenin at gmail.com Tue Jul 21 02:24:05 2009 From: ivlenin at gmail.com (I V) Date: 21 Jul 2009 08:24:05 +0200 Subject: comments? storing a function in an object References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: <4a655f05$1@news.x-privat.org> On Mon, 20 Jul 2009 21:53:59 -0400, Esmail wrote: > In general I would agree with you, but in my specific case I want so > store some additional meta-data with each function, such as the valid > range for input values, where the max or minimum are located, the > name/source for the function etc. I am creating list of functions for > use in testing optimization code, so it seems best to store this data > along with the function I am going to optimize in order to verify the > results for a given range (for instance). You can set attributes on functions, so if your class is just storing this data, rather than altering the behavior of the function, you may be able to just use a function: def f(x, y): "x * y" return x * y f.arguments_max = [1000, 255] A function also already knows how many arguments it takes, which you can access as f.func_code.co_argcount From jayshree06comp at gmail.com Tue Jul 21 02:29:06 2009 From: jayshree06comp at gmail.com (jayshree) Date: Mon, 20 Jul 2009 23:29:06 -0700 (PDT) Subject: python function for retrieving key and encryption Message-ID: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> M2Crypto package not showing the 'recipient_public_key.pem' file at linux terminal .how do i get/connect with recipient public key. exactly i need to check how can i open this file through linux commands. import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open ('recipient_public_key.pem','rb').read()) print recip; plaintext = whatever i need to encrypt msg = recip.public_encrypt (plaintext,RSA.pkcs1_padding) print msg; after calling the function its not giving any output and even any error i also tried as 'Will' said pk = open('public_key.pem','rb').read() print pk; rsa = M2Crypto.RSA.load_pub_key(pk) whats the mistake i am not getting .will somebody help me out. is something wrong in opening 'recipient_public_key.pem'. is M2Crypto contain this file inbuilt .from where this file taking public key of the recipient i'. what this file contain and how it will work .should i need to create such a file for my purpose.if it is then how can i create this and how it will retrieve the key,where i recognize my recipient to get his public key .is something like database at server. please give me a quick response...... looking for your answer. From jackdied at gmail.com Tue Jul 21 02:42:02 2009 From: jackdied at gmail.com (Jack Diederich) Date: Tue, 21 Jul 2009 02:42:02 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: <02746997$0$5185$c3e8da3@news.astraweb.com> References: <02746997$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Mon, Jul 20, 2009 at 10:00 AM, Steven D'Aprano wrote: > On Mon, 20 Jul 2009 09:34:24 +0000, Sion Arrowsmith wrote: > >> Terry Reedy ? wrote: >>>Sion Arrowsmith wrote: >>>> Jack Diederich ? wrote: >>>>> It isn't an OrderedDict thing, it is a comparison thing. ?Two regular >>>>> dicts also raise an error if you try to LT them. >>>> Python 2.5.2 >>>>>>> d1 = dict((str(i), i) for i in range (10)) d2 = dict((str(i), i) >>>>>>> for i in range (20)) d1 < d2 >>>> True >>>Try reversing the definitions of d1 and d2. The dicts are probably being >>>compared by id (address), which is the 2.x CPython default. >> >> Like this? >> >>>>> d1 = dict((str(i), i) for i in range (20)) >>>>> d2 = dict((str(i), i) for i in range (10)) >>>>> d1 < d2 >> False >>>>> id(d1) < id(d2) >> True >> >> I didn't know that comparison for anything other than equality defaulted >> to using id. That really is rather broken, and I'm glad 3.0 fixed it. > > > I don't think comparisons other than equality use id. That would be > rather insane. If anyone can demonstrate such comparisons in a built-in > or standard library class, I'd like to see it. > > > For the record, dicts have been comparable with < and > since at least > Python 1.5: > > $ python1.5 > Python 1.5.2 (#1, Apr ?1 2009, 22:55:54) ?[GCC 4.1.2 20070925 (Red Hat > 4.1.2-27)] on linux2 > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>> >>>> {1: 'a', 2: 'b'} < {2: 'b', 3: 'a'} > 1 > > > Reading the docs: > > http://docs.python.org/library/stdtypes.html#comparisons > http://docs.python.org/library/stdtypes.html#mapping-types-dict > > I can only suggest that dicts compare in an arbitrary but consistent > fashion. I should have specified 3.x but since we were talking about OrderedDicts (only in 3.1) I didn't say it explicitly. Earlier versions of python were very permissive with comparisons -- it was rare that any cmp() raised an error and the ultimate fallback was on the object's id. This is one of the non-backwards compatible changes in 3k. Now comparing two of the same thing that don't have an obvious ordering is an error. Is a dict "greater than" if it has a larger size? if its max key is larger? what does "max key" mean when the keys aren't even comparable?. Comparing things that aren't extremely similar is an error now also. -Jack From astan.chee at al.com.au Tue Jul 21 02:59:16 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 21 Jul 2009 16:59:16 +1000 Subject: ignore special characters in python regex In-Reply-To: References: Message-ID: <4A656744.7050908@al.com.au> I think the re.escape did the trick. to answer your questions: By "ignore" i meant instead of using non-alphanumeric characters that have special significance in regular expression (e.g. [|\]) and treat them as normal strings (i.e preceded by \), but since I don't know all the characters in regular expression that have special significance, I don't know which ones to add a '\' infront of. Thanks anyway John Machin wrote: > On Jul 21, 3:02 pm, Astan Chee wrote: > >> Hi, >> I'm reading text from a file (per line) and I want to do a regex using >> these lines but I want the regex to ignore any special characters and >> treat them like normal strings. >> Is there a regex function that can do this? >> > > It would help if you were to say > > (1) what "ignore ... characters" means -- pretend they don't exist? > (2) what are "special chararacters" -- non-alphanumeric? > (3) what "treat them like normal strings" means > (4) how you expect these special characters to be (a) ignored and (b) > "treated like normal strings" /at the same time/. > > Cheers, > John > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joswig at lisp.de Tue Jul 21 03:00:16 2009 From: joswig at lisp.de (Rainer Joswig) Date: Tue, 21 Jul 2009 00:00:16 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Message-ID: On 21 Jul., 06:57, Frank Buss wrote: > Scott Burson wrote: > > Have you looked at ECL? > > >http://ecls.sourceforge.net/ > > > I've used it only a little, so I can't vouch for its stability, but it > > fits the threading and license requirements (well, some corporate > > lawyers have trouble with the LGPL, but I think it's usable). > > I didn't tried it myself, but looks like it is not very stable: > > http://www.mail-archive.com/application-buil... at lispniks.com/msg01069.html I'm not sure if it is fair to post a reference to a single post by someone without context and without having used ECL. If there are stability problems, people can report to the ECL mailing list. The maintainers are very active. Frank, I have seen you constructive and posting code a year ago. What happened? Several messages of yours I've seen here are now going in the direction of been mostly useless. I thought you could do better. If you are no longer interested in Lisp with no first hand usage, why not switch to comp.lang.misc or some other random place? From hv at tbz-pariv.de Tue Jul 21 03:03:23 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Tue, 21 Jul 2009 09:03:23 +0200 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: <7cl7hrF28lsb2U1@mid.individual.net> casebash schrieb: > Hi, > > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? Hi, you could use a list of characters. It would not be difficult to implement this as a class with all fancy methods like startswith() ... Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From googler.1.webmaster at spamgourmet.com Tue Jul 21 03:22:27 2009 From: googler.1.webmaster at spamgourmet.com (moerchendiser2k3) Date: Tue, 21 Jul 2009 00:22:27 -0700 (PDT) Subject: PIL for OSX 64bit Message-ID: Hi, I have a problem with Python and the Python Image Library to get it work on OSX 64 bit. Maybe anyone can help me? I started Python in 64 bit mode and called Image.open(...).load() and got: ImportError: The _imaging C module is not installed Yes, it seems PIL is not available in 64 bit on my system, but I can't compile it in any way. Is there a tutorial or something like that which explains how I can compile it in 64 bit? I found a workaround where I added this function to the setup.py OrigExtension = Extension def Extension(*args, **kwargs): extra_args = ['-arch', 'x86_64'] kwargs['extra_compile_args'] = extra_args + kwargs.get ('extra_compile_args', []) kwargs['extra_link_args'] = extra_args + kwargs.get('extra_link_args', []) return OrigExtension(*args, **kwargs) without success. Can anyone help me? Thanks... Cheers, Googler From dtgeadamo at yahoo.com Tue Jul 21 03:27:45 2009 From: dtgeadamo at yahoo.com (sightseer) Date: Tue, 21 Jul 2009 00:27:45 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> Message-ID: <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> On Jul 20, 5:25?pm, "David Adamo Jr." wrote: > On Jul 20, 5:14?pm, Tim Golden wrote: > > > > > mistersexy wrote: > > > On Jul 20, 3:03 pm, Tim Golden wrote: > > >> mistersexy wrote: > > >>> I am trying to create a Windows service in Python using pywin32. I do > > >>> not want this service to run under a user account. I want this service > > >>> to be able to run as a LocalService, NetworkService and the like. How > > >>> do I specify this using the win32 library? Thanks, everyone. > > >> When you "install" the service, using the HandleCommandLine > > >> option, specify --username= and --password options. > > > >> TJG > > > > That's exactly my point. I do not want to have to specify username and > > > password options. For instance, when creating a Windows Service > > > in .NET, it is possible to specify that the service should run using > > > the LocalService or NetworkService account. Doing this, you would not > > > need to specify username and password options. Is there a way to > > > achieve this in Python? > > > Sorry, I misread: I mentally removed the "not" in your 'I do not want > > this service to run under a user account' and reinserted it > > further on! > > > By default, the service will run as LocalSystem: you > > only specify a username to override that default. The value > > in Username is passed straight through to the CreateService > > Win32 API, and the docs for that: > > > ?http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx > > > say: > > > """ > > lpServiceStartName [in, optional] > > > ? ? The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName. > > > ? ? If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account. > > > ? ? If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account. > > > ? ? A shared process can run as any user. > > > ? ? If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system. > > > ? ? A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide. > > > ? ? ? ? Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: ?Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2. > > """ > > > So, although I haven't tried it, it looks as though you can pass > > "LocalService" or "NetworkService" and so on if you want to > > override the default LocalSystem but don't want to specify a > > username/password. > > > TJG > > I'll try this stuff. Thanks a million...I'll let everyone know how it > goes. I'm finding it hard to install my Python service. When I try to install the service, this is what I get: Error Installing Service: Access is Denied. (5) Event with a correct username and password (I think). Also, inheriting from the win32serviceutil.Framework class, I do not see a way to explicitly specify the serviceType. I know some of the parameters such as the startupType can be specified when installing the service in the command line, but how are these things specified in the service code when inheriting from win32serviceutil.Framework (especially the serviceType). Does this mean I would have to code my service from scratch without using win32serviceutil.Framework? Basically, I still can't install my service using a NetworkService or LocalSystem Account because it still requires me to specify a username and password. Following Tim Golden's advice, I'm trying to specify the service type in order to determine what account to run the service under. Please help! From hendrik at microcorp.co.za Tue Jul 21 04:03:46 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Tue, 21 Jul 2009 10:03:46 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: <200907211003.46253.hendrik@microcorp.co.za> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: > On Jul 20, 6:08?pm, Duncan Booth wrote: > > The main reason why you need both lists and tuples is that because a > > tuple of immutable objects is itself immutable you can use it as a > > dictionary key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. simplest is something like a point in the cartesian plane with an associated attribute like colour. - Hendrik From wilk at flibuste.net Tue Jul 21 04:19:29 2009 From: wilk at flibuste.net (William Dode) Date: 21 Jul 2009 08:19:29 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> Message-ID: <4a657a11$0$417$426a74cc@news.free.fr> On 20-07-2009, srepmub wrote: > >> Nice timings, can you please show me the Python, Java and C code >> versions? I may do more tests. Of course, the codes are here : http://hg.flibuste.net/libre/games/cheval Like you'll see, i tried to use exactly the same code for each langage. > > also, which shedskin options did you use? did you use -bw, to disable > bounds and wrap-around checking? this can make quite a difference for > code that does a lot of indexing. if the code uses random numbers, > then -r can also make a big difference, to use C rand(), instead of > Python compatible random numbers. > > and which C++ compiler flags did you use? the default -O2, or > something like -O3 -fomit-frame-pointer -msse2..? I used the default, shedksin cheval.py; make shedskin 0.2 With -bw and -O3 -fomit-frame-pointer -msse2 i have 5.5s (instead of 8) Let me know if you find better. -- William Dod? - http://flibuste.net Informaticien Ind?pendant From wilk at flibuste.net Tue Jul 21 04:20:29 2009 From: wilk at flibuste.net (William Dode) Date: 21 Jul 2009 08:20:29 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <04f03d96-46b7-4761-8498-9149f19ecba6@k6g2000yqn.googlegroups.com> Message-ID: <4a657a4d$0$417$426a74cc@news.free.fr> On 20-07-2009, Bearophile wrote: > Skip Montanaro: >> I read just enough French to know that "avec" means "with", but I don't >> understand the difference between "avec malloc *int" and "avec []". ?Can you >> explain please? > > Maybe it's the time difference between using a Python list from Cython > and using a C "array" allocated with a malloc from Cython. yes, it's this -- William Dod? - http://flibuste.net Informaticien Ind?pendant From ben+python at benfinney.id.au Tue Jul 21 04:36:14 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 21 Jul 2009 18:36:14 +1000 Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <47ab4ab6-d8c8-4997-8963-ac41b00dd10f@y10g2000prg.googlegroups.com> Message-ID: <87ws628mbl.fsf@benfinney.id.au> John Machin writes: > OK, I'll bite: where does the Python 3.x bytearray type I wasn't previously aware of it, thanks for bringing it to my attention . > fit into your taxonomy? At first glance it appears to be mutable and > have a fair swag of functionality. Yes. I'd classify it as a mutable array of bytes :-) but, as such, not to be used as a text string. -- \ ?Science doesn't work by vote and it doesn't work by | `\ authority.? ?Richard Dawkins, _Big Mistake_ (The Guardian, | _o__) 2006-12-27) | Ben Finney From ben+python at benfinney.id.au Tue Jul 21 04:38:31 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 21 Jul 2009 18:38:31 +1000 Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> Message-ID: <87skgq8m7s.fsf@benfinney.id.au> Steven D'Aprano writes: > On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote: > > A mutable string would not (AFAICT) be usefully implementable as a > > subclass of the built-in string types. So even if such a type > > existed, it would not be useable with all the functionality that > > works with strings. > > If applications ignore duck-typing and do isinstance(value, str), it's > arguably the application and not the value that is broken. Agreed, I wouldn't advise that. My point was rather meant to imply that subclassing the built-in (immutable) string types was the best way to usefully get all their functionality, rather than re-implementing most of it. -- \ ?I went to a restaurant that serves ?breakfast at any time?. So | `\ I ordered French Toast during the Renaissance.? ?Steven Wright | _o__) | Ben Finney From dickinsm at gmail.com Tue Jul 21 04:51:50 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 21 Jul 2009 01:51:50 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> <24543805.post@talk.nabble.com> Message-ID: <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> On Jul 20, 11:34?pm, Ethan Furman wrote: > Dr. Phillip M. Feldman wrote: > ?> Suppose that 'xor' returns the value that is true when only one value is > ?> true, and False otherwise. ?This definition of xor doesn't have the > standard > ?> associative property, that is, > ?> > ?> (a xor b) xor c > ?> > ?> will not necessarily equal > ?> > ?> a xor (b xor c) > ?> > ?> To see that this is the case, let a= 1, b= 2, and c= 3. > ?> > ?> (a xor b) xor c > ?> > ?> yields 3, while > ?> > ?> a xor (b xor c) > ?> > ?> yields 1. ?So, I'd prefer an xor operator that simply returns True or > False. > ?> > ?> Phillip > ?> > > You are, of course, free to write your version however it makes sense to > you and your team. ?:) > > Since the 'and' and 'or' already return objects (and objects evaluate to > true or false), then 'xor' should behave likewise, IMO. ?I expect that > would be the case if it were ever added to the language. I'm not so sure. Did you ever wonder why the any() and all() functions introduced in 2.5 return a boolean rather than returning one of their arguments? (I did, and I'm still not sure what the answer is.) Mark From juanjose.garciaripoll at googlemail.com Tue Jul 21 05:23:59 2009 From: juanjose.garciaripoll at googlemail.com (Juanjo) Date: Tue, 21 Jul 2009 02:23:59 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Message-ID: <56721e57-e0b4-4d7d-8693-a96d1dca061b@j32g2000yqh.googlegroups.com> On Jul 21, 6:57?am, Frank Buss wrote: > Scott Burson wrote: > > Have you looked atECL? > > >http://ecls.sourceforge.net/ > > > I've used it only a little, so I can't vouch for its stability, but it > > fits the threading and license requirements (well, some corporate > > lawyers have trouble with the LGPL, but I think it's usable). > > I didn't tried it myself, but looks like it is not very stable: > > http://www.mail-archive.com/application-builder at lispniks.com/msg01069... ECL suffers from fast development problems if this is what you mean. People are advised to stay with certain releases and we announce when some ports are broken due to progress along certain lines. For instance, if I find that the generational garbage collector is needed for Linux, FreeBSD, OpenBSD and OS X, and it works, I do not mind dropping temporarily the mingw port until the garbage collector library catches up. People who wanted to stay with mingw produced a branch (see Samium's posts) with the old garbage collector. Now this is not so dramatic. ECL now has a release cycle of ONE MONTH. If you find that this month's release does not work on your platform (and this is normally explicit in the announcement), then do not upgrade and wait one or two months until the problem is solved. OTOH, people only seem to notice problems when their petty platform is temporarily broken, but nobody seems to notice the overall stability and portability of the platform. See the list http://ecls.sourceforge.net/logs.html which will soon expand including Solaris and regular builds on Windows using the free Microsoft compiler. And once the ARM computer I have been gifted by a happy arrives, then Debian-ARM as well. Juanjo From milanj at gmail.com Tue Jul 21 05:37:27 2009 From: milanj at gmail.com (milanj) Date: Tue, 21 Jul 2009 02:37:27 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> On Jul 19, 8:31?pm, Frank Buss wrote: > Python is not that bad. Unlike Lisp, there is much less undefined behavior, > there is one free unique implementation on the 3 major platforms Linux, > Windows and MacOS X, which is stable, support multithreading and has a > default GUI library binding, which is difficult to find for Lisp (e.g. I > don't know of a free modern and stable Lisp implemenation with > mulithreading support for Windows, with a licence with which you can use it > in closed source commercial programs, like you can do with Python). Many > problems in the Lispbuilder mailing list are related to problems due to > different operating systems and Lisp implementations. > Frank Buss, f... at frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de Someone should mention Clozure CL - http://trac.clozure.com/openmcl As you can see there is os x, freebsd, linux, solaris and windows port and all of them use native threads (python still use green threads ?) and development is pretty alive, they did planty of developing last year[s], ccl licence permits you to deliver closed source programs ... CCL is promising bright feature to CL since looks like the insist of building stable implementation across most arch in use today From martin.hellwig at dcuktec.org Tue Jul 21 05:40:54 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 21 Jul 2009 10:40:54 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> Message-ID: sightseer wrote: > > Error Installing Service: Access is Denied. (5) > Are you trying to do this on windows vista? -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From gagsl-py2 at yahoo.com.ar Tue Jul 21 05:56:33 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 06:56:33 -0300 Subject: ignore special characters in python regex References: <4A654C01.2000508@al.com.au> Message-ID: En Tue, 21 Jul 2009 02:02:57 -0300, Astan Chee escribi?: > I'm reading text from a file (per line) and I want to do a regex using > these lines but I want the regex to ignore any special characters and > treat them like normal strings. > Is there a regex function that can do this? > Here is what I have so far: > fp = open('file.txt','r') > notes = fp.readlines() > fp.close() > strin = "this is what I want" > for note in notes: > if re.search(r""+ str(note) + "",strin): > print "Found " + str(note) + " in " + strin You don't even need a regex for that. py> "fragil" in "supercalifragilisticexpialidocious" True Note that: r""+ str(note) + "" is the same as: str(note) which in turn is the same as: note Remember that each line keeps its '\n' final! for note in notes: if note.rstrip('\n') in strin: print "Found %s in %s" % (note, strin) -- Gabriel Genellina From duncan.booth at invalid.invalid Tue Jul 21 06:28:16 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 21 Jul 2009 10:28:16 GMT Subject: Help understanding the decisions *behind* python? References: <87hbx7yp16.fsf@busola.homelinux.net> Message-ID: Hrvoje Niksic wrote: > Chris Rebert writes: > >>>>>>>> x = [2,1,3] >>>>>>>> print sorted(x)[0] >>>>DB> 3 >>> >>> What kind of Python produces that? >> >> Assuming you're referring to the latter example, it was added in >> version 2.4 If you meant the former example, I think that's purely >> pseudo-Python. > > sorted([2, 1, 3])[0] evaluates to 1, not 3. > I guess you can tell I broke my unbreakable rule to never post untested code without labelling it as such. :-) -- Duncan Booth http://kupuguy.blogspot.com From jeanmichel at sequans.com Tue Jul 21 06:36:14 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 21 Jul 2009 12:36:14 +0200 Subject: How to import pydoc and then use it? In-Reply-To: <1248126024.9281.3.camel@centar> References: <1248126024.9281.3.camel@centar> Message-ID: <4A659A1E.9090509@sequans.com> Albert Hopkins wrote: > On Mon, 2009-07-20 at 13:38 -0700, mrstevegross wrote: > >> I know how to use pydoc from the command line. However, because of >> complicated environmental setup, it would be preferable to run it >> within a python script as a native API call. That is, my python runner >> looks a bit like this: >> >> import pydoc >> pydoc.generate_html_docs_for(someFile) >> >> However, it's not clear to me from the pydoc documentation which >> function calls I need to use to make this behavior work. Any ideas? >> > > Did you try 'pydoc pydoc'? ;) > > >>>> import pydoc >>>> htmldoc = pydoc.HTMLDoc() >>>> htmldoc_for_pydoc = htmldoc(pydoc) >>>> print htmldoc_for_pydoc >>>> > > By the way, just in case the OP didn't know, there is the epydoc module (google it) which is doing basically the same things than pydoc, but with a much better look and feel to my opinion. JM From mark.dufour at gmail.com Tue Jul 21 08:25:01 2009 From: mark.dufour at gmail.com (srepmub) Date: Tue, 21 Jul 2009 05:25:01 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> Message-ID: <60222088-2059-49b1-a442-a56fc85d6838@g31g2000yqc.googlegroups.com> > With -bw and -O3 -fomit-frame-pointer -msse2 i have 5.5s (instead of 8) > > Let me know if you find better. thanks. now I'm wondering how fast does the C version become with these flags..? :-) mark. From nick at craig-wood.com Tue Jul 21 08:29:58 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Tue, 21 Jul 2009 07:29:58 -0500 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: Mark Dufour wrote: > I have just released version 0.2 of Shed Skin, an experimental > (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). > It comes with 7 new example programs (for a total of 40 example > programs, at over 12,000 lines) and several important improvements/bug > fixes. See http://code.google.com/p/shedskin/wiki/ReleaseNotes for the > full changelog. Cool! I tried it on a program I wrote to solve a puzzle (Rush Hour). (Actually it solves the meta-puzzle - trying to make the hardest possible Rush Hour puzzle.) After a bit of twiddling (remove psyco and profiling) I got it to start compiling, but unfortunately it compiled for about an hour (in iterative type analysis..) used up 600 MB of RAM printing an '*' every 10 minutes or so then gave an error message and gave up. Unfortunately I shut the window by accident, but the error message was something about not being able to resolve types I think with a list of 20 or so unresolved types. Can you give a hint as to how I debug this? I presume my program has some instances of non static types which is causing the problem, but it is going to be a very long debugging session if it takes me an hour each cycle ;-) The program is about 700 lines of python (excluding comments). Thanks Nick -- Nick Craig-Wood -- http://www.craig-wood.com/nick From wilk at flibuste.net Tue Jul 21 08:44:30 2009 From: wilk at flibuste.net (William Dode) Date: 21 Jul 2009 12:44:30 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <60222088-2059-49b1-a442-a56fc85d6838@g31g2000yqc.googlegroups.com> Message-ID: <4a65b82e$0$11946$426a74cc@news.free.fr> On 21-07-2009, srepmub wrote: > >> With -bw and -O3 -fomit-frame-pointer -msse2 i have 5.5s (instead of 8) >> >> Let me know if you find better. > > thanks. now I'm wondering how fast does the C version become with > these flags..? :-) I don't see any difference... -- William Dod? - http://flibuste.net Informaticien Ind?pendant From dtgeadamo at yahoo.com Tue Jul 21 08:49:45 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Tue, 21 Jul 2009 05:49:45 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> Message-ID: <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> On Jul 21, 10:40?am, "Martin P. Hellwig" wrote: > sightseer wrote: > > > > > Error Installing Service: Access is Denied. (5) > > > Are you trying to do this on windows vista? > > -- > MPHhttp://blog.dcuktec.com > 'If consumed, best digested with added seasoning to own preference.' Yeah, I was trying to do it on Vista. Someone has just helped me out. I had to deactivate User Account Control on Windows Vista...and now everything is rosy. Thanks guys. From craig1.boyd at citi.com Tue Jul 21 08:52:19 2009 From: craig1.boyd at citi.com (Boyd, Craig1 ) Date: Tue, 21 Jul 2009 07:52:19 -0500 Subject: Auto-generate GUI from a database table structure In-Reply-To: References: <55660F9659966544BBA3AE14CE5D278D01243B8E04@exgtmb08.nam.nsroot.net> Message-ID: <55660F9659966544BBA3AE14CE5D278D01243B8E11@exgtmb08.nam.nsroot.net> Gabriel, Thanks for pointing this out. It does not look like it supports Oracle at this time, which is one of my requirements, but that is based on doing a quick read of the wiki. I will dig in further and see. Perhaps it supports ODBC which would be an acceptable work-around. Thanks again, Craig -----Original Message----- From: python-list-bounces+craig1.boyd=citi.com at python.org [mailto:python-list-bounces+craig1.boyd=citi.com at python.org] On Behalf Of Gabriel Genellina Sent: Monday, July 20, 2009 4:40 PM To: python-list at python.org Subject: Re: Auto-generate GUI from a database table structure En Mon, 20 Jul 2009 12:09:47 -0300, Boyd, Craig1 escribi?: > I am trying to write a couple screens to update three or four database > tables on Oracle 10g and I was wondering if there was a way to > automatically generate some kind of GUI shell based on the tables that > I could then fill in with the logic / functionality I need. I've never actually used it, but the DABO project seems to provide what you want: http://dabodev.com/ -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list From paul at boddie.org.uk Tue Jul 21 08:55:12 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Tue, 21 Jul 2009 05:55:12 -0700 (PDT) Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: <1c994086-8c58-488f-b3b3-6161c4b2ba05@k30g2000yqf.googlegroups.com> On 20 Jul, 18:00, a... at pythoncraft.com (Aahz) wrote: > > Out of curiosity, are there any JavaScript toolkits that generate code > that degrades gracefully when JavaScript is disabled? You mean "Web toolkits which use JavaScript", I presume. I have written (and use myself) a toolkit/framework called XSLForms (part of the XSLTools distribution) which supports in-page updates (AJAX, effectively) that degrade to vanilla whole-page requests if JavaScript is switched off: http://www.boddie.org.uk/python/XSLTools.html I've been meaning to have a few example applications running on that site to illustrate the point. Some people complain that by constraining the JavaScript functionality to merely enhance vanilla HTML/HTTP functionality, one is not using the full potential of the medium, but having used sites whose JavaScript has failed, thus preventing the use of those sites almost completely, I think that stacking AJAX-like stuff on top of normal stuff helps the majority of developers offer an accessible solution. Paul From rashidsdpk at gmail.com Tue Jul 21 08:56:21 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Tue, 21 Jul 2009 05:56:21 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: <2ea811b0-8135-4df2-b3c1-964414291341@g31g2000yqc.googlegroups.com> Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10.more link http://0nanotechnology0.blogspot.com/ From c.c.wood at gmail.com Tue Jul 21 09:22:30 2009 From: c.c.wood at gmail.com (ChrisW) Date: Tue, 21 Jul 2009 06:22:30 -0700 (PDT) Subject: Multiple versions of python Message-ID: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Hi, I have installed 2 versions of python on my Windows XP computer - I originally had 3.0.1, but then found that the MySQL module only supported 2.*, so I've now installed that. I have found that if I change the Windows Environment Variable path, then I can change the version of python called when I type 'python' into a command line. However, I'd like to be able to choose which version I use. I know that if I change C:\Python26\python.exe to C:\Python26\python2.exe and C:\Python30\python.exe to C: \Python26\python3.exe, then typing 'python2' or 'python3' will invoke the correct interpreter. However, is it safe just to rename the executable files? Is there a more elegant way to achieve the same task? Thanks, Chris From peter.fodrek at stuba.sk Tue Jul 21 09:30:47 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Tue, 21 Jul 2009 15:30:47 +0200 Subject: Regular expression Message-ID: <200907211530.47704.peter.fodrek@stuba.sk> Dear conference! I have third party regular expression self.pattern_main = re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| \#\d+\=(?:[+])?\d*(?:\.\d*)?)') with code def readline(self): self.line = self.file_in.readline().rstrip() if (len(self.line)) : return True else : return False while (self.readline()): words = self.pattern_main.findall(self.line) This handles text file like // remark PL_OFF PARK FS MA 52.8806 , 18.0914 SS AD_W PL_ON C 52.3955 , -16.1511 , -90.0000 It handles file correctly with two exceptions 1) omits ',' in the a output 2) omits minus sign in the numbers... Would anyone recommend me what to change regular expression to add thesee two think to the output,please? Thank you for any help. I look forward hearing form you Yours faithfully Peter Fodrek From Scott.Daniels at Acm.Org Tue Jul 21 09:46:12 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 21 Jul 2009 06:46:12 -0700 Subject: Mechanize not recognized by py2exe In-Reply-To: References: <4A60C81F.7080707@comcast.net> Message-ID: OrcaSoul wrote: > ...it's too late to name my first born after you, but > I did have a cat named Gabriel - she was a great cat! Remember Gabriel in English uses a hard A, as in the horn player, not Gabrielle. I know because when I first read his posts I made the same trick in my head, and hence imagined a woman. I suspect it would come to irk one almost enough to become a Gabe. --Scott David Daniels Scott.Daniels at Acm.Org From inky788 at gmail.com Tue Jul 21 09:49:59 2009 From: inky788 at gmail.com (Inky 788) Date: Tue, 21 Jul 2009 06:49:59 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> On Jul 20, 12:27?pm, Phillip B Oldham wrote: > [snip] We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. My guess is that it was probably for optimization reasons long ago. I've never heard a *good* reason why Python needs both. From Scott.Daniels at Acm.Org Tue Jul 21 10:03:29 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 21 Jul 2009 07:03:29 -0700 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <02746e73$0$5185$c3e8da3@news.astraweb.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote: >> What is it you're trying to do that makes you search for a mutable >> string type? It's likely that a better approach can be found. > > When dealing with very large strings, it is wasteful to have to duplicate > the entire string just to mutate a single character. > > However, when dealing with very large strings, it's arguably better to > use the rope data structure instead. The general problem is that whether strings are mutable or not is an early language design decision, and few languages provide both. Mutable strings need lots of data copying to be safe passing args to unknown functions; immutable strings need lots of copying for ncremental changes. The rope is a great idea for some cases. I'd argue Python works better with immutable strings, because Python is too slow at per-character operations to be running up and down strings a character at a time, changing here and there. So it becomes more natural to deal with strings as chunks to pass around, and it is nice not to have to copy the strings when doing that passing around. --Scott David Daniels Scott.Daniels at Acm.Org From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Tue Jul 21 10:13:00 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Tue, 21 Jul 2009 10:13:00 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: On 2009-07-21 05:37:27 -0400, milanj said: > Someone should mention Clozure CL - http://trac.clozure.com/openmcl > As you can see there is os x, freebsd, linux, solaris and windows port > and all of them use native threads (python still use green threads ?) > and development is pretty alive, they did planty of developing last > year[s], ccl licence permits you to deliver closed source programs ... > CCL is promising bright feature to CL since looks like the insist of > building stable implementation across most arch in use today Hear, hear! -- Raffael Cavallaro From Scott.Daniels at Acm.Org Tue Jul 21 10:17:13 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 21 Jul 2009 07:17:13 -0700 Subject: Multiple versions of python In-Reply-To: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: ChrisW wrote: > Hi, > > I have installed 2 versions of python on my Windows XP computer - I > originally had 3.0.1, but then found that the MySQL module only > supported 2.*, so I've now installed that. I have found that if I > change the Windows Environment Variable path, then I can change the > version of python called when I type 'python' into a command line. > However, I'd like to be able to choose which version I use. I know > that if I change C:\Python26\python.exe to > C:\Python26\python2.exe and C:\Python30\python.exe to C: > \Python26\python3.exe, then typing 'python2' or 'python3' will invoke > the correct interpreter. However, is it safe just to rename the > executable files? Is there a more elegant way to achieve the same > task? I wouldn't rename them. You can, of course, copy them (so you have two executables), or you can pick a somedir on your path (I made a directory "C:\cmds" that I add to my path, but tastes vary). C:> copy con \py25.cmd C:\Python25\python\python.exe %* ^Z C:> copy con \py31.cmd C:\Python31\python\python.exe %* ^Z I'd use the two-digit form, as that is where interface changes happen; trying code with py24, py25, py26 can be convenient. By the way, install Python 3.1 rather than 3.0; think of 3.0 as the alpha of the 3.X branches (it will get no love at all now that 3.1 is out). --Scott David Daniels Scott.Daniels at Acm.Org From davea at dejaviewphoto.com Tue Jul 21 10:19:42 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Tue, 21 Jul 2009 10:19:42 -0400 Subject: Multiple versions of python In-Reply-To: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: <4A65CE7E.6090800@dejaviewphoto.com> ChrisW wrote: > Hi, > > I have installed 2 versions of python on my Windows XP computer - I > originally had 3.0.1, but then found that the MySQL module only > supported 2.*, so I've now installed that. I have found that if I > change the Windows Environment Variable path, then I can change the > version of python called when I type 'python' into a command line. > However, I'd like to be able to choose which version I use. I know > that if I change C:\Python26\python.exe to > C:\Python26\python2.exe and C:\Python30\python.exe to C: > \Python26\python3.exe, then typing 'python2' or 'python3' will invoke > the correct interpreter. However, is it safe just to rename the > executable files? Is there a more elegant way to achieve the same > task? > > Thanks, > Chris > > The elegant way is to have a batch directory on your PATH ( I use m:\t\bat ) and put your *.bat files there. I do NOT put any python installations on the PATH. For example, I have a batch file called: m:\t\bat\python26.bat c:\progfiles\python26\python.exe %* The %* syntax means pass all arguments through to the program. Once it all works, you can add an "@" in front of the c: in order to suppress echoing the command. At that point, it'll look and work the same way as what you did, but without modifying anything in the install directory. (You may want python26.bat, pythonw26.bat, python31.bat and pythonw31.bat) The other thing you may want to do in a batch file is to change the file associations so that you can run the .py file directly, without typing "python" or "pythonw" in front of it. The relevant Windows commands are: assoc and ftype And on a related note, you may want to edit the PATHEXT environment variable, to add .PY and .PYW From hv at tbz-pariv.de Tue Jul 21 10:27:27 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Tue, 21 Jul 2009 16:27:27 +0200 Subject: Workflow Libraries (DB vs Code) Message-ID: <7cm1ifF28jmnhU1@mid.individual.net> Hi, I need to write some simple workflows with web interface. For the web stuff I will use django, but I am not sure how to do the workflow part. Here are ideas: Do it myself. I don't think it will be difficult, I did things like this before. But they were not reusable. Use spiff Workflow library http://code.google.com/p/spiff-workflow/ Use goflow for django (port of OpenFlow of Zope). It does not look like a active project. Has anyone worked with spiff, goflow or other workflow software? What do you like and dislike? Somehow I like it the way goflow does it: The process definitions are stored in the database. But on the other hand I prefer to have things like this in a SVN-Repository. Creating the workflow definitions will be done by python programmers. I don't need a GUI to build them. There are many languages to define processes (BPML, XPDL, BPEL or YAWL). But I prefer python. Feedback welcome, Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From sajmikins at gmail.com Tue Jul 21 10:30:56 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 07:30:56 -0700 (PDT) Subject: invoke method on many instances References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Jul 20, 3:29?am, "Gabriel Genellina" wrote: > En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac ? > escribi?: > > >> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: > >>> def apply2(itr, methodname, *args, **kwargs): > >>> ? ? f = operator.methodcaller(methodname, *args, **kwargs) > >>> ? ? for item in itr: > >>> ? ? ? ? f(item) > > > On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: > >> for obj in objects: > >> ? ? getattr(obj, methodname)(*args, **kwargs) > > > Are there any obvious considerations in choosing > > between those two? > > The operator.methodcaller version is faster in my tests for large ? > collections, but slightly slower when you have very few elements. > > > import operator > import timeit > > class X: > ? ?def method(self, x, y, **kw): pass > > def apply1(itr, methodname, *args, **kwargs): > ? ? ?for item in itr: > ? ? ? ? ?getattr(item, methodname)(*args, **kwargs) > > def apply2(itr, methodname, *args, **kwargs): > ? ? ?f = operator.methodcaller(methodname, *args, **kwargs) > ? ? ?for item in itr: > ? ? ? ? ?f(item) > > L=[X() for _ in range(3000)] > apply1(L,'method', 1, 2, foo=3) > apply2(L,'method', 1, 2, foo=3) > > timeit.timeit(setup="from __main__ import apply1,apply2,L", ? > stmt="apply1(L,'method', 1, 2, foo=3)", number=1000) > timeit.timeit(setup="from __main__ import apply1,apply2,L", ? > stmt="apply2(L,'method', 1, 2, foo=3)", number=1000) > > > -- > Gabriel Genellina If your instances are all of the same class you can pass in the method directly like so: def apply3(iterator, method, *args, **kwargs): for item in iterator: method(item, *args, **kwargs) class X: def method(self, x, y, **kw): pass items = [X() for _ in range(10)] apply3(items, X.method, 1, 2, foo=3) HTH, ~Simon From sajmikins at gmail.com Tue Jul 21 10:42:08 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 10:42:08 -0400 Subject: tough-to-explain Python In-Reply-To: <7xws627qc1.fsf@ruckus.brouhaha.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7xljmk8q1m.fsf@ruckus.brouhaha.com> <6348dcb8-bfc1-446e-b7bf-28657e2b607e@j32g2000yqh.googlegroups.com> <7xws627qc1.fsf@ruckus.brouhaha.com> Message-ID: <50f98a4c0907210742q47aa3e21s9a410e833ab78718@mail.gmail.com> On Mon, Jul 20, 2009 at 9:54 PM, Paul Rubin wrote: > Simon Forman writes: >> But I'm glad it's there to study, these are wheels I don't have to >> invent for myself. > > http://dwheeler.com/essays/high-assurance-floss.html > > might be an interesting place to start. OO la la! Thank you! From slobodan.blazeski at gmail.com Tue Jul 21 10:42:18 2009 From: slobodan.blazeski at gmail.com (Slobodan Blazeski) Date: Tue, 21 Jul 2009 07:42:18 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <03fef26b-b8ed-4863-ba55-309624c97eff@o6g2000yqj.googlegroups.com> On Jul 19, 7:33?pm, fft1976 wrote: > On Jul 19, 9:55?am, Frank Buss wrote: > > > E.g. the number system: In many Lisp > > implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > > "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > > it returns "0.6666666666", which will result in lots of fun for porting > > applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. Worse is better? Bobi http://www.linkedin.com/in/slobodanblazeski From bearophileHUGS at lycos.com Tue Jul 21 10:46:26 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 21 Jul 2009 07:46:26 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: Nick Craig-Wood: > Can you give a hint as to how I debug this? ?I presume my program has > some instances of non static types which is causing the problem, but > it is going to be a very long debugging session if it takes me an hour > each cycle ;-) > > The program is about 700 lines of python (excluding comments). You can show us the Python (SSPython) code, and we can try to find the problem. Sometimes there's no simple ways to solve such problems. Generally for not very large progrograms if SS doesn't compile in about a minute or so then it's gone in infinite loop (there's a compilation flag that avoids some infinite loops, try it). Bye, bearophile From python at mrabarnett.plus.com Tue Jul 21 10:50:15 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 21 Jul 2009 15:50:15 +0100 Subject: Regular expression In-Reply-To: <200907211530.47704.peter.fodrek@stuba.sk> References: <200907211530.47704.peter.fodrek@stuba.sk> Message-ID: <4A65D5A7.9060908@mrabarnett.plus.com> Peter Fodrek wrote: > Dear conference! > > I have third party regular expression > > self.pattern_main = re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| > \#\d+\=(?:[+])?\d*(?:\.\d*)?)') > [snip] > It handles file correctly with two exceptions > > 1) omits ',' in the a output > 2) omits minus sign in the numbers... > > Would anyone recommend me what to change regular expression to add thesee two > think to the output,please? > self.pattern_main = re.compile(r'(\s+|,|-?\w\+?\d*(?:\.\d*)?|\w#\d+|\(.*?\)|#\d+=\+?\d*(?:\.\d*)?)') From piet at cs.uu.nl Tue Jul 21 11:05:36 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 17:05:36 +0200 Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: >>>>> Hendrik van Rooyen (HvR) wrote: >HvR> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: >>> On Jul 20, 6:08?pm, Duncan Booth wrote: >>> > The main reason why you need both lists and tuples is that because a >>> > tuple of immutable objects is itself immutable you can use it as a >>> > dictionary key. >>> >>> Really? That sounds interesting, although I can't think of any real- >>> world cases where you'd use something like that. >HvR> simplest is something like a point in the cartesian plane with >HvR> an associated attribute like colour. There are numerous other examples. Anytime you need a key that is not a single object but composed of more than one: Name + birthday Street + number + City Student + Course etc. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From bearophileHUGS at lycos.com Tue Jul 21 11:08:37 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 21 Jul 2009 08:08:37 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> Message-ID: <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> William Dode': > http://hg.flibuste.net/libre/games/cheval > Like you'll see, i tried to use exactly the same code for each langage. It's a cute solver. Few more versions of mine: #1, a Psyco version of mine: http://codepad.org/9m5rf7kX #2, unrolled Psyco version: http://codepad.org/gKFLu34M #3, a quick D (D1) version: http://codepad.org/Tk9FL7Xk Timings (no printing), seconds, best of 3: #1: 4.79 #2: 3.67 #3: 1.10 Your Psyco version: 13.37 Your C version, compiled with GCC 4.3.2, -s -O3 -fomit-frame-pointer: 3.79 I have timed the whole running time of the programs, with an external timer, so my timings include the start of the PythonVM and the final cleanup of the GC. Please, feel free to time my code again, so we can compare with your other timings (Java, etc) better. I have used Psyco 1.6 final and Python 2.6.2 on a Core2 CPU at 2 GHz. To compile the D1 code you can use the free DMD compiler for D1, I have used version 1.042, www.digitalmars.com/d/download.html ). In such benchmarks it's often better to start from the fastest low level code (written in an imperative language as C/D/C++, or a version in a functional language like Clean or OCaML) and then use it to create higher level versions (in Python, etc). This offers you a baseline timing, and usually the small differences of the higher level code compared to the original C/Clean code don't invalidate the test. I have changed only small things in the program, as you can see the Psyco program #2 is faster than your C code :-) If you learn how to use it well, Psyco1.6 can often lead to very good performance. But lot of people don't know how to use Psyco well (I too am ignorant: I don't know how to profile Python programs yet). Bye, bearophile From dns4 at cornell.edu Tue Jul 21 11:14:37 2009 From: dns4 at cornell.edu (David Smith) Date: Tue, 21 Jul 2009 11:14:37 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: Piet van Oostrum wrote: >>>>>> Hendrik van Rooyen (HvR) wrote: > >> HvR> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: >>>> On Jul 20, 6:08 pm, Duncan Booth wrote: >>>>> The main reason why you need both lists and tuples is that because a >>>>> tuple of immutable objects is itself immutable you can use it as a >>>>> dictionary key. >>>> Really? That sounds interesting, although I can't think of any real- >>>> world cases where you'd use something like that. > >> HvR> simplest is something like a point in the cartesian plane with >> HvR> an associated attribute like colour. > > There are numerous other examples. Anytime you need a key that is not a > single object but composed of more than one: > Name + birthday > Street + number + City > Student + Course > etc. Compound keys (like what's listed above) can also be used for sorting lists of dictionaries using DSU style sorting. Something I believe (and I could be wrong) won't work with mutable types like lists. --David From piet at cs.uu.nl Tue Jul 21 11:32:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 17:32:26 +0200 Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: >>>>> David Smith (DS) wrote: >DS> Piet van Oostrum wrote: >>>>>>>> Hendrik van Rooyen (HvR) wrote: >>> >HvR> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: >>>>>> On Jul 20, 6:08 pm, Duncan Booth wrote: >>>>> The main reason why you need both lists and tuples is that because a >>>>> tuple of immutable objects is itself immutable you can use it as a >>>>> dictionary key. >>>>>> Really? That sounds interesting, although I can't think of any real- >>>>>> world cases where you'd use something like that. >>> >HvR> simplest is something like a point in the cartesian plane with >HvR> an associated attribute like colour. >>> >>> There are numerous other examples. Anytime you need a key that is not a >>> single object but composed of more than one: >>> Name + birthday >>> Street + number + City >>> Student + Course >>> etc. >DS> Compound keys (like what's listed above) can also be used for sorting >DS> lists of dictionaries using DSU style sorting. Something I believe (and >DS> I could be wrong) won't work with mutable types like lists. For sorting there is no problem with mutable arrays as long as you don't mutate them during the sorting process (for example in the comparison routine). Doing that would be extremely perverse. And there is no enforcement of that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From ethan at stoneleaf.us Tue Jul 21 11:34:19 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 21 Jul 2009 08:34:19 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> <24543805.post@talk.nabble.com> <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> Message-ID: <4A65DFFB.3090004@stoneleaf.us> Mark Dickinson wrote: > On Jul 20, 11:34 pm, Ethan Furman wrote: > >>Dr. Phillip M. Feldman wrote: >> > Suppose that 'xor' returns the value that is true when only one value is >> > true, and False otherwise. This definition of xor doesn't have the >>standard >> > associative property, that is, >> > >> > (a xor b) xor c >> > >> > will not necessarily equal >> > >> > a xor (b xor c) >> > >> > To see that this is the case, let a= 1, b= 2, and c= 3. >> > >> > (a xor b) xor c >> > >> > yields 3, while >> > >> > a xor (b xor c) >> > >> > yields 1. So, I'd prefer an xor operator that simply returns True or >>False. >> > >> > Phillip >> > >> >>You are, of course, free to write your version however it makes sense to >>you and your team. :) >> >>Since the 'and' and 'or' already return objects (and objects evaluate to >>true or false), then 'xor' should behave likewise, IMO. I expect that >>would be the case if it were ever added to the language. > > > I'm not so sure. Did you ever wonder why the any() and all() > functions introduced in 2.5 return a boolean rather than returning > one of their arguments? (I did, and I'm still not sure what the > answer is.) > > Mark Very good question -- I likewise do not know the answer. I will only observe that any() and all() are functions, while 'and' and 'or' are not. If one wanted the object-returning behavior one could string together 'or's or 'and's instead. ~Ethan~ -- Thinking out loud here -- YMMV. :) From rhodri at wildebst.demon.co.uk Tue Jul 21 11:39:51 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 21 Jul 2009 16:39:51 +0100 Subject: Regular expression In-Reply-To: <200907211530.47704.peter.fodrek@stuba.sk> References: <200907211530.47704.peter.fodrek@stuba.sk> Message-ID: On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek wrote: > Dear conference! > > I have third party regular expression > > self.pattern_main = > re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| > \#\d+\=(?:[+])?\d*(?:\.\d*)?)') Always use raw strings (r"...") when you're working with regular expressions. *Always*. It may happen that you don't need them, as in this case, because Python fails to throw an exception when it meets unknown escape sequences in string literals, but when you do need them you will spend hours before realising it. Also, whoever wrote this regular expression should be taken out and slapped with a wet fish, for it is abominable and unlikely to look much like what you actually want. > with code > > def readline(self): > self.line = self.file_in.readline().rstrip() > if (len(self.line)) : return True > else : return False > > while (self.readline()): > words = self.pattern_main.findall(self.line) Um. Yes. Well. > This handles text file like > > // remark > PL_OFF > PARK > FS > MA 52.8806 , 18.0914 > SS > AD_W > PL_ON > C 52.3955 , -16.1511 , -90.0000 > > It handles file correctly with two exceptions > > 1) omits ',' in the a output You don't do any output, so it's hard to correct that! > 2) omits minus sign in the numbers... > > Would anyone recommend me what to change regular expression to add > thesee two > think to the output,please? It would help considerably if you explained what "it handles file correctly" means, since the regular expresion makes little sense with the input you give. What are you trying to extract from the input, and what do you think you are getting at the moment? -- Rhodri James *-* Wildebeest Herder to the Masses From c.c.wood at gmail.com Tue Jul 21 11:51:08 2009 From: c.c.wood at gmail.com (CCW) Date: Tue, 21 Jul 2009 08:51:08 -0700 (PDT) Subject: Multiple versions of python References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> On 21 July, 15:19, Dave Angel wrote: > ChrisW wrote: > > Hi, > > > I have installed 2 versions of python on my Windows XP computer - I > > originally had 3.0.1, but then found that the MySQL module only > > supported 2.*, so I've now installed that. ?I have found that if I > > change the Windows Environment Variable path, then I can change the > > version of python called when I type 'python' into a command line. > > However, I'd like to be able to choose which version I use. ?I know > > that if I change C:\Python26\python.exe to > > C:\Python26\python2.exe and C:\Python30\python.exe to C: > > \Python26\python3.exe, then typing 'python2' or 'python3' will invoke > > the correct interpreter. ?However, is it safe just to rename the > > executable files? Is there a more elegant way to achieve the same > > task? > > > Thanks, > > Chris > > The elegant way is to have a batch directory on your PATH ( I use ? > m:\t\bat ) ?and put your *.bat files there. ? I do NOT put any python > installations on the PATH. > > For example, I have a batch file called: ? ? m:\t\bat\python26.bat > > c:\progfiles\python26\python.exe %* > > The %* syntax means pass all arguments through to the program. > > Once it all works, you can add an "@" in front of the c: ? in order to > suppress echoing the command. ?At that point, it'll look and work the > same way as what you did, but without modifying anything in the install > directory. ? ?(You may want ?python26.bat, pythonw26.bat, python31.bat > and pythonw31.bat) > > The other thing you may want to do in a batch file is to change the file > associations so that you can run the .py file directly, without typing > "python" or "pythonw" in front of it. > > The relevant Windows commands are: ? ? assoc and ftype ? ? ?And on a > related note, you may want to edit the PATHEXT environment variable, to > add .PY and .PYW Thanks for this - this way made a bit more sense to me. I've now got C:\commands with the 4 .bat files in, and C:\commands in my path. It all seems to work :) I think I've missed the point of the @ though - it doesn't seem to make any difference.. I'm also a bit confused with the associations - since I've got python 2.6 and 3.1, surely the command I type (python26 or python31) is the only way to force a script to be run using a specific interpreter at runtime without having to change the .bat file every time I want to run a script using 3.1 instead of 2.6? From peter.fodrek at stuba.sk Tue Jul 21 11:51:32 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Tue, 21 Jul 2009 17:51:32 +0200 Subject: Regular expression In-Reply-To: <4A65D5A7.9060908@mrabarnett.plus.com> References: <200907211530.47704.peter.fodrek@stuba.sk> <4A65D5A7.9060908@mrabarnett.plus.com> Message-ID: <86771222-4E27-4E0C-BC56-948169A277BF@stuba.sk> 21.7.2009 v 16:50, MRAB: > Peter Fodrek wrote: >> Dear conference! >> I have third party regular expression >> self.pattern_main = re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d >> +|\(.*?\)| >> \#\d+\=(?:[+])?\d*(?:\.\d*)?)') > [snip] >> It handles file correctly with two exceptions >> 1) omits ',' in the a output >> 2) omits minus sign in the numbers... >> Would anyone recommend me what to change regular expression to add >> thesee two think to the output,please? > > self.pattern_main = re.compile(r'(\s+|,|-?\w\+?\d*(?:\.\d*)?|\w#\d+|\ > (.*?\)|#\d+=\+?\d*(?:\.\d*)?)') Thank you I will try yesterday... Peter From deets at nospam.web.de Tue Jul 21 11:58:47 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 21 Jul 2009 17:58:47 +0200 Subject: Workflow Libraries (DB vs Code) References: <7cm1ifF28jmnhU1@mid.individual.net> Message-ID: <7cm6kmF26g9srU1@mid.uni-berlin.de> Thomas Guettler wrote: > Hi, > > I need to write some simple workflows with web interface. > > For the web stuff I will use django, but I am not sure how > to do the workflow part. Did you consider using OpenERP? It comes with a web-frontend (TG1.0.8-based), but also offers a XMLRPC-server to connect to, so using it from within django shouldn't be any difficult. Workflow-definitions are stored in the DB, and can be generated visually, not sure if they have to though. I'm not very familiar with the system myself, but the EuroPython presentation was truly impressive. Diez From piet at cs.uu.nl Tue Jul 21 11:59:47 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 17:59:47 +0200 Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> M2Crypto package not showing the 'recipient_public_key.pem' file at >j> linux terminal .how do i get/connect with recipient public key. >j> exactly i need to check how can i open this file through linux >j> commands. >j> import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open >j> ('recipient_public_key.pem','rb').read()) print recip; plaintext = >j> whatever i need to encrypt msg = recip.public_encrypt >j> (plaintext,RSA.pkcs1_padding) print msg; >j> after calling the function its not giving any output and even any >j> error >j> i also tried as 'Will' said >j> pk = open('public_key.pem','rb').read() print pk; rsa = >j> M2Crypto.RSA.load_pub_key(pk) >j> whats the mistake i am not getting .will somebody help me out. >j> is something wrong in opening 'recipient_public_key.pem'. is M2Crypto >j> contain this file inbuilt .from where this file taking public key of >j> the recipient i'. what this file contain and how it will work .should >j> i need to create such a file for my purpose.if it is then how can i >j> create this and how it will retrieve the key,where i recognize my >j> recipient to get his public key .is something like database at server. >j> please give me a quick response...... >j> looking for your answer. Please: 1. Type your python code with newlines and proper indentation. 2. Show the error messages that your code gives when you run it. 3. Use proper capital letters at the beginning of your sentences. 4. Don't fire so many questions in rapid succession. The recipient_public_key.pem file is the public key of the recipient which means the person that is going to receive the encrypted message. You should get it from the recipient him/herself or from some key store where s/he has deposited it. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From iwan at reahl.org Tue Jul 21 12:05:31 2009 From: iwan at reahl.org (Iwan Vosloo) Date: Tue, 21 Jul 2009 18:05:31 +0200 Subject: Issue combining gzip and subprocess Message-ID: <1248192331.30167.38.camel@easymoney> Hi there, We tried to gzip the output of a shell command, but this results in a strange error: the resulting file seems to be the concatenation of the plaintext file with the zipped content. For example: f = gzip.open(filename, 'w') subprocess.check_call(['ls','-la'], stdout=f) f.close() Using a normal file works as expected, but a GzipFile results in a file containing what looks like the unzipped data, followed by the zipped data. I suspect this may have something to do with limitations of GzipFile such as it not implementing truncate(). Does anyone have an explanation / can you suggest a nice solution for doing what we are trying to do? Regards - Iwan From nad at acm.org Tue Jul 21 12:08:48 2009 From: nad at acm.org (Ned Deily) Date: Tue, 21 Jul 2009 09:08:48 -0700 Subject: PIL for OSX 64bit References: Message-ID: In article , moerchendiser2k3 wrote: > I have a problem with Python and the Python Image Library to get it > work on OSX 64 bit. Maybe anyone can help me? I started Python in 64 > bit mode and called Image.open(...).load() and got: > > ImportError: The _imaging C module is not installed [...] It's hard to know what your problem is without more information. One potential stumbling block: PIL has dependencies on other open-source libraries, such as libjpeg and freetype. You'll need to have 64-bit versions of them available when building and when running, as well. There are a number of ways to do that. The simplest might be to download pre-built frameworks containing those libraries from here: http://www.kyngchaos.com/software:frameworks There are also build scripts there for building your own versions from scratch. -- Ned Deily, nad at acm.org From sajmikins at gmail.com Tue Jul 21 12:10:57 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 09:10:57 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: On Jul 20, 12:27?pm, Phillip B Oldham wrote: > My colleagues and I have been working with python for around 6 months > now, and while we love a lot of what python has done for us and what > it enables us to do some of the decisions behind such certain > data-types and their related methods baffle us slightly (when compared > to the decisions made in other, similarly powerful languages). > > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > > Consider the following: > > >>> x = [2,1,3] > >>> x.sort() > >>> print x > > [1, 2, 3] > > Now, if the sort operations were unable to affect the original > structure of the list (as in JavaScript) you'd effectively have a > tuple which you could add/remove from, and the example above would > look more like: > > >>> x = [2,1,3] > >>> print x.sort() > [1, 2, 3] > >>> print x > > [2,1,3] > > This make a lot more sense to us, and follows the convention from > other languages. It would also mean chaining methods to manipulate > lists would be easier: > > >>> x = [2,1,3] > >>> print x.sort()[0] > 3 (As others have already pointed out this would print 1, not 3.) > >>> print x > > [2,1,3] > > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. In this case you can use: >>> x = [2,1,3] >>> print min(x) 1 (There's also a max() function.) > We're not looking to start any arguments or religious wars and we're > not asking that python be changed into something its not. We'd simply > like to understand the decision behind the lists and tuple structures. > We feel that in not "getting" the difference between the two types we > may be missing out on using these data structures to their full > potential. One way to think about tuples (as distinct from lists) is as a short- hand version of Pascal's 'record' type, or C's 'struct' (with the caveats that the fields are not named, only indexed, and the types of the fields are implied by use, not explicitly declared.) (FWIW: http://en.wikipedia.org/wiki/Record_(computer_science) ) Broadly speaking, lists are useful for things like stacks and queues, and sorting, while tuples are useful for aggregating heterogeneous data into coherent units, and you can hash them (provided their contents are also hashable.) HTH, ~Simon From stef.mientki at gmail.com Tue Jul 21 12:13:30 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Tue, 21 Jul 2009 18:13:30 +0200 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: <4A65E92A.3050608@gmail.com> hi Marcus, >>> That sounds like a very specialized type of thing, >> Well from an application point of view, >> with the current netbooks, >> this looks like a perfect tool for any conversation or meeting. >>> which only the few people with experience with wxPython, PyAudio, >>> and Scintilla could help you with... >>> >> I was afraid of that too, so I dropped the question in several places, >> and the writer of Scintilla himself came with the perfect answer. >> >> cheers,Stef >>> But you might try having a dictionary with notes and associated >>> times, or just give each note a four-digit ID number at the >>> beginning of it when it's entered and use that in the dictionary (to >>> keep keys shorter). Or you could just do a little hack and increase >>> the number of bookmarks allowed (seeing as source is available) :p >>> >>> Marcus >> > Glad you got a good answer from somebody. Sounds like an interesting > project. About when would this be headed for a release? Could you post > a link to a googlecode or sourceforge project or something so I can > follow and/or help with development? > For the moment it's just an idea, so no line of code yet. I first like to tackle all the problems, at least to the level I think I can handle them. So first solve the next problem, before I start coding: automatic synchronization (file uploading and deleting) between EEE-pc and desktop PC over bluetooth. And another problem, as my customers are physicians, both the text and audio need to be stored encrypted. cheers, Stef > Marcus From fb at frank-buss.de Tue Jul 21 12:44:23 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 18:44:23 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Message-ID: <1t9jyo7lzm3ke.hh1blip7i2yp.dlg@40tude.net> Rainer Joswig wrote: > I'm not sure if it is fair to post a reference to a single > post by someone without context and without having used ECL. > If there are stability problems, people can report to the > ECL mailing list. The maintainers are very active. This was just one example. Another one: http://www.mail-archive.com/application-builder at lispniks.com/msg01024.html Luke and Elliott are no beginners, so it is at least difficult to use ECL: On Windows it doesn't work with MinGW and on MacOS X there was at least one case were it freezed. But as I have written, I didn't tried it myself, so this is only some second hand experience. Maybe it is all wrong setups in combination with Lispbuilder and ECL itself is stable, I don't know. But I know that it is much easier to get a full featured running Python system on Windows, MacOS X and Linux, so this is something where Lisp distributions could be improved. -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From nagle at animats.com Tue Jul 21 12:48:38 2009 From: nagle at animats.com (John Nagle) Date: Tue, 21 Jul 2009 09:48:38 -0700 Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops In-Reply-To: References: Message-ID: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> Ville Vainio wrote: > Has anyone implementing something like what the subject line > indicates? > > The idea: > > To run functions that execute a series of system commands without > blocking the ui, *and* without adding state machine logic. At some level, there's going to be state machine logic. You need interlocking so that the user can't initiate simultaneous conflicting background operations from the GUI. The usual solution is to run the background operations in another thread, and have them put events on a queue read by the GUI task. You also have to work out how to cancel background operations, if they're going to be as big as a "make". Here's the very first application which did this sort of thing, running commands in multiple threads without blocking the user interface thread. This is from 1972. http://www.fourmilab.ch/documents/univac/fang/ And yes, it did properly interlock everything it was doing in the background. John Nagle From piet at cs.uu.nl Tue Jul 21 12:50:00 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 18:50:00 +0200 Subject: Issue combining gzip and subprocess References: Message-ID: >>>>> Iwan Vosloo (IV) wrote: >IV> Hi there, >IV> We tried to gzip the output of a shell command, but this results in a >IV> strange error: the resulting file seems to be the concatenation of the >IV> plaintext file with the zipped content. >IV> For example: >IV> f = gzip.open(filename, 'w') >IV> subprocess.check_call(['ls','-la'], stdout=f) >IV> f.close() >IV> Using a normal file works as expected, but a GzipFile results in a file >IV> containing what looks like the unzipped data, followed by the zipped >IV> data. >IV> I suspect this may have something to do with limitations of GzipFile >IV> such as it not implementing truncate(). >IV> Does anyone have an explanation / can you suggest a nice solution for >IV> doing what we are trying to do? stdout (and the others) must be None, PIPE or a real file object or file descriptor, not a file like object. In your case the solution would be to use PIPE, and then read the output and write in to the GzipFile yourself. f = gzip.open(filename, 'w') proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) while True: line = proc.stdout.readline() if not line: break f.write(line) f.close() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mail at timgolden.me.uk Tue Jul 21 13:07:49 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 21 Jul 2009 18:07:49 +0100 Subject: win32api install problem In-Reply-To: References: Message-ID: <4A65F5E5.3060903@timgolden.me.uk> MCIPERF wrote: > On Jul 20, 9:57 am, Tim Golden wrote: >> Gerry wrote: >>> I'm running Python 2.6 under XP. >>> I've installed Windows 32 extensions for Python 2.6 version 1.4 >>> (pywin32-214.win32-py2.6.exe). >>> But If I try to import win32api, I get: >>> File "C:\python_projects\euler\driveletters.py", line 1, in >>> import win32api >>> ImportError: DLL load failed: The specified module could not be found. >> Used to be you'd get this error if you installed as a >> non-admin user. Don't know if that's still an issue. >> Possibility? >> >> TJG > > Not a possibility (afaict) -- I am an administrator, according to the > control panel. Not too many ideas, then, I'm afraid. Apart from the obvious -- uninstall and try again -- how about running Python with the -vv option: python -vv to get more information about what it's trying to get, and/or running sysinternal's Process Explorer at the same time to spot the DLL Dependencies. I have seen a (not too similar) problem when running TortoiseHg and the pywin32 extensions, since the shell import's TortoiseHg's version of various pywin32 DLLs before I try to import them myself for some other purpose, and the two versions clashed dramatically (crashed Explorer hard, I seem to remember). Just in case that rings any bells.. TJG From jeanmichel at sequans.com Tue Jul 21 13:22:05 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 21 Jul 2009 19:22:05 +0200 Subject: Regular expression In-Reply-To: References: <200907211530.47704.peter.fodrek@stuba.sk> Message-ID: <4A65F93D.8000908@sequans.com> Rhodri James wrote: > On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek > wrote: > >> Dear conference! >> >> I have third party regular expression >> >> self.pattern_main = >> re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| >> \#\d+\=(?:[+])?\d*(?:\.\d*)?)') > > Also, whoever wrote this regular expression should be taken > out and slapped with a wet fish, for it is abominable and > unlikely to look much like what you actually want. Here, take that fish and please, hit really hard ! ** ;'-. `;-._ ) '---.._ > `-.__.-' `'.__ /_.-'-._ _, ^ ---) ` `'------/_.'----`` JM ** From tfb at cley.com Tue Jul 21 13:41:59 2009 From: tfb at cley.com (Tim Bradshaw) Date: Tue, 21 Jul 2009 18:41:59 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <2009072118415916807-tfb@cleycom> On 2009-07-19 19:31:36 +0100, Frank Buss said: > (e.g. I > don't know of a free modern and stable Lisp implemenation with > mulithreading support for Windows, with a licence with which you can use it > in closed source commercial programs, like you can do with Python). Openmcl seems reasonably stable to me, is LLGPL-licensed, and runs on these platforms and Solaris x86. It's kind of tragic, of course, that this kind of parasitic behaviour (will not consider a commercial product to use in your commercial system) has now become so common. From robert.kern at gmail.com Tue Jul 21 13:52:23 2009 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 21 Jul 2009 12:52:23 -0500 Subject: clean way prepend an element to a numpy array In-Reply-To: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> Message-ID: On 2009-07-20 22:56, bdb112 wrote: > If I want to add an element at the beginning of an array, it seems > like I must make a list, insert in place, then make an array again. the_array = numpy.concatenate([new_value, the_array]) You will want to ask numpy questions on the numpy mailing list. http://www.scipy.org/Mailing_Lists -- 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 rhodri at wildebst.demon.co.uk Tue Jul 21 13:59:32 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 21 Jul 2009 18:59:32 +0100 Subject: Regular expression In-Reply-To: <1AE4BD36-9955-4F80-9FBC-6DF62F529CC3@stuba.sk> References: <200907211530.47704.peter.fodrek@stuba.sk> <1AE4BD36-9955-4F80-9FBC-6DF62F529CC3@stuba.sk> Message-ID: On Tue, 21 Jul 2009 17:12:53 +0100, Peter Fodrek wrote: > 21.7.2009 v 17:39, Rhodri James: > >> On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek > > wrote: [snipped for space] >>> This handles text file like >>> >>> // remark >>> PL_OFF >>> PARK >>> FS >>> MA 52.8806 , 18.0914 >>> SS >>> AD_W >>> PL_ON >>> C 52.3955 , -16.1511 , -90.0000 >>> >>> It handles file correctly with two exceptions >>> >>> 1) omits ',' in the a output >> >> You don't do any output, so it's hard to correct that! > > for line > > C 52.3955 , -16.1511 , -90.0000 > > it returns (as I remember) > > ('C', ' ', '52.3955', ' ', ' ', ' 16.1511', ' ', ' ','90.0') > > I was to get (number and positions of empty strings are not related to > me. I can add, remove then as you wish) > > ('C', ' ', '52.3955', ' ', ' ,', ' -16.1511', ' ', ' ,','-90.0') If whitespace isn't an issue and the format really is as simple as your example, you don't need the full power of a regular expression here. split() would do the trick: words = self.line.split() will return what you want for that example. It will produce different (but more consistent) results than the regular expression for other lines, though: for example, MA 52.8806 , 18.0914 when put through the regular expression (if fixed for commas) would give you ('M', 'A', ' ', '52.8806', ' ', ',', ' ', '18.0914') as against ['MA', '52.8806', ',', '18.0914'] > I use eval call in python to do format conversion, so just parsing is > problem float() might be better than eval(), if you know for sure that you are expecting floats. -- Rhodri James *-* Wildebeest Herder to the Masses From fred at bsdhost.net Tue Jul 21 14:12:01 2009 From: fred at bsdhost.net (Fred C) Date: Tue, 21 Jul 2009 11:12:01 -0700 Subject: setuptools question. Message-ID: I have a python program and when I install this program from the module home directory using setup.py everything works fine. But easy_install fails with the following error, because I am trying to install some start up shell scripts into /etc/init.d > The package setup script has attempted to modify files on your system >that are not within the EasyInstall build area, and has been aborted. Is there a way to use easy_install to install the start scripts into / etc/init.d. If not, what is the best way to install these scripts. Thanks -fred- From hyugaricdeau at gmail.com Tue Jul 21 14:13:35 2009 From: hyugaricdeau at gmail.com (Hyuga) Date: Tue, 21 Jul 2009 11:13:35 -0700 (PDT) Subject: are user defined classes hashable? References: <373d6c69-6965-4a88-bdd2-8028ef850bf8@k6g2000yqn.googlegroups.com> Message-ID: On Jul 20, 10:53?pm, a... at pythoncraft.com (Aahz) wrote: > In article <373d6c69-6965-4a88-bdd2-8028ef850... at k6g2000yqn.googlegroups.com>, > > Hyuga ? wrote: > > >Regardless, Nicolas's example can be applied to the class too: > > >>>> class Foo(object): > > ? ?pass > > >>>> hash(Foo) > >11443104 > >>>> id(Foo) > >11443104 > > >class objects are just objects of type 'type'. > > Not quite. ?They certainly default that way, but changing the metaclass > changes a class's type:: > > class M(type): > ? ? pass > > class C(object): > ? ? pass > > class C2(object): > ? ? __metaclass__ = M > > print type(C) > print type(C2) Well, okay, you got me there. But the OP wasn't asking about classes with different metaclasses. And besides, type(type(C2)) is still type ;) From tjreedy at udel.edu Tue Jul 21 14:26:39 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 21 Jul 2009 14:26:39 -0400 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: References: Message-ID: Nick Craig-Wood wrote: > > I tried it on a program I wrote to solve a puzzle (Rush Hour). > (Actually it solves the meta-puzzle - trying to make the hardest > possible Rush Hour puzzle.) > > After a bit of twiddling (remove psyco and profiling) I got it to > start compiling, but unfortunately it compiled for about an hour (in > iterative type analysis..) used up 600 MB of RAM printing an '*' every > 10 minutes or so then gave an error message and gave up. > > Unfortunately I shut the window by accident, but the error message was > something about not being able to resolve types I think with a list of > 20 or so unresolved types. > > Can you give a hint as to how I debug this? I presume my program has > some instances of non static types which is causing the problem, but > it is going to be a very long debugging session if it takes me an hour > each cycle ;-) > > The program is about 700 lines of python (excluding comments). Split it into pieces and compile each separately. Recurse. tjr From tjreedy at udel.edu Tue Jul 21 14:57:57 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 21 Jul 2009 14:57:57 -0400 Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops In-Reply-To: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> References: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> Message-ID: > Ville Vainio wrote: >> Has anyone implementing something like what the subject line >> indicates? Your subject line is so long that it is cut off even on my wide screen. Better to repeat the question in the body. From martin.hellwig at dcuktec.org Tue Jul 21 15:05:55 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 21 Jul 2009 20:05:55 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> Message-ID: <-vadnaHpJMoIjPvXnZ2dnUVZ8tli4p2d@bt.com> David Adamo Jr. wrote: > On Jul 21, 10:40 am, "Martin P. Hellwig" > wrote: >> sightseer wrote: >> >> >> >>> Error Installing Service: Access is Denied. (5) >> >> Are you trying to do this on windows vista? >> >> -- >> MPHhttp://blog.dcuktec.com >> 'If consumed, best digested with added seasoning to own preference.' > > Yeah, I was trying to do it on Vista. Someone has just helped me out. > I had to deactivate User Account Control on Windows Vista...and now > everything is rosy. Thanks guys. No need to deactivate it, just right click on the command shell program and say run as administrator, than you can install the service via the command line. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From lukepadawan at gmail.com Tue Jul 21 15:21:50 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Tue, 21 Jul 2009 16:21:50 -0300 Subject: List insertion cost Message-ID: <4A66154E.1050200@gmail.com> Hello, I would like to know how much it costs to insert an element into a list using this operation: a[2:2] = [ 1 ] i. e, what is the complexity of the operation above (given that len(a) = n)? Thanks. From daniel at stutzbachenterprises.com Tue Jul 21 15:35:55 2009 From: daniel at stutzbachenterprises.com (Daniel Stutzbach) Date: Tue, 21 Jul 2009 14:35:55 -0500 Subject: List insertion cost In-Reply-To: <4A66154E.1050200@gmail.com> References: <4A66154E.1050200@gmail.com> Message-ID: On Tue, Jul 21, 2009 at 2:21 PM, Lucas P Melo wrote: > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? > O(n) If you want O(log n), you can use the blist extension type from http://pypi.python.org/pypi/blist/ -- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC -------------- next part -------------- An HTML attachment was scrubbed... URL: From king.seth at gmail.com Tue Jul 21 15:37:12 2009 From: king.seth at gmail.com (Seth) Date: Tue, 21 Jul 2009 12:37:12 -0700 (PDT) Subject: Pyserial and pyQt Message-ID: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> I have used pyserial in the past but this is my first experience with pyQt. I am using the Python xy package for windows current but might move to linux. I have a small device that is outputting a basic text string. I want to be able to read this string(from the comm port) and update a text box and eventually a graph in pyQt. I can't find any documentation or tutorials on how to do this. If anyone can point me in the right direction or give me some tips I would be grateful. Thanks, Seth From robert.kern at gmail.com Tue Jul 21 15:43:41 2009 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 21 Jul 2009 14:43:41 -0500 Subject: List insertion cost In-Reply-To: <4A66154E.1050200@gmail.com> References: <4A66154E.1050200@gmail.com> Message-ID: On 2009-07-21 14:21, Lucas P Melo wrote: > Hello, > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? O(n). Python lists are contiguous arrays in memory, and everything after the insertion point needs to be moved. Raymond Hettinger has a good talk about the implementation of Python lists and other container objects. http://www.youtube.com/watch?v=hYUsssClE94 http://www.pycon.it/static/stuff/slides/core-python-containers-under-hood.ppt -- 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 lukepadawan at gmail.com Tue Jul 21 16:47:26 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Tue, 21 Jul 2009 17:47:26 -0300 Subject: List insertion cost In-Reply-To: References: <4A66154E.1050200@gmail.com> Message-ID: <4A66295E.9070004@gmail.com> Robert Kern wrote: > O(n). Python lists are contiguous arrays in memory, and everything > after the insertion point needs to be moved. Raymond Hettinger has a > good talk about the implementation of Python lists and other container > objects. > > http://www.youtube.com/watch?v=hYUsssClE94 > http://www.pycon.it/static/stuff/slides/core-python-containers-under-hood.ppt > Thanks. :) From ryniek90 at gmail.com Tue Jul 21 16:55:18 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Tue, 21 Jul 2009 22:55:18 +0200 Subject: Changing the private variables content In-Reply-To: References: Message-ID: <4A662B36.3010708@gmail.com> Hi. I'm writing some class, and decided to use inside private method and some private variables. While with method i haven't got any problem's with variables i have. Maybe some example. A class with private method and private variable: " >>> class Secret(object): # def __init__(self): self._number = 1 # def _secret(self): print self._number def showit(self): print "Secret number is:\n" self._secret() >>> sec = Secret() >>> sec.showit() Secret number is: 1 >>> sec._number 1 >>> sec._number = 2 >>> sec._number 2 >>> sec._number += 3 >>> sec._number 5 >>> " As You can see, i made class with private method and private variable inside __init__ constructor. I've changed also the variable value, but outside class. I've got problem with changing some variable value _inside__my_ class, which i'm writing. I've searched over google, some tuts with topics about operations on private variables, but didn't found anything - only how to make them, but no how to assign new objects/override them with new content (and other advanced and helpful options). If someone could help me, it would be great. Thanks. From davidj411 at gmail.com Tue Jul 21 17:00:51 2009 From: davidj411 at gmail.com (davidj411) Date: Tue, 21 Jul 2009 14:00:51 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order Message-ID: I am using a recursive function to print the time and a few other things on each pass. ( the function calculates size of file that is being transferred and if not 100 % copied, it waits 20 secs and checks again). i would expect the time to be correct anytime it is used: <--code below -->> print time.strftime('%m-%d-%Y %H:%m:%S') <--code above -->> here is an example of what i am seeing: 16:07:16 16:07:36 16:07:56 16:07:16 16:07:36 16:07:56 16:07:16 16:07:36 16:07:56 From lukepadawan at gmail.com Tue Jul 21 17:08:07 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Tue, 21 Jul 2009 18:08:07 -0300 Subject: Balanced binary tree implementation Message-ID: <4A662E37.6090406@gmail.com> Hello, I would like to use a balanced binary tree implementation (preferably within some API). Any hints about where I could find it? I am looking for something that implements insertion, deletion, search and a special search that returns the lesser element bigger than a given key [1]. A nice possibility would be an extensible API that allows me to inherit its classes and to add operations myself. Thanks in advance. [1] Ex: 1 2 3 4 5 6 are elements of the bbt. If I use this operation given 4 as the parameter, the value returned would be 5. From joncle at googlemail.com Tue Jul 21 17:09:31 2009 From: joncle at googlemail.com (Jon Clements) Date: Tue, 21 Jul 2009 14:09:31 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: <025efda8-0289-4d7d-a9e8-c90faa445c22@s31g2000yqs.googlegroups.com> On 21 July, 22:00, davidj411 wrote: > I am using a recursive function to print the time and a few other > things on each pass. ( the function calculates size of file that is > being transferred and if not 100 % copied, it waits 20 secs and checks > again). > > i would expect the time to be correct anytime it is used: > > <--code below -->> > print time.strftime('%m-%d-%Y %H:%m:%S') > <--code above -->> > > here is an example of what i am seeing: > > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 I assume month, day and year are actually being output and that you've removed it from your post. Err, what else do you expect to happen if you're doing this recursively? From gherron at islandtraining.com Tue Jul 21 17:14:44 2009 From: gherron at islandtraining.com (Gary Herron) Date: Tue, 21 Jul 2009 14:14:44 -0700 Subject: Changing the private variables content In-Reply-To: <4A662B36.3010708@gmail.com> References: <4A662B36.3010708@gmail.com> Message-ID: <4A662FC4.2050804@islandtraining.com> Ryniek90 wrote: > Hi. > I'm writing some class, and decided to use inside private method and > some private variables. While with method i haven't got any problem's > with variables i have. > Maybe some example. > A class with private method and private variable: > > " > >>> class Secret(object): > # > def __init__(self): > self._number = 1 > # > def _secret(self): > print self._number > def showit(self): > print "Secret number is:\n" > self._secret() > > >>> sec = Secret() > >>> sec.showit() > Secret number is: > > 1 > >>> sec._number > 1 > >>> sec._number = 2 > >>> sec._number > 2 > >>> sec._number += 3 > >>> sec._number > 5 > >>> > " > > As You can see, i made class with private method and private variable > inside __init__ constructor. I've changed also the variable value, but > outside class. > I've got problem with changing some variable value _inside__my_ class, > which i'm writing. Not sure this is what you are asking, but a method (which is how I interpret "_inside__my_ class") changes the value by normal assignment like this: class Secret(object): ... def SetNumber(self,value): self._number = value Is that what you were asking? Gary Herron > I've searched over google, some tuts with topics about operations on > private variables, but didn't found anything - only how to make them, > but no how to assign new objects/override them with new content (and > other advanced and helpful options). > > If someone could help me, it would be great. > > Thanks. From sajmikins at gmail.com Tue Jul 21 17:29:14 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 14:29:14 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> On Jul 21, 5:00?pm, davidj411 wrote: > I am using a recursive function to print the time and a few other > things on each pass. ( the function calculates size of file that is > being transferred and if not 100 % copied, it waits 20 secs and checks > again). > > i would expect the time to be correct anytime it is used: > > <--code below -->> > print time.strftime('%m-%d-%Y %H:%m:%S') > <--code above -->> > > here is an example of what i am seeing: > > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 Your output doesn't match your format string: In [1]: import time In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') 07-21-2009 17:07:16 There's no way to tell why your output times seem to repeat without seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: %m:%S')" line. From piet at cs.uu.nl Tue Jul 21 17:38:43 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 23:38:43 +0200 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: >>>>> davidj411 (d) wrote: >d> I am using a recursive function to print the time and a few other >d> things on each pass. ( the function calculates size of file that is >d> being transferred and if not 100 % copied, it waits 20 secs and checks >d> again). >d> i would expect the time to be correct anytime it is used: >d> <--code below -->> >d> print time.strftime('%m-%d-%Y %H:%m:%S') >d> <--code above -->> >d> here is an example of what i am seeing: >d> 16:07:16 >d> 16:07:36 >d> 16:07:56 >d> 16:07:16 >d> 16:07:36 >d> 16:07:56 >d> 16:07:16 >d> 16:07:36 >d> 16:07:56 You probably meant: print time.strftime('%m-%d-%Y %H:%M:%S') -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jeanmichel at sequans.com Tue Jul 21 17:42:41 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 21 Jul 2009 23:42:41 +0200 Subject: doted filenames in import statements Message-ID: <4A663651.5020006@sequans.com> Hi fellows, I'd like to use the dynamic __import__ statement. It works pretty well with non dotted names, but I cannot figure how to make it work with dotted file paths. example: file = "/home/dsp/test.py" test = __import__(file) works like a charm file = "/home/dsp/4.6.0.0/test.py" test = __import__(file) => no module name blalalal found. Any suggestion ? I tried multiple escape technics without any success. JM From joncle at googlemail.com Tue Jul 21 17:52:57 2009 From: joncle at googlemail.com (Jon Clements) Date: Tue, 21 Jul 2009 14:52:57 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: <577639e9-b64a-4a8e-8d90-b70ca491596b@18g2000yqa.googlegroups.com> On 21 July, 22:38, Piet van Oostrum wrote: > >>>>> davidj411 (d) wrote: > >d> I am using a recursive function to print the time and a few other > >d> things on each pass. ( the function calculates size of file that is > >d> being transferred and if not 100 % copied, it waits 20 secs and checks > >d> again). > >d> i would expect the time to be correct anytime it is used: > >d> <--code below -->> > >d> print time.strftime('%m-%d-%Y %H:%m:%S') > >d> <--code above -->> > >d> here is an example of what i am seeing: > >d> 16:07:16 > >d> 16:07:36 > >d> 16:07:56 > >d> 16:07:16 > >d> 16:07:36 > >d> 16:07:56 > >d> 16:07:16 > >d> 16:07:36 > >d> 16:07:56 > > You probably meant: print time.strftime('%m-%d-%Y %H:%M:%S') > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Good spot! From davidj411 at gmail.com Tue Jul 21 17:53:29 2009 From: davidj411 at gmail.com (davidj411) Date: Tue, 21 Jul 2009 14:53:29 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: On Jul 21, 5:29?pm, Simon Forman wrote: > On Jul 21, 5:00?pm, davidj411 wrote: > > > > > > > I am using a recursive function to print the time and a few other > > things on each pass. ( the function calculates size of file that is > > being transferred and if not 100 % copied, it waits 20 secs and checks > > again). > > > i would expect the time to be correct anytime it is used: > > > <--code below -->> > > print time.strftime('%m-%d-%Y %H:%m:%S') > > <--code above -->> > > > here is an example of what i am seeing: > > > 16:07:16 > > 16:07:36 > > 16:07:56 > > 16:07:16 > > 16:07:36 > > 16:07:56 > > 16:07:16 > > 16:07:36 > > 16:07:56 > > Your output doesn't match your format string: > > In [1]: import time > > In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') > 07-21-2009 17:07:16 > > There's no way to tell why your output times seem to repeat without > seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: > %m:%S')" line. sorry, yes, i did manually filter the output. here is the function: def log_out(msg,servername='std.out'): print msg open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') on each pass, it should output the newer time (whether recursive or not, right) ? From clp2 at rebertia.com Tue Jul 21 17:59:17 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 21 Jul 2009 14:59:17 -0700 Subject: doted filenames in import statements In-Reply-To: <4A663651.5020006@sequans.com> References: <4A663651.5020006@sequans.com> Message-ID: <50697b2c0907211459w88c3e28y2a96811bd307233c@mail.gmail.com> On Tue, Jul 21, 2009 at 2:42 PM, Jean-Michel Pichavant wrote: > Hi fellows, > > I'd like to use the dynamic __import__ statement. It works pretty well with > non dotted names, but I cannot figure how to make it work with dotted file > paths. > > example: > > file = "/home/dsp/test.py" > test = __import__(file) > > works like a charm > > file = "/home/dsp/4.6.0.0/test.py" > test = __import__(file) > => no module name blalalal found. > > Any suggestion ? I tried multiple escape technics without any success. You want the imp.load_module() function: http://docs.python.org/library/imp.html#imp.load_module __import__() only operates on module/package names. I'm not sure how you even got it to work with a filename... Cheers, Chris -- http://blog.rebertia.com From rhodri at wildebst.demon.co.uk Tue Jul 21 18:00:39 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 21 Jul 2009 23:00:39 +0100 Subject: Changing the private variables content In-Reply-To: <4A662B36.3010708@gmail.com> References: <4A662B36.3010708@gmail.com> Message-ID: On Tue, 21 Jul 2009 21:55:18 +0100, Ryniek90 wrote: > Hi. > I'm writing some class, and decided to use inside private method and > some private variables. While with method i haven't got any problem's > with variables i have. There is no mechanism in Python that makes attributes truly private. self._number is an attribute just like any other, the whole business with _leading_underscores is purely a matter of convention. If you have an instance attribute or method with a leading underscore, you know that using it or calling it isn't something you're supposed to do outside its class, but nothing will stop you doing exactly that if you're rude enough to try. Does that help? -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Tue Jul 21 18:01:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 00:01:35 +0200 Subject: Balanced binary tree implementation References: Message-ID: >>>>> Lucas P Melo (LPM) wrote: >LPM> Hello, >LPM> I would like to use a balanced binary tree implementation (preferably >LPM> within some API). >LPM> Any hints about where I could find it? >LPM> I am looking for something that implements insertion, deletion, search and >LPM> a special search that returns the lesser element bigger than a given key >LPM> [1]. >LPM> A nice possibility would be an extensible API that allows me to inherit its >LPM> classes and to add operations myself. >LPM> Thanks in advance. >LPM> [1] Ex: 1 2 3 4 5 6 are elements of the bbt. If I use this operation given >LPM> 4 as the parameter, the value returned would be 5. http://newcenturycomputers.net/projects/rbtree.html (warning: see end of page!) http://pypi.python.org/pypi/rbtree/ http://pyavl.sourceforge.net/ http://code.activestate.com/recipes/576817/ -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at mrabarnett.plus.com Tue Jul 21 18:04:39 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 21 Jul 2009 23:04:39 +0100 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order In-Reply-To: References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: <4A663B77.3020408@mrabarnett.plus.com> davidj411 wrote: > On Jul 21, 5:29 pm, Simon Forman wrote: >> On Jul 21, 5:00 pm, davidj411 wrote: >> >> >> >> >> >>> I am using a recursive function to print the time and a few other >>> things on each pass. ( the function calculates size of file that is >>> being transferred and if not 100 % copied, it waits 20 secs and checks >>> again). >>> i would expect the time to be correct anytime it is used: >>> <--code below -->> >>> print time.strftime('%m-%d-%Y %H:%m:%S') >>> <--code above -->> >>> here is an example of what i am seeing: >>> 16:07:16 >>> 16:07:36 >>> 16:07:56 >>> 16:07:16 >>> 16:07:36 >>> 16:07:56 >>> 16:07:16 >>> 16:07:36 >>> 16:07:56 >> Your output doesn't match your format string: >> >> In [1]: import time >> >> In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') >> 07-21-2009 17:07:16 >> >> There's no way to tell why your output times seem to repeat without >> seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: >> %m:%S')" line. > > sorry, yes, i did manually filter the output. > > here is the function: > > def log_out(msg,servername='std.out'): > print msg > open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" > + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') > > on each pass, it should output the newer time (whether recursive or > not, right) ? Maybe it does, but you were outputting the month ("07") instead of the minutes; the seconds were changing. From piet at cs.uu.nl Tue Jul 21 18:11:38 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 00:11:38 +0200 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: >>>>> davidj411 (d) wrote: >>> >>> > <--code below -->> >>> > print time.strftime('%m-%d-%Y %H:%m:%S') >>> > <--code above -->> >d> here is the function: >d> def log_out(msg,servername='std.out'): >d> print msg >d> open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" >d> + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') >d> on each pass, it should output the newer time (whether recursive or >d> not, right) ? How come your strftime is now different from the first posting? Are you cheating? If you want to ask a question about why your code is not working properly there are a few important rules: 1. Copy and paste your code, and at least all relevant stuff (preferably a minimal example that shows the problem). *DO NOT RETYPE THE CODE* 2. Copy and paste the output. *DO NOT RETYPE THE OUTPUT* 3. Tell what the expected or desired output is. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Tue Jul 21 18:29:52 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 00:29:52 +0200 Subject: doted filenames in import statements References: Message-ID: >>>>> Jean-Michel Pichavant (JP) wrote: >JP> Hi fellows, >JP> I'd like to use the dynamic __import__ statement. It works pretty well with >JP> non dotted names, but I cannot figure how to make it work with dotted file >JP> paths. What is a dotted file path? Is that 4.6.0.0? >JP> example: >JP> file = "/home/dsp/test.py" >JP> test = __import__(file) >JP> works like a charm That's not supposed to work. In older pythons it did work but that's a bug. In newer pythons it doesn't. __import__ works on module names, not on file names. Python 2.6: >>> spam = __import__('/Users/piet/TEMP/test.py', globals(), locals(), [], -1) Traceback (most recent call last): File "", line 1, in ImportError: Import by filename is not supported. >>> >JP> file = "/home/dsp/4.6.0.0/test.py" >JP> test = __import__(file) >JP> => no module name blalalal found. >JP> Any suggestion ? I tried multiple escape technics without any success. Rightly so. I think the best would be to add the directory to sys.path sys.path.add('/home/dsp/4.6.0.0') and then __import__('test', ... ) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From emile at fenx.com Tue Jul 21 18:37:40 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 21 Jul 2009 15:37:40 -0700 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <7xiqho1c2i.fsf@ruckus.brouhaha.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <7xiqho1c2i.fsf@ruckus.brouhaha.com> Message-ID: On 7/19/2009 4:35 PM Paul Rubin said... > Emile van Sebille writes: >>>> Most people would still consider Solaris to be a "major platform". >>> ?? I do not, but I have no idea what comes in 4th after the other >>> three by whatever metric. >> one metric calls fourth as the iPhone OS... >> http://marketshare.hitslink.com/report.aspx?qprid=8... > > That metric is clearly wrong. The #1 platform OS platform in terms of > number of units shipped is Javacard, which is in the SIM cards of > billions of GSM phones. Yes -- that location includes disclaimers on how they reach their conclusions. From lists at cheimes.de Tue Jul 21 18:48:40 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 22 Jul 2009 00:48:40 +0200 Subject: doted filenames in import statements In-Reply-To: <50697b2c0907211459w88c3e28y2a96811bd307233c@mail.gmail.com> References: <4A663651.5020006@sequans.com> <50697b2c0907211459w88c3e28y2a96811bd307233c@mail.gmail.com> Message-ID: Chris Rebert wrote: > You want the imp.load_module() function: > http://docs.python.org/library/imp.html#imp.load_module > > __import__() only operates on module/package names. I'm not sure how > you even got it to work with a filename... It used to work with filenames but it was a bug. I guess several people are swearing curses on me for removing this 'feature'. *g* Christian From ebonak at hotmail.com Tue Jul 21 19:06:01 2009 From: ebonak at hotmail.com (Esmail) Date: Tue, 21 Jul 2009 19:06:01 -0400 Subject: comments? storing a function in an object In-Reply-To: References: Message-ID: Thanks *everyone* for your help, and sharing your knowledge. I had no idea that Python functions could have user defined attributes, and I may now not even need the object to wrap my functions in. Amazing what one can learn here .. thanks again! Esmail From nyamatongwe+thunder at gmail.com Tue Jul 21 19:06:02 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Tue, 21 Jul 2009 23:06:02 GMT Subject: If Scheme is so good why MIT drops it? In-Reply-To: <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: milanj: > and all of them use native threads (python still use green threads ?) Python uses native threads. Neil From david at boddie.org.uk Tue Jul 21 19:24:32 2009 From: david at boddie.org.uk (David Boddie) Date: Wed, 22 Jul 2009 01:24:32 +0200 Subject: Pyserial and pyQt References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> Message-ID: On Tuesday 21 July 2009 21:37, Seth wrote: > I have used pyserial in the past but this is my first experience with > pyQt. I am using the Python xy package for windows current but might > move to linux. I have a small device that is outputting a basic text > string. I want to be able to read this string(from the comm port) and > update a text box and eventually a graph in pyQt. I can't find any > documentation or tutorials on how to do this. If anyone can point me > in the right direction or give me some tips I would be grateful. It seems that someone has already asked a similar question on Stack Overflow, though perhaps you should start with a simpler solution and look at more advanced ones later: http://stackoverflow.com/questions/771988/pyqt4-and-pyserial One starting point is this list of tutorials on the PyQt and PyKDE Wiki: http://www.diotavelli.net/PyQtWiki/Tutorials Later, when you want to draw graphs, you might find PyQwt useful: http://pyqwt.sourceforge.net/ You may already be aware that there's also a mailing list for PyQt and PyKDE: http://www.riverbankcomputing.com/pipermail/pyqt/ Another way to get answers to questions is to join the #pyqt IRC channel at freenode.net: irc://irc.freenode.net/ David From david.lyon at preisshare.net Tue Jul 21 19:48:24 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 21 Jul 2009 19:48:24 -0400 Subject: setuptools question. In-Reply-To: References: Message-ID: <21107bd58153b0a10d52ce5d3d9b70db@preisshare.net> On Tue, 21 Jul 2009 11:12:01 -0700, Fred C wrote: > I have a python program and when I install this program from the > module home directory using setup.py everything works fine. > > But easy_install fails with the following error, because I am trying > to install some start up shell scripts into /etc/init.d > > > The package setup script has attempted to modify files on your system > >that are not within the EasyInstall build area, and has been aborted. > > Is there a way to use easy_install to install the start scripts into / > etc/init.d. If not, what is the best way to install these scripts. What package is it? what platform ? From sajmikins at gmail.com Tue Jul 21 19:59:21 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 16:59:21 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: <16e1d810-bdf0-46b8-aef1-7e57eeb30547@k19g2000yqn.googlegroups.com> On Jul 21, 5:53?pm, davidj411 wrote: > On Jul 21, 5:29?pm, Simon Forman wrote: > > > > > On Jul 21, 5:00?pm, davidj411 wrote: > > > > I am using a recursive function to print the time and a few other > > > things on each pass. ( the function calculates size of file that is > > > being transferred and if not 100 % copied, it waits 20 secs and checks > > > again). > > > > i would expect the time to be correct anytime it is used: > > > > <--code below -->> > > > print time.strftime('%m-%d-%Y %H:%m:%S') > > > <--code above -->> > > > > here is an example of what i am seeing: > > > > 16:07:16 > > > 16:07:36 > > > 16:07:56 > > > 16:07:16 > > > 16:07:36 > > > 16:07:56 > > > 16:07:16 > > > 16:07:36 > > > 16:07:56 > > > Your output doesn't match your format string: > > > In [1]: import time > > > In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') > > 07-21-2009 17:07:16 > > > There's no way to tell why your output times seem to repeat without > > seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: > > %m:%S')" line. > > sorry, yes, i did manually filter the output. > > here is the function: > > def log_out(msg,servername='std.out'): > ? ? ? ? print msg > ? ? ? ? open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" > + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') > > on each pass, it should output the newer time (whether recursive or > not, right) ? Well, as Piet van Oostrum pointed out, your problem in the first code you posted was that you used '%m' rather than '%M' for the minutes. (Good eye Van Oostrum!) But now in this function you seem to have the correct '%M' field. Are you still having the same output after changing that? In any event, here's a rewritten version of that function that's a little cleaner, FWIW. from os.path import join from time import strftime format = '%m-%d-%Y %H:%M:%S' def log_out(msg, servername='std.out'): print msg msg = "%s - %s %s\n" % (servername, strftime(format), msg) log_file = open(join(log_dir, servername + ".log"), 'a') try: log_file.write(msg) finally: log_file.close() But why not just use the logging module? From mail.to.daniel.platz at googlemail.com Tue Jul 21 20:00:13 2009 From: mail.to.daniel.platz at googlemail.com (Daniel Platz) Date: Tue, 21 Jul 2009 17:00:13 -0700 (PDT) Subject: Fast reading and unpacking of binary data (struct module) Message-ID: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Hi, I have a Python newbie question about reading data from a binary file. I have an huge binary file from an external program. I want to read and process the data in this file in a reasonable time. It turns out that the reading of the data itself and the processing do not need most of the time. However, when using the read(bytes) method Python returns a string representing the binary information in hex. This string I have to "cast/translate" into a number (in my case a signed short). For this I am using the method struct.unpack from the struct module. This unpacking part of the program takes by far the most time. Is there a way to speed this up or to do it the unpacking more cleverly than with the struct module? Thanks in advance. With kind regards, Daniel From ronn.ross at gmail.com Tue Jul 21 20:08:57 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Tue, 21 Jul 2009 19:08:57 -0500 Subject: trouble with minidom Message-ID: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Hello I'm trying to read an xml file using minidome. The xml looks like: myProj /here/ My code looks like so: from xml.dom.minidom import parse dom = parse("myfile.xml") for node in dom.getElementsByTagName("project'): print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, node.childNodes[1]) Unfortunately, it returns 'nodeValue as none. I'm trying to read the value out of the node fir example name: myProj. I haven't found much help in the documentation. Can someone point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Tue Jul 21 20:18:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 21:18:42 -0300 Subject: Fast reading and unpacking of binary data (struct module) References: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Message-ID: En Tue, 21 Jul 2009 21:00:13 -0300, Daniel Platz escribi?: > I have an huge binary file from an external program. I want to read > and process the data in this file in a reasonable time. It turns out > that the reading of the data itself and the processing do not need > most of the time. However, when using the read(bytes) method Python > returns a string representing the binary information in hex. This > string I have to "cast/translate" into a number (in my case a signed > short). For this I am using the method struct.unpack from the struct > module. This unpacking part of the program takes by far the most time. > Is there a way to speed this up or to do it the unpacking more > cleverly than with the struct module? Try creating a Struct object with your format and use its unpack() method. http://docs.python.org/library/struct.html#struct-objects If your format consists of just integers, probably an array is more efficient: http://docs.python.org/library/array.html#array.array.fromfile -- Gabriel Genellina From greg at cosc.canterbury.ac.nz Tue Jul 21 20:18:44 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:18:44 +1200 Subject: tough-to-explain Python In-Reply-To: <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7cimf0F27u23eU1@mid.individual.net> <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> Message-ID: <7cn45nF28j1d4U1@mid.individual.net> Simon Forman wrote: > My understanding (so far) is that you (hope to) /derive/ correct code > using formal logic, rather than writing code and then proving its > soundness. But to the extent you can rigorously formalise it, all you've done is create Yet Another Programming Language. Which is fine, if you can come up with one that works at a high enough level in some domain that you can see just by looking that the program will do what you want. However, I suspect that you can't improve much on what we've already got without restricting the domain of applicability of the language. Otherwise, you just shift the intellectual bottleneck from writing a correct program to writing correct specifications. In the realm of programming language design, it's been said that you can't eliminate complexity, all you can do is push it around from one place to another. I suspect something similar applies to the difficulty of writing programs. -- Greg From greg at cosc.canterbury.ac.nz Tue Jul 21 20:28:39 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:28:39 +1200 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <87skgq8m7s.fsf@benfinney.id.au> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> <87skgq8m7s.fsf@benfinney.id.au> Message-ID: <7cn4obF286u4kU1@mid.individual.net> Ben Finney wrote: > My point was rather meant to imply that > subclassing the built-in (immutable) string types was the best way to > usefully get all their functionality However, it would be difficult to do that without changing all C code that deals with strings, including that in extension modules. That's because the existing string type stores the characters in the string object itself. A mutable variant would have to contain a pointer to a resizable memory block, and therefore couldn't be used as a drop-in replacement by existing C code that expects a string. -- Greg From gagsl-py2 at yahoo.com.ar Tue Jul 21 20:32:03 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 21:32:03 -0300 Subject: trouble with minidom References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Message-ID: En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross escribi?: > Hello I'm trying to read an xml file using minidome. The xml looks like: > > > myProj > /here/ > > > > My code looks like so: > from xml.dom.minidom import parse > > dom = parse("myfile.xml") > > for node in dom.getElementsByTagName("project'): > print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, > node.childNodes[1]) > > Unfortunately, it returns 'nodeValue as none. I'm trying to read the > value > out of the node fir example name: myProj. I haven't found much help in > the > documentation. Can someone point me in the right direction? Unless you have a specific reason to use the DOM interface (like having a masochistic mind), working with ElementTree usually is a lot easier: py> import xml.etree.ElementTree as ET py> xml = """ ... ... myProj ... /here/ ... ... """ py> doc = ET.fromstring(xml) py> for project in doc.findall('project'): ... for child in project.getchildren(): ... print child.tag, child.text ... name myProj path /here/ -- Gabriel Genellina From khanhqh2008i at gmail.com Tue Jul 21 20:32:22 2009 From: khanhqh2008i at gmail.com (khanh le) Date: Wed, 22 Jul 2009 07:32:22 +0700 Subject: Python Message-ID: do you have any materials about Python? can you show me the link of Python or some books? thanks a lots! -- Regard! Khanh -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg at cosc.canterbury.ac.nz Tue Jul 21 20:35:18 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:35:18 +1200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a645b81$0$12974$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <7cn54pF28nv72U1@mid.individual.net> William Dode wrote: > I just tested it with a litle game, to find the places of horse on > a board 5x5. The result is : > > cython avec malloc *int 18s Posting benchmark times for Pyrex or Cython is pretty meaningless without showing the exact code that was used, since times can vary enormously depending on how much you C-ify things. -- Greg From david.lyon at preisshare.net Tue Jul 21 20:37:50 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 21 Jul 2009 20:37:50 -0400 Subject: Python In-Reply-To: References: Message-ID: <00e687679e63af535bea6a4046f1df08@preisshare.net> On Wed, 22 Jul 2009 07:32:22 +0700, khanh le wrote: > do you have any materials about Python? > can you show me the link of Python or some books? http://www.python.org/ > thanks a lots! No problem. Have a nice day. From casey.mcginty at gmail.com Tue Jul 21 20:43:27 2009 From: casey.mcginty at gmail.com (Casey McGinty) Date: Tue, 21 Jul 2009 14:43:27 -1000 Subject: SMTPlib and SMTPd Performance Issues Message-ID: Hi, I just wanted mention a few workarounds I've come up with for the Python SMTP modules in regards to performance. Before I started, I was getting about 15MB/s while sending e-mail from smtplib to smptd over a local connection. (i.e. both client/server running on the same machine). After the following changes, I'm seeing around*220+MB/s * (increase of 14x) The source can be found here: http://svn.python.org/view/python/trunk/Lib/smtplib.py?view=markup http://svn.python.org/view/python/trunk/Lib/smtpd.py?view=markup When sending e-mail through *smtpdlib*, the following method is called. def quotedata(data): """Quote data for email. Double leading '.', and change Unix newline '\\n', or Mac '\\r' into Internet CRLF end-of-line. """ return re.sub(r'(?m)^\.', '..', re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)) As you can see there are two regular expressions parsing the data. If you know that your message is formatted correctly to start with, then this step is unnecessary. When receiving e-mail through *smtpd*, the SMTPChannel class inherits from * asynchat.async_chat*. The default recv buffer size for asynchat is 4K. This can be too much overhead for high data throughput. The buffer size can be increased with this code: import asynchat asynchat.async_chat.ac_in_buffer_size = 1024*128 asynchat.async_chat.ac_out_buffer_size = 1024*128 The *smtpd.SMTP* class prcoesses data through the *smtpd.SMTPChannel* class. There are a lot of debug statements in the module, like so: print >> DEBUGSTREAM, 'Data:', repr(line) By default, DEBUGSTREAM is a no-op, but that that doesn't prevent repr(line) from being called. When variable, line, contains a large email (multiple megabytes), this debug step will really kill performance. Secondly, the method *found_terminator* will also perform expensive strings ops on the e-mail. Maybe its not possible to disable this step, in all cases, but for testing performance, you can override the method like so: class QuickSMTPChannel( smtpd.SMTPChannel, object): def found_terminator(self): if (self._SMTPChannel__state == self.COMMAND or self._SMTPChannel__state != self.DATA): super(QuickSMTPChannel,self).found_terminator() else: data = smtpd.EMPTYSTRING.join(self._SMTPChannel__line) self._SMTPChannel__line = [] status = self._SMTPChannel__server.process_message( self._SMTPChannel__peer, self._SMTPChannel__mailfrom, self._SMTPChannel__rcpttos, data) self._SMTPChannel__rcpttos = [] self._SMTPChannel__mailfrom = None self._SMTPChannel__state = self.COMMAND self.set_terminator('\r\n') if not status: self.push('250 Ok') else: self.push(status Thanks, - Casey -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg at cosc.canterbury.ac.nz Tue Jul 21 20:47:35 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:47:35 +1200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: <7cn5rqF28emnnU1@mid.individual.net> >>Steven D'Aprano writes: >> >>>Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing >>>to do. It's the right thing to do if you're doing integer maths. True, but the question is how best to decide whether the programmer wants to do integer maths. Deciding based on the types of the operands is okay in a statically typed language. But it's asking for trouble in a dynamically-typed language, especially where it's common practice to use ints as a substitute for floats that happen to have integer values. EIBTI in this case. -- Greg From http Tue Jul 21 21:07:24 2009 From: http (Paul Rubin) Date: 21 Jul 2009 18:07:24 -0700 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7cimf0F27u23eU1@mid.individual.net> <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> <7cn45nF28j1d4U1@mid.individual.net> Message-ID: <7x4ot5bk4z.fsf@ruckus.brouhaha.com> greg writes: > However, I suspect that you can't improve much on what we've already > got without restricting the domain of applicability of the > language. Otherwise, you just shift the intellectual bottleneck from > writing a correct program to writing correct specifications. Really, it's easier to write specifications. They're a level of abstraction away from the implementation. Think of a sorting function that operates on a list a1,a2...a_n. The sorting algorithm might be some fiendishly clever, hyper-optimized hybrid of quicksort, heapsort, timsort, burstsort, and 52-pickup. It might be thousands of lines of intricate code that looks like it could break at the slightest glance. The specification simply says the output has the property a1<=a2<=...<=a_n. That is a lot easier to say. If you can then prove that the program satisfies the specification, you are far better off than if you have some super-complex pile of code that appears to sort when you test it, but has any number of possible edge cases that you didn't figure out needed testing. It's like the difference between stating a mathematical theorem (like the four-color theorem) and proving it. From pjb at informatimago.com Tue Jul 21 21:19:19 2009 From: pjb at informatimago.com (Pascal J. Bourguignon) Date: Wed, 22 Jul 2009 03:19:19 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <2009072118415916807-tfb@cleycom> Message-ID: <87my6xa50o.fsf@galatea.local> Tim Bradshaw writes: > On 2009-07-19 19:31:36 +0100, Frank Buss said: > >> (e.g. I >> don't know of a free modern and stable Lisp implemenation with >> mulithreading support for Windows, with a licence with which you can use it >> in closed source commercial programs, like you can do with Python). > > Openmcl seems reasonably stable to me, is LLGPL-licensed, and runs on > these platforms and Solaris x86. > > It's kind of tragic, of course, that this kind of parasitic behaviour > (will not consider a commercial product to use in your commercial > system) has now become so common. GPL. -- __Pascal Bourguignon__ From dstanek at dstanek.com Tue Jul 21 22:05:46 2009 From: dstanek at dstanek.com (David Stanek) Date: Tue, 21 Jul 2009 22:05:46 -0400 Subject: Changing the private variables content In-Reply-To: References: <4A662B36.3010708@gmail.com> Message-ID: On Tue, Jul 21, 2009 at 6:00 PM, Rhodri James wrote: > On Tue, 21 Jul 2009 21:55:18 +0100, Ryniek90 wrote: > >> Hi. >> I'm writing some class, and decided to use inside private method and some >> private variables. While with method i haven't got any problem's with >> variables i have. > > There is no mechanism in Python that makes attributes truly private. > self._number is an attribute just like any other, the whole business > with _leading_underscores is purely a matter of convention. ?If you > have an instance attribute or method with a leading underscore, you > know that using it or calling it isn't something you're supposed > to do outside its class, but nothing will stop you doing exactly that > if you're rude enough to try. > Doubling the _ will give you a little more privacy. It really just mangles the attribute name, but it close to what you want. I just use a _single_under to tell other programmers that they shouldn't be accessing that attribute directly. To my knowledge nothing bad has happened because things are public and using the __double_under makes testing a little harder. >>> class C(object): ... def __init__(self): ... self.__x = 0 ... >>> c = C() >>> c.__x Traceback (most recent call last): File "", line 1, in AttributeError: 'C' object has no attribute '__x' >>> c._C__x 0 -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From gagsl-py2 at yahoo.com.ar Tue Jul 21 22:18:26 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 23:18:26 -0300 Subject: Python References: <00e687679e63af535bea6a4046f1df08@preisshare.net> Message-ID: En Tue, 21 Jul 2009 21:37:50 -0300, David Lyon escribi?: > On Wed, 22 Jul 2009 07:32:22 +0700, khanh le > wrote: >> do you have any materials about Python? >> can you show me the link of Python or some books? > > http://www.python.org/ Also see http://wiki.python.org/moin/BeginnersGuide -- Gabriel Genellina From prateekkakirwar at gmail.com Wed Jul 22 00:42:39 2009 From: prateekkakirwar at gmail.com (Prateek) Date: Tue, 21 Jul 2009 21:42:39 -0700 (PDT) Subject: Python References: <00e687679e63af535bea6a4046f1df08@preisshare.net> Message-ID: <6281cf33-8ab6-4ff6-a2c5-a9fa1e66d368@m7g2000prd.googlegroups.com> Chk dis out ... http://www.swaroopch.com/notes/Python It's gr8 guide for beginners Cheers!!! Prateek From gagsl-py2 at yahoo.com.ar Wed Jul 22 01:02:55 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 02:02:55 -0300 Subject: Detect target name in descriptor __set__ method Message-ID: I have a class attribute 'foo' which is a data descriptor. I create an instance of such class. When I say instance.foo = value, the descriptor __set__ method is called. Is there any way to obtain the name being assigned to? ('foo' in this example). That is, I want to know the target name for the assignment that triggered the __set__ method call. class descriptor(object): def __get__(self, instance, owner): print 'descriptor.__get__ self=%r instance=%r owner=%r' % ( self, instance, owner) return self def __set__(self, instance, value): # I want to know the *name* this value is being assigned to print 'descriptor.__set__ self=%r instance=%r value=%r' % ( self, instance, value) def __delete__(self, instance): print 'descriptor.__delete__ self=%r instance=%r' % ( self, instance) class X(object): foo = descriptor() x = X() x.foo = "value" I can obtain the name descriptor() was assigned to at the time the X class was defined, using a custom metaclass: class Meta(type): def __new__(meta, name, bases, dict): for key,value in dict.iteritems(): if isinstance(value, descriptor): value.name = key return type.__new__(meta, name, bases, dict) class Y(object): __metaclass__ = Meta foo = descriptor() y = Y() print y.foo.name # prints 'foo' This is good enough for me (assuming no one modifies the class after defining it, no two names refer to the same descriptor, no two classes share the same descriptor instance...). But I would like to use a more direct/robust approach, if available. Any ideas? -- Gabriel Genellina From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Wed Jul 22 01:09:35 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Wed, 22 Jul 2009 01:09:35 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: On 2009-07-21 19:06:02 -0400, Neil Hodgson said: > Python uses native threads. So it can be teh-slowness on all ur cores! The global interpreter lock doesn't help much either. -- Raffael Cavallaro From wolfgang at rohdewald.de Wed Jul 22 01:24:21 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Wed, 22 Jul 2009 07:24:21 +0200 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: <200907220724.21185.wolfgang@rohdewald.de> On Wednesday 22 July 2009, Gabriel Genellina wrote: > x = X() > x.foo = "value" del x.foo -- Wolfgang From wolfgang at rohdewald.de Wed Jul 22 01:28:11 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Wed, 22 Jul 2009 07:28:11 +0200 Subject: Detect target name in descriptor __set__ method In-Reply-To: <200907220724.21185.wolfgang@rohdewald.de> References: <200907220724.21185.wolfgang@rohdewald.de> Message-ID: <200907220728.11536.wolfgang@rohdewald.de> On Wednesday 22 July 2009, Wolfgang Rohdewald wrote: > On Wednesday 22 July 2009, Gabriel Genellina wrote: > > x = X() > > x.foo = "value" > > del x.foo sorry, was not yet quite awaken - I read "delete target name" instead of "detect target name" -- Wolfgang From gagsl-py2 at yahoo.com.ar Wed Jul 22 01:43:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 02:43:07 -0300 Subject: Regular exprssion for IRC messages References: Message-ID: En Fri, 17 Jul 2009 13:59:47 -0300, nohics nohics escribi?: > When an IRC client receive a messages, this message in in a special > format > defined here: Message format in pseudo > BNF( > > I want to transforme the message format defined in the irc protocole in > the > last link, to a regular expression to get the nickname, the username, the > host, the commad, it's arguments if exist, and the message after ":" You may want to use the pyparsing module instead of a monstruous and unmantainable regular expression: http://pyparsing.wikispaces.com/ -- Gabriel Genellina From jhinak.sen at gmail.com Wed Jul 22 02:28:27 2009 From: jhinak.sen at gmail.com (jhinak sen) Date: Wed, 22 Jul 2009 11:58:27 +0530 Subject: plotting graph from another file Message-ID: <3d4ff96c0907212328q211801b4tc7a86986a9c35c25@mail.gmail.com> hello.. i want to plot a distribution graph for following data: *elements: number of occurance: a 2 b 4 c 1 d 9 e 3* ( this a simple version) the data is in a file , say abc.txt how can i call/import the text file and draw an appropriate distribution graph in python ? can it be done using matplotlib ? if yes , please give me the code for plotting the importing the text file and plotting the graph. thanx jhinak -------------- next part -------------- An HTML attachment was scrubbed... URL: From hendrik at microcorp.co.za Wed Jul 22 02:36:29 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 22 Jul 2009 08:36:29 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: <200907220836.30064.hendrik@microcorp.co.za> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > On Jul 20, 12:27?pm, Phillip B Oldham > > wrote: > > [snip] We > > understand that lists are mutable and tuples are not, but we're a > > little lost as to why the two were kept separate from the start. They > > both perform a very similar job as far as we can tell. > > My guess is that it was probably for optimization reasons long ago. > I've never heard a *good* reason why Python needs both. The good reason is the immutability, which lets you use a tuple as a dict key. - Hendrik From steven at REMOVE.THIS.cybersource.com.au Wed Jul 22 02:40:16 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 22 Jul 2009 06:40:16 GMT Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: On Tue, 21 Jul 2009 00:39:24 +0200, Niels L. Ellegaard wrote: > Phillip B Oldham writes: > >> We often find we need to do manipulations like the above without >> changing the order of the original list, and languages like JS allow >> this. We can't work out how to do this in python though, other than >> duplicating the list, sorting, reversing, then discarding. I think Phillip is confused if he thinks that other languages can sort a list without modifying or duplicating the original. If you don't duplicate the list, how can you sort it without modifying the original? The only difference is whether the language makes the duplicate for you, or expects you to do it yourself. > If you just want a one-liner, and you don't care about speed you can do > the following (but I don't think this is considered best practice) > >>>> x = [2,1,3] >>>> print list(sorted(x)) The call to list is redundant, as sorted() already creates a list. But that's the wrong solution to the problem. The OP wants the largest (or smallest) item, which he expects to get by sorting, then grabbing the first element: sorted(alist)[0] That requires sorting the entire list, only to throw away everything except the first item. A better solution is to use min() and max(), neither of which sort the list, so they are much faster. -- Steven From steven at REMOVE.THIS.cybersource.com.au Wed Jul 22 02:45:54 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 22 Jul 2009 06:45:54 GMT Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: On Tue, 21 Jul 2009 06:49:59 -0700, Inky 788 wrote: > On Jul 20, 12:27?pm, Phillip B Oldham wrote: >> [snip] We >> understand that lists are mutable and tuples are not, but we're a >> little lost as to why the two were kept separate from the start. They >> both perform a very similar job as far as we can tell. > > My guess is that it was probably for optimization reasons long ago. I've > never heard a *good* reason why Python needs both. Suppose you could do this: key = [1, 2] adict = {key: 'x', [1, 1]: 'y'} This would work as expected: adict[ [1, 2] ] => returns 'x' adict[ [1, 1] ] => returns 'y' But what happens if you mutate the key? key[1] = 0 adict[ [1, 2] ] => raises KeyError Okay, that's bad. What's even worse is this: key[1] = 1 adict[ [1, 1] ] => should it return 'x' or 'y'? The solution is to disallow mutable objects like lists from being used as keys in dicts, and to allow immutable list-like tuples instead. -- Steven From clp2 at rebertia.com Wed Jul 22 02:46:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 21 Jul 2009 23:46:13 -0700 Subject: plotting graph from another file In-Reply-To: <3d4ff96c0907212328q211801b4tc7a86986a9c35c25@mail.gmail.com> References: <3d4ff96c0907212328q211801b4tc7a86986a9c35c25@mail.gmail.com> Message-ID: <50697b2c0907212346t3673b2a0rc5669bf7663bc4c6@mail.gmail.com> On Tue, Jul 21, 2009 at 11:28 PM, jhinak sen wrote: > hello.. > > i want to plot a distribution graph for following data: > elements:?????????????? number of occurance: > ?? a ?? ? ? ? ? ? ? ? ? ? ? ? ? 2 > ?? b??????????????????????????? 4 > ?? c??????????????????????????? 1 > ?? d??????????????????????????? 9 > ?? e??????????????????????????? 3 > > ( this a simple version) > > the data is in a file , say abc.txt > > how can i call/import the text file and draw an appropriate distribution > graph in python? ? > can it be done using matplotlib ? > > if yes , please give me the code for plotting the? importing the text file > and plotting the graph. Given matplot's versatility, I would presume with high likelihood that matplot could make such a chart. However, the purpose of this mailinglist is *not* to do people's job/work/homework for them for free. I would suggest you first *attempt* coding a solution yourself. If you run into trouble, read http://catb.org/~esr/faqs/smart-questions.html Then ask a more specific question on the mailinglist. Best of luck. Regards, Chris From bearophileHUGS at lycos.com Wed Jul 22 02:48:55 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 21 Jul 2009 23:48:55 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> Message-ID: <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> greg: > Posting benchmark times for Pyrex or Cython is pretty > meaningless without showing the exact code that was > used, since times can vary enormously depending on > how much you C-ify things. Was this link, shown by William, not enough? http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 Bye, bearophile From greg at cosc.canterbury.ac.nz Wed Jul 22 02:51:22 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 18:51:22 +1200 Subject: clean way prepend an element to a numpy array In-Reply-To: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> Message-ID: <7cnr5tF28183hU1@mid.individual.net> bdb112 wrote: > I saw this interest syntax on this site > x[:0]=0 > > I guess that is cute, but could be confusing....(and doesn't work) It does if you use an array of the appropriate type on the right hand side: a[:0] = array('i', [0]) -- Greg From walkraft at gmail.com Wed Jul 22 02:59:34 2009 From: walkraft at gmail.com (casebash) Date: Tue, 21 Jul 2009 23:59:34 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> <87skgq8m7s.fsf@benfinney.id.au> <7cn4obF286u4kU1@mid.individual.net> Message-ID: Thanks all for your advice. I'm not actually going to use the mutable string right at the moment, but I thought I might in the future and I was just curious if it existed. I suppose a list of characters is close enough for most purposes. On Jul 22, 10:28?am, greg wrote: > Ben Finney wrote: > > My point was rather meant to imply that > > subclassing the built-in (immutable)stringtypes was the best way to > > usefully get all their functionality > > However, it would be difficult to do that without changing > all C code that deals with strings, including that in extension > modules. > > That's because the existingstringtype stores the characters > in thestringobject itself. Amutablevariant would have to > contain a pointer to a resizable memory block, and therefore > couldn't be used as a drop-in replacement by existing C > code that expects astring. > > -- > Greg From timr at probo.com Wed Jul 22 03:09:07 2009 From: timr at probo.com (Tim Roberts) Date: Wed, 22 Jul 2009 00:09:07 -0700 Subject: win32api install problem References: Message-ID: <8led65544idaf3630nk21rjij030aj4s4k@4ax.com> Gerry wrote: >I'm running Python 2.6 under XP. > >I've installed Windows 32 extensions for Python 2.6 version 1.4 >(pywin32-214.win32-py2.6.exe). > >But If I try to import win32api, I get: > > File "C:\python_projects\euler\driveletters.py", line 1, in > import win32api >ImportError: DLL load failed: The specified module could not be found. > >\Python26\Lib\site-packages has: > >03/23/2009 08:35 AM win32 >07/20/2009 09:08 AM win32com >02/18/2009 01:21 PM win32comext > >Can anyone offer a suggestion? It is very unusual that those three directories should have different creation dates. Normally, all three would have the same date and time, from whenever you ran the installer. I would suggest that you go into Add and Remove Programs, uninstall pywin32, and run the installer again. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From dtgeadamo at yahoo.com Wed Jul 22 03:13:49 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Wed, 22 Jul 2009 00:13:49 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> <-vadnaHpJMoIjPvXnZ2dnUVZ8tli4p2d@bt.com> Message-ID: On Jul 21, 8:05?pm, "Martin P. Hellwig" wrote: > David Adamo Jr. wrote: > > On Jul 21, 10:40 am, "Martin P. Hellwig" > > wrote: > >> sightseer wrote: > > >> > > >>> Error Installing Service: Access is Denied. (5) > >> > >> Are you trying to do this on windows vista? > > >> -- > >> MPHhttp://blog.dcuktec.com > >> 'If consumed, best digested with added seasoning to own preference.' > > > Yeah, I was trying to do it on Vista. Someone has just helped me out. > > I had to deactivate User Account Control on Windows Vista...and now > > everything is rosy. Thanks guys. > > No need to deactivate it, just right click on the command shell program > and say run as administrator, than you can install the service via the > command line. > > -- > MPHhttp://blog.dcuktec.com > 'If consumed, best digested with added seasoning to own preference.' Thanks MPH From milesck at umich.edu Wed Jul 22 03:21:06 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 22 Jul 2009 03:21:06 -0400 Subject: trouble with minidom In-Reply-To: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Message-ID: On Jul 21, 2009, at 8:08 PM, Ronn Ross wrote: > Hello I'm trying to read an xml file using minidome. The xml looks > like: > > > myProj > /here/ > > > > My code looks like so: > from xml.dom.minidom import parse > > dom = parse("myfile.xml") > > for node in dom.getElementsByTagName("project'): > print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, > node.childNodes[1]) > > Unfortunately, it returns 'nodeValue as none. I'm trying to read the > value out of the node fir example name: myProj. I haven't found much > help in the documentation. Can someone point me in the right > direction? Two issues: In your example XML file, the first child node of the project Element is the Text node containing the whitespace between the and tags. node.childNodes[0] will select that whitespace node. The nodeValue of an Element is null (None). In order to get the text contents of an element, you must get the nodeValue of the Text child node of the Element. Like Gabriel, I would recommend using an XML library with a more concise API than the W3C DOM (I like lxml.objectify). But if you stick with xml.dom, use the specification as a reference: http://www.w3.org/TR/REC-DOM-Level-1/ From hv at tbz-pariv.de Wed Jul 22 04:05:19 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Wed, 22 Jul 2009 10:05:19 +0200 Subject: Workflow Libraries (DB vs Code) In-Reply-To: <7cm6kmF26g9srU1@mid.uni-berlin.de> References: <7cm1ifF28jmnhU1@mid.individual.net> <7cm6kmF26g9srU1@mid.uni-berlin.de> Message-ID: <7cnvi0F28qk9aU1@mid.individual.net> Diez B. Roggisch schrieb: > Thomas Guettler wrote: > >> Hi, >> >> I need to write some simple workflows with web interface. >> >> For the web stuff I will use django, but I am not sure how >> to do the workflow part. > > Did you consider using OpenERP? It comes with a web-frontend > (TG1.0.8-based), but also offers a XMLRPC-server to connect to, so using it > from within django shouldn't be any difficult. > > Workflow-definitions are stored in the DB, and can be generated visually, > not sure if they have to though. Thank you for this hint. OpenERP seems well done. But the licence is GPL and the dependency is too big. But I will have a look at how they did it. Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From joncle at googlemail.com Wed Jul 22 04:21:49 2009 From: joncle at googlemail.com (Jon Clements) Date: Wed, 22 Jul 2009 01:21:49 -0700 (PDT) Subject: Regular exprssion for IRC messages References: Message-ID: <4e59a9a3-f565-4042-9016-5192b8ff8247@c29g2000yqd.googlegroups.com> On 22 July, 06:43, "Gabriel Genellina" wrote: > En Fri, 17 Jul 2009 13:59:47 -0300, nohics nohics ? > escribi?: > > > When an IRC client receive a messages, this message in in a special ? > > format > > defined here: Message format in pseudo > > BNF( > > > I want to transforme the message format defined in the irc protocole in ? > > the > > last link, to a regular expression to get the nickname, the username, the > > host, the commad, it's arguments if exist, and the message after ":" > > You may want to use the pyparsing module instead of a monstruous and ? > unmantainable regular expression: > > http://pyparsing.wikispaces.com/ > > -- > Gabriel Genellina Alternatively, if IIRC, the twisted framework has an IRC client, which automatically parses server messages. From dtgeadamo at yahoo.com Wed Jul 22 04:27:52 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Wed, 22 Jul 2009 01:27:52 -0700 (PDT) Subject: Ideas for problem with chat server application! Message-ID: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> I developed a chat application with an attendant chat server. Everything is working fine. The issue now is the fact that whenever the chat server goes down (for instance, the server system shuts down as a result of power failure or some other problem), by the time the server system come back on, the chat server would have to be restarted manually. I believe (and I know) it is more appropriate for the chat server application to restart itself when the computer comes back on (and of course regardless of who is logged in and of course, even before anyone logs in). I have a batch file that executes the chat server. My attempt was to create a windows service that start automatically and runs this batch file using a Network Service account on the server system. Although, I'm having a hard time with this (temporarily), I would love to ask if there are any alternatives to using a windows service. Suggestions are highly appreciated. From joncle at googlemail.com Wed Jul 22 04:31:13 2009 From: joncle at googlemail.com (Jon Clements) Date: Wed, 22 Jul 2009 01:31:13 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: Message-ID: <3cb5f960-16f7-4fc6-841f-d0706f0e1d0a@s31g2000yqs.googlegroups.com> On 22 July, 06:02, "Gabriel Genellina" wrote: > I have a class attribute 'foo' which is a data descriptor. I create an ? > instance of such class. When I say instance.foo = value, the descriptor ? > __set__ method is called. Is there any way to obtain the name being ? > assigned to? ('foo' in this example). That is, I want to know the target ? > name for the assignment that triggered the __set__ method call. > > class descriptor(object): > ? ?def __get__(self, instance, owner): > ? ? ?print 'descriptor.__get__ self=%r instance=%r owner=%r' % ( > ? ? ? ?self, instance, owner) > ? ? ?return self > > ? ?def __set__(self, instance, value): > ? ? ?# I want to know the *name* this value is being assigned to > ? ? ?print 'descriptor.__set__ self=%r instance=%r value=%r' % ( > ? ? ? ?self, instance, value) > > ? ?def __delete__(self, instance): > ? ? ?print 'descriptor.__delete__ self=%r instance=%r' % ( > ? ? ? ?self, instance) > > class X(object): > ? ?foo = descriptor() > > x = X() > x.foo = "value" > > I can obtain the name descriptor() was assigned to at the time the X class ? > was defined, using a custom metaclass: > > class Meta(type): > ? ?def __new__(meta, name, bases, dict): > ? ? ?for key,value in dict.iteritems(): > ? ? ? ?if isinstance(value, descriptor): > ? ? ? ? ?value.name = key > ? ? ?return type.__new__(meta, name, bases, dict) > > class Y(object): > ? ?__metaclass__ = Meta > ? ?foo = descriptor() > > y = Y() > print y.foo.name # prints 'foo' > > This is good enough for me (assuming no one modifies the class after ? > defining it, no two names refer to the same descriptor, no two classes ? > share the same descriptor instance...). But I would like to use a more ? > direct/robust approach, if available. > > Any ideas? > > -- > Gabriel Genellina >>> class Test: def __setattr__(self, what, value): print what, value >>> t = Test() >>> t.foo = 'x' foo x Is that what you're after? Jon. From phillip.oldham at gmail.com Wed Jul 22 05:27:56 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Wed, 22 Jul 2009 02:27:56 -0700 (PDT) Subject: Suggestions for Python MapReduce? Message-ID: I understand that there are a number of MapReduce frameworks/tools that play nicely with Python (Disco, Dumbo/Hadoop), however these have "large" dependencies (Erlang/Java). Are there any MapReduce frameworks/ tools which are either pure-Python, or play nicely with Python but don't require the Java/Erlang runtime environments? From user at example.net Wed Jul 22 06:03:44 2009 From: user at example.net (superpollo) Date: Wed, 22 Jul 2009 12:03:44 +0200 Subject: binary literal Message-ID: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> hello clp. i can insert a hex value for a character literal in a string: >>> stuff = "\x45" >>> print stuff E >>> can i do something like the above, but using a *binary* number? (e.g. 00101101 instead of 45) ? bye From pmarti at warp.es Wed Jul 22 06:24:22 2009 From: pmarti at warp.es (=?ISO-8859-1?Q?Pablo_Mart=ED_Gamboa?=) Date: Wed, 22 Jul 2009 12:24:22 +0200 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <53a2f8770907220324m445c3004s9c844176255eaf46@mail.gmail.com> 2009/7/22 superpollo > hello clp. > > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? (Python 3) >>> bin(45) '0b101101' >>> int(_, 2) 45 -- Pablo Mart? http://www.linkedin.com/in/pmarti || http://www.warp.es python -c "print '706d6172746940776172702e6573'.decode('hex')" -------------- next part -------------- An HTML attachment was scrubbed... URL: From eckhardt at satorlaser.com Wed Jul 22 06:25:32 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Wed, 22 Jul 2009 12:25:32 +0200 Subject: binary literal References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? There are binary number literals since 2.6 and there is the chr() function. >>> print chr(0b1000101) E Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From theajodharan.vfx at gmail.com Wed Jul 22 06:29:59 2009 From: theajodharan.vfx at gmail.com (TNR) Date: Wed, 22 Jul 2009 11:29:59 +0100 Subject: Python In-Reply-To: <6281cf33-8ab6-4ff6-a2c5-a9fa1e66d368@m7g2000prd.googlegroups.com> References: <00e687679e63af535bea6a4046f1df08@preisshare.net> <6281cf33-8ab6-4ff6-a2c5-a9fa1e66d368@m7g2000prd.googlegroups.com> Message-ID: Hi, Check the below links.. http://www.tutorialspoint.com/python/ http://www.java2s.com/Tutorial/Python/CatalogPython.htm Hope that helps. Cheers -theajo On Wed, Jul 22, 2009 at 5:42 AM, Prateek wrote: > Chk dis out ... > > http://www.swaroopch.com/notes/Python > > It's gr8 guide for beginners > > Cheers!!! > Prateek > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjmachin at lexicon.net Wed Jul 22 06:34:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 22 Jul 2009 10:34:16 +0000 (UTC) Subject: Fast reading and unpacking of binary data (struct module) References: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Message-ID: Daniel Platz googlemail.com> writes: > > Hi, > > I have a Python newbie question about reading data from a binary file. > I have an huge binary file from an external program. Please translate "huge" into Mb. Also, how much free real memory do you have? > I want to read > and process the data in this file in a reasonable time. It turns out > that the reading of the data itself and the processing do not need > most of the time. However, when using the read(bytes) method Python > returns a string representing the binary information in hex. "In hex" is not part of the string's nature. If you look at it through hexadecimal/decimal/octal/binary spectacles, you'll see respectively hex/decimal/octal/binary. > This > string I have to "cast/translate" into a number (in my case a signed > short). For this I am using the method struct.unpack from the struct > module. This unpacking part of the program takes by far the most time. > Is there a way to speed this up or to do it the unpacking more > cleverly than with the struct module? Are you reading the file (a) two bytes at a time (b) several Kb at a time (c) whole file? Are you unpacking it ... [same options as above but the two answers may differ]? What are you planning to do with this large list/array? The array option that Gabriel mentioned should be fastest to load, and take up least memory, but working with the elements will require making int objects out of the shorts (and the reverse if you are updating the array). Is numpy (http://numpy.scipy.org/) a possibility? Cheers, John From drunkard at thepub.co.za Wed Jul 22 07:05:31 2009 From: drunkard at thepub.co.za (Mark du Preez) Date: Wed, 22 Jul 2009 13:05:31 +0200 Subject: Documentation Problems Message-ID: Hi Can anyone tell me where to go to suggest changes to the Python documentation? Thanks. From ndbecker2 at gmail.com Wed Jul 22 07:12:07 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 22 Jul 2009 07:12:07 -0400 Subject: Fast reading and unpacking of binary data (struct module) References: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Message-ID: Daniel Platz wrote: > Hi, > > I have a Python newbie question about reading data from a binary file. > I have an huge binary file from an external program. I want to read > and process the data in this file in a reasonable time. It turns out > that the reading of the data itself and the processing do not need > most of the time. However, when using the read(bytes) method Python > returns a string representing the binary information in hex. This > string I have to "cast/translate" into a number (in my case a signed > short). For this I am using the method struct.unpack from the struct > module. This unpacking part of the program takes by far the most time. > Is there a way to speed this up or to do it the unpacking more > cleverly than with the struct module? > > Thanks in advance. > > With kind regards, > > Daniel Consider mmap Consider numpy Consider numpy+mmap From mail at timgolden.me.uk Wed Jul 22 07:15:22 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 22 Jul 2009 12:15:22 +0100 Subject: Documentation Problems In-Reply-To: References: Message-ID: <4A66F4CA.2070506@timgolden.me.uk> Mark du Preez wrote: > Hi > > Can anyone tell me where to go to suggest changes to the Python > documentation? Drop an entry in the tracker: http://bugs.python.org Patches are always welcome, or at least suggested wording rather than vague "I don't think it should say that" -- although that's better than nothing! TJG From nipunreddevil at gmail.com Wed Jul 22 07:15:33 2009 From: nipunreddevil at gmail.com (nipun batra) Date: Wed, 22 Jul 2009 16:45:33 +0530 Subject: GTK+ ques Message-ID: <1e61b3260907220415g6edb24sb7fe07c83fe346c8@mail.gmail.com> I need to develop a GUI preferably in GTK using c or PYGTK.I have little idea about Python. Following are requiremnts of the application: should be GUI Sould be real time Should interface serial port should display moving maps,like google earth It is basically for Ground control station of a UAV. How would Python+Pygtk combo work? is python slower than c/c++ ,if then by how much? any suggestions for the applcation? -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Wed Jul 22 07:15:45 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 22 Jul 2009 13:15:45 +0200 Subject: Documentation Problems References: Message-ID: <7coan1F28i1q2U1@mid.uni-berlin.de> Mark du Preez wrote: > Hi > > Can anyone tell me where to go to suggest changes to the Python > documentation? > > Thanks. http://docs.python.org/ has a "bugs"-section that leads to http://docs.python.org/bugs.html You can file doc-bugs in the tracker, and check if they are already fixed in the dev-docs. Diez From wilk at flibuste.net Wed Jul 22 07:38:08 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 11:38:08 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> Message-ID: <4a66fa20$0$11368$426a74cc@news.free.fr> I updated the script (python, c and java) with your unrolled version + somes litle thinks. I also tried with python3.1, unladen Q2, ironpython1.1.1 Unfortunately it doesn't work more with shedskin, i'll see on the shedskin group... c 1.85s gcj 2.15s java 2.8s python2.5 + psyco 3.1s unladen-2009Q2 145s (2m45) python2.5 254s (4m14s) python3.1 300s (5m) ironpython1.1.1 680s (11m20) -- William Dod? - http://flibuste.net Informaticien Ind?pendant From wfluro at gmail.com Wed Jul 22 07:38:43 2009 From: wfluro at gmail.com (ulf) Date: Wed, 22 Jul 2009 04:38:43 -0700 (PDT) Subject: Python ctypes on win64 Message-ID: Hi, Does anybody know if python includes a win64 version of ctypes? According to the documentation it should be included - at least there's no special remarks for win64, and I haven't found any recent notes saying that it shouldn't work on win64. Yet it looks like both the installer from Python.org and from ActiveState.com have disabled it. regards /ulfw From duncan.booth at invalid.invalid Wed Jul 22 07:55:13 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 22 Jul 2009 11:55:13 GMT Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: Steven D'Aprano wrote: > But that's the wrong solution to the problem. The OP wants the largest > (or smallest) item, which he expects to get by sorting, then grabbing > the first element: > > sorted(alist)[0] > > That requires sorting the entire list, only to throw away everything > except the first item. A better solution is to use min() and max(), > neither of which sort the list, so they are much faster. > And if they want the n smallest or largest items (where n is significantly less than the length of the list[*]) they might consider using heapq.nsmallest() and heapq.nlargest() I find it interesting that the heapq functions tell you in the documentation that they aren't suitable for use where n==1 or where n is near the total size of the sequence whereas random.sample() chooses what it thinks is the best algorithm based on the input. At the very least I would have thought the heapq functions could check for n==1 and call min/max when it is. [*] Some quick playing with timeit seems to indicate that with a list of 200 integers nsmallest is fastest (by a very small margin) when n==2 and n==3 but is beaten by sorted[:n] when n==4, so I guess you need a reasonably long list to make it worth not sorting it: with list of 20,000 integers it isn't worth sorting unless you want more than about 700 items. -- Duncan Booth http://kupuguy.blogspot.com From deets at nospam.web.de Wed Jul 22 07:55:29 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 22 Jul 2009 13:55:29 +0200 Subject: List insertion cost References: Message-ID: <7cod1hF28nhhlU1@mid.uni-berlin.de> Lucas P Melo wrote: > Hello, > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? O(n) I'd say. You need to copy nearly the whole list, and sometimes re-allocate it's storage (they are internally stored as arrays of object references) Diez From jeanmichel at sequans.com Wed Jul 22 08:16:19 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 22 Jul 2009 14:16:19 +0200 Subject: doted filenames in import statements In-Reply-To: References: Message-ID: <4A670313.8040204@sequans.com> Piet van Oostrum wrote: > > [snip] > >> JP> file = "/home/dsp/4.6.0.0/test.py" >> JP> test = __import__(file) >> JP> => no module name blalalal found. >> > > >> JP> Any suggestion ? I tried multiple escape technics without any success. >> > > Rightly so. > > I think the best would be to add the directory to sys.path > sys.path.add('/home/dsp/4.6.0.0') > and then > __import__('test', ... ) > I see. My problem is that a have to import 2 different files having de same name. In the same name space I have 2 objects from 2 different software branches, let's say 4.6.0 and 4.6.1. The first object shall import 4.6.0/orb.py and the second one 4.6.1/orb.py. If I add 4.6.1 to sys.path, the import statement will look like: self._orb = __import__('orb') The problem is, python wil assume orb is already imported and will assign the module from the 4.6.0 branch to my 4.6.1 object. Do I have to mess up with sys.modules keys to make python import the correct file ? Is there a standard/proper way to do that ? JM From ryniek90 at gmail.com Wed Jul 22 08:29:20 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 22 Jul 2009 14:29:20 +0200 Subject: Changing the private variables content In-Reply-To: References: Message-ID: <4A670620.1090501@gmail.com> > > ------------------------------------------------------------------------ > > Temat: > Re: Changing the private variables content > Od: > Gary Herron > Data: > Tue, 21 Jul 2009 14:14:44 -0700 > Do: > Ryniek90 > > Do: > Ryniek90 > Kopia: > python-list at python.org > > > Ryniek90 wrote: >> Hi. >> I'm writing some class, and decided to use inside private method and >> some private variables. While with method i haven't got any problem's >> with variables i have. >> Maybe some example. >> A class with private method and private variable: >> >> " >> >>> class Secret(object): >> # >> def __init__(self): >> self._number = 1 >> # >> def _secret(self): >> print self._number >> def showit(self): >> print "Secret number is:\n" >> self._secret() >> >> >>> sec = Secret() >> >>> sec.showit() >> Secret number is: >> >> 1 >> >>> sec._number >> 1 >> >>> sec._number = 2 >> >>> sec._number >> 2 >> >>> sec._number += 3 >> >>> sec._number >> 5 >> >>> >> " >> >> As You can see, i made class with private method and private variable >> inside __init__ constructor. I've changed also the variable value, >> but outside class. >> I've got problem with changing some variable value _inside__my_ >> class, which i'm writing. > > Not sure this is what you are asking, but a method (which is how I > interpret "_inside__my_ class") changes the value by normal assignment > like this: > > class Secret(object): > ... > def SetNumber(self,value): > self._number = value > > Is that what you were asking? > > > Gary Herron > > > >> I've searched over google, some tuts with topics about operations on >> private variables, but didn't found anything - only how to make them, >> but no how to assign new objects/override them with new content (and >> other advanced and helpful options). >> >> If someone could help me, it would be great. >> >> Thanks. > > Thanks for hint, but looks like i can't do what i want. Maybe i show You my class: " class ModPrint(object): u""" This will be the doc. """ def __init__(self): #Assign the Python installation directory - sys.exec_prefix - to variable self._default_search_path=sys.exec_prefix self._chosen_module = '' self._user_search_path = '' self._this_module = '' def _SetVar(self, attr, val): self.attr = val def _search_for_module(self, *args): """Private method which walks through default Python installation directories, and search for prefered module.""" #walking thru all directories available in path '_user_search_path' for root, dirs, files in os.walk(self._user_search_path): for f in files: #if found file 'f' is THAT module, #full path to THAT file is assigned to variable if f == ("%s.py" % self._chosen_module): self._SetVar(self._this_module, os.path.join(root, f)) def print_module(self, _chosen_module='', _user_search_path='', _default_search_path=sys.exec_prefix,): """Reads module chosen by user, and returns full content of this module, as it is.""" #if custom search path hasn't been assigned, #default path is being assigned as custom path if self._user_search_path == '': self._user_search_path = self._default_search_path #using private method '_search_for_module' with 'self.' preffix #to search for prefered module self._search_for_module(_chosen_module, _user_search_path) #opening prefered module with read-only binary mode #and assigning it to 'module_open' variable module_open = open(self._this_module, 'rb') #reading this file and assigning it to variable module_text = module_open.read() #closing read file; the read content is still available #it's stored in variable 'module_text' module_open.close() #returning the read content return module_text " When i use this class in Python IDLE, i've got this error: " >>> mod = ModPrint() >>> import socket >>> mod.print_module('socket') Traceback (most recent call last): File "", line 1, in mod.print_module('socket') File "", line 48, in print_module module_open = open(self._this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " As You can see, i can't assign the new value "os.path.join(root, f)" to the 'self._this_module variable'. So for sure i've made some mistake in method: " def _SetVar(self, attr, val): self.attr = val " When i've changed private variables to normal, stored in class (in __init__ method), it was the same situation - i couldn't change this variable value. " >>> mod = ModPrint() >>> mod.print_module('os') Traceback (most recent call last): File "", line 1, in mod.print_module('os') File "", line 48, in print_module module_open = open(self.this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " Thanks i someone could help me, give some hint. From contact at xavierho.com Wed Jul 22 08:45:37 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 22:45:37 +1000 Subject: Changing the private variables content In-Reply-To: <4A670620.1090501@gmail.com> References: <4A670620.1090501@gmail.com> Message-ID: <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> On Wed, Jul 22, 2009 at 10:29 PM, Ryniek90 wrote: > Thanks for hint, but looks like i can't do what i want. That's because > > def _SetVar(self, attr, val): > self.attr = val doesn't work as you might think. However, I tried to replace it with: val('self.' + attr + '=\'' + val + '\'') and it gives me a syntax error! File "test.py", line 7, in _setVar eval('self.' + attr + '=\'' + val + '\'') File "", line 1 self.var='Mrra' ^ SyntaxError: invalid syntax So, uh, I'm wondering if this is a bug. And also the solution to his problem. Good luck. Any ideas appreciated. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From python-url at phaseit.net Wed Jul 22 08:48:25 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Wed, 22 Jul 2009 12:48:25 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 22) Message-ID: QOTW: "If programming is symbol manipulation, then you should remember that the user interface is also symbol manipulation, and it is a MUCH harder problem than databases, sorting, searching, and all the other problems you learn about in academia. The user interface has to communicate over a rich but noisy channel using multiple under-specified protocols to a couple of pounds of meat which processes information using buggy heuristics evolved over millions of years to find the ripe fruit, avoid being eaten, and have sex. If you think getting XML was hard, that's *nothing* compared to user interfaces. The fact that even bad UIs work at all is a credit to the heuristics, bugs and all, in the meat." - Steven D'Aprano http://groups.google.com/group/comp.lang.python/msg/8c65eacbd76e79cf Invoke the same method over every object in a collection: http://groups.google.com/group/comp.lang.python/browse_thread/thread/8fd293a9b39c8733/ There is no 'xor' boolean operator, why? What should be its outcome? http://groups.google.com/group/comp.lang.python/browse_thread/thread/16eec722310e75e8/ A thread-safe method to create unique identifiers (not necesarily increasing integers): http://groups.google.com/group/comp.lang.python/browse_thread/thread/dfe22a6446c057df/ Tim Chase tells us three ways to turn standard output into unbuffered mode: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a65f71444bd4bb53/ How to receive data of unknown length using sockets: http://groups.google.com/group/comp.lang.python/browse_thread/thread/9e4a9f8b6e83320/ Override a method, but keeping the inherited docstring: http://groups.google.com/group/comp.lang.python/browse_thread/thread/1e4075ba10dcbdd9/ Limiting the execution time of a code fragment: http://groups.google.com/group/comp.lang.python/browse_thread/thread/734054de170e2904/ Sharing a big object between processes using the multiprocessing module: http://groups.google.com/group/comp.lang.python/browse_thread/thread/833d4988b7af6353/ Why aren't OrderedDicts [3.1] comparable using <, >, <=, >=? http://groups.google.com/group/comp.lang.python/browse_thread/thread/c3c6f4fe7b6d487d/ So many ways to call a function - why is that? Beginners get confused: http://groups.google.com/group/comp.lang.python/browse_thread/thread/3fd57f5ee4850530/ The reasons to choose your favorite programming language (Python, I presume!): http://groups.google.com/group/comp.lang.python/browse_thread/thread/5d87008e328efcf9/ Tuples, lists, their differences and motivation: http://groups.google.com/group/comp.lang.python/browse_thread/thread/cb0cf56c52321ccc/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From contact at xavierho.com Wed Jul 22 08:52:45 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 22:52:45 +1000 Subject: Changing the private variables content In-Reply-To: <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> References: <4A670620.1090501@gmail.com> <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> Message-ID: <2d56febf0907220552q6a96107ds7b60ba46ac82ef6a@mail.gmail.com> > > val('self.' + attr + '=\'' + val + '\'') > Obviously that was eval, not val. Also it doesn't work without the escaped single quotes, either. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark.dufour at gmail.com Wed Jul 22 08:55:57 2009 From: mark.dufour at gmail.com (srepmub) Date: Wed, 22 Jul 2009 05:55:57 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: please send any program that doesn't work with shedskin (it still is experimental after all) to me, or create an issue at shedskin.googlecode.com, and I will have a look at the problem. thanks, mark. From wilk at flibuste.net Wed Jul 22 08:59:37 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 12:59:37 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: <4a670d39$0$11286$426a74cc@news.free.fr> On 22-07-2009, srepmub wrote: > > please send any program that doesn't work with shedskin (it still is > experimental after all) to me, or create an issue at > shedskin.googlecode.com, and I will have a look at the problem. I did it, on the discussion group http://groups.google.com/group/shedskin-discuss/browse_thread/thread/c1f47a7c21897b44 -- William Dod? - http://flibuste.net Informaticien Ind?pendant From disappearedng at gmail.com Wed Jul 22 09:04:18 2009 From: disappearedng at gmail.com (disappearedng) Date: Wed, 22 Jul 2009 21:04:18 +0800 Subject: Finding all cycles within an undirected graph Message-ID: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> Hey everyone, I am interested to find out all the cycles within an undirected graph. I don't have a particular implementation of nodes and vertices yet, so would someone kindly point out a good graph library for this ( I looked at pygraph and python-graph, but not really satisfied ), in addition to good algorithm for solving this issue. From contact at xavierho.com Wed Jul 22 09:06:22 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 23:06:22 +1000 Subject: Changing the private variables content In-Reply-To: <2d56febf0907220552q6a96107ds7b60ba46ac82ef6a@mail.gmail.com> References: <4A670620.1090501@gmail.com> <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> <2d56febf0907220552q6a96107ds7b60ba46ac82ef6a@mail.gmail.com> Message-ID: <2d56febf0907220606q19e343f2mdd276d4e734ee048@mail.gmail.com> Got it: exec('self.' + attr + '=\'' + val + '\'') That worked. I think it'll do what you want now ;) Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Jul 22 09:11:26 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 09:11:26 -0400 Subject: Multiple versions of python In-Reply-To: <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> Message-ID: <4A670FFE.80405@ieee.org> CCW wrote: > On 21 July, 15:19, Dave Angel wrote: > >> ChrisW wrote: >> >>> Hi, >>> >>> I have installed 2 versions of python on my Windows XP computer - I >>> originally had 3.0.1, but then found that the MySQL module only >>> supported 2.*, so I've now installed that. I have found that if I >>> change the Windows Environment Variable path, then I can change the >>> version of python called when I type 'python' into a command line. >>> However, I'd like to be able to choose which version I use. I know >>> that if I change C:\Python26\python.exe to >>> C:\Python26\python2.exe and C:\Python30\python.exe to C: >>> \Python26\python3.exe, then typing 'python2' or 'python3' will invoke >>> the correct interpreter. However, is it safe just to rename the >>> executable files? Is there a more elegant way to achieve the same >>> task? >>> >>> Thanks, >>> Chris >>> >> The elegant way is to have a batch directory on your PATH ( I use >> m:\t\bat ) and put your *.bat files there. I do NOT put any python >> installations on the PATH. >> >> For example, I have a batch file called: m:\t\bat\python26.bat >> >> c:\progfiles\python26\python.exe %* >> >> The %* syntax means pass all arguments through to the program. >> >> Once it all works, you can add an "@" in front of the c: in order to >> suppress echoing the command. At that point, it'll look and work the >> same way as what you did, but without modifying anything in the install >> directory. (You may want python26.bat, pythonw26.bat, python31.bat >> and pythonw31.bat) >> >> The other thing you may want to do in a batch file is to change the file >> associations so that you can run the .py file directly, without typing >> "python" or "pythonw" in front of it. >> >> The relevant Windows commands are: assoc and ftype And on a >> related note, you may want to edit the PATHEXT environment variable, to >> add .PY and .PYW >> > > Thanks for this - this way made a bit more sense to me. I've now got > C:\commands with the 4 .bat files in, and C:\commands in my path. It > all seems to work :) I think I've missed the point of the @ though - > it doesn't seem to make any difference.. > > I'm also a bit confused with the associations - since I've got python > 2.6 and 3.1, surely the command I type (python26 or python31) is the > only way to force a script to be run using a specific interpreter at > runtime without having to change the .bat file every time I want to > run a script using 3.1 instead of 2.6? > > The @ symbol was used in older versions of CMD and COMMAND to suppress echoing of the command line. I think if you're using XP or later, it doesn't matter. As for file associations, I don't know just what you already know about. When you type data.doc at a command line, the system looks for the data file, then if it's found, it looks up the program associated with that extension. On my machine, that's a particular version of Word for Windows. Similarly, if I type digest.py and I'm in the directory containing that file, the system looks up my associations, and runs Python 2.6 on that file. If I wanted it to run Python 3.1 on that file, I'd have to change the associations, temporarily. So, first approximation, it's a way to avoid having to type PYTHON each time I want to run a script, as long as I don't need any other arguments on the (implied) command line. Further, the system will then look for digest.py everywhere on the PATH, so I don't even have to be in the same directory. And that means I can be in the data directory that digest.py is going to work on. Effectively, it raises digest.py to feeling like an executable, for most purposes. In fact, I put all my scripts in a directory, along with other simple executables. I use m:\t\bin for that purpose. These associations (to .py and .pyw) are established by the installer program for Python. But when you had multiple installs, you could choose whether the second one installed overrides the first. My point is that if you need to change them back and forth, you could use assoc and ftype to do it. On my system: M:\LabOrders>assoc .py .py=Python.File M:\LabOrders>ftype Python.File Python.File="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* M:\LabOrders> So my .py files are associated with the 2.6.2 version of ActivePython installation. Note the similarity of the ftype to your python26.bat file? One more little trick is that I could just type digest instead of digest.py, if I have added the .py and .pyw extensions to my PATHEXT environment variable. So my PATHEXT looks like: PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY;.PYW Note that the association changes made by assoc and ftype are global. They apply to all cmd.exe windows, even those already running, and they survive a reboot. But when you set environment variables like PATHEXT, you can choose to do it in a single window (with SET), or globally (using control-panel). If I found myself switching often, I'd make separate ftype entries for each interpreter, something like Python.File26="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* Python.File31="C:\PROGFI~1\..... and just change assoc .py to point at the one I needed today. As it is, I'm content leaving the associations pointing at 2.6, and explicitly entering PYTHON31 in front of any script name I want to run on that interpreter. DaveA From python at mrabarnett.plus.com Wed Jul 22 09:12:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 22 Jul 2009 14:12:18 +0100 Subject: Changing the private variables content In-Reply-To: <4A670620.1090501@gmail.com> References: <4A670620.1090501@gmail.com> Message-ID: <4A671032.10304@mrabarnett.plus.com> Ryniek90 wrote: >> >> ------------------------------------------------------------------------ >> >> Temat: >> Re: Changing the private variables content >> Od: >> Gary Herron >> Data: >> Tue, 21 Jul 2009 14:14:44 -0700 >> Do: >> Ryniek90 >> >> Do: >> Ryniek90 >> Kopia: >> python-list at python.org >> >> >> Ryniek90 wrote: >>> Hi. >>> I'm writing some class, and decided to use inside private method and >>> some private variables. While with method i haven't got any problem's >>> with variables i have. >>> Maybe some example. >>> A class with private method and private variable: >>> >>> " >>> >>> class Secret(object): >>> # >>> def __init__(self): >>> self._number = 1 >>> # >>> def _secret(self): >>> print self._number >>> def showit(self): >>> print "Secret number is:\n" >>> self._secret() >>> >>> >>> sec = Secret() >>> >>> sec.showit() >>> Secret number is: >>> >>> 1 >>> >>> sec._number >>> 1 >>> >>> sec._number = 2 >>> >>> sec._number >>> 2 >>> >>> sec._number += 3 >>> >>> sec._number >>> 5 >>> >>> >>> " >>> >>> As You can see, i made class with private method and private variable >>> inside __init__ constructor. I've changed also the variable value, >>> but outside class. >>> I've got problem with changing some variable value _inside__my_ >>> class, which i'm writing. >> >> Not sure this is what you are asking, but a method (which is how I >> interpret "_inside__my_ class") changes the value by normal assignment >> like this: >> >> class Secret(object): >> ... >> def SetNumber(self,value): >> self._number = value >> >> Is that what you were asking? >> >> >> Gary Herron >> >> >> >>> I've searched over google, some tuts with topics about operations on >>> private variables, but didn't found anything - only how to make them, >>> but no how to assign new objects/override them with new content (and >>> other advanced and helpful options). >>> >>> If someone could help me, it would be great. >>> >>> Thanks. >> >> > > > Thanks for hint, but looks like i can't do what i want. > > Maybe i show You my class: > > " > > class ModPrint(object): > u""" > This will be the doc. > """ > def __init__(self): > #Assign the Python installation directory - sys.exec_prefix > - to variable > self._default_search_path=sys.exec_prefix > self._chosen_module = '' > self._user_search_path = '' > self._this_module = '' > def _SetVar(self, attr, val): > self.attr = val def _search_for_module(self, *args): > """Private method which walks through default Python > installation directories, and search for prefered module.""" > #walking thru all directories available in path > '_user_search_path' > for root, dirs, files in os.walk(self._user_search_path): > for f in files: > #if found file 'f' is THAT module, > #full path to THAT file is assigned to variable > if f == ("%s.py" % self._chosen_module): > self._SetVar(self._this_module, os.path.join(root, f)) > def print_module(self, > _chosen_module='', _user_search_path='', > _default_search_path=sys.exec_prefix,): > """Reads module chosen by user, and returns full content of > this module, as it is.""" > #if custom search path hasn't been assigned, > #default path is being assigned as custom path > if self._user_search_path == '': > self._user_search_path = self._default_search_path > #using private method '_search_for_module' with 'self.' > preffix > #to search for prefered module > self._search_for_module(_chosen_module, _user_search_path) > #opening prefered module with read-only binary mode > #and assigning it to 'module_open' variable > module_open = open(self._this_module, 'rb') > #reading this file and assigning it to variable > module_text = module_open.read() > #closing read file; the read content is still available > #it's stored in variable 'module_text' > module_open.close() > #returning the read content > return module_text > > " > > When i use this class in Python IDLE, i've got this error: > " > >>> mod = ModPrint() > >>> import socket > >>> mod.print_module('socket') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 48, in print_module > module_open = open(self._this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > As You can see, i can't assign the new value "os.path.join(root, f)" to > the 'self._this_module variable'. > So for sure i've made some mistake in method: > > " > def _SetVar(self, attr, val): > self.attr = val " > When i've changed private variables to normal, stored in class (in > __init__ method), it was the same situation - i couldn't change this > variable value. > > " > >>> mod = ModPrint() > >>> mod.print_module('os') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('os') > File "", line 48, in print_module > module_open = open(self.this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > Thanks i someone could help me, give some hint. What is the point of the _SetVar method? Instead of: self._SetVar(self._this_module, os.path.join(root, f)) just do: self._this_module = os.path.join(root, f) (unless I'm misunderstanding what you're trying to do!) From alan.isaac at gmail.com Wed Jul 22 09:14:12 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Wed, 22 Jul 2009 13:14:12 GMT Subject: invoke method on many instances In-Reply-To: References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>>> def apply2(itr, methodname, *args, **kwargs): >>>> f = operator.methodcaller(methodname, *args, **kwargs) >>>> for item in itr: >>>> f(item) >> On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >>> for obj in objects: >>> getattr(obj, methodname)(*args, **kwargs) > En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac: >> Are there any obvious considerations in choosing >> between those two? On 7/20/2009 3:29 AM Gabriel Genellina apparently wrote: > The operator.methodcaller version is faster in my tests for large > collections, but slightly slower when you have very few elements. So it seems. Is this easily explained? Thanks, Alan Isaac From breamoreboy at yahoo.co.uk Wed Jul 22 09:18:55 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 Jul 2009 14:18:55 +0100 Subject: Python In-Reply-To: References: Message-ID: khanh le wrote: > do you have any materials about Python? > can you show me the link of Python or some books? > thanks a lots! > > -- > Regard! > Khanh > This comes highly recommeended http://diveintopython.org/ Regards. From Caseyweb at gmail.com Wed Jul 22 09:23:33 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Wed, 22 Jul 2009 06:23:33 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: Message-ID: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> On Jul 22, 5:27?am, Phillip B Oldham wrote: > I understand that there are a number of MapReduce frameworks/tools > that play nicely with Python (Disco, Dumbo/Hadoop), however these have > "large" dependencies (Erlang/Java). Are there any MapReduce frameworks/ > tools which are either pure-Python, or play nicely with Python but > don't require the Java/Erlang runtime environments? I can't answer your question, but I would like to better understand the problem you are trying to solve. The Apache Hadoop/MapReduce java application isn't really that "large" by modern standards, although it is generally run with large heap sizes for performance (-Xmx1024m or larger for the mapred.child.java.opts parameter). MapReduce is designed to do extremely fast parallel data set processing on terabytes of data over hundreds of physical nodes; what advantage would a pure Python approach have here? From contact at xavierho.com Wed Jul 22 09:24:05 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 23:24:05 +1000 Subject: Changing the private variables content In-Reply-To: <4A671032.10304@mrabarnett.plus.com> References: <4A670620.1090501@gmail.com> <4A671032.10304@mrabarnett.plus.com> Message-ID: <2d56febf0907220624u7401550ei24461dd9ca7d6934@mail.gmail.com> > What is the point of the _SetVar method? > > So you can set any variable in that class, I guess? -------------- next part -------------- An HTML attachment was scrubbed... URL: From brendon.wickham at gmail.com Wed Jul 22 09:28:16 2009 From: brendon.wickham at gmail.com (Brendon Wickham) Date: Wed, 22 Jul 2009 23:28:16 +1000 Subject: Getting cookie "expires" value Message-ID: Hi there, My web application uses a cookie to set/get a session id. This is working fine. However, I'm unable to work out how to get the cookie "expires" value. Below is test code that replicates the problem. It creates a simple web page and shows a cookie value if it's found, or creates a new one (correctly setting the "expires" value) and indicates that it was created: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #!/usr/bin/env python import Cookie,os def printHeader(): ??? print "Content-type: text/html\n" ??? print "Cookie test" ??? print "" cookies=Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE')) if cookies.has_key('test'): ??? #If cookie was found: ??? printHeader() ??? print "Found cookie:

" ??? #Get cookie: ??? c = cookies.get('test') ??? #Print its value: ??? print "
Value: %s
" % c.value ??? #Iterate through its items and print name/value: ??? for k,v in c.items(): ??? ??? ?print "
%s=%s
" % (k,v) else: ??? #Define cookie: ??? cookies['test'] = "1234" ??? cookies['test']['expires'] = 3600 ??? #Save cookie: ??? print cookies['test'] ??? #Now print HTML: ??? printHeader() ??? print "Cookie created." print "" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This is what I get if the cookie exists: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Found cookie: Value: 1234 comment= domain= secure= expires= max-age= version= path= <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< As you can see, the cookie attributes are valueless. Can anyone help? Am I missing something fundamental? Cheers, Brendon From steve at REMOVE-THIS-cybersource.com.au Wed Jul 22 09:28:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 22 Jul 2009 13:28:23 GMT Subject: Changing the private variables content References: Message-ID: <02770500$0$5185$c3e8da3@news.astraweb.com> On Wed, 22 Jul 2009 14:29:20 +0200, Ryniek90 wrote: > When i use this class in Python IDLE, i've got this error: " ... > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 48, in print_module > module_open = open(self._this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > As You can see, i can't assign the new value "os.path.join(root, f)" to > the 'self._this_module variable'. In the __init__ method, you successfully set the `_this_module` attribute: self._this_module = '' That works, so it proves that you *can* set a private attribute. Later on, you call the `_SetVar` method, which does this: def _SetVar(self, attr, val): self.attr = val In English, this method does this: (1) Take two arguments, and store them in variables called "attr" and "val". (2) Assign the value stored in "val" to an attribute named exactly "attr". What you actually want is: (1) Take two arguments, and store them in variables called "attr" and "val". (2) Assign the value stored in "val" to an attribute with the name stored in the variable "attr". Can you see the difference? What you actually want is: def _SetVar(self, attr, val): setattr(self, attr, val) Your code is terribly complicated. This will probably do what you want. (Warning -- I haven't tested it, so there may be a few errors.) class ModPrint(object): u""" This will be the doc. """ def __init__(self, default_search_path=''): self.default_search_path = '' def find_module(self, modulename, search_path=''): if search_path == '': # Try the default search path instead. search_path = self.default_search_path if search_path == '': # still blank. # Try the Python search path instead. search_path = sys.exec_prefix for root, dirs, files in os.walk(search_path): for f in files: if f == "%s.py" % modulename: return os.path.join(root, f) def get_module_text(self, modulename, search_path=''): filename = self.find_module(modulename, search_path) module_open = open(filename, 'rb') module_text = module_open.read() module_open.close() return module_text I changed the name of print_module_text because it doesn't actually print anything. -- Steven From robin at reportlab.com Wed Jul 22 09:37:35 2009 From: robin at reportlab.com (Robin Becker) Date: Wed, 22 Jul 2009 14:37:35 +0100 Subject: Finding all cycles within an undirected graph In-Reply-To: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> References: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> Message-ID: <4A67161F.8090908@chamonix.reportlab.co.uk> disappearedng wrote: > Hey everyone, > I am interested to find out all the cycles within an undirected graph. > I don't have a particular implementation of nodes and vertices yet, so > would someone kindly point out a good graph library for this ( I > looked at pygraph and python-graph, but not really satisfied ), in > addition to good algorithm for solving this issue. this one has been updated recently http://code.google.com/p/python-graph/ also this page has many links http://wiki.python.org/moin/PythonGraphApi -- Robin Becker From jjkk73 at gmail.com Wed Jul 22 09:39:09 2009 From: jjkk73 at gmail.com (jorma kala) Date: Wed, 22 Jul 2009 15:39:09 +0200 Subject: How to document Python code properly for Pydoc Message-ID: Hi, Do you know where I can find the rules for documenting Python code, so that automatic document generation with Pydoc makes the most of the comments inserted in the code? I know about documenting class and method through triple quote just under the class definition. But how do you comment a specific field or variable, or how do you document function arguments so that they are extracted like in javadoc? Thanks very much -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhodri at wildebst.demon.co.uk Wed Jul 22 10:01:09 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 22 Jul 2009 15:01:09 +0100 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina wrote: > class X(object): > foo = descriptor() > > x = X() > x.foo = "value" Isn't this going to create a brand new instance attribute x.foo that has nothing to do with the descriptor anyway? -- Rhodri James *-* Wildebeest Herder to the Masses From ryniek90 at gmail.com Wed Jul 22 10:01:19 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 22 Jul 2009 16:01:19 +0200 Subject: Changing the private variables content In-Reply-To: References: Message-ID: <4A671BAF.1060204@gmail.com> > > Got it: > > exec('self.' + attr + '=\'' + val + '\'') > > That worked. I think it'll do what you want now ;) > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ To bad, that didn't worked in my class. Still the same error: " >>> mod.print_module('socket') Traceback (most recent call last): File "", line 1, in mod.print_module('socket') File "", line 51, in print_module module_open = open(self._this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " :-/ > What is the point of the _SetVar method? > > Instead of: > > self._SetVar(self._this_module, os.path.join(root, f)) > > just do: > > self._this_module = os.path.join(root, f) > > (unless I'm misunderstanding what you're trying to do!) > Of course i;ve tried, but still get the same error: " >>> mod.print_module('socket') Traceback (most recent call last): File "", line 1, in mod.print_module('socket') File "", line 51, in print_module module_open = open(self.this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " It looks like private variable have specific naure, that prevent from traditional editing them. Still searching for some tuts about private methods and variables. From Scott.Daniels at Acm.Org Wed Jul 22 10:06:45 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 22 Jul 2009 07:06:45 -0700 Subject: Multiple versions of python In-Reply-To: <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> Message-ID: CCW wrote: > On 21 July, 15:19, Dave Angel wrote: >> The other thing you may want to do in a batch file is to change the file >> associations so that you can run the .py file directly, without typing >> "python" or "pythonw" in front of it. >> >> The relevant Windows commands are: assoc and ftype And on a >> related note, you may want to edit the PATHEXT environment variable, to >> add .PY and .PYW > > Thanks for this - this way made a bit more sense to me. I've now got > C:\commands with the 4 .bat files in, and C:\commands in my path. It > all seems to work :) I think I've missed the point of the @ though - > it doesn't seem to make any difference.. > > I'm also a bit confused with the associations - since I've got python > 2.6 and 3.1, surely the command I type (python26 or python31) is the > only way to force a script to be run using a specific interpreter at > runtime without having to change the .bat file every time I want to > run a script using 3.1 instead of 2.6? OK, for me currently: C:\> assoc .py .py=Python.File C:\> assoc .pyw .pyw=Python.NoConFile C:\> ftype Python.File Python.File="C:\Python31\python.exe" "%1" %* C:\> ftype Python.NoConFile Python.NoConFile="C:\Python31\pythonw.exe" "%1" %* C:\> ftype Python.File Python.File="C:\Python31\python.exe" "%1" %* Now imagine instead that you've added: C:\> ftype Python31.File="C:\Python31\python.exe" "%1" %* C:\> ftype Python31.NoConFile="C:\Python31\pythonw.exe" "%1" %* C:\> ftype Python26.File="C:\Python26\python.exe" "%1" %* C:\> ftype Python26.NoConFile="C:\Python26\pythonw.exe" "%1" %* Then you can do the following: C:\> assoc .py=Python26.File C:\> fumble.py C:\> assoc .py=Python31.File C:\> fumble.py That is the basic idea, but at the moment, I don't see a simple demo working for me. SO, if you want to pursue this, you can probably get it to work. --Scott David Daniels Scott.Daniels at Acm.Org From Scott.Daniels at Acm.Org Wed Jul 22 10:12:18 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 22 Jul 2009 07:12:18 -0700 Subject: Issue combining gzip and subprocess In-Reply-To: References: Message-ID: <2YednUGQxvntgfrXnZ2dnUVZ_hWdnZ2d@pdx.net> Piet van Oostrum wrote: > ... > f = gzip.open(filename, 'w') > proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) > while True: > line = proc.stdout.readline() > if not line: break > f.write(line) > f.close() Or even: proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) with gzip.open(filename, 'w') as dest: for line in iter(proc.stdout, ''): f.write(line) --Scott David Daniels Scott.Daniels at Acm.Org From invalid at invalid Wed Jul 22 10:13:28 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 22 Jul 2009 09:13:28 -0500 Subject: plotting graph from another file References: Message-ID: <096dnaQneZwVg_rXnZ2dnUVZ_hZi4p2d@posted.visi> On 2009-07-22, jhinak sen wrote: > i want to plot a distribution graph for following data: [...] > can it be done using matplotlib ? > > if yes , please give me the code for plotting the importing > the text file and plotting the graph. As soon as you're done cleaning my house, washing my car, and running a few errands. -- Grant Edwards grante Yow! Of course, you at UNDERSTAND about the PLAIDS visi.com in the SPIN CYCLE -- From disappearedng at gmail.com Wed Jul 22 10:16:31 2009 From: disappearedng at gmail.com (disappearedng) Date: Wed, 22 Jul 2009 22:16:31 +0800 Subject: Finding all cycles within an undirected graph (disappearedng) Message-ID: <15dd54ec0907220716g34a806e1w5c2a93176172db20@mail.gmail.com> Actually, I have realized that using the bit vector XOR method by http://www.me.utexas.edu/~bard/IP/Handouts/cycles.pdf gives some possibility of doing so. However this method is still experimental and doesn't really have a definitive end to the algorithm (the base number of vectors can change). Any ideas anyone? On Wed, Jul 22, 2009 at 9:12 PM, wrote: > 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. Finding all cycles within an undirected graph (disappearedng) > ? 2. Re: Re: Changing the private variables content (Xavier Ho) > ? 3. Python-URL! - weekly Python news and links (Jul 22) > ? ? ?(Gabriel Genellina) > ? 4. Re: Multiple versions of python (Dave Angel) > ? 5. Re: Changing the private variables content (MRAB) > > > ---------- Forwarded message ---------- > From:?disappearedng > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 21:04:18 +0800 > Subject:?Finding all cycles within an undirected graph > Hey everyone, > I am interested to find out all the cycles within an undirected graph. > I don't have a particular implementation of nodes and vertices yet, so > would someone kindly point out a good graph library for this ( I > looked at pygraph and python-graph, but not really satisfied ), in > addition to good algorithm for solving this issue. > > > > ---------- Forwarded message ---------- > From:?Xavier Ho > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 23:06:22 +1000 > Subject:?Re: Re: Changing the private variables content > Got it: > > exec('self.' + attr + '=\'' + val + '\'') > > That worked. I think it'll do what you want now ;) > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > > ---------- Forwarded message ---------- > From:?"Gabriel Genellina" > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 12:48:25 +0000 (UTC) > Subject:?Python-URL! - weekly Python news and links (Jul 22) > QOTW: ?"If programming is symbol manipulation, then you should remember that > the user interface is also symbol manipulation, and it is a MUCH harder > problem than databases, sorting, searching, and all the other problems you > learn about in academia. The user interface has to communicate over a rich > but noisy channel using multiple under-specified protocols to a couple of > pounds of meat which processes information using buggy heuristics evolved > over millions of years to find the ripe fruit, avoid being eaten, and have > sex. If you think getting XML was hard, that's *nothing* compared to user > interfaces. > > The fact that even bad UIs work at all is a credit to the heuristics, bugs > and all, in the meat." - Steven D'Aprano > ? ? http://groups.google.com/group/comp.lang.python/msg/8c65eacbd76e79cf > > > ? ?Invoke the same method over every object in a collection: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/8fd293a9b39c8733/ > > ? ?There is no 'xor' boolean operator, why? What should be its outcome? > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/16eec722310e75e8/ > > ? ?A thread-safe method to create unique identifiers (not necesarily > ? ?increasing integers): > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/dfe22a6446c057df/ > > ? ?Tim Chase tells us three ways to turn standard output into unbuffered mode: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/a65f71444bd4bb53/ > > ? ?How to receive data of unknown length using sockets: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/9e4a9f8b6e83320/ > > ? ?Override a method, but keeping the inherited docstring: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/1e4075ba10dcbdd9/ > > ? ?Limiting the execution time of a code fragment: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/734054de170e2904/ > > ? ?Sharing a big object between processes using the multiprocessing module: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/833d4988b7af6353/ > > ? ?Why aren't OrderedDicts [3.1] comparable using <, >, <=, >=? > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/c3c6f4fe7b6d487d/ > > ? ?So many ways to call a function - why is that? Beginners get confused: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/3fd57f5ee4850530/ > > ? ?The reasons to choose your favorite programming language (Python, I > ? ?presume!): > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/5d87008e328efcf9/ > > ? ?Tuples, lists, their differences and motivation: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/cb0cf56c52321ccc/ > > > ======================================================================== > Everything Python-related you want is probably one or two clicks away in > these pages: > > ? ?Python.org's Python Language Website is the traditional > ? ?center of Pythonia > ? ? ? ?http://www.python.org > ? ?Notice especially the master FAQ > ? ? ? ?http://www.python.org/doc/FAQ.html > > ? ?PythonWare complements the digest you're reading with the > ? ?marvelous daily python url > ? ? ? ? http://www.pythonware.com/daily > > ? ?Just beginning with Python? ?This page is a great place to start: > ? ? ? ?http://wiki.python.org/moin/BeginnersGuide/Programmers > > ? ?The Python Papers aims to publish "the efforts of Python enthusiasts": > ? ? ? ?http://pythonpapers.org/ > ? ?The Python Magazine is a technical monthly devoted to Python: > ? ? ? ?http://pythonmagazine.com > > ? ?Readers have recommended the "Planet" sites: > ? ? ? ?http://planetpython.org > ? ? ? ?http://planet.python.org > > ? ?comp.lang.python.announce announces new Python software. ?Be > ? ?sure to scan this newsgroup weekly. > ? ? ? ?http://groups.google.com/group/comp.lang.python.announce/topics > > ? ?Python411 indexes "podcasts ... to help people learn Python ..." > ? ?Updates appear more-than-weekly: > ? ? ? ?http://www.awaretek.com/python/index.html > > ? ?The Python Package Index catalogues packages. > ? ? ? ?http://www.python.org/pypi/ > > ? ?Much of Python's real work takes place on Special-Interest Group > ? ?mailing lists > ? ? ? ?http://www.python.org/sigs/ > > ? ?Python Success Stories--from air-traffic control to on-line > ? ?match-making--can inspire you or decision-makers to whom you're > ? ?subject with a vision of what the language makes practical. > ? ? ? ?http://www.pythonology.com/success > > ? ?The Python Software Foundation (PSF) has replaced the Python > ? ?Consortium as an independent nexus of activity. ?It has official > ? ?responsibility for Python's development and maintenance. > ? ? ? ?http://www.python.org/psf/ > ? ?Among the ways you can support PSF is with a donation. > ? ? ? ?http://www.python.org/psf/donations/ > > ? ?The Summary of Python Tracker Issues is an automatically generated > ? ?report summarizing new bugs, closed ones, and patch submissions. > ? ? ? ?http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date > > ? ?Although unmaintained since 2002, the Cetus collection of Python > ? ?hyperlinks retains a few gems. > ? ? ? ?http://www.cetus-links.org/oo_python.html > > ? ?Python FAQTS > ? ? ? ?http://python.faqts.com/ > > ? ?The Cookbook is a collaborative effort to capture useful and > ? ?interesting recipes. > ? ? ? ?http://code.activestate.com/recipes/langs/python/ > > ? ?Many Python conferences around the world are in preparation. > ? ?Watch this space for links to them. > > ? ?Among several Python-oriented RSS/RDF feeds available, see: > ? ? ? ?http://www.python.org/channews.rdf > ? ?For more, see: > ? ? ? ?http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all > ? ?The old Python "To-Do List" now lives principally in a > ? ?SourceForge reincarnation. > ? ? ? ?http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse > ? ? ? ?http://www.python.org/dev/peps/pep-0042/ > > ? ?del.icio.us presents an intriguing approach to reference commentary. > ? ?It already aggregates quite a bit of Python intelligence. > ? ? ? ?http://del.icio.us/tag/python > > ? ?Enjoy the *Python Magazine*. > ? ? ? ?http://pymag.phparch.com/ > > ? ?*Py: the Journal of the Python Language* > ? ? ? ?http://www.pyzine.com > > ? ?Dr.Dobb's Portal is another source of Python news and articles: > ? ? ? ?http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python > ? ?and Python articles regularly appear at IBM DeveloperWorks: > ? ? ? ?http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 > > Previous - (U)se the (R)esource, (L)uke! - messages are listed here: > ?http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date > ?http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& > ?http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes > > There is *not* an RSS for "Python-URL!"--at least not yet. ?Arguments > for and against are occasionally entertained. > > > Suggestions/corrections for next week's posting are always welcome. > E-mail to should get through. > > To receive a new issue of this posting in e-mail each Monday morning > (approximately), ask to subscribe. ?Mention > "Python-URL!". ?Write to the same address to unsubscribe. > > > -- The Python-URL! Team-- > > Phaseit, Inc. (http://phaseit.net) is pleased to participate in and > sponsor the "Python-URL!" project. ?Watch this space for upcoming > news about posting archives. > > > > ---------- Forwarded message ---------- > From:?Dave Angel > To:?CCW > Date:?Wed, 22 Jul 2009 09:11:26 -0400 > Subject:?Re: Multiple versions of python > CCW wrote: >> >> On 21 July, 15:19, Dave Angel wrote: >> >>> >>> ChrisW wrote: >>> >>>> >>>> Hi, >>>> ? ? ?I have installed 2 versions of python on my Windows XP computer - I >>>> originally had 3.0.1, but then found that the MySQL module only >>>> supported 2.*, so I've now installed that. ?I have found that if I >>>> change the Windows Environment Variable path, then I can change the >>>> version of python called when I type 'python' into a command line. >>>> However, I'd like to be able to choose which version I use. ?I know >>>> that if I change C:\Python26\python.exe to >>>> C:\Python26\python2.exe and C:\Python30\python.exe to C: >>>> \Python26\python3.exe, then typing 'python2' or 'python3' will invoke >>>> the correct interpreter. ?However, is it safe just to rename the >>>> executable files? Is there a more elegant way to achieve the same >>>> task? >>>> ? ? ?Thanks, >>>> Chris >>>> >>> >>> The elegant way is to have a batch directory on your PATH ( I use ?m:\t\bat ) ?and put your *.bat files there. ? I do NOT put any python >>> installations on the PATH. >>> >>> For example, I have a batch file called: ? ? m:\t\bat\python26.bat >>> >>> c:\progfiles\python26\python.exe %* >>> >>> The %* syntax means pass all arguments through to the program. >>> >>> Once it all works, you can add an "@" in front of the c: ? in order to >>> suppress echoing the command. ?At that point, it'll look and work the >>> same way as what you did, but without modifying anything in the install >>> directory. ? ?(You may want ?python26.bat, pythonw26.bat, python31.bat >>> and pythonw31.bat) >>> >>> The other thing you may want to do in a batch file is to change the file >>> associations so that you can run the .py file directly, without typing >>> "python" or "pythonw" in front of it. >>> >>> The relevant Windows commands are: ? ? assoc and ftype ? ? ?And on a >>> related note, you may want to edit the PATHEXT environment variable, to >>> add .PY and .PYW >>> >> >> Thanks for this - this way made a bit more sense to me. ?I've now got >> C:\commands with the 4 .bat files in, and C:\commands in my path. ?It >> all seems to work :) I think I've missed the point of the @ though - >> it doesn't seem to make any difference.. >> >> I'm also a bit confused with the associations - since I've got python >> 2.6 and 3.1, surely the command I type (python26 or python31) is the >> only way to force a script to be run using a specific interpreter at >> runtime without having to change the .bat file every time I want to >> run a script using 3.1 instead of 2.6? >> >> > > The @ symbol was used in older versions of CMD and COMMAND to suppress echoing of the command line. ?I think if you're using XP or later, it doesn't matter. > > As for file associations, I don't know just what you already know about. ?When you type ? ?data.doc ? ?at a command line, the system looks for the data file, then if it's found, it looks up the program associated with that extension. ?On my machine, that's a particular version of Word for Windows. ?Similarly, if I type ? ?digest.py ? and I'm in the directory containing that file, the system looks up my associations, and runs Python 2.6 on that file. ?If I wanted it to run Python 3.1 on that file, I'd have to change the associations, temporarily. > > So, first approximation, it's a way to avoid having to type PYTHON each time I want to run a script, as long as I don't need any other arguments on the (implied) command line. ?Further, the system will then look for digest.py everywhere on the PATH, so I don't even have to be in the same directory. ?And that means I can be in the data directory that digest.py is going to work on. ?Effectively, it raises digest.py to feeling like an executable, for most purposes. ?In fact, I put all my scripts in a directory, along with other simple executables. ? I use ?m:\t\bin ?for that purpose. > > These associations (to .py and .pyw) are established by the installer program for Python. ?But when you had multiple installs, you could choose whether the second one installed overrides the first. ?My point is that if you need to change them back and forth, you could use ?assoc and ftype to do it. > > On my system: > > M:\LabOrders>assoc .py > .py=Python.File > > M:\LabOrders>ftype Python.File > Python.File="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* > > M:\LabOrders> > > > So my .py files are associated with the 2.6.2 version of ActivePython installation. ? Note the similarity of the ftype to your python26.bat file? > > One more little trick is that I could just type ? digest ? ?instead of ? ?digest.py, ? if I have added the .py and .pyw extensions to my ? PATHEXT environment variable. ? ?So my PATHEXT looks like: > ?PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY;.PYW > > > > Note that the association changes made by ?assoc and ftype are global. ?They apply to all cmd.exe windows, even those already running, and they survive a reboot. ?But when you set environment variables like PATHEXT, ?you can choose to do it in a single window (with SET), or globally (using control-panel). > > If I found myself switching often, I'd make separate ftype entries for each interpreter, something like > ?Python.File26="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* > ?Python.File31="C:\PROGFI~1\..... > and just change assoc .py ?to point at the one I needed today. > > As it is, I'm content leaving the associations pointing at 2.6, and explicitly entering PYTHON31 in front of any script name I want to run on that interpreter. > > DaveA > > > > > ---------- Forwarded message ---------- > From:?MRAB > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 14:12:18 +0100 > Subject:?Re: Changing the private variables content > Ryniek90 wrote: >>> >>> ------------------------------------------------------------------------ >>> >>> Temat: >>> Re: Changing the private variables content >>> Od: >>> Gary Herron >>> Data: >>> Tue, 21 Jul 2009 14:14:44 -0700 >>> Do: >>> Ryniek90 >>> >>> Do: >>> Ryniek90 >>> Kopia: >>> python-list at python.org >>> >>> >>> Ryniek90 wrote: >>>> >>>> Hi. >>>> I'm writing some class, and decided to use inside private method and some private variables. While with method i haven't got any problem's with variables i have. >>>> Maybe some example. >>>> A class with private method and private variable: >>>> >>>> " >>>> >>> class Secret(object): >>>> ? # >>>> ? def __init__(self): >>>> ? ? ? self._number = 1 >>>> ? # >>>> ? def _secret(self): >>>> ? ? ? print self._number >>>> ? def showit(self): >>>> ? ? ? print "Secret number is:\n" >>>> ? ? ? self._secret() >>>> >>>> >>> sec = Secret() >>>> >>> sec.showit() >>>> Secret number is: >>>> >>>> 1 >>>> >>> sec._number >>>> 1 >>>> >>> sec._number = 2 >>>> >>> sec._number >>>> 2 >>>> >>> sec._number += 3 >>>> >>> sec._number >>>> 5 >>>> >>> >>>> " >>>> >>>> As You can see, i made class with private method and private variable inside __init__ constructor. I've changed also the variable value, but outside class. >>>> I've got problem with changing some variable value _inside__my_ class, which i'm writing. >>> >>> Not sure this is what you are asking, but a method (which is how I interpret "_inside__my_ class") changes the value by normal assignment like this: >>> >>> class Secret(object): >>> ?... >>> ?def SetNumber(self,value): >>> ? self._number = value >>> >>> Is that what you were asking? >>> >>> >>> Gary Herron >>> >>> >>> >>>> I've searched over google, some tuts with topics about operations on private variables, but didn't found anything - only how to make them, but no how to assign new objects/override them with new content (and other advanced and helpful options). >>>> >>>> If someone could help me, it would be great. >>>> >>>> Thanks. >>> >>> >> >> >> Thanks for hint, but looks like i can't do what i want. >> >> Maybe i show You my class: >> >> " >> >> class ModPrint(object): >> ? ? u""" >> ? This will be the doc. >> ? """ >> ? ? def __init__(self): >> ? ? ? ? ? ? #Assign the Python installation directory - sys.exec_prefix - to variable >> ? ? ? self._default_search_path=sys.exec_prefix >> ? ? ? self._chosen_module = '' >> ? ? ? self._user_search_path = '' >> ? ? ? self._this_module = '' >> ? ? ? ? def _SetVar(self, attr, val): >> ? ? ? self.attr = val ? ? ? ? ? ?def _search_for_module(self, *args): >> ? ? ? ? ? ? """Private method which walks through default Python installation directories, and search for prefered module.""" >> ? ? ? ? ? ? #walking thru all directories available in path '_user_search_path' >> ? ? ? for root, dirs, files in os.walk(self._user_search_path): >> ? ? ? ? ? for f in files: >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? #if found file 'f' is THAT module, >> ? ? ? ? ? ? ? #full path to THAT file is assigned to variable >> ? ? ? ? ? ? ? if f == ("%s.py" % self._chosen_module): >> ? ? ? ? ? ? ? ? ? self._SetVar(self._this_module, os.path.join(root, f)) >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? def print_module(self, _chosen_module='', _user_search_path='', _default_search_path=sys.exec_prefix,): >> ? ? ? ? ? ? """Reads module chosen by user, and returns full content of this module, as it is.""" >> ? ? ? ? ? ? ? ? ? #if custom search path hasn't been assigned, >> ? ? ? #default path is being assigned as custom path >> ? ? ? if self._user_search_path == '': >> ? ? ? ? ? self._user_search_path = self._default_search_path >> ? ? ? ? ? ? ? ? #using private method '_search_for_module' with 'self.' preffix >> ? ? ? #to search for prefered module >> ? ? ? self._search_for_module(_chosen_module, _user_search_path) >> ? ? ? ? ? ? #opening prefered module with read-only binary mode >> ? ? ? #and assigning it to 'module_open' variable >> ? ? ? module_open = open(self._this_module, 'rb') >> ? ? ? ? ? ? #reading this file and assigning it to variable >> ? ? ? module_text = module_open.read() >> ? ? ? ? ? ? #closing read file; the read content is still available >> ? ? ? #it's stored in variable 'module_text' >> ? ? ? module_open.close() >> ? ? ? ? ? ? #returning the read content >> ? ? ? return module_text >> >> " >> >> When i use this class in Python IDLE, i've got this error: >> " >> ?>>> mod = ModPrint() >> ?>>> import socket >> ?>>> mod.print_module('socket') >> >> Traceback (most recent call last): >> ?File "", line 1, in >> ? mod.print_module('socket') >> ?File "", line 48, in print_module >> ? module_open = open(self._this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> ?>>> >> " >> >> As You can see, i can't assign the new value "os.path.join(root, f)" to the 'self._this_module variable'. >> So for sure i've made some mistake in method: >> >> " >> def _SetVar(self, attr, val): >> ? ? ? self.attr = val ? " >> When i've changed private variables to normal, stored in class (in __init__ method), it was the same situation - i couldn't change this variable value. >> >> " >> ?>>> mod = ModPrint() >> ?>>> mod.print_module('os') >> >> Traceback (most recent call last): >> ?File "", line 1, in >> ? mod.print_module('os') >> ?File "", line 48, in print_module >> ? module_open = open(self.this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> ?>>> >> " >> >> Thanks i someone could help me, give some hint. > > What is the point of the _SetVar method? > > Instead of: > > ? ?self._SetVar(self._this_module, os.path.join(root, f)) > > just do: > > ? ?self._this_module = os.path.join(root, f) > > (unless I'm misunderstanding what you're trying to do!) > > > -- > http://mail.python.org/mailman/listinfo/python-list > From inky788 at gmail.com Wed Jul 22 10:36:51 2009 From: inky788 at gmail.com (Inky 788) Date: Wed, 22 Jul 2009 07:36:51 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> On Jul 22, 2:36?am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > On Jul 20, 12:27?pm, Phillip B Oldham > > > wrote: > > > [snip] We > > > understand that lists are mutable and tuples are not, but we're a > > > little lost as to why the two were kept separate from the start. They > > > both perform a very similar job as far as we can tell. > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. ? Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm just not sophisticated enough, but I've never wanted to use a list/ tuple as a dict key. This sounds like obscure usage, and a bit contrived as a reason for having *both* lists and tuples. From davea at ieee.org Wed Jul 22 10:38:14 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 10:38:14 -0400 Subject: Ideas for problem with chat server application! In-Reply-To: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> References: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> Message-ID: <4A672456.9010604@ieee.org> David Adamo Jr. wrote: > I developed a chat application with an attendant chat server. > Everything is working fine. The issue now is the fact that whenever > the chat server goes down (for instance, the server system shuts down > as a result of power failure or some other problem), by the time the > server system come back on, the chat server would have to be restarted > manually. > > I believe (and I know) it is more appropriate for the chat server > application to restart itself when the computer comes back on (and of > course regardless of who is logged in and of course, even before > anyone logs in). I have a batch file that executes the chat server. My > attempt was to create a windows service that start automatically and > runs this batch file using a Network Service account on the server > system. Although, I'm having a hard time with this (temporarily), I > would love to ask if there are any alternatives to using a windows > service. Suggestions are highly appreciated. > > As a starting place, take a look at the Windows Scheduler. One of the options is to schedule something at "boot." That happens before anybody logs on to the system. I don't know whether you can specify what user account will be used, but you can certainly launch a python program from there. DaveA From inky788 at gmail.com Wed Jul 22 10:46:06 2009 From: inky788 at gmail.com (Inky 788) Date: Wed, 22 Jul 2009 07:46:06 -0700 (PDT) Subject: Documentation Problems References: Message-ID: On Jul 22, 7:15?am, Tim Golden wrote: > Mark du Preez wrote: > > Hi > > > Can anyone tell me where to go to suggest changes to the Python > > documentation? > > Drop an entry in the tracker: > > ?http://bugs.python.org > > Patches are always welcome, Could you please provide brief instructions on exactly how to go about creating a suitable patch file? I mean, starting from the beginning (as in, "check out code from {here} using {this command}", "cd to {this} directory", "edit the file in place", "run {this} command to create a patch file"). Or perhaps this procedure is already outlined elsewhere? From f.orndorffplunkett at gmail.com Wed Jul 22 10:46:09 2009 From: f.orndorffplunkett at gmail.com (F.O.P.) Date: Wed, 22 Jul 2009 07:46:09 -0700 (PDT) Subject: Not understanding this documentation Message-ID: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> http://bugs.python.org/issue5358 I simply installed 3.0 on my ubuntubox brought my project from a windows machine and made necessary changes to what I had used from os and os.path. I have an error at the end of this line: for k in range(I): and the error: SyntaxError: invalid character in identifier I've already declared I way above as the lenth of an array (I = len (IA)) There shouldn't be a problem there aren't any issues with other declarations identical to this. From george.sakkis at gmail.com Wed Jul 22 10:55:38 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Wed, 22 Jul 2009 07:55:38 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> Message-ID: <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> On Jul 22, 7:38?am, William Dode wrote: > I updated the script (python, c and java) with your unrolled version > + somes litle thinks. > > I also tried with python3.1, unladen Q2, ironpython1.1.1 > > Unfortunately it doesn't work more with shedskin, i'll see on the > shedskin group... > > c 1.85s > gcj 2.15s > java 2.8s > python2.5 + psyco 3.1s > unladen-2009Q2 145s (2m45) > python2.5 254s (4m14s) > python3.1 300s (5m) > ironpython1.1.1 680s (11m20) Cool; it would be interesting to see the numbers for Jython and Boo as well if it's not too much effort. George From kyrie at uh.cu Wed Jul 22 11:01:20 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Wed, 22 Jul 2009 11:01:20 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: <1248274880.4a6729c036a68@comuh.uh.cu> Quoting Inky 788 : > > The good reason is the immutability, which lets you use > > a tuple as a dict key. ? > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. I don't seem to understand your definition of obscure and contrived. It seems that you got late to this thread, and you missed the examples. I'd suggest you to go back on this thread and look at them. heights = {} heights[1,2] = 5 heights[1,3] = 7 heights[3,5] = 1 addresses[lastname, firstname] = my_home_address census[location, age] = census.get((location, age), 0) + 1 All those are using tuples as dict keys. Regards, -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From rhodri at wildebst.demon.co.uk Wed Jul 22 11:07:53 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 22 Jul 2009 16:07:53 +0100 Subject: Not understanding this documentation In-Reply-To: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: On Wed, 22 Jul 2009 15:46:09 +0100, F.O.P. wrote: > http://bugs.python.org/issue5358 > I simply installed 3.0 on my ubuntubox brought my project from a > windows machine and made necessary changes to what I had used from os > and os.path. I have an error at the end of this line: > for k in range(I): > and the error: > SyntaxError: invalid character in identifier > > I've already declared I way above as the lenth of an array (I = len > (IA)) > There shouldn't be a problem there aren't any issues with other > declarations identical to this. Could you show us slightly more of your code and the rest of the error message, please? I have a suspicion that the error actually lies in the "necessary changes" you made. -- Rhodri James *-* Wildebeest Herder to the Masses From phillip.oldham at gmail.com Wed Jul 22 11:09:31 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Wed, 22 Jul 2009 08:09:31 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> Message-ID: <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> On Jul 22, 2:23?pm, Casey Webster wrote: > I can't answer your question, but I would like to better understand > the > problem you are trying to solve. ?The Apache Hadoop/MapReduce java > application isn't really that "large" by modern standards, although it > is generally run with large heap sizes for performance (-Xmx1024m or > larger for the mapred.child.java.opts parameter). > > MapReduce is designed to do extremely fast parallel data set > processing > on terabytes of data over hundreds of physical nodes; what advantage > would a pure Python approach have here? We're always taught that it is a good idea to reduce the number of dependencies for a project. So you could use Disco, or Dumbo, or even Skynet. But then you've introduced another system you have to manage. Each new system will have a learning curve, which is lessened if the system is written in an environment/language you already work with/ understand. And since a large cost with environments like erlang and java is in understanding them any issues that are out of the ordinary can be killer; changing the heap size as you mentioned above for Java could be one of these issues that a non-java dev trying to use Hadoop might come across. With the advent of cloud computing and the new semi-structured/ document databases that are coming to the fore sometimes you need to use MapReduce on smaller/fewer systems to the same effect. Also, you may need only to ensure that a job is done in a timely fashion without taking up too many resources, rather than lightening-fast. Dumbo/disco in these situations may be considered overkill. Implementations like BashReduce are perfect for such scenarios. I'm simply wondering if there's another simpler/smaller implementation of MapReduce that plays nicely with Python but doesn't require the setup/ knowledge overhead of more "robust" implementations such as hadoop and disco... maybe similar to Ruby's Skynet. From davea at ieee.org Wed Jul 22 11:10:35 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 11:10:35 -0400 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <4A672BEB.6010008@ieee.org> superpollo wrote: >
hello clp. > > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? > > bye > >
> There's no way to get a binary value directly into a literal, but you can convert one into a string value, as follows: x = 0b00101101 print x print chr(x) output is: 45 - Note that this is decimal 45, which is different than the hex 45 you used in your first example. For doing multiple characters, I can't see anything better than: lis = [0b00101101, 0b01000101, 0b01100001] print lis s= "".join(chr(x) for x in lis) print s DaveA From hackingkk at gmail.com Wed Jul 22 11:35:19 2009 From: hackingkk at gmail.com (Krishnakant) Date: Wed, 22 Jul 2009 21:05:19 +0530 Subject: challenging problem for changing to a dedicated non-privileged user within a script. Message-ID: <1248276919.8402.6.camel@krishna-laptop> hello all, This is a real challenge and I don't know if a solution even exists for this or not. I am writing an application which I run as my usual user on ubuntu. the usernake is let's say kk and it has sudo permission (meaning the user is in the sudoers list ). now when i do python myscript.py, the script has to change to another non-privileged user for some tasks. let's say for example switch to the postgres user which is dedicated for postgres and has no other privileges. I have tryed doing os.setuid(112) where 112 could be the uid of the user I want the script to swith over. but I got opperation not permitted. I tryed using subprocess but that did not help me either. I tryed sudo su into the Popen command but it throws me into the terminal (shell) with postgres as the user. But that's now my desired result. what I exactly want is that the script now continues to execute under postgres user till the end. I don't know how to achieve this iffect. Infact I will need this during a serious deployment because i would have my application run as a demon as a dedicated user. I am finding some code for deamonising a python application but don't know how to tell the script to change user. happy hacking. Krishnakant. From davea at ieee.org Wed Jul 22 11:39:51 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 11:39:51 -0400 Subject: Changing the private variables content In-Reply-To: <4A671BAF.1060204@gmail.com> References: <4A671BAF.1060204@gmail.com> Message-ID: <4A6732C7.7070909@ieee.org> Ryniek90 wrote: > > > > It looks like private variable have specific naure, that prevent from > traditional editing them. > Still searching for some tuts about private methods and variables. > > There's no tutorial on private variables for good reason: Private variables have no special characteristics. The compiler and runtime library don't treat them specially (except for the highly controversial from xx import *). It's strictly a programming convention. You've got some other bugs in your code, which I can't help you with because your code comes across formatted so strangely that it cannot compile here. Some things you might do to make it easier to get help: 1) make sure you're not mixing tabs and spaces in your source code 2) post using text messages, so they don't get reformatted. 3) copy/paste the code and error messages into your message, don't retype. 4) whenever you've made more than trivial changes to the code, repost it all, since it's very difficult to tell which suggestions you've followed, and which ones you haven't gotten yet, or maybe dismissed because they didn't work. DaveA From python at bdurham.com Wed Jul 22 11:42:07 2009 From: python at bdurham.com (python at bdurham.com) Date: Wed, 22 Jul 2009 11:42:07 -0400 Subject: Suggestions for Python MapReduce? In-Reply-To: References: Message-ID: <1248277327.15700.1326268767@webmail.messagingengine.com> Phillip, We've had great success writing simple, project specific algorithms to split content into chunks appropriate for ETL type, Python based processing in a hosted cloud environment like Amazon EC2 or the recently launched Rackspace Cloud Servers. Since we're purchasing our cloud hosting time in 1 hour blocks, we divide our data into much larger chunks than what a traditional map-reduce technique might use. For many of our projects, the data transfer time to and from the cloud takes the majority of clock time. Malcolm From jeanmichel at sequans.com Wed Jul 22 12:07:07 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 22 Jul 2009 18:07:07 +0200 Subject: Changing the private variables content In-Reply-To: <02770500$0$5185$c3e8da3@news.astraweb.com> References: <02770500$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A67392B.3090602@sequans.com> Steven D'Aprano wrote: > On Wed, 22 Jul 2009 14:29:20 +0200, Ryniek90 wrote: > > >> When i use this class in Python IDLE, i've got this error: " >> > ... > >> Traceback (most recent call last): >> File "", line 1, in >> mod.print_module('socket') >> File "", line 48, in print_module >> module_open = open(self._this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> >>> >> " >> >> As You can see, i can't assign the new value "os.path.join(root, f)" to >> the 'self._this_module variable'. >> > > In the __init__ method, you successfully set the `_this_module` attribute: > > self._this_module = '' > > That works, so it proves that you *can* set a private attribute. > > Later on, you call the `_SetVar` method, which does this: > > def _SetVar(self, attr, val): > self.attr = val > > In English, this method does this: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute named exactly "attr". > > What you actually want is: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute with the name stored > in the variable "attr". > > Can you see the difference? > > What you actually want is: > > def _SetVar(self, attr, val): > setattr(self, attr, val) > > > Your code is terribly complicated. This will probably do what you want. > (Warning -- I haven't tested it, so there may be a few errors.) > > > class ModPrint(object): > u""" > This will be the doc. > """ > def __init__(self, default_search_path=''): > self.default_search_path = '' > > def find_module(self, modulename, search_path=''): > if search_path == '': > # Try the default search path instead. > search_path = self.default_search_path > if search_path == '': # still blank. > # Try the Python search path instead. > search_path = sys.exec_prefix > for root, dirs, files in os.walk(search_path): > for f in files: > if f == "%s.py" % modulename: > return os.path.join(root, f) > > def get_module_text(self, modulename, search_path=''): > filename = self.find_module(modulename, search_path) > module_open = open(filename, 'rb') > module_text = module_open.read() > module_open.close() > return module_text > > > I changed the name of print_module_text because it doesn't actually print > anything. > > > > Moreover, if I'm not wrong there is absolutely no difference in the way python is handling private and public attributes, for python, both are the same kind of attributes. This is purely conventional (some doc processing tool will take into account the private prefix). So there is absolutely no restriction in writing self._myVar = 0 in any of the instance methods. JM From ryniek90 at gmail.com Wed Jul 22 12:10:52 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 22 Jul 2009 18:10:52 +0200 Subject: Python-list Digest, Vol 70, Issue 282 In-Reply-To: References: Message-ID: <4A673A0C.2000807@gmail.com> > >> When i use this class in Python IDLE, i've got this error: " >> > ... > >> Traceback (most recent call last): >> File "", line 1, in >> mod.print_module('socket') >> File "", line 48, in print_module >> module_open = open(self._this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> >>> >> " >> >> As You can see, i can't assign the new value "os.path.join(root, f)" to >> the 'self._this_module variable'. >> > > In the __init__ method, you successfully set the `_this_module` attribute: > > self._this_module = '' > > That works, so it proves that you *can* set a private attribute. > > Later on, you call the `_SetVar` method, which does this: > > def _SetVar(self, attr, val): > self.attr = val > > In English, this method does this: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute named exactly "attr". > > What you actually want is: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute with the name stored > in the variable "attr". > > Can you see the difference? > > What you actually want is: > > def _SetVar(self, attr, val): > setattr(self, attr, val) > > > Your code is terribly complicated. This will probably do what you want. > (Warning -- I haven't tested it, so there may be a few errors.) > > > class ModPrint(object): > u""" > This will be the doc. > """ > def __init__(self, default_search_path=''): > self.default_search_path = '' > > def find_module(self, modulename, search_path=''): > if search_path == '': > # Try the default search path instead. > search_path = self.default_search_path > if search_path == '': # still blank. > # Try the Python search path instead. > search_path = sys.exec_prefix > for root, dirs, files in os.walk(search_path): > for f in files: > if f == "%s.py" % modulename: > return os.path.join(root, f) > > def get_module_text(self, modulename, search_path=''): > filename = self.find_module(modulename, search_path) > module_open = open(filename, 'rb') > module_text = module_open.read() > module_open.close() > return module_text > > > I changed the name of print_module_text because it doesn't actually print > anything. > Thanks for ready class - it works great, but that code: " What you actually want is: def _SetVar(self, attr, val): setattr(self, attr, val) " Didn't worked, so i wil reuse your class, and modify it later. Now i see that working on private objects complex. From junhufr at gmail.com Wed Jul 22 12:13:37 2009 From: junhufr at gmail.com (Jun) Date: Wed, 22 Jul 2009 09:13:37 -0700 (PDT) Subject: Copy/paste through LAN Message-ID: Hello, I've a client/server application which uses Pyro to communicate each other. Now i try to implement copy/paste through LAN between server controlled filesystem to my local windows machine (I could list files in client's window). How can i pass the url information through Pyro ? Thank you in advance. Jun From tjreedy at udel.edu Wed Jul 22 12:28:04 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:28:04 -0400 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > hello clp. > > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? Language Ref 2.4. Literals From rNOSPAMon at flownet.com Wed Jul 22 12:36:28 2009 From: rNOSPAMon at flownet.com (Ron Garret) Date: Wed, 22 Jul 2009 09:36:28 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: In article , Neil Hodgson wrote: > milanj: > > > and all of them use native threads (python still use green threads ?) > > Python uses native threads. But then it adds the global interpreter lock, which completely undermines the utility of native threads. So yes, it uses native threads, but it does not actually realize the benefits of that use. rg From sjmachin at lexicon.net Wed Jul 22 12:37:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 22 Jul 2009 09:37:16 -0700 (PDT) Subject: Not understanding this documentation References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: On Jul 23, 12:46?am, "F.O.P." wrote: > http://bugs.python.org/issue5358 > I simply installed 3.0 on my ubuntubox brought my project from a > windows machine and made necessary changes to what I had used from os > and os.path. ?I have an error at the end of this line: > for k in range(I): > and the error: > SyntaxError: invalid character in identifier > > I've already declared I way above as the lenth of an array (I = len > (IA)) > There shouldn't be a problem there aren't any issues with other > declarations identical to this. You are have used the --ftniv and --strict options and hence it's objecting to the lower-case "k". From tjreedy at udel.edu Wed Jul 22 12:38:22 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:38:22 -0400 Subject: Documentation Problems In-Reply-To: <7coan1F28i1q2U1@mid.uni-berlin.de> References: <7coan1F28i1q2U1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > Mark du Preez wrote: > >> Hi >> >> Can anyone tell me where to go to suggest changes to the Python >> documentation? >> >> Thanks. > > http://docs.python.org/ has a "bugs"-section that leads to > > http://docs.python.org/bugs.html > > You can file doc-bugs in the tracker, and check if they are already fixed in > the dev-docs. Actually, check first at http://docs.python.org/dev/py3k/ for 3.2a http://docs.python.org/dev/ for 2.7a These are updated often, perhaps nightly. Then file if you still think a change is needed. For small changes, a specific suggested rewording is sufficient. "Change 'tihs' to 'this'." "Between 'Sentence 1' and 'sentence 2', I suggest adding 'sentence 3'." The doc maintainers are happy to do the rst formatting themselves. From tjreedy at udel.edu Wed Jul 22 12:44:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:44:08 -0400 Subject: doted filenames in import statements In-Reply-To: <4A670313.8040204@sequans.com> References: <4A670313.8040204@sequans.com> Message-ID: Jean-Michel Pichavant wrote: > Piet van Oostrum wrote: >> >> [snip] >> >>> JP> file = "/home/dsp/4.6.0.0/test.py" >>> JP> test = __import__(file) >>> JP> => no module name blalalal found. >>> >> >> >>> JP> Any suggestion ? I tried multiple escape technics without any >>> success. >>> >> >> Rightly so. >> >> I think the best would be to add the directory to sys.path >> sys.path.add('/home/dsp/4.6.0.0') >> and then >> __import__('test', ... ) > > I see. My problem is that a have to import 2 different files having de > same name. In the same name space I have 2 objects from 2 different > software branches, let's say 4.6.0 and 4.6.1. > The first object shall import 4.6.0/orb.py and the second one 4.6.1/orb.py. > > If I add 4.6.1 to sys.path, the import statement will look like: > self._orb = __import__('orb') > The problem is, python wil assume orb is already imported and will > assign the module from the 4.6.0 branch to my 4.6.1 object. > > Do I have to mess up with sys.modules keys to make python import the > correct file ? Is there a standard/proper way to do that ? If you make the directory names into proper identifiers like v460 and v461 and add __init__.py to each to make them packages and have both on search path, then import v460.orb #or import v460.orb as orb460 import v461.orb #or import v460.orb as orb461 will get you both. One way or another, they have to get different names within Python. Terry Jan Reedy From wilk at flibuste.net Wed Jul 22 12:45:19 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 16:45:19 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> Message-ID: <4a67421e$0$31038$426a74cc@news.free.fr> On 22-07-2009, George Sakkis wrote: > On Jul 22, 7:38?am, William Dode wrote: > >> I updated the script (python, c and java) with your unrolled version >> + somes litle thinks. >> >> I also tried with python3.1, unladen Q2, ironpython1.1.1 >> >> Unfortunately it doesn't work more with shedskin, i'll see on the >> shedskin group... >> >> c 1.85s >> gcj 2.15s >> java 2.8s >> python2.5 + psyco 3.1s >> unladen-2009Q2 145s (2m45) >> python2.5 254s (4m14s) >> python3.1 300s (5m) >> ironpython1.1.1 680s (11m20) > > Cool; it would be interesting to see the numbers for Jython and Boo as > well if it's not too much effort. I just tried with jython, but oddly it's faster without array. Thanks to Mark, i could compile to shedskin again. And add somes little improvements... c 1.65s gcj 1.9s java 2.4s python2.5 + psyco 2.9s shedskin 3.4s unladen-2009Q2 125s (2m05) Jython 2.2.1 on java1.6.0_12 176s (without array, like shedskin) Jython 2.2.1 on java1.6.0_12 334s (with array) python2.5 215s (3m35s) python3.1 246s (4m06s) ironpython1.1.1 512 (8m32s) -- William Dod? - http://flibuste.net Informaticien Ind?pendant From f.orndorffplunkett at gmail.com Wed Jul 22 12:45:58 2009 From: f.orndorffplunkett at gmail.com (F.O.P.) Date: Wed, 22 Jul 2009 09:45:58 -0700 (PDT) Subject: Not understanding this documentation References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: <081d1a50-fd7f-4f87-889e-262bc7e2debd@x5g2000prf.googlegroups.com> On Jul 22, 9:07?am, "Rhodri James" wrote: > On Wed, 22 Jul 2009 15:46:09 +0100, F.O.P. ? > wrote: > > >http://bugs.python.org/issue5358 > > I simply installed 3.0 on my ubuntubox brought my project from a > > windows machine and made necessary changes to what I had used from os > > and os.path. ?I have an error at the end of this line: > > for k in range(I): > > and the error: > > SyntaxError: invalid character in identifier > > > I've already declared I way above as the lenth of an array (I = len > > (IA)) > > There shouldn't be a problem there aren't any issues with other > > declarations identical to this. > > Could you show us slightly more of your code and the rest of the > error message, please? ?I have a suspicion that the error actually > lies in the "necessary changes" you made. > > -- > Rhodri James *-* Wildebeest Herder to the Masses I spoke to a friend about it and he found that I had put in a unicode "semicolon" rather than ASCII. I feel silly for asking a question like this but there's really no harm done in asking. Thank you for the quick response Rhodri! Much appreciated! Cheers From tjreedy at udel.edu Wed Jul 22 12:47:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:47:15 -0400 Subject: Finding all cycles within an undirected graph In-Reply-To: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> References: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> Message-ID: disappearedng wrote: > Hey everyone, > I am interested to find out all the cycles within an undirected graph. I think you have to qualify that as 'minimal cycles' to have a well-defined problem. From http Wed Jul 22 12:55:51 2009 From: http (Paul Rubin) Date: 22 Jul 2009 09:55:51 -0700 Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> Message-ID: <7xws60u06g.fsf@ruckus.brouhaha.com> Phillip B Oldham writes: > Implementations like BashReduce mapreduce-bash-script> are perfect for such scenarios. I'm simply > wondering if there's another simpler/smaller implementation of > MapReduce that plays nicely with Python but doesn't require the setup/ > knowledge overhead of more "robust" implementations such as hadoop and > disco... maybe similar to Ruby's Skynet. I usually just spew ssh tasks across whatever computing nodes I can get my hands on. It's less organized than something like mapreduce, but I tend to run one-off tasks that I have to keep an eye on anyway. I've done stuff like that across up to 100 or so machines and I think it wouldn't be really worse if the number were a few times higher. I don't think it would scale to really large (10,000's of nodes) clusters. From tjreedy at udel.edu Wed Jul 22 12:59:01 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:59:01 -0400 Subject: Not understanding this documentation In-Reply-To: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: F.O.P. wrote: Your subject line should have been about "SyntaxError message" > http://bugs.python.org/issue5358 Why reference this? Unless you added zero-width joiners in the move, this is irrelevant noise. > I simply installed 3.0 on my ubuntubox Upgrade to 3.1 as soon as you can. Works better. > brought my project from a > windows machine and made necessary changes to what I had used from os > and os.path. I have an error at the end of this line: > for k in range(I): > and the error: > SyntaxError: invalid character in identifier > > I've already declared I way above as the lenth of an array (I = len > (IA)) > There shouldn't be a problem there aren't any issues with other > declarations identical to this. I otherwise concur with R. James as there is no syntax error posted. To make sure there is nothing invisible in that line, do print(repr("for k in range(I):")) # should be no escaped chars and print(len("for k in range(I):")) # should be 18, I believe tjr From pavlovevidence at gmail.com Wed Jul 22 13:01:31 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 10:01:31 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> On Jul 22, 9:36?am, Ron Garret wrote: > In article , > ?Neil Hodgson wrote: > > > milanj: > > > > and all of them use native threads (python still use green threads ?) > > > ? ?Python uses native threads. > > But then it adds the global interpreter lock, which completely > undermines the utility of native threads. ?So yes, it uses native > threads, but it does not actually realize the benefits of that use. Wrong. It only partially undermines the utility of native threads, not completely. Native threading allows some threads to run while others are blocked in a system call (as well as in a few other minor cases), which can't be done with green threads. Carl Banks From lie.1296 at gmail.com Wed Jul 22 13:19:02 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 23 Jul 2009 03:19:02 +1000 Subject: Documentation Problems In-Reply-To: References: Message-ID: Inky 788 wrote: > On Jul 22, 7:15 am, Tim Golden wrote: >> Mark du Preez wrote: >>> Hi >>> Can anyone tell me where to go to suggest changes to the Python >>> documentation? >> Drop an entry in the tracker: >> >> http://bugs.python.org >> >> Patches are always welcome, > > Could you please provide brief instructions on exactly how to go about > creating a suitable patch file? I mean, starting from the beginning > (as in, "check out code from {here} using {this command}", "cd to > {this} directory", "edit the file in place", "run {this} command to > create a patch file"). Or perhaps this procedure is already outlined > elsewhere? It's the typical svn-based development, "svn checkout" from the repository (see list of svn urls here: http://www.python.org/dev/faq/#how-do-i-get-a-checkout-of-the-repository-read-only-and-read-write), modify the files, then "svn diff" (or "diff -u") And last but not least, check out the dev-faq: http://www.python.org/dev/faq/ From http Wed Jul 22 13:20:20 2009 From: http (Paul Rubin) Date: 22 Jul 2009 10:20:20 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> Message-ID: <7xskgobpnv.fsf@ruckus.brouhaha.com> Carl Banks writes: > Wrong. It only partially undermines the utility of native threads, > not completely. Native threading allows some threads to run while > others are blocked in a system call (as well as in a few other minor > cases), which can't be done with green threads. Why is that such an advantage? Green threads work fine if you just organize the i/o system to never block. From king.seth at gmail.com Wed Jul 22 13:32:51 2009 From: king.seth at gmail.com (Seth) Date: Wed, 22 Jul 2009 10:32:51 -0700 (PDT) Subject: Pyserial and pyQt References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> Message-ID: <70e4ebae-908f-4f3a-a96a-6fa0a7a7887a@y19g2000yqy.googlegroups.com> On Jul 21, 7:24?pm, David Boddie wrote: > On Tuesday 21 July 2009 21:37, Seth wrote: > > > I have used pyserial in the past but this is my first experience with > > pyQt. ?I am using the Python xy package for windows current but might > > move to linux. ?I have a small device that is outputting a basic text > > string. ?I want to be able to read this string(from the comm port) and > > update a text box and eventually a graph in pyQt. ?I can't find any > > documentation or tutorials on how to do this. ?If anyone can point me > > in the right direction or give me some tips I would be grateful. > > It seems that someone has already asked a similar question on Stack > Overflow, though perhaps you should start with a simpler solution > and look at more advanced ones later: > > ?http://stackoverflow.com/questions/771988/pyqt4-and-pyserial > > One starting point is this list of tutorials on the PyQt and PyKDE Wiki: > > ?http://www.diotavelli.net/PyQtWiki/Tutorials > > Later, when you want to draw graphs, you might find PyQwt useful: > > ?http://pyqwt.sourceforge.net/ > > You may already be aware that there's also a mailing list for PyQt and > PyKDE: > > ?http://www.riverbankcomputing.com/pipermail/pyqt/ > > Another way to get answers to questions is to join the #pyqt IRC channel > at freenode.net: > > ? irc://irc.freenode.net/ > > David Thanks for the response. I have gone through a lot of the tutorials. All of them(that I saw) seem to just deal will event-based applications ie calculator, image viewer, etc. How do I run pyserial in the background and pass the information to PyQT and refresh the screen? Is there a way to have pyserial run in another thread and pass the information to the UI? Thanks, Seth From lie.1296 at gmail.com Wed Jul 22 13:43:14 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 23 Jul 2009 03:43:14 +1000 Subject: How to document Python code properly for Pydoc In-Reply-To: References: Message-ID: jorma kala wrote: > Hi, > Do you know where I can find the rules for documenting Python code, so > that automatic document generation with Pydoc makes the most of the > comments inserted in the code? > I know about documenting class and method through triple quote just > under the class definition. But how do you comment a specific field or > variable, or how do you document function arguments so that they are > extracted like in javadoc? > Thanks very much pydoc is a simple tool, and doesn't do much. You write in freeform, although generally you'll do something like this: def myfunc(a, b): ''' short description of myfunc longer description of myfunc, if necessary, and typically includes description of the arguments and the behaviors. Also includes the description of the return value. ''' pass pydoc doesn't recognize any special markups. If you want to get more from the docstring, you need other documentation generator such as epydoc, Doxygen, or Sphinx. From phillip.oldham at gmail.com Wed Jul 22 13:51:17 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Wed, 22 Jul 2009 10:51:17 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> <7xws60u06g.fsf@ruckus.brouhaha.com> Message-ID: <83889f6f-1e40-435e-b947-af17964e90af@d32g2000yqh.googlegroups.com> On Jul 22, 5:55?pm, Paul Rubin wrote: > Phillip B Oldham writes: > I usually just spew ssh tasks across whatever computing nodes I can > get my hands on. ?It's less organized than something like mapreduce, > but I tend to run one-off tasks that I have to keep an eye on anyway. I'm thinking of repeated tasks; things you run regularly, but don't need to be blisteringly fast. I'll more than likely use Disco, but if I were to find something more light-weight I'd take a serious look. From wilk at flibuste.net Wed Jul 22 13:55:34 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 17:55:34 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> <4a67421e$0$31038$426a74cc@news.free.fr> Message-ID: <4a675296$0$1549$426a74cc@news.free.fr> On 22-07-2009, William Dode wrote: > c 1.65s > gcj 1.9s > java 2.4s > python2.5 + psyco 2.9s > shedskin 3.4s with -bw i have 2.6s > unladen-2009Q2 125s (2m05) > Jython 2.2.1 on java1.6.0_12 176s (without array, like shedskin) > Jython 2.2.1 on java1.6.0_12 334s (with array) > python2.5 215s (3m35s) > python3.1 246s (4m06s) > ironpython1.1.1 512 (8m32s) somebody can test with ironpython on windows ? Anyway, it's very impressive. I wonder if unladen will be so close in the futur. -- William Dod? - http://flibuste.net Informaticien Ind?pendant From pavlovevidence at gmail.com Wed Jul 22 13:59:50 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 10:59:50 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> Message-ID: <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> On Jul 22, 10:20?am, Paul Rubin wrote: > Carl Banks writes: > > Wrong. ?It only partially undermines the utility of native threads, > > not completely. ?Native threading allows some threads to run while > > others are blocked in a system call (as well as in a few other minor > > cases), which can't be done with green threads. > > Why is that such an advantage? ?Green threads work fine if you just > organize the i/o system to never block. ? Because then I don't have to organize the I/O system never to block. Carl Banks From davidj411 at gmail.com Wed Jul 22 14:04:36 2009 From: davidj411 at gmail.com (davidj411) Date: Wed, 22 Jul 2009 11:04:36 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> <16e1d810-bdf0-46b8-aef1-7e57eeb30547@k19g2000yqn.googlegroups.com> Message-ID: <524a57a9-ba45-459b-b62d-81f527888a4e@j19g2000vbp.googlegroups.com> i never heard of the logging module, but this function seemed simple enough. i assume this link is what you refering to: http://docs.python.org/library/logging.html thanks for the helpful info. i think "Piet van Oostrum" has resolved my issue. good eyes! From dangets at gmail.com Wed Jul 22 14:15:55 2009 From: dangets at gmail.com (DG) Date: Wed, 22 Jul 2009 11:15:55 -0700 (PDT) Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output Message-ID: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> There is probably a better way to do this (please enlighten me, if you know), but what I want to do is get a list of a class' attributes minus whatever the 'builtin' methods are. I would also like to do this for instances of classes as well. I don't want to use __dict__ because I want to get all of the attributes that have been added throughout the inheritance tree, just not the builtin ones. I was wondering if there is a function to return a list of these, so I could perform a 'dir' on the object (class or instance) and filter out the builtin attributes. I know I could create my own list something like ['__class__', '__delattr__', ..., '__weakref__'] and use that. I would see this being fragile to Python version differences as new ones are added or taken away. A more flexible way is to have the list be filled out dynamically by creating a 'junk' class that inherits only from object and storing it's dir() output into the filter list. This is probably what I'll do unless someone knows of a builtin function that will give me a (version-safe) list. Thanks! From dangets at gmail.com Wed Jul 22 14:24:21 2009 From: dangets at gmail.com (DG) Date: Wed, 22 Jul 2009 11:24:21 -0700 (PDT) Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output References: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> Message-ID: On Jul 22, 12:15?pm, DG wrote: > There is probably a better way to do this (please enlighten me, if you > know), but what I want to do is get a list of a class' attributes > minus whatever the 'builtin' methods are. ?I would also like to do > this for instances of classes as well. ?I don't want to use __dict__ > because I want to get all of the attributes that have been added > throughout the inheritance tree, just not the builtin ones. > > I was wondering if there is a function to return a list of these, so I > could perform a 'dir' on the object (class or instance) and filter out > the builtin attributes. ?I know I could create my own list something > like > ['__class__', '__delattr__', ..., '__weakref__'] and use that. ?I > would see this being fragile to Python version differences as new ones > are added or taken away. > > A more flexible way is to have the list be filled out dynamically by > creating a 'junk' class that inherits only from object and storing > it's dir() output into the filter list. ?This is probably what I'll do > unless someone knows of a builtin function that will give me a > (version-safe) list. > > Thanks! Haha, replying to my own post. Here is the implementation of my idea of dynamically creating the filter lists from a 'junk' object. As of now it appears that the dir output of a class is the same as the dir output of an instance, but if that ever changes, this should future proof it. This is only a 4-liner, so it is probably good enough for what I want to do, but I would rather use a builtin/standard way if there is one. class A(object): pass class_filter_methods = dir(A) instance_filter_methods = dir(A()) From dangets at gmail.com Wed Jul 22 14:28:58 2009 From: dangets at gmail.com (DG) Date: Wed, 22 Jul 2009 11:28:58 -0700 (PDT) Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output References: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> Message-ID: <48e4e3e1-7356-451e-845b-46516ead98b7@v37g2000prg.googlegroups.com> On Jul 22, 12:24?pm, DG wrote: > On Jul 22, 12:15?pm, DG wrote: > > > > > There is probably a better way to do this (please enlighten me, if you > > know), but what I want to do is get a list of a class' attributes > > minus whatever the 'builtin' methods are. ?I would also like to do > > this for instances of classes as well. ?I don't want to use __dict__ > > because I want to get all of the attributes that have been added > > throughout the inheritance tree, just not the builtin ones. > > > I was wondering if there is a function to return a list of these, so I > > could perform a 'dir' on the object (class or instance) and filter out > > the builtin attributes. ?I know I could create my own list something > > like > > ['__class__', '__delattr__', ..., '__weakref__'] and use that. ?I > > would see this being fragile to Python version differences as new ones > > are added or taken away. > > > A more flexible way is to have the list be filled out dynamically by > > creating a 'junk' class that inherits only from object and storing > > it's dir() output into the filter list. ?This is probably what I'll do > > unless someone knows of a builtin function that will give me a > > (version-safe) list. > > > Thanks! > > Haha, replying to my own post. ?Here is the implementation of my idea > of dynamically creating the filter lists from a 'junk' object. ?As of > now it appears that the dir output of a class is the same as the dir > output of an instance, but if that ever changes, this should future > proof it. ?This is only a 4-liner, so it is probably good enough for > what I want to do, but I would rather use a builtin/standard way if > there is one. > > class A(object): > ? ? pass > class_filter_methods = dir(A) > instance_filter_methods = dir(A()) Wow, I'm a quick one. Sharp as a marble. 2-liner: class_filter_methods = dir(object) instance_filter_methods = dir(object()) From invalid at invalid Wed Jul 22 14:49:58 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 22 Jul 2009 13:49:58 -0500 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: On 2009-07-22, Ron Garret wrote: > In article , > Neil Hodgson wrote: > >> milanj: >> >> > and all of them use native threads (python still use green threads ?) >> >> Python uses native threads. > > But then it adds the global interpreter lock, which completely > undermines the utility of native threads. So yes, it uses native > threads, but it does not actually realize the benefits of that use. Not all of the time. For library/extension calls that release the GIL, it does. -- Grant Edwards grante Yow! I was making donuts at and now I'm on a bus! visi.com From http Wed Jul 22 15:04:07 2009 From: http (Paul Rubin) Date: 22 Jul 2009 12:04:07 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> Message-ID: <7xfxco35g8.fsf@ruckus.brouhaha.com> Carl Banks writes: > > Why is that such an advantage? ?Green threads work fine if you just > > organize the i/o system to never block. ? > > Because then I don't have to organize the I/O system never to block. We're talking about what a language implementation does behind the scenes, I thought. From ethan at stoneleaf.us Wed Jul 22 15:21:04 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 22 Jul 2009 12:21:04 -0700 Subject: Einstein summation notation In-Reply-To: <7xprbybnso.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> <7xprbybnso.fsf@ruckus.brouhaha.com> Message-ID: <4A6766A0.4000809@stoneleaf.us> Paul Rubin wrote: > Ethan Furman writes: > >>Or if any(p for p in [header, body, footer, whatever, ...]) > > > No need for the genexp: > > if any([header, body, footer, whatever, ...]) > > But, you are using the built-in bool cast either way. Right you are -- and if any([header, body, footer, whatever, ...]) sure looks nicer to me than your original code of if any(p != '' for p in [header, body, footer, otherthing1, ...]) But there a are a lot of decisions and factors to writing these things one way or the other. I'm just glad Python supports the Something vs. Nothing model. ~Ethan~ From tjreedy at udel.edu Wed Jul 22 15:24:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 15:24:19 -0400 Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output In-Reply-To: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> References: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> Message-ID: DG wrote: > There is probably a better way to do this (please enlighten me, if you > know), but what I want to do is get a list of a class' attributes > minus whatever the 'builtin' methods are. I would also like to do > this for instances of classes as well. I don't want to use __dict__ > because I want to get all of the attributes that have been added > throughout the inheritance tree, just not the builtin ones. > > I was wondering if there is a function to return a list of these, so I > could perform a 'dir' on the object (class or instance) and filter out > the builtin attributes. I know I could create my own list something > like > ['__class__', '__delattr__', ..., '__weakref__'] and use that. I > would see this being fragile to Python version differences as new ones > are added or taken away. > > A more flexible way is to have the list be filled out dynamically by > creating a 'junk' class that inherits only from object and storing > it's dir() output into the filter list. This is probably what I'll do > unless someone knows of a builtin function that will give me a > (version-safe) list. > > Thanks! 3.1 >>> list(filter(lambda s: s[0:2]!='__', dir(object))) [] Now substitute your object for lambda. This assumes of course that you are doing no name-mangling __xxx names. tjr From pavlovevidence at gmail.com Wed Jul 22 15:35:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 12:35:52 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> Message-ID: On Jul 22, 12:04?pm, Paul Rubin wrote: > Carl Banks writes: > > > Why is that such an advantage? ?Green threads work fine if you just > > > organize the i/o system to never block. ? > > > Because then I don't have to organize the I/O system never to block. > > We're talking about what a language implementation does behind the > scenes, I thought. No we're not, we are talking about the whether GIL completely or only partially undermines the use of native threads on Python. I don't think your fantasy async-only all-green-thread langauge implementation is possible anyway. How would you wait on a pipe in one thread, a socket in another, a semaphore in a third? (Are there any popular OSes that offer a unified polling interface to all possible synchronizations?) And what do you do about drivers or libraries that make underlying blocking calls? What if you have a busy calculation going on in the background? Carl Banks From robert.kern at gmail.com Wed Jul 22 15:45:41 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 22 Jul 2009 14:45:41 -0500 Subject: clean way prepend an element to a numpy array In-Reply-To: <7cnr5tF28183hU1@mid.individual.net> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <7cnr5tF28183hU1@mid.individual.net> Message-ID: On 2009-07-22 01:51, greg wrote: > bdb112 wrote: > >> I saw this interest syntax on this site >> x[:0]=0 >> >> I guess that is cute, but could be confusing....(and doesn't work) > > It does if you use an array of the appropriate > type on the right hand side: > > a[:0] = array('i', [0]) Not when 'a' is a numpy array rather than an array.array. -- 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 torriem at gmail.com Wed Jul 22 16:26:04 2009 From: torriem at gmail.com (Michael Torrie) Date: Wed, 22 Jul 2009 14:26:04 -0600 Subject: GTK+ ques In-Reply-To: <1e61b3260907220415g6edb24sb7fe07c83fe346c8@mail.gmail.com> References: <1e61b3260907220415g6edb24sb7fe07c83fe346c8@mail.gmail.com> Message-ID: <4A6775DC.1050003@gmail.com> nipun batra wrote: > I need to develop a GUI preferably in GTK using c or PYGTK.I have little > idea about Python. > Following are requiremnts of the application: > should be GUI > Sould be real time > Should interface serial port > should display moving maps,like google earth > It is basically for Ground control station of a UAV. > How would Python+Pygtk combo work? > is python slower than c/c++ ,if then by how much? > any suggestions for the applcation? All of these things can be done in Python. Using nice math libraries like numpy, math-specific things can be very fast. Whether python is fast enough only you could say. PyGTK is nice, but doesn't have built-in primitives for doing super high level things like moving maps. But there are libraries out there that can do this, one of which is call libchamplain. A blog post about it is here: http://blog.pierlux.com/2009/01/10/libchamplain-and-libchamplain-gtk-028-released/en/ libchamplain does have python bindings I think. From george.sakkis at gmail.com Wed Jul 22 16:27:13 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Wed, 22 Jul 2009 13:27:13 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> <4a67421e$0$31038$426a74cc@news.free.fr> Message-ID: <36aeca4d-0609-4da2-83a4-376c88869667@p23g2000vbl.googlegroups.com> On Jul 22, 12:45?pm, William Dode wrote: > On 22-07-2009, George Sakkis wrote: > > > > > On Jul 22, 7:38?am, William Dode wrote: > > >> I updated the script (python, c and java) with your unrolled version > >> + somes litle thinks. > > >> I also tried with python3.1, unladen Q2, ironpython1.1.1 > > >> Unfortunately it doesn't work more with shedskin, i'll see on the > >> shedskin group... > > >> c 1.85s > >> gcj 2.15s > >> java 2.8s > >> python2.5 + psyco 3.1s > >> unladen-2009Q2 145s (2m45) > >> python2.5 254s (4m14s) > >> python3.1 300s (5m) > >> ironpython1.1.1 680s (11m20) > > > Cool; it would be interesting to see the numbers for Jython and Boo as > > well if it's not too much effort. > > I just tried with jython, but oddly it's faster without array. FYI Jython 2.5 was released last month, you may want to try this instead of 2.2. George From scriptlearner at gmail.com Wed Jul 22 16:51:18 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Wed, 22 Jul 2009 13:51:18 -0700 (PDT) Subject: logging outgoing HTTP POST message and incoming response message Message-ID: I am sending a HTTP POST by using the following codes: opener = urllib2.build_opener(proxyHandler, MultipartPostHandler) params = { "audio" : "http://sample.com/my.wav", "data" : open(file, "rb") } print opener.open(myURL, params).read() How do I log or print out the actual POST message (including headers) that is sent out? How do I log or print out the response headers as well? Thanks From exarkun at divmod.com Wed Jul 22 16:53:17 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Wed, 22 Jul 2009 16:53:17 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: Message-ID: <20090722205317.2543.659910647.divmod.quotient.4286@henry.divmod.com> On Wed, 22 Jul 2009 12:35:52 -0700 (PDT), Carl Banks wrote: >On Jul 22, 12:04?pm, Paul Rubin wrote: >> Carl Banks writes: >> > > Why is that such an advantage? ?Green threads work fine if you just >> > > organize the i/o system to never block. >> >> > Because then I don't have to organize the I/O system never to block. >> >> We're talking about what a language implementation does behind the >> scenes, I thought. > >No we're not, we are talking about the whether GIL completely or only >partially undermines the use of native threads on Python. > >I don't think your fantasy async-only all-green-thread langauge >implementation is possible anyway. How would you wait on a pipe in >one thread, a socket in another, a semaphore in a third? (Are there >any popular OSes that offer a unified polling interface to all >possible synchronizations?) Every OS I can think of can support the three examples you gave here. >And what do you do about drivers or >libraries that make underlying blocking calls? Certainly a point to consider. >What if you have a busy calculation going on in the background? What if you do? Are you suggesting this would somehow prevent I/O from being serviced? I'm not sure why, as long as the implementation pays attention to I/O events. Jean-Paul From deets at nospam.web.de Wed Jul 22 16:54:02 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 22 Jul 2009 22:54:02 +0200 Subject: logging outgoing HTTP POST message and incoming response message In-Reply-To: References: Message-ID: <7cpcjaF2915u5U1@mid.uni-berlin.de> scriptlearner at gmail.com schrieb: > I am sending a HTTP POST by using the following codes: > > opener = urllib2.build_opener(proxyHandler, MultipartPostHandler) > params = { "audio" : "http://sample.com/my.wav", > "data" : open(file, "rb") } > print opener.open(myURL, params).read() > > How do I log or print out the actual POST message (including headers) > that is sent out? > How do I log or print out the response headers as well? You can use proxy-tools such as tcpmon or sniff traffic using wireshark. Diez From scriptlearner at gmail.com Wed Jul 22 17:18:37 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Wed, 22 Jul 2009 14:18:37 -0700 (PDT) Subject: logging outgoing HTTP POST message and incoming response message References: <7cpcjaF2915u5U1@mid.uni-berlin.de> Message-ID: <0b403bd0-759f-49b6-9eac-3d585d877fe3@k13g2000prh.googlegroups.com> On Jul 22, 1:54?pm, "Diez B. Roggisch" wrote: > You can use proxy-tools such as tcpmon or sniff traffic using wireshark. > > Diez Thanks, but I am trying to enable some debug mode to log all outgoing and incoming messages for certain period of time, and running another proxy-tool is not very ideal. It would be great to log it in some log file. From piet at cs.uu.nl Wed Jul 22 17:24:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 23:24:34 +0200 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> <16e1d810-bdf0-46b8-aef1-7e57eeb30547@k19g2000yqn.googlegroups.com> <524a57a9-ba45-459b-b62d-81f527888a4e@j19g2000vbp.googlegroups.com> Message-ID: >>>>> davidj411 (d) wrote: >d> i never heard of the logging module, but this function seemed simple >d> enough. >d> i assume this link is what you refering to: >d> http://docs.python.org/library/logging.html >d> thanks for the helpful info. i think "Piet van Oostrum" has resolved >d> my issue. good eyes! Without glasses I wouldn't have noticed it :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 22 17:50:03 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 23:50:03 +0200 Subject: Issue combining gzip and subprocess References: <2YednUGQxvntgfrXnZ2dnUVZ_hWdnZ2d@pdx.net> Message-ID: >>>>> Scott David Daniels (SDD) schreef: >SDD> Piet van Oostrum wrote: >>> ... >>> f = gzip.open(filename, 'w') >>> proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) >>> while True: >>> line = proc.stdout.readline() >>> if not line: break >>> f.write(line) >>> f.close() >SDD> Or even: >SDD> proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) >SDD> with gzip.open(filename, 'w') as dest: >SDD> for line in iter(proc.stdout, ''): >SDD> f.write(line) If it would work. 1) with gzip... is not supported in Python < 3.1 2) for line in iter(proc.stdout), i.e. no second argument. 3) dest <==> f should be the same identifier. Lesson: if you post code either: - test it and copy verbatim from your test, or - write a disclaimer -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 22 18:17:12 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 00:17:12 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. References: Message-ID: >>>>> Krishnakant (K) wrote: >K> hello all, >K> This is a real challenge and I don't know if a solution even exists for >K> this or not. >K> I am writing an application which I run as my usual user on ubuntu. >K> the usernake is let's say kk and it has sudo permission (meaning the >K> user is in the sudoers list ). >K> now when i do python myscript.py, the script has to change to another >K> non-privileged user for some tasks. >K> let's say for example switch to the postgres user which is dedicated for >K> postgres and has no other privileges. >K> I have tryed doing os.setuid(112) where 112 could be the uid of the user >K> I want the script to swith over. >K> but I got opperation not permitted. Being a sudoer is not a privilege to issue the os.setuid system call. It is only a permission to use the sudo command. >K> I tryed using subprocess but that did not help me either. I tryed sudo >K> su into the Popen command but it throws me into the terminal (shell) >K> with postgres as the user. You could execute the command: sudo -u postgres required_command with subprocess. You have another problem then: your password must be supplied unless the NOPASSWD flag is set in the sudoers file. >K> But that's now my desired result. >K> what I exactly want is that the script now continues to execute under >K> postgres user till the end. I don't think that's possible if you start as the user kk. >K> I don't know how to achieve this iffect. >K> Infact I will need this during a serious deployment because i would have >K> my application run as a demon as a dedicated user. >K> I am finding some code for deamonising a python application but don't >K> know how to tell the script to change user. >K> happy hacking. >K> Krishnakant. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pavlovevidence at gmail.com Wed Jul 22 18:17:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 15:17:52 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: Message-ID: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> On Jul 22, 1:53?pm, Jean-Paul Calderone wrote: > On Wed, 22 Jul 2009 12:35:52 -0700 (PDT), Carl Banks wrote: > >On Jul 22, 12:04?pm, Paul Rubin wrote: > >> Carl Banks writes: > >> > > Why is that such an advantage? ?Green threads work fine if you just > >> > > organize the i/o system to never block. > > >> > Because then I don't have to organize the I/O system never to block. > > >> We're talking about what a language implementation does behind the > >> scenes, I thought. > > >No we're not, we are talking about the whether GIL completely or only > >partially undermines the use of native threads on Python. > > >I don't think your fantasy async-only all-green-thread langauge > >implementation is possible anyway. ?How would you wait on a pipe in > >one thread, a socket in another, a semaphore in a third? ?(Are there > >any popular OSes that offer a unified polling interface to all > >possible synchronizations?) > > Every OS I can think of can support the three examples you gave here. I guess you would know, but polling on all of these at once has got use more obscure calls than I'm familiar with. On Linux I can use select to wait for pipes and sockets, but not SysV semaphores AFAIK. On Windows it's well known that select only works for sockets. So do all these OSes have some kind of __mega_unifying_poll system call that works for anything that might possibly block, that you can exploit from a user process? > >And what do you do about drivers or > >libraries that make underlying blocking calls? > > Certainly a point to consider. > > >What if you have a busy calculation going on in the background? > > What if you do? ?Are you suggesting this would somehow prevent I/O from > being serviced? ?I'm not sure why, as long as the implementation pays > attention to I/O events. Using native theads with blocking allows one to run a background calculation without burdening it to yield often enough to provide sufficient response times for other operations (which may or may not be convenient to do). Even if it's possible to accomplish arbitrary background processing without native threading (and it is not, because the background calculations could be performed by a library you don't control whose author didn't bother yielding at any point), you cannot reasonably claim native threads have no advantage in this case. Carl Banks From ethan at stoneleaf.us Wed Jul 22 18:35:01 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 22 Jul 2009 15:35:01 -0700 Subject: Changing the private variables content In-Reply-To: <4A671BAF.1060204@gmail.com> References: <4A671BAF.1060204@gmail.com> Message-ID: <4A679415.90407@stoneleaf.us> Ryniek90 wrote: >> >> Got it: >> >> exec('self.' + attr + '=\'' + val + '\'') >> >> That worked. I think it'll do what you want now ;) >> >> Ching-Yun "Xavier" Ho, Technical Artist >> >> Contact Information >> Mobile: (+61) 04 3335 4748 >> Skype ID: SpaXe85 >> Email: contact at xavierho.com >> Website: http://xavierho.com/ > > > To bad, that didn't worked in my class. Still the same error: > " > >>> mod.print_module('socket') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 51, in print_module > module_open = open(self._this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > :-/ > >> What is the point of the _SetVar method? >> >> Instead of: >> >> self._SetVar(self._this_module, os.path.join(root, f)) >> >> just do: >> >> self._this_module = os.path.join(root, f) >> >> (unless I'm misunderstanding what you're trying to do!) >> > > Of course i;ve tried, but still get the same error: > > " > >>> mod.print_module('socket') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 51, in print_module > module_open = open(self.this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > It looks like private variable have specific naure, that prevent from > traditional editing them. > Still searching for some tuts about private methods and variables. No. There is nothing special about variables with a leading underscore. _number is treated by Python *exactly* the same as number . The specific problem in your code above is your _SetVar function, among the more general problem of not yet having a good understanding of classes in Python. Keep studying, Python is an awesome language. I was able to make this work -- hope it helps. 8<----------------------------------------------------------------------- import os import sys class ModPrint(object): u"""This will be the doc.""" def __init__(self): self._default_search_path = sys.exec_prefix def _search_for_module(self, chosen_module, search_path): for root, dirs, files in os.walk(search_path): for f in files: if f == ("%s.py" % chosen_module): return os.path.join(root, f) def print_module(self, chosen_module, user_search_path=''): search_path = user_search_path or self._default_search_path module = self._search_for_module(chosen_module, search_path) if module is not None: module_open = open(module, 'rb') module_text = module_open.read() module_open.close() return module_text return 'Module not found...' 8<----------------------------------------------------------------------- From pinkeen at gmail.com Wed Jul 22 18:43:08 2009 From: pinkeen at gmail.com (Filip) Date: Wed, 22 Jul 2009 15:43:08 -0700 (PDT) Subject: python fast HTML data extraction library Message-ID: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> Hello, Sometime ago I was searching for a library that would simplify mass data scraping/extraction from webpages. Python XPath implementation seemed like the way to go. The problem was that most of the HTML on the net doesn't conform to XML standards, even the XHTML (those advertised as valid XHTML too) pages. I tried to fix that with BeautifulSoup + regexp filtering of some particular cases I encountered. That was slow and after running my data scraper for some time a lot of new problems (exceptions from xpath parser) were showing up. Not to mention that BeautifulSoup stripped almost all of the content from some heavily broken pages (50+KiB page stripped down to some few hundred bytes). Character encoding conversion was a hell too - even UTF-8 pages had some non- standard characters causing issues. Cutting to the chase - that's when I decided to take the matter into my own hands. I hacked together a solution sporting completely new approach overnight. It's called htxpath - a small, lightweight (also without dependencies) python library which lets you to extract specific tag(s) from a HTML document using a path string which has very similar syntax to xpath (but is more convenient in some cases). It did a very good job for me. My library, rather than parsing the whole input into a tree, processes it like a char stream with regular expressions. I decided to share it with everyone so there it is: http://code.google.com/p/htxpath/ I am aware that it is not beautifully coded as my experience with python is rather brief, but I am curious if it will be useful to anyone (also it's my first potentially [real-world ;)] useful project gone public). In that case I promise to continue developing it. It's probably full of bugs, but I can't catch them all by myself. regards, Filip Sobalski From ben+python at benfinney.id.au Wed Jul 22 19:26:37 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 23 Jul 2009 09:26:37 +1000 Subject: Documentation Problems References: Message-ID: <87ocrc7102.fsf@benfinney.id.au> Inky 788 writes: > Could you please provide brief instructions on exactly how to go about > creating a suitable patch file? I mean, starting from the beginning > (as in, "check out code from {here} using {this command}", "cd to > {this} directory", "edit the file in place", "run {this} command to > create a patch file"). Or perhaps this procedure is already outlined > elsewhere? Yes. The documentation is built from source documents under version control, like the rest of Python. So the procedure for creating and submitting patches is the same, and is covered by the Developer's FAQ . -- \ ?If you ever fall off the Sears Tower, just go real limp, | `\ because maybe you'll look like a dummy and people will try to | _o__) catch you because, hey, free dummy.? ?Jack Handey | Ben Finney From piet at cs.uu.nl Wed Jul 22 19:34:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 01:34:34 +0200 Subject: Changing the private variables content References: Message-ID: >>>>> Ryniek90 (R) wrote: >R> It looks like private variable have specific naure, that prevent from >R> traditional editing them. >R> Still searching for some tuts about private methods and variables. Why do you stubbornly keep saying that, while you have been told that it is not the case. Instead you would do better debugging your own case instead of making false claims. Sprinkle a few print statements in your code to see what is happening. Here are a few thing for you to look at: #using private method '_search_for_module' with 'self.' prefix #to search for prefered module self._search_for_module(_chosen_module, _user_search_path) You call it with 2 parameters: _chosen_module, _user_search_path Look at the definition: def _search_for_module(self, *args): It has *args to pick up the arguments, but they are not used in the body. Instead you use self._user_search_path and self._chosen_module. The latter one is always '', never changed to anything else. So it is not surprising that the module is not found. In print_module you have the same kind of confusion. Do you think that magically self._chosen_module gets the value of _chosen_module? Is that you idea of `private variables'? Or do you think that *args does magical things? """Private method which walks through default Python installation directories, and search for prefered module.""" #walking thru all directories available in path _user_search_path' for root, dirs, files in os.walk(self._user_search_path): for f in files: #if found file 'f' is THAT module, #full path to THAT file is assigned to variable print "Trying %s" % f if f == ("%s.py" % self._chosen_module): self._this_module = os.path.join(root, f) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 22 19:36:38 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 01:36:38 +0200 Subject: Python-list Digest, Vol 70, Issue 282 References: Message-ID: >>>>> Ryniek90 (R) wrote: >R> " >R> Didn't worked, so i wil reuse your class, and modify it later. Now i see >R> that working on private objects complex. No it's not complex. *You* make it complex. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From Brian.Mingus at colorado.edu Wed Jul 22 19:51:52 2009 From: Brian.Mingus at colorado.edu (Brian) Date: Wed, 22 Jul 2009 17:51:52 -0600 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a675296$0$1549$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> <4a67421e$0$31038$426a74cc@news.free.fr> <4a675296$0$1549$426a74cc@news.free.fr> Message-ID: <9839a05c0907221651o6afaa80xc25f37ff9bcbeea5@mail.gmail.com> On Wed, Jul 22, 2009 at 11:55 AM, William Dode wrote: > On 22-07-2009, William Dode wrote: > > > c 1.65s > > gcj 1.9s > > java 2.4s > > python2.5 + psyco 2.9s > > shedskin 3.4s > > with -bw i have 2.6s > > > unladen-2009Q2 125s (2m05) > > Jython 2.2.1 on java1.6.0_12 176s (without array, like shedskin) > > Jython 2.2.1 on java1.6.0_12 334s (with array) > > python2.5 215s (3m35s) > > python3.1 246s (4m06s) > > ironpython1.1.1 512 (8m32s) > > somebody can test with ironpython on windows ? > > Anyway, it's very impressive. I wonder if unladen will be so close in > the futur. > > -- > William Dod? - http://flibuste.net > Informaticien Ind?pendant > I had time to run a few tests. Since psyco doesn't work on 64 bit and the developer now works on pypy I used that. Intel Core i7 920 @ 2.66GHz Kubuntu Jaunty Jackal c gcc 4.3.3 0.77s gcj 4.3.3 0.81s java 1.6 0.99s shedskin 1.63s jython 2.2.1 85.37s cpython 2.6.2 93.26s pypy 1.1.0 1612.15s -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Wed Jul 22 19:52:06 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 20:52:06 -0300 Subject: Detect target name in descriptor __set__ method References: <3cb5f960-16f7-4fc6-841f-d0706f0e1d0a@s31g2000yqs.googlegroups.com> Message-ID: En Wed, 22 Jul 2009 05:31:13 -0300, Jon Clements escribi?: > On 22 July, 06:02, "Gabriel Genellina" wrote: >> I have a class attribute 'foo' which is a data descriptor. I create an ? >> instance of such class. When I say instance.foo = value, the descriptor >> ? >> __set__ method is called. Is there any way to obtain the name being ? >> assigned to? ('foo' in this example). That is, I want to know the >> target ? >> name for the assignment that triggered the __set__ method call. >>>> class Test: > def __setattr__(self, what, value): > print what, value > > >>>> t = Test() >>>> t.foo = 'x' > foo x > > Is that what you're after? Yes and no. __setattr__ has a terrible overhead as it is invoked for each and every attribute being set, special or not. Using a custom descriptor seems better, because it allows to intercept just the desired attribute. If only I had access to the name being used... -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Wed Jul 22 20:05:55 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 21:05:55 -0300 Subject: Detect target name in descriptor __set__ method References: Message-ID: En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James escribi?: > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina > wrote: > > >> class X(object): >> foo = descriptor() >> >> x = X() >> x.foo = "value" > > Isn't this going to create a brand new instance attribute x.foo that has > nothing to do with the descriptor anyway? No, it's up to the descriptor __set__ method what happens in this case. Think of the standard 'property' descriptor, the fset function can do whatever it wants. Also, a data descriptor takes precedence over any instance attribute of the same name that might exist. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Wed Jul 22 20:10:54 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 21:10:54 -0300 Subject: invoke method on many instances References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Wed, 22 Jul 2009 10:14:12 -0300, Alan G Isaac escribi?: >>>> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>>>> def apply2(itr, methodname, *args, **kwargs): >>>>> f = operator.methodcaller(methodname, *args, **kwargs) >>>>> for item in itr: >>>>> f(item) > > >>> On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >>>> for obj in objects: >>>> getattr(obj, methodname)(*args, **kwargs) > > >> En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac: >>> Are there any obvious considerations in choosing >>> between those two? > > > On 7/20/2009 3:29 AM Gabriel Genellina apparently wrote: >> The operator.methodcaller version is faster in my tests for large >> collections, but slightly slower when you have very few elements. > > > So it seems. Is this easily explained? NLMPI -- Gabriel Genellina From martin.hellwig at dcuktec.org Wed Jul 22 20:22:14 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 23 Jul 2009 01:22:14 +0100 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: References: Message-ID: Krishnakant wrote: I've seen a method before in a MS cmd script (MakeMeAdmin.cmd) for the purpose of temporarily elevating your rights but remaining the same user. There was a need trick in that the script checks itself on what credentials it runs, if it is not the appropriate one it will call itself again with runas (about the same as sudo) and exits. If it has the right credentials it will perform the actual task. That might be a way to solve your problem. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From http Wed Jul 22 20:27:23 2009 From: http (Paul Rubin) Date: 22 Jul 2009 17:27:23 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> Message-ID: <7xws60p7kk.fsf@ruckus.brouhaha.com> Carl Banks writes: > I don't think your fantasy async-only all-green-thread langauge > implementation is possible anyway. Erlang and GHC both work like that, quite successfully: http://shootout.alioth.debian.org/gp4/benchmark.php?test=threadring&lang=all > How would you wait on a pipe in one thread, a socket in another, a > semaphore in a third? You can select on pipes and sockets, I think. Not sure about semaphores. > (Are there any popular OSes that offer a unified polling interface to > all possible synchronizations?) And what do you do about drivers or > libraries that make underlying blocking calls? What if you have a > busy calculation going on in the background? I don't think the concept of "drivers" applies to user-mode programs. For FFI calls you would use an OS thread. The language runtime switches between busy computation threads on a timer tick. From wells at submute.net Wed Jul 22 20:40:11 2009 From: wells at submute.net (Wells Oliver) Date: Wed, 22 Jul 2009 19:40:11 -0500 Subject: RPY2 examples? Message-ID: <3f1a902d0907221740i62487600xc2210ca9fede5791@mail.gmail.com> I am trying to find examples using RPY2 to render R graphs to PNG/PDF/etc. The only things I can find use rpy 1.x. Any references? Thanks! -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwmsmith at gmail.com Wed Jul 22 21:25:17 2009 From: kwmsmith at gmail.com (Kurt Smith) Date: Wed, 22 Jul 2009 21:25:17 -0400 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> Message-ID: On Wed, Jul 22, 2009 at 2:48 AM, Bearophile wrote: > greg: >> Posting benchmark times for Pyrex or Cython is pretty >> meaningless without showing the exact code that was >> used, since times can vary enormously depending on >> how much you C-ify things. > > Was this link, shown by William, not enough? > http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 I took a stab at converting the recent psyco-optimized code to cython, and got a speedup. gcj 4.3.3 1.39s gcc 4.3.3 1.55s cython 11.2 1.91s psyco 1.94s javac 1.5.0_19 2.00s python 2.5.4 168.37s It was just a matter of cdef-ing all the arrays & integers -- bearophile already did the hard work :-) Here's the cython code; all the others are from the repo. ################################################# DEF NMOVES = 8 DEF SIDE = 5 DEF SQR_SIDE = SIDE * SIDE cdef int circuit[SQR_SIDE] cdef int nsolutions = 0 cdef int movex[NMOVES] cdef int movey[NMOVES] py_movex = [-1,-2,-2,-1,+1,+2,+2,+1] py_movey = [-2,-1,+1,+2,+2,+1,-1,-2] for i in range(NMOVES): movex[i] = py_movex[i] movey[i] = py_movey[i] shift = [x * SIDE + y for x,y in zip(py_movex, py_movey)] cdef int shift_0 = shift[0] cdef int shift_1 = shift[1] cdef int shift_2 = shift[2] cdef int shift_3 = shift[3] cdef int shift_4 = shift[4] cdef int shift_5 = shift[5] cdef int shift_6 = shift[6] cdef int shift_7 = shift[7] def showCircuit(): print for x in xrange(SIDE): x_SIDE = x * SIDE for y in xrange(SIDE): if SQR_SIDE < 100: print "%02d " % circuit[x_SIDE + y], else: print "%03d " % circuit[x_SIDE + y], print cdef void solve(int nb, int x, int y, int SIDE=SIDE, int SQR_SIDE=SQR_SIDE, int *circuit=circuit, int shift_0=shift_0, int shift_1=shift_1, int shift_2=shift_2, int shift_3=shift_3, int shift_4=shift_4, int shift_5=shift_5, int shift_6=shift_6, int shift_7=shift_7, ): global nsolutions cdef int newx, newy cdef int pos = x * SIDE + y circuit[pos] = nb if nb == SQR_SIDE: #showCircuit() nsolutions += 1 circuit[pos] = 0 return newx = x + -1 if newx >= 0 and newx < SIDE: newy = y + -2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_0]: solve(nb+1, newx, newy) newx = x + -2 if newx >= 0 and newx < SIDE: newy = y + -1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_1]: solve(nb+1, newx, newy) newx = x + -2 if newx >= 0 and newx < SIDE: newy = y + 1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_2]: solve(nb+1, newx, newy) newx = x + -1 if newx >= 0 and newx < SIDE: newy = y + 2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_3]: solve(nb+1, newx, newy) newx = x + 1 if newx >= 0 and newx < SIDE: newy = y + 2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_4]: solve(nb+1, newx, newy) newx = x + 2 if newx >= 0 and newx < SIDE: newy = y + 1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_5]: solve(nb+1, newx, newy) newx = x + 2 if newx >= 0 and newx < SIDE: newy = y + -1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_6]: solve(nb+1, newx, newy) newx = x + 1 if newx >= 0 and newx < SIDE: newy = y + -2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_7]: solve(nb+1, newx, newy) circuit[pos] = 0 def main(): print "Search for side=%d" % SIDE cdef int x,y for x in range(SIDE): for y in range(SIDE): solve(1, x, y); print "\n%dx%d case, %d solutions." % (SIDE, SIDE, nsolutions) def run(): import time s=time.time() main() print time.time()-s ################################################# From scriptlearner at gmail.com Wed Jul 22 21:45:45 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Wed, 22 Jul 2009 18:45:45 -0700 (PDT) Subject: regex: multiple matching for one string Message-ID: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I will like to take out the values (valuea, valueb, and valuec). How do I do that in Python? The group method will only return the matched part. Thanks. p = re.compile('#a=*;b=*;c=*;') m = p.match(line) if m: print m.group(), From ptmcg at austin.rr.com Wed Jul 22 21:53:33 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Wed, 22 Jul 2009 18:53:33 -0700 (PDT) Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> Message-ID: <88e8ea19-7106-406a-a531-88c2b6893987@y7g2000yqa.googlegroups.com> On Jul 22, 5:43?pm, Filip wrote: > > My library, rather than parsing the whole input into a tree, processes > it like a char stream with regular expressions. > Filip - In general, parsing HTML with re's is fraught with easily-overlooked deviations from the norm. But since you have stepped up to the task, here are some comments on your re's: # You should use raw string literals throughout, as in: # blah_re = re.compile(r'sljdflsflds') # (note the leading r before the string literal). raw string literals # really help keep your re expressions clean, so that you don't ever # have to double up any '\' characters. # Attributes might be enclosed in single quotes, or not enclosed in any quotes at all. attr_re = re.compile('([\da-z]+?)\s*=\s*\"(.*?)\"', re.DOTALL | re.UNICODE | re.IGNORECASE) # Needs re.IGNORECASE, and can have tag attributes, such as
line_break_re = re.compile('', re.UNICODE) # what about HTML entities defined using hex syntax, such as &#xxxx; amp_re = re.compile('\&(?![a-z]+?\;)', re.UNICODE | re.IGNORECASE) How would you extract data from a table? For instance, how would you extract the data entries from the table at this URL: http://tf.nist.gov/tf-cgi/servers.cgi ? This would be a good example snippet for your module documentation. Try extracting all of the sldjlsfjd links from yahoo.com, and see how much of what you expect actually gets matched. Good luck! -- Paul From rurpy at yahoo.com Wed Jul 22 22:00:42 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Wed, 22 Jul 2009 19:00:42 -0700 (PDT) Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: On Jul 22, 7:45 pm, "scriptlear... at gmail.com" wrote: > For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I > will like to take out the values (valuea, valueb, and valuec). How do > I do that in Python? The group method will only return the matched > part. Thanks. > > p = re.compile('#a=*;b=*;c=*;') > m = p.match(line) > if m: > print m.group(), p = re.compile('#a=([^;]*);b=([^;]*);c=([^;]*);') m = p.match(line) if m: print m.group(1),m.group(2),m.group(3), Note that "=*;" in your regex will match zero or more "=" characters -- probably not what you intended. "[^;]* will match any string up to the next ";" character which will be a value (assuming you don't have or care about embedded whitespace.) You might also want to consider using a r'...' string for the regex, which will make including backslash characters easier if you need them at some future time. From wuwei23 at gmail.com Wed Jul 22 22:04:34 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 22 Jul 2009 19:04:34 -0700 (PDT) Subject: Ideas for problem with chat server application! References: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> Message-ID: <40c22282-7526-4064-8885-62a87107bb3d@p10g2000prm.googlegroups.com> On Jul 22, 6:27?pm, "David Adamo Jr." wrote: > Suggestions are highly appreciated. I haven't used it but I've heard good things about FireDaemon, which can run any script or program as a Windows service. Apparently the Lite version is free: http://www.download-freeware-shareware.com/Freeware-System-Utilities.php?Type=14387 Hope this helps. From cindy at speed-voip.com Wed Jul 22 22:36:31 2009 From: cindy at speed-voip.com (cindy) Date: Wed, 22 Jul 2009 22:36:31 -0400 Subject: [Unblocking VoIP in UAE] for freedom! Message-ID: Please view this newsletter online at: http://www.mynewsletterbuilder.com/tools/view_newsletter.php?newsletter_id=1409985533 SpeedVoIP Communication Technology Co., LTD. - 600, 6th Avenue S.W. - Calgary - Alberta - T2P 0S5 Subscribe to this newsletter: https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=s&newsletter_id=1409985533 Unsubscribe python-list at python.org: https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=u&email=python-list at python.org Change your preferences: https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=p&email=python-list at python.org Forward to a friend: http://www.mynewsletterbuilder.com/tools/forward.php?username=svsales&newsletter_id=1409985533&email=python-list at python.org&send_id=712043215 Report this email as spam: https://www.mynewsletterbuilder.com/tools/abuse_report.php?username=svsales&send_id=712043215&email=python-list at python.org This email was sent using MyNewsletterBuilder.com. -------------- next part -------------- An HTML attachment was scrubbed... URL: From songofacandy at gmail.com Wed Jul 22 23:06:56 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Wed, 22 Jul 2009 20:06:56 -0700 (PDT) Subject: What is file.encoding convention? Message-ID: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> In document : >> The encoding that this file uses. When Unicode strings are written to a file, >> they will be converted to byte strings using this encoding. In addition, >> when the file is connected to a terminal, the attribute gives the encoding >> that the terminal is likely to use But in logging.StreamHandler.emit() :: try: if (isinstance(msg, unicode) and getattr(stream, 'encoding', None)): #fs = fs.decode(stream.encoding) try: stream.write(fs % msg) except UnicodeEncodeError: #Printing to terminals sometimes fails. For example, #with an encoding of 'cp1251', the above write will #work if written to a stream opened or wrapped by #the codecs module, but fail when writing to a #terminal even when the codepage is set to cp1251. #An extra encoding step seems to be needed. stream.write((fs % msg).encode (stream.encoding)) else: stream.write(fs % msg) except UnicodeError: stream.write(fs % msg.encode("UTF-8")) And behavior of sys.stdout in Windows:: >>> import sys >>> sys.stdout.encoding 'cp932' >>> u = u"???" >>> u u'\u3042\u3044\u3046' >>> print >>sys.stdout, u ??? >>> sys.stderr.write(u) Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) What is file.encoding convention? If I want to write a unicode string to a file(-like) that have encoding attribute, I should do (1) try: file.write(unicode_str), (2) except UnicodeEncodeError: file.write(unicode_str.encode (file.encoding)) like logging? It seems agly. From sjmachin at lexicon.net Wed Jul 22 23:24:47 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 22 Jul 2009 20:24:47 -0700 (PDT) Subject: Not understanding this documentation References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> <081d1a50-fd7f-4f87-889e-262bc7e2debd@x5g2000prf.googlegroups.com> Message-ID: On Jul 23, 2:45?am, "F.O.P." wrote: > On Jul 22, 9:07?am, "Rhodri James" > wrote: > > > > > > > > > On Wed, 22 Jul 2009 15:46:09 +0100, F.O.P. ? > > wrote: > > > >http://bugs.python.org/issue5358 > > > I simply installed 3.0 on my ubuntubox brought my project from a > > > windows machine and made necessary changes to what I had used from os > > > and os.path. ?I have an error at the end of this line: > > > for k in range(I): > > > and the error: > > > SyntaxError: invalid character in identifier > > > > I've already declared I way above as the lenth of an array (I = len > > > (IA)) > > > There shouldn't be a problem there aren't any issues with other > > > declarations identical to this. > > > Could you show us slightly more of your code and the rest of the > > error message, please? ?I have a suspicion that the error actually > > lies in the "necessary changes" you made. > > > -- > > Rhodri James *-* Wildebeest Herder to the Masses > > I spoke to a friend about it and he found that I had put in a unicode > "semicolon" rather than ASCII. This is rather puzzling. What are you describing as a "unicode semicolon"? Possibilities include: (a) U+003B SEMICOLON (which is exactly what you need) (b) U+037E GREEK QUESTION MARK (which looks like (a)) (c) U+061B ARABIC SEMICOLON (which looks like (a) rotated 180 degrees) How did you get it into your source file? What was in your # coding: ????? or similar line at the top of your source file? From aahz at pythoncraft.com Wed Jul 22 23:38:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 22 Jul 2009 20:38:30 -0700 Subject: comments? storing a function in an object References: Message-ID: In article , Carl Banks wrote: > >You have to be REALLY REALLY careful not to pass any user-supplied >data to it if this is a server running on your computer, of course. Unless, of course, your users are paying for this service. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From aahz at pythoncraft.com Wed Jul 22 23:54:11 2009 From: aahz at pythoncraft.com (Aahz) Date: 22 Jul 2009 20:54:11 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: In article , D'Arcy J.M. Cain wrote: >On 20 Jul 2009 17:10:55 -0700 >aahz at pythoncraft.com (Aahz) wrote: >>>I understand what you want but I can't see how a toolkit can do that. >>>How do you program "graceful?" It seems pretty application specific. >> >> Presumably the JS toolkit generates both code and HTML. Actions that >> normally get executed by JS should degrade to plain HTML links and form >> submits (or possibly not display at all if it's a pure convenience). > >As I said, I do understand what you are after but I still think it is a >judgement call. What defines "convenience?" How does the library know >what is essential and what is pretty? Presumably a JS toolkit designed for this purpose would have an API to specify the handling of such issues. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From aahz at pythoncraft.com Wed Jul 22 23:55:20 2009 From: aahz at pythoncraft.com (Aahz) Date: 22 Jul 2009 20:55:20 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: <1c994086-8c58-488f-b3b3-6161c4b2ba05@k30g2000yqf.googlegroups.com> Message-ID: In article <1c994086-8c58-488f-b3b3-6161c4b2ba05 at k30g2000yqf.googlegroups.com>, Paul Boddie wrote: >On 20 Jul, 18:00, a... at pythoncraft.com (Aahz) wrote: >> >> Out of curiosity, are there any JavaScript toolkits that generate code >> that degrades gracefully when JavaScript is disabled? > >You mean "Web toolkits which use JavaScript", I presume. I have >written (and use myself) a toolkit/framework called XSLForms (part of >the XSLTools distribution) which supports in-page updates (AJAX, >effectively) that degrade to vanilla whole-page requests if JavaScript >is switched off: > >http://www.boddie.org.uk/python/XSLTools.html Thanks! I'll take a look after OSCON. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From gagsl-py2 at yahoo.com.ar Thu Jul 23 00:27:12 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 23 Jul 2009 01:27:12 -0300 Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: En Wed, 22 Jul 2009 11:36:51 -0300, Inky 788 escribi?: > On Jul 22, 2:36?am, Hendrik van Rooyen > wrote: >> The good reason is the immutability, which lets you use >> a tuple as a dict key. ? > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. Many people posted useful examples of tuples as dictionary keys in this thread. Just to add another one (emulate SQL GROUP BY): ope_by_dept = defaultdict(int) total_times = defaultdict(float) for dept_name, ope_name, ope_date, engineer in list_of_operations: ope_by_dept[dept_name, ope_start.month] += 1 total_times[dept_name, engineer] += ope_end - ope_start print "Operations per department per month" for dept_name, month in sorted(ope_by_dept): print dept_name, month, ope_by_dept[dept_name, month] -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 23 00:27:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 23 Jul 2009 01:27:15 -0300 Subject: logging outgoing HTTP POST message and incoming response message References: <7cpcjaF2915u5U1@mid.uni-berlin.de> <0b403bd0-759f-49b6-9eac-3d585d877fe3@k13g2000prh.googlegroups.com> Message-ID: En Wed, 22 Jul 2009 18:18:37 -0300, scriptlearner at gmail.com escribi?: > On Jul 22, 1:54?pm, "Diez B. Roggisch" wrote: >> You can use proxy-tools such as tcpmon or sniff traffic using wireshark. > > Thanks, > but I am trying to enable some debug mode to log all outgoing and > incoming messages for certain period of time, and running another > proxy-tool is not very ideal. It would be great to log it in some log > file. You may install a custom HTTPHandler: class LoggingHTTPConnection(httplib.HTTPConnection): def request(self, method, url, body=None, headers={}): print method, url, headers httplib.HTTPConnection.request(self, method, url, body, headers) def getresponse(self): response = httplib.HTTPConnection.getresponse(self) print response.status, response.msg return response class LoggingHTTPHandler(urllib2.HTTPHandler): def http_open(self, req): return self.do_open(LoggingHTTPConnection, req) opener = urllib2.build_opener(LoggingHTTPHandler, ...) -- Gabriel Genellina From nagle at animats.com Thu Jul 23 00:40:38 2009 From: nagle at animats.com (John Nagle) Date: Wed, 22 Jul 2009 21:40:38 -0700 Subject: MySQLdb for Python 3.1 getting close? Message-ID: <4a67e912$0$1603$742ec2ed@news.sonic.net> Is MySQLdb available for Python 3.1 yet? http://sourceforge.net/projects/mysql-python/ says the last supported Python version is 2.5. Any progress in sight? John Nagle From martin.hellwig at dcuktec.org Thu Jul 23 02:16:22 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 23 Jul 2009 07:16:22 +0100 Subject: Ideas for problem with chat server application! In-Reply-To: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> References: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> Message-ID: David Adamo Jr. wrote: > My > attempt was to create a windows service that start automatically and > runs this batch file using a Network Service account on the server > system. Although, I'm having a hard time with this (temporarily), I > would love to ask if there are any alternatives to using a windows > service. Suggestions are highly appreciated. This is definitely the right way to go, perhaps reviewing a template which I use for windows services may help you: http://blog.dcuktec.com/2009/07/python-windows-service-template.html -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From jayshree06comp at gmail.com Thu Jul 23 02:37:17 2009 From: jayshree06comp at gmail.com (jayshree) Date: Wed, 22 Jul 2009 23:37:17 -0700 (PDT) Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: On Jul 21, 8:59?pm, Piet van Oostrum wrote: > >>>>> jayshree (j) wrote: > >j> M2Crypto package not showing the 'recipient_public_key.pem' file at > >j> linux terminal .how do i get/connect with recipient public key. > >j> exactly i need to check how can i open this file through linux > >j> commands. > >j> import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open > >j> ('recipient_public_key.pem','rb').read()) print recip; plaintext = > >j> whatever i need to encrypt msg = recip.public_encrypt > >j> (plaintext,RSA.pkcs1_padding) print msg; > >j> after calling the function its not giving any output and even any > >j> error > >j> i also tried as 'Will' said > >j> pk = open('public_key.pem','rb').read() print pk; rsa = > >j> M2Crypto.RSA.load_pub_key(pk) > >j> whats the mistake i am not getting .will somebody help me out. > >j> is something wrong in opening 'recipient_public_key.pem'. is M2Crypto > >j> contain this file inbuilt .from where this file taking public key of > >j> the recipient i'. what this file contain and how it will work .should > >j> i need to create such a file for my purpose.if it is then how can i > >j> create this ?and how it will retrieve the key,where i recognize my > >j> recipient to get his public key .is something like database at server. > >j> please give me a quick response...... > >j> looking for your answer. > > Please: > 1. Type your python code with newlines and proper indentation. > 2. Show the error messages that your code gives when you run it. > 3. Use proper capital letters at the beginning of your sentences. > 4. Don't fire so many questions in rapid succession. > > The recipient_public_key.pem file is the public key of the recipient > which means the person that is going to receive the encrypted message. > You should get it from the recipient him/herself or from some key store > where s/he has deposited it. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org- Hide quoted text - > > - Show quoted text - Here is the code that i used import M2Crypto from M2Crypto import RSA def encrypt(): recip = M2Crypto.RSA.load_pub_key(open ('recipient_public_key.pem','rb').read()) #recip = M2Crypto.RSA.load_pub_key('recipient_public_key.pem') print recip; plaintext =" whatever i need to encrypt" msg = recip.public_encrypt(plaintext,RSA.pkcs1_padding) print msg; encrypt() error coming like - IOError: [Errno 2] No such file or directory: 'recipient_public_key.pem' Is this not the inbuilt file. How should i create such type of file. From wilk at flibuste.net Thu Jul 23 02:44:56 2009 From: wilk at flibuste.net (William Dode) Date: 23 Jul 2009 06:44:56 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> Message-ID: <4a6806e8$0$22900$426a74cc@news.free.fr> On 23-07-2009, Kurt Smith wrote: > On Wed, Jul 22, 2009 at 2:48 AM, Bearophile wrote: >> greg: >>> Posting benchmark times for Pyrex or Cython is pretty >>> meaningless without showing the exact code that was >>> used, since times can vary enormously depending on >>> how much you C-ify things. >> >> Was this link, shown by William, not enough? >> http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 > > I took a stab at converting the recent psyco-optimized code to cython, > and got a speedup. thanks, i updated my repo with litle more tips. -- William Dod? - http://flibuste.net Informaticien Ind?pendant From amrita at iisermohali.ac.in Thu Jul 23 03:22:15 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Thu, 23 Jul 2009 12:52:15 +0530 (IST) Subject: how two join and arrange two files together Message-ID: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> Hi, I have two large files: FileA 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 23 ALA H = 8.78 N = CA = HA = C = 179.93................. and FileB 21 ALA helix (helix_alpha, helix2) 23 ALA helix (helix_alpha, helix3) 38 ALA helix (helix_alpha, helix3)........... now what i want that i will make another file in which i will join the two file in such a way that only matching entries will come like here 21 and 23 ALA is in both files, so the output will be something like:- 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA helix (helix_alpha, helix2) 23 ALA H = 8.78 N = CA = HA = C = 179.93|23 ALA helix (helix_alpha, helix3) and further i will make another file in which i will be able to put those lines form this file based on the missing atom value, like for 21 ALA HA is not defined so i will put it another file based on its HA missing value similarly i will put 23 ALA on another file based on its missing N,CA and HA value. I tried to join the two file based on their matching entries by:--- >>>from collections import defaultdict >>> >>> if __name__ == "__main__": ... a = open("/home/amrita/alachems/chem100.txt") ... c = open("/home/amrita/secstr/secstr100.txt") ... >>> def source(stream): ... return (line.strip() for line in stream) ... ... >>> def merge(sources): ... for m in merge([source(a),source(c)]): ... print "|".join(c.ljust(10) for c in m) ... but it is not giving any value. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From hendrik at microcorp.co.za Thu Jul 23 03:25:22 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 23 Jul 2009 09:25:22 +0200 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <200907230925.22747.hendrik@microcorp.co.za> On Wednesday 22 July 2009 12:03:44 superpollo wrote: > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? 00101101 is not hex 45. hex 45 is 01000101 >>> chr(int('01000101',2)) 'E' >>> - Hendrik From hendrik at microcorp.co.za Thu Jul 23 03:42:27 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 23 Jul 2009 09:42:27 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> References: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: <200907230942.27558.hendrik@microcorp.co.za> On Wednesday 22 July 2009 16:36:51 Inky 788 wrote: > On Jul 22, 2:36?am, Hendrik van Rooyen > > wrote: > > The good reason is the immutability, which lets you use > > a tuple as a dict key. ? > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. Steven showed why you cannot have a mutable thing as a key in a dict. if you think it is contrived, then please consider how you would keep track of say the colour of a pixel on a screen at position (x,y) - this is about the simplest "natural" tuple format and example. There are other equally valid examples, as has been pointed out. (may have been in another thread - am a bit confused about that right now) - Hendrik From clp2 at rebertia.com Thu Jul 23 04:16:04 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 23 Jul 2009 01:16:04 -0700 Subject: how two join and arrange two files together In-Reply-To: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> References: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907230116m67a73918gbd344b9a5289b45@mail.gmail.com> On Thu, Jul 23, 2009 at 12:22 AM, wrote: > > Hi, > > I have two large files: > > FileA > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 > 23 ALA H = 8.78 N = ?CA = ?HA = C = 179.93................. > > and > > FileB > 21 ALA ?helix (helix_alpha, helix2) > 23 ALA ?helix (helix_alpha, helix3) > 38 ALA ?helix (helix_alpha, helix3)........... > > now what i want that i will make another file in which i will join the two > file in such a way that only matching entries will come like here 21 and > 23 ALA is in both files, so the output will be something like:- > > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA ?helix > (helix_alpha, helix2) > 23 ALA H = 8.78 N = ?CA = ?HA = C = 179.93|23 ALA ?helix (helix_alpha, > helix3) > > and further i will make another file in which i will be able to put those > lines form this file based on the missing atom value, like for 21 ALA HA > is not defined so i will put it another file based on its HA missing value > similarly i will put 23 ALA on another file based on its missing N,CA and > HA value. > > I tried to join the two file based on their matching entries by:--- >>>>from collections import defaultdict >>>> >>>> if __name__ == "__main__": > ... ? ? ?a = open("/home/amrita/alachems/chem100.txt") > ... ? ? ?c = open("/home/amrita/secstr/secstr100.txt") > ... >>>> def source(stream): > ... ? ? return (line.strip() for line in stream) > ... > ... >>>> def merge(sources): > ... ? ? for m in merge([source(a),source(c)]): > ... ? ? ? ? print "|".join(c.ljust(10) for c in m) > ... > > but it is not giving any value. You never actually called any of your functions. Slightly corrected version: from collections import defaultdict def source(stream): return (line.strip() for line in stream) def merge(sources): for m in sources: print "|".join(c.ljust(10) for c in m) if __name__ == "__main__": a = open("/home/amrita/alachems/chem100.txt") c = open("/home/amrita/secstr/secstr100.txt") merge([source(a), source(c)]) It's still not sophisticated enough to give the exact output you're looking for, but it is a step in the right direction. You really should try asking someone from your CS Dept to help you. It would seriously take a couple hours, at most. - Chris -- Still brandishing a cluestick a vain... http://blog.rebertia.com From pavlovevidence at gmail.com Thu Jul 23 04:47:03 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 01:47:03 -0700 (PDT) Subject: comments? storing a function in an object References: Message-ID: <20efdb6a-c1a5-47dc-8546-7c4ae548e22d@g1g2000pra.googlegroups.com> On Jul 22, 8:38?pm, a... at pythoncraft.com (Aahz) wrote: > In article , > Carl Banks ? wrote: > > >You have to be REALLY REALLY careful not to pass any user-supplied > >data to it if this is a server running on your computer, of course. > > Unless, of course, your users are paying for this service. Well, yes, but I assume that by the time you're deliberately letting users pay to run their programs on your server, you will already have deployed a full-blown, multi-tiered security strategy that includes validation by the server process. That was sort of beyond the scope of the OP's question. Carl Banks From helvinlui at gmail.com Thu Jul 23 04:55:51 2009 From: helvinlui at gmail.com (Helvin) Date: Thu, 23 Jul 2009 01:55:51 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> Message-ID: <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> I believe I now have vtkpython.exe. However, my 'import vtk' statement in my python code is not working. The error says something like "no module named vtk". Where do I find modules for vtk in pyqt? Do they exist? They must, right? Because there are people using vtk in pyqt? Or do I have to use OpenGL? Confused... Helvin From jjkk73 at gmail.com Thu Jul 23 05:01:45 2009 From: jjkk73 at gmail.com (jorma kala) Date: Thu, 23 Jul 2009 10:01:45 +0100 Subject: How to document Python code properly for Pydoc In-Reply-To: References: Message-ID: Thanks very much for your help On Wed, Jul 22, 2009 at 6:43 PM, Lie Ryan wrote: > jorma kala wrote: > > Hi, > > Do you know where I can find the rules for documenting Python code, so > > that automatic document generation with Pydoc makes the most of the > > comments inserted in the code? > > I know about documenting class and method through triple quote just > > under the class definition. But how do you comment a specific field or > > variable, or how do you document function arguments so that they are > > extracted like in javadoc? > > Thanks very much > > pydoc is a simple tool, and doesn't do much. You write in freeform, > although generally you'll do something like this: > > def myfunc(a, b): > ''' > short description of myfunc > > longer description of myfunc, if necessary, and typically includes > description of the arguments and the behaviors. Also includes the > description of the return value. > ''' > > pass > > pydoc doesn't recognize any special markups. If you want to get more > from the docstring, you need other documentation generator such as > epydoc, Doxygen, or Sphinx. > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From user at example.net Thu Jul 23 05:04:24 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 11:04:24 +0200 Subject: binary literal In-Reply-To: References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <4a682799$0$28504$4fafbaef@reader2.news.tin.it> Hendrik van Rooyen wrote: > On Wednesday 22 July 2009 12:03:44 superpollo wrote: > > >>can i do something like the above, but using a *binary* number? (e.g. >>00101101 instead of 45) ? > > > 00101101 is not hex 45. > hex 45 is 01000101 > whoopsie daisie! > >>>>chr(int('01000101',2)) > > 'E' > much obliged. bye From ryszard.szopa at gmail.com Thu Jul 23 05:08:39 2009 From: ryszard.szopa at gmail.com (Ryszard Szopa) Date: Thu, 23 Jul 2009 02:08:39 -0700 (PDT) Subject: strange error when trying to log something Message-ID: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Hi, I've recently reinstalled Python 2.6 (from DMG) on my Mac, and I am running into very strage errors. Namely, logging seems to be badly broken. When I open the interpreter through Django's manage.py shell and try to use logging, I get the following error: >>> logging.critical('ala') ------------------------------------------------------------ Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1416, in critical root.critical(*((msg,)+args), **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1074, in critical self._log(CRITICAL, msg, args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1142, in _log record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1117, in makeRecord rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 272, in __init__ from multiprocessing import current_process File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/multiprocessing/__init__.py", line 64, in from multiprocessing.util import SUBDEBUG, SUBWARNING File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/multiprocessing/util.py", line 121, in _afterfork_registry = weakref.WeakValueDictionary() File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/weakref.py", line 51, in __init__ UserDict.UserDict.__init__(self, *args, **kw) TypeError: unbound method __init__() must be called with UserDict instance as first argument (got WeakValueDictionary instance instead) I was able to silence the error (and be able to work normally) by making UserDict.UserDict inherit from object. Any ideas what is causing the error? Before I updated Python everything was fine. Am I breaking a lot of things by making UserDict.UserDict a new style class? Thanks in advance for any insight. -- Ryszard Szopa From songofacandy at gmail.com Thu Jul 23 05:24:42 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Thu, 23 Jul 2009 02:24:42 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> Message-ID: <697fa41f-9c6a-438f-9c47-9566a79bdf82@p36g2000prn.googlegroups.com> > What is file.encoding convention? > If I want to write a unicode string to a file(-like) that have > encoding attribute, I should do > (1) try: file.write(unicode_str), > (2) except UnicodeEncodeError: file.write(unicode_str.encode > (file.encoding)) > like logging? > It seems agly. s/agly/ugly/ From __peter__ at web.de Thu Jul 23 05:29:13 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 11:29:13 +0200 Subject: strange error when trying to log something References: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Message-ID: Ryszard Szopa wrote: > Hi, > > I've recently reinstalled Python 2.6 (from DMG) on my Mac, and I am > running into very strage errors. Namely, logging seems to be badly > broken. When I open the interpreter through Django's manage.py shell > and try to use logging, I get the following error: > >>>> logging.critical('ala') > ------------------------------------------------------------ > Traceback (most recent call last): > File "", line 1, in > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1416, in critical > root.critical(*((msg,)+args), **kwargs) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1074, in critical > self._log(CRITICAL, msg, args, **kwargs) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1142, in _log > record = self.makeRecord(self.name, level, fn, lno, msg, args, > exc_info, func, extra) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1117, in makeRecord > rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 272, in __init__ > from multiprocessing import current_process > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/multiprocessing/__init__.py", line 64, in > from multiprocessing.util import SUBDEBUG, SUBWARNING > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/multiprocessing/util.py", line 121, in > _afterfork_registry = weakref.WeakValueDictionary() > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/weakref.py", line 51, in __init__ > UserDict.UserDict.__init__(self, *args, **kw) > TypeError: unbound method __init__() must be called with UserDict > instance as first argument (got WeakValueDictionary instance instead) > > I was able to silence the error (and be able to work normally) by > making UserDict.UserDict inherit from object. > > Any ideas what is causing the error? Before I updated Python > everything was fine. Am I breaking a lot of things by making > UserDict.UserDict a new style class? > > Thanks in advance for any insight. > > -- Ryszard Szopa I have a hunch that you are triggering a reload() somewhere. Example: Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import weakref >>> weakref.WeakValueDictionary() >>> import UserDict >>> reload(UserDict) >>> weakref.WeakValueDictionary() Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/weakref.py", line 51, in __init__ UserDict.UserDict.__init__(self, *args, **kw) TypeError: unbound method __init__() must be called with UserDict instance as first argument (got WeakValueDictionary instance instead) Try restarting the interpreter after any change to source files. Peter From nobody at nowhere.com Thu Jul 23 05:37:55 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 23 Jul 2009 10:37:55 +0100 Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: On Wed, 22 Jul 2009 15:17:52 -0700, Carl Banks wrote: > So do all these OSes have some kind of __mega_unifying_poll system > call that works for anything that might possibly block, that you can > exploit from a user process? Threads ;) They also have the advantage that one thread can run while another is waiting on disk I/O, which isn't something which can be done with a select/poll interface (even if select/poll worked for files, it doesn't help for mapped files). From hackingkk at gmail.com Thu Jul 23 05:50:10 2009 From: hackingkk at gmail.com (Krishnakant) Date: Thu, 23 Jul 2009 15:20:10 +0530 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: References: Message-ID: <1248342610.3698.7.camel@krishna-laptop> On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote: > Being a sudoer is not a privilege to issue the os.setuid system call. It > is only a permission to use the sudo command. > Yes, So I would like to know if python can change the user to some other non-privileged user during the script execution? > >K> I tryed using subprocess but that did not help me either. I tryed sudo > >K> su into the Popen command but it throws me into the terminal (shell) > >K> with postgres as the user. > > You could execute the command: > sudo -u postgres required_command > with subprocess. > Ok, but the problem is much more complex. What if I want to do the following. 1, change the user for a particular script to the postgres user. 2. now execute the python code for connecting to the postgresql database. In the second point I actually want to execute python code not shell level command so will the sudo -u in the subprocess.Popen change the user in the script? In short I would just like to have the script run under another user let's say postgres as long as a certain action is going on, for example connecting to the postgresql database. > You have another problem then: your password must be supplied unless the > NOPASSWD flag is set in the sudoers file. > That is clear, the only problem is that I want the script to run as postgres user although it was started by the user kk. happy hacking. Krishnakant. From news123 at free.fr Thu Jul 23 05:56:45 2009 From: news123 at free.fr (News123) Date: Thu, 23 Jul 2009 11:56:45 +0200 Subject: available formats and params for Image.save() Message-ID: <4a6833dd$0$22599$426a34cc@news.free.fr> Hi, Somehow I have difficulties reading the documentation for PIL (Image) Is there an easy way to know which formats are supported and what their names are? Is there an easy way to know which parameters are supported by Image.save(). How can I list them where are they documented? Somehow I consider the documentation to be a little weak on this points. all the documentation says (at least where I looked) is: > class Image > | Methods defined here: > . . . > save(self, fp, format=None, **params) > | Save image to file or stream In order to find out how the format name for a .jpg file I did following: import Image img = Image.open("afile.jpg") print img.format Then I saw, that the result was 'JPEG' and not 'JPG' as I tried first I'm at a complete loss at finding out what parameters the save function accepts for saving a JPG file or a PNG file Is there an easy way of finding this in any doc or do I have to dive into the sources. If there's no easy way: Wouldn't a better documentation increase the 'user experience'? Thanks in advance for any pointers N From nobody at nowhere.com Thu Jul 23 06:03:58 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 23 Jul 2009 11:03:58 +0100 Subject: Multiple versions of python References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: On Tue, 21 Jul 2009 10:19:42 -0400, Dave Angel wrote: > The other thing you may want to do in a batch file is to change the file > associations so that you can run the .py file directly, without typing > "python" or "pythonw" in front of it. > > The relevant Windows commands are: assoc and ftype However: assoc and ftype modify the registry keys under HKLM\Software\Classes. This works fine if these are the only relevant keys, but if you also have settings under HKCU\Software\Classes, those will take precedence, so assoc and/or ftype won't have any effect. Also, you typically need at least "Power User" status in order to modify the keys under HKLM, while any user should be able to modify those under HKCU (HKCU is a "virtual" key, corresponding to HKU\). From user at example.net Thu Jul 23 06:23:13 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 12:23:13 +0200 Subject: raster (PIL) Message-ID: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> hi. i wrote a program which transforms a string of zeroes ando ones into a png file. #!/usr/bin/env python import Image import sys bits_in_a_byte = 8 raster_string = """\ 00000000000000000000000000000000 00100100111100100000100000111100 00100100100000100000100000100100 00111100111000100000100000100100 00100100100000100000100000100100 00100100111100111100111100111100 00000000000000000000000000000000 00000000000000000000000000000000 00000000111100100000111100000000 00000000100000100000100100000000 00000000100000100000111100000000 00000000100000100000100000000000 00000000111100111100100000000000 00000000000000000000000000000000 """ raster_lines = raster_string.splitlines() high = len(raster_lines) wide = len(raster_lines[0]) bytes_in_a_row = wide/bits_in_a_byte bitmap = "" for raster_line in raster_lines: for byte_count in range(bytes_in_a_row): first_bit = byte_count*bits_in_a_byte bitmap += chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) im = Image.fromstring("1", (wide , high) , bitmap) im.save(sys.stdout , "PNG") any suggestions for improvement? bye From digitig at gmail.com Thu Jul 23 06:28:22 2009 From: digitig at gmail.com (Tim Rowe) Date: Thu, 23 Jul 2009 11:28:22 +0100 Subject: Help understanding the decisions *behind* python? In-Reply-To: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: 2009/7/22 Inky 788 : > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. If you are used to working in a language that doesn't allow it then you'll probably carry on using the work-arounds that you have always used. It almost certainly only seems obscure because you're not considering it when it would be a natural solution. In a language that builds *very* heavily on the concept of dictionaries it's not obscure at all! -- Tim Rowe From nick at craig-wood.com Thu Jul 23 07:30:08 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 06:30:08 -0500 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: srepmub wrote: > please send any program that doesn't work with shedskin (it still is > experimental after all) to me, or create an issue at > shedskin.googlecode.com, and I will have a look at the problem. I divided and conquered the program as suggested and eventually I got it to compile and run correctly :-) I learnt that if you have lots of variables with indeterminate type then shedskin takes a very long time indeed before blowing up! I also learnt that shedskin doesn't support the idiom I'd been using for creating shallow copies, namely the Board.__new__(Board) below. shedskin compiles it ok, but the C++ won't compile complaning about not being able to find __init__ methods Producing these warnings *WARNING* rush_hour_solver_cut_down.py:71: class 'Vehicle' has no method '__new__' *WARNING* rush_hour_solver_cut_down.py:72: variable 'new' has no type *WARNING* rush_hour_solver_cut_down.py:236: variable 'new_vehicle' has no type And these compile errors rush_hour_solver_cut_down.cpp:94: error: ?__new__? is not a member of ?__rush_hour_solver_cut_down__::Vehicle? rush_hour_solver_cut_down.cpp:95: error: expected type-specifier before ?;? token rush_hour_solver_cut_down.cpp: In member function ?void* __rush_hour_solver_cut_down__::Board::move(int, int)?: rush_hour_solver_cut_down.cpp:276: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:276: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:279: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:279: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:281: error: invalid conversion from ?void*? to ?__rush_hour_solver_cut_down__::Vehicle*? def copy(self): new = Board.__new__(Board) new.me_x = self.me_x new.me_y = self.me_y new.depth = self.depth new.parent = self new.best_child = None new.board = [self.board[i][:] for i in range(WIDTH)] new.rep = self.rep[:] new.vehicles = self.vehicles[:] return new I changed to using copy.copy which did work, but I couldn't name my copy methods "copy" otherwise I got this error from the C++ compile rush_hour_solver_cut_down.cpp: In member function '__rush_hour_solver_cut_down__::Vehicle* __rush_hour_solver_cut_down__::Vehicle::copy()': rush_hour_solver_cut_down.cpp:94: error: no matching function for call to '__rush_hour_solver_cut_down__::Vehicle::copy(__rush_hour_solver_cut_down__::Vehicle* const)' rush_hour_solver_cut_down.cpp:89: note: candidates are: __rush_hour_solver_cut_down__::Vehicle* __rush_hour_solver_cut_down__::Vehicle::copy() rush_hour_solver_cut_down.cpp: In member function '__rush_hour_solver_cut_down__::Board* __rush_hour_solver_cut_down__::Board::copy()': rush_hour_solver_cut_down.cpp:135: error: no matching function for call to '__rush_hour_solver_cut_down__::Board::copy(__rush_hour_solver_cut_down__::Board* const)' rush_hour_solver_cut_down.cpp:129: note: candidates are: __rush_hour_solver_cut_down__::Board* __rush_hour_solver_cut_down__::Board::copy() So I renamed them to pycopy, and they ended up looking like def pycopy(self): new = copy(self) new.parent = self new.best_child = None new.board = [self.board[i][:] for i in range(WIDTH)] new.rep = self.rep[:] new.vehicles = self.vehicles[:] return new After all that - some timing results! Python: 9.3 seconds Psyco: 5.8 seconds ShedSkin: 1.0 seconds Impressive! I put the code http://www.craig-wood.com/nick/pub/rush_hour_solver_cut_down.py I left in the commented out bits of code I had to change. This is only part of the project (375 lines) - it solves Rush Hour boards. There is another part which I haven't attempted to compile yet which finds the most difficult possible boards using a combination of back tracking and a genetic algorithm. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From stef.mientki at gmail.com Thu Jul 23 07:36:54 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 13:36:54 +0200 Subject: sqlite3 performance problems only in python Message-ID: <4A684B56.3020906@gmail.com> hello, until now I used only small / simple databases in Python with sqlite3. Now I've a large and rather complex database. The most simple query (with just a result of 100 rows), takes about 70 seconds. And all that time is consumed in "cursor.fetchall" Using the same database in Delphi, using the same query, takes less than 5 seconds (including displaying the full table in a grid). Are there in Python faster ways to get the query results ? Would it be faster if I used an ODBC coupling and PyODBC to interface the database ? thanks, Stef Mientki From deets at nospam.web.de Thu Jul 23 07:47:45 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 13:47:45 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <7cr0v1F28dhltU1@mid.uni-berlin.de> superpollo wrote: > hi. > > i wrote a program which transforms a string of zeroes ando ones into a > png file. > > #!/usr/bin/env python > import Image > import sys > bits_in_a_byte = 8 > raster_string = """\ > 00000000000000000000000000000000 > 00100100111100100000100000111100 > 00100100100000100000100000100100 > 00111100111000100000100000100100 > 00100100100000100000100000100100 > 00100100111100111100111100111100 > 00000000000000000000000000000000 > 00000000000000000000000000000000 > 00000000111100100000111100000000 > 00000000100000100000100100000000 > 00000000100000100000111100000000 > 00000000100000100000100000000000 > 00000000111100111100100000000000 > 00000000000000000000000000000000 > """ > raster_lines = raster_string.splitlines() > high = len(raster_lines) > wide = len(raster_lines[0]) > bytes_in_a_row = wide/bits_in_a_byte This will give you the wrong result if not divideable by bits_in_a_byte. > bitmap = "" > for raster_line in raster_lines: > for byte_count in range(bytes_in_a_row): > first_bit = byte_count*bits_in_a_byte > bitmap += > chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) > im = Image.fromstring("1", (wide , high) , bitmap) > im.save(sys.stdout , "PNG") > > any suggestions for improvement? Instead of res = "" for ...: res += ... use res = [] for ...: res.append(...) "".join(res) There are some optimizations for the +=-op on strings, but I'm not sure how far they go, and the other form is safer. Diez From paul at subsignal.org Thu Jul 23 07:50:47 2009 From: paul at subsignal.org (paul) Date: Thu, 23 Jul 2009 13:50:47 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: <1248342610.3698.7.camel@krishna-laptop> References: <1248342610.3698.7.camel@krishna-laptop> Message-ID: Krishnakant schrieb: > On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote: >> Being a sudoer is not a privilege to issue the os.setuid system call. It >> is only a permission to use the sudo command. >> > Yes, So I would like to know if python can change the user to some other > non-privileged user during the script execution? If the user running python program is allowed to call setuid() then yes. > >>> K> I tryed using subprocess but that did not help me either. I tryed sudo >>> K> su into the Popen command but it throws me into the terminal (shell) >>> K> with postgres as the user. >> You could execute the command: >> sudo -u postgres required_command >> with subprocess. >> > Ok, but the problem is much more complex. No. > What if I want to do the following. > 1, change the user for a particular script to the postgres user. Did you try running "sudo -u postgres blabla" with subprocess? > 2. now execute the python code for connecting to the postgresql > database. > In the second point I actually want to execute python code not shell > level command so will the sudo -u in the subprocess.Popen change the > user in the script? No, as the name "subprocess" suggests you are spawning a new process which gets another uid through sudo. This does not affect the parent process. hth Paul From piet at cs.uu.nl Thu Jul 23 08:06:14 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 14:06:14 +0200 Subject: Copy/paste through LAN References: Message-ID: >>>>> Jun (J) wrote: >J> Hello, >J> I've a client/server application which uses Pyro to communicate each >J> other. >J> Now i try to implement copy/paste through LAN between server >J> controlled >J> filesystem to my local windows machine (I could list files in client's >J> window). >J> How can i pass the url information through Pyro ? What do you mean? Do you want to copy a file or copy file names? Copying a file between two machines can best be done by a specialized protocol, like HTTP or scp. Pasting a file doesn't make sense IMHO. And URLs are just strings. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Thu Jul 23 08:23:24 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 14:23:24 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. References: Message-ID: >>>>> Krishnakant (K) wrote: >K> On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote: >>> Being a sudoer is not a privilege to issue the os.setuid system call. It >>> is only a permission to use the sudo command. >>> >K> Yes, So I would like to know if python can change the user to some other >K> non-privileged user during the script execution? As I said you can't (unless you are root). It would be a security leak if an arbitrary user could suddenly run as another user. Sudo is the escape mechanism but it runs commands, and is not for changing the uid in the middle of a process. >>> >K> I tryed using subprocess but that did not help me either. I tryed sudo >>> >K> su into the Popen command but it throws me into the terminal (shell) >>> >K> with postgres as the user. >>> >>> You could execute the command: >>> sudo -u postgres required_command >>> with subprocess. >>> >K> Ok, but the problem is much more complex. >K> What if I want to do the following. >K> 1, change the user for a particular script to the postgres user. >K> 2. now execute the python code for connecting to the postgresql >K> database. >K> In the second point I actually want to execute python code not shell >K> level command so will the sudo -u in the subprocess.Popen change the >K> user in the script? You can run another python script as the other user (o even the same python script). You said you tried subprocess. If that is acceptable then running another python script should also be acceptable, becaus eit is basically the same. >K> In short I would just like to have the script run under another user >K> let's say postgres as long as a certain action is going on, for example >K> connecting to the postgresql database. Why would you have to be another user for connecting to a postgres database? The DBMS takes care of the permissions at the DB level. Otherwise you would have to do the DB access in another script. The script could even communicate withe the original script, e.g by pipes or some protocol like XMLRPC. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jyoti.mickey at gmail.com Thu Jul 23 08:30:34 2009 From: jyoti.mickey at gmail.com (Jyoti Sharma) Date: Thu, 23 Jul 2009 18:00:34 +0530 Subject: how two join and arrange two files together References: Message-ID: On Thu, 23 Jul 2009 12:52:15 +0530, wrote: > > Hi, > > I have two large files: > > FileA > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 > 23 ALA H = 8.78 N = CA = HA = C = 179.93................. > > and > > FileB > 21 ALA helix (helix_alpha, helix2) > 23 ALA helix (helix_alpha, helix3) > 38 ALA helix (helix_alpha, helix3)........... > > now what i want that i will make another file in which i will join the > two > file in such a way that only matching entries will come like here 21 and > 23 ALA is in both files, so the output will be something like:- > > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA helix > (helix_alpha, helix2) > 23 ALA H = 8.78 N = CA = HA = C = 179.93|23 ALA helix (helix_alpha, > helix3) > > and further i will make another file in which i will be able to put those > lines form this file based on the missing atom value, like for 21 ALA HA > is not defined so i will put it another file based on its HA missing > value > similarly i will put 23 ALA on another file based on its missing N,CA and > HA value. > > I tried to join the two file based on their matching entries by:--- (snip) I believe there are packages available for doing such things mostly written in perl. But if the aim is not to develop suitable applications but only to obtain the desired formatted file then doing this with the help of something like Excel would be easiest. This is my opinion from the experience I have from my bioinfo programming. Regards, Jyoti From hackingkk at gmail.com Thu Jul 23 08:30:53 2009 From: hackingkk at gmail.com (Krishnakant) Date: Thu, 23 Jul 2009 18:00:53 +0530 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: References: <1248342610.3698.7.camel@krishna-laptop> Message-ID: <1248352253.5100.37.camel@krishna-laptop> On Thu, 2009-07-23 at 13:50 +0200, paul wrote: > If the user running python program is allowed to call setuid() then yes. > NO, i don't think i can do that. I am getting opperation not permitted. Any ways I think probably subprocess will have to sort it out. > Did you try running "sudo -u postgres blabla" with subprocess? > Yes, but still not got the intended result which is now obvious. > > 2. now execute the python code for connecting to the postgresql > > database. > > In the second point I actually want to execute python code not shell > > level command so will the sudo -u in the subprocess.Popen change the > > user in the script? > No, as the name "subprocess" suggests you are spawning a new process > which gets another uid through sudo. This does not affect the parent > process. > Ok then here is the work-around which I am thinking to try, Plese tell me if it is correct. I will let that subprocess start python inthe background and execute the connecting code to postgresql including importing the pygresql library. Then I will create the connection and cursor objcts in that subprocess. But my concern is, will the connection object in the child process (subprocess) be available to the parrent process? happy hacking. Krishnakant. From __peter__ at web.de Thu Jul 23 08:35:37 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 14:35:37 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > i wrote a program which transforms a string of zeroes ando ones into a > png file. > > #!/usr/bin/env python > import Image > import sys > bits_in_a_byte = 8 > raster_string = """\ > 00000000000000000000000000000000 > 00100100111100100000100000111100 > 00100100100000100000100000100100 > 00111100111000100000100000100100 > 00100100100000100000100000100100 > 00100100111100111100111100111100 > 00000000000000000000000000000000 > 00000000000000000000000000000000 > 00000000111100100000111100000000 > 00000000100000100000100100000000 > 00000000100000100000111100000000 > 00000000100000100000100000000000 > 00000000111100111100100000000000 > 00000000000000000000000000000000 > """ > raster_lines = raster_string.splitlines() > high = len(raster_lines) > wide = len(raster_lines[0]) > bytes_in_a_row = wide/bits_in_a_byte > bitmap = "" > for raster_line in raster_lines: > for byte_count in range(bytes_in_a_row): > first_bit = byte_count*bits_in_a_byte > bitmap += > chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) > im = Image.fromstring("1", (wide , high) , bitmap) > im.save(sys.stdout , "PNG") > > any suggestions for improvement? You can simplify the inner loop: for first_bit in range(0, wide, bits_in_a_byte): bitmap += ... and get rid of a few helper variables. Here's a different approach: #!/usr/bin/env python import Image import string import sys mapping = string.maketrans("01", "\x00\xff") raster_string = ... width = raster_string.index("\n") height = raster_string.count("\n") raster_string = raster_string.translate(mapping, "\n") im = Image.fromstring("L", (width, height), raster_string) im.convert("1").save(sys.stdout, "PNG") The idea is to move the bit-twiddling from python to code written in C, pointless for such a tiny picture but crucial for the performance when you want to manipulate larger images. Peter From lists at cheimes.de Thu Jul 23 08:37:07 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 14:37:07 +0200 Subject: Looking for os.listdir() generator Message-ID: Hello, I'm looking for a generator version of os.listdir() for Python 2.5 and newer. I know somebody has worked on it because I've seen a generator version in a posting on some list or blog a while ago. I can't find it anymore. It seems my Google fu is lacking today. All I can find is a very old version of xlistdir. A Cython based solution is appreciated but please no ctypes version. Christian From python.list at tim.thechases.com Thu Jul 23 08:39:46 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 23 Jul 2009 07:39:46 -0500 Subject: sqlite3 performance problems only in python In-Reply-To: <4A684B56.3020906@gmail.com> References: <4A684B56.3020906@gmail.com> Message-ID: <4A685A12.4080800@tim.thechases.com> > until now I used only small / simple databases in Python with sqlite3. > Now I've a large and rather complex database. > > The most simple query (with just a result of 100 rows), > takes about 70 seconds. > And all that time is consumed in "cursor.fetchall" > > Using the same database in Delphi, > using the same query, > takes less than 5 seconds (including displaying the full table in a grid). While it may seem obvious, are you doing anything time-consuming with those results? Or have you tested just doing the fetchall() without doing any further processing? I'm curious on the timing of sql = "..." start = time() cursor.execute(sql) rows = cursor.fetchall() end = time() print end-start with no other processing. I regularly write sql that's fairly complex and brings back somewhat large datasets (sometimes in sqlite), and have never experienced problems with "simple quer[ies] (with just a result of 100 rows" taking such extrordinary times The answer from the above code will help determine whether it's the sqlite portion that's crazy (and might need some well-placed index statements; though if your Delphi code is fine, I suspect not), or if it's your application code that goes off into left field with the resulting data. -tkc From user at example.net Thu Jul 23 08:41:23 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 14:41:23 +0200 Subject: raster (PIL) In-Reply-To: <7cr0v1F28dhltU1@mid.uni-berlin.de> References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <7cr0v1F28dhltU1@mid.uni-berlin.de> Message-ID: <4a685a75$0$40009$4fafbaef@reader3.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: ... >>high = len(raster_lines) >>wide = len(raster_lines[0]) >>bytes_in_a_row = wide/bits_in_a_byte > > > This will give you the wrong result if not divideable by bits_in_a_byte. > then maybe: #!/usr/bin/env python import Image import sys bits_in_a_byte = 8 raster_string = """\ 0000000000000000000000 00100100111100100000100000111100 00100100100000100000100000100100 00111100111000100000100000100100 00100100100000100000100000100100 00100100111100111100111100111100 00000000000000000000000000000000 00000000000000000000000000000000 00000000111100100000111100000000 00000000100000100000100100000000 00000000100000100000111100000000 00000000100000100000100000000000 00000000111100111100100000000000 00000000000000000000000000000000 """ raster_lines = raster_string.splitlines() high = len(raster_lines) wide = len(raster_lines[0]) bytes_in_a_row = wide/bits_in_a_byte wide_for_real = bytes_in_a_row*bits_in_a_byte if wide_for_real: bitmap = "" for raster_line in raster_lines: for byte_count in range(bytes_in_a_row): first_bit = byte_count*bits_in_a_byte bitmap += chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) im = Image.fromstring("1", (wide_for_real , high) , bitmap) im.save(sys.stdout , "PNG") bye From piet at cs.uu.nl Thu Jul 23 08:48:29 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 14:48:29 +0200 Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> On Jul 21, 8:59?pm, Piet van Oostrum wrote: >>> The recipient_public_key.pem file is the public key of the recipient >>> which means the person that is going to receive the encrypted message. >>> You should get it from the recipient him/herself or from some key store >>> where s/he has deposited it. [...] >j> error coming like - IOError: [Errno 2] No such file or directory: >j> 'recipient_public_key.pem' >j> Is this not the inbuilt file. >j> How should i create such type of file. You don't create it. See above. If you understand what is is you know why it can't be builtin. If you don't understand it is better if you first learn about OpenSSL, otherwise you run the risk to make serious errors. If you are just experimenting to create a message for yourself then you have to create the public key because you are then the recipient yourself. That has been answered on http://stackoverflow.com/questions/1169798/m2crypto-package -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From stephen.cuppett at sas.com Thu Jul 23 08:48:44 2009 From: stephen.cuppett at sas.com (Stephen Cuppett) Date: Thu, 23 Jul 2009 08:48:44 -0400 Subject: Predefined Variables References: Message-ID: os.environment('QUERY_STRING') "Fred Atkinson" wrote in message news:p2qg65d0t4kki1sh0t3v6ileamhkvl9hgj at 4ax.com... > Is there a pre-defined variable that returns the GET line > (http://www.php.net/index.php?everythingafterthequestionmark) as a > single variable (rather than individual variables)? > > Regards, > > > > Fred From stephen.cuppett at sas.com Thu Jul 23 08:50:27 2009 From: stephen.cuppett at sas.com (Stephen Cuppett) Date: Thu, 23 Jul 2009 08:50:27 -0400 Subject: Predefined Variables References: Message-ID: $_SERVER['QUERY_STRING']; (if it's PHP) "Fred Atkinson" wrote in message news:p2qg65d0t4kki1sh0t3v6ileamhkvl9hgj at 4ax.com... > Is there a pre-defined variable that returns the GET line > (http://www.php.net/index.php?everythingafterthequestionmark) as a > single variable (rather than individual variables)? > > Regards, > > > > Fred From user at example.net Thu Jul 23 08:58:45 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 14:58:45 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <4a685e86$0$40011$4fafbaef@reader3.news.tin.it> Peter Otten wrote: > superpollo wrote: > > >>i wrote a program which transforms a string of zeroes ando ones into a >>png file. ... >>any suggestions for improvement? ... > Here's a different approach: ... > The idea is to move the bit-twiddling from python to code written in C, > pointless for such a tiny picture but crucial for the performance when you > want to manipulate larger images. very very interesting... you know i come from a lower level language approach (C/Pascal) so i find it difficult (but full of fascination) to adapt to such a different and much simpler way of thinking. anyways, thanks a lot. bye From stef.mientki at gmail.com Thu Jul 23 09:02:45 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 15:02:45 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: <4A685A12.4080800@tim.thechases.com> References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: <4A685F75.10701@gmail.com> Tim Chase wrote: >> until now I used only small / simple databases in Python with sqlite3. >> Now I've a large and rather complex database. >> >> The most simple query (with just a result of 100 rows), >> takes about 70 seconds. >> And all that time is consumed in "cursor.fetchall" >> >> Using the same database in Delphi, >> using the same query, >> takes less than 5 seconds (including displaying the full table in a >> grid). > > While it may seem obvious, are you doing anything time-consuming with > those results? Or have you tested just doing the fetchall() without > doing any further processing? I'm curious on the timing of > > sql = "..." > start = time() > cursor.execute(sql) > rows = cursor.fetchall() > end = time() > print end-start No this is exactly what I did, I timed the execute and fetchall seperatly: execute: 125 msec fetchall: 71000 msec (returning 100 rows and 25 columns) pysqlite: version 2.3.2 btw, I don't know if it's of any importance, the SQL-statement I perform is select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* from OPNAMEN inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID inner join POID_SSID_SCID on ( OPNAMEN.POID = POID_SSID_SCID.POID ) and ( SUBSCHAAL_GEGEVENS.SSID = POID_SSID_SCID.SSID ) inner join SCORES on SCORES.SCID = POID_SSID_SCID.SCID inner join PID_POID on OPNAMEN.POID = PID_POID.POID inner join PATIENT on PATIENT.PID = PID_POID.PID where substr ( lower( NAME) , 1, 6) = 'cis20r' and lower ( NAME_ ) = 'fatigue' and TEST_COUNT in (3,4) and DATETIME > 39814.0 and SCORE < 30 cheers, Stef > > with no other processing. I regularly write sql that's fairly complex > and brings back somewhat large datasets (sometimes in sqlite), and > have never experienced problems with "simple quer[ies] (with just a > result of 100 rows" taking such extrordinary times > > The answer from the above code will help determine whether it's the > sqlite portion that's crazy (and might need some well-placed index > statements; though if your Delphi code is fine, I suspect not), or if > it's your application code that goes off into left field with the > resulting data. > > -tkc > > > > From user at example.net Thu Jul 23 09:02:55 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 15:02:55 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <4a685f81$0$40008$4fafbaef@reader3.news.tin.it> Peter Otten wrote: ... > Here's a different approach: ... > raster_string = ... > > width = raster_string.index("\n") > height = raster_string.count("\n") your approach has a funny side-effect: try to remove just one zero from the first line of the raster ;-) bye From franke.rob at googlemail.com Thu Jul 23 09:03:06 2009 From: franke.rob at googlemail.com (Robert Franke) Date: Thu, 23 Jul 2009 15:03:06 +0200 Subject: Pyserial and pyQt In-Reply-To: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> Message-ID: On Tue, Jul 21, 2009 at 9:37 PM, Seth wrote: > > I have used pyserial in the past but this is my first experience with > pyQt. I am using the Python xy package for windows current but might > move to linux. I have a small device that is outputting a basic text > string. I want to be able to read this string(from the comm port) and > update a text box and eventually a graph in pyQt. I can't find any > documentation or tutorials on how to do this. If anyone can point me > in the right direction or give me some tips I would be grateful. > As I need to do something similar I looked around and found the following recipe at activestate: http://code.activestate.com/recipes/82965/ In the comments there is an example for PyQt. Cheers, Robert -------------- next part -------------- An HTML attachment was scrubbed... URL: From user at example.net Thu Jul 23 09:16:51 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 15:16:51 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> Peter Otten wrote: ... > im.convert("1").save(sys.stdout, "PNG") ... a q about pil: im.convert("1") is different from: im2 = im.convert("1") right? in the former im is changed (the method applies to im) but in the latter im is unchanged (first im is copied unto im2 and then the method is applied to im2)... am i right? bye From dangets at gmail.com Thu Jul 23 09:19:33 2009 From: dangets at gmail.com (DG) Date: Thu, 23 Jul 2009 06:19:33 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: Message-ID: <74cd3059-cd88-418b-8444-766a125d93c3@y4g2000prf.googlegroups.com> On Jul 22, 6:05?pm, "Gabriel Genellina" wrote: > En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James ? > escribi?: > > > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina ? > > wrote: > > >> class X(object): > >> ? ?foo = descriptor() > > >> x = X() > >> x.foo = "value" > > > Isn't this going to create a brand new instance attribute x.foo that has ? > > nothing to do with the descriptor anyway? > > No, it's up to the descriptor __set__ method what happens in this case. ? > Think of the standard 'property' descriptor, the fset function can do ? > whatever it wants. > Also, a data descriptor takes precedence over any instance attribute of ? > the same name that might exist. > > -- > Gabriel Genellin You might've already thought of this (and it is annoying), but you could pass the name through the descriptor's init method. I believe this is the only way besides assigning a metaclass that will look for that type of descriptor upon class creation and set the descriptor's name at that time. class A(object): def __init__(self, attr_name): self._name = attr_name def __set__(self, instance, value): self.instance.__dict__[self._name] = value # or something like that... class B(object): foo = A('foo') From breamoreboy at yahoo.co.uk Thu Jul 23 09:23:27 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 Jul 2009 14:23:27 +0100 Subject: regex: multiple matching for one string In-Reply-To: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: scriptlearner at gmail.com wrote: > For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I > will like to take out the values (valuea, valueb, and valuec). How do > I do that in Python? The group method will only return the matched > part. Thanks. > > p = re.compile('#a=*;b=*;c=*;') > m = p.match(line) > if m: > print m.group(), IMHO a regex for this is overkill, a combination of string methods such as split and find should suffice. Regards. From deets at nospam.web.de Thu Jul 23 09:27:48 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 15:27:48 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <7cr0v1F28dhltU1@mid.uni-berlin.de> <4a685a75$0$40009$4fafbaef@reader3.news.tin.it> Message-ID: <7cr6qkF28oinaU1@mid.uni-berlin.de> superpollo wrote: > Diez B. Roggisch wrote: >> superpollo wrote: > ... >>>high = len(raster_lines) >>>wide = len(raster_lines[0]) >>>bytes_in_a_row = wide/bits_in_a_byte >> >> >> This will give you the wrong result if not divideable by bits_in_a_byte. >> > > then maybe: > > #!/usr/bin/env python > import Image > import sys > bits_in_a_byte = 8 > raster_string = """\ > 0000000000000000000000 > 00100100111100100000100000111100 > 00100100100000100000100000100100 > 00111100111000100000100000100100 > 00100100100000100000100000100100 > 00100100111100111100111100111100 > 00000000000000000000000000000000 > 00000000000000000000000000000000 > 00000000111100100000111100000000 > 00000000100000100000100100000000 > 00000000100000100000111100000000 > 00000000100000100000100000000000 > 00000000111100111100100000000000 > 00000000000000000000000000000000 > """ > raster_lines = raster_string.splitlines() > high = len(raster_lines) > wide = len(raster_lines[0]) > bytes_in_a_row = wide/bits_in_a_byte > wide_for_real = bytes_in_a_row*bits_in_a_byte No. Because that would skip up to 7 bits. Instead, do bytes_in_a_row = wide/bits_in_a_byte if wide % bits_in_a_byte: bytes_in_a_row += 1 Diez From deets at nospam.web.de Thu Jul 23 09:28:50 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 15:28:50 +0200 Subject: Predefined Variables References: Message-ID: <7cr6siF28oinaU2@mid.uni-berlin.de> Fred Atkinson wrote: > Is there a pre-defined variable that returns the GET line > (http://www.php.net/index.php?everythingafterthequestionmark) as a > single variable (rather than individual variables)? Variables don't return things. Functions do. And additionally the answer depends on what and with what you actually do. Diez From paul at subsignal.org Thu Jul 23 09:32:10 2009 From: paul at subsignal.org (paul) Date: Thu, 23 Jul 2009 15:32:10 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: <1248352253.5100.37.camel@krishna-laptop> References: <1248342610.3698.7.camel@krishna-laptop> <1248352253.5100.37.camel@krishna-laptop> Message-ID: Krishnakant schrieb: > On Thu, 2009-07-23 at 13:50 +0200, paul wrote: > >> If the user running python program is allowed to call setuid() then yes. >> > NO, i don't think i can do that. I am getting opperation not permitted. > > Any ways I think probably subprocess will have to sort it out. > >> Did you try running "sudo -u postgres blabla" with subprocess? >> > Yes, but still not got the intended result which is now obvious. Why is that obvious? Works for me: ---- test.py --------- #!/usr/bin/python from subprocess import Popen, PIPE cmd = Popen('sudo -u vboxadd /home/pkoelle/Documents/whoami.sh', shell=True, stdout=PIPE, stderr=PIPE) print "OUT: "+cmd.stdout.read() print "ERR: "+cmd.stderr.read() ---- whoami.sh ----- #!/bin/bash echo $UID logger "whoami script called for $UID" Of course, you need to adapt path and user values to your situation. The user you use in your 'sudo -u ...' call needs execute permissions for whoami.sh. The relevant entry in /etc/sudoers: pkoelle ALL=NOPASSWD: /home/pkoelle/Documents/whoami.sh hth Paul PS: This has absolutely nothing to do with "connecting to postgresql". A "postgres user" is not a "system user" (Piet already asked the right questions here ;) >>> 2. now execute the python code for connecting to the postgresql >>> database. >>> In the second point I actually want to execute python code not shell >>> level command so will the sudo -u in the subprocess.Popen change the >>> user in the script? >> No, as the name "subprocess" suggests you are spawning a new process >> which gets another uid through sudo. This does not affect the parent >> process. >> > Ok then here is the work-around which I am thinking to try, Plese tell > me if it is correct. > I will let that subprocess start python inthe background and execute the > connecting code to postgresql including importing the pygresql library. > Then I will create the connection and cursor objcts in that subprocess. > But my concern is, will the connection object in the child process > (subprocess) be available to the parrent process? > > > happy hacking. > Krishnakant. > > From fatkinson at mishmash.com Thu Jul 23 09:41:30 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Thu, 23 Jul 2009 06:41:30 -0700 Subject: Predefined Variables Message-ID: Is there a pre-defined variable that returns the GET line (http://www.php.net/index.php?everythingafterthequestionmark) as a single variable (rather than individual variables)? Regards, Fred From __peter__ at web.de Thu Jul 23 09:43:36 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 15:43:36 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Peter Otten wrote: > ... >> im.convert("1").save(sys.stdout, "PNG") > ... > > a q about pil: > > im.convert("1") > > is different from: > > im2 = im.convert("1") > > right? > > in the former im is changed (the method applies to im) but in the latter > im is unchanged (first im is copied unto im2 and then the method is > applied to im2)... am i right? No. A method has no clue whether its result is used or discarded. Therefore im.convert("1") creates a new image in the specified mode, too, which is discarded immediately. If you don't need the result you probably shouldn't call the method at all. Peter From user at example.net Thu Jul 23 09:57:03 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 15:57:03 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4a686c30$0$40012$4fafbaef@reader3.news.tin.it> Peter Otten wrote: > superpollo wrote: > > >>Peter Otten wrote: >>... >> >>>im.convert("1").save(sys.stdout, "PNG") >> >>... >> >>a q about pil: >> >>im.convert("1") >> >>is different from: >> >>im2 = im.convert("1") >> >>right? >> >>in the former im is changed (the method applies to im) but in the latter >>im is unchanged (first im is copied unto im2 and then the method is >>applied to im2)... am i right? > > > No. A method has no clue whether its result is used or discarded. Therefore > > im.convert("1") > > creates a new image in the specified mode, too, which is discarded > immediately. but in: im.convert("1").save(sys.stdout, "PNG") the new (anonymous) image created by .convert is not discarded *immediately*, i mean *before* the .save method is called on it, right? bye From paul at boddie.org.uk Thu Jul 23 10:13:02 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Thu, 23 Jul 2009 07:13:02 -0700 (PDT) Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: <1c994086-8c58-488f-b3b3-6161c4b2ba05@k30g2000yqf.googlegroups.com> Message-ID: On 23 Jul, 05:55, a... at pythoncraft.com (Aahz) wrote: > In article <1c994086-8c58-488f-b3b3-6161c4b2b... at k30g2000yqf.googlegroups.com>, > Paul Boddie ? wrote: > > >http://www.boddie.org.uk/python/XSLTools.html > > Thanks! ?I'll take a look after OSCON. The JavaScript parts of the framework are a bit complicated, I'll admit: you have to write some nasty-looking function calls with awkward arguments to send AJAX-style requests to the server. I've been meaning to employ a more declarative "signals and slots" approach so that you don't have to write in the page templates where the in-page updates should be expected: it's really the server code that determines this kind of thing, and so the server code should be able to say where it wants the page to be updated. Again, I'll try and put up some examples in the relatively near future that will make it easier for you to see if it's your kind of thing. Paul From __peter__ at web.de Thu Jul 23 10:19:38 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 16:19:38 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> <4a686c30$0$40012$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Peter Otten wrote: >> superpollo wrote: >> >> >>>Peter Otten wrote: >>>... >>> >>>>im.convert("1").save(sys.stdout, "PNG") >>> >>>... >>> >>>a q about pil: >>> >>>im.convert("1") >>> >>>is different from: >>> >>>im2 = im.convert("1") >>> >>>right? >>> >>>in the former im is changed (the method applies to im) but in the latter >>>im is unchanged (first im is copied unto im2 and then the method is >>>applied to im2)... am i right? >> >> >> No. A method has no clue whether its result is used or discarded. >> Therefore >> >> im.convert("1") >> >> creates a new image in the specified mode, too, which is discarded >> immediately. > > but in: > > im.convert("1").save(sys.stdout, "PNG") > > the new (anonymous) image created by .convert is not discarded > *immediately*, i mean *before* the .save method is called on it, right? Of course. Think of it as a shortcut for tmp = im.convert(...) tmp.save(...) del tmp Peter From dangets at gmail.com Thu Jul 23 10:33:55 2009 From: dangets at gmail.com (DG) Date: Thu, 23 Jul 2009 07:33:55 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: <74cd3059-cd88-418b-8444-766a125d93c3@y4g2000prf.googlegroups.com> Message-ID: On Jul 23, 7:19?am, DG wrote: > On Jul 22, 6:05?pm, "Gabriel Genellina" > wrote: > > > > > En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James ? > > escribi?: > > > > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina ? > > > wrote: > > > >> class X(object): > > >> ? ?foo = descriptor() > > > >> x = X() > > >> x.foo = "value" > > > > Isn't this going to create a brand new instance attribute x.foo that has ? > > > nothing to do with the descriptor anyway? > > > No, it's up to the descriptor __set__ method what happens in this case. ? > > Think of the standard 'property' descriptor, the fset function can do ? > > whatever it wants. > > Also, a data descriptor takes precedence over any instance attribute of ? > > the same name that might exist. > > > -- > > Gabriel Genellin > > You might've already thought of this (and it is annoying), but you > could pass the name through the descriptor's init method. ?I believe > this is the only way besides assigning a metaclass that will look for > that type of descriptor upon class creation and set the descriptor's > name at that time. > > class A(object): > ? ? def __init__(self, attr_name): > ? ? ? ? self._name = attr_name > ? ? def __set__(self, instance, value): > ? ? ? ? self.instance.__dict__[self._name] = value > ? ? ? ? # or something like that... > > class B(object): > ? ? foo = A('foo') Well of course I think of more alternatives after I post. 1) still annoying... pass the class through a 'registering' function that will examine all of it's attrs and find the ones that are of your descriptor type, and assign the attr_name upon that descriptor.name attribute. 2) I just thought of, and it's kind of a hack is to do this examination within the '__set__' method. e.g. class A(object): def __get__(self, instance, owner): if instance is None: return self else: # do something different here for instances' access return self def __set__(self, instance, value): cls = instance.__class__ name = None for attr in dir(cls): if getattr(cls, attr) is self: name = attr print name From amrita at iisermohali.ac.in Thu Jul 23 10:34:15 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Thu, 23 Jul 2009 20:04:15 +0530 (IST) Subject: how two join and arrange two files together In-Reply-To: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> References: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> Message-ID: <20373.210.212.36.65.1248359655.squirrel@www.iisermohali.ac.in> I tried to print those lines having C value missing by: import re expr = re.compile("C = None") f = open("/home/amrita/helix.dat") for line in f: if expr.search(line): print line but it is not giving any value. > > Hi, > > I have two large files: > > FileA > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 > 23 ALA H = 8.78 N = CA = HA = C = 179.93................. > > and > > FileB > 21 ALA helix (helix_alpha, helix2) > 23 ALA helix (helix_alpha, helix3) > 38 ALA helix (helix_alpha, helix3)........... > > now what i want that i will make another file in which i will join the two > file in such a way that only matching entries will come like here 21 and > 23 ALA is in both files, so the output will be something like:- > > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA helix > (helix_alpha, helix2) > 23 ALA H = 8.78 N = CA = HA = C = 179.93|23 ALA helix (helix_alpha, > helix3) > > and further i will make another file in which i will be able to put those > lines form this file based on the missing atom value, like for 21 ALA HA > is not defined so i will put it another file based on its HA missing value > similarly i will put 23 ALA on another file based on its missing N,CA and > HA value. > > I tried to join the two file based on their matching entries by:--- >>>>from collections import defaultdict >>>> >>>> if __name__ == "__main__": > ... a = open("/home/amrita/alachems/chem100.txt") > ... c = open("/home/amrita/secstr/secstr100.txt") > ... >>>> def source(stream): > ... return (line.strip() for line in stream) > ... > ... >>>> def merge(sources): > ... for m in merge([source(a),source(c)]): > ... print "|".join(c.ljust(10) for c in m) > ... > > but it is not giving any value. > > > > > > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From rhodri at wildebst.demon.co.uk Thu Jul 23 10:39:15 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 23 Jul 2009 15:39:15 +0100 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: On Thu, 23 Jul 2009 01:05:55 +0100, Gabriel Genellina wrote: > En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James > escribi?: >> On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina >> wrote: >> >> >>> class X(object): >>> foo = descriptor() >>> >>> x = X() >>> x.foo = "value" >> >> Isn't this going to create a brand new instance attribute x.foo that >> has nothing to do with the descriptor anyway? > > No, it's up to the descriptor __set__ method what happens in this case. > Think of the standard 'property' descriptor, the fset function can do > whatever it wants. If it gets called, that is. > Also, a data descriptor takes precedence over any instance attribute of > the same name that might exist. This was the bit I wasn't clear on. Thanks! -- Rhodri James *-* Wildebeest Herder to the Masses From manu3d at gmail.com Thu Jul 23 10:44:50 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Thu, 23 Jul 2009 07:44:50 -0700 (PDT) Subject: import vs imp and friends. Message-ID: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Greetings, I was looking in the archive of this newsgroup and I found this snippet: import imp sourcecode = 'def foo(x): return 11*x' mod = imp.new_module('foo') exec sourcecode in mod.__dict__ mod.foo(16) Together with similar and sometimes more complete snippets available they show how a module can be created out of string, plain text files and compiled files. Neat! Now the question. Apart from checking sys.module first and eventually adding the new module to it if it isn't there already, and apart from setting __file__, is there anything else that import does and this snippet doesn't? Manu From MLists at romulo.de Thu Jul 23 10:44:56 2009 From: MLists at romulo.de (Rainer Mansfeld) Date: Thu, 23 Jul 2009 16:44:56 +0200 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: <4A687768.2050804@romulo.de> Gabriel Genellina schrieb: > I have a class attribute 'foo' which is a data descriptor. I create an > instance of such class. When I say instance.foo = value, the descriptor > __set__ method is called. Is there any way to obtain the name being > assigned to? ('foo' in this example). That is, I want to know the target > name for the assignment that triggered the __set__ method call. > class descriptor(object): def __get__(self, instance, owner): return self def __set__(self, instance, value): # I want to know the *name* this value is being assigned to for name in instance.__class__.__dict__: if getattr(instance, name) is self: print "assigning to %s" % name break class X(object): foo = descriptor() bar = descriptor() class Y(object): foo = descriptor() baz = descriptor() x = X() y = Y() x.foo = "value" x.bar = "value" y.foo = "value" y.baz = "value" Does this work for you? Rainer From luismgz at gmail.com Thu Jul 23 10:53:48 2009 From: luismgz at gmail.com (Neuruss) Date: Thu, 23 Jul 2009 07:53:48 -0700 (PDT) Subject: ANN: psyco V2 References: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> Message-ID: <649b38ef-fced-457e-953a-ffaf2fd9012e@c29g2000yqd.googlegroups.com> It seems psyco.org is still in the transfer process... Is there any charitable soul with a link to a Windows binary? :-) From lists at cheimes.de Thu Jul 23 11:04:05 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 17:04:05 +0200 Subject: import vs imp and friends. In-Reply-To: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> References: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Message-ID: Emanuele D'Arrigo wrote: > Now the question. Apart from checking sys.module first and eventually > adding the new module to it if it isn't there already, and apart from > setting __file__, is there anything else that import does and this > snippet doesn't? The import statement does several things. For instance it holds the import lock to stop other threads from importing the same module again. It also does lots of additional work for packages like relative imports, checking __path__, setting attributes on parent packages and so on. The import system also does a lot of work in order to find and load a module, too. Christian From lists at cheimes.de Thu Jul 23 11:17:42 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 17:17:42 +0200 Subject: ANN: psyco V2 In-Reply-To: <649b38ef-fced-457e-953a-ffaf2fd9012e@c29g2000yqd.googlegroups.com> References: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> <649b38ef-fced-457e-953a-ffaf2fd9012e@c29g2000yqd.googlegroups.com> Message-ID: <4A687F16.3030501@cheimes.de> Neuruss wrote: > It seems psyco.org is still in the transfer process... > Is there any charitable soul with a link to a Windows binary? :-) It seems like Christian is already working on Windows binaries. We are having a discussing about an obscure MinGW bug on the Python developer list. It looks like MinGW isn't fully ABI compatible with MSVC. Christian From lists at cheimes.de Thu Jul 23 11:22:55 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 17:22:55 +0200 Subject: ANN: psyco V2 In-Reply-To: <4A5FE13A.3080708__12565.7702716582$1247820318$gmane$org@stackless.com> References: <4A5FE13A.3080708__12565.7702716582$1247820318$gmane$org@stackless.com> Message-ID: <4A68804F.4030802@cheimes.de> Christian Tismer wrote: > Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, > and Mac OS X. Psyco is not supporting 64 bit, yet. But it > is well being considered. Can you estimate how much work needs to be done in order to get Psyco working on 64bit POSIX (Linux) systems? Christian From Aaron.Dushku at ma.usda.gov Thu Jul 23 11:27:34 2009 From: Aaron.Dushku at ma.usda.gov (Dushku, Aaron - Amherst, MA) Date: Thu, 23 Jul 2009 10:27:34 -0500 Subject: Office COM automatisation - calling python from VBA Message-ID: <7B11445876E166488E821C88E5A18778032F136CA3@mostlouis7s302.agent.one.usda.gov> I'd like a copy of that code. Thanks for taking the time for all of us. Sincerely, Aaron Dushku ****************************** Aaron Dushku GIS Specialist USDA-NRCS Amherst, Massachusetts (413) 253-4379 Email: aaron.dushku at ma.usda.gov -------------- next part -------------- An HTML attachment was scrubbed... URL: From drakonik at gmail.com Thu Jul 23 11:29:01 2009 From: drakonik at gmail.com (Nick Dumas) Date: Thu, 23 Jul 2009 11:29:01 -0400 Subject: regex: multiple matching for one string In-Reply-To: References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Agreed. Two string.split()s, first at the semi-colon and then at the equal sign, will yield you your value, without having to fool around with regexes. On 7/23/2009 9:23 AM, Mark Lawrence wrote: > scriptlearner at gmail.com wrote: >> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >> will like to take out the values (valuea, valueb, and valuec). How do >> I do that in Python? The group method will only return the matched >> part. Thanks. >> >> p = re.compile('#a=*;b=*;c=*;') >> m = p.match(line) >> if m: >> print m.group(), > > IMHO a regex for this is overkill, a combination of string methods such > as split and find should suffice. > > Regards. > -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkpogb0ACgkQLMI5fndAv9jtOwCgj3+YOLfKGvAdyCMOhh4NGgfy x5YAn1ydhUYxGlvC4Z4WlWKaa1gwviSh =jnp1 -----END PGP SIGNATURE----- From leerisq at gmail.com Thu Jul 23 11:33:39 2009 From: leerisq at gmail.com (LeeRisq) Date: Thu, 23 Jul 2009 08:33:39 -0700 (PDT) Subject: win32clipboard operation Message-ID: Hi all, Newbie question. I've written a script that outputs to a text file. Now, I just want to copy the content to win32clipboard for access to other applications. Here's the mess I've come up with so far:) import xlrd import win32clipboard def program_uno(): ofile = open(r"C:\Query\DQL.txt", "w") book = xlrd.open_workbook("C:\DocLoader\MCL_Drawing and Legacy Docloader Sheet.xls") sh = book.sheet_by_index(0) e = sh.cell_value(1, 0) a = sh.col_values(0, start_rowx=2, end_rowx=200) b = r'%' + e c = r'%Draft%' y = r"some text..." %(b, c) w = r"some more text..." ofile.writelines(y) for x in a: d = r'%' + x z = r" loop text..." %(d, c) f = ofile.writelines(z) ofile.writelines(w) def copy_text(): ifile = open(r"C:\Query\DQL.txt", "r") win32clipboard.OpenClipboard(0) win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText() win32clipboard.CloseClipboard program_uno() copy_text() From vinay_sajip at yahoo.co.uk Thu Jul 23 11:39:50 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Thu, 23 Jul 2009 08:39:50 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> Message-ID: <716ee910-452b-4872-b4eb-5f280344c990@n11g2000yqb.googlegroups.com> On Jul 23, 4:06 am, Naoki INADA wrote: > In document stdtypes.html#file.encoding>: > > >> The encoding that this file uses. When Unicode strings are written to a file, > >> they will be converted to byte strings using this encoding. In addition, > >> when the file is connected to a terminal, the attribute gives the encoding > >> that the terminal is likely to use > > But inlogging.StreamHandler.emit() :: > > try: > if (isinstance(msg, unicode) and > getattr(stream, 'encoding', None)): > #fs = fs.decode(stream.encoding) > try: > stream.write(fs % msg) > except UnicodeEncodeError: > #Printing to terminals sometimes fails. > For example, > #with an encoding of 'cp1251', the above > write will > #work if written to a stream opened or > wrapped by > #the codecs module, but fail when writing > to a > #terminal even when the codepage is set to > cp1251. > #An extra encoding step seems to be > needed. > stream.write((fs % msg).encode > (stream.encoding)) > else: > stream.write(fs % msg) > except UnicodeError: > stream.write(fs % msg.encode("UTF-8")) > > And behavior of sys.stdout in Windows::>>> import sys > >>> sys.stdout.encoding > 'cp932' > >>> u = u"???" > >>> u > > u'\u3042\u3044\u3046'>>> print >>sys.stdout, u > ??? > >>> sys.stderr.write(u) > > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 0-2: ordinal not in range(128) > > What is file.encoding convention? > If I want to write a unicode string to a file(-like) that have > encoding attribute, I should do > (1) try: file.write(unicode_str), > (2) except UnicodeEncodeError: file.write(unicode_str.encode > (file.encoding)) > likelogging? > It seems agly. If you are writing a Unicode string to a stream which has been opened with e.g. codecs.open with a specific encoding, then the stream is actually a wrapper. You can write Unicode strings directly to it, and the wrapper stream will encode the Unicode to bytes using the specific encoding and write those bytes to the underlyting stream. In your example you didn't show sys.stderr.encoding - you showed sys.stdout.encoding and printed out something to it which seemed to give the correct result, but then wrote to sys.stderr which gave a UnicodeEncodeError. What is the encoding of sys.stderr in your example? Also note that logging had to handle what appeared to be an oddity with terminals - they (at least sometimes) have an encoding attribute but appear to expect to have bytes written to them, and not Unicode. Hence the logging kludge, which should not be needed and so has been carefully commented. Regards, Vinay Sajip From Bill at SynectixLtd.com Thu Jul 23 11:44:50 2009 From: Bill at SynectixLtd.com (Bill Davy) Date: Thu, 23 Jul 2009 16:44:50 +0100 Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: <7creq2F29npujU1@mid.individual.net> "Mark Lawrence" wrote in message news:mailman.3588.1248355389.8015.python-list at python.org... > scriptlearner at gmail.com wrote: >> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >> will like to take out the values (valuea, valueb, and valuec). How do >> I do that in Python? The group method will only return the matched >> part. Thanks. >> >> p = re.compile('#a=*;b=*;c=*;') >> m = p.match(line) >> if m: >> print m.group(), > > IMHO a regex for this is overkill, a combination of string methods such as > split and find should suffice. > > Regards. > For the OP, it can be done with regex by grouping: p = re.compile(r'#a=(*);b=(*);c=(*);') m = p.match(line) if m: print m.group(1), m.group(1) has valuea in it, etc. But this may not be the best way, but it is reasonably terse. From icebergwtf at gmail.com Thu Jul 23 11:46:04 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Thu, 23 Jul 2009 23:46:04 +0800 Subject: extract c/cpp include file with regular expression Message-ID: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> Hi all! I need to parse c/cpp source files, one requirement is to extract included header file name. here is my solution: >>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")') >>> m = re.search(p, '#include ') >>> m.group(3) 'header.h' >>> m = re.search(p, '#include "header.h"') >>> m.group(3) 'header.h' >>> m = re.search(p, '#include >> print(m) None >>> m = re.search(p, '#include "header.h>') >>> print(m) None Pretty ugly! And I know for a valid c/cpp source file, it will be not necessary to check and match '<' with '>' and " with ", but I'm wondering to see more elegant way to do such thing. tiefeng wu 2009-07-23 From victorsubervi at gmail.com Thu Jul 23 11:46:16 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Thu, 23 Jul 2009 12:46:16 -0300 Subject: Cannot Get Form To Work Message-ID: <4dc0cfea0907230846g4f593219t308c64eec4943f93@mail.gmail.com> Hi: When the form comes up the first time, there is the default value for num. When I fill in a number in the form and press send, even though the form sends to itself (same page name), I would think it would read the number sent. Here again is the code: from primeNumbers import primeNumbers try: lang = form.getfirst('lang', 'en') browser = form.getfirst('browser', 'all') site = form.getfirst('site', 'bridge') num = form.getfirst('num','') except: pass ourFile = string.split(__file__, "/") p = ourFile[len(ourFile) - 1] p = p[: - 9] site = ourFile[4][:-10] if site != '': site = site[:-1] print "Content-Type: text/html" print print """ """ if num != '': num = round(float(num)) roots = primeNumbers(num) print roots if num == '': print """
""" print '\n' -------------- next part -------------- An HTML attachment was scrubbed... URL: From dr.mtarver at ukonline.co.uk Thu Jul 23 11:48:46 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Thu, 23 Jul 2009 08:48:46 -0700 (PDT) Subject: strange python scripting error Message-ID: I have a very strange error. I have two test python files test.py and python.py which contain the following code #!/usr/bin/python print "Content-type: text/html" print print "" print "
Hello, Linux.com!
" print "" One file (test.py) works; you call it up and it shows a web page with Hello, Linux.com The other fails with a server configuration error. Both are running under Linux, same server, same permissions. Running a character scan shows that both files contain the same printable characters and are therefore typographically identical. They are absolutely the same. The only hint at a difference I can see is that my ftp program says the files are of unequal lengths. test.py is 129 bytes long. python.py 134 bytes long. A zipped folder containing both files is at www.lambdassociates.org/weird.zip Any ideas welcome. Mark From icebergwtf at gmail.com Thu Jul 23 11:54:01 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Thu, 23 Jul 2009 23:54:01 +0800 Subject: regex: multiple matching for one string In-Reply-To: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: <4314c1f70907230854qcb85dc1vde5fede76b0c80ff@mail.gmail.com> 2009/7/23 scriptlearner at gmail.com : > For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I > will like to take out the values (valuea, valueb, and valuec). ?How do > I do that in Python? ?The group method will only return the matched > part. ?Thanks. > > p = re.compile('#a=*;b=*;c=*;') > m = p.match(line) > ? ? ? ?if m: > ? ? ? ? ? ? print m.group(), > -- > http://mail.python.org/mailman/listinfo/python-list > maybe like this: >>> p = re.compile(r'#?\w+=(\w+);') >>> l = re.findall(p, '#a=valuea;b=valueb;c=valuec;') >>> for r in l: print(r) ... valuea valueb valuec tiefeng wu 2009-07-23 From deets at nospam.web.de Thu Jul 23 11:57:44 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 17:57:44 +0200 Subject: strange python scripting error References: Message-ID: <7crfjoF29e4gtU1@mid.uni-berlin.de> Mark Tarver wrote: > I have a very strange error. I have two test python files test.py and > python.py which contain the following code > > #!/usr/bin/python > print "Content-type: text/html" > print > print "" > print "
Hello, Linux.com!
" > print "" > > One file (test.py) works; you call it up and it shows a web page with > > Hello, Linux.com > > The other fails with a server configuration error. Both are running > under Linux, same server, same permissions. Running a character scan > shows that both files contain the same printable characters and are > therefore typographically identical. They are absolutely the same. > > The only hint at a difference I can see is that my ftp program says > the files are of unequal lengths. test.py is 129 bytes long. > python.py 134 bytes long. > > A zipped folder containing both files is at > > www.lambdassociates.org/weird.zip > > Any ideas welcome. They have different line-ending-conventions. Not sure if and why that makes a difference. Diez From philip at semanchuk.com Thu Jul 23 11:58:13 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 11:58:13 -0400 Subject: extract c/cpp include file with regular expression In-Reply-To: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> Message-ID: On Jul 23, 2009, at 11:46 AM, tiefeng wu wrote: > Hi all! > I need to parse c/cpp source files, one requirement is to extract > included header file name. > here is my solution: >>>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")') >>>> m = re.search(p, '#include ') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include "header.h"') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include >>> print(m) > None >>>> m = re.search(p, '#include "header.h>') >>>> print(m) > None > > Pretty ugly! And I know for a valid c/cpp source file, it will be not > necessary to check and match '<' with '>' and " with ", > but I'm wondering to see more elegant way to do such thing. Hi tiefeng, Regexes are always a little ugly IMO. =) A side note -- does your parser need to handle /* comments like this one*/? If so, then regular expressions are not going be sufficient. Good luck Philip From python at mrabarnett.plus.com Thu Jul 23 12:05:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 17:05:09 +0100 Subject: win32clipboard operation In-Reply-To: References: Message-ID: <4A688A35.3080107@mrabarnett.plus.com> LeeRisq wrote: > Hi all, > > Newbie question. I've written a script that outputs to a text file. > > Now, I just want to copy the content to win32clipboard for access to > other applications. Here's the mess I've come up with so far:) > [snip] def copy_text(): ifile = open(r"C:\Query\DQL.txt", "r") text = ifile.read() ifile.close() win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(text) win32clipboard.CloseClipboard() From dstanek at dstanek.com Thu Jul 23 12:09:55 2009 From: dstanek at dstanek.com (David Stanek) Date: Thu, 23 Jul 2009 12:09:55 -0400 Subject: sqlite3 performance problems only in python In-Reply-To: <4A685F75.10701@gmail.com> References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote: > > btw, I don't know if it's of any importance, the SQL-statement I perform is > select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* > ?from OPNAMEN > ? inner join POID_VLID ? ? ? ? ?on OPNAMEN.POID ? ? ? ? ? ?= POID_VLID.POID > ? inner join VRAAGLST ? ? ? ? ? on VRAAGLST.VLID ? ? ? ? ? = POID_VLID.VLID > ? inner join VLID_SSID ? ? ? ? ?on VRAAGLST.VLID ? ? ? ? ? = VLID_SSID.VLID > ? inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID > ? inner join POID_SSID_SCID ? ? on ( OPNAMEN.POID ? ? ? ? ? ?= > POID_SSID_SCID.POID ) and > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?( SUBSCHAAL_GEGEVENS.SSID = > POID_SSID_SCID.SSID ) > ? inner join SCORES ? ? ? ? ? ? on SCORES.SCID ? ? ? ? ? ? = > POID_SSID_SCID.SCID > ? inner join PID_POID ? ? ? ? ? on OPNAMEN.POID ? ? ? ? ? ?= PID_POID.POID > ? inner join PATIENT ? ? ? ? ? ?on PATIENT.PID ? ? ? ? ? ? = PID_POID.PID > ?where substr ( lower( NAME) , 1, 6) ?= 'cis20r' > ? and lower ( NAME_ ) = 'fatigue' > ? and TEST_COUNT in (3,4) > ? and DATETIME > 39814.0 > ? and SCORE < 30 Warning: I suck at SQL and hate it with a passion... By using lower() on the left side of the where expressions I believe that you are table scanning. So it is not the size of the data returned, but the size of the data that needs to be scanned. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From R.Brodie at rl.ac.uk Thu Jul 23 12:11:21 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Thu, 23 Jul 2009 17:11:21 +0100 Subject: strange python scripting error References: <7crfjoF29e4gtU1@mid.uni-berlin.de> Message-ID: "Diez B. Roggisch" wrote in message news:7crfjoF29e4gtU1 at mid.uni-berlin.de... > They have different line-ending-conventions. Not sure if and why that makes > a difference. Depends on your setup. Shells can be a bit dumb about it, so it will likely break simple cgi-style hosting. -bash: ./python.py: /usr/bin/python^M: bad interpreter: From python at mrabarnett.plus.com Thu Jul 23 12:13:02 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 17:13:02 +0100 Subject: extract c/cpp include file with regular expression In-Reply-To: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> Message-ID: <4A688C0E.6040705@mrabarnett.plus.com> tiefeng wu wrote: > Hi all! > I need to parse c/cpp source files, one requirement is to extract > included header file name. > here is my solution: >>>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")') >>>> m = re.search(p, '#include ') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include "header.h"') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include >>> print(m) > None >>>> m = re.search(p, '#include "header.h>') >>>> print(m) > None > > Pretty ugly! And I know for a valid c/cpp source file, it will be not > necessary to check and match '<' with '>' and " with ", > but I'm wondering to see more elegant way to do such thing. > I'd probably do: >>> p = re.compile(r'#\s*include\s+(?:<([^>]*)>|"([^"]*)")') >>> m = p.search('#include ') >>> m.group(1) or m.group(2) 'header.h' From __peter__ at web.de Thu Jul 23 12:14:03 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 18:14:03 +0200 Subject: strange python scripting error References: <7crfjoF29e4gtU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > Mark Tarver wrote: > >> I have a very strange error. I have two test python files test.py and >> python.py which contain the following code >> >> #!/usr/bin/python >> print "Content-type: text/html" >> print >> print "" >> print "
Hello, Linux.com!
" >> print "" >> >> One file (test.py) works; you call it up and it shows a web page with >> >> Hello, Linux.com >> >> The other fails with a server configuration error. Both are running >> under Linux, same server, same permissions. Running a character scan >> shows that both files contain the same printable characters and are >> therefore typographically identical. They are absolutely the same. >> >> The only hint at a difference I can see is that my ftp program says >> the files are of unequal lengths. test.py is 129 bytes long. >> python.py 134 bytes long. >> >> A zipped folder containing both files is at >> >> www.lambdassociates.org/weird.zip >> >> Any ideas welcome. > > They have different line-ending-conventions. Not sure if and why that > makes a difference. Looks like the shell treats the CR as part of the interpreter name: $ cat python.py #!/usr/bin/python print "Content-type: text/html" print print "" print "
Hello, Linux.com!
" print ""$ $ python python.py Content-type: text/html
Hello, Linux.com!
$ chmod u+x python.py $ ./python.py bash: ./python.py: /usr/bin/python^M: bad interpreter: No such file or directory Peter From dangets at gmail.com Thu Jul 23 12:21:15 2009 From: dangets at gmail.com (DG) Date: Thu, 23 Jul 2009 09:21:15 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: Message-ID: On Jul 23, 8:44?am, Rainer Mansfeld wrote: > Gabriel Genellina schrieb: > > > I have a class attribute 'foo' which is a data descriptor. I create an > > instance of such class. When I say instance.foo = value, the descriptor > > __set__ method is called. Is there any way to obtain the name being > > assigned to? ('foo' in this example). That is, I want to know the target > > name for the assignment that triggered the __set__ method call. > > class descriptor(object): > ? ? ?def __get__(self, instance, owner): > ? ? ? ?return self > > ? ? ?def __set__(self, instance, value): > ? ? ? ? ?# I want to know the *name* this value is being assigned to > ? ? ? ? ?for name in instance.__class__.__dict__: > ? ? ? ? ? ? ?if getattr(instance, name) is self: > ? ? ? ? ? ? ? ? ?print "assigning to %s" % name > ? ? ? ? ? ? ? ? ?break > > class X(object): > ? ? ?foo = descriptor() > ? ? ?bar = descriptor() > > class Y(object): > ? ? ?foo = descriptor() > ? ? ?baz = descriptor() > > x = X() > y = Y() > > x.foo = "value" > x.bar = "value" > y.foo = "value" > y.baz = "value" > > Does this work for you? > > ? ? Rainer The reason I wasn't checking the class' '__dict__' attribute in my solution was because this won't show any descriptors that were inherited from base classes. Example with some optimizations below (sorry for the long code): builtin_methods = dir(object) class descriptor(object): def __init__(self): self.name = None def __get__(self, instance, owner): # if you want a 'useful' data descriptor only return self upon # non-instance access if instance is None: return self else: # do something besides the below for the usefulness return self def __set__(self, instance, value): if self.name is None: cls = instance.__class__ dir_attrs = [m for m in dir(cls) if m not in builtin_methods] # 'foo' is printed here print 'dir(cls): ', dir_attrs # 'foo' is not printed here print 'cls.__dict__:', cls.__dict__ for name in dir_attrs: if getattr(cls, name) is self: self.name = name print "setting %s to %s" % (self.name, value) class baseX(object): foo = descriptor() class X(baseX): pass x = X() x.foo = 'bar' From icebergwtf at gmail.com Thu Jul 23 12:21:41 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Fri, 24 Jul 2009 00:21:41 +0800 Subject: extract c/cpp include file with regular expression In-Reply-To: <4A688C0E.6040705@mrabarnett.plus.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> <4A688C0E.6040705@mrabarnett.plus.com> Message-ID: <4314c1f70907230921j2a5eacd0n1fdac763765b922f@mail.gmail.com> MRAB wrote: > I'd probably do: > >>>> p = re.compile(r'#\s*include\s+(?:<([^>]*)>|"([^"]*)")') >>>> m = p.search('#include ') >>>> m.group(1) or m.group(2) > 'header.h' > yes, it's easier to understand. thanks, MRAB! I always make things complicated :P tiefeng wu 2009-07-23 From icebergwtf at gmail.com Thu Jul 23 12:36:53 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Fri, 24 Jul 2009 00:36:53 +0800 Subject: extract c/cpp include file with regular expression In-Reply-To: <35D1D3C1-ECC3-4FEC-A21E-C6A17D9E22D7@semanchuk.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> <4314c1f70907230913t76567a7cm3b3e4649f5bc6e4e@mail.gmail.com> <35D1D3C1-ECC3-4FEC-A21E-C6A17D9E22D7@semanchuk.com> Message-ID: <4314c1f70907230936w51881daaja30e1abf73742ad2@mail.gmail.com> 2009/7/24 Philip Semanchuk : > > I know this will sound like a sarcastic comment, but it is sincere: my > suggestion is that if you want to parse C/C++ (or Python, or Perl, or > Fortran, etc.), use a real parser, not regexes unless you're willing to > sacrifice some accuracy. Sooner or later you'll come across some code that > your regexes won't handle, like this -- > > #ifdef FOO_BAR > #include > /* #else */ > #include > #endif > > > Parsing code is difficult... > I understand your point, thanks for your suggestion, Philip. And I've met the problem like in your example The reason I choose regex because I barely know about "real parser", for me it still in some "dark area" :) But I'll find something to learn. tiefeng wu 2009-07-23 From tismer at stackless.com Thu Jul 23 12:41:36 2009 From: tismer at stackless.com (Christian Tismer) Date: Thu, 23 Jul 2009 09:41:36 -0700 Subject: ANN: psyco V2 In-Reply-To: <4A68804F.4030802@cheimes.de> References: <4A5FE13A.3080708__12565.7702716582$1247820318$gmane$org@stackless.com> <4A68804F.4030802@cheimes.de> Message-ID: <4A6892C0.40104@stackless.com> On 7/23/09 8:22 AM, Christian Heimes wrote: > Christian Tismer wrote: >> Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, >> and Mac OS X. Psyco is not supporting 64 bit, yet. But it >> is well being considered. > > Can you estimate how much work needs to be done in order to get Psyco > working on 64bit POSIX (Linux) systems? This is not easy to tell. I'm in the process of estimating this, because my sponsor wants to know as well. They are very interested, but it has to be somehow affordable in time and money. There are different paths that can be taken. Simply hacking away, trying to go straight to 64 bit is obvious, but probably a bad approach. Half of the system needs to be rewritten and augmented with extra size info, and this goes very deep. I think this way I would produce a nightmare of even more complicated code, and would kill myself debugging-wise. I believe I need to simplify psyco and make many parts more abstract and more general, to become able to make it flexible. This also means slowing the compiler down quite a lot. Slowing it down will again become no problem, when my new compiler strategy is ready. The number of compilations will reduce so drastically, that the slowdown is neglectible. Yes, I did not give an answer. I have the vague feeling of three months full-time work. My problem right now is to ensure that it will become less and not more :-) cheers - chris -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From ethan at stoneleaf.us Thu Jul 23 12:42:17 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 23 Jul 2009 09:42:17 -0700 Subject: Changing the private variables content In-Reply-To: <4A679415.90407@stoneleaf.us> References: <4A671BAF.1060204@gmail.com> <4A679415.90407@stoneleaf.us> Message-ID: <4A6892E9.20602@stoneleaf.us> Or, in other words, what Steven D'Aprano had already said. Guess I should read the whole thread before madly posting! :) ~Ethan~ From mal at egenix.com Thu Jul 23 12:56:24 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Thu, 23 Jul 2009 18:56:24 +0200 Subject: Balanced binary tree implementation In-Reply-To: <4A662E37.6090406@gmail.com> References: <4A662E37.6090406@gmail.com> Message-ID: <4A689638.6050303@egenix.com> Lucas P Melo wrote: > Hello, > > I would like to use a balanced binary tree implementation (preferably > within some API). > Any hints about where I could find it? > > I am looking for something that implements insertion, deletion, search > and a special search that returns the lesser element bigger than a given > key [1]. > > A nice possibility would be an extensible API that allows me to inherit > its classes and to add operations myself. > > Thanks in advance. > > [1] Ex: 1 2 3 4 5 6 are elements of the bbt. If I use this operation > given 4 as the parameter, the value returned would be 5. You might want to have a look at the btree implementation we have in mxBeeBase: http://www.egenix.com/products/python/mxBase/mxBeeBase/ It's written in C and optimized for on-disk operations. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 23 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From philip at semanchuk.com Thu Jul 23 13:01:45 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 13:01:45 -0400 Subject: extract c/cpp include file with regular expression In-Reply-To: <4314c1f70907230936w51881daaja30e1abf73742ad2@mail.gmail.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> <4314c1f70907230913t76567a7cm3b3e4649f5bc6e4e@mail.gmail.com> <35D1D3C1-ECC3-4FEC-A21E-C6A17D9E22D7@semanchuk.com> <4314c1f70907230936w51881daaja30e1abf73742ad2@mail.gmail.com> Message-ID: <6835BCF7-0B3C-4E5D-B737-57B288D786D3@semanchuk.com> On Jul 23, 2009, at 12:36 PM, tiefeng wu wrote: > 2009/7/24 Philip Semanchuk : >> >> I know this will sound like a sarcastic comment, but it is sincere: >> my >> suggestion is that if you want to parse C/C++ (or Python, or Perl, or >> Fortran, etc.), use a real parser, not regexes unless you're >> willing to >> sacrifice some accuracy. Sooner or later you'll come across some >> code that >> your regexes won't handle, like this -- >> >> #ifdef FOO_BAR >> #include >> /* #else */ >> #include >> #endif >> >> >> Parsing code is difficult... >> > I understand your point, thanks for your suggestion, Philip. And I've > met the problem like in your example > The reason I choose regex because I barely know about "real parser", > for me it still in some "dark area" :) > But I'll find something to learn. Yes! Learning is always good. And as I said, if you don't mind missing some unusual cases, regexes are fine. I don't know how accurate you want your results to be. As for real parsers, there's lots of them out there, although they may be overkill for what you want to do. Here's one written entirely in Python: http://www.dabeaz.com/ply/ Whatever you choose, good luck with it. Cheers Philip From robert.kern at gmail.com Thu Jul 23 13:03:03 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 23 Jul 2009 12:03:03 -0500 Subject: PyQt GUI In-Reply-To: <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> Message-ID: On 2009-07-23 03:55, Helvin wrote: > I believe I now have vtkpython.exe. However, my 'import vtk' statement > in my python code is not working. The error says something like "no > module named vtk". > Where do I find modules for vtk in pyqt? Do they exist? There are no VTK modules in PyQt itself. The PyQt support is in the vtk package which can be built with VTK itself. You will need to install VTK and the vtk package correctly in order to achieve this. vtkpython.exe is not going to help you. Ignore it. After you have built VTK, you need to do an extra step to install the vtk package to where your Python interpreter will be able to find it. Let's say that your build directory is c:\vtkbuild. cd \vtkbuild\Wrapping\Python python setup.py install cd \ Now you should be able to import vtk from your normal Python interpreter. If you are still having problems, you will need to copy-and-paste exactly what you did and what error messages you got. Do not paraphrase error messages. -- 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 dwbear75 at gmail.com Thu Jul 23 13:06:35 2009 From: dwbear75 at gmail.com (DwBear75) Date: Thu, 23 Jul 2009 10:06:35 -0700 (PDT) Subject: installing 2.6 on vista64 Message-ID: I just downloaded and attempted to install python 2.6.2. The installer proceeds to do its work then dies, leaving an entry in the eventlog: Windows Installer installed the product. Product Name: Python 2.6.2. Product Version: 2.6.2150. Product Language: 1033. Installation success or error status: 1602. Googling for this I wasn't able to narrow the results down to something usable. Anyone know of issues and how to fix them installing on vista 64 (yes, I have 8 gigs of ram) From tismer at stackless.com Thu Jul 23 13:06:43 2009 From: tismer at stackless.com (Christian Tismer) Date: Thu, 23 Jul 2009 10:06:43 -0700 Subject: ANN: psyco V2 In-Reply-To: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> Message-ID: <4A6898A3.3060609@stackless.com> On 7/17/09 4:11 AM, Bearophile wrote: > Very good, thank you. I'll try it when I can. > > Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow? Psyco3: nice typo! :-) Well, I haven't so far found a new idea there that I'd want to borrow and did not know from PyPy, before. Wasn't the project plan saying the opposite, borrowing some ideas from psyco? :-) http://code.google.com/p/unladen-swallow/wiki/ProjectPlan > The problem I have with Psyco1.6 is that you can't use the normal > profilers to know how much seconds of running time is taken by each > function/method of your code. Yes, the profiler hooks are not useful with psyco. You need to write extra functions for timing, as we do in the benchmark directory. > Psyco1.6 has a profile() function, but I am not much able to use it > yet. The profile() function is used for profile driven compilation, as opposed to psyco.full(). This will go away, pretty soon. Psyco will only be switched on or off. Maybe I will add an option for profiling the compiled code. Interesting idea! cheers - chris -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From leerisq at gmail.com Thu Jul 23 13:07:05 2009 From: leerisq at gmail.com (LeeRisq) Date: Thu, 23 Jul 2009 10:07:05 -0700 (PDT) Subject: win32clipboard operation References: Message-ID: On Jul 23, 9:05?am, MRAB wrote: > LeeRisq wrote: > > Hi all, > > > Newbie question. I've written a script that outputs to a text file. > > > Now, I just want to copy the content to win32clipboard for access to > > other applications. Here's the mess I've come up with so far:) > > [snip] > > def copy_text(): > ? ? ?ifile = open(r"C:\Query\DQL.txt", "r") > ? ? ?text = ifile.read() > ? ? ?ifile.close() > > ? ? ?win32clipboard.OpenClipboard() > ? ? ?win32clipboard.EmptyClipboard() > ? ? ?win32clipboard.SetClipboardText(text) > ? ? ?win32clipboard.CloseClipboard() I've actually tried this configuration, but I did it again just to be sure. The program executes without exception, but the text still isn't copied to the clipboard. Any ideas? From robert.kern at gmail.com Thu Jul 23 13:09:55 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 23 Jul 2009 12:09:55 -0500 Subject: import vs imp and friends. In-Reply-To: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> References: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Message-ID: On 2009-07-23 09:44, Emanuele D'Arrigo wrote: > Greetings, > > I was looking in the archive of this newsgroup and I found this > snippet: > > import imp > sourcecode = 'def foo(x): return 11*x' > mod = imp.new_module('foo') > exec sourcecode in mod.__dict__ > mod.foo(16) > > Together with similar and sometimes more complete snippets available > they show how a module can be created out of string, plain text files > and compiled files. Neat! > > Now the question. Apart from checking sys.module first and eventually > adding the new module to it if it isn't there already, and apart from > setting __file__, is there anything else that import does and this > snippet doesn't? Brett Cannon has a good presentation that covers basically the entirety of the import mechanism: http://us.pycon.org/2008/conference/schedule/event/12/ -- 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 igouy2 at yahoo.com Thu Jul 23 13:15:00 2009 From: igouy2 at yahoo.com (Isaac Gouy) Date: Thu, 23 Jul 2009 10:15:00 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> On Jul 21, 10:09?pm, Raffael Cavallaro wrote: > On 2009-07-21 19:06:02 -0400, Neil Hodgson > said: > > > ? ?Python uses native threads. > > So it can be teh-slowness on all ur cores! > > > > The global interpreter lock doesn't help much either. As you've linked to programs that /have not/ been written to use threading or multiple cores (look at the "~ CPU Load" column) I get the feeling I'm missing the joke? From breamoreboy at yahoo.co.uk Thu Jul 23 13:21:59 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 Jul 2009 18:21:59 +0100 Subject: Help understanding the decisions *behind* python? In-Reply-To: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: Phillip B Oldham wrote: > My colleagues and I have been working with python for around 6 months > now, and while we love a lot of what python has done for us and what > it enables us to do some of the decisions behind such certain > data-types and their related methods baffle us slightly (when compared > to the decisions made in other, similarly powerful languages). > > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > [rest of original snipped as already discussed] Sorry if this has been discussed and I've missed it, but how about memory allocation. An immutable tuple has a fixed memory allocation whereas that for the mutable list must be liable to change. You might like to look at the recent thread on this ng 'List insertion cost' and follow the links to Raymond Hettinger's power point presentation. Kindest regards. Mark Lawrence. From piet at cs.uu.nl Thu Jul 23 13:25:48 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 19:25:48 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. References: <1248342610.3698.7.camel@krishna-laptop> Message-ID: >>>>> Krishnakant (K) wrote: >K> On Thu, 2009-07-23 at 13:50 +0200, paul wrote: >>> If the user running python program is allowed to call setuid() then yes. >>> >K> NO, i don't think i can do that. I am getting opperation not permitted. >K> Any ways I think probably subprocess will have to sort it out. >>> Did you try running "sudo -u postgres blabla" with subprocess? >>> >K> Yes, but still not got the intended result which is now obvious. >>> > 2. now execute the python code for connecting to the postgresql >>> > database. >>> > In the second point I actually want to execute python code not shell >>> > level command so will the sudo -u in the subprocess.Popen change the >>> > user in the script? >>> No, as the name "subprocess" suggests you are spawning a new process >>> which gets another uid through sudo. This does not affect the parent >>> process. >>> >K> Ok then here is the work-around which I am thinking to try, Plese tell >K> me if it is correct. >K> I will let that subprocess start python inthe background and execute the >K> connecting code to postgresql including importing the pygresql library. >K> Then I will create the connection and cursor objcts in that subprocess. >K> But my concern is, will the connection object in the child process >K> (subprocess) be available to the parrent process? No. However it is still not clear why you want to run under the postgres user id. Why can't the original process not do the postgres connection? If that is really impossible, then you might start the new process with sudo and let it do a socket tunnelling to postgress, i.e. make a connection to the postgres server and a socket connection to the original Python script, and copy everything from one socket to the other - in both directions. However this can also be done with a ssh tunnel which might be simpler. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at mrabarnett.plus.com Thu Jul 23 13:42:21 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 18:42:21 +0100 Subject: win32clipboard operation In-Reply-To: References: Message-ID: <4A68A0FD.4010204@mrabarnett.plus.com> LeeRisq wrote: > On Jul 23, 9:05 am, MRAB wrote: >> LeeRisq wrote: >>> Hi all, >>> Newbie question. I've written a script that outputs to a text file. >>> Now, I just want to copy the content to win32clipboard for access to >>> other applications. Here's the mess I've come up with so far:) >> [snip] >> >> def copy_text(): >> ifile = open(r"C:\Query\DQL.txt", "r") >> text = ifile.read() >> ifile.close() >> >> win32clipboard.OpenClipboard() >> win32clipboard.EmptyClipboard() >> win32clipboard.SetClipboardText(text) >> win32clipboard.CloseClipboard() > > I've actually tried this configuration, but I did it again just to be > sure. The program executes without exception, but the text still isn't > copied to the clipboard. Any ideas? All I can say is that it works for me (Windows XP Pro, service pack 3). From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Thu Jul 23 13:45:52 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Thu, 23 Jul 2009 13:45:52 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: On 2009-07-23 13:15:00 -0400, Isaac Gouy said: > I get > the feeling I'm missing the joke? Yes, you are missing the joke. The point is that if python is 60x slower than C, even if there were not a GIL, it would require running the python program on a 60 core machine just reach parity with C. The existence of the GIL means that in reality you'd probably need a several hundred core machine running python just to equal what C can do on one core. Hence the 13375p34k pseudo quote - "teh slowness on all ur cores!" -- Raffael Cavallaro From alex.repenning at gmail.com Thu Jul 23 13:54:26 2009 From: alex.repenning at gmail.com (game_designer) Date: Thu, 23 Jul 2009 10:54:26 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: Perhaps like Xah Lee I find, after many years of Lisp programming, these discussions increasingly frustrating and even, in some sense, amazing. We can speculate all we want about syntax and semantics of programing languages. What counts in the end are really the PRAGMATICS of programming languages. How can I do something with a language that is USEFUL to me? Will the result be good looking, and snappy or some ugly, dated looking, crashing application? For instance, last time I played with Scheme (drScheme) to explore some OpenGL 3D issue I was not impressed at all. One can debate the syntax and semantics of Scheme but in that particular instance all that was important to me was the fact that the Scheme example performed terrible and the threading fell completely apart when running more that a single OpenGL window. Perhaps this was coded poorly but I don't care. Scheme left a pretty bad impression. alex Prof. Alexander Repenning University of Colorado Computer Science Department Boulder, CO 80309-430 From piet at cs.uu.nl Thu Jul 23 14:20:50 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 20:20:50 +0200 Subject: sqlite3 performance problems only in python References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: >>>>> Stef Mientki (SM) wrote: >SM> btw, I don't know if it's of any importance, the SQL-statement I perform is >SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >SM> from OPNAMEN >SM> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >SM> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >SM> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >SM> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >SM> inner join POID_SSID_SCID on ( OPNAMEN.POID = >SM> POID_SSID_SCID.POID ) and >SM> ( SUBSCHAAL_GEGEVENS.SSID = >SM> POID_SSID_SCID.SSID ) >SM> inner join SCORES on SCORES.SCID = >SM> POID_SSID_SCID.SCID >SM> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >SM> inner join PATIENT on PATIENT.PID = PID_POID.PID >SM> where substr ( lower( NAME) , 1, 6) = 'cis20r' >SM> and lower ( NAME_ ) = 'fatigue' >SM> and TEST_COUNT in (3,4) >SM> and DATETIME > 39814.0 >SM> and SCORE < 30 1) Do you have indices on the join fields? 2) Look at the ANALYZE command 3) Look at the EXPLAIN command -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From paul-donnelly at sbcglobal.net Thu Jul 23 15:09:31 2009 From: paul-donnelly at sbcglobal.net (Paul Donnelly) Date: Thu, 23 Jul 2009 14:09:31 -0500 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: <87r5w72p3o.fsf@plap.localdomain> game_designer writes: > Perhaps like Xah Lee I find, after many years of Lisp programming, > these discussions increasingly frustrating and even, in some sense, > amazing. We can speculate all we want about syntax and semantics of > programing languages. What counts in the end are really the PRAGMATICS > of programming languages. How can I do something with a language that > is USEFUL to me? Will the result be good looking, and snappy or some > ugly, dated looking, crashing application? For instance, last time I > played with Scheme (drScheme) to explore some OpenGL 3D issue I was > not impressed at all. One can debate the syntax and semantics of > Scheme but in that particular instance all that was important to me > was the fact that the Scheme example performed terrible and the > threading fell completely apart when running more that a single OpenGL > window. Perhaps this was coded poorly but I don't care. Scheme left a > pretty bad impression. One implementation of one dialect of Lisp worked poorly for one particular project some unspecified number of years ago, judging by code (written by you, no less) that may or may not have been terrible? I appreciate that this was a frustrating experience, but I don't see what lesson about Lisp programming we're supposed to be getting from this. From bbarbero at inescporto.pt Thu Jul 23 15:23:54 2009 From: bbarbero at inescporto.pt (bbarbero at inescporto.pt) Date: Thu, 23 Jul 2009 21:23:54 +0200 Subject: Problems in commands.getoutput(cmd) with sox Message-ID: <20090723212354.5378695qs4beje8a@horde.inescporto.pt> Hello to all! I am a new researcher, new to Python as well, and I have a problem, when I try to get commands from sox in a python script. I am going crazy, because this code has been working before.. so I dont really know whats going on.. Ive already seen some solutions that match with what I have. I am running this: if os.path.splitext(file_au)[1] == ".au": resampled_file_path = os.path.join(resampled_path, file_au) cmd = "sox " + dir_file + " -r 44100 " + resampled_file_path print cmd output = commands.getoutput(cmd) print output What i do, is just to resample a song from dir_file to resampled_file_path. As a result of "cmd" I get: .... sox /Volumes/HAL/Datasets/Audio/gtzan_genres/rock/rock.00097.au -r 44100 /Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resampled/rock/rock.00097.au ..... If I run this on the command line, it will work perfectly! But The program stops at File "/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resamplingto44.py", line 35 output = commands.getoutput(cmd) ^ IndentationError: unexpected indent I dont have any idea, whats going on, I am trying lot of things, but I can understand where is the error as it has been running perfectly before. Any suggestions will be more than welcome! Thanks in advance for your help! Best regards, Beatriz Mora. ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program. From nick at craig-wood.com Thu Jul 23 15:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 14:29:57 -0500 Subject: extract c/cpp include file with regular expression References: Message-ID: tiefeng wu wrote: > I need to parse c/cpp source files, one requirement is to extract > included header file name. If you are serious about parsing C code then you'll want to investigate gcc-xml http://www.gccxml.org/HTML/Index.html This runs the gcc frontend over the code but instead of producing an object file, it produces a machine readable xml file describing the source. It is used by h2xml.py / xml2py.py to make ctypes header file automatically. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From gslindstrom at gmail.com Thu Jul 23 15:30:09 2009 From: gslindstrom at gmail.com (Greg Lindstrom) Date: Thu, 23 Jul 2009 14:30:09 -0500 Subject: Creating xml Message-ID: It's been a while since I've played with XML using Python but I've been asked to create XML using data from our postgres database. Currently we are generating XML directly from the database using a set of stored procedures but it is too slow (yes, I have numbers). I have been asked to create a routine to generate the XML to see if it will be faster that the sprocs (which I think it will be, based on other routines we have running). But I digress... Using my favorite search engine I see that there is a lot of material on Python and XML. At the risk of starting something (which is not my intention), what are my options if I want to create xml? Ceratinly writing my own routine is an option, but I bet there are better ones :-) How about if I need/want to parse or process an XML file? Thanks! --greg -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Thu Jul 23 15:30:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 21:30:56 +0200 Subject: Looking for os.listdir() generator References: Message-ID: >>>>> Christian Heimes (CH) wrote: >CH> Hello, >CH> I'm looking for a generator version of os.listdir() for Python 2.5 and >CH> newer. I know somebody has worked on it because I've seen a generator >CH> version in a posting on some list or blog a while ago. I can't find it >CH> anymore. It seems my Google fu is lacking today. All I can find is a >CH> very old version of xlistdir. A Cython based solution is appreciated but >CH> please no ctypes version. Nick Craig-Wood posted these about 5 weeks ago in the thread `waling a directory with very many files' [sic!] Message id's: (ctypes) (Cython) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From leerisq at gmail.com Thu Jul 23 15:31:14 2009 From: leerisq at gmail.com (LeeRisq) Date: Thu, 23 Jul 2009 12:31:14 -0700 (PDT) Subject: win32clipboard operation References: Message-ID: > I've actually tried this configuration, but I did it again just to be > sure. The program executes without exception, but the text still isn't > copied to the clipboard. Any ideas? So, I think I've figured out the issue. Which brings me to another question...why is it that I can manually copy and paste the text without issue, but when I run a python script that copies the same text to the clipboard, it won't accept the amount of text I am placing on the clipboard. Is there some kind of setting I am missing? Buffer size maybe? From clp2 at rebertia.com Thu Jul 23 15:32:58 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 23 Jul 2009 12:32:58 -0700 Subject: Creating xml In-Reply-To: References: Message-ID: <50697b2c0907231232o4e5614dame0adee88d4a15cf@mail.gmail.com> On Thu, Jul 23, 2009 at 12:30 PM, Greg Lindstrom wrote: > How about if I need/want to parse or process an XML file? xml.etree.ElementTree in the standard library: http://docs.python.org/library/xml.etree.elementtree.html Cheers, Chris -- http://blog.rebertia.com From vinay_sajip at yahoo.co.uk Thu Jul 23 15:42:29 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Thu, 23 Jul 2009 12:42:29 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> Message-ID: <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> On Jul 23, 4:06 am, Naoki INADA wrote: > In document stdtypes.html#file.encoding>: > > >> The encoding that this file uses. When Unicode strings are written to a file, > >> they will be converted to byte strings using this encoding. In addition, > >> when the file is connected to a terminal, the attribute gives the encoding > >> that the terminal is likely to use > > But inlogging.StreamHandler.emit() :: > > try: > if (isinstance(msg, unicode) and > getattr(stream, 'encoding', None)): > #fs = fs.decode(stream.encoding) > try: > stream.write(fs % msg) > except UnicodeEncodeError: > #Printing to terminals sometimes fails. > For example, > #with an encoding of 'cp1251', the above > write will > #work if written to a stream opened or > wrapped by > #the codecs module, but fail when writing > to a > #terminal even when the codepage is set to > cp1251. > #An extra encoding step seems to be > needed. > stream.write((fs % msg).encode > (stream.encoding)) > else: > stream.write(fs % msg) > except UnicodeError: > stream.write(fs % msg.encode("UTF-8")) > > And behavior of sys.stdout in Windows::>>> import sys > >>> sys.stdout.encoding > 'cp932' > >>> u = u"???" > >>> u > > u'\u3042\u3044\u3046'>>> print >>sys.stdout, u > ??? > >>> sys.stderr.write(u) > > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 0-2: ordinal not in range(128) > > What is file.encoding convention? > If I want to write a unicode string to a file(-like) that have > encoding attribute, I should do > (1) try: file.write(unicode_str), > (2) except UnicodeEncodeError: file.write(unicode_str.encode > (file.encoding)) > likelogging? > It seems agly. Further to my earlier mail, please have a look at the following screenshot: http://imgur.com/FtAi0.png As you can see, the codepage is set to 1251 (Cyrillic) at the beginning. A Unicode string is initialised with Cyrillic code points. Then sys.stdout.encoding shows 'cp1251', but writing the string to it gives a UnicodeEncodeError. Explicitly encoding the string and writing it works. Next, we get a wrapper from the codecs module for the same encoding and use it to wrap sys.stdout. Writing the Unicode string to the wrapped string works, too. So the problem is essentially this: if a stream has an encoding attribute, sometimes it is a wrapped stream which encodes Unicode (e.g. a stream obtained via the codecs module) and sometimes it is not (e.g. sys.stdout, sys.stderr). Regards, Vinay From clp2 at rebertia.com Thu Jul 23 15:52:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 23 Jul 2009 12:52:48 -0700 Subject: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox) Message-ID: <50697b2c0907231252w4715c94di7876b8580cddf170@mail.gmail.com> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: > You can use tabnanny to help diagnose the problem: > http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a Anyone know what's the deal with effbot.org? It seems to be broken at present, forcing me to use Google's cached version. It's a real shame since it has lots of handy Python info. Cheers, Chris -- http://blog.rebertia.com From stef.mientki at gmail.com Thu Jul 23 15:58:30 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 21:58:30 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: <4A68C0E6.90905@gmail.com> Piet van Oostrum wrote: >>>>>> Stef Mientki (SM) wrote: >>>>>> > > >> SM> btw, I don't know if it's of any importance, the SQL-statement I perform is >> SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >> SM> from OPNAMEN >> SM> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >> SM> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >> SM> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >> SM> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >> SM> inner join POID_SSID_SCID on ( OPNAMEN.POID = >> SM> POID_SSID_SCID.POID ) and >> SM> ( SUBSCHAAL_GEGEVENS.SSID = >> SM> POID_SSID_SCID.SSID ) >> SM> inner join SCORES on SCORES.SCID = >> SM> POID_SSID_SCID.SCID >> SM> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >> SM> inner join PATIENT on PATIENT.PID = PID_POID.PID >> SM> where substr ( lower( NAME) , 1, 6) = 'cis20r' >> SM> and lower ( NAME_ ) = 'fatigue' >> SM> and TEST_COUNT in (3,4) >> SM> and DATETIME > 39814.0 >> SM> and SCORE < 30 >> > > 1) Do you have indices on the join fields? > well I'm happily surprised, you came up with this suggestion - I thought that sqlite created indexes on all primairy key and unique fields - but after explicitly creating the indices, a gained a speed of about a factor 10 After checking the database creation, it seemed I forgot to make these fields the primary key so thanks very much. I gained another factor of 10 speed by updating to version 2.5.5 of pysqlite. cheers, Stef > 2) Look at the ANALYZE command > 3) Look at the EXPLAIN command > From stef.mientki at gmail.com Thu Jul 23 16:01:16 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 22:01:16 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: <4A68C18C.3090605@gmail.com> David Stanek wrote: > On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote: > >> btw, I don't know if it's of any importance, the SQL-statement I perform is >> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >> from OPNAMEN >> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >> inner join POID_SSID_SCID on ( OPNAMEN.POID = >> POID_SSID_SCID.POID ) and >> ( SUBSCHAAL_GEGEVENS.SSID = >> POID_SSID_SCID.SSID ) >> inner join SCORES on SCORES.SCID = >> POID_SSID_SCID.SCID >> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >> inner join PATIENT on PATIENT.PID = PID_POID.PID >> where substr ( lower( NAME) , 1, 6) = 'cis20r' >> and lower ( NAME_ ) = 'fatigue' >> and TEST_COUNT in (3,4) >> and DATETIME > 39814.0 >> and SCORE < 30 >> > > Warning: I suck at SQL and hate it with a passion... > +1, but I couldn't find anything better. I guess you have some better suggestions ? (I looked at Dee, but from my first view, it looks a bit premature) thanks, Stef From nick at craig-wood.com Thu Jul 23 16:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 15:29:57 -0500 Subject: sqlite3 performance problems only in python References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: David Stanek wrote: > On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote: > > > > btw, I don't know if it's of any importance, the SQL-statement I perform is > > select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* > > ?from OPNAMEN > > ? inner join POID_VLID ? ? ? ? ?on OPNAMEN.POID ? ? ? ? ? ?= POID_VLID.POID > > ? inner join VRAAGLST ? ? ? ? ? on VRAAGLST.VLID ? ? ? ? ? = POID_VLID.VLID > > ? inner join VLID_SSID ? ? ? ? ?on VRAAGLST.VLID ? ? ? ? ? = VLID_SSID.VLID > > ? inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID > > ? inner join POID_SSID_SCID ? ? on ( OPNAMEN.POID ? ? ? ? ? ?= > > POID_SSID_SCID.POID ) and > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?( SUBSCHAAL_GEGEVENS.SSID = > > POID_SSID_SCID.SSID ) > > ? inner join SCORES ? ? ? ? ? ? on SCORES.SCID ? ? ? ? ? ? = > > POID_SSID_SCID.SCID > > ? inner join PID_POID ? ? ? ? ? on OPNAMEN.POID ? ? ? ? ? ?= PID_POID.POID > > ? inner join PATIENT ? ? ? ? ? ?on PATIENT.PID ? ? ? ? ? ? = PID_POID.PID > > ?where substr ( lower( NAME) , 1, 6) ?= 'cis20r' > > ? and lower ( NAME_ ) = 'fatigue' > > ? and TEST_COUNT in (3,4) > > ? and DATETIME > 39814.0 > > ? and SCORE < 30 > > Warning: I suck at SQL and hate it with a passion... > > By using lower() on the left side of the where expressions I believe > that you are table scanning. So it is not the size of the data > returned, but the size of the data that needs to be scanned. In all the databases I've used, the like operator has been case insensitive, so if that is the problem you could use NAME like '%cis20r%' -- not quite the same, but close! and NAME_ like 'fatigue' instead which might be quicker. Or not ;-) -- Nick Craig-Wood -- http://www.craig-wood.com/nick From nick at craig-wood.com Thu Jul 23 16:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 15:29:57 -0500 Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: Ville Vainio wrote: > Has anyone implementing something like what the subject line > indicates? > > The idea: > > To run functions that execute a series of system commands without > blocking the ui, *and* without adding state machine logic. > > The syntax would be something like: > > def work(): > > showstatus("building") > r = yield runshell("make") > showstatus("installing") > r = yield runshell("make install") > showstatus("Success") > > mygui.startwork(work) > # returns immediately, runs work() gradually in the background. > > The catch is that showstatus() would need to be run in the mainloop, > so running the whole thing in a thread is a no-go. > > I imagine runshell() would be implemented in terms of QProcess, or > subprocess.Popen/os.system and a worker thread. > > Anyone done this already, or do I have to roll my own? You might want to look at twisted, in particular http://twistedmatrix.com/trac/wiki/DeferredGenerator -- Nick Craig-Wood -- http://www.craig-wood.com/nick From nick at craig-wood.com Thu Jul 23 16:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 15:29:57 -0500 Subject: Looking for os.listdir() generator References: Message-ID: Christian Heimes wrote: > I'm looking for a generator version of os.listdir() for Python 2.5 and > newer. I know somebody has worked on it because I've seen a generator > version in a posting on some list or blog a while ago. I can't find it > anymore. It seems my Google fu is lacking today. All I can find is a > very old version of xlistdir. A Cython based solution is appreciated but > please no ctypes version. I posted exactly that yesterday I think ;-) Note that this has a python part as well as a ctypes part because the version of ctypes I used doesn't support generators. I think the development version does though. Here is it --setup.py---------------------------------------------------------- from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( cmdclass = {'build_ext': build_ext}, ext_modules = [Extension("directory", ["directory.pyx"])] ) --directory.pyx---------------------------------------------------------- # Cython interface for listdir # python setup.py build_ext --inplace import cython cdef extern from "dirent.h": struct dirent: char d_name[0] ctypedef struct DIR DIR *opendir(char *name) int closedir(DIR *dirp) dirent *readdir(DIR *dirp) cdef extern from "errno.h": int errno cdef extern from "string.h": char *strerror(int errnum) cdef class Directory: """Represents an open directory""" cdef DIR *handle def __init__(self, path): self.handle = opendir(path) if self.handle is NULL: raise OSError(errno, "Failed to open directory: %s" % strerror(errno)) def readdir(self): """Read the next name in the directory""" cdef dirent *p p = readdir(self.handle) if p is NULL: return None return p.d_name def close(self): """Close the directory""" if self.handle is not NULL: closedir(self.handle) self.handle = NULL def __dealloc__(self): self.close() --listdir.py---------------------------------------------------------- from directory import Directory def listdir(path): """ A generator to return the names of files in the directory passed in """ d = Directory(path) while True: name = d.readdir() if not name: break if name not in (".", ".."): yield name d.close() del d if __name__ == "__main__": import sys paths = sys.argv[1:] if not paths: paths = ["."] for path in paths: print "*** %s ***" % path for name in listdir(path): print name ------------------------------------------------------------ -- Nick Craig-Wood -- http://www.craig-wood.com/nick From jaime.frio at gmail.com Thu Jul 23 16:34:21 2009 From: jaime.frio at gmail.com (Jaime Fernandez del Rio) Date: Thu, 23 Jul 2009 22:34:21 +0200 Subject: installing 2.6 on vista64 In-Reply-To: References: Message-ID: <97a8f1a70907231334y1fd64890g9c8cee11b1e41f8f@mail.gmail.com> I have installed the 32 bit 2.6 and the 64 bit 3.1 on my machine running Vista 64 without any issues. Which of course has nothing to do with your problem.... On Thu, Jul 23, 2009 at 7:06 PM, DwBear75 wrote: > I just downloaded and attempted to install python 2.6.2. ?The > installer proceeds to do its work then dies, leaving an entry in the > eventlog: > > Windows Installer installed the product. Product Name: Python 2.6.2. > Product Version: 2.6.2150. Product Language: 1033. Installation > success or error status: 1602. > > Googling for this I wasn't able to narrow the results down to > something usable. Anyone know of issues and how to fix them installing > on vista 64 (yes, I have 8 gigs of ram) > -- > http://mail.python.org/mailman/listinfo/python-list > -- (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ay?dale en sus planes de dominaci?n mundial. From malaclypse2 at gmail.com Thu Jul 23 16:50:56 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 23 Jul 2009 16:50:56 -0400 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: <16651e80907231350t554f8603i7e6bb82172169b52@mail.gmail.com> On Thu, Jul 23, 2009 at 4:29 PM, Nick Craig-Wood wrote: > In all the databases I've used, the like operator has been case > insensitive, so if that is the problem you could use This is not true in all databases! ?Many times, this is something that is configurable when setting up the database server. ?I'm not sure what the defaults are, but here at work we have DB/2 and Oracle servers that are case sensitive, and at least one MSSQL server that is not. -- Jerry From laran.evans at gmail.com Thu Jul 23 16:53:19 2009 From: laran.evans at gmail.com (Laran Evans) Date: Thu, 23 Jul 2009 13:53:19 -0700 (PDT) Subject: 2.3 missing on OSX Message-ID: I just tried to run MacVim on OSX 10.5. It crashed with this: Dyld Error Message: Library not loaded: /System/Library/Frameworks/Python.framework/ Versions/2.3/Python Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ MacVim.app/Contents/MacOS/Vim Reason: image not found I posted to the MacVim mailing list and was told that that version of 2.3 should always be available because it comes with the OS. I don't remember if I removed it for whatever reason at some point or something. In any case, I have python installed now via MacPorts. I have version 2.3 and 2.5 both installed. So my question is, is there a way for me to fix the issue by re- instating the default 2.3 location or something? I'm not much of a Python person. So I don't really know where to start with this one. Thanks. From deets at nospam.web.de Thu Jul 23 17:01:21 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 23:01:21 +0200 Subject: 2.3 missing on OSX In-Reply-To: References: Message-ID: <7cs1d1F1tbs10U1@mid.uni-berlin.de> Laran Evans schrieb: > I just tried to run MacVim on OSX 10.5. It crashed with this: > > Dyld Error Message: > Library not loaded: /System/Library/Frameworks/Python.framework/ > Versions/2.3/Python > Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ > MacVim.app/Contents/MacOS/Vim > Reason: image not found > > I posted to the MacVim mailing list and was told that that version of > 2.3 should always be available because it comes with the OS. On my 10.5 machine, it is under the above given path. > > I don't remember if I removed it for whatever reason at some point or > something. In any case, I have python installed now via MacPorts. I > have version 2.3 and 2.5 both installed. > > So my question is, is there a way for me to fix the issue by re- > instating the default 2.3 location or something? Most probably yes, but not using MacPorts. Those aren't framework-builds, and not in the proper location anyway. I suggest you try & get hold of a copy from another OSX-machine (match teh architecture), and just copy it into the needed location. Diez From philip at semanchuk.com Thu Jul 23 17:03:41 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 17:03:41 -0400 Subject: 2.3 missing on OSX In-Reply-To: References: Message-ID: On Jul 23, 2009, at 4:53 PM, Laran Evans wrote: > I just tried to run MacVim on OSX 10.5. It crashed with this: > > Dyld Error Message: > Library not loaded: /System/Library/Frameworks/Python.framework/ > Versions/2.3/Python > Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ > MacVim.app/Contents/MacOS/Vim > Reason: image not found > > I posted to the MacVim mailing list and was told that that version of > 2.3 should always be available because it comes with the OS. OS X 10.4 shipped with Python 2.3 as the system Python. OS X 10.5 ships with Python 2.5 as the system Python. 2.3 is not available unless you install it yourself. And if you install it yourself, I wouldn't put it where MacVim is looking for it because the System folder is OS X stuff. If MacVim (or any app) contains a hardcoded reference to /System/.../2.3/Python, then it will break on OS X 10.5. From philip at semanchuk.com Thu Jul 23 17:14:37 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 17:14:37 -0400 Subject: 2.3 missing on OSX In-Reply-To: <7cs1d1F1tbs10U1@mid.uni-berlin.de> References: <7cs1d1F1tbs10U1@mid.uni-berlin.de> Message-ID: On Jul 23, 2009, at 5:01 PM, Diez B. Roggisch wrote: > Laran Evans schrieb: >> I just tried to run MacVim on OSX 10.5. It crashed with this: >> Dyld Error Message: >> Library not loaded: /System/Library/Frameworks/Python.framework/ >> Versions/2.3/Python >> Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ >> MacVim.app/Contents/MacOS/Vim >> Reason: image not found >> I posted to the MacVim mailing list and was told that that version of >> 2.3 should always be available because it comes with the OS. > > On my 10.5 machine, it is under the above given path. Diez is correct, please ignore my earlier post on this topic. I have 2.3 on my machine in that location as well; I didn't look closely enough. Apologies for the misinformation. I'm not answering any more Mac Python questions as I seem to get them wrong every time. :-/ From nad at acm.org Thu Jul 23 17:14:50 2009 From: nad at acm.org (Ned Deily) Date: Thu, 23 Jul 2009 14:14:50 -0700 Subject: 2.3 missing on OSX References: Message-ID: In article , Laran Evans wrote: > I just tried to run MacVim on OSX 10.5. It crashed with this: > > Dyld Error Message: > Library not loaded: /System/Library/Frameworks/Python.framework/ > Versions/2.3/Python > Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ > MacVim.app/Contents/MacOS/Vim > Reason: image not found > > I posted to the MacVim mailing list and was told that that version of > 2.3 should always be available because it comes with the OS. > > I don't remember if I removed it for whatever reason at some point or > something. In any case, I have python installed now via MacPorts. I > have version 2.3 and 2.5 both installed. > > So my question is, is there a way for me to fix the issue by re- > instating the default 2.3 location or something? If, in fact, /System/Library/Frameworks/Python.framework/Versions/2.3 is empty or missing, you should restore it. Rule of thumb: everything under /System is installed and managed by Apple. Don't remove things from there! I don't know of a particularly easy way to restore it, short of hacking a Leopard installer image or re-installing OS X. If you have an unused external drive or partition, it might be easiest to do a vanilla install of Leopard and all software updates, then, from the terminal, do a ditto of that directory subtree back to your regular /. -- Ned Deily, nad at acm.org From __peter__ at web.de Thu Jul 23 17:20:42 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 23:20:42 +0200 Subject: Problems in commands.getoutput(cmd) with sox References: Message-ID: bbarbero at inescporto.pt wrote: > Hello to all! > > I am a new researcher, new to Python as well, and I have a problem, > when I try to get commands from sox in a python script. I am going > crazy, because this code has been working before.. so I dont really > know whats going on.. > > Ive already seen some solutions that match with what I have. I am > running this: > > > > if os.path.splitext(file_au)[1] == ".au": > resampled_file_path = os.path.join(resampled_path, file_au) > cmd = "sox " + dir_file + " -r 44100 " + resampled_file_path > print cmd > output = commands.getoutput(cmd) > print output > > What i do, is just to resample a song from dir_file to > resampled_file_path. As a result of "cmd" I get: > > .... > sox /Volumes/HAL/Datasets/Audio/gtzan_genres/rock/rock.00097.au -r > 44100 > /Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resampled/rock/rock.00097.au > ..... > > If I run this on the command line, it will work perfectly! > > But The program stops at > > File > "/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resamplingto44.py", > line 35 > output = commands.getoutput(cmd) > ^ > IndentationError: unexpected indent > > I dont have any idea, whats going on, I am trying lot of things, but I > can understand where is the error as it has been running perfectly > before. > > Any suggestions will be more than welcome! Thanks in advance for your > help! You are probably mixing tabs and spaces and have set your editor to display a tabsize != 8 spaces. When you change the tabsize to 8 you will see the inconsistent indentation. In the future you can avoid such problems by configuring your editor to replace one tab with four spaces. Recommended reading: http://www.python.org/dev/peps/pep-0008/ Peter From python at mrabarnett.plus.com Thu Jul 23 17:25:39 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 22:25:39 +0100 Subject: Problems in commands.getoutput(cmd) with sox In-Reply-To: <20090723212354.5378695qs4beje8a@horde.inescporto.pt> References: <20090723212354.5378695qs4beje8a@horde.inescporto.pt> Message-ID: <4A68D553.6090203@mrabarnett.plus.com> bbarbero at inescporto.pt wrote: > Hello to all! > > I am a new researcher, new to Python as well, and I have a problem, when > I try to get commands from sox in a python script. I am going crazy, > because this code has been working before.. so I dont really know whats > going on.. > > Ive already seen some solutions that match with what I have. I am > running this: > > > > if os.path.splitext(file_au)[1] == ".au": > resampled_file_path = os.path.join(resampled_path, file_au) > cmd = "sox " + dir_file + " -r 44100 " + resampled_file_path > print cmd ^ extra indent > output = commands.getoutput(cmd) > print output > > What i do, is just to resample a song from dir_file to > resampled_file_path. As a result of "cmd" I get: > > .... > sox /Volumes/HAL/Datasets/Audio/gtzan_genres/rock/rock.00097.au -r 44100 > /Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resampled/rock/rock.00097.au > > ..... > > If I run this on the command line, it will work perfectly! > > But The program stops at > > File > "/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resamplingto44.py", > line 35 > output = commands.getoutput(cmd) > ^ > IndentationError: unexpected indent > > I dont have any idea, whats going on, I am trying lot of things, but I > can understand where is the error as it has been running perfectly before. > > Any suggestions will be more than welcome! Thanks in advance for your help! > You need to double-check the indentation. From gh at ghaering.de Thu Jul 23 17:27:08 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Thu, 23 Jul 2009 23:27:08 +0200 Subject: Creating xml In-Reply-To: References: Message-ID: Greg Lindstrom wrote: > It's been a while since I've played with XML using Python but I've been > asked to create XML using data from our postgres database. Currently we > are generating XML directly from the database using a set of stored > procedures but it is too slow (yes, I have numbers). I have been asked > to create a routine to generate the XML to see if it will be faster that > the sprocs (which I think it will be, based on other routines we have > running). But I digress... I write to a cStringIO.StringIO instance to create XML. Like this (pseudo-code): class Foo: def toxml(self, stream): print >> stream, "" If you're concerned about producing valid XML, you can write tests ;-) > Using my favorite search engine I see that there is a lot of material on > Python and XML. At the risk of starting something (which is not my > intention), what are my options if I want to create xml? Ceratinly > writing my own routine is an option, but I bet there are better ones :-) > > How about if I need/want to parse or process an XML file? Use lxml. It's blazingly fast and its XPath support makes parsing XML a snap. If you don't want to depend on anything not in the standard library, use the etree module. Code will be slightly longer because you don't have XPath support and performance will be only "ok". Not "super-fast", like with lxml. HTH -- Gerhard From suruti94 at gmail.com Thu Jul 23 17:45:41 2009 From: suruti94 at gmail.com (Mohan Parthasarathy) Date: Thu, 23 Jul 2009 14:45:41 -0700 Subject: Bridging Python and C Message-ID: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Hi, I am a newbie. It looks like there are quite a few ways to bridge Python and C. I have a bunch of C code and I just need Python wrappers for it. If i google for this I get SWIG, Boost etc. And I also see http://www.python.org/doc/2.5.2/ext/intro.html What is the recommended way for doing this ? thanks mohan -------------- next part -------------- An HTML attachment was scrubbed... URL: From jon at ffconsultancy.com Thu Jul 23 17:49:13 2009 From: jon at ffconsultancy.com (Jon Harrop) Date: Thu, 23 Jul 2009 22:49:13 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: Raffael Cavallaro wrote: > Yes, you are missing the joke. The point is that if python is 60x > slower than C, even if there were not a GIL, it would require running > the python program on a 60 core machine just reach parity with C. The > existence of the GIL means that in reality you'd probably need a > several hundred core machine... No, OCaml is in the same boat as Python. Spawning a parallel work item takes 20,000x longer in OCaml than in F#. Gathering the results is asymptotically slower in general. Consequently, for most problems, Python or OCaml running on an infinite number of cores cannot beat F# running on 2 or more (and I already have 8) because the overheads are far too high for parallelism to pay off. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u From lists at cheimes.de Thu Jul 23 17:53:22 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 23:53:22 +0200 Subject: Bridging Python and C In-Reply-To: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> References: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Message-ID: Mohan Parthasarathy wrote: > Hi, > I am a newbie. It looks like there are quite a few ways to bridge Python and > C. I have a bunch of C code and I just need Python wrappers for it. If i > google for this I get SWIG, Boost etc. And I also see > > http://www.python.org/doc/2.5.2/ext/intro.html > > What is the recommended way for doing this ? I recommend Cython (http://cython.org/). I'm using it on a daily bases to wrap C libraries. Christian From philip at semanchuk.com Thu Jul 23 17:54:33 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 17:54:33 -0400 Subject: Bridging Python and C In-Reply-To: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> References: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Message-ID: <12D9D99B-6730-4534-BA61-631A4B9B27FE@semanchuk.com> On Jul 23, 2009, at 5:45 PM, Mohan Parthasarathy wrote: > Hi, > I am a newbie. It looks like there are quite a few ways to bridge > Python and > C. I have a bunch of C code and I just need Python wrappers for it. > If i > google for this I get SWIG, Boost etc. And I also see > > http://www.python.org/doc/2.5.2/ext/intro.html > > What is the recommended way for doing this ? "Recommended" depends on what you want to accomplish. SWIG, Boost, etc. all have pros and cons. One option you didn't mention is ctypes which is part of the Python standard library. It allows you to pass params to C functions. Works great for me! HTH Philip From cmpython at gmail.com Thu Jul 23 17:54:34 2009 From: cmpython at gmail.com (Che M) Date: Thu, 23 Jul 2009 14:54:34 -0700 (PDT) Subject: sqlite3 performance problems only in python References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: On Jul 23, 3:58?pm, Stef Mientki wrote: > Piet van Oostrum wrote: > >>>>>> Stef Mientki (SM) wrote: > > >> SM> btw, I don't know if it's of any importance, the SQL-statement I perform is > >> SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* > >> SM> ?from OPNAMEN > >> SM> ? ?inner join POID_VLID ? ? ? ? ?on OPNAMEN.POID ? ? ? ? ? ?= POID_VLID.POID > >> SM> ? ?inner join VRAAGLST ? ? ? ? ? on VRAAGLST.VLID ? ? ? ? ? = POID_VLID.VLID > >> SM> ? ?inner join VLID_SSID ? ? ? ? ?on VRAAGLST.VLID ? ? ? ? ? = VLID_SSID.VLID > >> SM> ? ?inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID > >> SM> ? ?inner join POID_SSID_SCID ? ? on ( OPNAMEN.POID ? ? ? ? ? ?= > >> SM> POID_SSID_SCID.POID ) and > >> SM> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ( SUBSCHAAL_GEGEVENS.SSID = > >> SM> POID_SSID_SCID.SSID ) > >> SM> ? ?inner join SCORES ? ? ? ? ? ? on SCORES.SCID ? ? ? ? ? ? = > >> SM> POID_SSID_SCID.SCID > >> SM> ? ?inner join PID_POID ? ? ? ? ? on OPNAMEN.POID ? ? ? ? ? ?= PID_POID.POID > >> SM> ? ?inner join PATIENT ? ? ? ? ? ?on PATIENT.PID ? ? ? ? ? ? = PID_POID.PID > >> SM> ?where substr ( lower( NAME) , 1, 6) ?= 'cis20r' > >> SM> ? ?and lower ( NAME_ ) = 'fatigue' > >> SM> ? ?and TEST_COUNT in (3,4) > >> SM> ? ?and DATETIME > 39814.0 > >> SM> ? ?and SCORE < 30 > > > 1) Do you have indices on the join fields? > > well I'm happily surprised, you came up with this suggestion > - I thought that sqlite created indexes on all primairy key and unique > fields > - but after explicitly creating the indices, a gained a speed of about a > factor 10 > After checking the database creation, it seemed I forgot to make these > fields the primary key > so thanks very much. > > I gained another factor of 10 speed by updating to version 2.5.5 of > pysqlite. > > cheers, > Stef > > > 2) Look at the ANALYZE command > > 3) Look at the EXPLAIN command > > You might want to consult the SQLite list for questions like this. Why do you use pysqlite? I just import sqlite3 in Python 2.5. What is the advantage of pysqlite? Che From longbotham at gmail.com Thu Jul 23 18:05:34 2009 From: longbotham at gmail.com (Nathan) Date: Thu, 23 Jul 2009 15:05:34 -0700 (PDT) Subject: netCDF4 usage issues Message-ID: <7c26c9b2-4560-4c16-a01a-68db6c206177@i6g2000yqj.googlegroups.com> I am having issues correctly implementing the multi-file read functionality in the Python module netCDF4 (whitaker - http://code.google.com/p/netcdf4-python/). I am a relative beginner to Python, so I may be missing something simple. I've done my best to follow the example in the documentation at the website referenced above (reprinted): >>> from netCDF4 import MFDataset >>> f = MFDataset('mftest*nc') >>> print f.variables['x'][:] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] >>> Where I attempt to follow the same flow, I don't get the full data set returned. I only get a data set for the first file in my list (see notes in the code below). >from netCDF4 import MFDataset >f = MFDataset('E*nc') # All files I want to read are .nc files in a single directory, each file starting with E >temp = f.variables['TEMP'][:] >temp.shape Out[17]: (8940, 150) #This is the exact shape of the TEMP variables array in the first file I need to read verified by an external netCDF reader application >f.file_format #There are two files I am trying to read in this example Out[33]: ['NETCDF3_CLASSIC', 'NETCDF3_CLASSIC'] Does the module only support netcdf4 files? If so, why is it getting data out of one of the files? I'm unsure how to trouble shoot this. Any suggestions would be appreciated. If this should be posted elsewhere, please direct me to another list/forum as I didn't find any directly related to netCDF or this specific netCDF module. Thank you! From http Thu Jul 23 18:06:22 2009 From: http (Paul Rubin) Date: 23 Jul 2009 15:06:22 -0700 Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: <7xk51zm4v5.fsf@ruckus.brouhaha.com> Nobody writes: > They also have the advantage that one thread can run while another is > waiting on disk I/O, which isn't something which can be done with a > select/poll interface (even if select/poll worked for files, it doesn't > help for mapped files). AIO can help with this, but I don't know any language runtimes that use it right now. Really, this (and the similar issue of ram cache misses) is basically why hardware hyperthreading exists too. To get processor parallelism you do have to use OS threads, but green threads are lighter weight and switch faster, so you want to use a combination of both. Basically, use about as many OS threads as you have hardware threads (i.e. CPU cores or hyperthreads), and then assign green threads to OS threads in your application runtime. From lists at cheimes.de Thu Jul 23 18:08:56 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 24 Jul 2009 00:08:56 +0200 Subject: Looking for os.listdir() generator In-Reply-To: References: Message-ID: Nick Craig-Wood wrote: > Christian Heimes wrote: >> I'm looking for a generator version of os.listdir() for Python 2.5 and >> newer. I know somebody has worked on it because I've seen a generator >> version in a posting on some list or blog a while ago. I can't find it >> anymore. It seems my Google fu is lacking today. All I can find is a >> very old version of xlistdir. A Cython based solution is appreciated but >> please no ctypes version. > > I posted exactly that yesterday I think ;-) > > Note that this has a python part as well as a ctypes part because the > version of ctypes I used doesn't support generators. I think the > development version does though. Thanks Nick! ctypes? I'm sure you wanted to say Cython :) If you don't mind I'm going to wrap it up into a nice package and release it on PyPI. Christian From myzoku at gmail.com Thu Jul 23 18:14:19 2009 From: myzoku at gmail.com (Gordon) Date: Thu, 23 Jul 2009 15:14:19 -0700 (PDT) Subject: Gedcom and Genealogy Message-ID: <5222a601-a128-4892-9a8d-e1942ac9ce4d@e27g2000yqm.googlegroups.com> We have many small libraries in JAVA or Ruby that need to be ported to Python. Actually it's so simple a rewrite is possible too. From dr.mtarver at ukonline.co.uk Thu Jul 23 18:20:04 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Thu, 23 Jul 2009 15:20:04 -0700 (PDT) Subject: strange python scripting error References: Message-ID: On 23 July, 18:01, Dennis Lee Bieber wrote: > On Thu, 23 Jul 2009 08:48:46 -0700 (PDT), Mark Tarver > declaimed the following in > gmane.comp.python.general: > > > I have a very strange error. ?I have two test python files test.py and > > python.py which contain the following code > > ? ? ? ? > > > The other fails with a server configuration error. ?Both are running > > ? ? ? ? Way out from left field... Have you tried renaming "python.py" to > some other name? > > ? ? ? ? If the server is configured to treat .py as executable files, it > might be getting confused over "python.py" trying to invoke "python". > -- > ? ? ? ? Wulfraed ? ? ? ?Dennis Lee Bieber ? ? ? ? ? ? ? KD6MOG > ? ? ? ? wlfr... at ix.netcom.com ? ? ? ? ? ? wulfr... at bestiaria.com > ? ? ? ? ? ? ? ? HTTP://wlfraed.home.netcom.com/ > ? ? ? ? (Bestiaria Support Staff: ? ? ? ? ? ? ? web-a... at bestiaria.com) > ? ? ? ? ? ? ? ? HTTP://www.bestiaria.com/ Yes; tried pyth.py - still failed. Mark From dr.mtarver at ukonline.co.uk Thu Jul 23 18:27:51 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Thu, 23 Jul 2009 15:27:51 -0700 (PDT) Subject: strange python scripting error References: Message-ID: <99dbbe65-adcd-4f06-aa8c-2d7fc25bf94c@s15g2000yqs.googlegroups.com> On 23 July, 18:01, Dennis Lee Bieber wrote: > On Thu, 23 Jul 2009 08:48:46 -0700 (PDT), Mark Tarver > declaimed the following in > gmane.comp.python.general: > > > The only hint at a difference I can see is that my ftp program says > > the files are of unequal lengths. ?test.py is 129 bytes long. > > python.py 134 bytes long. > > ? ? ? ? Just a guess... > > ? ? ? ? Line endings... vs > > -- > ? ? ? ? Wulfraed ? ? ? ?Dennis Lee Bieber ? ? ? ? ? ? ? KD6MOG > ? ? ? ? wlfr... at ix.netcom.com ? ? ? ? ? ? wulfr... at bestiaria.com > ? ? ? ? ? ? ? ? HTTP://wlfraed.home.netcom.com/ > ? ? ? ? (Bestiaria Support Staff: ? ? ? ? ? ? ? web-a... at bestiaria.com) > ? ? ? ? ? ? ? ? HTTP://www.bestiaria.com/ Is that linefeed + ctrl or what? I can't pick up any difference reading the files char by char in Lisp. How do you find the difference? Mark From stef.mientki at gmail.com Thu Jul 23 18:29:40 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Fri, 24 Jul 2009 00:29:40 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: <4A68E454.8040909@gmail.com> Che M wrote: > On Jul 23, 3:58 pm, Stef Mientki wrote: > >> Piet van Oostrum wrote: >> >>>>>>>> Stef Mientki (SM) wrote: >>>>>>>> >>>> SM> btw, I don't know if it's of any importance, the SQL-statement I perform is >>>> SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >>>> SM> from OPNAMEN >>>> SM> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >>>> SM> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >>>> SM> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >>>> SM> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >>>> SM> inner join POID_SSID_SCID on ( OPNAMEN.POID = >>>> SM> POID_SSID_SCID.POID ) and >>>> SM> ( SUBSCHAAL_GEGEVENS.SSID = >>>> SM> POID_SSID_SCID.SSID ) >>>> SM> inner join SCORES on SCORES.SCID = >>>> SM> POID_SSID_SCID.SCID >>>> SM> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >>>> SM> inner join PATIENT on PATIENT.PID = PID_POID.PID >>>> SM> where substr ( lower( NAME) , 1, 6) = 'cis20r' >>>> SM> and lower ( NAME_ ) = 'fatigue' >>>> SM> and TEST_COUNT in (3,4) >>>> SM> and DATETIME > 39814.0 >>>> SM> and SCORE < 30 >>>> >>> 1) Do you have indices on the join fields? >>> >> well I'm happily surprised, you came up with this suggestion >> - I thought that sqlite created indexes on all primairy key and unique >> fields >> - but after explicitly creating the indices, a gained a speed of about a >> factor 10 >> After checking the database creation, it seemed I forgot to make these >> fields the primary key >> so thanks very much. >> >> I gained another factor of 10 speed by updating to version 2.5.5 of >> pysqlite. >> >> cheers, >> Stef >> >> >>> 2) Look at the ANALYZE command >>> 3) Look at the EXPLAIN command >>> >> > > You might want to consult the SQLite list for questions like this. > thanks, but because the same SQL-statement in Delphi performed well, I thought it was a problem with the Python implementation. > Why do you use pysqlite? I just import sqlite3 in Python 2.5. > What is the advantage of pysqlite? > it's 10 .. 15 times faster then sqlite3 delivered with pyton 2.5. AFAIK it's nothing different, just a newer version. cheers, Stef > Che > From pavlovevidence at gmail.com Thu Jul 23 18:39:35 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 15:39:35 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: <36db2930-b4c9-4308-a267-a41703160580@h21g2000yqa.googlegroups.com> On Jul 23, 2:37?am, Nobody wrote: > On Wed, 22 Jul 2009 15:17:52 -0700, Carl Banks wrote: > > So do all these OSes have some kind of __mega_unifying_poll system > > call that works for anything that might possibly block, that you can > > exploit from a user process? > > Threads ;) Yeah, well that was kind of my point, you'd need native threads to do some of this. Jean-Paul Calderone seemed to be suggesting that it was possible to wait for events on sockets, pipes, and IPC semaphores at the same. I pointed out that it isn't possible with well-known I/O polling calls (like select), so there must be some other way, and I was wondering what it was. He's a proponent of Twisted, which is a major async framework, and I'd have to figure that once or twice they've encounted issues like "how to poll both a semaphore and a socket at the same time", so maybe they've found a good solution to it. Carl Banks From __peter__ at web.de Thu Jul 23 18:49:47 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 00:49:47 +0200 Subject: strange python scripting error References: <99dbbe65-adcd-4f06-aa8c-2d7fc25bf94c@s15g2000yqs.googlegroups.com> Message-ID: Mark Tarver wrote: > On 23 July, 18:01, Dennis Lee Bieber wrote: >> On Thu, 23 Jul 2009 08:48:46 -0700 (PDT), Mark Tarver >> declaimed the following in >> gmane.comp.python.general: >> >> > The only hint at a difference I can see is that my ftp program says >> > the files are of unequal lengths. test.py is 129 bytes long. >> > python.py 134 bytes long. >> >> Just a guess... >> >> Line endings... vs >> >> -- >> Wulfraed Dennis Lee Bieber KD6MOG >> wlfr... at ix.netcom.com wulfr... at bestiaria.com >> HTTP://wlfraed.home.netcom.com/ >> (Bestiaria Support Staff: web-a... at bestiaria.com) >> HTTP://www.bestiaria.com/ > > Is that linefeed + ctrl or what? I can't pick up any difference > reading the files char by char in Lisp. How do you find the > difference? carriage-return + linefeed That's the Windows convention for end-of-line markers. Unix uses linefeed only. If you are on Windows you have to open the file in binary mode to see the difference: >>> open("python.py", "rb").read() '#!/usr/bin/python\r\nprint "Content-type: text/html"\r\nprint\r\nprint""\r\nprint "
Hello, Linux.com!
"\r\nprint ""' >>> open("test.py", "rb").read() '#!/usr/bin/python\nprint "Content-type: text/html"\nprint\nprint ""\nprint "
Hello, Linux.com!
"\nprint ""' >>> \n denotes a newline (chr(10)) \r denotes a cr (chr(13)) You can fix the line endings with open(outfile, "wb").writelines(open(infile, "rU")) Here "U" denotes "universal newline" mode which recognizes "\r", "\n", and "\r\n" as line endings and translates all of these into "\n". "wb" means "write binary" which does no conversion. Therefore you'll end up with outfile following the Unix convention. Peter From lists at cheimes.de Thu Jul 23 18:54:40 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 24 Jul 2009 00:54:40 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: <4A684B56.3020906@gmail.com> References: <4A684B56.3020906@gmail.com> Message-ID: <4A68EA30.5060006@cheimes.de> Stef Mientki schrieb: > hello, > > until now I used only small / simple databases in Python with sqlite3. > Now I've a large and rather complex database. > > The most simple query (with just a result of 100 rows), > takes about 70 seconds. > And all that time is consumed in "cursor.fetchall" > > Using the same database in Delphi, > using the same query, > takes less than 5 seconds (including displaying the full table in a grid). Side note: Since you are coming from Delphi you are probably glad to hear that Firebird (formally known as Borland InterBase) is well supported. The kinterbasdb DBA provides a DBA 2.0 compatible interface plus lots of additional Firebird specific features. You might be better of using Firebird if you need large and complex databases. With Firebird you can then create a lower case index: CREATE INDEX idx_lower_name ON table COMPUTED BY (LOWER(name)); and use it to query the table for lower case names with: SELECT ... FROM table t WHERE LOWER(t.name) STARTING WITH LOWER(:query) or maybe: SELECT ... FROM table t WHERE t.name STARTING WITH LOWER(:query) PLAN JOIN (t INDEX(idx_lower_name)); Firebird 2.1 supports even more useful features to join on columns without falling back to a full table scan like UNICODE_CI. Christian From david at boddie.org.uk Thu Jul 23 18:58:19 2009 From: david at boddie.org.uk (David Boddie) Date: Fri, 24 Jul 2009 00:58:19 +0200 Subject: Pyserial and pyQt References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> <70e4ebae-908f-4f3a-a96a-6fa0a7a7887a@y19g2000yqy.googlegroups.com> Message-ID: On Thursday 23 July 2009 10:13, Dennis Lee Bieber wrote: > On Wed, 22 Jul 2009 10:32:51 -0700 (PDT), Seth > declaimed the following in gmane.comp.python.general: > >> Thanks for the response. I have gone through a lot of the tutorials. >> All of them(that I saw) seem to just deal will event-based >> applications ie calculator, image viewer, etc. How do I run pyserial >> in the background and pass the information to PyQT and refresh the >> screen? Is there a way to have pyserial run in another thread and >> pass the information to the UI? >> > So far as I've experienced (which isn't all that much), most all GUI > frameworks support some method of defining either: 1) a work routine > which will be called whenever the input event queue is empty -- work > routines should be coded to do a very short, fast, bit of processing and > return, so the event dispatcher can continue (collect one character at a > time, or one line using a non-blocking I/O operation; or use a thread to > collect and post via a Queue) (wx.EVT_IDLE [bind to your idle handler] & > wx.WakeUpIdle() [call from thread after posting to queue]); I think I can say that idle processing isn't a common idiom in PyQt applications. After all, you can't guarantee that your application will be idle or be able to use up idle time in a way that you can rely on, and perhaps it isn't a good situation to be in if your framework is using up CPU time by idling. > 2) a timed-event which the dispatcher can call at periodic intervals (use > a thread to collect lines from the serial port, post the lines via a > Queue, and have the timed event check for queued data) (wx.Timer() ); In PyQt, the easiest way to do this is with a timer, connecting its timeout() signal to a slot (method) to perform some processing. A widget class might be adapted to do this in the following way: class MyWindow(QWidget): def __init__(self): QWidget.__init__(self) # ... other initialisation ... timer = QTimer(self) timer.timeout.connect(self.processData) # PyQt 4.5 signal notation timer.start(100) # every 100 milliseconds def processData(self): # Do your processing here. There are various ways to use timers with PyQt, but this is a fairly simple pattern to use. > 3) a callback interface to the GUI event dispatcher which can be invoked > from a separate thread (wx.CallAfter() ) I think at this point I would recommend using a separate thread. The idiom for using threads with PyQt is quite simple, though it helps to go with the framework and use QThread instead of Python's thread class. > You'll have to track down the equivalent QT functions (I have to > confess, I found the above from "wxPython in Action"; my last real GUI > coding that needed such was nearly 20 years ago, using xt and DECWindows > for the interface, and GKS on X for the data drawing, emulating an > archaic Ramtek graphics engine command set. The idle/work routine > collected commands sent via VMS mailbox to the "display" and generated > GKS operations for them). The people demand screenshots! ;-) David From timro21 at gmail.com Thu Jul 23 19:02:23 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 16:02:23 -0700 (PDT) Subject: integer square roots Message-ID: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> I wish to process billions of 100-digit numbers and test if each has an integer square root. What's the most efficient way to do this? From davea at ieee.org Thu Jul 23 19:03:14 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 23 Jul 2009 19:03:14 -0400 Subject: strange python scripting error In-Reply-To: References: Message-ID: <4A68EC32.4090907@ieee.org> Mark Tarver wrote: > I have a very strange error. I have two test python files test.py and > python.py which contain the following code > > #!/usr/bin/python > print "Content-type: text/html" > print > print "" > print "
Hello, Linux.com!
" > print "" > > One file (test.py) works; you call it up and it shows a web page with > > Hello, Linux.com > > The other fails with a server configuration error. Both are running > under Linux, same server, same permissions. Running a character scan > shows that both files contain the same printable characters and are > therefore typographically identical. They are absolutely the same. > > The only hint at a difference I can see is that my ftp program says > the files are of unequal lengths. test.py is 129 bytes long. > python.py 134 bytes long. > > A zipped folder containing both files is at > > www.lambdassociates.org/weird.zip > > Any ideas welcome. > > Mark > > Easiest explanation is that python.py has Windows-style newlines. In other words, each line ends with 0d0a, rather than the Unix convention of 0a. If your server is Unix-based, it can't handle that first line, since it has an illegal character (0d) following the #!/usr/bin/python line. Convert it to Unix line-endings. DaveA From gagsl-py2 at yahoo.com.ar Thu Jul 23 19:14:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 23 Jul 2009 20:14:07 -0300 Subject: Detect target name in descriptor __set__ method References: <74cd3059-cd88-418b-8444-766a125d93c3@y4g2000prf.googlegroups.com> Message-ID: En Thu, 23 Jul 2009 10:19:33 -0300, DG escribi?: > On Jul 22, 6:05?pm, "Gabriel Genellina" > wrote: >> En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James ? >> escribi?: >> >> > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina ? >> > wrote: >> >> >> class X(object): >> >> ? ?foo = descriptor() >> >> >> x = X() >> >> x.foo = "value" >> > You might've already thought of this (and it is annoying), but you > could pass the name through the descriptor's init method. I believe > this is the only way besides assigning a metaclass that will look for > that type of descriptor upon class creation and set the descriptor's > name at that time. > > class A(object): > def __init__(self, attr_name): > self._name = attr_name > def __set__(self, instance, value): > self.instance.__dict__[self._name] = value > # or something like that... > > class B(object): > foo = A('foo') Thanks, this seems to be the less "magical" solution. I don't like having to repeat the attribute name, but nothing is perfect... And thanks to Rainer Mansfeld too; looking up the matching attribute may be useful in other cases. -- Gabriel Genellina From pavlovevidence at gmail.com Thu Jul 23 19:30:54 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 16:30:54 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> <7xws60p7kk.fsf@ruckus.brouhaha.com> Message-ID: <6a3f1211-b551-4012-904c-0ed0a2c9f6f8@o32g2000yqm.googlegroups.com> On Jul 22, 5:27?pm, Paul Rubin wrote: > Carl Banks writes: > > I don't think your fantasy async-only all-green-thread langauge > > implementation is possible anyway. > > Erlang and GHC both work like that, quite successfully: > > ?http://shootout.alioth.debian.org/gp4/benchmark.php?test=threadring&l... I am not suggesting that you can't do a lot of things with async I/O. But can you do *everything* that native threads can do? I don't think so. So when you asked: "Why is that [native threading] such an advantage? Green threads work fine if you just organize the i/o system to never block." Well, the answer is they don't always work fine. Just organizing the I/O system never to block won't cut it, since things other than I/O block. For green threads to work fine all the time you have to be able organize your entire system never to block, and I don't think it's possible on the common OSes in use today. Until that happens, there will always be advantages to being able to use native threads on a single core. > > How would you wait on a pipe in one thread, a socket in another, a > > semaphore in a third? ? > > You can select on pipes and sockets, I think. ?Not sure about > semaphores. ? So you made a claim that "green threads work fine", and rhetorically suggested that native threads had no advantages, yet you're not even 100% sure you can select on both pipes and sockets (both common forms of I/O), let alone semaphores? [You can select on sockets and pipes in Unix, not in Windows, although on Windows I think there are other ways to do it besides the select call.] > > (Are there any popular OSes that offer a unified polling interface to > > all possible synchronizations?) ?And what do you do about drivers or > > libraries that make underlying blocking calls? ?What if you have a > > busy calculation going on in the background? > > I don't think the concept of "drivers" applies to user-mode programs. > For FFI calls you would use an OS thread. That's contrary to the hypothesis, isn't it? >?The language runtime > switches between busy computation threads on a timer tick. This would seem to result in a tradeoff between performance and low- latency. Well that happens at the OS-level too but the OS can respond to interrupts and task switch between timer ticks at finer intervals. Back in my DOS days I wrote user programs that responded to I/O interrupts, but it doesn't seem to be common practice in modern OSes to easily allow this. And Paul, if I'm being a little hard on you here, it's not that I'm taking issue with your own claim so much as with your dismissal of mine. Carl Banks From scriptlearner at gmail.com Thu Jul 23 19:39:29 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Thu, 23 Jul 2009 16:39:29 -0700 (PDT) Subject: passing data to a liburl2 opener object Message-ID: <89de6563-1016-461b-87b6-090754ca224b@r2g2000yqm.googlegroups.com> I have prepared my headers and data for a HTTP POST message; however, I am not sure how to pass the data to the opener. Can you guys provide some suggestions? Thanks. proxy_handler = urllib2.ProxyHandler({'http': 'http://my.proxy.com: 3128/'}) opener = urllib2.build_opener(proxy_handler) url = "http://sample.company.com:9999/service?" headers['Content-Type'] = 'multipart/form-data; boundary=%s' % myBoundar headers['Cookie'] = 'somevalidcookiecontents' #there is an opener.addheaders method for adding headers data = multipart_encode(myDict) #how to pass data to the opener??? With the codes above, I need to send out a HTTP POST like the following one: POST http://sample.company.com:9999/service? Content-Type: multipart/form-data; boundary=---------------------------1234 ---------------------------1234 Content-Disposition: form-data; name="picture" PICTURE contents here ---------------------------1234 Content-Disposition: form-data; name="id" whateverid ---------------------------1234-- From shai at platonix.com Thu Jul 23 19:51:43 2009 From: shai at platonix.com (Shai) Date: Thu, 23 Jul 2009 16:51:43 -0700 (PDT) Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> <87ljmogglz.fsf@benfinney.id.au> <02701ee5$0$5185$c3e8da3@news.astraweb.com> Message-ID: <056f629b-aa63-458a-ae16-ac40a759e446@h11g2000yqb.googlegroups.com> On Jul 17, 10:52?am, Steven D'Aprano wrote: > > When the decorator is called, the function object is just a function > object, not a method, so there is no concept of "what class it is > destined for". > ... which points to the better solution: use a descriptor. With the doc_inherit decorator defined below, one may write class Foo(object): def foo(self): "Frobber" pass class Bar(Foo): @doc_inherit def foo(self): pass and it appears to work. The code below is a little longish because we need to do slightly different things when called for a class and for an instance. But there's no need to repeat the parent name, no need to look into namespaces (which, as you said, is probably messy and fragile), and it seems pretty readable, too. from functools import wraps class DocInherit(object): """ Docstring inheriting method descriptor The class itself is also used as a decorator """ def __init__(self, mthd): self.mthd = mthd self.name = mthd.__name__ def __get__(self, obj, cls): if obj: return self.get_with_inst(obj, cls) else: return self.get_no_inst(cls) def get_with_inst(self, obj, cls): overridden = getattr(super(cls, obj), self.name, None) @wraps(self.mthd, assigned=('__name__','__module__')) def f(*args, **kwargs): return self.mthd(obj, *args, **kwargs) return self.use_parent_doc(f, overridden) def get_no_inst(self, cls): for parent in cls.__mro__[1:]: overridden = getattr(parent, self.name, None) if overridden: break @wraps(self.mthd, assigned=('__name__','__module__')) def f(*args, **kwargs): return self.mthd(*args, **kwargs) return self.use_parent_doc(f, overridden) def use_parent_doc(self, func, source): if source is None: raise NameError, ("Can't find '%s' in parents"%self.name) func.__doc__ = source.__doc__ return func doc_inherit = DocInherit Combining docstrings (as suggested by Jean-Paul Calderone), and taking proper care of classmethods and staticmethods, are left as an exercise to the reader. Have fun, Shai. From davea at ieee.org Thu Jul 23 19:52:22 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 23 Jul 2009 19:52:22 -0400 Subject: Bridging Python and C In-Reply-To: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> References: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Message-ID: <4A68F7B6.2020101@ieee.org> Mohan Parthasarathy wrote: > Hi, > I am a newbie. It looks like there are quite a few ways to bridge Python and > C. I have a bunch of C code and I just need Python wrappers for it. If i > google for this I get SWIG, Boost etc. And I also see > > http://www.python.org/doc/2.5.2/ext/intro.html > > What is the recommended way for doing this ? > > thanks > mohan > > I think the real question to ask is what form is your C code in. If you're in Windows, and it's compiled into DLL's with documented exports, then I'd start by using ctypes to access a few of the exports. If that seems too hard, you might try SWIG or another automated tool, to parse the headers and generate wrappers. But if you haven't done it first by hand, you might find the automated tools very hard to figure out. DaveA From http Thu Jul 23 19:57:10 2009 From: http (Paul Rubin) Date: 23 Jul 2009 16:57:10 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> <7xws60p7kk.fsf@ruckus.brouhaha.com> <6a3f1211-b551-4012-904c-0ed0a2c9f6f8@o32g2000yqm.googlegroups.com> Message-ID: <7x8wifymuh.fsf@ruckus.brouhaha.com> Carl Banks writes: > > I don't think the concept of "drivers" applies to user-mode programs. > > For FFI calls you would use an OS thread. > That's contrary to the hypothesis, isn't it? Yeah, it would have been better to say, green threads are ok for most typical forms of i/o concurrency, but OS threads are still necessary for some things. An FFI call in particular isn't exactly under the control of your language's runtime system; it has to be treated more like an external program. OS thread?switching is much more expensive than green thread switching, so it's still preferable to use green threads when possible. > >?The language runtime switches between busy computation threads on > >?a timer tick. > This would seem to result in a tradeoff between performance and low- > latency. GHC appears to use a 100 hz timer, which I'd expect to be fast enough for interactive applications while still not causing too much cpu load. I don't know if it has a way to assign priorities to microthreads (e.g. if you have one running your program's GUI) though. I don't think either Python or GHC can implement hard-real-time latency because they both use stop-the-world garbage collection. E.g. in Python, releasing the last reference to a large list or dict can take a long time. > And Paul, if I'm being a little hard on you here, it's not that I'm > taking issue with your own claim so much as with your dismissal of > mine. Well, the issue was why Python uses native threads pervasively. As far as I can tell, it's just an implementation artifact that has no really compelling justification. From mensanator at aol.com Thu Jul 23 20:11:54 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 23 Jul 2009 17:11:54 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> Message-ID: <73b8529d-23f4-41b4-a6cd-9f191f74bcd4@y19g2000yqy.googlegroups.com> On Jul 23, 6:02?pm, timro21 wrote: > I wish to process billions of 100-digit numbers and test if each has > an integer square root. ?What's the most efficient way to do this? Use gmpy. >>> import gmpy >>> help(gmpy.sqrt) Help on built-in function sqrt in module gmpy: sqrt(...) sqrt(x): returns the integer, truncated square root of x, i.e. the largest y such that x>=y*y. x must be an mpz, or else gets coerced to one; further, x must be >= 0. >>> p = 10**100 >>> p1 = gmpy.next_prime(p) >>> p1 mpz (10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000267) >>> gmpy.sqrt(p1) mpz(100000000000000000000000000000000000000000000000000) From mensanator at aol.com Thu Jul 23 20:17:53 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 23 Jul 2009 17:17:53 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <73b8529d-23f4-41b4-a6cd-9f191f74bcd4@y19g2000yqy.googlegroups.com> Message-ID: On Jul 23, 7:11?pm, Mensanator wrote: > On Jul 23, 6:02?pm, timro21 wrote: > > > I wish to process billions of 100-digit numbers and test if each has > > an integer square root. ?What's the most efficient way to do this? > > Use gmpy. > > >>> import gmpy > >>> help(gmpy.sqrt) > > Help on built-in function sqrt in module gmpy: > > sqrt(...) > ? ? sqrt(x): returns the integer, truncated square root of x, i.e. the > ? ? largest y such that x>=y*y. x must be an mpz, or else gets coerced > ? ? to one; further, x must be >= 0. > > >>> p = 10**100 > >>> p1 = gmpy.next_prime(p) > >>> p1 > > mpz > (10000000000000000000000000000000000000000000000000000000000000000000000000?000000000000000000000000267) > > >>> gmpy.sqrt(p1) > > mpz(100000000000000000000000000000000000000000000000000) Duh. I completely misread this. I thought you wanted the square root, not IF it had a square root that's an integer. Sorry about that. Try this, instead: >>> gmpy.sqrtrem(p1) (mpz(100000000000000000000000000000000000000000000000000), mpz(267)) It has a non-zero remainder, so it's not an integer square root. From aahz at pythoncraft.com Thu Jul 23 20:25:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 23 Jul 2009 17:25:16 -0700 Subject: Help understanding the decisions *behind* python? References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: In article , Mark Lawrence wrote: > >Sorry if this has been discussed and I've missed it, but how about >memory allocation. An immutable tuple has a fixed memory allocation >whereas that for the mutable list must be liable to change. You might >like to look at the recent thread on this ng 'List insertion cost' and >follow the links to Raymond Hettinger's power point presentation. Not only that, but lists use more memory, period, for any given number of elements because the overallocation permits amortized constant time for appends. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From aahz at pythoncraft.com Thu Jul 23 20:27:35 2009 From: aahz at pythoncraft.com (Aahz) Date: 23 Jul 2009 17:27:35 -0700 Subject: invoke method on many instances References: Message-ID: In article , Gabriel Genellina wrote: > >NLMPI What? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From mensanator at aol.com Thu Jul 23 20:30:34 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 23 Jul 2009 17:30:34 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <73b8529d-23f4-41b4-a6cd-9f191f74bcd4@y19g2000yqy.googlegroups.com> Message-ID: On Jul 23, 7:17?pm, Mensanator wrote: > On Jul 23, 7:11?pm, Mensanator wrote: > > > > > > > On Jul 23, 6:02?pm, timro21 wrote: > > > > I wish to process billions of 100-digit numbers and test if each has > > > an integer square root. ?What's the most efficient way to do this? > > > Use gmpy. > > > >>> import gmpy > > >>> help(gmpy.sqrt) > > > Help on built-in function sqrt in module gmpy: > > > sqrt(...) > > ? ? sqrt(x): returns the integer, truncated square root of x, i.e. the > > ? ? largest y such that x>=y*y. x must be an mpz, or else gets coerced > > ? ? to one; further, x must be >= 0. > > > >>> p = 10**100 > > >>> p1 = gmpy.next_prime(p) > > >>> p1 > > > mpz > > (10000000000000000000000000000000000000000000000000000000000000000000000000??000000000000000000000000267) > > > >>> gmpy.sqrt(p1) > > > mpz(100000000000000000000000000000000000000000000000000) > > Duh. I completely misread this. I thought you wanted the square root, > not IF it had a square root that's an integer. > > Sorry about that. > > Try this, instead: > > >>> gmpy.sqrtrem(p1) > > (mpz(100000000000000000000000000000000000000000000000000), mpz(267)) > > It has a non-zero remainder, so it's not an integer square root. There's also is_square(...) is_square(x): returns 1 if x is a perfect square, else 0. x must be an mpz, or else gets coerced to one. From http Thu Jul 23 20:35:27 2009 From: http (Paul Rubin) Date: 23 Jul 2009 17:35:27 -0700 Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> Message-ID: <7x4ot2zzn4.fsf@ruckus.brouhaha.com> timro21 writes: > I wish to process billions of 100-digit numbers and test if each has > an integer square root. What's the most efficient way to do this? Is this a homework problem? If not, may I ask what the application is? I think the basic answer is 1) use the law of quadratic reciprocity to eliminate a lot of the inputs quickly (for example, just looking at the low 8 bits of an input number should eliminate about 5/6th of them); 2) use GMP and Newton's method to check the rest. http://cr.yp.to/papers/powers.pdf has some advice about how to do that. sci.crypt might be another good place to ask this question. From songofacandy at gmail.com Thu Jul 23 20:49:26 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Thu, 23 Jul 2009 17:49:26 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> Message-ID: <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> > What is the encoding of sys.stderr in your example? Sorry, I missed. It is cp932 > So the problem is essentially this: if a stream has an encoding > attribute, sometimes it is a wrapped stream which encodes Unicode > (e.g. a stream obtained via the codecs module) and sometimes it is not > (e.g. sys.stdout, sys.stderr). Yes! I confused by it. >> The encoding that this file uses. When Unicode strings are written to a file, >> they will be converted to byte strings using this encoding. In addition, >> when the file is connected to a terminal, the attribute gives the encoding >> that the terminal is likely to use I feel this doc means "file object with encoding attribute encodes unicode regardless it is tty or not" and sys.stdout/stderr defies convention. If this doc means "file object encodes unicode if it isn't tty.", I should write like below:: if not afile.isatty(): if getattr(afile, "encoding") is not None: afile.write(unicode_str) elif getattr(afile, "encoding") is not None: afile.write(unicode_str.encode(afile.encoding)) else: afile.write(unicode_str.encode(fallback_encoding)) # utf8, defaultencoding, preferedencoding, ... "Writing unicode to a file(-like)" is a simple requirement. Does python have any simple resolution for it? From rui.maciel at gmail.com Thu Jul 23 20:52:00 2009 From: rui.maciel at gmail.com (Rui Maciel) Date: Fri, 24 Jul 2009 01:52 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <4a6905b0$0$27110$a729d347@news.telepac.pt> fft1976 wrote: > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. Probably due to similar reasons that lead php to become remotely relevant. Rui Maciel From timro21 at gmail.com Thu Jul 23 21:00:20 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 18:00:20 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> Message-ID: On Jul 24, 10:35?am, Paul Rubin wrote: > timro21 writes: > > I wish to process billions of 100-digit numbers and test if each has > > an integer square root. ?What's the most efficient way to do this? > > Is this a homework problem? ?If not, may I ask what the application is? > > I think the basic answer is 1) use the law of quadratic reciprocity to > eliminate a lot of the inputs quickly (for example, just looking at > the low 8 bits of an input number should eliminate about 5/6th of > them); 2) use GMP and Newton's method to check the rest.http://cr.yp.to/papers/powers.pdfhas some advice about how to do > that. > > sci.crypt might be another good place to ask this question. Homework? Gosh no. I have several number theory applications which need to know (quickly) whether particular very large numbers are perfect squares. Since I asked this in this newsgroup I guess I assumed that responses wuld relate specifically to how to do this efficiently *and accurately* in Python. Sorry if I wasn't clear. From dstanek at dstanek.com Thu Jul 23 21:23:06 2009 From: dstanek at dstanek.com (David Stanek) Date: Thu, 23 Jul 2009 21:23:06 -0400 Subject: sqlite3 performance problems only in python In-Reply-To: <4A68E454.8040909@gmail.com> References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A68E454.8040909@gmail.com> Message-ID: On Thu, Jul 23, 2009 at 6:29 PM, Stef Mientki wrote: > > but because the same SQL-statement in Delphi performed well, > I thought it was a problem with the Python implementation. Same SQL, but were you also using Sqlite in Delphi? -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From http Thu Jul 23 21:51:01 2009 From: http (Paul Rubin) Date: 23 Jul 2009 18:51:01 -0700 Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> Message-ID: <7xzlaudf22.fsf@ruckus.brouhaha.com> timro21 writes: > Homework? Gosh no. I have several number theory applications which > need to know (quickly) whether particular very large numbers are > perfect squares. Since I asked this in this newsgroup I guess I > assumed that responses wuld relate specifically to how to do this > efficiently *and accurately* in Python. Sorry if I wasn't clear. comp.lang.python is a good place to get answers about Python. It's probably not such a good source of answers about computational number theory. Also, Python is more about productivity than speed, so answers involving Python may not be the most efficient possible answers. One obvious point though is that GMP/gmpy is pretty simple to use from Python and will be several times faster than Python's built in longs. Also, as Mensanator pointed out, GMP has built-in functions that will help you with precise checks (I'd forgotten or not known about them). I still think you'd get a speedup from first filtering out obviously non-square candidates before resorting to multi-precision arithmetic. Some other fairly simple optimization may be possible too. I asked about the application mainly because I wondered if you had realtime requirements, whether you were going to have to keep doing this problem with new numbers, etc. If you just have a 1-off task of checking a few billion numbers, you could probably do it in a few days with Python on a workstation using some fairly simple code. If you want to check billions of numbers per minute, or if what you really want is to check trillions of numbers rather than billions, then it's worth pursuing fancier methods. Another issue is the source of the numbers. E.g. maybe there is a way to program a graphics accelerator to turn a number into a list of residues mod a bunch of small primes in parallel and get a fast, near-certain test if the inputs are random, but that approach could be fooled if the numbers were concocted by a possible adversary. From casevh at gmail.com Thu Jul 23 22:04:50 2009 From: casevh at gmail.com (casevh) Date: Thu, 23 Jul 2009 19:04:50 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> Message-ID: > comp.lang.python is a good place to get answers about Python. ?It's > probably not such a good source of answers about computational number > theory. ?Also, Python is more about productivity than speed, so > answers involving Python may not be the most efficient possible > answers. ?One obvious point though is that GMP/gmpy is pretty simple > to use from Python and will be several times faster than Python's > built in longs. ?Also, as Mensanator pointed out, GMP has built-in > functions that will help you with precise checks (I'd forgotten or not > known about them). ?I still think you'd get a speedup from first > filtering out obviously non-square candidates before resorting to > multi-precision arithmetic. ?Some other fairly simple optimization may > be possible too. > gmpy.is_square() is quite fast. On a older 32-bit Linux box, it can test approximately 400,000 100-digits numbers per second. The time includes conversion from a string. If the numbers are already Python longs, it can check 800,000 per second. Checking a billion is not unreasonable. casevh From http Thu Jul 23 22:48:12 2009 From: http (Paul Rubin) Date: 23 Jul 2009 19:48:12 -0700 Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> Message-ID: <7xocra7q4z.fsf@ruckus.brouhaha.com> casevh writes: > gmpy.is_square() is quite fast. On a older 32-bit Linux box, it can > test approximately 400,000 100-digits numbers per second. The time > includes conversion from a string. If the numbers are already Python > longs, it can check 800,000 per second. Checking a billion is not > unreasonable. Wow, that is pretty impressive. A look at the code shows it uses quite a few optimizations: http://sage.math.washington.edu/home/novoselt/gmp-4.2.4/mpn/perfsqr.c From songofacandy at gmail.com Thu Jul 23 23:10:10 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Thu, 23 Jul 2009 20:10:10 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: > > Yes! I confused by it. s/I confused/I am confused/ > "Writing unicode to a file(-like)" is a simple requirement. > Does python have any simple resolution for it? s/resolution/solution/ Sorry about my poor English. From pavlovevidence at gmail.com Thu Jul 23 23:11:23 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 20:11:23 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> <7xws60p7kk.fsf@ruckus.brouhaha.com> <6a3f1211-b551-4012-904c-0ed0a2c9f6f8@o32g2000yqm.googlegroups.com> <7x8wifymuh.fsf@ruckus.brouhaha.com> Message-ID: On Jul 23, 4:57?pm, Paul Rubin wrote: > Carl Banks writes: > > > I don't think the concept of "drivers" applies to user-mode programs. > > > For FFI calls you would use an OS thread. > > That's contrary to the hypothesis, isn't it? > > Yeah, it would have been better to say, green threads are ok for most > typical forms of i/o concurrency, but OS threads are still necessary > for some things. ?An FFI call in particular isn't exactly under the > control of your language's runtime system; it has to be treated > more like an external program. > > OS thread?switching is much more expensive than green thread > switching, so it's still preferable to use green threads when > possible. That is reasonable. Thank you. > > And Paul, if I'm being a little hard on you here, it's not that I'm > > taking issue with your own claim so much as with your dismissal of > > mine. > > Well, the issue was why Python uses native threads pervasively. ?As > far as I can tell, it's just an implementation artifact that has no > really compelling justification. Probably for this reason: "Because then they didn't have to arrange for the I/O to never block." Carl Banks From timro21 at gmail.com Thu Jul 23 23:30:25 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 20:30:25 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> Message-ID: <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Thanks to all! Tim From pavlovevidence at gmail.com Thu Jul 23 23:51:02 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 20:51:02 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On Jul 23, 5:52?pm, Rui Maciel wrote: > fft1976 wrote: > > How do you explain that something as inferior as Python beat Lisp in > > the market place despite starting 40 years later. > > Probably due to similar reasons that lead php to become remotely relevant. Well, the only reason PHP became relevant because it was an easy to deploy solution in a single application domain, the web, that happened to explode. The same thing happened to Python. Oh, wait, no it didn't. Carl Banks From tundra at tundraware.com Thu Jul 23 23:51:26 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Thu, 23 Jul 2009 22:51:26 -0500 Subject: integer square roots In-Reply-To: <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Message-ID: timro21 wrote: > Thanks to all! > > Tim While we're at it, would you mind saying more about what exactly you're doing - Inquiring Math Dorks (tm) want to know ... -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From icebergwtf at gmail.com Thu Jul 23 23:58:04 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Fri, 24 Jul 2009 11:58:04 +0800 Subject: extract c/cpp include file with regular expression In-Reply-To: References: Message-ID: <4314c1f70907232058t4d295a25t86ba11d90e15001d@mail.gmail.com> Thanks, Philip Semanchuk and Nick Craig-Wood! My goal is gather informations from cpp sources and generate cross referenced html files (as more detail as fine) So I think " missing some unusual cases" might be acceptable, but informations you introduced make me more interests to dig things out :) I know some programs like doxygen do such things, but I can learn much by doing this than simply write configuration files. cheers! tiefeng wu 2009-07-24 From rurpy at yahoo.com Fri Jul 24 00:19:52 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 23 Jul 2009 21:19:52 -0700 (PDT) Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: Nick Dumas wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Agreed. Two string.split()s, first at the semi-colon and then at the > equal sign, will yield you your value, without having to fool around > with regexes. > > On 7/23/2009 9:23 AM, Mark Lawrence wrote: >> scriptlearner at gmail.com wrote: >>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >>> will like to take out the values (valuea, valueb, and valuec). How do >>> I do that in Python? The group method will only return the matched >>> part. Thanks. >>> >>> p = re.compile('#a=*;b=*;c=*;') >>> m = p.match(line) >>> if m: >>> print m.group(), >> >> IMHO a regex for this is overkill, a combination of string methods such >> as split and find should suffice. You're saying that something like the following is better than the simple regex used by the OP? [untested] values = [] parts = line.split(';') if len(parts) != 4: raise SomeError() for p, expected in zip (parts[-1], ('#a','b','c')): name, x, value = p.partition ('=') if name != expected or x != '=': raise SomeError() values.append (value) print values[0], values[1], values[2] Blech, not in my book. The regex checks the format of the string, extracts the values, and does so very clearly. Further, it is easily adapted to other similar formats, or evolutionary changes in format. It is also (once one is familiar with regexes -- a useful skill outside of Python too) easier to get right (at least in a simple case like this.) The only reason I can think of to prefer a split-based solution is if this code were performance-critical in that I would expect the split code to be faster (although I don't know that for sure.) This is a perfectly fine use of a regex. From d at vidr.cc Fri Jul 24 00:27:10 2009 From: d at vidr.cc (David Roberts) Date: Fri, 24 Jul 2009 14:27:10 +1000 Subject: OverflowError in RLock.acquire() Message-ID: Hi, I'm trying to port a Python application to Windows, and I'm getting the following error (which did not occur when running on Linux): Exception in thread Thread-4: Traceback (most recent call last): File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner self.run() File "C:\Documents and Settings\David\My Documents\pyzui\pyzui\tileprovider.py", line 97, in run self.__tilecache[tile_id] = Tile(tile) File "C:\Documents and Settings\David\My Documents\pyzui\pyzui\tilecache.py", line 165, in __setitem__ with self.__lock: File "C:\Python26\lib\threading.py", line 115, in acquire me = current_thread() File "C:\Python26\lib\threading.py", line 803, in currentThread return _active[_get_ident()] OverflowError: can't convert negative value to unsigned long Where __lock is an RLock object. The error only occurs for a single class (which is derived from the TileProvider class in tileprovider.py, which in turn is derived from threading.Thread), which would lead me to believe that there's an error in my code, but the traceback doesn't help much, and I haven't been able to find any similar problems with google. Any help would be appreciated. -- David Roberts http://da.vidr.cc/ From d at vidr.cc Fri Jul 24 00:41:01 2009 From: d at vidr.cc (David Roberts) Date: Fri, 24 Jul 2009 14:41:01 +1000 Subject: OverflowError in RLock.acquire() In-Reply-To: References: Message-ID: I forgot to mention, Python version is 2.6.2. -- David Roberts http://da.vidr.cc/ On Fri, Jul 24, 2009 at 14:27, David Roberts wrote: > Hi, > > I'm trying to port a Python application to Windows, and I'm getting > the following error (which did not occur when running on Linux): > > Exception in thread Thread-4: > Traceback (most recent call last): > ?File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner > ? ?self.run() > ?File "C:\Documents and Settings\David\My > Documents\pyzui\pyzui\tileprovider.py", line 97, in run > ? ?self.__tilecache[tile_id] = Tile(tile) > ?File "C:\Documents and Settings\David\My > Documents\pyzui\pyzui\tilecache.py", line 165, in __setitem__ > ? ?with self.__lock: > ?File "C:\Python26\lib\threading.py", line 115, in acquire > ? ?me = current_thread() > ?File "C:\Python26\lib\threading.py", line 803, in currentThread > ? ?return _active[_get_ident()] > OverflowError: can't convert negative value to unsigned long > > Where __lock is an RLock object. > > The error only occurs for a single class (which is derived from the > TileProvider class in tileprovider.py, which in turn is derived from > threading.Thread), which would lead me to believe that there's an > error in my code, but the traceback doesn't help much, and I haven't > been able to find any similar problems with google. > > Any help would be appreciated. > > -- > David Roberts > http://da.vidr.cc/ > From timro21 at gmail.com Fri Jul 24 00:43:48 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 21:43:48 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Message-ID: Hi Tim, sure, I'm looking at the perfect cuboid problem. I've just spent a couple of very frustrating hours. Given that I'm in my 50's but have the brain of a retarded 3-year-old, can someone please explain what I have to do to download gmpy to use with ActivePython 2.6 on a Windows system? I downloaded (or so I thought) GMPY 1.04 from http://code.google.com/p/gmpy/ but all the files look way too small...and then how do I import it into a Python program...I'm sure I could do this if it wasn't for my extreme stupidity... From casevh at gmail.com Fri Jul 24 01:12:23 2009 From: casevh at gmail.com (casevh) Date: Thu, 23 Jul 2009 22:12:23 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Message-ID: <0fc47aab-eb53-4e0a-be6d-9736883db888@i8g2000pro.googlegroups.com> On Jul 23, 9:43?pm, timro21 wrote: > Hi Tim, sure, I'm looking at the perfect cuboid problem. > > I've just spent a couple of very frustrating hours. ?Given that I'm in > my 50's but have the brain of a retarded 3-year-old, can someone > please explain what I have to do to download gmpy to use with > ActivePython 2.6 on a Windows system? ?I downloaded (or so I thought) > GMPY 1.04 fromhttp://code.google.com/p/gmpy/but all the files look > way too small...and then how do I import it into a Python > program...I'm sure I could do this if it wasn't for my extreme > stupidity... You should just download the Windows installer from the Downloads tab. I would recommend version 1.10 since it should be faster and fixes some bugs in 1.04. The direct link is http://gmpy.googlecode.com/files/gmpy-1.10-beta.win32-py2.6.exe Just run the installer and it should automatically detect your existing Python installation. The executable is small. ;-) casevh p.s. If you're using a 64-bit Windows system, let me know and I'll step you through the manual process. From Scott.Daniels at Acm.Org Fri Jul 24 01:20:27 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 23 Jul 2009 22:20:27 -0700 Subject: Issue combining gzip and subprocess In-Reply-To: References: <2YednUGQxvntgfrXnZ2dnUVZ_hWdnZ2d@pdx.net> Message-ID: Piet van Oostrum wrote: >>>>>> Scott David Daniels (SDD) schreef: > ... >> SDD> Or even: >> SDD> proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) >> SDD> with gzip.open(filename, 'w') as dest: >> SDD> for line in iter(proc.stdout, ''): >> SDD> f.write(line) > > If it would work. > > 1) with gzip... is not supported in Python < 3.1 > 2) for line in iter(proc.stdout), i.e. no second argument. > 3) dest <==> f should be the same identifier. > > Lesson: if you post code either: > - test it and copy verbatim from your test, or > - write a disclaimer Totally chagrined and embarassed. Chastisement absorbed and acknowledged. --Scott David Daniels Scott.Daniels at Acm.Org From timro21 at gmail.com Fri Jul 24 01:36:42 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 22:36:42 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> <0fc47aab-eb53-4e0a-be6d-9736883db888@i8g2000pro.googlegroups.com> Message-ID: <4132e60e-f586-42d1-b0fc-460aecf6a474@d9g2000prh.googlegroups.com> Bloody 'ell - thanks. :-) From girish.cfc at gmail.com Fri Jul 24 02:11:11 2009 From: girish.cfc at gmail.com (Girish) Date: Thu, 23 Jul 2009 23:11:11 -0700 (PDT) Subject: Registering DLL Message-ID: <071d3f78-a14b-4ea1-96fd-0f10d9dc2b80@d36g2000prb.googlegroups.com> Hello, I am facing a problem while registering DLL(Dspace.dll).. I am getting an error saying: "DLLRegisterServer inDspace.dll Failed. Return Code: 0x80040201" I am using following piece of code to generate DLL: ######################################################## # This setup script builds a single-file Python inprocess COM server. from distutils.core import setup import py2exe import sys # If run without args, build executables, in quiet mode. if len(sys.argv) == 1: sys.argv.append("py2exe") sys.argv.append("-q") class Target: def __init__(self, **kw): self.__dict__.update(kw) # for the versioninfo resources self.version = "1.0" self.company_name = "None" self.copyright = "None" self.name = "API" interp = Target( description = "COM server module", # what to build. For COM servers, the module name (not the # filename) must be specified! modules = ["Dspace"], #script = ["Dspace"], # we only want the inproc server. create_exe = True, ) excludes = ["pywin", "pywin.debugger", "pywin.debugger.dbgcon", "pywin.dialogs", "pywin.dialogs.list", 'email.Generator', 'email.Iterators', 'email.Utils'] options = { "bundle_files": 1, "ascii": 1, # to make a smaller executable, don't include the encodings "compressed": 1, # compress the library archive "excludes": excludes, # COM stuff we don't want } setup( options = {"py2exe": options}, zipfile = None, # append zip-archive to the executable. com_server = [interp], ) ################################################################ I am trying to generate a DLL from Python COM server module. I am able to register the module by running the python code, but when I generate a DLL and then try to register it using Regsvr32 command, I am getting above mentioned error Please help me resolve this issue. -Girish From pfeldman at verizon.net Fri Jul 24 02:35:43 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Thu, 23 Jul 2009 23:35:43 -0700 (PDT) Subject: len() should always return something Message-ID: <24639361.post@talk.nabble.com> Some aspects of the Python design are remarkably clever, while others leave me perplexed. Here's an example of the latter: Why does len() give an error when applied to an int or float? len() should always return something; in particular, when applied to a scalar, it should return a value of 1. Of course, I can define my own function like this: def mylen(x): if isinstance(x,int) or isinstance(x,float): return 1 return len(x) But, this shouldn't be necessary. -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24639361.html Sent from the Python - python-list mailing list archive at Nabble.com. From deets at nospam.web.de Fri Jul 24 02:57:44 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 08:57:44 +0200 Subject: len() should always return something In-Reply-To: References: Message-ID: <7ct4b8F29mednU1@mid.uni-berlin.de> Dr. Phillip M. Feldman schrieb: > Some aspects of the Python design are remarkably clever, while others leave > me perplexed. Here's an example of the latter: Why does len() give an error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. Of > course, I can define my own function like this: > > def mylen(x): > if isinstance(x,int) or isinstance(x,float): return 1 > return len(x) > > But, this shouldn't be necessary. Can you show some example of where that is actually making a piece of code more elegant? Diez From clp2 at rebertia.com Fri Jul 24 03:02:28 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 00:02:28 -0700 Subject: len() should always return something In-Reply-To: <24639361.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> Message-ID: <50697b2c0907240002i1721d7a4y54d618782ca1a071@mail.gmail.com> On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. Feldman wrote: > > Some aspects of the Python design are remarkably clever, while others leave > me perplexed. Here's an example of the latter: Why does len() give an error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. Of > course, I can define my own function like this: > > def mylen(x): > ? if isinstance(x,int) or isinstance(x,float): return 1 > ? return len(x) > > But, this shouldn't be necessary. The problem is that redefining len()/length/size that way would violate several principles of Python's design (The "Zen" of Python - http://www.python.org/dev/peps/pep-0020/). Specifically: - Explicit is better than implicit. - Special cases aren't special enough to break the rules. - Errors should never pass silently. - In the face of ambiguity, refuse the temptation to guess. If you'd explain the situation that prompts you to find this redefinition necessary, I'm sure someone can suggest a better approach. Cheers, Chris -- http://blog.rebertia.com From __peter__ at web.de Fri Jul 24 03:07:03 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 09:07:03 +0200 Subject: len() should always return something References: Message-ID: Dr. Phillip M. Feldman wrote: > Some aspects of the Python design are remarkably clever, while others > leave me perplexed. Here's an example of the latter: Why does len() give > an error when applied to an int or float? len() should always return > something; in particular, when applied to a scalar, it should return a > value of 1. Of course, I can define my own function like this: > > def mylen(x): > if isinstance(x,int) or isinstance(x,float): return 1 > return len(x) > > But, this shouldn't be necessary. Python should not blur the distinction between vectors an scalars like that. Instead of trying to be clever you should pass a vector with a single item and send mylen() to /dev/null. On a general note, I think one of Python's strengths is that it consistently /avoids/ this kind of cleverness. A prominent example is the handling of "1" + 1. Peter From nick at craig-wood.com Fri Jul 24 03:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Fri, 24 Jul 2009 02:29:57 -0500 Subject: Looking for os.listdir() generator References: Message-ID: Christian Heimes wrote: > Nick Craig-Wood wrote: > > Christian Heimes wrote: > >> I'm looking for a generator version of os.listdir() for Python 2.5 and > >> newer. I know somebody has worked on it because I've seen a generator > >> version in a posting on some list or blog a while ago. I can't find it > >> anymore. It seems my Google fu is lacking today. All I can find is a > >> very old version of xlistdir. A Cython based solution is appreciated but > >> please no ctypes version. > > > > I posted exactly that yesterday I think ;-) > > > > Note that this has a python part as well as a ctypes part because the > > version of ctypes I used doesn't support generators. I think the > > development version does though. > > Thanks Nick! > > ctypes? I'm sure you wanted to say Cython :) Er, yes! > If you don't mind I'm going to wrap it up into a nice package and > release it on PyPI. Yes thats fine with me! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From wilk at flibuste.net Fri Jul 24 04:04:03 2009 From: wilk at flibuste.net (William Dode) Date: 24 Jul 2009 08:04:03 GMT Subject: ANN: psyco V2 References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> Message-ID: <4a696af3$0$442$426a74cc@news.free.fr> On 23-07-2009, Christian Tismer wrote: > On 7/17/09 4:11 AM, Bearophile wrote: >> Very good, thank you. I'll try it when I can. >> >> Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow? > > Psyco3: nice typo! :-) > > Well, I haven't so far found a new idea there that I'd want > to borrow and did not know from PyPy, before. > Wasn't the project plan saying the opposite, borrowing > some ideas from psyco? :-) > http://code.google.com/p/unladen-swallow/wiki/ProjectPlan How do you see the futur of psyco when unladen-swallow will grab the best of psyco (if they can !) ? Wait and see ? Anyway, thanks a lot for your work that we can use NOW ! -- William Dod? - http://flibuste.net Informaticien Ind?pendant From hendrik at microcorp.co.za Fri Jul 24 04:13:02 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 24 Jul 2009 10:13:02 +0200 Subject: Gedcom and Genealogy In-Reply-To: <5222a601-a128-4892-9a8d-e1942ac9ce4d@e27g2000yqm.googlegroups.com> References: <5222a601-a128-4892-9a8d-e1942ac9ce4d@e27g2000yqm.googlegroups.com> Message-ID: <200907241013.02795.hendrik@microcorp.co.za> On Friday 24 July 2009 00:14:19 Gordon wrote: > We have many small libraries in JAVA or Ruby that need to be ported to > Python. Actually it's so simple a rewrite is possible too. Is this: 1 - A question? 2 - A job offer? 3 - A piece of random news? - Hendrik From mwawrzyczek at gmail.com Fri Jul 24 04:35:52 2009 From: mwawrzyczek at gmail.com (marekw2143) Date: Fri, 24 Jul 2009 01:35:52 -0700 (PDT) Subject: Adding method from one class to another class or to instance of another class Message-ID: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Hi, I have one class (A) that has defined method createVars. I would like to add that method to class B The code looks like this: class A(object): def createVars(self): self.v1 = 1 self.v2 = 3 pass class B(object): pass I don't want to use inheritance (because class A has many methods defined that class B doesn't need). When I try the folloowing: B.createVars = C.createVars B().createVars() then the following error occurs: Traceback (most recent call last): File "", line 1, in TypeError: unbound method createVars() must be called with A instance as first argument (got nothing instead) When I try to add the createVars method to instance of B: >>> b=B() >>> b.createVars = new.instancemethod(A.createVars, b, B) >>> b.createVars > >>> b.createVars() Then the following error raises: Traceback (most recent call last): File "", line 1, in TypeError: unbound method createVars() must be called with A instance as first argument (got B instance instead) How can I solve this problem? Regards, Marek From Slaunger at gmail.com Fri Jul 24 04:38:37 2009 From: Slaunger at gmail.com (Slaunger) Date: Fri, 24 Jul 2009 01:38:37 -0700 (PDT) Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 Message-ID: OS: Win XP SP3, 32 bit Python 2.5.4 Hi I have run into some problems with allocating numpy.memmaps exceeding and accumulated size of about 2 GB. I have found out that the real problem relates to numpy.memmap using mmap.mmap I've written a small test program to illustrate it: import itertools import mmap import os files = [] mmaps = [] file_names= [] mmap_cap=0 bytes_per_mmap = 100 * 1024 ** 2 try: for i in itertools.count(1): file_name = "d:/%d.tst" % i file_names.append(file_name) f = open(file_name, "w+b") files.append(f) mm = mmap.mmap(f.fileno(), bytes_per_mmap) mmaps.append(mm) mmap_cap += bytes_per_mmap print "Created %d writeable mmaps containing %d MB" % (i, mmap_cap/(1024**2)) #Clean up finally: print "Removing mmaps..." for mm, f, file_name in zip(mmaps, files, file_names): mm.close() f.close() os.remove(file_name) print "Done..." which creates this output Created 1 writeable mmaps containing 100 MB Created 2 writeable mmaps containing 200 MB .... Created 17 writeable mmaps containing 1700 MB Created 18 writeable mmaps containing 1800 MB Removing mmaps... Done... Traceback (most recent call last): File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha \mmaptest.py", line 16, in mm = mmap.mmap(f.fileno(), bytes_per_mmap) WindowsError: [Error 8] Not enough storage is available to process this command There is more than 25 GB of free space on drive d: at this stage. Is it a bug or a "feature" of the 32 bit OS? I am surprised about it as I have not found any notes about these kinds of limitations in the documentation. I am in dire need of these large memmaps for my task, and it is not an option to change OS due to other constraints in the system. Is there anything I can do about it? Best wishes, Kim From manu3d at gmail.com Fri Jul 24 04:55:03 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Fri, 24 Jul 2009 01:55:03 -0700 (PDT) Subject: import vs imp and friends. References: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Message-ID: Christian, Robert, thank you both for the replies, much appreciated. Manu From vinay_sajip at yahoo.co.uk Fri Jul 24 05:14:13 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Fri, 24 Jul 2009 02:14:13 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: <61b029e2-494f-4c87-871f-2469a88aaf91@o6g2000yqj.googlegroups.com> On Jul 24, 4:10?am, Naoki INADA wrote: > > Yes! I confused by it. > > s/I confused/I am confused/ > > > "Writing unicode to a file(-like)" is a simple requirement. > > Does python have any simple resolution for it? > > s/resolution/solution/ > Of course, Python 3 has much better Unicode support: --------------------------------------------------------------------- C:\Users\Vinay>chcp 1251 Active code page: 1251 C:\Users\Vinay>\python31\python ActivePython 3.1.0.1 (ActiveState Software Inc.) based on Python 3.1 (r31:73572, Jun 28 2009, 19:55:39) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.stdout.encoding 'cp1251' >>> u = '\u0434\u043e \u0441\u0432\u0438\u0434\u0430\u043d\u0438\u044f' >>> print(u) ?? ???????? >>> n = sys.stdout.write(u) ?? ????????>>> ^Z --------------------------------------------------------------------- Regards, Vinay Sajip From piet at cs.uu.nl Fri Jul 24 05:37:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 11:37:21 +0200 Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: >>>>> Naoki INADA (NI) wrote: >NI> "Writing unicode to a file(-like)" is a simple requirement. >NI> Does python have any simple resolution for it? Yes, Python 3 will do this. For Python < 3.0 you will have to use a codecs wrapper or explicitely do the encoding. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From deets at nospam.web.de Fri Jul 24 05:38:05 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 11:38:05 +0200 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 In-Reply-To: References: Message-ID: <7ctdo0F28litnU1@mid.uni-berlin.de> Slaunger schrieb: > OS: Win XP SP3, 32 bit > Python 2.5.4 > > Hi I have run into some problems with allocating numpy.memmaps > exceeding and accumulated size of about 2 GB. I have found out that > the real problem relates to numpy.memmap using mmap.mmap > > I've written a small test program to illustrate it: > > import itertools > import mmap > import os > > files = [] > mmaps = [] > file_names= [] > mmap_cap=0 > bytes_per_mmap = 100 * 1024 ** 2 > try: > for i in itertools.count(1): > file_name = "d:/%d.tst" % i > file_names.append(file_name) > f = open(file_name, "w+b") > files.append(f) > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > mmaps.append(mm) > mmap_cap += bytes_per_mmap > print "Created %d writeable mmaps containing %d MB" % (i, > mmap_cap/(1024**2)) > > #Clean up > finally: > print "Removing mmaps..." > for mm, f, file_name in zip(mmaps, files, file_names): > mm.close() > f.close() > os.remove(file_name) > print "Done..." > > > which creates this output > > Created 1 writeable mmaps containing 100 MB > Created 2 writeable mmaps containing 200 MB > .... > Created 17 writeable mmaps containing 1700 MB > Created 18 writeable mmaps containing 1800 MB > Removing mmaps... > Done... > Traceback (most recent call last): > File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha > \mmaptest.py", line 16, in > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > WindowsError: [Error 8] Not enough storage is available to process > this command > > There is more than 25 GB of free space on drive d: at this stage. > > Is it a bug or a "feature" of the 32 bit OS? It's a limitation, yes. That's what 64-bit-OSes are for. > I am surprised about it as I have not found any notes about these > kinds of limitations in the documentation. > > I am in dire need of these large memmaps for my task, and it is not an > option to change OS due to other constraints in the system. > > Is there anything I can do about it? Only by partitioning data yourself, and accessing these partitions. Like in the good old days of DOS-programming. Diez From piet at cs.uu.nl Fri Jul 24 05:38:51 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 11:38:51 +0200 Subject: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox) References: Message-ID: >>>>> Chris Rebert (CR) wrote: >CR> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: >>> You can use tabnanny to help diagnose the problem: >>> http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a >CR> Anyone know what's the deal with effbot.org? It seems to be broken at >CR> present, forcing me to use Google's cached version. >CR> It's a real shame since it has lots of handy Python info. Just try again. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From __peter__ at web.de Fri Jul 24 05:38:57 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 11:38:57 +0200 Subject: Adding method from one class to another class or to instance of another class References: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Message-ID: marekw2143 wrote: > Hi, > > I have one class (A) that has defined method createVars. I would like > to add that method to class B > The code looks like this: > > > class A(object): > def createVars(self): > self.v1 = 1 > self.v2 = 3 > pass > > class B(object): > pass > > > I don't want to use inheritance (because class A has many methods > defined that class B doesn't need). You can move createVars() into a mixin or common base class: class M(object): def createVars(self): ... class A(M): ... class B(M) ... > When I try the folloowing: > > > B.createVars = C.createVars > B().createVars() > > > then the following error occurs: > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got nothing instead) > > When I try to add the createVars method to instance of B: > >>>> b=B() >>>> b.createVars = new.instancemethod(A.createVars, b, B) >>>> b.createVars > > >>>> b.createVars() > > > > Then the following error raises: > > > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got B instance instead) > > > > How can I solve this problem? >>> class A(object): ... def create_vars(self): ... self.x = 42 ... >>> class B(object): pass ... >>> B.create_vars = A.create_vars.im_func >>> b = B() >>> b.create_vars() >>> b.x 42 An alternative I find a bit cleaner: >>> def create_vars(self): self.x = 42 ... >>> class A(object): ... create_vars = create_vars ... >>> class B(object): ... create_vars = create_vars ... >>> b = B() >>> b.create_vars() >>> b.x 42 Peter From gagsl-py2 at yahoo.com.ar Fri Jul 24 05:47:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 24 Jul 2009 06:47:37 -0300 Subject: available formats and params for Image.save() References: <4a6833dd$0$22599$426a34cc@news.free.fr> Message-ID: En Thu, 23 Jul 2009 06:56:45 -0300, News123 escribi?: > Somehow I have difficulties reading the documentation for PIL (Image) > > Is there an easy way to know which formats are supported and what their > names are? py> import PIL py> from PIL import Image py> Image.ID [] py> Image.init() py> Image.ID ['PNG', 'ARG', 'BMP', 'BUFR', 'CUR', 'PCX', 'DCX', 'EPS', 'FITS', 'FLI', 'FPX', 'GBR', 'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM', 'IMT', 'IPTC', 'JPEG', 'MCIDA S', 'TIFF', 'MIC', 'MPEG', 'MSP', 'PCD', 'PIXAR', 'PPM', 'PSD', 'SGI', 'SPIDER', 'SUN', 'TGA', 'WBMP', 'WMF', 'XBM', 'XPM', 'XVTHUMB'] py> Image.OPEN.keys() ['PCX', 'ICNS', 'HDF5', 'SUN', 'MIC', 'EPS', 'MSP', 'FLI', 'FITS', 'GBR', 'WBMP' , 'PCD', 'PIXAR', 'BUFR', 'PPM', 'WMF', 'SGI', 'BMP', 'TGA', 'DCX', 'ICO', 'CUR' , 'XPM', 'TIFF', 'JPEG', 'SPIDER', 'GIF', 'GRIB', 'IM', 'IMT', 'IPTC', 'FPX', 'X BM', 'MPEG', 'PSD', 'ARG', 'XVTHUMB', 'PNG', 'MCIDAS'] py> Image.SAVE.keys() ['XBM', 'PCX', 'SPIDER', 'HDF5', 'TIFF', 'BUFR', 'EPS', 'JPEG', 'MSP', 'GRIB', ' GIF', 'BMP', 'IM', 'PPM', 'PDF', 'FITS', 'PALM', 'WBMP', 'WMF', 'PNG'] > Is there an easy way to know which parameters are supported by > Image.save(). How can I list them where are they documented? That depends on the format being used. The PIL handbook lists the standard formats used and their parameters: http://www.pythonware.com/library/pil/handbook/index.htm > I'm at a complete loss at finding out what parameters the save function > accepts for saving a JPG file or a PNG file http://www.pythonware.com/library/pil/handbook/format-jpeg.htm -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 24 06:06:29 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 24 Jul 2009 07:06:29 -0300 Subject: invoke method on many instances References: Message-ID: En Thu, 23 Jul 2009 21:27:35 -0300, Aahz escribi?: > In article , > Gabriel Genellina wrote: >> >> NLMPI > > What? IHNFI -- Gabriel Genellina From amrita at iisermohali.ac.in Fri Jul 24 06:20:11 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Fri, 24 Jul 2009 15:50:11 +0530 (IST) Subject: how to get no value Message-ID: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> Hi, I have a file having lines:- 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = 85 ALA H = 8.60 N = CA = HA = 4.65 C = Now i want to make two another file in which i want to put those lines for which C is missing and another one for which N,CA and C all are missing, I tried in this way: import re f = open('chem.txt') for line in f: if re.search('C = ',''): print line but i am not getting the desired output. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From lpmelo at ualg.pt Fri Jul 24 06:21:30 2009 From: lpmelo at ualg.pt (Luis Pedro Almeida) Date: Fri, 24 Jul 2009 11:21:30 +0100 Subject: Convert points to polygon shapefile Message-ID: <20090724101638.E93B414B2A@smtp3.ualg.pt> Dear all, I would like to know how to convert a list of points into a polygon shapefile (esri). Thanks! Best regards, Luis Pedro Almeida From gagsl-py2 at yahoo.com.ar Fri Jul 24 06:24:52 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 24 Jul 2009 07:24:52 -0300 Subject: OverflowError in RLock.acquire() References: Message-ID: En Fri, 24 Jul 2009 01:27:10 -0300, David Roberts escribi?: > I'm trying to port a Python application to Windows, and I'm getting > the following error (which did not occur when running on Linux): > > Exception in thread Thread-4: > File "C:\Python26\lib\threading.py", line 803, in currentThread > return _active[_get_ident()] > OverflowError: can't convert negative value to unsigned long Looks like a bug in the thread module - you should report it at http://bugs.python.org -- Gabriel Genellina From piet at cs.uu.nl Fri Jul 24 06:46:14 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 12:46:14 +0200 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 References: Message-ID: >>>>> Slaunger (S) wrote: >S> OS: Win XP SP3, 32 bit >S> Python 2.5.4 >S> Hi I have run into some problems with allocating numpy.memmaps >S> exceeding and accumulated size of about 2 GB. I have found out that >S> the real problem relates to numpy.memmap using mmap.mmap On Windows XP the virtual address space of a process is limited to 2 GB unless the /3GB switch is used in the Boot.ini file. http://www.microsoft.com/whdc/system/platform/server/PAE/PAEmem.mspx -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From darcy at druid.net Fri Jul 24 06:56:03 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 24 Jul 2009 06:56:03 -0400 Subject: how to get no value In-Reply-To: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> References: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> Message-ID: <20090724065603.cd758a39.darcy@druid.net> On Fri, 24 Jul 2009 15:50:11 +0530 (IST) amrita at iisermohali.ac.in wrote: > but i am not getting the desired output. Show us what output you got and what you desired. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From deets at nospam.web.de Fri Jul 24 06:56:16 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 12:56:16 +0200 Subject: how to get no value In-Reply-To: References: Message-ID: <7ctiaiF1vrcvpU1@mid.uni-berlin.de> amrita at iisermohali.ac.in schrieb: > Hi, > > I have a file having lines:- > > 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 > 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = > 85 ALA H = 8.60 N = CA = HA = 4.65 C = > > Now i want to make two another file in which i want to put those lines for > which C is missing and another one for which N,CA and C all are missing, > > I tried in this way: > import re > f = open('chem.txt') > for line in f: > if re.search('C = ',''): > print line > > but i am not getting the desired output. Gosh. Must be groundhog-day. Again. And there is me thinking that my job could be endangered by cheap & qualified indian soft-workers - can't be to many of them around if the OP doesn't get a hold of one for the better part of a month now. Must be one of those management myths they tell you to scare you into a less well paid contract... Diez From beni.cherniavsky at gmail.com Fri Jul 24 07:13:09 2009 From: beni.cherniavsky at gmail.com (Beni Cherniavsky) Date: Fri, 24 Jul 2009 04:13:09 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> On Jul 22, 9:36?am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. ? > On Jul 22, 9:36 am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. > The *technical* reason is immutability for dict keys. Dict could allow mutable objects as keys by comparing *by value*, making a copy on insertion and hashing the current value on lookup. Prior art: the 2.3 sets module allows mutable Sets as elements in Sets, by making ImmutableSet copies on insertion, and hashing Sets as if they are temporarily immutable on lookup. This inspired PEP 351 and ambitious proposals to expand the approach to all Python with a copy-on-write scheme. But these ideas were rejected, and the 2.4 builtin sets only allow frozenset elements. Half the reason is technical: copy-on-write and harder than it sounds, and without it you pay a performance price. But the deeper reason is style: immutable types are convenient! The allow a pure-functional style of code, which can be simpler. Of course, sometimes an imperative style is simpler. Depends on the problem. My rule of thumb: - Use mutable lists when you are likely to modify individual elements. - Use immutable tuples when you are likely to replace the whole thing. (In practice, this boils down to similar usage to the "official" rule of thumb that lists are for homogenous data, and tuples for heterogenous records.) From d at vidr.cc Fri Jul 24 07:29:44 2009 From: d at vidr.cc (David Roberts) Date: Fri, 24 Jul 2009 21:29:44 +1000 Subject: OverflowError in RLock.acquire() In-Reply-To: References: Message-ID: Done: http://bugs.python.org/issue6562 -- David Roberts http://da.vidr.cc/ On Fri, Jul 24, 2009 at 20:24, Gabriel Genellina wrote: > En Fri, 24 Jul 2009 01:27:10 -0300, David Roberts escribi?: > >> I'm trying to port a Python application to Windows, and I'm getting >> the following error (which did not occur when running on Linux): >> >> Exception in thread Thread-4: >> ?File "C:\Python26\lib\threading.py", line 803, in currentThread >> ? ?return _active[_get_ident()] >> OverflowError: can't convert negative value to unsigned long > > Looks like a bug in the thread module - you should report it at > http://bugs.python.org > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > From piet at cs.uu.nl Fri Jul 24 07:48:09 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 13:48:09 +0200 Subject: how to get no value References: Message-ID: >>>>> amrita at iisermohali.ac.in (a) a ?crit: >a> Hi, >a> I have a file having lines:- >a> 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 >a> 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = >a> 85 ALA H = 8.60 N = CA = HA = 4.65 C = >a> Now i want to make two another file in which i want to put those lines for >a> which C is missing and another one for which N,CA and C all are missing, >a> I tried in this way: >a> import re >a> f = open('chem.txt') >a> for line in f: >a> if re.search('C = ',''): >a> print line >a> but i am not getting the desired output. You never look in the lines you read. if re.search('C = ',''): should be if re.search('C = ', line): Do you really think before you write your code? Or look at it after you have written it? Sorry if I offend you but you give the impression of just trying some more or less random stuff and then asking here if it doesn't give the required result. That's not the attitude of a researcher, unless your definition of research is 'asking on Usenet'. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pdpinheiro at gmail.com Fri Jul 24 08:08:21 2009 From: pdpinheiro at gmail.com (pdpi) Date: Fri, 24 Jul 2009 05:08:21 -0700 (PDT) Subject: Convert points to polygon shapefile References: Message-ID: On Jul 24, 11:21?am, Luis Pedro Almeida wrote: > Dear all, > > I would like to know how to convert a list of points into a polygon > shapefile (esri). > > Thanks! > > Best regards, > > Luis Pedro Almeida I think you'd be better served by asking this question in a newsgroup dedicated to GIS software (I'm guessing that's the ESRI you meant). Still, what you want is to find the file format specification, and implement it. From piet at cs.uu.nl Fri Jul 24 08:14:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 14:14:27 +0200 Subject: Convert points to polygon shapefile References: Message-ID: >>>>> Luis Pedro Almeida (LPA) wrote: >LPA> Dear all, >LPA> I would like to know how to convert a list of points into a >LPA> polygon shapefile (esri). http://lmgtfy.com/?q=esri+shapefile+Python -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Fri Jul 24 08:25:17 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 24 Jul 2009 08:25:17 -0400 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 In-Reply-To: References: Message-ID: <4A69A82D.8090301@ieee.org> Slaunger wrote: > OS: Win XP SP3, 32 bit > Python 2.5.4 > > Hi I have run into some problems with allocating numpy.memmaps > exceeding and accumulated size of about 2 GB. I have found out that > the real problem relates to numpy.memmap using mmap.mmap > > I've written a small test program to illustrate it: > > import itertools > import mmap > import os > > files = [] > mmaps = [] > file_names= [] > mmap_cap=0 > bytes_per_mmap = 100 * 1024 ** 2 > try: > for i in itertools.count(1): > file_name = "d:/%d.tst" % i > file_names.append(file_name) > f = open(file_name, "w+b") > files.append(f) > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > mmaps.append(mm) > mmap_cap += bytes_per_mmap > print "Created %d writeable mmaps containing %d MB" % (i, > mmap_cap/(1024**2)) > > #Clean up > finally: > print "Removing mmaps..." > for mm, f, file_name in zip(mmaps, files, file_names): > mm.close() > f.close() > os.remove(file_name) > print "Done..." > > > which creates this output > > Created 1 writeable mmaps containing 100 MB > Created 2 writeable mmaps containing 200 MB > .... > Created 17 writeable mmaps containing 1700 MB > Created 18 writeable mmaps containing 1800 MB > Removing mmaps... > Done... > Traceback (most recent call last): > File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha > \mmaptest.py", line 16, in > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > WindowsError: [Error 8] Not enough storage is available to process > this command > > There is more than 25 GB of free space on drive d: at this stage. > > Is it a bug or a "feature" of the 32 bit OS? > > I am surprised about it as I have not found any notes about these > kinds of limitations in the documentation. > > I am in dire need of these large memmaps for my task, and it is not an > option to change OS due to other constraints in the system. > > Is there anything I can do about it? > > Best wishes, > Kim > > It's not a question of how much disk space there is, but how much virtual space 32 bits can address. 2**32 is about 4 gig, and Windows XP reserves about half of that for system use. Presumably a 64 bit OS would have a much larger limit. Years ago I worked on Sun Sparc system which had much more limited shared memory access, due to hardware limitations. So 2gig seems pretty good to me. There is supposed to be a way to tell the Windows OS to only use 1 gb of virtual space, leaving 3gb for application use. But there are some limitations, and I don't recall what they are. I believe it has to be done globally (probably in Boot.ini), rather than per process. And some things didn't work in that configuration. DaveA From jeanmichel at sequans.com Fri Jul 24 08:34:23 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 24 Jul 2009 14:34:23 +0200 Subject: doted filenames in import statements In-Reply-To: References: <4A670313.8040204@sequans.com> Message-ID: <4A69AA4F.9060707@sequans.com> Terry Reedy wrote: > Jean-Michel Pichavant wrote: >> Piet van Oostrum wrote: >>> >>> [snip] >>> >>>> JP> file = "/home/dsp/4.6.0.0/test.py" >>>> JP> test = __import__(file) >>>> JP> => no module name blalalal found. >>> >>> >>>> JP> Any suggestion ? I tried multiple escape technics without any >>>> success. >>> >>> Rightly so. >>> >>> I think the best would be to add the directory to sys.path >>> sys.path.add('/home/dsp/4.6.0.0') >>> and then >>> __import__('test', ... ) >> >> I see. My problem is that a have to import 2 different files having >> de same name. In the same name space I have 2 objects from 2 >> different software branches, let's say 4.6.0 and 4.6.1. >> The first object shall import 4.6.0/orb.py and the second one >> 4.6.1/orb.py. >> >> If I add 4.6.1 to sys.path, the import statement will look like: >> self._orb = __import__('orb') >> The problem is, python wil assume orb is already imported and will >> assign the module from the 4.6.0 branch to my 4.6.1 object. >> >> Do I have to mess up with sys.modules keys to make python import the >> correct file ? Is there a standard/proper way to do that ? > > If you make the directory names into proper identifiers like v460 and > v461 and add __init__.py to each to make them packages and have both > on search path, then > > import v460.orb #or import v460.orb as orb460 > import v461.orb #or import v460.orb as orb461 > > will get you both. One way or another, they have to get different > names within Python. > > Terry Jan Reedy > I finally had to write my own import statement as I prefered to manipulate the python objects instead of manipulating third party files. Basically when importing 'file.py' it records it in sys.modules as sys.modules['__magic_word_file''] and then I remove the standard reference. This allows me to import file.py again, but with a totally different path. (path is temporarily added to sys.path) JM From sanne at kortec.nl Fri Jul 24 08:34:53 2009 From: sanne at kortec.nl (Sanne Korzec) Date: Fri, 24 Jul 2009 14:34:53 +0200 Subject: trouble with wrapping a c program Message-ID: <20090724123458.TOWM14607.viefep17-int.chello.at@edge02.upc.biz> Hi Mailing, I am using a c program, which first initializes for some seconds and then waits for user input (keyboard) to type something. When enter is pressed the c program continues. I have wrapped this program in a python script, which starts the c program. To start the c program, there are many options in python e.g. Os.system os.popen or subprocess.popen To me there does not seem to be a difference for starting the program. They all work well. However the problem occurs when I want to input data during this c program. Using the keyboard and then enter in the c program prompt works, but I wish to do this from the python script by sending the string from the python script. I am able to print the string from python with a print command or with a stdout.write command. But this simply prints it to the prompt, the c program does nothing with this printed string. Is there a way to pipe, stream, or send this string to the running c program? All tutorials about piping I have read, seem to have to wait for the process to finish. Or need the stdin string before starting the program. Note that the c program is not finished when the input is needed, so I cannot use subprocess.call or wait. It is possible to give the user input in advance, but I do not want this, because this makes everything much slower! I really would like to do this during/while the c program is running. I have created a thread and a small xmlrpc server for this and this works fine. However I cannot seem to pass the string. Here is a small fragment of my code: #initialization cmd = [a list of my program and arguments] subprocess.Popen(cmd) #starts the c program #somewhere along the way Send_string(str): #Sys.stdin = str #subprocess.stdin = str I have spent the entire night trying to get this to work, but I can't seem to get it right. Any help is much appreciated. Also, if anybody could explain me where the fuck-up in my brain is, I would be very happy.. Sanne -------------- next part -------------- An HTML attachment was scrubbed... URL: From doetoe at gmail.com Fri Jul 24 08:38:00 2009 From: doetoe at gmail.com (Utpal Sarkar) Date: Fri, 24 Jul 2009 05:38:00 -0700 (PDT) Subject: non-owning references? Message-ID: Hi, I'm not sure the subject describes what I'm looking for, but the question is the following: Is there a way I can tell a variable that the object it is pointing too is not owned by it, in the sense that if it is the only reference to the object it can be garbage collected? I want this for what is essentially a singleton class, so that on first instantiation the object is created and a reference is kept in the class, that is used to return the same object in subsequent instantiations. When all instances go out of scope, the reference in the class is still there, preventing it from being garbage collected, but since the instance can be huge, I would like it to be. Thanks, Utpal From __peter__ at web.de Fri Jul 24 08:52:07 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 14:52:07 +0200 Subject: non-owning references? References: Message-ID: Utpal Sarkar wrote: > Is there a way I can tell a variable that the object it is pointing > too is not owned by it, in the sense that if it is the only reference > to the object it can be garbage collected? http://docs.python.org/library/weakref.html From gh at ghaering.de Fri Jul 24 09:00:43 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Fri, 24 Jul 2009 15:00:43 +0200 Subject: non-owning references? In-Reply-To: References: Message-ID: Utpal Sarkar wrote: > Hi, > [...] You're looking for the weakref module. What you're describing there sounds like a nice exercise, but I cannot imagine why you'd really need to clean it up, if it really is a singleton. -- Gerhard From ben+python at benfinney.id.au Fri Jul 24 09:06:34 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 24 Jul 2009 23:06:34 +1000 Subject: non-owning references? References: Message-ID: <87d47q5ixx.fsf@benfinney.id.au> Utpal Sarkar writes: > Is there a way I can tell a variable that the object it is pointing > too is not owned by it, in the sense that if it is the only reference > to the object it can be garbage collected? Python doesn't have ?pointers?, and doesn't really have ?variables? either, at least not how many other languages use that term. What it does have is references to objects . > on first instantiation the object is created and a reference is kept > in the class, that is used to return the same object in subsequent > instantiations. When all instances go out of scope, the reference in > the class is still there, preventing it from being garbage collected, > but since the instance can be huge, I would like it to be. What you are asking for is called a ?weak reference? and is provided by the ?weakref? module . -- \ ?Patience, n. A minor form of despair, disguised as a virtue.? | `\ ?Ambrose Bierce, _The Devil's Dictionary_, 1906 | _o__) | Ben Finney From doetoex at gmail.com Fri Jul 24 09:18:48 2009 From: doetoex at gmail.com (Doetoe) Date: Fri, 24 Jul 2009 06:18:48 -0700 (PDT) Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> Message-ID: <14f1729b-9035-417f-a725-3deaa9408a69@c2g2000yqi.googlegroups.com> On Jul 24, 3:06?pm, Ben Finney wrote: > Utpal Sarkar writes: > > Is there a way I can tell a variable that the object it is pointing > > too is not owned by it, in the sense that if it is the only reference > > to the object it can be garbage collected? > > Python doesn't have ?pointers?, and doesn't really have ?variables? > either, at least not how many other languages use that term. > > What it does have is references to objects > . > > > on first instantiation the object is created and a reference is kept > > in the class, that is used to return the same object in subsequent > > instantiations. When all instances go out of scope, the reference in > > the class is still there, preventing it from being garbage collected, > > but since the instance can be huge, I would like it to be. > > What you are asking for is called a ?weak reference? and is provided > by the ?weakref? module . > > -- > ?\ ? ? ??Patience, n. A minor form of despair, disguised as a virtue.? | > ? `\ ? ? ? ? ? ? ? ? ? ?Ambrose Bierce, _The Devil's Dictionary_, 1906 | > _o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| > Ben Finney Thanks to the three of you. This is precisely what I needed! Gerhard, the reason I need to clean it up is that it is a lazy data structure that can grow to arbitrary size. When it is not needed anymore it would still remain in memory. Utpal From breamoreboy at yahoo.co.uk Fri Jul 24 09:50:33 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 24 Jul 2009 14:50:33 +0100 Subject: how to get no value In-Reply-To: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> References: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> Message-ID: amrita at iisermohali.ac.in wrote: > Hi, > > I have a file having lines:- > > 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 > 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = > 85 ALA H = 8.60 N = CA = HA = 4.65 C = > > Now i want to make two another file in which i want to put those lines for > which C is missing and another one for which N,CA and C all are missing, > > I tried in this way: > import re > f = open('chem.txt') > for line in f: > if re.search('C = ',''): > print line > > but i am not getting the desired output. > > > > > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > Try writing correct rather than incorrect code. Or as has been repeatedly stated get somone from your CS department to help. -- Kindest regards. Mark Lawrence. From hniksic at xemacs.org Fri Jul 24 09:55:45 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 24 Jul 2009 15:55:45 +0200 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> Message-ID: <87fxcm5gny.fsf@busola.homelinux.net> Ben Finney writes: > Utpal Sarkar writes: > >> Is there a way I can tell a variable that the object it is pointing >> too is not owned by it, in the sense that if it is the only reference >> to the object it can be garbage collected? > > Python doesn't have ?pointers?, and doesn't really have ?variables? > either, at least not how many other languages use that term. The OP didn't use the term "pointer", but the word "pointing", which makes sense in the context. The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it. Please stop complaining about valid terminology; it is not helpful. From invalid at invalid Fri Jul 24 09:57:02 2009 From: invalid at invalid (Grant Edwards) Date: Fri, 24 Jul 2009 08:57:02 -0500 Subject: len() should always return something References: Message-ID: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> On 2009-07-24, Dr. Phillip M. Feldman wrote: > > Some aspects of the Python design are remarkably clever, while > others leave me perplexed. Here's an example of the latter: > Why does len() give an error when applied to an int or float? > len() should always return something; in particular, when > applied to a scalar, it should return a value of 1. If len(7) returned a value of 1, then wouldn't one expect 7[0] to be valid? It isn't, so you'd then have to redefine all types so that they are sequences that can be indexed. Sounds like a big mess to me... [Are there types for which len() returns a value that can't be indexed?] -- Grant Edwards grante Yow! It's the RINSE CYCLE!! at They've ALL IGNORED the visi.com RINSE CYCLE!! From timlee126 at yahoo.com Fri Jul 24 10:03:22 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 24 Jul 2009 07:03:22 -0700 (PDT) Subject: Popen Message-ID: <896055.35192.qm@web63101.mail.re1.yahoo.com> Hi, I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? Thanks and regards! From invalid at invalid Fri Jul 24 10:10:29 2009 From: invalid at invalid (Grant Edwards) Date: Fri, 24 Jul 2009 09:10:29 -0500 Subject: how to get no value References: Message-ID: On 2009-07-24, amrita at iisermohali.ac.in wrote: > > Hi, > > I have a file having lines:- > > 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 > 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = > 85 ALA H = 8.60 N = CA = HA = 4.65 C = > > Now i want to make two another file in which i want to put > those lines for which C is missing and another one for which > N,CA and C all are missing, > > I tried in this way: > import re > f = open('chem.txt') > for line in f: > if re.search('C = ',''): > print line > > but i am not getting the desired output. I've told you before: don't use regular expressions (e.g. the "re" module). Stop using regular expressions now. Regular expressions are way beyond your capabilities. Use simple operations like split() and "in": f = open('chem.txt') for line in f: if "C = " in line: print line You really need to work through a Python tutorial or two: http://docs.python.org/tutorial/ http://www.greenteapress.com/thinkpython/thinkpython.html Better yet, take an couple introductory programming courses. I'm a bit surprised that one could become a "Research Fellow" in a scientific field without taking any programming courses. -- Grant Edwards grante Yow! I'm also against at BODY-SURFING!! visi.com From rhodri at wildebst.demon.co.uk Fri Jul 24 10:11:40 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 15:11:40 +0100 Subject: len() should always return something In-Reply-To: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards wrote: > On 2009-07-24, Dr. Phillip M. Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while >> others leave me perplexed. Here's an example of the latter: >> Why does len() give an error when applied to an int or float? >> len() should always return something; in particular, when >> applied to a scalar, it should return a value of 1. > > If len(7) returned a value of 1, then wouldn't one expect 7[0] > to be valid? It isn't, so you'd then have to redefine all > types so that they are sequences that can be indexed. Sounds > like a big mess to me... > > [Are there types for which len() returns a value that can't be > indexed?] > Dictionaries. Which doesn't make your point less valid. In fact I'd go so far as to argue that what len() gives you is the number of items in a container, so len(7) should return 0. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Fri Jul 24 10:21:36 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 15:21:36 +0100 Subject: non-owning references? In-Reply-To: <87fxcm5gny.fsf@busola.homelinux.net> References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic wrote: > Ben Finney writes: > >> Utpal Sarkar writes: >> >>> Is there a way I can tell a variable that the object it is pointing >>> too is not owned by it, in the sense that if it is the only reference >>> to the object it can be garbage collected? >> >> Python doesn't have ?pointers?, and doesn't really have ?variables? >> either, at least not how many other languages use that term. > > The OP didn't use the term "pointer", but the word "pointing", which > makes sense in the context. The term "variable" is used in the Python > language reference and elsewhere, and is quite compatible with how other > popular languages (Java, PHP, Lisp, ...) use it. Only superficially. Treating Python variables the same as C variables (say) is one of the classic ways that newbies come unstuck when mutable objects appear on the scene. While the OP appears to have the right idea, your "correction" here could be quite misleading. -- Rhodri James *-* Wildebeest Herder to the Masses From pruebauno at latinmail.com Fri Jul 24 10:45:21 2009 From: pruebauno at latinmail.com (nn) Date: Fri, 24 Jul 2009 07:45:21 -0700 (PDT) Subject: strange python scripting error References: Message-ID: On Jul 23, 7:03?pm, Dave Angel wrote: > Mark Tarver wrote: > > I have a very strange error. ?I have two test python files test.py and > > python.py which contain the following code > > > #!/usr/bin/python > > print "Content-type: text/html" > > print > > print "" > > print "
Hello, Linux.com!
" > > print "" > > > One file (test.py) works; you call it up and it shows a web page with > > > Hello, Linux.com > > > The other fails with a server configuration error. ?Both are running > > under Linux, same server, same permissions. ?Running a character scan > > shows that both files contain the same printable characters and are > > therefore typographically identical. ? They are absolutely the same. > > > The only hint at a difference I can see is that my ftp program says > > the files are of unequal lengths. ?test.py is 129 bytes long. > > python.py 134 bytes long. > > > A zipped folder containing both files is at > > >www.lambdassociates.org/weird.zip > > > Any ideas welcome. > > > Mark > > Easiest explanation is that python.py has Windows-style newlines. ?In > other words, each line ends with 0d0a, rather than the Unix convention > of 0a. > > If your server is Unix-based, it can't handle that first line, since it > has an illegal character (0d) following the > > #!/usr/bin/python > > line. ?Convert it to Unix line-endings. > > DaveA Use dos2unix for conversion of the longer file and try again: http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm From dickinsm at gmail.com Fri Jul 24 10:45:40 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Fri, 24 Jul 2009 07:45:40 -0700 (PDT) Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Jul 24, 3:11?pm, "Rhodri James" wrote: > Which doesn't make your point less valid. ?In fact I'd go so > far as to argue that what len() gives you is the number of > items in a container, so len(7) should return 0. Nah. 7 contains three bits, so len(7) should *clearly* return 3. Mark From user at example.net Fri Jul 24 10:50:03 2009 From: user at example.net (superpollo) Date: Fri, 24 Jul 2009 16:50:03 +0200 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> Mark Dickinson wrote: > On Jul 24, 3:11 pm, "Rhodri James" > wrote: > >>Which doesn't make your point less valid. In fact I'd go so >>far as to argue that what len() gives you is the number of >>items in a container, so len(7) should return 0. > > > Nah. 7 contains three bits, so len(7) should *clearly* return 3. and len("7") must return 8, by the same token... but wait! >>> len("7") 1 >>> my python installation must me outdated ;-) bye From deets at nospam.web.de Fri Jul 24 10:51:12 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 16:51:12 +0200 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: <7cu030F295totU1@mid.uni-berlin.de> Mark Dickinson schrieb: > On Jul 24, 3:11 pm, "Rhodri James" > wrote: >> Which doesn't make your point less valid. In fact I'd go so >> far as to argue that what len() gives you is the number of >> items in a container, so len(7) should return 0. > > Nah. 7 contains three bits, so len(7) should *clearly* return 3. But it contains a minimum of 32 bits! And why are you treating ones as special over zeros? I thought the times of BitRacism were finally over... Diez From kushal.kumaran+python at gmail.com Fri Jul 24 10:58:49 2009 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Fri, 24 Jul 2009 20:28:49 +0530 Subject: Popen In-Reply-To: <896055.35192.qm@web63101.mail.re1.yahoo.com> References: <896055.35192.qm@web63101.mail.re1.yahoo.com> Message-ID: <1e364c4e0907240758k753f8653o225ac97e92fd2eea@mail.gmail.com> On Fri, Jul 24, 2009 at 7:33 PM, Tim wrote: > > Hi, > I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? > Thanks and regards! > Assuming you mean subprocess.Popen, the child is executed asynchronously. You can use the wait() method on the Popen object if you want the parent to wait for the child to finish. -- kushal From piet at cs.uu.nl Fri Jul 24 11:03:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:03:58 +0200 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: >>>>> "Rhodri James" (RJ) wrote: >RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic wrote: >>> Ben Finney writes: >>> >>>> Utpal Sarkar writes: >>>> >>>>> Is there a way I can tell a variable that the object it is pointing >>>>> too is not owned by it, in the sense that if it is the only reference >>>>> to the object it can be garbage collected? >>>> >>>> Python doesn't have ?pointers?, and doesn't really have ?variables? >>>> either, at least not how many other languages use that term. >>> >>> The OP didn't use the term "pointer", but the word "pointing", which >>> makes sense in the context. The term "variable" is used in the Python >>> language reference and elsewhere, and is quite compatible with how other >>> popular languages (Java, PHP, Lisp, ...) use it. >RJ> Only superficially. Treating Python variables the same as C variables >RJ> (say) is one of the classic ways that newbies come unstuck when mutable >RJ> objects appear on the scene. While the OP appears to have the right idea, >RJ> your "correction" here could be quite misleading. If you read the OP, it is clear that he talked about a class variable, which is a perfectly legal notion in Python, and is mentioned as such in the language reference manual: `Variables defined in the class definition are class variables' And who was talking about C variables? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Fri Jul 24 11:06:23 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:06:23 +0200 Subject: how to get no value References: Message-ID: Well actually your subject is `how to get no value'. Your code does that perfectly. :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From inky788 at gmail.com Fri Jul 24 11:07:30 2009 From: inky788 at gmail.com (Inky 788) Date: Fri, 24 Jul 2009 08:07:30 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: On Jul 23, 3:42?am, Hendrik van Rooyen wrote: > On Wednesday 22 July 2009 16:36:51 Inky 788 wrote: > > > On Jul 22, 2:36?am, Hendrik van Rooyen > > > wrote: > > > The good reason is the immutability, which lets you use > > > a tuple as a dict key. ? > > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > > just not sophisticated enough, but I've never wanted to use a list/ > > tuple as a dict key. This sounds like obscure usage, and a bit > > contrived as a reason for having *both* lists and tuples. > > Steven showed why you cannot have a mutable thing > as a key in a dict. > > if you think it is contrived, then please consider how you would > keep track of say the colour of a pixel on a screen at position > (x,y) - this is about the simplest "natural" tuple format and > example. My guess is that this is probably the way most people do it: ~~~~ #!/usr/bin/env python import sys import random if len( sys.argv ) != 3: print "Please pass exactly 2 ints. Exiting." sys.exit(1) NUM_COLUMNS = int( sys.argv[1] ) NUM_ROWS = int( sys.argv[2] ) print "Making array of %s columns by %s rows." % (NUM_COLUMNS, NUM_ROWS) def rand(): return int( 255 * random.random()) def make_a_pixel(): # red green blue return [rand(), rand(), rand()] def make_a_row(num_columns): temp_row = [] for i in range(num_columns): temp_row.append( make_a_pixel() ) return temp_row def make_array_of_pixels(num_columns, num_rows): rows = [] for i in range(num_rows): rows.append( make_a_row(num_columns) ) return rows def show_pixels(pixel_array): for row in pixel_array: for pixel in row: print pixel, ' ', print rows_of_pixels = make_array_of_pixels(NUM_COLUMNS, NUM_ROWS) show_pixels(rows_of_pixels) ~~~~ From piet at cs.uu.nl Fri Jul 24 11:10:07 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:10:07 +0200 Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: >>>>> "Rhodri James" (RJ) wrote: >RJ> On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards wrote: >>> On 2009-07-24, Dr. Phillip M. Feldman wrote: >>>> >>>> Some aspects of the Python design are remarkably clever, while >>>> others leave me perplexed. Here's an example of the latter: >>>> Why does len() give an error when applied to an int or float? >>>> len() should always return something; in particular, when >>>> applied to a scalar, it should return a value of 1. >>> >>> If len(7) returned a value of 1, then wouldn't one expect 7[0] >>> to be valid? It isn't, so you'd then have to redefine all >>> types so that they are sequences that can be indexed. Sounds >>> like a big mess to me... >>> >>> [Are there types for which len() returns a value that can't be >>> indexed?] >>> >RJ> Dictionaries. >RJ> Which doesn't make your point less valid. In fact I'd go so >RJ> far as to argue that what len() gives you is the number of >RJ> items in a container, so len(7) should return 0. But len(7) could as well be defined as 3, 1, 32, or 64 (depending on the implementation). Therefore it doesn't make much sense. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From ben+python at benfinney.id.au Fri Jul 24 11:14:06 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 25 Jul 2009 01:14:06 +1000 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: <8763di5d1d.fsf@benfinney.id.au> Hrvoje Niksic writes: > The term "variable" is used in the Python language reference and > elsewhere Yes. It should also be abundantly clear from the constant stream of confused newbies on this point that its usage of that term is different to what many expect from usage elsewhere. > and is quite compatible with how other popular languages (Java, PHP, > Lisp, ...) use it. Please stop complaining about valid terminology; it > is not helpful. I disagree with your assertions. Rather than yet another round of this tedious debate, I merely point interested readers to and ask them to draw their own conclusion on how compatible their pre-existing ?variable? concept is with Python's object reference model. -- \ ?Too many pieces of music finish too long after the end.? ?Igor | `\ Stravinskey | _o__) | Ben Finney From piet at cs.uu.nl Fri Jul 24 11:17:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:17:56 +0200 Subject: Popen References: Message-ID: >>>>> Tim (T) wrote: >T> Hi, >T> I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? >T> Thanks and regards! Only if you use Popen.wait(), Popen.communicate() or something similar like os.waitpid(), subprocess.call() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rylesny at gmail.com Fri Jul 24 11:22:55 2009 From: rylesny at gmail.com (ryles) Date: Fri, 24 Jul 2009 08:22:55 -0700 (PDT) Subject: difference in printing to screen Mac / Windows References: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> Message-ID: On Jul 18, 7:03?am, Tim Chase wrote: > Lastly, you can force all standard-output in your program to be > unbuffered without the "-u" parameter: And if you're using -u a lot, the PYTHONUNBUFFERED environment variable can also be set (but not empty), so that python adds the option automatically. From Scott.Daniels at Acm.Org Fri Jul 24 11:54:32 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 24 Jul 2009 08:54:32 -0700 Subject: regex: multiple matching for one string In-Reply-To: References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: rurpy at yahoo.com wrote: > Nick Dumas wrote: >> On 7/23/2009 9:23 AM, Mark Lawrence wrote: >>> scriptlearner at gmail.com wrote: >>>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >>>> will like to take out the values (valuea, valueb, and valuec). How do >>>> I do that in Python? The group method will only return the matched >>>> part. Thanks. >>>> >>>> p = re.compile('#a=*;b=*;c=*;') >>>> m = p.match(line) >>>> if m: >>>> print m.group(), >>> IMHO a regex for this is overkill, a combination of string methods such >>> as split and find should suffice. > > You're saying that something like the following > is better than the simple regex used by the OP? > [untested] > values = [] > parts = line.split(';') > if len(parts) != 4: raise SomeError() > for p, expected in zip (parts[-1], ('#a','b','c')): > name, x, value = p.partition ('=') > if name != expected or x != '=': > raise SomeError() > values.append (value) > print values[0], values[1], values[2] I call straw man: [tested] line = "#a=valuea;b=valueb;c=valuec;" d = dict(single.split('=', 1) for single in line.split(';') if single) d['#a'], d['b'], d['c'] If you want checking code, add: if len(d) != 3: raise ValueError('Too many keys: %s in %r)' % ( sorted(d), line)) > Blech, not in my book. The regex checks the > format of the string, extracts the values, and > does so very clearly. Further, it is easily > adapted to other similar formats, or evolutionary > changes in format. It is also (once one is > familiar with regexes -- a useful skill outside > of Python too) easier to get right (at least in > a simple case like this.) The posted regex doesn't work; this might be homework, so I'll not fix the two problems. The fact that you did not see the failure weakens your claim of "does so very clearly." --Scott David Daniels Scott.Daniels at Acm.Org From pfeldman at verizon.net Fri Jul 24 11:58:36 2009 From: pfeldman at verizon.net (Phillip M. Feldman) Date: Fri, 24 Jul 2009 10:58:36 -0500 (CDT) Subject: len() should always return something Message-ID: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> An HTML attachment was scrubbed... URL: From Scott.Daniels at Acm.Org Fri Jul 24 12:06:54 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 24 Jul 2009 09:06:54 -0700 Subject: Predefined Variables In-Reply-To: References: Message-ID: Stephen Cuppett (should have written in this order): > "Fred Atkinson" wrote ... >> Is there a pre-defined variable that returns the GET line >> >> (http://www.php.net/index.php?everythingafterthequestionmark) as a >> single variable (rather than individual variables)? > os.environment('QUERY_STRING') Maybe you mean: os.environ['USER'] --Scott David Daniels Scott.Daniels at Acm.Org From timlee126 at yahoo.com Fri Jul 24 12:24:44 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 24 Jul 2009 09:24:44 -0700 (PDT) Subject: Popen Message-ID: <987525.41038.qm@web63102.mail.re1.yahoo.com> Thanks! Yes I mean subprocess.Popen. I was wondering the meaning of "asynchronously" Here is some code I am reading recently: " result = Popen(cmdline,shell=True,stdout=PIPE).stdout for line in result.readlines(): if find(line,"Cross") != -1: return float(split(line)[-1][0:-1]) " The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. So how to understand the meaning of "asynchronous"? Thanks and regards! --- On Fri, 7/24/09, Kushal Kumaran wrote: > From: Kushal Kumaran > Subject: Re: Popen > To: "Tim" > Cc: python-list at python.org > Date: Friday, July 24, 2009, 10:58 AM > On Fri, Jul 24, 2009 at 7:33 PM, > Tim > wrote: > > > > Hi, > > I wonder if I use Popen, the parent process will wait > for the child process to finish or continue without > waiting? > > Thanks and regards! > > > > Assuming you mean subprocess.Popen, the child is executed > asynchronously.? You can use the wait() method on the > Popen object if > you want the parent to wait for the child to finish. > > -- > kushal > From deets at nospam.web.de Fri Jul 24 12:35:56 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 18:35:56 +0200 Subject: Popen In-Reply-To: References: Message-ID: <7cu67cF29g680U1@mid.uni-berlin.de> Tim schrieb: > Thanks! > Yes I mean subprocess.Popen. > > I was wondering the meaning of "asynchronously" > Here is some code I am reading recently: > " > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > if find(line,"Cross") != -1: > return float(split(line)[-1][0:-1]) > " > The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. > > "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. > However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. > > So how to understand the meaning of "asynchronous"? "Asynchronous" means asynchronous - the parent is *not* waiting. Which is the reason that there is (amongst other things) the "wait"-method you can call to wait for the child to be terminated. Diez From bieffe62 at gmail.com Fri Jul 24 12:40:22 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 24 Jul 2009 09:40:22 -0700 (PDT) Subject: Popen References: Message-ID: <7df4ca4e-476a-4b3c-99d4-e710c75ab80b@o15g2000yqm.googlegroups.com> On Jul 24, 6:24?pm, Tim wrote: > Thanks! > Yes I mean subprocess.Popen. > > I was wondering the meaning of "asynchronously" > Here is some code I am reading recently: > " > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > ? ? if find(line,"Cross") != -1: > ? ? ? ? return float(split(line)[-1][0:-1]) > " > The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. > > "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. This is correct > However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. > Calling result.readlines() the parent process attempts to read from stdout until end of file. For a pipe, end of file means that the other end is closing its connection, which - unless the child process closes stdout explicitely - means that the child process is terminated. So the end effect is the almost like using 'communicate' on the result of Popen, except that communicates returns both standard output and standard error contents at the same time. > So how to understand the meaning of "asynchronous"? > > Thanks and regards! HTH Ciao ----- FB From mathieu.malaterre at gmail.com Fri Jul 24 12:48:22 2009 From: mathieu.malaterre at gmail.com (mathieu) Date: Fri, 24 Jul 2009 09:48:22 -0700 (PDT) Subject: SONAME for python modules is bad? (aka multiple module version) Message-ID: <2a2a36ca-72a0-470f-907c-2be43104617a@h18g2000yqj.googlegroups.com> As far as I know there has not been any consensus on how to install multiple version of a same module in python ? What are the recommended mechanism ? I could not find any documentation on the subject. Does anyone sees any issue with using standard SONAME mechanism when installing a python module ? Thanks, ref: http://mail.python.org/pipermail/pythonmac-sig/2009-January/020936.html From timlee126 at yahoo.com Fri Jul 24 12:52:01 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 24 Jul 2009 09:52:01 -0700 (PDT) Subject: Popen Message-ID: <414673.50429.qm@web63108.mail.re1.yahoo.com> Thanks! If that is the case, i.e. the parent doesn't wait, is the code in my last post wrong? "result" could be nothing. --- On Fri, 7/24/09, Diez B. Roggisch wrote: > From: Diez B. Roggisch > Subject: Re: Popen > To: python-list at python.org > Date: Friday, July 24, 2009, 12:35 PM > Tim schrieb: > > Thanks! Yes I mean subprocess.Popen. > > > > I was wondering the meaning of "asynchronously" > > Here is some code I am reading recently: > > " > > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > >? ???if find(line,"Cross") != -1: > >? ? ? ???return > float(split(line)[-1][0:-1]) " > > The computation in the program "cmdline" takes a long > time, at the end of which the results will be output to > stdout. > > > > "asynchronous" seems to mean Popen returns to the > parent process immediately and the parent and child > processes continue to be executed. > > However, if Popen returns immediately to the parent > process, then there will be nothing in "result", not to > mention extracting information from the output. Thus it > seems to me the parent process has to wait till the child > process finish. > > > > So how to understand the meaning of "asynchronous"? > > "Asynchronous" means asynchronous - the parent is *not* > waiting. > > Which is the reason that there is (amongst other things) > the "wait"-method you can call to wait for the child to be > terminated. > > Diez > -- http://mail.python.org/mailman/listinfo/python-list > From marcusw at cox.net Fri Jul 24 12:57:57 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 24 Jul 2009 12:57:57 -0400 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: On 7/21/2009 12:13 PM, Stef Mientki wrote: > hi Marcus, >>>> That sounds like a very specialized type of thing, >>> Well from an application point of view, >>> with the current netbooks, >>> this looks like a perfect tool for any conversation or meeting. >>>> which only the few people with experience with wxPython, PyAudio, >>>> and Scintilla could help you with... >>>> >>> I was afraid of that too, so I dropped the question in several places, >>> and the writer of Scintilla himself came with the perfect answer. >>> >>> cheers,Stef >>>> But you might try having a dictionary with notes and associated >>>> times, or just give each note a four-digit ID number at the >>>> beginning of it when it's entered and use that in the dictionary (to >>>> keep keys shorter). Or you could just do a little hack and increase >>>> the number of bookmarks allowed (seeing as source is available) :p >>>> >>>> Marcus >>> >> Glad you got a good answer from somebody. Sounds like an interesting >> project. About when would this be headed for a release? Could you post >> a link to a googlecode or sourceforge project or something so I can >> follow and/or help with development? >> > For the moment it's just an idea, so no line of code yet. > I first like to tackle all the problems, > at least to the level I think I can handle them. > So first solve the next problem, > before I start coding: > automatic synchronization (file uploading and deleting) between EEE-pc > and desktop PC over bluetooth. > And another problem, as my customers are physicians, > both the text and audio need to be stored encrypted. > > cheers, > Stef >> Marcus > I would recommend pybluez and http://www.google.com/search?q=python+aes+encryption Marcus From kyrie at uh.cu Fri Jul 24 13:00:38 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Fri, 24 Jul 2009 13:00:38 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: Message-ID: <200907241300.38738.kyrie@uh.cu> On Friday 24 July 2009 11:07:30 am Inky 788 wrote: > On Jul 23, 3:42?am, Hendrik van Rooyen > > if you think it is contrived, then please consider how you would > > keep track of say the colour of a pixel on a screen at position > > (x,y) - this is about the simplest "natural" tuple format and > > example. > > My guess is that this is probably the way most people do it: [...] > def make_array_of_pixels(num_columns, num_rows): [...] If you need to hold /every/ pixel on the screen, it makes sense to have a bidimentional array of points and colors. But if you need to hold a non-rectangular subset, or even a subset that doesn't need to be rectangular, you can either use dicts, or ... do what you did. Most people will use the less contrived version of: screen = {} screen[1,2] = (rand(), rand(), rand()) (btw, in your make_a_pixel function, unless you want to make the pixels updatable in place, you should also be using a tuple). Sparse datasets are extremely common, and dicts are a good way to [temporarily] store them. As someone mentioned before, dictionaries are essential to python. Don't go out of your way to avoid them, unless you have a /reason/ to do it. Btw, def get_color(point): return screen[point] is way more readable (and less obscure) than def get_color(point): return rows_of_pixels[point[0]][point[1]] Regards, -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From clp2 at rebertia.com Fri Jul 24 13:02:30 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 10:02:30 -0700 Subject: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox) In-Reply-To: References: Message-ID: <50697b2c0907241002o1cd9f262h853c2abe2ce534f6@mail.gmail.com> On Fri, Jul 24, 2009 at 2:38 AM, Piet van Oostrum wrote: >>>>>> Chris Rebert (CR) wrote: > >>CR> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: >>>> You can use tabnanny to help diagnose the problem: >>>> http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a > >>CR> Anyone know what's the deal with effbot.org? It seems to be broken at >>CR> present, forcing me to use Google's cached version. >>CR> It's a real shame since it has lots of handy Python info. > > Just try again. Yup, whatever the problem was, seems to be fixed now. Interesting. - Chris From roastie57 at gmail.com Fri Jul 24 13:14:31 2009 From: roastie57 at gmail.com (Uncle Roastie) Date: Fri, 24 Jul 2009 10:14:31 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <0505d29f-edeb-40a4-b513-6117ecf4de33@m18g2000vbi.googlegroups.com> On Jul 20, 12:27?pm, Phillip B Oldham wrote: > My colleagues and I have been working with python for around 6 months > now, and while we love a lot of what python has done for us and what > it enables us to do some of the decisions behind such certain > data-types and their related methods baffle us slightly (when compared > to the decisions made in other, similarly powerful languages). > > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > > Consider the following: > > >>> x = [2,1,3] > >>> x.sort() > >>> print x > > [1, 2, 3] > > Now, if the sort operations were unable to affect the original > structure of the list (as in JavaScript) you'd effectively have a > tuple which you could add/remove from, and the example above would > look more like: > > >>> x = [2,1,3] > >>> print x.sort() > [1, 2, 3] > >>> print x > > [2,1,3] > > This make a lot more sense to us, and follows the convention from > other languages. It would also mean chaining methods to manipulate > lists would be easier: > > >>> x = [2,1,3] > >>> print x.sort()[0] > 3 > >>> print x > > [2,1,3] > > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. > > We're not looking to start any arguments or religious wars and we're > not asking that python be changed into something its not. We'd simply > like to understand the decision behind the lists and tuple structures. > We feel that in not "getting" the difference between the two types we > may be missing out on using these data structures to their full > potential. A tuple can be used like a struct in C - the number of fields is meant to be fixed and should not be dynamically changed. From gert.cuykens at gmail.com Fri Jul 24 13:22:36 2009 From: gert.cuykens at gmail.com (gert) Date: Fri, 24 Jul 2009 10:22:36 -0700 (PDT) Subject: cgi.fieldstorage() Message-ID: this is a non standard way to store multi part post data on disk def application(environ, response): with open('/usr/httpd/var/wsgiTemp','w') as f: while True: chunk = environ['wsgi.input'].read(8192).decode('latin1') if not chunk: break f.write(chunk) response('200 OK',[]) return ['complete'] my question is how do i handle the file, so i can shuffle it into a db using small chunks of memorie ? From deets at nospam.web.de Fri Jul 24 13:27:16 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 19:27:16 +0200 Subject: SONAME for python modules is bad? (aka multiple module version) In-Reply-To: <2a2a36ca-72a0-470f-907c-2be43104617a@h18g2000yqj.googlegroups.com> References: <2a2a36ca-72a0-470f-907c-2be43104617a@h18g2000yqj.googlegroups.com> Message-ID: <7cu97kF29dnlbU1@mid.uni-berlin.de> mathieu schrieb: > As far as I know there has not been any consensus on how to install > multiple version of a same module in python ? What are the recommended > mechanism ? I use virtualenvs for everything. Especially on unixish OSes this usually works without problems. On windows, things are a bit different, as sometimes you only get binary installers that insist on installing into the base-installation. > I could not find any documentation on the subject. Does anyone sees > any issue with using standard SONAME mechanism when installing a > python module ? I don't understand that. You mean the .so.X.Y.Z-thingy under *ix? That would essentially be the pkg_resources.require-road, yes. But as it's not widely adopted, it will cause you troubles because some packages won't declare their dependencies properly. Diez From clp2 at rebertia.com Fri Jul 24 13:28:10 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 10:28:10 -0700 Subject: Popen In-Reply-To: <414673.50429.qm@web63108.mail.re1.yahoo.com> References: <414673.50429.qm@web63108.mail.re1.yahoo.com> Message-ID: <50697b2c0907241028s4f3ff1e6ma4f78ae2b61b2341@mail.gmail.com> > --- On Fri, 7/24/09, Diez B. Roggisch wrote: > >> From: Diez B. Roggisch >> Subject: Re: Popen >> To: python-list at python.org >> Date: Friday, July 24, 2009, 12:35 PM >> Tim schrieb: >> > Thanks! Yes I mean subprocess.Popen. >> > >> > I was wondering the meaning of "asynchronously" >> > Here is some code I am reading recently: >> > " >> > result = Popen(cmdline,shell=True,stdout=PIPE).stdout >> for line in result.readlines(): >> >? ???if find(line,"Cross") != -1: >> >? ? ? ???return >> float(split(line)[-1][0:-1]) " >> > The computation in the program "cmdline" takes a long >> time, at the end of which the results will be output to >> stdout. >> > >> > "asynchronous" seems to mean Popen returns to the >> parent process immediately and the parent and child >> processes continue to be executed. >> > However, if Popen returns immediately to the parent >> process, then there will be nothing in "result", not to >> mention extracting information from the output. Thus it >> seems to me the parent process has to wait till the child >> process finish. >> > >> > So how to understand the meaning of "asynchronous"? >> >> "Asynchronous" means asynchronous - the parent is *not* >> waiting. >> >> Which is the reason that there is (amongst other things) >> the "wait"-method you can call to wait for the child to be >> terminated. On Fri, Jul 24, 2009 at 9:52 AM, Tim wrote: > > Thanks! If that is the case, i.e. the parent doesn't wait, is the code in my last post wrong? "result" could be nothing. No, it will be a subprocess.Popen object representing the spawned process. Both the child process and the parent Python process will then be running simultaneously.This allows the Python program to interact with the subprocess via the Popen object while the subprocess is executing in parallel with Python. The asynchronicity means that the call to the Popen constructor does not wait for the spawned subprocess to terminate before returning the new Popen object to the parent Python program. Cheers, Chris -- http://blog.rebertia.com From deets at nospam.web.de Fri Jul 24 13:32:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 19:32:31 +0200 Subject: cgi.fieldstorage() In-Reply-To: References: Message-ID: <7cu9hhF29gt65U1@mid.uni-berlin.de> gert schrieb: > this is a non standard way to store multi part post data on disk > > def application(environ, response): > with open('/usr/httpd/var/wsgiTemp','w') as f: > while True: > chunk = environ['wsgi.input'].read(8192).decode('latin1') > if not chunk: break > f.write(chunk) > response('200 OK',[]) > return ['complete'] > > my question is how do i handle the file, so i can shuffle it into a db > using small chunks of memorie ? I don't think that's possible with the current DB-API. There is no stream-based BLOB-interface (as e.g. JDBC offers). So the answer certainly depends on your used RDBMS. For oracle, you would be lucky: http://cx-oracle.sourceforge.net/html/lob.html Other adapters I don't know about. Diez From clp2 at rebertia.com Fri Jul 24 13:41:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 10:41:13 -0700 Subject: trouble with wrapping a c program In-Reply-To: <20090724123458.TOWM14607.viefep17-int.chello.at@edge02.upc.biz> References: <20090724123458.TOWM14607.viefep17-int.chello.at@edge02.upc.biz> Message-ID: <50697b2c0907241041l10d13f09pa01abb505461e737@mail.gmail.com> On Fri, Jul 24, 2009 at 5:34 AM, Sanne Korzec wrote: > Hi Mailing, > > I am using a c program, which first initializes for some seconds and then > waits for user input (keyboard) to type something. When enter is pressed the > c program continues. > Using the keyboard and then enter in the c program prompt works, but I wish > to do this from the python script by sending the string from the python > script. > > I am able to print the string from python with a print command or with a > stdout.write command. But this simply prints it to the prompt, the c program > does nothing with this printed string. > > Is there a way to pipe, stream, or send this string to the running c > program? > Here is a small fragment of my code: > > #initialization > > cmd = [a list of my program and arguments] > > subprocess.Popen(cmd)?? #starts the c program import subprocess cmd = [a list of my program and arguments] process = subprocess.Popen(cmd, stdin=subprocess.PIPE) #starts the c program line = raw_input("Please enter a line of input for the C program:") process.stdin.write(line) process.stdin.write("\n") You might want to study the docs for the subprocess module: http://docs.python.org/library/subprocess.html Cheers, Chris -- http://blog.rebertia.com From kyrie at uh.cu Fri Jul 24 13:46:10 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Fri, 24 Jul 2009 13:46:10 -0400 Subject: len() should always return something In-Reply-To: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <200907241346.10966.kyrie@uh.cu> On Friday 24 July 2009 11:58:36 am Phillip M. Feldman wrote: > I've been converting Matlab codes to Python.? In Matlab, a scalar is just a > one-by-one matrix and has a length of 1.? This convention seems no less > arbitrary to me than Python's convention that the concept of length is not > applicable to ints and floats. Are you sure it isn't? (as opposed to being tainted by matlab?). Almost everywhere, a scalar and a tuple are different things. How many elements are "inside" the number 7? If you assume that 7 is a matrix, why should it be a two-dimensional matrix? Why not a vector, or a three dimensional matrix instead? (I don't use matlab, but in octave, size(7) returns [1,1], not [1] or [1,1,1,1]). That seems even more arbitrary to me. Of course, in a language like Matlab, that assumes that everything is a matrix, it makes sense for scalars to be mapped to a useful matrices. But that assumption is not natural. Even pure math makes a difference between scalars and vectors. Matrix x Matrix multiplication is not always defined (even when the second matrix contains exactly one element), while Matrix x Scalar always is. (Octave, btw, will demote a two-dimensional 1x1 matrix to a scalar for Matrix multiplication, which may hide errors) If you are converting from matlab, I'd say you have biggest things to worry about. As you said, you can just replace the len function (even safer may be to do [1]). Assignment, for instance, is /very/ different. > If there is only a single measurement, it is reasonable to allow the calling > program to pass a scalar without wrapping it up into a list or array. I try to avoid those automatic conversions, on the long run, I believe that most of them make the code more confusing for the caller. If you feel that you must allow that, my suggestion would be to create a method that will ensure that what you receive is a list (or whatever) and coerce its argument if not, and call that one at the beginning of your methods. Regards, [1] this will define len for any object, not only int and floats. === def size(x): try: return len(x) except TypeError: return 1,1 === -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From rhodri at wildebst.demon.co.uk Fri Jul 24 13:52:41 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 18:52:41 +0100 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Fri, 24 Jul 2009 16:10:07 +0100, Piet van Oostrum wrote: >>>>>> "Rhodri James" (RJ) wrote: > >> RJ> On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards >> wrote: >>>> On 2009-07-24, Dr. Phillip M. Feldman wrote: >>>>> >>>>> Some aspects of the Python design are remarkably clever, while >>>>> others leave me perplexed. Here's an example of the latter: >>>>> Why does len() give an error when applied to an int or float? >>>>> len() should always return something; in particular, when >>>>> applied to a scalar, it should return a value of 1. >>>> >>>> If len(7) returned a value of 1, then wouldn't one expect 7[0] >>>> to be valid? It isn't, so you'd then have to redefine all >>>> types so that they are sequences that can be indexed. Sounds >>>> like a big mess to me... >>>> >>>> [Are there types for which len() returns a value that can't be >>>> indexed?] >>>> > >> RJ> Dictionaries. > >> RJ> Which doesn't make your point less valid. In fact I'd go so >> RJ> far as to argue that what len() gives you is the number of >> RJ> items in a container, so len(7) should return 0. > > But len(7) could as well be defined as 3, 1, 32, or 64 (depending on the > implementation). Therefore it doesn't make much sense. Quite. -- Rhodri James *-* Wildebeest Herder to the Masses From andre.roberge at gmail.com Fri Jul 24 13:54:21 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Fri, 24 Jul 2009 10:54:21 -0700 (PDT) Subject: ElementTree's Element substitution in Python 3 Message-ID: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> I have a function to replace the content of an ElementTree Element by that of another one which works using Python 2 but not with Python 3. I get an assertion error. The function is as follows: def replace_element(elem, replacement): '''replace the content of an ElementTree Element by that of another one. ''' elem.clear() elem.text = replacement.text elem.tail = replacement.tail elem.tag = replacement.tag elem.attrib = replacement.attrib elem[:] = replacement[:] The last line is problematic. For example, if I do the following program with Python2.5 ### from xml.etree import ElementTree as et a = et.Element('a') b = et.SubElement(a, 'b') c = et.Element('c') a[:] = c[:] ### nothing of note happens - however, doing the same with Python 3.1, I get the following traceback: Traceback (most recent call last): File "test.py", line 7, in a[:] = c[:] File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line 210, in __setitem__ assert iselement(element) AssertionError ====== I would gladly welcome any suggestion for writing a replace_element() function that works with Python 3.1 Andr? From ronn.ross at gmail.com Fri Jul 24 13:56:29 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Fri, 24 Jul 2009 12:56:29 -0500 Subject: trouble with minidom In-Reply-To: References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Message-ID: <9c8c445f0907241056x11ec5316s2ea6310872cd342@mail.gmail.com> On Tue, Jul 21, 2009 at 7:32 PM, Gabriel Genellina wrote: > En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross > escribi?: > > > Hello I'm trying to read an xml file using minidome. The xml looks like: >> >> >> myProj >> /here/ >> >> >> >> My code looks like so: >> from xml.dom.minidom import parse >> >> dom = parse("myfile.xml") >> >> for node in dom.getElementsByTagName("project'): >> print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, >> node.childNodes[1]) >> >> Unfortunately, it returns 'nodeValue as none. I'm trying to read the value >> out of the node fir example name: myProj. I haven't found much help in the >> documentation. Can someone point me in the right direction? >> > > Unless you have a specific reason to use the DOM interface (like having a > masochistic mind), working with ElementTree usually is a lot easier: > > py> import xml.etree.ElementTree as ET > py> xml = """ > ... > ... myProj > ... /here/ > ... > ... """ > py> doc = ET.fromstring(xml) > py> for project in doc.findall('project'): > ... for child in project.getchildren(): > ... print child.tag, child.text > ... > name myProj > path /here/ > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > I have used the loop below and it works great, but I need get both child elements or 'project' per iteration. I want to build a dictionary that resemble this: my_dict = {'myProj':'/here/', 'anothername':'anotherpath'} I couldn't find how to do with in the element tree docs. Can you point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Fri Jul 24 14:03:59 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 11:03:59 -0700 Subject: len() should always return something In-Reply-To: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <50697b2c0907241103v5ad12d48re1857532c58903e8@mail.gmail.com> > Jul 24, 2009 07:02:29 AM, clp2 at rebertia.com wrote: > > On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. > Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while others >> leave >> me perplexed. Here's an example of the latter: Why does len() give an >> error >> when applied to an int or float? len() should always return something; in >> particular, when applied to a scalar, it should return a value of 1. Of >> course, I can define my own function like this: >> >> def mylen(x): >> ? if isinstance(x,int) or isinstance(x,float): return 1 >> ? return len(x) >> >> But, this shouldn't be necessary. > > The problem is that redefining len()/length/size that way would > violate several principles of Python's design (The "Zen" of Python - > http://www.python.org/dev/peps/pep-0020/). > > Specifically: > - Explicit is better than implicit. > - Special cases aren't special enough to break the rules. > - Errors should never pass silently. > - In the face of ambiguity, refuse the temptation to guess. > > If you'd explain the situation that prompts you to find this > redefinition necessary, I'm sure someone can suggest a better > approach. On Fri, Jul 24, 2009 at 8:58 AM, Phillip M. Feldman wrote: > I've read the "Zen of Python", but most of these aphorisms are vague and > could be understood differently by different readers. In particular, I > don't understand the statement that "explicit is better than implicit". > Some examples of this would be helpful. > > I've been converting Matlab codes to Python. In Matlab, a scalar is just a > one-by-one matrix and has a length of 1. This convention seems no less > arbitrary to me than Python's convention that the concept of length is not > applicable to ints and floats. My workaround was to write the following > function: > > def is_scalar(x): > """Return True if x is an instance of int, float, or complex. > Otherwise, return False. Note: If x is a length-1 list or array > containing an int, float, or complex value, False is returned.""" > if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex): > return True > return False > > The application is the following: In various types of scientific > applications, one operates on a list of measurements. If there is only a > single measurement, it is reasonable to allow the calling program to pass a > scalar without wrapping it up into a list or array. You could use Python's extended call syntax when defining your function: def average(*args): return sum(args) / len(args) average(7) #==> 7 average(2,3,4,5,6) #==> 4 average(*[2,3,4,5,6]) #==> 4 average([2,3,4,5,6]) #==> error Cheers, Chris -- http://blog.rebertia.com From rhodri at wildebst.demon.co.uk Fri Jul 24 14:05:58 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 19:05:58 +0100 Subject: non-owning references? In-Reply-To: References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: On Fri, 24 Jul 2009 16:03:58 +0100, Piet van Oostrum wrote: >>>>>> "Rhodri James" (RJ) wrote: > >> RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic >> wrote: >>>> Ben Finney writes: >>>> >>>>> Utpal Sarkar writes: >>>>> >>>>>> Is there a way I can tell a variable that the object it is pointing >>>>>> too is not owned by it, in the sense that if it is the only >>>>>> reference >>>>>> to the object it can be garbage collected? >>>>> >>>>> Python doesn't have ?pointers?, and doesn't really have ?variables? >>>>> either, at least not how many other languages use that term. >>>> >>>> The OP didn't use the term "pointer", but the word "pointing", which >>>> makes sense in the context. The term "variable" is used in the Python >>>> language reference and elsewhere, and is quite compatible with how >>>> other >>>> popular languages (Java, PHP, Lisp, ...) use it. > >> RJ> Only superficially. Treating Python variables the same as C >> variables >> RJ> (say) is one of the classic ways that newbies come unstuck when >> mutable >> RJ> objects appear on the scene. While the OP appears to have the >> right idea, >> RJ> your "correction" here could be quite misleading. > > If you read the OP, it is clear that he talked about a class variable, > which is a perfectly legal notion in Python, and is mentioned as such in > the language reference manual: > `Variables defined in the class definition are class variables' Yes. I didn't think I needed to say that explicitly. > And who was talking about C variables? Hrvoje, implicitly. 'The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it.' I listed C as another example of a popular language because I am very familiar with how C's variables work; I don't know Java, I've never programmed PHP in anger and it's twenty years since I last touched Lisp. The point was, and remains, that this newsgroup gets regular traffic from people who expect Python's variables to act like C's variables, demonstrating that describing them as "quite compatible" is somewhat misleading. -- Rhodri James *-* Wildebeest Herder to the Masses From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Fri Jul 24 14:06:32 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Fri, 24 Jul 2009 14:06:32 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-23 23:51:02 -0400, Carl Banks said: > On Jul 23, 5:52?pm, Rui Maciel wrote: >> fft1976 wrote: >>> How do you explain that something as inferior as Python beat Lisp in >>> the market place despite starting 40 years later. >> >> Probably due to similar reasons that lead php to become remotely relevant > . > > Well, the only reason PHP became relevant because it was an > easy ^^^^^^^^ ^^^^^^^^ (emphasis added) > to > deploy solution in a single application domain, the web, that happened > to explode. i.e., Python "beat" lisp because it is ~70% of lisp in a form that is much more palatable to the average programmer, just as php became popular because it is powerful enough to do websites and, most importantly, apprehensible to mediocre programmers and even some non-programmers. -- Raffael Cavallaro From gert.cuykens at gmail.com Fri Jul 24 14:10:17 2009 From: gert.cuykens at gmail.com (gert) Date: Fri, 24 Jul 2009 11:10:17 -0700 (PDT) Subject: cgi.fieldstorage() References: <7cu9hhF29gt65U1@mid.uni-berlin.de> Message-ID: On Jul 24, 7:32?pm, "Diez B. Roggisch" wrote: > gert schrieb: > > > this is a non standard way to store multi part post data on disk > > > def application(environ, response): > > ? ? with open('/usr/httpd/var/wsgiTemp','w') as f: > > ? ? ? ? while True: > > ? ? ? ? ? ? chunk = environ['wsgi.input'].read(8192).decode('latin1') > > ? ? ? ? ? ? if not chunk: break > > ? ? ? ? ? ? f.write(chunk) > > ? ? response('200 OK',[]) > > ? ? return ['complete'] > > > my question is how do i handle the file, so i can shuffle it into a db > > using small chunks of memorie ? > > I don't think that's possible with the current DB-API. There is no > stream-based BLOB-interface (as e.g. JDBC offers). > > So the answer certainly depends on your used RDBMS. For oracle, you > would be lucky: > > http://cx-oracle.sourceforge.net/html/lob.html > > Other adapters I don't know about. sqlite :) ok let say for now it would be impossible on a db level, but before i reach the impossible, i still need to parse the file to prepare the chunks. How do i do that ? How do i get the chunks without loading the hole file into memorie ? b = environ['CONTENT_TYPE'].split('boundary=')[1] data = search(b+r'.*?Content-Type: application/octet-stream\r\n\r \n (.*?)\r\n--'+b,t,DOTALL).group(1) data = data.encode('latin1') From jakecjacobson at gmail.com Fri Jul 24 14:24:58 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Fri, 24 Jul 2009 11:24:58 -0700 (PDT) Subject: exceptions.TypeError an integer is required Message-ID: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> I am trying to do a post to a REST API over HTTPS and requires the script to pass a cert to the server. I am getting "exceptions.TypeError an integer is required" error and can't find the reason. I commenting out the lines of code, it is happening on the connection.request() line. Here is the problem code. Would love some help if possible. head = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"} parameters = urlencode({"collection" : collection, "entryxml" : open (file,'r').read()}) try: connection = httplib.HTTPSConnection(host, port, key_file, cert_file) connection.request('POST', path, parameters, head) response = connection.getresponse() print response.status, response.reason except: print sys.exc_type, sys.exc_value connection.close() From piet at cs.uu.nl Fri Jul 24 14:39:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 20:39:46 +0200 Subject: Predefined Variables References: Message-ID: >>>>> Scott David Daniels (SDD) wrote: >SDD> Stephen Cuppett (should have written in this order): >>> "Fred Atkinson" wrote ... >>>> Is there a pre-defined variable that returns the GET line >>>> >>>> (http://www.php.net/index.php?everythingafterthequestionmark) as a >>>> single variable (rather than individual variables)? >>> os.environment('QUERY_STRING') >SDD> Maybe you mean: >SDD> os.environ['USER'] Let's take the best of both: os.environ['QUERY_STRING'] -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From maymunbeyin at gmail.com Fri Jul 24 14:51:54 2009 From: maymunbeyin at gmail.com (kk) Date: Fri, 24 Jul 2009 11:51:54 -0700 (PDT) Subject: How can I get the line number ? Message-ID: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> Hello I am writing some Python code that runs in another application(has wrapper functions). Due to lack of debugging I am printing out alot of outputs and manual messages. I want to be able to create a function that would let me print the current line number that is called from. This is not for debugging exceptions it is rather to simplify my debug messages, at least I can trace my debug messages. thanks From tjreedy at udel.edu Fri Jul 24 14:52:38 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 14:52:38 -0400 Subject: len() should always return something In-Reply-To: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: Phillip M. Feldman wrote: > I've been converting Matlab codes to Python. In Matlab, a scalar is > just a one-by-one matrix and has a length of 1. This convention seems > no less arbitrary to me than Python's convention that the concept of > length is not applicable to ints and floats. Multiplication of a vector/matrix by a scalar always defined and commutative. Multiplication of a vector/matrix by a 1x1 matrix is not always even defined. So not having scalars in a matrix package strikes me as a bit odd. > My workaround was to write > the following function: > > def is_scalar(x): > """Return True if x is an instance of int, float, or complex. > Otherwise, return False. Note: If x is a length-1 list or array > containing an int, float, or complex value, False is returned.""" > if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex): Better: if isinstance(x, (int, float, complex)): but you forgot decimals and fractions and any other possible number modules. In 3.1, >>> from numbers import Number >>> from decimal import Decimal >>> from fractions import Fraction >>> for x in 1, 1.0, (1+0j), Decimal(1), Fraction(1,1): isinstance(x, Number) True True True True True and the same for any other module that registers a class as a Number > return True > return False > > The application is the following: In various types of scientific > applications, one operates on a list of measurements. If there is only > a single measurement, it is reasonable to allow the calling program to > pass a scalar without wrapping it up into a list or array. If you want to do that, start with def f(x): try: len(x) except TypeError: x = x, or in 3.1 use Number test above. Terry Jan Reedy From mkhitrov at gmail.com Fri Jul 24 14:59:04 2009 From: mkhitrov at gmail.com (Maxim Khitrov) Date: Fri, 24 Jul 2009 14:59:04 -0400 Subject: How can I get the line number ? In-Reply-To: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> References: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> Message-ID: <26ddd1750907241159q3799e4c7k5a883d3b45feb4a5@mail.gmail.com> On Fri, Jul 24, 2009 at 2:51 PM, kk wrote: > Hello > > I am writing some Python code that runs in another application(has > wrapper functions). Due to lack of debugging I am printing out alot of > outputs and manual messages. I want to be able to create a function > that would let me print the current line number that is called from. > This is not for debugging exceptions it is rather to simplify my debug > messages, at least I can trace my debug messages. > > thanks Modify the following as needed: from inspect import currentframe, getframeinfo def caller_info(depth=0): """ Get file, line number, and name of the calling function. """ if depth < 0: raise ValueError('invalid stack depth') caller = frame = currentframe() try: for i in xrange(-1, depth): caller = caller.f_back if caller is None: return (None, None, None) return getframeinfo(caller, 0)[:3] finally: del caller, frame - Max From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 14:59:40 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 18:59:40 GMT Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <0279f596$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >> Nah. 7 contains three bits, so len(7) should *clearly* return 3. > > and len("7") must return 8, by the same token... but wait! > > >>> len("7") > 1 > >>> > >>> > my python installation must me outdated ;-) No no no, you're obviously using an awesome version of Python that can compress single-character strings to a single bit! -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:00:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:00:14 GMT Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: <0279f5b9$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 15:55:45 +0200, Hrvoje Niksic wrote: > The term "variable" is used in the Python > language reference and elsewhere, and is quite compatible with how other > popular languages (Java, PHP, Lisp, ...) use it. Please stop > complaining about valid terminology; it is not helpful. No, the use of the single term "variable" to describe two distinct program models is not helpful. Whether other languages muddy the water between memory-location based variables and name-binding is irrelevant to whether we should do so. And quite a few of us are disappointed that the Python language reference should confuse the issue by using misleading terminology. Unfortunately, the use of "variable" is so ingrained, and so simple compared to name binding terminology, that I fear we'll never eradicate it. I know sometimes I use it myself, but always with a little shiver of shame that I'm misusing terminology. -- Steven From robert.kern at gmail.com Fri Jul 24 15:01:47 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 24 Jul 2009 14:01:47 -0500 Subject: Convert points to polygon shapefile In-Reply-To: <20090724101638.E93B414B2A@smtp3.ualg.pt> References: <20090724101638.E93B414B2A@smtp3.ualg.pt> Message-ID: On 2009-07-24 05:21, Luis Pedro Almeida wrote: > Dear all, > > I would like to know how to convert a list of points into a polygon > shapefile (esri). shapelib has Python bindings. http://shapelib.maptools.org/ -- 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 roy at panix.com Fri Jul 24 15:03:29 2009 From: roy at panix.com (Roy Smith) Date: Fri, 24 Jul 2009 15:03:29 -0400 Subject: list vs. tuple [Re: len() should always return something] References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: In article , Terry Reedy wrote: > Better: if isinstance(x, (int, float, complex)): I never noticed this before, but it seems odd that the second argument to isinstance() should be a tuple. Using the normal arguments made about tuples vs. lists, it seems like a list would be the right data structure here. I suppose a set would be even more right, but (I'm pretty sure) isinstance() predates sets. I'm curious why a tuple was chosen. From roy at panix.com Fri Jul 24 15:04:55 2009 From: roy at panix.com (Roy Smith) Date: Fri, 24 Jul 2009 15:04:55 -0400 Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: > > >> Nah. 7 contains three bits, so len(7) should *clearly* return 3. > > > > and len("7") must return 8, by the same token... but wait! > > > > >>> len("7") > > 1 > > >>> > > >>> > > my python installation must me outdated ;-) > > No no no, you're obviously using an awesome version of Python that can > compress single-character strings to a single bit! Compressing strings to a single bit is easy. It's the uncompressing that's tricky. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:05:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:05:11 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> Message-ID: <0279f6df$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 00:02:28 -0700, Chris Rebert wrote: > On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. > Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while others >> leave me perplexed. Here's an example of the latter: Why does len() >> give an error when applied to an int or float? len() should always >> return something; in particular, when applied to a scalar, it should >> return a value of 1. Of course, I can define my own function like this: >> >> def mylen(x): >> ? if isinstance(x,int) or isinstance(x,float): return 1 return len(x) >> >> But, this shouldn't be necessary. > > The problem is that redefining len()/length/size that way would violate > several principles of Python's design (The "Zen" of Python - > http://www.python.org/dev/peps/pep-0020/). > > Specifically: > - Explicit is better than implicit. > - Special cases aren't special enough to break the rules. > - Errors should never pass silently. > - In the face of ambiguity, refuse the temptation to guess. Chris, I'm curious why you think that these Zen are relevant to the OP's complaint. Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). Arguably (I wouldn't argue this, but some people might) ints aren't "special enough" to break the rule that len(obj) should always return something. (I don't actually agree, but some people might be able to produce a coherent argument why len() should apply equally to all objects.) Re errors passing silently, the OP doesn't believe that len(42) should be an error, so that's not relevant. And there's nothing ambiguous about len(42). I agree with the current Python behaviour, but I don't think there's anything in the Zen to support it. As far as I know, there is no programming language which treats scalars like ints as if they were vectors of length 1, which makes Python's choice to make ints unlengthed a no-brainer. -- Steven From andre.roberge at gmail.com Fri Jul 24 15:07:32 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Fri, 24 Jul 2009 12:07:32 -0700 (PDT) Subject: ElementTree's Element substitution in Python 3 References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: <9923e1c2-aabe-40d3-9c55-830991ad2dc9@q11g2000yqi.googlegroups.com> Sorry for replying to myself ... the following seems to be a working solution to my original problem. On Jul 24, 2:54?pm, Andr? wrote: > I have a function to replace the content of an ElementTree Element by > that of another one which works using Python 2 but not with Python 3. > I get an assertion error. ?The function is as follows: > > def replace_element(elem, replacement): > ? ? '''replace the content of an ElementTree Element by that of > another > ? ? ? ?one. > ? ? ''' > ? ? elem.clear() > ? ? elem.text = replacement.text > ? ? elem.tail = replacement.tail > ? ? elem.tag = replacement.tag > ? ? elem.attrib = replacement.attrib > ? ? elem[:] = replacement[:] > Use instead: def replace_element(elem, replacement): '''replace the content of an ElementTree Element by that of another one. ''' elem.clear() elem.text = replacement.text elem.tail = replacement.tail elem.tag = replacement.tag elem.attrib = replacement.attrib try: elem[:] = replacement[:] # works with Python 2.x (fast) but not 3.x except AssertionError: del elem[:] for child in replacement: elem.append(child) Andr? From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:11:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:11:33 GMT Subject: exceptions.TypeError an integer is required References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> Message-ID: <0279f85d$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 11:24:58 -0700, jakecjacobson wrote: > I am trying to do a post to a REST API over HTTPS and requires the > script to pass a cert to the server. I am getting "exceptions.TypeError > an integer is required" error and can't find the reason. I commenting > out the lines of code, it is happening on the connection.request() line. > Here is the problem code. Would love some help if possible. Please post the traceback that you get. My guess is that you are passing a string instead of an integer, probably for the port. [...] > except: > print sys.exc_type, sys.exc_value As a general rule, a bare except of that fashion is bad practice. Unless you can explain why it is normally bad practice, *and* why your case is an exception (no pun intended) to the rule "never use bare except clauses", I suggest you either: * replace "except:" with "except Exception:" instead. * better still, re-write the entire try block as: try: [code goes here] finally: connection.close() and use the Python error-reporting mechanism instead of defeating it. -- Steven From piet at cs.uu.nl Fri Jul 24 15:17:10 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 21:17:10 +0200 Subject: ElementTree's Element substitution in Python 3 References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: >>>>> Andr? (A) a ?crit: >A> I have a function to replace the content of an ElementTree Element by >A> that of another one which works using Python 2 but not with Python 3. >A> I get an assertion error. The function is as follows: >A> def replace_element(elem, replacement): >A> '''replace the content of an ElementTree Element by that of >A> another >A> one. >A> ''' >A> elem.clear() >A> elem.text = replacement.text >A> elem.tail = replacement.tail >A> elem.tag = replacement.tag >A> elem.attrib = replacement.attrib >A> elem[:] = replacement[:] >A> The last line is problematic. For example, if I do the following >A> program with Python2.5 >A> ### >A> from xml.etree import ElementTree as et >A> a = et.Element('a') >A> b = et.SubElement(a, 'b') >A> c = et.Element('c') >A> a[:] = c[:] >A> ### >A> nothing of note happens - however, doing the same with Python 3.1, I >A> get the following traceback: >A> Traceback (most recent call last): >A> File "test.py", line 7, in >A> a[:] = c[:] >A> File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line >A> 210, in __setitem__ >A> assert iselement(element) >A> AssertionError This is a Python bug. Please report it. The problem is that in Python 3 slice assignments are done with __setitem__ rather than __setslice__ but ElementTree has not been adapted to that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From tjreedy at udel.edu Fri Jul 24 15:17:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 15:17:15 -0400 Subject: Adding method from one class to another class or to instance of another class In-Reply-To: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> References: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Message-ID: marekw2143 wrote: > Hi, > > I have one class (A) that has defined method createVars. I would like > to add that method to class B > The code looks like this: > > > class A(object): > def createVars(self): > self.v1 = 1 > self.v2 = 3 > pass > > class B(object): > pass > > > I don't want to use inheritance (because class A has many methods > defined that class B doesn't need). > When I try the folloowing: > > > B.createVars = C.createVars you meant A.createVars > B().createVars() > > > then the following error occurs: > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got nothing instead) In 3.1, your example works fine. The difference is that in 2.x, B.createVars is a method wrapperthat wraps the function, whereas in 3.1, it is the function itself. For 2.x, you need to extract the function from the wrapper. It is im_func or something like that. Use dir(B.createVars) to check for sure. > How can I solve this problem? Terry Jan Reedy From linuxguy123 at gmail.com Fri Jul 24 15:17:24 2009 From: linuxguy123 at gmail.com (Linuxguy123) Date: Fri, 24 Jul 2009 13:17:24 -0600 Subject: Eclipse Pydev update error ? Message-ID: <1248463044.8697.50.camel@localhost.localdomain> Does anyone know why this error is occurring in my Eclipse Pydev update ? An error occurred while collecting items to be installed No repository found containing: org.python.pydev/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.ast/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.core/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.debug/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 No repository found containing: org.python.pydev.help/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.jython/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.parser/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.templates/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.customizations/osgi.bundle/1.4.7.2843 Thanks From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:18:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:18:09 GMT Subject: list vs. tuple [Re: len() should always return something] References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <0279f9e9$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: > In article , > Terry Reedy wrote: > >> Better: if isinstance(x, (int, float, complex)): > > I never noticed this before, but it seems odd that the second argument > to isinstance() should be a tuple. Using the normal arguments made > about tuples vs. lists, it seems like a list would be the right data > structure here. What would be the point of using a list? You're never going to sort it, or append items to it, or otherwise mutate it. You build it, pass it to a function which doesn't modify it in any fashion, then it gets garbage collected. > I suppose a set would be even more right, but (I'm > pretty sure) isinstance() predates sets. Yes. [steve at sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> isinstance >>> set Traceback (innermost last): File "", line 1, in ? NameError: set > I'm curious why a tuple was chosen. Tuples are smaller and faster to build than lists -- they're the most lightweight sequence type in Python. You don't need all the extra functionality of lists, so why go to the time and effort of building a list? -- Steven From maymunbeyin at gmail.com Fri Jul 24 15:23:58 2009 From: maymunbeyin at gmail.com (kk) Date: Fri, 24 Jul 2009 12:23:58 -0700 (PDT) Subject: How can I get the line number ? References: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> Message-ID: <3ce2fe1a-c769-4f92-b4b0-559229158297@33g2000vbe.googlegroups.com> Maxim, Thank you so much. I will try right now. From fabiofz at gmail.com Fri Jul 24 15:43:36 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Fri, 24 Jul 2009 16:43:36 -0300 Subject: Eclipse Pydev update error ? In-Reply-To: <1248463044.8697.50.camel@localhost.localdomain> References: <1248463044.8697.50.camel@localhost.localdomain> Message-ID: On Fri, Jul 24, 2009 at 4:17 PM, Linuxguy123 wrote: > Does anyone know why this error is occurring in my Eclipse Pydev > update ? > > An error occurred while collecting items to be installed > ?No repository found containing: > org.python.pydev/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.ast/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.core/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.debug/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 > ?No repository found containing: > org.python.pydev.help/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.jython/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.parser/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.templates/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.customizations/osgi.bundle/1.4.7.2843 > This usually happens if there was some connection error during the update. You can try other mirrors (see: http://pydev.blogspot.com/2009/07/pydev-147-released.html for the new added mirrors). Cheers, Fabio From tjreedy at udel.edu Fri Jul 24 15:45:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 15:45:36 -0400 Subject: ElementTree's Element substitution in Python 3 In-Reply-To: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: Andr? wrote: > I have a function to replace the content of an ElementTree Element by > that of another one which works using Python 2 but not with Python 3. > I get an assertion error. The function is as follows: > > def replace_element(elem, replacement): > '''replace the content of an ElementTree Element by that of > another > one. > ''' > elem.clear() > elem.text = replacement.text > elem.tail = replacement.tail > elem.tag = replacement.tag > elem.attrib = replacement.attrib > elem[:] = replacement[:] > > The last line is problematic. For example, if I do the following > program with Python2.5 > ### > from xml.etree import ElementTree as et > > a = et.Element('a') > b = et.SubElement(a, 'b') > c = et.Element('c') > > a[:] = c[:] > ### > nothing of note happens - however, doing the same with Python 3.1, I > get the following traceback: > > Traceback (most recent call last): > File "test.py", line 7, in > a[:] = c[:] > File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line > 210, in __setitem__ > assert iselement(element) > AssertionError > > ====== > I would gladly welcome any suggestion for writing a replace_element() > function that works with Python 3.1 My guess is that you found a subtle bug in the 3.1 version of ElementTree, perhap a result of conversion. I would take a look at the noted line 210 to see whether it is 'a' or 'c' that is not passing as an element. Assuming that it is 'c', I would look at __getitem__ to see why not. Is [:] special-cased? Compare the codes for 2.5 and 3.1. Assuming it still looks like a bug, report it on the tracker using your minimal example, leaving out your function. tjr From tjreedy at udel.edu Fri Jul 24 15:47:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 15:47:36 -0400 Subject: exceptions.TypeError an integer is required In-Reply-To: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> Message-ID: jakecjacobson wrote: > I am trying to do a post to a REST API over HTTPS and requires the > script to pass a cert to the server. I am getting > "exceptions.TypeError an integer is required" error and can't find the > reason. I commenting out the lines of code, it is happening on the > connection.request() line. Here is the problem code. Would love some > help if possible. > > head = {"Content-Type" : "application/x-www-form-urlencoded", > "Accept" : "text/plain"} > parameters = urlencode({"collection" : collection, "entryxml" : open > (file,'r').read()}) > try: > connection = httplib.HTTPSConnection(host, port, key_file, > cert_file) > connection.request('POST', path, parameters, head) > response = connection.getresponse() > print response.status, response.reason > except: > print sys.exc_type, sys.exc_value > > connection.close() Help us help you by posting the full actual traceback. From andre.roberge at gmail.com Fri Jul 24 15:48:02 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Fri, 24 Jul 2009 12:48:02 -0700 (PDT) Subject: ElementTree's Element substitution in Python 3 References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: <7ea510e0-ceb8-4425-9af2-b30154ad6e6d@b14g2000yqd.googlegroups.com> On Jul 24, 4:17?pm, Piet van Oostrum wrote: > >>>>> Andr? (A) a ?crit: > >A> I have a function to replace the content of an ElementTree Element by > >A> that of another one which works using Python 2 but not with Python 3. > >A> I get an assertion error. [SNIP] > >A> Traceback (most recent call last): > >A> ? File "test.py", line 7, in > >A> ? ? a[:] = c[:] > >A> ? File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line > >A> 210, in __setitem__ > >A> ? ? assert iselement(element) > >A> AssertionError > > This is a Python bug. Please report it. Done. > The problem is that in Python 3 > slice assignments are done with __setitem__ rather than __setslice__ but > ElementTree has not been adapted to that. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org From linuxguy123 at gmail.com Fri Jul 24 15:57:40 2009 From: linuxguy123 at gmail.com (Linuxguy123) Date: Fri, 24 Jul 2009 13:57:40 -0600 Subject: Eclipse Pydev update error ? In-Reply-To: References: <1248463044.8697.50.camel@localhost.localdomain> Message-ID: <1248465460.8697.51.camel@localhost.localdomain> On Fri, 2009-07-24 at 16:43 -0300, Fabio Zadrozny wrote: > On Fri, Jul 24, 2009 at 4:17 PM, Linuxguy123 wrote: > > Does anyone know why this error is occurring in my Eclipse Pydev > > update ? > > > > An error occurred while collecting items to be installed > > No repository found containing: > > org.python.pydev/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.ast/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.core/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.debug/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 > > No repository found containing: > > org.python.pydev.help/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.jython/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.parser/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.templates/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.customizations/osgi.bundle/1.4.7.2843 > > > > This usually happens if there was some connection error during the > update. You can try other mirrors (see: > http://pydev.blogspot.com/2009/07/pydev-147-released.html for the new > added mirrors). Excellent reply. Thanks. I added http://fabioz.com/pydev/updates to my site list and the update completed immediately. I'll bookmark the blog as well. Thanks again. From clp2 at rebertia.com Fri Jul 24 16:02:24 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 13:02:24 -0700 Subject: len() should always return something In-Reply-To: <0279f6df$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: <50697b2c0907241302m166d0c61y4912a29aaa4caaf1@mail.gmail.com> On Fri, Jul 24, 2009 at 12:05 PM, Steven D'Aprano wrote: > On Fri, 24 Jul 2009 00:02:28 -0700, Chris Rebert wrote: > >> On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. >> Feldman wrote: >>> >>> Some aspects of the Python design are remarkably clever, while others >>> leave me perplexed. Here's an example of the latter: Why does len() >>> give an error when applied to an int or float? len() should always >>> return something; in particular, when applied to a scalar, it should >>> return a value of 1. Of course, I can define my own function like this: >>> >>> def mylen(x): >>> ? if isinstance(x,int) or isinstance(x,float): return 1 return len(x) >>> >>> But, this shouldn't be necessary. >> >> The problem is that redefining len()/length/size that way would violate >> several principles of Python's design (The "Zen" of Python - >> http://www.python.org/dev/peps/pep-0020/). >> >> Specifically: >> - Explicit is better than implicit. >> - Special cases aren't special enough to break the rules. >> - Errors should never pass silently. >> - In the face of ambiguity, refuse the temptation to guess. > > > Chris, I'm curious why you think that these Zen are relevant to the OP's > complaint. To explain in more detail: > Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). If you want a collection (something that has length), then one should explicitly create one, not implicitly have a singleton value act like it's a pseudo-collection. I admit I'm somewhat conflating this principle with the anti-ambiguity principle, but the two are related, imho. > Arguably (I wouldn't argue this, but some people might) ints aren't > "special enough" to break the rule that len(obj) should always return > something. Except that's not the current rule. The current rule is that it's defined only for collections. One would instead have to argue why ints are special enough to have len() defined despite not being collections. I think the point made by Grant Edwards is instructive. len(x) = 1 typically implies list(x)[0] and similar should be valid. Altering the behavior would invalidate that theorem and cause quite a bit of code upheaval, all just to save the OP from typing one pair of []s. > (I don't actually agree, but some people might be able to produce a > coherent argument why len() should apply equally to all objects.) Well, yes, this /whole/ "argument" is entirely academic; the behavior is extremely unlikely to change, we're just trying to give ex post facto rationales for pedagogical purposes. :) > Re errors passing silently, the OP doesn't believe that len(42) should be > an error, so that's not relevant. True, it would not directly silence an error, but defining len() on scalars would tend towards obscuring errors in code that incorrectly treats scalars as collections. > And there's nothing ambiguous about len(42). Really? What is its value then? I think arguments of varying quality can be made for: 1 - as the OP and those from array programming languages would suggest 2 - the number of decimal digits in 42, if one was feeling Perlish 6 - the minimum number of bits necessary to represent 42 in binary 32 (or 64, depending on your CPU) - the number of bits necessary to represent an int (obviously breaks down a bit with arbitrary-magnitude ints) undefined - i.e. it causes an error, the current behavior; asking for the length of a non-collection is "nonsensical" or "absurd" > I agree with the current Python behaviour, but I don't think there's > anything in the Zen to support it. As far as I know, there is no The problem and strength of the Zen is that it's all about how you interpret it. :-) Cheers, Chris -- http://blog.rebertia.com From marcusw at cox.net Fri Jul 24 16:09:15 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 24 Jul 2009 16:09:15 -0400 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 7/24/2009 3:04 PM, Roy Smith wrote: > In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, > Steven D'Aprano wrote: > >> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >> >>>> Nah. 7 contains three bits, so len(7) should *clearly* return 3. >>> and len("7") must return 8, by the same token... but wait! >>> >>> >>> len("7") >>> 1 >>> >>> >>> >>> >>> my python installation must me outdated ;-) >> No no no, you're obviously using an awesome version of Python that can >> compress single-character strings to a single bit! > > Compressing strings to a single bit is easy. It's the uncompressing that's > tricky. I assume you mean ord("7")%2? First one to correctly decompress the value 0 into an ASCII character wins the title of the world's most capable hacker :p Marcus From breamoreboy at yahoo.co.uk Fri Jul 24 16:18:26 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 24 Jul 2009 21:18:26 +0100 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: Marcus Wanner wrote: > On 7/24/2009 3:04 PM, Roy Smith wrote: >> In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, >> Steven D'Aprano wrote: >> >>> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >>> >>>>> Nah. 7 contains three bits, so len(7) should *clearly* return 3. >>>> and len("7") must return 8, by the same token... but wait! >>>> >>>> >>> len("7") >>>> 1 >>>> >>> >>>> >>> >>>> my python installation must me outdated ;-) >>> No no no, you're obviously using an awesome version of Python that >>> can compress single-character strings to a single bit! >> >> Compressing strings to a single bit is easy. It's the uncompressing >> that's tricky. > I assume you mean ord("7")%2? > > First one to correctly decompress the value 0 into an ASCII character > wins the title of the world's most capable hacker :p > > Marcus asciichar = chr(len(0)) if the OP's wishes come true? -- Kindest regards. Mark Lawrence. From marcusw at cox.net Fri Jul 24 16:25:57 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 24 Jul 2009 16:25:57 -0400 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 7/24/2009 4:18 PM, Mark Lawrence wrote: > Marcus Wanner wrote: >> On 7/24/2009 3:04 PM, Roy Smith wrote: >>> In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, >>> Steven D'Aprano wrote: >>> >>>> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >>>> >>>>>> Nah. 7 contains three bits, so len(7) should *clearly* return 3. >>>>> and len("7") must return 8, by the same token... but wait! >>>>> >>>>> >>> len("7") >>>>> 1 >>>>> >>> >>>>> >>> >>>>> my python installation must me outdated ;-) >>>> No no no, you're obviously using an awesome version of Python that >>>> can compress single-character strings to a single bit! >>> >>> Compressing strings to a single bit is easy. It's the uncompressing >>> that's tricky. >> I assume you mean ord("7")%2? >> >> First one to correctly decompress the value 0 into an ASCII character >> wins the title of the world's most capable hacker :p >> >> Marcus > asciichar = chr(len(0)) if the OP's wishes come true? > Nope, wasn't "?"... Marcus From python.list at tim.thechases.com Fri Jul 24 16:30:10 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 24 Jul 2009 15:30:10 -0500 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A6A19D2.2020204@tim.thechases.com> Marcus Wanner wrote: > First one to correctly decompress the value 0 into an ASCII > character wins the title of the world's most capable hacker :p Bah...uncompressing the value 0 into *an* ASCII character is easy. Uncompressing it into the *original* ASCII character from which it was compressed (with the aforementioned compression method) becomes a bit trickier ;-) -tkc From clp2 at rebertia.com Fri Jul 24 16:34:11 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 13:34:11 -0700 Subject: len() should always return something In-Reply-To: <4A6A19D2.2020204@tim.thechases.com> References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> <4A6A19D2.2020204@tim.thechases.com> Message-ID: <50697b2c0907241334t660fce21l3ae20e7772561cef@mail.gmail.com> On Fri, Jul 24, 2009 at 1:30 PM, Tim Chase wrote: > Marcus Wanner wrote: >> >> First one to correctly decompress the value 0 into an ASCII >> character wins the title of the world's most capable hacker :p > > Bah...uncompressing the value 0 into *an* ASCII character is easy. > ?Uncompressing it into the *original* ASCII character from which it was > compressed (with the aforementioned compression method) becomes a bit > trickier ;-) Deja vu: http://www.hackles.org/cgi-bin/archives.pl?request=310 - Chris -- http://blog.rebertia.com From tismer at stackless.com Fri Jul 24 16:39:36 2009 From: tismer at stackless.com (Christian Tismer) Date: Fri, 24 Jul 2009 13:39:36 -0700 Subject: ANN: psyco V2 In-Reply-To: <4a696af3$0$442$426a74cc@news.free.fr> References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> <4a696af3$0$442$426a74cc@news.free.fr> Message-ID: <4A6A1C08.6000202@stackless.com> On 7/24/09 1:04 AM, William Dode wrote: > On 23-07-2009, Christian Tismer wrote: ... >> Wasn't the project plan saying the opposite, borrowing >> some ideas from psyco? :-) >> http://code.google.com/p/unladen-swallow/wiki/ProjectPlan > > How do you see the future of psyco when unladen-swallow will grab the > best of psyco (if they can !) ? I have no objections, but also no idea so far how that could work. Sounded like an unreflected idea to me, without seriously checking the possibilities and/or implications. The same kind of research apparently did not happen concerning PyPy, which IMHO is much more suitable to take advantage from. This is for the current status of psyco, of course. It will change dramatically in the next months, slowly adopting some of PyPy's ideas. Then it might start to make more sense. > Wait and see ? Wait and see, not holding breath, I'd say. > Anyway, thanks a lot for your work that we can use NOW ! Thanks for so much encouraging reactions! cheers - chris -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From tjreedy at udel.edu Fri Jul 24 16:43:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 16:43:17 -0400 Subject: list vs. tuple [Re: len() should always return something] In-Reply-To: <0279f9e9$0$5185$c3e8da3@news.astraweb.com> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> <0279f9e9$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: > >> In article , >> Terry Reedy wrote: >> >>> Better: if isinstance(x, (int, float, complex)): >> I never noticed this before, but it seems odd that the second argument >> to isinstance() should be a tuple. Using the normal arguments made >> about tuples vs. lists, it seems like a list would be the right data >> structure here. I ignore the 'normal arguments' and it seems that Guido or the designer of isinstance did so here too. Fortunately. Practicality beats 'purity', especially misguided purity. > What would be the point of using a list? You're never going to sort it, > or append items to it, or otherwise mutate it. You build it, pass it to a > function which doesn't modify it in any fashion, then it gets garbage > collected. > > >> I suppose a set would be even more right, but (I'm >> pretty sure) isinstance() predates sets. > > Yes. > > [steve at sylar ~]$ python1.5 > Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat > 4.1.2-27)] on linux2 > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>> isinstance > >>>> set > Traceback (innermost last): > File "", line 1, in ? > NameError: set > > > >> I'm curious why a tuple was chosen. > > Tuples are smaller and faster to build than lists -- they're the most > lightweight sequence type in Python. You don't need all the extra > functionality of lists, so why go to the time and effort of building a > list? In fact, constant tuples can be and now are compiled as constants: >>> dis(compile('(1,2,3)','','eval')) 1 0 LOAD_CONST 3 ((1, 2, 3)) 3 RETURN_VALUE >>> dis(compile('[1,2,3]','','eval')) 1 0 LOAD_CONST 0 (1) 3 LOAD_CONST 1 (2) 6 LOAD_CONST 2 (3) 9 BUILD_LIST 3 12 RETURN_VALUE Internally, even a frozenset is more complicated than a tuple since it still needs a hash table, which is overkill for something that will be linearly scanned exactly once. tjr From qauzzix at gmail.com Fri Jul 24 16:52:47 2009 From: qauzzix at gmail.com (Qauzzix) Date: Fri, 24 Jul 2009 13:52:47 -0700 (PDT) Subject: How do I generate dia diagrams from python source code? Message-ID: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Greetings. Since I have been using dia to make my UML diagrams. I also found an util named dia2code that generates python code from dia diagram. Now that I have that option I really want to find a way to generate dia diagram from existing code and/or maintain my diagrams. I have been googling like crazy trying to find a way but with no luck. Anyone here know how to do this? Peace, - Jakob Sv. Bjarnason From tjreedy at udel.edu Fri Jul 24 16:52:53 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 16:52:53 -0400 Subject: len() should always return something In-Reply-To: <50697b2c0907241302m166d0c61y4912a29aaa4caaf1@mail.gmail.com> References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <50697b2c0907241302m166d0c61y4912a29aaa4caaf1@mail.gmail.com> Message-ID: Chris Rebert wrote: > I think the point made by Grant Edwards is instructive. len(x) = 1 > typically implies list(x)[0] and similar should be valid. At least, one should be able to iterate with x and get len(x) items. See below. >> And there's nothing ambiguous about len(42). > > Really? What is its value then? I think arguments of varying quality > can be made for: > 1 - as the OP and those from array programming languages would suggest > 2 - the number of decimal digits in 42, if one was feeling Perlish > 6 - the minimum number of bits necessary to represent 42 in binary > 32 (or 64, depending on your CPU) - the number of bits necessary to > represent an int (obviously breaks down a bit with arbitrary-magnitude > ints) > undefined - i.e. it causes an error, the current behavior; asking for > the length of a non-collection is "nonsensical" or "absurd" In set theory, one can define counts recursively as follows 0 = {} (set, not dict) n+1 = n | {n} so len(n) = n, and in particular, len(42) = 42. On the same basis, it has been proposed (and rejected) that iter(n) == range(n), do that we could write 'for i in 10' for instance. Guido thought this too esoteric. Terry Jan Reedy From tjreedy at udel.edu Fri Jul 24 16:57:13 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 16:57:13 -0400 Subject: non-owning references? In-Reply-To: <0279f5b9$0$5185$c3e8da3@news.astraweb.com> References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> <0279f5b9$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 24 Jul 2009 15:55:45 +0200, Hrvoje Niksic wrote: > >> The term "variable" is used in the Python >> language reference and elsewhere, and is quite compatible with how other >> popular languages (Java, PHP, Lisp, ...) use it. Please stop >> complaining about valid terminology; it is not helpful. > > No, the use of the single term "variable" to describe two distinct > program models is not helpful. Whether other languages muddy the water > between memory-location based variables and name-binding is irrelevant to > whether we should do so. And quite a few of us are disappointed that the > Python language reference should confuse the issue by using misleading > terminology. > > Unfortunately, the use of "variable" is so ingrained, and so simple > compared to name binding terminology, that I fear we'll never eradicate > it. I know sometimes I use it myself, but always with a little shiver of > shame that I'm misusing terminology. Some years ago, I read a claim that 'variable' has about 15 shades of meaning in math (some referring to non-variable constants), making it probably the most overloaded term in math. I am trying to mostly avoid it in the book I am writing. tjr From bryanvick at gmail.com Fri Jul 24 17:11:02 2009 From: bryanvick at gmail.com (Bryan) Date: Fri, 24 Jul 2009 14:11:02 -0700 (PDT) Subject: Script runs manually, but cron fails Message-ID: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> I have a backup script that runs fine when I run it manually from the command line. When I run it with cron, the script stops running at random points in the source code. The script calls rsync with the subprocess module, which in turn uses ssh to backup files from a box on my lan. It also uses the atexit module to always run a certain piece of cleanup code when the script exits. However, this piece of code is never called when I use cron to run the script, but a ps -A command also does not show the python process so I know the script is dead. The log files generated by the script all show rsync as completing, but then the logging gets cutoff at random places when the script dies after that point. I am not getting any email from cron complaining of an error either. The script runs fine in my bash shell, what could cron be doing to interfere? From duncan.booth at invalid.invalid Fri Jul 24 17:14:06 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 24 Jul 2009 21:14:06 GMT Subject: list vs. tuple [Re: len() should always return something] References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: Roy Smith wrote: > In article , > Terry Reedy wrote: > >> Better: if isinstance(x, (int, float, complex)): > > I never noticed this before, but it seems odd that the second argument > to isinstance() should be a tuple. Using the normal arguments made > about tuples vs. lists, it seems like a list would be the right data > structure here. I suppose a set would be even more right, but (I'm > pretty sure) isinstance() predates sets. > > I'm curious why a tuple was chosen. There's a very good reason[*]. The second argument to isinstance() is a classinfo where a classinfo is a type or a tuple of classinfo. That means you can have an arbitrarily complex set of nested tuples e.g. (int, (float, complex)), but the immutable nature of tuples means the implementation of isinstance can walk the tuple tree without ever having to worry about infinite loops. If classinfo could be a list of types then someone somewhere would feed it a recursive list and then complain that the interpreter crashed. Exception specifications are tuples for the same reason. [*] For some definition of 'very good'. Would Python be significantly impaired if you couldn't combine classinfos into a tree structure? Probably not. From anonymous.c.lisper at gmail.com Fri Jul 24 17:58:57 2009 From: anonymous.c.lisper at gmail.com (ACL) Date: Fri, 24 Jul 2009 14:58:57 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <12ce2d31-5028-4b60-a5b1-5d5bf1fc06d7@f20g2000prn.googlegroups.com> On Jul 24, 2:06?pm, Raffael Cavallaro wrote: > On 2009-07-23 23:51:02 -0400, Carl Banks said: > > > On Jul 23, 5:52?pm, Rui Maciel wrote: > >> fft1976 wrote: > >>> How do you explain that something as inferior as Python beat Lisp in > >>> the market place despite starting 40 years later. > > >> Probably due to similar reasons that lead php to become remotely relevant > > . > > > Well, the only reason PHP became relevant because it was an > > easy > > ^^^^^^^^ > ^^^^^^^^ (emphasis added) > > > to > > deploy solution in a single application domain, the web, that happened > > to explode. > > i.e., Python "beat" lisp because it is ~70% of lisp in a form that is > much more palatable to the average programmer, just as php became > popular because it is powerful enough to do websites and, most > importantly, apprehensible to mediocre programmers and even some > non-programmers. > > -- > Raffael Cavallaro I actually think that the thing holding lisp back is 'bus factor'. Lets assume I have a java project and a lisp project: Java project: I have maybe 10 or 12 people on my team working on various subsystems of my project. There are probably one or two 'technical leader' types in the group, and a bunch of others who are sort of 'serfs', banging out java classes. Lets say one of my important guys gets totally splattered by a bus... I've still got another one left! I can rely on the other guy to keep things afloat while I train up a new leader type. Lisp project: I don't need as many people. I have 3 or 4 people, and one person is my technical leader and lisp guru. Guru is probably in charge of more difficult macros and (because of that), also in charge of the overall design (macros are design patterns embodied in code). Lets say he gets totally annihilated by the bus. What do I do now? I had all my eggs in one basket and my project is now stalled. I think that (for example) when people are saying that lisp macros make projects difficult to understand, or that lisp hackers are much harder to find than other programmers, what they are really showing is that because of the nature of the language, lisp projects get organized in a different way. This different way of organization is especially hard on corporate projects, because managers are expected to plan for when some important guy drops dead. The problem with lisp is that if you hire too many extra hackers, you end up with a bunch of people sitting around twiddling their thumbs. This may also explain why it gets touted as an academic language. In academia if a professor who is leading some important research drops dead, who cares? Most likely no one had interest as to whether the university would make a profit on this particular research endeavor (except maybe the deceased, as he would like to get grant funding). So I guess then we can give some tips for making lisp more acceptable for 'paying gigs'. 1.) If you are a lisp hacker, it is your duty to be lazier and write less code. We need to make it so that these projects need sizable teams of people to complete. 2.) Write down everything that you do, everything you are going to do. Maybe draw some object diagrams of your important/fancy macros, make sure these are stored with the source. (You'll note that an iterative approach to programming isn't really conducive to making a lot of diagrams of stuff...) I don't know what this has to do with python or scheme or people dumping scheme for python. Honestly, I seriously doubt that any undergraduate programming course will give you enough actual programming experience to make you seriously proficient in the language (research projects not counted). The language used to teach the material is a cursory detail. From user at example.net Fri Jul 24 18:28:09 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 00:28:09 +0200 Subject: pack an integer into a string Message-ID: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> is there a pythonic and synthetic way (maybe some standard module) to "pack" an integer (maybe a *VERY* big one) into a string? like this: >>> number = 252509952 >>> hex(number) '0xf0cff00' >>> so i would like a string like '\xf0\xcf\xf0\x00' i wrote some code to do it, so ugly i am ashamed to post :-( i tried xdrlib, but does not quite do what i mean... bye From martin at v.loewis.de Fri Jul 24 18:28:24 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 25 Jul 2009 00:28:24 +0200 Subject: installing 2.6 on vista64 In-Reply-To: References: Message-ID: <4a6a3588$0$11346$9b622d9e@news.freenet.de> > I just downloaded and attempted to install python 2.6.2. The > installer proceeds to do its work then dies, leaving an entry in the > eventlog: > > Windows Installer installed the product. Product Name: Python 2.6.2. > Product Version: 2.6.2150. Product Language: 1033. Installation > success or error status: 1602. Are you using subst'ed drives by any chance? > Googling for this I wasn't able to narrow the results down to > something usable. Anyone know of issues and how to fix them installing > on vista 64 (yes, I have 8 gigs of ram) Run "msiexec /i /l*v python.log", and search the log file for 1602. This should give a clue. Regards, Martin From emile at fenx.com Fri Jul 24 18:41:59 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 24 Jul 2009 15:41:59 -0700 Subject: Script runs manually, but cron fails In-Reply-To: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> References: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> Message-ID: On 7/24/2009 2:11 PM Bryan said... > The script runs fine in my bash shell, what could cron be doing to > interfere? It's likely environmental -- make sure you're starting with the same path, etc. Sometimes I'll create a sell script by env'ing to a bash script file and append the actual command to execute, chmod a+x the script and put that in the cron file. Then strip out variables until you find the missing culprit. HTH, Emile From sbassi at clubdelarazon.org Fri Jul 24 18:43:58 2009 From: sbassi at clubdelarazon.org (Sebastian Bassi) Date: Fri, 24 Jul 2009 19:43:58 -0300 Subject: pack an integer into a string In-Reply-To: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <9e2f512b0907241543o5b4606agf80a87af74b4116@mail.gmail.com> On Fri, Jul 24, 2009 at 7:28 PM, superpollo wrote: > is there a pythonic and synthetic way (maybe some standard module) to "pack" > an integer (maybe a *VERY* big one) into a string? like this: What do you mean to pack? Maybe Pickle is what you want. import cPickle variable = 124348654333577698 cPickle.dump(variable,open('filename', 'w')) To load it: import cPickle vaariable = cPickle.load(open('filename')) It is not "into a string", but instead of a filename, use a string with stringio From 9372966370 at sms.mycricket.com Fri Jul 24 18:51:49 2009 From: 9372966370 at sms.mycricket.com (9372966370 at sms.mycricket.com) Date: 24 Jul 09 16:51:49 -0600 Subject: No subject Message-ID: <200907242251.n6OLv34O026362@mta2-den.mycricket.com> (Re: urllib2.URLError: error using twill with python) how can i change url/http://... --------------------------------------------- Sent by a Cricket mobile device --------------------------------------------- From 9372966370 at sms.mycricket.com Fri Jul 24 18:53:15 2009 From: 9372966370 at sms.mycricket.com (9372966370 at sms.mycricket.com) Date: 24 Jul 09 16:53:15 -0600 Subject: No subject Message-ID: <200907242253.n6OLv34f026362@mta2-den.mycricket.com> (Re: urllib2.URLError: error using twill with python) --------------------------------------------- Sent by a Cricket mobile device --------------------------------------------- From user at example.net Fri Jul 24 18:58:07 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 00:58:07 +0200 Subject: pack an integer into a string In-Reply-To: References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <4a6a3c82$0$6157$4fafbaef@reader5.news.tin.it> Sebastian Bassi wrote: > On Fri, Jul 24, 2009 at 7:28 PM, superpollo wrote: > >>is there a pythonic and synthetic way (maybe some standard module) to "pack" >>an integer (maybe a *VERY* big one) into a string? like this: > > > What do you mean to pack? Maybe Pickle is what you want. > > import cPickle > variable = 124348654333577698 > cPickle.dump(variable,open('filename', 'w')) > > To load it: > > import cPickle > vaariable = cPickle.load(open('filename')) > > It is not "into a string", but instead of a filename, use a string with stringio >>> number 252509952 >>> f = cStringIO.StringIO() >>> cPickle.dump(number , f) >>> f.getvalue() 'I252509952\n.' >>> as you can see, the pickled representation is not what i wanted... thanks anyway bye From piet at cs.uu.nl Fri Jul 24 19:03:25 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 01:03:25 +0200 Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: >>>>> superpollo (s) wrote: >s> is there a pythonic and synthetic way (maybe some standard module) to >s> "pack" an integer (maybe a *VERY* big one) into a string? like this: >>>>> number = 252509952 >>>>> hex(number) >s> '0xf0cff00' >>>>> >s> so i would like a string like '\xf0\xcf\xf0\x00' You have the string wrong. But the correct one you get with: In [67]: import struct In [68]: number = 252509952 In [69]: struct.pack('>I', number) Out[69]: '\x0f\x0c\xff\x00' (Please note that this is big endian) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From user at example.net Fri Jul 24 19:11:08 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 01:11:08 +0200 Subject: pack an integer into a string In-Reply-To: <4a6a3c82$0$6157$4fafbaef@reader5.news.tin.it> References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> <4a6a3c82$0$6157$4fafbaef@reader5.news.tin.it> Message-ID: <4a6a3f8f$0$6157$4fafbaef@reader5.news.tin.it> superpollo wrote: > Sebastian Bassi wrote: > >> On Fri, Jul 24, 2009 at 7:28 PM, superpollo wrote: >> >>> is there a pythonic and synthetic way (maybe some standard module) to >>> "pack" >>> an integer (maybe a *VERY* big one) into a string? like this: >> >> >> >> What do you mean to pack? Maybe Pickle is what you want. >> >> import cPickle >> variable = 124348654333577698 >> cPickle.dump(variable,open('filename', 'w')) >> >> To load it: >> >> import cPickle >> vaariable = cPickle.load(open('filename')) >> >> It is not "into a string", but instead of a filename, use a string >> with stringio > > > > >>> number > 252509952 > >>> f = cStringIO.StringIO() > >>> cPickle.dump(number , f) > >>> f.getvalue() > 'I252509952\n.' > >>> > > as you can see, the pickled representation is not what i wanted... > OTOH, using a higher protocol: >>> cPickle.dump(number , f , 1) >>> f.getvalue() 'J\x00\xff\x0c\x0f.' which is very close to '\xf0\xcf\xf0\x00', that i meant originally... so i i change my spec as; '\x0f\x0c\xff\x00' (i.e. left- intead of right- zeropadding) i am almost there... bye From bartc at freeuk.com Fri Jul 24 19:15:32 2009 From: bartc at freeuk.com (bartc) Date: Fri, 24 Jul 2009 23:15:32 GMT Subject: len() should always return something In-Reply-To: References: Message-ID: "Dr. Phillip M. Feldman" wrote in message news:mailman.3644.1248417347.8015.python-list at python.org... > > Some aspects of the Python design are remarkably clever, while others > leave > me perplexed. Here's an example of the latter: Why does len() give an > error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. So you want len() to treat 123 as though it could potentially be a list containing a single element? In that case there could be an ambiguity here: print len([10,20,30]) Should it print 3 (the elements in [10,20,30]), or 1 (treating [10,20,30] as a potential list containing the single element [10,20,30])? -- bartc From user at example.net Fri Jul 24 19:16:07 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 01:16:07 +0200 Subject: pack an integer into a string In-Reply-To: References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <4a6a40ba$0$6157$4fafbaef@reader5.news.tin.it> Piet van Oostrum wrote: ... > You have the string wrong. oops yea. > But the correct one you get with: > > In [67]: import struct > > In [68]: number = 252509952 > > In [69]: struct.pack('>I', number) > Out[69]: '\x0f\x0c\xff\x00' > > (Please note that this is big endian) thanks a lot, but it does not work for large integers: >>> number 283691163101781L >>> struct.pack('>I', number) Traceback (most recent call last): File "", line 1, in ? OverflowError: long int too large to convert >>> bye From scriptlearner at gmail.com Fri Jul 24 19:20:00 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Fri, 24 Jul 2009 16:20:00 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? Message-ID: My parent thread keeps a counter for the number of free child workers (say 100) and initializes some child threads and call child.start(). Once the number of free child workers reach 0, the parent thread will wait until some at least one child thread finishes and then it will initialize another child thread. My question is, how can a child thread notify the parent that it's done so that the parent can call join() on it? I am not sure how a child thread can send a signal to its parent while it may not even know anything about it's parent. Can you guys please provide some suggestions? Some code samples will be nice. Thanks. From greg at cosc.canterbury.ac.nz Fri Jul 24 19:24:13 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sat, 25 Jul 2009 11:24:13 +1200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> Message-ID: <7cuu3eF29jmmuU2@mid.individual.net> Bearophile wrote: > Was this link, shown by William, not enough? > http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 Yes, sorry, I posted too soon. -- Greg From greg at cosc.canterbury.ac.nz Fri Jul 24 19:26:44 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sat, 25 Jul 2009 11:26:44 +1200 Subject: clean way prepend an element to a numpy array In-Reply-To: References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <7cnr5tF28183hU1@mid.individual.net> Message-ID: <7cuu86F2945qmU1@mid.individual.net> Robert Kern wrote: >> a[:0] = array('i', [0]) > > Not when 'a' is a numpy array rather than an array.array. That's true, but I got the impression that the OP was talking about array.array, not numpy.array. It's very confusing having two widely-used types both called 'array'. :-( -- Greg From robert.kern at gmail.com Fri Jul 24 19:35:01 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 24 Jul 2009 18:35:01 -0500 Subject: clean way prepend an element to a numpy array In-Reply-To: <7cuu86F2945qmU1@mid.individual.net> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <7cnr5tF28183hU1@mid.individual.net> <7cuu86F2945qmU1@mid.individual.net> Message-ID: On 2009-07-24 18:26, greg wrote: > Robert Kern wrote: > >>> a[:0] = array('i', [0]) >> >> Not when 'a' is a numpy array rather than an array.array. > > That's true, but I got the impression that the OP was > talking about array.array, not numpy.array. Did you read the Subject: line? :-) -- 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 python at mrabarnett.plus.com Fri Jul 24 19:44:36 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 25 Jul 2009 00:44:36 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: References: Message-ID: <4A6A4764.1080206@mrabarnett.plus.com> scriptlearner at gmail.com wrote: > My parent thread keeps a counter for the number of free child workers > (say 100) and initializes some child threads and call child.start(). > Once the number of free child workers reach 0, the parent thread will > wait until some at least one child thread finishes and then it will > initialize another child thread. > My question is, how can a child thread notify the parent that it's > done so that the parent can call join() on it? I am not sure how a > child thread can send a signal to its parent while it may not even > know anything about it's parent. Can you guys please provide some > suggestions? Some code samples will be nice. Thanks. I would create a queue ("Queue" module) and pass it to each child; the child would put something (eg a reference to itself) in the queue when it had finished. From lists at cheimes.de Fri Jul 24 19:53:19 2009 From: lists at cheimes.de (Christian Heimes) Date: Sat, 25 Jul 2009 01:53:19 +0200 Subject: how can a child thread notify a parent thread its status? In-Reply-To: References: Message-ID: scriptlearner at gmail.com wrote: > My parent thread keeps a counter for the number of free child workers > (say 100) and initializes some child threads and call child.start(). > Once the number of free child workers reach 0, the parent thread will > wait until some at least one child thread finishes and then it will > initialize another child thread. > My question is, how can a child thread notify the parent that it's > done so that the parent can call join() on it? I am not sure how a > child thread can send a signal to its parent while it may not even > know anything about it's parent. Can you guys please provide some > suggestions? Some code samples will be nice. Thanks. You are using the wrong approach here. There is a much easier solution to your problem although it's not obvious in the first place. In this approach the worker threads don't have to notify the parent thread that they are ready. The main thread creates a queue and a bunch of worker threads that are pulling tasks from the queue. As long as the queue is empty all worker threads block and do nothing. When a task is put into the queue a random worker thread acquires the task, does it job and sleeps as soon as its ready. That way you can reuse a thread over and over again without creating a new worker threads. When you need to stop the threads you put a kill object into the queue that tells the thread to shut down instead of blocking on the queue. Christian From python at mrabarnett.plus.com Fri Jul 24 20:05:22 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 25 Jul 2009 01:05:22 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: References: Message-ID: <4A6A4C42.40004@mrabarnett.plus.com> Christian Heimes wrote: > scriptlearner at gmail.com wrote: >> My parent thread keeps a counter for the number of free child workers >> (say 100) and initializes some child threads and call child.start(). >> Once the number of free child workers reach 0, the parent thread will >> wait until some at least one child thread finishes and then it will >> initialize another child thread. >> My question is, how can a child thread notify the parent that it's >> done so that the parent can call join() on it? I am not sure how a >> child thread can send a signal to its parent while it may not even >> know anything about it's parent. Can you guys please provide some >> suggestions? Some code samples will be nice. Thanks. > > You are using the wrong approach here. There is a much easier solution > to your problem although it's not obvious in the first place. In this > approach the worker threads don't have to notify the parent thread that > they are ready. > > The main thread creates a queue and a bunch of worker threads that are > pulling tasks from the queue. As long as the queue is empty all worker > threads block and do nothing. When a task is put into the queue a random > worker thread acquires the task, does it job and sleeps as soon as its > ready. That way you can reuse a thread over and over again without > creating a new worker threads. > > When you need to stop the threads you put a kill object into the queue > that tells the thread to shut down instead of blocking on the queue. > The extra trick is that when a thread gets the kill object it puts it back before terminating so that another thread will also get it. From googler.1.webmaster at spamgourmet.com Fri Jul 24 20:12:13 2009 From: googler.1.webmaster at spamgourmet.com (moerchendiser2k3) Date: Fri, 24 Jul 2009 17:12:13 -0700 (PDT) Subject: Freetype2 in PIL Message-ID: <95f321b4-d7e8-4515-8743-dad86298024d@a26g2000yqn.googlegroups.com> Hi, I have a problem with Freetype2 in the Python Image Library. I compiled it as 64bit and I got a file named freetype239.lib. So I added this file to a folder called lib. I added the parent folder of the lib folder to the setup.py settings (like all the others: libjpeg, zlib). LIbjpeg and Zlib works fine on Windows 64 bit and the installer can find them. But I just get a "Freetype2 not found". Even renaming it to freetype.lib and freetype2.lib doesnt work. Any suggestions why PIL cant find the library? Bye and thanks in advance :) From googler.1.webmaster at spamgourmet.com Fri Jul 24 20:12:13 2009 From: googler.1.webmaster at spamgourmet.com (moerchendiser2k3) Date: Fri, 24 Jul 2009 17:12:13 -0700 (PDT) Subject: Freetype2 in PIL Message-ID: Hi, I have a problem with Freetype2 in the Python Image Library. I compiled it as 64bit and I got a file named freetype239.lib. So I added this file to a folder called lib. I added the parent folder of the lib folder to the setup.py settings (like all the others: libjpeg, zlib). LIbjpeg and Zlib works fine on Windows 64 bit and the installer can find them. But I just get a "Freetype2 not found". Even renaming it to freetype.lib and freetype2.lib doesnt work. Any suggestions why PIL cant find the library? Bye and thanks in advance :) From gallium.arsenide at gmail.com Fri Jul 24 20:14:59 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 24 Jul 2009 17:14:59 -0700 (PDT) Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> <4a6a40ba$0$6157$4fafbaef@reader5.news.tin.it> Message-ID: <669833bd-7d70-4cec-a9a3-9de50d77d8e7@c14g2000yqm.googlegroups.com> On Jul 24, 7:16?pm, superpollo wrote: > thanks a lot, but [struct] does not work for large integers: Since the struct module is designed specifically for C-style structs, it's definitely not going to handle arbitrary-length integers on its own. You could chop up your Python (long) integer into C-sized pieces and then pack the pieces; but if you need to do that, the advantage of using struct at all may be minimal or even completely gone. > OTOH, using a higher [pickle] protocol: If you are sure you are going to keep using the same version of Python, then this could work. I would personally not depend on this to keep working in future versions. I'm not aware of any existing package that does what you want. Maybe someone else does. How I personally would tackle your problem is to write the whole thing from scratch (as you've done). John From deets at nospam.web.de Fri Jul 24 20:33:11 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 25 Jul 2009 02:33:11 +0200 Subject: cgi.fieldstorage() In-Reply-To: References: <7cu9hhF29gt65U1@mid.uni-berlin.de> Message-ID: <7cv268F29q0j5U1@mid.uni-berlin.de> gert schrieb: > On Jul 24, 7:32 pm, "Diez B. Roggisch" wrote: >> gert schrieb: >> >>> this is a non standard way to store multi part post data on disk >>> def application(environ, response): >>> with open('/usr/httpd/var/wsgiTemp','w') as f: >>> while True: >>> chunk = environ['wsgi.input'].read(8192).decode('latin1') >>> if not chunk: break >>> f.write(chunk) >>> response('200 OK',[]) >>> return ['complete'] >>> my question is how do i handle the file, so i can shuffle it into a db >>> using small chunks of memorie ? >> I don't think that's possible with the current DB-API. There is no >> stream-based BLOB-interface (as e.g. JDBC offers). >> >> So the answer certainly depends on your used RDBMS. For oracle, you >> would be lucky: >> >> http://cx-oracle.sourceforge.net/html/lob.html >> >> Other adapters I don't know about. > > sqlite :) ok let say for now it would be impossible on a db level, but > before i reach the impossible, i still need to parse the file to > prepare the chunks. How do i do that ? How do i get the chunks without > loading the hole file into memorie ? It's memory - memorie might be some nice dutch girl you know :) Apart from that, your code above does exactly that - reads the data chunkwise. If the WSGI-implementation works proper, this will be the socket's input stream, so there is no memory overhead involved. Now of course if you want to have a multi-pass processing of the file without putting it into memory, then you need to save it to the harddisk befor. But honestly - we are talking about a web-application here. My DSL-connection has 1 MBit upstream, the average server has at least 2GB of memory available - so we are talking 20000 seconds uploading time to fill that memory. Which is about 5 hours. And you are decoding the data to a certain decoding, so we are not talking about binary data here - are you really sure memory is an issue? Diez Diez From casevh at gmail.com Fri Jul 24 20:35:59 2009 From: casevh at gmail.com (casevh) Date: Fri, 24 Jul 2009 17:35:59 -0700 (PDT) Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <7728c70f-4d1d-4e5c-ae8b-175491fc1d31@f18g2000prf.googlegroups.com> On Jul 24, 3:28?pm, superpollo wrote: > is there a pythonic and synthetic way (maybe some standard module) to > "pack" an integer (maybe a *VERY* big one) into a string? like this: > > ? >>> number = 252509952 > ? >>> hex(number) > ? '0xf0cff00' > ? >>> > > so i would like a string like '\xf0\xcf\xf0\x00' > > i wrote some code to do it, so ugly i am ashamed to post :-( > > i tried xdrlib, but does not quite do what i mean... > > bye This works, I think. >>> def hex_string(n): ... a = hex(n) ... ch_list = [] ... if a.startswith('0x'): a = a[2:] ... if a.endswith('L'): a = a[:-1] ... for i in range(0, len(a)): ... ch_list.append(chr(int(a[i:i+2],16))) ... return ''.join(ch_list) ... >>> hex_string(252509952) '\xf0\x0c\xcf\xff\xf0\x00\x00' >>> hex_string(283691163101781L) '\x10\x02 \x03?\xff\xf0\x00\n\xaa\xa5U\x05' >>> From deets at nospam.web.de Fri Jul 24 20:38:28 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 25 Jul 2009 02:38:28 +0200 Subject: Computation/E-mail Expert opinions? In-Reply-To: <9e520bf6-bc15-4942-a668-d18c2dbaf80f@o32g2000yqm.googlegroups.com> References: <9e520bf6-bc15-4942-a668-d18c2dbaf80f@o32g2000yqm.googlegroups.com> Message-ID: <7cv2g5F29q0j5U2@mid.uni-berlin.de> scribio_vide schrieb: > MAIL From: > From: "tommy02" > To: "MeAmI" > Subject: I AM LOOKING FORWARD TO YOUR ANSWER ON THIS ISSUE > Date: Fri, 24 Jul 09 11:52:27 Pacific Daylight Time > MIME-Version: 1.0 > Content-Type: multipart/mixed;boundary= "---- > =_NextPart_000_008C_8F228C8E.684AAFEA" > X-Priority: 3 > X-MSMail-Priority: Normal > X-Mailer: Microsoft Outlook Express 6.00.2462.0000 > X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2462.0000 > > ------=_NextPart_000_008C_8F228C8E.684AAFEA > Content-Type: text/plain > Content-Transfer-Encoding: base64 > > ZXRtYWlsLmNvbQ0KICAgIA== > ------=_NextPart_000_008C_8F228C8E.684AAFEA-- > > Any ides why I this came to be in my inbox? It is either an extremely dangerous virus - or an utterly harmless, but annoying base64-encoded spam-mail, given the subject. None of the possibilities justifies a post to *three* newsgroups. Diez From scriptlearner at gmail.com Fri Jul 24 20:39:04 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Fri, 24 Jul 2009 17:39:04 -0700 (PDT) Subject: time in milliseconds by calling time.time() Message-ID: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> I am trying to measure some system response time by using the time.time () or time.clock() in my script. However, the numbers I get are in 10s of milliseconds. For example, 1248481670.34 #from time.time() 0.08 #from time.clock() That won't work for me, since the response time may be only a few milliseconds. My environment is Solaris 10 with Python 2.4.4 (#7, Feb 9 2007, 22:10:21). SunOS 5.10 Generic_137112-07 i86pc i386 i86pc The tricky thing is, if I run the python interpreter and import the time module, I can get a time floating number in better precision by calling time.time(). Do you guys have any suggestion on debugging this problem? Or, is there any other module I can try? Thanks. $ python Python 2.4.4 (#7, Feb 9 2007, 22:10:21) [GCC 3.4.6] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> time.time() 1248481930.8023829 <--I like this! >>> time.clock() 0.0 >>> From aahz at pythoncraft.com Fri Jul 24 21:09:08 2009 From: aahz at pythoncraft.com (Aahz) Date: 24 Jul 2009 18:09:08 -0700 Subject: Python ctypes on win64 References: Message-ID: In article , ulf wrote: > >Does anybody know if python includes a win64 version of ctypes? > >According to the documentation it should be included - at least >there's no special remarks for win64, and I haven't found any recent >notes saying that it shouldn't work on win64. Yet it looks like both >the installer from Python.org and from ActiveState.com have disabled >it. Nobody seems to have followed up to this, you may have better luck on the capi-sig mailing list. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From rylesny at gmail.com Fri Jul 24 21:11:29 2009 From: rylesny at gmail.com (ryles) Date: Fri, 24 Jul 2009 18:11:29 -0700 (PDT) Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? Message-ID: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> According to http://www.python.org/doc/essays/packages.html: "The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it." However, I've noticed that once a module is imported using the `from pkg import mod' syntax, if its name is deleted from the package namespace then subsequent imports with `from' will fail. Here is an example of this type of scenario: $ ls -l pkg total 8.0K -rw-rw-r-- 1 ? general 0 Jul 24 20:21 _impl.py -rw-rw-r-- 1 ? general 147 Jul 24 20:28 __init__.py -rw-rw-r-- 1 ? general 0 Jul 24 20:33 public2.py -rw-rw-r-- 1 ? general 208 Jul 24 20:32 public.py $ cat pkg/__init__.py from pkg import _impl # Add functions which refer to objects in _impl # ... # Don't "currently" require this in the namespace anymore. del _impl $ cat pkg/public.py # Implement something with the aid of _impl. from pkg import public2 # OK, fine. from pkg import _impl # The module exists, but this will fail. # Could do import pkg._impl (or a relative import) $ python Python 2.6.2 (r262:71600, Jul 16 2009, 14:04:28) [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from pkg import public Traceback (most recent call last): File "", line 1, in File "pkg/public.py", line 4, in from pkg import _impl # The module exists, but this will fail. ImportError: cannot import name _impl >>> import sys >>> sys.modules["pkg._impl"] >>> from pkg import _impl Traceback (most recent call last): File "", line 1, in ImportError: cannot import name _impl >>> import pkg._impl # Giving up! >>> I had previously noted that once a module is imported from a package it is automatically added to the package namespace: >>> import pkg >>> dir(pkg) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__'] >>> from pkg import public2 >>> dir(pkg) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'public2'] >>> However, I didn't expect python to give up on importing those names again once removed. Can anyone provide more details on what's occurring here? Is this behavior intentional (a feature), or, is it in your opinion, a defect? From roy at panix.com Fri Jul 24 21:27:10 2009 From: roy at panix.com (Roy Smith) Date: Fri, 24 Jul 2009 21:27:10 -0400 Subject: time in milliseconds by calling time.time() References: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> Message-ID: In article <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50 at m7g2000prd.googlegroups.com>, "scriptlearner at gmail.com" wrote: > I am trying to measure some system response time by using the time.time > () or time.clock() in my script. However, the numbers I get are in > 10s of milliseconds. > [...] > The tricky thing is, if I run the python interpreter and import the > time module, I can get a time floating number in better precision by > calling time.time(). > [...] > >>> time.time() > 1248481930.8023829 <--I like this! time.time() is returning a float in either case. The difference you are seeing is purely related to how you are printing it; executing a "print" statement as opposed to the interactive interpreter printing a value. Notice: Roy-Smiths-MacBook-Pro:play$ python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> print time.time() 1248484949.75 >>> time.time() 1248484957.151274 and further notice: >>> x = time.time() >>> str(x) '1248485028.58' >>> repr(x) '1248485028.5814769' Keep in mind that while a float may have a large apparent precision, there's no promise that the actual value returned by the OS has that much precision. You should be fine if all you're looking for is ms, but I wouldn't count on much more than that. From http Fri Jul 24 21:30:46 2009 From: http (Paul Rubin) Date: 24 Jul 2009 18:30:46 -0700 Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <7xvdlh8s6x.fsf@ruckus.brouhaha.com> superpollo writes: > >>> number = 252509952 > >>> hex(number) > '0xf0cff00' > >>> > > so i would like a string like '\xf0\xcf\xf0\x00' def encode(number): h = '%x' % number if len(h) % 2 == 1: h = '0' + h return h.decode('hex') From pfeldman at verizon.net Fri Jul 24 22:30:12 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 24 Jul 2009 19:30:12 -0700 (PDT) Subject: list vs. tuple [Re: len() should always return something] In-Reply-To: References: <24639361.post@talk.nabble.com> <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <24654347.post@talk.nabble.com> isinstance(x, (int, float, complex)) is certainly very compact, and does what I want. Thanks! -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654347.html Sent from the Python - python-list mailing list archive at Nabble.com. From pfeldman at verizon.net Fri Jul 24 22:33:30 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 24 Jul 2009 19:33:30 -0700 (PDT) Subject: len() should always return something In-Reply-To: <0279f6df$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: <24654358.post@talk.nabble.com> "As far as I know, there is no programming language which treats scalars like ints as if they were vectors of length 1" Actually, Matlab does: >> length(5) ans = 1 >> -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654358.html Sent from the Python - python-list mailing list archive at Nabble.com. From rurpy at yahoo.com Fri Jul 24 22:41:50 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 24 Jul 2009 19:41:50 -0700 (PDT) Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: Scott David Daniels wrote: > rurpy at yahoo.com wrote: >> Nick Dumas wrote: >>> On 7/23/2009 9:23 AM, Mark Lawrence wrote: >>>> scriptlearner at gmail.com wrote: >>>>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >>>>> will like to take out the values (valuea, valueb, and valuec). How do >>>>> I do that in Python? The group method will only return the matched >>>>> part. Thanks. >>>>> >>>>> p = re.compile('#a=*;b=*;c=*;') >>>>> m = p.match(line) >>>>> if m: >>>>> print m.group(), >>>> IMHO a regex for this is overkill, a combination of string methods such >>>> as split and find should suffice. >> >> You're saying that something like the following >> is better than the simple regex used by the OP? >> [untested] >> values = [] >> parts = line.split(';') >> if len(parts) != 4: raise SomeError() >> for p, expected in zip (parts[-1], ('#a','b','c')): >> name, x, value = p.partition ('=') >> if name != expected or x != '=': >> raise SomeError() >> values.append (value) >> print values[0], values[1], values[2] > > I call straw man: [tested] > line = "#a=valuea;b=valueb;c=valuec;" > d = dict(single.split('=', 1) > for single in line.split(';') if single) > d['#a'], d['b'], d['c'] > If you want checking code, add: > if len(d) != 3: > raise ValueError('Too many keys: %s in %r)' % ( > sorted(d), line)) OK, that seems like a good solution. It certainly wasn't an obvious solution to me. I still have no problem maintaining that [tested] line = "#a=valuea;b=valueb;c=valuec;" m = re.match ('#a=(.*);b=(.*);c=(.*);', line) m.groups((1,2,3)) (If you want checking code, nothing else required.) is still simpler and clearer (with the obvious caveat that one is familiar with regexes.) >> Blech, not in my book. The regex checks the >> format of the string, extracts the values, and >> does so very clearly. Further, it is easily >> adapted to other similar formats, or evolutionary >> changes in format. It is also (once one is >> familiar with regexes -- a useful skill outside >> of Python too) easier to get right (at least in >> a simple case like this.) > The posted regex doesn't work; this might be homework, so > I'll not fix the two problems. The fact that you did not > see the failure weakens your claim of "does so very clearly." Fact? Maybe you should have read the whole thread before spewing claims that I did not see the regex problem. The fact that you did not bother to weakens any claims you make in this thread. (Of course this line of argumentation is stupid anyway -- even had I not noticed the problem, it would say nothing about the general case. My advice to you is not to try to extrapolate when the sample size is one.) From pfeldman at verizon.net Fri Jul 24 22:50:54 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 24 Jul 2009 19:50:54 -0700 (PDT) Subject: len() should always return something In-Reply-To: <7ct4b8F29mednU1@mid.uni-berlin.de> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: <24654439.post@talk.nabble.com> Here's a simple-minded example: def dumbfunc(xs): for x in xs: print x This function works fine if xs is a list of floats, but not if it is single float. It can be made to work as follows: def dumbfunc(xs): if isinstance(xs,(int,float,complex)): xs= [xs] for x in xs: print x Having to put such extra logic into practically every function is one of the annoying things about Python. Phillip Diez B. Roggisch-2 wrote: > > Dr. Phillip M. Feldman schrieb: >> Some aspects of the Python design are remarkably clever, while others >> leave >> me perplexed. Here's an example of the latter: Why does len() give an >> error >> when applied to an int or float? len() should always return something; in >> particular, when applied to a scalar, it should return a value of 1. Of >> course, I can define my own function like this: >> >> def mylen(x): >> if isinstance(x,int) or isinstance(x,float): return 1 >> return len(x) >> >> But, this shouldn't be necessary. > > Can you show some example of where that is actually making a piece of > code more elegant? > > Diez > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654439.html Sent from the Python - python-list mailing list archive at Nabble.com. From clp2 at rebertia.com Fri Jul 24 23:02:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 20:02:40 -0700 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: <50697b2c0907242002s1772783jed0a4a68177616cf@mail.gmail.com> On Fri, Jul 24, 2009 at 7:50 PM, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): > ? for x in xs: > ? ? ?print x > > This function works fine if xs is a list of floats, but not if it is single > float. ?It can be made to work as follows: > > def dumbfunc(xs): > ? if isinstance(xs,(int,float,complex)): xs= [xs] > ? for x in xs: > ? ? ?print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. You can easily fix that using decorators: (disclaimer: untested, except mentally) #bit of setup; pays off later def list_or_single(func): def wrapper(arg): try: iter(arg) except TypeError: arg = [arg] return func(arg) return wrapper #now just prefix functions with @list_or_single @list_or_single def dumbfunc(xs): for x in xs: print x Using the extended call syntax as I explained earlier is another option. Cheers, Chris -- http://blog.rebertia.com From davidj411 at gmail.com Sat Jul 25 00:21:48 2009 From: davidj411 at gmail.com (davidj411) Date: Fri, 24 Jul 2009 21:21:48 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? References: Message-ID: could i see an example of this maybe? From pavlovevidence at gmail.com Sat Jul 25 00:55:26 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 24 Jul 2009 21:55:26 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On Jul 24, 11:06?am, Raffael Cavallaro wrote: > On 2009-07-23 23:51:02 -0400, Carl Banks said: > > > On Jul 23, 5:52?pm, Rui Maciel wrote: > >> fft1976 wrote: > >>> How do you explain that something as inferior as Python beat Lisp in > >>> the market place despite starting 40 years later. > > >> Probably due to similar reasons that lead php to become remotely relevant > > . > > > Well, the only reason PHP became relevant because it was an > > easy > > ^^^^^^^^ > ^^^^^^^^ (emphasis added) > > > to > > deploy solution in a single application domain, the web, that happened > > to explode. > > i.e., Python "beat" lisp because it is ~70% of lisp in a form that is > much more palatable to the average programmer, just as php became > popular because it is powerful enough to do websites and, most > importantly, apprehensible to mediocre programmers and even some > non-programmers. That the two languages made something easier is the beginning and end of the similarities between them. PHP made hacking together a security-hole-ridden website easier. Python made actual programming easier. PHP became what it is because it rode on the coattails of a technology that grew in spite of it (kind of like everything Microsoft has shipped since Windows 3.1). Python became what it is because it earned the respect of programmers, who contributed to it and spread the word about it. That it is easy to use is only a part of why it earned that respect. Two languages could not have come to prominence by more different means. I can handle Python being called inferior to Lisp or a language for stupid people or a single-core-using dinosaur of a language or worse than Perl. But please don't put it on the same level as PHP. Their situations have almost nothing in common. Carl Banks From pavlovevidence at gmail.com Sat Jul 25 01:44:04 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 24 Jul 2009 22:44:04 -0700 (PDT) Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Jul 24, 6:57?am, Grant Edwards wrote: > On 2009-07-24, Dr. Phillip M. Feldman wrote: > > > > > Some aspects of the Python design are remarkably clever, while > > others leave me perplexed. Here's an example of the latter: > > Why does len() give an error when applied to an int or float? > > len() should always return something; in particular, when > > applied to a scalar, it should return a value of 1. > > If len(7) returned a value of 1, then wouldn't one expect 7[0] > to be valid? ?It isn't, so you'd then have to redefine all > types so that they are sequences that can be indexed. ?Sounds > like a big mess to me... You can call the big mess "Matlab". Carl Banks From andreengels at gmail.com Sat Jul 25 01:47:14 2009 From: andreengels at gmail.com (Andre Engels) Date: Sat, 25 Jul 2009 07:47:14 +0200 Subject: time in milliseconds by calling time.time() In-Reply-To: References: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> Message-ID: <6faf39c90907242247o4e6d50b6m5871faa7f08b447f@mail.gmail.com> On Sat, Jul 25, 2009 at 3:27 AM, Roy Smith wrote: > Keep in mind that while a float may have a large apparent precision, > there's no promise that the actual value returned by the OS has that much > precision. ?You should be fine if all you're looking for is ms, but I > wouldn't count on much more than that. Even stronger: I wouldn't count on _anything_ more than that. On my machine, time.time() changes value once per millisecond. I tested this by looking at a loop that recorded time.time() 100000 times. The total time in the loop was 61 ms; out of the 100000 numbers, 61 were higher than the previous one, with the highest difference being 1.00017 ms, the lowest 0.999928 ms. -- Andr? Engels, andreengels at gmail.com From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 02:18:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 06:18:23 GMT Subject: time in milliseconds by calling time.time() References: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> Message-ID: <027a94a5$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 07:47:14 +0200, Andre Engels wrote: > On Sat, Jul 25, 2009 at 3:27 AM, Roy Smith wrote: > >> Keep in mind that while a float may have a large apparent precision, >> there's no promise that the actual value returned by the OS has that >> much precision. ?You should be fine if all you're looking for is ms, >> but I wouldn't count on much more than that. > > Even stronger: I wouldn't count on _anything_ more than that. On my > machine, time.time() changes value once per millisecond. I tested this > by looking at a loop that recorded time.time() 100000 times. The total > time in the loop was 61 ms; out of the 100000 numbers, 61 were higher > than the previous one, with the highest difference being 1.00017 ms, the > lowest 0.999928 ms. I'm guessing you're running Windows, because for Windows time.time() has a low resolution and time.clock() is the higher resolution timer. I'm running Linux, which is the opposite: >>> from time import time, clock >>> def diffs(alist): ... deltas = [] ... for i in xrange(1, len(alist)): ... deltas.append( alist[i] - alist[i-1] ) ... return deltas ... >>> d = [time() for i in xrange(10000)] # grab raw times >>> dt = diffs(d) # calculate the difference between each call >>> max(dt), min(dt) (0.00060892105102539062, 1.9073486328125e-06) >>> >>> d = [clock() for i in xrange(10000)] # and again using clock() >>> dc = diffs(d) >>> max(dc), min(dc) (0.010000000000000009, 0.0) More important than the maximum and minimum time deltas is the resolution of ticks in each timer. Under Linux, clock() hardly ever gets updated: >>> len(dc) # how many time deltas? 9999 >>> len(filter(None, dc)) # how many non-zero deltas? 2 while time() gets updated frequently: >>> len(dt) 9999 >>> len(filter(None, dt)) 9999 See also the comments in the timeit module. -- Steven From gagsl-py2 at yahoo.com.ar Sat Jul 25 02:44:14 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 25 Jul 2009 03:44:14 -0300 Subject: trouble with minidom References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> <9c8c445f0907241056x11ec5316s2ea6310872cd342@mail.gmail.com> Message-ID: En Fri, 24 Jul 2009 14:56:29 -0300, Ronn Ross escribi?: > On Tue, Jul 21, 2009 at 7:32 PM, Gabriel Genellina > wrote: >> En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross >> escribi?: >> >> Hello I'm trying to read an xml file using minidome. The xml looks >> like: >>> >>> >>> myProj >>> /here/ >>> >>> > > I have used the loop below and it works great, but I need get both child > elements or 'project' per iteration. I want to build a dictionary that > resemble this: > my_dict = {'myProj':'/here/', 'anothername':'anotherpath'} > > I couldn't find how to do with in the element tree docs. Can you point > me in > the right direction? py> import xml.etree.ElementTree as ET py> xml = """ ... ... myProj ... /here/ ... ... ... anothername ... anotherpath ... ... """ py> doc = ET.fromstring(xml) py> result = {} py> for project in doc.findall('project'): ... name = project.find("name").text ... path = project.find("path").text ... result[name] = path ... py> result {'anothername': 'anotherpath', 'myProj': '/here/'} The Element interface is documented here: http://docs.python.org/library/xml.etree.elementtree.html#the-element-interface More info in the author's site: http://www.effbot.org/zone/element-index.htm (Perhaps you're using Python 2.5? That part of the documentation was omited in the 2.5 doc set by mistake. Read the 2.6 docs instead, there was no changes) -- Gabriel Genellina From xahlee at gmail.com Sat Jul 25 02:54:22 2009 From: xahlee at gmail.com (Xah Lee) Date: Fri, 24 Jul 2009 23:54:22 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: PHP is popular because it is geared for the server-side web scripting lang, and simpler and easy to use, than popular free alternatives at the time (such as Perl and Java's JSP). Python became popular primarily because its ease-to-read syntax. Btween the two, PHP is much easier to use, and much a pleasure to program in. Python is a pain in the ass. PHP is functional. The language is not elegant, lots of inconsistancies. However, it's a joy to use for any practical task in its web scripting field. PHP has one of the best documentation among open source computer languages, i'd say top 5. Python is a twist, with half-assed lambda, a culty community thinking "computer science R us", and it has this OOP obsession. The Guido guy do not understand functional programing, but has the pleasure to actitively badmouth it. References: ? Language, Purity, Cult, and Deception http://xahlee.org/UnixResource_dir/writ/lang_purity_cult_deception.html ? What Languages to Hate http://xahlee.org/UnixResource_dir/writ/language_to_hate.html ? Lambda in Python 3000 http://xahlee.org/perl-python/python_3000.html ? Python Documentation Problems http://xahlee.org/perl-python/python_doc_index.html ? Examples Of Quality Documentation In The Computing Industry http://xahlee.org/perl-python/quality_docs.html ? Xah's Perl and Python Tutorial http://xahlee.org/perl-python/index.html ? Xah's PHP Tutorial http://xahlee.org/php/index.html Xah ? http://xahlee.org/ ? From andreengels at gmail.com Sat Jul 25 02:55:40 2009 From: andreengels at gmail.com (Andre Engels) Date: Sat, 25 Jul 2009 08:55:40 +0200 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: <6faf39c90907242355m1de6d4e2s6a575a6c7469b67f@mail.gmail.com> On Sat, Jul 25, 2009 at 4:50 AM, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): > ? for x in xs: > ? ? ?print x > > This function works fine if xs is a list of floats, but not if it is single > float. ?It can be made to work as follows: > > def dumbfunc(xs): > ? if isinstance(xs,(int,float,complex)): xs= [xs] > ? for x in xs: > ? ? ?print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. I don't have this problem - when I have a list of one element, that's what I use - a list with one element. I think things would get much simpler if you do such a conversion right at the Matlab-Python interface, rather than changing all kind of things in Python so that its types are more matlab-like. Just like you shouldn't program Python by simply translating Java or C into Python idiom, you also shouldn't program Python by trying to mimic Matlab. Each language has its own strengths and weaknesses, and moving things over too much in a simplistic fashion will usually mean that you get the weaknesses of both and the strengths of neither. -- Andr? Engels, andreengels at gmail.com From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 03:03:02 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 07:03:02 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: <027a9f1a$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 19:50:54 -0700, Dr. Phillip M. Feldman wrote: > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is > single float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): > xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of > the annoying things about Python. But it's not "practically every function". It's hardly any function at all -- in my code, I don't think I've ever wanted this behavior. I would consider it an error for function(42) and function([42]) to behave the same way. One is a scalar, and the other is a vector -- they're different things, it's poor programming practice to treat them identically. (If Matlab does this, so much the worse for Matlab, in my opinion.) However, if you want this behaviour, it's easy enough to get it with a decorator: from functools import wraps def matlab(func): """Decorate func so that it behaves like Matlab, that is, it considers a single scalar argument to be the same as a vector of length one.""" @wraps(func) def inner(arg, **kwargs): # If arg is already a vector, don't touch it. try: # If this succeeds, it's a vector of some type. iter(arg) except TypeError: # It's a scalar. arg = [arg] # Now call the decorated function (the original). return func(arg, **kwargs) return inner With this decorator in hand, you can now easily get the behaviour you want with a single extra line per function: >>> @matlab ... def mean(numbers): ... return sum(numbers)/len(numbers) ... >>> mean([4.5]) 4.5 >>> mean(4.5) 4.5 >>> mean([4.5, 3.6]) 4.0499999999999998 Decorators are extremely powerful constructs, and well worth learning. As an example, here's a similar decorator that will accept multiple arguments, like the built-in functions min() and max(): def one_or_many(func): """Decorate func so that it behaves like the built-ins min() and max().""" @wraps(func) def inner(*args, **kwargs): # If we're given a single argument, and it's a vector, # use it as args. if len(args) == 1: try: iter(args[0]) except TypeError: pass else: # No exception was raised, so it's a vector. args = args[0] # Now call the decorated function (the original). return func(args, **kwargs) return inner And then use it: >>> @one_or_many ... def minmax(numbers): ... """Return the minimum and maximum element of numbers, ... making a single pass.""" ... # the following will fail if given no arguments ... smallest = biggest = numbers[0] ... for x in numbers[1:]: ... if x < smallest: ... smallest = x ... elif x > biggest: ... biggest = x ... return (smallest, biggest) ... >>> >>> minmax([2, 4, 6, 8, 1, 3, 5, 7]) (1, 8) >>> minmax(2, 4, 6, 8, 1, 3, 5, 7) (1, 8) -- Steven From tayssir.john at googlemail.com Sat Jul 25 03:23:45 2009 From: tayssir.john at googlemail.com (Tayssir John Gabbour) Date: Sat, 25 Jul 2009 00:23:45 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <12ce2d31-5028-4b60-a5b1-5d5bf1fc06d7@f20g2000prn.googlegroups.com> Message-ID: On Jul 24, 11:58 pm, ACL wrote: > I actually think that the thing holding lisp back is 'bus factor'. > > Lets assume I have a java project and a lisp project: > > Java project: > I have maybe 10 or 12 people on my team working on various subsystems > of my project. There are probably one or two 'technical leader' types > in the group, and a bunch of others who are sort of 'serfs', banging > out java classes. Lets say one of my important guys gets totally > splattered by a bus... I've still got another one left! I can rely on > the other guy to keep things afloat while I train up a new leader > type. > > Lisp project: > I don't need as many people. I have 3 or 4 people, and one person is > my technical leader and lisp guru. Guru is probably in charge of more > difficult macros and (because of that), also in charge of the overall > design (macros are design patterns embodied in code). Lets say he gets > totally annihilated by the bus. What do I do now? I had all my eggs in > one basket and my project is now stalled. A Clojure programmer mentioned interesting solutions to this used by his company, such as partnerships with similarly Agile companies. http://programmingtour.blogspot.com/2009/07/conversation-with-stuart-halloway.html I agree that the bus factor -- as well as some other problems -- works against Lisp's acceptance. All the best, Tayssir From pavlovevidence at gmail.com Sat Jul 25 03:49:12 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 25 Jul 2009 00:49:12 -0700 (PDT) Subject: len() should always return something References: Message-ID: On Jul 23, 11:35?pm, "Dr. Phillip M. Feldman" wrote: > Some aspects of the Python design are remarkably clever, while others leave > me perplexed. Here's an example of the latter: Why does len() give an error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. Of > course, I can define my own function like this: > > def mylen(x): > ? ?if isinstance(x,int) or isinstance(x,float): return 1 > ? ?return len(x) > > But, this shouldn't be necessary. I knew that you were coming from Matlab as soon as I saw the subject line. I use both Matlab and Python very often, and I understand the design decisions behind both (insofar as Matlab can be considered a "design"). The thing to keep in mind about Python is that it's a general purpose language, not just for mathematical computation, and there are some things in Python that are a bit suboptimal for math calculations. This is one of them: when the types of objects you are dealing with most often are numbers, then there's not much chance for ambiguity when treating a scalar as a 1x1 matrix. However, when doing general purpose programming, you often use lots of types besides numbers, some of which are containers themselves. This creates the possibility of ambiguity and inconsistent behavior. Let's consider your example function. def dumbfunc(xs): for x in xs: print x You can use it on an list, like this, and there is no ambiguity: dumbfunc([1,2,3]) Now suppose Python allowed numbers to be treated as degenerate sequences of length 1. Then you could pass a number, and it would work as you expect: dumbfunc(1) However, because Python is general purpose, you might also want to pass other types around, such as strings. Say you wanted to print a list of string, you would do this, and it would work as expected: dumbfunc(["abc","def","ghi"]) Now, the problem. In the numbers example, you were able to treat a single number as a degenerate list, and by our supposition, it worked. By analogy, you would expect to be able to do the same with a list of strings, like this: dumbfunc("abc") Whoops: doesn't work. Instead of printing one string "abc" it prints three strings, "a", "b", and "c". By allowing scalar numbers to act as degenerate lists, you've introduced a very bad inconsistency into the language, you've made it difficult to learn the language by analogy. For a general purpose language there is really no second-guessing this design decision. It would be unequivocally bad to have atomic types act like sequences. The reason it isn't so bad in Matlab is that you can rely on the elements of an array to be scalar. Matlab, for its part, does contain some types (cell arrays and structures) that can contain non-scalars for cases when you want to do that. However, you will notice that no scalar will ever be treated as a degenerate cell array or structure. Even a poorly designed, ad hoc language like Matlab won't try to treat scalar numbers as cell arrays, because ambiguity like I described above will appear. Nope, it takes an abomination like Perl to do that. As for how I would recommend you translate the Matlab function that rely on this: you need to decide whether that scalar number you are passing in is REALLY a scalar, or a degenerate vector. If it's the latter, just get over it and pass in a single-item list. I have done these kinds of conversions myself before, and that's the way to do it. Trying to be cute and writing functions that can work with both types will lead to trouble and inconsistency, especially if you are a Python newbie. Just pass in a list if your function expects a list. Carl Banks From hendrik at microcorp.co.za Sat Jul 25 03:50:30 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 09:50:30 +0200 Subject: len() should always return something In-Reply-To: References: Message-ID: <200907250950.30999.hendrik@microcorp.co.za> On Friday 24 July 2009 16:45:40 Mark Dickinson wrote: > On Jul 24, 3:11?pm, "Rhodri James" > > wrote: > > Which doesn't make your point less valid. ?In fact I'd go so > > far as to argue that what len() gives you is the number of > > items in a container, so len(7) should return 0. > > Nah. 7 contains three bits, so len(7) should *clearly* return 3. Almost right - however the first seven that you typed was a string seven and not an int - so the hex is 37 and that takes at least six bits... :-) Now if only someone can figure out a reason why it should return seven, then it would be perfect! - Hendrik From deets at nospam.web.de Sat Jul 25 03:55:26 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 25 Jul 2009 09:55:26 +0200 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: <7cvs3fF24hjh7U1@mid.uni-berlin.de> Dr. Phillip M. Feldman schrieb: > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is single > float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. And where comes "len(xs)" into play here? What you want is iteration over scalars. I do think that if you frequently have to write code like that, you are writing errorneous code. But might that as it is, a simple def iterable(i): if isinstance(i, (int, float, complex)): return [i] return i is all you need. So you can write for x in iterable(xs): wherever you expect values to be either scalar or iterable. Diez From piet at cs.uu.nl Sat Jul 25 04:03:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 10:03:58 +0200 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>>>> Steven D'Aprano (S) wrote: >S> Chris, I'm curious why you think that these Zen are relevant to the OP's >S> complaint. >S> Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). >S> Arguably (I wouldn't argue this, but some people might) ints aren't >S> "special enough" to break the rule that len(obj) should always return >S> something. >S> (I don't actually agree, but some people might be able to produce a >S> coherent argument why len() should apply equally to all objects.) >S> Re errors passing silently, the OP doesn't believe that len(42) should be >S> an error, so that's not relevant. >S> And there's nothing ambiguous about len(42). len(42) should be 7.5 million. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From hendrik at microcorp.co.za Sat Jul 25 04:10:32 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 10:10:32 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: Message-ID: <200907251010.32720.hendrik@microcorp.co.za> On Friday 24 July 2009 17:07:30 Inky 788 wrote: > On Jul 23, 3:42?am, Hendrik van Rooyen > > wrote: 8<------------------------ > > Steven showed why you cannot have a mutable thing > > as a key in a dict. > > > > if you think it is contrived, then please consider how you would > > keep track of say the colour of a pixel on a screen at position > > (x,y) - this is about the simplest "natural" tuple format and > > example. > > My guess is that this is probably the way most people do it: > > ~~~~ > #!/usr/bin/env python > > import sys > import random > > if len( sys.argv ) != 3: > print "Please pass exactly 2 ints. Exiting." > sys.exit(1) > > NUM_COLUMNS = int( sys.argv[1] ) > NUM_ROWS = int( sys.argv[2] ) > > print "Making array of %s columns by %s rows." % (NUM_COLUMNS, > NUM_ROWS) > > def rand(): > return int( 255 * random.random()) > > def make_a_pixel(): > # red green blue > return [rand(), rand(), rand()] > > def make_a_row(num_columns): > temp_row = [] > for i in range(num_columns): > temp_row.append( make_a_pixel() ) > return temp_row > > def make_array_of_pixels(num_columns, num_rows): > rows = [] > for i in range(num_rows): > rows.append( make_a_row(num_columns) ) > return rows > > def show_pixels(pixel_array): > for row in pixel_array: > for pixel in row: > print pixel, ' ', > print > > > rows_of_pixels = make_array_of_pixels(NUM_COLUMNS, NUM_ROWS) > > show_pixels(rows_of_pixels) > ~~~~ Good grief! - Not a dictionary in sight! I had something in mind where point was an (x,y) tuple, and point_colours was a dict of possibly named points with a list of RGB values. (The colour can change, the point is fixed) If I were doing it your way, I would use the Array module's Array. But then, strictly speaking it would not be your way any more, would it? - Hendrik From gagsl-py2 at yahoo.com.ar Sat Jul 25 04:31:20 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 25 Jul 2009 05:31:20 -0300 Subject: How do I generate dia diagrams from python source code? References: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Message-ID: En Fri, 24 Jul 2009 17:52:47 -0300, Qauzzix escribi?: > Since I have been using dia to make my UML diagrams. I also found an > util named dia2code that generates python code from dia diagram. Now > that I have that option I really want to find a way to generate dia > diagram from existing code and/or maintain my diagrams. > > I have been googling like crazy trying to find a way but with no > luck. Anyone here know how to do this? SPE does that: http://pythonide.stani.be/ -- Gabriel Genellina From hendrik at microcorp.co.za Sat Jul 25 05:01:46 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 11:01:46 +0200 Subject: len() should always return something In-Reply-To: References: <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: <200907251101.46672.hendrik@microcorp.co.za> On Friday 24 July 2009 21:04:55 Roy Smith wrote: > Compressing strings to a single bit is easy. It's the uncompressing that's > tricky. Not really - all you have to do is to apply the EXACT same sequence of operations that compressed it, in reverse. The unfortunate part is that this information is in almost all cases larger than the original, uncompressed string, so that it kind of defeats the object of compression. So yes - tricky! :-) - Hendrik From dr.mtarver at ukonline.co.uk Sat Jul 25 05:30:16 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Sat, 25 Jul 2009 02:30:16 -0700 (PDT) Subject: strange python scripting error References: Message-ID: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> On 24 July, 15:45, nn wrote: > On Jul 23, 7:03?pm, Dave Angel wrote: > > > > > > > Mark Tarver wrote: > > > I have a very strange error. ?I have two test python files test.py and > > > python.py which contain the following code > > > > #!/usr/bin/python > > > print "Content-type: text/html" > > > print > > > print "" > > > print "
Hello, Linux.com!
" > > > print "" > > > > One file (test.py) works; you call it up and it shows a web page with > > > > Hello, Linux.com > > > > The other fails with a server configuration error. ?Both are running > > > under Linux, same server, same permissions. ?Running a character scan > > > shows that both files contain the same printable characters and are > > > therefore typographically identical. ? They are absolutely the same. > > > > The only hint at a difference I can see is that my ftp program says > > > the files are of unequal lengths. ?test.py is 129 bytes long. > > > python.py 134 bytes long. > > > > A zipped folder containing both files is at > > > >www.lambdassociates.org/weird.zip > > > > Any ideas welcome. > > > > Mark > > > Easiest explanation is that python.py has Windows-style newlines. ?In > > other words, each line ends with 0d0a, rather than the Unix convention > > of 0a. > > > If your server is Unix-based, it can't handle that first line, since it > > has an illegal character (0d) following the > > > #!/usr/bin/python > > > line. ?Convert it to Unix line-endings. > > > DaveA > > Use dos2unix for conversion of the longer file and try again: > > http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm- Hide quoted text - > > - Show quoted text - That sounds the ticket - but is there anything that runs under Windows to do the trick? Mark From hendrik at microcorp.co.za Sat Jul 25 05:34:13 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 11:34:13 +0200 Subject: len() should always return something In-Reply-To: References: Message-ID: <200907251134.13221.hendrik@microcorp.co.za> On Friday 24 July 2009 22:09:15 Marcus Wanner wrote: > First one to correctly decompress the value 0 into an ASCII character > wins the title of the world's most capable hacker :p that is easy. the xor of 0 and 1 is 1, which is ASCII soh, if I remember right. soh is start of header. Burroughs poll select, anyone? - Hendrik From dr.mtarver at ukonline.co.uk Sat Jul 25 05:46:14 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Sat, 25 Jul 2009 02:46:14 -0700 (PDT) Subject: strange python scripting error References: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> Message-ID: <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> On 25 July, 10:30, Mark Tarver wrote: > On 24 July, 15:45, nn wrote: > > > > > > > On Jul 23, 7:03?pm, Dave Angel wrote: > > > > Mark Tarver wrote: > > > > I have a very strange error. ?I have two test python files test.py and > > > > python.py which contain the following code > > > > > #!/usr/bin/python > > > > print "Content-type: text/html" > > > > print > > > > print "" > > > > print "
Hello, Linux.com!
" > > > > print "" > > > > > One file (test.py) works; you call it up and it shows a web page with > > > > > Hello, Linux.com > > > > > The other fails with a server configuration error. ?Both are running > > > > under Linux, same server, same permissions. ?Running a character scan > > > > shows that both files contain the same printable characters and are > > > > therefore typographically identical. ? They are absolutely the same. > > > > > The only hint at a difference I can see is that my ftp program says > > > > the files are of unequal lengths. ?test.py is 129 bytes long. > > > > python.py 134 bytes long. > > > > > A zipped folder containing both files is at > > > > >www.lambdassociates.org/weird.zip > > > > > Any ideas welcome. > > > > > Mark > > > > Easiest explanation is that python.py has Windows-style newlines. ?In > > > other words, each line ends with 0d0a, rather than the Unix convention > > > of 0a. > > > > If your server is Unix-based, it can't handle that first line, since it > > > has an illegal character (0d) following the > > > > #!/usr/bin/python > > > > line. ?Convert it to Unix line-endings. > > > > DaveA > > > Use dos2unix for conversion of the longer file and try again: > > >http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm-Hide quoted text - > > > - Show quoted text - > > That sounds the ticket - but is there anything that runs under Windows > to do the trick? > > Mark- Hide quoted text - > > - Show quoted text - OK, got a version http://www.bastet.com/ has dos2unix.exe for Windows. And it solves the problem. Many thanks all for help on this baffling error Mark From piet at cs.uu.nl Sat Jul 25 06:20:37 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 12:20:37 +0200 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: >>>>> "Rhodri James" (RJ) wrote: >RJ> The point was, and remains, that this newsgroup gets regular traffic >RJ> from people who expect Python's variables to act like C's variables, >RJ> demonstrating that describing them as "quite compatible" is somewhat >RJ> misleading. So let' study these postings carefully to see what the actual confusion is. I myself think that the confusion is not so much the variable concept but the vale vs. reference semantics of objects. As such the same thing plays a role in Java. Although the binding mechanisms in Java and Python are different I don't think the actual semantics are so much difference as to cause much confusion (with C it could be more). But such a study could reveal that. BTW. Which postings from (say) the last month did you have in mind? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jon at ffconsultancy.com Sat Jul 25 07:02:08 2009 From: jon at ffconsultancy.com (Jon Harrop) Date: Sat, 25 Jul 2009 12:02:08 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <12ce2d31-5028-4b60-a5b1-5d5bf1fc06d7@f20g2000prn.googlegroups.com> Message-ID: ACL wrote: > Lisp project: > I don't need as many people... Is there any actual evidence of that? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u From piet at cs.uu.nl Sat Jul 25 07:14:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 13:14:08 +0200 Subject: how can a child thread notify a parent thread its status? References: Message-ID: >>>>> davidj411 (d) wrote: >d> could i see an example of this maybe? queue is a shared Queue instance. The parent puts the work in the queue with queue.put(work_object). After all work has been put in the queue it puts a sentinel queue.put(None) Each child thread (consumer) has a loop getting things out of the queue: while True: work = queue.get() if work is None: break #do real stuff using work queue.put(None) #put back the sentinel for other worker threads. #finish (Thanks to Dennis Lee Bieber) Instead of None you can also create a special sentinel object and use that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Sat Jul 25 07:18:58 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 25 Jul 2009 07:18:58 -0400 Subject: strange python scripting error In-Reply-To: <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> References: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> Message-ID: <4A6AEA22.1030207@ieee.org> Mark Tarver wrote: > > >> >>> Use dos2unix for conversion of the longer file and try again: >>> >>> http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm-Hide quoted text - >>> >>> - Show quoted text - >>> >> That sounds the ticket - but is there anything that runs under Windows >> to do the trick? >> >> Mark- Hide quoted text - >> >> - Show quoted text - >> > > OK, got a version > > http://www.bastet.com/ > > has dos2unix.exe for Windows. And it solves the problem. > > Many thanks all for help on this baffling error > > Mark > > I use metapad for simple text editing, see http://en.wikipedia.org/wiki/Metapad It's free, loads very quickly, and has a number of features I like. One of its trivial features is to read/write files in four different formats. In the file menu, you just choose what file-format you need now. Another thing I'd point out is that some ftp programs will do this conversion as the file is being sent between a local DOS machine and a Unix machine on the internet. DaveA From gert.cuykens at gmail.com Sat Jul 25 07:45:31 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 25 Jul 2009 04:45:31 -0700 (PDT) Subject: cgi.fieldstorage() References: <7cu9hhF29gt65U1@mid.uni-berlin.de> <7cv268F29q0j5U1@mid.uni-berlin.de> Message-ID: <13ed9818-aacd-4260-800b-a04177e13681@n11g2000yqb.googlegroups.com> On Jul 25, 2:33?am, "Diez B. Roggisch" wrote: > gert schrieb: > > > On Jul 24, 7:32 pm, "Diez B. Roggisch" wrote: > >> gert schrieb: > > >>> this is a non standard way to store multi part post data on disk > >>> def application(environ, response): > >>> ? ? with open('/usr/httpd/var/wsgiTemp','w') as f: > >>> ? ? ? ? while True: > >>> ? ? ? ? ? ? chunk = environ['wsgi.input'].read(8192).decode('latin1') > >>> ? ? ? ? ? ? if not chunk: break > >>> ? ? ? ? ? ? f.write(chunk) > >>> ? ? response('200 OK',[]) > >>> ? ? return ['complete'] > >>> my question is how do i handle the file, so i can shuffle it into a db > >>> using small chunks of memorie ? > >> I don't think that's possible with the current DB-API. There is no > >> stream-based BLOB-interface (as e.g. JDBC offers). > > >> So the answer certainly depends on your used RDBMS. For oracle, you > >> would be lucky: > > >>http://cx-oracle.sourceforge.net/html/lob.html > > >> Other adapters I don't know about. > > > sqlite :) ok let say for now it would be impossible on a db level, but > > before i reach the impossible, i still need to parse the file to > > prepare the chunks. How do i do that ? How do i get the chunks without > > loading the hole file into memorie ? > > It's memory - memorie might be some nice dutch girl you know :) > > Apart from that, your code above does exactly that - reads the data > chunkwise. If the WSGI-implementation works proper, this will be the > socket's input stream, so there is no memory overhead involved. > > Now of course if you want to have a multi-pass processing of the file > without putting it into memory, then you need to save it to the harddisk > befor. > > But honestly - we are talking about a web-application here. My > DSL-connection has 1 MBit upstream, the average server has at least 2GB > of memory available - so we are talking 20000 seconds uploading time to > fill that memory. Which is about 5 hours. And you are decoding the data > to a certain decoding, so we are not talking about binary data here - > are you really sure memory is an issue? > What about some http upload resume features ? From pavlovevidence at gmail.com Sat Jul 25 07:45:48 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 25 Jul 2009 04:45:48 -0700 (PDT) Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> <8763di5d1d.fsf@benfinney.id.au> Message-ID: <2977b59f-6843-4c2e-86c2-c0fa5dfdcf72@u38g2000pro.googlegroups.com> On Jul 24, 8:14?am, Ben Finney wrote: > Hrvoje Niksic writes: > > The term "variable" is used in the Python language reference and > > elsewhere > > Yes. It should also be abundantly clear from the constant stream of > confused newbies on this point that its usage of that term is different > to what many expect from usage elsewhere. Personally, I haven't noticed a constant stream, just a few here and there. I expect that someone used to C variables will expect Python variables to behave the same way even if you call them something different, so what really is the point? Carl Banks From pavlovevidence at gmail.com Sat Jul 25 07:49:09 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 25 Jul 2009 04:49:09 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <7fa7aa37-fd9e-4702-85f8-1f9b9a05c54d@i18g2000pro.googlegroups.com> On Jul 24, 11:54?pm, Xah Lee wrote: [snip] > References: > > ? Language, Purity, Cult, and Deception > ?http://xahlee.org/UnixResource_dir/writ/lang_purity_cult_deception.html > > ? What Languages to Hate > ?http://xahlee.org/UnixResource_dir/writ/language_to_hate.html > > ? Lambda in Python 3000 > ?http://xahlee.org/perl-python/python_3000.html > > ? Python Documentation Problems > ?http://xahlee.org/perl-python/python_doc_index.html > > ? Examples Of Quality Documentation In The Computing Industry > ?http://xahlee.org/perl-python/quality_docs.html > > ? Xah's Perl and Python Tutorial > ?http://xahlee.org/perl-python/index.html > > ? Xah's PHP Tutorial > ?http://xahlee.org/php/index.html > > ? Xah > ?http://xahlee.org/ Wow, you leave no stone unturned. > "computer science R us" I'm stealing this. Carl BAnks From marcusw at cox.net Sat Jul 25 08:55:47 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 25 Jul 2009 08:55:47 -0400 Subject: len() should always return something In-Reply-To: References: Message-ID: On 7/25/2009 5:34 AM, Hendrik van Rooyen wrote: > On Friday 24 July 2009 22:09:15 Marcus Wanner wrote: > >> First one to correctly decompress the value 0 into an ASCII character >> wins the title of the world's most capable hacker :p > > that is easy. > > the xor of 0 and 1 is 1, which is ASCII soh, if I remember right. > > soh is start of header. > > Burroughs poll select, anyone? > > - Hendrik > nope, not soh. Marcus From piet at cs.uu.nl Sat Jul 25 08:57:19 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 14:57:19 +0200 Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? References: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> Message-ID: >>>>> ryles (r) wrote: >r> According to http://www.python.org/doc/essays/packages.html: >r> "The import statement first tests whether the item is defined in the >r> package; if not, it assumes it is a module and attempts to load it." >r> However, I've noticed that once a module is imported using the >r> `from pkg import mod' syntax, if its name is deleted from the >r> package namespace then subsequent imports with `from' will fail. This is incorrectly stated. Also on the initial import it will fail, not just on subsequent imports. piet$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pkg import _impl _impl # this is from a print statement in _impl to show that it did import Traceback (most recent call last): File "", line 1, in ImportError: cannot import name _impl >>> According to the documentation the statement from pkg import _impl _temp = __import__('pkg', globals(), locals(), ['_impl'], -1) _impl = _temp._impl This fails because _temp (the imported module) doesn't have a binding for _impl because you deleted it. By the way, if you have a `normal' package that doesn't do anything with the name _impl, after the import the pkg module namespace will have got a binding for _impl although it is not in your code. It will have been put there by the import code. In the following example pkg2/__init__.py just contains the line A = 2 piet$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pkg2 import _impl _impl >>> import sys >>> dir(sys.modules['pkg2']) ['A', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '_impl'] >>> It is not clear to me, however what the order is of the statements in __init__.py and the insertion of _impl in the module namespace. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 08:59:43 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 12:59:43 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: <027af2b3$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 10:03:58 +0200, Piet van Oostrum wrote: >>S> And there's nothing ambiguous about len(42). > > len(42) should be 7.5 million. And "I don't understand your reasoning".upper() should be "Millennium Hand and Shrimp!". Every function would be ambiguous if we treat it as returning an arbitrary result. But we don't -- semantics do matter. We expect that string.upper() should return the string converted to uppercase, and that's not ambiguous because we already know what uppercase means. Even if somebody didn't know what uppercase meant, they would expect that it had a meaning, and it was just a matter of learning what it meant: for example, one might take string.upper() as any of: * convert every letter a..z to A..Z * convert the first letter of each word to A..Z * throw away the argument and return the literal "upper" * count how many characters are in A..Z and so forth. These are some of the infinite number of *possible* meanings to string.upper(), but only one of them is the *actual* meaning. The method isn't ambiguous, because the language designers have chosen one meaning, and having learned what that meaning is, it's the *only* meaning you can legitimately use. If you (generic you) are uncertain what that meaning is, that's uncertainty, not ambiguity. Similarly, we might be uncertain about the meaning of len(42) -- reasonable values it might return are 0, 1, 2, the number of bits in 42, and possibly even 7.5 million as you suggest. But that's our ignorance, due to the fact that we haven't yet agreed on a meaning to len(42), and not ambiguity: having made up our mind what len(42) should mean, it could only mean one thing. However, mixed string/integer addition is a good example of ambiguity. We can add strings: "1" + "2" => "12" and we can add integers: 1 + 2 => 3 but it is ambiguous as to what "1" + 2 should return. Guido might have made Python behave like Perl, and define addition of a string and an int, but there would still be ambiguity in the expression, because we can't tell if the coder intended "1"+"2" and forgot to quote the two, or intended 1+2 and forgot to convert the string "1" to an int. Ambiguity essentially boils down to being unable to reasonably predict the expectation of the coder. I say "reasonably", because if you allow unreasonable situations, everything is "ambiguous": I typed: x = 42 + y but maybe I intended to import the math module, therefore the + operator is ambiguous. Reasonable? No, because we're allowed to assume the coder is rational, and mistakes are small. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 09:37:51 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 13:37:51 GMT Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> <8763di5d1d.fsf@benfinney.id.au> <2977b59f-6843-4c2e-86c2-c0fa5dfdcf72@u38g2000pro.googlegroups.com> Message-ID: <027afba2$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 04:45:48 -0700, Carl Banks wrote: > On Jul 24, 8:14?am, Ben Finney wrote: >> Hrvoje Niksic writes: >> > The term "variable" is used in the Python language reference and >> > elsewhere >> >> Yes. It should also be abundantly clear from the constant stream of >> confused newbies on this point that its usage of that term is different >> to what many expect from usage elsewhere. > > Personally, I haven't noticed a constant stream, just a few here and > there. > > I expect that someone used to C variables will expect Python variables > to behave the same way even if you call them something different, so > what really is the point? >From time to time we get newbies to Python assuming that len(list) is O(N), because the lists they learned about in Comp Sci 101 (or Lisp) are linked lists and traversing a linked list to count the items is O(N). Do you believe that if Python lists were called "arrays", or "waskilators", they would make the same mistake? [Generalisation] C programmers have a mental model for how "variables" behave. They have no such mental model for how "objects bound to names" behave, and so they are less likely to jump to conclusions about the behaviour of name- bindings. So I think you are mostly wrong. However, only mostly -- human beings are generalisers par excellence, or rather, *over*-generalisers par excellence. Experienced coders are likely to rapidly realise that name-binding is very similar to the C variable model, and some percentage of them will over-generalise to assume that name-binding is the same as the C model. But if they do, at least that will be their fault for jumping to conclusions, not our fault for misleading them. -- Steven From piet at cs.uu.nl Sat Jul 25 10:08:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 16:08:27 +0200 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <027af2b3$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>>>> Steven D'Aprano (SD) wrote: >SD> Ambiguity essentially boils down to being unable to reasonably predict >SD> the expectation of the coder. I say "reasonably", because if you allow >SD> unreasonable situations, everything is "ambiguous": That's for me the reason that len(42) is ambiguous. The OP apparently had 1 as expectation, whereas my first thought was the minimal number of bits to represent the number and 7.5 million came later :=). The number of bits I certainly find reasonable, and I would find the number of decimal digits equally reasonable. More so than 1, actually. 1 as the length of an int doesn't give any information. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Sat Jul 25 10:33:52 2009 From: aahz at pythoncraft.com (Aahz) Date: 25 Jul 2009 07:33:52 -0700 Subject: comments? storing a function in an object References: <20efdb6a-c1a5-47dc-8546-7c4ae548e22d@g1g2000pra.googlegroups.com> Message-ID: In article <20efdb6a-c1a5-47dc-8546-7c4ae548e22d at g1g2000pra.googlegroups.com>, Carl Banks wrote: >On Jul 22, 8:38=A0pm, a... at pythoncraft.com (Aahz) wrote: >> In article .com>, >> Carl Banks =A0 wrote: >>> >>>You have to be REALLY REALLY careful not to pass any user-supplied >>>data to it if this is a server running on your computer, of course. >> >> Unless, of course, your users are paying for this service. > >Well, yes, but I assume that by the time you're deliberately letting >users pay to run their programs on your server, you will already have >deployed a full-blown, multi-tiered security strategy that includes >validation by the server process. That was sort of beyond the scope >of the OP's question. That's not necessarily a good assumption. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From aahz at pythoncraft.com Sat Jul 25 11:20:51 2009 From: aahz at pythoncraft.com (Aahz) Date: 25 Jul 2009 08:20:51 -0700 Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> Message-ID: In article <37da38d2-09a8-4fd2-94b4-5feae9675dcd at k1g2000yqf.googlegroups.com>, Filip wrote: > >I tried to fix that with BeautifulSoup + regexp filtering of some >particular cases I encountered. That was slow and after running my >data scraper for some time a lot of new problems (exceptions from >xpath parser) were showing up. Not to mention that BeautifulSoup >stripped almost all of the content from some heavily broken pages >(50+KiB page stripped down to some few hundred bytes). Character >encoding conversion was a hell too - even UTF-8 pages had some non- >standard characters causing issues. Have you tried lxml? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From aahz at pythoncraft.com Sat Jul 25 12:12:49 2009 From: aahz at pythoncraft.com (Aahz) Date: 25 Jul 2009 09:12:49 -0700 Subject: strange error when trying to log something References: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Message-ID: In article , Peter Otten <__peter__ at web.de> wrote: > >I have a hunch that you are triggering a reload() somewhere. Example: > >Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) >[GCC 4.3.3] on linux2 >Type "help", "copyright", "credits" or "license" for more information. >>>> import weakref >>>> weakref.WeakValueDictionary() > >>>> import UserDict >>>> reload(UserDict) > >>>> weakref.WeakValueDictionary() >Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.6/weakref.py", line 51, in __init__ > UserDict.UserDict.__init__(self, *args, **kw) >TypeError: unbound method __init__() must be called with UserDict instance >as first argument (got WeakValueDictionary instance instead) Nice sleuthing! How did you figure that out? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From hendrik at microcorp.co.za Sat Jul 25 12:15:37 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 18:15:37 +0200 Subject: len() should always return something In-Reply-To: <027af2b3$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <027af2b3$0$5185$c3e8da3@news.astraweb.com> Message-ID: <200907251815.37762.hendrik@microcorp.co.za> On Saturday 25 July 2009 14:59:43 Steven D'Aprano wrote: > On Sat, 25 Jul 2009 10:03:58 +0200, Piet van Oostrum wrote: > >>S> And there's nothing ambiguous about len(42). > > > > len(42) should be 7.5 million. > > And "I don't understand your reasoning".upper() should be "Millennium > Hand and Shrimp!". That does it. I will be kinder to you in future. Anybody who quotes foul old Ron cannot be all bad. - Hendrik From albert at spenarnc.xs4all.nl Sat Jul 25 13:27:52 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 25 Jul 2009 17:27:52 GMT Subject: missing 'xor' Boolean operator References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: In article , Jean-Michel Pichavant wrote: >Christian Heimes wrote: >> Chris Rebert wrote: >> >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >>> >> >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >> raise ValueError("Exactly one of a, b, c and d must be true") >> >> Christian >> >> >While everyone's trying to tell the OP how to workaround the missing xor >operator, nobody answered the question "why is there no xor operator ?". > >If the question was "Why is there no 'or' operator ?", would "because A >or B <=> not(not A and not B)" be a proper answer ? No. I think it is because and/or can be extended to be sensible in a context where objects can be used. (What others have expressed as having short-circuit evaluation. So sce indeed is the underlying reason that and/or can be extended sensibly to objects.) Remains whether we need an xor that only works and requires that both operands are booleans. That one we have already! It is called != . (a!=b)!=c and a!=(b!=c) are the same for booleans, so can indeed be expressed a!=b!=c (associativy of xor) > >JM > Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From tjreedy at udel.edu Sat Jul 25 13:30:54 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 25 Jul 2009 13:30:54 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: Albert van der Horst wrote: > Remains whether we need an xor that only works and requires that > both operands are booleans. That one we have already! > It is called != . > > (a!=b)!=c > and > a!=(b!=c) > > are the same for booleans, so can indeed be expressed > a!=b!=c (associativy of xor) Not in Python >>> a,b,c = True, False, True >>> (a!=b)!=c False >>> a!=(b!=c) False >>> a!=b!=c True >>> (a!=b) and (b!=c) True In Math and Python, a>> (a^b)^c False >>> a^b^c False Terry Jan Reedy From scriptlearner at gmail.com Sat Jul 25 13:32:06 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Sat, 25 Jul 2009 10:32:06 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? References: Message-ID: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> First of all, let me say thank you to all of you. I have asked many questions (some of them are dump questions), and you have kindly helped me. I am not going to reply every message to say thank-you since that would be annoying for such group with such high daily traffics. Thank you very much. Let's get back to topic of this message. Here's how I have implemented it so far, and I am taking the queue of work load items approach. In my child thread, I will keep checking for available work load item until a duration is reached. #inside the child# while endTime > time.time(): try: item = self.q.get(True, 3) except Queue.Empty: #what's wrong? AttributeError: class Queue has no attribute 'Empty' print 'cant find any work load item, so lets wait and try again later' time.sleep(1) #wait and then check again continue except: print "Unexpected error:", sys.exc_info()[0] raise #do the real work with load item In my parent thread, I will initialize X (depending on a cfg file) child threads and keep adding load items to a shared q until the duration is reached. #inside the parent# callCounter = 0 workers = [] #a list of child threads totalWorkers = 250 endTime = time.time() + duration for i in range(totalWorkers): w = Worker(q, duration, i) w.start() #worker, do your job now! workers.append(w) while endTime > time.time(): time.sleep(1) q.put(getWorkloadItem()) #add workload itmes callCounter += 1 #actually can we guarantee that the call will be sent?? #should we ask each child to report the number of calls they make? for i in range(totalWorkers): workers[i].join() # Wait for the child threads to finish Overall, it seems to be working now. Though, I still have a couple of problems to resolve. 1. I got the following error for the codes that attempt to catch Empty Queue exception. What's the right way to use it? except Queue.Empty: AttributeError: class Queue has no attribute 'Empty' 2. What's the best way to have each child thread to report the number of requests they send when they are done? To add the numbers to another queue? 3. I will need to do some logging for response time as well as some response contents. I have two choices, one big log file for all threads (both child and parent), and one log file for each thread. Given the fact that I may have to log tons of data, I think opening and maintaining a bunch of smaller logs may be better than dealing with a big one (it may grow very fast). Is there any best prastice for logging in Python? If I change my mind and go with one big log file (pass it to each thread), is there anything I should be aware of for multi-thread access (writting) to the same log file? Again, thank you. From albert at spenarnc.xs4all.nl Sat Jul 25 14:01:33 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 25 Jul 2009 18:01:33 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: <02746997$0$5185$c3e8da3@news.astraweb.com> Message-ID: In article , Jack Diederich wrote: >On Mon, Jul 20, 2009 at 10:00 AM, Steven >D'Aprano wrote: >> On Mon, 20 Jul 2009 09:34:24 +0000, Sion Arrowsmith wrote: >> >>> Terry Reedy =A0 wrote: >>>>Sion Arrowsmith wrote: >>>>> Jack Diederich =A0 wrote: >>>>>> It isn't an OrderedDict thing, it is a comparison thing. =A0Two regul= >ar >>>>>> dicts also raise an error if you try to LT them. >>>>> Python 2.5.2 >>>>>>>> d1 =3D dict((str(i), i) for i in range (10)) d2 =3D dict((str(i), i= >) >>>>>>>> for i in range (20)) d1 < d2 >>>>> True >>>>Try reversing the definitions of d1 and d2. The dicts are probably being >>>>compared by id (address), which is the 2.x CPython default. >>> >>> Like this? >>> >>>>>> d1 =3D dict((str(i), i) for i in range (20)) >>>>>> d2 =3D dict((str(i), i) for i in range (10)) >>>>>> d1 < d2 >>> False >>>>>> id(d1) < id(d2) >>> True >>> >>> I didn't know that comparison for anything other than equality defaulted >>> to using id. That really is rather broken, and I'm glad 3.0 fixed it. >> >> >> I don't think comparisons other than equality use id. That would be >> rather insane. If anyone can demonstrate such comparisons in a built-in >> or standard library class, I'd like to see it. >> >> >> For the record, dicts have been comparable with < and > since at least >> Python 1.5: >> >> $ python1.5 >> Python 1.5.2 (#1, Apr =A01 2009, 22:55:54) =A0[GCC 4.1.2 20070925 (Red Ha= >t >> 4.1.2-27)] on linux2 >> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>>> >>>>> {1: 'a', 2: 'b'} < {2: 'b', 3: 'a'} >> 1 >> >> >> Reading the docs: >> >> http://docs.python.org/library/stdtypes.html#comparisons >> http://docs.python.org/library/stdtypes.html#mapping-types-dict >> >> I can only suggest that dicts compare in an arbitrary but consistent >> fashion. > >I should have specified 3.x but since we were talking about >OrderedDicts (only in 3.1) I didn't say it explicitly. Earlier >versions of python were very permissive with comparisons -- it was >rare that any cmp() raised an error and the ultimate fallback was on >the object's id. This is one of the non-backwards compatible changes >in 3k. Now comparing two of the same thing that don't have an obvious >ordering is an error. Is a dict "greater than" if it has a larger >size? if its max key is larger? what does "max key" mean when the >keys aren't even comparable?. Comparing things that aren't extremely >similar is an error now also. With regard to < and > you are right. But I think there is a sensible == w.r.t. dict's. It is to mean that for each key dict1(key) == dict2(key) (implying that their key set must be the same) [I could have used that for one of the euler problems. You have a 4 by 4 field containing a red or blue square. That is naturally a mapping of (1,1) ..(4,4) tuples to one of the objects `blue' `red'. After moving a square you want to know whether this is a map you already have encountered.] > >-Jack -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From kawenks at gmail.com Sat Jul 25 14:04:45 2009 From: kawenks at gmail.com (allan) Date: Sat, 25 Jul 2009 11:04:45 -0700 (PDT) Subject: Form/Template Fill-in the blanks Message-ID: <4b77a992-370d-4879-88a0-fdd6a23f7c5d@p10g2000prm.googlegroups.com> I'm attempting to create a generic python script that will take an EDI document specification and a database of transactions and be able to generate a raw EDI file (ansi X12). I'm looking for ideas on how best to use the strengths of Python to implement this. I've initially tackled the EDI 812 specifications and narrowed this down to a specific implementation by a company. This narrowed down version is what I want to achieve for output. But I want the script to be generic enough such that I can plug-in another EDI specification + a database transaction set and it will output the proper raw file accordingly. My initial thought was to use: 1. .ini files to declare the EDI configuration 2. Use SQLAlchemy as ORB to simplify access to database objects. INI file configuration: * A "root" INI file indicates other INI files that define each segment of the EDI document. * Each segment INI defines the data elements of each segment and the behavior of the segment (one instance or multiple-instance as in a loop, sequence order, segment code, etc.) * Specify data elements as either constant, system function (like datetime), database field or object method (object being the segment's) * Load all the ini configuration into a "template" object. Each segment ini maps to its own segment object. DB using SQLAlchemy Gather a Trading Partner data and Credit Transaction (EDI 812 remember?) into one dictionary object Gather Credit Transaction details into another dictionary object where it can generate the multiple instance segments The heart of the matter is how to fuse together the template and the data from the dictionary objects efficiently. It should be generic enough to take another set of data dictionary and another template to generate a completely new EDI document. I'm stuck at this fusing together thing. Is this a good approach? Is there an easier to implement approach? Comments, suggestions, questions please. Allan From piet at cs.uu.nl Sat Jul 25 14:18:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 20:18:46 +0200 Subject: Why aren't OrderedDicts comparable with < etc? References: <02746997$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>>>> Albert van der Horst (AvdH) wrote: >AvdH> With regard to < and > you are right. >AvdH> But I think there is a sensible == w.r.t. dict's. >AvdH> It is to mean that for each key dict1(key) == dict2(key) >AvdH> (implying that their key set must be the same) >AvdH> [I could have used that for one of the euler problems. >AvdH> You have a 4 by 4 field containing a red or blue square. >AvdH> That is naturally a mapping of (1,1) ..(4,4) tuples to one >AvdH> of the objects `blue' `red'. After moving a square you >AvdH> want to know whether this is a map you already have encountered.] So what's the problem? piet$ python3 Python 3.1 (r31:73578, Jun 27 2009, 21:49:46) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from collections import OrderedDict >>> a = OrderedDict() >>> b = OrderedDict() >>> a[1]=2 >>> b[1]=2 >>> a[3]=4 >>> b[3]=4 >>> a==b True >>> b[5]=6 >>> a==b False >>> d1 = dict((str(i), i) for i in range (10)) >>> d2 = dict((str(i), i) for i in range (10)) >>> d1 == d2 True -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From constant.beta at gmail.com Sat Jul 25 14:30:54 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Sat, 25 Jul 2009 11:30:54 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones Message-ID: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Hi, Is there a way to tell if a generator has been exhausted using pure Python code? I've looked at CPython sources and it seems that something like "active"/"exhausted" attribute on genobject is missing from the API. For the time being I am using a simple C extension to look at f_stacktop pointer of the generator frame, which seems to differentiate active generators from exhausted ones. See http://bazaar.launchpad.net/~ruby/pythoscope/support-python2.3/annotate/286/pythoscope/_util.c#L16 for complete source code. I may be missing something obvious here. Is there a better way to tell if a given generator object is still active or not? Cheers, mk From python at mrabarnett.plus.com Sat Jul 25 14:39:24 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 25 Jul 2009 19:39:24 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> Message-ID: <4A6B515C.9040803@mrabarnett.plus.com> scriptlearner at gmail.com wrote: > First of all, let me say thank you to all of you. I have asked many > questions (some of them are dump questions), and you have kindly > helped me. I am not going to reply every message to say thank-you > since that would be annoying for such group with such high daily > traffics. Thank you very much. > > Let's get back to topic of this message. > Here's how I have implemented it so far, and I am taking the queue of > work load items approach. > In my child thread, I will keep checking for available work load item > until a duration is reached. > #inside the child# > while endTime > time.time(): > try: > item = self.q.get(True, 3) > except Queue.Empty: #what's wrong? AttributeError: class > Queue has no attribute 'Empty' > print 'cant find any work load item, so lets wait and > try again later' > time.sleep(1) #wait and then check again > continue > except: > print "Unexpected error:", sys.exc_info()[0] > raise > #do the real work with load item > > In my parent thread, I will initialize X (depending on a cfg file) > child threads and keep adding load items to a shared q until the > duration is reached. > #inside the parent# > callCounter = 0 > workers = [] #a list of child threads > totalWorkers = 250 > endTime = time.time() + duration > for i in range(totalWorkers): > w = Worker(q, duration, i) > w.start() #worker, do your job now! > workers.append(w) > > while endTime > time.time(): > time.sleep(1) > q.put(getWorkloadItem()) #add workload itmes > callCounter += 1 #actually can we guarantee that the > call will be sent?? > #should we ask each child to report > the number of calls they make? > > for i in range(totalWorkers): > workers[i].join() # Wait for the child threads to > finish > > > Overall, it seems to be working now. Though, I still have a couple of > problems to resolve. > 1. I got the following error for the codes that attempt to catch Empty > Queue exception. What's the right way to use it? > except Queue.Empty: > AttributeError: class Queue has no attribute 'Empty' > The exception 'Empty' belongs to the module, not the class. Try importing as: from Queue import Queue, Empty > 2. What's the best way to have each child thread to report the number > of requests they send when they are done? To add the numbers to > another queue? > Why not? :-) > 3. I will need to do some logging for response time as well as some > response contents. I have two choices, one big log file for all > threads (both child and parent), and one log file for each thread. > Given the fact that I may have to log tons of data, I think opening > and maintaining a bunch of smaller logs may be better than dealing > with a big one (it may grow very fast). Is there any best prastice > for logging in Python? If I change my mind and go with one big log > file (pass it to each thread), is there anything I should be aware of > for multi-thread access (writting) to the same log file? > > Again, thank you. If you like threads then you could put the log items into a queue and have another thread writing them to the logfile. :-) BTW, do you really need 250 threads? Seems like a lot. I notice that you stop putting items into the queue when endTime is reached and also the threads terminate when endTime is reached. If items are put into the queue faster than they're taken out (or an item is put in just before endTime) then there might still be unprocessed items in the queue at endTime. From rknop at pobox.com Sat Jul 25 14:57:55 2009 From: rknop at pobox.com (Rob Knop) Date: Sat, 25 Jul 2009 13:57:55 -0500 Subject: RSA cryptography between Python and Java Message-ID: I've created an RSA key in Java. I have exported the public key by making it into a X509EncodedKeySpec and spitting out the result of getEncoded(). I want to use this public key to encode something in python that I will send to Java, and then decode in Java with the corresponding private key. Are there any python libraries that will take a public key in this format and do RSA encoding on it? -- --Rob Knop E-mail: rknop at pobox.com Home Page: http://www.pobox.com/~rknop/ Blog: http://www.sonic.net/~rknop/blog/ From http Sat Jul 25 15:01:07 2009 From: http (Paul Rubin) Date: 25 Jul 2009 12:01:07 -0700 Subject: RSA cryptography between Python and Java References: Message-ID: <7xocr8si30.fsf@ruckus.brouhaha.com> Rob Knop writes: > Are there any python libraries that will take a public key in this > format and do RSA encoding on it? Try www.trevp.com/tlslite From rylesny at gmail.com Sat Jul 25 15:11:23 2009 From: rylesny at gmail.com (ryles) Date: Sat, 25 Jul 2009 12:11:23 -0700 (PDT) Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? References: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> Message-ID: On Jul 25, 8:57?am, Piet van Oostrum wrote: > >>>>> ryles (r) wrote: > >r> According tohttp://www.python.org/doc/essays/packages.html: > >r> "The import statement first tests whether the item is defined in the > >r> package; if not, it assumes it is a module and attempts to load it." > >r> However, I've noticed that once a module is imported using the > >r> `from pkg import mod' syntax, if its name is deleted from the > >r> package namespace then subsequent imports with `from' will fail. > > This is incorrectly stated. Also on the initial import it will fail, not > just on subsequent imports. > > piet$ python > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > Type "help", "copyright", "credits" or "license" for more information.>>> from pkg import _impl > > _impl # this is from a print statement in _impl to show that it did import > Traceback (most recent call last): > ? File "", line 1, in > ImportError: cannot import name _impl > Well, since __init__.py is executed first, I would regard it as causing the initial (successful) import, and yours being the subsequent one, so that the "initial" import does not fail, the subsequent one does. Wording aside, though, I think we are on the same page here. > > According to the documentation > the statement > from pkg import _impl > > _temp = __import__('pkg', globals(), locals(), ['_impl'], -1) > _impl = _temp._impl > > This fails because _temp (the imported module) doesn't have a binding > for _impl because you deleted it. But supposing we hadn't deleted it, and __init__.py didn't import _impl, there would still be no binding. Yet the importer would still import and make the module available (presumably it "knows" to do this since it was not already in sys.modules). The question really is that since in our example pkg._impl is still in sys.modules, why won't the importer add it to the pkg namespace again as it previous had? I would imagine this is either by design, or is a case that was never considered. > for _impl because you deleted it. By the way, if you have a `normal' > package that doesn't do anything with the name _impl, after the import > the pkg module namespace will have got a binding for _impl although it is not > in your code. It will have been put there by the import code. Yes, this was noted further down in the original post with an example. From tom.sully at gmx.com Sat Jul 25 15:27:37 2009 From: tom.sully at gmx.com (Tom) Date: Sat, 25 Jul 2009 20:27:37 +0100 Subject: Removing newlines from string on windows (without replacing) Message-ID: <1248550057.5509.86.camel@ubuntu.ubuntu-domain> This is my first post to this mailing list, so hi :) I have an annoying problem. While I mainly use Linux when I distribute this program to friends and on the internet, it'll get used on Windows. So, I tested my python program on my Windows Vista dual boot, running the same version of python (2.6) as my Linux, and got an error at the following code. s = sauce.replace("\n", "") Sauce is a string, read from a file, that I need to remove newlines from. This code works fine in Linux, but not in Windows. rstrip("\n") won't work for me, so anybody know how to get this working on Windows? Thanks! :D --print "Tom" From rhodri at wildebst.demon.co.uk Sat Jul 25 15:53:51 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sat, 25 Jul 2009 20:53:51 +0100 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: On Sat, 25 Jul 2009 03:50:54 +0100, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is > single > float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of > the > annoying things about Python. If you persist in treating as if it was , then your code will always be ugly, and often buggy. Unless we're talking natural languages, in which case Yoda-like you will sound. Fundamentally, your problem is your assertion that it is reasonable to allow users to treat a single object as if it were wrapped in a list. In Python, it is not reasonable, for the reasons that you are spending so much time complaining about. -- Rhodri James *-* Wildebeest Herder to the Masses From tack at urandom.ca Sat Jul 25 16:00:02 2009 From: tack at urandom.ca (Jason Tackaberry) Date: Sat, 25 Jul 2009 16:00:02 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <1248552002.4390.2.camel@willow> On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: > Is there a way to tell if a generator has been exhausted using pure > Python code? I've looked at CPython sources and it seems that Upon a cursory look, after a generator 'gen' is exhausted (meaning gen.next() has raised StopIteration), it seems that gen.gi_frame will be None. Cheers, Jason. From rknop at pobox.com Sat Jul 25 16:08:17 2009 From: rknop at pobox.com (Rob Knop) Date: Sat, 25 Jul 2009 15:08:17 -0500 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin writes: > www.trevp.com/tlslite Thanks, but that looks like a library for setting up a secure connection between two ends. What I need is the ability to encrypt with a public key in Python, where that public key was generated in Java as described, and where the cipertext can later be decrypted with the corresponding secret key in Java. -- --Rob Knop E-mail: rknop at pobox.com Home Page: http://www.pobox.com/~rknop/ Blog: http://www.sonic.net/~rknop/blog/ From rhodri at wildebst.demon.co.uk Sat Jul 25 16:08:54 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sat, 25 Jul 2009 21:08:54 +0100 Subject: Removing newlines from string on windows (without replacing) In-Reply-To: <1248550057.5509.86.camel@ubuntu.ubuntu-domain> References: <1248550057.5509.86.camel@ubuntu.ubuntu-domain> Message-ID: On Sat, 25 Jul 2009 20:27:37 +0100, Tom wrote: > This is my first post to this mailing list, so hi :) > > I have an annoying problem. While I mainly use Linux when I distribute > this program to friends and on the internet, it'll get used on Windows. > So, I tested my python program on my Windows Vista dual boot, running > the same version of python (2.6) as my Linux, and got an error at the > following code. > > s = sauce.replace("\n", "") What error? (I don't let Vista in the house.) > Sauce is a string, read from a file, that I need to remove newlines > from. This code works fine in Linux, but not in Windows. rstrip("\n") > won't work for me, so anybody know how to get this working on Windows? Why won't rstrip("\n") work? Is rstrip() OK instead, or does trailing whitespace matter to you? To provide a partial answer, your problem probably lies in the different ways Windows and Linux treat ends of lines. Under Linux, "\n" is the line terminator. Under Windows, it's "\r\n". If you opened your file in "byte mode" with open("myfile.txt", "rb"), you will be given all the bytes in the file, including those extra "\r" characters on Windows. If you open your file in text mode with open("myfile.txt, "r") instead, the line endings are converted to "\n" on both Windows and Linux. If my crystal ball has proved faulty, giving us more details will help get a more sensible answer. -- Rhodri James *-* Wildebeest Herder to the Masses From http Sat Jul 25 16:10:43 2009 From: http (Paul Rubin) Date: 25 Jul 2009 13:10:43 -0700 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: <7xiqhgmsl8.fsf@ruckus.brouhaha.com> Rob Knop writes: > > www.trevp.com/tlslite > > Thanks, but that looks like a library for setting up a secure connection > between two ends. What I need is the ability to encrypt with a public > key in Python, where that public key was generated in Java as described, > and where the cipertext can later be decrypted with the corresponding > secret key in Java. Yes, I think that library has the function that you want. It's just X509 DER encoding. From constant.beta at gmail.com Sat Jul 25 16:20:11 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Sat, 25 Jul 2009 13:20:11 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> On Jul 25, 10:00?pm, Jason Tackaberry wrote: > On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: > > Is there a way to tell if a generator has been exhausted using pure > > Python code? I've looked at CPython sources and it seems that > > Upon a cursory look, after a generator 'gen' is exhausted (meaning > gen.next() has raised StopIteration), it seems that gen.gi_frame will be > None. Only in Python 2.5 or higher though. I need to support Python 2.3 and 2.4 as well, sorry for not making that clear in the original post. Cheers, mk From news123 at free.fr Sat Jul 25 16:29:29 2009 From: news123 at free.fr (News123) Date: Sat, 25 Jul 2009 22:29:29 +0200 Subject: available formats and params for Image.save() In-Reply-To: References: <4a6833dd$0$22599$426a34cc@news.free.fr> Message-ID: <4a6b6b29$0$435$426a74cc@news.free.fr> Thanks a lot Gabriel, Your answer is perfect. I was only looking in part II of the PIL manual and overlooked the Appendix :-(. I also apreciate Image.ID , Image.SAVE , Image.OPEN I saw them when typing dir Image but I never had the idea of calling Image.init() first bye N Gabriel Genellina wrote: > En Thu, 23 Jul 2009 06:56:45 -0300, News123 escribi?: > >> Somehow I have difficulties reading the documentation for PIL (Image) >> >> Is there an easy way to know which formats are supported and what their >> names are? > > py> import PIL > py> from PIL import Image > py> Image.ID > [] > py> Image.init() > py> Image.ID > ['PNG', 'ARG', 'BMP', 'BUFR', 'CUR', 'PCX', 'DCX', 'EPS', 'FITS', 'FLI', > 'FPX', > 'GBR', 'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM', 'IMT', 'IPTC', > 'JPEG', 'MCIDA > S', 'TIFF', 'MIC', 'MPEG', 'MSP', 'PCD', 'PIXAR', 'PPM', 'PSD', 'SGI', > 'SPIDER', > 'SUN', 'TGA', 'WBMP', 'WMF', 'XBM', 'XPM', 'XVTHUMB'] > py> Image.OPEN.keys() > ['PCX', 'ICNS', 'HDF5', 'SUN', 'MIC', 'EPS', 'MSP', 'FLI', 'FITS', > 'GBR', 'WBMP' > , 'PCD', 'PIXAR', 'BUFR', 'PPM', 'WMF', 'SGI', 'BMP', 'TGA', 'DCX', > 'ICO', 'CUR' > , 'XPM', 'TIFF', 'JPEG', 'SPIDER', 'GIF', 'GRIB', 'IM', 'IMT', 'IPTC', > 'FPX', 'X > BM', 'MPEG', 'PSD', 'ARG', 'XVTHUMB', 'PNG', 'MCIDAS'] > py> Image.SAVE.keys() > ['XBM', 'PCX', 'SPIDER', 'HDF5', 'TIFF', 'BUFR', 'EPS', 'JPEG', 'MSP', > 'GRIB', ' > GIF', 'BMP', 'IM', 'PPM', 'PDF', 'FITS', 'PALM', 'WBMP', 'WMF', 'PNG'] > >> Is there an easy way to know which parameters are supported by >> Image.save(). How can I list them where are they documented? > > That depends on the format being used. The PIL handbook lists the > standard formats used and their parameters: > http://www.pythonware.com/library/pil/handbook/index.htm > >> I'm at a complete loss at finding out what parameters the save function >> accepts for saving a JPG file or a PNG file > > http://www.pythonware.com/library/pil/handbook/format-jpeg.htm > From mwawrzyczek at gmail.com Sat Jul 25 16:33:01 2009 From: mwawrzyczek at gmail.com (marekw2143) Date: Sat, 25 Jul 2009 13:33:01 -0700 (PDT) Subject: Adding method from one class to another class or to instance of another class References: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Message-ID: <953308c3-671f-4604-9562-d37767e600a4@g6g2000vbr.googlegroups.com> Thanks for your responses. im_func is all I need. I considered subclassing, wchih is more easy to extend, but I needed some quick way to add a method to another class. Regards, Marek From robert.kern at gmail.com Sat Jul 25 16:57:23 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 25 Jul 2009 15:57:23 -0500 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: On 2009-07-24 21:50, Dr. Phillip M. Feldman wrote: > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is single > float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. I have spent the last ten years writing scientific code in Python (i.e. that which otherwise might be written in Matlab), and I can guarantee you that you do not need to put such extra logic in practically every function. Even when naively translating code from Matlab, it's not often necessary. By the way, are you familiar with numpy? If you are converting code from Matlab, you will almost certainly need it. We have a number of functions that make these kinds of operations easy when they are in fact necessary. For example, we have isscalar() and atleast_1d(). def dumbfunc(xs): xs = numpy.atleast_1d(xs) for x in xs: print x http://numpy.scipy.org/ -- 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 Sat Jul 25 16:59:30 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 25 Jul 2009 15:59:30 -0500 Subject: len() should always return something In-Reply-To: <7cvs3fF24hjh7U1@mid.uni-berlin.de> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <7cvs3fF24hjh7U1@mid.uni-berlin.de> Message-ID: On 2009-07-25 02:55, Diez B. Roggisch wrote: > Dr. Phillip M. Feldman schrieb: >> Here's a simple-minded example: >> >> def dumbfunc(xs): >> for x in xs: >> print x >> >> This function works fine if xs is a list of floats, but not if it is >> single >> float. It can be made to work as follows: >> >> def dumbfunc(xs): >> if isinstance(xs,(int,float,complex)): xs= [xs] >> for x in xs: >> print x >> >> Having to put such extra logic into practically every function is one >> of the >> annoying things about Python. > > And where comes "len(xs)" into play here? What you want is iteration > over scalars. He explained in another post that iteration is another feature along the same lines that he would want for scalars. -- 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 lie.1296 at gmail.com Sat Jul 25 18:30:30 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 26 Jul 2009 08:30:30 +1000 Subject: Failed Regression Test: What socket.gethostname() is supposed to return? Message-ID: In my laptop, socket.gethostname() returned my username, and causing one of python's "make test" regression test to error (test_socket): ====================================================================== ERROR: testSockName (test.test_socket.GeneralModuleTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/var/tmp/portage/dev-lang/python-2.5.4-r3/work/Python-2.5.4/Lib/test/test_socket.py", line 456, in testSockName my_ip_addr = socket.gethostbyname(socket.gethostname()) gaierror: (-2, 'Name or service not known') ---------- since on my system socket.gethostname() returns 'lieryan', and since socket.gethostbyname('lieryan') does not resolve to anything; the test becomes an error. My system is Gentoo, but I think this also happened on Ubuntu (still on this laptop). The trunk failed in similar manner. Do I have a misconfigured system or is the test faulty? For convenience, the relevant test code (taken from trunk): ======================= def testSockName(self): # Testing getsockname() port = self._get_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(("0.0.0.0", port)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. # At least for eCos. This is required for the S/390 to pass. my_ip_addr = socket.gethostbyname(socket.gethostname()) self.assertTrue(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) self.assertEqual(name[1], port) ======================= lieryan at lieryan ~/Desktop/pythontrunk/trunk $ ./python -m test.regrtest test_socket Could not find '/home/lieryan/Desktop/pythontrunk/trunk/Lib/test' in sys.path to remove it test_socket test test_socket failed -- Traceback (most recent call last): File "/home/lieryan/Desktop/pythontrunk/trunk/Lib/test/test_socket.py", line 493, in testSockName my_ip_addr = socket.gethostbyname(socket.gethostname()) gaierror: [Errno -2] Name or service not known 1 test failed: test_socket I tracked the code for socket.gethostname() and socket.gethostbyname() and found that they are simply a wrapper for gethostname() from #import (http://linux.die.net/man/2/gethostname) and gethostbyname() from #import (http://linux.die.net/man/3/gethostbyname). A simple test in C found that the C's equivalent to gethostbyname(gethostname()) returns a null pointer (used to indicate error, per documentation). So, the question is: what is socket.gethostname() is supposed to return that will be a valid argument for socket.gethostbyname()? PS: I found an MSDN article by Microsoft stating that gethostbyname(gethostname) is guaranteed to always succeed (http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this guarantee also true in linux? From marcusw at cox.net Sat Jul 25 18:42:26 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 25 Jul 2009 18:42:26 -0400 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <027af2b3$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 7/25/2009 10:08 AM, Piet van Oostrum wrote: >>>>>> Steven D'Aprano (SD) wrote: > >> SD> Ambiguity essentially boils down to being unable to reasonably predict >> SD> the expectation of the coder. I say "reasonably", because if you allow >> SD> unreasonable situations, everything is "ambiguous": > > That's for me the reason that len(42) is ambiguous. The OP apparently > had 1 as expectation, whereas my first thought was the minimal number of > bits to represent the number and 7.5 million came later :=). The number > of bits I certainly find reasonable, and I would find the number of > decimal digits equally reasonable. More so than 1, actually. 1 as the > length of an int doesn't give any information. Well, for my two cents, I will say that the number of binary bits or decimal digits is certainly the most sensible return value, and that the former is the most useful, because the latter can be got with len(str(42)). However, the former can also be (/slightly/ less)easily got with len(bin(42))-2... I also think that "Explicit is better than implicit." says that there should be no return value in this case, as any return value would be implicit. Marcus From ben+python at benfinney.id.au Sat Jul 25 19:10:40 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 26 Jul 2009 09:10:40 +1000 Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <877hxw4avj.fsf@benfinney.id.au> Michal Kwiatkowski writes: > I may be missing something obvious here. Is there a better way to tell > if a given generator object is still active or not? foo = the_generator_object try: do_interesting_thing_that_needs(foo.next()) except StopIteration: generator_is_exhausted() In other words, don't LBYL, because it's EAFP. Whatever you need to do that requires the next item from the generator, do that; you'll get a specific exception if the generator is exhausted. -- \ ?Courteous and efficient self-service.? ?caf?, southern France | `\ | _o__) | Ben Finney From max at alcyone.com Sat Jul 25 19:21:39 2009 From: max at alcyone.com (Erik Max Francis) Date: Sat, 25 Jul 2009 16:21:39 -0700 Subject: len() should always return something In-Reply-To: <027a9f1a$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> Message-ID: <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Steven D'Aprano wrote: > But it's not "practically every function". It's hardly any function at > all -- in my code, I don't think I've ever wanted this behavior. I would > consider it an error for function(42) and function([42]) to behave the > same way. One is a scalar, and the other is a vector -- they're different > things, it's poor programming practice to treat them identically. > > (If Matlab does this, so much the worse for Matlab, in my opinion.) There's actually good reason to do this in heavily matrix-oriented specialized languages; there are numerous applications where scalars and 1x1 matrices are mathematically equivalent. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Gods are born and die, but the atom endures. -- Alexander Chase From clp2 at rebertia.com Sat Jul 25 19:30:44 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 25 Jul 2009 16:30:44 -0700 Subject: len() should always return something In-Reply-To: <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: <50697b2c0907251630s6e4f7e0en6876b3609eb4fa5@mail.gmail.com> On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: > Steven D'Aprano wrote: >> >> But it's not "practically every function". It's hardly any function at all >> -- in my code, I don't think I've ever wanted this behavior. I would >> consider it an error for function(42) and function([42]) to behave the same >> way. One is a scalar, and the other is a vector -- they're different things, >> it's poor programming practice to treat them identically. >> >> (If Matlab does this, so much the worse for Matlab, in my opinion.) > > There's actually good reason to do this in heavily matrix-oriented > specialized languages; there are numerous applications where scalars and 1x1 > matrices are mathematically equivalent. The pertinent issue here being that Python, as a language, is neither matrix-oriented nor special-purpose. :) Cheers, Chris -- http://blog.rebertia.com From davea at ieee.org Sat Jul 25 20:35:05 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 25 Jul 2009 20:35:05 -0400 Subject: strange python scripting error In-Reply-To: References: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> <4A6AEA22.1030207@ieee.org> Message-ID: <4A6BA4B9.5090800@ieee.org> Dennis Lee Bieber wrote: > On Sat, 25 Jul 2009 07:18:58 -0400, Dave Angel > declaimed the following in gmane.comp.python.general: > > >> Another thing I'd point out is that some ftp programs will do this >> conversion as the file is being sent between a local DOS machine and a >> Unix machine on the internet. >> >> > "some ftp programs"? > > Line end conversion is the whole reason, to my knowledge, for text > mode transfer, vs binary mode transfer. > > > > Perhaps I should have said "most ftp programs," or even "all ftp programs," but I don't feel qualified to make such a generalization. In any case, I don't currently use such automatic conversion when I'm doing transfers because I like to be able to see exactly what's there, and I don't have another way to directly examine the remote end. The FTP I use is an add-on for Firefox (FireFTP), and although it has text and "auto" modes, it's buried in the Tools->options menu, and the main panel just does a transfer using whichever mode was selected. Rather than risking corrupting the (mostly binary) files, I choose to use binary mode all the time. DaveA From scriptlearner at gmail.com Sat Jul 25 21:03:37 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Sat, 25 Jul 2009 18:03:37 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> Message-ID: <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> I decided to go with one big log file, which will be shared by all threads (child and parent). A log message Queue is used to store all log entries, and a customized logger thread will get log entries from the Queue. #from the logger thread# def run(self): while self.flag == 1: #if the flag is set to 0, the logger thread should exit try: entry = self.q.get() except Empty: self.logger.debug('cant find any log entry') continue except: self.logger.error("Unexpected error:", sys.exc_info() [0]) raise #do whatever that should be done self.logger.info("logger thread done") #should see this message in log file as well def off(self): self.logger.info('turning off flag') self.flag = 0 #in parent thread# logItemQ.put('We are done, lets stop the logger now.') time.sleep(1) #it seems that the logger thread cannot exit if I put a sleep here myLog.off() #off is called successfully myLog.join() I put an off method to turn off a flag so the logger thread knows it should exit. However, the last log message (the one 'We are done, lets stop the logger now.') won't be logged if I call myLog.off() and myLog.join() immediately. So, I put a time.sleep(1) to make sure the logger thread has enough time to finish it's job. Unfortunately, now the logger thread simply won't exit, and I don't see the message 'logger thread done'. I can't figure out at which point it hangs, since I don't any new log entry but the thread simply won't exit. Am I taking a right approach by using a flag? Should I lock the flag? From python at mrabarnett.plus.com Sat Jul 25 21:30:00 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 26 Jul 2009 02:30:00 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> Message-ID: <4A6BB198.4040209@mrabarnett.plus.com> scriptlearner at gmail.com wrote: > I decided to go with one big log file, which will be shared by all > threads (child and parent). A log message Queue is used to store all > log entries, and a customized logger thread will get log entries from > the Queue. > > #from the logger thread# > def run(self): > while self.flag == 1: #if the flag is set to 0, the logger > thread should exit > try: > entry = self.q.get() > except Empty: > self.logger.debug('cant find any log entry') > continue > except: > self.logger.error("Unexpected error:", sys.exc_info() > [0]) > raise > #do whatever that should be done > self.logger.info("logger thread done") #should see this > message in log file as well > def off(self): > self.logger.info('turning off flag') > self.flag = 0 > > > #in parent thread# > logItemQ.put('We are done, lets stop the logger now.') > time.sleep(1) #it seems that the logger thread cannot exit if > I put a sleep here > myLog.off() #off is called successfully > myLog.join() > > > I put an off method to turn off a flag so the logger thread knows it > should exit. However, the last log message (the one 'We are done, > lets stop the logger now.') won't be logged if I call myLog.off() and > myLog.join() immediately. So, I put a time.sleep(1) to make sure the > logger thread has enough time to finish it's job. Unfortunately, now > the logger thread simply won't exit, and I don't see the message > 'logger thread done'. I can't figure out at which point it hangs, > since I don't any new log entry but the thread simply won't exit. > Am I taking a right approach by using a flag? Should I lock the flag? self.q.get() will block if the queue is empty, so the Empty exception is never raised. What's happening is that the parent thread puts the final message into the queue, sleeps, and then clears the flag; meanwhile, the logging thread gets the message, writes it out, checks the flag, which is still set, and then tries to get the next message. The queue is empty, so the .get() blocks. The simplest solution is not to use a flag, but the sentinel trick. The parent thread can put, say, None into the queue after the last message; when the logging thread gets None, it knows it should terminate. From max at alcyone.com Sat Jul 25 21:47:07 2009 From: max at alcyone.com (Erik Max Francis) Date: Sat, 25 Jul 2009 18:47:07 -0700 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: Chris Rebert wrote: > On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: >> Steven D'Aprano wrote: >>> But it's not "practically every function". It's hardly any function at all >>> -- in my code, I don't think I've ever wanted this behavior. I would >>> consider it an error for function(42) and function([42]) to behave the same >>> way. One is a scalar, and the other is a vector -- they're different things, >>> it's poor programming practice to treat them identically. >>> >>> (If Matlab does this, so much the worse for Matlab, in my opinion.) >> There's actually good reason to do this in heavily matrix-oriented >> specialized languages; there are numerous applications where scalars and 1x1 >> matrices are mathematically equivalent. > > The pertinent issue here being that Python, as a language, is neither > matrix-oriented nor special-purpose. :) Yes. And I was responding to the comment that such a feature of a language would a priori be poor design. It _isn't_ poor design for special purpose languages. Python isn't one of them, but Matlab _is_. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis More fodder for the new lost generation -- Nik Kershaw From clp2 at rebertia.com Sat Jul 25 22:39:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 25 Jul 2009 19:39:13 -0700 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: <50697b2c0907251939u4c159290qf700640721e88e3a@mail.gmail.com> On Sat, Jul 25, 2009 at 6:47 PM, Erik Max Francis wrote: > Chris Rebert wrote: >> >> On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: >>> >>> Steven D'Aprano wrote: >>>> >>>> But it's not "practically every function". It's hardly any function at >>>> all >>>> -- in my code, I don't think I've ever wanted this behavior. I would >>>> consider it an error for function(42) and function([42]) to behave the >>>> same >>>> way. One is a scalar, and the other is a vector -- they're different >>>> things, >>>> it's poor programming practice to treat them identically. >>>> >>>> (If Matlab does this, so much the worse for Matlab, in my opinion.) >>> >>> There's actually good reason to do this in heavily matrix-oriented >>> specialized languages; there are numerous applications where scalars and >>> 1x1 >>> matrices are mathematically equivalent. >> >> The pertinent issue here being that Python, as a language, is neither >> matrix-oriented nor special-purpose. :) > > Yes. ?And I was responding to the comment that such a feature of a language > would a priori be poor design. ?It _isn't_ poor design for special purpose > languages. ?Python isn't one of them, but Matlab _is_. I was agreeing with your point actually. That was what I was trying to convey in my post. Apparently I wasn't as successful in that regard as I'd hoped. - Chris -- http://blog.rebertia.com From martin at v.loewis.de Sun Jul 26 00:07:21 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 06:07:21 +0200 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: References: Message-ID: <4a6bd67a$0$7423$9b622d9e@news.freenet.de> > I am very confused about unicode. Can someone point me in the right > direction? Try Python 3.1. This should accept Unicode strings directly for sp.call, so you wouldn't need to encode first. Regards, Martin From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Sun Jul 26 00:56:59 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Sun, 26 Jul 2009 00:56:59 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-25 00:55:26 -0400, Carl Banks said: > But please don't put it on the same level as PHP. Their situations > have almost nothing in common. Their situations have much in common; Python attracted programmers away from (for example) C++, becuse python is easier to master; Then php came along and attracted programmers away from (for example) python, because php is easier to master. This doesn't mean they're on the same level - in fact, if you read carefully you'll see my original post said as much: python attracted average programmers; php attracted mediocre programmers and even some non-programmers, which means that php is clearly a lesser language than python. -- Raffael Cavallaro From nagle at animats.com Sun Jul 26 01:47:37 2009 From: nagle at animats.com (John Nagle) Date: Sat, 25 Jul 2009 22:47:37 -0700 Subject: MySQLdb for Python 3.1 getting close? In-Reply-To: <4a67e912$0$1603$742ec2ed@news.sonic.net> References: <4a67e912$0$1603$742ec2ed@news.sonic.net> Message-ID: <4a6bed48$0$1648$742ec2ed@news.sonic.net> (This is actually a repost; outgoing netnews server was down for a while.) John Nagle wrote: > Is MySQLdb available for Python 3.1 yet? > > http://sourceforge.net/projects/mysql-python/ > > says the last supported Python version is 2.5. Any progress in sight? > > John Nagle From nagle at animats.com Sun Jul 26 01:52:18 2009 From: nagle at animats.com (John Nagle) Date: Sat, 25 Jul 2009 22:52:18 -0700 Subject: non-owning references? In-Reply-To: References: Message-ID: <4a6bee61$0$1648$742ec2ed@news.sonic.net> Peter Otten wrote: > Utpal Sarkar wrote: > >> Is there a way I can tell a variable that the object it is pointing >> too is not owned by it, in the sense that if it is the only reference >> to the object it can be garbage collected? > > http://docs.python.org/library/weakref.html Yes. Weak references can be quite useful. I have a version of BeautifulSoup in which all the "upward" and "backwards" links are weak references, but the "downward" and "forwards" links are strong references. This breaks the loops in the data structure, and thus trees and subtrees will go away when no longer required, without requiring a GC cycle. John Nagle From israel.unterman at gmail.com Sun Jul 26 01:52:36 2009 From: israel.unterman at gmail.com (is un) Date: Sat, 25 Jul 2009 22:52:36 -0700 (PDT) Subject: multiprocessing and __main__ Message-ID: Hi, Trying the multiprocessing module for the first time, I spent quite a bit on making this code run: from multiprocessing import Process import time def my_process(): i = 0 while 1: print i i += 1 time.sleep(0.5) p = Process(target=my_process, args=()) p.start() print 'Process started' The result was not what I expected, it seems like the process restarts all the time, and the message 'Process started' keeps getting printed... Going back to the documentation, I realized that the doc was really serious about the line: if __name__ == '__main__' .. which I always ignore, and by changing the code to if __name__ == '__main__': p = Process(target=my_process, args=()) p.start() print 'Process started' the problem was solved. So my question is what actually happens when I call p.start()? Is the entry file reloaded under a different module name? Thanks iu2 From israel.unterman at gmail.com Sun Jul 26 01:55:24 2009 From: israel.unterman at gmail.com (is un) Date: Sat, 25 Jul 2009 22:55:24 -0700 (PDT) Subject: multiprocessing and __main__ References: Message-ID: <88e64585-e719-4a1a-ade9-7d54b3884503@r2g2000yqm.googlegroups.com> Me again, fix a type in my question: So my question is what actually happens when I call p.start()? Is the *entire* file reloaded under a different module name? > > Thanks > iu2 From bhardwajjayesh7 at gmail.com Sun Jul 26 02:23:11 2009 From: bhardwajjayesh7 at gmail.com (golu) Date: Sat, 25 Jul 2009 23:23:11 -0700 (PDT) Subject: code debugging Message-ID: here is a code which crawls links sent to it. theres some problem with the retrieve_url function ,plz help me out in debugging the fuction retrive_url. This function retrives pages and saves them in file #TODO:Visited dict grows in size it needs to be handled smartly #Moreover server program needs to be in sync with the client eg. Myrobot #Take care of tag - 'if modified since',repeated links,hash links #This is the client side of the distributed crawling framework #It gets the list of urls to be crawled #Then crawls the urls and stores the pages in a temporary archive #which is then transferred to the server or grey_matter import httplib import os import sys import urlparse import urllib2 import urllib import zipfile import threading from socket import * PAGE_DIR="C:/users/jayesh/ pages/" # directory where the web pages are stored temporarily # before transfer to the grey_matter visited= {} # a dict to remember visited urls ROBOT_COUNT=4 def fget(): """ This function retrieves the zipped file containing the list of urls from the grey_matter and saves them in a local file 'list.txt'. """ httplib.HTTPConnection.debuglevel=1 request=urllib2.Request('http://192.168.153.57/list.zip') #Requesting the zipped file request.add_header('Accept-encoding','gzip') #containing the list of urls opener=urllib2.build_opener() flag=1 s='Waiting for server' while flag==1: try: op=opener.open(request) flag=0 except: s=s+'*' print s f=open('list.zip',"wb") f.write(op.read()) f.close() z=zipfile.ZipFile('list.zip') p=z.namelist() g=open('list.txt',"wb") g.write(z.read(p[0])) g.close() print 'got zipped file' def compress(): """ This function compresses the crawled pages and stores them in a single compressed file ready to be sent to the grey_matter.""" zfile=zipfile.ZipFile('C:/xampp/htdocs/pages.zip',mode='w') for fil in os.listdir(PAGE_DIR): full=os.path.join(PAGE_DIR,fil) zfile.write(full,fil) os.remove(full) os.rmdir(PAGE_DIR) #Removing the directory after transfer to grey_matter x=0 class robot(threading.Thread): """ The main robot class which does the crawling of listed urls it recieves from the grey matter. It uses 3 threads which crawl the listed urls synchronously.""" def __init__(self,urllist,urllistlock,dblock): threading.Thread.__init__(self) self.urllist=urllist self.urllistlock=urllistlock self.dblock=dblock def popurl(self): """ This method pops out urls from the urls file one by one and sends them for retrieval.""" self.urllistlock.acquire(1) if(len(self.urllist)<1): Nexturl=None else: Nexturl=self.urllist[0] if Nexturl[-1]=='\n':Nexturl=Nexturl[:-1] del self.urllist[0] self.urllistlock.release() return Nexturl def retrieve_url(self,url): """ The main method of the robot class and is called run method to retrieve the given urls from the web.""" global x if url is not None: try: if visited.has_key(url): return pieces=urlparse.urlparse(url) filepath=pieces[2] if filepath != '': filepath=filepath[1:] filename=filepath.split("/")[-1] else: filename=x+'.htm' x+=1 path=os.path.join(PAGE_DIR,filename) url=urlparse.urlunparse(pieces) p=url.rfind('#') #temporary if p!=-1: url=url[:p] visited[url]=1 m=urllib2.urlopen(url) fopen=open(path,'wb') fopen.seek(0) fopen.write(url+'|') fopen.write(m.read()) fopen.close() print url ,'retrieved' except IOError: print url print "ERROR:OOPS! THE URL CAN'T BE RETRIEVED" return def run(self): while(1): url=self.popurl() if url is None: break try: self.retrieve_url(url) except:sys.exit() if __name__=='__main__': s=socket(AF_INET,SOCK_STREAM) s.bind(('',444)) s.listen(5) q,v=s.accept() count=1 print 'Connecting...' while 1: print 'Phase: %s' %(count) message=q.recv(3) if(message!='yes'):continue print 'Connected' count=count+1 fget() # Calling the fget method to get the url list from # grey_matter(server). try: os.mkdir(PAGE_DIR) except: print 'Cant make dir' try: f=open('list.txt','r') urllist=f.readlines() f.close() except: print 'Error opening urls file' sys.exit() print 'startting threads' urllistlock=threading.Lock() dblock=threading.Lock() botlist=[] for X in range(0,ROBOT_COUNT): newbot=robot(urllist,urllistlock,dblock) newbot.setName('X') botlist.append(newbot) newbot.start() for X in range(0,ROBOT_COUNT): botlist[X].join() compress() try: q.send('yes') except: print 'socket disconnected' print sys.exit() From clp2 at rebertia.com Sun Jul 26 02:28:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 25 Jul 2009 23:28:48 -0700 Subject: code debugging In-Reply-To: References: Message-ID: <50697b2c0907252328t70ecd0ddp20898ddf497b5190@mail.gmail.com> On Sat, Jul 25, 2009 at 11:23 PM, golu wrote: > here is a code which crawls links sent to it. theres some problem with > the retrieve_url function ,plz help me out in debugging the fuction > retrive_url. This function retrives pages and saves them in file Please specify exactly what the problem is that you are experiencing. If you are getting an error, please provide the error message and full traceback. Cheers, Chris -- http://blog.rebertia.com From bhardwajjayesh7 at gmail.com Sun Jul 26 02:33:27 2009 From: bhardwajjayesh7 at gmail.com (golu) Date: Sat, 25 Jul 2009 23:33:27 -0700 (PDT) Subject: code debugging References: Message-ID: On Jul 26, 11:28?am, Chris Rebert wrote: > On Sat, Jul 25, 2009 at 11:23 PM, golu wrote: > > here is a code which crawls links sent to it. theres some problem with > > the retrieve_url function ,plz help me out in debugging the fuction > > retrive_url. This function retrives pages and saves them in file > > Please specify exactly what the problem is that you are experiencing. > If you are getting an error, please provide the error message and full > traceback. > > Cheers, > Chris > --http://blog.rebertia.com i want to save pages in a directory and i m using the urls to get filenames. The program gets stuck in the saving step.can u suggest me a way to save a page e.g google.com as a file google.html From bhardwajjayesh7 at gmail.com Sun Jul 26 02:57:35 2009 From: bhardwajjayesh7 at gmail.com (golu) Date: Sat, 25 Jul 2009 23:57:35 -0700 (PDT) Subject: web page retrieve problems Message-ID: <16343e3d-1be4-422b-8bff-c07fa4aa4c54@j9g2000prh.googlegroups.com> the following function retrieves pages from the web and saves them in a specified dir. i want to extract the respective filenames from the urls e.g the page code.google.com shud be saved as code-google.htm or something similar. can u suggest me a way to do it def retrieve_url(self,url): """ The main method of the robot class and is called run method to retrieve the given urls from the web.""" if url is not None: try: if visited.has_key(url): return pieces=urlparse.urlparse(url) filepath=pieces[2] if filepath != '': filepath=filepath[1:] filename=filepath.split("/")[-1] else: filename='home.htm' path=os.path.join(PAGE_DIR,filename) url=urlparse.urlunparse(pieces) p=url.rfind('#') #temporary if p!=-1: url=url[:p] visited[url]=1 m=urllib2.urlopen(url) fopen=open(path,'wb') fopen.seek(0) fopen.write(url+'|') fopen.write(m.read()) fopen.close() print url ,'retrieved' except IOError: print url print "ERROR:OOPS! THE URL CAN'T BE RETRIEVED" return From emmanuel.surleau at gmail.com Sun Jul 26 03:42:33 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sun, 26 Jul 2009 09:42:33 +0200 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> Message-ID: <200907260942.33841.emmanuel.surleau@gmail.com> On Sunday 26 July 2009 00:42:26 Marcus Wanner wrote: > On 7/25/2009 10:08 AM, Piet van Oostrum wrote: > >>>>>> Steven D'Aprano (SD) wrote: > >> > >> SD> Ambiguity essentially boils down to being unable to reasonably > >> predict SD> the expectation of the coder. I say "reasonably", because if > >> you allow SD> unreasonable situations, everything is "ambiguous": > > > > That's for me the reason that len(42) is ambiguous. The OP apparently > > had 1 as expectation, whereas my first thought was the minimal number of > > bits to represent the number and 7.5 million came later :=). The number > > of bits I certainly find reasonable, and I would find the number of > > decimal digits equally reasonable. More so than 1, actually. 1 as the > > length of an int doesn't give any information. > > Well, for my two cents, I will say that the number of binary bits or > decimal digits is certainly the most sensible return value, and that the > former is the most useful, because the latter can be got with > len(str(42)). However, the former can also be (/slightly/ less)easily > got with len(bin(42))-2... For my two eurocents, I'd expect such an operator to be called "sizeof" and when applied to a collection, to also return the number of bits it uses. I don't think a 'len' function has meaning when applied to something else than a collection, really... > I also think that "Explicit is better than implicit." says that there > should be no return value in this case, as any return value would be > implicit. Agreed. Cheers, Emm From hendrik at microcorp.co.za Sun Jul 26 03:48:49 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 26 Jul 2009 09:48:49 +0200 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <200907260948.49293.hendrik@microcorp.co.za> On Saturday 25 July 2009 20:30:54 Michal Kwiatkowski wrote: > Hi, > > Is there a way to tell if a generator has been exhausted using pure > Python code? I've looked at CPython sources and it seems that > something like "active"/"exhausted" attribute on genobject is missing > from the API. For the time being I am using a simple C extension to > look at f_stacktop pointer of the generator frame, which seems to > differentiate active generators from exhausted ones. See > http://bazaar.launchpad.net/~ruby/pythoscope/support-python2.3/annotate/286 >/pythoscope/_util.c#L16 for complete source code. > > I may be missing something obvious here. Is there a better way to tell > if a given generator object is still active or not? Is there a reason why you cannot just call the next method and handle the StopIteration when it happens? - Hendrik From emmanuel.surleau at gmail.com Sun Jul 26 03:50:42 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sun, 26 Jul 2009 09:50:42 +0200 Subject: non-owning references? In-Reply-To: <8763di5d1d.fsf@benfinney.id.au> References: <87fxcm5gny.fsf@busola.homelinux.net> <8763di5d1d.fsf@benfinney.id.au> Message-ID: <200907260950.42765.emmanuel.surleau@gmail.com> On Friday 24 July 2009 17:14:06 you wrote: > Hrvoje Niksic writes: > > The term "variable" is used in the Python language reference and > > elsewhere > > Yes. It should also be abundantly clear from the constant stream of > confused newbies on this point that its usage of that term is different > to what many expect from usage elsewhere. > > > and is quite compatible with how other popular languages (Java, PHP, > > Lisp, ...) use it. Please stop complaining about valid terminology; it > > is not helpful. > > I disagree with your assertions. > > Rather than yet another round of this tedious debate, I merely point > interested readers to > and ask them to draw their own conclusion on how compatible their > pre-existing ?variable? concept is with Python's object reference > model. Then I must be one of these "confused newbies". After a cursory look at your article, I don't see a difference with Java variables (as long as we speak about non-primitive types). Cheers, Emm From martin at v.loewis.de Sun Jul 26 04:04:12 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 10:04:12 +0200 Subject: C-API, tp_dictoffset vs tp_members In-Reply-To: <7cl194F28ojoqU1@mid.uni-berlin.de> References: <7cl194F28ojoqU1@mid.uni-berlin.de> Message-ID: <4A6C0DFC.4030702@v.loewis.de> > When would I use PyObject_SetAttrString/tp_dictoffset instead of tp_members? When I have a variable list of attributes, and cannot statically know what those attributes might be. > I have a predefined set of members, some of which are optional. Having optional fields is also a good reason. > The problem > I had with an embedded dictionary was that I can't see its elements using > "dir()". How so? That should work fine. > Now I just converted to using tp_members, and it still seems to > work correctly for the cases I tested. So how do you do optional fields now? In particular, how would you do optional integers? > Even better, I can declare the fields > as read-only then and add doc-strings. So, I wonder, what made the original > author[2] use tp_dictoffset instead? Most likely, it was the simplest approach. This code only wants to set the attributes, and never read them. It's easier to maintain: if you want to add a field with tp_members, you have to change multiple places, and you have to consider garbage collection (assuming you have embedded objects). With tp_dictoffset, adding another attribute is easy. Regards, Martin From jessica.1980.smith at gmail.com Sun Jul 26 04:12:47 2009 From: jessica.1980.smith at gmail.com (Jessica R Smith) Date: Sun, 26 Jul 2009 01:12:47 -0700 (PDT) Subject: I am trying to compile python 2.6.2 on my Mac Message-ID: Hello, I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 I downloaded python 2.6.2 from here: - http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 I unpacked it. I ran these shell commands: - ./configure --prefix=/pt/p - make Near the end of the make output I see this message: . Failed to find the necessary bits to build these modules: . _bsddb gdbm linuxaudiodev . ossaudiodev readline spwd . sunaudiodev . To find the necessary bits, look in setup.py in detect_modules() for the module's name. I looked in setup.py in detect_modules() It is not clear to me how to proceed. I think that I want the module: "readline" I doubt I need the other modules like linuxaudiodev, etc. If you have any clues or opinions on how I can build the module "readline", please feel free to share. Thanks, JRS From nobody at nowhere.com Sun Jul 26 04:13:30 2009 From: nobody at nowhere.com (Nobody) Date: Sun, 26 Jul 2009 09:13:30 +0100 Subject: Failed Regression Test: What socket.gethostname() is supposed to return? References: Message-ID: On Sun, 26 Jul 2009 08:30:30 +1000, Lie Ryan wrote: > since on my system socket.gethostname() returns 'lieryan', and since > socket.gethostbyname('lieryan') does not resolve to anything; the test > becomes an error. > > My system is Gentoo, but I think this also happened on Ubuntu (still on > this laptop). The trunk failed in similar manner. > > Do I have a misconfigured system or is the test faulty? Misconfigured system. > I tracked the code for socket.gethostname() and socket.gethostbyname() > and found that they are simply a wrapper for gethostname() from #import > (http://linux.die.net/man/2/gethostname) and gethostbyname() > from #import (http://linux.die.net/man/3/gethostbyname). A > simple test in C found that the C's equivalent to > gethostbyname(gethostname()) returns a null pointer (used to indicate > error, per documentation). > > So, the question is: what is socket.gethostname() is supposed to return > that will be a valid argument for socket.gethostbyname()? gethostname() returns the hostname. gethostbyname() returns some information about the host (specifically, aliases and IP addresses) obtained by platform-specific means (which normally includes /etc/hosts and DNS, and may also include NIS, LDAP, and others; see the nsswitch.conf manpage for more details). Python's socket.gethostname() simply returns the first IP address; socket.gethostbyname_ex() returns all of the information which libc's gethostbyname() provides. In order for gethostbyname() to work, the host must be listed in one of the databases which it uses. The simplest way to achieve this is to add an entry to /etc/hosts, e.g.: 192.168.0.2 lieryan.yourdomain.com lieryan or just: 192.168.0.2 lieryan > PS: I found an MSDN article by Microsoft stating that > gethostbyname(gethostname) is guaranteed to always succeed > (http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this > guarantee also true in linux? No. From gagsl-py2 at yahoo.com.ar Sun Jul 26 04:14:34 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 26 Jul 2009 05:14:34 -0300 Subject: multiprocessing and __main__ References: Message-ID: En Sun, 26 Jul 2009 02:52:36 -0300, is un escribi?: > Trying the multiprocessing module for the first time, I spent quite a > bit on making this code run: > [...] > The result was not what I expected, it seems like the process restarts > all the time, and the message 'Process started' keeps getting > printed... > > Going back to the documentation, I realized that the doc was really > serious about the line: > if __name__ == '__main__' .. which I always ignore, and by changing > the code to > > if __name__ == '__main__': > p = Process(target=my_process, args=()) > p.start() > > print 'Process started' > > the problem was solved. > So my question is what actually happens when I call p.start()? Is the > entry file reloaded under a different module name? The multiprocessing module handles multiple (separate) processes. On Windows, a new Python interpreter is launched; on other OSs a different approach is used. In any case, you get distinct and separate processes, each one with its own set of loaded modules, etc. -- Gabriel Genellina From clp2 at rebertia.com Sun Jul 26 04:37:43 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 26 Jul 2009 01:37:43 -0700 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: References: Message-ID: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> On Sun, Jul 26, 2009 at 1:12 AM, Jessica R Smith wrote: > Hello, > I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 > > I downloaded python 2.6.2 from here: > ?- http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 > > I unpacked it. > > I ran these shell commands: > ?- ./configure --prefix=/pt/p > ?- make > > Near the end of the make output I see this message: > > . Failed to find the necessary bits to build these modules: > . _bsddb ? ? ? ? ? ? gdbm ? ? ? ? ? ? ? linuxaudiodev > . ossaudiodev ? ? ? ?readline ? ? ? ? ? spwd > . sunaudiodev > . To find the necessary bits, look in setup.py in detect_modules() for > the module's name. > > I looked in setup.py in detect_modules() > > It is not clear to me how to proceed. > > I think that I want the module: "readline" > > I doubt I need the other modules like linuxaudiodev, etc. > > If you have any clues or opinions on how I can build the module > "readline", > please feel free to share. You need to install the GNU Readline library (http://tiswww.case.edu/php/chet/readline/rltop.html), which the module depends on. You might consider installing Python through MacPorts or Fink instead, as they automate the compilation and installation process and take care of dependencies (such as GNU Readline) for you: http://www.macports.org/ http://www.finkproject.org/ Cheers, Chris -- http://blog.rebertia.com From constant.beta at gmail.com Sun Jul 26 04:45:21 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Sun, 26 Jul 2009 01:45:21 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> Message-ID: <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> On Jul 26, 1:10?am, Ben Finney wrote: > Michal Kwiatkowski writes: > > I may be missing something obvious here. Is there a better way to tell > > if a given generator object is still active or not? > > ? ? foo = the_generator_object > ? ? try: > ? ? ? ? do_interesting_thing_that_needs(foo.next()) > ? ? except StopIteration: > ? ? ? ? generator_is_exhausted() > > In other words, don't LBYL, because it's EAFP. Whatever you need to do > that requires the next item from the generator, do that; you'll get a > specific exception if the generator is exhausted. The thing is I don't need the next item. I need to know if the generator has stopped without invoking it. Why - you may ask. Well, the answer needs some explaining. I'm working on the Pythoscope project (http://pythoscope.org) and I use tracing mechanisms of CPython (sys.settrace) to capture function calls and values passed to and from them. Now, the problem with generators is that when they are ending (i.e. returning instead of yielding) they return a None, which is in fact indistinguishable from "yield None". That means I can't tell if the last captured None was in fact yielded or is a bogus value which should be rejected. Let me show you on an example. import sys def trace(frame, event, arg): if event != 'line': print frame, event, arg return trace def gen1(): yield 1 yield None def gen2(): yield 1 sys.settrace(trace) print "gen1" g1 = gen1() g1.next() g1.next() print "gen2" g2 = gen2() [x for x in g2] sys.settrace(None) The first generator isn't finished, it yielded 1 and None. Second one is exhausted after yielding a single value (1). The problem is that, under Python 2.4 or 2.3 both invocations will generate the same trace output. So, to know whether the last None was actually a yielded value I need to know if a generator is active or not. Your solution, while gives me an answer, is not acceptable because generators can cause side effects (imagine call to launch_rockets() before the next yield statement ;). Cheers, mk From jessica.1980.smith at gmail.com Sun Jul 26 05:01:31 2009 From: jessica.1980.smith at gmail.com (Jessica Smith) Date: Sun, 26 Jul 2009 02:01:31 -0700 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> References: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> Message-ID: Chris thanks!! other members: I read this: - http://chrismiles.livejournal.com/25648.html I did this: - install mac ports - port install readline - vi setup.py def detect_modules(self): # Ensure that /usr/local is always used add_dir_to_list(self.compiler.library_dirs, '/opt/local/lib') add_dir_to_list(self.compiler.include_dirs, '/opt/local/include') - configure --prefix=/pt/p - make - make install I'm happy now! JRS On 7/26/09, Chris Rebert wrote: > On Sun, Jul 26, 2009 at 1:12 AM, Jessica R > Smith wrote: >> Hello, >> I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 >> >> I downloaded python 2.6.2 from here: >> ?- http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 >> >> I unpacked it. >> >> I ran these shell commands: >> ?- ./configure --prefix=/pt/p >> ?- make >> >> Near the end of the make output I see this message: >> >> . Failed to find the necessary bits to build these modules: >> . _bsddb ? ? ? ? ? ? gdbm ? ? ? ? ? ? ? linuxaudiodev >> . ossaudiodev ? ? ? ?readline ? ? ? ? ? spwd >> . sunaudiodev >> . To find the necessary bits, look in setup.py in detect_modules() for >> the module's name. >> >> I looked in setup.py in detect_modules() >> >> It is not clear to me how to proceed. >> >> I think that I want the module: "readline" >> >> I doubt I need the other modules like linuxaudiodev, etc. >> >> If you have any clues or opinions on how I can build the module >> "readline", >> please feel free to share. > > You need to install the GNU Readline library > (http://tiswww.case.edu/php/chet/readline/rltop.html), which the > module depends on. > > You might consider installing Python through MacPorts or Fink instead, > as they automate the compilation and installation process and take > care of dependencies (such as GNU Readline) for you: > http://www.macports.org/ > http://www.finkproject.org/ > > Cheers, > Chris > -- > http://blog.rebertia.com > From __peter__ at web.de Sun Jul 26 05:08:10 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 26 Jul 2009 11:08:10 +0200 Subject: strange error when trying to log something References: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Message-ID: Aahz wrote: > In article , > Peter Otten <__peter__ at web.de> wrote: >> >>I have a hunch that you are triggering a reload() somewhere. Example: >> >>Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) >>[GCC 4.3.3] on linux2 >>Type "help", "copyright", "credits" or "license" for more information. >>>>> import weakref >>>>> weakref.WeakValueDictionary() >> >>>>> import UserDict >>>>> reload(UserDict) >> >>>>> weakref.WeakValueDictionary() >>Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib/python2.6/weakref.py", line 51, in __init__ >> UserDict.UserDict.__init__(self, *args, **kw) >>TypeError: unbound method __init__() must be called with UserDict instance >>as first argument (got WeakValueDictionary instance instead) > > Nice sleuthing! How did you figure that out? Roughly: - The poster's description/remedy makes no sense (to me); have a look at the traceback. - Nothing suspicious except the actual error message. Isn't WeakValueDictionary a UserDict? Verify. - There must be two different UserDict classes. This can happen to an unsuspecting user by importing the main script or by doing a reload(). UserDict is unlikely to be the main script, and web frameworks (django was mentioned) often use a reload mechanism to avoid frequent server restarts. - Reproduce the error. Peter From gagsl-py2 at yahoo.com.ar Sun Jul 26 05:19:52 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 26 Jul 2009 06:19:52 -0300 Subject: code debugging References: Message-ID: En Sun, 26 Jul 2009 03:33:27 -0300, golu escribi?: > i want to save pages in a directory and i m using the urls to get > filenames. The program gets stuck in the saving step.can u suggest me > a way to save a page e.g google.com as a file google.html You may use str.translate to replace/remove all undesired characters: py> import string py> valid = string.ascii_letters+string.digits+'.' py> invalid = ''.join(chr(x) for x in range(256) if chr(x) not in valid) py> table = string.maketrans(invalid, '_'*len(invalid)) py> x = 'http://docs.python.org/library/string.html' py> x.translate(table) 'http___docs.python.org_library_string.html' See http://docs.python.org/library/stdtypes.html#str.translate -- Gabriel Genellina From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 05:45:03 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 09:45:03 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: <027c168c$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 16:21:39 -0700, Erik Max Francis wrote: > Steven D'Aprano wrote: >> But it's not "practically every function". It's hardly any function at >> all -- in my code, I don't think I've ever wanted this behavior. I >> would consider it an error for function(42) and function([42]) to >> behave the same way. One is a scalar, and the other is a vector -- >> they're different things, it's poor programming practice to treat them >> identically. >> >> (If Matlab does this, so much the worse for Matlab, in my opinion.) > > There's actually good reason to do this in heavily matrix-oriented > specialized languages; there are numerous applications where scalars and > 1x1 matrices are mathematically equivalent. I'm curious what those applications are, because regular multiplication behaves differently depending on whether you have a 1x1 matrix or a scalar: [[2]]*[[1, 2, 3], [2, 3, 4]] is not defined 2*[[1, 2, 3], [2, 3, 4]] = [[2, 4, 6], [2, 6, 8]] I'm curious as to what these applications are, and what they're actually doing. Kronecker multiplication perhaps? Do you have some examples of those applications? -- Steven From doomster at knuut.de Sun Jul 26 05:45:37 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sun, 26 Jul 2009 11:45:37 +0200 Subject: C-API, tp_dictoffset vs tp_members References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> Message-ID: <7d2mu7F27a9tbU1@mid.uni-berlin.de> "Martin v. L?wis" wrote: >> I have a predefined set of members, some of which are optional. > > Having optional fields is also a good reason. What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? I would have though that the former describes an optional field, because the behaviour of accessing it when it is NULL is the same as accessing a nonexistent field. However, I see that it still appears in the dir() output even if it is NULL, so it seems I'm misunderstanding this. >> The problem >> I had with an embedded dictionary was that I can't see its elements using >> "dir()". > > How so? That should work fine. >>> import example >>> x = example.Example() >>> dir(x) ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__'] >>> x.example_attribute 'example value' The 'example_attribute' can be accessed, but it is not visible in the dir() output. The code for this is below. >> Now I just converted to using tp_members, and it still seems to >> work correctly for the cases I tested. > > So how do you do optional fields now? In particular, how would you do > optional integers? See above, I would expect T_OBJECT_EX to do that. Thanks! Uli /* example.c gcc -Wall example.c -shared -I /usr/include/python2.5 -o example.so */ #include #include typedef struct { PyObject_HEAD PyObject* dict; } Example; static void Example_dealloc(PyObject* self) { Example* ex = (Example*)self; Py_XDECREF(ex->dict); self->ob_type->tp_free(self); } static PyObject * Example_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { Example* self; PyObject* attr; self = (Example*)type->tp_alloc(type, 0); if(self==NULL) return NULL; self->dict = PyDict_New(); if(self->dict==NULL) { Py_DECREF(self); return NULL; } attr = PyString_FromString("example value"); if(PyObject_SetAttrString((PyObject*)self, "example_attribute", attr)<0) { Py_DECREF(self); return NULL; } return (PyObject*)self; } static PyTypeObject ExampleType = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ "example.Example", /*tp_name*/ sizeof(Example), /*tp_basicsize*/ 0, /*tp_itemsize*/ Example_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT, /*tp_flags*/ 0, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ offsetof(Example, dict), /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ Example_new, /* tp_new */ }; void initexample(void) { PyObject* m; if (PyType_Ready(&ExampleType) < 0) return; m = Py_InitModule3("example", 0, "Example module that creates an extension type."); if (m == NULL) return; Py_INCREF(&ExampleType); PyModule_AddObject(m, "Example", (PyObject*)&ExampleType); } From israelu at elbit.co.il Sun Jul 26 05:50:15 2009 From: israelu at elbit.co.il (iu2) Date: Sun, 26 Jul 2009 02:50:15 -0700 (PDT) Subject: multiprocessing and __main__ References: Message-ID: On Jul 26, 8:52?am, is un wrote: > Hi, > > Trying the multiprocessing module for the first time, I spent quite a > bit on making this code run: > > from multiprocessing import Process > import time > > def my_process(): > ? ? i = 0 > ? ? while 1: > ? ? ? ? print i > ? ? ? ? i += 1 > ? ? ? ? time.sleep(0.5) > > p = Process(target=my_process, args=()) > p.start() > > print 'Process started' > > The result was not what I expected, it seems like the process restarts > all the time, and the message 'Process started' keeps getting > printed... > > Going back to the documentation, I realized that the doc was really > serious about the line: > if __name__ == '__main__' .. which I always ignore, and by changing > the code to > > if __name__ == '__main__': > ? ? p = Process(target=my_process, args=()) > ? ? p.start() > > ? ? print 'Process started' > > the problem was solved. > So my question is what actually happens when I call p.start()? Is the > entry file reloaded under a different module name? > > Thanks > iu2 Ok, I found it, it is nicely documented, I missed it before. Case closed. From doomster at knuut.de Sun Jul 26 05:54:55 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sun, 26 Jul 2009 11:54:55 +0200 Subject: Removing newlines from string on windows (without replacing) References: Message-ID: <7d2nfnF29tk6fU1@mid.uni-berlin.de> Tom wrote: > s = sauce.replace("\n", "") > > Sauce is a string, read from a file, that I need to remove newlines > from. This code works fine in Linux, but not in Windows. rstrip("\n") > won't work for me, so anybody know how to get this working on Windows? I'm pretty sure this works regardless of the system. What might change is the way that a line is terminated: MS Windows by default uses a CR/LF ('\r\n') pair, while most other systems just an LF ('\n'). I'd adapt the code to accept either line ending on either system. If that's not what you want, you should improve your "doesn't work" description. ;) Uli From max at alcyone.com Sun Jul 26 06:11:02 2009 From: max at alcyone.com (Erik Max Francis) Date: Sun, 26 Jul 2009 03:11:02 -0700 Subject: len() should always return something In-Reply-To: <027c168c$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> <027c168c$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I'm curious what those applications are, because regular multiplication > behaves differently depending on whether you have a 1x1 matrix or a > scalar: > > [[2]]*[[1, 2, 3], [2, 3, 4]] is not defined > > 2*[[1, 2, 3], [2, 3, 4]] = [[2, 4, 6], [2, 6, 8]] > > I'm curious as to what these applications are, and what they're actually > doing. Kronecker multiplication perhaps? Do you have some examples of > those applications? The most obvious example would be the matrix algebra equivalent of the calculation of the dot product, the equivalent of a dot product in normal vector algebra. If you have two 3-vectors (say), u = (u_1, u_2, u_3) and v = (v_1, v_2, v_3), then the dot product is the sum of the pairwise products of these terms, u dot v = sum_i u_i v_i = u_1 v_1 + u_2 v_2 + u_3 v_3. Nothing revolutionary there. The matrix way of writing this wouldn't obviously work, since multiplying two nx1 or (1xn) matrixes by each other is invalid. But you _can_ multiply a nx1 matrix by an 1xn matrix, and you get the equivalent of the dot product: u v^T = ( u dot v ), where the right hand side of the equation is formally a 1x1 matrix (intended to be indicated by the parentheses), but you can see how it is useful to think of it as just a scalar, because it's really just a matrix version of the same thing as a dot product. This stretches out to other kinds of applications, where, say, in tensor calculus, you can think of a contravariant vector as being transformed into a covariant vector by the application of the matrix tensor, which is written as lowering an index. The components of the contravariant vector can be thought of as a column vector, while the components of a covariant vector can be represented with a row vector. The application of the metric tensor to a contravariant vector turns it into a row vector, and then contracting over the two vectors gives you the inner product as above. The applications and contractions can all be visualized as matrix multiplications. (Contrary to a popularization, tensors _aren't_ generalizations of matrices, but their components can be.) It's a bunch of other examples like that where you end up with a 1x1 matrix that really represents a scalar, and since that was intended there really isn't a lot of efficacy to treat it as a separate entity. Especially if you're dealing with a special-purpose language where everything is really a form of an generalized array representation of something _anyway_. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Scars are like memories. We do not have them removed. -- Chmeee From wilk at flibuste.net Sun Jul 26 06:22:34 2009 From: wilk at flibuste.net (William Dode) Date: 26 Jul 2009 10:22:34 GMT Subject: ANN: psyco V2 References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> <4a696af3$0$442$426a74cc@news.free.fr> Message-ID: <4a6c2e6a$0$424$426a34cc@news.free.fr> On 24-07-2009, Christian Tismer wrote: > On 7/24/09 1:04 AM, William Dode wrote: >> On 23-07-2009, Christian Tismer wrote: > ... > >>> Wasn't the project plan saying the opposite, borrowing >>> some ideas from psyco? :-) >>> http://code.google.com/p/unladen-swallow/wiki/ProjectPlan >> >> How do you see the future of psyco when unladen-swallow will grab the >> best of psyco (if they can !) ? > > I have no objections, but also no idea so far how that could work. > Sounded like an unreflected idea to me, without seriously > checking the possibilities and/or implications. The same > kind of research apparently did not happen concerning PyPy, > which IMHO is much more suitable to take advantage from. > > This is for the current status of psyco, of course. > It will change dramatically in the next months, slowly > adopting some of PyPy's ideas. Then it might start to > make more sense. I didn't understand how psyco could use somes PyPy's ideas, i thought it's only the opposite... So i was going to read the PyPy's documentation : it's fascinating !!! But how to test it ? The doc says : "When PyPy is translated into an executable like pypy-c, the executable contains a full virtual machine that can optionally include a Just-In-Time compiler." Where is this 'option', is there somethings to activate it ? thx -- William Dod? - http://flibuste.net Informaticien Ind?pendant From deets at nospam.web.de Sun Jul 26 06:51:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 26 Jul 2009 12:51:31 +0200 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: References: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> Message-ID: <7d2qpkF29u2uiU1@mid.uni-berlin.de> Jessica Smith schrieb: > Chris thanks!! > > other members: > > I read this: > - http://chrismiles.livejournal.com/25648.html > > I did this: > - install mac ports > - port install readline > - vi setup.py > > def detect_modules(self): > # Ensure that /usr/local is always used > add_dir_to_list(self.compiler.library_dirs, '/opt/local/lib') > add_dir_to_list(self.compiler.include_dirs, '/opt/local/include') > > - configure --prefix=/pt/p > - make > - make install > > I'm happy now! You are aware that under OSX one usually wants a so-called framework-build? It's an configure-option. You don't need the --prefix then (unless you have another python2.6 installed), and without the FW-build no UI-packages are useable. Diez From piet at cs.uu.nl Sun Jul 26 07:50:57 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 26 Jul 2009 13:50:57 +0200 Subject: how can a child thread notify a parent thread its status? References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> Message-ID: >>>>> MRAB (M) wrote: >M> scriptlearner at gmail.com wrote: >>> I decided to go with one big log file, which will be shared by all >>> threads (child and parent). A log message Queue is used to store all >>> log entries, and a customized logger thread will get log entries from >>> the Queue. >>> >>> #from the logger thread# >>> def run(self): >>> while self.flag == 1: #if the flag is set to 0, the logger >>> thread should exit >>> try: >>> entry = self.q.get() >>> except Empty: >>> self.logger.debug('cant find any log entry') >>> continue >>> except: >>> self.logger.error("Unexpected error:", sys.exc_info() >>> [0]) >>> raise >>> #do whatever that should be done >>> self.logger.info("logger thread done") #should see this >>> message in log file as well >>> def off(self): >>> self.logger.info('turning off flag') >>> self.flag = 0 >>> >>> >>> #in parent thread# >>> logItemQ.put('We are done, lets stop the logger now.') >>> time.sleep(1) #it seems that the logger thread cannot exit if >>> I put a sleep here >>> myLog.off() #off is called successfully >>> myLog.join() >>> >>> >>> I put an off method to turn off a flag so the logger thread knows it >>> should exit. However, the last log message (the one 'We are done, >>> lets stop the logger now.') won't be logged if I call myLog.off() and >>> myLog.join() immediately. So, I put a time.sleep(1) to make sure the >>> logger thread has enough time to finish it's job. Unfortunately, now >>> the logger thread simply won't exit, and I don't see the message >>> 'logger thread done'. I can't figure out at which point it hangs, >>> since I don't any new log entry but the thread simply won't exit. >>> Am I taking a right approach by using a flag? Should I lock the flag? >M> self.q.get() will block if the queue is empty, so the Empty exception is >M> never raised. >M> What's happening is that the parent thread puts the final message into >M> the queue, sleeps, and then clears the flag; meanwhile, the logging >M> thread gets the message, writes it out, checks the flag, which is still >M> set, and then tries to get the next message. The queue is empty, so the >M> .get() blocks. >M> The simplest solution is not to use a flag, but the sentinel trick. The >M> parent thread can put, say, None into the queue after the last message; >M> when the logging thread gets None, it knows it should terminate. To the OP: a sleep will never do it because you can't be sure how long the logging thread will lag behind. Most multithreaded `solutions' which depend on timings are buggy. If you have more than one thread writing to the log queue one sentinel won't solve the problem. You would need as many sentinels as there are threads, and the logger should count the sentinels which can have normal messages between them. It may be a bit fragile to depend on the number of threads especially when this number is dynamic. One solution would be to have special StartLogging and StopLogging objects that are put in the queue when a thread starts and finishes respectively. The logger can stop then when the number of StopLogging objects equals the number of StartLogging objects received and that number is greater than 0. This presupposes that no new thread will be started after all the others have finished. The StartLogging objects can also be put in the queue by the parent when it starts up the threads, which may be more robust because it has fewer timing dependencies. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From breamoreboy at yahoo.co.uk Sun Jul 26 07:53:20 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 Jul 2009 12:53:20 +0100 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: Neil Hodgson wrote: > casebash: > >> I have searched this list and found out that Python doesn't have a >> mutable string class (it had an inefficient one, but this was removed >> in 3.0). Are there any libraries outside the core that offer this? > > I wrote a gap buffer implementation for Python 2.5 allowing > character, unicode character and integer elements. > > http://code.google.com/p/gapbuffer/ > > Its not seen much use or any maintenance so is unlikely to work with > Python 3.x. > > Neil I tried this as a learning exercise and found slicing doesn't work correctly. import gapbuffer print gapbuffer.GapBuffer(range(10))[:] GapBuffer('i')] If my sleuthing is correct the problem is with these lines ilow *= self->itemSize; ihigh *= self->itemSize; in GapBuffer_slice being computed before ilow and ihigh are compared to anything. Python 2.6.2 32 bit Windows. -- Kindest regards. Mark Lawrence. From helvinlui at gmail.com Sun Jul 26 08:04:11 2009 From: helvinlui at gmail.com (Helvin) Date: Sun, 26 Jul 2009 05:04:11 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> Message-ID: <4ee78bf1-476d-4e41-b9da-c20297aad861@13g2000prl.googlegroups.com> On Jul 24, 5:03?am, Robert Kern wrote: > On 2009-07-23 03:55, Helvin wrote: > > > I believe I now have vtkpython.exe. However, my 'import vtk' statement > > in my python code is not working. The error says something like "no > > module named vtk". > > Where do I find modules for vtk inpyqt? Do they exist? > > There are no VTK modules inPyQtitself. ThePyQtsupport is in the vtk package > which can be built with VTK itself. You will need to install VTK and the vtk > package correctly in order to achieve this. vtkpython.exe is not going to help > you. Ignore it. After you have built VTK, you need to do an extra step to > install the vtk package to where your Python interpreter will be able to find it. > > Let's say that your build directory is c:\vtkbuild. > > ? ?cd \vtkbuild\Wrapping\Python > ? ?python setup.py install > ? ?cd \ > > Now you should be able to import vtk from your normal Python interpreter. If you > are still having problems, you will need to copy-and-paste exactly what you did > and what error messages you got. Do not paraphrase error messages. > > -- > 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 Hi Robert, I realised that vtkpython.exe works, if I drag and drop my .py file on it. Ie, running my .py file using vtkpython.exe But my python interpreter does not work with the python file. I have tried what you suggested. Below is what I typed in command line, as well as the error that it produces. C:\Qt\VTKbin7\Wrapping\Python>python setup.py install Traceback (most recent call last): File "setup.py", line 138, in raise "ERROR: Must specify BUILD_TYPE= on command line." TypeError: exceptions must be classes or instances, not str Is it just a small error with input format or something? Thanks so much! Helvin From aahz at pythoncraft.com Sun Jul 26 09:16:39 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 06:16:39 -0700 Subject: If Scheme is so good why MIT drops it? References: Message-ID: In article , Raffael Cavallaro wrote: >On 2009-07-25 00:55:26 -0400, Carl Banks said: >> >> But please don't put it on the same level as PHP. Their situations >> have almost nothing in common. > >Their situations have much in common; Python attracted programmers away >from (for example) C++, becuse python is easier to master; Then php >came along and attracted programmers away from (for example) python, >because php is easier to master. No, Python attracted programmers away from C++ because it's easier to write good programs in it, in less time. There are plenty of expert C++ programmers who switched to Python; your thesis only applies to the legions of people who found it difficult to learn C++ in the first place. Moreover, AFAIK PHP never attracted people away from Python; Python was not particularly popular when PHP was in its heyday. PHP attracted people away from Perl and Java and C++. I'm curious, do you actually know Python at all? Have you used it for a serious project? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Sun Jul 26 09:31:06 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Sun, 26 Jul 2009 09:31:06 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-26 09:16:39 -0400, aahz at pythoncraft.com (Aahz) said: > There are plenty of expert C++ > programmers who switched to Python; "plenty" is an absolute term, not a relative term. I sincerely doubt that the majority of python users were formerly *expert* C++ programmers. > your thesis only applies to the > legions of people who found it difficult to learn C++ in the first place. No, my thesis applies to the overwhelming majority of programmers who found it more difficult to *master* (i.e., not merely use) C++ as opposed to mastering python. BTW, this is a *complement* not a dis; python is a better language than C++ precisely because it is more sensibly and elegantly designed than C++ and therefore easier to master. php represents the same process but farther down the ladder, as it were. There's often a tradeoff between ease of mastery and power. python hits a sweet spot for many tasks and many programmers, especially as compared to C++ (or even lisp, which though more powerful than python is more difficult to master. lisp beats C++ on both counts imho - more powerful *and* easier to master). php hits a sweet spot only in a very restricted domain. Beyond that, it is clearly inferior to python which has greater power, but is more difficult to master. -- Raffael Cavallaro From pauljbarry3 at gmail.com Sun Jul 26 10:29:44 2009 From: pauljbarry3 at gmail.com (Paul Barry) Date: Sun, 26 Jul 2009 07:29:44 -0700 (PDT) Subject: Can't get UDP example to work Message-ID: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> I'm trying to get one of the examples from Foundation of Python Network Programming to work. Specifically this is the UDP example from Ch 3. First there is the server: #!/usr/bin/env python # UDP Echo Server - Chapter 3 - udpechoserver.py import socket, traceback, time host = '127.0.0.1' # Bind to all interfaces port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) while 1: try: message, address = s.recvfrom(8192) print "Got message '%s' from %s" % (message, address) # Echo it back s.sendto(message, address) print "Sent response to '%s'" % (address,) except (KeyboardInterrupt, SystemExit): raise except: traceback.print_exc() Next I have a client written in Ruby, which works. I am posting thing not to start a Ruby/Python flame war, but to simply prove that the server works and there are no weird networking issues that would prevent the Python client from working. The Ruby code is: #!/usr/bin/env ruby require 'socket' socket = UDPSocket.new socket.connect ARGV[0], ARGV[1] puts "Enter data to transmit: " data = STDIN.gets.chomp socket.send data, 0 puts "Looking for replies; press Ctrl-C or Ctrl-Break to stop." loop do buf = socket.recvfrom(2048) puts buf.first end When I start the server and run that, the output looks like this: $ ch02/udp.rb 127.0.0.1 51423 Enter data to transmit: foobar Looking for replies; press Ctrl-C or Ctrl-Break to stop. foobar Now, when I try the python example: #!/usr/bin/env python # UDP Example - Chapter 2 - udp.py import socket, sys host = sys.argv[1] textport = sys.argv[2] s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: port = int(textport) except ValueError: # That didn't work. Look it up instead. port = socket.getservbyname(textport, 'udp') s.connect((host, port)) print "Enter data to transmit: " data = sys.stdin.readline().strip() s.sendall(data) print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." while 1: buf = s.recvfrom(2048) sys.stdout.write(buf[0]) I don't ever get a response: $ ch02/udp.py 127.0.0.1 51423 Enter data to transmit: foobar Looking for replies; press Ctrl-C or Ctrl-Break to stop. The server sees the message and says it has sent a reply: Got message 'foobar' from ('127.0.0.1', 49623) Sent response to '('127.0.0.1', 49623)' Any ideas as to why this doesn't work? From michael at stroeder.com Sun Jul 26 10:59:42 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Sun, 26 Jul 2009 16:59:42 +0200 Subject: ANN: python-ldap-2.3.9 Message-ID: Find a new release of python-ldap: http://www.python-ldap.org/ python-ldap provides an object-oriented API to access LDAP directory servers from Python programs. It mainly wraps the OpenLDAP 2.x libs for that purpose. Additionally it contains modules for other LDAP-related stuff (e.g. processing LDIF, LDAPURLs and LDAPv3 schema). Ciao, Michael. -- Michael Str?der E-Mail: michael at stroeder.com http://www.stroeder.com ---------------------------------------------------------------- Released 2.3.9 2009-07-26 Changes since 2.3.8: Lib/ * All modules (ldap, ldif, dsml and ldapurl) have common version number now * Non-exported function ldif.needs_base64() was abandoned and is now implemented as method LDIFWriter._needs_base64_encoding(). This allows sub-classes of LDIFWriter to implement determining whether attribute values have to be base64-encoded in a different manner and is the same approach like in class dsml.DSMLWriter. * LDAPUrlExtension._parse() now gracefully handles LDAP URL extensions without explicit exvalue as being set with implicit value None. Modules/ * New LDAP option constant ldap.OPT_X_SASL_NOCANON supported in LDAPObject.get/set_option() From python at mrabarnett.plus.com Sun Jul 26 11:07:40 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 26 Jul 2009 16:07:40 +0100 Subject: Can't get UDP example to work In-Reply-To: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: <4A6C713C.8050806@mrabarnett.plus.com> Paul Barry wrote: > I'm trying to get one of the examples from Foundation of Python > Network Programming to work. Specifically this is the UDP example > from Ch 3. First there is the server: > > #!/usr/bin/env python > # UDP Echo Server - Chapter 3 - udpechoserver.py > import socket, traceback, time > > host = '127.0.0.1' # Bind to all > interfaces > port = 51423 > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) > s.bind((host, port)) > > while 1: > try: > message, address = s.recvfrom(8192) > print "Got message '%s' from %s" % (message, address) > > # Echo it back > s.sendto(message, address) > print "Sent response to '%s'" % (address,) > > except (KeyboardInterrupt, SystemExit): > raise > except: > traceback.print_exc() > > Next I have a client written in Ruby, which works. I am posting thing > not to start a Ruby/Python flame war, but to simply prove that the > server works and there are no weird networking issues that would > prevent the Python client from working. The Ruby code is: > > #!/usr/bin/env ruby > require 'socket' > > socket = UDPSocket.new > socket.connect ARGV[0], ARGV[1] > > puts "Enter data to transmit: " > data = STDIN.gets.chomp > > socket.send data, 0 > puts "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > loop do > buf = socket.recvfrom(2048) > puts buf.first > end > > When I start the server and run that, the output looks like this: > > $ ch02/udp.rb 127.0.0.1 51423 > Enter data to transmit: > foobar > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > foobar > > Now, when I try the python example: > > #!/usr/bin/env python > # UDP Example - Chapter 2 - udp.py > > import socket, sys > > host = sys.argv[1] > textport = sys.argv[2] > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > try: > port = int(textport) > except ValueError: > # That didn't work. Look it up instead. > port = socket.getservbyname(textport, 'udp') > > s.connect((host, port)) > print "Enter data to transmit: " > data = sys.stdin.readline().strip() > s.sendall(data) > > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > while 1: > buf = s.recvfrom(2048) > sys.stdout.write(buf[0]) > > I don't ever get a response: > > $ ch02/udp.py 127.0.0.1 51423 > Enter data to transmit: > foobar > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > > The server sees the message and says it has sent a reply: > > Got message 'foobar' from ('127.0.0.1', 49623) > Sent response to '('127.0.0.1', 49623)' > > Any ideas as to why this doesn't work? > It works on my PC (Python 2.6.2, Windows XP Pro, service pack 3). From robert.avery at gmail.com Sun Jul 26 11:09:02 2009 From: robert.avery at gmail.com (Robert Avery) Date: Sun, 26 Jul 2009 08:09:02 -0700 (PDT) Subject: 'Registry' or 'Preference' settings in Mac Python Message-ID: <35258dc9-941d-42ec-9c35-9361d0c70da7@n11g2000yqb.googlegroups.com> Is there any way for me to save some sort of global preference for python that I can read/write at runtime that doesn't involve me explicitly creating a file? It may create a file underneath, but I don't want some path hard coded to my routine. In Windows, the Registry serves this purpose. Is there something similar for Mac? From deets at nospam.web.de Sun Jul 26 11:09:26 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 26 Jul 2009 17:09:26 +0200 Subject: cgi.fieldstorage() In-Reply-To: <13ed9818-aacd-4260-800b-a04177e13681@n11g2000yqb.googlegroups.com> References: <7cu9hhF29gt65U1@mid.uni-berlin.de> <7cv268F29q0j5U1@mid.uni-berlin.de> <13ed9818-aacd-4260-800b-a04177e13681@n11g2000yqb.googlegroups.com> Message-ID: <7d39t9F2963rqU1@mid.uni-berlin.de> gert schrieb: > On Jul 25, 2:33 am, "Diez B. Roggisch" wrote: >> gert schrieb: >> >>> On Jul 24, 7:32 pm, "Diez B. Roggisch" wrote: >>>> gert schrieb: >>>>> this is a non standard way to store multi part post data on disk >>>>> def application(environ, response): >>>>> with open('/usr/httpd/var/wsgiTemp','w') as f: >>>>> while True: >>>>> chunk = environ['wsgi.input'].read(8192).decode('latin1') >>>>> if not chunk: break >>>>> f.write(chunk) >>>>> response('200 OK',[]) >>>>> return ['complete'] >>>>> my question is how do i handle the file, so i can shuffle it into a db >>>>> using small chunks of memorie ? >>>> I don't think that's possible with the current DB-API. There is no >>>> stream-based BLOB-interface (as e.g. JDBC offers). >>>> So the answer certainly depends on your used RDBMS. For oracle, you >>>> would be lucky: >>>> http://cx-oracle.sourceforge.net/html/lob.html >>>> Other adapters I don't know about. >>> sqlite :) ok let say for now it would be impossible on a db level, but >>> before i reach the impossible, i still need to parse the file to >>> prepare the chunks. How do i do that ? How do i get the chunks without >>> loading the hole file into memorie ? >> It's memory - memorie might be some nice dutch girl you know :) >> >> Apart from that, your code above does exactly that - reads the data >> chunkwise. If the WSGI-implementation works proper, this will be the >> socket's input stream, so there is no memory overhead involved. >> >> Now of course if you want to have a multi-pass processing of the file >> without putting it into memory, then you need to save it to the harddisk >> befor. >> >> But honestly - we are talking about a web-application here. My >> DSL-connection has 1 MBit upstream, the average server has at least 2GB >> of memory available - so we are talking 20000 seconds uploading time to >> fill that memory. Which is about 5 hours. And you are decoding the data >> to a certain decoding, so we are not talking about binary data here - >> are you really sure memory is an issue? >> > > What about some http upload resume features ? What has that todo with reading data into memory or not? Of course you can create continous uploads with HTTP, but a single HTTP-request is a single HTTP-request. You can store the requests into individual blobs, and then when the overall file upload is finished, concatenate these. If you like. Or not, if you don't care, as you can as easily serve them even as one stream from those various chunks. But all of this has nothing todo with the need to not load POST-data into memory. This only becomes a problem if you are expecting uploads in the hundreds of megabytes. Diez From petite.abeille at gmail.com Sun Jul 26 11:18:17 2009 From: petite.abeille at gmail.com (Petite Abeille) Date: Sun, 26 Jul 2009 17:18:17 +0200 Subject: 'Registry' or 'Preference' settings in Mac Python In-Reply-To: <35258dc9-941d-42ec-9c35-9361d0c70da7@n11g2000yqb.googlegroups.com> References: <35258dc9-941d-42ec-9c35-9361d0c70da7@n11g2000yqb.googlegroups.com> Message-ID: <68879AA8-CC01-4CA1-815F-88C0DC0F0181@gmail.com> On Jul 26, 2009, at 5:09 PM, Robert Avery wrote: > In Windows, the Registry serves this purpose. Is there something > similar for Mac? http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html http://developer.apple.com/documentation/CoreFoundation/Reference/CFPreferencesUtils/Reference/reference.html http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFPreferences/CFPreferences.html -- PA. http://alt.textdrive.com/nanoki/ From aahz at pythoncraft.com Sun Jul 26 11:44:02 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 08:44:02 -0700 Subject: invoke method on many instances References: Message-ID: In article , Gabriel Genellina wrote: >En Thu, 23 Jul 2009 21:27:35 -0300, Aahz escribi?: >> In article , >> Gabriel Genellina wrote: >>> >>> NLMPI >> >> What? > >IHNFI What? Abbreviations are fine, but if someone asks you about one, it would be nice to just expand it instead of replying with another abbreviation. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From victorsubervi at gmail.com Sun Jul 26 11:48:29 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 26 Jul 2009 12:48:29 -0300 Subject: Cannot Get Form To Work In-Reply-To: References: <4dc0cfea0907230846g4f593219t308c64eec4943f93@mail.gmail.com> Message-ID: <4dc0cfea0907260848n1c59793bwd9f2dc1be46e66f1@mail.gmail.com> Oops. My bad. I did in fact have this line of code before calls to form: form = cgi.FieldStorage() I changed the except command from "pass" to "print sys.exc_info()" as per your suggestion. Still, however, the form just renews itself without offering any information whatsoever. You can see this here, if you like: http://13gems.com/test-Calculators_frame.py TIA, beno On Thu, Jul 23, 2009 at 2:01 PM, Dennis Lee Bieber wrote: > On Thu, 23 Jul 2009 12:46:16 -0300, Victor Subervi > declaimed the following in > gmane.comp.python.general: > > > sent. Here again is the code: > > > > from primeNumbers import primeNumbers > > > > try: > > lang = form.getfirst('lang', 'en') > > This code is incomplete -- where is "form" created? I see no > "import"s that might define such... > > > browser = form.getfirst('browser', 'all') > > site = form.getfirst('site', 'bridge') > > num = form.getfirst('num','') > > except: > > pass > > Since there is no such thing as "form" in the code you posted, the > first "form.getfirst()" operation will raise an exception. That > exception is then being ignored by this "pass". > > I'd suggest moving the code that outputs the HTTP response header > and the HTML prelude material to the top, then replace "pass" with > something that prints actual exception information (the stuff from > sys.exc_info() ) > -- > Wulfraed Dennis Lee Bieber KD6MOG > wlfraed at ix.netcom.com wulfraed at bestiaria.com > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: web-asst at bestiaria.com) > HTTP://www.bestiaria.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjmachin at lexicon.net Sun Jul 26 11:51:39 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 26 Jul 2009 08:51:39 -0700 (PDT) Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> <88e8ea19-7106-406a-a531-88c2b6893987@y7g2000yqa.googlegroups.com> Message-ID: On Jul 23, 11:53?am, Paul McGuire wrote: > On Jul 22, 5:43?pm, Filip wrote: > > # Needs re.IGNORECASE, and can have tag attributes, such as
CLEAR="ALL"> > line_break_re = re.compile('', re.UNICODE) Just in case somebody actually uses valid XHTML :-) it might be a good idea to allow for
> # what about HTML entities defined using hex syntax, such as &#xxxx; > amp_re = re.compile('\&(?![a-z]+?\;)', re.UNICODE | re.IGNORECASE) What about the decimal syntax ones? E.g. not only   and   but also   Also, entity names can contain digits e.g. ¹ ¾ From aahz at pythoncraft.com Sun Jul 26 11:55:18 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 08:55:18 -0700 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <87ljmogglz.fsf@benfinney.id.au> <02701ee5$0$5185$c3e8da3@news.astraweb.com> <056f629b-aa63-458a-ae16-ac40a759e446@h11g2000yqb.googlegroups.com> Message-ID: In article <056f629b-aa63-458a-ae16-ac40a759e446 at h11g2000yqb.googlegroups.com>, Shai wrote: > >class DocInherit(object): > """ > Docstring inheriting method descriptor > > The class itself is also used as a decorator > """ Nice! Maybe stick this on the Cookbook? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From piet at cs.uu.nl Sun Jul 26 12:13:22 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 26 Jul 2009 18:13:22 +0200 Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? References: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> Message-ID: >>>>> ryles (r) wrote: >r> On Jul 25, 8:57?am, Piet van Oostrum wrote: >>> >>>>> ryles (r) wrote: >>> >r> According tohttp://www.python.org/doc/essays/packages.html: >>> >r> "The import statement first tests whether the item is defined in the >>> >r> package; if not, it assumes it is a module and attempts to load it." >>> >r> However, I've noticed that once a module is imported using the >>> >r> `from pkg import mod' syntax, if its name is deleted from the >>> >r> package namespace then subsequent imports with `from' will fail. >>> >>> This is incorrectly stated. Also on the initial import it will fail, not >>> just on subsequent imports. >>> >>> piet$ python >>> Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) >>> [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin >>> Type "help", "copyright", "credits" or "license" for more information. >>> from pkg import _impl >>> >>> _impl # this is from a print statement in _impl to show that it did import >>> Traceback (most recent call last): >>> ? File "", line 1, in >>> ImportError: cannot import name _impl >>> >r> Well, since __init__.py is executed first, I would regard it as >r> causing the initial (successful) import, and yours being the >r> subsequent one, so that the "initial" import does not fail, the >r> subsequent one does. Wording aside, though, I think we are on the same >r> page here. I was talking about the 'from pkg import _impl' statements because I thought that's what we were talking about. Of course this does an implicit 'import pkg' first, but that doesn't fail. In your OP you were also talking about `from pkg import mod' imports and said that the subsequent ones of these fails. I noticed that also the first one of these would fail. >>> >>> According to the documentation >>> the statement >>> from pkg import _impl >>> >>> _temp = __import__('pkg', globals(), locals(), ['_impl'], -1) >>> _impl = _temp._impl >>> >>> This fails because _temp (the imported module) doesn't have a binding >>> for _impl because you deleted it. >r> But supposing we hadn't deleted it, and __init__.py didn't import >r> _impl, there would still be no binding. Yet the importer would still >r> import and make the module available (presumably it "knows" to do this >r> since it was not already in sys.modules). The question really is that >r> since in our example pkg._impl is still in sys.modules, why won't the >r> importer add it to the pkg namespace again as it previous had? I would >r> imagine this is either by design, or is a case that was never >r> considered. Yes, that is also something I don't understand. >>> for _impl because you deleted it. By the way, if you have a `normal' >>> package that doesn't do anything with the name _impl, after the import >>> the pkg module namespace will have got a binding for _impl although it is not >>> in your code. It will have been put there by the import code. >r> Yes, this was noted further down in the original post with an example. Sorry, I missed that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From bazwal at ftml.net Sun Jul 26 12:22:53 2009 From: bazwal at ftml.net (Baz Walter) Date: Sun, 26 Jul 2009 17:22:53 +0100 Subject: pyc files not automatically compiled on import Message-ID: <4A6C82DD.8020005@ftml.net> hello i thought that python automatically compiled pyc files after a module is successfully imported. what could prevent this happening? Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.mkdir('/home/baz/tmp/foo') >>> os.chdir('/home/baz/tmp/foo') >>> f = open('foo.py', 'w') >>> f.write('print "hello world"\n') >>> f.close() >>> os.listdir('.') ['foo.py'] >>> import foo hello world >>> os.listdir('.') # why no pyc file? ['foo.py'] >>> import py_compile >>> py_compile.compile('foo.py') >>> os.listdir('.') ['foo.py', 'foo.pyc'] From fuzzyman at gmail.com Sun Jul 26 12:44:14 2009 From: fuzzyman at gmail.com (Fuzzyman) Date: Sun, 26 Jul 2009 09:44:14 -0700 (PDT) Subject: pyc files not automatically compiled on import References: Message-ID: On Jul 26, 5:22?pm, Baz Walter wrote: > hello > > i thought that python automatically compiled pyc files after a module is > successfully imported. what could prevent this happening? > > Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > ?>>> import os > ?>>> os.mkdir('/home/baz/tmp/foo') > ?>>> os.chdir('/home/baz/tmp/foo') > ?>>> f = open('foo.py', 'w') > ?>>> f.write('print "hello world"\n') > ?>>> f.close() > ?>>> os.listdir('.') > ['foo.py'] > ?>>> import foo > hello world > ?>>> os.listdir('.') # why no pyc file? > ['foo.py'] > ?>>> import py_compile > ?>>> py_compile.compile('foo.py') > ?>>> os.listdir('.') > ['foo.py', 'foo.pyc'] Works for me I'm afraid (Mac OS X and Python 2.6). Michael Foord -- http://www.ironpythoninaction.com/ From roy at panix.com Sun Jul 26 12:53:59 2009 From: roy at panix.com (Roy Smith) Date: Sun, 26 Jul 2009 12:53:59 -0400 Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: In article <148abf0f-c9e4-4156-8f16-e4e5615d350b at s6g2000vbp.googlegroups.com>, Paul Barry wrote: > host = '127.0.0.1' # Bind to all interfaces This threw me off the track for a little while. The comment is wrong! You're not binding to all interfaces, you're binding specifically to the loopback interface. For what you're doing, it's fine, but the comment is still wrong. It's also a little weird that the example uses connect() and sendall(). Those are TCP-ish type constructs. They work with UDP sockets, but using them sort of obscures the fundamental datagram nature of UDP. Well, enough of that. The real problem seems to be right here: > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > while 1: > buf = s.recvfrom(2048) > sys.stdout.write(buf[0]) Try adding sys.stdout.flush(), and it should work. Or change it to print. I've never seen this book, but if this code really is verbatim from the book, I'm not very impressed. From __peter__ at web.de Sun Jul 26 13:07:30 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 26 Jul 2009 19:07:30 +0200 Subject: pyc files not automatically compiled on import References: Message-ID: Baz Walter wrote: > i thought that python automatically compiled pyc files after a module is > successfully imported. what could prevent this happening? > > > Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import os > >>> os.mkdir('/home/baz/tmp/foo') > >>> os.chdir('/home/baz/tmp/foo') > >>> f = open('foo.py', 'w') > >>> f.write('print "hello world"\n') > >>> f.close() > >>> os.listdir('.') > ['foo.py'] > >>> import foo > hello world > >>> os.listdir('.') # why no pyc file? > ['foo.py'] > >>> import py_compile > >>> py_compile.compile('foo.py') > >>> os.listdir('.') > ['foo.py', 'foo.pyc'] You did not set the PYTHONDONTWRITEBYTECODE environment variable in a former life, or did you? From Tom.sully at gmx.com Sun Jul 26 13:13:44 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 10:13:44 -0700 (PDT) Subject: Removing newlines from string on windows (without replacing) References: <7d2nfnF29tk6fU1@mid.uni-berlin.de> Message-ID: Sorry for not providing enough information - and Rhodri, for sending my second message I used the GMX webmail client rather than my usual Linux client as I was testing it on windows. That must have screwed it up, I need to download Thunderbird on Mozzila. The thing that was messing it up was that the endlines are handled differently on each each OS, so I changed the code to strip the endlines to be: if os.name == "nt": s = sauce.rstrip("\r\n") else: s = sauce.replace("\n", "") This works fine now, (os.name returns nt on windows and posix on linux) Thanks for the help, and sorry for slow response and lack of detail, I'm still trying to get used to this usenet thing :P -- print "Tom" From qauzzix at gmail.com Sun Jul 26 13:48:27 2009 From: qauzzix at gmail.com (Qauzzix) Date: Sun, 26 Jul 2009 10:48:27 -0700 (PDT) Subject: How do I generate dia diagrams from python source code? References: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Message-ID: On Jul 25, 8:31?am, "Gabriel Genellina" wrote: > En Fri, 24 Jul 2009 17:52:47 -0300, Qauzzix escribi?: > > > Since I have been usingdiato make my UML diagrams. I also found an > > util named dia2code that generates python code fromdiadiagram. Now > > that I have that option I really want to find a way to generatedia > > diagram from existing code and/or maintain my diagrams. > > > I have been googling ?like crazy trying to find a way but with no > > luck. Anyone here know how to do this? > > SPE does that:http://pythonide.stani.be/ > > -- > Gabriel Genellina I think You are misunderstanding my question. I downloaded SPE and I have been trying it out. It has an UML featus but in no way I can see can I export it to be used by DIA the diagram editor. I could export it as an image, nothing more. There exists many apps for extracting UML diagrams from Python source but I need it to conform with DIA. From gallium.arsenide at gmail.com Sun Jul 26 13:54:52 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Sun, 26 Jul 2009 10:54:52 -0700 (PDT) Subject: Removing newlines from string on windows (without replacing) References: <7d2nfnF29tk6fU1@mid.uni-berlin.de> Message-ID: <2abd5ecb-6fed-4763-9563-2caf63ca3c15@r25g2000vbn.googlegroups.com> On Jul 26, 1:13?pm, Tom wrote: > The thing that was messing it up was that the endlines are handled > differently on each each OS, so I changed the code to strip the > endlines to be: > > ? ? if os.name == "nt": > ? ? ? ? s = sauce.rstrip("\r\n") > ? ? else: > ? ? ? ? s = sauce.replace("\n", "") Well, this is doing two different things. When os.name is 'nt', you are getting rid of all *trailing* CRLFs. Otherwise, you are getting rid of all LFs *anywhere in the string*. (Even if it so happens sauce will only ever have LFs at the end, it's still better to use the method that is closest to your intended meaning.) Personally, I prefer using sauce.rstrip() with no parameters (one of the suggestions Rhodri mentioned) if it's not important to preserve trailing spaces, since this will work regardless of end-of-line style (and typically, trailing spaces, tabs, etc. are undesirable anyway). John From aahz at pythoncraft.com Sun Jul 26 13:56:54 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 10:56:54 -0700 Subject: Removing newlines from string on windows (without replacing) References: Message-ID: In article , Tom wrote: > >I have an annoying problem. While I mainly use Linux when I distribute >this program to friends and on the internet, it'll get used on Windows. >So, I tested my python program on my Windows Vista dual boot, running >the same version of python (2.6) as my Linux, and got an error at the >following code. > >s = sauce.replace("\n", "") > >Sauce is a string, read from a file, that I need to remove newlines >from. This code works fine in Linux, but not in Windows. rstrip("\n") >won't work for me, so anybody know how to get this working on Windows? Use open(fname, 'U') -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From martin at v.loewis.de Sun Jul 26 14:03:39 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 20:03:39 +0200 Subject: C-API, tp_dictoffset vs tp_members In-Reply-To: <7d2mu7F27a9tbU1@mid.uni-berlin.de> References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> <7d2mu7F27a9tbU1@mid.uni-berlin.de> Message-ID: <4a6c9a7c$0$12532$9b622d9e@news.freenet.de> >>> I have a predefined set of members, some of which are optional. >> Having optional fields is also a good reason. > > What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? Right - this works for optional objects. However, it can't possibly work for any of the other fields. > I would > have though that the former describes an optional field, because the > behaviour of accessing it when it is NULL is the same as accessing a > nonexistent field. However, I see that it still appears in the dir() > output even if it is NULL, so it seems I'm misunderstanding this. I suppose that's because there will still be a descriptor for the field in the class. > The 'example_attribute' can be accessed, but it is not visible in the > dir() output. The code for this is below. I see. So you do need a tp_members list: static PyMemberDef example_members[] = { {"__dict__", T_OBJECT, offsetof(Example, dict), READONLY}, {0} }; Perhaps dir() should know about tp_dictoffset, but alas, it doesn't. Regards, Martin From vivainio at gmail.com Sun Jul 26 14:20:59 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Sun, 26 Jul 2009 11:20:59 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: <69c8f3be-cc84-48e5-9969-ef9e87836094@e27g2000yqm.googlegroups.com> On Jul 23, 11:29?pm, Nick Craig-Wood wrote: > > ?The syntax would be something like: > > > ?def work(): > > > ? ?showstatus("building") > > ? ?r = yield runshell("make") > > ? ?showstatus("installing") > > ? ?r = yield runshell("make install") > > ? ?showstatus("Success") > > > ?mygui.startwork(work) > > ?# returns immediately, runs work() gradually in the background. > > > ?The catch is that showstatus() would need to be run in the mainloop, > > ?so running the whole thing in a thread is a no-go. > > > ?I imagine runshell() would be implemented in terms of QProcess, or > > ?subprocess.Popen/os.system and a worker thread. > > > ?Anyone done this already, or do I have to roll my own? > > You might want to look at twisted, in particular > > ?http://twistedmatrix.com/trac/wiki/DeferredGenerator Indeed, DeferredGenerator looks pretty much what I'm interested in, thanks for the pointer. Unfortunately, it has the big Twisted dependency. That's probably not a showstopper, but Twisted requires a degree of buy-in when something smaller would do. From santhosh.vkumar at gmail.com Sun Jul 26 14:22:42 2009 From: santhosh.vkumar at gmail.com (Santhosh Kumar) Date: Sun, 26 Jul 2009 23:52:42 +0530 Subject: Itext for Python Message-ID: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> Hi all, One of my cousin suggested me to do a IText PDF converter for python. Actually I heard that there is no separate IText converter either we have to go for jython or GCJ with wrapper. Instead of wrapping, my plan is to create a separate module with Python and I am thinking of doing this in as my final year project also. Kindly give me your suggestion. With anticipation, SanthoshVKumar. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nagle at animats.com Sun Jul 26 14:24:48 2009 From: nagle at animats.com (John Nagle) Date: Sun, 26 Jul 2009 11:24:48 -0700 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> Message-ID: <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Beni Cherniavsky wrote: > On Jul 22, 9:36 am, Hendrik van Rooyen > wrote: >> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >> >>> My guess is that it was probably for optimization reasons long ago. >>> I've never heard a *good* reason why Python needs both. >> The good reason is the immutability, which lets you use >> a tuple as a dict key. >> > On Jul 22, 9:36 am, Hendrik van Rooyen > wrote: >> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >> >>> My guess is that it was probably for optimization reasons long ago. >>> I've never heard a *good* reason why Python needs both. >> The good reason is the immutability, which lets you use >> a tuple as a dict key. >> > The *technical* reason is immutability for dict keys. > Dict could allow mutable objects as keys by comparing *by value*, > making a copy on insertion and hashing the current value on lookup. > Prior art: the 2.3 sets module allows mutable Sets as elements in > Sets, by making ImmutableSet copies on insertion, and hashing Sets as > if they are temporarily immutable on lookup. > > This inspired PEP 351 and ambitious proposals to expand the approach > to all Python with a copy-on-write scheme. But these ideas were > rejected, and the 2.4 builtin sets only allow frozenset elements. > Half the reason is technical: copy-on-write and harder than it sounds, > and without it you pay a performance price. > > But the deeper reason is style: immutable types are convenient! > The allow a pure-functional style of code, which can be simpler. > Of course, sometimes an imperative style is simpler. Depends on the > problem. An interesting issue is Python objects, which are always mutable. A "dict" of Python objects is allowed, but doesn't consider the contents of the objects, just their identity (address). Only built-in types are immutable; one cannot create a class of immutable objects. So things like "frozenset" had to be built into the language. A tuple is really a frozen list. Arguably, frozen objects should have been a general concept. Conceptually, they're simple - once "__init__" has run, there can be no more changes to fields of the object. Compare the C++ approach, where you can have a "map" of any object that defines the "<" operator. Python has "__cmp__" for objects, but built-in dictionaries don't use it. Of course, one could create a "map" class in Python that uses "__cmp__", which would allow dictionary-type operations on arbitrary objects. There are some interesting uses for immutable objects in multithreaded programs. They can be shared between threads without locking. If it weren't for the infamous Global Interpreter Lock, which basically limits Python programs to using one CPU at a time, Python would have to deal with locking in a more advanced way. A system where immutable objects can be shared and passed between threads, and mutable objects must be "synchronized" (in the Java sense) to be shared would be useful. John Nagle From vivainio at gmail.com Sun Jul 26 14:32:11 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Sun, 26 Jul 2009 11:32:11 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> Message-ID: Apologies for the long subject line, here it is again: "Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops" On Jul 21, 7:48?pm, John Nagle wrote: > > The idea: > > > To run functions that execute a series of system commands without > > blocking the ui, *and* without adding state machine logic. > > ? ? At some level, there's going to be state machine logic. > You need interlocking so that the user can't initiate > simultaneous conflicting background operations from the GUI. This belongs to the "I don't care" category, as you'd expect to have only one operation like this on the flight at one time. The idea is to allow making simple scripts that you execute from the gui, that *may* need to access the gui objects at one point or another (so execution has to happen in the main thread most of the time - apart from blocking, long operations). > ? ? The usual solution is to run the background operations > in another thread, and have them put events on a queue > read by the GUI task. Yeah, and the receiving end of the "queue" here would be the same block of statements that launched the request - that is, I want to avoid having to create handler functions, and keep the program from linear. > ? ? You also have to work out how to cancel background operations, > if they're going to be as big as a "make". I think that is easy to accomplish with generators, provided that you can let the long-lasting operation run to completion - just close() the generator. If the long lasting computation was in a subprocess, rather than a thread, it could be interrupted in a cleaner fashion even. From drobinow at gmail.com Sun Jul 26 15:26:46 2009 From: drobinow at gmail.com (David Robinow) Date: Sun, 26 Jul 2009 14:26:46 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> > This doesn't mean they're on the same level - in fact, if you read carefully > you'll see my original post said as much: python attracted average > programmers; php attracted mediocre programmers and even some > non-programmers, which means that php is clearly a lesser language than > python. I'm a mediocre programmer. Does this mean I should switch to PHP? From bazwal at ftml.net Sun Jul 26 15:30:12 2009 From: bazwal at ftml.net (Baz Walter) Date: Sun, 26 Jul 2009 20:30:12 +0100 Subject: pyc files not automatically compiled on import In-Reply-To: References: Message-ID: <4A6CAEC4.7020300@ftml.net> Peter Otten wrote: > You did not set the PYTHONDONTWRITEBYTECODE environment variable in a former > life, or did you? thanks peter no i didn't, but i've just discovered a script in /etc/profile.d that did. now i'll have to try to find out how that script got in there :-| From dotancohen at gmail.com Sun Jul 26 15:34:52 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sun, 26 Jul 2009 22:34:52 +0300 Subject: Looking for a dream language: sounds like Python to me. Message-ID: <880dece00907261234x684547f4g5272b211379a7ba1@mail.gmail.com> Referring to this article: http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ The author, who is specifically looking for math-related functions, writes: """ The dream algorithm R&D tool would be similar to Matlab or Mathematica but could be compiled to fast, efficient binaries similar to ANSI C and would be available for all platforms. An integrated GUI builder similar to Visual Basic and integrated network support would be helpful. """ It looks to me like he is looking for Python. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From doomster at knuut.de Sun Jul 26 15:39:17 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sun, 26 Jul 2009 21:39:17 +0200 Subject: C-API, tp_dictoffset vs tp_members References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> <7d2mu7F27a9tbU1@mid.uni-berlin.de> <4a6c9a7c$0$12532$9b622d9e@news.freenet.de> Message-ID: <7d3pnaF29o57gU1@mid.uni-berlin.de> "Martin v. L?wis" wrote: >>>> I have a predefined set of members, some of which are optional. >>> Having optional fields is also a good reason. >> >> What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? > > Right - this works for optional objects. However, it can't possibly > work for any of the other fields. I have two members, one T_OBJECT and one T_OBJECT_EX. Both are NULL and both still appear in the dir() output. For one, accessing it returns 'None', for the other it raises an exception. I would have expected an element that is not accessible, not even 'None', to also not be visible in dir(). >> I would have thought that the former describes an optional field, >> because the behaviour of accessing it when it is NULL is the same >> as accessing a nonexistent field. However, I see that it still >> appears in the dir() output even if it is NULL, so it seems I'm >> misunderstanding this. > > I suppose that's because there will still be a descriptor for the > field in the class. So is that intentional or is it dir() that could be improved there? Should I file a bugreport? Thanks for the tip with __dict__ in tp_members, that does the job! Uli From pauljbarry3 at gmail.com Sun Jul 26 15:40:39 2009 From: pauljbarry3 at gmail.com (Paul Barry) Date: Sun, 26 Jul 2009 12:40:39 -0700 (PDT) Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a@p23g2000vbl.googlegroups.com> On Jul 26, 12:53?pm, Roy Smith wrote: > In article > <148abf0f-c9e4-4156-8f16-e4e5615d3... at s6g2000vbp.googlegroups.com>, > ?Paul Barry wrote: > > > host = '127.0.0.1' ? ? ? ? ? # Bind to all interfaces > > This threw me off the track for a little while. ?The comment is wrong! ? > You're not binding to all interfaces, you're binding specifically to the > loopback interface. ?For what you're doing, it's fine, but the comment is > still wrong. You are right. The example in the book actually has host = '' So the comment is correct. In trying to debug what was going on, I put 127.0.0.1 to see if that had something to do with it, which it didn't. > > It's also a little weird that the example uses connect() and sendall(). ? > Those are TCP-ish type constructs. ?They work with UDP sockets, but using > them sort of obscures the fundamental datagram nature of UDP. > > Well, enough of that. ?The real problem seems to be right here: > > > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > while 1: > > ? ? buf = s.recvfrom(2048) > > ? ? sys.stdout.write(buf[0]) > > Try adding sys.stdout.flush(), and it should work. ?Or change it to print. > Yes, that did the trick, thanks. I should have thought of that, if I would have just printed something after the recvfrom line I would have realized it was no longer blocking and the problem was with getting the client to print the data to stdout, not with getting a response from the server. > I've never seen this book, but if this code really is verbatim from the > book, I'm not very impressed. I like the book so far. It's a little verbose, but I think that's because he's trying to teach the concepts, rather than the write it how you actually would in production. In this case, I think he's trying to illustrate how the UDP example compares to the TCP example from the previous section, which is why he choose to keep the methods the same. But why not use print buf[0] instead of sys.stdin.write(buf[0]) that much, I don't know. From pauljbarry3 at gmail.com Sun Jul 26 15:42:32 2009 From: pauljbarry3 at gmail.com (Paul Barry) Date: Sun, 26 Jul 2009 12:42:32 -0700 (PDT) Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: <87ff9f36-30cb-4982-9730-7dcee491c0de@l2g2000vba.googlegroups.com> On Jul 26, 11:07?am, MRAB wrote: > Paul Barry wrote: > > I'm trying to get one of the examples from Foundation of Python > > Network Programming to work. ?Specifically this is the UDP example > > from Ch 3. ?First there is the server: > > > #!/usr/bin/env python > > # UDP Echo Server - Chapter 3 - udpechoserver.py > > import socket, traceback, time > > > host = '127.0.0.1' ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Bind to all > > interfaces > > port = 51423 > > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > > s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) > > s.bind((host, port)) > > > while 1: > > ? ? try: > > ? ? ? ? message, address = s.recvfrom(8192) > > ? ? ? ? print "Got message '%s' from %s" % (message, address) > > > ? ? ? ? # Echo it back > > ? ? ? ? s.sendto(message, address) > > ? ? ? ? print "Sent response to '%s'" % (address,) > > > ? ? except (KeyboardInterrupt, SystemExit): > > ? ? ? ? raise > > ? ? except: > > ? ? ? ? traceback.print_exc() > > > Next I have a client written in Ruby, which works. ?I am posting thing > > not to start a Ruby/Python flame war, but to simply prove that the > > server works and there are no weird networking issues that would > > prevent the Python client from working. ?The Ruby code is: > > > #!/usr/bin/env ruby > > require 'socket' > > > socket = UDPSocket.new > > socket.connect ARGV[0], ARGV[1] > > > puts "Enter data to transmit: " > > data = STDIN.gets.chomp > > > socket.send data, 0 > > puts "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > > loop do > > ? buf = socket.recvfrom(2048) > > ? puts buf.first > > end > > > When I start the server and run that, the output looks like this: > > > $ ch02/udp.rb 127.0.0.1 51423 > > Enter data to transmit: > > foobar > > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > > foobar > > > Now, when I try the python example: > > > #!/usr/bin/env python > > # UDP Example - Chapter 2 - udp.py > > > import socket, sys > > > host = sys.argv[1] > > textport = sys.argv[2] > > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > > try: > > ? ? port = int(textport) > > except ValueError: > > ? ? # That didn't work. ?Look it up instead. > > ? ? port = socket.getservbyname(textport, 'udp') > > > s.connect((host, port)) > > print "Enter data to transmit: " > > data = sys.stdin.readline().strip() > > s.sendall(data) > > > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > while 1: > > ? ? buf = s.recvfrom(2048) > > ? ? sys.stdout.write(buf[0]) > > > I don't ever get a response: > > > $ ch02/udp.py 127.0.0.1 51423 > > Enter data to transmit: > > foobar > > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > > > The server sees the message and says it has sent a reply: > > > Got message 'foobar' from ('127.0.0.1', 49623) > > Sent response to '('127.0.0.1', 49623)' > > > Any ideas as to why this doesn't work? > > It works on my PC (Python 2.6.2, Windows XP Pro, service pack 3). Doesn't work on a Mac with Python 2.5.1 or 2.6.2 unless you flush stdout or change it to print. Not sure why it would work on one platform and not the other. From Tom.sully at gmx.com Sun Jul 26 15:45:10 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 12:45:10 -0700 (PDT) Subject: Globalize all variables in function without knowing names. Message-ID: Does anyknow know how to do this? The reason I'm asking is because I'm trying to make a mini-programming language for fun, and need to work with variables. The user types 'set NAME "DATA" ' or whatever else the variable is going to be called/contain. I have: def variablework(varname, varset): exec("global "+varname) #DEFUNCT vars()[varname] = varset exec("print "+varname) #Debug purposes only This sets the variable (varname is the name of the variable, and varset is the data for it to contain) all fine and dandy, but it is local, and after some googling I realised that you can't globalised variables using exec. So I think the next best thing would be to find a way to globalize every variable in the function, but I don't know how to do this. If there isn't a way of doing this, can someone suggest another way of globalizing variable without knowing their names? From roy at panix.com Sun Jul 26 16:31:43 2009 From: roy at panix.com (Roy Smith) Date: Sun, 26 Jul 2009 16:31:43 -0400 Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a@p23g2000vbl.googlegroups.com> Message-ID: In article <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a at p23g2000vbl.googlegroups.com>, Paul Barry wrote: > In this case, I think he's trying to illustrate how the UDP example > compares to the TCP example from the previous section, which is why he > choose to keep the methods the same. I suppose, but I still think that's a confusing way to teach the differences between UDP and TCP. The fundamental differences are: 1) TCP makes sure what you send gets there (for reasonably large values of "makes sure"). UDP just tosses stuff out onto the network and hopes for the best. 2) TCP is connection-oriented. Connections must be explicitly set up before any data can flow, and once two sockets are connected, they can never be disconnected (short of tearing down the connection and starting again with two new sockets). With UPD, every time you send data, you have to specify where it's going, and a given socket can be talking to many different remote endpoints on a packet-by-packet basis. 3) TCP is a stream protocol, which hides record boundaries; it is free to combine and/or split up your data into whatever packet boundaries it wants, and there is no way for the receiving end to discover where the boundaries are. UDP, on the other hand, honors record boundaries; there is a one-to-one correspondence between each write (send, sendto) call at one and each read (recv, recvfrom) call at the other. It's difficult to demonstrate #1 in a simple example, so I'll skip that. But, #2 and #3 are easy. The example you gave uses connect() on a UDP socket. That's not pointing out the differences between UDP and TCP, that's hiding them behind a strange corner of the API. There really is no connection set up when you do connect() on a UDP socket; it's just a way of saying, "From now on, I'm going to skip telling you the destination of each individual datagram. Just use this destination from now on". It saves moving a bit of data around, but it's not fundamentally a connection. As for the stream/datagram distinction, sendall() is very much a stream creature. It says, "Just keep banging out packets until you've managed to transmit the whole buffer". That only makes sense in an environment where packet boundaries are meaningless. Using it with UDP is sort of like saying, "Use as many packets as it takes, as long as that number is one". It's not not a concept that makes sense in a datagram world. That it works at all on UDP is (IHMO) a mis-guided attempt at interface uniformity. If I were writing an example on how to use UDP, I would leave out the connect() call, and use sendto() instead of sendall(). Once the student has those down, that's the time to talk about weird corner cases like how connect() and sendall() can be twisted to work with UDP sockets. Or maybe not even bother. From Tom.sully at gmx.com Sun Jul 26 16:57:02 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 13:57:02 -0700 (PDT) Subject: Removing newlines from string on windows (without replacing) References: <7d2nfnF29tk6fU1@mid.uni-berlin.de> <2abd5ecb-6fed-4763-9563-2caf63ca3c15@r25g2000vbn.googlegroups.com> Message-ID: > (Even if it so happens sauce > will only ever have LFs at the end, it's still better to use the > method that is closest to your intended meaning.) Oh, you're right. I'll change that now. I suppose removing all endlines will be better, incase, somehow, endlines in the middle of the line arrises. > if it's not important to preserve trailing spaces The way I parse the file currently splits away all whitespace later on. While I could change this, I have a nice system worked out and my code is working, so it's not top priority. From python at mrabarnett.plus.com Sun Jul 26 16:59:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 26 Jul 2009 21:59:42 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: <4A6CC3BE.6060806@mrabarnett.plus.com> David Robinow wrote: >> This doesn't mean they're on the same level - in fact, if you read carefully >> you'll see my original post said as much: python attracted average >> programmers; php attracted mediocre programmers and even some >> non-programmers, which means that php is clearly a lesser language than >> python. > I'm a mediocre programmer. Does this mean I should switch to PHP? If you're a mediocre programmer who knows you're mediocre, but wants to improve and is willing to learn, then stick with Python. You'll get better. :-) From roy at panix.com Sun Jul 26 17:04:23 2009 From: roy at panix.com (Roy Smith) Date: Sun, 26 Jul 2009 17:04:23 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: In article , Raffael Cavallaro wrote: > php is clearly a lesser language than python. I'm as much of a Python bigot as anybody. Likewise, I put down php for all the sorts of theoretical reasons people have been putting it down. Not to mention that it looks like Perl, which is enough to make anybody go screaming in the other direction. However, it's hard to argue with success. A ton of very useful software has been written in php (including MediaWiki, which drives Wikipedia). One needs to have a very highly developed sense of theoretical purity to look down their noses at the language that drives one of the highest volume web sites on the planet. From gagsl-py2 at yahoo.com.ar Sun Jul 26 17:06:48 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 26 Jul 2009 18:06:48 -0300 Subject: invoke method on many instances References: Message-ID: En Sun, 26 Jul 2009 12:44:02 -0300, Aahz escribi?: > In article , > Gabriel Genellina wrote: >> En Thu, 23 Jul 2009 21:27:35 -0300, Aahz >> escribi?: >>> In article , >>> Gabriel Genellina wrote: >>>> >>>> NLMPI >>> >>> What? >> >> IHNFI > > What? Abbreviations are fine, but if someone asks you about one, it > would be nice to just expand it instead of replying with another > abbreviation. Ok, if you insist... NLMPI = Ni La M?s Puta Idea. IHNFI = I Have No Fucking Idea. -- Gabriel Genellina From martin at v.loewis.de Sun Jul 26 17:24:43 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 23:24:43 +0200 Subject: C-API, tp_dictoffset vs tp_members In-Reply-To: <7d3pnaF29o57gU1@mid.uni-berlin.de> References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> <7d2mu7F27a9tbU1@mid.uni-berlin.de> <4a6c9a7c$0$12532$9b622d9e@news.freenet.de> <7d3pnaF29o57gU1@mid.uni-berlin.de> Message-ID: <4A6CC99B.4070406@v.loewis.de> >>>>> I have a predefined set of members, some of which are optional. >>>> Having optional fields is also a good reason. >>> What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? >> Right - this works for optional objects. However, it can't possibly >> work for any of the other fields. > > I have two members, one T_OBJECT and one T_OBJECT_EX. Both are NULL and both > still appear in the dir() output. For one, accessing it returns 'None', for > the other it raises an exception. I would have expected an element that is > not accessible, not even 'None', to also not be visible in dir(). I understood your expectation already. I maintain my theory that it shows up because there is a descriptor on the class. >> I suppose that's because there will still be a descriptor for the >> field in the class. > > So is that intentional or is it dir() that could be improved there? It's probably both. > Should I file a bugreport? Only if you can provide a patch also. My guess is that when you have the patch completed, you might realize that it is not an improvement, despite achieving what you wanted to achieve. Regards, Martin From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Sun Jul 26 17:41:31 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Sun, 26 Jul 2009 17:41:31 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-26 17:04:23 -0400, Roy Smith said: > One > needs to have a very highly developed sense of theoretical purity to look > down their noses at the language that drives one of the highest volume web > sites on the planet. It's nothing to do with theoretical purity and everything to do with practicality. php has a limited range of utility. Within that range, it's clearly quite useful. Python is useful for a greater range of tasks which makes it a more generally useful (and in this sense, better) language. -- Raffael Cavallaro From devent at deventm.org Sun Jul 26 17:49:58 2009 From: devent at deventm.org (Erwin Mueller) Date: Mon, 27 Jul 2009 07:49:58 +1000 Subject: How to comment constant values? Message-ID: <200907270749.58845.devent@deventm.org> Hello, I'm new to Python (using it for two months) and I wonder how can I comment the const. values with the doc-strings. I.e. if I have code: >FRACTION_MIN = 1 >FRACTION_MAX = 10 > >class Fraction(collections.MutableSequence): > '''Model a fraction with denominators. > > It contains one ore more denomintors in a list for quick access. > The class is thread save, it uses a lock for all operations. > ''' > > def __init__(self): > # ... The class and the methods can have doc-strings, but what about the const. values, FRACTION_MIN and FRACTION_MAX? Thank you, Erwin. From clp2 at rebertia.com Sun Jul 26 18:06:29 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 26 Jul 2009 15:06:29 -0700 Subject: How to comment constant values? In-Reply-To: <200907270749.58845.devent@deventm.org> References: <200907270749.58845.devent@deventm.org> Message-ID: <50697b2c0907261506o4c7b0a04t2e5155833f62be7e@mail.gmail.com> On Sun, Jul 26, 2009 at 2:49 PM, Erwin Mueller wrote: > Hello, I'm new to Python (using it for two months) and I wonder how can I > comment the const. values with the doc-strings. I.e. if I have code: > >>FRACTION_MIN = 1 >>FRACTION_MAX = 10 >> >>class Fraction(collections.MutableSequence): >> ? ?'''Model a fraction with denominators. >> >> ? ?It contains one ore more denomintors in a list for quick access. >> ? ?The class is thread save, it uses a lock for all operations. >> ? ?''' >> >> ? ?def __init__(self): >> ? ? ? ? ? ? ? # ... > > The class and the methods can have doc-strings, but what about the const. > values, FRACTION_MIN and FRACTION_MAX? Only modules, classes, and functions/methods can have docstrings associated with them. For anything else, you have to use comments; or you can mention them in the docstrings of related things. Cheers, Chris -- http://blog.rebertia.com From Tom.sully at gmx.com Sun Jul 26 18:07:08 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 15:07:08 -0700 (PDT) Subject: Globalize all variables in function without knowing names. References: Message-ID: <5649dfb8-01a6-490d-bf30-b4a5206e644e@j21g2000yqe.googlegroups.com> Ah, thanks for that Dennis. I have a system already figured out where the user manually pre-enters all the variables into a .py file, which obviously isn't very efficient. The language isn't going to be used much except by me, so I suppose I'll just work with that until I look more into dictionaries, but I'm still beginning Python, and before that the only coding I did was a little bit of C++, so I'll wait up for that :P From hekevintran at gmail.com Sun Jul 26 18:24:25 2009 From: hekevintran at gmail.com (Kevin) Date: Sun, 26 Jul 2009 15:24:25 -0700 (PDT) Subject: Error in compiling Python on OS X Message-ID: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> I am trying to compile Python 2.6.2 on Mac OS X 10.5.7. I have Xcode 3.1.3 installed. The error I got is below. $ ./configure --prefix=/Users/me/python checking for --with-universal-archs... 32-bit checking MACHDEP... darwin checking EXTRAPLATDIR... $(PLATMACDIRS) checking machine type as reported by uname -m... i386 checking for --without-gcc... no checking for gcc... gcc checking for C compiler default output file name... configure: error: C compiler cannot create executables See `config.log' for more details. Please send any ideas. From rhodri at wildebst.demon.co.uk Sun Jul 26 18:32:30 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 26 Jul 2009 23:32:30 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On Sun, 26 Jul 2009 14:31:06 +0100, Raffael Cavallaro wrote: > On 2009-07-26 09:16:39 -0400, aahz at pythoncraft.com (Aahz) said: > >> There are plenty of expert C++ >> programmers who switched to Python; > > "plenty" is an absolute term, not a relative term. I sincerely doubt > that the majority of python users were formerly *expert* C++ programmers. Nitpicking like this doesn't help your case. >> your thesis only applies to the >> legions of people who found it difficult to learn C++ in the first >> place. > > No, my thesis applies to the overwhelming majority of programmers who > found it more difficult to *master* (i.e., not merely use) C++ as > opposed to mastering python. BTW, this is a *complement* not a dis; > python is a better language than C++ precisely because it is more > sensibly and elegantly designed than C++ and therefore easier to master. It is perhaps more accurate to say that Python was designed where C++ aggregated. C was fundamentally the wrong place to have started from, making C++ (as distinct from the subset of C++ that is really C) harder to learn, never mind master, than it really needed to be. > php represents the same process but farther down the ladder, as it were. > There's often a tradeoff between ease of mastery and power. python hits > a sweet spot for many tasks and many programmers, especially as compared > to C++ (or even lisp, which though more powerful than python is more > difficult to master. lisp beats C++ on both counts imho - more powerful > *and* easier to master). php hits a sweet spot only in a very restricted > domain. Beyond that, it is clearly inferior to python which has greater > power, but is more difficult to master. Fundamentally incorrect. PHP attracted many people because of where it lives in the web application structure -- the part of the language that was thought about very hard. Beyond that there's nothing much to master, so the whole ease vs power debate is rather pointless. -- Rhodri James *-* Wildebeest Herder to the Masses From bearophileHUGS at lycos.com Sun Jul 26 18:42:22 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Sun, 26 Jul 2009 15:42:22 -0700 (PDT) Subject: How to comment constant values? References: <200907270749.58845.devent@deventm.org> Message-ID: <5c1b5c1e-7677-4786-a026-b4825568cf5b@j32g2000yqh.googlegroups.com> Chris Rebert: > Only modules, classes, and functions/methods can have docstrings > associated with them. > For anything else, you have to use comments; or you can mention them > in the docstrings of related things. What about adding docstrings to other Python things, especially to variables? Bye, bearophile From deets at nospam.web.de Sun Jul 26 18:46:01 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 00:46:01 +0200 Subject: Error in compiling Python on OS X In-Reply-To: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> References: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> Message-ID: <7d44lbF29p3p7U1@mid.uni-berlin.de> Kevin schrieb: > I am trying to compile Python 2.6.2 on Mac OS X 10.5.7. I have Xcode > 3.1.3 installed. > > The error I got is below. > > $ ./configure --prefix=/Users/me/python Use a framework-build. > checking for --with-universal-archs... 32-bit > checking MACHDEP... darwin > checking EXTRAPLATDIR... $(PLATMACDIRS) > checking machine type as reported by uname -m... i386 > checking for --without-gcc... no > checking for gcc... gcc > checking for C compiler default output file name... > configure: error: C compiler cannot create executables > See `config.log' for more details. > > Please send any ideas. What happens if you write a simple test.c like this: int main() { return 0; } and compile it with gcc on the commandline? There must be a file called "a.out" afterwards. diez From deets at nospam.web.de Sun Jul 26 18:47:08 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 00:47:08 +0200 Subject: How to comment constant values? In-Reply-To: References: <200907270749.58845.devent@deventm.org> Message-ID: <7d44ndF29p3p7U2@mid.uni-berlin.de> Chris Rebert schrieb: > On Sun, Jul 26, 2009 at 2:49 PM, Erwin Mueller wrote: >> Hello, I'm new to Python (using it for two months) and I wonder how can I >> comment the const. values with the doc-strings. I.e. if I have code: >> >>> FRACTION_MIN = 1 >>> FRACTION_MAX = 10 >>> >>> class Fraction(collections.MutableSequence): >>> '''Model a fraction with denominators. >>> >>> It contains one ore more denomintors in a list for quick access. >>> The class is thread save, it uses a lock for all operations. >>> ''' >>> >>> def __init__(self): >>> # ... >> The class and the methods can have doc-strings, but what about the const. >> values, FRACTION_MIN and FRACTION_MAX? > > Only modules, classes, and functions/methods can have docstrings > associated with them. > For anything else, you have to use comments; or you can mention them > in the docstrings of related things. While this is technically true, writing docstrings to constants (module or classlevel) works when one uses tools such as epydoc to generate documentation. Diez From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 18:49:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 22:49:33 GMT Subject: Globalize all variables in function without knowing names. References: <5649dfb8-01a6-490d-bf30-b4a5206e644e@j21g2000yqe.googlegroups.com> Message-ID: <027cce66$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 15:07:08 -0700, Tom wrote: > Ah, thanks for that Dennis. I have a system already figured out where > the user manually pre-enters all the variables into a .py file, which > obviously isn't very efficient. The language isn't going to be used much > except by me, so I suppose I'll just work with that until I look more > into dictionaries, but I'm still beginning Python, and before that the > only coding I did was a little bit of C++, so I'll wait up for that :P Dictionaries are fundamental to Python and very useful. Not learning about them before starting to write code is like not learning about the accelerator pedal before starting to drive a car. You might be able to get somewhere by getting out and pushing, but it will be slow and exhausting. Have you done the tutorial? http://www.python.org/doc/current/tutorial/ -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 18:49:47 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 22:49:47 GMT Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a@p23g2000vbl.googlegroups.com> Message-ID: <027cce74$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 12:40:39 -0700, Paul Barry wrote: > On Jul 26, 12:53?pm, Roy Smith wrote: >> In article >> <148abf0f-c9e4-4156-8f16-e4e5615d3... at s6g2000vbp.googlegroups.com>, >> ?Paul Barry wrote: >> >> > host = '127.0.0.1' ? ? ? ? ? # Bind to all interfaces >> >> This threw me off the track for a little while. ?The comment is wrong! >> You're not binding to all interfaces, you're binding specifically to >> the loopback interface. ?For what you're doing, it's fine, but the >> comment is still wrong. > > You are right. The example in the book actually has > > host = '' > > So the comment is correct. In trying to debug what was going on, I put > 127.0.0.1 to see if that had something to do with it, which it didn't. Now I have to quote Aahz's email sig: "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. " --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 > But why not use > > print buf[0] > > instead of > > sys.stdin.write(buf[0]) > > that much, I don't know. print adds a trailing newline to the output, so the two lines aren't quite equivalent. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 18:58:34 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 22:58:34 GMT Subject: Help understanding the decisions *behind* python? - immutable objects References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: <027cd083$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 11:24:48 -0700, John Nagle wrote: > An interesting issue is Python objects, which are always mutable. > A "dict" of Python objects is allowed, but doesn't consider the contents > of the objects, just their identity (address). Only built-in types are > immutable; one cannot create a class of immutable objects. Yes you can, for some definition of "can": http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html Admittedly pure Python objects are only "cooperatively immutable". The immutability relies on the caller not going out of its way to break the instance, so you can mutate it if you work at it. One could, I suppose, try putting in complicated tricks to prevent that, but anything written in Python can ultimately be modified in Python. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 19:20:58 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 23:20:58 GMT Subject: How to comment constant values? References: <200907270749.58845.devent@deventm.org> <5c1b5c1e-7677-4786-a026-b4825568cf5b@j32g2000yqh.googlegroups.com> Message-ID: <027cd5c2$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 15:42:22 -0700, Bearophile wrote: > Chris Rebert: >> Only modules, classes, and functions/methods can have docstrings >> associated with them. >> For anything else, you have to use comments; or you can mention them in >> the docstrings of related things. > > What about adding docstrings to other Python things, especially to > variables? Python doesn't have variables, it has objects bound to names. Because __doc__ is looked up on the class, not the instance, there's not much point in adding a docstring to the instance (even for instances which allow attributes to be added). And because names are internally stored as strings, you can't add a docstring to the name. So what you ask for is impossible. But of course that could be changed, if there was consensus that what you ask for would be useful. But consider the consequences: # A thought-experiment >>> margin = 3 # allow a little bit of whitespace >>> margin.__doc__ = "Extra space in pixels." >>> help(x) => displays "Extra space in pixels." So far so good. But now consider two equally unacceptable situations: (1) If the docstring sticks to the object, then you get the docstring inappropriately showing up in places where it should: >>> number_of_widgets = len( [a, b, c] ) >>> help(number_of_widgets) => displays "Extra space in pixels." (2) If the docstring sticks to the name, then you get the docstring hanging around when you recycle the name to mean something else: >>> margin = 15/100.0 # our profit margin is 15% >>> help(margin) => displays "Extra space in pixels." Now, in a large program, one shouldn't recycle names like that, but in a large program, you're unlikely to programmatically look up docstrings. Docstrings, and the help() function, are particularly useful in an interactive session, which is precisely the time that you're likely to recycle names. In other words, it's currently impossible to bind docstrings to "variables" in Python, but even more fundamentally, the request is semantically incompatible with the Python name/object binding model. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 19:23:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 23:23:30 GMT Subject: How to comment constant values? References: <200907270749.58845.devent@deventm.org> <7d44ndF29p3p7U2@mid.uni-berlin.de> Message-ID: <027cd65b$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote: >> Only modules, classes, and functions/methods can have docstrings >> associated with them. >> For anything else, you have to use comments; or you can mention them in >> the docstrings of related things. > > While this is technically true, writing docstrings to constants (module > or classlevel) works when one uses tools such as epydoc to generate > documentation. I've never used epydoc, so I'm not sure what you mean. Presumably it uses source code analysis to detect: CONSTANT = 42 """This is a constant.""" even though the string is ignored by the compiler. Is that correct? -- Steven From andrew-newspost at areilly.bpc-users.org Sun Jul 26 19:28:53 2009 From: andrew-newspost at areilly.bpc-users.org (Andrew Reilly) Date: 26 Jul 2009 23:28:53 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <7d475kF29aga9U1@mid.individual.net> On Sun, 26 Jul 2009 09:31:06 -0400, Raffael Cavallaro wrote: > On 2009-07-26 09:16:39 -0400, aahz at pythoncraft.com (Aahz) said: > >> There are plenty of expert C++ >> programmers who switched to Python; > > "plenty" is an absolute term, not a relative term. I sincerely doubt > that the majority of python users were formerly *expert* C++ > programmers. > >> your thesis only applies to the >> legions of people who found it difficult to learn C++ in the first >> place. > > No, my thesis applies to the overwhelming majority of programmers who > found it more difficult to *master* (i.e., not merely use) C++ as > opposed to mastering python. BTW, this is a *complement* not a dis; > python is a better language than C++ precisely because it is more > sensibly and elegantly designed than C++ and therefore easier to master. Isn't it widely accepted that the number of people who have mastered C++ is about five? All of the rest of us just struggle... [I know enough of C++ to avoid it whenever I can, and to not use it for my own projects. I'm happy with a mix of C, python and lisp(or scheme).] -- Andrew From pinkeen at gmail.com Sun Jul 26 19:44:54 2009 From: pinkeen at gmail.com (Filip) Date: Sun, 26 Jul 2009 16:44:54 -0700 (PDT) Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> <88e8ea19-7106-406a-a531-88c2b6893987@y7g2000yqa.googlegroups.com> Message-ID: <0643e7ab-3136-4352-a849-a19882c3f9b2@c2g2000yqi.googlegroups.com> On Jul 23, 3:53?am, Paul McGuire wrote: > # You should use raw string literals throughout, as in: > # blah_re = re.compile(r'sljdflsflds') > # (note the leading r before the string literal). ?raw string > literals > # really help keep your re expressions clean, so that you don't ever > # have to double up any '\' characters. Thanks, I didn't know about that, updated my code. > # Attributes might be enclosed in single quotes, or not enclosed in > any quotes at all. > attr_re = re.compile('([\da-z]+?)\s*=\s*\"(.*?)\"', re.DOTALL | > re.UNICODE | re.IGNORECASE) Of course, you mean attribute's *value* can be enclosed in single/ double quotes? To be true, I haven't seen single quote variant in HTML lately but I checked it and it seems to be in the specs and it can be even quite useful (man learns something every day). Thank you for pointing that one out, I updated the code accordingly (just realized that condition check REs need an update too :/). As far as the lack of value quoting is concerned, I am not so sure I need this - It would significanly obfuscate my REs and this practice is rather deprecated, considered unsafe and I've seen it only in very old websites. > How would you extract data from a table? ?For instance, how would you > extract the data entries from the table at this URL:http://tf.nist.gov/tf-cgi/servers.cgi? ?This would be a good example > snippet for your module documentation. This really seems like a nice example. I'll surely explain it in my docs (examples are surely needed there ;)). > Try extracting all of the sldjlsfjd links from > yahoo.com, and see how much of what you expect actually gets matched. The library was used in my humble production environment, processing a few hundred thousand+ of pages and spitting out about 10000 SQL records so it does work quite good with a simple task like extracting all links. However, I can't really say that the task introduced enough diversity (there were only 9 different page templates) to say that the library is 'tested'... On Jul 26, 5:51 pm, John Machin wrote: > On Jul 23, 11:53 am, Paul McGuire wrote: > > > On Jul 22, 5:43 pm, Filip wrote: > > > # Needs re.IGNORECASE, and can have tag attributes, such as
> CLEAR="ALL"> > > line_break_re = re.compile('', re.UNICODE) > > Just in case somebody actually uses valid XHTML :-) it might be a good > idea to allow for
> > > # what about HTML entities defined using hex syntax, such as &#xxxx; > > amp_re = re.compile('\&(?![a-z]+?\;)', re.UNICODE | re.IGNORECASE) > > What about the decimal syntax ones? E.g. not only   and   > but also   > > Also, entity names can contain digits e.g. ¹ ¾ Thanks for pointing this out, I fixed that. Although it has very little impact on how the library performs its main task (I'd like to see some comments on that ;)). From aahz at pythoncraft.com Sun Jul 26 19:56:59 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 16:56:59 -0700 Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> Message-ID: In article <2a408da6-af57-45d0-a75f-4cbe384bb7f9 at s15g2000yqs.googlegroups.com>, Michal Kwiatkowski wrote: >On Jul 25, 10:00=A0pm, Jason Tackaberry wrote: >> On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: >>> >>> Is there a way to tell if a generator has been exhausted using pure >>> Python code? I've looked at CPython sources and it seems that >> >> Upon a cursory look, after a generator 'gen' is exhausted (meaning >> gen.next() has raised StopIteration), it seems that gen.gi_frame will be >> None. > >Only in Python 2.5 or higher though. I need to support Python 2.3 and >2.4 as well, sorry for not making that clear in the original post. Are you sure? It appears to work in Python 2.4; I don't have time to check 2.3. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From tjreedy at udel.edu Sun Jul 26 20:10:00 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 26 Jul 2009 20:10:00 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> Message-ID: Michal Kwiatkowski wrote: > The thing is I don't need the next item. I need to know if the > generator has stopped without invoking it. Write a one-ahead iterator class, which I have posted before, that sets .exhausted to True when next fails. tjr From nyamatongwe+thunder at gmail.com Sun Jul 26 20:25:46 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Mon, 27 Jul 2009 00:25:46 GMT Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: Mark Lawrence: > If my sleuthing is correct the problem is with these lines > > ilow *= self->itemSize; > ihigh *= self->itemSize; > > in GapBuffer_slice being computed before ilow and ihigh are compared to > anything. This particular bug was because ihigh is the maximum 32 bit integer 2147483647 so multiplying it by the integer item size (4) caused overflow. Adding an extra check fixes this: if (ihigh > self->lengthBody / self->itemSize) ihigh = self->lengthBody / self->itemSize; Committed a new version 1.02 and new downloads are available from Google code. http://code.google.com/p/gapbuffer/downloads/list Neil From jackdied at gmail.com Sun Jul 26 20:36:17 2009 From: jackdied at gmail.com (Jack Diederich) Date: Sun, 26 Jul 2009 20:36:17 -0400 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: On Mon, Jul 20, 2009 at 5:57 AM, casebash wrote: > Hi, > > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? It depends on how mutable you want your strings. There have been several patches proposed on python-dev over the years that do things like lazy concatenation and ropes. They were always shot down because the implementation or the semantics were very complicated. -Jack From tdelaney at avaya.com Sun Jul 26 20:53:33 2009 From: tdelaney at avaya.com (Delaney, Timothy (Tim)) Date: Mon, 27 Jul 2009 08:53:33 +0800 Subject: missing 'xor' Boolean operator In-Reply-To: <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> Message-ID: Mark Dickinson wrote: >> Since the 'and' and 'or' already return objects (and objects >> evaluate to true or false), then 'xor' should behave likewise, IMO. >> I expect that would be the case if it were ever added to the >> language. > > I'm not so sure. Did you ever wonder why the any() and all() > functions introduced in 2.5 return a boolean rather than returning > one of their arguments? (I did, and I'm still not sure what the > answer is.) Consider the case of any() and all() operating on an empty iterable. What type should they return? It is impossible in the case of any() and all() to always return one of the elements due to this edge case. Similarly, it is impossible in all cases for a boolean xor to return one of the arguments - if both arguments evaluate to a true (Something) value, xor must return a false (Nothing) result. Tim Delaney From bearophileHUGS at lycos.com Sun Jul 26 21:09:47 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Sun, 26 Jul 2009 18:09:47 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> Message-ID: William Dode': > I updated the script (python, c and java) with your unrolled version > + somes litle thinks. [...] > c 1.85s > gcj 2.15s > java 2.8s > python2.5 + psyco 3.1s > unladen-2009Q2 145s (2m45) > python2.5 254s (4m14s) > python3.1 300s (5m) > ironpython1.1.1 680s (11m20) Sorry for being late, I was away. In your last C version this code is useless because the C compiler is able to perform such simple optimization by itself (but probably Python isn't able, so if you want the code to be the similar in all versions it may be better to keep it): shift_0=shift[0]; shift_1=shift[1]; shift_2=shift[2]; shift_3=shift[3]; shift_4=shift[4]; shift_5=shift[5]; shift_6=shift[6]; shift_7=shift[7]; This part in the Python code is useless: shift_0 = shift[0] shift_1 = shift[1] shift_2 = shift[2] shift_3 = shift[3] shift_4 = shift[4] shift_5 = shift[5] shift_6 = shift[6] shift_7 = shift[7] Because later you copy values locally anyway: def solve(nb, x, y, SIDE=SIDE, SQR_SIDE=SQR_SIDE, circuit=circuit, shift_0=shift_0, shift_1=shift_1, shift_2=shift_2, shift_3=shift_3, shift_4=shift_4, shift_5=shift_5, shift_6=shift_6, shift_7=shift_7, ): So doing something like this is probably enough: def solve(nb, x, y, SIDE=SIDE, SQR_SIDE=SQR_SIDE, circuit=circuit, shift_0=shift[0], shift_1=shift[1], shift_2=shift[2], shift_3=shift[3], shift_4=shift[4], shift_5=shift[5], shift_6=shift[6], shift_7=shift[7], ): In low-level languages like C unrolling has to be done with care, to avoid slowing down the code. I have tried your latest C version using your compiler options, my MinGW based on GCC 4.3.2 produces a crash at runtime. Using LLVM-GCC it runs in 1.31 seconds. The D version is a bit less optimized than your last C versions, yet using DMD it runs in 1.08-1.10 seconds. Let's see if someone is able to write a C version faster than that D code :-) Have you have compiled/read my D version? In the D version you may have missed that I did use an extra trick: unsigned integers, so it needs just two tests to see if a number is in the 0-5, 0-5 square :-) Note that Pyd, the Python-D bridge, may work with the latest DMD version still (and it works if you use a bit older DMD compiler): http://pyd.dsource.org/ Bye, bearophile From greg at cosc.canterbury.ac.nz Sun Jul 26 21:12:26 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Mon, 27 Jul 2009 13:12:26 +1200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <7d4d6gF28qk7kU1@mid.individual.net> Terry Reedy wrote: > In Math and Python, a != is a comparison operator like <, Although Python extends the chaining principle to !=, this is somewhat questionable, because a < b and b < c implies a < c, but a != b and b != c does not imply a != c. I'm not sure I've ever seen a mathematician write a != b != c, but if I did, I would tend to think he meant to say that none of a, b, c are equal to any other. That's not what it means in Python, though. -- Greg From greg at cosc.canterbury.ac.nz Sun Jul 26 21:30:41 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Mon, 27 Jul 2009 13:30:41 +1200 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> Message-ID: <7d4e8nF2a9i83U1@mid.individual.net> Michal Kwiatkowski wrote: > The first generator isn't finished, it yielded 1 and None. Second one > is exhausted after yielding a single value (1). The problem is that, > under Python 2.4 or 2.3 both invocations will generate the same trace > output. This seems to be a deficiency in the trace mechanism. There really ought to be a 'yield' event to distinguish yields from returns. You could put in a feature request on python-dev concerning this. -- Greg From aahz at pythoncraft.com Sun Jul 26 22:44:24 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 19:44:24 -0700 Subject: invoke method on many instances References: Message-ID: In article , Gabriel Genellina wrote: > >Ok, if you insist... > >NLMPI = Ni La M?s Puta Idea. >IHNFI = I Have No Fucking Idea. Thanks! -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From nidlobm at hotmail.com Sun Jul 26 23:14:52 2009 From: nidlobm at hotmail.com (Michael Boldin) Date: Sun, 26 Jul 2009 23:14:52 -0400 Subject: SEC doc parsing Message-ID: I am looking for any parsing routines (written in python), for Security Exchange Commision (SEC) documents. These documents are on the SEC's EDGAR system (at ftp://ftp.sec.gov ) and I am especially interested in insider trading files known as Forms 3,4 and 5. _________________________________________________________________ NEW mobile Hotmail. Optimized for YOUR phone. Click here. http://windowslive.com/Mobile?ocid=TXT_TAGLM_WL_CS_MB_new_hotmail_072009 -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 23:18:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 27 Jul 2009 03:18:53 GMT Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> Message-ID: <027d0d84$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: > Michal Kwiatkowski wrote: > >> The thing is I don't need the next item. I need to know if the >> generator has stopped without invoking it. > > Write a one-ahead iterator class, which I have posted before, that sets > .exhausted to True when next fails. And hope that the generator doesn't have side-effects... -- Steven From robert.kern at gmail.com Mon Jul 27 00:11:12 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sun, 26 Jul 2009 23:11:12 -0500 Subject: PyQt GUI In-Reply-To: <4ee78bf1-476d-4e41-b9da-c20297aad861@13g2000prl.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> <4ee78bf1-476d-4e41-b9da-c20297aad861@13g2000prl.googlegroups.com> Message-ID: On 2009-07-26 07:04, Helvin wrote: > C:\Qt\VTKbin7\Wrapping\Python>python setup.py install > Traceback (most recent call last): > File "setup.py", line 138, in > raise "ERROR: Must specify BUILD_TYPE= on command > line." > TypeError: exceptions must be classes or instances, not str > > Is it just a small error with input format or something? Read the source, please: # The build type ('Release', 'Debug' etc.). If vtk_has_configuration_types # is true this must be set. It may be set on the command line by something # like 'BUILD_TYPE=Release'. For example:: # python setup.py install --prefix=D:\\Python23 BUILD_TYPE=Release -- 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 Mon Jul 27 00:16:24 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sun, 26 Jul 2009 23:16:24 -0500 Subject: How to comment constant values? In-Reply-To: <027cd65b$0$5185$c3e8da3@news.astraweb.com> References: <200907270749.58845.devent@deventm.org> <7d44ndF29p3p7U2@mid.uni-berlin.de> <027cd65b$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 2009-07-26 18:23, Steven D'Aprano wrote: > On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote: > >>> Only modules, classes, and functions/methods can have docstrings >>> associated with them. >>> For anything else, you have to use comments; or you can mention them in >>> the docstrings of related things. >> While this is technically true, writing docstrings to constants (module >> or classlevel) works when one uses tools such as epydoc to generate >> documentation. > > I've never used epydoc, so I'm not sure what you mean. Presumably it uses > source code analysis to detect: > > CONSTANT = 42 > """This is a constant.""" > > even though the string is ignored by the compiler. > > Is that correct? epydoc 3 can actually parse the file to grab comments with a "#:" marker: #: This is a constant. CONSTANT = 42 -- 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 dpresley at midiowa.net Mon Jul 27 00:20:51 2009 From: dpresley at midiowa.net (drednot57) Date: Sun, 26 Jul 2009 21:20:51 -0700 (PDT) Subject: len() should always return something References: Message-ID: <6ac8501e-b3ca-403b-8edb-c055b65bbff1@e11g2000yqo.googlegroups.com> To the best of my recollection, the len() function only applies to container objects; i. e. tuples, lists, strings, etc. an integer object is not a container, thus one receives an error when sending an int as an argument for len(). From tjreedy at udel.edu Mon Jul 27 02:02:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 27 Jul 2009 02:02:19 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <027d0d84$0$5185$c3e8da3@news.astraweb.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: > >> Michal Kwiatkowski wrote: >> >>> The thing is I don't need the next item. I need to know if the >>> generator has stopped without invoking it. >> Write a one-ahead iterator class, which I have posted before, that sets >> .exhausted to True when next fails. > > > And hope that the generator doesn't have side-effects... If run to exhastion, the same number of side-effects happen. The only difference is that they each happen once step happpen sooner. For reading a file that is irrelevant. Much else, and the iterator is not just an iterator. tjr From tjreedy at udel.edu Mon Jul 27 02:05:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 27 Jul 2009 02:05:55 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <7d4d6gF28qk7kU1@mid.individual.net> References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <7d4d6gF28qk7kU1@mid.individual.net> Message-ID: greg wrote: > Terry Reedy wrote: > >> In Math and Python, a> != is a comparison operator like <, > > Although Python extends the chaining principle to > !=, this is somewhat questionable, because > a < b and b < c implies a < c, but a != b and > b != c does not imply a != c. > > I'm not sure I've ever seen a mathematician > write a != b != c, but if I did, I would tend > to think he meant to say that none of a, b, > c are equal to any other. That's not what it > means in Python, though. However, == is transitive, and a == b == c is quite common. It would hardly do to have different rules for !=. Either we have a uniform rule for a compare_op b compare_ob c, as we do, or we have several fussy rules that would be hard to remember. From steve at REMOVE-THIS-cybersource.com.au Mon Jul 27 02:22:54 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 27 Jul 2009 06:22:54 GMT Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> Message-ID: <027d38a5$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 02:02:19 -0400, Terry Reedy wrote: > Steven D'Aprano wrote: >> On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: >> >>> Michal Kwiatkowski wrote: >>> >>>> The thing is I don't need the next item. I need to know if the >>>> generator has stopped without invoking it. >>> Write a one-ahead iterator class, which I have posted before, that >>> sets .exhausted to True when next fails. >> >> >> And hope that the generator doesn't have side-effects... > > If run to exhastion, the same number of side-effects happen. The only > difference is that they each happen once step happpen sooner. For > reading a file that is irrelevant. Much else, and the iterator is not > just an iterator. I believe the OP specifically said he needs to detect whether or not an iterator is exhausted, without running it to exhaustion, so you shouldn't assume that the generator has been exhausted. When it comes to side-effects, timing matters. For example, a generator which cleans up after it has run (deleting temporary files, closing sockets, etc.) will leave the environment in a different state if run to exhaustion than just before exhaustion. Even if you store the final result in a one-ahead class, you haven't saved the environment, and that may be significant. (Of course, it's possible that it isn't significant. Not all differences make a difference.) The best advice is, try to avoid side-effects, especially in generators. -- Steven From jayshree06comp at gmail.com Mon Jul 27 02:47:00 2009 From: jayshree06comp at gmail.com (jayshree) Date: Sun, 26 Jul 2009 23:47:00 -0700 (PDT) Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: On Jul 23, 5:48?pm, Piet van Oostrum wrote: > >>>>> jayshree (j) wrote: > >j> On Jul 21, 8:59?pm, Piet van Oostrum wrote: > >>> The recipient_public_key.pem file is the public key of the recipient > >>> which means the person that is going to receive the encrypted message. > >>> You should get it from the recipient him/herself or from some key store > >>> where s/he has deposited it. > > [...] > > >j> error coming like - IOError: [Errno 2] No such file or directory: > >j> 'recipient_public_key.pem' > >j> Is this not the inbuilt file. > >j> How should i create such type of file. > > You don't create it. See above. If you understand what is is you know > why it can't be builtin. If you don't understand it is better if you > first learn about OpenSSL, otherwise you run the risk to make serious > errors. > > If you are just experimenting to create a message for yourself then you > have to create the public key because you are then the recipient > yourself. That has been answered onhttp://stackoverflow.com/questions/1169798/m2crypto-package > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org import M2Crypto from M2Crypto import RSA,SSL def encrypt(): pk = open('my_key.public.pem', 'rb').read() rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub object. plaintext = 4545479545655576767767686688782344 msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding) print msg; encrypt() This is code i am refering. The Problem is coming with .pem file. I also asked this questions at http://stackoverflow.com/questions/1176864/problem-with-the-pem-file-closed which has not been answered . please help me out.... thanks From nagle at animats.com Mon Jul 27 03:06:36 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 00:06:36 -0700 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: <027cd083$0$5185$c3e8da3@news.astraweb.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> <027cd083$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4a6d514d$0$1641$742ec2ed@news.sonic.net> Steven D'Aprano wrote: > On Sun, 26 Jul 2009 11:24:48 -0700, John Nagle wrote: > >> An interesting issue is Python objects, which are always mutable. >> A "dict" of Python objects is allowed, but doesn't consider the contents >> of the objects, just their identity (address). Only built-in types are >> immutable; one cannot create a class of immutable objects. > > Yes you can, for some definition of "can": > > http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html > > > Admittedly pure Python objects are only "cooperatively immutable". Right. I've been thinking about this as a way of improving concurrency handling. The general idea is that objects shared across threads would have to be either be immutable or synchronized. Regular objects would be limited to a single thread. It's a path to multithread programs without a global lock. Needs more work to become a serious proposal. (It may not be worth the trouble; a few years ago we were hearing about how 64-core CPUs were going to become mainstream, but instead, the industry is going in the direction of the same compute power for less money and with less power consumption.) John Nagle From abhigyan_agrawal at in.ibm.com Mon Jul 27 03:11:42 2009 From: abhigyan_agrawal at in.ibm.com (abhi) Date: Mon, 27 Jul 2009 00:11:42 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX Message-ID: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> Hi, I am facing a problem using PyArg_ParseTuple() in my C-API extension. Here is a small repro of the function: static PyObject *parsetuple_test(PyObject *self, PyObject *args) { SQLUSMALLINT param_no = 0; PyObject *py_obj = NULL; if (!PyArg_ParseTuple(args, "Oi", &py_obj, ¶m_no)){ return NULL; } printf("%d\n", param_no); return NULL; } This function works fine and prints the correct value passed (param_no) when I test it on Linux or Windows. However, it always prints 0 (initial value defined in function) when I run it on AIX. I think the problem is with "Oi" argument, if I replace that with "OO" and specifically convert it integer/sqlsmallint. It works fine on AIX, but I want to use "i" and not "O". Any ideas on what is the reason behind the problem and how to fix this? Thanks, Abhigyan From jayshree06comp at gmail.com Mon Jul 27 03:28:50 2009 From: jayshree06comp at gmail.com (jayshree) Date: Mon, 27 Jul 2009 00:28:50 -0700 (PDT) Subject: open a file in python Message-ID: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() Please tell me how to open a file placed in any directory or in same directory. After opening this file i want to use the contain (public key ) for encryption thanks From metallourlante at gmail.com Mon Jul 27 03:40:09 2009 From: metallourlante at gmail.com (Alex) Date: Mon, 27 Jul 2009 00:40:09 -0700 (PDT) Subject: web page retrieve problems References: <16343e3d-1be4-422b-8bff-c07fa4aa4c54@j9g2000prh.googlegroups.com> Message-ID: <7dd16ce7-8442-4e67-a949-442b287e00dd@j21g2000yqe.googlegroups.com> On Jul 26, 8:57?am, golu wrote: > the following function retrieves pages from the web and saves them in > a specified dir. i want to extract the respective filenames from the > urls e.g the page code.google.com shud be saved as code-google.htm ?or > something similar. can u suggest me a way to do it Try with urllib.urlretrieve from standard lib: urllib.urlretrieve(url[, filename[, reporthook[, data]]])? Copy a network object denoted by a URL to a local file, if necessary. If the URL points to a local file, or a valid cached copy of the object exists, the object is not copied. Return a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object, possibly cached). Exceptions are the same as for urlopen(). From kushal.kumaran+python at gmail.com Mon Jul 27 04:09:26 2009 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Mon, 27 Jul 2009 13:39:26 +0530 Subject: open a file in python In-Reply-To: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: <1e364c4e0907270109u2aa8e702oa3ebaaab065f5e90@mail.gmail.com> On Mon, Jul 27, 2009 at 12:58 PM, jayshree wrote: > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > Please tell me how to open a file placed in any directory or in same > directory. > > After opening this file i want to use the contain (public key ) for > encryption > Does the code you've put into your message not read that file? If you get an exception, copy-paste in the traceback message you get into your mail. -- kushal From piet at cs.uu.nl Mon Jul 27 04:18:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 10:18:20 +0200 Subject: Looking for a dream language: sounds like Python to me. References: Message-ID: >>>>> Dotan Cohen (DC) wrote: >DC> Referring to this article: >DC> http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ >DC> The author, who is specifically looking for math-related functions, writes: >DC> """ >DC> The dream algorithm R&D tool would be similar to Matlab or Mathematica >DC> but could be compiled to fast, efficient binaries similar to ANSI C >DC> and would be available for all platforms. An integrated GUI builder >DC> similar to Visual Basic and integrated network support would be >DC> helpful. >DC> """ >DC> It looks to me like he is looking for Python. No fast, efficient binaries yet, and no integrated GUI builder. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From vesa.koppa at gmail.com Mon Jul 27 04:51:51 2009 From: vesa.koppa at gmail.com (=?ISO-8859-1?Q?Vesa_K=F6pp=E4?=) Date: Mon, 27 Jul 2009 11:51:51 +0300 Subject: How do I generate dia diagrams from python source code? In-Reply-To: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> References: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Message-ID: <4a6d6aa6$0$24756$4f793bc4@news.tdc.fi> Qauzzix wrote: > Greetings. > > Since I have been using dia to make my UML diagrams. I also found an > util named dia2code that generates python code from dia diagram. Now > that I have that option I really want to find a way to generate dia > diagram from existing code and/or maintain my diagrams. > > I have been googling like crazy trying to find a way but with no > luck. Anyone here know how to do this? > > Peace, > - Jakob Sv. Bjarnason Hi. There is this utility called autodia. http://www.aarontrevena.co.uk/opensource/autodia/ I think it does what you want. It is written in perl. It can read several languages and outputs dia diagrams. BTW, I found it by going to dia2code homepage and then to its links page. It is listed in there. -- Vesa From s.selvamsiva at gmail.com Mon Jul 27 04:53:33 2009 From: s.selvamsiva at gmail.com (S.Selvam) Date: Mon, 27 Jul 2009 14:23:33 +0530 Subject: Itext for Python In-Reply-To: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> References: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> Message-ID: On Sun, Jul 26, 2009 at 11:52 PM, Santhosh Kumar wrote: > Hi all, One of my cousin suggested me to do a IText > PDF converter for python. Actually I heard that there is > no separate IText converter either we have to go for jython or GCJ with > wrapper. Instead of wrapping, my plan is to create a separate module with > Python and I am thinking of doing this in as my final year project also. > Kindly give me your suggestion. > As for as i know, no pure python module exists for iText PDF generator, where iText seems to be a standard one for PDF related works.There are some other modules for pdf work but might not be as standard as iText.Surely some senior programmers will suggest you better way to proceed further. > With anticipation, > > SanthoshVKumar. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Yours, S.Selvam -------------- next part -------------- An HTML attachment was scrubbed... URL: From Tom.sully at gmx.com Mon Jul 27 04:58:48 2009 From: Tom.sully at gmx.com (Tom) Date: Mon, 27 Jul 2009 01:58:48 -0700 (PDT) Subject: Globalize all variables in function without knowing names. References: <5649dfb8-01a6-490d-bf30-b4a5206e644e@j21g2000yqe.googlegroups.com> <027cce66$0$5185$c3e8da3@news.astraweb.com> Message-ID: <50a401f0-b203-4dcd-baa9-22895a068f3b@r27g2000vbn.googlegroups.com> > Dictionaries are fundamental to Python and very useful. Not learning > about them before starting to write code is like not learning about the > accelerator pedal before starting to drive a car. Heh, I just looked at the actual tutorial for Dictionaries and I have to say they are very useful. Basically just arrays with strings(or any other data type, except lists) instead of numbers. I'll work on trying to integrate them into my language code as, like you said, user data... Then when a user requests a variable it just accessess the dictionary, that should work. Thanks! This is easier than I thought. > Have you done the tutorial? I did the first few sections, and then flipped back to it later on to find out what I needed to know as I coded applications. A bad idea, in retrospect, particilary as it slowed down my coding time alot. From hendrik at microcorp.co.za Mon Jul 27 05:05:06 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 27 Jul 2009 11:05:06 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: <200907271105.06692.hendrik@microcorp.co.za> On Sunday 26 July 2009 21:26:46 David Robinow wrote: > > I'm a mediocre programmer. Does this mean I should switch to PHP? I have searched, but I can find nothing about this mediocre language. Could you tell us more? - Hendrik From dickinsm at gmail.com Mon Jul 27 05:12:16 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Mon, 27 Jul 2009 02:12:16 -0700 (PDT) Subject: missing 'xor' Boolean operator References: Message-ID: <668280c4-6c52-4c2e-ab6f-9b3701399134@s15g2000yqs.googlegroups.com> On Jul 27, 1:53?am, "Delaney, Timothy (Tim)" wrote: > Mark Dickinson wrote: > >> Since the 'and' and 'or' already return objects (and objects > >> evaluate to true or false), then 'xor' should behave likewise, IMO. > >> I expect that would be the case if it were ever added to the > >> language. > > > I'm not so sure. ?Did you ever wonder why the any() and all() > > functions introduced in 2.5 return a boolean rather than returning > > one of their arguments? ?(I did, and I'm still not sure what the > > answer is.) > > Consider the case of any() and all() operating on an empty iterable. > What type should they return? > > It is impossible in the case of any() and all() to always return one of > the elements due to this edge case. Yes, of course; the alternative implementation I was thinking of was the one that I implemented eons ago for my own pre-2.5 code, where I defined any and all roughly as: any([x1, x2, x3, ...]) <-> False or x1 or x2 or x3 or ... all([x1, x2, x3, ...]) <-> True and x1 and x2 and x3 and ... At the time this seemed like the obvious choice, so I was a bit surprised when it was chosen to always return a bool instead in the official versions. Now that I'm older and wise^H^H^H^H, well, maybe just older, the pure bool version seems cleaner and less error-prone, even if it's mildly inconsistent with the behaviour of and and or. Mark From sjmachin at lexicon.net Mon Jul 27 05:25:19 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 27 Jul 2009 02:25:19 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX References: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> Message-ID: <635a7311-62e1-4da5-98ad-b62cfa150287@k30g2000yqf.googlegroups.com> On Jul 27, 5:11?pm, abhi wrote: > Hi, > ? ? I am facing a problem using PyArg_ParseTuple() in my C-API > extension. Here is a small repro of the function: > > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > { > ? ? ? ? SQLUSMALLINT param_no = 0; Sorry, my crystal ball is on the fritz. What is SQLUSMALLINT on Windows/Linux/AIX? What does printf("%d %d %d\n", sizeof(SQLUSMALLINT), sizeof(int), sizeof (long)); give you on each platform? > ? ? ? ? PyObject *py_obj = NULL; > > ? ? ? ? if (!PyArg_ParseTuple(args, "Oi", &py_obj, ¶m_no)){ > > ? ? ? ? ? ? ? ? return NULL; > ? ? ? ? } > ? ? ? ? printf("%d\n", param_no); > ? ? ? ? return NULL; > > } > > This function works fine and prints the correct value passed > (param_no) when I test it on Linux or Windows. However, it always > prints 0 (initial value defined in function) when I run it on AIX. Consider that 0 could have been plucked out of the air and has nothing to do with the initial value of param_no. Try initialising it to something distinctive, like 0xfedcba98. Use %08x format in the printf rather than %d. > > I think the problem is with "Oi" argument, if I replace that with "OO" > and specifically convert it integer/sqlsmallint. It works fine on AIX, > but I want to use "i" and not "O". > > Any ideas on what is the reason behind the problem and how to fix > this? I jump-started the crystal ball from the over-head tram wire and saw this: """SQLSMALLINT is 16 bits on all platforms, int/long is 32 bits on all platforms, first 2 are little-endian, AIX is bigendian, the "i" format expects SIGNED int /long, so param_no is getting (win:x, linux:x, aix:0) and the next 16 bits in memory are getting trashed with (win:0, linux:0, aix:x) (when x is your input smallish integer) and luckily this trashing didn't cause a crash. You can receive the result from the function in an int/long then have code to reject it if it won't fit into a SQLUSMALLINT, and cast it otherwise. If you don't care about overflow (NOT recommended), just use "H" format instead if "i" format""" Could be wrong, really shouldn't subject precision instruments to 415V DC, only OPs :-) N.B. to detect endianness : use print sys.byteorder HTH, John From jayshree06comp at gmail.com Mon Jul 27 05:37:47 2009 From: jayshree06comp at gmail.com (jayshree) Date: Mon, 27 Jul 2009 02:37:47 -0700 (PDT) Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: On Jul 27, 1:09?pm, Kushal Kumaran wrote: > On Mon, Jul 27, 2009 at 12:58 PM, jayshree wrote: > > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > > Please tell me how to open a file placed in any directory or in same > > directory. > > > After opening this file i want to use the contain (public key ) for > > encryption > > Does the code you've put into your message not read that file? ?If you > get an exception, copy-paste in the traceback message you get into > your mail. > > -- > kushal try: pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() except IOError: print "Error: can\'t find file or read data" else: print "reading from file successfully" >Still no error it gives .what to do? From m_tayseer82 at yahoo.com Mon Jul 27 05:39:30 2009 From: m_tayseer82 at yahoo.com (Mohammad Tayseer) Date: Mon, 27 Jul 2009 02:39:30 -0700 (PDT) Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: References: Message-ID: <468250.83203.qm@web31106.mail.mud.yahoo.com> You can generate binaries using py2exe, and you can create UI using Tkinter (which is very easy) or wxPython (which have GUI builders) Mohammad Tayseer http://spellcoder.com/blogs/tayseer ________________________________ From: Piet van Oostrum To: python-list at python.org Sent: Monday, July 27, 2009 11:18:20 AM Subject: Re: Looking for a dream language: sounds like Python to me. >>>>> Dotan Cohen (DC) wrote: >DC> Referring to this article: >DC> http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ >DC> The author, who is specifically looking for math-related functions, writes: >DC> """ >DC> The dream algorithm R&D tool would be similar to Matlab or Mathematica >DC> but could be compiled to fast, efficient binaries similar to ANSI C >DC> and would be available for all platforms. An integrated GUI builder >DC> similar to Visual Basic and integrated network support would be >DC> helpful. >DC> """ >DC> It looks to me like he is looking for Python. No fast, efficient binaries yet, and no integrated GUI builder. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Mon Jul 27 05:55:02 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 11:55:02 +0200 Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: <7d5brmF2a1alcU1@mid.uni-berlin.de> jayshree wrote: > On Jul 27, 1:09?pm, Kushal Kumaran > wrote: >> On Mon, Jul 27, 2009 at 12:58 PM, jayshree >> wrote: >> > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() >> >> > Please tell me how to open a file placed in any directory or in same >> > directory. >> >> > After opening this file i want to use the contain (public key ) for >> > encryption >> >> Does the code you've put into your message not read that file? ?If you >> get an exception, copy-paste in the traceback message you get into >> your mail. >> >> -- >> kushal > > > try: > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > except IOError: > print "Error: can\'t find file or read data" > else: > print "reading from file successfully" > >>Still no error it gives .what to do? Erm - so it doesn't give an error - which means you have successfully opened a file, and read it's contents. So what exactly is your problem? Diez From nogetfx at gmail.com Mon Jul 27 06:18:55 2009 From: nogetfx at gmail.com (Lars) Date: Mon, 27 Jul 2009 03:18:55 -0700 (PDT) Subject: Call function from another class Message-ID: <4b6b0e4b-b85b-43cf-a695-e3a722de01cc@e11g2000yqo.googlegroups.com> Hi I'm trying to make an simple image viewer in wxPython and rotate an image with a slider. The code at Pastebin is striped down at bit. The class Frame(wx.Frame) is the main window, the function "def CreateMenuBar" (l. 39) creates a menu, where the function "def onRotate (self,event):" (l. 43) is called. The slider appear in at 2nd window, class rotationSlider(wx.Frame). My problem is what to do, when the Okay button is pressed in the "rotationSlider" frame. How to get the slider value to my main window and how to call a function to do the rotation. I tried something with a global variable, but it would make no difference regarding the function problem. Amongst the other things I've tried is to bind the event of the 2nd/slider window closing (l. 46), but no. Python code at Pastebin: http://pastebin.com/m7c24ec34 So some help on Classes and etc., would be appreciated. /Lars From himanshu.garg at gmail.com Mon Jul 27 06:38:04 2009 From: himanshu.garg at gmail.com (++imanshu) Date: Mon, 27 Jul 2009 03:38:04 -0700 (PDT) Subject: _msi.Record object has no attribute 'GetString' Message-ID: <32e0b242-8635-40b5-a366-2c9967e64e1c@d4g2000yqa.googlegroups.com> The documentation (http://docs.python.org/library/msilib.html#record- objects) for msilib mentions the GetString() method on Record objects. However, the following snippet :- db = msilib.OpenDatabase(os.path.join(root, file), msilib.MSIDBOPEN_READONLY) view = db.OpenView('SELECT * FROM Property') view.Execute(None) r = view.Fetch() print dir(r) print r.GetString(0) gives me this error :- c:\>python msi.py ['ClearData', 'GetFieldCount', 'SetInteger', 'SetStream', 'SetString', __class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', __reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__'] Traceback (most recent call last): File "msi.py", line 18, in print r.GetString(0) AttributeError: '_msi.Record' object has no attribute 'GetString' Am I missing something? Thank You, Himanshu From deets at nospam.web.de Mon Jul 27 06:43:14 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 12:43:14 +0200 Subject: Call function from another class References: <4b6b0e4b-b85b-43cf-a695-e3a722de01cc@e11g2000yqo.googlegroups.com> Message-ID: <7d5em2F2anm94U1@mid.uni-berlin.de> Lars wrote: > Hi > I'm trying to make an simple image viewer in wxPython and rotate an > image with a slider. The code at Pastebin is striped down at bit. The > class Frame(wx.Frame) is the main window, the function "def > CreateMenuBar" (l. 39) creates a menu, where the function "def onRotate > (self,event):" (l. 43) is called. The slider appear in at 2nd window, > class rotationSlider(wx.Frame). > My problem is what to do, when the Okay button is pressed in the > "rotationSlider" frame. How to get the slider value to my main window > and how to call a function to do the rotation. I tried something with > a global variable, but it would make no difference regarding the > function problem. Amongst the other things I've tried is to bind the > event of the 2nd/slider window closing (l. 46), but no. > Python code at Pastebin: http://pastebin.com/m7c24ec34 > > So some help on Classes and etc., would be appreciated. If I'm not mistaken, in "doRotate" you should be able to refer to the to-be-closed dialog via self.frameRotate Now if you change the "sliderUpdate"-code to store that angle instead of just letting it fall out of scope, you could access that value through frameRotate: def sliderUpdate(self, event): angle=self.slider.GetValue() ROTATION_ANGLE=angle self.angle = angle self.Destroy() def doRotate(self,event): """Rotating image""" print self.frameRotate.angle Diez From abhigyan_agrawal at in.ibm.com Mon Jul 27 06:49:04 2009 From: abhigyan_agrawal at in.ibm.com (abhi) Date: Mon, 27 Jul 2009 03:49:04 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX References: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> <635a7311-62e1-4da5-98ad-b62cfa150287@k30g2000yqf.googlegroups.com> Message-ID: <43b1acda-b518-40ef-acde-145e28379775@r25g2000vbn.googlegroups.com> On Jul 27, 2:25?pm, John Machin wrote: > On Jul 27, 5:11?pm, abhi wrote: > > > Hi, > > ? ? I am facing a problem using PyArg_ParseTuple() in my C-API > > extension. Here is a small repro of the function: > > > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > > { > > ? ? ? ? SQLUSMALLINT param_no = 0; > > Sorry, my crystal ball is on the fritz. What is SQLUSMALLINT on > Windows/Linux/AIX? > What does > ? ? printf("%d %d %d\n", sizeof(SQLUSMALLINT), sizeof(int), sizeof > (long)); > give you on each platform? > > > ? ? ? ? PyObject *py_obj = NULL; > > > ? ? ? ? if (!PyArg_ParseTuple(args, "Oi", &py_obj, ¶m_no)){ > > > ? ? ? ? ? ? ? ? return NULL; > > ? ? ? ? } > > ? ? ? ? printf("%d\n", param_no); > > ? ? ? ? return NULL; > > > } > > > This function works fine and prints the correct value passed > > (param_no) when I test it on Linux or Windows. However, it always > > prints 0 (initial value defined in function) when I run it on AIX. > > Consider that 0 could have been plucked out of the air and has nothing > to do with the initial value of param_no. Try initialising it to > something distinctive, like 0xfedcba98. Use %08x format in the printf > rather than %d. > > > > > I think the problem is with "Oi" argument, if I replace that with "OO" > > and specifically convert it integer/sqlsmallint. It works fine on AIX, > > but I want to use "i" and not "O". > > > Any ideas on what is the reason behind the problem and how to fix > > this? > > I jump-started the crystal ball from the over-head tram wire and saw > this: """SQLSMALLINT is 16 bits on all platforms, int/long is 32 bits > on all platforms, first 2 are little-endian, AIX is bigendian, the "i" > format expects SIGNED int /long, so param_no is getting (win:x, > linux:x, aix:0) and the next 16 bits in memory are getting trashed > with (win:0, linux:0, aix:x) (when x is your input smallish integer) > and luckily this trashing didn't cause a crash. You can receive the > result from the function in an int/long then have code to reject it if > it won't fit into a SQLUSMALLINT, and cast it otherwise. If you don't > care about overflow (NOT recommended), just use "H" format instead if > "i" format""" > > Could be wrong, really shouldn't subject precision instruments to 415V > DC, only OPs :-) > > N.B. to detect endianness : use print sys.byteorder > > HTH, > John Hi, Thanks for the response. If I don't use SQLUSMALLINT and use int instead, then also I see the same problem. static PyObject *parsetuple_test(PyObject *self, PyObject *args) { int param_no = 0; .... .... } This also prints out 0 for AIX and actual value passed in other platforms. If I initialize param_no with some other value (e.g. 90), it prints out that value. - Abhigyan From piet at cs.uu.nl Mon Jul 27 07:08:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 13:08:27 +0200 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: >>>>> Paul Rubin (PR) wrote: >PR> Rob Knop writes: >>> Are there any python libraries that will take a public key in this >>> format and do RSA encoding on it? >PR> Try www.trevp.com/tlslite I have looked into tlslite and found no easy way to do this. Maybe I overlooked it. On the other hand with M2Crypto it isn't that hard. However, M2Crypto wants to load keys in PEM format rather than the DER format that the Java getEncoded() produces. It is not hard to convert this to PEM format: just Base64 encode it and put a header and trailer line around it. The conversion can be done with openssl or just on the fly in the Python code. Please note that the text to be encrypted must be smaller than the key size (at least 11 bytes smaller). You shouldn't encrypt large data with RSA anyway: it is too slow. Normally you would encrypt a session key with RSA and encrypt the data with the session key using a symmetric algorithms like AES. Here is an example script: If your OS doesn't have /dev/urandom you should probably seed OpenSSL's PRNG to be more secure. ------------------------------------------------------------------------ from M2Crypto import BIO, RSA pubkey = open("pubkey.der", 'rb').read() import base64 pubkey = base64.encodestring(pubkey) pubkey = '-----BEGIN PUBLIC KEY-----\n' + pubkey + '-----END PUBLIC KEY-----' bio = BIO.MemoryBuffer(pubkey) rsa = RSA.load_pub_key_bio(bio) plaintext = "This is my secret text." codetext = rsa.public_encrypt(plaintext, RSA.pkcs1_padding) with open("codetext", 'wb') as out: out.write(codetext) ------------------------------------------------------------------------ The following Java program decodes this with the corresponding private key: ------------------------------------------------------------------------ import java.security.*; import javax.crypto.*; import java.security.spec.*; import java.security.interfaces.*; import java.io.*; class Decrypt { public static void main(String[] args) { byte[] key = null; try { File file = new File("privkey.der"); FileInputStream keyfile = new FileInputStream(file); key = new byte[(int)file.length()]; DataInputStream dis= new DataInputStream(keyfile); dis.readFully(key); dis.close(); } catch(FileNotFoundException e) { System.out.println("Key file not found" + e); } catch (IOException e) { System.out.println("Can't read key file" + e); } byte[] codetext = null; try { File file = new File("codetext"); FileInputStream codefile = new FileInputStream(file); codetext = new byte[(int)file.length()]; DataInputStream dis = new DataInputStream(codefile); dis.readFully(codetext); dis.close(); } catch(FileNotFoundException e) { System.out.println("Code file not found" + e); } catch (IOException e) { System.out.println("Can't read code file" + e); } try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(key); RSAPrivateKey rsakey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec); Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); c.init(Cipher.DECRYPT_MODE, rsakey); byte[] decrypted = c.doFinal(codetext); System.out.println("Decrypted text:"); System.out.println(new String(decrypted)); } catch (NoSuchAlgorithmException e) { System.out.println("Wrong Algorithm " + e); } catch (InvalidKeyException e) { System.out.println("Invalid key " + e); } catch (IllegalBlockSizeException e) { System.out.println("Illegal block size" + e); } catch (NoSuchPaddingException e) { System.out.println("Illegal padding " + e); } catch (BadPaddingException e) { System.out.println("Bad padding " + e); } catch (InvalidKeySpecException e) { System.out.println("Invalid keyspec " + e); } } } ------------------------------------------------------------------------ -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From sean.kemplay at gmail.com Mon Jul 27 07:14:10 2009 From: sean.kemplay at gmail.com (Sean Kemplay) Date: Mon, 27 Jul 2009 04:14:10 -0700 (PDT) Subject: Free hosting for open source Python projects Message-ID: <97378723-7e67-415b-84bb-06d1675f2e2b@n11g2000yqb.googlegroups.com> Hi All, If anyone is looking for somewhere to host their Python project / modules etc. for free, please get in touch with me. There of course are some restraints on disk space and bandwidth but they should be sufficient for most projects. Your project must be open source. You will have access to both Django or Plone/Zope(shared). No PHP etc - this is a Python only host. Unfortunately I can't host everyone but if I can't host your particular project now, I may be able to at a later stage. Please contact me at "postmaster (at) pythonicity.co.uk". Our website is located at www.pythonicity.co.uk if you want to see what we do. Best Regards, Sean Kemplay From wilk at flibuste.net Mon Jul 27 07:16:15 2009 From: wilk at flibuste.net (William Dode) Date: 27 Jul 2009 11:16:15 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> Message-ID: <4a6d8c7f$0$19614$426a74cc@news.free.fr> On 27-07-2009, Bearophile wrote: > William Dode': >> I updated the script (python, c and java) with your unrolled version >> + somes litle thinks. > [...] >> c 1.85s >> gcj 2.15s >> java 2.8s >> python2.5 + psyco 3.1s >> unladen-2009Q2 145s (2m45) >> python2.5 254s (4m14s) >> python3.1 300s (5m) >> ironpython1.1.1 680s (11m20) > > Sorry for being late, I was away. > > In your last C version this code is useless because the C compiler is > able to perform such simple optimization by itself (but probably > Python isn't able, so if you want the code to be the similar in all > versions it may be better to keep it): I wanted so, specialy if it doesn't change a lot of the result (the difference is so small that i cannot see it)... ... > I have tried your latest C version using your compiler options, my > MinGW based on GCC 4.3.2 produces a crash at runtime. Maybe because of -msse2 ? > Using LLVM-GCC > it runs in 1.31 seconds. The D version is a bit less optimized than > your last C versions, yet using DMD it runs in 1.08-1.10 seconds. > Let's see if someone is able to write a C version faster than that D > code :-) > > Have you have compiled/read my D version? In the D version you may > have missed that I did use an extra trick: unsigned integers, so it > needs just two tests to see if a number is in the 0-5, 0-5 square :-) I didn't see, fine ! But the difference is also too small to see... > Note that Pyd, the Python-D bridge, may work with the latest DMD > version still (and it works if you use a bit older DMD compiler): > http://pyd.dsource.org/ I completly don't know anything about D... When i see the result of psyco or shedskin, i'm affraid i'll not look somewhere else soon ! However, i'd like to see a lisp implementation of this... bye -- William Dod? - http://flibuste.net Informaticien Ind?pendant From davea at ieee.org Mon Jul 27 07:21:44 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 07:21:44 -0400 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 In-Reply-To: References: <4A69A82D.8090301@ieee.org> Message-ID: <4A6D8DC8.40304@ieee.org> (forwarding this message, as the reply was off-list) Kim Hansen wrote: > 2009/7/24 Dave Angel : > >> It's not a question of how much disk space there is, but how much virtual >> space 32 bits can address. 2**32 is about 4 gig, and Windows XP reserves >> about half of that for system use. Presumably a 64 bit OS would have a much >> larger limit. >> >> Years ago I worked on Sun Sparc system which had much more limited shared >> memory access, due to hardware limitations. So 2gig seems pretty good to >> me. >> >> There is supposed to be a way to tell the Windows OS to only use 1 gb of >> virtual space, leaving 3gb for application use. But there are some >> limitations, and I don't recall what they are. I believe it has to be done >> globally (probably in Boot.ini), rather than per process. And some things >> didn't work in that configuration. >> >> DaveA >> >> >> > Hi Dave, > > In the related post I did on the numpy discussions: > > http://article.gmane.org/gmane.comp.python.numeric.general/31748 > > another user was kind enough to run my test program on both 32 bit and > 64 bit machines. On the 64 bit machine, there was no such limit, very > much in line with what you wrote. Adding the /3GB option in boot.ini > did not increase the available memory as well. Apparently, Python > needs to have been compiled in a way, which makes it possible to take > advantage of that switch and that is either not the case or I did > something else wrong as well. > > I acknowledge the explanation concerning the address space available. > Being an ignorant of the inner details of the implementation of mmap, > it seems like somewhat an "implementation detail" to me that such an > address wall is hit. There may be some good arguments from a > programming point of view and it may be a relative high limit as > compared to other systems but it is certainly at the low side for my > application: I work with data files typically 200 GB in size > consisting of datapackets each having a fixed size frame and a > variable size payload. To handle these large files, I generate an > "index" file consisting of just the frames (which has all the metadata > I need for finding the payloads I am interested in) and "pointers" to > where in the large data file each payload begins. This index file can > be up to 1 GB in size and at times I need to have access to two of > those at the same time (and then i hit the address wall). I would > really really like to be able to access these index files in a > read-only manner as an array of records on a file for which I use > numpy.memmap (which wraps mmap.mmap) such that I can pick a single > element, extract, e.g., every thousand value of a specific field in > the record using the convenient indexing available in Python/numpy. > Now it seems like I have to resort to making my own encapsulation > layer, which seeks to the relevant place in the file, reads sections > as bytestrings into recarrays, etc. Well, I must just get on with > it... > > I think it would be worthwhile specifying this 32 bit OS limitation in > the documentation of mmap.mmap, as I doubt I am the only one being > surprised about this address space limitation. > > Cheers, > Kim > > I agree that some description of system limitations should be included in a system-specific document. There probably is one, I haven't looked recently. But I don't think it belongs in mmap documentation. Perhaps you still don't recognize what the limit is. 32 bits can only address 4 gigabytes of things as first-class addresses. So roughly the same limit that's on mmap is also on list, dict, bytearray, or anything else. If you had 20 lists taking 100 meg each, you would fill up memory. If you had 10 of them, you might have enough room for a 1gb mmap area. And your code takes up some of that space, as well as the Python interpreter, the standard library, and all the data structures that are normally ignored by the application developer. BTW, there is one difference between mmap and most of the other allocations. Most data is allocated out of the swapfile, while mmap is allocated from the specified file (unless you use -1 for fileno). Consequently, if the swapfile is already clogged with all the other running applications, you can still take your 1.8gb or whatever of your virtual space, when much less than that might be available for other kinds of allocations. Executables and dlls are also (mostly) mapped into memory just the same as mmap. So they tend not to take up much space from the swapfile. In fact, with planning, a DLL needn't take up any swapfile space (well, a few K is always needed, realistically).. But that's a linking issue for compiled languages. DaveA From mdekauwe at gmail.com Mon Jul 27 07:24:31 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 04:24:31 -0700 (PDT) Subject: quickly looping over a 2D array? Message-ID: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Hi, I am new to python and I was wondering if there was a way to speed up the way I index 2D arrays when I need to check two arrays simultaneously? My current implementations is (using numpy) something like the following... for i in range(numrows): for j in range(numcols): if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, j] * some_other_value array_1[i, j] = some_new_value Many Thanks, Martin From michael at stroeder.com Mon Jul 27 07:35:15 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Mon, 27 Jul 2009 13:35:15 +0200 Subject: RSA cryptography between Python and Java In-Reply-To: References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: Piet van Oostrum wrote: > Please note that the text to be encrypted must be smaller than the key > size (at least 11 bytes smaller). You shouldn't encrypt large data with > RSA anyway: it is too slow. Normally you would encrypt a session key > with RSA and encrypt the data with the session key using a symmetric > algorithms like AES. I'd use CMS (AKA PKCS#7) for encrypted/signed data with X.509 certs. One should not invent another message format. Ciao, Michael. -- Michael Str?der E-Mail: michael at stroeder.com http://www.stroeder.com From __peter__ at web.de Mon Jul 27 07:42:48 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 13:42:48 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Message-ID: Martin wrote: > I am new to python and I was wondering if there was a way to speed up > the way I index 2D arrays when I need to check two arrays > simultaneously? My current implementations is (using numpy) something > like the following... > > for i in range(numrows): > for j in range(numcols): > > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > j] * some_other_value > array_1[i, j] = some_new_value array_1[(array_1 == some_value) | (array_2 >= array_1 * some_other_value)] = some_new_value maybe? From nogetfx at gmail.com Mon Jul 27 07:48:30 2009 From: nogetfx at gmail.com (Lars) Date: Mon, 27 Jul 2009 04:48:30 -0700 (PDT) Subject: Call function from another class References: <4b6b0e4b-b85b-43cf-a695-e3a722de01cc@e11g2000yqo.googlegroups.com> <7d5em2F2anm94U1@mid.uni-berlin.de> Message-ID: > If I'm not mistaken, in "doRotate" you should be able to refer to the > to-be-closed dialog via > self.frameRotate > > Now if you change the "sliderUpdate"-code to store that angle instead of > just letting it fall out of scope, you could access that value through > frameRotate: > > def sliderUpdate(self, event): > ? ? ? ? angle=self.slider.GetValue() > ? ? ? ? ROTATION_ANGLE=angle > ? ? ? ? self.angle = angle > ? ? ? ? self.Destroy() > > def doRotate(self,event): > ? ? """Rotating image""" > ? ? print self.frameRotate.angle Thanks for replying. If Bind is used in "onRotate": def onRotate(self,event): """Rotate image""" self.frameRotate = rotationSlider(parent=None, id=-1) self.Bind(wx.EVT_CLOSE,self.doRotate, self.frameRotate.Destroy) self.frameRotate.Show() Then i get: Traceback (most recent call last): File "viewer_sep.py", line 136, in onRotate self.Bind(wx.EVT_CLOSE,self.doRotate, self.frameRotate.Destroy) File "/usr/lib/python2.5/site-packages/wx-2.6-gtk2-unicode/wx/ _core.py", line 3629, in Bind id = source.GetId() AttributeError: 'function' object has no attribute 'GetId' My experience with ID is setting it to "-1". So I'm not sure if it is a wrong use of Bind or an wxPython error. /Lars From Slaunger at gmail.com Mon Jul 27 07:50:52 2009 From: Slaunger at gmail.com (Slaunger) Date: Mon, 27 Jul 2009 04:50:52 -0700 (PDT) Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 References: <4A69A82D.8090301@ieee.org> Message-ID: On 27 Jul., 13:21, Dave Angel wrote: > (forwarding this message, as the reply was off-list) > > > > Kim Hansen wrote: > > 2009/7/24 Dave Angel : > > >> It's not a question of how much disk space there is, but how much virtual > >> space 32 bits can address. ?2**32 is about 4 gig, and Windows XP reserves > >> about half of that for system use. ?Presumably a 64 bit OS would have a much > >> larger limit. > > >> Years ago I worked on Sun Sparc system which had much more limited shared > >> memory access, due to hardware limitations. ?So 2gig seems pretty good to > >> me. > > >> There is supposed to be a way to tell the Windows OS to only use 1 gb of > >> virtual space, leaving 3gb for application use. ?But there are some > >> limitations, and I don't recall what they are. ?I believe it has to be done > >> globally (probably in Boot.ini), rather than per process. ?And some things > >> didn't work in that configuration. > > >> DaveA > > > Hi Dave, > > > In the related post I did on the numpy discussions: > > >http://article.gmane.org/gmane.comp.python.numeric.general/31748 > > > another user was kind enough to run my test program on both 32 bit and > > 64 bit machines. On the 64 bit machine, there was no such limit, very > > much in line with what you wrote. Adding the /3GB option in boot.ini > > did not increase the available memory as well. Apparently, Python > > needs to have been compiled in a way, which makes it possible to take > > advantage of that switch and that is either not the case or I did > > something else wrong as well. > > > I acknowledge the explanation concerning the address space available. > > Being an ignorant of the inner details of the implementation of mmap, > > it seems like somewhat an "implementation detail" to me that such an > > address wall is hit. There may be some good arguments from a > > programming point of view and it may be a relative high limit as > > compared to other systems but it is certainly at the low side for my > > application: I work with data files typically 200 GB in size > > consisting of datapackets each having a fixed size frame and a > > variable size payload. To handle these large files, I generate an > > "index" file consisting of just the frames (which has all the metadata > > I need for finding the payloads I am interested in) and "pointers" to > > where in the large data file each payload begins. This index file can > > be up to 1 GB in size and at times I need to have access to two of > > those at the same time (and then i hit the address wall). I would > > really really like to be able to access these index files in a > > read-only manner as an array of records on a file for which I use > > numpy.memmap (which wraps mmap.mmap) such that I can pick a single > > element, extract, e.g., every thousand value of a specific field in > > the record using the convenient indexing available in Python/numpy. > > Now it seems like I have to resort to making my own encapsulation > > layer, which seeks to the relevant place in the file, reads sections > > as bytestrings into recarrays, etc. Well, I must just get on with > > it... > > > I think it would be worthwhile specifying this 32 bit OS limitation in > > the documentation of mmap.mmap, as I doubt I am the only one being > > surprised about this address space limitation. > > > Cheers, > > Kim > > I agree that some description of system limitations should be included > in a system-specific document. ?There probably is one, I haven't looked > recently. ?But I don't think it belongs in mmap documentation. > > Perhaps you still don't recognize what the limit is. ?32 bits can only > address 4 gigabytes of things as first-class addresses. ?So roughly the > same limit that's on mmap is also on list, dict, bytearray, or anything > else. ?If you had 20 lists taking 100 meg each, you would fill up > memory. ?If you had 10 of them, you might have enough room for a 1gb > mmap area. ?And your code takes up some of that space, as well as the > Python interpreter, the standard library, and all the data structures > that are normally ignored by the application developer. > > BTW, ?there is one difference between mmap and most of the other > allocations. ?Most data is allocated out of the swapfile, while mmap is > allocated from the specified file (unless you use -1 for fileno). ? > Consequently, if the swapfile is already clogged with all the other > running applications, you can still take your 1.8gb or whatever of your > virtual space, when much less than that might be available for other > kinds of allocations. > > Executables and dlls are also (mostly) mapped into memory just the same > as mmap. ?So they tend not to take up much space from the swapfile. ?In > fact, with planning, a DLL needn't take up any swapfile space (well, a > few K is always needed, realistically).. ?But that's a linking issue for > compiled languages. > > DaveA- Skjul tekst i anf?rselstegn - > > - Vis tekst i anf?rselstegn - I do understand the 2 GB address space limitation. However, I think I have found a solution to my original numpy.memmap problem (which spun off to this problem), and that is PyTables, where I can address 2^64 data on a 32 bit machine using hd5 files and thus circumventing the "implementation detail" of the intermedia 2^32 memory address problem in the numpy.memmap/mmap.mmap implementation. http://www.pytables.org/moin I just watched the first tutorial video, and that seems like just what I am after (if it works as well in practise at it appears to do). http://showmedo.com/videos/video?name=1780000&fromSeriesID=178 Cheers, Kim From piet at cs.uu.nl Mon Jul 27 07:51:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 13:51:34 +0200 Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() By the way, a PEM file is a text file, no reason to open it in binary mode. Just replace the 'rb' with 'r' or leave it out. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 27 08:19:49 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 14:19:49 +0200 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: >>>>> Michael Str?der (MS) wrote: >MS> Piet van Oostrum wrote: >>> Please note that the text to be encrypted must be smaller than the key >>> size (at least 11 bytes smaller). You shouldn't encrypt large data with >>> RSA anyway: it is too slow. Normally you would encrypt a session key >>> with RSA and encrypt the data with the session key using a symmetric >>> algorithms like AES. >MS> I'd use CMS (AKA PKCS#7) for encrypted/signed data with X.509 certs. One >MS> should not invent another message format. Yes but the original question was not about X.509 certs. Just the RSA public key. I don't know what the OP wants to do with it. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From drobinow at gmail.com Mon Jul 27 08:20:48 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 27 Jul 2009 07:20:48 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4A6CC3BE.6060806@mrabarnett.plus.com> References: <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> <4A6CC3BE.6060806@mrabarnett.plus.com> Message-ID: <4eb0089f0907270520n782503b1ndc34bcb1cf14a47e@mail.gmail.com> On 7/26/09, MRAB wrote: > David Robinow wrote: > > > This doesn't mean they're on the same level - in fact, if you read > carefully > > > you'll see my original post said as much: python attracted average > > > programmers; php attracted mediocre programmers and even some > > > non-programmers, which means that php is clearly a lesser language than > > > python. > > I'm a mediocre programmer. Does this mean I should switch to PHP? > If you're a mediocre programmer who knows you're mediocre, but wants to > improve and is willing to learn, then stick with Python. You'll get > better. :-) I doubt it. I remember when Guido moved to the U.S. and I've even used stdwin. I've actually gotten worse over the years. Old age does that. Nevertheless, I question the idea that a language that is easy to use (I don't know if PHP qualifies) is somehow inferior. From mdekauwe at gmail.com Mon Jul 27 08:27:11 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 05:27:11 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Message-ID: <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > I am new to python and I was wondering if there was a way to speed up > > the way I index 2D arrays when I need to check two arrays > > simultaneously? My current implementations is (using numpy) something > > like the following... > > > for i in range(numrows): > > for j in range(numcols): > > > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > > j] * some_other_value > > array_1[i, j] = some_new_value > > array_1[(array_1 == some_value) | (array_2 >= array_1 * some_other_value)] = > some_new_value > > maybe? So I tried... band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 which led to ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() so not sure that works? From rlichlighter at gmail.com Mon Jul 27 08:29:23 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 05:29:23 -0700 (PDT) Subject: Convert raw binary file to ascii Message-ID: I have a memory dump from a machine I am trying to analyze. I can view the file in a hex editor to see text strings in the binary code. I don't see a way to save these ascii representations of the binary, so I went digging into Python to see if there were any modules to help. I found one I think might do what I want it to do - the binascii module. Can anyone describe to me how to convert a raw binary file to an ascii file using this module. I've tried? Boy, I've tried. Am I correct in assuming I can get the converted binary to ascii text I see in a hex editor using this module? I'm new to this forensics thing and it's quite possible I am mixing technical terms. I am not new to Python, however. Thanks for your help. From electriclightheads at gmail.com Mon Jul 27 08:29:58 2009 From: electriclightheads at gmail.com ('2+) Date: Mon, 27 Jul 2009 21:29:58 +0900 Subject: wave.setparams((2, 2, 44100, 44100 * 2 * 10, "NONE", "not compressed")) became more than 30secs Message-ID: <1078018b0907270529l676fdb75k4c1f42ef0a2e7d0a@mail.gmail.com> thanx to rob .. who gave me an example of how to use the WAVE lib now am on my way to use it as simple as i can i wrote an oil.py the instance of which will behave like an oscillator which constantly generates 7 different wave forms (non fixed) i thought this code might produce 10secs of wave file but the result was longer than 30secs was testing if i could use the WAVE lib withount taking advantage of StringIO nor struct but maybe am doing totally wrong? --8<--- import oil import wave a = oil.Sa() w = wave.open("current.wav", "w") w.setparams((2, 2, 44100, 44100 * 2 * 10, "NONE", "not compressed")) for gas in range(44100 * 10): a.breath() r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) w.writeframes(hex(r)) w.writeframes(hex(l)) w.close() -- SaRiGaMa's Oil Vending Orchestra is podcasting: http://sarigama.namaste.jp/podcast/rss.xml and supplying oil.py for free: http://oilpy.blogspot.com/ From benjamin.kaplan at case.edu Mon Jul 27 08:41:23 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 27 Jul 2009 08:41:23 -0400 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: On Sun, Jul 26, 2009 at 2:24 PM, John Nagle wrote: > Beni Cherniavsky wrote: >> >> On Jul 22, 9:36 am, Hendrik van Rooyen >> wrote: >>> >>> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >>> >>>> My guess is that it was probably for optimization reasons long ago. >>>> I've never heard a *good* reason why Python needs both. >>> >>> The good reason is the immutability, which lets you use >>> a tuple as a dict key. >> >> On Jul 22, 9:36 am, Hendrik van Rooyen >> wrote: >>> >>> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >>> >>>> My guess is that it was probably for optimization reasons long ago. >>>> I've never heard a *good* reason why Python needs both. >>> >>> The good reason is the immutability, which lets you use >>> a tuple as a dict key. >>> >> The *technical* reason is immutability for dict keys. >> Dict could allow mutable objects as keys by comparing *by value*, >> making a copy on insertion and hashing the current value on lookup. >> Prior art: the 2.3 sets module allows mutable Sets as elements in >> Sets, by making ImmutableSet copies on insertion, and hashing Sets as >> if they are temporarily immutable on lookup. >> >> This inspired PEP 351 and ambitious proposals to expand the approach >> to all Python with a copy-on-write scheme. ?But these ideas were >> rejected, and the 2.4 builtin sets only allow frozenset elements. >> Half the reason is technical: copy-on-write and harder than it sounds, >> and without it you pay a performance price. >> >> But the deeper reason is style: immutable types are convenient! >> The allow a pure-functional style of code, which can be simpler. >> Of course, sometimes an imperative style is simpler. ?Depends on the >> problem. > > ? ?An interesting issue is Python objects, which are always mutable. > A "dict" of Python objects is allowed, but doesn't consider > the contents of the objects, just their identity (address). Only > built-in types are immutable; one cannot create a class of immutable > objects. > So things like "frozenset" had to be built into the language. > A tuple is really a frozen list. ?Arguably, frozen objects > should have been a general concept. ?Conceptually, they're > simple - once "__init__" has run, there can be no more changes > to fields of the object. > > ? ?Compare the C++ approach, where you can have a "map" of > any object that defines the "<" operator. ?Python has > "__cmp__" for objects, but built-in dictionaries don't use it. > Of course, one could create a "map" class in Python that > uses "__cmp__", which would allow dictionary-type operations > on arbitrary objects. Python dictionaries are hash maps so they use hashes, not comparisons, to store objects. By default the hash is equal to the object's identity but all you need to do to change it is define your own __hash__ method. If you were to make a C++ hash map, it wouldn't use comparisons either. > > ? ?There are some interesting uses for immutable objects in > multithreaded programs. ?They can be shared between threads > without locking. ?If it weren't for the infamous Global > Interpreter Lock, which basically limits Python programs to using > one CPU at a time, Python would have to deal with locking > in a more advanced way. ?A system where immutable objects > can be shared and passed between threads, and mutable objects > must be "synchronized" (in the Java sense) to be shared would > be useful. > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?John Nagle > -- > http://mail.python.org/mailman/listinfo/python-list > From rashidsdpk at gmail.com Mon Jul 27 08:45:33 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Mon, 27 Jul 2009 05:45:33 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10.more http://0nanotechnology0.blogspot.com/ From __peter__ at web.de Mon Jul 27 08:46:54 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 14:46:54 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> Message-ID: Martin wrote: > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: >> Martin wrote: >> > I am new to python and I was wondering if there was a way to speed up >> > the way I index 2D arrays when I need to check two arrays >> > simultaneously? My current implementations is (using numpy) something >> > like the following... >> >> > for i in range(numrows): >> > for j in range(numcols): >> >> > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, >> > j] * some_other_value >> > array_1[i, j] = some_new_value >> >> array_1[(array_1 == some_value) | (array_2 >= array_1 * >> some_other_value)] = some_new_value >> >> maybe? > > So I tried... > > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > which led to > > ValueError: The truth value of an array with more than one element is > ambiguous. Use a.any() or a.all() > > so not sure that works? Copy and paste -- or replace "or" with "|". From mdekauwe at gmail.com Mon Jul 27 08:55:15 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 05:55:15 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> Message-ID: <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> On Jul 27, 1:46 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: > >> Martin wrote: > >> > I am new to python and I was wondering if there was a way to speed up > >> > the way I index 2D arrays when I need to check two arrays > >> > simultaneously? My current implementations is (using numpy) something > >> > like the following... > > >> > for i in range(numrows): > >> > for j in range(numcols): > > >> > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > >> > j] * some_other_value > >> > array_1[i, j] = some_new_value > > >> array_1[(array_1 == some_value) | (array_2 >= array_1 * > >> some_other_value)] = some_new_value > > >> maybe? > > > So I tried... > > > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > > which led to > > > ValueError: The truth value of an array with more than one element is > > ambiguous. Use a.any() or a.all() > > > so not sure that works? > > Copy and paste -- or replace "or" with "|". apologies - I mistook that for a type for "or" I now get the following error... ValueError: shape mismatch: objects cannot be broadcast to a single shape From __peter__ at web.de Mon Jul 27 09:06:17 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 15:06:17 +0200 Subject: Convert raw binary file to ascii References: Message-ID: r2 wrote: > I have a memory dump from a machine I am trying to analyze. I can view > the file in a hex editor to see text strings in the binary code. I > don't see a way to save these ascii representations of the binary, so > I went digging into Python to see if there were any modules to help. > > I found one I think might do what I want it to do - the binascii > module. Can anyone describe to me how to convert a raw binary file to > an ascii file using this module. I've tried? Boy, I've tried. That won't work because a text editor doesn't need any help to convert the bytes into characters. If it expects ascii it just will be puzzled by bytes that are not valid ascii. Also, it will happily display byte sequences that are valid ascii, but that you as a user will see as gibberish because they were meant to be binary data by the program that wrote them. > Am I correct in assuming I can get the converted binary to ascii text > I see in a hex editor using this module? I'm new to this forensics > thing and it's quite possible I am mixing technical terms. I am not > new to Python, however. Thanks for your help. Unix has the "strings" commandline tool to extract text from a binary. Get hold of a copy of the MinGW tools if you are on windows. Peter From nemelis at gmail.com Mon Jul 27 09:12:39 2009 From: nemelis at gmail.com (Harry Ebbers) Date: Mon, 27 Jul 2009 06:12:39 -0700 (PDT) Subject: Testoob: How do you use testoob.collector_from_globals / collector_from_modules? Message-ID: <1a8582cc-a7fc-4b1e-b1b2-d23e20ad6022@18g2000yqa.googlegroups.com> Hi, For a project I'm creating unittests using testoob. When all tests are in a single file there is no problem if __name__ == '__main__': testoob.main() does the trick as usual. But for a number of python-applications they have asked me to group unittests in different files, based on common functionality (e.g. GUI, main-application, etcetera). Ican get it to work by importing all unittestclasses in the following way and than use the normal if __name__ == '__main__' construction (from GUI import GUItests from APP import APPtests if __name__ == '__main__': testoob.main()) But looking at the future I would like to use testsuites. On the internet I found the functions testoob.collector_from_globals and collector_from_modules which one can use to 'easily' create testsuites which can be used with testoob. But no examples on how to use them can be found. Can anybody supply me with a working example? TIA Harry Ebbers From __peter__ at web.de Mon Jul 27 09:17:58 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 15:17:58 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> Message-ID: Martin wrote: > On Jul 27, 1:46 pm, Peter Otten <__pete... at web.de> wrote: >> Martin wrote: >> > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: >> >> Martin wrote: >> >> > I am new to python and I was wondering if there was a way to speed >> >> > up the way I index 2D arrays when I need to check two arrays >> >> > simultaneously? My current implementations is (using numpy) >> >> > something like the following... >> >> >> > for i in range(numrows): >> >> > for j in range(numcols): >> >> >> > if array_1[i, j] == some_value or array_2[i, j] >= >> >> > array_1[i, >> >> > j] * some_other_value >> >> > array_1[i, j] = some_new_value >> >> >> array_1[(array_1 == some_value) | (array_2 >= array_1 * >> >> some_other_value)] = some_new_value >> >> >> maybe? >> >> > So I tried... >> >> > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 >> >> > which led to >> >> > ValueError: The truth value of an array with more than one element is >> > ambiguous. Use a.any() or a.all() >> >> > so not sure that works? >> >> Copy and paste -- or replace "or" with "|". > > apologies - I mistook that for a type for "or" > > I now get the following error... > > ValueError: shape mismatch: objects cannot be broadcast to a single > shape It seems array_1 and array_2 have a -- dada! -- different shape. Assuming array_1 is the smaller one: numrows, numcols = array_1.shape array_1[(array_1 == some_value) | (array_2[:numrows,:numcols] >= array_1 * some_other_value)] = some_new_value There may be other options, but I'm not a numpy expert. Peter From constant.beta at gmail.com Mon Jul 27 09:46:01 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Mon, 27 Jul 2009 06:46:01 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> Message-ID: <1c8ae01e-2e9c-497c-9f8d-408f56f9c0bd@g31g2000yqc.googlegroups.com> On Jul 27, 1:56 am, a... at pythoncraft.com (Aahz) wrote: > >> Upon a cursory look, after a generator 'gen' is exhausted (meaning > >> gen.next() has raised StopIteration), it seems that gen.gi_frame will be > >> None. > > >Only in Python 2.5 or higher though. I need to support Python 2.3 and > >2.4 as well, sorry for not making that clear in the original post. > > Are you sure? It appears to work in Python 2.4; I don't have time to > check 2.3. No, it does not work in Python 2.4. gi_frame can be None only in Python 2.5 and higher. Via "What?s New in Python 2.5" (http://docs.python.org/whatsnew/ 2.5.html): """ Another even more esoteric effect of this change: previously, the gi_frame attribute of a generator was always a frame object. It?s now possible for gi_frame to be None once the generator has been exhausted. """ Cheers, mk From sjmachin at lexicon.net Mon Jul 27 09:57:26 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 27 Jul 2009 06:57:26 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX References: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> <635a7311-62e1-4da5-98ad-b62cfa150287@k30g2000yqf.googlegroups.com> <43b1acda-b518-40ef-acde-145e28379775@r25g2000vbn.googlegroups.com> Message-ID: <281196a8-c392-4818-b2fb-6b60c84aba2a@j21g2000vbn.googlegroups.com> On Jul 27, 8:49?pm, abhi wrote: > On Jul 27, 2:25?pm, John Machin wrote: > > > > > On Jul 27, 5:11?pm, abhi wrote: > > > > Hi, > > > ? ? I am facing a problem using PyArg_ParseTuple() in my C-API > > > extension. Here is a small repro of the function: > > > > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > > > { > > > ? ? ? ? SQLUSMALLINT param_no = 0; > > > Sorry, my crystal ball is on the fritz. What is SQLUSMALLINT on > > Windows/Linux/AIX? > > What does > > ? ? printf("%d %d %d\n", sizeof(SQLUSMALLINT), sizeof(int), sizeof > > (long)); > > give you on each platform? And the answer was: what? > ? ? Thanks for the response. If I don't use SQLUSMALLINT and use int > instead, then also I see the same problem. > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > ?{ > ? ? ? ? int param_no = 0; > .... > ....} > > This also prints out 0 for AIX and actual value passed in other > platforms. If I initialize param_no with some other value (e.g. 90), > it prints out that value. Please run the following on AIX with Python 2.5: import sys print sys.byteorder, sys.maxint print sys.version and tell us the output. Does the C compiler that you are using for the extension correspond with whatever is mentioned in sys.version about the C compiler used to compile Python 2.5? C code: Please add/change as follows: int dummy1 = 0xaaaabbbb; int param_no = 0xccccdddd; int dummy2 = 0xeeeeffff; ... printf("%08x %08x %08x\n", dummy1, param_no, dummy2); printf("%d %d\n", sizeof(int), sizeof(long)); Try this on AIX (a) with no optimisation (b) whatever O level you have been using. Try it with (i) small positive input (i) integer > 65535 Take a copy of your extension and cut out everything that is not part of executing this function i.e. apart from this function (with the dummies and printfs) you should have only a few includes, a table defining just 1 callable function, and the minimal module initialisation code. Check that this cut down version works on Windows and Linux and still fails on AIX. Then try it without the first ("O") arg. If it still fails on AIX, lose the first arg. Publish the minimal failing code, the instructions to build the .so, and a transcript of the Python interactive session showing calling the function and the results. Cheers, John From mdekauwe at gmail.com Mon Jul 27 10:01:18 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 07:01:18 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> Message-ID: <8b78b47f-799e-4d37-bc28-d1ed349362c9@k6g2000yqn.googlegroups.com> On Jul 27, 2:17 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > On Jul 27, 1:46 pm, Peter Otten <__pete... at web.de> wrote: > >> Martin wrote: > >> > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: > >> >> Martin wrote: > >> >> > I am new to python and I was wondering if there was a way to speed > >> >> > up the way I index 2D arrays when I need to check two arrays > >> >> > simultaneously? My current implementations is (using numpy) > >> >> > something like the following... > > >> >> > for i in range(numrows): > >> >> > for j in range(numcols): > > >> >> > if array_1[i, j] == some_value or array_2[i, j] >= > >> >> > array_1[i, > >> >> > j] * some_other_value > >> >> > array_1[i, j] = some_new_value > > >> >> array_1[(array_1 == some_value) | (array_2 >= array_1 * > >> >> some_other_value)] = some_new_value > > >> >> maybe? > > >> > So I tried... > > >> > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > >> > which led to > > >> > ValueError: The truth value of an array with more than one element is > >> > ambiguous. Use a.any() or a.all() > > >> > so not sure that works? > > >> Copy and paste -- or replace "or" with "|". > > > apologies - I mistook that for a type for "or" > > > I now get the following error... > > > ValueError: shape mismatch: objects cannot be broadcast to a single > > shape > > It seems array_1 and array_2 have a -- dada! -- different shape. > Assuming array_1 is the smaller one: > > numrows, numcols = array_1.shape > array_1[(array_1 == some_value) | (array_2[:numrows,:numcols] >= array_1 * > some_other_value)] = some_new_value > > There may be other options, but I'm not a numpy expert. > > Peter My mistake - the incorrect size in the arrays was my error. The statement works now, but it doesn't give the same results as my original logic, strangely!? in my logic: data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') for i in range(numrows): for j in range(numcols): if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: data[i,j] = 0 else: data[i,j] = 1 to do the same with what you suggested... data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') data[(band3 == 255) | (band6 >= band3 * factor)] = 0 Thanks From santhosh.vkumar at gmail.com Mon Jul 27 10:04:48 2009 From: santhosh.vkumar at gmail.com (santhoshvkumar) Date: Mon, 27 Jul 2009 07:04:48 -0700 (PDT) Subject: iText for Python Message-ID: <505f2bc1-cbfb-49a1-a7ef-0b6c357750a7@q35g2000vbi.googlegroups.com> Hi all, One of my cousin suggested me to do a IText PDF converter for python. Actually I heard that there is no separate IText converter either we have to go for jython or GCJ with wrapper. Instead of wrapping, my plan is to create a separate module with Python and I am thinking of doing this in as my final year project also. Kindly give me your suggestion. With anticipation, SanthoshVKumar. From jeanmichel at sequans.com Mon Jul 27 10:05:11 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 27 Jul 2009 16:05:11 +0200 Subject: Override a method but inherit the docstring In-Reply-To: <87r5wggm0y.fsf@benfinney.id.au> References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <4A6DB417.5080601@sequans.com> Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > Ideally there would be a way to specify that the docstring should be > inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > > but that violates DRY (the association between BarGonk and FooGonk is > being repeated), puts the docstring assignment awkwardly after the end > of the method instead of at the beginning where docstrings normally go, > and reads poorly besides. > > What is the most Pythonic, DRY-adherent, and preferably least-ugly > approach to override a method, but have the same docstring on both > methods? > > I am using epydoc and if the docstring is present only in the baseclass method, it will repeat the docstring for the child methods. So basically, there's nothing to do. I've also tried within the python interpreter, and it can perfectly solve docstring inheritance. So why would you explicitly assign docstring to child methods ? JM From invalid at invalid Mon Jul 27 10:11:21 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 27 Jul 2009 09:11:21 -0500 Subject: Convert raw binary file to ascii References: Message-ID: On 2009-07-27, r2 wrote: > I have a memory dump from a machine I am trying to analyze. I can view > the file in a hex editor to see text strings in the binary code. I > don't see a way to save these ascii representations of the binary, $ strings memdump.binary >memdump.strings $ hexdump -C memdump.binary >memdump.hex+ascii -- Grant Edwards grante Yow! I'm ZIPPY the PINHEAD at and I'm totally committed visi.com to the festive mode. From jeanmichel at sequans.com Mon Jul 27 10:17:13 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 27 Jul 2009 16:17:13 +0200 Subject: How to comment constant values? In-Reply-To: <027cd65b$0$5185$c3e8da3@news.astraweb.com> References: <200907270749.58845.devent@deventm.org> <7d44ndF29p3p7U2@mid.uni-berlin.de> <027cd65b$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A6DB6E9.3020308@sequans.com> Steven D'Aprano wrote: > On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote: > > >>> Only modules, classes, and functions/methods can have docstrings >>> associated with them. >>> For anything else, you have to use comments; or you can mention them in >>> the docstrings of related things. >>> >> While this is technically true, writing docstrings to constants (module >> or classlevel) works when one uses tools such as epydoc to generate >> documentation. >> > > I've never used epydoc, so I'm not sure what you mean. Presumably it uses > source code analysis to detect: > > CONSTANT = 42 > """This is a constant.""" > > even though the string is ignored by the compiler. > > Is that correct? > > > Yes, and because it is perfectly ignored by the compiler there's no harm using this feature. I would add that even if you're not using epydoc, having a way top discriminate comments from documentation is recommended, their purpose are definitely not the same. JM From piet at cs.uu.nl Mon Jul 27 10:23:04 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 16:23:04 +0200 Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> import M2Crypto >j> from M2Crypto import RSA,SSL >j> def encrypt(): >j> pk = open('my_key.public.pem', 'rb').read() >j> rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub >j> object. >j> plaintext = 4545479545655576767767686688782344 >j> msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding) >j> print msg; >j> encrypt() >j> This is code i am refering. >j> The Problem is coming with .pem file. You never tell what the problem is. Which error message do you get? Why can't you just copy the error messages in your post instead of letting us guess what is happening? There are indeed problems with the code above. One warning: If you are going to write cryptographic code while you really don't understand the subject enough there is a big chance that your programs will have security problems. One more question: Do you have now a valid my_key.public.pem file? And is it in the same directory where you run this program? Here are the problems in your code (and there may be more!) import M2Crypto from M2Crypto import RSA,SSL You never use the imports from the line above so you can leave it out. SSL isn't used at all, and RSA is used as M2Crypto.RSA so it uses the first import. Cleanliness is next to godliness. def encrypt(): pk = open('my_key.public.pem', 'rb').read() rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub object. load_pub_key requires a file name, not the contents of the file. So use rsa = M2Crypto.RSA.load_pub_key('my_key.public.pem') and leave the open line out. plaintext = 4545479545655576767767686688782344 That's not text, it is a number. rsa.public_encrypt will not accept a number. So you would have to use plaintext = "4545479545655576767767686688782344" or convert the number to a byte array. msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding) print msg; encrypt() The above line should be shifted to the left. It is not part of the function otherwise you get an endless recursion. And to answer another question from stackoverflow.com: RSA.pkcs1_padding is a good parameter, if the decryption side will also use it. See also my posting on the subject `RSA cryptography between Python and Java'. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From skip at pobox.com Mon Jul 27 10:26:42 2009 From: skip at pobox.com (skip at pobox.com) Date: Mon, 27 Jul 2009 09:26:42 -0500 (CDT) Subject: Wrapping prstat on Solaris Message-ID: <20090727142642.947081188353@montanaro.dyndns.org> At work we currently use top to monitor ongoing system utilization on our Solaris systems. As time has moved on though, use of top has become problematic. Our admins want us to switch to prstat, Sun's top-like command. It works fine however doesn't emit a timestamp at each display interval, so it's essentially impossible looking at a prstat output file to determine when the particular "screen" was emitted. If figured, "No problem. I'll just write a little wrapper." Alas, that is proving harder than I thought. I can run prstat directing output to a file and it seems to blast out a block of lines every N seconds, but when run from inside a Python script I can't seem to make the damn thing not massively buffer its output. Accordingly, my script doesn't really see the output as its emitted, so the timestamp line it prepends to a block of output is off. I'm currently using subprocess.Popen like so: os.environ["TERM"] = "dumb" cmd = "prstat -c %s" % " ".join(sys.argv[1:]) pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout I've tried these other variants as well: * prefacing the prstat command with unbuffer (the tcl/expect thingamabob) * explicitly redirect the prstat output to /dev/stdout * setting bufsize to 0 * used os.popen instead of Subprocess.Popen Nothing seems to help. I always seem to see about 30 seconds of data at once (I'm currently using a 5-second interval and 10 lines of output). I would have expected that prstat would simply flush stdout after each block of output. Any ideas about how to get prstat to cooperate better? Thanks, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire From simon at brunningonline.net Mon Jul 27 10:31:11 2009 From: simon at brunningonline.net (Simon Brunning) Date: Mon, 27 Jul 2009 15:31:11 +0100 Subject: iText for Python In-Reply-To: <505f2bc1-cbfb-49a1-a7ef-0b6c357750a7@q35g2000vbi.googlegroups.com> References: <505f2bc1-cbfb-49a1-a7ef-0b6c357750a7@q35g2000vbi.googlegroups.com> Message-ID: <8c7f10c60907270731x1d416e64vde1907d8c4dd42d1@mail.gmail.com> 2009/7/27 santhoshvkumar : > ? ? ? ? ? One of my cousin ?suggested me to do a IText PDF converter > for python. Actually I heard that there is no separate IText converter > either we have to go for jython or GCJ with wrapper. Instead of > wrapping, my plan is to create a separate module with Python and I am > thinking of doing this in as my final year project also. Kindly give > me your suggestion. What would this give us that ReportLab does not? I wouldn't want to see you spend a lot of time reinventing the wheel... -- Cheers, Simon B. From aahz at pythoncraft.com Mon Jul 27 10:32:53 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:32:53 -0700 Subject: Form/Template Fill-in the blanks References: <4b77a992-370d-4879-88a0-fdd6a23f7c5d@p10g2000prm.googlegroups.com> Message-ID: In article <4b77a992-370d-4879-88a0-fdd6a23f7c5d at p10g2000prm.googlegroups.com>, allan wrote: > >My initial thought was to use: >1. .ini files to declare the EDI configuration > >INI file configuration: >* A "root" INI file indicates other INI files that define each segment >of the EDI document. >* Each segment INI defines the data elements of each segment and the >behavior of the segment (one instance or multiple-instance as in a >loop, sequence order, segment code, etc.) >* Specify data elements as either constant, system function (like >datetime), database field or object method (object being the >segment's) >* Load all the ini configuration into a "template" object. Each >segment ini maps to its own segment object. This sounds like something you want to use XML for. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From bryanvick at gmail.com Mon Jul 27 10:37:53 2009 From: bryanvick at gmail.com (Bryan) Date: Mon, 27 Jul 2009 07:37:53 -0700 (PDT) Subject: Script runs manually, but cron fails References: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> Message-ID: On Jul 24, 2:11?pm, Bryan wrote: > I have a backup script that runs fine when I run it manually from the > command line. ?When I run it with cron, the script stops running at > random points in the source code. > > The script calls rsync with the subprocess module, which in turn uses > ssh to backup files from a box on my lan. ?It also uses the atexit > module to always run a certain piece of cleanup code when the script > exits. ?However, this piece of code is never called when I use cron to > run the script, but a ps -A command also does not show the python > process so I know the script is dead. ?The log files generated by the > script all show rsync as completing, but then the logging gets cutoff > at random places when the script dies after that point. ?I am not > getting any email from cron complaining of an error either. > > The script runs fine in my bash shell, what could cron be doing to > interfere? This script works when I run it at home, and I think it is because I don't use rsync's ability to backup over ssh. The working script at home simply backs up the local machine to a mounted external hard drive. So when I run a python script that calls rsync to work over ssh, cron is screwing things up. Can anyone recommend where I should report this behavior? Should I email the author of cron directly, I don't see a group/address specifically for cron bugs. From kushal.kumaran+python at gmail.com Mon Jul 27 10:40:40 2009 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Mon, 27 Jul 2009 20:10:40 +0530 Subject: Itext for Python In-Reply-To: References: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> Message-ID: <1e364c4e0907270740sdc0e6ceye49f78b342820096@mail.gmail.com> On Mon, Jul 27, 2009 at 2:23 PM, S.Selvam wrote: > > > On Sun, Jul 26, 2009 at 11:52 PM, Santhosh Kumar > wrote: >> >> Hi all, >> ?? ? ? ? ??One of my cousin ?suggested me to do a IText PDF?converter?for >> python. Actually I heard that there is no?separate?IText?converter?either we >> have to go for jython or GCJ with wrapper. Instead of wrapping, my plan is >> to?create a separate module with Python and I am thinking of doing this in >> as my final year project also. Kindly give me your?suggestion. > > As for as i know, no pure python module exists for iText PDF generator, > where iText seems to be a standard one for PDF related works.There are some > other modules for pdf work but might not be as standard as iText.Surely some > senior programmers? will suggest you better way to proceed further. > AFAIK, iText is just a java library for generating PDF files. You could have a look at http://www.reportlab.org/rl_toolkit.html -- kushal From james at agentultra.com Mon Jul 27 10:40:53 2009 From: james at agentultra.com (J Kenneth King) Date: Mon, 27 Jul 2009 10:40:53 -0400 Subject: Script runs manually, but cron fails References: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> Message-ID: <858wia6vey.fsf@agentultra.com> Bryan writes: > I have a backup script that runs fine when I run it manually from the > command line. When I run it with cron, the script stops running at > random points in the source code. > > The script calls rsync with the subprocess module, which in turn uses > ssh to backup files from a box on my lan. It also uses the atexit > module to always run a certain piece of cleanup code when the script > exits. However, this piece of code is never called when I use cron to > run the script, but a ps -A command also does not show the python > process so I know the script is dead. The log files generated by the > script all show rsync as completing, but then the logging gets cutoff > at random places when the script dies after that point. I am not > getting any email from cron complaining of an error either. > > The script runs fine in my bash shell, what could cron be doing to > interfere? Double check nothing is writing to stdout/stdin Some cron's don't mind, but I always squelch output because some do. From aahz at pythoncraft.com Mon Jul 27 10:41:45 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:41:45 -0700 Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> <1c8ae01e-2e9c-497c-9f8d-408f56f9c0bd@g31g2000yqc.googlegroups.com> Message-ID: In article <1c8ae01e-2e9c-497c-9f8d-408f56f9c0bd at g31g2000yqc.googlegroups.com>, Michal Kwiatkowski wrote: >On Jul 27, 1:56 am, a... at pythoncraft.com (Aahz) wrote: >>>> Upon a cursory look, after a generator 'gen' is exhausted (meaning >>>> gen.next() has raised StopIteration), it seems that gen.gi_frame will be >>>> None. >>> >>>Only in Python 2.5 or higher though. I need to support Python 2.3 and >>>2.4 as well, sorry for not making that clear in the original post. >> >> Are you sure? It appears to work in Python 2.4; I don't have time to >> check 2.3. > >No, it does not work in Python 2.4. gi_frame can be None only in >Python 2.5 and higher. You're right, I guess I must have made a boo-boo when I was switching versions. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From aahz at pythoncraft.com Mon Jul 27 10:45:50 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:45:50 -0700 Subject: pyc files not automatically compiled on import References: Message-ID: In article , Baz Walter wrote: > >i thought that python automatically compiled pyc files after a module is >successfully imported. what could prevent this happening? Looks like you got your problem fixed, but for the record, not having write permission on a directory also causes this. It's even uglier when the .pyc already exists but does not have write perms. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From aahz at pythoncraft.com Mon Jul 27 10:49:25 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:49:25 -0700 Subject: If Scheme is so good why MIT drops it? References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: In article , Hendrik van Rooyen wrote: >On Sunday 26 July 2009 21:26:46 David Robinow wrote: >> >> I'm a mediocre programmer. Does this mean I should switch to PHP? > >I have searched, but I can find nothing about this mediocre language. > >Could you tell us more? :-P (For anyone who is confused by Hendrik's humor, he is saying that David was referring to a programming language named "mediocre". English grammar is confusing!) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From ethan at stoneleaf.us Mon Jul 27 11:00:12 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 27 Jul 2009 08:00:12 -0700 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <468250.83203.qm@web31106.mail.mud.yahoo.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> Message-ID: <4A6DC0FC.90704@stoneleaf.us> [corrected top posting] Mohammad Tayseer wrote: >> *From:* Piet van Oostrum >> *To:* python-list at python.org >> *Sent:* Monday, July 27, 2009 11:18:20 AM >> *Subject:* Re: Looking for a dream language: sounds like Python to me. >> >> >>>>> Dotan Cohen > >> (DC) wrote: >> >> >DC> Referring to this article: >>>DC> >> http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ >> >> >DC> The author, who is specifically looking for math-related >> functions, writes: >> >DC> """ >> >DC> The dream algorithm R&D tool would be similar to Matlab or Mathematica >> >DC> but could be compiled to fast, efficient binaries similar to ANSI C >> >DC> and would be available for all platforms. An integrated GUI builder >> >DC> similar to Visual Basic and integrated network support would be >> >DC> helpful. >> >DC> """ >> >> >DC> It looks to me like he is looking for Python. >> >> No fast, efficient binaries yet, and no integrated GUI builder. >> -- >> Piet van Oostrum > >> URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] >> Private email: piet at vanoostrum.org >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > You can generate binaries using py2exe, and you can create UI using > Tkinter (which is very easy) or wxPython (which have GUI builders) > > Mohammad Tayseer > http://spellcoder.com/blogs/tayseer Creating binaries is not the same as creating /fast, efficient/ binaries. Py2Exe bundles it all together, but does not make it any faster. ~Ethan~ From __peter__ at web.de Mon Jul 27 11:12:02 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 17:12:02 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> <8b78b47f-799e-4d37-bc28-d1ed349362c9@k6g2000yqn.googlegroups.com> Message-ID: Martin wrote: > The statement works now, but it doesn't give the same results as my > original logic, strangely!? > > in my logic: > > data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') > > for i in range(numrows): > for j in range(numcols): > if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: > data[i,j] = 0 > else: > data[i,j] = 1 > > to do the same with what you suggested... > > data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') > data[(band3 == 255) | (band6 >= band3 * factor)] = 0 Did you swap band3 and band6? If that's the case, it is an error you should be able to find yourself. Please be a bit more careful. Also, it is good practice to write a few test cases where you have precalculated the result. How would you otherwise know which version is correct? Writing a third one to break the tie? Peter From dotancohen at gmail.com Mon Jul 27 11:12:09 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Mon, 27 Jul 2009 18:12:09 +0300 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <4A6DC0FC.90704@stoneleaf.us> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> Message-ID: <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> > Creating binaries is not the same as creating /fast, efficient/ binaries. > ?Py2Exe bundles it all together, but does not make it any faster. > How inefficient is py2exe. I was under the impression that it's really not that bad. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From dns4 at cornell.edu Mon Jul 27 11:14:15 2009 From: dns4 at cornell.edu (David Smith) Date: Mon, 27 Jul 2009 11:14:15 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: Aahz wrote: > In article , > Hendrik van Rooyen wrote: >> On Sunday 26 July 2009 21:26:46 David Robinow wrote: >>> I'm a mediocre programmer. Does this mean I should switch to PHP? >> I have searched, but I can find nothing about this mediocre language. >> >> Could you tell us more? > > :-P > > (For anyone who is confused by Hendrik's humor, he is saying that David > was referring to a programming language named "mediocre". English > grammar is confusing!) LOL ... I'm an American and that wasn't all that clear :-) From exarkun at divmod.com Mon Jul 27 11:18:41 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Mon, 27 Jul 2009 11:18:41 -0400 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> Message-ID: <20090727151841.388.143506053.divmod.quotient.793@henry.divmod.com> On Mon, 27 Jul 2009 18:12:09 +0300, Dotan Cohen wrote: >> Creating binaries is not the same as creating /fast, efficient/ binaries. >> ?Py2Exe bundles it all together, but does not make it any faster. >> > >How inefficient is py2exe. I was under the impression that it's really >not that bad. > py2exe doesn't really change the performance characteristics of a Python application at all - for better or for worse. Jean-Paul From gmweezel at gmail.com Mon Jul 27 11:21:29 2009 From: gmweezel at gmail.com (Eric) Date: Mon, 27 Jul 2009 08:21:29 -0700 (PDT) Subject: CRLF Newlines and libc Message-ID: <6bcb4820-11be-4053-8e3b-849b00aba55a@c2g2000yqi.googlegroups.com> I am working on the subprocess.Popen module for Google Summer of Code. Right now, I am having difficulty trying to work out how to deal with my '\n' newlines being converted to '\r\n' newlines when reading from a pipe on windows; see this blog post (http://subdev.blogspot.com/ 2009/07/stdout.html) and this chain on Python-Dev (http:// mail.python.org/pipermail/python-dev/2009-July/090720.html). Right now, unless there is way to disable the newline conversions, I am thinking I will just have to document the caveat and call it a day. Is there a better way to handle this? Eric From cournape at gmail.com Mon Jul 27 11:22:16 2009 From: cournape at gmail.com (David Cournapeau) Date: Tue, 28 Jul 2009 00:22:16 +0900 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> Message-ID: <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> On Tue, Jul 28, 2009 at 12:12 AM, Dotan Cohen wrote: >> Creating binaries is not the same as creating /fast, efficient/ binaries. >> ?Py2Exe bundles it all together, but does not make it any faster. >> > > How inefficient is py2exe. It is neither efficient or inefficient: it is just a distribution tool, to deploy python software in a form familiar to most windows users. It does not make it any faster than running the software under a python prompt. As much as I like python for scientific programming, I would say python is pretty far from the stated requirements in the posted blog post. It is difficult to deploy software written with python (much better than the alternatives, though), and it is slow if you can't leverage numpy/scipy (where vectorization does not apply). It remains to be seen whether it will be true in practice, but something like F#, with its integration in VS 2010, seems much closer IMHO. It is compiled, high level language, and backed by the biggest software vendor in the world. David From invalid at invalid Mon Jul 27 11:24:27 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 27 Jul 2009 10:24:27 -0500 Subject: Looking for a dream language: sounds like Python to me. References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> Message-ID: On 2009-07-27, Dotan Cohen wrote: >> Creating binaries is not the same as creating /fast, efficient/ binaries. >> ??Py2Exe bundles it all together, but does not make it any faster. > > How inefficient is py2exe. [Assuming that was a question.] py2exe just bundles up the files needed to run the program. The result runs pretty much the same as it did when it was "unbundled". By default, py2exe combines some stuff in a zip file, so that might slow down startup by a tiny amount. If you don't like that, you can tell py2exe not to "zip" things up, and then the program runs pretty much identically as did the unbundled version. > I was under the impression that it's really not that bad. py2exe doesn't really have any impact on the program's perfromance. It's still the same Python VM running the same bytecode using the same library files. -- Grant Edwards grante Yow! I'm totally DESPONDENT at over the LIBYAN situation visi.com and the price of CHICKEN ... From dotancohen at gmail.com Mon Jul 27 11:28:47 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Mon, 27 Jul 2009 18:28:47 +0300 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> Message-ID: <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> > It is neither efficient or inefficient: it is just a distribution > tool, to deploy python software in a form familiar to most windows > users. It does not make it any faster than running the software under > a python prompt. > > As much as I like python for scientific programming, I would say > python is pretty far from the stated requirements in the posted blog > post. It is difficult to deploy software written with python (much > better than the alternatives, though), and it is slow if you can't > leverage numpy/scipy (where vectorization does not apply). > > It remains to be seen whether it will be true in practice, but > something like F#, with its integration in VS 2010, seems much closer > IMHO. It is compiled, high level language, and backed by the biggest > software vendor in the world. > The blog post is not looking to distribute his code, but he would like it to be cross platform for his own reasons. VB is not cross platform. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From cournape at gmail.com Mon Jul 27 11:39:14 2009 From: cournape at gmail.com (David Cournapeau) Date: Tue, 28 Jul 2009 00:39:14 +0900 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: <5b8d13220907270839p3f65e1f8k7c1bb022bc922d6e@mail.gmail.com> On Tue, Jul 28, 2009 at 12:28 AM, Dotan Cohen wrote: >> It is neither efficient or inefficient: it is just a distribution >> tool, to deploy python software in a form familiar to most windows >> users. It does not make it any faster than running the software under >> a python prompt. >> >> As much as I like python for scientific programming, I would say >> python is pretty far from the stated requirements in the posted blog >> post. It is difficult to deploy software written with python (much >> better than the alternatives, though), and it is slow if you can't >> leverage numpy/scipy (where vectorization does not apply). >> >> It remains to be seen whether it will be true in practice, but >> something like F#, with its integration in VS 2010, seems much closer >> IMHO. It is compiled, high level language, and backed by the biggest >> software vendor in the world. >> > > The blog post is not looking to distribute his code, but he would like > it to be cross platform for his own reasons. VB is not cross platform. I understand his "efficient binary as Ansi C" partially as a deployment requirement, and independent of cross-platform issues. As a scientist, being able to share my software with colleagues is a non trivial matter. Python does not make this easy today. F# has nothing to do with VB: F# is a ML-language inspired from OCAML, and run on top of the CLR. It can thus leverage the huge .net framework (lack of non numerical API is one of the biggest matlab hindrance, and comparatively big advantage of python + numpy/scipy), and benefits from the much more efficient implementation compared to python (under the currently CPython implementation at least). Some recent F# versions are compatible with mono, making it compatible on most platforms that matter today for research (but of course, you lose the IDE integration outside windows). David From jakecjacobson at gmail.com Mon Jul 27 11:44:40 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Mon, 27 Jul 2009 08:44:40 -0700 (PDT) Subject: exceptions.TypeError an integer is required References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> <0279f85d$0$5185$c3e8da3@news.astraweb.com> Message-ID: <8d03a739-7e45-49e1-b93d-6a396beed877@a37g2000prf.googlegroups.com> On Jul 24, 3:11?pm, Steven D'Aprano wrote: > On Fri, 24 Jul 2009 11:24:58 -0700, jakecjacobson wrote: > > I am trying to do a post to a REST API over HTTPS and requires the > > script to pass a cert to the server. ?I am getting "exceptions.TypeError > > an integer is required" error and can't find the reason. ?I commenting > > out the lines of code, it is happening on the connection.request() line. > > ?Here is the problem code. ?Would love some help if possible. > > Please post the traceback that you get. > > My guess is that you are passing a string instead of an integer, probably > for the port. > > [...] > > > ? ?except: > > ? ? ? ? ? ?print sys.exc_type, sys.exc_value > > As a general rule, a bare except of that fashion is bad practice. Unless > you can explain why it is normally bad practice, *and* why your case is > an exception (no pun intended) to the rule "never use bare except > clauses", I suggest you either: > > * replace "except:" with "except Exception:" instead. > > * better still, re-write the entire try block as: > > ? ? try: > ? ? ? ? [code goes here] > ? ? finally: > ? ? ? ? connection.close() > > and use the Python error-reporting mechanism instead of defeating it. > > -- > Steven Steven, You are quite correct in your statements. My goal was not to make great code but something that I could quickly test. My assumption was that the httplib.HTTPSConnection() would do the cast to int for me. As soon as I cast it to an int, I was able to get past that issue. Still not able to post because I am getting a bad cert error. Jake Jacobson From jakecjacobson at gmail.com Mon Jul 27 11:57:40 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Mon, 27 Jul 2009 08:57:40 -0700 (PDT) Subject: bad certificate error Message-ID: Hi, I am getting the following error when doing a post to REST API, Enter PEM pass phrase: Traceback (most recent call last): File "./ices_catalog_feeder.py", line 193, in ? main(sys.argv[1]) File "./ices_catalog_feeder.py", line 60, in main post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join (input_dir, file), collection_name, key_file, cert_file) File "./ices_catalog_feeder.py", line 125, in post2Catalog connection.request('POST', path, parameters, head) File "/usr/lib/python2.4/httplib.py", line 810, in request self._send_request(method, url, body, headers) File "/usr/lib/python2.4/httplib.py", line 833, in _send_request self.endheaders() File "/usr/lib/python2.4/httplib.py", line 804, in endheaders self._send_output() File "/usr/lib/python2.4/httplib.py", line 685, in _send_output self.send(msg) File "/usr/lib/python2.4/httplib.py", line 652, in send self.connect() File "/usr/lib/python2.4/httplib.py", line 1079, in connect ssl = socket.ssl(sock, self.key_file, self.cert_file) File "/usr/lib/python2.4/socket.py", line 74, in ssl return _realssl(sock, keyfile, certfile) socket.sslerror: (1, 'error:14094412:SSL routines:SSL3_READ_BYTES:sslv3 alert bad certificate') My code where this error occurs is: head = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"} parameters = urlencode({"collection" : collection, "entryxml" : open (file,'r').read()}) print "Sending the file to: " + host try: try: # Default port is 443. # key_file is the name of a PEM formatted file that contains your private key. # cert_file is a PEM formatted certificate chain file. connection = httplib.HTTPSConnection(host, int(port), key_file, cert_file) connection.request('POST', path, parameters, head) response = connection.getresponse() print response.status, response.reason except httplib.error, (value,message): print value + ':' + message finally: connection.close() I was wondering if this is due to the server having a invalid server cert? If I go to this server in my browser, I get a "This server tried to identify itself with invalid information". Is there a way to ignore this issue with Python? Can I setup a trust store and add this server to the trust store? From mdekauwe at gmail.com Mon Jul 27 12:00:41 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 09:00:41 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> <8b78b47f-799e-4d37-bc28-d1ed349362c9@k6g2000yqn.googlegroups.com> Message-ID: On Jul 27, 4:12 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > The statement works now, but it doesn't give the same results as my > > original logic, strangely!? > > > in my logic: > > > data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') > > > for i in range(numrows): > > for j in range(numcols): > > if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: > > data[i,j] = 0 > > else: > > data[i,j] = 1 > > > to do the same with what you suggested... > > > data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') > > data[(band3 == 255) | (band6 >= band3 * factor)] = 0 > > Did you swap band3 and band6? If that's the case, it is an error you should > be able to find yourself. > > Please be a bit more careful. > > Also, it is good practice to write a few test cases where you have > precalculated the result. How would you otherwise know which version is > correct? Writing a third one to break the tie? > > Peter apologies... it was the way I typed it up, I wasn't copying and pasting from my text editor! Thanks Peter. From davea at ieee.org Mon Jul 27 12:09:38 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 12:09:38 -0400 Subject: [python-win32] subprocess and stdout In-Reply-To: References: <4A6D9D83.6050505@ieee.org> Message-ID: <4A6DD142.7040809@ieee.org> (This message was top-posted, and sent off-list. So I'm copying it back to the list, with my response at the end) Chris Chapman wrote: > Thanks Dave. You know after trying your suggestion on the command prompt it doesn't as a matter of fact. Not sure why I didn't just try that in windows to begin with. Guess I got stuck more on the python issue then it being the other program. > > There is an option for it to create an HTML log file, but it's formatted sort of strangely. I'm really not looking to parse through it just for a summary like that presented on the terminal. I'm not sure if you know but I'm guessing parsing the HTML would be a lot easier the trying to capture the console output from the buffer, huh? Thanks again for the help. > > Chris > > >> Date: Mon, 27 Jul 2009 08:28:51 -0400 >> From: davea at ieee.org >> To: oxydol010 at hotmail.com >> CC: python-win32 at python.org >> Subject: Re: [python-win32] subprocess and stdout >> >> Christopher Chapman wrote: >> >>> I'm trying to write a "module" to a larger program. This module eventually >>> needs to reference a config file with setup information in it, but I'm not >>> there yet. I'm using the subprocess module to run an external antivirus >>> program and trying to get the output written to a log file, but I keep >>> getting a weird return. My code is listed below. I'm a noob to Python so >>> I'm not sure if this is a windows specific issue or not, but I'm programming >>> on windows and more then likely what I'm working on won't be on anything >>> other then a windows machine. I'm also writing my code for 3.1 if that >>> makes any difference. >>> >>> # Pass target path to scanners command line and write output to a file >>> # Currently "scan" only targets the McAfee Command Line Scanner >>> print() >>> print ("Begining scan of " + target) >>> print() >>> scan = "scan /all " + target >>> s = subprocess.Popen(scan, >>> stdout=subprocess.PIPE) >>> out = s.communicate()[0] >>> chgout = str(out) >>> s.wait() >>> scanlog.write(chgout) >>> scanlog.close >>> " >>> If I change my stdout in s subprocess to None then everything gets written >>> to the terminal as if I had just run the program straight from the command >>> line, but when I try piping it to my file "scanlog" then literally the only >>> return I get in the file is '' or two single quotes. I've even tried piping >>> the output and then printing it instead of writing it to a file and I get >>> the same result. I've experimented with standard windows command line >>> commands and using the same syntax was able to pipe directory listings and >>> other things to my file. Any ideas what I'm missing here? Thanks in >>> advance >>> >>> Chris Chapman >>> >>> >>> >>> >> I suspect it's because of the way the antivirus program is written. I >> can't be sure what you tried at the command prompt, but have you tried this: >> >> c:\>scan /all > scanlog.txt >> >> >> If this does not capture to the file, then scan.exe isn't written in the >> way you're expecting. There are several ways to write directly to a >> console that do not redirect or pipe. >> >> You might also look to see whether scan has any other commandline >> options. One of them might be to create a log file. >> >> DaveA >> > > I wouldn't know where to begin trying to capture output that another process sends to its console window. While I'm sure it's possible, I do suspect that parsing the html would be easier. Remember that even if it looks ugly, it is generated by program, and it's quite possible that you could throw away the fluff and end up with a simple summary. There are libraries to make this easier, but I've never used them. If it's valid xhtml, which is by definition valid xml, you might find it useful to use elementree. But most html is buggy, and parsing that takes a more tolerant parser. DaveA From robert.kern at gmail.com Mon Jul 27 12:11:02 2009 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 27 Jul 2009 11:11:02 -0500 Subject: quickly looping over a 2D array? In-Reply-To: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Message-ID: On 2009-07-27 06:24, Martin wrote: > Hi, > > I am new to python and I was wondering if there was a way to speed up > the way I index 2D arrays when I need to check two arrays > simultaneously? My current implementations is (using numpy) something > like the following... > > for i in range(numrows): > for j in range(numcols): > > if array_1[i, j] == some_value or array_2[i, j]>= array_1[i, > j] * some_other_value > array_1[i, j] = some_new_value Peter has given you good answers, but you will probably want to ask future numpy questions on the numpy mailing list. http://www.scipy.org/Mailing_Lists -- 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 python at mrabarnett.plus.com Mon Jul 27 12:34:03 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 27 Jul 2009 17:34:03 +0100 Subject: New implementation of re module Message-ID: <4A6DD6FB.8040609@mrabarnett.plus.com> Hi all, I've been working on a new implementation of the re module. The details are at http://bugs.python.org/issue2636, specifically from http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for Python 2.6 on Windows if you want to try it out. I'm interested in how fast it is generally, compared with the current re module, but especially when faced with those 'pathological' regular expressions which seem to take a long time to finish, for example: re.search(r"^(.+|D)*A$", "x" * 25 + "B") which on my PC (1.8GHz) takes 18.98secs with the re module but <0.01secs with this new implementation. TIA From Ray.Joseph at CDICorp.com Mon Jul 27 12:42:06 2009 From: Ray.Joseph at CDICorp.com (ray) Date: Mon, 27 Jul 2009 09:42:06 -0700 (PDT) Subject: Using easy_install, reduncant? Message-ID: I am working on a Trac installation. I am new to Python. To install packages, it is suggested to use setuptools. I have not understood the directions. I execute ez_install.py. Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. There response that setuptools is already the active version in easy- install.pth. Then: Installing easy_install.exe script to C:\Python26\Scripts error: C: \Python26\Scripts\Easy_install.exe: Permission denied. I have compared the file entries before and after this attempt and there are no new files. Is there any problems here? What did I miss? Regards, Ray From nagle at animats.com Mon Jul 27 12:58:57 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 09:58:57 -0700 Subject: Using easy_install, reduncant? In-Reply-To: References: Message-ID: <4a6ddc22$0$1667$742ec2ed@news.sonic.net> ray wrote: > I am working on a Trac installation. I am new to Python. To install > packages, it is suggested to use setuptools. I have not understood > the directions. > > I execute ez_install.py. > > Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. > There response that setuptools is already the active version in easy- > install.pth. Then: > Installing easy_install.exe script to C:\Python26\Scripts error: C: > \Python26\Scripts\Easy_install.exe: Permission denied. "Easy Install" usually isn't "easy". If it works, it's great. Most of the time, it doesn't. It's not very good at finding all the files it needs to update. Hence problems like the above. John Nagle From deets at nospam.web.de Mon Jul 27 13:00:52 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 19:00:52 +0200 Subject: Using easy_install, reduncant? References: <4a6ddc22$0$1667$742ec2ed@news.sonic.net> Message-ID: <7d64q4F2an2drU1@mid.uni-berlin.de> John Nagle wrote: > ray wrote: >> I am working on a Trac installation. I am new to Python. To install >> packages, it is suggested to use setuptools. I have not understood >> the directions. >> >> I execute ez_install.py. >> >> Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. >> There response that setuptools is already the active version in easy- >> install.pth. Then: >> Installing easy_install.exe script to C:\Python26\Scripts error: C: >> \Python26\Scripts\Easy_install.exe: Permission denied. > > "Easy Install" usually isn't "easy". If it works, it's great. > Most of the time, it doesn't. It's not very good at finding all the > files it needs to update. Hence problems like the above. Which is the usual BS from you. It works most of the times as advertised, and in the concrete case the OP misunderstood what ez_setup.py does (already installing setuptools), and thus the complaint about "already active version". Which btw is just a warning, and setuptools would continue, but as the OP works on Windows which prohibits a running executable to be replaced, it fails. Again, not setuptools fault. Diez From nagle at animats.com Mon Jul 27 13:23:43 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 10:23:43 -0700 Subject: M2Crypto hangs on this URL Message-ID: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> There's something strange about this URL: "https://sagar310.pontins.com/sraep/" It hangs Firefox 2; there's no short timeout, the web page just gets stuck in initial load for about ten minutes. Then "The connection to sagar310.pontins.com was interrupted while the page was loading." It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. What does the new SSL implementation do? (Haven't tried that yet; waiting for MySQL support for Python 3.x before converting web apps.) John Nagle From rlichlighter at gmail.com Mon Jul 27 13:25:18 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 10:25:18 -0700 (PDT) Subject: Convert raw binary file to ascii References: Message-ID: On Jul 27, 9:06?am, Peter Otten <__pete... at web.de> wrote: > r2 wrote: > > I have a memory dump from a machine I am trying to analyze. I can view > > the file in a hex editor to see text strings in the binary code. I > > don't see a way to save these ascii representations of the binary, so > > I went digging into Python to see if there were any modules to help. > > > I found one I think might do what I want it to do - the binascii > > module. Can anyone describe to me how to convert a raw binary file to > > an ascii file using this module. I've tried? Boy, I've tried. > > That won't work because a text editor doesn't need any help to convert the > bytes into characters. If it expects ascii it just will be puzzled by bytes > that are not valid ascii. Also, it will happily display byte sequences that > are valid ascii, but that you as a user will see as gibberish because they > were meant to be binary data by the program that wrote them. > > > Am I correct in assuming I can get the converted binary to ascii text > > I see in a hex editor using this module? I'm new to this forensics > > thing and it's quite possible I am mixing technical terms. I am not > > new to Python, however. Thanks for your help. > > Unix has the "strings" commandline tool to extract text from a binary. > Get hold of a copy of the MinGW tools if you are on windows. > > Peter Okay. Thanks for the guidance. I have a machine with Linux, so I should be able to do what you describe above. Could Python extract the strings from the binary as well? Just wondering. From rlichlighter at gmail.com Mon Jul 27 13:25:44 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 10:25:44 -0700 (PDT) Subject: Convert raw binary file to ascii References: Message-ID: On Jul 27, 10:11?am, Grant Edwards wrote: > On 2009-07-27, r2 wrote: > > > I have a memory dump from a machine I am trying to analyze. I can view > > the file in a hex editor to see text strings in the binary code. I > > don't see a way to save these ascii representations of the binary, > > $ strings memdump.binary >memdump.strings > > $ hexdump -C memdump.binary >memdump.hex+ascii > > -- > Grant Edwards ? ? ? ? ? ? ? ? ? grante ? ? ? ? ? ? Yow! I'm ZIPPY the PINHEAD > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? at ? ? ? ? ? ? ? and I'm totally committed > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?visi.com ? ? ? ? ? ?to the festive mode. Grant, Thanks for the commands! From reckoner at gmail.com Mon Jul 27 13:40:03 2009 From: reckoner at gmail.com (Reckoner) Date: Mon, 27 Jul 2009 10:40:03 -0700 (PDT) Subject: initializing with empty list as default causes freaky problems Message-ID: Hi, Observe the following: In [202]: class Foo(): .....: def __init__(self,h=[]): .....: self.h=h .....: .....: In [203]: f=Foo() In [204]: g=Foo() In [205]: g.h Out[205]: [] In [206]: f.h Out[206]: [] In [207]: f.h.append(10) In [208]: f.h Out[208]: [10] In [209]: g.h Out[209]: [10] The question is: why is g.h updated when I append to f.h? Shouldn't g.h stay []? What am I missing here? I'm using Python 2.5.1. Thanks in advance. From python at mrabarnett.plus.com Mon Jul 27 13:55:57 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 27 Jul 2009 18:55:57 +0100 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: <4A6DEA2D.6000408@mrabarnett.plus.com> Reckoner wrote: > Hi, > X-Antispam: NO; Spamcatcher 5.2.1. Score 50 > > Observe the following: > > In [202]: class Foo(): > .....: def __init__(self,h=[]): > .....: self.h=h > .....: > .....: > > In [203]: f=Foo() > > In [204]: g=Foo() > > In [205]: g.h > Out[205]: [] > > In [206]: f.h > Out[206]: [] > > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? Shouldn't > g.h stay []? > > What am I missing here? > > I'm using Python 2.5.1. > Default arguments are evaluated once and then shared, so don't use them with mutable objects like lists. Do this instead: class Foo(): def __init__(self, h=None): if h is None: self.h = [] else: self.h = h From __peter__ at web.de Mon Jul 27 14:07:37 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 20:07:37 +0200 Subject: Convert raw binary file to ascii References: Message-ID: r2 wrote: > On Jul 27, 9:06 am, Peter Otten <__pete... at web.de> wrote: >> r2 wrote: >> > I have a memory dump from a machine I am trying to analyze. I can view >> > the file in a hex editor to see text strings in the binary code. I >> > don't see a way to save these ascii representations of the binary, so >> > I went digging into Python to see if there were any modules to help. >> >> > I found one I think might do what I want it to do - the binascii >> > module. Can anyone describe to me how to convert a raw binary file to >> > an ascii file using this module. I've tried? Boy, I've tried. >> >> That won't work because a text editor doesn't need any help to convert >> the bytes into characters. If it expects ascii it just will be puzzled by >> bytes that are not valid ascii. Also, it will happily display byte >> sequences that are valid ascii, but that you as a user will see as >> gibberish because they were meant to be binary data by the program that >> wrote them. >> >> > Am I correct in assuming I can get the converted binary to ascii text >> > I see in a hex editor using this module? I'm new to this forensics >> > thing and it's quite possible I am mixing technical terms. I am not >> > new to Python, however. Thanks for your help. >> >> Unix has the "strings" commandline tool to extract text from a binary. >> Get hold of a copy of the MinGW tools if you are on windows. >> >> Peter > > Okay. Thanks for the guidance. I have a machine with Linux, so I > should be able to do what you describe above. Could Python extract the > strings from the binary as well? Just wondering. As a special service for you here is a naive implementation to build upon: #!/usr/bin/env python import sys wanted_chars = ["\0"]*256 for i in range(32, 127): wanted_chars[i] = chr(i) wanted_chars[ord("\t")] = "\t" wanted_chars = "".join(wanted_chars) THRESHOLD = 4 for s in sys.stdin.read().translate(wanted_chars).split("\0"): if len(s) >= THRESHOLD: print s Peter From nagle at animats.com Mon Jul 27 14:13:40 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 11:13:40 -0700 Subject: M2Crypto hangs on this URL In-Reply-To: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> Message-ID: <4a6deda5$0$1639$742ec2ed@news.sonic.net> John Nagle wrote: > There's something strange about this URL: > > "https://sagar310.pontins.com/sraep/" > > It hangs Firefox 2; there's no short timeout, the web page just gets > stuck in initial load for about ten minutes. Then > "The connection to sagar310.pontins.com was interrupted while the page > was loading." > > It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. Correction: Linux is still hung at 5 hours, but Windows timed out after about 15 minutes. John Nagle From gagsl-py2 at yahoo.com.ar Mon Jul 27 14:14:38 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 15:14:38 -0300 Subject: CRLF Newlines and libc References: <6bcb4820-11be-4053-8e3b-849b00aba55a@c2g2000yqi.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 12:21:29 -0300, Eric escribi?: > I am working on the subprocess.Popen module for Google Summer of Code. > Right now, I am having difficulty trying to work out how to deal with > my '\n' newlines being converted to '\r\n' newlines when reading from > a pipe on windows; see this blog post (http://subdev.blogspot.com/ > 2009/07/stdout.html) and this chain on Python-Dev (http:// > mail.python.org/pipermail/python-dev/2009-July/090720.html). > > Right now, unless there is way to disable the newline conversions, I > am thinking I will just have to document the caveat and call it a day. > Is there a better way to handle this? It isn't clear to me what exactly your problem is. What are you reading, using which functions, how did you open the file? On the writing side, how did you open the file, which functions are used to write? The Windows API functions like CreateFile, ReadFile, WriteFile... don't perform any kind of '\n' conversion, neither reading nor writing. On the other hand, the C library does differentiate between files opened in text mode or binary mode (this comes from the ANSI C standard, it's not Windows specific). The default is "text" mode (unless you add "b" to the fopen call, or O_BINARY to the open call). In that mode, '\n' is translated to '\r\n' on writing, and '\r\n' is translated to '\n' on reading. Python files are implemented on top of the C library (on 2.x) so they inherit that behaviour. Console programs written in C and using printf/fwrite/write to stdout share that behaviour too (standard handles are opened in text mode by default). Note that """'\n' newlines being converted to '\r\n' newlines when reading""" cannot happen -- if you see '\r\n' when reading, it's because a) you're reading the file in binary mode, and b) the file was written in text mode. b) is true for process output redirected to a pipe, so you'll have to fix a). How to do that, depends on what exactly you're doing. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 27 14:23:41 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 15:23:41 -0300 Subject: bad certificate error References: Message-ID: En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson escribi?: > I was wondering if this is due to the server having a invalid server > cert? If I go to this server in my browser, I get a "This server > tried to identify itself with invalid information". Is there a way to > ignore this issue with Python? Can I setup a trust store and add this > server to the trust store? I don't see the point in trusting someone that you know is telling lies about itself. -- Gabriel Genellina From kyrie at uh.cu Mon Jul 27 14:25:47 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Mon, 27 Jul 2009 14:25:47 -0400 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: <1248719147.4a6df12b61835@mail.uh.cu> Quoting Reckoner : > Hi, > > Observe the following: > > In [202]: class Foo(): > .....: def __init__(self,h=[]): > .....: self.h=h [...] > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? Shouldn't > g.h stay []? What you are seeing is basically the same that happens here: === In [1]: def f(l=[]): ...: l.append(1) ...: print l ...: In [2]: f() [1] In [3]: f() [1, 1] In [4]: f() [1, 1, 1] === The problem is that the default value "[]" is evaluated only once, at function creation time, and not at invocation. Thus, every call to the function shares the same default object. That is consistent with python's function type: the "def" construct just creates a function object and initializes it with the code, argument list and default values. That means that the default value are part of the function object itself, regardless of when/if it is called: === In [5]: f.func_defaults Out[5]: ([1, 1, 1],) === The recommended way of dealing with this case (mutable default arguments) is: def f(l = None): if l is None: l = [] # the code goes here. (in your case, your g.__init__ and f.__init__ methods share the same Foo.__init__ function, and thus, share the same default value []) That is a very common python gotcha, and I think it is well documented in the standard doc (but I can't find it right now, sorry). Unfortunately, it doesn't become intuitive until you've spent a while understanding python's execution model (and then you suddenly realize that it just makes sense). [And now I wonder... how other languages do it? I've spent so much time with python that reevaluating the default argument on invocation feels clumsy, but I'm obviously tainted now...] Regards, -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From niedziala at gazeta.pl Mon Jul 27 14:43:36 2009 From: niedziala at gazeta.pl (Piotrek) Date: Mon, 27 Jul 2009 20:43:36 +0200 Subject: where do I put resources (images, audio files) when I wrote Python program? Message-ID: Hello, I write a Python program. It will contain some images (in .png format), some audio files (as .ogg) etc. Now I think where should my installer put these files and how should I access them. What is the normal Python way of doing that? I think about puting these files in /usr/share/myprogram and then reading it the normal way (so the path "/usr/share/myprogram" would be just hardwired in my program). Is it the way one usually does it in Python program or is there any more sofisticated way? From jakecjacobson at gmail.com Mon Jul 27 14:52:08 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Mon, 27 Jul 2009 11:52:08 -0700 (PDT) Subject: bad certificate error References: Message-ID: <157f16f9-2172-4fd5-8ec8-8d9569a3f186@f20g2000prn.googlegroups.com> On Jul 27, 2:23?pm, "Gabriel Genellina" wrote: > En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson ? > escribi?: > > > I was wondering if this is due to the server having a invalid server > > cert? ?If I go to this server in my browser, I get a "This server > > tried to identify itself with invalid information". ?Is there a way to > > ignore this issue with Python? ?Can I setup a trust store and add this > > server to the trust store? > > I don't see the point in trusting someone that you know is telling lies ? > about itself. > > -- > Gabriel Genellina It is a test box that the team I am on runs. That is why I would trust it. From mhearne808 at gmail.com Mon Jul 27 14:57:28 2009 From: mhearne808 at gmail.com (mhearne808) Date: Mon, 27 Jul 2009 11:57:28 -0700 (PDT) Subject: where do I put resources (images, audio files) when I wrote Python program? References: Message-ID: On Jul 27, 12:43?pm, Piotrek wrote: > Hello, > > I write a Python program. It will contain some images (in .png format), some > audio files (as .ogg) etc. Now I think where should my installer put these > files and how should I access them. What is the normal Python way of doing > that? I think about puting these files in /usr/share/myprogram and then > reading it the normal way (so the path "/usr/share/myprogram" would be just > hardwired in my program). Is it the way one usually does it in Python > program or is there any more sofisticated way? Usually the preferred method is either distutils (http:// docs.python.org/library/distutils.html#module-distutils) or setuptools (http://peak.telecommunity.com/DevCenter/setuptools). Either of these will allow you to create source (".tar.gz") or binary distributions that can be installed relatively easily on a target machine. Good luck, Mike From wilk at flibuste.net Mon Jul 27 15:04:59 2009 From: wilk at flibuste.net (William Dode) Date: 27 Jul 2009 19:04:59 GMT Subject: New implementation of re module References: Message-ID: <4a6dfa5b$0$10245$426a74cc@news.free.fr> On 27-07-2009, MRAB wrote: > Hi all, > > I've been working on a new implementation of the re module. The details > are at http://bugs.python.org/issue2636, specifically from > http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for > Python 2.6 on Windows if you want to try it out. > Someone can remember me how to compile it (on debian lenny), if possible with python2.5. I've also python3.1 that i build alone... I could test it with pytextile, i've a bunch of texts to bench and compare. Did you announce it on the unladen-swallow list ? They wanted to hack on RE also... -- William Dod? - http://flibuste.net Informaticien Ind?pendant From joshua at joshuakugler.com Mon Jul 27 15:09:47 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Mon, 27 Jul 2009 11:09:47 -0800 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <24654358.post@talk.nabble.com> Message-ID: Dr. Phillip M. Feldman wrote: > > "As far as I know, there is no programming language which treats scalars > like ints as if they were > vectors of length 1" > > Actually, Matlab does: > >>> length(5) > ans = > 1 >>> Oddly enough, so does Perl: $ print length(44) 2 (that's in the Zoidberg shell) j From deets at nospam.web.de Mon Jul 27 15:26:46 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 21:26:46 +0200 Subject: Wrapping prstat on Solaris In-Reply-To: References: Message-ID: <7d6dbmF2ahp61U1@mid.uni-berlin.de> skip at pobox.com schrieb: > At work we currently use top to monitor ongoing system utilization on our > Solaris systems. As time has moved on though, use of top has become > problematic. Our admins want us to switch to prstat, Sun's top-like > command. It works fine however doesn't emit a timestamp at each display > interval, so it's essentially impossible looking at a prstat output > file to determine when the particular "screen" was emitted. > > If figured, "No problem. I'll just write a little wrapper." Alas, that is > proving harder than I thought. I can run prstat directing output to a file > and it seems to blast out a block of lines every N seconds, but when run > from inside a Python script I can't seem to make the damn thing not > massively buffer its output. Accordingly, my script doesn't really see the > output as its emitted, so the timestamp line it prepends to a block of > output is off. > > I'm currently using subprocess.Popen like so: > > os.environ["TERM"] = "dumb" > cmd = "prstat -c %s" % " ".join(sys.argv[1:]) > pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout > > I've tried these other variants as well: > > * prefacing the prstat command with unbuffer (the tcl/expect thingamabob) > > * explicitly redirect the prstat output to /dev/stdout > > * setting bufsize to 0 > > * used os.popen instead of Subprocess.Popen > > Nothing seems to help. I always seem to see about 30 seconds of data at > once (I'm currently using a 5-second interval and 10 lines of output). I > would have expected that prstat would simply flush stdout after each block > of output. > > Any ideas about how to get prstat to cooperate better? Use pexpect, which will make the program believe it runs in a terminal, and not buffer the output. Diez From invalid at invalid Mon Jul 27 15:27:47 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 27 Jul 2009 14:27:47 -0500 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <24654358.post@talk.nabble.com> Message-ID: On 2009-07-27, Joshua Kugler wrote: > Dr. Phillip M. Feldman wrote: > >> "As far as I know, there is no programming language which >> treats scalars like ints as if they were vectors of length 1" >> >> Actually, Matlab does: >> >>>> length(5) >> ans = >> 1 >>>> > > Oddly enough, so does Perl: > > $ print length(44) > 2 No, that's not really treating scalars as vectors of length 1. Were it doing so, length(44) would be 1 rather than 2. What Perl does is treat everything as a string. -- Grant Edwards grante Yow! I was making donuts at and now I'm on a bus! visi.com From wolfgang at rohdewald.de Mon Jul 27 15:27:55 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Mon, 27 Jul 2009 21:27:55 +0200 Subject: New implementation of re module In-Reply-To: <4A6DD6FB.8040609@mrabarnett.plus.com> References: <4A6DD6FB.8040609@mrabarnett.plus.com> Message-ID: <200907272127.55328.wolfgang@rohdewald.de> On Monday 27 July 2009, MRAB wrote: > I've been working on a new implementation of the re module. The > details are at http://bugs.python.org/issue2636, specifically from > http://bugs.python.org/issue2636#msg90954. I've included a .pyd > file for Python 2.6 on Windows if you want to try it out. how do I compile _regex.c on Linux? -- Wolfgang From benjamin.kaplan at case.edu Mon Jul 27 15:30:31 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 27 Jul 2009 15:30:31 -0400 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: On Mon, Jul 27, 2009 at 1:40 PM, Reckoner wrote: > Hi, > > Observe the following: > > In [202]: class Foo(): > ? .....: ? ? def __init__(self,h=[]): > ? .....: ? ? ? ? self.h=h > ? .....: > ? .....: > > In [203]: f=Foo() > > In [204]: g=Foo() > > In [205]: g.h > Out[205]: [] > > In [206]: f.h > Out[206]: [] > > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? ?Shouldn't > g.h stay []? > > What am I missing here? > > I'm using Python 2.5.1. > > Thanks in advance. . It has to do with the way Python handles functions and class methods. Basically, when the parser creates the class Foo, it also creates all of Foo's class variables and functions. It's at this point, and only at this point, that the default parameter is evaluated. When you make an instance of Foo, just wraps the function __init__ by filling in the initial parameter. Because both f and g are really using the Foo.__init__ function (as opposed to separate f.__init__ and g.__init__ methods), they share the same default parameter, a list that was created at the very beginning. Since f.h and g.h both refer to the same list, updating f.h will also update g.h (and the default parameter). That's why it's a bad idea to use mutable default parameters- use either None class Foo(object) : def __init__(self, h=None) : if h is None: h = [] or, if None is an acceptable value, make a sentinel value sentinel = object() class Foo(object) : def __init__(self, h = sentinel) : if h is sentinel: h = [] > -- > http://mail.python.org/mailman/listinfo/python-list > From rlichlighter at gmail.com Mon Jul 27 15:33:21 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 12:33:21 -0700 (PDT) Subject: Convert raw binary file to ascii References: Message-ID: On Jul 27, 2:07?pm, Peter Otten <__pete... at web.de> wrote: > r2 wrote: > > On Jul 27, 9:06 am, Peter Otten <__pete... at web.de> wrote: > >> r2 wrote: > >> > I have a memory dump from a machine I am trying to analyze. I can view > >> > the file in a hex editor to see text strings in the binary code. I > >> > don't see a way to save these ascii representations of the binary, so > >> > I went digging into Python to see if there were any modules to help. > > >> > I found one I think might do what I want it to do - the binascii > >> > module. Can anyone describe to me how to convert a raw binary file to > >> > an ascii file using this module. I've tried? Boy, I've tried. > > >> That won't work because a text editor doesn't need any help to convert > >> the bytes into characters. If it expects ascii it just will be puzzled by > >> bytes that are not valid ascii. Also, it will happily display byte > >> sequences that are valid ascii, but that you as a user will see as > >> gibberish because they were meant to be binary data by the program that > >> wrote them. > > >> > Am I correct in assuming I can get the converted binary to ascii text > >> > I see in a hex editor using this module? I'm new to this forensics > >> > thing and it's quite possible I am mixing technical terms. I am not > >> > new to Python, however. Thanks for your help. > > >> Unix has the "strings" commandline tool to extract text from a binary. > >> Get hold of a copy of the MinGW tools if you are on windows. > > >> Peter > > > Okay. Thanks for the guidance. I have a machine with Linux, so I > > should be able to do what you describe above. Could Python extract the > > strings from the binary as well? Just wondering. > > As a special service for you here is a naive implementation to build upon: > > #!/usr/bin/env python > import sys > > wanted_chars = ["\0"]*256 > for i in range(32, 127): > ? ? wanted_chars[i] = chr(i) > wanted_chars[ord("\t")] = "\t" > wanted_chars = "".join(wanted_chars) > > THRESHOLD = 4 > > for s in sys.stdin.read().translate(wanted_chars).split("\0"): > ? ? if len(s) >= THRESHOLD: > ? ? ? ? print s > > Peter- Hide quoted text - > > - Show quoted text - Perfect! Thanks. From mondi at cs.unibo.it Mon Jul 27 15:35:51 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Mon, 27 Jul 2009 19:35:51 +0000 Subject: ioctl on socket Message-ID: Is there a reason why there is no ioctl interface for socket either then for windows platform? It's technical issues or what else?? thank in advance jacopo From python at mrabarnett.plus.com Mon Jul 27 16:00:48 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 27 Jul 2009 21:00:48 +0100 Subject: New implementation of re module In-Reply-To: <4a6dfa5b$0$10245$426a74cc@news.free.fr> References: <4a6dfa5b$0$10245$426a74cc@news.free.fr> Message-ID: <4A6E0770.4000908@mrabarnett.plus.com> William Dode wrote: > On 27-07-2009, MRAB wrote: >> Hi all, >> >> I've been working on a new implementation of the re module. The details >> are at http://bugs.python.org/issue2636, specifically from >> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >> Python 2.6 on Windows if you want to try it out. >> > > Someone can remember me how to compile it (on debian lenny), if possible > with python2.5. I've also python3.1 that i build alone... > > I could test it with pytextile, i've a bunch of texts to bench and > compare. > All I can do is point you to http://docs.python.org/extending/extending.html. For Linux (which I don't have) you'll need to use _regex.h and _regex.c to compile to _regex.so instead of _regex.pyd. > Did you announce it on the unladen-swallow list ? They wanted to hack on > RE also... > No. I haven't subscribed to it. From davea at ieee.org Mon Jul 27 16:07:16 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 16:07:16 -0400 Subject: Convert raw binary file to ascii In-Reply-To: References: Message-ID: <4A6E08F4.8080203@ieee.org> r2 wrote: > On Jul 27, 9:06 am, Peter Otten <__pete... at web.de> wrote: > >> r2 wrote: >> >>> I have a memory dump from a machine I am trying to analyze. I can view >>> the file in a hex editor to see text strings in the binary code. I >>> don't see a way to save these ascii representations of the binary, so >>> I went digging into Python to see if there were any modules to help. >>> >>> I found one I think might do what I want it to do - the binascii >>> module. Can anyone describe to me how to convert a raw binary file to >>> an ascii file using this module. I've tried? Boy, I've tried. >>> >> That won't work because a text editor doesn't need any help to convert the >> bytes into characters. If it expects ascii it just will be puzzled by bytes >> that are not valid ascii. Also, it will happily display byte sequences that >> are valid ascii, but that you as a user will see as gibberish because they >> were meant to be binary data by the program that wrote them. >> >> >>> Am I correct in assuming I can get the converted binary to ascii text >>> I see in a hex editor using this module? I'm new to this forensics >>> thing and it's quite possible I am mixing technical terms. I am not >>> new to Python, however. Thanks for your help. >>> >> Unix has the "strings" commandline tool to extract text from a binary. >> Get hold of a copy of the MinGW tools if you are on windows. >> >> Peter >> > > Okay. Thanks for the guidance. I have a machine with Linux, so I > should be able to do what you describe above. Could Python extract the > strings from the binary as well? Just wondering. > > Yes, you could do the same thing in Python easily enough. And with the advantage that you could define your own meanings for "characters." The memory dump could be storing characters that are strictly ASCII. Or it could have EBCDIC, or UTF-8. And it could be Unicode, 16 bit or 32 bits, and big-endian or little-endian. Or the characters could be in some other format specific to a particular program. However, it's probably very useful to see what a "strings" program might look like, because you can quickly code variations on it, to suit your particular data. Something like the following (totally untested) def isprintable(char): return 0x20 <= char <= 0x7f def string(filename): data = open(filename, "rb").read() count = 0 line = "" for ch in data: if isprintable(ch): count += 1 line = line + ch else: if count > 4 : #cutoff, don't print strings smaller than this because they're probably just coincidence print line count = 0 line= "" print line Now you can change the definition of what's "printable", you can change the min-length that you care about. And of course you can fine-tune things like max-length lines and such. DaveA From davea at dejaviewphoto.com Mon Jul 27 16:14:14 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Mon, 27 Jul 2009 16:14:14 -0400 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: <4A6E0A96.60001@dejaviewphoto.com> Benjamin Kaplan wrote: > On Sun, Jul 26, 2009 at 2:24 PM, John Nagle wrote: > >> Beni Cherniavsky wrote: >> >>> On Jul 22, 9:36 am, Hendrik van Rooyen >>> wrote: >>> >>>> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >>>> >>> problem. >>> >> An interesting issue is Python objects, which are always mutable. >> A "dict" of Python objects is allowed, but doesn't consider >> the contents of the objects, just their identity (address). Only >> built-in types are immutable; one cannot create a class of immutable >> objects. >> So things like "frozenset" had to be built into the language. >> A tuple is really a frozen list. Arguably, frozen objects >> should have been a general concept. Conceptually, they're >> simple - once "__init__" has run, there can be no more changes >> to fields of the object. >> >> Compare the C++ approach, where you can have a "map" of >> any object that defines the "<" operator. Python has >> "__cmp__" for objects, but built-in dictionaries don't use it. >> Of course, one could create a "map" class in Python that >> uses "__cmp__", which would allow dictionary-type operations >> on arbitrary objects. >> > > > Python dictionaries are hash maps so they use hashes, not comparisons, > to store objects. By default the hash is equal to the object's > identity but all you need to do to change it is define your own > __hash__ method. If you were to make a C++ hash map, it wouldn't use > comparisons either. > > > > I think you'll find that to make a proper dict of user-objects, you need to define not only __hash__(), but also __eq__() methods. A hash is only a starting place. In case of collision, the dict class calls another method to make sure. And of course, for the dict to function reasonably, you have to make sure that the hash and eq functions return consistent results, at least pretending that the objects involved are immutable. DaveA From ccurvey at gmail.com Mon Jul 27 16:16:28 2009 From: ccurvey at gmail.com (Chris Curvey) Date: Mon, 27 Jul 2009 13:16:28 -0700 (PDT) Subject: PyPDF and print restrictions Message-ID: <90034475-cf07-4a57-b8cf-ec2949e0e544@d23g2000vbm.googlegroups.com> Has anyone out there been able to enforce print restrictions on a PDF document by using PyPDF? The documentation for "encrypt" states: # @param user_pwd The "user password", which allows for opening and reading # the PDF file with the restrictions provided. But there is no parameter for providing a restriction, and I can't find a reference to any kind of restriction besides this comment in the docs. Thanks in advance! From nagle at animats.com Mon Jul 27 16:24:34 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 13:24:34 -0700 Subject: M2Crypto hangs on this URL In-Reply-To: <4a6deda5$0$1639$742ec2ed@news.sonic.net> References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> <4a6deda5$0$1639$742ec2ed@news.sonic.net> Message-ID: <4a6e0c53$0$1644$742ec2ed@news.sonic.net> John Nagle wrote: > John Nagle wrote: >> There's something strange about this URL: >> >> "https://sagar310.pontins.com/sraep/" >> >> It hangs Firefox 2; there's no short timeout, the web page just gets >> stuck in initial load for about ten minutes. Then >> "The connection to sagar310.pontins.com was interrupted while the page >> was loading." >> >> It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. > > Correction: Linux is still hung at 5 hours, but Windows timed out > after about 15 minutes. > Further update: M2Crypto on Windows now hung on that URL for hours. Last time, the connection apparently failed and the program got unstuck. It's waiting for something to happen inside SSL connection setup. John Nagle From djames.suhanko at gmail.com Mon Jul 27 16:25:32 2009 From: djames.suhanko at gmail.com (Djames Suhanko) Date: Mon, 27 Jul 2009 17:25:32 -0300 Subject: Discovery IP in connection Message-ID: Hello,all! I wrote a little programa that listening UDP packets. A can receive the messages, but I can't see the the origin IP. How can I to get the remote IP address in connection? My source: #!/usr/bin/env python import socket mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM ) mySocket.bind ( ( '', 514 ) ) data, addr = mySocket.recvfrom(100) print data -- Djames Suhanko LinuxUser 158.760 From 71david at libero.it Mon Jul 27 16:35:01 2009 From: 71david at libero.it (David) Date: Mon, 27 Jul 2009 22:35:01 +0200 Subject: Gracefully exiting CLI application Message-ID: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Greetings, I am writing a command line application, and I need to perform some cleaning on exit even if the process is killed. How can I do that with python? Thank you. David. From erikwickstrom at gmail.com Mon Jul 27 16:38:25 2009 From: erikwickstrom at gmail.com (erikcw) Date: Mon, 27 Jul 2009 13:38:25 -0700 (PDT) Subject: Download the "head" of a large file? Message-ID: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> I'm trying to figure out how to download just the first few lines of a large (50mb) text file form a server to save bandwidth. Can Python do this? Something like the Python equivalent of curl http://url.com/file.xml | head -c 2048 Thanks! Erik From tjreedy at udel.edu Mon Jul 27 16:47:34 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 27 Jul 2009 16:47:34 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <027d38a5$0$5185$c3e8da3@news.astraweb.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> <027d38a5$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 27 Jul 2009 02:02:19 -0400, Terry Reedy wrote: > >> Steven D'Aprano wrote: >>> On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: >>> >>>> Michal Kwiatkowski wrote: >>>> >>>>> The thing is I don't need the next item. I need to know if the >>>>> generator has stopped without invoking it. >>>> Write a one-ahead iterator class, which I have posted before, that >>>> sets .exhausted to True when next fails. >>> >>> And hope that the generator doesn't have side-effects... >> If run to exhastion, the same number of side-effects happen. The only >> difference is that they each happen once step happpen sooner. For >> reading a file that is irrelevant. Much else, and the iterator is not >> just an iterator. > > I believe the OP specifically said he needs to detect whether or not an > iterator is exhausted, without running it to exhaustion, so you shouldn't > assume that the generator has been exhausted. I believe the OP said he needs to determine whether or not an iterator (specifically generator) is exhausted without consuming an item when it is not. That is slightly different. The wrapper I suggested makes that easy. I am obviously not assuming exhaustion when there is a .exhausted True/False flag to check. There are two possible definition of 'exhausted': 1) will raise StopIteration on the next next() call; 2) has raised StopIteration at least once. The wrapper converts 2) to 1), which is to say, it obeys definition 1 once the underlying iteration has obeyed definition 2. Since it is trivial to set 'exhausted=True' in the generator user code once StopIteration has been raised (meaning 2), I presume the OP wants the predictive meaning 1). Without a iterator class wrapper, I see no way to predict what a generator will do (expecially, raise StopIteration the first time) without analyzing its code and local variable state. I said in response to YOU that once exhaustion has occurred, then the same number of side effects would have occurred. > When it comes to side-effects, timing matters. Sometimes. And I admitted that possibility (slight garbled). For example, a generator > which cleans up after it has run (deleting temporary files, closing > sockets, etc.) will leave the environment in a different state if run to > exhaustion than just before exhaustion. Even if you store the final > result in a one-ahead class, you haven't saved the environment, and that > may be significant. Of course, an eager-beaver generator written to be a good citizen might well close resources as soon as it knows *they* are exhausted, long before *it* yields the last items from the in-memory last block read. For all I know, file.readlines could do such. Assuming that is not the case, the cleanup will not happen until the what turns out to be the final item is requested from the wrapper. Once cleanup has happened, .exhausted will be set to True. If proper processing of even the last item requires that cleanup not have happened, then that and prediction of exhaustion are incompatible. One who wants both should write an iterator class instead of generator function. > (Of course, it's possible that it isn't significant. Not all differences > make a difference.) > > The best advice is, try to avoid side-effects, especially in generators. Agreed. Terry Jan Reedy From davea at ieee.org Mon Jul 27 16:53:12 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 16:53:12 -0400 Subject: where do I put resources (images, audio files) when I wrote Python program? In-Reply-To: References: Message-ID: <4A6E13B8.4080806@ieee.org> Piotrek wrote: > Hello, > > I write a Python program. It will contain some images (in .png format), some > audio files (as .ogg) etc. Now I think where should my installer put these > files and how should I access them. What is the normal Python way of doing > that? I think about puting these files in /usr/share/myprogram and then > reading it the normal way (so the path "/usr/share/myprogram" would be just > hardwired in my program). Is it the way one usually does it in Python > program or is there any more sofisticated way? > > > I think this question is mostly independent of the issue of what's used to actually install the files. My answer is to put read-only files right with the py* files, and writable files in the user's space. In the former case, you can find the files by parsing __file__ for one of your modules. In the latter case, it's system dependent. But I'd start with a single file which is put in a conventional place (for your target OS). That will be a configuration file ("preferences files"), which specifies, among other things, where everything else will be written. Somebody else will have to tell you what the Unix convention would be. In the case of Windows, the first "file" would just be a registry entry. And I wouldn't write any of the actual files till the user had configured things, either during installation or during the first run (with a File->Preferences menu setup). From zapwireDASHgroups at yahoo.com Mon Jul 27 17:04:20 2009 From: zapwireDASHgroups at yahoo.com (Joel Koltner) Date: Mon, 27 Jul 2009 14:04:20 -0700 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: "Dr. Phillip M. Feldman" wrote in message news:mailman.3699.1248490256.8015.python-list at python.org... > Here's a simple-minded example: ... > This function works fine if xs is a list of floats, but not if it is single > float. It can be made to work as follows: Wow, you could substitute "Matlab" for "Python" in your post and it'd still be just as good. :-) From zuo at chopin.edu.pl Mon Jul 27 17:09:14 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Mon, 27 Jul 2009 23:09:14 +0200 Subject: Convert raw binary file to ascii In-Reply-To: References: Message-ID: Hello Friends, It's my first post to python-list, so first let me introduce myself... * my name is Jan Kaliszewski, * country -- Poland, * occupation -- composer (studied in F. Chopin Academy of Music @Warsaw) and programmer (currently in Record System company, working on Anakonda -- ERP system for big companies [developed in Python + WX + Postgres]). Now, to the matter... 27-07-2009 Grant Edwards wrote: > On 2009-07-27, r2 wrote: > >> I have a memory dump from a machine I am trying to analyze. I can view >> the file in a hex editor to see text strings in the binary code. I >> don't see a way to save these ascii representations of the binary, > > $ strings memdump.binary >memdump.strings > > $ hexdump -C memdump.binary >memdump.hex+as Do You (r2) want to do get ASCII substrings (i.e. extract only those pieces of file that consist of ASCII codes -- i.e. 7-bit values -- i.e in range 0...127), or rather "possibly readable ascii representation" of the whole file, with printable ascii characters preserved 'as is' and not-printable/non-ascii characters being replaced with their codes (e.g. with '\x...' notation). If the latter, you probably want something like this: import codecs with open('memdump.binary', 'rb') as source: with open('memdump.txt', 'w') as target: for quasiline in codecs.iterencode(source, 'string_escape'): target.write(quasiline) -- Jan Kaliszewski (zuo) From martin.hellwig at dcuktec.org Mon Jul 27 17:12:23 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Mon, 27 Jul 2009 22:12:23 +0100 Subject: M2Crypto hangs on this URL In-Reply-To: <4a6e0c53$0$1644$742ec2ed@news.sonic.net> References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> <4a6deda5$0$1639$742ec2ed@news.sonic.net> <4a6e0c53$0$1644$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > John Nagle wrote: >> John Nagle wrote: >>> There's something strange about this URL: >>> >>> "https://sagar310.pontins.com/sraep/" >>> >>> It hangs Firefox 2; there's no short timeout, the web page just gets >>> stuck in initial load for about ten minutes. Then >>> "The connection to sagar310.pontins.com was interrupted while the >>> page was loading." >>> >>> It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. >> >> Correction: Linux is still hung at 5 hours, but Windows timed out >> after about 15 minutes. >> > Further update: M2Crypto on Windows now hung on that URL for hours. > Last time, > the connection apparently failed and the program got unstuck. > > It's waiting for something to happen inside SSL connection setup. > > John Nagle It looks to me like the SSL handshake is not done properly from the server side. Compare the output of: openssl s_client -host sagar310.pontins.com -port 443 -debug -showcerts -msg With (for example): openssl s_client -host www.google.com -port 443 -debug -showcerts -msg -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From shai at platonix.com Mon Jul 27 17:15:00 2009 From: shai at platonix.com (Shai) Date: Mon, 27 Jul 2009 14:15:00 -0700 (PDT) Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: On Jul 27, 5:05?pm, Jean-Michel Pichavant wrote: > Ben Finney wrote: > > > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > > the method, the original docstring is not associated with it. > > I've also tried within the python interpreter, and it can perfectly > solve docstring inheritance. So why would you explicitly assign > docstring to child methods ? > What do you mean by "can perfectly solve docstring inheritance" ? After the following, class Foo(object): def foo(self): "Frobber" pass class Bar(Foo): def foo(self): pass help(Bar.foo) does not display "Frobber" on my interpreter. From zuo at chopin.edu.pl Mon Jul 27 17:35:23 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Mon, 27 Jul 2009 23:35:23 +0200 Subject: Gracefully exiting CLI application In-Reply-To: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Message-ID: 27-07-2009 o 22:35:01 David <71david at libero.it> wrote: > I am writing a command line application, and I need to perform some > cleaning > on exit even if the process is killed. > How can I do that with python? See: http://docs.python.org/library/signal.html Cheers, *j -- Jan Kaliszewski (zuo) From constant.beta at gmail.com Mon Jul 27 17:37:57 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Mon, 27 Jul 2009 14:37:57 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> <027d38a5$0$5185$c3e8da3@news.astraweb.com> Message-ID: <47bfd80f-2f61-462e-8789-811a2c836b5e@n2g2000vba.googlegroups.com> On Jul 27, 10:47?pm, Terry Reedy wrote: > There are two possible definition of 'exhausted': 1) will raise > StopIteration on the next next() call; 2) has raised StopIteration at > least once. The wrapper converts 2) to 1), which is to say, it obeys > definition 1 once the underlying iteration has obeyed definition 2. > > Since it is trivial to set 'exhausted=True' in the generator user code > once StopIteration has been raised (meaning 2), I presume the OP wants > the predictive meaning 1). No, I meant the second meaning (i.e. generator is exhausted when it has returned instead of yielding). While, as you showed, it is trivial to create a generator that will have the "exhausted" flag, in my specific case I have no control over the user code. I have to use what the Python genobject API gives me plus the context of the trace function. Cheers, mk From ben+python at benfinney.id.au Mon Jul 27 17:40:32 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 28 Jul 2009 07:40:32 +1000 Subject: Gracefully exiting CLI application References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Message-ID: <873a8h4xf3.fsf@benfinney.id.au> David <71david at libero.it> writes: > I am writing a command line application, and I need to perform some > cleaning on exit even if the process is killed. How can I do that with > python? Write an ?exit handler? function, then use ?atexit.register? to register yours for processing whenever the program exits. -- \ ?Pinky, are you pondering what I'm pondering?? ?Wuh, I think | `\ so, Brain, but will they let the Cranberry Duchess stay in the | _o__) Lincoln Bedroom?? ?_Pinky and The Brain_ | Ben Finney From zuo at chopin.edu.pl Mon Jul 27 17:52:27 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Mon, 27 Jul 2009 23:52:27 +0200 Subject: Gracefully exiting CLI application In-Reply-To: <873a8h4xf3.fsf@benfinney.id.au> References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> <873a8h4xf3.fsf@benfinney.id.au> Message-ID: 27-07-2009 Ben Finney wrote: > David <71david at libero.it> writes: > >> I am writing a command line application, and I need to perform some >> cleaning on exit even if the process is killed. How can I do that with >> python? > > Write an ?exit handler? function, then use ?atexit.register? > to register yours for > processing whenever the program exits. Unfortunately neither sys.exitfunc nor function registered with atexit.register() are called when process is killed with signal. As I wrote, you must use signals. Though sometimes it's a good idea to combine these two techniques (i.e. signal handlers call sys.exit(), then sys.exitfunc/or function registered with atexit does the actual cleaning actions). *j -- Jan Kaliszewski (zuo) From bcharrow at csail.mit.edu Mon Jul 27 18:33:51 2009 From: bcharrow at csail.mit.edu (Ben Charrow) Date: Mon, 27 Jul 2009 18:33:51 -0400 Subject: Download the "head" of a large file? In-Reply-To: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> Message-ID: <4A6E2B4F.70102@csail.mit.edu> erikcw wrote: > ...download just the first few lines of a large (50mb) text file form a > server to save bandwidth..... Something like the Python equivalent of curl > http://url.com/file.xml | head -c 2048 If you're OK calling curl and head from within python: from subprocess import Popen, PIPE url = "http://docs.python.org/" p1 = Popen(["curl", url], stdout = PIPE, stderr = PIPE) p2 = Popen(["head", "-c", "1024"], stdin = p1.stdout, stdout = PIPE) p2.communicate()[0] If you want a pure python approach: import urllib2 url = "http://docs.python.org/" req = urllib2.Request(url) f = urllib2.urlopen(req) f.read(1024) HTH, Ben From gallium.arsenide at gmail.com Mon Jul 27 18:40:25 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Mon, 27 Jul 2009 15:40:25 -0700 (PDT) Subject: Download the "head" of a large file? References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> Message-ID: <5579245f-b8cb-44cc-9b5f-1e7009422a58@g19g2000vbi.googlegroups.com> On Jul 27, 4:38?pm, erikcw wrote: > I'm trying to figure out how to download just the first few lines of a > large (50mb) text file form a server to save bandwidth. ?Can Python do > this? > > Something like the Python equivalent of curlhttp://url.com/file.xml| > head -c 2048 urllib.urlopen gives you a file-like object, which you can then read line by line or in fixed-size chunks. For example: import urllib chunk = urllib.urlopen('http://url.com/file.xml').read(2048) At that point, chunk is just bytes, which you can write to a local file, print, or whatever it is you want. John From steve at REMOVE-THIS-cybersource.com.au Mon Jul 27 19:00:27 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 27 Jul 2009 23:00:27 GMT Subject: bad certificate error References: Message-ID: <027e226c$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 15:23:41 -0300, Gabriel Genellina wrote: > En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson > escribi?: > >> I was wondering if this is due to the server having a invalid server >> cert? If I go to this server in my browser, I get a "This server tried >> to identify itself with invalid information". Is there a way to ignore >> this issue with Python? Can I setup a trust store and add this server >> to the trust store? > > I don't see the point in trusting someone that you know is telling lies > about itself. Don't you? It's just commonsense risk assessment. It's far more likely that the server has an incorrectly setup certificate managed by an incompetent sys admin than it is that somebody is eaves- dropping on my connection to https://somerandom.site.com/something-trivial Particularly when the connection is on my own intranet and I know the sys admin in question has made a mistake and the new certificate is scheduled to be installed "some time next week". *wink* Of course, if there was sensitive information involved, or money, then I'd be more concerned. -- Steven From gherron at islandtraining.com Mon Jul 27 19:06:36 2009 From: gherron at islandtraining.com (Gary Herron) Date: Mon, 27 Jul 2009 16:06:36 -0700 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: <4A6E32FC.4080202@islandtraining.com> Reckoner wrote: > Hi, > > Observe the following: > > In [202]: class Foo(): > .....: def __init__(self,h=[]): > .....: self.h=h > .....: > .....: > > In [203]: f=Foo() > > In [204]: g=Foo() > > In [205]: g.h > Out[205]: [] > > In [206]: f.h > Out[206]: [] > > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? Shouldn't > g.h stay []? > > What am I missing here? > > I'm using Python 2.5.1. > > Thanks in advance. > This is a FAQ (and *very* common newbie trap)! See: http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects Gary Herron From drobinow at gmail.com Mon Jul 27 19:07:06 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 27 Jul 2009 18:07:06 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: <4eb0089f0907271607m1dd34f39qaec04a650afdd31f@mail.gmail.com> On Mon, Jul 27, 2009 at 9:49 AM, Aahz wrote: > In article , > Hendrik van Rooyen ? wrote: >>On Sunday 26 July 2009 21:26:46 David Robinow wrote: >>> >>> ?I'm a mediocre programmer. Does this mean I should switch to PHP? >> >>I have searched, but I can find nothing about this mediocre language. >> >>Could you tell us more? > > (For anyone who is confused by Hendrik's humor, he is saying that David > was referring to a programming language named "mediocre". ?English > grammar is confusing!) Well, Aahz, I think it rather rude to spoil the fun with your explanation. Just for that, I'm not going to post any mediocre code. You'll have to figure it out yourself. (Q: should the name of my favorite language be capitalized?) From roy at panix.com Mon Jul 27 19:21:43 2009 From: roy at panix.com (Roy Smith) Date: Mon, 27 Jul 2009 19:21:43 -0400 Subject: Discovery IP in connection References: Message-ID: In article , Djames Suhanko wrote: > Hello,all! > I wrote a little programa that listening UDP packets. A can receive > the messages, but I can't see the the origin IP. > How can I to get the remote IP address in connection? > > My source: > > #!/usr/bin/env python > import socket > mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM ) > mySocket.bind ( ( '', 514 ) ) > > data, addr = mySocket.recvfrom(100) > print data What happens when you print addr? From piet at cs.uu.nl Mon Jul 27 19:23:13 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 28 Jul 2009 01:23:13 +0200 Subject: Discovery IP in connection References: Message-ID: >>>>> Djames Suhanko (DS) wrote: >DS> Hello,all! >DS> I wrote a little programa that listening UDP packets. A can receive >DS> the messages, but I can't see the the origin IP. >DS> How can I to get the remote IP address in connection? What do you think the addr is for in data, addr = mySocket.recvfrom(100)? >DS> My source: >DS> #!/usr/bin/env python >DS> import socket >DS> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM ) >DS> mySocket.bind ( ( '', 514 ) ) >DS> data, addr = mySocket.recvfrom(100) >DS> print data -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From david.lyon at preisshare.net Mon Jul 27 19:53:00 2009 From: david.lyon at preisshare.net (David Lyon) Date: Mon, 27 Jul 2009 19:53:00 -0400 Subject: Using =?UTF-8?Q?easy=5Finstall=2C=20reduncant=3F?= In-Reply-To: References: Message-ID: <8ae7c63db1dfcc3bbc1619b67939a5d5@preisshare.net> On Mon, 27 Jul 2009 09:42:06 -0700 (PDT), ray wrote: > I am working on a Trac installation. I am new to Python. To install > packages, it is suggested to use setuptools. I have not understood > the directions. > > I execute ez_install.py. > > Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. > There response that setuptools is already the active version in easy- > install.pth. Then: > Installing easy_install.exe script to C:\Python26\Scripts error: C: > \Python26\Scripts\Easy_install.exe: Permission denied. > > I have compared the file entries before and after this attempt and > there are no new files. Is there any problems here? What did I miss? Try using python package manager : http://sourceforge.net/projects/pythonpkgmgr/ You might find it a lot simpler. It will download and install setuptools for you if you are still having problems. David From r1chardj0n3s at gmail.com Mon Jul 27 20:34:52 2009 From: r1chardj0n3s at gmail.com (Richard Jones) Date: Tue, 28 Jul 2009 10:34:52 +1000 Subject: Next meeting: Tuesday 11th August Message-ID: <1680A969-5DD9-4A2C-BACB-9B342C44123C@gmail.com> The next meeting of the Melbourne Python Users Group will be on Tuesday the 11th of August starting at 6:30pm. We'll be meeting at Horse Bazaar again but this time we'll have use of their projector. We'll have time for several short presentations or lightning talks. Meeting details, location and talks list are at: http://wiki.python.org/moin/MelbournePUG If you've seen something cool or are doing something cool then we'd like you to tell everyone about it! Presentations could be 5 minutes or up to 15 minutes if you'd like to ramble for a bit longer. I'll be getting up to talk a bit about my experiences playing with IronPython - what's cool and what's downright odd :) If you've got an idea for a talk just add it to the wiki page. Richard From gagsl-py2 at yahoo.com.ar Mon Jul 27 20:46:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 21:46:51 -0300 Subject: bad certificate error References: <157f16f9-2172-4fd5-8ec8-8d9569a3f186@f20g2000prn.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 15:52:08 -0300, jakecjacobson escribi?: > On Jul 27, 2:23?pm, "Gabriel Genellina" > wrote: >> En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson ? >> escribi?: >> >> > I was wondering if this is due to the server having a invalid server >> > cert? ?If I go to this server in my browser, I get a "This server >> > tried to identify itself with invalid information". ?Is there a way to >> > ignore this issue with Python? ?Can I setup a trust store and add this >> > server to the trust store? >> >> I don't see the point in trusting someone that you know is telling lies >> about itself. > > It is a test box that the team I am on runs. That is why I would > trust it. I'd fix the certificate issue on the server. Or use plain HTTP if it's just for testing. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 27 22:16:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 23:16:39 -0300 Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Mon, 27 Jul 2009 20:00:27 -0300, Steven D'Aprano escribi?: > On Mon, 27 Jul 2009 15:23:41 -0300, Gabriel Genellina wrote: >> En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson >> escribi?: >> >>> I was wondering if this is due to the server having a invalid server >>> cert? If I go to this server in my browser, I get a "This server tried >>> to identify itself with invalid information". Is there a way to ignore >>> this issue with Python? Can I setup a trust store and add this server >>> to the trust store? >> >> I don't see the point in trusting someone that you know is telling lies >> about itself. > > Don't you? It's just commonsense risk assessment. > > It's far more likely that the server has an incorrectly setup certificate > managed by an incompetent sys admin than it is that somebody is eaves- > dropping on my connection to > https://somerandom.site.com/something-trivial Fire the sys admin then :) I don't see the point on "fixing" either the Python script or httplib to accomodate for an invalid server certificate... If it's just for internal testing, I'd use HTTP instead (at least until the certificate is fixed). -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 27 22:16:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 23:16:42 -0300 Subject: Download the "head" of a large file? References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> <5579245f-b8cb-44cc-9b5f-1e7009422a58@g19g2000vbi.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 19:40:25 -0300, John Yeung escribi?: > On Jul 27, 4:38?pm, erikcw wrote: >> I'm trying to figure out how to download just the first few lines of a >> large (50mb) text file form a server to save bandwidth. ?Can Python do >> this? >> >> Something like the Python equivalent of curlhttp://url.com/file.xml| >> head -c 2048 > > urllib.urlopen gives you a file-like object, which you can then read > line by line or in fixed-size chunks. For example: > > import urllib > chunk = urllib.urlopen('http://url.com/file.xml').read(2048) > > At that point, chunk is just bytes, which you can write to a local > file, print, or whatever it is you want. As the OP wants to save bandwidth, it's better to ask exactly the amount of data to read. That is, add a Range header field [1] to the request, and inspect the response for a corresponding Content-Range header [2]. py> import urllib2 py> url = "http://www.python.org/" py> req = urllib2.Request(url) py> req.add_header('Range', 'bytes=0-10239') # first 10K py> f = urllib2.urlopen(req) py> data = f.read() py> print repr(data[-30:]), len(data) '\t > just >> hardwired in my program). Is it the way one usually does it in Python >> program or is there any more sofisticated way? >> >> > My answer is to put read-only files right with the py* files, and > writable files in the user's space. In the former case, you can find > the files by parsing __file__ for one of your modules. In the latter > case, it's system dependent. For those read-only resources I'd use pkgutil.get_data (instead of manually parsing __file__): http://docs.python.org/library/pkgutil.html#pkgutil.get_data It's easier to manage when someone later decide to install the package as an egg file, or distribute it using py2exe. Quoting the documentation: """The function returns a binary string that is the contents of the specified resource. For packages located in the filesystem, which have already been imported, this is the rough equivalent of: d = os.path.dirname(sys.modules[package].__file__) data = open(os.path.join(d, resource), 'rb').read() return data """ -- Gabriel Genellina From doug.farrell at gmail.com Mon Jul 27 22:52:01 2009 From: doug.farrell at gmail.com (writeson) Date: Mon, 27 Jul 2009 19:52:01 -0700 (PDT) Subject: Extract images from PDF files Message-ID: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Hi all, I've looked around with Google quite a bit, but haven't found anything like what I'm looking for. Is there a Python library that will extract images from PDF files? My ultimate goal is to pull the images out, use the PIL library to reduce the size of the images and rebuild another PDF file that's an essentially "thumbnail" version of the original PDF file, smaller in size. We've been using imagick to extract the images, but it's difficult to script and slow to process the input PDF. Can someone suggest something better? Thanks in advance, Doug From aahz at pythoncraft.com Mon Jul 27 22:52:26 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 19:52:26 -0700 Subject: New implementation of re module References: Message-ID: In article , MRAB wrote: > >I've been working on a new implementation of the re module. The details >are at http://bugs.python.org/issue2636, specifically from >http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >Python 2.6 on Windows if you want to try it out. How does it handle the re module's unit tests? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From dmw at coder.cl Mon Jul 27 23:02:47 2009 From: dmw at coder.cl (Daniel Molina Wegener) Date: Mon, 27 Jul 2009 23:02:47 -0400 Subject: [ANN] pyxser-1.1r --- python xml serialization Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 Hello, I'm pleased to announce pyxser-1.1r, a Python-Object to XML serializer and deserializer. This package it's completly written in C and licensed under LGPLv3. The tested Python versions are 2.5.X and 2.7.X. * home page: http://coder.cl/software/pyxser * hosted at: http://sourceforge.net/projects/pyxser/ The current ChangeLog is as follows: - -----8<----------8<----------8<----------8<----- 1.1r (2009.05.09): Daniel Molina Wegener * Removed bugs concerning XML schema declaration for output documents. * Removed bugs concerning XML schema design for C14N serialization. * Removed exc argument from C14N serialization functions, because they have no sense. * Code cleanup with function arguments (C code). * All changes seems to be transparent for previous versions. - -----8<----------8<----------8<----------8<----- Also, I need a little bit of feedback. I want it to be able to serialize files and buffers: I've planned to allow the user to have three options: ignore files, serialize them in base64 encoding and just put the file name as string. But my question concerns how the files and buffers are deserialized: I must allow the user to dump the files on a directory, put the complete file on a buffer object, or just extract the base64 encoded block? Best regards and thanks in advance... - -- .O. | Daniel Molina Wegener | FreeBSD & Linux ..O | dmw [at] coder [dot] cl | Open Standards OOO | http://coder.cl/ | FOSS Developer -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iQIcBAEBCgAGBQJKbmpXAAoJEHxqfq6Y4O5NkEEQAJd6dXtHRWY7dqajArrNLIOn RqQhzAvzmdWHSsGe6qW3F8f56q88B2Zd4U/5d53rzgR3d+bUlhddj07VpuxWjiIs 2IbJSXIeHzwwuvcB0N236DLNtKKnN4LaB29Ua+WofPK5QZ+NXX8SQMVyjgYNqV7X F98+PXQ7BLAZ96zzdxEqnvkOww1YNbxkpIYWhqWleOmE+kPTrrGP6H5yhUdqz+W9 VJRvy5FOJAyBlJSlA7HoFULvVjUx0q2EdoIY/PmLGBj0pIcMKJ0IM6sUhSTN/bjq nwJRvcOmn1LwE8TpK9ZiOiNp3/BgRV87v8buPPx4C1Mj5e1SJ3M2pziQftRdXtIp f8fI/IDA8+ekCEvC9Rkzt+iRRxknDXe5JqpukH4uSLidKCD4GbdCGVmxC5rnIodk JcnbfTdPduV94CH2OxBjPOcuBAYEeUZ3RnQRT6LPJRxm+EnOtZ2tTnY+ByVpTN7G kGD0MYI3yPa3nX9IYK07Icp/4hC8w7EitQhhxRQD/y2NdfQELdu2IQUxag7x3G/8 bX65CUBHUxMwKoT+/HzeTRi0lGHopa6ptRxIbHg2wjHBLh7HI/s7khZtzJN8CZZE AFuxsrraUzeP2ZQ/uk0cfGPC3QBlKCYL3gksnoN4Fa5sCSX5Z+bDiKSg/xEhjC7U +HMeDE431oIJthqDqfdK =7sXN -----END PGP SIGNATURE----- From huzaigang at cn.panasonic.com Mon Jul 27 23:06:06 2009 From: huzaigang at cn.panasonic.com (jammy) Date: Tue, 28 Jul 2009 11:06:06 +0800 Subject: How to get the output struct parameter by extending C function? Message-ID: <006a01ca0f30$59849b30$af97c20a@psdcd.local> Hello, I an just starting to use the ctypes module. Now I want to expentd C in python. In the function of C, I want to get the output struct parameter which is chaged in the function. following is my code: ----------- 1.Header file of C?information.h?----------- typedef struct { char name[20]; int height; int weight; } Infor; int getInfor( char* name, int height, int weight, Infor* infor); ----------- 2.Source file of C?information.c?----------- #include #include #include #include #include "Extest.h" int getInfor( char* name, int height, int weight, Infor* infor) { if(name == NULL || infor == NULL) return (-1); printf(" [%s] get params: name[%s] height[%d] weight[%d]\n", __FUNCTION__, name, height, weight); strcpy(infor->name, name); infor->height = height; infor->weight = weight; return (1); } ----------- 3.information.c's warp file for Python calling?information_wrap.c?----------- #include "Python.h" #include "ceval.h" #include "Extest.h" static PyObject * Extest_getInfor(PyObject *self, PyObject *args) { char* name; int height; int weight; Infor* infor; int res; if(!PyArg_ParseTuple(args, "siiO:infor", &name, &height, &weight, &infor)){ return NULL; } printf(" ***** Before Call: infor.name[%s] infor.height[%d] infor.weight[%d] *****\n",infor->name, infor->height, infor->weight);//[1]initial value res = getInfor(name, height, weight, infor); printf(" ***** After Call: infor.name[%s] infor.height[%d] infor.weight[%d] *****\n", infor->name, infor->height, infor->weight);//[2]changed value return (PyObject*)Py_BuildValue("i", res); } static PyMethodDef ExtestMethods[] = { { "getInfor", Extest_getInfor, METH_VARARGS }, { NULL, NULL }, }; void initExtest(void) { PyObject *m; m = Py_InitModule("Extest", ExtestMethods); } ----------- 4.Makefile----------- # Makefile of Extend C for Python OBJS = Extest.o ExtestWrappers.o TARGET = Extest.so INCLUDES = -I /usr/local/include/python2.6/ MAKEFILE = Makefile CC = gcc CFLAGS = -fPIC -Wall -Wstrict-prototypes -Winline \ ${INCLUDES} -O2 all : ${TARGET} ${TARGET} : ${OBJS} ${LIBS} ${CC} -shared -o ${TARGET} ${OBJS} rm -f ${OBJS} clean : rm -f *.o *.Z* *~ ${TARGET} ----------- 5.test file of Python?test.py?----------- #!/usr/bin/env python2.6 #coding=utf8 import Extest from ctypes import * class Infor(Structure): _fields_= [('name', c_char*20), ('height', c_int), ('weight', c_int)] def main(): infor = Infor() infor.name = 'NoName' infor.height = -1 infor.weight = -1 name = 'TestName' height = 175 weight = 160 print 'Before call getInfor(), Infor.name=%s Infor.height=%d Infor.weight=%d' % (infor.name, infor.height, infor.weight) Extest.getInfor(name, height, weight, byref(infor)) print 'After call getInfor(), Infor.name=%s Infor.height=%d Infor.weight=%d' % (infor.name, infor.height, infor.weight) [3]want to get value of infor changed print ' ----- all DONE -----' if __name__ == '__main__': main() ----------- 6.result of running test.py ----------- [root at localhost obj_struct_test]# ./test.py Before call getInfor(), Infor.name=NoName Infor.height=-1 Infor.weight=-1 sizeof(*infor)=28, sizeof(Infor)=28 ***** [Extest_getInfor] Before Call: infor.name[] infor.height[0] infor.weight[0] ***** [getInfor] get params: name[TestName] height[175] weight[160] ***** [Extest_getInfor] After Call: infor.name[TestName] infor.height[175] infor.weight[160] ***** After call getInfor(), Infor.name=NoName Infor.height=-1 Infor.weight=-1 ----- all DONE ----- Looking for the printed information, in the file of information_wrap.c(setp 3), In the comment 's tag[2], printed value is changed valuse. Why is the printed Information initial value atfer calling getInfor() in test.py? Thanks for your help! yours: jammy From mobiledreamers at gmail.com Mon Jul 27 23:31:25 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Mon, 27 Jul 2009 20:31:25 -0700 Subject: [Baypiggies] Egnyte: hiring In-Reply-To: <20090728024611.GA4649@panix.com> References: <20090728024611.GA4649@panix.com> Message-ID: Seems interesting http://fotoroll.com/searchvideo?q=vineet%20jain,%20ceo%20of%20egnyte&id=0&type=video On Mon, Jul 27, 2009 at 7:46 PM, Aahz wrote: > My new company (I just started today) closed a big venture capital round > (also today) and is looking to hire more people, preferably with Python > experience: > > Senior Linux Engineer: > http://sfbay.craigslist.org/pen/sof/1292008176.html > > Senior Platforms Engineer: > http://sfbay.craigslist.org/pen/sof/1291776907.html > > Venture announcement: > http://blogs.zdnet.com/BTL/?p=21777 > > I can't say a lot about the company myself yet, but I've gotten a good > impression of the team and the business plan. Find out more here: > http://www.egnyte.com/ > -- > Aahz (aahz at pythoncraft.com) <*> > http://www.pythoncraft.com/ > > "Many customs in this life persist because they ease friction and promote > productivity as a result of universal agreement, and whether they are > precisely the optimal choices is much less important." --Henry Spencer > _______________________________________________ > Baypiggies mailing list > Baypiggies at python.org > To change your subscription options or unsubscribe: > http://mail.python.org/mailman/listinfo/baypiggies > -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From david.lyon at preisshare.net Mon Jul 27 23:37:39 2009 From: david.lyon at preisshare.net (David Lyon) Date: Mon, 27 Jul 2009 23:37:39 -0400 Subject: Extract images from PDF files In-Reply-To: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> References: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Message-ID: pdftohtml on sourceforge may help... On Mon, 27 Jul 2009 19:52:01 -0700 (PDT), writeson wrote: > Hi all, > > I've looked around with Google quite a bit, but haven't found anything > like what I'm looking for. Is there a Python library that will extract > images from PDF files? My ultimate goal is to pull the images out, use > the PIL library to reduce the size of the images and rebuild another > PDF file that's an essentially "thumbnail" version of the original PDF > file, smaller in size. > > We've been using imagick to extract the images, but it's difficult to > script and slow to process the input PDF. Can someone suggest > something better? > > Thanks in advance, > Doug From himanshu.garg at gmail.com Tue Jul 28 00:22:29 2009 From: himanshu.garg at gmail.com (++imanshu) Date: Mon, 27 Jul 2009 21:22:29 -0700 (PDT) Subject: _msi.Record object has no attribute 'GetString' References: <32e0b242-8635-40b5-a366-2c9967e64e1c@d4g2000yqa.googlegroups.com> Message-ID: <2392afa6-59fe-4c98-8c12-403222a8ee24@a37g2000prf.googlegroups.com> On Jul 27, 4:38?pm, "++imanshu" wrote: > The documentation (http://docs.python.org/library/msilib.html#record- > objects) for msilib mentions the GetString() method on Record objects. > However, the following snippet :- > > db = msilib.OpenDatabase(os.path.join(root, file), > msilib.MSIDBOPEN_READONLY) > view = db.OpenView('SELECT * FROM Property') > view.Execute(None) > r = view.Fetch() > print dir(r) > print r.GetString(0) > > gives me this error :- > > c:\>python msi.py > ['ClearData', 'GetFieldCount', 'SetInteger', 'SetStream', 'SetString', > __class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', > '__init__', '__new__', __reduce__', '__reduce_ex__', '__repr__', > '__setattr__', '__str__'] > Traceback (most recent call last): > ? File "msi.py", line 18, in > ? ? print r.GetString(0) > AttributeError: '_msi.Record' object has no attribute 'GetString' > > Am I missing something? Known Issue. Already fixed here http://mail.python.org/pipermail/python-bugs-list/2008-February/047136.html From gagsl-py2 at yahoo.com.ar Tue Jul 28 00:34:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 01:34:42 -0300 Subject: How to get the output struct parameter by extending C function? References: <006a01ca0f30$59849b30$af97c20a@psdcd.local> Message-ID: En Tue, 28 Jul 2009 00:06:06 -0300, jammy escribi?: > I an just starting to use the ctypes module. > Now I want to expentd C in python. > In the function of C, I want to get the output struct parameter which > is chaged in the function. You're mixing a C extension with a ctypes call - why is that? ctypes is mainly used when you *don't* want to actually write a C extension - if you actually write one, you don't need ctypes at all. > ----------- 1.Header file of C(information.h)----------- > typedef struct { > char name[20]; > int height; > int weight; > } Infor; > int getInfor( char* name, int height, int weight, Infor* infor); > ----------- 5.test file of Python(test.py)----------- > Extest.getInfor(name, height, weight, byref(infor)) All arguments for a function intended to be called from Python must be PyObject*; byref is used to call a ctypes-wrapped function, not a function from an extension module. (You're lucky you didn't crash the interpreter process) Either use ctypes to directly call the getInfor function in information.c (and drop the extension module), or write a complete wrapper in the extension module and forget ctypes. Don't mix both ways. -- Gabriel Genellina From doron.tal.list at gmail.com Tue Jul 28 00:37:35 2009 From: doron.tal.list at gmail.com (Doron Tal) Date: Tue, 28 Jul 2009 07:37:35 +0300 Subject: Gracefully exiting CLI application In-Reply-To: References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> <873a8h4xf3.fsf@benfinney.id.au> Message-ID: <2a8674c60907272137u6df107b7v25c73d0e4a8a9821@mail.gmail.com> On Tue, Jul 28, 2009 at 12:52 AM, Jan Kaliszewski wrote: > As I wrote, you must use signals. Though sometimes it's a good idea > to combine these two techniques (i.e. signal handlers call sys.exit(), > then sys.exitfunc/or function registered with atexit does the actual > cleaning actions). Another way of combining signals and atexit functions is to write a signal handlers which update some QUIT_APP flag, which the application polls on occasionally. This way you'll avoid deadlocks which may happen if you have a multi-threaded application, and your main thread had just acquired a lock. --doron -------------- next part -------------- An HTML attachment was scrubbed... URL: From brenNOSPAMbarn at NObrenSPAMbarn.net Tue Jul 28 00:41:34 2009 From: brenNOSPAMbarn at NObrenSPAMbarn.net (OKB (not okblacke)) Date: Tue, 28 Jul 2009 04:41:34 GMT Subject: New implementation of re module References: Message-ID: MRAB wrote: > http://bugs.python.org/issue2636#msg90954 Variable-length lookbehind! My hero! -- --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 janssen at parc.com Tue Jul 28 01:45:23 2009 From: janssen at parc.com (Bill Janssen) Date: Mon, 27 Jul 2009 22:45:23 PDT Subject: new version of SSL module on PyPI Message-ID: <2029.1248759923@parc.com> I've uploaded ssl-1.15.tgz, the backport of the 2.6/3.x SSL module to Python 2.3-2.5. It provides an option for not using SSLv2, and also fixes a bug with write retries. Bill From hekevintran at gmail.com Tue Jul 28 02:02:30 2009 From: hekevintran at gmail.com (Kevin) Date: Mon, 27 Jul 2009 23:02:30 -0700 (PDT) Subject: Error in compiling Python on OS X References: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> <7d44lbF29p3p7U1@mid.uni-berlin.de> Message-ID: <5535080f-30ba-473c-8710-8752bf745e70@p36g2000prn.googlegroups.com> On Jul 26, 3:46?pm, "Diez B. Roggisch" wrote: > Kevin schrieb: > > > I am trying to compile Python 2.6.2 on Mac OS X 10.5.7. ?I have Xcode > > 3.1.3 installed. > > > The error I got is below. > > > $ ./configure --prefix=/Users/me/python > > Use a framework-build. > > > checking for --with-universal-archs... 32-bit > > checking MACHDEP... darwin > > checking EXTRAPLATDIR... $(PLATMACDIRS) > > checking machine type as reported by uname -m... i386 > > checking for --without-gcc... no > > checking for gcc... gcc > > checking for C compiler default output file name... > > configure: error: C compiler cannot create executables > > See `config.log' for more details. > > > Please send any ideas. > > What happens if you write a simple test.c like this: > > int main() { > ? ? return 0; > > } > > and compile it with gcc on the commandline? There must be a file called > "a.out" afterwards. > > diez I have solved my problem. I think something was wrong with my compiler. After I installed Xcode again, I was able to compile with no problems. From bcharrow at csail.mit.edu Tue Jul 28 02:18:37 2009 From: bcharrow at csail.mit.edu (Ben Charrow) Date: Tue, 28 Jul 2009 02:18:37 -0400 Subject: Download the "head" of a large file? In-Reply-To: References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> Message-ID: <4A6E983D.20707@csail.mit.edu> Dennis Lee Bieber wrote: > On Mon, 27 Jul 2009 13:38:25 -0700 (PDT), erikcw > declaimed the following in > gmane.comp.python.general: >> Something like the Python equivalent of curl http://url.com/file.xml | >> head -c 2048 >> > Presuming that | is a shell pipe operation, then doesn't that > command line use "curl" to download the entire file, and "head" to > display just the first 2k? No, the entire file is not downloaded. My understanding of why this is (which could be wrong) is that the output of curl is piped to head, and once head gets the first 2k it closes the pipe. Then, when curl tries to write to the pipe again, it gets sent the SIGPIPE signal at which point it exits. Cheers, Ben From paul.paj at gmail.com Tue Jul 28 02:21:10 2009 From: paul.paj at gmail.com (Paul Johnston) Date: Mon, 27 Jul 2009 23:21:10 -0700 (PDT) Subject: Determining __name__ from the code that called a function Message-ID: Hi, In ToscaWidgets 2 experimental, when defining resources you often do something like this: CSSLink(modname=__name__, filename='static/mycss.css') Now, what I'd like to do is make the "modname=__name__" optional, to make code more concise. I figure there must be some way (using inspect or something) to determine what __name__ would be in the code that just called my function. Couldn't immediately see how to do this - any suggestions? Thanks, Paul From gagsl-py2 at yahoo.com.ar Tue Jul 28 02:45:52 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 03:45:52 -0300 Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: En Thu, 23 Jul 2009 21:49:26 -0300, Naoki INADA escribi?: >>> The encoding that this file uses. When Unicode strings are written to >>> a file, >>> they will be converted to byte strings using this encoding. In >>> addition, >>> when the file is connected to a terminal, the attribute gives the >>> encoding >>> that the terminal is likely to use > > I feel this doc means "file object with encoding attribute encodes > unicode > regardless it is tty or not" and sys.stdout/stderr defies convention. The 'encoding' file attribute is very confusing and mostly unsupported (or there is a bug in the documentation, at least in Python 2.x). See http://bugs.python.org/issue4947 -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Tue Jul 28 02:46:00 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 03:46:00 -0300 Subject: exceptions.TypeError an integer is required References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> <0279f85d$0$5185$c3e8da3@news.astraweb.com> <8d03a739-7e45-49e1-b93d-6a396beed877@a37g2000prf.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 12:44:40 -0300, jakecjacobson escribi?: > > You are quite correct in your statements. My goal was not to make > great code but something that I could quickly test. My assumption was > that the httplib.HTTPSConnection() would do the cast to int for me. > As soon as I cast it to an int, I was able to get past that issue. A few remarks that may help learning the language: Note that Python is a strongly typed (and dynamic) language. All objects have a defined type: "443" is not the same thing as 443, and "2" + 2 raises a TypeError. If a function expects an integer, you must provide an integer (or something that at least "acts" as an integer; a string isn't "integer-alike" at all from Python's POV) Also, you don't "cast" an object into another: the expression int("443") is a constructor, and it returns a new object (an integer) based upon its argument. (so it's quite different from, say, casting "short" to "unsigned short" in C, that only changes the way the compiler treats the same bytes in memory). -- Gabriel Genellina From guitarzero at gmail.com Tue Jul 28 03:18:43 2009 From: guitarzero at gmail.com (guitarzero) Date: Tue, 28 Jul 2009 00:18:43 -0700 (PDT) Subject: WSDL and XML generation Message-ID: <3be39c48-6a89-4542-b08a-eaf00933b184@c1g2000yqi.googlegroups.com> Hi all Newbie in Python, i am looking for some pointers (or better existing modules) to do the followings: - generate (correct) XML messages from a WSDL file - make some modifications inside XML messages *before* sending them to the server hosting the Web services (described by previously mentionned WSDL file) - parse the answer. Some ideas ? Thanks in advance Stephane From oripel at gmail.com Tue Jul 28 03:23:10 2009 From: oripel at gmail.com (orip) Date: Tue, 28 Jul 2009 00:23:10 -0700 (PDT) Subject: Testoob: How do you use testoob.collector_from_globals / collector_from_modules? References: <1a8582cc-a7fc-4b1e-b1b2-d23e20ad6022@18g2000yqa.googlegroups.com> Message-ID: <0972bcba-a18c-4e33-9605-8955f723bd51@d32g2000yqh.googlegroups.com> Hi Harry, I forwarded this to the Testoob list at http://groups.google.com/group/testoob Ori. On Jul 27, 4:12?pm, Harry Ebbers wrote: > Hi, > > For a project I'm creating unittests using testoob. When all tests are > in a single file there is no problem > > if __name__ == '__main__': > ? ? testoob.main() > ?does the trick as usual. > > But for a number of python-applications they have asked me to group > unittests in different files, based on common functionality (e.g. GUI, > main-application, etcetera). > > Ican get it to work by importing all unittestclasses in the following > way and than use the normal if __name__ == '__main__' construction > > (from GUI import GUItests > from APP import APPtests > > if __name__ == '__main__': > ? ? testoob.main()) > > But looking at the future I would like to use testsuites. > > On the internet I found the functions testoob.collector_from_globals > and collector_from_modules which one can use to 'easily' create > testsuites which can be used with testoob. But no examples on how to > use them can be found. > > Can anybody supply me with a working example? > > TIA > > Harry Ebbers From nick at craig-wood.com Tue Jul 28 03:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Tue, 28 Jul 2009 02:29:56 -0500 Subject: bad certificate error References: Message-ID: jakecjacobson wrote: > I am getting the following error when doing a post to REST API, > > Enter PEM pass phrase: > Traceback (most recent call last): > File "./ices_catalog_feeder.py", line 193, in ? > main(sys.argv[1]) > File "./ices_catalog_feeder.py", line 60, in main > post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join > (input_dir, file), collection_name, key_file, cert_file) > File "./ices_catalog_feeder.py", line 125, in post2Catalog > connection.request('POST', path, parameters, head) > File "/usr/lib/python2.4/httplib.py", line 810, in request > self._send_request(method, url, body, headers) > File "/usr/lib/python2.4/httplib.py", line 833, in _send_request > self.endheaders() > File "/usr/lib/python2.4/httplib.py", line 804, in endheaders > self._send_output() > File "/usr/lib/python2.4/httplib.py", line 685, in _send_output > self.send(msg) > File "/usr/lib/python2.4/httplib.py", line 652, in send > self.connect() > File "/usr/lib/python2.4/httplib.py", line 1079, in connect > ssl = socket.ssl(sock, self.key_file, self.cert_file) > File "/usr/lib/python2.4/socket.py", line 74, in ssl > return _realssl(sock, keyfile, certfile) > socket.sslerror: (1, 'error:14094412:SSL > routines:SSL3_READ_BYTES:sslv3 alert bad certificate') > > > My code where this error occurs is: > > head = {"Content-Type" : "application/x-www-form-urlencoded", > "Accept" : "text/plain"} > parameters = urlencode({"collection" : collection, "entryxml" : open > (file,'r').read()}) > print "Sending the file to: " + host > > try: > try: > # Default port is 443. > # key_file is the name of a PEM formatted file that contains your > private key. > # cert_file is a PEM formatted certificate chain file. > connection = httplib.HTTPSConnection(host, int(port), key_file, > cert_file) > connection.request('POST', path, parameters, head) > response = connection.getresponse() > print response.status, response.reason > except httplib.error, (value,message): > print value + ':' + message > finally: > connection.close() > > I was wondering if this is due to the server having a invalid server > cert? I'd say judging from the traceback you messed up key_file or cert_file somehow. Try using the openssl binary on them (read the man page to see how!) to check them out. > If I go to this server in my browser, I get a "This server tried to > identify itself with invalid information". Is there a way to > ignore this issue with Python? Can I setup a trust store and add > this server to the trust store? Invalid how? Self signed certificate? Domain mismatch? Expired certificate? -- Nick Craig-Wood -- http://www.craig-wood.com/nick From mal at egenix.com Tue Jul 28 03:30:23 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Tue, 28 Jul 2009 09:30:23 +0200 Subject: RSA cryptography between Python and Java In-Reply-To: References: Message-ID: <4A6EA90F.1030000@egenix.com> Rob Knop wrote: > I've created an RSA key in Java. I have exported the public key by > making it into a X509EncodedKeySpec and spitting out the result of > getEncoded(). > > I want to use this public key to encode something in python that I will > send to Java, and then decode in Java with the corresponding private > key. > > Are there any python libraries that will take a public key in this > format and do RSA encoding on it? M2Crypto will let you do this: http://chandlerproject.org/bin/view/Projects/MeTooCrypto If you have access to the raw key data (instead of having it encoded in a X509 certificate), then you can also use PyCrypto, which does require setting up OpenSSL first: http://www.dlitz.net/software/pycrypto/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 28 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From gagsl-py2 at yahoo.com.ar Tue Jul 28 03:40:03 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 04:40:03 -0300 Subject: Determining __name__ from the code that called a function References: Message-ID: En Tue, 28 Jul 2009 03:21:10 -0300, Paul Johnston escribi?: > In ToscaWidgets 2 experimental, when defining resources you often do > something like this: > CSSLink(modname=__name__, filename='static/mycss.css') > > Now, what I'd like to do is make the "modname=__name__" optional, to > make code more concise. I figure there must be some way (using inspect > or something) to determine what __name__ would be in the code that > just called my function. Couldn't immediately see how to do this - any > suggestions? In CPython, you may use sys._getframe(1).f_globals['__name__'] but I don't know how portable is that across Python implementations. -- Gabriel Genellina From marco at sferacarta.com Tue Jul 28 04:18:57 2009 From: marco at sferacarta.com (Marco Mariani) Date: Tue, 28 Jul 2009 10:18:57 +0200 Subject: where do I put resources (images, audio files) when I wrote Python program? In-Reply-To: References: Message-ID: Piotrek wrote: > that? I think about puting these files in /usr/share/myprogram and then > reading it the normal way (so the path "/usr/share/myprogram" would be just > hardwired in my program). Is it the way one usually does it in Python > program or is there any more sofisticated way? Just keep them in your sources, and create an empty __init__.py file in the images directory. Then install setuptools and use the pkg_resources module. It will work even if your application is installed as an egg through easy_install, possibly zipped. To open a resource file: f = pkg_resources.resource_stream('path.to.package', 'resource.png') f.read() """ Return a readable file-like object for the specified resource; it may be an actual file, a StringIO, or some similar object. The stream is in "binary mode", in the sense that whatever bytes are in the resource will be read as-is. """ From niklasro at gmail.com Tue Jul 28 04:26:57 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 01:26:57 -0700 (PDT) Subject: The longest word Message-ID: Newbie hello onwards to the .py manual asks meantime how the longest word gets determined? First word, ok 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] 'a' rfind is another subset >>> 'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)] 'a aa aaa' One row should be able. It's a direct word 'a aa aaa...' and can be a variable, it's no big deal it occurs twice still naturally easier if also occured just once. Sincere thanks & regards Niklas From hendrik at microcorp.co.za Tue Jul 28 04:35:48 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Tue, 28 Jul 2009 10:35:48 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: Message-ID: <200907281035.48713.hendrik@microcorp.co.za> On Monday 27 July 2009 16:49:25 Aahz wrote: > In article , > > Hendrik van Rooyen wrote: > >On Sunday 26 July 2009 21:26:46 David Robinow wrote: > >> I'm a mediocre programmer. Does this mean I should switch to PHP? > > > >I have searched, but I can find nothing about this mediocre language. > > > >Could you tell us more? > > > :-P > > (For anyone who is confused by Hendrik's humor, he is saying that David > was referring to a programming language named "mediocre". English > grammar is confusing!) This is true - I intended, when I started the post, to make a crack about how he knew that he was mediocre - If there were some exam or test that you have to pass or fail to be able to make the claim to mediocrity. I was imagining a sort of devil's rating scale for programmers, that could cause one to say things like: "I am studying hard so that I can get my mediocre certificate, and one day I hope to reach hacker rank". And then the similarity to "I am a COBOL programmer" struck me, and I abandoned the ratings. - Hendrik From bearophileHUGS at lycos.com Tue Jul 28 05:02:23 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 28 Jul 2009 02:02:23 -0700 (PDT) Subject: The longest word References: Message-ID: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> On Jul 28, 10:26?am, NiklasRTZ wrote: > Newbie hello onwards to the .py manual asks meantime how the longest > word gets determined? > First word, ok > ?'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] Your language is not easy to understand, but I think you want to find the longest word in a string. If this is the input string: txt = "a aa aaa aa" There are several ways to do it, I'll show a simple one. You can split it into its parts (not having Python a built-in lazy split yet, you can split it all at once). You can do it with the string split method. It produces a list of the words, more or less (but you may have words like "gone,", you may have to take care of them too, this requires some code. Once you have a list of words, you have to take the longest. A simple way is to replace each word with a tuple that contains the length of the word and the word itself, then pick the tuple with the highest length value. But Python allows you to do such operation in a faster way: you can use the max() function, it has a "key" function that allows you to remap on the fly what you mean by "max". So you just have to give it a function (reference) that given the word spits its length, such function is "len" itself. If you instead want to find the position of the longest word the program gets a little longer. Ask if you need something different. Bye, bearophile From niklasro at gmail.com Tue Jul 28 05:22:53 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 02:22:53 -0700 (PDT) Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: On Jul 28, 5:02?am, Bearophile wrote: > On Jul 28, 10:26?am, NiklasRTZ wrote: > > > Newbie hello onwards to the .py manual asks meantime how the longest > > word gets determined? > > First word, ok > > ?'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] > > Your language is not easy to understand, but I think you want to find > the longest word in a string. > If this is the input string: > txt = "a aa aaa aa" > > There are several ways to do it, I'll show a simple one. > > You can split it into its parts (not having Python a built-in lazy > split yet, you can split it all at once). You can do it with the > string split method. It produces a list of the words, more or less > (but you may have words like "gone,", you may have to take care of > them too, this requires some code. > > Once you have a list of words, you have to take the longest. A simple > way is to replace each word with a tuple that contains the length of > the word and the word itself, then pick the tuple with the highest > length value. But Python allows you to do such operation in a faster > way: you can use the max() function, it has a "key" function that > allows you to remap on the fly what you mean by "max". So you just > have to give it a function (reference) that given the word spits its > length, such function is "len" itself. > > If you instead want to find the position of the longest word the > program gets a little longer. Ask if you need something different. > > Bye, > bearophile Thank you. This seems to work: sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- len(b))[-1] 'sdfsdfsdfsdf' From piet at cs.uu.nl Tue Jul 28 05:24:37 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 28 Jul 2009 11:24:37 +0200 Subject: The longest word References: Message-ID: >>>>> NiklasRTZ (N) wrote: >N> Newbie hello onwards to the .py manual asks meantime how the longest >N> word gets determined? >N> First word, ok >N> 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] >N> 'a' >N> rfind is another subset >>>>> 'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)] >N> 'a aa aaa' >N> One row should be able. It's a direct word 'a aa aaa...' and can be a >N> variable, it's no big deal it occurs twice still naturally easier if >N> also occured just once. >N> Sincere thanks & regards >N> Niklas 1. Is this homework? 2. It sounds very confusing. Are you trying to find the longest word in a series of words seperated by spaces? find and rfind will not help you much. You should start with var.split() if var contains the text. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jeanmichel at sequans.com Tue Jul 28 05:33:43 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 28 Jul 2009 11:33:43 +0200 Subject: Override a method but inherit the docstring In-Reply-To: References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <4A6EC5F7.9090402@sequans.com> Shai wrote: > On Jul 27, 5:05 pm, Jean-Michel Pichavant > wrote: > >> Ben Finney wrote: >> >>> The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >>> applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >>> the method, the original docstring is not associated with it. >>> >> I've also tried within the python interpreter, and it can perfectly >> solve docstring inheritance. So why would you explicitly assign >> docstring to child methods ? >> >> > > What do you mean by "can perfectly solve docstring inheritance" ? > > After the following, > > class Foo(object): > def foo(self): > "Frobber" > pass > > class Bar(Foo): > def foo(self): > pass > > help(Bar.foo) does not display "Frobber" on my interpreter. > > You're right. I must have made some dumb mistake. So interpreters do not solve docstring inheritance, Epydoc does. JM From piet at cs.uu.nl Tue Jul 28 05:36:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 28 Jul 2009 11:36:46 +0200 Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: >>>>> NiklasRTZ (N) wrote: >N> Thank you. This seems to work: >N> sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- >N> len(b))[-1] >N> 'sdfsdfsdfsdf' simpler: sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len)[-1] -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mondi at cs.unibo.it Tue Jul 28 06:05:43 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Tue, 28 Jul 2009 10:05:43 +0000 Subject: ioctl on socket In-Reply-To: References: Message-ID: Gabriel Genellina wrote: > En Mon, 27 Jul 2009 16:35:51 -0300, jacopo mondi > escribi?: > >> Is there a reason why there is no ioctl interface for socket either then >> for windows platform? It's technical issues or what else?? > > I don't completely understand your question. On Windows, you can use > socket.ioctl with sockets, and DeviceIoControl (from the pywin32 package) > with other files. On Linux, you have fcntl.ioctl that works with any kind > of file. > Ok, thanks a lot, my question was because I didn't know about fcntl.ioct, I thought that ioctl wrapping was implemented inside socketmodule.c as 'bind', 'connect', 'accept' etc. are, and I was disoriented because no documentation for socket usage on Linux nor the code have references about ioctl, except for windows. I hope the reason why you didn't completely understood my question is not my english, if it be so I'm sorry and I ask anyone to correct me, it's the only way to improve ;) thanks a lot jacopo From user at example.net Tue Jul 28 06:07:46 2009 From: user at example.net (superpollo) Date: Tue, 28 Jul 2009 12:07:46 +0200 Subject: Extract images from PDF files In-Reply-To: References: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Message-ID: <4a6ecdf3$0$40015$4fafbaef@reader3.news.tin.it> David Lyon wrote: > pdftohtml on sourceforge may help... > also see: http://linuxcommand.org/man_pages/pdfimages1.html bye From __peter__ at web.de Tue Jul 28 06:14:34 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 Jul 2009 12:14:34 +0200 Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: NiklasRTZ wrote: > On Jul 28, 5:02 am, Bearophile wrote: >> On Jul 28, 10:26 am, NiklasRTZ wrote: >> >> > Newbie hello onwards to the .py manual asks meantime how the longest >> > word gets determined? >> > First word, ok >> > 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] >> >> Your language is not easy to understand, but I think you want to find >> the longest word in a string. >> If this is the input string: >> txt = "a aa aaa aa" >> >> There are several ways to do it, I'll show a simple one. >> >> You can split it into its parts (not having Python a built-in lazy >> split yet, you can split it all at once). You can do it with the >> string split method. It produces a list of the words, more or less >> (but you may have words like "gone,", you may have to take care of >> them too, this requires some code. >> >> Once you have a list of words, you have to take the longest. A simple >> way is to replace each word with a tuple that contains the length of >> the word and the word itself, then pick the tuple with the highest >> length value. But Python allows you to do such operation in a faster >> way: you can use the max() function, it has a "key" function that >> allows you to remap on the fly what you mean by "max". So you just >> have to give it a function (reference) that given the word spits its >> length, such function is "len" itself. >> >> If you instead want to find the position of the longest word the >> program gets a little longer. Ask if you need something different. >> >> Bye, >> bearophile > > Thank you. This seems to work: > sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- > len(b))[-1] > 'sdfsdfsdfsdf' To spell out bearophile's more efficient suggestion: >>> max("a AAA aa aaaaa sdfsdfsdfsdf vv".split(), key=len) 'sdfsdfsdfsdf' From niklasro at gmail.com Tue Jul 28 06:23:58 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 03:23:58 -0700 (PDT) Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: <8a6313a3-5575-4195-a318-b823af0682fc@e11g2000yqo.googlegroups.com> There were 3 solutions. Everybody's homework is someone's "what's this?" max('asdasfd faop29v8 asejrhjhw awg5w35g aw3g5aw3g kajhsdkfhlskjdhfakljsdhf awg3 aw3'.split(), key=len) sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),key=len)[-1] sorted("a AAA aa aaaaa vv".split(' '),lambda a,b: len(a)-len(b))[-1] From jakecjacobson at gmail.com Tue Jul 28 06:35:55 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Tue, 28 Jul 2009 03:35:55 -0700 (PDT) Subject: bad certificate error References: Message-ID: On Jul 28, 3:29?am, Nick Craig-Wood wrote: > jakecjacobson wrote: > > ?I am getting the following error when doing a post to REST API, > > > ?Enter PEM pass phrase: > > ?Traceback (most recent call last): > > ? ?File "./ices_catalog_feeder.py", line 193, in ? > > ? ? ?main(sys.argv[1]) > > ? ?File "./ices_catalog_feeder.py", line 60, in main > > ? ? ?post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join > > ?(input_dir, file), collection_name, key_file, cert_file) > > ? ?File "./ices_catalog_feeder.py", line 125, in post2Catalog > > ? ? ?connection.request('POST', path, parameters, head) > > ? ?File "/usr/lib/python2.4/httplib.py", line 810, in request > > ? ? ?self._send_request(method, url, body, headers) > > ? ?File "/usr/lib/python2.4/httplib.py", line 833, in _send_request > > ? ? ?self.endheaders() > > ? ?File "/usr/lib/python2.4/httplib.py", line 804, in endheaders > > ? ? ?self._send_output() > > ? ?File "/usr/lib/python2.4/httplib.py", line 685, in _send_output > > ? ? ?self.send(msg) > > ? ?File "/usr/lib/python2.4/httplib.py", line 652, in send > > ? ? ?self.connect() > > ? ?File "/usr/lib/python2.4/httplib.py", line 1079, in connect > > ? ? ?ssl = socket.ssl(sock, self.key_file, self.cert_file) > > ? ?File "/usr/lib/python2.4/socket.py", line 74, in ssl > > ? ? ?return _realssl(sock, keyfile, certfile) > > ?socket.sslerror: (1, 'error:14094412:SSL > > ?routines:SSL3_READ_BYTES:sslv3 alert bad certificate') > > > ?My code where this error occurs is: > > > ?head = {"Content-Type" : "application/x-www-form-urlencoded", > > ?"Accept" : "text/plain"} > > ?parameters = urlencode({"collection" : collection, "entryxml" : open > > ?(file,'r').read()}) > > ?print "Sending the file to: " + host > > > ?try: > > ? ?try: > > ? ? ? ? ? ?# Default port is 443. > > ? ? ? ? ? ?# key_file is the name of a PEM formatted file that contains your > > ?private key. > > ? ? ? ? ? ?# cert_file is a PEM formatted certificate chain file. > > ? ? ? ? ? ?connection = httplib.HTTPSConnection(host, int(port), key_file, > > ?cert_file) > > ? ? ? ? ? ?connection.request('POST', path, parameters, head) > > ? ? ? ? ? ?response = connection.getresponse() > > ? ? ? ? ? ?print response.status, response.reason > > ? ?except httplib.error, (value,message): > > ? ? ? ? ? ?print value + ':' + message > > ?finally: > > ? ?connection.close() > > > ?I was wondering if this is due to the server having a invalid server > > ?cert? > > I'd say judging from the traceback you messed up key_file or cert_file > somehow. > > Try using the openssl binary on them (read the man page to see how!) > to check them out. > > > ?If I go to this server in my browser, I get a "This server tried to > > ?identify itself with invalid information". ?Is there a way to > > ?ignore this issue with Python? ?Can I setup a trust store and add > > ?this server to the trust store? > > Invalid how? ?Self signed certificate? Domain mismatch? Expired certificate? > > -- > Nick Craig-Wood --http://www.craig-wood.com/nick Nick, Thanks for the help on this. I will check my steps on openssl again and see if I messed up. What I tried to do was: 1. Save my PKI cert to disk. It was saved as a P12 file 2. Use openssl to convert it to the needed .pem file type 3. Saved the CA that my cert was signed by as a .crt file These are the 2 files that I was using for key_file and * cert_file -> CA * key_file -> my PKI cert converted to a .pem file "Invalid how? Self signed certificate? Domain mismatch? Expired certificate?" It is a server name mismatch. For everyone that wants to discuss why we shouldn't do this, great but I can't change the fact that I need to do this. I can't use http or even get a correct cert at this time. This is a quick a dirty project to demonstrate capability. I need something more than slide show briefs. From davea at ieee.org Tue Jul 28 06:43:16 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 28 Jul 2009 06:43:16 -0400 Subject: where do I put resources (images, audio files) when I wrote Python program? In-Reply-To: References: <4A6E13B8.4080806@ieee.org> Message-ID: <4A6ED644.7000805@ieee.org> Gabriel Genellina wrote: > For those read-only resources I'd use pkgutil.get_data (instead of > manually parsing __file__): > > http://docs.python.org/library/pkgutil.html#pkgutil.get_data > > It's easier to manage when someone later decide to install the package as > an egg file, or distribute it using py2exe. Quoting the documentation: > > """The function returns a binary string that is the contents of the > specified resource. > > For packages located in the filesystem, which have already been imported, > this is the rough equivalent of: > > d = os.path.dirname(sys.modules[package].__file__) > data = open(os.path.join(d, resource), 'rb').read() > return data > """ > Thanks Gabriel, I hadn't come across get_data() yet. DaveA From gagsl-py2 at yahoo.com.ar Tue Jul 28 06:56:27 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 07:56:27 -0300 Subject: ioctl on socket References: Message-ID: En Tue, 28 Jul 2009 07:05:43 -0300, jacopo mondi escribi?: > Gabriel Genellina wrote: >> En Mon, 27 Jul 2009 16:35:51 -0300, jacopo mondi >> escribi?: >> >>> Is there a reason why there is no ioctl interface for socket either >>> then >>> for windows platform? It's technical issues or what else?? >> >> I don't completely understand your question. On Windows, you can use >> socket.ioctl with sockets, and DeviceIoControl (from the pywin32 >> package) >> with other files. On Linux, you have fcntl.ioctl that works with any >> kind >> of file. >> > Ok, thanks a lot, my question was because I didn't know about > fcntl.ioct, I thought that ioctl wrapping was implemented inside > socketmodule.c as 'bind', 'connect', 'accept' etc. are, and I was > disoriented because no documentation for socket usage on Linux nor the > code have references about ioctl, except for windows. I see; socket.ioctl should menction fcntl.ioctl; I'll submit a documentation patch. -- Gabriel Genellina From hniksic at xemacs.org Tue Jul 28 07:03:54 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 28 Jul 2009 13:03:54 +0200 Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: <87prblul0l.fsf@busola.homelinux.net> Piet van Oostrum writes: >>>>>> NiklasRTZ (N) wrote: > >>N> Thank you. This seems to work: >>N> sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- >>N> len(b))[-1] >>N> 'sdfsdfsdfsdf' > > simpler: > > sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len)[-1] There is no need to sort the sequence to obtain the largest element. The max function is designed to do exactly that, and also supports the key argument: >>> max("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len) 'sdfsdfsdfsdf' From Ray.Joseph at CDICorp.com Tue Jul 28 07:54:00 2009 From: Ray.Joseph at CDICorp.com (ray) Date: Tue, 28 Jul 2009 04:54:00 -0700 (PDT) Subject: Looking for a dream language: sounds like Python to me. References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: On Jul 27, 10:39?am, David Cournapeau wrote: > On Tue, Jul 28, 2009 at 12:28 AM, Dotan Cohen wrote: > >> It is neither efficient or inefficient: it is just a distribution > >> tool, to deploy python software in a form familiar to most windows > >> users. It does not make it any faster than running the software under > >> a python prompt. > > >> As much as I like python for scientific programming, I would say > >> python is pretty far from the stated requirements in the posted blog > >> post. It is difficult to deploy software written with python (much > >> better than the alternatives, though), and it is slow if you can't > >> leverage numpy/scipy (where vectorization does not apply). > > >> It remains to be seen whether it will be true in practice, but > >> something like F#, with its integration in VS 2010, seems much closer > >> IMHO. It is compiled, high level language, and backed by the biggest > >> software vendor in the world. > > > The blog post is not looking to distribute his code, but he would like > > it to be cross platform for his own reasons. VB is not cross platform. > > I understand his "efficient binary as Ansi C" partially as a > deployment requirement, and independent of cross-platform issues. As a > scientist, being able to share my software with colleagues is a non > trivial matter. Python does not make this easy today. > > F# has nothing to do with VB: F# is a ML-language inspired from OCAML, > and run on top of the CLR. It can thus leverage the huge .net > framework (lack of non numerical API is one of the biggest matlab > hindrance, and comparatively big advantage of python + numpy/scipy), > and benefits from the much more efficient implementation compared to > python (under the currently CPython implementation at least). > > Some recent F# versions are compatible with mono, making it compatible > on most platforms that matter today for research (but of course, you > lose the IDE integration outside windows). > > David- Hide quoted text - > > - Show quoted text - I wish . . . For comparisons, Mathcad has the symbolic notation appropriate for mathematical communications. I like features of Mathematica and Maple but Mathcad provides for the user to 'stay' with mathematical symbolism longer. I prefer Matlab execution environment. So I develop concepts in Mathcad, prove them in Matlab and then compile to through C where direct performance is required. Maple and Matlab have this type of relation. Matlab, from The Mathworks, has a companion product called Simulink. This allows the user to graphically build ?algorithms? in block form. There is a similar Python function. Each of these components would best be served if allowed to exist independently but supported with transparent integration. I would like to develop in a stable user environment - a stable GUI. And then allow efficient algorithms behind the scenes. By separating the functionality of the workspace, the user can be given (or create at will) a GUI that suites her desires and provides for the creativity and productivity she chooses. The functionality under the GUI should then be pluggable. Developers can provide solutions from many directions, compete for varying performance requirements, enhance functional features technology changes, and still not disturb the fragile user interface. Allow the user the comfort of home. Let them keep whatever GUI suits them and provide for their deployment (if any) needs behind the scenes. Ray From niklasro at gmail.com Tue Jul 28 07:55:26 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 04:55:26 -0700 (PDT) Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> <87prblul0l.fsf@busola.homelinux.net> Message-ID: On Jul 28, 7:03?am, Hrvoje Niksic wrote: > Piet van Oostrum writes: > > >>>>>> NiklasRTZ (N) wrote: > > >>N> Thank you. This seems to work: > >>N> sorted("a AAA aa aaaaa ?sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- > >>N> len(b))[-1] > >>N> 'sdfsdfsdfsdf' > > > simpler: > > > sorted("a AAA aa aaaaa ?sdfsdfsdfsdf vv".split(' '), key=len)[-1] > > There is no need to sort the sequence to obtain the largest element. > The max function is designed to do exactly that, and also supports the > key argument: > > >>> max("a AAA aa aaaaa ?sdfsdfsdfsdf vv".split(' '), key=len) > > 'sdfsdfsdfsdf' Sincere thanks for strengthening python's superior flexibility. Same function also works around an exploding index problem returning results for longest word where otherwise a word with whitespace crashes the index: all().search(max(q.split(), key=len)).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1) Line below crashes the index for words with whitespace (unknown researchable, only occurs live, works with development) all().search(q).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch (PAGESIZE+1) best regards, Niklas From tommy.nordgren at comhem.se Tue Jul 28 07:58:32 2009 From: tommy.nordgren at comhem.se (Tommy Nordgren) Date: Tue, 28 Jul 2009 13:58:32 +0200 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> References: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> Message-ID: On Jul 26, 2009, at 10:37 AM, Chris Rebert wrote: > On Sun, Jul 26, 2009 at 1:12 AM, Jessica R > Smith wrote: >> Hello, >> I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 >> >> I downloaded python 2.6.2 from here: >> - http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 >> >> I unpacked it. >> >> I ran these shell commands: >> - ./configure --prefix=/pt/p >> - make >> >> Near the end of the make output I see this message: >> >> . Failed to find the necessary bits to build these modules: >> . _bsddb gdbm linuxaudiodev >> . ossaudiodev readline spwd >> . sunaudiodev >> . To find the necessary bits, look in setup.py in detect_modules() >> for >> the module's name. >> >> I looked in setup.py in detect_modules() >> >> It is not clear to me how to proceed. >> >> I think that I want the module: "readline" >> >> I doubt I need the other modules like linuxaudiodev, etc. >> >> If you have any clues or opinions on how I can build the module >> "readline", >> please feel free to share. > > You need to install the GNU Readline library > (http://tiswww.case.edu/php/chet/readline/rltop.html), which the > module depends on. > A better solution would be to configure the modules in question to use the libreadline.dylib library already installed on every Mac OS X computer. > You might consider installing Python through MacPorts or Fink instead, > as they automate the compilation and installation process and take > care of dependencies (such as GNU Readline) for you: > http://www.macports.org/ > http://www.finkproject.org/ > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list ----------------------------------- See the amazing new SF reel: Invasion of the man eating cucumbers from outer space. On congratulations for a fantastic parody, the producer replies : "What parody?" Tommy Nordgren tommy.nordgren at comhem.se From steve at REMOVE-THIS-cybersource.com.au Tue Jul 28 08:02:40 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 28 Jul 2009 12:02:40 GMT Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> Message-ID: <027ed9be$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 23:16:39 -0300, Gabriel Genellina wrote: > I don't see the point on "fixing" either the Python script or httplib to > accomodate for an invalid server certificate... If it's just for > internal testing, I'd use HTTP instead (at least until the certificate > is fixed). In real life, sometimes you need to drive with bad brakes on your car, walk down dark alleys in the bad part of town, climb a tree without a safety line, and use a hammer without wearing goggles. We can do all these things. The OP has said that, for whatever reason, he needs to ignore a bad server certificate when connecting to HTTPS. Python is a language where developers are allowed to shoot themselves in the foot, so long as they do so in full knowledge of what they're doing. So, putting aside all the millions of reasons why the OP shouldn't accept an invalid certificate, how can he accept an invalid certificate? -- Steven From nick at craig-wood.com Tue Jul 28 08:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Tue, 28 Jul 2009 07:29:56 -0500 Subject: bad certificate error References: Message-ID: jakecjacobson wrote: > > > ?If I go to this server in my browser, I get a "This server tried to > > > ?identify itself with invalid information". ?Is there a way to > > > ?ignore this issue with Python? ?Can I setup a trust store and add > > > ?this server to the trust store? > > > > Invalid how? ?Self signed certificate? Domain mismatch? Expired > > certificate? > > For everyone that wants to discuss why we shouldn't do this, great but > I can't change the fact that I need to do this. I can't use http or > even get a correct cert at this time. This is a quick a dirty project > to demonstrate capability. I need something more than slide show > briefs. I wasn't making a value judgement - I was trying to help! If you can get a bit more information out of the browser as to why the certificate is invalid it may help your python code. Real certificates cost real money. Usually a correctly set up self-signed certificate is fine for dev stuff. I'm certainly too cheap to by real certificates for dev or internal stuff! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From magobin at gmail.com Tue Jul 28 08:50:35 2009 From: magobin at gmail.com (Alex) Date: Tue, 28 Jul 2009 05:50:35 -0700 (PDT) Subject: index nested lists Message-ID: hi at all, If I have this list: >>> lista ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] if I want enumerate elements...I can see: >>> for parola in lista: print lista[i] i = i + 1 ciao 1 ['mela', 'pera', 'banana'] [1, 2, 3] >>> but, if I want to enumerate elements about nested lists ??, something like: ciao 1 mela pera banana 1 2 3 ...How can I do ?? Alex From dudekksoft at gmail.com Tue Jul 28 08:54:26 2009 From: dudekksoft at gmail.com (redhot) Date: Tue, 28 Jul 2009 05:54:26 -0700 (PDT) Subject: File and TagPy Message-ID: <11a3ce55-6ba5-4438-9b05-490686e258e6@k1g2000yqf.googlegroups.com> Hello, I have question, is it possible to read tag in tagpy module from file stream? For example I have earlier read file: file = open('some.mp3', 'rb') and next get tags from it? Regards, Konrad From bakes at ymail.com Tue Jul 28 08:59:02 2009 From: bakes at ymail.com (Bakes) Date: Tue, 28 Jul 2009 05:59:02 -0700 (PDT) Subject: FTP Offset larger than file. Message-ID: I am writing a python script that performs an identical function to the 'tail' unix utility, except that it connects to its files over FTP, rather than the local hard disk. I am currently using this python script to generate an increasing 'logfile' of garbage. > import time > for i in range(1, 20000): > time.sleep(0.2) > print i > f = open("data1.log","a") > f.write('%s: This logfile is being automatically generated to help Bakes test his python ftptail. \n' % i) > f.close() and use this script to actually download it. >import time >import os.path >from ftplib import FTP > >#Empty the file >filename = 'data1.log' >file = open(filename, 'w') >file.write('') >file.close() > >def handleDownload(block): > file.write(block) > print ".", > ># Create an instance of the FTP object ># Optionally, you could specify username and password: >ftp=FTP(host, user, pass) > >directory = '/temp' >ftp.cwd(directory) > >file = open(filename, 'a') > >for i in range(1,20000): > size=os.path.getsize('data1.log') > ftp.retrbinary('RETR ' + filename, handleDownload, rest=size) > >file.close() > >print ftp.close() Now, my problem is that I get a very strange error. What should be happening is the script gets the size of the local file before downloading all of the external file after that offset. The error I get is: ftplib.error_temp: 451-Restart offset 24576 is too large for file size 22852. 451 Restart offset reset to 0 which tells me that the local file is larger than the external file, by about a kilobyte. Certainly, the local file is indeed that size, so my local script is doing the right things. I do wonder what is going wrong, can anyone enlighten me? From npuloski at gmail.com Tue Jul 28 09:09:18 2009 From: npuloski at gmail.com (Nanime Puloski) Date: Tue, 28 Jul 2009 09:09:18 -0400 Subject: Internal Math Library of Numpy Message-ID: Does Numpy use Python's standard math library when calculating elementary functions such as exp(x) and acos(x)? Also, what is the internal library of Numpy and Python's standard math library? Is it platform independent? -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreas.tawn at ubisoft.com Tue Jul 28 09:12:41 2009 From: andreas.tawn at ubisoft.com (Andreas Tawn) Date: Tue, 28 Jul 2009 15:12:41 +0200 Subject: index nested lists In-Reply-To: References: Message-ID: <8AEDA5E3386EA742B8C24C95FF0C758007B66A65@PDC-MAIL3.ubisoft.org> > hi at all, > If I have this list: > > >>> lista > ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] > > if I want enumerate elements...I can see: > > >>> for parola in lista: > print lista[i] > i = i + 1 > > ciao > 1 > ['mela', 'pera', 'banana'] > [1, 2, 3] > >>> > > but, if I want to enumerate elements about nested lists ??, something > like: > > ciao > 1 > mela > pera > banana > 1 > 2 > 3 > > ...How can I do ?? > > Alex > -- > http://mail.python.org/mailman/listinfo/python-list You could do something like this. def printNestedList(lst): if isinstance(lst, list): for element in lst: printNestedList(element) else: print lst myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] printNestedList(myList) >>> ciao 1 mela pera banana 1 2 3 Cheers, Drea From magobin at gmail.com Tue Jul 28 09:19:03 2009 From: magobin at gmail.com (Alex) Date: Tue, 28 Jul 2009 06:19:03 -0700 (PDT) Subject: index nested lists References: Message-ID: On 28 Lug, 15:12, "Andreas Tawn" wrote: > > hi at all, > > ?If I have this list: > > > >>> lista > > ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] > > > if I want enumerate elements...I can see: > > > >>> for parola in lista: > > ? ?print lista[i] > > ? ?i = i + 1 > > > ciao > > 1 > > ['mela', 'pera', 'banana'] > > [1, 2, 3] > > > but, if I want to enumerate elements about nested lists ??, something > > like: > > > ciao > > 1 > > mela > > pera > > banana > > 1 > > 2 > > 3 > > > ...How can I do ?? > > > Alex > > -- > >http://mail.python.org/mailman/listinfo/python-list > > You could do something like this. > > def printNestedList(lst): > ? ? if isinstance(lst, list): > ? ? ? ? for element in lst: > ? ? ? ? ? ? printNestedList(element) > ? ? else: > ? ? ? ? print lst > > myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] > printNestedList(myList) > thanks a lot ! Alex From michele.petrazzo at REMOVE_me_unipex.it Tue Jul 28 09:30:49 2009 From: michele.petrazzo at REMOVE_me_unipex.it (Michele Petrazzo) Date: Tue, 28 Jul 2009 15:30:49 +0200 Subject: python 3 and stringio.seek Message-ID: Hi list, I'm trying to port a my library to python 3, but I have a problem with a the stringio.seek: the method not accept anymore a value like pos=-6 mode=1, but the "old" (2.X) version yes... The error: File "/home/devel/Py3/lib/python3.0/io.py", line 2031, in seek return self._seek(pos, whence) IOError: Can't do nonzero cur-relative seeks How solve this? Thanks, MIchele From nobody at nowhere.com Tue Jul 28 09:31:56 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 28 Jul 2009 14:31:56 +0100 Subject: Gracefully exiting CLI application References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Message-ID: On Mon, 27 Jul 2009 22:35:01 +0200, David wrote: > I am writing a command line application, and I need to perform some > cleaning on exit even if the process is killed. How can I do that with > python? Killed by what means? Ctrl-C sends SIGINT which is converted to a KeyboardInterrupt exception. This can be caught, or if it's allowed to terminate the process, any exit handlers registered via atexit.register() will be used. For other signals, you can install a handler with signal.signal(). This can call sys.exit() or raise an exception (e.g. KeyboardInterrupt). OTOH, if the process is terminated by SIGKILL, there's nothing you can do about it. And although it's possible to trap SIGSEGV, you shouldn't assume that the Python interpreter is still functional at this point. From jeanmichel at sequans.com Tue Jul 28 09:39:56 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 28 Jul 2009 15:39:56 +0200 Subject: index nested lists In-Reply-To: References: Message-ID: <4A6EFFAC.30106@sequans.com> Alex wrote: > On 28 Lug, 15:12, "Andreas Tawn" wrote: > >>> hi at all, >>> If I have this list: >>> >>>>>> lista >>>>>> >>> ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] >>> >>> if I want enumerate elements...I can see: >>> >>>>>> for parola in lista: >>>>>> >>> print lista[i] >>> i = i + 1 >>> >>> ciao >>> 1 >>> ['mela', 'pera', 'banana'] >>> [1, 2, 3] >>> >>> but, if I want to enumerate elements about nested lists ??, something >>> like: >>> >>> ciao >>> 1 >>> mela >>> pera >>> banana >>> 1 >>> 2 >>> 3 >>> >>> ...How can I do ?? >>> >>> Alex >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >> You could do something like this. >> >> def printNestedList(lst): >> if isinstance(lst, list): >> for element in lst: >> printNestedList(element) >> else: >> print lst >> >> myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] >> printNestedList(myList) >> >> > > > thanks a lot ! > > Alex > One hidden suggestion in Andreas answer is to write your code in english, if you can :o) JM From exarkun at divmod.com Tue Jul 28 09:48:02 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Tue, 28 Jul 2009 09:48:02 -0400 Subject: bad certificate error In-Reply-To: Message-ID: <20090728134802.11822.1316828413.divmod.quotient.0@henry.divmod.com> On Tue, 28 Jul 2009 03:35:55 -0700 (PDT), jakecjacobson wrote: > [snip] > >"Invalid how? Self signed certificate? Domain mismatch? Expired >certificate?" It is a server name mismatch. Python 2.4 is not capable of allowing you to customize this verification behavior. It is hard coded to let OpenSSL make the decision about whether to accept the certificate or not. Either M2Crypto or pyOpenSSL will let you ignore verification errors. The new ssl module in Python 2.6 may also as well. Jean-Paul From jakecjacobson at gmail.com Tue Jul 28 09:52:37 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Tue, 28 Jul 2009 06:52:37 -0700 (PDT) Subject: bad certificate error References: Message-ID: <332a5bba-48d5-4c8b-9c1e-288790d85dbb@o32g2000yqm.googlegroups.com> On Jul 28, 9:48?am, Jean-Paul Calderone wrote: > On Tue, 28 Jul 2009 03:35:55 -0700 (PDT), jakecjacobson wrote: > > [snip] > > >"Invalid how? ?Self signed certificate? Domain mismatch? Expired > >certificate?" ?It is a server name mismatch. > > Python 2.4 is not capable of allowing you to customize this verification > behavior. ?It is hard coded to let OpenSSL make the decision about whether > to accept the certificate or not. > > Either M2Crypto or pyOpenSSL will let you ignore verification errors. ?The > new ssl module in Python 2.6 may also as well. > > Jean-Paul Thanks, I will look into these suggestions. From li.weidong at gmail.com Tue Jul 28 09:54:21 2009 From: li.weidong at gmail.com (Weidong) Date: Tue, 28 Jul 2009 06:54:21 -0700 (PDT) Subject: Questions about unicodedata in python 2.6.2 Message-ID: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> I am trying to build python 2.6.2 from the source by following the instructions in README that comes with the source. The configure and make steps are fine, but there is an error in "make install" step. This "make install" attempts to build a lot of lib, and it complains about the lack of "unicodedata" shared object, I looked at the source tree and the build result, unicodedata.so does not exist. I further notice that in Modules/Setup.dist, the line for unicodedata.c is commented out by default. So I uncomment it, and experiment with rebuilding everything. This time, with or without re-configure, the "make" step failed with a link-time error when linking for libpython2.6.a due to "undefined reference to 'initunicodedata'. This symbol is defined in unicodedata.c, but unicodedata.o is not used in linking for libpython2.6.a, hence the error. So this is a problem in the generated Makefile. Does anyone know what special things I have to do to avoid such error? Is there any known problems in the configure/make files in the python 2.6.2 source code that require special patch? Is unicodedata.c really needed in python 2.6.2? If it is needed, why it is commented out by default in Modules/Setup.dist? If it is not needed, why "make install" attempts to use it in compiling the libraries? Why those libraries are not designed to be compiled in the make step, but in the install step? Thanks for your help! - Weidong From li.weidong at gmail.com Tue Jul 28 09:56:15 2009 From: li.weidong at gmail.com (Weidong) Date: Tue, 28 Jul 2009 06:56:15 -0700 (PDT) Subject: Questions about unicodedata in python 2.6.2 References: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> Message-ID: <01581b5a-3ddc-4ed8-abf4-ad41a542812c@l35g2000vba.googlegroups.com> On Jul 28, 9:54?am, Weidong wrote: > I am trying to build python 2.6.2 from the source by following the > instructions in README that comes with the source. ?The configure and > make steps are fine, but there is an error in "make install" step. > This "make install" attempts to build a lot of lib, and it complains > about the lack of "unicodedata" shared object, > > I looked at the source tree and the build result, unicodedata.so does > not exist. ?I further notice that in Modules/Setup.dist, the line for > unicodedata.c is commented out by default. ?So I uncomment it, and > experiment with rebuilding everything. > > This time, with or without re-configure, the "make" step failed with a > link-time error when linking for libpython2.6.a due to "undefined > reference to 'initunicodedata'. ?This symbol is defined in > unicodedata.c, but unicodedata.o is not used in linking for > libpython2.6.a, hence the error. ?So this is a problem in the > generated Makefile. > > Does anyone know what special things I have to do to avoid such > error? ?Is there any known problems in the configure/make files in the > python 2.6.2 source code that require special patch? > > Is unicodedata.c really needed in python 2.6.2? ?If it is needed, why > it is commented out by default in Modules/Setup.dist? ?If it is not > needed, why "make install" attempts to use it in compiling the > libraries? ?Why those libraries are not designed to be compiled in the > make step, but in the install step? > > Thanks for your help! > > - Weidong To add some info: This experiment was done on Linux, Ubuntu 8.x. From hniksic at xemacs.org Tue Jul 28 10:01:52 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 28 Jul 2009 16:01:52 +0200 Subject: FTP Offset larger than file. References: Message-ID: <87ljm8vrcf.fsf@busola.homelinux.net> Bakes writes: > The error I get is: > ftplib.error_temp: 451-Restart offset 24576 is too large for file size > 22852. > 451 Restart offset reset to 0 > which tells me that the local file is larger than the external file, > by about a kilobyte. Certainly, the local file is indeed that size, so > my local script is doing the right things. I do wonder what is going > wrong, can anyone enlighten me? I'd say you failed to take buffering into account. You write into a buffered file, yet you use os.path.getsize() to find out the current file size. If the data is not yet flushed, you keep re-reading the same stuff from the remote file, and writing it out. Once the buffer is flushed, your file will contain more data than was retrieved from the remote side, and eventually this will result in the error you see. As a quick fix, you can add a file.flush() line after the file.write(...) line, and the problem should go away. From python-url at phaseit.net Tue Jul 28 10:07:28 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Tue, 28 Jul 2009 14:07:28 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 28) Message-ID: QOTW: "But there's another principle at work here that's less well known, and that was first articulated to me by Robert Dewar: You can remove linear factors by profiling, but it's much harder to undo bad algorithmic decisions. In particular, whether a program runs in O(n) or O(n^2) sometimes depends on decisions that have to be frozen fairly early in the design." - Andrew Koenig Comparing the performance of the same algorithm using many compilers: CPython, psyco, Cython, ShedSkin, Unladen Swallow, Java, C and D: http://groups.google.com/group/comp.lang.python/browse_thread/thread/8d793c46903cc0b6/ MRAB has written a new implementation of re module with many new features, and he's looking for feedback: http://groups.google.com/group/comp.lang.python/browse_thread/thread/16c139b0a52ab023/ Importing two modules with the same name from different directories: http://groups.google.com/group/comp.lang.python/browse_thread/thread/3cb83a30e1b2c202/ Cleanly exiting an application, even if it gets killed by a signal: http://groups.google.com/group/comp.lang.python/browse_thread/thread/d60b8e0d93aeaaf9/ How can a child thread notify a parent thread of its status? http://groups.google.com/group/comp.lang.python/browse_thread/thread/d1d7f55716aacedc/ How to attach a docstring to global constants/variables? http://groups.google.com/group/comp.lang.python/browse_thread/thread/ac54186ad873036a/ In Python -unlike other languages- it does not make sense to treat numbers (scalars) as vectors of length 1: http://groups.google.com/group/comp.lang.python/browse_thread/thread/3ef498c906bd7e2d/ isinstance may take a tuple of types as its second argument: why a tuple, and not a list, or a set? http://mail.python.org/pipermail/python-list/2009-July/721205.html How to overcome the normal 2GB allocation limit of Windows XP, 32bits: http://groups.google.com/group/comp.lang.python/browse_thread/thread/59851df6e54f9ef0/ Distinguishing active generators from exhausted ones: http://groups.google.com/group/comp.lang.python/browse_thread/thread/9ff179e8cb5e9bc/ Peter Otten must be, undoubtedly, Sherlock Holmes reincarnated: http://groups.google.com/group/comp.lang.python/browse_thread/thread/838177c8a37d2b7c/ And Piet van Oostrum is not much behind him: http://groups.google.com/group/comp.lang.python/browse_thread/thread/d1f8627413cd3c4e/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From lists at cheimes.de Tue Jul 28 10:17:20 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 28 Jul 2009 16:17:20 +0200 Subject: Questions about unicodedata in python 2.6.2 In-Reply-To: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> References: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> Message-ID: Weidong schrieb: > I am trying to build python 2.6.2 from the source by following the > instructions in README that comes with the source. The configure and > make steps are fine, but there is an error in "make install" step. > This "make install" attempts to build a lot of lib, and it complains > about the lack of "unicodedata" shared object, unicodedata is usually build as a shared library and not linked into the Python core. How did you configure Python? The usual prodecure is: ./configure make sudo make install On Unix the preferred option for ./configure is "--enable-unicode=ucs4". Christian From gdamjan at gmail.com Tue Jul 28 10:17:45 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Tue, 28 Jul 2009 16:17:45 +0200 Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: <9td3k6-eid.ln1@archaeopteryx.softver.org.mk> > So do all these OSes have some kind of __mega_unifying_poll system > call that works for anything that might possibly block, that you can > exploit from a user process? On Linux at least, the select/poll/epoll is that system, the trick is to use eventfd, timerfd and signalfd which are Linux specific system calls. I think that covers everything needed. (eventfd is used for semaphores) -- ?????? ( http://softver.org.mk/damjan/ ) Give me the knowledge to change the code I do not accept, the wisdom not to accept the code I cannot change, and the freedom to choose my preference. From bakes at ymail.com Tue Jul 28 10:18:48 2009 From: bakes at ymail.com (Bakes) Date: Tue, 28 Jul 2009 07:18:48 -0700 (PDT) Subject: FTP Offset larger than file. References: <87ljm8vrcf.fsf@busola.homelinux.net> Message-ID: On 28 July, 15:01, Hrvoje Niksic wrote: > Bakes writes: > > The error I get is: > > ftplib.error_temp: 451-Restart offset 24576 is too large for file size > > 22852. > > 451 Restart offset reset to 0 > > which tells me that the local file is larger than the external file, > > by about a kilobyte. Certainly, the local file is indeed that size, so > > my local script is doing the right things. I do wonder what is going > > wrong, can anyone enlighten me? > > I'd say you failed to take buffering into account. ?You write into a > buffered file, yet you use os.path.getsize() to find out the current > file size. ?If the data is not yet flushed, you keep re-reading the same > stuff from the remote file, and writing it out. ?Once the buffer is > flushed, your file will contain more data than was retrieved from the > remote side, and eventually this will result in the error you see. > > As a quick fix, you can add a file.flush() line after the > file.write(...) line, and the problem should go away. Thank you very much, that worked perfectly. From bakes at ymail.com Tue Jul 28 10:33:05 2009 From: bakes at ymail.com (Bakes) Date: Tue, 28 Jul 2009 07:33:05 -0700 (PDT) Subject: FTP Offset larger than file. References: <87ljm8vrcf.fsf@busola.homelinux.net> Message-ID: On 28 July, 15:18, Bakes wrote: > On 28 July, 15:01, Hrvoje Niksic wrote: > > > > > Bakes writes: > > > The error I get is: > > > ftplib.error_temp: 451-Restart offset 24576 is too large for file size > > > 22852. > > > 451 Restart offset reset to 0 > > > which tells me that the local file is larger than the external file, > > > by about a kilobyte. Certainly, the local file is indeed that size, so > > > my local script is doing the right things. I do wonder what is going > > > wrong, can anyone enlighten me? > > > I'd say you failed to take buffering into account. ?You write into a > > buffered file, yet you use os.path.getsize() to find out the current > > file size. ?If the data is not yet flushed, you keep re-reading the same > > stuff from the remote file, and writing it out. ?Once the buffer is > > flushed, your file will contain more data than was retrieved from the > > remote side, and eventually this will result in the error you see. > > > As a quick fix, you can add a file.flush() line after the > > file.write(...) line, and the problem should go away. > > Thank you very much, that worked perfectly. Actually, no it didn't. That fix works seamlessly in Linux, but gave the same error in a Windows environment. Is that expected? From stef.mientki at gmail.com Tue Jul 28 10:35:39 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Tue, 28 Jul 2009 16:35:39 +0200 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: <4A6F0CBB.8050609@gmail.com> > Matlab, from The Mathworks, has a companion product called Simulink. > This allows the user to graphically build ?algorithms? in block form. > There is a similar Python function. > Where can I find a Python functionality like simulink ? thanks, Stef Mientki From cournape at gmail.com Tue Jul 28 10:37:22 2009 From: cournape at gmail.com (David Cournapeau) Date: Tue, 28 Jul 2009 23:37:22 +0900 Subject: Internal Math Library of Numpy In-Reply-To: References: Message-ID: <5b8d13220907280737k43d2fad1k479f597e062176d2@mail.gmail.com> On Tue, Jul 28, 2009 at 10:09 PM, Nanime Puloski wrote: > Does Numpy use Python's standard math library when calculating > elementary functions such as exp(x) and acos(x)? It depends on the dtype: for fundamental types (float, int, etc...), the underlying implementation is whatever the (C) math library provides. If the functionality is not there, we have our own custom implementation (but exp and acos happen to be mandatory - if they are not detected, the build fail). > Also, what is the > internal library of Numpy and Python's standard math library? Is it > platform independent? I don't know for python, but for numpy, we have our own math library on top of whatever the platform provides (which is not much on windows, for example). There is an ongoing effort in numpy to provides a portable, pure C (independent of the python runtime) math library. The goal is to have a portable C99 math library, and more later hopefully. cheers, David From woakas at gfc.edu.co Tue Jul 28 10:50:27 2009 From: woakas at gfc.edu.co (Gustavo =?iso-8859-1?Q?Andr=E9s?= Angulo) Date: Tue, 28 Jul 2009 09:50:27 -0500 Subject: WSDL and XML generation In-Reply-To: <3be39c48-6a89-4542-b08a-eaf00933b184@c1g2000yqi.googlegroups.com> References: <3be39c48-6a89-4542-b08a-eaf00933b184@c1g2000yqi.googlegroups.com> Message-ID: <20090728145027.GA11013@gfc.edu.co> Hi you can generate the WSDl with soaplib [1], and you can view web.py or django for this: for web.py -> http://webpy.org/cookbook/webservice for django -> http://www.djangosnippets.org/snippets/979/ [1]=> http://trac.optio.webfactional.com/ > Hi all > Newbie in Python, i am looking for some pointers (or better existing > modules) to do the followings: > > - generate (correct) XML messages from a WSDL file > - make some modifications inside XML messages *before* sending them to > the server hosting the Web services (described by previously > mentionned WSDL file) > - parse the answer. > > Some ideas ? > Thanks in advance > Stephane > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From python at mrabarnett.plus.com Tue Jul 28 10:59:06 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 28 Jul 2009 15:59:06 +0100 Subject: New implementation of re module In-Reply-To: References: Message-ID: <4A6F123A.7030002@mrabarnett.plus.com> Aahz wrote: > In article , > MRAB wrote: >> I've been working on a new implementation of the re module. The details >> are at http://bugs.python.org/issue2636, specifically from >> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >> Python 2.6 on Windows if you want to try it out. > > How does it handle the re module's unit tests? Basically, it passes all those tests I expect it to pass. :-) It fails those where the intended behaviour has changed, such as re.sub treating unmatched groups as empty strings, as requested in http://bugs.python.org/issue1519638. From python at mrabarnett.plus.com Tue Jul 28 11:11:02 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 28 Jul 2009 16:11:02 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <200907281035.48713.hendrik@microcorp.co.za> References: <200907281035.48713.hendrik@microcorp.co.za> Message-ID: <4A6F1506.9030207@mrabarnett.plus.com> Hendrik van Rooyen wrote: > On Monday 27 July 2009 16:49:25 Aahz wrote: >> In article , >> >> Hendrik van Rooyen wrote: >>> On Sunday 26 July 2009 21:26:46 David Robinow wrote: >>>> I'm a mediocre programmer. Does this mean I should switch to PHP? >>> I have searched, but I can find nothing about this mediocre language. >>> >>> Could you tell us more? >>> >> :-P >> >> (For anyone who is confused by Hendrik's humor, he is saying that David >> was referring to a programming language named "mediocre". English >> grammar is confusing!) > > This is true - I intended, when I started the post, to make a crack about > how he knew that he was mediocre - If there were some exam or test > that you have to pass or fail to be able to make the claim to mediocrity. > I was imagining a sort of devil's rating scale for programmers, that > could cause one to say things like: "I am studying hard so that I can > get my mediocre certificate, and one day I hope to reach hacker rank". > > And then the similarity to "I am a COBOL programmer" struck me, > and I abandoned the ratings. > If you were a "COBOL" programmer, would you want to shout about it? :-) From phillip.oldham at gmail.com Tue Jul 28 11:15:49 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Tue, 28 Jul 2009 08:15:49 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> <7xws60u06g.fsf@ruckus.brouhaha.com> <83889f6f-1e40-435e-b947-af17964e90af@d32g2000yqh.googlegroups.com> Message-ID: <62d0b6e4-5356-4b53-b08b-9c99933d2c0f@s31g2000yqs.googlegroups.com> In answer to my own question, I came across Octo.py[1] today. Not tested/played-with/used it yet, however, though it seems simple enough. Still welcoming any suggestions for other/similar tools. [1] http://code.google.com/p/octopy/ From dearmanzur at gmail.com Tue Jul 28 11:17:25 2009 From: dearmanzur at gmail.com (Manzur Ahmed) Date: Tue, 28 Jul 2009 11:17:25 -0400 Subject: need help using Tkinter Message-ID: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> i wanted to ask you if one of you could help me to use Tkinter. i'm trying to just create a simple GUI for which I have provided the code for below. # Simple GUI # Demonstrates creating a window from Tkinter import * # create the root window root = Tk () # modify the window root.title("Simple GUI") root.geometry("200x100") # kick off the window's event loop root.mainloop() i saved the file as both simple_gui.py and simple_gui.pyw. when i try to run simple_gui.pyw i don't get anything and when i try to run simple_gui.py i get the following: Traceback (most recent call last): File "C:\Python-Study\Chapter10\simple_gui.py", line 4, in ImportError: No module named Tkinter i tried to google search this and there were some threads that were saying to install tcl but that does not work. I did check the Lib directory under the Python directory and I did not see a Tkinter module anywhere which I think is the problem. Do you know how I would go about with getting this module and if that is not the problem or if you think it might be caused by something else, can you please give me some guidance? i even reinstalled python 3.1 which i got from www.python.org. i still do not see the Tkinter module in the lib directory. thank you for all your help. manzur -------------- next part -------------- An HTML attachment was scrubbed... URL: From hniksic at xemacs.org Tue Jul 28 11:22:38 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 28 Jul 2009 17:22:38 +0200 Subject: FTP Offset larger than file. References: <87ljm8vrcf.fsf@busola.homelinux.net> Message-ID: <873a8gg7cx.fsf@busola.homelinux.net> Bakes writes: >> > As a quick fix, you can add a file.flush() line after the >> > file.write(...) line, and the problem should go away. >> >> Thank you very much, that worked perfectly. > > Actually, no it didn't. That fix works seamlessly in Linux, but gave > the same error in a Windows environment. Is that expected? Consider opening the file in binary mode, by passing the 'wb' and 'ab' modes to open instead of 'w' and 'a' respectively. On Windows, python (and other languages) will convert '\n' to '\r\n' on write. From python at mrabarnett.plus.com Tue Jul 28 11:32:45 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 28 Jul 2009 16:32:45 +0100 Subject: need help using Tkinter In-Reply-To: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> References: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> Message-ID: <4A6F1A1D.7090604@mrabarnett.plus.com> Manzur Ahmed wrote: > i wanted to ask you if one of you could help me to use Tkinter. i'm > trying to just create a simple GUI for which I have provided the code > for below. > > # Simple GUI > # Demonstrates creating a window > > from Tkinter import * > > # create the root window > root = Tk () > > # modify the window > root.title("Simple GUI") > root.geometry("200x100") > > # kick off the window's event loop > root.mainloop() > > i saved the file as both simple_gui.py and simple_gui.pyw. when i try to > run simple_gui.pyw i don't get anything and when i try to run > simple_gui.py i get the following: > Traceback (most recent call last): > File "C:\Python-Study\Chapter10\simple_gui.py", line 4, in > ImportError: No module named Tkinter > > i tried to google search this and there were some threads that were > saying to install tcl but that does not work. I did check the Lib > directory under the Python directory and I did not see a Tkinter module > anywhere which I think is the problem. Do you know how I would go about > with getting this module and if that is not the problem or if you think > it might be caused by something else, can you please give me some > guidance? i even reinstalled python 3.1 which i got from www.python.org > . i still do not see the Tkinter module in the > lib directory. thank you for all your help. > There are some differences between Python 3.x and Python 2.x. In Python 3.1 the module is called "tkinter", not "Tkinter" (names are case-sensitive). From nagle at animats.com Tue Jul 28 12:02:29 2009 From: nagle at animats.com (John Nagle) Date: Tue, 28 Jul 2009 09:02:29 -0700 Subject: M2Crypto hangs on this URL In-Reply-To: References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> <4a6deda5$0$1639$742ec2ed@news.sonic.net> <4a6e0c53$0$1644$742ec2ed@news.sonic.net> Message-ID: <4a6f2067$0$1649$742ec2ed@news.sonic.net> Martin P. Hellwig wrote: > John Nagle wrote: >> John Nagle wrote: >>> John Nagle wrote: >>>> There's something strange about this URL: >>>> >>>> "https://sagar310.pontins.com/sraep/" ... > It looks to me like the SSL handshake is not done properly from the > server side. > > Compare the output of: > openssl s_client -host sagar310.pontins.com -port 443 -debug -showcerts > -msg > > With (for example): > openssl s_client -host www.google.com -port 443 -debug -showcerts -msg OpenSSL is clearly not happy with that site. But it doesn't hang. openssl s_client -host sagar310.pontins.com -port 443 -debug -showcerts -msg eventually prints "Verify return code: 19 (self signed certificate in certificate chain)" That's weird, because there's a Verisign certificate in the chain. That site is somehow mishandling its certs. My problem, though, is that M2Crypto 0.17 is hanging for hours to days on those connections. John Nagle From li.weidong at gmail.com Tue Jul 28 12:16:57 2009 From: li.weidong at gmail.com (Weidong) Date: Tue, 28 Jul 2009 09:16:57 -0700 (PDT) Subject: Questions about unicodedata in python 2.6.2 References: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> Message-ID: <1d728013-fc77-469f-97bb-5769092377a7@p36g2000vbn.googlegroups.com> On Jul 28, 10:17?am, Christian Heimes wrote: > unicodedatais usually build as a shared library and not linked into the > Python core. How did you configure Python? The usual prodecure is: > > ? ?./configure > ? ?make > ? ?sudo make install > > On Unix the preferred option for ./configure is "--enable-unicode=ucs4". > > Christian Thanks for your response! I configured it in the same way as you said, but without "--enable-unicode=ucs4". The ocnfig.log shows that checking for UCS-4 was failed. So I assume that by default UCS-2 was used. There was no other problme in the "make" step. The problem was in the "sudo make install" step, where there were errors in building libraries / test libraries that need unicodedata.so which did not exist. I also tried to use "make -i" to let it complete the building to ignore that error. In the end, I still did not see unicodedata.so in the build result. It seems that the Makefile did not even try to build unicodedata.so. Maybe something went wrong in my configuration? - Weidong From rylesny at gmail.com Tue Jul 28 12:21:39 2009 From: rylesny at gmail.com (ryles) Date: Tue, 28 Jul 2009 09:21:39 -0700 (PDT) Subject: Wrapping prstat on Solaris References: Message-ID: <1bce2ffd-85cb-4860-8b3e-92d8e1619a46@k19g2000yqn.googlegroups.com> On Jul 27, 10:26?am, s... at pobox.com wrote: > At work we currently use top to monitor ongoing system utilization on our > Solaris systems. ?As time has moved on though, use of top has become > problematic. ?Our admins want us to switch to prstat, Sun's top-like > command. ?It works fine however doesn't emit a timestamp at each display > interval, so it's essentially impossible looking at a prstat output > file to determine when the particular "screen" was emitted. > > If figured, "No problem. ?I'll just write a little wrapper." ?Alas, that is > proving harder than I thought. ?I can run prstat directing output to a file > and it seems to blast out a block of lines every N seconds, but when run > from inside a Python script I can't seem to make the damn thing not > massively buffer its output. ?Accordingly, my script doesn't really see the > output as its emitted, so the timestamp line it prepends to a block of > output is off. > > I'm currently using subprocess.Popen like so: > > ? ? os.environ["TERM"] = "dumb" > ? ? cmd = "prstat -c %s" % " ".join(sys.argv[1:]) > ? ? pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout > > I've tried these other variants as well: > > ? * prefacing the prstat command with unbuffer (the tcl/expect thingamabob) > > ? * explicitly redirect the prstat output to /dev/stdout > > ? * setting bufsize to 0 > > ? * used os.popen instead of Subprocess.Popen > > Nothing seems to help. ?I always seem to see about 30 seconds of data at > once (I'm currently using a 5-second interval and 10 lines of output). ?I > would have expected that prstat would simply flush stdout after each block > of output. > > Any ideas about how to get prstat to cooperate better? > > Thanks, > > -- > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > ? ? That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire Hey Skip, You haven't told us how you are actually reading from prstat's output pipe, which may be the cause. For instance, if you are doing for line in pipe: print line ... then this could cause your buffering issue. Instead try using readline(): while True: line = pipe.readline() ... From daniel.harjanto at gmail.com Tue Jul 28 12:30:26 2009 From: daniel.harjanto at gmail.com (misterdi) Date: Tue, 28 Jul 2009 09:30:26 -0700 (PDT) Subject: Python as active script The output should be something like I have tried and failed. I am reading http://www.amk.ca/python/howto/regex/ and hope to get somewhere with this soon. If this is something simple, can someone please help me out. Thanks in advance for any help. From contact at xavierho.com Tue Jul 28 19:15:34 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 09:15:34 +1000 Subject: Extract images from PDF files In-Reply-To: References: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Message-ID: <2d56febf0907281615g798087dcrb98b3b283befee71@mail.gmail.com> I've got a non-Python solution if you have Acrobat 6 or up. >From the menu, Advanced -> Document Processing -> Extract All Images... If you need multiple PDFs done, Batch Processing would be a great start. Then you can run another script to make the thumbnails, or use Photoshop. Either way works! Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Wed, Jul 29, 2009 at 6:21 AM, writeson wrote: > David, > > Thanks for your reply, I'll take a look at pdftohtml and see if it > suits my needs. > > Thanks! > Doug > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Tue Jul 28 19:19:41 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 29 Jul 2009 01:19:41 +0200 Subject: Semaphore Techniques In-Reply-To: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: <4A6F878D.6010403@cheimes.de> John D Giotta schrieb: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? Since you are talking about crontab I assume that you are on an os that supports pthreads. You problem can easily be solved with a named semaphore (see sem_open(3) and sem_overview(7)). Unfortunately Python doesn't expose named semaphores. The multiprocessing library uses named semaphores but you can't set the name yourself. You have to write your own C wrapper or search on pypi and through Google. If you are going to write your own semaphore I highly recommend Cython. Christian From philip at semanchuk.com Tue Jul 28 19:26:53 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Tue, 28 Jul 2009 19:26:53 -0400 Subject: Semaphore Techniques In-Reply-To: <4A6F878D.6010403@cheimes.de> References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> <4A6F878D.6010403@cheimes.de> Message-ID: On Jul 28, 2009, at 7:19 PM, Christian Heimes wrote: > John D Giotta schrieb: >> I'm looking to run a process with a limit of 3 instances, but each >> execution is over a crontab interval. I've been investigating the >> threading module and using daemons to limit active thread objects, >> but >> I'm not very successful at grasping the documentation. >> >> Is it possible to do what I'm trying to do and if so anyone know of a >> useful example to get started? > > Since you are talking about crontab I assume that you are on an os > that > supports pthreads. You problem can easily be solved with a named > semaphore (see sem_open(3) and sem_overview(7)). Unfortunately Python > doesn't expose named semaphores. The multiprocessing library uses > named > semaphores but you can't set the name yourself. > > You have to write your own C wrapper or search on pypi and through > Google. If you are going to write your own semaphore I highly > recommend > Cython. My POSIX IPC extension permits manipulation of interprocess semaphores: http://semanchuk.com/philip/posix_ipc/ There's also one for SysV IPC: http://semanchuk.com/philip/sysv_ipc/ Enjoy P From db3l.net at gmail.com Tue Jul 28 19:30:21 2009 From: db3l.net at gmail.com (David Bolen) Date: Tue, 28 Jul 2009 19:30:21 -0400 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: John D Giotta writes: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? Does it have to be built into the tool, or are you open to handling the restriction right in the crontab entry? For example, a crontab entry like: * * * * * test `pidof -x script.py | wc -w` -ge 4 || /script.py should attempt to run script.py every minute (adjust period as required) unless there are already four of them running. And if pidof isn't precise enough you can put anything in there that would accurately check your processes (grep a ps listing or whatever). This works because if the test expression is true it returns 0 which terminates the logical or (||) expression. There may be some variations based on cron implementation (the above was tested against Vixie cron), but some similar mechanism should be available. If you wanted to build it into the tool, it can be tricky in terms of managing shared state (the count) amongst purely sibling/cooperative processes. It's much easier to ensure no overlap (1 instance), but once you want 'n' instances you need an accurate process-wide counter. I'm not positive, but don't think Python's built-in semaphores or shared memory objects are cross-process. (Maybe something in multiprocessing in recent Python versions would work, though they may need the sharing processes to all have been executed from a parent script) I do believe there are some third party interfaces (posix_ipc, shm/shm_wrapper) that would provide access to posix shared-process objects. A semaphore may still not work as I'm not sure you can obtain the current count. But you could probably do something with a shared memory counter in conjunction with a mutex of some sort, as long as you were careful to clean it up on exit. Or, you could stick PIDs into the shared memory and count PIDs on a new startup (double checking against running processes to help protect against process failures without cleanup). You could also use the filesystem - have a shared directory where each process dumps its PID, after first counting how many other PIDs are in the directory and exiting if too many. Of course all of these (even with a PID check) are risky in the presence of unexpected failures. It would be worse with something like C code, but it should be reasonably easy to ensure that your script has cleanup code even on an unexpected termination, and it's not that likely the Python interpreter itself would crash. Then again, something external could kill the process. Ensuring accuracy and cleanup of shared state can be non-trivial. You don't mention if you can support a single master daemon, but if you could, then it can get a little easier as it can maintain and protect access to the state - you could have each worker process maintain a socket connection of some sort with the master daemon so it could detect when they terminate for the count, and it could just reject such connections from new processes if too many are running already. Of course, if the master daemon goes away then nobody would run, which may or may not be an acceptable failure mode. All in all, unless you need the scripts to enforce this behavior even in the presence of arbitrary use, I'd just use an appropriate crontab entry and move on to other problems :-) -- David From pavlovevidence at gmail.com Tue Jul 28 19:36:49 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 28 Jul 2009 16:36:49 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: On Jul 28, 3:15?pm, John D Giotta wrote: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? It seems like you want to limit the number of processes to three; the threading module won't help you there because it deals with threads within a single process. What I'd do is to simply run the system ps to see how many processes are running (ps is pretty versatile on most systems and can find specifically targeted processes like you program), and exit if there are already three. If you really are talking about multiple threads on a single server process, then you want to use a thread pool (create three threads, and give them tasks as necessary). But you'll have to have a way for the process started by crontab to communicate with the server. Carl Banks From zuo at chopin.edu.pl Tue Jul 28 20:06:32 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Wed, 29 Jul 2009 02:06:32 +0200 Subject: need help using Tkinter In-Reply-To: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> References: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> Message-ID: 28-07-2009 o 17:17 Manzur Ahmed wrote: > i wanted to ask you if one of you could help me to use Tkinter. i'm > trying > to just create a simple GUI for which I have provided the code for below. [snip] > i tried to google search this and there were some threads that were > saying > to install tcl but that does not work. I did check the Lib directory > under > the Python directory and I did not see a Tkinter module anywhere which I > think is the problem. Do you know how I would go about with getting this > module and if that is not the problem or if you think it might be caused > by > something else, can you please give me some guidance? What operating system do you use? *j -- Jan Kaliszewski (zuo) From sjmachin at lexicon.net Tue Jul 28 20:26:40 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 28 Jul 2009 17:26:40 -0700 (PDT) Subject: xlrd - insert column? References: <75fb7538-5079-4c99-83b7-cf3afa1e222c@c29g2000yqd.googlegroups.com> Message-ID: On Jul 29, 6:05?am, LeeRisq wrote: > Does xlrd have the capability to insert a column? No. If there was anything in the PyPI description, the README, or the docs that gave you the faintest hope that xlrd does anything other than read Excel files, please raise a documentation bug-fix request. Start here: http://www.python-excel.org and read the tutorial. Cheers, John From nobody at nowhere.com Tue Jul 28 20:45:08 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 01:45:08 +0100 Subject: bad certificate error References: <332a5bba-48d5-4c8b-9c1e-288790d85dbb@o32g2000yqm.googlegroups.com> Message-ID: On Tue, 28 Jul 2009 21:44:05 +0200, Piet van Oostrum wrote: >>j> # cert_file is a PEM formatted certificate chain file. >>j> connection = httplib.HTTPSConnection(host, int(port), key_file, >>j> cert_file) > > What happens if you set cert_file to None? This would indicate that you > are not interested in the server's certificate. > > By the way, is the cert_file you supply the certificate of the CA that > signed the server's cert (in contrast to yours)? The cert_file parameter is the client certificate. You must provide either both key_file and cert_file, or neither. From nobody at nowhere.com Tue Jul 28 20:54:23 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 01:54:23 +0100 Subject: Regular expression solution References: Message-ID: On Tue, 28 Jul 2009 16:12:19 -0700, nickname wrote: > I'm a newbie with python. I am looking to write a regex > program which can take in a piece of text and remove some parts of it > and supply the remaining parts back. > > The input can be something like: > > > The output should be something like > import re r = re.compile(r'"\s*\+\s*"') s = r'''''' r.sub('', s) From python at mrabarnett.plus.com Tue Jul 28 20:58:50 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 01:58:50 +0100 Subject: New implementation of re module In-Reply-To: <4a6f5a0f$0$25500$426a74cc@news.free.fr> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <4a6f5a0f$0$25500$426a74cc@news.free.fr> Message-ID: <4A6F9ECA.3010205@mrabarnett.plus.com> William Dode wrote: > On 28-07-2009, MRAB wrote: > >> With the official Python 2.6 distribution for Mac OS X it works. >> The source code is intended to replace the current 're' module in Python >> 2.7 (and I'll be porting it to Python 3.2), so I'm not that worried >> about Python versions earlier than 2.6 for testing, although if there's >> sufficient need then I could tweak the sources for 2.5. > > I understand now why i could'nt compile it ! > > So, i would like if it's not too much work for you. > There's a new version which should compile with Python 2.5 now. From cocobear.cn at gmail.com Tue Jul 28 21:20:29 2009 From: cocobear.cn at gmail.com (cocobear) Date: Tue, 28 Jul 2009 18:20:29 -0700 (PDT) Subject: merge two png pic Message-ID: This two png file has their own palette >>> im1.mode 'P' >>> im.mode 'P' >>> im.getpalette == im1.getpalette False I can use this code to merge two png pic together: Map = Image.new("RGB", (x,y)) Map.paste(im, box) Map.paste(im1,box) Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) But if the two png pic is too big , or if I have to merge more pic together, I will get MemoryError: >>> Image.new("RGB",(44544,38656)) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in new return Image()._new(core.fill(mode, size, color)) MemoryError How can I directly merge two pic to a ?P' mode png with palette. From contact at xavierho.com Tue Jul 28 21:29:15 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 11:29:15 +1000 Subject: merge two png pic In-Reply-To: References: Message-ID: <2d56febf0907281829x4d43a1abse2c271ad857174c1@mail.gmail.com> On Wed, Jul 29, 2009 at 11:20 AM, cocobear wrote: > > >>> Image.new("RGB",(44544,38656)) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in > new > return Image()._new(core.fill(mode, size, color)) > MemoryError a 44544x38656 pixel PNG file? I don't mean to be nosy, but what is this for, if you could share? > How can I directly merge two pic to a ?P' mode png with palette. Save to disk incrementally is one way I can think of. It's a lot slower, but definitely more space than your RAM. Good luck, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From icebergwtf at gmail.com Tue Jul 28 23:11:28 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Wed, 29 Jul 2009 11:11:28 +0800 Subject: Regular expression solution In-Reply-To: References: Message-ID: <4314c1f70907282011g799ccbf2tf44a4d4dd95dc8a7@mail.gmail.com> 2009/7/29 Nobody : >> The output should be something like >> > > import re > r = re.compile(r'"\s*\+\s*"') > s = r'''''' > r.sub('', s) > Nobody's solution is good. here is more complicated solution, just for regex practice :-) >>> s = '' >>> new_s = re.sub(r'(?<=")(.+)(?=")', ''.join(re.findall(r'"([^"]+)"', s)), s) >>> new_s '' From Brian.Mingus at colorado.edu Tue Jul 28 23:42:31 2009 From: Brian.Mingus at colorado.edu (Brian) Date: Tue, 28 Jul 2009 21:42:31 -0600 Subject: Regular expression solution In-Reply-To: <4314c1f70907282011g799ccbf2tf44a4d4dd95dc8a7@mail.gmail.com> References: <4314c1f70907282011g799ccbf2tf44a4d4dd95dc8a7@mail.gmail.com> Message-ID: <9839a05c0907282042w778caa87je53b3d34e4b00049@mail.gmail.com> On Tue, Jul 28, 2009 at 9:11 PM, tiefeng wu wrote: > 2009/7/29 Nobody : > >> The output should be something like > >> > > > > import re > > r = re.compile(r'"\s*\+\s*"') > > s = r'''''' > > r.sub('', s) > > > > Nobody's solution is good. > here is more complicated solution, just for regex practice :-) > > >>> s = '' > >>> new_s = re.sub(r'(?<=")(.+)(?=")', ''.join(re.findall(r'"([^"]+)"', > s)), s) > >>> new_s > '' > -- > http://mail.python.org/mailman/listinfo/python-list > >>> from time import time>>> import re>>> s=''>>> *# my version*>>> start=time();null=s.replace(' +','+').replace('+ ','+').replace('+','').replace('"','').replace('(','("').replace(';)','";)');*print* time()-start2.8133392334e-05>>> *# nobody's version*>>> r = re.compile(r'"\s*\+\s*"')>>> start = time();r.sub('',s);*print* time()-start3.40938568115e-05>>> *# tiefeng's version*>>> start=time();re.sub(r'(?<=")(.+)(?=")', ''.join(re.findall(r'"([^"]+)"', s)), s);*print* time()-start0.000455141067505>>> 3.40938568115e-05/2.8133392334e-051.2118644067781548>>> 0.000455141067505/2.8133392334e-0516.177966101690096>>> 0.000455141067505/3.40938568115e-0513.349650349662966 -------------- next part -------------- An HTML attachment was scrubbed... URL: From darkneter at gmail.com Tue Jul 28 23:58:53 2009 From: darkneter at gmail.com (NighterNet) Date: Tue, 28 Jul 2009 20:58:53 -0700 (PDT) Subject: simple splash screen? Message-ID: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> I am trying to make a simple splash screen from python 3.1.Not sure where to start looking for it. Can any one help? From hjtoi-better-remove-before-reply at comcast.net Wed Jul 29 00:16:38 2009 From: hjtoi-better-remove-before-reply at comcast.net (Heikki Toivonen) Date: Tue, 28 Jul 2009 21:16:38 -0700 Subject: M2Crypto hangs on this URL References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > There's something strange about this URL: > > "https://sagar310.pontins.com/sraep/" The following program finishes fine for me using M2Crypto 0.20beta1 + openssl 0.9.8g. Like Martin mentioned in another message, maybe someone fixed the site. from M2Crypto import SSL ctx = SSL.Context() # If you comment out the next 2 lines, the connection won't be secure #ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, depth=9) #if ctx.load_verify_locations('ca.pem') != 1: raise Exception('No CA certs') c = SSL.Connection(ctx) c.connect(('sagar310.pontins.com', 443)) c.send('GET /sraep/ \n') c.close() -- Heikki Toivonen - http://heikkitoivonen.net From dearmanzur at gmail.com Wed Jul 29 00:35:44 2009 From: dearmanzur at gmail.com (Manzur Ahmed) Date: Wed, 29 Jul 2009 00:35:44 -0400 Subject: need help using Tkinter In-Reply-To: References: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> Message-ID: <60972b200907282135s7fd125b4h5dc118a60027ff64@mail.gmail.com> i figured it out. its actually because i'm using python 3.1. for python 3.0+, many of the packages are lowercased, tkinter being one of them and so thats why i wan getting a module not found error . . . thanks for getting back to me. manzur On Tue, Jul 28, 2009 at 8:06 PM, Jan Kaliszewski wrote: > 28-07-2009 o 17:17 Manzur Ahmed wrote: > > i wanted to ask you if one of you could help me to use Tkinter. i'm trying >> to just create a simple GUI for which I have provided the code for below. >> > [snip] > >> i tried to google search this and there were some threads that were saying >> to install tcl but that does not work. I did check the Lib directory under >> the Python directory and I did not see a Tkinter module anywhere which I >> think is the problem. Do you know how I would go about with getting this >> module and if that is not the problem or if you think it might be caused >> by >> something else, can you please give me some guidance? >> > > What operating system do you use? > > *j > > -- > Jan Kaliszewski (zuo) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Wed Jul 29 02:08:19 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 29 Jul 2009 03:08:19 -0300 Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> <027ed9be$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Tue, 28 Jul 2009 09:02:40 -0300, Steven D'Aprano escribi?: > On Mon, 27 Jul 2009 23:16:39 -0300, Gabriel Genellina wrote: > >> I don't see the point on "fixing" either the Python script or httplib to >> accomodate for an invalid server certificate... If it's just for >> internal testing, I'd use HTTP instead (at least until the certificate >> is fixed). > > In real life, sometimes you need to drive with bad brakes on your car, > walk down dark alleys in the bad part of town, climb a tree without a > safety line, and use a hammer without wearing goggles. We can do all > these things. > > The OP has said that, for whatever reason, he needs to ignore a bad > server certificate when connecting to HTTPS. Python is a language where > developers are allowed to shoot themselves in the foot, so long as they > do so in full knowledge of what they're doing. > > So, putting aside all the millions of reasons why the OP shouldn't accept > an invalid certificate, how can he accept an invalid certificate? Yes, I understand the situation, but I'm afraid there is no way (that I know of). At least not without patching _ssl.c; all the SSL negotiation is handled by the OpenSSL library itself. I vaguely remember a pure Python SSL implementation somewhere that perhaps could be hacked to bypass all controls. But making it work properly will probably require a lot more effort than installing a self signed certificate in the server... -- Gabriel Genellina From jayshree06comp at gmail.com Wed Jul 29 02:58:36 2009 From: jayshree06comp at gmail.com (jayshree) Date: Tue, 28 Jul 2009 23:58:36 -0700 (PDT) Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> <7d5brmF2a1alcU1@mid.uni-berlin.de> Message-ID: <2b5b00b8-f9ba-4d16-b950-681aa6fa010a@24g2000yqm.googlegroups.com> On Jul 27, 2:55?pm, "Diez B. Roggisch" wrote: > jayshree wrote: > > On Jul 27, 1:09?pm, Kushal Kumaran > > wrote: > >> On Mon, Jul 27, 2009 at 12:58 PM, jayshree > >> wrote: > >> > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > >> > Please tell me how to open a file placed in any directory or in same > >> > directory. > > >> > After opening this file i want to use the contain (public key ) for > >> > encryption > > >> Does the code you've put into your message not read that file? ?If you > >> get an exception, copy-paste in the traceback message you get into > >> your mail. > > >> -- > >> kushal > > > ? ? try: > > ? ? ? ? pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > ? ? except IOError: > > ? ? ? ? print "Error: can\'t find file or read data" > > ? ? else: > > ? ? ? ? print "reading from file successfully" > > >>Still no error it gives .what to do? > > Erm - so it doesn't give an error - which means you have successfully opened > a file, and read it's contents. > > So what exactly is your problem? > > Diez- Hide quoted text - > > - Show quoted text - > The Exact Problem is how to use the key containing by .pem file for 'encryption' . >can i print the contents of the .pem file see http://stackoverflow.com/questions/1176864/problem-with-the-pem-file From gagsl-py2 at yahoo.com.ar Wed Jul 29 02:58:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 29 Jul 2009 03:58:39 -0300 Subject: "Deprecated sets module" with Python 2.6 References: <4A6F5F59.4080507@it.uu.se> Message-ID: En Tue, 28 Jul 2009 17:28:09 -0300, Virgil Stokes escribi?: > I would appreciate help on correcting a problem when trying to create an > *.exe file using py2exe via GUI2exe with Python 2.6.2. > > When using GUI2exe to create an *.exe I always get the following warning > during the compile process: > > C:\Python26\lib\site-packages\py2exe\build_exe.py:16: > DeprecationWarning: the sets module is deprecated > import sets Note that build_exe.py is part of the py2exe package - the warning says that py2exe itself is importing a deprecated module, it's not in your code. > and this results in the creation of an *.exe file that can not be > executed. I don't think the warning above is related to your problem. Are you using the latest py2exe version? You may get more help in the py2exe mailing list; see http://www.py2exe.org/ -- Gabriel Genellina From huangchonggnu at gmail.com Wed Jul 29 03:29:35 2009 From: huangchonggnu at gmail.com (hch) Date: Wed, 29 Jul 2009 15:29:35 +0800 Subject: No subject Message-ID: Hi, everyone Is there a python script can get a list of how much stack space each function in your program uses? ( the program is compiled by gcc) Any advice will be appreciate. From vs at it.uu.se Wed Jul 29 03:46:50 2009 From: vs at it.uu.se (Virgil Stokes) Date: Wed, 29 Jul 2009 09:46:50 +0200 Subject: "Deprecated sets module" with Python 2.6 In-Reply-To: <7d99s9F28m0aaU1@mid.uni-berlin.de> References: <7d99s9F28m0aaU1@mid.uni-berlin.de> Message-ID: <4A6FFE6A.8060503@it.uu.se> Diez B. Roggisch wrote: > Virgil Stokes schrieb: >> I would appreciate help on correcting a problem when trying to create >> an *.exe file using py2exe via GUI2exe with Python 2.6.2. >> >> When using GUI2exe to create an *.exe I always get the following >> warning during the compile process: >> >> C:\Python26\lib\site-packages\py2exe\build_exe.py:16: >> DeprecationWarning: the sets module is deprecated >> import sets >> >> and this results in the creation of an *.exe file that can not be >> executed. >> >> On the other hand, if I use the same procedure (on the same Python >> code) with >> Python 2.5, there are no warnings and the *.exe works fine. >> >> The procedure used is that given in the example "Less simpler one" at >> >> http://code.google.com/p/gui2exe/wiki/GUI2ExeExamplesin >> >> Any suggestions, help, ... would be greatly appreciated. > > If you don't need your app running on python2.3 and earlier, just > remove the sets-module and replace it with the builtin "set". Of course Diez, this is a good suggestion. However, in this case the Python code that I am trying to convert into an *.exe file does not refer to sets directly; i.e, the use of this module is buried within a package that is imported (wxPython) which is not under my control. --V > > Diez From huangchonggnu at gmail.com Wed Jul 29 03:51:26 2009 From: huangchonggnu at gmail.com (hch) Date: Wed, 29 Jul 2009 15:51:26 +0800 Subject: about analyze object's stack usage Message-ID: Hi, everyone Is there a python script can get a list of how much stack space each function in your program uses? ( the program is compiled by gcc) Any advice will be appreciate. From vs at it.uu.se Wed Jul 29 03:52:57 2009 From: vs at it.uu.se (Virgil Stokes) Date: Wed, 29 Jul 2009 09:52:57 +0200 Subject: "Deprecated sets module" with Python 2.6 In-Reply-To: <4A6FFE6A.8060503@it.uu.se> References: <7d99s9F28m0aaU1@mid.uni-berlin.de> <4A6FFE6A.8060503@it.uu.se> Message-ID: <4A6FFFD9.4050508@it.uu.se> Virgil Stokes wrote: > Diez B. Roggisch wrote: >> Virgil Stokes schrieb: >>> I would appreciate help on correcting a problem when trying to >>> create an *.exe file using py2exe via GUI2exe with Python 2.6.2. >>> >>> When using GUI2exe to create an *.exe I always get the following >>> warning during the compile process: >>> >>> C:\Python26\lib\site-packages\py2exe\build_exe.py:16: >>> DeprecationWarning: the sets module is deprecated >>> import sets >>> >>> and this results in the creation of an *.exe file that can not be >>> executed. >>> >>> On the other hand, if I use the same procedure (on the same Python >>> code) with >>> Python 2.5, there are no warnings and the *.exe works fine. >>> >>> The procedure used is that given in the example "Less simpler one" at >>> >>> http://code.google.com/p/gui2exe/wiki/GUI2ExeExamplesin >>> >>> Any suggestions, help, ... would be greatly appreciated. >> >> If you don't need your app running on python2.3 and earlier, just >> remove the sets-module and replace it with the builtin "set". > Of course Diez, this is a good suggestion. However, in this case the > Python code that I am trying to convert into an *.exe file does not > refer to sets directly; i.e, the use of this module is buried within a > package that is imported (wxPython) which is not under my control. > > --V >> >> Diez > > Whoops, the reference to the module sets is in py2exe not wxPython. --V From hendrik at microcorp.co.za Wed Jul 29 04:08:24 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 29 Jul 2009 10:08:24 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4A6F1506.9030207@mrabarnett.plus.com> References: <200907281035.48713.hendrik@microcorp.co.za> <4A6F1506.9030207@mrabarnett.plus.com> Message-ID: <200907291008.24572.hendrik@microcorp.co.za> On Tuesday 28 July 2009 17:11:02 MRAB wrote: > If you were a "COBOL" programmer, would you want to shout about it? :-) Hey don't knock it! - at the time, it was either COBOL or FORTRAN or some assembler or coding in hex or octal. And if code is data, where is Pythons ALTER statement? *Ducks* :-) Hendrik From milesck at umich.edu Wed Jul 29 04:14:03 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 29 Jul 2009 01:14:03 -0700 Subject: python 3 and stringio.seek In-Reply-To: References: Message-ID: On Jul 28, 2009, at 6:30 AM, Michele Petrazzo wrote: > Hi list, > I'm trying to port a my library to python 3, but I have a problem > with a > the stringio.seek: > the method not accept anymore a value like pos=-6 mode=1, but the > "old" > (2.X) version yes... > > The error: > File "/home/devel/Py3/lib/python3.0/io.py", line 2031, in seek > return self._seek(pos, whence) > IOError: Can't do nonzero cur-relative seeks > > > How solve this? In Python 2, StringIO is a stream of bytes (non-Unicode characters). In Python 3, StringIO is a stream of text (Unicode characters). In the early development of Python 3 (and 3.1's _pyio), it was implemented as a TextIOWrapper over a BytesIO buffer. TextIOWrapper does not support relative seeks because it is difficult to map the concept of a "current position" between bytes and the text that it encodes, especially with variable-width encodings and other considerations. Furthermore, the value returned from TextIOWrapper.tell isn't just a file position but a "cookie" that contains other data necessary to restore the decoding mechanism to the same state. However, for the default encoding (utf-8), the current position is equivalent to that of the underlying bytes buffer. In Python 3, StringIO is implemented using an internal buffer of Unicode characters. There is no technical reason why it can't support relative seeks; I assume it does not for compatibility with the original Python TextIOWrapper implementation (which is present in 3.1's _pyio, but not in 3.0). Note that because of the different implementations, StringIO.tell() (and seek) behaves differently for the C and Python implementations: $ python3.1 >>> import io, _pyio >>> s = io.StringIO('\u263A'); s.read(1), s.tell() ('?', 1) >>> s = _pyio.StringIO('\u263A'); s.read(1), s.tell() ('?', 3) The end result seems to be that, for text streams (including StreamIO), you *should* treat the value returned by tell() as an opaque magic cookie, and *only* pass values to seek() that you have obtained from a previous tell() call. However, in practice, it appears that you *may* seek StringIO objects relatively by characters using s.seek(s.tell() + n), so long as you do not use the _pyio.StringIO implementation. If what you actually want is a stream of bytes, use BytesIO, which may be seeked (sought?) however you please. I'm basing this all on my reading of the Python source (and svn history), since it doesn't seem to be documented, so take it with a grain of salt. -Miles From gregor.thalhammer at gmail.com Wed Jul 29 04:14:31 2009 From: gregor.thalhammer at gmail.com (gregorth) Date: Wed, 29 Jul 2009 01:14:31 -0700 (PDT) Subject: fast video encoding Message-ID: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Hi all, for a scientific application I need to save a video stream to disc for further post processing. My cam can deliver 8bit grayscale images with resolution 640x480 with a framerate up to 100Hz, this is a data rate of 30MB/s. Writing the data uncompressed to disc hits the data transfer limits of my current system and creates huge files. Therefore I would like to use video compression, preferably fast and high quality to lossless encoding. Final file size is not that important. Because of the hardware I am bound to WinXP. I already tried pymedia for encoding to mpeg2, however I only managed to get a framerate of about 30-40fps (on a 1.8GHz dual core). There is still room for improvements in my code, but before trying hard I want to ask for advices or other possibilities. I also found gstreamer with pygst python bindings, which seems to be more modern (and performant?) package than pymedia. I did not yet try it, especially since I didn't find a simple code example of how to use it for my use case. Can somebody give me a hint? I also found huffyuv or lagarith which is provided as a directshow codec for Windows. Can somebody give me a hint how to use a directshow codec with python? I am a novice with video encoding. I found that few codecs support gray scale images. Any hints to take advantage of the fact that I only have gray scale images? Thanks for any help Gregor From __peter__ at web.de Wed Jul 29 04:21:05 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jul 2009 10:21:05 +0200 Subject: instead of depending on data = array('h') .. write samples 1 by 1 to w = wave.open("wav.wav", "w") References: Message-ID: '2+ wrote: > it says > Wave_write.writeframes(data) > will that mean > "from array import array" > is a must? > > this does the job: > > import oil > import wave > from array import array > > a = oil.Sa() > > w = wave.open("current.wav", "w") > w.setnchannels(2) > w.setsampwidth(2) > w.setframerate(44100) > > data = array('h') > > for gas in range(44100 * 5): > a.breath() > r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > data.append(r) > data.append(l) > > w.writeframes(data.tostring()) > w.close() > > don't like array becoming so huge so tested this and it was also okay: > > for gas in range(44100 * 5): > a.breath() > data = array('h') > r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > data.append(r) > data.append(l) > w.writeframes(data.tostring()) > > but without array .. it becomes 15secs(3 times longer than was > intended to be) of wav file: > > for gas in range(44100 * 5): > a.breath() > r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > w.writeframes(hex(r)) > w.writeframes(hex(l)) Doesn't it sound strange, too? You are writing bogus bytes to the file. Compare: >>> import array >>> array.array("h", [42]).tostring() '*\x00' >>> hex(42) '0x2a' Not only do they differ in length, the contents are different, too. If you're unfamiliar with string escape codes, here's a way to see the bytes: >>> map(ord, array.array("h", [42]).tostring()) [42, 0] >>> map(ord, hex(42)) [48, 120, 50, 97] > should i just be happy with depennding on using array? > or is there a solution to make the last one work properly? There is a way to make the last one work: use struct.pack("h", r) instead of hex(r). I'm of course assuming that the first version does give you the desired result. You should not continue before you have verified that. I still recommend array for performance reasons. If memory usage is an issue you can adopt a hybrid approach: # untested from itertools import islice from array import array def gen_data(): for gas in xrange(44100 * 5): # range --> xrange a.breath() r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) yield r yield l data = gen_data() N = 2**20 while True: chunk = array('h') chunk.extend(islice(data, N)) if not chunk: break w.writeframes(chunk.tostring()) This will limit the array size to 4N bytes. Peter From michael at stroeder.com Wed Jul 29 04:45:05 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Wed, 29 Jul 2009 10:45:05 +0200 Subject: 64-bit issues with dictionaries in Python 2.6 Message-ID: HI! Are there any known issues with dictionaries in Python 2.6 (not 2.6.2) when running on a 64-bit platform? A friend of mine experiences various strange problems with my web2ldap running with Python 2.6 shipped with openSUSE 11.1 x64. For me it seems some dictionary-based internal registries of web2ldap are not working. Yes, the next test would be to compile Python 2.6.2 from source... Any hint is appreciated. Ciao, Michael. From user at example.net Wed Jul 29 05:02:25 2009 From: user at example.net (superpollo) Date: Wed, 29 Jul 2009 11:02:25 +0200 Subject: idiom for list looping Message-ID: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> hi clp. i want to get from here: nomi = ["one", "two", "three"] to here: 0 - one 1 - two 2 - three i found two ways: first way: for i in range(len(nomi)): print i, "-", nomi[i] or second way: for (i, e) in enumerate(nomi): print i, "-", e which one is "better"? is there a more pythonic way to do it? bye ps: max2 at 192.168.1.102:~/test$ python -V Python 2.3.4 max2 at 192.168.1.102:~/test$ uname -a Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux max2 at 192.168.1.102:~/test$ From d at vidr.cc Wed Jul 29 05:31:47 2009 From: d at vidr.cc (David Roberts) Date: Wed, 29 Jul 2009 19:31:47 +1000 Subject: idiom for list looping In-Reply-To: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> Message-ID: To the best of my knowledge the second way is more pythonic - the first is a little too reminiscent of C. A couple of notes: - you don't need the parentheses around "i, e" - if you were going to use the first way it's better to use xrange instead of range for iteration -- David Roberts http://da.vidr.cc/ On Wed, Jul 29, 2009 at 19:02, superpollo wrote: > hi clp. > > i want to get from here: > > nomi = ["one", "two", "three"] > > to here: > > 0 - one > 1 - two > 2 - three > > i found two ways: > > first way: > > for i in range(len(nomi)): > ? ?print i, "-", nomi[i] > > or second way: > > for (i, e) in enumerate(nomi): > ? ?print i, "-", e > > which one is "better"? is there a more pythonic way to do it? > > bye > > ps: > > max2 at 192.168.1.102:~/test$ python -V > Python 2.3.4 > max2 at 192.168.1.102:~/test$ uname -a > Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux > max2 at 192.168.1.102:~/test$ > -- > http://mail.python.org/mailman/listinfo/python-list > From python at mrabarnett.plus.com Wed Jul 29 05:38:29 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 10:38:29 +0100 Subject: idiom for list looping In-Reply-To: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> Message-ID: <4A701895.5000003@mrabarnett.plus.com> superpollo wrote: > hi clp. > > i want to get from here: > > nomi = ["one", "two", "three"] > > to here: > > 0 - one > 1 - two > 2 - three > > i found two ways: > > first way: > > for i in range(len(nomi)): > print i, "-", nomi[i] > > or second way: > > for (i, e) in enumerate(nomi): > print i, "-", e > > which one is "better"? is there a more pythonic way to do it? > 'enumerate' is the Pythonic solution. From gnewsg at gmail.com Wed Jul 29 06:25:19 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Wed, 29 Jul 2009 03:25:19 -0700 (PDT) Subject: "Deprecated sets module" with Python 2.6 References: <4A6F5F59.4080507@it.uu.se> Message-ID: What about this? import warnings warnings.simplefilter('ignore', DeprecationWarning) import the_module_causing_the_warning warnings.resetwarnings() --- Giampaolo http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil From contact at xavierho.com Wed Jul 29 06:37:44 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 20:37:44 +1000 Subject: idiom for list looping In-Reply-To: <4A701895.5000003@mrabarnett.plus.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> Message-ID: <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> > > superpollo wrote: > >> >> for (i, e) in enumerate(nomi): >> print i, "-", e >> >> Just to be random: print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) This has one advantage: only print once. So it's slightly faster if you have a list of a large amount. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Wed Jul 29 06:52:08 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 11:52:08 +0100 Subject: idiom for list looping In-Reply-To: <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> Message-ID: <4A7029D8.3010700@mrabarnett.plus.com> Xavier Ho wrote: > superpollo wrote: > > > for (i, e) in enumerate(nomi): > print i, "-", e > > Just to be random: > > print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) > > This has one advantage: only print once. So it's slightly faster if you > have a list of a large amount. > Slightly shorter: print '\n'.join("%s - %s" % p for p in enumerate(nomi)) :-) From contact at xavierho.com Wed Jul 29 07:00:37 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 21:00:37 +1000 Subject: idiom for list looping In-Reply-To: <4A7029D8.3010700@mrabarnett.plus.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4A7029D8.3010700@mrabarnett.plus.com> Message-ID: <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> On Wed, Jul 29, 2009 at 8:52 PM, MRAB wrote: > Slightly shorter: > > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > > :-) > That's cool. Does that make the "list" a tuple? (not that it matters, I'm just curious!) I just tested for a list of 1000 elements (number in letters from 1 to 1000 code I wrote for doing project euler problem 17), and the join method is 0.1 seconds faster than the for loop! Woo!? ;p -------------- next part -------------- An HTML attachment was scrubbed... URL: From user at example.net Wed Jul 29 07:02:14 2009 From: user at example.net (superpollo) Date: Wed, 29 Jul 2009 13:02:14 +0200 Subject: idiom for list looping In-Reply-To: References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> Message-ID: <4a702c37$0$34617$4fafbaef@reader4.news.tin.it> MRAB wrote: > Xavier Ho wrote: > >> superpollo wrote: >> >> >> for (i, e) in enumerate(nomi): >> print i, "-", e >> >> Just to be random: >> >> print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) >> >> This has one advantage: only print once. So it's slightly faster if >> you have a list of a large amount. >> > Slightly shorter: > > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > > :-) >>> print '\n'.join("%s - %s" % p for p in enumerate(nomi)) File "", line 1 print '\n'.join("%s - %s" % p for p in enumerate(nomi)) ^ SyntaxError: invalid syntax >>> print '\n'.join(["%s - %s" % p for p in enumerate(nomi)]) 0 - one 1 - two 2 - three >>> help() Welcome to Python 2.3! This is the online help utility. ... >>> ;-) bye From contact at xavierho.com Wed Jul 29 07:16:41 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 21:16:41 +1000 Subject: idiom for list looping In-Reply-To: <2d56febf0907290416x75d2edb3i88ad452ba18894f1@mail.gmail.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4a702c37$0$34617$4fafbaef@reader4.news.tin.it> <2d56febf0907290416x75d2edb3i88ad452ba18894f1@mail.gmail.com> Message-ID: <2d56febf0907290416x389a1b72w64f583d1d229bbf9@mail.gmail.com> Ack, sent to the wrong email again. On Wed, Jul 29, 2009 at 9:02 PM, superpollo wrote: > > >>> print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > File "", line 1 > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > ^ > SyntaxError: invalid syntax lol, I knew I shouldn't have trusted untested code! > >>> print '\n'.join(["%s - %s" % p for p in enumerate(nomi)]) > 0 - one > 1 - two > 2 - three > Yup.. but using a variable for the tuple itself was a good move though =P. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Wed Jul 29 07:17:14 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 12:17:14 +0100 Subject: idiom for list looping In-Reply-To: <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4A7029D8.3010700@mrabarnett.plus.com> <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> Message-ID: <4A702FBA.8060303@mrabarnett.plus.com> Xavier Ho wrote: > On Wed, Jul 29, 2009 at 8:52 PM, MRAB > wrote: > > Slightly shorter: > > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > > :-) > > That's cool. Does that make the "list" a tuple? (not that it matters, > I'm just curious!) > I've just replaced the list comprehension with a generator expression. From contact at xavierho.com Wed Jul 29 07:20:50 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 21:20:50 +1000 Subject: idiom for list looping In-Reply-To: <4A702FBA.8060303@mrabarnett.plus.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4A7029D8.3010700@mrabarnett.plus.com> <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> <4A702FBA.8060303@mrabarnett.plus.com> Message-ID: <2d56febf0907290420m7543ee8bs3c6ad9914fb51d39@mail.gmail.com> On Wed, Jul 29, 2009 at 9:17 PM, MRAB wrote: > I've just replaced the list comprehension with a generator expression. > > Oh, and that isn't in Python 2.3.... I see. Generators are slightly newer, eh. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From nick at craig-wood.com Wed Jul 29 07:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 29 Jul 2009 06:29:56 -0500 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: John D Giotta wrote: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? If you want a simple, cross platform way of doing it, then bind each process to a different local tcp port. Make a list of 3 ports, and try binding to each port in turn. If you can't find a port to bind to then there are already 3 instances running. Something like this import socket PORTS = range(10000,10003) lock_sock = None def lock_process(_locks = []): for port in PORTS: sock = socket.socket() try: sock.bind(("localhost", port)) except socket.error, e: sock = None else: _locks.append(sock) break else: raise Exception("Too many instances of me running") for i in range(5): print "Trying",i+1 lock_process() Which prints Trying 1 Trying 2 Trying 3 Trying 4 Traceback (most recent call last): File "", line 20, in File "", line 16, in lock_process Exception: Too many instances of me running You could do the same thing with lock files also very easily... -- Nick Craig-Wood -- http://www.craig-wood.com/nick From spookyk at gmail.com Wed Jul 29 07:35:42 2009 From: spookyk at gmail.com (Rincewind) Date: Wed, 29 Jul 2009 04:35:42 -0700 (PDT) Subject: QFileDialog setFileMode blues Message-ID: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> Heya, I am fairly new to Python and even newer to Qt. The problem is opening a Qt file dialog to select folders only. QFileDialog has a nice and dandy setFileMode() function just for that. The only trouble is that I cannot make it work. Last thing I've tried was something like this: self.fd = QtGui.QFileDialog() self.fd.setFileMode(self.fd.FileMode(4)) filename = self.fd.getOpenFileName() ..but it was fruitless. Any help? Tons of internets in return! From Ron.Barak at lsi.com Wed Jul 29 07:36:05 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Wed, 29 Jul 2009 12:36:05 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: References: Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> Hi, I wanted to make a python byte-code file executable, expecting to be able to run it without specifying "python" on the (Linux bash) command line. So, I wrote the following: [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python print "hello" [root at VMLinux1 python]# and made its pyc file executable: [root at VMLinux1 python]# ls -ls test_pyc.pyc 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc [root at VMLinux1 python]# So, I see: [root at VMLinux1 python]# file test_pyc.py* test_pyc.py: a python script text executable test_pyc.pyc: python 2.3 byte-compiled [root at VMLinux1 python]# If I try to do the following, no problem: [root at VMLinux1 python]# python test_pyc.pyc hello [root at VMLinux1 python]# However, the following fails: [root at VMLinux1 python]# ./test_pyc.pyc -bash: ./test_pyc.pyc: cannot execute binary file [root at VMLinux1 python]# Is there a way to run a pyc file without specifying the python path ? Bye, Ron. -------------- next part -------------- An HTML attachment was scrubbed... URL: From phil at riverbankcomputing.com Wed Jul 29 07:45:03 2009 From: phil at riverbankcomputing.com (Phil Thompson) Date: Wed, 29 Jul 2009 12:45:03 +0100 Subject: QFileDialog setFileMode blues In-Reply-To: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> References: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> Message-ID: <8477fc1d11b68f7e3e11237d957890b1@localhost> On Wed, 29 Jul 2009 04:35:42 -0700 (PDT), Rincewind wrote: > Heya, > > I am fairly new to Python and even newer to Qt. > The problem is opening a Qt file dialog to select folders only. > QFileDialog has a nice and dandy setFileMode() function just for that. > The only trouble is that I cannot make it work. > Last thing I've tried was something like this: > > self.fd = QtGui.QFileDialog() > self.fd.setFileMode(self.fd.FileMode(4)) > filename = self.fd.getOpenFileName() > > ..but it was fruitless. > > Any help? Tons of internets in return! QFileDialog.getOpenFileName() is a static method that creates its own QFileDialog internally. It's not using the one you have created and configured. Phil From marcusw at cox.net Wed Jul 29 08:23:14 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 08:23:14 -0400 Subject: fast video encoding In-Reply-To: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> References: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Message-ID: On 7/29/2009 4:14 AM, gregorth wrote: > Hi all, > > for a scientific application I need to save a video stream to disc for > further post processing. My cam can deliver 8bit grayscale images with > resolution 640x480 with a framerate up to 100Hz, this is a data rate > of 30MB/s. Writing the data uncompressed to disc hits the data > transfer limits of my current system and creates huge files. Therefore > I would like to use video compression, preferably fast and high > quality to lossless encoding. Final file size is not that important. Try googling realtime greyscale video codec... > Because of the hardware I am bound to WinXP. There's always a way to run linux :p > > I already tried pymedia for encoding to mpeg2, however I only managed > to get a framerate of about 30-40fps (on a 1.8GHz dual core). There is > still room for improvements in my code, but before trying hard I want > to ask for advices or other possibilities. I also found gstreamer with > pygst python bindings, which seems to be more modern (and performant?) > package than pymedia. I did not yet try it, especially since I didn't > find a simple code example of how to use it for my use case. Can > somebody give me a hint? Video encoding is not my specialty, but my recommendation here is to drop python because of its slow speed and work in c as much as possible. > > I also found huffyuv or lagarith which is provided as a directshow > codec for Windows. Can somebody give me a hint how to use a directshow > codec with python? Not me, sorry :( Never worked directly with directshow (no pun intended). > > I am a novice with video encoding. I found that few codecs support > gray scale images. Any hints to take advantage of the fact that I only > have gray scale images? Greyscale PNG or BMP compression. > > Thanks for any help Don't know if this counts as help, but you're welcome! > > Gregor > Marcus From marcusw at cox.net Wed Jul 29 08:24:33 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 08:24:33 -0400 Subject: simple splash screen? In-Reply-To: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: On 7/28/2009 11:58 PM, NighterNet wrote: > I am trying to make a simple splash screen from python 3.1.Not sure > where to start looking for it. Can any one help? Trying to make a splash screen for python? Or trying to do image processing in python? Marcus From sibtey.mehndi at genpact.com Wed Jul 29 08:25:22 2009 From: sibtey.mehndi at genpact.com (Mehndi, Sibtey) Date: Wed, 29 Jul 2009 05:25:22 -0700 Subject: how to embed the python interpreter into web App Message-ID: <1A343A98C8DAE44A8D9D1BF310F90D5E023D54C3@GCPWINGGN2EVS11.IND.CORP.AD> Hi All I am trying to embed the python interpreter in to a web app but could not get the way, any one can suggest me how to do this. Thanks, Sibtey Mehdi This e-mail (and any attachments), is confidential and may be privileged. It may be read, copied and used only by intended recipients. Unauthorized access to this e-mail (or attachments) and disclosure or copying of its contents or any action taken in reliance on it is unlawful. Unintended recipients must notify the sender immediately by e-mail/phone & delete it from their system without making any copies or disclosing it to a third person. -------------- next part -------------- An HTML attachment was scrubbed... URL: From duncan.booth at invalid.invalid Wed Jul 29 08:26:11 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 29 Jul 2009 12:26:11 GMT Subject: Need help in passing a "-c command" argument to the interactive shell. References: <230bcf11-2227-4d7f-9048-862c1bdb1912@13g2000prl.googlegroups.com> Message-ID: Bill wrote: > On my windows box I type => c:\x>python -c "import re" > > The result is => c:\x> > > In other words, the Python interactive shell doesn't even open. What > am I doing wrong? > > I did RTFM at http://www.python.org/doc/1.5.2p2/tut/node4.html on > argument passing, but to no avail. Why are you reading documentation for Python 1.5? There have been just a few releases since then. Try reading http://docs.python.org/using/cmdline.html instead, specifically the part about the -i command line option. Or you could just try "python -?" at a command prompt. -- Duncan Booth http://kupuguy.blogspot.com From marcusw at cox.net Wed Jul 29 08:34:09 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 08:34:09 -0400 Subject: about analyze object's stack usage In-Reply-To: References: Message-ID: On 7/29/2009 3:51 AM, hch wrote: > Is there a python script can get a list of how much stack space each > function in your program uses? I don't think so. You could try raw reading of the memory from another thread using ctypes and pointers, but that would be madness. > ( the program is compiled by gcc) If you're talking about a c program, almost certainly not. What you should do is just use gdb and disas each function and look at what it subtracts off of %esp at the third instruction in the function. I can explain it to you if you are not very experienced in gdb and assembly... Marcus From rashidsdpk at gmail.com Wed Jul 29 08:54:24 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Wed, 29 Jul 2009 05:54:24 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10.more http://0nanotechnology0.blogspot.com/ From catalinfest at gmail.com Wed Jul 29 09:03:06 2009 From: catalinfest at gmail.com (catalinfest at gmail.com) Date: Wed, 29 Jul 2009 06:03:06 -0700 (PDT) Subject: Help with sql and python Message-ID: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> Hello ! I have accont on myphpadmin on my server. I want to create one script in python. This script manage sql task from my sql server . How i make this in a simple way ? Thank you ! From contact at xavierho.com Wed Jul 29 09:08:12 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 23:08:12 +1000 Subject: Particle research opens door for new technology In-Reply-To: References: Message-ID: <2d56febf0907290608v4fe62d32n804e43008713572b@mail.gmail.com> Is this a spam? Why did you have to send it 4 times, and it's already in the past (July 9 and 10) ? Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From v1d4l0k4 at gmail.com Wed Jul 29 09:10:47 2009 From: v1d4l0k4 at gmail.com (v1d4l0k4) Date: Wed, 29 Jul 2009 13:10:47 +0000 Subject: Working with platform.system() Message-ID: <9a5b5be90907290610t1868ef07p20a5c2bdc3404ab8@mail.gmail.com> Hi! I want to know how I can get different expected return values by platform.system() method. I want to sniff some systems (Linux, Windows, Mac, BSD, Solaris) and I don't know how I can check them correctly. Could you give me some expected return values that you know? Thanks in advance, Paulo Ricardo -------------- next part -------------- An HTML attachment was scrubbed... URL: From james at agentultra.com Wed Jul 29 09:29:55 2009 From: james at agentultra.com (J Kenneth King) Date: Wed, 29 Jul 2009 09:29:55 -0400 Subject: escaping characters in filenames Message-ID: <854osv7h2k.fsf@agentultra.com> I wrote a script to process some files using another program. One thing I noticed was that both os.listdir() and os.path.walk() will return unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a module or recipe that escapes file names and was wondering if anyone could point me in the right direction. As an aside, the script is using subprocess.call() with the "shell=True" parameter. There isn't really a reason for doing it this way (was just the fastest way to write it and get a prototype working). I was wondering if Popen objects were sensitive to unescaped names like the shell. I intend to refactor the function to use Popen objects at some point and thought perhaps escaping file names may not be entirely necessary. Cheers From nospam at nospam.com Wed Jul 29 09:55:32 2009 From: nospam at nospam.com (Gilles Ganault) Date: Wed, 29 Jul 2009 15:55:32 +0200 Subject: Compiling Python for uCLinux appliance? Message-ID: <7sk075l5p7eq8mek9ooqj5kr0ds10hrvfq@4ax.com> Hello I just got a small appliance based on a Blackfin CPU with 64MB RAM and 258MB NAND flash. Using the stock software, there's about 30MB of RAM left. Besides C/C++ and shel scripts, I was wondering if it were realistic to upload a few Python scripts in such a small appliance? Thank you. From piet at cs.uu.nl Wed Jul 29 10:14:45 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 29 Jul 2009 16:14:45 +0200 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: >>>>> Carl Banks (CB) wrote: >CB> On Jul 28, 3:15?pm, John D Giotta wrote: >>> I'm looking to run a process with a limit of 3 instances, but each >>> execution is over a crontab interval. I've been investigating the >>> threading module and using daemons to limit active thread objects, but >>> I'm not very successful at grasping the documentation. >>> >>> Is it possible to do what I'm trying to do and if so anyone know of a >>> useful example to get started? >CB> It seems like you want to limit the number of processes to three; the >CB> threading module won't help you there because it deals with threads >CB> within a single process. >CB> What I'd do is to simply run the system ps to see how many processes >CB> are running (ps is pretty versatile on most systems and can find >CB> specifically targeted processes like you program), and exit if there >CB> are already three. That will surely run into some race conditions. If the limit of 3 processes is soft then that wouldn't be a big deal, however. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From spookyk at gmail.com Wed Jul 29 10:15:53 2009 From: spookyk at gmail.com (Rincewind) Date: Wed, 29 Jul 2009 07:15:53 -0700 (PDT) Subject: QFileDialog setFileMode blues References: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> Message-ID: On Jul 29, 12:45 pm, Phil Thompson wrote: > On Wed, 29 Jul 2009 04:35:42 -0700 (PDT), Rincewind > wrote: > > > Heya, > > > I am fairly new to Python and even newer to Qt. > > The problem is opening a Qt file dialog to select folders only. > > QFileDialog has a nice and dandy setFileMode() function just for that. > > The only trouble is that I cannot make it work. > > Last thing I've tried was something like this: > > > self.fd = QtGui.QFileDialog() > > self.fd.setFileMode(self.fd.FileMode(4)) > > filename = self.fd.getOpenFileName() > > > ..but it was fruitless. > > > Any help? Tons of internets in return! > > QFileDialog.getOpenFileName() is a static method that creates its own > QFileDialog internally. It's not using the one you have created and > configured. > > Phil You are awesome, thanks! From python at mrabarnett.plus.com Wed Jul 29 10:22:34 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 15:22:34 +0100 Subject: escaping characters in filenames In-Reply-To: <854osv7h2k.fsf@agentultra.com> References: <854osv7h2k.fsf@agentultra.com> Message-ID: <4A705B2A.2050807@mrabarnett.plus.com> J Kenneth King wrote: > I wrote a script to process some files using another program. One thing > I noticed was that both os.listdir() and os.path.walk() will return > unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ > File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a > module or recipe that escapes file names and was wondering if anyone > could point me in the right direction. > That's only necessary if you're building a command line and passing it as a string. > As an aside, the script is using subprocess.call() with the "shell=True" > parameter. There isn't really a reason for doing it this way (was just > the fastest way to write it and get a prototype working). I was > wondering if Popen objects were sensitive to unescaped names like the > shell. I intend to refactor the function to use Popen objects at some > point and thought perhaps escaping file names may not be entirely > necessary. > Pass the command line to Popen as a list of strings. From deets at nospam.web.de Wed Jul 29 10:46:47 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 29 Jul 2009 16:46:47 +0200 Subject: Compiling Python for uCLinux appliance? References: <7sk075l5p7eq8mek9ooqj5kr0ds10hrvfq@4ax.com> Message-ID: <7db5mnF2bf9i2U1@mid.uni-berlin.de> Gilles Ganault wrote: > Hello > > I just got a small appliance based on a Blackfin CPU with 64MB RAM and > 258MB NAND flash. Using the stock software, there's about 30MB of RAM > left. > > Besides C/C++ and shel scripts, I was wondering if it were realistic > to upload a few Python scripts in such a small appliance? Try & check out the gumstix software repo. It comes with python, and thus some crosscompiling/build-instructions should be in there. Diez From jakecjacobson at gmail.com Wed Jul 29 11:07:34 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Wed, 29 Jul 2009 08:07:34 -0700 (PDT) Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> <027ed9be$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Jul 29, 2:08?am, "Gabriel Genellina" wrote: > En Tue, 28 Jul 2009 09:02:40 -0300, Steven D'Aprano ? > escribi?: > > > > > On Mon, 27 Jul 2009 23:16:39 -0300, Gabriel Genellina wrote: > > >> I don't see the point on "fixing" either the Python script or httplib to > >> accomodate for an invalid server certificate... If it's just for > >> internal testing, I'd use HTTP instead (at least until the certificate > >> is fixed). > > > In real life, sometimes you need to drive with bad brakes on your car, > > walk down dark alleys in the bad part of town, climb a tree without a > > safety line, and use a hammer without wearing goggles. We can do all > > these things. > > > The OP has said that, for whatever reason, he needs to ignore a bad > > server certificate when connecting to HTTPS. Python is a language where > > developers are allowed to shoot themselves in the foot, so long as they > > do so in full knowledge of what they're doing. > > > So, putting aside all the millions of reasons why the OP shouldn't accept > > an invalid certificate, how can he accept an invalid certificate? > > Yes, I understand the situation, but I'm afraid there is no way (that I ? > know of). At least not without patching _ssl.c; all the SSL negotiation is ? > handled by the OpenSSL library itself. > > I vaguely remember a pure Python SSL implementation somewhere that perhaps ? > could be hacked to bypass all controls. But making it work properly will ? > probably require a lot more effort than installing a self signed ? > certificate in the server... > > -- > Gabriel Genellina I have it working and I want to thank everyone for their efforts and very helpful hints. The error was with me and not understanding the documentation about the cert_file & key_file. After using openssl to divide up my p12 file into a cert file and a key file using the instructions http://security.ncsa.uiuc.edu/research/grid-howtos/usefulopenssl.php. I got everything working. Again, much thanks. Jake From darkneter at gmail.com Wed Jul 29 11:20:45 2009 From: darkneter at gmail.com (NighterNet) Date: Wed, 29 Jul 2009 08:20:45 -0700 (PDT) Subject: simple splash screen? References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: <68539c2a-e2f6-4956-b779-17d21ca422b1@2g2000prl.googlegroups.com> On Jul 29, 5:24?am, Marcus Wanner wrote: > On 7/28/2009 11:58 PM, NighterNet wrote:> I am trying to make a simple splash screen from python 3.1.Not sure > > where to start looking for it. Can any one help? > > Trying to make a splash screen for python? > Or trying to do image processing in python? > > Marcus trying to use jpg,gif,png for the splash screen From wwn1 at sfu.ca Wed Jul 29 11:24:25 2009 From: wwn1 at sfu.ca (WilsonOfCanada) Date: Wed, 29 Jul 2009 08:24:25 -0700 (PDT) Subject: Clearing an array Message-ID: Hellos, I was wondering if there is any built-in function that clears the array. I was also wondering if this works: arrMoo = ['33', '342', .... '342'] arrMoo = [] From tutufan at gmail.com Wed Jul 29 11:24:52 2009 From: tutufan at gmail.com (Mike) Date: Wed, 29 Jul 2009 08:24:52 -0700 (PDT) Subject: New implementation of re module References: Message-ID: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> On Jul 27, 11:34?am, MRAB wrote: > I've been working on a new implementation of the re module. Fabulous! If you're extending/changing the interface, there are a couple of sore points in the current implementation I'd love to see addressed: - findall/finditer doesn't find overlapping matches. Sometimes you really *do* want to know all possible matches, even if they overlap. This comes up in bioinformatics, for example. - split won't split on empty patterns, e.g. empty lookahead patterns. This means that it can't be used for a whole class of interesting cases. This has been discussed previously: http://bugs.python.org/issue3262 http://bugs.python.org/issue852532 http://bugs.python.org/issue988761 - It'd be nice to have a version of split that generates the parts (one by one) rather than returning the whole list. - Repeated subgroup match information is not available. That is, for a match like this re.match('(.){3}', 'xyz') there's no way to discover that the subgroup first matched 'x', then matched 'y', and finally matched 'z'. Here is one past proposal (mine), perhaps over-complex, to address this problem: http://mail.python.org/pipermail/python-dev/2004-August/047238.html Mike From Omer.Khalid at cern.ch Wed Jul 29 11:26:55 2009 From: Omer.Khalid at cern.ch (Omer Khalid) Date: Wed, 29 Jul 2009 17:26:55 +0200 Subject: Very Strange Problem Message-ID: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Hi, I am having a very strange problem with modifying a variable in a list in my program. Here is the code: # a list that contains dictionary objects jobs = [] index=5 for each in range(index): jobs.append({'v':0}) some_function(index): if jobs[index]['v'] == 0: # set it to 1 jobs[index]['v'] = 1 print "Set to 1" else: print "Already set to 1" loop(): index=0 for each in range(len(jobs)): some_function(index) index +=1 Apparently, the jobs[index]['v'] never get updated in the some_function but the print statement afterwards get printed... What's really surprising is that there are no errors or exceptions and my my program runs in a single thread...so i have been unable to explain this behavior. Any insight would be much appreciated! Cheers Omer -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Wed Jul 29 11:32:36 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 29 Jul 2009 17:32:36 +0200 Subject: Clearing an array References: Message-ID: <7db8ckF28m03sU1@mid.uni-berlin.de> WilsonOfCanada wrote: > Hellos, > > I was wondering if there is any built-in function that clears the > array. I was also wondering if this works: > > arrMoo = ['33', '342', .... '342'] > arrMoo = [] Most of the time, yes. Unless you have something like this, then it won't work: foo = [10] bar = foo # an alias foo = [] assert foo == bar Then what you need to to is this: foo[:] = [] Diez From tack at urandom.ca Wed Jul 29 11:39:00 2009 From: tack at urandom.ca (Jason Tackaberry) Date: Wed, 29 Jul 2009 11:39:00 -0400 Subject: Clearing an array In-Reply-To: References: Message-ID: <1248881940.7696.28.camel@willow> On Wed, 2009-07-29 at 08:24 -0700, WilsonOfCanada wrote: > I was wondering if there is any built-in function that clears the > array. The proper python term would be "list." You can remove all elements of a list 'l' like so: del l[:] > I was also wondering if this works: > > arrMoo = ['33', '342', .... '342'] > arrMoo = [] That doesn't clear the list as such, but rather creates a new list, and reassigns the new list to the 'arrMoo' name in the local scope. Consider: >>> l1 = [1,2,3] >>> l2 = l1 >>> l1 = [] >>> print l2 [1, 2, 3] So the original list 'l1' lives on. However: >>> l1 = [1,2,3] >>> l2 = l1 >>> del l1[:] >>> print l1, l2 [] [] Cheers, Jason. From tjreedy at udel.edu Wed Jul 29 11:43:12 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 29 Jul 2009 11:43:12 -0400 Subject: python 3 and stringio.seek In-Reply-To: References: Message-ID: Miles Kaufmann wrote: > > On Jul 28, 2009, at 6:30 AM, Michele Petrazzo wrote: > >> Hi list, >> I'm trying to port a my library to python 3, but I have a problem with a >> the stringio.seek: >> the method not accept anymore a value like pos=-6 mode=1, but the "old" >> (2.X) version yes... >> >> The error: >> File "/home/devel/Py3/lib/python3.0/io.py", line 2031, in seek >> return self._seek(pos, whence) >> IOError: Can't do nonzero cur-relative seeks >> >> >> How solve this? > > In Python 2, StringIO is a stream of bytes (non-Unicode characters). In > Python 3, StringIO is a stream of text (Unicode characters). In the > early development of Python 3 (and 3.1's _pyio), it was implemented as a > TextIOWrapper over a BytesIO buffer. TextIOWrapper does not support > relative seeks because it is difficult to map the concept of a "current > position" between bytes and the text that it encodes, especially with > variable-width encodings and other considerations. Furthermore, the > value returned from TextIOWrapper.tell isn't just a file position but a > "cookie" that contains other data necessary to restore the decoding > mechanism to the same state. However, for the default encoding (utf-8), > the current position is equivalent to that of the underlying bytes buffer. > > In Python 3, StringIO is implemented using an internal buffer of Unicode > characters. There is no technical reason why it can't support relative > seeks; I assume it does not for compatibility with the original Python > TextIOWrapper implementation (which is present in 3.1's _pyio, but not > in 3.0). > > Note that because of the different implementations, StringIO.tell() (and > seek) behaves differently for the C and Python implementations: > > $ python3.1 > >>> import io, _pyio > >>> s = io.StringIO('\u263A'); s.read(1), s.tell() > ('?', 1) > >>> s = _pyio.StringIO('\u263A'); s.read(1), s.tell() > ('?', 3) It seems to me that this discrepancy might be worth a tracker item. I wonder why the second implementation is even there if not used. Two different commiters? > The end result seems to be that, for text streams (including StreamIO), > you *should* treat the value returned by tell() as an opaque magic > cookie, and *only* pass values to seek() that you have obtained from a > previous tell() call. However, in practice, it appears that you *may* > seek StringIO objects relatively by characters using s.seek(s.tell() + > n), so long as you do not use the _pyio.StringIO implementation. A tracker item could include a request that relative seek be restored if possible. tjr From python at mrabarnett.plus.com Wed Jul 29 11:45:55 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 16:45:55 +0100 Subject: New implementation of re module In-Reply-To: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> References: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> Message-ID: <4A706EB3.1040501@mrabarnett.plus.com> Mike wrote: > On Jul 27, 11:34 am, MRAB wrote: >> I've been working on a new implementation of the re module. > > Fabulous! > > If you're extending/changing the interface, there are a couple of sore > points in the current implementation I'd love to see addressed: > > - findall/finditer doesn't find overlapping matches. Sometimes you > really *do* want to know all possible matches, even if they overlap. > This comes up in bioinformatics, for example. > Perhaps by adding "overlapped=True"? > - split won't split on empty patterns, e.g. empty lookahead patterns. > This means that it can't be used for a whole class of interesting > cases. This has been discussed previously: > > http://bugs.python.org/issue3262 > http://bugs.python.org/issue852532 > http://bugs.python.org/issue988761 > Already addressed (see issue2636 for the full details). > - It'd be nice to have a version of split that generates the parts > (one by one) rather than returning the whole list. > Hmm, re.splititer() perhaps. > - Repeated subgroup match information is not available. That is, for > a match like this > > re.match('(.){3}', 'xyz') > > there's no way to discover that the subgroup first matched 'x', then > matched 'y', and finally matched 'z'. Here is one past proposal > (mine), perhaps over-complex, to address this problem: > > http://mail.python.org/pipermail/python-dev/2004-August/047238.html > Yikes! I think I'll let you code that... :-) From mdboldin at gmail.com Wed Jul 29 11:49:49 2009 From: mdboldin at gmail.com (mpython) Date: Wed, 29 Jul 2009 08:49:49 -0700 (PDT) Subject: SEC forms parsing Message-ID: I am looking for any python examples that have written to parse SEC filings (Security Exchange Commission's EDGAR system),or just a pointer to best modules to use. From python at mrabarnett.plus.com Wed Jul 29 11:53:47 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 16:53:47 +0100 Subject: Very Strange Problem In-Reply-To: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Message-ID: <4A70708B.4030402@mrabarnett.plus.com> Omer Khalid wrote: > Hi, > > I am having a very strange problem with modifying a variable in a list > in my program. Here is the code: > > # a list that contains dictionary objects > jobs = [] > > index=5 > for each in range(index): > jobs.append({'v':0}) > > some_function(index): > if jobs[index]['v'] == 0: > # set it to 1 > jobs[index]['v'] = 1 > print "Set to 1" > else: > print "Already set to 1" > > loop(): > index=0 > for each in range(len(jobs)): > some_function(index) > index +=1 > > > Apparently, the jobs[index]['v'] never get updated in the some_function > but the print statement afterwards get printed... > > What's really surprising is that there are no errors or exceptions and > my my program runs in a single thread...so i have been unable to explain > this behavior. > > Any insight would be much appreciated! > Well, when I insert the missing 'def's in the function definitions, it works for me. From ronn.ross at gmail.com Wed Jul 29 11:57:18 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Wed, 29 Jul 2009 10:57:18 -0500 Subject: Can you easy_install from your localhost? Message-ID: <9c8c445f0907290857x27930acbvc5067a242e18ed17@mail.gmail.com> I have a package i would like to store locally. If it is stored locally can I do something like ' easy_install http://localhost/package ' ? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid Wed Jul 29 12:02:53 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 29 Jul 2009 11:02:53 -0500 Subject: Compiling Python for uCLinux appliance? References: <7sk075l5p7eq8mek9ooqj5kr0ds10hrvfq@4ax.com> <7db5mnF2bf9i2U1@mid.uni-berlin.de> Message-ID: On 2009-07-29, Diez B. Roggisch wrote: > Gilles Ganault wrote: > >> Hello >> >> I just got a small appliance based on a Blackfin CPU with 64MB RAM and >> 258MB NAND flash. Using the stock software, there's about 30MB of RAM >> left. >> >> Besides C/C++ and shel scripts, I was wondering if it were realistic >> to upload a few Python scripts in such a small appliance? The standard uclinux-dist comes with python, so it's pretty much just a question of memory. Asking on the uclinux mailing list will probably be more useful, but I would guess that 64MB would be plenty -- especially if use JFFS to put your root filesystem in flash instead of in RAM. Of course it depends entirely on what else you want to do with that RAM at the same time... > Try & check out the gumstix software repo. It comes with > python, and thus some crosscompiling/build-instructions should > be in there. IIRC, all you have to do is "make menuconfig", go to the user apps page and check the "Python" box, and then "make". It should "just work". -- Grant Edwards grante Yow! I haven't been married at in over six years, but we visi.com had sexual counseling every day from Oral Roberts!! From ryniek90 at gmail.com Wed Jul 29 12:11:32 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 29 Jul 2009 18:11:32 +0200 Subject: how to embed the python interpreter into web App (Mehndi, Sibtey) In-Reply-To: References: Message-ID: <4A7074B4.2070204@gmail.com> > > > Hi All > > I am trying to embed the python interpreter in to a web app but could > not get the way, any one can suggest me how to do this. > > > > Thanks, > > Sibtey Mehdi > > > > This e-mail (and any attachments), is confidential and may be privileged. It may be read, copied and used only > by intended recipients. Unauthorized access to this e-mail (or attachments) and disclosure or copying of its > contents or any action taken in reliance on it is unlawful. Unintended recipients must notify the sender immediately > by e-mail/phone & delete it from their system without making any copies or disclosing it to a third person. > > > Here's example: http://try-python.mired.org/ Contact with author of that website/webapp. Good luck. From python at mrabarnett.plus.com Wed Jul 29 12:53:33 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 17:53:33 +0100 Subject: Very Strange Problem In-Reply-To: <4A7074E6.9030100@gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A70708B.4030402@mrabarnett.plus.com> <4A7074E6.9030100@gmail.com> Message-ID: <4A707E8D.4020306@mrabarnett.plus.com> Ricardo Ar?oz wrote: > MRAB wrote: >> Omer Khalid wrote: >>> Hi, >>> >>> I am having a very strange problem with modifying a variable in a >>> list in my program. Here is the code: >>> >>> # a list that contains dictionary objects >>> jobs = [] >>> >>> index=5 >>> for each in range(index): >>> jobs.append({'v':0}) >>> >>> some_function(index): >>> if jobs[index]['v'] == 0: >>> # set it to 1 >>> jobs[index]['v'] = 1 >>> print "Set to 1" >>> else: >>> print "Already set to 1" >>> >>> loop(): >>> index=0 >>> for each in range(len(jobs)): >>> some_function(index) >>> index +=1 >>> >>> >>> Apparently, the jobs[index]['v'] never get updated in the >>> some_function but the print statement afterwards get printed... >>> >>> What's really surprising is that there are no errors or exceptions >>> and my my program runs in a single thread...so i have been unable to >>> explain this behavior. >>> >>> Any insight would be much appreciated! >>> >> Well, when I insert the missing 'def's in the function definitions, it >> works for me. > Hi Omer, what he is trying to convey in his rude manner is that you are > missing "def" in your function definitions. It is probably a beginners > mistake. > > That is : > from "some_function(index): " > to "def some_function(index): " > > from "loop(): " > to "def loop(): " > > I have not tried your code so you should believe him when he states he > has actually run the code. > HTH > Omer says "the print statement afterwards get printed", but the code provided would have raised a SyntaxError, so omitting the 'def's can't be the cause of the actual problem reported. From nagle at animats.com Wed Jul 29 13:10:15 2009 From: nagle at animats.com (John Nagle) Date: Wed, 29 Jul 2009 10:10:15 -0700 Subject: bad certificate error In-Reply-To: References: Message-ID: <4a7081c9$0$1600$742ec2ed@news.sonic.net> jakecjacobson wrote: > Hi, > > I am getting the following error when doing a post to REST API, > > Enter PEM pass phrase: > Traceback (most recent call last): > File "./ices_catalog_feeder.py", line 193, in ? > main(sys.argv[1]) > File "./ices_catalog_feeder.py", line 60, in main > post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join > (input_dir, file), collection_name, key_file, cert_file) > File "./ices_catalog_feeder.py", line 125, in post2Catalog > connection.request('POST', path, parameters, head) > File "/usr/lib/python2.4/httplib.py", line 810, in request > self._send_request(method, url, body, headers) > File "/usr/lib/python2.4/httplib.py", line 833, in _send_request > self.endheaders() > File "/usr/lib/python2.4/httplib.py", line 804, in endheaders > self._send_output() > File "/usr/lib/python2.4/httplib.py", line 685, in _send_output > self.send(msg) > File "/usr/lib/python2.4/httplib.py", line 652, in send > self.connect() > File "/usr/lib/python2.4/httplib.py", line 1079, in connect > ssl = socket.ssl(sock, self.key_file, self.cert_file) > File "/usr/lib/python2.4/socket.py", line 74, in ssl > return _realssl(sock, keyfile, certfile) > socket.sslerror: (1, 'error:14094412:SSL > routines:SSL3_READ_BYTES:sslv3 alert bad certificate') What SSL implementation are you using? The old one from Python 2.4 doesn't even check the certificate chain. M2Crypto does, and the new SSL module does, but in each case you have to provide a root certificate file. (The one from Mozilla is available.) John Nagle From nagle at animats.com Wed Jul 29 13:12:27 2009 From: nagle at animats.com (John Nagle) Date: Wed, 29 Jul 2009 10:12:27 -0700 Subject: SEC forms parsing In-Reply-To: References: Message-ID: <4a70824d$0$1600$742ec2ed@news.sonic.net> mpython wrote: > I am looking for any python examples that have written to parse SEC > filings (Security Exchange Commission's EDGAR system),or just a > pointer to best modules to use. I've been doing that in Perl for years. See "www.downside.com". Actually extracting financial statements is possible, but you run into a patent problem with a Price-Waterhouse patent if you try to do a good job. What are you trying to do? John Nagle From piet at cs.uu.nl Wed Jul 29 13:17:51 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 29 Jul 2009 19:17:51 +0200 Subject: Help with sql and python References: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> Message-ID: >>>>> "catalinfest at gmail.com" (cc) wrote: >cc> Hello ! >cc> I have accont on myphpadmin on my server. >cc> I want to create one script in python. >cc> This script manage sql task from my sql server . >cc> How i make this in a simple way ? See http://mysql-python.sourceforge.net/MySQLdb.html This really has nothing to do with myphpadmin (you probably mean phpMyAdmin but even then it has nothing to do with it except that both acess a MySQL database). And if you give yourself a name than we might have an idea if we are talking to a human being or a robot. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From tutufan at gmail.com Wed Jul 29 13:21:39 2009 From: tutufan at gmail.com (Mike) Date: Wed, 29 Jul 2009 10:21:39 -0700 (PDT) Subject: New implementation of re module References: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> Message-ID: <19a1cbfa-6594-4956-bc3c-f1fd3108091a@c29g2000yqd.googlegroups.com> On Jul 29, 10:45?am, MRAB wrote: > Mike wrote: > > - findall/finditer doesn't find overlapping matches. ?Sometimes you > > really *do* want to know all possible matches, even if they overlap. > > Perhaps by adding "overlapped=True"? Something like that would be great, yes. > > - split won't split on empty patterns, e.g. empty lookahead patterns. > Already addressed (see issue2636 for the full details). Glad to hear it. > > - Repeated subgroup match information is not available. ?That is, for > > a match like this > > > ? ? re.match('(.){3}', 'xyz') > > > there's no way to discover that the subgroup first matched 'x', then > > matched 'y', and finally matched 'z'. ?Here is one past proposal > > (mine), perhaps over-complex, to address this problem: > > > ? ?http://mail.python.org/pipermail/python-dev/2004-August/047238.html > > Yikes! I think I'll let you code that... :-) I agree that that document looks a little scary--maybe I was trying to bite off too much at once. My intuition, though, is that the basic idea should be fairly simple to implement, at least for a depth-first matcher. The repeated match subgroups are already being discovered, it's just that they're not being saved, so there's no way to report them out once a complete match is found. If some trail of breadcrumbs were pushed onto a stack during the DFS, it could be traced at the end. And the whole thing might not even been that expensive to do. The hardest parts about this, in my mind, are figuring out how to report the repeated matches out in a useful form (hence all that detail in the proposal), and getting users to understand that using this feature *could* suck up a lot of memory, if they're not careful. As always, it's possible that my intuition is totally wrong. Plus I'm not sure how this would work out in the breadth-first case. Details aside, I would really, really, really like to have a way to get at the repeated subgroup matches. I write a lot of code that would be one-liners if this capability existed. Plus, it just plain burns me that Python is discovering this information but impudently refuses to tell me what it's found! ;-) From piet at cs.uu.nl Wed Jul 29 13:26:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 29 Jul 2009 19:26:26 +0200 Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> <7d5brmF2a1alcU1@mid.uni-berlin.de> <2b5b00b8-f9ba-4d16-b950-681aa6fa010a@24g2000yqm.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >>> The Exact Problem is how to use the key containing by .pem file for >>> 'encryption' . can i print the contents of the .pem file I have already answered this question in a previous thread. It is not very helpful if you repeat asking the same or similar questions in different threads, especially if you don't take notice of the answers given. I will repeat it once here: load_pub_key requires a file name, not the contents of the file. So use rsa = M2Crypto.RSA.load_pub_key('my_key.public.pem') and leave the open line out. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From dandi.kain at gmail.com Wed Jul 29 13:59:55 2009 From: dandi.kain at gmail.com (dandi kain) Date: Wed, 29 Jul 2009 10:59:55 -0700 (PDT) Subject: Does underscore has any special built-in meaningin Python ? Message-ID: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Hello everybody, I have just started learning Python.I heard its simple so I pick a presentation [1] and tried to work on it.But when it comes to underscores leading and trailing an object I dont understand any.I look through the python manual also but that was not helping .I searched some forums and I still dont have a clear picture. What is the functionality of __ or _ , leading or trailing an object , class ot function ? Is it just a naming convention to note special functions and objects , or it really mean someting to Python ? Thanks ahead , [1] http://www.aleax.it/goo_py4prog.pdf From lemmath at gmail.com Wed Jul 29 14:04:49 2009 From: lemmath at gmail.com (Vincent Vega) Date: Wed, 29 Jul 2009 11:04:49 -0700 (PDT) Subject: Print value CLOB via cx_Oracle References: <6de8842b-f1c6-460e-a705-4f6dbd81492c@a26g2000yqn.googlegroups.com> Message-ID: <372c160b-eee2-46f3-913c-f75d84f45e11@c14g2000yqm.googlegroups.com> On 28 Lip, 20:02, Vincent Vega wrote: > Hi, > > I call function in Oracle database use cx_Oracle. > In standard I define: > > db ? ? ? = cx_Oracle.connect(username, password, tnsentry) > cursor ?= db.cursor() > > I create variable 'y' (result function 'fun_name') and call function > 'fun_name': > > y ? = cursor.var(cx_Oracle.CLOB) > cursor.callfunc(fun_name, cx_Oracle.CLOB, y) > > When I print variable 'y' I receive: > > > > How can I get value variable 'y'??? > How read ??? > > Vincent Vega > > Note: > Off course y.read() does not work, cause 'y'is CLOB not LOB. > (see datatypes table in site:http://www.oracle.com/technology/pub/articles/prez-python-queries.html) The solution is simple: print y.getvalue() (see: http://cx-oracle.sourceforge.net/html/variable.html) Thanks! :) Vincent Vega From davea at ieee.org Wed Jul 29 14:05:28 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:05:28 -0400 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> Message-ID: <4A708F68.1010505@ieee.org> Barak, Ron wrote: > Hi, > > I wanted to make a python byte-code file executable, expecting to be able to run it without specifying "python" on the (Linux bash) command line. > > So, I wrote the following: > > [root at VMLinux1 python]# cat test_pyc.py > #!/usr/bin/env python > > print "hello" > [root at VMLinux1 python]# > > and made its pyc file executable: > > [root at VMLinux1 python]# ls -ls test_pyc.pyc > 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > [root at VMLinux1 python]# > > So, I see: > > [root at VMLinux1 python]# file test_pyc.py* > test_pyc.py: a python script text executable > test_pyc.pyc: python 2.3 byte-compiled > [root at VMLinux1 python]# > > If I try to do the following, no problem: > > [root at VMLinux1 python]# python test_pyc.pyc > hello > [root at VMLinux1 python]# > > However, the following fails: > > [root at VMLinux1 python]# ./test_pyc.pyc > -bash: ./test_pyc.pyc: cannot execute binary file > [root at VMLinux1 python]# > > Is there a way to run a pyc file without specifying the python path ? > > Bye, > Ron. > > I don't currently run Unix, but I think I know the problem. In a text file, the shell examines the first line, and if it begins #! it's assumed to point to the executable of an interpreter for that text file. Presumably the same trick doesn't work for a .pyc file. Why not write a trivial wrapper.py file, don't compile it, and let that invoke the main code in the .pyc file? Then make wrapper.py executable, and you're ready to go. DaveA From benjamin.kaplan at case.edu Wed Jul 29 14:08:07 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 29 Jul 2009 14:08:07 -0400 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: On Wed, Jul 29, 2009 at 1:59 PM, dandi kain wrote: > > Hello everybody, > I have just started learning Python.I heard its simple so I pick a > presentation [1] and tried to work on it.But when it comes to > underscores leading and trailing an object I dont understand any.I > look through the python manual also but that was not helping .I > searched some forums and I still dont have a clear picture. > > What is the functionality of __ or _ , leading or trailing an object , > class ot function ? Is it just a naming convention to note special > functions and objects , or it really mean someting to Python ? It's just a convention for the most part. A single leading underscore is used for "private" attributes. Two leading underscores will affect the code- it mangles the variable name so that you don't have to worry about the value being overwritten by a subclass. For instance """ class Foo(object) : def __init__(self) : self.__bar = '' foo = Foo() "" will store the attribute as foo._Foo__bar. Also, the "magic methods"- the ones that are used for operations and built-in stuff, all have two leading and two trailing underscores. These are things like __add__ (+), __eq__ (=), __cmp__ (old way for comparisons), __len__ (len), __str__ (str), and so on. > > Thanks ahead , > > [1] http://www.aleax.it/goo_py4prog.pdf > -- > http://mail.python.org/mailman/listinfo/python-list From martin.hellwig at dcuktec.org Wed Jul 29 14:16:31 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Wed, 29 Jul 2009 19:16:31 +0100 Subject: simple splash screen? In-Reply-To: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: NighterNet wrote: > I am trying to make a simple splash screen from python 3.1.Not sure > where to start looking for it. Can any one help? Sure, almost the same as with Python 2 :-) But to be a bit more specific: ++++ """Only works if you got Python 3 installed with tkinter""" import tkinter IMAGE_PATH = "/path/to/image" class Splash(object): "Splash Screen GUI" def __init__(self, root): self.root = root # No window borders and decoration self.root.overrideredirect(True) # Get the size of the screen screen_width = self.root.winfo_screenwidth() screen_height = self.root.winfo_screenheight() # Full screen geometry_text = "%dx%d+0+0" % (screen_width, screen_height) self.root.geometry(geometry_text) # Display an image self.label = tkinter.Label(self.root) # Only GIF and PGM/PPM supported, for more information see: self.label._image = tkinter.PhotoImage(file=IMAGE_PATH) # http://effbot.org/tkinterbook/photoimage.htm self.label.configure(image = self.label._image) self.label.pack() # This will quit the screen after about 5 seconds self.root.after(5000, self.root.quit) if __name__ == '__main__': ROOT = tkinter.Tk() APPL = Splash(ROOT) ROOT.mainloop() ++++ -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From davea at ieee.org Wed Jul 29 14:19:32 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:19:32 -0400 Subject: Help with sql and python In-Reply-To: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> References: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> Message-ID: <4A7092B4.8090609@ieee.org> catalinfest at gmail.com wrote: > Hello ! > > I have accont on myphpadmin on my server. > I want to create one script in python. > This script manage sql task from my sql server . > How i make this in a simple way ? > > Thank you ! > > It's seldom simple. I'm guessing it's not your server, but is actually a webserver, located remotely, and to which you have only ftp access. Next questions are do you have access to the admins of the machine, what kind of machine is it, what OS is it running, does it have Python installed, what version(s)? Next questions are dependent on the answers to those questions. For example, on Unix systems you'll generally need to be able to chmod the file. But before you ask your admins those questions, you need to ask how you *want* to run the script. Creating it isn't enough, if you don't run it. You might be running it over the web, via URL, perhaps using CGI. You might be activating it via some remote console, or rsh. Or it might be a cron job. It might need various privileges to do what you expect. And if it's going to run continuously, or if it uses lots of resources, the admins may refuse it entirely. DaveA From electriclightheads at gmail.com Wed Jul 29 14:26:47 2009 From: electriclightheads at gmail.com ('2+) Date: Thu, 30 Jul 2009 03:26:47 +0900 Subject: instead of depending on data = array('h') .. write samples 1 by 1 to w = wave.open("wav.wav", "w") In-Reply-To: References: Message-ID: <1078018b0907291126t32c453a7h1f2a86fc270d3701@mail.gmail.com> o wow .. there's still a lot to learn .. okay .. if i get stucked with the memory usage issue will try this hybrid .. thanx for the example!! it took about 40 min to render 1min of wav so i'll just keep this project like 15 sec oriented and maybe that'll keep me away from the mem trouble and my oil.py is still so cheap.. sad thing is that "the result sound strange" is true to all versions :s thanx again anyway! On Wed, Jul 29, 2009 at 5:21 PM, Peter Otten<__peter__ at web.de> wrote: > '2+ wrote: > >> it says >> Wave_write.writeframes(data) >> will that mean >> "from array import array" >> is a must? >> >> this does the job: >> >> import oil >> import wave >> from array import array >> >> a = oil.Sa() >> >> w = wave.open("current.wav", "w") >> w.setnchannels(2) >> w.setsampwidth(2) >> w.setframerate(44100) >> >> data = array('h') >> >> for gas in range(44100 * 5): >> ? ? a.breath() >> ? ? r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) >> ? ? l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) >> ? ? data.append(r) >> ? ? data.append(l) >> >> w.writeframes(data.tostring()) >> w.close() >> >> don't like array becoming so huge so tested this and it was also okay: >> >> for gas in range(44100 * 5): >> ? ? a.breath() >> ? ? data = array('h') >> ? ? r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) >> ? ? l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) >> ? ? data.append(r) >> ? ? data.append(l) >> ? ? w.writeframes(data.tostring()) >> >> but without array .. it becomes 15secs(3 times longer than was >> intended to be) of wav file: >> >> for gas in range(44100 * 5): >> ? ? a.breath() >> ? ? r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) >> ? ? l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) >> ? ? w.writeframes(hex(r)) >> ? ? w.writeframes(hex(l)) > > Doesn't it sound strange, too? You are writing bogus bytes to the file. > Compare: > >>>> import array >>>> array.array("h", [42]).tostring() > '*\x00' >>>> hex(42) > '0x2a' > > Not only do they differ in length, the contents are different, too. If > you're unfamiliar with string escape codes, here's a way to see the bytes: > >>>> map(ord, array.array("h", [42]).tostring()) > [42, 0] >>>> map(ord, hex(42)) > [48, 120, 50, 97] > >> should i just be happy with depennding on using array? >> or is there a solution to make the last one work properly? > > There is a way to make the last one work: use struct.pack("h", r) instead of > hex(r). I'm of course assuming that the first version does give you the > desired result. You should not continue before you have verified that. > > I still recommend array for performance reasons. If memory usage is an issue > you can adopt a hybrid approach: > > # untested > from itertools import islice > from array import array > > def gen_data(): > ? ?for gas in xrange(44100 * 5): # range --> xrange > ? ? ? ?a.breath() > ? ? ? ?r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > ? ? ? ?l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > ? ? ? ?yield r > ? ? ? ?yield l > > data = gen_data() > N = 2**20 > while True: > ? ?chunk = array('h') > ? ?chunk.extend(islice(data, N)) > ? ?if not chunk: > ? ? ? ?break > ? ?w.writeframes(chunk.tostring()) > > This will limit the array size to 4N bytes. > > Peter > > -- > http://mail.python.org/mailman/listinfo/python-list > -- SaRiGaMa's Oil Vending Orchestra is podcasting: http://sarigama.namaste.jp/podcast/rss.xml and supplying oil.py for free: http://oilpy.blogspot.com/ From davea at ieee.org Wed Jul 29 14:33:25 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:33:25 -0400 Subject: escaping characters in filenames In-Reply-To: <854osv7h2k.fsf@agentultra.com> References: <854osv7h2k.fsf@agentultra.com> Message-ID: <4A7095F5.6090100@ieee.org> J Kenneth King wrote: > I wrote a script to process some files using another program. One thing > I noticed was that both os.listdir() and os.path.walk() will return > unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ > File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a > module or recipe that escapes file names and was wondering if anyone > could point me in the right direction. > > As an aside, the script is using subprocess.call() with the "shell=True" > parameter. There isn't really a reason for doing it this way (was just > the fastest way to write it and get a prototype working). I was > wondering if Popen objects were sensitive to unescaped names like the > shell. I intend to refactor the function to use Popen objects at some > point and thought perhaps escaping file names may not be entirely > necessary. > > Cheers > > There are dozens of meanings for escaping characters in strings. Without some context, we're wasting our time. For example, if the filename is to be interpreted as part of a URL, then spaces are escaped by using %20. Exactly who is going to be using this string you think you have to modify? I don't know of any environment which expects spaces to be escaped with backslashes. Be very specific. For example, if a Windows application is parsing its own command line, you need to know what that particular application is expecting -- Windows passes the entire command line as a single string. But of course you may be invoking that application using subprocess.Popen(), in which case some transformations happen to your arguments before the single string is built. Then some more transformations may happen in the shell. Then some more in the C runtime library of the new process (if it happens to be in C, and if it happens to use those libraries). I'm probably not the one with the answer. But until you narrow down your case, you probably won't attract the attention of whichever person has the particular combination of experience that you're hoping for. DaveA From jdgiotta at gmail.com Wed Jul 29 14:39:55 2009 From: jdgiotta at gmail.com (John D Giotta) Date: Wed, 29 Jul 2009 11:39:55 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: I'm working with up to 3 process "session" per server, each process running three threads. I was wishing to tie back the 3 "session"/server to a semaphore, but everything (and everyone) say semaphores are only good per process. From jdgiotta at gmail.com Wed Jul 29 14:43:07 2009 From: jdgiotta at gmail.com (John D Giotta) Date: Wed, 29 Jul 2009 11:43:07 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: <171f69c0-44ae-4624-bd03-a47032e82c21@n11g2000yqb.googlegroups.com> That was my original idea. Restricting each process by pid: #bash procs=`ps aux | grep script.pl | grep -v grep | wc -l` if [ $procs -lt 3 ]; then python2.4 script.py config.xml else exit 0 fi From lists at cheimes.de Wed Jul 29 14:53:49 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 29 Jul 2009 20:53:49 +0200 Subject: Semaphore Techniques In-Reply-To: References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: John D Giotta schrieb: > I'm working with up to 3 process "session" per server, each process > running three threads. > I was wishing to tie back the 3 "session"/server to a semaphore, but > everything (and everyone) say semaphores are only good per process. That's not true. Named semaphores are the best solution for your problem and I said so yesterday. Christian From nobody at nowhere.com Wed Jul 29 14:55:16 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 19:55:16 +0100 Subject: escaping characters in filenames References: <854osv7h2k.fsf@agentultra.com> Message-ID: On Wed, 29 Jul 2009 09:29:55 -0400, J Kenneth King wrote: > I wrote a script to process some files using another program. One thing > I noticed was that both os.listdir() and os.path.walk() will return > unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ > File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a > module or recipe that escapes file names and was wondering if anyone > could point me in the right direction. > > As an aside, the script is using subprocess.call() with the "shell=True" > parameter. There isn't really a reason for doing it this way (was just > the fastest way to write it and get a prototype working). I was > wondering if Popen objects were sensitive to unescaped names like the > shell. I intend to refactor the function to use Popen objects at some > point and thought perhaps escaping file names may not be entirely > necessary. Note that subprocess.call() is nothing more than: def call(*popenargs, **kwargs): return Popen(*popenargs, **kwargs).wait() plus a docstring. It accepts exactly the same arguments as Popen(), with the same semantics. If you want to run a command given a program and arguments, you should pass the command and arguments as a list, rather than trying to construct a string. On Windows the value of shell= is unrelated to whether the command is a list or a string; a list is always converted to string using the list2cmdline() function. Using shell=True simply prepends "cmd.exe /c " to the string (this allows you to omit the .exe/.bat/etc extension for extensions which are in %PATHEXT%). On Unix, a string is first converted to a single-element list, so if you use a string with shell=False, it will be treated as the name of an executable to be run without arguments, even if contains spaces, shell metacharacters etc. The most portable approach seems to be to always pass the command as a list, and to set shell=True on Windows and shell=False on Unix. The only reason to pass a command as a string is if you're getting a string from the user and you want it to be interpreted using the platform's standard shell (i.e. cmd.exe or /bin/sh). If you want it to be interpreted the same way regardless of platform, parse it into a list using shlex.split(). From mdekauwe at gmail.com Wed Jul 29 14:56:28 2009 From: mdekauwe at gmail.com (Martin) Date: Wed, 29 Jul 2009 11:56:28 -0700 (PDT) Subject: set variable to looping index? Message-ID: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Hi, I am trying to set the return value from a function to a name which I grab from the for loop. I can't work out how I can do this without using an if statement... for f in var1_fn, var2_fn, var3_fn: if f.split('.')[0] == 'var1': var1 = call_some_function(f) . . . etc Really I would like to remove the need for this if loop and I am sure there is a simple way I am missing? Many thanks Martin From davea at ieee.org Wed Jul 29 14:56:55 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:56:55 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Message-ID: <4A709B77.9080209@ieee.org> Omer Khalid wrote: > Hi, > > I am having a very strange problem with modifying a variable in a list in my > program. Here is the code: > > # a list that contains dictionary objects > jobs = [] > > index=5 > for each in range(index): > jobs.append({'v':0}) > > some_function(index): > if jobs[index]['v'] == 0: > # set it to 1 > jobs[index]['v'] = 1 > print "Set to 1" > else: > print "Already set to 1" > > loop(): > index=0 > for each in range(len(jobs)): > some_function(index) > index +=1 > > > Apparently, the jobs[index]['v'] never get updated in the some_function but > the print statement afterwards get printed... > > What's really surprising is that there are no errors or exceptions and my my > program runs in a single thread...so i have been unable to explain this > behavior. > > Any insight would be much appreciated! > > Cheers > Omer > > There are four things to fix before the program does anything much at all. Two places you're missing the def, indentation is inconsistent, and you never actually call either of the functions. The first three are syntax errors, so presumably your cut/paste in your computer is broken. Once I make those four corrections, I get the following output: Set to 1 Set to 1 Set to 1 Set to 1 Set to 1 But you never said what you got, nor what you expected. That's certainly what I'd expect. And if you make a second call to loop() in your outer code, you get five copies of "Already set to 1" BTW, there are a number of things that could be done better. The main one I'll point out is that you shouldn't re-use a global variable 'index' as a local with different meaning. As someone else pointed out, since the global is a constant, making it all uppercase is the convention. DaveA From martin at v.loewis.de Wed Jul 29 15:06:31 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Wed, 29 Jul 2009 21:06:31 +0200 Subject: 64-bit issues with dictionaries in Python 2.6 In-Reply-To: References: Message-ID: <4a709db7$0$15886$9b622d9e@news.freenet.de> > Are there any known issues with dictionaries in Python 2.6 (not 2.6.2) > when running on a 64-bit platform? No, none. Regards, Martin From Omer.Khalid at cern.ch Wed Jul 29 15:10:44 2009 From: Omer.Khalid at cern.ch (Omer Khalid) Date: Wed, 29 Jul 2009 21:10:44 +0200 Subject: Very Strange Problem In-Reply-To: <4A709B77.9080209@ieee.org> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A709B77.9080209@ieee.org> Message-ID: <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> Hi Dave, Thanks for your reply. I actually didn't cut and paste my code as it was dispersed in different places, i typed the logic behind my code in the email (and obiviously made some typos, indentations is some thing else) but my real code does not have these problems as my application runs fine with out errors... Except that the line where i want to update the value doesn't get updated and no exception is thrown. What's surprising for me is that i am doing the same thing in hundreds of places in my 3k+ line code but by some reason this part doesn't work... As far as the global variables are concerned, i am using them in other places too and didn't see any problems. I think some thing else is going on here as the statement above and below my modified lines get executed. Is there a way in Python to debug memory address or to see where in memory this object is stored, and is there a lock on it or else? Thanks, Omer ************************************** On Wed, Jul 29, 2009 at 8:56 PM, Dave Angel wrote: > Omer Khalid wrote: > >> Hi, >> >> I am having a very strange problem with modifying a variable in a list in >> my >> program. Here is the code: >> >> # a list that contains dictionary objects >> jobs = [] >> >> index=5 >> for each in range(index): >> jobs.append({'v':0}) >> >> some_function(index): >> if jobs[index]['v'] == 0: >> # set it to 1 >> jobs[index]['v'] = 1 >> print "Set to 1" >> else: >> print "Already set to 1" >> >> loop(): >> index=0 >> for each in range(len(jobs)): >> some_function(index) >> index +=1 >> >> >> Apparently, the jobs[index]['v'] never get updated in the some_function >> but >> the print statement afterwards get printed... >> >> What's really surprising is that there are no errors or exceptions and my >> my >> program runs in a single thread...so i have been unable to explain this >> behavior. >> >> Any insight would be much appreciated! >> >> Cheers >> Omer >> >> >> > There are four things to fix before the program does anything much at all. > Two places you're missing the def, indentation is inconsistent, and you > never actually call either of the functions. The first three are syntax > errors, so presumably your cut/paste in your computer is broken. > > Once I make those four corrections, I get the following output: > > Set to 1 > Set to 1 > Set to 1 > Set to 1 > Set to 1 > > But you never said what you got, nor what you expected. That's certainly > what I'd expect. And if you make a second call to loop() in your outer > code, you get five copies of "Already set to 1" > > BTW, there are a number of things that could be done better. The main one > I'll point out is that you shouldn't re-use a global variable 'index' as a > local with different meaning. As someone else pointed out, since the global > is a constant, making it all uppercase is the convention. > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From darkneter at gmail.com Wed Jul 29 15:32:10 2009 From: darkneter at gmail.com (NighterNet) Date: Wed, 29 Jul 2009 12:32:10 -0700 (PDT) Subject: simple splash screen? References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: <79954da5-a437-48a1-985b-88abe601d021@a37g2000prf.googlegroups.com> On Jul 29, 11:16?am, "Martin P. Hellwig" wrote: > NighterNet wrote: > > I am trying to make a simple splash screen from python 3.1.Not sure > > where to start looking for it. Can any one help? > > Sure, almost the same as with Python 2 :-) > But to be a bit more specific: > ++++ > """Only works if you got Python 3 installed with tkinter""" > import tkinter > > IMAGE_PATH = "/path/to/image" > > class Splash(object): > ? ? ?"Splash Screen GUI" > ? ? ?def __init__(self, root): > ? ? ? ? ?self.root = root > ? ? ? ? ?# No window borders and decoration > ? ? ? ? ?self.root.overrideredirect(True) > ? ? ? ? ?# Get the size of the screen > ? ? ? ? ?screen_width = self.root.winfo_screenwidth() > ? ? ? ? ?screen_height = self.root.winfo_screenheight() > ? ? ? ? ?# Full screen > ? ? ? ? ?geometry_text = "%dx%d+0+0" % (screen_width, screen_height) > ? ? ? ? ?self.root.geometry(geometry_text) > ? ? ? ? ?# Display an image > ? ? ? ? ?self.label = tkinter.Label(self.root) > ? ? ? ? ?# Only GIF and PGM/PPM supported, for more information see: > ? ? ? ? ?self.label._image = tkinter.PhotoImage(file=IMAGE_PATH) > ? ? ? ? ?#http://effbot.org/tkinterbook/photoimage.htm > ? ? ? ? ?self.label.configure(image = self.label._image) > ? ? ? ? ?self.label.pack() > ? ? ? ? ?# This will quit the screen after about 5 seconds > ? ? ? ? ?self.root.after(5000, self.root.quit) > > if __name__ == '__main__': > ? ? ?ROOT = tkinter.Tk() > ? ? ?APPL = Splash(ROOT) > ? ? ?ROOT.mainloop() > ++++ > > -- > MPHhttp://blog.dcuktec.com > 'If consumed, best digested with added seasoning to own preference.' Thanks it help. Sorry about that, I was just wander what kind of answer and if there are other methods to learn it. Is there a way to position image to the center screen? From tjreedy at udel.edu Wed Jul 29 15:41:34 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 29 Jul 2009 15:41:34 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Message-ID: Omer Khalid wrote: > Hi, > > I am having a very strange problem with modifying a variable in a list > in my program. Here is the code: To me, this sentence clearly implies that the code that follows is the code that had the problem. Since the posted code cannot run, it clearly is not. People should test code to be posted before posting unless they clearly label it as 'untested'. Original posters, of course, should run the code first. tjr From davea at ieee.org Wed Jul 29 15:42:39 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 15:42:39 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A709B77.9080209@ieee.org> <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> Message-ID: <4A70A62F.6080403@ieee.org> Omer Khalid wrote: > Hi Dave, > > Thanks for your reply. I actually didn't cut and paste my code as it was > dispersed in different places, i typed the logic behind my code in the email > (and obiviously made some typos, indentations is some thing else) but my > real code does not have these problems as my application runs fine with out > errors... > > Except that the line where i want to update the value doesn't get updated > and no exception is thrown. What's surprising for me is that i am doing the > same thing in hundreds of places in my 3k+ line code but by some reason this > part doesn't work... > > As far as the global variables are concerned, i am using them in other > places too and didn't see any problems. > > I think some thing else is going on here as the statement above and below my > modified lines get executed. > > Is there a way in Python to debug memory address or to see where in memory > this object is stored, and is there a lock on it or else? > > Thanks, > Omer > > > ************************************** > > > On Wed, Jul 29, 2009 at 8:56 PM, Dave Angel wrote: > > >> Omer Khalid wrote: >> >> >>> Hi, >>> >>> I am having a very strange problem with modifying a variable in a list in >>> my >>> program. Here is the code: >>> >>> # a list that contains dictionary objects >>> jobs = [] >>> >>> index=5 >>> for each in range(index): >>> jobs.append({'v':0}) >>> >>> some_function(index): >>> if jobs[index]['v'] == 0: >>> # set it to 1 >>> jobs[index]['v'] = 1 >>> print "Set to 1" >>> else: >>> print "Already set to 1" >>> >>> loop(): >>> index=0 >>> for each in range(len(jobs)): >>> some_function(index) >>> index +=1 >>> >>> >>> Apparently, the jobs[index]['v'] never get updated in the some_function >>> but >>> the print statement afterwards get printed... >>> >>> What's really surprising is that there are no errors or exceptions and my >>> my >>> program runs in a single thread...so i have been unable to explain this >>> behavior. >>> >>> Any insight would be much appreciated! >>> >>> Cheers >>> Omer >>> >>> >>> >>> >> There are four things to fix before the program does anything much at all. >> Two places you're missing the def, indentation is inconsistent, and you >> never actually call either of the functions. The first three are syntax >> errors, so presumably your cut/paste in your computer is broken. >> >> Once I make those four corrections, I get the following output: >> >> Set to 1 >> Set to 1 >> Set to 1 >> Set to 1 >> Set to 1 >> >> But you never said what you got, nor what you expected. That's certainly >> what I'd expect. And if you make a second call to loop() in your outer >> code, you get five copies of "Already set to 1" >> >> BTW, there are a number of things that could be done better. The main one >> I'll point out is that you shouldn't re-use a global variable 'index' as a >> local with different meaning. As someone else pointed out, since the global >> is a constant, making it all uppercase is the convention. >> >> DaveA >> >> (You top-posted, so your ,message is out of sequence. More and more people are doing that in this list.) ++++ ...Except that the line where i want to update the value doesn't get updated... And what makes you think that? You never answered my question. What did you expect for output, and what did you get? I got exactly what I expected, when I ran it. ++++ ... Is there a way in Python to debug memory address or ++++ to see where in memory this object is stored, and ++++ is there a lock on it or else? If there really were a bug in the language, you might need such a tool. I use Komodo IDE as a debugger, but there was no need in this case. Adding a few print statements might clear up your confusion, but since you haven't spelled out what it is, I can't suggest where. How about if you just add a print jobs at the beginning of some_function() ? Then you could see things getting updated perfectly. DaveA From zuo at chopin.edu.pl Wed Jul 29 15:54:51 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Wed, 29 Jul 2009 21:54:51 +0200 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: 29-07-2009 Benjamin Kaplan wrote: > On Wed, Jul 29, 2009 at 1:59 PM, dandi kain wrote: [snip >> What is the functionality of __ or _ , leading or trailing an object , >> class ot function ? Is it just a naming convention to note special >> functions and objects , or it really mean someting to Python ? > > It's just a convention for the most part. A single leading underscore > is used for "private" attributes. Two leading underscores will affect > the code- Single leading underscore in some situations also affect the code... See: * http://docs.python.org/reference/lexical_analysis.html#reserved-classes-of-identifiers * http://docs.python.org/reference/datamodel.html#object.__del__ (in the the red "Warning" frame) -- Jan Kaliszewski (zuo) From tjreedy at udel.edu Wed Jul 29 15:57:25 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 29 Jul 2009 15:57:25 -0400 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: Benjamin Kaplan wrote: > On Wed, Jul 29, 2009 at 1:59 PM, dandi kain wrote: >> Hello everybody, >> I have just started learning Python.I heard its simple so I pick a >> presentation [1] and tried to work on it.But when it comes to >> underscores leading and trailing an object I dont understand any.I >> look through the python manual also but that was not helping .I >> searched some forums and I still dont have a clear picture. >> >> What is the functionality of __ or _ , leading or trailing an object , >> class ot function ? Is it just a naming convention to note special >> functions and objects , or it really mean someting to Python ? > > It's just a convention for the most part. A single leading underscore > is used for "private" attributes. Two leading underscores will affect > the code- it mangles the variable name so that you don't have to worry > about the value being overwritten by a subclass. For instance > """ > class Foo(object) : > def __init__(self) : > self.__bar = '' > > foo = Foo() > "" > will store the attribute as foo._Foo__bar. > > Also, the "magic methods"- the ones that are used for operations and > built-in stuff, all have two leading and two trailing underscores. > These are things like __add__ (+), __eq__ (=), __cmp__ (old way for > comparisons), __len__ (len), __str__ (str), and so on. For this last, see http://docs.python.org/dev/py3k/reference/datamodel.html#special-method-names From no.email at please.post Wed Jul 29 16:05:36 2009 From: no.email at please.post (kj) Date: Wed, 29 Jul 2009 20:05:36 +0000 (UTC) Subject: How to "gunzip-iterate" over a file? Message-ID: I need to iterate over the lines of *very* large (>1 GB) gzipped files. I would like to do this without having to read the full compressed contents into memory so that I can apply zlib.decompress to these contents. I also would like to avoid having to gunzip the file (i.e. creating an uncompressed version of the file in the filesystem) prior to iterating over it. Basically I'm looking for something that will give me the same functionality as Perl's gzip IO layer, which looks like this (from the documentation): use PerlIO::gzip; open FOO, "<:gzip", "file.gz" or die $!; print while ; # And it will be uncompressed... What's the best way to achieve the same functionality in Python? TIA! kynn From malc0de.encrypt at gmail.com Wed Jul 29 16:21:56 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Wed, 29 Jul 2009 13:21:56 -0700 (PDT) Subject: Does python have the capability for driver development ? Message-ID: hello there, I've a question : I want to know does python have any capability for using Ring0 and kernel functions for driver and device development stuff . if there's such a feature it is very good, and if there something for this kind that you know please refer me to some reference and show me some snippet . thanks - Malc0de From robert.kern at gmail.com Wed Jul 29 16:22:43 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 29 Jul 2009 15:22:43 -0500 Subject: How to "gunzip-iterate" over a file? In-Reply-To: References: Message-ID: On 2009-07-29 15:05, kj wrote: > > > I need to iterate over the lines of *very* large (>1 GB) gzipped > files. I would like to do this without having to read the full > compressed contents into memory so that I can apply zlib.decompress > to these contents. I also would like to avoid having to gunzip > the file (i.e. creating an uncompressed version of the file in the > filesystem) prior to iterating over it. > > Basically I'm looking for something that will give me the same > functionality as Perl's gzip IO layer, which looks like this (from > the documentation): > > use PerlIO::gzip; > open FOO, "<:gzip", "file.gz" or die $!; > print while; # And it will be uncompressed... > > What's the best way to achieve the same functionality in Python? http://docs.python.org/library/gzip import gzip f = gzip.open('filename.gz') for line in f: print line f.close() -- 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 npuloski at gmail.com Wed Jul 29 16:23:33 2009 From: npuloski at gmail.com (Nanime Puloski) Date: Wed, 29 Jul 2009 16:23:33 -0400 Subject: Differences Between Arrays and Matrices in Numpy Message-ID: What are some differences between arrays and matrices using the Numpy library? When would I want to use arrays instead of matrices and vice versa? From pfeldman at verizon.net Wed Jul 29 16:26:23 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Wed, 29 Jul 2009 13:26:23 -0700 (PDT) Subject: delayed sys.exit? Message-ID: <24726902.post@talk.nabble.com> In the attached http://www.nabble.com/file/p24726902/test.py test.py code, it appears that additional statements execute after the call to sys.exit(0). I'll be grateful if anyone can shed light on why this is happening. Below is a copy of some sample I/O. Note that in the last case I get additional output after what should be the final error message. In [126]: run test Input a string: 1,2 [1, 2] In [127]: run test Input a string: 1-3 [1, 2, 3] In [128]: run test Input a string: 1,4,5-12 [1, 4, 5, 6, 7, 8, 9, 10, 11, 12] In [129]: run test Input a string: 0 ERROR: 0 is invalid; run numbers must be positive. ERROR: '0' is not a valid run number. -- View this message in context: http://www.nabble.com/delayed-sys.exit--tp24726902p24726902.html Sent from the Python - python-list mailing list archive at Nabble.com. From james at agentultra.com Wed Jul 29 16:33:11 2009 From: james at agentultra.com (J Kenneth King) Date: Wed, 29 Jul 2009 16:33:11 -0400 Subject: escaping characters in filenames References: <854osv7h2k.fsf@agentultra.com> Message-ID: <85ws5r5iwo.fsf@agentultra.com> Nobody writes: > On Wed, 29 Jul 2009 09:29:55 -0400, J Kenneth King wrote: > >> I wrote a script to process some files using another program. One thing >> I noticed was that both os.listdir() and os.path.walk() will return >> unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ >> File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a >> module or recipe that escapes file names and was wondering if anyone >> could point me in the right direction. >> >> As an aside, the script is using subprocess.call() with the "shell=True" >> parameter. There isn't really a reason for doing it this way (was just >> the fastest way to write it and get a prototype working). I was >> wondering if Popen objects were sensitive to unescaped names like the >> shell. I intend to refactor the function to use Popen objects at some >> point and thought perhaps escaping file names may not be entirely >> necessary. > > Note that subprocess.call() is nothing more than: > > def call(*popenargs, **kwargs): > return Popen(*popenargs, **kwargs).wait() > > plus a docstring. It accepts exactly the same arguments as Popen(), with > the same semantics. > > If you want to run a command given a program and arguments, you > should pass the command and arguments as a list, rather than trying to > construct a string. > > On Windows the value of shell= is unrelated to whether the command is > a list or a string; a list is always converted to string using the > list2cmdline() function. Using shell=True simply prepends "cmd.exe /c " to > the string (this allows you to omit the .exe/.bat/etc extension for > extensions which are in %PATHEXT%). > > On Unix, a string is first converted to a single-element list, so if you > use a string with shell=False, it will be treated as the name of an > executable to be run without arguments, even if contains spaces, shell > metacharacters etc. > > The most portable approach seems to be to always pass the command as a > list, and to set shell=True on Windows and shell=False on Unix. > > The only reason to pass a command as a string is if you're getting a > string from the user and you want it to be interpreted using the > platform's standard shell (i.e. cmd.exe or /bin/sh). If you want it to be > interpreted the same way regardless of platform, parse it into a > list using shlex.split(). I understand; I think I was headed towards subprocess.Popen() either way. It seems to handle the problem I posted about. And I got to learn a little something on the way. Thanks! Only now there's a new problem in that the output of the program is different if I run it from Popen than if I run it from the command line. The program in question is 'pdftotext'. More investigation to ensue. Thanks again for the helpful post. From robert.kern at gmail.com Wed Jul 29 16:36:03 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 29 Jul 2009 15:36:03 -0500 Subject: Differences Between Arrays and Matrices in Numpy In-Reply-To: References: Message-ID: On 2009-07-29 15:23, Nanime Puloski wrote: > What are some differences between arrays and matrices using the Numpy > library? When would I want to use arrays instead of matrices and vice > versa? You will want to ask numpy questions on the numpy mailing list: http://www.scipy.org/Mailing_Lists An overview of how the matrix subclass differs from ndarray, see the documentation: http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects Basically, I suggest that you just use regular arrays always. There is a syntactical convenience to matrix objects, but it does cause incompatibilities with the majority of code that is written for regular arrays. The convenience is usually not worth the cost. -- 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 apt.shansen at gmail.com Wed Jul 29 16:36:32 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Wed, 29 Jul 2009 13:36:32 -0700 Subject: delayed sys.exit? In-Reply-To: <24726902.post@talk.nabble.com> References: <24726902.post@talk.nabble.com> Message-ID: <7a9c25c20907291336k5b4686c9sb410710967307907@mail.gmail.com> > > In the attached http://www.nabble.com/file/p24726902/test.py test.py > code, > it appears that additional statements execute after the call to > sys.exit(0). > I'll be grateful if anyone can shed light on why this is happening. Below > is a copy of some sample I/O. Note that in the last case I get additional > output after what should be the final error message. > A bare "except:" catches ALL exceptions; including SystemExit which is generated by sys.exit. And KeyboardInterrupt, too. That's why its basically a bad idea to use bare excepts unless you really, really, really need to. Try 'except Exception' instead. SystemExit and such do not inherit from Exception. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Jul 29 16:40:13 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jul 2009 22:40:13 +0200 Subject: delayed sys.exit? References: Message-ID: Dr. Phillip M. Feldman wrote: > In the attached http://www.nabble.com/file/p24726902/test.py test.py > code, it appears that additional statements execute after the call to > sys.exit(0). > try: > # If the conversion to int fails, nothing is appended to the list: > Runs.append(int(strs[i])) > if Runs[-1] <= 0: > print 'ERROR: ' + str(Runs[-i]) + \ > ' is invalid; run numbers must be positive.' > sys.exit(0) > except: sys.exit() works by raising a SystemExit exception which is caught by the bare except. http://docs.python.org/library/sys.html#sys.exit http://docs.python.org/library/exceptions.html#exceptions.SystemExit As a general rule try to be as specific as possible when catching exceptions. Peter From python at mrabarnett.plus.com Wed Jul 29 16:41:37 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 21:41:37 +0100 Subject: delayed sys.exit? In-Reply-To: <24726902.post@talk.nabble.com> References: <24726902.post@talk.nabble.com> Message-ID: <4A70B401.9040700@mrabarnett.plus.com> Dr. Phillip M. Feldman wrote: > In the attached http://www.nabble.com/file/p24726902/test.py test.py code, > it appears that additional statements execute after the call to sys.exit(0). > I'll be grateful if anyone can shed light on why this is happening. Below > is a copy of some sample I/O. Note that in the last case I get additional > output after what should be the final error message. > > In [126]: run test > Input a string: 1,2 > [1, 2] > > In [127]: run test > Input a string: 1-3 > [1, 2, 3] > > In [128]: run test > Input a string: 1,4,5-12 > [1, 4, 5, 6, 7, 8, 9, 10, 11, 12] > > In [129]: run test > Input a string: 0 > ERROR: 0 is invalid; run numbers must be positive. > ERROR: '0' is not a valid run number. > sys.exit raises an SystemExit exception, which then gets caught by the bare 'except' of the enclosing try...except... statement. A lot of things can raise an exception, which is why bare 'except's are a bad idea; catch only those you expect. From Russ.Davis at njpines.state.nj.us Wed Jul 29 16:55:27 2009 From: Russ.Davis at njpines.state.nj.us (Russ Davis) Date: Wed, 29 Jul 2009 16:55:27 -0400 Subject: IDLE Config Problems Message-ID: <4A70801E.DEDA.009D.0@njpines.state.nj.us> I am just getting started with Python and have installed v. 2.5.4 Idle version 1.2.4 I can't seem to get the idle to display text. It seems as if the install went fine. I start up the idle and the screen is blank. No text. It seems as if I can type on the screen but I just can't see the characters. I go to the config menu and it bombs and brings up the Visual Studio debugger. The computer that I am trying to install it on is a windows xp laptop serv pack 2. I have a workstation here that I am using also with the same os and the idle works fine. Any hints as to what might be interfering with the idle config. Thanks Russ Russell Davis PP, AICP, GISP GIS Administrator State of New Jersey Pinelands Commission Office of Land Use and Technology GIS Laboratory Po Box 7 New Lisbon, NJ 08064 Phone 609-894-7300 Fax 609-894-7330 russ.davis at njpines.state.nj.us From __peter__ at web.de Wed Jul 29 17:06:30 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jul 2009 23:06:30 +0200 Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: Martin wrote: > I am trying to set the return value from a function to a name which I > grab from the for loop. I can't work out how I can do this without > using an if statement... > > for f in var1_fn, var2_fn, var3_fn: > if f.split('.')[0] == 'var1': > var1 = call_some_function(f) > . > . > . > etc > > Really I would like to remove the need for this if loop and I am sure > there is a simple way I am missing? Use dictionaries: functions = {"var1": some_function, "var2": some_other_function, ...} return_values = {} for arg in var1_fn, var2_fn, var3_fn: key = arg.split(".")[0] return_values[key] = functions[key](arg) Peter From martin.hellwig at dcuktec.org Wed Jul 29 17:08:23 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Wed, 29 Jul 2009 22:08:23 +0100 Subject: simple splash screen? In-Reply-To: <79954da5-a437-48a1-985b-88abe601d021@a37g2000prf.googlegroups.com> References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> <79954da5-a437-48a1-985b-88abe601d021@a37g2000prf.googlegroups.com> Message-ID: NighterNet wrote: > Thanks it help. Sorry about that, I was just wander what kind of > answer and if there are other methods to learn it. > > Is there a way to position image to the center screen? Yes there is, just start reading from here: http://effbot.org/tkinterbook/ Though because Python 3 has done some module reorganisation/renaming, Tkinter is now called tkinter. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From deets at nospam.web.de Wed Jul 29 17:11:49 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 29 Jul 2009 23:11:49 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: References: Message-ID: <7dbs8nF2br07nU1@mid.uni-berlin.de> MalC0de schrieb: > hello there, I've a question : > I want to know does python have any capability for using Ring0 and > kernel functions for driver and device development stuff . > if there's such a feature it is very good, and if there something for > this kind that you know please refer me to some reference and show me > some snippet . No, it can't do such things. At least it isn't embedded in the kernel - in theory that might be possible, but given the timing-constraints and concurrency-requirements, it's not really feasible. Diez From bearophileHUGS at lycos.com Wed Jul 29 17:12:34 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Wed, 29 Jul 2009 14:12:34 -0700 (PDT) Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: Martin: > for f in var1_fn, var2_fn, var3_fn: > ? ? if f.split('.')[0] == 'var1': > ? ? ? ? var1 = call_some_function(f) > ? ? ? ? . > ? ? ? ? . > ? ? ? ? . > ? ? ? etc As usual I have big problems in understanding what you want to do. Please, show an example of the contents of var1_fn, var2_fn, etc. Generally you can create a dict: results = {} And then fill it with name-result pairs: name = value.split('_')[0] results[name] = some_function(value) Bye, bearophile From andre.roberge at gmail.com Wed Jul 29 17:18:52 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Wed, 29 Jul 2009 14:18:52 -0700 (PDT) Subject: how to embed the python interpreter into web App (Mehndi, Sibtey) References: Message-ID: <4c2f2ee0-1009-4dc7-8cd7-b38db43d83c7@o32g2000yqm.googlegroups.com> On Jul 29, 1:11?pm, Ryniek90 wrote: > > Hi All > > > I am trying to embed the python interpreter in to a web app but could > > not get the way, any one can suggest me how to do this. > > > Thanks, > > > Sibtey Mehdi > > > This e-mail (and any attachments), is confidential and may be privileged. It may be read, copied and used only > > by intended recipients. Unauthorized access to this e-mail (or attachments) and disclosure or copying of its > > contents or any action taken in reliance on it is unlawful. Unintended recipients must notify the sender immediately > > by e-mail/phone & delete it from their system without making any copies or disclosing it to a third person. > > Here's example:http://try-python.mired.org/ > > Contact with author of that website/webapp. > > Good luck. Or you can look at the code for Crunchy: http://code.google.com/p/crunchy Note however that this will result in something that is not secure... To quote the try-python site: "My ISP (idiom.com) provides a sandbox inside a FreeBSD Jail..." Andr? From davea at ieee.org Wed Jul 29 17:47:43 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 17:47:43 -0400 Subject: set variable to looping index? In-Reply-To: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: <4A70C37F.2030709@ieee.org> Martin wrote: > Hi, > > I am trying to set the return value from a function to a name which I > grab from the for loop. I can't work out how I can do this without > using an if statement... > > for f in var1_fn, var2_fn, var3_fn: > if f.split('.')[0] == 'var1': > var1 = call_some_function(f) > . > . > . > etc > > Really I would like to remove the need for this if loop and I am sure > there is a simple way I am missing? > > Many thanks > > Martin > > Is this a real problem, or is it a "programming puzzle"? If it's the latter, maybe someone else can help. But if it's a real problem, give us some context, and maybe we can figure out how to help. If I took this fragment at face value, I'd simply replace it by: var1 = call_some_function(var1_fn) var2 = call_some_function(var2_fn) var3 = call_some_function(var3_fn) Is this fragment part of a function definition, or is it top-level? Are there really exactly three var*_fn objects, or might there be an arbitrary number of them? Do you want to tell us the types of these three objects? Is there content really tied directly to their name? Did they get their values from literals, or were they computed at some other point? DaveA From rhodri at wildebst.demon.co.uk Wed Jul 29 18:02:00 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 29 Jul 2009 23:02:00 +0100 Subject: set variable to looping index? In-Reply-To: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: On Wed, 29 Jul 2009 19:56:28 +0100, Martin wrote: > Hi, > > I am trying to set the return value from a function to a name which I > grab from the for loop. I can't work out how I can do this without > using an if statement... > > for f in var1_fn, var2_fn, var3_fn: > if f.split('.')[0] == 'var1': > var1 = call_some_function(f) > . > . > . > etc > > Really I would like to remove the need for this if loop and I am sure > there is a simple way I am missing? It's a little hard to tell what you actually want from your description, but it looks like you're fighting the language unnecessarily here. If you have a sequence of functions that you want a sequence of results out of, you should be thinking in terms of a sequence type. A list, in other words. results = [] for f in fn1, fn2, fn3: results.append(f()) -- Rhodri James *-* Wildebeest Herder to the Masses From nobody at nowhere.com Wed Jul 29 18:21:27 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 23:21:27 +0100 Subject: Differences Between Arrays and Matrices in Numpy References: Message-ID: On Wed, 29 Jul 2009 16:23:33 -0400, Nanime Puloski wrote: > What are some differences between arrays and matrices using the Numpy > library? Matrices are always two-dimensional, as are slices of them. Matrices override mulitplication and exponentiation to use matrix multiplication rather than element-wise multiplication. > When would I want to use arrays instead of matrices and vice > versa? Use a matrix if you want a matrix, i.e. a linear transformation. Otherwise, use an array. From mdekauwe at gmail.com Wed Jul 29 18:29:30 2009 From: mdekauwe at gmail.com (Martin) Date: Wed, 29 Jul 2009 15:29:30 -0700 (PDT) Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> On Jul 29, 11:02?pm, "Rhodri James" wrote: > On Wed, 29 Jul 2009 19:56:28 +0100, Martin wrote: > > Hi, > > > I am trying to set the return value from a function to a name which I > > grab from the for loop. I can't work out how I can do this without > > using an if statement... > > > for f in var1_fn, var2_fn, var3_fn: > > ? ? if f.split('.')[0] == 'var1': > > ? ? ? ? var1 = call_some_function(f) > > ? ?. > > ? ? ? ? . > > ? ? ? ? . > > ? ? ? etc > > > ?Really I would like to remove the need for this if loop and I am sure > > there is a simple way I am missing? > > It's a little hard to tell what you actually want from your description, > but it looks like you're fighting the language unnecessarily here. ?If > you have a sequence of functions that you want a sequence of results > out of, you should be thinking in terms of a sequence type. ?A list, > in other words. > > results = [] > for f in fn1, fn2, fn3: > ? ? ?results.append(f()) > > -- > Rhodri James *-* Wildebeest Herder to the Masses Hi all, Thanks and apologises I wasn't trying to be cryptic, like all things when I wrote it I thought it was quite transparent. All I was trying to do was call a function and return the result to an a variable. I could admittedly of just done... var1 = some_function(var1_fn) var2 = some_function(var2_fn) var3 = some_function(var3_fn) where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn = 'x.txt'. But I figured I would try and make it slightly more generic whilst I was at it, hence my attempt to use the filenames to create the variable names (hence the loop). I will as suggested try the dictionary option. I appreciate all the suggestions. Thanks Martin From nick at craig-wood.com Wed Jul 29 18:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 29 Jul 2009 17:29:56 -0500 Subject: Does python have the capability for driver development ? References: <7dbs8nF2br07nU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > MalC0de schrieb: > > hello there, I've a question : > > I want to know does python have any capability for using Ring0 and > > kernel functions for driver and device development stuff . > > if there's such a feature it is very good, and if there something for > > this kind that you know please refer me to some reference and show me > > some snippet . > > No, it can't do such things. At least it isn't embedded in the kernel - > in theory that might be possible, but given the timing-constraints and > concurrency-requirements, it's not really feasible. You can write FUSE (file systems in userspace) drivers in python I believe. Not the same as running in ring0 but in most senses a kernel driver... -- Nick Craig-Wood -- http://www.craig-wood.com/nick From pavlovevidence at gmail.com Wed Jul 29 18:55:26 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 29 Jul 2009 15:55:26 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: <8cb1ba65-be71-49b7-8955-54c17f351a40@w6g2000yqw.googlegroups.com> On Jul 29, 7:14?am, Piet van Oostrum wrote: > >>>>> Carl Banks (CB) wrote: > >CB> On Jul 28, 3:15?pm, John D Giotta wrote: > >>> I'm looking to run a process with a limit of 3 instances, but each > >>> execution is over a crontab interval. I've been investigating the > >>> threading module and using daemons to limit active thread objects, but > >>> I'm not very successful at grasping the documentation. > > >>> Is it possible to do what I'm trying to do and if so anyone know of a > >>> useful example to get started? > >CB> It seems like you want to limit the number of processes to three; the > >CB> threading module won't help you there because it deals with threads > >CB> within a single process. > >CB> What I'd do is to simply run the system ps to see how many processes > >CB> are running (ps is pretty versatile on most systems and can find > >CB> specifically targeted processes like you program), and exit if there > >CB> are already three. > > That will surely run into some race conditions. What, the OS might not have gotten around to update the process table to include a process started minutes ago? (He said he was starting the processes over crontab intervals, not that he communicated what he wanted well.) Carl Banks From zuo at chopin.edu.pl Wed Jul 29 18:57:02 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 00:57:02 +0200 Subject: set variable to looping index? In-Reply-To: <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> Message-ID: 30-07-2009 wrote: > All I was trying to do was call a function and return the result to an > a variable. I could admittedly of just done... > > var1 = some_function(var1_fn) > var2 = some_function(var2_fn) > var3 = some_function(var3_fn) > > where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn = > 'x.txt'. But I figured I would try and make it slightly more generic > whilst I was at it, hence my attempt to use the filenames to create > the variable names (hence the loop). Hi, Then you could also consider using simply lists: filenames = p'foo', 'bar', baz'] results = [] for name in filenames: results.append(some_function(name)) If filenames were generated according to a particular pattern, you can mimic that pattern and generate filenames list using list-comprehension, e.g.: filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)] Chreers, *j -- Jan Kaliszewski (zuo) From zuo at chopin.edu.pl Wed Jul 29 19:03:33 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 01:03:33 +0200 Subject: set variable to looping index? In-Reply-To: References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> Message-ID: Me wrote: > filenames = p'foo', 'bar', baz'] Sorry, should be of course: filenames = ['foo', 'bar', baz'] *j -- Jan Kaliszewski (zuo) From mdekauwe at gmail.com Wed Jul 29 19:04:59 2009 From: mdekauwe at gmail.com (Martin) Date: Wed, 29 Jul 2009 16:04:59 -0700 (PDT) Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> Message-ID: On Jul 29, 11:57?pm, "Jan Kaliszewski" wrote: > 30-07-2009 wrote: > > All I was trying to do was call a function and return the result to an > > a variable. I could admittedly of just done... > > > var1 = some_function(var1_fn) > > var2 = some_function(var2_fn) > > var3 = some_function(var3_fn) > > > where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn = > > 'x.txt'. But I figured I would try and make it slightly more generic > > whilst I was at it, hence my attempt to use the filenames to create > > the variable names (hence the loop). > > Hi, > > Then you could also consider using simply lists: > > filenames = p'foo', 'bar', baz'] > results = [] > for name in filenames: > ? ? ?results.append(some_function(name)) > > If filenames were generated according to a particular pattern, you can > mimic that pattern and generate filenames list using > list-comprehension, e.g.: > > filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)] > > Chreers, > > *j > > -- > Jan Kaliszewski (zuo) I guess I wanted to keep the function returns in separate arrays in this case, hence my attempt to make variable names based on the filenames. Thanks Martin From martin.hellwig at dcuktec.org Wed Jul 29 19:05:04 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 00:05:04 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: References: Message-ID: MalC0de wrote: > hello there, I've a question : > I want to know does python have any capability for using Ring0 and > kernel functions for driver and device development stuff . > if there's such a feature it is very good, and if there something for > this kind that you know please refer me to some reference and show me > some snippet . > > thanks > > - Malc0de Python is interpreted, so the first requirement would be that the interpreter (the python VM to be more precise) would run in the kernel or that there is a way for the interpreter to delegate operations to kernel restricted operations. Most notably access to the memory location of the hardware you want to write a driver for and possibly also a way to pass through a callback function for triggered interrupt coming from the hardware. So technically speaking it shouldn't be impossible. And there is perhaps something to say for being able to write drivers in a readable/easy programming language however I am afraid that if such a beast would be made that it would remain an academical exercise only due to performance constraints. Though I would love to play around with a system where the kernel is essentially only a python interpreter, with full raw access to the hardware. But creating such a thing requires more talent and understanding than currently (and probably ever) in my possession. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From cjw at ncf.ca Wed Jul 29 19:27:41 2009 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 29 Jul 2009 19:27:41 -0400 Subject: Differences Between Arrays and Matrices in Numpy In-Reply-To: References: Message-ID: Robert Kern wrote: > On 2009-07-29 15:23, Nanime Puloski wrote: >> What are some differences between arrays and matrices using the Numpy >> library? When would I want to use arrays instead of matrices and vice >> versa? > > You will want to ask numpy questions on the numpy mailing list: > > http://www.scipy.org/Mailing_Lists > > An overview of how the matrix subclass differs from ndarray, see the > documentation: > > > http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects > > > Basically, I suggest that you just use regular arrays always. There is a > syntactical convenience to matrix objects, but it does cause > incompatibilities with the majority of code that is written for regular > arrays. The convenience is usually not worth the cost. > Numpy's arrays can have any dimensionality, whereas matrices[http://en.wikipedia.org/wiki/Matrix_%28mathematics%29], typically have two. A single column can represent a vector or a single row can represent a transposed vector. Does the additional cost arise because the commonly used procedures are accessed through numpy's array? Colin W. From rwanderley at rsw.digi.com.br Wed Jul 29 19:30:21 2009 From: rwanderley at rsw.digi.com.br (Rodrigo S Wanderley) Date: Wed, 29 Jul 2009 20:30:21 -0300 Subject: Does python have the capability for driver development ? In-Reply-To: (Martin P. Hellwig's message of "Thu, 30 Jul 2009 00:05:04 +0100") References: Message-ID: <87skgf83ua.fsf@rsw.digi.com.br> "Martin P. Hellwig" writes: > Python is interpreted, so the first requirement would be that the > interpreter (the python VM to be more precise) would run in the kernel > or that there is a way for the interpreter to delegate operations to > kernel restricted operations. Most notably access to the memory > location of the hardware you want to write a driver for and possibly > also a way to pass through a callback function for triggered > interrupt coming from the hardware. > > So technically speaking it shouldn't be impossible. And there is > perhaps something to say for being able to write drivers in a > readable/easy programming language however I am afraid that if such a > beast would be made that it would remain an academical exercise only > due to performance constraints. What about user level device drivers? Think the Python VM could communicate with the driver through the user space API. Is there a Python module for that? -- Rodrigo S. Wanderley -- Blog: http://rsw.digi.com.br From david.lyon at preisshare.net Wed Jul 29 19:33:01 2009 From: david.lyon at preisshare.net (David Lyon) Date: Wed, 29 Jul 2009 19:33:01 -0400 Subject: Does python have the capability for driver development =?UTF-8?Q?=3F?= In-Reply-To: References: Message-ID: MalC0de wrote: > hello there, I've a question : > I want to know does python have any capability for using Ring0 and > kernel functions for driver and device development stuff . > if there's such a feature it is very good, and if there something for > this kind that you know please refer me to some reference and show me > some snippet . What operating system are you talking about? Most device drivers run at ring 3 (or lower) and not zero. This way if there is a driver crash the whole operating system doesn't freeze. Python is generally considered a high-level language. If you want to play around with drivers.. usb serial.. etc do it at a python level through the existing device drivers. imho the performance of interpreted python isn't compatible with writing block-mode device drivers (hard-disks) and so forth. What hardware do you have that you need to write a device driver for ? Isn't there a device driver available already? or do you mean just a device controller? David From robert.kern at gmail.com Wed Jul 29 19:42:05 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 29 Jul 2009 18:42:05 -0500 Subject: Differences Between Arrays and Matrices in Numpy In-Reply-To: References: Message-ID: On 2009-07-29 18:27, Colin J. Williams wrote: > Robert Kern wrote: >> On 2009-07-29 15:23, Nanime Puloski wrote: >>> What are some differences between arrays and matrices using the Numpy >>> library? When would I want to use arrays instead of matrices and vice >>> versa? >> >> You will want to ask numpy questions on the numpy mailing list: >> >> http://www.scipy.org/Mailing_Lists >> >> An overview of how the matrix subclass differs from ndarray, see the >> documentation: >> >> http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects >> >> >> Basically, I suggest that you just use regular arrays always. There is >> a syntactical convenience to matrix objects, but it does cause >> incompatibilities with the majority of code that is written for >> regular arrays. The convenience is usually not worth the cost. > > Numpy's arrays can have any dimensionality, whereas > matrices[http://en.wikipedia.org/wiki/Matrix_%28mathematics%29], > typically have two. A single column can represent a vector or a single > row can represent a transposed vector. > > Does the additional cost arise because the commonly used procedures are > accessed through numpy's array? Most functions are written to expect that its inputs behave like ndarrays; e.g. a*b is elementwise multiplication rather than matrix multiplication. When you use the matrix subclass, you are basically confining yourself to a smallish ghetto of functions that knows how to deal with matrix semantics. That's a huge cost compared to the relatively small syntactic cost of having to write dot(a,b) instead of (a*b). -- 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 ben+python at benfinney.id.au Wed Jul 29 19:42:35 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 30 Jul 2009 09:42:35 +1000 Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: <87ljm72h04.fsf@benfinney.id.au> dandi kain writes: > What is the functionality of __ or _ , leading or trailing an object , > class ot function ? There is no change in functionality. It has some slight effects on import, but as a beginner you shouldn't need to worry about that. > Is it just a naming convention to note special functions and objects , > or it really mean someting to Python ? Both. It's a strongly-adhered-to naming convention, as an indicator to the reader how that name should be used. foo Ordinary name, part of public interface _foo Ordinary name, part of internal-only interface __foo Ordinary name, but will be mangled (this style used rarely) __foo__ Name which is used in a special way by Python There's no change in the objects themselves; the underscores rather indicate how those names are expected to be accessed. -- \ ?Pinky, are you pondering what I'm pondering?? ?I think so, | `\ Brain, but Tuesday Weld isn't a complete sentence.? ?_Pinky and | _o__) The Brain_ | Ben Finney From martin.hellwig at dcuktec.org Wed Jul 29 19:44:41 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 00:44:41 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <87skgf83ua.fsf@rsw.digi.com.br> References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: <4A70DEE9.1060105@dcuktec.org> Rodrigo S Wanderley wrote: > > What about user level device drivers? Think the Python VM could > communicate with the driver through the user space API. Is there a > Python module for that? > > Sure why not? Look for example to libusb, which provides a userspace environment and pyusb which uses that and provides an interface to Python. -- MPH From nyamatongwe+thunder at gmail.com Wed Jul 29 19:55:34 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Wed, 29 Jul 2009 23:55:34 GMT Subject: No PyPI search for 3.x compatible packages Message-ID: There appears to be no way to search PyPI for packages that are compatible with Python 3.x. There are classifiers for 'Programming Language' including 'Programming Language :: Python :: 3' but that seems to be for implementation language since there are so many packages that specify C. There are a total of 109 packages classified with Python :: [3, 3.0, 3.1] out of a total of 4829 packages. http://pypi.python.org/pypi?:action=browse&show=all&c=214&c=533 The search box appears to search for any word entered so a search like "xml 3.0" or "xml AND 3.0" does not help. Some packages include version information in the Py Version column of their download lists or embedded in the download file names. Projects are often constrained to a particular set of Python versions so need to choose packages that will work with those versions. It would be helpful if PyPI made this information more visible and searchable. Neil From malc0de.encrypt at gmail.com Wed Jul 29 20:19:42 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Wed, 29 Jul 2009 17:19:42 -0700 (PDT) Subject: Does python have the capability for driver development ? References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: <94abbddc-dac7-4215-84d1-d4eb3f82724d@h21g2000yqa.googlegroups.com> actually I mean driver programming under Windows operating system, if you know, there's A kit name DDK available at microsoft's website for developing device drivers under C / C++ environment, now i'm working with this kind of toolkit, but when I started programming with Python, I saw this is very good and easy to develop application in all aspects of a programmer or customer . I love python cuze I can write every application i want, it's easy and it's very robust, But i thought if there's any available toolkit like Microsoft DDK which can program driver under windows but based on python it's very good and well, I don't know it's the lack of python or interpreter concept but cuze of the extensibility of python if such a good feature can be added to python it will be good enough. actually I want to write device drivers in Ring0 mode or kernel mode ... please introduce me some resource for system programming, I know that python is very well at system programming level. thanks - Malc0de From wuwei23 at gmail.com Wed Jul 29 20:24:03 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 29 Jul 2009 17:24:03 -0700 (PDT) Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: <11942b01-07b4-44d9-9be5-92868493b706@g1g2000pra.googlegroups.com> On Jul 30, 3:59?am, dandi kain wrote: > What is the functionality of __ or _ , leading or trailing an object , > class ot function ? Is it just a naming convention to note special > functions and objects , or it really mean someting to Python ? I think everyone else has covered what you need to know, but there's still one remaining pseudo-convention you might see in Python code, and that's using _ as a label to indicate that you don't care about the value it holds. Overly simplistic example: data_set = [('gold','junk'),('gold','junk'),...] for keep, _ in data_set: ... It's a convenient way of not having to come up with a label for something you're not going to use, although arguably you get the same effect with names like 'dummy', 'ignore' etc. Not everyone agrees with this usage but you _will_ see it in use in other people's code, so it helps to have a heads up. From marcusw at cox.net Wed Jul 29 20:24:16 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 20:24:16 -0400 Subject: Does python have the capability for driver development ? In-Reply-To: References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: On 7/29/2009 7:44 PM, Martin P. Hellwig wrote: > Rodrigo S Wanderley wrote: >> >> What about user level device drivers? Think the Python VM could >> communicate with the driver through the user space API. Is there a >> Python module for that? >> >> > Sure why not? > Look for example to libusb, which provides a userspace environment and > pyusb which uses that and provides an interface to Python. > iicr pyusb uses a c interface to libusb, not python... Marcus From http Wed Jul 29 21:04:47 2009 From: http (Paul Rubin) Date: 29 Jul 2009 18:04:47 -0700 Subject: How to "gunzip-iterate" over a file? References: Message-ID: <7x8wi7j80g.fsf@ruckus.brouhaha.com> Robert Kern writes: > f = gzip.open('filename.gz') > for line in f: > print line or use f.read(nbytes) to read n uncompressed bytes from f. Note that the standard iterator (which iterates over lines) can potentially consume an unbounded amount of memory if the file contains no newlines. From greg at cosc.canterbury.ac.nz Wed Jul 29 21:09:14 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Thu, 30 Jul 2009 13:09:14 +1200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <200907281035.48713.hendrik@microcorp.co.za> <4A6F1506.9030207@mrabarnett.plus.com> Message-ID: <7dca4iF2bgbn5U1@mid.individual.net> Hendrik van Rooyen wrote: > And if code is data, where is Pythons ALTER statement? class Duck: def quack(self): print "Quack!" def moo(): print "Moo!" def ALTER(obj, name, TO_PROCEED_TO): setattr(obj, name, TO_PROCEED_TO) d = Duck() ALTER(d, 'quack', TO_PROCEED_TO = moo) d.quack() -- Greg From clp2 at rebertia.com Wed Jul 29 21:21:32 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 18:21:32 -0700 Subject: Working with platform.system() In-Reply-To: <9a5b5be90907290610t1868ef07p20a5c2bdc3404ab8@mail.gmail.com> References: <9a5b5be90907290610t1868ef07p20a5c2bdc3404ab8@mail.gmail.com> Message-ID: <50697b2c0907291821l54f30d99q44df2a9188a917f0@mail.gmail.com> On Wed, Jul 29, 2009 at 6:10 AM, v1d4l0k4 wrote: > Hi! > > I want to know how I can get different expected return values by > platform.system() method. I want to sniff some systems (Linux, Windows, Mac, > BSD, Solaris) and I don't know how I can check them correctly. Could you > give me some expected return values that you know? On my Mac, I get "Darwin". You should file a bug in the docs after compiling the results, to help future users. Cheers, Chris -- http://blog.rebertia.com From draghuram at gmail.com Wed Jul 29 23:00:57 2009 From: draghuram at gmail.com (Raghuram Devarakonda) Date: Wed, 29 Jul 2009 20:00:57 -0700 (PDT) Subject: Connecting multiple test cases in a sort of pipe Message-ID: Hi, I have three functions - create(), list(), and delete(). create() creates a new name in the file system hierarchy while list() displays all such created names and delete() removes them. I would like to write test cases for each of these functions, say, test_create() , test_list(), and test_delete() using the unittest module. It easily follows that test_list() and test_delete() will need to know the exact name that was created in test_create() in order to properly validate list() and delete(). One option is for test_create() to always use a hard coded name but unfortunately this is not feasible. Another way would be for test_create() to pass the new name down to the next test case test_list() which would do the same and pass the name down to test_delete(). In other words, the requirement is to connect all three test cases in a kind of pipe. This facility is obviously not currently present in the unittest module and nor does it seem to be available in other frameworks (such as py.test and nose, I only skimmed the feature list). I am thinking of implementing this feature on top of unittest.TestCase and would like to know what others think of the idea. Is there a framework that has similar feature? Or perhaps, given my requirement as stated above, is there some alternate way of implementing it? I greatly appreciate any feed back. Thanks, Raghu From rt8396 at gmail.com Wed Jul 29 23:06:33 2009 From: rt8396 at gmail.com (r) Date: Wed, 29 Jul 2009 20:06:33 -0700 (PDT) Subject: Confessions of a Python fanboy Message-ID: Hello fellow Pythonista's it's nice to be back after such a long hiatus. I have recently realized my blind love affair with Python was just that. I must admit there are better ways to program. Like your first lay, your first programing language can leave an indelible mark on you, but like all young chaps, the passion for programming has sweep me away into vast new orgies of programming bliss. (ig ,little head guides the big one.) My adventures in Ruby. an autobyography by rt8396 After breaking free from the wild love affair i had with Python, a new day had dawned. I dried my eyes and soon discovered the Ruby language (not by choice however) --now don't worry fellow Pythonistas, i am still on your side (mostly) -- but i now realize Ruby has some good things going for it. For instance... 1.) No need to use "()" to call a function with no arguments. Python --> "obj.m2().m3()" --ugly Ruby --> "obj.m1.m2.m3" -- sweeet! Man, i must admit i really like this, and your code will look so much cleaner. 2.) the .each method container.each{|localVar| block} This method can really cleanup some ugly for loops, although i really like the readability of for loops. 3.) true OOP Now before you go and get all "huffy" over this statement, hear me out. Python is the best language in the world. But it damn sure has some warts! "len(this)" instead of "obj.length" max(that) instead of [1,2,3,4,5].max(). You know what i am talking about here people. We all get complacent and It seems easier to just cope with these problems instead of fighting for change. But look at the French, WHAT THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! As for the rest of Ruby, i am not impressed. The redundant usage of "end" over indention perplexes me. The Perlish feel of "require" and the horrifically cryptic idioms of Ruby regular expressions. The "puts" and "gets" seem childish and the math class does not even have a degrees or radians function! Anyway, i thought i would get this off my chest and catch up with old friends in the meantime. We can all make Python the perfect language it needs to be, but it ain't gonna be easy! Thank you all PS stay tuned for more from this series.... From rt8396 at gmail.com Wed Jul 29 23:43:58 2009 From: rt8396 at gmail.com (r) Date: Wed, 29 Jul 2009 20:43:58 -0700 (PDT) Subject: IDLE Config Problems References: Message-ID: <37c1f7fa-8008-41b0-b2c4-d9c1e5288e2e@v20g2000yqm.googlegroups.com> On Jul 29, 3:55?pm, "Russ Davis" wrote: > I am just getting started with Python and have installed v. 2.5.4 ?Idle version 1.2.4 ?I can't seem to get the idle to display text. ?It seems as if the install went fine. ?I start up the idle and the screen is blank. ?No text. ?It seems as if I can type on the screen but I just can't see the characters. ? I go to the config menu and it bombs and brings up the Visual Studio debugger. ?The computer that I am trying to install it on is a windows xp laptop serv pack 2. ? I have a workstation here that I am using also with the same os and the idle works fine. ?Any hints as to what might be interfering with the idle config. > just spit balling here, but have you tried a un-install, re-install """ Bad things can happen to good installers """ From darkneter at gmail.com Wed Jul 29 23:52:06 2009 From: darkneter at gmail.com (NighterNet) Date: Wed, 29 Jul 2009 20:52:06 -0700 (PDT) Subject: socket send Message-ID: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> I am trying to figure out how to send text or byte in python 3.1. I am trying to send data to flash socket to get there. I am not sure how to work it. buff= 'id=' , self.id , ':balive=False\n' clientSock.send(buff); From wuwei23 at gmail.com Thu Jul 30 00:04:27 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 29 Jul 2009 21:04:27 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: On Jul 30, 1:06?pm, r wrote: > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. How do you distinguish between calling a method with no arguments, and getting access to the method object itself (because it _is_ an object, y'know, it's OO all the way down...)? > 2.) the .each method > container.each{|localVar| block} > This method can really cleanup some ugly for loops, although i really > like the readability of for loops. map(lambda localVar: , sequence) or: def usefully_named_func(var): return var transformed = [usefully_named_func(v) for v in sequence] > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). As the Zen says: '[P]racticality beats purity'. Personally, I'm not sure how a handful of convenient built-in functions make a language in which _everything is an object_ somehow "false" OO. If you're really that concerned with writing "true" OO (for some wildly variable value of "true"), there's nothing stopping you from doing so now: obj.__len__() With max(), this is a built-in that takes _any_ iterable and an optional key function, and returns the highest value as per the key. This means that _every_ iterable object - as _well_ as every object that supports enough of the iterator protocol - can be handed to max() and a result obtained. So at best, I just need to make sure my new sequence-type provides the iterator protocol and viola, it works with max() without me having to hand-code a .max() that's specialised for my new type, and without Python forcing me to have a complex inheritance chain just to make sure I include the right MaxableSequence ancestor to inherit the right .max(). > PS stay tuned for more from this series.... Is this going to be more of you telling us - without any apparent irony whatsoever - how Ruby has some valid points after all your vilification last year when we said the same to you? If so, where can I sign up?! (You should consider trading guest spot posts with Xah Lee on your respective blogs. You both have a very similar approach to programming & programming languages and I think the synergy would be amazing to see.) From pdlemper at earthlink.net Thu Jul 30 00:24:11 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Wed, 29 Jul 2009 23:24:11 -0500 Subject: gamma approximation : what is module cmath and where is it located ? Message-ID: The following numerical approximation for Euler's Gamma function is found in http://en.wikipedia.org/wiki/Lanczos_approximation from cmath import * # Coefficients used by the GNU Scientific Library g = 7 p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7] def gamma(z): z = complex(z) # Reflection formula if z.real < 0.5: return pi / (sin(pi*z)*gamma(1-z)) else: z -= 1 x = p[0] for i in range(1, g+2): x += p[i]/(z+i) t = z + g + 0.5 return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x This works in Python 3.0 But I can't figure out where it gets "cmath". Searching the Python directory reveals no more than a test_cmath. The only cmath I can find is a header file in another directory turboc++\Borland\include\ dir(cmath) reveals 23 functions overlapping the 37 functions of the math module. What is cmath, where did it come from and how does it differ from the standard math module ? Dave WB3DWE I saw the number 4 in silver, Guido (apologies to Wm Carlos Williams) From clp2 at rebertia.com Thu Jul 30 00:27:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 21:27:20 -0700 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <50697b2c0907292127l98ff61aief7f7205b5b5ca9@mail.gmail.com> On Wed, Jul 29, 2009 at 9:04 PM, alex23 wrote: > On Jul 30, 1:06?pm, r wrote: >> 1.) No need to use "()" to call a function with no arguments. >> Python --> "obj.m2().m3()" --ugly >> ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! >> Man, i must admit i really like this, and your code will look so much >> cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? IIRC from the Pickaxe, to get at a method object, you call a method on the object whose specific job it is to return method objects. Unsurprisingly, this method is itself named "method". So: foo.bar #calls the "bar" method on foo foo.method(:bar) #returns the method "bar" of foo, as an object Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Thu Jul 30 00:34:07 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 21:34:07 -0700 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: <50697b2c0907292134p594105fag2bf99e0b5e14e35c@mail.gmail.com> On Wed, Jul 29, 2009 at 9:24 PM, wrote: > But I can't figure out where it gets "cmath". ? ?Searching the Python > directory reveals no more than a test_cmath. ?The only cmath I can find > is a header file in another directory ?turboc++\Borland\include\ > > dir(cmath) reveals 23 functions overlapping the 37 functions of > the math module. > > What is cmath, where did it come from and how does it differ from > the standard math module ?? It's a module in the standard library: http://docs.python.org/library/cmath.html The difference is that it handles complex numbers, whereas the plain "math" module doesn't. I would guess the reason there are separate modules is for performance, so as to avoid having to dispatch on type at runtime. But this is only a guess. Cheers, Chris -- http://blog.rebertia.com From pavlovevidence at gmail.com Thu Jul 30 00:36:31 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 29 Jul 2009 21:36:31 -0700 (PDT) Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: On Jul 29, 9:24?pm, pdlem... at earthlink.net wrote: > What is cmath, where did it come from and how does it differ from > the standard math module ?? What happened when you did a keyword search for "cmath" in the Python library documentation? What happened when you entered "python cmath" in Google? Carl Banks From pavlovevidence at gmail.com Thu Jul 30 00:43:47 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 29 Jul 2009 21:43:47 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: On Jul 29, 9:04?pm, alex23 wrote: > On Jul 30, 1:06?pm, r wrote: > > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? > > > 2.) the .each method > > container.each{|localVar| block} > > This method can really cleanup some ugly for loops, although i really > > like the readability of for loops. > > map(lambda localVar: , sequence) > > or: > > def usefully_named_func(var): > ? ? > ? ? return var > > transformed = [usefully_named_func(v) for v in sequence] > > > 3.) true OOP > > Now before you go and get all "huffy" over this statement, hear me > > out. Python is the best language in the world. But it damn sure has > > some warts! "len(this)" instead of "obj.length" max(that) instead of > > [1,2,3,4,5].max(). > > As the Zen says: '[P]racticality beats purity'. Personally, I'm not > sure how a handful of convenient built-in functions make a language in > which _everything is an object_ somehow "false" OO. > > If you're really that concerned with writing "true" OO (for some > wildly variable value of "true"), there's nothing stopping you from > doing so now: > > ? ? obj.__len__() > > With max(), this is a built-in that takes _any_ iterable and an > optional key function, and returns the highest value as per the key. > This means that _every_ iterable object - as _well_ as every object > that supports enough of the iterator protocol - can be handed to max() > and a result obtained. So at best, I just need to make sure my new > sequence-type provides the iterator protocol and viola, it works with > max() without me having to hand-code a .max() that's specialised for > my new type, and without Python forcing me to have a complex > inheritance chain just to make sure I include the right > MaxableSequence ancestor to inherit the right .max(). > > > PS stay tuned for more from this series.... > > Is this going to be more of you telling us - without any apparent > irony whatsoever - how Ruby has some valid points after all your > vilification last year when we said the same to you? ?If so, where can > I sign up?! > > (You should consider trading guest spot posts with Xah Lee on your > respective blogs. You both have a very similar approach to programming > & programming languages and I think the synergy would be amazing to > see.) Here is some wisdom from Guido van Rossum on the matter: http://mail.python.org/pipermail/python-dev/2006-May/064841.html Carl Banks From pdlemper at earthlink.net Thu Jul 30 01:25:11 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Thu, 30 Jul 2009 00:25:11 -0500 Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> On Wed, 29 Jul 2009 21:36:31 -0700 (PDT), Carl Banks wrote: >On Jul 29, 9:24?pm, pdlem... at earthlink.net wrote: >> What is cmath, where did it come from and how does it differ from >> the standard math module ?? > >What happened when you did a keyword search for "cmath" in the Python >library documentation? > >What happened when you entered "python cmath" in Google? > > >Carl Banks Thanks Carl & Chris. I've read the Python doc 10.3 and now understand its for complex arithmetic. I've used python a few months and expected to find cmath seperately sort of as a " header file". A text search failed. I now understand its in the Python Standard Library, which I assume is that big file Python30\LIBS\libpython30.a And this is not readable as text. Dave From wuwei23 at gmail.com Thu Jul 30 01:28:06 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 29 Jul 2009 22:28:06 -0700 (PDT) Subject: IDLE Config Problems References: Message-ID: <89553ecc-a30e-4280-8a42-17a18af50ba9@v37g2000prg.googlegroups.com> On Jul 30, 6:55?am, "Russ Davis" wrote: > I am just getting started with Python and have installed v. 2.5.4 ? > Idle version 1.2.4 ?I can't seem to get the idle to display text. ?It > seems as if the install went fine. ?I start up the idle and the screen > is blank. ?No text. ?It seems as if I can type on the screen but I just > can't see the characters. Is this the official python.org distribution you're trying? I can't speak from experience, but I'm wondering if it doesn't include the pyreadline module by default, which you'll require under Windows (although I would've expected to hear more problems like your's if this is the case). You can easily check this by going to C: \Python25\Lib\site-packages and looking for a folder or a file called pyreadline. If it's not, you'll have to install it separately. If you're not already familiar with it, I do recommend checking out the ActiveState ActivePython distribution instead, especially if you're working under Windows. It includes a lot of Windows-specific 3rd party modules, such as the win32all modules, and it definitely includes pyreadline. Is there any particular reason why you're opting for Python 2.5.x? 2.6 should be pretty compatible with most things in the 2.x stream and it's a handy stepping stone towards using 3.x (which, admittedly, is probably the last of your concerns when you're first setting up...) From tjreedy at udel.edu Thu Jul 30 01:35:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 01:35:19 -0400 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: pdlemper at earthlink.net wrote: > The following numerical approximation for Euler's Gamma function > is found in http://en.wikipedia.org/wiki/Lanczos_approximation > > from cmath import * > But I can't figure out where it gets "cmath". The Python documentation set has a module index. Really handy. I use it all the time. cmath is under 'c'. To answer some of your question: >>> math.sqrt(-1) Traceback (most recent call last): File "", line 1, in math.sqrt(-1) ValueError: math domain error >>> cmath.sqrt(-1) 1j Which to use depends on the behavior you want. tjr From Ron.Barak at lsi.com Thu Jul 30 02:01:28 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 07:01:28 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <4A708F68.1010505@ieee.org> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> > -----Original Message----- > From: Dave Angel [mailto:davea at ieee.org] > Sent: Wednesday, July 29, 2009 21:05 > To: Barak, Ron > Cc: 'python-list at python.org' > Subject: Re: Run pyc file without specifying python path ? > > Barak, Ron wrote: > > Hi, > > > > I wanted to make a python byte-code file executable, > expecting to be able to run it without specifying "python" on > the (Linux bash) command line. > > > > So, I wrote the following: > > > > [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > > > > print "hello" > > [root at VMLinux1 python]# > > > > and made its pyc file executable: > > > > [root at VMLinux1 python]# ls -ls test_pyc.pyc > > 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > > [root at VMLinux1 python]# > > > > So, I see: > > > > [root at VMLinux1 python]# file test_pyc.py* > > test_pyc.py: a python script text executable > > test_pyc.pyc: python 2.3 byte-compiled > > [root at VMLinux1 python]# > > > > If I try to do the following, no problem: > > > > [root at VMLinux1 python]# python test_pyc.pyc hello > > [root at VMLinux1 python]# > > > > However, the following fails: > > > > [root at VMLinux1 python]# ./test_pyc.pyc > > -bash: ./test_pyc.pyc: cannot execute binary file > > [root at VMLinux1 python]# > > > > Is there a way to run a pyc file without specifying the > python path ? > > > > Bye, > > Ron. > > > > > I don't currently run Unix, but I think I know the problem. > > In a text file, the shell examines the first line, and if it > begins #! > it's assumed to point to the executable of an interpreter for > that text file. Presumably the same trick doesn't work for a > .pyc file. > > Why not write a trivial wrapper.py file, don't compile it, > and let that invoke the main code in the .pyc file? > > Then make wrapper.py executable, and you're ready to go. > > DaveA > > Hi Dave, Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. Bye, Ron. From deets at nospam.web.de Thu Jul 30 02:33:54 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 08:33:54 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: References: <7dbs8nF2br07nU1@mid.uni-berlin.de> Message-ID: <7dct6iF2bhhshU1@mid.uni-berlin.de> Nick Craig-Wood schrieb: > Diez B. Roggisch wrote: >> MalC0de schrieb: >>> hello there, I've a question : >>> I want to know does python have any capability for using Ring0 and >>> kernel functions for driver and device development stuff . >>> if there's such a feature it is very good, and if there something for >>> this kind that you know please refer me to some reference and show me >>> some snippet . >> No, it can't do such things. At least it isn't embedded in the kernel - >> in theory that might be possible, but given the timing-constraints and >> concurrency-requirements, it's not really feasible. > > You can write FUSE (file systems in userspace) drivers in python I believe. > Not the same as running in ring0 but in most senses a kernel driver... No. That's why it is called "userspace". The kernel just hooks into a running program. Diez From __peter__ at web.de Thu Jul 30 02:41:00 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 30 Jul 2009 08:41 +0200 Subject: gamma approximation : what is module cmath and where is it located ? References: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> Message-ID: pdlemper at earthlink.net wrote: > I've used python a few months and expected to find cmath seperately > sort of as a " header file". A text search failed. I now understand > its in the Python Standard Library, which I assume is that big file > Python30\LIBS\libpython30.a Python 3.1 is no longer maintained. I recommend that you upgrade to Python 3.1. > And this is not readable as text. The module is written in C. The source code for the 3.1 version is here: http://svn.python.org/view/python/branches/release31-maint/Modules/cmathmodule.c?view=markup If you want the source for all parts of python written in C you can download the sourcecode at http://www.python.org/download/releases/3.1/ Peter From deets at nospam.web.de Thu Jul 30 02:43:27 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 08:43:27 +0200 Subject: Connecting multiple test cases in a sort of pipe In-Reply-To: References: Message-ID: <7dctogF29q4c6U1@mid.uni-berlin.de> Raghuram Devarakonda schrieb: > Hi, > > I have three functions - create(), list(), and delete(). create() > creates a new name in the file system hierarchy while list() displays > all such created names and delete() removes them. I would like to > write test cases for each of these functions, say, test_create() , > test_list(), and test_delete() using the unittest module. It easily > follows that test_list() and test_delete() will need to know the exact > name that was created in test_create() in order to properly validate > list() and delete(). One option is for test_create() to always use a > hard coded name but unfortunately this is not feasible. > > Another way would be for test_create() to pass the new name down to > the next test case test_list() which would do the same and pass the > name down to test_delete(). In other words, the requirement is to > connect all three test cases in a kind of pipe. This facility is > obviously not currently present in the unittest module and nor does it > seem to be available in other frameworks (such as py.test and nose, I > only skimmed the feature list). > > I am thinking of implementing this feature on top of unittest.TestCase > and would like to know what others think of the idea. Is there a > framework that has similar feature? Or perhaps, given my requirement > as stated above, is there some alternate way of implementing it? I > greatly appreciate any feed back. Write a TestCase-subclass that simply works like this: class FileTests(TestCase): def test_create(self): self.created_file = create() assert something def test_list(self): self.test_create() delete(self.created_file) Passing state around is one of the reasons objects have been invented :) And nobody ever said that you can't invoke a test-method from somewhere else. Diez From clp2 at rebertia.com Thu Jul 30 02:53:36 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 23:53:36 -0700 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> Message-ID: <50697b2c0907292353i479f5d70ke59a91c330b6d8f7@mail.gmail.com> On Wed, Jul 29, 2009 at 11:41 PM, Peter Otten<__peter__ at web.de> wrote: > pdlemper at earthlink.net wrote: > >> I've used python a few months and expected to find cmath seperately >> sort of as a " header file". ?A text search failed. ?I now understand >> its in the Python Standard Library, which I assume is that big file >> Python30\LIBS\libpython30.a > > Python 3.1 is no longer maintained. I recommend that you upgrade to Python 3.1. (He meant to say 3.0.x is no longer maintained.) - Chris -- http://blog.rebertia.com From __peter__ at web.de Thu Jul 30 03:06:31 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 30 Jul 2009 09:06:31 +0200 Subject: gamma approximation : what is module cmath and where is it located ? References: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> Message-ID: Chris Rebert wrote: > On Wed, Jul 29, 2009 at 11:41 PM, Peter Otten<__peter__ at web.de> wrote: >> pdlemper at earthlink.net wrote: >> >>> I've used python a few months and expected to find cmath seperately >>> sort of as a " header file". A text search failed. I now understand >>> its in the Python Standard Library, which I assume is that big file >>> Python30\LIBS\libpython30.a >> >> Python 3.1 is no longer maintained. I recommend that you upgrade to >> Python 3.1. > > (He meant to say 3.0.x is no longer maintained.) Ouch. Thanks for the correction. From xubin.cz at gmail.com Thu Jul 30 03:12:33 2009 From: xubin.cz at gmail.com (xubin.cz) Date: Thu, 30 Jul 2009 00:12:33 -0700 (PDT) Subject: beautiful soup Message-ID: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> hi, everyone Is there any pakage or module handling html document like beautiful soup? thanks a lot From deets at nospam.web.de Thu Jul 30 03:30:48 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 09:30:48 +0200 Subject: beautiful soup In-Reply-To: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> References: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> Message-ID: <7dd0h8F2a2he0U1@mid.uni-berlin.de> xubin.cz schrieb: > hi, everyone > > Is there any pakage or module handling html document like beautiful > soup? why don't you *use* beautiful soup? It is a module... Diez From enleverLesX_XXmcX at XmclavXeauX.com Thu Jul 30 03:54:21 2009 From: enleverLesX_XXmcX at XmclavXeauX.com (Michel Claveau - MVP) Date: Thu, 30 Jul 2009 09:54:21 +0200 Subject: Does python have the capability for driver development ? References: Message-ID: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Hi! > Python is interpreted No. Python is compiled (--> .pyc) But the term "to compile" is not always unambiguous... And the notion of "compiler" is not attached to Python (the language), but is attached to the implementation. @+ MCI From massi_srb at msn.com Thu Jul 30 03:54:31 2009 From: massi_srb at msn.com (Massi) Date: Thu, 30 Jul 2009 00:54:31 -0700 (PDT) Subject: Bug in IEHtmlWindow? Message-ID: <8a5ed753-d437-45a6-8fee-300eea1207ca@c2g2000yqi.googlegroups.com> Hi everyone, I'm trying to use IEHtmlWindow in my application (python 2.5, wxpython 2.8 on win xp). Everything is ok, but I encountered a problem which also affects the demo. If I make a research on google a strange message appears, saying (I'll try to translate from italian): Error during execution. Do you want to run the debug? line 29: error: 'r' is NULL or it is not an object Did anybody have the same problem? is it a bug? Thanks in advance for your help. From masklinn at masklinn.net Thu Jul 30 03:55:24 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 09:55:24 +0200 Subject: beautiful soup In-Reply-To: <7dd0h8F2a2he0U1@mid.uni-berlin.de> References: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> <7dd0h8F2a2he0U1@mid.uni-berlin.de> Message-ID: On 30 Jul 2009, at 09:30 , Diez B. Roggisch wrote: > xubin.cz schrieb: >> hi, everyone >> Is there any pakage or module handling html document like beautiful >> soup? > > why don't you *use* beautiful soup? It is a module... Or lxml, which works a bit better than BF 3.1 (post parser change) nowadays. From martin.hellwig at dcuktec.org Thu Jul 30 04:03:26 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 09:03:26 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: Marcus Wanner wrote: >> Look for example to libusb, which provides a userspace environment and >> pyusb which uses that and provides an interface to Python. >> > iicr pyusb uses a c interface to libusb, not python... > According to them they use ctypes indeed. Sorry if I was misleading in my explanation. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From martin.hellwig at dcuktec.org Thu Jul 30 04:05:48 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 09:05:48 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <4a7151b2$0$17782$ba4acef3@news.orange.fr> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: Michel Claveau - MVP wrote: > Hi! > >> Python is interpreted > > No. Python is compiled (--> .pyc) > But the term "to compile" is not always unambiguous... > And the notion of "compiler" is not attached to Python (the language), but is attached to the implementation. > > @+ > > MCI Well the pyc, which I thought was the Python bytecode, is then interpreted by the VM. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From javier.collado at gmail.com Thu Jul 30 04:17:24 2009 From: javier.collado at gmail.com (Javier Collado) Date: Thu, 30 Jul 2009 10:17:24 +0200 Subject: string.Template issue Message-ID: Hello, In the string.Template documentation (http://docs.python.org/library/string.html) it's explained that if a custom regular expression for pattern substitution is needed, it's possible to override idpattern class attribute (whose default value is [_a-z][_a-z0-9]*). However, if the custom pattern that is needed is just uppercase letters something like [A-Z]+ won't work because of the following line in the _TemplateMetaclass class __init__ method: cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) I would say that this is an error (IGNORECASE just shouldn't be there) and that the line above should be: cls.pattern = _re.compile(pattern, _re.VERBOSE) and the default value for idpattern: [_a-zA-Z][_a-zA-Z0-9]* Do you agree on this? Is there any reason for the IGNORECASE option to be passed to re.compile? Best regards, Javier From kunguz at gmail.com Thu Jul 30 05:02:04 2009 From: kunguz at gmail.com (=?ISO-8859-9?Q?Kaan_AK=DE=DDT?=) Date: Thu, 30 Jul 2009 11:02:04 +0200 Subject: Pyplot Message-ID: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> I am a newbee in Python. I have some problem with usage of Pyplot. I have a loop that creates plot in every end of it. Problem starts here: The first plot is fine but the second one is plotted onto the first plot. I use: plt.cla() plt.draw() plt.hold(False) plt.close() plt.clf() but nothing works. Perhaps I am having a problem with basics, please help me if you know something about it. Kaan -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Thu Jul 30 05:10:32 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 30 Jul 2009 11:10:32 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: Martin P. Hellwig wrote: > Well the pyc, which I thought was the Python bytecode, is then > interpreted by the VM. Python is often referred as byte-code interpreted language. Most modern languages are interpreted languages. The list [1] is rather long. Technically speaking even native code is interpreted by the micro code of most CPUs. [1] http://en.wikipedia.org/wiki/Interpreted_language [2] http://en.wikipedia.org/wiki/Microcode From ben+python at benfinney.id.au Thu Jul 30 05:14:54 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 30 Jul 2009 19:14:54 +1000 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <878wi6v8fl.fsf@benfinney.id.au> "Michel Claveau - MVP" writes: > Hi! > > > Python is interpreted > > No. Yes. The same Python code is both interpreted and compiled so as to run it. > Python is compiled (--> .pyc) The Python bytecode (the contents of the compiled ?foo.pyc? file) is then interpreted by the run-time Python interpreter, to actually run the program. -- \ ?Value your freedom or you will lose it, teaches history. | `\ ?Don't bother us with politics,? respond those who don't want | _o__) to learn.? ?Richard Stallman, 2002 | Ben Finney From python at rgbaz.eu Thu Jul 30 05:17:51 2009 From: python at rgbaz.eu (PythonAB) Date: Thu, 30 Jul 2009 11:17:51 +0200 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> >> > Hi Dave, > Your solution sort of defeats my intended purpose (sorry for not > divulging my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python > script, and look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want > a cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) > version of the application, because it'll grow the resulting file > significantly, and I don't have the experience to know how it will > run on different Linuxes. > Bye, > Ron. Hey Ron, What i usually do to accomplish this is compile the script to a .pyc just like you did and then call that pyc from another script that's not compiled. so in your case the not compiled script looks like: #!/usr/bin/env python import test_pyc.pyc then run that script... hope this helps... alternatively you might have a look at: http://www.pyinstaller.org/ gr Arno -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Thu Jul 30 05:27:51 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 30 Jul 2009 11:27:51 +0200 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: Barak, Ron wrote: > Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. Ask Google for binfmt-support. Hint: Each Python version has its own magic header. Christian From kunguz at gmail.com Thu Jul 30 05:29:28 2009 From: kunguz at gmail.com (=?ISO-8859-9?Q?Kaan_AK=DE=DDT?=) Date: Thu, 30 Jul 2009 11:29:28 +0200 Subject: Pyplot In-Reply-To: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> References: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> Message-ID: <8fa7fe790907300229i496f7886w3c92676e51e0d9fa@mail.gmail.com> Please help me I have been dealing with this little piece of code for 2 days. I am stuck and loosing time with it. plt.figure(count) plt.xlabel("Time steps") plt.ylabel("Values") plt.title("Output of ADCs") plt.plot(timing,adc0) plt.plot(timing,adc1) plt.plot(timing,adc3) plt.show() plt.clf() plt.cla() plt.close() 30 Temmuz 2009 11:02 tarihinde Kaan AK??T yazd?: > I am a newbee in Python. I have some problem with usage of Pyplot. I have a > loop that creates plot in every end of it. Problem starts here: The first > plot is fine but the second one is plotted onto the first plot. I use: > > plt.cla() > plt.draw() > plt.hold(False) > plt.close() > plt.clf() > > but nothing works. Perhaps I am having a problem with basics, please help > me if you know something about it. > > Kaan > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Thu Jul 30 05:53:03 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 30 Jul 2009 11:53:03 +0200 Subject: Can you easy_install from your localhost? In-Reply-To: <9c8c445f0907290857x27930acbvc5067a242e18ed17@mail.gmail.com> References: <9c8c445f0907290857x27930acbvc5067a242e18ed17@mail.gmail.com> Message-ID: Ronn Ross schrieb: > I have a package i would like to store locally. If it is stored locally can > I do something like ' easy_install http://localhost/package ' ? Thanks You can do: easy_install /path/to/package-0.1.tar.gz or you can put your packages into /some/directory/simple/packagename/package-0.1.tar.gz and do: easy_install -i file:///some/directory packagename Christian From steven at REMOVE.THIS.cybersource.com.au Thu Jul 30 06:10:36 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 30 Jul 2009 10:10:36 GMT Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: On Wed, 29 Jul 2009 21:34:07 -0700, Chris Rebert wrote: > The difference is that it handles complex numbers, whereas the plain > "math" module doesn't. > I would guess the reason there are separate modules is for performance, > so as to avoid having to dispatch on type at runtime. But this is only a > guess. My understanding is that it is to avoid people who don't care about complex numbers being confused when math.sqrt(-1) returns 1j instead of raising an error. -- Steven From egcalso at gmail.com Thu Jul 30 06:13:52 2009 From: egcalso at gmail.com (eman) Date: Thu, 30 Jul 2009 03:13:52 -0700 (PDT) Subject: Help on Reading Attached Image on Excel Message-ID: <7a651be8-e653-448e-91a2-84af71c5f64d@p36g2000prn.googlegroups.com> Anyone know how to read included images in Excel using Python? I've tried xlrd and pyexcelerator, but i couldn't figure out how to do it. If anyone has experience in this, please help. Thanks. From steven at REMOVE.THIS.cybersource.com.au Thu Jul 30 06:20:24 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 30 Jul 2009 10:20:24 GMT Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> Message-ID: On Thu, 30 Jul 2009 07:01:28 +0100, Barak, Ron wrote: > I wanted my application to "hide" the fact that it's a python script, > and look as much as possible like it's a compiled program. What do you think the "c" in .pyc stands for? What it's not is a compiled to machine-code stand alone executable. > The reason I don't just give my user a py file, is that I don't want > a cleaver user to change the innards of the script. It's not the clever users you need to worry about, because they'll fix your bugs. It's the not-as-clever-as-they-think-they-are users you have to worry about. But frankly, the best way to deal with them is to make it absolutely clear that you don't provide free support for modified versions of the script, and as soon as they report a problem, you diff their version with the official version. If they vary, then you start charging them. Before you go spending vast amounts of time and energy creating a fragile solution, it's worth you working out what it actually is you're trying to prevent. If it's just "I don't want anybody touching my precious, precious code!" then I have no sympathy and have no interest in helping you. If it's "The damn users have mucked up the script AGAIN and now they expect me to fix it for free!!!", then that's a social problem, not a technical one, and a social solution might save you a lot of wasted effort. -- Steven From clp2 at rebertia.com Thu Jul 30 06:20:41 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 30 Jul 2009 03:20:41 -0700 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: <50697b2c0907300320s35658775tfacc182f16ce9711@mail.gmail.com> On Thu, Jul 30, 2009 at 3:10 AM, Steven D'Aprano wrote: > On Wed, 29 Jul 2009 21:34:07 -0700, Chris Rebert wrote: > >> The difference is that it handles complex numbers, whereas the plain >> "math" module doesn't. >> I would guess the reason there are separate modules is for performance, >> so as to avoid having to dispatch on type at runtime. But this is only a >> guess. > > My understanding is that it is to avoid people who don't care about > complex numbers being confused when math.sqrt(-1) returns 1j instead of > raising an error. That seems a better and more plausible rationale than my guess. Kudos. - Chris -- http://blog.rebertia.com From bieffe62 at gmail.com Thu Jul 30 06:29:24 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 03:29:24 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> On Jul 30, 5:52?am, NighterNet wrote: > I am trying to figure out how to send text or byte in python 3.1. I am > trying to send data to flash socket to get there. I am not sure how to > work it. > > buff= 'id=' , self.id , ':balive=False\n' > clientSock.send(buff); Try putting a 'b' before the constant string that you want to send: >>> type(b'123') or use something like this to convert non constant strings (with only ASCII characters) into bytes: >>> s = "A non-constant string : %d " % n >>> s 'A non-constant string : 12 ' >>> type(s) >>> b = bytes ( ord(c) for c in s ) >>> b b'A non-constant string : 12 ' You could also use struct.pack , that in python 3.x returns bytes and not strings. In this case you need to specify the size of the string (extra bytes are zero-filled): import struct >>> struct.pack( "30s", "A non-constant string : %d " % n ) b'A non-constant string : 12 \x00\x00\x00' Ciao ----- FB From marco at sferacarta.com Thu Jul 30 06:29:50 2009 From: marco at sferacarta.com (Marco Mariani) Date: Thu, 30 Jul 2009 12:29:50 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: r wrote: > My adventures in Ruby. Oh, it's you. Good boy. Now, why don't you have a look at javascript and come back in six months? Or better yet, haskell and twelve months. thanks From wolfgang at rohdewald.de Thu Jul 30 06:35:22 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 12:35:22 +0200 Subject: New implementation of re module In-Reply-To: References: <4A6DD6FB.8040609@mrabarnett.plus.com> Message-ID: <200907301235.22563.wolfgang@rohdewald.de> On Tuesday 28 July 2009, Christopher Arndt wrote: > setup(name='regex', > version='1.0', > py_modules = ['regex'], > ext_modules=[Extension('_regex', ['_regex.c'])], > ) > > Also, you need to copy "unicodedata_db.h" from the "Modules" > directory of the Python source tree to your working directory, > since this file apparently is not installed into the include > directory of a Python installation. using issue2636-20090729.zip I have Python 2.6.2 on ubuntu 9.04 with ggc-4.3: gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o build/temp.linux-i686-2.6/_regex.o _regex.c: In Funktion ?bmatch_context?: _regex.c:1462: Fehler: Als Erh?hungsoperand wird L-Wert erfordert _regex.c:1470: Fehler: Als Erh?hungsoperand wird L-Wert erfordert _regex.c:1478: Fehler: Als Verringerungsoperand wird L-Wert erfordert with gcc-4.4: gcc-4.4 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict- prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o build/temp.linux-i686-2.6/_regex.o _regex.c: In function ?bmatch_context?: _regex.c:1462: error: lvalue required as increment operand _regex.c:1470: error: lvalue required as increment operand _regex.c:1478: error: lvalue required as decrement operand -- Wolfgang From Ron.Barak at lsi.com Thu Jul 30 06:42:52 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 11:42:52 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> ________________________________ From: PythonAB [mailto:python at rgbaz.eu] Sent: Thursday, July 30, 2009 12:18 To: Barak, Ron Cc: 'Dave Angel'; 'python-list at python.org' Subject: Re: Run pyc file without specifying python path ? Hi Dave, Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. Bye, Ron. Hey Ron, What i usually do to accomplish this is compile the script to a .pyc just like you did and then call that pyc from another script that's not compiled. so in your case the not compiled script looks like: #!/usr/bin/env python import test_pyc.pyc then run that script... hope this helps... alternatively you might have a look at: http://www.pyinstaller.org/ gr Arno [BR] Thanks for the answer Arno. But, I wonder - How is your suggestion, which to the user would look like: python wrapper.py different (from the user's point-of-view) than the following: python test_pyc.pyc ? I'm trying to avoide having to write python on the command line. Of course, I could write the wrapper is bourne shell, but that interoduces other problems (especially when considering that my real application needs to parse many command line options). . Bye, Ron. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bieffe62 at gmail.com Thu Jul 30 06:49:36 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 03:49:36 -0700 (PDT) Subject: Python processors? : WAS Re: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <2d8298b9-b857-4b3d-ae63-2056d80cdf7f@d4g2000yqa.googlegroups.com> On Jul 30, 11:10?am, Christian Heimes wrote: > Martin P. Hellwig wrote: > > Well the pyc, which I thought was the Python bytecode, is then > > interpreted by the VM. > > Python is often referred as byte-code interpreted language. Most modern > languages are interpreted languages. The list [1] is rather long. > Technically speaking even native code is interpreted by the micro code > of most CPUs. > > [1]http://en.wikipedia.org/wiki/Interpreted_language > [2]http://en.wikipedia.org/wiki/Microcode Once upon a time there where lisp machines, whith processors designed to fastly execute lisp code ... I worked with one of them for 2 years. I wonder: has anybody thought of making a python-machine, or at least a processor able to directly execute high-level bytecode (LLVM-like?). I think the main feature of such a machine should be hyper-fast hash lookup. Then dynamic memory management hardware ... Ciao ----- FB From clp2 at rebertia.com Thu Jul 30 06:57:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 30 Jul 2009 03:57:40 -0700 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> Message-ID: <50697b2c0907300357ld78846ch257547666a453829@mail.gmail.com> On Thu, Jul 30, 2009 at 3:42 AM, Barak, Ron wrote: > > > ________________________________ > From: PythonAB [mailto:python at rgbaz.eu] > Sent: Thursday, July 30, 2009 12:18 > To: Barak, Ron > Cc: 'Dave Angel'; 'python-list at python.org' > Subject: Re: Run pyc file without specifying python path ? > > > Hi Dave, > Your solution sort of defeats my intended purpose (sorry for not divulging > my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python script, and > look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want a > cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) version of the > application, because it'll grow the resulting file significantly, and I > don't have the experience to know how it will run on different Linuxes. > Bye, > Ron. > > Hey Ron, > What i usually do to accomplish this is compile the script to a .pyc just > like you > did and then call that pyc from another script that's not compiled. > so in your case the not compiled script looks like: > #!/usr/bin/env python > import test_pyc.pyc > > > then run that script... > hope this helps... > > > alternatively you might have a look at: > http://www.pyinstaller.org/ > > > gr > Arno > [BR]?Thanks for the answer Arno. But, I wonder?- > > How is your suggestion, which to the user would look like: > python wrapper.py No, with the shebang line (and assuming execute permissions on the file), it would look like: ./wrapper.py (or just `wrapper.py` depending on whether the file is placed in the $PATH) Cheers, Chris -- http://blog.rebertia.com From python at mrabarnett.plus.com Thu Jul 30 07:05:58 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 12:05:58 +0100 Subject: New implementation of re module In-Reply-To: <200907301235.22563.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> Message-ID: <4A717E96.3020900@mrabarnett.plus.com> Wolfgang Rohdewald wrote: > On Tuesday 28 July 2009, Christopher Arndt wrote: >> setup(name='regex', >> version='1.0', >> py_modules = ['regex'], >> ext_modules=[Extension('_regex', ['_regex.c'])], >> ) >> >> Also, you need to copy "unicodedata_db.h" from the "Modules" >> directory of the Python source tree to your working directory, >> since this file apparently is not installed into the include >> directory of a Python installation. > > using issue2636-20090729.zip > > I have Python 2.6.2 on ubuntu 9.04 > > with ggc-4.3: > gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - > Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o > build/temp.linux-i686-2.6/_regex.o > _regex.c: In Funktion ?bmatch_context?: > _regex.c:1462: Fehler: Als Erh?hungsoperand wird L-Wert erfordert > _regex.c:1470: Fehler: Als Erh?hungsoperand wird L-Wert erfordert > _regex.c:1478: Fehler: Als Verringerungsoperand wird L-Wert erfordert > > with gcc-4.4: > gcc-4.4 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict- > prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o > build/temp.linux-i686-2.6/_regex.o > _regex.c: In function ?bmatch_context?: > _regex.c:1462: error: lvalue required as increment operand > _regex.c:1470: error: lvalue required as increment operand > _regex.c:1478: error: lvalue required as decrement operand > There are other lines which are similar, eg line 1487. Do they all give the same/similar error with your compiler? From masklinn at masklinn.net Thu Jul 30 07:36:49 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 13:36:49 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> On 30 Jul 2009, at 06:04 , alex23 wrote: > On Jul 30, 1:06 pm, r wrote: >> 1.) No need to use "()" to call a function with no arguments. >> Python --> "obj.m2().m3()" --ugly >> Ruby --> "obj.m1.m2.m3" -- sweeet! >> Man, i must admit i really like this, and your code will look so much >> cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? > As chris pointed out, there's a method for that, to which a symbol is provided: `Object#method(methodname)`, which is basically equivalent to `getattr(object, methodname)` in Python. >> 2.) the .each method >> container.each{|localVar| block} >> This method can really cleanup some ugly for loops, although i really >> like the readability of for loops. > > map(lambda localVar: , sequence) > > or: > > def usefully_named_func(var): > > return var > > transformed = [usefully_named_func(v) for v in sequence] > The issue here is of course that `map` and comprehensions are transformations. `#each` exists for effectful iterations (Ruby has `#map` for the map operation). So the intent expressed by `#each` and `map` isn't the same. Furthermore and this is the most problematic limitation of Python here, `lambda` doesn't allow complex transformations due to its restrictions, so one has to switch to named functions which works but isn't sexy (and tends to lower readability imo). >> 3.) true OOP >> Now before you go and get all "huffy" over this statement, hear me >> out. Python is the best language in the world. But it damn sure has >> some warts! "len(this)" instead of "obj.length" max(that) instead of >> [1,2,3,4,5].max(). > > As the Zen says: '[P]racticality beats purity'. Personally, I'm not > sure how a handful of convenient built-in functions make a language in > which _everything is an object_ somehow "false" OO. > That's an interesting point, but not relevant at the end of the day: `foo.length` and `length(foo)` have the same "practicality". On the other hand Ruby can be praised for the coherence: everything's a method period end of the story; while Python does have a dichotomy between methods and functions/generic methods (whether or not that dichotomy bothers users is another issue). > With max(), this is a built-in that takes _any_ iterable and an > optional key function, and returns the highest value as per the key. > This means that _every_ iterable object - as _well_ as every object > that supports enough of the iterator protocol - can be handed to max() > and a result obtained. So at best, I just need to make sure my new > sequence-type provides the iterator protocol and viola, it works with > max() without me having to hand-code a .max() that's specialised for > my new type, and without Python forcing me to have a complex > inheritance chain just to make sure I include the right > MaxableSequence ancestor to inherit the right .max(). > Well interestingly, Ruby works pretty much the same way. Where Python asks that you implement the iterator protocol to have `max` work, Ruby asks that you implement the `each` method (also called the Enumerable protocol) and mixin `Enumerable` (http://ruby-doc.org/core/classes/Enumerable.html ). Then you get max (and min, map, etc?) "for free". Of course you're free to re-implement all of them manually if you wish to do so (or if you need those operations to return your custom collection rather than arrays). Enumerable is merely a convenience mixin. So there's no big difference here (and note that mixins aren't necessarily part of the inheritance chain, though they're implemented that way in Ruby). Also, it's "voil?" not "viola" (a viola is a bowed string instrument, slightly bigger than a violin, whereas voil? is a french interjection used to call attention or suggest the appearance of something, as if by magic). >> PS stay tuned for more from this series.... > > Is this going to be more of you telling us - without any apparent > irony whatsoever - how Ruby has some valid points after all your > vilification last year when we said the same to you? If so, where can > I sign up?! I fear I'll have to agree with that sentiment. From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 30 07:40:05 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 30 Jul 2009 13:40:05 +0200 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: <87ljm72h04.fsf@benfinney.id.au> References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> <87ljm72h04.fsf@benfinney.id.au> Message-ID: <4a718695$0$2760$426a34cc@news.free.fr> Ben Finney a ?crit : > dandi kain writes: > >> What is the functionality of __ or _ , leading or trailing an object , >> class ot function ? Please note that in Python, classes and functions are objects too. But anyway, these underscores are part of *identifiers*, not objects themselves !-) > foo Ordinary name, part of public interface > _foo Ordinary name, part of internal-only interface > __foo Ordinary name, but will be mangled (this style used rarely) > __foo__ Name which is used in a special way by Python And FWIW: foo_ When you want to use a reserved name for identifier (ie: 'class_' , 'or_', 'and_' etc) From no.email at please.post Thu Jul 30 08:02:06 2009 From: no.email at please.post (kj) Date: Thu, 30 Jul 2009 12:02:06 +0000 (UTC) Subject: How to "gunzip-iterate" over a file? References: <7x8wi7j80g.fsf@ruckus.brouhaha.com> Message-ID: Robert, Paul, thanks. That was just what I was looking for. kynn From user at example.net Thu Jul 30 08:03:38 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 14:03:38 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> Masklinn wrote: ... > That's an interesting point, but not relevant at the end of the day: > `foo.length` and `length(foo)` have the same "practicality". On the > other hand Ruby can be praised for the coherence: everything's a method > period end of the story; while Python does have a dichotomy between > methods and functions/generic methods (whether or not that dichotomy > bothers users is another issue). ... how would you correct a newbie (like me) who says: "well, the main difference between a function and a method (from the caller's pow) is a syntactic one: fun(obj, arguments) as opposed to: obj.met(arguments) but the effect is just about the same." ? bye From Ray.Joseph at CDICorp.com Thu Jul 30 08:17:05 2009 From: Ray.Joseph at CDICorp.com (ray) Date: Thu, 30 Jul 2009 05:17:05 -0700 (PDT) Subject: Looking for a dream language: sounds like Python to me. References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: <976e1fea-4f40-49be-81e1-493456fcf52d@o36g2000vbl.googlegroups.com> > Where can I find a Python functionality like simulink ?? Stef, I saw this at: http://showmedo.com/videotutorials/video?name=7430000&fromSeriesID=743 Ray From jeanmichel at sequans.com Thu Jul 30 08:24:39 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 30 Jul 2009 14:24:39 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> References: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> Message-ID: <4A719107.9020500@sequans.com> superpollo wrote: > Masklinn wrote: > ... >> That's an interesting point, but not relevant at the end of the day: >> `foo.length` and `length(foo)` have the same "practicality". On the >> other hand Ruby can be praised for the coherence: everything's a >> method period end of the story; while Python does have a dichotomy >> between methods and functions/generic methods (whether or not that >> dichotomy bothers users is another issue). > ... > how would you correct a newbie (like me) who says: > > "well, the main difference between a function and a method (from the > caller's pow) is a syntactic one: > > fun(obj, arguments) > > as opposed to: > > obj.met(arguments) > > but the effect is just about the same." > > ? > > bye My suggestion - OO programming: length(foo) calls the foo instance(or class for some language) length method length(bar) calls the bar instance length method, it is **not** the same than the foo method - non OO programming: length(foo) calls the length function with foo as parameter length(bar) calls the **same** length function with the bar parameter (so you better make sure length is handling both foo and bar) So the difference is definitely **not** a syntactic one, but it is true that the syntax obj.method() better illustrates the bind mechanism of the OO programming. JM From clp2 at rebertia.com Thu Jul 30 08:27:12 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 30 Jul 2009 05:27:12 -0700 Subject: Python processors? : WAS Re: Does python have the capability for driver development ? In-Reply-To: <2d8298b9-b857-4b3d-ae63-2056d80cdf7f@d4g2000yqa.googlegroups.com> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <2d8298b9-b857-4b3d-ae63-2056d80cdf7f@d4g2000yqa.googlegroups.com> Message-ID: <50697b2c0907300527o37e2353vb2fa7d62e2f9ebdd@mail.gmail.com> On Thu, Jul 30, 2009 at 3:49 AM, Francesco Bochicchio wrote: > On Jul 30, 11:10?am, Christian Heimes wrote: >> Martin P. Hellwig wrote: >> > Well the pyc, which I thought was the Python bytecode, is then >> > interpreted by the VM. >> >> Python is often referred as byte-code interpreted language. Most modern >> languages are interpreted languages. The list [1] is rather long. >> Technically speaking even native code is interpreted by the micro code >> of most CPUs. >> >> [1]http://en.wikipedia.org/wiki/Interpreted_language >> [2]http://en.wikipedia.org/wiki/Microcode > > Once upon a time there where lisp machines, whith processors designed > to fastly execute lisp code ?... I worked with one of them for 2 > years. > I wonder: has anybody thought of making a python-machine, or at least > a processor able to directly execute high-level bytecode (LLVM-like?). > I think the main feature of such a machine should be hyper-fast hash > lookup. Then dynamic memory management hardware ... Yes, it was considered a decade ago: http://mail.python.org/pipermail/python-list/1999-June/004451.html Cheers, Chris -- http://blog.rebertia.com From wolfgang at rohdewald.de Thu Jul 30 08:29:07 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 14:29:07 +0200 Subject: New implementation of re module In-Reply-To: <4A717E96.3020900@mrabarnett.plus.com> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> <4A717E96.3020900@mrabarnett.plus.com> Message-ID: <200907301429.07850.wolfgang@rohdewald.de> On Thursday 30 July 2009, MRAB wrote: > There are other lines which are similar, eg line 1487. Do they all > give the same/similar error with your compiler? yes. The full output with gcc-4.3: notebook:~/kmj/src$ LANG=C python setup.py build running build running build_py running build_ext building '_regex' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o build/temp.linux-i686-2.6/_regex.o _regex.c: In function 'bmatch_context': _regex.c:1462: error: lvalue required as increment operand _regex.c:1470: error: lvalue required as increment operand _regex.c:1478: error: lvalue required as decrement operand _regex.c:1487: error: lvalue required as decrement operand _regex.c:1593: error: lvalue required as increment operand _regex.c:1606: error: lvalue required as decrement operand _regex.c:1616: error: lvalue required as increment operand _regex.c:1625: error: lvalue required as increment operand _regex.c:1634: error: lvalue required as decrement operand _regex.c:1643: error: lvalue required as decrement operand _regex.c:2036: error: lvalue required as increment operand _regex.c:2047: error: lvalue required as increment operand _regex.c:2059: error: lvalue required as decrement operand _regex.c:2070: error: lvalue required as decrement operand _regex.c:2316: error: lvalue required as increment operand In file included from _regex.c:2431: _regex.c: In function 'umatch_context': _regex.c:1462: error: lvalue required as increment operand _regex.c:1470: error: lvalue required as increment operand _regex.c:1478: error: lvalue required as decrement operand _regex.c:1487: error: lvalue required as decrement operand _regex.c:1593: error: lvalue required as increment operand _regex.c:1606: error: lvalue required as decrement operand _regex.c:1616: error: lvalue required as increment operand _regex.c:1625: error: lvalue required as increment operand _regex.c:1634: error: lvalue required as decrement operand _regex.c:1643: error: lvalue required as decrement operand _regex.c:2036: error: lvalue required as increment operand _regex.c:2047: error: lvalue required as increment operand _regex.c:2059: error: lvalue required as decrement operand _regex.c:2070: error: lvalue required as decrement operand _regex.c:2316: error: lvalue required as increment operand error: command 'gcc' failed with exit status 1 -- Wolfgang From masklinn at masklinn.net Thu Jul 30 08:30:48 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 14:30:48 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> References: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> Message-ID: On 30 Jul 2009, at 14:03 , superpollo wrote: > Masklinn wrote: > ... >> That's an interesting point, but not relevant at the end of the >> day: `foo.length` and `length(foo)` have the same "practicality". >> On the other hand Ruby can be praised for the coherence: >> everything's a method period end of the story; while Python does >> have a dichotomy between methods and functions/generic methods >> (whether or not that dichotomy bothers users is another issue). > ... > how would you correct a newbie (like me) who says: > > "well, the main difference between a function and a method (from the > caller's pow) is a syntactic one: > > fun(obj, arguments) > > as opposed to: > > obj.met(arguments) > > but the effect is just about the same." > > ? Depending on the way things are implemented, it can be very similar (see CLOS' generic functions, which have the same look as functions but dispatch based on parameter types). In Python, the difference would be that functions don't automatically dispatch (you have a function for everybody, and any dispatch you perform based on argument types or other qualifiers has to be done manually) whereas methods do dispatch on the first [implied] argument (you execute a precise method corresponding to the object it's being called on). So fun(obj, *arguments) will call the same `fun` whether `obj` is an int, a string or an HTTPFactory whereas obj.met(*arguments) will call a different `met` each time (they all have the same "unqualified" name [and signature if it's part of a protocol], but might very well be implemented completely differently). Therefore -- in python -- the effect isn't anywhere "just about the same". From beldar.cat at gmail.com Thu Jul 30 08:40:41 2009 From: beldar.cat at gmail.com (Beldar) Date: Thu, 30 Jul 2009 05:40:41 -0700 (PDT) Subject: Regexp problem Message-ID: Hi there! I have a problem and i'm not very good at regular expressions. I have a text like "lalala lalala tiruri beldar-is-listening tiruri lalala" I need a regexp to get the 'beldar' part, the format is 'something-is-listening', i need to get the something part, use it in my code, and then replace the whole 'something-is-listening' for another string. Someone can help me please? Thank you! From nobody at nowhere.com Thu Jul 30 08:43:29 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 30 Jul 2009 13:43:29 +0100 Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: On Wed, 29 Jul 2009 23:24:11 -0500, pdlemper wrote: > The following numerical approximation for Euler's Gamma function > is found in http://en.wikipedia.org/wiki/Lanczos_approximation > > from cmath import * > This works in Python 3.0 > > But I can't figure out where it gets "cmath". Searching the Python > directory reveals no more than a test_cmath. The only cmath I can find > is a header file in another directory turboc++\Borland\include\ > > dir(cmath) reveals 23 functions overlapping the 37 functions of > the math module. > > What is cmath, where did it come from and how does it differ from > the standard math module ? If you're looking for the file, it's written in C, so it's typically a dynamic library, e.g.: /usr/lib/python2.5/lib-dynload/cmath.so You won't find a cmath.py file. From nytrokiss at gmail.com Thu Jul 30 08:45:09 2009 From: nytrokiss at gmail.com (James Matthews) Date: Thu, 30 Jul 2009 15:45:09 +0300 Subject: Image merging Message-ID: <8a6b8e350907300545i48de1284h82e3c4d9d249ff8@mail.gmail.com> Hi, I need to create an app that takes to images and mergers them. After googling around I have found 4 graphic library's that I can use. However since I am new to "image programming" can you please tell me which one you think would be the best. The ones I found were cairographics PIL Imagemagick GraphicsMagick Any tips? Thanks James -- http://www.goldwatches.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at rgbaz.eu Thu Jul 30 08:54:21 2009 From: python at rgbaz.eu (PythonAB) Date: Thu, 30 Jul 2009 14:54:21 +0200 Subject: Run pyc file without specifying python path ? In-Reply-To: <50697b2c0907300357ld78846ch257547666a453829@mail.gmail.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> <50697b2c0907300357ld78846ch257547666a453829@mail.gmail.com> Message-ID: <736F147B-47A0-4559-8FC3-4134068B5F09@rgbaz.eu> On 30 jul 2009, at 12:57, Chris Rebert wrote: > On Thu, Jul 30, 2009 at 3:42 AM, Barak, Ron wrote: >> >> >> ________________________________ >> From: PythonAB [mailto:python at rgbaz.eu] >> Sent: Thursday, July 30, 2009 12:18 >> To: Barak, Ron >> Cc: 'Dave Angel'; 'python-list at python.org' >> Subject: Re: Run pyc file without specifying python path ? >> >> >> Hi Dave, >> Your solution sort of defeats my intended purpose (sorry for not >> divulging >> my 'hidden agenda'). >> I wanted my application to "hide" the fact that it's a python >> script, and >> look as much as possible like it's a compiled program. >> The reason I don't just give my user a py file, is that I don't >> want a >> cleaver user to change the innards of the script. >> On the other hand, I don't want to make a compiled (freezed?) >> version of the >> application, because it'll grow the resulting file significantly, >> and I >> don't have the experience to know how it will run on different >> Linuxes. >> Bye, >> Ron. >> >> Hey Ron, >> What i usually do to accomplish this is compile the script to >> a .pyc just >> like you >> did and then call that pyc from another script that's not compiled. >> so in your case the not compiled script looks like: >> #!/usr/bin/env python >> import test_pyc.pyc >> >> >> then run that script... >> hope this helps... >> >> >> alternatively you might have a look at: >> http://www.pyinstaller.org/ >> >> >> gr >> Arno >> [BR] Thanks for the answer Arno. But, I wonder - >> >> How is your suggestion, which to the user would look like: >> python wrapper.py > > No, with the shebang line (and assuming execute permissions on the > file), it would look like: > > ./wrapper.py > > (or just `wrapper.py` depending on whether the file is placed in the > $PATH) > > Cheers, > Chris > yes exactly, chris you can distribute a pyc containing your program, and have the wrapper.py execute it gr Arno From python at mrabarnett.plus.com Thu Jul 30 08:56:32 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 13:56:32 +0100 Subject: New implementation of re module In-Reply-To: <200907301429.07850.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> <4A717E96.3020900@mrabarnett.plus.com> <200907301429.07850.wolfgang@rohdewald.de> Message-ID: <4A719880.3070300@mrabarnett.plus.com> Wolfgang Rohdewald wrote: > On Thursday 30 July 2009, MRAB wrote: >> There are other lines which are similar, eg line 1487. Do they all >> give the same/similar error with your compiler? > > yes. The full output with gcc-4.3: > > > notebook:~/kmj/src$ LANG=C python setup.py build > running build > running build_py > running build_ext > building '_regex' extension > gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - > Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o > build/temp.linux-i686-2.6/_regex.o > _regex.c: In function 'bmatch_context': > _regex.c:1462: error: lvalue required as increment operand [snip] > error: command 'gcc' failed with exit status 1 > So it complains about: ++(RE_CHAR*)context->text_ptr but not about: ++info->repeat.count Does this mean that the gcc compiler thinks that the cast makes it an rvalue? I'm using Visual C++ 2008 Express Edition, which doesn't complain. What does the C standard say? From davea at dejaviewphoto.com Thu Jul 30 09:02:55 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Thu, 30 Jul 2009 09:02:55 -0400 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: <4A7199FF.8010202@dejaviewphoto.com> Barak, Ron wrote: >> -----Original Message----- >> From: Dave Angel [mailto:davea at ieee.org] >> Sent: Wednesday, July 29, 2009 21:05 >> To: Barak, Ron >> Cc: 'python-list at python.org' >> Subject: Re: Run pyc file without specifying python path ? >> >> Barak, Ron wrote: >> >>> Hi, >>> >>> I wanted to make a python byte-code file executable, >>> >> expecting to be able to run it without specifying "python" on >> the (Linux bash) command line. >> >>> So, I wrote the following: >>> >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python >>> >>> print "hello" >>> [root at VMLinux1 python]# >>> >>> and made its pyc file executable: >>> >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc >>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc >>> [root at VMLinux1 python]# >>> >>> So, I see: >>> >>> [root at VMLinux1 python]# file test_pyc.py* >>> test_pyc.py: a python script text executable >>> test_pyc.pyc: python 2.3 byte-compiled >>> [root at VMLinux1 python]# >>> >>> If I try to do the following, no problem: >>> >>> [root at VMLinux1 python]# python test_pyc.pyc hello >>> [root at VMLinux1 python]# >>> >>> However, the following fails: >>> >>> [root at VMLinux1 python]# ./test_pyc.pyc >>> -bash: ./test_pyc.pyc: cannot execute binary file >>> [root at VMLinux1 python]# >>> >>> Is there a way to run a pyc file without specifying the >>> >> python path ? >> >>> Bye, >>> Ron. >>> >>> >>> >> I don't currently run Unix, but I think I know the problem. >> >> In a text file, the shell examines the first line, and if it >> begins #! >> it's assumed to point to the executable of an interpreter for >> that text file. Presumably the same trick doesn't work for a >> .pyc file. >> >> Why not write a trivial wrapper.py file, don't compile it, >> and let that invoke the main code in the .pyc file? >> >> Then make wrapper.py executable, and you're ready to go. >> >> DaveA >> >> >> > > Hi Dave, > Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. > Bye, > Ron. > Most of the other answers basically paraphrased my suggestion of making a wrapper file, not compiling it, and making it executable. With that approach, the user just types "wrapper.py" on his command line. And wrapper.py only needs two lines, a shebang, and an import, no big deal if the user modifies it. The rest of your code can be .pyc files. Steven makes some good points. You have to define what level of clever you're protecting from. A determined hacker will get in no matter what you do, unless you want to ship the program in a proprietary embedded system, encased in epoxy. Further, if you have an extension of .py or .pyc, a knowledgeable hacker will know it's probably python. You imply you want it to run unmodifed on multiple unknown Linux versions. I think that lets out binfmt solutions. That means you need to test and support not only multiple Linux implementations, but multiple Python versions, because who knows what the user may have installed. I think you need to rethink your support strategy. And maybe concentrate on being able to detect change, rather than prevent it. DaveA From python.list at tim.thechases.com Thu Jul 30 09:04:24 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 30 Jul 2009 08:04:24 -0500 Subject: Regexp problem In-Reply-To: References: Message-ID: <4A719A58.4020509@tim.thechases.com> > I have a problem and i'm not very good at regular expressions. > I have a text like "lalala lalala tiruri beldar-is-listening tiruri > lalala" I need a regexp to get the 'beldar' part, the format is > 'something-is-listening', i need to get the something part, use it in > my code, and then replace the whole 'something-is-listening' for > another string. Pretty easy: >>> import re >>> s = "lalala lalala tiruri beldar-is-listening tiruri lalala" >>> r = re.compile(r'(\w+)-is-listening') >>> r.search(s).group(1) 'beldar' >>> r.sub('this is a replacement', s) 'lalala lalala tiruri this is a replacement tiruri lalala' -tkc From python at mrabarnett.plus.com Thu Jul 30 09:07:51 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 14:07:51 +0100 Subject: Regexp problem In-Reply-To: References: Message-ID: <4A719B27.8000404@mrabarnett.plus.com> Beldar wrote: > Hi there! > > I have a problem and i'm not very good at regular expressions. > I have a text like "lalala lalala tiruri beldar-is-listening tiruri > lalala" I need a regexp to get the 'beldar' part, the format is > 'something-is-listening', i need to get the something part, use it in > my code, and then replace the whole 'something-is-listening' for > another string. > \w+ will match a word and enclosing it in (...) will capture what was matched: m = re.search(r"(\w+)-is-listening", text) print "Captured '%s'" % m.group(1) print "Matched from %d to %d" % (m.start(), m.end()) From wolfgang at rohdewald.de Thu Jul 30 09:18:27 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 15:18:27 +0200 Subject: New implementation of re module In-Reply-To: <4A719880.3070300@mrabarnett.plus.com> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301429.07850.wolfgang@rohdewald.de> <4A719880.3070300@mrabarnett.plus.com> Message-ID: <200907301518.28122.wolfgang@rohdewald.de> On Thursday 30 July 2009, MRAB wrote: > So it complains about: > > ++(RE_CHAR*)context->text_ptr > > but not about: > > ++info->repeat.count > > Does this mean that the gcc compiler thinks that the cast makes it > an rvalue? I'm using Visual C++ 2008 Express Edition, which doesn't > complain. What does the C standard say? I am not really a C expert but I found some links. Most helpful: http://developer.apple.com/DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/C-Dialect-Options.html (search -fnon-lvalue-assign) so I did the conversion mentioned there. This works: --- _regex.c 2009-07-29 11:34:00.000000000 +0200 +++ n 2009-07-30 15:15:22.000000000 +0200 @@ -1459,7 +1459,7 @@ if (text_ptr < (RE_CHAR*)context->slice_end && text_ptr[0] != '\n') { context->node = node->next_1; - ++(RE_CHAR*)context->text_ptr; + ++*(RE_CHAR**)&context->text_ptr; } else context = reject_context(state, context); break; -- Wolfgang From wfluro at gmail.com Thu Jul 30 09:19:29 2009 From: wfluro at gmail.com (ulf) Date: Thu, 30 Jul 2009 06:19:29 -0700 (PDT) Subject: Python ctypes on win64 References: Message-ID: <433d4958-81e4-4d5f-be51-9d1b2170d1eb@c2g2000yqi.googlegroups.com> On Jul 25, 3:09?am, a... at pythoncraft.com (Aahz) wrote: > In article , Thanks, I'll try there. > > ulf ? wrote: > > >Does anybody know if python includes a win64 version ofctypes? > > >According to the documentation it should be included - at least > >there's no special remarks for win64, and I haven't found any recent > >notes saying that it shouldn't work on win64. Yet it looks like both > >the installer from Python.org and from ActiveState.com have disabled > >it. > > Nobody seems to have followed up to this, you may have better luck on > the capi-sig mailing list. > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "At Resolver we've found it useful to short-circuit any doubt and just ? ? ? ? > refer to comments in code as 'lies'. :-)" > --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From davea at ieee.org Thu Jul 30 09:20:45 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 09:20:45 -0400 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <4A719E2D.708@ieee.org> Martin P. Hellwig wrote: >
Michel > Claveau - MVP wrote: >> Hi! >> >>> Python is interpreted >> >> No. Python is compiled (--> .pyc) >> But the term "to compile" is not always unambiguous... >> And the notion of "compiler" is not attached to Python (the >> language), but is attached to the implementation. >> >> @+ >> >> MCI > > Well the pyc, which I thought was the Python bytecode, is then > interpreted by the VM. > As Michel says, "to compile" is not always unambiguous. My definition includes a one-way transformation from human-readable source text into something that can be more efficiently interpreted by other code, or by hardware. The compiler really doesn't care whether the machine it's targeting is real or virtual. The CPython implementation of Python compiles the source text into a bytecode file, with extension .pyc. That certainly is a compilation step. Followed (much) later by an interpreted one. To pick a specific implementation of C++, Microsoft C++ compiles C++ source text into an "executable file," with extension .exe (I'm ignoring little details, like the linker). That's a compilation step. Then the exe file is (later) interpreted by the microcode on the Pentium chip. As far as I know, nobody has yet built a microcode implementation of a Python VM (Virtual Machine). Nor have I seen one for the Java VM. However, in the early 80's there was a microcode implementation of the P-system VM. It was never a commercial success, but it existed. And there have been at least three Forth machines, where the hardware itself was designed to support the language's VM. No microcode at all. DaveA From wolfgang at rohdewald.de Thu Jul 30 09:24:28 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 15:24:28 +0200 Subject: New implementation of re module In-Reply-To: <200907301518.28122.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <4A719880.3070300@mrabarnett.plus.com> <200907301518.28122.wolfgang@rohdewald.de> Message-ID: <200907301524.28703.wolfgang@rohdewald.de> On Thursday 30 July 2009, Wolfgang Rohdewald wrote: > so I did the conversion mentioned there. This works: I actually do not know if it works - but it compiles. -- Wolfgang From beldar.cat at gmail.com Thu Jul 30 09:32:59 2009 From: beldar.cat at gmail.com (Beldar) Date: Thu, 30 Jul 2009 06:32:59 -0700 (PDT) Subject: Regexp problem References: Message-ID: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> On 30 jul, 15:07, MRAB wrote: > Beldar wrote: > > Hi there! > > > I have a problem and i'm not very good at regular expressions. > > I have a text like "lalala lalala tiruri beldar-is-listening tiruri > > lalala" I need a regexp to get the 'beldar' part, the format is > > 'something-is-listening', i need to get the something part, use it in > > my code, and then replace the whole 'something-is-listening' for > > another string. > > \w+ will match a word and enclosing it in (...) will capture what was > matched: > > ? ? ?m = re.search(r"(\w+)-is-listening", text) > ? ? ?print "Captured '%s'" % m.group(1) > ? ? ?print "Matched from %d to %d" % (m.start(), m.end()) Ok, thank you all, it was very helpful! From ccurvey at gmail.com Thu Jul 30 09:35:46 2009 From: ccurvey at gmail.com (Chris Curvey) Date: Thu, 30 Jul 2009 06:35:46 -0700 (PDT) Subject: PyPDF and print restrictions References: <90034475-cf07-4a57-b8cf-ec2949e0e544@d23g2000vbm.googlegroups.com> Message-ID: <13cc96b6-7c1b-4835-b711-eabedb4d6442@f10g2000vbf.googlegroups.com> On Jul 27, 4:16?pm, Chris Curvey wrote: > Has anyone out there been able to enforce print restrictions on a PDF > document by usingPyPDF? The documentation for "encrypt" states: > > ?# @param user_pwd The "user password", which allows for opening and > reading > ?# the PDF file with the restrictions provided. > > But there is no parameter for providing a restriction, and I can't > find a reference to any kind of restriction besides this comment in > the docs. > > Thanks in advance! I found some (I think old-ish) documentation on encryption and permissions (of course, I've lost the link). I think there are additional permissions that have been implemented since the doc that I found, but this patch works for me. Index: C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py =================================================================== --- C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py (revision 1) +++ C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py (revision 2) @@ -118,7 +118,10 @@ # @param use_128bit Boolean argument as to whether to use 128bit # encryption. When false, 40bit encryption will be used. By default, this # flag is on. - def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True): + # @param perm_mask bitmask of the permissions that should be granted. + # Defaults to -1, which is "everything permitted" + def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True, + perm_mask=-1): import md5, time, random if owner_pwd == None: owner_pwd = user_pwd @@ -130,8 +133,8 @@ V = 1 rev = 2 keylen = 40 / 8 - # permit everything: - P = -1 + + P = perm_mask O = ByteStringObject(_alg33(owner_pwd, user_pwd, rev, keylen)) ID_1 = md5.new(repr(time.time())).digest() ID_2 = md5.new(repr(random.random())).digest() Index: C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py =================================================================== --- C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py (revision 1) +++ C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py (revision 2) @@ -1,2 +1,11 @@ from pdf import PdfFileReader, PdfFileWriter + +PERM_NONE = 0 +PERM_PRINT = 2 +PERM_MODIFY = 4 +PERM_COPY_TEXT = 8 +PERM_ANNOTATE = 16 + +PERM_ALL = PERM_PRINT | PERM_MODIFY | PERM_COPY_TEXT | PERM_ANNOTATE + __all__ = ["pdf"] From marcusw at cox.net Thu Jul 30 09:38:49 2009 From: marcusw at cox.net (Marcus Wanner) Date: Thu, 30 Jul 2009 09:38:49 -0400 Subject: Regexp problem In-Reply-To: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> Message-ID: On 7/30/2009 9:32 AM, Beldar wrote: > On 30 jul, 15:07, MRAB wrote: >> Beldar wrote: >>> Hi there! >>> I have a problem and i'm not very good at regular expressions. >>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>> lalala" I need a regexp to get the 'beldar' part, the format is >>> 'something-is-listening', i need to get the something part, use it in >>> my code, and then replace the whole 'something-is-listening' for >>> another string. >> \w+ will match a word and enclosing it in (...) will capture what was >> matched: >> >> m = re.search(r"(\w+)-is-listening", text) >> print "Captured '%s'" % m.group(1) >> print "Matched from %d to %d" % (m.start(), m.end()) > > Ok, thank you all, it was very helpful! Wow, I really need to learn more about regexp... Any tutorials you guys can recommend? Marcus From piet at cs.uu.nl Thu Jul 30 09:39:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 30 Jul 2009 15:39:35 +0200 Subject: New implementation of re module References: Message-ID: >>>>> MRAB (M) wrote: >M> Hi all, >M> I've been working on a new implementation of the re module. The details >M> are at http://bugs.python.org/issue2636, specifically from >M> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >M> Python 2.6 on Windows if you want to try it out. >M> I'm interested in how fast it is generally, compared with the current re >M> module, but especially when faced with those 'pathological' regular >M> expressions which seem to take a long time to finish, for example: >M> re.search(r"^(.+|D)*A$", "x" * 25 + "B") >M> which on my PC (1.8GHz) takes 18.98secs with the re module but <0.01secs >M> with this new implementation. Is this version also going to use the Thompson approach? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From martin.hellwig at dcuktec.org Thu Jul 30 09:43:26 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 14:43:26 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: Dave Angel wrote: Ah yes, we thread on the territory of word definition and difference in interpretation. Any argument is doomed to fail if not agreed or at least taken in perspective of the terminology used by users. I could be (well it is quite likely) wrong in my interpretation of the terminology, but here goes it anyway: Machine Code: Whatever the machine executes, it could be that the CPU uses an abstraction of microcode to do this but from the perspective of the user, this is all done in the same 'black box' Compiling: Translate words/symbols/mnemonics to machine code, which than can be either loaded, linked and executed by an OS or read and executed by the BIOS. Interpreted: Instructions which can be fed to a previous compiled program that is able to dynamically change its execution and flow without the need to recompile itself. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From piet at cs.uu.nl Thu Jul 30 09:45:44 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 30 Jul 2009 15:45:44 +0200 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> <8cb1ba65-be71-49b7-8955-54c17f351a40@w6g2000yqw.googlegroups.com> Message-ID: >>>>> Carl Banks (CB) wrote: >CB> On Jul 29, 7:14?am, Piet van Oostrum wrote: >>> >>>>> Carl Banks (CB) wrote: >>> >CB> On Jul 28, 3:15?pm, John D Giotta wrote: >>> >>> I'm looking to run a process with a limit of 3 instances, but each >>> >>> execution is over a crontab interval. I've been investigating the >>> >>> threading module and using daemons to limit active thread objects, but >>> >>> I'm not very successful at grasping the documentation. >>> >>> >>> Is it possible to do what I'm trying to do and if so anyone know of a >>> >>> useful example to get started? >>> >CB> It seems like you want to limit the number of processes to three; the >>> >CB> threading module won't help you there because it deals with threads >>> >CB> within a single process. >>> >CB> What I'd do is to simply run the system ps to see how many processes >>> >CB> are running (ps is pretty versatile on most systems and can find >>> >CB> specifically targeted processes like you program), and exit if there >>> >CB> are already three. >>> >>> That will surely run into some race conditions. >CB> What, the OS might not have gotten around to update the process table >CB> to include a process started minutes ago? (He said he was starting >CB> the processes over crontab intervals, not that he communicated what he >CB> wanted well.) No but between the time you get the ps output and decide not to start a new process one of the processes might have exited. As I said it probably is not a big deal, but you (he) should be aware of it I think. The other possible race condition: two processes starting at approximately the same time and both not detecting the other will probably not occur because of the time distance between starting the processes by cron. Unless the system is so busy that ps takes a loooong time. The problem is similar to the sleeping barber problem (3 sleeping barbers actually). -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Thu Jul 30 09:53:31 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 09:53:31 -0400 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: <4A71A5DB.60409@ieee.org> pdlemper at earthlink.net wrote: > from cmath import * > > > What is cmath, where did it come from and how does it differ from > the standard math module ? > > Dave WB3DWE > I saw the number 4 in silver, Guido > (apologies to Wm Carlos Williams) > > I'm surprised that with all the responses, nobody has pointed out the dangers involved in using from cmath import * In general, this form of import is discouraged. I presume there's no problem if you're not also importing 'math' and all your calls are intending to be for the complex versions. But if you're mixing things up, consider using the more conventional import cmath and using cmath. as a prefix before the function calls. DaveA From metolone+gmane at gmail.com Thu Jul 30 09:56:07 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Thu, 30 Jul 2009 06:56:07 -0700 Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: "NighterNet" wrote in message news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com... >I am trying to figure out how to send text or byte in python 3.1. I am > trying to send data to flash socket to get there. I am not sure how to > work it. > > buff= 'id=' , self.id , ':balive=False\n' > clientSock.send(buff); > -- > http://mail.python.org/mailman/listinfo/python-list > Python 3.1 strings are Unicode (think characters not bytes). When writing to files, sockets, etc. bytes must be used. Unicode strings have an encode() method, where you can specify an appropriate encoding (ascii, latin-1, windows-1252, utf-8, etc.): clientSock.send(buff.encode('ascii')) When reading from the socket, you can decode() the byte strings back into Unicode strings. data = clientSock.recv(1024).decode('ascii') -Mark From bieffe62 at gmail.com Thu Jul 30 10:11:24 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 07:11:24 -0700 (PDT) Subject: No PyPI search for 3.x compatible packages References: Message-ID: On 30 Lug, 01:55, Neil Hodgson wrote: > ? ?There appears to be no way to search PyPI for packages that are > compatible with Python 3.x. There are classifiers for 'Programming > Language' including 'Programming Language :: Python :: 3' but that seems > to be for implementation language since there are so many packages that > specify C. There are a total of 109 packages classified with Python :: > [3, 3.0, 3.1] out of a total of 4829 packages.http://pypi.python.org/pypi?:action=browse&show=all&c=214&c=533 > > ? ?The search box appears to search for any word entered so a search > like "xml 3.0" or "xml AND 3.0" does not help. > > ? ?Some packages include version information in the Py Version column of > their download lists or embedded in the download file names. Projects > are often constrained to a particular set of Python versions so need to > choose packages that will work with those versions. It would be helpful > if PyPI made this information more visible and searchable. > > ? ?Neil Are you sure? I note that for example pygtk has as language tags both C and python. So maybe a C extension for python3 would have both C and python 3 as language tags. I suspect that the 109 packages you found are the only ones obf the 4829 which works with python3 (but I hope to be wrong ). Ciao ----- FB From peter at peter-b.co.uk Thu Jul 30 10:17:52 2009 From: peter at peter-b.co.uk (Peter Brett) Date: Thu, 30 Jul 2009 15:17:52 +0100 Subject: Regexp problem References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> Message-ID: Marcus Wanner writes: > On 7/30/2009 9:32 AM, Beldar wrote: >> On 30 jul, 15:07, MRAB wrote: >>> Beldar wrote: >>>> Hi there! >>>> I have a problem and i'm not very good at regular expressions. >>>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>>> lalala" I need a regexp to get the 'beldar' part, the format is >>>> 'something-is-listening', i need to get the something part, use it in >>>> my code, and then replace the whole 'something-is-listening' for >>>> another string. >>> \w+ will match a word and enclosing it in (...) will capture what was >>> matched: >>> >>> m = re.search(r"(\w+)-is-listening", text) >>> print "Captured '%s'" % m.group(1) >>> print "Matched from %d to %d" % (m.start(), m.end()) >> >> Ok, thank you all, it was very helpful! > Wow, I really need to learn more about regexp... > Any tutorials you guys can recommend? I have to confess that after fiddling with regexps for quite a while with no great success, I learnt the hard (and best) way, i.e. using them to write something vile and horrible. [*] I commend this path to you also. ;-) Cheers, Peter [*] http://git.gpleda.org/?p=gaf.git;a=blob;f=libgeda/desktop-i18n;h=6fab9b85b -- Peter Brett Remote Sensing Research Group Surrey Space Centre From p.f.moore at gmail.com Thu Jul 30 10:20:10 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Thu, 30 Jul 2009 15:20:10 +0100 Subject: No PyPI search for 3.x compatible packages In-Reply-To: References: Message-ID: <79990c6b0907300720s3a2ad4a8lf49bc265b653f55f@mail.gmail.com> 2009/7/30 Francesco Bochicchio : > On 30 Lug, 01:55, Neil Hodgson wrote: >> ? ?There appears to be no way to search PyPI for packages that are >> compatible with Python 3.x. There are classifiers for 'Programming >> Language' including 'Programming Language :: Python :: 3' but that seems >> to be for implementation language since there are so many packages that >> specify C. There are a total of 109 packages classified with Python :: >> [3, 3.0, 3.1] out of a total of 4829 packages.http://pypi.python.org/pypi?:action=browse&show=all&c=214&c=533 >> >> ? ?The search box appears to search for any word entered so a search >> like "xml 3.0" or "xml AND 3.0" does not help. >> >> ? ?Some packages include version information in the Py Version column of >> their download lists or embedded in the download file names. Projects >> are often constrained to a particular set of Python versions so need to >> choose packages that will work with those versions. It would be helpful >> if PyPI made this information more visible and searchable. >> >> ? ?Neil > > Are you sure? I note that for example pygtk has as language tags both > C and python. So maybe a C extension > for python3 would have both C and python 3 as language tags. > > I suspect that the 109 packages you found are the only ones obf the > 4829 which works with python3 (but I hope > to be wrong ). Also, of course, they may not be the only ones that work, but merely the only ones where the author has checked they work and tagged the entry. It's quite possible that some packages will work unmodified... Paul. From kwatch at gmail.com Thu Jul 30 10:30:25 2009 From: kwatch at gmail.com (kwatch) Date: Thu, 30 Jul 2009 07:30:25 -0700 (PDT) Subject: [ANN] pyKook 0.0.2 - a simple build tool similar to Make or Ant Message-ID: Hi, I have released pyKook 0.0.2. http://pypi.python.org/pypi/Kook/0.0.2 http://www.kuwata-lab.com/kook/ http://www.kuwata-lab.com/kook/pykook-users-guide.html Overview -------- pyKook is a simple build tool similar to Make, Ant, Rake, or SCons. pyKook regards software project as cooking. Terms used in pyKook are cooking terms. For example: cookbook - Makefile product - target file ingredient - source file recipe - how to create target from source spices - command-line options for recipe Cookbook (= Makefile) is written in pure Python. You can write any statements or expressions in cookbook. NOTICE: pyKook is under alpha release. Spec and features may be changed in the future without previous announcement. Example ------- Example of cookbook (Kookbook.py): -------------------------------------------------- ## ## properties ## cc = prop('cc', 'gcc') cflags = prop('cflags', '-g -Wall') ## ## recipes ## @ingreds("hello") def task_all(c): pass @product("hello") @ingreds("hello.o") def file_command(c): """generates hello command""" system(c%"$(cc) $(cflags) -o $(product) $(ingred)") @product("*.o") @ingreds("$(1).c", if_exists("$(1).h")) def file_ext_o(c): """compile '*.c' and '*.h'""" system(c%"$(cc) $(cflags) -c $(1).c") def task_clean(c): rm_f("*.o") -------------------------------------------------- Exampe of result: ================================================== bash> ls Kookbook.py hello.c hello.h bash> pykook -l Properties: cc : 'gcc' cflags : '-g -Wall' Task recipes: all : cook all products clean : remove by-products File recipes: hello : generates hello command *.o : compile '*.c' and '*.h' (Tips: you can set 'kook_default_product' variable in your kookbook.) bash> pykook all # or, pykook --cc=gcc4 all ### *** hello.o (func=file_ext_o) $ gcc -g -Wall -c hello.c ### ** hello (func=file_command) $ gcc -g -Wall -o hello hello.o ### * all (func=task_all) ================================================== See users-guide for more details. http://www.kuwata-lab.com/kook/pykook-users-guide.html Enhancements, Changes, Bug fixes sice 0.0.1 ------------------------------------------- Enhancements: - Python 3 support. - Add 'kk' script which is shortcat for kk command. Changes: - Decorator '@cmdopts()' is renamed to '@spices()', and there is no need to call parse_cmdopts(). ### prev version @cmdopts('-v: verbose', '-f file: filename') def task_example(c, *args): opts, rests = c.parse_cmdopts(args) verbose = opts.get('v', False): fileame = opts.get('f', None) ### since this release (0.0.2) @spices('-v: verbose', '-f file: filename') def task_example(c, *args, *kwargs): opts, rests = kwarts, args verbose = opts.get('v', False): fileame = opts.get('f', None) - Remove 'pyk' script - Testing library is changed from Python's unittest library into 'test/oktest.py'. Bugfixes: - glob2() now recognizes files on current directory. Have fun! -- regards, makoto kuwata From davea at ieee.org Thu Jul 30 10:30:55 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 10:30:55 -0400 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <4A71AE9F.2000900@ieee.org> Martin P. Hellwig wrote: >
Dave > Angel wrote: > > Ah yes, we thread on the territory of word definition and difference > in interpretation. Any argument is doomed to fail if not agreed or at > least taken in perspective of the terminology used by users. > > I could be (well it is quite likely) wrong in my interpretation of the > terminology, but here goes it anyway: > > Machine Code: > Whatever the machine executes, it could be that the CPU uses an > abstraction of microcode to do this but from the perspective of the > user, this is all done in the same 'black box' > > Compiling: > Translate words/symbols/mnemonics to machine code, which than can be > either loaded, linked and executed by an OS or read and executed by > the BIOS. > > Interpreted: > Instructions which can be fed to a previous compiled program that is > able to dynamically change its execution and flow without the need to > recompile itself. > Depending on the level of understanding of the user, plus his history and his biases, he will include more or less in his "black box." In the old days, microcode was not "on-chip" but stored separately in control memory. And on many machines, it was changeable at will. To many users these days, the entire system including software is a black box, which gradually breaks down (gets slow, runs out of space, crashes a lot) and must be replaced. They don't distinguish operating system from application, real memory from virtual, or viruses from bugs. But of course those users wouldn't be participating in this discussion. My background includes specifying hardware instruction sets and architecture. And writing microcode for multiple machines. And writing parts of compilers, interpreters, assemblers, and so on. And microcoding interpreters. And hooking into compilers to modify how they would generate code. And hooking into runtimes to test running code in realtime. So I tend to have very flexible definition of compiler and interpreter. Probably the only reason I jumped in here was the unmentioned bias that somehow a compiler is superior to an interpreter. I think I'd better extend my definition of compilation. It's a step that's statically taken over a series of instructions (not necessarily text source), that transforms it into a form closer to the targeted enviromment, real or virtual. Most C++ compilers have two compilers operating serially, the first to turn the source code into an intermediate form (like byte code), and the second to generate what is commonly called "machine code." The second part of course is duplicated for each different target processor. So Java is compiled into byte code, and the typical java VM then compiles that piecewise into machine code (so called JIT compiling, for just in time). BTW, interpreters don't have to be written in a compiled language. Anyway, I don't object to your definitions. We all have different perspectives. DaveA From Ron.Barak at lsi.com Thu Jul 30 10:47:37 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 15:47:37 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <4A7199FF.8010202@dejaviewphoto.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26D1@enbmail01.lsi.com> > -----Original Message----- > From: Dave Angel [mailto:davea at dejaviewphoto.com] > Sent: Thursday, July 30, 2009 16:03 > To: Barak, Ron > Cc: 'Dave Angel'; 'python-list at python.org' > Subject: RE: Run pyc file without specifying python path ? > > Barak, Ron wrote: > >> -----Original Message----- > >> From: Dave Angel [mailto:davea at ieee.org] > >> Sent: Wednesday, July 29, 2009 21:05 > >> To: Barak, Ron > >> Cc: 'python-list at python.org' > >> Subject: Re: Run pyc file without specifying python path ? > >> > >> Barak, Ron wrote: > >> > >>> Hi, > >>> > >>> I wanted to make a python byte-code file executable, > >>> > >> expecting to be able to run it without specifying "python" on the > >> (Linux bash) command line. > >> > >>> So, I wrote the following: > >>> > >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > >>> > >>> print "hello" > >>> [root at VMLinux1 python]# > >>> > >>> and made its pyc file executable: > >>> > >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc > >>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > >>> [root at VMLinux1 python]# > >>> > >>> So, I see: > >>> > >>> [root at VMLinux1 python]# file test_pyc.py* > >>> test_pyc.py: a python script text executable > >>> test_pyc.pyc: python 2.3 byte-compiled > >>> [root at VMLinux1 python]# > >>> > >>> If I try to do the following, no problem: > >>> > >>> [root at VMLinux1 python]# python test_pyc.pyc hello > >>> [root at VMLinux1 python]# > >>> > >>> However, the following fails: > >>> > >>> [root at VMLinux1 python]# ./test_pyc.pyc > >>> -bash: ./test_pyc.pyc: cannot execute binary file > >>> [root at VMLinux1 python]# > >>> > >>> Is there a way to run a pyc file without specifying the > >>> > >> python path ? > >> > >>> Bye, > >>> Ron. > >>> > >>> > >>> > >> I don't currently run Unix, but I think I know the problem. > >> > >> In a text file, the shell examines the first line, and if > it begins > >> #! > >> it's assumed to point to the executable of an interpreter for that > >> text file. Presumably the same trick doesn't work for a .pyc file. > >> > >> Why not write a trivial wrapper.py file, don't compile it, and let > >> that invoke the main code in the .pyc file? > >> > >> Then make wrapper.py executable, and you're ready to go. > >> > >> DaveA > >> > >> > >> > > > > Hi Dave, > > Your solution sort of defeats my intended purpose (sorry > for not divulging my 'hidden agenda'). > > I wanted my application to "hide" the fact that it's a > python script, and look as much as possible like it's a > compiled program. > > The reason I don't just give my user a py file, is that I > don't want a cleaver user to change the innards of the script. > > On the other hand, I don't want to make a compiled > (freezed?) version of the application, because it'll grow the > resulting file significantly, and I don't have the experience > to know how it will run on different Linuxes. > > Bye, > > Ron. > > Hi Dave, > Most of the other answers basically paraphrased my suggestion > of making a wrapper file, not compiling it, and making it > executable. With that > approach, the user just types "wrapper.py" on his command line. > > And wrapper.py only needs two lines, a shebang, and an > import, no big deal if the user modifies it. The rest of > your code can be .pyc files. > > Steven makes some good points. You have to define what level > of clever you're protecting from. A determined hacker will The clever I try to guard against is not at hacker level, just the curios worker. > get in no matter what you do, unless you want to ship the > program in a proprietary embedded system, encased in epoxy. > Further, if you have an extension of .py or .pyc, a > knowledgeable hacker will know it's probably python. Granted, but the user will know that from the user manual anyway :-) > > You imply you want it to run unmodifed on multiple unknown > Linux versions. I think that lets out binfmt solutions. Actually, no: I know the set of Linuxes my users are likely to have. > That means you need to test and support not only multiple > Linux implementations, but multiple Python versions, because > who knows what the user may have installed. I think you need > to rethink your support strategy. And maybe concentrate on > being able to detect change, rather than prevent it. > > DaveA > > Thanks for the helpful suggestions. Ron. From invalid at invalid Thu Jul 30 10:50:47 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 09:50:47 -0500 Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: On 2009-07-30, Dave Angel wrote: > Steven makes some good points. You have to define what level > of clever you're protecting from. A determined hacker will > get in no matter what you do, unless you want to ship the > program in a proprietary embedded system, encased in epoxy. That won't stop a dedicated reverse-engineering effort. It might take an X-ray machine, machine tools, various nasty chemicals, and quite a bit of skill and patience -- but it can be done. > Further, if you have an extension of .py or .pyc, a > knowledgeable hacker will know it's probably python. > > You imply you want it to run unmodifed on multiple unknown > Linux versions. I think that lets out binfmt solutions. That > means you need to test and support not only multiple Linux > implementations, but multiple Python versions, because who > knows what the user may have installed. I think you need to > rethink your support strategy. And maybe concentrate on being > able to detect change, rather than prevent it. Or even being able to benefit from change. :) -- Grant Edwards grante Yow! FROZEN ENTREES may at be flung by members of visi.com opposing SWANSON SECTS ... From sajmikins at gmail.com Thu Jul 30 10:52:46 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 30 Jul 2009 10:52:46 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A709B77.9080209@ieee.org> <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> Message-ID: <50f98a4c0907300752mbabd920ma208ca6a0b8f69f2@mail.gmail.com> On Wed, Jul 29, 2009 at 3:10 PM, Omer Khalid wrote: > Hi Dave, > > Thanks for your reply. I actually didn't cut and paste my code as it was > dispersed in different places, i typed the logic behind my code in the email > (and obiviously made some typos, indentations is some thing else) but my Please, do not do that. It's very difficult to debug code that hasn't been seen. The code you posted has numerous problems (that likely have nothing to do with your actual problem.) If you're going to post code, try to recreate the issue with a small runnable script. If you can't do that, as it sounds like it would be difficult in this case as your code is dispersed in different places, at least post the relevant portions of the actual code. Don't re-type. > real code does not have these problems as my application runs fine with out > errors... > > Except that the line where i want to update the value doesn't get updated > and no exception is thrown. What's surprising for me is that i am doing the > same thing in hundreds of places in my 3k+ line code but by some reason this > part doesn't work... > > As far as the global variables are concerned, i am using them in other > places too and didn't see any problems. > > I think some thing else is going on here as the statement above and below my > modified lines get executed. If the statements above and below that line(s) are executed, then that line is certainly being executed as well. Try introducing some additional print statements to verify your mental model of what's happening: # set it to 1 print jobs print jobs[index] print jobs[index]['v'] jobs[index]['v'] = 1 print jobs print jobs[index] print jobs[index]['v'] print "Set to 1" > Is there a way in Python to debug memory address or to see where in memory > this object is stored, and is there a lock on it or else? You are barking up the wrong tree. Somewhere in your code you're doing something silly that's causing your issue. From ben+python at benfinney.id.au Thu Jul 30 10:53:35 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 31 Jul 2009 00:53:35 +1000 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <87vdlate6o.fsf@benfinney.id.au> "Martin P. Hellwig" writes: > Machine Code: > Whatever the machine executes, it could be that the CPU uses an > abstraction of microcode to do this but from the perspective of the > user, this is all done in the same 'black box' This requires, of course, defining what is the machine. Python bytecode targets a virtual machine that is implemented differently for each hardware platform. > Compiling: > Translate words/symbols/mnemonics to machine code, which than can be > either loaded, linked and executed by an OS or read and executed by > the BIOS. Related to the above point, the ?machine code? can just as easily be codes for a virtual machine specification. This is the case for the bytecode instructions Python gets compiled to. > Interpreted: > Instructions which can be fed to a previous compiled program that is > able to dynamically change its execution and flow without the need to > recompile itself. This doesn't make much sense to me, I must say. I'd say, instead, that a program is interpreted if its instruction are dynamically translated to underlying platform instructions at execution time. This is the case for the bytecode instructions interpreted by the Python virtual machine. -- \ ?Often, the surest way to convey misinformation is to tell the | `\ strict truth.? ?Mark Twain, _Following the Equator_ | _o__) | Ben Finney From invalid at invalid Thu Jul 30 10:55:26 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 09:55:26 -0500 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: On 2009-07-30, Martin P. Hellwig wrote: > Michel Claveau - MVP wrote: > >>> Python is interpreted >> >> No. Python is compiled (--> .pyc) >> But the term "to compile" is not always unambiguous... >> And the notion of "compiler" is not attached to Python (the >> language), but is attached to the implementation. > > Well the pyc, which I thought was the Python bytecode, It is, but that's just one particular implementation you're talking about (though by far the most widely used one). > is then interpreted by the VM. Yup. Just like .exe files are interpreted by the microcode in the processor that implements the IA32 VM. It would be quite possible to put a Python VM into hardware. Alternatevly, you can compiler Python into Java bytecode and run that "directly" on hardware. -- Grant Edwards grante Yow! It don't mean a at THING if you ain't got visi.com that SWING!! From draghuram at gmail.com Thu Jul 30 10:59:40 2009 From: draghuram at gmail.com (Raghuram Devarakonda) Date: Thu, 30 Jul 2009 07:59:40 -0700 (PDT) Subject: Connecting multiple test cases in a sort of pipe References: <7dctogF29q4c6U1@mid.uni-berlin.de> Message-ID: <63715814-ff14-4b95-9160-d98325640a8b@v20g2000yqm.googlegroups.com> On Jul 30, 2:43 am, "Diez B. Roggisch" wrote: > Write a TestCase-subclass that simply works like this: > > class FileTests(TestCase): > > def test_create(self): > self.created_file = create() > assert something > > def test_list(self): > self.test_create() > delete(self.created_file) > > Passing state around is one of the reasons objects have been invented :) > And nobody ever said that you can't invoke a test-method from somewhere > else. Just to be clear, are you suggesting that these tests be run by a custom runnner rather than by the default runner from unittest? Thanks, Raghu From invalid at invalid Thu Jul 30 11:03:56 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 10:03:56 -0500 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: On 2009-07-30, Dave Angel wrote: > As far as I know, nobody has yet built a microcode implementation of a > Python VM (Virtual Machine). Nor have I seen one for the Java VM. Didn't Sun or somebody do one 10-12 years ago? Or did I misinterpret some press release or something? Off to google... > However, in the early 80's there was a microcode implementation of the > P-system VM. Ah yes. I remember sitting at an Intel MDS-80 "blue box" CP/M system entering assembly language by hand for a simple Pascal -> P-code compiler. IIRC, I typed it from a listing in the "Byte Big Book of Pascal". That machine was pretty high-tech, since I could save my work on an 8" floppy rather than a spool of paper tape. The floppy disks didn't leave big oil stains in your backpack! I got the compiler working, but I don't remember ever having a VM and run-time system. > It was never a commercial success, but it existed. And there > have been at least three Forth machines, where the hardware > itself was designed to support the language's VM. No > microcode at all. -- Grant Edwards grante Yow! When you get your at PH.D. will you get able to visi.com work at BURGER KING? From keith at nekotaku.com Thu Jul 30 11:09:20 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 08:09:20 -0700 (PDT) Subject: Use existing IE cookie Message-ID: Hi there, Relevant versions: Python 2.5, Vista Home, IE7 I am trying to scrape a website I have browsed manually in the past, and also manually selected my options, and now want python to use my existing cookie from the manual browse when downloading data. Using: http://code.activestate.com/recipes/80443/ I have found the "name" of the relevant cookie, just after reading urllib2 docs, I can't see how to "send" or have my python instance use "MY" existing cookie. Using the following: *** import re import urllib2, cookielib # set things up for cookies opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) urllib2.install_opener(opener) reply = urllib2.urlopen('foo.html').read() print reply *** This does return data, just default data, not the data from the options I set up when manually browsing. My sense is that I need "something" in the () part of HTTPCookieProcessor() but I have no idea as to what... the docs say "cookiejar" but the only code examples I have found are to create a cookiejar for the existing Python session, not to use the cookies from my prior manual meanderings. Any help greatly appreciated. From Ron.Barak at lsi.com Thu Jul 30 11:10:17 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 16:10:17 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <4A7199FF.8010202@dejaviewphoto.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26D2@enbmail01.lsi.com> Hi Dave, On second thoughts, I may have a problem implementing the wrapper solution, because my actual test_pyc.pyc, needs to parse its command line. Namely, the actual call to test_pyc.pyc looks something like this: $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t tvn -p pool1 -l -c And I don't know of a way to add these parameters to the "import test_pyc" in wrapper Is there a way to pass information to an imported module ? (Sorry if there's an obvious answer, I just cannot figure it out). Bye, Ron. > -----Original Message----- > From: Dave Angel [mailto:davea at dejaviewphoto.com] > Sent: Thursday, July 30, 2009 16:03 > To: Barak, Ron > Cc: 'Dave Angel'; 'python-list at python.org' > Subject: RE: Run pyc file without specifying python path ? > > Barak, Ron wrote: > >> -----Original Message----- > >> From: Dave Angel [mailto:davea at ieee.org] > >> Sent: Wednesday, July 29, 2009 21:05 > >> To: Barak, Ron > >> Cc: 'python-list at python.org' > >> Subject: Re: Run pyc file without specifying python path ? > >> > >> Barak, Ron wrote: > >> > >>> Hi, > >>> > >>> I wanted to make a python byte-code file executable, > >>> > >> expecting to be able to run it without specifying "python" on the > >> (Linux bash) command line. > >> > >>> So, I wrote the following: > >>> > >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > >>> > >>> print "hello" > >>> [root at VMLinux1 python]# > >>> > >>> and made its pyc file executable: > >>> > >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc > >>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > >>> [root at VMLinux1 python]# > >>> > >>> So, I see: > >>> > >>> [root at VMLinux1 python]# file test_pyc.py* > >>> test_pyc.py: a python script text executable > >>> test_pyc.pyc: python 2.3 byte-compiled > >>> [root at VMLinux1 python]# > >>> > >>> If I try to do the following, no problem: > >>> > >>> [root at VMLinux1 python]# python test_pyc.pyc hello > >>> [root at VMLinux1 python]# > >>> > >>> However, the following fails: > >>> > >>> [root at VMLinux1 python]# ./test_pyc.pyc > >>> -bash: ./test_pyc.pyc: cannot execute binary file > >>> [root at VMLinux1 python]# > >>> > >>> Is there a way to run a pyc file without specifying the > >>> > >> python path ? > >> > >>> Bye, > >>> Ron. > >>> > >>> > >>> > >> I don't currently run Unix, but I think I know the problem. > >> > >> In a text file, the shell examines the first line, and if > it begins > >> #! > >> it's assumed to point to the executable of an interpreter for that > >> text file. Presumably the same trick doesn't work for a .pyc file. > >> > >> Why not write a trivial wrapper.py file, don't compile it, and let > >> that invoke the main code in the .pyc file? > >> > >> Then make wrapper.py executable, and you're ready to go. > >> > >> DaveA > >> > >> > >> > > > > Hi Dave, > > Your solution sort of defeats my intended purpose (sorry > for not divulging my 'hidden agenda'). > > I wanted my application to "hide" the fact that it's a > python script, and look as much as possible like it's a > compiled program. > > The reason I don't just give my user a py file, is that I > don't want a cleaver user to change the innards of the script. > > On the other hand, I don't want to make a compiled > (freezed?) version of the application, because it'll grow the > resulting file significantly, and I don't have the experience > to know how it will run on different Linuxes. > > Bye, > > Ron. > > > Most of the other answers basically paraphrased my suggestion > of making a wrapper file, not compiling it, and making it > executable. With that > approach, the user just types "wrapper.py" on his command line. > > And wrapper.py only needs two lines, a shebang, and an > import, no big deal if the user modifies it. The rest of > your code can be .pyc files. > > Steven makes some good points. You have to define what level > of clever you're protecting from. A determined hacker will > get in no matter what you do, unless you want to ship the > program in a proprietary embedded system, encased in epoxy. > Further, if you have an extension of .py or .pyc, a > knowledgeable hacker will know it's probably python. > > You imply you want it to run unmodifed on multiple unknown > Linux versions. I think that lets out binfmt solutions. > That means you need to test and support not only multiple > Linux implementations, but multiple Python versions, because > who knows what the user may have installed. I think you need > to rethink your support strategy. And maybe concentrate on > being able to detect change, rather than prevent it. > > DaveA > > > From deets at nospam.web.de Thu Jul 30 11:16:44 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 17:16:44 +0200 Subject: Use existing IE cookie References: Message-ID: <7ddrqsF2b5mblU1@mid.uni-berlin.de> KB wrote: > Hi there, > > Relevant versions: Python 2.5, Vista Home, IE7 > > I am trying to scrape a website I have browsed manually in the past, > and also manually selected my options, and now want python to use my > existing cookie from the manual browse when downloading data. > > Using: http://code.activestate.com/recipes/80443/ I have found the > "name" of the relevant cookie, just after reading urllib2 docs, I > can't see how to "send" or have my python instance use "MY" existing > cookie. > > Using the following: > > *** > import re > import urllib2, cookielib > > # set things up for cookies > > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) > urllib2.install_opener(opener) > > reply = urllib2.urlopen('foo.html').read() > > print reply > > *** > > This does return data, just default data, not the data from the > options I set up when manually browsing. > > My sense is that I need "something" in the () part of > HTTPCookieProcessor() but I have no idea as to what... the docs say > "cookiejar" but the only code examples I have found are to create a > cookiejar for the existing Python session, not to use the cookies from > my prior manual meanderings. Because this is a completely different beast. You need to find out if and how to access IE-cookies from python - I guess some win32-road is to be walked down for that. Once you get a hold on them, you can build up whatever cookiejar urllib2 needs. Diez From invalid at invalid Thu Jul 30 11:16:46 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 10:16:46 -0500 Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <27mdnb-NxM3DJOzXnZ2dnUVZ_hxi4p2d@posted.visi> On 2009-07-30, Barak, Ron wrote: > Hi Dave, > > On second thoughts, I may have a problem implementing the > wrapper solution, because my actual test_pyc.pyc, needs to > parse its command line. Namely, the actual call to > test_pyc.pyc looks something like this: > > $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t tvn -p pool1 -l -c > > And I don't know of a way to add these parameters to the > "import test_pyc" in wrapper > > Is there a way to pass information to an imported module ? > (Sorry if there's an obvious answer, I just cannot figure it > out). I don't understand your problem. The module would parse sys.argv just like always. If you don't like that for some reason, you could define an entry point in the module and call it: #!/usr/bin/python import sys,test_pyc test_pyc.main(*sys.argv) -- Grant Edwards grante Yow! We just joined the at civil hair patrol! visi.com From keith at nekotaku.com Thu Jul 30 11:19:08 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 08:19:08 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> Message-ID: <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> > > Using:http://code.activestate.com/recipes/80443/ > Thanks for the prompt reply, Diez! Using the above I have found the name of the cookie (I did google how to use IE cookies in python and that was the best match) but it only tells me the name of the cookie, not how to use it. Any clues? TIA! From dkatkowski at gmail.com Thu Jul 30 11:25:58 2009 From: dkatkowski at gmail.com (Feyo) Date: Thu, 30 Jul 2009 08:25:58 -0700 (PDT) Subject: Help with Regex for domain names Message-ID: I'm trying to figure out how to write efficiently write a regex for domain names with a particular top level domain. Let's say, I want to grab all domain names with country codes .us, .au, and .de. I could create three different regexs that would work: regex = re.compile(r'[\w\-\.]+\.us) regex = re.compile(r'[\w\-\.]+\.au) regex = re.compile(r'[\w\-\.]+\.de) How would I write one to accommodate all three, or, better yet, to accommodate a list of them that I can pass into a method call? Thanks! From martin.hellwig at dcuktec.org Thu Jul 30 11:41:56 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 16:41:56 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <87vdlate6o.fsf@benfinney.id.au> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <87vdlate6o.fsf@benfinney.id.au> Message-ID: Ben Finney wrote: > "Martin P. Hellwig" writes: > >> Machine Code: >> Whatever the machine executes, it could be that the CPU uses an >> abstraction of microcode to do this but from the perspective of the >> user, this is all done in the same 'black box' > > This requires, of course, defining what is the machine. Python bytecode > targets a virtual machine that is implemented differently for each > hardware platform. > I would further define 'black box' as the hardware a kernel programmer writes to. >> Interpreted: >> Instructions which can be fed to a previous compiled program that is >> able to dynamically change its execution and flow without the need to >> recompile itself. > > This doesn't make much sense to me, I must say. > > I'd say, instead, that a program is interpreted if its instruction are > dynamically translated to underlying platform instructions at execution > time. This is the case for the bytecode instructions interpreted by the > Python virtual machine. > Yes that is indeed a much better description, I'll steal that from you :-) -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From deets at nospam.web.de Thu Jul 30 11:43:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 17:43:31 +0200 Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> Message-ID: <7ddtd3F297bafU1@mid.uni-berlin.de> KB wrote: > >> > Using:http://code.activestate.com/recipes/80443/ >> > > Thanks for the prompt reply, Diez! Using the above I have found the > name of the cookie (I did google how to use IE cookies in python and > that was the best match) but it only tells me the name of the cookie, > not how to use it. Ah, sorry, should have read the recipe also. For me it looks as if findIECookie from that recipe is to be called with the name. Then it should return the value, or None What does you full example look like, including the cookie-acquisition-stuff? Diez From tundra at tundraware.com Thu Jul 30 11:47:06 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Thu, 30 Jul 2009 10:47:06 -0500 Subject: Help with Regex for domain names In-Reply-To: References: Message-ID: Feyo wrote: > I'm trying to figure out how to write efficiently write a regex for > domain names with a particular top level domain. Let's say, I want to > grab all domain names with country codes .us, .au, and .de. > > I could create three different regexs that would work: > regex = re.compile(r'[\w\-\.]+\.us) > regex = re.compile(r'[\w\-\.]+\.au) > regex = re.compile(r'[\w\-\.]+\.de) > > How would I write one to accommodate all three, or, better yet, to > accommodate a list of them that I can pass into a method call? Thanks! Just a point of interest: A correctly formed domain name may have a trailing period at the end of the TLD [1]. Example: foo.bar.com. Though you do not often see this, it's worth accommodating "just in case"... [1] http://homepages.tesco.net/J.deBoynePollard/FGA/web-fully-qualified-domain-name.html -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From python at mrabarnett.plus.com Thu Jul 30 11:50:37 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 16:50:37 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <4A719E2D.708@ieee.org> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <4A719E2D.708@ieee.org> Message-ID: <4A71C14D.7030805@mrabarnett.plus.com> Dave Angel wrote: [snip] > As far as I know, nobody has yet built a microcode implementation of a > Python VM (Virtual Machine). Nor have I seen one for the Java VM. > However, in the early 80's there was a microcode implementation of the > P-system VM. It was never a commercial success, but it existed. And > there have been at least three Forth machines, where the hardware itself > was designed to support the language's VM. No microcode at all. > There's Jazelle: http://en.wikipedia.org/wiki/Jazelle. From python at mrabarnett.plus.com Thu Jul 30 11:53:35 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 16:53:35 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <87vdlate6o.fsf@benfinney.id.au> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <87vdlate6o.fsf@benfinney.id.au> Message-ID: <4A71C1FF.2070702@mrabarnett.plus.com> Ben Finney wrote: > "Martin P. Hellwig" writes: > >> Machine Code: >> Whatever the machine executes, it could be that the CPU uses an >> abstraction of microcode to do this but from the perspective of the >> user, this is all done in the same 'black box' > > This requires, of course, defining what is the machine. Python bytecode > targets a virtual machine that is implemented differently for each > hardware platform. > >> Compiling: >> Translate words/symbols/mnemonics to machine code, which than can be >> either loaded, linked and executed by an OS or read and executed by >> the BIOS. > > Related to the above point, the ?machine code? can just as easily be > codes for a virtual machine specification. This is the case for the > bytecode instructions Python gets compiled to. > >> Interpreted: >> Instructions which can be fed to a previous compiled program that is >> able to dynamically change its execution and flow without the need to >> recompile itself. > > This doesn't make much sense to me, I must say. > > I'd say, instead, that a program is interpreted if its instruction are > dynamically translated to underlying platform instructions at execution > time. This is the case for the bytecode instructions interpreted by the > Python virtual machine. > Interpretation doesn't necessarily mean translating to machine code at execution time. What you're describing is more like JIT. From python at mrabarnett.plus.com Thu Jul 30 11:56:23 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 16:56:23 +0100 Subject: Help with Regex for domain names In-Reply-To: References: Message-ID: <4A71C2A7.90900@mrabarnett.plus.com> Feyo wrote: > I'm trying to figure out how to write efficiently write a regex for > domain names with a particular top level domain. Let's say, I want to > grab all domain names with country codes .us, .au, and .de. > > I could create three different regexs that would work: > regex = re.compile(r'[\w\-\.]+\.us) > regex = re.compile(r'[\w\-\.]+\.au) > regex = re.compile(r'[\w\-\.]+\.de) > > How would I write one to accommodate all three, or, better yet, to > accommodate a list of them that I can pass into a method call? Thanks! > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') If you have a list of country codes ["us", "au", "de"] then you can build the regular expression from it: regex = re.compile(r'[\w\-\.]+\.(?:%s)' % '|'.join(domains)) From keith at nekotaku.com Thu Jul 30 11:58:56 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 08:58:56 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> Message-ID: <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> > What does you full example look like, including the > cookie-acquisition-stuff? > > Diez I ran them seperately, hoping for a clue as to what my "cookiejar" was. The cookie-acquisition stuff returns "screener.ashx?v=151" when I search with my domain I am interested in. I have tried urllib2.HTTPCookieProcessor('screener.ashx?v=151') but that failed with attr has no cookie header. >From the HTTPCookieProcessor doco, it appears that non-IE browsers have a cookie file (and example code) but from what I can tell IE uses a hidden folder. (you can set your location in IE but it appends a folder "\Temporary Internet Files" - From: http://docs.python.org/dev/library/cookielib.html *** This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx cookies (assumes Unix/Netscape convention for location of the cookies file): import os, cookielib, urllib2 cj = cookielib.MozillaCookieJar() cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt")) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) r = opener.open("http://example.com/") *** Not sure how to adapt this for IE. From darkneter at gmail.com Thu Jul 30 12:06:11 2009 From: darkneter at gmail.com (NighterNet) Date: Thu, 30 Jul 2009 09:06:11 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: <15017646-6e20-40ff-af70-5717598f26bb@a39g2000pre.googlegroups.com> On Jul 30, 6:56?am, "Mark Tolonen" wrote: > "NighterNet" wrote in message > > news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com... > > >I am trying to figure out how to send text or byte in python3.1. I am > > trying to send data to flashsocketto get there. I am not sure how to > > work it. > > > buff= 'id=' , self.id , ':balive=False\n' > > clientSock.send(buff); > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Python3.1strings are Unicode (think characters not bytes). ?When writing > to files, sockets, etc. bytes must be used. ?Unicode strings have an > encode() method, where you can specify an appropriate encoding (ascii, > latin-1, windows-1252, utf-8, etc.): > > ? ? clientSock.send(buff.encode('ascii')) > > When reading from thesocket, you can decode() the byte strings back into > Unicode strings. > > ? ? data = clientSock.recv(1024).decode('ascii') > > -Mark I am not sure how to use struct package. Here an example for the input: {id:1,playername:guest,x:100,y:50} {id:2,playername:tester,x:100,y:50} struct.pack(? ) From cjw at ncf.ca Thu Jul 30 12:18:07 2009 From: cjw at ncf.ca (Colin J. Williams) Date: Thu, 30 Jul 2009 12:18:07 -0400 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: Some have treated this as a troll. I don't. r wrote: [snip] > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > Ruby --> "obj.m1.m2.m3" -- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. > +1 > 2.) the .each method > container.each{|localVar| block} > This method can really cleanup some ugly for loops, although i really > like the readability of for loops. > Not clear. > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). You know what i am talking about here people. We > all get complacent and It seems easier to just cope with these > problems instead of fighting for change. But look at the French, WHAT > THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! > +0.6 or better [1, 2, 3, 4, 5].len or [1, 2, 3, 4, 5].max What has this got to do with "true OOP"? [snip] From deets at nospam.web.de Thu Jul 30 12:23:59 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 18:23:59 +0200 Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> Message-ID: <7ddvp0F2brmgrU1@mid.uni-berlin.de> KB wrote: > >> What does you full example look like, including the >> cookie-acquisition-stuff? >> >> Diez > > I ran them seperately, hoping for a clue as to what my "cookiejar" > was. > > The cookie-acquisition stuff returns "screener.ashx?v=151" when I > search with my domain I am interested in. I have tried > urllib2.HTTPCookieProcessor('screener.ashx?v=151') but that failed > with attr has no cookie header. > > From the HTTPCookieProcessor doco, it appears that non-IE browsers > have a cookie file (and example code) but from what I can tell IE uses > a hidden folder. (you can set your location in IE but it appends a > folder "\Temporary Internet Files" - > > From: http://docs.python.org/dev/library/cookielib.html > > *** > This example illustrates how to open a URL using your Netscape, > Mozilla, or Lynx cookies (assumes Unix/Netscape convention for > location of the cookies file): > > import os, cookielib, urllib2 > cj = cookielib.MozillaCookieJar() > cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt")) > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) > r = opener.open("http://example.com/") > *** > > Not sure how to adapt this for IE. You could create a file that resembles the cookies.txt - no idea how that looks, but I guess it's pretty simple. Diez From ethan at stoneleaf.us Thu Jul 30 12:27:32 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 30 Jul 2009 09:27:32 -0700 Subject: Does python have the capability for driver development ? In-Reply-To: <87vdlate6o.fsf@benfinney.id.au> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <87vdlate6o.fsf@benfinney.id.au> Message-ID: <4A71C9F4.8040006@stoneleaf.us> Ben Finney wrote: > "Martin P. Hellwig" writes: > > >>Machine Code: >>Whatever the machine executes, it could be that the CPU uses an >>abstraction of microcode to do this but from the perspective of the >>user, this is all done in the same 'black box' > > > This requires, of course, defining what is the machine. Python bytecode > targets a virtual machine that is implemented differently for each > hardware platform. > > >>Compiling: >>Translate words/symbols/mnemonics to machine code, which than can be >>either loaded, linked and executed by an OS or read and executed by >>the BIOS. > > > Related to the above point, the ?machine code? can just as easily be > codes for a virtual machine specification. This is the case for the > bytecode instructions Python gets compiled to. > > >>Interpreted: >>Instructions which can be fed to a previous compiled program that is >>able to dynamically change its execution and flow without the need to >>recompile itself. > > > This doesn't make much sense to me, I must say. > > I'd say, instead, that a program is interpreted if its instruction are > dynamically translated to underlying platform instructions at execution > time. This is the case for the bytecode instructions interpreted by the > Python virtual machine. > I would have said compiled is executed by hardware, interpreted is executed by software -- but I like your definition better. :) ~Ethan~ From garrickp at gmail.com Thu Jul 30 12:31:22 2009 From: garrickp at gmail.com (Falcolas) Date: Thu, 30 Jul 2009 09:31:22 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> On Jul 29, 9:06?pm, r wrote: > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. I personally would not prefer this, and would likely continue to use (), precisely for code clarity - let me explain: foo.nextval foo.val foo.previousval Which of the calls above referenced instance variables, and which ones called functions which changed the internal state of foo? I would have trouble saying, just based on the calls above. I would have to go back to the definition or documentation of foo to identify which is doing what. On the other hand, the following gives a better clue as to what is happening (granted not perfect, but better): foo.nextval() foo.val foo.previousval() ~G From keith at nekotaku.com Thu Jul 30 12:31:33 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 09:31:33 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> <7ddvp0F2brmgrU1@mid.uni-berlin.de> Message-ID: <33b7685b-054a-442c-8e97-46a5b77f7900@z4g2000prh.googlegroups.com> On Jul 30, 9:23?am, "Diez B. Roggisch" wrote: > KB wrote: > > >> What does you full example look like, including the > >> cookie-acquisition-stuff? > > >> Diez > > > I ran them seperately, hoping for a clue as to what my "cookiejar" > > was. > > > The cookie-acquisition stuff returns "screener.ashx?v=151" when I > > search with my domain I am interested in. I have tried > > urllib2.HTTPCookieProcessor('screener.ashx?v=151') but that failed > > with attr has no cookie header. > > > From the HTTPCookieProcessor doco, it appears that non-IE browsers > > have a cookie file (and example code) but from what I can tell IE uses > > a hidden folder. (you can set your location in IE but it appends a > > folder "\Temporary Internet Files" ?- > > > From:http://docs.python.org/dev/library/cookielib.html > > > *** > > This example illustrates how to open a URL using your Netscape, > > Mozilla, or Lynx cookies (assumes Unix/Netscape convention for > > location of the cookies file): > > > import os, cookielib, urllib2 > > cj = cookielib.MozillaCookieJar() > > cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt")) > > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) > > r = opener.open("http://example.com/") > > *** > > > Not sure how to adapt this for IE. > > You could create a file that resembles the cookies.txt - no idea how that > looks, but I guess it's pretty simple. > > Diez- Hide quoted text - > > - Show quoted text - Yeah unfortunately I just tried Firefox and it uses cookies.sqlite now... more dead ends :) From edreamleo at charter.net Thu Jul 30 12:45:32 2009 From: edreamleo at charter.net (Edward K Ream) Date: Thu, 30 Jul 2009 11:45:32 -0500 Subject: ANN: Leo 4.6.1 final released Message-ID: Leo 4.6.1 final is now available at: http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106 Leo is a text editor, data organizer, project manager and much more. See: http://webpages.charter.net/edreamleo/intro.html Leo 4.6.1 fixes several minor bugs in Leo 4.6. The highlights of Leo 4.6 -------------------------- - Cached external files *greatly* reduces the time to load .leo files. - Leo now features a modern Qt interface by default. Leo's legacy Tk interface can also be used. - New --config, --file and --gui command-line options. - Leo tests syntax of .py files when saving them. - Leo can now open any kind of file into @edit nodes. - @auto-rst nodes allow easy editing of reStructuredText files. - Properties of commanders, positions and nodes simplify programming. - Improved Leo's unit testing framework. - Leo now requires Python 2.5 or later. - Dozens of small improvements and bug fixes. Links: ------ Leo: http://webpages.charter.net/edreamleo/front.html Forum: http://groups.google.com/group/leo-editor Download: http://sourceforge.net/project/showfiles.php?group_id=3458 Bzr: http://code.launchpad.net/leo-editor/ Quotes: http://webpages.charter.net/edreamleo/testimonials.html -------------------------------------------------------------------- Edward K. Ream email: edreamleo at yahoo.com Leo: http://webpages.charter.net/edreamleo/front.html -------------------------------------------------------------------- From inky788 at gmail.com Thu Jul 30 13:01:40 2009 From: inky788 at gmail.com (Inky 788) Date: Thu, 30 Jul 2009 10:01:40 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> On Jul 30, 12:04?am, alex23 wrote: > On Jul 30, 1:06?pm, r wrote: > > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? I agree with alex here. Will take the explicit syntax over the extra cognitive load of figuring out exactly what's going on with `obj.m1.m2.m3`. Python has its warts, but requiring ()'s on function calls isn't one of them. :) From bieffe62 at gmail.com Thu Jul 30 13:03:42 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 10:03:42 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> <15017646-6e20-40ff-af70-5717598f26bb@a39g2000pre.googlegroups.com> Message-ID: <3b14ad80-2e0e-4d4f-92e7-684dc75a7a05@w41g2000yqb.googlegroups.com> On 30 Lug, 18:06, NighterNet wrote: > On Jul 30, 6:56?am, "Mark Tolonen" wrote: > > > > > > > "NighterNet" wrote in message > > >news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com... > > > >I am trying to figure out how to send text or byte in python3.1. I am > > > trying to send data to flashsocketto get there. I am not sure how to > > > work it. > > > > buff= 'id=' , self.id , ':balive=False\n' > > > clientSock.send(buff); > > > -- > > >http://mail.python.org/mailman/listinfo/python-list > > > Python3.1strings are Unicode (think characters not bytes). ?When writing > > to files, sockets, etc. bytes must be used. ?Unicode strings have an > > encode() method, where you can specify an appropriate encoding (ascii, > > latin-1, windows-1252, utf-8, etc.): > > > ? ? clientSock.send(buff.encode('ascii')) > > > When reading from thesocket, you can decode() the byte strings back into > > Unicode strings. > > > ? ? data = clientSock.recv(1024).decode('ascii') > > > -Mark > > I am not sure how to use struct package. > Here an example for the input: > {id:1,playername:guest,x:100,y:50} > {id:2,playername:tester,x:100,y:50} > > struct.pack(? ) If your messages are ASCII, like it seems, forget about struct, which is for 'binary' message format. Format the string as you would in 2.6 ( using % or string.format for instance ) and then use encode as instructed. From davea at dejaviewphoto.com Thu Jul 30 13:07:43 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Thu, 30 Jul 2009 13:07:43 -0400 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26D2@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> <7F0503CD69378F49BE0DC30661C6CCF6701A26D2@enbmail01.lsi.com> Message-ID: <4A71D35F.3090800@dejaviewphoto.com> Barak, Ron wrote: > Hi Dave, > > On second thoughts, I may have a problem implementing the wrapper solution, because my actual test_pyc.pyc, needs to parse its command line. > Namely, the actual call to test_pyc.pyc looks something like this: > > $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t tvn -p pool1 -l -c > > And I don't know of a way to add these parameters to the "import test_pyc" in wrapper > > Is there a way to pass information to an imported module ? (Sorry if there's an obvious answer, I just cannot figure it out). > > Bye, > Ron. > > >> -----Original Message----- >> From: Dave Angel [mailto:davea at dejaviewphoto.com] >> Sent: Thursday, July 30, 2009 16:03 >> To: Barak, Ron >> Cc: 'Dave Angel'; 'python-list at python.org' >> Subject: RE: Run pyc file without specifying python path ? >> >> Barak, Ron wrote: >> >>>> -----Original Message----- >>>> From: Dave Angel [mailto:davea at ieee.org] >>>> Sent: Wednesday, July 29, 2009 21:05 >>>> To: Barak, Ron >>>> Cc: 'python-list at python.org' >>>> Subject: Re: Run pyc file without specifying python path ? >>>> >>>> Barak, Ron wrote: >>>> >>>> >>>>> Hi, >>>>> >>>>> I wanted to make a python byte-code file executable, >>>>> >>>>> >>>> expecting to be able to run it without specifying "python" on the >>>> (Linux bash) command line. >>>> >>>> >>>>> So, I wrote the following: >>>>> >>>>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python >>>>> >>>>> print "hello" >>>>> [root at VMLinux1 python]# >>>>> >>>>> and made its pyc file executable: >>>>> >>>>> [root at VMLinux1 python]# ls -ls test_pyc.pyc >>>>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc >>>>> [root at VMLinux1 python]# >>>>> >>>>> So, I see: >>>>> >>>>> [root at VMLinux1 python]# file test_pyc.py* >>>>> test_pyc.py: a python script text executable >>>>> test_pyc.pyc: python 2.3 byte-compiled >>>>> [root at VMLinux1 python]# >>>>> >>>>> If I try to do the following, no problem: >>>>> >>>>> [root at VMLinux1 python]# python test_pyc.pyc hello >>>>> [root at VMLinux1 python]# >>>>> >>>>> However, the following fails: >>>>> >>>>> [root at VMLinux1 python]# ./test_pyc.pyc >>>>> -bash: ./test_pyc.pyc: cannot execute binary file >>>>> [root at VMLinux1 python]# >>>>> >>>>> Is there a way to run a pyc file without specifying the >>>>> >>>>> >>>> python path ? >>>> >>>> >>>>> Bye, >>>>> Ron. >>>>> >>>>> >>>>> >>>>> >>>> I don't currently run Unix, but I think I know the problem. >>>> >>>> In a text file, the shell examines the first line, and if >>>> >> it begins >> >>>> #! >>>> it's assumed to point to the executable of an interpreter for that >>>> text file. Presumably the same trick doesn't work for a .pyc file. >>>> >>>> Why not write a trivial wrapper.py file, don't compile it, and let >>>> that invoke the main code in the .pyc file? >>>> >>>> Then make wrapper.py executable, and you're ready to go. >>>> >>>> DaveA >>>> >>>> >>>> >>>> >>> Hi Dave, >>> Your solution sort of defeats my intended purpose (sorry >>> >> for not divulging my 'hidden agenda'). >> >>> I wanted my application to "hide" the fact that it's a >>> >> python script, and look as much as possible like it's a >> compiled program. >> >>> The reason I don't just give my user a py file, is that I >>> >> don't want a cleaver user to change the innards of the script. >> >>> On the other hand, I don't want to make a compiled >>> >> (freezed?) version of the application, because it'll grow the >> resulting file significantly, and I don't have the experience >> to know how it will run on different Linuxes. >> >>> Bye, >>> Ron. >>> >>> >> Most of the other answers basically paraphrased my suggestion >> of making a wrapper file, not compiling it, and making it >> executable. With that >> approach, the user just types "wrapper.py" on his command line. >> >> And wrapper.py only needs two lines, a shebang, and an >> import, no big deal if the user modifies it. The rest of >> your code can be .pyc files. >> >> Steven makes some good points. You have to define what level >> of clever you're protecting from. A determined hacker will >> get in no matter what you do, unless you want to ship the >> program in a proprietary embedded system, encased in epoxy. >> Further, if you have an extension of .py or .pyc, a >> knowledgeable hacker will know it's probably python. >> >> You imply you want it to run unmodifed on multiple unknown >> Linux versions. I think that lets out binfmt solutions. >> That means you need to test and support not only multiple >> Linux implementations, but multiple Python versions, because >> who knows what the user may have installed. I think you need >> to rethink your support strategy. And maybe concentrate on >> being able to detect change, rather than prevent it. >> >> DaveA >> >> >> >> (Please don't top-post. It puts responses out of order) You don't have to do anything special. Any module can import sys, and parse sys.argv, as long as it wasn't yet modified. DaveA From rt8396 at gmail.com Thu Jul 30 13:09:02 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:09:02 -0700 (PDT) Subject: Confessions of a Python fanboy References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: On Jul 30, 11:31?am, Falcolas wrote: > On Jul 29, 9:06?pm, r wrote: > > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > I personally would not prefer this, and would likely continue to use > (), precisely for code clarity - let me explain: > > foo.nextval > foo.val > foo.previousval > > Which of the calls above referenced instance variables, and which ones > called functions which changed the internal state of foo? I would have > trouble saying, just based on the calls above. I would have to go back > to the definition or documentation of foo to identify which is doing > what. On the other hand, the following gives a better clue as to what > is happening (granted not perfect, but better): > > foo.nextval() > foo.val > foo.previousval() > > ~G I held your exact same view before i learned the Ruby language. And your veiw makes some good points, however, naming conventions with eliminate this problem all together. All "method names" should use the underscore to separate words, "variable names" should use camelCase, "constants" in all caps, and "class defs" in titlecase. def go_and_do_this_for_me_now(self, *args) self.variableNameHere MyClassNameHere THISISACONSTANT -or- THIS_IS_A_CONSTANT in your example i would have used the following foo.next_value foo.value foo.prev_value good naming conventions will make your life (and everybody else's) much easier when debugging code. From masklinn at masklinn.net Thu Jul 30 13:10:35 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:10:35 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: On 30 Jul 2009, at 18:31 , Falcolas wrote: > On Jul 29, 9:06 pm, r wrote: > >> 1.) No need to use "()" to call a function with no arguments. >> Python --> "obj.m2().m3()" --ugly >> Ruby --> "obj.m1.m2.m3" -- sweeet! >> Man, i must admit i really like this, and your code will look so much >> cleaner. > > I personally would not prefer this, and would likely continue to use > (), precisely for code clarity - let me explain: > > foo.nextval > foo.val > foo.previousval > > Which of the calls above referenced instance variables Well, that's very simple: none of them. In Ruby (as in Smalltalk), public instance variables simply don't exist. > and which ones called functions which changed the internal state of > foo? That you can't say, but neither can you say in Python as they could all be properties. And of course just because it's a method doesn't mean it mutates the object. > I would have > trouble saying, just based on the calls above. I would have to go back > to the definition or documentation of foo to identify which is doing > what. On the other hand, the following gives a better clue as to what > is happening (granted not perfect, but better): Well... it doesn't give much of a clue no really. From masklinn at masklinn.net Thu Jul 30 13:15:14 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:15:14 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> Message-ID: On 30 Jul 2009, at 19:01 , Inky 788 wrote: > On Jul 30, 12:04 am, alex23 wrote: >> On Jul 30, 1:06 pm, r wrote: >> >>> 1.) No need to use "()" to call a function with no arguments. >>> Python --> "obj.m2().m3()" --ugly >>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>> Man, i must admit i really like this, and your code will look so >>> much >>> cleaner. >> >> How do you distinguish between calling a method with no arguments, >> and >> getting access to the method object itself (because it _is_ an >> object, >> y'know, it's OO all the way down...)? > > I agree with alex here. Will take the explicit syntax over the extra > cognitive load of figuring out exactly what's going on with > `obj.m1.m2.m3`. There's no cognitive load whatsoever: it's calling methods. Always. Ruby simply gives you no other option. Now it could be very simple methods to instance attributes, akin to a java getter, but it's still only methods. Furthermore Ruby has a pretty nice convention (sadly not used enough I think) taken from Scheme where it's possible to postfix a method name with "!" (note: the "!" is part of the name, there's no magic) to indicate that this method modifies the object it's called on rather than simply returning stuff. From keith at nekotaku.com Thu Jul 30 13:16:45 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 10:16:45 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> <7ddvp0F2brmgrU1@mid.uni-berlin.de> <33b7685b-054a-442c-8e97-46a5b77f7900@z4g2000prh.googlegroups.com> Message-ID: Winner, winner, chicken dinner... resolved. http://wwwsearch.sourceforge.net/mechanize/doc.html import mechanize cj = mechanize.MSIECookieJar(delayload=True) cj.load_from_registry() # finds cookie index file from registry Thanks Mr Lee! From mhanbali at teksystems.com Thu Jul 30 13:22:20 2009 From: mhanbali at teksystems.com (Mehdi H) Date: Thu, 30 Jul 2009 10:22:20 -0700 (PDT) Subject: NASA using Python / Django for NEBULA (cloud comp.) Message-ID: <57859e1d-ff69-49fc-b758-1331912b4d0e@v36g2000yqv.googlegroups.com> Hello Python devs! I'm supporting NASA on their NEBULA project and they're looking for Python / Django experts to join their team here in Mountain View, CA. If you're interested, let me know and we can talk more on how they're using Django in their environment! Thanks, Mehdi From robert.kern at gmail.com Thu Jul 30 13:24:52 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 30 Jul 2009 12:24:52 -0500 Subject: Pyplot In-Reply-To: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> References: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> Message-ID: On 2009-07-30 04:02, Kaan AK??T wrote: > I am a newbee in Python. I have some problem with usage of Pyplot. I > have a loop that creates plot in every end of it. Problem starts here: > The first plot is fine but the second one is plotted onto the first > plot. I use: > > plt.cla() > plt.draw() > plt.hold(False) > plt.close() > plt.clf() > > but nothing works. Perhaps I am having a problem with basics, please > help me if you know something about it. Create a new window by using figure(). You will want to ask matplotlib questions on the matplotlib mailing list: https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- 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 dkatkowski at gmail.com Thu Jul 30 13:25:58 2009 From: dkatkowski at gmail.com (Feyo) Date: Thu, 30 Jul 2009 10:25:58 -0700 (PDT) Subject: Help with Regex for domain names References: Message-ID: <78504224-b114-45f3-a302-95fbd8393443@o6g2000yqj.googlegroups.com> On Jul 30, 11:56?am, MRAB wrote: > Feyo wrote: > > I'm trying to figure out how to write efficiently write a regex for > > domain names with a particular top level domain. Let's say, I want to > > grab all domain names with country codes .us, .au, and .de. > > > I could create three different regexs that would work: > > regex = re.compile(r'[\w\-\.]+\.us) > > regex = re.compile(r'[\w\-\.]+\.au) > > regex = re.compile(r'[\w\-\.]+\.de) > > > How would I write one to accommodate all three, or, better yet, to > > accommodate a list of them that I can pass into a method call? Thanks! > > ?> > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') > > If you have a list of country codes ["us", "au", "de"] then you can > build the regular expression from it: > > regex = re.compile(r'[\w\-\.]+\.(?:%s)' % '|'.join(domains)) Perfect! Thanks. From rt8396 at gmail.com Thu Jul 30 13:28:44 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:28:44 -0700 (PDT) Subject: Confessions of a Python fanboy References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> Message-ID: <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> On Jul 30, 12:15?pm, Masklinn wrote: [snip] > Furthermore Ruby has a pretty nice convention (sadly not used enough I ? > think) taken from Scheme where it's possible to postfix a method name ? > with "!" (note: the "!" is part of the name, there's no magic) to ? > indicate that this method modifies the object it's called on rather ? > than simply returning stuff. Another oddity i did not like at first but must admit is growing on me vector.reverse --> returns a new reversed vector vector.reverse! --> modifies the instance vector in-place Of course in python you would do... vector.reverse --> in-place vector.reversed --> in-place The above example works pretty good, but this doesn't always "sound" good. Take for example this... point3d.offset --> return a new pt point3d.offseted --> What did you say!?!?! Now try Ruby's way point3d.offset! point3d.offset a symbol works better for these things From rurpy at yahoo.com Thu Jul 30 13:29:09 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 30 Jul 2009 10:29:09 -0700 (PDT) Subject: Help with Regex for domain names References: Message-ID: <0422bf8f-ce59-4de2-a1a4-b16a32e5f152@o15g2000yqm.googlegroups.com> On Jul 30, 9:56 am, MRAB wrote: > Feyo wrote: > > I'm trying to figure out how to write efficiently write a regex for > > domain names with a particular top level domain. Let's say, I want to > > grab all domain names with country codes .us, .au, and .de. > > > I could create three different regexs that would work: > > regex = re.compile(r'[\w\-\.]+\.us) > > regex = re.compile(r'[\w\-\.]+\.au) > > regex = re.compile(r'[\w\-\.]+\.de) > > > How would I write one to accommodate all three, or, better yet, to > > accommodate a list of them that I can pass into a method call? Thanks! > > > > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') You might also want to consider that some country codes such as "co" for Columbia might match more than you want, for example: re.match(r'[\w\-\.]+\.(?:us|au|de|co)', 'foo.boo.com') will match. From torriem at gmail.com Thu Jul 30 13:30:03 2009 From: torriem at gmail.com (Michael Torrie) Date: Thu, 30 Jul 2009 11:30:03 -0600 Subject: Does python have the capability for driver development ? In-Reply-To: <94abbddc-dac7-4215-84d1-d4eb3f82724d@h21g2000yqa.googlegroups.com> References: <87skgf83ua.fsf@rsw.digi.com.br> <94abbddc-dac7-4215-84d1-d4eb3f82724d@h21g2000yqa.googlegroups.com> Message-ID: <4A71D89B.6030301@gmail.com> MalC0de wrote: > please introduce me some resource for system programming, I know that > python is very well at system programming level. > thanks Although it is possible (as others have said) to embed Python the interpreter into a driver, no one has done that that I know of. You'd have to write the driver in C or C++, though, and then provide embed a python interpreter and then provide a python interface (wrapper code written in C++) that would expose binary primitives and structures to python that the OS and driver use. Sounds like quite an undertaking. I embedded a python interpreter in a gina dll once (to write login code in python). That's a form of system programming, but not what you're asking about. Someone years ago someone embedded the perl interpreter into a Linux driver allowing some kinds of device drivers to be written in perl. It was made more as a curiosity though. Python is not well-suited to system programming. It takes extra work (via the ctypes library or other wrappers) to interface with C structs, plus calls all have to be marshalled to and from the native C APIs. Might be cool, though. System programming in Win32 is very complicated and messy. What little system programming I've done in Linux was a dream compared. Definitely you'll have to master win32 system programming in C and C++ before you try to embed python in anything. I'm sure there are a number of books on the subject. That's where I'd start. From rt8396 at gmail.com Thu Jul 30 13:33:38 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:33:38 -0700 (PDT) Subject: Confessions of a Python fanboy References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: Traceback (most recent post last): File "", lines (13,14), in vector.reverse --> in-place vector.reversed --> in-place DumbMistakeError: Of course in python you would do... vector.reverse --> in-place vector.reversed --> new vector From jeanmichel at sequans.com Thu Jul 30 13:37:00 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 30 Jul 2009 19:37:00 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: <4A71DA3C.7060502@sequans.com> r wrote: > On Jul 30, 11:31 am, Falcolas wrote: > >> On Jul 29, 9:06 pm, r wrote: >> >> >>> 1.) No need to use "()" to call a function with no arguments. >>> Python --> "obj.m2().m3()" --ugly >>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>> Man, i must admit i really like this, and your code will look so much >>> cleaner. >>> >> I personally would not prefer this, and would likely continue to use >> (), precisely for code clarity - let me explain: >> >> foo.nextval >> foo.val >> foo.previousval >> >> Which of the calls above referenced instance variables, and which ones >> called functions which changed the internal state of foo? I would have >> trouble saying, just based on the calls above. I would have to go back >> to the definition or documentation of foo to identify which is doing >> what. On the other hand, the following gives a better clue as to what >> is happening (granted not perfect, but better): >> >> foo.nextval() >> foo.val >> foo.previousval() >> >> ~G >> > > I held your exact same view before i learned the Ruby language. And > your veiw makes some good points, however, naming conventions with > eliminate this problem all together. All "method names" should use the > underscore to separate words, "variable names" should use camelCase, > "constants" in all caps, and "class defs" in titlecase. > > def go_and_do_this_for_me_now(self, *args) > self.variableNameHere > MyClassNameHere > THISISACONSTANT -or- THIS_IS_A_CONSTANT > > > in your example i would have used the following > > foo.next_value > foo.value > foo.prev_value > > good naming conventions will make your life (and everybody else's) > much easier when debugging code. > > > > How do I know if foo.value is an attribute or if it is a method that returns the foo value ? It seems you can make the difference only when tokens are composed of more than one word, not very handy don't you think ? Just feeding the troll. JM From carsten.haese at gmail.com Thu Jul 30 13:42:30 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Thu, 30 Jul 2009 13:42:30 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: r wrote: > Of course in python you would do... > vector.reverse --> in-place > vector.reversed --> in-place You do know that only one of those works in-place, right? > The above example works pretty good, but this doesn't always "sound" > good. Take for example this... > point3d.offset --> return a new pt > point3d.offseted --> What did you say!?!?! > > Now try Ruby's way > point3d.offset! > point3d.offset > > a symbol works better for these things Or you could use a better verb: point3d.move -> modifies in place point3d.moved -> returns a new point -Carsten From rt8396 at gmail.com Thu Jul 30 13:45:49 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:45:49 -0700 (PDT) Subject: Confessions of a Python fanboy References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: <91508ea6-a9dd-4d80-81e6-c056c26aae5c@b15g2000yqd.googlegroups.com> On Jul 30, 12:37?pm, Jean-Michel Pichavant wrote: > r wrote: > > On Jul 30, 11:31 am, Falcolas wrote: > > >> On Jul 29, 9:06 pm, r wrote: > > >>> 1.) No need to use "()" to call a function with no arguments. > >>> Python --> "obj.m2().m3()" --ugly > >>> ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > >>> Man, i must admit i really like this, and your code will look so much > >>> cleaner. > > >> I personally would not prefer this, and would likely continue to use > >> (), precisely for code clarity - let me explain: > > >> foo.nextval > >> foo.val > >> foo.previousval > > >> Which of the calls above referenced instance variables, and which ones > >> called functions which changed the internal state of foo? I would have > >> trouble saying, just based on the calls above. I would have to go back > >> to the definition or documentation of foo to identify which is doing > >> what. On the other hand, the following gives a better clue as to what > >> is happening (granted not perfect, but better): > > >> foo.nextval() > >> foo.val > >> foo.previousval() > > >> ~G > > > I held your exact same view before i learned the Ruby language. And > > your veiw makes some good points, however, naming conventions with > > eliminate this problem all together. All "method names" should use the > > underscore to separate words, "variable names" should use camelCase, > > "constants" in all caps, and "class defs" in titlecase. > > > def go_and_do_this_for_me_now(self, *args) > > self.variableNameHere > > MyClassNameHere > > THISISACONSTANT -or- THIS_IS_A_CONSTANT > > > in your example i would have used the following > > > foo.next_value > > foo.value > > foo.prev_value > > > good naming conventions will make your life (and everybody else's) > > much easier when debugging code. > > How do I know if foo.value is an attribute or if it is a method that > returns the foo value ? It seems you can make the difference only when > tokens are composed of more than one word, not very handy don't you think ? > > Just feeding the troll. > > JM You can still use empty parenthesis in Ruby if that floats your boat. obj.method() <-- note the empty parenthesis I am just saying don't make them mandatory, thats all. In any case where ambiguity abounds, by all means use the damn parenthisis. Now Ruby will even let you call a method *that has arguments* with no parenthisis -- however, i do not think this is a good idea. Consider the following Geom::Transformation.rotation pt, vec, ang bad coder!, bad! ...and leads to worn out eyeball parsers in a hurry Geom::Transformation.rotation(pt, vec, ang) yes, good boy! From kyosohma at gmail.com Thu Jul 30 13:45:57 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Thu, 30 Jul 2009 10:45:57 -0700 (PDT) Subject: Bug in IEHtmlWindow? References: <8a5ed753-d437-45a6-8fee-300eea1207ca@c2g2000yqi.googlegroups.com> Message-ID: On Jul 30, 2:54?am, Massi wrote: > Hi everyone, I'm trying to use IEHtmlWindow in my application (python > 2.5, wxpython 2.8 on win xp). Everything is ok, but I encountered a > problem which also affects the demo. If I make a research on google a > strange message appears, saying (I'll try to translate from italian): > > Error during execution. > Do you want to run the debug? > > line 29: > error: 'r' is NULL or it is not an object > > Did anybody have the same problem? is it a bug? > Thanks in advance for your help. I've never seen that error. Could you re-post to the wxPython user's list? They can probably help you there: http://groups.google.com/group/wxPython Mike From digitig at gmail.com Thu Jul 30 13:47:04 2009 From: digitig at gmail.com (Tim Rowe) Date: Thu, 30 Jul 2009 18:47:04 +0100 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: 2009/7/30 r : > > Like your > first lay, your first programing language can leave an indelible mark > on you That's true. FOCAL scarred me for life. > but i now realize Ruby has some good > things going for it. Any language that gets any sort of real use has to have. For instance, I love Ada's numeric types (you can specify either the minimum number of significant figures or the maximum delta for a real type, and it will give you a type that satisfies that -- or a compilation error if it can't. That matches genuine problem domains far better than having to remember how many bits in a double on this particular system, and reduces portability bugs). > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). You know what i am talking about here people. We > all get complacent and It seems easier to just cope with these > problems instead of fighting for change. But look at the French, ?WHAT > THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! I seem to recall recent studies showing that the French were on average happier than Brits or Americans. Don't knock it! > As for the rest of Ruby, i am not impressed. The redundant usage of > "end" over indention perplexes me. The Perlish feel of "require" and > the horrifically cryptic idioms of Ruby regular expressions. The > "puts" and "gets" seem childish and the math class does not even have > a degrees or radians function! The operating system dependency built into the language did it for me. That and the fact that I couldn't stop laughing for long enough to learn any more when I read in the Pragmatic Programmer's Guide that "Ruby, unlike less flexible languages, lets you alter the value of a constant." Yep, as they say "Bug" = "Undocumented feature"! -- Tim Rowe From masklinn at masklinn.net Thu Jul 30 13:56:07 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:56:07 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4A71DA3C.7060502@sequans.com> References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> <4A71DA3C.7060502@sequans.com> Message-ID: On 30 Jul 2009, at 19:37 , Jean-Michel Pichavant wrote: > r wrote: >> On Jul 30, 11:31 am, Falcolas wrote: >> >>> On Jul 29, 9:06 pm, r wrote: >>> >>> >>>> 1.) No need to use "()" to call a function with no arguments. >>>> Python --> "obj.m2().m3()" --ugly >>>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>>> Man, i must admit i really like this, and your code will look so >>>> much >>>> cleaner. >>>> >>> I personally would not prefer this, and would likely continue to use >>> (), precisely for code clarity - let me explain: >>> >>> foo.nextval >>> foo.val >>> foo.previousval >>> >>> Which of the calls above referenced instance variables, and which >>> ones >>> called functions which changed the internal state of foo? I would >>> have >>> trouble saying, just based on the calls above. I would have to go >>> back >>> to the definition or documentation of foo to identify which is doing >>> what. On the other hand, the following gives a better clue as to >>> what >>> is happening (granted not perfect, but better): >>> >>> foo.nextval() >>> foo.val >>> foo.previousval() >>> >>> ~G >>> >> >> I held your exact same view before i learned the Ruby language. And >> your veiw makes some good points, however, naming conventions with >> eliminate this problem all together. All "method names" should use >> the >> underscore to separate words, "variable names" should use camelCase, >> "constants" in all caps, and "class defs" in titlecase. >> >> def go_and_do_this_for_me_now(self, *args) >> self.variableNameHere >> MyClassNameHere >> THISISACONSTANT -or- THIS_IS_A_CONSTANT >> >> >> in your example i would have used the following >> >> foo.next_value >> foo.value >> foo.prev_value >> >> good naming conventions will make your life (and everybody else's) >> much easier when debugging code. >> > How do I know if foo.value is an attribute or if it is a method that > returns the foo value ? It cannot be an attribute. Ruby doesn't give access to attributes, they're always solely private (as in Smalltalk). Also you shouldn't reply to r, he has no idea about what he's talking about. From masklinn at masklinn.net Thu Jul 30 13:57:54 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:57:54 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: <92229A0F-EC80-43C7-97B8-8C3636581EDE@masklinn.net> On 30 Jul 2009, at 19:42 , Carsten Haese wrote: > r wrote: >> Of course in python you would do... >> vector.reverse --> in-place >> vector.reversed --> in-place > > You do know that only one of those works in-place, right? > Well mostly because the other one doesn't exist (as python has `lst.reverse()` but `reversed(lst)`) but he was probably talking hypothetically. Or something. From user at example.net Thu Jul 30 14:05:53 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 20:05:53 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> r wrote: > On Jul 30, 12:15 pm, Masklinn wrote: > [snip] > >>Furthermore Ruby has a pretty nice convention (sadly not used enough I >>think) taken from Scheme where it's possible to postfix a method name >>with "!" (note: the "!" is part of the name, there's no magic) to >>indicate that this method modifies the object it's called on rather >>than simply returning stuff. > > > Another oddity i did not like at first but must admit is growing on me > vector.reverse --> returns a new reversed vector > vector.reverse! --> modifies the instance vector in-place > > Of course in python you would do... > vector.reverse --> in-place > vector.reversed --> in-place > how to reverse a string in python? must i usa a temp? like: >>> s = "ciccio" >>> l = list(s) >>> l.reverse() >>> s = "".join(l) >>> s 'oiccic' >>> ??? bye From garrickp at gmail.com Thu Jul 30 14:06:33 2009 From: garrickp at gmail.com (Falcolas) Date: Thu, 30 Jul 2009 11:06:33 -0700 (PDT) Subject: Confessions of a Python fanboy References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> <4A71DA3C.7060502@sequans.com> Message-ID: <776cafad-cfe8-442d-8d40-cab4e5c71d9b@c14g2000yqm.googlegroups.com> On Jul 30, 11:56?am, Masklinn wrote: > On 30 Jul 2009, at 19:37 , Jean-Michel Pichavant wrote: > > > r wrote: > > How do I know if foo.value is an attribute or if it is a method that ? > > returns the foo value ? > > It cannot be an attribute. Ruby doesn't give access to attributes, ? > they're always solely private (as in Smalltalk). Also you shouldn't ? > reply to r, he has no idea about what he's talking about. That doesn't sound like a feature I would appreciate much, though I am a big fan of "we're all adults here" programming. Though if they are all functions, then adding parentheses wouldn't make much difference, so offering the option of getting rid of them makes sense. >> I would have >> trouble saying, just based on the calls above. I would have to go back >> to the definition or documentation of foo to identify which is doing >> what. On the other hand, the following gives a better clue as to what >> is happening (granted not perfect, but better): > Well... it doesn't give much of a clue no really. It, with few exceptions, tells me that fetching the value foo.val will not modify the state of foo. Important stuff, at least to me. From user at example.net Thu Jul 30 14:08:36 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 20:08:36 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <4a71e1a5$0$40016$4fafbaef@reader3.news.tin.it> Tim Rowe wrote: > 2009/7/30 r : > >>Like your >>first lay, your first programing language can leave an indelible mark >>on you > > > That's true. FOCAL scarred me for life. > > >>but i now realize Ruby has some good >>things going for it. > > > Any language that gets any sort of real use has to have. For instance, > I love Ada's numeric types (you can specify either the minimum number > of significant figures or the maximum delta for a real type, and it > will give you a type that satisfies that -- or a compilation error if > it can't. That matches genuine problem domains far better doesn't the decimal module do some of that too? bye From darkneter at gmail.com Thu Jul 30 14:13:44 2009 From: darkneter at gmail.com (NighterNet) Date: Thu, 30 Jul 2009 11:13:44 -0700 (PDT) Subject: class or thread id count Message-ID: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> Need some help on doing some bit simple id count. It like every time it create a class or thread there is id++. Python 3.1 example: class ClientThread (threading.Thread): def __init__(self,clientSocket): threading.Thread.__init__(self) self.id += 1; Not sure it how it works. From python at mrabarnett.plus.com Thu Jul 30 14:20:11 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 19:20:11 +0100 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4A71E45B.3070009@mrabarnett.plus.com> superpollo wrote: > r wrote: >> On Jul 30, 12:15 pm, Masklinn wrote: >> [snip] >> >>> Furthermore Ruby has a pretty nice convention (sadly not used enough >>> I think) taken from Scheme where it's possible to postfix a method >>> name with "!" (note: the "!" is part of the name, there's no magic) >>> to indicate that this method modifies the object it's called on >>> rather than simply returning stuff. >> >> >> Another oddity i did not like at first but must admit is growing on me >> vector.reverse --> returns a new reversed vector >> vector.reverse! --> modifies the instance vector in-place >> >> Of course in python you would do... >> vector.reverse --> in-place >> vector.reversed --> in-place >> > > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? > Use slicing with a step of -1: >>> s = "ciccio" >>> s[::-1] 'oiccic' From masklinn at masklinn.net Thu Jul 30 14:22:21 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 20:22:21 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <772E43D2-61B0-4C26-AAA3-C1E9785176AF@masklinn.net> On 30 Jul 2009, at 20:05 , superpollo wrote: > r wrote: >> On Jul 30, 12:15 pm, Masklinn wrote: >> [snip] >>> Furthermore Ruby has a pretty nice convention (sadly not used >>> enough I think) taken from Scheme where it's possible to postfix >>> a method name with "!" (note: the "!" is part of the name, >>> there's no magic) to indicate that this method modifies the >>> object it's called on rather than simply returning stuff. >> Another oddity i did not like at first but must admit is growing on >> me >> vector.reverse --> returns a new reversed vector >> vector.reverse! --> modifies the instance vector in-place >> Of course in python you would do... >> vector.reverse --> in-place >> vector.reversed --> in-place > > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? > > bye s = s[::-1] From masklinn at masklinn.net Thu Jul 30 14:24:23 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 20:24:23 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <776cafad-cfe8-442d-8d40-cab4e5c71d9b@c14g2000yqm.googlegroups.com> References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> <4A71DA3C.7060502@sequans.com> <776cafad-cfe8-442d-8d40-cab4e5c71d9b@c14g2000yqm.googlegroups.com> Message-ID: On 30 Jul 2009, at 20:06 , Falcolas wrote: > On Jul 30, 11:56 am, Masklinn wrote: >> On 30 Jul 2009, at 19:37 , Jean-Michel Pichavant wrote: >> >>> r wrote: > >>> How do I know if foo.value is an attribute or if it is a method that >>> returns the foo value ? >> >> It cannot be an attribute. Ruby doesn't give access to attributes, >> they're always solely private (as in Smalltalk). Also you shouldn't >> reply to r, he has no idea about what he's talking about. > > That doesn't sound like a feature I would appreciate much, though I am > a big fan of "we're all adults here" programming. Though if they are > all functions, then adding parentheses wouldn't make much difference, > so offering the option of getting rid of them makes sense. > That's pretty much it. >>> I would have >>> trouble saying, just based on the calls above. I would have to go >>> back >>> to the definition or documentation of foo to identify which is doing >>> what. On the other hand, the following gives a better clue as to >>> what >>> is happening (granted not perfect, but better): > >> Well... it doesn't give much of a clue no really. > > It, with few exceptions, tells me that fetching the value foo.val will > not modify the state of foo. Important stuff, at least to me. But because of those few exceptions, it really tells you that foo.val *probably* doesn't modify the state of foo. Unless you go source- diving, you can't know for sure. Only Haskell gives you that kind of guarantees (and even then, the code you call could have an unsafe* somewhere) From davea at ieee.org Thu Jul 30 14:28:11 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 14:28:11 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4A71E63B.6080200@ieee.org> superpollo wrote: >> > > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? > > bye > > >
> simply s = s[::-1] DaveA From user at example.net Thu Jul 30 14:30:45 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 20:30:45 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4a71e6d6$0$40004$4fafbaef@reader3.news.tin.it> MRAB wrote: > superpollo wrote: ... >> how to reverse a string in python? must i usa a temp? like: >> >> >>> s = "ciccio" >> >>> l = list(s) >> >>> l.reverse() >> >>> s = "".join(l) >> >>> s >> 'oiccic' >> >>> >> >> ??? >> > Use slicing with a step of -1: > > >>> s = "ciccio" > >>> s[::-1] > 'oiccic' lol bye From hniksic at xemacs.org Thu Jul 30 14:32:10 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 30 Jul 2009 20:32:10 +0200 Subject: New implementation of re module References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> <4A717E96.3020900@mrabarnett.plus.com> <200907301429.07850.wolfgang@rohdewald.de> Message-ID: <87y6q6dnth.fsf@busola.homelinux.net> MRAB writes: > So it complains about: > > ++(RE_CHAR*)context->text_ptr > > but not about: > > ++info->repeat.count > > Does this mean that the gcc compiler thinks that the cast makes it an > rvalue? The cast operator does return an rvalue, treating it otherwise used to be an extension to popular compilers, including ironically gcc. The standard-compliant way of writing the above would be: ++ *(RE_CHAR **) &context->text_ptr From wells at submute.net Thu Jul 30 14:45:53 2009 From: wells at submute.net (Wells Oliver) Date: Thu, 30 Jul 2009 13:45:53 -0500 Subject: Sorting dict by value w/ operator.itemgetter- using key name? Message-ID: <3f1a902d0907301145t2aa559b7ifcb3dc45d9b7a363@mail.gmail.com> Bit of code: print sorted(results.items(), key=operator.itemgetter(1)) Would rather use 'H9', which is the name of the key in position 1 like: print sorted(results.items(), key=operator.itemgetter('H9')) Obviously that ain't work else I wouldn't be sending this email. Any tips? -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.com Thu Jul 30 14:51:23 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 30 Jul 2009 19:51:23 +0100 Subject: Help with Regex for domain names References: <0422bf8f-ce59-4de2-a1a4-b16a32e5f152@o15g2000yqm.googlegroups.com> Message-ID: On Thu, 30 Jul 2009 10:29:09 -0700, rurpy wrote: >> regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') > > You might also want to consider that some country > codes such as "co" for Columbia might match more than > you want, for example: > > re.match(r'[\w\-\.]+\.(?:us|au|de|co)', 'foo.boo.com') > > will match. ... so put \b at the end, i.e.: regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)\b') From sri_annauni at yahoo.co.in Thu Jul 30 15:04:09 2009 From: sri_annauni at yahoo.co.in (srinivasan srinivas) Date: Fri, 31 Jul 2009 00:34:09 +0530 (IST) Subject: calling obect method Message-ID: <960855.56660.qm@web7906.mail.in.yahoo.com> Hi, I would like to know if i have an object and one of its methods as a string. How to call the method using this string? For ex If object is x and string y has one of its method names. x.value(y)() ---. Is there a way to do this? Thanks, Srini See the Web's breaking stories, chosen by people like you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/ From rt8396 at gmail.com Thu Jul 30 15:08:25 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 12:08:25 -0700 (PDT) Subject: calling obect method References: Message-ID: <2e8c5c91-be27-430b-9cad-46a5bff3ee37@26g2000yqk.googlegroups.com> >>> import math >>> getattr(math, 'hypot')(3,4) 5.0 From rt8396 at gmail.com Thu Jul 30 15:14:12 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 12:14:12 -0700 (PDT) Subject: class or thread id count References: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> Message-ID: On Jul 30, 1:13?pm, NighterNet wrote: > Need some help on doing some bit simple id count. It like every time > it create a class or thread there is id++. Python 3.1 > > example: > class ClientThread (threading.Thread): > ? ?def __init__(self,clientSocket): > ? ? ? threading.Thread.__init__(self) > ? ? ? ? ?self.id += 1; > > Not sure it how it works. use a class atrribute >>> class Person(): count = 0 def __init__(self, name, info): self.name = name self.info = info Person.count+=1 >>> p1 = Person('alex23', 'very annoying') >>> p2 = Person('Guido', 'very intelligent') >>> p2.count 2 >>> p1.count 2 >>> Person.count 2 From zuo at chopin.edu.pl Thu Jul 30 15:20:21 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 21:20:21 +0200 Subject: socket send In-Reply-To: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: 30-07-2009 o 05:52:06 NighterNet wrote: > I am trying to figure out how to send text or byte in python 3.1. I am > trying to send data to flash socket to get there. I am not sure how to > work it. > > buff= 'id=' , self.id , ':balive=False\n' > clientSock.send(buff); And what is the problem? *j -- Jan Kaliszewski (zuo) From bdezonia at wisc.edu Thu Jul 30 15:36:18 2009 From: bdezonia at wisc.edu (BDZ) Date: Thu, 30 Jul 2009 12:36:18 -0700 (PDT) Subject: os.path.exists() and Samba shares Message-ID: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> Hello. I have written a Python 3.1 script running on Windows that uses os.path.exists() to connect to network shares. If the various network shares require different user account and password combos than the account the script is running under the routine returns false. I need something like os.samba.path.exists(username,password,path). Does anyone have a suggestion on how I can accomplish what I need to do in Python? From martin at v.loewis.de Thu Jul 30 15:56:24 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Thu, 30 Jul 2009 21:56:24 +0200 Subject: No PyPI search for 3.x compatible packages In-Reply-To: References: Message-ID: <4a71fae9$0$10471$9b622d9e@news.freenet.de> Neil Hodgson wrote: > There appears to be no way to search PyPI for packages that are > compatible with Python 3.x. There are classifiers for 'Programming > Language' including 'Programming Language :: Python :: 3' but that seems > to be for implementation language since there are so many packages that > specify C. Not at all. The Trove classifiers *always* mean what the package author wants them to mean. In the specific case of Python :: 3, they should mean what makes most sense. For an application, it might make most sense to specify the language it is written in. For a Python library, it surely would make most sense to specify the Python version it supports - notice, however, that this is *also* the Python version the library is written in. Regards, Martin From darkneter at gmail.com Thu Jul 30 16:09:36 2009 From: darkneter at gmail.com (NighterNet) Date: Thu, 30 Jul 2009 13:09:36 -0700 (PDT) Subject: class or thread id count References: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> Message-ID: <68b09353-7e30-4df2-8e05-32159b03414a@f20g2000prn.googlegroups.com> On Jul 30, 12:14?pm, r wrote: > On Jul 30, 1:13?pm, NighterNet wrote: > > > Need some help on doing some bit simple id count. It like every time > > it create a class orthreadthere is id++. Python 3.1 > > > example: > > class ClientThread (threading.Thread): > > ? ?def __init__(self,clientSocket): > > ? ? ? threading.Thread.__init__(self) > > ? ? ? ? ?self.id += 1; > > > Not sure it how it works. > > use a class atrribute > > >>> class Person(): > > ? ? ? ? count = 0 > ? ? ? ? def __init__(self, name, info): > ? ? ? ? ? ? ? ? self.name = name > ? ? ? ? ? ? ? ? self.info = info > ? ? ? ? ? ? ? ? Person.count+=1 > > >>> p1 = Person('alex23', 'very annoying') > >>> p2 = Person('Guido', 'very intelligent') > >>> p2.count > 2 > >>> p1.count > 2 > >>> Person.count > > 2 Thanks it work. class Person(): count = 0 def __init__(self, name, info): self.name = name self.info = info Person.count+=1 self.count = Person.count From zuo at chopin.edu.pl Thu Jul 30 16:16:13 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 22:16:13 +0200 Subject: socket send In-Reply-To: <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> Message-ID: 30-07-2009 o 12:29:24 Francesco Bochicchio wrote: > On Jul 30, 5:52?am, NighterNet wrote: >> I am trying to figure out how to send text or byte in python 3.1. I am >> trying to send data to flash socket to get there. I am not sure how to >> work it. >> >> buff= 'id=' , self.id , ':balive=False\n' >> clientSock.send(buff); > > Try putting a 'b' before the constant string that you want to send: > > >>> type(b'123') > It's true. In python '...' literals are for strings ('str' type) and b'...' literals are for binary data (bytes type). Sockets' send() and recv() need binary data ('bytes' objects). > or use something like this to convert non constant strings (with only > ASCII characters) into bytes: > >>> s = "A non-constant string : %d " % n > >>> s > 'A non-constant string : 12 ' > >>> type(s) > What??? 'str' type in Python 3.x IS NOT a type of "non-constant" strings and IS NOT a type of strings "with only ASCII characters"! 'str' type in Python 3.x *is* the type of immutable ('constant') and Unicode character (Unicode, not only ASCII) strings. It's the same what 'unicode' type in Python 2.x. 'bytes' type objects in Python 3.x are also immutable. They are good for binary data (also text data encoded to binary data; see below). 'bytearray' type objects are like 'bytes', but mutable (like e. g. lists). >>>> b = bytes ( ord(c) for c in s ) >>>> b > b'A non-constant string : 12 ' > > You could also use struct.pack , that in python 3.x returns bytes and > not strings. In this case you need to specify the size of the string > (extra bytes are zero-filled): > > import struct What for all that weird actions??? * Simply do: bytes_object = str_object.encode() or bytes_object = bytes(str_object, ) -- where could be e.g. 'ascii', 'utf-8', 'iso-8859-1' etc.; using the former method, you can omit this argument -> then default encoding will be used (usualy 'utf-8'). * As easily as that, you can convert bytes to str: str_object = bytes_object.decode() or str_object = str(bytes_object, ) * Some examples: >>> 'What makes you think she is a witch?' # it's str 'What makes you think she is a witch?' >>> 'What makes you think she is a witch?'.encode() # to bytes b'What makes you think she is a witch?' >>> 'What makes you think she is a witch?'.encode('utf-8') # to bytes b'What makes you think she is a witch?' >>> bytes('What makes you think she is a witch?') # to bytes??? Traceback (most recent call last): File "", line 1, in TypeError: string argument without an encoding >>> bytes('What makes you think she is a witch?', 'ascii') # to bytes b'What makes you think she is a witch?' >>> b'What makes you think she is a witch?'.decode() # to str 'What makes you think she is a witch?' >>> b'What makes you think she is a witch?'.decode('utf-8') # to str 'What makes you think she is a witch?' >>> str(b'What makes you think she is a witch?', 'utf-8') # to str 'What makes you think she is a witch?' >>> str(b'What makes you think she is a witch?') # but not this way "b'What makes you think she is a witch?'" [^ note this] Cheers, *j -- Jan Kaliszewski (zuo) From zuo at chopin.edu.pl Thu Jul 30 16:23:32 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 22:23:32 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> Message-ID: 30-07-2009 o 13:36:49 Masklinn wrote: > On 30 Jul 2009, at 06:04 , alex23 wrote: >> On Jul 30, 1:06 pm, r wrote: >>> 2.) the .each method >>> container.each{|localVar| block} >>> This method can really cleanup some ugly for loops, although i really >>> like the readability of for loops. >> >> map(lambda localVar: , sequence) >> >> or: >> >> def usefully_named_func(var): >> >> return var >> >> transformed = [usefully_named_func(v) for v in sequence] >> > The issue here is of course that `map` and comprehensions are > transformations. `#each` exists for effectful iterations (Ruby has > `#map` for the map operation). So the intent expressed by `#each` and > `map` isn't the same. Furthermore and this is the most problematic > limitation of Python here, `lambda` doesn't allow complex > transformations due to its restrictions, so one has to switch to named > functions which works but isn't sexy (and tends to lower readability > imo). I don't see any real limitation. What's wrong in: for localVar in container: block And ruby's container.each is very similar to Python's iter() *j -- Jan Kaliszewski (zuo) From masklinn at masklinn.net Thu Jul 30 16:41:57 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 22:41:57 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> Message-ID: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: > 30-07-2009 o 13:36:49 Masklinn wrote: > >> On 30 Jul 2009, at 06:04 , alex23 wrote: >>> On Jul 30, 1:06 pm, r wrote: >>>> 2.) the .each method >>>> container.each{|localVar| block} >>>> This method can really cleanup some ugly for loops, although i >>>> really >>>> like the readability of for loops. >>> >>> map(lambda localVar: , sequence) >>> >>> or: >>> >>> def usefully_named_func(var): >>> >>> return var >>> >>> transformed = [usefully_named_func(v) for v in sequence] >>> >> The issue here is of course that `map` and comprehensions are >> transformations. `#each` exists for effectful iterations (Ruby has >> `#map` for the map operation). So the intent expressed by `#each` >> and `map` isn't the same. Furthermore and this is the most >> problematic limitation of Python here, `lambda` doesn't allow >> complex transformations due to its restrictions, so one has to >> switch to named functions which works but isn't sexy (and tends to >> lower readability imo). > > I don't see any real limitation. What's wrong in: > > for localVar in container: > block > Well what's wrong with using that rather than `map`, `filter` or a list comprehension? (and if you don't see what the limitations of `lambda` are, you probably very rarely use it) > And ruby's container.each is very similar to Python's iter() > Uh? not at all? From tjreedy at udel.edu Thu Jul 30 16:55:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 16:55:17 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > r wrote: > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? No. >>> ''.join(list(reversed('abc'))) 'cba' >>> 'abc'[2::-1] 'cba' >>> 'abc'[1000000000::-1] 'cba' Any int >= len(string)-1 will do, so the call to len will usually not be necessary. Needing strings reversed is not common. More common might be for char in reversed(somestring): process(char) Terry Jan Reedy From rt8396 at gmail.com Thu Jul 30 17:02:03 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 14:02:03 -0700 (PDT) Subject: Confessions of a Python fanboy References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <39013d06-e281-4459-bdb2-19831c9526ea@k1g2000yqf.googlegroups.com> On Jul 30, 3:55?pm, Terry Reedy wrote: > superpollo wrote: > > r wrote: > > how to reverse a string in python? must i usa a temp? like: [snip] > Terry Jan Reedy No "r" never wrote anything like that. reversing a string is RTFM material, this is basic stuff here! Stop quoting me as saying things i did not say! I never asked how to reverse a string, "superpollo" did. From tjreedy at udel.edu Thu Jul 30 17:02:20 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 17:02:20 -0400 Subject: Sorting dict by value w/ operator.itemgetter- using key name? In-Reply-To: <3f1a902d0907301145t2aa559b7ifcb3dc45d9b7a363@mail.gmail.com> References: <3f1a902d0907301145t2aa559b7ifcb3dc45d9b7a363@mail.gmail.com> Message-ID: Wells Oliver wrote: > Bit of code: > > print sorted(results.items(), key=operator.itemgetter(1)) > > Would rather use 'H9', which is the name of the key in position 1 like: > > print sorted(results.items(), key=operator.itemgetter('H9')) > > Obviously that ain't work else I wouldn't be sending this email. Any tips? Suppose namepos=['H9':1,} etc, then itemgetter(namepos['H9']) From ethan at stoneleaf.us Thu Jul 30 17:04:21 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 30 Jul 2009 14:04:21 -0700 Subject: Regexp problem In-Reply-To: References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> Message-ID: <4A720AD5.1030202@stoneleaf.us> Marcus Wanner wrote: > On 7/30/2009 9:32 AM, Beldar wrote: > >> On 30 jul, 15:07, MRAB wrote: >> >>> Beldar wrote: >>> >>>> Hi there! >>>> I have a problem and i'm not very good at regular expressions. >>>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>>> lalala" I need a regexp to get the 'beldar' part, the format is >>>> 'something-is-listening', i need to get the something part, use it in >>>> my code, and then replace the whole 'something-is-listening' for >>>> another string. >>> >>> \w+ will match a word and enclosing it in (...) will capture what was >>> matched: >>> >>> m = re.search(r"(\w+)-is-listening", text) >>> print "Captured '%s'" % m.group(1) >>> print "Matched from %d to %d" % (m.start(), m.end()) >> >> >> Ok, thank you all, it was very helpful! > > Wow, I really need to learn more about regexp... > Any tutorials you guys can recommend? > > Marcus Mastering Regular Expressions Powerful Techniques for Perl and Other Tools By Jeffrey E. F. Friedl Great book! ~Ethan~ From python at mrabarnett.plus.com Thu Jul 30 17:28:55 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 22:28:55 +0100 Subject: Help with Regex for domain names In-Reply-To: References: <0422bf8f-ce59-4de2-a1a4-b16a32e5f152@o15g2000yqm.googlegroups.com> Message-ID: <4A721097.50201@mrabarnett.plus.com> Nobody wrote: > On Thu, 30 Jul 2009 10:29:09 -0700, rurpy wrote: > >>> regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') >> You might also want to consider that some country >> codes such as "co" for Columbia might match more than >> you want, for example: >> >> re.match(r'[\w\-\.]+\.(?:us|au|de|co)', 'foo.boo.com') >> >> will match. > > ... so put \b at the end, i.e.: > > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)\b') > It would still match "www.bbc.co.uk", so you might need: regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)\b(?!\.\b)') From emmanuel.surleau at gmail.com Thu Jul 30 17:29:21 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Thu, 30 Jul 2009 23:29:21 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <200907302329.21286.emmanuel.surleau@gmail.com> > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > Ruby --> "obj.m1.m2.m3" -- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. It has benefits - code does look better. It has also significant cons - it is ambiguous. For instance: a = b Is b a variable or a method called without parameter? > 2.) the .each method > container.each{|localVar| block} > This method can really cleanup some ugly for loops, although i really > like the readability of for loops. It's not only the 'each' method. It's "anonymous callbacks everywhere", which is pretty cool. Certainly one of my favourite features of Ruby. > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). You know what i am talking about here people. We > all get complacent and It seems easier to just cope with these > problems instead of fighting for change. While ob.length() is more clear to read, it follows the same "operator" logic as __iter__. Which is OK, I suppose. > But look at the French, WHAT > THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! I assume this was supposed to be funny? > As for the rest of Ruby, i am not impressed. The redundant usage of > "end" over indention perplexes me. You have the choice to use {}. If you are confused over languages which do not use indentation to delimit code blocks, you should probably try out a few languages outside Python. > The Perlish feel of "require" and > the horrifically cryptic idioms of Ruby regular expressions. Cryptic idioms of Ruby regular expressions? They are Perl-like regular expressions, not exactly a far cry from what re does. Also, you can chain them, which is extremely nice. > The "puts" and "gets" seem childish Well, they're short and easy. Writing "gets" is more intuitive than using raw_input() > and the math class does not even have > a degrees or radians function! > > Anyway, i thought i would get this off my chest and catch up with old > friends in the meantime. We can all make Python the perfect language > it needs to be, but it ain't gonna be easy! > Thank you all > > PS stay tuned for more from this series.... Cheers, Emm From python at mrabarnett.plus.com Thu Jul 30 17:40:46 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 22:40:46 +0100 Subject: Regexp problem In-Reply-To: <4A720AD5.1030202@stoneleaf.us> References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> <4A720AD5.1030202@stoneleaf.us> Message-ID: <4A72135E.7060205@mrabarnett.plus.com> Ethan Furman wrote: > Marcus Wanner wrote: >> On 7/30/2009 9:32 AM, Beldar wrote: >> >>> On 30 jul, 15:07, MRAB wrote: >>> >>>> Beldar wrote: >>>> >>>>> Hi there! >>>>> I have a problem and i'm not very good at regular expressions. >>>>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>>>> lalala" I need a regexp to get the 'beldar' part, the format is >>>>> 'something-is-listening', i need to get the something part, use it in >>>>> my code, and then replace the whole 'something-is-listening' for >>>>> another string. >>>> >>>> \w+ will match a word and enclosing it in (...) will capture what was >>>> matched: >>>> >>>> m = re.search(r"(\w+)-is-listening", text) >>>> print "Captured '%s'" % m.group(1) >>>> print "Matched from %d to %d" % (m.start(), m.end()) >>> >>> >>> Ok, thank you all, it was very helpful! >> >> Wow, I really need to learn more about regexp... >> Any tutorials you guys can recommend? >> >> Marcus > > Mastering Regular Expressions > Powerful Techniques for Perl and Other Tools > By Jeffrey E. F. Friedl > > Great book! > +1 I have the first edition, seventh printing (December 1998). It refers to the 'regex' module of Python 1.4b1, which was subsequently replaced by the current 're' module and then removed from the standard library. I hope it's been updated since then. :-) From loic.domaigne at googlemail.com Thu Jul 30 17:41:50 2009 From: loic.domaigne at googlemail.com (=?ISO-8859-1?Q?Lo=EFc_Domaign=E9?=) Date: Thu, 30 Jul 2009 14:41:50 -0700 (PDT) Subject: os.path.exists() and Samba shares References: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> Message-ID: <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> Hi, > Hello. I have written a Python 3.1 script running on Windows that uses > os.path.exists() to connect to network shares. If the various network > shares require different user account and password combos than the > account the script is running under the routine returns false. I need > something like os.samba.path.exists(username,password,path). Does > anyone have a suggestion on how I can accomplish what I need to do in > Python? Could the Python Samba module PySamba be interesting for you? http://sourceforge.net/projects/pysamba/ HTH, Lo?c -- My blog: http://www.domaigne.com/blog From python at mrabarnett.plus.com Thu Jul 30 17:44:31 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 22:44:31 +0100 Subject: New implementation of re module In-Reply-To: <200907301524.28703.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <4A719880.3070300@mrabarnett.plus.com> <200907301518.28122.wolfgang@rohdewald.de> <200907301524.28703.wolfgang@rohdewald.de> Message-ID: <4A72143F.6070506@mrabarnett.plus.com> Wolfgang Rohdewald wrote: > On Thursday 30 July 2009, Wolfgang Rohdewald wrote: >> so I did the conversion mentioned there. This works: > > I actually do not know if it works - but it compiles. > Yes, it works. I've updated my code accordingly and it'll be in the next release. From rt8396 at gmail.com Thu Jul 30 17:44:34 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 14:44:34 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> On Jul 30, 4:29?pm, Emmanuel Surleau wrote: > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > It has benefits - code does look better. It has also significant cons - it is > ambiguous. > For instance: > > a = b > > Is b a variable or a method called without parameter? Hello Emanuel, Again, who so ever names a method with such a non-descriptive name will get whats coming to him. And if you did for some reason use such a cryptic name as "b", do yourself (and everyone else) a favor and follow it with "()" to denote the method call. Remember when something is optional that means you have an option to use it OR not use it. optional (http://definr.com/optional) adj: possible but not necessary; left to personal choice [ant: obligatory] simple solutions for simple problems. From zuo at chopin.edu.pl Thu Jul 30 17:52:45 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 23:52:45 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: Dnia 30-07-2009 o 22:41:57 Masklinn napisa?(a): > On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: >> 30-07-2009 o 13:36:49 Masklinn wrote: >> >>> On 30 Jul 2009, at 06:04 , alex23 wrote: >>>> On Jul 30, 1:06 pm, r wrote: >>>>> 2.) the .each method >>>>> container.each{|localVar| block} >>>>> This method can really cleanup some ugly for loops, although i really >>>>> like the readability of for loops. >>>> >>>> map(lambda localVar: , sequence) >>>> >>>> or: >>>> >>>> def usefully_named_func(var): >>>> >>>> return var >>>> >>>> transformed = [usefully_named_func(v) for v in sequence] >>>> >>> The issue here is of course that `map` and comprehensions are >>> transformations. `#each` exists for effectful iterations (Ruby has >>> `#map` for the map operation). So the intent expressed by `#each` and >>> `map` isn't the same. Furthermore and this is the most problematic >>> limitation of Python here, `lambda` doesn't allow complex >>> transformations due to its restrictions, so one has to switch to named >>> functions which works but isn't sexy (and tends to lower readability >>> imo). >> >> I don't see any real limitation. What's wrong in: >> >> for localVar in container: >> block >> > Well what's wrong with using that rather than `map`, `filter` or a list > comprehension? (and if you don't see what the limitations of `lambda` > are, you probably very rarely use it) I know well about the expression-only-limitation of lambda, fortnately there is the 'for' loop construct (or, alternatively, you can define a named function). And then, what's wrong with using 'for' rather than 'map', 'filter' etc.? Agree, that 'anonymous block syntax' would be nice in some cases, but I don't see any real limitation caused by lack of it i.e. something you can't (all you can only with unreasonable effort). >> And ruby's container.each is very similar to Python's iter() > Uh? not at all? OK, .each is like Python's iter() + some form of iterating over it ('for' loop or '[i]map'...). Still, in this matter there is no real functionality in Ruby that you can't reach in Python. All that confessions of a (former?) Python fanboy are about sintactic sugars, which importance is incomparable which such issues as e.g. particular mechanism of classes, their inheritance etc. or even such small helpers like function annotations. Cheers, *j -- Jan Kaliszewski (zuo) From kyrie at uh.cu Thu Jul 30 17:57:48 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Thu, 30 Jul 2009 17:57:48 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <200907301757.48761.kyrie@uh.cu> On Thursday 30 July 2009 04:41:57 pm Masklinn wrote: > On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: > > 30-07-2009 o 13:36:49 Masklinn wrote: > > > > I don't see any real limitation. What's wrong in: > > > > for localVar in container: > > block > > Well what's wrong with using that rather than `map`, `filter` or a > list comprehension? As I understood the question, it was "was wrong in 'for var in container' in comparison with ruby's container.each?" What's the (semantic) difference between for localVar in container: block and container.each{|localVar| block} ? (the sintactic difference is quite clear... if there is no semantic difference, I'd rather use the first version!) Map, filter and comprehensions have little to do with that (syntactic sugar that may allow some optimizations, but that could otherwise be replaced by the for loop). > (and if you don't see what the limitations of > `lambda` are, you probably very rarely use it) I'm not the poster, but: * I use lambdas, a lot. * I "know" python lambdas are limited, compared with theoretical lambdas. * I don't [usually] see that as a "limitation". Most of the time, the limitation is the lack of multiline/multiexpression lambdas. But I've never seen a case where I would /prefer/ to have a multiline function definition inside a comprehension or method invocation, instead of a named function defined two lines before it. (The second famous limitation is the inability to use statements inside lambdas... luckily, now that we have a ternary operator and a print function, it is less limiting) > > And ruby's container.each is very similar to Python's iter() > > Uh? not at all? I agree with this. It should have said that they are equivalent in the sense that they represent the iterator protocols of both languages, and nothing more. I'd like to ask, what "container.each" is, exactly? It looks like a function call (as I've learned a few posts ago), but, what are its arguments? How the looping "works"? Does it receive a "code" object that it has to execute? Is .each some kind of magic keyword? (This has little to do with python or the current thread, so feel free to reply off-list if you want to...) Regards, Luis. -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From python at mrabarnett.plus.com Thu Jul 30 18:06:05 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 23:06:05 +0100 Subject: New implementation of re module In-Reply-To: References: Message-ID: <4A72194D.4050604@mrabarnett.plus.com> Piet van Oostrum wrote: >>>>>> MRAB (M) wrote: > >> M> Hi all, >> M> I've been working on a new implementation of the re module. The details >> M> are at http://bugs.python.org/issue2636, specifically from >> M> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >> M> Python 2.6 on Windows if you want to try it out. > >> M> I'm interested in how fast it is generally, compared with the current re >> M> module, but especially when faced with those 'pathological' regular >> M> expressions which seem to take a long time to finish, for example: > >> M> re.search(r"^(.+|D)*A$", "x" * 25 + "B") > >> M> which on my PC (1.8GHz) takes 18.98secs with the re module but <0.01secs >> M> with this new implementation. > > Is this version also going to use the Thompson approach? That approach is what inspired the method in the new implementation, but its speed comes from asking _whether_ there's a match, not _where_ there's a match, so it can ignore the route taken to get the match. "grep", for example, selects those lines which match a pattern, whereas the typical Python (and Perl) usage is to extract part of a string or which part of the string matches. Trying to support lazy and possessive quantifiers in Thompson's approach is tricky (at least, I haven't been able to figure it out), and back-references are right out! :-) There's always the chance that I could come up with some sort of hybrid scheme, applying different approaches depending on certain conditions. It already switches from depth-first to breadth-first when it's taken too many iterations without advancing and merges similar states, which is why it's so much better with 'pathological' patterns; I just have to get the depth-first phase to be as fast as the current 're' module. From benjamin.kaplan at case.edu Thu Jul 30 18:08:21 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 30 Jul 2009 18:08:21 -0400 Subject: calling obect method In-Reply-To: <960855.56660.qm@web7906.mail.in.yahoo.com> References: <960855.56660.qm@web7906.mail.in.yahoo.com> Message-ID: On Thu, Jul 30, 2009 at 3:04 PM, srinivasan srinivas < sri_annauni at yahoo.co.in> wrote: > Hi, > I would like to know if i have an object and one of its methods as a > string. How to call the method using this string? > For ex > If object is x and string y has one of its method names. > x.value(y)() ---. Is there a way to do this? > > Thanks, > Srini getattr(x,y)() > > > > See the Web's breaking stories, chosen by people like you. Check out > Yahoo! Buzz. http://in.buzz.yahoo.com/ > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rt8396 at gmail.com Thu Jul 30 18:13:41 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 15:13:41 -0700 (PDT) Subject: Confessions of a Python fanboy References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <80839e72-9df4-4590-b22d-4c5c829e37b6@h11g2000yqb.googlegroups.com> On Jul 30, 4:57?pm, Luis Zarrabeitia wrote: [snip] > I'd like to ask, what "container.each" is, exactly? It looks like a function > call (as I've learned a few posts ago), but, what are its arguments? How the > looping "works"? Does it receive a "code" object that it has to execute? > Is .each some kind of magic keyword? (This has little to do with python or > the current thread, so feel free to reply off-list if you want to...) > > Regards, > > Luis. Hello Luis, I think your question is completely valid within the context of this thread. The purpose of his thread was to get feedback on how Python and Ruby ideas could be cumulated into the best high level language. And being that i am the BDFL of the "Confessions of a Python Fanboy" thread, you have my personal permission to continue on with this subject matter..., From rcdailey at gmail.com Thu Jul 30 18:40:37 2009 From: rcdailey at gmail.com (Robert Dailey) Date: Thu, 30 Jul 2009 15:40:37 -0700 (PDT) Subject: Printing with colors in a portable way Message-ID: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Anyone know of a way to print text in Python 3.1 with colors in a portable way? In other words, I should be able to do something like this: print_color( "This is my text", COLOR_BLUE ) And this should be portable (i.e. it should work on Linux, Mac, Windows). From nyamatongwe+thunder at gmail.com Thu Jul 30 18:47:35 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Thu, 30 Jul 2009 22:47:35 GMT Subject: No PyPI search for 3.x compatible packages In-Reply-To: References: Message-ID: Francesco Bochicchio : > Are you sure? I note that for example pygtk has as language tags both > C and python. So maybe a C extension > for python3 would have both C and python 3 as language tags. > > I suspect that the 109 packages you found are the only ones obf the > 4829 which works with python3 (but I hope > to be wrong ). There are 523 packages marked with Python :: [2, 2.1, ...] and I hope more than that work with Python 2.x. What I would like to encourages is adding Python :: 3 or a more specific minimum supported version classifier to packages that support Python 3. I wouldn't want to go to the level of adding a classifier for each version of Python supported since a package will often stay valid when a new Python is released. Neil From gdamjan at gmail.com Thu Jul 30 18:59:31 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Fri, 31 Jul 2009 00:59:31 +0200 Subject: [ANN] pyKook 0.0.2 - a simple build tool similar to Make or Ant References: Message-ID: > I have released pyKook 0.0.2. > http://pypi.python.org/pypi/Kook/0.0.2 > http://www.kuwata-lab.com/kook/ > http://www.kuwata-lab.com/kook/pykook-users-guide.html How does it compare to http://code.google.com/p/fabricate/ and why is this problem (solution) reoccurring all the time -- ?????? ( http://softver.org.mk/damjan/ ) Teaching a pig to sing wastes your time & annoys the pig. From robert.kern at gmail.com Thu Jul 30 19:06:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 30 Jul 2009 18:06:31 -0500 Subject: Confessions of a Python fanboy In-Reply-To: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> Message-ID: On 2009-07-30 16:44, r wrote: > On Jul 30, 4:29 pm, Emmanuel Surleau > wrote: >>> 1.) No need to use "()" to call a function with no arguments. >>> Python --> "obj.m2().m3()" --ugly >>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>> Man, i must admit i really like this, and your code will look so much >>> cleaner. >> It has benefits - code does look better. It has also significant cons - it is >> ambiguous. >> For instance: >> >> a = b >> >> Is b a variable or a method called without parameter? > > Hello Emanuel, > Again, who so ever names a method with such a non-descriptive name > will get whats coming to him. And if you did for some reason use such > a cryptic name as "b", do yourself (and everyone else) a favor and > follow it with "()" to denote the method call. Remember when something > is optional that means you have an option to use it OR not use it. I believe his point is that it is ambiguous to the compiler, not humans reading the code. Python functions and methods are first class objects. They can be passed around. If they were auto-called, then you could not do this. -- 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 rhodri at wildebst.demon.co.uk Thu Jul 30 19:29:50 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 31 Jul 2009 00:29:50 +0100 Subject: Printing with colors in a portable way In-Reply-To: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: On Thu, 30 Jul 2009 23:40:37 +0100, Robert Dailey wrote: > Anyone know of a way to print text in Python 3.1 with colors in a > portable way? In other words, I should be able to do something like > this: > > print_color( "This is my text", COLOR_BLUE ) > > And this should be portable (i.e. it should work on Linux, Mac, > Windows). ...in an xterm, over a telnet connection, via VNC... There's no portable way of doing this in any language, since it's up to the terminal emulator exactly what it does with what incoming bytes. Some respect the ANSI colour codes, some don't; that's about all that you can say. -- Rhodri James *-* Wildebeest Herder to the Masses From zuo at chopin.edu.pl Thu Jul 30 19:54:20 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Fri, 31 Jul 2009 01:54:20 +0200 Subject: Printing with colors in a portable way In-Reply-To: References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: 31-07-2009 o 01:29:50 Rhodri James wrote: > On Thu, 30 Jul 2009 23:40:37 +0100, Robert Dailey > wrote: > >> Anyone know of a way to print text in Python 3.1 with colors in a >> portable way? In other words, I should be able to do something like >> this: >> >> print_color( "This is my text", COLOR_BLUE ) >> >> And this should be portable (i.e. it should work on Linux, Mac, >> Windows). > > ...in an xterm, over a telnet connection, via VNC... > > There's no portable way of doing this in any language, since it's > up to the terminal emulator exactly what it does with what incoming > bytes. Some respect the ANSI colour codes, some don't; that's about > all that you can say. Yeah. Although you could try to provide "term-sensitive" mapping of colour names -> codes, e.g.: # color_codes.py class NoColors: blue = '' red = '' class XtermColors: blue = '
' red = '' class FooBarColors: blue = '' red = '' COLORS = { 'nocolors': NoColors, 'xterm': XtermColors, 'another term': FooBarColors, } # a_program.py import color_codes # e.g. with used_term == 'xterm'... colors = color_codes.COLORS[used_term] print('Some text, {colors.blue}Something in blue, ' '{colors.red}And now in red.').format(colors=colors) Regards, *j -- Jan Kaliszewski (zuo) From gagsl-py2 at yahoo.com.ar Thu Jul 30 20:04:01 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 30 Jul 2009 21:04:01 -0300 Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> <27mdnb-NxM3DJOzXnZ2dnUVZ_hxi4p2d@posted.visi> Message-ID: En Thu, 30 Jul 2009 12:16:46 -0300, Unknown escribi?: > On 2009-07-30, Barak, Ron wrote: >> On second thoughts, I may have a problem implementing the >> wrapper solution, because my actual test_pyc.pyc, needs to >> parse its command line. Namely, the actual call to >> test_pyc.pyc looks something like this: >> >> $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume >> --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t >> tvn -p pool1 -l -c >> >> And I don't know of a way to add these parameters to the >> "import test_pyc" in wrapper >> >> Is there a way to pass information to an imported module ? >> (Sorry if there's an obvious answer, I just cannot figure it >> out). > > I don't understand your problem. The module would parse > sys.argv just like always. > > If you don't like that for some reason, you could define an > entry point in the module and call it: > > #!/usr/bin/python > import sys,test_pyc > test_pyc.main(*sys.argv) And you *should* do it that way, instead of just importing the other module and executing ALL the program as a side-effect of the import statement. In the first case, the import lock will prevent other threads to run. -- Gabriel Genellina From digitig at gmail.com Thu Jul 30 20:05:29 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 31 Jul 2009 01:05:29 +0100 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e1a5$0$40016$4fafbaef@reader3.news.tin.it> References: <4a71e1a5$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: 2009/7/30 superpollo : > Tim Rowe wrote: >> Any language that gets any sort of real use has to have. For instance, >> I love Ada's numeric types (you can specify either the minimum number >> of significant figures or the maximum delta for a real type, and it >> will give you a type that satisfies that -- or a compilation error if >> it can't. ?That matches genuine problem domains far better > > doesn't the decimal module do some of that too? Not as far as I can tell, no. -- Tim Rowe From j.sundo at cs.ucl.ac.uk Thu Jul 30 20:17:04 2009 From: j.sundo at cs.ucl.ac.uk (=?ISO-8859-1?Q?Jannik_Sund=F8?=) Date: Fri, 31 Jul 2009 01:17:04 +0100 Subject: Confused About Python Loggin Message-ID: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> Dear all, I am quite confused about the Python logging. I have read and re-read the Python documentation for the Python logging module and googled, but to no avail. I simply want one logger to log to a file and another logger to log to the console. Neither should log the other's messages. The code below causes fileLogger to log to BOTH a file and the console, how come? It seems to me that a call such as "consoleHandler = logging.StreamHandler()" affects other loggers, which I do not want. Also, if I do not do logging.basicConfig(level=logging.INFO) in one of the classes of my application, the logging does not work. Any ideas how come? Thanks a lot for any help! :) fileHandler = logging.FileHandler('../../logs/log.txt') fileLogger = logging.getLogger('TESTHARNESSFILE') fileLogger.addHandler(fileHandler) fileLogger.setLevel(logging.INFO) consoleHandler = logging.StreamHandler() logger = logging.getLogger('TESTHARNESS') logger.addHandler(consoleHandler) logger.setLevel(logging.INFO) From dhaneshpai at gmail.com Thu Jul 30 20:24:10 2009 From: dhaneshpai at gmail.com (Dhanesh) Date: Thu, 30 Jul 2009 17:24:10 -0700 (PDT) Subject: Non-blocking read with popen subprocess Message-ID: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> Hi , I am trying to use subprocess popen on a windows command line executable with spits messages on STDOUT as well as STDIN. Code snippet is as below :- ########################################################################## sOut="" sErr="" javaLoaderPath = os.path.join("c:\\","Program Files","Research In Motion","BlackBerry JDE 4.7.0","bin","Javaloader.exe") cmd = [javaLoaderPath,'-u','load','helloworld.jad'] popen = subprocess.Popen (cmd,bufsize=256,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: sErr = sErr + popen.stderr.read(64) sOut = sOut + popen.stdout.read(64)------------------> Blocks here if None != popen.poll(): break; ########################################################################## I observed that python scripts blocks at stdout read on the other hand when I do not create a child stdout PIPE say " stdout=None" , things seems to be working fine. how can I we have a non blocking read ? And why does stdout block even where data is available to be read ( which seem to be apparent when stdout=None, and logs appear on parents STDOUT) ? From tjreedy at udel.edu Thu Jul 30 20:38:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 20:38:15 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <39013d06-e281-4459-bdb2-19831c9526ea@k1g2000yqf.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> <39013d06-e281-4459-bdb2-19831c9526ea@k1g2000yqf.googlegroups.com> Message-ID: r wrote: > On Jul 30, 3:55 pm, Terry Reedy wrote: >> superpollo wrote: >>> r wrote: >>> how to reverse a string in python? must i usa a temp? like: > [snip] >> Terry Jan Reedy > > No "r" never wrote anything like that. reversing a string is RTFM > material, this is basic stuff here! Stop quoting me as saying things i > did not say! I never asked how to reverse a string, "superpollo" > did. Nor did what I posted say that you did. The above says that superpollo wrote "how to reverse..., as he did. Quotes are indented once more than the author. The above says 'r wrote' with no indent and then "No...", which you did write, indented once. I agree that since I did not quote anything that you wrote, I should also have snipped your name. But I least I do attempt to snip material irrelavant to my comments. Terry Jan Reedy From brochu121 at gmail.com Thu Jul 30 20:57:56 2009 From: brochu121 at gmail.com (David Brochu) Date: Thu, 30 Jul 2009 20:57:56 -0400 Subject: httplib headers Message-ID: <9583ed900907301757q50e85b42o830820980386ff57@mail.gmail.com> Using the following code I am trying to pass a header to my POST request. (Note: urls masked) file = sys.argv[0] url = sys.argv[1] conn = httplib.HTTPConnection("XX.XXX.XX.XXX",XXXX) headers = {'content-type':'application/xml'} conn.request("POST",url,file,headers) response = conn.getresponse() head = response.getheaders() status = response.status response = response.read() print response, status, head When I run this script and view the headers that get printed the following displays: [('content-length', '691'), ('proxy-connection', 'Keep-Alive'), ('connection', 'Keep-Alive'), ('pragma', 'no-cache'), ('cache-control', 'no-cache'), ('content-type', 'text/html; charset=utf-8')] Why isn't my content-type header using the value I pass in from headers? -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Thu Jul 30 21:13:53 2009 From: aahz at pythoncraft.com (Aahz) Date: 30 Jul 2009 18:13:53 -0700 Subject: Run pyc file without specifying python path ? References: <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> Message-ID: In article , Chris Rebert wrote: > >No, with the shebang line (and assuming execute permissions on the >file), it would look like: > >./wrapper.py There's no reason to name it ".py"; you can have a perfectly useful "shell script" that just happens to list python on the shebang line instead of the usual /bin/sh. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From python at mrabarnett.plus.com Thu Jul 30 21:20:40 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 02:20:40 +0100 Subject: httplib headers In-Reply-To: <9583ed900907301757q50e85b42o830820980386ff57@mail.gmail.com> References: <9583ed900907301757q50e85b42o830820980386ff57@mail.gmail.com> Message-ID: <4A7246E8.3010400@mrabarnett.plus.com> David Brochu wrote: > Using the following code I am trying to pass a header to my POST > request. (Note: urls masked) > > file = sys.argv[0] > url = sys.argv[1] > conn = httplib.HTTPConnection("XX.XXX.XX.XXX",XXXX) > headers = {'content-type':'application/xml'} > conn.request("POST",url,file,headers) > response = conn.getresponse() > head = response.getheaders() > status = response.status > response = response.read() > print response, status, head > > When I run this script and view the headers that get printed the > following displays: > > [('content-length', '691'), ('proxy-connection', 'Keep-Alive'), > ('connection', 'Keep-Alive'), ('pragma', 'no-cache'), ('cache-control', > 'no-cache'), ('content-type', 'text/html; charset=utf-8')] > Why isn't my content-type header using the value I pass in from headers? > Aren't you just setting the headers for the "POST" you're sending (you're posting a file whose content type is 'application/xml'), but printing the headers of the response you receive (the content type of the response is 'text/html; charset=utf-8')? From jgardner at jonathangardner.net Thu Jul 30 22:30:46 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Thu, 30 Jul 2009 19:30:46 -0700 (PDT) Subject: Non-blocking read with popen subprocess References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> Message-ID: <824f8092-55b1-4df2-a6b5-74a4b1b1d9ea@i18g2000pro.googlegroups.com> On Jul 30, 5:24?pm, Dhanesh wrote: > > how can I we have a non blocking read ? See http://docs.python.org/library/popen2.html#flow-control-issues Note well: In the non-blocking world, you have to use select() or poll () to get your job done. You may want to look at "communicate" (http://docs.python.org/library/ subprocess.html#popen-objects) which may be what you need. From lukepadawan at gmail.com Thu Jul 30 22:55:40 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Thu, 30 Jul 2009 23:55:40 -0300 Subject: Generators through the C API Message-ID: <4A725D2C.4030705@gmail.com> Hello, I'm a total noob about the C API. Is there any way to create a generator function using the C API? I couldn't find anything like the 'yield' keyword in it. Thanks in advance. From grante at visi.com Thu Jul 30 23:02:17 2009 From: grante at visi.com (Grant Edwards) Date: Thu, 30 Jul 2009 22:02:17 -0500 Subject: Printing with colors in a portable way References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: On 2009-07-30, Robert Dailey wrote: > Anyone know of a way to print text in Python 3.1 with colors in a > portable way? In other words, I should be able to do something like > this: > > print_color( "This is my text", COLOR_BLUE ) > > And this should be portable (i.e. it should work on Linux, Mac, > Windows). Generating postscript or PDF is the only remotely portable way to print anything. -- Grant From cocobear.cn at gmail.com Fri Jul 31 00:18:25 2009 From: cocobear.cn at gmail.com (cocobear) Date: Thu, 30 Jul 2009 21:18:25 -0700 (PDT) Subject: merge two png pic References: Message-ID: On Jul 29, 9:20?am, cocobear wrote: > Thistwopngfile has their own palette > > >>> im1.mode > 'P' > >>> im.mode > 'P' > >>> im.getpalette == im1.getpalette > > False > > I can use this code tomergetwopngpictogether: > > Map = Image.new("RGB", (x,y)) > Map.paste(im, box) > Map.paste(im1,box) > > Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) > > But if thetwopngpicis too big , or if I have tomergemorepic > together, I will get MemoryError: > > >>> Image.new("RGB",(44544,38656)) > > Traceback (most recent call last): > ? File "", line 1, in > ? File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in > new > ? ? return Image()._new(core.fill(mode, size, color)) > MemoryError > > How can I directlymergetwopicto a ?P' modepngwith palette. Nobody replied. From ivlenin at gmail.com Fri Jul 31 01:27:22 2009 From: ivlenin at gmail.com (I V) Date: 31 Jul 2009 07:27:22 +0200 Subject: Confessions of a Python fanboy References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <4a7280ba$1@news.x-privat.org> On Thu, 30 Jul 2009 17:57:48 -0400, Luis Zarrabeitia wrote: > As I understood the question, it was "was wrong in 'for var in > container' in comparison with ruby's container.each?" > > What's the (semantic) difference between > > for localVar in container: > block > > and > > container.each{|localVar| block} I don't think "each" is a particularly compelling example of Ruby's blocks - as you say, it's easily replaceable with a Python for-loop. The advantage of Ruby's syntax is that it allows you to define further functions that are sort-of like new control structures. So you have things like: File.open('myfile', 'r') do |file| while line = file.gets puts line end end Which is like a python's: with open('myfile', 'r) as f: for line in f: print f However, with Ruby, this kind of scoping construct can be added without adding new syntax. Another interesting example is the Sinatra web framework, which allows you to write a web app with something like: get '/' do "Index page" end post '/:name' do "Posted to " + params[:name] end Which looks something like a DSL for web apps, but is accomplished solely with normal ruby functions. More: http://www.sinatrarb.com/intro.html Ruby blocks are, fundamentally, syntactic sugar, but they are kind of neat. From emmanuel.surleau at gmail.com Fri Jul 31 01:46:49 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 07:46:49 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> Message-ID: <200907310746.49392.emmanuel.surleau@gmail.com> On Friday 31 July 2009 01:06:31 Robert Kern wrote: > On 2009-07-30 16:44, r wrote: > > On Jul 30, 4:29 pm, Emmanuel Surleau > > > > wrote: > >>> 1.) No need to use "()" to call a function with no arguments. > >>> Python --> "obj.m2().m3()" --ugly > >>> Ruby --> "obj.m1.m2.m3" -- sweeet! > >>> Man, i must admit i really like this, and your code will look so much > >>> cleaner. > >> > >> It has benefits - code does look better. It has also significant cons - > >> it is ambiguous. > >> For instance: > >> > >> a = b > >> > >> Is b a variable or a method called without parameter? > > > > Hello Emanuel, > > Again, who so ever names a method with such a non-descriptive name > > will get whats coming to him. And if you did for some reason use such > > a cryptic name as "b", do yourself (and everyone else) a favor and > > follow it with "()" to denote the method call. Remember when something > > is optional that means you have an option to use it OR not use it. > > I believe his point is that it is ambiguous to the compiler, not humans > reading the code. Actually, both. Use a variable you didn't initialize? This is what you get: NameError: undefined local variable or method `b' for main:Object from (irb):1 The compiler has no idea if b is a variable or a method. It also makes it very easy to shadow an existing method by declaring a variable of the same name, which is problematic. I suppose it is a side-effect of Ruby's Perl philosophical inheritance - except that Perl uses sigils to prefix its variables, negating this issue. Cheers, Emm From steve at nospam.au Fri Jul 31 02:09:48 2009 From: steve at nospam.au (steve) Date: Fri, 31 Jul 2009 16:09:48 +1000 Subject: Test for Pythonwin? Message-ID: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> Is there a good way to check if a script is running inside Pythonwin? Perhaps a property or method that is exposed by that environment? or, alternatively, is there a better place to ask :-) Steven From __peter__ at web.de Fri Jul 31 02:52:01 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 08:52:01 +0200 Subject: merge two png pic References: Message-ID: cocobear wrote: > On Jul 29, 9:20 am, cocobear wrote: >> Thistwopngfile has their own palette >> >> >>> im1.mode >> 'P' >> >>> im.mode >> 'P' >> >>> im.getpalette == im1.getpalette >> >> False >> >> I can use this code tomergetwopngpictogether: >> >> Map = Image.new("RGB", (x,y)) >> Map.paste(im, box) >> Map.paste(im1,box) >> >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) >> >> But if thetwopngpicis too big , or if I have tomergemorepic >> together, I will get MemoryError: >> >> >>> Image.new("RGB",(44544,38656)) As a workaround you could split the image into tiles that fit into your machine's RAM. Or you could try to do the rendering on a 64-bit system. You'll need at least >>> 3*44544*38656./(2**30) 4.8109130859375 5 gigabytes for the target image plus memory for the biggest source image. Alternatively there are probably tools that can keep parts of the image on disk (imagemagick, maybe? you'll have to check yourself or ask in a specialized forum). As a last resort you should be able to write such a tool yourself. I don't know how hard it would be to generate the PNG, but the RGB pasting should be trivial. >> Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in >> new >> return Image()._new(core.fill(mode, size, color)) >> MemoryError >> >> How can I directlymergetwopicto a ?P' modepngwith palette. > > > Nobody replied. What do you want to do with such a big image? You will run into the same limitation when you are trying to display it. Peter From tjreedy at udel.edu Fri Jul 31 02:52:56 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 02:52:56 -0400 Subject: merge two png pic In-Reply-To: References: Message-ID: cocobear wrote: > On Jul 29, 9:20 am, cocobear wrote: >> Thistwopngfile has their own palette >> >>>>> im1.mode >> 'P' >>>>> im.mode >> 'P' >>>>> im.getpalette == im1.getpalette >> False >> >> I can use this code tomergetwopngpictogether: >> >> Map = Image.new("RGB", (x,y)) >> Map.paste(im, box) >> Map.paste(im1,box) >> >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) >> >> But if thetwopngpicis too big , or if I have tomergemorepic >> together, I will get MemoryError: >> >>>>> Image.new("RGB",(44544,38656)) >> Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in >> new >> return Image()._new(core.fill(mode, size, color)) >> MemoryError >> >> How can I directlymergetwopicto a ?P' modepngwith palette. > > > Nobody replied. A. Leavingoutspacesmakesyourposthardtoread. B. Nobody knows how to avoid mem error. tjr From shearichard at gmail.com Fri Jul 31 03:09:48 2009 From: shearichard at gmail.com (northof40) Date: Fri, 31 Jul 2009 00:09:48 -0700 (PDT) Subject: Python 2.6 in shared/VPS environment Message-ID: <6babf860-0161-4c1a-82f8-69e03dc53da0@p36g2000prn.googlegroups.com> Hi - I'd really like to have access to Python 2.6 to try something out. It needs to be on Linux/Unix machine. I don't mind paying but whichever way I turn I find Python 2.4 is the standard installation. Anyone know of anyone who offers this out of the box ? Or got any smart way of achieving it ? thanks R. From __peter__ at web.de Fri Jul 31 03:14:59 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 09:14:59 +0200 Subject: Confused About Python Loggin References: Message-ID: Jannik Sund? wrote: > Dear all, I am quite confused about the Python logging. I have read > and re-read the Python documentation for the Python logging module and > googled, but to no avail. I simply want one logger to log to a file > and another logger to log to the console. Neither should log the > other's messages. The code below causes fileLogger to log to BOTH a > file and the console, how come? It seems to me that a call such as > "consoleHandler = logging.StreamHandler()" affects other loggers, > which I do not want. Also, if I do not do > logging.basicConfig(level=logging.INFO) in one of the classes of my > application, the logging does not work. Any ideas how come? > > Thanks a lot for any help! :) > > fileHandler = logging.FileHandler('../../logs/log.txt') > fileLogger = logging.getLogger('TESTHARNESSFILE') > fileLogger.addHandler(fileHandler) > fileLogger.setLevel(logging.INFO) > consoleHandler = logging.StreamHandler() > logger = logging.getLogger('TESTHARNESS') > logger.addHandler(consoleHandler) > logger.setLevel(logging.INFO) The code you present should work as you expect: $ cat tmp.py import logging fileHandler = logging.FileHandler('tmp.txt') fileLogger = logging.getLogger('TESTHARNESSFILE') fileLogger.addHandler(fileHandler) fileLogger.setLevel(logging.INFO) consoleHandler = logging.StreamHandler() logger = logging.getLogger('TESTHARNESS') logger.addHandler(consoleHandler) logger.setLevel(logging.INFO) logger.info("to console") fileLogger.info("to file") $ python tmp.py to console $ cat tmp.txt to file What problems exactly are you running into if you omit the basicConfig() call? Peter From malc0de.encrypt at gmail.com Fri Jul 31 03:25:09 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 00:25:09 -0700 (PDT) Subject: problem on socket programming Message-ID: hello there, I'm using Learning Python 3rd edition as my core reference, the book is great. here's some snippet for demonstrating echo-server program, but I can't interpret it in both windows / Gnu linux ... #!/usr/bin/env python from socket import * myHost = '' myPort = 50007 sockobj = socket(AF_INET,SOCK_STREAM) sockobj.bind((myHost,myPort)) sockobj.listen(5) while true : connection,address = sockobj.accept() print 'server connected by :',address while True: data = connection.recv(1024) if not data: break connection.send('echo : '+ data) connection.close() where's the problem, beforehand appreciate ... From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 03:28:29 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 07:28:29 GMT Subject: Confessions of a Python fanboy References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> Message-ID: <008e7846$0$9756$c3e8da3@news.astraweb.com> On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote: > On 2009-07-30 16:44, r wrote: >> On Jul 30, 4:29 pm, Emmanuel Surleau wrote: >>>> 1.) No need to use "()" to call a function with no arguments. Python >>>> --> "obj.m2().m3()" --ugly >>>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>>> Man, i must admit i really like this, and your code will look so much >>>> cleaner. >>> It has benefits - code does look better. It has also significant cons >>> - it is ambiguous. >>> For instance: >>> >>> a = b >>> >>> Is b a variable or a method called without parameter? >> >> Hello Emanuel, >> Again, who so ever names a method with such a non-descriptive name will >> get whats coming to him. And if you did for some reason use such a >> cryptic name as "b", do yourself (and everyone else) a favor and follow >> it with "()" to denote the method call. Remember when something is >> optional that means you have an option to use it OR not use it. > > I believe his point is that it is ambiguous to the compiler, not humans > reading the code. Python functions and methods are first class objects. > They can be passed around. If they were auto-called, then you could not > do this. Oh my, "r" is still around is he??? And now he's singing the praises of Ruby, the language which he treated as the Devil's Spawn when he first arrived. That's hilarious. But back on topic... "r" has missed the point. It's not that a=b is hard to understand because b is a poor name. The example could have been: def factory_function(): magic = time.time() # or whatever def inner(): return magic return inner my_function = factory_function It's still ambiguous. Does the programmer intend my_function to become factory_function itself, or the output of factory_function? In Python, it's not ambiguous at all -- my_function is set to factory_function, and *not* the output of factory_function, because you haven't called the function. Python's model is consistent and simple: given a function "func", you *always* refer to the function object itself as func and you *always* call it with func(). This applies no matter how many arguments the function takes, or what it returns, or where it is defined. I think it's telling that "r" the fanboy has rejected Python's advantages (simplicity, consistency, unambiguity) in favour of lazily saving two keystrokes. -- Steven From lkrzysiak at gmail.com Fri Jul 31 03:28:54 2009 From: lkrzysiak at gmail.com (=?UTF-8?Q?=C5=81ukasz?=) Date: Fri, 31 Jul 2009 00:28:54 -0700 (PDT) Subject: How to force SAX parser to ignore encoding problems Message-ID: <89c214a4-e846-4101-b0b3-8078eec15114@w41g2000yqb.googlegroups.com> Hi, I have a problem with my XML parser (created with libraries from xml.sax package). When parser finds a invalid character (in CDATA section) for example ?, throws an exception SAXParseException. Is there any way to just ignore this kind of problem. Maybe there is a way to set up parser in less strict mode? I know that I can catch this exception and determine if this is this kind of problem and then ignore this, but I am asking about any global setting. From contact at xavierho.com Fri Jul 31 03:34:32 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 17:34:32 +1000 Subject: problem on socket programming In-Reply-To: <2d56febf0907310033n66afa69ch3a43bcbe078396c5@mail.gmail.com> References: <2d56febf0907310033n66afa69ch3a43bcbe078396c5@mail.gmail.com> Message-ID: <2d56febf0907310034kf012f4br2b8ef56688e2883c@mail.gmail.com> Whoops, posted to the wrong address.... yet again. I deserve some punishment. On Fri, Jul 31, 2009 at 5:33 PM, Xavier Ho wrote: > On Fri, Jul 31, 2009 at 5:25 PM, MalC0de wrote: > >> >> while true : >> >> where's the problem, > > > There. =] > (Btw, it's True, not true.) -------------- next part -------------- An HTML attachment was scrubbed... URL: From efotinis at oohay.com Fri Jul 31 03:38:41 2009 From: efotinis at oohay.com (eliasf) Date: Fri, 31 Jul 2009 10:38:41 +0300 Subject: Test for Pythonwin? In-Reply-To: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> References: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> Message-ID: <1249025993.67838@athprx04> "steve" wrote: > Is there a good way to check if a script is running inside Pythonwin? > Perhaps a property or method that is exposed by that environment? I don't know how foolproof it is, but this works: 'pywin' in sys.modules From piet at cs.uu.nl Fri Jul 31 03:47:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 31 Jul 2009 09:47:34 +0200 Subject: Non-blocking read with popen subprocess References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> <824f8092-55b1-4df2-a6b5-74a4b1b1d9ea@i18g2000pro.googlegroups.com> Message-ID: >>>>> Jonathan Gardner (JG) wrote: >JG> On Jul 30, 5:24?pm, Dhanesh wrote: >>> >>> how can I we have a non blocking read ? >JG> See http://docs.python.org/library/popen2.html#flow-control-issues >JG> Note well: In the non-blocking world, you have to use select() or poll >JG> () to get your job done. AFAIK, on Windows these work only on sockets, not on files. I think pipes are in the files camp. Alternatively you can read one of the two in a different thread. >JG> You may want to look at "communicate" (http://docs.python.org/library/ >JG> subprocess.html#popen-objects) which may be what you need. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From masklinn at masklinn.net Fri Jul 31 03:48:24 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 09:48:24 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <27BA9FDF-8295-41F5-A4E7-170B0D755159@masklinn.net> On 30 Jul 2009, at 23:52 , Jan Kaliszewski wrote: > Dnia 30-07-2009 o 22:41:57 Masklinn > napisa?(a): > On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: >>> 30-07-2009 o 13:36:49 Masklinn wrote: >>>> On 30 Jul 2009, at 06:04 , alex23 wrote: >>>>> On Jul 30, 1:06 pm, r wrote: >>>>>> 2.) the .each method >>>>>> container.each{|localVar| block} >>>>>> This method can really cleanup some ugly for loops, although i >>>>>> really >>>>>> like the readability of for loops. >>>>> >>>>> map(lambda localVar: , sequence) >>>>> >>>>> or: >>>>> >>>>> def usefully_named_func(var): >>>>> >>>>> return var >>>>> >>>>> transformed = [usefully_named_func(v) for v in sequence] >>>>> >>>> The issue here is of course that `map` and comprehensions are >>>> transformations. `#each` exists for effectful iterations (Ruby >>>> has `#map` for the map operation). So the intent expressed by >>>> `#each` and `map` isn't the same. Furthermore and this is the >>>> most problematic limitation of Python here, `lambda` doesn't >>>> allow complex transformations due to its restrictions, so one has >>>> to switch to named functions which works but isn't sexy (and >>>> tends to lower readability imo). >>> >>> I don't see any real limitation. What's wrong in: >>> >>> for localVar in container: >>> block >>> >> Well what's wrong with using that rather than `map`, `filter` or a >> list comprehension? (and if you don't see what the limitations of >> `lambda` are, you probably very rarely use it) > > I know well about the expression-only-limitation of lambda, fortnately > there is the 'for' loop construct (or, alternatively, you can define > a named function). > > And then, what's wrong with using 'for' rather than 'map', 'filter' > etc.? Nothing, but then again nothing's wrong using C's for either. But I do think that using higher-order functions: * tends to be terser while not going overly terse, it only removes boilerplate * is more composable (it's pretty easy to tack another transformer in your chain, the same way defining iterator/generator transformers and chaining them is simpler than doing the same thing using explicit `for?in`) * it clearly spells the intent of the programmer, while `for` can be anything and everything, and one has to dive into the code to know even the high-level operations performed (is it a basic transformation? A filtering or partitioning? A reduction? An application of side effects?) > Agree, that 'anonymous block syntax' would be nice in some cases, but > I don't see any real limitation caused by lack of it i.e. something > you > can't (all you can only with unreasonable effort). > There are no limitations, but once again there never are. There are no actual limitations to using conditional jumps over iterators either. >>> And ruby's container.each is very similar to Python's iter() > >> Uh? not at all? > > OK, .each is like Python's iter() + some form of iterating over it > ('for' loop or '[i]map'...). Well of course Enumerable#each is similar to map/imap, it's the same core principle. From malc0de.encrypt at gmail.com Fri Jul 31 03:50:19 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 00:50:19 -0700 (PDT) Subject: problem on socket programming References: Message-ID: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> problem with this code solved in win32, but I didn't test it after solved problem on gnu/linux . From bieffe62 at gmail.com Fri Jul 31 03:51:45 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 31 Jul 2009 00:51:45 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> Message-ID: <45056c13-9ff6-4a0f-b357-1893d7d03144@s15g2000yqs.googlegroups.com> On Jul 30, 10:16?pm, "Jan Kaliszewski" wrote: > 30-07-2009 o 12:29:24 Francesco Bochicchio wrote: > > > On Jul 30, 5:52?am, NighterNet wrote: > >> I am trying to figure out how to send text or byte in python 3.1. I am > >> trying to send data to flash socket to get there. I am not sure how to > >> work it. > > >> buff= 'id=' , self.id , ':balive=False\n' > >> clientSock.send(buff); > > > Try putting a 'b' before the constant string that you want to send: > > > >>> type(b'123') > > > > It's true. In python '...' literals are for strings ('str' type) and > b'...' literals are for binary data (bytes type). > > Sockets' send() and recv() need binary data ('bytes' objects). > > > or use something like this to convert non constant strings (with only > > ASCII characters) into bytes: > > >>> s = "A non-constant string : %d " % n > > >>> s > > 'A non-constant string : 12 ' > > >>> type(s) > > > > What??? > > 'str' type in Python 3.x IS NOT a type of "non-constant" strings and > IS NOT a type of strings "with only ASCII characters"! > > 'str' type in Python 3.x *is* the type of immutable ('constant') and > Unicode character (Unicode, not only ASCII) strings. It's the same what > 'unicode' type in Python 2.x. > ... unfortunate choice of words and not enough research on my part here. WHat I meant was: if you want to send via socket a constant string, use b"..."; if you want to send via socket a string that you made out of variables (the "non-constant string" ) then you have to convert it in bytes. Since I did not now of the encode method, I tried other solutions, like the one-liner using ord or using the struct module. Obviously, encode is better. My bad :-) Ciao ------- FB From malc0de.encrypt at gmail.com Fri Jul 31 04:02:33 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 01:02:33 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> Message-ID: now I've been encountered to some errors with this snippet : import sys from socket import * serverHost = 'localhost' serverPort = 50007 message = ['Hello network world'] if len(sys.argv) > 1: serverHost = sys.argv[1] if len(sys.argv) > 2: message = sys.argv[2:] sockobj = socket(AF_INET,SOCK_STREAM) sockobj.connect((serverHost,serverPort)) for line in message: sockobj.send(line) data = sockobj.recv(1024) print 'Client recieved :',repr(data) sockobj.close() thanks if someone can solve my problem . - malc0de. From matt.urry at googlemail.com Fri Jul 31 04:04:08 2009 From: matt.urry at googlemail.com (ma3mju) Date: Fri, 31 Jul 2009 01:04:08 -0700 (PDT) Subject: Processes not exiting Message-ID: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Hi all, I'm having trouble with multiprocessing I'm using it to speed up some simulations, I find for large queues when the process reaches the poison pill it does not exit whereas for smaller queues it works without any problems. Has anyone else had this trouble? Can anyone tell me a way around it? The code is in two files below. Thanks Matt parallel.py =================================================== import GaussianProcessRegression as GP import numpy as np import networkx as nx import pickle import multiprocessing ############################################################################################ # Things You Can Change ############################################################################################ #savefiles savefile = "wattsdata2" graphfile = "wattsgraphs2" #sample sizes num_graphs = 5 num_sets_of_data = 10 #other things... intervals = np.ceil(np.logspace(-2,1,50)*500) noise = [np.sqrt(0.1),np.sqrt(0.01),np.sqrt(0.001),np.sqrt(0.0001)] ############################################################################################ #generate graphs graphs = [] for i in range(0,num_graphs): graphs.append(nx.watts_strogatz_graph(500,5,0.01)) #save them for later reference filehandler = open(graphfile,'w') pickle.dump(graphs,filehandler,-1) filehandler.close() #queues easy_work_queue = multiprocessing.Queue() hard_work_queue = multiprocessing.Queue() result_queue = multiprocessing.Queue() #construct the items in the hard queue l=0 for j in range(0,len(intervals)): for i in range(0,len(noise)): for k in range(0,num_graphs): if int(intervals[j]) <=4000: easy_work_queue.put({'datapt': l,'graph': graphs [k],'noise': noise[i],'number_of_sets_of_data': num_sets_of_data,'number_of_data_points':int(intervals[j])}) else: hard_work_queue.put({'datapt': l,'graph': graphs [k],'noise': noise[i],'number_of_sets_of_data': num_sets_of_data,'number_of_data_points':int(intervals[j])}) l+=1 #get number of cores and set the number on concurrent processes num_hard_workers = 2 num_workers = multiprocessing.cpu_count()*1.5 easy_workers = [] hard_workers = [] #add poison pill for each worker and create the worker for i in range(0,num_workers-num_hard_workers): easy_work_queue.put(None) easy_workers.append(multiprocessing.Process (target=GP.RandomWalkGeneralizationErrorParallel,args= (easy_work_queue,result_queue,))) for i in range(0,num_hard_workers): hard_work_queue.put(None) hard_workers.append(multiprocessing.Process (target=GP.RandomWalkGeneralizationErrorParallel,args= (hard_work_queue,result_queue,))) #run all workers for worker in hard_workers: worker.start() for worker in easy_workers: worker.start() #wait for easy workers to finish for worker in easy_workers: worker.join() print('worker joined') #set off some of the easy workers on the hard work (maybe double number of hard) for i in range(0,num_hard_workers): hard_work_queue.put(None) hard_workers.append(multiprocessing.Process (target=GP.RandomWalkGeneralizationErrorParallel,args= (hard_work_queue,result_queue,))) #wait for all hard workers to finish for worker in hard_workers: worker.join() #construct data from the mess in the result queue tempdata = np.zeros(l) while not result_queue.empty(): data = result_queue.get() tempdata[data[0]] = data[1] finaldata = tempdata.reshape((len(intervals),len(noise),num_graphs)) np.save(savefile,finaldata) ======================================================= GaussianProcessRegression.py ======================================================= import CovarianceFunction as CF import networkx as nx import numpy as np import scipy.linalg as sp #fortran code from lapack-blas (hopefully when scipy updated this wont be needed) import dtrsv #to use more than one core import multiprocessing #Currently we assume Gaussian noise TODO change to general noise #Assume 0 mean TODO change to general mean Gaussian Process class GaussianProcessRegression: def __init__(self,covariance_function,sigma): #a covariance function object defined in CovarianceFunction class #note this uses the parent class but any children can be used self.C = covariance_function #a list of pts that are known and their values self.pts = [] self.vals = [] #the inverse of K as defined in #@book{coolen05:theoryofneural, #ISBN = {0-19-853024-2}, #publisher = {Oxford University Press, USA}, #author = {Coolen, A. C. C. and K{\"{u}}hn, R. and Sollich, P.}, #title = {Theory of neural information processing systems}, #year = {2005}, #} self.K = np.array([]) #gaussian noise variable self.sigma = float(sigma) self.cholL = np.array([]) def add_data_points(self,points,vals): #add all points to list self.pts += points self.vals += vals arraysize = len(self.pts) #construct K K = np.zeros((arraysize,arraysize)) #for speed pts = self.pts between_points = self.C.between_points if len(self.K): K[:-1,:-1] = self.K for i in xrange(0,arraysize): for j in xrange(arraysize-len(points),arraysize): K[i,j] = between_points(pts[i],pts[j]) K[j,i] = K[i,j] K[arraysize-len(points):arraysize,arraysize-len (points):arraysize] = K[arraysize-len(points):arraysize,arraysize-len (points):arraysize] + self.sigma**2 * np.eye(len(points)) self.K = K #calculate the prediction of a point based on data previously given def point_prediction(self,points): mean = [] variance =[] arraysize = len(self.pts) #cholesky #if self.cholL.shape[0] < arraysize: L=np.linalg.cholesky(self.K) # self.cholL = L #else: # L = self.cholL alpha = sp.cho_solve((L,1),self.vals) #create L in banded form k=np.zeros((arraysize,len(points))) ################################################################## #for speed get ref to functions im going to use and save them between_points = self.C.between_points pts = self.pts dot = np.dot ################################################################## for j in xrange(0,len(points)): #create k for i in xrange(0,arraysize): k[i,j] = between_points(pts[i],points[j]) #calculate mean and variance #call the command for forward substitution ###############fortran call####################################### v = dtrsv.dtrsv('L','N',arraysize,L,k) ################################################################## #result mean=dot(alpha,k) for i in xrange(0,len(points)): variance.append(between_points(points[i],points[i]) - dot(v [:,i],v[:,i])) #return it in dictionary form return {'mean':mean,'variance':variance} # calculate the error for data given, where function is a vector # of the function evaluated at a sufficiently large number of points # that the GPregression has been trying to learn def error(self,function): total = 0 #sum up variances result = self.point_prediction(function[::2]) total = np.sum(result['variance']) total = (1/float(len(function)/2))*total return total #clear what has been learnt so far def clear(self): self.pts = [] self.vals = [] self.K = np.array([]) #calculate the average error for a function defined in function when give #number_of_examples examples def average_error_over_samples(self,function, sample_size, number_of_examples): avg = 0 numberofpoints = len(function)/2 for i in range(0,sample_size): self.clear() #generate points of the function permpts = np.random.randint (0,numberofpoints,number_of_examples) #create the vectors pts = [] vals = [] for j in range(0,number_of_examples): pts.append(function[permpts[j]*2]) vals.append(function[permpts[j]*2+1]) #learn these points self.add_data_points(pts,vals) #print("points added") avg = avg + self.error(function) avg = avg/sample_size return avg #calculate the average error over functions over data of size number_of_data_points for MOST cases this is #also the generalization error a summary of which and approximations to can be found in: #@inproceedings{Sollich99learningcurves, #booktitle = {Neural Computation}, #author = {Sollich, P.}, #title = {Learning curves for Gaussian process regression: Approximations and bounds}, #pages = {200-2}, #year = {1999}, #} def emprical_average_error_over_functions (self,number_of_functions,number_of_sets_of_data,number_of_data_points,function_detail =0,progress=0): avg = 0 step = float(100)/number_of_functions for i in range(0,number_of_functions): if progress: print step*float(i),"%" if function_detail: fx = self.C.generate_function (self.sigma,function_detail) else: fx = self.C.generate_function(self.sigma) avg = self.average_error_over_samples (fx,number_of_sets_of_data,number_of_data_points)+avg avg = avg / number_of_functions return avg def average_error_over_functions (self,number_of_sets_of_data,number_of_data_points,function_detail=0): if function_detail: fx = self.C.generate_function (self.sigma,function_detail) else: fx = self.C.generate_function(self.sigma) avg = self.average_error_over_samples (fx,number_of_sets_of_data,number_of_data_points) return(avg) def function_prediction(self,pts): temp = self.point_prediction(pts) return {'func':temp['mean'],'varpos':temp ['variance'],'varneg':-temp['variance']} ######################################################################################################################################################### #Functions not contained in a class ######################################################################################################################################################### #function to calculate the generalization error for a RandomWalk kernel averaging over graphs graphs def RandomWalkGeneralizationError (noise,graphs,number_of_sets_of_data,number_of_data_points,a=2,p=10): graph_specific = np.zeros(len(graphs)) avg = 0 for i in range(0,len(graphs)): rw = CF.RandomWalk(a,p,graphs[i]) GP = GaussianProcessRegression(rw,noise) graph_specific[i] = GP.average_error_over_functions (number_of_sets_of_data,number_of_data_points) avg = np.sum(graph_specific)/len(graphs) return avg, graph_specific #as above but using queues to create parallel architechture def RandomWalkGeneralizationErrorParallel (work_queue,result_queue,a=2,p=10): while True: input = work_queue.get() if input is None: print "poison" break print 'this should not appear' print input['datapt'], ' ', input['number_of_data_points'] rw=CF.RandomWalk(a,p,input['graph']) GP = GaussianProcessRegression(rw,input['noise']) err = GP.average_error_over_functions(input ['number_of_sets_of_data'],input['number_of_data_points']) result_queue.put([input['datapt'],err]) print 'here' return From matt.urry at googlemail.com Fri Jul 31 04:06:25 2009 From: matt.urry at googlemail.com (ma3mju) Date: Fri, 31 Jul 2009 01:06:25 -0700 (PDT) Subject: Processes not exiting References: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Message-ID: <28791ddd-5270-4a74-9c86-8fe719b34069@d4g2000yqa.googlegroups.com> Sorry ###############fortran call####################################### is meant to be ###############fortran call####################################### Matt From clp2 at rebertia.com Fri Jul 31 04:07:41 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 01:07:41 -0700 Subject: problem on socket programming In-Reply-To: References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> Message-ID: <50697b2c0907310107v40f20d51ic5852303e545cd77@mail.gmail.com> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: > now I've been encountered to some errors with this snippet : And these errors are? Provide error messages and full tracebacks. Cheers, Chris -- http://blog.rebertia.com From masklinn at masklinn.net Fri Jul 31 04:08:33 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 10:08:33 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <200907301757.48761.kyrie@uh.cu> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: On 30 Jul 2009, at 23:57 , Luis Zarrabeitia wrote: > I'd like to ask, what "container.each" is, exactly? It looks like a > function > call (as I've learned a few posts ago), but, what are its arguments? > How the > looping "works"? Does it receive a "code" object that it has to > execute? > Is .each some kind of magic keyword? (This has little to do with > python or > the current thread, so feel free to reply off-list if you want to...) #each is simply a method that takes a function (called blocks in ruby). One could call it a higher-order method I guess. It's an implementation of the concept of internal iteration: instead of collections yielding iterator objects, and programmers using those through specially-built iteration constructs (e.g. `for?in`), collections control iteration over themselves (the iteration is performed "inside" the collection, thus the "internal" part) and the programmer provides the operations to perform at each iterative step through (usually) a function. In Python (assuming we had anonymous defs and an each method on lists), the following loop: for item in some_list: do_something(item) do_something_else(item) some_list.each((def (item): do_something(item) do_something_else(item) )) There's absolutely nothing magic there, it's simply using anonymous functions and method calls (SmallTalk initiated this approach in the 70s, and actually went much, much further than Ruby as it did away not only with `for?in` but also with `if?else` and `while` and a bunch of other stuff). Now as IV pointed out, #each isn't the most interesting usage of blocks/anonymous functions (though I do like it, because it gets rid of no less than two keywords? even if Ruby reintroduced them as syntactic sugar), higher-order functions (functions which act on other functions) are (among other things) ways to create new control structures without having to extend the core language (so are lisp- style macros, by the way). Of course Python does have higher-order functions (functions are first- class objects, so it's possible and frequent to have functions act on other functions), but since Python doesn't have anonymous functions that usage tends to be a bit too verbose beyond simple cases (also, of course, this kind of usages is neither in the "genes" nor in the stdlib). In closing, IV has an example of how blocks make `with` unnecessary in Ruby (the functionality can be implemented at the library level rather than the language one). Generally, anonymous functions in OO languages are a way to inject behavior into third-party methods/objects. Kind-of a first-class Visitor support. -m PS: there's actually a bit of syntactic magic in Ruby's blocks, and it's one of the things I hate in the language, but it's not relevant to the role of blocks, and unnecessary to it: SmallTalk has no magical syntax). From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 04:13:50 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 08:13:50 GMT Subject: Confessions of a Python fanboy References: Message-ID: <008e82e8$0$9756$c3e8da3@news.astraweb.com> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > That and the fact that I couldn't stop laughing for long enough to learn > any more when I read in the Pragmatic Programmer's Guide that "Ruby, > unlike less flexible languages, lets you alter the value of a constant." > Yep, as they say "Bug" = "Undocumented feature"! That's no different from Python's "constant by convention". We don't even get a compiler warning! On the other hand, we don't have to prefix names with @ and @@, and we don't have the compiler trying to *guess* whether we're calling a function or referring to a variable. Somebody who knows more Ruby than me should try writing the Zen of Ruby. Something like: Line noise is beautiful. Simplicity is for the simple. Complicated just proves we're smart. Readability only matters to the schmuck who has to maintain our code. Special cases require breaking the rules. In the face of ambiguity, try to guess. Go on, what could go wrong? The more ways to do it, the better. Although I'm sure Ruby has its good points. I'm not convinced anonymous code blocks are one of them though. -- Steven From hendrik at microcorp.co.za Fri Jul 31 04:18:15 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 31 Jul 2009 10:18:15 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <7dca4iF2bgbn5U1@mid.individual.net> References: <7dca4iF2bgbn5U1@mid.individual.net> Message-ID: <200907311018.15784.hendrik@microcorp.co.za> On Thursday 30 July 2009 03:09:14 greg wrote: > Hendrik van Rooyen wrote: > > And if code is data, where is Pythons ALTER statement? > > class Duck: > > def quack(self): > print "Quack!" > > def moo(): > print "Moo!" > > def ALTER(obj, name, TO_PROCEED_TO): > setattr(obj, name, TO_PROCEED_TO) > > d = Duck() > ALTER(d, 'quack', TO_PROCEED_TO = moo) > d.quack() Nice, and I really appreciate the Duck, but (there is always a but): 1) That is a function, not a statement. 2) The original changed a jump target address from one address to another, and did not need the heavy machinery of object oriented code. So what you are doing is not quite the same. It is actually not really needed in python, as one can pass functions around, so that you can call different things instead of jumping to them: thing_to_call = moo thing_to_call() Does the equivalent without user level OO. The original ALTER statement found use in building fast state machines, where the next thing to do was set up by the current state. In python one simply returns the next state. The equivalent python code is easier to read as it is obvious what is happening - The COBOL source was more obscure, as any jump could have been altered, and you could not see that until you have read further on in the program, where the ALTER statement was. - Hendrik From contact at xavierho.com Fri Jul 31 04:21:45 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 18:21:45 +1000 Subject: Confessions of a Python fanboy In-Reply-To: References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn wrote: > ... but since Python doesn't have anonymous functions that usage > tends to be a bit too verbose ... > Sorry to interrupt, but wouldn't lambda in Python be considered as 'anonymous functions'? -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Fri Jul 31 04:25:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 01:25:37 -0700 Subject: Confessions of a Python fanboy In-Reply-To: <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> Message-ID: <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho wrote: > On Fri, Jul 31, 2009 at 6:08 PM, Masklinn wrote: >> >> ... but since Python doesn't have anonymous functions that usage >> tends to be a bit too verbose ... > > Sorry to interrupt, but wouldn't lambda in Python be considered as > 'anonymous functions'? I believe "full" anonymous functions was intended by the author. lambdas are limited to a single expression. "Full" anonymous functions would be allowed to contain multiple statements. Cheers, Chris -- http://blog.rebertia.com From javier.collado at gmail.com Fri Jul 31 04:27:01 2009 From: javier.collado at gmail.com (Javier Collado) Date: Fri, 31 Jul 2009 10:27:01 +0200 Subject: Non-blocking read with popen subprocess In-Reply-To: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> Message-ID: Hello, According to my experience and from what I've read in other threads, subprocess isn't easy to use for interactive tasks. I don't really know, but maybe it wasn't even designed for that at all. On the other hand, pexpect seems to work fine for interactive use and even provides a method for nonblocking reads, so I think that you should consider to take a look at it: http://pexpect.sourceforge.net/pexpect.html#spawn-read_nonblocking Best regards, Javier 2009/7/31 Dhanesh : > Hi , > > I am trying to use subprocess popen on a windows command line > executable with spits messages on STDOUT as well as STDIN. Code > snippet is as below :- > ########################################################################## > sOut="" > ? ?sErr="" > ? ?javaLoaderPath = os.path.join("c:\\","Program Files","Research In > Motion","BlackBerry JDE 4.7.0","bin","Javaloader.exe") > ? ?cmd = [javaLoaderPath,'-u','load','helloworld.jad'] > ? ?popen = subprocess.Popen > (cmd,bufsize=256,shell=False,stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > ? ?while True: > ? ? ? ?sErr = sErr + popen.stderr.read(64) > ? ? ? ?sOut = sOut + popen.stdout.read(64)------------------> Blocks > here > ? ? ? ?if None != popen.poll(): > ? ? ? ? ? ?break; > ########################################################################## > > I observed that python scripts blocks at stdout read on the other hand > when I do not create a child stdout PIPE ?say ?" stdout=None" , things > seems to be working fine. > > how can I we have a non blocking read ? > And why does stdout block even where data is available to be read > ( which seem to be apparent when stdout=None, and logs appear on > parents STDOUT) ? > > -- > http://mail.python.org/mailman/listinfo/python-list > From contact at xavierho.com Fri Jul 31 04:31:43 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 18:31:43 +1000 Subject: Confessions of a Python fanboy In-Reply-To: <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> Message-ID: <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> On Fri, Jul 31, 2009 at 6:25 PM, Chris Rebert wrote: > I believe "full" anonymous functions was intended by the author. > lambdas are limited to a single expression. "Full" anonymous functions > would be allowed to contain multiple statements. > > Cheers, but what about this: def goBig(x): while True: x = x ** 2 yield x for result in goBig(10): if result > 10 ** 100: break print result It's a silly example, but wouldn't goBig(10) in this example be a "full anonymous function"? Just being curious. -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Fri Jul 31 04:38:16 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 01:38:16 -0700 Subject: Confessions of a Python fanboy In-Reply-To: <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> Message-ID: <50697b2c0907310138u1ea485f9yf9bac0095e09927d@mail.gmail.com> On Fri, Jul 31, 2009 at 1:31 AM, Xavier Ho wrote: > On Fri, Jul 31, 2009 at 6:25 PM, Chris Rebert wrote: >> >> I believe "full" anonymous functions was intended by the author. >> lambdas are limited to a single expression. "Full" anonymous functions >> would be allowed to contain multiple statements. >> > Cheers, but what about this: > > ?def goBig(x): > ??? while True: > ??????? x = x ** 2 > ??????? yield x > > for result in goBig(10): > ??? if result > 10 ** 100: > ??????? break > ??? print result > > It's a silly example, but wouldn't goBig(10) in this example be a "full > anonymous function"? No, because it has a name, namely "goBig"; this obviously prevents it from being "anonymous". For comparison, note how the function in the following example is never given a name, and is thus anonymous: >>> (lambda x: x+5)(6) 11 Cheers, Chris -- http://blog.rebertia.com From malc0de.encrypt at gmail.com Fri Jul 31 04:38:21 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 01:38:21 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> Message-ID: <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> On Jul 31, 12:07?pm, Chris Rebert wrote: > On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: > > now I've been encountered to some errors with this snippet : > > And these errors are? Provide error messages and full tracebacks. > > Cheers, > Chris > --http://blog.rebertia.com these are errors : Traceback (most recent call last): File "echo-client.py", line 11, in ", line 1, in connect socket.error: (10061, 'Connection refused') From contact at xavierho.com Fri Jul 31 04:41:17 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 18:41:17 +1000 Subject: Confessions of a Python fanboy In-Reply-To: <50697b2c0907310138u1ea485f9yf9bac0095e09927d@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> <50697b2c0907310138u1ea485f9yf9bac0095e09927d@mail.gmail.com> Message-ID: <2d56febf0907310141m15bfc526i3c916f5a2b48f544@mail.gmail.com> On Fri, Jul 31, 2009 at 6:38 PM, Chris Rebert wrote: > No, because it has a name, namely "goBig"; this obviously prevents it > from being "anonymous". > > For comparison, note how the function in the following example is > never given a name, and is thus anonymous: > >>> (lambda x: x+5)(6) > 11 > Oh, I see. I was thinking in terms of using a variable to "give the function a name". In that case, I understand. Thanks again for bearing me with my random thoughts. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hendrik at microcorp.co.za Fri Jul 31 04:47:24 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 31 Jul 2009 10:47:24 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: <4A719E2D.708@ieee.org> References: <4A719E2D.708@ieee.org> Message-ID: <200907311047.25029.hendrik@microcorp.co.za> On Thursday 30 July 2009 15:20:45 Dave Angel wrote: > As far as I know, nobody has yet built a microcode implementation of a > Python VM (Virtual Machine). Nor have I seen one for the Java VM. Atmel has made an ARM that has support for Java Bytecode. AT91SAM9X512 and friends (smaller versions) - Hendrik From greg at cosc.canterbury.ac.nz Fri Jul 31 05:02:08 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Fri, 31 Jul 2009 21:02:08 +1200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <7dca4iF2bgbn5U1@mid.individual.net> Message-ID: <7dfq78F2c8bolU1@mid.individual.net> Hendrik van Rooyen wrote: > The COBOL source was > more obscure, as any jump could have been altered, > and you could not see that until you have read further > on in the program, where the ALTER statement was. Well, in Python you can pretty much replace any function with any other function, so you can obfuscate things just as much if you really want! -- Greg From jeanmichel at sequans.com Fri Jul 31 05:20:47 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 31 Jul 2009 11:20:47 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <80839e72-9df4-4590-b22d-4c5c829e37b6@h11g2000yqb.googlegroups.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <80839e72-9df4-4590-b22d-4c5c829e37b6@h11g2000yqb.googlegroups.com> Message-ID: <4A72B76F.4020805@sequans.com> r wrote: > The purpose of his thread was to get feedback on how Python > and Ruby ideas could be cumulated into the best high level language. > And being that i am the BDFL of the "Confessions of a Python Fanboy" > thread, you have my personal permission to continue on with this > subject matter..., > > Challenging even the most accepted mechanisms of a language is a proof of intelligence. Descartes's doubt method : "The basic strategy of /Descartes/ 's method of doubt is to defeat skepticism on its own ground. Begin by doubting the truth of everything?not only the evidence of the senses and the more extravagant cultural presuppositions, but even the fundamental process of reasoning itself. If any particular truth about the world can survive this extreme skeptical challenge, then it must be truly indubitable and therefore a perfectly certain foundation for knowledge" So let's make the method call parenthesis a "truly indubitable and therefore a perfectly certain foundation". Those who want to remove the parenthesis on python method calls raise the hand ! JM From pyvault2.6 at gmail.com Fri Jul 31 05:25:17 2009 From: pyvault2.6 at gmail.com (learner learner) Date: Fri, 31 Jul 2009 14:55:17 +0530 Subject: file comparison Message-ID: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Hi all, I want to compare two text files line by line and eliminate the matching/repeated line and store the unmatched/leftout lines into a third file or overwrite into one of them. regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From lkrzysiak at gmail.com Fri Jul 31 05:29:57 2009 From: lkrzysiak at gmail.com (=?UTF-8?Q?=C5=81ukasz?=) Date: Fri, 31 Jul 2009 02:29:57 -0700 (PDT) Subject: How to force SAX parser to ignore encoding problems References: <89c214a4-e846-4101-b0b3-8078eec15114@w41g2000yqb.googlegroups.com> Message-ID: <5587c712-1a15-4666-a5d9-5c99ace648f6@a13g2000yqc.googlegroups.com> On 31 Lip, 09:28, ?ukasz wrote: > Hi, > I have a problem with my XML parser (created with libraries from > xml.sax package). When parser finds a invalid character (in CDATA > section) for example , After sending this message I noticed that example invalid characters are not displaying on some platforms :) From shearichard at gmail.com Fri Jul 31 05:30:36 2009 From: shearichard at gmail.com (northof40) Date: Fri, 31 Jul 2009 02:30:36 -0700 (PDT) Subject: Python 2.6 in shared/VPS environment References: <6babf860-0161-4c1a-82f8-69e03dc53da0@p36g2000prn.googlegroups.com> Message-ID: <923e59f8-638c-4c2e-8b6a-b6e13d415424@j9g2000prh.googlegroups.com> Ignore this question. Managed to get Amazon Web Services going and have installed Python 2.6 on there. Thanks for your eyeballs time. On Jul 31, 7:09?pm, northof40 wrote: > Hi - I'd really like to have access to Python 2.6 to try something > out. It needs to be on Linux/Unix machine. I don't mind paying but > whichever way I turn I find Python 2.4 is the standard installation. > > Anyone know of anyone who offers this out of the box ? Or got any > smart way of achieving it ? > > thanks > > R. From contact at xavierho.com Fri Jul 31 05:34:58 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 19:34:58 +1000 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: <2d56febf0907310234h5bda3a66m66f71201efb92fe8@mail.gmail.com> On Fri, Jul 31, 2009 at 7:25 PM, learner learner wrote: > Hi all, > > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a third > file or overwrite into one of them. > Sounds like homework to me. Why not look into: - the file() function and its methods. It's in the documentation. -------------- next part -------------- An HTML attachment was scrubbed... URL: From gh at ghaering.de Fri Jul 31 05:35:03 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Fri, 31 Jul 2009 11:35:03 +0200 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: learner learner wrote: > Hi all, > > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a > third file or overwrite into one of them. gl & hf! From clp2 at rebertia.com Fri Jul 31 05:37:25 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 02:37:25 -0700 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: <50697b2c0907310237y2c79e938g70952e966b74ce87@mail.gmail.com> On Fri, Jul 31, 2009 at 2:25 AM, learner learner wrote: > Hi all, > > I?want to compare two text files line by line and eliminate the > matching/repeated?line and store the unmatched/leftout lines into?a third > file or overwrite into one of them. See the `difflib` module: http://docs.python.org/library/difflib.html Cheers, Chris -- http://blog.rebertia.com From duncan.booth at invalid.invalid Fri Jul 31 05:41:34 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 31 Jul 2009 09:41:34 GMT Subject: Generators through the C API References: Message-ID: Lucas P Melo wrote: > Hello, I'm a total noob about the C API. Is there any way to create a > generator function using the C API? I couldn't find anything like the > 'yield' keyword in it. > > Thanks in advance. You define a new type with tp_flags including Py_TPFLAGS_HAVE_ITER. Anything that would be a local variable in your generator needs to become an attribute in the type. The tp_init initialization function should contain all the code up to the first yield, tp_iter should return self and tp_iternext should execute code up to the next yield. -- Duncan Booth http://kupuguy.blogspot.com From masklinn at masklinn.net Fri Jul 31 05:49:31 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 11:49:31 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> Message-ID: On 31 Jul 2009, at 10:25 , Chris Rebert wrote: > On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho > wrote: >> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn >> wrote: >>> >>> ... but since Python doesn't have anonymous functions that >>> usage >>> tends to be a bit too verbose ... >> >> Sorry to interrupt, but wouldn't lambda in Python be considered as >> 'anonymous functions'? > > I believe "full" anonymous functions was intended by the author. > lambdas are limited to a single expression. Yes, and they're limited to a single *expression*, so before Python 3, lambda: print "foo" is out. Likewise, it isn't possible to have an if/ else statement within a lambda (though a ternary is ok), or a with, ? since Python statements aren't expressions. From iainking at gmail.com Fri Jul 31 05:54:50 2009 From: iainking at gmail.com (Iain King) Date: Fri, 31 Jul 2009 02:54:50 -0700 (PDT) Subject: Confessions of a Python fanboy References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> <008e7846$0$9756$c3e8da3@news.astraweb.com> Message-ID: On Jul 31, 8:28?am, Steven D'Aprano wrote: > On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote: > > On 2009-07-30 16:44, r wrote: > >> On Jul 30, 4:29 pm, Emmanuel Surleau wrote: > >>>> 1.) No need to use "()" to call a function with no arguments. Python > >>>> --> ?"obj.m2().m3()" --ugly > >>>> ? ?Ruby --> ?"obj.m1.m2.m3" ?-- sweeet! > >>>> Man, i must admit i really like this, and your code will look so much > >>>> cleaner. > >>> It has benefits - code does look better. It has also significant cons > >>> - it is ambiguous. > >>> For instance: > > >>> a = b > > >>> Is b a variable or a method called without parameter? > > >> Hello Emanuel, > >> Again, who so ever names a method with such a non-descriptive name will > >> get whats coming to him. And if you did for some reason use such a > >> cryptic name as "b", do yourself (and everyone else) a favor and follow > >> it with "()" to denote the method call. Remember when something is > >> optional that means you have an option to use it OR not use it. > > > I believe his point is that it is ambiguous to the compiler, not humans > > reading the code. Python functions and methods are first class objects. > > They can be passed around. If they were auto-called, then you could not > > do this. > > Oh my, "r" is still around is he??? And now he's singing the praises of > Ruby, the language which he treated as the Devil's Spawn when he first > arrived. That's hilarious. > > But back on topic... "r" has missed the point. It's not that a=b is hard > to understand because b is a poor name. The example could have been: > > def factory_function(): > ? ? magic = time.time() ?# or whatever > ? ? def inner(): > ? ? ? ? return magic > ? ? return inner > > my_function = factory_function > > It's still ambiguous. Does the programmer intend my_function to become > factory_function itself, or the output of factory_function? Not only that - does 'return inner' return the function inner or the result of function inner? How does ruby pass a function as an object? Iain From piet at cs.uu.nl Fri Jul 31 06:01:09 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 31 Jul 2009 12:01:09 +0200 Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> Message-ID: >>>>> MalC0de (M) wrote: >M> On Jul 31, 12:07?pm, Chris Rebert wrote: >>> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: >>> > now I've been encountered to some errors with this snippet : >>> >>> And these errors are? Provide error messages and full tracebacks. >>> >>> Cheers, >>> Chris >>> --http://blog.rebertia.com >M> these are errors : >M> Traceback (most recent call last): >M> File "echo-client.py", line 11, in M> sockobj.connect((serverHost,serverPort)) >M> File "", line 1, in connect >M> socket.error: (10061, 'Connection refused') Is your server running? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Fri Jul 31 06:27:31 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 31 Jul 2009 12:27:31 +0200 Subject: Processes not exiting References: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Message-ID: >>>>> ma3mju (m) wrote: >m> Hi all, >m> I'm having trouble with multiprocessing I'm using it to speed up some >m> simulations, I find for large queues when the process reaches the >m> poison pill it does not exit whereas for smaller queues it works >m> without any problems. Has anyone else had this trouble? Can anyone >m> tell me a way around it? The code is in two files below. How do you know it doesn't exit. You haven't shown any of your output. I did discover a problem in your code, but it should cause an exception: >m> #set off some of the easy workers on the hard work (maybe double >m> number of hard) >m> for i in range(0,num_hard_workers): >m> hard_work_queue.put(None) >m> hard_workers.append(multiprocessing.Process >m> (target=GP.RandomWalkGeneralizationErrorParallel,args= >m> (hard_work_queue,result_queue,))) >m> #wait for all hard workers to finish >m> for worker in hard_workers: >m> worker.join() Here you create new hard workers, but you never start them. The join should then give an exception when it reaches these. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From masklinn at masklinn.net Fri Jul 31 06:32:56 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 12:32:56 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> <008e7846$0$9756$c3e8da3@news.astraweb.com> Message-ID: <1AD26B34-17FA-4315-8A4F-B3F3D09F7D29@masklinn.net> On 31 Jul 2009, at 11:54 , Iain King wrote: > On Jul 31, 8:28 am, Steven D'Aprano cybersource.com.au> wrote: >> On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote: >>> On 2009-07-30 16:44, r wrote: >>>> On Jul 30, 4:29 pm, Emmanuel Surleau >>>> wrote: >>>>>> 1.) No need to use "()" to call a function with no arguments. >>>>>> Python >>>>>> --> "obj.m2().m3()" --ugly >>>>>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>>>>> Man, i must admit i really like this, and your code will look >>>>>> so much >>>>>> cleaner. >>>>> It has benefits - code does look better. It has also significant >>>>> cons >>>>> - it is ambiguous. >>>>> For instance: >> >>>>> a = b >> >>>>> Is b a variable or a method called without parameter? >> >>>> Hello Emanuel, >>>> Again, who so ever names a method with such a non-descriptive >>>> name will >>>> get whats coming to him. And if you did for some reason use such a >>>> cryptic name as "b", do yourself (and everyone else) a favor and >>>> follow >>>> it with "()" to denote the method call. Remember when something is >>>> optional that means you have an option to use it OR not use it. >> >>> I believe his point is that it is ambiguous to the compiler, not >>> humans >>> reading the code. Python functions and methods are first class >>> objects. >>> They can be passed around. If they were auto-called, then you >>> could not >>> do this. >> >> Oh my, "r" is still around is he??? And now he's singing the >> praises of >> Ruby, the language which he treated as the Devil's Spawn when he >> first >> arrived. That's hilarious. >> >> But back on topic... "r" has missed the point. It's not that a=b is >> hard >> to understand because b is a poor name. The example could have been: >> >> def factory_function(): >> magic = time.time() # or whatever >> def inner(): >> return magic >> return inner >> >> my_function = factory_function >> >> It's still ambiguous. Does the programmer intend my_function to >> become >> factory_function itself, or the output of factory_function? > > Not only that - does 'return inner' return the function inner or the > result of function inner? > > How does ruby pass a function as an object? Ruby doesn't have functions as such. It has methods and blocks (which would be anonymous functions). `def` always creates methods. To get a (bound) method object, you simply call `method` on an instance e.g. `foo.method(:bar)` is equivalent to `foo.bar` in Python (it returns a bound method object without calling it). Blocks are usually created as part of the calls: `foo.bar {do_something}` the part between braces (braces included) is a block, which will be passed to the method `bar`. Sadly, Ruby's blocks are magical syntax (doesn't mean they have to be, in Smalltalk there's nothing magical about blocks for instance) so you can't just do `foo = {do_something}`, you have to turn them into `Proc` objects with the `proc` constructor (or `lambda`, it's equivalent and looks better so I'll use that): `foo = lambda {do_something}`. If you use the magical syntax previously shown, Ruby handles the turning of a block into an actual `Proc` instance. And since Ruby doesn't have a `()` operator, it uses a method instead (`#call`), so you simply do `foo.call` to execute the proc and get its value. All in all, much like Smalltalk, Ruby tends not to favor raw functions the way Python does, so a direct translation of the Python code doesn't make much sense. From python at mrabarnett.plus.com Fri Jul 31 06:34:29 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 11:34:29 +0100 Subject: Processes not exiting In-Reply-To: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> References: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Message-ID: <4A72C8B5.1030309@mrabarnett.plus.com> ma3mju wrote: > Hi all, > > I'm having trouble with multiprocessing I'm using it to speed up some > simulations, I find for large queues when the process reaches the > poison pill it does not exit whereas for smaller queues it works > without any problems. Has anyone else had this trouble? Can anyone > tell me a way around it? The code is in two files below. > [snip] > > #get number of cores and set the number on concurrent processes > num_hard_workers = 2 > num_workers = multiprocessing.cpu_count()*1.5 > easy_workers = [] > hard_workers = [] > #add poison pill for each worker and create the worker > for i in range(0,num_workers-num_hard_workers): > easy_work_queue.put(None) > easy_workers.append(multiprocessing.Process > (target=GP.RandomWalkGeneralizationErrorParallel,args= > (easy_work_queue,result_queue,))) > for i in range(0,num_hard_workers): > hard_work_queue.put(None) > hard_workers.append(multiprocessing.Process > (target=GP.RandomWalkGeneralizationErrorParallel,args= > (hard_work_queue,result_queue,))) > You have 2 hard workers and ceil(CPU_count * 1.5) - 2 easy workers. What if the number of CPUs was 1? That would give 2 hard and 0 easy! Also, I recommend that you put only 1 'poison pill' in each queue and have the workers put it back when they see it. From hendrik at microcorp.co.za Fri Jul 31 06:47:30 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 31 Jul 2009 12:47:30 +0200 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: <200907311247.30793.hendrik@microcorp.co.za> On Friday 31 July 2009 11:25:17 learner learner wrote: > Hi all, > > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a third > file or overwrite into one of them. This is not as simple as it seems. You will probably be better off using diff, or, if it is a learning exercise, studying its source. - Hendrik From deets at nospam.web.de Fri Jul 31 07:26:06 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 31 Jul 2009 13:26:06 +0200 Subject: problem on socket programming In-Reply-To: <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> Message-ID: <4A72D4CE.6030501@nospam.web.de> MalC0de schrieb: > On Jul 31, 12:07 pm, Chris Rebert wrote: >> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: >>> now I've been encountered to some errors with this snippet : >> And these errors are? Provide error messages and full tracebacks. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > these are errors : > > Traceback (most recent call last): > File "echo-client.py", line 11, in sockobj.connect((serverHost,serverPort)) > File "", line 1, in connect > socket.error: (10061, 'Connection refused') Can you ping and telnet to the port on the desired server? Firewalls can be an issue here. Diez From deets at nospam.web.de Fri Jul 31 07:26:41 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 31 Jul 2009 13:26:41 +0200 Subject: problem on socket programming In-Reply-To: <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> Message-ID: <7dg2niF2bue0tU2@mid.uni-berlin.de> MalC0de schrieb: > On Jul 31, 12:07 pm, Chris Rebert wrote: >> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: >>> now I've been encountered to some errors with this snippet : >> And these errors are? Provide error messages and full tracebacks. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > these are errors : > > Traceback (most recent call last): > File "echo-client.py", line 11, in sockobj.connect((serverHost,serverPort)) > File "", line 1, in connect > socket.error: (10061, 'Connection refused') Can you ping and telnet to the port on the desired server? Firewalls can be an issue here. Diez From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 31 07:38:56 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 31 Jul 2009 13:38:56 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <008e82e8$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <4a72d7cf$0$25239$426a34cc@news.free.fr> Steven D'Aprano a ?crit : > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > >> That and the fact that I couldn't stop laughing for long enough to learn >> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >> unlike less flexible languages, lets you alter the value of a constant." >> Yep, as they say "Bug" = "Undocumented feature"! > > That's no different from Python's "constant by convention". Well, at least Python doesn't pretend to have real symbolic constants - we all know it's only a convention !-) > We don't even > get a compiler warning! Of course - from the compiler's POV, it's only usual rebinding. > On the other hand, we don't have to prefix names with @ and @@, Nope, we have to prefix them with 'self' or 'cls' (or even 'self.__class__'). > and we > don't have the compiler trying to *guess* whether we're calling a > function or referring to a variable. Please re-read a bit more carefully - it's *all* method call. Python is 'uniform' in obj.name is always an attribute lookup (methods being attributes), Ruby is uniform in that 'obj.name' is always a method call. > Somebody who knows more Ruby than me should try writing the Zen of Ruby. > Something like: (snip childish parody of Python Zen) Steven, is that any useful ? > > Although I'm sure Ruby has its good points. I'm not convinced anonymous > code blocks are one of them though. Ruby's code blocks come from Smalltalk, where they are an absolute necessity since message passing (which code blocks are part of) is the *only* builtin control flow in Smalltalk - so you just *need* this construction to provide branching and iterations. Wether it makes sense to have code blocks in Ruby is another question since Ruby does provide traditional control flow features, but then one could question Python's "lambda" (hem...) too. From j.sundo at cs.ucl.ac.uk Fri Jul 31 07:41:49 2009 From: j.sundo at cs.ucl.ac.uk (=?ISO-8859-1?Q?Jannik_Sund=F8?=) Date: Fri, 31 Jul 2009 12:41:49 +0100 Subject: Confused About Python Loggin In-Reply-To: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> References: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> Message-ID: <14CCA71D-F575-4B8A-8C99-67BDAAC2C6E0@cs.ucl.ac.uk> Hi all. I think I fixed this problem by setting fileLogger.propagate = 0. Otherwise it will propagate up to the root logger, which outputs to stdout, as far as I can understand. On 31 Jul 2009, at 01:17, Jannik Sund? wrote: > Dear all, I am quite confused about the Python logging. I have read > and re-read the Python documentation for the Python logging module > and googled, but to no avail. I simply want one logger to log to a > file and another logger to log to the console. Neither should log > the other's messages. The code below causes fileLogger to log to > BOTH a file and the console, how come? It seems to me that a call > such as "consoleHandler = logging.StreamHandler()" affects other > loggers, which I do not want. Also, if I do not do > logging.basicConfig(level=logging.INFO) in one of the classes of my > application, the logging does not work. Any ideas how come? > > Thanks a lot for any help! :) > > fileHandler = logging.FileHandler('../../logs/log.txt') > fileLogger = logging.getLogger('TESTHARNESSFILE') > fileLogger.addHandler(fileHandler) > fileLogger.setLevel(logging.INFO) > consoleHandler = logging.StreamHandler() > logger = logging.getLogger('TESTHARNESS') > logger.addHandler(consoleHandler) > logger.setLevel(logging.INFO) From malc0de.encrypt at gmail.com Fri Jul 31 07:49:09 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 04:49:09 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> Message-ID: <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> you want to say shall i disable the firewall, there's no specific firewall or IPS/IDS system out there, this is just windows xp firewall From deets at nospam.web.de Fri Jul 31 08:08:25 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 31 Jul 2009 14:08:25 +0200 Subject: problem on socket programming In-Reply-To: <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> Message-ID: <7dg55pF2bfubpU1@mid.uni-berlin.de> MalC0de schrieb: > you want to say shall i disable the firewall, there's no specific > firewall or IPS/IDS system out there, this is just windows xp firewall Which is a firewall, don't you agree? So it could well be the reason for the problems you see. Diez From masklinn at masklinn.net Fri Jul 31 08:18:56 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 14:18:56 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a72d7cf$0$25239$426a34cc@news.free.fr> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> Message-ID: <23AB2CC9-71D1-4498-8D23-D3648C00DD4C@masklinn.net> On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote: > Steven D'Aprano a ?crit : >> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >>> That and the fact that I couldn't stop laughing for long enough to >>> learn >>> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >>> unlike less flexible languages, lets you alter the value of a >>> constant." >>> Yep, as they say "Bug" = "Undocumented feature"! >> That's no different from Python's "constant by convention". > Ruby's code blocks come from Smalltalk, where they are an absolute > necessity since message passing (which code blocks are part of) is > the *only* builtin control flow in Smalltalk - so you just *need* > this construction to provide branching and iterations. > I'm not so sure about the way you say it. I'm pretty sure "traditional" flow control structures preceded Smalltalk by a pair of decades so it's not that Smalltalk's designers found it necessary to use blocks & messages, but that they understood blocks & messages could trivially replace most control structures (making *those* unnecessary), making the core language simpler and more flexible. In other words, I see it the other way around. > Wether it makes sense to have code blocks in Ruby is another > question since Ruby does provide traditional control flow features Well it does at least allow for the creation of new flow control structures in library land when the existing ones aren't enough (e.g. allows Ruby not to require the introduction of a `with` statement). Though Ruby's blocks are nowhere near as flexible as Smalltalk's. From vinay_sajip at yahoo.co.uk Fri Jul 31 08:24:58 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Fri, 31 Jul 2009 05:24:58 -0700 (PDT) Subject: Confused About Python Loggin References: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> Message-ID: <21effe68-2202-4f58-a2d9-f1ea3968149a@d32g2000yqh.googlegroups.com> On Jul 31, 12:41?pm, Jannik Sund? wrote: > Hi all. I think I fixed this problem by setting fileLogger.propagate = ? > 0. Otherwise it will propagate up to the root logger, which outputs to ? > stdout, as far as I can understand. > Only if you specifically configure it to do so - for example, calling basicConfig() configures the root logger (if it has no handlers) by adding a StreamHandler which outputs to sys.stderr. You shouldn't need to set propagate - the script in Peter's reply appears to do what you need. Regards, Vinay Sajip From malc0de.encrypt at gmail.com Fri Jul 31 08:37:21 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 05:37:21 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> Message-ID: no, I disabled my firewall, no problem with even enabled firewall ... no one can help !? From davea at ieee.org Fri Jul 31 08:55:27 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 31 Jul 2009 08:55:27 -0400 Subject: file comparison In-Reply-To: <200907311247.30793.hendrik@microcorp.co.za> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> <200907311247.30793.hendrik@microcorp.co.za> Message-ID: <4A72E9BF.3080808@ieee.org> Hendrik van Rooyen wrote: > On Friday 31 July 2009 11:25:17 learner learner wrote: > >> Hi all, >> >> I want to compare two text files line by line and eliminate the >> matching/repeated line and store the unmatched/leftout lines into a third >> file or overwrite into one of them. >> > > This is not as simple as it seems. > > You will probably be better off using diff, or, > if it is a learning exercise, studying its source. > > - Hendrik > > > It is not only "not simple," it's totally underspecified. Learner: If this is a homework assignment, you should read the whole assignment, and see what it says. Without knowing something about the text files, I couldn't even guess what the real goal is. For example, if the text files are really text (e.g. a letter to grandma), then we want a context-sensitive comparison, the kind that Windiff, RCSDiff, DiffMerge, and others can do. If the text files are each a list of items, with order and duplication irrelevant, then your best bet is probably to build a set from each, and difference the sets. DaveA From jeanmichel at sequans.com Fri Jul 31 09:04:59 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 31 Jul 2009 15:04:59 +0200 Subject: Confused About Python Loggin In-Reply-To: <14CCA71D-F575-4B8A-8C99-67BDAAC2C6E0@cs.ucl.ac.uk> References: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> <14CCA71D-F575-4B8A-8C99-67BDAAC2C6E0@cs.ucl.ac.uk> Message-ID: <4A72EBFB.4080306@sequans.com> Jannik Sund? wrote: > Hi all. I think I fixed this problem by setting fileLogger.propagate = > 0. Otherwise it will propagate up to the root logger, which outputs to > stdout, as far as I can understand. > > > On 31 Jul 2009, at 01:17, Jannik Sund? wrote: > >> Dear all, I am quite confused about the Python logging. I have read >> and re-read the Python documentation for the Python logging module >> and googled, but to no avail. I simply want one logger to log to a >> file and another logger to log to the console. Neither should log the >> other's messages. The code below causes fileLogger to log to BOTH a >> file and the console, how come? It seems to me that a call such as >> "consoleHandler = logging.StreamHandler()" affects other loggers, >> which I do not want. Also, if I do not do >> logging.basicConfig(level=logging.INFO) in one of the classes of my >> application, the logging does not work. Any ideas how come? >> >> Thanks a lot for any help! :) >> >> fileHandler = logging.FileHandler('../../logs/log.txt') >> fileLogger = logging.getLogger('TESTHARNESSFILE') >> fileLogger.addHandler(fileHandler) >> fileLogger.setLevel(logging.INFO) >> consoleHandler = logging.StreamHandler() >> logger = logging.getLogger('TESTHARNESS') >> logger.addHandler(consoleHandler) >> logger.setLevel(logging.INFO) > > --http://mail.python.org/mailman/listinfo/python-list > Please do not top post :o) Are sure you have used the code provided above ? Loggers only propagate events to its parent. fileLogger = logging.getLogger('TESTHARNESSFILE" logger = logging.getLogger('TESTHARNESS') Those one have no parent relationship. However these one have parent relationship: fileLogger = logging.getLogger('TESTHARNESS.FILE") # note the dotted name logger = logging.getLogger('TESTHARNESS') Or maybe you have added a handler to the root logger... Using the propagate attribute is correct only between 2 loggers that are meant to be parents. JM From python at mrabarnett.plus.com Fri Jul 31 09:05:35 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 14:05:35 +0100 Subject: problem on socket programming In-Reply-To: References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> Message-ID: <4A72EC1F.7040709@mrabarnett.plus.com> MalC0de wrote: > no, I disabled my firewall, no problem with even enabled firewall ... > no one can help !? I hope you disconnected your computer from the internet before disabling the firewall... From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 31 09:12:42 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 31 Jul 2009 15:12:42 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> Message-ID: <4a72edca$0$32755$426a74cc@news.free.fr> Masklinn a ?crit : > On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote: >> Steven D'Aprano a ?crit : >>> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >>>> That and the fact that I couldn't stop laughing for long enough to >>>> learn >>>> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >>>> unlike less flexible languages, lets you alter the value of a >>>> constant." >>>> Yep, as they say "Bug" = "Undocumented feature"! >>> That's no different from Python's "constant by convention". >> Ruby's code blocks come from Smalltalk, where they are an absolute >> necessity since message passing (which code blocks are part of) is the >> *only* builtin control flow in Smalltalk - so you just *need* this >> construction to provide branching and iterations. >> > I'm not so sure about the way you say it. I'm not sure about the way you understand it !-) > I'm pretty sure "traditional" > flow control structures preceded Smalltalk by a pair of decades Yes, of course - and that's not the point. What's important is that: > so it's > not that Smalltalk's designers found it necessary to use blocks & > messages, but that they understood blocks & messages could trivially > replace most control structures (making *those* unnecessary), making the > core language simpler and more flexible. Exactly. > In other words, I see it the other way around. My wording may have been a bit confusing, indeed. It was implied (but perhaps a bit unclear) that restricting control structures to messages and blocks was a design choice. >> Wether it makes sense to have code blocks in Ruby is another question >> since Ruby does provide traditional control flow features > > Well it does at least allow for the creation of new flow control > structures in library land when the existing ones aren't enough (e.g. > allows Ruby not to require the introduction of a `with` statement). Yeps. But then other "traditionnal" control flow features become redundant. > Though Ruby's blocks are nowhere near as flexible as Smalltalk's. From processor.dev1l at gmail.com Fri Jul 31 09:15:00 2009 From: processor.dev1l at gmail.com (Processor-Dev1l) Date: Fri, 31 Jul 2009 06:15:00 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> Message-ID: <72f851f9-a2d4-4448-900d-3fe20e4ebeb0@w41g2000yqb.googlegroups.com> On Jul 31, 2:37?pm, MalC0de wrote: > no, I disabled my firewall, no problem with even enabled firewall ... > no one can help !? Connection refused... connection was refused by the listening socket, it could also be due to the settings of your service, if it accepts only from localhost to localhost (127.0.0.1 to 127.0.0.1) then other connections will not be granted. Try to find some socket samples on python.org (there are plenty of tip and tricks for using sockets) From processor.dev1l at gmail.com Fri Jul 31 09:21:14 2009 From: processor.dev1l at gmail.com (Processor-Dev1l) Date: Fri, 31 Jul 2009 06:21:14 -0700 (PDT) Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <52bdb49f-935f-459c-bc64-76e5ccb16f36@k6g2000yqn.googlegroups.com> On Jul 30, 4:47?pm, "Barak, Ron" wrote: > > -----Original Message----- > > From: Dave Angel [mailto:da... at dejaviewphoto.com] > > Sent: Thursday, July 30, 2009 16:03 > > To: Barak, Ron > > Cc: 'Dave Angel'; 'python-l... at python.org' > > Subject: RE: Run pyc file without specifying python path ? > > > Barak, Ron wrote: > > >> -----Original Message----- > > >> From: Dave Angel [mailto:da... at ieee.org] > > >> Sent: Wednesday, July 29, 2009 21:05 > > >> To: Barak, Ron > > >> Cc: 'python-l... at python.org' > > >> Subject: Re: Run pyc file without specifying python path ? > > > >> Barak, Ron wrote: > > > >>> Hi, > > > >>> I wanted to make a python byte-code file executable, > > > >> expecting to be able to run it without specifying "python" on the > > >> (Linux bash) command line. > > > >>> So, I wrote the following: > > > >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > > > >>> print "hello" > > >>> [root at VMLinux1 python]# > > > >>> and made its pyc file executable: > > > >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc > > >>> 4 -rwxr-xr-x ?1 root root 106 Jul 29 14:22 test_pyc.pyc > > >>> [root at VMLinux1 python]# > > > >>> So, I see: > > > >>> [root at VMLinux1 python]# file test_pyc.py* > > >>> test_pyc.py: ?a python script text executable > > >>> test_pyc.pyc: python 2.3 byte-compiled > > >>> [root at VMLinux1 python]# > > > >>> If I try to do the following, no problem: > > > >>> [root at VMLinux1 python]# python test_pyc.pyc hello > > >>> [root at VMLinux1 python]# > > > >>> However, the following fails: > > > >>> [root at VMLinux1 python]# ./test_pyc.pyc > > >>> -bash: ./test_pyc.pyc: cannot execute binary file > > >>> [root at VMLinux1 python]# > > > >>> Is there a way to run a pyc file without specifying the > > > >> python path ? > > > >>> Bye, > > >>> Ron. > > > >> I don't currently run Unix, but I think I know the problem. > > > >> In a text file, the shell examines the first line, and if > > it begins > > >> #! > > >> it's assumed to point to the executable of an interpreter for that > > >> text file. ?Presumably the same trick doesn't work for a .pyc file. > > > >> Why not write a trivial wrapper.py file, don't compile it, and let > > >> that invoke the main code in the .pyc file? > > > >> Then make wrapper.py executable, and you're ready to go. > > > >> DaveA > > > > Hi Dave, > > > Your solution sort of defeats my intended purpose (sorry > > for not divulging my 'hidden agenda'). > > > I wanted my application to "hide" the fact that it's a > > python script, and look as much as possible like it's a > > compiled program. > > > The reason I don't just give my user a py file, is that I > > don't want a cleaver user to change the innards of the script. > > > On the other hand, I don't want to make a compiled > > (freezed?) version of the application, because it'll grow the > > resulting file significantly, and I don't have the experience > > to know how it will run on different Linuxes. > > > Bye, > > > Ron. > > Hi Dave, > > > Most of the other answers basically paraphrased my suggestion > > of making a wrapper file, not compiling it, and making it > > executable. ?With that > > approach, the user just types ? "wrapper.py" on his command line. > > > And wrapper.py only needs two lines, a shebang, and an > > import, no big deal if the user modifies it. ?The rest of > > your code can be .pyc files. > > > Steven makes some good points. ?You have to define what level > > of clever you're protecting from. ?A determined hacker will > > The clever I try to guard against is not at hacker level, just the curios worker. > > > get in no matter what you do, unless you want to ship the > > program in a proprietary embedded system, encased in epoxy. ? > > Further, if you have an extension of .py or .pyc, a > > knowledgeable hacker will know it's probably python. > > Granted, but the user will know that from the user manual anyway :-) > > > > > You imply you want it to run unmodifed on multiple unknown > > Linux versions. ?I think that lets out binfmt solutions. ? > > Actually, no: I know the set of Linuxes my users are likely to have. > > > That means you need to test and support not only multiple > > Linux implementations, but multiple Python versions, because > > who knows what the user may have installed. ?I think you need > > to rethink your support strategy. ?And maybe concentrate on > > being able to detect change, rather than prevent it. > > > DaveA > > Thanks for the helpful suggestions. > Ron. how can a compiled python file (pyc) can defend your code against clever-user? Try command cat "yourcode.pyc" and you will is how "hardly" it can be read or modified :). The most easy way how to distribute such file is with some shell batch containing #!/usr/bin/sh python ./lib/myscript.pyc the topology of the app would be a shell script called simply run, or name of your app and something like that and a folder called lib where your pyc file is stored, just make the script executable (chmod a+x shellfile or chmod 755 shellfile) and force ppl to use it instead of the pyc file in the lib folder :) From masklinn at masklinn.net Fri Jul 31 09:39:47 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 15:39:47 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a72edca$0$32755$426a74cc@news.free.fr> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <4a72edca$0$32755$426a74cc@news.free.fr> Message-ID: On 31 Jul 2009, at 15:12 , Bruno Desthuilliers wrote: > Masklinn a ?crit : >> On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote: >>> Steven D'Aprano a ?crit : >>>> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >>>>> That and the fact that I couldn't stop laughing for long enough >>>>> to learn >>>>> any more when I read in the Pragmatic Programmer's Guide that >>>>> "Ruby, >>>>> unlike less flexible languages, lets you alter the value of a >>>>> constant." >>>>> Yep, as they say "Bug" = "Undocumented feature"! >>>> That's no different from Python's "constant by convention". >>> Ruby's code blocks come from Smalltalk, where they are an absolute >>> necessity since message passing (which code blocks are part of) is >>> the *only* builtin control flow in Smalltalk - so you just *need* >>> this construction to provide branching and iterations. > [misunderstandings on my part/clarifications on yours] >> Well it does at least allow for the creation of new flow control >> structures in library land when the existing ones aren't enough >> (e.g. allows Ruby not to require the introduction of a `with` >> statement). > > Yeps. But then other "traditionnal" control flow features become > redundant. They can be anyway: Ruby doesn't deprecate most control flows as the actual usages of blocks are a bit restricted (cannot be used for `while` as it would require the serialization to a Proc, and Ruby's syntax doesn't allow sending multiple blocks to a method so `if?else` is out as well). And I assume they reintroduced the for?in sugar to ease the transition from more traditional languages (using #each and others seems the suggested collection iterators across the community). From malc0de.encrypt at gmail.com Fri Jul 31 09:43:49 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 06:43:49 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> <72f851f9-a2d4-4448-900d-3fe20e4ebeb0@w41g2000yqb.googlegroups.com> Message-ID: none of you can't interpret it ? do you have errors like me !? From gary.wilson at gmail.com Fri Jul 31 09:51:05 2009 From: gary.wilson at gmail.com (Gary Wilson) Date: Fri, 31 Jul 2009 06:51:05 -0700 (PDT) Subject: supported versions policy? Message-ID: <07526890-6264-43c0-bd01-29ffdb3eedc2@d9g2000prh.googlegroups.com> Does Python have a formal policy on the support lifetime (bug fixes, security fixes, etc.) for major and minor versions? I did a bit of searching on the Python web site and this group, but didn't find anything. If there is a policy posted somewhere (and I just didn't dig deep enough), would someone please point me in the right direction. If there is no posted policy, would someone be kind enough to describe the practiced policy here. I'm looking for is something like this: http://docs.djangoproject.com/en/dev/internals/release-process/#supported-versions Thanks, Gary From Scott.Daniels at Acm.Org Fri Jul 31 09:51:17 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 31 Jul 2009 06:51:17 -0700 Subject: fast video encoding In-Reply-To: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> References: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Message-ID: gregorth wrote: > for a scientific application I need to save a video stream to disc for > further post processing. My cam can deliver 8bit grayscale images with > resolution 640x480 with a framerate up to 100Hz, this is a data rate > of 30MB/s. Writing the data uncompressed to disc hits the data > transfer limits of my current system and creates huge files. Therefore > I would like to use video compression, preferably fast and high > quality to lossless encoding. Final file size is not that important. Well, it sounds like it better be enough to affect bandwidth. > I am a novice with video encoding. I found that few codecs support > gray scale images. Any hints to take advantage of the fact that I only > have gray scale images? You might try to see if there is a primitive .MNG encoder around. That could give you lossless with perhaps enough compression to make you happy, and I'm sure it will handle the grayscale. .MNG is pictures only, but that doesn't hurt you in the least. --Scott David Daniels Scott.Daniels at Acm.Org From roger.miller at nova-sol.com Fri Jul 31 10:22:56 2009 From: roger.miller at nova-sol.com (Roger Miller) Date: Fri, 31 Jul 2009 07:22:56 -0700 (PDT) Subject: Test for Pythonwin? References: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> Message-ID: <7be1b318-851d-4197-afd4-48de38c878e6@d34g2000vbm.googlegroups.com> On Jul 31, 2:09?am, "steve" wrote: > Is there a good way to check if a script is running inside Pythonwin? > Perhaps a property or method that is exposed by that environment? I've used if sys.stdin.fileno() < 0: This is not precisely a test for pythonwin, but indicates whether standard streams are available, which is usually what I really want to know. From ethan at stoneleaf.us Fri Jul 31 11:08:29 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 31 Jul 2009 08:08:29 -0700 Subject: Confessions of a Python fanboy In-Reply-To: <008e82e8$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <4A7308ED.7040108@stoneleaf.us> Steven D'Aprano wrote: > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > > >>That and the fact that I couldn't stop laughing for long enough to learn >>any more when I read in the Pragmatic Programmer's Guide that "Ruby, >>unlike less flexible languages, lets you alter the value of a constant." >>Yep, as they say "Bug" = "Undocumented feature"! > > > That's no different from Python's "constant by convention". We don't even > get a compiler warning! > That's quite different, actually. Python doesn't claim to have constants! Can't misinterpret what you can't have, can you? [Holds breath while awaiting counter-example... :] ~Ethan~ From ethan at stoneleaf.us Fri Jul 31 11:15:14 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 31 Jul 2009 08:15:14 -0700 Subject: Regexp problem In-Reply-To: <4A72135E.7060205@mrabarnett.plus.com> References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> <4A720AD5.1030202@stoneleaf.us> <4A72135E.7060205@mrabarnett.plus.com> Message-ID: <4A730A82.3050502@stoneleaf.us> MRAB wrote: > Ethan Furman wrote: > >> Marcus Wanner wrote: >>> Wow, I really need to learn more about regexp... >>> Any tutorials you guys can recommend? >>> >>> Marcus >> >> >> Mastering Regular Expressions >> Powerful Techniques for Perl and Other Tools >> By Jeffrey E. F. Friedl >> >> Great book! >> > +1 > > I have the first edition, seventh printing (December 1998). It refers to > the 'regex' module of Python 1.4b1, which was subsequently replaced by > the current 're' module and then removed from the standard library. I > hope it's been updated since then. :-) I have the second edition (no idea which printing ;), and according to his preface it has indeed been much updated. Most examples are in perl, the few in python are decent. The knowledge embodied seems very thorough. Since I've had the book (two weeks now?) I've been able to solve two otherwise thorny issues using regular expressions. Yay! ~Ethan~ From iainking at gmail.com Fri Jul 31 11:35:45 2009 From: iainking at gmail.com (Iain King) Date: Fri, 31 Jul 2009 08:35:45 -0700 (PDT) Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: On Jul 31, 4:08?pm, Ethan Furman wrote: > Steven D'Aprano wrote: > > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > > >>That and the fact that I couldn't stop laughing for long enough to learn > >>any more when I read in the Pragmatic Programmer's Guide that "Ruby, > >>unlike less flexible languages, lets you alter the value of a constant." > >>Yep, as they say "Bug" = "Undocumented feature"! > > > That's no different from Python's "constant by convention". We don't even > > get a compiler warning! > > That's quite different, actually. ?Python doesn't claim to have > constants! ?Can't misinterpret what you can't have, can you? > > [Holds breath while awaiting counter-example... :] > > ~Ethan~ The convention being detailed in PEP8: http://www.python.org/dev/peps/pep-0008/ basically, anything in ALL_CAPS is a constant, assuming you follow those style guidelines. Iain From benjamin.kaplan at case.edu Fri Jul 31 11:52:37 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 31 Jul 2009 11:52:37 -0400 Subject: Confessions of a Python fanboy In-Reply-To: References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: On Fri, Jul 31, 2009 at 11:35 AM, Iain King wrote: > On Jul 31, 4:08?pm, Ethan Furman wrote: >> Steven D'Aprano wrote: >> > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >> >> >>That and the fact that I couldn't stop laughing for long enough to learn >> >>any more when I read in the Pragmatic Programmer's Guide that "Ruby, >> >>unlike less flexible languages, lets you alter the value of a constant." >> >>Yep, as they say "Bug" = "Undocumented feature"! >> >> > That's no different from Python's "constant by convention". We don't even >> > get a compiler warning! >> >> That's quite different, actually. ?Python doesn't claim to have >> constants! ?Can't misinterpret what you can't have, can you? >> >> [Holds breath while awaiting counter-example... :] >> >> ~Ethan~ > > The convention being detailed in PEP8: http://www.python.org/dev/peps/pep-0008/ > basically, anything in ALL_CAPS is a constant, assuming you follow > those style guidelines. > Right, but that's a style guide and not a language definition. Nobody claims that anything in there is a part of the language. > Iain > -- > http://mail.python.org/mailman/listinfo/python-list > From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 11:55:43 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 15:55:43 GMT Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> Message-ID: <008eef29$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 13:38:56 +0200, Bruno Desthuilliers wrote: >> On the other hand, we don't have to prefix names with @ and @@, > > Nope, we have to prefix them with 'self' or 'cls' (or even > 'self.__class__'). Incorrect. >>> class K: ... class_attribute = 'No @@ required.' ... >>> K().class_attribute 'No @@ required.' No 'self' or 'cls' in sight. I can see a vague advantage to using @ to create instance attributes and @@ to creating class attributes -- it makes it easy to create class attributes inside a method without needing to do a double lookup. In Python terms: def K: def method(self): self.__class__.attr = "Class attribute" would be: def K: def method(self): @@attr = "Class attribute" Advantages: you save a few characters and (possibly!) a couple of runtime lookups. Disadvantages: your code is filled with line noise. It's an arbitrary choice between @@ meaning instance attribute and @@ meaning class attribute -- there's no logical reason for choosing one over the other, so you have to memorise which is which. It's easy to get it wrong. 'self' and 'cls' at least are words, even if 'cls' is badly misspelled :) >> and we >> don't have the compiler trying to *guess* whether we're calling a >> function or referring to a variable. > > Please re-read a bit more carefully - it's *all* method call. What did I misread from here? [quote] When Ruby sees a name such as ``a'' in an expression, it needs to determine if it is a local variable reference or a call to a method with no parameters. To decide which is the case, Ruby uses a heuristic. As Ruby reads a source file, it keeps track of symbols that have been assigned to. It assumes that these symbols are variables. When it subsequently comes across a symbol that might be either a variable or a method call, it checks to see if it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable; otherwise it treats it as a method call. [end quote] And see the example "pathological case" comparing a function call (not method) and a variable. http://ruby-doc.org/docs/ProgrammingRuby/html/language.html > Python is > 'uniform' in obj.name is always an attribute lookup (methods being > attributes), Ruby is uniform in that 'obj.name' is always a method call. Which would be relevant if I was talking about method calls, but I wasn't. >> Somebody who knows more Ruby than me should try writing the Zen of >> Ruby. Something like: > > (snip childish parody of Python Zen) > > Steven, is that any useful ? It made me feel good. But seriously, while I admit that I have very little Ruby experience, and so aren't in a great position to judge, it seems to me that Ruby doesn't have anything like Python's over-riding design principles (the Zen). If there is a design principle to Ruby, I can't see what it is. I'm the first to admit that I'm far too inexperienced with the language to make this a fair judgement. Unfair it might be, but it doesn't necessarily mean I'm wrong! *wink* Ruby just seems to be far more complicated than Python: things which Python does at runtime, with a function call, Ruby has special syntax for: "a bunch of words".split() %w(a bunch of words) ord('a') ?a That makes the barrier to entry far higher: it's easier to leverage existing knowledge to interpret unfamiliar Python code than unfamiliar Ruby code. Or so it seems to me. Oh, and I admit that Python decorators are a conspicuous counter-example. I wouldn't do without them, but neither would I expect somebody to intuit what they do. >> Although I'm sure Ruby has its good points. I'm not convinced anonymous >> code blocks are one of them though. > > Ruby's code blocks come from Smalltalk, where they are an absolute > necessity since message passing (which code blocks are part of) is the > *only* builtin control flow in Smalltalk - so you just *need* this > construction to provide branching and iterations. Just because Smalltalk had a particular (mis?)feature doesn't mean that other languages should copy it. I know, I know, Ruby people swear by anonymous code blocks, and I've read Paul Graham too. But I'm really not so sure that the benefits of anonymous code blocks are great enough to overcome the disadvantages of anonymous code blocks. > Wether it makes sense to have code blocks in Ruby is another question > since Ruby does provide traditional control flow features, but then one > could question Python's "lambda" (hem...) too. lambda, by allowing the function to be only a single expression, doesn't suffer the disadvantages of anonymous code blocks. lambda, by allowing the function to be only a single expression, also has fewer advantages than anonymous code blocks. I think lambda ends up on the "more advantages than disadvantages" side. I'm keeping my mind open regarding Ruby code blocks. -- Steven From masklinn at masklinn.net Fri Jul 31 12:15:15 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 18:15:15 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <008eef29$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <008eef29$0$9756$c3e8da3@news.astraweb.com> Message-ID: <11938EF7-4FC3-4F1A-A665-D9360442456F@masklinn.net> On 31 Jul 2009, at 17:55 , Steven D'Aprano wrote: > But seriously, while I admit that I have very little Ruby > experience, and > so aren't in a great position to judge, it seems to me that Ruby > doesn't > have anything like Python's over-riding design principles (the Zen). > If > there is a design principle to Ruby, I can't see what it is. > As far as I know, Ruby doesn't have anything like the Zen no. But then again, I don't know any language other than Python with such a document. > Ruby just seems to be far more complicated than Python: things which > Python does at runtime, with a function call, Ruby has special > syntax for: > > "a bunch of words".split() > %w(a bunch of words) > > ord('a') > ?a > That's the Perl heritage. And since it inherits from Perl, TIMTOWTDI: >> "a bunch of words".split => ["a", "bunch", "of", "words"] >> "a"[0] => 97 (yes, the indexing operator returns a character code if provided a single index. With two, it returns as string slice) Oh and %w() isn't really equivalent to split(): you don't use it to split a string but to create a list of strings, so the equivalent expression in Python would be `["a", "bunch", "of", "words"]`. > That makes the barrier to entry far higher: it's easier to leverage > existing knowledge to interpret unfamiliar Python code than unfamiliar > Ruby code. Or so it seems to me. > That's probable. >>> Although I'm sure Ruby has its good points. I'm not convinced >>> anonymous >>> code blocks are one of them though. >> >> Ruby's code blocks come from Smalltalk, where they are an absolute >> necessity since message passing (which code blocks are part of) is >> the >> *only* builtin control flow in Smalltalk - so you just *need* this >> construction to provide branching and iterations. > > Just because Smalltalk had a particular (mis?)feature doesn't mean > that > other languages should copy it. I know, I know, Ruby people swear by > anonymous code blocks, and I've read Paul Graham too. But I'm really > not > so sure that the benefits of anonymous code blocks are great enough to > overcome the disadvantages of anonymous code blocks. > What are the disadvantages of anonymous functions? From tjreedy at udel.edu Fri Jul 31 12:24:28 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 12:24:28 -0400 Subject: Confessions of a Python fanboy In-Reply-To: References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: Masklinn wrote: > #each is simply a method that takes a function (called blocks in ruby). > One could call it a higher-order method I guess. > > It's an implementation of the concept of internal iteration: instead of > collections yielding iterator objects, and programmers using those > through specially-built iteration constructs (e.g. `for?in`), > collections control iteration over themselves (the iteration is > performed "inside" the collection, thus the "internal" part) and the > programmer provides the operations to perform at each iterative step > through (usually) a function. Python's iterator protocol was developed in part to avoid the (inside-out) callback style of programming. Writing virtual collections as generator functions instead of iterator or iterable classes saves a lot of boilerplate code. The itertools modules shows how nicely iterators can be composed in a way that is much more awkward with callbacks. > In Python (assuming we had anonymous defs and an each method on lists), > the following loop: > > for item in some_list: > do_something(item) > do_something_else(item) > > some_list.each((def (item): > do_something(item) > do_something_else(item) > )) And how does Ruby do the equivalent of def double(it): for i in it: yield 2*i for i,j in zip(double(some_list), some_gen_func(args)): print(do_something(i+j,i-j)) Terry Jan Reedy From bdezonia at wisc.edu Fri Jul 31 12:33:45 2009 From: bdezonia at wisc.edu (BDZ) Date: Fri, 31 Jul 2009 09:33:45 -0700 (PDT) Subject: os.path.exists() and Samba shares References: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> Message-ID: <337482f9-8ad9-4600-9cf4-39911f5368ac@h31g2000yqd.googlegroups.com> On Jul 30, 4:41?pm, Lo?c Domaign? wrote: > Hi, > > > Hello. I have written a Python 3.1 script running on Windows that uses > > os.path.exists() to connect to network shares. If the various network > > shares require different user account and password combos than the > > account the script is running under the routine returns false. I need > > something like os.samba.path.exists(username,password,path). Does > > anyone have a suggestion on how I can accomplish what I need to do in > > Python? > > Could the Python Samba module PySamba be interesting for you?http://sourceforge.net/projects/pysamba/ > > HTH, > Lo?c > -- > My blog:http://www.domaigne.com/blog Unfortunately, although it has the calls I'd want, pysamba appears to be *nix only. I need something that will work under Windows. Is there a set of Python Windows functions (official or contributed) that might do what I need? (I'm new to Python) From darkneter at gmail.com Fri Jul 31 12:35:10 2009 From: darkneter at gmail.com (NighterNet) Date: Fri, 31 Jul 2009 09:35:10 -0700 (PDT) Subject: threads check socket Message-ID: I been trying to find a way to check the socket is open or not. The thread are in a group and loop if the sockets are open. If they are not open delete the thread and remove the group. I need on this. From tjreedy at udel.edu Fri Jul 31 12:39:21 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 12:39:21 -0400 Subject: supported versions policy? In-Reply-To: <07526890-6264-43c0-bd01-29ffdb3eedc2@d9g2000prh.googlegroups.com> References: <07526890-6264-43c0-bd01-29ffdb3eedc2@d9g2000prh.googlegroups.com> Message-ID: Gary Wilson wrote: > Does Python have a formal policy on the support lifetime (bug fixes, > security fixes, etc.) for major and minor versions? I did a bit of > searching on the Python web site and this group, but didn't find > anything. If there is a policy posted somewhere (and I just didn't > dig deep enough), would someone please point me in the right > direction. If there is no posted policy, would someone be kind enough > to describe the practiced policy here. Defining Pyx.y.z as Pymajor.minor.bugfix, the last normal bugfix comes out about the same time as the next minor. True security fixes (very rare) come out for a couple of years thereafter. (Exception, 3.0 support ended with 3.0.1 -- it should be replaced with 3.1.) Currently Py2 and Py3 are running in parallel. The future 2.7.z's will probably be the end of the Py2 series. Everything is contingent on voluteer time to do the above. tjr From db3l.net at gmail.com Fri Jul 31 12:40:34 2009 From: db3l.net at gmail.com (David Bolen) Date: Fri, 31 Jul 2009 12:40:34 -0400 Subject: fast video encoding References: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Message-ID: gregorth writes: > I am a novice with video encoding. I found that few codecs support > gray scale images. Any hints to take advantage of the fact that I only > have gray scale images? I don't know that there's any good way around the fact that video encoding is simply one of the heavier CPU-bound activities you're likely to encounter. So I suspect that codec choice (barring letting quality drop a bit) is going to move the bar less than picking the beefiest CPU you can. If I were looking to do this, I'd probably include investigating pumping the raw camera images into an ffmpeg encoding subprocess and let it handle all the encoding. There's about a gazillion options and a variety of codec options to play with for performance. You could grab and store a short video sample from the camera and use it as a benchmark to compare encoding options. ffmpeg does have a -pix_fmt option that can be used to indicate the input pixel type - "gray" would be 8-bit, and result in a 4:0:0 image with a YUV-based encoder, for example. Not sure how much, if any, impact it would have on encoding speed though. To be honest, with your data rate, I might even consider getting Python out of the pure encoding path once it starts - depending on the raw network format delivered by the camera you might be able to have ffmpeg read directly from it. Might not be worth it, since the data transfer is probably I/O bound, but a 640x480x1 stream at 100Hz is still nothing to sniff at, and even managing the raw data flow in Python might eat up some CPU that you could better allocate to the encoding process, or require extra data copies along the way that would be best to avoid. -- David From garrickp at gmail.com Fri Jul 31 12:53:44 2009 From: garrickp at gmail.com (Falcolas) Date: Fri, 31 Jul 2009 09:53:44 -0700 (PDT) Subject: Confessions of a Python fanboy References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> Message-ID: <5028a8b0-21b6-4052-8483-7236b9142bd9@h21g2000yqa.googlegroups.com> On Jul 31, 3:49?am, Masklinn wrote: > On 31 Jul 2009, at 10:25 , Chris Rebert wrote:> On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho ? > > wrote: > >> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn ? > >> wrote: > > >>> ... but since Python doesn't have anonymous functions that ? > >>> usage > >>> tends to be a bit too verbose ... > > >> Sorry to interrupt, but wouldn't lambda in Python be considered as > >> 'anonymous functions'? > > > I believe "full" anonymous functions was intended by the author. > > lambdas are limited to a single expression. > > Yes, and they're limited to a single *expression*, so before Python 3, ? > lambda: print "foo" is out. Likewise, it isn't possible to have an if/ > else statement within a lambda (though a ternary is ok), or a with, ? ? > since Python statements aren't expressions. Perhaps you can't do lambda foo: print foo, but you *can* do lambda x: sys.stdout.write(x). Combined with the ternary if, it seem sufficient if you want to stick to the ideal "Simple is better than Complex". Sure, with statements allowed in anonymous functions, you can save on a line of typing (def blargh(x):), but I don't feel the obfuscation is worth the cost. ~G From digitig at gmail.com Fri Jul 31 12:54:23 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 31 Jul 2009 17:54:23 +0100 Subject: Confessions of a Python fanboy In-Reply-To: <008e82e8$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: 2009/7/31 Steven D'Aprano : > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > >> That and the fact that I couldn't stop laughing for long enough to learn >> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >> unlike less flexible languages, lets you alter the value of a constant." >> Yep, as they say "Bug" = "Undocumented feature"! > > That's no different from Python's "constant by convention". We don't even > get a compiler warning! We don't actually *declare* that something is constant and then have that declaration ignored. Python doesn't lie to us, although (as in any language) a programmer might. -- Tim Rowe From jabronson at gmail.com Fri Jul 31 13:02:55 2009 From: jabronson at gmail.com (Joshua Bronson) Date: Fri, 31 Jul 2009 10:02:55 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: On Jul 22, 7:55?am, Duncan Booth wrote: > I find it interesting that the heapq functions tell you in the > documentation that they aren't suitable for use where n==1 or where n is > near the total size of the sequence whereas random.sample() chooses what it > thinks is the best algorithm based on the input. At the very least I would > have thought the heapq functions could check for n==1 and call min/max when > it is. I've had the same thought myself a number of times, so I filed a bug: http://bugs.python.org/issue6614 From marcusw at cox.net Fri Jul 31 13:22:24 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 31 Jul 2009 13:22:24 -0400 Subject: threads check socket In-Reply-To: References: Message-ID: On 7/31/2009 12:35 PM, NighterNet wrote: > I been trying to find a way to check the socket is open or not. The > thread are in a group and loop if the sockets are open. If they are > not open delete the thread and remove the group. I need on this. Being a bit more specific would help. Are you using tcp or udp? Is it a local socket or a remote one? What is controlling the socket? Define "open". Marcus From gagsl-py2 at yahoo.com.ar Fri Jul 31 13:23:02 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 14:23:02 -0300 Subject: threads check socket References: Message-ID: En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet escribi?: > I been trying to find a way to check the socket is open or not. The > thread are in a group and loop if the sockets are open. If they are > not open delete the thread and remove the group. I need on this. What means an "open socket"? Do you want to detect whether the other side closed the connection? In that case reading from the socket returns an empty string. Regarding the "threads in a group", I don't understand what you mean. But in general, you don't delete a thread, you just return from its run() method and the thread finishes execution. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 13:32:13 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 14:32:13 -0300 Subject: file comparison References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: En Fri, 31 Jul 2009 06:25:17 -0300, learner learner escribi?: > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a third > file or overwrite into one of them. Look at the difflib module: http://docs.python.org/library/difflib.html If that's not what you want, you have to provide much more details. -- Gabriel Genellina From python at rcn.com Fri Jul 31 13:32:46 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 10:32:46 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: On Jul 22, 4:55?am, Duncan Booth wrote: > Steven D'Aprano wrote: > > But that's the wrong solution to the problem. The OP wants the largest > > (or smallest) item, which he expects to get by sorting, then grabbing > > the first element: > > > sorted(alist)[0] > > > That requires sorting the entire list, only to throw away everything > > except the first item. A better solution is to use min() and max(), > > neither of which sort the list, so they are much faster. > > And if they want the n smallest or largest items (where n is significantly > less than the length of the list[*]) they might consider using > heapq.nsmallest() and heapq.nlargest() > > I find it interesting that the heapq functions tell you in the > documentation that they aren't suitable for use where n==1 or where n is > near the total size of the sequence whereas random.sample() chooses what it > thinks is the best algorithm based on the input. At the very least I would > have thought the heapq functions could check for n==1 and call min/max when > it is. The heapq.py code in Py2.7 and Py3.1 already does some automatic algorithm selection: http://svn.python.org/view/python/branches/release31-maint/Lib/heapq.py?revision=73579&view=markup The automatic seletion of alternatives only occurs in clear-cut cases (such as n==1 or where n==len(iterable) when the iterable has a known length). For the remaining cases, the switchover point from heapq to sorted needs a programmer's judgment based on whether the input iterable has a known length, the cost of comparison function, and whether input is already partially ordered. The advice in the docs helps the reader understand the relationships between min, max, nsmallest, nlargest, and sorted. Raymond From ethan at stoneleaf.us Fri Jul 31 13:41:15 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 31 Jul 2009 10:41:15 -0700 Subject: .dbf tables and Null Message-ID: <4A732CBB.9090400@stoneleaf.us> Mornin'! and a good one, too, I hope. Question for you... First part of the question: What is the general value in having Null capability for fields? Second part: Is there a tangible difference between Null, and the nothing of 0, '', False, etc, in python? Third part: If there is a tangible difference, do those of us who use python and these old, refuse-to-die, .dbf files actually have need of, or have tables including, Null values? P.S. part (for John Machin, if he sees this ;) Will the dbf package you are working on support Null values? Any and all information appreciated! ~Ethan~ From darkneter at gmail.com Fri Jul 31 13:42:29 2009 From: darkneter at gmail.com (NighterNet) Date: Fri, 31 Jul 2009 10:42:29 -0700 (PDT) Subject: threads check socket References: Message-ID: <7b3247b0-4c85-4290-9de2-41fc45348d3d@z4g2000prh.googlegroups.com> On Jul 31, 10:23?am, "Gabriel Genellina" wrote: > En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet ? > escribi?: > > > I been trying to find a way to check the socket is open or not. The > > thread are in a group and loop if the sockets are open. If they are > > not open delete the thread and remove the group. I need on this. > > What means an "open socket"? Do you want to detect whether the other side ? > closed the connection? In that case reading from the socket returns an ? > empty string. > > Regarding the "threads in a group", I don't understand what you mean. But ? > in general, you don't delete a thread, you just return from its run() ? > method and the thread finishes execution. > > -- > Gabriel Genellina Thread added into the array. Well this thread when user leave the server want to make sure it not in used so that I can reuse it some how or move into another array. Need to know the thread speed that need for game server timer to control it. Don't want it too fast or slow for the game server. fps = 30 def run(self): while True: self.gametime += 1 if self.gametime > 10000000/fps: self.gametime = 0; From jabronson at gmail.com Fri Jul 31 13:44:33 2009 From: jabronson at gmail.com (Joshua Bronson) Date: Fri, 31 Jul 2009 10:44:33 -0700 (PDT) Subject: heapq "key" arguments Message-ID: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> According to http://docs.python.org/library/heapq.html, Python 2.5 added an optional "key" argument to heapq.nsmallest and heapq.nlargest. I could never understand why they didn't also add a "key" argument to the other relevant functions (heapify, heappush, etc). Say I want to maintain a heap of (x, y) pairs sorted only by first coordinate. Without being able to pass key=itemgetter(0), won't heapifying a list of such pairs unnecessarily compare both coordinates? And worse, if the second coordinate is actually an object with no ordering defined for it, heapifying will cause an error even though all I care about is sorting by the first coordinate, which does have an ordering. Am I missing something? From python at rcn.com Fri Jul 31 13:49:04 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 10:49:04 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> On Jul 20, 9:27?am, Phillip B Oldham wrote: > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. The underlying C code for the two is substantially the same. In terms of code, tuples are in effect just immutable lists that have the added feature of being hashable (and therefore usable as dictionary keys or elements of sets). Beyond the mutable/hashable distinction, there is an important philosophical distinction articulated by Guido. He deems tuples to be useful for struct like groupings of non-homogenous fields and lists to be useful for sequences of homogenous data suitable for looping. While nothing in the list/tuple code requires you to make that distinction, it is important because that philosophy pervades the language. If you follow Guido's direction, you'll find that the various parts of the language fit together better. Do otherwise and you'll be going against the grain. Raymond P.S. The C code for lists and tuples have a slightly difference internal structure that makes tuples a little more space efficient (since their size is known at the outset and doesn't change during use). From jgardner at jonathangardner.net Fri Jul 31 14:02:48 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Fri, 31 Jul 2009 11:02:48 -0700 (PDT) Subject: heapq "key" arguments References: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> Message-ID: <1582f82e-34aa-4b66-b989-c9f8351a5f1c@o9g2000prg.googlegroups.com> On Jul 31, 10:44?am, Joshua Bronson wrote: > Say I want to maintain a heap of (x, y) pairs sorted only by > first coordinate. Without being able to pass key=itemgetter(0), won't > heapifying a list of such pairs unnecessarily compare both > coordinates? It will compare the second value only if the first values are equal. From bfrederi at library.unt.edu Fri Jul 31 14:08:44 2009 From: bfrederi at library.unt.edu (Brandon Fredericks) Date: Fri, 31 Jul 2009 11:08:44 -0700 (PDT) Subject: base64 Incorrect Padding Message-ID: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> I did a search within this group, but couldn't find any information on this. I am sending base64 encoded data as the content over http using urllib2 urlopen. When I receive the data and attempt to decode it, I get an "Incorrect Padding" error. Is there a simple way to fix this? A better way to send and receive the data possibly (using base64 of course)? From masklinn at masklinn.net Fri Jul 31 14:15:09 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 20:15:09 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: <003A7A2F-0FF1-42D4-96ED-1EBBA605B475@masklinn.net> On 31 Jul 2009, at 18:24 , Terry Reedy wrote: > Masklinn wrote: > >> #each is simply a method that takes a function (called blocks in >> ruby). One could call it a higher-order method I guess. >> It's an implementation of the concept of internal iteration: >> instead of collections yielding iterator objects, and programmers >> using those through specially-built iteration constructs (e.g. `for? >> in`), collections control iteration over themselves (the iteration >> is performed "inside" the collection, thus the "internal" part) and >> the programmer provides the operations to perform at each iterative >> step through (usually) a function. > > Python's iterator protocol was developed in part to avoid the > (inside-out) callback style of programming. Writing virtual > collections as generator functions instead of iterator or iterable > classes saves a lot of boilerplate code. The itertools modules shows > how nicely iterators can be composed in a way that is much more > awkward with callbacks. > >> In Python (assuming we had anonymous defs and an each method on >> lists), the following loop: >> for item in some_list: >> do_something(item) >> do_something_else(item) >> some_list.each((def (item): >> do_something(item) >> do_something_else(item) >> )) > > And how does Ruby do the equivalent of > > def double(it): > for i in it: > yield 2*i > > for i,j in zip(double(some_list), some_gen_func(args)): > print(do_something(i+j,i-j)) Somethign along the lines of some_list.map{|e| 2*e}.zip(some_gen_func(args)).each {|i, j| puts(do_something(i+j, i-j)) } The `#each` call after `#zip` is not actually necessary, but I find it clearer. Oh, and some_gen_func and do_something probably wouldn't work that way (as I said above, Ruby isn't big on named functions and doesn't actually have them) From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 14:17:05 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 18:17:05 GMT Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <008eef29$0$9756$c3e8da3@news.astraweb.com> Message-ID: <008f104b$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 18:15:15 +0200, Masklinn wrote: > > I know, I know, Ruby people swear by > > anonymous code blocks, and I've read Paul Graham too. But I'm really > > not so sure that the benefits of anonymous code blocks are great > > enough to overcome the disadvantages of anonymous code blocks. > > > What are the disadvantages of anonymous functions? In no particular order, and not necessarily exhaustive: * The risk of obfuscation in your code. That's fairly minimal for lambdas, because they're just a single expression, but for a large anonymous code block (ACB) defined inside a named function, it may be difficult for the reader to easily distinguish which bits are the outer function and which are the ACB. * Loss of useful debugging information. Take this example from Python: >>> def main(f): ... return f(3) ... >>> main(lambda n: 2/(n-3)) Traceback (most recent call last): File "", line 1, in File "", line 2, in main File "", line 1, in ZeroDivisionError: integer division or modulo by zero >>> >>> def my_special_function(n): ... return 2/(n-3) ... >>> main(my_special_function) Traceback (most recent call last): File "", line 1, in File "", line 2, in main File "", line 2, in my_special_function ZeroDivisionError: integer division or modulo by zero If your code has only one anonymous function (whether a lambda or a full multi-line block), then it's easy to identify which lambda raised the exception: there is only one it could be. But if your code uses lots of lambdas, the lack of a function name makes it hard to distinguish one from another . Anonymity makes identification harder. * Risk of code-duplication and breaking the principle of Once And Only Once. Anonymous functions are generally created, used, then immediately thrown away -- or at least made more-or-less inaccessible for reuse. An anonymous function stored in a callback still exists, but the coder isn't able to easily re-use it for another callback somewhere else in the code. Consequently, there's a temptation for the coder to write the same function multiple times: add_button("Parrot", colour=blue, callback=lambda x: x.stuff('a')) add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) add_button("Canary", colour=yellow, callback=lambda x: x.stuff('a')) instead of: def bird_callback(x): return x.stuff('a') add_button("Parrot", colour=blue, callback=bird_callback) add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) add_button("Canary", colour=yellow, callback=bird_callback) * Recursion is more or less impossible without fragile tricks. (At least for Python. I don't know how recursion operates in Ruby.) -- Steven From dciphercomputing at gmail.com Fri Jul 31 14:22:26 2009 From: dciphercomputing at gmail.com (Simon) Date: Fri, 31 Jul 2009 11:22:26 -0700 (PDT) Subject: Newbie Question regarding __init__() Message-ID: Hi I want to create an instance of dcCursor which inherits from dcObject. When I run the following code it gives the error shown. Can some explain to me what is wrong? I have included the dcObject.py and dcCursor.py below. >>>import dcObject >>> import dcCursor >>> x = dcCursor.dcCursor() Traceback (most recent call last): File "", line 1, in TypeError: __init__() takes no arguments (1 given) # -*- coding: utf-8 -*- # dcCursor.py - The base Cursor Class from dcObject import dcObject class dcCursor(dcObject): """The base Cursor Class""" def OpenCursor(tcTemp,tcTable): """Opens the specified cursor Parameters: tcTemp,tcTable""" pass def CreateCursor(tcTable): """Creates the specified cursor Parameters: tcTable""" pass def init_Exec(): print("init_Exec called") # -*- coding: utf-8 -*- # dcObject.py - Base Object class dcObject(object): """Base Object""" def __init__(): """default init method has the form of init_Pre() and init_Exec init_Post()""" self.init_Pre() and self.init_Exec() self.init_Post() def init_Pre(): """Always called before init_Exec() if it returns false init_Exec is not executed""" return True def init_Exec(): """Base __init__ code goes here and this is only executed if init_Pre() returns true""" return True def init_Post(): """Always called after the init_Pre() and init_Exec()""" return True From nobody at nowhere.com Fri Jul 31 14:22:33 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 31 Jul 2009 19:22:33 +0100 Subject: Printing with colors in a portable way References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: On Thu, 30 Jul 2009 15:40:37 -0700, Robert Dailey wrote: > Anyone know of a way to print text in Python 3.1 with colors in a > portable way? In other words, I should be able to do something like > this: > > print_color( "This is my text", COLOR_BLUE ) > > And this should be portable (i.e. it should work on Linux, Mac, > Windows). The way that terminals (and emulators) handle colour is fundamentally different from the DOS/Windows console. If you want something which will work on both, you will have write separate implementations for terminals and the DOS/Windows console. For terminals, you can use the "curses" package, e.g.: import curses curses.setupterm() setaf = curses.tigetstr('setaf') setab = curses.tigetstr('setab') def foreground(num): if setaf: sys.stdout.write(curses.tparm(setaf, num)) def background(num): if setab: sys.stdout.write(curses.tparm(setab, num)) For the Windows console, you'll need to use ctypes to interface to the SetConsoleTextAttribute() function from Kernel32.dll. From python at mrabarnett.plus.com Fri Jul 31 14:33:57 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 19:33:57 +0100 Subject: Newbie Question regarding __init__() In-Reply-To: References: Message-ID: <4A733915.9020203@mrabarnett.plus.com> Simon wrote: > Hi > > I want to create an instance of dcCursor which inherits from > dcObject. When I run the following code it gives the error shown. > Can some explain to me what is wrong? I have included the dcObject.py > and dcCursor.py below. > >>>> import dcObject >>>> import dcCursor >>>> x = dcCursor.dcCursor() > Traceback (most recent call last): > File "", line 1, in > TypeError: __init__() takes no arguments (1 given) > All instance methods must have the instance as the first argument, by convention called 'self': > # -*- coding: utf-8 -*- > # dcCursor.py - The base Cursor Class > > from dcObject import dcObject > > class dcCursor(dcObject): > """The base Cursor Class""" > def OpenCursor(tcTemp,tcTable): def OpenCursor(self, tcTemp, tcTable): > """Opens the specified cursor > Parameters: tcTemp,tcTable""" > pass > > def CreateCursor(tcTable): def CreateCursor(self, tcTable): ... and so on, including the '__init__' method. From emmanuel.surleau at gmail.com Fri Jul 31 14:41:12 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 20:41:12 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <200907312041.12241.emmanuel.surleau@gmail.com> On Friday 31 July 2009 18:54:23 Tim Rowe wrote: > 2009/7/31 Steven D'Aprano : > > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > >> That and the fact that I couldn't stop laughing for long enough to learn > >> any more when I read in the Pragmatic Programmer's Guide that "Ruby, > >> unlike less flexible languages, lets you alter the value of a constant." > >> Yep, as they say "Bug" = "Undocumented feature"! > > > > That's no different from Python's "constant by convention". We don't even > > get a compiler warning! > > We don't actually *declare* that something is constant and then have > that declaration ignored. Python doesn't lie to us, although (as in > any language) a programmer might. You could say that Ruby doesn't either, you just need to read the documentation. Ruby's unwritten motto is "flexibility ?ber alles". In this regard, it is consistent (1). Not much is really bolted down in Ruby. You get encapsulation, but it's so easy to break that it's mostly symbolic. It's a language which gives great power to the programmer. Whether Ruby programmers handle this power responsibly is another debate. Cheers, Emm (1) I find Ruby to be pretty consistent in general, which is not always the case of Python. From emmanuel.surleau at gmail.com Fri Jul 31 14:48:03 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 20:48:03 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> Message-ID: <200907312048.03922.emmanuel.surleau@gmail.com> On Friday 31 July 2009 19:49:04 Raymond Hettinger wrote: > On Jul 20, 9:27 am, Phillip B Oldham wrote: > > Specifically the "differences" between lists and tuples have us > > confused and have caused many "discussions" in the office. We > > understand that lists are mutable and tuples are not, but we're a > > little lost as to why the two were kept separate from the start. They > > both perform a very similar job as far as we can tell. > > The underlying C code for the two is substantially the same. In terms > of code, tuples are in effect just immutable lists that have the > added > feature of being hashable (and therefore usable as dictionary keys or > elements of sets). > > Beyond the mutable/hashable distinction, there is an important > philosophical distinction articulated by Guido. He deems tuples > to be useful for struct like groupings of non-homogenous fields > and lists to be useful for sequences of homogenous data suitable > for looping. > > While nothing in the list/tuple code requires you to make that > distinction, > it is important because that philosophy pervades the language. If you > follow Guido's direction, you'll find that the various parts of the > language fit together better. Do otherwise and you'll be going > against > the grain. I might be wrong, but I get the impression that many people don't indeed "get it" and use tuples as static arrays and lists when the array needs to be dynamically resized. This is certainly how I use them as well. This would tend to show that Guido's notion here was not particularly intuitive. Cheers, Emm From __peter__ at web.de Fri Jul 31 14:56:15 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 20:56:15 +0200 Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: Brandon Fredericks wrote: > I did a search within this group, but couldn't find any information on > this. > > I am sending base64 encoded data as the content over http using > urllib2 urlopen. When I receive the data and attempt to decode it, I > get an "Incorrect Padding" error. Is there a simple way to fix this? A > better way to send and receive the data possibly (using base64 of > course)? Have the server send some known data. Read it with urllib2.urlopen(). Compare with that same data that you encoded locally. What are the differences? Peter From masklinn at masklinn.net Fri Jul 31 15:24:33 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 21:24:33 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <008f104b$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <008eef29$0$9756$c3e8da3@news.astraweb.com> <008f104b$0$9756$c3e8da3@news.astraweb.com> Message-ID: On 31 Jul 2009, at 20:17 , Steven D'Aprano wrote: > On Fri, 31 Jul 2009 18:15:15 +0200, Masklinn wrote: > >>> I know, I know, Ruby people swear by >>> anonymous code blocks, and I've read Paul Graham too. But I'm really >>> not so sure that the benefits of anonymous code blocks are great >>> enough to overcome the disadvantages of anonymous code blocks. >>> >> What are the disadvantages of anonymous functions? > > In no particular order, and not necessarily exhaustive: > > * The risk of obfuscation in your code. That's fairly minimal for > lambdas, because they're just a single expression, but for a large > anonymous code block (ACB) defined inside a named function, it may be > difficult for the reader to easily distinguish which bits are the > outer > function and which are the ACB. > I believe that one's unadulterated BS. > * Loss of useful debugging information. Take this example from Python: > >>>> def main(f): > ... return f(3) > ... >>>> main(lambda n: 2/(n-3)) > Traceback (most recent call last): > File "", line 1, in > File "", line 2, in main > File "", line 1, in > ZeroDivisionError: integer division or modulo by zero >>>> >>>> def my_special_function(n): > ... return 2/(n-3) > ... >>>> main(my_special_function) > Traceback (most recent call last): > File "", line 1, in > File "", line 2, in main > File "", line 2, in my_special_function > ZeroDivisionError: integer division or modulo by zero > > If your code has only one anonymous function (whether a lambda or a > full > multi-line block), then it's easy to identify which lambda raised the > exception: there is only one it could be. But if your code uses lots > of > lambdas, the lack of a function name makes it hard to distinguish one > from another . Anonymity makes identification harder. > The traceback gives you the line of the anonymous function (even in python) so unless you have several anonymous functions on the same line, there's no reason why that would be much of an issue. Furthermore, Python doesn't provide any more information when the error happens out of a function (in a `for` or a `with`), so it's not like there's much of a difference here between Ruby's block-based approach and Python's statements-based approach. > * Risk of code-duplication and breaking the principle of Once And Only > Once. Anonymous functions are generally created, used, then > immediately > thrown away -- or at least made more-or-less inaccessible for reuse. > An > anonymous function stored in a callback still exists, but the coder > isn't > able to easily re-use it for another callback somewhere else in the > code. > Consequently, there's a temptation for the coder to write the same > function multiple times: > > add_button("Parrot", colour=blue, callback=lambda x: x.stuff('a')) > add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) > add_button("Canary", colour=yellow, callback=lambda x: x.stuff('a')) > > instead of: > > def bird_callback(x): > return x.stuff('a') > > add_button("Parrot", colour=blue, callback=bird_callback) > add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) > add_button("Canary", colour=yellow, callback=bird_callback) > Yes, that one I can give you though I don't think that's a big issue. And it's not like it's hard to extract the anonymous function into a named one and then use that on the third strike, so I really don't believe that point holds much water. > * Recursion is more or less impossible without fragile tricks. > > (At least for Python. I don't know how recursion operates in Ruby.) Code blocks are rarely if ever used recursively. If an operation is using anonymous functions recursively, then there's often something very wrong with the idea leading to that code. So I find this objection irrelevant. From masklinn at masklinn.net Fri Jul 31 15:31:21 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 21:31:21 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <200907312048.03922.emmanuel.surleau@gmail.com> References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> <200907312048.03922.emmanuel.surleau@gmail.com> Message-ID: <3F279D7F-68C2-4523-B01D-7576F3DFAF3C@masklinn.net> On 31 Jul 2009, at 20:48 , Emmanuel Surleau wrote: > On Friday 31 July 2009 19:49:04 Raymond Hettinger wrote: >> On Jul 20, 9:27 am, Phillip B Oldham >> wrote: >>> Specifically the "differences" between lists and tuples have us >>> confused and have caused many "discussions" in the office. We >>> understand that lists are mutable and tuples are not, but we're a >>> little lost as to why the two were kept separate from the start. >>> They >>> both perform a very similar job as far as we can tell. >> >> The underlying C code for the two is substantially the same. In >> terms >> of code, tuples are in effect just immutable lists that have the >> added >> feature of being hashable (and therefore usable as dictionary keys or >> elements of sets). >> >> Beyond the mutable/hashable distinction, there is an important >> philosophical distinction articulated by Guido. He deems tuples >> to be useful for struct like groupings of non-homogenous fields >> and lists to be useful for sequences of homogenous data suitable >> for looping. >> >> While nothing in the list/tuple code requires you to make that >> distinction, >> it is important because that philosophy pervades the language. If >> you >> follow Guido's direction, you'll find that the various parts of the >> language fit together better. Do otherwise and you'll be going >> against >> the grain. > > I might be wrong, but I get the impression that many people don't > indeed "get > it" and use tuples as static arrays and lists when the array needs > to be > dynamically resized. This is certainly how I use them as well. > > This would tend to show that Guido's notion here was not particularly > intuitive. It's intuitive if you come to Python knowing other languages with tuples (which are mostly functional, and in which tuples are *never* sequences/iterables). At the end of the day, and if Guido's intention truly was what Raymond says, implementing tuples as immutable sequence was a mistake. And the documentation is to blame as well: in it, tuples are clearly described as a *sequence* type, not a *structure* type. From jabronson at gmail.com Fri Jul 31 15:38:01 2009 From: jabronson at gmail.com (Joshua Bronson) Date: Fri, 31 Jul 2009 12:38:01 -0700 (PDT) Subject: heapq "key" arguments References: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> <1582f82e-34aa-4b66-b989-c9f8351a5f1c@o9g2000prg.googlegroups.com> Message-ID: <4f096546-36a1-4633-9a24-1265ce0929ce@q14g2000vbi.googlegroups.com> On Jul 31, 2:02?pm, Jonathan Gardner wrote: > On Jul 31, 10:44?am, Joshua Bronson wrote: > > > Say I want to maintain a heap of (x, y) pairs sorted only by > > first coordinate. Without being able to pass key=itemgetter(0), won't > > heapifying a list of such pairs unnecessarily compare both > > coordinates? > > It will compare the second value only if the first values are equal. I don't see how that helps. That's not at all the same thing as being able to pass key=itemgetter(0). From gagsl-py2 at yahoo.com.ar Fri Jul 31 15:45:53 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 16:45:53 -0300 Subject: class or thread id count References: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> <68b09353-7e30-4df2-8e05-32159b03414a@f20g2000prn.googlegroups.com> Message-ID: En Thu, 30 Jul 2009 17:09:36 -0300, NighterNet escribi?: > On Jul 30, 12:14?pm, r wrote: >> On Jul 30, 1:13?pm, NighterNet wrote: >> >> > Need some help on doing some bit simple id count. It like every time >> > it create a class orthreadthere is id++. Python 3.1 >> >> use a class atrribute > > class Person(): > count = 0 > def __init__(self, name, info): > self.name = name > self.info = info > Person.count+=1 > self.count = Person.count Since you menctioned "thread" in the subject, note that the above isn't thread-safe. You'll need a lock around the last two statements. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 15:46:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 16:46:45 -0300 Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> <7ddvp0F2brmgrU1@mid.uni-berlin.de> <33b7685b-054a-442c-8e97-46a5b77f7900@z4g2000prh.googlegroups.com> Message-ID: En Thu, 30 Jul 2009 13:31:33 -0300, KB escribi?: > On Jul 30, 9:23?am, "Diez B. Roggisch" wrote: >> KB wrote: >> >> > From the HTTPCookieProcessor doco, it appears that non-IE browsers >> > have a cookie file (and example code) but from what I can tell IE uses >> > a hidden folder. (you can set your location in IE but it appends a >> > folder "\Temporary Internet Files" ?- >> >> > Not sure how to adapt this for IE. For IE you may use the pywin32 package: py> import win32inet py> win32inet.InternetGetCookie("http://sldm/", None) '__ac_name="softlab"' Or use ctypes to call the function of the same name in wininet.dll; see http://msdn.microsoft.com/en-us/library/aa384710(VS.85).aspx -- Gabriel Genellina From tjreedy at udel.edu Fri Jul 31 15:55:11 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 15:55:11 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <200907312048.03922.emmanuel.surleau@gmail.com> References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> <200907312048.03922.emmanuel.surleau@gmail.com> Message-ID: Emmanuel Surleau wrote: > >> Beyond the mutable/hashable distinction, there is an important >> philosophical distinction articulated by Guido. He deems tuples to >> be useful for struct like groupings of non-homogenous fields and >> lists to be useful for sequences of homogenous data suitable for >> looping. I think the use of the with 'homegeneous' in this context is wrong because 1) it is ambiguous or even sometimes not the case, and 2) it misleads. Everything is an object, so all collections are in that sense homogeneous. If the meaning is restricted to type(ob), than a list 'mixing' ints and floats would be non-homogeneous and not 'legitimate', but Guido does not really mean that. The word tuple comes from relational databases as a generalization of single, double, triple, quadruple, quintuple, sextuple, sestuple, octuple, etc. A tuple is a data record with a fixed number of fields with individual meaning. There is nothing about homogeneity of data type in that definition. A triple of floats is legitimately a tuple when each is a coordinate (the individual meanings). In other contexts, the same triple might properly be a list (such as of heights of people arbitrarily ordered). >> While nothing in the list/tuple code requires you to make that >> distinction, which needs to be properly articulated... >> it is important because that philosophy pervades the language. If >> you follow Guido's direction, you'll find that the various parts of >> the language fit together better. Do otherwise and you'll be going >> against the grain. Understanding the idea certainly helps understanding the design. For instance, it explains why no tuple comprehensions. > I might be wrong, but I get the impression that many people don't > indeed "get it" and use tuples as static arrays and lists when the > array needs to be dynamically resized. This is certainly how I use > them as well. You also *need* lists if you want to dynamically re-arrange or replace. If you *must not* re-arrange or edit, then a tuple will prevent that. I would also refer back to the idea of tuple as database record. But a list is needed to edit without copying, so lists can be used for writable database records. I believe tuple(iterable) for indefinite-length iterables is somewhat equivalent to tuple(list(iterable)), so there is usually no point to constructing a tuple that way from such an input. For collections defined by constants, there is a real advantage to using tuples, since tuple constants are compiled when the bytecode is compiled, rather than being dynamically constructed. Inside an inner loop, this might make enough time difference that "Practicality beats purity" would apply. > This would tend to show that Guido's notion here was not particularly > intuitive. The problem is that it *is* intuitive, on his part, and usually not well explained rationally. Terry Jan Reedy From python at mrabarnett.plus.com Fri Jul 31 16:03:53 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 21:03:53 +0100 Subject: base64 Incorrect Padding In-Reply-To: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: <4A734E29.9080708@mrabarnett.plus.com> Brandon Fredericks wrote: > I did a search within this group, but couldn't find any information on > this. > > I am sending base64 encoded data as the content over http using > urllib2 urlopen. When I receive the data and attempt to decode it, I > get an "Incorrect Padding" error. Is there a simple way to fix this? A > better way to send and receive the data possibly (using base64 of > course)? Base-64 encodes 3 bytes (echo in the range 0-255) to 4 bytes (each in a more restricted range). The length of the output is _always_ a multiple of 4 (ignoring whitespace); the method adds extra padding bytes (ASCII '=') to ensure that this is the case if the length of the input isn't a multiple of 3. So, if decoding raises an "Incorrect Padding" error then strip out any whitespace and append extra ASCII '=' to make its length a multiple of 3, then try decoding again. (Or you could just repeatedly add one pad character and retry, up to 3 times.) From no.email at please.post Fri Jul 31 16:10:45 2009 From: no.email at please.post (kj) Date: Fri, 31 Jul 2009 20:10:45 +0000 (UTC) Subject: Python docs disappointing Message-ID: I'm pretty new to Python, and I like a lot overall, but I find the documentation for Python rather poor, overall. I'm sure that Python experts don't have this problem: they have internalized some good ways to access the documentation, are productive with it, and therefore have lost the ability to see why the Python documentations is deficient for beginners. This explains why a suboptimal situation can persist like this: those who are most able fix it are also the least able to perceive it. I've heard similar complaints from other experienced programmers who are trying out Python for the first time: poor documentation. Here is an *entirely typical* example: on some Unix, try % pydoc urllib The displayed documentation mention the optional parameter "data" in practically every function listed (a few dozen of them). This parameter is not documented *anywhere* on that page. All that we are told is that its default value is always None. I'm sure that I can find a full description of this parameter if I fire up Google, and search online. In fact, more likely than not, I'll find far more documentation than I want. But my point is that a programmer should not need to do this. The full documentation should be readily accessible directly through a few keystrokes. I would love to know how experienced Python programmers quickly zero in on the Python documentation they need. TIA! kynn From jstroud at mbi.ucla.edu Fri Jul 31 16:11:49 2009 From: jstroud at mbi.ucla.edu (James Stroud) Date: Fri, 31 Jul 2009 13:11:49 -0700 Subject: Python quirk in evaluation order Message-ID: Python 2.5: mbi136-176 211% python *** Pasting of code with ">>>" or "..." has been enabled. ######################################################################## ## ipython ## ######################################################################## py> b = 4 if True else b py> b 4 Isn't the right side supposed to be evaluated first? From emmanuel.surleau at gmail.com Fri Jul 31 16:26:58 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 22:26:58 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: <200907312048.03922.emmanuel.surleau@gmail.com> Message-ID: <200907312226.59027.emmanuel.surleau@gmail.com> On Friday 31 July 2009 21:55:11 Terry Reedy wrote: > Emmanuel Surleau wrote: > >> Beyond the mutable/hashable distinction, there is an important > >> philosophical distinction articulated by Guido. He deems tuples to > >> be useful for struct like groupings of non-homogenous fields and > >> lists to be useful for sequences of homogenous data suitable for > >> looping. > > I think the use of the with 'homegeneous' in this context is wrong > because 1) it is ambiguous or even sometimes not the case, and 2) it > misleads. Everything is an object, so all collections are in that sense > homogeneous. If the meaning is restricted to type(ob), than a list > 'mixing' ints and floats would be non-homogeneous and not 'legitimate', > but Guido does not really mean that. > > The word tuple comes from relational databases as a generalization of > single, double, triple, quadruple, quintuple, sextuple, sestuple, > octuple, etc. A tuple is a data record with a fixed number of fields > with individual meaning. There is nothing about homogeneity of data type > in that definition. A triple of floats is legitimately a tuple when each > is a coordinate (the individual meanings). In other contexts, the same > triple might properly be a list (such as of heights of people > arbitrarily ordered). My understanding is that, in this context, it's not so much data types which are heterogeneous, but the semantic meaning of the data. For instance, a tuple containing (first_name, last_name, address) would be a "legitimate" tuple, but not a tuple containing (address, address, address), which, if we follow Guido's philosophy, ought to be represented as a list. Whether including the distinction in the language offers any practical benefit is questionable. [snip] > > This would tend to show that Guido's notion here was not particularly > > intuitive. > > The problem is that it *is* intuitive, on his part, and usually not well > explained rationally. Excellent point. Cheers, Emm From robert.kern at gmail.com Fri Jul 31 16:28:01 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 31 Jul 2009 15:28:01 -0500 Subject: Python docs disappointing In-Reply-To: References: Message-ID: On 2009-07-31 15:10, kj wrote: > I would love to know how experienced Python programmers quickly > zero in on the Python documentation they need. http://docs.python.org/library/urllib I use Firefox's "Quick Searches" feature to make getting this URL as fast as possible: "m urllib" -- 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 rt8396 at gmail.com Fri Jul 31 16:29:36 2009 From: rt8396 at gmail.com (r) Date: Fri, 31 Jul 2009 13:29:36 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: On Jul 31, 3:10?pm, kj wrote: > I'm pretty new to Python, and I like a lot overall, but I find the > documentation for Python rather poor, overall. [snip] If you mean the built-in docs i *highly agree* with you. if you mean docs/tutorials available across the WWW i *highly disagree* with you, and if you mean the command line "help" i also *highly disagree*, of which python has the-best-in the-world! I am using 2.6.2 and the help docs are a joke. trying to find even the simplest thing (built-in functions, etc...) is like pulling teeth. I do not look at them anymore, or do i plan to in the future. Luckily i know python pretty well, but i can't image the horrors a new Python Programmer must face now... Kj, you could not be more right. Doc maintainers need to get some feedback from Python noobies when writing this stuff. Does anybody remember CP4E? I know Guido put a lot of effort into this project and very much believed in this philosophy(circa 1999~2005), although it never really went very far, i still believe in Guido's heart the CP4E exists -- if only barley clinging to life(...maybe?) any thoughts on this Pythonistas? From robert.kern at gmail.com Fri Jul 31 16:31:21 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 31 Jul 2009 15:31:21 -0500 Subject: Python quirk in evaluation order In-Reply-To: References: Message-ID: On 2009-07-31 15:11, James Stroud wrote: > Python 2.5: > > mbi136-176 211% python > *** Pasting of code with ">>>" or "..." has been enabled. > ######################################################################## > ## ipython ## > ######################################################################## > py> b = 4 if True else b > py> b > 4 > > > Isn't the right side supposed to be evaluated first? The right side of the assignment or the if-else expression? The "expr1 if condition else expr2" expression evaluates condition, then either expr1 or expr2 depending on the result. The other expression is unevaluated. >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> b Traceback (most recent call last): File "", line 1, in NameError: name 'b' is not defined >>> 5 if True else b 5 >>> a if False else 5 5 -- 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 marduk at letterboxes.org Fri Jul 31 16:33:00 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Fri, 31 Jul 2009 16:33:00 -0400 Subject: Python quirk in evaluation order In-Reply-To: References: Message-ID: <1249072381.24514.4.camel@centar> On Fri, 2009-07-31 at 13:11 -0700, James Stroud wrote: > Python 2.5: > > mbi136-176 211% python > *** Pasting of code with ">>>" or "..." has been enabled. > ######################################################################## > ## ipython ## > ######################################################################## > py> b = 4 if True else b > py> b > 4 > > > Isn't the right side supposed to be evaluated first? Yes, the right hand side of the '=', which is evaluated from LtoR. >From the Language Reference: Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. So the right-hand side of the '=' is evaluated first: 4 if True else b And that's evaluated from left to right: 4 Therefore: b = 4 From emmanuel.surleau at gmail.com Fri Jul 31 16:34:06 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 22:34:06 +0200 Subject: Python docs disappointing In-Reply-To: References: Message-ID: <200907312234.06374.emmanuel.surleau@gmail.com> On Friday 31 July 2009 22:10:45 kj wrote: > I'm pretty new to Python, and I like a lot overall, but I find the > documentation for Python rather poor, overall. > > I'm sure that Python experts don't have this problem: they have > internalized some good ways to access the documentation, are > productive with it, and therefore have lost the ability to see why > the Python documentations is deficient for beginners. This explains > why a suboptimal situation can persist like this: those who are > most able fix it are also the least able to perceive it. > > I've heard similar complaints from other experienced programmers > who are trying out Python for the first time: poor documentation. The documentation on python.org is quite extensive. In particular, you may want to look at the PEPs, which offer fascinating insights for the reasoning behind particular features of Python. > Here is an *entirely typical* example: on some Unix, try > > % pydoc urllib > > The displayed documentation mention the optional parameter "data" > in practically every function listed (a few dozen of them). This > parameter is not documented *anywhere* on that page. All that we > are told is that its default value is always None. > > I'm sure that I can find a full description of this parameter if > I fire up Google, and search online. In fact, more likely than > not, I'll find far more documentation than I want. But my point > is that a programmer should not need to do this. The full > documentation should be readily accessible directly through a few > keystrokes. You have first-grade documentation on the Python website: http://docs.python.org/library/urllib.html I'm not really using pydoc, but I'd wager it's more used as a quick lookup than anything else. > I would love to know how experienced Python programmers quickly > zero in on the Python documentation they need. I wouldn't count myself as an 'experienced' Python programmer, but I rely on docs.python.org for most things with regards to the standard library. Cheers, Emm From __peter__ at web.de Fri Jul 31 16:39:25 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 22:39:25 +0200 Subject: Python quirk in evaluation order References: Message-ID: James Stroud wrote: > py> b = 4 if True else b > py> b > 4 > Isn't the right side supposed to be evaluated first? Perhaps it becomes clearer if you change it a bit: >>> b = 4 if True else whatever >>> whatever Traceback (most recent call last): File "", line 1, in NameError: name 'whatever' is not defined I. e. the else clause is never evaluated at all. Peter From breamoreboy at yahoo.co.uk Fri Jul 31 16:55:35 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Jul 2009 21:55:35 +0100 Subject: Python docs disappointing In-Reply-To: References: Message-ID: r wrote: > On Jul 31, 3:10 pm, kj wrote: >> I'm pretty new to Python, and I like a lot overall, but I find the >> documentation for Python rather poor, overall. > [snip] > > If you mean the built-in docs i *highly agree* with you. if you mean > docs/tutorials available across the WWW i *highly disagree* with you, > and if you mean the command line "help" i also *highly disagree*, of > which python has the-best-in the-world! > > I am using 2.6.2 and the help docs are a joke. trying to find even the > simplest thing (built-in functions, etc...) is like pulling teeth. I > do not look at them anymore, or do i plan to in the future. Luckily i > know python pretty well, but i can't image the horrors a new Python > Programmer must face now... > > Kj, you could not be more right. Doc maintainers need to get some > feedback from Python noobies when writing this stuff. Does anybody > remember CP4E? I know Guido put a lot of effort into this project and > very much believed in this philosophy(circa 1999~2005), although it > never really went very far, i still believe in Guido's heart the CP4E > exists -- if only barley clinging to life(...maybe?) > > any thoughts on this Pythonistas? I entirely agree with these comments. Looking at home on the compiled help file on windows for python 2.6.2 I can't see anything except for some icons at the top, the Contents, Index, Search and Favorites tabs on the left and the following on the right. What's new in Python 2.6? or all "What's new" documents since 2.0 Tutorial start here Using Python how to use Python on different platforms Language Reference describes syntax and language elements Library Reference keep this under your pillow Python HOWTOs in-depth documents on specific topics Extending and Embedding tutorial for C/C++ programmers Python/C API reference for C/C++ programmers Installing Python Modules information for installers & sys-admins Distributing Python Modules sharing modules with others Documenting Python guide for documentation authors Indices and tables: Global Module Index quick access to all modules General Index all functions, classes, terms Glossary the most important terms explained Search page search this documentation Complete Table of Contents lists all sections and subsections Meta information: Reporting bugs About the documentation History and License of Python Copyright Apart from that what have the Pythonistas ever done for us? Nothing!:) -- Kindest regards. Mark Lawrence. From nagle at animats.com Fri Jul 31 16:56:25 2009 From: nagle at animats.com (John Nagle) Date: Fri, 31 Jul 2009 13:56:25 -0700 Subject: beautiful soup In-Reply-To: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> References: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> Message-ID: <4a7359ce$0$1629$742ec2ed@news.sonic.net> xubin.cz wrote: > hi, everyone > > Is there any pakage or module handling html document like beautiful > soup? Try "http://code.google.com/p/html5lib/". That's supposed to be a parser which complies with the HTML 5 specification, including its rules for handling bad HTML. John Nagle From maxerickson at gmail.com Fri Jul 31 17:04:10 2009 From: maxerickson at gmail.com (Max Erickson) Date: Fri, 31 Jul 2009 21:04:10 +0000 (UTC) Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> <4A734E29.9080708@mrabarnett.plus.com> Message-ID: MRAB wrote: > Brandon Fredericks wrote: >> I did a search within this group, but couldn't find any >> information on this. >> >> I am sending base64 encoded data as the content over http using >> urllib2 urlopen. When I receive the data and attempt to decode >> it, I get an "Incorrect Padding" error. Is there a simple way to >> fix this? A better way to send and receive the data possibly >> (using base64 of course)? > > Base-64 encodes 3 bytes (echo in the range 0-255) to 4 bytes > (each in a more restricted range). The length of the output is > _always_ a multiple of 4 (ignoring whitespace); the method adds > extra padding bytes (ASCII '=') to ensure that this is the case > if the length of the input isn't a multiple of 3. > > So, if decoding raises an "Incorrect Padding" error then strip > out any whitespace and append extra ASCII '=' to make its length > a multiple of 3, then try decoding again. (Or you could just > repeatedly add one pad character and retry, up to 3 times.) The length of the encoded string should be a multiple of 4 (as you state in the second sentence of your post), not a multiple of 3. max From davea at ieee.org Fri Jul 31 17:07:01 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 31 Jul 2009 17:07:01 -0400 Subject: Python quirk in evaluation order In-Reply-To: References: Message-ID: <4A735CF5.1070304@ieee.org> James Stroud wrote: >
Python 2.5: > > mbi136-176 211% python > *** Pasting of code with ">>>" or "..." has been enabled. > ######################################################################## > ## ipython ## > ######################################################################## > py> b = 4 if True else b > py> b > 4 > > > Isn't the right side supposed to be evaluated first? > I don't have a clue what value you expected b to have. The if/else ternary expression is roughly equivalent to: if True: b = 4 else b = b The first part to be evaluated is the if expression, which is hard-coded to True. Then the part to the left will be used for the expression result, and the part on the right ignored. This is because of short-circuit rules. Try this one for size: b = 22 if True else I.am.Not.Being.Evaluated(4.4) The else clause does need to be a syntactically valid expression, but it's not evaluated unless the if-expression is false. DaveA From pavlovevidence at gmail.com Fri Jul 31 17:16:48 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 14:16:48 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: On Jul 31, 1:10?pm, kj wrote: > I'm pretty new to Python, and I like a lot overall, but I find the > documentation for Python rather poor, overall. > > I'm sure that Python experts don't have this problem: they have > internalized some good ways to access the documentation, are > productive with it, and therefore have lost the ability to see why > the Python documentations is deficient for beginners. That may be so, but I do remember when I was a beginner myself and I had no issue with the documentation. [snip] > I'm sure that I can find a full description of this parameter if > I fire up Google, and search online. Are you aware that Python ships with a full set of documentation, where (for instance) the meaning of "data" in urllib is defined? You don't need Google. > In fact, more likely than > not, I'll find far more documentation than I want. ?But my point > is that a programmer should not need to do this. ?The full > documentation should be readily accessible directly through a few > keystrokes. Well, no one volunteered to do that. Oh well. > I would love to know how experienced Python programmers quickly > zero in on the Python documentation they need. Choose bookmark in web browser for Python documentation -> keyword search for correct module (easy with firefox) -> scroll. This might not be the smoothest method ever (omg you have to use a *mouse*) but it should be usable enough. Carl Banks From python at mrabarnett.plus.com Fri Jul 31 17:17:30 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 22:17:30 +0100 Subject: base64 Incorrect Padding In-Reply-To: References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> <4A734E29.9080708@mrabarnett.plus.com> Message-ID: <4A735F6A.1010405@mrabarnett.plus.com> Max Erickson wrote: > MRAB wrote: > >> Brandon Fredericks wrote: >>> I did a search within this group, but couldn't find any >>> information on this. >>> >>> I am sending base64 encoded data as the content over http using >>> urllib2 urlopen. When I receive the data and attempt to decode >>> it, I get an "Incorrect Padding" error. Is there a simple way to >>> fix this? A better way to send and receive the data possibly >>> (using base64 of course)? >> Base-64 encodes 3 bytes (echo in the range 0-255) to 4 bytes >> (each in a more restricted range). The length of the output is >> _always_ a multiple of 4 (ignoring whitespace); the method adds >> extra padding bytes (ASCII '=') to ensure that this is the case >> if the length of the input isn't a multiple of 3. >> >> So, if decoding raises an "Incorrect Padding" error then strip >> out any whitespace and append extra ASCII '=' to make its length >> a multiple of 3, then try decoding again. (Or you could just >> repeatedly add one pad character and retry, up to 3 times.) > > The length of the encoded string should be a multiple of 4 (as you > state in the second sentence of your post), not a multiple of 3. > Oops, correct! :-) From masklinn at masklinn.net Fri Jul 31 17:19:07 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 23:19:07 +0200 Subject: Python docs disappointing In-Reply-To: <200907312234.06374.emmanuel.surleau@gmail.com> References: <200907312234.06374.emmanuel.surleau@gmail.com> Message-ID: On 31 Jul 2009, at 22:34 , Emmanuel Surleau wrote: > You have first-grade documentation on the Python website: > http://docs.python.org/library/urllib.html > I'm not really using pydoc, but I'd wager it's more used as a quick > lookup > than anything else. Another important documentary resource for the stdlib is Doug Hellmann's awesome Python Module of the Day series (http://www.doughellmann.com/PyMOTW/ ) From pavlovevidence at gmail.com Fri Jul 31 17:23:02 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 14:23:02 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: <7bb4df03-f09a-40b0-83f8-1c6d0c9800a8@j21g2000vbn.googlegroups.com> On Jul 31, 1:55?pm, Mark Lawrence wrote: > Apart from that what have the Pythonistas ever done for us? Nothing!:) Please don't feed the trolls. And if you do feed the trolls don't smile at them. Carl Banks From no.email at please.post Fri Jul 31 17:28:36 2009 From: no.email at please.post (kj) Date: Fri, 31 Jul 2009 21:28:36 +0000 (UTC) Subject: Python docs disappointing References: Message-ID: In Carl Banks writes: >(omg you have to use a >*mouse*) That's precisely the point. There's a huge number of programmers out there who, like me, *hate* to use the mouse while they're coding. It is truly disappointing to us that the developers of Python chose to completely disregard this constituency. This is one area in which Perl still whips Python... kynn From rt8396 at gmail.com Fri Jul 31 17:30:47 2009 From: rt8396 at gmail.com (r) Date: Fri, 31 Jul 2009 14:30:47 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> On Jul 31, 4:16?pm, Carl Banks wrote: > On Jul 31, 1:10?pm, kj wrote: > > > I'm pretty new to Python, and I like a lot overall, but I find the > > documentation for Python rather poor, overall. > > > I'm sure that Python experts don't have this problem: they have > > internalized some good ways to access the documentation, are > > productive with it, and therefore have lost the ability to see why > > the Python documentations is deficient for beginners. > > That may be so, but I do remember when I was a beginner myself and I > had no issue with the documentation. have you tried the new docs (>= 2.6) The interface has changed drastically as to render itself completely useless. The old docs (<= 2.5) --the ones i learned from-- where flawless. @ Mark Lawrence Have you clicked any of those links? try the "Tutorial start here" and then try to find a menu of sorts. It seems you have to click "next" to navigate. Thats pretty counter intuitive if you need to get to page 589!! Upon clicking the tutorial link in the OLD DOCS, you where presented with a nice menu for concise navigation. From python at rcn.com Fri Jul 31 17:43:17 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 14:43:17 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> Message-ID: <4677d753-364f-4d29-9091-e55008daec83@12g2000pri.googlegroups.com> > > While nothing in the list/tuple code requires you to make that > > distinction, > > it is important because that philosophy pervades the language. ?If you > > follow Guido's direction, you'll find that the various parts of the > > language fit together better. ?Do otherwise and you'll be going > > against > > the grain. > > I might be wrong, but I get the impression that many people don't indeed "get > it" and use tuples as static arrays and lists when the array needs to be > dynamically resized. This is certainly how I use them as well. > > This would tend to show that Guido's notion here was not particularly > intuitive. No, it just shows that they are not Dutch ;-) IIRC, Aahz once made an eloquent statement about how languages aren't intuitive because different people have different intuitions. The fact is that every language has its own set of ways. People usually pick- up on what is Pythonic in a short order. The problem comes when people decide-in-advance how the language ought to be -- it takes them longer to pick-up the culture and idioms. More than one person here has observed that the time to learn to program Pythonically is inversely proportional to their experience in Java. It is perfectly okay for you to use tuples for statically sized arrays. That isn't wrong. It is against then grain though. Guido articulated his concept early and many of his minions (me included) have designed the APIs and optimizations to flow nicely with his preferred way of doing things. Raymond From breamoreboy at yahoo.co.uk Fri Jul 31 17:53:02 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Jul 2009 22:53:02 +0100 Subject: Python docs disappointing In-Reply-To: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> References: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> Message-ID: r wrote: > On Jul 31, 4:16 pm, Carl Banks wrote: >> On Jul 31, 1:10 pm, kj wrote: >> >>> I'm pretty new to Python, and I like a lot overall, but I find the >>> documentation for Python rather poor, overall. >>> I'm sure that Python experts don't have this problem: they have >>> internalized some good ways to access the documentation, are >>> productive with it, and therefore have lost the ability to see why >>> the Python documentations is deficient for beginners. >> That may be so, but I do remember when I was a beginner myself and I >> had no issue with the documentation. > > have you tried the new docs (>= 2.6) The interface has changed > drastically as to render itself completely useless. The old docs (<= > 2.5) --the ones i learned from-- where flawless. > > > @ Mark Lawrence > Have you clicked any of those links? try the "Tutorial start here" and > then try to find a menu of sorts. It seems you have to click "next" to > navigate. Thats pretty counter intuitive if you need to get to page > 589!! Upon clicking the tutorial link in the OLD DOCS, you where > presented with a nice menu for concise navigation. Yes. Works perfectly as evidenced by the fact that this evening I've checked data on the cProfile, pstats and array modules. -- Kindest regards. Mark Lawrence. From python at rcn.com Fri Jul 31 17:55:16 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 14:55:16 -0700 (PDT) Subject: Help understanding the decisions *behind* python? - immutable objects References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: <6b343b95-27d0-4167-bef2-d3e0a5fd87dc@u38g2000pro.googlegroups.com> On Jul 26, 11:24?am, John Nagle wrote: > A tuple is really a frozen list. ?Arguably, frozen objects > should have been a general concept. ?Conceptually, they're > simple - once "__init__" has run, there can be no more changes > to fields of the object. I would argue that freezing and thawing (making the container statefull) is the proverbial road to hell paved with good intentions. It is roughly in the same category as dynamic scoping -- programs become harder to analyze for correctness -- code that used to work can break because something froze it along the way (perhaps to use it as a key for dict or set in a caching function) or because something unfroze it (so it could mutate a value). Raymond From brfredericks at gmail.com Fri Jul 31 17:58:43 2009 From: brfredericks at gmail.com (bfrederi) Date: Fri, 31 Jul 2009 14:58:43 -0700 (PDT) Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: So what if I used a different encoding that isn't ASCII? Like UTF-8? Would that give me lengths that are multiples of 4 based on how the characters are represented? Or would I still need to pad with '='? From pavlovevidence at gmail.com Fri Jul 31 18:03:25 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 15:03:25 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> On Jul 31, 2:28?pm, kj wrote: > In Carl Banks writes: > > >(omg you have to use a > >*mouse*) > > That's precisely the point. ?There's a huge number of programmers > out there who, like me, *hate* to use the mouse while they're > coding. You would have figured one of them would have written a script or something if there were so many. Maybe check the Python Cookbook for someone's pet trick, I bet there is one or two there. Or figure out a way to use elinks, the text mode web browser. You know, text mode browsers have come a long way since the lynx days. >?It is truly disappointing to us that the developers of > Python chose to completely disregard this constituency. Python is a volunteer effort, chief. If all these programmers have this itch, as you claim, they've all disregarded themselves by failing to contribute something. > This is one area in which Perl still whips Python... No way. Perl's man pages are organized so poorly there is no ergonomic pit deep enough to offset them. Quick, what man page is the "do" statement documented in? Carl Banks From no.email at please.post Fri Jul 31 18:09:42 2009 From: no.email at please.post (kj) Date: Fri, 31 Jul 2009 22:09:42 +0000 (UTC) Subject: Python docs disappointing References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: In <09bf4f17-40a5-4bad-81d3-1950545b7570 at g6g2000vbr.googlegroups.com> Carl Banks writes: Thanks. Your remarks at least confirm that my impression was not simply due to my noob ignorance: the keyboard-accessible docs are indeed as poor as they look. kynn From pfeldman at verizon.net Fri Jul 31 18:17:56 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 31 Jul 2009 15:17:56 -0700 (PDT) Subject: possible to round number and convert to string? Message-ID: <24763821.post@talk.nabble.com> I'd like to be able to convert a float to a string representation in which the number is rounded to a specified number of digits. If num2str is a hypothetical function that does this, then num2str(pi,3) would be '3.142' (not '3.141'). I've been told that there is no such function in Python. I tried to write this myself, but wasn't successful. Any suggestions will be appreciated. -- View this message in context: http://www.nabble.com/possible-to-round-number-and-convert-to-string--tp24763821p24763821.html Sent from the Python - python-list mailing list archive at Nabble.com. From python at mrabarnett.plus.com Fri Jul 31 18:24:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 23:24:09 +0100 Subject: base64 Incorrect Padding In-Reply-To: References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: <4A736F09.6090505@mrabarnett.plus.com> bfrederi wrote: > So what if I used a different encoding that isn't ASCII? Like UTF-8? > Would that give me lengths that are multiples of 4 based on how the > characters are represented? Or would I still need to pad with '='? I think that when it says Base-64 it's talking about the byte values used for the encoding. From david.birdsong at gmail.com Fri Jul 31 18:27:19 2009 From: david.birdsong at gmail.com (birdsong) Date: Fri, 31 Jul 2009 15:27:19 -0700 (PDT) Subject: Non-blocking read with popen subprocess References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> <824f8092-55b1-4df2-a6b5-74a4b1b1d9ea@i18g2000pro.googlegroups.com> Message-ID: On Jul 30, 7:30?pm, Jonathan Gardner wrote: > On Jul 30, 5:24?pm, Dhanesh wrote: > > > > > how can I we have a non blocking read ? > > Seehttp://docs.python.org/library/popen2.html#flow-control-issues > > Note well: In the non-blocking world, you have to use select() or poll > () to get your job done. I solved a problem just yesterday regarding select on a Popen.stdout file descriptor that was created with a 100Mb buffer. I'd love it if anybody could critique my solution and my understanding of my findings. In my case, select would return with my Popen object ready for read, I'd go off and try to read and then my read would block. After a few strace's I found that Popen.stdout.read(), which I am assuming is the same as the read builtin, would make multiple read system calls, in many cases blocking until it returned my default buffer size. Sure enough, it's in the docs: http://docs.python.org/library/stdtypes.html?highlight=fread """ Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given. """ I couldn't find a way to tell subprocess to return my stdout fd in non- blocking mode, but then I thought about it not being a problem with the fd and how it was opened as much as a problem with the read call. A non-blocking fd will have a read call throw EWOULDBLOCK when asked for more bytes than is in buffer, a blocking fd would have read just return fewer bytes ...do I have this this right? I can handle assembling the data fine, I just don't want to block. I wrote a little test program to spit out random stdout to test a Popen object's stdout in os.read. The len() of the result of os.read was all over the place depending on how quickly i re-ran read() on the fd. There were slight delays from time to time, but I attributed that to the interpreter being context switched in, ssh delays ...or the weather...in general the return was pretty fast. I switched my program to use os.read and now it's snappy and spends most of it's time blocking on select exactly where I want it to. (this is on linux btw, I guess os.read is different everywhere) > You may want to look at "communicate" (http://docs.python.org/library/subprocess.html#popen-objects) which may be what you need. From Nikolaus at rath.org Fri Jul 31 18:38:25 2009 From: Nikolaus at rath.org (Nikolaus Rath) Date: Fri, 31 Jul 2009 18:38:25 -0400 Subject: Python docs disappointing References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: <87prbgscke.fsf@vostro.rath.org> Carl Banks writes: >> This is one area in which Perl still whips Python... > > No way. Perl's man pages are organized so poorly there is no > ergonomic pit deep enough to offset them. Quick, what man page is the > "do" statement documented in? Of course there is: $ perldoc -f do | head do BLOCK Not really a function. Returns the value of the last command in the sequence of commands indicated by BLOCK. When modified by the "while" or "until" loop modifier, executes the BLOCK once before testing the loop condition. (On other statements the loop modifiers test the conditional first.) "do BLOCK" does not count as a loop, so the loop control statements "next", "last", or "redo" cannot be used to leave or restart the block. See perlsyn for alternative strategies. $ Best, -Nikolaus -- ?Time flies like an arrow, fruit flies like a Banana.? PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C From malaclypse2 at gmail.com Fri Jul 31 18:42:03 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 31 Jul 2009 18:42:03 -0400 Subject: possible to round number and convert to string? In-Reply-To: <24763821.post@talk.nabble.com> References: <24763821.post@talk.nabble.com> Message-ID: <16651e80907311542w1a94168egd173641cdfb002ce@mail.gmail.com> On Fri, Jul 31, 2009 at 6:17 PM, Dr. Phillip M. Feldman wrote: > > I'd like to be able to convert a float to a string representation in which > the number is rounded to a specified number of digits. ?If num2str is a > hypothetical function that does this, then num2str(pi,3) would be '3.142' > (not '3.141'). ?I've been told that there is no such function in Python. ?I > tried to write this myself, but wasn't successful. ?Any suggestions will be > appreciated. You should be able to do this with standard string formatting. ( http://docs.python.org/library/stdtypes.html#string-formatting ) For example: >>> from math import pi >>> pi 3.1415926535897931 >>> "%0.3f" % pi '3.142' If you need it as a function, you could do something like this: >>> def num2str(num, precision): return "%0.*f" % (precision, num) >>> num2str(pi, 4) '3.1416' Does that do what you need? -- Jerry From user at example.net Fri Jul 31 18:42:21 2009 From: user at example.net (superpollo) Date: Sat, 01 Aug 2009 00:42:21 +0200 Subject: possible to round number and convert to string? In-Reply-To: References: Message-ID: <4a737350$0$28514$4fafbaef@reader2.news.tin.it> Dr. Phillip M. Feldman wrote: > I'd like to be able to convert a float to a string representation in which > the number is rounded to a specified number of digits. If num2str is a > hypothetical function that does this, then num2str(pi,3) would be '3.142' > (not '3.141'). I've been told that there is no such function in Python. I > tried to write this myself, but wasn't successful. Any suggestions will be > appreciated. >>> import math >>> s = str(round(math.pi, 3)) >>> s '3.142' >>> From gallium.arsenide at gmail.com Fri Jul 31 18:46:22 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 31 Jul 2009 15:46:22 -0700 (PDT) Subject: possible to round number and convert to string? References: Message-ID: <3b719e5e-0cce-4059-86e1-623a0ad7777a@f33g2000vbm.googlegroups.com> On Jul 31, 6:17?pm, "Dr. Phillip M. Feldman" wrote: > I'd like to be able to convert a float to a > string representation in which the number is > rounded to a specified number of digits. ?If > num2str is a hypothetical function that does > this, then num2str(pi,3) would be '3.142' > (not '3.141'). ?I've been told that there is > no such function in Python. ?I tried to write > this myself, but wasn't successful. ?Any > suggestions will be appreciated. This is what I get in Python 2.6.1 on Windows: >>> from math import pi >>> pi 3.1415926535897931 >>> round(pi, 3) 3.1419999999999999 >>> str(round(pi, 3)) '3.142' John From cjns1989 at gmail.com Fri Jul 31 18:54:52 2009 From: cjns1989 at gmail.com (Chris Jones) Date: Fri, 31 Jul 2009 18:54:52 -0400 Subject: Python docs disappointing In-Reply-To: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: <20090731225452.GK25977@turki.gavron.org> On Fri, Jul 31, 2009 at 06:03:25PM EDT, Carl Banks wrote: > On Jul 31, 2:28?pm, kj wrote: > > In Carl Banks writes: > > > > >(omg you have to use a > > >*mouse*) > > > > That's precisely the point. ?There's a huge number of programmers > > out there who, like me, *hate* to use the mouse while they're > > coding. > > You would have figured one of them would have written a script or > something if there were so many. Maybe check the Python Cookbook for > someone's pet trick, I bet there is one or two there. Or figure out a > way to use elinks, the text mode web browser. You know, text mode > browsers have come a long way since the lynx days. So true.. I would add that one of the unplanned merits of a well-thought out text mode browser such as ELinks is to remove all the fluff and leave you, the reader, face to face with the site's content - or lack thereof. CJ From pavlovevidence at gmail.com Fri Jul 31 18:55:19 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 15:55:19 -0700 (PDT) Subject: Python docs disappointing References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: <109f1ff7-8fa9-40d3-80b4-1e90b5fc669c@d34g2000vbm.googlegroups.com> On Jul 31, 3:09?pm, kj wrote: > In <09bf4f17-40a5-4bad-81d3-1950545b7... at g6g2000vbr.googlegroups.com> > > Carl Banks writes: > > > > Thanks. ?Your remarks at least confirm that my impression was not > simply due to my noob ignorance: the keyboard-accessible docs are > indeed as poor as they look. In the standard library, docstrings (which is what pydoc prints) are intended to be a brief explanation/reminder of usage, not an exhaustive description. Where docstrings are present, they are more or less adequate for their intended use (although the quality of docstrings does vary highly throughout the library). If you are trying to use pydoc in a way not intended, then yes you would likely find that they are poor when used in that unintended way. Docstrings take up memory in a running process, so they are not ever likely to be converted to an exhaustive description. Your best bet is to figure out a way to automate lookup in the html documentation. Carl Banks From aahz at pythoncraft.com Fri Jul 31 19:09:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 31 Jul 2009 16:09:16 -0700 Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: In article <062813eb-7eec-4504-b32c-abadf02c3e38 at 12g2000pri.googlegroups.com>, dandi kain wrote: > >What is the functionality of __ or _ , leading or trailing an object , >class ot function ? Is it just a naming convention to note special >functions and objects , or it really mean someting to Python ? One more thing: if you have global module names with a single leading underscore (e.g. "_foo"), they will not be loaded when you use from import * However, "import *" is strongly discouraged for the most part. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From jeremy.cowles at gmail.com Fri Jul 31 19:57:23 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Fri, 31 Jul 2009 16:57:23 -0700 Subject: Bug with Python, Cygwin and /dev/urandom? Message-ID: <373cf0740907311657j3e65d94mbbb2eda9e0f94fb7@mail.gmail.com> urllib2.py is crashes when calling randombytes(n). I run the following under Cygwin (mimicking how randombytes works): $ python Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14) [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import sys,os >>> os.path.exists("/dev/urandom") True >>> f = open("/dev/urandom") >>> s = f.read(2) >>> f.close() Traceback (most recent call last): File "", line 1, in IOError: [Errno 0] Error Errno 0 is supposed to be impossible according to the following thread. Apparently this same issue also causes Mercurial to crash: http://www.nabble.com/hg-1.0-exits-with-abort:-Error-td19021833.html Is this a bug? -- Jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: From rt8396 at gmail.com Fri Jul 31 20:00:28 2009 From: rt8396 at gmail.com (r) Date: Fri, 31 Jul 2009 17:00:28 -0700 (PDT) Subject: Python docs disappointing References: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> Message-ID: On Jul 31, 4:53?pm, Mark Lawrence wrote: > r wrote: > > On Jul 31, 4:16 pm, Carl Banks wrote: > >> On Jul 31, 1:10 pm, kj wrote: > > >>> I'm pretty new to Python, and I like a lot overall, but I find the > >>> documentation for Python rather poor, overall. > >>> I'm sure that Python experts don't have this problem: they have > >>> internalized some good ways to access the documentation, are > >>> productive with it, and therefore have lost the ability to see why > >>> the Python documentations is deficient for beginners. > >> That may be so, but I do remember when I was a beginner myself and I > >> had no issue with the documentation. > > > have you tried the new docs (>= 2.6) The interface has changed > > drastically as to render itself completely useless. The old docs (<= > > 2.5) --the ones i learned from-- where flawless. > > > @ Mark Lawrence > > Have you clicked any of those links? try the "Tutorial start here" and > > then try to find a menu of sorts. It seems you have to click "next" to > > navigate. Thats pretty counter intuitive if you need to get to page > > 589!! Upon clicking the tutorial link in the OLD DOCS, you where > > presented with a nice menu for concise navigation. > > Yes. ?Works perfectly as evidenced by the fact that this evening I've > checked data on the cProfile, pstats and array modules. > > -- > Kindest regards. > > Mark Lawrence. Hold the phone... You checked data on modules using the "Tutorial Start Here" link? Would not the "Global Module Index" be more, shall we say, "informative"? From kee at kagi.com Fri Jul 31 20:12:43 2009 From: kee at kagi.com (Kee Nethery) Date: Fri, 31 Jul 2009 17:12:43 -0700 Subject: Python docs disappointing - group effort to hire writers? In-Reply-To: <109f1ff7-8fa9-40d3-80b4-1e90b5fc669c@d34g2000vbm.googlegroups.com> References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> <109f1ff7-8fa9-40d3-80b4-1e90b5fc669c@d34g2000vbm.googlegroups.com> Message-ID: <756580E3-CFFA-404C-B66A-9F4281760C8E@kagi.com> I too find the Python docs not very useful and it really slows down my learning curve. I wonder if it would make sense to find good tech writers, get a quotes, and get some professionally written documentation WITH LOTS OF EXAMPLES added to the standard Python documentation tree. I'd chip in money for that task. I've certainly spent enough buying Python books to where it would be very reasonable to chip in the cost of one book towards this project. Get enough people ... could be a great thing. Even though it is not the version I use, I would suggest that the really detailed docs with lots of examples be written against the latest python version. Just a thought. Kee Nethery From martin.hellwig at dcuktec.org Fri Jul 31 20:14:33 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Sat, 01 Aug 2009 01:14:33 +0100 Subject: Python docs disappointing In-Reply-To: References: Message-ID: kj wrote: Well to a level I agree with you. If you are totally new to programming _and_ you won't/can't invest in educational material _and_ have an adversity for looking up resources using a web browser _and_ don't have the patience for trial and error *then* getting proficient with the language is almost impossible. Sometimes some modules are completely incomprehensible for me and that is after I have looked at the docstrings, the python doc and examples found using Google. However usually after combining all three with a fair bit of experimenting I figure it out, to me that is fun I like that kind of puzzles but I am aware that this isn't the best possible situation. Overall I would say a better situation would be if the standard python documentation would have an *extensive* 'pattern' section for each module much like, for example, Frederik Lundh did this with his tkinter documentation. Incorporating these patterns in the standard documentation would save you some jumping around sites and it is perhaps more 'trustworthy' coming from the source (pun intended). I believe that the documentation is all ReST format, so there should be a way (if there isn't already) to display the extensive documentation from the interpreter directly for the module you are working with. As you probably noticed, I actually never bothered to look if this isn't already the case. Which is a good indication to how much it has actually bothered me. Furthermore I like to add to my reply, which is already longer then I anticipated at first, the thing I don't agree with you is the following (and comparative in nature): Compared to all other 'popular' languages I find Python actually very, very good documented and if you can't learn Python using the available documentation and on-line resources I think it is unlikely you can learn any other language, whether this is a programming language or a natural language. Actually, I should have replaced 'any' with 'anything' followed by a full stop. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From breamoreboy at yahoo.co.uk Fri Jul 31 20:20:32 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 01 Aug 2009 01:20:32 +0100 Subject: Python docs disappointing In-Reply-To: References: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> Message-ID: r wrote: > On Jul 31, 4:53 pm, Mark Lawrence wrote: >> r wrote: >>> On Jul 31, 4:16 pm, Carl Banks wrote: >>>> On Jul 31, 1:10 pm, kj wrote: >>>>> I'm pretty new to Python, and I like a lot overall, but I find the >>>>> documentation for Python rather poor, overall. >>>>> I'm sure that Python experts don't have this problem: they have >>>>> internalized some good ways to access the documentation, are >>>>> productive with it, and therefore have lost the ability to see why >>>>> the Python documentations is deficient for beginners. >>>> That may be so, but I do remember when I was a beginner myself and I >>>> had no issue with the documentation. >>> have you tried the new docs (>= 2.6) The interface has changed >>> drastically as to render itself completely useless. The old docs (<= >>> 2.5) --the ones i learned from-- where flawless. >>> @ Mark Lawrence >>> Have you clicked any of those links? try the "Tutorial start here" and >>> then try to find a menu of sorts. It seems you have to click "next" to >>> navigate. Thats pretty counter intuitive if you need to get to page >>> 589!! Upon clicking the tutorial link in the OLD DOCS, you where >>> presented with a nice menu for concise navigation. >> Yes. Works perfectly as evidenced by the fact that this evening I've >> checked data on the cProfile, pstats and array modules. >> >> -- >> Kindest regards. >> >> Mark Lawrence. > > Hold the phone... You checked data on modules using the "Tutorial > Start Here" link? Would not the "Global Module Index" be more, shall > we say, "informative"? You asked "Have you clicked any of those links?". I answered yes since they all work. I usually navigate to module data via the index tab as I find this the easiest way. I'm sure other people prefer the global module index. Six of one, half a dozen of the other? -- Kindest regards. Mark Lawrence. From aahz at pythoncraft.com Fri Jul 31 20:39:20 2009 From: aahz at pythoncraft.com (Aahz) Date: 31 Jul 2009 17:39:20 -0700 Subject: Python docs disappointing References: Message-ID: In article , kj wrote: >In Carl Banks writes: >> >>(omg you have to use a *mouse*) > >That's precisely the point. There's a huge number of programmers out >there who, like me, *hate* to use the mouse while they're coding. It >is truly disappointing to us that the developers of Python chose to >completely disregard this constituency. That's why I use Lynx on a local copy of the Python docs. Next question? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From pfeldman at verizon.net Fri Jul 31 20:42:57 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 31 Jul 2009 17:42:57 -0700 (PDT) Subject: possible to round number and convert to string? In-Reply-To: <4a737350$0$28514$4fafbaef@reader2.news.tin.it> References: <24763821.post@talk.nabble.com> <4a737350$0$28514$4fafbaef@reader2.news.tin.it> Message-ID: <24764948.post@talk.nabble.com> This was very close to what I wanted. Thanks! My final code looks like this: def num2str(x,f=4): """Convert x (int or float) to a string with f digits to right of the decimal point. f may be zero or negative, in which case the decimal point is suppressed.""" s= str(round(x,f)) if f<=0: # Delete decimal point: i= s.find('.') if i>0: s= s[:i] return s -- View this message in context: http://www.nabble.com/possible-to-round-number-and-convert-to-string--tp24763821p24764948.html Sent from the Python - python-list mailing list archive at Nabble.com. From gagsl-py2 at yahoo.com.ar Fri Jul 31 20:46:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 21:46:22 -0300 Subject: threads check socket References: <7b3247b0-4c85-4290-9de2-41fc45348d3d@z4g2000prh.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 14:42:29 -0300, NighterNet escribi?: > On Jul 31, 10:23?am, "Gabriel Genellina" > wrote: >> En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet >> escribi?: >> > I been trying to find a way to check the socket is open or not. The >> > thread are in a group and loop if the sockets are open. If they are >> > not open delete the thread and remove the group. I need on this. >> >> What means an "open socket"? Do you want to detect whether the other >> side ? >> closed the connection? In that case reading from the socket returns an ? >> empty string. >> >> Regarding the "threads in a group", I don't understand what you mean. >> But ? >> in general, you don't delete a thread, you just return from its run() ? >> method and the thread finishes execution. > > Thread added into the array. Which array? > Well this thread when user leave the > server want to make sure it not in used so that I can reuse it some > how or move into another array. Sorry but it's hard to help if you don't provide enough background. Looks like you're developing a game server; maybe this story is relevant to you: http://comments.gmane.org/gmane.comp.python.general/627319 Assuming you're talking about TCP: when the client closes the connection gracefully, you get an empty string '' when reading from the socket. If the client loses the connection abruptly (the power goes down, the network is broken, the process is killed...) you get no indication of that. You may need to implement some mechanism (a PING? query/response, or perhaps the client must send an "I'm alive!" message once each N minutes; no answer means it's down and the server closes the connection). > Need to know the thread speed that need for game server timer to > control it. Don't want it too fast or slow for the game server. > fps = 30 > def run(self): > while True: > self.gametime += 1 > if self.gametime > 10000000/fps: > self.gametime = 0; What's this code used for, apart from sucking lots of CPU power? -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 21:02:56 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 22:02:56 -0300 Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 18:58:43 -0300, bfrederi escribi?: > So what if I used a different encoding that isn't ASCII? Like UTF-8? > Would that give me lengths that are multiples of 4 based on how the > characters are represented? Or would I still need to pad with '='? It doesn't matter, a base64-encoded string contains only ASCII characters. How are you encoding the text? base64.b64encode does the padding for you, so you don't have to worry about that. Do as P. Otten suggests, try to send some known text and see what you actually get on the server. Most likely you'll find another problem (like trying to decode the complete HTTP response instead of just the entity body, or some other stupid mistake :) ). -- Gabriel Genellina From nagle at animats.com Fri Jul 31 21:32:49 2009 From: nagle at animats.com (John Nagle) Date: Fri, 31 Jul 2009 18:32:49 -0700 Subject: Module updating plans for Python 3.1: feedparser, MySQLdb Message-ID: <4a739a96$0$1615$742ec2ed@news.sonic.net> Any progress on updating feedparser and MySQLdb for Python 3.x in the foreseeable future? Feedparser shouldn't be that hard; it's just that nobody is working on it. MySQLdb is known to be hard, and that may be a while. John Nagle From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 21:46:12 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Aug 2009 01:46:12 GMT Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <008f7990$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 20:41:12 +0200, Emmanuel Surleau wrote: >> We don't actually *declare* that something is constant and then have >> that declaration ignored. Python doesn't lie to us, although (as in any >> language) a programmer might. > > You could say that Ruby doesn't either, Well you could say a lot of things. Admittedly you don't need a separate "constant" declaration, but creating a variable name with an initial uppercase letter is sufficient to make it a (supposed) constant: irb(main):049:0* Thing = 5 => 5 irb(main):050:0> Thing = 7 (irb):50: warning: already initialized constant Thing => 7 As you can see, Ruby (well, irb at least) considers that Thing is a constant, and then goes ahead and changes it anyway. Apart from the use of an arbitrary naming convention instead of an explicit "make this constant" declaration, and the feeble way Ruby capitulates when you change it, I think having actual write-once constants is actually a plus. > you just need to read the > documentation. Ruby's unwritten motto is "flexibility ?ber alles". In > this regard, it is consistent (1). "It's consistent in its inconsistency" sort of thing perhaps? > Not much is really bolted down in > Ruby. You get encapsulation, but it's so easy to break that it's mostly > symbolic. Out of curiosity, can you read/write class and instance attributes from outside the class without using a getter/setter? -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 21:48:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Aug 2009 01:48:09 GMT Subject: Python docs disappointing References: Message-ID: <008f7a05$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 21:28:36 +0000, kj wrote: > In > Carl Banks writes: > >>(omg you have to use a >>*mouse*) > > That's precisely the point. There's a huge number of programmers out > there who, like me, *hate* to use the mouse while they're coding. Complain to the developers of your web browser then. Or use a different web browser. > It is > truly disappointing to us that the developers of Python chose to > completely disregard this constituency. Python is open source software developed by a community of volunteers, so *you* are one of the developers of Python. You have an itch that needs scratching. Solve it yourself, and release it to the wider community, don't expect somebody else to do so. Nobody is entitled to any functionality in Python. You can ask for it nicely, and if somebody thinks it's a good idea, and have nothing better to do, they may code it out of the goodness of their heart. Or you can do it yourself, or you can pay somebody to do it if you don't have the technical skill yourself. Or you can do without. > This is one area in which Perl still whips Python... If so, it's because Perl has more people who understand that the way to get something done is to do it, rather than whining about it. -- Steven From dciphercomputing at gmail.com Fri Jul 31 21:53:47 2009 From: dciphercomputing at gmail.com (Simon) Date: Fri, 31 Jul 2009 18:53:47 -0700 (PDT) Subject: Newbie Question regarding __init__() References: Message-ID: Hi So should the dcObject class include the "self" as well since I have not defined an __init__ method in dcCursor? Simon From gagsl-py2 at yahoo.com.ar Fri Jul 31 22:06:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 23:06:39 -0300 Subject: Bug with Python, Cygwin and /dev/urandom? References: <373cf0740907311657j3e65d94mbbb2eda9e0f94fb7@mail.gmail.com> Message-ID: En Fri, 31 Jul 2009 20:57:23 -0300, Jeremy Cowles escribi?: > urllib2.py is crashes when calling randombytes(n). I run the following > under > Cygwin (mimicking how randombytes works): > > $ python > Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14) > [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin > Type "help", "copyright", "credits" or "license" for more information. >>>> import sys,os >>>> os.path.exists("/dev/urandom") > True >>>> f = open("/dev/urandom") >>>> s = f.read(2) >>>> f.close() > Traceback (most recent call last): > File "", line 1, in > IOError: [Errno 0] Error > > > Errno 0 is supposed to be impossible according to the following thread. > Apparently this same issue also causes Mercurial to crash: > http://www.nabble.com/hg-1.0-exits-with-abort:-Error-td19021833.html > > > Is this a bug? It seems so, but not necesarily a Python one; it might be a bug in cygwin instead. If not already reported, file a bug at http://bugs.python.org/ -- Gabriel Genellina From python.list at tim.thechases.com Fri Jul 31 22:39:31 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 31 Jul 2009 21:39:31 -0500 Subject: Python docs disappointing In-Reply-To: References: Message-ID: <4A73AAE3.1050209@tim.thechases.com> Martin P. Hellwig wrote: > kj wrote: [excerpt of previously snipped content restored] >> I'm sure that I can find a full description of this parameter if >> I fire up Google, and search online. In fact, more likely than >> not, I'll find far more documentation than I want. But my point >> is that a programmer should not need to do this. The full >> documentation should be readily accessible directly through a few >> keystrokes. > > Well to a level I agree with you. [snip] > have an adversity for looking up resources using a web browser It may not be an adversity for looking things up using a web-browser, but rather the need to access documentation offline. Whether on an airplane or simply away from a wifi/landline connection, there are plenty of times I'm coding offline (another reason I'm happy to have DVCS tools). This thread prompted me to go digging a little further, and all the online docs are available in downloadable format, but you have to start by going to the base docs and chasing down the "downloads" rabbit-hole. Perhaps it would be easier if there were some sort of back-link from individual pages, referencing the downloadable format package. Combined with the downloaded HTML version of the docs (there are PDFs too if you prefer), using a lightweight browser like lynx/links/dillo, and optionally adding htdig for local search/indexing, you can access (and search) the full docs offline. -tkc From sjmachin at lexicon.net Fri Jul 31 22:41:30 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 31 Jul 2009 19:41:30 -0700 (PDT) Subject: .dbf tables and Null References: Message-ID: <2f4d5719-6ca8-4ef2-8734-420b3044b1dc@l35g2000pra.googlegroups.com> On Aug 1, 3:41?am, Ethan Furman wrote: > Mornin'! ?and a good one, too, I hope. > > Question for you... > > First part of the question: ?What is the general value in having Null > capability for fields? In general, in any database system, so that one can distinguish between "the customer has no 'middle name'" ('') and "the customer's 'middle name' is unknown" (NULL). > > Second part: ?Is there a tangible difference between Null, and the > nothing of 0, '', False, etc, in python? 0 is the zero thing, it is not nothing. False is not nothing. > > Third part: ?If there is a tangible difference, do those of us who use > python and these old, refuse-to-die, .dbf files actually have need of, > or have tables including, Null values? > > P.S. part (for John Machin, if he sees this ?;) > Will the dbf package you are working on support Null values? My philosophy when digging stuff out of arcane storages is to expose what is found and leave any kludging/sanitising to the next layer. For example, None is returned for an N (Number) field that's all spaces; it's up to the caller to decide whether to treat None as zero, raise an exception, pop up a data-collection dialogue box, ... If you mean specifically the Visual Foxpro v3 _NullFlags hack, yes, it already supports that, as well as the VFP9 abuse of that hack for Varchar and Varbinary fields :-) From gagsl-py2 at yahoo.com.ar Fri Jul 31 22:58:40 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 23:58:40 -0300 Subject: heapq "key" arguments References: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> <1582f82e-34aa-4b66-b989-c9f8351a5f1c@o9g2000prg.googlegroups.com> <4f096546-36a1-4633-9a24-1265ce0929ce@q14g2000vbi.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 16:38:01 -0300, Joshua Bronson escribi?: > On Jul 31, 2:02?pm, Jonathan Gardner > wrote: >> On Jul 31, 10:44?am, Joshua Bronson wrote: >> > Say I want to maintain a heap of (x, y) pairs sorted only by >> > first coordinate. Without being able to pass key=itemgetter(0), won't >> > heapifying a list of such pairs unnecessarily compare both >> > coordinates? >> >> It will compare the second value only if the first values are equal. > > I don't see how that helps. That's not at all the same thing as being > able to pass key=itemgetter(0). Ok, it's not strictly the same, but usually it doesn't hurt. The heaqp module doesn't promise anything about equal elements: it may keep the original order, rearrange them at will, reverse them, whatever. So the element-wise comparison of tuples is as good as comparing only the first element - *except* when comparing the second element isn't cheap or has side effects or something like that. In that case, use a custom class to redefine comparison operators: from heapq import heapify, heappop class tuplebyfirst(tuple): "tuple that sorts only on first element" def __lt__(self, other): return self[0] <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> <337482f9-8ad9-4600-9cf4-39911f5368ac@h31g2000yqd.googlegroups.com> Message-ID: <87ab2ktexg.fsf@benfinney.id.au> BDZ writes: > Unfortunately, although it has the calls I'd want, pysamba appears to > be *nix only. That's because Samba is *nix-only. If you want to use something knowing that it's Samba, you are by definition working on *nix. > I need something that will work under Windows. In that case, forget the term ?samba?. What you want is the ability to connect, *over a network*, to shares served via the SMB protocol suite. Those shares might be implemented via Samba, MS Windows, or aything else that can speak the SMB protocol suite. Your program, running on Windows, should not need to care about Samba, only about the various SMB protocols. A search for ?site:pypi.python.org smb client? gets me directly to . That's totally useless to you though, unfortunately, since it depends on Samba installed ? which you can't, by definition, on MS Windows. > Is there a set of Python Windows functions (official or contributed) > that might do what I need? (I'm new to Python) Not that I know of. It would probably involve a Python wrapper around the native MS Windows networking API, something which I'm not sure has been done. -- \ ?Whenever you read a good book, it's like the author is right | `\ there, in the room talking to you, which is why I don't like to | _o__) read good books.? ?Jack Handey | Ben Finney From nat.williams at gmail.com Fri Jul 31 23:13:05 2009 From: nat.williams at gmail.com (Nat Williams) Date: Fri, 31 Jul 2009 22:13:05 -0500 Subject: Newbie Question regarding __init__() In-Reply-To: References: Message-ID: As MRAB described, ALL instance methods need to accept 'self' as a first parameter, as that will be passed to them implicitly when they are called. This includes __init__. The name 'self' is just a commonly accepted convention for the name of the instance object passed to methods. You don't have to call it that, but you really should. Take a look at http://docs.python.org/tutorial/classes.html#class-objects It might help shed some light on how methods and instances work. One other thing. I'm a little confused by the first line of dcObject.__init__: self.init_Pre() and self.init_Exec() I suspect this does not do what you think it does. init_Pre and init_Exec will both be called by this expression (unless init_Pre throws an exception, of course). You're not getting anything here that you wouldn't by just calling each method on a separate line, except just making it harder to read. Nat On Fri, Jul 31, 2009 at 8:53 PM, Simon wrote: > Hi > > So should the dcObject class include the "self" as well since I have > not defined an __init__ method in dcCursor? > > Simon > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From darkneter at gmail.com Fri Jul 31 23:14:13 2009 From: darkneter at gmail.com (NighterNet) Date: Fri, 31 Jul 2009 20:14:13 -0700 (PDT) Subject: socket policy flash help Message-ID: <04e26f53-5f51-476d-823e-47bfeed9b2a8@m7g2000prd.googlegroups.com> I need help on the policy to able to let access to user to the server once the policy access is finish. I been trying to find a good example, but I got no luck. Using python version 3.1. Here the code I tested but it not working. if str(buff) == str("b\'\\x00\'"): print ('policy FOUND >>> sending...') rawinput = str('') print (rawinput) b = bytes ( ord(c) for c in rawinput) self.sockfd.send(b); From tjreedy at udel.edu Fri Jul 31 23:18:35 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 23:18:35 -0400 Subject: Python docs disappointing In-Reply-To: <4A73AAE3.1050209@tim.thechases.com> References: <4A73AAE3.1050209@tim.thechases.com> Message-ID: Tim Chase wrote: > It may not be an adversity for looking things up using a web-browser, > but rather the need to access documentation offline. Whether on an > airplane or simply away from a wifi/landline connection, there are > plenty of times I'm coding offline (another reason I'm happy to have > DVCS tools). This is one area where Windows users seems to have an advantage. The standard installer includes the doc set as a Windows help file. I often keep that open in one window while programming in others. I only later discovered that this was a copy of the online docs ;-), which I only use to check the in-development version before submitting a doc bug. From ben+python at benfinney.id.au Fri Jul 31 23:21:05 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 01 Aug 2009 13:21:05 +1000 Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: <873a8cte1q.fsf@benfinney.id.au> aahz at pythoncraft.com (Aahz) writes: > One more thing: if you have global module names with a single leading > underscore (e.g. "_foo"), they will not be loaded when you use > > from import * > > However, "import *" is strongly discouraged for the most part. Dude, that's exactly the ?underscore has an effect on import but as a beginner don't worry about it? that I avoided drawing attention to. The explanation was just fine without that. But no, you have to find a way to *contribute*, don't you? Things were different before all these new-fangled changes, I say. Get off my lawn. -- \ ?I love and treasure individuals as I meet them, I loathe and | `\ despise the groups they identify with and belong to.? ?George | _o__) Carlin, 2007 | Ben Finney From gagsl-py2 at yahoo.com.ar Fri Jul 31 23:22:35 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 01 Aug 2009 00:22:35 -0300 Subject: Help understanding the decisions *behind* python? References: <200907312048.03922.emmanuel.surleau@gmail.com> <200907312226.59027.emmanuel.surleau@gmail.com> Message-ID: En Fri, 31 Jul 2009 17:26:58 -0300, Emmanuel Surleau escribi?: > On Friday 31 July 2009 21:55:11 Terry Reedy wrote: >> The word tuple comes from relational databases as a generalization of >> single, double, triple, quadruple, quintuple, sextuple, sestuple, >> octuple, etc. A tuple is a data record with a fixed number of fields >> with individual meaning. There is nothing about homogeneity of data type >> in that definition. A triple of floats is legitimately a tuple when each >> is a coordinate (the individual meanings). In other contexts, the same >> triple might properly be a list (such as of heights of people >> arbitrarily ordered). > > My understanding is that, in this context, it's not so much data types > which > are heterogeneous, but the semantic meaning of the data. For instance, a > tuple > containing (first_name, last_name, address) would be a "legitimate" > tuple, but > not a tuple containing (address, address, address), which, if we follow > Guido's philosophy, ought to be represented as a list. Note that years ago the distinction was much stronger: tuples had NO methods at all. All you could do with a tuple was: len(t), t[i], t1+t2, t*n, e in t, for e in t Being so "crippled", thinking of tuples just as immutable lists probably wasn't so natural. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 23:31:25 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 01 Aug 2009 00:31:25 -0300 Subject: Newbie Question regarding __init__() References: Message-ID: En Fri, 31 Jul 2009 22:53:47 -0300, Simon escribi?: > So should the dcObject class include the "self" as well since I have > not defined an __init__ method in dcCursor? Every method that you define takes "self" as its first argument. Every method that you want to call on the current instance must be qualified by self.methodname(argu, ments) -- it is not different from anotherobject.methodname(...) Every attribute that you want to access from the current instance must be qualified too: self.attributename There is no implicit self/this in Python as in other languages. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 23:56:43 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 01 Aug 2009 00:56:43 -0300 Subject: os.path.exists() and Samba shares References: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> <337482f9-8ad9-4600-9cf4-39911f5368ac@h31g2000yqd.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 13:33:45 -0300, BDZ escribi?: > On Jul 30, 4:41?pm, Lo?c Domaign? > wrote: >> > Hello. I have written a Python 3.1 script running on Windows that uses >> > os.path.exists() to connect to network shares. If the various network >> > shares require different user account and password combos than the >> > account the script is running under the routine returns false. I need >> > something like os.samba.path.exists(username,password,path). Does >> > anyone have a suggestion on how I can accomplish what I need to do in >> > Python? >> >> Could the Python Samba module PySamba be interesting for >> you?http://sourceforge.net/projects/pysamba/ > > Unfortunately, although it has the calls I'd want, pysamba appears to > be *nix only. I need something that will work under Windows. Is there > a set of Python Windows functions (official or contributed) that might > do what I need? (I'm new to Python) SAMBA is a Linux implementation of the SMB protocol, natively supported on Windows. You may use the pywin32 package (available on sourceforge.net) to call the WNetAddConnection2 Windows function: http://msdn.microsoft.com/en-us/library/aa385413(VS.85).aspx -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Wed Jul 1 00:15:06 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 01 Jul 2009 01:15:06 -0300 Subject: invoking a method from two superclasses References: Message-ID: En Tue, 30 Jun 2009 21:34:02 -0300, Mitchell L Model escribi?: > Allow me to add to my previous question that certainly the superclass > methods can be called explicitly without resorting to super(), e.g.: > > class C(A, B): > def __init__(self): > A.__init__(self) > B.__init__(self) > > My question is really whether there is any way of getting around the > explicit class names by using super() and if not, shouldn't the > documentation > of super point out that if more than one class on the mro defines a > method > only the first will get called? super returns [a proxy to] the *next* class in the MRO chain; it may or may not be a superclass of C (it may be a sibling class instead). If you know that only A B and C are involved and no other subclass ever exists, yes, you can explicitely invoke A.__init__ and B.__init__. If not (and this covers the vast majority of cases) using super in all places is the only way to get correct results (for details, see the "Harmful" article by James Knight [1] that someone has already referenced) Note that doing it right with __init__ is tricky: usually, cooperative methods share the same signature so the super() call just passes the same arguments to the next class in the chain. But isn't uncommon for __init__ to have different signatures for different subclasses; the article [1] contains a recipe for this case. > What's strange is that it specifically mentions diamond patterns, which > is an important caseto get right, but it doesn't show how. The "typical superclass call" in the 2.6 docs is, uhm, typical :) - in principle you use it everywhere you have cooperative classes. For the sake of completeness I'll copy it here: class C(A, B): def method(self, arg): super(C, self).method(arg) (you may simply use super().method(arg) in Python 3). Every implementation of method() in the class hierarchy should contain a super call like above (well, it gets somewhat more complex due to error handling, but you get the idea...) In addition to the "Harmful" document, consider reading the three-article series by M. Simionato [2] which explains the problem from a different point of view. [1] Python's Super Considered Harmful http://fuhm.net/super-harmful/ [2] http://www.artima.com/weblogs/viewpost.jsp?thread=236275 -- Gabriel Genellina From nmartelaro at gmail.com Wed Jul 1 00:51:37 2009 From: nmartelaro at gmail.com (Nik Martelaro) Date: Wed, 1 Jul 2009 00:51:37 -0400 Subject: Streaming Webcam over network Message-ID: <885d14c40906302151u57d7005n5532093d68d2c786@mail.gmail.com> Hi eveyone, I am working on a project that requires streaming video from a webcam over LAN and displaying the live video on the remote computer. Basically, I am creating a specialized video chat client. I can get really nice video locally using VideoCapture and Pygame, however have only been able to send pixel data and recreate a single image at the other end. Does anyone know of a good video streaming module or any other way of doing this in Python? Thanks for any help! -------------- next part -------------- An HTML attachment was scrubbed... URL: From rylesny at gmail.com Wed Jul 1 00:52:52 2009 From: rylesny at gmail.com (ryles) Date: Tue, 30 Jun 2009 21:52:52 -0700 (PDT) Subject: Making code run in both source tree and installation path References: Message-ID: On Jun 29, 12:20?pm, Javier Collado wrote: > Hello, > > I would like to be able to run the main script in a python project > from both the source tree and the path in which it's installed on > Ubuntu. The script, among other things, imports a package which in > turns makes use of some data files that contains some metadata that is > needed in xml format. > > The source tree has an structure such as this one: > setup.py > debian/ (packaging files) > src/ (source code) > src/lib (package files) > src/data (data files) > src/bin (main script) > > However, when the project is installed using setup.py install, the > directory structure is approximately this way: > /usr/local/bin (main script) > /usr/local/share/ (data files) > /usr/local/lib/python2.x/dist-packages/ (library files) > > And when installing the code through a package, the structure is the > same one, but removing "local". > > Hence, the data files aren't always in the same relative directories > depending on we're executing code from the source tree or from the > installation. To make it possible to run the code from both places, > I've seen different approaches: > - distutils trick in setup.py to modify the installed script (i.e. > changing a global variable value) so that it has a reference to the > data files location. > - Heuristic in the package code to detect when it's being executed > from the source tree and when it has been the installed > - Just using an environment variable that the user must set according > to his needs > > I guess that there are other options, for example, maybe using > buildout. What would you say it's the best/more elegant option to > solve this problem? > > Best regards, > ? ?Javier It's kludgey, but one option may be to try and use __file__ to figure out where the script is installed. Something like os.path.dirname (os.path.abspath(__file__)) could tell you if it's in src/ or in the bin/ directory, and then data files could be found in the appropriate place. I like the distutils/variable option better. Your script is more likely to still behave correctly when copied to another directory. Plus its code definitely remains cleaner. From gagsl-py2 at yahoo.com.ar Wed Jul 1 00:54:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 01 Jul 2009 01:54:45 -0300 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: En Tue, 30 Jun 2009 22:52:18 -0300, Mag Gam escribi?: > I am very new to python and I am in the process of loading a very > large compressed csv file into another format. I was wondering if I > can do this in a multi thread approach. Does the format conversion involve a significant processing time? If not, the total time is dominated by the I/O time (reading and writing the file) so it's doubtful you gain anything from multiple threads. > Here is the pseudo code I was thinking about: > > Let T = Total number of lines in a file, Example 1000000 (1 million > files) > Let B = Total number of lines in a buffer, for example 10000 lines > > > Create a thread to read until buffer > Create another thread to read buffer+buffer ( So we have 2 threads > now. But since the file is zipped I have to wait until the first > thread is completed. Unless someone knows of a clever technique. > Write the content of thread 1 into a numpy array > Write the content of thread 2 into a numpy array Can you process each line independently? Is the record order important? If not (or at least, some local dis-ordering is acceptable) you may use a few worker threads (doing the conversion), feed them thru a Queue object, put the converted lines into another Queue, and let another thread write the results onto the destination file. import Queue, threading, csv def convert(in_queue, out_queue): while True: row = in_queue.get() if row is None: break # ... convert row out_queue.put(converted_line) def write_output(out_queue): while True: line = out_queue.get() if line is None: break # ... write line to output file in_queue = Queue.Queue() out_queue = Queue.Queue() tlist = [] for i in range(4): t = threading.Thread(target=convert, args=(in_queue, out_queue)) t.start() tlist.append(t) output_thread = threading.Thread(target=write_output, args=(out_queue,)) output_thread.start() with open("...") as csvfile: reader = csv.reader(csvfile, ...) for row in reader: in_queue.put(row) for t in tlist: in_queue.put(None) # indicate end-of-work for t in tlist: t.join() # wait until finished out_queue.put(None) output_thread.join() # wait until finished -- Gabriel Genellina From MLMLists at Comcast.net Wed Jul 1 00:57:13 2009 From: MLMLists at Comcast.net (Mitchell L Model) Date: Wed, 1 Jul 2009 00:57:13 -0400 Subject: invoking a method from two superclasses Message-ID: [Continuing the discussion about super() and __init__] The documentation of super points out that good design of diamond patterns require the methods to have the same signature throughout the diamond. That's fine for non-mixin classes where the diamond captures different ways of handling the same data. The classical example is BufferedStram / \ / \ / \ BufInputStrm BufOutputStrm both have buffers, but use them differentlyu \ / \ / \ / RandomAccessStream or something like that The idea of the diamond is to have just one buffer, rather than the two buffers that would result in C++ without making the base classes virtual. All four classes could define __init__ with the argument filename, or whatever, and everything works fine. The problems start with the use of mixins. In essence, mixins intentionally do NOT want to be part of diamond patterns. They are orthogonal to the "true" or "main" class hierarchy and just poke their heads in hear and there in that hierarchy. Moreover, a class could inherit from multiple mixins. Typical simple orthogonal mixins would be NamedObject, TrackedObject, LoggedObject, ColoredWidget, and other such names compounded from an adjective, participle, or gerund and a completely meaningless name such as Object or Thing and which classes typically manage one action or piece of state to factor it out from the many other classes that need it where the pattern of which classes need them does not follow the regular class hierarchy. Suppose I have a class User that includes NamedObject, TrackedObject, and LoggedObject as base classes. (By Tracked I mean instances are automatically registered in a list or dictionary for use by class methods that search, count, or do other operations on them.) The problem is that each mixin's __init__ is likely to have a different signature. User.__init__ would have arguments (self, name, log), but it would need to call each mixin class's __init__ with different arguments. Mixins are different than what the document refers to as cooperative multiple inheritance -- does that make them uncooperative multiple inheritance classes :-)? I think I'd be back to having to call each mixin class's __init__ explicitly: class User(NamedObject, TrackedObject, LoggedObject) def __init__(self, name, log): NamedObject.__init__(self, name) TrackedObject.__init__(self) LoggedObject.__init__(self, log) This is not terrible. It seems somehow appropriate that because mixin use is "orthogonal" to the "real" inheritance hierarchy, they shouldn't have the right to use super() and the full mro (after all, who knows where the mro will take these calls anyway). And in many cases, two mixins will join into another (a NamedTrackedObject) for convenience, and then classes inheriting from that have one less init to worry about. I suspect this problem is largely with __init__. All the other __special__ fns have defined argument lists and so can be used with diamond patterns, mixins, etc. Does this all sound accurate? (Again, not looking for design advice, just trying to ferret out the subtleties and implications of multiple inheritance and super().) From rylesny at gmail.com Wed Jul 1 01:00:10 2009 From: rylesny at gmail.com (ryles) Date: Tue, 30 Jun 2009 22:00:10 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: On Jun 29, 5:43?pm, Scott David Daniels wrote: >?and I personally wouldn't have it any other way. Simulating a shell > with hooks on its I/O should be so complicated that a "script kiddie" > has trouble writing a Trojan. +1 QOTW From ldo at geek-central.gen.new_zealand Wed Jul 1 01:07:47 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 17:07:47 +1200 Subject: Multi thread reading a file References: Message-ID: In message , Mag Gam wrote: > I am very new to python and I am in the process of loading a very > large compressed csv file into another format. I was wondering if I > can do this in a multi thread approach. Why bother? From ldo at geek-central.gen.new_zealand Wed Jul 1 01:19:30 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 17:19:30 +1200 Subject: Idioms and Anti-Idioms Question References: Message-ID: In message , J. Cliff Dyer wrote: > If the lines got separated, a leading + could disappear into its line > without any errors showing up. A trailing + would raise a syntax error. Unless, of course, it was moved onto the previous line as part of whatever caused the separation of the lines. How would you guard against that? From stefan_ml at behnel.de Wed Jul 1 01:30:13 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 01 Jul 2009 07:30:13 +0200 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <4a4af466$0$31328$9b4e6d93@newsspool4.arcor-online.net> Gabriel Genellina wrote: > En Tue, 30 Jun 2009 22:52:18 -0300, Mag Gam escribi?: > >> I am very new to python and I am in the process of loading a very >> large compressed csv file into another format. I was wondering if I >> can do this in a multi thread approach. > > Does the format conversion involve a significant processing time? If > not, the total time is dominated by the I/O time (reading and writing > the file) so it's doubtful you gain anything from multiple threads. Well, the OP didn't say anything about multiple processors, so multiple threads may not help wrt. processing time. However, if the file is large and the OS can schedule the I/O in a way that a seek disaster is avoided (although that's hard to assure with today's hard disk storage density, but SSDs may benefit), multiple threads reading multiple partial streams may still reduce the overall runtime due to increased I/O throughput. That said, the OP was mentioning that the data was compressed, so I doubt that the I/O bandwidth is a problem here. As another poster put it: why bother? Run a few benchmarks first to see where (and if!) things really get slow, and then check what to do about the real problem. Stefan From pavlovevidence at gmail.com Wed Jul 1 01:37:19 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 30 Jun 2009 22:37:19 -0700 (PDT) Subject: invoking a method from two superclasses References: Message-ID: <65fa395e-0ac1-4b64-9792-74323ecbecc8@a36g2000yqc.googlegroups.com> On Jun 30, 9:15?pm, "Gabriel Genellina" wrote: > En Tue, 30 Jun 2009 21:34:02 -0300, Mitchell L Model ? > escribi : > > > Allow me to add to my previous question that certainly the superclass > > methods can be called explicitly without resorting to super(), e.g.: > > > ? ? class C(A, B): > > ? ? ? ? def __init__(self): > > ? ? ? ? ? ? A.__init__(self) > > ? ? ? ? ? ? B.__init__(self) > > > My question is really whether there is any way of getting around the > > explicit class names by using super() and if not, shouldn't the ? > > documentation > > of super point out that if more than one class on the mro defines a ? > > method > > only the first will get called? > > super returns [a proxy to] the *next* class in the MRO chain; it may or ? > may not be a superclass of C (it may be a sibling class instead). It could be even a niece class. Carl Banks From sato.photo at gmail.com Wed Jul 1 01:38:04 2009 From: sato.photo at gmail.com (sato.photo at gmail.com) Date: Tue, 30 Jun 2009 22:38:04 -0700 (PDT) Subject: Basic question from pure beginner Message-ID: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> I have been going through some Python Programming exercises while following the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with the first "If" exercise listed on this page: http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is entered. I know this is really basic, please forgive me. I have no programming experience and have just started going through these tutorials. My code is here: http://python.pastebin.com/m6036b52e -daniel From pavlovevidence at gmail.com Wed Jul 1 02:04:15 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 30 Jun 2009 23:04:15 -0700 (PDT) Subject: Making code run in both source tree and installation path References: Message-ID: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> On Jun 29, 9:20?am, Javier Collado wrote: > I've seen different approaches: > - distutils trick in setup.py to modify the installed script (i.e. > changing a global variable value) so that it has a reference to the > data files location. One of my biggest complaints about distutils is that it doesn't do this, a limitation made worse by the fact that distutils allows you to specify an alternate data file directory, but your scripts have no way to know what that alternate directory is installed. Which really limits the usefulness of that functionality. The most common way I've seen people work around this issue is to throw their data files into the package directories. Yuck. At one point I hacked up a setup.py file to look in the distutils data structure and pull out the data install location, and wrote out a trivial python file listing that location. (The distutils build data structure is helpfully returned by setup function.) I never felt good about it, and it wouldn't work if the install was done in steps (such as build as user, install as superuser). If you care to navigate the murky internals of distutils to do this in a more elegant way, more power to you, and if so I'd recommend doing it that way. > - Heuristic in the package code to detect when it's being executed > from the source tree and when it has been the installed > - Just using an environment variable that the user must set according > to his needs I guess I'd combine these two. Make a sensible guess about where the data files are by checking out the environment, but if the data files aren't there (such as if the user installs to a different data location) then they are responsible for setting an env variable or config option. > I guess that there are other options, for example, maybe using > buildout. What would you say it's the best/more elegant option to > solve this problem? Another option is not to use distutils at all. If you are writing an application, I think that would be a good idea. I don't think applications really need to be located in site-packages. If you have any C-extensions it might complicate this. Carl Banks From __peter__ at web.de Wed Jul 1 03:01:44 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 01 Jul 2009 09:01:44 +0200 Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: sato.photo at gmail.com wrote: > I have been going through some Python Programming exercises while > following the MIT OpenCourseWare Intro to CS syllabus and am having > some trouble with the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password > three times, but can't get it to quit right away after the correct one > is entered. I know this is really basic, please forgive me. I have > no programming experience and have just started going through these > tutorials. > > My code is here: > > http://python.pastebin.com/m6036b52e > > -daniel Some hints: (1) Have the while loop check for both the correct password and the number of tries. Pseudocode: while and : ... (2) After the loop has terminated you can check whether the password was entered correctly and print the appropriate message. Aside: you can't mix if...else with other statements correct: if cond: print "sunny" else: print "cloudy" print "weather" wrong: if cond: print "norwegian" print "blue" else: # syntax error, else must immediately follow the indented if-block: print "moon" you could fix it like so: if cond: print "norwegian" print "blue" if not cond: print "moon" Peter From wuwei23 at gmail.com Wed Jul 1 03:06:38 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 00:06:38 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: On Jul 1, 3:38?pm, "sato.ph... at gmail.com" wrote: > I have been able to make the module quit after entering a password > three times, but can't get it to quit right away after the correct one > is entered. ? Not with the code you pasted, you haven't. There's a missing colon on line 7 & line 9 isn't indented properly. It's always best if we're referring to the same source when trying to help with a problem. The problem you're having, though, has to do with the while loop and the fact that it's only checking one condition: has the 'count' counter been incremented to 3. What you need to do is _also_ check for the success condition: is_confirmed = False while count != 3 or is_confirmed: guess = raw_input("Enter your password: ") guess = str(guess) if guess != password: print "Access Denied" count = count + 1 else: print "Password Confirmed" is_confirmed = True This also provides you with a nice boolean that shows whether or not the password was confirmed, which may be useful for latter code. However, whenever you want to loop a set number of times, it's usually better to use a 'for' loop instead: PASSWORD = 'qwerty' MAXRETRY = 3 for attempt in xrange(MAXRETRY): guess = str(raw_input('Enter your password: ')) is_complete = guess == PASSWORD if is_complete: print 'Password confirmed' break # this exits the for loop else: print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) This saves you from the burden of creating, incrementing & testing your own counter. If there's anything here that isn't clear, please don't hesitate to ask. From sato.photo at gmail.com Wed Jul 1 03:58:27 2009 From: sato.photo at gmail.com (sato.photo at gmail.com) Date: Wed, 1 Jul 2009 00:58:27 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Thank you for all of the help. With your assistance and help from the Python Tutor mailing list I was able to come up with the following code: password = "qwerty" correct_password_given = False guess = "0" count = 0 while count != 3 and not correct_password_given : guess = raw_input("Enter your password: ") guess = str(guess) if guess != password: print "Access Denied" count = count + 1 else: print "Password Confirmed" correct_password_given = True If I understand it correctly, the function will continue to loop until either the count == 3 or until correct_password_give = True, satisfying the two conditions of the assignment, which were to lock a user out after three failed password attempts and to print "Access Granted" and end the module if the correct password is given. Am I understanding this correctly? Thanks! On Jul 1, 12:06?am, alex23 wrote: > On Jul 1, 3:38?pm, "sato.ph... at gmail.com" > wrote: > > > I have been able to make the module quit after entering a password > > three times, but can't get it to quit right away after the correct one > > is entered. ? > > Not with the code you pasted, you haven't. There's a missing colon on > line 7 & line 9 isn't indented properly. It's always best if we're > referring to the same source when trying to help with a problem. > > The problem you're having, though, has to do with the while loop and > the fact that it's only checking one condition: has the 'count' > counter been incremented to 3. What you need to do is _also_ check for > the success condition: > > is_confirmed = False > while count != 3 or is_confirmed: > ? guess = raw_input("Enter your password: ") > ? guess = str(guess) > ? if guess != password: > ? ? print "Access Denied" > ? ? count = count + 1 > ? else: > ? ? print "Password Confirmed" > ? ? is_confirmed = True > > This also provides you with a nice boolean that shows whether or not > the password was confirmed, which may be useful for latter code. > > However, whenever you want to loop a set number of times, it's usually > better to use a 'for' loop instead: > > PASSWORD = 'qwerty' > MAXRETRY = 3 > for attempt in xrange(MAXRETRY): > ? guess = str(raw_input('Enter your password: ')) > ? is_complete = guess == PASSWORD > ? if is_complete: > ? ? print 'Password confirmed' > ? ? break # this exits the for loop > ? else: > ? ? print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) > > This saves you from the burden of creating, incrementing & testing > your own counter. > > If there's anything here that isn't clear, please don't hesitate to > ask. From bruno.42.desthuilliers at websiteburo.invalid Wed Jul 1 04:06:21 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 10:06:21 +0200 Subject: Learning to use decorators with classes In-Reply-To: References: Message-ID: <4a4b18f2$0$15174$426a74cc@news.free.fr> sk8in_zombi at yahoo.com.au a ?crit : > --- On Tue, 30/6/09, Bruno Desthuilliers wrote: > (snip) >> >> This can't work, and it's a FAQ FWIW - but since there's no >> official c.l.py FAQ, we won't hold it against you !-) >> > > Can you please point me to the FAQ related to this snippet. I would be grateful. Well, as I said, there's *no* c.l.py FAQ (I mean, any half-official text document listing frequently asked questions and their canonical answers), so I just can't provide any pointer here :-/ I guess we all here love talking about Python (and showing out our science, knowledge and wisdom) too much to maintain a FAQ !-) (snip) From bruno.42.desthuilliers at websiteburo.invalid Wed Jul 1 04:26:16 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 10:26:16 +0200 Subject: Basic question from pure beginner In-Reply-To: <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <4a4b1d9d$0$1525$426a74cc@news.free.fr> sato.photo at gmail.com a ?crit : > Thank you for all of the help. With your assistance and help from the > Python Tutor mailing list I was able to come up with the following > code: > > password = "qwerty" > correct_password_given = False > guess = "0" You could just use None here: guess=None > count = 0 > while count != 3 and not correct_password_given : > guess = raw_input("Enter your password: ") > guess = str(guess) IIRC, raw_input() already returns a string !-) > if guess != password: > print "Access Denied" > count = count + 1 Or: count += 1 > else: > print "Password Confirmed" > correct_password_given = True > > > If I understand it correctly, the function will continue to loop until > either the count == 3 or until correct_password_give = True, There are many ways to write a same boolean test, and some are easier to understand. Your test could be written: while not (count == 3 or correct_password_given) : # proceed Does it answer your question ?-) As a general rule: * not A and not B <=> not (A or B) * not A or not B <=> not (A and B) > satisfying the two conditions of the assignment, which were to lock a > user out after three failed password attempts and to print "Access > Granted" and end the module if the correct password is given. Well, given your snippet, execution indeed terminates after either successful 'login' or 3 failed attempts, but that's mostly because there's no code to execute after the while loop... From mr.spoon21 at gmail.com Wed Jul 1 04:33:28 2009 From: mr.spoon21 at gmail.com (Mr.SpOOn) Date: Wed, 1 Jul 2009 10:33:28 +0200 Subject: Getting input the scanf way Message-ID: <8f67b6f80907010133t7f6e2d9eg9ffa3b927d540728@mail.gmail.com> Hi, I need to do some kind of interactive command line program. I mean: I run the program, it asks me for input, I type something and then I get the output or other questions. I'm not sure what is the right way to achieve this. Thanks, Carlo From ldo at geek-central.gen.new_zealand Wed Jul 1 04:33:45 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:33:45 +1200 Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: In message , ryles wrote: > On Jun 29, 5:43 pm, Scott David Daniels wrote: > >>and I personally wouldn't have it any other way. Simulating a shell >> with hooks on its I/O should be so complicated that a "script kiddie" >> has trouble writing a Trojan. > > +1 QOTW Trouble is, script kiddies, by definition, don't need to write anything. They just need to download something somebody else has already written. -- Lawrence "I counter your +1 with my -1" D'Oliveiro From ldo at geek-central.gen.new_zealand Wed Jul 1 04:39:06 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:39:06 +1200 Subject: No trees in the stdlib? References: Message-ID: In message , Jo?o Valverde wrote: > Simple example usage case: Insert string into data structure in sorted > order if it doesn't exist, else retrieve it. the_set = set( ... ) if str in the_set : ... "retrieval" case ... else : the_set.add(str) #end if Want sorted order? sorted(tuple(the_set)) What could be simpler? From ldo at geek-central.gen.new_zealand Wed Jul 1 04:40:19 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:40:19 +1200 Subject: No trees in the stdlib? References: <4A4462E6.7080700@netcabo.pt> <50697b2c0906252323y60c237d0i44725e841ad29ed5@mail.gmail.com> Message-ID: In message , Miles Kaufmann wrote: > On Jun 26, 2009, at 2:23 AM, Chris Rebert wrote: > >> That's pretty much the bisect module in a nutshell. It manipulates a >> sorted list using binary search. > > With O(n) insertions and removals, though. A decent self-balancing > binary tree will generally do those in O(log n). For values of n below, say, a million, would you even notice the difference on modern hardware? From http Wed Jul 1 04:42:42 2009 From: http (Paul Rubin) Date: 01 Jul 2009 01:42:42 -0700 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <7xeit0iykd.fsf@ruckus.brouhaha.com> Joachim Str?mbergson writes: > http://docs.python.org/library/hashlib.html > I would suggest to use the SHA-256 in the library. I agree with this. We should even move towards supporting x509 and/or gpg signatures in eggs, similar to signed .jar files. From ldo at geek-central.gen.new_zealand Wed Jul 1 04:43:10 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 01 Jul 2009 20:43:10 +1200 Subject: No trees in the stdlib? References: <11eb0a6e-4e21-4a61-bf46-4221fd75dc8d@v15g2000prn.googlegroups.com> Message-ID: In message , Jo?o Valverde wrote: > But a dict can't be used to implement a (sorted) table ADT. for key in sorted(the_dict.keys(), cmp = ... whatever ordering criteria you like ...) : ... do something with the_dict[key] ... #end for From R.Brodie at rl.ac.uk Wed Jul 1 04:43:40 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Wed, 1 Jul 2009 09:43:40 +0100 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: "Joachim Str?mbergson" wrote in message news:mailman.2422.1246418400.8015.python-list at python.org... > Even so, choosing md5 in 2009 for something that (hopefully) will be > used in years is a bad design decision. It creates a dependency for to > an algorithm that all sensible recommendations point you to move away > from. Why not write the field as algorithm:value? e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 Installers can fallback to using hashlib.new(), so you can plug in a new algorithm without changing the PEP or the installer code. From http Wed Jul 1 04:43:49 2009 From: http (Paul Rubin) Date: 01 Jul 2009 01:43:49 -0700 Subject: No trees in the stdlib? References: <4A4462E6.7080700@netcabo.pt> <50697b2c0906252323y60c237d0i44725e841ad29ed5@mail.gmail.com> Message-ID: <7xab3oiyii.fsf@ruckus.brouhaha.com> Lawrence D'Oliveiro writes: > > With O(n) insertions and removals, though. A decent self-balancing > > binary tree will generally do those in O(log n). > > For values of n below, say, a million, would you even notice the difference > on modern hardware? Huh? Yes, of course you'd notice, unless you were doing the operation just once or something like that. From http Wed Jul 1 04:47:34 2009 From: http (Paul Rubin) Date: 01 Jul 2009 01:47:34 -0700 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <7x3a9gdc2h.fsf@ruckus.brouhaha.com> "Richard Brodie" writes: > Why not write the field as algorithm:value? > e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 This is reasonable, though I would deprecate md5 and sha1 already, and start with sha256. From iainking at gmail.com Wed Jul 1 05:52:24 2009 From: iainking at gmail.com (Iain King) Date: Wed, 1 Jul 2009 02:52:24 -0700 (PDT) Subject: string character count References: <58ceecd5-6d2c-44a9-8ef9-01feac5792f4@b9g2000yqm.googlegroups.com> Message-ID: On Jun 30, 6:27?pm, noydb wrote: > If I have a string for a file name such that I want to find the number > of characters to the left of the dot, how can that be done? > > I did it this way: > x = "text12345.txt" > dot = x.find('.') > print dot > > Was curious to see what method others would use - helps me learn. ?I > guess I was most curious to see if it could be done in one line. > > And, how would a char count be done with no dot -- like if the string > were "textstringwithoutdot" or "no dot in text string"? import os root, ext = os.path.splitext(x) print len(root) or in one line (assuming you've imported os): print len(os.path.splitext(x)[0]) Iain From pm567426 at gmail.com Wed Jul 1 06:00:32 2009 From: pm567426 at gmail.com (Pedram) Date: Wed, 1 Jul 2009 03:00:32 -0700 (PDT) Subject: A question about fill_free_list(void) function Message-ID: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Hello community, I'm reading the CPython interpreter source code, first, if you have something that I should know for better reading this source code, I would much appreciate that :) second, in intobject.c file, I read the following code in fill_free_list function that I couldn't understand: while (--q > p) Py_TYPE(q) = (struct _typeobject *)(q-1); Py_TYPE(q) = NULL; What does it mean? Thanks. From jerome.fuselier at gmail.com Wed Jul 1 06:14:20 2009 From: jerome.fuselier at gmail.com (=?ISO-8859-1?Q?J=E9r=F4me_Fuselier?=) Date: Wed, 1 Jul 2009 03:14:20 -0700 (PDT) Subject: Problem with uuid package when embedding a python interpreter References: <7d5cfbf0-38d5-4505-a93a-f321d0da74b8@c36g2000yqn.googlegroups.com> Message-ID: On Jun 30, 7:02?pm, "Gabriel Genellina" wrote: > En Tue, 30 Jun 2009 13:05:42 -0300, J?r?me Fuselier > escribi?: > > > > > ? I've tried to import a script in an embedded python intrepreter but > > this script fails when it imports the uuid module. I have a > > segmentation fault in Py_Finalize(). > > > #include "Python.h" > > > void test() { > > ? ? Py_Initialize(); > > ? ? PyImport_Import(PyString_FromString("uuid")); > > ? ? Py_Finalize(); > > } > > > main(int argc, char **argv) > > { > > ? ? for (i=0 ; i < 10; i++) > > ? ? ? ? test(); > > } > > > For my application, I have to call Py_initialize and Py_Finalize > > several times so factorizing them in the main function is not an easy > > solution for me. > > Are you sure you can't do that? Not even using Py_IsInitialized? Try to > avoid repeatedly calling Py_Initialize - won't work. > > Python 2.x does not have a way to "un-initialize" an extension module > (that's a big flaw in Python design). Modules that contain global state > are likely to crash the interpreter when used by the second time. (Python > 3 attempts to fix that) > > -- > Gabriel Genellina Hi Gabriel, Thanks for your response. I can modify my code to call Py_IsInitialized which I didn't know before and this works well. The problem is that I did not own the process which calls Py_Initialize and Py_Finalize. I'm able to call Py_Initialize correctly but I can't call Py_Finalize so this is not perfect. At least I have a temporary solution which is not too bad. Thanks, Jerome From jeanmichel at sequans.com Wed Jul 1 06:33:43 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 01 Jul 2009 12:33:43 +0200 Subject: string character count In-Reply-To: <4A4A51D7.6040602@mrabarnett.plus.com> References: <58ceecd5-6d2c-44a9-8ef9-01feac5792f4@b9g2000yqm.googlegroups.com> <4A4A51D7.6040602@mrabarnett.plus.com> Message-ID: <4A4B3B87.5040703@sequans.com> MRAB wrote: > noydb wrote: >> If I have a string for a file name such that I want to find the number >> of characters to the left of the dot, how can that be done? >> >> I did it this way: >> x = "text12345.txt" >> dot = x.find('.') >> print dot >> >> Was curious to see what method others would use - helps me learn. I >> guess I was most curious to see if it could be done in one line. >> > >>> print "text12345.txt".find('.') > 9 > >> And, how would a char count be done with no dot -- like if the string >> were "textstringwithoutdot" or "no dot in text string"? > > If there's no dot then find() returns -1. > > If you wanted to know the number of characters before the dot, if > present, or in total otherwise, then you could use split(): > > >>> len("text12345.txt".split(".", 1)[0]) > 9 > >>> len("textstringwithoutdot".split(".", 1)[0]) > 20 > You may have problems with files containing multiple dots. the os module provide nice & safe methods to parse a file path (on any system, mac os windows, linux). >>>f = "/dir1/dir2/test.log" >>>os.path.splitext(f) ('/dir1/dir2/test', '.log') >>>os.path.splitext(os.path.basename(f)) ('test', '.log') >>>f2 = 'test.log' >>>os.path.splitext(os.path.basename(f2)) ('test', '.log') one safe way would be then >>> import os >>> x = "text12345.txt" >>> base , ext = os.path.splitext(os.path.basename(x)) >>> print len(base) 9 Jean-Michel From icanbob at gmail.com Wed Jul 1 06:40:22 2009 From: icanbob at gmail.com (bobicanprogram) Date: Wed, 1 Jul 2009 03:40:22 -0700 (PDT) Subject: Passing parameters for a C program in Linux. References: <7fa13d0c-d031-4576-8fee-a1c84af3e265@y7g2000yqa.googlegroups.com> Message-ID: On Jun 30, 6:46 am, "venutaurus... at gmail.com" wrote: > Hi all, > I have to write an automted script which will test my c > program. That program when run will ask for the commands. For example: > > local-host# ./cli > Enter 1 for add > Enter 2 for sub > Enter 3 for mul > 1 -------Our option > Enter two numbers > 44 33 -------- Our option > Result is 77 > > As shown in the above example it lists all the options and waits for > user input and once given, it does some operations and waits for some > more. This has to be automated. > > Can someone give suggestions how to do this in Python (Linux Platform > in particular). > > Thank you, > Venu. Take a look at the examples here: http://www.icanprogram.com/06py/main.html You can probably do what you want using the SIMPL toolkit quite easily. bob From steve at REMOVE-THIS-cybersource.com.au Wed Jul 1 07:41:07 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Jul 2009 11:41:07 GMT Subject: No trees in the stdlib? References: Message-ID: <025b3cfd$0$20654$c3e8da3@news.astraweb.com> On Wed, 01 Jul 2009 20:39:06 +1200, Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > >> Simple example usage case: Insert string into data structure in sorted >> order if it doesn't exist, else retrieve it. > > the_set = set( ... ) > > if str in the_set : > ... "retrieval" case ... Not terribly useful, because there's no way of attaching any data to retrieve to the key. Apart from the case of interning strings (or other keys), you'd probably want a dict rather than a set: if str in the_dict: return the_dict[str] else: the_dict[str] = something_or_other > else : > the_set.add(str) > #end if > Want sorted order? > > sorted(tuple(the_set)) > > What could be simpler? Dropping the entirely superfluous creation of a tuple: sorted(the_set) That's a reasonable approach for small sets of data, but it isn't very useful if the_set contains 4GB of keys, because you have to duplicate the keys, then sort them. More effective would be a data structure that was always sorted. This has the further advantage that it's even simpler than sorted(the_set): the_set -- Steven From magawake at gmail.com Wed Jul 1 07:41:29 2009 From: magawake at gmail.com (Mag Gam) Date: Wed, 1 Jul 2009 07:41:29 -0400 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <1cbd6f830907010441l3f953d13r6dd4e1a55dedc806@mail.gmail.com> Thanks for the response Gabriel. On Wed, Jul 1, 2009 at 12:54 AM, Gabriel Genellina wrote: > En Tue, 30 Jun 2009 22:52:18 -0300, Mag Gam escribi?: > >> I am very new to python and I am in the process of loading a very >> large compressed csv file into another format. ?I was wondering if I >> can do this in a multi thread approach. > > Does the format conversion involve a significant processing time? If not, > the total time is dominated by the I/O time (reading and writing the file) > so it's doubtful you gain anything from multiple threads. The format does inolve significant time processing each line. > >> Here is the pseudo code I was thinking about: >> >> Let T ?= Total number of lines in a file, Example 1000000 (1 million >> files) >> Let B = Total number of lines in a buffer, for example 10000 lines >> >> >> Create a thread to read until buffer >> Create another thread to read buffer+buffer ?( So we have 2 threads >> now. But since the file is zipped I have to wait until the first >> thread is completed. Unless someone knows of a clever technique. >> Write the content of thread 1 into a numpy array >> Write the content of thread 2 into a numpy array > > Can you process each line independently? Is the record order important? If > not (or at least, some local dis-ordering is acceptable) you may use a few > worker threads (doing the conversion), feed them thru a Queue object, put > the converted lines into another Queue, and let another thread write the > results onto the destination file. Yes, each line can be independent. The original file is a time series file which I am placing it into a Numpy array therefore I don't think the record order is important. The writing is actually done when I place a value into a "dset" object. Let me show you what I mean. reader=csv.reader(open("100000.csv")) for s,row in enumerate(reader): if s!=0 and s%bufsize==0: dset[s-bufsize:s] = t #here is where I am writing the data to the data structure. Using a range or broadcasting. #15 columns if len(row) != 15: break t[s%bufsize] = tuple(row) #Do this all the way at the end for flushing. if (s%bufsize != 0): dset[(s//bufsize)*bufsize:s]=t[0:s%bufsize] > > import Queue, threading, csv > > def convert(in_queue, out_queue): > ?while True: > ? ?row = in_queue.get() > ? ?if row is None: break > ? ?# ... convert row > ? ?out_queue.put(converted_line) > > def write_output(out_queue): > ?while True: > ? ?line = out_queue.get() > ? ?if line is None: break > ? ?# ... write line to output file > > in_queue = Queue.Queue() > out_queue = Queue.Queue() > tlist = [] > for i in range(4): > ?t = threading.Thread(target=convert, args=(in_queue, out_queue)) > ?t.start() > ?tlist.append(t) > output_thread = threading.Thread(target=write_output, args=(out_queue,)) > output_thread.start() > > with open("...") as csvfile: > ?reader = csv.reader(csvfile, ...) > ?for row in reader: > ? ?in_queue.put(row) > > for t in tlist: in_queue.put(None) # indicate end-of-work > for t in tlist: t.join() # wait until finished > out_queue.put(None) > output_thread.join() # wait until finished > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > From ganesh.gbg at gmail.com Wed Jul 1 07:48:11 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Wed, 1 Jul 2009 04:48:11 -0700 (PDT) Subject: what will be best framework to select to develop Multi-Level Marketing (network marketing) application in python? Message-ID: <2f8c1642-6cad-4cd1-bc97-60c16aea31fd@g6g2000vbr.googlegroups.com> hi group, I have to build a software for Multi-Level Marketing (network marketing),There are several Frameworks available in python ,i could like to know the best Framework suitable to develop an application on Multi-Level Marketing . Thanks From Ron.Barak at lsi.com Wed Jul 1 07:48:40 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Wed, 1 Jul 2009 12:48:40 +0100 Subject: What are the limitations of cStringIO.StringIO() ? Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Hi, I think I'm up against a limitation in cStringIO.StringIO(), but could not find any clues on the web, especially not in http://docs.python.org/library/stringio.html. What I have is the following function: def concatenate_files(self,filename_array): import types f = cStringIO.StringIO() for filename in filename_array: log_stream = LogStream(filename) if not isinstance(log_stream, types.NoneType): try: string_ = log_stream.input_file.read() except AttributeError, e: sys.stderr.write("AttributeError: "+str(e)+"\n") sys.stderr.write("log_stream: "+str(log_stream)+"\n") else: return(None) f.write(string_) return (f) And, when the list of files - in filename_array - is pointing to long/large enough files, I get the exception: Traceback (most recent call last): File "svm_ts_tool_in_progress.py", line 244, in OnSelectCell self.InspectorViewController(row,col) File "svm_ts_tool_in_progress.py", line 1216, in InspectorViewController self.InspectorePaneGetRecords(self.req_table, rec) File "svm_ts_tool_in_progress.py", line 1315, in InspectorePaneGetRecords log_stream = ConcatenatedLogStream(self.events_dict[req_table]["db_dict"].keys()) File "c:\Documents and Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 31, in __init__ self.input_file = self.concatenate_files(filename_array) File "c:\Documents and Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 47, in concatenate_files string_ = log_stream.input_file.read() MemoryError 1. Anyone knows whet's the limitation on cStringIO.StringIO() objects ? 2. Could you suggest a better way to create a string that contains the concatenation of several big files ? Thanks, Ron. -------------- next part -------------- An HTML attachment was scrubbed... URL: From thomas.bugzilla at gmx.net Wed Jul 1 08:38:36 2009 From: thomas.bugzilla at gmx.net (Thomas) Date: Wed, 01 Jul 2009 14:38:36 +0200 Subject: infinite recursion in pickle.load() Message-ID: we regularly pickle and unpickle data from the same script (mostly dictionaries). sometimes, a file written this way cannot be unpickled with pickle.load(), due to an infinite recursion with __getattr__ in codecs.py. here is a python2.5 stack trace excerpt: /usr/local/lib/python2.5/pickle.py in load(file) 1403 1404 def load(file): -> 1405 return Unpickler(file).load() 1406 1407 def loads(str): /usr/local/lib/python2.5/pickle.py in load(self) 891 while 1: 892 key = read(1) --> 893 dispatch[key](self) 894 except _Stop, stopinst: 895 return stopinst.value /usr/local/lib/python2.5/pickle.py in load_build(self) 1248 state = stack.pop() 1249 inst = stack[-1] -> 1250 setstate = getattr(inst, "__setstate__", None) 1251 if setstate: 1252 setstate(state) /usr/local/lib/python2.5/codecs.py in __getattr__(self, name, getattr) 328 """ Inherit all other methods from the underlying stream. 329 """ --> 330 return getattr(self.stream, name) 331 332 def __enter__(self): /usr/local/lib/python2.5/codecs.py in __getattr__(self, name, getattr) 328 """ Inherit all other methods from the underlying stream. 329 """ --> 330 return getattr(self.stream, name) 331 332 def __enter__(self): ... The last frame repeats ad infinitum. 'inst' in the third frame is a The problem is the same with cPickle. This looks somewhat related to this Python issue, which is only about the exception reporting: http://bugs.python.org/issue5508 (The title of the issue is the error you get when running our code in python2.6). Any idea how to go about that? T. From gil.shinar at gmail.com Wed Jul 1 08:56:12 2009 From: gil.shinar at gmail.com (eranlevi) Date: Wed, 1 Jul 2009 05:56:12 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> Message-ID: <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> On Jun 30, 7:48?pm, s... at pobox.com wrote: > ? ? Gil> I have looked for a timeout parameter to limit the 4 minutes to > ? ? Gil> something more reasonable but couldn't find one. ?Can anyone please > ? ? Gil> help? > > ? ? Gil> BTW, I'm using Sybase.connect(, , , > ? ? Gil> datetime='auto') > > We use the Sybase module where I work, but I've never encountered this > problem (or a timeout parameter of any kind). ?At any rate, you'll probably > have more luck asking on the python-sybase mailing list: > > ? ? python-sybase-m... at lists.sourceforge.net > > -- > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > ? ? when i wake up with a heart rate below 40, i head right for the espresso > ? ? machine. -- chaos @ forums.usms.org Are you saying, that when you trying to connect to a sybase DBS server and the DBS or the server is down, you get an error after a few seconds and not after a few minutes? Which python version are you using? I'll try to ask in the python-sybase mailing list Thanks From gil.shinar at gmail.com Wed Jul 1 09:08:01 2009 From: gil.shinar at gmail.com (eranlevi) Date: Wed, 1 Jul 2009 06:08:01 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> Message-ID: <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> On Jul 1, 3:56?pm, eranlevi wrote: > On Jun 30, 7:48?pm, s... at pobox.com wrote: > > > > > ? ? Gil> I have looked for a timeout parameter to limit the 4 minutes to > > ? ? Gil> something more reasonable but couldn't find one. ?Can anyone please > > ? ? Gil> help? > > > ? ? Gil> BTW, I'm using Sybase.connect(, , , > > ? ? Gil> datetime='auto') > > > We use the Sybase module where I work, but I've never encountered this > > problem (or a timeout parameter of any kind). ?At any rate, you'll probably > > have more luck asking on the python-sybase mailing list: > > > ? ? python-sybase-m... at lists.sourceforge.net > > > -- > > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > > ? ? when i wake up with a heart rate below 40, i head right for the espresso > > ? ? machine. -- chaos @ forums.usms.org > > Are you saying, that when you trying to connect to a sybase DBS server > and the DBS or the server is down, you get an error after a few > seconds and not after a few minutes? Which python version are you > using? > > I'll try to ask in the python-sybase mailing list > > Thanks There's no such group as python-sybase :-( Thanks From pm567426 at gmail.com Wed Jul 1 09:50:31 2009 From: pm567426 at gmail.com (Pedram) Date: Wed, 1 Jul 2009 06:50:31 -0700 (PDT) Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Message-ID: Oh... I got it! :) I found this line in ctypes.h: #define Py_TYPE(q) = ((PyObject *)(q))->ob_next; So those lines are trying to set the blocks type from rear to front. But I still don't know why after the while (when q is equal to p), the Py_TYPE(q) set to NULL! From sebastian.schabe at gmx.de Wed Jul 1 10:51:47 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Wed, 01 Jul 2009 16:51:47 +0200 Subject: deleting certain entries in numpy array Message-ID: <4a4b7763$1@news.fhg.de> Hello everybody, I'm new to python and numpy and have a little/special problem: I have an numpy array which is in fact a gray scale image mask, e.g.: mask = array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 255, 255, 255, 0, 0, 255, 0], [ 0, 0, 255, 255, 255, 0, 0, 255, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) and I have another array of (y, x) positions in that mask (first two values of each row): pos = array([[ 3., 2., 0., 0.], [ 3., 4., 0., 0.], [ 5., 2., 0., 0.], [ 5., 4., 0., 0.], [ 6., 2., 0., 0.], [ 6., 7., 0., 0.], [ 0., 0., 0., 0.], [ 8., 8., 0., 0.]]) and now I only want to keep all lines from 2nd array pos with those indices that are nonzero in the mask, i.e. line 3-6 (pos[2]-pos[5]). F.e. line 4 in pos has the values (5, 4) and mask[5][4] is nonzero, so I want to keep it. While line 2 (pos[1]) has the values (4, 6) and mask[4][6] is zero, so shall be discarded. I want to avoid a for loop (if possible!!!) cause I think (but don't know) numpy array are handled in another way. I think numpy.delete is the right function for discarding the values, but I don't know how to build the indices. Maybe someone can help me with that or suggest which way to do this Sebastian From Frank.Aune at broadpark.no Wed Jul 1 11:02:20 2009 From: Frank.Aune at broadpark.no (Frank Aune) Date: Wed, 1 Jul 2009 17:02:20 +0200 Subject: logging module and binary strings Message-ID: <200907011702.21450.Frank.Aune@broadpark.no> Hello, ---- snip ---- >>> import logging >>> logging.basicConfig(level=logging.DEBUG) >>> x='\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>> x '\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>> print x ????? >>> logging.info(x) Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit stream.write(fs % msg.encode("UTF-8")) UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 10: ordinal not in range(128) >>> logging.info(x, extra={'encoding':'utf-8'}) Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit stream.write(fs % msg.encode("UTF-8")) UnicodeDecodeError: 'utf8' codec can't decode byte 0xfe in position 10: unexpected code byte ---- snip ---- Is there any way to log the above value of x "properly" using the python logging module? By properly I mean logging the value displayed when entering just x in the python shell and pressing enter. Regards, Frank From eric.pruitt at gmail.com Wed Jul 1 11:17:31 2009 From: eric.pruitt at gmail.com (Eric Pruitt) Date: Wed, 1 Jul 2009 10:17:31 -0500 Subject: Bytes, Strings, Encoding Message-ID: <171e8a410907010817j715f1d5ye638ea4b2469afd9@mail.gmail.com> Hello, I am working on the subprocess.Popen module for Python 2.7 and am now moving my changes over to Python 3.1 however I am having trouble with the whole byte situation and I can't quite seem to understand how to go back and forth between bytes and strings. I am also looking for the Python 3k equivalent for the Python 2.X built-in buffer class. One version of the file with my modifications can be found here < http://code.google.com/p/subprocdev/source/browse/subprocess.py?spec=svn5b570f8cbfcaae859091eb01b21b183aa5221af9&r=5b570f8cbfcaae859091eb01b21b183aa5221af9>. Lines 1 - 15 are me attempting to get around certain changes between Python 3.0 and Python 2.7. Further down on line 916, we have the function "send" and "recv" in which I am having the most trouble with bytes and strings. Any help is appreciated. Feel free to comment on my blog http://subdev.blogspot.com/ or reply to the last. Thanks in advance, Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: From Scott.Daniels at Acm.Org Wed Jul 1 11:38:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 01 Jul 2009 08:38:58 -0700 Subject: invoking a method from two superclasses In-Reply-To: References: Message-ID: > Mitchell L Model wrote: Sorry, after looking over some other responses, I went back and re-read your reply. I'm just making sure here, but: > Scott David Daniels wrote: Below compressed for readability in comparison: >> class A: >> def __init__(self): super().__init__(); print('A') >> class B: >> def __init__(self): super().__init__(); print('B') >> class C(A, B): >> def __init__(self): super().__init__(); print('C') >> C() >> And, if you are doing it with a message not available in object: Renamed to disambiguate later discussion >> class root: >> def prints(self): print('root') # or pass if you prefer >> class D(root): >> def prints(self): super().prints(); print('D') >> class E(root): >> def prints(self): super().prints(); print('E') >> class F(D, E): >> def prints(self): super().prints(); print('F') >> F().prints() > What I was missing is that each path up to and including the top of the diamond > must include a definition of the method, along with super() calls to move the method > calling on its way up. Actually, not really true. In the F through root example, any of the "prints" methods except that on root may be deleted and the whole thing works fine. The rootward (closer to object) end must contain the method in question, possibly only doing a pass as the action, and _not_ calling super. The other methods (those in D, E, and F above are all optional (you can freely comment out those methods where you like), but each should call super() in their bodies. Note that you can also add a class: class G(E, D): def prints(self): super().prints(); print('G') G().prints() Also note that the inheritances above can be "eventually inherits from" as well as direct inheritance. > Is this what the documentation means by "cooperative multiple inheritance"? Yes, the phrase is meant to convey "No magic (other than super itelf) is involved in causing the other methods to be invoked. If you want "all" prints methods called, make sure all but the last method do super calls. Of course, any method that doesn't do the super call will be the last by definition (no flow control then), but by putting the non-forwarding version at or below the lower point of the diamond, the mro order guarantees that you will have a "good" place to stop. Think of the mro order as a solution to the partial order constraints that a class must appear before any of its direct superclasses, and (by implication) after any of its subclasses. > If your correction of my example, if you remove super().__init__ from B.__init__ > the results aren't affected, because object.__init__ doesn't do anything and > B comes after A in C's mro. However, if you remove super().__init__ from > A.__init__, it stops the "supering" process dead in its tracks. Removing the super from B.__init__ means that you don't execute object.__init__. It turns out that object does nothing in its __init__, but without knowing that, removing the super from B.__init__ is also a mistake. So, you may well already have it all right, but as long as I'm putting in the effort to get the "operational rules about using super" out, I thought I'd fill in this last little bit. --Scott David Daniels Scott.Daniels at Acm.Org From jenn.duerr at gmail.com Wed Jul 1 11:45:40 2009 From: jenn.duerr at gmail.com (noydb) Date: Wed, 1 Jul 2009 08:45:40 -0700 (PDT) Subject: string character count References: <58ceecd5-6d2c-44a9-8ef9-01feac5792f4@b9g2000yqm.googlegroups.com> Message-ID: <54b554d3-c03e-40de-a02d-a176d0583cea@z28g2000vbl.googlegroups.com> thanks everyone for all the ideas -- simple stuff, I know for you all, but very helpful for me. From jcd at sdf.lonestar.org Wed Jul 1 11:48:23 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Wed, 01 Jul 2009 11:48:23 -0400 Subject: It's ... In-Reply-To: References: Message-ID: <1246463303.11768.0.camel@aalcdl07> On Tue, 2009-06-30 at 13:24 -0700, Beni Cherniavsky wrote: > On Jun 24, 11:40 pm, "J. Cliff Dyer" wrote: > > Also note that you can iterate over a file several times: > > > > f = open('foo.txt') > > for line in f: > > print line[0] # prints the first character of every line > > for line in f: > > print line[1] #prints the second character of every line > > > No, you can't. The second loop prints nothing! > A file by default advances forward. Once you reach the end, you stay > there. You are, of course, absolutely right. Sorry for the misinformation. :( From Scott.Daniels at Acm.Org Wed Jul 1 11:49:31 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 01 Jul 2009 08:49:31 -0700 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: Gabriel Genellina wrote: > ... > def convert(in_queue, out_queue): > while True: > row = in_queue.get() > if row is None: break > # ... convert row > out_queue.put(converted_line) These loops work well with the two-argument version of iter, which is easy to forget, but quite useful to have in your bag of tricks: def convert(in_queue, out_queue): for row in iter(in_queue.get, None): # ... convert row out_queue.put(converted_line) --Scott David Daniels Scott.Daniels at Acm.Org From petshmidt at googlemail.com Wed Jul 1 12:05:37 2009 From: petshmidt at googlemail.com (someone) Date: Wed, 1 Jul 2009 09:05:37 -0700 (PDT) Subject: =?windows-1252?Q?getting_rid_of__=97?= Message-ID: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Hello, how can I replace '?' sign from string? Or do split at that character? Getting unicode error if I try to do it: UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 1: ordinal not in range(128) Thanks, Pet script is # -*- coding: UTF-8 -*- From michele.simionato at gmail.com Wed Jul 1 12:08:55 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Wed, 1 Jul 2009 09:08:55 -0700 (PDT) Subject: invoking a method from two superclasses References: Message-ID: <8bc6afc1-21fc-40dd-ba7d-f0d549a7de93@24g2000yqm.googlegroups.com> On Jul 1, 2:34?am, Mitchell L Model wrote: > I suspect we should have a Multiple Inheritance HOWTO, though details and > recommendations would be controversial. I've accumulated lots of abstract > examples along the lines of my question, using multiple inheritance both to > create combination classes (the kinds that are probably best done with > composition instead of inheritance) and mixins. I like mixins, and I like abstract > classes. And yes I understand the horrors of working with a large component > library that uses mixins heavily, because I've experienced many of them, going > all the way back to Lisp-Machine Lisp's window system with very many combo > classes such as FancyFontScrollingTitledMinimizableWindow, or whatever. > Also, I understand that properties might be better instead of multiple inheritance > for some situations. What I'm trying to do is puzzle out what the reasonable uses > of multiple inheritance are in Python 3 and how classes and methods that follow > them should be written. You may be interest in my "Things to know about super": http://www.artima.com/weblogs/viewpost.jsp?thread=236275 http://www.artima.com/weblogs/viewpost.jsp?thread=236278 http://www.artima.com/weblogs/viewpost.jsp?thread=237121 From robert.kern at gmail.com Wed Jul 1 12:17:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 01 Jul 2009 11:17:31 -0500 Subject: Making code run in both source tree and installation path In-Reply-To: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> References: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> Message-ID: On 2009-07-01 01:04, Carl Banks wrote: > On Jun 29, 9:20 am, Javier Collado wrote: >> I've seen different approaches: >> - distutils trick in setup.py to modify the installed script (i.e. >> changing a global variable value) so that it has a reference to the >> data files location. > > > One of my biggest complaints about distutils is that it doesn't do > this, a limitation made worse by the fact that distutils allows you to > specify an alternate data file directory, but your scripts have no way > to know what that alternate directory is installed. Which really > limits the usefulness of that functionality. > > The most common way I've seen people work around this issue is to > throw their data files into the package directories. Yuck. Huh. I always found that to be a more elegant solution than hardcoding the data location into the program at install-time. -- 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 python at mrabarnett.plus.com Wed Jul 1 12:18:24 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 01 Jul 2009 17:18:24 +0100 Subject: Basic question from pure beginner In-Reply-To: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> Message-ID: <4A4B8C50.4010202@mrabarnett.plus.com> sato.photo at gmail.com wrote: > I have been going through some Python Programming exercises while > following the MIT OpenCourseWare Intro to CS syllabus and am having > some trouble with the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password > three times, but can't get it to quit right away after the correct one > is entered. I know this is really basic, please forgive me. I have > no programming experience and have just started going through these > tutorials. > > My code is here: > > http://python.pastebin.com/m6036b52e > raw_input() returns a string, so you don't need "guess = str(guess)". The 'if' line should end with a colon. Incrementing the count should probably be after the 'if' statement. You can leave a loop prematurely with the 'break' statement. From mail at timgolden.me.uk Wed Jul 1 12:20:38 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 01 Jul 2009 17:20:38 +0100 Subject: No trees in the stdlib? In-Reply-To: References: Message-ID: <4A4B8CD6.6050606@timgolden.me.uk> Lawrence D'Oliveiro wrote: > Want sorted order? > > sorted(tuple(the_set)) Not sure why you want the tuple () there, but you probably knew that... TJG From python at mrabarnett.plus.com Wed Jul 1 12:21:54 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 01 Jul 2009 17:21:54 +0100 Subject: No trees in the stdlib? In-Reply-To: References: Message-ID: <4A4B8D22.3060308@mrabarnett.plus.com> Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > >> Simple example usage case: Insert string into data structure in sorted >> order if it doesn't exist, else retrieve it. > > the_set = set( ... ) > > if str in the_set : > ... "retrieval" case ... > else : > the_set.add(str) > #end if > > Want sorted order? > > sorted(tuple(the_set)) > > What could be simpler? > Why not just "sorted(the_set)"? From charles at declareSub.com Wed Jul 1 12:26:41 2009 From: charles at declareSub.com (Charles Yeomans) Date: Wed, 1 Jul 2009 12:26:41 -0400 Subject: Basic question from pure beginner In-Reply-To: <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <1A63D369-87EF-4429-8C05-C1579C9BE71C@declareSub.com> Let me offer a bit of editing. First, using the condition count != 3 is perhaps risky. A mistake or a change in logic in the loop body might result in an infinite loop. So instead I suggest while count < 3... Second, I'd suggest storing the value 3 in a variable with a name that describes it. MaxAttempts = 3 while count < MaxAttempts This suggests that you change the name 'count' to 'attemptcount'. The variable 'guess' is used only inside the loop, so I suggest removing the assignment outside the loop. Finally, I'd remove correct_password_given from the loop test, and replace it with a break statement when the correct password is entered. password = "qwerty" correct_password_given = False attemptcount = 0 MaxAttempts = 3 while attemptcount < MaxAttempts: guess = raw_input("Enter your password: ") guess = str(guess) if guess != password: print "Access Denied" attemptcount = attemptcount + 1 else: print "Password Confirmed" correct_password_given = True break Charles Yeomans On Jul 1, 2009, at 3:58 AM, sato.photo at gmail.com wrote: > Thank you for all of the help. With your assistance and help from the > Python Tutor mailing list I was able to come up with the following > code: > > password = "qwerty" > correct_password_given = False > guess = "0" > count = 0 > while count != 3 and not correct_password_given : > guess = raw_input("Enter your password: ") > guess = str(guess) > if guess != password: > print "Access Denied" > count = count + 1 > else: > print "Password Confirmed" > correct_password_given = True > > > If I understand it correctly, the function will continue to loop until > either the count == 3 or until correct_password_give = True, > satisfying the two conditions of the assignment, which were to lock a > user out after three failed password attempts and to print "Access > Granted" and end the module if the correct password is given. Am I > understanding this correctly? > > Thanks! > > On Jul 1, 12:06 am, alex23 wrote: >> On Jul 1, 3:38 pm, "sato.ph... at gmail.com" >> wrote: >> >>> I have been able to make the module quit after entering a password >>> three times, but can't get it to quit right away after the correct >>> one >>> is entered. >> >> Not with the code you pasted, you haven't. There's a missing colon on >> line 7 & line 9 isn't indented properly. It's always best if we're >> referring to the same source when trying to help with a problem. >> >> The problem you're having, though, has to do with the while loop and >> the fact that it's only checking one condition: has the 'count' >> counter been incremented to 3. What you need to do is _also_ check >> for >> the success condition: >> >> is_confirmed = False >> while count != 3 or is_confirmed: >> guess = raw_input("Enter your password: ") >> guess = str(guess) >> if guess != password: >> print "Access Denied" >> count = count + 1 >> else: >> print "Password Confirmed" >> is_confirmed = True >> >> This also provides you with a nice boolean that shows whether or not >> the password was confirmed, which may be useful for latter code. >> >> However, whenever you want to loop a set number of times, it's >> usually >> better to use a 'for' loop instead: >> >> PASSWORD = 'qwerty' >> MAXRETRY = 3 >> for attempt in xrange(MAXRETRY): >> guess = str(raw_input('Enter your password: ')) >> is_complete = guess == PASSWORD >> if is_complete: >> print 'Password confirmed' >> break # this exits the for loop >> else: >> print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) >> >> This saves you from the burden of creating, incrementing & testing >> your own counter. >> >> If there's anything here that isn't clear, please don't hesitate to >> ask. > > -- > From bdesth.quelquechose at free.quelquepart.fr Wed Jul 1 12:39:18 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 18:39:18 +0200 Subject: Getting input the scanf way In-Reply-To: References: Message-ID: <4a4bacd3$0$296$426a74cc@news.free.fr> Mr.SpOOn a ?crit : > Hi, > I need to do some kind of interactive command line program. > > I mean: I run the program, it asks me for input, I type something and > then I get the output or other questions. > I'm not sure what is the right way to achieve this. Simplest : use raw_input() Friendlier (at least on *n*x platforms) : use readline. HTH From bdesth.quelquechose at free.quelquepart.fr Wed Jul 1 12:43:44 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 18:43:44 +0200 Subject: Basic question from pure beginner In-Reply-To: References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <4a4badde$0$10226$426a74cc@news.free.fr> Charles Yeomans a ?crit : Please don't top-post (not corrected) > Let me offer a bit of editing. > > First, using the condition count != 3 is perhaps risky. A mistake or a > change in logic in the loop body might result in an infinite loop. So > instead I suggest > > while count < 3... > > Second, I'd suggest storing the value 3 in a variable with a name that > describes it. Pretty sensible suggestion, but then: > MaxAttempts = 3 follow conventions instead : MAX_ATTEMPS = 3 (snip) > Finally, I'd remove correct_password_given from the loop test, and > replace it with a break statement when the correct password is entered. Then using a while loop and manually incrementing a counter is a waste of time - using a for loop would be simplier and less error-prone. From bdesth.quelquechose at free.quelquepart.fr Wed Jul 1 12:45:26 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Wed, 01 Jul 2009 18:45:26 +0200 Subject: Basic question from pure beginner In-Reply-To: References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <4a4bae44$0$10226$426a74cc@news.free.fr> Scott David Daniels a ?crit : (snip) > And even simpler: > PASSWORD = "qwerty" > MAXRETRY = 3 > for attempt in range(MAXRETRY): > if raw_input('Enter your password: ') == PASSWORD: > print 'Password confirmed' > break # this exits the for loop > print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) > else: > # The else for a for statement is not executed for breaks, > # So indicates the end of testing without a match > raise SystemExit # Or whatever you'd rather do. Which is probably the most pythonic implementation. From cmpython at gmail.com Wed Jul 1 13:24:27 2009 From: cmpython at gmail.com (Che M) Date: Wed, 1 Jul 2009 10:24:27 -0700 (PDT) Subject: Ubigraph vs Matplotlib (dynamic plotting, event handling) References: <300620092320590209%shaibani@ymail.com> Message-ID: <307b4075-ec5b-4d06-a638-c62f38307e7e@q11g2000yqi.googlegroups.com> On Jun 30, 6:20?pm, Ala wrote: > Hello everyone, > > I intend to use python for some network graph plotting, with event > handling (clicking on network nodes, zooming in/out etc..) and so far I > have come across two good candidates which are Matplotlib and Ubigraph. > > Did anyone have any experience with either of them for dynamic plotting > (a slider bar on a Qt interface causing the graph to be replotted for > each value for example), as well as event handling? (it seems on first > notice that Ubigraph will have an upperhand on that), as well as event > handling such as mouse clicks? (on this one Matplotlib has good > documentation showing it does achieve that while I find ubigraph's > documentation lacking, but I'd preffere to have the opinion of those > who have used them before). > > Thank you. I have used Matplotlib for both uses. It has a built-in toolbar that allows for changing the plot (zoom, pan, etc), and has good support for point picking (clicking on a datapoint to do send an event). You could probably connect the QT slider to the zoom function without much trouble, and Matplotlib is embeddable in QT GUIs. From andrew at acooke.org Wed Jul 1 13:42:06 2009 From: andrew at acooke.org (andrew cooke) Date: Wed, 1 Jul 2009 13:42:06 -0400 (CLT) Subject: Suppressing Implicit Chained Exceptions (Python 3.0) Message-ID: <59a8ef2bf270d290bc5ef5ee98ceb717.squirrel@acooke.dyndns.org> I have some (library) code where an exception is caught and, since the underlying cause is rather obscure, a different exception is raised that more clearly explains the issue to the caller. However, when printed via format_exc(), this new exception still has the old exception attached via the mechanism described at http://www.python.org/dev/peps/pep-3134/ (this is Python 3.0). Is there any simple way to stop this? It's rather annoying and misleading, as it exposes a lot of internal detail that the caller does not understand or want. This is marked as an "open issue" in the PEP described above. Thanks, Andrew From __peter__ at web.de Wed Jul 1 13:44:56 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 01 Jul 2009 19:44:56 +0200 Subject: logging module and binary strings References: Message-ID: Frank Aune wrote: >>>> import logging >>>> logging.basicConfig(level=logging.DEBUG) >>>> x='\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> x > '\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> logging.info(x) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 10: > ordinal not in range(128) >>>> logging.info(x, extra={'encoding':'utf-8'}) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'utf8' codec can't decode byte 0xfe in position 10: > unexpected code byte > Is there any way to log the above value of x "properly" using the python > logging module? By properly I mean logging the value displayed when > entering just x in the python shell and pressing enter. How about logging.info(repr(x))? Peter From Scott.Daniels at Acm.Org Wed Jul 1 13:47:47 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 01 Jul 2009 10:47:47 -0700 Subject: Basic question from pure beginner In-Reply-To: References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: Charles Yeomans wrote: > Let me offer a bit of editing.... > Finally, I'd remove correct_password_given from the loop test, and > replace it with a break statement when the correct password is entered. > > password = "qwerty" > correct_password_given = False > attemptcount = 0 > MaxAttempts = 3 > while attemptcount < MaxAttempts: > guess = raw_input("Enter your password: ") > guess = str(guess) > if guess != password: > print "Access Denied" > attemptcount = attemptcount + 1 > else: > print "Password Confirmed" > correct_password_given = True > break And even simpler: PASSWORD = "qwerty" MAXRETRY = 3 for attempt in range(MAXRETRY): if raw_input('Enter your password: ') == PASSWORD: print 'Password confirmed' break # this exits the for loop print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) else: # The else for a for statement is not executed for breaks, # So indicates the end of testing without a match raise SystemExit # Or whatever you'd rather do. --Scott David Daniels Scott.Daniels at Acm.Org From no.email at please.post Wed Jul 1 13:56:55 2009 From: no.email at please.post (kj) Date: Wed, 1 Jul 2009 17:56:55 +0000 (UTC) Subject: Why re.match()? Message-ID: For a recovering Perl-head like me it is difficult to understand why Python's re module offers both match and search. Why not just use search with a beginning-of-string anchor? I find it particularly puzzling because I have this (possibly mistaken) idea that the Python design philosophy tends towards minimalism, a sort of Occam's razor, when it comes to language entities; i.e. having re.match along with re.search seems to me like an "unnecessary multiplication of entities". What am I missing? TIA! kj From timmyt at smarttypes.org Wed Jul 1 13:58:33 2009 From: timmyt at smarttypes.org (timmyt) Date: Wed, 1 Jul 2009 10:58:33 -0700 (PDT) Subject: a little wsgi framework Message-ID: i'm interested in getting opinions on a small wsgi framework i assembled from webob, sqlalchemy, genshi, and various code fragments i found on the inter-tubes here is the interesting glue - any comments / suggestions would be much appreciated ------------------ the wsgi app ------------------ def application(environ, start_response): path = environ.get('PATH_INFO', '').lstrip('/') for regex, callback in urls: match = re.search(regex, path) if match: environ['myapp.url_args'] = match.groups() request = webob.Request(environ) try: return callback(request, start_response) except Exception, ex: start_response('500 Internal Server Error', [('Content- Type', 'text/plain')]) return [traceback.format_exc()] start_response('404 Not Found', [('Content-Type', 'text/plain')]) return ["Couldn't find the URL specified."] ---------------------------------- the controller decorator ---------------------------------- def web_decorator(filename, method='html'): def decorator(target): def wrapper(request, start_response): #genshi TemplateLoader template = loader.load(filename) global config try: return_dict = target(request, start_response) return_string = template.generate(**return_dict).render (method) config['database.Session'].commit() except: config['database.Session'].rollback() raise finally: config['database.Session'].remove() #TODO: alter 'Content-Type' per method being passed start_response('200 OK', [('Content-Type', 'text/html')]) return [return_string] return wrapper return decorator From lists at cheimes.de Wed Jul 1 14:01:55 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 01 Jul 2009 20:01:55 +0200 Subject: A question about fill_free_list(void) function In-Reply-To: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Message-ID: Pedram schrieb: > Hello community, > I'm reading the CPython interpreter source code, > first, if you have something that I should know for better reading > this source code, I would much appreciate that :) > second, in intobject.c file, I read the following code in > fill_free_list function that I couldn't understand: > while (--q > p) > Py_TYPE(q) = (struct _typeobject *)(q-1); > Py_TYPE(q) = NULL; > > What does it mean? The code is abusing the ob_type member to store a single linked, NULL terminated list of free integer objects. It's a tricky optimization you don't have to understand in order to understand the rest of the code. And no, the code in ctypes.h is totally unrelated to the free list in intobject.c. Christian From robert.kern at gmail.com Wed Jul 1 14:33:53 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 01 Jul 2009 13:33:53 -0500 Subject: deleting certain entries in numpy array In-Reply-To: <4a4b7763$1@news.fhg.de> References: <4a4b7763$1@news.fhg.de> Message-ID: On 2009-07-01 09:51, Sebastian Schabe wrote: > Hello everybody, > > I'm new to python and numpy and have a little/special problem: You will want to ask numpy questions on the numpy mailing list. http://www.scipy.org/Mailing_Lists > I have an numpy array which is in fact a gray scale image mask, e.g.: > > mask = > array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 255, 255, 255, 0, 0, 255, 0], > [ 0, 0, 255, 255, 255, 0, 0, 255, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0], > [ 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) > > and I have another array of (y, x) positions in that mask (first two > values of each row): > > pos = > array([[ 3., 2., 0., 0.], > [ 3., 4., 0., 0.], > [ 5., 2., 0., 0.], > [ 5., 4., 0., 0.], > [ 6., 2., 0., 0.], > [ 6., 7., 0., 0.], > [ 0., 0., 0., 0.], > [ 8., 8., 0., 0.]]) > > and now I only want to keep all lines from 2nd array pos with those > indices that are nonzero in the mask, i.e. line 3-6 (pos[2]-pos[5]). > > F.e. line 4 in pos has the values (5, 4) and mask[5][4] is nonzero, so I > want to keep it. While line 2 (pos[1]) has the values (4, 6) and > mask[4][6] is zero, so shall be discarded. > > I want to avoid a for loop (if possible!!!) cause I think (but don't > know) numpy array are handled in another way. I think numpy.delete is > the right function for discarding the values, but I don't know how to > build the indices. First, convert the pos array to integers, and just the columns with indices in them: ipos = pos[:,:2].astype(int) Now check the values in the mask corresponding to these positions: mask_values = mask[ipos[:,0], ipos[:,1]] Now extract the rows from the original pos array where mask_values is nonzero: result = pos[mask_values != 0] -- 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 backup95 at netcabo.pt Wed Jul 1 14:45:26 2009 From: backup95 at netcabo.pt (=?UTF-8?B?Sm/Do28gVmFsdmVyZGU=?=) Date: Wed, 01 Jul 2009 19:45:26 +0100 Subject: No trees in the stdlib? In-Reply-To: References: Message-ID: <4A4BAEC6.1060109@netcabo.pt> Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > > >> Simple example usage case: Insert string into data structure in sorted >> order if it doesn't exist, else retrieve it. >> > > the_set = set( ... ) > > if str in the_set : > ... "retrieval" case ... > else : > the_set.add(str) > #end if > > Want sorted order? > > sorted(tuple(the_set)) > > What could be simpler? > > Try putting that inside a loop with thousands of iterations and you'll see what the problem is. From duncan.booth at invalid.invalid Wed Jul 1 14:52:34 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 1 Jul 2009 18:52:34 GMT Subject: Why re.match()? References: Message-ID: kj wrote: > > > For a recovering Perl-head like me it is difficult to understand > why Python's re module offers both match and search. Why not just > use search with a beginning-of-string anchor? I find it particularly > puzzling because I have this (possibly mistaken) idea that the > Python design philosophy tends towards minimalism, a sort of Occam's > razor, when it comes to language entities; i.e. having re.match > along with re.search seems to me like an "unnecessary multiplication > of entities". What am I missing? > RTFM? http://docs.python.org/library/re.html#matching-vs-searching says: > Note that match may differ from search even when using a regular > expression beginning with '^': '^' matches only at the start of the > string, or in MULTILINE mode also immediately following a newline. The > ?match? operation succeeds only if the pattern matches at the start of > the string regardless of mode, or at the starting position given by > the optional pos argument regardless of whether a newline precedes > it. So, for example: >>> re.compile("c").match("abcdef", 2) <_sre.SRE_Match object at 0x0000000002C09B90> >>> re.compile("^c").search("abcdef", 2) >>> The ^ anchors you to the start of the string (or start of line in multiline mode) even if you specify a non-zero position. The match method just tells you if the pattern matches at the specified starting position regardless of whether it is the start of the string. From damienlmoore at gmail.com Wed Jul 1 15:30:51 2009 From: damienlmoore at gmail.com (spillz) Date: Wed, 1 Jul 2009 12:30:51 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: On Jun 29, 3:15?pm, Pascal Chambon wrote: > Hello everyone > > I've had real issues with subprocesses recently : from a python script, > on windows, I wanted to "give control" to a command line utility, i.e > forward user in put to it and display its output on console. It seems > simple, but I ran into walls : If you are willing to have a wxPython dependency, wx.Execute handles non-blocking i/o with processes on all supported platforms more discussion of python's blocking i/o issues here: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a037349e18f99630/60886b8beb55cfbc?q=#60886b8beb55cfbc From melissa at corsairsolutions.com Wed Jul 1 15:57:35 2009 From: melissa at corsairsolutions.com (Melissa Powers) Date: Wed, 1 Jul 2009 15:57:35 -0400 Subject: Python 2.5 incompatible with Fedora Core 6 - packaging problems again Message-ID: <51BDF10DBC00564DBBBE62B537CF53922B9ADD@csi-svr1.csi.local> Hi, This may be a stretch but I came across this blog while searching for a SW Packaging Engineer. Do you know anyone in the Boston, MA area who may be interested in this position? The company is a start up with a lot of funding and this is an opportunity to define the environment and grow with a promising company. Please4= forward to anyone you may know of... SW Packaging Engineer * Python developer * Red Hat Applications expert (Fedora 10) * They want a real Red Hat Linux expert * Custom Build Distribution * Custom RPM's * Some Linux Server administration duties * Virtualization experience a big plus Melissa Frechette Powers Account Manager Corsair Solutions, Inc. 2 New Pasture Rd Unit 1 Newburyport, MA 01950 978-465-0085 x 206 Phone 978-465-0068 Fax http://www.corsairsolutions.com Please visit our NEW website: www.corsairsolutions.com The documents included with this electronic mail transmission contain information from the staffing firm of Corsair Solutions, Inc which is confidential and/or privileged. This information is intended to be for the use of the addressee only. Note that any disclosure, printing, photocopying, distribution or use of the contents of this e-mailed information by persons other than the addressee or an agent of the addressee, is unauthorized and prohibited. If you have received this electronic mail in error, please notify us via electronic mail reply to the sender or by telephone (collect 877-626-7724) immediately. -------------- next part -------------- An HTML attachment was scrubbed... URL: From stef.mientki at gmail.com Wed Jul 1 16:08:13 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Wed, 01 Jul 2009 22:08:13 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? Message-ID: <4A4BC22D.70502@gmail.com> hello, I need to add an object's name to the global namespace. The reason for this is to create an environment, where you can add some kind of math environment, where no need for Python knowledge is needed. The next statement works, but I'm not sure if it will have any dramatical side effects, other than overruling a possible object with the name A def some_function ( ...) : A = object ( ...) sys._getframe(1).f_globals [ Name ] = A thanks, Stef Mientki From ben+python at benfinney.id.au Wed Jul 1 16:16:43 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 02 Jul 2009 06:16:43 +1000 Subject: deleting certain entries in numpy array References: <4a4b7763$1@news.fhg.de> Message-ID: <87fxdgtaz8.fsf@benfinney.id.au> Sebastian Schabe writes: > I want to avoid a for loop (if possible!!!) cause I think (but don't > know) numpy array are handled in another way. Yes, Numpy arrays can be indexed logically by a boolean array. > I think numpy.delete is the right function for discarding the values, > but I don't know how to build the indices. You don't need to discard the values. You can get a new array which is a filtered version of an existing array, by using an array of bool values as the index to the old array:: >>> import numpy >>> mask = numpy.array([ ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 255, 255, 255, 0, 0, 255, 0], ... [ 0, 0, 255, 255, 255, 0, 0, 255, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... [ 0, 0, 0, 0, 0, 0, 0, 0, 0], ... ], dtype=numpy.uint8) >>> mask > 0 array([[False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, True, True, True, False, False, True, False], [False, False, True, True, True, False, False, True, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False], [False, False, False, False, False, False, False, False, False]], dtype=bool) >>> mask[mask > 0] array([255, 255, 255, 255, 255, 255, 255, 255], dtype=uint8) However, your case is somewhat more tricky: you need to construct the boolean array based on coordinates from a separate array. That doesn't require a for loop statement, but AFAICT it does require manually generating the array of bools. I've done it with a list comprehension:: >>> pos = numpy.array([ ... [ 3., 2., 0., 0.], ... [ 3., 4., 0., 0.], ... [ 5., 2., 0., 0.], ... [ 5., 4., 0., 0.], ... [ 6., 2., 0., 0.], ... [ 6., 7., 0., 0.], ... [ 0., 0., 0., 0.], ... [ 8., 8., 0., 0.], ... ]) >>> pos[numpy.array( ... [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]])] array([[ 5., 2., 0., 0.], [ 5., 4., 0., 0.], [ 6., 2., 0., 0.], [ 6., 7., 0., 0.]]) Here it is again, showing my working steps:: >>> pos[:, 0:2] array([[ 3., 2.], [ 3., 4.], [ 5., 2.], [ 5., 4.], [ 6., 2.], [ 6., 7.], [ 0., 0.], [ 8., 8.]]) >>> [(int(x), int(y)) for (x, y) in pos[:, 0:2]] [(3, 2), (3, 4), (5, 2), (5, 4), (6, 2), (6, 7), (0, 0), (8, 8)] >>> [mask[int(x), int(y)] for (x, y) in pos[:, 0:2]] [0, 0, 255, 255, 255, 255, 0, 0] >>> [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]] [False, False, True, True, True, True, False, False] >>> numpy.array( ... [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]]) array([False, False, True, True, True, True, False, False], dtype=bool) >>> pos[numpy.array( ... [mask[int(x), int(y)] > 0 for (x, y) in pos[:, 0:2]])] array([[ 5., 2., 0., 0.], [ 5., 4., 0., 0.], [ 6., 2., 0., 0.], [ 6., 7., 0., 0.]]) -- \ ?Holy knit one purl two, Batman!? ?Robin | `\ | _o__) | Ben Finney From Digvijoy.C at gmail.com Wed Jul 1 16:20:03 2009 From: Digvijoy.C at gmail.com (digz) Date: Wed, 1 Jul 2009 13:20:03 -0700 (PDT) Subject: problems displaying results in ibm_db Message-ID: This simple db connectivity and select test program works in the interactive mode >>> import ibm_db >>> conn = ibm_db.connect( 'DATABASE', 'user', 'passwd') >>> result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST 1 ROWS ONLY') >>> row = ibm_db.fetch_tuple( result ) >>> for r in row: ... print r ... 13 ABC 4 2009-05-11 The same executed from a script using python testdb.py does not yield anything , What am I doing wrong ? import ibm_db conn = ibm_db.connect( 'DATABASE', 'user', 'pwd') result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST 1 ROWS ONLY') row = ibm_db.fetch_tuple( result ) for r in row: print r == >python testdb.py database value after unicode conversion is : N database value sent to CLI is : N > == From psimon at sonic.net Wed Jul 1 17:16:18 2009 From: psimon at sonic.net (Paul Simon) Date: Wed, 1 Jul 2009 14:16:18 -0700 Subject: problem installing python 2.6.2 from tarball Message-ID: <4a4bd226$0$95520$742ec2ed@news.sonic.net> I have just finished going through the usual ./configure, make, etc. and now have a load of stuff in my home directory that I think doesn't belong there. Apparently I did the installation from my home directory (linux) and have a directory in my home directory "Python2.6.2" with subdirectories like "build", "Demo", "Doc", "Include", "Lib", etc. There are many files under /usr/bin/local/ which appear to be duplicates. This looks like a mess to me and would like some help to sort this out. Paul Simon From Mariosky at gmail.com Wed Jul 1 17:34:51 2009 From: Mariosky at gmail.com (Mario Garcia) Date: Wed, 1 Jul 2009 14:34:51 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order Message-ID: Im trying to use sets for doing statistics from a data set. I want to select, 70% random records from a List. I thougth set where a good idea so I tested this way: c = set(range(1000)) for d in range(1000): print c.pop() I was hoping to see a print out of random selected numbers from 1 to 1000 but I got an ordered count from 1 to 1000. I also tried using a dictionary, with keys from 1 to 10, and also got the keys in order. Im using: Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug 4 2008, 13:45:20) [GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin Examples in the documentation seem to work. But I cant make it. Can some one, give me a hint on whats going on? From lance.ellinghaus at hp.com Wed Jul 1 18:02:49 2009 From: lance.ellinghaus at hp.com (ELLINGHAUS, LANCE) Date: Wed, 1 Jul 2009 22:02:49 +0000 Subject: Using Python to set desktop background image under Windows XP In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <197A4D1DF629F64A9260E3B454A218D13403C48D2E@GVW1158EXB.americas.hpqcorp.net> Does anyone have any code that would allow setting the desktop background image under Windows XP? Thank you, lance Lance Ellinghaus mailto:lance.ellinghaus at hp.com From skip at pobox.com Wed Jul 1 18:07:16 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 1 Jul 2009 17:07:16 -0500 Subject: Timeout when connecting to sybase DBS In-Reply-To: <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> Message-ID: <19019.56852.751152.510037@montanaro.dyndns.org> Gil> Are you saying, that when you trying to connect to a sybase DBS Gil> server and the DBS or the server is down, you get an error after a Gil> few seconds and not after a few minutes? Yes, though thankfully our server tends to almost always be up. Gil> Which python version are you using? We're still running 2.4.5 at work and have a slightly hacked very old version of the python-sybase package (0.36). -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ when i wake up with a heart rate below 40, i head right for the espresso machine. -- chaos @ forums.usms.org From rhodri at wildebst.demon.co.uk Wed Jul 1 18:08:33 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 01 Jul 2009 23:08:33 +0100 Subject: Python/Pygame question In-Reply-To: References: Message-ID: On Tue, 30 Jun 2009 19:15:24 +0100, Crip wrote: > I have been experimenting with pygame. I would like to know how to go > about placing an image of my creation as the background of the > generated window. To where should I save the image, and what should it > be saved as? Wherever will be most useful to you (the same directory as where you are developing your game is often good). You can use most of the common formats, so pick whatever's most appropriate for your image. The documentation is at http://www.pygame.org/docs/ref/image.html In particular, you use pygame.image.load() to load your file into a surface, then blit it into your display. Some resizing may be necessary! -- Rhodri James *-* Wildebeest Herder to the Masses From skip at pobox.com Wed Jul 1 18:08:57 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 1 Jul 2009 17:08:57 -0500 Subject: Timeout when connecting to sybase DBS In-Reply-To: <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> Message-ID: <19019.56953.762133.210100@montanaro.dyndns.org> Gil> There's no such group as python-sybase :-( http://sourceforge.net/mailarchive/forum.php?forum_name=python-sybase-misc S From clp2 at rebertia.com Wed Jul 1 18:11:45 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 1 Jul 2009 15:11:45 -0700 Subject: Noob In-Reply-To: References: Message-ID: <50697b2c0907011511r6aac78d9m3f190c19b79ed832@mail.gmail.com> On Tue, Jun 30, 2009 at 11:02 AM, kaffeen wrote: > Hello all, just joined this list and am just beginning to learn Python. > Thanks to everyone for making this a great place to learn and their > contributions. > > As mentioned, I'm new to Python (but have experience with other programming > languages/scripting). I have a couple of questions... > 2) What are the differences between IPython and IDLE, is one better than the > other, why? IDLE is a GUI interactive interpreter and IDE that uses the Tk GUI toolkit. IPython is a command-line interactive interpreter with tab-completion, advanced command history functions, and other handy extras. I would say they have completely different purposes. IPython is a souped-up version of the command-line Python REPL, but doesn't include a text editor for writing a file of code. It is very good for experimenting and exploring with though. IDLE is a quite servicable free IDE. Cheers, Chris -- http://blog.rebertia.com From pavlovevidence at gmail.com Wed Jul 1 18:28:10 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 15:28:10 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> On Jul 1, 2:34?pm, Mario Garcia wrote: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): > ? ? ?print c.pop() > > I was hoping to see a print out of random selected numbers from 1 to > 1000 > but I got an ordered count from 1 to 1000. > I also tried using a dictionary, with keys from 1 to 10, and also got > the keys in order. > > Im using: > ?Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug ?4 2008, 13:45:20) > ?[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin > > Examples in the documentation seem to work. But I cant make it. > Can some one, give me a hint on whats going on? The keys in a dict or set are not in random order, but (more or less) they are in hash key order modulo the size of the hash. This neglects the effect of hash collisions. The hash code of an integer happens to the integer itself, so oftentimes a dict or set storing a sequence of integers will end up with keys in order, although it's not guaranteed to be so. Point it, it's unsafe to rely on *any* ordering behavior in a dict or set, even relatively random order. Instead, call random.shuffle() on the list, and iterate through that to get the elements in random order. Carl Banks From mensanator at aol.com Wed Jul 1 18:29:19 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 1 Jul 2009 15:29:19 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <51a36108-d3dc-4a03-9a85-84a5da9b541a@l31g2000yqb.googlegroups.com> On Jul 1, 4:34?pm, Mario Garcia wrote: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): > ? ? ?print c.pop() > > I was hoping to see a print out of random selected numbers from 1 to > 1000 > but I got an ordered count from 1 to 1000. > I also tried using a dictionary, with keys from 1 to 10, and also got > the keys in order. > > Im using: > ?Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug ?4 2008, 13:45:20) > ?[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin > > Examples in the documentation seem to work. But I cant make it. > Can some one, give me a hint on whats going on? Sets don't help. Try this: >>> import random >>> c = range(100) >>> c [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> random.shuffle(c) >>> c [78, 62, 38, 54, 12, 48, 24, 20, 8, 14, 1, 69, 49, 92, 41, 64, 17, 35, 88, 40, 73, 45, 5, 84, 96, 90, 98, 57, 51, 75, 99, 13, 29, 4, 97, 77, 74, 56, 91, 95, 59, 79, 89, 19, 9, 42, 31, 85, 86, 23, 27, 50, 6, 21, 15, 80, 3, 30, 87, 82, 16, 63, 2, 55, 37, 33, 10, 61, 93, 72, 60, 67, 44, 65, 11, 70, 52, 58, 47, 18, 36, 66, 94, 28, 22, 68, 32, 76, 53, 25, 83, 34, 26, 71, 39, 43, 7, 46, 0, 81] >>> for d in c: print c.pop(), 81 0 46 7 43 39 71 26 34 83 25 53 76 32 68 22 28 94 66 36 18 47 58 52 70 11 65 44 67 60 72 93 61 10 33 37 55 2 63 16 82 87 30 3 80 15 21 6 50 27 Notice that the numbers are coming out in the exact reverse order of the list, because you used .pop() without an index number. But it's ok in _THIS_ example, because the list was randomly shuffled before popping. From pavlovevidence at gmail.com Wed Jul 1 18:33:01 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 15:33:01 -0700 (PDT) Subject: Why re.match()? References: Message-ID: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> On Jul 1, 10:56?am, kj wrote: > For a recovering Perl-head like me it is difficult to understand > why Python's re module offers both match and search. ?Why not just > use search with a beginning-of-string anchor? ?I find it particularly > puzzling because I have this (possibly mistaken) idea that the > Python design philosophy tends towards minimalism, a sort of Occam's > razor, when it comes to language entities; i.e. having re.match > along with re.search seems to me like an "unnecessary multiplication > of entities". ?What am I missing? It always seemed redundant to me also (notwithstanding Duncan Booth's explanation of slight semantic differences). However, I find myself using re.match much more often than re.search, so perhaps in this case a "second obvious way" is justified. Carl Banks From aahz at pythoncraft.com Wed Jul 1 18:39:55 2009 From: aahz at pythoncraft.com (Aahz) Date: 1 Jul 2009 15:39:55 -0700 Subject: Problem with uuid package when embedding a python interpreter References: <7d5cfbf0-38d5-4505-a93a-f321d0da74b8@c36g2000yqn.googlegroups.com> Message-ID: In article <7d5cfbf0-38d5-4505-a93a-f321d0da74b8 at c36g2000yqn.googlegroups.com>, =?ISO-8859-1?Q?J=E9r=F4me_Fuselier?= wrote: > >I've tried to import a script in an embedded python intrepreter but >this script fails when it imports the uuid module. I have a >segmentation fault in Py_Finalize(). You may want to also ask on the capi-sig mailing list. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From mensanator at aol.com Wed Jul 1 18:47:08 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 1 Jul 2009 15:47:08 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <51a36108-d3dc-4a03-9a85-84a5da9b541a@l31g2000yqb.googlegroups.com> Message-ID: <6891d82f-9dc3-4f43-8490-c339edf85ad8@p23g2000vbl.googlegroups.com> On Jul 1, 5:29?pm, Mensanator wrote: > On Jul 1, 4:34?pm, Mario Garcia wrote: > > > > > > > Im trying to use sets for doing statistics from a data set. > > I want to select, 70% random records from a List. I thougth set where > > a good idea so I > > tested this way: > > > c = set(range(1000)) > > for d in range(1000): > > ? ? ?print c.pop() > > > I was hoping to see a print out of random selected numbers from 1 to > > 1000 > > but I got an ordered count from 1 to 1000. > > I also tried using a dictionary, with keys from 1 to 10, and also got > > the keys in order. > > > Im using: > > ?Python 2.5.2 |EPD 2.5.2001| (r252:60911, Aug ?4 2008, 13:45:20) > > ?[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin > > > Examples in the documentation seem to work. But I cant make it. > > Can some one, give me a hint on whats going on? > > Sets don't help. Try this: > > >>> import random > >>> c = range(100) > >>> c > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, > 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, > 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, > 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, > 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, > 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] > > >>> random.shuffle(c) > >>> c > > [78, 62, 38, 54, 12, 48, 24, 20, 8, 14, 1, 69, 49, 92, 41, 64, 17, 35, > 88, 40, 73, 45, 5, 84, 96, 90, 98, 57, 51, 75, 99, 13, 29, 4, 97, 77, > 74, 56, 91, 95, 59, 79, 89, 19, 9, 42, 31, 85, 86, 23, 27, 50, 6, 21, > 15, 80, 3, 30, 87, 82, 16, 63, 2, 55, 37, 33, 10, 61, 93, 72, 60, 67, > 44, 65, 11, 70, 52, 58, 47, 18, 36, 66, 94, 28, 22, 68, 32, 76, 53, > 25, 83, 34, 26, 71, 39, 43, 7, 46, 0, 81] > > >>> for d in c: Oops! You don't want to iterate through c while popping it. Your original for statement is what to use. > > ? ? ? ? print c.pop(), > > 81 0 46 7 43 39 71 26 34 83 25 53 76 32 68 22 28 94 66 36 18 47 58 52 > 70 11 65 44 67 60 72 93 61 10 33 37 55 2 63 16 82 87 30 3 80 15 21 6 > 50 27 > > Notice that the numbers are coming out in the exact reverse order > of the list, because you used .pop() without an index number. > But it's ok in _THIS_ example, because the list was randomly > shuffled before popping. From ben+python at benfinney.id.au Wed Jul 1 19:01:10 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 02 Jul 2009 09:01:10 +1000 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <87bpo4t3d5.fsf@benfinney.id.au> Mario Garcia writes: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I > tested this way: > > c = set(range(1000)) > for d in range(1000): > print c.pop() > > I was hoping to see a print out of random selected numbers from 1 to > 1000 The ?set? and ?dict? types are unordered collections, but that doesn't have any implication of randomness. Rather, it means that the order of retrieval is not guaranteed to be predictable. You can't even predict that it'll be a random order; you can't predict (based on the Python code) *anything* about the order. Any appearance of predictable ordering is an unreliable artefact of the implementation and may change at any time, since the language explicitly disclaims any specific ordering to the items. For a random sequence, you want the ?random? module :: >>> import random >>> for n in (random.randint(0, 999) for _ in range(10)): ... print n ... 381 4 471 624 70 979 110 932 105 262 -- \ ?When in doubt tell the truth. It will confound your enemies | `\ and astound your friends.? ?Mark Twain, _Following the Equator_ | _o__) | Ben Finney From alex.lavoro.propio at gmail.com Wed Jul 1 19:18:13 2009 From: alex.lavoro.propio at gmail.com (Alex) Date: Wed, 1 Jul 2009 16:18:13 -0700 (PDT) Subject: Open Source RSS Reader in Python? Message-ID: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> I am looking for an open source RSS reader (desktop, not online) written in Python but in vain. I am not looking for a package but a fully functional software. Google: python "open source" (rss OR feeds) reader Any clue ? From tn.pablo at gmail.com Wed Jul 1 19:26:36 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Wed, 1 Jul 2009 18:26:36 -0500 Subject: problem installing python 2.6.2 from tarball In-Reply-To: <4a4bd226$0$95520$742ec2ed@news.sonic.net> References: <4a4bd226$0$95520$742ec2ed@news.sonic.net> Message-ID: On Wed, Jul 1, 2009 at 16:16, Paul Simon wrote: > I have just finished going through the usual ./configure, make, etc. and now > have a load of stuff in my home directory that I think doesn't belong there. > Apparently I did the installation from my home directory (linux) and have a > directory ?in my home directory "Python2.6.2" with subdirectories like > "build", "Demo", "Doc", "Include", "Lib", etc. ?There are many files under > /usr/bin/local/ ?which appear to be duplicates. > > This looks like a mess to me and would like some help to sort this out. > > Paul Simon Sounds like you untarred directly into your home dir. You're OK if you delete those, they are the sources, although you should do so only after uninstalling, just in case. To unistall, follow this thread from the list from a while ago: http://mail.python.org/pipermail/python-list/2004-December/296269.html I have followed it and it worked. Pablo Torres N. From tn.pablo at gmail.com Wed Jul 1 19:34:18 2009 From: tn.pablo at gmail.com (ptn) Date: Wed, 1 Jul 2009 16:34:18 -0700 (PDT) Subject: problems displaying results in ibm_db References: Message-ID: <052ecdf2-6253-4885-9f16-2f24062f1f07@g6g2000vbr.googlegroups.com> On Jul 1, 3:20?pm, digz wrote: > result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST > 1 ROWS ONLY') You have the same string split into two lines, which means that what you actually have is this: result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST 1 ROWS ONLY') You need to escape that little '\n' with a backslash: result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST\ 1 ROWS ONLY') Better yet, use implicit string concatenation: result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST' '1 ROWS ONLY') Note that now you have two strings: 'SELECT * FROM TEST FETCH FIRST' and '1 ROWS ONLY' Pablo Torres N. From tn.pablo at gmail.com Wed Jul 1 19:36:38 2009 From: tn.pablo at gmail.com (ptn) Date: Wed, 1 Jul 2009 16:36:38 -0700 (PDT) Subject: problem installing python 2.6.2 from tarball References: <4a4bd226$0$95520$742ec2ed@news.sonic.net> Message-ID: <9286d6b5-91bf-4a74-99ba-c7c90329d4aa@f33g2000vbm.googlegroups.com> Sounds like you untarred directly into your home dir. You're OK if you delete those, they are the sources, although you should do so only after uninstalling, just in case. To unistall, follow this thread from the list from a while ago: http://mail.python.org/pipermail/python-list/2004-December/296269.html I have followed it and it worked. From sajmikins at gmail.com Wed Jul 1 19:39:31 2009 From: sajmikins at gmail.com (Simon Forman) Date: Wed, 1 Jul 2009 16:39:31 -0700 (PDT) Subject: Open Source RSS Reader in Python? References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: On Jul 1, 7:18?pm, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? There's Canto: http://codezen.org/canto/ From python at mrabarnett.plus.com Wed Jul 1 19:50:16 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 00:50:16 +0100 Subject: Why re.match()? In-Reply-To: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> References: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> Message-ID: <4A4BF638.5060500@mrabarnett.plus.com> Carl Banks wrote: > On Jul 1, 10:56 am, kj wrote: >> For a recovering Perl-head like me it is difficult to understand >> why Python's re module offers both match and search. Why not just >> use search with a beginning-of-string anchor? I find it particularly >> puzzling because I have this (possibly mistaken) idea that the >> Python design philosophy tends towards minimalism, a sort of Occam's >> razor, when it comes to language entities; i.e. having re.match >> along with re.search seems to me like an "unnecessary multiplication >> of entities". What am I missing? > > It always seemed redundant to me also (notwithstanding Duncan Booth's > explanation of slight semantic differences). However, I find myself > using re.match much more often than re.search, so perhaps in this case > a "second obvious way" is justified. > re.match is anchored at a certain position, whereas re.search isn't. The re module doesn't support Perl's \G anchor. From robert.kern at gmail.com Wed Jul 1 19:51:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 01 Jul 2009 18:51:31 -0500 Subject: logging module and binary strings In-Reply-To: <200907011702.21450.Frank.Aune@broadpark.no> References: <200907011702.21450.Frank.Aune@broadpark.no> Message-ID: On 2009-07-01 10:02, Frank Aune wrote: > Hello, > > ---- snip ---- >>>> import logging >>>> logging.basicConfig(level=logging.DEBUG) >>>> x='\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> x > '\xfe\x9f\xce\xc3\xa1\x00\xff\x01' >>>> print x > ????? >>>> logging.info(x) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 10: > ordinal not in range(128) >>>> logging.info(x, extra={'encoding':'utf-8'}) > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit > stream.write(fs % msg.encode("UTF-8")) > UnicodeDecodeError: 'utf8' codec can't decode byte 0xfe in position 10: > unexpected code byte > > ---- snip ---- > > Is there any way to log the above value of x "properly" using the python > logging module? By properly I mean logging the value displayed when entering > just x in the python shell and pressing enter. logging.info(repr(x)) Or, to be more descriptive in your logging message: logging.info("Got a binary value: %r", x) -- 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 benjamin at python.org Wed Jul 1 19:55:23 2009 From: benjamin at python.org (Benjamin Peterson) Date: Wed, 1 Jul 2009 23:55:23 +0000 (UTC) Subject: getting rid of =?utf-8?b?4oCU?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Message-ID: someone googlemail.com> writes: > > Hello, > > how can I replace '?' sign from string? Or do split at that character? > Getting unicode error if I try to do it: > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > 1: ordinal not in range(128) Please paste your code. I suspect that you are mixing unicode and normal strings. From python at mrabarnett.plus.com Wed Jul 1 19:56:22 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 00:56:22 +0100 Subject: getting rid of =?windows-1252?Q?=97?= In-Reply-To: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Message-ID: <4A4BF7A6.9010500@mrabarnett.plus.com> someone wrote: > Hello, > > how can I replace '?' sign from string? Or do split at that character? > Getting unicode error if I try to do it: > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > 1: ordinal not in range(128) > > > Thanks, Pet > > script is # -*- coding: UTF-8 -*- It sounds like you're mixing bytestrings with Unicode strings. I can't be any more helpful because you haven't shown the code. From python at mrabarnett.plus.com Wed Jul 1 20:26:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 01:26:09 +0100 Subject: Getting input the scanf way In-Reply-To: <8f67b6f80907010133t7f6e2d9eg9ffa3b927d540728@mail.gmail.com> References: <8f67b6f80907010133t7f6e2d9eg9ffa3b927d540728@mail.gmail.com> Message-ID: <4A4BFEA1.6020301@mrabarnett.plus.com> Mr.SpOOn wrote: > Hi, > I need to do some kind of interactive command line program. > > I mean: I run the program, it asks me for input, I type something and > then I get the output or other questions. > I'm not sure what is the right way to achieve this. > Use raw_input() and print. From benjamin.kaplan at case.edu Wed Jul 1 20:27:33 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 1 Jul 2009 20:27:33 -0400 Subject: What are the limitations of cStringIO.StringIO() ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: On Wed, Jul 1, 2009 at 7:48 AM, Barak, Ron wrote: > Hi, > > I think I'm up against a limitation in cStringIO.StringIO(), but could not > find any clues on the web, especially not in > http://docs.python.org/library/stringio.html. > > What I have is the following function: > > ??? def concatenate_files(self,filename_array): > ??????? import types > > ??????? f = cStringIO.StringIO() > ??????? for filename in filename_array: > ??????????? log_stream = LogStream(filename) > ??????????? if not isinstance(log_stream, types.NoneType): > ??????????????? try: > ??????????????????? string_ = log_stream.input_file.read() > ??????????????? except AttributeError, e: > ??????????????????????? sys.stderr.write("AttributeError: "+str(e)+"\n") > ??????????????????????? sys.stderr.write("log_stream: > "+str(log_stream)+"\n") > ??????????? else: > ??????????????? return(None) > > ??????????? f.write(string_) > ??????? return (f) > > And, when the list of files?- in filename_array - is pointing to long/large > enough files, I get the exception: > > Traceback (most recent call last): > ? File "svm_ts_tool_in_progress.py", line 244, in OnSelectCell > ??? self.InspectorViewController(row,col) > ? File "svm_ts_tool_in_progress.py", line 1216, in InspectorViewController > ??? self.InspectorePaneGetRecords(self.req_table, rec) > ? File "svm_ts_tool_in_progress.py", line 1315, in InspectorePaneGetRecords > ??? log_stream = > ConcatenatedLogStream(self.events_dict[req_table]["db_dict"].keys()) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 31, in > __init__ > ??? self.input_file = self.concatenate_files(filename_array) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 47, in > concatenate_files > ??? string_ = log_stream.input_file.read() > MemoryError > > Anyone knows whet's the limitation on cStringIO.StringIO() objects ? > Could you suggest a better way to create a string that contains the > concatenation of several big files ? > MemoryErrors are caused because, for whatever reason, the OS won't allocate any more memory for the process. For a 32-bit Windows process, that maximum is 2 GB. No matter what programming language or library you use, you can't bypass that limit. There is a way to up the limit to 3GB but I don't know how to do it. The link below has all the information about Windows memory limits. If you need lots of really big strings in memory simultaneously, you'll have to get a 64-bit system with a ton of RAM. http://msdn.microsoft.com/en-us/library/aa366778(VS.85).aspx > Thanks, > Ron. > > -- > http://mail.python.org/mailman/listinfo/python-list > > From missive at hotmail.com Wed Jul 1 20:37:40 2009 From: missive at hotmail.com (Lee Harr) Date: Thu, 2 Jul 2009 05:07:40 +0430 Subject: [ANNC] acromania-0.4 Message-ID: Acromania is a word game of acronyms. This program is a computer moderator for networked games of acromania. It can connect to a channel on IRC, or start a standalone server which can be accessed much like a MUD. http://acromania.googlecode.com/ Acromania uses Twisted and SQLite. Optionally, it can use NLTK to generate computer opponents. Acromania is released under GPLv3. Changes in acromania-0.4: - autocreate conf.py on first start - initialize database on first start - add contact information - various bug fixes Notes: I have only played the game using the standalone server on a secure network. If you have experience with programming IRC bots securely, please take a look and contact me if you see any problems. If you decide to connect it to IRC, please let me know, because I'd like to play :o) _________________________________________________________________ Drag n? drop?Get easy photo sharing with Windows Live? Photos. http://www.microsoft.com/windows/windowslive/products/photos.aspx From http Wed Jul 1 20:48:46 2009 From: http (Paul Rubin) Date: 01 Jul 2009 17:48:46 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <7xhbxvgb9t.fsf@ruckus.brouhaha.com> Mario Garcia writes: > Im trying to use sets for doing statistics from a data set. > I want to select, 70% random records from a List. I thougth set where > a good idea so I No that's not a good idea. When the set/dict documentation says you get the keys in an undetermined order, it doesn't mean the ordering is random. It means there is a fixed ordering controlled by the implementation and not by you. If you want a random sample, use random.sample(). See the docs for the random module. From http Wed Jul 1 20:49:21 2009 From: http (Paul Rubin) Date: 01 Jul 2009 17:49:21 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> Message-ID: <7xd48jgb8u.fsf@ruckus.brouhaha.com> Carl Banks writes: > Instead, call random.shuffle() on the list, and iterate through that > to get the elements in random order. It's better to use random.sample() than random.shuffle(). From sjmachin at lexicon.net Wed Jul 1 21:20:29 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 1 Jul 2009 18:20:29 -0700 (PDT) Subject: Why re.match()? References: <86cabd92-f41f-4666-a6e6-3bfc4fda41ea@g19g2000yql.googlegroups.com> Message-ID: On Jul 2, 9:50?am, MRAB wrote: > Carl Banks wrote: > > On Jul 1, 10:56 am, kj wrote: > >> For a recovering Perl-head like me it is difficult to understand > >> why Python's re module offers both match and search. ?Why not just > >> use search with a beginning-of-string anchor? ?I find it particularly > >> puzzling because I have this (possibly mistaken) idea that the > >> Python design philosophy tends towards minimalism, a sort of Occam's > >> razor, when it comes to language entities; i.e. having re.match > >> along with re.search seems to me like an "unnecessary multiplication > >> of entities". ?What am I missing? > > > It always seemed redundant to me also (notwithstanding Duncan Booth's > > explanation of slight semantic differences). ?However, I find myself > > using re.match much more often than re.search, so perhaps in this case > > a "second obvious way" is justified. > > re.match is anchored at a certain position, whereas re.search isn't. The > re module doesn't support Perl's \G anchor. re.search is obstinately determined when it comes to flogging dead horses: C:\junk>\python26\python -mtimeit -s"import re" "re.match('xxx', 'y'*100)" 100000 loops, best of 3: 3.37 usec per loop C:\junk>\python26\python -mtimeit -s"import re" "re.search('^xxx', 'y'*100)" 100000 loops, best of 3: 7.01 usec per loop C:\junk>\python26\python -mtimeit -s"import re" "re.match('xxx', 'y'*1000)" 100000 loops, best of 3: 3.85 usec per loop C:\junk>\python26\python -mtimeit -s"import re" "re.search('^xxx', 'y'*1000)" 10000 loops, best of 3: 37.9 usec per loop C:\junk> From magawake at gmail.com Wed Jul 1 21:28:12 2009 From: magawake at gmail.com (Mag Gam) Date: Wed, 1 Jul 2009 21:28:12 -0400 Subject: Multi thread reading a file In-Reply-To: References: Message-ID: <1cbd6f830907011828i789e1f6fk18ce71da7290f6ee@mail.gmail.com> LOL! :-) Why not. I think I will take just do it single thread for now and if performance is really that bad I can re investigate. Either way, thanks everyone for your feedback! I think I like python a lot because of the great user community and wiliness to help! On Wed, Jul 1, 2009 at 1:07 AM, Lawrence D'Oliveiro wrote: > In message , Mag Gam > wrote: > >> I am very new to python and I am in the process of loading a very >> large compressed csv file into another format. ?I was wondering if I >> can do this in a multi thread approach. > > Why bother? > > -- > http://mail.python.org/mailman/listinfo/python-list > From steven at REMOVE.THIS.cybersource.com.au Wed Jul 1 21:28:19 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Jul 2009 01:28:19 GMT Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> Message-ID: On Wed, 01 Jul 2009 17:49:21 -0700, Paul Rubin wrote: > Carl Banks writes: >> Instead, call random.shuffle() on the list, and iterate through that to >> get the elements in random order. > > It's better to use random.sample() than random.shuffle(). Why? -- Steven From pavlovevidence at gmail.com Wed Jul 1 21:29:21 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 18:29:21 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> Message-ID: <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> On Jul 1, 5:49?pm, Paul Rubin wrote: > Carl Banks writes: > > Instead, call random.shuffle() on the list, and iterate through that > > to get the elements in random order. > > It's better to use random.sample() than random.shuffle(). If you're iterating through the whole list and don't need to preserve the original order (as was the case here) random.shuffle() is better. Aren't-absolutist-opinions-cool?-ly yr's, Carl Banks From http Wed Jul 1 21:37:38 2009 From: http (Paul Rubin) Date: 01 Jul 2009 18:37:38 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> Message-ID: <7xr5wzzwyl.fsf@ruckus.brouhaha.com> Carl Banks writes: > If you're iterating through the whole list and don't need to preserve > the original order (as was the case here) random.shuffle() is better. 1. Random.sample avoids iterating through the whole list when it can. 2. Say you want to choose 10 random numbers between 1 and 1000000. random.sample(xrange(1000000), 10) works nicely. Doing the same with shuffle is painful. From pavlovevidence at gmail.com Wed Jul 1 21:46:10 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 18:46:10 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> <7xr5wzzwyl.fsf@ruckus.brouhaha.com> Message-ID: <5572daf2-7149-41a7-97e5-91c4bc1e908f@n16g2000yqm.googlegroups.com> On Jul 1, 6:37?pm, Paul Rubin wrote: > Carl Banks writes: > > If you're iterating through the whole list and don't need to preserve > > the original order (as was the case here) random.shuffle() is better. > > 1. Random.sample avoids iterating through the whole list when it can. > > 2. Say you want to choose 10 random numbers between 1 and 1000000. > > ? ?random.sample(xrange(1000000), 10) > > works nicely. ?Doing the same with shuffle is painful. random.shuffle() is still better when you're iterating through the whole list as the OP was doing. Carl Banks From http Wed Jul 1 22:02:46 2009 From: http (Paul Rubin) Date: 01 Jul 2009 19:02:46 -0700 Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> <7xr5wzzwyl.fsf@ruckus.brouhaha.com> <5572daf2-7149-41a7-97e5-91c4bc1e908f@n16g2000yqm.googlegroups.com> Message-ID: <7xtz1vu9ix.fsf@ruckus.brouhaha.com> Carl Banks writes: > random.shuffle() is still better when you're iterating through the > whole list as the OP was doing. The OP wrote: I want to select, 70% random records from a List. I thougth set where a good idea so I tested this way: ... That sounds like 70% of the list, not the whole list. Note that in addition to using time proportional to the size of the entire lsit rather than the size of the sample, shuffle() also messes up the order of the list. Why would shuffle ever be better? It is designed for a different purpose. Using a function called "sample" when you want to sample should be a no-brainer, and if using shuffle instead is ever preferable, that should be treated as a misfeature in the "sample" implementation, and fixed. From pavlovevidence at gmail.com Wed Jul 1 22:17:56 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 19:17:56 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <12eaea3c-b0f1-4109-a6ef-dc24f2bc7925@n11g2000yqb.googlegroups.com> <7xd48jgb8u.fsf@ruckus.brouhaha.com> <00378c8a-0920-4f2a-9ec0-7abc360d857b@l2g2000vba.googlegroups.com> <7xr5wzzwyl.fsf@ruckus.brouhaha.com> <5572daf2-7149-41a7-97e5-91c4bc1e908f@n16g2000yqm.googlegroups.com> <7xtz1vu9ix.fsf@ruckus.brouhaha.com> Message-ID: <263f472b-3555-4d62-b37d-fb134da5a683@r25g2000vbn.googlegroups.com> On Jul 1, 7:02?pm, Paul Rubin wrote: > Carl Banks writes: > > random.shuffle() is still better when you're iterating through the > > whole list as the OP was doing. > The OP wrote: > > ? ? I want to select, 70% random records from a List. I thougth set > ? ? where a good idea so I tested this way: ... I was going by his example which went through all the items in the list. > That sounds like 70% of the list, not the whole list. ?Note that > in addition to using time proportional to the size of the entire > lsit rather than the size of the sample, shuffle() also messes up > the order of the list. ? > > Why would shuffle ever be better? Because we were talking about different things. Carl Banks From rtm74 at yahoo.com Wed Jul 1 22:38:14 2009 From: rtm74 at yahoo.com (rtm74 at yahoo.com) Date: Wed, 1 Jul 2009 19:38:14 -0700 (PDT) Subject: Newby Windows User Message-ID: <447092.99927.qm@web110201.mail.gq1.yahoo.com> I am new to Python and use Windows XP. I would like to know how to run a python program file? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bpytlik at Sun.COM Wed Jul 1 22:48:04 2009 From: bpytlik at Sun.COM (Brock Pytlik) Date: Wed, 01 Jul 2009 19:48:04 -0700 Subject: System default sys.path Message-ID: <4A4C1FE4.1070901@sun.com> Hi, I'm trying to find a way to get the value sys.path would have on a particular system if python was started with an empty python path. I do want it to include the site specific additional paths. I know I can hack this information myself, but I'd rather be able to get this on demand so that if things change in the future, I don't have grovel around looking for which directory string to change. If nothing else, I think the following would satisfy my needs: 1) a variable containing the directories to use as a prefix (I think sys.exec_prefix and sys.prefix are what I want here) 2) a variable containing the list of suffixes that are applies to the prefixes, like lib/python/site-packages 3) a way of handing the *.pth files Thanks in advance for the help, Brock From pavlovevidence at gmail.com Wed Jul 1 23:18:22 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 1 Jul 2009 20:18:22 -0700 (PDT) Subject: invoking a method from two superclasses References: Message-ID: <24ebfdef-0832-4b78-983a-d886e56160b7@f10g2000vbf.googlegroups.com> On Jun 30, 9:57?pm, Mitchell L Model wrote: > [Continuing the discussion about super() and __init__] > > The documentation of super points out that good design of > diamond patterns require the methods to have the same > signature throughout the diamond. That's fine for non-mixin > classes where the diamond captures different ways of handling > the same data. [snip] > The problem is that each mixin's __init__ is likely to have a > different signature. [snip] Here are my recommendations for mixin classes. 1. Try to write mixins so that they don't need initializer arguments. There's several approaches to this. I prefer to burden the subclass to set any attributes the mixin needs before calling super().__init__ (). class A(SomeMixin,SomeBaseClass): def __init__(self,arg1,arg2): self.some_attribute_needed_by_mixin = 10 super().__init__(arg1,arg2) That might seem a little weird to some (possibly because it is impossible in C++ or Java to do that), but I find it to be a far more versatile way for a subclass to send data to its parent. I use it for regular single inheritance, even. Another possibility is to have a separate mixin initializer that is called separately. 2. The __init__ of a mixin, if it has one, should always take *args and **kwargs arguments, and only those arguments, and pass them as-is to the superclass. class SomeMixin: def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) self.use_somehow(self.some_attribute_needed_by_mixin) If your mixin must have its own initializer argument, make it a keyword-only argument, and have __init__ pop it off kwargs. 3. List mixin bases first among base classes. I don't think Python provides an ideal way to do MI, BTW. I believe that's by design: MI is somewhat troublesome specifically to discourage overuse. As a power MI user, I am fine with this. > I think I'd be back to having to call each mixin class's > __init__ explicitly: I'd say never mix super() and direct calls, for any reason. Pick one and go with it. Carl Banks From contact at xavierho.com Wed Jul 1 23:18:42 2009 From: contact at xavierho.com (Xavier Ho) Date: Thu, 2 Jul 2009 13:18:42 +1000 Subject: Newby Windows User In-Reply-To: <447092.99927.qm@web110201.mail.gq1.yahoo.com> References: <447092.99927.qm@web110201.mail.gq1.yahoo.com> Message-ID: <2d56febf0907012018h79a6ca5bt97d3a900a4b409bf@mail.gmail.com> Double click should work. If it flashes up and goes away really quickly, then you need to run it under IDLE or some kind of python shell. Once you have Python installed, go to Start -> Run, and type in cmd to bring up the windows command line or whatever you wanna call it. Go to the folder where your python script is. Command is the same like macs or linux, with cd being change directory, and such. Type e: to switch to the e drive and so on. Note that dir lists the directory and its contents, not ls like in mac terminals. When you're in the same folder as your script, type the command like this: python .py That should do the trick. Alternatively, you can type in python and start the python shell in the same folder. Then you can just do import and whatever you like. :] Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Thu, Jul 2, 2009 at 12:38 PM, wrote: > I am new to Python and use Windows XP. I would like to know how to run a > python program file? > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wuwei23 at gmail.com Wed Jul 1 23:41:19 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:41:19 -0700 (PDT) Subject: Using Python to set desktop background image under Windows XP References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: On Jul 2, 8:02?am, "ELLINGHAUS, LANCE" wrote: > Does anyone have any code that would allow setting the desktop background image under Windows XP? It's amazing what typing "python windows desktop" into Google will find you: http://snippets.dzone.com/posts/show/3348 Any other searches you'd like us to do? (Also: please post a new message instead of replying to an existing one and changing the subject, it screws up threading) From wuwei23 at gmail.com Wed Jul 1 23:47:46 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:47:46 -0700 (PDT) Subject: Getting input the scanf way References: Message-ID: On Jul 1, 6:33?pm, "Mr.SpOOn" wrote: > I need to do some kind of interactive command line program. > > I mean: I run the program, it asks me for input, I type something and > then I get the output or other questions. > I'm not sure what is the right way to achieve this. While the simplest way would be raw_input & print, as suggested, there's also the cmd[1] module in the stdlib, which is what the standard CPython interpreter uses, I believe. [1]: http://docs.python.org/library/cmd.html From no.email at please.post Wed Jul 1 23:49:57 2009 From: no.email at please.post (kj) Date: Thu, 2 Jul 2009 03:49:57 +0000 (UTC) Subject: Why re.match()? References: Message-ID: In Duncan Booth writes: >So, for example: >>>> re.compile("c").match("abcdef", 2) ><_sre.SRE_Match object at 0x0000000002C09B90> >>>> re.compile("^c").search("abcdef", 2) >>>> I find this unconvincing; with re.search alone one could simply do: >>> re.compile("^c").search("abcdef"[2:]) <_sre.SRE_Match object at 0x75918> No need for re.match(), at least as far as your example shows. Maybe there are times when re.match() is more "convenient" in some way, but it is awfully Perlish to "multiply language elements" for the sake of this trifling convenience. kynn From wuwei23 at gmail.com Wed Jul 1 23:53:37 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:53:37 -0700 (PDT) Subject: Open Source RSS Reader in Python? References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: <425d611e-ee56-4def-875e-899b2753eeb4@a7g2000yqk.googlegroups.com> On Jul 2, 9:18?am, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? It's always worth taking a look at PyPI. Some packages work perfectly well as stand alone applications. http://pypi.python.org/pypi?%3Aaction=search&term=rss&submit=search These two look promising: + NewsFeed (Tk-based): http://home.arcor.de/mdoege/newsfeed/ + urssus (QT-based): http://code.google.com/p/urssus/ From wuwei23 at gmail.com Wed Jul 1 23:56:28 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 20:56:28 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> Message-ID: <1e79b6bf-f33e-4267-bc61-dde6093a95e1@r33g2000yqn.googlegroups.com> On Jul 2, 3:47?am, Scott David Daniels wrote: > And even simpler: > ? ? ?PASSWORD = "qwerty" > ? ? ?MAXRETRY = 3 > ? ? ?for attempt in range(MAXRETRY): > ? ? ? ? ?if raw_input('Enter your password: ') == PASSWORD: > ? ? ? ? ? ? ?print 'Password confirmed' > ? ? ? ? ? ? ?break # this exits the for loop > ? ? ? ? ?print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY) > ? ? ?else: > ? ? ? ? ?# The else for a for statement is not executed for breaks, > ? ? ? ? ?# So indicates the end of testing without a match > ? ? ? ? ?raise SystemExit # Or whatever you'd rather do. Nice one, I always forget about for-else :) From steven at REMOVE.THIS.cybersource.com.au Thu Jul 2 00:14:36 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 02 Jul 2009 04:14:36 GMT Subject: Why re.match()? References: Message-ID: On Thu, 02 Jul 2009 03:49:57 +0000, kj wrote: > In Duncan Booth > writes: >>So, for example: > >>>>> re.compile("c").match("abcdef", 2) >><_sre.SRE_Match object at 0x0000000002C09B90> >>>>> re.compile("^c").search("abcdef", 2) >>>>> >>>>> > I find this unconvincing; with re.search alone one could simply do: > >>>> re.compile("^c").search("abcdef"[2:]) > <_sre.SRE_Match object at 0x75918> > > No need for re.match(), at least as far as your example shows. Your source string "abcdef" is tiny. Consider the case where the source string is 4GB of data. You want to duplicate the whole lot, minus two characters. Not so easy now. > Maybe there are times when re.match() is more "convenient" in some way, > but it is awfully Perlish to "multiply language elements" for the sake > of this trifling convenience. No, it's absolutely Pythonic. >>> import this ... Although practicality beats purity. -- Steven From rajat.dudeja at gmail.com Thu Jul 2 00:33:55 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Wed, 1 Jul 2009 21:33:55 -0700 (PDT) Subject: Access NtQueryInformationProces() from Python References: <408014003A0DA34BA5287D7A07EC089A1F94F4@EVS1.aeroflex.corp> Message-ID: <5e107810-2378-41fc-8fb5-329a943ab3bd@t13g2000yqt.googlegroups.com> On Jun 30, 2:10?pm, Tim Golden wrote: > Dudeja, Rajat wrote: > > Hi, > > > I'm looking for a way to call the NtQueryInformationProces() and ReadProcessMemory() of WindowsXP from Python. > > > Can anyone direct me to some examples where they have been successfully called through Python? > > You want to look at ctypes: > > http://docs.python.org/library/ctypes.html?highlight=ctypes#module-ct... > > TJG Thanks Tim From david.lyon at preisshare.net Thu Jul 2 00:40:25 2009 From: david.lyon at preisshare.net (David Lyon) Date: Thu, 02 Jul 2009 00:40:25 -0400 Subject: System default sys.path In-Reply-To: <4A4C1FE4.1070901@sun.com> References: <4A4C1FE4.1070901@sun.com> Message-ID: <81a8195ca5ecf44f3bf9ddbfaae37bdc@preisshare.net> On Wed, 01 Jul 2009 19:48:04 -0700, Brock Pytlik wrote: > Hi, I'm trying to find a way to get the value sys.path would have on a > particular system if python was started with an empty python path. I do > want it to include the site specific additional paths. I know I can hack > this information myself, Copy the code out from site.py... > but I'd rather be able to get this on demand so > that if things change in the future, I don't have grovel around looking > for which directory string to change. for index in range(len(sys.path)): del sys.path[0] site.addsitedir(self.p.python_sitepackages_path) > If nothing else, I think the following would satisfy my needs: > 1) a variable containing the directories to use as a prefix (I think > sys.exec_prefix and sys.prefix are what I want here) > 2) a variable containing the list of suffixes that are applies to the > prefixes, like lib/python/site-packages > 3) a way of handing the *.pth files What way do you want to handle them? From stefan_ml at behnel.de Thu Jul 2 00:50:54 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 02 Jul 2009 06:50:54 +0200 Subject: Multi thread reading a file In-Reply-To: References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <4a4c3cae$0$31873$9b4e6d93@newsspool3.arcor-online.net> Mag Gam wrote: > On Wed, Jul 1, 2009 at 12:54 AM, Gabriel Genellina wrote: >> Does the format conversion involve a significant processing time? If not, >> the total time is dominated by the I/O time (reading and writing the file) >> so it's doubtful you gain anything from multiple threads. > > The format does inolve significant time processing each line. > >>> Here is the pseudo code I was thinking about: >>> >>> Let T = Total number of lines in a file, Example 1000000 (1 million >>> files) >>> Let B = Total number of lines in a buffer, for example 10000 lines >>> >>> >>> Create a thread to read until buffer >>> Create another thread to read buffer+buffer ( So we have 2 threads >>> now. But since the file is zipped I have to wait until the first >>> thread is completed. Unless someone knows of a clever technique. >>> Write the content of thread 1 into a numpy array >>> Write the content of thread 2 into a numpy array >> Can you process each line independently? Is the record order important? If >> not (or at least, some local dis-ordering is acceptable) you may use a few >> worker threads (doing the conversion), feed them thru a Queue object, put >> the converted lines into another Queue, and let another thread write the >> results onto the destination file. > > Yes, each line can be independent. The original file is a time series > file which I am placing it into a Numpy array therefore I don't think > the record order is important. The writing is actually done when I > place a value into a "dset" object. > > Let me show you what I mean. > reader=csv.reader(open("100000.csv")) Have you tried if using the csv reader here is actually fast enough for you? Maybe you can just .split(',') each line or something (no idea what else the csv reader may do, but anyway...) > for s,row in enumerate(reader): > if s!=0 and s%bufsize==0: > dset[s-bufsize:s] = t #here is where I am writing the data to > the data structure. Using a range or broadcasting. Erm, what's "t"? And where do you think the "significant" processing time comes from in your example? From your code, that's totally non-obvious to me. > #15 columns > if len(row) != 15: > break > > t[s%bufsize] = tuple(row) > > #Do this all the way at the end for flushing. > if (s%bufsize != 0): > dset[(s//bufsize)*bufsize:s]=t[0:s%bufsize] If you *really* think your code is not I/O bound, but that the code for writing the data into the NumPy array is the slow part (which I actually doubt, but anyway), you can try writing your code in Cython, which has direct support for writing to NumPy arrays through their buffer interface. http://docs.cython.org/docs/numpy_tutorial.html Stefan From apt.shansen at gmail.com Thu Jul 2 01:11:10 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Wed, 1 Jul 2009 22:11:10 -0700 Subject: What are the limitations of cStringIO.StringIO() ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: <7a9c25c20907012211m610dac7dr3c18c33ea33f0ed6@mail.gmail.com> > > > 1. Anyone knows whet's the limitation on cStringIO.StringIO() objects ? > 2. Could you suggest a better way to create a string that contains the > concatenation of several big files ? > > This isn't a limit in cStringIO.StringIO, but a limit on the amount of memory a process is able to allocate from the OS. There's no real way around it-- but you can take a different approach. You have to get the memory somewhere, but if what you're combining can't fit into memory (generally, 2 GB, but it depends on OS and platform), you can use memory from a different source-- the hard-drive for instance. I would suggest you create a temporary file somewhere, and instead of reading the contents into memory, read it in chunks and write it to this temporary file, then go on and do the rest. That way you'll never have so much in memory that you hit the limits. When this is done, you can move this temporary file to where you want it, or read from it and pass its output somewhere. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Jul 2 01:25:05 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jul 2009 01:25:05 -0400 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: <4A4BC22D.70502@gmail.com> References: <4A4BC22D.70502@gmail.com> Message-ID: Stef Mientki wrote: > hello, > > I need to add an object's name to the global namespace. > The reason for this is to create an environment, > where you can add some kind of math environment, > where no need for Python knowledge is needed. > > The next statement works, > but I'm not sure if it will have any dramatical side effects, > other than overruling a possible object with the name A > > def some_function ( ...) : > A = object ( ...) > sys._getframe(1).f_globals [ Name ] = A global name name = A or is name is a string var globals()[name] = A From esj at harvee.org Thu Jul 2 01:30:16 2009 From: esj at harvee.org (Eric S. Johansson) Date: Thu, 02 Jul 2009 01:30:16 -0400 Subject: pep 8 constants In-Reply-To: <4A4A3FC1.5000108@tim.thechases.com> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4A2EE5.8040301@harvee.org> <4A4A3FC1.5000108@tim.thechases.com> Message-ID: <4A4C45E8.9070309@harvee.org> Tim Chase wrote: >> I've tried it least two dozen editors and they all fail miserably >> because they're focused on keyboard use (but understandable) > [...snip...] >> I've tried a whole bunch, like I said at least a dozen. They >> all fail for first reasons such as inability to access all >> functionality through keystrokes (easiest interface method >> from speech recognition) > > I'm not sure I follow which you want...you kvetch that > they're too focused on keyboard use, but then that you can't > access all functionality through the keyboard. sorry. I've been living in this world for so long that I sometimes forget to explain what's obvious inside my head. I apologize for the confusion. The apparent conflict arises because the same term (keyboard access) is used to describe two different problems. In the first case, the user interface model (how you access taxes, manipulate light which (language) constructs are designed to be efficient for your hands. speech recognition users need an editor that supports constructs that are efficient for the voice. One such construct is the ability to navigate or operate on language elements by name. I've used some of them before such as predicate, index, first method etc. The second problem is that editors are designed to be driven by keystroke. If I had a cross process boundary APIPaula I can simply call the editor function to perform to give operation. Unfortunately, today my speech recognition environment needs to generate keystrokes to access functionality. for example, in Emacs, when I say "delete word", the macro and marmot emits M-d > > [warning, blatant vim fanaticism follows] > > I use vim and can't remember the last time I used the mouse > with it (save for occasionally lazily scrolling with the > mouse-wheel, but you can scroll with the keyboard too), so > it meets your "must be fully accessible through the > keyboard" constraint. It also has single keystroke commands > for indenting/exdenting the current line as you mention: you are forgiven. :-) try this experiment. Bring up the buffer full Python code code you value them, starting in command mode, close your eyes and type a random word. While your eyes are closed, undo that word. Every time I've tried it the results have been mildly disastrous. If you been paying attention, I've tried to leave speech recognition errors in place whatever didn't impede understanding (too much) but, remember I'm getting one error in 20 errors is on average. That means, I'll be correcting the IE errors, potentially much more dangerous errors are regular basis. Any energy use by speech recognition must be able to cope with a random dictation errors and let the user recover gracefully. Again, until I started using speech recognition, I would never would've thought of this as a failure case. > > > I know this is also counter to the Python way but having > > something marking and outdent would be really useful so > > that a vocal driven command to indent or outdent a block > > would be practical. > > which can be done in insert-mode with control+D, control+T, > and if you want to clear all indentation (all the way back > to the first column), you can use "0" followed by control+D. > Vim also allows for fairly detailed control over > auto-indentation settings so you can match them to your > preferences. I suspect Emacs may be configurable to offer > similar functionality but I'd have to defer to the emacsen > on the list. Yes, Emacs will, a single tab key, tab view to the indentation level specified by the previous line unless it's already there and then it will start inventing further. This really sucks. Sometimes, you just weary and then to (you just want to) go to the right place and stay there > > That said, I'll be the first to admit that the vi/vim > learning curve is more like a brick wall than a curve. > However, I find the efficiency gains abundantly than repaid > the time I invested to learn it well. Yeah, I first encountered the eye in 1985. Or maybe I should say VI (Roman numeral six). > >>> Dasher[1] >> >> I've tried it, it's quite promising but my hands term are sufficiently >> that I can't target accurately. > > Last I tried it, it scaled the target sizes based on > probability. You might also try unconventional pointing > devices for better precision. I've seen web-cam > eye-tracking tools which might make it a bit easier. Or try > a tablet input (where the motion corresponds linearly, as > opposed to accelerated mouse motion). Such input options > are listed on the Dasher website, at least for those they > tested. > I like tablets. I need to buy a new one. We only problem is that most pens for tablets are a little bit too small and hard for my hands (although they are much better than mice). The major downfall is the buttons on the pen shaft. Damn that's awkward to reach. I think it would need a separate tab of three buttons that I can click and double-click independent of the pen controls. >> Oh, another failure point. Have you ever tried to selected beach and >> by voice. I mean I should say have you ever tried to >> select a region by voice. > > yes, not a pleasant experience. Again, vim's modal > interface allows for using text-objects so you can issue > short-hand commands like > > ci" > > to "[c]hange the contents [i]nside the double-quotes I'm > currently on/withing". The operator+motion and > operator+textobject command syntax obviates a lot selection. > While vim does offer a "visual" mode (akin to selection in > other editors), I use it much less because of the power > provided by the operator+[motion/textobject]. that's getting close to what I want to do with voice command except instead of cryptic sequence of characters, I would say something like string_edit = edit [] string which = first | second | third | fourth | fifth | sixth > as you're an emacs user, my vim suggestions may sound like > heresy. ;-) But I suspect a skilled emacs user (or perhaps > asking on an emacs-users list) could mimic much of the vim > functionality. I just can't be of much assistance there. I really don't care what editor I use as long as it gets the job done. I have strong preferences for always insert mode but, if I had an editor I had no keystroke commands and everything I type went into the buffer but I committed a complete of my speech, that would be kind of cool. > > You're welcome to move it off-list...it's kinda drifted from python to > general accessibility issues. CC'ing c.p.l for this one in case any > emacsen care to chime in on their suggested tweaks. well, that you are really joined. For example, if we look at the string edit command above, that implies he had or has the ability to properly distinguish a string in a line of code. Think of how many ways you can express a string triple or single quotes into different forms sometimes with quotes nested inside. then take the whole object instance and know method is possible with its type signature. This is editor work. From Mariosky at gmail.com Thu Jul 2 01:33:25 2009 From: Mariosky at gmail.com (Mario Garcia) Date: Wed, 1 Jul 2009 22:33:25 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: Message-ID: <05dde563-a8f1-45d1-8f59-7740c58787ba@e21g2000yqb.googlegroups.com> Thank you all for your input, Yes random is what I need!! I checked the docs, and follow your comments, I will use both random.sample and random.shuffle,and random.choice etc. !! For this particular problem I think ramdom.shuffle() is what I need. I was not very clear or complete in my explanation, and I didn't expect such a discussion on my needs, now I'm embarrassed :) . The list is a data set for training a machine learning algorithm. I want to use 70% of the records (random) for training, but then the remaining 30% are used for validation. This is repeated a few times choose again 70% at random for training and the rest for validation. With random.shuffle() I just iterate for the first 70% of the records for training and I just continue with the remaining 30%. This last 30% we dont care if its random anymore, so shuffle is doing some extra work as it was pointed out. Conceptually something like this: population = range(100) training = random.sample(population,70) validation = set(population).difference(set(training)) But I think this is more costly Thanks Again Mario From rajat.dudeja at gmail.com Thu Jul 2 01:42:43 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Wed, 1 Jul 2009 22:42:43 -0700 (PDT) Subject: Accessing windows structures through ctypes. Message-ID: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Hi, Using ctypes can I access the windows structures like: PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), PEB_LDR_DATA, etc? Regards, Rajat From esj at harvee.org Thu Jul 2 01:47:28 2009 From: esj at harvee.org (Eric S. Johansson) Date: Thu, 02 Jul 2009 01:47:28 -0400 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <025949cb$0$20671$c3e8da3@news.astraweb.com> <4A497A5B.4040503@harvee.org> Message-ID: <4A4C49F0.5060306@harvee.org> Rhodri James wrote: > > Gah. Ignore me. I hit 'send' instead of 'cancel', after my musings > concluded that yes, an editor could be smart enough, but it would have to > embed a hell of a lot of semantic knowledge of Python and it still wouldn't > eliminate the need to speak the keyboard at times. imagine having the same problem except that happens at random while you're speaking and watching something else other than a screen. :-) Yeah, it would take a lot of semantic knowledge about Python. Is it possible to leverage compiler internals? Is it possible to gather as much information as you can and when the system says "I don't know" you tell what you know it remembers that for future reference? And you're right, they're always corner cases where you'll need to speak the keyboard or type. I've mostly resigned to accept on days when I have an absolute hissy hissy fit because my hands hurt so bad I can't get enough painkillers drop the pain and let me function. In the real short term what I really want. More than anything right now is to get back to the point where I can pick away at writing Python again. I'm in this really weird confluence of virtual machine hell, the evolution of naturally speaking under wine and the problems of telling an editor how to do the right thing. There's a gap between the fragile crappy way I and others operate today and the really ideal completely voice driven environment that exists between my ears. I would like to move into that gap but it's sufficiently far away from what my hands can do that I'm dependent on others and that makes me really crazy sometimes. >From my perspective, what I consider a reasonable intermediate step would be an editor that has the following features: The ability to select regions either by NaturallySpeaking Select-and-Say and Emacs mark and point semantics. Very deterministic indentation control. (I.e. indent x tabs or indent to "right" level.) ability to navigate/operate on generic language features it would be nice if one could edit language features in a separate window again, using Select-and-Say type interface. another it would be nice would be the ability to look up type signatures (classes, methods etc. and inject the appropriate template when using a particular method. (Doesn't have to be by voice) this level of functionality, would also make it possible to comply with PEP-8 :-) given proper easy and automatic name generation and dumpling. From ldo at geek-central.gen.new_zealand Thu Jul 2 01:53:09 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 02 Jul 2009 17:53:09 +1200 Subject: Making code run in both source tree and installation path References: <5db6ce60-0645-40e1-b8fd-415beca46a47@l2g2000vba.googlegroups.com> Message-ID: In message , Robert Kern wrote: > On 2009-07-01 01:04, Carl Banks wrote: >> >> The most common way I've seen people work around this issue is to >> throw their data files into the package directories. Yuck. > > Huh. I always found that to be a more elegant solution than hardcoding the > data location into the program at install-time. Think in terms of a portable OS, written to run on multiple architectures. Read-only data is architecture-dependent, that's why it pays to separate it from the code. Otherwise you end up with a system that needs a reinstall every time the hardware changes. From wuwei23 at gmail.com Thu Jul 2 02:09:13 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 23:09:13 -0700 (PDT) Subject: Basic question from pure beginner References: <815232c0-e3ac-4b42-bcef-8f9881429048@c36g2000yqn.googlegroups.com> <49a531ad-f6c8-4875-869f-6a694905fc72@k8g2000yqn.googlegroups.com> <1e79b6bf-f33e-4267-bc61-dde6093a95e1@r33g2000yqn.googlegroups.com> Message-ID: <19cbe70f-d9a0-4c77-b02a-98b943b063b7@o6g2000yqj.googlegroups.com> Dennis Lee Bieber wrote: > ? ? ? ? There is also the getpass module to play with! I don't think I've ever seen getpass, so thanks for pointing that out. Unfortunately, it wouldn't have helped the OP understand why his original code wasn't working ;) From tkjthingone at gmail.com Thu Jul 2 02:10:43 2009 From: tkjthingone at gmail.com (Horace Blegg) Date: Wed, 1 Jul 2009 23:10:43 -0700 Subject: Accessing windows structures through ctypes. In-Reply-To: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Message-ID: http://www.codeproject.com/KB/threads/GetNtProcessInfo.aspx Looks rather to be pretty simple: Acquire the PED base pointer (article explains how) and then just read that information into a struct using ReadProcessMemory(). On Wed, Jul 1, 2009 at 10:42 PM, Rajat wrote: > Hi, > > Using ctypes can I access the windows structures like: > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > PEB_LDR_DATA, etc? > > > Regards, > Rajat > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From shenyute at gmail.com Thu Jul 2 02:11:54 2009 From: shenyute at gmail.com (Shen, Yu-Teh) Date: Wed, 1 Jul 2009 23:11:54 -0700 (PDT) Subject: dealloc function in python extend c module Message-ID: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> I create my extend type something like http://www.python.org/doc/current/extending/newtypes.html. And my type has a member which is a pointer point to my allocate memory ( no ref count). ex: --------------------------------------------------- typedef struct { PyObject_HEAD /* Type-specific fields go here. */ mytype *p; } noddy_NoddyObject; And i write dealloc function like this: --------------------------------------------------- static void Noddy_dealloc(Noddy* self) { delete p; self->ob_type->tp_free((PyObject*)self); } And I found it's strange that it didn't call dealloc when there is no reference. ex: a = Noddy() b = a del a del b # i think there is no ref to the object, and it should call dealloc but it didn't call!!! could anyone tell me what happened? Thanks a lot!! From pm567426 at gmail.com Thu Jul 2 02:13:52 2009 From: pm567426 at gmail.com (Pedram) Date: Wed, 1 Jul 2009 23:13:52 -0700 (PDT) Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> Message-ID: <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> On Jul 1, 10:01?pm, Christian Heimes wrote: > Pedram schrieb: > > > Hello community, > > I'm reading the CPython interpreter source code, > > first, if you have something that I should know for better reading > > this source code, I would much appreciate that :) > > second, in intobject.c file, I read the following code in > > fill_free_list function that I couldn't understand: > > while (--q > p) > > ? ? Py_TYPE(q) = (struct _typeobject *)(q-1); > > Py_TYPE(q) = NULL; > > > > What does it mean? > > The code is abusing the ob_type member to store a single linked, NULL > terminated list of free integer objects. It's a tricky optimization you > don't have to understand in order to understand the rest of the code. > > And no, the code in ctypes.h is totally unrelated to the free list in > intobject.c. > > Christian Thanks for reply. I totally understood the fill_free_list() function. Thanks. But I have problems in understanding the PyInt_FromLong(). Here's the code: PyObject * PyInt_FromLong(long ival) { register PyIntObject *v; #if NSMALLNEGINTS + NSMALLPOSINTS > 0 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { v = small_ints[ival + NSMALLNEGINTS]; Py_INCREF(v); #ifdef COUNT_ALLOCS if (ival >= 0) quick_int_allocs++; else quick_neg_int_allocs++; #endif return (PyObject *) v; } #endif if (free_list == NULL) { if ((free_list = fill_free_list()) == NULL) return NULL; } v = free_list; // <-- No problem till here :) free_list = (PyIntObject *)Py_TYPE(v); PyObject_INIT(v, &PyInt_Type); v->ob_ival = ival; return (PyObject *) v; } I understood that small numbers (between -5 and 256) are referenced to small_ints list. Others have to store in PyIntBlock, am I right? OK, no problem till 'v = free_list;' but can't understand the line 'free_list = (PyIntObject *)Py_TYPE(v);' :(. Where free_list referenced to? Don't this correct that free_list have to be referenced to another free block so next time the function called v store in this location? Sorry for my bad English. I hope you could understand me :) From israelu at elbit.co.il Thu Jul 2 02:17:00 2009 From: israelu at elbit.co.il (iu2) Date: Wed, 1 Jul 2009 23:17:00 -0700 (PDT) Subject: wxPython: Plaing widgets in status bar Message-ID: <335b69b5-eabe-443e-842d-fb6ec95a3bd3@x3g2000yqa.googlegroups.com> Hi all, I try to placs widgets (button, static text labels) in a status bar, wxPython. >From the examples I could place them using exact positioning by the status bar methof GetFieldRect. In this case I need to add an EVT_SIZER handler to keep the widgets in their propotional places within the status bar. I try to do it without exact positioning, that is using a sizer. I tried adding a sizer directly to the status bar, but it didn't work, it layed out the widgets at the upper left corner of the status bar, atop of each other. I tried adding a panel to the status bar and then adding the widgets to it., but then the widgets partially cover the status bar. They also don't keep their propotional positions, although I used it in the sizer.Add method. How can I position widgets within the status bar like positioning them in a frame? thanks From wuwei23 at gmail.com Thu Jul 2 02:25:38 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 1 Jul 2009 23:25:38 -0700 (PDT) Subject: Accessing windows structures through ctypes. References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Message-ID: On Jul 2, 3:42?pm, Rajat wrote: > Using ctypes can I access the windows structures like: > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > PEB_LDR_DATA, etc? ctypes.wintypes lists all of the Windows structures included with the module. You should be able to use ctypes.Structure class to roll your own: http://docs.python.org/library/ctypes.html#structured-data-types http://code.activestate.com/recipes/208699/ http://msdn.microsoft.com/en-us/library/ms684855(VS.85).aspx From esj at harvee.org Thu Jul 2 02:33:10 2009 From: esj at harvee.org (Eric S. Johansson) Date: Thu, 02 Jul 2009 02:33:10 -0400 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <025949cb$0$20671$c3e8da3@news.astraweb.com> Message-ID: <4A4C54A6.9030707@harvee.org> Steven D'Aprano wrote: > That assumes that every word is all caps. In practice, for real-life > Python code, I've tripled the vocal load of perhaps one percent of your > utterances, which cuts your productivity by 2%. > > If you have 10000 words in you per day, and one percent get wrapped with > a leading and trailing "capslock", you have: > > 9900 regular words plus 100 constants > > versus > > 9700 regular words plus 100 constants plus 200 by capslock. > > Your productivity goes down by 200 words out of 10,000, or two percent. oh I wish it was so simple. I would absolutely you if it was that simple the real world. Somehow, I fear that you wouldn't understand unless you sat next to me and watch me try to write code to a variety of standards. In practice, my experience says that with a really good speech driven environment for programming, IT, vocal load probably by a quarter to a half and be more compliant with current fashion for naming. > > >>>> Heck, have you ever noticed how most Python smart editors can't even >>>> indent properly according to local contexts. Emacs is the only one and >>>> even that one sometimes fails >>> I use kwrite for editing, and I can't say I've ever noticed a failure. >> okay, it fails from a speech recognition user perspective. Put the >> cursor on the line and hit the tab key. Insert spaces/tabs in the line >> of text. Not good for speech recognition. If mess of indentation and >> hit the tab key, it doesn't automatically indent to the right location. >> I'll be damned if I'm in the sit there speaking >> "tabkeytabkeytabkeytabkeytabkey" because the error should take care of >> it for me and save my voice. Like I said, Emacs does it correctly and >> nobody else does as far as I can tell. > > I don't understand what you're describing. > > If you set kwrite to use "Python style" indent mode, then starting a new > line will automatically indent to either the current indent level, or one > extra indent level if the line ends with a colon. You shouldn't need to > indent forward more than one level at a time when writing Python code, > although you will need to dedent multiple levels on occasion. I recognize your confusion. It's very common when describing this problem. If I had some way of making a movie of what and describing, obviously be more clear. I'll see if I can make a movie with my camera that demonstrates the problem. It probably won't happen fast but, what I tried your editor, it failed with a trivial effort. I think I just hit the tab key multiple times in different places on the line. In all cases it did the wrong thing. For me, the right thing is to force the line to the right indentation based on the previous line to matter how may times you get the tab key or where you are on that line when you hit the tab key. I could be in the middle of the line, the end of line, start a line, the start of the string and if I hit a tab key, that line should go to exactly the right place stayed there no matter how me more times I typed a tab key. But this might be what you're getting confused. What I'm really asking for is the ability to say "force indent" and have the line indent exactly correctly based on context. It doesn't have to be the tab key. The tab key is the method of convenience. > >> You're looking at it from the classical anti-accommodation perspective. >> Don't change the convention, give us the tools so we can comply with >> what the rest of you do. > > Just a minute. You're the one who started this discussion rejecting the > convention. Your first post started off: > > "I reject this convention..." > > and then finished with > > "so I reject pep 8 because I have no voice safe alternative" > > Do you blame us for reading this as a protest against uppercase > identifiers? it's a protest against identifiers that increase vocal or hand load. It's also protest against decisions people make without awareness of the implications, in this case, increasing the barriers against disabled programmers. > > Now you're saying you're happy for us to keep the all-uppercase > convention and just want better tools. Okay, you want better tools. > Great. If you're attempting to raise the profile of disabled programmers, > you've done so, but what exactly are you expecting? I waffle back and forth on that. I recognize my opinion matters as much as a piss hole in the snow. But the unintentional discrimination really gets to me. In case you're wondering, I feel this way about many forms of bad design/exclusionary practices. Some I can even do something about. my expectations are evolving. I wanted to raise awareness, like you said, I've done that. But now, I'd like to turn it into action. As I said in another post, the editor that would let me get back to writing Python relatively easily again doesn't exist. It's not the flashy wonderful no keystrokes whatsoever condition but it's significantly better than the current Emacs/VI/Python editor du jour. it would be able to operate open loop with relation to speech recognition but I also have an idea of how to add closed loop capability to make it more efficient as that capability becomes available. > I've already said it's your right to reject the convention for your own > code. Go right ahead. This is only a problem if you're editing other > people's code which is using uppercase constants. exactly. If I'm unable to comply with local conventions, I cannot work within an organization and be an active member of the team. This is one example of what I mean by unintentional discriminatory effects of a given decision. rhetorical devices aside, I think the right way to deal with accessibility issues and decisions leading to discrimination is to make the appropriate tools if possible for the individual. But I'm sure that would be a very long discussion we would have over a couple of beers. > > You've rejected capslock red capslock because it takes three utterances, > but said you use constant_red instead. That's two utterances, saving you > only one. If you actually have to say the underscore, your convention > takes six syllables versus five for my idea. my opinion comes out of experience. Strictly numerically speaking, you are correct. But if you look at recognition accuracy, pacing of speech, and flow of words through the vocal track, my solution is gentler on the vocal tract, body stress (tension anticipating this recognition errors), and cognition (don't need a spare any mental cycles on anticipation of failure and how to correct). I can just say focused on what I'm dictating. Additionally, making corrections of my style is easier (i.e. you can make the correction) but the form you propose with Locks (see, this is one of those "I'm going to bust a In your ass" type errors) cannot be corrected. He can only be deleted and restated. I apologize for being a "I'm the expert know it all" on this but, I really do have 15 years experience with these kind of failures and I like to think I know something about the topic. >>> Especially when these coding conventions are entirely optional. >> these conventions are religion for some special if you want to >> contribute code. > > Does your editor have search and replace? Before you contribute code, do > a global replacement of constant_red for RED. Yes it does and, it you know as well as I do that global search replace blindly leads to disastrous errors and, increased testing, and the problem doesn't go away because you always modify your code later. > > >> But look at the bigger issue. > > Yes, we get it. It sucks to have a physical disability. The universe > isn't arranged to make it easy for those who do, and even systems built > by people are only indifferently suitable. fine. I'll put down that particular baseball bat stop hitting you over the head with it. I still think you should try living with naturally speaking for a week or two. > > So, let's get down to practical matters: > > Do you wish to register a protest against all-caps constants in PEP 8? only if I had a rational, reasonable alternative. Let me chew on that. > > Do you wish to ask the Python Dev team to rethink their decision? only when I have rational, reasonable alternatives. > > Do you wish to raise the profile of disabled programmers? yes. Not exactly sure of the best way but I would like to do that through providing tools enabling them to participate on equal footing with not yet disabled programmers. I really don't like saying "take pity on the poor programmers with broken hands." I'd rather say "you need talent? Here's a pool you have previously thrown away but they can now generate code faster than the keyboard users. > Do you wish to start a project developing a better, smarter editor, and > are looking for volunteers? yes. This also opens up a great big giant hairball. Specifically, how do you edit on one machine but do all of your testing and debugging on the second (i.e. edit on Windows, do everything else on Linux. Unfortunately, I have quite a bit of scar tissue with this as well and can tell you all about how the different network file systems fail in special and wonderful ways. but let's deal with the editor first. yes, I would like to start a smarter editor project and the only way I could do it is with volunteers. I would not be able to write code for it until it had progressed significantly. What I can do is provide the knowledge of a first-generation system should look like, evolutionary corrections thereafter. I can also help with interfacing Python two to speech recognition and creation of grammars and their code. I'm a strong believer in recycling existing systems. We might get lucky and make it better for them to. if you're willing to help me find volunteers, get this started (I only need some nudges in the right direction), I gladly accept your help and your knowledge. From rajat.dudeja at gmail.com Thu Jul 2 02:50:43 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Wed, 1 Jul 2009 23:50:43 -0700 (PDT) Subject: Accessing windows structures through ctypes. References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> Message-ID: <8c8b5cf2-bc77-4633-96ca-e3b908430492@z14g2000yqa.googlegroups.com> > > Using ctypes can I access the windows structures like: > > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > > PEB_LDR_DATA, etc? > > ctypes.wintypes lists all of the Windows structures included with the > module. > > You should be able to use ctypes.Structure class to roll your own: Thanks Alex. As you suggested, I'm trying to implemenet the below structure, windows PEB, in Python: typedef struct _PEB { BYTE Reserved1[2]; BYTE BeingDebugged; BYTE Reserved2[21]; PPEB_LDR_DATA LoaderData; PRTL_USER_PROCESS_PARAMETERS ProcessParameters; BYTE Reserved3[520]; PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine; BYTE Reserved4[136]; ULONG SessionId; } PEB; My equivalent Python structure is: class PEB(Structure): _fields_ = [("Reserved1", wintypes.BYTE * 2), ("BeingDebugged", wintypes.BYTE), ("Reserved2", wintypes.BYTE * 2), ("Reserved3", c_void_p), ("Ldr", pointer(PEB_LDR_DATA)), ("ProcessParameters", pointer (RTL_USER_PROCESS_PARAMETERS)), ("Reserved4", wintypes.BYTE * 104), ("Reserved5", c_void_p), (), ("Reserved6", wintypes.BYTE), ("Reserved7", c_void_p), ("SessionId", c_ulong)] I'm not sure what needs to go in the above empty tuple for "PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine" (in Original PEB). Please suggest. From sgm at objexx.com Thu Jul 2 02:54:25 2009 From: sgm at objexx.com (SK) Date: Wed, 1 Jul 2009 23:54:25 -0700 (PDT) Subject: multiprocessing and freezing on Windows Message-ID: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Is there a method for freezing a Python 2.6 app using multiprocessing on Windows using PyInstaller or py2exe that works? It is trying to call my executable instead of python.exe when the process starts and passes it --multiprocessing-fork . Adding a freeze_support() to my main doesn't help. Do I have to bundle all of Python and use set_executable() to point to the bundled python.exe? I can't find any workable information on the canonical way to do this. Thanks! From clp2 at rebertia.com Thu Jul 2 03:02:41 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Jul 2009 00:02:41 -0700 Subject: Open Source RSS Reader in Python? In-Reply-To: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: <50697b2c0907020002o16536566h948856375ea3289e@mail.gmail.com> On Wed, Jul 1, 2009 at 4:18 PM, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? Straw for GTK/GNOME is written in Python, though it's somewhat inactive. http://projects.gnome.org/straw/ http://strawreader.wordpress.com/ Cheers, Chris -- http://blog.rebertia.com From eckhardt at satorlaser.com Thu Jul 2 03:11:48 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Thu, 02 Jul 2009 09:11:48 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors Message-ID: Hi! I'm currently converting my bioware to handle Python code and I have stumbled across a problem... Simple scenario: I have a handle to a resource. This handle allows me to manipulate the resource in various ways and it also represents ownership. Now, when I put this into a class, instances to that class can be shared, using Python's reference counting. What I'm missing is a way to automatically release the resource, something which I would do in the destructor in C++. Any ideas how to solve this? Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From 4564 at 755189.45 Thu Jul 2 03:42:09 2009 From: 4564 at 755189.45 (Enrico) Date: Thu, 2 Jul 2009 09:42:09 +0200 Subject: Accessing windows structures through ctypes. References: <26f1eb49-f972-40c7-aa9e-d4d6e3079d40@i6g2000yqj.googlegroups.com> <8c8b5cf2-bc77-4633-96ca-e3b908430492@z14g2000yqa.googlegroups.com> Message-ID: <4a4c650c$0$1115$4fafbaef@reader3.news.tin.it> "Rajat" ha scritto nel messaggio news:8c8b5cf2-bc77-4633-96ca-e3b908430492 at z14g2000yqa.googlegroups.com... > > > > Using ctypes can I access the windows structures like: > > > > > PROCESS_INFORMATION_BLOCK, Process Environment Block(PEB), > > > PEB_LDR_DATA, etc? > > > > ctypes.wintypes lists all of the Windows structures included with the > > module. > > > > You should be able to use ctypes.Structure class to roll your own: > > Thanks Alex. As you suggested, I'm trying to implemenet the below > structure, windows PEB, in Python: > > typedef struct _PEB { > BYTE Reserved1[2]; > BYTE BeingDebugged; > BYTE Reserved2[21]; > PPEB_LDR_DATA LoaderData; > PRTL_USER_PROCESS_PARAMETERS ProcessParameters; > BYTE Reserved3[520]; > PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine; > BYTE Reserved4[136]; > ULONG SessionId; > } PEB; > > My equivalent Python structure is: > class PEB(Structure): > _fields_ = [("Reserved1", wintypes.BYTE * 2), > ("BeingDebugged", wintypes.BYTE), > ("Reserved2", wintypes.BYTE * 2), > ("Reserved3", c_void_p), > ("Ldr", pointer(PEB_LDR_DATA)), > ("ProcessParameters", pointer > (RTL_USER_PROCESS_PARAMETERS)), > ("Reserved4", wintypes.BYTE * 104), > ("Reserved5", c_void_p), > (), > ("Reserved6", wintypes.BYTE), > ("Reserved7", c_void_p), > ("SessionId", c_ulong)] > > I'm not sure what needs to go in the above empty tuple for > "PPS_POST_PROCESS_INIT_ROUTINE PostProcessInitRoutine" (in Original > PEB). > > Please suggest. PostProcessInitRoutine should be a callback function or something similar. It should be enough to define a type PostProcessInitRoutine = WINFUNCTYPE(...parameters...) and use this. Regards, Enrico From Joachim at Strombergson.com Thu Jul 2 03:57:14 2009 From: Joachim at Strombergson.com (=?UTF-8?B?Sm9hY2hpbSBTdHLDtm1iZXJnc29u?=) Date: Thu, 02 Jul 2009 09:57:14 +0200 Subject: PEP 376 In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> Message-ID: <4A4C685A.8020300@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! Richard Brodie wrote: > "Joachim Str?mbergson" wrote in message > news:mailman.2422.1246418400.8015.python-list at python.org... > >> Even so, choosing md5 in 2009 for something that (hopefully) will be >> used in years is a bad design decision. It creates a dependency for to >> an algorithm that all sensible recommendations point you to move away >> from. > > Why not write the field as algorithm:value? > > e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 > > Installers can fallback to using hashlib.new(), so you can plug in a new > algorithm without changing the PEP or the installer code. +1 Good idea and future proof as well as being simple. - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMaFoACgkQZoPr8HT30QHVxQCfWqKprUaAQxaxTfYJVfIp8+FC 5UIAn2cWNoMe+sdNO4BSZG7e+19qkqvl =Id7s -----END PGP SIGNATURE----- From petshmidt at googlemail.com Thu Jul 2 04:25:29 2009 From: petshmidt at googlemail.com (Tep) Date: Thu, 2 Jul 2009 01:25:29 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> Message-ID: <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> On 2 Jul., 01:56, MRAB wrote: > someone wrote: > > Hello, > > > how can I replace '?' sign from string? Or do split at that character? > > Getting unicode error if I try to do it: > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > 1: ordinal not in range(128) > > > Thanks, Pet > > > script is # -*- coding: UTF-8 -*- > > It sounds like you're mixing bytestrings with Unicode strings. I can't > be any more helpful because you haven't shown the code. Oh, I'm sorry. Here it is def cleanInput(input) return input.replace('?', '') From petshmidt at googlemail.com Thu Jul 2 04:31:46 2009 From: petshmidt at googlemail.com (Tep) Date: Thu, 2 Jul 2009 01:31:46 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> Message-ID: <9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> On 2 Jul., 10:25, Tep wrote: > On 2 Jul., 01:56, MRAB wrote: > > > someone wrote: > > > Hello, > > > > how can I replace '?' sign from string? Or do split at that character? > > > Getting unicode error if I try to do it: > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > > 1: ordinal not in range(128) > > > > Thanks, Pet > > > > script is # -*- coding: UTF-8 -*- > > > It sounds like you're mixing bytestrings with Unicode strings. I can't > > be any more helpful because you haven't shown the code. > > Oh, I'm sorry. Here it is > > def cleanInput(input) > ? ? return input.replace('?', '') I also need: #input is html source code, I have problem with only this character #input = 'foo ? bar' #return should be foo def splitInput(input) parts = input.split(' ? ') return parts[0] Thanks! From Joachim at Strombergson.com Thu Jul 2 04:32:04 2009 From: Joachim at Strombergson.com (=?UTF-8?B?Sm9hY2hpbSBTdHLDtm1iZXJnc29u?=) Date: Thu, 02 Jul 2009 10:32:04 +0200 Subject: PEP368 and pixeliterators Message-ID: <4A4C7084.60000@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! I just read the PEP368 and really liked the proposed idea, sound like a great battery addition to include in the std lib: http://www.python.org/dev/peps/pep-0368/ One question/idea though: The proposed iterator will iterate over all pixels in a line, but only one line. The example code looks like this: # iterate over an image for line in rgb_image: for pixel in line: # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, pixel.r But, wouldn't it be more Pythonic and simpler to have an iterator that iterates over all pixels in an image? Starting with upper left corner and moving left-right and (line by line) to lower right. This would change the code above to: for pixel in rgb_image: # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, pixel.r The idea I'm having is that fundamentally the image is made up of a 2D array of pixels, not rows of pixels. And having the iterator focus on pixels instead make then more sense, no? Or if possible have two iterators. Otherwise, big thumbs up for PEP 368 as well as having PEP:s for driving the language development process in an orderly and well documented way. - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMcIQACgkQZoPr8HT30QFkcgCgzxb9JS2l87B/nkpf05FLDjY5 RPQAni2yPxKjCd4lM/qMBNhjp8HHg/PZ =9gB/ -----END PGP SIGNATURE----- From jeremiah.dodds at gmail.com Thu Jul 2 04:44:44 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Thu, 2 Jul 2009 09:44:44 +0100 Subject: Determining if a function is a method of a class within a decorator In-Reply-To: <4A4AB863.1020205@ilm.com> References: <4A4AB863.1020205@ilm.com> Message-ID: <12cbbbfc0907020144s6d7848aci65bab54a726a9f22@mail.gmail.com> On Wed, Jul 1, 2009 at 2:14 AM, David Hirschfield wrote: > Unfortunately that still requires two separate decorators, when I was > hoping there was a way to determine if I was handed a function or method > from within the same decorator. > > Seems like there really isn't, so two decorators is the way to go. > Thanks, > -David > > This is a really horrible idea, but it may work: If you can rely on your code using the word "self", you could use the inspect module to look at the arguments of the function being decorated, and dispatch based on that. The idea itself feels extremely dirty though. -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Jul 2 05:11:34 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 11:11:34 +0200 Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> Message-ID: Pedram wrote: > On Jul 1, 10:01 pm, Christian Heimes wrote: >> Pedram schrieb: >> >> > Hello community, >> > I'm reading the CPython interpreter source code, >> > first, if you have something that I should know for better reading >> > this source code, I would much appreciate that :) >> > second, in intobject.c file, I read the following code in >> > fill_free_list function that I couldn't understand: >> > while (--q > p) >> > Py_TYPE(q) = (struct _typeobject *)(q-1); >> > Py_TYPE(q) = NULL; >> > >> > What does it mean? >> >> The code is abusing the ob_type member to store a single linked, NULL >> terminated list of free integer objects. It's a tricky optimization you >> don't have to understand in order to understand the rest of the code. >> >> And no, the code in ctypes.h is totally unrelated to the free list in >> intobject.c. >> >> Christian > > Thanks for reply. > I totally understood the fill_free_list() function. Thanks. > But I have problems in understanding the PyInt_FromLong(). Here's the > code: > > PyObject * > PyInt_FromLong(long ival) > { > register PyIntObject *v; > #if NSMALLNEGINTS + NSMALLPOSINTS > 0 > if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { > v = small_ints[ival + NSMALLNEGINTS]; > Py_INCREF(v); > #ifdef COUNT_ALLOCS > if (ival >= 0) > quick_int_allocs++; > else > quick_neg_int_allocs++; > #endif > return (PyObject *) v; > } > #endif > if (free_list == NULL) { > if ((free_list = fill_free_list()) == NULL) > return NULL; > } > > v = free_list; // <-- No problem till here :) > free_list = (PyIntObject *)Py_TYPE(v); > PyObject_INIT(v, &PyInt_Type); > v->ob_ival = ival; > return (PyObject *) v; > } > > I understood that small numbers (between -5 and 256) are referenced to > small_ints list. Others have to store in PyIntBlock, am I right? OK, > no problem till 'v = free_list;' but can't understand the line > 'free_list = (PyIntObject *)Py_TYPE(v);' :(. Where free_list > referenced to? Don't this correct that free_list have to be referenced > to another free block so next time the function called v store in this > location? > Sorry for my bad English. I hope you could understand me :) Well, if I understand fill_free_list() correctly, you haven't ;) It only makes sense together with the allocation code in PyInt_FromLong(). Py_TYPE(v) translates to v->ob_type, and in fill_free_list() that field was abused(*) to hold a pointer to the previous item in the array of free integers, thereby temporarily turning it into a linked list. Assuming that free_list pointed to block->objects[10] in the current PyIntBlock it will now point to block->objects[9]. When block->objects[0] is reached free_list will be set to NULL as block- >objects[0]->ob_type was initialized with NULL by the line Py_TYPE(q) = NULL; /* in fill_free_list() */ The next call of PyInt_FromLong() will then trigger the allocation of a new block. Peter (*) It may help if you think of the objects array as a union linked { linked *next; PyInt_Object obj; }; From davitadze.tengiz at gmail.com Thu Jul 2 05:23:00 2009 From: davitadze.tengiz at gmail.com (Tengiz Davitadze) Date: Thu, 2 Jul 2009 13:23:00 +0400 Subject: Need Help Message-ID: <2f0bc6280907020223kf2701au81da279a5f8fabaf@mail.gmail.com> Hello. I can't find a wright mail address. If you can help me I need to get an information about UNICODE. I am georgian and I need to write programs on georgian language . If you can transfer this mail or send me a wright mail about encoding or unicode information. -- Tengiz Davitadze { programing Future } -------------- next part -------------- An HTML attachment was scrubbed... URL: From sebastian.schabe at gmx.de Thu Jul 2 05:40:12 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Thu, 02 Jul 2009 11:40:12 +0200 Subject: deleting certain entries in numpy array In-Reply-To: References: <4a4b7763$1@news.fhg.de> Message-ID: <4a4c7fd9$1@news.fhg.de> Robert Kern schrieb: > First, convert the pos array to integers, and just the columns with > indices in them: > > ipos = pos[:,:2].astype(int) > > Now check the values in the mask corresponding to these positions: > > mask_values = mask[ipos[:,0], ipos[:,1]] > > Now extract the rows from the original pos array where mask_values is > nonzero: > > result = pos[mask_values != 0] > Great!!! That's the way I wanted. After reading the numpy reference guide I supposed the delete function was not the right way, but that it's all about cerrect indexing. But I didn't really know how. So thanks a lot, also to Ben. > > You will want to ask numpy questions on the numpy mailing list. > > http://www.scipy.org/Mailing_Lists > I ever thought news-groups are the right way for questions like this. And the mailing list page just confuses me, but I'm trying to get used to it. Sebastian From bearophileHUGS at lycos.com Thu Jul 2 05:50:17 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 2 Jul 2009 02:50:17 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> Ulrich Eckhardt: > a way to automatically release the resource, something > which I would do in the destructor in C++. Is this helpful? http://effbot.org/pyref/with.htm Bye, bearophile From gagsl-py2 at yahoo.com.ar Thu Jul 2 06:10:17 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 07:10:17 -0300 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: En Wed, 01 Jul 2009 12:49:31 -0300, Scott David Daniels escribi?: > Gabriel Genellina wrote: >> ... >> def convert(in_queue, out_queue): >> while True: >> row = in_queue.get() >> if row is None: break >> # ... convert row >> out_queue.put(converted_line) > > These loops work well with the two-argument version of iter, > which is easy to forget, but quite useful to have in your bag > of tricks: > > def convert(in_queue, out_queue): > for row in iter(in_queue.get, None): > # ... convert row > out_queue.put(converted_line) Yep, I always forget about that variant of iter() -- very handy! -- Gabriel Genellina From eckhardt at satorlaser.com Thu Jul 2 06:12:19 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Thu, 02 Jul 2009 12:12:19 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> Message-ID: <3pduh6-g52.ln1@satorlaser.homedns.org> Bearophile wrote: > Ulrich Eckhardt: >> a way to automatically release the resource, something >> which I would do in the destructor in C++. > > Is this helpful? > http://effbot.org/pyref/with.htm Yes, it aims in the same direction. However, I'm not sure this applies to my case. The point is that the resource handle is not just used locally in a restricted scope but it is allocated and stored. The 'with' is something that makes sense in the context of mutex locking, where you have a well-defined critical section. What I need is something similar to open(), which returs a file. When the last reference to that object goes out of scope, the underlying file object is closed. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From __peter__ at web.de Thu Jul 2 06:46:27 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 12:46:27 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: Ulrich Eckhardt wrote: > Bearophile wrote: >> Ulrich Eckhardt: >>> a way to automatically release the resource, something >>> which I would do in the destructor in C++. >> >> Is this helpful? >> http://effbot.org/pyref/with.htm > > Yes, it aims in the same direction. However, I'm not sure this applies to > my case. The point is that the resource handle is not just used locally in > a restricted scope but it is allocated and stored. The 'with' is something > that makes sense in the context of mutex locking, where you have a > well-defined critical section. Isn't that exactly what RAII does? > What I need is something similar to open(), > which returs a file. When the last reference to that object goes out of > scope, the underlying file object is closed. You can go ahead and implement a __del__() method. It will often work in CPython, but you get no guarantees, especially when you have reference cycles and with other Python implementations that don't use refcounting. Peter From pm567426 at gmail.com Thu Jul 2 06:55:42 2009 From: pm567426 at gmail.com (Pedram) Date: Thu, 2 Jul 2009 03:55:42 -0700 (PDT) Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> Message-ID: <7c8e96a1-cbb2-4036-9aa1-1ecdfd84cca8@j32g2000yqh.googlegroups.com> On Jul 2, 1:11?pm, Peter Otten <__pete... at web.de> wrote: > Pedram wrote: > > On Jul 1, 10:01 pm, Christian Heimes wrote: > >> Pedram schrieb: > > >> > Hello community, > >> > I'm reading the CPython interpreter source code, > >> > first, if you have something that I should know for better reading > >> > this source code, I would much appreciate that :) > >> > second, in intobject.c file, I read the following code in > >> > fill_free_list function that I couldn't understand: > >> > while (--q > p) > >> > Py_TYPE(q) = (struct _typeobject *)(q-1); > >> > Py_TYPE(q) = NULL; > >> > > >> > What does it mean? > > >> The code is abusing the ob_type member to store a single linked, NULL > >> terminated list of free integer objects. It's a tricky optimization you > >> don't have to understand in order to understand the rest of the code. > > >> And no, the code in ctypes.h is totally unrelated to the free list in > >> intobject.c. > > >> Christian > > > Thanks for reply. > > I totally understood the fill_free_list() function. Thanks. > > But I have problems in understanding the PyInt_FromLong(). Here's the > > code: > > > PyObject * > > PyInt_FromLong(long ival) > > { > > register PyIntObject *v; > > #if NSMALLNEGINTS + NSMALLPOSINTS > 0 > > if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { > > v = small_ints[ival + NSMALLNEGINTS]; > > Py_INCREF(v); > > #ifdef COUNT_ALLOCS > > if (ival >= 0) > > quick_int_allocs++; > > else > > quick_neg_int_allocs++; > > #endif > > return (PyObject *) v; > > } > > #endif > > if (free_list == NULL) { > > if ((free_list = fill_free_list()) == NULL) > > return NULL; > > } > > > v = free_list; // <-- No problem till here :) > > free_list = (PyIntObject *)Py_TYPE(v); > > PyObject_INIT(v, &PyInt_Type); > > v->ob_ival = ival; > > return (PyObject *) v; > > } > > > I understood that small numbers (between -5 and 256) are referenced to > > small_ints list. Others have to store in PyIntBlock, am I right? OK, > > no problem till 'v = free_list;' but can't understand the line > > 'free_list = (PyIntObject *)Py_TYPE(v);' :(. Where free_list > > referenced to? Don't this correct that free_list have to be referenced > > to another free block so next time the function called v store in this > > location? > > Sorry for my bad English. I hope you could understand me :) > > Well, if I understand fill_free_list() correctly, you haven't ;) It only > makes sense together with the allocation code in PyInt_FromLong(). > > Py_TYPE(v) translates to v->ob_type, and in fill_free_list() that field was > abused(*) to hold a pointer to the previous item in the array of free > integers, thereby temporarily turning it into a linked list. > > Assuming that free_list pointed to block->objects[10] in the current > PyIntBlock it will now point to block->objects[9]. > > When block->objects[0] is reached free_list will be set to NULL as block- > > >objects[0]->ob_type was initialized with NULL by the line > > Py_TYPE(q) = NULL; /* in fill_free_list() */ > > The next call of PyInt_FromLong() will then trigger the allocation of a new > block. > > Peter > > (*) It may help if you think of the objects array as a > > union linked { > ? ? linked *next; > ? ? PyInt_Object obj; > > > > > > }; > > Oh, I got it! What a wonderful implementation! :o Thanks so much Peter. You made my day :) I didn't read the code carefully. > > ;) > From no.email at please.post Thu Jul 2 07:19:40 2009 From: no.email at please.post (kj) Date: Thu, 2 Jul 2009 11:19:40 +0000 (UTC) Subject: Why re.match()? References: Message-ID: In Steven D'Aprano writes: >On Thu, 02 Jul 2009 03:49:57 +0000, kj wrote: >> In Duncan Booth >> writes: >>>So, for example: >> >>>>>> re.compile("c").match("abcdef", 2) >>><_sre.SRE_Match object at 0x0000000002C09B90> >>>>>> re.compile("^c").search("abcdef", 2) >>>>>> >>>>>> >> I find this unconvincing; with re.search alone one could simply do: >> >>>>> re.compile("^c").search("abcdef"[2:]) >> <_sre.SRE_Match object at 0x75918> >> >> No need for re.match(), at least as far as your example shows. >Your source string "abcdef" is tiny. Consider the case where the source >string is 4GB of data. You want to duplicate the whole lot, minus two >characters. Not so easy now. I'm sure that it is possible to find cases in which the *current* implementation of re.search() would be inefficient, but that's because this implementation is perverse, which, I guess, is ultimately the point of my original post. Why privilege the special case of a start-of-string anchor? What if you wanted to apply an end-anchored pattern to some prefix of your 4GB string? Why not have a special re method for that? And another for every possible special case? If the concern is efficiency for such cases, then simply implement optional offset and length parameters for re.search(), to specify any arbitrary substring to apply the search to. To have a special-case re.match() method in addition to a general re.search() method is antithetical to language minimalism, and plain-old bizarre. Maybe there's a really good reason for it, but it has not been mentioned yet. kj From nils at ccsg.de Thu Jul 2 07:30:44 2009 From: nils at ccsg.de (=?ISO-8859-1?Q?Nils_R=FCttershoff?=) Date: Thu, 02 Jul 2009 13:30:44 +0200 Subject: performance problem with time.strptime() Message-ID: <4A4C9A64.9030507@ccsg.de> Hi everyone, In my free time I translate scripts from open source projects or write my own, to train my python skills. ATM I convert the aplogmerge.pl from awstats. It merges multiple apache logfiles and sort the output by the timestamps of each line. My first version of this script hasn't a good performance, so I started profiling. It turned out that the script spend much time for converting the timestamps of the line into a struct_time object. Here a code example (Python 2.6.2 on Ubuntu 7.10): Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def strptime(): m = Rec.match(Line) if m: date_string = m.group(1) # date_string example: '02/Jul/2009:01:50:26' return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") with timeit this functions takes approximate 125 sec and 29.004.081 function calls (I've configured timeit with 1.000.000 runs). A look at the output of cProfile told me that more than the half time is spent in locale.py: 1000002 11.712 0.000 19.592 0.000 locale.py:316(normalize) 1000002 3.639 0.000 23.231 0.000 locale.py:382(_parse_localename) 1000002 5.162 0.000 30.271 0.000 locale.py:481(getlocale) I studied the time documentation and thought that I had to set TZ in os environ: os.environ['TZ'] = 'Europe/Berlin' time.set() but that had no effect. I don't know why time.strptime() looks every time for my locale. Maybe it's a problem with my OS locale... However. I've introduced a work around, which works perfectly for my problem. For time comparison I could use any sort of time representation and so I convert to epoch: # needed to translate month str to dig repr Shortmonth = {'Jan' : '01', 'Feb' : '02', 'Mar' : '03', 'Apr' : '04', 'May' : '05', 'Jun' : '06', 'Jul' : '07', 'Aug' : '08', 'Sep' : '09', 'Oct' : '10', 'Nov' : '11', 'Dec' : '12'} Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def epoch(): m = Rec.match(Line) if m: result = m.groupdict() date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) date_tuple = map(int,date_tuple) return time.mktime(date_tuple) with this workaround I had a speed up to 4 times; it tooks only 31 sec with only 5.000.009 function calls. Maybe this helps some of you, who had the similar problems with time conversion.... ...But one big question remains: Why time.strptime() checks everytime the locale? had I missed something or could I have a problem with my OS locale? With python 3.1 there is no difference, unless that time.strptime() took approx 12 sec longer... :( regards, Nils Here a complete test script: #!/opt/python/2.6.2/bin/python import time import timeit import cProfile import re # needed to tranlate month str to dig repr Shortmonth = {'Jan' : '01', 'Feb' : '02', 'Mar' : '03', 'Apr' : '04', 'May' : '05', 'Jun' : '06', 'Jul' : '07', 'Aug' : '08', 'Sep' : '09', 'Oct' : '10', 'Nov' : '11', 'Dec' : '12'} Rec1 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") Rec2 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def epoch(): m = Rec1.match(Line) if m: result = m.groupdict() date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) date_tuple = map(int,date_tuple) return time.mktime(date_tuple) def strptime(): m = Rec2.match(Line) if m: date_string = m.group(1) return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") if __name__ == "__main__": t1 = timeit.Timer("epoch()","from __main__ import epoch") t2 = timeit.Timer("strptime()", "from __main__ import strptime") cProfile.run("t1.timeit();print") print "" cProfile.run("t2.timeit();print") From simon at brunningonline.net Thu Jul 2 07:34:10 2009 From: simon at brunningonline.net (Simon Brunning) Date: Thu, 2 Jul 2009 12:34:10 +0100 Subject: Need Help In-Reply-To: <2f0bc6280907020223kf2701au81da279a5f8fabaf@mail.gmail.com> References: <2f0bc6280907020223kf2701au81da279a5f8fabaf@mail.gmail.com> Message-ID: <8c7f10c60907020434u26e0518el6045d23cd89c4bcf@mail.gmail.com> 2009/7/2 Tengiz Davitadze : > Hello. I can't find a wright mail address. If you can help me I need to get > an information about UNICODE. I am georgian and I need to write programs on > georgian language . If you can transfer this mail or send me a wright mail > about encoding or unicode information. Our chief link is . and . Our two links are and . And . Our *three* links are , , and . And . Our *four*...no... *Amongst* our links.... Amongst our linkry are such elements as , .... I'll come in again. -- Cheers, Simon B. From Caseyweb at gmail.com Thu Jul 2 07:45:22 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Thu, 2 Jul 2009 04:45:22 -0700 (PDT) Subject: performance problem with time.strptime() References: Message-ID: <6a2cc3a6-0e5c-4a43-bcf7-416a77253f97@h2g2000yqg.googlegroups.com> On Jul 2, 7:30?am, Nils R?ttershoff wrote: > Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' I'm not sure how much it will help but if you are only using the regex to get the date/time group element, it might be faster to replace the regex with: >>> date_string = Line.split()[3][1:-1] From ziade.tarek at gmail.com Thu Jul 2 07:55:30 2009 From: ziade.tarek at gmail.com (=?ISO-8859-1?Q?Tarek_Ziad=E9?=) Date: Thu, 2 Jul 2009 13:55:30 +0200 Subject: PEP 376 In-Reply-To: <4A4C685A.8020300@Strombergson.com> References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> Message-ID: <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> 2009/7/2 Joachim Str?mbergson : > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Aloha! > > Richard Brodie wrote: >> "Joachim Str?mbergson" wrote in message >> news:mailman.2422.1246418400.8015.python-list at python.org... >> >>> Even so, choosing md5 in 2009 for something that (hopefully) will be >>> used in years is a bad design decision. It creates a dependency for to >>> an algorithm that all sensible recommendations point you to move away >>> from. >> >> Why not write the field as algorithm:value? >> >> e.g. sha1:8590b685654367e3eba70dc00df7e45e88c21da4 >> >> Installers can fallback to using hashlib.new(), so you can plug in a new >> algorithm without changing the PEP or the installer code. > > +1 > > Good idea and future proof as well as being simple. The prefix is a good idea but since it's just a checksum to control that the file hasn't changed what's wrong with using a weak hash algorithm like md5 or now sha1 ? If someone wants to modify a file of a distribution he can recreate the checksum as well, the only secured way to prevent that would be to use gpg keys but isn't that overkill for what we need ? e.g. making sure a file wasn't modified when distutils uninstalls a distribution. Tarek From hniksic at xemacs.org Thu Jul 2 07:55:39 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 02 Jul 2009 13:55:39 +0200 Subject: Why re.match()? References: Message-ID: <87k52rwb7o.fsf@busola.homelinux.net> kj writes: > For a recovering Perl-head like me it is difficult to understand > why Python's re module offers both match and search. Why not just > use search with a beginning-of-string anchor? I need re.match when parsing the whole string. In that case I never want to search through the string, but process the whole string with some regulat expression, for example when tokenizing. For example: pos = 0 while pos != len(s): match = TOKEN_RE.match(s, pos) if match: process_token(match) pos = match.end() else: raise ParseError('invalid syntax at position %d' % pos) From davea at ieee.org Thu Jul 2 08:05:12 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 02 Jul 2009 08:05:12 -0400 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: References: Message-ID: <4A4CA278.4050604@ieee.org> Ulrich Eckhardt wrote: > Hi! > > I'm currently converting my bioware to handle Python code and I have > stumbled across a problem... > > Simple scenario: I have a handle to a resource. This handle allows me to > manipulate the resource in various ways and it also represents ownership. > Now, when I put this into a class, instances to that class can be shared, > using Python's reference counting. What I'm missing is a way to > automatically release the resource, something which I would do in the > destructor in C++. > > Any ideas how to solve this? > > Uli > > As someone else pointed out, 'with' is the first line of defense. It makes the stuff you could already do with try/except/finally much easier to get right. Look also at 'del' a command in the language which explicitly deletes an object. But I'm guessing you want something that automatically deletes objects whenever the last reference disappears. That's an implementation detail, not a language guarantee. In particular CPython does what you want, by using reference counting. That's the only Python I've used, so it's only hearsay when I say that other implementations, (maybe Cython or Jython) do not all work the same way. In CPython, any object whose *last* reference goes away, will get immediately deleted. So if you avoid circular references, it should do just what you want. Orphaned circular references are caught by the garbage collector, which runs periodically, but is non-deterministic. Two more caveats. Exceptions tend to hang onto stuff in their near vicinity, and I can't tell you the algorithm for what happens. But an explicit del in the except clause can probably handle that. Finally, closures can hang onto stuff. So if you have nested functions, or lambda functions, it's possible they're going to keep things longer than you realize. From lists at cheimes.de Thu Jul 2 08:10:55 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 02 Jul 2009 14:10:55 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: <4A4CA278.4050604@ieee.org> References: <4A4CA278.4050604@ieee.org> Message-ID: Dave Angel wrote: > Look also at 'del' a command in the language which explicitly deletes an > object. No, you are either explaining it the wrong way or you have been fallen for a common misinterpretation of the del statement. The del statement only removes the object from the current scope. This means it decreases the reference count by one. It does *not* remove the object. Christian From Caseyweb at gmail.com Thu Jul 2 08:17:28 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Thu, 2 Jul 2009 05:17:28 -0700 (PDT) Subject: PEP368 and pixeliterators References: Message-ID: <2aeeb8de-c8f3-4e9d-98f7-d1fa71e1f3a3@d4g2000yqa.googlegroups.com> On Jul 2, 4:32?am, Joachim Str?mbergson wrote: > But, wouldn't it be more Pythonic and simpler to have an iterator that > iterates over all pixels in an image? Starting with upper left corner > and moving left-right and (line by line) to lower right. This would > change the code above to: Unless I'm totally misreading the PEP, the author does provide both iterators. Quoting the PEP: Non-planar images offer the following additional methods: pixels() -> iterator[pixel] Returns an iterator that iterates over all the pixels in the image, starting from the top line and scanning each line from left to right. See below for a description of the pixel objects. __iter__() -> iterator[line] Returns an iterator that iterates over all the lines in the image, from top to bottom. See below for a description of the line objects. From __peter__ at web.de Thu Jul 2 08:54:50 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 14:54:50 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: Dave Angel wrote: > But I'm guessing you want something that automatically deletes objects > whenever the last reference disappears. That's an implementation > detail, not a language guarantee. In particular CPython does what you > want, by using reference counting. That's the only Python I've used, so > it's only hearsay when I say that other implementations, (maybe Cython > or Jython) do not all work the same way. Here are some examples from Kubuntu 9.04's zoo of python implementations: $ cat del.py import sys print sys.version class A(object): def __init__(self, x): self.x = x def __del__(self): print "releasing A(%r)" % self.x def f(): a = A("local in function") f() a = A("global (one ref)") c = A("global (cycle)") c.a = c del c b = A("global (no refs)") del b print "about to quit" $ python del.py 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) [GCC 4.3.3] releasing A('local in function') releasing A('global (no refs)') about to quit releasing A('global (one ref)') $ jython del.py 2.2.1 about to quit $ ipy del.py 2.4.0 (IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.42) about to quit releasing A('global (no refs)') releasing A('global (cycle)') releasing A('local in function') Unhandled Exception: System.ArgumentException: I/O operation on closed file at IronPython.Runtime.PythonFile.ThrowIfClosed () [0x00000] at IronPython.Runtime.PythonFile.Write (System.String s) [0x00000] $ IronPython sometimes segfaulted. Peter From nils at ccsg.de Thu Jul 2 09:00:11 2009 From: nils at ccsg.de (=?ISO-8859-1?Q?Nils_R=FCttershoff?=) Date: Thu, 02 Jul 2009 15:00:11 +0200 Subject: performance problem with time.strptime() In-Reply-To: <6a2cc3a6-0e5c-4a43-bcf7-416a77253f97@h2g2000yqg.googlegroups.com> References: <6a2cc3a6-0e5c-4a43-bcf7-416a77253f97@h2g2000yqg.googlegroups.com> Message-ID: <4A4CAF5B.2040502@ccsg.de> Hi Casey Casey Webster wrote: > On Jul 2, 7:30 am, Nils R?ttershoff wrote: > > >> Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") >> Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' >> > > I'm not sure how much it will help but if you are only using the regex > to get the date/time group element, it might be faster to replace the > regex with: > > >>>> date_string = Line.split()[3][1:-1] >>>> Indeed this would give a little speed up (by 1000000 iteration approx 3-4 sec). But this would be only a small piece of the cake. Although thx :) The problem is that time.strptime() consult locale.py for each iteration. Here the hole cProfile trace: first with epoch and second with strptime (condensed): 5000009 function calls in 33.084 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 33.084 33.084 :1() 1 2.417 2.417 33.084 33.084 :2(inner) 1000000 9.648 0.000 30.667 0.000 time_test.py:30(epoch) 1 0.000 0.000 33.084 33.084 timeit.py:177(timeit) 1000000 3.711 0.000 3.711 0.000 {built-in method groupdict} 1000000 4.318 0.000 4.318 0.000 {built-in method match} 1 0.000 0.000 0.000 0.000 {gc.disable} 1 0.000 0.000 0.000 0.000 {gc.enable} 1 0.000 0.000 0.000 0.000 {gc.isenabled} 1000000 7.764 0.000 7.764 0.000 {map} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 1000000 5.225 0.000 5.225 0.000 {time.mktime} 2 0.000 0.000 0.000 0.000 {time.time} ################################################################ 29000009 function calls in 124.449 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 124.449 124.449 :1() 1 2.244 2.244 124.449 124.449 :2(inner) 1000000 3.500 0.000 33.559 0.000 _strptime.py:27(_getlang) 1000000 41.814 0.000 100.754 0.000 _strptime.py:295(_strptime) 1000000 4.010 0.000 104.764 0.000 _strptime.py:453(_strptime_time) 1000000 11.647 0.000 19.529 0.000 locale.py:316(normalize) 1000000 3.638 0.000 23.167 0.000 locale.py:382(_parse_localename) 1000000 5.120 0.000 30.059 0.000 locale.py:481(getlocale) 1000000 7.242 0.000 122.205 0.000 time_test.py:37(strptime) 1 0.000 0.000 124.449 124.449 timeit.py:177(timeit) 1000000 1.771 0.000 1.771 0.000 {_locale.setlocale} 1000000 1.735 0.000 1.735 0.000 {built-in method __enter__} 1000000 1.626 0.000 1.626 0.000 {built-in method end} 1000000 3.854 0.000 3.854 0.000 {built-in method groupdict} 1000000 1.646 0.000 1.646 0.000 {built-in method group} 2000000 8.409 0.000 8.409 0.000 {built-in method match} 1 0.000 0.000 0.000 0.000 {gc.disable} 1 0.000 0.000 0.000 0.000 {gc.enable} 1 0.000 0.000 0.000 0.000 {gc.isenabled} 2000000 2.942 0.000 2.942 0.000 {len} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 3000000 4.552 0.000 4.552 0.000 {method 'get' of 'dict' objects} 1000000 2.072 0.000 2.072 0.000 {method 'index' of 'list' objects} 1000000 1.517 0.000 1.517 0.000 {method 'iterkeys' of 'dict' objects} 2000000 3.113 0.000 3.113 0.000 {method 'lower' of 'str' objects} 2000000 3.233 0.000 3.233 0.000 {method 'replace' of 'str' objects} 2000000 2.953 0.000 2.953 0.000 {method 'toordinal' of 'datetime.date' objects} 1000000 1.476 0.000 1.476 0.000 {method 'weekday' of 'datetime.date' objects} 1000000 4.332 0.000 109.097 0.000 {time.strptime} 2 0.000 0.000 0.000 0.000 {time.time} -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Jul 2 09:00:44 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 02 Jul 2009 09:00:44 -0400 Subject: performance problem with time.strptime() In-Reply-To: <4A4C9A64.9030507@ccsg.de> References: <4A4C9A64.9030507@ccsg.de> Message-ID: <4A4CAF7C.9040800@ieee.org> Nils R?ttershoff wrote: > Hi everyone, > > In my free time I translate scripts from open source projects or write > my own, to train my python skills. ATM I convert the aplogmerge.pl from > awstats. It merges multiple apache logfiles and sort the output by the > timestamps of each line. My first version of this script hasn't a good > performance, so I started profiling. It turned out that the script spend > much time for converting the timestamps of the line into a struct_time > object. Here a code example (Python 2.6.2 on Ubuntu 7.10): > > Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' > > def strptime(): > m = Rec.match(Line) > if m: > date_string = m.group(1) > # date_string example: '02/Jul/2009:01:50:26' > return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") > > with timeit this functions takes approximate 125 sec and 29.004.081 > function calls (I've configured timeit with 1.000.000 runs). A look at > the output of cProfile told me that more than the half time is spent in > locale.py: > > 1000002 11.712 0.000 19.592 0.000 locale.py:316(normalize) > > 1000002 3.639 0.000 23.231 0.000 locale.py:382(_parse_localename) > > 1000002 5.162 0.000 30.271 0.000 locale.py:481(getlocale) > > > > I studied the time documentation and thought that I had to set TZ in os > environ: > > os.environ['TZ'] = 'Europe/Berlin' > > time.set() > > > but that had no effect. I don't know why time.strptime() looks every > time for my locale. Maybe it's a problem with my OS locale... However. > I've introduced a work around, which works perfectly for my problem. For > time comparison I could use any sort of time representation and so I > convert to epoch: > > # needed to translate month str to dig repr > > Shortmonth = {'Jan' : '01', > > 'Feb' : '02', > > 'Mar' : '03', > > 'Apr' : '04', > > 'May' : '05', > > 'Jun' : '06', > > 'Jul' : '07', > > 'Aug' : '08', > > 'Sep' : '09', > > 'Oct' : '10', > > 'Nov' : '11', > > 'Dec' : '12'} > > Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") > > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' > > def epoch(): > > m = Rec.match(Line) > > if m: > > result = m.groupdict() > > date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) > > date_tuple = map(int,date_tuple) > > return time.mktime(date_tuple) > > > with this workaround I had a speed up to 4 times; it tooks only 31 sec > with only 5.000.009 function calls. Maybe this helps some of you, who > had the similar problems with time conversion.... > > ...But one big question remains: Why time.strptime() checks everytime > the locale? had I missed something or could I have a problem with my OS > locale? > > With python 3.1 there is no difference, unless that time.strptime() took > approx 12 sec longer... :( > > regards, Nils > > > > Here a complete test script: > > #!/opt/python/2.6.2/bin/python > > import time > > import timeit > > import cProfile > > import re > > # needed to tranlate month str to dig repr > > Shortmonth = {'Jan' : '01', > > 'Feb' : '02', > > 'Mar' : '03', > > 'Apr' : '04', > > 'May' : '05', > > 'Jun' : '06', > > 'Jul' : '07', > > 'Aug' : '08', > > 'Sep' : '09', > > 'Oct' : '10', > > 'Nov' : '11', > > 'Dec' : '12'} > > Rec1 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") > > Rec2 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") > > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' > > def epoch(): > > m = Rec1.match(Line) > > if m: > > result = m.groupdict() > > date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) > > date_tuple = map(int,date_tuple) > > return time.mktime(date_tuple) > > def strptime(): > > m = Rec2.match(Line) > > if m: > > date_string = m.group(1) > > return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") > > if __name__ == "__main__": > > t1 = timeit.Timer("epoch()","from __main__ import epoch") > > t2 = timeit.Timer("strptime()", "from __main__ import strptime") > > cProfile.run("t1.timeit();print") > > print "" > > cProfile.run("t2.timeit();print") > > > > As you say, if you don't actually need the datetime fields, why waste time generating them. You gained a lot more than the time spent in locale(), so your algorithm must be faster than the one in strptime(). That frequently happens when you write a special case for code which is otherwise general. Notice that only a quarter of the time is spent in locale logic (30.271 secs). You can't add up the three terms since they're measuring the same thing. Every call to getlocale() results in a call to _parse_localename(), so the cumulative times of the latter are already included in the cumulative times in getlocale(). Why does it need to call locale() ? How else is it going to know how to spell the 11th month? From Joachim at Strombergson.com Thu Jul 2 09:15:18 2009 From: Joachim at Strombergson.com (=?UTF-8?B?Sm9hY2hpbSBTdHLDtm1iZXJnc29u?=) Date: Thu, 02 Jul 2009 15:15:18 +0200 Subject: PEP 376 In-Reply-To: <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> Message-ID: <4A4CB2E6.8010607@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! Tarek Ziad? wrote: > The prefix is a good idea but since it's just a checksum to control > that the file hasn't changed > what's wrong with using a weak hash algorithm like md5 or now sha1 ? Because it creates a dependency to an old algorithm that should be deprecated. Also using MD5, even for a thing like this might make people belive that it is an ok algorithm to use - "Hey, it is used by the default install in Python, so it must be ok, right?" If we flip the argument around: Why would you want to use MD5 instead of SHA-256? For the specific use case the performance will not (should not) be an issue. As I wrote a few mails ago, it is time to move forward from MD5 and designing something in 2009 that will be around for many years that uses MD5 is (IMHO) a bad design decision. > If someone wants to modify a file of a distribution he can recreate > the checksum as well, > the only secured way to prevent that would be to use gpg keys but > isn't that overkill for what we need ? Actually, adding this type of security would IMHO be a good idea. - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMsuYACgkQZoPr8HT30QELagCghfYyHyK5jnkS8DlaQ2ZX4KR8 W+YAniWSvWRvm47/xGu0thTaYioETY94 =2x3X -----END PGP SIGNATURE----- From Joachim at Strombergson.com Thu Jul 2 09:16:36 2009 From: Joachim at Strombergson.com (=?ISO-8859-1?Q?Joachim_Str=F6mbergson?=) Date: Thu, 02 Jul 2009 15:16:36 +0200 Subject: PEP368 and pixeliterators In-Reply-To: <2aeeb8de-c8f3-4e9d-98f7-d1fa71e1f3a3@d4g2000yqa.googlegroups.com> References: <2aeeb8de-c8f3-4e9d-98f7-d1fa71e1f3a3@d4g2000yqa.googlegroups.com> Message-ID: <4A4CB334.9080505@Strombergson.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Aloha! Casey Webster wrote: > Unless I'm totally misreading the PEP, the author does provide both > iterators. Quoting the PEP: > > Non-planar images offer the following additional methods: > > pixels() -> iterator[pixel] > > Returns an iterator that iterates over all the pixels in the image, > starting from the top line and scanning each line from left to > right. > See below for a description of the pixel objects. Ah! I knew there was a reason for me wearing glasses. ;-) Go PEP368! - -- Med v?nlig h?lsning, Yours Joachim Str?mbergson - Alltid i harmonisk sv?ngning. ======================================================================== Kryptoblog - IT-s?kerhet p? svenska http://www.strombergson.com/kryptoblog ======================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkpMszQACgkQZoPr8HT30QGmVACfezmlZ+K93exuIEoUDJyzNzgM 3dcAn1x4Ghu9AlMKEme2og4sK4TwhXaR =bNbl -----END PGP SIGNATURE----- From davea at dejaviewphoto.com Thu Jul 2 09:21:40 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Thu, 02 Jul 2009 09:21:40 -0400 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: References: <4A4CA278.4050604@ieee.org> Message-ID: <4A4CB464.80607@dejaviewphoto.com> Christian Heimes wrote: > Dave Angel wrote: > >> Look also at 'del' a command in the language which explicitly deletes an >> object. >> > > No, you are either explaining it the wrong way or you have been fallen > for a common misinterpretation of the del statement. The del statement > only removes the object from the current scope. This means it decreases > the reference count by one. It does *not* remove the object. > > Christian > > > You're right of course. What I was trying to say was it deletes the reference to the object. Unlike obj = None, del obj removes the reference (attribute) entirely. Although I don't know what it should be called if it's a local variable. Perhaps it "unbinds" the name. DaveA From Mariosky at gmail.com Thu Jul 2 09:27:36 2009 From: Mariosky at gmail.com (Mario Garcia) Date: Thu, 2 Jul 2009 06:27:36 -0700 (PDT) Subject: Trying to use sets for random selection, but the pop() method returns items in order References: <05dde563-a8f1-45d1-8f59-7740c58787ba@e21g2000yqb.googlegroups.com> Message-ID: <32f177b3-f305-4ff7-be64-ed161eb7c38d@h11g2000yqb.googlegroups.com> This could be better: >>> import random >>> population = range(10) >>> choice = random.choice(population) >>> population.remove(choice) >>> print population >>> print population [0, 1, 2, 3, 4, 5, 6, 8, 9] That was my idea with the previous pop(), remove from the population a certain number of elements at random. In the docs pop is defined as: Remove and return an arbitrary element from the set. My mistake: arbitrary is not the same as random :( Mario From lie.1296 at gmail.com Thu Jul 2 09:33:38 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 13:33:38 GMT Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <025949cb$0$20671$c3e8da3@news.astraweb.com> Message-ID: Eric S. Johansson wrote: > Steven D'Aprano wrote: >> That assumes that every word is all caps. In practice, for real-life >> Python code, I've tripled the vocal load of perhaps one percent of your >> utterances, which cuts your productivity by 2%. >> >> If you have 10000 words in you per day, and one percent get wrapped with >> a leading and trailing "capslock", you have: >> >> 9900 regular words plus 100 constants >> >> versus >> >> 9700 regular words plus 100 constants plus 200 by capslock. >> >> Your productivity goes down by 200 words out of 10,000, or two percent. > > oh I wish it was so simple. I would absolutely you if it was that simple the > real world. Somehow, I fear that you wouldn't understand unless you sat next to > me and watch me try to write code to a variety of standards. > > In practice, my experience says that with a really good speech driven > environment for programming, IT, vocal load probably by a quarter to a half and > be more compliant with current fashion for naming. I've tried (Windows') speech recognition some time before, and I know it's one heck of a mess trying to type anything with it. I have a fully working hand, so I don't actually need a voice recognition, but I remember trying to write a single (not code) paragraph solely by recognition. It took me over an hour correcting all those mistakes and it really starts to gets into the nerves after the first sentence. I did a little victory dance, after doing the first paragraph. I know they gets better with time as they learn our speech pattern and we learn how to pronounce the way the voice recognition expects. But one thing I can't imagine is using voice recognition for writing code (heck... that'll be a nightmare inside a nightmare) >>>>> Heck, have you ever noticed how most Python smart editors can't even >>>>> indent properly according to local contexts. Emacs is the only one and >>>>> even that one sometimes fails >>>> I use kwrite for editing, and I can't say I've ever noticed a failure. >>> okay, it fails from a speech recognition user perspective. Put the >>> cursor on the line and hit the tab key. Insert spaces/tabs in the line >>> of text. Not good for speech recognition. If mess of indentation and >>> hit the tab key, it doesn't automatically indent to the right location. >>> I'll be damned if I'm in the sit there speaking >>> "tabkeytabkeytabkeytabkeytabkey" because the error should take care of >>> it for me and save my voice. Like I said, Emacs does it correctly and >>> nobody else does as far as I can tell. >> I don't understand what you're describing. >> >> If you set kwrite to use "Python style" indent mode, then starting a new >> line will automatically indent to either the current indent level, or one >> extra indent level if the line ends with a colon. You shouldn't need to >> indent forward more than one level at a time when writing Python code, >> although you will need to dedent multiple levels on occasion. > > I recognize your confusion. It's very common when describing this problem. If I > had some way of making a movie of what and describing, obviously be more clear. > I'll see if I can make a movie with my camera that demonstrates the problem. It > probably won't happen fast but, what I tried your editor, it failed with a > trivial effort. I think I just hit the tab key multiple times in different > places on the line. In all cases it did the wrong thing. For me, the right thing > is to force the line to the right indentation based on the previous line to > matter how may times you get the tab key or where you are on that line when you > hit the tab key. I could be in the middle of the line, the end of line, start a > line, the start of the string and if I hit a tab key, that line should go to > exactly the right place stayed there no matter how me more times I typed a tab key. > > But this might be what you're getting confused. What I'm really asking for is > the ability to say "force indent" and have the line indent exactly correctly > based on context. It doesn't have to be the tab key. The tab key is the method > of convenience. Maybe we need an editor that could sort of understand something like: "constant red" and apply the appropriate code-sensitive convention transformation (in python it'll be RED) "class foo inheriting object" and typed "class Foo(object):\n\t" "indent", "dedent" put a tab or delete a tab (or softtab) but for all those, we're going to need a specialized text editor or perhaps a context-sensitive voice recognition software. The simplest solution would be a vim plugin or emacs mode that is specially tailored to recognize these commands and apply the appropriate transformations depending on the contexts. >> I've already said it's your right to reject the convention for your own >> code. Go right ahead. This is only a problem if you're editing other >> people's code which is using uppercase constants. > > exactly. If I'm unable to comply with local conventions, I cannot work within an > organization and be an active member of the team. This is one example of what I > mean by unintentional discriminatory effects of a given decision. rhetorical > devices aside, I think the right way to deal with accessibility issues and > decisions leading to discrimination is to make the appropriate tools if possible > for the individual. But I'm sure that would be a very long discussion we would > have over a couple of beers. Personally, I think it'd be very difficult to try to change conventions that were made for a job that requires so much typing. In programming, typing is almost a requirement. I'm aware people will still try to insist on doing things they love the most no matter what happens, but I still think if the disabled want to program, they're the one that need to adjust their work flow and definitely with the current voice recognition technology, they really need better tools. >> Do you wish to raise the profile of disabled programmers? > > yes. Not exactly sure of the best way but I would like to do that through > providing tools enabling them to participate on equal footing with not yet > disabled programmers. I really don't like saying "take pity on the poor > programmers with broken hands." I'd rather say "you need talent? Here's a pool > you have previously thrown away but they can now generate code faster than the > keyboard users. Stephen Hawking didn't stop with his experiments when his whole body can't move. I agree that disabilities shouldn't stop people from programming, which is mostly the head's job. I think if we can make voice recognition faster and easier than typing (like in those sci-fi movies), disabled or not we will all benefit from it. Maybe some day, we all, disabled or not, might code with voice recognition and keyboard will be considered obsolete. From lie.1296 at gmail.com Thu Jul 2 09:40:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 13:40:49 GMT Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> Message-ID: Eric S. Johansson wrote: > I've been working with speech recognition for 15 years. I've written something > on the order of 10,000 lines of Python code both as open source and private > projects. I've tried it least two dozen editors and they all fail miserably > because they're focused on keyboard use (but understandable) I get good > recognition accuracy because I train the system and then I let it train me. >> For coding, you might want to investigate a tool like Dasher[1] which >> offers an alternate form of input. It allows for custom >> vocabularies/keymaps if you need, as well as more precise specification >> of a full keyboard (caps vs. mixed-case, specific punctuation >> characters, etc). The predictive entry should be smart enough to pick >> up previously entered constants/terms saving you entry speed. It can >> also be driven by a wide variety of pointing devices (mouse, trackball, >> touchpad, head-tracker, gyro-input, etc). > > I've tried it, it's quite promising but my hands term are sufficiently that I > can't target accurately. This is a problem for me with mice as well. Maybe these > tiny little spit dot icons and webpages drive me insane because it takes me two > or three tries to put the right spot. You might want to try eye-ball tracking device. Dashers are designed for that as well. You won't need to use your hand to type with it. From nils at ccsg.de Thu Jul 2 09:51:25 2009 From: nils at ccsg.de (=?ISO-8859-1?Q?Nils_R=FCttershoff?=) Date: Thu, 02 Jul 2009 15:51:25 +0200 Subject: Spam? Re: whizBase vs. Python In-Reply-To: <0bc58d9a-37cf-4e09-a28f-5edb0489ea30@h8g2000yqm.googlegroups.com> References: <620ad7d3-6c1a-4869-b844-1951095bb92c@g19g2000yql.googlegroups.com> <0bc58d9a-37cf-4e09-a28f-5edb0489ea30@h8g2000yqm.googlegroups.com> Message-ID: <4A4CBB5D.9070104@ccsg.de> NurAzije wrote: > On Jun 29, 11:04 am, Tim Harig wrote: > >> On 2009-06-29, NurAzije wrote: >> >> >>> Hi, >>> I am working on a study and I need expert opinion, I did not work with >>> Python before, can anyone help me with a comparison betweenWhizBase >>> (www.whizbase.com) and Python please. >>> >> Given posts like:http://groups.google.com/group/Server-side-programing/browse_thread/t... >> is this just thinly veiled spam? The domain and the posting IP address are >> both out of Sarajevo, Bosnia. >> >> Obviously you alreadly know your product is nothing but a database >> macro processor. Python is a dynamic programming language and therefore >> far more capable. >> >> Your product is proprietary with less capability while Python is free and >> powerful. >> > > Hi Tim, > I am not a spammer, WhizBase is from Sarajevo and I am from Sarajevo, > for that it is interesting for me. > > regards, > Ashraf Gheith > Hi Ashraf, hm... but it seems you know WhizBase very well, here a blog (http://whizbase-scripting-language.blogspot.com/) of you with very detailed information about WhizBase. Why you are asking nearly any programming list/group about the difference?: http://www.highdots.com/forums/javascript-discussion-multi-lingual/whizbase-vs-javascript-281242.html http://groups.google.com/group/alt.comp.lang.learn.c-c++/browse_thread/thread/a7a47adb904319ad http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/9dfa76ad94d75168/01a999b7b8638621?lnk=raot http://www.mail-archive.com/dotnetdevelopment at googlegroups.com/msg07381.html Hm here the main blog: http://whizbase.blogspot.com/ There is a second contributer Faik Djikic and Whizbase is from a company called "Djikic Software Development"...... What's the Topic of your study? Whizbase compared to other languages? Why Whizbase? Is your study public and if so were we could read it? ;) Regards, Nils -------------- next part -------------- An HTML attachment was scrubbed... URL: From jcd at sdf.lonestar.org Thu Jul 2 10:15:49 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Thu, 02 Jul 2009 10:15:49 -0400 Subject: Idioms and Anti-Idioms Question In-Reply-To: References: Message-ID: <1246544149.5775.0.camel@aalcdl07> On Wed, 2009-07-01 at 17:19 +1200, Lawrence D'Oliveiro wrote: > In message , J. Cliff > Dyer wrote: > > > If the lines got separated, a leading + could disappear into its line > > without any errors showing up. A trailing + would raise a syntax error. > > Unless, of course, it was moved onto the previous line as part of whatever > caused the separation of the lines. How would you guard against that? > Can you give an example of what you mean? From gil.shinar at gmail.com Thu Jul 2 10:17:58 2009 From: gil.shinar at gmail.com (eranlevi) Date: Thu, 2 Jul 2009 07:17:58 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> <5efbbfef-6dde-4244-ab6c-6d30be5ac0cc@x17g2000yqd.googlegroups.com> Message-ID: <20e625ef-8fa2-4ed5-9ae4-a5b3fa2b1019@o6g2000yqj.googlegroups.com> On Jul 2, 1:08?am, s... at pobox.com wrote: > ? ? Gil> There's no such group as python-sybase :-( > > http://sourceforge.net/mailarchive/forum.php?forum_name=python-sybase... > > S Thanks :-) From gil.shinar at gmail.com Thu Jul 2 10:19:36 2009 From: gil.shinar at gmail.com (eranlevi) Date: Thu, 2 Jul 2009 07:19:36 -0700 (PDT) Subject: Timeout when connecting to sybase DBS References: <2dc9f53f-acc0-44cd-9b5b-7062ab401030@a38g2000yqc.googlegroups.com> <794c83d9-9a53-4e03-89d0-1c369cdc70ec@c36g2000yqn.googlegroups.com> Message-ID: On Jul 2, 1:07?am, s... at pobox.com wrote: > ? ? Gil> Are you saying, that when you trying to connect to a sybase DBS > ? ? Gil> server and the DBS or the server is down, you get an error after a > ? ? Gil> few seconds and not after a few minutes? > > Yes, though thankfully our server tends to almost always be up. > > ? ? Gil> Which python version are you using? > > We're still running 2.4.5 at work and have a slightly hacked very old > version of the python-sybase package (0.36). > > -- > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > ? ? when i wake up with a heart rate below 40, i head right for the espresso > ? ? machine. -- chaos @ forums.usms.org That is really strange :-( I'll ask in the python-sybase group Thanks From nobody at nowhere.com Thu Jul 2 10:23:51 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 02 Jul 2009 15:23:51 +0100 Subject: PEP368 and pixeliterators References: Message-ID: On Thu, 02 Jul 2009 10:32:04 +0200, Joachim Str?mbergson wrote: > I just read the PEP368 and really liked the proposed idea, sound like a > great battery addition to include in the std lib: > > http://www.python.org/dev/peps/pep-0368/ Unfortunately, it's too simplistic, meaning that most of the existing image formats used by the various GUI toolkits (and OpenGL) will need to be retained. Obvious omissions include support for arbitrary numbers of bits per component (e.g. R5G5B5A1), component, pixel and row strides (padding), mixing packed and planar components (e.g. packed RGB with a separate alpha plane), and storage orientation (top-to-bottom or bottom-to-top). Ideally, an Image should be able to use any sane storage format, so that you don't have to keep converting the Image to an application-specific format. NumPy's arrays would probably be a good model here. Essentially, each component would have a base pointer plus horizontal and vertical strides measured in bits. The strides wouldn't necessarily be the same for all components (allowing for an R8G8B8 plane with a 3-byte stride plus a distinct A8 plane with a 1-byte stride), vertical stride wouldn't necessarily be greater than horizontal stride (allowing transposition), strides could be negative (allowing mirroring), and could be less than a byte (allowing e.g. a 1-bpp alpha channel, planar 4-bpp data, 12-bpp greyscale, etc). AFAICT, this would allow all common image storage formats to be represented. The most common exception is 8-bit RGB data using a 6x6x6 colour cube, but that's not common enough to be worth the added complexity. The interface should also be flexible enough to allow an Image to be a "proxy" for a non-local image (e.g. one held in video RAM or in the X server). You wouldn't be able to get a buffer for the image data, but you should at least be able to obtain the dimensions and format of such images, and ideally copy the data (or rectangular subregions) to/from a local image. From hv at tbz-pariv.de Thu Jul 2 10:50:34 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Thu, 02 Jul 2009 16:50:34 +0200 Subject: logging of strings with broken encoding Message-ID: <7b3vpqF20nct6U1@mid.individual.net> Hi, I have bug in my code, which results in the same error has this one: https://bugs.launchpad.net/bzr/+bug/295653 {{{ Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit self.stream.write(fs % msg.encode("UTF-8")) .. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128) }}} I run Python 2.6. In SVN the code is the same (StreamHandler ... def emit...): http://svn.python.org/view/python/branches/release26-maint/Lib/logging/__init__.py?revision=72507&view=markup I think msg.encode("UTF-8", 'backslashreplace') would be better here. What do you think? Should I fill a bugreport? Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From vertespain at gmail.com Thu Jul 2 11:09:43 2009 From: vertespain at gmail.com (masher) Date: Thu, 2 Jul 2009 08:09:43 -0700 (PDT) Subject: multiprocessing: pool with blocking queue Message-ID: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> Hi, I am trying to implement a multiprocessing pool that assigns tasks from a blocking queue. My situation is a pretty classic producer/ consumer conundrum, where the producer can produce much faster than the consumers can consume. The wrinkle in the story is that the producer produces objects that consume large amounts of memory, so I would like the queue to block when it reaches, say, twice the number of tasks as there are processes in the pool, so that I don't devour my RAM. I have been able to implement this one way, but I am somewhat displeased with the result. Code speaks louder than words, so here is a brief example of the technique I am using, followed by an explanation of why I am unhappy with it: === #!/usr/bin/env python3 import time import random from multiprocessing import Pool, Queue, M def procfunc(queue): time.sleep(random.random() * 2) return queue.get()*2 def printrange(n): for i in range(n): print("generated " + str(i)) yield i if __name__ == "__main__": sm = Manager() pool = Pool() queuelen = len(pool._pool) * 2 queue = sm.Queue(queuelen) for i in printrange(100): queue.put(i) pool.apply_async(procfunc, (queue,), callback=print) pool.close() pool.join() === The reason I am unhappy with this trick is that if you examine the source code of pool.py, you will note that the class Pool already uses an internal queue, "_taskqueue" from which the tasks are assigned to processes in the pool. Particularly: def __init__(self, processes=None, initializer=None, initargs=()): self._setup_queues() self._taskqueue = queue.Queue() ...snip It seems to me that if I could only subclass and do queuelen = len(pool._pool) * 2 self._taskqueue = queue.Queue(queuelen) later in the constructor, once the pool length has been established, I would have a much more elegant, transparent solution to the problem. Unfortunately, the design of the Pool class is such that actually implementing this solution would be very hackish and inelegant. If only, say, _setup_queues() were called after the _taskqueue assignment, then I could override it. My questions, then, is: Is there a more elegant/pythonic way of doing what I am trying to do with the current Pool class? If the verdict is no, I'll be happy to file a bug report. From dns4 at cornell.edu Thu Jul 2 11:09:58 2009 From: dns4 at cornell.edu (David Smith) Date: Thu, 02 Jul 2009 11:09:58 -0400 Subject: logging of strings with broken encoding In-Reply-To: <7b3vpqF20nct6U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > Hi, > > I have bug in my code, which results in the same error has this one: > > https://bugs.launchpad.net/bzr/+bug/295653 > {{{ > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit > self.stream.write(fs % msg.encode("UTF-8")) > .. > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128) > }}} > > I run Python 2.6. In SVN the code is the same (StreamHandler ... def emit...): > http://svn.python.org/view/python/branches/release26-maint/Lib/logging/__init__.py?revision=72507&view=markup > > I think msg.encode("UTF-8", 'backslashreplace') would be better here. > > What do you think? > > Should I fill a bugreport? > > Thomas > > I think you have to decode it first using the strings original encoding whether that be cp1252 or mac-roman or any of the other 8-bit encodings. Once that's done, you can encode in UTF-8 --David From __peter__ at web.de Thu Jul 2 11:16:29 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2009 17:16:29 +0200 Subject: logging of strings with broken encoding References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > I have bug in my code, which results in the same error has this one: > > https://bugs.launchpad.net/bzr/+bug/295653 > {{{ > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit > self.stream.write(fs % msg.encode("UTF-8")) > .. > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: > ordinal not in range(128) }}} > > I run Python 2.6. In SVN the code is the same (StreamHandler ... def > emit...): > http://svn.python.org/view/python/branches/release26- maint/Lib/logging/__init__.py?revision=72507&view=markup > > I think msg.encode("UTF-8", 'backslashreplace') would be better here. > > What do you think? That won't help. It's a *decoding* error. You are feeding it a non-ascii byte string. Peter From wells at submute.net Thu Jul 2 11:32:40 2009 From: wells at submute.net (Wells Oliver) Date: Thu, 2 Jul 2009 10:32:40 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor Message-ID: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> Is there some kind of mysterious logic to how the the columns are ordered when executing the following: sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' GROUP BY player_id" % (start, date) cursor.execute(sql) for row in cursor.fetchall(): print row.keys() What I get is: ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] Neither alphabetical nor the order in which they were specified in the query nor... any seeming order I can suss out. Any ideas? Thanks! (cursor being a MySQLdb.cursors.DictCursor object.) -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Thu Jul 2 11:48:18 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 02 Jul 2009 10:48:18 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> Message-ID: <4A4CD6C2.2030103@tim.thechases.com> > sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) > AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN > Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' > GROUP BY player_id" % (start, date) > cursor.execute(sql) > > for row in cursor.fetchall(): > print row.keys() > > What I get is: > > ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] > > Neither alphabetical nor the order in which they were specified in the query > nor... any seeming order I can suss out. Any ideas? Thanks! > > (cursor being a MySQLdb.cursors.DictCursor object.) My guess is you're experiencing the fact that dicts are unordered by nature which allows it to return in any order it likes (usually per the internal representation/storage). -tkc From lie.1296 at gmail.com Thu Jul 2 11:49:46 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 15:49:46 GMT Subject: logging of strings with broken encoding In-Reply-To: <7b3vpqF20nct6U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > Hi, > > I have bug in my code, which results in the same error has this one: > > https://bugs.launchpad.net/bzr/+bug/295653 > {{{ > Traceback (most recent call last): > File "/usr/lib/python2.6/logging/__init__.py", line 765, in emit > self.stream.write(fs % msg.encode("UTF-8")) > .. > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128) > }}} What's the encoding of self.stream? Is it sys.stdout/sys.stderr or a file object? From hv at tbz-pariv.de Thu Jul 2 11:53:14 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Thu, 02 Jul 2009 17:53:14 +0200 Subject: logging of strings with broken encoding In-Reply-To: <7b3vpqF20nct6U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> Message-ID: <7b43faF21l89hU1@mid.individual.net> My quick fix is this: class MyFormatter(logging.Formatter): def format(self, record): msg=logging.Formatter.format(self, record) if isinstance(msg, str): msg=msg.decode('utf8', 'replace') return msg But I still think handling of non-ascii byte strings should be better. A broken logging message is better than none. And, if there is a UnicodeError, handleError() should not send the message to sys.stderr, but it should use emit() of the current handler. In my case sys.stderr gets discarded. Its very hard to debug, if you don't see any logging messages. Thomas Thomas Guettler schrieb: > Hi, > > I have bug in my code, which results in the same error has this one: > ... -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From wells at submute.net Thu Jul 2 11:57:05 2009 From: wells at submute.net (Wells Oliver) Date: Thu, 2 Jul 2009 10:57:05 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <4A4CD6C2.2030103@tim.thechases.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> <4A4CD6C2.2030103@tim.thechases.com> Message-ID: <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> Will this order at least be the same for that same query every time the script is executed? On Thu, Jul 2, 2009 at 10:48 AM, Tim Chase wrote: > sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) >> AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN >> Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' >> GROUP BY player_id" % (start, date) >> cursor.execute(sql) >> >> for row in cursor.fetchall(): >> print row.keys() >> >> What I get is: >> >> ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] >> >> Neither alphabetical nor the order in which they were specified in the >> query >> nor... any seeming order I can suss out. Any ideas? Thanks! >> >> (cursor being a MySQLdb.cursors.DictCursor object.) >> > > My guess is you're experiencing the fact that dicts are unordered by nature > which allows it to return in any order it likes (usually per the internal > representation/storage). > > -tkc > > > > -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From james at agentultra.com Thu Jul 2 12:06:58 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 02 Jul 2009 12:06:58 -0400 Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> Message-ID: <851vozf4rh.fsf@agentultra.com> masher writes: > My questions, then, is: Is there a more elegant/pythonic way of doing > what I am trying to do with the current Pool class? Forgive me, I may not fully understand what you are trying to do here (I've never really used multiprocessing all that much)... But couldn't you just assign your own Queue object to the Pool instance? From robert.kern at gmail.com Thu Jul 2 12:23:40 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 02 Jul 2009 11:23:40 -0500 Subject: deleting certain entries in numpy array In-Reply-To: <4a4c7fd9$1@news.fhg.de> References: <4a4b7763$1@news.fhg.de> <4a4c7fd9$1@news.fhg.de> Message-ID: On 2009-07-02 04:40, Sebastian Schabe wrote: > Robert Kern schrieb: > > You will want to ask numpy questions on the numpy mailing list. > > > > http://www.scipy.org/Mailing_Lists > > > > I ever thought news-groups are the right way for questions like this. > And the mailing list page just confuses me, but I'm trying to get used > to it. You may want to try GMane, then: http://dir.gmane.org/gmane.comp.python.numeric.general It's how I read this list, for instance. -- 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 vertespain at gmail.com Thu Jul 2 12:25:54 2009 From: vertespain at gmail.com (masher) Date: Thu, 2 Jul 2009 09:25:54 -0700 (PDT) Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> <851vozf4rh.fsf@agentultra.com> Message-ID: On Jul 2, 12:06?pm, J Kenneth King wrote: > masher writes: > > My questions, then, is: Is there a more elegant/pythonic way of doing > > what I am trying to do with the current Pool class? > > Forgive me, I may not fully understand what you are trying to do here > (I've never really used multiprocessing all that much)... > > But couldn't you just assign your own Queue object to the Pool instance? That's basically my question. It does not appear as though there is any straightforward way of doing this because of the design of Pool's __init__ method, which passes _taskqueue to several functions. Hence, even if I were to reassign _taskqueue after __init__, that wouldn't change anything. From ethan at stoneleaf.us Thu Jul 2 12:38:56 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 02 Jul 2009 09:38:56 -0700 Subject: regex question on .findall and \b Message-ID: <4A4CE2A0.5040407@stoneleaf.us> Greetings! My closest to successfull attempt: Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.9.1 -- An enhanced Interactive Python. In [161]: re.findall('\d+','this is test a3 attempt 79') Out[161]: ['3', '79'] What I really want in just the 79, as a3 is not a decimal number, but when I add the \b word boundaries I get: In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') Out[162]: [] What am I missing? ~Ethan~ From tim.wintle at teamrubber.com Thu Jul 2 12:49:48 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Thu, 02 Jul 2009 17:49:48 +0100 Subject: Dict ordering [was: Re: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor] In-Reply-To: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> Message-ID: <1246553388.18497.5.camel@tim-laptop> On Thu, 2009-07-02 at 10:32 -0500, Wells Oliver wrote: > for row in cursor.fetchall(): > print row.keys() > > What I get is: > > ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] > > Neither alphabetical nor the order in which they were specified in the > query nor... any seeming order I can suss out. Any ideas? Thanks! keys in a dict are not guaranteed to be in any specific order. It's not specific to the MySQLdb cursor. IIRC it's related to the hashes of the keys (but obviously not in strictly increasing order). You definitely shouldn't rely on the ordering (hence the need for an OrderedDict. e.g. >>> a = ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] >>> for s in a: ... print hash(s) ... -3369365635700083487 8448050754076166 9216055368083080 9344056137084361 9600028874 9216027721 1844482854915224472 8832053061079775 From python.list at tim.thechases.com Thu Jul 2 12:50:09 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 02 Jul 2009 11:50:09 -0500 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> <4A4CD6C2.2030103@tim.thechases.com> <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> Message-ID: <4A4CE541.9030308@tim.thechases.com> > Will this order at least be the same for that same query every time the > script is executed? I wouldn't count on it. The order is only defined for the one iteration (result of the keys() call). If the order matters, I'd suggest a double-dispatch with a non-dict (regular/default) query result, something like header_map = dict( (desc[0], i) for i,desc in enumerate(cursor.description) ) for row in cursor.fetchall(): print row[header_map['league']] If you have a lot of fields, I often use a closure to simplify the coding: for row in cursor.fetchall(): item = lambda s: row[headermap[s]] print item('league') print item('BB') print item('player_id') That way, the row stays in order, if you need it that way: league, bb, hr, ip, k, h, id, er = row -tkc From stefan_ml at behnel.de Thu Jul 2 13:01:37 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 02 Jul 2009 19:01:37 +0200 Subject: logging of strings with broken encoding In-Reply-To: <7b43faF21l89hU1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> Message-ID: <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> Thomas Guettler wrote: > My quick fix is this: > > class MyFormatter(logging.Formatter): > def format(self, record): > msg=logging.Formatter.format(self, record) > if isinstance(msg, str): > msg=msg.decode('utf8', 'replace') > return msg > > But I still think handling of non-ascii byte strings should be better. > A broken logging message is better than none. Erm, may I note that this is not a problem in the logging library but in the code that uses it? How should the logging library know what you meant by passing that byte string in the first place? And where is the difference between accidentally passing a byte string and accidentally passing another non-printable object? Handling this "better" may simply hide the bugs in your code, I don't find that's any "better" at all. Anyway, this has been fixed in Py3. Stefan From ethan at stoneleaf.us Thu Jul 2 13:12:32 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 02 Jul 2009 10:12:32 -0700 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A4CEA80.4050803@stoneleaf.us> Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit > (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? > > ~Ethan~ ARGH!! Okay, I need two \\ so I'm not trying to match a backspace. I knew (okay, hoped ;) I would figure it out once I posted the question and moved on. *sheepish grin* From lie.1296 at gmail.com Thu Jul 2 13:14:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 17:14:34 GMT Subject: logging of strings with broken encoding In-Reply-To: <7b43faF21l89hU1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> Message-ID: <_V53m.2227$ze1.167@news-server.bigpond.net.au> Thomas Guettler wrote: > My quick fix is this: > > class MyFormatter(logging.Formatter): > def format(self, record): > msg=logging.Formatter.format(self, record) > if isinstance(msg, str): > msg=msg.decode('utf8', 'replace') > return msg > > But I still think handling of non-ascii byte strings should be better. > A broken logging message is better than none. > The problem is, python 2.x assumed the default encoding of `ascii` whenever you don't explicitly mention the encoding, and your code apparently broke with that assumption. I haven't looked at your code, but others have suggested that you've fed the logging module with non-ascii byte strings. The logging module can only work with 1) unicode string, 2) ascii-encoded byte string If you want a quick fix, you may be able to get away with repr()-ing your log texts. A proper fix, however, is to pass a unicode string to the logging module instead. >>> logging.warn('?') # or logging.warn('\xd1\x8b') Traceback (most recent call last): File "/usr/lib64/python2.6/logging/__init__.py", line 773, in emit stream.write(fs % msg.encode("UTF-8")) UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 13: ordinal not in range(128) >>> logging.warn(repr('?')) WARNING:root:'\xd1\x8b' >>> logging.warn(u'?') WARNING:root:? From python.list at tim.thechases.com Thu Jul 2 13:17:22 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 02 Jul 2009 12:17:22 -0500 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A4CEBA2.9040303@tim.thechases.com> Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? The sneaky detail that the regexp should be in a raw string (always a good practice), not a cooked string: r'\b\d+\b' The "\d" isn't a valid character-expansion, so python leaves it alone. However, I believe the "\b" is a control character, so your actual string ends up something like: >>> print repr('\b\d+\b') '\x08\\d+\x08' >>> print repr(r'\b\d+\b') '\\b\\d+\\b' the first of which doesn't match your target string, as you might imagine. -tkc From lie.1296 at gmail.com Thu Jul 2 13:18:00 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 17:18:00 GMT Subject: invoking a method from two superclasses In-Reply-To: References: <31fe4bef-6738-4de5-b9bb-c0dc42c695a7@o6g2000yqj.googlegroups.com> Message-ID: Carl Banks wrote: > On Jun 30, 6:23 pm, Carl Banks wrote: >> On Jun 30, 5:34 pm, Mitchell L Model wrote: >> >>> Allow me to add to my previous question that certainly the superclass >>> methods can be called explicitly without resorting to super(), e.g.: >>> class C(A, B): >>> def __init__(self): >>> A.__init__(self) >>> B.__init__(self) >>> My question is really whether there is any way of getting around the >>> explicit class names by using super() >> Yes there is: just make sure that all subclasses also call super. > > And by subclasses I mean base classes, of course. ugh > > > Carl Banks super() is short for two things: 1) superharmful 2) superuseful From sjoerd at acm.org Thu Jul 2 13:18:07 2009 From: sjoerd at acm.org (Sjoerd Mullender) Date: Thu, 02 Jul 2009 19:18:07 +0200 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A4CEBCF.9010203@acm.org> On 2009-07-02 18:38, Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit > (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? > > ~Ethan~ Try this: >>> re.findall(r'\b\d+\b','this is test a3 attempt 79') ['79'] The \b is a backspace, by using raw strings you get an actual backslash and b. -- Sjoerd Mullender From philip at semanchuk.com Thu Jul 2 13:22:53 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 2 Jul 2009 13:22:53 -0400 Subject: dealloc function in python extend c module In-Reply-To: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> References: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> Message-ID: <8599A253-D2C9-4EE4-AF4F-AEF658BA2033@semanchuk.com> On Jul 2, 2009, at 2:11 AM, Shen, Yu-Teh wrote: > I create my extend type something like http://www.python.org/doc/current/extending/newtypes.html > . > And my type has a member which is a pointer point to my allocate > memory ( no ref count). > ex: > --------------------------------------------------- > typedef struct { > PyObject_HEAD > /* Type-specific fields go here. */ > mytype *p; > } noddy_NoddyObject; > > And i write dealloc function like this: > --------------------------------------------------- > static void > Noddy_dealloc(Noddy* self) > { > delete p; > self->ob_type->tp_free((PyObject*)self); > } > > And I found it's strange that it didn't call dealloc when there is no > reference. > > ex: > a = Noddy() > b = a > del a > del b > # i think there is no ref to the object, and it should call dealloc > but it didn't call!!! > > could anyone tell me what happened? Hi Shen, I'm no expert on Python memory management, but since no once else has answered your question I'll tell you what I *think* is happening. Python doesn't delete objects as soon as they're dereferenced. It merely marks them as safe for garbage collection (GC). If GC never happens (and it might not in a small test program), your dealloc function won't run. Now some more knowledgeable person will probably correct me. =) You can control the garbage collector manually with Python's gc module. You will probably never want to control the garbage collector manually in ordinary programs, but it can be useful for testing. Hope this helps Philip From sajmikins at gmail.com Thu Jul 2 13:23:55 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 10:23:55 -0700 (PDT) Subject: question of style Message-ID: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Hey I was hoping to get your opinions on a sort of minor stylistic point. These two snippets of code are functionally identical. Which would you use and why? The first one is easier [for me anyway] to read and understand, but slightly less efficient, while the second is [marginally] harder to follow but more efficient. ## First snippet if self.higher is self.lower is None: return if self.lower is None: return self.higher if self.higher is None: return self.lower ## Second snippet if self.higher is None: if self.lower is None: return return self.lower if self.lower is None: return self.higher What do you think? (One minor point: in the first snippet, the "is None" in the first line is superfluous in the context in which it will be used, the only time "self.lower is self.higher" will be true is when they are both None.) From chambon.pascal at gmail.com Thu Jul 2 13:34:00 2009 From: chambon.pascal at gmail.com (Pascal Chambon) Date: Thu, 02 Jul 2009 19:34:00 +0200 Subject: Direct interaction with subprocess - the curse of blocking I/O In-Reply-To: References: Message-ID: <4A4CEF88.80201@gmail.com> Thank you all for the comments >you might want something like Expect. Yes "Expect" deals with such things, unfortunately it's posix only (due to the PTY module requirement...); whereas I'd like to find generic ways (i.e at least windows/linux/mac recipes) > The latter is inherently tricky (which is why C's popen() lets you connect > to stdin or stdout but not both). You have to use either multiple threads, > select/poll, or non-blocking I/O. > > If the child's output is to the console, it should presumably be the > former, i.e. piping stdin but allowing the child to inherit stdout, in > which case, where's the problem? Or are you piping its stdout via Python > for the hell of it It's well a too-way piping that I'd like to get ; but actually, even a single-way piping is error-prone : if I try to send data to the child's stdin, and this child never wants to receive that data (for whatever reason), the parent thread will be forever stuck. I can use multiple threads, but it doesn't fully solve the problem, because having even a single thread stuck might prevent the process from terminating, on some operating systems... I'd need a way to deblock I/O blocking threads (but even closing the pipe isn't sure to do it properly - the closing might be delayed by the I/O operations wating). >I would guess the architectural differences are so great that an attempt >to "do something simple" is going to involve architecture-specific code. >and I personally wouldn't have it any other way. Simulating a shell >with hooks on its I/O should be so complicated that a "script kiddie" >has trouble writing a Trojan. I'm not sure I understand the security hole there - if a script kiddie manages to make you execute his program, he doesn't need complex I/O redirections to harm you, simply destroying files randomly on your hard drive should fit his needs shouldn't it. :? > > I met the issue : select() works only on windows ... > >No it doesn't. It works only on sockets on Windows, on Unix/Linux it works >with all file descriptors . Indeed, I mixed up my words there... >_< >If you are willing to have a wxPython dependency, wx.Execute handles non-blocking i/o with processes on all supported platforms more discussion of python's blocking i/o issues here: > http://groups.google.com/group/comp.lang.python/browse_thread/thread/a037349e18f99630/60886b8beb55cfbc?q=#60886b8beb55cfbc Very interesting link, that's exactly the problem... and it seems no obvious solution comes out of it, except, of course, going down to the OS' internals... File support is really weak in python I feel (stdio is a little outdated...), it'd need a complete blocking/non-blocking, locking/non-locking stream API straight in the stdlib... I'm already working on locking classes, I'll explore the opportunity for a higher level "file" object too whenever possible (a new filesystem api is in progress here at europython2009, it might be the occasion). regards, Pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Thu Jul 2 13:37:01 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 02 Jul 2009 17:37:01 GMT Subject: PEP 376 In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> Message-ID: <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Joachim Str?mbergson wrote: > Aloha! > > Tarek Ziad? wrote: >> The prefix is a good idea but since it's just a checksum to control >> that the file hasn't changed >> what's wrong with using a weak hash algorithm like md5 or now sha1 ? > > Because it creates a dependency to an old algorithm that should be > deprecated. Also using MD5, even for a thing like this might make people > belive that it is an ok algorithm to use - "Hey, it is used by the > default install in Python, so it must be ok, right?" > > If we flip the argument around: Why would you want to use MD5 instead of > SHA-256? For the specific use case the performance will not (should not) > be an issue. > > As I wrote a few mails ago, it is time to move forward from MD5 and > designing something in 2009 that will be around for many years that uses > MD5 is (IMHO) a bad design decision. > >> If someone wants to modify a file of a distribution he can recreate >> the checksum as well, >> the only secured way to prevent that would be to use gpg keys but >> isn't that overkill for what we need ? > > Actually, adding this type of security would IMHO be a good idea. > Now, are we actually talking about security or checksum? It has been known for years that MD5 is weak, weak, weak. Not just in the recent years. But it doesn't matter since MD5 was never designed for security, MD5 was designed to protect against random bits corruption. If you want security, look at least to GPG. For data protection against intentional, malicious forging, definitely MD5 is the wrong choice. But when you just want a simple checksum to ensure that a faulty router somewhere in the internet backbone doesn't destroying your data, MD5 is a fine algorithm. From duncan.booth at invalid.invalid Thu Jul 2 13:44:34 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 2 Jul 2009 17:44:34 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) > I'd write the first one as: if self.lower is None: return self.higher if self.higher is None: return self.lower because the entire first 'if' statement is redundant. As a matter of style however I wouldn't use the shorthand to run two 'is' comparisons together, I'd write that out in full if it was actually needed here. Likewise in the second one: if self.lower is None: return return self.lower is obviously the same as: return self.lower so apart from reversing the order of the comparisons once you've dropped the redundant test it is the same as the first one. From petr.messner at gmail.com Thu Jul 2 13:48:28 2009 From: petr.messner at gmail.com (Petr Messner) Date: Thu, 2 Jul 2009 19:48:28 +0200 Subject: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor In-Reply-To: <4A4CE541.9030308@tim.thechases.com> References: <3f1a902d0907020832v99c0d82o860df02cf47be4fc@mail.gmail.com> <4A4CD6C2.2030103@tim.thechases.com> <3f1a902d0907020857m6c8790a9la329758bf1265dfc@mail.gmail.com> <4A4CE541.9030308@tim.thechases.com> Message-ID: <67c97cd90907021048u650ae97dhb6da2f34fabd971b@mail.gmail.com> 2009/7/2 Tim Chase : >> Will this order at least be the same for that same query every time the >> script is executed? > > I wouldn't count on it. The order is only defined for the one iteration > (result of the keys() call). If the order matters, I'd suggest a > double-dispatch with a non-dict (regular/default) query result, something ... Dictionaries are usually designed to have the best performance when accessing certain key, so it is not expected that dictionary items will be in any particular order; this applies not only for Python. But (for Python dict) you can be sure that results of items(), keys(), values(), iteritems(), iterkeys(), and itervalues() will correspond if called with no intervening modifications to the dictionary. If such a functionality is needed, there is a collections.OrderedDict that was introduced in Python 3.1; there is also an implementation for Python 2.4 or later: http://code.activestate.com/recipes/576693/ (this links is from "What?s New In Python 3.1") PM From cbc at unc.edu Thu Jul 2 13:52:48 2009 From: cbc at unc.edu (Chris Calloway) Date: Thu, 02 Jul 2009 13:52:48 -0400 Subject: Deadline for Toronto PyCamp Registraiton Appoaching Message-ID: <4A4CF3F0.4020304@unc.edu> Tomorrow (July 3) by midnight will be the last opportunity for Toronto PyCamp registration before the late registration period ending July 10. PyCamp is the original Python BootCamp developed by a user group for user groups. This year PyCamp is July 13-17 at the University of Toronto, sponsored by the Department of Physics and Scryent. For beginners, PyCamp makes you productive so you can get your work done quickly. PyCamp emphasizes the features which make Python a simpler and more efficient language. Following along by example speeds your learning process in a modern high-tech classroom. Become a self-sufficient Python developer in just five days at PyCamp! http://pycamp.org -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From cbc at unc.edu Thu Jul 2 13:52:48 2009 From: cbc at unc.edu (Chris Calloway) Date: Thu, 02 Jul 2009 13:52:48 -0400 Subject: Deadline for Toronto PyCamp Registraiton Appoaching Message-ID: <4A4CF3F0.4020304@unc.edu> Tomorrow (July 3) by midnight will be the last opportunity for Toronto PyCamp registration before the late registration period ending July 10. PyCamp is the original Python BootCamp developed by a user group for user groups. This year PyCamp is July 13-17 at the University of Toronto, sponsored by the Department of Physics and Scryent. For beginners, PyCamp makes you productive so you can get your work done quickly. PyCamp emphasizes the features which make Python a simpler and more efficient language. Following along by example speeds your learning process in a modern high-tech classroom. Become a self-sufficient Python developer in just five days at PyCamp! http://pycamp.org -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/ . From hobesh at gmail.com Thu Jul 2 13:56:10 2009 From: hobesh at gmail.com (Zach Hobesh) Date: Thu, 2 Jul 2009 10:56:10 -0700 Subject: Config files with different types Message-ID: Hi all, I've written a function that reads a specifically formatted text file and spits out a dictionary. ?Here's an example: config.txt: Destination = C:/Destination Overwrite = True Here's my function that takes 1 argument (text file) the_file = open(textfile,'r') linelist = the_file.read().split('\n') the_file.close() configs = {} for line in linelist: try: key,value = line.split('=') key.strip() value.strip() key.lower() value.lower() configs[key] = value except ValueError: break so I call this on my config file, and then I can refer back to any config in my script like this: shutil.move(your_file,configs['destination']) which I like because it's very clear and readable. So this works great for simple text config files. Here's how I want to improve it: I want to be able to look at the value and determine what type it SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when it might be more useful as a boolean. Is there a quick way to do this? I'd also like to able to read '1' as an in, '1.0' as a float, etc... I remember once I saw a script that took a string and tried int(), float() wrapped in a try except, but I was wondering if there was a more direct way. Thanks in advance, Zach From db3l.net at gmail.com Thu Jul 2 13:58:24 2009 From: db3l.net at gmail.com (David Bolen) Date: Thu, 02 Jul 2009 13:58:24 -0400 Subject: Suppressing Implicit Chained Exceptions (Python 3.0) References: Message-ID: "andrew cooke" writes: > However, when printed via format_exc(), this new exception still has the > old exception attached via the mechanism described at > http://www.python.org/dev/peps/pep-3134/ (this is Python 3.0). If you're in control of the format_exc() call, I think the new chain keyword parameter can disable this and restore the old behavior. If you're not in control of the traceback display, I'm not sure there's an easy way to prevent it, given that displaying chained exceptions is the default mode. -- David From sajmikins at gmail.com Thu Jul 2 13:58:56 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 10:58:56 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On Jul 2, 1:44?pm, Duncan Booth wrote: > Simon Forman wrote: > > Hey I was hoping to get your opinions on a sort of minor stylistic > > point. > > These two snippets of code are functionally identical. Which would you > > use and why? > > The first one is easier [for me anyway] to read and understand, but > > slightly less efficient, while the second is [marginally] harder to > > follow but more efficient. > > > ## First snippet > > > if self.higher is self.lower is None: return > > if self.lower is None: return self.higher > > if self.higher is None: return self.lower > > > ## Second snippet > > > if self.higher is None: > > ? ? if self.lower is None: > > ? ? ? ? return > > ? ? return self.lower > > if self.lower is None: > > ? ? return self.higher > > > What do you think? > > > (One minor point: in the first snippet, the "is None" in the first > > line is superfluous in the context in which it will be used, the only > > time "self.lower is self.higher" will be true is when they are both > > None.) > > I'd write the first one as: > > ? ? ? ? if self.lower is None: > ? ? ? ? ? ? ? ? return self.higher > ? ? ? ? if self.higher is None: > ? ? ? ? ? ? return self.lower > > because the entire first 'if' statement is redundant. > > As a matter of style however I wouldn't use the shorthand to run two 'is' > comparisons together, I'd write that out in full if it was actually needed > here. > > Likewise in the second one: > > ? ? if self.lower is None: > ? ? ? ? return > ? ? return self.lower > > is obviously the same as: > > ? ? ? ? return self.lower > > so apart from reversing the order of the comparisons once you've dropped > the redundant test it is the same as the first one. Wow. The equivalence in the second bit is obvious enough-- in hindsight :] but I totally missed the redundancy in the first bit. I must be getting old. Thank you very much. From charles at declareSub.com Thu Jul 2 14:00:51 2009 From: charles at declareSub.com (Charles Yeomans) Date: Thu, 2 Jul 2009 14:00:51 -0400 Subject: PEP 376 In-Reply-To: <1f63m.2229$ze1.1410@news-server.bigpond.net.au> References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Message-ID: On Jul 2, 2009, at 1:37 PM, Lie Ryan wrote: > Joachim Str?mbergson wrote: >> Aloha! >> >> Tarek Ziad? wrote: >>> The prefix is a good idea but since it's just a checksum to control >>> that the file hasn't changed >>> what's wrong with using a weak hash algorithm like md5 or now sha1 ? >> >> Because it creates a dependency to an old algorithm that should be >> deprecated. Also using MD5, even for a thing like this might make >> people >> belive that it is an ok algorithm to use - "Hey, it is used by the >> default install in Python, so it must be ok, right?" >> >> If we flip the argument around: Why would you want to use MD5 >> instead of >> SHA-256? For the specific use case the performance will not (should >> not) >> be an issue. >> >> As I wrote a few mails ago, it is time to move forward from MD5 and >> designing something in 2009 that will be around for many years that >> uses >> MD5 is (IMHO) a bad design decision. >> >>> If someone wants to modify a file of a distribution he can recreate >>> the checksum as well, >>> the only secured way to prevent that would be to use gpg keys but >>> isn't that overkill for what we need ? >> >> Actually, adding this type of security would IMHO be a good idea. >> > > Now, are we actually talking about security or checksum? > > It has been known for years that MD5 is weak, weak, weak. Not just in > the recent years. But it doesn't matter since MD5 was never designed > for > security, MD5 was designed to protect against random bits > corruption. If > you want security, look at least to GPG. For data protection against > intentional, malicious forging, definitely MD5 is the wrong choice. > But > when you just want a simple checksum to ensure that a faulty router > somewhere in the internet backbone doesn't destroying your data, MD5 > is > a fine algorithm. > -- On the contrary, MD5 was intended to be a cryptographic hash function, not a checksum. Charles Yeomans From python at mrabarnett.plus.com Thu Jul 2 14:12:07 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 02 Jul 2009 19:12:07 +0100 Subject: Config files with different types In-Reply-To: References: Message-ID: <4A4CF877.6030906@mrabarnett.plus.com> Zach Hobesh wrote: > Hi all, > > I've written a function that reads a specifically formatted text file > and spits out a dictionary. Here's an example: > > config.txt: > > Destination = C:/Destination > Overwrite = True > > > Here's my function that takes 1 argument (text file) > > the_file = open(textfile,'r') > linelist = the_file.read().split('\n') You could use .splitlines() or iterate over the file itself. > the_file.close() > configs = {} > for line in linelist: > try: > key,value = line.split('=') > key.strip() > value.strip() > key.lower() > value.lower() Strings are immutable. These methods don't modify the strings, but _return_ the result. > configs[key] = value > > except ValueError: > break > 'break' will leave the loop. Is this intentional? > so I call this on my config file, and then I can refer back to any > config in my script like this: > > shutil.move(your_file,configs['destination']) > > which I like because it's very clear and readable. > > So this works great for simple text config files. Here's how I want > to improve it: > > I want to be able to look at the value and determine what type it > SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when > it might be more useful as a boolean. Is there a quick way to do > this? I'd also like to able to read '1' as an in, '1.0' as a float, > etc... > > I remember once I saw a script that took a string and tried int(), > float() wrapped in a try except, but I was wondering if there was a > more direct way. > The way you saw was the safe, and recommended, way. When checking for Boolean you might want to ignore the case; something like: bool_dict = {"false": False, "true": True} ... try: value = bool_dict[value.strip().lower()] except ValueError: # Not a Boolean. ... From kee at kagi.com Thu Jul 2 14:12:29 2009 From: kee at kagi.com (Kee Nethery) Date: Thu, 2 Jul 2009 11:12:29 -0700 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: the fact that you felt compelled to explain the "one minor point" in the first snippet tells me that the second snippet does not need that explanation and will be easier for someone (like you for example) to maintain in the future. Second snippet would be my choice. Kee Nethery On Jul 2, 2009, at 10:23 AM, Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) > -- > http://mail.python.org/mailman/listinfo/python-list ------------------------------------------------- I check email roughly 2 to 3 times per business day. Kagi main office: +1 (510) 550-1336 From pavlovevidence at gmail.com Thu Jul 2 14:36:38 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 2 Jul 2009 11:36:38 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> On Jul 2, 3:12?am, Ulrich Eckhardt wrote: > Bearophile wrote: > > Ulrich Eckhardt: > >> a way to automatically release the resource, something > >> which I would do in the destructor in C++. > > > Is this helpful? > >http://effbot.org/pyref/with.htm > > Yes, it aims in the same direction. However, I'm not sure this applies to my > case. The point is that the resource handle is not just used locally in a > restricted scope but it is allocated and stored. The 'with' is something > that makes sense in the context of mutex locking, where you have a > well-defined critical section. What I need is something similar to open(), > which returs a file. When the last reference to that object goes out of > scope, the underlying file object is closed. On CPython you can do it with a __del__ attribute. Warning: objects with a __del__ attribute prevent reference cycle detection, which can potentially lead to memory (and resource) leaks. So you must be careful to avoid creating reference loops with that object. Note that file objects have a close method; you can explicitly close it at any time. Your object should follow that example, and define a close (or release, or whatever) method. I'd recommend making an effort to call it and to rely on __del__ as little as possible. Carl Banks From nobody at nowhere.com Thu Jul 2 14:41:54 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 02 Jul 2009 19:41:54 +0100 Subject: regex question on .findall and \b References: Message-ID: On Thu, 02 Jul 2009 09:38:56 -0700, Ethan Furman wrote: > Greetings! > > My closest to successfull attempt: > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] > Type "copyright", "credits" or "license" for more information. > > IPython 0.9.1 -- An enhanced Interactive Python. > > In [161]: re.findall('\d+','this is test a3 attempt 79') > Out[161]: ['3', '79'] > > What I really want in just the 79, as a3 is not a decimal number, but > when I add the \b word boundaries I get: > > In [162]: re.findall('\b\d+\b','this is test a3 attempt 79') > Out[162]: [] > > What am I missing? You need to use a raw string (r'...') to prevent \b from being interpreted as a backspace: re.findall(r'\b\d+\b','this is test a3 attempt 79') \d isn't a recognised escape sequence, so it doesn't get interpreted: > print '\b'  ^H > print '\d' \d > print r'\b' \b Try to get into the habit of using raw strings for regexps. From tjreedy at udel.edu Thu Jul 2 14:53:43 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 02 Jul 2009 14:53:43 -0400 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher What happen is neither is None? From james at agentultra.com Thu Jul 2 14:54:49 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 02 Jul 2009 14:54:49 -0400 Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> <851vozf4rh.fsf@agentultra.com> Message-ID: <85ws6qewzq.fsf@agentultra.com> masher writes: > On Jul 2, 12:06?pm, J Kenneth King wrote: >> masher writes: >> > My questions, then, is: Is there a more elegant/pythonic way of doing >> > what I am trying to do with the current Pool class? >> >> Forgive me, I may not fully understand what you are trying to do here >> (I've never really used multiprocessing all that much)... >> >> But couldn't you just assign your own Queue object to the Pool instance? > > That's basically my question. It does not appear as though there is > any straightforward way of doing this because of the design of Pool's > __init__ method, which passes _taskqueue to several functions. Hence, > even if I were to reassign _taskqueue after __init__, that wouldn't > change anything. I think I understand. There are ways to modify the class before instantiating it, but even the most clever or elegant solution will still smell funny. I suppose this might be worth submitting as a feature suggestion to the multiprocessing project. Best of luck. From rdrehoff at technotraining.net Thu Jul 2 14:57:40 2009 From: rdrehoff at technotraining.net (Rich Drehoff) Date: Thu, 2 Jul 2009 14:57:40 -0400 Subject: Intro to Python class, 7/21-23, Ft Worth TX Message-ID: <000901c9fb46$faa21ae0$efe650a0$@net> We are looking for someone that can help with the subject class, Intro to Python class, 7/21-23, Ft Worth TX. Please let me know if you can help. Would need your resume and best possible daily rate. Best regards, Rich Drehoff TechnoTraining, Inc. 328 Office Square Lane, Ste. 202, Virginia Beach, VA 23462-3648 (757) 425-0728 x15 / (757) 425-7323 Fax Email: RDrehoff at TechnoTraining.net Website: www.TechnoTraining.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew at acooke.org Thu Jul 2 15:07:20 2009 From: andrew at acooke.org (andrew cooke) Date: Thu, 2 Jul 2009 15:07:20 -0400 (CLT) Subject: Suppressing Implicit Chained Exceptions (Python 3.0) Message-ID: <7915bc3d5213848b805f461940e3cbec.squirrel@localhost> David Bolen wrote: > "andrew cooke" writes: > >> However, when printed via format_exc(), this new exception still has the old exception attached via the mechanism described at >> http://www.python.org/dev/peps/pep-3134/ (this is Python 3.0). > > If you're in control of the format_exc() call, I think the new chain keyword parameter can disable this and restore the old behavior. > > If you're not in control of the traceback display, I'm not sure there's an easy way to prevent it, given that displaying chained exceptions is the default mode. Yeah, that's the impression I got too. Just in case it wasn't clear, I'd like the chaining not to exist at all in this case), so that it doesn't appear in format_exc outside my control. Thanks, Andrew From thudfoo at opensuse.us Thu Jul 2 15:10:41 2009 From: thudfoo at opensuse.us (member thudfoo) Date: Thu, 2 Jul 2009 12:10:41 -0700 Subject: Open Source RSS Reader in Python? In-Reply-To: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> References: <50675e1c-91e4-4774-83bd-9e9e8fc1679a@z34g2000vbl.googlegroups.com> Message-ID: <3d881a310907021210haabc998x9e4f678299716462@mail.gmail.com> On Wed, Jul 1, 2009 at 4:18 PM, Alex wrote: > I am looking for an open source RSS reader (desktop, not online) > written in Python but in vain. I am not looking for a package but a > fully functional software. > > Google: python "open source" (rss OR feeds) reader > > Any clue ? > -- > http://mail.python.org/mailman/listinfo/python-list > rawdog is an RSS Aggregator Without Delusions Of Grandeur. It is a "river of news"-style aggregator: it uses feedparser to download feeds in RSS, Atom and a variety of other formats, and (by default) produces static HTML pages containing the newest articles in date order. For example, rawdog's default configuration produces output that looks like this: http://offog.org/code/rawdog.html From http Thu Jul 2 15:13:48 2009 From: http (Paul Rubin) Date: 02 Jul 2009 12:13:48 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <7x63eahp8z.fsf@ruckus.brouhaha.com> Simon Forman writes: > These two snippets of code are functionally identical. Which would you > use and why? Both are terrible. I can't tell what you're really trying to do. As Terry Reedy points out, the case where self.higher and self.lower are both not None is not handled. If you want to explicitly return None, use "return None" rather than "return". Generally, having a special value like None to denote a missing datum was considered standard practice a few decades ago, but these days in some circles it's treated as something of a code smell. You may want to look for some other representation instead. Most importantly, whatever code you write, put in an explanatory comment saying what the real goal is. From usernet at ilthio.net Thu Jul 2 15:16:25 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 19:16:25 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On 2009-07-02, Duncan Booth wrote: > so apart from reversing the order of the comparisons once you've dropped > the redundant test it is the same as the first one. I try to evaluate what you have given regardless of what Booth pointed out. So, I will only evaluate the first line as it contains most of what I want to illistrate. > Simon Forman wrote: >> if self.higher is self.lower is None: return I *really* don't like the fact that you don't specify a return value here when you have specified a return value for the other conditions. Obviously it is possible that something will be looking at the return value for this function (ie, it would not be a void function() in C). I had to lookup to make sure what Python would return in such a case. If you want the condition to return something then you should specify what you are returning. Specifing 'return None' is much clearer then relying on default behavior. I always prefer to see a: condition: operation structure to my code. It is the way it is almost always see in Python and in other languages. Putting it on one line doesn't save you anything and it makes it easier to miss something. I have no particular problem with running the is statements together as an extended if statement together when it makes it clearer to do so. The fact that you want all of them to be the same object is clear. I do have some wroblem in this instance because you are referencing "is" against an instance of a builtin object. The fact that you used is instead of == for this particular instance throws me a little because of the use of None. I wouldn't think second about it if it was a user defined object. I have to wonder whether all objects that are assigned the value of None are actually re-referenced to the origional None object? Could deep copy style operations create two equivilant but different copys of none? The same problems may be encounted from any builtin objects such as number literals. Had None not been specified I would be fine with stringing the comparison together: # if all three objects are the same instance, return None if object1 is object2 is object3: return None: Because of this pecularity, I think that it would be clearer to break the ifs for simularity and equality: if object1 is object2: if object1 == None: return None else: return object1 else: if object1 == None: return Object2 elif object2 == None:: return Object1 else: # what do we do if neither object == None? raise houstonWeHaveAProblemException From Scott.Daniels at Acm.Org Thu Jul 2 15:57:57 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 02 Jul 2009 12:57:57 -0700 Subject: question of style In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Duncan Booth wrote: > Simon Forman wrote: >> ... >> if self.higher is self.lower is None: return >> ... > As a matter of style however I wouldn't use the shorthand to run two 'is' > comparisons together, I'd write that out in full if it was actually needed > here. Speaking only to the style issue, when I've wanted to do something like that, I find: if self.higher is None is self.lower: ... more readable, by making clear they are both being compared to a constant, rather than compared to each other. More often, I've used code like: if is not None is not : ... since I am usually working on non-defaulting cases in the body. I find the form above simpler to read than: if is not None and is not None: ... I do draw the line at two, though, and with three or more I'll paren-up a list of parallel comparisons: if ( is not None and is not None and is not None): ... --Scott David Daniels Scott.Daniels at Acm.Org From http Thu Jul 2 16:02:58 2009 From: http (Paul Rubin) Date: 02 Jul 2009 13:02:58 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <7xws6qrgy5.fsf@ruckus.brouhaha.com> Simon Forman writes: > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? I'm not sure, but my guess is that what you are really trying to write is something like this: # self.higher and self.lower are each either "available" (i.e. are # not None), or unavailable (are None). Return the highest of the # available values. If no value is available, return None. if self.higher is not None: return self.higher elif self.lower is not None: return self.lower else: return None From sanket.s.patel at gmail.com Thu Jul 2 16:07:54 2009 From: sanket.s.patel at gmail.com (sanket) Date: Thu, 2 Jul 2009 13:07:54 -0700 (PDT) Subject: how to spawn a process under different user Message-ID: Hello All, I am trying to use python's subprocess module to launch a process. but in order to do that I have to change the user. I am not getting any clue how to do that? so can anyone please tell me How can I spawn a process under different user than currently I am logging in as. Thank you, sanket From max at alcyone.com Thu Jul 2 16:08:57 2009 From: max at alcyone.com (Erik Max Francis) Date: Thu, 02 Jul 2009 13:08:57 -0700 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? Explicit is always better, `return None` when that is your intent. `return` shouts to the reader, "I want to get out of this function now, and no one cares about the return result." Personally, I only use the one-liner if/else clauses if it's an extremely trivial condition, and even then, usually not there. (The only place I use it consistently is `if __name__ == '__main__': main()`.) If it's part of something more important that needs inspection -- as this obviously does given the questions you've had about it, doesn't skip space. Newlines are free. Even when expanding about the first test, there's no reason to do chained `if`s. One `if` with an `and` works just fine. Paul Rubin's looked to be the best overall replacement, as the logic here looks strangely turned inside out. You're obviously looking for which one _isn't_ `None`, so write the tests that way. It's much easier for everyone else (including your potential future self) to follow. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis They are ill discoverers that think there is no land when they can see nothing but sea. -- Francis Bacon From usernet at ilthio.net Thu Jul 2 16:27:33 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 20:27:33 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On 2009-07-02, Scott David Daniels wrote: > Duncan Booth wrote: >> Simon Forman wrote: >> As a matter of style however I wouldn't use the shorthand to run two 'is' >> comparisons together, I'd write that out in full if it was actually needed >> here. That part I don't really have a problem with. The chained is statements can actually be much clearer burying the tests into nested ifs. Nested ifs get the same result but they also give an immediate impression that the control structure might be looking for more then a single test result. I have to walk through the ifs to see all of the potential results one is looking for before I know what the entire control struture is doing. Multiple is operators makes it clear that I just want everything to be the same. The same would not be true if the operators where not all equilance operators ( is and == are clear as long as they are not mixed; but <, >=, != would be more confusing). > Speaking only to the style issue, when I've wanted to do something like > that, I find: > if self.higher is None is self.lower: > more readable, by making clear they are both being compared to a > constant, rather than compared to each other. By comparing them to *any* constant with 'is' you end up with the same potential problems as to whether something with a value of None is actually None? Is something with the value of 3 actually the same object as 3? What about if I make a deep copy? Can I have several objects with the value of three, all the same 3 object, or maybe even a combination of 1 separate 3 object with two other objects of the same 3. Is if self.higher == None == self.lower: any more clear then: if self.higher == self.lower == None: I don't personally have a problem with either. > More often, I've used code like: > if is not None is not : > since I am usually working on non-defaulting cases in the body. > I find the form above simpler to read than: > if is not None and is not None: This is a much more compicated expression. With equality operators everything is either the same or it isn't. I can start from the first variable and compare it to the second then to the third then ... In the case of the inequality, I have to start with the first and compare it to all of the rest to make sure that none are the same. Then I have to do the same for each with the remaining. This is much more difficult to do in my head. I requires much more thought, better left to a computer, when evaluating the expression for myself. Therefore, equalites are quick and easy to keep straight whereas any inequalites are more error prone when trying to evaluate how a section of code will react under different circumstances. > I do draw the line at two, though, and with three or more I'll > paren-up a list of parallel comparisons: As long as everything is an equality, then I don't mind comparing to the end of an 70 column line. Mixed types of equalities or inequalities require too many operations mentally evaluate safely. From bpytlik at sun.com Thu Jul 2 16:31:35 2009 From: bpytlik at sun.com (Brock Pytlik) Date: Thu, 02 Jul 2009 13:31:35 -0700 Subject: System default sys.path In-Reply-To: <81a8195ca5ecf44f3bf9ddbfaae37bdc@preisshare.net> References: <4A4C1FE4.1070901@sun.com> <81a8195ca5ecf44f3bf9ddbfaae37bdc@preisshare.net> Message-ID: <4A4D1927.5070305@sun.com> David Lyon wrote: > On Wed, 01 Jul 2009 19:48:04 -0700, Brock Pytlik wrote: > >> Hi, I'm trying to find a way to get the value sys.path would have on a >> particular system if python was started with an empty python path. I do >> want it to include the site specific additional paths. I know I can hack >> this information myself, >> > > Copy the code out from site.py... > > Well, as far as I can tell, site does several things. It calls abs__file__ which, afaict, doesn't effect sys.path. Then it calls removeduppaths, which walks over sys.path. That means that sys.path has already been set by something at that point. So, how/where did sys.path get set then? Or is sys.path always empty there at startup and this code is just to handle other situations? [snip] Brock From usernet at ilthio.net Thu Jul 2 16:37:13 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 20:37:13 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> Message-ID: On 2009-07-02, Paul Rubin wrote: [reformated with emphasis added] > #self.higher and self.lower are each either "available" (i.e. are not > #None), or unavailable (are None). [EMPHASIS] Return the highest of the > #available values. [/EMPHASIS] If no value is available, return None. Your code doesn't do what your comment says (notice EMPHASIS tags): > if self.higher is not None: > return self.higher > elif self.lower is not None: > return self.lower > else: > return None If lower is 5 and higher is 3, then it returns 3 because 3 != None in the first if. From dfstrauss at gmail.com Thu Jul 2 16:49:00 2009 From: dfstrauss at gmail.com (Dawie Strauss) Date: Thu, 2 Jul 2009 13:49:00 -0700 (PDT) Subject: PyTextMagicSMS text messaging library released Message-ID: <2b583f41-78d7-4a7e-a925-04934efe5458@m7g2000prd.googlegroups.com> TextMagic (http://www.textmagic.com) offers a convenient way to send text messages programmatically. This package provides a simple Python API on top of the TextMagic HTTPS API. Project documentation and source code is hosted at http://code.google.com/p/textmagic-sms-api-python/ The package can be dowloaded from http://pypi.python.org/pypi/PyTextMagicSMS/ From user at example.net Thu Jul 2 16:57:49 2009 From: user at example.net (superpollo) Date: Thu, 02 Jul 2009 22:57:49 +0200 Subject: stringio+tarfile Message-ID: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> why the following does not work? can you help me correct (if possible)? 1 import tarfile 2 import StringIO 3 sf1 = StringIO.StringIO("one\n") 4 sf2 = StringIO.StringIO("two\n") 5 tf = StringIO.StringIO() 6 tar = tarfile.open(tf , "w") 7 for name in [sf1 , sf2]: 8 tar.add(name) 9 print tf 10 sf1.close() 11 sf2.close() 12 tf.close() 13 tar.close() tia From mail at timgolden.me.uk Thu Jul 2 16:58:03 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 02 Jul 2009 21:58:03 +0100 Subject: how to spawn a process under different user In-Reply-To: References: Message-ID: <4A4D1F5B.6070605@timgolden.me.uk> sanket wrote: > Hello All, > > I am trying to use python's subprocess module to launch a process. > but in order to do that I have to change the user. > > I am not getting any clue how to do that? > so can anyone please tell me How can I spawn a process under different > user than currently I am logging in as. What platform are you on? If you are on Windows, there was a thread on this subject some time in the last month. (Short answer: switching user before launching subprocess won't work; switching within the subprocess will...) TJG From rschroev_nospam_ml at fastmail.fm Thu Jul 2 17:28:19 2009 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Thu, 02 Jul 2009 23:28:19 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: Peter Otten schreef: > Ulrich Eckhardt wrote: > >> Bearophile wrote: >>> Ulrich Eckhardt: >>>> a way to automatically release the resource, something >>>> which I would do in the destructor in C++. >>> Is this helpful? >>> http://effbot.org/pyref/with.htm >> Yes, it aims in the same direction. However, I'm not sure this applies to >> my case. The point is that the resource handle is not just used locally in >> a restricted scope but it is allocated and stored. The 'with' is something >> that makes sense in the context of mutex locking, where you have a >> well-defined critical section. > > Isn't that exactly what RAII does? RAII also works if the resource handle is stored, for example, in a data member of an object. If that object is destroyed (because it goes out of scope, or because it is deleted), the resource is automatically destroyed too. The way RAII works is actually the one thing from C++ that I miss in Python. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From http Thu Jul 2 17:43:38 2009 From: http (Paul Rubin) Date: 02 Jul 2009 14:43:38 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> Message-ID: <7xocs2222d.fsf@ruckus.brouhaha.com> Tim Harig writes: > If lower is 5 and higher is 3, then it returns 3 because 3 != None in the > first if. Sorry, the presumption was that lower <= higher, i.e. the comparison had already been made and the invariant was enforced by the class constructor. The comment should have been more explicit, I guess. From sanket.s.patel at gmail.com Thu Jul 2 17:56:30 2009 From: sanket.s.patel at gmail.com (sanket) Date: Thu, 2 Jul 2009 14:56:30 -0700 (PDT) Subject: how to spawn a process under different user References: Message-ID: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> On Jul 2, 1:58?pm, Tim Golden wrote: > sanket wrote: > > Hello All, > > > I am trying to use python's subprocess module to launch a process. > > but in order to do that I have to change the user. > > > I am not getting any clue how to do that? > > so can anyone please tell me How can I spawn a process under different > > user than currently I am logging in as. > > What platform are you on? If you are on Windows, > there was a thread on this subject some time in > the last month. (Short answer: switching user before > launching subprocess won't work; switching within > the subprocess will...) > > TJG Hi TJG, Thanks for the reply. I am using python 2.4 on centos. Thanks, sanket From hannarosie at gmail.com Thu Jul 2 18:05:46 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Thu, 2 Jul 2009 15:05:46 -0700 Subject: String to List Question Message-ID: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> Hi, I am brand new to python and I love it, but I've been having some trouble with a file parser that I've been working on. It contains lines that start with a name and then continue with names, nicknames and phone numbers of people associated with that name. I need to create a list of the names of people associated with each singular person (the first name in each line). Each name/phone number is separated by a tab but if someone doesn't have a nickname there are two tabs between their name and number. I've been trying to figure out how to test for two tabs, skip over these people and move onto the next name but I just can't figure out how that will work in python. Any help would be greatly appreciated! Thanks, Hanna -------------- next part -------------- An HTML attachment was scrubbed... URL: From allen.fowler at yahoo.com Thu Jul 2 18:06:17 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Thu, 2 Jul 2009 15:06:17 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? Message-ID: <115250.26948.qm@web45607.mail.sp1.yahoo.com> I have an (in-development) python system that needs to shuttle events / requests around over the network to other parts of itself. It will also need to cooperate with a .net application running on yet a different machine. So, naturally I figured some sort of HTTP event / RPC type of would be a good idea? Are there any modules I should know about, or guidelines I could read, that could aid me in the design of the API? Thank you, :) From usernet at ilthio.net Thu Jul 2 18:14:18 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:14:18 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> Message-ID: <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> On 2009-07-02, Paul Rubin wrote: > Tim Harig writes: >> If lower is 5 and higher is 3, then it returns 3 because 3 != None in the >> first if. > Sorry, the presumption was that lower <= higher, i.e. the comparison > had already been made and the invariant was enforced by the class > constructor. The comment should have been more explicit, I guess. That being the case, it might be a good idea either to handle the situation and raise an exception or add: assert self.lower <= self.higher That way an exception will be raised if there is an error somewhere else in the code rather then silently passing a possibly incorrect value. From philip at semanchuk.com Thu Jul 2 18:15:03 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 2 Jul 2009 18:15:03 -0400 Subject: String to List Question In-Reply-To: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> References: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> Message-ID: <76DF2C44-9A40-4FC9-AA20-CEDC3ED37806@semanchuk.com> On Jul 2, 2009, at 6:05 PM, Hanna Michelsen wrote: > Hi, > > I am brand new to python and I love it, but I've been having some > trouble > with a file parser that I've been working on. It contains lines that > start > with a name and then continue with names, nicknames and phone > numbers of > people associated with that name. I need to create a list of the > names of > people associated with each singular person (the first name in each > line). > Each name/phone number is separated by a tab but if someone doesn't > have a > nickname there are two tabs between their name and number. > > I've been trying to figure out how to test for two tabs, skip over > these > people and move onto the next name but I just can't figure out how > that will > work in python. > > Any help would be greatly appreciated! Hi Hanna, Are you familiar with a string's split() function? It sounds like just what you need. http://docs.python.org/library/stdtypes.html#str.split HTH Philip From allen.fowler at yahoo.com Thu Jul 2 18:17:56 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Thu, 2 Jul 2009 15:17:56 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? Message-ID: <219972.64445.qm@web45607.mail.sp1.yahoo.com> > I have an (in-development) python system that needs to shuttle events / requests > around over the network to other parts of itself. It will also need to > cooperate with a .net application running on yet a different machine. > > So, naturally I figured some sort of HTTP event / RPC type of would be a good > idea? > > Are there any modules I should know about, or guidelines I could read, that > could aid me in the design of the API? > > To clarify: Each message would be <1KB of data total, and consist of some structured object containing strings, numbers, dates, etc. For instance there would be an "add user" request that would contain one or more User objects each having a number of properties like: - Full Name - Username - Password - Email addresses (a variable length array) - Street Address line1 - Street Address line1 - Street Address line1 - City - State - Zip - Sign Up Date .... and so on. Since I need to work with other platforms, pickle is out... what are the alternatives? XML? JSON? How should I formally define each of the valid messages and objects? Thank you, :) From mccdo at iastate.edu Thu Jul 2 18:22:07 2009 From: mccdo at iastate.edu (Doug McCorkle) Date: Thu, 2 Jul 2009 17:22:07 -0500 Subject: Detecting platform architecture with Parallels and Win XP x64 Message-ID: <8B80C75F-1F27-4E65-8BAE-C77441235943@iastate.edu> Hello, I am using Python 2.5.4 on Windows XP x64 with Parallels. When I try to use: distutils.util.get_platform sys.platform I always get win32 but if I use: platform.architecture() with the amd64 installer I get 64bit for the first argument. Is there a way to detect win64 without having to use the python executable to see if the application is on a 64bit architecture? Thanks. Doug From usernet at ilthio.net Thu Jul 2 18:27:05 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:27:05 GMT Subject: how to spawn a process under different user References: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> Message-ID: On 2009-07-02, sanket wrote: >> sanket wrote: >> > I am trying to use python's subprocess module to launch a process. >> > but in order to do that I have to change the user. > I am using python 2.4 on centos. I have never done this in python; but, using the normal system calls in C the process is basically: 1. fork() a new process 2. the child process changes its user id with setreuid() and possibly its group id with setregid() 3. then the child exec()s new process which replaces itself All of the necessary functions should be under the os module on POSIX operating systems. From clp2 at rebertia.com Thu Jul 2 18:27:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 2 Jul 2009 15:27:23 -0700 Subject: XML(JSON?)-over-HTTP: How to define API? In-Reply-To: <115250.26948.qm@web45607.mail.sp1.yahoo.com> References: <115250.26948.qm@web45607.mail.sp1.yahoo.com> Message-ID: <50697b2c0907021527j6cd6cfb6o734c989b71da77b1@mail.gmail.com> On Thu, Jul 2, 2009 at 3:06 PM, Allen Fowler wrote: > > I have an (in-development) python system that needs to shuttle events / requests around over the network to other parts of itself. ? It will also need to cooperate with a .net application running on yet a different machine. > > So, naturally I figured some sort of HTTP event / RPC type of would be a good idea? > > Are there any modules I should know about, or guidelines I could read, that could aid me in the design of the API? http://en.wikipedia.org/wiki/Representational_State_Transfer (aka REST) O'Reilly also has a book on the subject. Cheers, Chris -- http://blog.rebertia.com From http Thu Jul 2 18:29:49 2009 From: http (Paul Rubin) Date: 02 Jul 2009 15:29:49 -0700 Subject: XML(JSON?)-over-HTTP: How to define API? References: Message-ID: <7xprcipvky.fsf@ruckus.brouhaha.com> Allen Fowler writes: > Since I need to work with other platforms, pickle is out... what > are the alternatives? XML? JSON? json is the easiest to prototype with and is less bureaucratic. xml has more serious tools for schema specification and validation etc. You could start with json and switch later. As for the rpc mechanism, using urllib and one of the HTTP server modules is probably simplest. From charles at declareSub.com Thu Jul 2 18:30:43 2009 From: charles at declareSub.com (Charles Yeomans) Date: Thu, 2 Jul 2009 18:30:43 -0400 Subject: XML(JSON?)-over-HTTP: How to define API? In-Reply-To: <115250.26948.qm@web45607.mail.sp1.yahoo.com> References: <115250.26948.qm@web45607.mail.sp1.yahoo.com> Message-ID: <35DA358F-DFDC-425D-BD68-AFEF2024F537@declareSub.com> On Jul 2, 2009, at 6:06 PM, Allen Fowler wrote: > > I have an (in-development) python system that needs to shuttle > events / requests around over the network to other parts of > itself. It will also need to cooperate with a .net application > running on yet a different machine. > > So, naturally I figured some sort of HTTP event / RPC type of would > be a good idea? > > Are there any modules I should know about, or guidelines I could > read, that could aid me in the design of the API? > > > Thank you, > :) I'd suggest the O'Reilly book "RESTful Web Services". Charles Yeomans From http Thu Jul 2 18:32:27 2009 From: http (Paul Rubin) Date: 02 Jul 2009 15:32:27 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> Message-ID: <7xljn6pvgk.fsf@ruckus.brouhaha.com> Tim Harig writes: > That being the case, it might be a good idea either to handle the situation > and raise an exception or add: > > assert self.lower <= self.higher > > That way an exception will be raised if there is an error somewhere else > in the code rather then silently passing a possibly incorrect value. Well, that assert is not right because you have to handle the case where one of the values is None. The general sentiment is reasonable though, if you're concerned that the precondition may not be valid. From rhodri at wildebst.demon.co.uk Thu Jul 2 18:38:49 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 02 Jul 2009 23:38:49 +0100 Subject: String to List Question In-Reply-To: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> References: <564970db0907021505i74a6599dlc9582143343c0701@mail.gmail.com> Message-ID: On Thu, 02 Jul 2009 23:05:46 +0100, Hanna Michelsen wrote: > Hi, > > I am brand new to python and I love it, but I've been having some trouble > with a file parser that I've been working on. It contains lines that > start > with a name and then continue with names, nicknames and phone numbers of > people associated with that name. I need to create a list of the names of > people associated with each singular person (the first name in each > line). > Each name/phone number is separated by a tab but if someone doesn't have > a > nickname there are two tabs between their name and number. > > I've been trying to figure out how to test for two tabs, skip over these > people and move onto the next name but I just can't figure out how that > will > work in python. You might find the csv module in the standard library does a lot of the hard work for you: http://docs.python.org/library/csv.html You can define yourself a reader that splits the input on tabs, and then see how long the rows it returns are. Something like this (untested): import csv for row in csv.reader(open("phone_numbers.txt", "rb"), delimiter='\t'): if len(row) > 1: # Do your stuff -- Rhodri James *-* Wildebeest Herder to the Masses From Russ.Paielli at gmail.com Thu Jul 2 18:41:56 2009 From: Russ.Paielli at gmail.com (Russ P.) Date: Thu, 2 Jul 2009 15:41:56 -0700 (PDT) Subject: Is Psyco good for Python 2.6.1? Message-ID: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> I need to speed up some Python code, and I discovered Psyco. However, the Psyco web page has not been updated since December 2007. Before I go to the trouble of installing it, does anyone know if it is still good for Python 2.6.1? Thanks. From usernet at ilthio.net Thu Jul 2 18:49:52 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:49:52 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: On 2009-07-02, Paul Rubin wrote: > Tim Harig writes: >> That being the case, it might be a good idea either to handle the situation >> and raise an exception or add: >> assert self.lower <= self.higher >> That way an exception will be raised if there is an error somewhere else >> in the code rather then silently passing a possibly incorrect value. > Well, that assert is not right because you have to handle the case > where one of the values is None. The general sentiment is reasonable > though, if you're concerned that the precondition may not be valid. Sorry, it worked under 2.5: 2.5 >>> lower = None 2.5 >>> higher = 10 2.5 >>> assert lower <= higher 2.5 >>> assert higher < lower 2.5 Traceback (most recent call last): 2.5 File "", line 1, in 2.5 AssertionError 2.5 >>> higher = -10 2.5 >>> assert lower <= higher 2.5 >>> assert higher < lower 2.5 Traceback (most recent call last): 2.5 File "", line 1, in 2.5 AssertionError None seems to have been evaluated less then any integer. The same isn't true under 3.0: 3.0 >>> lower = None 3.0 >>> higher = 10 3.0 >>> assert lower <= higher 3.0 Traceback (most recent call last): 3.0 File "", line 1, in 3.0 TypeError: unorderable types: NoneType() <= int() Then it is probably easier to just handle it in the conditionals and raise an exception manually. From usernet at ilthio.net Thu Jul 2 18:56:49 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 02 Jul 2009 22:56:49 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: On 2009-07-02, Tim Harig wrote: > Sorry, it worked under 2.5: [SNIP] > None seems to have been evaluated less then any integer. The same isn't > true under 3.0: None seem to have been evaluated less then any non-negative integer including 0. From sajmikins at gmail.com Thu Jul 2 19:04:31 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 16:04:31 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: On Jul 2, 4:08?pm, Erik Max Francis wrote: > Simon Forman wrote: > > Hey I was hoping to get your opinions on a sort of minor stylistic > > point. > > These two snippets of code are functionally identical. Which would you > > use and why? > > The first one is easier [for me anyway] to read and understand, but > > slightly less efficient, while the second is [marginally] harder to > > follow but more efficient. > > > ## First snippet > > > if self.higher is self.lower is None: return > > if self.lower is None: return self.higher > > if self.higher is None: return self.lower > > > ## Second snippet > > > if self.higher is None: > > ? ? if self.lower is None: > > ? ? ? ? return > > ? ? return self.lower > > if self.lower is None: > > ? ? return self.higher > > > What do you think? > > Explicit is always better, `return None` when that is your intent. > `return` shouts to the reader, "I want to get out of this function now, > and no one cares about the return result." > > Personally, I only use the one-liner if/else clauses if it's an > extremely trivial condition, and even then, usually not there. ?(The > only place I use it consistently is `if __name__ == '__main__': > main()`.) ?If it's part of something more important that needs > inspection -- as this obviously does given the questions you've had > about it, doesn't skip space. ?Newlines are free. > > Even when expanding about the first test, there's no reason to do > chained `if`s. ?One `if` with an `and` works just fine. > > Paul Rubin's looked to be the best overall replacement, as the logic > here looks strangely turned inside out. ?You're obviously looking for > which one _isn't_ `None`, so write the tests that way. ?It's much easier > for everyone else (including your potential future self) to follow. > Thanks for all the great feedback! Some notes: This code is part of a delete() method for a binary tree implementation. "None" is used to simulate a NULL pointer. In the case where both children are non-None the code goes on to handle that. None is a "unitary" or "singleton" value, so "is" is the right comparison operator to use with it, at least in this "imitation pointer" usage. The bare return was used both to preclude executing the rest of the function and to return None. (I sometimes use "return # None" with None in a comment to note that the return value is used. Also, I will put "# return" or "# return None" at the end of a function if the returning-of-None is important to the implementation of whatever. I've never used dis.dis() to check the bytecodes, but I wouldn't be surprised to find that the compiler generated the same bytecode whether you explicitly state the return or comment it out.) In any event, as Duncan Booth pointed out, the entire line was redundant so the issue is somewhat moot in this particular case. As for one-line control flow statements in python, I usually refrain but this code wasn't originally meant for public viewing. Last but not least, the logic may seem backwards but it's actually correct. If you check for non-None (NULL) -ness and return the thing you checked, rather than checking for None-ness and returning the other, the case where both are non-None is not handled correctly. Thanks again, ~Simon FWIW, here's the BinaryTree class, the code is in the _delete_me() method. from random import random class BinaryTree: ''' A Binary Tree implementation. Provides: get(key) - Return item for key or None. insert(key, value) - Insert value under key, replacing old ones. delete(key) - Delete value under key if any. keys() - Iterate through the keys in sort order. values() - Iterate through the values in sort order. items() - Iterate through (key, value) pairs in sort order. ''' def __init__(self, key, value): self.key = key self.value = value self.higher = self.lower = None def get(self, key): ''' Return item for key or None. ''' if key == self.key: return self.value if key < self.key and self.lower is not None: return self.lower.get(key) if self.higher is not None: assert key > self.key return self.higher.get(key) def insert(self, key, value): ''' Insert value under key, replacing old ones. ''' if key == self.key: self.value = value elif key < self.key: if self.lower is None: self.lower = BinaryTree(key, value) else: self.lower.insert(key, value) else: assert key > self.key if self.higher is None: self.higher = BinaryTree(key, value) else: self.higher.insert(key, value) def delete(self, key): ''' Delete value under key if any. ''' if key < self.key and self.lower is not None: self.lower = self.lower.delete(key) elif key > self.key and self.higher is not None: self.higher = self.higher.delete(key) elif key == self.key: return self._delete_me() return self def _delete_me(self): if self.lower is None: return self.higher if self.higher is None: return self.lower # Two children... try: side = self._which_side except AttributeError: side = bool(int(random() * 2)) # Alternate sides, should help keep tree balanced. self._which_side = not side method = self._delete_lower if side else self._delete_higher return method() def _delete_lower(self): next = self.lower while next.higher is not None: next = next.higher self.key = next.key self.value = next.value self.lower = self.lower.delete(self.key) return self def _delete_higher(self): next = self.higher while next.lower is not None: next = next.lower self.key = next.key self.value = next.value self.higher = self.higher.delete(self.key) return self def keys(self): ''' Iterate through the keys in sort order. ''' for key, value in self.items(): yield key def values(self): ''' Iterate through the values in sort order. ''' for key, value in self.items(): yield value def items(self): ''' Iterate through (key, value) pairs in sort order. ''' if self.lower is not None: for kv in self.lower.items(): yield kv yield self.key, self.value if self.higher is not None: for kv in self.higher.items(): yield kv From barry at python.org Thu Jul 2 19:10:39 2009 From: barry at python.org (Barry Warsaw) Date: Thu, 2 Jul 2009 19:10:39 -0400 Subject: Python 3.0 (pinin' for the fjords) Message-ID: Now that Python 3.1 is out, it seems there's been some confusion as to whether there will be one last Python 3.0 release, i.e. Python 3.0.2. At the PyCon 2009 language summit it was decided that there will be *no* Python 3.0.2. Python 3.0 is different than all other releases. There will be no last maintenance release and no ongoing security releases. Python 3.1 is the replacement for Python 3.0, and it's release schedule was accelerated specifically so that production users would be able to switch immediately. If you're using Python 3, you are strongly urged to update to Python 3.1. Python 3.1 will be a 'normal' release in that we're making all the guarantees that we make for other releases. It is likely that Python 3.2 is 18-24 months away, there will be Python 3.1.x maintenance releases, and there will be one last Python 3.1.x release after the final release of Python 3.2. Cheers, -Barry -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 849 bytes Desc: This is a digitally signed message part URL: From sajmikins at gmail.com Thu Jul 2 19:13:47 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 16:13:47 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> On Jul 2, 3:57?pm, Scott David Daniels wrote: > Duncan Booth wrote: > > Simon Forman wrote: > >> ... > >> if self.higher is self.lower is None: return > >> ... > > As a matter of style however I wouldn't use the shorthand to run two 'is' > > comparisons together, I'd write that out in full if it was actually needed > > here. > > Speaking only to the style issue, when I've wanted to do something like > that, I find: > > ? ? ? ?if self.higher is None is self.lower: > ? ? ? ? ? ?... > > more readable, by making clear they are both being compared to a > constant, rather than compared to each other. I was going to do it that way for aesthetics if nothing else, but in this particular code "self.higher is self.lower" could only have been True if they were both None, so the final "is None" was redundant and I only included it as a "just-in-case" check in case someone someday used the code in such a way as to assign the same object (not None) to both self.higher and self.lower... Totally pointless, I'm sorry to say. I'm glad the whole line was redundant really. > More often, I've used code like: > > ? ? ? ?if is not None is not : > ? ? ? ? ? ?... > > since I am usually working on non-defaulting cases in the body. > I find the form above simpler to read than: > > ? ? ? ?if is not None and is not None: > ? ? ? ? ? ?... > > I do draw the line at two, though, and with three or more I'll > paren-up a list of parallel comparisons: > > ? ? ? ?if ( is not None > ? ? ? ? ?and is not None > ? ? ? ? ?and is not None): > ? ? ? ? ? ?... > > --Scott David Daniels > Scott.Dani... at Acm.Org From mensanator at aol.com Thu Jul 2 20:34:31 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 2 Jul 2009 17:34:31 -0700 (PDT) Subject: Is Psyco good for Python 2.6.1? References: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> Message-ID: On Jul 2, 5:41?pm, "Russ P." wrote: > I need to speed up some Python code, and I discovered Psyco. However, > the Psyco web page has not been updated since December 2007. Before I > go to the trouble of installing it, does anyone know if it is still > good for Python 2.6.1? Thanks. Go back to the web site and look at the download page. Usually, the download page will list various versions to use with various Python versions. If it lists a 2.4 version, a 2.5 version, but no 2.6 version, most likely the authors haven't yet made a 2.6 version so you're SOL. Even that isn't guaranteed to always work. For instance, the authors of the smpy module didn't think they needed a seperate 2.6 version (and didn't make any effort to actually try it 2.6). Turned out to be dead wrong on that and had to scramble to release a fix. From http Thu Jul 2 20:57:15 2009 From: http (Paul Rubin) Date: 02 Jul 2009 17:57:15 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: <7x8wj61t3o.fsf@ruckus.brouhaha.com> Tim Harig writes: > > Well, that assert is not right because you have to handle the case > > where one of the values is None... > Sorry, it worked under 2.5: Well, it didn't crash under 2.5. Whether the result was correct is a different question. > None seems to have been evaluated less then any integer. The same isn't > true under 3.0: But the original code didn't specify the non-None values to be integers. Really, it's unwise to rely on an ordering relation between None and values of arbitrary other types, unless supportable by a clear specification, but even then, it's a code smell. > 3.0 TypeError: unorderable types: NoneType() <= int() That is far preferable to what 2.x does. From rylesny at gmail.com Thu Jul 2 21:03:59 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 18:03:59 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> Message-ID: <5a6891ba-bb93-41ab-8ba5-acb23bde3ae9@a7g2000yqk.googlegroups.com> > You can go ahead and implement a __del__() method. It will often work in > CPython, but you get no guarantees, especially when you have reference > cycles and with other Python implementations that don't use refcounting. And for resources whose lifetime is greater than your process (e.g. a file), you can also use the atexit module to help ensure they are cleaned/reclaimed at shutdown. One way to do this is to maintain a weak dictionary (from the weakref module) to your objects and install a handler which iterates this. This is useful for not only dealing with reference cycles, but for objects which may exist at the time the interpreter exits. These may not be deleted. http://docs.python.org/reference/datamodel.html#object.__del__ From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:37 -0300 Subject: stringio+tarfile References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: En Thu, 02 Jul 2009 17:57:49 -0300, superpollo escribi?: > why the following does not work? can you help me correct (if possible)? Explain "does not work" please. Does it raise an exception? Which one? Please post the complete traceback. You don't get the expected result? Tell us what did you expect and what you actually get. If you provide this information, it's a lot easier for people to look at your problem and eventually find a solution. Fortunately my crystall ball is back from the repair shop and I can guess that you want to create a tar file in memory, and add some content from a memory buffer too, right? I didn't run your code but I can see two problems: > 5 tf = StringIO.StringIO() > 6 tar = tarfile.open(tf , "w") The first argument to tarfile.open is the *name* of the file to open/create. In this case you don't have a real file, but a StringIO object; use the fileobj argument instead: tarfile.open(fileobj=tf, mode="w") See http://docs.python.org/library/tarfile.html#tarfile.open > 7 for name in [sf1 , sf2]: > 8 tar.add(name) The add method takes the *name* of a file to add to the archive. Again, you don't have a real file to add; try the addfile method instead. See http://docs.python.org/library/tarfile.html#tarfile.TarFile.add -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:42 -0300 Subject: how to spawn a process under different user References: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> Message-ID: En Thu, 02 Jul 2009 19:27:05 -0300, Tim Harig escribi?: > On 2009-07-02, sanket wrote: >>> sanket wrote: >>> > I am trying to use python's subprocess module to launch a process. >>> > but in order to do that I have to change the user. >> I am using python 2.4 on centos. > > I have never done this in python; but, using the normal system calls in C > the process is basically: > > 1. fork() a new process > 2. the child process changes its user id with setreuid() and > possibly its group id with setregid() > 3. then the child exec()s new process which replaces itself > > All of the necessary functions should be under the os module on POSIX > operating systems. How to do that using the subprocess module: write a function for item (2) above and pass it as the preexec_fn argument to Popen. preexec_fn is executed after fork() and before exec() -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:45 -0300 Subject: dealloc function in python extend c module References: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> <8599A253-D2C9-4EE4-AF4F-AEF658BA2033@semanchuk.com> Message-ID: En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk escribi?: > On Jul 2, 2009, at 2:11 AM, Shen, Yu-Teh wrote: > >> I create my extend type something like >> http://www.python.org/doc/current/extending/newtypes.html. >> And my type has a member which is a pointer point to my allocate >> memory ( no ref count). >> ex: >> --------------------------------------------------- >> typedef struct { >> PyObject_HEAD >> /* Type-specific fields go here. */ >> mytype *p; >> } noddy_NoddyObject; >> >> And i write dealloc function like this: >> --------------------------------------------------- >> static void >> Noddy_dealloc(Noddy* self) >> { >> delete p; >> self->ob_type->tp_free((PyObject*)self); >> } >> >> And I found it's strange that it didn't call dealloc when there is no >> reference. And you set the tp_dealloc field in the type structure to Noddy_dealloc, did you? >> ex: >> a = Noddy() >> b = a >> del a >> del b >> # i think there is no ref to the object, and it should call dealloc >> but it didn't call!!! You can use sys.getrefcount(a) to check how many references it has. Remember that it returns one more than the value you expect (because the function itself holds a temporary reference to its argument) > Hi Shen, > I'm no expert on Python memory management, but since no once else has > answered your question I'll tell you what I *think* is happening. > > Python doesn't delete objects as soon as they're dereferenced. Nope. CPython *does* destroy objects as soon as their reference count reaches zero. It does not rely on garbage collection for that. > It merely marks them as safe for garbage collection (GC). If GC never > happens (and it might not in a small test program), your dealloc > function won't run. The garbage collector is only used to recover memory from object cycles (in the CPython implementation; Jython *does* use garbage collection for "normal" object destruction too) > Now some more knowledgeable person will probably correct me. =) And now some more knowledgeable person will probably correct me. =) -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 2 21:28:48 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 02 Jul 2009 22:28:48 -0300 Subject: A question about fill_free_list(void) function References: <169b5d83-696b-44d4-8816-0522844d3fe4@h8g2000yqm.googlegroups.com> <9829f925-0091-4bc3-8f12-de83f7aba8e6@j32g2000yqh.googlegroups.com> <7c8e96a1-cbb2-4036-9aa1-1ecdfd84cca8@j32g2000yqh.googlegroups.com> Message-ID: En Thu, 02 Jul 2009 07:55:42 -0300, Pedram escribi?: > On Jul 2, 1:11?pm, Peter Otten <__pete... at web.de> wrote: >> [snip explanation of strange pointer manipulations in the C code of the >> integer type] > Oh, I got it! What a wonderful implementation! :o Well, I would not say it's "wonderful"... The wonderful part is that all those nasty tricks are only there, hidden from us poor mortals, so we can focus on writing nice and elegant Python code instead :) -- Gabriel Genellina From http Thu Jul 2 21:30:07 2009 From: http (Paul Rubin) Date: 02 Jul 2009 18:30:07 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <7x4otu1rkw.fsf@ruckus.brouhaha.com> Simon Forman writes: > This code is part of a delete() method for a binary tree > implementation. "None" is used to simulate a NULL pointer. In the case > where both children are non-None the code goes on to handle that. > > None is a "unitary" or "singleton" value, so "is" is the right > comparison operator to use with it, at least in this "imitation > pointer" usage. Yes, but the concept of null pointers is considered kludgy these days. One alternative is to represent an empty node as an empty list [], and a node with value x as the 1-element list [x]. That incurs some storage overhead, but all the case by case checking in your comparison function goes away: return (self.higher + self.lower)[:1] > I've never used dis.dis() to check the bytecodes, but I wouldn't be > surprised to find that the compiler generated the same bytecode > whether you explicitly state the return or comment it out.) I really wouldn't worry about this. Python is so slow no matter what you do, that 1 more no-op bytecode won't matter. > Last but not least, the logic may seem backwards but it's actually > correct. If you check for non-None (NULL) -ness and return the thing > you checked, rather than checking for None-ness and returning the > other, the case where both are non-None is not handled correctly. The code you posted didn't return anything if both values were non-None. As for checking for None-ness vs. non-None-ness, sure, the two are logically equivalent, but I think checking for non-None expresses the algorithm more clearly. > FWIW, here's the BinaryTree class, ... > A Binary Tree implementation. Provides: > get(key) - Return item for key or None. I'm sorry but I find that class rather ugly. The code would be a lot smaller and have fewer corner cases with a different data representation. From rylesny at gmail.com Thu Jul 2 22:10:18 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 19:10:18 -0700 (PDT) Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> Message-ID: <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> On Jul 2, 6:10?am, "Gabriel Genellina" wrote: > En Wed, 01 Jul 2009 12:49:31 -0300, Scott David Daniels ? > escribi?: > > These loops work well with the two-argument version of iter, > > which is easy to forget, but quite useful to have in your bag > > of tricks: > > > ? ? ?def convert(in_queue, out_queue): > > ? ? ? ? ?for row in iter(in_queue.get, None): > > ? ? ? ? ? ? ?# ... convert row > > ? ? ? ? ? ? ?out_queue.put(converted_line) > > Yep, I always forget about that variant of iter() -- very handy! Yes, at first glance using iter() here seems quite elegant and clever. You might even pat yourself on the back, or treat yourself to an ice cream cone, as I once did. There is one subtle distinction, however. Please allow me to demonstrate. >>> import Queue >>> >>> queue = Queue.Queue() >>> >>> queue.put(1) >>> queue.put("la la la") >>> queue.put(None) >>> >>> list(iter(queue.get, None)) [1, 'la la la'] >>> >>> # Cool, it really works! I'm going to change all my old code to use this... new and *improved* ... >>> # And then one day your user inevitably does something like this. ... >>> class A(object): ... def __init__(self, value): ... self.value = value ... ... def __eq__(self, other): ... return self.value == other.value ... >>> queue.put(A(1)) >>> queue.put(None) >>> >>> # And then this happens inside your 'generic' code (which probably even passed your unit tests). ... >>> list(iter(queue.get, None)) Traceback (most recent call last): File "", line 1, in File "", line 5, in __eq__ AttributeError: 'NoneType' object has no attribute 'value' >>> >>> # Oh... yeah. I really *did* want 'is None' and not '== None' which iter() will do. Sorry guys! Please don't let this happen to you too ;) From http Thu Jul 2 22:20:03 2009 From: http (Paul Rubin) Date: 02 Jul 2009 19:20:03 -0700 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> Message-ID: <7xfxdev770.fsf@ruckus.brouhaha.com> ryles writes: > >>> # Oh... yeah. I really *did* want 'is None' and not '== None' > >>> which iter() will do. Sorry guys! > > Please don't let this happen to you too ;) None is a perfectly good value to put onto a queue. I prefer using a unique sentinel to mark the end of the stream: sentinel = object() From rylesny at gmail.com Thu Jul 2 22:23:54 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 19:23:54 -0700 (PDT) Subject: What are the limitations of cStringIO.StringIO() ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: <5354b682-1f7e-4274-96e6-b51a3556e931@h11g2000yqb.googlegroups.com> This reminds me. Would anyone object to adding a sentence to http://docs.python.org/library/stringio.html#module-cStringIO just to mention that attributes cannot be added to a cStringIO, like such: % import cStringIO, StringIO % StringIO.StringIO().value = 1 % cStringIO.StringIO().value = 1 Traceback (most recent call last): File "", line 1, in AttributeError: 'cStringIO.StringO' object has no attribute 'value' From bearophileHUGS at lycos.com Thu Jul 2 22:34:50 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 2 Jul 2009 19:34:50 -0700 (PDT) Subject: Is Psyco good for Python 2.6.1? References: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> Message-ID: Russ P.: Python works well to me on Python 2.6+, on Windows. You can find a compiled version too, around. Bye, bearophile From davea at ieee.org Thu Jul 2 22:35:59 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 02 Jul 2009 22:35:59 -0400 Subject: stringio+tarfile In-Reply-To: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4A4D6E8F.1000506@ieee.org> superpollo wrote: >
why the > following does not work? can you help me correct (if possible)? > > 1 import tarfile > 2 import StringIO > 3 sf1 = StringIO.StringIO("one\n") > 4 sf2 = StringIO.StringIO("two\n") > 5 tf = StringIO.StringIO() > 6 tar = tarfile.open(tf , "w") > 7 for name in [sf1 , sf2]: > 8 tar.add(name) > 9 print tf > 10 sf1.close() > 11 sf2.close() > 12 tf.close() > 13 tar.close() > > tia > >
> Couldn't you have given a clue as to what doesn't work? What version of Python are you using, and on what platform? Do you get an error message, or are the results unreasonable? Details please. First problem I see is all those numbers before the lines. That's not valid python. Assuming that was a transcription error, we get to some other problems. tarfile.open() first argument is a filename, and you're passing it a Stringio object. Not the same at all. Fortunately, the Stringio should work as the third parameter, so you can change this line to tarfile.open(None, "w", tf) Similarly tar.add() takes a filename as a parameter, and you're trying to pass another Stringio. That's not a possible argument to the add() method at all. Try tar.addfile() I stopped at this point. Maybe somebody else can help, after you fill in a few details. From sajmikins at gmail.com Thu Jul 2 22:52:10 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 22:52:10 -0400 Subject: What are the limitations of cStringIO.StringIO() ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> Message-ID: <50f98a4c0907021952y50bea866heb69ed4cff52d590@mail.gmail.com> On Wed, Jul 1, 2009 at 7:48 AM, Barak, Ron wrote: > Hi, > > I think I'm up against a limitation in cStringIO.StringIO(), but could not > find any clues on the web, especially not in > http://docs.python.org/library/stringio.html. > > What I have is the following function: > > ??? def concatenate_files(self,filename_array): > ??????? import types > > ??????? f = cStringIO.StringIO() > ??????? for filename in filename_array: > ??????????? log_stream = LogStream(filename) > ??????????? if not isinstance(log_stream, types.NoneType): > ??????????????? try: > ??????????????????? string_ = log_stream.input_file.read() > ??????????????? except AttributeError, e: > ??????????????????????? sys.stderr.write("AttributeError: "+str(e)+"\n") > ??????????????????????? sys.stderr.write("log_stream: > "+str(log_stream)+"\n") > ??????????? else: > ??????????????? return(None) > > ??????????? f.write(string_) > ??????? return (f) > > And, when the list of files?- in filename_array - is pointing to long/large > enough files, I get the exception: > > Traceback (most recent call last): > ? File "svm_ts_tool_in_progress.py", line 244, in OnSelectCell > ??? self.InspectorViewController(row,col) > ? File "svm_ts_tool_in_progress.py", line 1216, in InspectorViewController > ??? self.InspectorePaneGetRecords(self.req_table, rec) > ? File "svm_ts_tool_in_progress.py", line 1315, in InspectorePaneGetRecords > ??? log_stream = > ConcatenatedLogStream(self.events_dict[req_table]["db_dict"].keys()) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 31, in > __init__ > ??? self.input_file = self.concatenate_files(filename_array) > ? File "c:\Documents and > Settings\rbarak\rbarak_devel\dpm16\ConcatenatedLogStream.py", line 47, in > concatenate_files > ??? string_ = log_stream.input_file.read() > MemoryError > > Anyone knows whet's the limitation on cStringIO.StringIO() objects ? > Could you suggest a better way to create a string that contains the > concatenation of several big files ? > > Thanks, > Ron. > > -- > http://mail.python.org/mailman/listinfo/python-list > > MemoryError means you're out of memory. Try using the operating system commands for file concatenation, i.e. "cat" on *nix, dunno how on windows. Or just do whatever it is you're doing in a different way that doesn't require having all the data in memory at once. Also, this: not isinstance(log_stream, types.NoneType) should just be this: log_stream is not None and return is not a function, leave out the ()'s around the expression you're returning, and if you're returning None, you can leave out the None as it is the default return value anyway. Generally speaking don't do module imports in functions. From benjamin at python.org Thu Jul 2 22:54:31 2009 From: benjamin at python.org (Benjamin Peterson) Date: Fri, 3 Jul 2009 02:54:31 +0000 (UTC) Subject: What are the limitations of cStringIO.StringIO() ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A2600@enbmail01.lsi.com> <5354b682-1f7e-4274-96e6-b51a3556e931@h11g2000yqb.googlegroups.com> Message-ID: ryles gmail.com> writes: > > This reminds me. Would anyone object to adding a sentence to > http://docs.python.org/library/stringio.html#module-cStringIO just to > mention that attributes cannot be added to a cStringIO, like such: That's true of almost all types that are implemented in C. From rylesny at gmail.com Thu Jul 2 22:55:19 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 19:55:19 -0700 (PDT) Subject: Adding an object to the global namespace through " f_globals" is that allowed ? References: <4A4BC22D.70502@gmail.com> Message-ID: <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> On Jul 2, 1:25 am, Terry Reedy wrote: > > The next statement works, > > but I'm not sure if it will have any dramatical side effects, > > other than overruling a possible object with the name A > > > def some_function ( ...) : > > A = object ( ...) > > sys._getframe(1).f_globals [ Name ] = A > > global name > name = A > > or is name is a string var > globals()[name] = A It wasn't explicit, but I think Stef meant that the global should be added to the caller's environment, which was the reason for sys._getframe(). Is this environment only intended for interactive use? I wonder if you might just set things up in a PYTHONSTARTUP script instead. From schickb at gmail.com Thu Jul 2 22:56:50 2009 From: schickb at gmail.com (schickb) Date: Thu, 2 Jul 2009 19:56:50 -0700 (PDT) Subject: Sequence splitting Message-ID: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> I have fairly often found the need to split a sequence into two groups based on a function result. Much like the existing filter function, but returning a tuple of true, false sequences. In Python, something like: def split(seq, func=None): if func is None: func = bool t, f = [], [] for item in seq: if func(item): t.append(item) else: f.append(item) return (t, f) The discussion linked to below has various approaches for doing this now, but most traverse the sequence twice and many don't apply a function to spit the sequence. http://stackoverflow.com/questions/949098/python-split-a-list-based-on-a-condition Is there any interest in a C implementation of this? Seems too trivial to write a PEP, so I'm just trying to measure interest before diving in. This wouldn't really belong in intertool. Would it be best implemented as a top level built-in? -Brad From alan.isaac at gmail.com Thu Jul 2 22:59:51 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 02:59:51 GMT Subject: Python 3.0 (pinin' for the fjords) In-Reply-To: References: Message-ID: Using the 3.1 Windows installer, I chose that I did not want the extensions registered, and the installer unregistered the .py and .pyw extensions (which I had wanted to keep associated with Python 2.6). Is this intentional? Alan Isaac PS I already fixed the problem. My question is about intent. From rylesny at gmail.com Thu Jul 2 23:09:25 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 20:09:25 -0700 (PDT) Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> Message-ID: On Jul 2, 10:20?pm, Paul Rubin wrote: > ryles writes: > > >>> # Oh... yeah. I really *did* want 'is None' and not '== None' > > >>> which iter() will do. Sorry guys! > > > Please don't let this happen to you too ;) > > None is a perfectly good value to put onto a queue. ?I prefer > using a unique sentinel to mark the end of the stream: > > ? ?sentinel = object() I agree, this is cleaner than None. We're still in the same boat, though, regarding iter(). Either it's 'item == None' or 'item == object ()', and depending on the type, __eq__ can introduce some avoidable risk. FWIW, even object() has its disadvantages. Namely, it doesn't work for multiprocessing.Queue which pickles and unpickles, thus giving you a new object. One way to deal with this is to define a "Stopper" class and type check objects taken from the queue. This is not news to anyone who's worked with multiprocessing.Queue, though. From tn.pablo at gmail.com Thu Jul 2 23:10:14 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 2 Jul 2009 22:10:14 -0500 Subject: Sequence splitting In-Reply-To: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: On Thu, Jul 2, 2009 at 21:56, schickb wrote: > I have fairly often found the need to split a sequence into two groups > based on a function result. Much like the existing filter function, > but returning a tuple of true, false sequences. In Python, something > like: > > def split(seq, func=None): > ? ?if func is None: > ? ? ? ?func = bool > ? ?t, f = [], [] > ? ?for item in seq: > ? ? ? ?if func(item): > ? ? ? ? ? ?t.append(item) > ? ? ? ?else: > ? ? ? ? ? ?f.append(item) > ? ?return (t, f) > > The discussion linked to below has various approaches for doing this > now, but most traverse the sequence twice and many don't apply a > function to spit the sequence. > http://stackoverflow.com/questions/949098/python-split-a-list-based-on-a-condition > > Is there any interest in a C implementation of this? Seems too trivial > to write a PEP, so I'm just trying to measure interest before diving > in. This wouldn't really belong in intertool. Would it be best > implemented as a top level built-in? > > -Brad > -- > http://mail.python.org/mailman/listinfo/python-list > This sounds like it belongs to the python-ideas list. I suggest posting there for better feedback, since the core developers check that list more often than this one. -- Pablo Torres N. From http Thu Jul 2 23:14:24 2009 From: http (Paul Rubin) Date: 02 Jul 2009 20:14:24 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: <7xzlbm5ugf.fsf@ruckus.brouhaha.com> schickb writes: > def split(seq, func=None): > if func is None: > func = bool > t, f = [], [] > for item in seq: > if func(item): > t.append(item) > else: > f.append(item) > return (t, f) untested: def split(seq, func=bool): xs = zip(seq, itertools.imap(func, seq)) t = list(x for (x,y) in xs if y) f = list(x for (x,y) in xs if not y) return (t, f) From http Thu Jul 2 23:15:40 2009 From: http (Paul Rubin) Date: 02 Jul 2009 20:15:40 -0700 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> Message-ID: <7xvdma5ueb.fsf@ruckus.brouhaha.com> ryles writes: > > ? ?sentinel = object() > > I agree, this is cleaner than None. We're still in the same boat, > though, regarding iter(). Either it's 'item == None' or 'item == object ()' Use "item is sentinel". From tn.pablo at gmail.com Thu Jul 2 23:17:32 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 2 Jul 2009 20:17:32 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> On Jul 2, 9:56?pm, schickb wrote: > I have fairly often found the need to split a sequence into two groups > based on a function result. Much like the existing filter function, > but returning a tuple of true, false sequences. In Python, something > like: > > def split(seq, func=None): > ? ? if func is None: > ? ? ? ? func = bool > ? ? t, f = [], [] > ? ? for item in seq: > ? ? ? ? if func(item): > ? ? ? ? ? ? t.append(item) > ? ? ? ? else: > ? ? ? ? ? ? f.append(item) > ? ? return (t, f) > > The discussion linked to below has various approaches for doing this > now, but most traverse the sequence twice and many don't apply a > function to spit the sequence.http://stackoverflow.com/questions/949098/python-split-a-list-based-o... > > Is there any interest in a C implementation of this? Seems too trivial > to write a PEP, so I'm just trying to measure interest before diving > in. This wouldn't really belong in intertool. Would it be best > implemented as a top level built-in? > > -Brad This sounds like it belongs to the python-ideas list. I suggest posting there for better feedback, since the core developers check that list more often than this one. From philip at semanchuk.com Thu Jul 2 23:17:35 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 2 Jul 2009 23:17:35 -0400 Subject: dealloc function in python extend c module In-Reply-To: References: <291af939-28c0-441f-ab4e-7b3cf5867a83@d32g2000yqh.googlegroups.com> <8599A253-D2C9-4EE4-AF4F-AEF658BA2033@semanchuk.com> Message-ID: On Jul 2, 2009, at 9:28 PM, Gabriel Genellina wrote: > En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk > escribi?: >> >> Hi Shen, >> I'm no expert on Python memory management, but since no once else >> has answered your question I'll tell you what I *think* is happening. >> >> Python doesn't delete objects as soon as they're dereferenced. > > Nope. CPython *does* destroy objects as soon as their reference count > reaches zero. It does not rely on garbage collection for that. > >> It merely marks them as safe for garbage collection (GC). If GC >> never happens (and it might not in a small test program), your >> dealloc function won't run. > > The garbage collector is only used to recover memory from object > cycles > (in the CPython implementation; Jython *does* use garbage collection > for > "normal" object destruction too) > >> Now some more knowledgeable person will probably correct me. =) Wow, that last part was the only part I got right. =) Thanks for straightening me out, Gabriel. From gagsl-py2 at yahoo.com.ar Thu Jul 2 23:47:54 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Jul 2009 00:47:54 -0300 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> <7xvdma5ueb.fsf@ruckus.brouhaha.com> Message-ID: En Fri, 03 Jul 2009 00:15:40 -0300, > escribi?: > ryles writes: >> > ? ?sentinel = object() >> >> I agree, this is cleaner than None. We're still in the same boat, >> though, regarding iter(). Either it's 'item == None' or 'item == object >> ()' > > Use "item is sentinel". We're talking about the iter() builtin behavior, and that uses == internally. It could have used an identity test, and that would be better for this specific case. But then iter(somefile.read, '') wouldn't work. A compromise solution is required; since one can customize the equality test but not an identity test, the former has a small advantage. (I don't know if this was the actual reason, or even if this really was a concious decision, but that's why *I* would choose == to test against the sentinel value). -- Gabriel Genellina From sajmikins at gmail.com Thu Jul 2 23:54:18 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 20:54:18 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> Message-ID: On Jul 2, 9:30?pm, Paul Rubin wrote: > Simon Forman writes: > > This code is part of a delete() method for a binary tree > > implementation. "None" is used to simulate a NULL pointer. In the case > > where both children are non-None the code goes on to handle that. > > > None is a "unitary" or "singleton" value, so "is" is the right > > comparison operator to use with it, at least in this "imitation > > pointer" usage. > > Yes, but the concept of null pointers is considered kludgy these days. Seriously? "kludgy"? (I really AM getting old.) Well I wouldn't know, I've been fortunate enough to program mostly in python for over half a decade now and None and 0 are as close as I've gotten to NULL in a long time. > One alternative is to represent an empty node as an empty list [], and > a node with value x as the 1-element list [x]. ?That incurs some > storage overhead, but all the case by case checking in your comparison > function goes away: > > ? ?return (self.higher + self.lower)[:1] Heh, program LISP much? :] That sounds very interesting, but I'm only writing a BTree to then use it to play with "persistent data structures" and the models I'm using are based on pointer code. I wouldn't want to try to re-implement them on top of a different "architecture", at least not at first. > > I've never used dis.dis() to check the bytecodes, but I wouldn't be > > surprised to find that the compiler generated the same bytecode > > whether you explicitly state the return or comment it out.) > > I really wouldn't worry about this. ?Python is so slow no matter what > you do, that 1 more no-op bytecode won't matter. I'm not worried about it at all. In fact, I'm pretty sure the compiler handles it for me as well as I could want or expect it to. "A Computer's Perspective on Moore's Law: Humans are getting more expensive at an exponential rate." - Mark Miller http://www.caplet.com/adages.html Python may be slow to execute but it's fast to write. (And write well.) ;] > > Last but not least, the logic may seem backwards but it's actually > > correct. If you check for non-None (NULL) -ness and return the thing > > you checked, rather than checking for None-ness and returning the > > other, the case where both are non-None is not handled correctly. > > The code you posted didn't return anything if both values were non-None. Yep, it was just a snippet, just the part I wanted feedback on. > As for checking for None-ness vs. non-None-ness, sure, the two are > logically equivalent, but I think checking for non-None expresses the > algorithm more clearly. In this particular case it's somewhat more elegant (IMO) to check "is None". > > FWIW, here's the BinaryTree class, ... > > ? ? A Binary Tree implementation. Provides: > > ? ? ? ? get(key) - Return item for key or None. > > I'm sorry but I find that class rather ugly. ?The code would be a lot > smaller and have fewer corner cases with a different data > representation. Um, thanks? Seriously though, care to put your money where your mouth is? I'd be interested to see a BTree implemented as you indicated above. Pointer code in python is silly in general, since you're only imitating real pointers with attributes (i.e. instance dicts) anyway. As I said above, I'm faking it with python to explore persistent data structures and the models I've found so far are pointer-based. I'm sure there are better (i.e. faster) ways to get the same (or sufficiently similar) behavior in a more "pythonic" fashion. (for example, to maintain a sorted sequence under insertion I'd use a list and the bisect module, and so on.) Reminds me, a few years back I implemented Knuth's "Dancing Links" algorithm in python for a Sudoku solver. It uses a kind of grid of doubly-linked lists to store information about the search space and to traverse that space to resolve an answer. The algorithm takes advantage of the fact that allocated nodes persisted in memory even when completely unlinked from the "surrounding" lists. You unlinked and re-linked the nodes/lists in a certain pattern to search and backtrack the search space. It's more than I can explain here. Really elegant algorithm. The point is in python you had to put ALL the nodes into a storage structure just to prevent them disappearing while unlinked from the "head" of the grid. Regards, ~Simon From schickb at gmail.com Thu Jul 2 23:55:12 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 20:55:12 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> Message-ID: <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> On Jul 2, 8:14?pm, Paul Rubin wrote: > schickb writes: > > def split(seq, func=None): > > ? ? if func is None: > > ? ? ? ? func = bool > > ? ? t, f = [], [] > > ? ? for item in seq: > > ? ? ? ? if func(item): > > ? ? ? ? ? ? t.append(item) > > ? ? ? ? else: > > ? ? ? ? ? ? f.append(item) > > ? ? return (t, f) > > untested: > > ? ?def split(seq, func=bool): > ? ? ? xs = zip(seq, itertools.imap(func, seq)) > ? ? ? t = list(x for (x,y) in xs if y) > ? ? ? f = list(x for (x,y) in xs if not y) > ? ? ? return (t, f) In my testing that is 3.5x slower than the original solution (and less clear imo). I fixed my version to take a bool default. Either way, I'm not really looking for additional ways to do this in Python unless I've totally missed something. What I am considering is writing it in C, much like filter. -Brad From http Thu Jul 2 23:55:12 2009 From: http (Paul Rubin) Date: 02 Jul 2009 20:55:12 -0700 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> <7xvdma5ueb.fsf@ruckus.brouhaha.com> Message-ID: <7xhbxu4dzz.fsf@ruckus.brouhaha.com> "Gabriel Genellina" writes: > We're talking about the iter() builtin behavior, and that uses == > internally. Oh, I see. Drat. > It could have used an identity test, and that would be better for this > specific case. But then iter(somefile.read, '') wouldn't work. Yeah, it should allow supplying a predicate instead of using == on a value. How about (untested): from itertools import * ... for row in takewhile(lambda x: x is sentinel, starmap(in_queue.get, repeat(()))): ... From schickb at gmail.com Thu Jul 2 23:56:03 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 20:56:03 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: On Jul 2, 8:17?pm, "Pablo Torres N." wrote: > On Jul 2, 9:56?pm, schickb wrote: > > > I have fairly often found the need to split a sequence into two groups > > based on a function result. > > This sounds like it belongs to the python-ideas list. ?I suggest > posting there for better feedback, since the core developers check > that list more often than this one. Thanks, I didn't know about that list. From rylesny at gmail.com Thu Jul 2 23:56:52 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 20:56:52 -0700 (PDT) Subject: multiprocessing: pool with blocking queue References: <80eaa77f-c78a-4549-8e95-29292b77b280@n21g2000vba.googlegroups.com> Message-ID: On Jul 2, 11:09?am, masher wrote: > My questions, then, is: Is there a more elegant/pythonic way of doing > what I am trying to do with the current Pool class? Another thing you might try is to subclass Pool and add an apply_async () wrapper which would wait for _taskqueue.qsize() to reach the desired size. You would probably do this wait in a loop with a small sleep. This approach would avoid needing a second Queue, but you would also add some delay to your producer due to the sleep (something you're not currently worried about). The minimum sleep may be something like 1 ms (it's really system dependent), but the time it takes for a thread blocked on a mutex to wake up is often more on the order of microseconds, which you have with your blocking queue. I doubt this offers you much satisfaction, though. > If the verdict is no, I'll be happy to file a bug report. Yeah, I think it's a worth a try. From http Fri Jul 3 00:08:02 2009 From: http (Paul Rubin) Date: 02 Jul 2009 21:08:02 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> Message-ID: <7xhbxu4del.fsf@ruckus.brouhaha.com> Brad writes: > On Jul 2, 8:14?pm, Paul Rubin wrote: > > schickb writes: > > > def split(seq, func=None): > > > ? ? if func is None: > > > ? ? ? ? func = bool > > > ? ? t, f = [], [] > > > ? ? for item in seq: > > > ? ? ? ? if func(item): > > > ? ? ? ? ? ? t.append(item) > > > ? ? ? ? else: > > > ? ? ? ? ? ? f.append(item) > > > ? ? return (t, f) > > > > untested: > > > > ? ?def split(seq, func=bool): > > ? ? ? xs = zip(seq, itertools.imap(func, seq)) > > ? ? ? t = list(x for (x,y) in xs if y) > > ? ? ? f = list(x for (x,y) in xs if not y) > > ? ? ? return (t, f) > > In my testing that is 3.5x slower than the original solution (and less > clear imo). I fixed my version to take a bool default. Either way, I'm > not really looking for additional ways to do this in Python unless > I've totally missed something. What I am considering is writing it in > C, much like filter. I'm a little skeptical that the C version will help much, if it's evaluating a python function at every list element. Here's a variant of your version: def split(seq, func=bool): ? ? t, f = [], [] ta, fa = t.append, f.append ? ? for item in seq: (ta if func(item) else fa)(item) ? ? return (t, f) This avoids some dict lookups and copying. I wonder if that helps significantly. From rylesny at gmail.com Fri Jul 3 00:13:19 2009 From: rylesny at gmail.com (ryles) Date: Thu, 2 Jul 2009 21:13:19 -0700 (PDT) Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <7xfxdev770.fsf@ruckus.brouhaha.com> <7xvdma5ueb.fsf@ruckus.brouhaha.com> <7xhbxu4dzz.fsf@ruckus.brouhaha.com> Message-ID: <34271171-ba63-43bb-b79c-83c036453007@c9g2000yqm.googlegroups.com> On Jul 2, 11:55?pm, Paul Rubin wrote: > Yeah, it should allow supplying a predicate instead of using == on > a value. ?How about (untested): > > ? ?from itertools import * > ? ?... > ? ?for row in takewhile(lambda x: x is sentinel, > ? ? ? ? ? ? ? ? ? ? ? ? ?starmap(in_queue.get, repeat(()))): > ? ? ? ... Yeah, it's a small recipe I'm sure a lot of others have written as well. My old version: def iterwhile(callable_, predicate): """ Like iter() but with a predicate instead of a sentinel. """ return itertools.takewhile(predicate, repeatfunc(callable_)) where repeatfunc is as defined here: http://docs.python.org/library/itertools.html#recipes I wish all of these little recipes made their way into itertools or a like module; itertools seems a bit tightly guarded. From schickb at gmail.com Fri Jul 3 00:34:21 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 21:34:21 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: On Jul 2, 9:08?pm, Paul Rubin wrote: > Brad writes: > > On Jul 2, 8:14?pm, Paul Rubin wrote: > > > schickb writes: > > > > def split(seq, func=None): > > > > ? ? if func is None: > > > > ? ? ? ? func = bool > > > > ? ? t, f = [], [] > > > > ? ? for item in seq: > > > > ? ? ? ? if func(item): > > > > ? ? ? ? ? ? t.append(item) > > > > ? ? ? ? else: > > > > ? ? ? ? ? ? f.append(item) > > > > ? ? return (t, f) > > > > untested: > > > > ? ?def split(seq, func=bool): > > > ? ? ? xs = zip(seq, itertools.imap(func, seq)) > > > ? ? ? t = list(x for (x,y) in xs if y) > > > ? ? ? f = list(x for (x,y) in xs if not y) > > > ? ? ? return (t, f) > > > In my testing that is 3.5x slower than the original solution (and less > > clear imo). I fixed my version to take a bool default. Either way, I'm > > not really looking for additional ways to do this in Python unless > > I've totally missed something. What I am considering is writing it in > > C, much like filter. > > I'm a little skeptical that the C version will help much, if it's > evaluating a python function at every list element. ? Perhaps true, but it would be a nice convenience (for me) as a built- in written in either Python or C. Although the default case of a bool function would surely be faster. > Here's a variant of your version: > > ?def split(seq, func=bool): > ?? ? t, f = [], [] > ? ? ?ta, fa = t.append, f.append > ?? ? for item in seq: > ? ? ? ? ?(ta if func(item) else fa)(item) > ?? ? return (t, f) > > This avoids some dict lookups and copying. ?I wonder if that helps > significantly. Faster, but in tests of a few short sequences only 1% so. -Brad From ldo at geek-central.gen.new_zealand Fri Jul 3 00:39:46 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 03 Jul 2009 16:39:46 +1200 Subject: Config files with different types References: Message-ID: In message , Zach Hobesh wrote: > I want to be able to look at the value and determine what type it > SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when > it might be more useful as a boolean. Typically the type should be what you expect, not what is given. For example, it doesn't make sense for your configs["overwrite"] setting to be "red", does it? A reasonable solution might be to have a table of expected types for each config item keyword, e.g. configs_types = \ { ... "overwrite" : lambda x : x[0] not in set("nNfF"), ... } Then you can just do something like configs[keyword] = configs_types[keyword](configs[keyword]) with appropriate trapping of ValueError exceptions. From tn.pablo at gmail.com Fri Jul 3 00:40:13 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 2 Jul 2009 23:40:13 -0500 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: On Thu, Jul 2, 2009 at 23:34, Brad wrote: > On Jul 2, 9:08?pm, Paul Rubin wrote: >> Brad writes: >> > On Jul 2, 8:14?pm, Paul Rubin wrote: >> > > schickb writes: >> > > > def split(seq, func=None): >> > > > ? ? if func is None: >> > > > ? ? ? ? func = bool >> > > > ? ? t, f = [], [] >> > > > ? ? for item in seq: >> > > > ? ? ? ? if func(item): >> > > > ? ? ? ? ? ? t.append(item) >> > > > ? ? ? ? else: >> > > > ? ? ? ? ? ? f.append(item) >> > > > ? ? return (t, f) >> >> > > untested: >> >> > > ? ?def split(seq, func=bool): >> > > ? ? ? xs = zip(seq, itertools.imap(func, seq)) >> > > ? ? ? t = list(x for (x,y) in xs if y) >> > > ? ? ? f = list(x for (x,y) in xs if not y) >> > > ? ? ? return (t, f) >> >> > In my testing that is 3.5x slower than the original solution (and less >> > clear imo). I fixed my version to take a bool default. Either way, I'm >> > not really looking for additional ways to do this in Python unless >> > I've totally missed something. What I am considering is writing it in >> > C, much like filter. >> >> I'm a little skeptical that the C version will help much, if it's >> evaluating a python function at every list element. > > Perhaps true, but it would be a nice convenience (for me) as a built- > in written in either Python or C. Although the default case of a bool > function would surely be faster. > >> Here's a variant of your version: >> >> ?def split(seq, func=bool): >> ?? ? t, f = [], [] >> ? ? ?ta, fa = t.append, f.append >> ?? ? for item in seq: >> ? ? ? ? ?(ta if func(item) else fa)(item) >> ?? ? return (t, f) >> >> This avoids some dict lookups and copying. ?I wonder if that helps >> significantly. > > Faster, but in tests of a few short sequences only 1% so. > > -Brad > -- > http://mail.python.org/mailman/listinfo/python-list > If it is speed that we are after, it's my understanding that map and filter are faster than iterating with the for statement (and also faster than list comprehensions). So here is a rewrite: def split(seq, func=bool): t = filter(func, seq) f = filter(lambda x: not func(x), seq) return list(t), list(f) The lambda thing is kinda ugly, but I can't think of anything else. Also, is it ok to return lists? Py3k saw a lot of APIs changed to return iterables instead of lists, so maybe my function should have 'return t, f' as it's last statement. -- Pablo Torres N. From sajmikins at gmail.com Fri Jul 3 00:40:42 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 2 Jul 2009 21:40:42 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> <9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> Message-ID: <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> On Jul 2, 4:31?am, Tep wrote: > On 2 Jul., 10:25, Tep wrote: > > > > > On 2 Jul., 01:56, MRAB wrote: > > > > someone wrote: > > > > Hello, > > > > > how can I replace '?' sign from string? Or do split at that character? > > > > Getting unicode error if I try to do it: > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > > > 1: ordinal not in range(128) > > > > > Thanks, Pet > > > > > script is # -*- coding: UTF-8 -*- > > > > It sounds like you're mixing bytestrings with Unicode strings. I can't > > > be any more helpful because you haven't shown the code. > > > Oh, I'm sorry. Here it is > > > def cleanInput(input) > > ? ? return input.replace('?', '') > > I also need: > > #input is html source code, I have problem with only this character > #input = 'foo ? bar' > #return should be foo > def splitInput(input) > ? ? parts = input.split(' ? ') > ? ? return parts[0] > > Thanks! Okay people want to help you but you must make it easy for us. Post again with a small piece of code that is runnable as-is and that causes the traceback you're talking about, AND post the complete traceback too, as-is. I just tried a bit of your code above in my interpreter here and it worked fine: |>>> data = 'foo ? bar' |>>> data.split('?') |['foo ', ' bar'] |>>> data = u'foo ? bar' |>>> data.split(u'?') |[u'foo ', u' bar'] Figure out the smallest piece of "html source code" that causes the problem and include that with your next post. HTH, ~Simon You might also read this: http://catb.org/esr/faqs/smart-questions.html From zookog at gmail.com Fri Jul 3 00:48:10 2009 From: zookog at gmail.com (zooko) Date: Thu, 2 Jul 2009 21:48:10 -0700 (PDT) Subject: No trees in the stdlib? References: <11eb0a6e-4e21-4a61-bf46-4221fd75dc8d@v15g2000prn.googlegroups.com> <006078f0$0$9721$c3e8da3@news.astraweb.com> Message-ID: Try PyJudy: http://www.dalkescientific.com/Python/PyJudy.html From http Fri Jul 3 00:56:40 2009 From: http (Paul Rubin) Date: 02 Jul 2009 21:56:40 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> Message-ID: <7x3a9egy9j.fsf@ruckus.brouhaha.com> Simon Forman writes: > > Yes, but the concept of null pointers is considered kludgy these days. > Seriously? "kludgy"? (I really AM getting old.) http://math.andrej.com/2009/04/11/on-programming-language-design/ discusses the issue (scroll down to "Undefined values" section). > Well I wouldn't know, I've been fortunate enough to program mostly in > python for over half a decade now and None and 0 are as close as I've > gotten to NULL in a long time. Right, and how many times have you had to debug AttributeError: 'NoneType' object has no attribute 'foo' or the equivalent null pointer exceptions in Java, C, or whatever? They are very common. And the basic idea is that if you avoid using null pointers in the first place, you'll get fewer accidental null pointer exceptions. > That sounds very interesting, but I'm only writing a BTree to then use > it to play with "persistent data structures" But you have implemented a mutable tree. If you want to write a persistent one, then write a persistent one! You also use a wishful heuristic in the hope of keeping the tree balanced--if you want a balanced tree, why not use one that's guaranteed to stay in balance? AVL trees are pretty easy to implement; maybe you could try writing a persistent one: http://en.wikipedia.org/wiki/AVL_tree > In this particular case it's somewhat more elegant (IMO) to check "is > None". Well, I can't understand why that might be, but it's surely subjective, so ok. > > I'm sorry but I find that class rather ugly. ?The code would be a lot > > smaller and have fewer corner cases with a different data representation. > > Um, thanks? Seriously though, care to put your money where your mouth > is? I'd be interested to see a BTree implemented as you indicated above. Er, I'm not trying to argue with you. You asked for people's advice and impressions, so I gave you some. I don't feel like rewriting that whole class, but I'll do a method or two, using [] to represent an empty node. (Hmm, actually changing the empty node representation did less to shorten the code than just getting rid of a bunch of duplication. Really, I'd tend to look for a slightly different tree structure which tried to avoid these issues). Untested: class BinaryTree: def __init__(self, key, value): self.key = key self.value = value self.higher = self.lower = [] def get(self, key): if key == self.key: return self.value branch = self.lower if key < self.key else self.higher return branch.get(key) if branch else [] def insert(self, key, value): def xinsert(xtree): # xtree is either a tree or [] if not xtree: xtree = BinaryTree(key, value) else: xtree.insert(key, value) return xtree if key == self.key: self.value = value elif key < self.key: self.lower = xinsert(self.lower) else: self.higher = xinsert(self.higher) and so forth. From http Fri Jul 3 00:58:22 2009 From: http (Paul Rubin) Date: 02 Jul 2009 21:58:22 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: <7xy6r6fjm9.fsf@ruckus.brouhaha.com> "Pablo Torres N." writes: > def split(seq, func=bool): > t = filter(func, seq) > f = filter(lambda x: not func(x), seq) > return list(t), list(f) That is icky--you're calling func (which might be slow) twice instead of once on every element of the seq. From lie.1296 at gmail.com Fri Jul 3 02:05:06 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:05:06 GMT Subject: question of style In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Tim Harig wrote: >> Speaking only to the style issue, when I've wanted to do something like >> that, I find: >> if self.higher is None is self.lower: >> more readable, by making clear they are both being compared to a >> constant, rather than compared to each other. > > By comparing them to *any* constant with 'is' you end up with the same > potential problems as to whether something with a value of None is actually > None? Oh really? None is a Singleton and all None always point to the same None object. And the fact that it is impossible to override `is` operator is the reason for the "is None" idiom. This does not happen with other objects though (except if you make them as Singletons as well), and in case of immutable, you may get bugs due to python's optimizing small integers. > With equality operators > everything is either the same or it isn't. How about: class Fake(object): def __eq__(self, other): return True we uses `is` when we want to make sure an object is really the same object as another, not just an equivalent object. I can start from the first > variable and compare it to the second then to the third then ... In the > case of the inequality, I have to start with the first and compare it to > all of the rest to make sure that none are the same. Then I have to do the > same for each with the remaining. This is much more difficult to do in my > head. I requires much more thought, better left to a computer, when > evaluating the expression for myself. Therefore, equalites are quick and > easy to keep straight whereas any inequalites are more error prone when > trying to evaluate how a section of code will react under different > circumstances. > >> I do draw the line at two, though, and with three or more I'll >> paren-up a list of parallel comparisons: > > As long as everything is an equality, then I don't mind comparing to the > end of an 70 column line. Mixed types of equalities or inequalities > require too many operations mentally evaluate safely. Personally, I'd only chain inequality for something like this: if 0 < x < 10: From lie.1296 at gmail.com Fri Jul 3 02:09:31 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:09:31 GMT Subject: question of style In-Reply-To: <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> Message-ID: Simon Forman wrote: > On Jul 2, 3:57 pm, Scott David Daniels wrote: >> Duncan Booth wrote: >>> Simon Forman wrote: >>>> ... >>>> if self.higher is self.lower is None: return >>>> ... >>> As a matter of style however I wouldn't use the shorthand to run two 'is' >>> comparisons together, I'd write that out in full if it was actually needed >>> here. >> Speaking only to the style issue, when I've wanted to do something like >> that, I find: >> >> if self.higher is None is self.lower: >> ... >> >> more readable, by making clear they are both being compared to a >> constant, rather than compared to each other. > > I was going to do it that way for aesthetics if nothing else, but in > this particular code "self.higher is self.lower" could only have been > True if they were both None, so the final "is None" was redundant and > I only included it as a "just-in-case" check in case someone someday > used the code in such a way as to assign the same object (not None) to > both self.higher and self.lower... Totally pointless, I'm sorry to > say. I'm glad the whole line was redundant really. > I say, you should keep the `is None` for readability and to safe guard against immutable optimization and/or Singleton objects. Due to an implementation detail, python does not guarantee that two immutable object will return different objects; you have: >>> a = 1 >>> b = 1 >>> a is b True From schickb at gmail.com Fri Jul 3 02:31:18 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 23:31:18 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> On Jul 2, 9:40?pm, "Pablo Torres N." wrote: > > If it is speed that we are after, it's my understanding that map and > filter are faster than iterating with the for statement (and also > faster than list comprehensions). ?So here is a rewrite: > > def split(seq, func=bool): > ? ? ? ? t = filter(func, seq) > ? ? ? ? f = filter(lambda x: not func(x), seq) > ? ? ? ? return list(t), list(f) > In my simple tests, that takes 1.8x as long as the original solution. Better than the itertools solution, when "func" is short and fast. I think the solution here would worse if func was more complex. Either way, what I am still wondering is if people would find a built- in implementation useful? -Brad From gagsl-py2 at yahoo.com.ar Fri Jul 3 02:31:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 03 Jul 2009 03:31:42 -0300 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <7xy6r6fjm9.fsf@ruckus.brouhaha.com> Message-ID: En Fri, 03 Jul 2009 01:58:22 -0300, > escribi?: > "Pablo Torres N." writes: >> def split(seq, func=bool): >> t = filter(func, seq) >> f = filter(lambda x: not func(x), seq) >> return list(t), list(f) > > That is icky--you're calling func (which might be slow) twice instead > of once on every element of the seq. In addition, this doesn't work if seq is an iterator instead of a sequence. -- Gabriel Genellina From lie.1296 at gmail.com Fri Jul 3 02:33:03 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:33:03 GMT Subject: question of style In-Reply-To: <7x63eahp8z.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x63eahp8z.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Generally, having a special > value like None to denote a missing datum was considered standard > practice a few decades ago, I guess in python, None as the missing datum idiom is still quite prevalent: def cat_list(a=None, b=None): # poor man's list concatenation if a is None and b is None: return [] if a is None: return b if b is None: return a return a + b From schickb at gmail.com Fri Jul 3 02:34:21 2009 From: schickb at gmail.com (Brad) Date: Thu, 2 Jul 2009 23:34:21 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: On Jul 2, 8:17?pm, "Pablo Torres N." wrote: > > This sounds like it belongs to the python-ideas list. ?I suggest > posting there for better feedback, since the core developers check > that list more often than this one. I tried posting on python-ideas and received a "You are not allowed to post to this mailing list" reply. Perhaps because I am posting through Google groups? Or maybe one must be an approved member to post? -Brad From lie.1296 at gmail.com Fri Jul 3 02:37:32 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:37:32 GMT Subject: question of style In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Lie Ryan wrote: > Tim Harig wrote: >>> Speaking only to the style issue, when I've wanted to do something like >>> that, I find: >>> if self.higher is None is self.lower: >>> more readable, by making clear they are both being compared to a >>> constant, rather than compared to each other. >> By comparing them to *any* constant with 'is' you end up with the same >> potential problems as to whether something with a value of None is actually >> None? > > Oh really? None is a Singleton and all None always point to the same > None object. And the fact that it is impossible to override `is` > operator is the reason for the "is None" idiom. > http://docs.python.org/c-api/none.html From aotto1968 at users.sourceforge.net Fri Jul 3 02:39:17 2009 From: aotto1968 at users.sourceforge.net (Andreas Otto) Date: Fri, 03 Jul 2009 08:39:17 +0200 Subject: ANNOUNCE: libmsgque 3.4 Message-ID: Dear Users, I would like to provide a new !! Major Feature Release !! of >>> LibMsgque (3.4) <<< This Release Introduce a new technology called >>> Master / Slave - Link <<< and is used to create, mange and configure a MESH of node's (process or thread) distributed over multiple hosts. But a picture says more then all the words, read more at: -> http://libmsgque.sourceforge.net/master_versa_slave.htm To get the Details and the Downloads read more at: -> http://libmsgque.sourceforge.net/ P.S. sourceforge introduce a new interface yesterday .... and this has still some bugs ... to get the release 3.4 files you have to use the files link. mfg Andreas Otto From lie.1296 at gmail.com Fri Jul 3 02:40:19 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:40:19 GMT Subject: question of style In-Reply-To: <7xljn6pvgk.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <7xljn6pvgk.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Tim Harig writes: >> That being the case, it might be a good idea either to handle the situation >> and raise an exception or add: >> >> assert self.lower <= self.higher >> >> That way an exception will be raised if there is an error somewhere else >> in the code rather then silently passing a possibly incorrect value. > > Well, that assert is not right because you have to handle the case > where one of the values is None. The general sentiment is reasonable > though, if you're concerned that the precondition may not be valid. Well, it depends on where you put the assert statement. If your code is like this: if self.higher is self.lower is None: return if self.lower is None: return self.higher if self.higher is None: return self.lower assert self.lower <= self.higher then the assert statement is correct. Some people might prefer to keep all asserts at the top of function though. From ricli85 at gmail.com Fri Jul 3 02:46:46 2009 From: ricli85 at gmail.com (Rickard Lindberg) Date: Fri, 3 Jul 2009 08:46:46 +0200 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: <890d8ab70907022346r635b00dfr5001e65838a1b994@mail.gmail.com> > I tried posting on python-ideas and received a "You are not allowed to > post to this mailing list" reply. Perhaps because I am posting through > Google groups? Or maybe one must be an approved member to post? If you got an "awaiting moderator approval" message you post might appear on the list soon. The reason for getting those can be that it is a member only list and you posted from another address. I am not sure if that was the message you got. -- Rickard Lindberg From lie.1296 at gmail.com Fri Jul 3 02:47:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 06:47:49 GMT Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? If self is an object (and it must have been), it would be preferrable to set self.higher and self.lower to a known value inside self.__init__ and avoid having the comparison. I'm guessing you have something like this in your __init__: def __init__(self, lower=None, higher=None): self.lower = lower self.higher = higher that should be changed to: def __init__(self, lower=None, higher=None): self.lower = lower if lower is not None else higher self.higher = higher if lower is not None else lower or whatever else-value you see is appropriate for the context. From javier.collado at gmail.com Fri Jul 3 02:55:28 2009 From: javier.collado at gmail.com (Javier Collado) Date: Fri, 3 Jul 2009 08:55:28 +0200 Subject: Config files with different types In-Reply-To: References: Message-ID: Hello, Have you considered using something that is already developed? You could take a look at this presentation for an overview of what's available: http://us.pycon.org/2009/conference/schedule/event/5/ Anyway, let me explain that, since I "discovered" it, my favourite format for configuration files is yaml (http://yaml.org/, http://pyyaml.org/). It's easy to read, easy to write, available in different programming languagues, etc. In addition to this, type conversion is already in place so I think it covers your requirement. For example: IIn [1]: import yaml In [2]: yaml.load("""name: person name ...: age: 25 ...: is_programmer: true""") Out[2]: {'age': 25, 'is_programmer': True, 'name': 'person name'} Best regards, Javier 2009/7/2 Zach Hobesh : > Hi all, > > I've written a function that reads a specifically formatted text file > and spits out a dictionary. ?Here's an example: > > config.txt: > > Destination = C:/Destination > Overwrite = True > > > Here's my function that takes 1 argument (text file) > > the_file = open(textfile,'r') > linelist = the_file.read().split('\n') > the_file.close() > configs = {} > for line in linelist: > ? ? ? try: > ? ? ? ? ? ? ?key,value = line.split('=') > ? ? ? ? ? ? ?key.strip() > ? ? ? ? ? ? ?value.strip() > ? ? ? ? ? ? ?key.lower() > ? ? ? ? ? ? ?value.lower() > ? ? ? ? ? ? ?configs[key] = value > > ? ? ? except ValueError: > ? ? ? ? ? ? ?break > > so I call this on my config file, and then I can refer back to any > config in my script like this: > > shutil.move(your_file,configs['destination']) > > which I like because it's very clear and readable. > > So this works great for simple text config files. ?Here's how I want > to improve it: > > I want to be able to look at the value and determine what type it > SHOULD be. ?Right now, configs['overwrite'] = 'true' (a string) when > it might be more useful as a boolean. ?Is there a quick way to do > this? ?I'd also like to able to read '1' as an in, '1.0' as a float, > etc... > > I remember once I saw a script that took a string and tried int(), > float() wrapped in a try except, but I was wondering if there was a > more direct way. > > Thanks in advance, > > Zach > -- > http://mail.python.org/mailman/listinfo/python-list > From hv at tbz-pariv.de Fri Jul 3 03:02:06 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Fri, 03 Jul 2009 09:02:06 +0200 Subject: logging of strings with broken encoding In-Reply-To: <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> Message-ID: <7b5oneF223712U1@mid.individual.net> Stefan Behnel schrieb: > Thomas Guettler wrote: >> My quick fix is this: >> >> class MyFormatter(logging.Formatter): >> def format(self, record): >> msg=logging.Formatter.format(self, record) >> if isinstance(msg, str): >> msg=msg.decode('utf8', 'replace') >> return msg >> >> But I still think handling of non-ascii byte strings should be better. >> A broken logging message is better than none. > > Erm, may I note that this is not a problem in the logging library but in > the code that uses it? I know that my code passes the broken string to the logging module. But maybe I get the non-ascii byte string from a third party (psycopg2 sometime passes latin1 byte strings from postgres in error messages). I like Python very much because "it refused to guess". But in this case, "best effort" is a better approach. It worked in 2.5 and will in py3k. I think it is a bug, that it does not in 2.6. Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From lie.1296 at gmail.com Fri Jul 3 03:16:35 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 07:16:35 GMT Subject: Sequence splitting In-Reply-To: <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: Brad wrote: > On Jul 2, 9:40 pm, "Pablo Torres N." wrote: >> If it is speed that we are after, it's my understanding that map and >> filter are faster than iterating with the for statement (and also >> faster than list comprehensions). So here is a rewrite: >> >> def split(seq, func=bool): >> t = filter(func, seq) >> f = filter(lambda x: not func(x), seq) >> return list(t), list(f) >> > > In my simple tests, that takes 1.8x as long as the original solution. > Better than the itertools solution, when "func" is short and fast. I > think the solution here would worse if func was more complex. > > Either way, what I am still wondering is if people would find a built- > in implementation useful? > > -Brad A built-in/itertools should always try to provide the general solution to be as useful as possible, something like this: def group(seq, func=bool): ret = {} for item in seq: fitem = func(item) try: ret[fitem].append(item) except KeyError: ret[fitem] = [item] return ret definitely won't be faster, but it is a much more general solution. Basically, the function allows you to group sequences based on the result of func(item). It is similar to itertools.groupby() except that this also group non-contiguous items. From lie.1296 at gmail.com Fri Jul 3 03:25:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 07:25:28 GMT Subject: logging of strings with broken encoding In-Reply-To: <7b5oneF223712U1@mid.individual.net> References: <7b3vpqF20nct6U1@mid.individual.net> <7b43faF21l89hU1@mid.individual.net> <4a4ce7f1$0$32669$9b4e6d93@newsspool2.arcor-online.net> <7b5oneF223712U1@mid.individual.net> Message-ID: Thomas Guettler wrote: > Stefan Behnel schrieb: >> Thomas Guettler wrote: >>> My quick fix is this: >>> >>> class MyFormatter(logging.Formatter): >>> def format(self, record): >>> msg=logging.Formatter.format(self, record) >>> if isinstance(msg, str): >>> msg=msg.decode('utf8', 'replace') >>> return msg >>> >>> But I still think handling of non-ascii byte strings should be better. >>> A broken logging message is better than none. >> Erm, may I note that this is not a problem in the logging library but in >> the code that uses it? > > I know that my code passes the broken string to the logging module. But maybe > I get the non-ascii byte string from a third party (psycopg2 sometime passes > latin1 byte strings from postgres in error messages). If the database contains non-ascii byte string, then you could repr() them before logging (repr also adds some niceties such as quotes). I think that's the best solution, unless you want to decode the byte string (which might be an overkill, depending on the situation). > I like Python very much because "it refused to guess". But in this case, "best effort" > is a better approach. One time it refused to guess, then the next time it tries best effort. I don't think Guido liked such inconsistency. > It worked in 2.5 and will in py3k. I think it is a bug, that it does not in 2.6. In python 3.x, the default string is unicode string. If it works in python 2.5, then it is a bug in 2.5 From http Fri Jul 3 03:27:54 2009 From: http (Paul Rubin) Date: 03 Jul 2009 00:27:54 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x63eahp8z.fsf@ruckus.brouhaha.com> Message-ID: <7x8wj6usxx.fsf@ruckus.brouhaha.com> Lie Ryan writes: > I guess in python, None as the missing datum idiom is still quite prevalent: Well, sometimes there is no way around it, but: > def cat_list(a=None, b=None): > # poor man's list concatenation > if a is None and b is None: return [] > if a is None: return b > if b is None: return a > return a + b def cat_list(a=[], b=[]): return a + b From nillgump at gmail.com Fri Jul 3 03:31:27 2009 From: nillgump at gmail.com (nillgump) Date: Fri, 3 Jul 2009 00:31:27 -0700 (PDT) Subject: use python to edit pdf document Message-ID: <7094d63e-21a6-4b1a-995a-280b0b1b26c4@q40g2000prh.googlegroups.com> hi all. I are looking for some packages which use python to edit pdf format docutment. when I searched the internet,I found the paper " Using Python as PDF Editing and Processing Framework" which is at: *********http://www.python.org/workshops/2002-02/papers/17/ index.htm********* this paper is posted at 2002.it had a long years.But I cann't find the impletation. Does anyone know what happened about this?And I have seen the Reportlab which can generate the pdf .But what I want is not only this,but also can edit the pdf. So does everyone know there are some exsiting python packages about this? With using the exsiting things,I can save much work. Any advice is appropriated! --nillgump --2009/7/3 --China From lie.1296 at gmail.com Fri Jul 3 03:31:45 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 07:31:45 GMT Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: Rickard Lindberg wrote: >> I tried posting on python-ideas and received a "You are not allowed to >> post to this mailing list" reply. Perhaps because I am posting through >> Google groups? Or maybe one must be an approved member to post? > > If you got an "awaiting moderator approval" message you post might appear on > the list soon. The reason for getting those can be that it is a member only > list and you posted from another address. I am not sure if that was the message > you got. > AFAIK, python-ideas is not moderated (I followed python-ideas). I've never used Google Groups to access it though. Try subscribing to the mailing list directly (instead of using Google Group's web-gateway) here: http://mail.python.org/mailman/listinfo/python-ideas From clp2 at rebertia.com Fri Jul 3 03:44:43 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 00:44:43 -0700 Subject: use python to edit pdf document In-Reply-To: <7094d63e-21a6-4b1a-995a-280b0b1b26c4@q40g2000prh.googlegroups.com> References: <7094d63e-21a6-4b1a-995a-280b0b1b26c4@q40g2000prh.googlegroups.com> Message-ID: <50697b2c0907030044p740e0e96h3f1ac9a81b578d57@mail.gmail.com> On Fri, Jul 3, 2009 at 12:31 AM, nillgump wrote: > hi all. > I ?are looking for some packages which use python to edit pdf format > docutment. > when I searched the internet,I found ?the > paper ?" Using Python as PDF Editing and Processing Framework" ?which > is at: > *********http://www.python.org/workshops/2002-02/papers/17/ > index.htm********* > this paper is posted at ?2002.it ?had a ?long ?years.But I cann't > find ?the impletation. > Does anyone know what ?happened ?about > this?And ?I ?have seen the Reportlab ?which can generate the pdf .But > what I ?want is not only this,but also can edit the pdf. See ReportLab's proprietary+commercial PageCatcher library (http://developer.reportlab.com/). Cheers, Chris -- http://blog.rebertia.com From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 03:50:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 07:50:53 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> Message-ID: <025da9f8$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 21:56:40 -0700, Paul Rubin wrote: >> Well I wouldn't know, I've been fortunate enough to program mostly in >> python for over half a decade now and None and 0 are as close as I've >> gotten to NULL in a long time. > > Right, and how many times have you had to debug > > AttributeError: 'NoneType' object has no attribute 'foo' Hardly ever, and mostly when I do something silly like: another_list = alist.sort() Of course, YMMV. > or the equivalent null pointer exceptions in Java, C, or whatever? They > are very common. And the basic idea is that if you avoid using null > pointers in the first place, you'll get fewer accidental null pointer > exceptions. And some other error instead, due to the bugs in your more complicated code *wink* -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 03:57:02 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 07:57:02 GMT Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> Message-ID: <025dab69$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 22:10:14 -0500, Pablo Torres N. wrote: > This sounds like it belongs to the python-ideas list. I suggest posting > there for better feedback, since the core developers check that list > more often than this one. If you post to python-ideas, you'll probably be told to gather feedback here first. The core-developers aren't hugely interested in arbitrary new features unless they have significant community support. I've never needed such a split function, and I don't like the name, and the functionality isn't general enough. I'd prefer something which splits the input sequence into as many sublists as necessary, according to the output of the key function. Something like itertools.groupby(), except it runs through the entire sequence and collates all the elements with identical keys. E.g.: splitby(range(10), lambda n: n%3) => [ (0, [0, 3, 6, 9]), (1, [1, 4, 7]), (2, [2, 5, 8]) ] Your split() would be nearly equivalent to this with a key function that returns a Boolean. -- Steven From http Fri Jul 3 04:02:56 2009 From: http (Paul Rubin) Date: 03 Jul 2009 01:02:56 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7x1voy5h3j.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > I've never needed such a split function, and I don't like the name, and > the functionality isn't general enough. I'd prefer something which splits > the input sequence into as many sublists as necessary, according to the > output of the key function. Something like itertools.groupby(), except it > runs through the entire sequence and collates all the elements with > identical keys. No really, groupby makes iterators, not lists, and it you have to develop quite a delicate sense of when you can use it without having bugs caused by the different iterators it makes getting advanced at the wrong times. The concept of a split function that actually works on lists is useful. I'm neutral about whether it's worth having a C version in the stdlib. From lie.1296 at gmail.com Fri Jul 3 04:06:18 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 08:06:18 GMT Subject: question of style In-Reply-To: <7x8wj6usxx.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x63eahp8z.fsf@ruckus.brouhaha.com> <7x8wj6usxx.fsf@ruckus.brouhaha.com> Message-ID: <_Zi3m.2347$ze1.458@news-server.bigpond.net.au> Paul Rubin wrote: > Lie Ryan writes: >> I guess in python, None as the missing datum idiom is still quite prevalent: > > Well, sometimes there is no way around it, but: > >> def cat_list(a=None, b=None): >> # poor man's list concatenation >> if a is None and b is None: return [] >> if a is None: return b >> if b is None: return a >> return a + b > > def cat_list(a=[], b=[]): > return a + b Being super contrived is why I tagged it as poor man's concat. Generally, there will be a processing: ... if b is None: return a # processing here return retlist ... and it will not be about concatenating two lists, but something more complex. But I thought that was unnecessary since I just want to mention about the None argument idiom. From stef.mientki at gmail.com Fri Jul 3 04:07:50 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Fri, 03 Jul 2009 10:07:50 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> References: <4A4BC22D.70502@gmail.com> <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> Message-ID: <4A4DBC56.9020202@gmail.com> ryles wrote: > On Jul 2, 1:25 am, Terry Reedy wrote: > >>> The next statement works, >>> but I'm not sure if it will have any dramatical side effects, >>> other than overruling a possible object with the name A >>> >>> def some_function ( ...) : >>> A = object ( ...) >>> sys._getframe(1).f_globals [ Name ] = A >>> >> global name >> name = A >> >> or is name is a string var >> globals()[name] = A >> > > It wasn't explicit, but I think Stef meant that the global should be > added to the caller's environment, which was the reason for > sys._getframe(). > > Is this environment only intended for interactive use? I wonder if you > might just set things up > in a PYTHONSTARTUP script instead. > the idea is to get a simple environment where you can do interactive 3D geometry, without knowing anything about Python. So to create a triangle e.g., the whole program will look like this: Point ( 'A', (1,1,0) ) Point ( 'B', (5,5,0) ) Point ( 'C', (1,5,0) ) Line_Segment ( 'AB' ) Line_Segment ( 'BC' ) Line_Segment ( 'AC' ) And now the points A,B,C and the line segments AB,BC,AC also exists as variables in the namespace of the above environment. So now you can add a point "D" in the middle of the line-segment AB, by giving the formula of that point: Point ( 'D', ' ( A + B ) / 2 ' ) Or display the properties of point A, with : Print A which (for the moment) will result in: Point Instance: A Childs : AB(Line-Segment), AC(Line-Segment), Parents: The graphics above is done in VPython, which gives you an easy way to make all objects dragable, and if objects are defined by formulas, the will stick together according to that formula. And now it's obvious I can't use the solution of Terry, because I don't know what names will to become global. thanks, Stef -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: moz-screenshot-3.jpg Type: image/jpeg Size: 3333 bytes Desc: not available URL: From ldo at geek-central.gen.new_zealand Fri Jul 3 04:08:20 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 03 Jul 2009 20:08:20 +1200 Subject: No trees in the stdlib? References: Message-ID: In message , Jo?o Valverde wrote: > Lawrence D'Oliveiro wrote: > >> In message , Jo?o >> Valverde wrote: >> >>> Simple example usage case: Insert string into data structure in sorted >>> order if it doesn't exist, else retrieve it. >> >> the_set = set( ... ) >> >> if str in the_set : >> ... "retrieval" case ... >> else : >> the_set.add(str) >> #end if >> >> Want sorted order? >> >> sorted(tuple(the_set)) >> >> What could be simpler? > > Try putting that inside a loop with thousands of iterations and you'll > see what the problem is. You could apply the same argument to anything. E.g. why create a tree structure with a million elements? Try putting that inside a loop with thousands of iterations and you'll see what the problem is. From deets at nospam.web.de Fri Jul 3 04:11:02 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 03 Jul 2009 10:11:02 +0200 Subject: XML(JSON?)-over-HTTP: How to define API? In-Reply-To: References: Message-ID: <7b5somF21peceU1@mid.uni-berlin.de> Allen Fowler schrieb: > > > >> I have an (in-development) python system that needs to shuttle events / requests >> around over the network to other parts of itself. It will also need to >> cooperate with a .net application running on yet a different machine. >> >> So, naturally I figured some sort of HTTP event / RPC type of would be a good >> idea? >> >> Are there any modules I should know about, or guidelines I could read, that >> could aid me in the design of the API? >> >> > > > To clarify: > > Each message would be <1KB of data total, and consist of some structured object containing strings, numbers, dates, etc. > > For instance there would be an "add user" request that would contain one or more User objects each having a number of properties like: > > - Full Name > - Username > - Password > - Email addresses (a variable length array) > - Street Address line1 > - Street Address line1 > - Street Address line1 > - City > - State > - Zip > - Sign Up Date > > .... and so on. > > > Since I need to work with other platforms, pickle is out... what are the alternatives? XML? JSON? > > How should I formally define each of the valid messages and objects? > > Thank you, Use XMLRPC. Implementations for both languages are available. There is no need for formal spec - which is a good thing. You just call the server, and it works. Diez From clp2 at rebertia.com Fri Jul 3 04:12:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 01:12:20 -0700 Subject: Sequence splitting In-Reply-To: <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: <50697b2c0907030112g38a3c992tb07eea45acc58312@mail.gmail.com> On Thu, Jul 2, 2009 at 11:31 PM, Brad wrote: > On Jul 2, 9:40?pm, "Pablo Torres N." wrote: >> >> If it is speed that we are after, it's my understanding that map and >> filter are faster than iterating with the for statement (and also >> faster than list comprehensions). ?So here is a rewrite: >> >> def split(seq, func=bool): >> ? ? ? ? t = filter(func, seq) >> ? ? ? ? f = filter(lambda x: not func(x), seq) >> ? ? ? ? return list(t), list(f) >> > > In my simple tests, that takes 1.8x as long as the original solution. > Better than the itertools solution, when "func" is short and fast. I > think the solution here would worse if func was more complex. > > Either way, what I am still wondering is if people would find a built- > in implementation useful? FWIW, Ruby has Enumerable#partition, which does the same thing as split() and has a better name IMHO. http://www.ruby-doc.org/core/classes/Enumerable.html#M003130 Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Fri Jul 3 04:15:35 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 01:15:35 -0700 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: <4A4DBC56.9020202@gmail.com> References: <4A4BC22D.70502@gmail.com> <4eab6e76-833a-4cc7-a21d-c3d8ed344f8e@a7g2000yqk.googlegroups.com> <4A4DBC56.9020202@gmail.com> Message-ID: <50697b2c0907030115l5aab4816i159eb22d39785f0f@mail.gmail.com> On Fri, Jul 3, 2009 at 1:07 AM, Stef Mientki wrote: > ryles wrote: > > On Jul 2, 1:25 am, Terry Reedy wrote: > > > The next statement works, > but I'm not sure if it will have any dramatical side effects, > other than overruling a possible object with the name A > > > def some_function ( ...) : > A = object ( ...) > sys._getframe(1).f_globals [ Name ] = A > > > global name > name = A > > or is name is a string var > globals()[name] = A > > > It wasn't explicit, but I think Stef meant that the global should be > added to the caller's environment, which was the reason for > sys._getframe(). > > Is this environment only intended for interactive use? I wonder if you > might just set things up > in a PYTHONSTARTUP script instead. > > > the idea is to get a simple environment where you can do interactive 3D > geometry, > without knowing anything about Python. > So to create a triangle e.g., the whole program will look like this: > > Point ( 'A', (1,1,0) ) > Point ( 'B', (5,5,0) ) > Point ( 'C', (1,5,0) ) > Line_Segment ( 'AB' ) > Line_Segment ( 'BC' ) > Line_Segment ( 'AC' ) > > > > And now the points A,B,C and the line segments AB,BC,AC also exists as > variables in the namespace of the above environment. > So now you can add a point "D" in the middle of the line-segment AB, by > giving the formula of that point: > > Point ( 'D',? ' ( A + B ) / 2 ' ) > > Or display the properties of point A, with : > > Print A > > which (for the moment) will result in: > > Point Instance: A > ? Childs : AB(Line-Segment), AC(Line-Segment), > ? Parents: > > The graphics above is done in VPython, which gives you an easy way to make > all objects dragable, > and if objects are defined by formulas, the will stick together according to > that formula. > > And now it's obvious I can't use the solution of Terry, > because I don't know what names will to become global. Have you considered just using imports instead? Requiring a single opaque: from graphicslibthingy import * at the start of a file using the environment isn't so bad, IMHO. Cheers, Chris -- http://blog.rebertia.com From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:18:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:18:19 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> Message-ID: <025db066$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 22:14:18 +0000, Tim Harig wrote: > On 2009-07-02, Paul Rubin wrote: >> Tim Harig writes: >>> If lower is 5 and higher is 3, then it returns 3 because 3 != None in >>> the first if. >> Sorry, the presumption was that lower <= higher, i.e. the comparison >> had already been made and the invariant was enforced by the class >> constructor. The comment should have been more explicit, I guess. > > That being the case, it might be a good idea either to handle the > situation and raise an exception or add: > > assert self.lower <= self.higher Only if you want strange and mysterious bugs whenever somebody runs your code with the -O flag. > That way an exception will be raised if there is an error somewhere else > in the code rather then silently passing a possibly incorrect value. asserts are disabled when the -O flag is given. You should never use asserts for testing data. That's not what they're for. They're for testing program logic (and unit-testing). This is wrong, because it will sometimes fail when running under -O: def parrot(colour): assert colour.lower() in ('red', 'green', 'blue') return "Norwegian %s" % colour.title() That should be written as test followed by an explicit raise: def parrot(colour): if colour.lower() not in ('red', 'green', 'blue'): raise ValueError return "Norwegian %s" % colour.title() An acceptable use for assert is to verify your program logic by testing something which should never fail. If it does fail, then you know something bizarre and unexpected has happened, and you have a program bug rather than bad data. Here's a silly example: def cheese_available(kind): """Return True if the kind of cheese specified is in stock in the cheeseshop run by Michael Palin. This function always returns False. """ return False def get_cheese(): exchange_amusing_banter() for kind in ('Cheddar', 'Gouda', 'Swiss', 'Camembert'): ask_proprietor("Do you have any %s?" % kind) flag = cheese_available(kind) assert not flag if flag: buy_cheese(kind) return None else: express_disappointment() # We only get here if no cheese is bought. print "Cleese: Does this cheeseshop actually have ANY cheese?" print "Palin: No sir, I'm afraid I've been wasting your time." print "Cleese: Well then, I'm going to have to shoot you." -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:19:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:19:23 GMT Subject: Why re.match()? References: Message-ID: <025db0a6$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: > I'm sure that it is possible to find cases in which the *current* > implementation of re.search() would be inefficient, but that's because > this implementation is perverse, which, I guess, is ultimately the point > of my original post. Why privilege the special case of a > start-of-string anchor? Because wanting to see if a string matches from the beginning is a very important and common special case. > What if you wanted to apply an end-anchored > pattern to some prefix of your 4GB string? Why not have a special re > method for that? And another for every possible special case? Because they're not common special cases. They're rare and not special enough to justify special code. > If the concern is efficiency for such cases, then simply implement > optional offset and length parameters for re.search(), to specify any > arbitrary substring to apply the search to. To have a special-case > re.match() method in addition to a general re.search() method is > antithetical to language minimalism, and plain-old bizarre. Maybe > there's a really good reason for it, but it has not been mentioned yet. There is, and it has. You're welcome to keep your own opinion, but I don't think you'll find many experienced Python coders will agree with it. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:21:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:21:09 GMT Subject: PEP368 and pixeliterators References: Message-ID: <025db110$0$20657$c3e8da3@news.astraweb.com> On Thu, 02 Jul 2009 10:32:04 +0200, Joachim Str?mbergson wrote: > for pixel in rgb_image: > # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, > pixel.r > > > The idea I'm having is that fundamentally the image is made up of a 2D > array of pixels, not rows of pixels. A 2D array implies rows (and columns) of pixels. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:26:59 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:26:59 GMT Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> Message-ID: <025db26e$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 01:02:56 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> I've never needed such a split function, and I don't like the name, and >> the functionality isn't general enough. I'd prefer something which >> splits the input sequence into as many sublists as necessary, according >> to the output of the key function. Something like itertools.groupby(), >> except it runs through the entire sequence and collates all the >> elements with identical keys. > > No really, groupby makes iterators, not lists, and it you have to > develop quite a delicate sense of when you can use it without having > bugs caused by the different iterators it makes getting advanced at the > wrong times. The concept of a split function that actually works on > lists is useful. I'm neutral about whether it's worth having a C > version in the stdlib. groupby() works on lists. The difference between what I'm suggesting and what groupby() does is that my suggestion would collate *all* the elements with the same key, not just runs of them. This (as far as I can tell) requires returning lists rather than iterators. The most important difference between my suggestion and that of the OP is that he limited the key function to something which returns a truth value, while I'm looking for something more general which can split the input into an arbitrary number of collated sublists. -- Steven From nick at craig-wood.com Fri Jul 3 04:30:02 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Fri, 03 Jul 2009 03:30:02 -0500 Subject: how to spawn a process under different user References: <25765da2-7d27-48c8-b7c8-125fdc6c1c71@g1g2000pra.googlegroups.com> Message-ID: Gabriel Genellina wrote: > En Thu, 02 Jul 2009 19:27:05 -0300, Tim Harig > escribi?: > > On 2009-07-02, sanket wrote: > >>> sanket wrote: > >>> > I am trying to use python's subprocess module to launch a process. > >>> > but in order to do that I have to change the user. > >> I am using python 2.4 on centos. > > > > I have never done this in python; but, using the normal system calls in C > > the process is basically: > > > > 1. fork() a new process > > 2. the child process changes its user id with setreuid() and > > possibly its group id with setregid() > > 3. then the child exec()s new process which replaces itself > > > > All of the necessary functions should be under the os module on POSIX > > operating systems. > > How to do that using the subprocess module: write a function for item (2) > above and pass it as the preexec_fn argument to Popen. preexec_fn is > executed after fork() and before exec() If you are forking 100s of processes a second you want to use the method above, however I think it is easier to use su (assuming you start off as root), so instead of passing ['mycommand', 'my_arg1', 'my_arg2'] to Popen, pass ['su', '-', 'username', '-c', 'mycommand my_arg1 my_arg2'] There is some opportunity for quoting problems there, but it is easy! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From http Fri Jul 3 04:33:27 2009 From: http (Paul Rubin) Date: 03 Jul 2009 01:33:27 -0700 Subject: No trees in the stdlib? References: Message-ID: <7xocs2go88.fsf@ruckus.brouhaha.com> Lawrence D'Oliveiro writes: > >> Want sorted order? > >> sorted(tuple(the_set)) > >> What could be simpler? > > > > Try putting that inside a loop with thousands of iterations and you'll > > see what the problem is. > > You could apply the same argument to anything. E.g. why create a tree > structure with a million elements? Try putting that inside a loop with > thousands of iterations and you'll see what the problem is. The idea is you're going to insert or delete something in the tree structure at each iteration, an O(log n) operation, making the whole loop O(n log n). If you sort a set (which is O(n log n)) inside the loop then you end up with O(n**2 log n) which is impractical. A reason you might sort inside the loop might be to find the nearby neighbors of the new element or traverse some elements in the middle. This is trivial with a tree structure but messy with something like sets. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 04:39:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 08:39:19 GMT Subject: No trees in the stdlib? References: Message-ID: <025db552$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 20:08:20 +1200, Lawrence D'Oliveiro wrote: > In message , Jo?o > Valverde wrote: > >> Lawrence D'Oliveiro wrote: [...] >>> Want sorted order? >>> >>> sorted(tuple(the_set)) >>> >>> What could be simpler? >> >> Try putting that inside a loop with thousands of iterations and you'll >> see what the problem is. > > You could apply the same argument to anything. E.g. why create a tree > structure with a million elements? Try putting that inside a loop with > thousands of iterations and you'll see what the problem is. The difference is, it's vanishingly rare to want to build a tree with millions of elements thousands of times over and over again, but it is not unreasonable to want to access sorted data thousands of times. Needing to re-sort it over and over and over again is wasteful, slow and stupid. Binary trees were invented, in part, specifically to solve this use-case. -- Steven From http Fri Jul 3 04:39:27 2009 From: http (Paul Rubin) Date: 03 Jul 2009 01:39:27 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xk52qgny8.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > groupby() works on lists. >>> a = [1,3,4,6,7] >>> from itertools import groupby >>> b = groupby(a, lambda x: x%2==1) # split into even and odd >>> c = list(b) >>> print len(c) 3 >>> d = list(c[1][1]) # should be [4,6] >>> print d # oops. [] > The difference between what I'm suggesting and what groupby() does is > that my suggestion would collate *all* the elements with the same key, > not just runs of them. This (as far as I can tell) requires returning > lists rather than iterators. I guess that is reasonable. > The most important difference between my suggestion and that of the OP is > that he limited the key function to something which returns a truth > value, while I'm looking for something more general which can split the > input into an arbitrary number of collated sublists. Also ok. From pyklass at gmail.com Fri Jul 3 04:55:04 2009 From: pyklass at gmail.com (Goksie Aruna) Date: Fri, 03 Jul 2009 09:55:04 +0100 Subject: is it possible to write USSD / SMS /SS7 apps in python Message-ID: <4A4DC768.40604@gmail.com> Can someone give me an insight into these? developing ss7 or USSD or SMS apps in python. is there any existing ones in this manner? Thanks From tsangpo.newsgroup at gmail.com Fri Jul 3 05:03:51 2009 From: tsangpo.newsgroup at gmail.com (tsangpo) Date: Fri, 3 Jul 2009 17:03:51 +0800 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: Just a shorter implementation: from itertools import groupby def split(lst, func): gs = groupby(lst, func) return list(gs[True]), list(gs[False]) "Lie Ryan" ????????????:nfi3m.2341$ze1.1151 at news-server.bigpond.net.au... > Brad wrote: >> On Jul 2, 9:40 pm, "Pablo Torres N." wrote: >>> If it is speed that we are after, it's my understanding that map and >>> filter are faster than iterating with the for statement (and also >>> faster than list comprehensions). So here is a rewrite: >>> >>> def split(seq, func=bool): >>> t = filter(func, seq) >>> f = filter(lambda x: not func(x), seq) >>> return list(t), list(f) >>> >> >> In my simple tests, that takes 1.8x as long as the original solution. >> Better than the itertools solution, when "func" is short and fast. I >> think the solution here would worse if func was more complex. >> >> Either way, what I am still wondering is if people would find a built- >> in implementation useful? >> >> -Brad > > A built-in/itertools should always try to provide the general solution > to be as useful as possible, something like this: > > def group(seq, func=bool): > ret = {} > for item in seq: > fitem = func(item) > try: > ret[fitem].append(item) > except KeyError: > ret[fitem] = [item] > return ret > > definitely won't be faster, but it is a much more general solution. > Basically, the function allows you to group sequences based on the > result of func(item). It is similar to itertools.groupby() except that > this also group non-contiguous items. From usernet at ilthio.net Fri Jul 3 05:07:30 2009 From: usernet at ilthio.net (Tim Harig) Date: Fri, 03 Jul 2009 09:07:30 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xws6qrgy5.fsf@ruckus.brouhaha.com> <7xocs2222d.fsf@ruckus.brouhaha.com> <_ia3m.3614$Jb1.3478@flpi144.ffdc.sbc.com> <025db066$0$20657$c3e8da3@news.astraweb.com> Message-ID: On 2009-07-03, Steven D'Aprano wrote: > On Thu, 02 Jul 2009 22:14:18 +0000, Tim Harig wrote: >> On 2009-07-02, Paul Rubin wrote: >>> Tim Harig writes: >>>> If lower is 5 and higher is 3, then it returns 3 because 3 != None in >>>> the first if. >>> Sorry, the presumption was that lower <= higher, i.e. the comparison >>> had already been made and the invariant was enforced by the class >>> constructor. The comment should have been more explicit, I guess. >> That being the case, it might be a good idea either to handle the >> situation and raise an exception or add: >> assert self.lower <= self.higher > Only if you want strange and mysterious bugs whenever somebody runs your > code with the -O flag. The point here is that Rubin presumed that the condition where lower > higher should never exist. Therefore, because the program given segment of program doesn't test it elswhere, it is possible that a bug in the code that sets lower > higher could go unoticed while silently passing the wrong data. Therefore, It is better to test that assumption in a way that *will* crash if something is wrong, for instance, if another method accidently changes one of the values. That would tend to make errors in another method of the class (or even higher up in this method), more likely to be found. > asserts are disabled when the -O flag is given. Right, like defining NDEBUG in C. In _Writing Solid Code_ by Steve Maguire, he likens it to having two pieces of code: one for testing where any errors should cause crashes as early as possible and one for shipping where it may be better to handle errors if possible from within the code. > You should never use asserts for testing data. That's not what they're > for. They're for testing program logic (and unit-testing). What I am suggesting here is exactly that. If lower is always defined such that it should always be equal to or lower then higher, then there is an error in the code somewhere if lower is higher by the time this code is called. Either the constructor didn't didn't reject input properly or another method within the class has inadvertantly changed higher or lower in an uncorrect way. In any event, whether I am using it 100% correctly, I want to find any errors in my code. If there is an error, I want the program to crash during testing rather then silently passing bad data. assert will help here. Unfortunatly, Rubin is right. I did make the mistake that the assert will fail with higher or lower values of None, which is allowed here. The same basic premise is correct but will require more complex assertions to allow that through. From mjohnson at cfi.co.ug Fri Jul 3 05:18:04 2009 From: mjohnson at cfi.co.ug (Johnson Mpeirwe) Date: Fri, 03 Jul 2009 12:18:04 +0300 Subject: PSP Caching Message-ID: <4A4DCCCC.6040605@cfi.co.ug> Hello All, How do I stop caching of Python Server Pages (or whatever causes changes in a page not to be noticed in a web browser)? I am new to developing web applications in Python and after looking at implementations of PSP like Spyce (which I believed introduces new unnecessary non-PSP syntax), I decided to write my own PSP applications from scratch. When I modify a file, I keep getting the old results until I intentionally introduce an error (e.g parse error) and correct it after to have the changes noticed. There's no proxy (I am working on a windows machine unplugged from the network). I have Googled and no documents seem to talk about this. Is there any particular mod_python directive I must set in my Apache configuration to fix this? Any help will be highly appreciated. Johnson From clp2 at rebertia.com Fri Jul 3 05:21:33 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 3 Jul 2009 02:21:33 -0700 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <4A4DC768.40604@gmail.com> References: <4A4DC768.40604@gmail.com> Message-ID: <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> On Fri, Jul 3, 2009 at 1:55 AM, Goksie Aruna wrote: > Can someone give me an insight into these? > > ? developing ss7 or USSD or SMS apps in python. > > is there any existing ones in this manner? Advice for the future: STFW. http://pypi.python.org/pypi?%3Aaction=search&term=sms&submit=search Cheers, Chris -- http://blog.rebertia.com From pyklass at gmail.com Fri Jul 3 05:31:38 2009 From: pyklass at gmail.com (Goke Aruna) Date: Fri, 3 Jul 2009 10:31:38 +0100 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> Message-ID: <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> On Fri, Jul 3, 2009 at 10:21 AM, Chris Rebert wrote: > On Fri, Jul 3, 2009 at 1:55 AM, Goksie Aruna wrote: > > Can someone give me an insight into these? > > > > developing ss7 or USSD or SMS apps in python. > > > > is there any existing ones in this manner? > > Advice for the future: STFW. > > http://pypi.python.org/pypi?%3Aaction=search&term=sms&submit=search > > Cheers, > Chris > -- > http://blog.rebertia.com > thanks Chris, however, i have checked all the packages there are specifics for a particular company or device. what am saying is reading the ITU info on USSD, is it possible to use python to write the application SS7 with support for TCAP/MAP talking to E1 card to do the ss7 signalling. Thanks. goksie -------------- next part -------------- An HTML attachment was scrubbed... URL: From kurt.alfred.mueller at gmail.com Fri Jul 3 05:38:48 2009 From: kurt.alfred.mueller at gmail.com (yam850) Date: Fri, 3 Jul 2009 02:38:48 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: Message-ID: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> On 1 Jul., 21:30, spillz wrote: > On Jun 29, 3:15 pm, Pascal Chambon wrote: > > I've had real issues with subprocesses recently : from a python script, > > on windows, I wanted to "give control" to a command line utility, i.e > > forward user in put to it and display its output on console. It seems > > simple, but I ran into walls : > If you are willing to have a wxPython dependency, wx.Execute handles > non-blockingi/o with processes on all supported platforms I made a python method/function for non blocking read from a file object. I use it in one of my python programs. When looking at the code bear in mind that I am not an expert and I am happy to see comments. #------------------------------------------------------------------ def non_blocking_readline(f_read=sys.stdin, timeout_select=0.0): """to readline non blocking from the file object 'f_read' for 'timeout_select' see module 'select'""" import select text_lines = '' # empty string while True: # as long as there are bytes to read try: # try select rlist, wlist, xlist = select.select([f_read], [], [], timeout_select) except: # select ERROR print >>sys.stderr, ("non_blocking_read select ERROR") break if DEBUG: print("rlist=%s, wlist=%s, xlist=%s" % (repr(rlist), repr(wlist), repr(xlist))) if len(rlist) > 0: text_read = f_read.readline() # get a line if DEBUG: print("after read/readline text_read:'%s', len= %s" % (text_read, repr(len(text_read)))) if len(text_read) > 0: # there were some bytes text_lines = "".join([text_lines, text_read]) if DEBUG: print("text_lines:'%s'" % (text_lines)) else: break # there was no byte in a line else: break # there was no byte in the f_read if text_lines == '': return None else: return text_lines -- Kurt From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 05:50:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 09:50:09 GMT Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> <7xk52qgny8.fsf@ruckus.brouhaha.com> Message-ID: <025dc5ec$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 01:39:27 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> groupby() works on lists. > >>>> a = [1,3,4,6,7] >>>> from itertools import groupby >>>> b = groupby(a, lambda x: x%2==1) # split into even and odd >>>> c = list(b) >>>> print len(c) > 3 >>>> d = list(c[1][1]) # should be [4,6] print d # oops. > [] I didn't say it worked properly *wink* Seriously, this behaviour caught me out too. The problem isn't that the input data is a list, the same problem occurs for arbitrary iterators. >From the docs: [quote] The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function). That behavior differs from SQL?s GROUP BY which aggregates common elements regardless of their input order. The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list [end quote] http://www.python.org/doc/2.6/library/itertools.html#itertools.groupby -- Steven From petshmidt at googlemail.com Fri Jul 3 06:28:25 2009 From: petshmidt at googlemail.com (Tep) Date: Fri, 3 Jul 2009 03:28:25 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com> <02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com> <9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> Message-ID: <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> On 3 Jul., 06:40, Simon Forman wrote: > On Jul 2, 4:31?am, Tep wrote: > > > > > > > On 2 Jul., 10:25, Tep wrote: > > > > On 2 Jul., 01:56, MRAB wrote: > > > > > someone wrote: > > > > > Hello, > > > > > > how can I replace '?' sign from string? Or do split at that character? > > > > > Getting unicode error if I try to do it: > > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position > > > > > 1: ordinal not in range(128) > > > > > > Thanks, Pet > > > > > > script is # -*- coding: UTF-8 -*- > > > > > It sounds like you're mixing bytestrings with Unicode strings. I can't > > > > be any more helpful because you haven't shown the code. > > > > Oh, I'm sorry. Here it is > > > > def cleanInput(input) > > > ? ? return input.replace('?', '') > > > I also need: > > > #input is html source code, I have problem with only this character > > #input = 'foo ? bar' > > #return should be foo > > def splitInput(input) > > ? ? parts = input.split(' ? ') > > ? ? return parts[0] > > > Thanks! > > Okay people want to help you but you must make it easy for us. > > Post again with a small piece of code that is runnable as-is and that > causes the traceback you're talking about, AND post the complete > traceback too, as-is. > > I just tried a bit of your code above in my interpreter here and it > worked fine: > > |>>> data = 'foo ? bar' > |>>> data.split('?') > |['foo ', ' bar'] > |>>> data = u'foo ? bar' > |>>> data.split(u'?') > |[u'foo ', u' bar'] > > Figure out the smallest piece of "html source code" that causes the > problem and include that with your next post. The problem was, I've converted "html source code" to unicode object and didn't encoded to utf-8 back, before using split... Thanks for help and sorry for not so smart question Pet > > HTH, > ~Simon > > You might also read this:http://catb.org/esr/faqs/smart-questions.html From jeanmichel at sequans.com Fri Jul 3 06:33:45 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 03 Jul 2009 12:33:45 +0200 Subject: question of style In-Reply-To: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <4A4DDE89.1080003@sequans.com> Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > Easier for you but not for those who are familiar with the standard way of python coding. (by standard I don't mean the "only Godly way of coding", there's room for custom rules) But I think most of the people will write: if self.higher is None: return self.lower if self.lower is None: return self.higher # here neither are None > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > if self.lower is None: > return > return self.lower > if self.lower is None: > return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) > From hkmshb at gmail.com Fri Jul 3 06:46:32 2009 From: hkmshb at gmail.com (Klone) Date: Fri, 3 Jul 2009 03:46:32 -0700 (PDT) Subject: Is code duplication allowed in this instance? Message-ID: Hi all. I believe in programming there is a common consensus to avoid code duplication, I suppose such terms like 'DRY' are meant to back this idea. Anyways, I'm working on a little project and I'm using TDD (still trying to get a hang of the process) and am trying to test the functionality within a method. Whoever it so happens to verify the output from the method I have to employ the same algorithm within the method to do the verification since there is no way I can determine the output before hand. So in this scenario is it OK to duplicate the algorithm to be tested within the test codes or refactor the method such that it can be used within test codes to verify itself(??). From bthate at gmail.com Fri Jul 3 06:50:36 2009 From: bthate at gmail.com (Bart Thate) Date: Fri, 3 Jul 2009 03:50:36 -0700 (PDT) Subject: GOZERBOT 0.9.1 BETA2 released Message-ID: <67b84335-211d-4f7f-b417-71faeb70fbf5@o6g2000yqj.googlegroups.com> GOZERBOT has a new website !! check it out at http://gozerbot.org. This is all in preparation for the 0.9.1 release and the latest GOZERBOT beta has been released as well. Please try this version and let me know how goes. Install is as simple as .. easy_install gozerbot gozerplugs, see README. This release will be used to move GOZERBOT 0.9 into the debian repositories and freebsd ports. we can be contacted on #dunkbots EFNET/IRCNET or use http://dev.gozerbot.org for any bugs you might find. NEW IN THIS RELEASE: * GOZERBOT is now truely free as it no longer depends on GPL licensed xmpppy, a own xmpp package has been implemented for this. * GOZERBOT now depends on setuptools to install the proper packages * gozerbot-nest script can be used to install all dependacies and bot code in 1 directory that can be run by the user (no root required) * morphs are added that allow for encryption of input and output streams (not used yet) ABOUT GOZERBOT: GOZERBOT is a channel bot that aids with conversation in irc channels and jabber conference rooms. its mainly used to serve rss feeds and to have custom commands made for the channel. More then just a channel bot GOZERBOT aims to provide a platform for the user to program his own bot and make it into something thats usefull. This is done with a plugin structure that makes it easy to program your own. But GOZERBOT comes with some batteries included, there are now over 100 plugins already written and ready for use. groet, Bart From p.f.moore at gmail.com Fri Jul 3 07:01:04 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Fri, 3 Jul 2009 12:01:04 +0100 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> Message-ID: <79990c6b0907030401w2e668ac3qd315c751d9e3e924@mail.gmail.com> 2009/7/3 Brad : > Perhaps true, but it would be a nice convenience (for me) as a built- > in written in either Python or C. Although the default case of a bool > function would surely be faster. The chance of getting this accepted as a builtin is essentially zero. To be a builtin, as opposed to being in the standard library, something has to have a very strong justification. This suggestion may find a home in the standard library, although it's not entirely clear where (maybe itertools, although it's not entirely obvious that it's a fit there). You'd have to justify this against the argument "not every 2-3 line function needs to be built in". Personally, I'm not sure it passes that test - sure, it's a useful function, but it's not that hard to write when you need it. It may be better as a recipe in the cookbook. Or if it's close enough to the spirit of the itertools, it may be suitable as a sample in the itertools documentation (the "recipes" section). Paul. From user at example.net Fri Jul 3 07:21:12 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 13:21:12 +0200 Subject: stringio+tarfile In-Reply-To: References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4a4de9a8$0$1105$4fafbaef@reader3.news.tin.it> > First problem I see is all those numbers before the lines. That's not > valid python. > > Assuming that was a transcription error not so. i intentionally add linenumbers to facilitare reference to the code, but if it is a nuisance i will not include them anymore. thanks From tkjthingone at gmail.com Fri Jul 3 07:28:47 2009 From: tkjthingone at gmail.com (Horace Blegg) Date: Fri, 3 Jul 2009 04:28:47 -0700 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> Message-ID: I've been kinda following this. I have a cousin who is permanently wheel chair bound and doesn't have perfect control of her hands, but still manages to use a computer and interact with society. However, the idea/thought of disabled programmers was new to me/hadn't ever occurred to me. You say that using your hands is painful, but what about your feet? Wouldn't it be possible to rig up some kind of foot peddle for shift/caps lock? Kinda like the power peddle used with sowing machines, so the hands are free to hold fabric. I don't mean this in a condescending manor, and I apologize if you take it as such. I'm genuinely curious if you think something like this could work. The way I was envisioning it working last night (and I haven't the faintest clue how SR works, nor have I ever used SR) was that you would hit the foot peddle, which would tell the SR program to capitalize the first letter of the next word (a smart shift, basically, so you don't end up doing something like ... WONderland -or- "stocks are up 1,0))% TOday".) Possible? Stupid? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ricli85 at gmail.com Fri Jul 3 07:29:44 2009 From: ricli85 at gmail.com (Rickard Lindberg) Date: Fri, 3 Jul 2009 13:29:44 +0200 Subject: Is code duplication allowed in this instance? In-Reply-To: References: Message-ID: <890d8ab70907030429i615c4bdat2263ac9555bd8c4e@mail.gmail.com> > Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine > the output before hand. Can you clarify this scenario a bit? If you are performing black-box testing I don't see why you need to use the same algorithm in the test code. But maybe your case is special. On the other hand you can not perform black-box testing if the output is not known for a given input. -- Rickard Lindberg From user at example.net Fri Jul 3 07:29:52 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 13:29:52 +0200 Subject: stringio+tarfile In-Reply-To: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4a4debb1$0$1106$4fafbaef@reader3.news.tin.it> thanks to the guys who bothered to answer me even using their chrystal ball ;-) i'll try to be more specific. yes: i want to create a tar file in memory, and add some content from a memory buffer... my platform: $ uname -a Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux $ python -V Python 2.3.4 following some suggestions i modified the code: $ cat tar001.py import tarfile import StringIO sfo1 = StringIO.StringIO("one\n") sfo2 = StringIO.StringIO("two\n") tfo = StringIO.StringIO() tar = tarfile.open(fileobj=tfo , mode="w") ti = tar.gettarinfo(fileobj=tfo) for sfo in [sfo1 , sfo2]: tar.addfile(fileobj=sfo , tarinfo=ti) print tfo and that's what i get: $ python tar001.py > tar001.out Traceback (most recent call last): File "tar001.py", line 7, in ? ti = tar.gettarinfo(fileobj=tfo) File "/usr/lib/python2.3/tarfile.py", line 1060, in gettarinfo name = fileobj.name AttributeError: StringIO instance has no attribute 'name' can you help? TIA ps: i'd also like that the tar file has names for the buffers, so than once output the file can be untarred with /bin/tar into regular files... From eckhardt at satorlaser.com Fri Jul 3 07:58:44 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Fri, 03 Jul 2009 13:58:44 +0200 Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: Thanks to all that answered, in particular I wasn't aware of the existence of the __del__ function. For completeness' sake, I think I have found another way to not really solve but at least circumvent the problem: weak references. If I understand correctly, those would allow me to pass out handles to the resources and, if some code decides it is time, release the resources and render all the weak references invalid. At least I don't suffer resource leaks but rather get meaningful errors that way, which is enough for my case. cheers! Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From __peter__ at web.de Fri Jul 3 08:12:38 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 03 Jul 2009 14:12:38 +0200 Subject: stringio+tarfile References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4debb1$0$1106$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > thanks to the guys who bothered to answer me even using their chrystal > ball ;-) > > i'll try to be more specific. > > yes: i want to create a tar file in memory, and add some content from a > memory buffer... > > my platform: > > $ uname -a > Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux > $ python -V > Python 2.3.4 > > following some suggestions i modified the code: > > $ cat tar001.py > import tarfile > import StringIO > sfo1 = StringIO.StringIO("one\n") > sfo2 = StringIO.StringIO("two\n") > tfo = StringIO.StringIO() > tar = tarfile.open(fileobj=tfo , mode="w") > ti = tar.gettarinfo(fileobj=tfo) gettarinfo() expects a real file, not a file-like object. You have to create your TarInfo manually. > for sfo in [sfo1 , sfo2]: > tar.addfile(fileobj=sfo , tarinfo=ti) > print tfo > > and that's what i get: > > $ python tar001.py > tar001.out > Traceback (most recent call last): > File "tar001.py", line 7, in ? > ti = tar.gettarinfo(fileobj=tfo) > File "/usr/lib/python2.3/tarfile.py", line 1060, in gettarinfo > name = fileobj.name > AttributeError: StringIO instance has no attribute 'name' > > can you help? I recommend that you have a look into the tarfile module's source code. The following seems to work: import sys import time import tarfile import StringIO sf1 = "first.txt", StringIO.StringIO("one one\n") sf2 = "second.txt", StringIO.StringIO("two\n") tf = StringIO.StringIO() tar = tarfile.open(fileobj=tf , mode="w") mtime = time.time() for name, f in [sf1 , sf2]: ti = tarfile.TarInfo(name) ti.size = f.len ti.mtime = mtime # add more attributes as needed tar.addfile(ti, f) sys.stdout.write(tf.getvalue()) Peter From sri_annauni at yahoo.co.in Fri Jul 3 08:15:47 2009 From: sri_annauni at yahoo.co.in (srinivasan srinivas) Date: Fri, 3 Jul 2009 17:45:47 +0530 (IST) Subject: Python debugger Message-ID: <740981.83964.qm@web7901.mail.in.yahoo.com> Hi, Could you suggest some python debuggers? Thanks, Srini Love Cricket? Check out live scores, photos, video highlights and more. Click here http://cricket.yahoo.com From jackdied at gmail.com Fri Jul 3 08:34:01 2009 From: jackdied at gmail.com (Jack Diederich) Date: Fri, 3 Jul 2009 08:34:01 -0400 Subject: Searching equivalent to C++ RAII or deterministic destructors In-Reply-To: <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> Message-ID: On Thu, Jul 2, 2009 at 2:36 PM, Carl Banks wrote: > On Jul 2, 3:12?am, Ulrich Eckhardt wrote: >> Bearophile wrote: >> > Ulrich Eckhardt: >> >> a way to automatically release the resource, something >> >> which I would do in the destructor in C++. >> >> > Is this helpful? >> >http://effbot.org/pyref/with.htm >> >> Yes, it aims in the same direction. However, I'm not sure this applies to my >> case. The point is that the resource handle is not just used locally in a >> restricted scope but it is allocated and stored. The 'with' is something >> that makes sense in the context of mutex locking, where you have a >> well-defined critical section. What I need is something similar to open(), >> which returs a file. When the last reference to that object goes out of >> scope, the underlying file object is closed. > > On CPython you can do it with a __del__ attribute. > > Warning: objects with a __del__ attribute prevent reference cycle > detection, which can potentially lead to memory (and resource) leaks. > So you must be careful to avoid creating reference loops with that > object. WARNING-er: As Carl points out, adding a __del__ method actually makes it /less/ likely that your object will be cleaned up. __del__ is only useful if you have a complicated tear-down procedure. Since you come from C++ RAII land (my old haunt) your objects probably only allocate one resource each, or worse: a bunch of objects that allocate one resource each. In that case __del__ will always hurt you. The C++ temptation is to match every __init__ with a __del__. A better rule of thumb is to only add a __del__ method after confirming with someone else that it would be useful. Better still is to ask them by postal courier. For best results that someone should be your grandmother. > Note that file objects have a close method; you can explicitly close > it at any time. ?Your object should follow that example, and define a > close (or release, or whatever) method. ?I'd recommend making an > effort to call it and to rely on __del__ as little as possible. This. If you care enough about a resource to write a __del__ method you care enough to clean it up explicitly. 'with' blocks are very nice for that. -jack From tn.pablo at gmail.com Fri Jul 3 08:34:20 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Fri, 3 Jul 2009 07:34:20 -0500 Subject: Sequence splitting In-Reply-To: <79990c6b0907030401w2e668ac3qd315c751d9e3e924@mail.gmail.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <79990c6b0907030401w2e668ac3qd315c751d9e3e924@mail.gmail.com> Message-ID: On Fri, Jul 3, 2009 at 06:01, Paul Moore wrote: > 2009/7/3 Brad : >> Perhaps true, but it would be a nice convenience (for me) as a built- >> in written in either Python or C. Although the default case of a bool >> function would surely be faster. > > The chance of getting this accepted as a builtin is essentially zero. > To be a builtin, as opposed to being in the standard library, > something has to have a very strong justification. That's right. Mr. schickb, I think what you need is a few concrete examples as to where this function would be beneficial, so it can be judged objectively. -- Pablo Torres N. From bieffe62 at gmail.com Fri Jul 3 08:34:27 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 3 Jul 2009 05:34:27 -0700 (PDT) Subject: Is code duplication allowed in this instance? References: Message-ID: On Jul 3, 12:46?pm, Klone wrote: > Hi all. I believe in programming there is a common consensus to avoid > code duplication, I suppose such terms like 'DRY' are meant to back > this idea. Anyways, I'm working on a little project and I'm using TDD > (still trying to get a hang of the process) and am trying to test the > functionality within a method. Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine > the output before hand. > > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). If the purpose of the test is to verify the algorithm, you obviously should not use the algorithm to verify itself ... you should use a set of pairs (input data, exoected output data) data that you know is well representative of the data your algorithm will process. Possibly to prepare the test data set you might need a different - and already proven - implementation of the algorithm. Another thing I sometime do when testing mathematics function is use counter-proof: for instance, if my function computes the roots of a quadratic equation, the test verifies that the roots applied to the equation actually give (almost) zero as result. This kind of test might not be as rigorous as preparing the data set with the known answers, but it is easier to setup and could give you a first idea if your code is "correct enough" to stand more formal proof. Ciao ---- FB From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 08:46:08 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 14:46:08 +0200 Subject: Python debugger In-Reply-To: References: Message-ID: <4a4dfd78$0$404$426a74cc@news.free.fr> srinivasan srinivas a ?crit : > Hi, > Could you suggest some python debuggers? http://docs.python.org/library/pdb.html#module-pdb HTH From lie.1296 at gmail.com Fri Jul 3 08:49:21 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 12:49:21 GMT Subject: Is code duplication allowed in this instance? In-Reply-To: References: Message-ID: Klone wrote: > Hi all. I believe in programming there is a common consensus to avoid > code duplication, I suppose such terms like 'DRY' are meant to back > this idea. Anyways, I'm working on a little project and I'm using TDD > (still trying to get a hang of the process) and am trying to test the > functionality within a method. Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine > the output before hand. > > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). When unittesting a complex output, usually the first generation code would generate the output and then you (manually) verify this output and copy the output to the test code. The testing code should be as dumb as possible to avoid bugs in the testing code itself. The most important thing is not to use a possibly buggy algorithm implementation to check the algorithm itself. If your test code looks like this: # this the function to be tested def func(a, b): return a + b class TC(unittest.TestCase): def test_foo(self): a, b = 10, 20 # worse: use random module result = a + b self.assertEqual(func(a, b), result) then something is definitely wrong. Instead the testing code should be simple and stupid like: class TC(unittest.TestCase): def test_foo(self): self.assertEqual(func(10, 20), 30) There are exceptions though, such as if you're 100% sure your first-generation program is correct and you simply want to replicate the same function with different algorithm, or probably optimizing the function. For that case, you could do something like this: def func(a, b): # some fancy new algo pass class TC(unittest.TestCase): def original_func(self, a, b): return a + b def test_foo(self): self.assertEquals(func(a, b), self.original_func(a, b)) From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 08:50:10 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 14:50:10 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: References: Message-ID: <4a4dfe6a$0$404$426a74cc@news.free.fr> Stef Mientki a ?crit : > hello, > > I need to add an object's name to the global namespace. > The reason for this is to create an environment, > where you can add some kind of math environment, > where no need for Python knowledge is needed. > > The next statement works, > but I'm not sure if it will have any dramatical side effects, > other than overruling a possible object with the name A > > def some_function ( ...) : > A = object ( ...) > sys._getframe(1).f_globals [ Name ] = A > > Anything wrong with the 'global' statement ? Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. pythonrc start pythonrc done >>> def foo(): ... global a ... a = 42 ... >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> foo() >>> a 42 >>> From ziade.tarek at gmail.com Fri Jul 3 08:57:19 2009 From: ziade.tarek at gmail.com (=?ISO-8859-1?Q?Tarek_Ziad=E9?=) Date: Fri, 3 Jul 2009 14:57:19 +0200 Subject: PEP 376 In-Reply-To: References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Message-ID: <94bdd2610907030557q79cf7018t77c39beb6f45437b@mail.gmail.com> Ok here's my proposal for the checksum : - I'll add the "hash_type:" suffix in the record file - install will get a new option to define what hash should be used when writing the RECORD file it will default to SHA1 for 2.7/3.2 - pkgutil, that reads the RECORD files, will pick the right hash function by looking at the suffix now for the security it's another story that goes beyond the scope of this PEP notice though, that the PEP provides a place-holder for distributions metadata, so it could host a key later on. On Thu, Jul 2, 2009 at 8:00 PM, Charles Yeomans wrote: > > On Jul 2, 2009, at 1:37 PM, Lie Ryan wrote: > >> Joachim Str?mbergson wrote: >>> >>> Aloha! >>> >>> Tarek Ziad? wrote: >>>> >>>> The prefix is a good idea but since it's just a checksum to control >>>> that the file hasn't changed >>>> what's wrong with using a weak hash algorithm like md5 or now sha1 ? >>> >>> Because it creates a dependency to an old algorithm that should be >>> deprecated. Also using MD5, even for a thing like this might make people >>> belive that it is an ok algorithm to use - "Hey, it is used by the >>> default install in Python, so it must be ok, right?" >>> >>> If we flip the argument around: Why would you want to use MD5 instead of >>> SHA-256? For the specific use case the performance will not (should not) >>> be an issue. >>> >>> As I wrote a few mails ago, it is time to move forward from MD5 and >>> designing something in 2009 that will be around for many years that uses >>> MD5 is (IMHO) a bad design decision. >>> >>>> If someone wants to modify a file of a distribution he can recreate >>>> the checksum as well, >>>> the only secured way to prevent that would be to use gpg keys but >>>> isn't that overkill for what we need ? >>> >>> Actually, adding this type of security would IMHO be a good idea. >>> >> >> Now, are we actually talking about security or checksum? >> >> It has been known for years that MD5 is weak, weak, weak. Not just in >> the recent years. But it doesn't matter since MD5 was never designed for >> security, MD5 was designed to protect against random bits corruption. If >> you want security, look at least to GPG. For data protection against >> intentional, malicious forging, definitely MD5 is the wrong choice. But >> when you just want a simple checksum to ensure that a faulty router >> somewhere in the internet backbone doesn't destroying your data, MD5 is >> a fine algorithm. >> -- > > On the contrary, MD5 was intended to be a cryptographic hash function, not a > checksum. > > Charles Yeomans > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Tarek Ziad? | http://ziade.org From nosklo at gmail.com Fri Jul 3 08:57:35 2009 From: nosklo at gmail.com (Clovis Fabricio) Date: Fri, 3 Jul 2009 09:57:35 -0300 Subject: Python debugger In-Reply-To: <740981.83964.qm@web7901.mail.in.yahoo.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> Message-ID: <9187a60d0907030557q69a2429cn509e3792ed7708be@mail.gmail.com> 2009/7/3 srinivasan srinivas : > Could you suggest some python debuggers? Two graphical debugger frontends: http://www.gnu.org/software/emacs/ http://winpdb.org/ From lie.1296 at gmail.com Fri Jul 3 09:10:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 13:10:34 GMT Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <7xzlbm5ugf.fsf@ruckus.brouhaha.com> <08034bd0-14e5-4d67-aacf-f10988b3be43@z4g2000prh.googlegroups.com> <7xhbxu4del.fsf@ruckus.brouhaha.com> <890bd597-0662-4586-979e-982de0c6f705@v23g2000pro.googlegroups.com> Message-ID: tsangpo wrote: > Just a shorter implementation: > > from itertools import groupby > def split(lst, func): > gs = groupby(lst, func) > return list(gs[True]), list(gs[False]) > As you're replying to my post, I assume you meant a shorter implementation my function. But it doesn't do the same thing. The idea with my group() is similar to what Steven D'Aprano is describing in another branch of this thread (i.e. splitting not only based on True and False, but arbitrary groupings, e.g. 'tru', 'flash' or perhaps -1, 0, 1). For example: >>> def group(seq, func=bool): ... ret = {} ... for item in seq: ... fitem = func(item) ... try: ... ret[fitem].append(item) ... except KeyError: ... ret[fitem] = [item] ... return ret ... >>> group(range(10), lambda x: x%3) {0: [0, 3, 6, 9], 1: [1, 4, 7], 2: [2, 5, 8]} >>> # the next one is the OP's split >>> group(['true', '', [], [''], 'false'], bool) {False: ['', []], True: ['true', [''], 'false']} From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 09:11:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 13:11:00 GMT Subject: Is code duplication allowed in this instance? References: Message-ID: <025df4fd$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 03:46:32 -0700, Klone wrote: > Hi all. I believe in programming there is a common consensus to avoid > code duplication, I suppose such terms like 'DRY' are meant to back this > idea. Anyways, I'm working on a little project and I'm using TDD (still > trying to get a hang of the process) and am trying to test the > functionality within a method. Whoever it so happens to verify the > output from the method I have to employ the same algorithm within the > method to do the verification since there is no way I can determine the > output before hand. > > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). Neither -- that's essentially a pointless test. The only way to *correctly* test a function is to compare the result of that function to an *independent* test. If you test a function against itself, of course it will always pass: def plus_one(x): """Return x plus 1.""" return x-1 # Oops, a bug. # Test it is correct: assert plus_one(5) == plus_one(5) The only general advice I can give is: (1) Think very hard about finding an alternative algorithm to calculate the same result. There usually will be one. (2) If there's not, at least come up with an alternative implementation. It doesn't need to be particularly efficient, because it will only be called for testing. A rather silly example: def plus_one_testing(x): """Return x plus 1 using a different algorithm for testing.""" if type(x) in (int, long): temp = 1 for i in range(x-1): temp += 1 return temp else: floating_part = x - int(x) return floating_part + plus_one_testing(int(x)) (The only problem is, if a test fails, you may not be sure whether it's because your test function is wrong or your production function.) (3) Often you can check a few results by hand. Even if it takes you fifteen minutes, at least that gives you one good test. If need be, get a colleague to check your results. (4) Test failure modes. It might be really hard to calculate func(arg) independently for all possible arguments, but if you know that func(obj) should fail, at least you can test that. E.g. it's hard to test whether or not you've downloaded the contents of a URL correctly without actually downloading it, but you know that http://example.com/ should fail because that domain doesn't exist. (5) Test the consequences of your function rather than the exact results. E.g. if it's too difficult to calculate plus_one(x) independently: assert plus_one(x) > x # except for x = inf or -inf assert plus_one( -plus_one(x) ) == x # -(x+1)+1 = x (6) While complete test coverage is the ideal you aspire to, any tests are better than no tests. But they have to be good tests to be useful. Even *one* test is better than no tests. Hope this helps. -- Steven From lie.1296 at gmail.com Fri Jul 3 09:16:00 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 13:16:00 GMT Subject: stringio+tarfile In-Reply-To: <4a4de9a8$0$1105$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4de9a8$0$1105$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: >> First problem I see is all those numbers before the lines. That's not >> valid python. >> >> Assuming that was a transcription error > > not so. i intentionally add linenumbers to facilitare reference to the > code, but if it is a nuisance i will not include them anymore. The line numbering makes it impossible for others to copy and paste to the interactive interpreter to see the problem code running, and makes it hard to copy and paste into a script file. For code reference, quoting the problematic code is usually much easier. From ronn.ross at gmail.com Fri Jul 3 09:26:52 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Fri, 3 Jul 2009 09:26:52 -0400 Subject: VirtualEnv Message-ID: <9c8c445f0907030626i62d912e6k6d92247a428145e0@mail.gmail.com> I'm attempting to write a bootstrap script for virtualenv. I just want to do a couple of easy_install's after the environment is created. It was fairly easy to create the script, but I can't figure out how to implement it. The documentation was not of much help. Can someone please point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From no.email at please.post Fri Jul 3 09:38:20 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 13:38:20 +0000 (UTC) Subject: Why re.match()? References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: In <025db0a6$0$20657$c3e8da3 at news.astraweb.com> Steven D'Aprano writes: >On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: >> If the concern is efficiency for such cases, then simply implement >> optional offset and length parameters for re.search(), to specify any >> arbitrary substring to apply the search to. To have a special-case >> re.match() method in addition to a general re.search() method is >> antithetical to language minimalism, and plain-old bizarre. Maybe >> there's a really good reason for it, but it has not been mentioned yet. >There is, and it has. I "misspoke" earlier. I should have written "I'm *sure* there's a really good reason for it." And I think no one in this thread (myself included, of course) has a clue of what it is. I miss the days when Guido still posted to comp.lang.python. He'd know. Regarding the "practicality beats purity" line, it's hard to think of a better motto for *Perl*, with all its practicality-oriented special doodads. (And yes, I know where the "practicality beats purity" line comes from.) Even *Perl* does not have a special syntax for the task that re.match is supposedly tailor-made for, according to the replies I've received. Given that it is so trivial to implement all of re.match's functionality with only one additional optional parameter for re.search (i.e. pos), it is absurd to claim that re.match is necessary for the sake of this special functionality. The justification for re.match must be elsewhere. But thanks for letting me know that I'm entitled to my opinion. That's a huge relief. kj From python at mrabarnett.plus.com Fri Jul 3 09:53:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 14:53:42 +0100 Subject: Why re.match()? In-Reply-To: References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4A4E0D66.6070704@mrabarnett.plus.com> kj wrote: > In <025db0a6$0$20657$c3e8da3 at news.astraweb.com> Steven D'Aprano writes: > >> On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: > >>> If the concern is efficiency for such cases, then simply implement >>> optional offset and length parameters for re.search(), to specify any >>> arbitrary substring to apply the search to. To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, and plain-old bizarre. Maybe >>> there's a really good reason for it, but it has not been mentioned yet. > >> There is, and it has. > > I "misspoke" earlier. I should have written "I'm *sure* there's > a really good reason for it." And I think no one in this thread > (myself included, of course) has a clue of what it is. I miss the > days when Guido still posted to comp.lang.python. He'd know. > > Regarding the "practicality beats purity" line, it's hard to think > of a better motto for *Perl*, with all its practicality-oriented > special doodads. (And yes, I know where the "practicality beats > purity" line comes from.) Even *Perl* does not have a special > syntax for the task that re.match is supposedly tailor-made for, > according to the replies I've received. Given that it is so trivial > to implement all of re.match's functionality with only one additional > optional parameter for re.search (i.e. pos), it is absurd to claim > that re.match is necessary for the sake of this special functionality. > The justification for re.match must be elsewhere. > > But thanks for letting me know that I'm entitled to my opinion. > That's a huge relief. > As I wrote, re.match anchors the match whereas re.search doesn't. An alternative would have been to implement Perl's \G anchor, but I believe that that was invented after the re module was written. From no.email at please.post Fri Jul 3 10:05:08 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 14:05:08 +0000 (UTC) Subject: Clarity vs. code reuse/generality Message-ID: I'm will be teaching a programming class to novices, and I've run into a clear conflict between two of the principles I'd like to teach: code clarity vs. code reuse. I'd love your opinion about it. The context is the concept of a binary search. In one of their homeworks, my students will have two occasions to use a binary search. This seemed like a perfect opportunity to illustrate the idea of abstracting commonalities of code into a re-usable function. So I thought that I'd code a helper function, called _binary_search, that took five parameters: a lower limit, an upper limit, a one-parameter function, a target value, and a tolerance (epsilon). It returns the value of the parameter for which the value of the passed function is within the tolerance of the target value. This seemed straightforward enough, until I realized that, to be useful to my students in their homework, this _binary_search function had to handle the case in which the passed function was monotonically decreasing in the specified interval... The implementation is still very simple, but maybe not very clear, particularly to programming novices (docstring omitted): def _binary_search(lo, hi, func, target, epsilon): assert lo < hi assert epsilon > 0 sense = cmp(func(hi), func(lo)) if sense == 0: return None target_plus = sense * target + epsilon target_minus = sense * target - epsilon while True: param = (lo + hi) * 0.5 value = sense * func(param) if value > target_plus: hi = param elif value < target_minus: lo = param else: return param if lo == hi: return None My question is: is the business with sense and cmp too "clever"? Here's the rub: the code above is more general (hence more reusable) by virtue of this trick with the sense parameter, but it is also a bit harder to understand. This not an unusual situation. I find that the processing of abstracting out common logic often results in code that is harder to read, at least for the uninitiated... I'd love to know your opinions on this. TIA! kj From esj at harvee.org Fri Jul 3 10:19:10 2009 From: esj at harvee.org (Eric S. Johansson) Date: Fri, 03 Jul 2009 10:19:10 -0400 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> Message-ID: <4A4E135E.5090400@harvee.org> Horace Blegg wrote: > I've been kinda following this. I have a cousin who is permanently wheel > chair bound and doesn't have perfect control of her hands, but still > manages to use a computer and interact with society. However, the > idea/thought of disabled programmers was new to me/hadn't ever occurred > to me. > > You say that using your hands is painful, but what about your feet? > Wouldn't it be possible to rig up some kind of foot peddle for > shift/caps lock? Kinda like the power peddle used with sowing machines, > so the hands are free to hold fabric. > > I don't mean this in a condescending manor, and I apologize if you take > it as such. I'm genuinely curious if you think something like this could > work. > > The way I was envisioning it working last night (and I haven't the > faintest clue how SR works, nor have I ever used SR) was that you would > hit the foot peddle, which would tell the SR program to capitalize the > first letter of the next word (a smart shift, basically, so you don't > end up doing something like ... WONderland -or- "stocks are up 1,0))% > TOday".) > > Possible? Stupid? > it's not stupid. People have used foot pedals for decades for a variety of controls. I don't think foot pedals would work for me because when I am dictating, I pace. Standing, sitting, I pace. With a cord headset, I'm forced to stay within about 4 feet of the computer. But what I'm using a Bluetooth headset, I will sometimes ramble as far as 10 or 15 feet from the computer. It helps if I make the font larger so I can glance over and see what kind of errors I've gotten. I really love a Bluetooth headset with speech recognition. It's so liberating. Your question about foot pedals makes me think of alternative. would it make sense to have a handheld keyboard which would be used for command-and-control functionality or as an adjunct to speech recognition use? It would have to be designed in such a way that it doesn't aggravate a hand injury which may not be possible. Anyway, just thinking out loud. From aahz at pythoncraft.com Fri Jul 3 10:31:36 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 07:31:36 -0700 Subject: Why re.match()? References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: In article , kj wrote: >In <025db0a6$0$20657$c3e8da3 at news.astraweb.com> Steven D'Aprano writes: >>On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: >>> >>> If the concern is efficiency for such cases, then simply implement >>> optional offset and length parameters for re.search(), to specify any >>> arbitrary substring to apply the search to. To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, and plain-old bizarre. Maybe >>> there's a really good reason for it, but it has not been mentioned yet. >> >>There is, and it has. > >I "misspoke" earlier. I should have written "I'm *sure* there's >a really good reason for it." And I think no one in this thread >(myself included, of course) has a clue of what it is. I miss the >days when Guido still posted to comp.lang.python. He'd know. You may find this enlightening: http://www.python.org/doc/1.4/lib/node52.html -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 10:33:12 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 14:33:12 GMT Subject: Clarity vs. code reuse/generality References: Message-ID: <025e0841$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 14:05:08 +0000, kj wrote: > ... I find that the processing of > abstracting out common logic often results in code that is harder to > read ... Yes. There is often a conflict between increasing abstraction and ease of understanding. [...] > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 You're using assert for data sanitation. That is a very bad habit for you to be teaching novices. Your code will fail to behave as expected whenever the caller runs Python with the -O switch. [...] > My question is: is the business with sense and cmp too "clever"? For production code? Maybe -- you would need to profile the code, and compare it to a less general, more simple-minded function, and see which performs better. If there is an excessive performance penalty, that's a good sign that perhaps you're being too clever, too general, or too abstract -- or all three. Too clever for novices? That depends on how inexperienced they are -- are they new to Python or new to programming in general? Are they children or adults? Do they have a PhD degree or are they still at primary school? It depends on their experience and their intelligence -- dare I say it, some people will *never* get programming, let alone code-reuse. It also depends on how you lead up to it -- are you dropping them straight into the abstract code, or giving them two pieces of nearly the same code and showing them how to pull out the common functionality? -- Steven From bearophileHUGS at lycos.com Fri Jul 3 10:34:23 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 3 Jul 2009 07:34:23 -0700 (PDT) Subject: Is code duplication allowed in this instance? References: Message-ID: <4d99b710-45f1-42dc-b80c-1894af26db42@p29g2000yqh.googlegroups.com> Francesco Bochicchio: > Possibly to prepare the test data set you might need a > different - and already proven - implementation of > the algorithm. Usually a brute force or slow but short algorithm is OK (beside some hard-coded input-output pairs). Sometimes you may use the first implementation of your code that's usually simpler, before becoming complex because of successive optimizations. But you have to be careful, because the first implementation too may be buggy. Some other times there are simpler algorithms to test if the output of another algorithm is correct (think of exponential algorithms with a polinomial test of the correct result), for example to test a fancy Introsort implementation you can use a very small and simple O(n^2) sorter, or better a simple linear loop that tests the result to be sorted. Here, beside unit tests, are also useful languages (or tools) that allow to add pre- and post-conditions, class invariants, etc. Bye, bearophile From alan.isaac at gmail.com Fri Jul 3 10:34:58 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 14:34:58 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On 7/3/2009 10:05 AM kj apparently wrote: > The context is the concept of a binary search. In one of their > homeworks, my students will have two occasions to use a binary > search. This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. > > This seemed straightforward enough, until I realized that, to be > useful to my students in their homework, this _binary_search function > had to handle the case in which the passed function was monotonically > decreasing in the specified interval... > > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > while True: > param = (lo + hi) * 0.5 > value = sense * func(param) > if value > target_plus: > hi = param > elif value < target_minus: > lo = param > else: > return param > > if lo == hi: > return None 1. Don't use assertions to test argument values! 2. from scipy.optimize import bisect def _binary_search(lo, hi, func, target, epsilon): def f(x): return func(x) - target return bisect(f, lo, high, xtol=epsilon) 3. If you don't want to use SciPy (why?), have them implement http://en.wikipedia.org/wiki/Bisection_method#Pseudo-code to produce their own `bisect` function. hth, Alan Isaac From aahz at pythoncraft.com Fri Jul 3 10:36:38 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 07:36:38 -0700 Subject: Clarity vs. code reuse/generality References: Message-ID: In article , kj wrote: > >This seemed straightforward enough, until I realized that, to be >useful to my students in their homework, this _binary_search function >had to handle the case in which the passed function was monotonically >decreasing in the specified interval... > >def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > while True: > param = (lo + hi) * 0.5 > value = sense * func(param) > if value > target_plus: > hi = param > elif value < target_minus: > lo = param > else: > return param > > if lo == hi: > return None > >My question is: is the business with sense and cmp too "clever"? First of all, cmp() is gone in Python 3, unfortunately, so I'd avoid using it. Second, assuming I understand your code correctly, I'd change "sense" to "direction" or "order". -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From timinganalyzer at gmail.com Fri Jul 3 10:42:12 2009 From: timinganalyzer at gmail.com (chewie) Date: Fri, 3 Jul 2009 07:42:12 -0700 (PDT) Subject: The TimingAnalyzer -- project to convert from Java to Python Message-ID: <368b6501-5f01-4a96-9e31-274601878464@c36g2000yqn.googlegroups.com> ?Hello, This a project related to the development of an EDA CAD tool program called the TimingAnalyzer. Digital engineers could use this kind of program to analyze and document inteface timing diagrams for IC, ASIC, FPGA, and board level hardware projects. The TimingAnalyzer is licensed as freeware. I don't have the time needed to make a high quality commercial product but I do want to keep the development moving forward and continue to fix problems and add new features as time permits. www.timing-diagrams.com Recently, I have become very interested in Python and using it to develop similar type cad programs. My plan is to convert the TimingAnalyzer Java to Python with mostly a scripting interface for building complex timing diagrams, doing timing analysis, creating testbenches and testvectors from waveform diagrams, and creating timing diagrams from simulation VCD files. Most all of this is text based work anyway. Developing professional GUIs is very time consuming for me. This has been my bottleneck with the program all along. With a command line interface, you will execute a script and in one window, and view and edit and print the timing diagram shown in another window. Like the Matlab interface. For example: micro = m68000() micro.write(add, data, wait_states) micro.read(add, wait_states). or add_clock(......) add_signal(.....) add_delay(......) add_constraint(.....) add_or_gate(....) add_and_gate(....) add_counter(....) add_clock_jitter(.....) analyze_clock_domains(.....) analyze_worst_case_timings(....) analyze_best_case_timings. read_vcd(....) vcd_2_timing_diagram(.....) create_testvectors(.....) create_testbench(....) A lot of these functions are built into the program now so its a matter of converting them java to python. I won't have to spend most of the time getting the user interface to look good and be friendly. If this is made an open source project, I would hope that others would help with the development and new features and bug fixes will so progress will be made quickly. If anyone is interested in helping with the development, I will make this an open source project. Just let me know if your interested. Thank you, Dan Fabrizio From bearophileHUGS at lycos.com Fri Jul 3 10:48:57 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 3 Jul 2009 07:48:57 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: Message-ID: <5461f308-3b3b-4baf-9ef0-c1a94d059119@y17g2000yqn.googlegroups.com> On 3 Lug, 16:05, kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. They are both important principles, but clarity is usually more important because short code that can't be read can't be fixed and modified, while long code that can be read is alive still. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). Five arguments are a bit too much, even for non-novices. 2-3 arguments are more than enough for novices. Where are doctests'? Novices have to learn to think of tests as part of the normal code :-) I think the main problem in all this discussion is that generally to learn to program you have to write code yourself, so your students have to invent and write their own binary search. Reading your code they are going to learn much less. Creating a binary search from almost scratch can turn out to be too much hard for them, it means you have to ask them to invent something simpler :-) Programming is (among other things) problem solving, so they have to learn to solve not easy problems from the beginning. Showing them famous algorithms (and very good code to copy from) can be useful, but it's less important than learning basic problem solving skills, and much less important than learning why solving problems has to became their purpose and pleasure :-) Bye, bearophile From python at mrabarnett.plus.com Fri Jul 3 10:52:31 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 15:52:31 +0100 Subject: pep 8 constants In-Reply-To: <4A4E135E.5090400@harvee.org> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> Message-ID: <4A4E1B2F.2000700@mrabarnett.plus.com> Eric S. Johansson wrote: > Horace Blegg wrote: >> I've been kinda following this. I have a cousin who is permanently wheel >> chair bound and doesn't have perfect control of her hands, but still >> manages to use a computer and interact with society. However, the >> idea/thought of disabled programmers was new to me/hadn't ever occurred >> to me. >> >> You say that using your hands is painful, but what about your feet? >> Wouldn't it be possible to rig up some kind of foot peddle for >> shift/caps lock? Kinda like the power peddle used with sowing machines, >> so the hands are free to hold fabric. >> >> I don't mean this in a condescending manor, and I apologize if you take >> it as such. I'm genuinely curious if you think something like this could >> work. >> >> The way I was envisioning it working last night (and I haven't the >> faintest clue how SR works, nor have I ever used SR) was that you would >> hit the foot peddle, which would tell the SR program to capitalize the >> first letter of the next word (a smart shift, basically, so you don't >> end up doing something like ... WONderland -or- "stocks are up 1,0))% >> TOday".) >> >> Possible? Stupid? >> > it's not stupid. > > People have used foot pedals for decades for a variety of controls. I don't > think foot pedals would work for me because when I am dictating, I pace. > Standing, sitting, I pace. With a cord headset, I'm forced to stay within about > 4 feet of the computer. But what I'm using a Bluetooth headset, I will sometimes > ramble as far as 10 or 15 feet from the computer. It helps if I make the font > larger so I can glance over and see what kind of errors I've gotten. > > I really love a Bluetooth headset with speech recognition. It's so liberating. > > Your question about foot pedals makes me think of alternative. would it make > sense to have a handheld keyboard which would be used for command-and-control > functionality or as an adjunct to speech recognition use? It would have to be > designed in such a way that it doesn't aggravate a hand injury which may not be > possible. Anyway, just thinking out loud. > You can get giant piano keyboards that you step on, so how about a giant computer keyboard? "I wrote 5 miles of code before lunch!" :-) From pdmef at gmx.net Fri Jul 3 10:54:26 2009 From: pdmef at gmx.net (Rocco Rutte) Date: Fri, 3 Jul 2009 16:54:26 +0200 Subject: Custom CFLAGS with distutils Message-ID: <20090703145426.GJ316@andreas.daprodeges.fqdn.th-h.de> Hi, I've been trying to make distutils build mercurial with custom cflags. The only way this works is to change Makefile because I don't want to put my changed CFLAGS into the environment and I tend to forget to run "make" with a CFLAGS=... option. Google brings up a special "Setup" file which should solve my problem, but it somehow doesn't. I've tried: mercurial mercurial/base85.c -Xcompiler -arch x86_64 mercurial.base85 mercurial/base85.c -Xcompiler -arch x86_64 for base85.c in the mercurial/ subdirectory. Hacking Makefile does the trick, but having a Setup file working would never produce merge conflicts. What am I doing wrong? Rocco From metolone+gmane at gmail.com Fri Jul 3 10:58:35 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 3 Jul 2009 07:58:35 -0700 Subject: =?utf-8?Q?Re:_getting_rid_of_=E2=80=94?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> Message-ID: "Tep" wrote in message news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... > On 3 Jul., 06:40, Simon Forman wrote: > > On Jul 2, 4:31 am, Tep wrote: [snip] > > > > > > how can I replace '?' sign from string? Or do split at that > > > > > > character? > > > > > > Getting unicode error if I try to do it: > > > > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in > > > > > > position > > > > > > 1: ordinal not in range(128) > > > > > > > > Thanks, Pet > > > > > > > > script is # -*- coding: UTF-8 -*- [snip] > > I just tried a bit of your code above in my interpreter here and it > > worked fine: > > > > |>>> data = 'foo ? bar' > > |>>> data.split('?') > > |['foo ', ' bar'] > > |>>> data = u'foo ? bar' > |>>> data.split(u'?') > > |[u'foo ', u' bar'] > > > > Figure out the smallest piece of "html source code" that causes the > > problem and include that with your next post. > > The problem was, I've converted "html source code" to unicode object > and didn't encoded to utf-8 back, before using split... > Thanks for help and sorry for not so smart question > Pet You'd still benefit from posting some code. You shouldn't be converting back to utf-8 to do a split, you should be using a Unicode string with split on the Unicode version of the "html source code". Also make sure your file is actually saved in the encoding you declare. I print the encoding of your symbol in two encodings to illustrate why I suspect this. Below, assume "data" is your "html source code" as a Unicode string: # -*- coding: UTF-8 -*- data = u'foo ? bar' print repr(u'?'.encode('utf-8')) print repr(u'?'.encode('windows-1252')) print data.split(u'?') print data.split('?') OUTPUT: '\xe2\x80\x94' '\x97' [u'foo ', u' bar'] Traceback (most recent call last): File "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 427, in ImportFile exec codeObj in __main__.__dict__ File "", line 1, in File "x.py", line 6, in print data.split('?') UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) Note that using the Unicode string in split() works. Also note the decode byte in the error message when using a non-Unicode string to split the Unicode data. In your original error message the decode byte that caused an error was 0x97, which is 'EM DASH' in Windows-1252 encoding. Make sure to save your source code in the encoding you declare. If I save the above script in windows-1252 encoding and change the coding line to windows-1252 I get the same results, but the decode byte is 0x97. # coding: windows-1252 data = u'foo ? bar' print repr(u'?'.encode('utf-8')) print repr(u'?'.encode('windows-1252')) print data.split(u'?') print data.split('?') '\xe2\x80\x94' '\x97' [u'foo ', u' bar'] Traceback (most recent call last): File "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 427, in ImportFile exec codeObj in __main__.__dict__ File "", line 1, in File "x.py", line 6, in print data.split('?) UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: ordinal not in range(128) -Mark From nhuthuy.huynh at gmail.com Fri Jul 3 11:18:58 2009 From: nhuthuy.huynh at gmail.com (Huynh Nhu Thuy) Date: Fri, 3 Jul 2009 08:18:58 -0700 (PDT) Subject: Video shows vigorous Jackson before death??? Message-ID: <333e0920-4dd9-4ab3-8e92-8dcfb207fc65@x5g2000prf.googlegroups.com> Video shows vigorous Jackson before death??? http://hd-family.blogspot.com/2009/07/video-shows-vigorous-jackson-before.html LOS ANGELES (AFP) ? A video released Thursday showed Michael Jackson vigorously practicing a song-and-dance routine days before his death, supporting accounts he had been in good health. In footage obtained by AFP, the pop legend performed at the Staples Center in Los Angeles on June 23, two days before he died, as he prepared for a 50-date set in London starting in July. http://hd-family.blogspot.com/2009/07/video-shows-vigorous-jackson-before.html From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 11:19:34 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 17:19:34 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <4a4e216e$0$7801$426a74cc@news.free.fr> kj a ?crit : > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. I'd love your opinion about > it. (snip - others already commented on this code) > Here's the rub: the code above is more general (hence more reusable) > by virtue of this trick with the sense parameter, but it is also > a bit harder to understand. Perhaps better naming (s/sense/direction/g ?) and a small comment could help ? > This not an unusual situation. I find that the processing of > abstracting out common logic often results in code that is harder > to read, at least for the uninitiated... IOW : the notion of "clarity" depends on the context (including the reader). Anyway, there are algorithms (or implementations of...) that are definitly and inherently on the 'hard to read' side - well, complexity is something you have to learn to live with, period. The key is to try and make it as simple and readable *as possible*. Also, factoring out common code - or even slightly complex code - often makes _client_ code (much) more readable. From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 3 11:22:40 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 03 Jul 2009 17:22:40 +0200 Subject: Why re.match()? In-Reply-To: References: Message-ID: <4a4e2227$0$7801$426a74cc@news.free.fr> kj a ?crit : (snipo > To have a special-case > re.match() method in addition to a general re.search() method is > antithetical to language minimalism, FWIW, Python has no pretention to minimalism. From Scott.Daniels at Acm.Org Fri Jul 3 11:43:47 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 03 Jul 2009 08:43:47 -0700 Subject: Direct interaction with subprocess - the curse of blocking I/O In-Reply-To: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> References: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> Message-ID: yam850 wrote: > I made a python method/function for non blocking read from a file > object.... I am happy to see comments. OK, here's a fairly careful set of comments with a few caveats: Does this work on windows? The top comment should say where you know it works. Does this code correctly read a single line? perhaps you need to check for a final '\n' whenever you actually get characters, and break out there. The rest below simply is style-changing suggestions; take what you like and leave the rest. > > def non_blocking_readline(f_read=sys.stdin, timeout_select=0.0): > """to readline non blocking from the file object 'f_read' > for 'timeout_select' see module 'select'""" > import select Typically put imports at the top of the source XXX text_lines = '' # empty string Either no comment here or say _why_ it is empty. (A) text_lines = [] # for result accumulation if collecting pieces (see below) (B) text_lines = '' # for result accumulation if collecting a single string > while True: # as long as there are bytes to read > try: # try select > rlist, wlist, xlist = select.select([f_read], [], [], > timeout_select) XXX except: # select ERROR XXX print >>sys.stderr, ("non_blocking_read select ERROR") I'd not hide the details of the exception like that. Don't do empty excepts. Either don't catch it at all here, (I prefer that) or do something like the following to capture the error: except Exception, why: print >>sys.stderr, "non_blocking_read select: %s" % why > break XXX if DEBUG: print("rlist=%s, wlist=%s, xlist=%s" % (repr(rlist), XXX repr(wlist), repr(xlist))) Don't be scared of vertical space; if you must, define a function DPRINT to ignore args; use %r to get repr So, either: if DEBUG: print("rlist=%r, wlist=%r, xlist=%r" % ( rlist, wlist, xlist)) or: elsewhere: def DPRINT(format_str, *args, **kwargs): '''Conditionally print formatted output based on DEBUG''' if DEBUG: print(format_str % (args or kwargs)) and then here: DPRINT("rlist=%r, wlist=%r, xlist=%r", rlist, wlist, xlist) XXX if len(rlist) > 0: Idiomatic Python: use the fact that empty containers evaluate false: if rlist: > text_read = f_read.readline() # get a line XXX if DEBUG: print("after read/readline text_read:'%s', len= > %s" % (text_read, repr(len(text_read)))) Similar comment to above: if DEBUG: print("after read/readline text_read:%r, len=%s" % ( text_read, len(text_read)) or: DPRINT("after read/readline text_read:%r, len=%s", text_read, len(text_read)) XXX if len(text_read) > 0: # there were some bytes if text_read: # there were some bytes XXX text_lines = "".join([text_lines, text_read]) Note the join is a good idea only if you have several elements. For a single concatenation, "=" is just fine. The (A) case combines at the end, the (B) case if you expect multiple concatenates are rare. (A) text_lines.append(text_read) (B) text_lines += text_read XXX if DEBUG: print("text_lines:'%s'" % (text_lines)) Similar to above if DEBUG: print("text_lines:%r" % text_lines) or: DPRINT("text_lines:%r", text_lines) XXX else: XXX break # there was no byte in a line XXX else: XXX break # there was no byte in the f_read To make the flow of control above more clear (one case continues, others get out), I'd also replace the above with: continue # In one case we keep going break # No new chars found, let's get out. XXX if text_lines == '': XXX return None XXX else: XXX return text_lines Simplify using 'or's semantics: (A) return ''.join(text_lines) or None (B) return text_lines or None So, with everything applied: import select def DPRINT(format_str, *args, **kwargs): '''Conditionally print formatted output based on DEBUG''' if DEBUG: print(format_str % (args or kwargs)) def non_blocking_readline(f_read=sys.stdin, timeout_select=0.0): """to readline non blocking from the file object 'f_read' for 'timeout_select' see module 'select' """ text_lines = '' # for result accumulation while True: # as long as there are bytes to read rlist, wlist, xlist = select.select([f_read], [], [], timeout_select) DPRINT("rlist=%r, wlist=%r, xlist=%r", rlist, wlist, xlist) if rlist: text_read = f_read.readline() # get a line DPRINT("after read/readline text_read:%r, len=%s", text_read, len(text_read)) if text_read: # there were some bytes text_lines += text_read DPRINT("text_lines:%r", text_lines) continue # Got some chars, keep going. break # Nothing new found, let's get out. return text_lines or None --Scott David Daniels Scott.Daniels at Acm.Org From jeanmichel at sequans.com Fri Jul 3 11:58:48 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 03 Jul 2009 17:58:48 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <4A4E2AB8.6060808@sequans.com> kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. I'd love your opinion about > it. > [...] > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None My suggestion on how to improve this part for python novices: # assuming func is monotonous if func(high) > func(low): direction = 1 # aka sign of the func derivative elif func(low) > func(high): direction = -1 else: return None Avoid using cmp, this will prevent the "what the hell is cmp doing ?" , unless you want to teach your students how to search for inline python documentation. Some other list members have suggested to improve the variable naming. I couldn't agree more, in your case, I think clarity can be achieved as well with the abstraction (these notions does not necessarily collide). Here's a link on my how-to-name bible : http://tottinge.blogsome.com/meaningfulnames/ Jean-Michel From kurt.alfred.mueller at gmail.com Fri Jul 3 11:59:24 2009 From: kurt.alfred.mueller at gmail.com (yam850) Date: Fri, 3 Jul 2009 08:59:24 -0700 (PDT) Subject: Direct interaction with subprocess - the curse of blocking I/O References: <58d4c4c9-9873-499b-a9cc-1692de67893d@d32g2000yqh.googlegroups.com> Message-ID: <8a974010-e0c8-470d-b6a8-9934cf61a377@18g2000yqa.googlegroups.com> On 3 Jul., 17:43, Scott David Daniels wrote: > yam850 wrote: > > I made a python method/function for nonblockingread from a file > > object.... I am happy to see comments. > OK, here's a fairly careful set of comments with a few caveats: [snip] valuable comments > --Scott David Daniels > Scott.Dani... at Acm.Org Wow, thats a GREAT answer!!! Thanks!!! I will learn a lot while "digesting" this mail. G -- yam850 From lie.1296 at gmail.com Fri Jul 3 12:10:42 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 16:10:42 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <64q3m.2408$ze1.1228@news-server.bigpond.net.au> kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. I'd love your opinion about > it. Sometimes when the decision between clarity and generality becomes too hard; you might fare better to save the code, go out for a walk to forget the code, and start a blank text editor. Being in a fresh mind, you may found an alternative approach, e.g.: from __future__ import division def binary_search(lo, hi, func, target, epsilon): # reverses hi and lo if monotonically decreasing lo, hi = (lo, hi) if func(hi) > func(lo) else (hi, lo) param = (lo + hi) / 2 # loop while not precise enough while abs(func(param) - target) > epsilon: param = (lo + hi) / 2 if target < func(param): hi = param else: lo = param return param > The context is the concept of a binary search. In one of their > homeworks, my students will have two occasions to use a binary > search. This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. > > This seemed straightforward enough, until I realized that, to be > useful to my students in their homework, this _binary_search function > had to handle the case in which the passed function was monotonically > decreasing in the specified interval... > > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > assert lo < hi > assert epsilon > 0 > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > while True: > param = (lo + hi) * 0.5 > value = sense * func(param) > if value > target_plus: > hi = param > elif value < target_minus: > lo = param > else: > return param > > if lo == hi: > return None > > My question is: is the business with sense and cmp too "clever"? > > Here's the rub: the code above is more general (hence more reusable) > by virtue of this trick with the sense parameter, but it is also > a bit harder to understand. > > This not an unusual situation. I find that the processing of > abstracting out common logic often results in code that is harder > to read, at least for the uninitiated... > > I'd love to know your opinions on this. > > TIA! > > kj > From no.email at please.post Fri Jul 3 12:19:22 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 16:19:22 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Alan G Isaac writes: >1. Don't use assertions to test argument values! Out of curiosity, where does this come from? Thanks, kj From no.email at please.post Fri Jul 3 12:20:33 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 16:20:33 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In aahz at pythoncraft.com (Aahz) writes: >First of all, cmp() is gone in Python 3, unfortunately, so I'd avoid >using it. Good to know. >Second, assuming I understand your code correctly, I'd change >"sense" to "direction" or "order". Thanks, kj From petshmidt at googlemail.com Fri Jul 3 12:24:43 2009 From: petshmidt at googlemail.com (Tep) Date: Fri, 3 Jul 2009 09:24:43 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> Message-ID: <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> On 3 Jul., 16:58, "Mark Tolonen" wrote: > "Tep" wrote in message > > news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... > > > > > > > On 3 Jul., 06:40, Simon Forman wrote: > > > On Jul 2, 4:31 am, Tep wrote: > [snip] > > > > > > > how can I replace '?' sign from string? Or do split at that > > > > > > > character? > > > > > > > Getting unicode error if I try to do it: > > > > > > > > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in > > > > > > > position > > > > > > > 1: ordinal not in range(128) > > > > > > > > Thanks, Pet > > > > > > > > script is # -*- coding: UTF-8 -*- > [snip] > > > I just tried a bit of your code above in my interpreter here and it > > > worked fine: > > > > |>>> data = 'foo ? bar' > > > |>>> data.split('?') > > > |['foo ', ' bar'] > > > |>>> data = u'foo ? bar' > > |>>> data.split(u'?') > > > |[u'foo ', u' bar'] > > > > Figure out the smallest piece of "html source code" that causes the > > > problem and include that with your next post. > > > The problem was, I've converted "html source code" to unicode object > > and didn't encoded to utf-8 back, before using split... > > Thanks for help and sorry for not so smart question > > Pet > > You'd still benefit from posting some code. ?You shouldn't be converting I've posted code below > back to utf-8 to do a split, you should be using a Unicode string with split > on the Unicode version of the "html source code". ?Also make sure your file > is actually saved in the encoding you declare. ?I print the encoding of your > symbol in two encodings to illustrate why I suspect this. File was indeed in windows-1252, I've changed this. For errors see below > > Below, assume "data" is your "html source code" as a Unicode string: > > # -*- coding: UTF-8 -*- > data = u'foo ? bar' > print repr(u'?'.encode('utf-8')) > print repr(u'?'.encode('windows-1252')) > print data.split(u'?') > print data.split('?') > > OUTPUT: > > '\xe2\x80\x94' > '\x97' > [u'foo ', u' bar'] > Traceback (most recent call last): > ? File > "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > line 427, in ImportFile > ? ? exec codeObj in __main__.__dict__ > ? File "", line 1, in > ? File "x.py", line 6, in > ? ? print data.split('?') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: > ordinal not in range(128) > > Note that using the Unicode string in split() works. ?Also note the decode > byte in the error message when using a non-Unicode string to split the > Unicode data. ?In your original error message the decode byte that caused an > error was 0x97, which is 'EM DASH' in Windows-1252 encoding. ?Make sure to > save your source code in the encoding you declare. ?If I save the above > script in windows-1252 encoding and change the coding line to windows-1252 I > get the same results, but the decode byte is 0x97. > > # coding: windows-1252 > data = u'foo ? bar' > print repr(u'?'.encode('utf-8')) > print repr(u'?'.encode('windows-1252')) > print data.split(u'?') > print data.split('?') > > '\xe2\x80\x94' > '\x97' > [u'foo ', u' bar'] > Traceback (most recent call last): > ? File > "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > line 427, in ImportFile > ? ? exec codeObj in __main__.__dict__ > ? File "", line 1, in > ? File "x.py", line 6, in > ? ? print data.split('?) > UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: > ordinal not in range(128) > > -Mark #! /usr/bin/python # -*- coding: UTF-8 -*- import urllib2 import re def getTitle(input): title = re.search('(.*?)', input) title = title.group(1) print "FULL TITLE", title.encode('UTF-8') parts = title.split(' ? ') return parts[0] def getWebPage(url): user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } req = urllib2.Request(url, '', headers) response = urllib2.urlopen(req) the_page = unicode(response.read(), 'UTF-8') return the_page def main(): url = "http://bg.wikipedia.org/wiki/ %D0%91%D0%B0%D1%85%D1%80%D0%B5%D0%B9%D0%BD" title = getTitle(getWebPage(url)) print title[0] if __name__ == "__main__": main() Traceback (most recent call last): File "C:\user\Projects\test\src\new_main.py", line 29, in main() File "C:\user\Projects\test\src\new_main.py", line 24, in main title = getTitle(getWebPage(url)) FULL TITLE ?????????????? ??? ?????????????????? File "C:\user\Projects\test\src\new_main.py", line 9, in getTitle parts = title.split(' ??? ') UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1: ordinal not in range(128) From keflavich at gmail.com Fri Jul 3 12:30:21 2009 From: keflavich at gmail.com (Keflavich) Date: Fri, 3 Jul 2009 09:30:21 -0700 (PDT) Subject: Compiling 64 bit python on a mac - cannot compute sizeof (int) Message-ID: I'm trying to compile a 64 bit version of python 2.6.2 on my mac (OS X 10.5.7), and am running into a problem during the configure stage. I configure with: ./configure --enable-framework=/Library/Frameworks --enable- universalsdk MACOSX_DEPLOYMENT_TARGET=10.5 --with-universal-archs=all - with-readline-dir=/usr/local because I want 64 and 32 bit, and I needed to install a 64 bit readline as a prerequisite. configure fails at: checking size of int... configure: error: cannot compute sizeof (int) I'm not reporting this as a bug because I know it's a problem with my path somewhere (a friend with an identical computer but slightly different setup was able to compile without a problem), but I don't know what paths to change. Any tips? Thanks, Adam From no.email at please.post Fri Jul 3 12:30:33 2009 From: no.email at please.post (kj) Date: Fri, 3 Jul 2009 16:30:33 +0000 (UTC) Subject: Why re.match()? References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: In aahz at pythoncraft.com (Aahz) writes: >In article , kj wrote: >You may find this enlightening: >http://www.python.org/doc/1.4/lib/node52.html Indeed. Thank you. kj From http Fri Jul 3 12:33:35 2009 From: http (Paul Rubin) Date: 03 Jul 2009 09:33:35 -0700 Subject: Clarity vs. code reuse/generality References: Message-ID: <7xk52p4tgg.fsf@ruckus.brouhaha.com> kj writes: > sense = cmp(func(hi), func(lo)) > if sense == 0: > return None > target_plus = sense * target + epsilon > target_minus = sense * target - epsilon > ... The code looks confusing to me and in some sense incorrect. Suppose func(hi)==func(lo)==target. In this case the solver gives up and returns None even though it already has found a root. Also, the stuff with the sense parameter, and target_minus and target_plus looks messy. I do like to use cmp. I'd just write something like (untested): def _binary_search(lo, hi, func, target, epsilon): y_hi, y_lo = func(hi), func(lo) while True: x_new = (lo + hi) * 0.5 y_new = func(x_new) if abs(y_new - target) < epsilon: return x_new elif cmp(y_new, target) == cmp(y_hi, target): hi = x_new else: lo = x_new if lo == hi: return None This uses an extra couple function calls in that trivial case, of course. From lie.1296 at gmail.com Fri Jul 3 12:34:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 16:34:49 GMT Subject: Why re.match()? In-Reply-To: <025db0a6$0$20657$c3e8da3@news.astraweb.com> References: <025db0a6$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Thu, 02 Jul 2009 11:19:40 +0000, kj wrote: > >> I'm sure that it is possible to find cases in which the *current* >> implementation of re.search() would be inefficient, but that's because >> this implementation is perverse, which, I guess, is ultimately the point >> of my original post. Why privilege the special case of a >> start-of-string anchor? > > Because wanting to see if a string matches from the beginning is a very > important and common special case. > I find the most oddest thing about re.match is that it have an implicit beginning anchor, but not implicit end anchor. I thought it was much more common to ensure that a string matches a certain pattern, than just matching the beginning. But everyone's mileages vary. From buzzard at urubu.freeserve.co.uk Fri Jul 3 12:35:45 2009 From: buzzard at urubu.freeserve.co.uk (duncan smith) Date: Fri, 03 Jul 2009 17:35:45 +0100 Subject: Is Psyco good for Python 2.6.1? In-Reply-To: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> References: <30535cef-28ee-4674-999b-9d0013e72796@x3g2000yqa.googlegroups.com> Message-ID: Russ P. wrote: > I need to speed up some Python code, and I discovered Psyco. However, > the Psyco web page has not been updated since December 2007. Before I > go to the trouble of installing it, does anyone know if it is still > good for Python 2.6.1? Thanks. If you look at http://psyco.sourceforge.net/ it would seem so (a Windows binary for 2.6 is available). You might want to wait a little while because Psyco 2 is likely to be released very soon (tomorrow, the last I heard). Duncan From hobesh at gmail.com Fri Jul 3 12:53:19 2009 From: hobesh at gmail.com (Zach Hobesh) Date: Fri, 3 Jul 2009 09:53:19 -0700 Subject: Config files with different types In-Reply-To: References: Message-ID: yaml looks pretty interesting. Also, I wouldn't have to change much, I would still use the same function, and still output a dict. Thanks! -Zach On Thu, Jul 2, 2009 at 11:55 PM, Javier Collado wrote: > Hello, > > Have you considered using something that is already developed? > > You could take a look at this presentation for an overview of what's available: > http://us.pycon.org/2009/conference/schedule/event/5/ > > Anyway, let me explain that, since I "discovered" it, my favourite > format for configuration files is yaml (http://yaml.org/, > http://pyyaml.org/). It's easy to read, easy to write, available in > different programming languagues, etc. In addition to this, type > conversion is already in place so I think it covers your requirement. > For example: > > IIn [1]: import yaml > > In [2]: yaml.load("""name: person name > ? ...: age: 25 > ? ...: is_programmer: true""") > Out[2]: {'age': 25, 'is_programmer': True, 'name': 'person name'} > > Best regards, > ? ?Javier > > 2009/7/2 Zach Hobesh : >> Hi all, >> >> I've written a function that reads a specifically formatted text file >> and spits out a dictionary. ?Here's an example: >> >> config.txt: >> >> Destination = C:/Destination >> Overwrite = True >> >> >> Here's my function that takes 1 argument (text file) >> >> the_file = open(textfile,'r') >> linelist = the_file.read().split('\n') >> the_file.close() >> configs = {} >> for line in linelist: >> ? ? ? try: >> ? ? ? ? ? ? ?key,value = line.split('=') >> ? ? ? ? ? ? ?key.strip() >> ? ? ? ? ? ? ?value.strip() >> ? ? ? ? ? ? ?key.lower() >> ? ? ? ? ? ? ?value.lower() >> ? ? ? ? ? ? ?configs[key] = value >> >> ? ? ? except ValueError: >> ? ? ? ? ? ? ?break >> >> so I call this on my config file, and then I can refer back to any >> config in my script like this: >> >> shutil.move(your_file,configs['destination']) >> >> which I like because it's very clear and readable. >> >> So this works great for simple text config files. ?Here's how I want >> to improve it: >> >> I want to be able to look at the value and determine what type it >> SHOULD be. ?Right now, configs['overwrite'] = 'true' (a string) when >> it might be more useful as a boolean. ?Is there a quick way to do >> this? ?I'd also like to able to read '1' as an in, '1.0' as a float, >> etc... >> >> I remember once I saw a script that took a string and tried int(), >> float() wrapped in a try except, but I was wondering if there was a >> more direct way. >> >> Thanks in advance, >> >> Zach >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > From python at mrabarnett.plus.com Fri Jul 3 12:54:10 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 17:54:10 +0100 Subject: getting rid of =?UTF-8?B?4oCU?= In-Reply-To: <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> Message-ID: <4A4E37B2.10300@mrabarnett.plus.com> Tep wrote: > On 3 Jul., 16:58, "Mark Tolonen" wrote: >> "Tep" wrote in message >> >> news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... >> >> >> >> >> >>> On 3 Jul., 06:40, Simon Forman wrote: >>>> On Jul 2, 4:31 am, Tep wrote: >> [snip] >>>>>>>> how can I replace '?' sign from string? Or do split at that >>>>>>>> character? >>>>>>>> Getting unicode error if I try to do it: >>>>>>>> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in >>>>>>>> position >>>>>>>> 1: ordinal not in range(128) >>>>>>>> Thanks, Pet >>>>>>>> script is # -*- coding: UTF-8 -*- >> [snip] >>>> I just tried a bit of your code above in my interpreter here and it >>>> worked fine: >>>> |>>> data = 'foo ? bar' >>>> |>>> data.split('?') >>>> |['foo ', ' bar'] >>>> |>>> data = u'foo ? bar' >>> |>>> data.split(u'?') >>>> |[u'foo ', u' bar'] >>>> Figure out the smallest piece of "html source code" that causes the >>>> problem and include that with your next post. >>> The problem was, I've converted "html source code" to unicode object >>> and didn't encoded to utf-8 back, before using split... >>> Thanks for help and sorry for not so smart question >>> Pet >> You'd still benefit from posting some code. You shouldn't be converting > > I've posted code below > >> back to utf-8 to do a split, you should be using a Unicode string with split >> on the Unicode version of the "html source code". Also make sure your file >> is actually saved in the encoding you declare. I print the encoding of your >> symbol in two encodings to illustrate why I suspect this. > > File was indeed in windows-1252, I've changed this. For errors see > below > >> Below, assume "data" is your "html source code" as a Unicode string: >> >> # -*- coding: UTF-8 -*- >> data = u'foo ? bar' >> print repr(u'?'.encode('utf-8')) >> print repr(u'?'.encode('windows-1252')) >> print data.split(u'?') >> print data.split('?') >> >> OUTPUT: >> >> '\xe2\x80\x94' >> '\x97' >> [u'foo ', u' bar'] >> Traceback (most recent call last): >> File >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", >> line 427, in ImportFile >> exec codeObj in __main__.__dict__ >> File "", line 1, in >> File "x.py", line 6, in >> print data.split('?') >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: >> ordinal not in range(128) >> >> Note that using the Unicode string in split() works. Also note the decode >> byte in the error message when using a non-Unicode string to split the >> Unicode data. In your original error message the decode byte that caused an >> error was 0x97, which is 'EM DASH' in Windows-1252 encoding. Make sure to >> save your source code in the encoding you declare. If I save the above >> script in windows-1252 encoding and change the coding line to windows-1252 I >> get the same results, but the decode byte is 0x97. >> >> # coding: windows-1252 >> data = u'foo ? bar' >> print repr(u'?'.encode('utf-8')) >> print repr(u'?'.encode('windows-1252')) >> print data.split(u'?') >> print data.split('?') >> >> '\xe2\x80\x94' >> '\x97' >> [u'foo ', u' bar'] >> Traceback (most recent call last): >> File >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", >> line 427, in ImportFile >> exec codeObj in __main__.__dict__ >> File "", line 1, in >> File "x.py", line 6, in >> print data.split('?) >> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: >> ordinal not in range(128) >> >> -Mark > > #! /usr/bin/python > # -*- coding: UTF-8 -*- > import urllib2 > import re > def getTitle(input): > title = re.search('(.*?)', input) The input is Unicode, so it's probably better for the regular expression to also be Unicode: title = re.search(u'(.*?)', input) (In the current implementation it actually doesn't matter.) > title = title.group(1) > print "FULL TITLE", title.encode('UTF-8') > parts = title.split(' ? ') The title is Unicode, so the string with which you're splitting should also be Unicode: parts = title.split(u' ? ') > return parts[0] > > > def getWebPage(url): > user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' > headers = { 'User-Agent' : user_agent } > req = urllib2.Request(url, '', headers) > response = urllib2.urlopen(req) > the_page = unicode(response.read(), 'UTF-8') > return the_page > > > def main(): > url = "http://bg.wikipedia.org/wiki/ > %D0%91%D0%B0%D1%85%D1%80%D0%B5%D0%B9%D0%BD" > title = getTitle(getWebPage(url)) > print title[0] > > > if __name__ == "__main__": > main() > > > Traceback (most recent call last): > File "C:\user\Projects\test\src\new_main.py", line 29, in > main() > File "C:\user\Projects\test\src\new_main.py", line 24, in main > title = getTitle(getWebPage(url)) > FULL TITLE ?????????????? ??? ?????????????????? > File "C:\user\Projects\test\src\new_main.py", line 9, in getTitle > parts = title.split(' ??? ') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position > 1: ordinal not in range(128) From kd at kendyck.com Fri Jul 3 13:00:31 2009 From: kd at kendyck.com (Ken Dyck) Date: Fri, 3 Jul 2009 10:00:31 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? References: Message-ID: <08bb0349-4bc7-448c-9ae5-49eca6d7f3ae@g7g2000prg.googlegroups.com> On Jul 2, 6:17?pm, Allen Fowler wrote: > Since I need to work with other platforms, pickle is out... ?what are the alternatives? ?XML? JSON? Don't forget YAML (http://yaml.org). Libraries available for Python and .NET, among others. -Ken From schickb at gmail.com Fri Jul 3 13:03:32 2009 From: schickb at gmail.com (Brad) Date: Fri, 3 Jul 2009 10:03:32 -0700 (PDT) Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: On Jul 3, 12:57?am, Steven D'Aprano wrote: > I've never needed such a split function, and I don't like the name, and > the functionality isn't general enough. I'd prefer something which splits > the input sequence into as many sublists as necessary, according to the > output of the key function. That's not a bad idea, I'll have to experiment with the alternatives. My thought process for this, however, was that filter itself already splits the sequence and it would have been more useful had it not thrown away "half" of what it discovers. It could have been written to returned two sequences with very litter perf hit for all but very large input sequences, and been useful in more situations. What I *really* wanted was a way to make filter itself more useful, since it seems a bit silly to have two very similar functions. Maybe this would be difficult to get into the core, but how about this idea: Rename the current filter function to something like "split" or "partition" (which I agree may be a better name) and modify it to return the desired true and false sequences. Then recreate the existing "filter" function with a wrapper that throws away the false sequence. Here are two simplified re-creations of situations where I could have used partition (aka split): words = ['this', 'is', 'a', 'bunch', 'of', 'words'] short, long = partition(words, lambda w: len(w) < 3) d = {1 : 'w', 2 : 'x' ,3 : 'y' ,4 : 'z'} keys = [1, 3, 4, 9] found, missing = partition(keys, d.has_key) There are probably a dozen other approaches, but the existing "filter" is fast, clear, and *almost* good enough. So when is this useful in general: Whenever filter itself is useful, but you want to use both sides of the partitioning work it already does. -Brad From http Fri Jul 3 13:14:00 2009 From: http (Paul Rubin) Date: 03 Jul 2009 10:14:00 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xr5wx8zaf.fsf@ruckus.brouhaha.com> Brad writes: > Maybe this would be difficult to get into the core, but how about this > idea: Rename the current filter function to something like "split" or > "partition" (which I agree may be a better name) and modify it to > return the desired true and false sequences. Then recreate the > existing "filter" function with a wrapper that throws away the false > sequence. This isn't so attractive, since filter takes a sequence input but returns a list. So if I have an iterator that produces a billion elements of which I expect three to satisfy some predicate, then xs = filter(func, seq) as currently implemented will build a 3-element list and return it. Under your suggestion, it would also build and throw away an (almost) billion element list. From lie.1296 at gmail.com Fri Jul 3 13:27:59 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 03 Jul 2009 17:27:59 GMT Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: Brad wrote: > On Jul 3, 12:57 am, Steven D'Aprano cybersource.com.au> wrote: >> I've never needed such a split function, and I don't like the name, and >> the functionality isn't general enough. I'd prefer something which splits >> the input sequence into as many sublists as necessary, according to the >> output of the key function. > > That's not a bad idea, I'll have to experiment with the alternatives. > My thought process for this, however, was that filter itself already > splits the sequence and it would have been more useful had it not > thrown away "half" of what it discovers. It could have been written to > returned two sequences with very litter perf hit for all but very > large input sequences, and been useful in more situations. What I > *really* wanted was a way to make filter itself more useful, since it > seems a bit silly to have two very similar functions. It's not that easy. filter is nearly by definition a lazy function. The various split/group functions is impossible to be made an efficient iterator, since they must traverse the whole list before being sure that they have finished the group/part of the split (or even to be sure that they have all the group keys). > Maybe this would be difficult to get into the core, but how about this > idea: Rename the current filter function to something like "split" or > "partition" (which I agree may be a better name) and modify it to > return the desired true and false sequences. Then recreate the > existing "filter" function with a wrapper that throws away the false > sequence. filter has a very deep root in functional programming, I doubt it will change any time soon. Also, consider infinite iterators. With the "filter as wrapper to partition" scheme, it is impossible for split/group/partition to use infinite iterator. From fiafulli at hotmail.it Fri Jul 3 13:39:27 2009 From: fiafulli at hotmail.it (fiafulli) Date: Fri, 3 Jul 2009 19:39:27 +0200 Subject: PyGtkSourceView / PyScintilla Message-ID: <4a4e41d5$0$836$4fafbaef@reader5.news.tin.it> Hello to all. I would have need of the recent version (possibly most recent) than one of the two controls in subject. I think that wrapper for scintilla it would be better but creed that the development for pygtk is stopped. therefore I'm more inclined for GtkSourceView (than today it is to the version 2.6.0 http://projects.gnome.org/gtksourceview/). In the specific I need the version for windows (for that for linux I have not still tried but I must not have problems) of which a setup exists for python 2.5 but it is stop to version 2.2 http://ftp.gnome.org/pub/gnome/binaries/win32/pygtksourceview/2.2/). Someone would be to post the compiled in issue (if it were last version would be fantastic!)? or to explain step to me step as to compile it to me alone on windows (I have tried installing mingw, msys, autoconf, automake, libtools, and all the other ones of time in time, I have begun to compile but they are stopped to me on reference lacking pygobject 2.15 or something of the sort. I lost all morning!)? The version of python that I use is the 2.5.4. on windows xp sp3 (and ubuntu 9.04) Thanks in advance, Francesco p.s.: sorry for my bad english From user at example.net Fri Jul 3 13:42:39 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 19:42:39 +0200 Subject: stringio+tarfile In-Reply-To: References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4debb1$0$1106$4fafbaef@reader3.news.tin.it> Message-ID: <4a4e4311$0$47538$4fafbaef@reader1.news.tin.it> Peter Otten wrote: > gettarinfo() expects a real file, not a file-like object. > You have to create your TarInfo manually. ok... which attributes are mandatory, and which optional? > I recommend that you have a look into the tarfile module's source code. > i will try... but: $ cat /usr/lib/python2.3/tarfile.py | wc -l 1938 wow! it'll take some time ;-) > The following seems to work: > > import sys > import time > import tarfile > import StringIO > > sf1 = "first.txt", StringIO.StringIO("one one\n") > sf2 = "second.txt", StringIO.StringIO("two\n") > tf = StringIO.StringIO() > > tar = tarfile.open(fileobj=tf , mode="w") > > mtime = time.time() > for name, f in [sf1 , sf2]: > ti = tarfile.TarInfo(name) > ti.size = f.len > ti.mtime = mtime > # add more attributes as needed > tar.addfile(ti, f) > > sys.stdout.write(tf.getvalue()) > > Peter > much obliged mr otten bye From sajmikins at gmail.com Fri Jul 3 13:44:52 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 10:44:52 -0700 (PDT) Subject: PSP Caching References: Message-ID: <3e1c0979-fb6b-4d6e-80e5-a4af8d5fd96a@a36g2000yqc.googlegroups.com> On Jul 3, 5:18?am, Johnson Mpeirwe wrote: > Hello All, > > How do I stop caching of Python Server Pages (or whatever causes changes > in a page not to be noticed in a web browser)? I am new to developing > web applications in Python and after looking at implementations of PSP > like Spyce (which I believed introduces new unnecessary non-PSP syntax), > I decided to write my own PSP applications from scratch. When I modify a > file, I keep getting the old results until I intentionally introduce an > error (e.g parse error) and correct it after to have the changes > noticed. There's no proxy (I am working on a windows machine unplugged > from the network). I have Googled and no documents seem to talk about > this. Is there any particular mod_python directive I must set in my > Apache configuration to fix this? > > Any help will be highly appreciated. > > Johnson I don't know much about caching with apache, but the answer mght be on this page: http://httpd.apache.org/docs/2.2/caching.html Meanwhile, couldn't you just send apache a restart signal when you modify your code? HTH, ~Simon From Scott.Daniels at Acm.Org Fri Jul 3 13:47:15 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 03 Jul 2009 10:47:15 -0700 Subject: Sequence splitting In-Reply-To: <025dab69$0$20657$c3e8da3@news.astraweb.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I've never needed such a split function, and I don't like the name, and > the functionality isn't general enough. I'd prefer something which splits > the input sequence into as many sublists as necessary, according to the > output of the key function. Something like itertools.groupby(), except it > runs through the entire sequence and collates all the elements with > identical keys. > > splitby(range(10), lambda n: n%3) > => [ (0, [0, 3, 6, 9]), > (1, [1, 4, 7]), > (2, [2, 5, 8]) ] > > Your split() would be nearly equivalent to this with a key function that > returns a Boolean. Well, here is my go at doing the original with iterators: def splitter(source, test=bool): a, b = itertools.tee((x, test(x)) for x in source) return (data for data, decision in a if decision), ( data for data, decision in b if not decision) This has the advantage that it can operate on infinite lists. For something like splitby for grouping, I seem to need to know the cases up front: def _make_gen(particular, src): return (x for x, c in src if c == particular) def splitby(source, cases, case): '''Produce a dict of generators for case(el) for el in source''' decided = itertools.tee(((x, case(x)) for x in source), len(cases)) return dict((c, _make_gen(c, src)) for c, src in zip(cases, decided)) example: def classify(n): '''Least prime factor of a few''' for prime in [2, 3, 5, 7]: if n % prime == 0: return prime return 0 for k,g in splitby(range(50), (2, 3, 5, 7, 0), classify).items(): print('%s: %s' % (k, list(g))) 0: [1, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] 2: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48] 3: [3, 9, 15, 21, 27, 33, 39, 45] 5: [5, 25, 35] 7: [7, 49] --Scott David Daniels Scott.Daniels at Acm.Org From alan.isaac at gmail.com Fri Jul 3 13:50:23 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 17:50:23 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: > In Alan G Isaac writes: >> 1. Don't use assertions to test argument values! On 7/3/2009 12:19 PM kj apparently wrote: > Out of curiosity, where does this come from? http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_stmt "The current code generator emits no code for an assert statement when optimization is requested at compile time." Alan Isaac From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 14:03:07 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 Jul 2009 18:03:07 GMT Subject: Clarity vs. code reuse/generality References: Message-ID: <025e3974$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 16:19:22 +0000, kj wrote: > In Alan G Isaac > writes: > >>1. Don't use assertions to test argument values! > > Out of curiosity, where does this come from? Assertions are disabled when you run Python with the -O (optimise) flag. If you rely on assertions to verify data, then any time the user runs python with -O your code will be running without error checking. assert should be used to verify program logic, not to sanitize data. -- Steven From kee at kagi.com Fri Jul 3 14:08:19 2009 From: kee at kagi.com (Kee Nethery) Date: Fri, 3 Jul 2009 11:08:19 -0700 Subject: Python debugger In-Reply-To: <740981.83964.qm@web7901.mail.in.yahoo.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> Message-ID: <4A19FA5C-C135-480F-BFD1-82B7AF1500A8@kagi.com> It's not free but I like the debugger in Komodo IDE. Lets me simulate a web connection, lets me step through the code and examine the variables as it executes, can be run remotely (have not played with that aspect yet). Does variable inspection of the variables so you can dive into the parts of arrays and dictionaries to see what the (for example) 5th item of the 4th item named "blah" is set to and what type of data element it is (int, unicode, etc). I find it tremendously useful as a newbie to Python. Kee Nethery From patrick.just4fun at gmail.com Fri Jul 3 14:18:51 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Fri, 03 Jul 2009 20:18:51 +0200 Subject: Reversible Debugging Message-ID: <4A4E4B8B.7090104@gmail.com> Hello, I am interested if there are any python modules, that supports reversible debugging aka stepping backwards. Any links or ideas would be helpful, because I am thinking of implementing something like that. Thanks in advance, Patrick From petshmidt at googlemail.com Fri Jul 3 14:34:21 2009 From: petshmidt at googlemail.com (Tep) Date: Fri, 3 Jul 2009 11:34:21 -0700 (PDT) Subject: =?windows-1252?Q?Re=3A_getting_rid_of_=97?= References: <1bbf32ca-e5a0-4d03-8dbb-49d10dd0a89a@18g2000yqa.googlegroups.com><02e939ad-7769-4272-80bd-bceb4b0a149d@r33g2000yqn.googlegroups.com><9594004c-7419-444d-9077-61e922468214@s9g2000yqd.googlegroups.com> <4416985a-b8e4-409a-b237-f0d638012524@d32g2000yqh.googlegroups.com> <46d36544-1ea2-4391-8922-11b8127a2fef@o6g2000yqj.googlegroups.com> <9f343cda-58fc-43c1-9619-4da00fa4dc57@d32g2000yqh.googlegroups.com> Message-ID: On 3 Jul., 18:54, MRAB wrote: > Tep wrote: > > On 3 Jul., 16:58, "Mark Tolonen" wrote: > >> "Tep" wrote in message > > >>news:46d36544-1ea2-4391-8922-11b8127a2fef at o6g2000yqj.googlegroups.com... > > >>> On 3 Jul., 06:40, Simon Forman wrote: > >>>> On Jul 2, 4:31 am, Tep wrote: > >> [snip] > >>>>>>>> how can I replace '?' sign from string? Or do split at that > >>>>>>>> character? > >>>>>>>> Getting unicode error if I try to do it: > >>>>>>>> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in > >>>>>>>> position > >>>>>>>> 1: ordinal not in range(128) > >>>>>>>> Thanks, Pet > >>>>>>>> script is # -*- coding: UTF-8 -*- > >> [snip] > >>>> I just tried a bit of your code above in my interpreter here and it > >>>> worked fine: > >>>> |>>> data = 'foo ? bar' > >>>> |>>> data.split('?') > >>>> |['foo ', ' bar'] > >>>> |>>> data = u'foo ? bar' > >>> |>>> data.split(u'?') > >>>> |[u'foo ', u' bar'] > >>>> Figure out the smallest piece of "html source code" that causes the > >>>> problem and include that with your next post. > >>> The problem was, I've converted "html source code" to unicode object > >>> and didn't encoded to utf-8 back, before using split... > >>> Thanks for help and sorry for not so smart question > >>> Pet > >> You'd still benefit from posting some code. ?You shouldn't be converting > > > I've posted code below > > >> back to utf-8 to do a split, you should be using a Unicode string with split > >> on the Unicode version of the "html source code". ?Also make sure your file > >> is actually saved in the encoding you declare. ?I print the encoding of your > >> symbol in two encodings to illustrate why I suspect this. > > > File was indeed in windows-1252, I've changed this. For errors see > > below > > >> Below, assume "data" is your "html source code" as a Unicode string: > > >> # -*- coding: UTF-8 -*- > >> data = u'foo ? bar' > >> print repr(u'?'.encode('utf-8')) > >> print repr(u'?'.encode('windows-1252')) > >> print data.split(u'?') > >> print data.split('?') > > >> OUTPUT: > > >> '\xe2\x80\x94' > >> '\x97' > >> [u'foo ', u' bar'] > >> Traceback (most recent call last): > >> ? File > >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > >> line 427, in ImportFile > >> ? ? exec codeObj in __main__.__dict__ > >> ? File "", line 1, in > >> ? File "x.py", line 6, in > >> ? ? print data.split('?') > >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: > >> ordinal not in range(128) > > >> Note that using the Unicode string in split() works. ?Also note the decode > >> byte in the error message when using a non-Unicode string to split the > >> Unicode data. ?In your original error message the decode byte that caused an > >> error was 0x97, which is 'EM DASH' in Windows-1252 encoding. ?Make sure to > >> save your source code in the encoding you declare. ?If I save the above > >> script in windows-1252 encoding and change the coding line to windows-1252 I > >> get the same results, but the decode byte is 0x97. > > >> # coding: windows-1252 > >> data = u'foo ? bar' > >> print repr(u'?'.encode('utf-8')) > >> print repr(u'?'.encode('windows-1252')) > >> print data.split(u'?') > >> print data.split('?') > > >> '\xe2\x80\x94' > >> '\x97' > >> [u'foo ', u' bar'] > >> Traceback (most recent call last): > >> ? File > >> "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > >> line 427, in ImportFile > >> ? ? exec codeObj in __main__.__dict__ > >> ? File "", line 1, in > >> ? File "x.py", line 6, in > >> ? ? print data.split('?) > >> UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 0: > >> ordinal not in range(128) > > >> -Mark > > > #! /usr/bin/python > > # -*- coding: UTF-8 -*- > > import urllib2 > > import re > > def getTitle(input): > > ? ? title = re.search('(.*?)', input) > > The input is Unicode, so it's probably better for the regular expression > to also be Unicode: > > ? ? ?title = re.search(u'(.*?)', input) > > (In the current implementation it actually doesn't matter.) > > > ? ? title = title.group(1) > > ? ? print "FULL TITLE", title.encode('UTF-8') > > ? ? parts = title.split(' ? ') > > The title is Unicode, so the string with which you're splitting should > also be Unicode: > > ? ? ?parts = title.split(u' ? ') > Oh, so simple. I'm new to python and still feel uncomfortable with unicode stuff. Thanks to all for help! > > > > ? ? return parts[0] > > > def getWebPage(url): > > ? ? user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' > > ? ? headers = { 'User-Agent' : user_agent } > > ? ? req = urllib2.Request(url, '', headers) > > ? ? response = urllib2.urlopen(req) > > ? ? the_page = unicode(response.read(), 'UTF-8') > > ? ? return the_page > > > def main(): > > ? ? url = "http://bg.wikipedia.org/wiki/ > > %D0%91%D0%B0%D1%85%D1%80%D0%B5%D0%B9%D0%BD" > > ? ? title = getTitle(getWebPage(url)) > > ? ? print title[0] > > > if __name__ == "__main__": > > ? ? main() > > > Traceback (most recent call last): > > ? File "C:\user\Projects\test\src\new_main.py", line 29, in > > ? ? main() > > ? File "C:\user\Projects\test\src\new_main.py", line 24, in main > > ? ? title = getTitle(getWebPage(url)) > > FULL TITLE ?????????????? ??? ????????????????? > > ? File "C:\user\Projects\test\src\new_main.py", line 9, in getTitle > > ? ? parts = title.split(' ??? ') > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position > > 1: ordinal not in range(128) From charles at declareSub.com Fri Jul 3 14:50:48 2009 From: charles at declareSub.com (Charles Yeomans) Date: Fri, 3 Jul 2009 14:50:48 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <025e3974$0$20657$c3e8da3@news.astraweb.com> References: <025e3974$0$20657$c3e8da3@news.astraweb.com> Message-ID: <50230590-3F00-45ED-A1B2-DCA85366C416@declareSub.com> On Jul 3, 2009, at 2:03 PM, Steven D'Aprano wrote: > On Fri, 03 Jul 2009 16:19:22 +0000, kj wrote: > >> In Alan G Isaac >> writes: >> >>> 1. Don't use assertions to test argument values! >> >> Out of curiosity, where does this come from? > > Assertions are disabled when you run Python with the -O (optimise) > flag. > If you rely on assertions to verify data, then any time the user runs > python with -O your code will be running without error checking. > > assert should be used to verify program logic, not to sanitize data. I wouldn't describe the use of an assert statement in the original code as data sanitizing, but rather as a precondition. And with that description, the use of an assert statement that might be compiled away is not unreasonable; indeed, it certainly is so in the context of design by contract. Charles Yeomans From tjreedy at udel.edu Fri Jul 3 14:57:20 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 14:57:20 -0400 Subject: Python 3.0 (pinin' for the fjords) In-Reply-To: References: Message-ID: Alan G Isaac wrote: > Using the 3.1 Windows installer, I chose that I did not > want the extensions registered, and the installer > unregistered the .py and .pyw extensions (which I had wanted > to keep associated with Python 2.6). > > Is this intentional? I suspect not, but Martin is the one to answer. If you had started a new thread for this new topic "3.1 Windows Installer trashes extension registrations" this would have been seen more easily ;-) From mwilson at the-wire.com Fri Jul 3 15:08:35 2009 From: mwilson at the-wire.com (Mel) Date: Fri, 03 Jul 2009 15:08:35 -0400 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > The most important difference between my suggestion and that of the OP is > that he limited the key function to something which returns a truth > value, while I'm looking for something more general which can split the > input into an arbitrary number of collated sublists. Generally along the lines of def scatter (seq, criterion): lists = {} for s in seq: lists.setdefault (criterion (s), []).append (s) return lists modulo a defaultdict or some such ? Mel. > > > From dudeja.rajat at gmail.com Fri Jul 3 15:10:06 2009 From: dudeja.rajat at gmail.com (dudeja.rajat at gmail.com) Date: Sat, 4 Jul 2009 00:40:06 +0530 Subject: [Help] Python implmenentation of finding process's command line parameters. Message-ID: I'm looking for some python implementation of finding the command line parameters of a process on Windows XP SP3 machine. I found some links but they all point me to the direction of c/c++ implememtations. I've seen WMI, but this takes time to load. So I just though if some one has done implementation and could pointing me in some right direction. Regards, Rajat -------------- next part -------------- An HTML attachment was scrubbed... URL: From nosklo at gmail.com Fri Jul 3 15:12:39 2009 From: nosklo at gmail.com (Clovis Fabricio) Date: Fri, 3 Jul 2009 16:12:39 -0300 Subject: Python debugger In-Reply-To: <4A19FA5C-C135-480F-BFD1-82B7AF1500A8@kagi.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> <4A19FA5C-C135-480F-BFD1-82B7AF1500A8@kagi.com> Message-ID: <9187a60d0907031212m7f91024m46b7d440e0815f29@mail.gmail.com> 2009/7/3 Kee Nethery : > It's not free but I like the debugger in Komodo IDE. > Lets me simulate a web connection, lets me step through the code and examine > the variables as it executes, can be run remotely (have not played with that > aspect yet). > Does variable inspection of the variables so you can dive into the parts of > arrays and dictionaries to see what the (for example) 5th item of the 4th > item named "blah" is set to and what type of data element it is (int, > unicode, etc). I find it tremendously useful as a newbie to Python. > Kee Nethery Winpdb is free and offers all those features. From mail at timgolden.me.uk Fri Jul 3 15:15:37 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 03 Jul 2009 20:15:37 +0100 Subject: [Help] Python implmenentation of finding process's command line parameters. In-Reply-To: References: Message-ID: <4A4E58D9.6050806@timgolden.me.uk> dudeja.rajat at gmail.com wrote: > I'm looking for some python implementation of finding the command line > parameters of a process on Windows XP SP3 machine. I found some links but > they all point me to the direction of c/c++ implememtations. > > I've seen WMI, but this takes time to load. So I just though if some one has > done implementation and could pointing me in some right direction. Someone recently posted an example using PSI [1] which seemed quite fast. TJG [1] http://www.psychofx.com/psi/trac/wiki/ From mr.spoon21 at gmail.com Fri Jul 3 15:23:20 2009 From: mr.spoon21 at gmail.com (Mr.SpOOn) Date: Fri, 3 Jul 2009 21:23:20 +0200 Subject: Getting input the scanf way In-Reply-To: References: Message-ID: <8f67b6f80907031223u767044ara169ddf137b5207a@mail.gmail.com> Thanks, I think raw_input() will work just fine for my needs. From tjreedy at udel.edu Fri Jul 3 15:23:47 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 15:23:47 -0400 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <3e538aa0-e549-480d-b29b-51fa9f96b785@b15g2000yqd.googlegroups.com> Message-ID: Brad wrote: > On Jul 2, 8:17 pm, "Pablo Torres N." wrote: >> This sounds like it belongs to the python-ideas list. I suggest >> posting there for better feedback, since the core developers check >> that list more often than this one. > > I tried posting on python-ideas and received a "You are not allowed to > post to this mailing list" reply. Perhaps because I am posting through > Google groups? Or maybe one must be an approved member to post? Spammers post thru Google groups Either subscribe or access via news.gmane.org as gmane.comp.python.ideas. As to your main question: this was discuss some years ago. There did not seem enough use cases for 'bifilter' or 'distributor' to warrent inclusion in the stdlib. Easy enough to do on one's own for those few cases. Just filter twice. Usually, one wants to do one thing or another to each item with the original scan for item in collection: if pred(item): proc_true(item) else: proc_false(item) Having proc_true and proc_false both be append(item) is a special case. Note that filter has been changed from returning a list to returning an iterator. So a proposal for a function to return two lists would seem backwards. A function that returns two iterators would be possible. Something like itertools.tee except that each item would go on one tee or the other instead of both. It would have two queues (deques) instead of one. And it should be lazy -- items should only be pulled from the original iterator when an item is requested from an empty queue. I am not sure it is worth it. In the worst case, all items are say 'True' and you request 'False' items and all items get copied to the True queue before the False iterator raised StopIteration. The way to avoid calling keyfunc(item) twice on each item is to make a new list first: newlist = list(map(keyfunc,iterable)). Then scan newlist twice. If you write such a function, first in Python, then C, you can register code on PyPI and see how much interest it gets. Terry Jan Reedy From tjreedy at udel.edu Fri Jul 3 15:27:25 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 15:27:25 -0400 Subject: Sequence splitting In-Reply-To: <7xr5wx8zaf.fsf@ruckus.brouhaha.com> References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7xr5wx8zaf.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Brad writes: >> Maybe this would be difficult to get into the core, but how about this >> idea: Rename the current filter function to something like "split" or >> "partition" (which I agree may be a better name) and modify it to >> return the desired true and false sequences. Then recreate the >> existing "filter" function with a wrapper that throws away the false >> sequence. > > This isn't so attractive, since filter takes a sequence input but > returns a list. In modern Python (py3), it returns an iterator. From gnewsg at gmail.com Fri Jul 3 15:33:48 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Fri, 3 Jul 2009 12:33:48 -0700 (PDT) Subject: Python implmenentation of finding process's command line parameters. References: Message-ID: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> On 3 Lug, 21:15, Tim Golden wrote: > dudeja.ra... at gmail.com wrote: > > I'm looking for some python implementation of finding the command line > > parameters of a process on Windows XP SP3 machine. I found some links but > > they all point me to the direction of c/c++ implememtations. > > > I've seen WMI, but this takes time to load. So I just though if some one has > > done implementation and could pointing me in some right direction. > > Someone recently posted an example using PSI [1] which seemed > quite fast. > > TJG > > [1]http://www.psychofx.com/psi/trac/wiki/ If I'm not mistaken psi shouldn't support Windows. You can use psutil: http://code.google.com/p/psutil/ >>> import psutil >>> p = psutil.Process(7055) >>> p.cmdline ['/usr/bin/python', '-O'] >>> --- Giampaolo http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil/ From http Fri Jul 3 15:41:20 2009 From: http (Paul Rubin) Date: 03 Jul 2009 12:41:20 -0700 Subject: Sequence splitting References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7xr5wx8zaf.fsf@ruckus.brouhaha.com> Message-ID: <7xr5wximfz.fsf@ruckus.brouhaha.com> Terry Reedy writes: > > This isn't so attractive, since filter takes a sequence input but > > returns a list. > > In modern Python (py3), it returns an iterator. Still not much help in the proposed version that would return two iterators. From dudeja.rajat at gmail.com Fri Jul 3 15:41:30 2009 From: dudeja.rajat at gmail.com (dudeja.rajat at gmail.com) Date: Sat, 4 Jul 2009 01:11:30 +0530 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: I have had a look at psutil, however, this only works with Python 2.6.x and above. We are using Python 2.5.2 and updating python could pose certain problems. So, I'm not keep on using psutils. -- Regrads, Rajat -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Fri Jul 3 15:48:20 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 03 Jul 2009 20:48:20 +0100 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: <4A4E6084.6030001@timgolden.me.uk> Giampaolo Rodola' wrote: > On 3 Lug, 21:15, Tim Golden wrote: >> dudeja.ra... at gmail.com wrote: >>> I'm looking for some python implementation of finding the command line >>> parameters of a process on Windows XP SP3 machine. I found some links but >>> they all point me to the direction of c/c++ implememtations. >>> I've seen WMI, but this takes time to load. So I just though if some one has >>> done implementation and could pointing me in some right direction. >> Someone recently posted an example using PSI [1] which seemed >> quite fast. >> >> TJG >> >> [1]http://www.psychofx.com/psi/trac/wiki/ > > If I'm not mistaken psi shouldn't support Windows. > You can use psutil: > http://code.google.com/p/psutil/ Sorry. Exactly what I meant. (Should have checked for the exact post rather than trusting to my memory). TKG From mail at timgolden.me.uk Fri Jul 3 15:48:32 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 03 Jul 2009 20:48:32 +0100 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: <4A4E6090.4060102@timgolden.me.uk> Giampaolo Rodola' wrote: > On 3 Lug, 21:15, Tim Golden wrote: >> dudeja.ra... at gmail.com wrote: >>> I'm looking for some python implementation of finding the command line >>> parameters of a process on Windows XP SP3 machine. I found some links but >>> they all point me to the direction of c/c++ implememtations. >>> I've seen WMI, but this takes time to load. So I just though if some one has >>> done implementation and could pointing me in some right direction. >> Someone recently posted an example using PSI [1] which seemed >> quite fast. >> >> TJG >> >> [1]http://www.psychofx.com/psi/trac/wiki/ > > If I'm not mistaken psi shouldn't support Windows. > You can use psutil: > http://code.google.com/p/psutil/ Sorry. Exactly what I meant. (Should have checked for the exact post rather than trusting to my memory). TJG From tjgolden at gmail.com Fri Jul 3 15:50:04 2009 From: tjgolden at gmail.com (Tim Golden) Date: Fri, 03 Jul 2009 20:50:04 +0100 Subject: Python implmenentation of finding process's command line parameters. In-Reply-To: References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: <4A4E60EC.7030400@gmail.com> dudeja.rajat at gmail.com wrote: > I have had a look at psutil, however, this only works with Python 2.6.x and > above. We are using Python 2.5.2 and updating python could pose certain > problems. So, I'm not keep on using psutils. Doesn't look as if there's anything 2.6-specific there. Maybe the maintainer can comment, but I suspect you'd just need to recompile and link against 2.5 TJG From tjreedy at udel.edu Fri Jul 3 15:51:56 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 15:51:56 -0400 Subject: pep 8 constants In-Reply-To: <4A4E135E.5090400@harvee.org> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> Message-ID: Eric S. Johansson wrote: > Horace Blegg wrote: >> I've been kinda following this. I have a cousin who is permanently wheel >> chair bound and doesn't have perfect control of her hands, but still >> manages to use a computer and interact with society. However, the >> idea/thought of disabled programmers was new to me/hadn't ever occurred >> to me. >> >> You say that using your hands is painful, but what about your feet? >> Wouldn't it be possible to rig up some kind of foot peddle for >> shift/caps lock? Kinda like the power peddle used with sowing machines, >> so the hands are free to hold fabric. >> >> I don't mean this in a condescending manor, and I apologize if you take >> it as such. I'm genuinely curious if you think something like this could >> work. >> >> The way I was envisioning it working last night (and I haven't the >> faintest clue how SR works, nor have I ever used SR) was that you would >> hit the foot peddle, which would tell the SR program to capitalize the >> first letter of the next word (a smart shift, basically, so you don't >> end up doing something like ... WONderland -or- "stocks are up 1,0))% >> TOday".) >> >> Possible? Stupid? >> > it's not stupid. > > People have used foot pedals for decades for a variety of controls. I don't > think foot pedals would work for me because when I am dictating, I pace. > Standing, sitting, I pace. With a cord headset, I'm forced to stay within about > 4 feet of the computer. But what I'm using a Bluetooth headset, I will sometimes > ramble as far as 10 or 15 feet from the computer. It helps if I make the font > larger so I can glance over and see what kind of errors I've gotten. > > I really love a Bluetooth headset with speech recognition. It's so liberating. > > Your question about foot pedals makes me think of alternative. would it make > sense to have a handheld keyboard which would be used for command-and-control > functionality or as an adjunct to speech recognition use? It would have to be > designed in such a way that it doesn't aggravate a hand injury which may not be > possible. Anyway, just thinking out loud. As long as we are thinking wildly, how about a pair of bluetooth-connected switches on a belt or chest band that you could press with your arms or elbows -- no hands involved. Sort of become a walking mouse ;-). Terry From gnewsg at gmail.com Fri Jul 3 15:57:44 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Fri, 3 Jul 2009 12:57:44 -0700 (PDT) Subject: Python implmenentation of finding process's command line parameters. References: <86554d61-9c84-4c1d-bf9f-2104d92d29ae@c36g2000yqn.googlegroups.com> Message-ID: On 3 Lug, 21:50, Tim Golden wrote: > Sorry. Exactly what I meant. (Should have checked for > the exact post rather than trusting to my memory) No prolem. > dudeja.ra... at gmail.com wrote: > > I have had a look at psutil, however, this only works with Python 2.6.x and > > above. We are using Python 2.5.2 and updating python could pose certain > > problems. So, I'm not keep on using psutils. > > Doesn't look as if there's anything 2.6-specific there. > Maybe the maintainer can comment, but I suspect you'd > just need to recompile and link against 2.5 > > TJG psutil works on Python 2.4, 2.5 and 2.6. If you are on Windows, you can get the precompiled binary for Python 2.5 from here: http://code.google.com/p/psutil/downloads/list --- Giampaolo http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil/ From sajmikins at gmail.com Fri Jul 3 16:04:21 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 13:04:21 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <529c44a7-eb97-4d56-a376-1152cb9eaea2@37g2000yqp.googlegroups.com> Message-ID: On Jul 3, 2:09?am, Lie Ryan wrote: > Simon Forman wrote: > > On Jul 2, 3:57 pm, Scott David Daniels wrote: > >> Duncan Booth wrote: > >>> Simon Forman wrote: > >>>> ... > >>>> if self.higher is self.lower is None: return > >>>> ... > >>> As a matter of style however I wouldn't use the shorthand to run two 'is' > >>> comparisons together, I'd write that out in full if it was actually needed > >>> here. > >> Speaking only to the style issue, when I've wanted to do something like > >> that, I find: > > >> ? ? ? ?if self.higher is None is self.lower: > >> ? ? ? ? ? ?... > > >> more readable, by making clear they are both being compared to a > >> constant, rather than compared to each other. > > > I was going to do it that way for aesthetics if nothing else, but in > > this particular code "self.higher is self.lower" could only have been > > True if they were both None, so the final "is None" was redundant and > > I only included it as a "just-in-case" check in case someone someday > > used the code in such a way as to assign the same object (not None) to > > both self.higher and self.lower... ?Totally pointless, I'm sorry to > > say. ?I'm glad the whole line was redundant really. > > I say, you should keep the `is None` for readability and to safe guard > against immutable optimization and/or Singleton objects. Due to an > implementation detail, python does not guarantee that two immutable > object will return different objects; you have: > > >>> a = 1 > >>> b = 1 > >>> a is b > > True I probably would have kept the 'is None' for those reasons except that this code was never meant to be used by anyone but me, and it turned out the entire line was redundant anyway. :] From python at mrabarnett.plus.com Fri Jul 3 16:07:50 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 03 Jul 2009 21:07:50 +0100 Subject: Sequence splitting In-Reply-To: References: <6b9702ab-2eb6-4348-b298-5f53012682be@q40g2000prh.googlegroups.com> <025dab69$0$20657$c3e8da3@news.astraweb.com> <7x1voy5h3j.fsf@ruckus.brouhaha.com> <025db26e$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4A4E6516.8090207@mrabarnett.plus.com> Mel wrote: > Steven D'Aprano wrote: > >> The most important difference between my suggestion and that of the OP is >> that he limited the key function to something which returns a truth >> value, while I'm looking for something more general which can split the >> input into an arbitrary number of collated sublists. > > Generally along the lines of > > def scatter (seq, criterion): > lists = {} > for s in seq: > lists.setdefault (criterion (s), []).append (s) > return lists > > modulo a defaultdict or some such ? > Somehow this reminds me of the new Counter class, except that it's making lists instead of counts. From user at example.net Fri Jul 3 16:14:22 2009 From: user at example.net (superpollo) Date: Fri, 03 Jul 2009 22:14:22 +0200 Subject: stringio+tarfile (or better... zipfile) In-Reply-To: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: <4a4e66a0$0$1115$4fafbaef@reader4.news.tin.it> following the excellent suggestions received, i tried to adapt the problem to zipfile, and i wrote this: $ cat zip001.py import sys import zipfile import StringIO nb1 = "first.txt", StringIO.StringIO("one one\n") nb2 = "second.txt", StringIO.StringIO("two\n") zb = StringIO.StringIO() zip = zipfile.ZipFile(zb , "w" , zipfile.ZIP_DEFLATED) for name , buffer in [nb1 , nb2]: zip.writestr(name, buffer.getvalue()*1000) zip.close() sys.stdout.write(zb.getvalue()) $ python zip001.py > zip001.out $ unzip -l zip001.out Archive: zip001.out Length Date Time Name -------- ---- ---- ---- 8000 07-03-09 22:07 first.txt 4000 07-03-09 22:07 second.txt -------- ------- 12000 2 files $ it seems to me thaz zipfile has a simpler usability... any comments? thanks again and bye bye From sajmikins at gmail.com Fri Jul 3 16:17:16 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 13:17:16 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> Message-ID: <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> On Jul 3, 12:56?am, Paul Rubin wrote: > Simon Forman writes: > > > Yes, but the concept of null pointers is considered kludgy these days. > > Seriously? "kludgy"? ?(I really AM getting old.) > > ?http://math.andrej.com/2009/04/11/on-programming-language-design/ > > discusses the issue (scroll down to "Undefined values" section). Hmm, I'm not convinced. I read that post with interest, but his argument against None et. al. was just this: "...stick to the principle: NULL is wrong because it causes horrible and tricky mistakes which appear even after the program was tested thoroughly. You cannot introduce NULL into the language and tell the programmer to be careful about it. The programmer is not capable of being careful!" To me that seems weak and too general, you could say /anything/ "...causes horrible and tricky mistakes which appear even after the program was tested thoroughly." He does go on to say, in a reply to a comment, "In my opinion, at the end of the day the quality of the code depends more on the quality of its author than on the quality of the language he uses. But this does not mean that the quality of the programming language does not matter." Which I agree with wholeheartedly. I just don't see that he made a strong case that the inclusion of NULLs directly implies poor quality of language. (I like Haskell's Maybe, but saying A is better than B doesn't imply that B is therefore terrible.) > > Well I wouldn't know, I've been fortunate enough to program mostly in > > python for over half a decade now and None and 0 are as close as I've > > gotten to NULL in a long time. > > Right, and how many times have you had to debug > > ? ?AttributeError: 'NoneType' object has no attribute 'foo' > > or the equivalent null pointer exceptions in Java, C, or whatever? > They are very common. ?And the basic idea is that if you avoid using > null pointers in the first place, you'll get fewer accidental null > pointer exceptions. I have encountered exceptions like that, but usually as a result of e.g. omitting a return statement (so the caller gets None instead of whatever should have been returned.) I don't usually write "pointer" style code in python. In fact, other than this BTree and the Dancing Links algorithm I mentioned earlier I can't recall ever having done it. Indeed, python's omission of pointers is one of the reasons I'm so fond of it. In my experience with python exceptions like that one are indicators of something bad wrong with my code, not of misuse of None (as NULL pointer.) > > That sounds very interesting, but I'm only writing a BTree to then use > > it to play with "persistent data structures" > > But you have implemented a mutable tree. ?If you want to write a > persistent one, then write a persistent one! ?You also use a wishful The paper I'm working from is about general techniques for making persistent data structures from mutable forms, thus I wanted a mutable BTree to then "overwrite" with a persistent form. > heuristic in the hope of keeping the tree balanced--if you want a > balanced tree, why not use one that's guaranteed to stay in balance? > AVL trees are pretty easy to implement; maybe you could try writing a > persistent one: > > ?http://en.wikipedia.org/wiki/AVL_tree The balance (or lack thereof) of the tree is not important (in this use.) I threw in that heuristic in the delete function because it was easy enough and it was mentioned in the wikipedia article on BTrees. AVL trees are easy too, but still more complicated than I need. > > In this particular case it's somewhat more elegant (IMO) to check "is > > None". > > Well, I can't understand why that might be, but it's surely > subjective, so ok. It's a matter of this: if a is None: return b if b is None: return a # handle both non-None here... vs. this: if a is not None: if b is not None: # handle both non-None here... else: return a return b Everything is subjective, but I think the first one is more elegant IMO. > > > I'm sorry but I find that class rather ugly. ?The code would be a lot > > > smaller and have fewer corner cases with a different data representation. > > > Um, thanks? ?Seriously though, care to put your money where your mouth > > is? I'd be interested to see a BTree implemented as you indicated above. > > Er, I'm not trying to argue with you. ?You asked for people's advice > and impressions, so I gave you some. ?I don't feel like rewriting that I know, I asked for it. :] But it still stings a little to hear someone call my code "rather ugly". No hard feelings. I'm not trying to argue with you either. :] > whole class, but I'll do a method or two, using [] to represent an > empty node. ?(Hmm, actually changing the empty node representation did > less to shorten the code than just getting rid of a bunch of > duplication. ?Really, I'd tend to look for a slightly different tree > structure which tried to avoid these issues). Hmm, I really don't see that using an empty list rather than None as a stand-in for NULL pointer bought you anything there. In fact, this code would work identically if you replaced [] with None. I was hoping to see some clever code-fu involving list manipulation or something. When would you ever store a value in the empty list "nodes" anyway? > Untested: > > class BinaryTree: > ? ? def __init__(self, key, value): > ? ? ? ? self.key = key > ? ? ? ? self.value = value > ? ? ? ? self.higher = self.lower = [] > > ? ? def get(self, key): > ? ? ? ? if key == self.key: > ? ? ? ? ? ? return self.value > ? ? ? ? branch = self.lower if key < self.key else self.higher > ? ? ? ? return branch.get(key) if branch else [] > > ? ? def insert(self, key, value): > ? ? ? ? def xinsert(xtree): ?# xtree is either a tree or [] > ? ? ? ? ? ?if not xtree: xtree = BinaryTree(key, value) > ? ? ? ? ? ?else: xtree.insert(key, value) > ? ? ? ? ? ?return xtree > > ? ? ? ? if key == self.key: > ? ? ? ? ? ? self.value = value > ? ? ? ? elif key < self.key: > ? ? ? ? ? ? self.lower = xinsert(self.lower) > ? ? ? ? else: > ? ? ? ? ? ? self.higher = xinsert(self.higher) > > and so forth. From dickinsm at gmail.com Fri Jul 3 16:18:09 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Fri, 3 Jul 2009 13:18:09 -0700 (PDT) Subject: Compiling 64 bit python on a mac - cannot compute sizeof (int) References: Message-ID: <6c0287d8-c9f5-44e2-8159-fe5c88494a62@g1g2000yqh.googlegroups.com> On Jul 3, 5:30?pm, Keflavich wrote: > I'm trying to compile a 64 bit version of python 2.6.2 on my mac (OS X > 10.5.7), and am running into a problem during the configure stage. > > I configure with: > ./configure --enable-framework=/Library/Frameworks --enable- > universalsdk MACOSX_DEPLOYMENT_TARGET=10.5 --with-universal-archs=all - > with-readline-dir=/usr/local > > because I want 64 and 32 bit, and I needed to install a 64 bit > readline as a prerequisite. > > configure fails at: > checking size of int... configure: error: cannot compute sizeof (int) I confess that I don't fully understand the intricacies of the various OS X autoconf options, but I think the problem is that the --enable- universalsdk option takes a directory. If that directory isn't given, it appears to default to /Developer/SDKs/MacOSX10.4u.sdk (at least on my OS X 10.5.7 machine), which would likely conflict with your MACOSX_DEPLOYMENT_TARGET=10.5 setting. Try either changing MACOSX_DEPLOYMENT_TARGET to 10.4, or using -- enable-universalsdk=/Developer/SDKs/MacOSX10.5.sdk (or whatever the appropriate directory is on your system). For some reason, I think using --enable-universalsdk=/ also works on my system. If none of that helps, you might try asking this question over on the pythonmac- sig mailing list. (http://mail.python.org/mailman/listinfo/pythonmac- sig) Mark From timr at probo.com Fri Jul 3 16:20:18 2009 From: timr at probo.com (Tim Roberts) Date: Fri, 03 Jul 2009 13:20:18 -0700 Subject: Searching equivalent to C++ RAII or deterministic destructors References: <4A4CA278.4050604@ieee.org> Message-ID: Dave Angel wrote: > >You're right of course. What I was trying to say was it deletes the >reference to the object. Unlike obj = None, del obj removes the >reference (attribute) entirely. Although I don't know what it should be >called if it's a local variable. Perhaps it "unbinds" the name. Yes. As far as the object is concerned, "obj = None" and "del obj" are exactly identical. In both cases, there is one less binding to the name. The difference between the two is only whether the name lives on in the namespace. A local variable is (usually) just a name in the local() namespace. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From tjreedy at udel.edu Fri Jul 3 16:36:50 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Jul 2009 16:36:50 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <025e3974$0$20657$c3e8da3@news.astraweb.com> References: <025e3974$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 03 Jul 2009 16:19:22 +0000, kj wrote: > >> In Alan G Isaac >> writes: >> >>> 1. Don't use assertions to test argument values! >> Out of curiosity, where does this come from? > > Assertions are disabled when you run Python with the -O (optimise) flag. > If you rely on assertions to verify data, then any time the user runs > python with -O your code will be running without error checking. > > assert should be used to verify program logic, not to sanitize data. In other words, assertions should never happen, whereas your assertions could easily happen with 'bad' input. An example of program logic assertion would be 'assert ' at the top of the loop. That would be useful for development, but would not be needed for production use of tested code. tjr From sajmikins at gmail.com Fri Jul 3 16:46:28 2009 From: sajmikins at gmail.com (Simon Forman) Date: Fri, 3 Jul 2009 13:46:28 -0700 (PDT) Subject: Python debugger References: Message-ID: On Jul 3, 8:15?am, srinivasan srinivas wrote: > Hi, > Could you suggest some python debuggers? > > Thanks, > Srini > > ? ? ? Love Cricket? Check out live scores, photos, video highlights and more. Click herehttp://cricket.yahoo.com Ipython has good debugger integration. From http Fri Jul 3 16:50:12 2009 From: http (Paul Rubin) Date: 03 Jul 2009 13:50:12 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> Message-ID: <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Simon Forman writes: > "...stick to the principle: NULL is wrong because it causes horrible > and tricky mistakes which appear even after the program was tested > thoroughly. You cannot introduce NULL into the language and tell the > programmer to be careful about it. The programmer is not capable of > being careful!" > > To me that seems weak and too general, you could say /anything/ > "...causes horrible and tricky mistakes which appear even after the > program was tested thoroughly." On the contrary, general means powerful :). For example, you can say that malloc/free in C causes horrible and tricky mistakes etc. Python avoids the mistakes by having automatic storage management. > Which I agree with wholeheartedly. I just don't see that he made a > strong case that the inclusion of NULLs directly implies poor quality > of language. > > (I like Haskell's Maybe, but saying A is better than B doesn't imply > that B is therefore terrible.) I wouldn't say Python's None is terrible, but the programming style in which None is used as a marker for "absent value" is genuinely a source of bugs, requiring care when used. Often it's easy to just avoid it and all the bugs that come with it. Haskell's Maybe type, ML's Option type, and various similar constructs in other recently designed languages all are responses to the shortcomings of None-like constructs in older languages. I'm not going purely by that one guy's blog post, though I do think that post was pretty good. > Hmm, I really don't see that using an empty list rather than None as a > stand-in for NULL pointer bought you anything there. In fact, this > code would work identically if you replaced [] with None. Yes I noticed that too (and mentioned it) after writing the code. I think the real problem is that the class has unnatural semantics and Python's OOP system (well, OOP in general) is also sort of kludgy. I will think about a possible rewrite. From keflavich at gmail.com Fri Jul 3 19:01:42 2009 From: keflavich at gmail.com (Adam) Date: Fri, 3 Jul 2009 16:01:42 -0700 (PDT) Subject: Compiling 64 bit python on a mac - cannot compute sizeof (int) References: <6c0287d8-c9f5-44e2-8159-fe5c88494a62@g1g2000yqh.googlegroups.com> Message-ID: <73186719-e5a6-4661-a06c-cf6f2570996b@2g2000prl.googlegroups.com> On Jul 3, 2:18?pm, Mark Dickinson wrote: > On Jul 3, 5:30?pm, Keflavich wrote: > > > I'm trying to compile a 64 bit version of python 2.6.2 on my mac (OS X > > 10.5.7), and am running into a problem during the configure stage. > > > I configure with: > > ./configure --enable-framework=/Library/Frameworks --enable- > > universalsdk MACOSX_DEPLOYMENT_TARGET=10.5 --with-universal-archs=all - > > with-readline-dir=/usr/local > > > because I want 64 and 32 bit, and I needed to install a 64 bit > > readline as a prerequisite. > > > configure fails at: > > checking size of int... configure: error: cannot compute sizeof (int) > > I confess that I don't fully understand the intricacies of the various > OS X autoconf options, but I think the problem is that the --enable- > universalsdk option takes a directory. ?If that directory isn't > given, it appears to default to /Developer/SDKs/MacOSX10.4u.sdk (at > least on my OS X 10.5.7 machine), which would likely conflict with > your MACOSX_DEPLOYMENT_TARGET=10.5 setting. > > Try either changing MACOSX_DEPLOYMENT_TARGET to 10.4, or using -- > enable-universalsdk=/Developer/SDKs/MacOSX10.5.sdk ?(or whatever the > appropriate directory is on your system). ?For some reason, I think > using --enable-universalsdk=/ also works on my system. ?If none of > that helps, you might try asking this question over on the pythonmac- > sig mailing list. (http://mail.python.org/mailman/listinfo/pythonmac- > sig) > > Mark Thanks. I also determined after the fact that universalsdk was the problem, but I didn't know how to fix it. Unfortunately, it turns out what you identified was a transcription error on my part - I had been using --enable-universalsdk instead of --enable-universalsdk=/. Thanks for the help, I appreciate it! Adam From alan.isaac at gmail.com Fri Jul 3 19:18:15 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 03 Jul 2009 23:18:15 GMT Subject: 3.1 Windows Installer trashes extension registrations In-Reply-To: References: Message-ID: > Alan G Isaac wrote: >> Using the 3.1 Windows installer, I chose that I did not >> want the extensions registered, and the installer >> unregistered the .py and .pyw extensions (which I had >> wanted to keep associated with Python 2.6). >> Is this intentional? On 7/3/2009 2:57 PM Terry Reedy apparently wrote: > I suspect not, but Martin is the one to answer. > If you had started a new thread for this new topic > "3.1 Windows Installer trashes extension registrations" > this would have been seen more easily ;-) Done. Cheers, Alan Isaac From gallium.arsenide at gmail.com Fri Jul 3 19:26:39 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 3 Jul 2009 16:26:39 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: <9d99c5ca-f497-473f-a22f-a5219597bcd7@h11g2000yqb.googlegroups.com> On Jul 3, 4:50?pm, Paul Rubin wrote: > I wouldn't say Python's None is terrible, but the > programming style in which None is used as a marker > for "absent value" is genuinely a source of bugs, > requiring care when used. ?Often it's easy to just > avoid it and all the bugs that come with it. While other languages may make it easier to avoid these types of bugs, I think it's pretty natural to use Python's None as a marker for "absent value", if you're coding in Python. Python seems to encourage this by having functions return None when there is no explicit return value, and by having the interactive command line print nothing at all when the expression evaluates to None. I won't argue with anyone who wants to call these design flaws, but it seems to me that using None this way in one's own Python code is reasonably Pythonic. John From tn.pablo at gmail.com Fri Jul 3 19:40:03 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Fri, 3 Jul 2009 18:40:03 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On Fri, Jul 3, 2009 at 09:05, kj wrote: > The context is the concept of a binary search. ?In one of their > homeworks, my students will have two occasions to use a binary > search. ?This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. Five params for a novice function is overkill. I'd drop the epsilon thing and, assuming you are searching in a list, tuple or something similar, make the lower limit default to the first index and the upper default to the last one. In fact, I'd let them to realize that a function is convenient, and base some of the grading in whether they wrote it or not. Just a thought. -- Pablo Torres N. From ldo at geek-central.gen.new_zealand Fri Jul 3 20:08:17 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 04 Jul 2009 12:08:17 +1200 Subject: Idioms and Anti-Idioms Question References: Message-ID: In message , J. Cliff Dyer wrote: > On Wed, 2009-07-01 at 17:19 +1200, Lawrence D'Oliveiro wrote: >> >> In message , J. >> Cliff Dyer wrote: >> >> > If the lines got separated, a leading + could disappear into its line >> > without any errors showing up. A trailing + would raise a syntax >> > error. >> >> Unless, of course, it was moved onto the previous line as part of >> whatever caused the separation of the lines. How would you guard against >> that? > > Can you give an example of what you mean? Sure. Just start with an example of what you mean. From asm198 at gmail.com Fri Jul 3 21:14:23 2009 From: asm198 at gmail.com (Icarus) Date: Fri, 3 Jul 2009 18:14:23 -0700 (PDT) Subject: Problems with using queue in Tkinter application Message-ID: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> I'm working on a serial protocol analyzer in python. We have an application written by someone else in MFC but we need something that is cross platform. I intended to implement the GUI portion in Tkinter but am having trouble. The idea is that I will read messages from the serial port and output them to a Tkinter Text object initially. Eventually it will have other functionality but that's it for the short term. I've written this little test app to experiment with putting things on the GUI via a Queue which is polled by the Tkinter loop. On some machines this code works fine and I get whatever I type in displayed in the Text widget. On others I get errors like this as soon as I start it running. error in background error handler: out of stack space (infinite loop?) while executing "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) while execu..." I don't understand why on some machines it works exactly as expected and on others it acts the same way Tkinter does when I call functions directly from outside the Tkinter thread. Does anyone have any suggestions? The full code as appended below. Thanks in advance. [code] import Queue class functionQueue: def __init__(self, root = None, timeout = 250): self.functionQueue = Queue.Queue() self.root = root self.timeout = timeout if(self.root): self.pop_function(root) def pop_function(self, root = None): try: funcArgList = self.functionQueue.get(block = False) except Queue.Empty: pass else: try: funcArgList[0](*funcArgList[1]) except: try: print "Failed to call function", funcArgList[0] except: print "Failed to call function" if(root): root.after(self.timeout, lambda: self.pop_function (self.root)) def add_function(self, function, argList): try: self.functionQueue.put([function, argList]) except: pass if( __name__ == '__main__'): import Tkinter import thread text = Tkinter.Text() text.pack() myQueue = functionQueue(text, 50) def gui_loop(): try: text.mainloop() except: import os os._exit(1) thread.start_new_thread(text.mainloop, ()) while(True): usrInput = raw_input() if(usrInput == "-1"): import os os._exit(0) myQueue.add_function(text.insert, ['end', usrInput + "\n"]) myQueue.add_function(text.see, ['end']) [/code] From steve at REMOVE-THIS-cybersource.com.au Fri Jul 3 22:04:42 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 02:04:42 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: <025eaa4f$0$20657$c3e8da3@news.astraweb.com> On Fri, 03 Jul 2009 13:50:12 -0700, Paul Rubin wrote: > I wouldn't say Python's None is terrible, but the programming style in > which None is used as a marker for "absent value" is genuinely a source > of bugs, requiring care when used. Often it's easy to just avoid it and > all the bugs that come with it. So you keep saying, but I don't believe it. I've never found this to be a problem in my own code: a few silly mistakes where I accidentally assign the result of a function that operates by side-effect: blist = alist.sort() or similar, and I haven't done that for years now. No, wait, I tell I lie... re.search() sometimes bites me, because sometimes it returns None and sometimes it returns a matchobject and I don't use re often enough to have good habits with it yet. But that's a good example of why removing NULL (None, nul, nil, whatever you want to call it) doesn't lower the number of bugs, it just changes their nature: if re.search didn't return None, I'd still have trouble with it. There are three natural approaches to (say) re.search() for dealing with failure: (1) return a sentinel value like None; (2) return a matchobject which tests False; (3) raise an exception. Here's how you would use them correctly: # 1 or 2 are handled identically o = re.search(s) if o: process_match(o) else: print "Not found" # 3 is different try: process_match(re.search(s)) except Exception: print "Not found" There's essentially little or no difference in the handling. In all three cases, if you assume search will always succeed or forget to handle the failure case, you will write incorrect code: process_match(re.search(s)) and get an unexpected exception. The only difference is that the specific exception will differ between the cases. Remove None from the scenario doesn't reduce the number of bugs, it just changes their nature. -- Steven From gagsl-py2 at yahoo.com.ar Fri Jul 3 23:50:12 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 04 Jul 2009 00:50:12 -0300 Subject: stringio+tarfile (or better... zipfile) References: <4a4d1f4e$0$1116$4fafbaef@reader3.news.tin.it> <4a4e66a0$0$1115$4fafbaef@reader4.news.tin.it> Message-ID: En Fri, 03 Jul 2009 17:14:22 -0300, superpollo escribi?: > following the excellent suggestions received, i tried to adapt the > problem to zipfile, and i wrote this: > > zip = zipfile.ZipFile(zb , "w" , zipfile.ZIP_DEFLATED) > for name , buffer in [nb1 , nb2]: > zip.writestr(name, buffer.getvalue()*1000) > zip.close() > > it seems to me thaz zipfile has a simpler usability... any comments? Maybe - but a zip and a tar file are not the same thing. Now, if you want to say that there should be a common interfase to handle all those file formats like tar,zip,bz2: Yes, I agree. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Sat Jul 4 00:04:29 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 04 Jul 2009 01:04:29 -0300 Subject: Reversible Debugging References: <4A4E4B8B.7090104@gmail.com> Message-ID: En Fri, 03 Jul 2009 15:18:51 -0300, Patrick Sabin escribi?: > I am interested if there are any python modules, that supports > reversible debugging aka stepping backwards. Any links or ideas would be > helpful, because I am thinking of implementing something like that. Do you want reverse execution, like an undo function? Undo all changes made by executing some piece of code? There are many cases where that's hard to do, or impossible. How to undo object destruction? How to undo external effects, like writing to files? You should think carefully what can and cannot be done - good luck! -- Gabriel Genellina From aahz at pythoncraft.com Sat Jul 4 00:47:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 21:47:30 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: In article <7xhbxtjxtn.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: >Simon Forman writes: >> >> (I like Haskell's Maybe, but saying A is better than B doesn't imply >> that B is therefore terrible.) > >I wouldn't say Python's None is terrible, but the programming style in >which None is used as a marker for "absent value" is genuinely a >source of bugs, requiring care when used. Often it's easy to just >avoid it and all the bugs that come with it. > >Haskell's Maybe type, ML's Option type, and various similar constructs >in other recently designed languages all are responses to the >shortcomings of None-like constructs in older languages. I'm not >going purely by that one guy's blog post, though I do think that post >was pretty good. AFAICT, in order for Maybe and Option to work, you need to have some kind of static typing system. Am I missing something? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From aahz at pythoncraft.com Sat Jul 4 00:50:47 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 Jul 2009 21:50:47 -0700 Subject: Searching equivalent to C++ RAII or deterministic destructors References: Message-ID: In article , Tim Roberts wrote: >Dave Angel wrote: >> >>You're right of course. What I was trying to say was it deletes the >>reference to the object. Unlike obj = None, del obj removes the >>reference (attribute) entirely. Although I don't know what it should be >>called if it's a local variable. Perhaps it "unbinds" the name. > >Yes. As far as the object is concerned, "obj = None" and "del obj" are >exactly identical. In both cases, there is one less binding to the name. > >The difference between the two is only whether the name lives on in the >namespace. > >A local variable is (usually) just a name in the local() namespace. OTOH, Python's ``del`` applies to targets generally, not just names: >>> L = [1,2,3] >>> del L[1] >>> L [1, 3] -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From upwestdon at gmail.com Sat Jul 4 02:03:39 2009 From: upwestdon at gmail.com (upwestdon) Date: Fri, 3 Jul 2009 23:03:39 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> Message-ID: <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> On Jul 2, 1:23?pm, Simon Forman wrote: > Hey I was hoping to get your opinions on a sort of minor stylistic > point. > These two snippets of code are functionally identical. Which would you > use and why? > The first one is easier [for me anyway] to read and understand, but > slightly less efficient, while the second is [marginally] harder to > follow but more efficient. > > ## First snippet > > if self.higher is self.lower is None: return > if self.lower is None: return self.higher > if self.higher is None: return self.lower > > ## Second snippet > > if self.higher is None: > ? ? if self.lower is None: > ? ? ? ? return > ? ? return self.lower > if self.lower is None: > ? ? return self.higher > > What do you think? > > (One minor point: in the first snippet, the "is None" in the first > line is superfluous in the context in which it will be used, the only > time "self.lower is self.higher" will be true is when they are both > None.) How about just: if not (self.higher and self.lower): return self.higher or self.lower From tkjthingone at gmail.com Sat Jul 4 02:46:13 2009 From: tkjthingone at gmail.com (Horace Blegg) Date: Fri, 3 Jul 2009 23:46:13 -0700 Subject: Reversible Debugging In-Reply-To: <4A4E4B8B.7090104@gmail.com> References: <4A4E4B8B.7090104@gmail.com> Message-ID: You might consider using a VM with 'save-points'. You run the program (in a debugger/ida/what have you) to a certain point (logical point would be if/ifelse/else statements, etc) and save the VM state. Once you've saved, you continue. If you find the path you've taken isn't what you are after, you can reload a previous save point and start over, trying a different path the next time. This is also somewhat useful if you're trying to debug an error that happens deep inside of a program, so you can continually jump to the point right before the error happens, instead of needing to run through the entire program each time it crashes. Just beware of tunnel vision. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hanooter at gmail.com Sat Jul 4 03:33:52 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 00:33:52 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. Message-ID: Currently I need to find the most common elements in thousands of arrays within one large array (arround 2 million instances with ~70k unique elements) so I set up a dictionary to handle the counting so when I am iterating I up the count on the corrosponding dictionary element. I then iterate through the dictionary and find the 25 most common elements. the elements are initially held in a array within an array. so I am am just trying to find the most common elements between all the arrays contained in one large array. my current code looks something like this: d = {} for arr in my_array: -----for i in arr: #elements are numpy integers and thus are not accepted as dictionary keys -----------d[int(i)]=d.get(int(i),0)+1 then I filter things down. but with my algorithm that only takes about 1 sec so I dont need to show it here since that isnt the problem. But there has to be something better. I have to do this many many times and it seems silly to iterate through 2 million things just to get 25. The element IDs are integers and are currently being held in numpy arrays in a larger array. this ID is what makes up the key to the dictionary. It currently takes about 5 seconds to accomplish this with my current algorithm. So does anyone know the best solution or algorithm? I think the trick lies in matrix intersections but I do not know. From clp2 at rebertia.com Sat Jul 4 03:45:32 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 00:45:32 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: Message-ID: <50697b2c0907040045i5316c803ma199bfab10324735@mail.gmail.com> On Sat, Jul 4, 2009 at 12:33 AM, mclovin wrote: > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating ?I up the count on the corrosponding dictionary element. I > then iterate through the dictionary and find the 25 most common > elements. > > the elements are initially held in a array within an array. so I am am > just trying to find the most common elements between all the arrays > contained in one large array. > my current code looks something like this: > d = {} > for arr in my_array: > -----for i in arr: > #elements are numpy integers and thus are not accepted as dictionary > keys > -----------d[int(i)]=d.get(int(i),0)+1 > > then I filter things down. but with my algorithm that only takes about > 1 sec so I dont need to show it here since that isnt the problem. > > > But there has to be something better. I have to do this many many > times and it seems silly to iterate through 2 million things just to > get 25. The element IDs are integers and are currently being held in > numpy arrays in a larger array. this ID is what makes up the key to > the dictionary. > > ?It currently takes about 5 seconds to accomplish this with my current > algorithm. > > So does anyone know the best solution or algorithm? I think the trick > lies in matrix intersections but I do not know. Using a defaultdict (http://docs.python.org/library/collections.html#collections.defaultdict) would speed it up (but only slightly) and make it clearer to read. Cheers, Chris -- http://blog.rebertia.com From patrick.just4fun at gmail.com Sat Jul 4 04:04:08 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Sat, 04 Jul 2009 10:04:08 +0200 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F0CF8.5090101@gmail.com> Gabriel Genellina schrieb: > Do you want reverse execution, like an undo function? Undo all changes > made by executing some piece of code? I am not completly sure, if I really want to make exact undo, i.e. undoing commands by reversing all their effects, or just restoring the program state to an arbitrary state of its history, which would not effect things like files. > There are many cases where that's hard to do, or impossible. How to > undo object destruction? How to undo external effects, like writing to > files? Object destruction should not be a big problem to restore. But writing files or other resources is because it is not guaranted that it is even possible or wanted. I considered either ignoring undoing external commands completly or adding some plugin system to do it. > You should think carefully what can and cannot be done - good luck! > From andreengels at gmail.com Sat Jul 4 04:04:42 2009 From: andreengels at gmail.com (Andre Engels) Date: Sat, 4 Jul 2009 10:04:42 +0200 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: Message-ID: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating ?I up the count on the corrosponding dictionary element. I > then iterate through the dictionary and find the 25 most common > elements. > > the elements are initially held in a array within an array. so I am am > just trying to find the most common elements between all the arrays > contained in one large array. > my current code looks something like this: > d = {} > for arr in my_array: > -----for i in arr: > #elements are numpy integers and thus are not accepted as dictionary > keys > -----------d[int(i)]=d.get(int(i),0)+1 > > then I filter things down. but with my algorithm that only takes about > 1 sec so I dont need to show it here since that isnt the problem. > > > But there has to be something better. I have to do this many many > times and it seems silly to iterate through 2 million things just to > get 25. The element IDs are integers and are currently being held in > numpy arrays in a larger array. this ID is what makes up the key to > the dictionary. > > ?It currently takes about 5 seconds to accomplish this with my current > algorithm. > > So does anyone know the best solution or algorithm? I think the trick > lies in matrix intersections but I do not know. There's no better algorithm for the general case. No method of checking the matrices using less than 2000000-x look-ups will ensure you that there's not a new value with x occurences lurking somewhere. However, if you need this information more often, and if the number of values that changes in between is small compared to the total size of the matrices, you could make a gain in subsequent calculations by remembering part results. Depending on the situation you could for example keep the dictionary with the counts and update it each time, or keep such a dictionary for each "big" matrix, and set a flag when that dictionary is not up-to-date any more . -- Andr? Engels, andreengels at gmail.com From Lacrima.Maxim at gmail.com Sat Jul 4 04:12:10 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Sat, 4 Jul 2009 01:12:10 -0700 (PDT) Subject: urllib with x509 certs Message-ID: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Hello! I am trying to use urllib to fetch some internet resources, using my client x509 certificate. I have divided my .p12 file into mykey.key and mycert.cer files. Then I use following approach: >>> import urllib >>> url = 'https://example.com' >>> xml = ''' ... somexml ''' >>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') >>> f = opener.open(url, xml) This works Ok! But every time I am asked to enter PEM pass phrase, which I specified during dividing my .p12 file. So my question... What should I do to make my code fetch any url automatically (without asking me every time to enter pass phrase)? As I understand there is impossible to specify pass phrase while constructing URLopener. So what should I do? With regards, Max (sorry if my English isn't very proper) From __peter__ at web.de Sat Jul 4 04:21:13 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 04 Jul 2009 10:21:13 +0200 Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: Icarus wrote: > I'm working on a serial protocol analyzer in python. We have an > application written by someone else in MFC but we need something that > is cross platform. I intended to implement the GUI portion in Tkinter > but am having trouble. > > The idea is that I will read messages from the serial port and output > them to a Tkinter Text object initially. Eventually it will have > other functionality but that's it for the short term. I've written > this little test app to experiment with putting things on the GUI via > a Queue which is polled by the Tkinter loop. > > On some machines this code works fine and I get whatever I type in > displayed in the Text widget. On others I get errors like this as > soon as I start it running. > > error in background error handler: > out of stack space (infinite loop?) > while executing > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > while execu..." > > > I don't understand why on some machines it works exactly as expected > and on others it acts the same way Tkinter does when I call functions > directly from outside the Tkinter thread. Does anyone have any > suggestions? The full code as appended below. Thanks in advance. > > [code] > > import Queue > > class functionQueue: > > def __init__(self, root = None, timeout = 250): > > self.functionQueue = Queue.Queue() > self.root = root > self.timeout = timeout > > if(self.root): > self.pop_function(root) > > > def pop_function(self, root = None): > > try: > funcArgList = self.functionQueue.get(block = False) > except Queue.Empty: > pass > else: > try: > funcArgList[0](*funcArgList[1]) > except: > try: > print "Failed to call function", funcArgList[0] > except: > print "Failed to call function" > > if(root): > root.after(self.timeout, lambda: self.pop_function > (self.root)) > > def add_function(self, function, argList): > > try: > self.functionQueue.put([function, argList]) > except: > pass > > > if( __name__ == '__main__'): > > import Tkinter > import thread > > text = Tkinter.Text() > text.pack() > > myQueue = functionQueue(text, 50) > > def gui_loop(): > try: > text.mainloop() > except: > import os > os._exit(1) > > thread.start_new_thread(text.mainloop, ()) > > while(True): > usrInput = raw_input() > > if(usrInput == "-1"): > import os > os._exit(0) > > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > myQueue.add_function(text.see, ['end']) > > [/code] I can make it work over here by putting the UI into the main thread, as suggested by http://effbot.org/zone/tkinter-threads.htm: import Queue import Tkinter import threading class FunctionQueue: # unchanged def input_loop(): while True: try: usrInput = raw_input() except EOFError: break myQueue.add_function(text.insert, ['end', usrInput + "\n"]) myQueue.add_function(text.see, ['end']) myQueue.add_function(text.quit, []) if __name__ == '__main__': text = Tkinter.Text() text.pack() myQueue = FunctionQueue(text, 50) threading.Thread(target=input_loop).start() text.mainloop() Peter From clp2 at rebertia.com Sat Jul 4 04:24:36 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 01:24:36 -0700 Subject: urllib with x509 certs In-Reply-To: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: <50697b2c0907040124j4e309c25j77e01c8127711f80@mail.gmail.com> On Sat, Jul 4, 2009 at 1:12 AM, Lacrima wrote: > Hello! > > I am trying to use urllib to fetch some internet resources, using my > client x509 certificate. > I have divided my .p12 file into mykey.key and mycert.cer files. > Then I use following approach: >>>> import urllib >>>> url = 'https://example.com' >>>> xml = ''' > ... somexml > ''' >>>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') >>>> f = opener.open(url, xml) > > This works Ok! But every time I am asked to enter PEM pass phrase, > which I specified during dividing my .p12 file. > So my question... What should I do to make my code fetch any url > automatically (without asking me every time to enter pass phrase)? > As I understand there is impossible to specify pass phrase while > constructing URLopener. > So what should I do? Subclass FancyURLopener [http://docs.python.org/library/urllib.html#urllib.FancyURLopener], overriding the prompt_user_passwd() method [http://docs.python.org/library/urllib.html#urllib.FancyURLopener.prompt_user_passwd]. Then use an instance of your subclass instead of URLopener. Cheers, Chris -- http://blog.rebertia.com From patrick.just4fun at gmail.com Sat Jul 4 04:33:03 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Sat, 04 Jul 2009 10:33:03 +0200 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F13BF.9050605@gmail.com> Horace Blegg schrieb: > You might consider using a VM with 'save-points'. You run the program > (in a debugger/ida/what have you) to a certain point (logical point > would be if/ifelse/else statements, etc) and save the VM state. Once > you've saved, you continue. If you find the path you've taken isn't > what you are after, you can reload a previous save point and start > over, trying a different path the next time. That was my idea to implement it. I thought of taking snapshots of the current state every time a "unredoable instruction", e.g random number generation, is done. For every other instruction I count the number of instructions done since the last snapshot. So I can go back one instruction by restoring to the previous state and go the number of instructions minus one forward. > This is also somewhat useful if you're trying to debug an error that > happens deep inside of a program, so you can continually jump to the > point right before the error happens, instead of needing to run > through the entire program each time it crashes. Just beware of tunnel > vision. I think of implementing some snapshot/restore mechanism first. That may help in many other situations. From stef.mientki at gmail.com Sat Jul 4 05:25:38 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sat, 04 Jul 2009 11:25:38 +0200 Subject: Reversible Debugging In-Reply-To: <4A4E4B8B.7090104@gmail.com> References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F2012.2020206@gmail.com> Patrick Sabin wrote: > Hello, > > I am interested if there are any python modules, that supports > reversible debugging aka stepping backwards. Any links or ideas would > be helpful, because I am thinking of implementing something like that. > In some cases it would be nice to have something like that, specially in combination with a "normal" debugger, which allows you to change values on the flight, like winpdb. cheers, Stef > Thanks in advance, > Patrick From martin at v.loewis.de Sat Jul 4 05:38:15 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 04 Jul 2009 11:38:15 +0200 Subject: urllib with x509 certs In-Reply-To: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: <4a4f2307$0$8384$9b622d9e@news.freenet.de> > This works Ok! But every time I am asked to enter PEM pass phrase, > which I specified during dividing my .p12 file. > So my question... What should I do to make my code fetch any url > automatically (without asking me every time to enter pass phrase)? > As I understand there is impossible to specify pass phrase while > constructing URLopener. > So what should I do? You can remove the passphrase on the private key, e.g. with the openssl rsa utility. Regards, Martin From banibrata.dutta at gmail.com Sat Jul 4 05:47:18 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 4 Jul 2009 15:17:18 +0530 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> Message-ID: <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> QuoteGoke Aruna what am saying is reading the ITU info on USSD, is it possible to use python to write the application SS7 with support for TCAP/MAP talking to E1 card to do the ss7 signalling. As Chris mentioned "possible, but probably not easy". AFAIK, not much of what you might need exists in form of existing Python modules, if that's what you were thinking -- and especially in the Open-source form. The only open-source initiative in the SS7 domain that I am aware of, is the openss7 (http://www.openss7.org/), which has 'some' software available. No personal experience though, API's are C/C++ AFAIR, and last I'd checked, the status wasn't 'practically usable' in functionality terms. If they offer functionality of the ITU-T SS7 stack upto the TCAP layer, exposing a C/C++ API to create dialogs, create components, populate and send them etc. (usual TCAP Primitives), then as Chris mentions, you could have Python wrappers (ctypes, swig...). Then, for the MAP layer, which is largely ASN.1 BER encoding/decoding, you'd need such functionality in Python. There are some ASN.1 BER codec implementations having Python API/bindings, but AFAIK, they are largely domain/application specific (s.a. for SNMP), and not generic or robust enough. Given the API's however, writing any application s.a. for performing some "business-logic" on receiving or before sending USSD / SMS message in Python, that's a no-brainer ! :-) If however, you want to implement the whole SS7 stack in Python -- everything that Chris wrote is accurate, and that's potentially (and the most obvious) way you'd have to implement it. -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Sat Jul 4 05:50:18 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 02:50:18 -0700 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> Message-ID: <50697b2c0907040250o8e6d9baqc1b368c2d0c547ff@mail.gmail.com> On Sat, Jul 4, 2009 at 2:47 AM, Banibrata Dutta wrote: > QuoteGoke Aruna >> >> what am saying is reading the ITU info on USSD, is it possible to use >> python >> >> to write the application SS7 with support for TCAP/MAP talking to E1 card >> to do the ss7 signalling. > > As Chris mentioned "possible, but probably not easy". AFAIK, not much of s/Chris/Dennis/ Credit where credit is due. I merely gave a PyPI link. Cheers, Chris -- http://blog.rebertia.com From banibrata.dutta at gmail.com Sat Jul 4 05:53:19 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 4 Jul 2009 15:23:19 +0530 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> Message-ID: <3de8e1f70907040253r60211297o8dbea679d3f5273a@mail.gmail.com> BTW, if you just want to be able to send/receive SMS's in a Python application, there are several alternatives. 1. Most operators, or 3rd party bulk-SMS vendors (who resell SMS capacity from operators at a premium but in smaller bundles than what an operator would sell), offer -- a. HTTP based mechanism to send/receive messages b. SMPP protocol. OpenSMPP has a C API, and you could create Python wrappers for same. 2. For very low volume application, you could use the Serial (or Serial emulation over USB/Bluetooth) way, i.e. using "AT" commandset of a tethered GSM mobile (with inbuilt GSM modem), or a separate GSM-modem (PCMCIA, USB,...) via Python, to send / receive SMS's. On Sat, Jul 4, 2009 at 3:17 PM, Banibrata Dutta wrote: > QuoteGoke Aruna > > what am saying is reading the ITU info on USSD, is it possible to use python > > to write the application SS7 with support for TCAP/MAP talking to E1 card to do the ss7 signalling. > > As Chris mentioned "possible, but probably not easy". AFAIK, not much of > what you might need exists in form of existing Python modules, if that's > what you were thinking -- and especially in the Open-source form. The only > open-source initiative in the SS7 domain that I am aware of, is the openss7 > (http://www.openss7.org/), which has 'some' software available. No > personal experience though, API's are C/C++ AFAIR, and last I'd checked, the > status wasn't 'practically usable' in functionality terms. If they offer > functionality of the ITU-T SS7 stack upto the TCAP layer, exposing a C/C++ > API to create dialogs, create components, populate and send them etc. (usual > TCAP Primitives), then as Chris mentions, you could have Python wrappers > (ctypes, swig...). Then, for the MAP layer, which is largely ASN.1 BER > encoding/decoding, you'd need such functionality in Python. There are some > ASN.1 BER codec implementations having Python API/bindings, but AFAIK, they > are largely domain/application specific (s.a. for SNMP), and not generic or > robust enough. Given the API's however, writing any application s.a. for > performing some "business-logic" on receiving or before sending USSD / SMS > message in Python, that's a no-brainer ! :-) > If however, you want to implement the whole SS7 stack in Python -- > everything that Chris wrote is accurate, and that's potentially (and the > most obvious) way you'd have to implement it. > > -- > regards, > Banibrata > http://www.linkedin.com/in/bdutta > -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From banibrata.dutta at gmail.com Sat Jul 4 05:54:08 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Sat, 4 Jul 2009 15:24:08 +0530 Subject: is it possible to write USSD / SMS /SS7 apps in python In-Reply-To: <50697b2c0907040250o8e6d9baqc1b368c2d0c547ff@mail.gmail.com> References: <4A4DC768.40604@gmail.com> <50697b2c0907030221i5fa25134s6905ac9c2da39358@mail.gmail.com> <30ef32480907030231q3d02564dse0a5ecad3810805@mail.gmail.com> <3de8e1f70907040247t50e447feu6ed3ec5d5887e79b@mail.gmail.com> <50697b2c0907040250o8e6d9baqc1b368c2d0c547ff@mail.gmail.com> Message-ID: <3de8e1f70907040254ucc68ff3l65e791b0457678aa@mail.gmail.com> On Sat, Jul 4, 2009 at 3:20 PM, Chris Rebert wrote: > On Sat, Jul 4, 2009 at 2:47 AM, Banibrata > Dutta wrote: > > QuoteGoke Aruna > >> > >> what am saying is reading the ITU info on USSD, is it possible to use > >> python > >> > >> to write the application SS7 with support for TCAP/MAP talking to E1 > card > >> to do the ss7 signalling. > > > > As Chris mentioned "possible, but probably not easy". AFAIK, not much of > > s/Chris/Dennis/ > > Credit where credit is due. I merely gave a PyPI link. > Indeed. Thanks Chris, sorry Dennis. -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From vilya.harvey at gmail.com Sat Jul 4 05:55:44 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sat, 4 Jul 2009 10:55:44 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> Message-ID: <6aef848f0907040255y296c5ea1n2fff44c859cf12ab@mail.gmail.com> 2009/7/4 Andre Engels : > On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >> Currently I need to find the most common elements in thousands of >> arrays within one large array (arround 2 million instances with ~70k >> unique elements) >> >> so I set up a dictionary to handle the counting so when I am >> iterating ?I up the count on the corrosponding dictionary element. I >> then iterate through the dictionary and find the 25 most common >> elements. >> >> the elements are initially held in a array within an array. so I am am >> just trying to find the most common elements between all the arrays >> contained in one large array. >> my current code looks something like this: >> d = {} >> for arr in my_array: >> -----for i in arr: >> #elements are numpy integers and thus are not accepted as dictionary >> keys >> -----------d[int(i)]=d.get(int(i),0)+1 >> >> then I filter things down. but with my algorithm that only takes about >> 1 sec so I dont need to show it here since that isnt the problem. >> >> >> But there has to be something better. I have to do this many many >> times and it seems silly to iterate through 2 million things just to >> get 25. The element IDs are integers and are currently being held in >> numpy arrays in a larger array. this ID is what makes up the key to >> the dictionary. >> >> ?It currently takes about 5 seconds to accomplish this with my current >> algorithm. >> >> So does anyone know the best solution or algorithm? I think the trick >> lies in matrix intersections but I do not know. > > There's no better algorithm for the general case. No method of > checking the matrices using less than 2000000-x look-ups will ensure > you that there's not a new value with x occurences lurking somewhere. Try flattening the arrays into a single large array & sorting it. Then you can just iterate over the large array counting as you go; you only ever have to insert into the dict once for each value and there's no lookups in the dict. I don't know numpy, so there's probably a more efficient way to write this, but this should show what I'm talking about: big_arr = sorted(reduce(list.__add__, my_array, [])) counts = {} last_val = big_arr[0] count = 0 for val in big_arr: if val == last_val: count += 1 else: counts[last_val] = count count = 0 last_val = val counts[last_val] = count # to get the count for the last value. If flattening the arrays isn't practical, you may still get some improvements by sorting them individually and applying the same principle to each of them: counts = {} for arr in my_array: sorted_arr = sorted(arr) last_val = sorted_arr[0] count = 0 for val in sorted_arr: if val == last_val: count += 1 else: counts[last_val] = counts.get(last_val, 0) + count count = 0 last_val = val counts[last_val] = counts.get(last_val, 0) + count Hope that helps... Vil. From parikshatdubey at gmail.com Sat Jul 4 06:06:11 2009 From: parikshatdubey at gmail.com (Parikshat Dubey) Date: Sat, 4 Jul 2009 15:36:11 +0530 Subject: Calling C or C++ Functions inside Python script Message-ID: Hi, Is this possible to call C functions(inbuilt or user defined) inside python script.If yes, please illustrate how it can be done. Thanks, Parikshat Dubey -- -----Fear not the path of truth, for the lack of people walking on it. ----Practice yourself, for heaven's sake, in little things; and thence proceed to greater. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Lacrima.Maxim at gmail.com Sat Jul 4 06:08:40 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Sat, 4 Jul 2009 03:08:40 -0700 (PDT) Subject: urllib with x509 certs References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: On Jul 4, 11:24?am, Chris Rebert wrote: > On Sat, Jul 4, 2009 at 1:12 AM, Lacrima wrote: > > Hello! > > > I am trying to use urllib to fetch some internet resources, using my > > client x509 certificate. > > I have divided my .p12 file into mykey.key and mycert.cer files. > > Then I use following approach: > >>>> import urllib > >>>> url = 'https://example.com' > >>>> xml = ''' > > ... somexml > > ''' > >>>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') > >>>> f = opener.open(url, xml) > > > This works Ok! But every time I am asked to enter PEM pass phrase, > > which I specified during dividing my .p12 file. > > So my question... What should I do to make my code fetch any url > > automatically (without asking me every time to enter pass phrase)? > > As I understand there is impossible to specify pass phrase while > > constructing URLopener. > > So what should I do? > > Subclass FancyURLopener > [http://docs.python.org/library/urllib.html#urllib.FancyURLopener], > overriding the prompt_user_passwd() method > [http://docs.python.org/library/urllib.html#urllib.FancyURLopener.prom...]. > Then use an instance of your subclass instead of URLopener. > > Cheers, > Chris > --http://blog.rebertia.com Hi Chris, Thanks for your quick reply. According to docs the return value of prompt_user_passwd() method should be a tuple (user, password), but there is no user when authenticating with certificate. So how should I use this method? This doesn't work: >>> import urllib >>> class MyOpener(urllib.FancyURLopener): ... def prompt_user_passwd(self, host, realm): ... return ('password') ... With regards, Max From Lacrima.Maxim at gmail.com Sat Jul 4 06:14:17 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Sat, 4 Jul 2009 03:14:17 -0700 (PDT) Subject: urllib with x509 certs References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> <4a4f2307$0$8384$9b622d9e@news.freenet.de> Message-ID: On Jul 4, 12:38?pm, "Martin v. L?wis" wrote: > > This works Ok! But every time I am asked to enter PEM pass phrase, > > which I specified during dividing my .p12 file. > > So my question... What should I do to make my code fetch any url > > automatically (without asking me every time to enter pass phrase)? > > As I understand there is impossible to specify pass phrase while > > constructing URLopener. > > So what should I do? > > You can remove the passphrase on the private key, e.g. with the > openssl rsa utility. > > Regards, > Martin Hi Martin! Thanks for the reply. I want my key to be as secure as possible. So I will remove pass phrase if only there is no other possibility to go through authentication. With regards, Max From clp2 at rebertia.com Sat Jul 4 06:27:46 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 03:27:46 -0700 Subject: urllib with x509 certs In-Reply-To: References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> Message-ID: <50697b2c0907040327w23b7f0eal13934bc8d33c7231@mail.gmail.com> 2009/7/4 Lacrima : > On Jul 4, 11:24?am, Chris Rebert wrote: >> On Sat, Jul 4, 2009 at 1:12 AM, Lacrima wrote: >> > Hello! >> >> > I am trying to use urllib to fetch some internet resources, using my >> > client x509 certificate. >> > I have divided my .p12 file into mykey.key and mycert.cer files. >> > Then I use following approach: >> >>>> import urllib >> >>>> url = 'https://example.com' >> >>>> xml = ''' >> > ... somexml >> > ''' >> >>>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer') >> >>>> f = opener.open(url, xml) >> >> > This works Ok! But every time I am asked to enter PEM pass phrase, >> > which I specified during dividing my .p12 file. >> > So my question... What should I do to make my code fetch any url >> > automatically (without asking me every time to enter pass phrase)? >> > As I understand there is impossible to specify pass phrase while >> > constructing URLopener. >> > So what should I do? >> >> Subclass FancyURLopener >> [http://docs.python.org/library/urllib.html#urllib.FancyURLopener], >> overriding the prompt_user_passwd() method >> [http://docs.python.org/library/urllib.html#urllib.FancyURLopener.prom...]. >> Then use an instance of your subclass instead of URLopener. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > Hi Chris, > Thanks for your quick reply. > According to docs the return value of prompt_user_passwd() method > should be a tuple (user, password), but there is no user when > authenticating with certificate. So how should I use this method? This > doesn't work: >>>> import urllib >>>> class MyOpener(urllib.FancyURLopener): > ... ? ? ?def prompt_user_passwd(self, host, realm): > ... ? ? ? ? ?return ('password') Only a guess: def prompt_user_passwd(self, host, realm): return ('', 'password') Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Sat Jul 4 06:29:30 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 4 Jul 2009 03:29:30 -0700 Subject: Calling C or C++ Functions inside Python script In-Reply-To: References: Message-ID: <50697b2c0907040329w5085b9c9xa8d6a2aaae9667de@mail.gmail.com> On Sat, Jul 4, 2009 at 3:06 AM, Parikshat Dubey wrote: > Hi, > > Is this possible to call C functions(inbuilt or user defined) inside python > script.If yes, please illustrate how it can be done. The `ctypes` module: http://docs.python.org/library/ctypes.html Note: does not work w/ C++ directly. Cheers, Chris -- http://blog.rebertia.com From martin at v.loewis.de Sat Jul 4 07:09:51 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 04 Jul 2009 13:09:51 +0200 Subject: urllib with x509 certs In-Reply-To: References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> <4a4f2307$0$8384$9b622d9e@news.freenet.de> Message-ID: <4A4F387F.7050804@v.loewis.de> > Thanks for the reply. I want my key to be as secure as possible. So I > will remove pass phrase if only there is no other possibility to go > through authentication. And you put the passphrase into the source code instead? How does it make that more secure? Regards, Martin From larudwer at freenet.de Sat Jul 4 08:26:27 2009 From: larudwer at freenet.de (larudwer) Date: Sat, 4 Jul 2009 14:26:27 +0200 Subject: psyco V2 beta2 benchmark Message-ID: just out of curiosity i've downloaded the latest Version of Psyco V2 Beta 2 and run the benchmarks against the old Version of psyco 1.6 Because it might be of common interest, i am posting the results here. My machine is a Pentium D 3.2 Ghz running Windows XP SP 3 and Python 2.6.2. Psyco V2 was built with 4.3.3-tdm-1 mingw32 with optimisation flags changed to -O3 Benchmark | avg. Base time | psyco 1.6 time | psyco 2.0 time | ratio | possible error +- time_anyall all_bool_genexp | 2.270 | 2.250 | 2.420 | 0.930 | 8.3 % time_anyall all_bool_listcomp | 3.450 | 1.900 | 1.910 | 0.995 | 0.0 % time_anyall all_genexp | 1.970 | 1.940 | 2.160 | 0.898 | 9.6 % time_anyall all_listcomp | 3.485 | 1.660 | 1.660 | 1.000 | 1.4 % time_anyall all_loop | 0.665 | 0.090 | 0.090 | 1.000 | 4.4 % time_anyall any_bool_genexp | 2.215 | 2.130 | 2.340 | 0.910 | 10.0 % time_anyall any_bool_listcomp | 3.620 | 1.930 | 1.940 | 0.995 | 9.0 % time_anyall any_genexp | 1.985 | 1.920 | 2.180 | 0.881 | 10.1 % time_anyall any_listcomp | 3.360 | 1.680 | 1.680 | 1.000 | 8.0 % time_anyall any_loop | 0.660 | 0.090 | 0.090 | 1.000 | 3.0 % time_builtins chr(i) | 2.420 | 0.010 | 0.010 | 1.000 | 0.0 % time_builtins hash(i) | 1.280 | 0.370 | 0.080 | 4.625 | 8.1 % time_builtins int(round(f)) | 2.635 | 1.510 | 1.120 | 1.348 | 0.4 % time_builtins min | 0.535 | 0.520 | 0.120 | 4.333 | 1.9 % time_builtins min_kw | 4.430 | 4.400 | 0.160 | 27.500 | 0.5 % time_builtins ord(i) | 0.320 | 0.000 | 0.000 | 1.000 | 6.5 % time_builtins pow | 0.345 | 0.230 | 0.150 | 1.533 | 2.9 % time_builtins reduce | 0.735 | 0.710 | 0.020 | 35.498 | 1.4 % time_builtins round(f) | 1.720 | 0.890 | 0.400 | 2.225 | 1.2 % time_builtins sums | 0.180 | 0.180 | 0.100 | 1.800 | 0.0 % time_fib matrix | 0.425 | 0.360 | 0.360 | 1.000 | 2.3 % time_fib recursive | 0.000 | 0.000 | 0.000 | 1.000 | 0.0 % time_fib takahashi | 0.410 | 0.320 | 0.330 | 0.970 | 0.0 % time_generators call next just many times | 0.900 | 0.630 | 0.970 | 0.649 | 4.3 % time_generators iterate just many times | 0.660 | 0.550 | 0.950 | 0.579 | 3.1 % time_generators send and loop 1000 | 2.805 | 2.540 | 0.060 | 42.333 | 9.8 % time_generators send call loop 1000 | 2.505 | 2.940 | 0.060 | 48.999 | 10.9 % time_generators send just many times | 1.280 | 0.590 | 0.980 | 0.602 | 3.1 % time_iter iter | 1.490 | 0.590 | 0.440 | 1.341 | 5.5 % time_math floats | 2.910 | 1.500 | 1.630 | 0.920 | 0.7 % time_properties method_get | 0.935 | 0.120 | 0.130 | 0.923 | 1.1 % time_properties method_set | 1.005 | 0.170 | 0.180 | 0.944 | 1.0 % time_properties property_get | 0.960 | 0.740 | 0.100 | 7.400 | 2.1 % time_properties property_set | 1.020 | 0.920 | 0.930 | 0.989 | 0.0 % time_properties pyproperty_get | 1.535 | 1.310 | 0.140 | 9.357 | 0.7 % time_properties pyproperty_set | 1.030 | 0.920 | 0.930 | 0.989 | 2.0 % time_subdist subdist(i) | 3.665 | 1.640 | 6.140 | 0.267 | 0.8 % time_sums rounding | 0.800 | 0.790 | 0.810 | 0.975 | 2.5 % Running new timings with C:\Programme\GNU\python26\lib\site-packages\psyco\_psyco.pyd Sat Jul 04 12:09:07 2009 time_anyall : all_bool_genexp plain: 2.36 psyco: 2.42 ratio: 0.97 time_anyall : all_bool_listcomp plain: 3.45 psyco: 1.91 ratio: 1.80 time_anyall : all_genexp plain: 2.06 psyco: 2.16 ratio: 0.95 time_anyall : all_listcomp plain: 3.51 psyco: 1.66 ratio: 2.11 time_anyall : all_loop plain: 0.65 psyco: 0.09 ratio: 7.03 time_anyall : any_bool_genexp plain: 2.32 psyco: 2.34 ratio: 0.99 time_anyall : any_bool_listcomp plain: 3.45 psyco: 1.94 ratio: 1.78 time_anyall : any_genexp plain: 2.08 psyco: 2.18 ratio: 0.96 time_anyall : any_listcomp plain: 3.49 psyco: 1.68 ratio: 2.08 time_anyall : any_loop plain: 0.65 psyco: 0.09 ratio: 7.05 time_builtins : chr(i) plain: 2.42 psyco: 0.01 ratio: 197.40 time_builtins : hash(i) plain: 1.33 psyco: 0.08 ratio: 17.69 time_builtins : int(round(f)) plain: 2.63 psyco: 1.12 ratio: 2.36 time_builtins : min plain: 0.53 psyco: 0.12 ratio: 4.28 time_builtins : min_kw plain: 4.44 psyco: 0.16 ratio: 28.58 time_builtins : ord(i) plain: 0.33 psyco: 0.00 ratio: 123.57 time_builtins : pow plain: 0.34 psyco: 0.15 ratio: 2.24 time_builtins : reduce plain: 0.74 psyco: 0.02 ratio: 40.93 time_builtins : round(f) plain: 1.73 psyco: 0.40 ratio: 4.38 time_builtins : sums plain: 0.18 psyco: 0.10 ratio: 1.80 time_fib : matrix plain: 0.42 psyco: 0.36 ratio: 1.18 time_fib : recursive plain: 0.00 psyco: 0.00 ratio: 19.45 time_fib : takahashi plain: 0.41 psyco: 0.33 ratio: 1.25 time_generators: send call loop 1000 plain: 2.36 psyco: 0.06 ratio: 41.30 time_generators: send and loop 1000 plain: 2.66 psyco: 0.06 ratio: 46.69 time_generators: send just many times plain: 1.26 psyco: 0.98 ratio: 1.29 time_generators: iterate just many times plain: 0.67 psyco: 0.95 ratio: 0.70 time_generators: call next just many times plain: 0.88 psyco: 0.97 ratio: 0.91 time_iter : iter plain: 1.53 psyco: 0.44 ratio: 3.47 time_math : floats plain: 2.90 psyco: 1.63 ratio: 1.78 time_properties: method_get plain: 0.93 psyco: 0.13 ratio: 7.22 time_properties: method_set plain: 1.00 psyco: 0.18 ratio: 5.61 time_properties: property_get plain: 0.95 psyco: 0.10 ratio: 9.26 time_properties: property_set plain: 1.02 psyco: 0.93 ratio: 1.09 time_properties: pyproperty_get plain: 1.54 psyco: 0.14 ratio: 10.80 time_properties: pyproperty_set plain: 1.04 psyco: 0.93 ratio: 1.12 time_subdist : subdist(i) plain: 3.68 psyco: 6.14 ratio: 0.60 time_sums : rounding plain: 0.79 psyco: 0.81 ratio: 0.98 Running new timings with original psyco time_anyall : all_bool_genexp plain: 2.18 psyco: 2.25 ratio: 0.97 time_anyall : all_bool_listcomp plain: 3.45 psyco: 1.90 ratio: 1.82 time_anyall : all_genexp plain: 1.88 psyco: 1.94 ratio: 0.97 time_anyall : all_listcomp plain: 3.46 psyco: 1.66 ratio: 2.09 time_anyall : all_loop plain: 0.68 psyco: 0.09 ratio: 7.34 time_anyall : any_bool_genexp plain: 2.11 psyco: 2.13 ratio: 0.99 time_anyall : any_bool_listcomp plain: 3.79 psyco: 1.93 ratio: 1.96 time_anyall : any_genexp plain: 1.89 psyco: 1.92 ratio: 0.98 time_anyall : any_listcomp plain: 3.23 psyco: 1.68 ratio: 1.92 time_anyall : any_loop plain: 0.67 psyco: 0.09 ratio: 7.26 time_builtins : chr(i) plain: 2.42 psyco: 0.01 ratio: 197.10 time_builtins : hash(i) plain: 1.23 psyco: 0.37 ratio: 3.30 time_builtins : int(round(f)) plain: 2.64 psyco: 1.51 ratio: 1.75 time_builtins : min plain: 0.54 psyco: 0.52 ratio: 1.03 time_builtins : min_kw plain: 4.42 psyco: 4.40 ratio: 1.00 time_builtins : ord(i) plain: 0.31 psyco: 0.00 ratio: 116.56 time_builtins : pow plain: 0.35 psyco: 0.23 ratio: 1.50 time_builtins : reduce plain: 0.73 psyco: 0.71 ratio: 1.03 time_builtins : round(f) plain: 1.71 psyco: 0.89 ratio: 1.91 time_builtins : sums plain: 0.18 psyco: 0.18 ratio: 1.03 time_fib : matrix plain: 0.43 psyco: 0.36 ratio: 1.19 time_fib : recursive plain: 0.00 psyco: 0.00 ratio: 19.28 time_fib : takahashi plain: 0.41 psyco: 0.32 ratio: 1.27 time_generators: send call loop 1000 plain: 2.65 psyco: 2.94 ratio: 0.90 time_generators: send and loop 1000 plain: 2.95 psyco: 2.54 ratio: 1.16 time_generators: send just many times plain: 1.30 psyco: 0.59 ratio: 2.23 time_generators: iterate just many times plain: 0.65 psyco: 0.55 ratio: 1.17 time_generators: call next just many times plain: 0.92 psyco: 0.63 ratio: 1.45 time_iter : iter plain: 1.45 psyco: 0.59 ratio: 2.47 time_math : floats plain: 2.92 psyco: 1.50 ratio: 1.95 time_properties: method_get plain: 0.94 psyco: 0.12 ratio: 7.88 time_properties: method_set plain: 1.01 psyco: 0.17 ratio: 6.08 time_properties: property_get plain: 0.97 psyco: 0.74 ratio: 1.31 time_properties: property_set plain: 1.02 psyco: 0.92 ratio: 1.12 time_properties: pyproperty_get plain: 1.53 psyco: 1.31 ratio: 1.17 time_properties: pyproperty_set plain: 1.02 psyco: 0.92 ratio: 1.11 time_subdist : subdist(i) plain: 3.65 psyco: 1.64 ratio: 2.23 time_sums : rounding plain: 0.81 psyco: 0.79 ratio: 1.03 From Scott.Daniels at Acm.Org Sat Jul 4 08:28:01 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 05:28:01 -0700 Subject: question of style In-Reply-To: <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> Message-ID: upwestdon wrote: > if not (self.higher and self.lower): > return self.higher or self.lower self.lower = 0 self.higher = 123 ??? More than just None is False From Scott.Daniels at Acm.Org Sat Jul 4 08:31:55 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 05:31:55 -0700 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: Patrick Sabin wrote: > Horace Blegg schrieb: >> You might consider using a VM with 'save-points'. You run the program >> (in a debugger/ida/what have you) to a certain point (logical point >> would be if/ifelse/else statements, etc) and save the VM state. Once >> you've saved, you continue. If you find the path you've taken isn't >> what you are after, you can reload a previous save point and start >> over, trying a different path the next time. > That was my idea to implement it. I thought of taking snapshots of the > current state every time a "unredoable instruction", e.g random number > generation, is done. Remember, storing into a location is destruction. Go over a list of VM instructions and see how many of them are undoable. From tn.pablo at gmail.com Sat Jul 4 09:25:39 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sat, 4 Jul 2009 08:25:39 -0500 Subject: Calling C or C++ Functions inside Python script In-Reply-To: References: Message-ID: On Sat, Jul 4, 2009 at 05:06, Parikshat Dubey wrote: > Hi, > > Is this possible to call C functions(inbuilt or user defined) inside python > script.If yes, please illustrate how it can be done. > Thanks, > Parikshat Dubey >From http://www.swig.org : "SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby. " --- Pablo Torres N. From http Sat Jul 4 09:29:09 2009 From: http (Paul Rubin) Date: 04 Jul 2009 06:29:09 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> Message-ID: <7xws6oa862.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > AFAICT, in order for Maybe and Option to work, you need to have some > kind of static typing system. Am I missing something? I'm not sure. Maybe there could be some kind of syntactic support in a dynamic language (not that I'm suggesting adding that to Python). I've been meaning to look up how Erlang does it. For the limited purpose of regexps that might or might not match, Perl has some special support. A kludgy Python counterpart to Option might be to use a pair (present, x) where present is a bool saying whether the value is available. If present is True, then x is the value. That could make it harder to forget to test the flag before using the value. In the stuff I'm doing, I often find it best to just use a list value, so instead of: y = f(x) if x is not None else None I can just write y = map(f, x) In many cases it later turns out that list really was the natural representation and it ends up growing additional potential elements. I saw some article about database design recently that claimed as a program evolves, all relationships end up becoming many-to-many. It gave as an example, if your program deals with names and addresses, it will eventually have to handle the case where someone has more than one residence address. From davea at ieee.org Sat Jul 4 09:31:44 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 09:31:44 -0400 Subject: question of style In-Reply-To: <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> Message-ID: <4A4F59C0.8030201@ieee.org> upwestdon wrote: > On Jul 2, 1:23 pm, Simon Forman wrote: > >> Hey I was hoping to get your opinions on a sort of minor stylistic >> point. >> These two snippets of code are functionally identical. Which would you >> use and why? >> The first one is easier [for me anyway] to read and understand, but >> slightly less efficient, while the second is [marginally] harder to >> follow but more efficient. >> >> ## First snippet >> >> if self.higher is self.lower is None: return >> if self.lower is None: return self.higher >> if self.higher is None: return self.lower >> >> ## Second snippet >> >> if self.higher is None: >> if self.lower is None: >> return >> return self.lower >> if self.lower is None: >> return self.higher >> >> What do you think? >> >> (One minor point: in the first snippet, the "is None" in the first >> line is superfluous in the context in which it will be used, the only >> time "self.lower is self.higher" will be true is when they are both >> None.) >> > > How about just: > > if not (self.higher and self.lower): > return self.higher or self.lower > > But if self.higher is 0 and self.lower is None, this'll return None, which wasn't the original intent. Without having some documented constraints on the self.higher and lower types and values, it's difficult to simplify further. From neilcrighton at gmail.com Sat Jul 4 09:39:41 2009 From: neilcrighton at gmail.com (Neil Crighton) Date: Sat, 4 Jul 2009 13:39:41 +0000 (UTC) Subject: finding most common elements between thousands of multiple arrays. References: Message-ID: You can join all your arrays into a single big array with concatenate. >>> import numpy as np >>> a = np.concatenate(array_of_arrays) Then count the number of occurrances of each unique element using this trick with searchsorted. This should be pretty fast. >>> a.sort() >>> unique_a = np.unique(a) >>> count = [] >>> for val in unique_a: ... count.append(a.searchsorted(val,side='right') - a.searchsorted(val, side='left')) >>> mostcommonvals = unique_a[np.argsort(count)[-25:]] Neil From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 09:42:06 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 13:42:06 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> Message-ID: <025f4dbf$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: > 2009/7/4 Andre Engels : >> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>> Currently I need to find the most common elements in thousands of >>> arrays within one large array (arround 2 million instances with ~70k >>> unique elements) ... >> There's no better algorithm for the general case. No method of checking >> the matrices using less than 2000000-x look-ups will ensure you that >> there's not a new value with x occurences lurking somewhere. > > Try flattening the arrays into a single large array & sorting it. Then > you can just iterate over the large array counting as you go; you only > ever have to insert into the dict once for each value and there's no > lookups in the dict. You're suggesting to do a whole bunch of work copying 2,000,000 pointers into a single array, then a whole bunch of more work sorting that second array (which is O(N*log N) on average), and then finally iterate over the second array. Sure, that last step will on average involve fewer than O(N) steps, but to get to that point you've already done more work than just iterating over the array-of-arrays in the first place. Now, if you're really lucky, your strategy can be done in fast C code instead of slow Python code, and you might see a speed-up for values of N which aren't too big. But I wouldn't put money on it. Another strategy might be to pre-count elements in each array, as you build or modify them. This will marginally slow down each modification you make to the array, but the payback will be that finding the frequency of any element will be almost instantaneous. -- Steven From davea at ieee.org Sat Jul 4 09:58:39 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 09:58:39 -0400 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: <4A4F600F.8040402@ieee.org> Scott David Daniels wrote: >
Patrick > Sabin wrote: >> Horace Blegg schrieb: >>> You might consider using a VM with 'save-points'. You run the >>> program (in a debugger/ida/what have you) to a certain point >>> (logical point would be if/ifelse/else statements, etc) and save the >>> VM state. Once you've saved, you continue. If you find the path >>> you've taken isn't what you are after, you can reload a previous >>> save point and start over, trying a different path the next time. >> That was my idea to implement it. I thought of taking snapshots of >> the current state every time a "unredoable instruction", e.g random >> number generation, is done. > Remember, storing into a location is destruction. > Go over a list of VM instructions and see how many of them are undoable. > >
> > Read his suggested approach more carefully. He's not "undoing" anything. He's rolling back to the save-point, and then stepping forward to the desired spot. Except for influences outside his control (eg. file system operations), this approach has to work. Even random will work the same, if the generator uses only data that was restored from the save-point. A typical pseudo-random generator works from a seed, and if the seed is restored as part of rolling back, he's fine. If the snapshot is done via VMWare (for example), he's even okay for file operations, except for network and VM-shared files. "I thought of taking snapshots of the current state every time a "unredoable instruction", e.g random number generation, is done. For every other instruction I count the number of instructions done since the last snapshot. So I can go back one instruction by restoring to the previous state and go the number of instructions minus one forward." DaveA From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 10:10:01 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 14:10:01 GMT Subject: Reversible Debugging References: <4A4E4B8B.7090104@gmail.com> Message-ID: <025f544a$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 09:58:39 -0400, Dave Angel wrote: > Read his suggested approach more carefully. He's not "undoing" > anything. He's rolling back to the save-point, and then stepping > forward to the desired spot. Except for influences outside his control > (eg. file system operations), this approach has to work. Is that anything like saying "Except for all the diseases this drug won't cure, it cures everything"? *wink* -- Steven From Scott.Daniels at Acm.Org Sat Jul 4 10:19:48 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 07:19:48 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> Message-ID: <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> Vilya Harvey wrote: > 2009/7/4 Andre Engels : >> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>> Currently I need to find the most common elements in thousands of >>> arrays within one large array (arround 2 million instances with ~70k >>> unique elements)... > Try flattening the arrays into a single large array & sorting it. Then > you can just iterate over the large array counting as you go; you only > ever have to insert into the dict once for each value and there's no > lookups in the dict.... Actually the next step is to maintain a min-heap as you run down the sorted array. Something like: import numpy as np import heapq def frequency(arr): '''Generate frequency-value pairs from a numpy array''' clustered = arr.flatten() # copy (so can safely sort) clustered.sort() # Bring identical values together scanner = iter(clustered) last = scanner.next() count = 1 for el in scanner: if el == last: count += 1 else: yield count, last last = el count = 1 yield count, last def most_frequent(arr, N): '''Return the top N (freq, val) elements in arr''' counted = frequency(arr) # get an iterator for freq-val pairs heap = [] # First, just fill up the array with the first N distinct for i in range(N): try: heap.append(counted.next()) except StopIteration: break # If we run out here, no need for a heap else: # more to go, switch to a min-heap, and replace the least # element every time we find something better heapq.heapify(heap) for pair in counted: if pair > heap[0]: heapq.heapreplace(heap, pair) return sorted(heap, reverse=True) # put most frequent first. --Scott David Daniels Scott.Daniels at Acm.Org From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 10:38:52 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 04 Jul 2009 14:38:52 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> Message-ID: <025f5b0d$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 13:42:06 +0000, Steven D'Aprano wrote: > On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: > >> 2009/7/4 Andre Engels : >>> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>>> Currently I need to find the most common elements in thousands of >>>> arrays within one large array (arround 2 million instances with ~70k >>>> unique elements) > ... >>> There's no better algorithm for the general case. No method of >>> checking the matrices using less than 2000000-x look-ups will ensure >>> you that there's not a new value with x occurences lurking somewhere. >> >> Try flattening the arrays into a single large array & sorting it. Then >> you can just iterate over the large array counting as you go; you only >> ever have to insert into the dict once for each value and there's no >> lookups in the dict. > > You're suggesting to do a whole bunch of work copying 2,000,000 pointers > into a single array, then a whole bunch of more work sorting that second > array (which is O(N*log N) on average), and then finally iterate over > the second array. Sure, that last step will on average involve fewer > than O(N) steps, Er what? Ignore that last comment -- I don't know what I was thinking. You still have to iterate over all N elements, sorted or not. > but to get to that point you've already done more work > than just iterating over the array-of-arrays in the first place. What it does buy you though, as you pointed out, is reducing the number of explicit dict lookups and writes. However, dict lookups and writes are very fast, fast enough that they're used throughout Python. A line like: count += 1 actually is a dict lookup and write. -- Steven From sajmikins at gmail.com Sat Jul 4 10:54:38 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sat, 4 Jul 2009 07:54:38 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> Message-ID: <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> On Jul 4, 2:03?am, upwestdon wrote: > On Jul 2, 1:23?pm, Simon Forman wrote: > > > > > Hey I was hoping to get your opinions on a sort of minor stylistic > > point. > > These two snippets of code are functionally identical. Which would you > > use and why? > > The first one is easier [for me anyway] to read and understand, but > > slightly less efficient, while the second is [marginally] harder to > > follow but more efficient. > > > ## First snippet > > > if self.higher is self.lower is None: return > > if self.lower is None: return self.higher > > if self.higher is None: return self.lower > > > ## Second snippet > > > if self.higher is None: > > ? ? if self.lower is None: > > ? ? ? ? return > > ? ? return self.lower > > if self.lower is None: > > ? ? return self.higher > > > What do you think? > > > (One minor point: in the first snippet, the "is None" in the first > > line is superfluous in the context in which it will be used, the only > > time "self.lower is self.higher" will be true is when they are both > > None.) > > How about just: > > if not (self.higher and self.lower): > ? ? return self.higher or self.lower That would work too in this case, both higher and lower are expected to be either None or an object (that doesn't override the default all- objects-are-true rule.) Thanks, ~Simon From no.email at please.post Sat Jul 4 11:00:00 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 15:00:00 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: In <7xk52p4tgg.fsf at ruckus.brouhaha.com> Paul Rubin writes: >kj writes: >> sense = cmp(func(hi), func(lo)) >> if sense == 0: >> return None >> target_plus = sense * target + epsilon >> target_minus = sense * target - epsilon >> ... >The code looks confusing to me and in some sense incorrect. Suppose >func(hi)==func(lo)==target. In hindsight, I too think that it is incorrect, but for a different reason. I've rewritten it like this: sense = cmp(func(hi), func(lo)) assert sense != 0, "func is not strictly monotonic in [lo, hi]" I regard the very special case of func(hi)==func(lo)==target as pathological (analogous to the fact that a stopped watch is "exactly right" twice a day), and not one I care to support. Thanks for your feedback! kj From Scott.Daniels at Acm.Org Sat Jul 4 11:03:39 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 08:03:39 -0700 Subject: Reversible Debugging In-Reply-To: References: <4A4E4B8B.7090104@gmail.com> Message-ID: Dave Angel wrote: > Scott David Daniels wrote: >> Patrick Sabin wrote: >>> Horace Blegg schrieb: >>>> You might consider using a VM with 'save-points'. You run the >>>> program (in a debugger/ida/what have you) to a certain point >>>> (logical point would be if/ifelse/else statements, etc) and save the >>>> VM state. Once you've saved, you continue. If you find the path >>>> you've taken isn't what you are after, you can reload a previous >>>> save point and start over, trying a different path the next time. >>> That was my idea to implement it. I thought of taking snapshots of >>> the current state every time a "unredoable instruction", e.g random >>> number generation, is done. >> Remember, storing into a location is destruction. >> Go over a list of VM instructions and see how many of them are undoable. > Read his suggested approach more carefully. He's not "undoing" > anything. He's rolling back to the save-point, and then stepping > forward to the desired spot. Right, I did misread "unredoable" as "undoable." However, I suspect a surprising amount of stuff is "unredoable" -- iff the random number generator counts as one of those things. The random number seeder is unredoable with empty args, but running the generator once seeded is predictable (by design). If you don't capture the random number state as part of your "snapshot," _lots_ of C space storage will be in the same class, and you are stuck finding the exceptional "safe to use" cases, rather than the exceptional "unsafe to use." Similarly, system calls about time or _any_ callback (when and where executed) create snapshot points, and I suspect roll forwards will be relatively short. In fact, in some sense the _lack_ of a callback is unredoable. --Scott David Daniels Scott.Daniels at Acm.Org From no.email at please.post Sat Jul 4 11:05:48 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 15:05:48 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Alan G Isaac writes: >> In Alan G Isaac writes: >>> 1. Don't use assertions to test argument values! >On 7/3/2009 12:19 PM kj apparently wrote: >> Out of curiosity, where does this come from? >http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_stmt >"The current code generator emits no code for an assert statement when optimization is requested at compile time." Sorry, this doesn't say anything like "don't use assertions to test argument values". I'm aware of the fact that assertions are silenced under certain circumstances. But where does the principle 1. in your first reply come from? I've never seen it before, and would like to understand the reasoning behind it. kj From vinay_sajip at yahoo.co.uk Sat Jul 4 11:12:36 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Sat, 4 Jul 2009 08:12:36 -0700 (PDT) Subject: ANN: A new version of the Python module which wraps GnuPG has been released. Message-ID: A new version of the Python module which wraps GnuPG has been released. What Does It Do? ================ The gnupg module allows Python programs to make use of the functionality provided by the Gnu Privacy Guard (abbreviated GPG or GnuPG). Using this module, Python programs can encrypt and decrypt data, digitally sign documents and verify digital signatures, manage (generate, list and delete) encryption keys, using proven Public Key Infrastructure (PKI) encryption technology based on OpenPGP. This module is expected to be used with Python versions >= 2.4, as it makes use of the subprocess module which appeared in that version of Python. Development and testing has been carried out on Windows and Ubuntu. This module is a newer version derived from earlier work by Andrew Kuchling, Richard Jones and Steve Traugott. A test suite using unittest is included with the source distribution. Simple usage: >>> import gnupg >>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory') >>> gpg.list_keys() [{ ... 'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2', 'keyid': '197D5DAC68F1AAB2', 'length': '1024', 'type': 'pub', 'uids': ['', 'Gary Gross (A test user) ']}, { ... 'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A', 'keyid': '0C5FEFA7A921FC4A', 'length': '1024', ... 'uids': ['', 'Danny Davis (A test user) ']}] >>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A']) >>> str(encrypted) '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n \nhQIOA/6NHMDTXUwcEAf ... -----END PGP MESSAGE-----\n' >>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret') >>> str(decrypted) 'Hello, world!' >>> signed = gpg.sign("Goodbye, world!", passphrase='secret') >>> verified = verified = gpg.verify(str(signed)) >>> print "Verified" if verified else "Not verified" 'Verified' For more information, visit http://code.google.com/p/python-gnupg/ - as always, your feedback is most welcome (especially bug reports, patches and suggestions for improvement). Enjoy! Cheers Vinay Sajip Red Dove Consultants Ltd. From asm198 at gmail.com Sat Jul 4 11:23:33 2009 From: asm198 at gmail.com (Icarus) Date: Sat, 4 Jul 2009 08:23:33 -0700 (PDT) Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: On Jul 4, 3:21?am, Peter Otten <__pete... at web.de> wrote: > Icarus wrote: > > I'm working on a serial protocol analyzer in python. ?We have an > > application written by someone else in MFC but we need something that > > is cross platform. ?I intended to implement the GUI portion in Tkinter > > but am having trouble. > > > The idea is that I will read messages from the serial port and output > > them to a Tkinter Text object initially. ?Eventually it will have > > other functionality but that's it for the short term. ?I've written > > this little test app to experiment with putting things on the GUI via > > a Queue which is polled by the Tkinter loop. > > > On some machines this code works fine and I get whatever I type in > > displayed in the Text widget. ?On others I get errors like this as > > soon as I start it running. > > > error in background error handler: > > out of stack space (infinite loop?) > > ? ? while executing > > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > > ? ? while execu..." > > > I don't understand why on some machines it works exactly as expected > > and on others it acts the same way Tkinter does when I call functions > > directly from outside the Tkinter thread. ?Does anyone have any > > suggestions? ?The full code as appended below. ?Thanks in advance. > > > [code] > > > import Queue > > > class functionQueue: > > > ? ? def __init__(self, root = None, timeout = 250): > > > ? ? ? ? self.functionQueue = Queue.Queue() > > ? ? ? ? self.root = root > > ? ? ? ? self.timeout = timeout > > > ? ? ? ? if(self.root): > > ? ? ? ? ? ? self.pop_function(root) > > > ? ? def pop_function(self, root = None): > > > ? ? ? ? try: > > ? ? ? ? ? ? funcArgList = self.functionQueue.get(block = False) > > ? ? ? ? except Queue.Empty: > > ? ? ? ? ? ? pass > > ? ? ? ? else: > > ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? funcArgList[0](*funcArgList[1]) > > ? ? ? ? ? ? except: > > ? ? ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? ? ? print "Failed to call function", funcArgList[0] > > ? ? ? ? ? ? ? ? except: > > ? ? ? ? ? ? ? ? ? ? print "Failed to call function" > > > ? ? ? ? if(root): > > ? ? ? ? ? ? root.after(self.timeout, lambda: self.pop_function > > (self.root)) > > > ? ? def add_function(self, function, argList): > > > ? ? ? ? try: > > ? ? ? ? ? ? self.functionQueue.put([function, argList]) > > ? ? ? ? except: > > ? ? ? ? ? ? pass > > > if( __name__ == '__main__'): > > > ? ? import Tkinter > > ? ? import thread > > > ? ? text = Tkinter.Text() > > ? ? text.pack() > > > ? ? myQueue = functionQueue(text, 50) > > > ? ? def gui_loop(): > > ? ? ? ? try: > > ? ? ? ? ? ? text.mainloop() > > ? ? ? ? except: > > ? ? ? ? ? ? import os > > ? ? ? ? ? ? os._exit(1) > > > ? ? thread.start_new_thread(text.mainloop, ()) > > > ? ? while(True): > > ? ? ? ? usrInput = raw_input() > > > ? ? ? ? if(usrInput == "-1"): > > ? ? ? ? ? ? import os > > ? ? ? ? ? ? os._exit(0) > > > ? ? ? ? myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > > ? ? ? ? myQueue.add_function(text.see, ['end']) > > > [/code] > > I can make it work over here by putting the UI into the main thread, as > suggested byhttp://effbot.org/zone/tkinter-threads.htm: > > import Queue > import Tkinter > import threading > > class FunctionQueue: > ? ? # unchanged > > def input_loop(): > ? ? while True: > ? ? ? ? try: > ? ? ? ? ? ? usrInput = raw_input() > ? ? ? ? except EOFError: > ? ? ? ? ? ? break > ? ? ? ? myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > ? ? ? ? myQueue.add_function(text.see, ['end']) > ? ? myQueue.add_function(text.quit, []) > > if __name__ == '__main__': > ? ? text = Tkinter.Text() > ? ? text.pack() > > ? ? myQueue = FunctionQueue(text, 50) > ? ? threading.Thread(target=input_loop).start() > ? ? text.mainloop() > > Peter Peter, thanks for the suggestion. I tried your code exactly on my box and I still get the same results. As soon as I run the script and every time I click on the Text box I get tcl::Bgerror ... just like I mentioned above. I'm fairly certain that I'm not calling Tkinter functions from any other thread but it's acting as though I am as soon as I create the input thread. If I comment out the input loop thread everything is fine but of course that's not terribly useful as a logging box. From tn.pablo at gmail.com Sat Jul 4 11:26:15 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sat, 4 Jul 2009 10:26:15 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On Sat, Jul 4, 2009 at 10:05, kj wrote: >>http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_stmt >>"The current code generator emits no code for an assert statement when optimization is requested at compile time." > > Sorry, this doesn't say anything like "don't use assertions to test > argument values". ?I'm aware of the fact that assertions are silenced > under certain circumstances. ?But where does the principle 1. in > your first reply come from? ?I've never seen it before, and would > like to understand the reasoning behind it. > > kj > -- > http://mail.python.org/mailman/listinfo/python-list > But...if no code is generated for assertions on some occasions, then the parameters would go unchecked, potentially breaking your code in said occasions. -- Pablo Torres N. From aahz at pythoncraft.com Sat Jul 4 11:48:21 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 Jul 2009 08:48:21 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> <7xws6oa862.fsf@ruckus.brouhaha.com> Message-ID: In article <7xws6oa862.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: > >In many cases it later turns out that list really was the natural >representation and it ends up growing additional potential elements. >I saw some article about database design recently that claimed as a >program evolves, all relationships end up becoming many-to-many. It >gave as an example, if your program deals with names and addresses, it >will eventually have to handle the case where someone has more than >one residence address. That's definitely a critical point, and I absolutely agree that this should get rubbed into people's faces. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From dw at botanicus.net Sat Jul 4 11:50:56 2009 From: dw at botanicus.net (David Wilson) Date: Sat, 4 Jul 2009 08:50:56 -0700 (PDT) Subject: Where does setuptools live? Message-ID: I'm trying to create a patch for a diabolical issue I keep running into, but I can't seem to find the setuptools repository. Is it this one? http://svn.python.org/view/sandbox/trunk/setuptools/ It's seen no changes in 9 months. The issue in question is its (ab)use of .svn to directly read working copy information rather than going via the well designed 'svn info -- xml' route, which if done, wouldn't result in setuptools busting every few years, nor require me to downgrade my Subversion installation (OS X packages don't support multiple versions installed at a time), checkout a source tree, use setuptools, then re-upgrade so I can go back to using my other trees. David From davea at ieee.org Sat Jul 4 12:09:34 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 12:09:34 -0400 Subject: Reversible Debugging In-Reply-To: <025f544a$0$20657$c3e8da3@news.astraweb.com> References: <4A4E4B8B.7090104@gmail.com> <025f544a$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4A4F7EBE.4030004@ieee.org> Steven D'Aprano wrote: > On Sat, 04 Jul 2009 09:58:39 -0400, Dave Angel wrote: > > >> Read his suggested approach more carefully. He's not "undoing" >> anything. He's rolling back to the save-point, and then stepping >> forward to the desired spot. Except for influences outside his control >> (eg. file system operations), this approach has to work. >> > > Is that anything like saying "Except for all the diseases this drug won't > cure, it cures everything"? > > *wink* > > > Yeah, pretty close. That's the problem with speaking in concepts, rather than something concrete. Somebody else brought up VM, but that has two meanings these days. The sense I took it (with regard to save-point) was a program like VMWare, where it virtualizes the entire machine. So any program that deals only with the local machine will fit that approach. And yes, I know there are subtleties beyond networks and virtual networks, like time. When you roll the VM back, the time magically stays current. But for a program that deals only with its own process, that tends to just look like some other process suddenly hogged the CPU. Now, if the snapshot is a feature of the Python VM, that's another matter entirely. From amrita at iisermohali.ac.in Sat Jul 4 12:21:12 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 4 Jul 2009 21:51:12 +0530 (IST) Subject: mail Message-ID: <22154.210.212.36.65.1246724472.squirrel@www.iisermohali.ac.in> Hi, I want to know that whether using python programming is it possible to extract chemical shift information about some amino acids of some protein from BMRB(BioMagResBank) or Ref-DB(referenced databank) or not. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From hanooter at gmail.com Sat Jul 4 12:22:48 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 09:22:48 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> Message-ID: <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> OK then. I will try some of the strategies here but I guess things arent looking too good. I need to run this over a dataset that someone pickled. I need to run this 480,000 times so you can see my frustration. So it doesn't need to be "real time" but it would be nice it was done sorting this month. Is there a "bet guess" strategy where it is not 100% accurate but much faster? On Jul 4, 7:38?am, Steven D'Aprano wrote: > On Sat, 04 Jul 2009 13:42:06 +0000, Steven D'Aprano wrote: > > On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: > > >> 2009/7/4 Andre Engels : > >>> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: > >>>> Currently I need to find the most common elements in thousands of > >>>> arrays within one large array (arround 2 million instances with ~70k > >>>> unique elements) > > ... > >>> There's no better algorithm for the general case. No method of > >>> checking the matrices using less than 2000000-x look-ups will ensure > >>> you that there's not a new value with x occurences lurking somewhere. > > >> Try flattening the arrays into a single large array & sorting it. Then > >> you can just iterate over the large array counting as you go; you only > >> ever have to insert into the dict once for each value and there's no > >> lookups in the dict. > > > You're suggesting to do a whole bunch of work copying 2,000,000 pointers > > into a single array, then a whole bunch of more work sorting that second > > array (which is O(N*log N) on average), and then finally iterate over > > the second array. Sure, that last step will on average involve fewer > > than O(N) steps, > > Er what? > > Ignore that last comment -- I don't know what I was thinking. You still > have to iterate over all N elements, sorted or not. > > > but to get to that point you've already done more work > > than just iterating over the array-of-arrays in the first place. > > What it does buy you though, as you pointed out, is reducing the number > of explicit dict lookups and writes. However, dict lookups and writes are > very fast, fast enough that they're used throughout Python. A line like: > > count += 1 > > actually is a dict lookup and write. > > -- > Steven From __peter__ at web.de Sat Jul 4 12:24:06 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 04 Jul 2009 18:24:06 +0200 Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: Icarus wrote: > On Jul 4, 3:21 am, Peter Otten <__pete... at web.de> wrote: >> Icarus wrote: >> > I'm working on a serial protocol analyzer in python. We have an >> > application written by someone else in MFC but we need something that >> > is cross platform. I intended to implement the GUI portion in Tkinter >> > but am having trouble. >> >> > The idea is that I will read messages from the serial port and output >> > them to a Tkinter Text object initially. Eventually it will have >> > other functionality but that's it for the short term. I've written >> > this little test app to experiment with putting things on the GUI via >> > a Queue which is polled by the Tkinter loop. >> >> > On some machines this code works fine and I get whatever I type in >> > displayed in the Text widget. On others I get errors like this as >> > soon as I start it running. >> >> > error in background error handler: >> > out of stack space (infinite loop?) >> > while executing >> > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level >> > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) >> > while execu..." >> >> > I don't understand why on some machines it works exactly as expected >> > and on others it acts the same way Tkinter does when I call functions >> > directly from outside the Tkinter thread. Does anyone have any >> > suggestions? The full code as appended below. Thanks in advance. >> >> > [code] >> >> > import Queue >> >> > class functionQueue: >> >> > def __init__(self, root = None, timeout = 250): >> >> > self.functionQueue = Queue.Queue() >> > self.root = root >> > self.timeout = timeout >> >> > if(self.root): >> > self.pop_function(root) >> >> > def pop_function(self, root = None): >> >> > try: >> > funcArgList = self.functionQueue.get(block = False) >> > except Queue.Empty: >> > pass >> > else: >> > try: >> > funcArgList[0](*funcArgList[1]) >> > except: >> > try: >> > print "Failed to call function", funcArgList[0] >> > except: >> > print "Failed to call function" >> >> > if(root): >> > root.after(self.timeout, lambda: self.pop_function >> > (self.root)) >> >> > def add_function(self, function, argList): >> >> > try: >> > self.functionQueue.put([function, argList]) >> > except: >> > pass >> >> > if( __name__ == '__main__'): >> >> > import Tkinter >> > import thread >> >> > text = Tkinter.Text() >> > text.pack() >> >> > myQueue = functionQueue(text, 50) >> >> > def gui_loop(): >> > try: >> > text.mainloop() >> > except: >> > import os >> > os._exit(1) >> >> > thread.start_new_thread(text.mainloop, ()) >> >> > while(True): >> > usrInput = raw_input() >> >> > if(usrInput == "-1"): >> > import os >> > os._exit(0) >> >> > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) >> > myQueue.add_function(text.see, ['end']) >> >> > [/code] >> >> I can make it work over here by putting the UI into the main thread, as >> suggested byhttp://effbot.org/zone/tkinter-threads.htm: >> >> import Queue >> import Tkinter >> import threading >> >> class FunctionQueue: >> # unchanged >> >> def input_loop(): >> while True: >> try: >> usrInput = raw_input() >> except EOFError: >> break >> myQueue.add_function(text.insert, ['end', usrInput + "\n"]) >> myQueue.add_function(text.see, ['end']) >> myQueue.add_function(text.quit, []) >> >> if __name__ == '__main__': >> text = Tkinter.Text() >> text.pack() >> >> myQueue = FunctionQueue(text, 50) >> threading.Thread(target=input_loop).start() >> text.mainloop() >> >> Peter > > Peter, thanks for the suggestion. I tried your code exactly on my box > and I still get the same results. As soon as I run the script and > every time I click on the Text box I get tcl::Bgerror ... just like I > mentioned above. I'm fairly certain that I'm not calling Tkinter > functions from any other thread but it's acting as though I am as soon > as I create the input thread. > If I comment out the input loop thread everything is fine but of > course that's not terribly useful as a logging box. http://bugs.python.org/issue3835 Could tcl have been built without thread support on the failing machines? Peter From no.email at please.post Sat Jul 4 12:30:10 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 16:30:10 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In "Pablo Torres N." writes: >On Sat, Jul 4, 2009 at 10:05, kj wrote: >>>http://docs.python.org/reference/simple_stmts.html#grammar-token-assert_s= >tmt >>>"The current code generator emits no code for an assert statement when op= >timization is requested at compile time." >> >> Sorry, this doesn't say anything like "don't use assertions to test >> argument values". =C2=A0I'm aware of the fact that assertions are silence= >d >> under certain circumstances. =C2=A0But where does the principle 1. in >> your first reply come from? =C2=A0I've never seen it before, and would >> like to understand the reasoning behind it. >> >> kj >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >But...if no code is generated for assertions on some occasions, then the >parameters would go unchecked, potentially breaking your code in said >occasions. This implies that code that uses *any* assert statement (other than perhaps the trivial and meaningless ones like "assert True") is liable to break, because whatever it is that these assert statements are checking "on some occasions, ... would go unchecked, potentially breaking your code." I'm beginning to think that the original "precept" was simply "cargo cult," i.e. one of those rules that are parroted without being fully understood. As I wrote in my original post, the function I posted was an internal ("helper") function, not to be used outside of the file. This fact was further (ahem) underscored by the leading underscore in the function's name. Under these circumstances, the assert statements seem to me perfectly in keeping with the intended use for them. kj From floris.bruynooghe at gmail.com Sat Jul 4 12:35:47 2009 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Sat, 4 Jul 2009 09:35:47 -0700 (PDT) Subject: Where does setuptools live? References: Message-ID: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> On Jul 4, 4:50?pm, David Wilson wrote: > I'm trying to create a patch for a diabolical issue I keep running > into, but I can't seem to find the setuptools repository. Is it this > one? > > ? ?http://svn.python.org/view/sandbox/trunk/setuptools/ It is, see http://mail.python.org/pipermail/distutils-sig/2009-July/012374.html > It's seen no changes in 9 months. It's setuptools... I'm sure you can find many flamefests on distutils- sig about this. Regards Floris From python at mrabarnett.plus.com Sat Jul 4 12:40:48 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 Jul 2009 17:40:48 +0100 Subject: Problems with using queue in Tkinter application In-Reply-To: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: <4A4F8610.1080202@mrabarnett.plus.com> Icarus wrote: > I'm working on a serial protocol analyzer in python. We have an > application written by someone else in MFC but we need something that > is cross platform. I intended to implement the GUI portion in Tkinter > but am having trouble. > > The idea is that I will read messages from the serial port and output > them to a Tkinter Text object initially. Eventually it will have > other functionality but that's it for the short term. I've written > this little test app to experiment with putting things on the GUI via > a Queue which is polled by the Tkinter loop. > > On some machines this code works fine and I get whatever I type in > displayed in the Text widget. On others I get errors like this as > soon as I start it running. > > error in background error handler: > out of stack space (infinite loop?) > while executing > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > while execu..." > > > I don't understand why on some machines it works exactly as expected > and on others it acts the same way Tkinter does when I call functions > directly from outside the Tkinter thread. Does anyone have any > suggestions? The full code as appended below. Thanks in advance. > > [code] > > import Queue > > class functionQueue: > > def __init__(self, root = None, timeout = 250): > > self.functionQueue = Queue.Queue() > self.root = root > self.timeout = timeout > > if(self.root): > self.pop_function(root) > > > def pop_function(self, root = None): > > try: > funcArgList = self.functionQueue.get(block = False) > except Queue.Empty: > pass > else: > try: > funcArgList[0](*funcArgList[1]) > except: > try: > print "Failed to call function", funcArgList[0] > except: > print "Failed to call function" > > if(root): > root.after(self.timeout, lambda: self.pop_function > (self.root)) > > def add_function(self, function, argList): > > try: > self.functionQueue.put([function, argList]) > except: > pass > > > if( __name__ == '__main__'): > > import Tkinter > import thread > > text = Tkinter.Text() > text.pack() > > myQueue = functionQueue(text, 50) > > def gui_loop(): > try: > text.mainloop() > except: > import os > os._exit(1) > > thread.start_new_thread(text.mainloop, ()) > > while(True): > usrInput = raw_input() > > if(usrInput == "-1"): > import os > os._exit(0) > > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > myQueue.add_function(text.see, ['end']) > > [/code] > Only an idea, but: myQueue = functionQueue(text, 50) runs in the main thread, so the pop_function method is calling Tkinter methods in the main thread every 50ms. But: thread.start_new_thread(text.mainloop, ()) is running the Tkinter main loop in another thread. You might be experiencing a race condition, so different timings on different machines might cause a problem to appear sooner on one than another. From fabiofz at gmail.com Sat Jul 4 13:17:31 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Sat, 4 Jul 2009 14:17:31 -0300 Subject: Python debugger In-Reply-To: <740981.83964.qm@web7901.mail.in.yahoo.com> References: <740981.83964.qm@web7901.mail.in.yahoo.com> Message-ID: > Hi, > Could you suggest some python debuggers? > > Thanks, > Srini Pydev has a debugger that supports the common debugger features (watches, multiple threads, breakpoints, conditional breakpoints, step in, out, etc -- http://fabioz.com/pydev/manual_adv_debugger.html ), and pydev extensions adds to that a remote debugger ( http://fabioz.com/pydev/manual_adv_remote_debugger.html ) and a console ( http://fabioz.com/pydev/manual_adv_debug_console.html ). Cheers, Fabio From patrick.just4fun at gmail.com Sat Jul 4 13:21:14 2009 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Sat, 04 Jul 2009 19:21:14 +0200 Subject: Reversible Debugging In-Reply-To: <4A4F7EBE.4030004@ieee.org> References: <4A4E4B8B.7090104@gmail.com> <025f544a$0$20657$c3e8da3@news.astraweb.com> <4A4F7EBE.4030004@ieee.org> Message-ID: <4A4F8F8A.4080702@gmail.com> > Now, if the snapshot is a feature of the Python VM, that's another > matter entirely. I thought of taking a snapshot using fork, which creates a copy of the process. It may not be the most performant, but it should be quite portable. Of course there are some issues with multithreading/multiprocessing. If someone has another idea of taking a snapshot let me know. Using VMWare is not a very elegant way in my opinion. From nagle at animats.com Sat Jul 4 13:33:15 2009 From: nagle at animats.com (John Nagle) Date: Sat, 04 Jul 2009 10:33:15 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. Message-ID: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> As an example of code that really needs to run fast, but is speed-limited by Python's limitations, see "tokenizer.py" in http://code.google.com/p/html5lib/ This is a parser for HTML 5, a piece of code that will be needed in many places and will process large amounts of data. It's written entirely in Python. Take a look at how much work has to be performed per character. This is a good test for Python implementation bottlenecks. Run that tokenizer on HTML, and see where the time goes. ("It should be written in C" is not an acceptable answer.) Python doesn't have a "switch" or "case" statement, and when you need a state machine with many states, that makes for painful, slow code. There's a comment in the code that it would be useful to run a few billion lines of HTML through an instrumented version of the parser to decide in which order the IF statements should be executed. You shouldn't have to do that. Yes, I've read PEP 3103. The big problem is the difficulty of figuring out what's a constant and what might change. If all the cases are constants, case statements are easy. But in Python, the compiler can't tell. Parsers have many named compile-time constants. Python doesn't support named compile-time constants, and this is one of the places where we have to pay the bill for that limitation. Something to think about when you need three more racks of servers because the HTML parser is slow. John Nagle From jeremy.cowles at gmail.com Sat Jul 4 13:44:57 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Sat, 4 Jul 2009 10:44:57 -0700 Subject: Zipped Python? Message-ID: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> Hi all, I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. no install). My intentions are to use it for a distributed computing project (PyMW) where there is no guarantee that Python is installed on the worker machines and an interactive install is out of the question. I've been looking around, but I haven't been able to turn anything useful up so far. Any help would be appreciated. -- Jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: From vilya.harvey at gmail.com Sat Jul 4 13:53:58 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sat, 4 Jul 2009 18:53:58 +0100 Subject: Reversible Debugging In-Reply-To: <4A4F8F8A.4080702@gmail.com> References: <4A4E4B8B.7090104@gmail.com> <025f544a$0$20657$c3e8da3@news.astraweb.com> <4A4F7EBE.4030004@ieee.org> <4A4F8F8A.4080702@gmail.com> Message-ID: <6aef848f0907041053j24d646a4rd6eca49e44a4b0d7@mail.gmail.com> 2009/7/4 Patrick Sabin : > If someone has another idea of taking a snapshot let me know. Using VMWare > is not a > very elegant way in my opinion. Someone implemented the same idea for Java a while ago. They called it "omniscient debugging"; you can find details at http://www.lambdacs.com/debugger/ and a paper about it at http://www.lambdacs.com/debugger/AADEBUG_Mar_03.pdf Another more recent paper on the topic is http://scg.unibe.ch/archive/papers/Lien08bBackInTimeDebugging.pdf I haven't read either of these papers myself, but maybe they'll give you some ideas. Vil. From vilya.harvey at gmail.com Sat Jul 4 13:59:24 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sat, 4 Jul 2009 18:59:24 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <025f5b0d$0$20657$c3e8da3@news.astraweb.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> Message-ID: <6aef848f0907041059n27f88449v16d5fdb4af5c22d2@mail.gmail.com> 2009/7/4 Steven D'Aprano : > On Sat, 04 Jul 2009 13:42:06 +0000, Steven D'Aprano wrote: > >> On Sat, 04 Jul 2009 10:55:44 +0100, Vilya Harvey wrote: >> >>> 2009/7/4 Andre Engels : >>>> On Sat, Jul 4, 2009 at 9:33 AM, mclovin wrote: >>>>> Currently I need to find the most common elements in thousands of >>>>> arrays within one large array (arround 2 million instances with ~70k >>>>> unique elements) >> ... >>>> There's no better algorithm for the general case. No method of >>>> checking the matrices using less than 2000000-x look-ups will ensure >>>> you that there's not a new value with x occurences lurking somewhere. >>> >>> Try flattening the arrays into a single large array & sorting it. Then >>> you can just iterate over the large array counting as you go; you only >>> ever have to insert into the dict once for each value and there's no >>> lookups in the dict. >> >> You're suggesting to do a whole bunch of work copying 2,000,000 pointers >> into a single array, then a whole bunch of more work sorting that second >> array (which is O(N*log N) on average), and then finally iterate over >> the second array. Sure, that last step will on average involve fewer >> than O(N) steps, > > Er what? > > Ignore that last comment -- I don't know what I was thinking. You still > have to iterate over all N elements, sorted or not. > >> but to get to that point you've already done more work >> than just iterating over the array-of-arrays in the first place. > > What it does buy you though, as you pointed out, is reducing the number > of explicit dict lookups and writes. However, dict lookups and writes are > very fast, fast enough that they're used throughout Python. A line like: > > count += 1 > > actually is a dict lookup and write. I did some tests, just to be sure, and you're absolutely right: just creating the flattened list took several hundred (!) times as long as iterating through all the lists in place. Live and learn... Vil. From benjamin.kaplan at case.edu Sat Jul 4 14:03:52 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 4 Jul 2009 14:03:52 -0400 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: On Sat, Jul 4, 2009 at 1:33 PM, John Nagle wrote: > ? As an example of code that really needs to run fast, but is > speed-limited by Python's limitations, see "tokenizer.py" in > > ? ? ? ?http://code.google.com/p/html5lib/ > > This is a parser for HTML 5, a piece of code that will be needed > in many places and will process large amounts of data. It's written > entirely in Python. ?Take a look at how much work has to be performed > per character. > > This is a good test for Python implementation bottlenecks. ?Run > that tokenizer on HTML, and see where the time goes. > > ("It should be written in C" is not an acceptable answer.) > > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. ?There's a comment in the code that it would be useful > to run a few billion lines of HTML through an instrumented version > of the parser to decide in which order the IF statements should be > executed. ?You shouldn't have to do that. > If you're cases are hashable, just use a dict instead of the if chain. Then you get the constant time access to it. def func_a() : ... def func_b(): ... def func_c(): .... case = {"a":func_a, "b":func_b, "c":func_c} case[value]() > Yes, I've read PEP 3103. ?The big problem is the difficulty of figuring > out what's a constant and what might change. ?If all the cases are > constants, > case statements are easy. ?But in Python, the compiler can't tell. > > Parsers have many named compile-time constants. ?Python doesn't support > named compile-time constants, and this is one of the places where we > have to pay the bill for that limitation. > > Something to think about when you need three more racks of servers > because the HTML parser is slow. > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?John Nagle > -- > http://mail.python.org/mailman/listinfo/python-list > From http Sat Jul 4 14:10:48 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:10:48 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> Message-ID: <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Simon Forman writes: > > if not (self.higher and self.lower): > > ? ? return self.higher or self.lower > > That would work too in this case, both higher and lower are expected > to be either None or an object (that doesn't override the default all- > objects-are-true rule.) -1. Objects can support methods like __bool__, __len__ (a tree might use this to compute the number of nodes or something like that), and __nonzero__. If len(object)==0 then the object is treated as false. So that test can fail if a __len__ method gets added to the tree class sometime. Or you might decide you want to support empty trees which would test as false. Anyway, Python's overloading of bool(...) is yet another misfeature and although it's convenient, the "explicit is better than implicit" principle indicates to avoid that sort of trick. From mwilson at the-wire.com Sat Jul 4 14:20:09 2009 From: mwilson at the-wire.com (Mel) Date: Sat, 04 Jul 2009 14:20:09 -0400 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: [ ... ] > Parsers have many named compile-time constants. Python doesn't support > named compile-time constants, and this is one of the places where we > have to pay the bill for that limitation. > > Something to think about when you need three more racks of servers > because the HTML parser is slow. One technique used in such a case is to dispatch different case-handling functions via a dictionary lookup. Mel. From http Sat Jul 4 14:20:20 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:20:20 -0700 Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: <7x4otsux7f.fsf@ruckus.brouhaha.com> kj writes: > sense = cmp(func(hi), func(lo)) > assert sense != 0, "func is not strictly monotonic in [lo, hi]" bisection search usually just requires the function to be continuous and to have its value cross the target somewhere between the endpoints, not be monotonic. > I regard the very special case of func(hi)==func(lo)==target as > pathological (analogous to the fact that a stopped watch is "exactly > right" twice a day), and not one I care to support. I do think you should support that case, under the "do 'nothing' gracefully" principle. Having your equation solver crash if the equation is already solved is like having your sorting function crash if the input is already sorted. E.g. the function should do something reasonable if you end up calling it twice. From http Sat Jul 4 14:29:20 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:29:20 -0700 Subject: Clarity vs. code reuse/generality References: Message-ID: <7xzlbkti7z.fsf@ruckus.brouhaha.com> kj writes: > This implies that code that uses *any* assert statement (other than > perhaps the trivial and meaningless ones like "assert True") is > liable to break, because whatever it is that these assert statements > are checking "on some occasions, ... would go unchecked, potentially > breaking your code." Yes, that implication is absolutely valid. The purpose of assert statements is to debug the code, by checking for conditions that are supposed to be impossible. Unless the program is broken (i.e. the impossible happened), no assert statement should ever trigger. Invalid input data is not considered impossible and doesn't imply a broken program, so assert statements are not the appropriate way to check for it. I like to use a function like def check(condition, msg="data error"): if not condition: raise ValueError, msg ... check (x >= 0, "invalid x") # raises ValueError if x is negative y = sqrt(x) instead. From lie.1296 at gmail.com Sat Jul 4 14:36:38 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 04 Jul 2009 18:36:38 GMT Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: mclovin wrote: > OK then. I will try some of the strategies here but I guess things > arent looking too good. I need to run this over a dataset that someone > pickled. I need to run this 480,000 times so you can see my > frustration. So it doesn't need to be "real time" but it would be nice > it was done sorting this month. > > Is there a "bet guess" strategy where it is not 100% accurate but much > faster? Heuristics? If you don't need 100% accuraccy, you can simply sample 10000 or so element and find the most common element in this small sample space. It should be much faster, though you'll probably need to determine the best cutoff number (too small and you're risking biases, too large and it would be slower). random.sample() might be useful here. From http Sat Jul 4 14:40:19 2009 From: http (Paul Rubin) Date: 04 Jul 2009 11:40:19 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <7xvdm8thpo.fsf@ruckus.brouhaha.com> John Nagle writes: > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. ... > There's a comment in the code that it would be useful > to run a few billion lines of HTML through an instrumented version > of the parser to decide in which order the IF statements should be > executed. You shouldn't have to do that. In that particular program it would probably be better to change those if/elif/elif/else constructs to dictionary lookups. I see the program already does that for some large tables. From python at mrabarnett.plus.com Sat Jul 4 14:43:24 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 Jul 2009 19:43:24 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: <7xzlbkti7z.fsf@ruckus.brouhaha.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <4A4FA2CC.1080105@mrabarnett.plus.com> Paul Rubin wrote: > kj writes: >> This implies that code that uses *any* assert statement (other than >> perhaps the trivial and meaningless ones like "assert True") is >> liable to break, because whatever it is that these assert statements >> are checking "on some occasions, ... would go unchecked, potentially >> breaking your code." > > Yes, that implication is absolutely valid. The purpose of assert > statements is to debug the code, by checking for conditions that are > supposed to be impossible. Unless the program is broken (i.e. the > impossible happened), no assert statement should ever trigger. > Technically these are known as "invariants". An assertion will always be True if the program is bug-free, no matter what the user might throw at it; it's not the same as validation. > Invalid input data is not considered impossible and doesn't imply a > broken program, so assert statements are not the appropriate way to > check for it. I like to use a function like > > def check(condition, msg="data error"): > if not condition: raise ValueError, msg > > ... > check (x >= 0, "invalid x") # raises ValueError if x is negative > y = sqrt(x) > > instead. From alan.isaac at gmail.com Sat Jul 4 14:54:02 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sat, 04 Jul 2009 18:54:02 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On 7/4/2009 12:30 PM kj apparently wrote: > I'm beginning to think that the original "precept" was simply "cargo > cult," i.e. one of those rules that are parroted without being > fully understood. Adopting such a view is of course an alternative to attempting to understand, but perhaps less useful. Alan Isaac PS For additional explanation: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html From grante at visi.com Sat Jul 4 15:15:31 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 04 Jul 2009 14:15:31 -0500 Subject: mail References: Message-ID: On 2009-07-04, amrita at iisermohali.ac.in wrote: > I want to know that whether using python programming is it > possible to extract chemical shift information about some > amino acids of some protein from BMRB(BioMagResBank) or > Ref-DB(referenced databank) or not. Yes. I don't know how easy it would be, but I'm pretty confident it is possible. I'm assuming it is currently possible to do so using some sort of software. If it's currently impossible to do so using other methods (e.g. such information doesn't exist or you don't have access to the dabases), then using Python isn't going cause any miracles to happen. -- Grant From Scott.Daniels at Acm.Org Sat Jul 4 15:51:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 12:51:58 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: mclovin wrote: > OK then. I will try some of the strategies here but I guess things > arent looking too good. I need to run this over a dataset that someone > pickled. I need to run this 480,000 times so you can see my > frustration. So it doesn't need to be "real time" but it would be nice > it was done sorting this month. > > Is there a "bet guess" strategy where it is not 100% accurate but much > faster? Well, I timed a run of a version of mine, and the scan is approx 5X longer than the copy-and-sort. Time arr_of_arr.flatten().sort() to see how quickly the copy and sort happens.So you could try a variant exploiting the following property: If you know the minimum length of a run that will be in the top 25, then the value for each of the most-frequent run entries must show up at positions n * stride and (n + 1) * stride (for some n). That should drastically reduce the scan cost, as long as stride is reasonably large. For my uniformly distributed 0..1024 values in 5M x 5M array, About 2.5 sec to flatten and sort. About 15 sec to run one of my heapish thingies. the least frequency encountered: 24716 so, with stride at sum(flattened[:-stride:stride] == flattened[stride::stride]) == 1000 So there are only 1000 points to investigate. With any distribution other than uniform, that should go _way_ down. So just pull out those points, use bisect to get their frequencies, and feed those results into the heap accumulation. --Scott David Daniels From Scott.Daniels at Acm.Org Sat Jul 4 16:01:00 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 13:01:00 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: <7xzlbkti7z.fsf@ruckus.brouhaha.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Invalid input data is not considered impossible and doesn't imply a > broken program, so assert statements are not the appropriate way to > check for it. I like to use a function like > > def check(condition, msg="data error"): > if not condition: raise ValueError, msg > > ... > check (x >= 0, "invalid x") # raises ValueError if x is negative > y = sqrt(x) And I curse such uses, since I don't get to see the troublesome value, or why it is troublesome. In the above case, y = sqrt(x) at least raises ValueError('math domain error'), which is more information than you are providing. How about: ... if x >= 0: raise ValueError('x = %r not allowed (negative)?' % x) ... --Scott David Daniels Scott.Daniels at Acm.Org From sajmikins at gmail.com Sat Jul 4 16:05:36 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sat, 4 Jul 2009 13:05:36 -0700 (PDT) Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: <0a175b39-f084-4814-92e0-80b6cfce6130@c36g2000yqn.googlegroups.com> On Jul 4, 2:10?pm, Paul Rubin wrote: > Simon Forman writes: > > > if not (self.higher and self.lower): > > > ? ? return self.higher or self.lower > > > That would work too in this case, both higher and lower are expected > > to be either None or an object (that doesn't override the default all- > > objects-are-true rule.) > > -1. Heh heh heh, I figured someone might call me on this, but I stopped short of adding further elucidation to that parenthetical clause. What I meant (of course, *wink*) was "...an instance of a user-defined class that doesn't override the special double-underscore methods in a way that causes it (the instance) to be considered False in a 'boolean context', as they say in perl." In [1]: class bar: pass ...: In [2]: b = bar() In [3]: bool(b) Out[3]: True > Objects can support methods like __bool__, __len__ (a tree might use > this to compute the number of nodes or something like that), and > __nonzero__. ?If len(object)==0 then the object is treated as false. > So that test can fail if a __len__ method gets added to the tree class > sometime. ?Or you might decide you want to support empty trees which > would test as false. Ya, that's exactly why I stuck to checking explicitly against None in that tree code. :] > Anyway, Python's overloading of bool(...) is yet another misfeature > and although it's convenient, the "explicit is better than implicit" > principle indicates to avoid that sort of trick. ? I really like the "misfeature" myself. Python (to me) seems to relieve you of gory details that get in the way (compare equivalent C+ + and Python code) but still exposes you to gory details that, with care in coding, can make your code easier to grok and more elegant in general. BTW, Paul, kind of a tangent: I reimplemented the same algorithm but using tuples instead of instances (and empty tuples for "NULL" values.) I was trying to mess around in the space you seemed to indicate existed, i.e. a better implementation using other datatypes, but I didn't have a clear idea what I was doing and, as I said, I started by simply re-implementing with a different datatype. Much to my surprise and delight, I discovered the tuple-based BTree was /already/ a "persistent data type"! It was both awesome and a bit of an anti-climax. :] From http Sat Jul 4 16:55:04 2009 From: http (Paul Rubin) Date: 04 Jul 2009 13:55:04 -0700 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <7xbpo0f9sn.fsf@ruckus.brouhaha.com> Scott David Daniels writes: > And I curse such uses, since I don't get to see the troublesome value, > or why it is troublesome. In the above case, y = sqrt(x) at least > raises ValueError('math domain error'), which is more information than > you are providing. > > How about: > if x >= 0: raise ValueError('x = %r not allowed (negative)?' % x) Better still in these situations is to throw to pdb. But yes, you can put a formatted string in a check message. From no.email at please.post Sat Jul 4 17:10:44 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 21:10:44 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> <7x4otsux7f.fsf@ruckus.brouhaha.com> Message-ID: In <7x4otsux7f.fsf at ruckus.brouhaha.com> Paul Rubin writes: >kj writes: >> sense = cmp(func(hi), func(lo)) >> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >bisection search usually just requires the function to be continuous >and to have its value cross the target somewhere between the endpoints, >not be monotonic. Try the algorithm I posted with lo = -pi/4, hi = 2*pi, func = cos, target = -1, and see what you get... >> I regard the very special case of func(hi)==func(lo)==target as >> pathological (analogous to the fact that a stopped watch is "exactly >> right" twice a day), and not one I care to support. >I do think you should support that case, under the "do 'nothing' >gracefully" principle. You keep missing the point that this is an *internal* *helper* *convenience* function, meant to abstract away common logic from a handful of places and thus eliminate some code repetition within a module. It is *not* a library function intended to be called from elsewhere. So talk of "supporting" anything is besides the point. Any internal use of this function that applies it to a non-strictly-monotonic function is, by assumption, an error. kj From no.email at please.post Sat Jul 4 17:13:23 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 21:13:23 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: In <7xzlbkti7z.fsf at ruckus.brouhaha.com> Paul Rubin writes: >kj writes: >> This implies that code that uses *any* assert statement (other than >> perhaps the trivial and meaningless ones like "assert True") is >> liable to break, because whatever it is that these assert statements >> are checking "on some occasions, ... would go unchecked, potentially >> breaking your code." >Yes, that implication is absolutely valid. The purpose of assert >statements is to debug the code, by checking for conditions that are >supposed to be impossible. Precisely. As I've stated elsewhere, this is an internal helper function, to be called only a few times under very well-specified conditions. The assert statements checks that these conditions are as intended. I.e. they are checks against the module writer's programming errors. From no.email at please.post Sat Jul 4 17:14:45 2009 From: no.email at please.post (kj) Date: Sat, 4 Jul 2009 21:14:45 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: In MRAB writes: >Paul Rubin wrote: >> kj writes: >>> This implies that code that uses *any* assert statement (other than >>> perhaps the trivial and meaningless ones like "assert True") is >>> liable to break, because whatever it is that these assert statements >>> are checking "on some occasions, ... would go unchecked, potentially >>> breaking your code." >> >> Yes, that implication is absolutely valid. The purpose of assert >> statements is to debug the code, by checking for conditions that are >> supposed to be impossible. Unless the program is broken (i.e. the >> impossible happened), no assert statement should ever trigger. >> >Technically these are known as "invariants". An assertion will always be >True if the program is bug-free, no matter what the user might throw at >it; it's not the same as validation. What *user* are you talking about??? I've stated a bazillion times that this function is meant to be called only from within this module. From hanooter at gmail.com Sat Jul 4 18:06:29 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 15:06:29 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: On Jul 4, 12:51?pm, Scott David Daniels wrote: > mclovin wrote: > > OK then. I will try some of the strategies here but I guess things > > arent looking too good. I need to run this over a dataset that someone > > pickled. I need to run this 480,000 times so you can see my > > frustration. So it doesn't need to be "real time" but it would be nice > > it was done sorting this month. > > > Is there a "bet guess" strategy where it is not 100% accurate but much > > faster? > > Well, I timed a run of a version of mine, and the scan is approx 5X > longer than the copy-and-sort. ?Time arr_of_arr.flatten().sort() to > see how quickly the copy and sort happens.So you could try a variant > exploiting the following property: > ? ? ?If you know the minimum length of a run that will be in the top 25, > then the value for each of the most-frequent run entries must show up at > positions n * stride and (n + 1) * stride (for some n). ?That should > drastically reduce the scan cost, as long as stride is reasonably large. > > For my uniformly distributed 0..1024 values in 5M x 5M array, > About 2.5 sec to flatten and sort. > About 15 sec to run one of my heapish thingies. > the least frequency encountered: 24716 > so, with stride at > > sum(flattened[:-stride:stride] == flattened[stride::stride]) == 1000 > So there are only 1000 points to investigate. > With any distribution other than uniform, that should go _way_ down. > So just pull out those points, use bisect to get their frequencies, and > feed those results into the heap accumulation. > > --Scott David Daniels I dont quite understand what you are saying but I know this: the times the most common element appears varies greatly. Sometimes it appears over 1000 times, and some times it appears less than 50. It all depends on the density of the arrays I am analyzing. like I said I need to do this 480,000 times so to get this done realistically I need to analyse about 5 a second. It appears that the average matrix size contains about 15 million elements. I threaded my program using your code and I did about 1,000 in an hour so it is still much too slow. When I selected 1 million random elements to count, 8 out of the top 10 of those were in the top 25 of the precise way and 18 of the 25 were in the top 25 of the precise way. so I suppose that could be an option. From sajmikins at gmail.com Sat Jul 4 18:22:47 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sat, 4 Jul 2009 15:22:47 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: Message-ID: On Jul 4, 12:30?pm, kj wrote: > In "Pablo Torres N." writes: > > > > >On Sat, Jul 4, 2009 at 10:05, kj wrote: > >>>http://docs.python.org/reference/simple_stmts.html#grammar-token-asse... > >tmt > >>>"The current code generator emits no code for an assert statement when op= > >timization is requested at compile time." > > >> Sorry, this doesn't say anything like "don't use assertions to test > >> argument values". =C2=A0I'm aware of the fact that assertions are silence= > >d > >> under certain circumstances. =C2=A0But where does the principle 1. in > >> your first reply come from? =C2=A0I've never seen it before, and would > >> like to understand the reasoning behind it. > > >> kj > >> -- > >>http://mail.python.org/mailman/listinfo/python-list > > >But...if no code is generated for assertions on some occasions, then the > >parameters would go unchecked, potentially breaking your code in said > >occasions. > > This implies that code that uses *any* assert statement (other than > perhaps the trivial and meaningless ones like "assert True") is > liable to break, because whatever it is that these assert statements > are checking "on some occasions, ... would go unchecked, potentially > breaking your code." > > I'm beginning to think that the original "precept" was simply "cargo > cult," i.e. one of those rules that are parroted without being > fully understood. > > As I wrote in my original post, the function I posted was an internal > ("helper") function, not to be used outside of the file. ?This fact > was further (ahem) underscored by the leading underscore in the > function's name. ?Under these circumstances, the assert statements > seem to me perfectly in keeping with the intended use for them. > > kj Assertions are for you, the programmer, to check that your understanding of the code is correct (or not, i.e. if the assertion fails.) It's unfortunately not uncommon to see code where the assert statement is used as an integral part of the processing, or, in other words, where running the code with '-O' will cause changes in the way the it runs. In the code you posted, the assert statements seem to be checking that other code/algorithms aren't (will never) pass that function "out of bounds" arguments. Perfectly valid IMO. Since it's an internal helper function you can (should) know that client code will /always/ call it with valid values, the asserts simply make your intuition or knowledge explicit in a way that will show up dramatically if you're wrong about that (i.e. if there's some problem elsewhere in the code feeding that function.) Assertions should never fail, which is why you can (or should be able to without breaking your code) remove them entirely and know that your program will run identically. That's why folks are quick to jump on cases where assertions are used as control-flow constructs. (In your function, however, they aren't.) In a sense, assertions /are/ meaningless, except to the programmer. FWIW, you can use "if __debug__:" and put anything you like in the statement suite and the whole if statement goes away when run with '- O'. Sort of a "super-assert". Regards, ~Simon From http Sat Jul 4 18:24:01 2009 From: http (Paul Rubin) Date: 04 Jul 2009 15:24:01 -0700 Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> <7x4otsux7f.fsf@ruckus.brouhaha.com> Message-ID: <7xljn484u6.fsf@ruckus.brouhaha.com> kj writes: > >bisection search usually just requires the function to be continuous > >and to have its value cross the target somewhere between the endpoints, > >not be monotonic. > > Try the algorithm I posted with lo = -pi/4, hi = 2*pi, func = cos, > target = -1, and see what you get... Sorry, my comment was poorly phrase. Bisection search usually is phrased in terms of solving f(x)=0 where f is continuous and if the initial endpoints are a and b, then f(a) and f(b) have opposite sign. > You keep missing the point that this is an *internal* *helper* > *convenience* function, ... Any internal use of this function that > applies it to a non-strictly-monotonic function is, by assumption, > an error. In that case there are better algorithms you can use. But didn't this thread start out asking what version of the code was best suited for presentation to students? In that case, the function's preconditions should be stated explicitly and not be narrower than necessary. From python at mrabarnett.plus.com Sat Jul 4 18:29:41 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 04 Jul 2009 23:29:41 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <4A4FD7D5.6050201@mrabarnett.plus.com> mclovin wrote: [snip] > like I said I need to do this 480,000 times so to get this done > realistically I need to analyse about 5 a second. It appears that the > average matrix size contains about 15 million elements. > > I threaded my program using your code and I did about 1,000 in an hour > so it is still much too slow. > > When I selected 1 million random elements to count, 8 out of the top > 10 of those were in the top 25 of the precise way and 18 of the 25 > were in the top 25 of the precise way. so I suppose that could be an > option. The values are integers, aren't they? What is the range of values? From hanooter at gmail.com Sat Jul 4 18:33:38 2009 From: hanooter at gmail.com (mclovin) Date: Sat, 4 Jul 2009 15:33:38 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: On Jul 4, 3:29?pm, MRAB wrote: > mclovin wrote: > > [snip] > > > like I said I need to do this 480,000 times so to get this done > > realistically I need to analyse about 5 a second. It appears that the > > average matrix size contains about 15 million elements. > > > I threaded my program using your code and I did about 1,000 in an hour > > so it is still much too slow. > > > When I selected 1 million random elements to count, 8 out of the top > > 10 of those were in the top 25 of the precise way and 18 of the 25 > > were in the top 25 of the precise way. so I suppose that could be an > > option. > > The values are integers, aren't they? What is the range of values? There are appox 550k unique values with a range of 0-2million with gaps. From pavlovevidence at gmail.com Sat Jul 4 18:47:57 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 4 Jul 2009 15:47:57 -0700 (PDT) Subject: Searching equivalent to C++ RAII or deterministic destructors References: <6b17de68-9a8e-4896-b0fe-99dd12471314@j32g2000yqh.googlegroups.com> <3pduh6-g52.ln1@satorlaser.homedns.org> <6c15c237-c3e6-4eb7-aae3-0548e9ab279f@d7g2000prl.googlegroups.com> Message-ID: <94eaeb9d-fe90-45fb-99ee-8846eb8cb19e@r33g2000yqn.googlegroups.com> On Jul 3, 5:34?am, Jack Diederich wrote: > On Thu, Jul 2, 2009 at 2:36 PM, Carl Banks wrote: > > Warning: objects with a __del__ attribute prevent reference cycle > > detection, which can potentially lead to memory (and resource) leaks. > > So you must be careful to avoid creating reference loops with that > > object. > > WARNING-er: As Carl points out, adding a __del__ method actually makes > it /less/ likely that your object will be cleaned up. No I wasn't pointing that out, and I don't really agree with it. (You can't really say it is "more likely" or "less likely" without considering the skill and patience of the programmer, and there are many programmers who are up to the task.) __del__ can succeed with care, and if you are willing to give up a certain amount of flexibility and portability I don't recommend going that way. For one thing RAII isn't quite so wonderful in Python's dynamic environment as it is in C++'s static universe, and people seem to expect more from it than they get. Second, relying on implementation-dependent behavior is not a good thing and can set you up for problems later, something few people expect to happen to them but it does more often than they think. (Search comp.lang.python for a thread started by Warren DeLano for an example of someone who got bit hard by such a change he should have seen coming.) But still I leave that for them to risk for themselves. [snip] >?If you care enough about a resource to write a __del__ method > you care enough to clean it up explicitly. ?'with' blocks are very > nice for that. The OP already said with blocks won't suffice since the resources are long-lived objects that aren't limited to a single scope. Carl Banks From Scott.Daniels at Acm.Org Sat Jul 4 18:50:23 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 04 Jul 2009 15:50:23 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <4A4FDCAF.8010209@Acm.Org> mclovin wrote: > On Jul 4, 12:51 pm, Scott David Daniels wrote: >> mclovin wrote: >>> OK then. I will try some of the strategies here but I guess things >>> arent looking too good. I need to run this over a dataset that someone >>> pickled. I need to run this 480,000 times so you can see my >>> frustration. So it doesn't need to be "real time" but it would be nice >>> it was done sorting this month. >>> Is there a "bet guess" strategy where it is not 100% accurate but much >>> faster? >> >> Well, I timed a run of a version of mine, and the scan is approx 5X >> longer than the copy-and-sort. Time arr_of_arr.flatten().sort() to >> see how quickly the copy and sort happens.So you could try a variant >> exploiting the following property: >> If you know the minimum length of a run that will be in the top 25, >> then the value for each of the most-frequent run entries must show up at >> positions n * stride and (n + 1) * stride (for some n). That should >> drastically reduce the scan cost, as long as stride is reasonably large.... >> >> sum(flattened[:-stride:stride] == flattened[stride::stride]) == 1000 >> So there are only 1000 points to investigate. >> With any distribution other than uniform, that should go _way_ down. >> So just pull out those points, use bisect to get their frequencies, and >> feed those results into the heap accumulation. >> >> --Scott David Daniels > > I dont quite understand what you are saying but I know this: the times > the most common element appears varies greatly. Sometimes it appears > over 1000 times, and some times it appears less than 50. It all > depends on the density of the arrays I am analyzing. Here's a heuristic replacement for my previous frequency code: I've tried to mark where you could fudge numbers if the run time is at all close. def frequency(arr_of_arr, N, stride=100) '''produce (freq, value) pairs for data in arr_of_arr. Tries to produce > N pairs. stride is a guess at half the length of the shortest run in the top N runs. ''' # if the next two lines are too slow, this whole approach is toast data = arr_of_arr.flatten() # big allocation data.sort() # a couple of seconds for 25 million ints # stride is a length forcing examination of a run. sampled = data[::stride] # Note this is a view into data, and is still sorted. # We know that any run of length 2 * stride - 1 in data _must_ have # consecutive entries in sampled. Compare them "in parallel" matches = sampled[:-1] == sampled[1:] # matches is True or False for stride-separated values from sampled candidates = sum(matches) # count identified matches # while candidates is huge, keep trying with a larger stride while candidates > N *10: # 10 -- heuristic stride *= 2 # # heuristic increase sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = sum(matches) # if we got too few, move stride down: while candidates < N * 3: # heuristic slop for long runs stride //= 2 # heuristic decrease sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = sum(matches) # Here we have a "nice" list of candidates that is likely # to include every run we actually want. sampled[matches] is # the sorted list of candidate values. It may have duplicates former = None past = 0 # In the loop here we only use sampled to the pick values we # then go find in data. We avoid checking for same value twice for value in sampled[matches]: if value == former: continue # A long run: multiple matches in sampled former = value # Make sure we only try this one once # find the beginning of the run start = bisect.bisect_left(data, value, past) # find the end of the run (we know it is at least stride long) past = bisect.bisect_right(data, value, start + stride) yield past - start, value # produce frequency, value data --Scott David Daniels Scott.Daniels at Acm.Org From python at mrabarnett.plus.com Sat Jul 4 19:05:50 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 05 Jul 2009 00:05:50 +0100 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <4A4FE04E.1050202@mrabarnett.plus.com> mclovin wrote: > On Jul 4, 3:29 pm, MRAB wrote: >> mclovin wrote: >> >> [snip] >> >>> like I said I need to do this 480,000 times so to get this done >>> realistically I need to analyse about 5 a second. It appears that the >>> average matrix size contains about 15 million elements. >>> I threaded my program using your code and I did about 1,000 in an hour >>> so it is still much too slow. >>> When I selected 1 million random elements to count, 8 out of the top >>> 10 of those were in the top 25 of the precise way and 18 of the 25 >>> were in the top 25 of the precise way. so I suppose that could be an >>> option. >> The values are integers, aren't they? What is the range of values? > > There are appox 550k unique values with a range of 0-2million with > gaps. I've done a little experimentation with lists (no numpy involved) and found that I got a x2 speed increase if I did the counting using a list, something like this: counts = [0] * 2000000 for x in values: counts[x] += 1 counts = dict(e for e in enumerate(values) if e[1] != 0) From hkmshb at gmail.com Sat Jul 4 19:18:50 2009 From: hkmshb at gmail.com (Klone) Date: Sat, 4 Jul 2009 16:18:50 -0700 (PDT) Subject: Is code duplication allowed in this instance? References: <4d99b710-45f1-42dc-b80c-1894af26db42@p29g2000yqh.googlegroups.com> Message-ID: <46ce50ea-0272-4285-ab06-65c4e53a1e99@k8g2000yqn.googlegroups.com> Thank you all. I think I get the gist and am about trying your suggestions. From nagle at animats.com Sat Jul 4 19:35:08 2009 From: nagle at animats.com (John Nagle) Date: Sat, 04 Jul 2009 16:35:08 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xvdm8thpo.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> Message-ID: <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Paul Rubin wrote: > John Nagle writes: >> Python doesn't have a "switch" or "case" statement, and when >> you need a state machine with many states, that makes for painful, >> slow code. ... >> There's a comment in the code that it would be useful >> to run a few billion lines of HTML through an instrumented version >> of the parser to decide in which order the IF statements should be >> executed. You shouldn't have to do that. > > In that particular program it would probably be better to change those > if/elif/elif/else constructs to dictionary lookups. I see the program > already does that for some large tables. A dictionary lookup (actually, several of them) for every input character is rather expensive. Tokenizers usually index into a table of character classes, then use the character class index in a switch statement. This is an issue that comes up whenever you have to parse some formal structure, from XML/HTML to Pickle to JPEG images to program source. If Python could figure out what's a constant and what isn't during compilation, this sort of thing could be much more efficient. In fact, you don't even need a switch statement at the source level, if the language is such that the compiler can figure out when "elif" clauses are mutually exclusive. The temptation is to write tokenizers in C, but that's an admission of language design failure. (A general problem with Python is "hidden dynamism". That is, changes to variables that can't be found by examining the source. This is a killer for optimizations. One could take the position that any module variable with exactly one visible assignment to it is, in fact, only assigned in one place, and if the right hand side is a constant, the variable is a constant. This would break some programs doing funny stuff with "eval", or using some of the backdoor ways to modify variables, but that's very rare in practice. In return, you get the ability to hard-compile more of Python into fast code. I'm thinking Shed Skin here, not yet another attempt at a JIT system.) On the other hand, trying to do this in Perl, where you can't even index strings, is far worse. John Nagle From http Sat Jul 4 19:48:19 2009 From: http (Paul Rubin) Date: 04 Jul 2009 16:48:19 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <7xfxdcqabg.fsf@ruckus.brouhaha.com> John Nagle writes: > A dictionary lookup (actually, several of them) for every > input character is rather expensive. Tokenizers usually index into > a table of character classes, then use the character class index in > a switch statement. Maybe you could use a regexp (and then have -two- problems...) to find the token boundaries, then a dict to identify the actual token. Tables of character classes seem a bit less attractive in the Unicode era than in the old days. From davea at ieee.org Sat Jul 4 19:53:48 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Jul 2009 19:53:48 -0400 Subject: Zipped Python? In-Reply-To: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> References: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> Message-ID: <4A4FEB8C.4040302@ieee.org> Jeremy Cowles wrote: > Hi all, > > I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. no > install). My intentions are to use it for a distributed computing project > (PyMW) where there is no guarantee that Python is installed on the worker > machines and an interactive install is out of the question. > > I've been looking around, but I haven't been able to turn anything useful up > so far. Any help would be appreciated. > > -- > Jeremy > > > How about portable python: http://www.portablepython.com/ or Movable Python: http://www.voidspace.org.uk/python/movpy/ No experience with either, but they bear looking at. From matt at tplus1.com Sat Jul 4 20:03:38 2009 From: matt at tplus1.com (Matthew Wilson) Date: Sun, 05 Jul 2009 00:03:38 GMT Subject: Does cProfile include IO wait time? Message-ID: I have a command-line script that loads about 100 yaml files. It takes 2 or 3 seconds. I profiled my code and I'm using pstats to find what is the bottleneck. Here's the top 10 functions, sorted by internal time: In [5]: _3.sort_stats('time').print_stats(10) Sat Jul 4 13:25:40 2009 pitz_prof 756872 function calls (739759 primitive calls) in 8.621 CPU seconds Ordered by: internal time List reduced from 1700 to 10 due to restriction <10> ncalls tottime percall cumtime percall filename:lineno(function) 15153 0.446 0.000 0.503 0.000 build/bdist.linux-i686/egg/yaml/reader.py:134(forward) 30530 0.424 0.000 0.842 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:142(need_more_tokens) 98037 0.423 0.000 0.423 0.000 build/bdist.linux-i686/egg/yaml/reader.py:122(peek) 1955 0.415 0.000 1.265 0.001 build/bdist.linux-i686/egg/yaml/scanner.py:1275(scan_plain) 69935 0.381 0.000 0.381 0.000 {isinstance} 18901 0.329 0.000 3.908 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:113(check_token) 5414 0.277 0.000 0.794 0.000 /home/matt/projects/pitz/pitz/__init__.py:34(f) 30935 0.258 0.000 0.364 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:276(stale_possible_simple_keys) 18945 0.192 0.000 0.314 0.000 /usr/local/lib/python2.6/uuid.py:180(__cmp__) 2368 0.172 0.000 1.345 0.001 build/bdist.linux-i686/egg/yaml/parser.py:268(parse_node) I expected to see a bunch of my IO file-reading code in there, but I don't. So this makes me think that the profiler uses CPU time, not clock-on-the-wall time. I'm not an expert on python profiling, and the docs seem sparse. Can I rule out IO as the bottleneck here? How do I see the IO consequences? TIA Matt From jeremy.cowles at gmail.com Sat Jul 4 20:03:44 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Sat, 4 Jul 2009 17:03:44 -0700 Subject: Zipped Python? In-Reply-To: <4A4FEB8C.4040302@ieee.org> References: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> <4A4FEB8C.4040302@ieee.org> Message-ID: <373cf0740907041703g5250fc9cg346c2c6f2b6d992c@mail.gmail.com> > > I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. no >> install). My intentions are to use it for a distributed computing project >> (PyMW) where there is no guarantee that Python is installed on the worker >> machines and an interactive install is out of the question. >> >> I've been looking around, but I haven't been able to turn anything useful >> up >> so far. Any help would be appreciated. >> >> >> How about portable python: > http://www.portablepython.com/ > > or Movable Python: > http://www.voidspace.org.uk/python/movpy/ > Sorry, forgot - I need something that will work for Windows, Linux and Mac. It could be 3 different distros (zips), but I need something for all three. Both portable python and movable Python are Windows only. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Sat Jul 4 20:12:22 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Jul 2009 12:12:22 +1200 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> Message-ID: In message <1beffd94-cfe6-4cf6- bd48-2ccac8637796 at j32g2000yqh.googlegroups.com>, ryles wrote: >>>> # Oh... yeah. I really *did* want 'is None' and not '== None' which >>>> # iter() will do. Sorry guys! > > Please don't let this happen to you too ;) Strange. others have got told off for using "== None" instead of "is None" , and yet it turns out Python itself does exactly the same thing. From emile at fenx.com Sat Jul 4 21:00:29 2009 From: emile at fenx.com (Emile van Sebille) Date: Sat, 04 Jul 2009 18:00:29 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: Message-ID: On 7/4/2009 12:33 AM mclovin said... > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating I ** up the count on the corrosponding dictionary element ** Right at this point, instead of or in addition to counting, why not save the large array index in a list? Then when you've identified the 25 most common elements you'll already have a list of pointer to the instances to work from. Emile From sgm at objexx.com Sat Jul 4 21:15:43 2009 From: sgm at objexx.com (SK) Date: Sat, 4 Jul 2009 18:15:43 -0700 (PDT) Subject: multiprocessing and freezing on Windows References: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Message-ID: To add a bit more information, I found that I needed to patch get_command_line in multiprocessing/forking.py replacing: if getattr(sys, 'frozen', False): return [sys.executable, '--multiprocessing-fork'] else: prog = 'from multiprocessing.forking import main; main()' return [_python_exe, '-c', prog, '--multiprocessing-fork'] with: if WINEXE: prog = 'from multiprocessing.forking import main; main()' return [_python_exe, '-c', prog, '--multiprocessing-fork'] elif getattr(sys, 'frozen', False): return [sys.executable, '--multiprocessing-fork'] else: prog = 'from multiprocessing.forking import main; main()' return [_python_exe, '-c', prog, '--multiprocessing-fork'] and then using py2exe I can get a working "frozen" package if I include pythonw.exe and a subset of Python's Lib directory in my package and call multiprocessing.set_executable to point to that pythonw.exe. Does the above patch address a real bug? Is packaging with multiprocessing supposed to be this hard? If so, some documentation is needed. From nobody at nowhere.com Sat Jul 4 21:25:38 2009 From: nobody at nowhere.com (Nobody) Date: Sun, 05 Jul 2009 02:25:38 +0100 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: On Sat, 04 Jul 2009 16:35:08 -0700, John Nagle wrote: > The temptation is to write tokenizers in C, but that's an admission > of language design failure. The only part that really needs to be written in C is the DFA loop. The code to construct the state table from regexps could be written entirely in Python, but I don't see any advantage to doing so. From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:27:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:27:30 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <025ff310$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 21:14:45 +0000, kj wrote: >>Technically these are known as "invariants". An assertion will always be >>True if the program is bug-free, no matter what the user might throw at >>it; it's not the same as validation. > > What *user* are you talking about??? I've stated a bazillion times that > this function is meant to be called only from within this module. In that case, you would be the user, and like any user, you might accidentally call the function with invalid data. I believe this discussion started because you are presenting this as code for novices. In that case, it is absolutely important that you start off by teaching them the Right Way to do things. As a general rule, the Right Way is to do an explicit test and raise rather than use assert. In production code, "internal use only" code is a grey area where assert is sometimes justified (although I'll point out that in practice, code written for internal use only has a habit of being called by others). But you're not writing production code, you're writing *teaching code*, where even more important than code correctness is user education. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:31:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:31:23 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> Message-ID: <025ff3f9$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 15:06:29 -0700, mclovin wrote: > like I said I need to do this 480,000 times so to get this done > realistically I need to analyse about 5 a second. It appears that the > average matrix size contains about 15 million elements. Have you considered recording the element counts as you construct the arrays? This will marginally increase the time it takes to build the array, but turn finding the most frequent elements into a very quick operation. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:35:31 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:35:31 GMT Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> Message-ID: <025ff4f1$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 12:12:22 +1200, Lawrence D'Oliveiro wrote: > In message <1beffd94-cfe6-4cf6- > bd48-2ccac8637796 at j32g2000yqh.googlegroups.com>, ryles wrote: > >>>>> # Oh... yeah. I really *did* want 'is None' and not '== None' which >>>>> # iter() will do. Sorry guys! >> >> Please don't let this happen to you too ;) > > Strange. others have got told off for using "== None" instead of "is > None" > , > and yet it turns out Python itself does exactly the same thing. That's not "strange", that's a bug. Did you report it to the tracker? -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:36:13 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:36:13 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: <025ff51b$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 11:10:48 -0700, Paul Rubin wrote: > Anyway, Python's overloading of bool(...) is yet another misfeature and > although it's convenient, the "explicit is better than implicit" > principle indicates to avoid that sort of trick. "Overloading of bool()"? I don't understand what that means -- you mean you dislike the ability to overload __bool__ (__nonzero__ in older Pythons)? That seems strange. Python allows you to overload __int__ and __str__ and __float__, why is __bool__ a misfeature? If that's what you mean, then I'm perplexed. Or do you mean that all Python objects are interpretable in a truth context? If that's what you mean, then I'm not perplexed, I'm sure you're utterly wrong. Certain people -- a tiny minority -- keep trying to argue that the ability to say "if obj" for arbitrary objects is somehow a bad thing, and their arguments seem to always boil down to: "If you write code that assumes that only bools have a truth value, then surprising things will happen because all objects have a truth value." Well duh. If you think you have a better reason for calling the truth value of arbitrary objects a "misfeature", I'd like to hear it. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 4 21:39:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 01:39:33 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> Message-ID: <025ff5e2$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 07:19:48 -0700, Scott David Daniels wrote: > Actually the next step is to maintain a min-heap as you run down the > sorted array. Something like: Not bad. I did some tests on it, using the following sample data: arr = np.array([xrange(i, i+7000) for i in xrange(143)] + [[750]*7000] + [xrange(3*i, 3*i+7000) for i in xrange(142)]) and compared your code against the following simple function: def count(arr, N): D = {} for v in arr: for x in v: D[x] = D.get(x, 0) + 1 freq = [] for el, f in D.iteritems(): freq.append((f, el)) return sorted(freq, reverse=True)[:N] As a rough figure, your min-heap code is approximately twice as fast as mine. To the OP: I think you should start profiling your code and find out exactly *where* it is slow and concentrate on that. I think that trying a heuristic to estimate the most frequent elements by taking a random sample is likely to be a mistake -- whatever you're trying to accomplish with the frequency counts, the use of such a heuristic will mean that you're only approximately accomplishing it. -- Steven From ben+python at benfinney.id.au Sat Jul 4 22:09:12 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 05 Jul 2009 12:09:12 +1000 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <87tz1rrid3.fsf@benfinney.id.au> John Nagle writes: > A dictionary lookup (actually, several of them) for every input > character is rather expensive. Tokenizers usually index into a table > of character classes, then use the character class index in a switch > statement. > > This is an issue that comes up whenever you have to parse some > formal structure, from XML/HTML to Pickle to JPEG images to program > source. > [?] > The temptation is to write tokenizers in C, but that's an admission > of language design failure. This sounds like a job for Pyparsing. -- \ ?Better not take a dog on the space shuttle, because if he | `\ sticks his head out when you're coming home his face might burn | _o__) up.? ?Jack Handey | Ben Finney From benjamin.kaplan at case.edu Sat Jul 4 22:28:23 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 4 Jul 2009 22:28:23 -0400 Subject: Zipped Python? In-Reply-To: <373cf0740907041703g5250fc9cg346c2c6f2b6d992c@mail.gmail.com> References: <373cf0740907041044q673b0e1u4341bdee84bef1d5@mail.gmail.com> <4A4FEB8C.4040302@ieee.org> <373cf0740907041703g5250fc9cg346c2c6f2b6d992c@mail.gmail.com> Message-ID: On Sat, Jul 4, 2009 at 8:03 PM, Jeremy Cowles wrote: >>> I'm looking for a full 2.5 or 2.6 version of Python that is zipped (i.e. >>> no >>> install). My intentions are to use it for a distributed computing project >>> (PyMW) where there is no guarantee that Python is installed on the worker >>> machines and an interactive install is out of the question. >>> >>> I've been looking around, but I haven't been able to turn anything useful >>> up >>> so far. Any help would be appreciated. >>> >>> >> How about portable python: >> ? ? http://www.portablepython.com/ >> >> or Movable Python: >> ? ? ?http://www.voidspace.org.uk/python/movpy/ > > Sorry, forgot - I need something that will work for Windows, Linux and Mac. > It could be 3 different distros (zips), but I need something for all three. > Both portable python and movable Python are Windows only. OS X and most, if not all, Linux distributions come with Python preinstalled (they use them for system features) so no one bothered to make something like Portable Python for them. > -- > http://mail.python.org/mailman/listinfo/python-list > > From nagle at animats.com Sat Jul 4 23:15:11 2009 From: nagle at animats.com (John Nagle) Date: Sat, 04 Jul 2009 20:15:11 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xfxdcqabg.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> Message-ID: <4a501a5e$0$1640$742ec2ed@news.sonic.net> Paul Rubin wrote: > John Nagle writes: >> A dictionary lookup (actually, several of them) for every >> input character is rather expensive. Tokenizers usually index into >> a table of character classes, then use the character class index in >> a switch statement. > > Maybe you could use a regexp (and then have -two- problems...) to > find the token boundaries, then a dict to identify the actual token. > Tables of character classes seem a bit less attractive in the Unicode > era than in the old days. I want to see a regular expression that expresses the HTML 5 token parsing rules, including all the explicitly specified error handling. Here's some actual code, from "tokenizer.py". This is called once for each character in an HTML document, when in "data" state (outside a tag). It's straightforward code, but look at all those dictionary lookups. def dataState(self): data = self.stream.char() # Keep a charbuffer to handle the escapeFlag if self.contentModelFlag in\ (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): if len(self.lastFourChars) == 4: self.lastFourChars.pop(0) self.lastFourChars.append(data) # The rest of the logic if data == "&" and self.contentModelFlag in\ (contentModelFlags["PCDATA"], contentModelFlags["RCDATA"]) and not\ self.escapeFlag: self.state = self.states["entityData"] elif data == "-" and self.contentModelFlag in\ (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) and not\ self.escapeFlag and "".join(self.lastFourChars) == "": self.escapeFlag = False self.tokenQueue.append({"type": "Characters", "data":data}) elif data == EOF: # Tokenization ends. return False elif data in spaceCharacters: # Directly after emitting a token you switch back to the "data # state". At that point spaceCharacters are important so they are # emitted separately. self.tokenQueue.append({"type": "SpaceCharacters", "data": data + self.stream.charsUntil(spaceCharacters, True)}) # No need to update lastFourChars here, since the first space will # have already broken any sequences else: chars = self.stream.charsUntil(("&", "<", ">", "-")) self.tokenQueue.append({"type": "Characters", "data": data + chars}) self.lastFourChars += chars[-4:] self.lastFourChars = self.lastFourChars[-4:] return True John Nagle From kyosohma at gmail.com Sat Jul 4 23:17:07 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Sat, 4 Jul 2009 20:17:07 -0700 (PDT) Subject: Pyowa is this Monday! Message-ID: <7224400c-cadc-403f-b090-74cb49373dee@h11g2000yqb.googlegroups.com> Hi, The next Pyowa meeting is on Monday, July 6th from 7-9 p.m. We will be at the Marshall County Sheriff's Office (directions on website). Currently, the plan is to have a talk about Python at Fisher/Emerson that will segue into a general discussion of what we do with our favorite programming language. Be prepared to share! We may have another presentation in our Standard Library series as well or just something random. Let me know if you think you'll be there. Pop & water will be provided. We hope to see you there! -------------------------- Mike Driscoll http://www.pyowa.org http://pyowa.blip.tv/ http://twitter.com/pyowa http://upcoming.yahoo.com/event/2853430/ Mailing list: http://pyowalist.pythonlibrary.org/listinfo.cgi/pyowa-pythonlibrary.org From aahz at pythoncraft.com Sat Jul 4 23:37:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 Jul 2009 20:37:16 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: In article <4a501a5e$0$1640$742ec2ed at news.sonic.net>, John Nagle wrote: > > Here's some actual code, from "tokenizer.py". This is called once >for each character in an HTML document, when in "data" state (outside >a tag). It's straightforward code, but look at all those >dictionary lookups. > > def dataState(self): > data = self.stream.char() > > # Keep a charbuffer to handle the escapeFlag > if self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > if len(self.lastFourChars) == 4: > self.lastFourChars.pop(0) > self.lastFourChars.append(data) > > # The rest of the logic > if data == "&" and self.contentModelFlag in\ > (contentModelFlags["PCDATA"], contentModelFlags["RCDATA"]) and not\ > self.escapeFlag: > self.state = self.states["entityData"] > elif data == "-" and self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) and not\ > self.escapeFlag and "".join(self.lastFourChars) == "": > self.escapeFlag = False > self.tokenQueue.append({"type": "Characters", "data":data}) > elif data == EOF: > # Tokenization ends. > return False > elif data in spaceCharacters: > # Directly after emitting a token you switch back to the "data > # state". At that point spaceCharacters are important so they are > # emitted separately. > self.tokenQueue.append({"type": "SpaceCharacters", "data": > data + self.stream.charsUntil(spaceCharacters, True)}) > # No need to update lastFourChars here, since the first space will > # have already broken any sequences > else: > chars = self.stream.charsUntil(("&", "<", ">", "-")) > self.tokenQueue.append({"type": "Characters", "data": > data + chars}) > self.lastFourChars += chars[-4:] > self.lastFourChars = self.lastFourChars[-4:] > return True Every single "self." is a dictionary lookup. Were you referring to those? If not, I don't see your point. If yes, well, that's kind of the whole point of using Python. You do pay a performance penalty. You can optimize out some lookups, but you need to switch to C for some kinds of computationally intensive algorithms. In this case, you can probably get a large boost out of Pysco or Cython or Pyrex. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From tn.pablo at gmail.com Sun Jul 5 00:26:39 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sat, 4 Jul 2009 23:26:39 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Sat, Jul 4, 2009 at 18:01, Dennis Lee Bieber wrote: > ? ? ? ?Do you really want to inflict novices with the subtleties of when > "assert" is a proper structure to use? I'll second that. This very thread is proof that assertions are polemic, so maybe you (kj) should just save your student's sanity and stick to good old conditionals :-) As for your initial question, I think, thirty four emails after, that yes, your function is a bit too clever and you should sacrifice some generality in order to make it more readable. -- Pablo Torres N. From pavlovevidence at gmail.com Sun Jul 5 01:51:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 4 Jul 2009 22:51:52 -0700 (PDT) Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <4c2c7b44-23cd-43b4-8086-1b7cf6a14954@n11g2000yqb.googlegroups.com> On Jul 4, 4:35?pm, John Nagle wrote: > ? ? The temptation is to write tokenizers in C, but that's an admission > of language design failure. No it isn't. It's only a failure of Python to be the language that does everything *you* want. Carl Banks From http Sun Jul 5 02:17:21 2009 From: http (Paul Rubin) Date: 04 Jul 2009 23:17:21 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xfxdbk61a.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Certain people -- a tiny minority -- keep trying to argue > that the ability to say "if obj" for arbitrary objects is somehow a bad > thing, and their arguments seem to always boil down to: > "If you write code that assumes that only bools have a truth value, then > surprising things will happen because all objects have a truth value." I'd put it under the general rubric of "explicit is better than implicit". The language shouldn't do silly automatic typecasts all over the place. Yes, it saves a few keystrokes to say "if x:" instead of "if len(x)==0:" or even "if bool(x):", but if I program in a style where I like to think I know the type of something when I use it, I'd like the interpreter to let me know when I'm wrong instead of proceeding silently. From nick at craig-wood.com Sun Jul 5 03:30:04 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Sun, 05 Jul 2009 02:30:04 -0500 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > As an example of code that really needs to run fast, but is > speed-limited by Python's limitations, see "tokenizer.py" in > > http://code.google.com/p/html5lib/ > > This is a parser for HTML 5, a piece of code that will be needed > in many places and will process large amounts of data. It's written > entirely in Python. Take a look at how much work has to be performed > per character. > > This is a good test for Python implementation bottlenecks. Run > that tokenizer on HTML, and see where the time goes. > > ("It should be written in C" is not an acceptable answer.) You could compile it with Cython though. lxml took this route... -- Nick Craig-Wood -- http://www.craig-wood.com/nick From pm567426 at gmail.com Sun Jul 5 03:38:30 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 00:38:30 -0700 (PDT) Subject: How Python Implements "long integer"? Message-ID: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Hello, I'm reading about implementation of long ints in Python. I downloaded the source code of CPython and will read the longobject.c, but from where I should start reading this file? I mean which function is the first? Anyone can help? Thanks Pedram From mail at microcorp.co.za Sun Jul 5 04:12:54 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 5 Jul 2009 10:12:54 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <006201c9fd48$666d94e0$0d00a8c0@Hendrik> "John Nagle" wrote: > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. There's a comment in the code that it would be useful > to run a few billion lines of HTML through an instrumented version > of the parser to decide in which order the IF statements should be > executed. You shouldn't have to do that. You do not have to implement a state machine in a case statement, or in a series of if... elifs. Python is not C. Use a dispatch dict, and have each state return the next state. Then you can use strings representing state names, and everybody will be able to understand the code. toy example, not tested, nor completed: protocol = {"start":initialiser,"hunt":hunter,"classify":classifier,....other states} def state_machine(): next_step = protocol["start"]() while True: next_step = protocol[next_step]() Simple, and almost as fast as if you did the same thing in assembler using pointers. And each state will have a finite set of reasons to either stay where its at, or move on. Not a lot you can do about that, but test for them one at a time. But at least you will have split the problem up, and you won't be doing irrelevant tests. You can even do away with the dict, by having each state return the actual next state routine: next_state = protocol_initialiser() while True: next_state = next_state() time.sleep(0.001) # this prevents thrashing when monitoring real events If you are using a gui, and you have access to an after callback, then you can make a ticking stutter thread to run some monitoring machine in the background, using the same "tell me what to do next" technique. To take the timing thing further, you can do: wait_time, next_state = protocol_initialiser() while True: if wait_time: time.sleep(wait_time) wait_time, next_state = next_state() This gives you control over busy-wait loops, and lets you speed up when it is needed. Python really is not C. - Hendrik From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 04:28:01 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 08:28:01 GMT Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: <0260559c$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 20:15:11 -0700, John Nagle wrote: > Paul Rubin wrote: >> John Nagle writes: >>> A dictionary lookup (actually, several of them) for every >>> input character is rather expensive. Tokenizers usually index into a >>> table of character classes, then use the character class index in a >>> switch statement. >> >> Maybe you could use a regexp (and then have -two- problems...) to find >> the token boundaries, then a dict to identify the actual token. Tables >> of character classes seem a bit less attractive in the Unicode era than >> in the old days. > > I want to see a regular expression that expresses the HTML 5 token > parsing rules, including all the explicitly specified error handling. Obviously the regex can't do the error handling. Nor should you expect a single regex to parse an entire HTML document. But you could (perhaps) use regexes to parse pieces of the document, as needed. Have you investigated the pyparsing module? Unless you have some reason for avoiding it, for any complicated parsing job I'd turn to that before trying to roll your own. > Here's some actual code, from "tokenizer.py". This is called once > for each character in an HTML document, when in "data" state (outside a > tag). It's straightforward code, but look at all those dictionary > lookups. Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't see how a case statement would help you here: you're not dispatching on a value, but running through a series of tests until one passes. There are languages where you can write something like: case: x > 0: process_positive(x) x < 0: process_negative(x) x == 0: process_zero(x) but that's generally just syntactic sugar for the obvious if...elif... block. (Although clever compilers might recognise that it's the same x in each expression, and do something clever to optimize the code.) Nor do I see why you were complaining about Python not having true constants. I don't see how that would help you... most of your explicit dict lookups are against string literals e.g. contentModelFlags["RCDATA"]. So while I feel your pain, I'm not sure I understand why you're blaming this on *Python*. -- Steven From stefan_ml at behnel.de Sun Jul 5 04:41:58 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 10:41:58 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <4a506755$0$31333$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Python doesn't have a "switch" or "case" statement, and when > you need a state machine with many states, that makes for painful, > slow code. Cython has a built-in optimisation that maps if-elif-else chains to C's switch statement if they only test a single int/char variable, even when you write things like "elif x in [1,5,9,12]". This works in Cython, because we know that the comparison to a C int/char is side-effect free. It may not always be side-effect free in Python, so this won't work in general. It would be perfect for your case, though. Stefan From http Sun Jul 5 04:58:13 2009 From: http (Paul Rubin) Date: 05 Jul 2009 01:58:13 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xws6nik0q.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't > see how a case statement would help you here: you're not dispatching on a > value, but running through a series of tests until one passes. A case statement switch(x):... into a bunch of constant case labels would be able to use x as an index into a jump vector, and/or do an unrolled logarithmic (bisection-like) search through the tests, instead of a linear search. From stefan_ml at behnel.de Sun Jul 5 04:58:27 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 10:58:27 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a501a5e$0$1640$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Here's some actual code, from "tokenizer.py". This is called once > for each character in an HTML document, when in "data" state (outside > a tag). It's straightforward code, but look at all those > dictionary lookups. > > def dataState(self): > data = self.stream.char() > > # Keep a charbuffer to handle the escapeFlag > if self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): Is the tuple (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) constant? If that is the case, I'd cut it out into a class member (or module-local variable) first thing in the morning. And I'd definitely keep the result of the "in" test in a local variable for reuse, seeing how many times it's used in the rest of the code. Writing inefficient code is not something to blame the language for. Stefan From stefan_ml at behnel.de Sun Jul 5 05:04:33 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 11:04:33 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> Message-ID: <4a506ca1$0$31344$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Paul Rubin wrote: >> John Nagle writes: >>> Python doesn't have a "switch" or "case" statement, and when >>> you need a state machine with many states, that makes for painful, >>> slow code. ... >>> There's a comment in the code that it would be useful >>> to run a few billion lines of HTML through an instrumented version >>> of the parser to decide in which order the IF statements should be >>> executed. You shouldn't have to do that. >> >> In that particular program it would probably be better to change those >> if/elif/elif/else constructs to dictionary lookups. I see the program >> already does that for some large tables. > > A dictionary lookup (actually, several of them) for every > input character is rather expensive. Did you implement this and prove your claim in benchmarks? Taking a look at the current implementation, I'm pretty sure a dict-based implementation would outrun it in your first try. Stefan From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 05:04:58 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 09:04:58 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> Message-ID: <02605e46$0$20657$c3e8da3@news.astraweb.com> On Sat, 04 Jul 2009 23:17:21 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> Certain people -- a tiny minority -- keep trying to argue that the >> ability to say "if obj" for arbitrary objects is somehow a bad thing, >> and their arguments seem to always boil down to: "If you write code >> that assumes that only bools have a truth value, then surprising things >> will happen because all objects have a truth value." > > I'd put it under the general rubric of "explicit is better than > implicit". "if x" is explicit. It's difficult to see how a branch could be anything other than explicit, but never mind. > The language shouldn't do silly automatic typecasts all over > the place. "if x" doesn't involve a typecast. Python doesn't have typecasts, except possibly for the special case of myobject.__class__ = Another_Class. If you mean Python shouldn't do silly automatic type conversions all over the place, I absolutely agree with you! Fortunately, testing the truth value of an object isn't a silly automatic type conversion. > Yes, it saves a few keystrokes to say "if x:" instead of "if > len(x)==0:" or even "if bool(x):", It's not about saving keystrokes -- that's a furphy. It's about encapsulation. Objects are in a better position to recognise when they are "something" (true) or "nothing" (false) than you are. Given an arbitrary object x, how do you know if it's something or nothing? In general, you can't tell -- but the object can, provided it's well written. (The conspicuous exception is iterators, but that's a special case.) "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like iterable instead of a list, and calculating the actual length is O(N). Why spend all that effort to find out the length of the object is 59,872,819 only to throw that away? My object knows when it's empty, but instead you make foolish assumptions about the object and consequently write wastefully slow code. > but if I program in a style where I > like to think I know the type of something when I use it, I'd like the > interpreter to let me know when I'm wrong instead of proceeding > silently. Oh come on now... that's a silly objection. If you want strongly-typed variables in Python, say so, don't pretend this is a failure of truth- testing. If you write len(x)==0 Python doesn't complain if x is a dict instead of the list you were expecting. Why is it acceptable to duck-type len(x) but not truth-testing? -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 05:07:50 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 09:07:50 GMT Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <02605ef2$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 10:12:54 +0200, Hendrik van Rooyen wrote: > Python is not C. John Nagle is an old hand at Python. He's perfectly aware of this, and I'm sure he's not trying to program C in Python. I'm not entirely sure *what* he is doing, and hopefully he'll speak up and say, but whatever the problem is it's not going to be as simple as that. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 05:13:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 09:13:23 GMT Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> Message-ID: <0260603f$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 01:58:13 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't >> see how a case statement would help you here: you're not dispatching on >> a value, but running through a series of tests until one passes. > > A case statement switch(x):... into a bunch of constant case labels > would be able to use x as an index into a jump vector, and/or do an > unrolled logarithmic (bisection-like) search through the tests, instead > of a linear search. Yes, I'm aware of that, but that's not what John's code is doing -- he's doing a series of if expr ... elif expr tests. I don't think a case statement can do much to optimize that. -- Steven From stef.mientki at gmail.com Sun Jul 5 05:35:14 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 05 Jul 2009 11:35:14 +0200 Subject: Adding an object to the global namespace through " f_globals" is that allowed ? In-Reply-To: References: <4A4BC22D.70502@gmail.com> Message-ID: <4A5073D2.4010000@gmail.com> Terry Reedy wrote: > Stef Mientki wrote: >> hello, >> >> I need to add an object's name to the global namespace. >> The reason for this is to create an environment, >> where you can add some kind of math environment, >> where no need for Python knowledge is needed. >> >> The next statement works, >> but I'm not sure if it will have any dramatical side effects, >> other than overruling a possible object with the name A >> >> def some_function ( ...) : >> A = object ( ...) >> sys._getframe(1).f_globals [ Name ] = A > > global name > name = A > > or is name is a string var > globals()[name] = A great, the last 2 lines works in most cases. Now to get everything working correctly, I have to use both globals() and f_globals() but everything seems to work perfect now. thanks for all the hints. cheers, Stef From http Sun Jul 5 05:38:54 2009 From: http (Paul Rubin) Date: 05 Jul 2009 02:38:54 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Yes, I'm aware of that, but that's not what John's code is doing -- he's > doing a series of if expr ... elif expr tests. I don't think a case > statement can do much to optimize that. The series of tests is written that way because there is no case statement available. It is essentially switching on a bunch of character constants and then doing some additional tests in each branch. It could be that using ord(c) as an index into a list of functions might be faster than a dict lookup on c to get a function. I think John is hoping to avoid a function call and instead get an indexed jump within the Python bytecode for the big function. From stefan_ml at behnel.de Sun Jul 5 05:41:18 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 11:41:18 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <4a50753e$0$31331$9b4e6d93@newsspool4.arcor-online.net> Stefan Behnel wrote: > John Nagle wrote: >> Here's some actual code, from "tokenizer.py". This is called once >> for each character in an HTML document, when in "data" state (outside >> a tag). It's straightforward code, but look at all those >> dictionary lookups. >> >> def dataState(self): >> data = self.stream.char() >> >> # Keep a charbuffer to handle the escapeFlag >> if self.contentModelFlag in\ >> (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > > Is the tuple > > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) > > constant? If that is the case, I'd cut it out into a class member (or > module-local variable) first thing in the morning. Ah, and there's also this little trick to make it a (fast) local variable in that method: def some_method(self, some_const=(1,2,3,4)): ... Stefan From stefan_ml at behnel.de Sun Jul 5 05:48:38 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 11:48:38 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xr5wvtqoh.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: <4a5076f8$0$31341$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Steven D'Aprano writes: >> Yes, I'm aware of that, but that's not what John's code is doing -- he's >> doing a series of if expr ... elif expr tests. I don't think a case >> statement can do much to optimize that. > > The series of tests is written that way because there is no case > statement available. It is essentially switching on a bunch of > character constants and then doing some additional tests in each > branch. Although doing some of the tests first and then checking the input conditionally might be faster here. Another idea: You could exchange the methods whenever self.contentModelFlag changes, i.e. you'd have a "dataState_CDATA", a "dataState_PCDATA" etc. > It could be that using ord(c) as an index into a list of functions > might be faster than a dict lookup on c to get a function. Rather unlikely, given that calling "ord(c)" involves a dict lookup for "ord". You might get away with the pre-initialised keywords trick, though. > I think > John is hoping to avoid a function call and instead get an indexed > jump within the Python bytecode for the big function. Hmm, yes, the actual code inside the conditionals is pretty short, so the call overhead might hurt here. Stefan From http Sun Jul 5 05:52:17 2009 From: http (Paul Rubin) Date: 05 Jul 2009 02:52:17 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7x7hyn5uem.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > > # Keep a charbuffer to handle the escapeFlag > > if self.contentModelFlag in\ > > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > Is the tuple > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) > constant? If that is the case, I'd cut it out into a class member ... I think the main issue for that function comes after that if statement. There is a multi-way switch on a bunch of different possible character values. I do agree with you that the first "if" can also be optimized. From ldo at geek-central.gen.new_zealand Sun Jul 5 05:52:22 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Jul 2009 21:52:22 +1200 Subject: Multi thread reading a file References: <1cbd6f830906301852t4febbb02q99528f2d6ec94a82@mail.gmail.com> <1beffd94-cfe6-4cf6-bd48-2ccac8637796@j32g2000yqh.googlegroups.com> <025ff4f1$0$20657$c3e8da3@news.astraweb.com> Message-ID: In message <025ff4f1$0$20657$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > On Sun, 05 Jul 2009 12:12:22 +1200, Lawrence D'Oliveiro wrote: > >> In message <1beffd94-cfe6-4cf6-bd48-2ccac8637796 at j32g2000yqh.googlegroups.com>, ryles wrote: >> >>>>>> # Oh... yeah. I really *did* want 'is None' and not '== None' which >>>>>> # iter() will do. Sorry guys! >>> >>> Please don't let this happen to you too ;) >> >> Strange. others have got told off for using "== None" instead of "is >> None" , >> and yet it turns out Python itself does exactly the same thing. > > That's not "strange", that's a bug. It's not a bug, as Gabriel Genellina has pointed out. From http Sun Jul 5 05:54:23 2009 From: http (Paul Rubin) Date: 05 Jul 2009 02:54:23 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> <4a5076f8$0$31341$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7x3a9b5ub4.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > > The series of tests is written that way because there is no case > > statement available. It is essentially switching on a bunch of > > character constants and then doing some additional tests in each > > branch. > Although doing some of the tests first and then checking the input > conditionally might be faster here. That is essentially what happens. There are a bunch of tests of the form if data=='<' and [some other stuff]: ... Because of short-circuit evaluation of "and", the additional tests only happen once the character has been matched. From ldo at geek-central.gen.new_zealand Sun Jul 5 05:54:37 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 05 Jul 2009 21:54:37 +1200 Subject: Is code duplication allowed in this instance? References: Message-ID: In message , Klone wrote: > So in this scenario is it OK to duplicate the algorithm to be tested > within the test codes or refactor the method such that it can be used > within test codes to verify itself(??). I think you should be put on the management fast-track. From dickinsm at gmail.com Sun Jul 5 05:57:02 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sun, 5 Jul 2009 02:57:02 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: On Jul 5, 8:38?am, Pedram wrote: > Hello, > I'm reading about implementation of long ints in Python. I downloaded > the source code of CPython and will read the longobject.c, but from > where I should start reading this file? I mean which function is the > first? I don't really understand the question: what do you mean by 'first'? It might help if you tell us what your aims are. In any case, you probably also want to look at the Include/ longintrepr.h and Include/longobject.h files. Mark From stefan_ml at behnel.de Sun Jul 5 06:04:43 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 12:04:43 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7x3a9b5ub4.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> <4a5076f8$0$31341$9b4e6d93@newsspool4.arcor-online.net> <7x3a9b5ub4.fsf@ruckus.brouhaha.com> Message-ID: <4a507abb$0$31327$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Stefan Behnel writes: >>> The series of tests is written that way because there is no case >>> statement available. It is essentially switching on a bunch of >>> character constants and then doing some additional tests in each >>> branch. >> Although doing some of the tests first and then checking the input >> conditionally might be faster here. > > That is essentially what happens. There are a bunch of tests of the > form > if data=='<' and [some other stuff]: ... That's what I meant. Some of the "other stuff" is redundant enough to do it once at the beginning of the function (or even before entering the function, by writing specialised methods), i.e. I'd (partially) reverse the order of the "and" operands. Stefan From stefan_ml at behnel.de Sun Jul 5 06:06:49 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 12:06:49 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7x7hyn5uem.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> <7x7hyn5uem.fsf@ruckus.brouhaha.com> Message-ID: <4a507b39$0$31327$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Stefan Behnel writes: >>> # Keep a charbuffer to handle the escapeFlag >>> if self.contentModelFlag in\ >>> (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): >> Is the tuple >> (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) >> constant? If that is the case, I'd cut it out into a class member ... > > I think the main issue for that function comes after that if statement. > There is a multi-way switch on a bunch of different possible character > values. I do agree with you that the first "if" can also be optimized. You may notice that the creation of this exact tuple appears in almost all if the conditionals of this method. So it is part of the bottleneck. Stefan From http Sun Jul 5 06:08:16 2009 From: http (Paul Rubin) Date: 05 Jul 2009 03:08:16 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7xy6r34f3j.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > > Yes, it saves a few keystrokes to say "if x:" instead of "if > > len(x)==0:" or even "if bool(x):", > > It's not about saving keystrokes -- that's a furphy. It's about > encapsulation. Objects are in a better position to recognise when they > are "something" (true) or "nothing" (false) than you are. I don't know what a furphy is, but I don't accept that "somethingness" vs. "nothingness" is the same distinction as truth vs falsehood. True and False are values in a specific datatype (namely bool), not abstract qualities of arbitrary data structures. The idea that the "if" statement selects between "somethingness" and "nothingness" rather than between True and False is a bogus re-imagining of the traditional function of an "if" statement and has been an endless source of bugs in Python code. Look how much confusion it causes here in the newsgroup all the time. > "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like > iterable instead of a list, and calculating the actual length is O(N). That doesn't happen in any of Python's built-in container types. I could see some value to having a generic "is_empty" predicate on containers though, to deal with this situation. Your iterable could support that predicate. In fact maybe all iterables should support that predicate. They don't (and can't) all support "len". > If you write len(x)==0 Python doesn't complain if x is a dict > instead of the list you were expecting. Why is it acceptable to > duck-type len(x) but not truth-testing? I haven't seen the amount of bugs coming from generic "len" as from something-vs-nothing confusion. From http Sun Jul 5 07:09:26 2009 From: http (Paul Rubin) Date: 05 Jul 2009 04:09:26 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> <7x7hyn5uem.fsf@ruckus.brouhaha.com> <4a507b39$0$31327$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7xljn3bd3t.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > You may notice that the creation of this exact tuple appears in almost all > if the conditionals of this method. So it is part of the bottleneck. I don't think so. The tuple is only created when the character has already matched, and for the vast majority of the chars in the input stream (ordinary text chars rather than html delimiters) none of them match. From stefan_ml at behnel.de Sun Jul 5 07:23:23 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 13:23:23 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <7xljn3bd3t.fsf@ruckus.brouhaha.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <4a506b33$0$31344$9b4e6d93@newsspool4.arcor-online.net> <7x7hyn5uem.fsf@ruckus.brouhaha.com> <4a507b39$0$31327$9b4e6d93@newsspool4.arcor-online.net> <7xljn3bd3t.fsf@ruckus.brouhaha.com> Message-ID: <4a508d2c$0$31332$9b4e6d93@newsspool4.arcor-online.net> Paul Rubin wrote: > Stefan Behnel writes: >> You may notice that the creation of this exact tuple appears in almost all >> if the conditionals of this method. So it is part of the bottleneck. > > I don't think so. The tuple is only created when the character has > already matched, and for the vast majority of the chars in the input > stream (ordinary text chars rather than html delimiters) none of them > match. Well, it's the second thing that happens when entering the method, it happens several times later on when specific characters are matched, and it also happens at the end when none of the special characters did match. So it /is/ part of the bottleneck because the dict lookups, the tuple creation, the "in" test and the tuple deallocation happen *twice* for almost all characters in the stream. Stefan From 13139781969 at mymetropcs.com Sun Jul 5 07:36:00 2009 From: 13139781969 at mymetropcs.com (13139781969 at mymetropcs.com) Date: Sun, 5 Jul 2009 06:36:00 -0500 Subject: No subject Message-ID: <1295065.145352441246808164305.JavaMail.mms@mms12.mms.metropcs.net> An HTML attachment was scrubbed... URL: -------------- next part -------------- (Homer & Marge having sex with Bart Simpson) From lie.1296 at gmail.com Sun Jul 5 07:37:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 05 Jul 2009 11:37:49 GMT Subject: question of style In-Reply-To: <7xy6r34f3j.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > Steven D'Aprano writes: >> "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like >> iterable instead of a list, and calculating the actual length is O(N). > > That doesn't happen in any of Python's built-in container types. I > could see some value to having a generic "is_empty" predicate on > containers though, to deal with this situation. Your iterable could > support that predicate. In fact maybe all iterables should support > that predicate. They don't (and can't) all support "len". That doesn't happen because all python's built-in container keep track of its own length and are able to quickly determine its own length. But outside builtins, certain iterables cannot determine its own length quickly, e.g. iterators, but may have alternative ways to determine whether itself is empty (through a "private" attributes). If you peek through this private attribute, you're breaking encapsulation. Although python is a language of consenting adults and doesn't really have a real private, codes that breaks encapsulation is prone to bugs. >>> Yes, it saves a few keystrokes to say "if x:" instead of "if >>> len(x)==0:" or even "if bool(x):", >> It's not about saving keystrokes -- that's a furphy. It's about >> encapsulation. Objects are in a better position to recognise when >> they are "something" (true) or "nothing" (false) than you are. > > I don't know what a furphy is, but I don't accept that "somethingness" > vs. "nothingness" is the same distinction as truth vs falsehood. True > and False are values in a specific datatype (namely bool), not > abstract qualities of arbitrary data structures. The idea that the > "if" statement selects between "somethingness" and "nothingness" > rather than between True and False is a bogus re-imagining of the > traditional function of an "if" statement and has been an endless > source of bugs in Python code. Look how much confusion it causes here > in the newsgroup all the time. Neither python's `if` nor `if` in formal logic is about testing True vs. False. `if` in python and formal logic receives a statement. The statement must be evaluatable to True or False, but does not have to be True or False themselves. It just happens that True evaluates to True and False evaluates to False. For example, the statement: P := `e = 2.7182818284590451` Q := `e = m*c**2` ---------------------------------- P -> Q P -> Q evaluates to: `e = 2.7182818284590451` -> `e = m*c**2` Note that `e = 2.7182818284590451` is a statement, not a boolean value. The truth value of `e = 2.7182818284590451` is determined by "calling" (note the double quotes) `e = 2.7182818284590451`.statement_is_true(), which when written in python syntax becomes: (e == 2.7182818284590451).__bool__() >> If you write len(x)==0 Python doesn't complain if x is a dict >> instead of the list you were expecting. Why is it acceptable to >> duck-type len(x) but not truth-testing? > > I haven't seen the amount of bugs coming from generic "len" as from > something-vs-nothing confusion. From ptmcg at austin.rr.com Sun Jul 5 07:41:27 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sun, 5 Jul 2009 04:41:27 -0700 (PDT) Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <7061fac4-954d-49e2-a03b-3ed8653a99f3@o6g2000yqj.googlegroups.com> On Jul 5, 3:12 am, "Hendrik van Rooyen" wrote: > > Use a dispatch dict, and have each state return the next state. > Then you can use strings representing state names, and > everybody will be able to understand the code. > > toy example, not tested, nor completed: > > protocol = {"start":initialiser,"hunt":hunter,"classify":classifier,....other > states} > > def state_machine(): > next_step = protocol["start"]() > while True: > next_step = protocol[next_step]() > I've just spent about an hour looking over this code, with a few comments to inject to the thread here: - To all those suggesting the OP convert to a dispatch table, be assured that this code is well aware of this idiom. It is used HEAVILY at a macro level, picking through the various HTML states (starting a tag, reading attributes, reading body, etc.). There still are a number of cascading if-elif's within some of these states, and some of them *may* be candidates for further optimization. - There is an underlying HTMLInputStream that seems to be doing some unnecessary position bookkeeping (positionLine and positionCol). Commenting this out increases my test speed by about 13%. In my ignorance, I may be removing some important behavior, but this does not seem to be critical as I tested against a few megs of HTML source. Before blaming the tokenizer for everything, there may be more performance to be wrung from the input stream processor. For that matter, I would guess that about 90% of all HTML files that this code would process would easily fit in memory - in that case, the stream processing (and all of the attendant "if I'm not at the end of the current chunk" code) could be skipped/removed entirely. - The HTMLInputStream's charsUntil code is an already-identified bottleneck, and some re enhancements have been applied here to help out. - Run-time construction of tuple literals where the tuple members are constants can be lifted out. emitCurrentToken rebuilds this tuple every time it is called (which is a lot!): if (token["type"] in (tokenTypes["StartTag"], tokenTypes ["EndTag"], tokenTypes["EmptyTag"])): Move this tuple literal into a class constant (or if you can tolerate it, a default method argument to gain LOAD_FAST benefits - sometimes optimization isn't pretty). - These kinds of optimizations are pretty small, and only make sense if they are called frequently. Tallying which states are called in my test gives the following list in decreasing frequency. Such a list would help guide your further tuning efforts: tagNameState 194848 dataState 182179 attributeNameState 116507 attributeValueDoubleQuotedState 114931 tagOpenState 105556 beforeAttributeNameState 58612 beforeAttributeValueState 58216 afterAttributeValueState 58083 closeTagOpenState 50547 entityDataState 1673 attributeValueSingleQuotedState 1098 commentEndDashState 372 markupDeclarationOpenState 370 commentEndState 364 commentStartState 362 commentState 362 selfClosingStartTagState 359 doctypePublicIdentifierDoubleQuotedState 291 doctypeSystemIdentifierDoubleQuotedState 247 attributeValueUnQuotedState 191 doctypeNameState 32 beforeDoctypePublicIdentifierState 16 afterDoctypePublicIdentifierState 14 afterDoctypeNameState 9 doctypeState 8 beforeDoctypeNameState 8 afterDoctypeSystemIdentifierState 6 afterAttributeNameState 5 commentStartDashState 2 bogusCommentState 2 For instance, I wouldn't bother doing much tuning of the bogusCommentState. Anything called fewer than 50,000 times in this test doesn't look like it would be worth the trouble. -- Paul (Thanks to those who suggested pyparsing as an alternative, but I think this code is already beyond pyparsing in a few respects. For one thing, this code works with an input stream, in order to process large HTML files; pyparsing *only* works with an in-memory string. This code can also take advantage of some performance short cuts, knowing that it is parsing HTML; pyparsing's generic classes can't do that.) From stefan_ml at behnel.de Sun Jul 5 07:52:07 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 05 Jul 2009 13:52:07 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a501a5e$0$1640$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> Message-ID: <4a5093e7$0$31338$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > Here's some actual code, from "tokenizer.py". This is called once > for each character in an HTML document, when in "data" state (outside > a tag). It's straightforward code, but look at all those > dictionary lookups. > > def dataState(self): > data = self.stream.char() > > # Keep a charbuffer to handle the escapeFlag > if self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]): > if len(self.lastFourChars) == 4: > self.lastFourChars.pop(0) > self.lastFourChars.append(data) > > # The rest of the logic > if data == "&" and self.contentModelFlag in\ > (contentModelFlags["PCDATA"], contentModelFlags["RCDATA"]) and > not\ > self.escapeFlag: > self.state = self.states["entityData"] > elif data == "-" and self.contentModelFlag in\ > (contentModelFlags["CDATA"], contentModelFlags["RCDATA"]) and > not\ > self.escapeFlag and "".join(self.lastFourChars) == "": > self.escapeFlag = False > self.tokenQueue.append({"type": "Characters", "data":data}) > elif data == EOF: > # Tokenization ends. > return False > elif data in spaceCharacters: > # Directly after emitting a token you switch back to the "data > # state". At that point spaceCharacters are important so > they are > # emitted separately. > self.tokenQueue.append({"type": "SpaceCharacters", "data": > data + self.stream.charsUntil(spaceCharacters, True)}) > # No need to update lastFourChars here, since the first > space will > # have already broken any sequences > else: > chars = self.stream.charsUntil(("&", "<", ">", "-")) > self.tokenQueue.append({"type": "Characters", "data": > data + chars}) > self.lastFourChars += chars[-4:] > self.lastFourChars = self.lastFourChars[-4:] > return True Giving this some more thought, I'd also try is to split the huge if-elif-else block like this: if data in string_with_all_special_characters: if data == '&' ...: ... else: ... So there are three things to improve: - eliminate common subexpressions which you know are constant - split the large conditional sequence as shown above - use separate dataState() methods when inside and outside of CDATA/RCDATA blocks and (maybe) escaped blocks Stefan From pm567426 at gmail.com Sun Jul 5 08:09:49 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 05:09:49 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> On Jul 5, 1:57?pm, Mark Dickinson wrote: > On Jul 5, 8:38?am, Pedram wrote: > > > Hello, > > I'm reading about implementation of long ints in Python. I downloaded > > the source code of CPython and will read the longobject.c, but from > > where I should start reading this file? I mean which function is the > > first? > > I don't really understand the question: ?what do you mean by 'first'? > It might help if you tell us what your aims are. > > In any case, you probably also want to look at the Include/ > longintrepr.h and Include/longobject.h files. > > Mark Thanks for reply, Sorry I can't explain too clear! I'm not English ;) But I want to understand the implementation of long int object in Python. How Python allocates memory and how it implements operations for this object? Although, I'm reading the source code (longobject.c and as you said, longintrepr.h and longobject.h) but if you can help me, I really appreciate that. Pedram From drobinow at gmail.com Sun Jul 5 08:21:33 2009 From: drobinow at gmail.com (David Robinow) Date: Sun, 5 Jul 2009 08:21:33 -0400 Subject: Is code duplication allowed in this instance? In-Reply-To: References: Message-ID: <4eb0089f0907050521n8d0f6eo271ec18c10040290@mail.gmail.com> On Sun, Jul 5, 2009 at 5:54 AM, Lawrence D'Oliveiro wrote: > In message c1d1c62d69fb at y17g2000yqn.googlegroups.com>, Klone wrote: > >> So in this scenario is it OK to duplicate the algorithm to be tested >> within the test codes or refactor the method such that it can be used >> within test codes to verify itself(??). > > I think you should be put on the management fast-track. Heavens, no. He's too valuable as a managee. From mjohnson at cfi.co.ug Sun Jul 5 08:27:12 2009 From: mjohnson at cfi.co.ug (Johnson Mpeirwe) Date: Sun, 5 Jul 2009 16:27:12 +0400 Subject: PSP Caching In-Reply-To: <3e1c0979-fb6b-4d6e-80e5-a4af8d5fd96a@a36g2000yqc.googlegroups.com> References: <3e1c0979-fb6b-4d6e-80e5-a4af8d5fd96a@a36g2000yqc.googlegroups.com> Message-ID: <20090705122217.M97570@cfi.co.ug> Thanks Simon, I got around this behavior by adding "MaxRequestsPerChild 1" (default value of this is 0) to my httpd.conf to limit the number of requests a child server process will handle before it dies but I think it is important to keep it 0 in production environment. Regards, Johnson On Fri, 3 Jul 2009 10:44:52 -0700 (PDT), Simon Forman wrote > On Jul 3, 5:18?am, Johnson Mpeirwe wrote: > > Hello All, > > > > How do I stop caching of Python Server Pages (or whatever causes changes > > in a page not to be noticed in a web browser)? I am new to developing > > web applications in Python and after looking at implementations of PSP > > like Spyce (which I believed introduces new unnecessary non-PSP syntax), > > I decided to write my own PSP applications from scratch. When I modify a > > file, I keep getting the old results until I intentionally introduce an > > error (e.g parse error) and correct it after to have the changes > > noticed. There's no proxy (I am working on a windows machine unplugged > > from the network). I have Googled and no documents seem to talk about > > this. Is there any particular mod_python directive I must set in my > > Apache configuration to fix this? > > > > Any help will be highly appreciated. > > > > Johnson > > I don't know much about caching with apache, but the answer mght be > on this page: http://httpd.apache.org/docs/2.2/caching.html > > Meanwhile, couldn't you just send apache a restart signal when you > modify your code? > > HTH, > ~Simon > -- > http://mail.python.org/mailman/listinfo/python-list From dickinsm at gmail.com Sun Jul 5 09:04:11 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sun, 5 Jul 2009 06:04:11 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> Message-ID: <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> On Jul 5, 1:09?pm, Pedram wrote: > Thanks for reply, > Sorry I can't explain too clear! I'm not English ;) That's shocking. Everyone should be English. :-) > But I want to understand the implementation of long int object in > Python. How Python allocates memory and how it implements operations > for this object? I'd pick one operation (e.g., addition), and trace through the relevant functions in longobject.c. Look at the long_as_number table to see where to get started. In the case of addition, that table shows that the nb_add slot is given by long_add. long_add does any necessary type conversions (CONVERT_BINOP) and then calls either x_sub or x_add to do the real work. x_add calls _PyLong_New to allocate space for a new PyLongObject, then does the usual digit-by-digit-with-carry addition. Finally, it normalizes the result (removes any unnecessary zeros) and returns. As far as memory allocation goes: almost all operations call _PyLong_New at some point. (Except in py3k, where it's a bit more complicated because small integers are cached.) If you have more specific questions I'll have a go at answering them. Mark From http Sun Jul 5 09:12:25 2009 From: http (Paul Rubin) Date: 05 Jul 2009 06:12:25 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> <025eaa4f$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7x1vovxohy.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > > I wouldn't say Python's None is terrible,... > No, wait, I tell I lie... re.search() sometimes bites me, because > sometimes it returns None and sometimes it returns a matchobject and I > don't use re often enough to have good habits with it yet. re is a common source of this type of bug but there are others. > There are three natural approaches to (say) re.search() for dealing with > failure: > > (1) return a sentinel value like None; > (2) return a matchobject which tests False; > (3) raise an exception. 4. Have re.search return a bool and possible matchobject separately: put_match_here = [] if re.search(pat, s, target=put_match_here): do_something_with(put_match_here[0]) or alternatively (cleaner), have a new type of object which supports search operations while self-updating with the match object: mt = re.match_target() ... if mt.search(pat, s): do_something_with(mt.match) if mt.search(pat2, s): do_another_thing_with(mt.match) ... This is sort of inspired by what Perl does. I often do something like this because it makes it cleaner to chain a series of matches. From mail at microcorp.co.za Sun Jul 5 09:45:45 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 5 Jul 2009 15:45:45 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> Message-ID: <00f201c9fd76$e61565a0$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >On Sun, 05 Jul 2009 10:12:54 +0200, Hendrik van Rooyen wrote: > >> Python is not C. > >John Nagle is an old hand at Python. He's perfectly aware of this, and >I'm sure he's not trying to program C in Python. > >I'm not entirely sure *what* he is doing, and hopefully he'll speak up >and say, but whatever the problem is it's not going to be as simple as >that. I am well aware that John is not a newbie. He was complaining about Python's lack of a case statement in the context of a state machine. The point I was trying to make is that, if any state machine is examined, then, if you examine any one state, the reasons for leaving it ("state transitions") is always a subset of the choices that _can_ be made. So that drawing a circle round each state in a state diagram, and making a routine to examine the arrows leaving that circle, and returning the destination point of the chosen arrow, is a way of splitting the job up, and results in making only the relevant decisions at the time of their relevance. This is in contrast to the classic C way of making one big case statement to implement a finite state machine, which gets its efficiency (if any) out of compiler optimisations such as replacing a skip chain with a jump table. I understand that it leads to a lot of what looks like boilerplate code, but he was looking for speed... - Hendrik From mail at microcorp.co.za Sun Jul 5 10:03:05 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 5 Jul 2009 16:03:05 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net><7xvdm8thpo.fsf@ruckus.brouhaha.com><4a4fe6cb$0$1591$742ec2ed@news.sonic.net><7xfxdcqabg.fsf@ruckus.brouhaha.com><4a501a5e$0$1640$742ec2ed@news.sonic.net><0260559c$0$20657$c3e8da3@news.astraweb.com><7xws6nik0q.fsf@ruckus.brouhaha.com><0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: <00f901c9fd79$51d12160$0d00a8c0@Hendrik> "Paul Rubin" wrote: > The series of tests is written that way because there is no case > statement available. It is essentially switching on a bunch of > character constants and then doing some additional tests in each > branch. > > It could be that using ord(c) as an index into a list of functions > might be faster than a dict lookup on c to get a function. I think > John is hoping to avoid a function call and instead get an indexed > jump within the Python bytecode for the big function. I agree about the ord(c). However, avoiding the function call is, I think, right now, in the realms of wishful thinking. I cannot see how you could avoid a python function call - even if he bites the bullet and implements my laborious scheme, he would still have to fetch the next character to test against, inside the current state. So if it is the function calls that is slowing him down, I cannot imagine a solution using less than one per character, in which case he is screwed no matter what he does. But wait - maybe if he passes an iterator around - the equivalent of for char in input_stream... Still no good though, unless the next call to the iterator is faster than an ordinary python call. - Hendrik From pm567426 at gmail.com Sun Jul 5 10:15:11 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 07:15:11 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> Message-ID: On Jul 5, 5:04?pm, Mark Dickinson wrote: > That's shocking. ?Everyone should be English. :-) Yes, I'm trying :) > I'd pick one operation (e.g., addition), and trace through the > relevant functions in longobject.c. ?Look at the long_as_number > table to see where to get started. > > In the case of addition, that table shows that the nb_add slot is > given by long_add. ?long_add does any necessary type conversions > (CONVERT_BINOP) and then calls either x_sub or x_add to do the real > work. > x_add calls _PyLong_New to allocate space for a new PyLongObject, then > does the usual digit-by-digit-with-carry addition. ?Finally, it > normalizes > the result (removes any unnecessary zeros) and returns. > > As far as memory allocation goes: almost all operations call > _PyLong_New at some point. ?(Except in py3k, where it's a bit more > complicated because small integers are cached.) Oh, I didn't see long_as_number before. I'm reading it. That was very helpful, thanks. > If you have more specific questions I'll have a go at answering them. > > Mark Thank you a million. I will write your name in my "Specially thanks to" section of my article (In font size 72) ;) Pedram From l.mastrodomenico at gmail.com Sun Jul 5 10:25:31 2009 From: l.mastrodomenico at gmail.com (Lino Mastrodomenico) Date: Sun, 5 Jul 2009 16:25:31 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <00f901c9fd79$51d12160$0d00a8c0@Hendrik> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> <00f901c9fd79$51d12160$0d00a8c0@Hendrik> Message-ID: 2009/7/5 Hendrik van Rooyen : > I cannot see how you could avoid a python function call - even if he > bites the bullet and implements my laborious scheme, he would still > have to fetch the next character to test against, inside the current state. > > So if it is the function calls that is slowing him down, I cannot > imagine a solution using less than one per character, in which > case he is screwed no matter what he does. A simple solution may be to read the whole input HTML file in a string. This potentially requires lots of memory but I suspect that the use case by far most common for this parser is to build a DOM (or DOM-like) tree of the whole document. This tree usually requires much more memory that the HTML source itself. So, if the code duplication is acceptable, I suggest keeping this implementation for cases where the input is extremely big *AND* the whole program will work on it in "streaming", not just the parser itself. Then write a simpler and faster parser for the more common case when the data is not huge *OR* the user will keep the whole document in memory anyway (e.g. on a tree). Also: profile, profile a lot. HTML pages are very strange beasts and the bottlenecks may be in innocent-looking places! -- Lino Mastrodomenico From tn.pablo at gmail.com Sun Jul 5 10:30:47 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Sun, 5 Jul 2009 09:30:47 -0500 Subject: How Python Implements "long integer"? In-Reply-To: References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: On Sun, Jul 5, 2009 at 04:57, Mark Dickinson wrote: > On Jul 5, 8:38?am, Pedram wrote: >> Hello, >> I'm reading about implementation of long ints in Python. I downloaded >> the source code of CPython and will read the longobject.c, but from >> where I should start reading this file? I mean which function is the >> first? > > I don't really understand the question: ?what do you mean by 'first'? > It might help if you tell us what your aims are. I think he means the entry point, problem is that libraries have many. -- Pablo Torres N. From asm198 at gmail.com Sun Jul 5 10:56:26 2009 From: asm198 at gmail.com (Icarus) Date: Sun, 5 Jul 2009 07:56:26 -0700 (PDT) Subject: Problems with using queue in Tkinter application References: <0dae5496-622a-4fca-a2f5-6ba2ecac1284@r33g2000yqn.googlegroups.com> Message-ID: <39985616-d6b4-42f5-9db6-e7827f97c21d@24g2000yqm.googlegroups.com> On Jul 4, 11:24?am, Peter Otten <__pete... at web.de> wrote: > Icarus wrote: > > On Jul 4, 3:21 am, Peter Otten <__pete... at web.de> wrote: > >> Icarus wrote: > >> > I'm working on a serial protocol analyzer in python. ?We have an > >> > application written by someone else in MFC but we need something that > >> > is cross platform. ?I intended to implement the GUI portion in Tkinter > >> > but am having trouble. > > >> > The idea is that I will read messages from the serial port and output > >> > them to a Tkinter Text object initially. ?Eventually it will have > >> > other functionality but that's it for the short term. ?I've written > >> > this little test app to experiment with putting things on the GUI via > >> > a Queue which is polled by the Tkinter loop. > > >> > On some machines this code works fine and I get whatever I type in > >> > displayed in the Text widget. ?On others I get errors like this as > >> > soon as I start it running. > > >> > error in background error handler: > >> > out of stack space (infinite loop?) > >> > while executing > >> > "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level > >> > 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) > >> > while execu..." > > >> > I don't understand why on some machines it works exactly as expected > >> > and on others it acts the same way Tkinter does when I call functions > >> > directly from outside the Tkinter thread. ?Does anyone have any > >> > suggestions? ?The full code as appended below. ?Thanks in advance. > > >> > [code] > > >> > import Queue > > >> > class functionQueue: > > >> > def __init__(self, root = None, timeout = 250): > > >> > self.functionQueue = Queue.Queue() > >> > self.root = root > >> > self.timeout = timeout > > >> > if(self.root): > >> > self.pop_function(root) > > >> > def pop_function(self, root = None): > > >> > try: > >> > funcArgList = self.functionQueue.get(block = False) > >> > except Queue.Empty: > >> > pass > >> > else: > >> > try: > >> > funcArgList[0](*funcArgList[1]) > >> > except: > >> > try: > >> > print "Failed to call function", funcArgList[0] > >> > except: > >> > print "Failed to call function" > > >> > if(root): > >> > root.after(self.timeout, lambda: self.pop_function > >> > (self.root)) > > >> > def add_function(self, function, argList): > > >> > try: > >> > self.functionQueue.put([function, argList]) > >> > except: > >> > pass > > >> > if( __name__ == '__main__'): > > >> > import Tkinter > >> > import thread > > >> > text = Tkinter.Text() > >> > text.pack() > > >> > myQueue = functionQueue(text, 50) > > >> > def gui_loop(): > >> > try: > >> > text.mainloop() > >> > except: > >> > import os > >> > os._exit(1) > > >> > thread.start_new_thread(text.mainloop, ()) > > >> > while(True): > >> > usrInput = raw_input() > > >> > if(usrInput == "-1"): > >> > import os > >> > os._exit(0) > > >> > myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > >> > myQueue.add_function(text.see, ['end']) > > >> > [/code] > > >> I can make it work over here by putting the UI into the main thread, as > >> suggested byhttp://effbot.org/zone/tkinter-threads.htm: > > >> import Queue > >> import Tkinter > >> import threading > > >> class FunctionQueue: > >> # unchanged > > >> def input_loop(): > >> while True: > >> try: > >> usrInput = raw_input() > >> except EOFError: > >> break > >> myQueue.add_function(text.insert, ['end', usrInput + "\n"]) > >> myQueue.add_function(text.see, ['end']) > >> myQueue.add_function(text.quit, []) > > >> if __name__ == '__main__': > >> text = Tkinter.Text() > >> text.pack() > > >> myQueue = FunctionQueue(text, 50) > >> threading.Thread(target=input_loop).start() > >> text.mainloop() > > >> Peter > > > Peter, thanks for the suggestion. ?I tried your code exactly on my box > > and I still get the same results. ?As soon as I run the script and > > every time I click on the Text box I get tcl::Bgerror ... just like I > > mentioned above. ?I'm fairly certain that I'm not calling Tkinter > > functions from any other thread but it's acting as though I am as soon > > as I create the input thread. > > If I comment out the input loop thread everything is fine but of > > course that's not terribly useful as a logging box. > > http://bugs.python.org/issue3835 > > Could tcl have been built without thread support on the failing machines? > > Peter You had it Peter. I tried the "import pydoc pydoc.gui()" in the bug report you referenced and the same thing described there occurred. After recompiling tcl/tk with --threads-enabled and replacing the slackware default packages with those everything is working as I expected. Thanks for the help. From aahz at pythoncraft.com Sun Jul 5 11:08:23 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Jul 2009 08:08:23 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: In article , Hendrik van Rooyen wrote: > >But wait - maybe if he passes an iterator around - the equivalent of >for char in input_stream... Still no good though, unless the next call >to the iterator is faster than an ordinary python call. Calls to iterators created by generators are indeed faster than an ordinary Python call, because the stack frame is already mostly set up. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From nomail at thank.you Sun Jul 5 11:26:21 2009 From: nomail at thank.you (jack catcher (nick)) Date: Sun, 05 Jul 2009 18:26:21 +0300 Subject: Python and webcam capture delay? Message-ID: Hi, I'm thinking of using Python for capturing and showing live webcam stream simultaneously between two computers via local area network. Operating system is Windows. I'm going to begin with VideoCapture extension, no ideas about other implementation yet. Do you have any suggestions on how short delay I should hope to achieve in showing the video? This would be part of a psychological experiment, so I would need to deliver the video stream with a reasonable delay (say, below 100ms). From serverin2000 at yahoo.com Sun Jul 5 11:56:12 2009 From: serverin2000 at yahoo.com (RAM) Date: Sun, 5 Jul 2009 08:56:12 -0700 (PDT) Subject: generation of keyboard events Message-ID: Hi, I need to start an external program and pass the keyboard events like F1,Right arrow key etc to the program..I am trying to use the subprocess module to invoke the external program. I am able to invoke but not able to generate the keyboard events and pass them on to the external progam. Please help me in this because I am a beginner. regards Sreerama V From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 12:00:03 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 16:00:03 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: <0260bf8d$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 03:08:16 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> > Yes, it saves a few keystrokes to say "if x:" instead of "if >> > len(x)==0:" or even "if bool(x):", >> >> It's not about saving keystrokes -- that's a furphy. It's about >> encapsulation. Objects are in a better position to recognise when they >> are "something" (true) or "nothing" (false) than you are. > > I don't know what a furphy is, Is your Google broken? *wink* http://en.wikipedia.org/wiki/Furphy > but I don't accept that "somethingness" > vs. "nothingness" is the same distinction as truth vs falsehood. It's the distinction used by Python since the dawn of time. Python only grew a bool type a few versions back. > True > and False are values in a specific datatype (namely bool), not abstract > qualities of arbitrary data structures. I'm not talking about the constants True and False (nouns), but about true and false values (adjectives). > The idea that the "if" > statement selects between "somethingness" and "nothingness" rather than > between True and False is a bogus re-imagining of the traditional > function of an "if" statement There's nothing bogus about it. Some languages such as Pascal and Java require a special Boolean type for if-branches. Other languages, like Forth, C, Lisp and Ruby do not. http://en.wikipedia.org/wiki/Boolean_data_type > and has been an endless source of bugs in Python code. I wonder why these "endless" bugs weren't important enough to be mentioned in the rationale to PEP 285: http://www.python.org/dev/peps/pep-0285/ You'd think something as vital as "if x Considered Harmful" would have made it into the PEP, but no. Instead Guido *explicitly* stated that he had no intention of forcing `if` to require a bool, describing `if x` as the "correct form" and calling scrapping such a feature as "crippling the language". > Look how much confusion it causes here in the newsgroup all the time. The only confusion is that you're causing me. Would you care to link to some? >> "if len(x) == 0" is wasteful. Perhaps I've passed you a list-like >> iterable instead of a list, and calculating the actual length is O(N). > > That doesn't happen in any of Python's built-in container types. And if they were the only types possible in Python, that might be relevant. > I > could see some value to having a generic "is_empty" predicate on > containers though, to deal with this situation. We have that already. It's spelled __bool__ or __nonzero__, and it applies to any object, not just containers. > Your iterable could > support that predicate. In fact maybe all iterables should support that > predicate. They don't (and can't) all support "len". Iterators are a special case, because in general they can't tell if they're exhausted until they try to yield a value. >> If you write len(x)==0 Python doesn't complain if x is a dict instead >> of the list you were expecting. Why is it acceptable to duck-type >> len(x) but not truth-testing? > > I haven't seen the amount of bugs coming from generic "len" as from > something-vs-nothing confusion. Again with these alleged bugs. -- Steven From pm567426 at gmail.com Sun Jul 5 12:01:30 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 09:01:30 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> Hello again, This time I have a simple C question! As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I found PyObject_NEW_VAR in objimpl.h header file. But I can't understand the last line :( Here's the code: #define PyObject_NEW_VAR(type, typeobj, n) \ ( (type *) PyObject_InitVar( \ (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), (n)) ),\ (typeobj), (n)) ) I know this will replace the PyObject_New_VAR(type, typeobj, n) everywhere in the code and but I can't understand the last line, which is just 'typeobj' and 'n'! What do they do? Are they make any sense in allocation process? From aahz at pythoncraft.com Sun Jul 5 12:12:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 Jul 2009 09:12:19 -0700 Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> Message-ID: In article <6f6be2b9-49f4-4db0-9c21-52062d8ea3df at l31g2000yqb.googlegroups.com>, Pedram wrote: > >This time I have a simple C question! >As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I >found PyObject_NEW_VAR in objimpl.h header file. But I can't >understand the last line :( Here's the code: > >#define PyObject_NEW_VAR(type, typeobj, n) \ >( (type *) PyObject_InitVar( \ > (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), >(n)) ),\ > (typeobj), (n)) ) > >I know this will replace the PyObject_New_VAR(type, typeobj, n) >everywhere in the code and but I can't understand the last line, which >is just 'typeobj' and 'n'! What do they do? Are they make any sense in >allocation process? Look in the code to find out what PyObject_InitVar() does -- and, more importantly, what its signature is. The clue you're missing is the trailing backslash on the third line, but that should not be required if you're using an editor that shows you matching parentheses. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From usernet at ilthio.net Sun Jul 5 12:12:37 2009 From: usernet at ilthio.net (Tim Harig) Date: Sun, 05 Jul 2009 16:12:37 GMT Subject: generation of keyboard events References: Message-ID: On 2009-07-05, RAM wrote: > I need to start an external program and pass the keyboard events like > F1,Right arrow key etc to the program..I am trying to use the > subprocess module to invoke the external program. I am able to invoke > but not able to generate the keyboard events and pass them on to the catb.org/esr/faqs/smart-questions.html You have told us nothing about the environment where you are trying to accomplish this. GUI, CLI, Unix, Windows, etc? So I suggest that you checkout the curses getch functions. You can find them in the standard library documentation at http://docs.python.org. You should also reference documentation for the C version in your systems man pages. From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 12:13:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 16:13:30 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: <0260c2b4$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 11:37:49 +0000, Lie Ryan wrote: > Neither python's `if` nor `if` in formal logic is about testing True vs. > False. `if` in python and formal logic receives a statement. The > statement must be evaluatable to True or False, but does not have to be > True or False themselves. It just happens that True evaluates to True > and False evaluates to False. I think your explanation is a little confused, or at least confusing. `if` implements a two-way branch. Some languages, like Pascal and Java, requires the switch value to take one of two specific enumerable values conventionally spelled TRUE and FALSE (modulo variations in case). Other languages don't require specific enumerable values, and instead accept (e.g.) any integer, or any object, with rules for how to interpret such values in such a context. Forth, for example, branches according to whether the word on the stack is zero or non-zero: "nothing" or "something". Lisp branches according to empty list or non-empty list: "nothing" or "something" again. Other languages, like Ruby, have less intuitive rules. That's their problem. -- Steven From emile at fenx.com Sun Jul 5 12:19:48 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 05 Jul 2009 09:19:48 -0700 Subject: generation of keyboard events In-Reply-To: References: Message-ID: On 7/5/2009 8:56 AM RAM said... > Hi, > > I need to start an external program and pass the keyboard events like > F1,Right arrow key etc to the program..I am trying to use the > subprocess module to invoke the external program. I am able to invoke > but not able to generate the keyboard events and pass them on to the > external progam. If you're on *nix, search for python and expect and you'll find something based apps -- I'm not sure what you do for GUIs. On windows, I'm sure there are ways of doing this with the win32 extensions, but I commonly use msched from www.mjtnet.com. I create msched scripts from within python and use subprocess to invoke msched and execute the python generated script. HTH, Emile > Please help me in this because I am a beginner. > > regards > Sreerama V From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 12:20:32 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 05 Jul 2009 16:20:32 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7x4otu1rkw.fsf@ruckus.brouhaha.com> <7x3a9egy9j.fsf@ruckus.brouhaha.com> <2f0a035e-ad2e-4364-a224-d4e8c1e8110a@j32g2000yqh.googlegroups.com> <7xhbxtjxtn.fsf@ruckus.brouhaha.com> <025eaa4f$0$20657$c3e8da3@news.astraweb.com> <7x1vovxohy.fsf@ruckus.brouhaha.com> Message-ID: <0260c45a$0$20657$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 06:12:25 -0700, Paul Rubin wrote: >> There are three natural approaches to (say) re.search() for dealing >> with failure: >> >> (1) return a sentinel value like None; (2) return a matchobject which >> tests False; (3) raise an exception. > > 4. Have re.search return a bool and possible matchobject separately: > > put_match_here = [] > if re.search(pat, s, target=put_match_here): > do_something_with(put_match_here[0]) Wow. I never for the life of me thought I'd see an experienced Python programmer re-implement Pascal's VAR parameters. That is... hideous. Returning a (flag, matchobject) tuple is the height of beauty in comparison. -- Steven From pm567426 at gmail.com Sun Jul 5 12:32:41 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 09:32:41 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> Message-ID: <179f64f8-6528-4953-88b1-4096d5907cbe@p29g2000yqh.googlegroups.com> On Jul 5, 8:12?pm, a... at pythoncraft.com (Aahz) wrote: > In article <6f6be2b9-49f4-4db0-9c21-52062d8ea... at l31g2000yqb.googlegroups.com>, > > > > Pedram ? wrote: > > >This time I have a simple C question! > >As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I > >found PyObject_NEW_VAR in objimpl.h header file. But I can't > >understand the last line :( Here's the code: > > >#define PyObject_NEW_VAR(type, typeobj, n) \ > >( (type *) PyObject_InitVar( \ > > ? ? ?(PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), > >(n)) ),\ > > ? ? ?(typeobj), (n)) ) > > >I know this will replace the PyObject_New_VAR(type, typeobj, n) > >everywhere in the code and but I can't understand the last line, which > >is just 'typeobj' and 'n'! What do they do? Are they make any sense in > >allocation process? > > Look in the code to find out what PyObject_InitVar() does -- and, more > importantly, what its signature is. ?The clue you're missing is the > trailing backslash on the third line, but that should not be required if > you're using an editor that shows you matching parentheses. > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "as long as we like the same operating system, things are cool." --piranha No, they wrapped the 3rd line! I'll show you the code in picture below: http://lh3.ggpht.com/_35nHfALLgC4/SlDVMEl6oOI/AAAAAAAAAKg/vPWA1gttvHM/s640/Screenshot.png As you can see the PyObject_MALLOC has nothing to do with typeobj and n in line 4. From pm567426 at gmail.com Sun Jul 5 12:34:37 2009 From: pm567426 at gmail.com (Pedram) Date: Sun, 5 Jul 2009 09:34:37 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <6f6be2b9-49f4-4db0-9c21-52062d8ea3df@l31g2000yqb.googlegroups.com> <179f64f8-6528-4953-88b1-4096d5907cbe@p29g2000yqh.googlegroups.com> Message-ID: <5a0b1063-ae2b-4229-b040-4904b4d01077@b15g2000yqd.googlegroups.com> On Jul 5, 8:32?pm, Pedram wrote: > On Jul 5, 8:12?pm, a... at pythoncraft.com (Aahz) wrote: > > > > > In article <6f6be2b9-49f4-4db0-9c21-52062d8ea... at l31g2000yqb.googlegroups.com>, > > > Pedram ? wrote: > > > >This time I have a simple C question! > > >As you know, _PyLong_New returns the result of PyObject_NEW_VAR. I > > >found PyObject_NEW_VAR in objimpl.h header file. But I can't > > >understand the last line :( Here's the code: > > > >#define PyObject_NEW_VAR(type, typeobj, n) \ > > >( (type *) PyObject_InitVar( \ > > > ? ? ?(PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj), > > >(n)) ),\ > > > ? ? ?(typeobj), (n)) ) > > > >I know this will replace the PyObject_New_VAR(type, typeobj, n) > > >everywhere in the code and but I can't understand the last line, which > > >is just 'typeobj' and 'n'! What do they do? Are they make any sense in > > >allocation process? > > > Look in the code to find out what PyObject_InitVar() does -- and, more > > importantly, what its signature is. ?The clue you're missing is the > > trailing backslash on the third line, but that should not be required if > > you're using an editor that shows you matching parentheses. > > -- > > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > > "as long as we like the same operating system, things are cool." --piranha > > No, they wrapped the 3rd line! > > I'll show you the code in picture below:http://lh3.ggpht.com/_35nHfALLgC4/SlDVMEl6oOI/AAAAAAAAAKg/vPWA1gttvHM... > > As you can see the PyObject_MALLOC has nothing to do with typeobj and > n in line 4. Oooooh! What a mistake! I got it! they're Py_Object_InitVar parameters. Sorry and Thanks! From lie.1296 at gmail.com Sun Jul 5 12:43:35 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 05 Jul 2009 16:43:35 GMT Subject: question of style In-Reply-To: <0260c2b4$0$20657$c3e8da3@news.astraweb.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> <0260c2b4$0$20657$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 05 Jul 2009 11:37:49 +0000, Lie Ryan wrote: > >> Neither python's `if` nor `if` in formal logic is about testing True vs. >> False. `if` in python and formal logic receives a statement. The >> statement must be evaluatable to True or False, but does not have to be >> True or False themselves. It just happens that True evaluates to True >> and False evaluates to False. > > I think your explanation is a little confused, or at least confusing. Indeed, partially because I said "statement" when I really meant "expression". > Other languages don't require specific enumerable values, and instead > accept (e.g.) any integer, or any object, with rules for how to interpret > such values in such a context. That was what I was wanting to say, except that I stretched that to formal logic (mathematical logic). Even in formal logic `if` receives any arbitrary expression that can be -- according to certain rules -- interpreted as True or False (i.e. the expressions themselves are not required to be a boolean value). The conclusion is python's `if` does not deviate from `if`'s semantic in mathematical sense. From piet at cs.uu.nl Sun Jul 5 13:28:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 05 Jul 2009 19:28:35 +0200 Subject: Determining if a function is a method of a class within a decorator References: <4A4963E9.30202@ilm.com> Message-ID: >>>>> David Hirschfield (DH) wrote: >DH> Yeah, it definitely seems like having two separate decorators is the >DH> solution. But the strange thing is that I found this snippet after some >DH> deep googling, that seems to do something *like* what I want, though I >DH> don't understand the descriptor stuff nearly well enough to get what's >DH> happening: >DH> http://stackoverflow.com/questions/306130/python-decorator-makes-function-forget-that-it-belongs-to-a-class >DH> answer number 3, by ianb. It seems to indicate there's a way to introspect >DH> and determine the class that the function is going to be bound to...but I >DH> don't get it, and I'm not sure it's applicable to my case. >DH> I'd love an explanation of what is going on in that setup, and if it isn't >DH> usable for my situation, why not? What that example does is not getting the name of the class in the decorator, but in the bound method that is the result of the decorator when that method is called. This is just done by asking for the class of the self parameter. Actually they even don't do that in that example but it could have been done. Note also that there is an error in the code: keyargs should be kw. There is also something special in that code: it uses the descriptor protocol. This is necessary for a method. The descriptor protocol for methods defines a __get__ method that transforms the unbound method into a bound method. That code uses this to decorate the generated bound method object instead of decorating the unbound method. A side effect of doing the class detection at call time is that you get the name of the subclass if you use the method on an instance of the subclass, not the name of the class that the method was defined in: class D(C): pass D().f(1, 2) will talk about class D, not class C. So if you would like to do something special for bound methods the __get__ might be the proper place to do it. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From a0046088 at airmail.net Sun Jul 5 14:20:26 2009 From: a0046088 at airmail.net (wwwayne) Date: Sun, 05 Jul 2009 12:20:26 -0600 Subject: Clarity vs. code reuse/generality References: Message-ID: <3in15557eu33n494hh3lvmcnssujl2ueds@4ax.com> On Fri, 03 Jul 2009 14:34:58 GMT, Alan G Isaac wrote: >On 7/3/2009 10:05 AM kj apparently wrote: === 8< === >2. >from scipy.optimize import bisect >def _binary_search(lo, hi, func, target, epsilon): > def f(x): return func(x) - target > return bisect(f, lo, high, xtol=epsilon) > >3. If you don't want to use SciPy (why?), have them >implement http://en.wikipedia.org/wiki/Bisection_method#Pseudo-code >to produce their own `bisect` function. Of course this isn't really pseudo-code, it's VB code with quite poor comments: 'Bisection Method 'Start loop Do While (abs(right - left) > 2*epsilon) 'Calculate midpoint of domain midpoint = (right + left) / 2 'Find f(midpoint) If ((f(left) * f(midpoint)) > 0) Then 'Throw away left half left = midpoint Else 'Throw away right half right = midpoint End If Loop Return (right + left) / 2 and even just throwing away the VB code and leaving the comments does not give a good algorithm: 'Bisection Method 'Start loop 'Calculate midpoint of domain 'Find f(midpoint) 'Throw away left half 'Throw away right half A much better approach to teaching introductory programming in any language at almost any level is to incorporate some "top down problem solving", including writing a method of solution (algorithm) in some reasonably well-defined pseudo-code that can be easily elaborated and translated into one's target language (and, peferably, some reasonable-sized subset of related languages). This pseudo-code should then become comments (or equivalent) for the students to convert to real code, as in: Algorithm bisect (f, left, right, epsilon): # Bisection Method to find a root of a real continuous function f(x): # Assume f(x) changes sign between f(left) and f(right) and # we want a value not further than epsilon from a real root. # Begin with the domain left...right. # While the absolute value of (left - right) exceeds 2*epsilon: # Calculate the midpoint, mid, of the domain. # If the product of f(left) and f(mid) is positive: # Set left to mid; # Otherwise: # Set right to mid. # Return the midpoint of left...right. === And adapting this approach to kj's case is straightforward. Of course, what consitutes a suitable vocabulary and syntax for an algorithm pseudo-code language depends upon the target language(s), the tastes of the instructor, and the point in the lesson or course. My choice is for python+COBOL (as above) initially, soon incorporating the usual arithmetic and relational operators (and how soon and how many at once depends upon the level of the students: for an introductory university/college course in Computer Science or equivalent, where everyone should have a reasonable background in mathemtics notation as a prerequisite, this should be very soon and quite fast), arrays and subscripting, etc. But if we were to write this algorithm or kj's in python-like pseudo-code it would already *be* python codeor very close to it--which is why we should teach intorductory programming in python. Very soon students would be writing algorithms that required very little elaboration to be programs. But without including suitable problem solving and psudo-code algorithm writing there will soon come a time or an example where students are trying to think in code instead of in their natural language and don't have the experience and repertoire to be able to do that well. I hope that's not too pedantic or AR? wayne >hth, >Alan Isaac From dotancohen at gmail.com Sun Jul 5 14:32:56 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sun, 5 Jul 2009 21:32:56 +0300 Subject: A C++ user's introduction to Python: a really good read Message-ID: <880dece00907051132p117ca7e0sf5774d1eb5014c64@mail.gmail.com> Here is a C++ KDE programer's take on Python: http://majewsky.wordpress.com/2009/07/04/python-experiences-or-why-i-like-c-more/ Good read. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From nick.m.daly at gmail.com Sun Jul 5 14:48:06 2009 From: nick.m.daly at gmail.com (Nick Daly) Date: Sun, 5 Jul 2009 13:48:06 -0500 Subject: Method to separate unit-test methods and data? In-Reply-To: References: Message-ID: Hi, I was wondering if it's possible / if there are any simple methods known of storing unit-test functions and their data in separate files? Perhaps this is a strange request, but it does an excellent job of modularizing code. ?As far as revision control goes, it makes it easier to discern between new or changed test cases, and changes in the test data. I've worked through this idea a bit and actually have a nearly working model. ?For example, when testing a class, I have a test module, which contains a class for testing each method of the class. ?This allows me to generalize each method's parameters into each class, which can then be overridden by the config file's data, something like as follows (with a really arbitrary example, Python 2.5 code): Demo code (midpoint.py): ======================= class Midpoint(object): ? ?def __init__(self, a, b): ? ? ? ?self.a = a ? ? ? ?self.b = b ? ?def mid(): ? ? ? ?return (self.a + self.b) / 2.0 ? ?def sum() ? ? ? ?return (self.a + self.b) Testing Code (test_Midpoint.py): =============================== import unittest import midpoint import sys, ConfigParser as configparser # set up the config file that overrides each class's data config = configparser.SafeConfigParser() config.read(sys.argv[0]) class test_Midpoint_mid(unittest.TestCase): ? ?def __init__(self): ? ? ? ?# default testing values ? ? ? ?self.none_values = ((None, 1), ? ? ? ? ? ? ? ? ? ? ? ? ? ?(0, None)) ? ? ? ?# override the default values with the config file values ? ? ? ?for key, value in config.items(self.__class__.__name__): ? ? ? ? ? ?if value: ? ? ? ? ? ? ? ?setattr(self, key, value) ? ?# a few tests of the method ? ?def test_rejectNone(self): ? ? ? ?for tests in self.none_values: ? ? ? ? ? ?self.assertRaises(TypeError, ? ? ? ? ? ? ? ?midpoint.Midpoint(tests[0], tests[1]).mid) # and repeat the concept for class test_Midpoint_sum Config Code (test_Midpoint.cfg): ============================== # override the default values of the test class's members [test_Midpoint_mid] none_values = ((-1, None), ? ? ? ? ? ? ? (None, -12.8)) What I haven't yet figured out how to do though, is properly override the default class member values with values from the config file. ?The config file's data is loaded as a string instead of as a list, as I'd want. ?This causes all the tests to fail, as while none_values needs to be interpreted as a list, it is instead understood as: " ((-1, None),\n ? ? ? ? ? ? ? (None, -12.8))" Does anyone have any solutions for these problems? ?First, is there a known and simple way to separate unit-test data and methods into separate files? ?Secondly, if not, is there a simple way to convert strings into other base types, like lists, dictionaries, and so forth? Or, am I going to have to write my own list-parsing methods? ?Would pickling help? ?I haven't yet had a chance to look into if or how that would work... ?If there's anything else I can clarify about this request, feel free to let me know. Thanks for any help you can provide, Nick From maymunbeyin at gmail.com Sun Jul 5 14:59:01 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 11:59:01 -0700 (PDT) Subject: Creating alot of class instances? Message-ID: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Hi I am new to Python classes and trying to figure out this particular issue here. I will need to create instances of a class. But at the moment I do not know how many instances I will end up having, in every case it might be different. Most of the documents I read makes this simpl class-student analogy to explain python classes which is fine. But in those examples the number and the names of the instances were known and limited. I will be querying some data and create class instances based on the data I gather. But the problem as I mentioned is that I do not know the names and the number of the end class instances. They will be based on the content of the data. So how can I create class instances within a loop and when the loop is done how can I figure out the list of instances via class membership? I can track the names by introducing another list but I want to understand the class side of things. The solution might be dead simple but I just cannot figure out at the moment. For example this is what I need in the simplest form class myclass(): def __init__(self,name): self.name=name for count,data in enumerate(some list): instance_count=myclass() instance_count.name=data print instances thanks From tjreedy at udel.edu Sun Jul 5 15:09:33 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 05 Jul 2009 15:09:33 -0400 Subject: question of style In-Reply-To: <7xy6r34f3j.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin wrote: > I don't know what a furphy is, but I don't accept that "somethingness" > vs. "nothingness" is the same distinction as truth vs falsehood. True > and False are values in a specific datatype (namely bool), not > abstract qualities of arbitrary data structures. The idea that the > "if" statement selects between "somethingness" and "nothingness" > rather than between True and False is a bogus re-imagining of the > traditional function of an "if" statement and has been an endless > source of bugs in Python code. Look how much confusion it causes here > in the newsgroup all the time. You appear to be confusing a specific interpretation of an abstraction with the abstraction itself. Or perhaps better, you seem to be confusing a specific example of a general process with the general process. A boolean variable is a variable that is in one of two states -- a binary variable -- a variable that carries one bit of information. The two states are the marked state and the unmarked or default state. Which is to say, when we draw a distinction to distinguish two states, we mark one of them to distinguish one from the other. The if statement tests whether an object is in the marked state (or not). Truth and falsity of propositions are one possible interpretation of marked and unmarked, giving us propositional logic. But they are only one of many. So are in and out of a particular class and member or not of a set or subclass or not of a set, giving us class and set logic. So are closed and open, said of gates or switches, or on and off, giving us switching logic. So are non-zero and zero, said of numbers. Or done and not done, said of an algorithmic process. Counts 0 and 1, and their representations '0' and '1', taken in themselves, are as good as any distinct pair as a pair of labels for the two distinct states. They have some computational advantages, including making it easy to count the number of objects in a collection in the marked state (and, given the total number, the number in the unmarked state). They have one disadvantage, though. If I say 'x = 1', do I mean 1 versus 0 or 1 versus all possible ints? Similarly, If 'print(x)' prints 1, does it mean 1 versus 0 or 1 versus all other ints? Recognizing this, Guido decided to subclass them and give them alternate names. He could have chosen 'Marked' and 'Unmarked', or any of several other pairs, but did choose the conventional 'True' and 'False', referring to the common propositional interpretation. However, he specifically disclaimed any intention to restrict 'if' to testing specific logic propositions, as opposed to the general proposition 'object is in the marked state'. Terry Jan Reedy From andreengels at gmail.com Sun Jul 5 15:20:38 2009 From: andreengels at gmail.com (Andre Engels) Date: Sun, 5 Jul 2009 21:20:38 +0200 Subject: Creating alot of class instances? In-Reply-To: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <6faf39c90907051220o5b2adebbnae1136d987209a5a@mail.gmail.com> On 7/5/09, kk wrote: > I am new to Python classes and trying to figure out this particular > issue here. I will need to create instances of a class. But at the > moment I do not know how many instances I will end up having, in every > case it might be different. Most of the documents I read makes this > simpl class-student analogy to explain python classes which is fine. > But in those examples the number and the names of the instances were > known and limited That's no problem. The only limit to the number of instances of a class you can create is your memory - and not even that if you don't need to 'keep' the instances. > I will be querying some data and create class instances based on the > data I gather. But the problem as I mentioned is that I do not know > the names and the number of the end class instances. They will be > based on the content of the data. So how can I create class instances > within a loop and when the loop is done how can I figure out the list > of instances via class membership? I can track the names by > introducing another list but I want to understand the class side of > things. > > The solution might be dead simple but I just cannot figure out at the > moment. > > For example this is what I need in the simplest form > > class myclass(): > def __init__(self,name): > self.name=name > > for count,data in enumerate(some list): > instance_count=myclass() > instance_count.name=data > > print instances Okay, to solve your problem, we add a list containing all the instances: class myclass(): def __init__(self,name): self.name=name instances = [] for count,data in enumerate(some list): instance_count=myclass() instance_count.name=data instances.append(instance_count) print instances ============================================= However, that won't work because myclass has an __init__ with 2 attributes, so you will have to call it using an attribute: class myclass(): def __init__(self,name): self.name=name instances = [] for count,data in enumerate(some list): instance_count=myclass(data) instances.append(instance_count) print instances ============================================= This works, but it can be done better: First we notice that count is not used at all, so why create it? class myclass(): def __init__(self,name): self.name=name instances = [] for data in some list: instance_count=myclass(data) instances.append(instance_count) print instances ============================================= Then, the variable instance_count is created once, then used in the next line. We can do that at once: class myclass(): def __init__(self,name): self.name=name instances = [] for data in some list: instances.append(myclass(data)) print instances ==================== Finally, "print instances" does not give very nice looking information, so I would change this to: class myclass(): def __init__(self,name): self.name=name instances = [] for data in some list: instances.append(myclass(data)) print (instance.name for instance in instances) -- Andr? Engels, andreengels at gmail.com From python.list at tim.thechases.com Sun Jul 5 15:21:35 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 05 Jul 2009 14:21:35 -0500 Subject: Creating alot of class instances? In-Reply-To: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <4A50FD3F.7080105@tim.thechases.com> > The solution might be dead simple but I just cannot figure out at the > moment. > > For example this is what I need in the simplest form > > class myclass(): > def __init__(self,name): > self.name=name > > for count,data in enumerate(some list): > instance_count=myclass() > instance_count.name=data > > print instances Sounds like a use for a list: instances = [] for count, data in enumerate(some_list): # 1) camel-case is preferred for classnames # 2) since your __init__() expects the name # pass it in, instead of setting it later instance = MyClass(data) instances.append(instance) This can be written in a slightly more condensed-yet-readable list-comprehension form as: instances = [MyClass(data) for data in some_list] You then have a list/array of instances you can print: print instances or pick off certain items from it: print instances[42] -tkc From martin at v.loewis.de Sun Jul 5 15:23:50 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 05 Jul 2009 21:23:50 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: <4A50FDC6.1040604@v.loewis.de> > This is a good test for Python implementation bottlenecks. Run > that tokenizer on HTML, and see where the time goes. I looked at it with cProfile, and the top function that comes up for a larger document (52k) is ...validator.HTMLConformanceChecker.__iter__. This method dispatches various validation routines, and it computes the method names from the input over and over again, doing lots of redundant string concatenations. It also capitalizes the element names, even though the spelling in the original document is probably not capitalized (but either upper-case or lower case). In my patch below, I create a dictionary of bound methods, indexed by (syntax) type and name, following the logic of falling back to just type-based validation if no type/name routine exists. However, in order to reduce the number of dictionary lookups, it will also cache type/name pairs (both in the original spelling, and the capitalized spelling), so that subsequent occurrences of the same element will hit the method cache. With this simple optimization, I get a 20% speedup on my test case. In my document, there are no attributes - the same changes should be made to attribute validation routines. I don't think this has anything to do with the case statement. Regards, Martin -------------- next part -------------- A non-text attachment was scrubbed... Name: methodlookup.diff Type: text/x-patch Size: 2837 bytes Desc: not available URL: From maymunbeyin at gmail.com Sun Jul 5 15:54:58 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 12:54:58 -0700 (PDT) Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> Hi Thank you soo much for speedy and in detailed help. Your replies really cleared out most of the cloud for me. I have one last issue to resolve which is something I did not articulate properly, I realize now. The last issue is actually automatically naming the instances. The reason I was using the "instance_count" is for enumerating the actual name of an instance. For example lets say I have class MyMaterials: and my instances might need to look like material_01 material_02 or light_01 light_02 or Mesh_01 Mesh_02 etc I will need to get the base names from the "some list" and create the names accordingly from the list array. Basically I also need to generate the instance names based on the some list. For example some_list[2] might denote a name for the instance. I will study the sampled codes in depth now. Maybe you have answered the naming issue as well, if so please frgove the noise. thanks From vasudevram at gmail.com Sun Jul 5 16:14:31 2009 From: vasudevram at gmail.com (vasudevram) Date: Sun, 5 Jul 2009 13:14:31 -0700 (PDT) Subject: XML(JSON?)-over-HTTP: How to define API? References: <7b5somF21peceU1@mid.uni-berlin.de> Message-ID: <5328d134-5fb0-4a0f-b24b-86deeeaf97aa@i4g2000prm.googlegroups.com> On Jul 3, 1:11?pm, "Diez B. Roggisch" wrote: > Allen Fowler schrieb: > > > > > > > > >> I have an (in-development) python system that needs to shuttle events / requests > >> around over the network to other parts of itself. ? It will also need to > >> cooperate with a .net application running on yet a different machine. > > >> So, naturally I figured some sort of HTTP event / RPC type of would be a good > >> idea? > > >> Are there any modules I should know about, or guidelines I could read, that > >> could aid me in the design of the API? ? ? > > > To clarify: > > > Each message would be <1KB of data total, and consist of some structured object containing strings, numbers, dates, etc. > > > For instance there would be an "add user" request that would contain one or more User objects each having a number of properties like: > > > - Full Name > > - Username > > - Password > > - Email addresses (a variable length array) > > - Street Address line1 > > - Street Address line1 > > - Street Address line1 > > - City > > - State > > - Zip > > - Sign Up Date > > > .... and so on. > > > Since I need to work with other platforms, pickle is out... ?what are the alternatives? ?XML? JSON? > > > How should I formally define each of the valid messages and objects? > > > Thank you, > > Use XMLRPC. Implementations for both languages are available. There is > no need for formal spec - which is a good thing. You just call the > server, and it works. > > Diez I second the suggestion of Diez to use XML-RPC. Very simple to learn and use. Supports structs (as method arguments and method return values) which can consist of other data types bundled together, also supports arrays. Just check whether .NET supports XML-RPC. From ricli85 at gmail.com Sun Jul 5 16:38:50 2009 From: ricli85 at gmail.com (Rickard Lindberg) Date: Sun, 5 Jul 2009 22:38:50 +0200 Subject: Creating alot of class instances? In-Reply-To: <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> Message-ID: <890d8ab70907051338p2eaac716qe96f3a1953d4696c@mail.gmail.com> > Thank you soo much for speedy and in detailed help. Your replies > really cleared out most of the cloud for me. I have one last issue to > resolve which is something I did not articulate properly, I realize > now. The last issue is actually automatically naming the instances. > The reason I was using the "instance_count" is for enumerating the > actual name of an instance. > > For example lets say I have > > class MyMaterials: > > and my instances might need to look like > > material_01 > material_02 > or > light_01 > light_02 > or > Mesh_01 > Mesh_02 etc If you do not know how many instances you are going to create from the beginning, there is no way for you to know which of the instances you mentioned above will get created. So having names for all of the instances will not help you since you will never know what names are "safe" to use. On the other hand, if you have all instances in a list, you can refer to them by index and you know exactly how many of them you have. If you would like to get instances by some name you gave to them, maybe something like this will work: def get_instance(name): for instance in instance_list: if instance.name == name: return instance return None Note that this might very well return None if no instance with that particular name was found. -- Rickard Lindberg From no.email at please.post Sun Jul 5 16:42:41 2009 From: no.email at please.post (kj) Date: Sun, 5 Jul 2009 20:42:41 +0000 (UTC) Subject: memoization module? Message-ID: Is there a memoization module for Python? I'm looking for something like Mark Jason Dominus' handy Memoize module for Perl. TIA! kj From l.mastrodomenico at gmail.com Sun Jul 5 16:50:30 2009 From: l.mastrodomenico at gmail.com (Lino Mastrodomenico) Date: Sun, 5 Jul 2009 22:50:30 +0200 Subject: memoization module? In-Reply-To: References: Message-ID: 2009/7/5 kj : > Is there a memoization module for Python? ?I'm looking for something > like Mark Jason Dominus' handy Memoize module for Perl. Check out the "memoized" class example here: -- Lino Mastrodomenico From python.list at tim.thechases.com Sun Jul 5 17:07:45 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 05 Jul 2009 16:07:45 -0500 Subject: Creating alot of class instances? In-Reply-To: <890d8ab70907051338p2eaac716qe96f3a1953d4696c@mail.gmail.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <0bbb2017-7ded-43cb-88cc-81703a2b02d3@y7g2000yqa.googlegroups.com> <890d8ab70907051338p2eaac716qe96f3a1953d4696c@mail.gmail.com> Message-ID: <4A511621.9050101@tim.thechases.com> >> For example lets say I have >> >> class MyMaterials: >> >> and my instances might need to look like >> >> material_01 >> material_02 >> or >> light_01 >> light_02 >> or >> Mesh_01 >> Mesh_02 etc > > If you do not know how many instances you are going to create from the > beginning, there is no way for you to know which of the instances you mentioned > above will get created. So having names for all of the instances will not help > you since you will never know what names are "safe" to use. > > On the other hand, if you have all instances in a list, you can refer to them > by index and you know exactly how many of them you have. > > If you would like to get instances by some name you gave to them, maybe > something like this will work: > > def get_instance(name): > for instance in instance_list: > if instance.name == name: > return instance > return None Another option might be to use a counter in the class that keeps track of the number of instances: class MyMaterial: instances = 0 def __init__(self, name): self.name = name self.instance = MyMaterial.instances MyMaterial.instances += 1 def __str__(self): return "%s_%02i" % (self.name, self.instance) m = MyMaterial("Brick") print m # "Brick_00" print repr([MyMaterial("Stone") for _ in range(5)]) # "[Stone_01, Stone_02, Stone_03, Stone_04, Stone_05]" It's not thread-safe, but it may do the trick. -tkc From lists at cheimes.de Sun Jul 5 17:42:41 2009 From: lists at cheimes.de (Christian Heimes) Date: Sun, 05 Jul 2009 23:42:41 +0200 Subject: Creating alot of class instances? In-Reply-To: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <4A511E51.9030408@cheimes.de> kk wrote: > I will be querying some data and create class instances based on the > data I gather. But the problem as I mentioned is that I do not know > the names and the number of the end class instances. They will be > based on the content of the data. So how can I create class instances > within a loop and when the loop is done how can I figure out the list > of instances via class membership? I can track the names by > introducing another list but I want to understand the class side of > things. Do you need an exact number or just a rough statistic? In order to estimate the number of instances you can query the reference count of the class. Since every instance usually increases the reference count by one it provides a good overview. Note that lots of other things like imports increase the reference count, too. >>> import sys >>> class Example(object): ... pass ... >>> sys.getrefcount(Example) 5 >>> examples = list(Example() for i in range(10)) >>> examples [<__main__.Example object at 0x7f2e5cd61110>, <__main__.Example object at 0x7f2e5cd61150>, <__main__.Example object at 0x7f2e5cd61190>, <__main__.Example object at 0x7f2e5cd611d0>, <__main__.Example object at 0x7f2e5cd61210>, <__main__.Example object at 0x7f2e5cd61250>, <__main__.Example object at 0x7f2e5cd61390>, <__main__.Example object at 0x7f2e5cd613d0>, <__main__.Example object at 0x7f2e5cd61410>, <__main__.Example object at 0x7f2e5cd61450>] >>> sys.getrefcount(Example) 15 From rhodri at wildebst.demon.co.uk Sun Jul 5 17:47:25 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 05 Jul 2009 22:47:25 +0100 Subject: pep 8 constants In-Reply-To: <4A4E1B2F.2000700@mrabarnett.plus.com> References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> <4A4E1B2F.2000700@mrabarnett.plus.com> Message-ID: On Fri, 03 Jul 2009 15:52:31 +0100, MRAB wrote: > Eric S. Johansson wrote: >> Horace Blegg wrote: >>> I've been kinda following this. I have a cousin who is permanently >>> wheel >>> chair bound and doesn't have perfect control of her hands, but still >>> manages to use a computer and interact with society. However, the >>> idea/thought of disabled programmers was new to me/hadn't ever occurred >>> to me. >>> >>> You say that using your hands is painful, but what about your feet? >>> Wouldn't it be possible to rig up some kind of foot peddle for >>> shift/caps lock? Kinda like the power peddle used with sowing machines, >>> so the hands are free to hold fabric. >>> >>> I don't mean this in a condescending manor, and I apologize if you take >>> it as such. I'm genuinely curious if you think something like this >>> could >>> work. >>> >>> The way I was envisioning it working last night (and I haven't the >>> faintest clue how SR works, nor have I ever used SR) was that you would >>> hit the foot peddle, which would tell the SR program to capitalize the >>> first letter of the next word (a smart shift, basically, so you don't >>> end up doing something like ... WONderland -or- "stocks are up 1,0))% >>> TOday".) >>> >>> Possible? Stupid? >>> >> it's not stupid. >> People have used foot pedals for decades for a variety of controls. I >> don't >> think foot pedals would work for me because when I am dictating, I pace. >> Standing, sitting, I pace. With a cord headset, I'm forced to stay >> within about >> 4 feet of the computer. But what I'm using a Bluetooth headset, I will >> sometimes >> ramble as far as 10 or 15 feet from the computer. It helps if I make >> the font >> larger so I can glance over and see what kind of errors I've gotten. >> I really love a Bluetooth headset with speech recognition. It's so >> liberating. >> Your question about foot pedals makes me think of alternative. would >> it make >> sense to have a handheld keyboard which would be used for >> command-and-control >> functionality or as an adjunct to speech recognition use? It would have >> to be >> designed in such a way that it doesn't aggravate a hand injury which >> may not be >> possible. Anyway, just thinking out loud. >> > You can get giant piano keyboards that you step on, so how about a giant > computer keyboard? "I wrote 5 miles of code before lunch!" :-) You can get/make MIDI organ pedal-boards (a friend of mine has two). From there it's just one small step... :-) -- Rhodri James *-* Wildebeest Herder to the Masses From python.list at tim.thechases.com Sun Jul 5 17:56:07 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 05 Jul 2009 16:56:07 -0500 Subject: pep 8 constants In-Reply-To: References: <49a50517$0$3567$426a74cc@news.free.fr> <4A477991.4050109@harvee.org> <4A484C07.9050800@harvee.org> <4A48A495.7040504@tim.thechases.com> <4A48E307.50804@harvee.org> <4A49E232.6090101@tim.thechases.com> <4A4E135E.5090400@harvee.org> <4A4E1B2F.2000700@mrabarnett.plus.com> Message-ID: <4A512177.8070106@tim.thechases.com> >> You can get giant piano keyboards that you step on, so how about a giant >> computer keyboard? "I wrote 5 miles of code before lunch!" :-) > > You can get/make MIDI organ pedal-boards (a friend of mine has two). From > there it's just one small step... Is that a two-step? a box-step? Count it off...slow, slow, quick, quick. I think I just coded up Conway's Game of Life... -tkc From rhodri at wildebst.demon.co.uk Sun Jul 5 18:03:13 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 05 Jul 2009 23:03:13 +0100 Subject: PEP368 and pixeliterators In-Reply-To: <025db110$0$20657$c3e8da3@news.astraweb.com> References: <025db110$0$20657$c3e8da3@news.astraweb.com> Message-ID: On Fri, 03 Jul 2009 09:21:09 +0100, Steven D'Aprano wrote: > On Thu, 02 Jul 2009 10:32:04 +0200, Joachim Str?mbergson wrote: > >> for pixel in rgb_image: >> # swap red and blue, and set green to 0 pixel.value = pixel.b, 0, >> pixel.r >> >> >> The idea I'm having is that fundamentally the image is made up of a 2D >> array of pixels, not rows of pixels. > > A 2D array implies rows (and columns) of pixels. But not necessarily an internal representation in which those rows or columns are contiguous. An efficient internal storage format might well include margins to make transform edge cases easier, or store the pixel components in separate arrays, or both. I'd imagine that quite frequently, the iterator across all pixels will in fact just be hiding from the programmer the fact that it's really iterating by row and then by pixel. At Python's level of abstraction, that's just fine, but the assumption that an image is made up of a 2D array of pixels is not safe. -- Rhodri James *-* Wildebeest Herder to the Masses From torriem at gmail.com Sun Jul 5 18:27:40 2009 From: torriem at gmail.com (Michael Torrie) Date: Sun, 05 Jul 2009 16:27:40 -0600 Subject: Wrapping comments In-Reply-To: References: Message-ID: <4A5128DC.5060305@gmail.com> Lawrence D'Oliveiro wrote: > I tried using Emacs via SSH from a Mac once. Made me run screaming for the > nearest Windows box > . Interesting rant, but the problem is with the key bindings they chose to use in Terminal.app program. Fortunately in leopard most of the problems are now fixed, or can be configured to work in a non-broken fashion. The rest of us, in the meantime, all switched to iTerm which, although had some performance issues, behaved like we all expected terminals to behave. As far as terminal hell goes, I regularly find that when ssh-ing to remote boxes that backspace doesn't work. Or does in bash (because it's smart enough to play games) but not in vim. Somehow sometimes over ssh the key bindings for backspace get lost of messed up. From http Sun Jul 5 18:51:09 2009 From: http (Paul Rubin) Date: 05 Jul 2009 15:51:09 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <025ff51b$0$20657$c3e8da3@news.astraweb.com> <7xfxdbk61a.fsf@ruckus.brouhaha.com> <02605e46$0$20657$c3e8da3@news.astraweb.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> <0260bf8d$0$20657$c3e8da3@news.astraweb.com> Message-ID: <7x8wj2agma.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > > but I don't accept that "somethingness" > > vs. "nothingness" is the same distinction as truth vs falsehood. > > It's the distinction used by Python since the dawn of time. Python only > grew a bool type a few versions back. That's true, part of the situation we have now is an artifact of that history. > I'm not talking about the constants True and False (nouns), but about > true and false values (adjectives). But, it seems to me, the constants True and False are the only values to which the adjectives "true" and "false" should be applicable to. > > > The idea that the "if" > > statement selects between "somethingness" and "nothingness" rather than > > between True and False is a bogus re-imagining of the traditional > > function of an "if" statement > > There's nothing bogus about it. > > > and has been an endless source of bugs in Python code. > I wonder why these "endless" bugs weren't important enough to be > mentioned in the rationale to PEP 285: Because adding the bool type doesn't really fix those bugs. > describing `if x` as the "correct form" and calling scrapping such a > feature as "crippling the language". Certainly, changing "if" would have broken an immense amount of code and been a completely unworkable approach. We are using a fairly mature language by now; it has a culture and history that carries certain baggage, as one should expect. > > Look how much confusion it causes here in the newsgroup all the time. > The only confusion is that you're causing me. Would you care to link to > some? This current discussion (about bools) came from such confusion just a few posts up in this very thread: From: upwestdon Date: Fri, 3 Jul 2009 23:03:39 -0700 (PDT) How about just: if not (self.higher and self.lower): return self.higher or self.lower That test was designed to treat None as a boolean False, without noticing that numeric 0 is also treated as False and could make the test do the wrong thing. This is an extremely common type of error. > > could see some value to having a generic "is_empty" predicate > We have that already. It's spelled __bool__ or __nonzero__ That's fine, but under the "explicit is better than implicit" principle, it's preferable to call that predicate explicitly: "if bool(x): ..." rather than "if x:". Also, after many years of fixing bugs caused by the mushing together of None and False, it seems to me that we'd have been better off if bool(None) raised an exception rather than returning false. None is supposed to denote a nonexistent value, not a false or empty value. The only valid way to check whether something is None should have been "if x is None". However, it is of course way too late to do this differently. > Iterators are a special case, because in general they can't tell if > they're exhausted until they try to yield a value. Right, it would be nice if they supported a lookahead slot, though that would complicate a lot of __iter__ methods. There's been various kludgy attempts to wrap them in ways that support this, though. From http Sun Jul 5 18:53:34 2009 From: http (Paul Rubin) Date: 05 Jul 2009 15:53:34 -0700 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <0a175b39-f084-4814-92e0-80b6cfce6130@c36g2000yqn.googlegroups.com> Message-ID: <7x4otqagi9.fsf@ruckus.brouhaha.com> Simon Forman writes: > BTW, Paul, kind of a tangent: I reimplemented the same algorithm but > using tuples instead of instances (and empty tuples for "NULL" > values.) I was trying to mess around in the space you seemed to > indicate existed, i.e. a better implementation using other datatypes, > but I didn't have a clear idea what I was doing and, as I said, I > started by simply re-implementing with a different datatype. > > Much to my surprise and delight, I discovered the tuple-based BTree > was /already/ a "persistent data type"! It was both awesome and a bit > of an anti-climax. :] Cool ;-). It also seems to me a bit irregular to require every tree to have a node with optional children, rather than allowing trees to be compleely empty. I think the irregularity complicated the code somewhat. From ldo at geek-central.gen.new_zealand Sun Jul 5 18:54:43 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 10:54:43 +1200 Subject: Wrapping comments References: Message-ID: In message , Michael Torrie wrote: > Lawrence D'Oliveiro wrote: > >> I tried using Emacs via SSH from a Mac once. Made me run screaming for >> the nearest Windows box >> . > > Interesting rant, but the problem is with the key bindings they chose to > use in Terminal.app program. Except for control-space, which was defined systemwide to bring up Spotlight. So you can't blame the Terminal app for that. The brain damage was more widespread than just one program. From contact at xavierho.com Sun Jul 5 20:20:39 2009 From: contact at xavierho.com (Xavier Ho) Date: Mon, 6 Jul 2009 10:20:39 +1000 Subject: Why is my code faster with append() in a loop than with a large list? Message-ID: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> (Here's a short version of the long version below if you don't want to read:) Why is version B of the code faster than version A? (Only three lines different) Version A: http://pastebin.com/f14561243 Version B: http://pastebin.com/f1f657afc ------------------------------------------------ I was doing the problems on Project Euler for practice with Python last night. Problem 12 was to find the value of the first triangular number that has over 500 divisors. ========================================================================================= The sequence of triangle numbers is generated by adding the natural numbers. So the 7[image: ^(]th[image: )] triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: * 1*: 1 * 3*: 1,3 * 6*: 1,2,3,6 *10*: 1,2,5,10 *15*: 1,3,5,15 *21*: 1,3,7,21 *28*: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? ========================================================================================= My initial code was to loop through from 1 to half the number and see which were divisors, and as I find them I store them in a list. That would have taken days. My second try was factorising the number each time, and count the divisors using the powers of each factor, plus 1, and multiply together. The code is here (Version A): http://pastebin.com/f14561243 This worked, but it took overnight to compute. Before I went to bed a friend of mine caught me online, and apparently left me a working version under 8 seconds with only 3 line difference. The code is here (Version B): http://pastebin.com/f1f657afc That was amazing. But I have no idea why his edit makes it so much faster. I did a test to see whether if append() was faster (which I doubted) than defining a list with a large size to begin with, and I was right: http://pastebin.com/f4b46d0db Which shows that appending is 40x slower, and was expected. But I still can't puzzle out why his use of appending in Version B was so much faster than mine. Any insights would be welcome. I'm going on a family trip, though, so my replies may delay. Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From fetchinson at googlemail.com Sun Jul 5 20:23:49 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Sun, 5 Jul 2009 17:23:49 -0700 Subject: memoization module? In-Reply-To: References: Message-ID: > Is there a memoization module for Python? I'm looking for something > like Mark Jason Dominus' handy Memoize module for Perl. The Python Cookbook has several examples: http://www.google.com/search?q=python+memoize&sitesearch=code.activestate.com HTH, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From ldo at geek-central.gen.new_zealand Sun Jul 5 20:30:30 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 12:30:30 +1200 Subject: PEP 376 References: <73587ae3-4f4f-4243-a8af-cf4a6bfb598b@k26g2000vbp.googlegroups.com> <4A4C685A.8020300@Strombergson.com> <94bdd2610907020455x35239f45jc35290f5c21d3229@mail.gmail.com> <1f63m.2229$ze1.1410@news-server.bigpond.net.au> Message-ID: In message , Charles Yeomans wrote: > On the contrary, MD5 was intended to be a cryptographic hash function, > not a checksum. Just like MD4 and MD2 before it. They have long since been considered worthless, and now MD5 has joined them. From Scott.Daniels at Acm.Org Sun Jul 5 20:30:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 05 Jul 2009 17:30:58 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <4A4FDCAF.8010209@Acm.Org> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: Scott David Daniels wrote: > ... Here's a heuristic replacement for my previous frequency code: > I've tried to mark where you could fudge numbers if the run time > is at all close. Boy, I cannot let go. I did a bit of a test checking for cost to calculated number of discovered samples, and found after: import timeit import numpy original = numpy.random.random(0, 100, (1000, 1000)).astype(int) data = original.flatten() data.sort() part = data[::100] t = timeit.Timer('sum(part[:-1]==part[1:])', 'from __main__ import part') v = timeit.Timer('len(part[part[:-1]==part[1:]])', 'from __main__ import part') I got: >>> t.repeat(3, 10) [0.58319842326318394, 0.57617574300638807, 0.57831819407238072] >>> v.repeat(3, 1000) [0.93933027801040225, 0.93704535073584339, 0.94096260837613954] So, len(part[mask]) is almost 50X faster! I checked: >>> sum(part[:-1]==part[1:]) 9393 >>> len(part[part[:-1]==part[1:]]) 9393 That's an awful lot of matches, so I with high selectivity: data = original.flatten() # no sorting, so runs missing part = data[::100] >>> t.repeat(3, 10) [0.58641335700485797, 0.58458854407490435, 0.58872594142576418] >>> v.repeat(3, 1000) [0.27352554584422251, 0.27375686015921019, 0.27433291102624935] about 200X faster >>> len(part[part[:-1]==part[1:]]) 39 >>> sum(part[:-1]==part[1:]) 39 So my new version of this (compressed) code: > ... > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) # count identified matches > while candidates > N * 10: # 10 -- heuristic > stride *= 2 # # heuristic increase > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > while candidates < N * 3: # heuristic slop for long runs > stride //= 2 # heuristic decrease > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > former = None > past = 0 > for value in sampled[matches]: > ... is: ... sampled = data[::stride] candidates = sampled[sampled[:-1] == sampled[1:]] while len(candidates) > N * 10: # 10 -- heuristic stride *= 2 # # heuristic increase sampled = data[::stride] candidates = sampled[sampled[:-1] == sampled[1:]] while len(candidates) < N * 3: # heuristic slop for long runs stride //= 2 # heuristic decrease sampled = data[::stride] candidates = sampled[sampled[:-1] == sampled[1:]] former = None past = 0 for value in candidates: ... This change is important, for we try several strides before settling on a choice, meaning the optimization can be valuable. This also means we could be pickier at choosing strides (try more values), since checking is cheaper than before. Summary: when dealing with numpy, (or any bulk <-> individual values transitions), try several ways that you think are equivalent and _measure_. In the OODB work I did we called this "impedance mismatch," and it is likely some boundary transitions are _much_ faster than others. The sum case is one of them; I am getting numpy booleans back, rather than numpy booleans, so conversions aren't going fastpath. --Scott David Daniels Scott.Daniels at Acm.Org From timr at probo.com Sun Jul 5 20:36:11 2009 From: timr at probo.com (Tim Roberts) Date: Sun, 05 Jul 2009 17:36:11 -0700 Subject: Python and webcam capture delay? References: Message-ID: "jack catcher (nick)" wrote: > >I'm thinking of using Python for capturing and showing live webcam >stream simultaneously between two computers via local area network. >Operating system is Windows. I'm going to begin with VideoCapture >extension, no ideas about other implementation yet. Do you have any >suggestions on how short delay I should hope to achieve in showing the >video? This would be part of a psychological experiment, so I would need >to deliver the video stream with a reasonable delay (say, below 100ms). You need to do the math on this. Remember that a full 640x480 RGB stream at 30 frames per second runs 28 megabytes per second. That's more than twice what a 100 megabit network can pump. You can probably use Python to oversee this, but you might want to consider using lower-level code to control the actual hardware. If you are targeting Windows, for example, you could write a DirectShow graph to pump into a renderer that transmits out to a network, then another graph to receive from the network and display it. You can manage the network latency by adding a delays in the local graph. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From david at pythontoo.com Sun Jul 5 20:48:39 2009 From: david at pythontoo.com (David) Date: Sun, 05 Jul 2009 20:48:39 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A5149E7.6030809@pythontoo.com> Xavier Ho wrote: > (Here's a short version of the long version below if you don't want to > read:) > > Why is version B of the code faster than version A? (Only three lines > different) > > Version A: http://pastebin.com/f14561243 > Version B: http://pastebin.com/f1f657afc I don't know but here is the diff for someone that may; 1c1,2 < # This one only took 8 seconds on my machine. Wow? --- > > # This one took hours to compute, overnight. 28c29 < powers = [0, 0] --- > powers = [0] * (num + 1) 32c33 < powers[factor-1] += 1 --- > powers[factor] += 1 35d35 < powers.append(0) 55c55 < n += 1 \ No newline at end of file --- > n += 1 -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From python at mrabarnett.plus.com Sun Jul 5 20:54:39 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 01:54:39 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A514B4F.6010408@mrabarnett.plus.com> Xavier Ho wrote: > (Here's a short version of the long version below if you don't want to > read:) > > Why is version B of the code faster than version A? (Only three lines > different) > > Version A: http://pastebin.com/f14561243 > Version B: http://pastebin.com/f1f657afc > > ------------------------------------------------ > > I was doing the problems on Project Euler for practice with Python last > night. Problem 12 was to find the value of the first triangular number > that has over 500 divisors. > ========================================================================================= > > The sequence of triangle numbers is generated by adding the natural > numbers. So the 7^(^th ) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 > + 7 = 28. The first ten terms would be: > > 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... > > Let us list the factors of the first seven triangle numbers: > > * 1*: 1 > * 3*: 1,3 > * 6*: 1,2,3,6 > *10*: 1,2,5,10 > *15*: 1,3,5,15 > *21*: 1,3,7,21 > *28*: 1,2,4,7,14,28 > > We can see that 28 is the first triangle number to have over five divisors. > > What is the value of the first triangle number to have over five hundred > divisors? > > ========================================================================================= > > My initial code was to loop through from 1 to half the number and see > which were divisors, and as I find them I store them in a list. That > would have taken days. > > My second try was factorising the number each time, and count the > divisors using the powers of each factor, plus 1, and multiply together. > The code is here (Version A): http://pastebin.com/f14561243 > > This worked, but it took overnight to compute. Before I went to bed a > friend of mine caught me online, and apparently left me a working > version under 8 seconds with only 3 line difference. > The code is here (Version B): http://pastebin.com/f1f657afc > > That was amazing. But I have no idea why his edit makes it so much > faster. I did a test to see whether if append() was faster (which I > doubted) than defining a list with a large size to begin with, and I was > right: > http://pastebin.com/f4b46d0db > Which shows that appending is 40x slower, and was expected. But I still > can't puzzle out why his use of appending in Version B was so much > faster than mine. > > Any insights would be welcome. I'm going on a family trip, though, so my > replies may delay. > In your version you're creating a list of (num + 1) elements, but in the other version the list is only as long as the largest factor. For example, for num=28, your version creates a list 29 elements long, but the other version creates one only 7 elements long. Also, 'the time needed to append an item to the list is "amortized constant"' (quoted from http://effbot.org/zone/python-list.htm). This means that your little speed test isn't representation of what's actually happening. From notvalid2 at sbcglobal.net Sun Jul 5 21:48:46 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Sun, 05 Jul 2009 18:48:46 -0700 Subject: Adding the Copy Property to a Simple Histogram Message-ID: The code below produces a text window 60hx20w with a scroll bar. The contents are something of a histogram of values from 0 to 255. If one tries to copy the contents, Windows doesn't allow it. What needs to be done to allow a copy and paste? def ShowHistogram(self): if not self.current_image: return if self.histogram: self.histogram.destroy() t = Toplevel( self.master ) t.title("Histogram") t.bind( '', self.DestroyHistogram ) text = Text( t, height=60, width=20 ) scroll = Scrollbar(t, command=text.yview) text.configure(yscrollcommand=scroll.set) text.pack(side=LEFT, fill='both', expand=1) scroll.pack(side=RIGHT, fill=Y) self.histogram = t hist = self.current_image.histogram() for i in range(len(hist)): msg = "%5d %6d\n" % (i,hist[i]) text.insert( END, msg ) -- W. eWatson (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Web Page: From tanner989 at hotmail.com Sun Jul 5 22:06:30 2009 From: tanner989 at hotmail.com (tanner barnes) Date: Sun, 5 Jul 2009 21:06:30 -0500 Subject: Help with Sockets. Message-ID: I am writing a program and in one section there is going to be a lobby with (for testing purposes) about 4 people in it. in the lobby there are two txtctrl's the first for entering your message and the second for displaying the message you and the other people in the lobby type. i am trying to figure out how to get all the text entrys from the users and display them in the second one. For clarification and example of what i want would be like yahoo or windows live messanger but for more than 2 people. Python version: 2.6 GUI toolkit: WxPython _________________________________________________________________ Hotmail? has ever-growing storage! Don?t worry about storage limits. http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage_062009 -------------- next part -------------- An HTML attachment was scrubbed... URL: From maymunbeyin at gmail.com Sun Jul 5 22:27:25 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 19:27:25 -0700 (PDT) Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> Message-ID: <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> Hi Thank you so much for wonderful tips and suggestions. I also found a solution to dynamic naming of the instances(I think). It does not sound like a very secure method but since my application will be just processing data one way I think it might be alright. I will compare to the list and dictionary methods. globals()["Some_Instance_Name"] From ldo at geek-central.gen.new_zealand Sun Jul 5 22:32:46 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 14:32:46 +1200 Subject: A Bug By Any Other Name ... Message-ID: I wonder how many people have been tripped up by the fact that ++n and --n fail silently for numeric-valued n. From clp2 at rebertia.com Sun Jul 5 22:45:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 5 Jul 2009 19:45:39 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: <50697b2c0907051945x67f97094s370449fa6d05b449@mail.gmail.com> On Sun, Jul 5, 2009 at 7:32 PM, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ? ?++n > > and > > ? ?--n > > fail silently for numeric-valued n. Given that C-style for-loops are relatively infrequent in Python and are usually written using range() when they are needed, it's probably not that prevalent a problem. I suppose the lexer could be changed to make ++ and -- illegal... Cheers, Chris -- http://blog.rebertia.com From sajmikins at gmail.com Sun Jul 5 23:11:51 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sun, 5 Jul 2009 20:11:51 -0700 (PDT) Subject: Adding the Copy Property to a Simple Histogram References: Message-ID: On Jul 5, 9:48?pm, "W. eWatson" wrote: > The code below produces a text window 60hx20w with a scroll bar. The > contents are something of a histogram of values from 0 to 255. If one tries > to copy the contents, Windows doesn't allow it. What needs to be done to > allow a copy and paste? > > ? ? ?def ShowHistogram(self): > ? ? ? ? ?if not self.current_image: > ? ? ? ? ? ? ?return > > ? ? ? ? ?if self.histogram: > ? ? ? ? ? ? ?self.histogram.destroy() > > ? ? ? ? ?t = Toplevel( self.master ) > ? ? ? ? ?t.title("Histogram") > ? ? ? ? ?t.bind( '', self.DestroyHistogram ) > ? ? ? ? ?text = Text( t, height=60, width=20 ) > ? ? ? ? ?scroll = Scrollbar(t, command=text.yview) > ? ? ? ? ?text.configure(yscrollcommand=scroll.set) > ? ? ? ? ?text.pack(side=LEFT, fill='both', expand=1) > ? ? ? ? ?scroll.pack(side=RIGHT, fill=Y) > ? ? ? ? ?self.histogram = t > > ? ? ? ? ?hist = self.current_image.histogram() > ? ? ? ? ?for i in range(len(hist)): > ? ? ? ? ? ? ?msg = "%5d %6d\n" % (i,hist[i]) > ? ? ? ? ? ? ?text.insert( END, msg ) > -- > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? W. eWatson > > ? ? ? ? ? ? ? (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) > ? ? ? ? ? ? ? ?Obz Site: ?39? 15' 7" N, 121? 2' 32" W, 2700 feet > > ? ? ? ? ? ? ? ? ? ? ?Web Page: Do you mean that the Text widget doesn't let you copy-and-paste copy its contents using selection and ? That shouldn't have anything to do with the contents of the Text widget. From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 23:28:43 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 03:28:43 GMT Subject: A Bug By Any Other Name ... References: Message-ID: <006d4a50$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ++n > > and > > --n > > fail silently for numeric-valued n. What do you mean, "fail silently"? They do exactly what you should expect: >>> ++5 # positive of a positive number is positive 5 >>> --5 # negative of a negative number is positive 5 >>> -+5 # negative of a positive number is negative -5 So does the bitwise-not unary operator: >>> ~~5 5 I'm not sure what "bug" you're seeing. Perhaps it's your expectations that are buggy, not Python. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 23:28:55 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 03:28:55 GMT Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: <006d4a5c$0$9711$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 17:30:58 -0700, Scott David Daniels wrote: > Summary: when dealing with numpy, (or any bulk <-> individual values > transitions), try several ways that you think are equivalent and > _measure_. This advice is *much* more general than numpy -- it applies to any optimization exercise. People's intuitions about what's fast and what's slow are often very wrong. -- Steven From notvalid2 at sbcglobal.net Sun Jul 5 23:33:48 2009 From: notvalid2 at sbcglobal.net (W. eWatson) Date: Sun, 05 Jul 2009 20:33:48 -0700 Subject: Adding the Copy Property to a Simple Histogram In-Reply-To: References: Message-ID: Simon Forman wrote: > On Jul 5, 9:48 pm, "W. eWatson" wrote: >> The code below produces a text window 60hx20w with a scroll bar. The >> contents are something of a histogram of values from 0 to 255. If one tries >> to copy the contents, Windows doesn't allow it. What needs to be done to >> allow a copy and paste? >> >> def ShowHistogram(self): >> if not self.current_image: >> return >> >> if self.histogram: >> self.histogram.destroy() >> >> t = Toplevel( self.master ) >> t.title("Histogram") >> t.bind( '', self.DestroyHistogram ) >> text = Text( t, height=60, width=20 ) >> scroll = Scrollbar(t, command=text.yview) >> text.configure(yscrollcommand=scroll.set) >> text.pack(side=LEFT, fill='both', expand=1) >> scroll.pack(side=RIGHT, fill=Y) >> self.histogram = t >> >> hist = self.current_image.histogram() >> for i in range(len(hist)): >> msg = "%5d %6d\n" % (i,hist[i]) >> text.insert( END, msg ) >> -- >> W. eWatson >> >> (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) >> Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet >> >> Web Page: > > Do you mean that the Text widget doesn't let you copy-and-paste copy > its contents using selection and ? That shouldn't have > anything to do with the contents of the Text widget. Whoops, I missed it. I'm not quite sure what I did to make it seem like it doesn't copy, but it does. I fooled myself. All is well. -- W. eWatson (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Web Page: From steve at REMOVE-THIS-cybersource.com.au Sun Jul 5 23:46:41 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 03:46:41 GMT Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> Message-ID: <006d4e86$0$9711$c3e8da3@news.astraweb.com> On Sun, 05 Jul 2009 19:27:25 -0700, kk wrote: > Hi > > Thank you so much for wonderful tips and suggestions. > > I also found a solution to dynamic naming of the instances(I think). It > does not sound like a very secure method but since my application will > be just processing data one way I think it might be alright. I will > compare to the list and dictionary methods. > > globals()["Some_Instance_Name"] You're fighting the computer instead of working with it. That's the Wrong Way to solve the problem -- you're doing more work than needed, for little or no benefit. My bet is, you have code that looks something like this: for i in range(N): # N comes from somewhere else # Create a new variable globals()["Some_Instance_Name%s" % i] = instance() # Do something with the variables for i in range(N): # Look up the variable x = globals()["Some_Instance_Name%s" % i] process(x) Am I close? That's the Wrong Way to do it -- you're using a screwdriver to hammer a nail. The right way to work with an unknown number of data elements is to put them in a list, and process each element in the list, not to try giving them all unique names. The only reason for using named variables is so you can use the name in source code: my_value = Some_Instance87 + Some_Instance126 But you can't do that, because you don't know how many instances there are, you don't know whether to write Some_Instance87 or Some_Instance125 or Some_Instance19. So instead, do something like this: instances = [] for i in range(N): # Create a new instance and store it for later instances.append( instance() ) # Later on: for x in instances(): process(x) -- Steven From dns4 at cornell.edu Sun Jul 5 23:53:56 2009 From: dns4 at cornell.edu (David Smith) Date: Sun, 05 Jul 2009 23:53:56 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> <7x4otsux7f.fsf@ruckus.brouhaha.com> Message-ID: kj wrote: > In <7x4otsux7f.fsf at ruckus.brouhaha.com> Paul Rubin writes: > >> kj writes: >>> sense = cmp(func(hi), func(lo)) >>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" > >> bisection search usually just requires the function to be continuous >> and to have its value cross the target somewhere between the endpoints, >> not be monotonic. > > Try the algorithm I posted with lo = -pi/4, hi = 2*pi, func = cos, > target = -1, and see what you get... > >>> I regard the very special case of func(hi)==func(lo)==target as >>> pathological (analogous to the fact that a stopped watch is "exactly >>> right" twice a day), and not one I care to support. > >> I do think you should support that case, under the "do 'nothing' >> gracefully" principle. > > You keep missing the point that this is an *internal* *helper* > *convenience* function, meant to abstract away common logic from > a handful of places and thus eliminate some code repetition within > a module. It is *not* a library function intended to be called > from elsewhere. So talk of "supporting" anything is besides the > point. Any internal use of this function that applies it to a > non-strictly-monotonic function is, by assumption, an error. > > kj First, let me say *I got the point*. I use asserts, but only in unit testing where I want to test the result of some action for correctness. In the course of programming product code, I personally don't think they should ever be used exactly for the reasons everyone else is pointing out. They can be disabled with the -O option and that changes the program's behavior in ways that could break in production. If you insist on teaching the assert statement, teach it in the context of writing unit testing code. Its an extremely valuable skill. --David From casevh at gmail.com Mon Jul 6 01:04:36 2009 From: casevh at gmail.com (casevh) Date: Sun, 5 Jul 2009 22:04:36 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 Message-ID: An alpha release of GMPY that supports Python 2 and 3 is available. GMPY is a wrapper for the GMP multiple-precision arithmetic library. The MPIR multiple-precision arithmetic library is also supported. GMPY is available for download from http://code.google.com/p/gmpy/ Support for Python 3 required many changes to the logic used to convert between different numerical types. The result type of some combinations has changed. For example, 'mpz' + 'float' now returns an 'mpf' instead of a 'float'. See the file "changes.txt" for more information. In addition to support for Python 3, there are several other changes and bug fixes: - Bug fixes in mpz.binary() and mpq.binary(). - Unicode strings are accepted as input on Python 2. (Known bug: works for mpz, fails for mpq and mpf) - The overhead for calling GMPY routines has been reduced. If one operand in a small integer, it is not converted to mpz. - 'mpf' and 'mpq' now support % and divmod. Comments on provided binaries The 32-bit Windows installers were compiled using MPIR 1.2.1 and will automatically recognize the CPU type and use code optimized for that CPU. Please test with your applications and report any issues found! casevh From gagsl-py2 at yahoo.com.ar Mon Jul 6 01:19:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 02:19:51 -0300 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano escribi?: > On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: > >> I wonder how many people have been tripped up by the fact that >> >> ++n >> >> and >> >> --n >> >> fail silently for numeric-valued n. > > What do you mean, "fail silently"? They do exactly what you should > expect: >>>> ++5 # positive of a positive number is positive > > I'm not sure what "bug" you're seeing. Perhaps it's your expectations > that are buggy, not Python. Well, those expectations are taken seriously when new features are introduced into the language - and sometimes the feature is dismissed just because it would be confusing for some. If a += 1 works, expecting ++a to have the same meaning is very reasonable (for those coming from languages with a ++ operator, like C or Java) - more when ++a is a perfectly valid expression. If this issue isn't listed under the various "Python gotchas" articles, it should... -- Gabriel Genellina From banibrata.dutta at gmail.com Mon Jul 6 01:37:19 2009 From: banibrata.dutta at gmail.com (Banibrata Dutta) Date: Mon, 6 Jul 2009 11:07:19 +0530 Subject: mail In-Reply-To: <22154.210.212.36.65.1246724472.squirrel@www.iisermohali.ac.in> References: <22154.210.212.36.65.1246724472.squirrel@www.iisermohali.ac.in> Message-ID: <3de8e1f70907052237m442e563bj4e040eec12d63f0d@mail.gmail.com> On Sat, Jul 4, 2009 at 9:51 PM, wrote: > Hi, > > I want to know that whether using python programming is it possible to > extract chemical shift information about some amino acids of some protein > from BMRB(BioMagResBank) or Ref-DB(referenced databank) or not. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > Without any real knowledge of the problem domain, but with keyword level google search, here's one one finds -- http://www.ccpn.ac.uk/api-documentation/ccpnmr/ccpnmr2.0/python/doc/api.html http://biopython.org/wiki/Main_Page http://chempython.org/ http://www.sschwarzer.net/ Also as Grant says, if you can create a software program in any language, you can potentially do it with Python as well. As Python is a "batteries included" language, chances are, the included-batteries would make your life easier. Of course mileage may vary. If you find existing modules that do much of what you want, you have a very good starting point. -- regards, Banibrata http://www.linkedin.com/in/bdutta -------------- next part -------------- An HTML attachment was scrubbed... URL: From maymunbeyin at gmail.com Mon Jul 6 01:50:36 2009 From: maymunbeyin at gmail.com (kk) Date: Sun, 5 Jul 2009 22:50:36 -0700 (PDT) Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven, Before your post I was contemplating about the merits of using the globals(). After reading your post I am totally convinced that your suggestion that was also suggested by previous posters is the way to go. At first I thought it would be limiting to not to have the instance names properly setup, but now I understand it better. thank you all again. From gagsl-py2 at yahoo.com.ar Mon Jul 6 01:58:59 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 02:58:59 -0300 Subject: Does cProfile include IO wait time? References: Message-ID: En Sat, 04 Jul 2009 21:03:38 -0300, Matthew Wilson escribi?: > I expected to see a bunch of my IO file-reading code in there, but I > don't. So > this makes me think that the profiler uses CPU time, not > clock-on-the-wall time. > I'm not an expert on python profiling, and the docs seem sparse. Can I > rule out IO as the bottleneck here? How do I see the IO consequences? I don't know either - but it's easy to check. Write a program that just reads a lot from /dev/zero, and look at its profile. You should be able to tell whether I/O time is included or not. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 6 02:10:25 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 03:10:25 -0300 Subject: Help with Sockets. References: Message-ID: En Sun, 05 Jul 2009 23:06:30 -0300, tanner barnes escribi?: > I am writing a program and in one section there is going to be a lobby > with (for testing purposes) about 4 people in it. in the lobby there are > two txtctrl's the first for entering your message and the second for > displaying the message you and the other people in the lobby type. i am > trying to figure out how to get all the text entrys from the users and > display them in the second one. For clarification and example of what i > want would be like yahoo or windows live messanger but for more than 2 > people. Like a chat room, IRC? It's easy to do using a client-server architecture. Make all the clients connect to a central server. Any time someone writes some text, the client sends it to the server (but does not display it). The server just receives text from any client, and sends the received messages to all connected clients (including the one that sent it originally). Any book on socket programming will help; there are a few specific for Python. You may start with the "echo" example in the Python documentation. Make the networking part work first, then add the wx GUI if you want. -- Gabriel Genellina From nomail at thank.you Mon Jul 6 02:10:38 2009 From: nomail at thank.you (jack catcher (nick)) Date: Mon, 06 Jul 2009 09:10:38 +0300 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: Tim Roberts kirjoitti: > "jack catcher (nick)" wrote: >> I'm thinking of using Python for capturing and showing live webcam >> stream simultaneously between two computers via local area network. >> Operating system is Windows. I'm going to begin with VideoCapture >> extension, no ideas about other implementation yet. Do you have any >> suggestions on how short delay I should hope to achieve in showing the >> video? This would be part of a psychological experiment, so I would need >> to deliver the video stream with a reasonable delay (say, below 100ms). > > You need to do the math on this. Remember that a full 640x480 RGB stream > at 30 frames per second runs 28 megabytes per second. That's more than > twice what a 100 megabit network can pump. > > You can probably use Python to oversee this, but you might want to consider > using lower-level code to control the actual hardware. If you are > targeting Windows, for example, you could write a DirectShow graph to pump > into a renderer that transmits out to a network, then another graph to > receive from the network and display it. > > You can manage the network latency by adding a delays in the local graph. Thanks Tim, you're correct about the math. What is your main point about DirectShow: that it is generally faster and more reliable than doing the job high-level, or that one could use coding/decoding in DirectShow to speed up the transmission? I think the latter would be a great idea if the latency were tolerable. On the other hand, I'd like to keep things simple and do all the programming in Python. I've got no experience with DirectShow, but I guess the filters need to be programmed in C++ and called from Python? Another option might be to use resolution 320x240 at 15fps. From gherron at islandtraining.com Mon Jul 6 02:33:36 2009 From: gherron at islandtraining.com (Gary Herron) Date: Sun, 05 Jul 2009 23:33:36 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A519AC0.1050905@islandtraining.com> Gabriel Genellina wrote: > En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano > escribi?: >> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >> >>> I wonder how many people have been tripped up by the fact that >>> >>> ++n >>> >>> and >>> >>> --n >>> >>> fail silently for numeric-valued n. >> >> What do you mean, "fail silently"? They do exactly what you should >> expect: >>>>> ++5 # positive of a positive number is positive >> >> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >> that are buggy, not Python. > > Well, those expectations are taken seriously when new features are > introduced into the language - and sometimes the feature is dismissed > just because it would be confusing for some. > If a += 1 works, expecting ++a to have the same meaning is very > reasonable (for those coming from languages with a ++ operator, like C > or Java) - more when ++a is a perfectly valid expression. > If this issue isn't listed under the various "Python gotchas" > articles, it should... Well sure, it's not unreasonable to expect ++n and --n to behave as in other languages, and since they don't, perhaps they should be listed as a "Python gotcha". But even so, it's quite arrogant of the OP to flaunt his ignorance of the language by claiming this is a bug and a failure. It shouldn't have been all that hard for him to figure out what was really happening. Gary Herron From __peter__ at web.de Mon Jul 6 02:33:53 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 06 Jul 2009 08:33:53 +0200 Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: Scott David Daniels wrote: > Scott David Daniels wrote: > t = timeit.Timer('sum(part[:-1]==part[1:])', > 'from __main__ import part') What happens if you calculate the sum in numpy? Try t = timeit.Timer('(part[:-1]==part[1:]).sum()', 'from __main__ import part') Peter From gagsl-py2 at yahoo.com.ar Mon Jul 6 02:46:33 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 03:46:33 -0300 Subject: Method to separate unit-test methods and data? References: Message-ID: En Sun, 05 Jul 2009 15:48:06 -0300, Nick Daly escribi?: > [test_Midpoint_mid] > none_values = ((-1, None), > ? ? ? ? ? ? ? (None, -12.8)) > > What I haven't yet figured out how to do though, is properly override > the default class member values with values from the config file. ?The > config file's data is loaded as a string instead of as a list, as I'd > want. ?This causes all the tests to fail, as while none_values needs > to be interpreted as a list, it is instead understood as: > > " ((-1, None),\n ? ? ? ? ? ? ? (None, -12.8))" > > Does anyone have any solutions for these problems? ? You may use a .py file to configure it, instead of your .cfg > First, is there a > known and simple way to separate unit-test data and methods into > separate files? ? Just write them in separate files? I didn't quite understand the question... > Secondly, if not, is there a simple way to convert > strings into other base types, like lists, dictionaries, and so forth? eval(), but I'd just use a .py file People usually warns against using eval on arbitrary strings, or on user-supplied data, but in this case it is not worse than importing/executing the module. > Or, am I going to have to write my own list-parsing methods? ?Would > pickling help? ?I haven't yet had a chance to look into if or how that > would work... ?If there's anything else I can clarify about this > request, feel free to let me know. Another alternative would be a .csv file; I use them when the test data comes from other parties, like a manufacturer's data sheet. I've never used a pickle to store test data - how do you generate the pickle contents in the first place? Usually I'd just use *that* code directly, but I think pickling the resulting objects would be OK is the process to regenerate them takes a lot of time. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 6 02:53:57 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 03:53:57 -0300 Subject: multiprocessing and freezing on Windows References: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Message-ID: En Sat, 04 Jul 2009 22:15:43 -0300, SK escribi?: > To add a bit more information, I found that I needed to patch > get_command_line in multiprocessing/forking.py [...] > > Is packaging with multiprocessing supposed to be this hard? If so, > some documentation is needed. Shouldn't be so hard, I presume. You may want to post your comments and upload the patch to http://bugs.python.org -- Gabriel Genellina From martin at librador.com Mon Jul 6 03:44:33 2009 From: martin at librador.com (Martin Vilcans) Date: Mon, 6 Jul 2009 09:44:33 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. ?I'd love your opinion about > it. In general, code clarity is more important than reusability. Unfortunately, many novice programmers have the opposite impression. I have seen too much convoluted code written by beginners who try to make the code generic. Writing simple, clear, to-the-point code is hard enough as it is, even when not aiming at making it reusable. If in the future you see an opportunity to reuse the code, then and only then is the time to make it generic. YAGNI is a wonderful principle. -- martin at librador.com http://www.librador.com From gagsl-py2 at yahoo.com.ar Mon Jul 6 03:49:28 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 04:49:28 -0300 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: En Mon, 06 Jul 2009 03:33:36 -0300, Gary Herron escribi?: > Gabriel Genellina wrote: >> En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano >> escribi?: >>> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >>> >>>> I wonder how many people have been tripped up by the fact that >>>> >>>> ++n >>>> >>>> and >>>> >>>> --n >>>> >>>> fail silently for numeric-valued n. >>> >>> What do you mean, "fail silently"? They do exactly what you should >>> expect: >>>>>> ++5 # positive of a positive number is positive >>> >>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>> that are buggy, not Python. >> >> Well, those expectations are taken seriously when new features are >> introduced into the language - and sometimes the feature is dismissed >> just because it would be confusing for some. >> If a += 1 works, expecting ++a to have the same meaning is very >> reasonable (for those coming from languages with a ++ operator, like C >> or Java) - more when ++a is a perfectly valid expression. >> If this issue isn't listed under the various "Python gotchas" articles, >> it should... > > Well sure, it's not unreasonable to expect ++n and --n to behave as in > other languages, and since they don't, perhaps they should be listed as > a "Python gotcha". But even so, it's quite arrogant of the OP to flaunt > his ignorance of the language by claiming this is a bug and a failure. > It shouldn't have been all that hard for him to figure out what was > really happening. That depends on what you call a "bug". In his classical book "The art of software testing", Myers says that a program has a bug when it doesn't perform as the user expects reasonably it to do (not an exact quote, I don't have the book at hand). That's a lot broader than developers like to accept. In this case, a note in the documentation warning about the potential confusion would be fine. -- Gabriel Genellina From andreengels at gmail.com Mon Jul 6 03:51:04 2009 From: andreengels at gmail.com (Andre Engels) Date: Mon, 6 Jul 2009 09:51:04 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: <6faf39c90907060051n65089c88od45c75c80f3a7ccf@mail.gmail.com> On Mon, Jul 6, 2009 at 9:44 AM, Martin Vilcans wrote: > On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: >> I'm will be teaching a programming class to novices, and I've run >> into a clear conflict between two of the principles I'd like to >> teach: code clarity vs. code reuse. ?I'd love your opinion about >> it. > > In general, code clarity is more important than reusability. > Unfortunately, many novice programmers have the opposite impression. I > have seen too much convoluted code written by beginners who try to > make the code generic. Writing simple, clear, to-the-point code is > hard enough as it is, even when not aiming at making it reusable. > > If in the future you see an opportunity to reuse the code, then and > only then is the time to make it generic. Not just that, when you actually get to that point, making simple and clear code generic is often easier than making complicated-and-supposedly-generic code that little bit more generic that you need. -- Andr? Engels, andreengels at gmail.com From mail at timgolden.me.uk Mon Jul 6 03:56:20 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 06 Jul 2009 08:56:20 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <4A51AE24.9080608@timgolden.me.uk> Gabriel Genellina wrote: [... re confusion over ++n etc ...] > In this case, a note in the documentation warning about the potential > confusion would be fine. The difficulty here is knowing where to put such a warning. You obviously can't put it against the "++" operator as such because... there isn't one. You could put it against the unary plus operator, but who's going to look there? :) I've wondered for a while whether it wouldn't be a good move to include the official (or any other) Python FAQ into the standard docs set. If we did, that would be the obvious place for this piece of documentation, seems to me. TJG From stef.mientki at gmail.com Mon Jul 6 04:05:32 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Mon, 06 Jul 2009 10:05:32 +0200 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: <4A51B04C.2080207@gmail.com> jack catcher (nick) wrote: > Hi, > > I'm thinking of using Python for capturing and showing live webcam > stream simultaneously between two computers via local area network. > Operating system is Windows. I'm going to begin with VideoCapture > extension, no ideas about other implementation yet. Do you have any > suggestions on how short delay I should hope to achieve in showing the > video? This would be part of a psychological experiment, so I would > need to deliver the video stream with a reasonable delay (say, below > 100ms). I would first check if video capture extension works anyway. I couldn't get it working, ... ... it seems to start ok ... but moving the captured window, ... or sometimes even just moving the mouse hangs the program :-( So I'm still looking for a good / open source workiing video capture in Python. I can make a good working capture in delphi, make a DLL or ActiveX from that and use it in Python, but I'm not allowed to ditribute these. cheers, Stef From serverin2000 at yahoo.com Mon Jul 6 04:11:27 2009 From: serverin2000 at yahoo.com (RAM) Date: Mon, 6 Jul 2009 01:11:27 -0700 (PDT) Subject: generation of keyboard events References: Message-ID: On 5 July, 17:12, Tim Harig wrote: > On 2009-07-05, RAM wrote: > > > I need to start an external program and pass the keyboard events like > > F1,Right arrow key etc to the program..I am trying to use the > > subprocess module to invoke the external program. I am able to invoke > > but not able to generate the keyboard events and pass them on to the > > catb.org/esr/faqs/smart-questions.html > > You have told us nothing about the environment where you are trying to > accomplish this. ?GUI, CLI, Unix, Windows, etc? So I suggest that you > checkout the curses getch functions. ?You can find them in the standard > library documentation athttp://docs.python.org. ?You should also reference > documentation for the C version in your systems man pages. Hi Tim, I am trying to do this on windows. My program(executable) has been written in VC++ and when I run this program, I need to click on one button on the program GUI i,e just I am entering "Enter key" on the key board. But this needs manual process. So i need to write a python script which invokes my program and pass "Enter key" event to my program so that it runs without manual intervention. Thank in advance for the help. regards Sreerama V From david.m.cooke at gmail.com Mon Jul 6 04:16:44 2009 From: david.m.cooke at gmail.com (David M. Cooke) Date: Mon, 6 Jul 2009 08:16:44 +0000 (UTC) Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <4A50FDC6.1040604@v.loewis.de> Message-ID: Martin v. L?wis v.loewis.de> writes: > > This is a good test for Python implementation bottlenecks. Run > > that tokenizer on HTML, and see where the time goes. > > I looked at it with cProfile, and the top function that comes up > for a larger document (52k) is > ...validator.HTMLConformanceChecker.__iter__. [...] > With this simple optimization, I get a 20% speedup on my test > case. In my document, there are no attributes - the same changes > should be made to attribute validation routines. > > I don't think this has anything to do with the case statement. I agree. I ran cProfile over just the tokenizer step; essentially tokenizer = html5lib.tokenizer.HTMLStream(htmldata) for tok in tokenizer: pass It mostly *isn't* tokenizer.py that's taking the most time, it's inputstream.py. (There is one exception: tokenizer.py:HTMLStream.__init__ constructs a dictionary of states each time -- this is unnecessary, replace all expressions like self.states["attributeName"] with self.attributeNameState.) I've done several optimisations -- I'll upload the patch to the html5lib issue tracker. In particular, * The .position property of EncodingBytes is used a lot. Every self.position +=1 calls getPosition() and setPosition(). Another getPosition() call is done in the self.currentByte property. Most of these can be optimised away by using methods that move the position and return the current byte. * In HTMLInputStream, the current line number and column are updated every time a new character is read with .char(). The current position is *only* used in error reporting, so I reworked it to only calculate the position when .position() is called, by keeping track of the number of lines in previous read chunks, and computing the number of lines to the current offset in the current chunk. These give me about a 20% speedup. This just illustrates that the first step in optimisation is profiling :D As other posters have said, slurping the whole document into memory and using a regexp-based parser (such as pyparsing) would likely give you the largest speedups. If you want to keep the chunk- based approach, you can still use regexp's, but you'd have to think about matching on chunk boundaries. One way would be to guarantee a minimum number of characters available, say 10 or 50 (unless end-of-file, of course) -- long enough such that any *constant* string you'd want to match like From ldo at geek-central.gen.new_zealand Mon Jul 6 04:29:04 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 20:29:04 +1200 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: In message , Tim Golden wrote: > The difficulty here is knowing where to put such a warning. > You obviously can't put it against the "++" operator as such > because... there isn't one. This bug is an epiphenomenon. :) From wuwei23 at gmail.com Mon Jul 6 04:30:03 2009 From: wuwei23 at gmail.com (alex23) Date: Mon, 6 Jul 2009 01:30:03 -0700 (PDT) Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <19f3d19d-c4bc-4f94-8387-febe624245a2@p18g2000pra.googlegroups.com> On Jul 6, 5:56?pm, Tim Golden wrote: > Gabriel Genellina wrote: > > In this case, a note in the documentation warning about the potential > > confusion would be fine. > > The difficulty here is knowing where to put such a warning. > You obviously can't put it against the "++" operator as such > because... there isn't one. You could put it against the unary > plus operator, but who's going to look there? :) The problem is: where do you stop? If you're going to add something to the documentation to address every expectation someone might hold coming from another language, the docs are going to get pretty big. I think a language should be intuitive within itself, but not be required to be intuitable based on _other_ languages (unless, of course, that's an objective of the language). If I expect something in language-A to operate the same way as completely-unrelated-language-B, I'd see that as a failing on my behalf, especially if I hadn't read language-A's documentation first. I'm not adverse to one language being _explained_ in terms of another, but would much prefer to see those relegated to "Python for programmers" articles rather than in the main docs themselves. From ldo at geek-central.gen.new_zealand Mon Jul 6 04:32:34 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 06 Jul 2009 20:32:34 +1200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> Message-ID: In message <4a4f91f9$0$1587$742ec2ed at news.sonic.net>, John Nagle wrote: > ("It should be written in C" is not an acceptable answer.) I don't see why not. State machines that have to process input byte by byte are well known to be impossible to implement efficiently in high-level languages. That's why lex/flex isn't worth using. Even GCC has been moving to hand-coded parsers, first the lexical analyzer, and more recently even for the syntax parser (getting rid of yacc/bison), certainly for C. From clp2 at rebertia.com Mon Jul 6 04:32:51 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 01:32:51 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <50697b2c0907060132w592ce54dt3c9392b764639bb2@mail.gmail.com> On Mon, Jul 6, 2009 at 1:29 AM, Lawrence D'Oliveiro wrote: > In message , Tim Golden > wrote: > >> The difficulty here is knowing where to put such a warning. >> You obviously can't put it against the "++" operator as such >> because... there isn't one. > > This bug is an epiphenomenon. :) Well, like I suggested, it /could/ be made an operator (or rather, a lexer token) which just causes a compile/parse error. Cheers, Chris -- http://blog.rebertia.com From simon at brunningonline.net Mon Jul 6 05:02:08 2009 From: simon at brunningonline.net (Simon Brunning) Date: Mon, 6 Jul 2009 10:02:08 +0100 Subject: generation of keyboard events In-Reply-To: References: Message-ID: <8c7f10c60907060202v60c7ea0ct51749bbaf0f81bb6@mail.gmail.com> 2009/7/6 RAM : > I am trying to do this on windows. My program(executable) has been > written in VC++ and when I run this program, I need to click on one > button on the program GUI i,e just I am entering "Enter key" on the > key board. But this needs manual process. So i need to write a python > script which invokes my program and pass "Enter key" event to my > program so that it runs without manual intervention. Try . -- Cheers, Simon B. From sjmachin at lexicon.net Mon Jul 6 05:15:57 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 6 Jul 2009 02:15:57 -0700 (PDT) Subject: A Bug By Any Other Name ... References: Message-ID: <2e02da2c-28d5-4ce5-9aa0-2827ba7110d3@12g2000pri.googlegroups.com> On Jul 6, 12:32?pm, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ? ? ++n > > and > > ? ? --n > > fail silently for numeric-valued n. What fail? In Python, ++n and --n are fatuous expressions which SUCCEED silently except for rare circiumstances e.g. --n will cause an overflow exception on older CPython versions if isinstance(n, int) and n == -sys.maxint - 1. From vilya.harvey at gmail.com Mon Jul 6 05:21:07 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Mon, 6 Jul 2009 10:21:07 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <6aef848f0907060221t6a44e10bga5a7798855779b5@mail.gmail.com> 2009/7/6 Xavier Ho : > Why is version B of the code faster than version A? (Only three lines > different) Here's a guess: As the number you're testing gets larger, version A is creating very big list. I'm not sure exactly how much overhead each list entry has in python, but I guess it's at least 8 bytes: a 32-bit reference for each list entry, and 32 bits to hold the int value (assuming a 32-bit version of python). The solution you're looking for is a large 8 digit number; let's say 80,000,000, for the sake of easy calculation. That means, as you get close to the solution, you'll be trying to allocate almost 640 Mb of memory for every number you're checking. That's going to make the garbage collector work extremely hard. Also, depending on how much memory your computer has free, you'll probably start hitting virtual memory too, which will slow you down even further. Finally, the reduce step has to process all 80,000,000 elements which is clearly going to take a while. Version b creates a list which is only as long as the largest prime factor, so at worst the list size will be approx. sqrt(80,000,000), which is approx. 8900 elements or approx. 72 Kb or memory - a much more manageable size. Hope that helps, Vil. From tjreedy at udel.edu Mon Jul 6 05:36:27 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 05:36:27 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: Gabriel Genellina wrote: > > In this case, a note in the documentation warning about the potential > confusion would be fine. How would that help someone who does not read the doc? From mooniitk at gmail.com Mon Jul 6 05:55:26 2009 From: mooniitk at gmail.com (mayank gupta) Date: Mon, 6 Jul 2009 11:55:26 +0200 Subject: Tree structure consuming lot of memory Message-ID: Hi, I am creating a tree data-structure in python; with nodes of the tree created by a simple class : class Node : def __init__(self , .... other attributes): # initialise the attributes here!! But the problem is I am working with a huge tree (millions of nodes); and each node is consuming much more memory than it should. After a little analysis, I found out that in general it uses about 1.4 kb of memory for each node!! I will be grateful if someone could help me optimize the memory usage. Thanks. Regards, Mayank -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 05:58:21 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 06 Jul 2009 09:58:21 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006da5a2$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 02:19:51 -0300, Gabriel Genellina wrote: > En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano > escribi?: >> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >> >>> I wonder how many people have been tripped up by the fact that >>> >>> ++n >>> >>> and >>> >>> --n >>> >>> fail silently for numeric-valued n. >> >> What do you mean, "fail silently"? They do exactly what you should >> expect: >>>>> ++5 # positive of a positive number is positive >> >> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >> that are buggy, not Python. > > Well, those expectations are taken seriously when new features are > introduced into the language - and sometimes the feature is dismissed > just because it would be confusing for some. If a += 1 works, expecting > ++a to have the same meaning is very reasonable (for those coming from > languages with a ++ operator, like C or Java) - more when ++a is a > perfectly valid expression. If this issue isn't listed under the various > "Python gotchas" articles, it should... The fact that it isn't suggests strongly to me that it isn't that common a surprise even for Java and C programmers. This is the first time I've seen anyone raise it as an issue. There are plenty of other languages other than Java and C. If we start listing every feature of Python that's different from some other language, we'll never end. For what it's worth, Ruby appears to behave the same as Python: $ irb irb(main):001:0> n = 5 => 5 irb(main):002:0> ++n => 5 irb(main):003:0> --n => 5 irb(main):004:0> -+n => -5 -- Steven From clp2 at rebertia.com Mon Jul 6 06:03:00 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 03:03:00 -0700 Subject: Tree structure consuming lot of memory In-Reply-To: References: Message-ID: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> On Mon, Jul 6, 2009 at 2:55 AM, mayank gupta wrote: > Hi, > > I am creating a tree data-structure in python; with nodes of the tree > created by a simple class : > > class Node : > ?????? def __init__(self , .... other attributes): > ????????????? # initialise the attributes here!! > > But the problem is I am working with a huge tree (millions of nodes); and > each node is consuming much more memory than it should. After a little > analysis, I found out that in general it uses about 1.4 kb of memory for > each node!! > I will be grateful if someone could help me optimize the memory usage. (1) Use __slots__ (see http://docs.python.org/reference/datamodel.html#slots) (2) Use some data structure other than a tree (3) Rewrite your Node/Tree implementation in C Cheers, Chris -- http://blog.rebertia.com From davea at dejaviewphoto.com Mon Jul 6 06:09:46 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Mon, 06 Jul 2009 06:09:46 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A51CD6A.2010106@dejaviewphoto.com> Xavier Ho wrote: > (Here's a short version of the long version below if you don't want to > read:) > > Why is version B of the code faster than version A? (Only three lines > different) > > Version A: http://pastebin.com/f14561243 > Version B: http://pastebin.com/f1f657afc > > ------------------------------------------------ > > I was doing the problems on Project Euler for practice with Python last > night. Problem 12 was to find the value of the first triangular number that > has over 500 divisors. > ========================================================================================= > > The sequence of triangle numbers is generated by adding the natural numbers. > So the 7[image: ^(]th[image: )] triangle number would be 1 + 2 + 3 + 4 + 5 + > 6 + 7 = 28. The first ten terms would be: > > 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... > > Let us list the factors of the first seven triangle numbers: > > * 1*: 1 > * 3*: 1,3 > * 6*: 1,2,3,6 > *10*: 1,2,5,10 > *15*: 1,3,5,15 > *21*: 1,3,7,21 > *28*: 1,2,4,7,14,28 > > We can see that 28 is the first triangle number to have over five divisors. > > What is the value of the first triangle number to have over five hundred > divisors? > ========================================================================================= > > My initial code was to loop through from 1 to half the number and see which > were divisors, and as I find them I store them in a list. That would have > taken days. > > My second try was factorising the number each time, and count the divisors > using the powers of each factor, plus 1, and multiply together. > The code is here (Version A): http://pastebin.com/f14561243 > > This worked, but it took overnight to compute. Before I went to bed a friend > of mine caught me online, and apparently left me a working version under 8 > seconds with only 3 line difference. > The code is here (Version B): http://pastebin.com/f1f657afc > > That was amazing. But I have no idea why his edit makes it so much faster. I > did a test to see whether if append() was faster (which I doubted) than > defining a list with a large size to begin with, and I was right: > http://pastebin.com/f4b46d0db > Which shows that appending is 40x slower, and was expected. But I still > can't puzzle out why his use of appending in Version B was so much faster > than mine. > > Any insights would be welcome. I'm going on a family trip, though, so my > replies may delay. > > Best regards, > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > Just by inspection, it would seem the bottleneck in your first version is that you return a huge list of nearly all zeroes, from factorize(). This slows down countDivisors() a great deal. It would probably save some time to not bother storing the zeroes in the list at all. And it should help if you were to step through a list of primes, rather than trying every possible int. Or at least constrain yourself to odd numbers (after the initial case of 2). DaveA From mooniitk at gmail.com Mon Jul 6 06:12:06 2009 From: mooniitk at gmail.com (mayank gupta) Date: Mon, 6 Jul 2009 12:12:06 +0200 Subject: Tree structure consuming lot of memory In-Reply-To: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> References: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> Message-ID: Thanks for the other possibilites. I would consider option (2) and (3) to improve my code. But out of curiosity, I would still like to know why does an object of a Python-class consume "so" much of memory (1.4 kb), and this memory usage has nothing to do with its attributes. Thanks Regards. On Mon, Jul 6, 2009 at 12:03 PM, Chris Rebert wrote: > On Mon, Jul 6, 2009 at 2:55 AM, mayank gupta wrote: > > Hi, > > > > I am creating a tree data-structure in python; with nodes of the tree > > created by a simple class : > > > > class Node : > > def __init__(self , .... other attributes): > > # initialise the attributes here!! > > > > But the problem is I am working with a huge tree (millions of nodes); and > > each node is consuming much more memory than it should. After a little > > analysis, I found out that in general it uses about 1.4 kb of memory for > > each node!! > > I will be grateful if someone could help me optimize the memory usage. > > (1) Use __slots__ (see > http://docs.python.org/reference/datamodel.html#slots) > (2) Use some data structure other than a tree > (3) Rewrite your Node/Tree implementation in C > > Cheers, > Chris > -- > http://blog.rebertia.com > -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From p.f.moore at gmail.com Mon Jul 6 08:05:02 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 6 Jul 2009 13:05:02 +0100 Subject: Opening a SQLite database in readonly mode Message-ID: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> The SQLite documentation mentions a flag, SQLITE_OPEN_READONLY, to open a database read only. I can't find any equivalent documented in the Python standard library documentation for the sqlite3 module (or, for that matter, on the pysqlite library's website). Is it possible to open a sqlite database in readonly mode, in Python? Thanks, Paul. From mail at microcorp.co.za Mon Jul 6 08:12:42 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 6 Jul 2009 14:12:42 +0200 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <00df01c9fe33$10c36d80$0d00a8c0@Hendrik> "Terry Reedy" wrote: > Gabriel Genellina wrote: > > > > In this case, a note in the documentation warning about the potential > > confusion would be fine. > > How would that help someone who does not read the doc? It obviously won't. All it will do, is that it will enable people on this group, who may read the manual, to tell people who complain, to RTFM. I agree that it would be a good idea to make it an error, or a warning - "this might not do what you think it does", or an "are you sure?" exception. :-) - Hendrik From contact at xavierho.com Mon Jul 6 08:12:58 2009 From: contact at xavierho.com (Xavier Ho) Date: Mon, 6 Jul 2009 20:12:58 +0800 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A51CD6A.2010106@dejaviewphoto.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> Message-ID: <2d56febf0907060512m794c2468sfa01cf332df3167a@mail.gmail.com> Thanks for the response all, I finally got my 'net working on the mountains, and I think your reasons are quite sound. I'll keep that in mind for the future. Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Mon, Jul 6, 2009 at 6:09 PM, Dave Angel wrote: > Xavier Ho wrote: > >> (Here's a short version of the long version below if you don't want to >> read:) >> >> Why is version B of the code faster than version A? (Only three lines >> different) >> >> Version A: http://pastebin.com/f14561243 >> Version B: http://pastebin.com/f1f657afc >> >> ------------------------------------------------ >> >> I was doing the problems on Project Euler for practice with Python last >> night. Problem 12 was to find the value of the first triangular number >> that >> has over 500 divisors. >> >> ========================================================================================= >> >> The sequence of triangle numbers is generated by adding the natural >> numbers. >> So the 7[image: ^(]th[image: )] triangle number would be 1 + 2 + 3 + 4 + 5 >> + >> >> 6 + 7 = 28. The first ten terms would be: >> >> 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... >> >> Let us list the factors of the first seven triangle numbers: >> >> * 1*: 1 >> * 3*: 1,3 >> * 6*: 1,2,3,6 >> *10*: 1,2,5,10 >> *15*: 1,3,5,15 >> *21*: 1,3,7,21 >> *28*: 1,2,4,7,14,28 >> >> We can see that 28 is the first triangle number to have over five >> divisors. >> >> What is the value of the first triangle number to have over five hundred >> divisors? >> >> ========================================================================================= >> >> My initial code was to loop through from 1 to half the number and see >> which >> were divisors, and as I find them I store them in a list. That would have >> taken days. >> >> My second try was factorising the number each time, and count the divisors >> using the powers of each factor, plus 1, and multiply together. >> The code is here (Version A): http://pastebin.com/f14561243 >> >> This worked, but it took overnight to compute. Before I went to bed a >> friend >> of mine caught me online, and apparently left me a working version under 8 >> seconds with only 3 line difference. >> The code is here (Version B): http://pastebin.com/f1f657afc >> >> That was amazing. But I have no idea why his edit makes it so much faster. >> I >> did a test to see whether if append() was faster (which I doubted) than >> defining a list with a large size to begin with, and I was right: >> http://pastebin.com/f4b46d0db >> Which shows that appending is 40x slower, and was expected. But I still >> can't puzzle out why his use of appending in Version B was so much faster >> than mine. >> >> Any insights would be welcome. I'm going on a family trip, though, so my >> replies may delay. >> >> Best regards, >> >> Ching-Yun "Xavier" Ho, Technical Artist >> >> Contact Information >> Mobile: (+61) 04 3335 4748 >> Skype ID: SpaXe85 >> Email: contact at xavierho.com >> Website: http://xavierho.com/ >> >> >> > Just by inspection, it would seem the bottleneck in your first version is > that you return a huge list of nearly all zeroes, from factorize(). This > slows down countDivisors() a great deal. > > It would probably save some time to not bother storing the zeroes in the > list at all. And it should help if you were to step through a list of > primes, rather than trying every possible int. Or at least constrain > yourself to odd numbers (after the initial case of 2). > > DaveA > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pm567426 at gmail.com Mon Jul 6 08:24:43 2009 From: pm567426 at gmail.com (Pedram) Date: Mon, 6 Jul 2009 05:24:43 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: OK, fine, I read longobject.c at last! :) I found that longobject is a structure like this: struct _longobject { struct _object *_ob_next; struct _object *_ob_prev; Py_ssize_t ob_refcnt; struct _typeobject *ob_type; digit ob_digit[1]; } And a digit is a 15-item array of C's unsigned short integers. Am I right? Or I missed something! Is this structure is constant in all environments (Linux, Windows, Mobiles, etc.)? From jeanmichel at sequans.com Mon Jul 6 08:32:10 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 06 Jul 2009 14:32:10 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: <4A51EECA.10509@sequans.com> kj wrote: > I've rewritten it like this: > > sense = cmp(func(hi), func(lo)) > assert sense != 0, "func is not strictly monotonic in [lo, hi]" > > Thanks for your feedback! > > kj > As already said before, unlike other languages, sense in english does **not** mean direction. You should rewrite this part using a better name. Wrong informations are far worse than no information at all. JM From Scott.Daniels at Acm.Org Mon Jul 6 08:33:02 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 05:33:02 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: References: Message-ID: Andre Engels wrote: > On Mon, Jul 6, 2009 at 9:44 AM, Martin Vilcans wrote: >> On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: >>> I'm will be teaching a programming class to novices, and I've run >>> into a clear conflict between two of the principles I'd like to >>> teach: code clarity vs. code reuse. I'd love your opinion about >>> it. >> In general, code clarity is more important than reusability. >> Unfortunately, many novice programmers have the opposite impression. I >> have seen too much convoluted code written by beginners who try to >> make the code generic. Writing simple, clear, to-the-point code is >> hard enough as it is, even when not aiming at making it reusable. >> >> If in the future you see an opportunity to reuse the code, then and >> only then is the time to make it generic. > > Not just that, when you actually get to that point, making simple and > clear code generic is often easier than making > complicated-and-supposedly-generic code that little bit more generic > that you need. First, a quote which took me a bit to find: Thomas William K?rner paraphrasing Polya and Svego in A Companion to Analysis: Recalling that 'once is a trick, twice is a method, thrice is a theorem, and four times a theory,' we seek to codify this insight. Let us apply this insight: Suppose in writing code, we pretty much go with that. A method is something you notice, a theorem is a function, and a theory is a generalized function. Even though we like DRY ("don't repeat yourself") as a maxim, let it go the first time and wait until you see the pattern (a possible function). I'd go with a function first, a pair of functions, and only then look to abstracting the function. --Scott David Daniels Scott.Daniels at Acm.Org From Scott.Daniels at Acm.Org Mon Jul 6 08:47:18 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 05:47:18 -0700 Subject: Creating alot of class instances? In-Reply-To: <006d4e86$0$9711$c3e8da3@news.astraweb.com> References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > ... That's the Wrong Way to do it -- > you're using a screwdriver to hammer a nail.... Don't knock tool abuse (though I agree with you here). Sometimes tool abuse can produce good results. For example, using hammers to drive screws for temporary strong holds led to making better nails. --Scott David Daniels Scott.Daniels at Acm.Org From ronn.ross at gmail.com Mon Jul 6 08:47:30 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Mon, 6 Jul 2009 08:47:30 -0400 Subject: VirtualEnv Message-ID: <9c8c445f0907060547g5a279821n4cd31efe9231887b@mail.gmail.com> I'm attempting to write a bootstrap script for virtualenv. I just want to do a couple of easy_install's after the environment is created. It was fairly easy to create the script, but I can't figure out how to implement it. The documentation was not of much help. Can someone please point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeanmichel at sequans.com Mon Jul 6 08:54:03 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 06 Jul 2009 14:54:03 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <006201c9fd48$666d94e0$0d00a8c0@Hendrik> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <006201c9fd48$666d94e0$0d00a8c0@Hendrik> Message-ID: <4A51F3EB.1080509@sequans.com> > protocol = {"start":initialiser,"hunt":hunter,"classify":classifier,....other > states} > > def state_machine(): > next_step = protocol["start"]() > while True: > next_step = protocol[next_step]() > > Woot ! I'll keep this one in my mind, while I may not be that concerned by speed unlike the OP, I still find this way of doing very simple and so intuitive (one will successfully argue how I was not figuring this out by myself if it was so intuitive). Anyway I wanted to participated to this thread, as soon as I saw 'due to python limitations' in the title, I foretold a hell of a thread ! This is just provocation ! :-) JM From pdpinheiro at gmail.com Mon Jul 6 09:38:13 2009 From: pdpinheiro at gmail.com (pdpi) Date: Mon, 6 Jul 2009 06:38:13 -0700 (PDT) Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: On Jul 6, 1:12?pm, "Hendrik van Rooyen" wrote: > "Terry Reedy" wrote: > > Gabriel Genellina wrote: > > > > In this case, a note in the documentation warning about the potential > > > confusion would be fine. > > > How would that help someone who does not read the doc? > > It obviously won't. > > All it will do, is that it will enable people on this group, > who may read the manual, to tell people who complain, > to RTFM. > > ?I agree that it would be a good idea to make it an > error, or a warning - "this might not do what you > think it does", or an "are you sure?" exception. > > ? :-) > > - Hendrik I dunno. Specifically recognizing (and emitting code code for) a token that's not actually part of the language because people coming from other languages think it exists seems like the start of a fustercluck. From dickinsm at gmail.com Mon Jul 6 09:46:05 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Mon, 6 Jul 2009 06:46:05 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> Message-ID: <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> On Jul 6, 1:24?pm, Pedram wrote: > OK, fine, I read longobject.c at last! :) > I found that longobject is a structure like this: > > struct _longobject { > ? ? struct _object *_ob_next; > ? ? struct _object *_ob_prev; For current CPython, these two fields are only present in debug builds; for a normal build they won't exist. > ? ? Py_ssize_t ob_refcnt; > ? ? struct _typeobject *ob_type; You're missing an important field here (see the definition of PyObject_VAR_HEAD): Py_ssize_t ob_size; /* Number of items in variable part */ For the current implementation of Python longs, the absolute value of this field gives the number of digits in the long; the sign gives the sign of the long (0L is represented with zero digits). > ? ? digit ob_digit[1]; Right. This is an example of the so-called 'struct hack' in C; it looks as though there's just a single digit, but what's intended here is that there's an array of digits tacked onto the end of the struct; for any given PyLongObject, the size of this array is determined at runtime. (C99 allows you to write this as simply ob_digit[], but not all compilers support this yet.) > } > And a digit is a 15-item array of C's unsigned short integers. No: a digit is a single unsigned short, which is used to store 15 bits of the Python long. Python longs are stored in sign-magnitude format, in base 2**15. So each of the base 2**15 'digits' is an integer in the range [0, 32767). The unsigned short type is used to store those digits. Exception: for Python 2.7+ or Python 3.1+, on 64-bit machines, Python longs are stored in base 2**30 instead of base 2**15, using a 32-bit unsigned integer type in place of unsigned short. > Is this structure is constant in > all environments (Linux, Windows, Mobiles, etc.)? I think it would be dangerous to rely on this struct staying constant, even just for CPython. It's entirely possible that the representation of Python longs could change in Python 2.8 or 3.2. You should use the public, documented C-API whenever possible. Mark From philip at semanchuk.com Mon Jul 6 09:51:58 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 09:51:58 -0400 Subject: How to map size_t using ctypes? Message-ID: Hi all, I can't figure out how to map a C variable of size_t via Python's ctypes module. Let's say I have a C function like this: void populate_big_array(double *the_array, size_t element_count) {...} How would I pass parameter 2? A long (or ulong) will (probably) work (on most platforms), but I like my code to be more robust than that. Furthermore, this is scientific code and it's entirely possible that someone will want to pass a huge array with more elements than can be described by a 32-bit long. Suggestions appreciated. Thanks Philip From Scott.Daniels at Acm.Org Mon Jul 6 09:59:44 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 06:59:44 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <025f4dbf$0$20657$c3e8da3@news.astraweb.com> <025f5b0d$0$20657$c3e8da3@news.astraweb.com> <600d2270-2c02-4709-b636-6b18b36da913@l31g2000yqb.googlegroups.com> <4A4FDCAF.8010209@Acm.Org> Message-ID: <4A520350.9000806@Acm.Org> Peter Otten wrote: > Scott David Daniels wrote: > >> Scott David Daniels wrote: > >> t = timeit.Timer('sum(part[:-1]==part[1:])', >> 'from __main__ import part') > > What happens if you calculate the sum in numpy? Try > > t = timeit.Timer('(part[:-1]==part[1:]).sum()', > 'from __main__ import part') Good idea, I hadn't thought of adding numpy bools. (part[:-1]==part[1:]).sum() is only a slight improvement over len(part[part[:-1]==part[1:]]) when there are few elements, but it is almost twice as fast when there are a lot (reflecting the work of allocating and copying). >>> import numpy >>> import timeit >>> original = numpy.random.normal(0, 100, (1000, 1000)).astype(int) >>> data = original.flatten() >>> data.sort() >>> t = timeit.Timer('sum(part[:-1]==part[1:])', 'from __main__ import part') >>> u = timeit.Timer('len(part[part[:-1]==part[1:]])', 'from __main__ import part') >>> v = timeit.Timer('(part[:-1]==part[1:]).sum()', 'from __main__ import part') >>> part = data[::100] >>> (part[:-1]==part[1:]).sum() 9390 >>> t.repeat(3, 10) [0.56368281443587875, 0.55615057220961717, 0.55465764503594528] >>> u.repeat(3, 1000) [0.89576580263690175, 0.89276374511291579, 0.8937328626963108] >>> v.repeat(3, 1000) [0.24798598704592223, 0.24715431709898894, 0.24498979618920202] >>> >>> part = original.flatten()[::100] >>> (part[:-1]==part[1:]).sum() 27 >>> t.repeat(3, 10) [0.57576898739921489, 0.56410158274297828, 0.56988248506445416] >>> u.repeat(3, 1000) [0.27312186325366383, 0.27315007913011868, 0.27214492344683094] >>> v.repeat(3, 1000) [0.28410342655297427, 0.28374053126867693, 0.28318990262732768] >>> Net result: go back to former definition of candidates (a number, not the actual entries), but calculate that number as matches.sum(), not len(part[matches]). Now the latest version of this (compressed) code: > ... > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) # count identified matches > while candidates > N * 10: # 10 -- heuristic > stride *= 2 # # heuristic increase > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > while candidates < N * 3: # heuristic slop for long runs > stride //= 2 # heuristic decrease > sampled = data[::stride] > matches = sampled[:-1] == sampled[1:] > candidates = sum(matches) > former = None > past = 0 > for value in sampled[matches]: > ... is: ... sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = matches.sum() # count identified matches while candidates > N * 10: # 10 -- heuristic stride *= 2 # # heuristic increase sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = matches.sum() while candidates < N * 3: # heuristic slop for long runs stride //= 2 # heuristic decrease sampled = data[::stride] matches = sampled[:-1] == sampled[1:] candidates = matches.sum() former = None past = 0 for value in sampled[matches]: ... Now I think I can let this problem go, esp. since it was mclovin's problem in the first place. --Scott David Daniels Scott.Daniels at Acm.Org From dickinsm at gmail.com Mon Jul 6 10:01:42 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Mon, 6 Jul 2009 07:01:42 -0700 (PDT) Subject: A Bug By Any Other Name ... References: Message-ID: On Jul 6, 3:32?am, Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ? ? ++n > > and > > ? ? --n > > fail silently for numeric-valued n. Recent python-ideas discussion on this subject: http://mail.python.org/pipermail/python-ideas/2009-March/003741.html Mark From mail at microcorp.co.za Mon Jul 6 10:04:30 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 6 Jul 2009 16:04:30 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <006201c9fd48$666d94e0$0d00a8c0@Hendrik> <4A51F3EB.1080509@sequans.com> Message-ID: <000901c9fe42$b026cfc0$0d00a8c0@Hendrik> "Jean-Michel Pichavant" wrote: > Woot ! I'll keep this one in my mind, while I may not be that concerned > by speed unlike the OP, I still find this way of doing very simple and > so intuitive (one will successfully argue how I was not figuring this > out by myself if it was so intuitive). > Anyway I wanted to participated to this thread, as soon as I saw 'due to > python limitations' in the title, I foretold a hell of a thread ! This > is just provocation ! :-) The OP was not being provocative - he has a real problem, and the code he is complaining about already does more or less what my snippet showed, as I rushed in where angels fear to tread... The bit that was not clearly shown in what I proposed, is that you should stay in the individual states, testing for the reasons for the state transitions, until it is time to change - so there is a while loop in each of the individual states too. It becomes a terribly big structure if you have a lot of states, it duplicates a lot of tests across the different states, and it is very awkward if the states nest. Have a look at the one without the dict too - it is even faster as it avoids the dict lookup. That, however, is a bit like assembler code, as it kind of "jumps" from state to state, and there is no central thing to show what does, and what does not, belong together, as there is no dict. Not an easy beast to fix if it's big and it's wrong. - Hendrik From "sandrodll[remove]" at googlemail.com Mon Jul 6 10:21:01 2009 From: "sandrodll[remove]" at googlemail.com (gialloporpora) Date: Mon, 06 Jul 2009 16:21:01 +0200 Subject: Help to find a regular expression to parse po file Message-ID: <4A52084D.1010900@googlemail.com> Hi all, I would like to extract string from a PO file. To do this I have created a little python function to parse po file and extract string: import re regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") m=r.findall(s) where s is a po file like this: msgctxt "write ubiquity commands.description" msgid "Takes you to the Ubiquity
command editor page." msgstr "Apre l'editor dei comandi di Ubiquity." #. list ubiquity commands command: #. use | to separate multiple name values: msgctxt "list ubiquity commands.names" msgid "list ubiquity commands" msgstr "elenco comandi disponibili" msgctxt "list ubiquity commands.description" msgid "Opens the list\n" " of all Ubiquity commands available and what they all do." msgstr "Apre una pagina\n" " in cui sono elencati tutti i comandi disponibili e per ognuno viene spiegato in breve a cosa serve." #. change ubiquity settings command: #. use | to separate multiple name values: msgctxt "change ubiquity settings.names" msgid "change ubiquity settings|change ubiquity preferences|change ubiquity skin" msgstr "modifica impostazioni di ubiquity|modifica preferenze di ubiquity|modifica tema di ubiquity" msgctxt "change ubiquity settings.description" msgid "Takes you to the settings page,\n" " where you can change your skin, key combinations, etc." msgstr "Apre la pagina delle impostazioni di Ubiquity,\n" " dalla quale ?? possibile modificare la combinazione da tastiera utilizzata per richiamare Ubiquity, il tema, ecc." but, obviusly, with the code above the last string is not matched. If I use re.DOTALL to match also new line character it not works because it match the entire file, I would like to stop the matching when "msgstr" is found. regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n\\n",re.DOTALL) is it possible or not ? From python at mrabarnett.plus.com Mon Jul 6 10:22:23 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 15:22:23 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A51CD6A.2010106@dejaviewphoto.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> Message-ID: <4A52089F.7020301@mrabarnett.plus.com> Dave Angel wrote: [snip] > It would probably save some time to not bother storing the zeroes in the > list at all. And it should help if you were to step through a list of > primes, rather than trying every possible int. Or at least constrain > yourself to odd numbers (after the initial case of 2). > Or stop looking for more factors when you've passed the square root of num. I don't know what effect there'll be on the time if you recalculate the square root when num changes (expensive calculation vs smaller search space). From rhodri at wildebst.demon.co.uk Mon Jul 6 11:00:59 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 06 Jul 2009 16:00:59 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: <006da5a2$0$9711$c3e8da3@news.astraweb.com> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: On Mon, 06 Jul 2009 10:58:21 +0100, Steven D'Aprano wrote: > On Mon, 06 Jul 2009 02:19:51 -0300, Gabriel Genellina wrote: > >> En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano >> escribi?: >>> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >>> >>>> I wonder how many people have been tripped up by the fact that >>>> >>>> ++n >>>> >>>> and >>>> >>>> --n >>>> >>>> fail silently for numeric-valued n. >>> >>> What do you mean, "fail silently"? They do exactly what you should >>> expect: >>>>>> ++5 # positive of a positive number is positive >>> >>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>> that are buggy, not Python. >> >> Well, those expectations are taken seriously when new features are >> introduced into the language - and sometimes the feature is dismissed >> just because it would be confusing for some. If a += 1 works, expecting >> ++a to have the same meaning is very reasonable (for those coming from >> languages with a ++ operator, like C or Java) - more when ++a is a >> perfectly valid expression. If this issue isn't listed under the various >> "Python gotchas" articles, it should... > > The fact that it isn't suggests strongly to me that it isn't that common > a surprise even for Java and C programmers. This is the first time I've > seen anyone raise it as an issue. Indeed, arguably it's a bug for C compilers to fail to find the valid parsing of "++5" as "+(+5)". All I can say is that I've never even accidentally typed that in twenty years of C programming. -- Rhodri James *-* Wildebeest Herder to the Masses From h.b.furuseth at usit.uio.no Mon Jul 6 11:04:06 2009 From: h.b.furuseth at usit.uio.no (Hallvard B Furuseth) Date: Mon, 06 Jul 2009 17:04:06 +0200 Subject: Help to find a regular expression to parse po file References: <4A52084D.1010900@googlemail.com> Message-ID: gialloporpora writes: > I would like to extract string from a PO file. To do this I have created > a little python function to parse po file and extract string: > > import re > regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") > m=r.findall(s) I don't know the syntax of a po file, but this works for the snippet you posted: arg_re = r'"[^\\\"]*(?:\\.[^\\\"]*)*"' arg_re = '%s(?:\s+%s)*' % (arg_re, arg_re) find_re = re.compile( r'^msgid\s+(' + arg_re + ')\s*\nmsgstr\s+(' + arg_re + ')\s*\n', re.M) However, can \ quote a newline? If so, replace \\. with \\[\s\S] or something. Can there be other keywords between msgid and msgstr? If so, add something like (?:\w+\s+\s*\n)*? between them. Can msgstr come before msgid? If so, forget using a single regexp. Anything else to the syntax to look out for? Single quotes, maybe? Is it a problem if the regexp isn't quite right and doesn't match all cases, yet doesn't report an error when that happens? All in all, it may be a bad idea to sqeeze this into a single regexp. It gets ugly real fast. Might be better to parse the file in a more regular way, maybe using regexps just to extract each (keyword, "value") pair. -- Hallvard From james at agentultra.com Mon Jul 6 11:06:50 2009 From: james at agentultra.com (J Kenneth King) Date: Mon, 06 Jul 2009 11:06:50 -0400 Subject: Code that ought to run fast, but can't due to Python limitations. References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <0260603f$0$20657$c3e8da3@news.astraweb.com> <7xr5wvtqoh.fsf@ruckus.brouhaha.com> Message-ID: <85prcdetpx.fsf@agentultra.com> aahz at pythoncraft.com (Aahz) writes: > In article , > Hendrik van Rooyen wrote: >> >>But wait - maybe if he passes an iterator around - the equivalent of >>for char in input_stream... Still no good though, unless the next call >>to the iterator is faster than an ordinary python call. > > Calls to iterators created by generators are indeed faster than an > ordinary Python call, because the stack frame is already mostly set up. I think Beazely demonstrated this in his talk on using the python 2.5 co-routines to setup an xml parser. I believe he benchmarked it roughly and the initial results were rather impressive. http://www.dabeaz.com/coroutines/ From python at mrabarnett.plus.com Mon Jul 6 11:12:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 16:12:18 +0100 Subject: Help to find a regular expression to parse po file In-Reply-To: <4A52084D.1010900@googlemail.com> References: <4A52084D.1010900@googlemail.com> Message-ID: <4A521452.4090405@mrabarnett.plus.com> gialloporpora wrote: > Hi all, > I would like to extract string from a PO file. To do this I have created > a little python function to parse po file and extract string: > > import re > regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") > m=r.findall(s) > > where s is a po file like this: > > msgctxt "write ubiquity commands.description" > msgid "Takes you to the Ubiquity href=\"chrome://ubiquity/content/editor.html\">command editor page." > msgstr "Apre l'editor > dei comandi di Ubiquity." > > > #. list ubiquity commands command: > #. use | to separate multiple name values: > msgctxt "list ubiquity commands.names" > msgid "list ubiquity commands" > msgstr "elenco comandi disponibili" > > msgctxt "list ubiquity commands.description" > msgid "Opens the > list\n" > " of all Ubiquity commands available and what they all do." > msgstr "Apre una href=\"chrome://ubiquity/content/cmdlist.html\">pagina\n" > " in cui sono elencati tutti i comandi disponibili e per ognuno > viene spiegato in breve a cosa serve." > > > > #. change ubiquity settings command: > #. use | to separate multiple name values: > msgctxt "change ubiquity settings.names" > msgid "change ubiquity settings|change ubiquity preferences|change > ubiquity skin" > msgstr "modifica impostazioni di ubiquity|modifica preferenze di > ubiquity|modifica tema di ubiquity" > > msgctxt "change ubiquity settings.description" > msgid "Takes you to the href=\"chrome://ubiquity/content/settings.html\">settings page,\n" > " where you can change your skin, key combinations, etc." > msgstr "Apre la pagina href=\"chrome://ubiquity/content/settings.html\">delle impostazioni > di Ubiquity,\n" > " dalla quale ?? possibile modificare la combinazione da tastiera > utilizzata per richiamare Ubiquity, il tema, ecc." > > > > but, obviusly, with the code above the last string is not matched. If > I use re.DOTALL to match also new line character it not works because it > match the entire file, I would like to stop the matching when "msgstr" > is found. > > regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n\\n",re.DOTALL) > > is it possible or not ? > You could try: regex = re.compile(r"msgid (.*(?:\n".*")*)\nmsgstr (.*(?:\n".*")*)$") and then, if necessary, tidy what you get. From pm567426 at gmail.com Mon Jul 6 11:13:00 2009 From: pm567426 at gmail.com (Pedram) Date: Mon, 6 Jul 2009 08:13:00 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> Message-ID: <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> Hello Mr. Dickinson. Glad to see you again :) On Jul 6, 5:46?pm, Mark Dickinson wrote: > On Jul 6, 1:24?pm, Pedram wrote: > > > OK, fine, I read longobject.c at last! :) > > I found that longobject is a structure like this: > > > struct _longobject { > > ? ? struct _object *_ob_next; > > ? ? struct _object *_ob_prev; > > For current CPython, these two fields are only present in debug > builds; ?for a normal build they won't exist. I couldn't understand the difference between them. What are debug build and normal build themselves? And You mean in debug build PyLongObject is a doubly-linked-list but in normal build it is just an array (Or if not how it'll store in this mode)? > > ? ? Py_ssize_t ob_refcnt; > > ? ? struct _typeobject *ob_type; > > You're missing an important field here (see the definition of > PyObject_VAR_HEAD): > > ? ? Py_ssize_t ob_size; /* Number of items in variable part */ > > For the current implementation of Python longs, the absolute value of > this field gives the number of digits in the long; ?the sign gives the > sign of the long (0L is represented with zero digits). Oh, you're right. I missed that. Thanks :) > > ? ? digit ob_digit[1]; > > Right. ?This is an example of the so-called 'struct hack' in C; it > looks as though there's just a single digit, but what's intended here > is that there's an array of digits tacked onto the end of the struct; > for any given PyLongObject, the size of this array is determined at > runtime. ?(C99 allows you to write this as simply ob_digit[], but not > all compilers support this yet.) WOW! I didn't know anything about 'struct hacks'! I read about them and they were very wonderful. Thanks for your point. :) > > } > > And a digit is a 15-item array of C's unsigned short integers. > > No: a digit is a single unsigned short, which is used to store 15 bits > of the Python long. ?Python longs are stored in sign-magnitude format, > in base 2**15. ?So each of the base 2**15 'digits' is an integer in > the range [0, 32767). ?The unsigned short type is used to store those > digits. > > Exception: for Python 2.7+ or Python 3.1+, on 64-bit machines, Python > longs are stored in base 2**30 instead of base 2**15, using a 32-bit > unsigned integer type in place of unsigned short. > > > Is this structure is constant in > > all environments (Linux, Windows, Mobiles, etc.)? > > I think it would be dangerous to rely on this struct staying constant, > even just for CPython. ?It's entirely possible that the representation > of Python longs could change in Python 2.8 or 3.2. ?You should use the > public, documented C-API whenever possible. > > Mark Thank you a lot Mark :) From piet at cs.uu.nl Mon Jul 6 11:15:47 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Jul 2009 17:15:47 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: >>>>> Dave Angel (DA) wrote: >DA> It would probably save some time to not bother storing the zeroes in the >DA> list at all. And it should help if you were to step through a list of >DA> primes, rather than trying every possible int. Or at least constrain >DA> yourself to odd numbers (after the initial case of 2). The first and the last save a constant factor (slightly less than 2): def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] factor = 2 while num > 1: power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: powers.append(power+1) factor += 1 return powers ... return reduce(mul, powers) or to skip the odd factors: def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] factor = 2 while num > 1: power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: powers.append(power+1) factor = 3 if factor == 2 else factor + 2 return powers This can be slightly optimised by taking factor 2 out of the loop. def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] power = 0 while num % 2 == 0: power += 1 num /= 2 if power > 0: powers.append(power+1) factor = 3 while num > 1: power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: powers.append(power+1) factor += 2 return powers To restrict the search to primes you would have to use a sieve of Eratosthenes or something similar. My first attempt (with a sieve from http://code.activestate.com/recipes/117119/) only gave a speed decrease!! But this had the sieve recreated for every triangle number. A global sieve that is reused at each triangle number is better. But the speed increase relative to the odd factors only is not dramatical. # Based upon http://code.activestate.com/recipes/117119/ D = {9: 6} # contains composite numbers Dlist = [2, 3] # list of already generated primes def sieve(): '''generator that yields all prime numbers''' global D global Dlist for p in Dlist: yield p q = Dlist[-1]+2 while True: if q in D: p = D[q] x = q + p while x in D: x += p D[x] = p else: Dlist.append(q) yield q D[q*q] = 2*q q += 2 def factorise(num): """Returns a list of prime factor powers. For example: factorise(6) will return [2, 2] (the powers are returned one higher than the actual value) as in, 2^1 * 3^1 = 6.""" powers = [] power = 0 for factor in sieve(): power = 0 while num % factor == 0: power += 1 num /= factor if power > 0: # if you really want the factors then append((factor, power)) powers.append(power+1) if num == 1: break return powers -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rhodri at wildebst.demon.co.uk Mon Jul 6 11:22:43 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 06 Jul 2009 16:22:43 +0100 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: On Mon, 06 Jul 2009 07:10:38 +0100, jack catcher (nick) wrote: > Tim Roberts kirjoitti: >> "jack catcher (nick)" wrote: >>> I'm thinking of using Python for capturing and showing live webcam >>> stream simultaneously between two computers via local area network. >>> Operating system is Windows. I'm going to begin with VideoCapture >>> extension, no ideas about other implementation yet. Do you have any >>> suggestions on how short delay I should hope to achieve in showing the >>> video? This would be part of a psychological experiment, so I would >>> need to deliver the video stream with a reasonable delay (say, below >>> 100ms). >> You need to do the math on this. Remember that a full 640x480 RGB >> stream >> at 30 frames per second runs 28 megabytes per second. That's more than >> twice what a 100 megabit network can pump. >> You can probably use Python to oversee this, but you might want to >> consider >> using lower-level code to control the actual hardware. If you are >> targeting Windows, for example, you could write a DirectShow graph to >> pump >> into a renderer that transmits out to a network, then another graph to >> receive from the network and display it. >> You can manage the network latency by adding a delays in the local >> graph. > > Thanks Tim, you're correct about the math. What is your main point about > DirectShow: that it is generally faster and more reliable than doing the > job high-level, or that one could use coding/decoding in DirectShow to > speed up the transmission? I think the latter would be a great idea if > the latency were tolerable. On the other hand, I'd like to keep things > simple and do all the programming in Python. I've got no experience with > DirectShow, but I guess the filters need to be programmed in C++ and > called from Python? > > Another option might be to use resolution 320x240 at 15fps. Does the webcam just deliver frames, or are you getting frames out of a decoder layer? If it's the latter, you want to distribute the encoded video, which should be much lower bandwidth. Exactly how you do that depends a bit on what format the webcam claims to deliver. -- Rhodri James *-* Wildebeest Herder to the Masses From jeanmichel at sequans.com Mon Jul 6 11:24:53 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 06 Jul 2009 17:24:53 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <000901c9fe42$b026cfc0$0d00a8c0@Hendrik> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <006201c9fd48$666d94e0$0d00a8c0@Hendrik> <4A51F3EB.1080509@sequans.com> <000901c9fe42$b026cfc0$0d00a8c0@Hendrik> Message-ID: <4A521745.8040307@sequans.com> Hendrik van Rooyen wrote: > "Jean-Michel Pichavant" wrote: > > >> Woot ! I'll keep this one in my mind, while I may not be that concerned >> by speed unlike the OP, I still find this way of doing very simple and >> so intuitive (one will successfully argue how I was not figuring this >> out by myself if it was so intuitive). >> Anyway I wanted to participated to this thread, as soon as I saw 'due to >> python limitations' in the title, I foretold a hell of a thread ! This >> is just provocation ! :-) >> > > The OP was not being provocative - he has a real problem, I was just kidding, asserting for python limitations in this list guarantees that the thread will last for several days, whether or not the assertion is right. JM From python-url at phaseit.net Mon Jul 6 11:33:47 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Mon, 6 Jul 2009 15:33:47 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 6) Message-ID: QOTW: "Simulating a shell with hooks on its I/O should be so complicated that a 'script kiddie' has trouble writing a Trojan." - Scott David Daniels http://groups.google.com/group/comp.lang.python/msg/1c0f70d5fc69b5aa Python 3.1 final was released last week - congratulations! http://groups.google.com/group/comp.lang.python/browse_thread/thread/b37a041ce01d4168/ Clarity of code vs. reusability in an introductory course: http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1a6ca84d3bfc483/ A piece of code analyzed, looking for ways to improve speed: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a420417a1e215cf0/ Beginner question: How to define an indeterminate number of variables? How to create objects, and later reference them, when one doesn't know how many of them will be needed? http://groups.google.com/group/comp.lang.python/browse_thread/thread/c40e3c0c843ce6fb/ Simple things made simple: correct use of Unicode helps a lot in this case: http://groups.google.com/group/comp.lang.python/browse_thread/thread/9cbb01aacf23fa8f/ super() and multiple inheritance: __init__ is hard to get right. Also, Carl Banks explains how mixin classes are best used: http://groups.google.com/group/comp.lang.python/browse_thread/thread/c934715ac83dfbd/ iter(function, sentinel) is a handy way to iterate in some cases, but beware of the fine print: http://groups.google.com/group/comp.lang.python/browse_thread/thread/b977c3b39abd98b0 The testing code should not merely repeat the code being tested (this fact isn't obvious to everyone): http://groups.google.com/group/comp.lang.python/browse_thread/thread/8978c54b83f80bd6/ Organize code so it can be run both in the source tree and after being installed: http://groups.google.com/group/comp.lang.python/browse_thread/thread/62284434d2ca5e1f/ Mark Dickinson explains the C implementation of long integers: http://groups.google.com/group/comp.lang.python/browse_thread/thread/aa1d06d371658135/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From digitig at gmail.com Mon Jul 6 11:43:43 2009 From: digitig at gmail.com (Tim Rowe) Date: Mon, 6 Jul 2009 16:43:43 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: 2009/7/4 kj : > Precisely. ?As I've stated elsewhere, this is an internal helper > function, to be called only a few times under very well-specified > conditions. ?The assert statements checks that these conditions > are as intended. ?I.e. they are checks against the module writer's > programming errors. Good for you. I'm convinced that you have used the assertion appropriately, and the fact that so many here are unable to see that looks to me like a good case for teaching the right use of assertions. For what it's worth, I read assertions at the beginning of a procedure as part of the specification of the procedure, and I use them there in order to document the procedure. An assertion in that position is for me a statement to the user of the procedure "it's your responsibility to make sure that you never call this procedure in such a way as to violate these conditions". They're part of a contract, as somebody (maybe you) pointed out. As somebody who works in the safety-critical domain, it's refreshing to see somebody teaching students to think about the circumstances in which a procedure can legitimately be called. The hostility you've received to that idea is saddening, and indicative of why there's so much buggy software out there. -- Tim Rowe From dmhouse at gmail.com Mon Jul 6 11:46:45 2009 From: dmhouse at gmail.com (David House) Date: Mon, 6 Jul 2009 16:46:45 +0100 Subject: try -> except -> else -> except? Message-ID: Hi all, I'm looking for some structure advice. I'm writing something that currently looks like the following: try: except KeyError: else: This is working fine. However, I now want to add a call to a function in the `else' part that may raise an exception, say a ValueError. So I was hoping to do something like the following: try: except KeyError: else: except ValueError: However, this isn't allowed in Python. An obvious way round this is to move the `else' clause into the `try', i.e., try: except KeyError: except ValueError: However, I am loath to do this, for two reasons: (i) if I modify the block at some point in the future so that it may raise a KeyError, I have to somehow tell this exception from the one that may be generated from the line. (ii) it moves the error handler for the bit miles away from the line that might generate the error, making it unclear which code the KeyError error handler is an error handler for. What would be the best way to structure this? -- -David From piet at cs.uu.nl Mon Jul 6 11:51:02 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Jul 2009 17:51:02 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: Sorry, there was an error in the sieve in my last example. Here is a corrected version: D = {9: 6} # contains composite numbers Dlist = [2, 3] # list of already generated primes def sieve(): '''generator that yields all prime numbers''' global D global Dlist for q in Dlist: yield q while True: q += 2 p = D.pop(q, 0) if p: x = q + p while x in D: x += p D[x] = p else: Dlist.append(q) D[q*q] = 2*q yield q -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 6 11:56:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 06 Jul 2009 17:56:21 +0200 Subject: try -> except -> else -> except? References: Message-ID: >>>>> David House (DH) wrote: >DH> Hi all, >DH> I'm looking for some structure advice. I'm writing something that >DH> currently looks like the following: >DH> try: >DH> >DH> except KeyError: >DH> >DH> else: >DH> >DH> This is working fine. However, I now want to add a call to a function >DH> in the `else' part that may raise an exception, say a ValueError. So I >DH> was hoping to do something like the following: >DH> try: >DH> >DH> except KeyError: >DH> >DH> else: >DH> >DH> except ValueError: >DH> >DH> However, this isn't allowed in Python. >DH> An obvious way round this is to move the `else' clause into the `try', i.e., >DH> try: >DH> >DH> >DH> except KeyError: >DH> >DH> except ValueError: >DH> >DH> However, I am loath to do this, for two reasons: >DH> (i) if I modify the block at some point in >DH> the future so that it may raise a KeyError, I have to somehow tell >DH> this exception from the one that may be generated from the DH> amount of code that may raise a KeyError> line. >DH> (ii) it moves the error handler for the DH> raise a KeyError> bit miles away from the line that might generate the >DH> error, making it unclear which code the KeyError error handler is an >DH> error handler for. >DH> What would be the best way to structure this? try: except KeyError: else: try: except ValueError: -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at rgbaz.eu Mon Jul 6 12:02:18 2009 From: python at rgbaz.eu (Python) Date: Mon, 6 Jul 2009 18:02:18 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: On 6 jul 2009, at 17:46, David House wrote: > Hi all, > > I'm looking for some structure advice. I'm writing something that > currently looks like the following: > > try: > > except KeyError: > > else: > > > This is working fine. However, I now want to add a call to a function > in the `else' part that may raise an exception, say a ValueError. So I > was hoping to do something like the following: > > try: > > except KeyError: > > else: > > except ValueError: > > > However, this isn't allowed in Python. > > An obvious way round this is to move the `else' clause into the > `try', i.e., > > try: > > > except KeyError: > > except ValueError: > > > However, I am loath to do this, for two reasons: > > (i) if I modify the block at some point in > the future so that it may raise a KeyError, I have to somehow tell > this exception from the one that may be generated from the amount of code that may raise a KeyError> line. > (ii) it moves the error handler for the raise a KeyError> bit miles away from the line that might generate the > error, making it unclear which code the KeyError error handler is an > error handler for. > > What would be the best way to structure this? > > -- > -David > as far as I know try has no 'else' it's 'finally' try: a except: b except: c finally: d gr Arno From deets at nospam.web.de Mon Jul 6 12:10:36 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Jul 2009 18:10:36 +0200 Subject: How to map size_t using ctypes? References: Message-ID: <7belo1F21fs3vU1@mid.uni-berlin.de> Philip Semanchuk wrote: > Hi all, > I can't figure out how to map a C variable of size_t via Python's > ctypes module. Let's say I have a C function like this: > > void populate_big_array(double *the_array, size_t element_count) {...} > > How would I pass parameter 2? A long (or ulong) will (probably) work > (on most platforms), but I like my code to be more robust than that. > Furthermore, this is scientific code and it's entirely possible that > someone will want to pass a huge array with more elements than can be > described by a 32-bit long. from ctypes import c_size_t doesn't work for you? On my system, it's aliased to c_ulong, but I guess that's depending on the platfrom of course. Diez From dmhouse at gmail.com Mon Jul 6 12:14:23 2009 From: dmhouse at gmail.com (David House) Date: Mon, 6 Jul 2009 17:14:23 +0100 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: 2009/7/6 Python : > as far as I know try has no 'else' It does: http://docs.python.org/reference/compound_stmts.html#the-try-statement > it's 'finally' There is a `finally', too, but they are semantically different. See the above link. -- -David From python at rgbaz.eu Mon Jul 6 12:17:24 2009 From: python at rgbaz.eu (Python) Date: Mon, 6 Jul 2009 18:17:24 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: <1661ED1F-8AC9-48A7-8C87-0090942664B1@rgbaz.eu> On 6 jul 2009, at 18:14, David House wrote: > 2009/7/6 Python : >> as far as I know try has no 'else' > > It does: > http://docs.python.org/reference/compound_stmts.html#the-try-statement > >> it's 'finally' > > There is a `finally', too, but they are semantically different. See > the above link. > > -- > -David > > ah yeah you;re right, sry shouldn't the else statement come after the except ones maybe? From usernet at ilthio.net Mon Jul 6 12:25:33 2009 From: usernet at ilthio.net (Tim Harig) Date: Mon, 06 Jul 2009 16:25:33 GMT Subject: generation of keyboard events References: Message-ID: <1Ap4m.2113$8r.633@nlpi064.nbdc.sbc.com> On 2009-07-06, RAM wrote: > I am trying to do this on windows. My program(executable) has been > written in VC++ and when I run this program, I need to click on one > button on the program GUI i,e just I am entering "Enter key" on the > key board. But this needs manual process. So i need to write a python > script which invokes my program and pass "Enter key" event to my > program so that it runs without manual intervention. This can be done using the WScript.WshShell.SendKeys() method when running Python with Windows Scripting Host by using the .pyw extension: http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx From philip at semanchuk.com Mon Jul 6 12:29:21 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 12:29:21 -0400 Subject: How to map size_t using ctypes? In-Reply-To: <7belo1F21fs3vU1@mid.uni-berlin.de> References: <7belo1F21fs3vU1@mid.uni-berlin.de> Message-ID: On Jul 6, 2009, at 12:10 PM, Diez B. Roggisch wrote: > Philip Semanchuk wrote: > >> Hi all, >> I can't figure out how to map a C variable of size_t via Python's >> ctypes module. Let's say I have a C function like this: >> >> void populate_big_array(double *the_array, size_t element_count) >> {...} >> >> How would I pass parameter 2? A long (or ulong) will (probably) work >> (on most platforms), but I like my code to be more robust than that. >> Furthermore, this is scientific code and it's entirely possible that >> someone will want to pass a huge array with more elements than can be >> described by a 32-bit long. > > from ctypes import c_size_t > > doesn't work for you? On my system, it's aliased to c_ulong, but I > guess > that's depending on the platfrom of course. D'oh! [slaps forehead] That will teach me to RTFM. In my 2.5 doc, it's not listed in the "Fundamental data types" section in the tutorial, but it is mentioned in "Fundamental data types" in the ctypes reference. You'd be surprised at the amount of Googling I did without learning this on my own. Thanks Philip From "sandrodll[remove]" at googlemail.com Mon Jul 6 12:32:47 2009 From: "sandrodll[remove]" at googlemail.com (gialloporpora) Date: Mon, 06 Jul 2009 18:32:47 +0200 Subject: Help to find a regular expression to parse po file In-Reply-To: References: <4A52084D.1010900@googlemail.com> Message-ID: <4A52272F.10209@googlemail.com> Risposta al messaggio di Hallvard B Furuseth : > > I don't know the syntax of a po file, but this works for the > snippet you posted: > > arg_re = r'"[^\\\"]*(?:\\.[^\\\"]*)*"' > arg_re = '%s(?:\s+%s)*' % (arg_re, arg_re) > find_re = re.compile( > r'^msgid\s+(' + arg_re + ')\s*\nmsgstr\s+(' + arg_re + ')\s*\n', re.M) > > However, can \ quote a newline? If so, replace \\. with \\[\s\S] or > something. > Can there be other keywords between msgid and msgstr? If so, > add something like (?:\w+\s+\s*\n)*? between them. > Can msgstr come before msgid? If so, forget using a single regexp. > Anything else to the syntax to look out for? Single quotes, maybe? > > Is it a problem if the regexp isn't quite right and doesn't match all > cases, yet doesn't report an error when that happens? > > All in all, it may be a bad idea to sqeeze this into a single regexp. > It gets ugly real fast. Might be better to parse the file in a more > regular way, maybe using regexps just to extract each (keyword, "value") > pair. > Thank you very much, Haldvard, it seem to works, there is a strange match in the file header but I could skip the first match. The po files have this structure: http://bit.ly/18qbVc msgid "string to translate" " second string to match" " n string to match" msgstr "translated sting" " second translated string" " n translated string" One or more new line before the next group. In past I have created a Python script to parse PO files where msgid and msgstr are in two sequential lines, for example: msgid "string to translate" msgstr "translated string" now the problem is how to match also (optional) string between msgid and msgstr. Sandro From davea at ieee.org Mon Jul 6 12:49:49 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 12:49:49 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A52089F.7020301@mrabarnett.plus.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> Message-ID: <4A522B2D.60407@ieee.org> MRAB wrote: >
Dave > Angel wrote: > [snip] >> It would probably save some time to not bother storing the zeroes in >> the list at all. And it should help if you were to step through a >> list of primes, rather than trying every possible int. Or at least >> constrain yourself to odd numbers (after the initial case of 2). >> > Or stop looking for more factors when you've passed the square root of > num. I don't know what effect there'll be on the time if you recalculate > the square root when num changes (expensive calculation vs smaller > search space). > >
> But if I remember the code, it stopped when the quotient is one, which is usually sooner than the square root. And no need to precalculate the square root. From Scott.Daniels at Acm.Org Mon Jul 6 12:53:34 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 09:53:34 -0700 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: Piet van Oostrum wrote: >>>>>> Dave Angel (DA) wrote: > >> DA> It would probably save some time to not bother storing the zeroes in the >> DA> list at all. And it should help if you were to step through a list of >> DA> primes, rather than trying every possible int. Or at least constrain >> DA> yourself to odd numbers (after the initial case of 2). > > ... > # Based upon http://code.activestate.com/recipes/117119/ > > D = {9: 6} # contains composite numbers XXX Dlist = [2, 3] # list of already generated primes Elist = [(2, 4), (3, 9)] # list of primes and their squares > XXX def sieve(): XXX '''generator that yields all prime numbers''' XXX global D XXX global Dlist def sieve2(): '''generator that yields all primes and their squares''' # No need for global declarations, we alter, not replace XXX for p in Dlist: XXX yield p XXX q = Dlist[-1]+2 for pair in Elist: yield pair q = pair[0] + 2 > while True: > if q in D: > p = D[q] > x = q + p > while x in D: x += p > D[x] = p > else: XXX Dlist.append(q) XXX yield q XXX D[q*q] = 2*q square = q * q pair = q, square Elist.append(pair) yield pair D[square] = 2 * q > q += 2 > > def factorise(num): > """Returns a list of prime factor powers. For example: > factorise(6) will return > [2, 2] (the powers are returned one higher than the actual value) > as in, 2^1 * 3^1 = 6.""" > powers = [] > power = 0 XXX for factor in sieve(): for factor, limit in sieve2(): > power = 0 > while num % factor == 0: > power += 1 > num /= factor XXX if power > 0: if power: # good enough here, and faster > # if you really want the factors then append((factor, power)) > powers.append(power+1) XXX if num == 1: XXX break XXX return powers if num < limit: if num > 1: # if you really want the factors then append((num, 1)) powers.append(2) return powers OK, that's a straightforward speedup, _but_: factorize(6) == [2, 2] == factorize(10) == factorize(15) So I am not sure exactly what you are calculating. --Scott David Daniels Scott.Daniels at Acm.Org From davea at ieee.org Mon Jul 6 12:54:35 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 12:54:35 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A522C4B.20102@ieee.org> Rhodri James wrote: >
On Mon, > 06 Jul 2009 10:58:21 +0100, Steven D'Aprano > wrote: > >> On Mon, 06 Jul 2009 02:19:51 -0300, Gabriel Genellina wrote: >> >>> En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano >>> escribi?: >>>> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >>>> >>>>> I wonder how many people have been tripped up by the fact that >>>>> >>>>> ++n >>>>> >>>>> and >>>>> >>>>> --n >>>>> >>>>> fail silently for numeric-valued n. >>>> >>>> What do you mean, "fail silently"? They do exactly what you should >>>> expect: >>>>>>> ++5 # positive of a positive number is positive >>>> >>>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>>> that are buggy, not Python. >>> >>> Well, those expectations are taken seriously when new features are >>> introduced into the language - and sometimes the feature is dismissed >>> just because it would be confusing for some. If a += 1 works, expecting >>> ++a to have the same meaning is very reasonable (for those coming from >>> languages with a ++ operator, like C or Java) - more when ++a is a >>> perfectly valid expression. If this issue isn't listed under the >>> various >>> "Python gotchas" articles, it should... >> >> The fact that it isn't suggests strongly to me that it isn't that common >> a surprise even for Java and C programmers. This is the first time I've >> seen anyone raise it as an issue. > > Indeed, arguably it's a bug for C compilers to fail to find the valid > parsing of "++5" as "+(+5)". All I can say is that I've never even > accidentally typed that in twenty years of C programming. > But the C language specifically defines the tokenizer as doing a max-match, finding the longest legal token at any point. That's how many things that would otherwise be ambiguous are well-defined. For example, if you want to divide two integers, given pointers to them, you need a space between the slash and the start. *p1/*p2 begins a comment, while *p1/ *p2 does a division From davea at ieee.org Mon Jul 6 13:13:48 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 13:13:48 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: <4A5230CC.2020801@ieee.org> Scott David Daniels wrote: >
Piet van > Oostrum wrote: >>>>>>> Dave Angel (DA) wrote: >> >>> DA> It would probably save some time to not bother storing the >>> zeroes in the >>> DA> list at all. And it should help if you were to step through a >>> list of >>> DA> primes, rather than trying every possible int. Or at least >>> constrain >>> DA> yourself to odd numbers (after the initial case of 2). >> >> ... >> # Based upon http://code.activestate.com/recipes/117119/ >> >> D = {9: 6} # contains composite numbers > XXX Dlist = [2, 3] # list of already generated primes > Elist = [(2, 4), (3, 9)] # list of primes and their squares >> > XXX def sieve(): > XXX '''generator that yields all prime numbers''' > XXX global D > XXX global Dlist > def sieve2(): > '''generator that yields all primes and their squares''' > # No need for global declarations, we alter, not replace > XXX for p in Dlist: > XXX yield p > XXX q = Dlist[-1]+2 > > for pair in Elist: > yield pair > q = pair[0] + 2 > >> while True: >> if q in D: >> p = D[q] >> x = q + p >> while x in D: x += p >> D[x] = p >> else: > XXX Dlist.append(q) > XXX yield q > XXX D[q*q] = 2*q > square = q * q > pair = q, square > Elist.append(pair) > yield pair > D[square] = 2 * q >> q += 2 >> >> def factorise(num): >> """Returns a list of prime factor powers. For example: >> factorise(6) will return >> [2, 2] (the powers are returned one higher than the actual >> value) >> as in, 2^1 * 3^1 = 6.""" >> powers = [] >> power = 0 > XXX for factor in sieve(): > for factor, limit in sieve2(): >> power = 0 >> while num % factor == 0: >> power += 1 >> num /= factor > XXX if power > 0: > if power: # good enough here, and faster >> # if you really want the factors then append((factor, >> power)) >> powers.append(power+1) > XXX if num == 1: > XXX break > XXX return powers > if num < limit: > if num > 1: > # if you really want the factors then append((num, 1)) > powers.append(2) > return powers > > OK, that's a straightforward speedup, _but_: > factorize(6) == [2, 2] == factorize(10) == factorize(15) > So I am not sure exactly what you are calculating. > > > --Scott David Daniels > Scott.Daniels at Acm.Org > >
> The OP only needed the number of factors, not the actual factors. So the zeroes in the list are unneeded. 6, 10, and 15 each have 4 factors. From sajmikins at gmail.com Mon Jul 6 13:15:22 2009 From: sajmikins at gmail.com (Simon Forman) Date: Mon, 6 Jul 2009 13:15:22 -0400 Subject: Tree structure consuming lot of memory In-Reply-To: References: <50697b2c0907060303r6bad875dr1eb1ab2482f54e03@mail.gmail.com> Message-ID: <50f98a4c0907061015n68191f9bj868b8e1b8908223c@mail.gmail.com> On Mon, Jul 6, 2009 at 6:12 AM, mayank gupta wrote: > Thanks for the other possibilites. I would consider option (2) and (3) to > improve my code. > > But out of curiosity, I would still like to know why does an object of a > Python-class consume "so" much of memory (1.4 kb), and this memory usage has > nothing to do with its attributes. > > Thanks > > Regards. > > On Mon, Jul 6, 2009 at 12:03 PM, Chris Rebert wrote: >> >> On Mon, Jul 6, 2009 at 2:55 AM, mayank gupta wrote: >> > Hi, >> > >> > I am creating a tree data-structure in python; with nodes of the tree >> > created by a simple class : >> > >> > class Node : >> > ?????? def __init__(self , .... other attributes): >> > ????????????? # initialise the attributes here!! >> > >> > But the problem is I am working with a huge tree (millions of nodes); >> > and >> > each node is consuming much more memory than it should. After a little >> > analysis, I found out that in general it uses about 1.4 kb of memory for >> > each node!! >> > I will be grateful if someone could help me optimize the memory usage. >> >> (1) Use __slots__ (see >> http://docs.python.org/reference/datamodel.html#slots) >> (2) Use some data structure other than a tree >> (3) Rewrite your Node/Tree implementation in C >> >> Cheers, >> Chris >> -- >> http://blog.rebertia.com > For option 2 you should try using the built in types, list tuple or dict. You might get better results. I'm curious too as to why the class/instance code should take so much memory, could you mention more about the code? From python at mrabarnett.plus.com Mon Jul 6 13:20:59 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 18:20:59 +0100 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A522B2D.60407@ieee.org> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> <4A522B2D.60407@ieee.org> Message-ID: <4A52327B.3000403@mrabarnett.plus.com> Dave Angel wrote: > MRAB wrote: >>
Dave >> Angel wrote: >> [snip] >>> It would probably save some time to not bother storing the zeroes in >>> the list at all. And it should help if you were to step through a >>> list of primes, rather than trying every possible int. Or at least >>> constrain yourself to odd numbers (after the initial case of 2). >>> >> Or stop looking for more factors when you've passed the square root of >> num. I don't know what effect there'll be on the time if you recalculate >> the square root when num changes (expensive calculation vs smaller >> search space). >> >>
>> > But if I remember the code, it stopped when the quotient is one, which > is usually sooner than the square root. And no need to precalculate the > square root. > If the number is a large prime then the code will try all the numbers up to that, eg if num == 1000003 then it'll try 2..1000003 even though it could've given up after 1000. From pescadero10 at gmail.com Mon Jul 6 13:30:41 2009 From: pescadero10 at gmail.com (pescadero10) Date: Mon, 6 Jul 2009 10:30:41 -0700 (PDT) Subject: updating, adding new pages to confluence remotely, using python Message-ID: Hello, I am new to python and have been trying to figure out how to remotely add new pages to my confluence wiki space. I'm running my python script from a linux rhel4 machine and using confluence version 2.10. As a test I tried to read from stdin and write a page but it fails- that is, the script runs without errors but nothing is added. Does anyone have an example of how this is done? Here is my script: --- begin script ----------- #!/usr/local/bin/python # # Reads from standard input, dumps it onto a Confluence page # You'll need to modify the URL/username/password/spacekey/page title # below, because I'm too lazy to bother with argv. import sys from xmlrpclib import Server # Read the text of the page from standard input content = sys.stdin.read() s = Server("http://confluence.slac.stanford.edu/display/GO/Home") token = s.confluence1.login("chee", "******") page = s.confluence1.getPage(token, "SPACEKEY", "TEST Python-2- Confluence") page["content"] = content s.confluence1.storePage(token, page) newpagedata = {"title":"New Page","content":"new content","space":"spaceKey"} newpage = s.confluence1.storePage(token, newpagedata); ------------ end script ------------------------ Any help would be greatly appreciated. thanks, Pescadero10 From nomail at thank.you Mon Jul 6 13:41:03 2009 From: nomail at thank.you (jack catcher (nick)) Date: Mon, 06 Jul 2009 20:41:03 +0300 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: Rhodri James kirjoitti: > On Mon, 06 Jul 2009 07:10:38 +0100, jack catcher (nick) > wrote: > >> Tim Roberts kirjoitti: >>> "jack catcher (nick)" wrote: >>>> I'm thinking of using Python for capturing and showing live webcam >>>> stream simultaneously between two computers via local area network. >>>> Operating system is Windows. I'm going to begin with VideoCapture >>>> extension, no ideas about other implementation yet. Do you have any >>>> suggestions on how short delay I should hope to achieve in showing >>>> the video? This would be part of a psychological experiment, so I >>>> would need to deliver the video stream with a reasonable delay (say, >>>> below 100ms). >>> You need to do the math on this. Remember that a full 640x480 RGB >>> stream >>> at 30 frames per second runs 28 megabytes per second. That's more than >>> twice what a 100 megabit network can pump. >>> You can probably use Python to oversee this, but you might want to >>> consider >>> using lower-level code to control the actual hardware. If you are >>> targeting Windows, for example, you could write a DirectShow graph to >>> pump >>> into a renderer that transmits out to a network, then another graph to >>> receive from the network and display it. >>> You can manage the network latency by adding a delays in the local >>> graph. >> >> Thanks Tim, you're correct about the math. What is your main point >> about DirectShow: that it is generally faster and more reliable than >> doing the job high-level, or that one could use coding/decoding in >> DirectShow to speed up the transmission? I think the latter would be a >> great idea if the latency were tolerable. On the other hand, I'd like >> to keep things simple and do all the programming in Python. I've got >> no experience with DirectShow, but I guess the filters need to be >> programmed in C++ and called from Python? >> >> Another option might be to use resolution 320x240 at 15fps. > > Does the webcam just deliver frames, or are you getting frames out of > a decoder layer? If it's the latter, you want to distribute the encoded > video, which should be much lower bandwidth. Exactly how you do that > depends a bit on what format the webcam claims to deliver. Well, getting already encoded video from the webcam sounds almost like a free lunch (which it probably is not). At least I wouldn't want to get too long a delay because of the encoding. I haven't got the webcam(s) yet, and I guess I can basically purchase any ones I find suitable for getting the job done. Any recommendations? From "sandrodll[remove]" at googlemail.com Mon Jul 6 13:42:46 2009 From: "sandrodll[remove]" at googlemail.com (gialloporpora) Date: Mon, 06 Jul 2009 19:42:46 +0200 Subject: Help to find a regular expression to parse po file In-Reply-To: References: <4A52084D.1010900@googlemail.com> Message-ID: <4A523796.3050509@googlemail.com> Risposta al messaggio di MRAB : > gialloporpora wrote: >> Hi all, >> I would like to extract string from a PO file. To do this I have created >> a little python function to parse po file and extract string: >> >> import re >> regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") >> m=r.findall(s) >> >> where s is a po file like this: >> >> msgctxt "write ubiquity commands.description" >> msgid "Takes you to the Ubiquity> href=\"chrome://ubiquity/content/editor.html\">command editor page." >> msgstr "Apre l'editor >> dei comandi di Ubiquity." >> >> >> #. list ubiquity commands command: >> #. use | to separate multiple name values: >> msgctxt "list ubiquity commands.names" >> msgid "list ubiquity commands" >> msgstr "elenco comandi disponibili" >> >> msgctxt "list ubiquity commands.description" >> msgid "Opensthe >> list\n" >> " of all Ubiquity commands available and what they all do." >> msgstr "Apre una> href=\"chrome://ubiquity/content/cmdlist.html\">pagina\n" >> " in cui sono elencati tutti i comandi disponibili e per ognuno >> viene spiegato in breve a cosa serve." >> >> >> >> #. change ubiquity settings command: >> #. use | to separate multiple name values: >> msgctxt "change ubiquity settings.names" >> msgid "change ubiquity settings|change ubiquity preferences|change >> ubiquity skin" >> msgstr "modifica impostazioni di ubiquity|modifica preferenze di >> ubiquity|modifica tema di ubiquity" >> >> msgctxt "change ubiquity settings.description" >> msgid "Takes you to the> href=\"chrome://ubiquity/content/settings.html\">settings page,\n" >> " where you can change your skin, key combinations, etc." >> msgstr "Apre la pagina> href=\"chrome://ubiquity/content/settings.html\">delle impostazioni >> di Ubiquity,\n" >> " dalla quale ?? possibile modificare la combinazione da tastiera >> utilizzata per richiamare Ubiquity, il tema, ecc." >> >> >> >> but, obviusly, with the code above the last string is not matched. If >> I use re.DOTALL to match also new line character it not works because it >> match the entire file, I would like to stop the matching when "msgstr" >> is found. >> >> regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n\\n",re.DOTALL) >> >> is it possible or not ? >> > You could try: > > regex = re.compile(r"msgid (.*(?:\n".*")*)\nmsgstr (.*(?:\n".*")*)$") > > and then, if necessary, tidy what you get. MRAB, thank you for your help, I have tried the code posted by Hallvard because I have seen it before and it works. Now I'll check also your suggestions. Sandro -- *Pink Floyd ? The Great Gig in the Sky* - http://sn.im/kggo7 * FAQ* di /it-alt.comp.software.mozilla/: http://bit.ly/1MZ04d From bdesth.quelquechose at free.quelquepart.fr Mon Jul 6 13:48:29 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Mon, 06 Jul 2009 19:48:29 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: <4a52548c$0$31414$426a74cc@news.free.fr> David House a ?crit : > Hi all, > > I'm looking for some structure advice. I'm writing something that > currently looks like the following: > > try: > > except KeyError: > > else: > > > This is working fine. However, I now want to add a call to a function > in the `else' part that may raise an exception, say a ValueError. If your error handler terminates the function (which is usually the case when using the else clause), you can just skip the else statement, ie: try: except KeyError: Then adding one or more try/except is just trivial. > So I > was hoping to do something like the following: > > try: > > except KeyError: > > else: > > except ValueError: > > > However, this isn't allowed in Python. Nope. But this is legal: try: except KeyError: else: try: except ValueError: > An obvious way round this is to move the `else' clause into the `try' "obvious" but not necessarily the best thing to do. (snip - cf above for simple answers) From bdesth.quelquechose at free.quelquepart.fr Mon Jul 6 13:49:49 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Mon, 06 Jul 2009 19:49:49 +0200 Subject: try -> except -> else -> except? In-Reply-To: References: Message-ID: <4a5254db$0$31414$426a74cc@news.free.fr> Python a ?crit : (snip whole OP) > as far as I know try has no 'else' Then you may want to RTFM. From ethan at stoneleaf.us Mon Jul 6 13:53:31 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 06 Jul 2009 10:53:31 -0700 Subject: regex question on .findall and \b In-Reply-To: <4A4CE2A0.5040407@stoneleaf.us> References: <4A4CE2A0.5040407@stoneleaf.us> Message-ID: <4A523A1B.70901@stoneleaf.us> Many thanks to all who replied! And, yes, I will *definitely* use raw strings from now on. :) ~Ethan~ From bdesth.quelquechose at free.quelquepart.fr Mon Jul 6 13:56:00 2009 From: bdesth.quelquechose at free.quelquepart.fr (Bruno Desthuilliers) Date: Mon, 06 Jul 2009 19:56:00 +0200 Subject: How Python Implements "long integer"? In-Reply-To: <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <68c143c9-10b2-4762-9214-f8bfb0b7a9de@37g2000yqp.googlegroups.com> <249d9248-ae81-4f51-b734-24e92795aadf@b15g2000yqd.googlegroups.com> Message-ID: <4a52564e$0$31414$426a74cc@news.free.fr> Mark Dickinson a ?crit : > On Jul 5, 1:09 pm, Pedram wrote: >> Thanks for reply, >> Sorry I can't explain too clear! I'm not English ;) > > That's shocking. Everyone should be English. :-) > Mark, tu sors ! From davea at ieee.org Mon Jul 6 14:28:05 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 14:28:05 -0400 Subject: Why is my code faster with append() in a loop than with a large list? In-Reply-To: <4A52327B.3000403@mrabarnett.plus.com> References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> <4A522B2D.60407@ieee.org> <4A52327B.3000403@mrabarnett.plus.com> Message-ID: <4A524235.1010002@ieee.org> MRAB wrote: >
Dave > Angel wrote: >> MRAB wrote: >>>
Dave >>> Angel wrote: >>> [snip] >>>> It would probably save some time to not bother storing the zeroes >>>> in the list at all. And it should help if you were to step through >>>> a list of primes, rather than trying every possible int. Or at >>>> least constrain yourself to odd numbers (after the initial case of 2). >>>> >>> Or stop looking for more factors when you've passed the square root of >>> num. I don't know what effect there'll be on the time if you >>> recalculate >>> the square root when num changes (expensive calculation vs smaller >>> search space). >>> >>>
>>> >> But if I remember the code, it stopped when the quotient is one, >> which is usually sooner than the square root. And no need to >> precalculate the square root. >> > If the number is a large prime then the code will try all the numbers up > to that, eg if num == 1000003 then it'll try 2..1000003 even though it > could've given up after 1000. > >
> That's one reason I suggested (and Piet implemented) a sieve. You can stop dividing when the square of the next prime exceeds your quotient. From tn.pablo at gmail.com Mon Jul 6 14:49:56 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Mon, 6 Jul 2009 13:49:56 -0500 Subject: A Bug By Any Other Name ... In-Reply-To: <00df01c9fe33$10c36d80$0d00a8c0@Hendrik> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <00df01c9fe33$10c36d80$0d00a8c0@Hendrik> Message-ID: On Mon, Jul 6, 2009 at 07:12, Hendrik van Rooyen wrote: > "Terry Reedy" wrote: > >> Gabriel Genellina wrote: >> > >> > In this case, a note in the documentation warning about the potential >> > confusion would be fine. >> >> How would that help someone who does not read the doc? > > It obviously won't. > > All it will do, is that it will enable people on this group, > who may read the manual, to tell people who complain, > to RTFM. Yes, it's their problem if they don't read the docs. > > ?I agree that it would be a good idea to make it an > error, or a warning - "this might not do what you > think it does", or an "are you sure?" exception. > > ?:-) > > - Hendrik That would be even harder than adding a line to the docs. Besides, the problem that Mr. alex23 pointed: "where do you stop?" would really get out of hand. -- Pablo Torres N. From jdnier at gmail.com Mon Jul 6 14:52:26 2009 From: jdnier at gmail.com (David Niergarth) Date: Mon, 6 Jul 2009 11:52:26 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: Message-ID: I remember in college taking an intro programming class (C++) where the professor started us off writing a program to factor polynomials; he probably also incorporated binary search into an assignment. But people don't generally use Python to implement binary search or factor polynomials so maybe you should start with a problem more germane to typical novice users (and less algorithm-y). Wouldn't starting them off with string processing or simple calculations be a practical way to get comfortable with the language? --David On Jul 3, 9:05?am, kj wrote: > I'm will be teaching a programming class to novices, and I've run > into a clear conflict between two of the principles I'd like to > teach: code clarity vs. code reuse. ?I'd love your opinion about > it. > > The context is the concept of a binary search. ?In one of their > homeworks, my students will have two occasions to use a binary > search. ?This seemed like a perfect opportunity to illustrate the > idea of abstracting commonalities of code into a re-usable function. > So I thought that I'd code a helper function, called _binary_search, > that took five parameters: a lower limit, an upper limit, a > one-parameter function, a target value, and a tolerance (epsilon). > It returns the value of the parameter for which the value of the > passed function is within the tolerance of the target value. > > This seemed straightforward enough, until I realized that, to be > useful to my students in their homework, this _binary_search function > had to handle the case in which the passed function was monotonically > decreasing in the specified interval... > > The implementation is still very simple, but maybe not very clear, > particularly to programming novices (docstring omitted): > > def _binary_search(lo, hi, func, target, epsilon): > ? ? assert lo < hi > ? ? assert epsilon > 0 > ? ? sense = cmp(func(hi), func(lo)) > ? ? if sense == 0: > ? ? ? ? return None > ? ? target_plus = sense * target + epsilon > ? ? target_minus = sense * target - epsilon > ? ? while True: > ? ? ? ? param = (lo + hi) * 0.5 > ? ? ? ? value = sense * func(param) > ? ? ? ? if value > target_plus: > ? ? ? ? ? ? hi = param > ? ? ? ? elif value < target_minus: > ? ? ? ? ? ? lo = param > ? ? ? ? else: > ? ? ? ? ? ? return param > > ? ? ? ? if lo == hi: > ? ? ? ? ? ? return None > > My question is: is the business with sense and cmp too "clever"? > > Here's the rub: the code above is more general (hence more reusable) > by virtue of this trick with the sense parameter, but it is also > a bit harder to understand. > > This not an unusual situation. ?I find that the processing of > abstracting out common logic often results in code that is harder > to read, at least for the uninitiated... > > I'd love to know your opinions on this. > > TIA! > > kj From nobody at nowhere.com Mon Jul 6 15:41:53 2009 From: nobody at nowhere.com (Nobody) Date: Mon, 06 Jul 2009 20:41:53 +0100 Subject: Python and webcam capture delay? References: Message-ID: On Mon, 06 Jul 2009 20:41:03 +0300, jack catcher (nick) wrote: >> Does the webcam just deliver frames, or are you getting frames out of >> a decoder layer? If it's the latter, you want to distribute the encoded >> video, which should be much lower bandwidth. Exactly how you do that >> depends a bit on what format the webcam claims to deliver. > > Well, getting already encoded video from the webcam sounds almost like a > free lunch (which it probably is not). At least I wouldn't want to get > too long a delay because of the encoding. > > I haven't got the webcam(s) yet, and I guess I can basically purchase > any ones I find suitable for getting the job done. Any recommendations? The webcam is bound to do some encoding; most of them use USB "full speed" (12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. Chroma subsampling and JPEG compression will both reduce the bandwidth without introducing noticable latency (the compression time will be no more than one frame). Temporal compression will reduce the bandwidth further. Using B-frames (frames which contain the differences from a predicted frame which is based upon both previous and subsequent frames) will provide more compression, but increases the latency significantly. For this reason, the compression built into video cameras normally only uses P-frames (frames which contain the differences from a predicted frame which is based only upon previous frames). The biggest issue is likely to be finding latency specifications, followed by finding a camera which meets your latency requirement. Also, any "read frame, write frame" loops will add an additional frame of latency, as you can't start writing the first byte of the frame until after you've read the last byte of the frame. Video APIs which let you get rows as they're decoded are rare. From sisibley at gmail.com Mon Jul 6 15:43:40 2009 From: sisibley at gmail.com (Scott Sibley) Date: Mon, 6 Jul 2009 14:43:40 -0500 Subject: Ctypes to wrap libusb-1.0 Message-ID: I have been having issues trying to wrap libusb-1.0 with ctypes. Actually, there's not much of an issue if I keep everything synchronous, but I need this to be asynchronous and that is where my problem arises. Please refer to the following link on Stackoverflow for a full overview of the issue. http://stackoverflow.com/questions/1060305/usb-sync-vs-async-vs-semi-async-partially-answered-now As mentioned in the question on Stackoverflow, synchronous transfers work great. I wrote an asynchronous C version that works fine. usbmon's output suggests the transfers are making it through. libusb's debug output shows nothing out of the ordinary. I've also asked about this on the libusb mailing list. I've hesitated asking on the Python mailing list up till now. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Mon Jul 6 15:55:02 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 15:55:02 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: Mark Dickinson wrote: > On Jul 6, 3:32 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> I wonder how many people have been tripped up by the fact that >> >> ++n >> >> and >> >> --n >> >> fail silently for numeric-valued n. Rather few, it seems. > Recent python-ideas discussion on this subject: > > http://mail.python.org/pipermail/python-ideas/2009-March/003741.html To add to what I wrote in that thread: it is C, not Python, that is out of step with standard usage in math and most languages. --x = x; 1/1/x = x; non not a = a; inverse(inverse(f)) = f; etc. And ++x= +x = x corresponded to I(I(x)) = I(x) = x, where I is identity function. In C as high-level assembler, the inc and dec functions are written as -- and ++ operators for some mix of the following reasons. 1) They correspond to machine language / assembler functions. 2) They need to be efficient since they are used in inner loops. Function calls have overhead. Original C did not allow inlining of function calls as best I remember. 3) They save keystrokes; 4) 2 versions that return pre and post change values are needed. My impression is that some opcode sets (and hence assemblers) have only one, and efficient code requires allowing direct access to whichever one a particular processor supports. Basic routines can usually be written either way. These reasons do not apply to Python or do not fit Python's style. Anyone who spends 15 minutes skimming the chapter on expressions could notice that 5.5. Unary arithmetic and bitwise operations has only +,-, and ~ or that the Summary operator table at the end has no -- or ++. Terry Jan Reedy From solipsis at pitrou.net Mon Jul 6 15:58:26 2009 From: solipsis at pitrou.net (Antoine Pitrou) Date: Mon, 6 Jul 2009 19:58:26 +0000 (UTC) Subject: Tree structure consuming lot of memory References: Message-ID: mayank gupta gmail.com> writes: > > After a little analysis, I found out that in general it uses about > 1.4 kb of memory for each node!! How did you measure memory use? Python objects are not very compact, but 1.4KB per object seems a bit too much (I would expect more about 150-200 bytes/object in 32-bit mode, or 300-400 bytes/object in 64-bit mode). One of the solutions is to use __slots__ as already suggested. Another, which will have similar benefits, is to use a namedtuple. Both suppress the instance dictionnary (`instance`.__dict__), which is a major contributor to memory consumption. Illustration (64-bit mode, by the way): >>> import sys >>> from collections import namedtuple # First a normal class >>> class Node(object): pass ... >>> o = Node() >>> o.value = 1 >>> o.children = () >>> >>> sys.getsizeof(o) 64 >>> sys.getsizeof(o.__dict__) 280 # The object seems to take a mere 64 bytes, but the attribute dictionnary # adds a whoppy 280 bytes and bumps actual size to 344 bytes! # Now a namedtuple (a tuple subclass with property accessors for the various # tuple items) >>> Node = namedtuple("Node", "value children") >>> >>> o = Node(value=1, children=()) >>> sys.getsizeof(o) 72 >>> sys.getsizeof(o.__dict__) Traceback (most recent call last): File "", line 1, in AttributeError: 'Node' object has no attribute '__dict__' # The object doesn't have a __dict__, so 72 bytes is its real total size. From tjreedy at udel.edu Mon Jul 6 16:27:25 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 16:27:25 -0400 Subject: updating, adding new pages to confluence remotely, using python In-Reply-To: References: Message-ID: pescadero10 wrote: > > I am new to python and have been trying to figure out how to remotely > add new pages to my confluence > wiki space. I'm running my python script from a linux rhel4 machine > and using confluence version 2.10. As a test I tried to read from > stdin and write a page but it fails- that is, the script runs without > errors but nothing is added. Does anyone have an example of how this > is done? Here is my script: > > --- begin script ----------- > #!/usr/local/bin/python > # > # Reads from standard input, dumps it onto a Confluence page > # You'll need to modify the URL/username/password/spacekey/page title > # below, because I'm too lazy to bother with argv. > > import sys > from xmlrpclib import Server > > # Read the text of the page from standard input > content = sys.stdin.read() > > s = Server("http://confluence.slac.stanford.edu/display/GO/Home") > token = s.confluence1.login("chee", "******") > page = s.confluence1.getPage(token, "SPACEKEY", "TEST Python-2- > Confluence") > page["content"] = content > s.confluence1.storePage(token, page) > > newpagedata = {"title":"New Page","content":"new > content","space":"spaceKey"} > newpage = s.confluence1.storePage(token, newpagedata); > ------------ end script ------------------------ > > Any help would be greatly appreciated. You neglected to specify Python version. As near as I can tell from 2.x docs, xmlrpclib has ServerProxy class but no Server class. Whoops, just saw "Server is retained as an alias for ServerProxy for backwards compatibility. New code should use ServerProxy". Good idea -- calling a client 'server' is confusing. Do you have access to logs on remote machine to see what was received? Or to a sysadmin? tjr From mooniitk at gmail.com Mon Jul 6 16:30:28 2009 From: mooniitk at gmail.com (mayank gupta) Date: Tue, 7 Jul 2009 02:00:28 +0530 Subject: Tree structure consuming lot of memory In-Reply-To: References: Message-ID: I worked out a small code which initializes about 1,000,000 nodes with some attributes, and saw the memory usage on my linux machine (using 'top' command). Then just later I averaged out the memory usage per node. I know this is not the most accurate way but just for estimated value. The kind of Node class I am working on in my original code is like : class Node: def __init__(self, #attributes ): self.coordinates = coordinates self.index = index self.sibNum = sibNum self.branchNum - branchNum #here 'coordinates' and 'index' are LISTS with length = "dimension", where "dimension" is a user-input. The most shocking part of it after the memory-analysis was that, the memory usage was never dependent on the "dimension". Yeah it varied a bit, but there wasnt any significant changes in the memory usage even when the "dimension" was doubled -- Any clues? Thank you for all your suggestions till this point. Regards. On Tue, Jul 7, 2009 at 1:28 AM, Antoine Pitrou wrote: > mayank gupta gmail.com> writes: > > > > After a little analysis, I found out that in general it uses about > > 1.4 kb of memory for each node!! > > How did you measure memory use? Python objects are not very compact, but > 1.4KB > per object seems a bit too much (I would expect more about 150-200 > bytes/object > in 32-bit mode, or 300-400 bytes/object in 64-bit mode). > > One of the solutions is to use __slots__ as already suggested. Another, > which > will have similar benefits, is to use a namedtuple. Both suppress the > instance > dictionnary (`instance`.__dict__), which is a major contributor to memory > consumption. Illustration (64-bit mode, by the way): > > >>> import sys > >>> from collections import namedtuple > > # First a normal class > >>> class Node(object): pass > ... > >>> o = Node() > >>> o.value = 1 > >>> o.children = () > >>> > >>> sys.getsizeof(o) > 64 > >>> sys.getsizeof(o.__dict__) > 280 > # The object seems to take a mere 64 bytes, but the attribute dictionnary > # adds a whoppy 280 bytes and bumps actual size to 344 bytes! > > # Now a namedtuple (a tuple subclass with property accessors for the > various > # tuple items) > >>> Node = namedtuple("Node", "value children") > >>> > >>> o = Node(value=1, children=()) > >>> sys.getsizeof(o) > 72 > >>> sys.getsizeof(o.__dict__) > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'Node' object has no attribute '__dict__' > > # The object doesn't have a __dict__, so 72 bytes is its real total size. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From thebrasse at gmail.com Mon Jul 6 16:36:59 2009 From: thebrasse at gmail.com (brasse) Date: Mon, 6 Jul 2009 13:36:59 -0700 (PDT) Subject: Cleaning up after failing to contructing objects Message-ID: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Hello! I have been thinking about how write exception safe constructors in Python. By exception safe I mean a constructor that does not leak resources when an exception is raised within it. The following is an example of one possible way to do it: class Foo(object): def __init__(self, name, fail=False): self.name = name if not fail: print '%s.__init__(%s)' % (self.__class__.__name__, self.name) else: print '%s.__init__(%s), FAIL' % (self.__class__.__name__, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) class Bar(object): def __init__(self): try: self.a = Foo('a') self.b = Foo('b', fail=True) except: self.close() def close(self): if hasattr(self, 'a'): self.a.close() if hasattr(self, 'b'): self.b.close() bar = Bar() As you can see this is less than straight forward. Is there some kind of best practice that I'm not aware of? :.:: mattias From no.email at please.post Mon Jul 6 16:40:49 2009 From: no.email at please.post (kj) Date: Mon, 6 Jul 2009 20:40:49 +0000 (UTC) Subject: Why re.match()? References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: >kj a ?crit : >(snipo >> To have a special-case >> re.match() method in addition to a general re.search() method is >> antithetical to language minimalism, >FWIW, Python has no pretention to minimalism. Assuming that you mean by this that Python's authors have no such pretensions: "There is real value in having a small language." Guido van Rossum, 2007.07.03 http://mail.python.org/pipermail/python-3000/2007-July/008663.html So there. BTW, that's just one example. I've seen similar sentiments expressed by Guido over and over and over: any new proposed enhancement to Python must be good enough in his mind to justify cluttering the language. That attitude counts as minimalism in my book. The best explanation I have found so far for re.match is that it is an unfortunate bit of legacy, something that would not be there if the design of Python did not have to be mindful of keeping old code chugging along... kj From joshua at joshuakugler.com Mon Jul 6 16:45:45 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Mon, 06 Jul 2009 12:45:45 -0800 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: Paul Moore wrote: > The SQLite documentation mentions a flag, SQLITE_OPEN_READONLY, to > open a database read only. I can't find any equivalent documented in > the Python standard library documentation for the sqlite3 module (or, > for that matter, on the pysqlite library's website). > > Is it possible to open a sqlite database in readonly mode, in Python? Yes, but most likely not with pysqlite. The python sqlite3 module in the standard library, and the pysqlite module are both DB-API compliant, which means they probably do not have a method to open the DB read only (as that is usually enforced at the user permission level). If you want to use that flag, take a look at APSW. It is a very thin layer on top of the SQLite C API, and thus supports everything that the C API supports. It is not, however, DB API compliant, but is very close so not hard to pick up. BTW, APSW is written by the same author as pysqlite. j From clp2 at rebertia.com Mon Jul 6 17:03:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 14:03:48 -0700 Subject: Tree structure consuming lot of memory In-Reply-To: References: Message-ID: <50697b2c0907061403s16c0602cp5d3767772e6e8ba@mail.gmail.com> > On Tue, Jul 7, 2009 at 1:28 AM, Antoine Pitrou wrote: >> >> mayank gupta gmail.com> writes: >> > >> > After a little analysis, I found out that in general it uses about >> > 1.4 kb of memory for each node!! >> >> How did you measure memory use? Python objects are not very compact, but >> 1.4KB >> per object seems a bit too much (I would expect more about 150-200 >> bytes/object >> in 32-bit mode, or 300-400 bytes/object in 64-bit mode). >> >> One of the solutions is to use __slots__ as already suggested. Another, >> which >> will have similar benefits, is to use a namedtuple. Both suppress the >> instance >> dictionnary (`instance`.__dict__), which is a major contributor to memory >> consumption. Illustration (64-bit mode, by the way): >> >> >>> import sys >> >>> from collections import namedtuple >> >> # First a normal class >> >>> class Node(object): pass >> ... >> >>> o = Node() >> >>> o.value = 1 >> >>> o.children = () >> >>> >> >>> sys.getsizeof(o) >> 64 >> >>> sys.getsizeof(o.__dict__) >> 280 >> # The object seems to take a mere 64 bytes, but the attribute dictionnary >> # adds a whoppy 280 bytes and bumps actual size to 344 bytes! >> >> # Now a namedtuple (a tuple subclass with property accessors for the >> various >> # tuple items) >> >>> Node = namedtuple("Node", "value children") >> >>> >> >>> o = Node(value=1, children=()) >> >>> sys.getsizeof(o) >> 72 >> >>> sys.getsizeof(o.__dict__) >> Traceback (most recent call last): >> ?File "", line 1, in >> AttributeError: 'Node' object has no attribute '__dict__' >> >> # The object doesn't have a __dict__, so 72 bytes is its real total size. On Mon, Jul 6, 2009 at 1:30 PM, mayank gupta wrote: > I worked out a small code which initializes about 1,000,000 nodes with some > attributes, and saw the memory usage on my linux machine (using 'top' > command). Then just later I averaged out the memory usage per node. I know > this is not the most accurate way but just for estimated value. You should try the more accurate sys.getsizeof() function: http://docs.python.org/library/sys.html#sys.getsizeof Cheers, Chris P.S. Please don't top-post in the future. -- http://blog.rebertia.com From deets at nospam.web.de Mon Jul 6 17:10:28 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 06 Jul 2009 23:10:28 +0200 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: <7bf7i4F2350o5U1@mid.uni-berlin.de> kj schrieb: > In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: > >> kj a ?crit : >> (snipo >>> To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, > >> FWIW, Python has no pretention to minimalism. > > Assuming that you mean by this that Python's authors have no such > pretensions: > > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html > > So there. > > BTW, that's just one example. I've seen similar sentiments expressed > by Guido over and over and over: any new proposed enhancement to > Python must be good enough in his mind to justify cluttering the > language. That attitude counts as minimalism in my book. > > The best explanation I have found so far for re.match is that it > is an unfortunate bit of legacy, something that would not be there > if the design of Python did not have to be mindful of keeping old > code chugging along... language != libraries. Diez From Scott.Daniels at Acm.Org Mon Jul 6 17:15:44 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 06 Jul 2009 14:15:44 -0700 Subject: Cleaning up after failing to contructing objects In-Reply-To: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: brasse wrote: > I have been thinking about how write exception safe constructors in > Python. By exception safe I mean a constructor that does not leak > resources when an exception is raised within it. ... > As you can see this is less than straight forward. Is there some kind > of best practice that I'm not aware of? Not so tough. Something like this tweaked version of your example: class Foo(object): def __init__(self, name, fail=False): self.name = name if not fail: print '%s.__init__(%s)' % (type(self).__name__, name) else: print '%s.__init__(%s), FAIL' % (type(self).__name__, name) raise ValueError('Asked to fail: %r' % fail) def close(self): print '%s.close(%s)' % (type(self).__name__, self.name) class Bar(object): def __init__(self): unwind = [] try: self.a = Foo('a') unwind.append(a) self.b = Foo('b', fail=True) unwind.append(b) ... except Exception, why: while unwind): unwind.pop().close() raise bar = Bar() --Scott David Daniels Scott.Daniels at Acm.Org From macaodha at gmail.com Mon Jul 6 17:36:37 2009 From: macaodha at gmail.com (Oisin) Date: Mon, 6 Jul 2009 14:36:37 -0700 (PDT) Subject: getting text from webpage that has embedded flash Message-ID: <8aaca352-e115-4aa8-b76d-a37a0edf7a74@h31g2000yqd.googlegroups.com> HI, Im trying to parse a bands myspace page and get the total number of plays for their songs. e.g. http://www.myspace.com/mybloodyvalentine The problem is that I cannot use urllib2 as the "Total plays" string does not appear in the page source. Any idea of ways around this? Thanks, O From michaelmossey at gmail.com Mon Jul 6 17:37:53 2009 From: michaelmossey at gmail.com (Michael Mossey) Date: Mon, 6 Jul 2009 14:37:53 -0700 (PDT) Subject: Catching control-C Message-ID: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> What is required in a python program to make sure it catches a control- c on the command-line? Do some i/o? The OS here is Linux. Thanks, Mike From clp2 at rebertia.com Mon Jul 6 17:44:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 14:44:20 -0700 Subject: Catching control-C In-Reply-To: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <50697b2c0907061444x1a361d47j7c3eeb610431a8ba@mail.gmail.com> On Mon, Jul 6, 2009 at 2:37 PM, Michael Mossey wrote: > What is required in a python program to make sure it catches a control- > c on the command-line? Do some i/o? The OS here is Linux. try: #code that reads input except KeyboardInterrupt: #Ctrl-C was pressed Cheers, Chris -- http://blog.rebertia.com From philip at semanchuk.com Mon Jul 6 17:47:57 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 17:47:57 -0400 Subject: Catching control-C In-Reply-To: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <928186A7-E76E-4BF3-8696-DC61ED025B99@semanchuk.com> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > What is required in a python program to make sure it catches a > control- > c on the command-line? Do some i/o? The OS here is Linux. You can use a try/except to catch a KeyboardInterrupt exception, or you can trap it using the signal module: http://docs.python.org/library/signal.html You want to trap SIGINT. HTH Philip From michaelmossey at gmail.com Mon Jul 6 18:02:26 2009 From: michaelmossey at gmail.com (Michael Mossey) Date: Mon, 6 Jul 2009 15:02:26 -0700 (PDT) Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 6, 2:47?pm, Philip Semanchuk wrote: > On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > > > What is required in a python program to make sure it catches a ? > > control- > > c on the command-line? Do some i/o? The OS here is Linux. > > You can use a try/except to catch a KeyboardInterrupt exception, or ? > you can trap it using the signal module:http://docs.python.org/library/signal.html > > You want to trap SIGINT. > > HTH > Philip Thanks to both of you. However, my question is also about whether I need to be doing i/o or some similar operation for my program to notice in any shape or form that Control-C has been pressed. In the past, I've written Python programs that go about their business ignoring Ctrl-C. Other programs respond to it immediately by exiting. I think the difference is that the latter programs are doing i/o. But I want to understand better what the "secret" is to responding to a ctrl-C in any shape or form. For example, does trapping SIGINT always work, regardless of what my process is doing? Thanks, Mike From nile_mcadams at yahoo.com Mon Jul 6 18:02:45 2009 From: nile_mcadams at yahoo.com (Nile) Date: Mon, 6 Jul 2009 15:02:45 -0700 (PDT) Subject: Semi-Newbie needs a little help Message-ID: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> I am trying to write a simple little program to do some elementary stock market analysis. I read lines, send each line to a function and then the function returns a date which serves as a key to a dictionary. Each time a date is returned I want to increment the value associated with that date. The function seems to be working properly. By means of a print statement I have inserted just before the return value I can see there are three dates that are returned which is correct. The dictionary only seems to capture the last date. My test data consists of five stocks, each stock with five days. The correct answer would be a count of 5 for the second day, the third day, and the last day -- 11/14/2008. Here is the a code, followed by a portion of the output. I know enough to write simple little programs like this with no problems up until now but I don't know enough to figure out what I am doing wrong. Code for x in range(len(file_list)): d = open(file_list[x] , "r") data = d.readlines() k = above_or_below(data) # This function seems to work correctly print "here is the value that was returned " , k dict[k] = dict.get(k,0) + 1 dict_list = dict.values() print "here is a list of the dictionary values ", dict_list print "the length of the dictionary is ", len(dict) And here is some output Function will return k which = 11/11/2008 # These 3 lines are printed from the function just before the return Function will return k which = 11/12/2008 # This sample shows stocks 4 and 5 but 1,2,3 are the same. Function will return k which = 11/14/2008 here is the value that was returned 11/14/2008 # printed from code above - only the last day seems to be Function will return k which = 11/11/2008 # recognized. Function will return k which = 11/12/2008 Function will return k which = 11/14/2008 here is the value that was returned 11/14/2008 here is a list of the dictionary values [5] # dict has counted only the last day for 5 stocks the length of the dictionary is 1 >Exit code: 0 From philip at semanchuk.com Mon Jul 6 18:16:06 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Mon, 6 Jul 2009 18:16:06 -0400 Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 6, 2009, at 6:02 PM, Michael Mossey wrote: > On Jul 6, 2:47 pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >>> What is required in a python program to make sure it catches a >>> control- >>> c on the command-line? Do some i/o? The OS here is Linux. >> >> You can use a try/except to catch a KeyboardInterrupt exception, or >> you can trap it using the signal module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH >> Philip > > Thanks to both of you. However, my question is also about whether I > need to be doing i/o or some similar operation for my program to > notice in any shape or form that Control-C has been pressed. In the > past, I've written Python programs that go about their business > ignoring Ctrl-C. Other programs respond to it immediately by exiting. > I think the difference is that the latter programs are doing i/o. But > I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. > > For example, does trapping SIGINT always work, regardless of what my > process is doing? Hi Mike, Sorry, I don't know the Python internals well enough to answer your question. Good luck Philip From clp2 at rebertia.com Mon Jul 6 18:22:54 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 15:22:54 -0700 Subject: Semi-Newbie needs a little help In-Reply-To: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> On Mon, Jul 6, 2009 at 3:02 PM, Nile wrote: > I am trying to write a simple little program to do some elementary > stock market analysis. ?I read lines, send each line to a function and > then the function returns a date which serves as a key to a > dictionary. Each time a date is returned I want to increment the value > associated with that date. The function seems to be working properly. > By means of a print statement I have inserted just before the return > value I can see there are three dates that are returned which is > correct. ?The dictionary only seems to capture the last date. My test > data consists of five stocks, each stock with five days. The correct > answer would be a count of 5 for the second day, the third day, and > the last day -- 11/14/2008. > > Here is the a code, followed by a portion of the output. ?I know > enough to write simple little programs like this with no problems up > until now but I don't know enough to figure out what I am doing > wrong. > ? ?for x in range(len(file_list)): for filename in file_list: #I'm assuming the lack of indentation on the subsequent lines is a mere transcription error... > ? ?d = open(file_list[x] , "r") d = open(filename , "r") > ? ?data = d.readlines() > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > function seems to work correctly > ? ?print "here is the value that was returned " , k > ? ?dict[k] = dict.get(k,0) + 1 `dict` is the name of a builtin type. Please rename this variable to avoid shadowing the type. Also, where is this variable even initialized? It's not in this code snippet you gave. Further, I would recommend using a defaultdict (http://docs.python.org/dev/library/collections.html#collections.defaultdict) rather than a regular dictionary; this would make the count-incrementing part nicer. Taking these changes into account, your code becomes: from collections import defaultdict counts = defaultdict(lambda: 0) for filename in file_list: d = open(filename , "r") data = d.readlines() k = above_or_below(data) # This function seems to work correctly print "here is the value that was returned " , k counts[k] += 1 values = counts.values() print "here is a list of the dictionary values ", values print "the length of the dictionary is ", len(counts) I don't immediately see what's causing your problem, but guess that it might've be related to the initialization of the `dict` variable. Cheers, Chris -- http://blog.rebertia.com From tn.pablo at gmail.com Mon Jul 6 18:30:21 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Mon, 6 Jul 2009 17:30:21 -0500 Subject: Semi-Newbie needs a little help In-Reply-To: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Mon, Jul 6, 2009 at 17:02, Nile wrote: > Code > > ? ?for x in range(len(file_list)): > ? ?d = open(file_list[x] , "r") > ? ?data = d.readlines() > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > function seems to work correctly > ? ?print "here is the value that was returned " , k > ? ?dict[k] = dict.get(k,0) + 1 > > ? ?dict_list = dict.values() > ? ?print "here is a list of the dictionary values ", dict_list > ? ?print "the length of the dictionary is ", len(dict) Correcting your indentation errors and moving your comments above the line they reference will attract more help from others in this list ;-) Also, I'd recommend limiting your line length to 80 chars, since lines are wrapped anyway. -- Pablo Torres N. From bcharrow at csail.mit.edu Mon Jul 6 18:38:12 2009 From: bcharrow at csail.mit.edu (Ben Charrow) Date: Mon, 06 Jul 2009 18:38:12 -0400 Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <4A527CD4.3010406@csail.mit.edu> Michael Mossey wrote: > On Jul 6, 2:47 pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >>> What is required in a python program to make sure it catches a control- >>> c on the command-line? Do some i/o? The OS here is Linux. >> You can use a try/except to catch a KeyboardInterrupt exception, or you >> can trap it using the signal >> module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH Philip > > Thanks to both of you. However, my question is also about whether I need to > be doing i/o or some similar operation for my program to notice in any shape > or form that Control-C has been pressed. You don't need to be doing I/O in order to raise a KeyboardIneterrupt. For example, the following program should use up a lot of your CPU until you hit Ctrl-C. >>> while True: ... pass ... ^CTraceback (most recent call last): File "", line 1, in KeyboardInterrupt > In the past, I've written Python programs that go about their business > ignoring Ctrl-C. Can you be more specific? Can you share the relevant sections of these programs? Were these programs multi-threaded? > But I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. Strictly speaking, I don't think you can always respond to a Ctrl-C in any shape or form. Quoting from the signal module: Although Python signal handlers are called asynchronously as far as the Python user is concerned, they can only occur between the "atomic" instructions of the Python interpreter. This means that signals arriving during long calculations implemented purely in C (such as regular expression matches on large bodies of text) may be delayed for an arbitrary amount of time. HTH, Ben From python at mrabarnett.plus.com Mon Jul 6 18:49:41 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 06 Jul 2009 23:49:41 +0100 Subject: Semi-Newbie needs a little help In-Reply-To: <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> Message-ID: <4A527F85.7030400@mrabarnett.plus.com> Chris Rebert wrote: > On Mon, Jul 6, 2009 at 3:02 PM, Nile wrote: >> I am trying to write a simple little program to do some elementary >> stock market analysis. I read lines, send each line to a function and >> then the function returns a date which serves as a key to a >> dictionary. Each time a date is returned I want to increment the value >> associated with that date. The function seems to be working properly. >> By means of a print statement I have inserted just before the return >> value I can see there are three dates that are returned which is >> correct. The dictionary only seems to capture the last date. My test >> data consists of five stocks, each stock with five days. The correct >> answer would be a count of 5 for the second day, the third day, and >> the last day -- 11/14/2008. >> >> Here is the a code, followed by a portion of the output. I know >> enough to write simple little programs like this with no problems up >> until now but I don't know enough to figure out what I am doing >> wrong. > >> for x in range(len(file_list)): > > for filename in file_list: > #I'm assuming the lack of indentation on the subsequent lines is a > mere transcription error... > >> d = open(file_list[x] , "r") > > d = open(filename , "r") > >> data = d.readlines() >> k = above_or_below(data) # This >> function seems to work correctly >> print "here is the value that was returned " , k >> dict[k] = dict.get(k,0) + 1 > > `dict` is the name of a builtin type. Please rename this variable to > avoid shadowing the type. > Also, where is this variable even initialized? It's not in this code > snippet you gave. > Further, I would recommend using a defaultdict > (http://docs.python.org/dev/library/collections.html#collections.defaultdict) > rather than a regular dictionary; this would make the > count-incrementing part nicer. > > Taking these changes into account, your code becomes: > > from collections import defaultdict > > counts = defaultdict(lambda: 0) > Better is: counts = defaultdict(int) > for filename in file_list: > d = open(filename , "r") > data = d.readlines() > k = above_or_below(data) # This function seems to work correctly > print "here is the value that was returned " , k > counts[k] += 1 > > values = counts.values() > print "here is a list of the dictionary values ", values > print "the length of the dictionary is ", len(counts) > > > I don't immediately see what's causing your problem, but guess that it > might've be related to the initialization of the `dict` variable. > It might be that the indentation was wrong where the count is incremented, but I can't tell because none of the lines were shown indented. From p.f.moore at gmail.com Mon Jul 6 18:50:08 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 6 Jul 2009 23:50:08 +0100 Subject: Opening a SQLite database in readonly mode In-Reply-To: References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: <79990c6b0907061550o43df92c0qec1fe2c7e49b916@mail.gmail.com> 2009/7/6 Joshua Kugler : > Paul Moore wrote: >> The SQLite documentation mentions a flag, SQLITE_OPEN_READONLY, to >> open a database read only. I can't find any equivalent documented in >> the Python standard library documentation for the sqlite3 module (or, >> for that matter, on the pysqlite library's website). >> >> Is it possible to open a sqlite database in readonly mode, in Python? > > Yes, but most likely not with pysqlite. ?The python sqlite3 module in the > standard library, and the pysqlite module are both DB-API compliant, which > means they probably do not have a method to open the DB read only (as that > is usually enforced at the user permission level). > > If you want to use that flag, take a look at APSW. It is a very thin layer > on top of the SQLite C API, and thus supports everything that the C API > supports. ?It is not, however, DB API compliant, but is very close so not > hard to pick up. > > BTW, APSW is written by the same author as pysqlite. Excellent, thanks. I'll have to think whether I want the extra dependency but at least I know it's possible now. Thanks for the pointer. Paul. From nile_mcadams at yahoo.com Mon Jul 6 18:50:59 2009 From: nile_mcadams at yahoo.com (Nile) Date: Mon, 6 Jul 2009 15:50:59 -0700 (PDT) Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Jul 6, 5:30?pm, "Pablo Torres N." wrote: > On Mon, Jul 6, 2009 at 17:02, Nile wrote: > > Code > > > ? ?for x in range(len(file_list)): > > ? ?d = open(file_list[x] , "r") > > ? ?data = d.readlines() > > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > > function seems to work correctly > > ? ?print "here is the value that was returned " , k > > ? ?dict[k] = dict.get(k,0) + 1 > > > ? ?dict_list = dict.values() > > ? ?print "here is a list of the dictionary values ", dict_list > > ? ?print "the length of the dictionary is ", len(dict) > > Correcting your indentation errors and moving your comments above the > line they reference will attract more help from others in this list > ;-) > > Also, I'd recommend limiting your line length to 80 chars, since lines > are wrapped anyway. > > -- > Pablo Torres N. Yup - Sorry, first post ever - next ones will be better formatted From piet at cs.uu.nl Mon Jul 6 18:51:24 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 00:51:24 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> Message-ID: >>>>> Scott David Daniels (SDD) wrote: >SDD> # No need for global declarations, we alter, not replace Yes, I know, but I find it neater to do the global declarations, if only for documentation. And they don't affect the run time, only the compile time. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From matt0177 at GMAIL.COM Mon Jul 6 18:56:40 2009 From: matt0177 at GMAIL.COM (matt0177) Date: Mon, 6 Jul 2009 15:56:40 -0700 (PDT) Subject: Python Error from Apress book Message-ID: <24364269.post@talk.nabble.com> http://www.nabble.com/file/p24364269/simple_markup2.py simple_markup2.py http://www.nabble.com/file/p24364269/util2.py util2.py The two above files are from chapter 20 of the Apress book "Beginning Python >From Novice to Professional". When I try to run the command as outlined in the book "simple_markup2.py < test_input.txt > test_output.html i get the following error every time. File "C:\Python25\Scripts\simple_markup2.py", line 7, in for block in blocks(sys.stdin): File "C:\Python25\Scripts\util2.py", line 7, in blocks for line in lines(file): File "C:\Python25\Scripts\util2.py", line 2, in lines for line in file: yield line IOError: [Errno 9] Bad file descriptor I've tried this code on two different computers (both winxp, python 2.5.1) with the same results. when simple_markup2.py is run with only output, it will correctly write the headers. When you try to add an input file, you get the error every time, and I have tried every text file known to man, including the downloadable sample. This has been frustrating me for several days, any advice would be greatly appreciated. I'm just starting to learn python so troubleshooting is a sorely lacked skill atm :) Thanks, Matt -- View this message in context: http://www.nabble.com/Python-Error-from-Apress-book-tp24364269p24364269.html Sent from the Python - python-list mailing list archive at Nabble.com. From hannarosie at gmail.com Mon Jul 6 18:57:20 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Mon, 6 Jul 2009 15:57:20 -0700 Subject: Write matrix to text file Message-ID: <564970db0907061557s2c559b3cxaf52859abd2050a3@mail.gmail.com> Hi, I'm working with both python and matlab at the moment and I was wondering if there is an efficient way to take a 2-D array (of 1s and 0s) in python and write it to a text file such that matlab will be able to create a sparse matrix from it later. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Mon Jul 6 18:58:00 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 00:58:00 +0200 Subject: Why is my code faster with append() in a loop than with a large list? References: <2d56febf0907051720y1958c08flc1e67ed55700bfd5@mail.gmail.com> <4A51CD6A.2010106@dejaviewphoto.com> <4A52089F.7020301@mrabarnett.plus.com> Message-ID: >>>>> Dave Angel (DA) wrote: >DA> MRAB wrote: >>>
Dave Angel >>> wrote: >>> [snip] >>>> It would probably save some time to not bother storing the zeroes in the >>>> list at all. And it should help if you were to step through a list of >>>> primes, rather than trying every possible int. Or at least constrain >>>> yourself to odd numbers (after the initial case of 2). >>>> >>> Or stop looking for more factors when you've passed the square root of >>> num. I don't know what effect there'll be on the time if you recalculate >>> the square root when num changes (expensive calculation vs smaller >>> search space). >>> >>>
>>> >DA> But if I remember the code, it stopped when the quotient is one, which is >DA> usually sooner than the square root. And no need to precalculate the >DA> square root. That's right. I thought of doing the sqrt trick by testing for num < factor, i.e." if num < factor: break but found out that it is useless, as for each factor found, num is divided by that factor as many times as possible. Therefore when the largest prime factor of num has been found, num ends up being 1 and the loop stops. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nacim_bravo at agilent.com Mon Jul 6 19:00:39 2009 From: nacim_bravo at agilent.com (nacim_bravo at agilent.com) Date: Mon, 6 Jul 2009 17:00:39 -0600 Subject: Newbie needs help In-Reply-To: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Dear Python gurus, If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? Please let me know, nacim From rhodri at wildebst.demon.co.uk Mon Jul 6 19:03:00 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:03:00 +0100 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: On Mon, 06 Jul 2009 18:41:03 +0100, jack catcher (nick) wrote: > Rhodri James kirjoitti: >> Does the webcam just deliver frames, or are you getting frames out of >> a decoder layer? If it's the latter, you want to distribute the encoded >> video, which should be much lower bandwidth. Exactly how you do that >> depends a bit on what format the webcam claims to deliver. > > Well, getting already encoded video from the webcam sounds almost like a > free lunch (which it probably is not). At least I wouldn't want to get > too long a delay because of the encoding. Not so unlikely as you might think, since there are very basic M-JPEG and MPEG-2 on-chip encoders available, and the webcam will have to do some compression to get the video data into the computer. USB is not as fast as it would like to pretend. > I haven't got the webcam(s) yet, and I guess I can basically purchase > any ones I find suitable for getting the job done. Any recommendations? Sorry, no. I'm used to getting my video feeds down fibre-optic cables :-) -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Mon Jul 6 19:05:44 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 01:05:44 +0200 Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: >>>>> Philip Semanchuk (PS) wrote: >PS> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >>> What is required in a python program to make sure it catches a control- >>> c on the command-line? Do some i/o? The OS here is Linux. >PS> You can use a try/except to catch a KeyboardInterrupt exception, or you >PS> can trap it using the signal module: >PS> http://docs.python.org/library/signal.html >PS> You want to trap SIGINT. And make sure threads don't mess up the signal handling. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rhodri at wildebst.demon.co.uk Mon Jul 6 19:16:00 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:16:00 +0100 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: On Mon, 06 Jul 2009 21:40:49 +0100, kj wrote: > In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers > writes: > >> kj a ?crit : >> (snipo >>> To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, > >> FWIW, Python has no pretention to minimalism. > > Assuming that you mean by this that Python's authors have no such > pretensions: > > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html re.match() is part of the library, not the language. The standard library is in no sense of the word small. It has a mild tendency to avoid repeating itself, but presumably the stonkingly obvious optimisation possibilities of re.match() over re.search() are considered worth the (small) increase in size. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Mon Jul 6 19:20:48 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:20:48 +0100 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Message-ID: On Tue, 07 Jul 2009 00:00:39 +0100, wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it > possible to do such in Python environment? If yes, how to do or what > syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible > to get this value in Python environment? Quite possibly, however you're going to have to give us a *lot* more information before the answers you get will be worth anything at all. How is "the certain material" represented? What simulator are you using? Which Python environment? Which Python version for that matter? We may appear to be mind-readers, but we usually need a bit more than this to work on. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Mon Jul 6 19:23:57 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 00:23:57 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: <4A522C4B.20102@ieee.org> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4A522C4B.20102@ieee.org> Message-ID: On Mon, 06 Jul 2009 17:54:35 +0100, Dave Angel wrote: > Rhodri James wrote: >> Indeed, arguably it's a bug for C compilers to fail to find the valid >> parsing of "++5" as "+(+5)". All I can say is that I've never even >> accidentally typed that in twenty years of C programming. >> > But the C language specifically defines the tokenizer as doing a > max-match, finding the longest legal token at any point. That's how > many things that would otherwise be ambiguous are well-defined. For > example, if you want to divide two integers, given pointers to them, you > need a space between the slash and the start. > *p1/*p2 begins a comment, while *p1/ *p2 does a division You know, I've never had that come up either! My habit of sticking spaces around binary operators purely for legibility probably helped, but I'm still a bit boggled. -- Rhodri James *-* Wildebeest Herder to the Masses From nile_mcadams at yahoo.com Mon Jul 6 19:29:36 2009 From: nile_mcadams at yahoo.com (Nile) Date: Mon, 6 Jul 2009 16:29:36 -0700 (PDT) Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Jul 6, 5:22?pm, Chris Rebert wrote: > On Mon, Jul 6, 2009 at 3:02 PM, Nile wrote: > > I am trying to write a simple little program to do some elementary > > stock market analysis. ?I read lines, send each line to a function and > > then the function returns a date which serves as a key to a > > dictionary. Each time a date is returned I want to increment the value > > associated with that date. The function seems to be working properly. > > By means of a print statement I have inserted just before the return > > value I can see there are three dates that are returned which is > > correct. ?The dictionary only seems to capture the last date. My test > > data consists of five stocks, each stock with five days. The correct > > answer would be a count of 5 for the second day, the third day, and > > the last day -- 11/14/2008. > > > Here is the a code, followed by a portion of the output. ?I know > > enough to write simple little programs like this with no problems up > > until now but I don't know enough to figure out what I am doing > > wrong. > > ? ?for x in range(len(file_list)): > > for filename in file_list: > #I'm assuming the lack of indentation on the subsequent lines is a > mere transcription error... > > > ? ?d = open(file_list[x] , "r") > > ? ? d = open(filename , "r") > > > ? ?data = d.readlines() > > ? ?k = above_or_below(data) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# This > > function seems to work correctly > > ? ?print "here is the value that was returned " , k > > ? ?dict[k] = dict.get(k,0) + 1 > > `dict` is the name of a builtin type. Please rename this variable to > avoid shadowing the type. > Also, where is this variable even initialized? It's not in this code > snippet you gave. > Further, I would recommend using a defaultdict > (http://docs.python.org/dev/library/collections.html#collections.defau...) > rather than a regular dictionary; this would make the > count-incrementing part nicer. > > Taking these changes into account, your code becomes: > > from collections import defaultdict > > counts = defaultdict(lambda: 0) > > for filename in file_list: > ? ? d = open(filename , "r") > ? ? data = d.readlines() > ? ? k = above_or_below(data) # This function seems to work correctly > ? ? print "here is the value that was returned " , k > ? ? counts[k] += 1 > > ? ? values = counts.values() > ? ? print "here is a list of the dictionary values ", values > ? ? print "the length of the dictionary is ", len(counts) > > I don't immediately see what's causing your problem, but guess that it > might've be related to the initialization of the `dict` variable. > > Cheers, > Chris > --http://blog.rebertia.com- Hide quoted text - > > - Show quoted text - I initialized the dictionary earlier in the program like this - hashtable = {} I changed the "dict" to hashtable but I still get the same result I will try to learn about the defaultdict but I'm just trying to keep it as simple as I can for now Revised code for x in range(len(file_list)): d = open(file_list[x] , "r") data = d.readlines() k = 0 k = above_or_below(data) print "here is the value that was returned ",k hashtable[k] = hashtable.get(k,0) + 1 hashtable_list = hashtable.values() print "here is a list of the dictionary values ", hashtable_list print "the length of the dictionary is ", len(hashtable) Output # The first 3 lines are printed from the function # right before the return statement. This output # snippet shows the last two stocks. The function # SAYS it is returning the correct value but only # the last date seems to make it to the hashtable Function will return k which = 11/11/2008 Function will return k which = 11/12/2008 Function will return k which = 11/14/2008 # this line is printed from the code above # I don't understand why all three dates don't # seem to make it to the main program. Only # the last date seems to be recognized here is the value that was returned 11/14/2008 Function will return k which = 11/11/2008 Function will return k which = 11/12/2008 Function will return k which = 11/14/2008 here is the value that was returned 11/14/2008 here is a list of the dictionary values [5] the length of the dictionary is 1 >Exit code: 0 From python at mrabarnett.plus.com Mon Jul 6 19:41:53 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 00:41:53 +0100 Subject: Semi-Newbie needs a little help In-Reply-To: References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <4A528BC1.4040400@mrabarnett.plus.com> Nile wrote: [snip] > I initialized the dictionary earlier in the program like this - > > hashtable = {} > > I changed the "dict" to hashtable but I still get the same result > I will try to learn about the defaultdict but I'm just trying to keep > it as simple as I can for now > > Revised code > > for x in range(len(file_list)): > d = open(file_list[x] , "r") > data = d.readlines() What's the point of the following line? > k = 0 > k = above_or_below(data) > print "here is the value that was returned ",k > hashtable[k] = hashtable.get(k,0) + 1 > > > hashtable_list = hashtable.values() > print "here is a list of the dictionary values ", hashtable_list > print "the length of the dictionary is ", len(hashtable) > > Output > # The first 3 lines are printed from the function > # right before the return statement. This output > # snippet shows the last two stocks. The function > # SAYS it is returning the correct value but only > # the last date seems to make it to the hashtable > Function will return k which = 11/11/2008 > Function will return k which = 11/12/2008 > Function will return k which = 11/14/2008 > > # this line is printed from the code above > # I don't understand why all three dates don't > # seem to make it to the main program. Only > # the last date seems to be recognized > here is the value that was returned 11/14/2008 > > Function will return k which = 11/11/2008 > Function will return k which = 11/12/2008 > Function will return k which = 11/14/2008 > here is the value that was returned 11/14/2008 > here is a list of the dictionary values [5] > the length of the dictionary is 1 >> Exit code: 0 I think there's a bug in 'above_or_below' which you haven't noticed. From gagsl-py2 at yahoo.com.ar Mon Jul 6 19:49:40 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 20:49:40 -0300 Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: En Mon, 06 Jul 2009 18:15:44 -0300, Scott David Daniels escribi?: > brasse wrote: >> I have been thinking about how write exception safe constructors in >> Python. By exception safe I mean a constructor that does not leak >> resources when an exception is raised within it. > ... > > As you can see this is less than straight forward. Is there some kind > > of best practice that I'm not aware of? > > Not so tough. Something like this tweaked version of your example: Another variant: Presumably, there is already a method in Bar responsible for "normal" cleanup; just make sure it gets called (and write it in a robust way): > class Foo(object): > def __init__(self, name, fail=False): > self.name = name > if not fail: > print '%s.__init__(%s)' % (type(self).__name__, name) > else: > print '%s.__init__(%s), FAIL' % (type(self).__name__, name) > raise ValueError('Asked to fail: %r' % fail) > > def close(self): > print '%s.close(%s)' % (type(self).__name__, self.name) class Bar(object): a = None # default values b = None def __init__(self): try: self.a = Foo('a') self.b = Foo('b', fail=True) except Exception, why: self.cleanup() raise def cleanup(self): if self.a is not None: self.a.close() if self.b is not None: self.b.close() bar = Bar() -- Gabriel Genellina From mh at pixar.com Mon Jul 6 20:21:32 2009 From: mh at pixar.com (mh at pixar.com) Date: Tue, 07 Jul 2009 00:21:32 GMT Subject: parsing times like "5 minutes ago"? Message-ID: I'm looking for something like Tcl's [clock scan] command which parses human-readable time strings such as: % clock scan "5 minutes ago" 1246925569 % clock scan "tomorrow 12:00" 1246993200 % clock scan "today + 1 fortnight" 1248135628 Does any such package exist for Python? Many TIA! Mark -- Mark Harrison Pixar Animation Studios From pavlovevidence at gmail.com Mon Jul 6 20:43:09 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 6 Jul 2009 17:43:09 -0700 (PDT) Subject: parsing times like "5 minutes ago"? References: Message-ID: On Jul 6, 5:21?pm, m... at pixar.com wrote: > I'm looking for something like Tcl's [clock scan] command which parses > human-readable time strings such as: > > ? ? % clock scan "5 minutes ago" > ? ? 1246925569 > ? ? % clock scan "tomorrow 12:00" > ? ? 1246993200 > ? ? % clock scan "today + 1 fortnight" > ? ? 1248135628 > > Does any such package exist for Python? If you're on a machine with GNU datethe simplest solution is to use it to parse the string. Q&D: def clock_scan(datestring): stdout,stderr = subprocess.Popen( ['date','+%s','--date=%s' % datestring ], stdout=subprocess.PIPE).communicate() return float(stdout) clock_scan('1 hour ago') clock_scan('next week') Carl Banks From rhodri at wildebst.demon.co.uk Mon Jul 6 20:58:07 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 01:58:07 +0100 Subject: Semi-Newbie needs a little help In-Reply-To: References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: On Tue, 07 Jul 2009 00:29:36 +0100, Nile wrote: > Revised code > > for x in range(len(file_list)): > d = open(file_list[x] , "r") > data = d.readlines() > k = 0 > k = above_or_below(data) > print "here is the value that was returned ",k > hashtable[k] = hashtable.get(k,0) + 1 > > > hashtable_list = hashtable.values() > print "here is a list of the dictionary values ", hashtable_list > print "the length of the dictionary is ", len(hashtable) > > Output > # The first 3 lines are printed from the function > # right before the return statement. This output > # snippet shows the last two stocks. The function > # SAYS it is returning the correct value but only > # the last date seems to make it to the hashtable > Function will return k which = 11/11/2008 > Function will return k which = 11/12/2008 > Function will return k which = 11/14/2008 Have you checked the indentation of the print statement that produces this? Is it perhaps inside a loop still? -- Rhodri James *-* Wildebeest Herder to the Masses From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:01:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:01:44 GMT Subject: Clarity vs. code reuse/generality References: <7xk52p4tgg.fsf@ruckus.brouhaha.com> Message-ID: <006e795f$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: > kj wrote: >> I've rewritten it like this: >> >> sense = cmp(func(hi), func(lo)) >> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >> >> Thanks for your feedback! >> >> kj >> >> > As already said before, unlike other languages, sense in english does > **not** mean direction. You should rewrite this part using a better > name. Wrong informations are far worse than no information at all. Absolutely. >From Webster's Dictionary: 8. (Geom.) One of two opposite directions in which a line, surface, or volume, may be supposed to be described by the motion of a point, line, or surface. [1913 Webster] And from WordNet: 2: the meaning of a word or expression; the way in which a word or expression or situation can be interpreted Both meanings are relevant to the way KJ is using the word. Please take your own advice and stop giving wrong information. As a native English speaker, I had no difficulty understanding the meaning of "sense" in the sense intended by KJ. -- Steven From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:04:02 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:04:02 GMT Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006e79e9$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 05:47:18 -0700, Scott David Daniels wrote: > Steven D'Aprano wrote: >> ... That's the Wrong Way to do it -- >> you're using a screwdriver to hammer a nail.... > > Don't knock tool abuse (though I agree with you here). Sometimes tool > abuse can produce good results. For example, using hammers to drive > screws for temporary strong holds led to making better nails. Historically, nails were invented a long time before screws. Screws as fasteners weren't invented until the 1400s, nails were used thousands of years ago. And hammering screws makes temporary *weak* holds, not strong, because the screw only touches the sides of the hole lightly. Unless the screw has been specifically designed to be hammered, hammering screws is pretty much the definition of incompetence and/or laziness! -- Steven From chris at simplistix.co.uk Mon Jul 6 21:09:41 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 02:09:41 +0100 Subject: Where does setuptools live? In-Reply-To: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> Message-ID: <4A52A055.8080108@simplistix.co.uk> Floris Bruynooghe wrote: > It is, see > http://mail.python.org/pipermail/distutils-sig/2009-July/012374.html > > >> It's seen no changes in 9 months. > > It's setuptools... I'm sure you can find many flamefests on distutils- > sig about this. Yeah, distutils-sig is the right place to discuss. I wonder how close setuptools is to being forked because of Phil Eby's unwillingness to apply patches and/or clean up the horrible setuptools code? Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:16:41 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:16:41 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <006e7ce0$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 16:43:43 +0100, Tim Rowe wrote: > 2009/7/4 kj : > >> Precisely. ?As I've stated elsewhere, this is an internal helper >> function, to be called only a few times under very well-specified >> conditions. ?The assert statements checks that these conditions are as >> intended. ?I.e. they are checks against the module writer's programming >> errors. > > Good for you. I'm convinced that you have used the assertion > appropriately, and the fact that so many here are unable to see that > looks to me like a good case for teaching the right use of assertions. > For what it's worth, I read assertions at the beginning of a procedure > as part of the specification of the procedure, and I use them there in > order to document the procedure. An assertion in that position is for me > a statement to the user of the procedure "it's your responsibility to > make sure that you never call this procedure in such a way as to violate > these conditions". They're part of a contract, as somebody (maybe you) > pointed out. > > As somebody who works in the safety-critical domain, it's refreshing to > see somebody teaching students to think about the circumstances in which > a procedure can legitimately be called. The hostility you've received to > that idea is saddening, and indicative of why there's so much buggy > software out there. LOL. Maybe the reason for "so much buggy software" is that people inappropriately use assert, thus changing the behaviour of code depending on whether it is run with the -O flag or not. I don't know what "hostility" you're seeing. The only hostility I'm seeing is from the OP, which is bizarre considering that he asked for advice and we gave it. What I see is a bunch of people concerned that the OP is teaching novices a bad habit, namely, using assert for error checking. He claims -- angrily and over and over again -- that in his code, the assertions should never fail. Great. Good for him for knowing when to use assert. But are the novices going to learn that lesson, or will they simply learn "use assert for error checking"? -- Steven From davea at ieee.org Mon Jul 6 21:18:57 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 21:18:57 -0400 Subject: Semi-Newbie needs a little help In-Reply-To: <4A528BC1.4040400@mrabarnett.plus.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <4A528BC1.4040400@mrabarnett.plus.com> Message-ID: <4A52A281.3040101@ieee.org> MRAB wrote: >
Nile wrote: > [snip] >> I initialized the dictionary earlier in the program like this - >> >> hashtable = {} >> >> I changed the "dict" to hashtable but I still get the same result >> I will try to learn about the defaultdict but I'm just trying to keep >> it as simple as I can for now >> >> Revised code >> >> for x in range(len(file_list)): >> d = open(file_list[x] , "r") >> data = d.readlines() > > What's the point of the following line? > >> k = 0 >> k = above_or_below(data) >> print "here is the value that was returned ",k >> hashtable[k] = hashtable.get(k,0) + 1 >> >> >> hashtable_list = hashtable.values() >> print "here is a list of the dictionary values ", hashtable_list >> print "the length of the dictionary is ", len(hashtable) >> >> Output >> # The first 3 lines are printed from the function >> # right before the return statement. This output >> # snippet shows the last two stocks. The function >> # SAYS it is returning the correct value but only >> # the last date seems to make it to the hashtable > >> Function will return k which = 11/11/2008 >> Function will return k which = 11/12/2008 >> Function will return k which = 11/14/2008 >> >> # this line is printed from the code above >> # I don't understand why all three dates don't >> # seem to make it to the main program. Only >> # the last date seems to be recognized >> here is the value that was returned 11/14/2008 >> >> Function will return k which = 11/11/2008 >> Function will return k which = 11/12/2008 >> Function will return k which = 11/14/2008 >> here is the value that was returned 11/14/2008 >> here is a list of the dictionary values [5] >> the length of the dictionary is 1 >>> Exit code: 0 > > I think there's a bug in 'above_or_below' which you haven't noticed. > >
> The code supplied doesn't match the output supplied. It'd probably help if the output was actually pasted from the command window, instead of retyped with more comments than data. And of course it'd help if you actually showed us the function above_or_below(), which is probably where the bug is. If it prints three values, but returns a string, then why would you be surprised? Maybe you intended it to return a list? Each time above_or_below() it's called, it prints three lines before returning, but only returns the final value. How big is file_list? I suspect it's of length 5. And the output is shown as repeated twice, but it probably was actually five sets of data. You do know you can print hashtable, don't you? You're extracting and printing the values, but not bothering with the keys. I suggest you add a print to the entry point of above_or_below(), to match the one you have for its return. And all of these print lines should be indented. That might make it easier to interpret the output, without lots of inserted comments. DaveA From steve at REMOVE-THIS-cybersource.com.au Mon Jul 6 21:20:04 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 01:20:04 GMT Subject: Newbie needs help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <006e7dac$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 17:00:39 -0600, nacim_bravo wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it > possible to do such in Python environment? If yes, how to do or what > syntax can be used? certain_material.dielectric_constant = 1.234 > Also, I'd like to get a simulation result, like voltage, is it possible > to get this value in Python environment? Yes. -- Steven From ben.welsh at gmail.com Mon Jul 6 21:25:13 2009 From: ben.welsh at gmail.com (palewire) Date: Mon, 6 Jul 2009 18:25:13 -0700 (PDT) Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else Message-ID: <24365710.post@talk.nabble.com> In my application, I'd like to have a function that compares two values, either of which may be null, and then classify the result depending on whether one is higher or lower than the other. I find myself writing something clunky like this, and I'm curious whether anyone might know a more elegant way for parsing this out. def compare(a, b): if not a and not b: return "No data" else: if a == 0 and b == 0: return "Never had" else: if a == b: return "Stayed the same" elif a < b: return "Gained" elif a > b and b > 0: return "Lost Some" elif a > b and b == 0: return "Lost All" If there's some obvious way to search for this solution online, feel free to slap me with that. I tried digging around on Google and couldn't come up with much. Thanks much. -- View this message in context: http://www.nabble.com/Looking-for-a-slick-way-to-classify-relationship-between-two-numbers%2C-without-tons-of-if-else-tp24365710p24365710.html Sent from the Python - python-list mailing list archive at Nabble.com. From gherron at islandtraining.com Mon Jul 6 21:29:51 2009 From: gherron at islandtraining.com (Gary Herron) Date: Mon, 06 Jul 2009 18:29:51 -0700 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Message-ID: <4A52A50F.1060208@islandtraining.com> nacim_bravo at agilent.com wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? > > Please let me know, > nacim > This would be a good place for you to start: http://www.catb.org/~esr/faqs/smart-questions.html From ldo at geek-central.gen.new_zealand Mon Jul 6 21:33:18 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 07 Jul 2009 13:33:18 +1200 Subject: A Bug By Any Other Name ... References: Message-ID: In message , Terry Reedy wrote: > ... it is C, not Python, that is out of step with standard usage in math > and most languages ... And it is C that introduced "==" for equality, versus "=" for assignment, which Python slavishly followed instead of keeping "=" with its mathematical meaning and using ":=" for assignment. From david.lyon at preisshare.net Mon Jul 6 21:55:03 2009 From: david.lyon at preisshare.net (David Lyon) Date: Mon, 06 Jul 2009 21:55:03 -0400 Subject: Where does setuptools =?UTF-8?Q?live=3F?= In-Reply-To: <4A52A055.8080108@simplistix.co.uk> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> Message-ID: On Tue, 07 Jul 2009 02:09:41 +0100, Chris Withers wrote: > I wonder how close setuptools is to being forked because of Phil Eby's > unwillingness to apply patches and/or clean up the horrible setuptools > code? setuptools... as far as I can see isn't actually installed until you install easyinstall... Pip (http://pypi.python.org/pypi/pip) and enstall (http://pypi.python.org/pypi/Enstaller/3.1.0) seem to be forks of setuptools already... So it looks like it's already been forked to some degree.. What hasn't happened is enough testing of pypi packages and installing with setuptools/pip/enstall from pypi. If the xmlrpc links actually "worked" on pypi... http://wiki.python.org/moin/PyPiXmlRpc?action=show&redirect=CheeseShopXmlRpc ie.. that they would allow a developer to download a packages via rpc.... then this whole issue could probably be fixed more easily... ok - the process isn't perfect... but there's room for improvement... David From rhodri at wildebst.demon.co.uk Mon Jul 6 22:11:43 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 07 Jul 2009 03:11:43 +0100 Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else In-Reply-To: <24365710.post@talk.nabble.com> References: <24365710.post@talk.nabble.com> Message-ID: On Tue, 07 Jul 2009 02:25:13 +0100, palewire wrote: > In my application, I'd like to have a function that compares two values, > either of which may be null, and then classify the result depending on > whether one is higher or lower than the other. > > I find myself writing something clunky like this, and I'm curious whether > anyone might know a more elegant way for parsing this out. It depends on what you want to do with the information. If you're doing different things on each case, you might as well do it with an if-chain, though you could make it a bit tidier: > def compare(a, b): > if not a and not b: Did you mean "if a is None or b is None:" here? > return "No data" > else: > if a == 0 and b == 0: This will never fire, because it will always be caught by the test above. > return "Never had" > else: > if a == b: > return "Stayed the same" > elif a < b: > return "Gained" > elif a > b and b > 0: > return "Lost Some" > elif a > b and b == 0: > return "Lost All" def compare(a, b): if a is not None and b is not None: return "No data" elif a == 0 and b == 0: return "Never had" elif a == b: return "Stayed the same" elif a < b: return "Gained" elif b > 0: return "Lost Some" return "Lost All" Alternatively you can do something like the (deprecated) cmp and a dispatch table: CMP_TABLE = [ "Gained", "Stayed the same", "Lost Some", "Lost All"] def compare(a, b): if a is not None or b is not None: return "No data" c = cmp(a, b) + 1 if c == 2 and b == 0: c = 3 return CMP_TABLE[c] There's no cmp() in Python 3.x so you'd have to roll your own. At that point you might as well revert to the if-chain, since it'll be a lot easier to understand when you come back to it in six months time. -- Rhodri James *-* Wildebeest Herder to the Masses From gagsl-py2 at yahoo.com.ar Mon Jul 6 22:18:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 06 Jul 2009 23:18:37 -0300 Subject: Python Error from Apress book References: <24364269.post@talk.nabble.com> Message-ID: En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 escribi?: > When I try to run the command as outlined in > the book "simple_markup2.py < test_input.txt > test_output.html i get the > following error every time. > > IOError: [Errno 9] Bad file descriptor That's a Windows problem. When you execute the script as itself (either as you do in the command line, or by double-clicking on it), it doesn't have valid standard handles. You have to invoke Python explicitely: python simple_markup2.py < test_input.txt > test_output.html (you may need to specify the full path to python.exe, or add the directory where Python is installed to your system PATH). -- Gabriel Genellina From fetchinson at googlemail.com Mon Jul 6 22:48:39 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Mon, 6 Jul 2009 19:48:39 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <006da5a2$0$9711$c3e8da3@news.astraweb.com> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: >>>> I wonder how many people have been tripped up by the fact that >>>> >>>> ++n >>>> >>>> and >>>> >>>> --n >>>> >>>> fail silently for numeric-valued n. >>> >>> What do you mean, "fail silently"? They do exactly what you should >>> expect: >>>>>> ++5 # positive of a positive number is positive >>> >>> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >>> that are buggy, not Python. >> >> Well, those expectations are taken seriously when new features are >> introduced into the language - and sometimes the feature is dismissed >> just because it would be confusing for some. If a += 1 works, expecting >> ++a to have the same meaning is very reasonable (for those coming from >> languages with a ++ operator, like C or Java) - more when ++a is a >> perfectly valid expression. If this issue isn't listed under the various >> "Python gotchas" articles, it should... > > The fact that it isn't suggests strongly to me that it isn't that common > a surprise even for Java and C programmers. This is the first time I've > seen anyone raise it as an issue. > > There are plenty of other languages other than Java and C. If we start > listing every feature of Python that's different from some other > language, we'll never end. Yes, there are plenty of languages other than Java and C, but the influence of C is admittedly huge in Python. Why do you think loops are called "for", conditionals "if" or "while", functions return via "return", loops terminate via "break" and keep going via "continue" and why is comparison written as "==", etc, etc? All of these are coming from C (or an even earlier language) and my point is that users are most of time correct when they assume that something will work the same way as in C. So I'd think that putting a warning in a FAQ or a Python Gotchas list about ++n would be very useful for many users. And it does not imply that the differences from every other language should be documented in a similar fashion. Only C :) Cheers, Daniel > For what it's worth, Ruby appears to behave the same as Python: > > $ irb > irb(main):001:0> n = 5 > => 5 > irb(main):002:0> ++n > => 5 > irb(main):003:0> --n > => 5 > irb(main):004:0> -+n > => -5 > -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From astan.chee at al.com.au Mon Jul 6 22:58:24 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 7 Jul 2009 12:58:24 +1000 Subject: copytree with timeout in windows Message-ID: <4A52B9D0.9040608@al.com.au> Hi, I'm trying to modify the copytree function in shutil so that any file being copied does not take more than 5 minutes (if it does, skip to the next one). This is what I have so far: import shutil import signal, os def handler(signum, frame): print 'Signal handler called with signal', signum raise IOError, "Couldn't open device!" signal.signal(signal.SIGALRM, handler) def copytree(src, dst): names = os.listdir(src) os.makedirs(dst) signal.alarm(0) for name in names: if name in ignored_names: continue srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if os.path.isdir(srcname): copytree(srcname, dstname, symlinks, ignore) else: signal.alarm(300) shutil.copy2(srcname, dstname) except (IOError, os.error), why: print str(why) # catch the Error from the recursive copytree so that we can # continue with other files except Error, err: print str(err.args[0]) source = "c:\\testwa\\" destination = "d:\\testwa\\" copytree(source,destination) Then I re-read the documentation which said that signal.SIGALRM is unix/linux only. How do I go about doing this in windows? Thanks for any help. Cheers Astan From rocky at gnu.org Mon Jul 6 23:13:09 2009 From: rocky at gnu.org (rocky) Date: Mon, 6 Jul 2009 20:13:09 -0700 (PDT) Subject: module name versus function name resolution conflict. Message-ID: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Someone recently reported a problem in pydb where a function defined in his program was conflicting with a module name that pydb uses. I think I understand what's wrong, but I don't have any elegant solutions to the problem. Suggestions would be appreciated. In a nutshell, here's the problem: In file fns: def foo(): print "foo" In file pdebug.py: import fns, sys def trace_dispatch(frame, event, arg): fns.foo() print frame, event, arg return trace_dispatch sys.settrace(trace_dispatch) execfile('myprogram.py') Finally file myprogram.py: def fns(): print "This is the *other* fns" When you run pdebug.py you get: $ python pdebug.py foo call None foo line None Traceback (most recent call last): File "pdebug.py", line 7, in execfile('myprogram.py') File "myprogram.py", line 1, in def fns(): print "This is the *other* fns" File "pdebug.py", line 3, in trace_dispatch fns.foo() AttributeError: 'function' object has no attribute 'foo' Basically inside the trace hook, local functions are visible and take precedence over (global) module names. I could move "import fns" inside trace_dispatch(), but I'd have to do that in all of the methods that module "fns" is used. Also note that using the form: from fns import foo would eliminate the conflict on "fns", but I'd still have potential conflicts on "foo". Using more obscure names (e.g. pydb_fns) would reduce the chance of a conflict but not eliminate it. Suggestions? From davea at ieee.org Mon Jul 6 23:28:39 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Jul 2009 23:28:39 -0400 Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else In-Reply-To: <24365710.post@talk.nabble.com> References: <24365710.post@talk.nabble.com> Message-ID: <4A52C0E7.9060502@ieee.org> palewire wrote: > In my application, I'd like to have a function that compares two values, > either of which may be null, and then classify the result depending on > whether one is higher or lower than the other. > > I find myself writing something clunky like this, and I'm curious whether > anyone might know a more elegant way for parsing this out. > > def compare(a, b): > if not a and not b: > return "No data" > else: > if a == 0 and b == 0: > return "Never had" > else: > if a == b: > return "Stayed the same" > elif a < b: > return "Gained" > elif a > b and b > 0: > return "Lost Some" > elif a > b and b == 0: > return "Lost All" > > If there's some obvious way to search for this solution online, feel free to > slap me with that. I tried digging around on Google and couldn't come up > with much. > > Thanks much. > Before one can "optimize" a function, one needs to get the function correct. Looks to me that if a and b are both zero, it'll say "No data" and never get to the "Never had" test. And if a is positive and b is None, it'll return None, rather than any string. Anyway, any "optimized" construct is going to be nearly as big as this. You'd have to define a list of strings, and then compute an index value that gave you the appropriate value. While I could probably come up with such, I hardly think it's worthwhile. The cases are confusing enough to me that I'd not want to make the code any less readable by some formula & lookup. That brings us to the question of what is more elegant. To me the elegant code is readable first, and efficient (space & time) second. I suspect efficiency would be nearly optimal by careful ordering of the nested ifs. Find the most likely cases, and test for them first. If this is a programming puzzle, for entertainment purposes, here's the way I'd tackle making it "efficient" obfuscated code. Start by checking if either value is None. Something like: table = ("Lost Some","Lost All","Gained","Gained","Stayed the same","Never had, never will") def compare2(a,b): global table if a is None or b is None: return "Invalid data" return table[4*(a==b)+2*(a Message-ID: On Jul 6, 8:43?pm, Carl Banks wrote: > On Jul 6, 5:21?pm, m... at pixar.com wrote: > > > I'm looking for something like Tcl's [clock scan] command which parses > > human-readable time strings such as: > > > ? ? % clock scan "5 minutes ago" > > ? ? 1246925569 > > ? ? % clock scan "tomorrow 12:00" > > ? ? 1246993200 > > ? ? % clock scan "today + 1 fortnight" > > ? ? 1248135628 > > > Does any such package exist for Python? > > If you're on a machine with GNU datethe simplest solution is to use it > to parse the string. > > Q&D: > > def clock_scan(datestring): > ? ? stdout,stderr = subprocess.Popen( > ? ? ? ? ['date','+%s','--date=%s' % datestring ], > ? ? ? ? stdout=subprocess.PIPE).communicate() > ? ? return float(stdout) > > clock_scan('1 hour ago') > clock_scan('next week') > > Carl Banks The timelib module might do what you want http://pypi.python.org/pypi/timelib/0.2 From tjreedy at udel.edu Mon Jul 6 23:50:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 06 Jul 2009 23:50:08 -0400 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: kj wrote: > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html > > So there. small != minimal > > BTW, that's just one example. I've seen similar sentiments expressed > by Guido over and over and over: any new proposed enhancement to > Python must be good enough in his mind to justify cluttering the > language. That attitude counts as minimalism in my book. > > The best explanation I have found so far for re.match is that it > is an unfortunate bit of legacy, something that would not be there > if the design of Python did not have to be mindful of keeping old > code chugging along... It is possible that someone proposed removing re.match for 3.0, but I do not remember any such discussion. Some things were dropped when that contraint was (teporarily) dropped. tjr From gagsl-py2 at yahoo.com.ar Mon Jul 6 23:51:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 00:51:07 -0300 Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <50697b2c0907061522o59bf5333qec3722039e688426@mail.gmail.com> <4A527F85.7030400@mrabarnett.plus.com> Message-ID: En Mon, 06 Jul 2009 19:49:41 -0300, MRAB escribi?: > Chris Rebert wrote: >> from collections import defaultdict >> counts = defaultdict(lambda: 0) >> > Better is: > counts = defaultdict(int) For speed? This is even faster: zerogen = itertools.repeat(0).next counts = defaultdict(zerogen) -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 6 23:51:09 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 00:51:09 -0300 Subject: How to map size_t using ctypes? References: <7belo1F21fs3vU1@mid.uni-berlin.de> Message-ID: En Mon, 06 Jul 2009 13:29:21 -0300, Philip Semanchuk escribi?: > On Jul 6, 2009, at 12:10 PM, Diez B. Roggisch wrote: >> Philip Semanchuk wrote: >> >>> I can't figure out how to map a C variable of size_t via Python's >>> ctypes module. >> >> from ctypes import c_size_t > > D'oh! [slaps forehead] > > That will teach me to RTFM. [...] You'd be surprised at the amount of > Googling I did without learning this on my own. Yep, seems like these terms in particular are hard to find. Searching for +ctypes +size_t, the first "good" result comes only at page 3. Bad luck... :( -- Gabriel Genellina From aahz at pythoncraft.com Tue Jul 7 00:02:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 6 Jul 2009 21:02:19 -0700 Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: >On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >> kj wrote: >>> >>> sense = cmp(func(hi), func(lo)) >>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >> >> As already said before, unlike other languages, sense in english does >> **not** mean direction. You should rewrite this part using a better >> name. Wrong informations are far worse than no information at all. > >Absolutely. > >From Webster's Dictionary: > > 8. (Geom.) One of two opposite directions in which a line, > surface, or volume, may be supposed to be described by the > motion of a point, line, or surface. > [1913 Webster] > > >And from WordNet: > > 2: the meaning of a word or expression; the way in which a word > or expression or situation can be interpreted > >Both meanings are relevant to the way KJ is using the word. Please take >your own advice and stop giving wrong information. As a native English >speaker, I had no difficulty understanding the meaning of "sense" in the >sense intended by KJ. As another native English speaker, I agree with Jean-Michel; this is the first time I've seen "sense" used to mean direction. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From mail at timgolden.me.uk Tue Jul 7 00:11:30 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Jul 2009 05:11:30 +0100 Subject: copytree with timeout in windows In-Reply-To: <4A52B9D0.9040608@al.com.au> References: <4A52B9D0.9040608@al.com.au> Message-ID: <4A52CAF2.7010707@timgolden.me.uk> Astan Chee wrote: > Hi, > I'm trying to modify the copytree function in shutil so that any file > being copied does not take more than 5 minutes (if it does, skip to the > next one). One suggestion is to use the CopyFileEx API exposed in the win32file module from pywin32. That allows for a callback function which can abort the copy (or issue a progress meter, etc.). Obviously, you'd have to roll your own copytree, but that's not beyond the wit of man. TJG From me at haolian.org Tue Jul 7 00:38:10 2009 From: me at haolian.org (Hao Lian) Date: Tue, 07 Jul 2009 00:38:10 -0400 Subject: a little wsgi framework In-Reply-To: References: Message-ID: <6a368$4a52d120$a652c80b$16349@ALLTEL.NET> timmyt wrote: > i'm interested in getting opinions on a small wsgi framework i > assembled from webob, sqlalchemy, genshi, and various code fragments i > found on the inter-tubes > > here is the interesting glue - any comments / suggestions would be > much appreciated Fun! Since you're already using WebOb, you should give webob.Response a try, which will handle your headers in a sexier way. (If you start using paste.httpexceptions and the middleware, you can avoid manually sending headers altogether.) http://pythonpaste.org/webob/reference.html#response-as-a-wsgi-application http://pythonpaste.org/modules/httpexceptions.html#paste.httpexceptions.HTTPExceptionHandler timmyt wrote: > global config > > try: > return_dict = target(request, start_response) > return_string = template.generate(**return_dict).render > (method) > config['database.Session'].commit() > except: > config['database.Session'].rollback() > raise > finally: > config['database.Session'].remove() The config global probably isn't thread-safe depending on what you're doing with the application. A safer way to go would be use Paste Registry like Pylons does with its globals or just attach it to Request. http://pythonpaste.org/webob/reference.html#ad-hoc-attributes From lie.1296 at gmail.com Tue Jul 7 00:51:51 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 04:51:51 GMT Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: Chris Rebert wrote: > On Mon, Jul 6, 2009 at 1:29 AM, Lawrence > D'Oliveiro wrote: >> In message , Tim Golden >> wrote: >> >>> The difficulty here is knowing where to put such a warning. >>> You obviously can't put it against the "++" operator as such >>> because... there isn't one. >> This bug is an epiphenomenon. :) > > Well, like I suggested, it /could/ be made an operator (or rather, a > lexer token) which just causes a compile/parse error. > > Cheers, > Chris There are edge cases (level: very rare) where you legitimately want to actually do that, e.g.: class A(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return B(self) def __neg__(self): return D(self) class B(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return C(self) def __neg__(self): return A(self) class C(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return D(self) def __neg__(self): return B(self) class D(object): def __init__(self, arg): self.value = arg.value def __pos__(self): return A(self) def __neg__(self): return C(self) def cons(val): class E(object): value = val return E() >>> a = A(cons(10)) >>> +a <__main__.B object at 0x7fbf723c8690> >>> ++a <__main__.C object at 0x7fbf723c8710> >>> +++a <__main__.D object at 0x7fbf723c8710> >>> --a <__main__.C object at 0x7fbf723c8710> From fetchinson at googlemail.com Tue Jul 7 00:52:40 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Mon, 6 Jul 2009 21:52:40 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: >> Yes, there are plenty of languages other than Java and C, but the >> influence of C is admittedly huge in Python. Why do you think loops >> are called "for", conditionals "if" or "while", functions return via >> "return", loops terminate via "break" and keep going via "continue" >> and why is comparison written as "==", etc, etc? All of these are >> coming from C (or an even earlier language) and my point is that users > > for, if, and return were common keywords in FORTRAN. > > Not to mention BASIC > > Both of which predate C Yes, hence my comment above, ".... coming from C (or an even earlier language) ......". Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:05:12 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:05:12 GMT Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006eb26f$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 21:02:19 -0700, Aahz wrote: > In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, Steven D'Aprano > wrote: >>On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >>> kj wrote: >>>> >>>> sense = cmp(func(hi), func(lo)) >>>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >>> >>> As already said before, unlike other languages, sense in english does >>> **not** mean direction. You should rewrite this part using a better >>> name. Wrong informations are far worse than no information at all. >> >>Absolutely. >> >>From Webster's Dictionary: >> >> 8. (Geom.) One of two opposite directions in which a line, >> surface, or volume, may be supposed to be described by the motion >> of a point, line, or surface. >> [1913 Webster] >> >> >>And from WordNet: >> >> 2: the meaning of a word or expression; the way in which a word >> or expression or situation can be interpreted >> >>Both meanings are relevant to the way KJ is using the word. Please take >>your own advice and stop giving wrong information. As a native English >>speaker, I had no difficulty understanding the meaning of "sense" in the >>sense intended by KJ. > > As another native English speaker, I agree with Jean-Michel; this is the > first time I've seen "sense" used to mean direction. Just goes to show you learn something new all the time. http://www.merriam-webster.com/dictionary/sense 7: one of two opposite directions especially of motion (as of a point, line, or surface) http://dictionary.reference.com/browse/sense 18. Mathematics. one of two opposite directions in which a vector may point. Paraphrasing the Collins Dictionary of Mathematics: The sense of a vector is the sign of the measure, contrasted with the magnitude. Thus the vectors AB and BA have the same direction but opposite sense. Sense is also used to distinguish clockwise and anti- clockwise. Sense is, if you like, a "signed direction". "Towards north" (say) as opposed to "along the north-south axis". -- Steven From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:07:08 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:07:08 GMT Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <006eb2e3$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 15:02:26 -0700, Michael Mossey wrote: > On Jul 6, 2:47?pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >> > What is required in a python program to make sure it catches a >> > control- >> > c on the command-line? Do some i/o? The OS here is Linux. >> >> You can use a try/except to catch a KeyboardInterrupt exception, or you >> can trap it using the signal >> module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH >> Philip > > Thanks to both of you. However, my question is also about whether I need > to be doing i/o or some similar operation for my program to notice in > any shape or form that Control-C has been pressed. In the past, I've > written Python programs that go about their business ignoring Ctrl-C. I bet that somewhere in your code you have something like: for x in really_big_list: try: long_running_process(x) except: continue If that's what you're doing, stop! The correct way is: for x in really_big_list: try: long_running_process(x) except Exception: # Let KeyboardInterrupt and SystemExit through. continue or even: for x in really_big_list: try: long_running_process(x) except (KeyboardInterrupt, SystemExit): print "User requested exit... shutting down now" cleanup() raise except Exception: continue -- Steven From lie.1296 at gmail.com Tue Jul 7 01:13:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 05:13:28 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: Aahz wrote: > In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, > Steven D'Aprano wrote: >> On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >>> kj wrote: >>>> sense = cmp(func(hi), func(lo)) >>>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >>> As already said before, unlike other languages, sense in english does >>> **not** mean direction. You should rewrite this part using a better >>> name. Wrong informations are far worse than no information at all. >> Absolutely. >> >>From Webster's Dictionary: >> 8. (Geom.) One of two opposite directions in which a line, >> surface, or volume, may be supposed to be described by the >> motion of a point, line, or surface. >> [1913 Webster] >> >> >> And from WordNet: >> >> 2: the meaning of a word or expression; the way in which a word >> or expression or situation can be interpreted >> >> Both meanings are relevant to the way KJ is using the word. Please take >> your own advice and stop giving wrong information. As a native English >> speaker, I had no difficulty understanding the meaning of "sense" in the >> sense intended by KJ. > > As another native English speaker, I agree with Jean-Michel; this is the > first time I've seen "sense" used to mean direction. When people are fighting over things like `sense`, although sense may not be strictly wrong dictionary-wise, it smells of something burning... From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:13:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:13:30 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> Message-ID: <006eb461$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 04:51:51 +0000, Lie Ryan wrote: > Chris Rebert wrote: >> On Mon, Jul 6, 2009 at 1:29 AM, Lawrence >> D'Oliveiro wrote: >>> In message , Tim >>> Golden wrote: >>> >>>> The difficulty here is knowing where to put such a warning. You >>>> obviously can't put it against the "++" operator as such because... >>>> there isn't one. >>> This bug is an epiphenomenon. :) >> >> Well, like I suggested, it /could/ be made an operator (or rather, a >> lexer token) which just causes a compile/parse error. >> >> Cheers, >> Chris > > There are edge cases (level: very rare) where you legitimately want to > actually do that, e.g.: Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. Help on method __pos__ in module decimal: __pos__(self, context=None) unbound decimal.Decimal method Returns a copy, unless it is a sNaN. Rounds the number (if more then precision digits) -- Steven From casevh at gmail.com Tue Jul 7 01:16:39 2009 From: casevh at gmail.com (casevh) Date: Mon, 6 Jul 2009 22:16:39 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: Message-ID: I discovered a serious bug with comparisons and have posted alpha2 which fixes that bug and adds Unicode support for Python 2.x casevh From clp2 at rebertia.com Tue Jul 7 01:18:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 6 Jul 2009 22:18:20 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <006eb461$0$9711$c3e8da3@news.astraweb.com> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <006eb461$0$9711$c3e8da3@news.astraweb.com> Message-ID: <50697b2c0907062218t34c011ecna831de8aa441d8e1@mail.gmail.com> On Mon, Jul 6, 2009 at 10:13 PM, Steven D'Aprano wrote: > On Tue, 07 Jul 2009 04:51:51 +0000, Lie Ryan wrote: > >> Chris Rebert wrote: >>> On Mon, Jul 6, 2009 at 1:29 AM, Lawrence >>> D'Oliveiro wrote: >>>> In message , Tim >>>> Golden wrote: >>>> >>>>> The difficulty here is knowing where to put such a warning. You >>>>> obviously can't put it against the "++" operator as such because... >>>>> there isn't one. >>>> This bug is an epiphenomenon. :) >>> >>> Well, like I suggested, it /could/ be made an operator (or rather, a >>> lexer token) which just causes a compile/parse error. >>> >>> Cheers, >>> Chris >> >> There are edge cases (level: very rare) where you legitimately want to >> actually do that, e.g.: > > Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. > > > Help on method __pos__ in module decimal: > > __pos__(self, context=None) unbound decimal.Decimal method > ? ?Returns a copy, unless it is a sNaN. > > ? ?Rounds the number (if more then precision digits) Well, yes, but when would you apply it twice in a row? (Not that I strongly support the prohibition idea, just thought it should be brought up) Cheers, Chris -- http://blog.rebertia.com From mensanator at aol.com Tue Jul 7 01:47:21 2009 From: mensanator at aol.com (Mensanator) Date: Mon, 6 Jul 2009 22:47:21 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: Message-ID: On Jul 7, 12:16?am, casevh wrote: > I discovered a serious bug with comparisons and have posted alpha2 > which fixes that bug and adds Unicode support for Python 2.x > > casevh Damn! I was just congatulating myself for pulling off a hat trick (there had been no point in downloading 3.x without gmpy so I have been putting it off): - installing Python 3.1 - installing gmpy 1.10 - converting my Collatz Function library to 3.1 syntax And it all worked smoothly, just had to add parentheses to my print statements, change xrange to range and all my / to // (the library is exclusively integer). I had gmpy running in my library on 3.1 in about 10 minutes. So I'll have to re-do the gmpy install. Shouldn't be any big deal. I started doing some real world tests. Generally, things look good (nothing crashes, timing looks not bad) but I'm getting some funny results on one of my tests, so I'll report back when I have more information. From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 01:57:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 05:57:14 GMT Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006ebea2$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: > When people are fighting over things like `sense`, although sense may > not be strictly wrong dictionary-wise, it smells of something burning... That would be my patience. I can't believe the direction this discussion has taken. Anybody sensible would be saying "Oh wow, I've just learned a new meaning to the word, that's great, I'm now less ignorant than I was a minute ago". But oh no, we mustn't use a standard meaning to a word, heaven forbid we disturb people's ignorance by teaching them something new. It's as simple as this: using `sense` as a variable name to record the sense of a function is not a code smell, any more than using `flag` to record a flag would be, or `sign` to record the sign of an object. If you don't know the appropriate meanings of the words sense, flag or sign, learn them, don't dumb down my language. -- Steven From nomail at thank.you Tue Jul 7 02:01:39 2009 From: nomail at thank.you (jack catcher (nick)) Date: Tue, 07 Jul 2009 09:01:39 +0300 Subject: Python and webcam capture delay? In-Reply-To: References: Message-ID: <6xB4m.21104$vi5.8567@uutiset.elisa.fi> Nobody kirjoitti: > On Mon, 06 Jul 2009 20:41:03 +0300, jack catcher (nick) wrote: > >>> Does the webcam just deliver frames, or are you getting frames out of >>> a decoder layer? If it's the latter, you want to distribute the encoded >>> video, which should be much lower bandwidth. Exactly how you do that >>> depends a bit on what format the webcam claims to deliver. >> Well, getting already encoded video from the webcam sounds almost like a >> free lunch (which it probably is not). At least I wouldn't want to get >> too long a delay because of the encoding. >> >> I haven't got the webcam(s) yet, and I guess I can basically purchase >> any ones I find suitable for getting the job done. Any recommendations? > > The webcam is bound to do some encoding; most of them use USB "full speed" > (12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. > > Chroma subsampling and JPEG compression will both reduce the bandwidth > without introducing noticable latency (the compression time will be no > more than one frame). > > Temporal compression will reduce the bandwidth further. Using B-frames > (frames which contain the differences from a predicted frame which is > based upon both previous and subsequent frames) will provide more > compression, but increases the latency significantly. For this reason, the > compression built into video cameras normally only uses P-frames (frames > which contain the differences from a predicted frame which is based only > upon previous frames). > > The biggest issue is likely to be finding latency specifications, followed > by finding a camera which meets your latency requirement. Thanks for the comments. Unfortunately, such specifications aren't easy to find, even in reviews. Fortunately several newer webcams seem at least to use usb2. Regarding Python, I'll have to check whether the ImageCapture extension works at all, as someone suggested earlier, and the possibility of using DirectShow. I'm still considering Matlab as an option for Python here. From ptmcg at austin.rr.com Tue Jul 7 02:02:44 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Mon, 6 Jul 2009 23:02:44 -0700 (PDT) Subject: parsing times like "5 minutes ago"? References: Message-ID: <90911ef7-ff17-4aa0-9816-4579a638db92@j32g2000yqh.googlegroups.com> On Jul 6, 7:21?pm, m... at pixar.com wrote: > I'm looking for something like Tcl's [clock scan] command which parses > human-readable time strings such as: > > ? ? % clock scan "5 minutes ago" > ? ? 1246925569 > ? ? % clock scan "tomorrow 12:00" > ? ? 1246993200 > ? ? % clock scan "today + 1 fortnight" > ? ? 1248135628 > > Does any such package exist for Python? > > Many TIA! > Mark > > -- > Mark Harrison > Pixar Animation Studios I've been dabbling with such a parser with pyparsing - here is my progress so far: http://pyparsing.wikispaces.com/UnderDevelopment It parses these test cases: today tomorrow yesterday in a couple of days a couple of days from now a couple of days from today in a day 3 days ago 3 days from now a day ago now 10 minutes ago 10 minutes from now in 10 minutes in a minute in a couple of minutes 20 seconds ago in 30 seconds 20 seconds before noon 20 seconds before noon tomorrow noon midnight noon tomorrow -- Paul From wsysdu at gmail.com Tue Jul 7 02:06:13 2009 From: wsysdu at gmail.com (Eric Wong) Date: Tue, 07 Jul 2009 14:06:13 +0800 Subject: why PyObject_VAR_HEAD? References: Message-ID: kio wrote: > Hi, > > I'm studying the CPython source code. I don?t quite understand why > they?re using PyObject_VAR_HEAD to define struct like PyListObject. To > define such kind of struct, could I use _PyObject_HEAD_EXTRA as a > header and add "items" pointer and "allocated" count explicity? Is > there any difference? > > Thanks. > > Eric first, _PyObject_HEAD_EXTRA is only useful for debug and defined as: #ifdef Py_TRACE_REFS #define _PyObject_HEAD_EXTRA \ struct _object *_ob_next; \ struct _object *_ob_prev; #define _PyObject_EXTRA_INIT 0, 0, #else #define _PyObject_HEAD_EXTRA #define _PyObject_EXTRA_INIT #endif and PyObject_HEAD is defined as #define PyObject_HEAD \ _PyObject_HEAD_EXTRA \ int ob_refcnt; \ struct _typeobject *ob_type; PyObject_VAR_HEAD defined as #define PyObject_VAR_HEAD \ PyObject_HEAD \ int ob_size; so you can see the differences between them here. PyListObject is defined as typedef struct{ PyObject_VAR_HEAD PyObject **ob_item; int allocated; }PyListObject; After unfolding these macros we can get typedef struct{ _PyObject_HEAD_EXTRA int ob_refcnt; struct _typeobject *ob_type; int ob_size; PyObject **ob_item; int allocated; }PyListObject; So, if we don't use macros, we have to specify not only the "item" and "allocated", but also the ref count and type information of the object and the size info for a variable object. These information is not only used by PyListObject, but also PyStringObject and PyDictObject, so macros make it convenient and consize. From rogerb at rogerbinns.com Tue Jul 7 02:16:10 2009 From: rogerb at rogerbinns.com (Roger Binns) Date: Mon, 06 Jul 2009 23:16:10 -0700 Subject: Opening a SQLite database in readonly mode In-Reply-To: References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Joshua Kugler wrote: > BTW, APSW is written by the same author as pysqlite. Not even remotely true :-) pysqlite was written by various people, with the maintainer of the last several years being Gerhard H?ring. I am the (sole) author of APSW and have not contributed any code to pysqlite although ideas have flowed freely between the projects and we share a mailing list. I started APSW in late 2004 because I wanted to use SQLite from Python rather than using a layer that pretended SQLite was like other databases. There were various quirks of pysqlite I also didn't like (many since corrected) and so scratched my itch. If you are just doing simple queries then there isn't much apparent difference. If you want to be a "power user" of SQLite then APSW is for you. SQLite has many cool features such as virtual tables (you provide the underlying data for the SQL queries to work on) and VFS (you provide the file access). See this link for more details: http://apsw.googlecode.com/svn/publish/pysqlite.html Roger -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkpS6CUACgkQmOOfHg372QS0/gCgiHD9ukUlQYJGCIMWb9hNMLCM Y/cAnid4dAeFHIdLBmKzGsXrvANkvhR5 =U0wg -----END PGP SIGNATURE----- From nagle at animats.com Tue Jul 7 02:21:02 2009 From: nagle at animats.com (John Nagle) Date: Mon, 06 Jul 2009 23:21:02 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <0260603f$0$20657$c3e8da3@news.astraweb.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <7xvdm8thpo.fsf@ruckus.brouhaha.com> <4a4fe6cb$0$1591$742ec2ed@news.sonic.net> <7xfxdcqabg.fsf@ruckus.brouhaha.com> <4a501a5e$0$1640$742ec2ed@news.sonic.net> <0260559c$0$20657$c3e8da3@news.astraweb.com> <7xws6nik0q.fsf@ruckus.brouhaha.com> <0260603f$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4a52e8ef$0$1633$742ec2ed@news.sonic.net> Steven D'Aprano wrote: > On Sun, 05 Jul 2009 01:58:13 -0700, Paul Rubin wrote: > >> Steven D'Aprano writes: >>> Okay, we get it. Parsing HTML 5 is a bitch. What's your point? I don't >>> see how a case statement would help you here: you're not dispatching on >>> a value, but running through a series of tests until one passes. >> A case statement switch(x):... into a bunch of constant case labels >> would be able to use x as an index into a jump vector, and/or do an >> unrolled logarithmic (bisection-like) search through the tests, instead >> of a linear search. > > Yes, I'm aware of that, but that's not what John's code is doing -- he's > doing a series of if expr ... elif expr tests. I don't think a case > statement can do much to optimize that. (I didn't write that code; it's from "http://code.google.com/p/html5lib/", which is a general purpose HTML 5 parser written in Python. It's compatible with ElementTree and/or BeautifulSoup. I currently use a modified BeautifulSoup for parsing real-world HTML in a small-scale crawler, and I'm looking at this as an HTML 5 compatible replacement.) John Nagle From nagle at animats.com Tue Jul 7 02:31:26 2009 From: nagle at animats.com (John Nagle) Date: Mon, 06 Jul 2009 23:31:26 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <02605ef2$0$20657$c3e8da3@news.astraweb.com> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> Message-ID: <4a52eb5f$0$1663$742ec2ed@news.sonic.net> Steven D'Aprano wrote: > On Sun, 05 Jul 2009 10:12:54 +0200, Hendrik van Rooyen wrote: > >> Python is not C. > > John Nagle is an old hand at Python. He's perfectly aware of this, and > I'm sure he's not trying to program C in Python. > > I'm not entirely sure *what* he is doing, and hopefully he'll speak up > and say, but whatever the problem is it's not going to be as simple as > that. I didn't write this code; I'm just using it. As I said in the original posting, it's from "http://code.google.com/p/html5lib". It's from an effort to write a clean HTML 5 parser in Python for general-purpose use. HTML 5 parsing is well-defined for the awful cases that make older browsers incompatible, but quite complicated. The Python implementation here is intended partly as a reference implementation, so browser writers have something to compare with. I have a small web crawler robust enough to parse real-world HTML, which can be appallingly bad. I currently use an extra-robust version of BeautifulSoup, and even that sometimes blows up. So I'm very interested in a new Python parser which supposedly handles bad HTML in the same way browsers do. But if it's slower than BeautifulSoup, there's a problem. John Nagle From __peter__ at web.de Tue Jul 7 02:33:48 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Jul 2009 08:33:48 +0200 Subject: module name versus function name resolution conflict. References: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Message-ID: rocky wrote: > Someone recently reported a problem in pydb where a function defined > in his program was conflicting with a module name that pydb uses. I > think I understand what's wrong, but I don't have any elegant > solutions to the problem. Suggestions would be appreciated. > > In a nutshell, here's the problem: > > In file fns: > > def foo(): print "foo" > > In file pdebug.py: > > import fns, sys > > def trace_dispatch(frame, event, arg): > fns.foo() > print frame, event, arg > return trace_dispatch > > sys.settrace(trace_dispatch) > execfile('myprogram.py') > > Finally file myprogram.py: > > def fns(): print "This is the *other* fns" > > When you run pdebug.py you get: > > $ python pdebug.py > foo > call None > foo > line None > Traceback (most recent call last): > File "pdebug.py", line 7, in > execfile('myprogram.py') > File "myprogram.py", line 1, in > def fns(): print "This is the *other* fns" > File "pdebug.py", line 3, in trace_dispatch > fns.foo() > AttributeError: 'function' object has no attribute 'foo' > > > Basically inside the trace hook, local functions are visible and take > precedence over (global) module names. I could move "import fns" > inside trace_dispatch(), but I'd have to do that in all of the methods > that module "fns" is used. Also note that using the form: > from fns import foo > > would eliminate the conflict on "fns", but I'd still have potential > conflicts on "foo". Using more obscure names (e.g. pydb_fns) would > reduce the chance of a conflict but not eliminate it. > > Suggestions? Can you run the program in another namespace? execfile("myprogram.py", {"__name__":"__main__"}) Alternatively move trace_dispatch into yet another module and import it into pdebug. from elsewhere import trace_dispatch Peter From wsysdu at gmail.com Tue Jul 7 02:56:40 2009 From: wsysdu at gmail.com (Eric Wong) Date: Tue, 07 Jul 2009 14:56:40 +0800 Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> Message-ID: Pedram wrote: > Hello Mr. Dickinson. Glad to see you again :) > > On Jul 6, 5:46 pm, Mark Dickinson wrote: >> On Jul 6, 1:24 pm, Pedram wrote: >> >> > OK, fine, I read longobject.c at last! :) >> > I found that longobject is a structure like this: >> >> > struct _longobject { >> > struct _object *_ob_next; >> > struct _object *_ob_prev; >> >> For current CPython, these two fields are only present in debug >> builds; for a normal build they won't exist. > > I couldn't understand the difference between them. What are debug > build and normal build themselves? And You mean in debug build > PyLongObject is a doubly-linked-list but in normal build it is just an > array (Or if not how it'll store in this mode)? > we use the macro Py_TRACE_REFS to differ the code for debug build and normal build, that's to say, in debug build and normal build the codes are actually *different*. In debug build, not only PyLongObject but all Objects are linked by a doubly-linked-list and it can make the debug process less painful. But in normal build, objects are seperated! After an object is created, it will never be moved, so we can and should refer to an object only by it's address(pointer). There's no one-big-container like a list or an array for objects. From astan.chee at al.com.au Tue Jul 7 02:56:55 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 7 Jul 2009 16:56:55 +1000 Subject: copytree with timeout in windows In-Reply-To: <4A52CAF2.7010707@timgolden.me.uk> References: <4A52B9D0.9040608@al.com.au> <4A52CAF2.7010707@timgolden.me.uk> Message-ID: <4A52F1B7.2070507@al.com.au> Tim Golden wrote: > Astan Chee wrote: > >> Hi, >> I'm trying to modify the copytree function in shutil so that any file >> being copied does not take more than 5 minutes (if it does, skip to the >> next one). >> > > One suggestion is to use the CopyFileEx API > exposed in the win32file module from pywin32. That > allows for a callback function which can abort > the copy (or issue a progress meter, etc.). > Thanks, but looking in the documentation there is no callback function. Did you mean */the Progress Routine? Thanks again /* > Obviously, you'd have to roll your own copytree, but > that's not beyond the wit of man. > > TJG > -------------- next part -------------- An HTML attachment was scrubbed... URL: From http Tue Jul 7 03:38:50 2009 From: http (Paul Rubin) Date: 07 Jul 2009 00:38:50 -0700 Subject: Looking for a slick way to classify relationship between two numbers, without tons of if/else References: Message-ID: <7x3a99gcxh.fsf@ruckus.brouhaha.com> palewire writes: > In my application, I'd like to have a function that compares two values, > either of which may be null, and then classify the result depending on > whether one is higher or lower than the other. Didn't we just have a huge thread about that? Using a special null value is often (not always) a code smell. But anyway (untested): def compare(a,b): if a is None and b is None: # I'm guessing you meant this return "No data" if a == b: return "Stayed the same" if a != 0 else "Never had" elif a < b: return "gained" else: # we know here that a > b return "Lost some" if b > 0 else "Lost all" I don't know what you intended to do in the case where exactly one of (a,b) is None, so I'll leave that to you. From mail at timgolden.me.uk Tue Jul 7 03:42:55 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 07 Jul 2009 08:42:55 +0100 Subject: copytree with timeout in windows In-Reply-To: <4A52F1B7.2070507@al.com.au> References: <4A52B9D0.9040608@al.com.au> <4A52CAF2.7010707@timgolden.me.uk> <4A52F1B7.2070507@al.com.au> Message-ID: <4A52FC7F.8050308@timgolden.me.uk> Astan Chee wrote: > Tim Golden wrote: >> Astan Chee wrote: >> >>> Hi, >>> I'm trying to modify the copytree function in shutil so that any file >>> being copied does not take more than 5 minutes (if it does, skip to >>> the next one). >> >> One suggestion is to use the CopyFileEx API >> exposed in the win32file module from pywin32. That >> allows for a callback function which can abort >> the copy (or issue a progress meter, etc.). >> > Thanks, but looking in the documentation there is no callback function. > Did you mean */the Progress Routine? Well, depending on which hairs you're splitting, I would argue that the Progress Routine *is* a callback function: it is a function which is called back. But, yes, I did mean that. :) TJG From piet at cs.uu.nl Tue Jul 7 04:01:40 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 10:01:40 +0200 Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Nile (N) wrote: >N> I initialized the dictionary earlier in the program like this - >N> hashtable = {} >N> I changed the "dict" to hashtable but I still get the same result >N> I will try to learn about the defaultdict but I'm just trying to keep >N> it as simple as I can for now >N> Revised code >N> for x in range(len(file_list)): >N> d = open(file_list[x] , "r") >N> data = d.readlines() >N> k = 0 >N> k = above_or_below(data) >N> print "here is the value that was returned ",k >N> hashtable[k] = hashtable.get(k,0) + 1 >N> hashtable_list = hashtable.values() >N> print "here is a list of the dictionary values ", hashtable_list >N> print "the length of the dictionary is ", len(hashtable) >N> Output >N> # The first 3 lines are printed from the function >N> # right before the return statement. This output >N> # snippet shows the last two stocks. The function >N> # SAYS it is returning the correct value but only >N> # the last date seems to make it to the hashtable >N> Function will return k which = 11/11/2008 >N> Function will return k which = 11/12/2008 >N> Function will return k which = 11/14/2008 >N> # this line is printed from the code above >N> # I don't understand why all three dates don't >N> # seem to make it to the main program. Only >N> # the last date seems to be recognized >N> here is the value that was returned 11/14/2008 >N> Function will return k which = 11/11/2008 >N> Function will return k which = 11/12/2008 >N> Function will return k which = 11/14/2008 >N> here is the value that was returned 11/14/2008 >N> here is a list of the dictionary values [5] >N> the length of the dictionary is 1 >>> Exit code: 0 Now in your code there is a 1-1 relation between printing "here is the value that was returned" and incrementing the hashtable entry. In your log there are only 2 prints of "here is the value that was returned" so how can the count be 5? Are you hiding something from us? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From chris at simplistix.co.uk Tue Jul 7 04:06:49 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 09:06:49 +0100 Subject: Where does setuptools live? In-Reply-To: References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> Message-ID: <4A530219.6040007@simplistix.co.uk> David Lyon wrote: > setuptools... as far as I can see isn't actually installed until you > install easyinstall... That depends... I exclusively use buildout to manage my python packages, and sadly that relies on setuptools... > Pip (http://pypi.python.org/pypi/pip) and enstall > (http://pypi.python.org/pypi/Enstaller/3.1.0) seem to be forks of > setuptools already... > > So it looks like it's already been forked to some degree.. > > What hasn't happened is enough testing of pypi packages and installing > with setuptools/pip/enstall from pypi. What needs testing? More important for me is which of these has the most active development community. How do we judge that? > If the xmlrpc links actually "worked" on pypi... What about it doesn't work? Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From duncan.booth at invalid.invalid Tue Jul 7 04:19:47 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 7 Jul 2009 08:19:47 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: Dennis Lee Bieber wrote: > for, if, and return were common keywords in FORTRAN. Really? What does 'for' do in FORTRAN? P.S. Does FORTRAN actually have keywords these days? Back when I learned it there was no such thing as a reserved word though for all I know they may have since added them. -- Duncan Booth http://kupuguy.blogspot.com From afriere at yahoo.co.uk Tue Jul 7 04:48:30 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Tue, 7 Jul 2009 01:48:30 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006eb26f$0$9711$c3e8da3@news.astraweb.com> Message-ID: On Jul 7, 3:05?pm, Steven D'Aprano wrote: [snip] > Sense is, if you like, a "signed direction". Or to put it another way, in the graphical representation of a vector, 'direction' is the line, 'sense' is the arrowhead. From stefan_ml at behnel.de Tue Jul 7 05:09:59 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 11:09:59 +0200 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a52eb5f$0$1663$742ec2ed@news.sonic.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> <4a52eb5f$0$1663$742ec2ed@news.sonic.net> Message-ID: <4a5310e7$0$31327$9b4e6d93@newsspool4.arcor-online.net> John Nagle wrote: > I have a small web crawler robust enough to parse > real-world HTML, which can be appallingly bad. I currently use > an extra-robust version of BeautifulSoup, and even that sometimes > blows up. So I'm very interested in a new Python parser which supposedly > handles bad HTML in the same way browsers do. But if it's slower > than BeautifulSoup, there's a problem. Well, if performance matters in any way, you can always use lxml's blazingly fast parser first, possibly trying a couple of different configurations, and only if all fail, fall back to running html5lib over the same input. That should give you a tremendous speed-up over your current code in most cases, while keeping things robust in the hard cases. Note the numbers that Ian Bicking has for HTML parser performance: http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ You should be able to run lxml's parser ten times in different configurations (e.g. different charset overrides) before it even reaches the time that BeautifulSoup would need to parse a document once. Given that undeclared character set detection is something where BS is a lot better than lxml, you can also mix the best of both worlds and use BS's character set detection to configure lxml's parser if you notice that the first parsing attempts fail. And yes, html5lib performs pretty badly in comparison (or did, at the time). But the numbers seem to indicate that if you can drop the ratio of documents that require a run of html5lib below 30% and use lxml's parser for the rest, you will still be faster than with BeautifulSoup alone. Stefan From dickinsm at gmail.com Tue Jul 7 05:10:31 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 7 Jul 2009 02:10:31 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> Message-ID: <2169e268-3fa7-4320-bef0-2619b8d1d040@h8g2000yqm.googlegroups.com> On Jul 6, 4:13?pm, Pedram wrote: > On Jul 6, 5:46?pm, Mark Dickinson wrote: > > On Jul 6, 1:24?pm, Pedram wrote: > > > > OK, fine, I read longobject.c at last! :) > > > I found that longobject is a structure like this: > > > > struct _longobject { > > > ? ? struct _object *_ob_next; > > > ? ? struct _object *_ob_prev; > > > For current CPython, these two fields are only present in debug > > builds; ?for a normal build they won't exist. > > I couldn't understand the difference between them. What are debug > build and normal build themselves? And You mean in debug build > PyLongObject is a doubly-linked-list but in normal build it is just an > array (Or if not how it'll store in this mode)? No: a PyLongObject is stored the same way (ob_size giving sign and number of digits, ob_digit giving the digits themselves) whether or not a debug build is in use. A debug build does various things (extra checks, extra information) to make it easier to track down problems. On Unix-like systems, you can get a debug build by configuring with the --with-pydebug flag. The _ob_next and _ob_prev fields have nothing particularly to do with Python longs; for a debug build, these two fields are added to *all* Python objects, and provide a doubly-linked list that links all 'live' Python objects together. I'm not really sure what, if anything, the extra information is used for within Python---it might be used by some external tools, I guess. Have you looked at the C-API documentation? http://docs.python.org/c-api/index.html _ob_next and _ob_prev are described here: http://docs.python.org/c-api/typeobj.html#_ob_next (These docs are for Python 2.6; I'm not sure what version you're working with.) Mark From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 05:19:46 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 11:19:46 +0200 Subject: a little wsgi framework In-Reply-To: References: Message-ID: <4a53132c$0$10839$426a34cc@news.free.fr> timmyt a ?crit : > i'm interested in getting opinions on a small wsgi framework i > assembled from webob, sqlalchemy, genshi, and various code fragments i > found on the inter-tubes > > here is the interesting glue - any comments / suggestions would be > much appreciated Well... My first comment would be about the usefulness of yet another Python web framework, but let's not worry about this... > ------------------ > the wsgi app > ------------------ > def application(environ, start_response): > path = environ.get('PATH_INFO', '').lstrip('/') > > for regex, callback in urls: > match = re.search(regex, path) I don't know where these "urls" come from. But anyway : if they live in some sort of long-running process, you may want to precompile them. > if match: > environ['myapp.url_args'] = match.groups() > request = webob.Request(environ) > > try: > return callback(request, start_response) > except Exception, ex: How are redirect etc handled ? > start_response('500 Internal Server Error', [('Content- > Type', 'text/plain')]) > return [traceback.format_exc()] A configuration option controlling the display of the traceback would be fine - I surely don't want the traceback being displayed to anyone when the app goes into production. Also, logging the error and traceback might be useful. > start_response('404 Not Found', [('Content-Type', 'text/plain')]) > return ["Couldn't find the URL specified."] > And in both cases, having the ability to use "custom" 500 and 404 pages might be nice too. > ---------------------------------- > the controller decorator > ---------------------------------- > def web_decorator(filename, method='html'): > > def decorator(target): May I suggest some renaming here ? filename => path (or 'template_path' ?) method => content_type target => controller or function or callback or.... > def wrapper(request, start_response): > > #genshi TemplateLoader > template = loader.load(filename) > > global config Probably not thread-safe.... > try: > return_dict = target(request, start_response) > return_string = template.generate(**return_dict).render > (method) Renaming again: return_dict => context return_string => data or text or ??? > config['database.Session'].commit() > except: > config['database.Session'].rollback() > raise > finally: > config['database.Session'].remove() This doesn't leave much control on transactions... Sometimes you want to handle it manually one way or another. > #TODO: alter 'Content-Type' per method being passed > start_response('200 OK', [('Content-Type', 'text/html')]) > return [return_string] Ok, so here again, I don't understand how redirects can work. Also, if you do use start_response here, I don't get why you still pass it to the callback function. > return wrapper > > return decorator slightly OT, but preserving infos on the decorated function might help too (introspection, debugging etc). My 2 cents... From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 05:27:52 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 11:27:52 +0200 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4a531512$0$9355$426a74cc@news.free.fr> Daniel Fetchinson a ?crit : (snip) > and my point is that users > are most of time correct when they assume that something will work the > same way as in C. Oh, really ? They would surely be wrong if they'd expect the for loop to have any similarity with a C for loop, or - a *very* common error - if they'd expect assignment to work the same way as in C. > So I'd think that putting a warning in a FAQ or a Python Gotchas list > about ++n would be very useful for many users. Might eventually be useful, but I can't hardly recall of more than a couple threads on this topic in 8+ years. From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 05:29:10 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 11:29:10 +0200 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4a531560$0$9355$426a74cc@news.free.fr> Daniel Fetchinson a ?crit : >>> Yes, there are plenty of languages other than Java and C, but the >>> influence of C is admittedly huge in Python. Why do you think loops >>> are called "for", conditionals "if" or "while", functions return via >>> "return", loops terminate via "break" and keep going via "continue" >>> and why is comparison written as "==", etc, etc? All of these are >>> coming from C (or an even earlier language) and my point is that users >> for, if, and return were common keywords in FORTRAN. >> >> Not to mention BASIC >> >> Both of which predate C > > Yes, hence my comment above, ".... coming from C (or an even earlier > language) ......". Mmm... Should we then claim that "the influence of FORTRAN is admittedly huge in Python" ?-) From rocky at gnu.org Tue Jul 7 05:44:07 2009 From: rocky at gnu.org (rocky) Date: Tue, 7 Jul 2009 02:44:07 -0700 (PDT) Subject: module name versus function name resolution conflict. References: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Message-ID: On Jul 7, 2:33?am, Peter Otten <__pete... at web.de> wrote: > rocky wrote: > > Someone recently reported a problem in pydb where a function defined > > in his program was conflicting with amodulenamethat pydb uses. I > > think I understand what's wrong, but I don't have any elegant > > solutions to the problem. Suggestions would be appreciated. > > > In a nutshell, here's the problem: > > > In file fns: > > > ? def foo(): print "foo" > > > In file pdebug.py: > > > ? import fns, sys > > > ? def trace_dispatch(frame, event, arg): > > ? ? ? fns.foo() > > ? ? ? print frame, event, arg > > ? ? ? return trace_dispatch > > > ? sys.settrace(trace_dispatch) > > ? execfile('myprogram.py') > > > Finally file myprogram.py: > > > ? def fns(): print "This is the *other* fns" > > > When you run pdebug.py you get: > > > $ python pdebug.py > > foo > > call None > > foo > > line None > > Traceback (most recent call last): > > ? File "pdebug.py", line 7, in > > ? ? execfile('myprogram.py') > > ? File "myprogram.py", line 1, in > > ? ? def fns(): print "This is the *other* fns" > > ? File "pdebug.py", line 3, in trace_dispatch > > ? ? fns.foo() > > AttributeError: 'function' object has no attribute 'foo' > > > Basically inside the trace hook, local functions are visible and take > > precedence over (global)modulenames. I was sloppy here. The "local" is the wrong word as suggested by the first remedy proposed. "entry in the global namespace dictionary" is probably closer to the truth. > > I could move "import fns" > > inside trace_dispatch(), but I'd have to do that in all of the methods > > thatmodule"fns" is used. ?Also note that using the form: > > ? from fns import foo > > > would eliminate the conflict on "fns", but I'd still have potential > > conflicts on "foo". Using more obscure names (e.g. pydb_fns) would > > reduce the chance of a conflict but not eliminate it. > > > Suggestions? > > Can you run the program in another namespace? > > execfile("myprogram.py", {"__name__":"__main__"}) > > Alternatively move trace_dispatch into yet anothermoduleand import it into > pdebug. > > from elsewhere import trace_dispatch > > Peter Both of these work. Thanks! (I haven't figured out how to adapt it to the messier context of the program yet). The second suggestion interesting/weird: by putting trace_dispatch in another module, compilation and name/ function resolution of fns.foo is done before myprogram has a chance to redefine fns. From lie.1296 at gmail.com Tue Jul 7 06:08:14 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 10:08:14 GMT Subject: Method to separate unit-test methods and data? In-Reply-To: References: Message-ID: Nick Daly wrote: > Hi, > > I was wondering if it's possible / if there are any simple methods > known of storing unit-test functions and their data in separate files? > > Perhaps this is a strange request, but it does an excellent job of > modularizing code. As far as revision control goes, it makes it > easier to discern between new or changed test cases, and changes in > the test data. > > Does anyone have any solutions for these problems? First, is there a > known and simple way to separate unit-test data and methods into > separate files? Secondly, if not, is there a simple way to convert > strings into other base types, like lists, dictionaries, and so forth? > Or, am I going to have to write my own list-parsing methods? Would > pickling help? I haven't yet had a chance to look into if or how that > would work... If there's anything else I can clarify about this > request, feel free to let me know. > It is usually not a very good idea to complicate the testing framework with configuration files (as then you will have two things to be tested). Unless the testing data is very huge (so huge that it interferes with reading and writing the testing code), and huge data is necessary for the test, it is much simpler and better to just put the data into the test code. In cases where you want to have fairly complex object (list, etc) you can "import" the test data. Pickling is usually unnecessary unless you want to store instance data (classes, etc). -- code.py -- def add(a, b): return a + b -- test.py -- import unittest # import the file we're testing import code # imports the data from separate file from testdata import data class AddCase(unittest.TestCase): def test_add(): for case in data: self.assertEqual(code.add(case[0], case[1]), case[2]) -- testdata.py -- data = [(0, 0, 0), (1, 0, 1), (0, 1, 1), (1, -1, 0), ] From pm567426 at gmail.com Tue Jul 7 07:06:07 2009 From: pm567426 at gmail.com (Pedram) Date: Tue, 7 Jul 2009 04:06:07 -0700 (PDT) Subject: How Python Implements "long integer"? References: <53f6b9bf-9661-4baf-b5c7-57a6bfee370d@t13g2000yqt.googlegroups.com> <088097b7-7a6b-4272-991e-60e6b410c68c@37g2000yqp.googlegroups.com> <173653b0-3468-42c0-bb51-d53e7a520917@y17g2000yqn.googlegroups.com> <2169e268-3fa7-4320-bef0-2619b8d1d040@h8g2000yqm.googlegroups.com> Message-ID: <06ec69cc-2183-41b6-92ac-eafaa3b5cd02@n11g2000yqb.googlegroups.com> On Jul 7, 1:10?pm, Mark Dickinson wrote: > On Jul 6, 4:13?pm, Pedram wrote: > > > > > On Jul 6, 5:46?pm, Mark Dickinson wrote: > > > On Jul 6, 1:24?pm, Pedram wrote: > > > > > OK, fine, I read longobject.c at last! :) > > > > I found that longobject is a structure like this: > > > > > struct _longobject { > > > > ? ? struct _object *_ob_next; > > > > ? ? struct _object *_ob_prev; > > > > For current CPython, these two fields are only present in debug > > > builds; ?for a normal build they won't exist. > > > I couldn't understand the difference between them. What are debug > > build and normal build themselves? And You mean in debug build > > PyLongObject is a doubly-linked-list but in normal build it is just an > > array (Or if not how it'll store in this mode)? > > No: ?a PyLongObject is stored the same way (ob_size giving sign and > number of digits, ob_digit giving the digits themselves) whether or > not a debug build is in use. > > A debug build does various things (extra checks, extra information) to > make it easier to track down problems. ?On Unix-like systems, you can > get a debug build by configuring with the --with-pydebug flag. > > The _ob_next and _ob_prev fields have nothing particularly to do with > Python longs; for a debug build, these two fields are added to *all* > Python objects, and provide a doubly-linked list that links all 'live' > Python objects together. ?I'm not really sure what, if anything, the > extra information is used for within Python---it might be used by some > external tools, I guess. > > Have you looked at the C-API documentation? > > http://docs.python.org/c-api/index.html > > _ob_next and _ob_prev are described here: > > http://docs.python.org/c-api/typeobj.html#_ob_next > > (These docs are for Python 2.6; ?I'm not sure what version you're > working with.) > > Mark It seems there's an island named Python! Thanks for links, I'm on reading them. From serverin2000 at yahoo.com Tue Jul 7 07:07:46 2009 From: serverin2000 at yahoo.com (RAM) Date: Tue, 7 Jul 2009 04:07:46 -0700 (PDT) Subject: generation of keyboard events References: Message-ID: On 6 July, 10:02, Simon Brunning wrote: > 2009/7/6 RAM : > > > I am trying to do this on windows. My program(executable) has been > > written in VC++ and when I run this program, I need to click on one > > button on the program GUI i,e just I am entering "Enter key" on the > > key board. But this needs manual process. So i need to write a python > > script which invokes my program and pass "Enter key" event to my > > program so that it runs without manual intervention. > > Try . > > -- > Cheers, > Simon B. Thank you all for the help the below url helped me in accomplishing my task http://www.rutherfurd.net/python/sendkeys/index.html regards Sreerama V From no.email at please.post Tue Jul 7 07:50:01 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 11:50:01 +0000 (UTC) Subject: ISO library ref in printed form Message-ID: Does anyone know where I can buy the Python library reference in printed form? (I'd rather not print the whole 1200+-page tome myself.) I'm interested in both/either 2.6 and 3.0. TIA! kj From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 07:51:11 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 13:51:11 +0200 Subject: Why re.match()? In-Reply-To: References: <4a4e2227$0$7801$426a74cc@news.free.fr> Message-ID: <4a5336a8$0$11315$426a74cc@news.free.fr> kj a ?crit : > In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: > >> kj a ?crit : >> (snipo >>> To have a special-case >>> re.match() method in addition to a general re.search() method is >>> antithetical to language minimalism, > >> FWIW, Python has no pretention to minimalism. > > Assuming that you mean by this that Python's authors have no such > pretensions: > > "There is real value in having a small language." > > Guido van Rossum, 2007.07.03 > http://mail.python.org/pipermail/python-3000/2007-July/008663.html There are some differences between "small" and "minimal"... > So there. > > BTW, that's just one example. I've seen similar sentiments expressed > by Guido over and over and over: any new proposed enhancement to > Python must be good enough in his mind to justify cluttering the > language. That attitude counts as minimalism in my book. And in mine, it counts as "keeping the language's evolution under control" - which is still not the same thing as being "minimalist". If Python really was on the "minimalist" side, you wouldn't even have "class" or "def" statements - both being mostly syntactic sugar. And let's not even talk about @decorator syntax... From dnhkng at googlemail.com Tue Jul 7 08:00:09 2009 From: dnhkng at googlemail.com (Dr Mephesto) Date: Tue, 7 Jul 2009 05:00:09 -0700 (PDT) Subject: Python/pyobjC Apps on iPhone now a possibility? Message-ID: I have been following the discussion about python and pyobjc on the iphone, and it seemed to me that the app-store rules prohibited embedded interpreters; so, python apps are a no-no. But now it seems that the Rubyists have the option that we don't. It seems there is a company, http://rhomobile.com/home, that has an SDK that allows ruby programs to be embedded together with an interpreter in an app! More interesting is the fact that several of these hybrid apps seem to have been accepted on the itunes app store. Here's a quote from a representative, found on this blog: http://www.rubyinside.com/rhodes-develop-full-iphone-rim-and-symbian-apps-using-ruby-1475.html "...First of all, to clarify, we precompile all framework and app code down to Ruby 1.9 VM bytecode. This yields great performance advantages. We also disable eval and other dynamic execution aspects of Ruby. In the end, on all platforms your app gets compiled with our framework all into one single executable, indistinguishable from any other executable. But even if we were shipping a fullon Ruby interpreter without compiling to bytecode and leaving dynamic evaluation enabled (as has been well remarked in the blogosphere by now) App Store rule 3.3.2 does not disallow interpreters but only downloading code to be executed by the interpreter." So, the question is, can the same thing be done for Python apps? From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 08:00:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 07 Jul 2009 12:00:33 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <006eb461$0$9711$c3e8da3@news.astraweb.com> Message-ID: <006f13c9$0$9711$c3e8da3@news.astraweb.com> On Mon, 06 Jul 2009 22:18:20 -0700, Chris Rebert wrote: >> Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. [...] > Well, yes, but when would you apply it twice in a row? My point was that unary + isn't a no-op, and therefore neither is ++. For Decimal, I can't think why you'd want to apply ++x, but for other objects, who knows? Here's a toy example: >>> class Spam(str): ... def __pos__(self): ... return self.__class__("spam " + self) ... >>> s = Spam("") >>> ++++s 'spam spam spam spam ' -- Steven From david.lyon at preisshare.net Tue Jul 7 08:02:07 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 07 Jul 2009 08:02:07 -0400 Subject: Where does setuptools =?UTF-8?Q?live=3F?= In-Reply-To: <4A530219.6040007@simplistix.co.uk> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> Message-ID: On Tue, 07 Jul 2009 09:06:49 +0100, Chris Withers wrote: >> What hasn't happened is enough testing of pypi packages and installing >> with setuptools/pip/enstall from pypi. > > What needs testing? It's software... therefore it needs testing... David From paul.nospam at rudin.co.uk Tue Jul 7 08:03:54 2009 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Tue, 07 Jul 2009 13:03:54 +0100 Subject: Why re.match()? References: <4a4e2227$0$7801$426a74cc@news.free.fr> <4a5336a8$0$11315$426a74cc@news.free.fr> Message-ID: <87prccsnrp.fsf@rudin.co.uk> Bruno Desthuilliers writes: > kj a ?crit : >> In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: >> >>> kj a ?crit : >>> (snipo >>>> To have a special-case >>>> re.match() method in addition to a general re.search() method is >>>> antithetical to language minimalism, >> >>> FWIW, Python has no pretention to minimalism. >> >> Assuming that you mean by this that Python's authors have no such >> pretensions: >> >> "There is real value in having a small language." >> >> Guido van Rossum, 2007.07.03 >> http://mail.python.org/pipermail/python-3000/2007-July/008663.html > > There are some differences between "small" and "minimal"... > There's also a difference between the language and its standard library. From chris at simplistix.co.uk Tue Jul 7 08:07:48 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 13:07:48 +0100 Subject: Where does setuptools live? In-Reply-To: References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> Message-ID: <4A533A94.7030503@simplistix.co.uk> David Lyon wrote: > On Tue, 07 Jul 2009 09:06:49 +0100, Chris Withers > wrote: >>> What hasn't happened is enough testing of pypi packages and installing >>> with setuptools/pip/enstall from pypi. >> What needs testing? > > It's software... therefore it needs testing... Yes, which is why I asked WHAT needs testing? :-) Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From david.lyon at preisshare.net Tue Jul 7 08:21:50 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 07 Jul 2009 08:21:50 -0400 Subject: Where does setuptools =?UTF-8?Q?live=3F?= In-Reply-To: <4A533A94.7030503@simplistix.co.uk> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> <4A533A94.7030503@simplistix.co.uk> Message-ID: On Tue, 07 Jul 2009 13:07:48 +0100, Chris Withers wrote: >>>> What hasn't happened is enough testing of pypi packages and installing >>>> with setuptools/pip/enstall from pypi. >>> What needs testing? >> >> It's software... therefore it needs testing... > > Yes, which is why I asked WHAT needs testing? :-) I've written a package manager gui. I think it is orderly to comprehensively ascertain which packages will and won't install from pypi with the tool. I'll run the same install test for pip, easyinstall and enstall. And come up with a preferred installer. Which I will then suggest as the preferred tool. David From chris at simplistix.co.uk Tue Jul 7 08:28:14 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Tue, 07 Jul 2009 13:28:14 +0100 Subject: Where does setuptools live? In-Reply-To: References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <4A530219.6040007@simplistix.co.uk> <4A533A94.7030503@simplistix.co.uk> Message-ID: <4A533F5E.1080004@simplistix.co.uk> David Lyon wrote: > I've written a package manager gui. I think it is > orderly to comprehensively ascertain which packages will > and won't install from pypi with the tool. > > I'll run the same install test for pip, easyinstall > and enstall. And come up with a preferred installer. > > Which I will then suggest as the preferred tool. Cool :-) If you can publish your results or make the raw data downloadable somewhere (especially which packages fail with which installers) I'm sure a lot of people would find that *very* interesting... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From zdenekmaxa at yahoo.co.uk Tue Jul 7 08:38:16 2009 From: zdenekmaxa at yahoo.co.uk (Zdenek Maxa) Date: Tue, 07 Jul 2009 14:38:16 +0200 Subject: Tkinter.Canvas thread safety problem? Message-ID: <4A5341B8.5000502@yahoo.co.uk> Hello, I have started a project using Tkinter. The application performs some regular checks in a thread and updates Canvas components. I have observed that sometimes the application hangs when it is about to call canvas.itemconfig() when the thread is about to terminate in the next loop. Experimenting with this problem for a while, I have compiled a little example which always reproduces the problem. Commenting out the line 52 (before canvas.itemconfig()), the example always finishes all right, having the delay there, it hangs. I would like to ask if you could have a look at the snippet in the attachment and tell me if that is actually me doing something wrong or indeed Tkinter thread safety problem and what the workaround could be. Could you please also comment on wxPython thread safety? I am using: Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45) [GCC 4.3.3] 2.6.26-1-amd64 #1 SMP Fri Mar 13 19:34:38 UTC 2009 x86_64 GNU/Linux Thanks in advance, Zdenek -------------- next part -------------- A non-text attachment was scrubbed... Name: tkinter_thread_safety.py Type: text/x-python Size: 2278 bytes Desc: not available URL: From lie.1296 at gmail.com Tue Jul 7 08:48:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 07 Jul 2009 12:48:34 GMT Subject: Clarity vs. code reuse/generality In-Reply-To: <006ebea2$0$9711$c3e8da3@news.astraweb.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: > >> When people are fighting over things like `sense`, although sense may >> not be strictly wrong dictionary-wise, it smells of something burning... > > That would be my patience. > > I can't believe the direction this discussion has taken. Me neither. > Anybody sensible > would be saying "Oh wow, I've just learned a new meaning to the word, > that's great, I'm now less ignorant than I was a minute ago". But oh no, > we mustn't use a standard meaning to a word, heaven forbid we disturb > people's ignorance by teaching them something new. A meaning of a word is meaningless if nobody apart the writer understands it. The purpose of code is 1) to communicate with the computer, 2) to communicate with fellow programmer. The second point is especially important if the code are written for pedantic purpose. Teaching is largely one-way communication and often students that does not understand about a slight point could not or would not communicate their feelings because they think it is too silly. If the use of word is criticized on a two-way communication channel (e.g. newsgroup), it should raise a question of whether the word should be described first or whether a synonym would be more suitable for the purpose. Most of these do not apply on practical, non-pedantic purpose though, since in non-pedantic settings you are expected to know and use the jargons however (in)sensible they may be at first sight. > It's as simple as this: using `sense` as a variable name to record the > sense of a function is not a code smell, any more than using `flag` to > record a flag would be, or `sign` to record the sign of an object. Nobody said code smell... linguistic smell is more appropriate. > If you > don't know the appropriate meanings of the words sense, flag or sign, > learn them, don't dumb down my language. From jeanmichel at sequans.com Tue Jul 7 08:51:10 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 07 Jul 2009 14:51:10 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: <006ebea2$0$9711$c3e8da3@news.astraweb.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A5344BE.2080006@sequans.com> Steven D'Aprano wrote: > On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: > > >> When people are fighting over things like `sense`, although sense may >> not be strictly wrong dictionary-wise, it smells of something burning... >> > > That would be my patience. > > I can't believe the direction this discussion has taken. Anybody sensible > would be saying "Oh wow, I've just learned a new meaning to the word, > that's great, I'm now less ignorant than I was a minute ago". But oh no, > we mustn't use a standard meaning to a word, heaven forbid we disturb > people's ignorance by teaching them something new. > > It's as simple as this: using `sense` as a variable name to record the > sense of a function is not a code smell, any more than using `flag` to > record a flag would be, or `sign` to record the sign of an object. If you > don't know the appropriate meanings of the words sense, flag or sign, > learn them, don't dumb down my language. > > Can't we just calm down ? I'm really sorry my ignorance started this thread, and my apologies go to Kj who's obviously more fluent in english than me. I've never used sense in that way before, nor I've seen used by others until now. However Kj is right, and my dictionary seems wrong (wordreference.com). I've searched through others dictionaries and find out this is actually applicable to functions. My bad. JM From davea at ieee.org Tue Jul 7 08:55:13 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 08:55:13 -0400 Subject: Python Error from Apress book In-Reply-To: References: <24364269.post@talk.nabble.com> Message-ID: <4A5345B1.8050306@ieee.org> Gabriel Genellina wrote: >
En Mon, > 06 Jul 2009 19:56:40 -0300, matt0177 escribi?: > >> When I try to run the command as outlined in >> the book "simple_markup2.py < test_input.txt > test_output.html i get >> the >> following error every time. >> >> IOError: [Errno 9] Bad file descriptor > > That's a Windows problem. When you execute the script as itself > (either as you do in the command line, or by double-clicking on it), > it doesn't have valid standard handles. > You have to invoke Python explicitely: > > python simple_markup2.py < test_input.txt > test_output.html > > (you may need to specify the full path to python.exe, or add the > directory where Python is installed to your system PATH). > I use stdout this way all the time, with no problem (python 2.6, Windows XP). But as you point out, stdin redirection doesn't seem to work using the file associations. I do get a different error though. When I look at sys.stdin, it shows an open file, with handle of zero, as expected. But when I do a raw_input(), it gets: EOFError: EOF when reading a line From stefan_ml at behnel.de Tue Jul 7 09:01:07 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 15:01:07 +0200 Subject: parsing times like "5 minutes ago"? In-Reply-To: References: Message-ID: <4a534713$0$31332$9b4e6d93@newsspool4.arcor-online.net> mh at pixar.com wrote: > I'm looking for something like Tcl's [clock scan] command which parses > human-readable time strings such as: > > % clock scan "5 minutes ago" > 1246925569 > % clock scan "tomorrow 12:00" > 1246993200 > % clock scan "today + 1 fortnight" > 1248135628 > > Does any such package exist for Python? Is this only for English times or is I18N a concern? Stefan From python at mrabarnett.plus.com Tue Jul 7 09:07:00 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 14:07:00 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A534874.8050503@mrabarnett.plus.com> Dennis Lee Bieber wrote: > On Mon, 6 Jul 2009 19:48:39 -0700, Daniel Fetchinson > declaimed the following in > gmane.comp.python.general: > >> Yes, there are plenty of languages other than Java and C, but the >> influence of C is admittedly huge in Python. Why do you think loops >> are called "for", conditionals "if" or "while", functions return via >> "return", loops terminate via "break" and keep going via "continue" >> and why is comparison written as "==", etc, etc? All of these are >> coming from C (or an even earlier language) and my point is that users > > for, if, and return were common keywords in FORTRAN. > > Not to mention BASIC > > Both of which predate C > FORTRAN also used "=" for assignment (and ".EQ." for comparison). C was derived from BCPL which used ":=" for assignment and "=" for comparison. From james at agentultra.com Tue Jul 7 09:07:58 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 07 Jul 2009 09:07:58 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? References: Message-ID: <85ljn0ej4h.fsf@agentultra.com> Dr Mephesto writes: > I have been following the discussion about python and pyobjc on the > iphone, and it seemed to me that the app-store rules prohibited > embedded interpreters; so, python apps are a no-no. > > But now it seems that the Rubyists have the option that we don't. It > seems there is a company, http://rhomobile.com/home, that has an SDK > that allows ruby programs to be embedded together with an interpreter > in an app! More interesting is the fact that several of these hybrid > apps seem to have been accepted on the itunes app store. > > Here's a quote from a representative, found on this blog: > http://www.rubyinside.com/rhodes-develop-full-iphone-rim-and-symbian-apps-using-ruby-1475.html > > "...First of all, to clarify, we precompile all framework and app code > down to Ruby 1.9 VM bytecode. This yields great performance > advantages. We also disable eval and other dynamic execution aspects > of Ruby. In the end, on all platforms your app gets compiled with our > framework all into one single executable, indistinguishable from any > other executable. > > But even if we were shipping a fullon Ruby interpreter without > compiling to bytecode and leaving dynamic evaluation enabled (as has > been well remarked in the blogosphere by now) App Store rule 3.3.2 > does not disallow interpreters but only downloading code to be > executed by the interpreter." > > So, the question is, can the same thing be done for Python apps? I love Python and all, but it'd be apt to ask, what's the point? The iPhone is running on what? A 400Mhz ARM processor? Resources on the device are already limited; running your program on top of an embedded Python interpreter would only be adding pressure to the constraints; even if it was an optimized interpreter. Might as well just suck it up and learn C/Objective-C .. it's really not that hard. It took me about a day to pick up the language and another two or three to finagle with the libraries. From davea at ieee.org Tue Jul 7 09:10:14 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 09:10:14 -0400 Subject: module name versus function name resolution conflict. In-Reply-To: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> References: <30998c64-2c9c-4dd6-a495-90423267dd64@32g2000yqj.googlegroups.com> Message-ID: <4A534936.7060300@ieee.org> rocky wrote: > Someone recently reported a problem in pydb where a function defined > in his program was conflicting with a module name that pydb uses. I > think I understand what's wrong, but I don't have any elegant > solutions to the problem. Suggestions would be appreciated. > > In a nutshell, here's the problem: > > In file fns: > > def foo(): print "foo" > > In file pdebug.py: > > import fns, sys > > def trace_dispatch(frame, event, arg): > fns.foo() > print frame, event, arg > return trace_dispatch > > sys.settrace(trace_dispatch) > execfile('myprogram.py') > > Finally file myprogram.py: > > def fns(): print "This is the *other* fns" > > When you run pdebug.py you get: > > $ python pdebug.py > foo > call None > foo > line None > Traceback (most recent call last): > File "pdebug.py", line 7, in > execfile('myprogram.py') > File "myprogram.py", line 1, in > def fns(): print "This is the *other* fns" > File "pdebug.py", line 3, in trace_dispatch > fns.foo() > AttributeError: 'function' object has no attribute 'foo' > > > Basically inside the trace hook, local functions are visible and take > precedence over (global) module names. I could move "import fns" > inside trace_dispatch(), but I'd have to do that in all of the methods > that module "fns" is used. Also note that using the form: > from fns import foo > > would eliminate the conflict on "fns", but I'd still have potential > conflicts on "foo". Using more obscure names (e.g. pydb_fns) would > reduce the chance of a conflict but not eliminate it. > > Suggestions? > > > Some context would be useful. For example, you may be writing pdebug.py as a library to be used by other developers, and you want to minimize the likelihood that arbitrary code they write will conflict. If you own all the code, I'd just say to rename one or the other. My preference is also for using import of myprogram, as I've never needed execfile(). But if you need one of the features of execfile(), and really want myprogram in the same namespace, then you probably ought to obfuscate the other import. Instead of import fns, try: import fns as _module_fns And of course any references to the module will use the longer name. The leading underscore is a clue that the name is not to be used outside of the module. And the prefix 'module' helps to make it unlikely they'll make a data or function or class with that name. From philip at semanchuk.com Tue Jul 7 09:18:32 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Tue, 7 Jul 2009 09:18:32 -0400 Subject: How to map size_t using ctypes? In-Reply-To: References: <7belo1F21fs3vU1@mid.uni-berlin.de> Message-ID: <9DD9E3DD-1C6E-4B12-A9DD-9F768832DF60@semanchuk.com> On Jul 6, 2009, at 11:51 PM, Gabriel Genellina wrote: > En Mon, 06 Jul 2009 13:29:21 -0300, Philip Semanchuk > escribi?: >> On Jul 6, 2009, at 12:10 PM, Diez B. Roggisch wrote: >>> Philip Semanchuk wrote: >>> >>>> I can't figure out how to map a C variable of size_t via Python's >>>> ctypes module. >>> >>> from ctypes import c_size_t >> >> D'oh! [slaps forehead] >> >> That will teach me to RTFM. [...] You'd be surprised at the amount >> of Googling I did without learning this on my own. > > Yep, seems like these terms in particular are hard to find. > Searching for > +ctypes +size_t, the first "good" result comes only at page 3. Bad > luck... > :( Fortunately, through my ignorance I may provide enlightenment...this conversation is now hit #3 on page 1 =) If I paste the URL here, will I create an infinite loop? http://www.google.com/search?q=%2Bctypes+%2Bsize_t From dnhkng at googlemail.com Tue Jul 7 09:23:25 2009 From: dnhkng at googlemail.com (Dr Mephesto) Date: Tue, 7 Jul 2009 06:23:25 -0700 (PDT) Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: Sure, I am learning Objective C already, but the syntax is really unfriendly after python. I think it really depends on the type of app you want to write. Anything held back by network delays or that sits around waiting for user input are perfectly acceptable target apps. If you need speed for something really intensive, falling back to C is still much nicer than coding in Objective C. I agree that having a certain basic understanding of objective C is a must, but having the option to use a coder-friendly language like Ruby or Python can cut development time dramatically. If Ruby can do it (and it generally slower than python), why can Python also get a legal iPhone interpreter? From deets at nospam.web.de Tue Jul 7 09:23:34 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 07 Jul 2009 15:23:34 +0200 Subject: Tkinter.Canvas thread safety problem? References: Message-ID: <7bh0anF22udtoU1@mid.uni-berlin.de> Zdenek Maxa wrote: > Hello, > > I have started a project using Tkinter. The application performs some > regular checks in a thread and updates Canvas components. I have > observed that sometimes the application hangs when it is about to call > canvas.itemconfig() when the thread is about to terminate in the next > loop. > > Experimenting with this problem for a while, I have compiled a little > example which always reproduces the problem. Commenting out the line 52 > (before canvas.itemconfig()), the example always finishes all right, > having the delay there, it hangs. > > I would like to ask if you could have a look at the snippet in the > attachment and tell me if that is actually me doing something wrong or > indeed Tkinter thread safety problem and what the workaround could be. > > Could you please also comment on wxPython thread safety? > As a rule of thumb, GUI-toolkits aren't threadsafe. Qt being a notable exception to this rule. There are various ways to cope with this, mostly through injecting events into the event-queue of the main gui thread, or using timers. This is also true for wx (google wx python threading for a plethora of information on this) For Tx, I dimly remember an "after" function that can be used to invoke timer-based code. Use that to update aggregated data from the events. Diez From bruno.42.desthuilliers at websiteburo.invalid Tue Jul 7 09:23:49 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 07 Jul 2009 15:23:49 +0200 Subject: Why re.match()? In-Reply-To: <87prccsnrp.fsf@rudin.co.uk> References: <4a4e2227$0$7801$426a74cc@news.free.fr> <4a5336a8$0$11315$426a74cc@news.free.fr> <87prccsnrp.fsf@rudin.co.uk> Message-ID: <4a534c5e$0$414$426a74cc@news.free.fr> Paul Rudin a ?crit : > Bruno Desthuilliers writes: > >> kj a ?crit : >>> In <4a4e2227$0$7801$426a74cc at news.free.fr> Bruno Desthuilliers writes: >>> >>>> kj a ?crit : >>>> (snipo >>>>> To have a special-case >>>>> re.match() method in addition to a general re.search() method is >>>>> antithetical to language minimalism, >>>> FWIW, Python has no pretention to minimalism. >>> Assuming that you mean by this that Python's authors have no such >>> pretensions: >>> >>> "There is real value in having a small language." >>> >>> Guido van Rossum, 2007.07.03 >>> http://mail.python.org/pipermail/python-3000/2007-July/008663.html >> There are some differences between "small" and "minimal"... >> > > There's also a difference between the language and its standard > library. Indeed. From pdpinheiro at gmail.com Tue Jul 7 09:37:02 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 06:37:02 -0700 (PDT) Subject: Why re.match()? References: Message-ID: On Jul 2, 4:49?am, kj wrote: > In Duncan Booth writes: > > >So, for example: > >>>> re.compile("c").match("abcdef", 2) > ><_sre.SRE_Match object at 0x0000000002C09B90> > >>>> re.compile("^c").search("abcdef", 2) > > I find this unconvincing; with re.search alone one could simply > do: > > >>> re.compile("^c").search("abcdef"[2:]) given large enough values of "abcdef", you just allocated several megs for no good reason, when re.compile("c").match("abcdef", 2) would process "abcdef" in-place. From davea at ieee.org Tue Jul 7 09:38:37 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 09:38:37 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A534FDD.8030105@ieee.org> Duncan Booth wrote: > Dennis Lee Bieber wrote: > > >> for, if, and return were common keywords in FORTRAN. >> > > Really? What does 'for' do in FORTRAN? > > P.S. Does FORTRAN actually have keywords these days? Back when I learned it > there was no such thing as a reserved word though for all I know they may > have since added them. > > The way I remember it (last used Fortran in 1970), DO was the looping construct, not FOR. Naturally it was uppercase, as keypunches of the time didn't have an easy way to do lowercase. (It was possible, you just had to use multi-punch, and a good memory for the appropriate 0-12-11 prefixes). From piet at cs.uu.nl Tue Jul 7 09:41:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 15:41:46 +0200 Subject: Creating alot of class instances? References: <078efee7-c96f-4075-8074-7b651c23cd86@y7g2000yqa.googlegroups.com> <22e6e4a0-7140-488a-b802-bb94754ff53b@c1g2000yqi.googlegroups.com> <006d4e86$0$9711$c3e8da3@news.astraweb.com> <006e79e9$0$9711$c3e8da3@news.astraweb.com> Message-ID: >>>>> Steven D'Aprano (SD) wrote: >SD> On Mon, 06 Jul 2009 05:47:18 -0700, Scott David Daniels wrote: >>> Steven D'Aprano wrote: >>>> ... That's the Wrong Way to do it -- >>>> you're using a screwdriver to hammer a nail.... >>> >>> Don't knock tool abuse (though I agree with you here). Sometimes tool >>> abuse can produce good results. For example, using hammers to drive >>> screws for temporary strong holds led to making better nails. >SD> Historically, nails were invented a long time before screws. Screws as >SD> fasteners weren't invented until the 1400s, nails were used thousands of >SD> years ago. >SD> And hammering screws makes temporary *weak* holds, not strong, because >SD> the screw only touches the sides of the hole lightly. Unless the screw >SD> has been specifically designed to be hammered, hammering screws is pretty >SD> much the definition of incompetence and/or laziness! In our language (Dutch, i.e. Guido's mother tongue) `American screwdriver' is an expression meaning `hammer' :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nile_mcadams at yahoo.com Tue Jul 7 09:45:12 2009 From: nile_mcadams at yahoo.com (Nile) Date: Tue, 7 Jul 2009 06:45:12 -0700 (PDT) Subject: Semi-Newbie needs a little help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> Message-ID: <97697cdc-e6b3-4277-b26e-d859ee4cd499@h31g2000yqd.googlegroups.com> Thanks all for your help. I appreciate it. The problem was in the function. A simple bug which I should have caught but I had my mental blinders on and was sure the problem was outside the function. The answers have given me a lot to learn so thanks for that as well. From __peter__ at web.de Tue Jul 7 09:50:35 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Jul 2009 15:50:35 +0200 Subject: Tkinter.Canvas thread safety problem? References: Message-ID: Zdenek Maxa wrote: > I would like to ask if you could have a look at the snippet in the > attachment and tell me if that is actually me doing something wrong or > indeed Tkinter thread safety problem and what the workaround could be. http://effbot.org/zone/tkinter-threads.htm From CHausberger at gmx.de Tue Jul 7 09:59:49 2009 From: CHausberger at gmx.de (Claus Hausberger) Date: Tue, 07 Jul 2009 15:59:49 +0200 Subject: Problem reading file with umlauts Message-ID: <20090707135949.100950@gmx.net> Hello I have a text file with is encoding in Latin1 (ISO-8859-1). I can't change that as I do not create those files myself. I have to read those files and convert the umlauts like ? to stuff like &oumol; as the text files should become html files. I have this code: #!/usr/bin/python # -*- coding: latin1 -*- import codecs f = codecs.open('abc.txt', encoding='latin1') for line in f: print line for c in line: if c == "?": print "oe" else: print c and I get this error message: $ ./read.py Abc ./read.py:11: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if c == "?": A b c Traceback (most recent call last): File "./read.py", line 9, in print line UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) I checked the web and tried several approaches but I also get some strange encoding errors. Has anyone ever done this before? I am currently using Python 2.5 and may be able to use 2.6 but I cannot yet move to 3.1 as many libs we use don't yet work with Python 3. any help more than welcome. This has been driving me crazy for two days now. best wishes Claus -- Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate f?r nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02 From stefan_ml at behnel.de Tue Jul 7 10:04:09 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 16:04:09 +0200 Subject: Problem reading file with umlauts In-Reply-To: References: Message-ID: <4a5355d9$0$31339$9b4e6d93@newsspool4.arcor-online.net> Claus Hausberger wrote: > Hello > > I have a text file with is encoding in Latin1 (ISO-8859-1). I can't change that as I do not create those files myself. > > I have to read those files and convert the umlauts like ? to stuff like &oumol; as the text files should become html files. > > I have this code: > > > #!/usr/bin/python > # -*- coding: latin1 -*- > > import codecs > > f = codecs.open('abc.txt', encoding='latin1') > > for line in f: > print line > for c in line: > if c == "?": You are reading Unicode strings, so you have to compare it to a unicode string as in if c == u"?": > print "oe" > else: > print c Note that printing non-ASCII characters may not always work, depending on your terminal. Stefan From sajmikins at gmail.com Tue Jul 7 10:18:30 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 10:18:30 -0400 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> Message-ID: <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> On Mon, Jul 6, 2009 at 7:00 PM, wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? > > Please let me know, > nacim > > > -- > http://mail.python.org/mailman/listinfo/python-list > The answers to your first and third questions are, "yes" and "yes". :] (Generally speaking if something can be done by a computer it can be done with python.) As for your second question check out the "magnitude" package: http://pypi.python.org/pypi/magnitude/ and http://juanreyero.com/magnitude/ (That second link also has links to three other packages that deal with units of measurement.) Ii has units for the SI measurements, including volts and coulombs, so you should be able to accomplish your goals with it. The tricky thing is, as far as I can tell from the wikipedia entry (http://en.wikipedia.org/wiki/Relative_static_permittivity), "dielectric constant" seems to be a dimensionless number, i.e. C/C... I could be totally daft though. HTH, ~Simon From sajmikins at gmail.com Tue Jul 7 10:22:48 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 07:22:48 -0700 (PDT) Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 6, 6:02?pm, Michael Mossey wrote: > On Jul 6, 2:47?pm, Philip Semanchuk wrote: > > > On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > > > > What is required in a python program to make sure it catches a ? > > > control- > > > c on the command-line? Do some i/o? The OS here is Linux. > > > You can use a try/except to catch a KeyboardInterrupt exception, or ? > > you can trap it using the signal module:http://docs.python.org/library/signal.html > > > You want to trap SIGINT. > > > HTH > > Philip > > Thanks to both of you. However, my question is also about whether I > need to be doing i/o or some similar operation for my program to > notice in any shape or form that Control-C has been pressed. In the > past, I've written Python programs that go about their business > ignoring Ctrl-C. Other programs respond to it immediately by exiting. > I think the difference is that the latter programs are doing i/o. But > I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. > > For example, does trapping SIGINT always work, regardless of what my > process is doing? > > Thanks, > Mike Try some experiments. ;] From aahz at pythoncraft.com Tue Jul 7 10:41:59 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Jul 2009 07:41:59 -0700 Subject: Clarity vs. code reuse/generality References: <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Jean-Michel Pichavant wrote: > >Can't we just calm down ? I'm really sorry my ignorance started this >thread, and my apologies go to Kj who's obviously more fluent in english >than me. >I've never used sense in that way before, nor I've seen used by others >until now. However Kj is right, and my dictionary seems wrong >(wordreference.com). I've searched through others dictionaries and find >out this is actually applicable to functions. My bad. You were not the first person to point out that "sense" is a poor variable name. Unless KJ is specifically using this code in the context of a math class, I still think that "sense" is completely inappropriate. Your English fluency is just fine. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From hubaghdadi at gmail.com Tue Jul 7 10:46:18 2009 From: hubaghdadi at gmail.com (Hussein B) Date: Tue, 7 Jul 2009 07:46:18 -0700 (PDT) Subject: Remoting over SSH Message-ID: Hey, I want to perform commands on a remote server over SSH. What do I need? Thanks. From aahz at pythoncraft.com Tue Jul 7 10:47:49 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Jul 2009 07:47:49 -0700 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: In article <85ljn0ej4h.fsf at agentultra.com>, J Kenneth King wrote: > >The iPhone is running on what? A 400Mhz ARM processor? Resources on the >device are already limited; running your program on top of an embedded >Python interpreter would only be adding pressure to the constraints; >even if it was an optimized interpreter. Ten years ago, a 400MHz ARM processor would have been fast, and it would have been running Python 1.5.2. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From sajmikins at gmail.com Tue Jul 7 10:48:16 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 10:48:16 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <4A5344BE.2080006@sequans.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> <4A5344BE.2080006@sequans.com> Message-ID: <50f98a4c0907070748w39481ad9sed8ffba64b299dbc@mail.gmail.com> On Tue, Jul 7, 2009 at 8:51 AM, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> >> On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: >> >> >>> >>> When people are fighting over things like `sense`, although sense may >>> not be strictly wrong dictionary-wise, it smells of something burning... >>> >> >> That would be my patience. >> >> I can't believe the direction this discussion has taken. Anybody sensible >> would be saying "Oh wow, I've just learned a new meaning to the word, that's >> great, I'm now less ignorant than I was a minute ago". But oh no, we mustn't >> use a standard meaning to a word, heaven forbid we disturb people's >> ignorance by teaching them something new. >> >> It's as simple as this: using `sense` as a variable name to record the >> sense of a function is not a code smell, any more than using `flag` to >> record a flag would be, or `sign` to record the sign of an object. If you >> don't know the appropriate meanings of the words sense, flag or sign, learn >> them, don't dumb down my language. >> >> > > Can't we just calm down ? I'm really sorry my ignorance started this thread, > and my apologies go to Kj who's obviously more fluent in english than me. > I've never used sense in that way before, nor I've seen used by others until > now. However Kj is right, and my dictionary seems wrong (wordreference.com). > I've searched through others dictionaries and find out this is actually > applicable to functions. My bad. > > JM Well met, sir. From wuwei23 at gmail.com Tue Jul 7 10:52:56 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 7 Jul 2009 07:52:56 -0700 (PDT) Subject: Remoting over SSH References: Message-ID: On Jul 8, 12:46?am, Hussein B wrote: > I want to perform commands on a remote server over SSH. > What do I need? Take a look at pexpect: http://pexpect.sourceforge.net/pexpect.html From usernet at ilthio.net Tue Jul 7 10:58:34 2009 From: usernet at ilthio.net (Tim Harig) Date: Tue, 07 Jul 2009 14:58:34 GMT Subject: Remoting over SSH References: Message-ID: On 2009-07-07, Hussein B wrote: > I want to perform commands on a remote server over SSH. > What do I need? catb.org/esr/faqs/smart-questions.html There are many ways to remote using ssh. If we know what you are trying to do, maybe we could give you a better answer. If you just want to open the python interpreter interactively on another host you can do something like: ssh -t user at host python That will allow you send "commands" to the python interpeter. You could write a script and pipe it into the interpreter: cat script.py | ssh -t user at host python Maybe you want to open an ssh connection and send shell commands from a python script. You can do that using one of the popen2 functions: http://docs.python.org/library/popen2.html From nacim_bravo at agilent.com Tue Jul 7 11:02:05 2009 From: nacim_bravo at agilent.com (nacim_bravo at agilent.com) Date: Tue, 7 Jul 2009 09:02:05 -0600 Subject: Newbie needs help In-Reply-To: <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> Message-ID: <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> Hello Gurus, Thank you for trying to help to my initial and not well written questions. I will compile more detailed information and ask again. Btw, I am giving a glimpse to: "How To Ask Questions The Smart Way". nacim -----Original Message----- From: Simon Forman [mailto:sajmikins at gmail.com] Sent: Tuesday, July 07, 2009 7:19 AM To: BRAVO,NACIM (A-Sonoma,ex1) Cc: python-list at python.org Subject: Re: Newbie needs help On Mon, Jul 6, 2009 at 7:00 PM, wrote: > Dear Python gurus, > > If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used? > > Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment? > > Please let me know, > nacim > > > -- > http://mail.python.org/mailman/listinfo/python-list > The answers to your first and third questions are, "yes" and "yes". :] (Generally speaking if something can be done by a computer it can be done with python.) As for your second question check out the "magnitude" package: http://pypi.python.org/pypi/magnitude/ and http://juanreyero.com/magnitude/ (That second link also has links to three other packages that deal with units of measurement.) Ii has units for the SI measurements, including volts and coulombs, so you should be able to accomplish your goals with it. The tricky thing is, as far as I can tell from the wikipedia entry (http://en.wikipedia.org/wiki/Relative_static_permittivity), "dielectric constant" seems to be a dimensionless number, i.e. C/C... I could be totally daft though. HTH, ~Simon From matt0177 at GMAIL.COM Tue Jul 7 11:02:22 2009 From: matt0177 at GMAIL.COM (matt0177) Date: Tue, 7 Jul 2009 08:02:22 -0700 (PDT) Subject: Python Error from Apress book In-Reply-To: <24364269.post@talk.nabble.com> References: <24364269.post@talk.nabble.com> Message-ID: <24374988.post@talk.nabble.com> Adding the python before the command line didn't work at first, but upon moving the files to the c:\python25 it worked like a champ. Thank you both for the help. Very frustrating to run into stuff like this when you're first trying to learn a knew language, it really throws off your momentum! -- View this message in context: http://www.nabble.com/Python-Error-from-Apress-book-tp24364269p24374988.html Sent from the Python - python-list mailing list archive at Nabble.com. From motoom at xs4all.nl Tue Jul 7 11:05:32 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Tue, 07 Jul 2009 17:05:32 +0200 Subject: Problem reading file with umlauts In-Reply-To: <20090707135949.100950@gmx.net> References: <20090707135949.100950@gmx.net> Message-ID: <4A53643C.4020905@xs4all.nl> Claus Hausberger wrote: > I have a text file with is encoding in Latin1 (ISO-8859-1). I can't > change that as I do not create those files myself. I have to read > those files and convert the umlauts like ? to stuff like &oumol; as > the text files should become html files. umlaut-in.txt: ---- This file is contains data in the unicode character set and is encoded with utf-8. Viele R?hre. Macht spa?! Ts?sch! umlaut-in.txt hexdump: ---- 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e unicode..chara 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and is 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with utf 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele R..hr 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht spa..! 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 Ts..sch!....... umlaut.py: ---- # -*- coding: utf-8 -*- import codecs text=codecs.open("umlaut-in.txt",encoding="utf-8").read() text=text.replace(u"?",u"oe") text=text.replace(u"?",u"ss") text=text.replace(u"?",u"ue") of=open("umlaut-out.txt","w") of.write(text) of.close() umlaut-out.txt: ---- This file is contains data in the unicode character set and is encoded with utf-8. Viele Roehre. Macht spass! Tsuesch! umlaut-out.txt hexdump: ---- 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e unicode...char 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and is 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with ut 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele Roe 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht spass 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! Tsuesch!..... -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From chengsoon.ong at inf.ethz.ch Tue Jul 7 11:08:20 2009 From: chengsoon.ong at inf.ethz.ch (Cheng Soon Ong) Date: Tue, 07 Jul 2009 17:08:20 +0200 Subject: automatic multiprocessing Message-ID: <4A5364E4.4040607@inf.ethz.ch> Hi all, I'm trying to automate the use of multiprocessing when it is available. The setting I have is quite simple, with a for loop where the operations inside are independent of each other. Here's a bit of code. function_inputs is a list of dictionaries, each of which match the signature of function_handle. if multiprocessing_present: # Passing keyword arguments to map still doesn't work cpus = multiprocessing.Pool() function_outputs = cpus.map(function_handle, function_inputs) else: function_outputs = [] for kwargs in function_inputs: cur_out = function_handle(**kwargs) function_outputs.append(cur_out) Am I missing something here? I cannot seem to get map to pass on keyword arguments. Thanks in advance, Cheng Soon From Scott.Daniels at Acm.Org Tue Jul 7 11:11:27 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 07 Jul 2009 08:11:27 -0700 Subject: ISO library ref in printed form In-Reply-To: References: Message-ID: kj wrote: > Does anyone know where I can buy the Python library reference in > printed form? (I'd rather not print the whole 1200+-page tome > myself.) I'm interested in both/either 2.6 and 3.0. Personally, I'd get the new Beazley's Python Essential Reference, which is due out "real soon now," and then use the provided docs as a addon. Also consider grabbing Gruet's "Python Quick Reference" page. When I was working in a printer site I printed the color version of Gruet's page two-sided; it was neither too bulky nor too sketchy for my needs (and he uses color to distinguish version-to-version changes). http://rgruet.free.fr/ Sadly, I no longer work there, so my copy is gone. :-( --Scott David Daniels Scott.Daniels at Acm.Org From tn.pablo at gmail.com Tue Jul 7 11:12:38 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Tue, 7 Jul 2009 10:12:38 -0500 Subject: Newbie needs help In-Reply-To: <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <527BE7864AF97047BD9F4DEA26124D2B04906B23@cos-us-mb01.cos.agilent.com> <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> Message-ID: On Tue, Jul 7, 2009 at 10:02, wrote: > Hello Gurus, > > Thank you for trying to help to my initial and not well written questions. ?I will compile more detailed information and ask again. ?Btw, I am giving a glimpse to: "How To Ask Questions The Smart Way". > > nacim Give this one a try too: http://www.mikeash.com/getting_answers.html It doesn't talk down to you...as much :P -- Pablo Torres N. From aahz at pythoncraft.com Tue Jul 7 11:29:28 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 Jul 2009 08:29:28 -0700 Subject: ISO library ref in printed form References: Message-ID: In article , kj wrote: > >Does anyone know where I can buy the Python library reference in >printed form? (I'd rather not print the whole 1200+-page tome >myself.) I'm interested in both/either 2.6 and 3.0. There used to be one for Python 2.1, but I can't tell whether it's ever been updated because the website isn't responding: http://wiki.python.org/moin/ReferenceBooks -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From davea at ieee.org Tue Jul 7 11:44:44 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 11:44:44 -0400 Subject: Python Error from Apress book In-Reply-To: <24374988.post@talk.nabble.com> References: <24364269.post@talk.nabble.com> <24374988.post@talk.nabble.com> Message-ID: <4A536D6C.4010308@ieee.org> matt0177 wrote: > Adding the python before the command line didn't work at first, but upon > moving the files to the c:\python25 it worked like a champ. Thank you both > for the help. Very frustrating to run into stuff like this when you're first > trying to learn a knew language, it really throws off your momentum! > I'm glad you got things working. But you really shouldn't need to add your own files to the installation directory. What you need is straightforward, but the details are Windows specific The following is all at the command prompt, not inside Python. You have a PATH environment variable, with various directories on it. Type it out using the PATH command. Pick a location on it, and add a batch file we'll write below. It's also possible to add a new directory to the PATH, using the control panel. Normally, the first thing I do on a new machine is add two directories to the PATH, perhaps c:\bin and c:\bat. The former is for the simple one-file utilities you accumulate over the years, and the latter is for batch files. If you want to pursue this, let me know, and I'll give details. Now that you've picked a location for your batch file, let's create it, calling it Python25.bat You could just call it Python.bat, but this way, you can have more than one Python installed, and choose between them. The contents of Python25.bat are a single line: @c:\python25\python.exe %* The leading @ says we don't wan the line echoed to the screen. If you're unsure of yourself, you can leave it off till everything works well, then add it in. The %* says to copy all the arguments you pass the batch file into the executable. Once this batch file is stored in your PATH, you can just type something like: python25 myscript.py arg1 arg2 or, to run the interpreter itself, python25 DaveA From fetchinson at googlemail.com Tue Jul 7 12:14:48 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 7 Jul 2009 09:14:48 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <4a531512$0$9355$426a74cc@news.free.fr> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: > (snip) >> and my point is that users >> are most of time correct when they assume that something will work the >> same way as in C. > > Oh, really ? They would surely be wrong if they'd expect the for loop to > have any similarity with a C for loop, or - a *very* common error - if > they'd expect assignment to work the same way as in C. Yes, really. I wrote ".... most of the time ....." and not "all the time". >> So I'd think that putting a warning in a FAQ or a Python Gotchas list >> about ++n would be very useful for many users. > > Might eventually be useful, but I can't hardly recall of more than a > couple threads on this topic in 8+ years. I'm happy we agree. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From sajmikins at gmail.com Tue Jul 7 12:16:18 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 09:16:18 -0700 (PDT) Subject: automatic multiprocessing References: mailman.2782.1246980030.8015.python-list@python.org Message-ID: On Jul 7, 11:08?am, Cheng Soon Ong wrote: > Hi all, > > I'm trying to automate the use of multiprocessing when it is available. The > setting I have is quite simple, with a for loop where the operations inside are > independent of each other. Here's a bit of code. function_inputs is a list of > dictionaries, each of which match the signature of function_handle. > > ? ? ?if multiprocessing_present: > ? ? ? ? ?# Passing keyword arguments to map still doesn't work > ? ? ? ? ?cpus = multiprocessing.Pool() > ? ? ? ? ?function_outputs = cpus.map(function_handle, function_inputs) > ? ? ?else: > ? ? ? ? ?function_outputs = [] > ? ? ? ? ?for kwargs in function_inputs: > ? ? ? ? ? ? ?cur_out = function_handle(**kwargs) > ? ? ? ? ? ? ?function_outputs.append(cur_out) > > Am I missing something here? I cannot seem to get map to pass on keyword arguments. > > Thanks in advance, > Cheng Soon Pool.map() doesn't handle "**dict" keyword argument notation automatically. You could use a wrapper function like so: cpus = multiprocessing.Pool() def f(kwargs): return function_handle(**kwargs) function_outputs = cpus.map(f, function_inputs) (Note that f() could be defined outside the if statement if you're going to use it often.) HTH, ~Simon From fetchinson at googlemail.com Tue Jul 7 12:23:30 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 7 Jul 2009 09:23:30 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <4a531560$0$9355$426a74cc@news.free.fr> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531560$0$9355$426a74cc@news.free.fr> Message-ID: >>>> Yes, there are plenty of languages other than Java and C, but the >>>> influence of C is admittedly huge in Python. Why do you think loops >>>> are called "for", conditionals "if" or "while", functions return via >>>> "return", loops terminate via "break" and keep going via "continue" >>>> and why is comparison written as "==", etc, etc? All of these are >>>> coming from C (or an even earlier language) and my point is that users >>> for, if, and return were common keywords in FORTRAN. >>> >>> Not to mention BASIC >>> >>> Both of which predate C >> >> Yes, hence my comment above, ".... coming from C (or an even earlier >> language) ......". > > > Mmm... Should we then claim that "the influence of FORTRAN is admittedly > huge in Python" ?-) Hmmmm, your comments reached a level of pedanticism beyond which I can not follow :) Seriously, ask Guido about the influence of C vs. fortran. Somewhere you can find him quoted as saying that python was originally intended to "bridge the gap between the shell and C". I've never heard him talk about fortran. But this academic discussion is honestly a little pointless. The OP was referring to a expectation, coming from C, that is not fulfilled in python. What's wrong with mentioning it somewhere for the sake of helping C programmers? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From fetchinson at googlemail.com Tue Jul 7 12:29:51 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 7 Jul 2009 09:29:51 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: <4a531512$0$9355$426a74cc@news.free.fr> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: >> and my point is that users >> are most of time correct when they assume that something will work the >> same way as in C. > > Oh, really ? They would surely be wrong if they'd expect the for loop to > have any similarity with a C for loop, or - a *very* common error - if > they'd expect assignment to work the same way as in C. By the way, assignments in conditionals. Guido explicitly referred to C when he forbade assignment in conditionals, citing common typos/errors in C code such as if( x = 5 ){ .... } instead of if( x == 5 ){ ..... }. So even he realized that warning people about different usage in python and C is a good thing. Expectations from C work sometimes, and sometimes they don't. In latter case a little warning is useful. Cheers, Daniel >> So I'd think that putting a warning in a FAQ or a Python Gotchas list >> about ++n would be very useful for many users. > > Might eventually be useful, but I can't hardly recall of more than a > couple threads on this topic in 8+ years. -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From nagle at animats.com Tue Jul 7 12:40:15 2009 From: nagle at animats.com (John Nagle) Date: Tue, 07 Jul 2009 09:40:15 -0700 Subject: Code that ought to run fast, but can't due to Python limitations. In-Reply-To: <4a5310e7$0$31327$9b4e6d93@newsspool4.arcor-online.net> References: <4a4f91f9$0$1587$742ec2ed@news.sonic.net> <02605ef2$0$20657$c3e8da3@news.astraweb.com> <4a52eb5f$0$1663$742ec2ed@news.sonic.net> <4a5310e7$0$31327$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <4a537a11$0$1672$742ec2ed@news.sonic.net> Stefan Behnel wrote: > John Nagle wrote: >> I have a small web crawler robust enough to parse >> real-world HTML, which can be appallingly bad. I currently use >> an extra-robust version of BeautifulSoup, and even that sometimes >> blows up. So I'm very interested in a new Python parser which supposedly >> handles bad HTML in the same way browsers do. But if it's slower >> than BeautifulSoup, there's a problem. > > Well, if performance matters in any way, you can always use lxml's > blazingly fast parser first, possibly trying a couple of different > configurations, and only if all fail, fall back to running html5lib over > the same input. Detecting "fail" is difficult. A common problem is badly terminated comments which eat most of the document if you follow the spec. The document seems to parse correctly, but most of it is missing. The HTML 5 spec actually covers things like and treats it as a bogus comment. (That's because HTML 5 doesn't include general SGML; the only directive recognized is DOCTYPE. Anything else after " References: <20090707135949.100950@gmx.net> Message-ID: <4a537ce8$0$30220$9b4e6d93@newsspool1.arcor-online.net> Michiel Overtoom schrob: > Viele R?hre. Macht spa?! Ts?sch! LOL! :) Stefan From dana_at_work at yahoo.com Tue Jul 7 13:05:07 2009 From: dana_at_work at yahoo.com (dana) Date: Tue, 7 Jul 2009 10:05:07 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? Message-ID: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC modules together. Although I don't have Python 2.5, I've been informed the DBI module has been deprecated at 2.5. A few questions: 1) Although deprecated, will it work at all in 2.5? Does the fact that it is deprecrated mean it has been removed entirely, or does Python 2.5 simply issuing a warning? 2) What do I use in place of DBI for my Python 2.4. scripts that import modules DBI and ODBC together. I don't use DBI directly. It was simply a dependency for the ODBC module as best I knew. Thanks. Dana From python at mrabarnett.plus.com Tue Jul 7 13:10:27 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 18:10:27 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: <4A538183.9020009@mrabarnett.plus.com> Daniel Fetchinson wrote: >>> and my point is that users >>> are most of time correct when they assume that something will work the >>> same way as in C. >> Oh, really ? They would surely be wrong if they'd expect the for loop to >> have any similarity with a C for loop, or - a *very* common error - if >> they'd expect assignment to work the same way as in C. > > By the way, assignments in conditionals. Guido explicitly referred to > C when he forbade assignment in conditionals, citing common > typos/errors in C code such as if( x = 5 ){ .... } instead of if( x == > 5 ){ ..... }. So even he realized that warning people about different > usage in python and C is a good thing. Expectations from C work > sometimes, and sometimes they don't. In latter case a little warning > is useful. > I wonder whether the problem with assignment in conditionals in C is due to the assignment operator being "=". If it was ":=", would the error still occur? From douglas.packard at gmail.com Tue Jul 7 13:34:17 2009 From: douglas.packard at gmail.com (weforgottenuno) Date: Tue, 7 Jul 2009 10:34:17 -0700 (PDT) Subject: Unable to get Tkinter menubar to appear under OS X References: <3737fd18-4973-4cc3-914d-fbdefdbbb8a7@t10g2000vbg.googlegroups.com> Message-ID: <6a9b0831-ce2e-417b-ab4b-419629d0a25d@u16g2000pru.googlegroups.com> I'm having the same problem, though I am just using the pre-installed python and tkinter versions that are on my OS X 10.5 computer, I did not install them on my own. Any advice? -Doug On Jun 24, 9:22?am, Eric Winter wrote: > Hi all. I've googled this issue several times and come up dry. Here's > the situation: > > I have a X-windows build of Tkinter for Python built on an OS X > machine (10.4 or 10.5, same issue). This is not the Aqua version of > Tk, but Tk built from source using X-Window support. I also use a from- > source build of Python, not the system Python. I don't like this > arrangement, but project requirements (allegedly) dictate this > approach. > > When I run any application using that copy of Tkinter and that copy of > Python, everything seems to work fine except menus - the code which > creates the menus runs, but no menus appear, either in the window or > at the top of the screen. > > Am I required to use the Aqua version of Tk on OS X? If not, do I need > to do something special on OS X when I built Tk and/or Python? Any > hints here would be greatly appreciated. > > Thanks, > Eric Winter > Fermi Science Support Center > NASA Goddard Space Flight Center From piet at cs.uu.nl Tue Jul 7 13:38:59 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 19:38:59 +0200 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: >>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >A> In article <85ljn0ej4h.fsf at agentultra.com>, >A> J Kenneth King wrote: >>> >>> The iPhone is running on what? A 400Mhz ARM processor? Resources on the >>> device are already limited; running your program on top of an embedded >>> Python interpreter would only be adding pressure to the constraints; >>> even if it was an optimized interpreter. >A> Ten years ago, a 400MHz ARM processor would have been >A> fast, and it would have been running Python 1.5.2. My first Python experience at home was on a 40MHz 80486 (Python 1.5.2 I think). It was a bit slow :=( -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From hannarosie at gmail.com Tue Jul 7 13:46:47 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Tue, 7 Jul 2009 10:46:47 -0700 Subject: Write matrix to text file Message-ID: <564970db0907071046o60e927dao21cc0557f1fa2903@mail.gmail.com> Hi, I'm working with both python and matlab at the moment and I was wondering if there is an efficient way to take a 2-D array (of 1s and 0s) in python and write it to a text file such that matlab will be able to create a sparse matrix from it later. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Tue Jul 7 13:48:32 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 07 Jul 2009 19:48:32 +0200 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: <4a538a71$0$30236$9b4e6d93@newsspool1.arcor-online.net> Lawrence D'Oliveiro wrote: > I wonder how many people have been tripped up by the fact that > > ++n > > and > > --n > > fail silently for numeric-valued n. I doubt that there are many. Plus, you misspelled them from the more obvious n++ and n-- which *do* fail with a SyntaxError. I think I faintly remember trying those in my early Python days and immediately went for "+=" when I saw them fail (as I had expected). Stefan From robert.oschler at gmail.com Tue Jul 7 14:01:17 2009 From: robert.oschler at gmail.com (roschler) Date: Tue, 7 Jul 2009 11:01:17 -0700 (PDT) Subject: Embedded Python : Why does thread lock here? Message-ID: <00e8b4e8-5386-4f3d-b900-ab1ba41294f2@q40g2000prh.googlegroups.com> I have the Python Intepreter embedded in a Delphi (Object Pascal) program. In the Python script shown below, I have a module that creates a thread object and starts it. The thread's run() call calls a function called deleteOutputVariables() declared at the module level. In my code's first incarnation the thread's run() call would deadlock when trying to call the deleteOutputVariables() function declared at Module level. I would never see the statement "(deleteOutputVariables) Top of Call" printed to the screen. I now know that I must periodically call join() with a very fast time-out to keep Python threads happy, and that solved the problem. However I am curious as to why it deadlocked at the deleteOutputVariables() call? Is it because deleteOutputVariables() is declared at the module level or because that function deletes module level variables? If so why? The code with the relevant parts excerpted is shown below, hopefully the indents hold up. Thanks, Robert ---------------------------- # MODULE: PythonThreadTest.py # FUNCTION: Delete the variables given in the list. def deleteOutputVariables(theModule, theOutputVariablesListOfNames): try: print "(deleteOutputVariables) Top of call." for theOutputVariableName in theOutputVariablesListOfNames: if theModule.__dict__.has_key(theOutputVariableName): print "(Python::deleteOutputVariables) Deleting the Output Variable named " + theOutputVariableName del theModule.__dict__[theOutputVariableName] except: print "(deleteOutputVariables) Exception occurred." # Import needed system modules import sys, os from threading import Thread # ----------------- BEGIN: THREAD class to execute robodanceCommand() class threadRun(Thread): def __init__ (self, theJobID = None): Thread.__init__(self) self.jobCompleted = False # def: __init__ def run(self): try: # NOTE ---> This is where the thread locks if I don't call join(0.001) in # my DELPHI (not Python) loop that waits on the thread to complete. Once # theNewThread.join() is called, execution resumes in # deleteOutputVariables() and the thread completes. deleteOutputVariables(Test_PYTHON, ["PyOut1"]) # Let others know we are done. self.jobCompleted = True except Exception, exc: self.exceptionCaught = exc # Let others know we are done. self.jobCompleted = True print("(Python::threadRun) Exception occurred.") # end: try # def: run() # ----------------- END: THREAD to execute robodanceCommand() theNewThread = None theNewThread = threadRun("TestThread") theNewThread.start() From pdpinheiro at gmail.com Tue Jul 7 14:01:33 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:01:33 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> Message-ID: <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> On Jul 7, 2:16?am, Steven D'Aprano wrote: > On Mon, 06 Jul 2009 16:43:43 +0100, Tim Rowe wrote: > > 2009/7/4 kj : > > >> Precisely. ?As I've stated elsewhere, this is an internal helper > >> function, to be called only a few times under very well-specified > >> conditions. ?The assert statements checks that these conditions are as > >> intended. ?I.e. they are checks against the module writer's programming > >> errors. > > > Good for you. I'm convinced that you have used the assertion > > appropriately, and the fact that so many here are unable to see that > > looks to me like a good case for teaching the right use of assertions. > > For what it's worth, I read assertions at the beginning of a procedure > > as part of the specification of the procedure, and I use them there in > > order to document the procedure. An assertion in that position is for me > > a statement to the user of the procedure "it's your responsibility to > > make sure that you never call this procedure in such a way as to violate > > these conditions". They're part of a contract, as somebody (maybe you) > > pointed out. > > > As somebody who works in the safety-critical domain, it's refreshing to > > see somebody teaching students to think about the circumstances in which > > a procedure can legitimately be called. The hostility you've received to > > that idea is saddening, and indicative of why there's so much buggy > > software out there. > > LOL. > > Maybe the reason for "so much buggy software" is that people > inappropriately use assert, thus changing the behaviour of code depending > on whether it is run with the -O flag or not. > > I don't know what "hostility" you're seeing. The only hostility I'm > seeing is from the OP, which is bizarre considering that he asked for > advice and we gave it. What I see is a bunch of people concerned that the > OP is teaching novices a bad habit, namely, using assert for error > checking. He claims -- angrily and over and over again -- that in his > code, the assertions should never fail. Great. Good for him for knowing > when to use assert. (...) But he doesn't. He asserts: assert lo < hi but then compares: sense = cmp(func(hi), func(lo)) sense can't ever be anything other than 1. I actually find it amusing that this threat got to 50 posts of raving discussion about assertions without anyone spotting that. Personally, I think the code is an unreadable mess, but that's mostly because of all the micro optimizations, not the generality of it. Here's my unoptimized, but still equally generic, version: def _binary_search(lo, hi, func, target, epsilon): sense = cmp(func(hi), func(lo)) if sense == 0: return None guess = (lo + hi) / 2. while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if func(guess) > target: hi = guess elif func(guess) < target: lo = guess elif lo == hi: return None return guess This is a newbie course, right? A while True loop might be faster, but it's all sorts of wrong for teaching newbies. Likewise, calculating a midpoint as mid = (hi + lo) * .5 is an aberration in a beginner environment. You want your students asking why you're calculating an average, not asking why you're multiplying by 0.5. In the same vein, I have no words for your target_plus/target_minus cleverness. The right way to go about it, IMO, is to give them my version, let them digest it, make all the necessary changes to it to turn it into your (faster) version. Present benchmarks for both, then let the readability/performance trade-off sink in. What you achieve with this exercise is that, instead of making your students think "I have to read that crud!?", they'll respect that ugly code is often the result of eking out every last drop of performance from a program as you possibly can. From inky788 at gmail.com Tue Jul 7 14:01:45 2009 From: inky788 at gmail.com (Inky 788) Date: Tue, 7 Jul 2009 11:01:45 -0700 (PDT) Subject: Where does setuptools live? References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> Message-ID: <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> On Jul 7, 4:06?am, Chris Withers wrote: > David Lyon wrote: > > > What hasn't happened is enough testing of pypi packages and installing > > with setuptools/pip/enstall from pypi. > > What needs testing? > > More important for me is which of these has the most active development > community. How do we judge that? Currently, distutils itself is being actively developed. More info about this here: http://tarekziade.wordpress.com/ My (albeit anonymous) advice is: use distutils. Manually download packages as-needed from PyPI and install manually using standard distutils. Read more about distutils here http://wiki.python.org/moin/Distutils and of course in the Python docs. If you want to contribute, my first guess would be that Tarek could use help writing tests (but I don't know what distutils coverage looks like at the moment). When Tarek says, "For package installation that takes care of dependencies, uninstall, etc., use $tool", then I'll start using $tool. Until then, it's just straight distutils for me. From http Tue Jul 7 14:06:59 2009 From: http (Paul Rubin) Date: 07 Jul 2009 11:06:59 -0700 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <7xab3gz7ss.fsf@ruckus.brouhaha.com> pdpi writes: > Personally, I think the code is an unreadable mess, but that's mostly > because of all the micro optimizations, not the generality of it. > Here's my unoptimized, but still equally generic, version: That version doesn't use "sense" inside the binary search, i.e. it relies on the function being monotonically increasing. From andreengels at gmail.com Tue Jul 7 14:26:45 2009 From: andreengels at gmail.com (Andre Engels) Date: Tue, 7 Jul 2009 20:26:45 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <6faf39c90907071126o5c774a79h4de2921fb3af205a@mail.gmail.com> On Tue, Jul 7, 2009 at 8:01 PM, pdpi wrote: > He asserts: > ? ?assert lo < hi > but then compares: > ? ?sense = cmp(func(hi), func(lo)) > > sense can't ever be anything other than 1. It can - there is no necessity that func is monotonically increasing. -- Andr? Engels, andreengels at gmail.com From pdpinheiro at gmail.com Tue Jul 7 14:30:42 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:30:42 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: On Jul 7, 7:26?pm, Andre Engels wrote: > On Tue, Jul 7, 2009 at 8:01 PM, pdpi wrote: > > He asserts: > > ? ?assert lo < hi > > but then compares: > > ? ?sense = cmp(func(hi), func(lo)) > > > sense can't ever be anything other than 1. > > It can - there is no necessity that func is monotonically increasing. > > -- > Andr? Engels, andreeng... at gmail.com Yes, I realized that as I was walking home. In other news, some of you may be interested in my seminar in advanced foot-in-mouth placement. From pdpinheiro at gmail.com Tue Jul 7 14:31:39 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:31:39 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> Message-ID: <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> On Jul 7, 7:06?pm, Paul Rubin wrote: > pdpi writes: > > Personally, I think the code is an unreadable mess, but that's mostly > > because of all the micro optimizations, not the generality of it. > > Here's my unoptimized, but still equally generic, version: > > That version doesn't use "sense" inside the binary search, i.e. it > relies on the function being monotonically increasing. You're right, make that: def _binary_search(lo, hi, func, target, epsilon): sense = cmp(func(hi), func(lo)) if sense == 0: return None guess = (lo + hi) / 2. while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if sense * func(guess) > target: hi = guess elif sense * func(guess) < target: lo = guess elif lo == hi: return None return guess Seems I had a serious brain cramp while posting that... From pdpinheiro at gmail.com Tue Jul 7 14:51:59 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 11:51:59 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> Message-ID: <1f09e856-bd04-4d27-bf51-5d67953a7e13@y7g2000yqa.googlegroups.com> On Jul 7, 7:31?pm, pdpi wrote: > On Jul 7, 7:06?pm, Paul Rubin wrote: > > > pdpi writes: > > > Personally, I think the code is an unreadable mess, but that's mostly > > > because of all the micro optimizations, not the generality of it. > > > Here's my unoptimized, but still equally generic, version: > > > That version doesn't use "sense" inside the binary search, i.e. it > > relies on the function being monotonically increasing. > > You're right, make that: > > def _binary_search(lo, hi, func, target, epsilon): > ? ? sense = cmp(func(hi), func(lo)) > ? ? if sense == 0: > ? ? ? ? return None > ? ? guess = (lo + hi) / 2. > ? ? while abs(func(guess) - target) > epsilon: > ? ? ? ? guess = (lo + hi) / 2. > ? ? ? ? if sense * func(guess) > target: > ? ? ? ? ? ? hi = guess > ? ? ? ? elif sense * func(guess) < target: > ? ? ? ? ? ? lo = guess > ? ? ? ? elif lo == hi: > ? ? ? ? ? ? return None > ? ? return guess > > Seems I had a serious brain cramp while posting that... Actually, scratch that. def _binary_search(lo, hi, func, target, epsilon): sense = cmp(func(hi), func(lo)) if sense == 0: return None guess = (lo + hi) / 2. while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if sense * func(guess) > sense * target: hi = guess elif sense * func(guess) < sense * target: lo = guess elif lo == hi: return None return guess From CHausberger at gmx.de Tue Jul 7 15:00:46 2009 From: CHausberger at gmx.de (Claus Hausberger) Date: Tue, 07 Jul 2009 21:00:46 +0200 Subject: Problem reading file with umlauts In-Reply-To: <4A53643C.4020905@xs4all.nl> References: <20090707135949.100950@gmx.net> <4A53643C.4020905@xs4all.nl> Message-ID: <20090707190046.152740@gmx.net> Thanks a lot. Now I am one step further but I get another strange error: Traceback (most recent call last): File "./read.py", line 12, in of.write(text) UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 0: ordinal not in range(128) according to google ufeff has something to do with byte order. I use an Linux system, maybe this helps to find the error. Claus > Claus Hausberger wrote: > > > I have a text file with is encoding in Latin1 (ISO-8859-1). I can't > > change that as I do not create those files myself. I have to read > > those files and convert the umlauts like ? to stuff like &oumol; as > > the text files should become html files. > > umlaut-in.txt: > ---- > This file is contains data in the unicode > character set and is encoded with utf-8. > Viele R?hre. Macht spa?! Ts?sch! > > > umlaut-in.txt hexdump: > ---- > 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con > 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th > 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e unicode..chara > 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and is > 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with utf > 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele R..hr > 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht spa..! > 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 Ts..sch!....... > > > umlaut.py: > ---- > # -*- coding: utf-8 -*- > import codecs > text=codecs.open("umlaut-in.txt",encoding="utf-8").read() > text=text.replace(u"?",u"oe") > text=text.replace(u"?",u"ss") > text=text.replace(u"?",u"ue") > of=open("umlaut-out.txt","w") > of.write(text) > of.close() > > > umlaut-out.txt: > ---- > This file is contains data in the unicode > character set and is encoded with utf-8. > Viele Roehre. Macht spass! Tsuesch! > > > umlaut-out.txt hexdump: > ---- > 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con > 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th > 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e unicode...char > 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and is > 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with ut > 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele Roe > 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht spass > 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! Tsuesch!..... > > > > > > -- > "The ability of the OSS process to collect and harness > the collective IQ of thousands of individuals across > the Internet is simply amazing." - Vinod Valloppillil > http://www.catb.org/~esr/halloween/halloween4.html -- Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate f?r nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02 From davea at ieee.org Tue Jul 7 15:04:27 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 15:04:27 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <4A539C3B.3040508@ieee.org> pdpi wrote: > On Jul 7, 2:16 am, Steven D'Aprano cybersource.com.au> wrote: > >> On Mon, 06 Jul 2009 16:43:43 +0100, Tim Rowe wrote: >> >>> 2009/7/4 kj : >>> >>>> Precisely. As I've stated elsewhere, this is an internal helper >>>> function, to be called only a few times under very well-specified >>>> conditions. The assert statements checks that these conditions are as >>>> intended. I.e. they are checks against the module writer's programming >>>> errors. >>>> >>> Good for you. I'm convinced that you have used the assertion >>> appropriately, and the fact that so many here are unable to see that >>> looks to me like a good case for teaching the right use of assertions. >>> For what it's worth, I read assertions at the beginning of a procedure >>> as part of the specification of the procedure, and I use them there in >>> order to document the procedure. An assertion in that position is for me >>> a statement to the user of the procedure "it's your responsibility to >>> make sure that you never call this procedure in such a way as to violate >>> these conditions". They're part of a contract, as somebody (maybe you) >>> pointed out. >>> >>> As somebody who works in the safety-critical domain, it's refreshing to >>> see somebody teaching students to think about the circumstances in which >>> a procedure can legitimately be called. The hostility you've received to >>> that idea is saddening, and indicative of why there's so much buggy >>> software out there. >>> >> LOL. >> >> Maybe the reason for "so much buggy software" is that people >> inappropriately use assert, thus changing the behaviour of code depending >> on whether it is run with the -O flag or not. >> >> I don't know what "hostility" you're seeing. The only hostility I'm >> seeing is from the OP, which is bizarre considering that he asked for >> advice and we gave it. What I see is a bunch of people concerned that the >> OP is teaching novices a bad habit, namely, using assert for error >> checking. He claims -- angrily and over and over again -- that in his >> code, the assertions should never fail. Great. Good for him for knowing >> when to use assert. (...) >> > > But he doesn't. > > He asserts: > assert lo < hi > but then compares: > sense =mp(func(hi), func(lo)) > > sense can't ever be anything other than 1. I actually find it amusing > that this threat got to 50 posts of raving discussion about assertions > without anyone spotting that. > > That's because the assert and the comparison are unrelated to each other. If the function is monotonically decreasing, then by definition lo= func(hi), which would yield a sense of 0 or -1. Trivial example of monotonically decreasing: def func(inp): return 53.0 - inp > Personally, I think the code is an unreadable mess, but that's mostly > because of all the micro optimizations, not the generality of it. > Here's my unoptimized, but still equally generic, version: > > def _binary_search(lo, hi, func, target, epsilon): > sense =mp(func(hi), func(lo)) > if sense =0: > return None > guess =lo + hi) / 2. > while abs(func(guess) - target) > epsilon: > guess =lo + hi) / 2. > if func(guess) > target: > hi =uess > elif func(guess) < target: > lo =uess > elif lo =hi: > return None > return guess > > And of course your clarified function will fail if the func is monotonically decreasing. I still think that rather than using sense in the loop, one should simply swap lo and hi, and continue. > This is a newbie course, right? A while True loop might be faster, but > it's all sorts of wrong for teaching newbies. Likewise, calculating a > midpoint as mid =hi + lo) * .5 is an aberration in a beginner > environment. You want your students asking why you're calculating an > average, not asking why you're multiplying by 0.5. In the same vein, I > have no words for your target_plus/target_minus cleverness. > > The right way to go about it, IMO, is to give them my version, let > them digest it, make all the necessary changes to it to turn it into > your (faster) version. Present benchmarks for both, then let the > readability/performance trade-off sink in. What you achieve with this > exercise is that, instead of making your students think "I have to > read that crud!?", they'll respect that ugly code is often the result > of eking out every last drop of performance from a program as you > possibly can. > > (untested) def _binary_search(lo, hi, func, target, epsilon): """ lo, hi are floats representing the desired range of input values to func() func() is a function that takes a float argument, and returns a float result target is the desired result value of func() epsilon is the allowable error compared to target. If set too small, this function will fail by returning None precondition: func is monotonic over the range of floating point inputs from lo to hi """ return a float value between lo and hi (inclusive) which yields approximately target if func(lo) > func(hi): lo, hi = hi, lo if not (func(lo) <= target <= func(hi)): return None #function doesn't have target value within the input range guess = lo while abs(func(guess) - target) > epsilon: guess = (lo + hi) / 2. if func(guess) > target: hi = guess elif func(guess) < target: lo = guess elif lo == hi: return None #function is too steep to have a value within epsilon return guess The reason for the "too steep" condition is that with a finite resolution for 'guess' epsilon could be enough smaller than target as to make a result impossible. For example, if the slope is 45 degrees, this happens when epsilon is about 2**51 smaller. From pdpinheiro at gmail.com Tue Jul 7 15:10:59 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 7 Jul 2009 12:10:59 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> Message-ID: <51f115a9-3e03-49f8-a340-4969133488cf@t33g2000yqe.googlegroups.com> On Jul 7, 8:04?pm, Dave Angel wrote: > And of course your clarified function will fail if the func is > monotonically decreasing. yeah, I eventually realized that and corrected it... And the assert()/ cmp() confusion too. I blame lack of sleep. The whole sign/sense thing left a really bad taste in my mouth, though, and the swapping lo and hi suggestion of yours seems like the neatest solution presented. From davea at ieee.org Tue Jul 7 15:17:42 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 07 Jul 2009 15:17:42 -0400 Subject: Python Error from Apress book In-Reply-To: <8e2cbd660907071048p3a7c3bf9r22026712bf9bc3e4@mail.gmail.com> References: <24364269.post@talk.nabble.com> <24374988.post@talk.nabble.com> <4A536D6C.4010308@ieee.org> <8e2cbd660907071048p3a7c3bf9r22026712bf9bc3e4@mail.gmail.com> Message-ID: <4A539F56.9070109@ieee.org> Matthew Edmondson wrote: > Thanks a ton for the help. At first adding the path didn't work, but after > restarting my computer, ran like a champ :) > > Hopefully I can get decent with this language one day! > > All you needed was to restart the DOS-box (Command Prompt), after you did the control-panel thing. Those changes don't affect currently running processes. . By the way, those "standalone executables" could very well be python scripts. Except for input redirection, all my single-file scripts work fine stored there. And if you add .py and .pyw to the PATHEXT environment variable, you can run them without extension, so they're pretty much interchangeable with .exe files. So I have a script called digest.py, and I run it from a directory I want to analyze, by just typing digest . From dudeja.rajat at gmail.com Tue Jul 7 15:31:07 2009 From: dudeja.rajat at gmail.com (dudeja.rajat at gmail.com) Date: Wed, 8 Jul 2009 01:01:07 +0530 Subject: Check file is locked? Message-ID: How to check if a particular file is locked by some application? (i.e. the file is opened by some application)? -- Regrads, Rajat -------------- next part -------------- An HTML attachment was scrubbed... URL: From no.email at please.post Tue Jul 7 15:37:20 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 19:37:20 +0000 (UTC) Subject: ISO library ref in printed form References: Message-ID: In aahz at pythoncraft.com (Aahz) writes: >In article , kj wrote: >> >>Does anyone know where I can buy the Python library reference in >>printed form? (I'd rather not print the whole 1200+-page tome >>myself.) I'm interested in both/either 2.6 and 3.0. >There used to be one for Python 2.1, but I can't tell whether it's ever >been updated because the website isn't responding: >http://wiki.python.org/moin/ReferenceBooks Hmmm. That's a shame. How is one supposed to keep it under one's pillow??? kj From no.email at please.post Tue Jul 7 15:38:32 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 19:38:32 +0000 (UTC) Subject: ISO library ref in printed form References: Message-ID: In Scott David Daniels writes: >Also consider grabbing Gruet's "Python Quick Reference" page. Not quite what I had in mind, but handy all the same. Thanks. kj From pruebauno at latinmail.com Tue Jul 7 15:51:26 2009 From: pruebauno at latinmail.com (nn) Date: Tue, 7 Jul 2009 12:51:26 -0700 (PDT) Subject: library search path when compiling python Message-ID: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> I am trying to compile python with ssl support but the libraries are not in /usr/lib but in /opt/freeware/lib. How do I add that folder to the default library search path? It looks like configure --libdir=DIR might do the job but I don't want to replace the default lib search path, just add an additional folder to it. From no.email at please.post Tue Jul 7 16:04:46 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 20:04:46 +0000 (UTC) Subject: tough-to-explain Python Message-ID: I'm having a hard time coming up with a reasonable way to explain certain things to programming novices. Consider the following interaction sequence: >>> def eggs(some_int, some_list, some_tuple): ... some_int += 2 ... some_list += [2] ... some_tuple += (2,) ... >>> x = 42 >>> y = (42,) >>> z = [42] >>> eggs(x, y, z) >>> x 42 >>> y (42,) >>> z [42, 2] >>> How do I explain to rank beginners (no programming experience at all) why x and y remain unchanged above, but not z? Or consider this one: >>> ham = [1, 2, 3, 4] >>> spam = (ham,) >>> spam ([1, 2, 3, 4],) >>> spam[0] is ham True >>> spam[0] += [5] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> ham += [5] >>> spam ([1, 2, 3, 4, 5, 5],) >>> What do you say to that? I can come up with much mumbling about pointers and stacks and heaps and much hand-waving about the underlying this-and-that, but nothing that sounds even remotely illuminating. Your suggestions would be much appreciated! TIA! kj From lists at cheimes.de Tue Jul 7 16:06:01 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 07 Jul 2009 22:06:01 +0200 Subject: library search path when compiling python In-Reply-To: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> References: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> Message-ID: <4A53AAA9.9060307@cheimes.de> nn wrote: > I am trying to compile python with ssl support but the libraries are > not in /usr/lib but in /opt/freeware/lib. How do I add that folder to > the default library search path? > > It looks like configure --libdir=DIR might do the job but I don't want > to replace the default lib search path, just add an additional folder > to it. You didn't mention your OS. On Linux you can set the environment variable LD_RUN_PATH prior to compiling Python. The env var sets the rpath for the linker. See man ld(1) for details. Christian From lists at cheimes.de Tue Jul 7 16:07:32 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 07 Jul 2009 22:07:32 +0200 Subject: Check file is locked? In-Reply-To: References: Message-ID: dudeja.rajat at gmail.com wrote: > How to check if a particular file is locked by some application? (i.e. the > file is opened by some application)? It depends on your operating system. By the way most operating systems don't lock a file when it's opened for reading or writing or even executed. Christian From clp2 at rebertia.com Tue Jul 7 16:08:50 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 7 Jul 2009 13:08:50 -0700 Subject: tough-to-explain Python In-Reply-To: References: Message-ID: <50697b2c0907071308u375c81edt49fd2d1a2f6c7c34@mail.gmail.com> On Tue, Jul 7, 2009 at 1:04 PM, kj wrote: > > > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > > Consider the following interaction sequence: > >>>> def eggs(some_int, some_list, some_tuple): > ... ? ? some_int += 2 > ... ? ? some_list += [2] > ... ? ? some_tuple += (2,) > ... >>>> x = 42 >>>> y = (42,) >>>> z = [42] >>>> eggs(x, y, z) >>>> x > 42 >>>> y > (42,) >>>> z > [42, 2] >>>> > > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? > > Or consider this one: > >>>> ham = [1, 2, 3, 4] >>>> spam = (ham,) >>>> spam > ([1, 2, 3, 4],) >>>> spam[0] is ham > True >>>> spam[0] += [5] > Traceback (most recent call last): > ?File "", line 1, in > TypeError: 'tuple' object does not support item assignment >>>> ham += [5] >>>> spam > ([1, 2, 3, 4, 5, 5],) >>>> > > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! You might find the following helpful (partially): http://effbot.org/zone/call-by-object.htm Cheers, Chris -- http://blog.rebertia.com From kevin.p.dwyer at gmail.com Tue Jul 7 16:11:17 2009 From: kevin.p.dwyer at gmail.com (Kevin Dwyer) Date: Tue, 7 Jul 2009 20:11:17 +0000 (UTC) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: Hello, I think this is discussed PEP 249 - see the "major changes" section. http://www.python.org/dev/peps/pep-0249/ Kev On Tue, 07 Jul 2009 10:05:07 -0700, dana wrote: > I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC > modules together. Although I don't have Python 2.5, I've been informed > the DBI module has been deprecated at 2.5. A few questions: > > 1) Although deprecated, will it work at all in 2.5? Does the fact that > it is deprecrated mean it has been removed entirely, or does Python 2.5 > simply issuing a warning? > > 2) What do I use in place of DBI for my Python 2.4. scripts that import > modules DBI and ODBC together. I don't use DBI directly. It was simply a > dependency for the ODBC module as best I knew. > > Thanks. > > Dana From deets at nospam.web.de Tue Jul 7 16:15:40 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 07 Jul 2009 22:15:40 +0200 Subject: ISO library ref in printed form In-Reply-To: References: Message-ID: <7bhoncF23eocqU1@mid.uni-berlin.de> kj schrieb: > In aahz at pythoncraft.com (Aahz) writes: > >> In article , kj wrote: >>> Does anyone know where I can buy the Python library reference in >>> printed form? (I'd rather not print the whole 1200+-page tome >>> myself.) I'm interested in both/either 2.6 and 3.0. > >> There used to be one for Python 2.1, but I can't tell whether it's ever >> been updated because the website isn't responding: > >> http://wiki.python.org/moin/ReferenceBooks > > Hmmm. That's a shame. How is one supposed to keep it under one's > pillow??? That's what netbooks were created for... Diez From piet at cs.uu.nl Tue Jul 7 16:37:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 22:37:56 +0200 Subject: ISO library ref in printed form References: Message-ID: >>>>> kj (kj) wrote: >kj> Does anyone know where I can buy the Python library reference in >kj> printed form? (I'd rather not print the whole 1200+-page tome >kj> myself.) I'm interested in both/either 2.6 and 3.0. Maybe you can have a copy printed at lulu.com. It would even be nicer if the PSF would offer them at lulu. They could even make some money from it if enough copies would be sold.. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at mrabarnett.plus.com Tue Jul 7 16:40:20 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 07 Jul 2009 21:40:20 +0100 Subject: Problem reading file with umlauts In-Reply-To: <20090707190046.152740@gmx.net> References: <20090707135949.100950@gmx.net> <4A53643C.4020905@xs4all.nl> <20090707190046.152740@gmx.net> Message-ID: <4A53B2B4.9090409@mrabarnett.plus.com> Claus Hausberger wrote: > Thanks a lot. Now I am one step further but I get another strange error: > > Traceback (most recent call last): > File "./read.py", line 12, in > of.write(text) > UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 0: ordinal not in range(128) > > according to google ufeff has something to do with byte order. > > I use an Linux system, maybe this helps to find the error. > 'text' contains Unicode, but you're writing it to a file that's not opened for Unicode. Either open the output file for Unicode: of = codecs.open("umlaut-out.txt", "w", encoding="latin1") or encode the text before writing: text = text.encode("latin1") (I'm assuming you want the output file to be in Latin1.) > >> Claus Hausberger wrote: >> >>> I have a text file with is encoding in Latin1 (ISO-8859-1). I can't >>> change that as I do not create those files myself. I have to read >>> those files and convert the umlauts like ? to stuff like &oumol; as >>> the text files should become html files. >> umlaut-in.txt: >> ---- >> This file is contains data in the unicode >> character set and is encoded with utf-8. >> Viele R?hre. Macht spa?! Ts?sch! >> >> >> umlaut-in.txt hexdump: >> ---- >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e unicode..chara >> 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and is >> 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with utf >> 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele R..hr >> 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht spa..! >> 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 Ts..sch!....... >> >> >> umlaut.py: >> ---- >> # -*- coding: utf-8 -*- >> import codecs >> text=codecs.open("umlaut-in.txt",encoding="utf-8").read() >> text=text.replace(u"?",u"oe") >> text=text.replace(u"?",u"ss") >> text=text.replace(u"?",u"ue") >> of=open("umlaut-out.txt","w") >> of.write(text) >> of.close() >> >> >> umlaut-out.txt: >> ---- >> This file is contains data in the unicode >> character set and is encoded with utf-8. >> Viele Roehre. Macht spass! Tsuesch! >> >> >> umlaut-out.txt hexdump: >> ---- >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is con >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in th >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e unicode...char >> 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and is >> 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with ut >> 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele Roe >> 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht spass >> 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! Tsuesch!..... >> >> >> >> >> >> -- >> "The ability of the OSS process to collect and harness >> the collective IQ of thousands of individuals across >> the Internet is simply amazing." - Vinod Valloppillil >> http://www.catb.org/~esr/halloween/halloween4.html > From piet at cs.uu.nl Tue Jul 7 16:53:13 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 07 Jul 2009 22:53:13 +0200 Subject: tough-to-explain Python References: Message-ID: >>>>> kj (k) wrote: >k> I'm having a hard time coming up with a reasonable way to explain >k> certain things to programming novices. >k> Consider the following interaction sequence: >>>>> def eggs(some_int, some_list, some_tuple): >k> ... some_int += 2 >k> ... some_list += [2] >k> ... some_tuple += (2,) >k> ... >>>>> x = 42 >>>>> y = (42,) >>>>> z = [42] >>>>> eggs(x, y, z) >>>>> x >k> 42 >>>>> y >k> (42,) >>>>> z >k> [42, 2] >>>>> >k> How do I explain to rank beginners (no programming experience at >k> all) why x and y remain unchanged above, but not z? You shouldn't. That's not for beginners. Leave it waiing until you get to the advanced level. >k> Or consider this one: >>>>> ham = [1, 2, 3, 4] >>>>> spam = (ham,) >>>>> spam >k> ([1, 2, 3, 4],) >>>>> spam[0] is ham >k> True >>>>> spam[0] += [5] >k> Traceback (most recent call last): >k> File "", line 1, in >k> TypeError: 'tuple' object does not support item assignment >>>>> ham += [5] >>>>> spam >k> ([1, 2, 3, 4, 5, 5],) >>>>> >k> What do you say to that? Mutable and immutable. But use different examples. Like ham = [1, 2, 3, 4] spam = (1, 2, 3, 4) spam[0] += 1 will give the same error message. You can't change the components of a tuple. Your example above is similar. The spam[0] += [5] appends the 5 to the list in spam[0] (so it appends to ham), and then tries to assign the result of it to spam[0], which is not allowed. That the item it tries to assign is the same as the item that was already there doesn't matter. So dont't forget += is a real assignment, even when it is an in-place modification. Your example just proves that. The language ref manual says: With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. But I think that your example isn't for beginners either. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From joshua at joshuakugler.com Tue Jul 7 17:07:55 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Tue, 07 Jul 2009 13:07:55 -0800 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: Roger Binns wrote: > Joshua Kugler wrote: >> BTW, APSW is written by the same author as pysqlite. > Not even remotely true :-) Sorry about that...since pysqlite and APSW are both discusses on the pysqlite list, I had made an incorrect assumption. Oops. j From no.email at please.post Tue Jul 7 17:11:29 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 21:11:29 +0000 (UTC) Subject: tough-to-explain Python References: Message-ID: In Piet van Oostrum writes: >>>>>> kj (k) wrote: >>k> I'm having a hard time coming up with a reasonable way to explain >>k> certain things to programming novices. >>k> Consider the following interaction sequence: >>>>>> def eggs(some_int, some_list, some_tuple): >>k> ... some_int += 2 >>k> ... some_list += [2] >>k> ... some_tuple += (2,) >>k> ... >>>>>> x = 42 >>>>>> y = (42,) >>>>>> z = [42] >>>>>> eggs(x, y, z) >>>>>> x >>k> 42 >>>>>> y >>k> (42,) >>>>>> z >>k> [42, 2] >>>>>> >>k> How do I explain to rank beginners (no programming experience at >>k> all) why x and y remain unchanged above, but not z? >You shouldn't. That's not for beginners. No, of course not. And I don't plan to present these examples to them. But beginners have a way of bumping into such conundrums all on their own, and, as a former beginner myself, I can tell you that they find them, at best, extremely frustrating, and at worst, extremely discouraging. I doubt that an answer of the form "don't worry your pretty little head over this now; wait until your second course" will do the trick. Thanks for your comments! kj From no.email at please.post Tue Jul 7 17:18:53 2009 From: no.email at please.post (kj) Date: Tue, 7 Jul 2009 21:18:53 +0000 (UTC) Subject: tough-to-explain Python References: Message-ID: In Chris Rebert writes: >You might find the following helpful (partially): >http://effbot.org/zone/call-by-object.htm Extremely helpful. Thanks! (I learned more from it than someone who will teach the stuff would care to admit...) I had not realized how *profoundly* different the meaning of the "=" in Python's spam = ham is from the "=" in its spam[3] = ham[3] So much for "explicit is better than implicit"... And it confirmed Paul Graham's often-made assertion that all of programming language design is still catching up to Lisp... kj From james at agentultra.com Tue Jul 7 17:21:26 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 07 Jul 2009 17:21:26 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: <85hbxodwa1.fsf@agentultra.com> Dr Mephesto writes: > Sure, I am learning Objective C already, but the syntax is really > unfriendly after python. > > I think it really depends on the type of app you want to write. > Anything held back by network delays or that sits around waiting for > user input are perfectly acceptable target apps. If you need speed for > something really intensive, falling back to C is still much nicer than > coding in Objective C. I agree that having a certain basic > understanding of objective C is a must, but having the option to use a > coder-friendly language like Ruby or Python can cut development time > dramatically. > > If Ruby can do it (and it generally slower than python), why can > Python also get a legal iPhone interpreter? It can if you want to write the interpreter. I just don't see the point. I can understand wanting a higher level language than assembler or maybe even C, but that's precisely what Objective-C is. Unfortunately, while the hardware for these mobile platforms is getting better these days, it's still not where it needs to be to run programs written in interpreted languages, IMO. Users typically only interact with an app for around two minutes at a time. Your app needs to be as fast as it can be. It's one of the few areas of software development today where optimizations can be justified (the other are games and scientific computing). Trust me, if there were an SMS app for the iPhone that loaded faster than the one that ships with it, guaranteed it would sell like hot-cakes (if Apple would let it in the store of course). If you do write the interpreter, let me know. I would certainly experiment with it. From bearophileHUGS at lycos.com Tue Jul 7 17:46:01 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 7 Jul 2009 14:46:01 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: kj, as Piet van Oostrum as said, that's the difference between mutable an immutable. It comes from the procedural nature of Python, and probably an explanation of such topic can't be avoided if you want to learn/teach Python. Lot of people think that a language where everything is immutable is simpler for newbies to understand (even if they have to learn what higher-order functions are). That's why some people think Scheme or other languages (even Clojure, I guess) are better for novices (Scheme has mutability, but you can avoid showing it to newbies). Some people say that languages that mostly encourage the use of immutable data structures (like F#, Clojure, Scala and many other less modern ones) help avoid bugs, maybe for novices too. On the other hand, it's hard or impossible to actually remove complexity from a system, and you usually just move it elsewhere. So other things will become harder to do for those novices. I have no idea if for the average novice it's simpler to learn to use a immutables-based language instead of a mutables-based one (it can also be possible that some novices prefer the first group, and other novices prefer the second group). >From what I have seen lot of students seem able to learn Python, so it's not a bad choice. Python isn't perfect, and in *many* situations it is pragmatic, for example to increase its performance. Generally for a novice programmer running speed is not important, but it's important to have a really coherent and clean language. I've personally seen that for such people even Python looks very "dirty" (even if it's one of the less dirty ones). For example a novice wants to see 124 / 38 to return the 62/19 fraction and not 3 or 3.263157894736842 :-) People may be able to invent a clean and orthogonal language that's easy to use and has very few compromises, fit for newbies. But this language may be very slow, not much useful in practice (who knows? Maybe there's a practical niche even for such very high level language), and it doesn't teach how to use lower level languages like C :-) Today I think there are no languages really fit for teaching. Python is one of the few fit ones, but it's getting more and more complex as time passes because it's getting used in more and more complex real world situations (a language fit for newbies must not have abstract base classes, decorators, etc). D language is too much complex for a newbie. Java is not too much bad, but it's requires to write too much code, it's too much fussy (semicolons at the end of lines? Newbies say that the computer is an idiot when it refuses code just because there's a missing useless semicolon!), and it misses some necessary things (first class functions! Damn). A nice language like Boo running on the Mono VM seems another option :-) In the past Pascal was good enough, but I think it's not good enough anymore. The problem is that teaching is a niche activity (even if a very important one). PLT Scheme is one of the few environments (beside Squeak, Python itself, and little more) that look refined and implemented well enough for such purpose. See you later, bearophile From andrew.henshaw at gtri.gatech.edu Tue Jul 7 17:48:37 2009 From: andrew.henshaw at gtri.gatech.edu (Andrew Henshaw) Date: Tue, 7 Jul 2009 17:48:37 -0400 Subject: finding most common elements between thousands of multiple arrays. References: Message-ID: "mclovin" wrote in message news:c5332c9b-2348-4194-bfa0-d70c7710765d at x3g2000yqa.googlegroups.com... > Currently I need to find the most common elements in thousands of > arrays within one large array (arround 2 million instances with ~70k > unique elements) > > so I set up a dictionary to handle the counting so when I am > iterating I up the count on the corrosponding dictionary element. I > then iterate through the dictionary and find the 25 most common > elements. > > the elements are initially held in a array within an array. so I am am > just trying to find the most common elements between all the arrays > contained in one large array. > my current code looks something like this: > d = {} > for arr in my_array: > -----for i in arr: > #elements are numpy integers and thus are not accepted as dictionary > keys > -----------d[int(i)]=d.get(int(i),0)+1 > > then I filter things down. but with my algorithm that only takes about > 1 sec so I dont need to show it here since that isnt the problem. > > > But there has to be something better. I have to do this many many > times and it seems silly to iterate through 2 million things just to > get 25. The element IDs are integers and are currently being held in > numpy arrays in a larger array. this ID is what makes up the key to > the dictionary. > > It currently takes about 5 seconds to accomplish this with my current > algorithm. > > So does anyone know the best solution or algorithm? I think the trick > lies in matrix intersections but I do not know. Would the following work for you, or am I missing something? For a 5Kx5K array, this takes about a tenth of a second on my machine. This code doesn't deal with the sub-array issue. ##################### import numpy import time LOWER = 0 UPPER = 1024 SIZE = 5000 NUM_BEST = 4 # sample data data = numpy.random.randint(LOWER, UPPER, (SIZE,SIZE)).astype(int) time.clock() count = numpy.bincount(data.flat) best = sorted(zip(count, range(len(count))))[-NUM_BEST:] print 'time=', time.clock() print best From python at bdurham.com Tue Jul 7 17:54:14 2009 From: python at bdurham.com (python at bdurham.com) Date: Tue, 07 Jul 2009 17:54:14 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? In-Reply-To: <85hbxodwa1.fsf@agentultra.com> References: <85ljn0ej4h.fsf@agentultra.com> <85hbxodwa1.fsf@agentultra.com> Message-ID: <1247003654.9346.1323970563@webmail.messagingengine.com> > If you do write the interpreter, let me know. I would certainly experiment with it. +2 over here! Malcolm From nobody at nowhere.com Tue Jul 7 18:37:43 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 07 Jul 2009 23:37:43 +0100 Subject: Python and webcam capture delay? References: <6xB4m.21104$vi5.8567@uutiset.elisa.fi> Message-ID: On Tue, 07 Jul 2009 09:01:39 +0300, jack catcher (nick) wrote: > Thanks for the comments. Unfortunately, such specifications aren't easy > to find, even in reviews. Fortunately several newer webcams seem at > least to use usb2. Supporting USB-2 doesn't mean that the camera necessarily uses high-speed (480Mbit/sec). AFAIK, the only real difference between "USB-1 conformant" and "USB-2 conformant" is that the latter actually passed a test of its ability to say "no, I can't do high-speed", while the former didn't. The check for high-speed capability was designed such that it shouldn't cause problems for USB-1 devices, but individual USB-1 devices weren't actually tested for this. From gagsl-py2 at yahoo.com.ar Tue Jul 7 18:50:57 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 19:50:57 -0300 Subject: Python Error from Apress book References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> Message-ID: En Tue, 07 Jul 2009 09:55:13 -0300, Dave Angel escribi?: > Gabriel Genellina wrote: >> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >> escribi?: >>> When I try to run the command as outlined in >>> the book "simple_markup2.py < test_input.txt > test_output.html i get >>> the >>> following error every time. >>> >>> IOError: [Errno 9] Bad file descriptor >> >> That's a Windows problem. When you execute the script as itself (either >> as you do in the command line, or by double-clicking on it), it doesn't >> have valid standard handles. >> You have to invoke Python explicitely: >> >> python simple_markup2.py < test_input.txt > test_output.html >> >> (you may need to specify the full path to python.exe, or add the >> directory where Python is installed to your system PATH). >> > I use stdout this way all the time, with no problem (python 2.6, Windows > XP). But as you point out, stdin redirection doesn't seem to work using > the file associations. I do get a different error though. When I look > at sys.stdin, it shows an open file, with handle of zero, as expected. > But when I do a raw_input(), it gets: > EOFError: EOF when reading a line I think the error depends on the specific OS version/service pack. But at least on XP this appears to fix it: http://support.microsoft.com/kb/321788/en-us -- Gabriel Genellina From philr at aspexconsulting.co.nz Tue Jul 7 19:04:25 2009 From: philr at aspexconsulting.co.nz (Phil Runciman) Date: Wed, 8 Jul 2009 11:04:25 +1200 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> Message-ID: -----Original Message----- From: Dennis Lee Bieber [mailto:wlfraed at ix.netcom.com] Sent: Tuesday, 7 July 2009 4:45 p.m. To: python-list at python.org Subject: Re: A Bug By Any Other Name ... On Mon, 6 Jul 2009 19:48:39 -0700, Daniel Fetchinson declaimed the following in gmane.comp.python.general: > Yes, there are plenty of languages other than Java and C, but the > influence of C is admittedly huge in Python. Why do you think loops > are called "for", conditionals "if" or "while", functions return via > "return", loops terminate via "break" and keep going via "continue" > and why is comparison written as "==", etc, etc? All of these are > coming from C (or an even earlier language) and my point is that users for, if, and return were common keywords in FORTRAN. Not to mention BASIC Both of which predate C -- __________________________________________________________________________ Guido was probably influenced by the ALGOL language stream, which used "for" and "if". ALGOL 60 was a joint American and European effort and was significant in the late 50s and 60's. Guido's fellow countryman, Edsgar Dijkstra, took this publication language (the ALGOL60 version) and produced a compiler for it. (It was not the first, but was very early on). Then B. Randell and L.J. Russell visited him, learnt from his experiences and returned to the UK to produce the KDF9 Whetstone ALGOL60 Compiler. Their book "ALGOL 60 Implementation" was a very early and influential book in the field and occupies a significant place in the history of computing. Computer language designers, including Nicholas Wirth (Pascal, Modula, Oberon), have been influenced by ALGOL. Sadly, "C" and its ilk, come from a totally different stream of language development beginning with the likes of "CPL", "BCPL", "B" and developing into "C" and "C++". This stream was originally focussed on language portability and performance. This stream was much closer to the assembler language end of the language spectrum, whereas the other stream was heavily biased towards publication and later teaching. I could say more but will restrain myself. My 2c Phil FWIW "++" reeks of assembler language notation. The KDF9 Whetstone ALGOL60 Compiler was the one I used at Whetstone for 1-Dimensional Transient Heat-Flow calculations and Steam-Table generation. Our course on it was 2.5 days long. We had wonderful training by English Electric, Kidsgrove staff. I hate to think how long the same course would be now, a month? From norseman at hughes.net Tue Jul 7 19:07:04 2009 From: norseman at hughes.net (norseman) Date: Tue, 07 Jul 2009 16:07:04 -0700 Subject: tough-to-explain Python In-Reply-To: References: Message-ID: <4A53D518.6080306@hughes.net> Bearophile wrote: > kj, as Piet van Oostrum as said, that's the difference between mutable > an immutable. It comes from the procedural nature of Python, and > probably an explanation of such topic can't be avoided if you want to > learn/teach Python. ...(snip) > > See you later, > bearophile ==========================Perhaps because it is the way I learned or the way I "naturally" approach programming, or maybe the way one influences the other, but at any rate I think "newbies" to programming should first learn a few basic concepts (if/then, for, while, do and other loops and other control constructs) and then be forced to deal with the computer on it's own terms. Assembly. Once the student learns what the computer already knows, that the whole thing is just bits and the combination of them determines it's responses, it then becomes easier to present 'idealistic' concepts and their implementations. With the knowledge and 'mental picture' of the computer's nature the rest of the ideas for programming have a tendency to drift in the direction of reality and the underlying needs to fulfill the 'better' and/or 'easier' languages. Having both the knowledge of the 'full capabilities' of a computer and the experience of a formalized language the student can, or should, learn/compare the trade offs of each. By understanding the computer (I at least) grasp the basics of a new language quite quickly. No, I don't become a guru overnight, if at all, but I do have the advantage in deciding if a given language is appropriate for a given job with very little research. The more I delve into OOP the more I liken an 'object' to a box. A box with a shipping manifest. There are big boxes, little boxes, squat boxes and so on. A box can contain corn flakes, bullets, raisins, rice, burlap, silk, motorcycle(s), soap and more. The manifest describes contents. The manifest is there but the description(s) change with content (type). The descriptions always use one or more of the basics like: color, count, dimension and so forth. Just like an OOP object. A box can contain things of all sorts, including references to the contents of other box(es). A box can even be a virtual of another (the global concept). The return statement, in this context, means hauling the contents of the box (and/or its manifest) back to (wherever) and disposing of the current box (a local). Just like an OOP object. It is easier to visualize a box and it's use than a non described blob. Abstracts are never precise - hence the evolution of the word. The one thing a teacher will always fail is the same as anyone else who tries to adequately describe a pretty sunset to a person born totally blind. No point(s) of reference. Time for me to sign off. To all those that helped me when I needed it - I thank you very much. Food for thought: your watch (clock) does not tell time. The watch (clock) only mimics one movement of the earth. ie... 3 dimensions are still static, the 4th is movement. Steve From stef.mientki at gmail.com Tue Jul 7 19:12:08 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Wed, 08 Jul 2009 01:12:08 +0200 Subject: Python/pyobjC Apps on iPhone now a possibility? In-Reply-To: <85ljn0ej4h.fsf@agentultra.com> References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: <4A53D648.60005@gmail.com> >> So, the question is, can the same thing be done for Python apps? >> > > I love Python and all, but it'd be apt to ask, what's the point? > > The iPhone is running on what? A 400Mhz ARM processor? Resources on the > device are already limited; running your program on top of an embedded > Python interpreter would only be adding pressure to the constraints; > even if it was an optimized interpreter. > I don't know iPhone, but I've done some experiments with 400 MHz arm, running Windows Mobile, and found PocketPyGUI running very very well on these devices. cheers, Stef Mientki From james at agentultra.com Tue Jul 7 19:29:25 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 07 Jul 2009 19:29:25 -0400 Subject: Python/pyobjC Apps on iPhone now a possibility? References: <85ljn0ej4h.fsf@agentultra.com> Message-ID: <85d48cdqcq.fsf@agentultra.com> Stef Mientki writes: >>> So, the question is, can the same thing be done for Python apps? >>> >> >> I love Python and all, but it'd be apt to ask, what's the point? >> >> The iPhone is running on what? A 400Mhz ARM processor? Resources on the >> device are already limited; running your program on top of an embedded >> Python interpreter would only be adding pressure to the constraints; >> even if it was an optimized interpreter. >> > I don't know iPhone, > but I've done some experiments with 400 MHz arm, running Windows Mobile, > and found PocketPyGUI running very very well on these devices. > > cheers, > Stef Mientki Sure, but it's pretty relative in the sense that it might be fast enough if I'm sitting around but too slow if I want to enter some information in the app before the next train comes. As a programmer, I don't really see the benefit of using an embedded interpreter on the iPhone. Objective-C isn't the greatest language, but it's easy to learn and well supported. It also compiles into some pretty speedy executables. If you can sacrifice a little run-time speed for your users in exchange for ease of development on your part, all the more to you. My original point was that I don't see the benefit in that decision. From gagsl-py2 at yahoo.com.ar Tue Jul 7 19:44:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 20:44:45 -0300 Subject: Embedded Python : Why does thread lock here? References: <00e8b4e8-5386-4f3d-b900-ab1ba41294f2@q40g2000prh.googlegroups.com> Message-ID: En Tue, 07 Jul 2009 15:01:17 -0300, roschler escribi?: > I have the Python Intepreter embedded in a Delphi (Object Pascal) > program. In the Python script shown below, I have a module that > creates a thread object and starts it. Do you *execute* the module or do you *import* it? Isn't a good idea to spawn a thread by side effect of importing a module. > The thread's run() call calls > a function called deleteOutputVariables() declared at the module > level. In my code's first incarnation the thread's run() call would > deadlock when trying to call the deleteOutputVariables() function > declared at Module level. I would never see the statement > "(deleteOutputVariables) Top of Call" printed to the screen. I now > know that I must periodically call join() with a very fast time-out to > keep Python threads happy, and that solved the problem. What does the Delphi code? Isn't a join with a long timeout enough? > However I am > curious as to why it deadlocked at the deleteOutputVariables() call? > Is it because deleteOutputVariables() is declared at the module level > or because that function deletes module level variables? If so why? I don't know, but you can make some experiments - move te deleteOutputVariables as a method, or don't delete module level variables at all, and see what happens. I'd say both factors are irrelevant. > # FUNCTION: Delete the variables given in the list. > def deleteOutputVariables(theModule, theOutputVariablesListOfNames): > try: > print "(deleteOutputVariables) Top of call." > for theOutputVariableName in theOutputVariablesListOfNames: > if theModule.__dict__.has_key(theOutputVariableName): > print "(Python::deleteOutputVariables) Deleting the > Output Variable named " + theOutputVariableName > del theModule.__dict__[theOutputVariableName] > except: > print "(deleteOutputVariables) Exception occurred." As a rule, avoid using __special__ names. There is almost never need of using them (__init__ is a notable exception) unless you want to change Python behaviour. In this case: for theOutputVariableName in theOutputVariablesListOfNames: if hasattr(theModule, theOutputVariableName): delattr(theModule, theOutputVariableName) (a name like theOutputVariablesListOfNames is too long for my taste, but that's a matter of style...) > theNewThread = None > theNewThread = threadRun("TestThread") That first line is useless... -- Gabriel Genellina From stef.mientki at gmail.com Tue Jul 7 19:46:51 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Wed, 08 Jul 2009 01:46:51 +0200 Subject: Python/pyobjC Apps on iPhone now a possibility? In-Reply-To: <85d48cdqcq.fsf@agentultra.com> References: <85ljn0ej4h.fsf@agentultra.com> <85d48cdqcq.fsf@agentultra.com> Message-ID: <4A53DE6B.8040706@gmail.com> J Kenneth King wrote: > Stef Mientki writes: > > >>>> So, the question is, can the same thing be done for Python apps? >>>> >>>> >>> I love Python and all, but it'd be apt to ask, what's the point? >>> >>> The iPhone is running on what? A 400Mhz ARM processor? Resources on the >>> device are already limited; running your program on top of an embedded >>> Python interpreter would only be adding pressure to the constraints; >>> even if it was an optimized interpreter. >>> >>> >> I don't know iPhone, >> but I've done some experiments with 400 MHz arm, running Windows Mobile, >> and found PocketPyGUI running very very well on these devices. >> >> cheers, >> Stef Mientki >> > > Sure, but it's pretty relative in the sense that it might be fast enough > if I'm sitting around but too slow if I want to enter some information > in the app before the next train comes. > > As a programmer, I don't really see the benefit of using an embedded > interpreter on the iPhone. Objective-C isn't the greatest language, but > it's easy to learn and well supported. It also compiles into some > pretty speedy executables. > > If you can sacrifice a little run-time speed for your users in exchange > for ease of development on your part, all the more to you. > > My original point was that I don't see the benefit in that decision. > > > ok 20 years ago I might have agreed with you, but now a days, speed of development is a much more important decision maker than speed of execution ;-) cheers, Stef From freelancer317 at gmail.com Tue Jul 7 19:49:54 2009 From: freelancer317 at gmail.com (Bret Fledderjohn) Date: Tue, 7 Jul 2009 19:49:54 -0400 Subject: tough-to-explain Python In-Reply-To: <4A53D518.6080306@hughes.net> References: <4A53D518.6080306@hughes.net> Message-ID: I really enjoyed your boxes analogy, from a guy with a trucking background, it makes a lot of sense! -Bret > ... The more I delve into OOP the more I liken an 'object' to a box. A box > with a shipping manifest. > > There are big boxes, > little boxes, > squat boxes and so on. > > A box can contain corn flakes, > bullets, raisins, rice, burlap, silk, motorcycle(s), soap and more. > > The manifest describes contents. > The manifest is there but the description(s) change with content (type). > The descriptions always use one or more of the basics like: color, count, > dimension and so forth. > > Just like an OOP object. > > A box can contain things of all sorts, including references to the contents > of other box(es). A box can even be a virtual of another (the global > concept). The return statement, in this context, means hauling the contents > of the box (and/or its manifest) back to (wherever) and disposing of the > current box (a local). > > Just like an OOP object. > > > It is easier to visualize a box and it's use than a non described blob. > Abstracts are never precise - hence the evolution of the word. > > > The one thing a teacher will always fail is the same as anyone else who > tries to adequately describe a pretty sunset to a person born totally blind. > No point(s) of reference. > > > > Time for me to sign off. To all those that helped me when I needed it - > > I thank you very much. > > Food for thought: your watch (clock) does not tell time. > The watch (clock) only mimics one movement of the earth. > ie... 3 dimensions are still static, the 4th is movement. > > > Steve -- - Bret -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Tue Jul 7 19:57:35 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Wed, 08 Jul 2009 11:57:35 +1200 Subject: Check file is locked? References: Message-ID: In message , Christian Heimes wrote: > By the way most operating systems don't lock a file when it's opened for > reading or writing or even executed. The general conclusion seems to be that mandatory locking is more trouble than it's worth. From ben+python at benfinney.id.au Tue Jul 7 20:06:07 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 08 Jul 2009 10:06:07 +1000 Subject: tough-to-explain Python References: Message-ID: <8763e4ox74.fsf@benfinney.id.au> kj writes: > In Chris Rebert writes: > > >You might find the following helpful (partially): > >http://effbot.org/zone/call-by-object.htm > > Extremely helpful. Thanks! (I learned more from it than someone > who will teach the stuff would care to admit...) I have got very good results from teaching using the analogy of ?paper tags tied to physical objects? to describe Python's references to values. The analogy allows illustration of many otherwise confusing facts: * an assignment is the act of putting a tag onto an object * a tag leads to exactly one object, and it can be re-tied to a different object * an object can have arbitrarily many tags on it * containers (e.g. lists, dicts, sets, etc.) don't contain objects, they contain tags leading to objects * passing arguments to a function always makes new tags leading to the objects passed in, and throws those new tags away once the function returns * etc. The analogy can then be contrasted to how Python *doesn't* do it: named boxes with one value per box. You can point out that many other languages do use this model, so they should be aware of it, but Python doesn't use it. > I had not realized how *profoundly* different the meaning of the > "=" in Python's > > spam = ham Access the object referenced by ?ham?, and assigns the reference ?spam? to that object. > is from the "=" in its > > spam[3] = ham[3] Access the object referenced by ?ham[3]?, and assigns the reference ?spam[3]? to that object. No, they're exactly the same. The only thing that's different is the references you use; the assignment operates *exactly* the same in both cases. -- \ ?We are no more free to believe whatever we want about God than | `\ we are free to adopt unjustified beliefs about science or | _o__) history [?].? ?Sam Harris, _The End of Faith_, 2004 | Ben Finney From gagsl-py2 at yahoo.com.ar Tue Jul 7 20:45:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 21:45:15 -0300 Subject: Write matrix to text file References: <564970db0907071046o60e927dao21cc0557f1fa2903@mail.gmail.com> Message-ID: En Tue, 07 Jul 2009 14:46:47 -0300, Hanna Michelsen escribi?: > I'm working with both python and matlab at the moment and I was > wondering if > there is an efficient way to take a 2-D array (of 1s and 0s) in python > and > write it to a text file such that matlab will be able to create a sparse > matrix from it later. Your yesterday post appeared on this list successfully. Posting the very same text again isn't going to help - those that are reading it now are likely the same that read it before. If nobody replied, assume that something is wrong with the question itself. Not the grammar, looks clear to me. Maybe the meaning: you say "a 2-D array" but you don't say what that means (a list? an array.array object? a Numpy array?). Consider *who* might be able to answer: someone that knows both Python and matlab, and knows how to use a sparse matrix, and knows how to read data from files, and knows how to generate such files from Python. The last part is the easy part, because people here is supposed to know how to do things in Python; but not how matlab works. So, please help people help you: tell us what file format would be fine for matlab to read, and people here surely will suggest the best way to write that in Python. Reading http://www.mikeash.com/getting_answers.html may help too. -- Gabriel Genellina From gallium.arsenide at gmail.com Tue Jul 7 20:55:13 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 7 Jul 2009 17:55:13 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <8fa4563b-9de7-44cb-a8a2-a6d7c87a0ce4@q11g2000yqi.googlegroups.com> On Jul 7, 5:11?pm, kj wrote: > I don't plan to present these examples to them. > But beginners have a way of bumping into such > conundrums all on their own [...]. ?I doubt that > an answer of the form "don't worry your pretty > little head over this now; wait until your second > course" will do the trick. I agree that beginners are bound to come across difficult issues on their own, and when they do, you have to at least try to explain, rather than dismiss them. I believe that the beginners which are most curious, and thus most likely to run into things you didn't plan for them, are also most likely to be ready and able to receive your explanation. That said, I think it's worth planning a fairly controlled flow of information. For example, you can go a LONG way without touching tuples or the augmented assignment operators. For your function example, I suppose the key ideas to understand are binding and containers (which may or may not be mutable). The oversimplified version I think I would attempt to explain is that, as far as the "outside world" is concerned, a function cannot rebind the arguments passed to it. However, the contents of a mutable container may be altered without rebinding. That is, you can hold a bucket, pass that bucket to a function, and the function can put stuff in the bucket or take stuff out without you ever letting go of the bucket. When the function returns, you are still holding the bucket. Nested containers, especially with "outside" bindings to inner containers, may be tougher to visualize with real-world objects. Hopefully by then the students can grasp a more abstract (pointerlike) notion of binding! Good luck! John From usernet at ilthio.net Tue Jul 7 20:59:05 2009 From: usernet at ilthio.net (Tim Harig) Date: Wed, 08 Jul 2009 00:59:05 GMT Subject: IP Address Function References: Message-ID: On 2009-07-08, Fred Atkinson wrote: > Is there a Python function I can use to get the user's IP > address so I can display it on his browser? If you are using CGI you can get it from the REMOTE_ADDR environmental variable. From clp2 at rebertia.com Tue Jul 7 21:09:09 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 7 Jul 2009 18:09:09 -0700 Subject: IP Address Function In-Reply-To: References: Message-ID: <50697b2c0907071809y57b78162odd06b94cfff54d01@mail.gmail.com> On Tue, Jul 7, 2009 at 6:45 PM, Fred Atkinson wrote: > ? ? ? ?Is there a Python function I can use to get the user's IP > address so I can display it on his browser? from socket import gethostname, gethostbyname ip = gethostbyname(gethostname()) Cheers, Chris -- http://blog.rebertia.com From http Tue Jul 7 21:24:28 2009 From: http (Paul Rubin) Date: 07 Jul 2009 18:24:28 -0700 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> <1f09e856-bd04-4d27-bf51-5d67953a7e13@y7g2000yqa.googlegroups.com> Message-ID: <7xskh83r1v.fsf@ruckus.brouhaha.com> pdpi writes: > while abs(func(guess) - target) > epsilon: > guess = (lo + hi) / 2. > if sense * func(guess) > sense * target: > hi = guess > elif sense * func(guess) < sense * target: > lo = guess > elif lo == hi: > return None > return guess That is completely confusing. I get the, er, impression that "sense" is supposed to change during the loop, and it takes much head scratching to tell whether what you have there is right or not. Also, it calls func 3 times on each loop, which could be extremely slow. You don't know what func does, so eliminating 2/3 of the calls to it is not a micro-optimization--it could be a major time saving. Yet another version: def _binary_search(x0, x1, func, target, epsilon): y0,y1 = func(x0), func(x1) while abs(y1 - target) > epsilon: if x0 == x1 or cmp(y0,target) == cmp(y1,target): return None xn = (x0 + x1) / 2. yn = func(xn) if cmp(yn,target) == cmp(y0,target): x0,y0 = xn,yn else: x1,y1 = xn,yn return x1 From gallium.arsenide at gmail.com Tue Jul 7 21:26:13 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 7 Jul 2009 18:26:13 -0700 (PDT) Subject: tough-to-explain Python References: <8763e4ox74.fsf@benfinney.id.au> Message-ID: <197bfc81-05a6-4328-acec-3ddd5b733655@d32g2000yqh.googlegroups.com> On Jul 7, 8:06?pm, Ben Finney wrote: > I have got very good results from teaching using > the analogy of ?paper tags tied to physical objects? > to describe Python's references to values. Ah, I like that! I think it's better than what I used in my post (which I composed and submitted before yours showed up in my reader). I am not formally a teacher, but I do try to help "nonprogrammers" learn Python from time to time, and this will be a good one to remember. John From gagsl-py2 at yahoo.com.ar Tue Jul 7 21:30:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 22:30:07 -0300 Subject: Clarity vs. code reuse/generality References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006ebea2$0$9711$c3e8da3@news.astraweb.com> <4A5344BE.2080006@sequans.com> Message-ID: En Tue, 07 Jul 2009 09:51:10 -0300, Jean-Michel Pichavant escribi?: > I've never used sense in that way before, nor I've seen used by others > until now. However Kj is right, and my dictionary seems wrong > (wordreference.com). I've searched through others dictionaries and find > out this is actually applicable to functions. My bad. Using a common word with its common meaning is important too in order to understand the code. It's hard enough for students to grasp the algorithm itself, why make it artificially harder by using strange variable names. Some years ago I had to endure using an in-house framework with names like bring_XXX and fix_XXX instead of the usual names get_XXX and set_XXX (that was C++, emulating properties; I'm not sure of the actual verbs used, perhaps "obtain" and "establish", but certainly not get/set/put). Add some undecipherable comments in spanglish, profuse usage of macros that alter the lexical appearance of the language, and even reading code was a torture. -- Gabriel Genellina From fatkinson at mishmash.com Tue Jul 7 21:45:24 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Tue, 07 Jul 2009 18:45:24 -0700 Subject: IP Address Function Message-ID: Is there a Python function I can use to get the user's IP address so I can display it on his browser? Regards, Fred From gagsl-py2 at yahoo.com.ar Tue Jul 7 21:54:03 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 07 Jul 2009 22:54:03 -0300 Subject: IP Address Function References: Message-ID: En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson escribi?: > Is there a Python function I can use to get the user's IP > address so I can display it on his browser? There is a long distance between "Python" and "browser" - you'll have to tell us what is in between the two. By example, do you have a server and the user connects to it? is it running Python? how do you run the Python application? And why do you want to do that on the server side? Isn't easier to do that on the client side? What about proxies? NAT? If using CGI, look at the REMOTE_ADDR environment variable. -- Gabriel Genellina From steve at REMOVE-THIS-cybersource.com.au Tue Jul 7 22:03:16 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 02:03:16 GMT Subject: tough-to-explain Python References: Message-ID: <006fd94e$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 21:18:53 +0000, kj wrote: > I had not realized how *profoundly* different the meaning of the "=" in > Python's > > spam = ham > > is from the "=" in its > > spam[3] = ham[3] > > So much for "explicit is better than implicit"... I'm sorry, I don't get it. Can you explain please? I don't see why it's so "profoundly" different. Apart from spam[3] = x not being permitted if spam is an immutable type. I suppose though they are *fundamentally* different, in that spam=ham is dealt with by the compiler while spam[3]=ham is handled by the object spam using the __setitem__ method. Is that the difference you're talking about? -- Steven From python at bdurham.com Tue Jul 7 22:40:05 2009 From: python at bdurham.com (python at bdurham.com) Date: Tue, 07 Jul 2009 22:40:05 -0400 Subject: tough-to-explain Python In-Reply-To: <197bfc81-05a6-4328-acec-3ddd5b733655@d32g2000yqh.googlegroups.com> References: <8763e4ox74.fsf@benfinney.id.au> <197bfc81-05a6-4328-acec-3ddd5b733655@d32g2000yqh.googlegroups.com> Message-ID: <1247020805.1725.1324003095@webmail.messagingengine.com> Ben, > I have got very good results from teaching using the analogy of "paper tags tied to physical objects" to describe Python's references to values. Great analogy!! And an excellent analogy for newcomers to Python. (this would have saved me some personal pain in my early days). Regards, Malcolm From gagsl-py2 at yahoo.com.ar Tue Jul 7 23:02:53 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Jul 2009 00:02:53 -0300 Subject: tough-to-explain Python References: Message-ID: En Tue, 07 Jul 2009 17:04:46 -0300, kj escribi?: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > >>>> ham = [1, 2, 3, 4] >>>> spam = (ham,) >>>> spam > ([1, 2, 3, 4],) >>>> spam[0] is ham > True >>>> spam[0] += [5] > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment >>>> ham += [5] >>>> spam > ([1, 2, 3, 4, 5, 5],) > > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. This article is based on an old thread in this list that is very enlightening: How To Think Like A Pythonista http://python.net/crew/mwh/hacks/objectthink.html and I'll quote just a paragraph from Alex Martelli: ?There is [...] a huge difference between changing an object, and changing (mutating) some OTHER object to which the first refers. In Bologna over 100 years ago we had a statue of a local hero depicted pointing forwards with his finger -- presumably to the future, but given where exactly it was placed, the locals soon identified it as "the statue that points to Hotel Belfiore". Then one day some enterprising developer bought the hotel's building and restructured it -- in particular, where the hotel used to be was now a restaurant, Da Carlo. So, "the statue that points to Hotel Belfiore" had suddenly become "the statue that points to Da Carlo"...! Amazing isn't it? Considering that marble isn't very fluid and the statue had not been moved or disturbed in any way...? This is a real anecdote, by the way (except that I'm not sure of the names of the hotel and restaurant involved -- I could be wrong on those), but I think it can still help here. The dictionary, or statue, has not changed at all, even though the objects it refers/points to may have been mutated beyond recognition, and the name people know it by (the dictionary's string-representation) may therefore change. That name or representation was and is referring to a non-intrinsic, non-persistent, "happenstance" characteristic of the statue, or dictionary...? -- Gabriel Genellina From peter.milliken at gmail.com Tue Jul 7 23:18:20 2009 From: peter.milliken at gmail.com (Peter) Date: Tue, 7 Jul 2009 20:18:20 -0700 (PDT) Subject: How to use Python to interface with Web pages? Message-ID: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Any help would be appreciated :-) I want to write an auction sniping tool in Python. I know Python, but I know absolutely nothing about web pages, javascript etc i.e. I want the program to automatically log me into my eBay account, access the appropriate item, locate how many mins/seconds until the bid time ends and then automatically place a bid at the last possible moment. I can see the web-page source - it looks to be javascript (to my untutored eye :-)). But how do I enter data and "simulated" mouse presses on a web-page that I have accessed via a Python program? So where can I start to learn how to do this? Any books available? web resources that show examples or offer tutorials? I have (in the past) written some python to scoop data off web-sites but I have never written anything that interactively interacts with the web-page contents and don't know where to even start on this one. Thanks for any help/pointers Peter From ralf at schoenian-online.de Wed Jul 8 00:05:53 2009 From: ralf at schoenian-online.de (Ralf Schoenian) Date: Wed, 08 Jul 2009 06:05:53 +0200 Subject: How to use Python to interface with Web pages? In-Reply-To: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> References: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Message-ID: <4A541B21.3030203@schoenian-online.de> Peter wrote: > Any help would be appreciated :-) > > I want to write an auction sniping tool in Python. I know Python, but > I know absolutely nothing about web pages, javascript etc i.e. I want > the program to automatically log me into my eBay account, access the > appropriate item, locate how many mins/seconds until the bid time ends > and then automatically place a bid at the last possible moment. > > I can see the web-page source - it looks to be javascript (to my > untutored eye :-)). > > But how do I enter data and "simulated" mouse presses on a web-page > that I have accessed via a Python program? > > So where can I start to learn how to do this? Any books available? web > resources that show examples or offer tutorials? > > I have (in the past) written some python to scoop data off web-sites > but I have never written anything that interactively interacts with > the web-page contents and don't know where to even start on this one. > > Thanks for any help/pointers > > Peter Hi, a good starting point possibly is http://wwwsearch.sourceforge.net/mechanize/ Regards, Ralf From sajmikins at gmail.com Wed Jul 8 00:10:38 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 7 Jul 2009 21:10:38 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> On Jul 7, 4:04 pm, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > > Consider the following interaction sequence: > > >>> def eggs(some_int, some_list, some_tuple): > > ... some_int += 2 > ... some_list += [2] > ... some_tuple += (2,) > ... > > >>> x = 42 > >>> y = (42,) > >>> z = [42] > >>> eggs(x, y, z) > >>> x > 42 > >>> y > (42,) > >>> z > [42, 2] You have transposed some_list and some some_tuple. I.e. you should be calling eggs(x, z, y). > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? You don't. Rank beginners don't have enough background knowledge to grok that code. Why would you even tell the poor bastards about "+=" before they were comfortable with (python's style of) function calls, immutable integers, mutable lists and immutable tuples? Let them use "x = x + y" until they have enough knowledge to understand "augmented" assignment. Syntax matters (I mean general order of things, not linguistic syntax.) Making an omelette requires putting eggs in a pan and cracking them, but not in that order. > Or consider this one: > > >>> ham = [1, 2, 3, 4] > >>> spam = (ham,) > >>> spam > ([1, 2, 3, 4],) > >>> spam[0] is ham > True > >>> spam[0] += [5] > > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment > >>> ham += [5] > >>> spam > > ([1, 2, 3, 4, 5, 5],) > > > > What do you say to that? I say, "Don't use augmented assignment with indexed tuples." Seriously, python's augmented assignment is almost magical. I think you're just making trouble for yourself and your students if you introduce it too early. I get python pretty well (if I say so myself) but I wouldn't know how to explain: In [1]: def foo(a_list): ...: a_list = a_list + [5] ...: ...: In [2]: n = [] In [3]: foo(n) In [4]: n Out[4]: [] In [5]: def bar(a_list): ...: a_list += [5] ...: ...: In [6]: bar(n) In [7]: n Out[7]: [5] It's "Just The Way It Is". Freakin' magic, man. > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! Frankly, I'm of the impression that it's a mistake not to start teaching programming with /the bit/ and work your way up from there. I'm not kidding. I wrote a (draft) article about this: "Computer Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 I really think the only good way to teach computers and programming is to start with a bit, and build up from there. "Ontology recapitulates phylogeny" I realize that doesn't help you teach python, but I wanted to put it out there. From sjmachin at lexicon.net Wed Jul 8 00:30:26 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 7 Jul 2009 21:30:26 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: On Jul 8, 3:05?am, dana wrote: > I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC > modules together. Although I don't have Python 2.5, I've been informed > the DBI module has been deprecated at 2.5. > > A few questions: > > 1) Although deprecated, will it work at all in 2.5? Does the fact that > it is deprecrated mean it has been removed entirely, or does Python > 2.5 simply issuing a warning? Deprecated certainly doesn't mean removed. > > 2) What do I use in place of DBI for my Python 2.4. scripts that > import modules DBI and ODBC together. I don't use DBI directly. It was > simply a dependency for the ODBC module as best I knew. For a start, none of (DBI, ODBC, dbi, odbc) are standard Python- supplied modules. Perhaps you are referring to the odbc (and dbi) from the pywin32 package? Where did you get them from? If you can't remember, try this: |Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32 | Type "help", "copyright", "credits" or "license" for more information. | >>> import odbc | >>> odbc.__file__ | 'C:\\python24\\lib\\site-packages\\win32\\odbc.pyd' | >>> If this is what you're talking about, you should be asking on the pywin32 dicussion list (http://mail.python.org/mailman/listinfo/python- win32). General advice: if you are thinking of upgrading your Python version, go straight to 2.6. odbc is AFAIK stuck at version 1.0 of the Python DB API; consider switching to pyodbc (http://code.google.com/p/ pyodbc/) HTH, John From rajat.dudeja at gmail.com Wed Jul 8 00:31:12 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Tue, 7 Jul 2009 21:31:12 -0700 (PDT) Subject: Check file is locked? References: Message-ID: On Jul 8, 4:57?am, Lawrence D'Oliveiro wrote: > In message , Christian > > Heimes wrote: > > By the way most operating systems don't lock a file when it's opened for > > reading or writing or even executed. > > The general conclusion seems to be that mandatory locking is more trouble > than it's worth. My OS is a windows XP sp3. All I'm trying to achieve is to close an application ( which could be a notepad, a wordpad or some other text editor) that have opened my file. Once the file is closed I can easily delete that file. I guess, if file is opened by one of that application, the file must be locked and so is the reason I cannot delete the file. Please suggest if this is fine. From timr at probo.com Wed Jul 8 01:11:12 2009 From: timr at probo.com (Tim Roberts) Date: Tue, 07 Jul 2009 22:11:12 -0700 Subject: Python and webcam capture delay? References: Message-ID: <0ca855t29jklb0o0gi31ourfk3lstdiepf@4ax.com> Nobody wrote: > >The webcam is bound to do some encoding; most of them use USB "full speed" >(12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. That's not true. Most of the web cams made in the last 5 years or so run at high speed, 480 Mbps. Full speed only gets you 1 fps at 640x480 uncompressed, so it's really only useful for the most primitive video conference cams. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 02:09:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 06:09:19 GMT Subject: tough-to-explain Python References: Message-ID: <007012f9$0$9711$c3e8da3@news.astraweb.com> On Tue, 07 Jul 2009 20:04:46 +0000, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. [...] > Or consider this one: > >>>> ham = [1, 2, 3, 4] >>>> spam = (ham,) >>>> spam > ([1, 2, 3, 4],) >>>> spam[0] is ham > True >>>> spam[0] += [5] > Traceback (most recent call last): > File "", line 1, in > TypeError: 'tuple' object does not support item assignment >>>> ham += [5] >>>> spam > ([1, 2, 3, 4, 5, 5],) >>>> >>>> > What do you say to that? That one surely is very straight forward. Just like the exception says, tuples don't support item assignment, so spam[0] += [5] is not allowed. But just because you have put a list inside a tuple doesn't mean the list stops being a list -- you can still append to the list, which is what ham += [5] does. So spam is unchanged: it is still the one-item tuple containing a list. It is just that the list has now been modified. This is only troublesome (in my opinion) if you imagine that tuples are somehow magical "frozen-lists", where the contents can't be modified once created. That's not the case -- the tuple itself can't be modified, but the objects inside it remain ordinary objects, and the mutable ones can be modified. The thing to remember is that the tuple spam doesn't know anything about the *name* ham -- it knows the object referred to by the name ham. You can modify the name, and nothing happens to the tuple: >>> spam ([1, 2, 3, 4, 5],) >>> ham = [5] >>> spam ([1, 2, 3, 4, 5],) Or if you prefer: >>> ham = spam[0] # label the list inside spam as 'ham' >>> ham += [6] # modify the list labelled as 'ham' >>> spam ([1, 2, 3, 4, 5, 6],) >>> pork = ham # create a new label, 'pork', and bind it to the same list >>> del ham # throw away the label 'ham' >>> pork += [7] # modify the list labelled as 'pork' >>> spam ([1, 2, 3, 4, 5, 6, 7],) It's all about the objects, not the names. -- Steven From dhananjay.c.joshi at gmail.com Wed Jul 8 02:10:59 2009 From: dhananjay.c.joshi at gmail.com (Dhananjay) Date: Wed, 8 Jul 2009 11:40:59 +0530 Subject: count Message-ID: Dear all, I have file as follows,however, tab seperated (not shown in following file): 6 3 4.309726 7 65 93.377388 8 47 50.111952 9 270 253.045923 10 184 182.684670 11 76 121.853455 12 85 136.283470 13 114 145.910662 14 45 80.703013 15 44 47.154646 16 41 66.461339 17 16 33.819488 18 127 136.105455 19 70 88.798681 20 29 61.297823 I wanted to sort column 2 in assending order and I read whole file in array "data" and did the following: data.sort(key = lambda fields:(fields[2])) I have sorted column 2, however I want to count the numbers in the column 2. i.e. I want to know, for example, how many repeates of say '3' (first row, 2nd column in above data) are there in column 2. I could write seperate programme to get the result.s. However, is there any way to count the numbers there itself while sorting in column 2 ? Thanking you in advance, -- Dhananjay -------------- next part -------------- An HTML attachment was scrubbed... URL: From romeoamp at gmail.com Wed Jul 8 02:13:42 2009 From: romeoamp at gmail.com (romeoamp) Date: Tue, 7 Jul 2009 23:13:42 -0700 (PDT) Subject: SSH utility In-Reply-To: References: Message-ID: <24385961.post@talk.nabble.com> Please look at on : http://www.lag.net/paramiko/ http://www.lag.net/paramiko/ Sample Code: Find Attachment Thanks, S.V.RAJKUMAR, XOU Solutions India Private Limited No. 37, PM Towers, Greams Road, Thousand Lights, Chennai - 6 . Mobile No : +91 - 9940632275. half.italian wrote: > > On Aug 11, 5:17?am, Edwin.Mad... at VerizonWireless.com wrote: >> for similar tasks, I use pexpecthttp://pypi.python.org/pypi/pexpect. >> >> spawning bash process and simulate an interactive session. Here sending >> ls command, retrieving results and exiting. In the spawned process ssh or >> any other command, is just another command. >> >> ------------actual session-------------------------- >> $ python >> Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) >> [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin >> Type "help", "copyright", "credits" or "license" for more information.>>> >> import pexpect >> >>> c = pexpect.spawn('/bin/bash') >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('\x1b[?1034hmadared at NJWARHQD0IT696A:~\r\n', '$ ')>>> c.sendline('ls') >> 3 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('ls\r\x.txt ?xx.txt ?xy.txt ?y.txt\r\nmadared at NJWARHQD0IT696A:~\r\n', '$ >> ')>>> c.sendline('exit') >> 5 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 1 >> >>> c.before, c.after >> >> ('exit\r\nexit\r\n', )>>> exit() >> >> madared at NJWARHQD0IT696A:~ >> $ >> --------------------------------------------------------------- >> >> hope that helps. >> >> regards. >> Edwin >> >> -----Original Message----- >> From: python-list-bounces+edwin.madari=verizonwireless.... at python.org >> >> [mailto:python-list-bounces+edwin.madari=verizonwireless.... at python.org] >> On Behalf Of James Brady >> Sent: Monday, August 11, 2008 12:26 AM >> To: python-l... at python.org >> Subject: SSH utility >> >> Hi all, >> I'm looking for a python library that lets me execute shell commands >> on remote machines. >> >> I've tried a few SSH utilities so far: paramiko, PySSH and pssh; >> unfortunately all been unreliable, and repeated questions on their >> respective mailing lists haven't been answered... >> >> It seems like the sort of commodity task that there should be a pretty >> robust library for. Are there any suggestions for alternative >> libraries or approaches? >> >> Thanks! >> James >> --http://mail.python.org/mailman/listinfo/python-list >> >> The information contained in this message and any attachment may be >> proprietary, confidential, and privileged or subject to the work >> product doctrine and thus protected from disclosure. ?If the reader >> of this message is not the intended recipient, or an employee or >> agent responsible for delivering this message to the intended >> recipient, you are hereby notified that any dissemination, >> distribution or copying of this communication is strictly prohibited. >> If you have received this communication in error, please notify me >> immediately by replying to this message and deleting it and all >> copies and backups thereof. ?Thank you. > > I second pexpect and the nice little module that comes with it > ssh_session.py. > > Been using it for ages now! > > ~Sean > -- > http://mail.python.org/mailman/listinfo/python-list > > http://www.nabble.com/file/p24385961/sshclient.py sshclient.py -- View this message in context: http://www.nabble.com/SSH-utility-tp18920030p24385961.html Sent from the Python - python-list mailing list archive at Nabble.com. -------------- next part -------------- An HTML attachment was scrubbed... URL: From romeoamp at gmail.com Wed Jul 8 02:15:08 2009 From: romeoamp at gmail.com (romeoamp) Date: Tue, 7 Jul 2009 23:15:08 -0700 (PDT) Subject: SSH utility Message-ID: <24385961.post@talk.nabble.com> Please look at on : http://www.lag.net/paramiko/ Sample Code: Find Attachment Thanks, S.V.RAJKUMAR, XOU Solutions India Private Limited No. 37, PM Towers, Greams Road, Thousand Lights, Chennai - 6 . Mobile No : +91 - 9940632275. half.italian wrote: > > On Aug 11, 5:17?am, Edwin.Mad... at VerizonWireless.com wrote: >> for similar tasks, I use pexpecthttp://pypi.python.org/pypi/pexpect. >> >> spawning bash process and simulate an interactive session. Here sending >> ls command, retrieving results and exiting. In the spawned process ssh or >> any other command, is just another command. >> >> ------------actual session-------------------------- >> $ python >> Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) >> [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin >> Type "help", "copyright", "credits" or "license" for more information.>>> >> import pexpect >> >>> c = pexpect.spawn('/bin/bash') >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('\x1b[?1034hmadared at NJWARHQD0IT696A:~\r\n', '$ ')>>> c.sendline('ls') >> 3 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 2 >> >>> c.before, c.after >> >> ('ls\r\x.txt ?xx.txt ?xy.txt ?y.txt\r\nmadared at NJWARHQD0IT696A:~\r\n', '$ >> ')>>> c.sendline('exit') >> 5 >> >>> c.expect([pexpect.TIMEOUT, pexpect.EOF, '\$ ']) >> 1 >> >>> c.before, c.after >> >> ('exit\r\nexit\r\n', )>>> exit() >> >> madared at NJWARHQD0IT696A:~ >> $ >> --------------------------------------------------------------- >> >> hope that helps. >> >> regards. >> Edwin >> >> -----Original Message----- >> From: python-list-bounces+edwin.madari=verizonwireless.... at python.org >> >> [mailto:python-list-bounces+edwin.madari=verizonwireless.... at python.org] >> On Behalf Of James Brady >> Sent: Monday, August 11, 2008 12:26 AM >> To: python-l... at python.org >> Subject: SSH utility >> >> Hi all, >> I'm looking for a python library that lets me execute shell commands >> on remote machines. >> >> I've tried a few SSH utilities so far: paramiko, PySSH and pssh; >> unfortunately all been unreliable, and repeated questions on their >> respective mailing lists haven't been answered... >> >> It seems like the sort of commodity task that there should be a pretty >> robust library for. Are there any suggestions for alternative >> libraries or approaches? >> >> Thanks! >> James >> --http://mail.python.org/mailman/listinfo/python-list >> >> The information contained in this message and any attachment may be >> proprietary, confidential, and privileged or subject to the work >> product doctrine and thus protected from disclosure. ?If the reader >> of this message is not the intended recipient, or an employee or >> agent responsible for delivering this message to the intended >> recipient, you are hereby notified that any dissemination, >> distribution or copying of this communication is strictly prohibited. >> If you have received this communication in error, please notify me >> immediately by replying to this message and deleting it and all >> copies and backups thereof. ?Thank you. > > I second pexpect and the nice little module that comes with it > ssh_session.py. > > Been using it for ages now! > > ~Sean > -- > http://mail.python.org/mailman/listinfo/python-list > > http://www.nabble.com/file/p24385961/sshclient.py sshclient.py -- View this message in context: http://www.nabble.com/SSH-utility-tp18920030p24385961.html Sent from the Python - python-list mailing list archive at Nabble.com. From python at rcn.com Wed Jul 8 03:11:50 2009 From: python at rcn.com (Raymond Hettinger) Date: Wed, 8 Jul 2009 00:11:50 -0700 (PDT) Subject: finding most common elements between thousands of multiple arrays. References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> Message-ID: <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> [Scott David Daniels] > def most_frequent(arr, N): > ? ? ?'''Return the top N (freq, val) elements in arr''' > ? ? ?counted = frequency(arr) # get an iterator for freq-val pairs > ? ? ?heap = [] > ? ? ?# First, just fill up the array with the first N distinct > ? ? ?for i in range(N): > ? ? ? ? ?try: > ? ? ? ? ? ? ?heap.append(counted.next()) > ? ? ? ? ?except StopIteration: > ? ? ? ? ? ? ?break # If we run out here, no need for a heap > ? ? ?else: > ? ? ? ? ?# more to go, switch to a min-heap, and replace the least > ? ? ? ? ?# element every time we find something better > ? ? ? ? ?heapq.heapify(heap) > ? ? ? ? ?for pair in counted: > ? ? ? ? ? ? ?if pair > heap[0]: > ? ? ? ? ? ? ? ? ?heapq.heapreplace(heap, pair) > ? ? ?return sorted(heap, reverse=True) # put most frequent first. In Py2.4 and later, see heapq.nlargest(). In Py3.1, see collections.Counter(data).most_common(n) Raymond From wuwei23 at gmail.com Wed Jul 8 03:29:32 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 8 Jul 2009 00:29:32 -0700 (PDT) Subject: How to use Python to interface with Web pages? References: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Message-ID: <127e0a78-89fc-4c09-bda7-95836c57ac58@v37g2000prg.googlegroups.com> On Jul 8, 1:18?pm, Peter wrote: > I can see the web-page source - it looks to be javascript (to my > untutored eye :-)). > > But how do I enter data and "simulated" mouse presses on a web-page > that I have accessed via a Python program? > [...] > I have (in the past) written some python to scoop data off web-sites > but I have never written anything that interactively interacts with > the web-page contents and don't know where to even start on this one. I don't have much experience with it, but for interacting with javascript-heavy pages you might want to take a look at Selenium: http://seleniumhq.org/ http://pypi.python.org/pypi/selenium http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html http://joker.linuxstuff.pl/documentation/make_selenium It's primarily aimed at testing web app UIs, but if it allows for values to be passed into tests then 'test' == 'action' in terms of your requirements. Hope this helps. - alex23 From bieffe62 at gmail.com Wed Jul 8 03:32:07 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Wed, 8 Jul 2009 00:32:07 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <70654e24-aa1b-4a78-87a3-d36873da6109@i6g2000yqj.googlegroups.com> On Jul 7, 10:04?pm, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > > Consider the following interaction sequence: > > >>> def eggs(some_int, some_list, some_tuple): > > ... ? ? some_int += 2 > ... ? ? some_list += [2] > ... ? ? some_tuple += (2,) > ... > > >>> x = 42 > >>> y = (42,) > >>> z = [42] > >>> eggs(x, y, z) > >>> x > 42 > >>> y > (42,) > >>> z > [42, 2] > > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? > > Or consider this one: > > >>> ham = [1, 2, 3, 4] > >>> spam = (ham,) > >>> spam > ([1, 2, 3, 4],) > >>> spam[0] is ham > True > >>> spam[0] += [5] > > Traceback (most recent call last): > ? File "", line 1, in > TypeError: 'tuple' object does not support item assignment>>> ham += [5] > >>> spam > > ([1, 2, 3, 4, 5, 5],) > > > > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! > > TIA! > > kj I would go with something like this: """ In object oriented programming, the same function or operator can be used to represent different things. This is called overloading. To understand what the operator/function do, we have to look at the kind of object it is applied to. In this case, the operator "+=" means two different things: - for strings and numbers it means : "create a new object by merging the two operands". This is why the original object is left the same. - for lists, it means : "increase the left operand with the contents of the right operand". This is why the original object is changed """ You couuld also add: """ You see, in python everithing is an object. Some object can be changed (mutable objects), others cannot. """ but this is another story. P:S : Sometime I think they should not have allowed += on immutables and forced everybody to write s = s + "some more". Ciao ---- FB From mail at timgolden.me.uk Wed Jul 8 03:45:58 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 08 Jul 2009 08:45:58 +0100 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A544EB6.5090404@timgolden.me.uk> Rajat wrote: > On Jul 8, 4:57 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> In message , Christian >> >> Heimes wrote: >>> By the way most operating systems don't lock a file when it's opened for >>> reading or writing or even executed. >> The general conclusion seems to be that mandatory locking is more trouble >> than it's worth. > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > application ( which could be a notepad, a wordpad or some other text > editor) that have opened my file. Once the file is closed I can easily > delete that file. > > I guess, if file is opened by one of that application, the file must > be locked and so is the reason I cannot delete the file. I assume that your real requirement is: I can't open/close/delete this file; it must be locked by something else; what is that something else? The simplest thing is probably to run sysinternals' handle util: http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx and use the subprocess module to parse the output. That will give you the pid, and you can then use, eg, psutil: http://code.google.com/p/psutil/ to get the details of the process. TJG From piet at cs.uu.nl Wed Jul 8 04:11:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 10:11:35 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Simon Forman (SF) wrote: >SF> Why would you even tell the poor bastards about "+=" before they were >SF> comfortable with (python's style of) function calls, immutable >SF> integers, mutable lists and immutable tuples? >SF> Let them use "x = x + y" until they have enough knowledge to >SF> understand "augmented" assignment. And *then* you can tell them that "x += y" can be subtly different from "x = x + y", which is what happened in the example that the OP gave. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From gagsl-py2 at yahoo.com.ar Wed Jul 8 04:13:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 08 Jul 2009 05:13:42 -0300 Subject: tough-to-explain Python References: <70654e24-aa1b-4a78-87a3-d36873da6109@i6g2000yqj.googlegroups.com> Message-ID: En Wed, 08 Jul 2009 04:32:07 -0300, Francesco Bochicchio escribi?: > I would go with something like this: > > """ > In object oriented programming, the same function or operator can be > used to represent > different things. This is called overloading. To understand what the > operator/function do, we have to look at > the kind of object it is applied to. > In this case, the operator "+=" means two different things: > - for strings and numbers it means : "create a new object by merging > the two operands". This is why the original object is left the same. > - for lists, it means : "increase the left operand with the contents > of the right operand". This is why the original object is changed > """ Mmm, but this isn't quite exact. += isn't an operator, but a statement (augmented assignment). a += b translates to: [*] a = a.__iadd__(b) [*] It's up to the __iadd__ implementation to return the same object or a new one, and the assignment occurs even if the same object is returned. If you think of an assignments as binding names to objects (well, that's the documented behavior, btw) things become more clear. > P:S : Sometime I think they should not have allowed += on immutables > and forced everybody to write s = s + "some more". I think not everyone agreed when += was introduced. But now, having s += "some more" allows the operation to be optimized. [*] except 'a' is evaluated only once -- Gabriel Genellina From helvinlui at gmail.com Wed Jul 8 04:51:07 2009 From: helvinlui at gmail.com (Helvin) Date: Wed, 8 Jul 2009 01:51:07 -0700 (PDT) Subject: PyQt GUI Message-ID: Hi experts! I'm developing a GUI for a software using PyQT, and need 3D visualization. Should I use PyOpenGL or VTK? I understand that the PyQt package comes with a opengl module. What else would I need? I think I need to download opengl. but how? where? I have VTK and pyVTK installed, but I don't know how to use it in my code, as when I run it, an error says it can't find the vtk module. Help would be sooooo appreciated! Helvin From fatkinson at mishmash.com Wed Jul 8 04:55:29 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Wed, 08 Jul 2009 01:55:29 -0700 Subject: IP Address Function References: Message-ID: On Tue, 07 Jul 2009 22:54:03 -0300, "Gabriel Genellina" wrote: >En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson >escribi?: > >> Is there a Python function I can use to get the user's IP >> address so I can display it on his browser? > >There is a long distance between "Python" and "browser" - you'll have to >tell us what is in between the two. I want to have a Web page come up (written in Python, cgi) that returns the IP address of the browser (user's PC's IP address). >By example, do you have a server and the user connects to it? is it >running Python? how do you run the Python application? >And why do you want to do that on the server side? Isn't easier to do that >on the client side? What about proxies? NAT? Yes. By CGI. >If using CGI, look at the REMOTE_ADDR environment variable. I did look at REMOTE_ADDR but I've been unable to figure out the correct way to code it. I've tried a number of ways but I've been unsuccessful. Ideally, I'd like to store the brower's IP address in a string and then print the string on the Web page from a Python CGI script. Regards, Fred From vilya.harvey at gmail.com Wed Jul 8 04:56:55 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Wed, 8 Jul 2009 09:56:55 +0100 Subject: count In-Reply-To: References: Message-ID: <6aef848f0907080156k114b0bb0me13b26904683d411@mail.gmail.com> 2009/7/8 Dhananjay : > I wanted to sort column 2 in assending order? and I read whole file in array > "data" and did the following: > > data.sort(key = lambda fields:(fields[2])) > > I have sorted column 2, however I want to count the numbers in the column 2. > i.e. I want to know, for example, how many repeates of say '3' (first row, > 2nd column in above data) are there in column 2. One thing: indexes in Python start from 0, so the second column has an index of 1 not 2. In other words, it should be data.sort(key = lambda fields: fields[1]) instead. With that out of the way, the following will print out a count of each unique item in the second column: from itertools import groupby for x, g in groupby([fields[1] for fields in data]): print x, len(tuple(g)) Hope that helps, Vil. From deets at nospam.web.de Wed Jul 8 05:11:51 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Jul 2009 11:11:51 +0200 Subject: PyQt GUI References: Message-ID: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Helvin wrote: > Hi experts! > > I'm developing a GUI for a software using PyQT, and need 3D > visualization. Should I use PyOpenGL or VTK? > I understand that the PyQt package comes with a opengl module. What > else would I need? I think I need to download opengl. but how? where? > I have VTK and pyVTK installed, but I don't know how to use it in my > code, as when I run it, an error says it can't find the vtk module. VTK won't mix with Qt. And I don't think you need to download opengl - it either comes with your system (or gfx-drivers), or not. I'm not sure if Qt actually wraps the OpenGL api itself, AFAIK it doesn't, so you need PyOpenGL I guess. Diez From phil at riverbankcomputing.com Wed Jul 8 05:23:10 2009 From: phil at riverbankcomputing.com (Phil Thompson) Date: Wed, 08 Jul 2009 10:23:10 +0100 Subject: PyQt GUI In-Reply-To: <7bj5ulF22b4ktU1@mid.uni-berlin.de> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Message-ID: <66fe05cfa1805aef3a0d4aadc4e7c9c1@localhost> On Wed, 08 Jul 2009 11:11:51 +0200, "Diez B. Roggisch" wrote: > Helvin wrote: > >> Hi experts! >> >> I'm developing a GUI for a software using PyQT, and need 3D >> visualization. Should I use PyOpenGL or VTK? >> I understand that the PyQt package comes with a opengl module. What >> else would I need? I think I need to download opengl. but how? where? >> I have VTK and pyVTK installed, but I don't know how to use it in my >> code, as when I run it, an error says it can't find the vtk module. > > VTK won't mix with Qt. And I don't think you need to download opengl - it > either comes with your system (or gfx-drivers), or not. I'm not sure if Qt > actually wraps the OpenGL api itself, AFAIK it doesn't, so you need > PyOpenGL I guess. VTK has explicit support for both Qt (ie. via C++) and PyQt. Phil From eckhardt at satorlaser.com Wed Jul 8 05:24:28 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Wed, 08 Jul 2009 11:24:28 +0200 Subject: Fractions as result from divisions (was: Re: tough-to-explain Python) References: Message-ID: Bearophile wrote: > For example a novice wants to see 124 / 38 to return the 62/19 > fraction and not 3 or 3.263157894736842 :-) Python has adopted the latter of the three for operator / and the the second one for operator //. I wonder if it was considered to just return a fraction from that operation. x = 3/5 -> x = fraction(3, 5) x = x*7 -> x = fraction(21, 5) x = x/2 -> x = fraction(21, 10) range(x) -> error, x not an integer range(int(x)) -> [0, 1,] y = float(x) -> y = 2.1 This would have allowed keeping the operator what people are used to. On the other hand, implicit conversion to either of float or int would have been avoided, which is usually the #1 cause for subtle problems. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From davea at ieee.org Wed Jul 8 05:35:54 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Jul 2009 05:35:54 -0400 Subject: Python Error from Apress book In-Reply-To: References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> Message-ID: <4A54687A.6010004@ieee.org> Gabriel Genellina wrote: >
En Tue, > 07 Jul 2009 09:55:13 -0300, Dave Angel escribi?: >> Gabriel Genellina wrote: >>> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >>> escribi?: > >>>> When I try to run the command as outlined in >>>> the book "simple_markup2.py < test_input.txt > test_output.html i >>>> get the >>>> following error every time. >>>> >>>> IOError: [Errno 9] Bad file descriptor >>> >>> That's a Windows problem. When you execute the script as itself >>> (either as you do in the command line, or by double-clicking on it), >>> it doesn't have valid standard handles. >>> You have to invoke Python explicitely: >>> >>> python simple_markup2.py < test_input.txt > test_output.html >>> >>> (you may need to specify the full path to python.exe, or add the >>> directory where Python is installed to your system PATH). >>> >> I use stdout this way all the time, with no problem (python 2.6, >> Windows XP). But as you point out, stdin redirection doesn't seem to >> work using the file associations. I do get a different error though. >> When I look at sys.stdin, it shows an open file, with handle of zero, >> as expected. But when I do a raw_input(), it gets: >> EOFError: EOF when reading a line > > I think the error depends on the specific OS version/service pack. But > at least on XP this appears to fix it: > > http://support.microsoft.com/kb/321788/en-us > Thanks for the link. Looking at that one, it indicates that Windows 2000 fixed it in SP4, and XP fixed it in Sp1. But I'm already running XP SP3, so I wonder if it's something new. Matt, what OS version are you running, and exactly what is happening when the error occurs? (you should be able to figure that out from the stack trace) DaveA From deets at nospam.web.de Wed Jul 8 05:44:15 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Jul 2009 11:44:15 +0200 Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Message-ID: <7bj7reF23oparU1@mid.uni-berlin.de> Phil Thompson wrote: > On Wed, 08 Jul 2009 11:11:51 +0200, "Diez B. Roggisch" > > wrote: >> Helvin wrote: >> >>> Hi experts! >>> >>> I'm developing a GUI for a software using PyQT, and need 3D >>> visualization. Should I use PyOpenGL or VTK? >>> I understand that the PyQt package comes with a opengl module. What >>> else would I need? I think I need to download opengl. but how? where? >>> I have VTK and pyVTK installed, but I don't know how to use it in my >>> code, as when I run it, an error says it can't find the vtk module. >> >> VTK won't mix with Qt. And I don't think you need to download opengl - it >> either comes with your system (or gfx-drivers), or not. I'm not sure if > Qt >> actually wraps the OpenGL api itself, AFAIK it doesn't, so you need >> PyOpenGL I guess. > > VTK has explicit support for both Qt (ie. via C++) and PyQt. Oh, I'm sorry for the FUD - they talk about Tk on their homepage. Diez From python.list at tim.thechases.com Wed Jul 8 05:53:02 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 08 Jul 2009 04:53:02 -0500 Subject: count In-Reply-To: References: Message-ID: <4A546C7E.503@tim.thechases.com> > I wanted to sort column 2 in assending order and I read whole file in array > "data" and did the following: > > data.sort(key = lambda fields:(fields[2])) > > I have sorted column 2, however I want to count the numbers in the column 2. > i.e. I want to know, for example, how many repeates of say '3' (first row, > 2nd column in above data) are there in column 2. I think you're trying to count in the wrong step -- I'd do the counting while you read the file in; not while you're trying to sort. Maybe something like this untested file-wrapper: class Histo: def __init__(self, data, column): self.data = data self.column = column self.stats = defaultdict(int) self.exhausted = False def __iter__(self): if self.exhausted: raise StopIteration("Input exhausted") for line in self.data: self.stats[line[self.column]] += 1 yield line self.exhausted = True def stats_so_far(self): return self.stats with file('in.txt') as f: r = csv.reader(f, delimiter='\t') h = Histo(r, 2) data = list(h) # slurp up the elements print repr(h.stats_so_far()) data.sort(key = lambda fields: (fields[2])) -tkc From davea at ieee.org Wed Jul 8 06:18:47 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Jul 2009 06:18:47 -0400 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A547287.8020507@ieee.org> Rajat wrote: > On Jul 8, 4:57 am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: > >> In message , Christian >> >> Heimes wrote: >> >>> By the way most operating systems don't lock a file when it's opened for >>> reading or writing or even executed. >>> >> The general conclusion seems to be that mandatory locking is more trouble >> than it's worth. >> > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > application ( which could be a notepad, a wordpad or some other text > editor) that have opened my file. Once the file is closed I can easily > delete that file. > > I guess, if file is opened by one of that application, the file must > be locked and so is the reason I cannot delete the file. > > Please suggest if this is fine. > > > Neither Wordpad nor Notepad lock the files they have open, at least in my experience. Specifically, I just tried it in XP Sp3. I created a file, opened the file in one of the apps, then deleted the file before closing the app. Word for Windows, on the other hand, has locked files for me in the past. When I tried that one just now (on a text file, because that was easy), results were rather confusing. It let me delete the file, but then when I tried to "save" it, it gave me a permissions error. My experience with Winword has been that it's in a world of its own, best not messed with. And this latest version (Office 2007 or somesuch) is so modern that it has removed the Help->About menu to make sure I can't even tell what version it is. Next time I rebuild my system, I think I'll go back to Office 97. But perhaps your question is intended to cover the general case. You can get a program from sysinternals called handle, which will list all the files (and other handles) opened by all the processes in the system. You may be able to run that (with subprocess, or just from the commandline), and filter its output for the information you want. DaveA From dana_at_work at yahoo.com Wed Jul 8 06:25:33 2009 From: dana_at_work at yahoo.com (dana) Date: Wed, 8 Jul 2009 03:25:33 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: <084fac41-364f-431c-9b8f-b57c3279f91d@x3g2000yqa.googlegroups.com> On Jul 8, 12:30?am, John Machin wrote: > Deprecated certainly doesn't mean removed. > For a start, none of (DBI, ODBC, dbi, odbc) are standard Python- > supplied modules. Perhaps you are referring to the odbc (and dbi) from > the pywin32 package? Where did you get them from? If you can't > remember, try this: Thanks John. I mean the lower-case dbi and odbc modules from pywin32. Sorry for being vague. I assumed incorrectly they were part of the standard library because they came with pywin32. > If this is what you're talking about, you should be asking on the > pywin32 dicussion list (http://mail.python.org/mailman/listinfo/python- > win32). Thanks again. > General advice: if you are thinking of upgrading your Python version, > go straight to 2.6. odbc is AFAIK stuck at version 1.0 of the Python > DB API; consider switching to pyodbc (http://code.google.com/p/ > pyodbc/) Thanks thrice. We "have" to use the version of Python our software vendor supports. Presently, that's pywin32 version 2.5. Dana From piet at cs.uu.nl Wed Jul 8 06:29:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 12:29:54 +0200 Subject: IP Address Function References: Message-ID: >>>>> Fred Atkinson (FA) wrote: >FA> On Tue, 07 Jul 2009 22:54:03 -0300, "Gabriel Genellina" >FA> wrote: >>> En Tue, 07 Jul 2009 22:45:24 -0300, Fred Atkinson >>> escribi?: >>> >>>> Is there a Python function I can use to get the user's IP >>>> address so I can display it on his browser? >>> >>> There is a long distance between "Python" and "browser" - you'll have to >>> tell us what is in between the two. >FA> I want to have a Web page come up (written in Python, cgi) >FA> that returns the IP address of the browser (user's PC's IP address). >>> By example, do you have a server and the user connects to it? is it >>> running Python? how do you run the Python application? >>> And why do you want to do that on the server side? Isn't easier to do that >>> on the client side? What about proxies? NAT? >FA> Yes. By CGI. >>> If using CGI, look at the REMOTE_ADDR environment variable. >FA> I did look at REMOTE_ADDR but I've been unable to figure out >FA> the correct way to code it. I've tried a number of ways but I've been >FA> unsuccessful. >FA> Ideally, I'd like to store the brower's IP address in a string >FA> and then print the string on the Web page from a Python CGI script. Something like: #! /usr/bin/env python import cgi from os import getenv print "Content-type: text/html" print ipaddr = (getenv("HTTP_CLIENT_IP") or getenv("HTTP_X_FORWARDED_FOR") or getenv("HTTP_X_FORWARDED_FOR") or getenv("REMOTE_ADDR") or "UNKNOWN") print ipaddr -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nick at craig-wood.com Wed Jul 8 06:30:01 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 08 Jul 2009 05:30:01 -0500 Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> <006eb2e3$0$9711$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 06 Jul 2009 15:02:26 -0700, Michael Mossey wrote: > > > On Jul 6, 2:47?pm, Philip Semanchuk wrote: > >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: > >> > >> > What is required in a python program to make sure it catches a > >> > control- > >> > c on the command-line? Do some i/o? The OS here is Linux. > >> > >> You can use a try/except to catch a KeyboardInterrupt exception, or you > >> can trap it using the signal > >> module:http://docs.python.org/library/signal.html > >> > >> You want to trap SIGINT. > >> > >> HTH > >> Philip > > > > Thanks to both of you. However, my question is also about whether I need > > to be doing i/o or some similar operation for my program to notice in > > any shape or form that Control-C has been pressed. In the past, I've > > written Python programs that go about their business ignoring Ctrl-C. > > I bet that somewhere in your code you have something like: > > > for x in really_big_list: > try: > long_running_process(x) > except: > continue > > > If that's what you're doing, stop! The correct way is: > > > for x in really_big_list: > try: > long_running_process(x) > except Exception: > # Let KeyboardInterrupt and SystemExit through. > continue Note that it is a relatively recent change (in python 2.5) which made KeyboardInterrupt not a child of Exception ncw at dogger:~$ python2.4 Python 2.4.6 (#2, Feb 17 2009, 20:01:48) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. Loaded customisations from '/home/ncw/.pystartup' >>> isinstance(KeyboardInterrupt(), Exception) True >>> ncw at dogger:~$ python2.5 Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. Loaded customisations from '/home/ncw/.pystartup' >>> isinstance(KeyboardInterrupt(), Exception) False >>> > for x in really_big_list: > try: > long_running_process(x) > except (KeyboardInterrupt, SystemExit): > print "User requested exit... shutting down now" > cleanup() > raise > except Exception: > continue That is the backwards compatible way -- Nick Craig-Wood -- http://www.craig-wood.com/nick From bearophileHUGS at lycos.com Wed Jul 8 07:20:56 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Wed, 8 Jul 2009 04:20:56 -0700 (PDT) Subject: count References: Message-ID: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Vilya Harvey: > from itertools import groupby > for x, g in groupby([fields[1] for fields in data]): > ? ? print x, len(tuple(g)) Avoid that len(tuple(g)), use something like the following, it's lazy and saves some memory. def leniter(iterator): """leniter(iterator): return the length of a given iterator, consuming it, without creating a list. Never use it with infinite iterators. >>> leniter() Traceback (most recent call last): ... TypeError: leniter() takes exactly 1 argument (0 given) >>> leniter([]) 0 >>> leniter([1]) 1 >>> leniter(iter([1])) 1 >>> leniter(x for x in xrange(100) if x%2) 50 >>> from itertools import groupby >>> [(leniter(g), h) for h,g in groupby("aaaabccaadeeee")] [(4, 'a'), (1, 'b'), (2, 'c'), (2, 'a'), (1, 'd'), (4, 'e')] >>> def foo0(): ... if False: yield 1 >>> leniter(foo0()) 0 >>> def foo1(): yield 1 >>> leniter(foo1()) 1 """ # This code is faster than: sum(1 for _ in iterator) if hasattr(iterator, "__len__"): return len(iterator) nelements = 0 for _ in iterator: nelements += 1 return nelements Bye, bearophile From no.email at please.post Wed Jul 8 07:59:19 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 11:59:19 +0000 (UTC) Subject: Fractions as result from divisions (was: Re: tough-to-explain Python) References: Message-ID: In Ulrich Eckhardt writes: >Bearophile wrote: >> For example a novice wants to see 124 / 38 to return the 62/19 >> fraction and not 3 or 3.263157894736842 :-) >Python has adopted the latter of the three for operator / and the the second >one for operator //. I wonder if it was considered to just return a >fraction from that operation. http://www.python.org/dev/peps/pep-0239/ kj From mail at microcorp.co.za Wed Jul 8 08:15:08 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 8 Jul 2009 14:15:08 +0200 Subject: Remoting over SSH References: Message-ID: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> "Hussein B" wrote: > Hey, > I want to perform commands on a remote server over SSH. > What do I need? > Thanks. Access privileges for the remote machine. - Hendrik From no.email at please.post Wed Jul 8 08:23:50 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 12:23:50 +0000 (UTC) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> Simon Forman writes: >Frankly, I'm of the impression that it's a mistake not to start >teaching programming with /the bit/ and work your way up from there. >I'm not kidding. I wrote a (draft) article about this: "Computer >Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 >I really think the only good way to teach computers and programming is >to start with a bit, and build up from there. "Ontology recapitulates >phylogeny" I happen to be very receptive to this point of view. I had the benefit of that sort of training (one of the first computer courses I took started, believe it or not, with Turing machines, through coding in machine language, and compiler theory, and all the way up to dabbling with Unix!), and I suspect that the reason it is sometimes difficult for me to explain even relatively simple-looking things to others is that I have this background that I unconsciously, and incorrectly, take for granted in others... There is this persistent idea "out there" that programming is a very accessible skill, like cooking or gardening, anyone can do it, and even profit from it, monetarily or otherwise, etc., and to some extent I am actively contributing to this perception by teaching this course to non-programmers (experimental biologists to be more precise), but maybe this idea is not entirely true... Maybe, to get past the most amateurish level, one has to, one way or another, come face-to-face with bits, compilers, algorithms, and all the rest that real computer scientists learn about in their formal training... kj From p.f.moore at gmail.com Wed Jul 8 08:33:50 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Wed, 8 Jul 2009 13:33:50 +0100 Subject: DBI module deprecated at Python 2.5--what to use in its place? In-Reply-To: <084fac41-364f-431c-9b8f-b57c3279f91d@x3g2000yqa.googlegroups.com> References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> <084fac41-364f-431c-9b8f-b57c3279f91d@x3g2000yqa.googlegroups.com> Message-ID: <79990c6b0907080533v54f97099oabc77a8d146fe5e7@mail.gmail.com> 2009/7/8 dana : > On Jul 8, 12:30?am, John Machin wrote: >> Deprecated certainly doesn't mean removed. >> For a start, none of (DBI, ODBC, dbi, odbc) are standard Python- >> supplied modules. Perhaps you are referring to the odbc (and dbi) from >> the pywin32 package? Where did you get them from? If you can't >> remember, try this: > > Thanks John. I mean the lower-case dbi and odbc modules from pywin32. > Sorry for being vague. I assumed incorrectly they were part of the > standard library because they came with pywin32. pywin32 isn't part of the standard library, either - it just feels like it if you're on Windows :-) As you've already seen, the Python-Win32 list is probably of more use to you: http://mail.python.org/mailman/listinfo/python-win32 Paul. From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 08:43:55 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 12:43:55 GMT Subject: Fractions as result from divisions (was: Re: tough-to-explain Python) References: Message-ID: <00706f76$0$9711$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 11:24:28 +0200, Ulrich Eckhardt wrote: > Bearophile wrote: >> For example a novice wants to see 124 / 38 to return the 62/19 fraction >> and not 3 or 3.263157894736842 :-) > > Python has adopted the latter of the three for operator / and the the > second one for operator //. Up until a few years ago, / would return the integer division if both arguments where ints or longs, and // didn't even exist. Even in Python 2.6, you still need to do from __future__ import division to get the behaviour you describe. > I wonder if it was considered to just return > a fraction from that operation. Because of his experience with ABC, Guido dislikes using fractions for standard arithmetic. He was resistant to even allowing the rational module be added to the standard library. The problem with fractions is that, unless you're very careful, repeated arithmetic operations on numbers ends up giving you fractions like 498655847957/569892397664 instead of the 7/8 you were expecting. Not only is that ugly, but it becomes slower and slower as the denominator and numerator become larger. At least, that was Guido's experience. -- Steven From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 08:46:28 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 12:46:28 GMT Subject: Check file is locked? References: Message-ID: <0070700f$0$9711$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 00:06:11 -0700, Dennis Lee Bieber wrote: > Also, some applications may still have the file open, but Windows > allows one to make copies of that file... Not always though... some applications open files for exclusive read access. -- Steven From zephyrfalcon!NO_SPAM! at gmail.com Wed Jul 8 08:51:30 2009 From: zephyrfalcon!NO_SPAM! at gmail.com (Hans Nowak) Date: Wed, 08 Jul 2009 08:51:30 -0400 Subject: Fractions as result from divisions In-Reply-To: References: Message-ID: <4A549652.9070408@gmail.com> Ulrich Eckhardt wrote: > Bearophile wrote: >> For example a novice wants to see 124 / 38 to return the 62/19 >> fraction and not 3 or 3.263157894736842 :-) > > Python has adopted the latter of the three for operator / and the the second > one for operator //. I wonder if it was considered to just return a > fraction from that operation. http://python-history.blogspot.com/2009/03/problem-with-integer-division.html "For example, in ABC, when you divided two integers, the result was an exact rational number representing the result. In Python however, integer division truncated the result to an integer. In my experience, rational numbers didn't pan out as ABC's designers had hoped. A typical experience would be to write a simple program for some business application (say, doing one?s taxes), and find that it was running much slower than expected. After some debugging, the cause would be that internally the program was using rational numbers with thousands of digits of precision to represent values that would be truncated to two or three digits of precision upon printing. This could be easily fixed by starting an addition with an inexact zero, but this was often non-intuitive and hard to debug for beginners." -- Hans Nowak (zephyrfalcon at gmail dot com) http://4.flowsnake.org/ From steve at REMOVE-THIS-cybersource.com.au Wed Jul 8 09:10:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 08 Jul 2009 13:10:23 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <007075aa$0$9711$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 12:23:50 +0000, kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> > Simon Forman writes: > >>Frankly, I'm of the impression that it's a mistake not to start teaching >>programming with /the bit/ and work your way up from there. I'm not >>kidding. I wrote a (draft) article about this: "Computer Curriculum" >>http://docs.google.com/View?id=dgwr777r_31g4572gp4 > >>I really think the only good way to teach computers and programming is >>to start with a bit, and build up from there. "Ontology recapitulates >>phylogeny" > > > I happen to be very receptive to this point of view. [...] > There is this persistent idea "out there" that > programming is a very accessible skill, like cooking or gardening, > anyone can do it, and even profit from it, monetarily or otherwise, > etc., and to some extent I am actively contributing to this perception > by teaching this course to non-programmers (experimental biologists to > be more precise), but maybe this idea is not entirely true... There is some evidence that 30-60% of people simply cannot learn to program, no matter how you teach them: http://www.codinghorror.com/blog/archives/000635.html http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ I'm sympathetic to the idea, but not entirely convinced. Perhaps the problem isn't with the students, but with the teachers, and the languages: http://www.csse.monash.edu.au/~damian/papers/PDF/SevenDeadlySins.pdf (My money is that it's a little of both.) > Maybe, to > get past the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest that > real computer scientists learn about in their formal training... The "No True Scotsman" fallacy. There's nothing amateurish about building software applications that work, with well-designed interfaces and a minimum of bugs, even if you've never heard of Turing Machines. -- Steven From lucascarvalho at gmail.com Wed Jul 8 09:11:35 2009 From: lucascarvalho at gmail.com (Lucas Carvalho) Date: Wed, 08 Jul 2009 15:11:35 +0200 Subject: Remoting over SSH In-Reply-To: References: Message-ID: <4A549B07.2090402@gmail.com> Hussein B wrote: > Hey, > I want to perform commands on a remote server over SSH. > What do I need? > Thanks. > Hi, If you want to use the SSH2 protocol into a python code, you should take a look at this module: paramiko [1]. [1] http://www.lag.net/paramiko/ Regards, Lucas. From pruebauno at latinmail.com Wed Jul 8 09:23:57 2009 From: pruebauno at latinmail.com (nn) Date: Wed, 8 Jul 2009 06:23:57 -0700 (PDT) Subject: library search path when compiling python References: <13d8e508-0f09-43c3-acfd-b260e41d9cd6@a36g2000yqc.googlegroups.com> Message-ID: <11e4f2f2-a32f-4671-a457-7cdd928d29fb@a7g2000yqk.googlegroups.com> On Jul 7, 4:06?pm, Christian Heimes wrote: > nn wrote: > > I am trying to compile python with ssl support but the libraries are > > not in /usr/lib but in /opt/freeware/lib. How do I add that folder to > > the default library search path? > > > It looks like configure --libdir=DIR might do the job but I don't want > > to replace the default lib search path, just add an additional folder > > to it. > > You didn't mention your OS. On Linux you can set the environment > variable LD_RUN_PATH prior to compiling Python. The env var sets the > rpath for the linker. See man ld(1) for details. > > Christian Sorry I didn't realize that was OS specific. I am on AIX 5.3. From no.email at please.post Wed Jul 8 09:27:34 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 13:27:34 +0000 (UTC) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> Simon Forman writes: >I'm not kidding. I wrote a (draft) article about this: "Computer >Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 Very cool. kj From aahz at pythoncraft.com Wed Jul 8 09:40:34 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 06:40:34 -0700 Subject: Newbie needs help References: <8694d66b-307f-4f35-9bad-dd78e553ac8c@q11g2000yqi.googlegroups.com> <50f98a4c0907070718v31331f63tf0bdae930b22d01e@mail.gmail.com> <527BE7864AF97047BD9F4DEA26124D2B04906DC3@cos-us-mb01.cos.agilent.com> Message-ID: In article , Pablo Torres N. wrote: > >Give this one a try too: http://www.mikeash.com/getting_answers.html >It doesn't talk down to you...as much :P Nice! I'll try remembering that one. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From p.f.moore at gmail.com Wed Jul 8 09:47:48 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Wed, 8 Jul 2009 14:47:48 +0100 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <79990c6b0907080647l22dce38epf03709b1119b2529@mail.gmail.com> 2009/7/8 kj : > There is this > persistent idea "out there" that programming is a very accessible > skill, like cooking or gardening, anyone can do it, and even profit > from it, monetarily or otherwise, etc., and to some extent I am > actively contributing to this perception by teaching this course > to non-programmers (experimental biologists to be more precise), > but maybe this idea is not entirely true... ?Maybe, to get past > the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest > that real computer scientists learn about in their formal training... Look at it another way. Experimental biologists don't want to program, they want to use computers to do experimental biology. It's a tool, and they (quite reasonably) don't *care* about robustness, portability, etc. Or even about programming, to be honest. In the context of the original question, it's entirely reasonable (in my view) to tell this audience "if the code does something weird you don't understand, either ignore it and find another way or dig into the manuals and experiment if you care". They'd very quickly find a = a + b as a less confusing alternative to a += b. (As has been pointed out earlier, to some extent a += b is quite an advanced construct - after all, it's essentially an optimisation of a = a + b). Biologists don't expect me to understand their discipline before I can plant seeds in my garden, after all. (And when I do plant seeds, I usually get far more surprising results than I could get from a += b :-)) Paul. From pruebauno at latinmail.com Wed Jul 8 09:55:18 2009 From: pruebauno at latinmail.com (nn) Date: Wed, 8 Jul 2009 06:55:18 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: On Jul 7, 5:18?pm, kj wrote: > In Chris Rebert writes: > > >You might find the following helpful (partially): > >http://effbot.org/zone/call-by-object.htm > > Extremely helpful. ?Thanks! ?(I learned more from it than someone > who will teach the stuff would care to admit...) > > And it confirmed Paul Graham's often-made assertion that all of > programming language design is still catching up to Lisp... > > kj One possibility is to avoid teaching mutable datatypes at the beginning (i.e. no lists,dicts and sets), then you would have something more lispish. You would have to use tuples instead of lists for everything. That avoids the gotchas of mutable types at the cost of efficiency. Python is not a purist language but rather walks a fine line between high flexible abstractions and simple fast implementation. From no.email at please.post Wed Jul 8 10:04:35 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 14:04:35 +0000 (UTC) Subject: The meaning of "=" (Was: tough-to-explain Python) Message-ID: In kj writes: >I had not realized how *profoundly* different the meaning of the >"=" in Python's > spam = ham >is from the "=" in its > spam[3] = ham[3] To clarify, this comes from my reading of Fredrik Lundh's pages "Python Objects" (http://effbot.org/zone/python-objects.htm) and "Call By Object" (http://effbot.org/zone/call-by-object.htm). (Thanks to Chris Rebert for the pointer!) In the first one of these pages, Lundh writes: [START OF LENGTHY QUOTE] Assignment statements modify namespaces, not objects. In other words, name = 10 means that you're adding the name 'name' to your local namespace, and making it refer to an integer object containing the value 10. If the name is already present, the assignment replaces the original name: name = 10 name = 20 means that you're first adding the name 'name' to the local namespace, and making it refer to an integer object containing the value 10. You're then replacing the name, making it point to an integer object containing the value 20. The original '10' object isn't affected by this operation, and it doesn't care. In contrast, if you do: name = [] name.append(1) you're first adding the name 'name' to the local namespace, making it refer to an empty list object. This modifies the namespace. You're then calling a method on that object, telling it to append an integer object to itself. This modifies the content of the list object, but it doesn't touch the namespace, and it doesn't touch the integer object. Things like name.attr and name[index] are just syntactic sugar for method calls. The first corresponds to __setattr__/__getattr__, the second to __setitem__/__getitem__ (depending on which side of the assignment they appear). [END OF LENGTHY QUOTE] Therefore, extending just a bit beyond Lundh's explanation, if we did: name = [] name.append(1) name[0] = 3 ...the second assignment would amount to a method call on the object called 'name', an operation of a very different nature (according to Lundh) from the first assignment, which is a modification of a namespace. In the second one of these pages, Lundh makes a very similar point (third post from the bottom). But note that Lundh appears to be contradicting himself somewhat when he writes "Assignment statements modify namespaces, not objects." If by "assignment statements" he means ones consisting of a left operand, a "=", and a right operand, then according to the rest of what he writes on this subject, this assertion applies only to *some* assignment statements, namely those of the form = and not to those like, for example, [] = or . = The former are syntatic sugar for certain namespace modifications that leave objects unchanged. The latter are syntactic sugar for certain object-modifying method calls that leave namespaces unchanged. At least this is how I interpret what Lundh writes. kj From pdpinheiro at gmail.com Wed Jul 8 10:06:09 2009 From: pdpinheiro at gmail.com (pdpi) Date: Wed, 8 Jul 2009 07:06:09 -0700 (PDT) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> <91b1c921-52bf-4fd3-8361-4620fff93821@c9g2000yqm.googlegroups.com> <7xab3gz7ss.fsf@ruckus.brouhaha.com> <1695009b-12e9-426a-9aab-8073d4303d3b@t13g2000yqt.googlegroups.com> <1f09e856-bd04-4d27-bf51-5d67953a7e13@y7g2000yqa.googlegroups.com> <7xskh83r1v.fsf@ruckus.brouhaha.com> Message-ID: <9b819fa8-92f0-4b73-866f-95097d0a5b03@c1g2000yqi.googlegroups.com> On Jul 8, 2:24?am, Paul Rubin wrote: > pdpi writes: > > ? ? while abs(func(guess) - target) > epsilon: > > ? ? ? ? guess = (lo + hi) / 2. > > ? ? ? ? if sense * func(guess) > sense * target: > > ? ? ? ? ? ? hi = guess > > ? ? ? ? elif sense * func(guess) < sense * target: > > ? ? ? ? ? ? lo = guess > > ? ? ? ? elif lo == hi: > > ? ? ? ? ? ? return None > > ? ? return guess > > That is completely confusing. ?I get the, er, impression that "sense" > is supposed to change during the loop, and it takes much head > scratching to tell whether what you have there is right or not. ?Also, > it calls func 3 times on each loop, which could be extremely slow. > You don't know what func does, so eliminating 2/3 of the calls to it > is not a micro-optimization--it could be a major time saving. > > Yet another version: > > ? ? def _binary_search(x0, x1, func, target, epsilon): > ? ? ? ? y0,y1 = func(x0), func(x1) > ? ? ? ? while abs(y1 - target) > epsilon: > ? ? ? ? ? ? if x0 == x1 or cmp(y0,target) == cmp(y1,target): > ? ? ? ? ? ? ? ? return None > ? ? ? ? ? ? xn = (x0 + x1) / 2. > ? ? ? ? ? ? yn = func(xn) > ? ? ? ? ? ? if cmp(yn,target) == cmp(y0,target): > ? ? ? ? ? ? ? ?x0,y0 = xn,yn > ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ?x1,y1 = xn,yn > ? ? ? ? return x1 micro-optimization was perhaps too harsh, but it is an optimization that's not obvious for the newbie, and one best learnt from experience rather than having it spoon fed. I'll restate: you should start with the cleanest code possible and mangle it for performance. Then again, that implicitly assumes that f(x) is more readable than y, which is really a matter of taste... From futurebase at gmx.at Wed Jul 8 10:44:37 2009 From: futurebase at gmx.at (Daniel Austria) Date: Wed, 8 Jul 2009 07:44:37 -0700 (PDT) Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values Message-ID: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Hi python - hackers, just one question. How can i remove all 0 values in a list? Sure - i can loop over it, but that s not a neat style. list.remove() will only remove the first occurence. Doing that while no exception is raised is also uncool, right? Some suggestions? Best, Dan From paul at boddie.org.uk Wed Jul 8 10:53:44 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Wed, 8 Jul 2009 07:53:44 -0700 (PDT) Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> On 8 Jul, 16:04, kj wrote: > > ? = > > and not to those like, for example, > > ? [] = > > or > > ? . = > > The former are syntatic sugar for certain namespace modifications > that leave objects unchanged. ?The latter are syntactic sugar for > certain object-modifying method calls that leave namespaces unchanged. Almost. The latter can modify namespaces - the objects themselves - but through properties or dynamic attribute access, they may choose not to modify such a namespace. Really, we can phrase assignment (=) as follows: = # make refer to the result of Here, has to provide something that can be made to refer to something else, such as a name within a namespace - the first and last of your cases - or an item or slice within a sequence - the special second case which is actually handled differently from the other cases. Meanwhile, the will always provide an object to refer to, never anything of the nature of referring to something else. In other words, if you have this... x[1] = y[2] ...then the which is y[2] will yield an object which is then assigned to x[1]. The concept of y[2] is not assignable - it must be fully evaluated and produce the object at location #2 in the sequence for assignment. I suppose you could say that the left-hand side is like a sign on a signpost which always points to a real place, not another sign on a signpost. You could stretch this analogy by treating sequences as signposts holding many signs, each adjustable to point to something different. Since signposts (not the individual signs) are located in real places, they would naturally be acceptable as targets of assignments: where the signs are allowed to point to. Indeed, this would be a world of signposts with the occasional primitive value mixed in to keep weary travellers interested. ;-) Paul From mabdelkader at gmail.com Wed Jul 8 10:54:09 2009 From: mabdelkader at gmail.com (ma) Date: Wed, 8 Jul 2009 10:54:09 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> filter(lambda x: x, your_list) On Wed, Jul 8, 2009 at 10:44 AM, Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > > > Best, > Dan > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bruno.42.desthuilliers at websiteburo.invalid Wed Jul 8 10:57:16 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Wed, 08 Jul 2009 16:57:16 +0200 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <4a54b3bf$0$405$426a74cc@news.free.fr> Daniel Austria a ?crit : > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? the_list = [0, 0, 0, 1, 1, 1, 0] # Simple solution print [x for x in the_list if x != 0] # if you want to mutate the list 'in place': the_list[:] = [x for x in the_list if x != 0] print the_list HTH From djames.suhanko at gmail.com Wed Jul 8 10:59:38 2009 From: djames.suhanko at gmail.com (Djames Suhanko) Date: Wed, 8 Jul 2009 11:59:38 -0300 Subject: Remoting over SSH In-Reply-To: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> References: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> Message-ID: Hello! I wrote a litle program that send commands to many cluster nodes: #!/usr/bin/env python #By: Djames Suhanko #servers list sincroniza =["server1.domain","server2.domain", "server3.domain"] import pexpect import sys from threading import Thread #the user and pass can be in ini file. #Test parameters list try: if sys.argv[3]: pass except: print "Use: " + "script" + " " sys.exit() #This function send the command in argument def executor(command,username,password,server): a = 'ssh ' + username + '@' + server foo = pexpect.spawn(a) foo.expect('.*ssword:') foo.sendline(password) foo.sendline('su') foo.expect('.*sword:') foo.sendline('root_password_here') foo.sendline(command + '&& exit') print "command to: " + server + "..........[OK]" foo.sendline('exit') foo.expect('.*osed.') foo.interact() #make a list tasks = [] #theading loop for i in sincroniza: t = Thread(target=executor,args=(sys.argv[1],sys.argv[2],sys.argv[3],i)) t.start() tasks.append(t) #wait the end for t in tasks: t.join() it's all! On Wed, Jul 8, 2009 at 9:15 AM, Hendrik van Rooyen wrote: > "Hussein B" wrote: > >> Hey, >> I want to perform commands on a remote server over SSH. >> What do I need? >> Thanks. > > Access privileges for the remote machine. > > - Hendrik > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Djames Suhanko LinuxUser 158.760 From charles at declareSub.com Wed Jul 8 11:02:16 2009 From: charles at declareSub.com (Charles Yeomans) Date: Wed, 8 Jul 2009 11:02:16 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: On Jul 8, 2009, at 10:54 AM, ma wrote: > filter(lambda x: x, your_list) > > On Wed, Jul 8, 2009 at 10:44 AM, Daniel Austria > wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > L = [0, 0, 0, 1, 1, 1, 0] M = [x for x in L if x !=0] Charles Yeomans -------------- next part -------------- An HTML attachment was scrubbed... URL: From darcy at druid.net Wed Jul 8 11:03:15 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Wed, 8 Jul 2009 11:03:15 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: <20090708110315.634de5d2.darcy@druid.net> On Wed, 8 Jul 2009 10:54:09 -0400 ma wrote: > filter(lambda x: x, your_list) Or... [x for x in your_list if x] I'm not sure which one is more efficient but I like the syntax of the latter. A smart person could probably figure it out even without knowing Python syntax. Clarity is trump. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From fridrik at pyth.net Wed Jul 8 11:03:56 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Wed, 8 Jul 2009 15:03:56 +0000 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: ma wrote: > filter(lambda x: x, your_list) Good call! Equivalent but more efficient: filter(None, your_list) Regards, Fri?rik M?r From deets at nospam.web.de Wed Jul 8 11:06:23 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 08 Jul 2009 17:06:23 +0200 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <7bjqndF22l519U1@mid.uni-berlin.de> Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. Why not? If you need to potentially look at *all* elements of a list, nothing but a loop will take you there. OTOH, your proposed "remove until nothing is found"-thingy will become quadratic in behavior, as remove also loops over the list - so if you have list with say 10 ones followed by 10 zeros, you will loop ten times for 11 elements, which is in the order of (n/2)**2. Diez From nachogomez at gmail.com Wed Jul 8 11:14:36 2009 From: nachogomez at gmail.com (=?UTF-8?B?UmHDumwgR8OzbWV6IEMu?=) Date: Wed, 8 Jul 2009 10:44:36 -0430 Subject: Remoting over SSH In-Reply-To: References: <007901c9ffc5$cef87780$0d00a8c0@Hendrik> Message-ID: <684b0a740907080814t4f4911e1u9b417aaaba76e44a@mail.gmail.com> You also could use TwistedConch, which is an implementation of the SSH2 protocol for Python. I've done something with it, you can read my first questions on the TwistedConch list here . Hope that helps... -- Nacho Linux Counter #156439 -------------- next part -------------- An HTML attachment was scrubbed... URL: From no.email at please.post Wed Jul 8 11:42:09 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 15:42:09 +0000 (UTC) Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In <0778f257-d36c-4e13-93ea-bf8d448c82e2 at b15g2000yqd.googlegroups.com> Paul Boddie writes: >On 8 Jul, 16:04, kj wrote: >> >> =A0 =3D >> >> and not to those like, for example, >> >> =A0 [] =3D >> >> or >> >> =A0 . =3D >> >> The former are syntatic sugar for certain namespace modifications >> that leave objects unchanged. =A0The latter are syntactic sugar for >> certain object-modifying method calls that leave namespaces unchanged. >Almost. The latter can modify namespaces - the objects themselves - >but through properties or dynamic attribute access, they may choose >not to modify such a namespace. Really, we can phrase assignment (=3D) >as follows: > =3D # make refer to the result of > >Here, has to provide something that can be made to refer to >something else, such as a name within a namespace - the first and last >of your cases - or an item or slice within a sequence - the special >second case which is actually handled differently from the other >cases. Thanks for this correction. OK, so, scratching from my original post the case . = (as being a special case of = ), still, to the extent that I understand your post, the "=" in x = 1 means something fundamentally different (in terms of Python's underlying implementation) from the "=" in y[0] = 1 No? >You could stretch this analogy by treating sequences as >signposts holding many signs, each adjustable to point to something >different. Notionally, yes, I can see that, but there's no counterpart of this analogy at the level of Python's implementation. The "x" above is a sign, as you put it, i.e. an entry in a namespace, but "y[0]" is, in essence, a method call. kj From aahz at pythoncraft.com Wed Jul 8 11:44:32 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 08:44:32 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In article <0778f257-d36c-4e13-93ea-bf8d448c82e2 at b15g2000yqd.googlegroups.com>, Paul Boddie wrote: > >Almost. The latter can modify namespaces - the objects themselves - >but through properties or dynamic attribute access, they may choose >not to modify such a namespace. Really, we can phrase assignment (=) >as follows: > > = # make refer to the result of Right, except s/thing/target/ http://docs.python.org/reference/simple_stmts.html#assignment-statements -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From matt0177 at gmail.com Wed Jul 8 11:54:36 2009 From: matt0177 at gmail.com (Matthew Edmondson) Date: Wed, 8 Jul 2009 08:54:36 -0700 Subject: Python Error from Apress book In-Reply-To: <4A54687A.6010004@ieee.org> References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> <4A54687A.6010004@ieee.org> Message-ID: <8e2cbd660907080854p65bad365p54ccdad4771786a6@mail.gmail.com> I'm running XP SP3. The program now works great from either of the directories, as long as include 'python' before it. As far as looking at the error with stack trace, I really don't know enough yet to know how to do that. I'm running the file from command line, because I'm not sure how to run it referencing other files from idle. Lot to learn..... Once again, thanks a ton for all of the help. Matt On Wed, Jul 8, 2009 at 2:35 AM, Dave Angel wrote: > > > Gabriel Genellina wrote: > >>
En Tue, 07 >> Jul 2009 09:55:13 -0300, Dave Angel escribi?: >> >>> Gabriel Genellina wrote: >>> >>>> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >>>> escribi?: >>>> >>> >> When I try to run the command as outlined in >>>>> the book "simple_markup2.py < test_input.txt > test_output.html i get >>>>> the >>>>> following error every time. >>>>> >>>>> IOError: [Errno 9] Bad file descriptor >>>>> >>>> >>>> That's a Windows problem. When you execute the script as itself (either >>>> as you do in the command line, or by double-clicking on it), it doesn't have >>>> valid standard handles. >>>> You have to invoke Python explicitely: >>>> >>>> python simple_markup2.py < test_input.txt > test_output.html >>>> >>>> (you may need to specify the full path to python.exe, or add the >>>> directory where Python is installed to your system PATH). >>>> >>>> I use stdout this way all the time, with no problem (python 2.6, >>> Windows XP). But as you point out, stdin redirection doesn't seem to work >>> using the file associations. I do get a different error though. When I look >>> at sys.stdin, it shows an open file, with handle of zero, as expected. But >>> when I do a raw_input(), it gets: >>> EOFError: EOF when reading a line >>> >> >> I think the error depends on the specific OS version/service pack. But at >> least on XP this appears to fix it: >> >> http://support.microsoft.com/kb/321788/en-us >> >> Thanks for the link. Looking at that one, it indicates that Windows 2000 > fixed it in SP4, and XP fixed it in Sp1. But I'm already running XP SP3, so > I wonder if it's something new. > > Matt, what OS version are you running, and exactly what is happening when > the error occurs? (you should be able to figure that out from the stack > trace) > > > DaveA > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Jul 8 11:59:14 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Jul 2009 11:59:14 -0400 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <4A54C252.2090304@ieee.org> kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19 at q11g2000yqi.googlegroups.com> Simon Forman writes: > > >> Frankly, I'm of the impression that it's a mistake not to start >> teaching programming with /the bit/ and work your way up from there. >> I'm not kidding. I wrote a (draft) article about this: "Computer >> Curriculum" http://docs.google.com/View?id=dgwr777r_31g4572gp4 >> > > >> I really think the only good way to teach computers and programming is >> to start with a bit, and build up from there. "Ontology recapitulates >> phylogeny" >> > > > I happen to be very receptive to this point of view. I had the > benefit of that sort of training (one of the first computer courses > I took started, believe it or not, with Turing machines, through > coding in machine language, and compiler theory, and all the way > up to dabbling with Unix!), and I suspect that the reason it is > sometimes difficult for me to explain even relatively simple-looking > things to others is that I have this background that I unconsciously, > and incorrectly, take for granted in others... There is this > persistent idea "out there" that programming is a very accessible > skill, like cooking or gardening, anyone can do it, and even profit > from it, monetarily or otherwise, etc., and to some extent I am > actively contributing to this perception by teaching this course > to non-programmers (experimental biologists to be more precise), > but maybe this idea is not entirely true... Maybe, to get past > the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest > that real computer scientists learn about in their formal training... > > kj > > > And I learned to program Analog Computers, along with APL, in an early course. In my case assembly language came later, but by then we had covered the electronics behind transistors, and Karnough maps, logic diagrams, and so on. By the time I graduated, I had five six-level languages and two assembly languages under my belt. .DaveA From aahz at pythoncraft.com Wed Jul 8 11:59:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 08:59:19 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In article , kj wrote: > >OK, so, scratching from my original post the case > >. = > >(as being a special case of = ), still, >to the extent that I understand your post, the "=" in > > x = 1 > >means something fundamentally different (in terms of Python's >underlying implementation) from the "=" in > > y[0] = 1 > >No? No. ;-) What's different is not the ``=`` but the construction of the assignment target before ``=`` gets executed. Consider also this: x, y = y, x -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From xahlee at gmail.com Wed Jul 8 12:19:23 2009 From: xahlee at gmail.com (Xah Lee) Date: Wed, 8 Jul 2009 09:19:23 -0700 (PDT) Subject: OT: unix to Windows technology Message-ID: Dear unixers & lispers, i've been using Mac for the past 19 years, and been a professional sys admin or web app developers on the unix platform, since 1998 (maily Solaris, Apache, Perl, Java, SQL, PHP). In june, i bought a PC (not for the first time though), and made a switch to Windows, for the first time, in the sense as a developer instead of just a casual PC user i've been. In the past month, i've spend about 5 hours a day digging into MS Windows tech, in particluar, read over 200 Wikipedia articles in detail related to Windows technology. (192 of them linked) Here's a write up of the whole story, my experiences, including some tech introduction to MS Windows from a sys admin or programer point of view. ? Switching from Mac/Unix To PC/Windows http://xahlee.org/mswin/switch_to_windows.html Some slightly noteworthy subsections are: ? Removing HP/Compaq Software http://xahlee.org/mswin/hp_bundled_apps.html ? Installing Cygwin Tutorial http://xahlee.org/mswin/installing_cygwin.html ? Mac and Windows File Conversion http://xahlee.org/mswin/mac_windows_file_conv.html ? Unix And Windows File Permission Systems http://xahlee.org/mswin/file_perm_systems.html ? Introduction to Windows Scripting http://xahlee.org/mswin/windows_scripting.html Some articles (not shown above) are still work in progress, such as VBScript tutorial and PowerShell tutorial. Hoping to complete in the coming months or years. comment & feedback welcome, esp if you are a Windows expert and answer some of my unanswered questions on the page. Xah ? http://xahlee.org/ ? From james at agentultra.com Wed Jul 8 12:31:40 2009 From: james at agentultra.com (J Kenneth King) Date: Wed, 08 Jul 2009 12:31:40 -0400 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> Message-ID: <858wizdtlf.fsf@agentultra.com> Fri?rik M?r J?nsson writes: > ma wrote: >> filter(lambda x: x, your_list) > > Good call! Equivalent but more efficient: > > filter(None, your_list) > > Regards, > Fri?rik M?r I was wondering when someone would mention filter() From robert.kern at gmail.com Wed Jul 8 12:38:33 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 08 Jul 2009 11:38:33 -0500 Subject: Does cProfile include IO wait time? In-Reply-To: References: Message-ID: On 2009-07-04 19:03, Matthew Wilson wrote: > I have a command-line script that loads about 100 yaml files. It takes > 2 or 3 seconds. I profiled my code and I'm using pstats to find what is > the bottleneck. > > Here's the top 10 functions, sorted by internal time: > > In [5]: _3.sort_stats('time').print_stats(10) > Sat Jul 4 13:25:40 2009 pitz_prof > > 756872 function calls (739759 primitive calls) in 8.621 CPU seconds > > Ordered by: internal time > List reduced from 1700 to 10 due to restriction<10> > > ncalls tottime percall cumtime percall filename:lineno(function) > 15153 0.446 0.000 0.503 0.000 build/bdist.linux-i686/egg/yaml/reader.py:134(forward) > 30530 0.424 0.000 0.842 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:142(need_more_tokens) > 98037 0.423 0.000 0.423 0.000 build/bdist.linux-i686/egg/yaml/reader.py:122(peek) > 1955 0.415 0.000 1.265 0.001 build/bdist.linux-i686/egg/yaml/scanner.py:1275(scan_plain) > 69935 0.381 0.000 0.381 0.000 {isinstance} > 18901 0.329 0.000 3.908 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:113(check_token) > 5414 0.277 0.000 0.794 0.000 /home/matt/projects/pitz/pitz/__init__.py:34(f) > 30935 0.258 0.000 0.364 0.000 build/bdist.linux-i686/egg/yaml/scanner.py:276(stale_possible_simple_keys) > 18945 0.192 0.000 0.314 0.000 /usr/local/lib/python2.6/uuid.py:180(__cmp__) > 2368 0.172 0.000 1.345 0.001 build/bdist.linux-i686/egg/yaml/parser.py:268(parse_node) > > I expected to see a bunch of my IO file-reading code in there, but I don't. So > this makes me think that the profiler uses CPU time, not > clock-on-the-wall time. It should be basically wall-clock time on Linux. The timer function underneath is gettimeofday(2). Look in Modules/_lsprof.c of the Python sources for the function hpTimer(). -- 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 no.email at please.post Wed Jul 8 12:38:48 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 16:38:48 +0000 (UTC) Subject: The meaning of "=" (Was: tough-to-explain Python) References: <0778f257-d36c-4e13-93ea-bf8d448c82e2@b15g2000yqd.googlegroups.com> Message-ID: In aahz at pythoncraft.com (Aahz) writes: >In article , kj wrote: >> >>OK, so, scratching from my original post the case >> >>. = >> >>(as being a special case of = ), still, >>to the extent that I understand your post, the "=" in >> >> x = 1 >> >>means something fundamentally different (in terms of Python's >>underlying implementation) from the "=" in >> >> y[0] = 1 >> >>No? >No. ;-) No??? Just when I thought I finally understood all this! >What's different is not the ``=`` but the construction of the >assignment target before ``=`` gets executed. Hmm. OK, I went to the link you posted in your other message (http://docs.python.org/reference/simple_stmts.html#assignment-statements) and I find this (my emphasis): Assignment of an object to a single target is recursively defined as follows. * If the target is an identifier (name): o If the name does not occur in a global statement in the current code block: the name is bound to the object ^^^^^^^^^^^^^^^^^ in the current local namespace. o Otherwise: the name is bound to the object in the ^^^^^^^^^^^^^^^^^ current global namespace. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. * If the target is a target list enclosed in parentheses or in square brackets... (I'LL IGNORE THIS FOR NOW) * If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case, TypeError is raised. That object is then asked to assign the assigned ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object to the given attribute; if it cannot perform the ^^^^^^ assignment, it raises an exception (usually but not necessarily AttributeError). * If the target is a subscription: The primary expression in the reference is evaluated. It should yield either a mutable sequence object (such as a list) or a mapping object (such as a dictionary). Next, the subscript expression is evaluated. If the primary is a mutable sequence object (such as a list),... [CONDITIONS ON THE INDEX EXPRESSION OMITTED]... the sequence is asked to assign the assigned object to its ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ item with that index.... If the primary is a mapping object (such as a dictionary),... [CONDITIONS ON THE SUBSCRIPT EXPRESSION OMITTED]... the ^^^ mapping is then asked to create a key/datum pair which maps ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the subscript to the assigned object. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * If the target is a slicing: [INDEX STUFF OMITTED]... the ^^^ sequence object is asked to replace the slice with the items ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ of the assigned sequence... ^^^^^^^^^^^^^^^^^^^^^^^^ OK, I originally interpreted what Lundh wrote in his two articles that the "binding" described at the very beginning (i.e. when the target is an identifier), which I take to make an entry or modify such an entry in a namespace, is a *categorically different* operation from the remaining operations underlined above. I interpreted Paul Boddie's correction in his response to me as saying that the "assignment" mentioned for the case when the target is an attribute reference is actually a special case of the "assignment" to simple identifiers (i.e. it also means "binding"). But that still leaves all the other "assignments" (or the like) underlined above. I don't think that the full definitions of these remaining cases are covered by the same rule, even though the rule is described as "recursive." I think that the writer has something else in mind, and in particular, something *other* than binding, but the author remains vague on exactly what this means. Clearly, both Lundh and the documentation draw some distinction between "binding" and some other forms of "assignment" (which remain ill-defined throughout). This distinction is what I was referring to when I said that "=" means different things in different contexts. kj From fridrik at pyth.net Wed Jul 8 12:44:47 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Wed, 8 Jul 2009 16:44:47 +0000 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <858wizdtlf.fsf@agentultra.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <148918f0907080754i31e8b2e5i4132a51f94d204e1@mail.gmail.com> <858wizdtlf.fsf@agentultra.com> Message-ID: <7D21C328-8320-4DF8-9E8B-08093441D270@pyth.net> J Kenneth King wrote: > I was wondering when someone would mention filter() I was happy to see that too. It's clean, faster than list comprehension and in terms of clarity it's only to be expected that the developer is familiar with, or at least willing to look up, the available built-in methods. Regards, Fri?rik M?r From kentilton at gmail.com Wed Jul 8 12:51:57 2009 From: kentilton at gmail.com (Kenneth Tilton) Date: Wed, 08 Jul 2009 12:51:57 -0400 Subject: OT: unix to Windows technology In-Reply-To: References: Message-ID: <4a54ceb0$0$5898$607ed4bc@cv.net> Xah Lee wrote: > Dear unixers & lispers, > > i've been using Mac for the past 19 years, and been a professional sys > admin or web app developers on the unix platform, since 1998 (maily > Solaris, Apache, Perl, Java, SQL, PHP). In june, i bought a PC (not > for the first time though), and made a switch to Windows, for the > first time, in the sense as a developer instead of just a casual PC > user i've been. > > In the past month, i've spend about 5 hours a day digging into MS > Windows tech, in particluar, read over 200 Wikipedia articles in > detail related to Windows technology. (192 of them linked) > > Here's a write up of the whole story, my experiences, including some > tech introduction to MS Windows from a sys admin or programer point of > view. > > ? Switching from Mac/Unix To PC/Windows > http://xahlee.org/mswin/switch_to_windows.html > > Some slightly noteworthy subsections are: > > ? Removing HP/Compaq Software > http://xahlee.org/mswin/hp_bundled_apps.html > > ? Installing Cygwin Tutorial > http://xahlee.org/mswin/installing_cygwin.html > > ? Mac and Windows File Conversion > http://xahlee.org/mswin/mac_windows_file_conv.html > > ? Unix And Windows File Permission Systems > http://xahlee.org/mswin/file_perm_systems.html > > ? Introduction to Windows Scripting > http://xahlee.org/mswin/windows_scripting.html > > Some articles (not shown above) are still work in progress, such as > VBScript tutorial and PowerShell tutorial. Hoping to complete in the > coming months or years. > > comment & feedback welcome, esp if you are a Windows expert and answer > some of my unanswered questions on the page. > > Xah > ? http://xahlee.org/ > > ? You just discovered PCs are cheaper? The funny thing is that that is Microsoft's answer to the Apple Mac-PC ads, they show people shopping for computers and just comparing hardware and price as if this is some kind of breakthrough. But I understand: they have no answer to Windows being such a nightmare and the Mac being such a joy. kt From tjreedy at udel.edu Wed Jul 8 12:57:31 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 08 Jul 2009 12:57:31 -0400 Subject: The meaning of "=" (Was: tough-to-explain Python) In-Reply-To: References: Message-ID: kj wrote: > To clarify, this comes from my reading of Fredrik Lundh's pages > "Python Objects" (http://effbot.org/zone/python-objects.htm) and > "Call By Object" (http://effbot.org/zone/call-by-object.htm). [snip] > [END OF LENGTHY QUOTE] > > Therefore, extending just a bit beyond Lundh's explanation, if we > did: > > name = [] > name.append(1) > name[0] = 3 > > ...the second assignment would amount to a method call on the object > called 'name', an operation of a very different nature (according > to Lundh) from the first assignment, which is a modification of a > namespace. I disagree. Assignment creates an association. Modification of a namespace, when implemented, amounts to a method call on the concrete object, whether a Python object or not, that implements the abstraction of a namespace. At module scope, name = ob is the same as globals()['name']=ob Within a class statement, substitute '' for 'globals' Within functions, CPython uses an internal array, so name = ob becomes [name-number] = ob Or, to put it another way, Python dicts and lists are, considered abstractly, associations also, just like namespaces. Dicts are more general than namespaces, sequences are 'number-spaces' instead of name-spaces. Terry Jan Reedy From nobody at nowhere.com Wed Jul 8 13:03:35 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 08 Jul 2009 18:03:35 +0100 Subject: Python and webcam capture delay? References: <6xB4m.21104$vi5.8567@uutiset.elisa.fi> Message-ID: On Tue, 07 Jul 2009 22:49:22 -0700, Dennis Lee Bieber wrote: > On Tue, 07 Jul 2009 23:37:43 +0100, Nobody > declaimed the following in gmane.comp.python.general: > >> AFAIK, the only real difference between "USB-1 conformant" and "USB-2 >> conformant" is that the latter actually passed a test of its ability to >> say "no, I can't do high-speed", while the former didn't. The check for >> high-speed capability was designed such that it shouldn't cause problems >> for USB-1 devices, but individual USB-1 devices weren't actually tested >> for this. > > USB-1 or USB-1.1? As I recall the latter is capable of 11Mbps > whereas the original USB spec was much lower... (of course, USB 2 at top > is 480Mbps) USB 1.0 supports 1.5Mbps (low-speed) and 12MBps (full-speed). Hosts and hubs must support both, functions (devices) can use either. USB 1.1 was a bug-fix release which solved some interoperability issues arising from ambiguities in the USB 1.0 standard. From lucas_junqueira at yahoo.com Wed Jul 8 13:07:33 2009 From: lucas_junqueira at yahoo.com (Lucas Junqueira) Date: Wed, 8 Jul 2009 10:07:33 -0700 (PDT) Subject: windows command-line Message-ID: <376017.28322.qm@web30601.mail.mud.yahoo.com> Hi, I'd like to run a simple windows command-line program from within my python script and agt all the returt it generates. Is this possible? How can I do it? Thank you! ____________________________________________________________________________________ Veja quais s?o os assuntos do momento no Yahoo! +Buscados http://br.maisbuscados.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From cameron.pulsford at gmail.com Wed Jul 8 13:09:43 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Wed, 8 Jul 2009 13:09:43 -0400 Subject: Problem with list of dicts and copying Message-ID: <5700df6b0907081009p5ec4167cqec36e915e0f6c4@mail.gmail.com> Hello all, I'm redoing a sudoku solver of mine and I ran into an issue with lists of dicts. Bear with me for a second before I get to the actual problem... I'm representing the board as a dictionary, where the keys are (x, y) positions, and the values are candidates. So my program goes along picking numbers from the list of candidates and then propagating the constraints. So it might place a 6, which would then remove 6 as a candidate from that row column and grid. However, that might leave a spot with no candidates, which means we placed a legal but not correct number. This is where my problem arises. I keep a running list of the boards which is updated every time I find a legal number that doesn't invalidate the boards. I add onto this list by doing "boards.append(self.board.copy())". When I place a legal but invalid number I do "self.board = boards[-1].copy()" to return the board to the last known good state. And when I completely exhaust the candidates for a spot and must backtrack to a different spot (instead of simply trying a different candidate of the current spot) I do "self.board = boards[-1].copy() and then del boards[-1]" Basically my boards list should be copies of good boards, and when I need to refresh the current board, I want to pull off a copy of the correct one. Essentially (after watching the debugging output) it looks like this isn't happening. I am always modifying the same board. The algo follows, and I can post the rest of the code if necessary. So am I using the dict.copy() wrong? Am I guessing I have a tricky problem with the "=" in "self.board = boards[-1].copy()" def solve(self): previousValue = dict((blank, 0) for blank in self.blanks()) self.fillInCandidates() continuing, boards, guesses = False, [self.board.copy()], 0 while len(self.blanks()) > 0: if continuing == False: cp = self.mrv() continuing = False nl = self.nextLegal(cp, previousValue[cp]) previousValue[cp] = nl if nl != 0: self.board[cp] = nl guesses += 1 self.update(nl, cp[0], cp[1]) if self.isNotLegal(): continuing = True self.board = boards[-1].copy() else: boards.append(self.board.copy()) else: previousValue[cp] = 0 self.board = boards[-1].copy() del boards[-1] -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.com Wed Jul 8 13:19:16 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 08 Jul 2009 18:19:16 +0100 Subject: Python and webcam capture delay? References: <0ca855t29jklb0o0gi31ourfk3lstdiepf@4ax.com> Message-ID: On Tue, 07 Jul 2009 22:11:12 -0700, Tim Roberts wrote: >>The webcam is bound to do some encoding; most of them use USB "full speed" >>(12Mbit/sec), which isn't enough for raw 640x480x24bpp at 30fps data. > > That's not true. Most of the web cams made in the last 5 years or so run > at high speed, 480 Mbps. Full speed only gets you 1 fps at 640x480 > uncompressed, so it's really only useful for the most primitive video > conference cams. The very earliest models typically only did 320x200, while later USB-1 webcams used onboard compression to get a decent framerate. For internet use, 12Mbps isn't that much of an obstacle; the internet connection's upload speed is more likely to be the limiting factor. Faster speeds are more useful for things like LAN-based CCTV systems. From emile at fenx.com Wed Jul 8 13:23:54 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 08 Jul 2009 10:23:54 -0700 Subject: windows command-line In-Reply-To: <376017.28322.qm@web30601.mail.mud.yahoo.com> References: <376017.28322.qm@web30601.mail.mud.yahoo.com> Message-ID: On 7/8/2009 10:07 AM Lucas Junqueira said... > Hi, I'd like to run a simple windows command-line program from within my > python script and agt all the returt it generates. Is this possible? How > can I do it? Depending on python version, look into subprocess, commands or os.pipe and related. Emile From nobody at nowhere.com Wed Jul 8 13:25:49 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 08 Jul 2009 18:25:49 +0100 Subject: Check file is locked? References: Message-ID: On Tue, 07 Jul 2009 21:31:12 -0700, Rajat wrote: >> > By the way most operating systems don't lock a file when it's opened for >> > reading or writing or even executed. >> >> The general conclusion seems to be that mandatory locking is more trouble >> than it's worth. > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > application ( which could be a notepad, a wordpad or some other text > editor) that have opened my file. Once the file is closed I can easily > delete that file. > > I guess, if file is opened by one of that application, the file must > be locked and so is the reason I cannot delete the file. Windows doesn't allow you to delete (or rename) open files. It's up to the application as to whether it keeps the file open or closes it once it has been read. > Please suggest if this is fine. If you can't delete the file, then you can't delete it. You shouldn't be trying to kill off applications so that you can delete the file. Just do what other Windows programs (including those which are part of Windows) do: display a dialog telling the user to close the application then try again. From xahlee at gmail.com Wed Jul 8 13:34:50 2009 From: xahlee at gmail.com (Xah Lee) Date: Wed, 8 Jul 2009 10:34:50 -0700 (PDT) Subject: OT: unix to Windows technology References: <4a54ceb0$0$5898$607ed4bc@cv.net> Message-ID: <16430ee7-888e-42ef-9c84-14df4b78ee04@p36g2000prn.googlegroups.com> Xah Lee wrote: > ? Switching from Mac/Unix To PC/Windows > http://xahlee.org/mswin/switch_to_windows.html Kenneth Tilton wrote: > You just discovered PCs are cheaper? > > The funny thing is that that is Microsoft's answer to the Apple Mac-PC > ads, they show people shopping for computers and just comparing hardware > and price as if this is some kind of breakthrough. But I understand: > they have no answer to Windows being such a nightmare and the Mac being > such a joy. well, i've been owning Macs for over the past 19 years. From a dedicated fan thru-out the 1990s, to still fan in early of 2000s with debut of OS X. Mac prices in comparison to PC has gone up and downs. In the early 1990s for example, it is maybe 4 times more. Lowest is probably in the mid 1990s, where 3rd party companies are licensed to producing clones, and that happens to also be a period that Mac OS is the worst, crashing few times per day, similar to Win 95 and 98. I think in the early 2000s the price gap came closer, and since maybe 2005 it start to increase again. for someone like me, i've read all the pros and cons of Mac vs PC. I've read, for instance, basically all MacWorld and MacUser mags in the early 1990s until they become defunct. (and often MacWeek too) Not to mention the huge amount of websites, especially in the late 1990s where there are a number of high profile dedicated mac fan sites. I also have often attended the yearly Mac World Expo, eagerly antipating Steve Job's ?O, one more thing...? etc... and etc and etc. As a hyperbole, i've prob read more Mac vs PC argument from newsgroup users combined. LOL as to the price comparison... a better metric is value/price ratio. I think, over the past 20 years, the value/price ratio of Mac compared to PC, namely: (MacValue/MacPrice)/(PC_value/PC_price) is going down, starting with Windows NT 4, and going downhill quickly with Windows XP, and Vista, .NET. as far as SOFTWARE technology goes, Apple's falling lamer and lamer from its 1990s mountain top of lisps and hypercards and desktop publishing stuff. Microsoft Windows, starting from MS-DOS moronicity, today, with its .NET, F#, PowerShell, is quite beyond Apple's perpetual diddling with its prettification of OS X. Xah ? http://xahlee.org/ ? From paul.lafollette at gmail.com Wed Jul 8 13:35:44 2009 From: paul.lafollette at gmail.com (Paul LaFollette) Date: Wed, 8 Jul 2009 13:35:44 -0400 Subject: function local namespace question Message-ID: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Kind people, Using Python 3.1 under FreeBSD and WinXP. I've been tearing my hair out trying to solve this myself, but I need to ask for help. I want (for obscure reasons) to be able to log transactions in the namespace(s) of a script. Specifically I would like to log creation of identifiers, changes in the binding of identifiers ("assignment") and lookups. This turns out to be pretty easy in the global and local namespaces... I simply subclass dict, override the appropriate operations to include the logging operations I want, and then exec the code using my dictionaries as the global and local namespaces. All of this works just dandy until I try to extend it to functions. I cannot figure out any way to get a hook into the local namespace of a user defined function. I have tried making a wrapper class that grabs the function call and then uses exec to invoke myfunction.__code__ with my own dictionaries. This runs the (no argument) function properly (losing the return value, but I can deal with that) but never accesses the local logging dictionary that I specify in the exec() call. Perhaps the local namespace of a function is not a dict at all? Anyway, is there any way (however clumsy) to do what I want to do? Thank you for your help. Paul --------------------------- Paul LaFollette CIS Department Temple University paul.lafollette(at)temple.edu www.cis.temple.edu/~lafollet From Lacrima.Maxim at gmail.com Wed Jul 8 13:52:44 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Wed, 8 Jul 2009 10:52:44 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. Message-ID: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> Hello! I have just started using Emacs to write python scripts. I installed python-mode.el Then I just tried this code: print 'hello world' When I press C-c RET, new blank window is opened and emacs says: (Shell command succeeded with no output) So where is my 'hello world'? When I do C-c C-c, it prints 'hello world' successfully. Why in the first case I get no output? Thanks in advance! With regards, Max (sorry if my English isn't very proper) From sajmikins at gmail.com Wed Jul 8 14:01:28 2009 From: sajmikins at gmail.com (Simon Forman) Date: Wed, 8 Jul 2009 11:01:28 -0700 (PDT) Subject: ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: On Jul 8, 10:44?am, Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. ?list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > > Best, > Dan If you are doing something like this: L = [0, 0, 0, 1, 1, 1, 0] removeZeros(L) number_of_ones = len(L) you can just use sum() like so: number_of_ones = sum(L) HTH From no.email at please.post Wed Jul 8 14:10:08 2009 From: no.email at please.post (kj) Date: Wed, 8 Jul 2009 18:10:08 +0000 (UTC) Subject: ISO library ref in printed form References: Message-ID: In Piet van Oostrum writes: >>>>>> kj (kj) wrote: >>kj> Does anyone know where I can buy the Python library reference in >>kj> printed form? (I'd rather not print the whole 1200+-page tome >>kj> myself.) I'm interested in both/either 2.6 and 3.0. >Maybe you can have a copy printed at lulu.com. Interesting idea... Doesn't look like they offer such service. >It would even be nicer if >the PSF would offer them at lulu. That would be great. kj From http Wed Jul 8 14:43:19 2009 From: http (Paul Rubin) Date: 08 Jul 2009 11:43:19 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Message-ID: <7xiqi3dni0.fsf@ruckus.brouhaha.com> Bearophile writes: > > ? ? print x, len(tuple(g)) > > Avoid that len(tuple(g)), use something like the following print x, sum(1 for _ in g) From http Wed Jul 8 14:49:52 2009 From: http (Paul Rubin) Date: 08 Jul 2009 11:49:52 -0700 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> Message-ID: <7xd48bdn73.fsf@ruckus.brouhaha.com> Daniel Austria writes: > just one question. How can i remove all 0 values in a list? I prefer: newlist = list(x for x in oldlist if x != 0) to the square bracket list comprehension that a few people have suggested. This is because in python 2.x, the listcomp "leaks" its index variable into the surrounding scope, but the generator expression above doesn't. Usually not a big deal, but an extra bit of hygiene never(?) hurts. From fetchinson at googlemail.com Wed Jul 8 14:56:39 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Wed, 8 Jul 2009 11:56:39 -0700 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531560$0$9355$426a74cc@news.free.fr> Message-ID: >> But this academic discussion is honestly a little pointless. The OP >> was referring to a expectation, coming from C, that is not fulfilled >> in python. What's wrong with mentioning it somewhere for the sake of >> helping C programmers? >> > And where does one stop? After all, my primary work language at the > time I first encountered Python was FORTRAN77; and my home system at the > time was an Amiga with ARexx... (granted, I did have a C compiler on it > -- which I do not have on this WinXP machine)... And programming, even > in C, on the Amiga still inflicted BCPL concepts upon one ("AmigaDOS", > the "user" view, was a port of Tripos user view on top of the Amiga > executive libraries). > > Do we mention how Python differs from F77, F90, Ada, Rexx, LISP, > RPG, APL, Pascal, BASIC, and COBOL (I've done all except RPG since > graduating high school). > > In my mind... Anyone whose only experience with programming language > concepts is C/C++ deserves the shock of finding that other languages ARE > DIFFERENT! Even if they never use other languages they should at least > have been exposed to language design options and rationales... > > Okay, I don't know what current curricula consist of, but in the > late 70s, a CS degree at my college required two sessions of FORTRAN, > two of COBOL, Assembly (numerically, it followed advanced FORTRAN), > database (followed COBOL)... and then diverged into Business Programming > vs Systems Programming (Business required lots of accounting/statistics > courses [stat-II was SPSS], Systems was operating system and language > design). Electives included BASIC, Pascal, APL (The professor for the > data structures course made the mistake of once allowing an assignment > -- hashed head multiple-linked list -- to be done in any language he > could read ; I did it in a BASIC that only supported 4 open files at > a time... I think someone did it in SNOBOL) > > C wasn't available on the XEROX Sigma-7; I did have a first edition > K&R. By graduation I'd also been exposed to the initial Ada > reference/rational (a working compiler -- Ada/Ed -- didn't come out > until after I'd graduated). Okay, so where does one stop? I'd say C deserves special treatment as opposed to all the other languages you mentioned because Guido himself admits to influences from C (and ABC but I hope you won't assume that ABC is a widely used language). Maybe you didn't read these messages from me in this thread: By the way, assignments in conditionals. Guido explicitly referred to C when he forbade assignment in conditionals, citing common typos/errors in C code such as if( x = 5 ){ .... } instead of if( x == 5 ){ ..... }. So even he realized that warning people about different usage in python and C is a good thing. Expectations from C work sometimes, and sometimes they don't. In latter case a little warning is useful. And also, Seriously, ask Guido about the influence of C vs. fortran (or cobol, ada, pascal, etc). Somewhere you can find him quoted as saying that python was originally intended to "bridge the gap between the shell and C". I've never heard him talk about fortran (or cobol, ada, pascal, etc). So don't get me wrong, I'm sufficiently impressed by your knowledge of various computer languages, I admit to only knowing C and basic and some fortran (and of course python :)), but if Guido himself thinks the influence of C on python is more important than the others, then let's not doubt him. And yes, I shamelessly admit to arguing based on a higher authority and not based on merit, but in this case it's appropriate, I think :) Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From backup95 at netcabo.pt Wed Jul 8 15:32:36 2009 From: backup95 at netcabo.pt (=?ISO-8859-1?Q?Jo=E3o_Valverde?=) Date: Wed, 08 Jul 2009 20:32:36 +0100 Subject: ISO library ref in printed form In-Reply-To: References: Message-ID: <4A54F454.9060007@netcabo.pt> kj wrote: > Does anyone know where I can buy the Python library reference in > printed form? (I'd rather not print the whole 1200+-page tome > myself.) I'm interested in both/either 2.6 and 3.0. > > TIA! > > kj > Why not download the documentation, take it to a local copy shop and have it printed and bound there? http://docs.python.org/download.html http://docs.python.org/3.1/download.html From aahz at pythoncraft.com Wed Jul 8 15:33:57 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 12:33:57 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Message-ID: In article <050094ea-faf4-4e03-875d-9c2c63090a89 at y17g2000yqn.googlegroups.com>, Bearophile wrote: >Vilya Harvey: >> >> from itertools import groupby >> for x, g in groupby([fields[1] for fields in data]): >> =A0 =A0 print x, len(tuple(g)) > >Avoid that len(tuple(g)), use something like the following, it's lazy >and saves some memory. The question is whether it saves time, have you tested it? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From piet at cs.uu.nl Wed Jul 8 15:54:23 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 21:54:23 +0200 Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> Message-ID: >>>>> Lacrima (L) wrote: >L> Hello! >L> I have just started using Emacs to write python scripts. >L> I installed python-mode.el >L> Then I just tried this code: >L> print 'hello world' >L> When I press C-c RET, new blank window is opened and emacs says: >L> (Shell command succeeded with no output) >L> So where is my 'hello world'? >L> When I do C-c C-c, it prints 'hello world' successfully. >L> Why in the first case I get no output? Can you check in the buffer where you have the python code what command C-c RET is bound to? With C-h k C-c RET Shell command succeeded with no output suggests that is has a different binding than the standard one in python-mode. Did you happen to name your file 'test' or 'test.py? C-c RET does an import and 'import test' imports a standard module test. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 8 15:56:01 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 08 Jul 2009 21:56:01 +0200 Subject: ISO library ref in printed form References: Message-ID: >>>>> kj (k) wrote: >k> In Piet van Oostrum writes: >>>>>>>> kj (kj) wrote: >kj> Does anyone know where I can buy the Python library reference in >kj> printed form? (I'd rather not print the whole 1200+-page tome >kj> myself.) I'm interested in both/either 2.6 and 3.0. >>> Maybe you can have a copy printed at lulu.com. >k> Interesting idea... Doesn't look like they offer such service. You can upload your own PDF file and have them print a single copy. You would have to split the file yourself in manageble pieces (< 600 something pages) >>> It would even be nicer if >>> the PSF would offer them at lulu. >k> That would be great. >k> kj -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From milesck at umich.edu Wed Jul 8 16:50:44 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 8 Jul 2009 16:50:44 -0400 Subject: function local namespace question In-Reply-To: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> References: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Message-ID: On Jul 8, 2009, at 1:35 PM, Paul LaFollette wrote: > I cannot figure out any way to get a hook into the local namespace of > a user defined function. I have tried making a wrapper class that > grabs the function call and then uses exec to invoke > myfunction.__code__ with my own dictionaries. This runs the (no > argument) function properly (losing the return value, but I can deal > with that) but never accesses the local logging dictionary that I > specify in the exec() call. Perhaps the local namespace of a function > is not a dict at all? Right. Within functions, local variable name accesses and assignments are compiled into bytecode instructions (LOAD_FAST, STORE_FAST) that manipulate pointers to objects by indexing directly into a C array in the frame. The "locals" dictionary of a frame is generated on-demand from that array when accessed. There is no way that I'm aware of to directly hook into function namespace access and manipulation. -Miles From http Wed Jul 8 17:45:53 2009 From: http (Paul Rubin) Date: 08 Jul 2009 14:45:53 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> Message-ID: <7xbpnuzw4u.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > >Avoid that len(tuple(g)), use something like the following, it's lazy > >and saves some memory. > The question is whether it saves time, have you tested it? len(tuple(xrange(100000000))) ... hmm. From david.bramer at googlemail.com Wed Jul 8 18:06:22 2009 From: david.bramer at googlemail.com (David) Date: Wed, 8 Jul 2009 15:06:22 -0700 (PDT) Subject: regex help Message-ID: Hi I have a few regexs I need to do, but im struggling to come up with a nice way of doing them, and more than anything am here to learn some tricks and some neat code rather than getting an answer - although thats obviously what i would like to get to. Problem 1 - (25.47%)
I want to extract 25.47 from here - so far I've tried - xPer = re.search('(.*?)%', content) and xPer = re.search('\((\d*)%\)
', content) neither of these seem to do what I want - am I not doing this correctly? (obviously!) Problem 2 -   Open: 5.50   Mkt Cap: 6.92M   P/E: 21.99 I want to extract the open, mkt cap and P/E values - but apart from doing loads of indivdual REs which I think would look messy, I can't think of a better and neater looking way. Any ideas? Cheers David From aahz at pythoncraft.com Wed Jul 8 18:22:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 Jul 2009 15:22:19 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: In article <7xbpnuzw4u.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: >aahz at pythoncraft.com (Aahz) writes: >>Paul Rubin deleted an attribution: >>> >>>Avoid that len(tuple(g)), use something like the following, it's lazy >>>and saves some memory. >> >> The question is whether it saves time, have you tested it? > >len(tuple(xrange(100000000))) ... hmm. When dealing with small N, O() can get easily swamped by the constant factors. How often do you deal with more than a hundred fields? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From digitig at gmail.com Wed Jul 8 18:25:21 2009 From: digitig at gmail.com (Tim Rowe) Date: Wed, 8 Jul 2009 23:25:21 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: <006e7ce0$0$9711$c3e8da3@news.astraweb.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <006e7ce0$0$9711$c3e8da3@news.astraweb.com> Message-ID: 2009/7/7 Steven D'Aprano : > Maybe the reason for "so much buggy software" is that people > inappropriately use assert, thus changing the behaviour of code depending > on whether it is run with the -O flag or not. I've done my share of code review and process audits, and assertions seem *far* to rare for that distinction. > I don't know what "hostility" you're seeing. The only hostility I'm > seeing is from the OP, which is bizarre considering that he asked for > advice and we gave it. What I see is a bunch of people concerned that the > OP is teaching novices a bad habit, namely, using assert for error > checking. He claims -- angrily and over and over again -- that in his > code, the assertions should never fail. Great. Good for him for knowing > when to use assert. But are the novices going to learn that lesson, or > will they simply learn "use assert for error checking"? They are rather more likely to learn if they are taught, aren't they? Or do you think it's better to keep them in the dark? "There are these things called assertions -- work them out for yourselves". I am convinced that the time to teach programmers to consider under what circumstances a routine can be called and who is responsible for ensuring that those conditions are met is as soon as they hit the concept of calling routines. And assertions provide an excellent way of doing that, fostering good habits for the rest of their careers. Any hostility from the OP seems to be a response to the persistent refusal to accept his assurances that he is not using the assertions for run-time error checking, nor teaching the students to do that, -- Tim Rowe From http Wed Jul 8 18:28:17 2009 From: http (Paul Rubin) Date: 08 Jul 2009 15:28:17 -0700 Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: <7xvdm2n726.fsf@ruckus.brouhaha.com> aahz at pythoncraft.com (Aahz) writes: > When dealing with small N, O() can get easily swamped by the constant > factors. How often do you deal with more than a hundred fields? The number of fields in the OP's post was not stated. Expecting it to be less than 100 seems like an ill-advised presumption. If N is unknown, speed-tuning the case where N is small at the expense of consuming monstrous amounts of memory when N is large sounds somewhere between a premature optimization and a nasty bug. From clp2 at rebertia.com Wed Jul 8 18:37:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 8 Jul 2009 15:37:48 -0700 Subject: regex help In-Reply-To: References: Message-ID: <50697b2c0907081537v44c45510kf8e9f143e2213a0@mail.gmail.com> On Wed, Jul 8, 2009 at 3:06 PM, David wrote: > Hi > > I have a few regexs I need to do, but im struggling to come up with a > nice way of doing them, and more than anything am here to learn some > tricks and some neat code rather than getting an answer - although > thats obviously what i would like to get to. > > Problem 1 - > > ? ? ? ? ? ? ? ?id="ref_678774_cp">(25.47%)
> > I want to extract 25.47 from here - so far I've tried - > > xPer = re.search(' \">(.*?)%', content) > > and > > xPer = re.search(' \">\((\d*)%\)
', content) > > neither of these seem to do what I want - am I not doing this > correctly? (obviously!) > > Problem 2 - > >   > > Open: > > 5.50 > >   > Mkt Cap: > > 6.92M > >   > P/E: > > 21.99 > > > > I want to extract the open, mkt cap and P/E values - but apart from > doing loads of indivdual REs which I think would look messy, I can't > think of a better and neater looking way. Any ideas? Use an actual HTML parser? Like BeautifulSoup (http://www.crummy.com/software/BeautifulSoup/), for instance. I will never understand why so many people try to parse/scrape HTML/XML with regexes... Cheers, Chris -- http://blog.rebertia.com From usernet at ilthio.net Wed Jul 8 18:43:41 2009 From: usernet at ilthio.net (Tim Harig) Date: Wed, 08 Jul 2009 22:43:41 GMT Subject: regex help References: Message-ID: On 2009-07-08, Chris Rebert wrote: > On Wed, Jul 8, 2009 at 3:06 PM, David wrote: >> I want to extract the open, mkt cap and P/E values - but apart from >> doing loads of indivdual REs which I think would look messy, I can't >> think of a better and neater looking way. Any ideas? You are downloading market data? Yahoo offers its stats in CSV format that is easier to parse without a dedicated parser. > Use an actual HTML parser? Like BeautifulSoup > (http://www.crummy.com/software/BeautifulSoup/), for instance. I agree with your sentiment exactly. If the regex he is trying to get is difficult enough that he has to ask; then, yes, he should be using a parser. > I will never understand why so many people try to parse/scrape > HTML/XML with regexes... Why? Because some times it is good enough to get the job done easily. From thebrasse at gmail.com Wed Jul 8 18:43:59 2009 From: thebrasse at gmail.com (=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=) Date: Wed, 8 Jul 2009 15:43:59 -0700 (PDT) Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: <48bcf21c-4c40-4d49-9966-e0cedfdf206e@d4g2000yqa.googlegroups.com> On Jul 6, 11:15?pm, Scott David Daniels wrote: > brasse wrote: > > I have been thinking about how write exception safe constructors in > > Python. By exception safe I mean a constructor that does not leak > > resources when an exception is raised within it. > > ... > ?> As you can see this is less than straight forward. Is there some kind > ?> of best practice that I'm not aware of? > > Not so tough. ?Something like this tweaked version of your example: > > class Foo(object): > ? ? ?def __init__(self, name, fail=False): > ? ? ? ? ?self.name = name > ? ? ? ? ?if not fail: > ? ? ? ? ? ? ?print '%s.__init__(%s)' % (type(self).__name__, name) > ? ? ? ? ?else: > ? ? ? ? ? ? ?print '%s.__init__(%s), FAIL' % (type(self).__name__, name) > ? ? ? ? ? ? ?raise ValueError('Asked to fail: %r' % fail) > > ? ? ?def close(self): > ? ? ? ? ?print '%s.close(%s)' % (type(self).__name__, self.name) > > class Bar(object): > ? ? ?def __init__(self): > ? ? ? ? ?unwind = [] > ? ? ? ? ?try: > ? ? ? ? ? ? ?self.a = Foo('a') > ? ? ? ? ? ? ?unwind.append(a) > ? ? ? ? ? ? ?self.b = Foo('b', fail=True) > ? ? ? ? ? ? ?unwind.append(b) > ? ? ? ? ? ? ?... > ? ? ? ? ?except Exception, why: > ? ? ? ? ? ? ?while unwind): > ? ? ? ? ? ? ? ? ?unwind.pop().close() > ? ? ? ? ? ? ?raise > > bar = Bar() > OK. That's another way. I have been thinking about this some more and right now I am experimenting with a decorator that could help me do these kinds of things. This is what I have right now: import functools class Foo(object): def __init__(self, name, fail=False): self.name = name self.closed = False if not fail: print '%s.__init__(%s)' % (self.__class__.__name__, self.name) else: print '%s.__init__(%s), FAIL' % (self.__class__.__name__, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) def safe(f): @functools.wraps(f) def wrapper(self, *args, **kwargs): variables = [] def recorder(self, name, value): if name != '__class__': variables.append(name) object.__setattr__(self, name, value) class Customized(object): pass setattr(Customized, '__setattr__', recorder) old_class = self.__class__ self.__class__ = Customized try: f(self, *args, **kwargs) self.__class__ = old_class except: # clean up objects created in __init__ (i.e. the call to f ()) for name in reversed(variables): o = getattr(self, name) if hasattr(o, 'close'): o.close() raise return wrapper class Bar(object): @safe def __init__(self): self.a = Foo('a') self.b = Foo('b') self.c = Foo('c', fail=True) bar = Bar() The safe decorator will record all attributes created on self. If an exception is raised during the execution of the decorated constructor it will call close on all the recorded objects. I realize that this decorator still needs a lot of work before it is usable, but I think it illustrates the concept. It might even be possible for the decorator to create a close/cleanup method dynamically. To have a decorator like this that actually worked would be great since it would remove the need to write error prone cleanup code. Any thoughts? :.:: mattias > --Scott David Daniels > Scott.Dani... at Acm.Org From fabiofz at gmail.com Wed Jul 8 19:01:40 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Wed, 8 Jul 2009 20:01:40 -0300 Subject: Pydev 1.4.7 Released Message-ID: Hi All, Pydev and Pydev Extensions 1.4.7 have been released Details on Pydev Extensions: http://www.fabioz.com/pydev Details on Pydev: http://pydev.sf.net Details on its development: http://pydev.blogspot.com Release Highlights in Pydev Extensions: ----------------------------------------------------------------- * The interactive console can be used on the remote debugger * Remote debugger properly redirects contents from the server on multiple runs. * Providing context-independent completions when other completions are not available (until now, it only gave those completions for method parameters) * The number of chars required to show the context-insensitive completion can now be customized. * Fixed problem with Eclipse 3.5: "Invalid Thread Access" when trying to rename a class Release Highlights in Pydev: ---------------------------------------------- * Iron Python support * Fixed issue when configuring interpreter on Eclipse 3.3 and 3.2 (was using API only available in 3.4) * Google App Engine o Popup menus for google app engine are now working with eclipse 3.2 o Fixed issues when google app engine project has spaces in path * Launching o Ctrl+F9 can be used to run as unit-test and select which tests will be run o F9 will now run the current editor based on the project type o Changed run icons o Run configurations can be created for the project o Run as unit-test can have --filter and --tests as a parameter set in the run configuration * Shift left can now shift even when there are less chars than the required indent string * Top-level modules on .egg files are now properly recognized * Auto-config fixed * Fixed problem when .pydevproject was not a parseable xml file (the pydev package explorer wouldn't work because of that) * When a new interpreter is created, it's properly selected in the tree * Code-completion better heuristic when analyzing function return that's called on self. * Code-completion in the interactive console now handles import sections correctly * Code formatter: Spaces after square and curly braces are no longer changed when an unary operator is found afterwards * Fixed problem when recognizing encodings (regexp was not correct) What is PyDev? --------------------------- PyDev is a plugin that enables users to use Eclipse for Python, Jython and Iron Python 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 Aptana http://aptana.com/python Pydev Extensions http://www.fabioz.com/pydev Pydev - Python Development Environment for Eclipse http://pydev.sf.net http://pydev.blogspot.com From helvinlui at gmail.com Wed Jul 8 19:10:37 2009 From: helvinlui at gmail.com (Helvin) Date: Wed, 8 Jul 2009 16:10:37 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> Message-ID: <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> On Jul 8, 9:23?pm, Phil Thompson wrote: > On Wed, 08 Jul 2009 11:11:51 +0200, "Diez B. Roggisch" > > wrote: > > > > > > > Helvin wrote: > > >> Hi experts! > > >> I'm developing a GUI for a software using PyQT, and need 3D > >> visualization. Should I use PyOpenGL or VTK? > >> I understand that the PyQt package comes with a opengl module. What > >> else would I need? I think I need to download opengl. but how? where? > >> I have VTK and pyVTK installed, but I don't know how to use it in my > >> code, as when I run it, an error says it can't find the vtk module. > > > VTK won't mix with Qt. And I don't think you need to download opengl - it > > either comes with your system (or gfx-drivers), or not. I'm not sure if > Qt > > actually wraps the OpenGL api itself, AFAIK it doesn't, so you need > > PyOpenGL I guess. > > VTK has explicit support for both Qt (ie. via C++) and PyQt. > > Phil Thanks for the fast replies! I will look into how to use VTK now. Where would I find VTK's explicit support for PyQt? Because I have installed VTK (using its installer) and pyVTK (using its setup.py file), but how do I actually use it in my code? According to: http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521455.html, I have tried 'import vtk', but python can't find the vtk module. Thanks a million! Helvin From psimon at sonic.net Wed Jul 8 19:18:02 2009 From: psimon at sonic.net (Paul Simon) Date: Wed, 8 Jul 2009 16:18:02 -0700 Subject: tkinter problem Message-ID: <4a55292f$0$95538$742ec2ed@news.sonic.net> I have the "tkinter" problem and need some assistance to straighten it out. >From the web page "http://wiki.python.org/moin/TkInter" I tested as in "step 1" and cannot import "_tkinter." I do not have that file on my computer, but do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. This python stuff is great, but the documentation frequently feels like it is just a bit out of my grasp. I realize that all of this is free but I understand the instructions on the web page to repair only to the point of confusion. I'm not an expert. How do I modify my python configuration? Is there a file that needs to be edited? Which setup.py file do I use? Make? or python setup.py build and python setup.py install? Thanks. I appreciate your help. Paul Simon From rhodri at wildebst.demon.co.uk Wed Jul 8 19:20:59 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 09 Jul 2009 00:20:59 +0100 Subject: regex help In-Reply-To: References: Message-ID: On Wed, 08 Jul 2009 23:06:22 +0100, David wrote: > Hi > > I have a few regexs I need to do, but im struggling to come up with a > nice way of doing them, and more than anything am here to learn some > tricks and some neat code rather than getting an answer - although > thats obviously what i would like to get to. > > Problem 1 - > > id="ref_678774_cp">(25.47%)
> > I want to extract 25.47 from here - so far I've tried - > > xPer = re.search(' \">(.*?)%', content) Supposing that str(xID.group(1)) == "678774", let's see how that string concatenation turns out: (.*?)% The obvious problems here are the spurious double quotes, the spurious (but harmless) escaping of a double quote, and the lack of (escaped) backslash and (escaped) open parenthesis. The latter you can always strip off later, but the first sink the match rather thoroughly. > > and > > xPer = re.search(' \">\((\d*)%\)
', content) With only two single quotes present, the biggest problem should be obvious. Unfortunately if you just fix the obvious in either of the two regular expressions, you're setting yourself up for a fall later on. As The Fine Manual says right at the top of the page on the re module (http://docs.python.org/library/re.html), you want to be using raw string literals when you're dealing with regular expressions, because you want the backslashes getting through without being interpreted specially by Python's own parser. As it happens you get away with it in this case, since neither '\d' nor '\(' have a special meaning to Python, so aren't changed, and '\"' is interpreted as '"', which happens to be the right thing anyway. > Problem 2 - > >   > > Open: > > 5.50 > >   > Mkt Cap: > > 6.92M > >   > P/E: > > 21.99 > > > > I want to extract the open, mkt cap and P/E values - but apart from > doing loads of indivdual REs which I think would look messy, I can't > think of a better and neater looking way. Any ideas? What you're trying to do is inherently messy. You might want to use something like BeautifulSoup to hide the mess, but never having had cause to use it myself I couldn't say for sure. -- Rhodri James *-* Wildebeest Herder to the Masses From clp2 at rebertia.com Wed Jul 8 19:22:14 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 8 Jul 2009 16:22:14 -0700 Subject: tkinter problem In-Reply-To: <4a55292f$0$95538$742ec2ed@news.sonic.net> References: <4a55292f$0$95538$742ec2ed@news.sonic.net> Message-ID: <50697b2c0907081622w11ed9275mcce5c0377cafd273@mail.gmail.com> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: > I have the "tkinter" problem and need some assistance to straighten it out. > >From the web page "http://wiki.python.org/moin/TkInter" I tested as in "step > 1" and cannot import "_tkinter." I do not have that file on my computer, but > do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the > directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. > This ?python stuff is great, but the documentation frequently > feels like it is just a bit out of my grasp. I realize that all of this is > free but I understand the instructions on the web page to repair only to the > point of confusion. I'm not an expert. How do I modify my python > configuration? Is there a file that needs to be edited? Which setup.py file > do I use? Make? or python setup.py build and python setup.py install? > Thanks. I appreciate your help. - How did you install Python? - What Linux distro are you using? Cheers, Chris -- http://blog.rebertia.com From robert.kern at gmail.com Wed Jul 8 19:29:26 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 08 Jul 2009 18:29:26 -0500 Subject: PyQt GUI In-Reply-To: <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> Message-ID: On 2009-07-08 18:10, Helvin wrote: > Thanks for the fast replies! I will look into how to use VTK now. > Where would I find VTK's explicit support for PyQt? Wrapping/Python/vtk/qt4/ in the VTK sources. > Because I have installed VTK (using its installer) and pyVTK (using > its setup.py file), but how do I actually use it in my code? According > to: http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521455.html, > I have tried 'import vtk', but python can't find the vtk module. Then you have not installed VTK's Python bindings correctly. Note that pyVTK is just a library for manipulating VTK files. The VTK Python bindings are part of VTK's distribution itself. Exactly how did you install VTK? Did you compile it yourself? -- 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 ldo at geek-central.gen.new_zealand Wed Jul 8 19:49:54 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 09 Jul 2009 11:49:54 +1200 Subject: walking a directory with very many files References: <5f51bc62-16fb-4e24-ac6c-1e5ea3ee596c@d38g2000prn.googlegroups.com> <234b19ac-7baf-4356-9fe5-37d00146d982@z9g2000yqi.googlegroups.com> <20090617091858.432f89ca@malediction> <20090617110705.7e7c423f@malediction> <%Zv_l.19493$y61.5958@news-server.bigpond.net.au> <0058d36a$0$9759$c3e8da3@news.astraweb.com> Message-ID: In message , Lie Ryan wrote: > Lawrence D'Oliveiro wrote: > >> ... certainly it is characteristic of GUIs to show you all 400,000 files >> in a directory, or at least try to do so, and either hang for half an >> hour or run out of memory and crash, rather than give you some >> intelligent way of prefiltering the file display up front. > > In many debugging cases, you don't even know what to filter ... So pick a random sample to start with. Do you know of a GUI tool that can do that? From mensanator at aol.com Wed Jul 8 20:03:42 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 8 Jul 2009 17:03:42 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: Message-ID: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> On Jul 7, 12:47?am, Mensanator wrote: > On Jul 7, 12:16 am, casevh wrote: > > > I discovered a serious bug with comparisons and have posted alpha2 > > which fixes that bug and adds Unicode support for Python 2.x > > > casevh > > Damn! I was just congatulating myself for pulling off > a hat trick (there had been no point in downloading > 3.x without gmpy so I have been putting it off): > > - installing Python 3.1 > - installing gmpy 1.10 > - converting my Collatz Function library to 3.1 syntax > > And it all worked smoothly, just had to add parentheses > to my print statements, change xrange to range and all > my / to // (the library is exclusively integer). I had > gmpy running in my library on 3.1 in about 10 minutes. > > So I'll have to re-do the gmpy install. Shouldn't be > any big deal. > > I started doing some real world tests. Generally, things > look good (nothing crashes, timing looks not bad) but > I'm getting some funny results on one of my tests, so > I'll report back when I have more information. As I said, I was getting funny results from one of my tests. It seemed to work ok, but timing varied from 2 to 88 seconds, which seemed odd. The other tests were generally consistent for a given environment (cpu speed, OS, Python version, gmpy version). At some point I watched the memory usage profile from Windows. On the same machine I have both Python 2.6 and 3.1 installed, with the appropriate gmpy 1.10 version loaded for each. In Python 2.6, it looks like this: memory usage profile Python 2.6 gmpy 1.1 Vista /--------------\ /-------\ .....RESTART SHELL / .\/ \ ____/ . \________ . . . . start of RUN start of RUN The first "start of RUN" is the first time the test is run (from IDLE). That change in usage represents about 700 MB (I'm testing really BIG numbers, up to 500 million digits). The memory remains allocated after the program terminates (the flat plateau). When I run a second time, we see the allocation dip, then climb back up to the plateau, so it appears that the allocation never climbs above 1.1 GB. Finally, doing a RESTART SHELL seems to completely free the allocated memory. I assume this is normal behaviour. With Python 3.1, it get this profile: memory usage profile Python 3.1 gmpy 1.1 Vista /- / | /----------------/ \------\ .....RESTART SHELL / . \ ____/ . \___________ . . . . start of RUN start of RUN Here, at the start of the second RUN, it appears that new memory is allocated BEFORE the previous is cleared. Is this a quirk in the way 3.1 behaves? Here, the peak usage climbs to 1.8 GB which I think causes VM thrashing accounting for the increased execution times. My guess is that gmpy is provoking, but not causing this behaviour. The actual test is: t0 = time.time() n=10 for k in range(1,n): for i in range(1,n-2): print((str(cf.gmpy.numdigits(cf.Type12MH(k,i))).zfill(n)),end=' ') print() print() t1 = time.time() The library function Type12MH is: def Type12MH(k,i): """Find ith, kth Generation Type [1,2] Mersenne Hailstone using the closed form equation Type12MH(k,i) k: generation i: member of generation returns Hailstone (a) """ ONE = gmpy.mpz(1) TWO = gmpy.mpz(2) SIX = gmpy.mpz(6) NIN = gmpy.mpz(9) if (k<1) or (i<1): return 0 i = gmpy.mpz(i) k = gmpy.mpz(k) # a = (i-1)*9**(k-1) + (9**(k-1) - 1)//2 + 1 # return 2**(6*a - 1) - 1 a = (i-ONE)*NIN**(k-ONE) + (NIN**(k-ONE) - ONE)//TWO + ONE return TWO**(SIX*a - ONE) - ONE ## Sample runs ## ## Test 1 - create numbers up to 500 million digits ## ## 0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ## 0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ## 0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ## 0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ## 0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ## 0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ## 0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ## 0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ## 0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ## 15.5460000038 ## >>> ================================ RESTART ================================ ## >>> ## 0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ## 0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ## 0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ## 0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ## 0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ## 0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ## 0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ## 0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ## 0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ## 3.06299996376 From psimon at sonic.net Wed Jul 8 20:16:15 2009 From: psimon at sonic.net (Paul Simon) Date: Wed, 8 Jul 2009 17:16:15 -0700 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> Message-ID: <4a5536d4$0$95520$742ec2ed@news.sonic.net> "Chris Rebert" wrote in message news:mailman.2863.1247095339.8015.python-list at python.org... On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: > I have the "tkinter" problem and need some assistance to straighten it > out. > >From the web page "http://wiki.python.org/moin/TkInter" I tested as in > >"step > 1" and cannot import "_tkinter." I do not have that file on my computer, > but > do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the > directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. > This python stuff is great, but the documentation frequently > feels like it is just a bit out of my grasp. I realize that all of this is > free but I understand the instructions on the web page to repair only to > the > point of confusion. I'm not an expert. How do I modify my python > configuration? Is there a file that needs to be edited? Which setup.py > file > do I use? Make? or python setup.py build and python setup.py install? > Thanks. I appreciate your help. - How did you install Python? - What Linux distro are you using? Cheers, Chris -- http://blog.rebertia.com I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure exactly how I installed Python. Originally I had installed 2.5 from RPM but 2.6 was not available for my distro (2008.1) in RPM. I downloaded something from python.org and installed. Not sure if it was tarball or zip file. Paul From casevh at gmail.com Wed Jul 8 20:38:06 2009 From: casevh at gmail.com (casevh) Date: Wed, 8 Jul 2009 17:38:06 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> Message-ID: <4ae1e16f-124b-49c8-8364-36c59419be9b@d32g2000yqh.googlegroups.com> On Jul 8, 5:03?pm, Mensanator wrote: > On Jul 7, 12:47?am, Mensanator wrote: > > > > > On Jul 7, 12:16 am, casevh wrote: > > > > I discovered a serious bug with comparisons and have posted alpha2 > > > which fixes that bug and adds Unicode support for Python 2.x > > > > casevh > > > Damn! I was just congatulating myself for pulling off > > a hat trick (there had been no point in downloading > > 3.x without gmpy so I have been putting it off): > > > - installing Python 3.1 > > - installing gmpy 1.10 > > - converting my Collatz Function library to 3.1 syntax > > > And it all worked smoothly, just had to add parentheses > > to my print statements, change xrange to range and all > > my / to // (the library is exclusively integer). I had > > gmpy running in my library on 3.1 in about 10 minutes. > > > So I'll have to re-do the gmpy install. Shouldn't be > > any big deal. > > > I started doing some real world tests. Generally, things > > look good (nothing crashes, timing looks not bad) but > > I'm getting some funny results on one of my tests, so > > I'll report back when I have more information. > > As I said, I was getting funny results from one of my tests. > > It seemed to work ok, but timing varied from 2 to 88 seconds, > which seemed odd. The other tests were generally consistent > for a given environment (cpu speed, OS, Python version, gmpy > version). > > At some point I watched the memory usage profile from Windows. > On the same machine I have both Python 2.6 and 3.1 installed, > with the appropriate gmpy 1.10 version loaded for each. > > In Python 2.6, it looks like this: > > memory usage profile Python 2.6 gmpy 1.1 Vista > > ? ? ? /--------------\ ?/-------\ .....RESTART SHELL > ? ? ?/ ? ? ? ? ? ? ? .\/ ? ? ? ? \ > ____/ ? ? ? ? ? ? ? ?. ? ? ? ? ? ?\________ > ? ?. ? ? ? ? ? ? ? ? . > ? ?. ? ? ? ? ? ? ? ? . > ? ?start of RUN ? ? ?start of RUN > > The first "start of RUN" is the first time the test is run > (from IDLE). That change in usage represents about 700 MB > (I'm testing really BIG numbers, up to 500 million digits). > > The memory remains allocated after the program terminates > (the flat plateau). When I run a second time, we see the > allocation dip, then climb back up to the plateau, so it > appears that the allocation never climbs above 1.1 GB. > > Finally, doing a RESTART SHELL seems to completely free > the allocated memory. I assume this is normal behaviour. > > With Python 3.1, it get this profile: > > memory usage profile Python 3.1 gmpy 1.1 Vista > > ? ? ? ? ? ? ? ? ? ? ? ? ?/- > ? ? ? ? ? ? ? ? ? ? ? ? / | > ? ? ? /----------------/ ? \------\ .....RESTART SHELL > ? ? ?/ ? ? ? ? ? ? ? ? . ? ? ? ? ? \ > ____/ ? ? ? ? ? ? ? ? ?. ? ? ? ? ? ?\___________ > ? ?. ? ? ? ? ? ? ? ? ? . > ? ?. ? ? ? ? ? ? ? ? ? . > ? ?start of RUN ? ? ? ?start of RUN > > Here, at the start of the second RUN, it appears that new > memory is allocated BEFORE the previous is cleared. Is this > a quirk in the way 3.1 behaves? Here, the peak usage climbs > to 1.8 GB which I think causes VM thrashing accounting for > the increased execution times. > Hmmm. It looks like memory is not being release properly. I don't see that behavior under Linux. The running time is a very consistent 1.35 seconds. I'm traveling at the moment so it will be at least a week before I can test under Windows. Thanks for the report. I'll try to see if I can figure out what is going on. casevh > My guess is that gmpy is provoking, but not causing this > behaviour. > > The actual test is: > > t0 = time.time() > n=10 > for k in range(1,n): > ? for i in range(1,n-2): > ? ? print((str(cf.gmpy.numdigits(cf.Type12MH(k,i))).zfill(n)),end=' ') > ? print() > print() > t1 = time.time() > > The library function Type12MH is: > > def Type12MH(k,i): > ? ? """Find ith, kth Generation Type [1,2] Mersenne Hailstone using > the closed form equation > > ? ? Type12MH(k,i) > ? ? k: generation > ? ? i: member of generation > ? ? returns Hailstone (a) > ? ? """ > ? ? ONE = gmpy.mpz(1) > ? ? TWO = gmpy.mpz(2) > ? ? SIX = gmpy.mpz(6) > ? ? NIN = gmpy.mpz(9) > > ? ? if (k<1) or (i<1): return 0 > > ? ? i = gmpy.mpz(i) > ? ? k = gmpy.mpz(k) > > ? ? # a = (i-1)*9**(k-1) + (9**(k-1) - 1)//2 + 1 > ? ? # return 2**(6*a - 1) - 1 > > ? ? a = (i-ONE)*NIN**(k-ONE) + (NIN**(k-ONE) - ONE)//TWO + ONE > ? ? return TWO**(SIX*a - ONE) - ONE > > ## ?Sample runs > ## > ## ?Test 1 - create numbers up to 500 million digits > ## > ## ?0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 > 0000000013 > ## ?0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 > 0000000107 > ## ?0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 > 0000000952 > ## ?0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 > 0000008560 > ## ?0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 > 0000077028 > ## ?0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 > 0000693246 > ## ?0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 > 0006239209 > ## ?0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 > 0056152869 > ## ?0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 > 0505375816 > ## > ## ?15.5460000038 > ## ?>>> ================================ RESTART > ================================ > ## ?>>> > ## ?0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 > 0000000013 > ## ?0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 > 0000000107 > ## ?0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 > 0000000952 > ## ?0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 > 0000008560 > ## ?0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 > 0000077028 > ## ?0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 > 0000693246 > ## ?0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 > 0006239209 > ## ?0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 > 0056152869 > ## ?0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 > 0505375816 > ## > ## ?3.06299996376 From no.email at please.post Wed Jul 8 21:20:10 2009 From: no.email at please.post (kj) Date: Thu, 9 Jul 2009 01:20:10 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Martin Vilcans writes: >On Fri, Jul 3, 2009 at 4:05 PM, kj wrote: >> I'm will be teaching a programming class to novices, and I've run >> into a clear conflict between two of the principles I'd like to >> teach: code clarity vs. code reuse. =A0I'd love your opinion about >> it. >In general, code clarity is more important than reusability. >Unfortunately, many novice programmers have the opposite impression. I >have seen too much convoluted code written by beginners who try to >make the code generic. Writing simple, clear, to-the-point code is >hard enough as it is, even when not aiming at making it reusable. >If in the future you see an opportunity to reuse the code, then and >only then is the time to make it generic. >YAGNI is a wonderful principle. Thanks! kynn From no.email at please.post Wed Jul 8 21:21:22 2009 From: no.email at please.post (kj) Date: Thu, 9 Jul 2009 01:21:22 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: Message-ID: In Scott David Daniels writes: >First, a quote which took me a bit to find: > Thomas William K??rner paraphrasing Polya and Svego > in A Companion to Analysis: > Recalling that 'once is a trick, twice is a method, > thrice is a theorem, and four times a theory,' we > seek to codify this insight. Good stuff. >Let us apply this insight: > Suppose in writing code, we pretty much go with that. >A method is something you notice, a theorem is a function, and >a theory is a generalized function. >Even though we like DRY ("don't repeat yourself") as a maxim, let >it go the first time and wait until you see the pattern (a possible >function). I'd go with a function first, a pair of functions, and >only then look to abstracting the function. Thanks! kynn From jcd at sdf.lonestar.org Wed Jul 8 22:10:47 2009 From: jcd at sdf.lonestar.org (J. Clifford Dyer) Date: Wed, 08 Jul 2009 22:10:47 -0400 Subject: count In-Reply-To: <7xbpnuzw4u.fsf@ruckus.brouhaha.com> References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: <1247105447.4532.8.camel@mctell> On Wed, 2009-07-08 at 14:45 -0700, Paul Rubin wrote: > aahz at pythoncraft.com (Aahz) writes: > > >Avoid that len(tuple(g)), use something like the following, it's lazy > > >and saves some memory. > > The question is whether it saves time, have you tested it? > > len(tuple(xrange(100000000))) ... hmm. timer.py -------- from datetime import datetime def tupler(n): return len(tuple(xrange(n))) def summer(n): return sum(1 for x in xrange(n)) def test_func(f, n): print f.__name__, start = datetime.now() print f(n) end = datetime.now() print "Start: %s" % start print "End: %s" % end print "Duration: %s" % (end - start,) if __name__ == '__main__': test_func(summer, 10000000) test_func(tupler, 10000000) test_func(summer, 100000000) test_func(tupler, 100000000) $ python timer.py summer 10000000 Start: 2009-07-08 22:02:13.216689 End: 2009-07-08 22:02:15.855931 Duration: 0:00:02.639242 tupler 10000000 Start: 2009-07-08 22:02:15.856122 End: 2009-07-08 22:02:16.743153 Duration: 0:00:00.887031 summer 100000000 Start: 2009-07-08 22:02:16.743863 End: 2009-07-08 22:02:49.372756 Duration: 0:00:32.628893 Killed $ Note that "Killed" did not come from anything I did. The tupler just bombed out when the tuple got too big for it to handle. Tupler was faster for as large an input as it could handle, as well as for small inputs (test not shown). From no.email at please.post Wed Jul 8 22:19:25 2009 From: no.email at please.post (kj) Date: Thu, 9 Jul 2009 02:19:25 +0000 (UTC) Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: In Tim Rowe writes: >2009/7/4 kj : >> Precisely. =A0As I've stated elsewhere, this is an internal helper >> function, to be called only a few times under very well-specified >> conditions. =A0The assert statements checks that these conditions >> are as intended. =A0I.e. they are checks against the module writer's >> programming errors. >Good for you. I'm convinced that you have used the assertion >appropriately, and the fact that so many here are unable to see that >looks to me like a good case for teaching the right use of assertions. >For what it's worth, I read assertions at the beginning of a procedure >as part of the specification of the procedure, and I use them there in >order to document the procedure. An assertion in that position is for >me a statement to the user of the procedure "it's your responsibility >to make sure that you never call this procedure in such a way as to >violate these conditions". They're part of a contract, as somebody >(maybe you) pointed out. >As somebody who works in the safety-critical domain, it's refreshing >to see somebody teaching students to think about the circumstances in >which a procedure can legitimately be called. The hostility you've >received to that idea is saddening, and indicative of why there's so >much buggy software out there. Thanks for the encouragement. When I teach programming, the students are scientists. For the stuff they do, correctness of the code trumps everything else. I teach them to view assertions as way of translating their assumptions into code. And by this I mean not only assumptions about the correctness of their code (the typical scope of assertions), but also, more broadly, assumptions about the data that they are dealing with (which often comes from external sources with abysmal quality control). My scientific code is jam-packed with assertions. I can't count the number of times that one such lowly assertion saved me from a silent but potentially disastrous bug. And yes I find it distressing that so so few programmers in my line of work use assertions at all. kynn From vel.accel at gmail.com Wed Jul 8 22:23:53 2009 From: vel.accel at gmail.com (dieter) Date: Wed, 8 Jul 2009 19:23:53 -0700 (PDT) Subject: PyGtk Depends on Numeric Message-ID: <1ebe9314-9434-459a-bd3e-2b2386a35f1b@n11g2000yqb.googlegroups.com> Get with the times people and port to numpy. :P Don't you think its about time? ~Pete From jpablo.romero at gmail.com Wed Jul 8 22:55:53 2009 From: jpablo.romero at gmail.com (=?ISO-8859-1?Q?Juan_Pablo_Romero_M=E9ndez?=) Date: Wed, 8 Jul 2009 21:55:53 -0500 Subject: PyQt GUI In-Reply-To: References: Message-ID: I use OpenInventor (Coin3d) which have a python binding called "pivy". It works great. http://pivy.coin3d.org/ Juan Pablo 2009/7/8 Helvin : > Hi experts! > > I'm developing a GUI for a software using PyQT, and need 3D > visualization. Should I use PyOpenGL or VTK? > I understand that the PyQt package comes with a opengl module. What > else would I need? I think I need to download opengl. but how? where? > I have VTK and pyVTK installed, but I don't know how to use it in my > code, as when I run it, an error says it can't find the vtk module. > > Help would be sooooo appreciated! > Helvin > -- > http://mail.python.org/mailman/listinfo/python-list > From fatkinson at mishmash.com Wed Jul 8 23:53:12 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Wed, 08 Jul 2009 20:53:12 -0700 Subject: IP Address Function References: Message-ID: On Wed, 08 Jul 2009 12:29:54 +0200, Piet van Oostrum wrote: >Something like: > >#! /usr/bin/env python > >import cgi >from os import getenv > >print "Content-type: text/html" >print > >ipaddr = (getenv("HTTP_CLIENT_IP") or > getenv("HTTP_X_FORWARDED_FOR") or > getenv("HTTP_X_FORWARDED_FOR") or > getenv("REMOTE_ADDR") or > "UNKNOWN") > >print ipaddr That did it. I wonder why they don't just have a function to return it instead of putting you through all of that? At any rate, it works. Regards, Fred From nobody at nowhere.com Thu Jul 9 00:38:32 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 09 Jul 2009 05:38:32 +0100 Subject: IP Address Function References: Message-ID: On Wed, 08 Jul 2009 20:53:12 -0700, Fred Atkinson wrote: >>ipaddr = (getenv("HTTP_CLIENT_IP") or >> getenv("HTTP_X_FORWARDED_FOR") or >> getenv("HTTP_X_FORWARDED_FOR") or >> getenv("REMOTE_ADDR") or >> "UNKNOWN") >> >>print ipaddr > > That did it. > > I wonder why they don't just have a function to return it instead of > putting you through all of that? There's no unambiguous definition of "client IP", so you have to choose which definition you want. REMOTE_ADDR is set to the actual IP address from which the connection originated (from getpeername()). If the connection was made via a proxy, REMOTE_ADDR will contain the IP address of the proxy. The others are set from HTTP headers. Proxies often add Client-IP or X-Forwarded-For headers to indicate the originating IP address. OTOH, there's no way to know if the headers are accurate; nuisance users may add these headers to confuse poorly-conceived banning mechanisms. From sajmikins at gmail.com Thu Jul 9 01:05:57 2009 From: sajmikins at gmail.com (Simon Forman) Date: Wed, 8 Jul 2009 22:05:57 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: (I wanted to reply to a few messages in one post so I quoted them all below. Let me know if this is bad etiquette.) On Jul 8, 8:23 am, kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5... at q11g2000yqi.googlegroups.com> Simon Forman writes: > > >Frankly, I'm of the impression that it's a mistake not to start > >teaching programming with /the bit/ and work your way up from there. > >I'm not kidding. I wrote a (draft) article about this: "Computer > >Curriculum"http://docs.google.com/View?id=dgwr777r_31g4572gp4 > >I really think the only good way to teach computers and programming is > >to start with a bit, and build up from there. "Ontology recapitulates > >phylogeny" > > I happen to be very receptive to this point of view. I had the > benefit of that sort of training (one of the first computer courses > I took started, believe it or not, with Turing machines, through > coding in machine language, and compiler theory, and all the way > up to dabbling with Unix!), and I suspect that the reason it is > sometimes difficult for me to explain even relatively simple-looking > things to others is that I have this background that I unconsciously, > and incorrectly, take for granted in others... There is this Yes! Once the concepts become so familiar that you call them "intuitive" it seems to be very difficult to remember what they were like before. Something like "a = b" becomes "obvious" only after you've internalized the preceding concepts. > persistent idea "out there" that programming is a very accessible > skill, like cooking or gardening, anyone can do it, and even profit > from it, monetarily or otherwise, etc., and to some extent I am Programming is not like any other human activity. I've been reading some of Prof. Dijkstra's EWDs in the last few days. In one [1] he says, "automatic computers embody not only one radical novelty but two of them", to wit: First, the huge scales that must be understood, "from a bit to a few hundred megabytes, from a microsecond to a half an hour of computing"; and second, "that the automatic computer is our first large-scale digital device" which our until-now overwhelmingly analog experience does not prepare us to deal with well. He talks about how "when all is said and done, the only thing computers can do for us is to manipulate symbols and produce results of such manipulations" and he emphasises the "uninterpreted" nature of mechanical symbol manipulation, i.e. that the machine is doing it mindlessly. Dijkstra[1]: "It is true that the student that has never manipulated uninterpreted formulae quickly realizes that he is confronted with something totally unlike anything he has ever seen before. But fortunately, the rules of manipulation are in this case so few and simple that very soon thereafter he makes the exciting discovery that he is beginning to master the use of a tool that, in all its simplicity, gives him a power that far surpasses his wildest dreams." [1] > actively contributing to this perception by teaching this course > to non-programmers (experimental biologists to be more precise), Experimental biologists? Well that's probably harmless. Mostly harmless. > but maybe this idea is not entirely true... Maybe, to get past > the most amateurish level, one has to, one way or another, come > face-to-face with bits, compilers, algorithms, and all the rest > that real computer scientists learn about in their formal training... > > kj If you're never exposed to that constellation of concepts that underpins "mechanical symbol manipulation" you are adrift in a sea ("c", ha ha) of abstractions. However, if you /are/ exposed to the "so few and simple" rules of manipulation the gates (no pun intended) to the kingdom are thrown wide. On Jul 8, 9:10 am, Steven D'Aprano wrote: > On Wed, 08 Jul 2009 12:23:50 +0000, kj wrote: > > I happen to be very receptive to this point of view. > [...] > > There is this persistent idea "out there" that > > programming is a very accessible skill, like cooking or gardening, > > anyone can do it, and even profit from it, monetarily or otherwise, > > etc., and to some extent I am actively contributing to this perception > > by teaching this course to non-programmers (experimental biologists to > > be more precise), but maybe this idea is not entirely true... > > There is some evidence that 30-60% of people simply cannot learn to > program, no matter how you teach them: > > http://www.codinghorror.com/blog/archives/000635.html > http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ Thank you! That's exactly the paper that prompted me to write the article I mentioned. (Now I don't have to go find the link myself. Win!) I don't buy it: I believe strongly that any normal person can learn to program, to manipulate symbols to create formulae that guide the machine in its uninterpreted symbol manipulation. I find it significant that in the paper [2] they say, "Formal logical proofs, and therefore programs ? formal logical proofs that particular computations are possible, expressed in a formal system called a programming language ? are utterly meaningless. To write a computer program you have to come to terms with this, to accept that whatever you might want the program to mean, the machine will blindly follow its meaningless rules and come to some meaningless conclusion. In the test the consistent group showed a pre-acceptance of this fact: they are capable of seeing mathematical calculation problems in terms of rules, and can follow those rules wheresoever they may lead. The inconsistent group, on the other hand, looks for meaning where it is not." In other words the people who don't understand computers, don't understand computers. I think that "first hump" people can become "second hump" people but that it requires teaching them the foundations first, not confronting them with such incredible novelties as "a = b" and saying in effect, "here you go buddy, sink or swim." Quoting Dijkstra again [1]: "Before we part, I would like to invite you to consider the following way of doing justice to computing's radical novelty in an introductory programming course. "On the one hand, we teach what looks like the predicate calculus, but we do it very differently from the philosophers. In order to train the novice programmer in the manipulation of uninterpreted formulae, we teach it more as boolean algebra, familiarizing the student with all algebraic properties of the logical connectives. To further sever the links to intuition, we rename the values {true, false} of the boolean domain as {black, white}. "On the other hand, we teach a simple, clean, imperative programming language, with a skip and a multiple assignment as basic statements, with a block structure for local variables, the semicolon as operator for statement composition, a nice alternative construct, a nice repetition and, if so desired, a procedure call. To this we add a minimum of data types, say booleans, integers, characters and strings. The essential thing is that, for whatever we introduce, the corresponding semantics is defined by the proof rules that go with it." Imagine my surprise: he's saying (with immensely greater brilliance and eloquence) much what I said in my little article. The major difference from what he's outlined is that I think the students should implement the imperative programming language themselves in Forth, but the gist is the same. > I'm sympathetic to the idea, but not entirely convinced. Perhaps the > problem isn't with the students, but with the teachers, and the > languages: > > http://www.csse.monash.edu.au/~damian/papers/PDF/SevenDeadlySins.pdf > > (My money is that it's a little of both.) Hmm, that paper contains some good insights IMO, but I think they're still missing the big picture, so to speak. Really I suspect it's a case of "Programming languages considered harmful." The core abstractions of [mechanical] computation are just not that complicated. You can teach them to anybody in about a half an hour, drunk. I have. After that, if they're interested, there is a smooth easy path to "higher" abstractions: parsing, compiling, tree traversal and transformation. (It is said that possession is 9/10s of the law, in the same vein I would claim parsing is 9/10s of computer programming.) I am beginning to suspect that concrete static (in the sense of "standard" language specifications) languages are part of the problem. Everyone gets so caught up in programming via languages that you get, well, people trying to teach "Computer Programming" as if it were only necessary to grok a language, rather than grokking /symbol manipulation/ itself. (Did you read that last paragraph and think, "Well how the heck else are you supposed to program a computer if not in a computer language?"? If so, well, that is kind of my point.) > > Maybe, to > > get past the most amateurish level, one has to, one way or another, come > > face-to-face with bits, compilers, algorithms, and all the rest that > > real computer scientists learn about in their formal training... > > The "No True Scotsman" fallacy. > > There's nothing amateurish about building software applications that > work, with well-designed interfaces and a minimum of bugs, even if you've > never heard of Turing Machines. > > -- > Steven I beg to differ. I recall a conversation with a co-worker who had "learned" to program using PHP. Another co-worker and I were trying to convince him that there was a good reason to differentiate between hash tables and arrays. He didn't even know that they were different "things". I remember telling him, "between the metal and the desktop there is nothing but layers of abstraction. We use different names precisely because of different behaviours." He made "well-designed interfaces", but "amateurish" is about the nicest thing I would have called him. As for "a minimum of bugs"... sigh. The "minimum of bugs" is zero, if you derive your "uninterpreted formulae" /correctly/. Deriving provably correct "programs" should be what computer science and computer education are all about (not "java vocational training" as Alan Kay once decried.) Again with Dijkstra[3]: "The prime paradigma of the pragmatic designer is known as "poor man's induction", i.e. he believes in his design as long as "it works", i.e. until faced with evidence to the contrary. (He will then "fix the design".) The scientific designer, however, believes in his design because he understands why it will work under all circumstances. The transition from pragmatic to scientific design would indeed be a drastic change within the computer industry." "Obviously no errors" is the goal to strive for, and I am comfortable calling anyone an amateur who prefers "no obvious errors." (Actually that's a little harsh on the amateurs, "ama" meaning love, "amateur" is one who does something for love of it.) On Jul 8, 9:27 am, kj wrote: > In <5f0a2722-45eb-468c-b6b2-b7bb80ae5... at q11g2000yqi.googlegroups.com> Simon Forman writes: > > >I'm not kidding. I wrote a (draft) article about this: "Computer > >Curriculum"http://docs.google.com/View?id=dgwr777r_31g4572gp4 > > Very cool. > > kj Hey, thank you! On Jul 8, 9:47 am, Paul Moore wrote: > 2009/7/8 kj : > > > There is this > > persistent idea "out there" that programming is a very accessible > > that real computer scientists learn about in their formal training... > > Look at it another way. Experimental biologists don't want to program, > they want to use computers to do experimental biology. It's a tool, > and they (quite reasonably) don't *care* about robustness, > portability, etc. Or even about programming, to be honest. I'd say it's just the opposite: to "use computers to do experimental biology" they want to instruct that machine to manipulate their (meaningful to them but meaningless to it) symbols in useful ways. This is nothing more or less than programming. The fact that they need to learn all sorts of details of a programming language to do that is NOT because they can't grok programming. It's because computer scientists have put too many layers of abstraction on top of the "pure" symbol manipulation and then forgotten what they have done. I have a very nice book "Introduction to Programming and Problem Solving with Pascal" that I picked up for $0.50 at a used bookstore not long ago. It says, right on page 201, in the chapter on "Running, Debugging, and Testing Programs": "One of the nice features of programming in a high-level language like Pascal is that it can be done with almost a total lack of understanding of what a computer is and how it actually operates. [...] There is no reason why someone who wants to write a computer program should have to understand the electronic circuitry of a computer, any more than someone learning to drive a car should have to understand how the internal combustion engine works." I think that's exactly wrong. What you're doing with computers doesn't change from the bit to the high-level language. It's all symbol manipulation according to the same set of rules, all the way up. The elaboration becomes involved as you go up but the process does not change qualitatively. > In the context of the original question, it's entirely reasonable (in > my view) to tell this audience "if the code does something weird you > don't understand, either ignore it and find another way or dig into > the manuals and experiment if you care". They'd very quickly find a = > a + b as a less confusing alternative to a += b. (As has been pointed > out earlier, to some extent a += b is quite an advanced construct - > after all, it's essentially an optimisation of a = a + b). On that point I completely agree. The language is getting in the way of the programming. > Biologists don't expect me to understand their discipline before I can > plant seeds in my garden, after all. (And when I do plant seeds, I > usually get far more surprising results than I could get from a += b > :-)) > > Paul. The discipline of programming is different than biology. It's incredibly simple yet profound if it's taught as what it is, i.e. automatic symbol manipulation. No scientist is a stranger to logic and reasoned argument. They shouldn't be strangers to telling their mechanical brains what to "reason" about. Learning to program should be /easy/ for someone who basically already gets it. Wow, long post. (Oh, and, er, it's ONTOGENY not Ontology that recapitulates phylogeny. Heh. My bad.) [1] "On the cruelty of really teaching computing science" http://www.cs.utexas.edu/~EWD/transcriptions/EWD10xx/EWD1036.html [2] "The camel has two humps" http://www.cs.mdx.ac.uk/research/PhDArea/saeed/paper1.pdf [3] "Can computing science save the computer industry?" http://www.cs.utexas.edu/users/EWD/transcriptions/EWD09xx/EWD920.html From gagsl-py2 at yahoo.com.ar Thu Jul 9 02:14:18 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 03:14:18 -0300 Subject: Python Error from Apress book References: <24364269.post@talk.nabble.com> <4A5345B1.8050306@ieee.org> <4A54687A.6010004@ieee.org> Message-ID: En Wed, 08 Jul 2009 06:35:54 -0300, Dave Angel escribi?: > Gabriel Genellina wrote: >> Jul 2009 09:55:13 -0300, Dave Angel escribi?: >>> Gabriel Genellina wrote: >>>> En Mon, 06 Jul 2009 19:56:40 -0300, matt0177 >>>> escribi?: >> >>>>> When I try to run the command as outlined in >>>>> the book "simple_markup2.py < test_input.txt > test_output.html i >>>>> get the >>>>> following error every time. >>>>> >>>>> IOError: [Errno 9] Bad file descriptor >> >> I think the error depends on the specific OS version/service pack. But >> at least on XP this appears to fix it: >> >> http://support.microsoft.com/kb/321788/en-us >> > Thanks for the link. Looking at that one, it indicates that Windows > 2000 fixed it in SP4, and XP fixed it in Sp1. But I'm already running > XP SP3, so I wonder if it's something new. I'm using XP SP3 too. Before applying the registry fix, stdout and stderr were working fine, but not stdin. After making the registry change, stdin works too. I didn't notice the issue with stdin until now (I don't use stdin very often) -- Gabriel Genellina From helvinlui at gmail.com Thu Jul 9 02:27:53 2009 From: helvinlui at gmail.com (Helvin) Date: Wed, 8 Jul 2009 23:27:53 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> Message-ID: <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> On Jul 9, 11:29?am, Robert Kern wrote: > On 2009-07-08 18:10, Helvin wrote: > > > Thanks for the fast replies! I will look into how to use VTK now. > > Where would I find VTK's explicit support for PyQt? > > Wrapping/Python/vtk/qt4/ in the VTK sources. > > > Because I have installed VTK (using its installer) and pyVTK (using > > its setup.py file), but how do I actually use it in my code? According > > to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., > > I have tried 'import vtk', but python can't find the vtk module. > > Then you have not installed VTK's Python bindings correctly. Note that pyVTK is > just a library for manipulating VTK files. The VTK Python bindings are part of > VTK's distribution itself. Exactly how did you install VTK? Did you compile it > yourself? > > -- > 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 You mean, when I download VTK, the VTK Python bindings come with it? I downloaded VTK from:http://vtk.org/files/release/5.4/ I downloaded pyVTK from: http://pypi.python.org/pypi/PyVTK/0.4.67 And then installed it by the command 'python setup.py install' according to: http://cens.ioc.ee/projects/pyvtk/ I understand that this command runs the file called 'setup.py' that came with the pyVTK download. Thanks so much for your help! I am very desperate now... Helvin From greg at cosc.canterbury.ac.nz Thu Jul 9 02:29:26 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Thu, 09 Jul 2009 18:29:26 +1200 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <7blh0eF23r79rU1@mid.individual.net> Dave Angel wrote: > By the time I graduated, I had five six-level languages ^^^ Are they languages that you have to edit using vi? :-) -- Greg From morten.aasnes at gmail.com Thu Jul 9 02:32:56 2009 From: morten.aasnes at gmail.com (Morten Nygaard Aasnes) Date: 9 Jul 2009 06:32:56 GMT Subject: Pydev 1.4.7 Released References: Message-ID: <7blh8oF2367ioU2@mid.individual.net> On 2009-07-08, Fabio Zadrozny wrote: > Hi All, > > Pydev and Pydev Extensions 1.4.7 have been released > > Details on Pydev Extensions: http://www.fabioz.com/pydev > Details on Pydev: http://pydev.sf.net > Details on its development: http://pydev.blogspot.com Nice! Will this work in Eclipse 3.5 as well? -- Morten Nygaard ?snes http://twitter.com/mortenaa http://identi.ca/mortenaa From stefan_ml at behnel.de Thu Jul 9 02:42:06 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 09 Jul 2009 08:42:06 +0200 Subject: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values In-Reply-To: <7xd48bdn73.fsf@ruckus.brouhaha.com> References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <7xd48bdn73.fsf@ruckus.brouhaha.com> Message-ID: <4a55913f$0$31874$9b4e6d93@newsspool3.arcor-online.net> Paul Rubin wrote: > Daniel Austria writes: >> just one question. How can i remove all 0 values in a list? > > I prefer: > > newlist = list(x for x in oldlist if x != 0) > > to the square bracket list comprehension that a few people have > suggested. This is because in python 2.x, the listcomp "leaks" its > index variable into the surrounding scope, but the generator > expression above doesn't. As you indicated, that's been fixed in Py3, though. So if your code works in Py3.x, you can be somewhat sure that the leak doesn't have side effects. Plus, it's pretty easy to ignore those leaks as long as you use a suitable name for the loop variable. Also note that the performance characteristics may not be identical in both cases, depending on where you run your code. Cython, for example, will write a list comprehension out as a rather straight C loop, but when we implement generator expressions in Cython, it may have to get turned into a generator function instead of a loop, so that you'd get a much larger overhead than for the plain list comprehension (although in the simple case above it would likely get optimised away). CPython shows a similar difference: $ python3.1 -m timeit '[x for x in range(1000) if x]' 10000 loops, best of 3: 170 usec per loop $ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \ '[x for x in r if x]' 1000 loops, best of 3: 222 usec per loop $ python3.1 -m timeit 'list(x for x in range(1000) if x)' 1000 loops, best of 3: 227 usec per loop $ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \ 'list(x for x in r if x)' 1000 loops, best of 3: 280 usec per loop Not that I'd consider those numbers worth bothering... Stefan From Lacrima.Maxim at gmail.com Thu Jul 9 02:50:37 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Wed, 8 Jul 2009 23:50:37 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> Message-ID: <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> On Jul 8, 10:54?pm, Piet van Oostrum wrote: > >>>>> Lacrima (L) wrote: > >L> Hello! > >L> I have just started using Emacs to write python scripts. > >L> I installed python-mode.el > >L> Then I just tried this code: > >L> print 'hello world' > >L> When I press C-c RET, new blank window is opened and emacs says: > >L> (Shell command succeeded with no output) > >L> So where is my 'hello world'? > >L> When I do C-c C-c, it prints 'hello world' successfully. > >L> Why in the first case I get no output? > > Can you check in the buffer where you have the python code what command > C-c RET is bound to? > With C-h k C-c RET > > Shell command succeeded with no output suggests that is has a different > binding than the standard one in python-mode. > > Did you happen to name your file 'test' or 'test.py? > > C-c RET does an import and 'import test' imports a standard module test. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Hi, Piet! Thanks for your reply! My file name is 'trains.py'. When I do C-h k C-c RET, it shows me help from manual: "C-c RET runs the command py-execute-import-or-reload which is an interactive Lisp function in `python-mode'..." and so on. And still when I do C-c RET, I receive "(Shell command succeeded with no output)" Any more help would be really appreciated, because I am newbie with Emacs. With regards, Max From reddy.mrp at gmail.com Thu Jul 9 03:17:52 2009 From: reddy.mrp at gmail.com (m.reddy prasad reddy) Date: Thu, 9 Jul 2009 12:47:52 +0530 Subject: can i write a assemly language programs in python Message-ID: can any one tell me how to write assembly language programs in python...if no is there any other way to write the programs in python Reddi prasad reddy ph.no:09958083797 -------------- next part -------------- An HTML attachment was scrubbed... URL: From futurebase at gmx.at Thu Jul 9 03:22:43 2009 From: futurebase at gmx.at (Daniel Austria) Date: Thu, 9 Jul 2009 00:22:43 -0700 (PDT) Subject: ... remove all 0 values References: <29df6e77-0d5b-4da1-8ded-89c5840b9680@c36g2000yqn.googlegroups.com> <7xd48bdn73.fsf@ruckus.brouhaha.com> <4a55913f$0$31874$9b4e6d93@newsspool3.arcor-online.net> Message-ID: <4dcaab15-2d2d-4d7c-bd93-5e7f931cb369@j32g2000yqh.googlegroups.com> Thanks a lot for your advices, i decided to use the filter() method to sort out the 0. i can ?t use the sum() function cause i need the list afterwards .... best, Dan From reddy.mrp at gmail.com Thu Jul 9 03:25:17 2009 From: reddy.mrp at gmail.com (m.reddy prasad reddy) Date: Thu, 9 Jul 2009 12:55:17 +0530 Subject: i need immediate help from u Message-ID: i m new this community .; can any one send me the solution for writing the assembly language programs in python my mail ID:redd.mrp at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Jul 9 03:29:04 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Jul 2009 09:29:04 +0200 Subject: regex help References: Message-ID: David wrote: >   > > Open: > > 5.50 > >   > Mkt Cap: > > 6.92M > >   > P/E: > > 21.99 > > > > I want to extract the open, mkt cap and P/E values - but apart from > doing loads of indivdual REs which I think would look messy, I can't > think of a better and neater looking way. Any ideas? >>> from BeautifulSoup import BeautifulSoup >>> bs = BeautifulSoup("""  ... ... Open: ... ... 5.50 ... ...   ... Mkt Cap: ... ... 6.92M ... ...   ... P/E: ... ... 21.99 ... ... """) >>> for key in bs.findAll(attrs={"class": "key"}): ... value = key.findNext(attrs={"class": "val"}) ... print key.string.strip(), "-->", value.string.strip() ... Open: --> 5.50 Mkt Cap: --> 6.92M P/E: --> 21.99 From clp2 at rebertia.com Thu Jul 9 03:29:33 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 00:29:33 -0700 Subject: i need immediate help from u In-Reply-To: References: Message-ID: <50697b2c0907090029n66002f7cy68a5c51082c2b232@mail.gmail.com> On Thu, Jul 9, 2009 at 12:25 AM, m.reddy prasad reddy wrote: > i m new this community .; > can any one send me the solution for writing the assembly language programs > in python > my mail ID:redd.mrp at gmail.com Please read http://catb.org/esr/faqs/smart-questions.html Cheers, Chris -- http://blog.rebertia.com From CHausberger at gmx.de Thu Jul 9 03:41:20 2009 From: CHausberger at gmx.de (Claus Hausberger) Date: Thu, 09 Jul 2009 09:41:20 +0200 Subject: Problem reading file with umlauts In-Reply-To: <4A53B2B4.9090409@mrabarnett.plus.com> References: <20090707135949.100950@gmx.net> <4A53643C.4020905@xs4all.nl> <20090707190046.152740@gmx.net> <4A53B2B4.9090409@mrabarnett.plus.com> Message-ID: <20090709074120.198030@gmx.net> Thanks a lot. I will try that on the weekend. Claus > Claus Hausberger wrote: > > Thanks a lot. Now I am one step further but I get another strange error: > > > > Traceback (most recent call last): > > File "./read.py", line 12, in > > of.write(text) > > UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in > position 0: ordinal not in range(128) > > > > according to google ufeff has something to do with byte order. > > > > I use an Linux system, maybe this helps to find the error. > > > 'text' contains Unicode, but you're writing it to a file that's not > opened for Unicode. Either open the output file for Unicode: > > of = codecs.open("umlaut-out.txt", "w", encoding="latin1") > > or encode the text before writing: > > text = text.encode("latin1") > > (I'm assuming you want the output file to be in Latin1.) > > > > >> Claus Hausberger wrote: > >> > >>> I have a text file with is encoding in Latin1 (ISO-8859-1). I can't > >>> change that as I do not create those files myself. I have to read > >>> those files and convert the umlauts like ? to stuff like &oumol; as > >>> the text files should become html files. > >> umlaut-in.txt: > >> ---- > >> This file is contains data in the unicode > >> character set and is encoded with utf-8. > >> Viele R?hre. Macht spa?! Ts?sch! > >> > >> > >> umlaut-in.txt hexdump: > >> ---- > >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is > con > >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in > th > >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0A 63 68 61 72 61 e > unicode..chara > >> 000030: 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 20 cter set and > is > >> 000040: 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 66 encoded with > utf > >> 000050: 2D 38 2E 0D 0A 56 69 65 6C 65 20 52 C3 B6 68 72 -8...Viele > R..hr > >> 000060: 65 2E 20 4D 61 63 68 74 20 73 70 61 C3 9F 21 20 e. Macht > spa..! > >> 000070: 20 54 73 C3 BC 73 63 68 21 0D 0A 00 00 00 00 00 > Ts..sch!....... > >> > >> > >> umlaut.py: > >> ---- > >> # -*- coding: utf-8 -*- > >> import codecs > >> text=codecs.open("umlaut-in.txt",encoding="utf-8").read() > >> text=text.replace(u"?",u"oe") > >> text=text.replace(u"?",u"ss") > >> text=text.replace(u"?",u"ue") > >> of=open("umlaut-out.txt","w") > >> of.write(text) > >> of.close() > >> > >> > >> umlaut-out.txt: > >> ---- > >> This file is contains data in the unicode > >> character set and is encoded with utf-8. > >> Viele Roehre. Macht spass! Tsuesch! > >> > >> > >> umlaut-out.txt hexdump: > >> ---- > >> 000000: 54 68 69 73 20 66 69 6C 65 20 69 73 20 63 6F 6E This file is > con > >> 000010: 74 61 69 6E 73 20 64 61 74 61 20 69 6E 20 74 68 tains data in > th > >> 000020: 65 20 75 6E 69 63 6F 64 65 0D 0D 0A 63 68 61 72 e > unicode...char > >> 000030: 61 63 74 65 72 20 73 65 74 20 61 6E 64 20 69 73 acter set and > is > >> 000040: 20 65 6E 63 6F 64 65 64 20 77 69 74 68 20 75 74 encoded with > ut > >> 000050: 66 2D 38 2E 0D 0D 0A 56 69 65 6C 65 20 52 6F 65 f-8....Viele > Roe > >> 000060: 68 72 65 2E 20 4D 61 63 68 74 20 73 70 61 73 73 hre. Macht > spass > >> 000070: 21 20 20 54 73 75 65 73 63 68 21 0D 0D 0A 00 00 ! > Tsuesch!..... > >> > >> > >> > >> > >> > >> -- > >> "The ability of the OSS process to collect and harness > >> the collective IQ of thousands of individuals across > >> the Internet is simply amazing." - Vinod Valloppillil > >> http://www.catb.org/~esr/halloween/halloween4.html > > > > -- > http://mail.python.org/mailman/listinfo/python-list -- Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate f?r nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02 From rajat.dudeja at gmail.com Thu Jul 9 03:55:37 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Thu, 9 Jul 2009 00:55:37 -0700 (PDT) Subject: Check file is locked? References: Message-ID: On Jul 8, 12:45?pm, Tim Golden wrote: > Rajat wrote: > > On Jul 8, 4:57 am, Lawrence D'Oliveiro > central.gen.new_zealand> wrote: > >> In message , Christian > > >> Heimes wrote: > >>> By the way most operating systems don't lock a file when it's opened for > >>> reading or writing or even executed. > >> The general conclusion seems to be that mandatory locking is more trouble > >> than it's worth. > > > My OS is a windows XP sp3. All I'm trying to achieve is to close an > > application ( which could be a notepad, a wordpad or some other text > > editor) that have opened my file. Once the file is closed I can easily > > delete that file. > > > I guess, if file is opened by one of that application, the file must > > be locked and so is the reason I cannot delete the file. > > I assume that your real requirement is: I can't open/close/delete > this file; it must be locked by something else; what is that > something else? > > The simplest thing is probably to run sysinternals' handle util: > > ?http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx > > and use the subprocess module to parse the output. > That will give you the pid, and you can then use, eg, psutil: > > ?http://code.google.com/p/psutil/ > > to get the details of the process. > > TJG- Hide quoted text - > > - Show quoted text - I've used the Handle.exe and got the following results: ------------------------------------------------------------------------------ notepad.exe pid: 3540 COMP\rajatd C: File (RW-) C:\Documents and Settings\rajatd\Desktop 10: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 44: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 7C: Section \BaseNamedObjects\ShimSharedMemory ------------------------------------------------------------------------------ wordpad.exe pid: 2212 COMP\rajatd 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 74: Section \BaseNamedObjects\ShimSharedMemory F8: Section \BaseNamedObjects \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 170: Section \BaseNamedObjects\RotHintTable 184: File (RW-) C:\Documents and Settings\rajatd\My Documents I've also parsed this output for the PIDS. But no where in the result I got to know what file has been associated with a PID. Does for this I need to use pustil? From gagsl-py2 at yahoo.com.ar Thu Jul 9 03:57:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 04:57:15 -0300 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: En Wed, 08 Jul 2009 23:19:25 -0300, kj escribi?: > In Tim Rowe > writes: > >> 2009/7/4 kj : > >>> Precisely. =A0As I've stated elsewhere, this is an internal helper >>> function, to be called only a few times under very well-specified >>> conditions. =A0The assert statements checks that these conditions >>> are as intended. =A0I.e. they are checks against the module writer's >>> programming errors. > >> Good for you. I'm convinced that you have used the assertion >> appropriately, and the fact that so many here are unable to see that >> looks to me like a good case for teaching the right use of assertions. >> For what it's worth, I read assertions at the beginning of a procedure >> as part of the specification of the procedure, and I use them there in >> order to document the procedure. An assertion in that position is for >> me a statement to the user of the procedure "it's your responsibility >> to make sure that you never call this procedure in such a way as to >> violate these conditions". They're part of a contract, as somebody >> (maybe you) pointed out. > >> As somebody who works in the safety-critical domain, it's refreshing >> to see somebody teaching students to think about the circumstances in >> which a procedure can legitimately be called. The hostility you've >> received to that idea is saddening, and indicative of why there's so >> much buggy software out there. > > Thanks for the encouragement. > > When I teach programming, the students are scientists. For the > stuff they do, correctness of the code trumps everything else. I > teach them to view assertions as way of translating their assumptions > into code. And by this I mean not only assumptions about the > correctness of their code (the typical scope of assertions), but > also, more broadly, assumptions about the data that they are dealing > with (which often comes from external sources with abysmal quality > control). Nobody says you shouldn't check your data. Only that "assert" is not the right way to do that. Ok, it's easier to write: assert x>0 and y>0 and x0 and y>0 and x My scientific code is jam-packed with assertions. I can't count > the number of times that one such lowly assertion saved me from a > silent but potentially disastrous bug. And yes I find it distressing > that so so few programmers in my line of work use assertions at > all. I'm just saying that some of those assertions probably should be real "if ... raise" statements instead - not that you remove the checks. You may use this short function, if you prefer: def fassert(condition, message='', exc_type=AssertionError): if not condition: raise exc_type(message) -- Gabriel Genellina From __peter__ at web.de Thu Jul 9 03:59:54 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Jul 2009 09:59:54 +0200 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > "Chris Rebert" wrote in message > news:mailman.2863.1247095339.8015.python-list at python.org... > On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >> I have the "tkinter" problem and need some assistance to straighten it >> out. >> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >> >"step >> 1" and cannot import "_tkinter." I do not have that file on my computer, >> but >> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >> This python stuff is great, but the documentation frequently >> feels like it is just a bit out of my grasp. I realize that all of this >> is free but I understand the instructions on the web page to repair only >> to the >> point of confusion. I'm not an expert. How do I modify my python >> configuration? Is there a file that needs to be edited? Which setup.py >> file >> do I use? Make? or python setup.py build and python setup.py install? >> Thanks. I appreciate your help. > > - How did you install Python? > - What Linux distro are you using? > > Cheers, > Chris > http://blog.rebertia.com > I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure > exactly how I installed Python. Originally I had installed 2.5 from RPM > but 2.6 was not available for my distro (2008.1) in RPM. I downloaded > something from python.org and installed. Not sure if it was tarball or > zip file. Zip or tar doesn't matter, you are installing "from source". Python has to find the necessary include files for tcl/tk. These are in separate packages that you have to install before you invoke Python's configure script. I don't know what they are called on your system -- look for tk-dev.rpm, tcl-dev.rpm or similar. You may run into the same problem with other modules like readline. Peter From mukherjee.tanmoy at gmail.com Thu Jul 9 04:18:30 2009 From: mukherjee.tanmoy at gmail.com (Tanmoy) Date: Thu, 9 Jul 2009 13:48:30 +0530 Subject: Hello Message-ID: Hi ... I have been trying to set this 2-D array of this sort. 0 10 20 ........... 1000 1 11 21............... 1000 Here is the code i tried ... arr=[] for i in range(0,1010,10): arr.append([]) for j in range(0,1001,1): arr[i].append(i+j) print arr I am getting the following error list index out of range Please let me know where am i getting wrong. Sincerely tank -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Thu Jul 9 04:19:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 05:19:37 -0300 Subject: Problem with list of dicts and copying References: <5700df6b0907081009p5ec4167cqec36e915e0f6c4@mail.gmail.com> Message-ID: En Wed, 08 Jul 2009 14:09:43 -0300, Cameron Pulsford escribi?: > I'm representing the board as a dictionary, where the keys are > (x, y) positions, and the values are candidates. So my program goes along > picking numbers from the list of candidates and then propagating the > constraints. [...]Basically my boards list should be copies of good > boards, and when I need to > refresh the current board, I want to pull off a copy of the correct one. > [...] this isn't happening. I am always modifying the same board. You have a dictionary whose values are lists, ok? The copy() method doesn't make a copy of those lists - only of the dictionary itself. Try using the deepcopy function in the copy module: http://docs.python.org/library/copy.html -- Gabriel Genellina From mail at timgolden.me.uk Thu Jul 9 04:21:17 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 09 Jul 2009 09:21:17 +0100 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A55A87D.6060802@timgolden.me.uk> Rajat wrote: > I've used the Handle.exe and got the following results: > > ------------------------------------------------------------------------------ > notepad.exe pid: 3540 COMP\rajatd > C: File (RW-) C:\Documents and Settings\rajatd\Desktop > 10: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 44: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 7C: Section \BaseNamedObjects\ShimSharedMemory > > ------------------------------------------------------------------------------ > wordpad.exe pid: 2212 COMP\rajatd > 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 74: Section \BaseNamedObjects\ShimSharedMemory > F8: Section \BaseNamedObjects > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > 170: Section \BaseNamedObjects\RotHintTable > 184: File (RW-) C:\Documents and Settings\rajatd\My Documents > > I've also parsed this output for the PIDS. But no where in the result > I got to know what file has been associated with a PID. > > Does for this I need to use pustil? Well unless I'm missing your point considerably, the output tells you all you need to know: notepad.exe (pid 3540) has some kind of handle open on the desktop, the common controls DLL and an area of shared memory. As has been pointed out elsewhere, notepad doesn't hold the file open which it's editing: it opens it, reads the contents, and closes it again. For demonstration purposes: import os, sys import subprocess f = open (sys.executable) subprocess.call (["handle", sys.executable]) f.close () TJG From gagsl-py2 at yahoo.com.ar Thu Jul 9 04:23:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 05:23:51 -0300 Subject: windows command-line References: <376017.28322.qm@web30601.mail.mud.yahoo.com> Message-ID: En Wed, 08 Jul 2009 14:23:54 -0300, Emile van Sebille escribi?: > On 7/8/2009 10:07 AM Lucas Junqueira said... >> Hi, I'd like to run a simple windows command-line program from within >> my python script and agt all the returt it generates. Is this possible? >> How can I do it? > > Depending on python version, look into subprocess, commands or os.pipe > and related. commands is Unix only, and os.pipe by itself doesn't solve the problem - so we are left with subprocess, and that would be my reccomendation too. -- Gabriel Genellina From dickinsm at gmail.com Thu Jul 9 04:29:02 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Thu, 9 Jul 2009 01:29:02 -0700 (PDT) Subject: python3 fail to start References: <788ec223-9894-4168-b1f8-758cd51a47ad@r16g2000vbn.googlegroups.com> Message-ID: <06650e06-5073-4420-972c-1add7c9bbbd6@h8g2000yqm.googlegroups.com> On Jun 30, 3:43?pm, ts wrote: > i just install the python 3.1 dmg onto my mac. when i run python3, it > fail with : > > Fatal Python error: Py_Initialize: can't initialize sys standard > streamsLookupError: unknown encoding: > Abort trap > > couldnt understand the problem. anyone can help? Any chance you could give us some feedback about your system? The Python devs are working to fix the problem: http://bugs.python.org/issue6393 but it would be really helpful to know: - what version of OS X you're using - what output you get when you type 'locale' at a Terminal prompt - what the International settings are in your Terminal preferences (Terminal preferences -> Settings -> Advanced tab): what's the setting for the character encoding, and do you have the 'Set LANG environment variable at startup' checkbox checked? I managed to reproduce the crash by starting python3 (again at a Terminal prompt) with: LANG=UTF-8 python3 So I suspect that your locale settings are part of the problem. As a temporary workaround, something like LANG=en_US.UTF-8 python3 might work. (Substitute whatever language setting is appropriate for you in place of en_US.) Thanks! Mark From python at mrabarnett.plus.com Thu Jul 9 04:41:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 Jul 2009 09:41:42 +0100 Subject: Hello In-Reply-To: References: Message-ID: <4A55AD46.8010407@mrabarnett.plus.com> Tanmoy wrote: T Hi ... > I have been trying to set this 2-D array of this sort. > > 0 10 20 ........... 1000 > 1 11 21............... > > 1000 > > > Here is the code i tried ... > > arr=[] > for i in range(0,1010,10): > arr.append([]) > for j in range(0,1001,1): > arr[i].append(i+j) > > print arr > > I am getting the following error > list index out of range > > Please let me know where am i getting wrong. > The value of 'i' goes 0, 10, ..., etc. When i==0 you append an empty list to arr, so arr[i] is arr[0]. No problem. When i==10 you append another empty list to arr, so arr[i] is arr[10]. Index error because there's no arr[10], only arr[0] and arr[1]. From gagsl-py2 at yahoo.com.ar Thu Jul 9 05:03:21 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 06:03:21 -0300 Subject: can i write a assemly language programs in python References: Message-ID: En Thu, 09 Jul 2009 04:17:52 -0300, m.reddy prasad reddy escribi?: > can any one tell me how to write assembly language programs in > python...if > no is there any other way to write the programs in python You write Python programs using Python, not assembly. Perhaps if you provide more info on what you want to do, someone can suggest a different way... -- Gabriel Genellina From andy2.0 at gmail.com Thu Jul 9 05:26:10 2009 From: andy2.0 at gmail.com (Andy Clegg) Date: Thu, 9 Jul 2009 02:26:10 -0700 (PDT) Subject: subprocess + python-daemon - bug/problem? Message-ID: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> Hi all, I'm trying to daemonize a python program, and occasionally have it run subprocesses, however I'm running into a nasty error, as follows: "File "/users/rsg/ancl/devcocast/devcocast-svn/scripts/DaemonSpawnTes t.py", line 5, in subprocess.Popen('echo 1').wait() File "/usr/lib64/python2.5/subprocess.py", line 594, in __init__ errread, errwrite) File "/usr/lib64/python2.5/subprocess.py", line 1089, in _execute_ch ild os.waitpid(self.pid, 0) OSError: [Errno 10] No child processes" The minimal testcase showing this problem is as follows: "import daemon import subprocess daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() subprocess.Popen('echo 1').wait()" So there is no threading going on (I've found some bugs relating to subprocess and threading). I'm using Fedora 10, Python 2.5.2, and python-daemon 1.4.6 from here http://pypi.python.org/pypi/python-daemon/ . If anyone can shed some light on the situation, I'd be extremely grateful! Yours, Andy From user at example.net Thu Jul 9 05:36:13 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 11:36:13 +0200 Subject: older pythons Message-ID: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> hi everybody. i have a certain set of old python scripts and data used by said scripts which were written using python 2.3 in particular i used some features which were later modified or deprecated by the language newer versions (e.g.: cmp(), print as keyword, and such...) for reasons i find hard to explain briefly, it is unfeasible at the moment to modify scripts and data to adapt to the changes now: i ask if it is possible to have different versions of python installed on my system (linux debian) so that i can continue to use python 2.3 (as "python" from the shell) but i can also use - say - python 3.1 (maybe as "python3.1" from the shell) if yes: is there a known recommended way to do so? thanks a lot bye From andy2.0 at gmail.com Thu Jul 9 05:42:54 2009 From: andy2.0 at gmail.com (Andy Clegg) Date: Thu, 9 Jul 2009 02:42:54 -0700 (PDT) Subject: subprocess + python-daemon - bug/problem? References: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> Message-ID: <828acd8d-a9bd-4506-b695-e537af4587d8@l31g2000yqb.googlegroups.com> My apologies, the python code should have been: "import daemon import subprocess daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() subprocess.Popen(['echo','1']).wait()" However the error remains the same. Yours, Andy On Jul 9, 10:26?am, Andy Clegg wrote: > Hi all, > > I'm trying to daemonize a python program, and occasionally have it run > subprocesses, however I'm running into a nasty error, as follows: > > "File "/users/rsg/ancl/devcocast/devcocast-svn/scripts/DaemonSpawnTes > t.py", line 5, in > ? ? subprocess.Popen('echo 1').wait() > ? File "/usr/lib64/python2.5/subprocess.py", line 594, in __init__ > ? ? errread, errwrite) > ? File "/usr/lib64/python2.5/subprocess.py", line 1089, in _execute_ch > ild > ? ? os.waitpid(self.pid, 0) > OSError: [Errno 10] No child processes" > > The minimal testcase showing this problem is as follows: > > "import daemon > import subprocess > > daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() > subprocess.Popen('echo 1').wait()" > > So there is no threading going on (I've found some bugs relating to > subprocess and threading). I'm using Fedora 10, Python 2.5.2, and > python-daemon 1.4.6 from herehttp://pypi.python.org/pypi/python-daemon/ > . > > If anyone can shed some light on the situation, I'd be extremely > grateful! > > Yours, > > Andy From adrian.dziubek at gmail.com Thu Jul 9 05:53:40 2009 From: adrian.dziubek at gmail.com (Adrian Dziubek) Date: Thu, 9 Jul 2009 02:53:40 -0700 (PDT) Subject: older pythons References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> Message-ID: The recommended Debian way is update-alternatives. I find it a bit unintuitive, so I have to read through the documentation every time I use it, but it should be able link a chosen version of python to /usr/ bin/python. I don't know if it's set up by default, I have only one version installed. -- Adrian From rajat.dudeja at gmail.com Thu Jul 9 06:03:00 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Thu, 9 Jul 2009 03:03:00 -0700 (PDT) Subject: Check file is locked? References: Message-ID: On Jul 9, 1:21?pm, Tim Golden wrote: > Rajat wrote: > > I've used the Handle.exe and got the following results: > > > ---------------------------------------------------------------------------?--- > > notepad.exe pid: 3540 COMP\rajatd > > ? ? C: File ?(RW-) ? C:\Documents and Settings\rajatd\Desktop > > ? ?10: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?44: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?7C: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > > ---------------------------------------------------------------------------?--- > > wordpad.exe pid: 2212 COMP\rajatd > > ? ?1C: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?40: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?74: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > ? ?F8: Section ? ? ? \BaseNamedObjects > > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > > ? 170: Section ? ? ? \BaseNamedObjects\RotHintTable > > ? 184: File ?(RW-) ? C:\Documents and Settings\rajatd\My Documents > > > I've also parsed this output for the PIDS. But no where in the result > > I got to know what file has been associated with a PID. > > > Does for this I need to use pustil? > > Well unless I'm missing your point considerably, the output tells > you all you need to know: notepad.exe (pid 3540) has some kind > of handle open on the desktop, the common controls DLL and an > area of shared memory. As has been pointed out elsewhere, notepad > doesn't hold the file open which it's editing: it opens it, reads > the contents, and closes it again. > > For demonstration purposes: > > > import os, sys > import subprocess > > f = open (sys.executable) > subprocess.call (["handle", sys.executable]) > f.close () > > > > TJG- Hide quoted text - > > - Show quoted text - The Notepad process information is fine here. However, with wordpad the results are not much differentiating: ------------------------------------------------------------------------------ wordpad.exe pid: 2832 COMP\rajatd 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 74: Section \BaseNamedObjects\ShimSharedMemory F8: Section \BaseNamedObjects \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 170: Section \BaseNamedObjects\RotHintTable 184: File (RW-) C:\Documents and Settings\rajatd\My Documents ------------------------------------------------------------------------------ wordpad.exe pid: 844 COMP\rajatd 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 74: Section \BaseNamedObjects\ShimSharedMemory F8: Section \BaseNamedObjects \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 170: Section \BaseNamedObjects\RotHintTable 184: File (RW-) C:\Documents and Settings\rajatd\My Documents Both the wordpad applications opened 2 totally different files kept at different locations on the system. So, on the basis of above results one can not say out of these 2 wordpad apps which is the right one that could be closed. The only different thing among the two is the PIDs. From luislupeXXX at gmailXXX.com Thu Jul 9 06:06:16 2009 From: luislupeXXX at gmailXXX.com (Luis P. Mendes) Date: 09 Jul 2009 10:06:16 GMT Subject: Psyco 64 bits Message-ID: <4a55c118$0$48237$14726298@news.sunsite.dk> Hi, I used Psyco to speed up my Python code. Due to the great amount of data I have to proccess, I moved my Linux system to a 64 bits version with more RAM. It seems that Psyco cannot be used in such platforms. Or is there another version of Psyco for 64 bits platform? I googled and arrived to PyPy. But I don't know how to use it. With psyco, I used to include two statements roughly at the beginning of the script: import psyco psyco.full() With PyPy, is there a similar way to try to speed up my script? Luis From tkermode at gmail.com Thu Jul 9 06:13:43 2009 From: tkermode at gmail.com (Tom Kermode) Date: Thu, 9 Jul 2009 11:13:43 +0100 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: I wonder if the OP is trying to find out whether python programmes can be compiled and run as stand alone executables. (I know assembly and machine code are not the same, but it might be what they're after.) On windows you can use http://www.py2exe.org/ to bundle python programs into stand alone executables. They're not compiled, but your code and all the modules it relies on are brought together, meaning you can deliver a single exe than can be run like any other windows program. Maybe that helps... 2009/7/9 Gabriel Genellina : > En Thu, 09 Jul 2009 04:17:52 -0300, m.reddy prasad reddy > escribi?: > >> can any one tell me how to write assembly language programs in python...if >> no is there any other way to write the programs in python > > You write Python programs using Python, not assembly. > > Perhaps if you provide more info on what you want to do, someone can suggest > a different way... > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.kiloday.com http://www.fourstopspast.com From mail at timgolden.me.uk Thu Jul 9 06:21:51 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 09 Jul 2009 11:21:51 +0100 Subject: Check file is locked? In-Reply-To: References: Message-ID: <4A55C4BF.6080200@timgolden.me.uk> Rajat wrote: > The Notepad process information is fine here. However, with wordpad > the results are not much differentiating: > > ------------------------------------------------------------------------------ > wordpad.exe pid: 2832 COMP\rajatd > 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 74: Section \BaseNamedObjects\ShimSharedMemory > F8: Section \BaseNamedObjects > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > 170: Section \BaseNamedObjects\RotHintTable > 184: File (RW-) C:\Documents and Settings\rajatd\My Documents > ------------------------------------------------------------------------------ > wordpad.exe pid: 844 COMP\rajatd > 1C: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 40: File (RW-) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > 74: Section \BaseNamedObjects\ShimSharedMemory > F8: Section \BaseNamedObjects > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > 170: Section \BaseNamedObjects\RotHintTable > 184: File (RW-) C:\Documents and Settings\rajatd\My Documents > > Both the wordpad applications opened 2 totally different files kept at > different locations on the system. > > So, on the basis of above results one can not say out of these 2 > wordpad apps which is the right one that could be closed. The only > different thing among the two is the PIDs. Rajat: are you trying to find out which app is holding a file open? If so -- run handle.exe *for that filename*, as my code does: handle.exe This will only show which apps are holding that file. I suspect you're running handle.exe on its own which will show everything which is holding handles on anything. 1) Use wordpad.exe to open c:\temp\blah.txt 2) Run "handle.exe c:\temp\blah.txt" 3) Observe (at least on my Win XP Sp3 machine) that *no* process has a handle open on c:\temp\blah.txt, including wordpad, which presumably opens the file, reads it, and closes it again. The items you're seeing above are system-level handles which wordpad is holding for reasons of its own, but none of them is a user filename. TJG From mukherjee.tanmoy at gmail.com Thu Jul 9 06:31:56 2009 From: mukherjee.tanmoy at gmail.com (Tanmoy) Date: Thu, 9 Jul 2009 16:01:56 +0530 Subject: Python-list Digest, Vol 70, Issue 123 In-Reply-To: References: Message-ID: Hello , When i==0 you append an empty list to arr, so arr[i] is arr[0]. No problem. When i==10 you append another empty list to arr, so arr[i] is arr[10]. Index error because there's no arr[10], only arr[0] and arr[1]. Thanks for your prompt reply... How can i pass this problem Sincerely Tanmoy -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Jul 9 06:39:34 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 Jul 2009 11:39:34 +0100 Subject: Python-list Digest, Vol 70, Issue 123 In-Reply-To: References: Message-ID: <4A55C8E6.3040803@mrabarnett.plus.com> Tanmoy wrote: > Hello , > When i==0 you append an empty list to arr, so arr[i] is arr[0]. No > problem. > > When i==10 you append another empty list to arr, so arr[i] is arr[10]. > Index error because there's no arr[10], only arr[0] and arr[1]. > > > Thanks for your prompt reply... > > How can i pass this problem > You could try appending to arr[-1] instead. Better still: ... row = [] for j in range(0,1001,1): row.append(i+j) arr.append(row) ... From piet at cs.uu.nl Thu Jul 9 06:43:43 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 09 Jul 2009 12:43:43 +0200 Subject: IP Address Function References: Message-ID: >>>>> Fred Atkinson (FA) wrote: >FA> On Wed, 08 Jul 2009 12:29:54 +0200, Piet van Oostrum >FA> wrote: >>> Something like: >>> >>> #! /usr/bin/env python >>> >>> import cgi >>> from os import getenv >>> >>> print "Content-type: text/html" >>> print >>> >>> ipaddr = (getenv("HTTP_CLIENT_IP") or >>> getenv("HTTP_X_FORWARDED_FOR") or >>> getenv("HTTP_X_FORWARDED_FOR") or >>> getenv("REMOTE_ADDR") or >>> "UNKNOWN") >>> >>> print ipaddr >FA> That did it. >FA> I wonder why they don't just have a function to return it instead of >FA> putting you through all of that? I see now that I had getenv("HTTP_X_FORWARDED_FOR") or duplicated. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From user at example.net Thu Jul 9 06:49:59 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 12:49:59 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> Message-ID: <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Adrian Dziubek wrote: > The recommended Debian way is update-alternatives. I find it a bit > unintuitive, so I have to read through the documentation every time I > use it, but it should be able link a chosen version of python to /usr/ > bin/python. I don't know if it's set up by default, I have only one > version installed. > -- > Adrian what i was asking for is about a way to *INSTALL* and mantain different python versions, a task i think is not unusal for developers. thanks anyway for reply bye From reddy.mrp at gmail.com Thu Jul 9 06:52:44 2009 From: reddy.mrp at gmail.com (m.reddy prasad reddy) Date: Thu, 9 Jul 2009 16:22:44 +0530 Subject: need to write a assembly progrm Message-ID: my aim is to run the assembly programs in python.by that we can use that in the any labs.because we can run the python in the mobiles also.if we write assembly programs in the mobile ,the mobile act as a tool kit for the lab.tell me any other solutions for this -------------- next part -------------- An HTML attachment was scrubbed... URL: From rajat.dudeja at gmail.com Thu Jul 9 06:53:29 2009 From: rajat.dudeja at gmail.com (Rajat) Date: Thu, 9 Jul 2009 03:53:29 -0700 (PDT) Subject: Check file is locked? References: Message-ID: <652cca82-44a3-473f-b640-c2336a9cf929@v15g2000prn.googlegroups.com> On Jul 9, 3:21?pm, Tim Golden wrote: > Rajat wrote: > > The Notepad process information is fine here. However, with wordpad > > the results are not much differentiating: > > > ---------------------------------------------------------------------------?--- > > wordpad.exe pid: 2832 COMP\rajatd > > ? ?1C: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?40: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?74: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > ? ?F8: Section ? ? ? \BaseNamedObjects > > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > > ? 170: Section ? ? ? \BaseNamedObjects\RotHintTable > > ? 184: File ?(RW-) ? C:\Documents and Settings\rajatd\My Documents > > ---------------------------------------------------------------------------?--- > > wordpad.exe pid: 844 COMP\rajatd > > ? ?1C: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?40: File ?(RW-) ? C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common- > > Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83 > > ? ?74: Section ? ? ? \BaseNamedObjects\ShimSharedMemory > > ? ?F8: Section ? ? ? \BaseNamedObjects > > \CiceroSharedMemDefaultS-1-5-21-57989841-1580818891-839522115-1653 > > ? 170: Section ? ? ? \BaseNamedObjects\RotHintTable > > ? 184: File ?(RW-) ? C:\Documents and Settings\rajatd\My Documents > > > Both the wordpad applications opened 2 totally different files kept at > > different locations on the system. > > > So, on the basis of above results one can not say out of these 2 > > wordpad apps which is the right one that could be closed. The only > > different thing among the two is the PIDs. > > Rajat: are you trying to find out which app is holding a file open? > > If so -- run handle.exe *for that filename*, as my code does: > > handle.exe > > This will only show which apps are holding that file. I suspect > you're running handle.exe on its own which will show everything > which is holding handles on anything. > > 1) Use wordpad.exe to open c:\temp\blah.txt > > 2) Run "handle.exe c:\temp\blah.txt" > > 3) Observe (at least on my Win XP Sp3 machine) that *no* process > has a handle open on c:\temp\blah.txt, including wordpad, which > presumably opens the file, reads it, and closes it again. > > The items you're seeing above are system-level handles which > wordpad is holding for reasons of its own, but none of them > is a user filename. > > TJG- Hide quoted text - > > - Show quoted text - Thanks Tim for the details. Just further on this, my whole idea is to close the wordpad / notepad application so that I can delete the file and the directory where this file resides. With notepad it is no more a problem. But I'm concerned about the wordpad now. From manu3d at gmail.com Thu Jul 9 06:59:45 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Thu, 9 Jul 2009 03:59:45 -0700 (PDT) Subject: property using a classmethod Message-ID: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Greetings, today I did something like this: class MyClass(object): @classmethod def myClassMethod(self): print "ham" myProperty = property(myClassMethod, None, None) As many of you know this doesn't work and returns a TypeError: the object passed to the property is not a callable function but a classmethod object, which isn't callable at all. So, how do I do this? Ultimately all I want is a non-callable class-level attribute MyClass.myProperty that gives the result of MyClass.myClassMethod(). Can it be done? Manu From bearophileHUGS at lycos.com Thu Jul 9 07:04:10 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 9 Jul 2009 04:04:10 -0700 (PDT) Subject: count References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin: > print x, sum(1 for _ in g) Don't use that, use my function :-) If g has a __len__ you are wasting time. And sum(1 ...) is (on my PC) slower. J. Clifford Dyer: > if __name__ == '__main__': > ? ? test_func(summer, 10000000) > ? ? test_func(tupler, 10000000) > ? ? test_func(summer, 100000000) > ? ? test_func(tupler, 100000000) Have you forgotten my function? Bye, bearophile From larrykavanagh at examiner.ie Thu Jul 9 07:05:46 2009 From: larrykavanagh at examiner.ie (Larry kavanagh) Date: Thu, 09 Jul 2009 12:05:46 +0100 Subject: python-ldap Message-ID: <4A55CF0A.50707@examiner.ie> Hi, I need a PYTHON-LDAP package. I'm trying to install ploneldap and it tells me I need python-ldap package first .. but I can't find one to match my versions. I'm using plone 3.2.2, Python 2.4.4 and Zope 2.10.7 on a Win32 environment. Preferable I'd like an EXE as not too familiar with whole plone configuration. The ones I've tried have all told me I need Python 2.4 installed and it can't find in the registry. My plone site tells me I'm python 2.4.4 so maybe I just need to configure some environment variables or PATHs or something prior to running an install ?? Can anyone help me Thanks Larry Please consider the environment before printing this email. Examiner Publications (Cork) Ltd Directors: G. A. Crosbie (Chairman), Thomas J. Murphy (Chief Executive), A.W. Dinan (Secretary), T.P. Crosbie. Registered In Dublin, Ireland. Registered number: 73385. Registered Office: City Quarter, Lapps Quay, Cork. From bearophileHUGS at lycos.com Thu Jul 9 07:10:53 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 9 Jul 2009 04:10:53 -0700 (PDT) Subject: Psyco 64 bits References: <4a55c118$0$48237$14726298@news.sunsite.dk> Message-ID: <51ae217e-729d-4eac-957e-b01f054e4057@t13g2000yqt.googlegroups.com> Luis P. Mendes: > It seems that Psyco cannot be used in such platforms. > Or is there another version of Psyco for 64 bits platform? > I googled and arrived to PyPy. ?But I don't know how to use it. Psyco will be updated, but probably not for 64 bit CPUs. At the moment PyPy doesn't give you much speed up. So you can try IronPython, but I don't think it will help. There's also the Boo language, that's often fast enough. If your code has few hot spots, then there are many solutions that can be used. For example Cython, Weave, ShedSkin. Or writing external code in C/C++/D/Fortran and interfacing with it with a plethora of means, like ctypes, swig, PIL, Pyd, Boost.Python, Inline, and many other. Running speed is a big problem in many Python programs, so they have invented an army of possible solutions. Bye, bearophile From lie.1296 at gmail.com Thu Jul 9 07:15:45 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 11:15:45 GMT Subject: older pythons In-Reply-To: <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > Adrian Dziubek wrote: >> The recommended Debian way is update-alternatives. I find it a bit >> unintuitive, so I have to read through the documentation every time I >> use it, but it should be able link a chosen version of python to /usr/ >> bin/python. I don't know if it's set up by default, I have only one >> version installed. >> -- >> Adrian > > what i was asking for is about a way to *INSTALL* and mantain different > python versions, a task i think is not unusal for developers. > > thanks anyway for reply > > bye Installing several minor versions (e.g. 2.3, 2.4, 2.5, 3.0) in parallel has always been supported. Just install them and it should work "automagically". However changing the system's default /usr/bin/python is NOT recommended, especially in linux systems that rely heavily on python for many of the system tools. If you install from source, you need to check for some settings to ensure the installer does not install the /usr/bin/python's symlink. If you install from a package manager, they usually doesn't change the symlink and you'll need to run a separate program to change the symlinks (e.g. python-updater in Gentoo, which will also updates all installed python packages to the appropriate version. Neat.). The easiest way to make a specific script use a specific version of python is by changing its hashbang (#!) line: instead of: #!/usr/bin/env python use #!/usr/bin/env python2.3 or whatever version it should run on. AFAIK, no major linux distributions have officially ported to python 3.x. This means switching /usr/bin/python to python3.1 will definitely break your system. As an alternative, you can always explicitly specify the version number of python you want to use: $ python2.5 Python 2.5.4 (r254:67916, Jul 5 2009, 04:12:16) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit() $ python2.6 Python 2.6.2 (r262:71600, Jul 5 2009, 04:08:11) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit() $ python3.0 Python 3.0.1 (r301:69556, Jul 5 2009, 04:03:20) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit() $ python3.0 need3.py happily running with python 3.0 $ python2 need3.py SillyError: not running with python 3.0 From piet at cs.uu.nl Thu Jul 9 07:31:44 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 09 Jul 2009 13:31:44 +0200 Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Lacrima (L) wrote: >L> Thanks for your reply! >L> My file name is 'trains.py'. >L> When I do C-h k C-c RET, it shows me help from manual: "C-c RET runs >L> the command py-execute-import-or-reload >L> which is an interactive Lisp function in `python-mode'..." and so >L> on. >L> And still when I do C-c RET, I receive "(Shell command succeeded with >L> no output)" >L> Any more help would be really appreciated, because I am newbie with >L> Emacs. I tested it and I can get this message also. It happens when you have no Python shell running (*Python* buffer). The python code is then executed by calling the python shell command and catching the output. This output is displayed in the *Python Output* buffer. It happens also with C-c C-c and it will give the same message. There is a difference, however between C-c C-x and C-c RET: The latter will import your module, and most modules are written such that importing them doesn't print anything. If you add something like print "test" and the end of file, then "test" will appear in the *Python Output* buffer. If your file has the print "hello world" statement it should display in the *Python Output* buffer. I checked that. On the other hand, if you have a python shell running you must make sure (e.g. with the cd command) that its working directory is the directory where your python file is located, otherwise the import statement will not find your file. Unless it is somewhere in the python path. Also the *Python* buffer will not automatically pop up if it is not visible, in contrast with the *Python Output* buffer. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From fabiofz at gmail.com Thu Jul 9 07:35:32 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Thu, 9 Jul 2009 08:35:32 -0300 Subject: Pydev 1.4.7 Released In-Reply-To: <7blh8oF2367ioU2@mid.individual.net> References: <7blh8oF2367ioU2@mid.individual.net> Message-ID: Yes. On Thu, Jul 9, 2009 at 3:32 AM, Morten Nygaard Aasnes wrote: > On 2009-07-08, Fabio Zadrozny wrote: >> Hi All, >> >> Pydev and Pydev Extensions 1.4.7 have been released >> >> Details on Pydev Extensions: http://www.fabioz.com/pydev >> Details on Pydev: http://pydev.sf.net >> Details on its development: http://pydev.blogspot.com > > Nice! Will this work in Eclipse 3.5 as well? > > -- > Morten Nygaard ?snes ? http://twitter.com/mortenaa ? http://identi.ca/mortenaa > -- > http://mail.python.org/mailman/listinfo/python-list > From user at example.net Thu Jul 9 07:39:37 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 13:39:37 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Lie Ryan wrote: > AFAIK, no major linux distributions have officially ported to python > 3.x. http://packages.debian.org/experimental/python3.1 thanks for help From helvinlui at gmail.com Thu Jul 9 07:42:37 2009 From: helvinlui at gmail.com (Helvin) Date: Thu, 9 Jul 2009 04:42:37 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> Message-ID: <7a641820-7e7e-44a8-aae5-9cf9822aab6d@p36g2000prn.googlegroups.com> On Jul 9, 6:27?pm, Helvin wrote: > On Jul 9, 11:29?am, Robert Kern wrote: > > > > > > > On 2009-07-08 18:10, Helvin wrote: > > > > Thanks for the fast replies! I will look into how to use VTK now. > > > Where would I find VTK's explicit support for PyQt? > > > Wrapping/Python/vtk/qt4/ in the VTK sources. > > > > Because I have installed VTK (using its installer) and pyVTK (using > > > its setup.py file), but how do I actually use it in my code? According > > > to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., > > > I have tried 'import vtk', but python can't find the vtk module. > > > Then you have not installed VTK's Python bindings correctly. Note that pyVTK is > > just a library for manipulating VTK files. The VTK Python bindings are part of > > VTK's distribution itself. Exactly how did you install VTK? Did you compile it > > yourself? > > > -- > > 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 > > You mean, when I download VTK, the VTK Python bindings come with it? > I downloaded VTK from:http://vtk.org/files/release/5.4/ > > I downloaded pyVTK from:http://pypi.python.org/pypi/PyVTK/0.4.67 > And then installed it by the command 'python setup.py install' > according to:http://cens.ioc.ee/projects/pyvtk/ > I understand that this command runs the file called 'setup.py' that > came with the pyVTK download. > > Thanks so much for your help! I am very desperate now... > > Helvin I googled: vtk for beginners python. >From there, I found this: http://www.nabble.com/pls-help-how-to-install-VTK-and-run-td14977428.html And from there, this: http://www-viz.tamu.edu/courses/viza658/08spring/tutorials/WinVTKInstall.html This last one has very detailed info about how to get started with VTK. I am now redoing the installing VTK thing. Hope it will work. Helvin From mondi at cs.unibo.it Thu Jul 9 07:50:20 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Thu, 09 Jul 2009 11:50:20 +0000 Subject: hoe to build a patched socketmodule.c Message-ID: Hi all, I need to patch socketmodule.c (the _socket module) in order to add support to an experimental socket family. I'm guessing about the patched module build process: does a stand alone build script using distutils makes sense, or have I to patch sockmodule.c, eventualy rename it, modify setup.py inside python sources to let it know about the new module, and then rebuild whole python? I think the latter makes more sense, because I suppose socketmodule could not be build stand alone, but need the whole python build system, in order to have right definition specified by a Makefile generated by autotools from inside python sources. Or maybe distutils is able to retreive the whole build system? I don't think so, because there are no requirement about having python source tree installed. If I have to go for the second way, then how could I distribuite the patched _socket module? I could not use distutils, have I to distribuite the already built .so with only a script to install it, and the source code in another directory? Thanks a lot jacopo PS correct my english please, is the only way I have to improve it! From rashidsdpk at gmail.com Thu Jul 9 07:59:28 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Thu, 9 Jul 2009 04:59:28 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10. for more details on http://0nanotechnology0.blogspot.com From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 9 07:59:44 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 09 Jul 2009 13:59:44 +0200 Subject: property using a classmethod In-Reply-To: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: <4a55db9e$0$10243$426a74cc@news.free.fr> Emanuele D'Arrigo a ?crit : > Greetings, > > today I did something like this: > > class MyClass(object): > > @classmethod > def myClassMethod(self): Usually, the first argument of classmethods is named 'cls' > print "ham" > > myProperty = property(myClassMethod, None, None) > > As many of you know this doesn't work and returns a TypeError: the > object passed to the property is not a callable function but a > classmethod object, which isn't callable at all. So, how do I do this? > Ultimately all I want is a non-callable class-level attribute > MyClass.myProperty properties *are* class attributes. > that gives the result of MyClass.myClassMethod(). > > Can it be done? You could write your own custom descriptor. Or just use an additional level of indirection, ie: myProperty = property(lambda self: self.myClassMethod()) but since you already have myClassMethod available, I don't see the point. What problem are you trying to solve exactly ? From lie.1296 at gmail.com Thu Jul 9 08:10:33 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 12:10:33 GMT Subject: older pythons In-Reply-To: <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Lie Ryan wrote: > >> AFAIK, no major linux distributions have officially ported to python >> 3.x. > > http://packages.debian.org/experimental/python3.1 > > thanks for help Note the word "experimental" From michael at stroeder.com Thu Jul 9 08:17:49 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Thu, 09 Jul 2009 14:17:49 +0200 Subject: python-ldap In-Reply-To: References: Message-ID: Larry kavanagh wrote: > I need a PYTHON-LDAP package. > > I'm trying to install ploneldap and it tells me I need python-ldap > package first .. but I can't find one to match my versions. > > I'm using plone 3.2.2, Python 2.4.4 and Zope 2.10.7 on a Win32 environment. Did you check all the links on this page? http://www.python-ldap.org/download.shtml There's a MSI installer for Python 2.4 on Win32: http://pypi.python.org/packages/2.4/p/python-ldap/python-ldap-2.3.8.win32-py2.4.exe#md5=35da547711280c18bd4ccd6e637cdf9b There has been an update of the .egg files recently (also link on the download page above). Ciao, Michael. -- Michael Str?der E-Mail: michael at stroeder.com http://www.stroeder.com From xahlee at gmail.com Thu Jul 9 08:19:29 2009 From: xahlee at gmail.com (Xah Lee) Date: Thu, 9 Jul 2009 05:19:29 -0700 (PDT) Subject: OT: unix to Windows technology References: <4a54ceb0$0$5898$607ed4bc@cv.net> <16430ee7-888e-42ef-9c84-14df4b78ee04@p36g2000prn.googlegroups.com> Message-ID: <0de99585-6a35-498d-b5d1-6eb8724c41b3@m3g2000pri.googlegroups.com> a fried showed me this today: http://xahlee.org/funny/Microsoft_eula.html not sure which site originally reported it. Xah ? http://xahlee.org/ ? From user at example.net Thu Jul 9 08:26:37 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 14:26:37 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Message-ID: <4a55e1fb$0$1116$4fafbaef@reader3.news.tin.it> Lie Ryan wrote: > superpollo wrote: > >>Lie Ryan wrote: >> >> >>>AFAIK, no major linux distributions have officially ported to python >>>3.x. >> >>http://packages.debian.org/experimental/python3.1 >> >>thanks for help > > > Note the word "experimental" i noticed. isn't experimental official? i thought it was... thanks for your reply bye and god bless you From marco at sferacarta.com Thu Jul 9 08:37:11 2009 From: marco at sferacarta.com (Marco Mariani) Date: Thu, 09 Jul 2009 14:37:11 +0200 Subject: older pythons In-Reply-To: <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > what i was asking for is about a way to *INSTALL* and mantain different > python versions, a task i think is not unusal for developers. Check out virtualenv, I ask myself how I could work without it. http://pypi.python.org/pypi/virtualenv From lie.1296 at gmail.com Thu Jul 9 08:37:30 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 12:37:30 GMT Subject: property using a classmethod In-Reply-To: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: Emanuele D'Arrigo wrote: > Greetings, > > today I did something like this: > > class MyClass(object): > > @classmethod > def myClassMethod(self): > print "ham" > > myProperty = property(myClassMethod, None, None) > > As many of you know this doesn't work and returns a TypeError: the > object passed to the property is not a callable function but a > classmethod object, which isn't callable at all. That code runs fine for me. Although I doubt the behavior is what you wanted to do. > So, how do I do this? > Ultimately all I want is a non-callable class-level attribute > MyClass.myProperty that gives the result of MyClass.myClassMethod(). This works like what you seem to want (it's ugly): class MyClass(object): class _MyClass(object): @classmethod def myClassMethod(cls): return 'ham' @property def myProperty(self): return MyClass._MyClass.myClassMethod() @classmethod def myClassMethod(cls): return MyClass._MyClass.myClassMethod() @property def myProperty(self): return MyClass._MyClass.myClassMethod() def __call__(self, *args, **kargs): # this is __init__ return MyClass._MyClass(*args, **kargs) # note this is NOT a real MyClass instantiation MyClass = MyClass() $ python -i ./strangeclass.py >>> MyClass.myClassMethod() 'ham' >>> MyClass.myProperty 'ham' >>> mc = MyClass() >>> mc.myProperty 'ham' >>> mc.myClassMethod() 'ham' From mukherjee.tanmoy at gmail.com Thu Jul 9 08:43:57 2009 From: mukherjee.tanmoy at gmail.com (Tanmoy) Date: Thu, 9 Jul 2009 18:13:57 +0530 Subject: Hello Message-ID: > > Hello , > When i==0 you append an empty list to arr, so arr[i] is arr[0]. No > problem. > > When i==10 you append another empty list to arr, so arr[i] is arr[10]. > Index error because there's no arr[10], only arr[0] and arr[1]. > > Thanks for your prompt reply... > > How can i pass this problem > > You could try appending to arr[-1] instead. Better still: ... row = [] for j in range(0,1001,1): row.append(i+j) arr.append(row) ... I tried both the ways... the only thing is its not coming like 0 10 20.......1000 . Its coming like 0,1,2,........1000.....Any answer to the problem. Thanks Tanmoy Mukherjee -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Jul 9 08:44:18 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 08:44:18 -0400 Subject: function local namespace question In-Reply-To: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> References: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Message-ID: <4A55E622.3050906@ieee.org> Paul LaFollette wrote: > Kind people, > > Using Python 3.1 under FreeBSD and WinXP. > > I've been tearing my hair out trying to solve this myself, but I need > to ask for help. I want (for obscure reasons) to be able to log > transactions in the namespace(s) of a script. Specifically I would > like to log creation of identifiers, changes in the binding of > identifiers ("assignment") and lookups. This turns out to be pretty > easy in the global and local namespaces... I simply subclass dict, > override the appropriate operations to include the logging operations > I want, and then exec the code using my dictionaries as the global and > local namespaces. All of this works just dandy until I try to > extend it to functions. > > I cannot figure out any way to get a hook into the local namespace of > a user defined function. I have tried making a wrapper class that > grabs the function call and then uses exec to invoke > myfunction.__code__ with my own dictionaries. This runs the (no > argument) function properly (losing the return value, but I can deal > with that) but never accesses the local logging dictionary that I > specify in the exec() call. Perhaps the local namespace of a function > is not a dict at all? > > Anyway, is there any way (however clumsy) to do what I want to do? > Thank you for your help. > Paul > > --------------------------- > Paul LaFollette > CIS Department > Temple University > paul.lafollette(at)temple.edu > www.cis.temple.edu/~lafollet > > Is there a way? Undoubtedly. The simplest way that occurs to me is to have a pre-pass that rewrites the .pyc files, adding instrumentation to the byte-code. You could also recompile the Python interpreter, with some extra hooks in it. That could get tricky, as you don't want to instrument the code that's doing the tracking. Fnally, you could hook into the byte-code loader, doing the changes at that time. As always, if the code is for commercial use, beware of infringing on patents. I have a few in the 3rd area (though the ownership is with other companies; I was just the inventor.) DaveA From deets at nospam.web.de Thu Jul 9 08:54:25 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 09 Jul 2009 14:54:25 +0200 Subject: function local namespace question References: <65d199b50907081035l65ef8f2bn5a049aa3c1bd9b93@mail.gmail.com> Message-ID: <7bm7bsF22aoa9U1@mid.uni-berlin.de> Dave Angel wrote: > Paul LaFollette wrote: >> Kind people, >> >> Using Python 3.1 under FreeBSD and WinXP. >> >> I've been tearing my hair out trying to solve this myself, but I need >> to ask for help. I want (for obscure reasons) to be able to log >> transactions in the namespace(s) of a script. Specifically I would >> like to log creation of identifiers, changes in the binding of >> identifiers ("assignment") and lookups. This turns out to be pretty >> easy in the global and local namespaces... I simply subclass dict, >> override the appropriate operations to include the logging operations >> I want, and then exec the code using my dictionaries as the global and >> local namespaces. All of this works just dandy until I try to >> extend it to functions. >> >> I cannot figure out any way to get a hook into the local namespace of >> a user defined function. I have tried making a wrapper class that >> grabs the function call and then uses exec to invoke >> myfunction.__code__ with my own dictionaries. This runs the (no >> argument) function properly (losing the return value, but I can deal >> with that) but never accesses the local logging dictionary that I >> specify in the exec() call. Perhaps the local namespace of a function >> is not a dict at all? >> >> Anyway, is there any way (however clumsy) to do what I want to do? You might consider using the trace-functionality of python, exposed throug sys.settrace It's very expensive of course to do that. Diez From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 9 08:56:18 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 09 Jul 2009 14:56:18 +0200 Subject: property using a classmethod In-Reply-To: References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: <4a55e8e0$0$8041$426a34cc@news.free.fr> Lie Ryan a ?crit : > Emanuele D'Arrigo wrote: (snip) >> Ultimately all I want is a non-callable class-level attribute >> MyClass.myProperty that gives the result of MyClass.myClassMethod(). > > This works like what you seem to want (it's ugly): Ugly, indeed. And an extreme case of arbitrary overcomplexification too :-/ (snip rube goldberg code) From user at example.net Thu Jul 9 09:04:30 2009 From: user at example.net (superpollo) Date: Thu, 09 Jul 2009 15:04:30 +0200 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> Message-ID: <4a55eadd$0$1108$4fafbaef@reader3.news.tin.it> Marco Mariani wrote: > superpollo wrote: > >> what i was asking for is about a way to *INSTALL* and mantain >> different python versions, a task i think is not unusal for developers. > > > Check out virtualenv, I ask myself how I could work without it. > > http://pypi.python.org/pypi/virtualenv > much obliged! From lie.1296 at gmail.com Thu Jul 9 09:20:19 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 13:20:19 GMT Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: Michael Mossey wrote: > On Jul 6, 2:47 pm, Philip Semanchuk wrote: >> On Jul 6, 2009, at 5:37 PM, Michael Mossey wrote: >> >>> What is required in a python program to make sure it catches a >>> control- >>> c on the command-line? Do some i/o? The OS here is Linux. >> You can use a try/except to catch a KeyboardInterrupt exception, or >> you can trap it using the signal module:http://docs.python.org/library/signal.html >> >> You want to trap SIGINT. >> >> HTH >> Philip > > Thanks to both of you. However, my question is also about whether I > need to be doing i/o or some similar operation for my program to > notice in any shape or form that Control-C has been pressed. In the > past, I've written Python programs that go about their business > ignoring Ctrl-C. Other programs respond to it immediately by exiting. > I think the difference is that the latter programs are doing i/o. But > I want to understand better what the "secret" is to responding to a > ctrl-C in any shape or form. > > For example, does trapping SIGINT always work, regardless of what my > process is doing? > > Thanks, > Mike Are you asking: "when would the python interpreter process KeyboardInterrupt?" In a multi threaded python program (where KeyboardInterrupt doesn't always work), only the main thread can process KeyboardInterrupt; thus the main thread must regain control before the interrupt is raised. Normally, python will context switch (i.e. thread switch) every 100 interpreter "ticks", but when the interpreter received a SIGINT, the interpreter will try to context switch as fast as it can (every tick) to allow the main thread to regain control. So the answer is in multithreaded python program: "when the main thread regains control" In single threaded python program, the currently running thread is always the main thread (which can handle KeyboardInterrupt). I believe SIGINT is checked at every ticks. But SIGINT cannot interrupt atomic operations (i.e. it cannot interrupt long operations that takes a single tick). An example, where python have difficulties processing KeyboardInterrupt: >>> print 'foo'*10000000 ...foofoofoo... because printing a string is an atomic operation I believe a tick in python is equivalent to a single bytecode, but please correct me if I'm wrong. From python at mrabarnett.plus.com Thu Jul 9 09:25:36 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 09 Jul 2009 14:25:36 +0100 Subject: Hello In-Reply-To: References: Message-ID: <4A55EFD0.7080408@mrabarnett.plus.com> Tanmoy wrote: > Hello , > When i==0 you append an empty list to arr, so arr[i] is > arr[0]. No > problem. > > When i==10 you append another empty list to arr, so arr[i] is arr[10]. > Index error because there's no arr[10], only arr[0] and arr[1]. > > Thanks for your prompt reply... > > How can i pass this problem > > You could try appending to arr[-1] instead. > > Better still: > > ... > row = [] > for j in range(0,1001,1): > row.append(i+j) > arr.append(row) > ... > I tried both the ways... the only thing is its not coming like 0 10 > 20.......1000 . Its coming like 0,1,2,........1000.....Any answer to the > problem. > Each row should be 1 more than the previous row and each column should be 10 more than the previous column. Just swap your ranges. From Lacrima.Maxim at gmail.com Thu Jul 9 09:28:07 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Thu, 9 Jul 2009 06:28:07 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: On Jul 9, 2:31?pm, Piet van Oostrum wrote: > >>>>> Lacrima (L) wrote: > >L> Thanks for your reply! > >L> My file name is 'trains.py'. > >L> When I do C-h k C-c RET, it shows me help from manual: "C-c RET runs > >L> the command py-execute-import-or-reload > >L> ? ?which is an interactive Lisp function in `python-mode'..." and so > >L> on. > >L> And still when I do C-c RET, I receive "(Shell command succeeded with > >L> no output)" > >L> Any more help would be really appreciated, because I am newbie with > >L> Emacs. > > I tested it and I can get this message also. It happens when you have no > Python shell running (*Python* buffer). The python code is then executed > by calling the python shell command and catching the output. This output > is displayed in the *Python Output* buffer. It happens also with C-c C-c > and it will give the same message. > > There is a difference, however between C-c C-x and C-c RET: The latter > will import your module, and most modules are written such that > importing them doesn't print anything. If you add something like > print "test" and the end of file, then "test" will appear in the *Python > Output* ?buffer. > > If your file has the print "hello world" statement it should display in > the *Python Output* buffer. I checked that. > > On the other hand, if you have a python shell running you must make sure > (e.g. with the cd command) that its working directory is the directory > where your python file is located, otherwise the import statement will > not find your file. Unless it is somewhere in the python path. Also the > *Python* buffer will not automatically pop up if it is not visible, in > contrast with the *Python Output* buffer. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Thank you for really useful and detailed explanation. Now I can test my code using Emacs. But I think it works for me in a little bit different way. My file contains only the print 'hello world'. If I have no python shell running, then: a) C-c RET opens *Python Output* buffer without any output and with 'Shell command succeeded with no output' message at the bottom b) C-c C-c opens *Python Output* with appropriate 'hello world' message. Then I run python shell with C-c ! command. And: a) C-c RET opens *Python* buffer and prints 'hello world' in the python shell b) C-c C-c gives the same result. With regards, Max From davea at ieee.org Thu Jul 9 09:28:29 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 09:28:29 -0400 Subject: tough-to-explain Python In-Reply-To: <7blh0eF23r79rU1@mid.individual.net> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <7blh0eF23r79rU1@mid.individual.net> Message-ID: <4A55F07D.7040400@ieee.org> greg wrote: >
Dave > Angel wrote: >> By the time I graduated, I had five six-level languages > ^^^ > > Are they languages that you have to edit using vi? :-) > Back then I didn't need glasses. That was of course intended to be "six high-level languages" From shinejose22 at gmail.com Thu Jul 9 09:40:18 2009 From: shinejose22 at gmail.com (Shine Jose) Date: Thu, 9 Jul 2009 08:40:18 -0500 Subject: Problem with two instances of PySerial Message-ID: Hello friends,I am developing an application to read data from serial port and display it on the screen using GUI developed in pyQT. For this I have instantiated the GUI as well as pySerial objects in the main thread. I have provided a button 'Read' which when clicked by the user reads from the serial port using read() method of pySerial. However, for debugging purposes I also want to poll the serial port continuously for arrival of data and display on screen. I achieve this by creating a separate worker thread to poll the serial port for arrival of data and then updating the required widget in main thread of program. For polling the serial port, I create a separate instance of pySerial object. However, I am unable to read any data in the worker thread. Can the reason for this be 2 instances of pySerial objects being connected to serial port. The reason I had this doubt was because Serial.Connect() in the worker thread did not throw any exception & isOpen() method returns true. I request you to help me in figuring out the problem. -- Regards, Shine Jose -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Jul 9 09:41:38 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 09:41:38 -0400 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: <4A55F392.3080404@ieee.org> m.reddy prasad reddy wrote: > can any one tell me how to write assembly language programs in python...if > no is there any other way to write the programs in python > > Reddi prasad reddy > ph.no:09958083797 > > Assembly language is a different programming language than Python. You can use both in the same process, much in the same way you can use C or C++ with Python. In fact, some of the system DLL's are written (partly) in assembler, though mostly in C or C++. You'll need to tell us what your real goal is. Are you a python programmer trying to optimize some loop? Are you taking a course in assembler, and hope that somehow Python will help? Do you have Python installed on your system? What system is it? As far as I know, Python has no "inline assembler" mode, as C does. So anything written in assembler would be in a separate DLL, not mixed in your .py files. It's not hard to mix assembly files with C or C++ files (or Pascal, or dozens of other compiled languages), and link them into a single DLL. But Python's access to that DLL will be done through a module like ctypes, or SWIG. DaveA From nleioatt at gmail.com Thu Jul 9 09:42:06 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 9 Jul 2009 06:42:06 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" Message-ID: I've seen a lot of posts on this problem, but none seems to help. Here is the code: /code file = open(prefix1) text = file.readlines() len = len(text) fields = text[1].split() num_rows = int(fields[1]) num_cols = int(fields[2]) U1_matrix = [] print fields print repr(fields) print len(fields) for line in text[2: num_rows+2]: fields = line.split() # print "fields", fields, line for i in range(len(fields)): fields[i] = float(fields[i]) U1_matrix.append(fields) /*code prefix is a space/line delimited ascii file that represents a 2D matrix. i'm trying to read in 2 matrices from different files, strip away the header stuff and then take the dot product of the 2 matrices. any help is much appreciated. thanks, nick From gabriel.rossetti at arimaz.com Thu Jul 9 09:43:01 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Thu, 09 Jul 2009 15:43:01 +0200 Subject: Blocking XMPP API? Message-ID: <4A55F3E5.8040800@arimaz.com> Hello everyone, I am looking for blocking XMPP API. I'm wanting to make a webservice that uses XMPP, but all the XMPP libs I find are non-blocking (i.e. w/ callbacks). I'd like to be able to do something like : cl = Client("test at domain.com/res01", "password",) msg = " References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: On Thu, Jul 9, 2009 at 1:05 AM, Simon Forman wrote: > Everyone gets so caught up in programming via languages that > you get, well, people trying to teach "Computer Programming" as if it > were only necessary to grok a language, rather than grokking /symbol > manipulation/ itself. > > +1 QOTW. I'm a CS major and this is exactly what I see happening in the intro to CS courses. In class, the "Intro to Programming" students are showed how to write Java code. Then they try their homework and are completely lost because they were never taught how to think like a programmer so they don't understand how to approach the question. From lutz.horn at fastmail.fm Thu Jul 9 09:50:02 2009 From: lutz.horn at fastmail.fm (Lutz Horn) Date: Thu, 09 Jul 2009 15:50:02 +0200 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <4A55F58A.9060003@fastmail.fm> Hi, Nick schrieb: > I've seen a lot of posts on this problem, but none seems to help. Could you please post a sample input file and the exact error message? Thanks Lutz -- Strike Out ? http://www.fourmilab.ch/documents/strikeout From benjamin.kaplan at case.edu Thu Jul 9 09:51:49 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 9 Jul 2009 09:51:49 -0400 Subject: older pythons In-Reply-To: References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> Message-ID: On Thu, Jul 9, 2009 at 8:10 AM, Lie Ryan wrote: > superpollo wrote: >> Lie Ryan wrote: >> >>> AFAIK, no major linux distributions have officially ported to python >>> 3.x. >> >> http://packages.debian.org/experimental/python3.1 >> >> thanks for help > > Note the word "experimental" Assuming that Debian does the same thing as Ubuntu (which is highly likely), Python3.1 won't install itself as the default. Too many system features depend on Python 2.x. > -- > http://mail.python.org/mailman/listinfo/python-list > From fridrik at pyth.net Thu Jul 9 09:53:29 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 13:53:29 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <3A693537-927D-44B1-840A-D97FACFB6809@pyth.net> Look at: len = len(text) You're overriding `len` (a built-in method), with an integer (`len(text)`). You then call: for i in range(len(fields)): But `len` is no longer a callable, but merely an integer. Regards, Fri?rik M?r P.S. While this is a fairly obvious problem it's usually a good idea to post working code and a traceback when requesting help. From gagsl-py2 at yahoo.com.ar Thu Jul 9 09:53:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 09 Jul 2009 10:53:42 -0300 Subject: Check file is locked? References: <652cca82-44a3-473f-b640-c2336a9cf929@v15g2000prn.googlegroups.com> Message-ID: En Thu, 09 Jul 2009 07:53:29 -0300, Rajat escribi?: > Thanks Tim for the details. Just further on this, my whole idea is to > close the wordpad / notepad application so that I can delete the file > and the directory where this file resides. > > With notepad it is no more a problem. But I'm concerned about the > wordpad now. I don't see Wordpad keeping the file open either. If you can't remove the file, use "handle.exe filename.txt" to discover which process is holding it open, as Tim already explained. -- Gabriel Genellina From R.Brodie at rl.ac.uk Thu Jul 9 10:02:27 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Thu, 9 Jul 2009 15:02:27 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: "Nick" wrote in message news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f at h18g2000yqj.googlegroups.com... > file = open(prefix1) > text = file.readlines() > len = len(text) You have redefined two built-in functions "file" and "len" in the first three lines. This is usually considered poor practice. Stick to meaningless variable names, it's safer (only joking). TypeError: 'int' object is not callable". This means that something you thought was a function is in fact an integer. It's helpful to post/look at the line number of the error; "how is this line failing", is much easier to answer than "how is my program failing". print len(fields) Here len is an integer, because you redefined it in line 3. I'm guessing this is the problem. From davea at ieee.org Thu Jul 9 10:04:23 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 10:04:23 -0400 Subject: Hello In-Reply-To: References: Message-ID: <4A55F8E7.6090304@ieee.org> You could pick a much better title. And apparently you started a new thread with this one. When responding to an existing message, you ought to reply-all to the existing message, rather than starting a new thread. Tanmoy wrote: >> Hello , >> When i==0 you append an empty list to arr, so arr[i] is arr[0]. No >> problem. >> >> When i==10 you append another empty list to arr, so arr[i] is arr[10]. >> Index error because there's no arr[10], only arr[0] and arr[1]. >> >> Thanks for your prompt reply... >> >> How can i pass this problem >> >> You could try appending to arr[-1] instead. >> > > Better still: > > ... > row = [] > for j in range(0,1001,1): > row.append(i+j) > arr.append(row) > ... > I tried both the ways... the only thing is its not coming like 0 10 > 20.......1000 . Its coming like 0,1,2,........1000.....Any answer to the > problem. > Thanks > Tanmoy Mukherjee > > If you want the row to count by 10's you have to build it that way. You have the arguments to the ranges backwards. Try: arr=[] for i in range(1001): row = [] for j in range(0,1010,10): row.append(i+j) arr.append(row) Coincidentally, once you reverse them, the original problem would go away. But this is better anyway, as it makes it clear what's happening. You are building 1001 rows, each containing non-contiguous numbers. DaveA From timmyt at smarttypes.org Thu Jul 9 10:07:52 2009 From: timmyt at smarttypes.org (timmyt) Date: Thu, 9 Jul 2009 07:07:52 -0700 (PDT) Subject: a little wsgi framework References: <4a53132c$0$10839$426a34cc@news.free.fr> Message-ID: <103cb80e-5a5a-4c41-ae7c-c0ae122cfebf@b25g2000prb.googlegroups.com> On Jul 7, 5:19?am, Bruno Desthuilliers wrote: > timmyt a ?crit : > > > i'm interested in getting opinions on a smallwsgiframework i > > assembled from webob, sqlalchemy, genshi, and various code fragments i > > found on the inter-tubes > > > here is the interesting glue - any comments / suggestions would be > > much appreciated > > > Well... My first comment would be about the usefulness of yet another > Python web framework, but let's not worry about this... > > > > ------------------ > > thewsgiapp > > ------------------ > > def application(environ, start_response): > > ? ? path = environ.get('PATH_INFO', '').lstrip('/') > > > ? ? for regex, callback in urls: > > ? ? ? ? match = re.search(regex, path) > > I don't know where these "urls" come from. But anyway : if they live in > some sort of long-running process, you may want to precompile them. > > > ? ? ? ? if match: > > ? ? ? ? ? ? environ['myapp.url_args'] = match.groups() > > ? ? ? ? ? ? request = webob.Request(environ) > > > ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? return callback(request, start_response) > > ? ? ? ? ? ? except Exception, ex: > > How are redirect etc handled ? > > > ? ? ? ? ? ? ? ? start_response('500 Internal Server Error', [('Content- > > Type', 'text/plain')]) > > ? ? ? ? ? ? ? ? return [traceback.format_exc()] > > A configuration option controlling the display of the traceback would be > fine - I surely don't want the traceback being displayed to anyone when > ? the app goes into production. > > Also, logging the error and traceback might be useful. > > > ? ? start_response('404 Not Found', [('Content-Type', 'text/plain')]) > > ? ? return ["Couldn't find the URL specified."] > > And in both cases, having the ability to use "custom" 500 and 404 pages > might be nice too. > > > > > ---------------------------------- > > the controller decorator > > ---------------------------------- > > def web_decorator(filename, method='html'): > > > ? ? def decorator(target): > > May I suggest some renaming here ? > > filename => path (or 'template_path' ?) > method ? => content_type > target ? => controller or function or callback or.... > > > ? ? ? ? def wrapper(request, start_response): > > > ? ? ? ? ? ? #genshi TemplateLoader > > ? ? ? ? ? ? template = loader.load(filename) > > > ? ? ? ? ? ? global config > > Probably not thread-safe.... > > > ? ? ? ? ? ? try: > > ? ? ? ? ? ? ? ? return_dict = target(request, start_response) > > ? ? ? ? ? ? ? ? return_string = template.generate(**return_dict).render > > (method) > > Renaming again: > return_dict => context > return_string => data or text or ??? > > > ? ? ? ? ? ? ? ? config['database.Session'].commit() > > ? ? ? ? ? ? except: > > ? ? ? ? ? ? ? ? config['database.Session'].rollback() > > ? ? ? ? ? ? ? ? raise > > ? ? ? ? ? ? finally: > > ? ? ? ? ? ? ? ? config['database.Session'].remove() > > This doesn't leave much control on transactions... Sometimes you want to > handle it manually one way or another. > > > ? ? ? ? ? ? #TODO: alter 'Content-Type' per method being passed > > ? ? ? ? ? ? start_response('200 OK', [('Content-Type', 'text/html')]) > > ? ? ? ? ? ? return [return_string] > > Ok, so here again, I don't understand how redirects can work. Also, if > you do use start_response here, I don't get why you still pass it to the > callback function. > > > ? ? ? ? return wrapper > > > ? ? return decorator > > slightly OT, but preserving infos on the decorated function might help > too (introspection, debugging etc). > > My 2 cents... thank you gentlemen i'm now looking for Redirect exceptions as well, and returning the appropriate response code (stole this idea from turbogears) i like doing the response codes and exceptions myself which is why i'm not using the webob response - i think it's more explicit and straightforward the conditional display of errors goes without say - that will be trivial to add the 'global config' line was an error - config is a module level variable set before the application definition i assume as long as i don't write to the config variable within the application it is thread-safe - is that correct the underlying assumption is the wsgi application setup does not have to be thread safe - this is only run once per process From nleioatt at gmail.com Thu Jul 9 10:08:05 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 9 Jul 2009 07:08:05 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> On Jul 9, 10:02 am, "Richard Brodie" wrote: > "Nick" wrote in message > > news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f at h18g2000yqj.googlegroups.com... > > > file = open(prefix1) > > text = file.readlines() > > len = len(text) > > You have redefined two built-in functions "file" and "len" in the first three lines. > This is usually considered poor practice. Stick to meaningless variable names, > it's safer (only joking). > > TypeError: 'int' object is not callable". This means that something you thought > was a function is in fact an integer. It's helpful to post/look at the line number of > the error; "how is this line failing", is much easier to answer than > "how is my program failing". > > print len(fields) > > Here len is an integer, because you redefined it in line 3. I'm guessing this is the > problem. thanks for spotting the obvious errors, its my 2nd day programming python in about 3 years. fridrick, code should be workable with the exception of the errors...thats the whole program Thanks again for all the help problem fixed From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 9 10:24:19 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 09 Jul 2009 16:24:19 +0200 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <4a55fd80$0$9943$426a74cc@news.free.fr> Nick a ?crit : > I've seen a lot of posts on this problem, but none seems to help. > Here is the code: > /code > > file = open(prefix1) shadows the builtin 'file' type. > text = file.readlines() > len = len(text) shadows the builtin 'len' function. > fields = text[1].split() > num_rows = int(fields[1]) > num_cols = int(fields[2]) > > U1_matrix = [] > > print fields > print repr(fields) > print len(fields) And here's your problem - 'len' is now bound to the result of the previous call to len(text). Hint : Python's functions, classes and modules are objects too, and don't live in a distinct namespace. So _don't_ use builtin's types / functions / etc names as identifiers. HTH From davea at ieee.org Thu Jul 9 10:52:14 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 09 Jul 2009 10:52:14 -0400 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <4A56041E.6020206@ieee.org> Nick wrote: > I've seen a lot of posts on this problem, but none seems to help. > Here is the code: > /code > > file = open(prefix1) > text = file.readlines() > len = len(text) > fields = text[1].split() > num_rows = int(fields[1]) > num_cols = int(fields[2]) > > U1_matrix = [] > > print fields > print repr(fields) > print len(fields) > > for line in text[2: num_rows+2]: > fields = line.split() > # print "fields", fields, line > for i in range(len(fields)): > fields[i] = float(fields[i]) > U1_matrix.append(fields) > > /*code > > prefix is a space/line delimited ascii file that represents a 2D > matrix. i'm trying to read in 2 matrices from different files, strip > away the header stuff and then take the dot product of the 2 > matrices. any help is much appreciated. > > thanks, > nick > > You have at least two problems with that code, one of which is causing your symptom. Both 'file' and 'len' are defined in the standard library, and shouldn't be redefined in your code. And your problem is that after you redefined 'len', you then tried to use it in its original meaning. Rename those two and you'll get further. And it would have saved lots of time for lots of people if you included sample data and the actual error message, marking where in your code it occurs. Once you know it's complaining about the len() call, it's not too hard to figure out that the problem was you rebound the len attribute from a function to an integer. Traceback (most recent call last): File "M:\Programming\Python\sources\dummy\echo2.py", line 21, in print len(fields) TypeError: 'int' object is not callable DaveA From jcd at sdf.lonestar.org Thu Jul 9 11:15:41 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Thu, 09 Jul 2009 11:15:41 -0400 Subject: count In-Reply-To: References: <050094ea-faf4-4e03-875d-9c2c63090a89@y17g2000yqn.googlegroups.com> <7xbpnuzw4u.fsf@ruckus.brouhaha.com> Message-ID: <1247152541.3694.45.camel@aalcdl07> Bearophile wins! (This only times the loop itself. It doesn't check for __len__) summer:5 0:00:00.000051 bearophile:5 0:00:00.000009 summer:50 0:00:00.000030 bearophile:50 0:00:00.000013 summer:500 0:00:00.000077 bearophile:500 0:00:00.000053 summer:5000 0:00:00.000575 bearophile:5000 0:00:00.000473 summer:50000 0:00:00.005583 bearophile:50000 0:00:00.004625 summer:500000 0:00:00.055834 bearophile:500000 0:00:00.046137 summer:5000000 0:00:00.426734 bearophile:5000000 0:00:00.349573 summer:50000000 0:00:04.180920 bearophile:50000000 0:00:03.652311 summer:500000000 0:00:42.647885 bearophile: 500000000 0:00:35.190550 On Thu, 2009-07-09 at 04:04 -0700, Bearophile wrote: > Paul Rubin: > > print x, sum(1 for _ in g) > > Don't use that, use my function :-) If g has a __len__ you are wasting > time. And sum(1 ...) is (on my PC) slower. > > > J. Clifford Dyer: > > if __name__ == '__main__': > > test_func(summer, 10000000) > > test_func(tupler, 10000000) > > test_func(summer, 100000000) > > test_func(tupler, 100000000) > > Have you forgotten my function? > > Bye, > bearophile From ethan at stoneleaf.us Thu Jul 9 11:16:30 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 09 Jul 2009 08:16:30 -0700 Subject: tough-to-explain Python In-Reply-To: <007075aa$0$9711$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <007075aa$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A5609CE.70807@stoneleaf.us> Steven D'Aprano wrote: > > There is some evidence that 30-60% of people simply cannot learn to > program, no matter how you teach them: > > http://www.codinghorror.com/blog/archives/000635.html > http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ > > I'm sympathetic to the idea, but not entirely convinced. Perhaps the > problem isn't with the students, but with the teachers, and the > languages: > > http://www.csse.monash.edu.au/~damian/papers/PDF/SevenDeadlySins.pdf > > (My money is that it's a little of both.) > > Very interesting articles. Not suprising (to me, at least) -- everybody has their strengths and weaknesses. I will never be, and could never have been, any kind of sports person; I don't have the right personality to be a good people person; I don't have the knack for writing stories. Not everyone can do anything, or even many things. ~Ethan~ From fridrik at pyth.net Thu Jul 9 11:30:35 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 15:30:35 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> References: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> Message-ID: <391EDF07-7FCE-4DD2-B510-1EBA7A70D36A@pyth.net> Previously, I wrote: >> P.S. While this is a fairly obvious problem it's usually a good >> idea to post working code and a traceback when requesting help. Nick wrote: > thanks for spotting the obvious errors, its my 2nd day programming > python in about 3 years. I'm sorry, my saying it was obvious may have come off a little arrogant. My clumsily delivered point was that because it was a small snippet of code it didn't take much time to run through for someone who codes daily with Python. What you did there was a perfectly ordinary thing to do until experience teaches you how Python does things. :) > fridrick, code should be workable with the exception of the > errors...thats the whole program You're right, I failed to say it explicitely but I was referring to the input file. In some cases, albeit not this one, problems can exist in parsing gotchas. Regards, Fri?rik M?r From robert.kern at gmail.com Thu Jul 9 11:54:37 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 09 Jul 2009 10:54:37 -0500 Subject: PyQt GUI In-Reply-To: <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> Message-ID: On 2009-07-09 01:27, Helvin wrote: > On Jul 9, 11:29 am, Robert Kern wrote: >> On 2009-07-08 18:10, Helvin wrote: >> >>> Thanks for the fast replies! I will look into how to use VTK now. >>> Where would I find VTK's explicit support for PyQt? >> Wrapping/Python/vtk/qt4/ in the VTK sources. >> >>> Because I have installed VTK (using its installer) and pyVTK (using >>> its setup.py file), but how do I actually use it in my code? According >>> to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., >>> I have tried 'import vtk', but python can't find the vtk module. >> Then you have not installed VTK's Python bindings correctly. Note that pyVTK is >> just a library for manipulating VTK files. The VTK Python bindings are part of >> VTK's distribution itself. Exactly how did you install VTK? Did you compile it >> yourself? > > You mean, when I download VTK, the VTK Python bindings come with it? > I downloaded VTK from:http://vtk.org/files/release/5.4/ Exactly which file did you download? I don't think the vtk-5.4.x-win32.exe installers have the Python bindings. You will need to build VTK from sources. -- 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 psimon at sonic.net Thu Jul 9 12:02:34 2009 From: psimon at sonic.net (Paul Simon) Date: Thu, 9 Jul 2009 09:02:34 -0700 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> Message-ID: <4a5615ba$0$95523$742ec2ed@news.sonic.net> "Peter Otten" <__peter__ at web.de> wrote in message news:h3481q$d95$00$1 at news.t-online.com... > Paul Simon wrote: > >> "Chris Rebert" wrote in message >> news:mailman.2863.1247095339.8015.python-list at python.org... >> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>> I have the "tkinter" problem and need some assistance to straighten it >>> out. >>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >>> >"step >>> 1" and cannot import "_tkinter." I do not have that file on my computer, >>> but >>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>> This python stuff is great, but the documentation frequently >>> feels like it is just a bit out of my grasp. I realize that all of this >>> is free but I understand the instructions on the web page to repair only >>> to the >>> point of confusion. I'm not an expert. How do I modify my python >>> configuration? Is there a file that needs to be edited? Which setup.py >>> file >>> do I use? Make? or python setup.py build and python setup.py install? >>> Thanks. I appreciate your help. >> >> - How did you install Python? >> - What Linux distro are you using? >> >> Cheers, >> Chris >> http://blog.rebertia.com >> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure >> exactly how I installed Python. Originally I had installed 2.5 from RPM >> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >> something from python.org and installed. Not sure if it was tarball or >> zip file. > > Zip or tar doesn't matter, you are installing "from source". > > Python has to find the necessary include files for tcl/tk. These are in > separate packages that you have to install before you invoke Python's > configure script. > > I don't know what they are called on your system -- look for tk-dev.rpm, > tcl-dev.rpm or similar. > > You may run into the same problem with other modules like readline. > > Peter > Thank you Peter. I understand what you are saying but don't know how to do it. Although I installed from source, I followed a "cookbook" recipe. Could you tell me what files to execute, where they might be, and file arguments? I'm just ignorant, not stupid. ;-). Paul From nleioatt at gmail.com Thu Jul 9 12:04:58 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 9 Jul 2009 09:04:58 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" References: <75c32082-81ac-46e8-9ec2-195e63e3d829@y7g2000yqa.googlegroups.com> Message-ID: no problem, i understand, i haven't coded anything in literally 2 years, but it was a simple and pretty obvious mistake. thanks for all your help, nick On Jul 9, 11:30 am, Fri?rik M?r J?nsson wrote: > Previously, I wrote: > >> P.S. While this is a fairly obvious problem it's usually a good > >> idea to post working code and a traceback when requesting help. > Nick wrote: > > thanks for spotting the obvious errors, its my 2nd day programming > > python in about 3 years. > > I'm sorry, my saying it was obvious may have come off a little > arrogant. My clumsily delivered point was that because it was a small > snippet of code it didn't take much time to run through for someone > who codes daily with Python. What you did there was a perfectly > ordinary thing to do until experience teaches you how Python does > things. :) > > > fridrick, code should be workable with the exception of the > > errors...thats the whole program > > You're right, I failed to say it explicitely but I was referring to > the input file. In some cases, albeit not this one, problems can > exist in parsing gotchas. > > Regards, > Fri?rik M?r From tkermode at gmail.com Thu Jul 9 12:06:45 2009 From: tkermode at gmail.com (Tom Kermode) Date: Thu, 9 Jul 2009 17:06:45 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Hi, Do you know a good way to avoid running into this problem? It makes sense to suggest not calling variables the same names as built-in functions, but that's hard for a new python programmer who doesn't already know what all the built-in functions are. Over time a programmer will learn which names to avoid, but it's a bit of a pitfall early on. Cheers, Tom 2009/7/9 Richard Brodie : > > "Nick" wrote in message > news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f at h18g2000yqj.googlegroups.com... > >> file = open(prefix1) >> text = file.readlines() >> len = len(text) > > You have redefined two built-in functions "file" and "len" in the first three lines. > This is usually considered poor practice. Stick to meaningless variable names, > it's safer (only joking). > > TypeError: 'int' object is not callable". This means that something you thought > was a function is in fact an integer. It's helpful to post/look at the line number of > the error; "how is this line failing", is much easier to answer than > "how is my program failing". > > print len(fields) > > Here len is an integer, because you redefined it in line 3. I'm guessing this is the > problem. > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.kiloday.com http://www.fourstopspast.com From R.Brodie at rl.ac.uk Thu Jul 9 12:30:42 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Thu, 9 Jul 2009 17:30:42 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: "Tom Kermode" wrote in message news:mailman.2903.1247155607.8015.python-list at python.org... > Do you know a good way to avoid running into this problem? It > makes sense to suggest not calling variables the same names as > built-in functions, but that's hard for a new python programmer who > doesn't already know what all the built-in functions are. No, but not redefining the ones you actually use is a good start. Learning to understand the traceback is the more important lesson, IMHO. It takes a while to tune into what error messages are trying to tell you; even when you stop making newbie mistakes, you're going to have to deal with runtime errors from time to time. From m at n.d Thu Jul 9 12:32:41 2009 From: m at n.d (Broken) Date: Thu, 9 Jul 2009 18:32:41 +0200 Subject: send keys to focused window Message-ID: Hi, I am new to Python, and I'm miserably failing to send specific keys to (say) notepad. Here is the scenario: I have notepad open. My python script is running in the background. When I press ALT+a I want to intercept the keys and send "?"(ASCII code: ALT+0228) instead. OS: Windows 7 Libraries used: pyHook, SendKeys, pyWin32 Here is my code: ############################### # -*- coding: cp1252 -*- import pythoncom, pyHook from SendKeys import SendKeys def OnKeyboardEvent(event): if event.Alt == 32: #alt pressed if event.Ascii == 97: #a pressed SendKeys("?") return False #don't pass to other handlers # return True to pass the event to other handlers return True # create a hook manager hm = pyHook.HookManager() # watch for all mouse events hm.KeyDown = OnKeyboardEvent # set the hook hm.HookKeyboard() # wait forever pythoncom.PumpMessages() ############################### It's largely copied from the pyhook example. My problem is, I need to somehow disable ALT while sonding the key. Please help :) From h at realh.co.uk Thu Jul 9 12:33:43 2009 From: h at realh.co.uk (Tony Houghton) Date: Thu, 9 Jul 2009 17:33:43 +0100 Subject: Running a script to build docs from setup.py Message-ID: <20090709173343.1784710d@realh.co.uk> I want to write a setup.py script, using distutils, for a python library called ROX-Lib2 (package name "rox"). The library includes a script to build HTML documentation from the pydoc strings. I'd like to run that script from setup.py but I don't know the best way to do that. I've looked through the manual but I can't find any hooks in distutils for generating files at install time other than extension modules and .pyc files. Should I just run the script from somewhere in my setup.py before calling distutils' setup function? -- TH * http://www.realh.co.uk From fridrik at pyth.net Thu Jul 9 12:40:47 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 16:40:47 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Tom Kermode wrote: > Do you know a good way to avoid running into this problem? It > makes sense to suggest not calling variables the same names as > built-in functions, but that's hard for a new python programmer who > doesn't already know what all the built-in functions are. One way is using a code checker like PyChecker[1]. This neat software for finding bugs will check for lots of other pitfalls too, but you can filter it down to what you need if you're only interested in this one. I don't use an IDE, but this would seem like something for an IDE[2] to support if you're into that kind of magic. Regards, Fri?rik M?r [1] http://pychecker.sourceforge.net/ [2] http://wiki.python.org/moin/IntegratedDevelopmentEnvironments From __peter__ at web.de Thu Jul 9 13:00:37 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Jul 2009 19:00:37 +0200 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > > "Peter Otten" <__peter__ at web.de> wrote in message > news:h3481q$d95$00$1 at news.t-online.com... >> Paul Simon wrote: >> >>> "Chris Rebert" wrote in message >>> news:mailman.2863.1247095339.8015.python-list at python.org... >>> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>>> I have the "tkinter" problem and need some assistance to straighten it >>>> out. >>>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >>>> >"step >>>> 1" and cannot import "_tkinter." I do not have that file on my >>>> computer, but >>>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>>> This python stuff is great, but the documentation frequently >>>> feels like it is just a bit out of my grasp. I realize that all of this >>>> is free but I understand the instructions on the web page to repair >>>> only to the >>>> point of confusion. I'm not an expert. How do I modify my python >>>> configuration? Is there a file that needs to be edited? Which setup.py >>>> file >>>> do I use? Make? or python setup.py build and python setup.py install? >>>> Thanks. I appreciate your help. >>> >>> - How did you install Python? >>> - What Linux distro are you using? >>> >>> Cheers, >>> Chris >>> http://blog.rebertia.com >>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not >>> sure >>> exactly how I installed Python. Originally I had installed 2.5 from RPM >>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>> something from python.org and installed. Not sure if it was tarball or >>> zip file. >> >> Zip or tar doesn't matter, you are installing "from source". >> >> Python has to find the necessary include files for tcl/tk. These are in >> separate packages that you have to install before you invoke Python's >> configure script. >> >> I don't know what they are called on your system -- look for tk-dev.rpm, >> tcl-dev.rpm or similar. >> >> You may run into the same problem with other modules like readline. >> >> Peter >> > > Thank you Peter. I understand what you are saying but don't know how to > do > it. Although I installed from source, I followed a "cookbook" recipe. > Could you tell me what files to execute, where they might be, and file > arguments? I'm just ignorant, not stupid. ;-). > > Paul Once you have the necessary development packages for tcl/tk just go into the directory where you unzipped the source and ./configure make sudo make altinstall Unfortunately I don't know the names of these packages nor how to install them, and Google didn't turn up anything useful. If you don't get any additional answers here you may try a Mandriva forum since this is not a question that requires python knowlegde. Peter From duane.kaufman at gmail.com Thu Jul 9 13:14:29 2009 From: duane.kaufman at gmail.com (TheSeeker) Date: Thu, 9 Jul 2009 10:14:29 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted Message-ID: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Hi, I am embarking on teaching myself Microsoft UI Automation using Python as the scripting language. I have asked some questions in the IronPython users group, but have yet to get a response, so I thought I would broaden the audience by asking here. Specifically, I have a WinForms application I will be wanting to automate. Does anyone have some Python examples of driving Microsoft UI Automation they could share with me to get me started? The structure of the UI automation classes etc. seem quite convoluted, and I am having difficulty getting my brain wrapped around it. Alternatives to Microsoft's UI Automation are welcome too, but I have tried using winguiauto and watsup (along with AutoIt), and there seems to be severe limitations when using these tools with WinForm applications. Thanks in advance, Duane From joshua at joshuakugler.com Thu Jul 9 13:17:25 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Thu, 09 Jul 2009 09:17:25 -0800 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: Joshua Kugler wrote: > Sorry about that...since pysqlite and APSW are both discusses on the > pysqlite list, I had made an incorrect assumption. Oops. "are both discusses?" Yeef, I must have been out of it. Discussed, thank you. :) j From usernet at ilthio.net Thu Jul 9 13:18:49 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 09 Jul 2009 17:18:49 GMT Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: On 2009-07-09, TheSeeker wrote: > Specifically, I have a WinForms application I will be wanting to > automate. Does anyone have some Python examples of driving Microsoft > UI Automation they could share with me to get me started? The > structure of the UI automation classes etc. seem quite convoluted, and > I am having difficulty getting my brain wrapped around it. If you find a way to work through the UI using the keyboard (tabs, etc); then, you can send it keyboard commands using WScript.WshShell.SendKeys(): http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx From steve at REMOVE-THIS-cybersource.com.au Thu Jul 9 13:20:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 Jul 2009 17:20:14 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <007201bc$0$9710$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > The core abstractions of [mechanical] computation are just not that > complicated. You can teach them to anybody in about a half an hour, > drunk. I have. That's *easy*. Anyone can teach the most complicated and abstract principles of any topic at all drunk. What's hard is doing it sober. http://stackoverflow.com/questions/63668/confessions-of-your-worst-wtf- moment-what-not-to-do/350267#350267 or http://tinyurl.com/lur784 You'll excuse my skepticism about all these claims about how anyone can program, how easy it is to teach the fundamentals of Turing Machines and functional programming to anybody at all. Prove it. Where are your peer- reviewed studies demonstrating such successes on randomly chosen people, not self-selected motivated, higher-than-average intelligence students? In the real world, human beings are prone to serious cognitive biases and errors. Our brains are buggy. Take any list of reasoning fallacies, and you'll find the majority of people have difficulty avoid them. The first struggle is to get them to even accept that they *are* fallacies, but even once they have intellectually accepted it, getting them to live up to that knowledge in practice is much, much harder. In fact I'd go further and say that *every single person*, no matter how intelligent and well-educated, will fall for cognitive biases and fallacious reasoning on a regular basis. http://en.wikipedia.org/wiki/Cognitive_bias http://en.wikipedia.org/wiki/Fallacy -- Steven From lie.1296 at gmail.com Thu Jul 9 13:30:02 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 17:30:02 GMT Subject: Cleaning up after failing to contructing objects In-Reply-To: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: brasse wrote: > Hello! > I have been thinking about how write exception safe constructors in > Python. By exception safe I mean a constructor that does not leak > resources when an exception is raised within it. The following is an > example of one possible way to do it: First, your automatic cleaning Bar() silently pass an unitialized Bar() into the calling code. When a code fails, it should fail as loud as possible. Bar() should raises an Exception and the calling code should turn into something like: try: bar = Bar() except Exception, e: print 'bar failed to construct' And about the cleaning up, how about: class CleanWrap(object): def __init__(self, cls, cleanup): self.cls = cls self.cleanup = cleanup def __call__(self, *args, **kargs): try: return self.cls(*args, **kargs) except: self.cleanup() raise class Bar(object): def __init__(self): CleanWrappedFoo = CleanWrap(Foo, self.close) self.a = CleanWrappedFoo('a') self.b = CleanWrappedFoo('b', fail=True) def close(self): if hasattr(self, 'a'): self.a.close() if hasattr(self, 'b'): self.b.close() try: bar = Bar() except Exception, e: print 'Bar() failed to construct' ========== My next attempt is pretty neat, using a totally different approach, see the docstring for details: class Foo1(object): def __init__(self, name, fail=False): self.name = name cls_name = self.__class__.__name__ if not fail: print '%s.__init__(%s)' % (cls_name, self.name) else: print '%s.__init__(%s), FAIL' % (cls_name, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) class Foo2(object): def __init__(self, name, fail=False): self.name = name cls_name = self.__class__.__name__ if not fail: print '%s.__init__(%s)' % (cls_name, self.name) else: print '%s.__init__(%s), FAIL' % (cls_name, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) class CleanList(object): ''' Each CleanList() instance is rigged so that if exceptions happen in the same CleanList() instance's wrapped class, all objects created from the wrapped classes in the same CleanList() instance will be cleaned (see "Usage" for much better explanation). Usage: >>> cleaner = CleanList() >>> othercleaner = CleanList() >>> F = cleaner(F, F.close) >>> G = cleaner(G, G.close) >>> H = othercleaner(H, H.close) >>> a = F() >>> b = F() >>> c = G() >>> d = H() >>> cleaner.cleanall() cleaner.cleanall() will clean a, b, and c but not d exceptions in (F|G|H).__init__ will trigger cleaner.cleanall() Can be subclassed if you want to override the conditions that determines the triggering of cleanups ''' def wrap(self, cls): ''' Wrapper factory that customizes Wrapper's subclass ''' class Wrapper(cls): ''' Wraps the class' __init__ with cleanup guard. Subclasses cls to simulate cls as close as possible ''' # change __class__ to make printing prettier # e.g. Foo1.__init__ instead of Wrapper.__init__ # probably should be removed # I'm not sure of the side effects of changing __class__ __class__ = cls def __init__(self_in, *args, **kargs): try: sup = super(Wrapper, self_in) ret = sup.__init__(*args, **kargs) except: self.cleanall() raise else: self.add_to_list(cls, self_in) return ret return Wrapper def __init__(self): self.cleaners = {} def __call__(self, cls, cleanup): ''' wraps the class constructor ''' # cleanup, []: # cleanup is the function called to clean # [] is the object list for that `cleanup` function # may not be the best data structure, but it works... self.cleaners[cls] = cleanup, [] return self.wrap(cls) def cleanall(self): ''' clean all objects ''' for cleaner, insts in self.cleaners.values(): for inst in insts: cleaner(inst) def add_to_list(self, cls, inst): ''' add objects to the cleanup list ''' self.cleaners[cls][1].append(inst) class Bar(object): def __init__(self): clist = CleanList() otherclist = CleanList() CleanFoo1 = clist(Foo1, Foo1.close) CleanFoo2 = clist(Foo2, Foo2.close) OtherCleanFoo1 = otherclist(Foo1, Foo1.close) self.a = CleanFoo1('a') self.b = CleanFoo2('b') # self.c should not be close()ed self.c = OtherCleanFoo1('c') self.d = CleanFoo1('d', fail=True) self.e = CleanFoo2('e') class Car(object): def __init__(self): Clean = CleanList() CleanFoo1 = Clean(Foo1, Foo1.close) CleanFoo2 = Clean(Foo2, Foo2.close) self.a = CleanFoo1('a') self.b = CleanFoo2('b') self.c = CleanFoo1('c') self.d = CleanFoo2('d') try: bar = Car() except Exception, e: print e print 'Car() failed to construct' print try: bar = Bar() except Exception, e: print e print 'Bar() failed to construct' From sebastian.schabe at gmx.de Thu Jul 9 13:34:59 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Thu, 09 Jul 2009 19:34:59 +0200 Subject: Concatenating images (numpy arrays), but they look like HSV images Message-ID: <4a5629ba$1@news.fhg.de> Hello everybody, I want to concatenate 2 numpy array which in fact are RGB images: def concat_images(im1,im2): rows1 = im1.shape[0] rows2 = im2.shape[0] if rows1 < rows2: im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1],3), int)), axis=0) elif rows1 > rows2: im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1],3), int)), axis=0) return concatenate((im1,im2), axis=1) It's all working fine, except that the images when showing with pylab are somewhat interpreted as HSV images as it looks. The function zeros() must be responsible for that circumstance, because when the arrays have the same shape and are concatenated they appear as horizontally concatenated images as I expected. Can someone help me with that? Thanks a lot, Basti From lie.1296 at gmail.com Thu Jul 9 13:35:19 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 17:35:19 GMT Subject: property using a classmethod In-Reply-To: <4a55e8e0$0$8041$426a34cc@news.free.fr> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> <4a55e8e0$0$8041$426a34cc@news.free.fr> Message-ID: Bruno Desthuilliers wrote: > Lie Ryan a ?crit : >> Emanuele D'Arrigo wrote: > (snip) >>> Ultimately all I want is a non-callable class-level attribute >>> MyClass.myProperty that gives the result of MyClass.myClassMethod(). >> >> This works like what you seem to want (it's ugly): > > Ugly, indeed. And an extreme case of arbitrary overcomplexification too :-/ > > (snip rube goldberg code) > Can't think of anything simpler than that without meddling with descriptor. I'm not even sure descriptor can help here as it seems descriptor needs an instance? (I've just skimmed it, so I may be wrong) The ugliness of the code hints to two possible reasons: - there should be a better way - if there isn't an easier way, then something is wrong the class' design From duane.kaufman at gmail.com Thu Jul 9 13:36:50 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 10:36:50 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> On Jul 9, 12:18?pm, Tim Harig wrote: > On 2009-07-09, TheSeeker wrote: > > > Specifically, I have a WinForms application I will be wanting to > > automate. Does anyone have some Python examples of driving Microsoft > > UI Automation they could share with me to get me started? The > > structure of the UI automation classes etc. seem quite convoluted, and > > I am having difficulty getting my brain wrapped around it. > > If you find a way to work through the UI using the keyboard (tabs, etc); > then, you can send it keyboard commands using WScript.WshShell.SendKeys(): > > http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx Thanks for the link. Unfortunately, I need to be able to find out the contents of a few text-boxes as well, so SendKeys isn't all I need. Thanks again, Duane From tjreedy at udel.edu Thu Jul 9 13:40:01 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jul 2009 13:40:01 -0400 Subject: can i write a assemly language programs in python In-Reply-To: <4A55F392.3080404@ieee.org> References: <4A55F392.3080404@ieee.org> Message-ID: Dave Angel wrote: > m.reddy prasad reddy wrote: >> can any one tell me how to write assembly language programs in >> python...if >> no is there any other way to write the programs in python >> >> Reddi prasad reddy >> ph.no:09958083797 >> >> > Assembly language is a different programming language than Python. You > can use both in the same process, much in the same way you can use C or > C++ with Python. In fact, some of the system DLL's are written (partly) > in assembler, though mostly in C or C++. It is possible that he meant how to write assembly *with* python. Or how to translate python to assembly. As it turns out, CPython translates Python to byte code and has a dis (assembly) module that produces very nice assembly code ;-) So that is one answer to his question. > You'll need to tell us what your real goal is. Definitely. tjr From lie.1296 at gmail.com Thu Jul 9 13:40:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 09 Jul 2009 17:40:28 GMT Subject: older pythons In-Reply-To: <4a55e1fb$0$1116$4fafbaef@reader3.news.tin.it> References: <4a55ba0c$0$1105$4fafbaef@reader4.news.tin.it> <4a55cb55$0$1116$4fafbaef@reader4.news.tin.it> <4a55d6f7$0$1118$4fafbaef@reader3.news.tin.it> <4a55e1fb$0$1116$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Lie Ryan wrote: >> superpollo wrote: >> >>> Lie Ryan wrote: >>> >>> >>>> AFAIK, no major linux distributions have officially ported to python >>>> 3.x. >>> >>> http://packages.debian.org/experimental/python3.1 >>> >> Note the word "experimental" > > i noticed. isn't experimental official? i thought it was... Official as in "official release"? From Scott.Daniels at Acm.Org Thu Jul 9 13:40:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 09 Jul 2009 10:40:58 -0700 Subject: property using a classmethod In-Reply-To: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> Message-ID: <3dKdndiWOZRutMvXnZ2dnUVZ_gFi4p2d@pdx.net> Emanuele D'Arrigo wrote: > class MyClass(object): > @classmethod > def myClassMethod(self): > print "ham" > myProperty = property(myClassMethod, None, None) > > ... doesn't work and returns a TypeError: .... So, how do I do this? > Ultimately all I want is a non-callable class-level attribute > MyClass.myProperty that gives the result of MyClass.myClassMethod(). properties affect instances, and classes are instances of types. What you want is a new metaclass: class MyType(type): @property def demo(class_): return class_.a + 3 class MyClass(object): __metaclass__ = MyType a = 5 print MyClass.a, MyClass.demo --Scott David Daniels Scott.Daniels at Acm.Org From rhodri at wildebst.demon.co.uk Thu Jul 9 13:41:59 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 09 Jul 2009 18:41:59 +0100 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: On Thu, 09 Jul 2009 17:06:45 +0100, Tom Kermode wrote: > Hi, > > Do you know a good way to avoid running into this problem? It > makes sense to suggest not calling variables the same names as > built-in functions, but that's hard for a new python programmer who > doesn't already know what all the built-in functions are. Over time a > programmer will learn which names to avoid, but it's a bit of a > pitfall early on. It's only really a pitfall if you try to use the built-in after you've redefined it. That's the thing to keep an eye open for. -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Thu Jul 9 13:42:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 09 Jul 2009 19:42:56 +0200 Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: >>>>> Lacrima (L) wrote: >L> Thank you for really useful and detailed explanation. Now I can test >L> my code using Emacs. >L> But I think it works for me in a little bit different way. >L> My file contains only the print 'hello world'. >L> If I have no python shell running, then: >L> a) C-c RET opens *Python Output* buffer without any output and with >L> 'Shell command succeeded with no output' message at the bottom >L> b) C-c C-c opens *Python Output* with appropriate 'hello world' >L> message. >L> Then I run python shell with C-c ! command. And: >L> a) C-c RET opens *Python* buffer and prints 'hello world' in the >L> python shell >L> b) C-c C-c gives the same result. Which version of python-mode.el do you have? (Variable py-version) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From usernet at ilthio.net Thu Jul 9 13:45:13 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 09 Jul 2009 17:45:13 GMT Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On 2009-07-09, DuaneKaufman wrote: > On Jul 9, 12:18?pm, Tim Harig wrote: >> On 2009-07-09, TheSeeker wrote: >> > Specifically, I have a WinForms application I will be wanting to >> > automate. Does anyone have some Python examples of driving Microsoft >> > UI Automation they could share with me to get me started? The >> > structure of the UI automation classes etc. seem quite convoluted, and >> > I am having difficulty getting my brain wrapped around it. >> If you find a way to work through the UI using the keyboard (tabs, etc); >> then, you can send it keyboard commands using WScript.WshShell.SendKeys(): >> http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx > Unfortunately, I need to be able to find out the contents of a few > text-boxes as well, > so SendKeys isn't all I need. Then you will need some way to import your application as an object so that you can gain access to the methods and properties directly. Using IronPython you should be able to access anyting in the GAC. If you register your UI componets then you should be able to access them. If not, or using cpython, you might be able to do something like: http://msdn.microsoft.com/en-us/library/fh1h056h(VS.71).aspx to get a mashalled COM object. From black.linen99 at gmail.com Thu Jul 9 13:46:47 2009 From: black.linen99 at gmail.com (Alex Rosslyn) Date: Thu, 9 Jul 2009 10:46:47 -0700 (PDT) Subject: Colour of output text Message-ID: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Hi, I would like to learn a way of changing the colour of a particular part of the output text. I've tried the following: import os os.system("color 17") print "This should be white on blue" But that command changes the colour of ALL the text and the whole background. What i'm trying to do is simple: Change a single part (A letter, a word, a phrase) of the output text. Is there a way of doing this? Thanks in advance, ~~A.Rosslyn From tjreedy at udel.edu Thu Jul 9 13:49:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jul 2009 13:49:36 -0400 Subject: tkinter problem In-Reply-To: <4a5615ba$0$95523$742ec2ed@news.sonic.net> References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > "Peter Otten" <__peter__ at web.de> wrote in message > news:h3481q$d95$00$1 at news.t-online.com... >> Paul Simon wrote: >> >>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure >>> exactly how I installed Python. Originally I had installed 2.5 from RPM >>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>> something from python.org and installed. Not sure if it was tarball or >>> zip file. >> Zip or tar doesn't matter, you are installing "from source". >> >> Python has to find the necessary include files for tcl/tk. These are in >> separate packages that you have to install before you invoke Python's >> configure script. >> >> I don't know what they are called on your system -- look for tk-dev.rpm, >> tcl-dev.rpm or similar. >> >> You may run into the same problem with other modules like readline. >> >> Peter >> > > Thank you Peter. I understand what you are saying but don't know how to do > it. Although I installed from source, I followed a "cookbook" recipe. > Could you tell me what files to execute, where they might be, and file > arguments? I'm just ignorant, not stupid. ;-). Is there a Mandriva list where you can ask such distribution-specific questions? From usernet at ilthio.net Thu Jul 9 13:53:12 2009 From: usernet at ilthio.net (Tim Harig) Date: Thu, 09 Jul 2009 17:53:12 GMT Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On 2009-07-09, Alex Rosslyn wrote: > I would like to learn a way of changing the colour of a particular > part of the output text. I've tried the following http://catb.org/esr/faqs/smart-questions.html > import os > os.system("color 17") > print "This should be white on blue" I assume that you are on Windows? > But that command changes the colour of ALL the text and the whole > background. What i'm trying to do is simple: Change a single part (A > letter, a word, a phrase) of the output text. If you are on Windows, then there is an API accessing the Windows Console: http://msdn.microsoft.com/en-us/library/ms682010(VS.85).aspx On Unix operating systems this would be done through the curses interface: http://docs.python.org/library/curses.html From robert.kern at gmail.com Thu Jul 9 14:08:08 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 09 Jul 2009 13:08:08 -0500 Subject: Concatenating images (numpy arrays), but they look like HSV images In-Reply-To: <4a5629ba$1@news.fhg.de> References: <4a5629ba$1@news.fhg.de> Message-ID: On 2009-07-09 12:34, Sebastian Schabe wrote: > Hello everybody, > > I want to concatenate 2 numpy array which in fact are RGB images: > > def concat_images(im1,im2): > rows1 = im1.shape[0] > rows2 = im2.shape[0] > > if rows1 < rows2: > im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1],3), int)), axis=0) > elif rows1 > rows2: > im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1],3), int)), axis=0) > > return concatenate((im1,im2), axis=1) > > It's all working fine, except that the images when showing with pylab > are somewhat interpreted as HSV images as it looks. The function zeros() > must be responsible for that circumstance, because when the arrays have > the same shape and are concatenated they appear as horizontally > concatenated images as I expected. > > Can someone help me with that? Ask on the matplotlib mailing list. https://lists.sourceforge.net/lists/listinfo/matplotlib-users Probably, you need to use zeros(..., dtype=uint8). When you use dtype=int, that will result in dtype=int arrays. I suspect that matplotlib is then interpreting that to mean that you want it to treat the input as scalar data (which it will pass through a colormap) rather than an RGB image. -- 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 duane.kaufman at gmail.com Thu Jul 9 14:09:03 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 11:09:03 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 12:45?pm, Tim Harig wrote: > On 2009-07-09, DuaneKaufman wrote: > > > On Jul 9, 12:18?pm, Tim Harig wrote: > >> On 2009-07-09, TheSeeker wrote: > >> > Specifically, I have a WinForms application I will be wanting to > >> > automate. Does anyone have some Python examples of driving Microsoft > >> > UI Automation they could share with me to get me started? The > >> > structure of the UI automation classes etc. seem quite convoluted, and > >> > I am having difficulty getting my brain wrapped around it. > >> If you find a way to work through the UI using the keyboard (tabs, etc); > >> then, you can send it keyboard commands using WScript.WshShell.SendKeys(): > >>http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx > > Unfortunately, I need to be able to find out the contents of a few > > text-boxes as well, > > so SendKeys isn't all I need. > > Then you will need some way to import your application as an object so that > you can gain access to the methods and properties directly. ?Using > IronPython you should be able to access anyting in the GAC. ?If you > register your UI componets then you should be able to access them. ?If not, > or using cpython, you might be able to do something like: > > http://msdn.microsoft.com/en-us/library/fh1h056h(VS.71).aspx > > to get a mashalled COM object. Thanks again Tim. I believe I probably mis-communicated my requirements (or at least, was not explicit enough) The application I wish to interact with is not my own, but an ERP system GUI front-end. With MS utilities like UISpy and the like, I can 'see' the controls in the application, but I do not seem to be able to manipulate them programatically, and I believe it us simply due to my not understanding of the UI Automation API. Hence, my request for example code to boot-strap myself. Thanks, Duane From steve at REMOVE-THIS-cybersource.com.au Thu Jul 9 14:10:16 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 Jul 2009 18:10:16 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> Message-ID: <00720d76$0$9710$c3e8da3@news.astraweb.com> On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: >> persistent idea "out there" that programming is a very accessible >> skill, like cooking or gardening, anyone can do it, and even profit >> from it, monetarily or otherwise, etc., and to some extent I am > > Programming is not like any other human activity. In practice? In principle? Programming in principle is not the same as it is performed in practice. But in either case, programming requires both the logical reasoning of mathematics and the creativity of the arts. Funnily enough, mathematicians will tell you that mathematics requires the same, and so will the best artists. I think mathematicians, engineers, artists, even great chefs, will pour scorn on your claim that programming is not like any other human activity. [...] > He talks about how "when all is said and done, the only thing computers > can do for us is to manipulate symbols and produce results of such > manipulations" and he emphasises the "uninterpreted" nature of > mechanical symbol manipulation, i.e. that the machine is doing it > mindlessly. "Manipulate symbols" is so abstract as to be pointless. By that reasoning, I can build a "computer" consisting of a box open at the top. I represent a symbol by an object (say, a helium-filled balloon, or a stone), instead of a pattern of bits. I manipulate the symbol by holding the object over the box and letting go. If it flies up into the sky, that represents the symbol "Love is War", if it falls into the box, it represents the symbol "Strength is Blue", and if it just floats there, it represents "Cheddar Cheese". This is a deterministic, analog computer which manipulates symbols. Great. And utterly, utterly useless. So what is my computer lacking that real computers have? When you have answered that question, you'll see why Dijkstra's claim is under-specified. > Dijkstra[1]: "It is true that the student that has never manipulated > uninterpreted formulae quickly realizes that he is confronted with > something totally unlike anything he has ever seen before. But > fortunately, the rules of manipulation are in this case so few and > simple that very soon thereafter he makes the exciting discovery that he > is beginning to master the use of a tool that, in all its simplicity, > gives him a power that far surpasses his wildest dreams." [1] What is an uninterpreted formula? If you don't interpret it, how can you distinguish it from random noise? >> but maybe this idea is not entirely true... Maybe, to get past the >> most amateurish level, one has to, one way or another, come >> face-to-face with bits, compilers, algorithms, and all the rest that >> real computer scientists learn about in their formal training... >> >> kj > > If you're never exposed to that constellation of concepts that underpins > "mechanical symbol manipulation" you are adrift in a sea ("c", ha ha) of > abstractions. > > However, if you /are/ exposed to the "so few and simple" rules of > manipulation the gates (no pun intended) to the kingdom are thrown wide. What nonsense. The keys to the kingdom are the abstractions. Here's an exercise for you, if you dare. It's not that difficult to remove the abstraction from integer addition, to explain it in terms of bit manipulation. If you prefer, you can write a Turing Machine instead. Now try doing the same thing for Quicksort. Don't worry, we'll wait. Once you've done that, let's see you implement a practical, scalable, non- toy webserver as a Turing Machine. No, it's not the fundamental operations that open the doors to the kingdom. It's the abstractions. The history of computing is to gain more and more power by leveraging better and better abstractions. > On Jul 8, 9:10 am, Steven D'Aprano cybersource.com.au> wrote: >> There is some evidence that 30-60% of people simply cannot learn to >> program, no matter how you teach them: >> >> http://www.codinghorror.com/blog/archives/000635.html >> http://www.cs.mdx.ac.uk/research/PhDArea/saeed/ ... > I don't buy it: I'm not entirely convinced myself. It's plausible -- not everyone has got what it takes to be musically talented, or a great athlete, or a skilled surgeon, or a charismatic leader. Why should programming be the one high- level intellectual skill that everybody is good at? Seems mighty implausible to me. But I'm not sure that the distribution of skill is *fundamentally* bi- modal. Maybe it is, maybe it isn't. > I believe strongly that any normal person can learn to > program, to manipulate symbols to create formulae that guide the machine > in its uninterpreted symbol manipulation. Most people don't manipulate abstract symbols *at all* -- even something as simple as: "if x is to z as half y is to twice z, then x is the same as ____ y" (fill in the blank) will perplex most people. > I think that "first hump" people can become "second hump" people but > that it requires teaching them the foundations first, not confronting > them with such incredible novelties as "a = b" and saying in effect, > "here you go buddy, sink or swim." The weakness of the paper is not that "a=b" is a novelty, but that it's dangerously familiar. All their students will have already seen "a=b" in the context of many years of mathematics. But that's not what it means in this context. So the risk is, they will be interpreting the symbols "a=b" in terms of mathematical equality, and getting confused. > Quoting Dijkstra again [1]: "Before we part, I would like to invite you > to consider the following way of doing justice to computing's radical > novelty in an introductory programming course. > > "On the one hand, we teach what looks like the predicate calculus, but > we do it very differently from the philosophers. In order to train the > novice programmer in the manipulation of uninterpreted formulae, we > teach it more as boolean algebra, familiarizing the student with all > algebraic properties of the logical connectives. To further sever the > links to intuition, we rename the values {true, false} of the boolean > domain as {black, white}. This is supposed to be a good thing? Oh my. It must be nice in that ivory tower, never having to deal with anyone who hasn't already been winnowed by years of study and self- selection before taking on a comp sci course. I don't think Dijkstra would have taken that suggestion seriously if he had to teach school kids instead of college adults. ... > (Did you read that last paragraph and think, "Well how the heck else are > you supposed to program a computer if not in a computer language?"? If > so, well, that is kind of my point.) No. What I thought was, how the hell are you supposed to manipulate symbols without a language to describe what sort of manipulations you can do? >> > Maybe, to >> > get past the most amateurish level, one has to, one way or another, >> > come face-to-face with bits, compilers, algorithms, and all the rest >> > that real computer scientists learn about in their formal training... >> >> The "No True Scotsman" fallacy. >> >> There's nothing amateurish about building software applications that >> work, with well-designed interfaces and a minimum of bugs, even if >> you've never heard of Turing Machines. >> >> -- >> Steven > > I beg to differ. I recall a conversation with a co-worker who had > "learned" to program using PHP. Another co-worker and I were trying to > convince him that there was a good reason to differentiate between hash > tables and arrays. He didn't even know that they were different > "things". I shall manfully resist the urge to make sarcastic comments about ignorant PHP developers, and instead refer you to my earlier reply to you about cognitive biases. For extra points, can you identify all the fallacious reasoning in that paragraph of yours? > I remember telling him, "between the metal and the desktop there is > nothing but layers of abstraction. We use different names precisely > because of different behaviours." But those abstractions just get in the way, according to you. He should be programming in the bare metal, doing pure symbol manipulation without language. > He made "well-designed interfaces", but "amateurish" is about the nicest > thing I would have called him. Well, perhaps this is a failure of his profession, in that there isn't enough specialisation. If he is skilled with making interfaces, and unskilled with programming the backend, then he should work where his strength lies, instead of being insulted by somebody who has an entirely unjustified opinion about what "real programming" is. If programming is symbol manipulation, then you should remember that the user interface is also symbol manipulation, and it is a MUCH harder problem than databases, sorting, searching, and all the other problems you learn about in academia. The user interface has to communicate over a rich but noisy channel using multiple under-specified protocols to a couple of pounds of meat which processes information using buggy heuristics evolved over millions of years to find the ripe fruit, avoid being eaten, and have sex. If you think getting XML was hard, that's *nothing* compared to user interfaces. The fact that even bad UIs work at all is a credit to the heuristics, bugs and all, in the meat. > As for "a minimum of bugs"... sigh. The "minimum of bugs" is zero, if > you derive your "uninterpreted formulae" /correctly/. And the secret of getting rich on the stock market is to buy low, sell high. Very true, and very pointless. How do you derive the formula correctly? Do you have an algorithm to do so? If so, how do you know that algorithm is correct? > Deriving provably > correct "programs" should be what computer science and computer > education are all about That's impossible. Not just impractical, or hard, or subject to hardware failures, but impossible. I really shouldn't need to raise this to anyone with pretensions of being a Computer Scientist and not just a programmer, but... try proving an arbitrary program will *halt*. If you can't do that, then what makes you think you can prove all useful, necessary programs are correct? > Again with Dijkstra[3]: "The prime paradigma of the pragmatic designer > is known as "poor man's induction", i.e. he believes in his design as > long as "it works", i.e. until faced with evidence to the contrary. (He > will then "fix the design".) Yes, that is the only way it can be. > The scientific designer, however, believes > in his design because he understands why it will work under all > circumstances. Really? Under *all* circumstances? Memory failures? Stray cosmic radiation flipping bits? Noise in the power supply? What's that you say? "That's cheating, the software designer can't be expected to deal with the actual reality of computers!!! We work in an abstract realm where computers never run out of memory, swapping never occurs, and the floating point unit is always correct!" Fail enough. I don't want to make it too hard for you. Okay, so let's compare the mere "pragmatic designer" with his provisional expectation that his code is correct, and Dijkstra's "scientific designer". He understands his code is correct. Wonderful! Er, *how exactly* does he reach that understanding? He reasons about the logic of the program. Great! I can do that. See, here's a proof that binary search is correct. How easy this is. Now try it for some non-trivial piece of code. Say, the Linux kernel. This may take a while... What guarantee do we have that the scientific designer's reasoning was correct? I mean, sure he is convinced he has reasoned correctly, but he would, wouldn't he? If he was unsure, he'd keep reasoning (or ask for help), until he was sure. But being sure doesn't make him correct. It just makes him sure. So how do you verify the verification? And speaking of binary search: [quote] I was shocked to learn that the binary search program that Bentley PROVED CORRECT and SUBSEQUENTLY TESTED [emphasis added] in Chapter 5 of "Programming Pearls" contains a bug. Once I tell you what the it is, you will understand why it escaped detection for two decades. [end quote] http://northernplanets.blogspot.com/2006/07/nearly-all-binary-searches- are-broken.html or http://tinyurl.com/nco6yv Perhaps the "scientific designer" should be a little less arrogant, and take note of the lesson of real science: all knowledge is provisional and subject to correction and falsification. Which really makes the "scientific designer" just a slightly more clever and effective "pragmatic designer", doesn't it? > The transition from pragmatic to scientific design would > indeed be a drastic change within the computer industry." Given Dijkstra's definition of "scientific design", it certainly would. It would be as drastic a change as the discovery of Immovable Objects and Irresistible Forces would be to engineering, or the invention of a Universal Solvent for chemistry, or any other impossibility. > "Obviously no errors" is the goal to strive for, and I am comfortable > calling anyone an amateur who prefers "no obvious errors." It's not a matter of preferring no obvious errors, as understanding the limitations of reality. You simply can't do better than no obvious errors (although of course you can do worse) -- the only question is what you call "obvious". -- Steven From Lacrima.Maxim at gmail.com Thu Jul 9 14:22:46 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Thu, 9 Jul 2009 11:22:46 -0700 (PDT) Subject: Emacs Python-mode. Newbie problem. References: <5332c16d-7e78-4f82-9da9-84f6d704d3df@c9g2000yqm.googlegroups.com> <379be2bd-6d79-498e-a90f-009ff7002559@q11g2000yqi.googlegroups.com> Message-ID: <53aaa579-e69a-43c3-b297-10c6ea6ebf35@b15g2000yqd.googlegroups.com> On Jul 9, 8:42?pm, Piet van Oostrum wrote: > >>>>> Lacrima (L) wrote: > >L> Thank you for really useful and detailed explanation. Now I can test > >L> my code usingEmacs. > >L> But I think it works for me in a little bit different way. > >L> My file contains only the print 'hello world'. > >L> If I have no python shell running, then: > >L> a) C-c RET opens *Python Output* buffer without any output and with > >L> 'Shell command succeeded with no output' message at the bottom > >L> b) C-c C-c opens *Python Output* with appropriate 'hello world' > >L> message. > >L> Then I run python shell with C-c ! command. And: > >L> a) C-c RET opens *Python* buffer and prints 'hello world' in the > >L> python shell > >L> b) C-c C-c gives the same result. > > Which version of python-mode.el do you have? (Variable py-version) > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org py-version's value is "5.1.0" Also maybe the problem is because I use Emacs version 21.4.1. But I can't change it to 22, because I access Emacs via SSH on some shared hosting service. With regards, Max. From ptmcg at austin.rr.com Thu Jul 9 14:29:12 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 9 Jul 2009 11:29:12 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 1:09?pm, DuaneKaufman wrote: > The application I wish to interact with is not my own, but an ERP > system GUI front-end. > I have used pywinauto to drive a Flash game running inside of an Internet Explorer browser - that's pretty GUI! -- Paul From duane.kaufman at gmail.com Thu Jul 9 14:50:45 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 11:50:45 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 1:29?pm, Paul McGuire wrote: > On Jul 9, 1:09?pm, DuaneKaufman wrote: > > > The application I wish to interact with is not my own, but an ERP > > system GUI front-end. > > I have used pywinauto to drive a Flash game running inside of an > Internet Explorer browser - that's pretty GUI! > > -- Paul Hi, Could you share some code examples? Thanks, Duane (duanek (at) chorus (dot) net) From fridrik at pyth.net Thu Jul 9 14:57:47 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Thu, 9 Jul 2009 18:57:47 +0000 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <9DBED98B-8B7A-45DC-9177-D3E97B878AAF@pyth.net> Hi Rhodri, > It's only really a pitfall if you try to use the built-in after you've > redefined it. That's the thing to keep an eye open for. You're right, but in cases where you're editing a codebase which you didn't author entirely by yourself you may not be aware of that. That said, if the codebase you're working on is structured (short, concise methods) you should be working with small, consumable scopes you can inhale in entirety before modifying. Regards, Fri?rik M?r From dns4 at cornell.edu Thu Jul 9 15:08:29 2009 From: dns4 at cornell.edu (David Smith) Date: Thu, 09 Jul 2009 15:08:29 -0400 Subject: tkinter problem In-Reply-To: <4a5615ba$0$95523$742ec2ed@news.sonic.net> References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: Paul Simon wrote: > "Peter Otten" <__peter__ at web.de> wrote in message > news:h3481q$d95$00$1 at news.t-online.com... >> Paul Simon wrote: >> >>> "Chris Rebert" wrote in message >>> news:mailman.2863.1247095339.8015.python-list at python.org... >>> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>>> I have the "tkinter" problem and need some assistance to straighten it >>>> out. >>>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as in >>>>> "step >>>> 1" and cannot import "_tkinter." I do not have that file on my computer, >>>> but >>>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>>> This python stuff is great, but the documentation frequently >>>> feels like it is just a bit out of my grasp. I realize that all of this >>>> is free but I understand the instructions on the web page to repair only >>>> to the >>>> point of confusion. I'm not an expert. How do I modify my python >>>> configuration? Is there a file that needs to be edited? Which setup.py >>>> file >>>> do I use? Make? or python setup.py build and python setup.py install? >>>> Thanks. I appreciate your help. >>> - How did you install Python? >>> - What Linux distro are you using? >>> >>> Cheers, >>> Chris >>> http://blog.rebertia.com >>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not sure >>> exactly how I installed Python. Originally I had installed 2.5 from RPM >>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>> something from python.org and installed. Not sure if it was tarball or >>> zip file. >> Zip or tar doesn't matter, you are installing "from source". >> >> Python has to find the necessary include files for tcl/tk. These are in >> separate packages that you have to install before you invoke Python's >> configure script. >> >> I don't know what they are called on your system -- look for tk-dev.rpm, >> tcl-dev.rpm or similar. >> >> You may run into the same problem with other modules like readline. >> >> Peter >> > > Thank you Peter. I understand what you are saying but don't know how to do > it. Although I installed from source, I followed a "cookbook" recipe. > Could you tell me what files to execute, where they might be, and file > arguments? I'm just ignorant, not stupid. ;-). > > Paul > > Just install the tkinter package from the Mandriva Linux Control Center's Software Management system. I just did it, doing a search for tkinter brought it right up. All done. --David From ethan at stoneleaf.us Thu Jul 9 16:06:16 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 09 Jul 2009 13:06:16 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <4A564DB8.3050209@stoneleaf.us> kj wrote: > > My scientific code is jam-packed with assertions. I can't count > the number of times that one such lowly assertion saved me from a > silent but potentially disastrous bug. Now imagine that asserts had been disabled for that run... The issue is not "should you validate your inputs", the issue is "do you want your validation able to be turned off?" ~Ethan~ From sberry2a at gmail.com Thu Jul 9 16:16:38 2009 From: sberry2a at gmail.com (Sean) Date: Thu, 9 Jul 2009 13:16:38 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list Message-ID: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> I have a huge list, 10,000,000+ items. Each item is a dictionary with fields used to sort the list. When I have completed sorting I want to grab a page of items, say 1,000 of them which I do easily by using list_data[x:x+1000] Now I want to add an additional key/value pair to each dictionary in the list, incrementing them by 1 each time. So, if I grabbed page 2 of the list I would get: [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': 1002}, ...] Any way to do that with list comprehension? Any other good way to do it besides iterating over the list? Thanks From sberry2a at gmail.com Thu Jul 9 16:33:01 2009 From: sberry2a at gmail.com (Sean) Date: Thu, 9 Jul 2009 13:33:01 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: On Jul 9, 1:16?pm, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? > > Thanks I was able to do this by doing the following: page_data = [[start_position + 1 + n, x] for n, x in enumerate (page_data)] However, this creates a list of lists, each containing an int and a dictionary. I wanted to add it directly to the dictionary. I can add the key position to the dictionary when the data is created, but how would I assign the value in a list comprehension? From tn.pablo at gmail.com Thu Jul 9 16:38:16 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 9 Jul 2009 15:38:16 -0500 Subject: Best way to add a "position" value to each item in a list In-Reply-To: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: Howdy, On Thu, Jul 9, 2009 at 15:16, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > I don't get it, what do you increment by one, the value of a given key or the number of key/value pairs? Also, if the 'position' key is the index of the item in the list, then I don't understand what you mean by 'page'. Could you tell us about the structure of these dictionaries? > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? > > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > -- Pablo Torres N. From ptmcg at austin.rr.com Thu Jul 9 16:41:17 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 9 Jul 2009 13:41:17 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: On Jul 9, 1:50?pm, DuaneKaufman wrote: > > I have used pywinauto to drive a Flash game running inside of an > > Internet Explorer browser - that's pretty GUI! > > > -- Paul > > Hi, > > Could you share some code examples? > > Thanks, > Duane (duanek (at) chorus (dot) net) I just went on a brief fishing expedition through two disk backups, and no luck. I guess it's been a while since I worked on this. The work I did was entirely graphical, which is to say, my script interacted with the Flash program by using PIL to take image snapshots of the window, and then sifting through the bitmap looking for the status of a "time remaining" thermometer-style gauge in the game. Then the script could click on X-Y coordinates within the window, which would get picked up by the Flash game, and the script would monitor the "time remaining" gauge some more, and so on. I'm not sure how well pywinauto would work in allowing you to access controls such as textboxes within a form. I remember that I had to access the IE window using a caption name, and then found the embedded Flash program as an embedded control of some sort, again, I probably needed to indicate that it was some sort of "Adobe.FlashWidget1" object or something. I may have another, older disk backup at home, I can look for it later this evening. -- Paul From malaclypse2 at gmail.com Thu Jul 9 16:49:22 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 9 Jul 2009 16:49:22 -0400 Subject: Best way to add a "position" value to each item in a list In-Reply-To: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: <16651e80907091349x409158b7sf62f7bbd0fb7c1a6@mail.gmail.com> On Thu, Jul 9, 2009 at 4:16 PM, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? Normally you wouldn't mutate the items in a list with a list comprehension. Instead, you would use a for loop, like this: for idx,item in enumerate(my_list_of_dicts): item['position'] = idx Is there a particular reason you want to do this with a list comprehension? Using a list comp means you're going to create an extra copy of your 10 million item list, just so you can add a key to each member, then (probably) throw away the original list. It doesn't seem like the right tool for the job here. -- Jerry From jcd at sdf.lonestar.org Thu Jul 9 16:56:44 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Thu, 09 Jul 2009 16:56:44 -0400 Subject: tough-to-explain Python In-Reply-To: <00720d76$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <1247173004.20204.0.camel@aalcdl07> On Thu, 2009-07-09 at 18:10 +0000, Steven D'Aprano wrote: > If programming is symbol manipulation, then you should remember that > the > user interface is also symbol manipulation, and it is a MUCH harder > problem than databases, sorting, searching, and all the other > problems > you learn about in academia. The user interface has to communicate > over a > rich but noisy channel using multiple under-specified protocols to a > couple of pounds of meat which processes information using buggy > heuristics evolved over millions of years to find the ripe fruit, > avoid > being eaten, and have sex. If you think getting XML was hard, that's > *nothing* compared to user interfaces. +1 QOTW! QOTM, even! From sberry2a at gmail.com Thu Jul 9 17:00:27 2009 From: sberry2a at gmail.com (Sean) Date: Thu, 9 Jul 2009 14:00:27 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: <58d94d82-3482-4429-9632-3255d2a85295@12g2000pri.googlegroups.com> On Jul 9, 1:16?pm, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? > > Thanks I was able to do this by doing the following: page_data = [[start_position + 1 + n, x] for n, x in enumerate (page_data)] However, this creates a list of lists, each containing an int and a dictionary. I wanted to add it directly to the dictionary. I can add the key position to the dictionary when the data is created, but how would I assign the value in a list comprehension? From pavlovevidence at gmail.com Thu Jul 9 17:03:13 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 9 Jul 2009 14:03:13 -0700 (PDT) Subject: Best way to add a "position" value to each item in a list References: <929bf3f5-4413-4c46-8883-506322ce0b04@j9g2000prh.googlegroups.com> Message-ID: <1bf2ab02-2a84-41a2-8156-9ec3ac7301c4@t13g2000yqt.googlegroups.com> On Jul 9, 1:16?pm, Sean wrote: > I have a huge list, 10,000,000+ items. ?Each item is a dictionary with > fields used to sort the list. ?When I have completed sorting I want to > grab a page of items, say 1,000 of them which I do easily by using > list_data[x:x+1000] > > Now I want to add an additional key/value pair to each dictionary in > the list, incrementing them by 1 each time. ?So, if I grabbed page 2 > of the list I would get: > > [{'a':'a', 'b':'b', 'position':1001}, {'c':'c', 'd':'d', 'position': > 1002}, ...] > > Any way to do that with list comprehension? ?Any other good way to do > it besides iterating over the list? Not really, but if you want to avoid the delay in setting the dictionary elements (I'm guessing that is why you don't want to iterate over the list--and parenthetically it's not iterating over the list but adding dictionary items that is driving the time, and that cost is unavoidable), you should consider adding the position elements on demand. That is, instead of running a big loop once to add position to all ten million elements, just run the loop on individual pages. def get_page(start,end): page = list_data[start:end] for i,item in enumerate(page): page['position'] = start+i return page That might not work depending on what you are doing but you should consider it. Carl Banks From torriem at gmail.com Thu Jul 9 17:09:13 2009 From: torriem at gmail.com (Michael Torrie) Date: Thu, 09 Jul 2009 15:09:13 -0600 Subject: Examples of Python driven Microsoft UI Automation wanted In-Reply-To: References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: <4A565C79.4020501@gmail.com> DuaneKaufman wrote: > With MS utilities like UISpy and the like, I can 'see' the controls in > the application, but I > do not seem to be able to manipulate them programatically, and I > believe it us simply due > to my not understanding of the UI Automation API. You're probably better off using a free app like AutoIt3 rather than roll your own. AutoIt's scripting language isn't as nice as Python, but it does the job and it turing-complete. http://www.autoitscript.com/autoit3/ From thebrasse at gmail.com Thu Jul 9 17:10:02 2009 From: thebrasse at gmail.com (=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=) Date: Thu, 9 Jul 2009 14:10:02 -0700 (PDT) Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> Message-ID: <42fbcee1-61b2-4ba8-a50a-39c9c0d2fe9c@k19g2000yqn.googlegroups.com> On Jul 9, 7:30?pm, Lie Ryan wrote: > brasse wrote: > > Hello! > > I have been thinking about how write exception safe constructors in > > Python. By exception safe I mean a constructor that does not leak > > resources when an exception is raised within it. The following is an > > example of one possible way to do it: > > First, your automatic cleaning Bar() silently pass an unitialized Bar() > into the calling code. When a code fails, it should fail as loud as > possible. Bar() should raises an Exception and the calling code should > turn into something like: > > try: > ? ? bar = Bar() > except Exception, e: > ? ? print 'bar failed to construct' > I know, I missed that little detail. :-) > And about the cleaning up, how about: > > class CleanWrap(object): > ? ? def __init__(self, cls, cleanup): > ? ? ? ? self.cls = cls > ? ? ? ? self.cleanup = cleanup > ? ? def __call__(self, *args, **kargs): > ? ? ? ? try: > ? ? ? ? ? ? return self.cls(*args, **kargs) > ? ? ? ? except: > ? ? ? ? ? ? self.cleanup() > ? ? ? ? ? ? raise > > class Bar(object): > ? ? def __init__(self): > ? ? ? ? CleanWrappedFoo = CleanWrap(Foo, self.close) > ? ? ? ? self.a = CleanWrappedFoo('a') > ? ? ? ? self.b = CleanWrappedFoo('b', fail=True) > ? ? def close(self): > ? ? ? ? if hasattr(self, 'a'): > ? ? ? ? ? ? self.a.close() > ? ? ? ? if hasattr(self, 'b'): > ? ? ? ? ? ? self.b.close() > I think this example adds about as much overhead as my original example with the try block. Ideally I would like to be able to write my classes in the most straight forward manner possible. In my mind that would be something like a naive constructor and a close/dispose method: def __init__(self): self.a = Foo() self.b = Bar() def close(self): self.a.close() self.b.close() See my other post (the one with the decorator named safe) in this thread for an experiment I thought was promising for a while. However there are some some use cases it has to cover to be useful: (1) References to objects owned by someone else def __init__(self, some_resource): self.not_my_responsibility = some_resource self.a = Foo() # If Foo.__init__ raises an exception self.not_my responsibility should not be closed. (2) Local objects def __init__(self): x = Bar() self.a = Foo(x) # If Foo.__init__ raises an exception x should also be closed. So far I have not been able to find any good solutions for (at least) case 1. I'll give it a few more days, but I'm not that hopeful. I think that I will end up writing my classes very much like Bar in my original post. If someone has any ideas for how to solve my two use cases above with my decorator based approach I would really like to hear them! :.:: mattias From torriem at gmail.com Thu Jul 9 17:12:40 2009 From: torriem at gmail.com (Michael Torrie) Date: Thu, 09 Jul 2009 15:12:40 -0600 Subject: Examples of Python driven Microsoft UI Automation wanted In-Reply-To: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: <4A565D48.2060608@gmail.com> TheSeeker wrote: > Alternatives to Microsoft's UI Automation are welcome too, but I have > tried using winguiauto and watsup (along with AutoIt), and there seems > to be severe limitations when using these tools with WinForm > applications. http://www.autoitscript.com/forum/index.php?showtopic=96752 -- looks like recent betas have much better support for WinForms, at least from the change list. From helvinlui at gmail.com Thu Jul 9 17:19:23 2009 From: helvinlui at gmail.com (Helvin) Date: Thu, 9 Jul 2009 14:19:23 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> Message-ID: <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> On Jul 10, 3:54?am, Robert Kern wrote: > On 2009-07-09 01:27, Helvin wrote: > > > > > > > On Jul 9, 11:29 am, Robert Kern ?wrote: > >> On 2009-07-08 18:10, Helvin wrote: > > >>> Thanks for the fast replies! I will look into how to use VTK now. > >>> Where would I find VTK's explicit support for PyQt? > >> Wrapping/Python/vtk/qt4/ in the VTK sources. > > >>> Because I have installed VTK (using its installer) and pyVTK (using > >>> its setup.py file), but how do I actually use it in my code? According > >>> to:http://www.nabble.com/embedded-VTK-window-in-PyQt-application-td23521..., > >>> I have tried 'import vtk', but python can't find the vtk module. > >> Then you have not installed VTK's Python bindings correctly. Note that pyVTK is > >> just a library for manipulating VTK files. The VTK Python bindings are part of > >> VTK's distribution itself. Exactly how did you install VTK? Did you compile it > >> yourself? > > > You mean, when I download VTK, the VTK Python bindings come with it? > > I downloaded VTK from:http://vtk.org/files/release/5.4/ > > Exactly which file did you download? I don't think the vtk-5.4.x-win32.exe > installers have the Python bindings. You will need to build VTK from sources. > > -- > 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 Yes, I think you are right. I did initially use the windows installer. I didn't know that it did't have python bindings. I thought that if I had pyVTK, then it would work. But it didn't. I am now building VTK with sources according to this EXCELLENT tutorial that I have somehow come across, at: http://www-viz.tamu.edu/courses/viza658/08spring/tutorials/WinVTKInstall.html It's for Windows XP, and I am on Vista, but I hope it will work. I have worked my way through most of it now. Thanks Robert! Helvin From mail.to.daniel.platz at googlemail.com Thu Jul 9 18:03:36 2009 From: mail.to.daniel.platz at googlemail.com (tt-industries) Date: Thu, 9 Jul 2009 15:03:36 -0700 (PDT) Subject: Package for fast plotting of many data points in Python? Message-ID: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Hi, I am programming a oscilloscope module in Python. For this reason, I want to plot very many data points as fast as possible. This can be more than 100 000 at once. So far I have been using the ploting module of wxPython. However, it becomes unstable for more than 25000 points. Can someone recommend me a faster plotting library? It would be really cool if one could embed this in wxPython. If someone has an idea I would be very glad about answer. With kind regards, Daniel From duane.kaufman at gmail.com Thu Jul 9 18:10:10 2009 From: duane.kaufman at gmail.com (DuaneKaufman) Date: Thu, 9 Jul 2009 15:10:10 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: <4be34b72-0961-4031-b943-82c9e7c95577@24g2000yqm.googlegroups.com> On Jul 9, 4:12?pm, Michael Torrie wrote: > TheSeeker wrote: > > Alternatives to Microsoft's UI Automation are welcome too, but I have > > tried using winguiauto and watsup (along with AutoIt), and there seems > > to be severe limitations when using these tools with WinForm > > applications. > > http://www.autoitscript.com/forum/index.php?showtopic=96752-- looks > like recent betas have much better support for WinForms, at least from > the change list. Hi, Thanks for the information. Boy, you are away from a utility for a couple of weeks, and it starts growing things one needs! Unfortunately, while it looks like AutoIt is gaining the ability to 'see' WinForms controls, this beta doesn't seem to be able to retrieve the text say, in a TextBox. Darn. Thanks, Duane From stef.mientki at gmail.com Thu Jul 9 18:26:09 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Fri, 10 Jul 2009 00:26:09 +0200 Subject: Package for fast plotting of many data points in Python? In-Reply-To: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <4A566E81.5040503@gmail.com> tt-industries wrote: > Hi, > > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. At once is impossible ;-) > So far I have been using the ploting module > of wxPython. which plotting module ? > However, it becomes unstable for more than 25000 points. > again what do you mean: 25000 points per ? 25000 points on a 1600 pixel width screen ? Please try to be somewhat more specific. cheers, Stef > Can someone recommend me a faster plotting library? It would be really > cool if one could embed this in wxPython. If someone has an idea I > would be very glad about answer. > > With kind regards, > > Daniel > From tn.pablo at gmail.com Thu Jul 9 18:49:48 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 9 Jul 2009 17:49:48 -0500 Subject: can i write a assemly language programs in python In-Reply-To: References: <4A55F392.3080404@ieee.org> Message-ID: Or maybe he meant if you can have both Python and assembler code in the same source file, but I haven't heard of it. If what you are trying to do is write a faster version of some part of your code, try SWIG: http://www.swig.org/ Playing the guessing game is a time sink, and since nobody has done it so far, I will: OP, please read http://www.mikeash.com/getting_answers.html and http://catb.org/esr/faqs/smart-questions.html On Thu, Jul 9, 2009 at 12:40, Terry Reedy wrote: > Dave Angel wrote: >> >> m.reddy prasad reddy wrote: >>> >>> can any one tell me how to write assembly language programs in >>> python...if >>> no is there any other way to write the programs in python >>> >>> Reddi prasad reddy >>> ph.no:09958083797 >>> >>> >> >> Assembly language is a different programming language than Python. ?You >> can use both in the same process, much in the same way you can use C or C++ >> with Python. ?In fact, some of the system DLL's are written (partly) in >> assembler, though mostly in C or C++. > > It is possible that he meant how to write assembly *with* python. > Or how to translate python to assembly. > > As it turns out, CPython translates Python to byte code and has a dis > (assembly) module that produces very nice assembly code ;-) > So that is one answer to his question. > >> You'll need to tell us what your real goal is. > > Definitely. > > tjr > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Pablo Torres N. From milesck at umich.edu Thu Jul 9 19:28:11 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Thu, 9 Jul 2009 19:28:11 -0400 Subject: Catching control-C In-Reply-To: References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: <4F69EE56-6911-400D-B324-4F6CE3E789CF@umich.edu> On Jul 9, 2009, at 9:20 AM, Lie Ryan wrote: > Michael Mossey wrote: >> I want to understand better what the "secret" is to responding to a >> ctrl-C in any shape or form. > > Are you asking: "when would the python interpreter process > KeyboardInterrupt?" > ... > In single threaded python program, the currently running thread is > always the main thread (which can handle KeyboardInterrupt). I believe > SIGINT is checked at every ticks. But SIGINT cannot interrupt atomic > operations (i.e. it cannot interrupt long operations that takes a > single > tick). Some otherwise atomic single-bytecode operations (like large integer arithmetic) do manual checks for whether signals were raised (though that won't help at all if the operation isn't on the main thread). > I believe a tick in python is equivalent to a single bytecode, but > please correct me if I'm wrong. Not all opcodes qualify as a tick. In general, those opcodes that cause control to remain in the eval loop (and not make calls to other Python or C functions) don't qualify as ticks (but there are exceptions, e.g. so that while True: pass is interruptible). In Python/ceval.c: PyEval_EvalFrameEx(), those opcodes that don't end in goto fast_next_opcode are ticks. Please correct me if _I'm_ wrong! :) -Miles From sajmikins at gmail.com Thu Jul 9 19:38:18 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 9 Jul 2009 19:38:18 -0400 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: <50f98a4c0907091638u6ad23eaeo779a333a46860dc8@mail.gmail.com> On Thu, Jul 9, 2009 at 3:17 AM, m.reddy prasad reddy wrote: > > can any one tell me how to write assembly language programs in python...if > no is there any other way to write the programs in python > > Reddi prasad reddy > ph.no:09958083797 > > -- > http://mail.python.org/mailman/listinfo/python-list > > Examine CorePy http://www.corepy.org "CorePy is a complete system for developing machine-level programs in Python. CorePy lets developers build and execute assembly-level programs interactively from the Python command prompt, embed them directly in Python applications, or export them to standard assembly languages." HTH, ~Simon From xavier.benech at t-immersion.com Thu Jul 9 19:39:07 2009 From: xavier.benech at t-immersion.com (=?ISO-8859-1?Q?Xavier_B=E9nech?=) Date: Fri, 10 Jul 2009 01:39:07 +0200 Subject: sys.exit() and PyRun_SimpleFileExFlags() Message-ID: <4A567F9B.80108@t-immersion.com> Hi, There is a behaviour I do not understand of PyRun_SimpleFileExFlags(), normally when it executes a python script containing a sys.exit(), it results by ending the calling application. I have got this behaviour with PyRun_SimpleFileExFlags() when I call it from the main thread of a GUI application (so I use PyRun_FileExFlags() in this case). But in another application when it is called (PyRun_SimpleFileExFlags())) from a working thread (not the main one) of a console application, everything just go fine without exiting the application nor the thread. It returns the exit code specified in sys.exit() just fine. Last things, I am working on Windows. And for the story, I started to make a small function to wrap some embedded python script call using the simple way with PyRun_SimpleFileExFlags() for the second application, but when I integrate it in the first one (with GUI) got some problems (:)) so I redo things with a cleaner PyRun_FileExFlags() call. So, my question is: why do I have different behaviour of PyRun_SimpleFileExFlags() in this two cases? Regards, Xavier Benech. From bearophileHUGS at lycos.com Thu Jul 9 19:49:10 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 9 Jul 2009 16:49:10 -0700 (PDT) Subject: can i write a assemly language programs in python References: Message-ID: <4d8af709-e4c1-431e-9a43-7f0f0dd8f1a6@j32g2000yqh.googlegroups.com> Simon Forman: > Examine CorePyhttp://www.corepy.org > > "CorePy is a complete system for developing machine-level programs in > Python. CorePy lets developers build and execute assembly-level > programs interactively from the Python command prompt, embed them > directly in Python applications, or export them to standard assembly > languages." I have never used CorePy yet, but it's an awesome project ^_^ With some care and work even Python programs can get fast enough :-) Bye, bearophile From clp2 at rebertia.com Thu Jul 9 19:54:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 16:54:23 -0700 Subject: Package for fast plotting of many data points in Python? In-Reply-To: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <50697b2c0907091654l188ea9c1v6b568fdf1dc5e89@mail.gmail.com> On Thu, Jul 9, 2009 at 3:03 PM, tt-industries wrote: > Hi, > > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. So far I have been using the ploting module > of wxPython. However, it becomes unstable for more than 25000 points. > Can someone recommend me a faster plotting library? It would be really > cool if one could embed this in wxPython. If someone has an idea I > would be very glad about answer. Don't know how fast it is or how well it integrates, but matplotlib is probably worth considering: http://matplotlib.sourceforge.net/ Cheers, Chris -- http://blog.rebertia.com From sajmikins at gmail.com Thu Jul 9 20:01:47 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 9 Jul 2009 20:01:47 -0400 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: <50f98a4c0907091701q660fec7cxfe75315a4e72575b@mail.gmail.com> On Thu, Jul 9, 2009 at 9:42 AM, Nick wrote: > ? ?fields = line.split() > ? ?for i in range(len(fields)): > ? ? ? ?fields[i] = float(fields[i]) instead of the above code you could say: fields = [float(n) for n in in line.split()] Have fun getting back into python! :] (A lot has changed in the last few years) HTH, ~Simon From http Thu Jul 9 20:22:06 2009 From: http (Paul Rubin) Date: 09 Jul 2009 17:22:06 -0700 Subject: gett error message: "TypeError: 'int' object is not callable" References: Message-ID: <7xab3d9ykx.fsf@ruckus.brouhaha.com> Nick writes: > text = file.readlines() > len = len(text) > fields = text[1].split() Is that intended to split the first line of the file? Remember that arrays in python begin at index 0. From marty.musatov at gmail.com Thu Jul 9 21:03:31 2009 From: marty.musatov at gmail.com (Musatov) Date: Thu, 9 Jul 2009 18:03:31 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google Message-ID: Los Angeles (AP) --MeAmI.org now has users in 50 countries following its adopted use in Pakistan. The search engine has grown in popularity 10,000 fold following its Beta test launch three months ago in April, 2009. Supporters of the site claim it is better than rival Google upon which platform it is based. Controversy arose after MeAmI.org search code allowed users to search other users Google results with no advertising. "It is truly an innovative thing we are doing," said Founder and CEO, Martin Musatov. "Letting users search the results of other Google users immediately results in a level of accuracy and relevance above and beyond Google." Google changed their API following the launch or MeAmI.org and explored the possibility of blocking site access from MeAmI.org to Google search results but was unable to do so. Critics of MeAmI.org say what it is doing is tantamount to intellectual theft. When asked about this topper Musatov exclaimed, "The Internet was made for people, not companies." An analyst at Goldman Sachs says, requesting to remain anonymous, "MeAmI.org has a strong presence in promoting itself as a vehicle for global activism and to tell you the truth, this makes it much more likely an acquisition target than potential intellectual property violator." Google could not be reached for comment. From ldo at geek-central.gen.new_zealand Thu Jul 9 21:05:01 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 10 Jul 2009 13:05:01 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , kj wrote: > .., Lundh writes: > > Assignment statements modify namespaces, not objects. >>> a = [3] >>> b = a These may indeed modify a namespace, not any object. However: >>> a[:] = [4] >>> a [4] >>> b [4] What change has happened to the namespace in this case? None. Yet an object has changed. From ben+python at benfinney.id.au Thu Jul 9 21:06:34 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jul 2009 11:06:34 +1000 Subject: Running a script to build docs from setup.py References: <20090709173343.1784710d@realh.co.uk> Message-ID: <87fxd5ny79.fsf@benfinney.id.au> Tony Houghton writes: > I've looked through the manual but I can't find any hooks in distutils > for generating files at install time other than extension modules and > .pyc files. Should I just run the script from somewhere in my setup.py > before calling distutils' setup function? Indirectly related: Ian Bicking's article on using Python's ?setup.py? as a ?Makefile? replacement: If one is writing a ?setup.py? anyway, I think it makes sense to use that as the build program for the whole project if possible. Good hunting! -- \ ?I have an answering machine in my car. It says, ?I'm home now. | `\ But leave a message and I'll call when I'm out.?? ?Steven Wright | _o__) | Ben Finney From scriber17 at aol.com Thu Jul 9 21:07:55 2009 From: scriber17 at aol.com (The Masked Logician) Date: Thu, 9 Jul 2009 18:07:55 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <8bbed30b-53e5-4558-8e50-fbc395221915@y10g2000prf.googlegroups.com> On Jul 9, 6:03?pm, Musatov wrote: > Los Angeles (AP) -- MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search > the results of other Google users immediately results in a level of > accuracy and relevance above and beyond Google." Google changed their > API following the launch or MeAmI.org and explored the possibility of > blocking site access from MeAmI.org to Google search results but was > unable to do so. Critics of MeAmI.org say what it is doing is > tantamount to intellectual theft. When asked about this topper Musatov > exclaimed, "The Internet was made for people, not companies." An > analyst at Goldman Sachs says, requesting to remain anonymous, > "MeAmI.org has a strong presence in promoting itself as a vehicle for > global activism and to tell you the truth, this makes it much more > likely an acquisition target than potential intellectual property > violator." Google could not be reached for comment. Wow. From ben+python at benfinney.id.au Thu Jul 9 21:11:26 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jul 2009 11:11:26 +1000 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <87bpntnxz5.fsf@benfinney.id.au> tt-industries writes: > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. I think you will find good results using Numpy for your arrays of data and Matplotlib to plot those arrays . -- \ ?Ubi dubium, ibi libertas.? (?Where there is doubt, there is | `\ freedom.?) | _o__) | Ben Finney From roy at panix.com Thu Jul 9 21:16:09 2009 From: roy at panix.com (Roy Smith) Date: Thu, 09 Jul 2009 21:16:09 -0400 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: In article <0734dc45-d8a0-4f28-b945-f9e179f30f5c at h11g2000yqb.googlegroups.com>, tt-industries wrote: > I am programming a oscilloscope module in Python. Sigh. I guess I'm showing my age, but I still can't get used to the idea that the right tool to build an oscilloscope is no longer a soldering iron. From bcb at undisclosedlocation.net Thu Jul 9 21:18:23 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Thu, 9 Jul 2009 20:18:23 -0500 Subject: AP -- MeAmI.org Paces Google References: Message-ID: "Musatov" wrote in message news:fe9969b0-3c6a-49f7-8f38-0ed89dc9ad42 at x6g2000prc.googlegroups.com... [snip-a-rooney] > violator." Google could not be reached for comment. Possibly because they were laughing so hard they couldn't hear their cell phones ringing ...? From scriber17 at aol.com Thu Jul 9 21:29:58 2009 From: scriber17 at aol.com (The Masked Logician) Date: Thu, 9 Jul 2009 18:29:58 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <7657db2e-71b3-4a49-b70f-127e362824a2@x5g2000prf.googlegroups.com> On Jul 9, 6:18?pm, "Bruce C. Baker" wrote: > "Musatov" wrote in message > > news:fe9969b0-3c6a-49f7-8f38-0ed89dc9ad42 at x6g2000prc.googlegroups.com... > > [snip-a-rooney] > > > violator." Google could not be reached for comment. > > Possibly because they were laughing so hard they couldn't hear their cell > phones ringing ...? Thank you for your comment Bruce C. Baker. Are you aware you're dead? http://wcc.dli.mt.gov/B/BAKER_BRUCE_SJ_DECISION.htm http://MeAmI.org "The Best Search Engine for Fat Dead Guys named Bruce" From matt.dubins at sympatico.ca Thu Jul 9 21:36:05 2009 From: matt.dubins at sympatico.ca (inkhorn) Date: Thu, 9 Jul 2009 18:36:05 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? Message-ID: Hi all, For one of my projects, I came across the need to check if one of many items from a list of strings could be found in a long string. I came up with a pretty quick helper function to check this, but I just want to find out if there's something a little more elegant than what I've cooked up. The helper function follows: def list_items_in_string(list_items, string): for item in list_items: if item in string: return True return False So if you define a list x = ['Blah','Yadda','Hoohoo'] and a string y = 'Yip yip yippee Blah' and you run list_items_in_string(x, y), it should return True. Any ideas how to make that function look nicer? :) Matt Dubins From david250 at videotron.ca Thu Jul 9 21:41:53 2009 From: david250 at videotron.ca (David Bernier) Date: Thu, 09 Jul 2009 21:41:53 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search [ snip ] I wonder what _AP_ means ... David Bernier From clp2 at rebertia.com Thu Jul 9 21:43:31 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 18:43:31 -0700 Subject: How to check if any item from a list of strings is in a big string? In-Reply-To: References: Message-ID: <50697b2c0907091843m174a065dx86e5ca6c21776a63@mail.gmail.com> On Thu, Jul 9, 2009 at 6:36 PM, inkhorn wrote: > Hi all, > > For one of my projects, I came across the need to check if one of many > items from a list of strings could be found in a long string. ?I came > up with a pretty quick helper function to check this, but I just want > to find out if there's something a little more elegant than what I've > cooked up. ?The helper function follows: > > def list_items_in_string(list_items, string): > ? ?for item in list_items: > ? ? ? ?if item in string: > ? ? ? ? ? ?return True > ? ?return False > > So if you define a list x = ['Blah','Yadda','Hoohoo'] and a string y = > 'Yip yip yippee Blah' and you run list_items_in_string(x, y), it > should return True. > > Any ideas how to make that function look nicer? :) any(substr in y for substr in x) Note that any() was added in Python 2.5 Cheers, Chris -- http://blog.rebertia.com From fridrik at pyth.net Thu Jul 9 21:45:01 2009 From: fridrik at pyth.net (=?ISO-8859-1?Q?Fri=F0rik_M=E1r_J=F3nsson?=) Date: Fri, 10 Jul 2009 01:45:01 +0000 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: <8BCC0DC4-7ADB-496C-AE87-5C419656CD73@pyth.net> I'll be the first to admit it. The point of writing a fake story by Associated Press and publishing it on a programming mailing list is totally beyond me. Confoundedly yours, Fri?rik M?r From scriber17 at aol.com Thu Jul 9 21:48:17 2009 From: scriber17 at aol.com (The Masked Logician) Date: Thu, 9 Jul 2009 18:48:17 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <90319430-9fdf-4286-b19a-8b1a852f75a9@g7g2000prg.googlegroups.com> On Jul 9, 6:41?pm, David Bernier wrote: > Musatov wrote: > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > its adopted use in Pakistan. ?The search engine has grown in > > popularity 10,000 fold following its Beta test launch three months ago > > in April, 2009. Supporters of the site claim it is better than rival > > Google upon which platform it is based. Controversy arose after > > MeAmI.org search code allowed users to search other users Google > > results with no advertising. "It is truly an innovative thing we are > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > [ snip ] > > I wonder what _AP_ means ... > > David Bernier 1. Dec 16, 2008 ... "Staffers recognize the tough times, but they also understand that quality journalism at AP means attracting and retaining the best ... www.google.com/.../afp/.../ALeqM5gSCzjMBFqxtXCVC-am3ve59koBYQ 2. Business Accounting and Bookkeeping question: What does AP mean? many things inc. associated press. wiki.answers.com/Q/What_does_AP_mean Found: http://www.meami.org/?cx=000961116824240632825%3A5n3yth9xwbo&cof=FORID%3A9%3B+NB%3A1&ie=UTF-8&q=AP+means#943 3. David Bernier - Wikipedia, the free encyclopedia David Enrique Bernier also known as "Kike" (born January 21, 1977) former Secretary of Sports and Recreation of Puerto Rico as well as Executive Director of ... en.wikipedia.org/wiki/David_Bernier Found: http://www.meami.org/?cx=000961116824240632825%3A5n3yth9xwbo&cof=FORID%3A9%3B+NB%3A1&ie=UTF-8&q=David+Bernier#913 http://MeAmI.org "Use it to fend off assholes." From aahz at pythoncraft.com Thu Jul 9 22:00:21 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 Jul 2009 19:00:21 -0700 Subject: Opening a SQLite database in readonly mode References: <79990c6b0907060505v73703134wd02cd15ba0d3da66@mail.gmail.com> Message-ID: In article , Joshua Kugler wrote: >Joshua Kugler wrote: >> >> Sorry about that...since pysqlite and APSW are both discusses on the >> pysqlite list, I had made an incorrect assumption. Oops. > >"are both discusses?" Yeef, I must have been out of it. Discussed, thank >you. :) Who's Yeef? ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From jason at powerpull.net Thu Jul 9 22:06:35 2009 From: jason at powerpull.net (Jason S. Friedman) Date: Fri, 10 Jul 2009 02:06:35 +0000 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: <4A56A22B.5050204@powerpull.net> Hello, I administer the Informatica ETL tool at my company. Part of that role involves creating and enforcing standards. I want the Informatica developers to add comments to certain key objects and I want to be able to verify (in an automated fashion) that they have done so. I cannot merely check for non-emptiness; that is trivial to circumvent. On the other hand, I probably do not need to be able to catch developers who are determined to not create comments. There are not too many of them and perhaps they will find it is easier to write a (useful) comment than to game the system. Any thoughts on how I might proceed? Stated plainly, how can I tell when a string more-or-less forms at least one phrase? From marty.musatov at gmail.com Thu Jul 9 22:14:08 2009 From: marty.musatov at gmail.com (Martin Musatov) Date: Thu, 9 Jul 2009 19:14:08 -0700 Subject: AP -- MeAmI.org Paces Google In-Reply-To: <8BCC0DC4-7ADB-496C-AE87-5C419656CD73@pyth.net> References: <8BCC0DC4-7ADB-496C-AE87-5C419656CD73@pyth.net> Message-ID: Thank you. Martin Musatov 2009/7/9 Fri?rik M?r J?nsson > I'll be the first to admit it. The point of writing a fake story by > Associated Press and publishing it on a programming mailing list is totally > beyond me. > > Confoundedly yours, > Fri?rik M?r -------------- next part -------------- An HTML attachment was scrubbed... URL: From rogerb at rogerbinns.com Thu Jul 9 22:15:00 2009 From: rogerb at rogerbinns.com (Roger Binns) Date: Thu, 09 Jul 2009 19:15:00 -0700 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 jacopo mondi wrote: > Hi all, I need to patch socketmodule.c (the _socket module) in order to > add support to an experimental socket family. You may find it considerably easier to use ctypes since that will avoid the need for any patching. You'll also be able to control how read and write are done (eg read vs recvfrom vs recvmsg vs readv). You can use os.fdopen to convert your raw file descriptor into a Python file object if appropriate. If you do use ctypes then you'll only need to distribute pure Python source code. Roger -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkpWpCAACgkQmOOfHg372QQq0QCbB+rslUqK/AuUu0gA6N4m01Jo C8IAn3EkzFKCyt/K5WiuCnw3jzORBQh5 =JCv+ -----END PGP SIGNATURE----- From nobody at nowhere.com Thu Jul 9 22:28:04 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 10 Jul 2009 03:28:04 +0100 Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > Nobody says you shouldn't check your data. Only that "assert" is not the > right way to do that. "assert" is not the right way to check your *inputs*. It's a perfectly reasonable way to check data which "should" be valid, as well as a way to document what variables are supposed to contain. From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 22:42:04 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 02:42:04 GMT Subject: language analysis to enforce code standards References: Message-ID: On Fri, 10 Jul 2009 02:06:35 +0000, Jason S. Friedman wrote: > Hello, I administer the Informatica ETL tool at my company. Part of > that role involves creating and enforcing standards. I want the > Informatica developers to add comments to certain key objects and I want > to be able to verify (in an automated fashion) that they have done so. > > I cannot merely check for non-emptiness; that is trivial to circumvent. > On the other hand, I probably do not need to be able to catch > developers who are determined to not create comments. There are not too > many of them and perhaps they will find it is easier to write a (useful) > comment than to game the system. > > Any thoughts on how I might proceed? Stated plainly, how can I tell > when a string more-or-less forms at least one phrase? Define "phrase". if len(s) > 0: print "at least one character" if len(s.split()) > 0: print "at least one word" if len(s.split('\n') > 0: print "at least one line" -- Steven From timr at probo.com Thu Jul 9 22:52:58 2009 From: timr at probo.com (Tim Roberts) Date: Thu, 09 Jul 2009 19:52:58 -0700 Subject: IP Address Function References: Message-ID: Fred Atkinson wrote: > >I wonder why they don't just have a function to return it instead of >putting you through all of that? In CGI, EVERYTHING gets communicated through environment variables. That (and the stdin stream) is really the only option, since you get a new process for every request. The Python CGI module doesn't provide a wrapper function because this information is just not useful. Most corporate users sit behind proxies, so everyone at the company appears to come from the same IP. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From nobody at nowhere.com Thu Jul 9 22:53:57 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 10 Jul 2009 03:53:57 +0100 Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > For one of my projects, I came across the need to check if one of many > items from a list of strings could be found in a long string. If you need to match many strings or very long strings against the same list of items, the following should (theoretically) be optimal: r = re.compile('|'.join(map(re.escape,list_items))) ... result = r.search(string) From david250 at videotron.ca Thu Jul 9 22:54:16 2009 From: david250 at videotron.ca (David Bernier) Date: Thu, 09 Jul 2009 22:54:16 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search > the results of other Google users immediately results in a level of > accuracy and relevance above and beyond Google." Google changed their > API following the launch or MeAmI.org and explored the possibility of > blocking site access from MeAmI.org to Google search results but was > unable to do so. Critics of MeAmI.org say what it is doing is > tantamount to intellectual theft. When asked about this topper Musatov > exclaimed, "The Internet was made for people, not companies." An > analyst at Goldman Sachs says, requesting to remain anonymous, > "MeAmI.org has a strong presence in promoting itself as a vehicle for > global activism and to tell you the truth, this makes it much more > likely an acquisition target than potential intellectual property > violator." Google could not be reached for comment. Mr. Musatov, do you know who originally wrote the article above? Thank you. David Bernier From zac256 at gmail.com Thu Jul 9 22:54:47 2009 From: zac256 at gmail.com (Zac Burns) Date: Thu, 9 Jul 2009 19:54:47 -0700 Subject: psyco V2 beta2 benchmark In-Reply-To: References: Message-ID: <333edbe80907091954u7451e11fhf9600d1e2549137c@mail.gmail.com> Where do you get this beta? I heard that Psyco V2 is coming out but can't find anything on their site to support this. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games On Sat, Jul 4, 2009 at 5:26 AM, larudwer wrote: > just out of curiosity i've downloaded the latest Version of Psyco V2 Beta 2 > and run the benchmarks against the old Version of psyco 1.6 > > Because it might be of common interest, i am posting the results here. > > My machine is a Pentium D 3.2 Ghz running Windows XP SP 3 and Python 2.6.2. > Psyco V2 was built with 4.3.3-tdm-1 mingw32 with optimisation flags changed > to -O3 > > > Benchmark ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | avg. Base time | psyco 1.6 time > | psyco 2.0 time | ratio ? ? | possible error +- > time_anyall all_bool_genexp ? ? ? ? ? ? ? | 2.270 ? ? ? ? ?| 2.250 > | 2.420 ? ? ? ? ?| 0.930 ? ? | 8.3 % > time_anyall all_bool_listcomp ? ? ? ? ? ? | 3.450 ? ? ? ? ?| 1.900 > | 1.910 ? ? ? ? ?| 0.995 ? ? | 0.0 % > time_anyall all_genexp ? ? ? ? ? ? ? ? ? ?| 1.970 ? ? ? ? ?| 1.940 > | 2.160 ? ? ? ? ?| 0.898 ? ? | 9.6 % > time_anyall all_listcomp ? ? ? ? ? ? ? ? ?| 3.485 ? ? ? ? ?| 1.660 > | 1.660 ? ? ? ? ?| 1.000 ? ? | 1.4 % > time_anyall all_loop ? ? ? ? ? ? ? ? ? ? ?| 0.665 ? ? ? ? ?| 0.090 > | 0.090 ? ? ? ? ?| 1.000 ? ? | 4.4 % > time_anyall any_bool_genexp ? ? ? ? ? ? ? | 2.215 ? ? ? ? ?| 2.130 > | 2.340 ? ? ? ? ?| 0.910 ? ? | 10.0 % > time_anyall any_bool_listcomp ? ? ? ? ? ? | 3.620 ? ? ? ? ?| 1.930 > | 1.940 ? ? ? ? ?| 0.995 ? ? | 9.0 % > time_anyall any_genexp ? ? ? ? ? ? ? ? ? ?| 1.985 ? ? ? ? ?| 1.920 > | 2.180 ? ? ? ? ?| 0.881 ? ? | 10.1 % > time_anyall any_listcomp ? ? ? ? ? ? ? ? ?| 3.360 ? ? ? ? ?| 1.680 > | 1.680 ? ? ? ? ?| 1.000 ? ? | 8.0 % > time_anyall any_loop ? ? ? ? ? ? ? ? ? ? ?| 0.660 ? ? ? ? ?| 0.090 > | 0.090 ? ? ? ? ?| 1.000 ? ? | 3.0 % > time_builtins chr(i) ? ? ? ? ? ? ? ? ? ? ?| 2.420 ? ? ? ? ?| 0.010 > | 0.010 ? ? ? ? ?| 1.000 ? ? | 0.0 % > time_builtins hash(i) ? ? ? ? ? ? ? ? ? ? | 1.280 ? ? ? ? ?| 0.370 > | 0.080 ? ? ? ? ?| 4.625 ? ? | 8.1 % > time_builtins int(round(f)) ? ? ? ? ? ? ? | 2.635 ? ? ? ? ?| 1.510 > | 1.120 ? ? ? ? ?| 1.348 ? ? | 0.4 % > time_builtins min ? ? ? ? ? ? ? ? ? ? ? ? | 0.535 ? ? ? ? ?| 0.520 > | 0.120 ? ? ? ? ?| 4.333 ? ? | 1.9 % > time_builtins min_kw ? ? ? ? ? ? ? ? ? ? ?| 4.430 ? ? ? ? ?| 4.400 > | 0.160 ? ? ? ? ?| 27.500 ? ?| 0.5 % > time_builtins ord(i) ? ? ? ? ? ? ? ? ? ? ?| 0.320 ? ? ? ? ?| 0.000 > | 0.000 ? ? ? ? ?| 1.000 ? ? | 6.5 % > time_builtins pow ? ? ? ? ? ? ? ? ? ? ? ? | 0.345 ? ? ? ? ?| 0.230 > | 0.150 ? ? ? ? ?| 1.533 ? ? | 2.9 % > time_builtins reduce ? ? ? ? ? ? ? ? ? ? ?| 0.735 ? ? ? ? ?| 0.710 > | 0.020 ? ? ? ? ?| 35.498 ? ?| 1.4 % > time_builtins round(f) ? ? ? ? ? ? ? ? ? ?| 1.720 ? ? ? ? ?| 0.890 > | 0.400 ? ? ? ? ?| 2.225 ? ? | 1.2 % > time_builtins sums ? ? ? ? ? ? ? ? ? ? ? ?| 0.180 ? ? ? ? ?| 0.180 > | 0.100 ? ? ? ? ?| 1.800 ? ? | 0.0 % > time_fib matrix ? ? ? ? ? ? ? ? ? ? ? ? ? | 0.425 ? ? ? ? ?| 0.360 > | 0.360 ? ? ? ? ?| 1.000 ? ? | 2.3 % > time_fib recursive ? ? ? ? ? ? ? ? ? ? ? ?| 0.000 ? ? ? ? ?| 0.000 > | 0.000 ? ? ? ? ?| 1.000 ? ? | 0.0 % > time_fib takahashi ? ? ? ? ? ? ? ? ? ? ? ?| 0.410 ? ? ? ? ?| 0.320 > | 0.330 ? ? ? ? ?| 0.970 ? ? | 0.0 % > time_generators call next just many times | 0.900 ? ? ? ? ?| 0.630 > | 0.970 ? ? ? ? ?| 0.649 ? ? | 4.3 % > time_generators iterate just many times ? | 0.660 ? ? ? ? ?| 0.550 > | 0.950 ? ? ? ? ?| 0.579 ? ? | 3.1 % > time_generators send and loop 1000 ? ? ? ?| 2.805 ? ? ? ? ?| 2.540 > | 0.060 ? ? ? ? ?| 42.333 ? ?| 9.8 % > time_generators send call loop 1000 ? ? ? | 2.505 ? ? ? ? ?| 2.940 > | 0.060 ? ? ? ? ?| 48.999 ? ?| 10.9 % > time_generators send just many times ? ? ?| 1.280 ? ? ? ? ?| 0.590 > | 0.980 ? ? ? ? ?| 0.602 ? ? | 3.1 % > time_iter iter ? ? ? ? ? ? ? ? ? ? ? ? ? ?| 1.490 ? ? ? ? ?| 0.590 > | 0.440 ? ? ? ? ?| 1.341 ? ? | 5.5 % > time_math floats ? ? ? ? ? ? ? ? ? ? ? ? ?| 2.910 ? ? ? ? ?| 1.500 > | 1.630 ? ? ? ? ?| 0.920 ? ? | 0.7 % > time_properties method_get ? ? ? ? ? ? ? ?| 0.935 ? ? ? ? ?| 0.120 > | 0.130 ? ? ? ? ?| 0.923 ? ? | 1.1 % > time_properties method_set ? ? ? ? ? ? ? ?| 1.005 ? ? ? ? ?| 0.170 > | 0.180 ? ? ? ? ?| 0.944 ? ? | 1.0 % > time_properties property_get ? ? ? ? ? ? ?| 0.960 ? ? ? ? ?| 0.740 > | 0.100 ? ? ? ? ?| 7.400 ? ? | 2.1 % > time_properties property_set ? ? ? ? ? ? ?| 1.020 ? ? ? ? ?| 0.920 > | 0.930 ? ? ? ? ?| 0.989 ? ? | 0.0 % > time_properties pyproperty_get ? ? ? ? ? ?| 1.535 ? ? ? ? ?| 1.310 > | 0.140 ? ? ? ? ?| 9.357 ? ? | 0.7 % > time_properties pyproperty_set ? ? ? ? ? ?| 1.030 ? ? ? ? ?| 0.920 > | 0.930 ? ? ? ? ?| 0.989 ? ? | 2.0 % > time_subdist subdist(i) ? ? ? ? ? ? ? ? ? | 3.665 ? ? ? ? ?| 1.640 > | 6.140 ? ? ? ? ?| 0.267 ? ? | 0.8 % > time_sums rounding ? ? ? ? ? ? ? ? ? ? ? ?| 0.800 ? ? ? ? ?| 0.790 > | 0.810 ? ? ? ? ?| 0.975 ? ? | 2.5 % > > > Running new timings with > C:\Programme\GNU\python26\lib\site-packages\psyco\_psyco.pyd Sat Jul 04 > 12:09:07 2009 > > time_anyall ? ?: all_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.36 ? ? psyco: > 2.42 ? ? ratio: ?0.97 > time_anyall ? ?: all_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.45 ? ? psyco: > 1.91 ? ? ratio: ?1.80 > time_anyall ? ?: all_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 2.06 ? ? psyco: > 2.16 ? ? ratio: ?0.95 > time_anyall ? ?: all_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.51 ? ? psyco: > 1.66 ? ? ratio: ?2.11 > time_anyall ? ?: all_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.65 ? ? psyco: > 0.09 ? ? ratio: ?7.03 > time_anyall ? ?: any_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.32 ? ? psyco: > 2.34 ? ? ratio: ?0.99 > time_anyall ? ?: any_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.45 ? ? psyco: > 1.94 ? ? ratio: ?1.78 > time_anyall ? ?: any_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 2.08 ? ? psyco: > 2.18 ? ? ratio: ?0.96 > time_anyall ? ?: any_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.49 ? ? psyco: > 1.68 ? ? ratio: ?2.08 > time_anyall ? ?: any_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.65 ? ? psyco: > 0.09 ? ? ratio: ?7.05 > time_builtins ?: chr(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.42 ? ? psyco: > 0.01 ? ? ratio: 197.40 > time_builtins ?: hash(i) ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 1.33 ? ? psyco: > 0.08 ? ? ratio: 17.69 > time_builtins ?: int(round(f)) ? ? ? ? ? ? ? ? ?plain: ? 2.63 ? ? psyco: > 1.12 ? ? ratio: ?2.36 > time_builtins ?: min ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.53 ? ? psyco: > 0.12 ? ? ratio: ?4.28 > time_builtins ?: min_kw ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 4.44 ? ? psyco: > 0.16 ? ? ratio: 28.58 > time_builtins ?: ord(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.33 ? ? psyco: > 0.00 ? ? ratio: 123.57 > time_builtins ?: pow ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.34 ? ? psyco: > 0.15 ? ? ratio: ?2.24 > time_builtins ?: reduce ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.74 ? ? psyco: > 0.02 ? ? ratio: 40.93 > time_builtins ?: round(f) ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.73 ? ? psyco: > 0.40 ? ? ratio: ?4.38 > time_builtins ?: sums ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.18 ? ? psyco: > 0.10 ? ? ratio: ?1.80 > time_fib ? ? ? : matrix ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.42 ? ? psyco: > 0.36 ? ? ratio: ?1.18 > time_fib ? ? ? : recursive ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.00 ? ? psyco: > 0.00 ? ? ratio: 19.45 > time_fib ? ? ? : takahashi ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.41 ? ? psyco: > 0.33 ? ? ratio: ?1.25 > time_generators: send call loop 1000 ? ? ? ? ? ?plain: ? 2.36 ? ? psyco: > 0.06 ? ? ratio: 41.30 > time_generators: send and loop 1000 ? ? ? ? ? ? plain: ? 2.66 ? ? psyco: > 0.06 ? ? ratio: 46.69 > time_generators: send just many times ? ? ? ? ? plain: ? 1.26 ? ? psyco: > 0.98 ? ? ratio: ?1.29 > time_generators: iterate just many times ? ? ? ?plain: ? 0.67 ? ? psyco: > 0.95 ? ? ratio: ?0.70 > time_generators: call next just many times ? ? ?plain: ? 0.88 ? ? psyco: > 0.97 ? ? ratio: ?0.91 > time_iter ? ? ?: iter ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.53 ? ? psyco: > 0.44 ? ? ratio: ?3.47 > time_math ? ? ?: floats ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.90 ? ? psyco: > 1.63 ? ? ratio: ?1.78 > time_properties: method_get ? ? ? ? ? ? ? ? ? ? plain: ? 0.93 ? ? psyco: > 0.13 ? ? ratio: ?7.22 > time_properties: method_set ? ? ? ? ? ? ? ? ? ? plain: ? 1.00 ? ? psyco: > 0.18 ? ? ratio: ?5.61 > time_properties: property_get ? ? ? ? ? ? ? ? ? plain: ? 0.95 ? ? psyco: > 0.10 ? ? ratio: ?9.26 > time_properties: property_set ? ? ? ? ? ? ? ? ? plain: ? 1.02 ? ? psyco: > 0.93 ? ? ratio: ?1.09 > time_properties: pyproperty_get ? ? ? ? ? ? ? ? plain: ? 1.54 ? ? psyco: > 0.14 ? ? ratio: 10.80 > time_properties: pyproperty_set ? ? ? ? ? ? ? ? plain: ? 1.04 ? ? psyco: > 0.93 ? ? ratio: ?1.12 > time_subdist ? : subdist(i) ? ? ? ? ? ? ? ? ? ? plain: ? 3.68 ? ? psyco: > 6.14 ? ? ratio: ?0.60 > time_sums ? ? ?: rounding ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.79 ? ? psyco: > 0.81 ? ? ratio: ?0.98 > > > Running new timings with original psyco > > time_anyall ? ?: all_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.18 ? ? psyco: > 2.25 ? ? ratio: ?0.97 > time_anyall ? ?: all_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.45 ? ? psyco: > 1.90 ? ? ratio: ?1.82 > time_anyall ? ?: all_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 1.88 ? ? psyco: > 1.94 ? ? ratio: ?0.97 > time_anyall ? ?: all_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.46 ? ? psyco: > 1.66 ? ? ratio: ?2.09 > time_anyall ? ?: all_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.68 ? ? psyco: > 0.09 ? ? ratio: ?7.34 > time_anyall ? ?: any_bool_genexp ? ? ? ? ? ? ? ?plain: ? 2.11 ? ? psyco: > 2.13 ? ? ratio: ?0.99 > time_anyall ? ?: any_bool_listcomp ? ? ? ? ? ? ?plain: ? 3.79 ? ? psyco: > 1.93 ? ? ratio: ?1.96 > time_anyall ? ?: any_genexp ? ? ? ? ? ? ? ? ? ? plain: ? 1.89 ? ? psyco: > 1.92 ? ? ratio: ?0.98 > time_anyall ? ?: any_listcomp ? ? ? ? ? ? ? ? ? plain: ? 3.23 ? ? psyco: > 1.68 ? ? ratio: ?1.92 > time_anyall ? ?: any_loop ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.67 ? ? psyco: > 0.09 ? ? ratio: ?7.26 > time_builtins ?: chr(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.42 ? ? psyco: > 0.01 ? ? ratio: 197.10 > time_builtins ?: hash(i) ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 1.23 ? ? psyco: > 0.37 ? ? ratio: ?3.30 > time_builtins ?: int(round(f)) ? ? ? ? ? ? ? ? ?plain: ? 2.64 ? ? psyco: > 1.51 ? ? ratio: ?1.75 > time_builtins ?: min ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.54 ? ? psyco: > 0.52 ? ? ratio: ?1.03 > time_builtins ?: min_kw ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 4.42 ? ? psyco: > 4.40 ? ? ratio: ?1.00 > time_builtins ?: ord(i) ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.31 ? ? psyco: > 0.00 ? ? ratio: 116.56 > time_builtins ?: pow ? ? ? ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.35 ? ? psyco: > 0.23 ? ? ratio: ?1.50 > time_builtins ?: reduce ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.73 ? ? psyco: > 0.71 ? ? ratio: ?1.03 > time_builtins ?: round(f) ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.71 ? ? psyco: > 0.89 ? ? ratio: ?1.91 > time_builtins ?: sums ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.18 ? ? psyco: > 0.18 ? ? ratio: ?1.03 > time_fib ? ? ? : matrix ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.43 ? ? psyco: > 0.36 ? ? ratio: ?1.19 > time_fib ? ? ? : recursive ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.00 ? ? psyco: > 0.00 ? ? ratio: 19.28 > time_fib ? ? ? : takahashi ? ? ? ? ? ? ? ? ? ? ?plain: ? 0.41 ? ? psyco: > 0.32 ? ? ratio: ?1.27 > time_generators: send call loop 1000 ? ? ? ? ? ?plain: ? 2.65 ? ? psyco: > 2.94 ? ? ratio: ?0.90 > time_generators: send and loop 1000 ? ? ? ? ? ? plain: ? 2.95 ? ? psyco: > 2.54 ? ? ratio: ?1.16 > time_generators: send just many times ? ? ? ? ? plain: ? 1.30 ? ? psyco: > 0.59 ? ? ratio: ?2.23 > time_generators: iterate just many times ? ? ? ?plain: ? 0.65 ? ? psyco: > 0.55 ? ? ratio: ?1.17 > time_generators: call next just many times ? ? ?plain: ? 0.92 ? ? psyco: > 0.63 ? ? ratio: ?1.45 > time_iter ? ? ?: iter ? ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 1.45 ? ? psyco: > 0.59 ? ? ratio: ?2.47 > time_math ? ? ?: floats ? ? ? ? ? ? ? ? ? ? ? ? plain: ? 2.92 ? ? psyco: > 1.50 ? ? ratio: ?1.95 > time_properties: method_get ? ? ? ? ? ? ? ? ? ? plain: ? 0.94 ? ? psyco: > 0.12 ? ? ratio: ?7.88 > time_properties: method_set ? ? ? ? ? ? ? ? ? ? plain: ? 1.01 ? ? psyco: > 0.17 ? ? ratio: ?6.08 > time_properties: property_get ? ? ? ? ? ? ? ? ? plain: ? 0.97 ? ? psyco: > 0.74 ? ? ratio: ?1.31 > time_properties: property_set ? ? ? ? ? ? ? ? ? plain: ? 1.02 ? ? psyco: > 0.92 ? ? ratio: ?1.12 > time_properties: pyproperty_get ? ? ? ? ? ? ? ? plain: ? 1.53 ? ? psyco: > 1.31 ? ? ratio: ?1.17 > time_properties: pyproperty_set ? ? ? ? ? ? ? ? plain: ? 1.02 ? ? psyco: > 0.92 ? ? ratio: ?1.11 > time_subdist ? : subdist(i) ? ? ? ? ? ? ? ? ? ? plain: ? 3.65 ? ? psyco: > 1.64 ? ? ratio: ?2.23 > time_sums ? ? ?: rounding ? ? ? ? ? ? ? ? ? ? ? plain: ? 0.81 ? ? psyco: > 0.79 ? ? ratio: ?1.03 > > > > -- > http://mail.python.org/mailman/listinfo/python-list > From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 22:57:44 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 02:57:44 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > >> Nobody says you shouldn't check your data. Only that "assert" is not >> the right way to do that. > > "assert" is not the right way to check your *inputs*. It's a perfectly > reasonable way to check data which "should" be valid, as well as a way > to document what variables are supposed to contain. Where are those variables coming from? The distinction really boils down to this: * asserts should never fail. If there is any chance that an assertion might fail outside of test suites, then don't use assert. You can't control what input the caller provides to a function, so unless the data is generated inside the function, a caller might provide something unexpected. Therefore, assert is inappropriate for checking function parameters, even if that function is "private" and only called by your own functions. (1) So-called "private" or "internal" functions have a habit of becoming public, and then you have asserts in public functions. (2) If public() calls _private(x), and _private uses assert to check the value of x, there is a risk that if public() is buggy and supplies an invalid x, the assertion will never be checked and _private() will return incorrect results. (3) assert is absolutely unsuitable for enforcing pre-conditions and post- conditions, unless such conditions are mere "guidelines", because assert can be switched off at runtime. It's a fine line to draw. Obviously, if we think something *truly* can never fail, we don't bother checking it: def add(x, y): result = x+y assert result-y == x assert result-x == y return result is probably overkill for most programs, and yet surprisingly those assertions will commonly fail for float arguments! -- Steven From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 23:07:09 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 03:07:09 GMT Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > def list_items_in_string(list_items, string): > for item in list_items: > if item in string: > return True > return False ... > Any ideas how to make that function look nicer? :) Change the names. Reverse the order of the arguments. Add a docstring. Otherwise looks pretty nice to me. Simple, straightforward, and correct. If you're running Python 2.5 or better, then this is even shorter (and probably faster): def contains(s, targets): """Return True if any item of targets is in string s.""" return any(target in s for target in targets) -- Steven From tjreedy at udel.edu Thu Jul 9 23:07:34 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 09 Jul 2009 23:07:34 -0400 Subject: tough-to-explain Python In-Reply-To: <00720d76$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > And speaking of binary search: > > [quote] > I was shocked to learn that the binary search program that Bentley PROVED > CORRECT and SUBSEQUENTLY TESTED [emphasis added] in Chapter 5 of > "Programming Pearls" contains a bug. Once I tell you what the it is, you > will understand why it escaped detection for two decades. > [end quote] > > http://northernplanets.blogspot.com/2006/07/nearly-all-binary-searches-are-broken.html > > or http://tinyurl.com/nco6yv The actual report is at http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html The is the so called bug: "In Programming Pearls Bentley says that the analogous line "sets m to the average of l and u, truncated down to the nearest integer." On the face of it, this assertion might appear correct, but it fails for large values of the int variables low and high. Specifically, it fails if the sum of low and high is greater than the maximum positive int value (231 - 1). The sum overflows to a negative value, and the value stays negative when divided by two. In C this causes an array index out of bounds with unpredictable results. In Java, it throws ArrayIndexOutOfBoundsException." The is *not* a bug is Bentley program. It is a bug in bad, buggy, insane integer arithmetic implementations. low + high should either return the right answer, as it will in Python, or raise an overflow error. tjr From sjmachin at lexicon.net Thu Jul 9 23:07:58 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 9 Jul 2009 20:07:58 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: <347b30b2-48a9-49cd-8e33-172325d34a34@v15g2000prn.googlegroups.com> On Jul 10, 12:53?pm, Nobody wrote: > On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > > For one of my projects, I came across the need to check if one of many > > items from a list of strings could be found in a long string. > > If you need to match many strings or very long strings against the same > list of items, the following should (theoretically) be optimal: > > ? ? ? ? r = re.compile('|'.join(map(re.escape,list_items))) > ? ? ? ? ... > ? ? ? ? result = r.search(string) "theoretically optimal" happens only if the search mechanism builds a DFA or similar out of the list of strings. AFAIK Python's re module doesn't. Try this: http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/ From miki.tebeka at gmail.com Thu Jul 9 23:14:30 2009 From: miki.tebeka at gmail.com (lazy1) Date: Thu, 9 Jul 2009 20:14:30 -0700 (PDT) Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <128ab32f-3609-44a5-9fb3-1a7cb64f04e9@y10g2000prg.googlegroups.com> Hello Daniel, > Can someone recommend me a faster plotting library? I found out gnuplot to be blazing fast for many many points. I usually just call it using subprocess but there are Python bindings to it somewhere as well. HTH, -- Miki http://pythonwise.blogspot.com From http Thu Jul 9 23:24:54 2009 From: http (Paul Rubin) Date: 09 Jul 2009 20:24:54 -0700 Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: <7x63e1kynt.fsf@ruckus.brouhaha.com> inkhorn writes: > def list_items_in_string(list_items, string): > for item in list_items: > if item in string: > return True > return False You could write that as (untested): def list_items_in_string(list_items, string): return any(item in string for item in list_items) but there are faster algorithms you could use if the list is large and you want to do the test on lots of long strings, etc. From scriber77 at yahoo.com Thu Jul 9 23:25:13 2009 From: scriber77 at yahoo.com (scriber77 at yahoo.com) Date: Thu, 9 Jul 2009 20:25:13 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: On Jul 9, 7:54?pm, David Bernier wrote: > Musatov wrote: > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > its adopted use in Pakistan. ?The search engine has grown in > > popularity 10,000 fold following its Beta test launch three months ago > > in April, 2009. Supporters of the site claim it is better than rival > > Google upon which platform it is based. Controversy arose after > > MeAmI.org search code allowed users to search other users Google > > results with no advertising. "It is truly an innovative thing we are > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > the results of other Google users immediately results in a level of > > accuracy and relevance above and beyond Google." Google changed their > > API following the launch or MeAmI.org and explored the possibility of > > blocking site access from MeAmI.org to Google search results but was > > unable to do so. Critics of MeAmI.org say what it is doing is > > tantamount to intellectual theft. When asked about this topper Musatov > > exclaimed, "The Internet was made for people, not companies." An > > analyst at Goldman Sachs says, requesting to remain anonymous, > > "MeAmI.org has a strong presence in promoting itself as a vehicle for > > global activism and to tell you the truth, this makes it much more > > likely an acquisition target than potential intellectual property > > violator." Google could not be reached for comment. > > Mr. Musatov, ?do you know who originally wrote the > article above? > > Thank you. > > David Bernier- Hide quoted text - > > - Show quoted text - Hello, I am an anonymous Professor of Mathematics at a University in the Midwest. You see, I have been contacted by this "Musatov" fellow as well and posted a link he sent me to gather a second opinion. I was immediately attacked by a Mr. Tim Chow from M.I.T. as though I was him. I explained to him Mr. Musatov had contacted me, that I had read his simple and very light 3 page paper and said I thought the work in the equations was elegant and the mending of the binary with the traditional mathematics symbols inventive. Listen, I understand this "Musatov" character has annoyed a lot of you and apparently rightfully so, at least to some extent, has gained a bit of contempt from the community. But perhaps he is trying to see things a bit differently and is just not getting the feedback he needs, so he is throwing tantrums apparently across USENET. Like I said before, I am just trying to do right by this person who contacted me, and seemed to be a decent person with a genuine interest in mathematics. He was very respectful. Alas, I am at a loss on what specific feedback to give him on his paper as though I am a professor of Mathematics, Computer Science is not my specialty. I told him I would try to get him some feedback on the equations on page 3. Would any of you be so kind to help me? Here is the paper: http://MeAmI.org/pversusnp.pdf I do thank you for your time. Good day, Professor X From msjmb at tpg.com.au Thu Jul 9 23:28:15 2009 From: msjmb at tpg.com.au (MichaelW) Date: Thu, 9 Jul 2009 20:28:15 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: <109bd130-12d6-458b-879b-fa26afab678d@l5g2000pra.googlegroups.com> On Jul 10, 1:25?pm, scribe... at yahoo.com wrote: > On Jul 9, 7:54?pm, David Bernier wrote: > > > > > > > Musatov wrote: > > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > > its adopted use in Pakistan. ?The search engine has grown in > > > popularity 10,000 fold following its Beta test launch three months ago > > > in April, 2009. Supporters of the site claim it is better than rival > > > Google upon which platform it is based. Controversy arose after > > > MeAmI.org search code allowed users to search other users Google > > > results with no advertising. "It is truly an innovative thing we are > > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > > the results of other Google users immediately results in a level of > > > accuracy and relevance above and beyond Google." Google changed their > > > API following the launch or MeAmI.org and explored the possibility of > > > blocking site access from MeAmI.org to Google search results but was > > > unable to do so. Critics of MeAmI.org say what it is doing is > > > tantamount to intellectual theft. When asked about this topper Musatov > > > exclaimed, "The Internet was made for people, not companies." An > > > analyst at Goldman Sachs says, requesting to remain anonymous, > > > "MeAmI.org has a strong presence in promoting itself as a vehicle for > > > global activism and to tell you the truth, this makes it much more > > > likely an acquisition target than potential intellectual property > > > violator." Google could not be reached for comment. > > > Mr. Musatov, ?do you know who originally wrote the > > article above? > > > Thank you. > > > David Bernier- Hide quoted text - > > > - Show quoted text - > > Hello, I am an anonymous Professor of Mathematics at a University in > the Midwest. You see, I have been contacted by this "Musatov" fellow > as well and posted a link he sent me to gather a second opinion. I was > immediately attacked by a Mr. Tim Chow from M.I.T. as though I was > him. I explained to him Mr. Musatov had contacted me, that I had read > his simple and very light 3 page paper and said I thought the work in > the equations was elegant and the mending of the binary with the > traditional mathematics symbols inventive. Listen, I understand this > "Musatov" character has annoyed a lot of you and apparently rightfully > so, at least to some extent, has gained a bit of contempt from the > community. But perhaps he is trying to see things a bit differently > and is just not getting the feedback he needs, so he is throwing > tantrums apparently across USENET. > > Like I said before, I am just trying to do right by this person who > contacted me, and seemed to be a decent person with a genuine interest > in mathematics. He was very respectful. > > Alas, I am at a loss on what specific feedback to give him on his > paper as though I am a professor of Mathematics, Computer Science is > not my specialty. > > I told him I would try to get him some feedback on the equations on > page 3. ?Would any of you be so kind to help me? > > Here is the paper:http://MeAmI.org/pversusnp.pdf > > I do thank you for your time. > > Good day, > > Professor X- Hide quoted text - > > - Show quoted text - Please see reply in the "Breathtaking..." thread. It will come as no surprise to anyone that "Professor X" and "The Masked Logician" share the same computer. MichaelW. From bcb at undisclosedlocation.net Thu Jul 9 23:28:46 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Thu, 9 Jul 2009 22:28:46 -0500 Subject: language analysis to enforce code standards References: Message-ID: "Jason S. Friedman" wrote in message news:mailman.2927.1247192026.8015.python-list at python.org... > Hello, I administer the Informatica ETL tool at my company. Part of that > role involves creating and enforcing standards. I want the Informatica > developers to add comments to certain key objects and I want to be able to > verify (in an automated fashion) that they have done so. > > I cannot merely check for non-emptiness; that is trivial to circumvent. On > the other hand, I probably do not need to be able to catch developers who > are determined to not create comments. There are not too many of them and > perhaps they will find it is easier to write a (useful) comment than to > game the system. > > Any thoughts on how I might proceed? Stated plainly, how can I tell when > a string more-or-less forms at least one phrase? Well, you *could* try analyzing the comment text using NLTK: http://www.nltk.org/ From bcb at undisclosedlocation.net Thu Jul 9 23:33:25 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Thu, 9 Jul 2009 22:33:25 -0500 Subject: AP -- MeAmI.org Paces Google References: Message-ID: <5Cy5m.121$Ly1.67@newsfe06.iad> wrote in message news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! From steven at REMOVE.THIS.cybersource.com.au Thu Jul 9 23:41:43 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 03:41:43 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: On Thu, 09 Jul 2009 23:07:34 -0400, Terry Reedy wrote: > Steven D'Aprano wrote: > >> And speaking of binary search: >> >> [quote] >> I was shocked to learn that the binary search program that Bentley >> PROVED CORRECT and SUBSEQUENTLY TESTED [emphasis added] in Chapter 5 of >> "Programming Pearls" contains a bug. Once I tell you what the it is, >> you will understand why it escaped detection for two decades. [end >> quote] >> >> http://northernplanets.blogspot.com/2006/07/nearly-all-binary-searches- are-broken.html >> >> or http://tinyurl.com/nco6yv > > The actual report is at > http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about- it-nearly.html > > The is the so called bug: > "In Programming Pearls Bentley says that the analogous line "sets m to > the average of l and u, truncated down to the nearest integer." On the > face of it, this assertion might appear correct, but it fails for large > values of the int variables low and high. Specifically, it fails if the > sum of low and high is greater than the maximum positive int value (231 > - 1). The sum overflows to a negative value, and the value stays > negative when divided by two. In C this causes an array index out of > bounds with unpredictable results. In Java, it throws > ArrayIndexOutOfBoundsException." > > The is *not* a bug is Bentley program. Wow. That's an impressive set of typos :) > It is a bug in bad, buggy, insane > integer arithmetic implementations. low + high should either return the > right answer, as it will in Python, or raise an overflow error. Is binary search *supposed* to raise an overflow error if given more than 2**30 elements? If not, then you're solving a different problem: it's just like binary search, but it only works up to a maximum number of elements. Now perhaps that's a legitimate problem to solve, but unless you make the change in behaviour clear up front, your code has failed to live up to specifications and therefore is buggy. Is there something special about OverflowError that is "better" than ArrayIndexOutOfBoundsException? I don't have "Programming Pearls", and unfortunately the section on binary search is not online: http://netlib.bell-labs.com/cm/cs/pearls/sketch04.html but there's no reason to imagine that the book will assume -- or even state as a requirement for binary search -- that addition will never overflow. Far from it: it seems to me that the author is very aware of real world constraints, and in the early 1980s, BigInt types were not something you took as a given. Of course *binary search* as an algorithm is not broken. The bug was that "Programming Pearls" neglected to take into account overflow when you have more than 2**30 items. One way to take that into account is to insist on using a language like Python where addition can never overflow. But that's not the code presented in "Programming Pearls". -- Steven From marty.musatov at gmail.com Thu Jul 9 23:43:08 2009 From: marty.musatov at gmail.com (Musatov) Date: Thu, 9 Jul 2009 20:43:08 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: On Jul 9, 7:54?pm, David Bernier wrote: > Musatov wrote: > > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > > its adopted use in Pakistan. ?The search engine has grown in > > popularity 10,000 fold following its Beta test launch three months ago > > in April, 2009. Supporters of the site claim it is better than rival > > Google upon which platform it is based. Controversy arose after > > MeAmI.org search code allowed users to search other users Google > > results with no advertising. "It is truly an innovative thing we are > > doing," said Founder and CEO, Martin Musatov. "Letting users search > > the results of other Google users immediately results in a level of > > accuracy and relevance above and beyond Google." Google changed their > > API following the launch or MeAmI.org and explored the possibility of > > blocking site access from MeAmI.org to Google search results but was > > unable to do so. Critics of MeAmI.org say what it is doing is > > tantamount to intellectual theft. When asked about this topper Musatov > > exclaimed, "The Internet was made for people, not companies." An > > analyst at Goldman Sachs says, requesting to remain anonymous, > > "MeAmI.org has a strong presence in promoting itself as a vehicle for > > global activism and to tell you the truth, this makes it much more > > likely an acquisition target than potential intellectual property > > violator." Google could not be reached for comment. > > Mr. Musatov, ?do you know who originally wrote the > article above? > > Thank you. > > David Bernier- Hide quoted text - > > - Show quoted text - Yes. From timr at probo.com Thu Jul 9 23:44:50 2009 From: timr at probo.com (Tim Roberts) Date: Thu, 09 Jul 2009 20:44:50 -0700 Subject: How to use Python to interface with Web pages? References: <1d17d40d-ee5a-4f67-a6f0-1d824c54da41@m7g2000prd.googlegroups.com> Message-ID: Peter wrote: > >Any help would be appreciated :-) > >I want to write an auction sniping tool in Python. Please don't. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From timr at probo.com Thu Jul 9 23:50:30 2009 From: timr at probo.com (Tim Roberts) Date: Thu, 09 Jul 2009 20:50:30 -0700 Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: TheSeeker wrote: > >I am embarking on teaching myself Microsoft UI Automation using Python >as the scripting language. The University of North Carolina at Chapel Hill has a great Python package called pyAA that does exactly this: http://www.cs.unc.edu/Research/assist/developer.shtml http://mindtrove.info/articles/gui-automation-with-pyaa/ -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From scriptlearner at gmail.com Fri Jul 10 01:29:15 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Thu, 9 Jul 2009 22:29:15 -0700 (PDT) Subject: Looking for the right library for a simple HTTP client Message-ID: I am trying to implement a simple client that can do the following: 1)to send the following kinds of HTTP requests and validate responses 1.1 GET 1.2 POST with application/x-www-form-urlencoded encoding 1.3 POST with multipart/form-data encoding 2)to set any number of (even duplicate) headers. For example, I may intentionally add the following Cookie: headers to a request: Cookie: id_1=v1;Domain=sample.com;Path=/ Cookie: id_1=v1;Domain=sample.com;Path=/ <--same as the one above Cookie: id_2=v1;Domain=sample.com;Path=/ 3)to set proxy cfg so requests can go through a proxy server to reach the target server. 4)to send multiple requests simultaneously. I have Python 2.4.1 on Solaris 9 and 10, and I don't have any plan to upgrade to latest version of Python. I looked around and I found httplib and urllib. Are they sufficient for my tasks 1 to 3 above? Any good sample codes will be great. Thanks. From david250 at videotron.ca Fri Jul 10 01:45:36 2009 From: david250 at videotron.ca (David Bernier) Date: Fri, 10 Jul 2009 01:45:36 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > On Jul 9, 7:54 pm, David Bernier wrote: >> Musatov wrote: >>> Los Angeles (AP) --MeAmI.org now has users in 50 countries following >>> its adopted use in Pakistan. The search engine has grown in >>> popularity 10,000 fold following its Beta test launch three months ago >>> in April, 2009. Supporters of the site claim it is better than rival >>> Google upon which platform it is based. Controversy arose after >>> MeAmI.org search code allowed users to search other users Google >>> results with no advertising. "It is truly an innovative thing we are >>> doing," said Founder and CEO, Martin Musatov. "Letting users search >>> the results of other Google users immediately results in a level of >>> accuracy and relevance above and beyond Google." Google changed their >>> API following the launch or MeAmI.org and explored the possibility of >>> blocking site access from MeAmI.org to Google search results but was >>> unable to do so. Critics of MeAmI.org say what it is doing is >>> tantamount to intellectual theft. When asked about this topper Musatov >>> exclaimed, "The Internet was made for people, not companies." An >>> analyst at Goldman Sachs says, requesting to remain anonymous, >>> "MeAmI.org has a strong presence in promoting itself as a vehicle for >>> global activism and to tell you the truth, this makes it much more >>> likely an acquisition target than potential intellectual property >>> violator." Google could not be reached for comment. >> Mr. Musatov, do you know who originally wrote the >> article above? >> >> Thank you. >> >> David Bernier- Hide quoted text - >> >> - Show quoted text - > > Yes. Mr. Musatov, do you know the name of the person who originally wrote the article above? Thank you. David Bernier From scriber77 at yahoo.com Fri Jul 10 01:52:14 2009 From: scriber77 at yahoo.com (Floetry) Date: Thu, 9 Jul 2009 22:52:14 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: <5Cy5m.121$Ly1.67@newsfe06.iad> Message-ID: <803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3@2g2000prl.googlegroups.com> On Jul 9, 8:33?pm, "Bruce C. Baker" wrote: > wrote in message > > news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... > > > > Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! Any reason "ka"-snip? Binomial Theorem: page 3. http://MeAmI.org/pversusnp.pdf From s.deshe at gmail.com Fri Jul 10 02:18:03 2009 From: s.deshe at gmail.com (slamdunk) Date: Thu, 9 Jul 2009 23:18:03 -0700 (PDT) Subject: way for a function to understand whether it's being run through a OnCreate callback or not Message-ID: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> is there a way for a function to understand whether it's being run through a OnCreate callback or not? I have working functions that I want to recycle through the OnCreate but need to catch the "nuke.thisNode()" bit inside them so they can still function when called manually through other scripts functions. From bcb at undisclosedlocation.net Fri Jul 10 02:21:46 2009 From: bcb at undisclosedlocation.net (Bruce C. Baker) Date: Fri, 10 Jul 2009 01:21:46 -0500 Subject: AP -- MeAmI.org Paces Google References: <5Cy5m.121$Ly1.67@newsfe06.iad> <803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3@2g2000prl.googlegroups.com> Message-ID: "Floetry" wrote in message news:803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3 at 2g2000prl.googlegroups.com... On Jul 9, 8:33 pm, "Bruce C. Baker" wrote: > wrote in message > > news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... > > > > Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! Any reason "ka"-snip? Binomial Theorem: page 3. http://MeAmI.org/pversusnp.pdf _________________________________________________ Well, *Martin*, since you think I'm dead, it could only have been my "ka" that did the snipping. Surely a polymath of your vast accomplishments will have no problem with the religio-historical reference, eh, *Martin*? (I'm predicting that not only will you *not* recognize it, you'll find it beneath your notice. No fair googling, *Martin*!) From clp2 at rebertia.com Fri Jul 10 02:23:17 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 9 Jul 2009 23:23:17 -0700 Subject: way for a function to understand whether it's being run through a OnCreate callback or not In-Reply-To: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> References: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> Message-ID: <50697b2c0907092323n6da421f1o9b3faec654bd88e7@mail.gmail.com> On Thu, Jul 9, 2009 at 11:18 PM, slamdunk wrote: > is there a way for a function to understand whether it's being run > through a OnCreate callback or not? > I have working functions that I want to recycle through the OnCreate > but need to catch the "nuke.thisNode()" bit inside them so they can > still function when called manually through other scripts functions. Your question lacks context. What's an OnCreate callback? Cheers, Chris -- http://blog.rebertia.com From gherron at islandtraining.com Fri Jul 10 02:59:22 2009 From: gherron at islandtraining.com (Gary Herron) Date: Thu, 09 Jul 2009 23:59:22 -0700 Subject: way for a function to understand whether it's being run through a OnCreate callback or not In-Reply-To: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> References: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> Message-ID: <4A56E6CA.5090802@islandtraining.com> slamdunk wrote: > is there a way for a function to understand whether it's being run > through a OnCreate callback or not? > I have working functions that I want to recycle through the OnCreate > but need to catch the "nuke.thisNode()" bit inside them so they can > still function when called manually through other scripts functions. > You've given us almost *NO* clue what you are talking about. I suppose I could Google "onCreate", and perhaps "nuke" and "thisNode", and see what falls out. But why would I? If you want our help, *you* take the time to tell is what you are asking about. You may want to start here: http://www.catb.org/~esr/faqs/smart-questions.html#intro Gary Herron From kf9150 at gmail.com Fri Jul 10 03:13:54 2009 From: kf9150 at gmail.com (Kelie) Date: Fri, 10 Jul 2009 00:13:54 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> Message-ID: On Jul 9, 5:50?pm, Tim Roberts wrote: > The University of North Carolina at Chapel Hill has a great Python package > called pyAA that does exactly this: > > http://www.cs.unc.edu/Research/assist/developer.shtmlhttp://mindtrove.info/articles/gui-automation-with-pyaa/ > -- > Tim Roberts, t... at probo.com Thanks Tim. From this page http://sourceforge.net/projects/uncassist/files/, there isn't a installer for Python2.5 or higher. Didn't find the source code there either. From marty.musatov at gmail.com Fri Jul 10 03:19:40 2009 From: marty.musatov at gmail.com (Musatov) Date: Fri, 10 Jul 2009 00:19:40 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: <5Cy5m.121$Ly1.67@newsfe06.iad> <803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3@2g2000prl.googlegroups.com> Message-ID: <1d58ce00-9436-4220-a7ad-fc586df6625c@h18g2000yqj.googlegroups.com> On Jul 9, 11:21?pm, "Bruce C. Baker" wrote: > "Floetry" wrote in message > > news:803a7f06-8e43-4ff5-86a6-9fd9d2eee0c3 at 2g2000prl.googlegroups.com... > On Jul 9, 8:33 pm, "Bruce C. Baker" > wrote: > > > wrote in message > > >news:defacf35-6149-485a-8f03-15472d63de43 at a39g2000pre.googlegroups.com... > > > > > > Oh, puh-LEEZ, Martin! A two-year-old wouldn't be fooled by this! > > Any reason "ka"-snip? > > Binomial Theorem: page 3.http://MeAmI.org/pversusnp.pdf > > _________________________________________________ > > Well, *Martin*, since you think I'm dead, it could only have been my "ka" > that did the snipping. Surely a polymath of your vast accomplishments will > have no problem with the religio-historical reference, eh, *Martin*? (I'm > predicting that not only will you *not* recognize it, you'll find it beneath > your notice. No fair googling, *Martin*!) I did not Google. ka in ancient Egypt, the supposed spiritual part of an individual human being or god, which survived (with the soul) after death and could reside in a statue of the person. ELIZABETH KNOWLES. "ka." The Oxford Dictionary of Phrase and Fable. Oxford University Press. 2006. Encyclopedia.com. 10 Jul. 2009 . In a third class of religion?usually heavily interlaced with fetishism? magic, momentary and special deities, nature gods, and deities personifying natural functions (such as the Egyptian solar god Ra, the Babylonian goddess of fertility Ishtar, the Greek sea-god Poseidon, and the Hindu goddess of death and destruction *Ka*li) emerge and are incorporated into a system of mythology and ritual. Sometimes they take on distinctively human characteristics (see anthropomorphism). The axis of reason - equinox, mass, and time - the orbits of ...Et a basso profundo hashi-hashi-brek-a-time todo ka tiempo. > Vozacka dozvola macarana lambada ... (&?&@?&?&?@&?&)) P. -- Musatov http://MeAmI.org ... From martin at librador.com Fri Jul 10 03:30:48 2009 From: martin at librador.com (Martin Vilcans) Date: Fri, 10 Jul 2009 09:30:48 +0200 Subject: way for a function to understand whether it's being run through a OnCreate callback or not In-Reply-To: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> References: <04fb0113-bea2-427f-9a6a-cb742184dd02@g1g2000pra.googlegroups.com> Message-ID: On Fri, Jul 10, 2009 at 8:18 AM, slamdunk wrote: > is there a way for a function to understand whether it's being run > through a OnCreate callback or not? > I have working functions that I want to recycle through the OnCreate > but need to catch the "nuke.thisNode()" bit inside them so they can > still function when called manually through other scripts functions. I suppose you're programming for The Foundry's Nuke. Python is used in lots of different contexts, and most people on this list use Python for something totally unrelated to video. As an attempt at answering your question, you can add a parameter to your function that tells from where it's called. I.e. you put True in the parameter when it is called from OnCreate, False when it is not. But that isn't a very good design. A function should not need to care from where it is called. You probably want to have the node as a parameter to the function instead of calling nuke.thisNode inside of it. I can't find documentation for the OnCreate function online. Do you mean that you can only call nuke.thisNode() from inside OnCreate? Here's my guess of what I think you want to do: def OnCreate(): # call your function with the current node as argument your_function(nuke.thisNode()) def your_function(node): # The function takes a node as an argument. # Do whatever you want here For more Nuke-specific questions, you'd probably get better results by asking on the Nuke-python mailing list: http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python -- martin at librador.com http://www.librador.com From dstanek at dstanek.com Fri Jul 10 03:34:05 2009 From: dstanek at dstanek.com (David Stanek) Date: Fri, 10 Jul 2009 03:34:05 -0400 Subject: Looking for the right library for a simple HTTP client In-Reply-To: References: Message-ID: On Fri, Jul 10, 2009 at 1:29 AM, scriptlearner at gmail.com wrote: > I am trying to implement a simple client that can do the following: > 1)to send the following kinds of HTTP requests and validate responses > 1.1 GET > 1.2 POST with application/x-www-form-urlencoded encoding > 1.3 POST with multipart/form-data encoding > > 2)to set any number of (even duplicate) headers. ?For example, I may > intentionally add the following Cookie: headers to a request: > Cookie: id_1=v1;Domain=sample.com;Path=/ > Cookie: id_1=v1;Domain=sample.com;Path=/ <--same as the one above > Cookie: id_2=v1;Domain=sample.com;Path=/ > > 3)to set proxy cfg so requests can go through a proxy server to reach > the target server. > > 4)to send multiple requests simultaneously. > > > I have Python 2.4.1 on Solaris 9 and 10, and I don't have any plan to > upgrade to latest version of Python. > > I looked around and I found httplib and urllib. ?Are they sufficient > for my tasks 1 to 3 above? ?Any good sample codes will be great. > Thanks. > -- > http://mail.python.org/mailman/listinfo/python-list > I like urllib2. The examples[1] are pretty good. [1] http://www.python.org/doc/2.4.1/lib/urllib2-examples.html -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From bhardwajjayesh at gmail.com Fri Jul 10 03:54:52 2009 From: bhardwajjayesh at gmail.com (jayesh bhardwaj) Date: Fri, 10 Jul 2009 00:54:52 -0700 (PDT) Subject: Httplib issues Message-ID: <017b8d15-6a4c-4a6c-8d92-1f8e8cbf4c6d@t33g2000yqe.googlegroups.com> Hi, i was trying to download file frm a terminal having apache with httplib manipulation. It worked, now i want to upload file to the terminal. Can this b done by httplib too? From lie.1296 at gmail.com Fri Jul 10 04:03:05 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 10 Jul 2009 08:03:05 GMT Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Fri?rik M?r J?nsson wrote: > Hi Rhodri, > >> It's only really a pitfall if you try to use the built-in after you've >> redefined it. That's the thing to keep an eye open for. > > You're right, but in cases where you're editing a codebase which you > didn't author entirely by yourself you may not be aware of that. > > That said, if the codebase you're working on is structured (short, > concise methods) you should be working with small, consumable scopes you > can inhale in entirety before modifying. > > Regards, > Fri?rik M?r But if you are responsible for a large codebase that you don't write yourself, it is doubtful that you're a real newbie. From lie.1296 at gmail.com Fri Jul 10 04:47:51 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 10 Jul 2009 08:47:51 GMT Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: References: Message-ID: Simon Forman wrote: > On Thu, Jul 9, 2009 at 9:42 AM, Nick wrote: > >> fields = line.split() >> for i in range(len(fields)): >> fields[i] = float(fields[i]) > > > instead of the above code you could say: > > fields = [float(n) for n in in line.split()] > > Have fun getting back into python! :] (A lot has changed in the last few years) >>> fields = [float(n) for n in in line.split()] File "", line 1 fields = [float(n) for n in in line.split()] ^ SyntaxError: invalid syntax s/in in/in/ From jeremy+complangpython at jeremysanders.net Fri Jul 10 04:51:29 2009 From: jeremy+complangpython at jeremysanders.net (Jeremy Sanders) Date: Fri, 10 Jul 2009 09:51:29 +0100 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: tt-industries wrote: > Hi, > > I am programming a oscilloscope module in Python. For this reason, I > want to plot very many data points as fast as possible. This can be > more than 100 000 at once. So far I have been using the ploting module > of wxPython. However, it becomes unstable for more than 25000 points. > Can someone recommend me a faster plotting library? It would be really > cool if one could embed this in wxPython. If someone has an idea I > would be very glad about answer. Veusz can plot a line with that many points with that many points in a couple of seconds on my system. It's a bit faster without antialiasing, slower if you want to actually plot markers at each position. Jeremy -- Jeremy Sanders http://www.jeremysanders.net/ From deets at nospam.web.de Fri Jul 10 04:59:08 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 10 Jul 2009 10:59:08 +0200 Subject: Httplib issues In-Reply-To: <017b8d15-6a4c-4a6c-8d92-1f8e8cbf4c6d@t33g2000yqe.googlegroups.com> References: <017b8d15-6a4c-4a6c-8d92-1f8e8cbf4c6d@t33g2000yqe.googlegroups.com> Message-ID: <7boe6sF249o1gU1@mid.uni-berlin.de> jayesh bhardwaj schrieb: > Hi, > i was trying to download file frm a terminal having apache with > httplib manipulation. It worked, now i want to upload file to the > terminal. Can this b done by httplib too? "Upload file to the terminal" makes no sense to me. You can upload something to a server. You can run a terminal. You can run a ssh-session to a server in a terminal. You can't upload to a terminal. But you can use httplib (or better urllib2) to make http-post-requests to webservers. And last but not least the ubiquious http://catb.org/esr/faqs/smart-questions.html Diez From jeanmichel at sequans.com Fri Jul 10 05:11:33 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 10 Jul 2009 11:11:33 +0200 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <4A5705C5.6010301@sequans.com> Nobody wrote: > On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > > >> Nobody says you shouldn't check your data. Only that "assert" is not the >> right way to do that. >> > > "assert" is not the right way to check your *inputs*. It's a perfectly > reasonable way to check data which "should" be valid, as well as a way to > document what variables are supposed to contain. > > Maybe, one of the assert problem is a semantic issue. Assert is a convenient way to write in the code "This is a requirement, get lost if you don't fit in". However it seems we often forget that builtin asserts can be disabled. One possible solution for those who prefer to write assertions instead a 'if A then Exception' is to write their own assertion statement and **raise an exception** that will not be disabled at runtime. Jea-Michel From garabik-news-2005-05 at kassiopeia.juls.savba.sk Fri Jul 10 05:23:54 2009 From: garabik-news-2005-05 at kassiopeia.juls.savba.sk (garabik-news-2005-05 at kassiopeia.juls.savba.sk) Date: Fri, 10 Jul 2009 09:23:54 +0000 (UTC) Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: Tim Harig wrote: > On 2009-07-09, Alex Rosslyn wrote: >> I would like to learn a way of changing the colour of a particular >> part of the output text. I've tried the following > On Unix operating systems this would be done through the curses interface: > > http://docs.python.org/library/curses.html Or using ANSI colour codes: colours = { 'none' : "", 'default' : "\033[0m", 'bold' : "\033[1m", 'underline' : "\033[4m", 'blink' : "\033[5m", 'reverse' : "\033[7m", 'concealed' : "\033[8m", 'black' : "\033[30m", 'red' : "\033[31m", 'green' : "\033[32m", 'yellow' : "\033[33m", 'blue' : "\033[34m", 'magenta' : "\033[35m", 'cyan' : "\033[36m", 'white' : "\033[37m", 'on_black' : "\033[40m", 'on_red' : "\033[41m", 'on_green' : "\033[42m", 'on_yellow' : "\033[43m", 'on_blue' : "\033[44m", 'on_magenta' : "\033[45m", 'on_cyan' : "\033[46m", 'on_white' : "\033[47m", 'beep' : "\007", # non-standard attributes, supported by some terminals 'dark' : "\033[2m", 'italic' : "\033[3m", 'rapidblink' : "\033[6m", 'strikethrough': "\033[9m", } print colours['red'], 'this is red', colours['blue'], 'blue', colours['on_green'], 'and with a green background', colours['default'] -- ----------------------------------------------------------- | Radovan Garab?k http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk | ----------------------------------------------------------- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread! From roland at catalogix.se Fri Jul 10 05:33:25 2009 From: roland at catalogix.se (Roland Hedberg) Date: Fri, 10 Jul 2009 11:33:25 +0200 Subject: A zlib question Message-ID: <80F42625-5FB2-438F-AFF2-913236A6D336@catalogix.se> Hi! I have a problem with zlib and compressing/decompressing according to RFC 1951. It seems like I can decompress, something compressed according to RFC 1951 by someone else, provided I set wbits to something negative (used -8 but I guess any negative number would work?). But how can I compress using zlib so it doesn't add a gzip header ? -- Roland From jeanmichel at sequans.com Fri Jul 10 05:39:54 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 10 Jul 2009 11:39:54 +0200 Subject: the ultimate logging facility for debugging code Message-ID: <4A570C6A.2060904@sequans.com> Greetings, Sorry for the dubious title :o). I was wandering if there is a standard (or reliable) python module that implements the following feature: http://code.activestate.com/recipes/198078/ >>>Recipe 198078: Wrapping method calls (meta-class example) >>> >>>A metaclass is used to wrap all (or just some) methods for logging purposes. The underlying mechanism can be used as well to check pre/post conditions, attribute access,... The basic point is, that the actual class must not be changed in any way to achive the desired effect. I had to adapt the code to some of my needs, but now it's almost working, by simply defining a metaclass for any of my class, I get the class methods calls logged with the instance that called the method, the method name, the call line in the file, the parameter **value** passed to the method and the returned value. It just saved me from hours of ipython interactive debugging with the old print way, and to beauty of it: I don't have to write any "self._log('Please log me')" line. As it is so useful, I bet there is a module that is doing this in a more reliable way that I did. Anyone has heard of it ? Jean-Michel From jhinak.sen at gmail.com Fri Jul 10 05:41:03 2009 From: jhinak.sen at gmail.com (jhinak sen) Date: Fri, 10 Jul 2009 15:11:03 +0530 Subject: help me to find the error Message-ID: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> hi, i am a beginner in python language, i am trying with this programme : to find the addition and mean from a data set in a file and writing the mean and sum in some other file : "" *#! /usr/bin/env python import re import cPickle as p import math from numpy import * f0= open("temp9","r+").readlines() f2= open("out1","r+") add_1=[ ]; for i in range(0, len(f0)): f1=f0[i].split() add= float(f1[1])+float(f1[2]) mean= float(add)/2 print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), repr(mean).ljust(7) add_1.append(add) add_1.append(mean) f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % repr(mean).ljust(7)) print "printing from file" for i in range(0, len(add_1),2): print add_1[i]," ", add_1[i+1] f0.close() f2.close()* "" and this programme is givving me this error : "" *Traceback (most recent call last): File "./temporary1.py", line 24, in f0.close() AttributeError: 'list' object has no attribute 'close'* "" please help to to find the error. or suggest some simpler or better way note: 1)file "temp9" is already exist 2)this programme is giving me all my outputs, but at the end of the out ..its giving me that error. -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 10 05:53:15 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 10 Jul 2009 17:53:15 +0800 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: <2d56febf0907100253m55cca65eo1889431de5b9835d@mail.gmail.com> I'm new to Python myself, but I think it's because the method readlist() returns a list. Hence, f0 is typed 'list'. And lists can't be closed. If you leave it to: *f0= open("temp9","r+") *and then assign another variable to the readlist, such as: *lines = f0.readlines()* Then in the for loop, change f1 to *f1=lines[i].split()* That may just stop it from complaining. Untested, but logically it's sound. Hope that works, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Fri, Jul 10, 2009 at 5:41 PM, jhinak sen wrote: > hi, > i am a beginner in python language, > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the > mean and sum in some other file : > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * > > f0= open("temp9","r+").readlines() > f2= open("out1","r+") > add_1=[ ]; > for i in range(0, len(f0)): > f1=f0[i].split() > add= float(f1[1])+float(f1[2]) > mean= float(add)/2 > print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), > repr(mean).ljust(7) > add_1.append(add) > add_1.append(mean) > f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % > repr(mean).ljust(7)) > print "printing from file" > for i in range(0, len(add_1),2): > print add_1[i]," ", add_1[i+1] > > f0.close() > f2.close()* > > > "" > > and this programme is givving me this error : > > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" > > please help to to find the error. > or suggest some simpler or better way > > note: > 1)file "temp9" is already exist > 2)this programme is giving me all my outputs, but at the end of the out > ..its giving me that error. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gabriel.rossetti at arimaz.com Fri Jul 10 05:57:23 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 10 Jul 2009 11:57:23 +0200 Subject: Threading.Condition problem Message-ID: <4A571083.8030403@arimaz.com> Hello everyone, I wrote a small example that listens for xmpp msgs in a thread. The main program calls a function that blocks (using Condition.wait) until a msg has been received and then returns the msg. When a msg arrives, it is put in a variable in the thread's object, it then calls the notify() attr on the Condition object. For some reason, this doesn't work, the thread gets the msg, tries to notify the Condition object, fails because the lock has not been acquired yet and blocks. I tried ignoring the failure, thinking that since it has not been acquired yet then when it is, it will get the msg right away and never call Condition.wait, thus not causing any problems, but this does not work either. Does someone know what I am doing wrong? I attached the code to this msg. Thank you, Gabriel -------------- next part -------------- A non-text attachment was scrubbed... Name: xmpp_client.py Type: text/x-python Size: 6127 bytes Desc: not available URL: From bearophileHUGS at lycos.com Fri Jul 10 05:58:33 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 10 Jul 2009 02:58:33 -0700 (PDT) Subject: psyco V2 beta2 benchmark References: Message-ID: larudwer, is that time_subdist subdist(i) a bad worsening? Something to be fixed in Psyco2? Bye, bearophile From sebastian.schabe at gmx.de Fri Jul 10 06:01:51 2009 From: sebastian.schabe at gmx.de (Sebastian Schabe) Date: Fri, 10 Jul 2009 12:01:51 +0200 Subject: Concatenating images (numpy arrays), but they look like HSV images In-Reply-To: References: <4a5629ba$1@news.fhg.de> Message-ID: <4a5710ea$1@news.fhg.de> Robert Kern schrieb: > Probably, you need to use zeros(..., dtype=uint8). When you use > dtype=int, that will result in dtype=int arrays. I suspect that > matplotlib is then interpreting that to mean that you want it to treat > the input as scalar data (which it will pass through a colormap) rather > than an RGB image. > Thanks Robert, that was exactly the problem. Now I'am really wondering how one can know such details. Well, with your answer I searched again in the mathplotlib documentation and under the function imshow(X, ...) I indeed found the hint, that X has to be an uint8 or float array or PIL image, but before I hadn't known where to search. So again, thank you Sebastian From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 10 06:14:26 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 10 Jul 2009 12:14:26 +0200 Subject: property using a classmethod In-Reply-To: References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> <4a55e8e0$0$8041$426a34cc@news.free.fr> Message-ID: <4a571483$0$19592$426a74cc@news.free.fr> Lie Ryan a ?crit : > Bruno Desthuilliers wrote: >> Lie Ryan a ?crit : >>> Emanuele D'Arrigo wrote: >> (snip) >>>> Ultimately all I want is a non-callable class-level attribute >>>> MyClass.myProperty that gives the result of MyClass.myClassMethod(). >>> This works like what you seem to want (it's ugly): >> Ugly, indeed. And an extreme case of arbitrary overcomplexification too :-/ >> >> (snip rube goldberg code) >> > > Can't think of anything simpler than that without meddling with > descriptor. Hmmm... Rereading the OP's spec, I guess you understood it better than I did - seems the OP wants to be able to call the "property" on the class object itself - which won't work with the builtin property type. So my own proposed solution won't do :-/ But still, "meddling with descriptor" is *way* simpler than your proposed solution. Here's a simple non-binding descriptor that do the job: class ClsProperty(object): def __init__(self, fget): if not isinstance(fget, (classmethod, staticmethod)): raise ValueError( "fget must be a classmethod or staticmethod" ) self.fget = fget def __get__(self, obj, cls=None): if cls is None: assert obj is not None cls = type(obj) return self.fget.__get__(obj, cls)() # example use class Foo(object): @classmethod def bar(cls): return "%s.bar" % cls.__name__ quux = ClsProperty(bar) > I'm not even sure descriptor can help here as it seems > descriptor needs an instance? wrt/ descriptors, no, they don't "need" an instance, at least for non-binding descriptors. Else, MyClass.MyMethod would return the MyClass.__dict__['MyMethod'] function, not an unbound method object !-) From jhinak.sen at gmail.com Fri Jul 10 06:20:47 2009 From: jhinak.sen at gmail.com (jhinak sen) Date: Fri, 10 Jul 2009 15:50:47 +0530 Subject: Python-list Digest, Vol 70, Issue 142 In-Reply-To: References: Message-ID: <3d4ff96c0907100320s346403baga8795d0c61cd54d7@mail.gmail.com> hey Xavier thnx a lot :) its working and ya.. i got your pont regarding typecasting . jhinak On Fri, Jul 10, 2009 at 3:30 PM, wrote: > 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. A zlib question (Roland Hedberg) > 2. Re: help me to find the error (Xavier Ho) > 3. Re: Concatenating images (numpy arrays), but they look like > HSV images (Sebastian Schabe) > > > ---------- Forwarded message ---------- > From: Roland Hedberg > To: python-list at python.org > Date: Fri, 10 Jul 2009 11:33:25 +0200 > Subject: A zlib question > Hi! > > I have a problem with zlib and compressing/decompressing according to RFC > 1951. > > It seems like I can decompress, something compressed according to RFC 1951 > by someone else, provided I set wbits to something negative (used -8 but I > guess any negative number would work?). > > But how can I compress using zlib so it doesn't add a gzip header ? > > -- Roland > > > ---------- Forwarded message ---------- > From: Xavier Ho > To: python-list at python.org > Date: Fri, 10 Jul 2009 17:53:15 +0800 > Subject: Re: help me to find the error > I'm new to Python myself, but I think it's because the method readlist() > returns a list. Hence, f0 is typed 'list'. And lists can't be closed. > > If you leave it to: > > *f0= open("temp9","r+") > > *and then assign another variable to the readlist, such as: > > *lines = f0.readlines()* > > Then in the for loop, change f1 to > > *f1=lines[i].split()* > > That may just stop it from complaining. > > Untested, but logically it's sound. > > Hope that works, > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > > On Fri, Jul 10, 2009 at 5:41 PM, jhinak sen wrote: > >> hi, >> i am a beginner in python language, >> >> i am trying with this programme : >> to find the addition and mean from a data set in a file and writing the >> mean and sum in some other file : >> "" >> *#! /usr/bin/env python >> >> import re >> import cPickle as p >> import math >> from numpy import * >> >> f0= open("temp9","r+").readlines() >> f2= open("out1","r+") >> add_1=[ ]; >> for i in range(0, len(f0)): >> f1=f0[i].split() >> add= float(f1[1])+float(f1[2]) >> mean= float(add)/2 >> print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), >> repr(mean).ljust(7) >> add_1.append(add) >> add_1.append(mean) >> f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % >> repr(mean).ljust(7)) >> print "printing from file" >> for i in range(0, len(add_1),2): >> print add_1[i]," ", add_1[i+1] >> >> f0.close() >> f2.close()* >> >> >> "" >> >> and this programme is givving me this error : >> >> "" *Traceback (most recent call last): >> File "./temporary1.py", line 24, in >> f0.close() >> AttributeError: 'list' object has no attribute 'close'* >> "" >> >> please help to to find the error. >> or suggest some simpler or better way >> >> note: >> 1)file "temp9" is already exist >> 2)this programme is giving me all my outputs, but at the end of the out >> ..its giving me that error. >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > > ---------- Forwarded message ---------- > From: Sebastian Schabe > To: python-list at python.org > Date: Fri, 10 Jul 2009 12:01:51 +0200 > Subject: Re: Concatenating images (numpy arrays), but they look like HSV > images > Robert Kern schrieb: > >> Probably, you need to use zeros(..., dtype=uint8). When you use dtype=int, >> that will result in dtype=int arrays. I suspect that matplotlib is then >> interpreting that to mean that you want it to treat the input as scalar data >> (which it will pass through a colormap) rather than an RGB image. >> >> > Thanks Robert, that was exactly the problem. Now I'am really wondering how > one can know such details. Well, with your answer I searched again in the > mathplotlib documentation and under the function imshow(X, ...) I indeed > found the hint, that X has to be an uint8 or float array or PIL image, but > before I hadn't known where to search. > > So again, thank you > > Sebastian > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 10 06:38:49 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 10 Jul 2009 12:38:49 +0200 Subject: property using a classmethod In-Reply-To: <4a55db9e$0$10243$426a74cc@news.free.fr> References: <7a23c6d9-509e-4662-bcb1-1923d893d057@g31g2000yqc.googlegroups.com> <4a55db9e$0$10243$426a74cc@news.free.fr> Message-ID: <4a571a39$0$29561$426a74cc@news.free.fr> Bruno Desthuilliers a ?crit : (snip) > You could write your own custom descriptor. Or just use an additional > level of indirection, ie: > > myProperty = property(lambda self: self.myClassMethod()) > Sorry, looks like I didn't read carefully enough. The above code won't work if you intend to lookup the property directly on the class object, ie "MyClass.myProperty". If that was your intention, you'll need a custom descriptor. The following code should do the job, or at least get you started: # python 2.5.x # the custom (non binding) descriptor class ClsProperty(object): def __init__(self, fget): if not isinstance(fget, (classmethod, staticmethod)): # XXX better error message raise ValueError( "fget must be a classmethod or staticmethod" ) self.fget = fget def __get__(self, obj, cls=None): if cls is None: assert obj is not None cls = type(obj) return self.fget.__get__(obj, cls)() # helper -> a simple decorator def classproperty(func): if not isinstance(func, (classmethod, staticmethod)): func = classmethod(func) return ClsProperty(func) # example use class Foo(object): # the hard way @classmethod def bar(cls): return "%s.bar" % cls.__name__ quux = ClsProperty(bar) # the simple way @classproperty def baaz(cls): return "%s.baaz" % cls Given your example, this should be enough. If you need a binding descriptor (one with a setter), you'll have to implement the __set__ method (and possibly __del__). Google for "python descriptor" to find more doc about the descriptor protocol. HTH From mail at microcorp.co.za Fri Jul 10 06:54:21 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 10 Jul 2009 12:54:21 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <001401ca014c$ca414d60$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > >>> persistent idea "out there" that programming is a very accessible >>> skill, like cooking or gardening, anyone can do it, and even profit >>> from it, monetarily or otherwise, etc., and to some extent I am >> >> Programming is not like any other human activity. > >In practice? In principle? Programming in principle is not the same as it >is performed in practice. > >But in either case, programming requires both the logical reasoning of >mathematics and the creativity of the arts. Funnily enough, I do not buy this arty creativity stuff. - or are you talking about making a website look pretty? >mathematicians will tell you that mathematics requires the same, and so >will the best artists. I think mathematicians, engineers, artists, even >great chefs, will pour scorn on your claim that programming is not like >any other human activity. So a chef is now an authority on programming? Programming is actually kind of different - almost everything else is just done, at the time that you do it. Programming is creating stuff that is completely useless until it is fed into something that uses it, to do something else, in conjuction with the thing it is fed into, at a later time. This is a highly significant difference, IMHO. > > >[...] >> He talks about how "when all is said and done, the only thing computers >> can do for us is to manipulate symbols and produce results of such >> manipulations" and he emphasises the "uninterpreted" nature of >> mechanical symbol manipulation, i.e. that the machine is doing it >> mindlessly. > >"Manipulate symbols" is so abstract as to be pointless. By that >reasoning, I can build a "computer" consisting of a box open at the top. >I represent a symbol by an object (say, a helium-filled balloon, or a >stone), instead of a pattern of bits. I manipulate the symbol by holding >the object over the box and letting go. If it flies up into the sky, that >represents the symbol "Love is War", if it falls into the box, it >represents the symbol "Strength is Blue", and if it just floats there, it >represents "Cheddar Cheese". This is a deterministic, analog computer >which manipulates symbols. Great. > >And utterly, utterly useless. So what is my computer lacking that real >computers have? When you have answered that question, you'll see why >Dijkstra's claim is under-specified. > So if computers do not manipulate symbols, what is it that they do? They sure cannot think, or drink, or reason, or almost any verb you can think of. "Manipulating symbols" is actually an elegant definition. Try coming up with a better one and you will see. And calling an abstraction pointless kind of contradicts what you say later... 8<------- camel humps and other stuff ------------------- - Hendrik From gabriel.rossetti at arimaz.com Fri Jul 10 07:09:28 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 10 Jul 2009 13:09:28 +0200 Subject: Threading.Condition problem In-Reply-To: <4A571083.8030403@arimaz.com> References: <4A571083.8030403@arimaz.com> Message-ID: <4A572168.90201@arimaz.com> The previous msg w/ attached code is the wrong code, please use the code attached to this msg, thank you and sorry for this. Gabriel Gabriel Rossetti wrote: > Hello everyone, > > I wrote a small example that listens for xmpp msgs in a thread. The > main program calls a function that blocks (using Condition.wait) until > a msg has been received and then returns the msg. When a msg arrives, > it is put in a variable in the thread's object, it then calls the > notify() attr on the Condition object. For some reason, this doesn't > work, the thread gets the msg, tries to notify the Condition object, > fails because the lock has not been acquired yet and blocks. I tried > ignoring the failure, thinking that since it has not been acquired yet > then when it is, it will get the msg right away and never call > Condition.wait, thus not causing any problems, but this does not work > either. Does someone know what I am doing wrong? I attached the code > to this msg. > > Thank you, > Gabriel -------------- next part -------------- A non-text attachment was scrubbed... Name: test_xmpp.py Type: text/x-python Size: 1878 bytes Desc: not available URL: From gabriel.rossetti at arimaz.com Fri Jul 10 07:09:42 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 10 Jul 2009 13:09:42 +0200 Subject: Threading.Condition problem Message-ID: <4A572176.9070100@arimaz.com> Sorry if this appears twice, I sent it once with an attachment and it never arrived so maybe the attachment is posing problems. I inlined the code this time (at the bottom), thank you, Gabriel ########################## Original message ############################ Hello everyone, I wrote a small example that listens for xmpp msgs in a thread. The main program calls a function that blocks (using Condition.wait) until a msg has been received and then returns the msg. When a msg arrives, it is put in a variable in the thread's object, it then calls the notify() attr on the Condition object. For some reason, this doesn't work, the thread gets the msg, tries to notify the Condition object, fails because the lock has not been acquired yet and blocks. I tried ignoring the failure, thinking that since it has not been acquired yet then when it is, it will get the msg right away and never call Condition.wait, thus not causing any problems, but this does not work either. Does someone know what I am doing wrong? I attached the code to this msg. Thank you, Gabriel ############################ Example code ############################ from __future__ import with_statement import xmpp, sys from threading import Thread, Condition, Event class Listener(Thread): def __init__(self, ws): Thread.__init__(self) self.interrupt = Event() self.message = None self._cv = ws._cv self.client = ws._client self.client.RegisterHandler('message', self.onMessage) def onMessage(self, conn, msg): self.message = msg try: self._cv.notify() except RuntimeError: print "self._cv has not acquired the lock yet" def getMsg(self): return self.message def run(self): try: while(not self.interrupt.isSet()): self.client.Process(1) except KeyboardInterrupt: return 0 class WS(object): def __init__(self, username, password, res): self._jid = xmpp.protocol.JID(username) self._client = xmpp.Client(self._jid.getDomain()) self._cv = Condition() if(self._client.connect(server=("localhost", 5222)) == ""): raise Exception("Error while connecting!") if(self._client.auth(self._jid.getNode(), password, res) is None): raise Exception("Authentication failed!") self._client.sendInitPresence() self._listener = Listener(self) self._listener.start() def getMsg(self, mid=None): """ """ with self._cv: res = self._listener.getMsg() while not res: self._cv.wait() res = self._listener.getMsg() return res if(__name__ == "__main__"): ws = WS("test at localhost", "123", "test") res = ws.getMsg() print "I just received : %s" % str(res) sys.exit(0) From ben+python at benfinney.id.au Fri Jul 10 07:30:41 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 10 Jul 2009 21:30:41 +1000 Subject: subprocess + python-daemon - bug/problem? References: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> <828acd8d-a9bd-4506-b695-e537af4587d8@l31g2000yqb.googlegroups.com> Message-ID: <873a94ojvi.fsf@benfinney.id.au> Andy Clegg writes: > "import daemon > import subprocess > > daemon.DaemonContext(stderr = open("fakeConsole.txt","w+")).open() > subprocess.Popen(['echo','1']).wait()" > > However the error remains the same. The error appears in the file specified for the stderr output of the DaemonContext. Here it is without the unwanted extra line-wrapping that seems to plague all Google Mail users (seriously, folks: get a real mail provider that won't mangle your messages):: Traceback (most recent call last): File "/home/bignose/Projects/python/python-daemon/bin/andy-clegg-test", line 7, in subprocess.Popen(['echo', '1']).wait() File "/usr/lib/python2.5/subprocess.py", line 1184, in wait pid, sts = self._waitpid_no_intr(self.pid, 0) File "/usr/lib/python2.5/subprocess.py", line 1014, in _waitpid_no_intr return os.waitpid(pid, options) OSError: [Errno 10] No child processes I confirm that I'm getting the same error; Python 2.5.4. My first thought was perhaps it's related to the fact that the process has no stdout? But setting ?stdout? and ?stderr? to the same file:: #! /usr/bin/python import daemon import subprocess fake_console = open("fake_console.txt", "w+") daemon.DaemonContext(stdout=fake_console, stderr=fake_console).open() subprocess.Popen(['echo', '1']).wait() still gives the same error:: 1 Traceback (most recent call last): File "/home/bignose/Projects/python/python-daemon/bin/andy-clegg-test", line 8, in subprocess.Popen(['echo', '1']).wait() File "/usr/lib/python2.5/subprocess.py", line 1184, in wait pid, sts = self._waitpid_no_intr(self.pid, 0) File "/usr/lib/python2.5/subprocess.py", line 1014, in _waitpid_no_intr return os.waitpid(pid, options) OSError: [Errno 10] No child processes I'm not familiar enough with the nuances of the ?subprocess? module to know what might be going wrong here. I'd like to know whether it might be a problem in the ?python-daemon? library. -- \ ?Whatever a man prays for, he prays for a miracle. Every prayer | `\ reduces itself to this: ?Great God, grant that twice two be not | _o__) four.?? ?Ivan Turgenev | Ben Finney From vox2000 at gmail.com Fri Jul 10 07:46:52 2009 From: vox2000 at gmail.com (vox) Date: Fri, 10 Jul 2009 04:46:52 -0700 (PDT) Subject: Query regarding set([])? Message-ID: Hi, I'm contsructing a simple compare-script and thought I would use set ([]) to generate the difference output. But I'm obviosly doing something wrong. file1 contains 410 rows. file2 contains 386 rows. I want to know what rows are in file1 but not in file2. This is my script: s1 = set(open("file1")) s2 = set(open("file2")) s3 = set([]) s1temp = set([]) s2temp = set([]) s1temp = set(i.strip() for i in s1) s2temp = set(i.strip() for i in s2) s3 = s1temp-s2temp print len(s3) Output is 119. AFAIK 410-386=24. What am I doing wrong here? BR, Andy From davea at ieee.org Fri Jul 10 07:48:49 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 07:48:49 -0400 Subject: can i write a assemly language programs in python In-Reply-To: References: <4A55F392.3080404@ieee.org> Message-ID: <4A572AA1.7010207@ieee.org> Terry Reedy wrote: >
Dave > Angel wrote: >> m.reddy prasad reddy wrote: >>> can any one tell me how to write assembly language programs in >>> python...if >>> no is there any other way to write the programs in python >>> >>> Reddi prasad reddy >>> ph.no:09958083797 >>> >>> >> Assembly language is a different programming language than Python. >> You can use both in the same process, much in the same way you can >> use C or C++ with Python. In fact, some of the system DLL's are >> written (partly) in assembler, though mostly in C or C++. > > It is possible that he meant how to write assembly *with* python. > Or how to translate python to assembly. > > As it turns out, CPython translates Python to byte code and has a dis > (assembly) module that produces very nice assembly code ;-) > So that is one answer to his question. > >> You'll need to tell us what your real goal is. > > Definitely. > > tjr > > >
> If you mean dis.dis() that only gives you byte code. I assumed he was referring to native code assembly. I guess I'd better stop guessing . DaveA From __peter__ at web.de Fri Jul 10 08:04:35 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 14:04:35 +0200 Subject: Query regarding set([])? References: Message-ID: vox wrote: > I'm contsructing a simple compare-script and thought I would use set > ([]) to generate the difference output. But I'm obviosly doing > something wrong. > > file1 contains 410 rows. > file2 contains 386 rows. > I want to know what rows are in file1 but not in file2. > > This is my script: > s1 = set(open("file1")) > s2 = set(open("file2")) Remove the following three lines: > s3 = set([]) > s1temp = set([]) > s2temp = set([]) > s1temp = set(i.strip() for i in s1) > s2temp = set(i.strip() for i in s2) > s3 = s1temp-s2temp > > print len(s3) > > Output is 119. AFAIK 410-386=24. What am I doing wrong here? You are probably misinterpreting len(s3). s3 contains lines occuring in "file1" but not in "file2". Duplicate lines are only counted once, and the order doesn't matter. So there are 119 lines that occur at least once in "file2", but not in "file1". If that is not what you want you have to tell us what exactly you are looking for. Peter From paul at boddie.org.uk Fri Jul 10 08:07:21 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Fri, 10 Jul 2009 05:07:21 -0700 (PDT) Subject: psyco V2 beta2 benchmark References: Message-ID: <35dcc79b-4f9d-4f16-942e-7cee2a470844@t13g2000yqt.googlegroups.com> On 10 Jul, 04:54, Zac Burns wrote: > Where do you get this beta? I heard that Psyco V2 is coming out but > can't find anything on their site to support this. I found the Subversion repository from the Psyco site: http://psyco.sourceforge.net/ -> http://codespeak.net/svn/psyco/dist/ -> http://codespeak.net/svn/psyco/v2/ It's not widely advertised, but I imagine that this is the correct repository. Navigating around on the codespeak.net site took me to the ViewVC instance which gives some date/time information that would confirm my suspicions: https://codespeak.net/viewvc/psyco/ A log of the development can be viewed here: https://codespeak.net/viewvc/psyco/v2/dist/?view=log Paul From mondi at cs.unibo.it Fri Jul 10 08:10:04 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Fri, 10 Jul 2009 12:10:04 +0000 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: Roger Binns wrote: > jacopo mondi wrote: >> Hi all, I need to patch socketmodule.c (the _socket module) in order to >> add support to an experimental socket family. > > You may find it considerably easier to use ctypes since that will avoid > the need for any patching. You'll also be able to control how read and > write are done (eg read vs recvfrom vs recvmsg vs readv). You can use > os.fdopen to convert your raw file descriptor into a Python file object > if appropriate. > > If you do use ctypes then you'll only need to distribute pure Python > source code. > > Roger Thanks a lot, I'll give it a try.. cheers jacopo From rhodri at wildebst.demon.co.uk Fri Jul 10 08:22:31 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 10 Jul 2009 13:22:31 +0100 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: On Fri, 10 Jul 2009 10:41:03 +0100, jhinak sen wrote: > hi, > i am a beginner in python language, > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the > mean > and sum in some other file : > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * > > f0= open("temp9","r+").readlines() > f2= open("out1","r+") > add_1=[ ]; > for i in range(0, len(f0)): > f1=f0[i].split() > add= float(f1[1])+float(f1[2]) > mean= float(add)/2 > print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), > repr(mean).ljust(7) > add_1.append(add) > add_1.append(mean) > f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % > repr(mean).ljust(7)) > print "printing from file" > for i in range(0, len(add_1),2): > print add_1[i]," ", add_1[i+1] > > f0.close() > f2.close()* > > > "" > > and this programme is givving me this error : > > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" As the error message tells you, 'f0' is a list. readlines() returns a list of the lines in the file object, rather than the file object itself. Since you promptly (and entirely reasonably) discard the file object, you haven't got any way to close() it. You don't actually need to do that, though. You can use a file object as something you use a 'for' loop over, and be given one line at a time. So instead you can write your code like this: f0 = open("temp9", "r") for line in f0: f1 = line.split() # ... and so on Notice that I only opened the file as "r" -- you are only reading it, not updating it, and giving yourself more permission than you need is a bad habit to get into. Opening the file this way means that if you accidentally write f0.write("something") instead of "f2.write(something)", Python will stop you with an exception rather than silently trash your data. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Fri Jul 10 08:25:49 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 10 Jul 2009 13:25:49 +0100 Subject: need to write a assembly progrm In-Reply-To: References: Message-ID: On Thu, 09 Jul 2009 11:52:44 +0100, m.reddy prasad reddy wrote: > my aim is to run the assembly programs in python.by that we can use that > in > the any labs.because we can run the python in the mobiles also.if we > write > assembly programs in the mobile ,the mobile act as a tool kit for the > lab.tell me any other solutions for this Please define what you mean by "assembly". -- Rhodri James *-* Wildebeest Herder to the Masses From mtnbikingmark at gmail.com Fri Jul 10 08:30:46 2009 From: mtnbikingmark at gmail.com (Mark) Date: Fri, 10 Jul 2009 05:30:46 -0700 (PDT) Subject: Examples of Python driven Microsoft UI Automation wanted References: <0e99f569-e5ab-42d5-8393-9918a7dc34cb@n11g2000yqb.googlegroups.com> <558d5613-ca3d-46ff-8dbe-0239f5acaad3@o7g2000yqb.googlegroups.com> Message-ID: <3aa3398a-b944-40fc-bb3b-2c2650d41454@26g2000yqk.googlegroups.com> Hi, I am the author of pywinauto (http://pywinauto.openqa.org/). It doesn't have specific functionality built in for winforms - but many winforms controls are built on top of win32 functionality and pywinauto can work with them for that. On Jul 9, 4:41?pm, Paul McGuire wrote: > On Jul 9, 1:50?pm, DuaneKaufman wrote: > > > > I have used pywinauto to drive a Flash game running inside of an > > > Internet Explorer browser - that's pretty GUI! > Wow - cool :) > > > -- Paul > > > Hi, > > > Could you share some code examples? > You might have a look at http://pywinauto.pbworks.com/ (the site is no longer used - but there is a cool movie giving an example of using pywinauto at the interactive prompt) Other examples (Notepad, mspaint) are included with the install. PyPi - http://pypi.python.org/pypi/pywinauto/0.3.8 > > Thanks, > > Duane (duanek (at) chorus (dot) net) > Good luck! :) Mark > I just went on a brief fishing expedition through two disk backups, > and no luck. ?I guess it's been a while since I worked on this. > > The work I did was entirely graphical, which is to say, my script > interacted with the Flash program by using PIL to take image snapshots > of the window, and then sifting through the bitmap looking for the > status of a "time remaining" thermometer-style gauge in the game. > Then the script could click on X-Y coordinates within the window, > which would get picked up by the Flash game, and the script would > monitor the "time remaining" gauge some more, and so on. > > I'm not sure how well pywinauto would work in allowing you to access > controls such as textboxes within a form. ?I remember that I had to > access the IE window using a caption name, and then found the embedded > Flash program as an embedded control of some sort, again, I probably > needed to indicate that it was some sort of "Adobe.FlashWidget1" > object or something. > > I may have another, older disk backup at home, I can look for it later > this evening. > > -- Paul From jeanmichel at sequans.com Fri Jul 10 08:32:52 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 10 Jul 2009 14:32:52 +0200 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: <4A5734F4.7010105@sequans.com> Steven D'Aprano wrote: > On Fri, 10 Jul 2009 02:06:35 +0000, Jason S. Friedman wrote: > > >> Hello, I administer the Informatica ETL tool at my company. Part of >> that role involves creating and enforcing standards. I want the >> Informatica developers to add comments to certain key objects and I want >> to be able to verify (in an automated fashion) that they have done so. >> >> I cannot merely check for non-emptiness; that is trivial to circumvent. >> On the other hand, I probably do not need to be able to catch >> developers who are determined to not create comments. There are not too >> many of them and perhaps they will find it is easier to write a (useful) >> comment than to game the system. >> >> Any thoughts on how I might proceed? Stated plainly, how can I tell >> when a string more-or-less forms at least one phrase? >> > > Define "phrase". > > > if len(s) > 0: > print "at least one character" > if len(s.split()) > 0: > print "at least one word" > if len(s.split('\n') > 0: > print "at least one line" > > > > You could also verify there are at least N different characters used in the sentence: N = 5 # must contains at least 5 different characters record = [] for c in s: if c not in record: record += [c] if len(record) >= N: print "at least %s different characters" % N Jean-Michel From python.list at tim.thechases.com Fri Jul 10 08:37:59 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jul 2009 07:37:59 -0500 Subject: can i write a assemly language programs in python In-Reply-To: References: Message-ID: <4A573627.8090308@tim.thechases.com> m.reddy prasad reddy wrote: > can any one tell me how to write assembly language programs in python...if > no is there any other way to write the programs in python Bah, writing assembly language is easy in Python: print("MOV EAX, [EBX]") print("XOR EBX, EBX") Just adjust the strings for your favorite processor architecture and Python will produce the assembly code you want. Now compiling assembly to *machine* code...or calling between Python and assembly...that's another matter. But writing assembly language programs in Python? Easy. ;-) -tkc From __peter__ at web.de Fri Jul 10 08:48:18 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 14:48:18 +0200 Subject: language analysis to enforce code standards References: Message-ID: Jason S. Friedman wrote: > Hello, I administer the Informatica ETL tool at my company. Part of > that role involves creating and enforcing standards. I want the > Informatica developers to add comments to certain key objects and I want > to be able to verify (in an automated fashion) that they have done so. > > I cannot merely check for non-emptiness; that is trivial to circumvent. > On the other hand, I probably do not need to be able to catch > developers who are determined to not create comments. There are not too > many of them and perhaps they will find it is easier to write a (useful) > comment than to game the system. > > Any thoughts on how I might proceed? Stated plainly, how can I tell > when a string more-or-less forms at least one phrase? Don't be a fool. Have someone other than the author read the comment. Peter From vox2000 at gmail.com Fri Jul 10 08:52:10 2009 From: vox2000 at gmail.com (vox) Date: Fri, 10 Jul 2009 05:52:10 -0700 (PDT) Subject: Query regarding set([])? References: Message-ID: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> On Jul 10, 2:04?pm, Peter Otten <__pete... at web.de> wrote: > You are probably misinterpreting len(s3). s3 contains lines occuring in > "file1" but not in "file2". Duplicate lines are only counted once, and the > order doesn't matter. > > So there are 119 lines that occur at least once in "file2", but not in > "file1". > > If that is not what you want you have to tell us what exactly you are > looking for. > > Peter Hi, Thanks for the answer. I am looking for a script that compares file1 and file2, for each line in file1, check if line is present in file2. If the line from file1 is not present in file2, print that line/write it to file3, because I have to know what lines to add to file2. BR, Andy From drobinow at gmail.com Fri Jul 10 09:10:14 2009 From: drobinow at gmail.com (David Robinow) Date: Fri, 10 Jul 2009 09:10:14 -0400 Subject: Query regarding set([])? In-Reply-To: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: <4eb0089f0907100610j3c53f4efjae119c9ec21f3992@mail.gmail.com> On Fri, Jul 10, 2009 at 8:52 AM, vox wrote: > I am looking for a script that compares file1 and file2, for each line > in file1, check if line is present in file2. If the line from file1 is > not present in file2, print that line/write it to file3, because I > have to know what lines to add to file2. Just copy file1 to file2. (I'm pretty sure that's not what you want, but in explaining why it should become clearer what you're trying to do.) From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 09:11:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 13:11:09 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <007318de$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 12:54:21 +0200, Hendrik van Rooyen wrote: > "Steven D'Aprano" wrote: > >>On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: >> >>>> persistent idea "out there" that programming is a very accessible >>>> skill, like cooking or gardening, anyone can do it, and even profit >>>> from it, monetarily or otherwise, etc., and to some extent I am >>> >>> Programming is not like any other human activity. >> >>In practice? In principle? Programming in principle is not the same as >>it is performed in practice. >> >>But in either case, programming requires both the logical reasoning of >>mathematics and the creativity of the arts. Funnily enough, > > I do not buy this arty creativity stuff. - or are you talking about > making a website look pretty? I must admit, it never crossed my mind that anyone here would claim that there was no creativity involved in programming, that it was all a mindless, algorithmic process capable of being done by a simple mechanical device. This is certainly the accusation made against *bad* programmers -- that they can't actually solve new, unique problems, but just apply recipes they learned without any insight or intelligence. The sort of people who program so poorly that "a trained monkey could do what they do". Do you really think that applies to good programmers too? If so, then a good code generator should be able to replace any programmer. Is that what you believe? >>mathematicians will tell you that mathematics requires the same, and so >>will the best artists. I think mathematicians, engineers, artists, even >>great chefs, will pour scorn on your claim that programming is not like >>any other human activity. > > So a chef is now an authority on programming? Did I say that? Chefs are authorities on OTHER HUMAN ACTIVITIES. > Programming is actually kind of different - almost everything else is > just done, at the time that you do it. > > Programming is creating stuff that is completely useless until it is fed > into something that uses it, to do something else, in conjuction with > the thing it is fed into, at a later time. Somebody should teach Hendrik that human beings have been creating TOOLS for hundreds of thousands of years. People have been creating tools to build tools for thousands of years. Software is just more of the same. Even *soup stock* fits the same profile as what Hendrik claims is almost unique to programming. On its own, soup stock is totally useless. But you make it, now, so you can you feed it into something else later on. Or instant coffee. No, Henrik, if that's the best you can do, it's not very good. It is rather sad, but also hilarious, that the most different thing you have noticed about software is that it's just like instant coffee. > This is a highly significant difference, IMHO. >>[...] >>> He talks about how "when all is said and done, the only thing >>> computers can do for us is to manipulate symbols and produce results >>> of such manipulations" and he emphasises the "uninterpreted" nature of >>> mechanical symbol manipulation, i.e. that the machine is doing it >>> mindlessly. >> >>"Manipulate symbols" is so abstract as to be pointless. By that >>reasoning, I can build a "computer" consisting of a box open at the top. >>I represent a symbol by an object (say, a helium-filled balloon, or a >>stone), instead of a pattern of bits. I manipulate the symbol by holding >>the object over the box and letting go. If it flies up into the sky, >>that represents the symbol "Love is War", if it falls into the box, it >>represents the symbol "Strength is Blue", and if it just floats there, >>it represents "Cheddar Cheese". This is a deterministic, analog computer >>which manipulates symbols. Great. >> >>And utterly, utterly useless. So what is my computer lacking that real >>computers have? When you have answered that question, you'll see why >>Dijkstra's claim is under-specified. >> >> > So if computers do not manipulate symbols, what is it that they do? Did I say they don't manipulate symbols? > They > sure cannot think, > or drink, > or reason, They can't reason? Then what are they doing when they manipulate symbols? Yet again, it didn't even cross my mind that somebody would make this claim. My entire point is that it's not enough to just "manipulate symbols", you have to manipulate symbols the correct way, following laws of logic, so that the computer can *mindlessly* reason. > or almost any verb you can think of. If you're going to take that argument, then I'll remind you that there are no symbols inside a computer. There are only bits. And in fact, there aren't even any bits -- there are only analog voltages, and analog magnetic fields. > "Manipulating symbols" is actually an elegant definition. Try coming up > with a better one and you will see. As I said above, it's not enough to just manipulate symbols. Here's a set of rules to manipulate symbols: X => 0 or in English, "Any symbol becomes the zero symbol". That's symbol manipulation. Utterly useless. This is why it's not enough to just manipulate symbols, you have to manipulate them THE RIGHT WAY. -- Steven From david250 at videotron.ca Fri Jul 10 09:14:50 2009 From: david250 at videotron.ca (David Bernier) Date: Fri, 10 Jul 2009 09:14:50 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: Musatov wrote: > Los Angeles (AP) --MeAmI.org now has users in 50 countries following > its adopted use in Pakistan. The search engine has grown in > popularity 10,000 fold following its Beta test launch three months ago > in April, 2009. Supporters of the site claim it is better than rival > Google upon which platform it is based. Controversy arose after > MeAmI.org search code allowed users to search other users Google > results with no advertising. "It is truly an innovative thing we are > doing," said Founder and CEO, Martin Musatov. "Letting users search > the results of other Google users immediately results in a level of > accuracy and relevance above and beyond Google." Google changed their > API following the launch or MeAmI.org and explored the possibility of > blocking site access from MeAmI.org to Google search results but was > unable to do so. Critics of MeAmI.org say what it is doing is > tantamount to intellectual theft. When asked about this topper Musatov > exclaimed, "The Internet was made for people, not companies." An > analyst at Goldman Sachs says, requesting to remain anonymous, > "MeAmI.org has a strong presence in promoting itself as a vehicle for > global activism and to tell you the truth, this makes it much more > likely an acquisition target than potential intellectual property > violator." Google could not be reached for comment. Mr. Musatov, is it you who removed the post quoted above from the Google Groups archive? Thank you. David Bernier From Nikolaus at rath.org Fri Jul 10 09:22:29 2009 From: Nikolaus at rath.org (Nikolaus Rath) Date: Fri, 10 Jul 2009 09:22:29 -0400 Subject: Implementing a cache Message-ID: <87ab3cwu3u.fsf@vostro.rath.org> Hello, I want to implement a caching data structure in Python that allows me to: 1. Quickly look up objects using a key 2. Keep track of the order in which the objects are accessed (most recently and least recently accessed one, not a complete history) 3. Quickly retrieve and remove the least recently accessed object. Here's my idea for the implementation: The objects in the cache are encapsulated in wrapper objects: class OrderedDictElement(object): __slots__ = [ "next", "prev", "key", "value" ] These wrapper objects are then kept in a linked lists and in an ordinary dict (self.data) in parallel. Object access then works as follows: def __setitem__(self, key, value): if key in self.data: # Key already exists, just overwrite value self.data[key].value = value else: # New key, attach at head of list with self.lock: el = OrderedDictElement(key, value, next=self.head.next, prev=self.head) self.head.next.prev = el self.head.next = el self.data[key] = el def __getitem__(self, key): return self.data[key].value To 'update the access time' of an object, I use def to_head(self, key): with self.lock: el = self.data[key] # Splice out el.prev.next = el.next el.next.prev = el.prev # Insert back at front el.next = self.head.next el.prev = self.head self.head.next.prev = el self.head.next = el self.head and self.tail are special sentinel objects that only have a .next and .prev attribute respectively. While this is probably going to work, I'm not sure if its the best solution, so I'd appreciate any comments. Can it be done more elegantly? Or is there an entirely different way to construct the data structure that also fulfills my requirements? I already looked at the new OrderedDict class in Python 3.1, but apparently it does not allow me to change the ordering and is therefore not suitable for my purpose. (I can move something to one end by deleting and reinserting it, but I'd like to keep at least the option of also moving objects to the opposite end). Best, -Nikolaus -- ?Time flies like an arrow, fruit flies like a Banana.? PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C From aahz at pythoncraft.com Fri Jul 10 09:50:29 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 06:50:29 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: [excessive quoting ahead, I'm too tired to trim] In article , kj wrote: >In aahz at pythoncraft.com (Aahz) writes: >>In article , kj wrote: >>> >>>OK, so, scratching from my original post the case >>> >>>. = >>> >>>(as being a special case of = ), still, >>>to the extent that I understand your post, the "=" in >>> >>> x = 1 >>> >>>means something fundamentally different (in terms of Python's >>>underlying implementation) from the "=" in >>> >>> y[0] = 1 >>> >>>No? >> >>No. ;-) > >No??? Just when I thought I finally understood all this! > >>What's different is not the ``=`` but the construction of the >>assignment target before ``=`` gets executed. > >Hmm. OK, I went to the link you posted in your other message >(http://docs.python.org/reference/simple_stmts.html#assignment-statements) >and I find this (my emphasis): > > Assignment of an object to a single target is recursively defined as follows. > > * If the target is an identifier (name): > > o If the name does not occur in a global statement in > the current code block: the name is bound to the object > ^^^^^^^^^^^^^^^^^ > in the current local namespace. > > o Otherwise: the name is bound to the object in the > ^^^^^^^^^^^^^^^^^ > current global namespace. > > The name is rebound if it was already bound. This may cause > the reference count for the object previously bound to the > name to reach zero, causing the object to be deallocated and > its destructor (if it has one) to be called. > > * If the target is a target list enclosed in parentheses or in > square brackets... (I'LL IGNORE THIS FOR NOW) > > * If the target is an attribute reference: The primary expression > in the reference is evaluated. It should yield an object with > assignable attributes; if this is not the case, TypeError is > raised. That object is then asked to assign the assigned > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > object to the given attribute; if it cannot perform the > ^^^^^^ > assignment, it raises an exception (usually but not necessarily > AttributeError). > > * If the target is a subscription: The primary expression in > the reference is evaluated. It should yield either a mutable > sequence object (such as a list) or a mapping object (such > as a dictionary). Next, the subscript expression is evaluated. > > If the primary is a mutable sequence object (such as a > list),... [CONDITIONS ON THE INDEX EXPRESSION OMITTED]... > the sequence is asked to assign the assigned object to its > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > item with that index.... > > If the primary is a mapping object (such as a dictionary),... > [CONDITIONS ON THE SUBSCRIPT EXPRESSION OMITTED]... the > ^^^ > mapping is then asked to create a key/datum pair which maps > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > the subscript to the assigned object. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > * If the target is a slicing: [INDEX STUFF OMITTED]... the > ^^^ > sequence object is asked to replace the slice with the items > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > of the assigned sequence... > ^^^^^^^^^^^^^^^^^^^^^^^^ > >OK, I originally interpreted what Lundh wrote in his two articles >that the "binding" described at the very beginning (i.e. when the >target is an identifier), which I take to make an entry or modify >such an entry in a namespace, is a *categorically different* >operation from the remaining operations underlined above. > >I interpreted Paul Boddie's correction in his response to me as >saying that the "assignment" mentioned for the case when the target >is an attribute reference is actually a special case of the >"assignment" to simple identifiers (i.e. it also means "binding"). > >But that still leaves all the other "assignments" (or the like) >underlined above. I don't think that the full definitions of these >remaining cases are covered by the same rule, even though the rule >is described as "recursive." I think that the writer has something >else in mind, and in particular, something *other* than binding, >but the author remains vague on exactly what this means. > >Clearly, both Lundh and the documentation draw some distinction >between "binding" and some other forms of "assignment" (which remain >ill-defined throughout). This distinction is what I was referring >to when I said that "=" means different things in different contexts. Consider this: x = 1 globals()['x'] = 1 locals()[1] = 1 What's the difference between the three? Although there's a lot of machinery amenable to manipulation, with the corresponding potential for breaking the standard model, fundamentally it all comes down to the intention that *some* piece of code establishes a target on the left-hand side and binds it to the object returned by the right-hand side. IME, except when going down to the very lowest-level details, Python is easiest to understand when you treat all assignment statements as working the same. It helps to remember that names and namespaces are in many ways syntactic sugar for dicts or lists. (Obviously, the example with locals() doesn't do what it purports to do, but it shows how function/method namespaces work under the covers.) Slicing is the one real exception, but again I think that it's easier to understand by wedging your mental model into conformance with the simple target/binding metaphor. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From davea at ieee.org Fri Jul 10 09:55:58 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 09:55:58 -0400 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: <4A57486E.9070900@ieee.org> jhinak sen wrote: > hi, > i am a beginner in python language, > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the mean > and sum in some other file : > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * > > f0= open("temp9","r+").readlines() > f2= open("out1","r+") > add_1=[ ]; > for i in range(0, len(f0)): > f1=f0[i].split() > add= float(f1[1])+float(f1[2]) > mean= float(add)/2 > print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), > repr(mean).ljust(7) > add_1.append(add) > add_1.append(mean) > f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % > repr(mean).ljust(7)) > print "printing from file" > for i in range(0, len(add_1),2): > print add_1[i]," ", add_1[i+1] > > f0.close() > f2.close()* > > > "" > > and this programme is givving me this error : > > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" > > please help to to find the error. > or suggest some simpler or better way > > note: > 1)file "temp9" is already exist > 2)this programme is giving me all my outputs, but at the end of the out > ..its giving me that error. > > Others have pointed out the specific problem that gives you this error. But I'd like to point out a few other things to consider: 1) Don't mix tabs and spaces. Best practice is to bind tab to (4) spaces in your editor, and never have a tab in a Python source file. 2) Think about your variable names. As it stands, f0 is a list of lines, f1 is a list of "word" within a line, and f2 is a file. No wonder you accidentally tried to close the list. I'd suggest things like: infile = open(....) lines = infile.readlines() outfile = open(....) for line in lines: words = line.split(" ") or even val1, val2 = lines.split(" ") Then of course the last two lines become infile.close() outfile.close() 3) Learn to use the for statement directly on a list, rather than using len() on the list to make an index, then using the index to find the value 4) On the open() calls, get your modes right. Looks like you really want infile = open(infilename, "r") outfile = open(outfilename, "w") 5) Consider using tuples in your add_1 list, rather than separate elements. That way, each element of the list would contain both sum and mean. add_1.append((add, mean)) and the final print would become for item in add_1: print item[0]," ", item[1] 6) Put anything over three lines into a function, instead of doing it at module scope. That way, you'll be clearer about what things are local to this code, and what might be useful to other code in the same module. In this case, infilename, and outfilename might be arguments to that function. There are lots of other refinements, but these are all within your reach, and would make the program much clearer. From usernet at ilthio.net Fri Jul 10 10:05:13 2009 From: usernet at ilthio.net (Tim Harig) Date: Fri, 10 Jul 2009 14:05:13 GMT Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On 2009-07-10, garabik-news-2005-05 at kassiopeia.juls.savba.sk wrote: > Tim Harig wrote: >> On 2009-07-09, Alex Rosslyn wrote: >>> I would like to learn a way of changing the colour of a particular >>> part of the output text. I've tried the following >> On Unix operating systems this would be done through the curses interface: >> http://docs.python.org/library/curses.html > Or using ANSI colour codes: Which will only work for ANSI terminals. From t.lehmann at rtsgroup.net Fri Jul 10 10:07:22 2009 From: t.lehmann at rtsgroup.net (Thomas Lehmann) Date: Fri, 10 Jul 2009 07:07:22 -0700 (PDT) Subject: Tkinter only: table widget with canvas... Message-ID: My intention is to keep dependencies low that means using python and tkinter as base package is quite easy because it's available on nearly every system. There is good documentation on Tkinter but going into the depth I'm missing many details. As an example... Using the Tkinter.Canvas class I should be able to create a nice table. The missing informations are: a) Assume I would have some different widgets to add per row. How do I get the maximum row height? b) Assume something like a label in one column. The length of all texts in a column will differ. How do I choose the maxium column width? c) Placing headers in a canvas does not look like a good idea because I don't want to scroll the headers. Am I right? c.1) How do I place a none scrollable header in a canvas? or c.2) How do I place all headers outside the canvas correctly above the relating column? best regards Thomas From davea at ieee.org Fri Jul 10 10:17:55 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 10:17:55 -0400 Subject: Query regarding set([])? In-Reply-To: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: <4A574D93.30103@ieee.org> vox wrote: > On Jul 10, 2:04 pm, Peter Otten <__pete... at web.de> wrote: > >> You are probably misinterpreting len(s3). s3 contains lines occuring in >> "file1" but not in "file2". Duplicate lines are only counted once, and the >> order doesn't matter. >> >> So there are 119 lines that occur at least once in "file2", but not in >> "file1". >> >> If that is not what you want you have to tell us what exactly you are >> looking for. >> >> Peter >> > > Hi, > Thanks for the answer. > > I am looking for a script that compares file1 and file2, for each line > in file1, check if line is present in file2. If the line from file1 is > not present in file2, print that line/write it to file3, because I > have to know what lines to add to file2. > > BR, > Andy > > > There's no more detail in that response. To the level of detail you provide, the program works perfectly. Just loop through the set and write the members to the file. But you have some unspecified assumptions: 1) order doesn't matter 2) duplicates are impossible in the input file, or at least not meaningful. So the correct output file could very well be smaller than either of the input files. And a few others that might matter: 3) the two files are both text files, with identical line endings matching your OS default 4) the two files are ASCII, or at least 8 bit encoded, using the same encoding (such as both UTF-8) 5) the last line of each file DOES have a trailing newline sequence From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 10:22:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 14:22:11 GMT Subject: Implementing a cache References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: <00732984$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 09:22:29 -0400, Nikolaus Rath wrote: > Hello, > > I want to implement a caching data structure in Python that allows me > to: > > 1. Quickly look up objects using a key 2. Keep track of the order in > which the objects are accessed (most > recently and least recently accessed one, not a complete history) > 3. Quickly retrieve and remove the least recently accessed object. Google for "python LRU cache". Here are the first three hits: http://code.activestate.com/recipes/498245/ http://code.activestate.com/recipes/252524/ http://www.algorithm.co.il/blogs/index.php/programming/python/small-python-challenge-no-2-lru-cache/ -- Steven From chris at simplistix.co.uk Fri Jul 10 10:26:00 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Fri, 10 Jul 2009 15:26:00 +0100 Subject: Where does setuptools live? In-Reply-To: <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> Message-ID: <4A574F78.9000801@simplistix.co.uk> Inky 788 wrote: > Currently, distutils itself is being actively developed. More info > about this here: http://tarekziade.wordpress.com/ > > My (albeit anonymous) advice is: use distutils. Manually download > packages as-needed from PyPI and install manually using standard > distutils. No thanks. I'm a big fan of buildout. Making it possible for packages to specify their dependencies is a big win... Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From vox2000 at gmail.com Fri Jul 10 10:28:04 2009 From: vox2000 at gmail.com (vox) Date: Fri, 10 Jul 2009 07:28:04 -0700 (PDT) Subject: Query regarding set([])? References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: On Jul 10, 4:17?pm, Dave Angel wrote: > vox wrote: > > On Jul 10, 2:04 pm, Peter Otten <__pete... at web.de> wrote: > > >> You are probably misinterpreting len(s3). s3 contains lines occuring in > >> "file1" but not in "file2". Duplicate lines are only counted once, and the > >> order doesn't matter. > > >> So there are 119 lines that occur at least once in "file2", but not in > >> "file1". > > >> If that is not what you want you have to tell us what exactly you are > >> looking for. > > >> Peter > > > Hi, > > Thanks for the answer. > > > I am looking for a script that compares file1 and file2, for each line > > in file1, check if line is present in file2. If the line from file1 is > > not present in file2, print that line/write it to file3, because I > > have to know what lines to add to file2. > > > BR, > > Andy > > There's no more detail in that response. ?To the level of detail you > provide, the program works perfectly. ?Just loop through the set and > write the members to the file. > > But you have some unspecified assumptions: > ? ? 1) order doesn't matter > ? ? 2) duplicates are impossible in the input file, or at least not > meaningful. ?So the correct output file could very well be smaller than > either of the input files. > > And a few others that might matter: > ? ? 3) the two files are both text files, with identical line endings > matching your OS default > ? ? 4) the two files are ASCII, or at least 8 bit encoded, using the > same encoding ?(such as both UTF-8) > ? ? 5) the last line of each file DOES have a trailing newline sequence Thanks all for the input! I have guess I have to think it through a couple times more. :) BR, Andy From digitig at gmail.com Fri Jul 10 10:38:54 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 10 Jul 2009 15:38:54 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: 2009/7/9 kj : > Thanks for the encouragement. [snip] > into code. ?And by this I mean not only assumptions about the > correctness of their code (the typical scope of assertions), but > also, more broadly, assumptions about the data that they are dealing > with (which often comes from external sources with abysmal quality > control). There we diverge. A lot. If "correctness of the code trumps everything else" (in fact, if it matters at all) and the external data has "abysmal quality control" then it *must* be checked for correctness before it is used. If it is not, you have no idea whether your output is correct or not. And assertions *will* *not* reliably provide that checking (because they may not be executed). You *must* actively check the data, using good old-fasioned "if" statements and so on, because not to do so is to declare that you *don't* care about correctness. You *know* the input is often wrong, but you're not bothering to check it? -- Tim Rowe From __peter__ at web.de Fri Jul 10 10:59:53 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 16:59:53 +0200 Subject: Query regarding set([])? References: <3b50124d-8f92-43fb-a784-c8516c22579d@h18g2000yqj.googlegroups.com> Message-ID: vox wrote: > On Jul 10, 4:17 pm, Dave Angel wrote: >> vox wrote: >> > On Jul 10, 2:04 pm, Peter Otten <__pete... at web.de> wrote: >> >> >> You are probably misinterpreting len(s3). s3 contains lines occuring >> >> in "file1" but not in "file2". Duplicate lines are only counted once, >> >> and the order doesn't matter. >> >> >> So there are 119 lines that occur at least once in "file2", but not in >> >> "file1". >> >> >> If that is not what you want you have to tell us what exactly you are >> >> looking for. >> >> >> Peter >> >> > Hi, >> > Thanks for the answer. >> >> > I am looking for a script that compares file1 and file2, for each line >> > in file1, check if line is present in file2. If the line from file1 is >> > not present in file2, print that line/write it to file3, because I >> > have to know what lines to add to file2. >> >> > BR, >> > Andy >> >> There's no more detail in that response. To the level of detail you >> provide, the program works perfectly. Just loop through the set and >> write the members to the file. >> >> But you have some unspecified assumptions: >> 1) order doesn't matter >> 2) duplicates are impossible in the input file, or at least not >> meaningful. So the correct output file could very well be smaller than >> either of the input files. >> >> And a few others that might matter: >> 3) the two files are both text files, with identical line endings >> matching your OS default >> 4) the two files are ASCII, or at least 8 bit encoded, using the >> same encoding (such as both UTF-8) >> 5) the last line of each file DOES have a trailing newline sequence > > Thanks all for the input! > I have guess I have to think it through a couple times more. :) Indeed. Note that others thinking through related problems have come up with http://docs.python.org/library/difflib.html Peter From mondi at cs.unibo.it Fri Jul 10 11:16:14 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Fri, 10 Jul 2009 15:16:14 +0000 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: Roger Binns wrote: > jacopo mondi wrote: >> Hi all, I need to patch socketmodule.c (the _socket module) in order to >> add support to an experimental socket family. > > You may find it considerably easier to use ctypes since that will avoid > the need for any patching. You'll also be able to control how read and > write are done (eg read vs recvfrom vs recvmsg vs readv). You can use > os.fdopen to convert your raw file descriptor into a Python file object > if appropriate. > > If you do use ctypes then you'll only need to distribute pure Python > source code. > > Roger Wait a minute, am I confused or ctypes seems to be unusless for my needs? Ctypes wraps a system shared object (as libc is) and gives you the possibility to access its functions. What have I to wrap? the fuctionalities I need to use are those exposed by the kernel via sys/socket.h haeder (socket, bind, connect etc.). Maybe I could wrapp _socket.so (I think it's possible) but seems to be unuseless because I need to modify, for example, the parameters that _socket.so passes to the socket() function in order to create a socket owned by the new socket family I'm actualy experimenting. Hope it's clear what I'm trying to explain... Thanks anyway, but ctypes doen't seem to fit my needs.. cheers jacopo From aahz at pythoncraft.com Fri Jul 10 11:19:48 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 08:19:48 -0700 Subject: PyGtk Depends on Numeric References: <1ebe9314-9434-459a-bd3e-2b2386a35f1b@n11g2000yqb.googlegroups.com> Message-ID: In article <1ebe9314-9434-459a-bd3e-2b2386a35f1b at n11g2000yqb.googlegroups.com>, dieter wrote: > >Get with the times people and port to numpy. :P >Don't you think its about time? Are you trying to get something to happen or just posting a random drive-by? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From francois.dot.grondin at bpr-cso.dot.com Fri Jul 10 11:20:48 2009 From: francois.dot.grondin at bpr-cso.dot.com (François Grondin) Date: Fri, 10 Jul 2009 15:20:48 GMT Subject: AP -- MeAmI.org Paces Google References: Message-ID: "David Bernier" a ?crit dans le message de news: h36ki102d7m at news5.newsguy.com... > Musatov wrote: >> On Jul 9, 7:54 pm, David Bernier wrote: >>> Musatov wrote: >>>> Los Angeles (AP) --MeAmI.org now has users in 50 countries following >>>> its adopted use in Pakistan. The search engine has grown in >>>> popularity 10,000 fold following its Beta test launch three months ago >>>> in April, 2009. Supporters of the site claim it is better than rival >>>> Google upon which platform it is based. Controversy arose after >>>> MeAmI.org search code allowed users to search other users Google >>>> results with no advertising. "It is truly an innovative thing we are >>>> doing," said Founder and CEO, Martin Musatov. "Letting users search >>>> the results of other Google users immediately results in a level of >>>> accuracy and relevance above and beyond Google." Google changed their >>>> API following the launch or MeAmI.org and explored the possibility of >>>> blocking site access from MeAmI.org to Google search results but was >>>> unable to do so. Critics of MeAmI.org say what it is doing is >>>> tantamount to intellectual theft. When asked about this topper Musatov >>>> exclaimed, "The Internet was made for people, not companies." An >>>> analyst at Goldman Sachs says, requesting to remain anonymous, >>>> "MeAmI.org has a strong presence in promoting itself as a vehicle for >>>> global activism and to tell you the truth, this makes it much more >>>> likely an acquisition target than potential intellectual property >>>> violator." Google could not be reached for comment. >>> Mr. Musatov, do you know who originally wrote the >>> article above? >>> >>> Thank you. >>> >>> David Bernier- Hide quoted text - >>> >>> - Show quoted text - >> >> Yes. > > Mr. Musatov, do you know the name of the person who > originally wrote the article above? > > Thank you. > > David Bernier Maybe Mr. Musatov should answer the following questions instead : 1. Did you write the article above? 2. If not, who did? And I don't want AP as the answer, but the name of the journalist. David, don't take it bad, but he answered your question with an accurate answer (yes), You gave him the opportunity to avoid the real answer and he took it. Based on Musatov's strange behavior and logic, don't expect more from him. Ask anyone else the same question and you'd get a real answer. BTW, my guess for question 2 would be the Masked Logician, Floetry, scriber77, or Professor X. Francois From aahz at pythoncraft.com Fri Jul 10 11:23:29 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 08:23:29 -0700 Subject: language analysis to enforce code standards References: Message-ID: In article , Jean-Michel Pichavant wrote: > >You could also verify there are at least N different characters used in >the sentence: > >N = 5 # must contains at least 5 different characters >record = [] >for c in s: > if c not in record: > record += [c] >if len(record) >= N: > print "at least %s different characters" % N Much simpler and *way* more efficient with a set: if len(set(s)) < N: print "Must have at least %s different characters" % N -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From Scott.Daniels at Acm.Org Fri Jul 10 11:28:29 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 08:28:29 -0700 Subject: tough-to-explain Python In-Reply-To: <007318de$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > Even *soup stock* fits the same profile as what Hendrik claims is almost > unique to programming. On its own, soup stock is totally useless. But you > make it, now, so you can you feed it into something else later on. > > Or instant coffee. I think I'll avoid coming to your house for a cup of coffee. :-) --Scott David Daniels Scott.Daniels at Acm.Org From magobin at gmail.com Fri Jul 10 11:33:18 2009 From: magobin at gmail.com (Alex) Date: Fri, 10 Jul 2009 08:33:18 -0700 (PDT) Subject: problem with keys combination! Message-ID: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Hi at all, I made a simple program that make a screenshot of Desktop and use it as fullscreen background and then a ball erase image making illusion that erase Desktop. The program working fine and I succesfully blocked all keys but I have a problem with hotkey combination Ctrl-Alt- Del...that bypass my FullScreen Application. Reading in google I understood that if I want disable this keys I have to operate in more low level. But I'm a newbe and I don't know how to make it(operate with api is very hard 4 me).I tried several days to find a workaround like replace one of that keys but doesn't work :-(( Plus..I can't install pyHook because there isn't a 2.6 version :-(( Can someone help me ?? thanks in advance Alex This is a piece of my code: while True: pg.event.pump() keyinput = pg.key.get_pressed() # press ESC to exit if keyinput[pg.K_ESCAPE] raise SystemExit if keyinput[pygame.K_LALT] and keyinput[pygame.K_LCTRL] and keyinput[pygame.K_DELETE]: win32api.keybd_event(win32con.VK_ESCAPE,0) #shell = win32com.client.Dispatch("WScript.Shell") #shell.SendKeys("{ESC}") #ignore keyboard input def IgnoreKeyboardInterrupt(): return signal.signal(signal.SIGINT,signal.SIG_IGN) From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 11:48:47 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 15:48:47 GMT Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <00733dd0$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 08:28:29 -0700, Scott David Daniels wrote: > Steven D'Aprano wrote: >> Even *soup stock* fits the same profile as what Hendrik claims is >> almost unique to programming. On its own, soup stock is totally >> useless. But you make it, now, so you can you feed it into something >> else later on. >> >> Or instant coffee. > > I think I'll avoid coming to your house for a cup of coffee. :-) I meant the instant coffee powder is prepared in advance. It's useless on it's own, but later on you feed it into boiling water, add sugar and milk, and it's slightly less useless. -- Steven From davea at ieee.org Fri Jul 10 11:57:21 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 11:57:21 -0400 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100718p5e48506cg2d133842972d6328@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> <4A57486E.9070900@ieee.org> <3d4ff96c0907100718p5e48506cg2d133842972d6328@mail.gmail.com> Message-ID: <4A5764E1.7010102@ieee.org> jhinak sen wrote: > hey, > thanx a lot :) > i got ur points .. and it really helps.. > > and please also tell me ... > where i can get more basic and detail knowledge of python.. as i am > beginners in this , i need more examples of python programmes so that i can > understand better. > also if you know of any gud pdf file or book please let me know > > thnx a lot > jhinak > > On Fri, Jul 10, 2009 at 7:25 PM, Dave Angel wrote: > > >> jhinak sen wrote: >> >> >>> hi, >>> i am a beginner in python language, >>> >>> i am trying with this programme : >>> to find the addition and mean from a data set in a file and writing the >>> mean >>> and sum in some other file : >>> "" >>> *#! /usr/bin/env python >>> >>> import re >>> import cPickle as p >>> import math >>> from numpy import * >>> >>> f0= open("temp9","r+").readlines() >>> f2= open("out1","r+") >>> add_1=[ ]; >>> for i in range(0, len(f0)): >>> f1=f0[i].split() >>> add= float(f1[1])+float(f1[2]) >>> mean= float(add)/2 >>> print (f1[1]).ljust(6) ,(f1[2]).ljust(6),repr(add).ljust(7), >>> repr(mean).ljust(7) >>> add_1.append(add) >>> add_1.append(mean) >>> f2.write("%s" % repr(add).ljust(7)),f2.write("%s" % >>> repr(mean).ljust(7)) >>> print "printing from file" >>> for i in range(0, len(add_1),2): >>> print add_1[i]," ", add_1[i+1] >>> >>> f0.close() >>> f2.close()* >>> >>> >>> "" >>> >>> and this programme is givving me this error : >>> >>> "" *Traceback (most recent call last): >>> File "./temporary1.py", line 24, in >>> f0.close() >>> AttributeError: 'list' object has no attribute 'close'* >>> "" >>> >>> please help to to find the error. >>> or suggest some simpler or better way >>> >>> note: >>> 1)file "temp9" is already exist >>> 2)this programme is giving me all my outputs, but at the end of the out >>> ..its giving me that error. >>> >>> >>> >>> >> Others have pointed out the specific problem that gives you this error. >> But I'd like to point out a few other things to consider: >> >> 1) Don't mix tabs and spaces. Best practice is to bind tab to (4) spaces >> in your editor, and never have a tab in a Python source file. >> 2) Think about your variable names. As it stands, f0 is a list of lines, >> f1 is a list of "word" within a line, and f2 is a file. No wonder you >> accidentally tried to close the list. I'd suggest things like: >> infile = open(....) >> lines = infile.readlines() >> outfile = open(....) >> >> for line in lines: >> words = line.split(" ") or even val1, val2 = >> lines.split(" ") >> >> Then of course the last two lines become >> infile.close() >> outfile.close() >> >> 3) Learn to use the for statement directly on a list, rather than using >> len() on the list to make an index, then using the index to find the value >> 4) On the open() calls, get your modes right. Looks like you really want >> infile = open(infilename, "r") >> outfile = open(outfilename, "w") >> 5) Consider using tuples in your add_1 list, rather than separate elements. >> That way, each element of the list would contain both sum and mean. >> add_1.append((add, mean)) >> >> and the final print would become >> >> for item in add_1: >> print item[0]," ", item[1] >> >> 6) Put anything over three lines into a function, instead of doing it at >> module scope. That way, you'll be clearer about what things are local to >> this code, and what might be useful to other code in the same module. >> In this case, infilename, and outfilename might be arguments to that >> function. >> >> There are lots of other refinements, but these are all within your reach, >> and would make the program much clearer. >> >> >> > > Please don' t top-post. Putting your reply out of order makes it harder for others to see the sequences of things. Some people top-post everything, but on this mailing list (and maybe most), the standard is to add to bottom, or inline where appropriate. Anyway, http://docs.python.org/tutorial/ http://diveintopython.org/ http://www.openbookproject.net/thinkCSpy/ are all good, depending on your experience with other languages, and with your computer's OS. You could also check out http://code.activestate.com/recipes/ or http://code.activestate.com/recipes/langs/python/ for Python specifically which has a large set of relatively small modules of code. For a examples that might stretch your thought process: http://code.activestate.com/recipes/576755/ http://code.activestate.com/recipes/576647/ From darcy at druid.net Fri Jul 10 12:09:28 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 10 Jul 2009 12:09:28 -0400 Subject: tough-to-explain Python In-Reply-To: <00733dd0$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> <00733dd0$0$9710$c3e8da3@news.astraweb.com> Message-ID: <20090710120928.075451c8.darcy@druid.net> On 10 Jul 2009 15:48:47 GMT Steven D'Aprano wrote: > I meant the instant coffee powder is prepared in advance. It's useless on > it's own, but later on you feed it into boiling water, add sugar and > milk, and it's slightly less useless. I don't know about that. I find instant coffee pretty useless no matter what it is fed to. :-) -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From bryanvick at gmail.com Fri Jul 10 12:13:41 2009 From: bryanvick at gmail.com (Bryan) Date: Fri, 10 Jul 2009 09:13:41 -0700 (PDT) Subject: Automate rsync w/ authentication Message-ID: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> I am trying to automate rsync to backup server A from server B. I have set up a private/public key between the two servers so I don't have to enter a password when using rsync. Running rsync manually with the following command works fine: rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp But when I try to do it with python, the subprocess simply returns the ssh -h output on stderr like I am passing some invalid syntax. What is wrong in my translation of rsync's -e command from shell to pythyon? #! /usr/bin/python from subprocess import Popen, PIPE rsyncExec = '/usr/bin/ssh' source = 'root at 10.0.45.67:/home/bry/jquery.lookup' dest = '/home/bry/tmp' rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] try: p = Popen(args, stdout=PIPE, stderr=PIPE) print 'rsync running with pid %s' % p.pid out, err = p.communicate() print 'Errors: %s' % err print 'Output: %s' % out except Exception: print 'Error running rsync' From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 12:14:04 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 16:14:04 GMT Subject: problem with keys combination! References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Message-ID: <007343bd$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 08:33:18 -0700, Alex wrote: > Hi at all, > I made a simple program that make a screenshot of Desktop and use it > as fullscreen background and then a ball erase image making illusion > that erase Desktop. The program working fine and I succesfully blocked > all keys but I have a problem with hotkey combination Ctrl-Alt- > Del...that bypass my FullScreen Application. What operating system are you using? Windows? As I understand it, you can't block, modify, or otherwise access Ctrl-Alt- Del while running under Windows: it is the "Secure Attention Key", and is designed to be virtually impossible to interfere with. It's not *quite* impossible, but it is the deepest, darkest black magic. Microsoft makes it close enough to impossible as makes no difference even for experienced developers. As a newbie, well, put it this way: it's like somebody saying "Hi guys, I have a shiny new Swiss Army Knife, the one with the screwdriver and the corkscrew. I'd like to build my own Space Shuttle -- what do I do?" http://stackoverflow.com/questions/886076/how-can-i-intercept-all-key-events-including-ctrlaltdel-and-ctrltab http://en.wikipedia.org/wiki/Control-Alt-Delete You should also read this: http://blogs.msdn.com/oldnewthing/archive/2004/02/16/73780.aspx -- Steven From mal at egenix.com Fri Jul 10 12:15:31 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Fri, 10 Jul 2009 18:15:31 +0200 Subject: DBI module deprecated at Python 2.5--what to use in its place? In-Reply-To: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: <4A576923.6050507@egenix.com> dana wrote: > I have a variety of Python 2.4 scripts that utilitize the DBI and ODBC > modules together. Although I don't have Python 2.5, I've been informed > the DBI module has been deprecated at 2.5. A few questions: > > 1) Although deprecated, will it work at all in 2.5? Does the fact that > it is deprecrated mean it has been removed entirely, or does Python > 2.5 simply issuing a warning? > > 2) What do I use in place of DBI for my Python 2.4. scripts that > import modules DBI and ODBC together. I don't use DBI directly. It was > simply a dependency for the ODBC module as best I knew. If you're looking for a stable and maintained ODBC for Python, have a look at our mxODBC extension or mxODBC Connect package: http://www.egenix.com/products/python/mxODBC/ http://www.egenix.com/products/python/mxODBCConnect/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 10 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From steve at REMOVE-THIS-cybersource.com.au Fri Jul 10 12:17:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 Jul 2009 16:17:09 GMT Subject: help me to find the error References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> <4A57486E.9070900@ieee.org> <3d4ff96c0907100718p5e48506cg2d133842972d6328@mail.gmail.com> Message-ID: <00734476$0$9710$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 11:57:21 -0400, Dave Angel wrote: [...] > Please don' t top-post. Putting your reply out of order makes it harder > for others to see the sequences of things. Some people top-post > everything, but on this mailing list (and maybe most), the standard is > to add to bottom, or inline where appropriate. Inline is nearly always appropriate. Please trim your replies, leaving only what you need for context and what you are replying to directly, not the entire every-growing collection of quoted-quoted-quoted-quoted-quotes. Thank you. -- Steven From mal at egenix.com Fri Jul 10 12:18:28 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Fri, 10 Jul 2009 18:18:28 +0200 Subject: Remoting over SSH In-Reply-To: <4A549B07.2090402@gmail.com> References: <4A549B07.2090402@gmail.com> Message-ID: <4A5769D4.8080106@egenix.com> Lucas Carvalho wrote: > Hussein B wrote: >> Hey, >> I want to perform commands on a remote server over SSH. >> What do I need? >> Thanks. >> > Hi, > If you want to use the SSH2 protocol into a python code, you should > take a look at this module: paramiko [1]. > > [1] http://www.lag.net/paramiko/ If you're looking for remote Python execution over SSH have a look at http://codespeak.net/py/dist/execnet.html -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 10 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From pdpinheiro at gmail.com Fri Jul 10 12:23:55 2009 From: pdpinheiro at gmail.com (pdpi) Date: Fri, 10 Jul 2009 09:23:55 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: On Jul 10, 2:11?pm, Steven D'Aprano wrote: > On Fri, 10 Jul 2009 12:54:21 +0200, Hendrik van Rooyen wrote: > > "Steven D'Aprano" wrote: > > >>On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > > >>>> persistent idea "out there" that programming is a very accessible > >>>> skill, like cooking or gardening, anyone can do it, and even profit > >>>> from it, monetarily or otherwise, etc., and to some extent I am > > >>> Programming is not like any other human activity. > > >>In practice? In principle? Programming in principle is not the same as > >>it is performed in practice. > > >>But in either case, programming requires both the logical reasoning of > >>mathematics and the creativity of the arts. Funnily enough, > > > I do not buy this arty creativity stuff. - or are you talking about > > making a website look pretty? > > I must admit, it never crossed my mind that anyone here would claim that > there was no creativity involved in programming, that it was all a > mindless, algorithmic process capable of being done by a simple > mechanical device. > > This is certainly the accusation made against *bad* programmers -- that > they can't actually solve new, unique problems, but just apply recipes > they learned without any insight or intelligence. The sort of people who > program so poorly that "a trained monkey could do what they do". I wholeheartedly agree. Coming up with Duff's device is nothing if not creative. My mind still reels at trying to grok it. http://www.lysator.liu.se/c/duffs-device.html > Even *soup stock* fits the same profile as what Hendrik claims is almost > unique to programming. On its own, soup stock is totally useless. But you > make it, now, so you can you feed it into something else later on. > > Or instant coffee. I've always found cooking an apt metaphor for programming. You've got your well-limited for loops (cook for x minutes), your less straightforward while/until loops (roast until golden), you have your subprocedures (prepare sauce in advance/in parallel), you have some conditionals (tenderize the steak if the meat isn't really that tender), etc etc. The complexities of assignment can be easily visualized in terms of containers and mixing stuff together. Nothing makes a += b more obvious than having a bowl of cream (a), an egg (b), and adding the egg to the bowl of cream (a += b). Well, except for the part where that in that case evaluating b is destructive ;) > They can't reason? Then what are they doing when they manipulate symbols? "Computers aren't intelligent. They only think they are." Or, more to the point: the typical definition of "reasoning" tends to involve more of what defines humans as self-aware, animate beings than what is usually ascribed to computers. From charles at declareSub.com Fri Jul 10 12:27:25 2009 From: charles at declareSub.com (Charles Yeomans) Date: Fri, 10 Jul 2009 12:27:25 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: On Jul 9, 2009, at 10:57 PM, Steven D'Aprano wrote: > On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > >> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: >> >>> Nobody says you shouldn't check your data. Only that "assert" is not >>> the right way to do that. >> >> "assert" is not the right way to check your *inputs*. It's a >> perfectly >> reasonable way to check data which "should" be valid, as well as a >> way >> to document what variables are supposed to contain. > > Where are those variables coming from? > > The distinction really boils down to this: > > * asserts should never fail. If there is any chance that an assertion > might fail outside of test suites, then don't use assert. > > > You can't control what input the caller provides to a function, so > unless > the data is generated inside the function, a caller might provide > something unexpected. Therefore, assert is inappropriate for checking > function parameters, even if that function is "private" and only > called > by your own functions. > > (1) So-called "private" or "internal" functions have a habit of > becoming > public, and then you have asserts in public functions. > > (2) If public() calls _private(x), and _private uses assert to check > the > value of x, there is a risk that if public() is buggy and supplies an > invalid x, the assertion will never be checked and _private() will > return > incorrect results. > > (3) assert is absolutely unsuitable for enforcing pre-conditions and > post- > conditions, unless such conditions are mere "guidelines", because > assert > can be switched off at runtime. Unless, of course, you want to switch off such checking at runtime, as you might when using a design-by-contract approach. Charles Yeomans From aahz at pythoncraft.com Fri Jul 10 12:33:06 2009 From: aahz at pythoncraft.com (Aahz) Date: Fri, 10 Jul 2009 09:33:06 -0700 Subject: BayPIGgies at OSCON: 7/23 8-9:30pm Message-ID: <20090710163306.GA10586@panix.com> NOTE: time change AND location change The July BayPIGgies meeting will be held at OSCON in the San Jose Convention Center as one of the BoF (Birds of a Feather) sessions from 8pm to 9:30pm Thursday July 23. Everyone is welcome: you do NOT need to be an OSCON member to attend a BoF. Wesley Chun will have a newbie-oriented "What is Python?" BoF from 7-8pm in the same room as BayPIGgies (we don't know which room yet). The July meeting is supposed to have a Django focus, but the program hasn't been settled yet, either. For more information, see http://baypiggies.net/ Discussion of details will take place on the BayPIGgies list: http://mail.python.org/mailman/listinfo/baypiggies -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From gabrielmonnerat at gmail.com Fri Jul 10 12:36:22 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Fri, 10 Jul 2009 13:36:22 -0300 Subject: problem with subprocess Message-ID: <4A576E06.5080006@gmail.com> Hi all, I need start a openoffice in Xvfb, but when I call with the DISPLAY occurs this error: [root at localhost oood]# Xvfb :99 -screen 0 1024x768x24 & In [9]: subprocess.call('/opt/ooo-dev3/program/soffice.bin') Out[9]: 0 In [10]: subprocess.call('DISPLAY=:99 /opt/ooo-dev3/program/soffice.bin') --------------------------------------------------------------------------- OSError Traceback (most recent call last) /home/gabriel/ in () /usr/lib/python2.6/subprocess.pyc in call(*popenargs, **kwargs) 442 retcode = call(["ls", "-l"]) 443 """ --> 444 return Popen(*popenargs, **kwargs).wait() 445 446 /usr/lib/python2.6/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags) 593 p2cread, p2cwrite, 594 c2pread, c2pwrite, --> 595 errread, errwrite) 596 597 # On Windows, you cannot just redirect one or two handles: You /usr/lib/python2.6/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) 1104 os.waitpid(self.pid, 0) 1105 child_exception = pickle.loads(data) -> 1106 raise child_exception 1107 1108 OSError: [Errno 2] No such file or directory I am using subprocess because I need store the pid. Any suggestions? thanks in advance, Gabriel M. Monnerat From inky788 at gmail.com Fri Jul 10 12:44:34 2009 From: inky788 at gmail.com (Inky 788) Date: Fri, 10 Jul 2009 09:44:34 -0700 (PDT) Subject: Where does setuptools live? References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> Message-ID: On Jul 10, 10:26?am, Chris Withers wrote: > Inky 788 wrote: > > Currently, distutils itself is being actively developed. More info > > about this here:http://tarekziade.wordpress.com/ > > > My (albeit anonymous) advice is: use distutils. Manually download > > packages as-needed from PyPI and install manually using standard > > distutils. > > No thanks. I'm a big fan of buildout. Making it possible for packages to > specify their dependencies is a big win... Yup, it's a big win. But package installation for Python is a bit of a mess right now. Neither setuptools nor buildout (nor pip for that matter) are a standard part of Python. It's rather silly that although Python is a batteries-included language, and it's mid-2009, and Python 3.1 has been released, that Python *still* doesn't have a standard built-in way to handle package installation (including dependencies and uninstallation). My guess is that once distutils finishes getting spruced up, some intrepid hacker is going to: * take the best parts of pip and the best parts of setuptools (I don't know anything about buildout), * stir vigorously, * ruthlessly remove the excess pieces, * write good documentation for it, * throw the result up on github/launchpad/bitbucket/whatever, and then *that's* what everyone's going to start using and which will eventually make it into the Python std lib. But that's just my anon 2 cents. From psimon at sonic.net Fri Jul 10 12:47:42 2009 From: psimon at sonic.net (Paul Simon) Date: Fri, 10 Jul 2009 09:47:42 -0700 Subject: tkinter problem References: <4a55292f$0$95538$742ec2ed@news.sonic.net> <4a5536d4$0$95520$742ec2ed@news.sonic.net> <4a5615ba$0$95523$742ec2ed@news.sonic.net> Message-ID: <4a5770bc$0$95549$742ec2ed@news.sonic.net> "David Smith" wrote in message news:h35f78$pts$1 at ruby.cit.cornell.edu... > Paul Simon wrote: >> "Peter Otten" <__peter__ at web.de> wrote in message >> news:h3481q$d95$00$1 at news.t-online.com... >>> Paul Simon wrote: >>> >>>> "Chris Rebert" wrote in message >>>> news:mailman.2863.1247095339.8015.python-list at python.org... >>>> On Wed, Jul 8, 2009 at 4:18 PM, Paul Simon wrote: >>>>> I have the "tkinter" problem and need some assistance to straighten it >>>>> out. >>>>> >From the web page "http://wiki.python.org/moin/TkInter" I tested as >>>>> >in >>>>>> "step >>>>> 1" and cannot import "_tkinter." I do not have that file on my >>>>> computer, >>>>> but >>>>> do have tkinter.py in /usr/local/lib/python2.6/lib-tk. as well as the >>>>> directories /usr/lib/tk8.5 and /usr/lib/tcl8.5. >>>>> This python stuff is great, but the documentation frequently >>>>> feels like it is just a bit out of my grasp. I realize that all of >>>>> this >>>>> is free but I understand the instructions on the web page to repair >>>>> only >>>>> to the >>>>> point of confusion. I'm not an expert. How do I modify my python >>>>> configuration? Is there a file that needs to be edited? Which setup.py >>>>> file >>>>> do I use? Make? or python setup.py build and python setup.py install? >>>>> Thanks. I appreciate your help. >>>> - How did you install Python? >>>> - What Linux distro are you using? >>>> >>>> Cheers, >>>> Chris >>>> http://blog.rebertia.com >>>> I"m using Mandriva 2008.1. I have to tell you honestly that I'm not >>>> sure >>>> exactly how I installed Python. Originally I had installed 2.5 from >>>> RPM >>>> but 2.6 was not available for my distro (2008.1) in RPM. I downloaded >>>> something from python.org and installed. Not sure if it was tarball or >>>> zip file. >>> Zip or tar doesn't matter, you are installing "from source". >>> >>> Python has to find the necessary include files for tcl/tk. These are in >>> separate packages that you have to install before you invoke Python's >>> configure script. >>> >>> I don't know what they are called on your system -- look for tk-dev.rpm, >>> tcl-dev.rpm or similar. >>> >>> You may run into the same problem with other modules like readline. >>> >>> Peter >>> >> >> Thank you Peter. I understand what you are saying but don't know how to >> do >> it. Although I installed from source, I followed a "cookbook" recipe. >> Could you tell me what files to execute, where they might be, and file >> arguments? I'm just ignorant, not stupid. ;-). >> >> Paul >> >> > > Just install the tkinter package from the Mandriva Linux Control > Center's Software Management system. I just did it, doing a search for > tkinter brought it right up. All done. > > --David Thanks to all for your patient help. I have made some progress, but still no success. I installed Active Tcl-8.5.7 and corrected the PATH accordingly. However I still get a "missing" message on building Python. "Failed to find the necessary bits (!) to build these modules: .... _tkinter (among others) To find the necessary bits, look in setup.py in detect_modules() for teh module's name." Not sure what bits are, euphemism? but am about to wipe the disk and reinstall linux, etc. Paul From gabrielmonnerat at gmail.com Fri Jul 10 12:48:50 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Fri, 10 Jul 2009 13:48:50 -0300 Subject: problem with subprocess In-Reply-To: <4A576E06.5080006@gmail.com> References: <4A576E06.5080006@gmail.com> Message-ID: <4A5770F2.7050308@gmail.com> gabrielmonnerat wrote: > Hi all, > > I need start a openoffice in Xvfb, but when I call with the DISPLAY > occurs this error: > > [root at localhost oood]# Xvfb :99 -screen 0 1024x768x24 & > > In [9]: subprocess.call('/opt/ooo-dev3/program/soffice.bin') > Out[9]: 0 > > In [10]: subprocess.call('DISPLAY=:99 /opt/ooo-dev3/program/soffice.bin') > --------------------------------------------------------------------------- > > OSError Traceback (most recent call > last) > > /home/gabriel/ in () > > /usr/lib/python2.6/subprocess.pyc in call(*popenargs, **kwargs) > 442 retcode = call(["ls", "-l"]) > 443 """ > --> 444 return Popen(*popenargs, **kwargs).wait() > 445 > 446 > > /usr/lib/python2.6/subprocess.pyc in __init__(self, args, bufsize, > executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, > env, universal_newlines, startupinfo, creationflags) > 593 p2cread, p2cwrite, > 594 c2pread, c2pwrite, > --> 595 errread, errwrite) > 596 > 597 # On Windows, you cannot just redirect one or two > handles: You > > > /usr/lib/python2.6/subprocess.pyc in _execute_child(self, args, > executable, preexec_fn, close_fds, cwd, env, universal_newlines, > startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, > c2pwrite, errread, errwrite) > 1104 os.waitpid(self.pid, 0) > 1105 child_exception = pickle.loads(data) > -> 1106 raise child_exception > 1107 > 1108 > > OSError: [Errno 2] No such file or directory > > I am using subprocess because I need store the pid. Any suggestions? Sorry, I was forgot the parameter shell=True. i.e In [20]: subprocess.call('DISPLAY=:99 /opt/ooo-dev3/program/soffice.bin',shell=True) lack of attention mine > > thanks in advance, > > Gabriel M. Monnerat > > Gabriel M. Monnerat From jcd at sdf.lonestar.org Fri Jul 10 12:50:27 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 12:50:27 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <1247244628.8029.15.camel@aalcdl07> On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: > On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > > > On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > > > >> Nobody says you shouldn't check your data. Only that "assert" is not > >> the right way to do that. > > > > "assert" is not the right way to check your *inputs*. It's a perfectly > > reasonable way to check data which "should" be valid, as well as a way > > to document what variables are supposed to contain. > > Where are those variables coming from? > > The distinction really boils down to this: > > * asserts should never fail. If there is any chance that an assertion > might fail outside of test suites, then don't use assert. > I'm no expert, but the more I read this thread, and the more I think on it, the more I believe that asserts don't really need to exist outside of test suites. The function that assertions provide is handled in a far more robust and maintainable way by unit tests and doctests. Anything else should be handled by more descriptive exceptions. The use of assertions in regular code may just be a historical baby step on the way to real code testing. To play devils advocate for a moment, one possible use case for assert statements is if you need to test something that you can't easily get under a proper unittest or doctest. Maybe you need to know the value of a variable halfway through a method. A judicious assertion can help supplement your test suite A better solution would be to refactor so you can get the needed value under test, but if time constraints won't allow it, then make your assertion and move on, but only to help you debug the code, not to protect your code at runtime. Even then, why not use a proper Exception (unless speed is a major issue)? Even if it is only used internally in a module, I would still prefer a ValueError to an AssertionError. It tells you what's happening more clearly. And it protects you if another caller (even one internal to the class) calls it with a different set of assumptions. At minimum, I think there's a heavy burden on an author to justify the use of AssertionErrors rather than other kinds of Exceptions. Cheers, Cliff From magobin at gmail.com Fri Jul 10 12:52:47 2009 From: magobin at gmail.com (Alex) Date: Fri, 10 Jul 2009 09:52:47 -0700 (PDT) Subject: problem with keys combination! References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> <007343bd$0$9710$c3e8da3@news.astraweb.com> Message-ID: <03eeae5c-fe58-41d1-91ea-f3e5dba602dc@h11g2000yqb.googlegroups.com> Hi Steven, > As I understand it, you can't block, modify, or otherwise access Ctrl-Alt- > Del while running under Windows: it is the "Secure Attention Key", and is > designed to be virtually impossible to interfere with. It's not *quite* > impossible, but it is the deepest, darkest black magic. Microsoft makes > it close enough to impossible as makes no difference even for experienced > developers. No, is possible but for my level is quite impossible: http://www.codeproject.com/KB/winsdk/AntonioWinLock.aspx ...in this article the author move a lot a functions in a dll and make a demonstration with a VB and C code to use it...plus...there is his program that make it :-)))) I thought that python can read dll via ctypes...but it's very hard for me :-(( > > As a newbie, well, put it this way: it's like somebody saying "Hi guys, I > have a shiny new Swiss Army Knife, the one with the screwdriver and the > corkscrew. I'd like to build my own Space Shuttle -- what do I do?" > :-))))))))))....but yes We caaaaannnnnnn Alex From robert.kern at gmail.com Fri Jul 10 12:57:16 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 Jul 2009 11:57:16 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: <1247244628.8029.15.camel@aalcdl07> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <1247244628.8029.15.camel@aalcdl07> Message-ID: On 2009-07-10 11:50, J. Cliff Dyer wrote: > On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: >> On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: >> >>> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: >>> >>>> Nobody says you shouldn't check your data. Only that "assert" is not >>>> the right way to do that. >>> "assert" is not the right way to check your *inputs*. It's a perfectly >>> reasonable way to check data which "should" be valid, as well as a way >>> to document what variables are supposed to contain. >> Where are those variables coming from? >> >> The distinction really boils down to this: >> >> * asserts should never fail. If there is any chance that an assertion >> might fail outside of test suites, then don't use assert. >> > > I'm no expert, but the more I read this thread, and the more I think on > it, the more I believe that asserts don't really need to exist outside > of test suites. Actually, there is a good argument that one shouldn't use an assert statement in test suites: code can have bugs that only show up under -O so you want to be able to run your test suite under -O. -- 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 inky788 at gmail.com Fri Jul 10 12:57:46 2009 From: inky788 at gmail.com (Inky 788) Date: Fri, 10 Jul 2009 09:57:46 -0700 (PDT) Subject: Where does setuptools live? References: <3f8cc929-557d-49ca-990e-69809f134171@t21g2000yqi.googlegroups.com> <4A52A055.8080108@simplistix.co.uk> <59968eb0-bfbb-4f38-aa72-26438e5bd6cd@h18g2000yqj.googlegroups.com> Message-ID: On Jul 10, 10:26?am, Chris Withers wrote: > Inky 788 wrote: > > Currently, distutils itself is being actively developed. More info > > about this here:http://tarekziade.wordpress.com/ > > > My (albeit anonymous) advice is: use distutils. Manually download > > packages as-needed from PyPI and install manually using standard > > distutils. > > No thanks. I'm a big fan of buildout. Making it possible for packages to > specify their dependencies is a big win... Read this: http://tarekziade.wordpress.com/2009/07/03/dropping-pep-386-versions-comparison/ So, again, I don't know anything about buildout, and it might be a nice interim solution, but there are some new and exciting developments in distutils coming down the pike, and whatever becomes the standard Python package management system will very likely be based on those new developments. I just hope it all happens sooner than later. :) From manuel.graune at koeln.de Fri Jul 10 13:00:11 2009 From: manuel.graune at koeln.de (Manuel Graune) Date: Fri, 10 Jul 2009 19:00:11 +0200 Subject: Nested Classes and Instances Message-ID: Hello, as an example of what I would like to achieve, think of a street where each house has a door and a sign with a unique (per house) number on it. I tried to model this like this: class House(object): class Door(object): def __init__(self,color): self.color=color class Sign(object): def __init__(self,text): self.text=text def __init__(self, doorcolor,housenumber): self.housenumber=housenumber self.door=House.Door(doorcolor) self.sign=House.Sign(housenumber) house1=House("red","1") house2=House("blue","2") Well, so far, so good. Now, what I'd like to achive is that the text of the "sign" changes whenever the variable "housenumber" of the "parent-instance" changes or that "house1.sign.text" is a reference/pointer to "house1.housenumber" Thanks in advance, Manuel -- A hundred men did the rational thing. The sum of those rational choices was called panic. Neal Stephenson -- System of the world http://www.graune.org/GnuPG_pubkey.asc Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99 From lists at cheimes.de Fri Jul 10 13:21:43 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 10 Jul 2009 19:21:43 +0200 Subject: problem with subprocess In-Reply-To: <4A5770F2.7050308@gmail.com> References: <4A576E06.5080006@gmail.com> <4A5770F2.7050308@gmail.com> Message-ID: gabrielmonnerat wrote: >> I am using subprocess because I need store the pid. Any suggestions? > Sorry, I was forgot the parameter shell=True. > i.e > In [20]: subprocess.call('DISPLAY=:99 > /opt/ooo-dev3/program/soffice.bin',shell=True) You should avoid using the shell=True parameter. It may result in security issues and it consumes more resources. Try this: env = os.environ.copy() env["DISPLAY"] = ":99" subprocess.call(['opt/ooo-dev3/program/soffice.bin', env=env) Christian From thudfoo at opensuse.us Fri Jul 10 13:24:22 2009 From: thudfoo at opensuse.us (member thudfoo) Date: Fri, 10 Jul 2009 10:24:22 -0700 Subject: can i write a assemly language programs in python In-Reply-To: <4A573627.8090308@tim.thechases.com> References: <4A573627.8090308@tim.thechases.com> Message-ID: <3d881a310907101024o7c03448cyc45103d54e7cc4ef@mail.gmail.com> On Fri, Jul 10, 2009 at 5:37 AM, Tim Chase wrote: > m.reddy prasad reddy wrote: >> >> can any one tell me how to write assembly language programs in python...if >> no is there any other way to write the programs in python > > Bah, writing assembly language is easy in Python: > > ?print("MOV EAX, [EBX]") > ?print("XOR EBX, EBX") > > Just adjust the strings for your favorite processor architecture and Python > will produce the assembly code you want. > > Now compiling assembly to *machine* code...or calling between Python and > assembly...that's another matter. ?But writing assembly language programs in > Python? ?Easy. ;-) > "PyASM is a full-featured dynamic assembler written entirely in Python. By dynamic, I mean that it can be used to generate and execute machine code in python at runtime without requiring the generation of object files and linkage. It essentially allow 'inline' assembly in python modules on x86 platforms. "PyASM can also generate object files (for windows) like a traditional standalone assembler, although you're probably better off using one of the many freely available assemblers if this is you primary goal." http://members.verizon.net/~olsongt/usersGuide.html From nleioatt at gmail.com Fri Jul 10 13:26:07 2009 From: nleioatt at gmail.com (Nick) Date: Fri, 10 Jul 2009 10:26:07 -0700 (PDT) Subject: gett error message: "TypeError: 'int' object is not callable" References: <7xab3d9ykx.fsf@ruckus.brouhaha.com> Message-ID: <04f36636-bf25-423e-8deb-d28d81511678@f16g2000vbf.googlegroups.com> On Jul 9, 8:22 pm, Paul Rubin wrote: > Nick writes: > > text = file.readlines() > > len = len(text) > > fields = text[1].split() > > Is that intended to split the first line of the file? Remember > that arrays in python begin at index 0. no the '1st line' is garbled meta data, but thanks man From __peter__ at web.de Fri Jul 10 13:37:15 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 10 Jul 2009 19:37:15 +0200 Subject: Nested Classes and Instances References: Message-ID: Manuel Graune wrote: > as an example of what I would like to achieve, think of a street > where each house has a door and a sign with a unique (per house) > number on it. I tried to model this like this: > > class House(object): > class Door(object): > def __init__(self,color): > self.color=color > class Sign(object): > def __init__(self,text): > self.text=text > def __init__(self, doorcolor,housenumber): > self.housenumber=housenumber > self.door=House.Door(doorcolor) > self.sign=House.Sign(housenumber) > > house1=House("red","1") > house2=House("blue","2") > > Well, so far, so good. Now, what I'd like to achive is that the text of > the "sign" changes whenever the variable "housenumber" of the > "parent-instance" changes or that > "house1.sign.text" is a reference/pointer to "house1.housenumber" Python doesn't support C-style pointers, but you can work around it to some degree: >>> class House(object): ... def __init__(self, housenumber): ... self.housenumber = housenumber ... self.sign = Sign(self) ... >>> class Sign(object): ... def __init__(self, house): ... self.house = house ... @property ... def text(self): return self.house.housenumber ... >>> house = House(42) >>> house.sign.text 42 >>> house.housenumber = "42b" >>> house.sign.text '42b' If you are concerned about the tight coupling between House and Sign you can modify Sign to accept a function that gets the housenumber: >>> class House(object): ... def __init__(self, n): self.housenumber = n ... >>> class Sign(object): ... def __init__(self, gettext): ... self._gettext = gettext ... @property ... def text(self): return self._gettext() ... >>> house = House(7) >>> house.sign = Sign(lambda house=house: house.housenumber) >>> house.sign.text 7 Peter From h at realh.co.uk Fri Jul 10 13:43:54 2009 From: h at realh.co.uk (Tony Houghton) Date: Fri, 10 Jul 2009 18:43:54 +0100 Subject: Running a script to build docs from setup.py References: <20090709173343.1784710d@realh.co.uk> <87fxd5ny79.fsf@benfinney.id.au> Message-ID: <20090710184354.406c26ae@realh.co.uk> On Fri, 10 Jul 2009 11:06:34 +1000 Ben Finney wrote: > Tony Houghton writes: > > > I've looked through the manual but I can't find any hooks in distutils > > for generating files at install time other than extension modules and > > .pyc files. Should I just run the script from somewhere in my setup.py > > before calling distutils' setup function? > > Indirectly related: Ian Bicking's article on using Python's ?setup.py? > as a ?Makefile? replacement: > > Thanks, but I don't think that adds much to the distutils manual. > If one is writing a ?setup.py? anyway, I think it makes sense to use > that as the build program for the whole project if possible. Good > hunting! Yes. Really I only want to write a setup.py because it makes it easier to make a debian package, but the more of the installation that's done by setup.py the better I suppose, then other people might find it useful. -- TH * http://www.realh.co.uk From wescpy at gmail.com Fri Jul 10 13:48:43 2009 From: wescpy at gmail.com (Wesley Chun) Date: Fri, 10 Jul 2009 10:48:43 -0700 (PDT) Subject: tough-to-explain Python References: Message-ID: <89727cd5-a039-4295-8641-c5fe60e8b50f@m18g2000vbi.googlegroups.com> On Jul 7, 1:04?pm, kj wrote: > I'm having a hard time coming up with a reasonable way to explain > certain things to programming novices. > : > How do I explain to rank beginners (no programming experience at > all) why x and y remain unchanged above, but not z? > : > What do you say to that? > > I can come up with much mumbling about pointers and stacks and > heaps and much hand-waving about the underlying this-and-that, but > nothing that sounds even remotely illuminating. > > Your suggestions would be much appreciated! kj, i don't have too much to add to everyone else's response except to describe how i deal with this. i teach Python courses several times a year and realized long ago that conveying the concept of mutable vs. immutable is a key to getting up-to-speed quickly with Python as well as helping beginners. so, although techically, this is more of an intermediate topic rather than "beginner" material, i still teach it anyway, with the hopes of producing better Python programmers out of the gate, and hopefully, less frustrated ones. in fact, i dedicated an entire chapter (4) in Core Python Programming just to address this important issue. to top it all off, i end this module in the class by giving 2 quizzes, just to make sure they understood what i just told them. i put the 1st one online, so if you're curious, the PDF is at http://roadkill.com/~wesc/cyberweb/introQuiz.pdf ... the 2nd quiz is harder and involves the discussion of the differences between shallow and deep copies. so yes, not very beginner- ish stuff, hence the reason why i (re)named my course "Intro +Intermediate Python". finally, rather than the "paper tag" or alex's hotel statue analogy, i just say that variables are like Post-It® or sticky notes on objects. i can tag objects anytime, tag objects more than once, remove tags, or switch them to another object, etc. just my $0.02, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From tony.lay at lmco.com Fri Jul 10 14:03:05 2009 From: tony.lay at lmco.com (Lay, Tony) Date: Fri, 10 Jul 2009 14:03:05 -0400 Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory Message-ID: <71417B877DBC6946AFC52F0C1576C7D6474D7B524B@HVXMSP1.us.lmco.com> Trying to build python-2.6.2 ./configure --prefix=/usr/local --exec-prefix=/usr/local LDFLAGS="-L/usr/local" (runs through happily, had to make some libs local) make runs most of the way until. building '_tkinter' extension gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c /tmp/meld/Python-2.6.2/Modules/_tkinter.c -o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/_tkinter.o gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c /tmp/meld/Python-2.6.2/Modules/tkappinit.c -o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/tkappinit.o gcc -pthread -shared build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/_tkinter.o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/Modules/tkappinit.o -L/usr/X11R6/lib64 -L/usr/X11R6/lib -L/usr/local/lib -ltk8.5 -ltcl8.5 -lX11 -o build/lib.linux-i686-2.6/_tkinter.so *** WARNING: renaming "_tkinter" since importing it failed: libtk8.5.so: cannot open shared object file: No such file or directory Failed to find the necessary bits to build these modules: _sqlite3 bsddb185 sunaudiodev To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: _tkinter running build_scripts # cd /usr/local/lib # ls -la | grep libtk8.5.so -r-xr-xr-x 1 root root 1112606 Jul 10 13:28 libtk8.5.so Am I missing something, it's there? Regards, Tony Lay UNIX Administration 407-306-6559 Lockheed Martin - EBS One Company, One Team -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Anthony (Tony) Lay (tony.lay at lmco.com).vcf Type: text/x-vcard Size: 430 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 7647 bytes Desc: not available URL: From digitig at gmail.com Fri Jul 10 14:29:26 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 10 Jul 2009 19:29:26 +0100 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: 2009/7/10 Peter Otten <__peter__ at web.de>: > Don't be a fool. Have someone other than the author read the comment. That's the winning answer as far as I'm concerned. Automated tools are good for picking up some types of accidental mistakes, but for checking that comments are meaningful (and variable names, for that matter) you can't do without peer review. Think about it. What's the purpose of "enforcing standards". Just a tick in some assurance box to say "we meet these standards"? Ot to ensure something about the product quality? No automated tool -- not for a while yet, anyway -- is going to pick up comments such as: # increment x x += 1 or # You are not expected to understand this. The former is the sort of thing that any programmer might produce when against a deadline and forced to comment their code. The latter is a classic from a programming guru of old. An automatic checker that just checks that the comment exists without understanding its contents simply is not adding value but is rather petty bureaucracy that will annoy the programmers. -- Tim Rowe From clp2 at rebertia.com Fri Jul 10 14:53:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 11:53:39 -0700 Subject: Automate rsync w/ authentication In-Reply-To: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: <50697b2c0907101153k69a6b5ej88fbd3a9993ed5d2@mail.gmail.com> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: > I am trying to automate rsync to backup server A from server B. ?I > have set up a private/public key between the two servers so I don't > have to enter a password when using rsync. ?Running rsync manually > with the following command works fine: > rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp > > But when I try to do it with python, the subprocess simply returns the > ssh -h output on stderr like I am passing some invalid syntax. ?What > is wrong in my translation of rsync's -e command from shell to > pythyon? > > #! /usr/bin/python > from subprocess import Popen, PIPE > rsyncExec = '/usr/bin/ssh' > source = 'root at 10.0.45.67:/home/bry/jquery.lookup' > dest = '/home/bry/tmp' > rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] Like many problems involving the subprocess module, I think you've tokenized the arguments incorrectly. Try: rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] Note that the -e switch and its operand are separate arguments for the purposes of POSIX shell tokenization. Cheers, Chris -- http://blog.rebertia.com From edreamleo at charter.net Fri Jul 10 14:54:50 2009 From: edreamleo at charter.net (Edward K Ream) Date: Fri, 10 Jul 2009 13:54:50 -0500 Subject: ANN: Leo 4.6 rc1 released Message-ID: Leo 4.6 rc1 is now available at: http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106 Leo is a text editor, data organizer, project manager and much more. See: http://webpages.charter.net/edreamleo/intro.html The highlights of Leo 4.6: -------------------------- - Cached external files *greatly* reduces the time to load .leo files. - Leo now features a modern Qt interface by default. Leo's legacy Tk interface can also be used. - New --config, --file and --gui command-line options. - Leo tests syntax of .py files when saving them. - Leo can now open any kind of file into @edit nodes. - @auto-rst nodes allow easy editing of reStructuredText files. - Properties of commanders, positions and nodes simplify programming. - Improved Leo's unit testing framework. - Leo now requires Python 2.5 or later. - Dozens of small improvements and bug fixes. Links: ------ Leo: http://webpages.charter.net/edreamleo/front.html Forum: http://groups.google.com/group/leo-editor Download: http://sourceforge.net/project/showfiles.php?group_id=3458 Bzr: http://code.launchpad.net/leo-editor/ Quotes: http://webpages.charter.net/edreamleo/testimonials.html -------------------------------------------------------------------- Edward K. Ream email: edreamleo at yahoo.com Leo: http://webpages.charter.net/edreamleo/front.html -------------------------------------------------------------------- From jcd at sdf.lonestar.org Fri Jul 10 14:56:48 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 14:56:48 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <1247244628.8029.15.camel@aalcdl07> Message-ID: <1247252208.11457.6.camel@aalcdl07> On Fri, 2009-07-10 at 11:57 -0500, Robert Kern wrote: > On 2009-07-10 11:50, J. Cliff Dyer wrote: > > On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: > >> On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: > >> > >>> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: > >>> > >>>> Nobody says you shouldn't check your data. Only that "assert" is not > >>>> the right way to do that. > >>> "assert" is not the right way to check your *inputs*. It's a perfectly > >>> reasonable way to check data which "should" be valid, as well as a way > >>> to document what variables are supposed to contain. > >> Where are those variables coming from? > >> > >> The distinction really boils down to this: > >> > >> * asserts should never fail. If there is any chance that an assertion > >> might fail outside of test suites, then don't use assert. > >> > > > > I'm no expert, but the more I read this thread, and the more I think on > > it, the more I believe that asserts don't really need to exist outside > > of test suites. > > Actually, there is a good argument that one shouldn't use an assert statement in > test suites: code can have bugs that only show up under -O so you want to be > able to run your test suite under -O. > That's an interesting point. Presumably TestCase.assert_() doesn't suffer from this defect? Otherwise the entire unittest suite is essentially broken by your argument. I suppose I should have said one should only raise AssertionErrors in test suites. Practically speaking, that's what a test suite is: The place where you assert what the code does. From jcd at sdf.lonestar.org Fri Jul 10 14:59:29 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 14:59:29 -0400 Subject: gett error message: "TypeError: 'int' object is not callable" In-Reply-To: <3A693537-927D-44B1-840A-D97FACFB6809@pyth.net> References: <3A693537-927D-44B1-840A-D97FACFB6809@pyth.net> Message-ID: <1247252369.11457.8.camel@aalcdl07> On Thu, 2009-07-09 at 13:53 +0000, Fri?rik M?r J?nsson wrote: > Look at: > > len = len(text) > > You're overriding `len` (a built-in method), with an integer > (`len(text)`). You then call: > > for i in range(len(fields)): > > But `len` is no longer a callable, but merely an integer. > > Regards, > Fri?rik M?r > > P.S. While this is a fairly obvious problem it's usually a good idea > to post working code and a traceback when requesting help. While we're on the subject of good question posting form: The body of your post should contain all relevant information. Please don't make readers look back at the subject heading for the statement of the problem. Duplicating the statement of the problem in the subject and the body is ok, but it really ought to be in the body. From Scott.Daniels at Acm.Org Fri Jul 10 14:59:50 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 11:59:50 -0700 Subject: tough-to-explain Python In-Reply-To: <00733dd0$0$9710$c3e8da3@news.astraweb.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> <00733dd0$0$9710$c3e8da3@news.astraweb.com> Message-ID: <4-CdnSJso5pwEMrXnZ2dnUVZ_sVi4p2d@pdx.net> Steven D'Aprano wrote: > On Fri, 10 Jul 2009 08:28:29 -0700, Scott David Daniels wrote: >> Steven D'Aprano wrote: >>> Even *soup stock* fits the same profile as what Hendrik claims is >>> almost unique to programming. On its own, soup stock is totally >>> useless. But you make it, now, so you can you feed it into something >>> else later on. >>> Or instant coffee. >> I think I'll avoid coming to your house for a cup of coffee. :-) > I meant the instant coffee powder is prepared in advance. It's useless on > it's own, but later on you feed it into boiling water, add sugar and > milk, and it's slightly less useless. I know, but the image of even a _great_ soup stock with instant coffee poured in, both appalled me and made me giggle. So, I thought I'd share. --Scott David Daniels Scott.Daniels at Acm.Org From robert.kern at gmail.com Fri Jul 10 15:02:58 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 10 Jul 2009 14:02:58 -0500 Subject: Clarity vs. code reuse/generality In-Reply-To: <1247252208.11457.6.camel@aalcdl07> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <1247244628.8029.15.camel@aalcdl07> <1247252208.11457.6.camel@aalcdl07> Message-ID: On 2009-07-10 13:56, J. Cliff Dyer wrote: > On Fri, 2009-07-10 at 11:57 -0500, Robert Kern wrote: >> On 2009-07-10 11:50, J. Cliff Dyer wrote: >>> On Fri, 2009-07-10 at 02:57 +0000, Steven D'Aprano wrote: >>>> On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote: >>>> >>>>> On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote: >>>>> >>>>>> Nobody says you shouldn't check your data. Only that "assert" is not >>>>>> the right way to do that. >>>>> "assert" is not the right way to check your *inputs*. It's a perfectly >>>>> reasonable way to check data which "should" be valid, as well as a way >>>>> to document what variables are supposed to contain. >>>> Where are those variables coming from? >>>> >>>> The distinction really boils down to this: >>>> >>>> * asserts should never fail. If there is any chance that an assertion >>>> might fail outside of test suites, then don't use assert. >>>> >>> I'm no expert, but the more I read this thread, and the more I think on >>> it, the more I believe that asserts don't really need to exist outside >>> of test suites. >> Actually, there is a good argument that one shouldn't use an assert statement in >> test suites: code can have bugs that only show up under -O so you want to be >> able to run your test suite under -O. >> > > That's an interesting point. Presumably TestCase.assert_() doesn't > suffer from this defect? Otherwise the entire unittest suite is > essentially broken by your argument. It explicitly raises AssertionErrors. It does not use the assert statement. > I suppose I should have said one > should only raise AssertionErrors in test suites. Practically speaking, > that's what a test suite is: The place where you assert what the code > does. Yup. -- 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 mgi820 at motorola.com Fri Jul 10 15:03:49 2009 From: mgi820 at motorola.com (Gary Duzan) Date: Fri, 10 Jul 2009 19:03:49 +0000 (UTC) Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: In article <3af970b1-b454-4d56-a33f-889ecfacacc5 at l28g2000vba.googlegroups.com>, Bryan wrote: > >rsyncExec = '/usr/bin/ssh' >source = 'root at 10.0.45.67:/home/bry/jquery.lookup' >dest = '/home/bry/tmp' >rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] I think you want -e and the ssh command to be separate args. Something like: rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] or: rshArgs = [ '-e', '/usr/bin/ssh -i /home/bry/keys/brybackup.key' ] args = [rsyncExec, '-a', '-v', '--dry-run'] + rshArgs + [ source, dest] Gary Duzan Motorola H&NM From clp2 at rebertia.com Fri Jul 10 15:11:12 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 12:11:12 -0700 Subject: Colour of output text In-Reply-To: References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: <50697b2c0907101211u5d6ddc80ra85857075c986217@mail.gmail.com> On Fri, Jul 10, 2009 at 2:23 AM, wrote: > Tim Harig wrote: >> On 2009-07-09, Alex Rosslyn wrote: >>> I would like to learn a way of changing the colour of a particular >>> part of the output text. I've tried the following > >> On Unix operating systems this would be done through the curses interface: >> >> http://docs.python.org/library/curses.html > > Or using ANSI colour codes: > > colours = { > ? ? ? ? ? ?'beep' ? ? ? : ? ?"\007", Sound is a color? Maybe if you have synaesthesia... Cheers, Chris -- http://blog.rebertia.com From walterbyrd at iname.com Fri Jul 10 15:22:15 2009 From: walterbyrd at iname.com (walterbyrd) Date: Fri, 10 Jul 2009 12:22:15 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? Message-ID: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> I believe Guido himself has said that all indentions should be four spaces - no tabs. Since backward compatibility is being thrown away anyway, why not enforce the four space rule? At least that way, when I get python code from somebody else, I would know what I am looking at, without having to do a hex dump, or something. From marty.musatov at gmail.com Fri Jul 10 15:24:27 2009 From: marty.musatov at gmail.com (Musatov) Date: Fri, 10 Jul 2009 12:24:27 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: Fran?ois Grondin wrote: > "David Bernier" a ?crit dans le message de news: > h36ki102d7m at news5.newsguy.com... > > Musatov wrote: > >> On Jul 9, 7:54 pm, David Bernier wrote: > >>> Musatov wrote: > >>>> Los Angeles (AP) --MeAmI.org now has users in 50 countries following > >>>> its adopted use in Pakistan. The search engine has grown in > >>>> popularity 10,000 fold following its Beta test launch three months ago > >>>> in April, 2009. Supporters of the site claim it is better than rival > >>>> Google upon which platform it is based. Controversy arose after > >>>> MeAmI.org search code allowed users to search other users Google > >>>> results with no advertising. "It is truly an innovative thing we are > >>>> doing," said Founder and CEO, Martin Musatov. "Letting users search > >>>> the results of other Google users immediately results in a level of > >>>> accuracy and relevance above and beyond Google." Google changed their > >>>> API following the launch or MeAmI.org and explored the possibility of > >>>> blocking site access from MeAmI.org to Google search results but was > >>>> unable to do so. Critics of MeAmI.org say what it is doing is > >>>> tantamount to intellectual theft. When asked about this topper Musatov > >>>> exclaimed, "The Internet was made for people, not companies." An > >>>> analyst at Goldman Sachs says, requesting to remain anonymous, > >>>> "MeAmI.org has a strong presence in promoting itself as a vehicle for > >>>> global activism and to tell you the truth, this makes it much more > >>>> likely an acquisition target than potential intellectual property > >>>> violator." Google could not be reached for comment. > >>> Mr. Musatov, do you know who originally wrote the > >>> article above? > >>> > >>> Thank you. > >>> > >>> David Bernier- Hide quoted text - > >>> > >>> - Show quoted text - > >> > >> Yes. > > > > Mr. Musatov, do you know the name of the person who > > originally wrote the article above? > > > > Thank you. > > > > David Bernier > > Maybe Mr. Musatov should answer the following questions instead : > 1. Did you write the article above? > 2. If not, who did? And I don't want AP as the answer, but the name of the > journalist. > > David, don't take it bad, but he answered your question with an accurate > answer (yes), You gave him the opportunity to avoid the real answer and he > took it. Based on Musatov's strange behavior and logic, don't expect more > from him. Ask anyone else the same question and you'd get a real answer. > > BTW, my guess for question 2 would be the Masked Logician, Floetry, > scriber77, or Professor X. > > Francois 1. Yes. From aahz at pythoncraft.com Fri Jul 10 15:26:14 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 12:26:14 -0700 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: In article <260f0f1f-1115-4db8-a955-74c9f459ecc3 at h30g2000vbr.googlegroups.com>, walterbyrd wrote: > >I believe Guido himself has said that all indentions should be four >spaces - no tabs. > >Since backward compatibility is being thrown away anyway, why not >enforce the four space rule? Probably the main reason is that Guido also hates non-functional code changes because it messes up version history. Not sure, though. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From kwmsmith at gmail.com Fri Jul 10 15:29:09 2009 From: kwmsmith at gmail.com (Kurt Smith) Date: Fri, 10 Jul 2009 14:29:09 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: On Fri, Jul 10, 2009 at 2:22 PM, walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. > > Since backward compatibility is being thrown away anyway, why not > enforce the four space rule? > > At least that way, when I get python code from somebody else, I would > know what I am looking at, without having to do a hex dump, or > something. What you propose has already been (forcefully) rejected: http://www.python.org/dev/peps/pep-0666/ From clp2 at rebertia.com Fri Jul 10 15:29:59 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 12:29:59 -0700 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: <50697b2c0907101229l720e9c92y94a921dc5c58de52@mail.gmail.com> On Fri, Jul 10, 2009 at 12:22 PM, walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. That's a (very good) recommendation at most. http://www.python.org/dev/peps/pep-0666/ Cheers, Chris -- http://blog.rebertia.com From tjreedy at udel.edu Fri Jul 10 15:30:04 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 15:30:04 -0400 Subject: help me to find the error In-Reply-To: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> References: <3d4ff96c0907100241p1f0a3b9eueef3157989f7e98@mail.gmail.com> Message-ID: jhinak sen wrote: > hi, > i am a beginner in python language, Welcome to Python. > > i am trying with this programme : > to find the addition and mean from a data set in a file and writing the > mean and sum in some other file : This is three things: input data from, perform calculation, output data to file. I would start simpler. See below. You did two things right that too many people do not do. 1. You posted a complete chunk of code. > "" > *#! /usr/bin/env python > > import re > import cPickle as p > import math > from numpy import * You never use these. > > f0= open("temp9","r+").readlines() [snip] > f0.close() > f2.close()* 2. You posted the complete traceback (instead of the annoying 'program didn't work ;-). > "" *Traceback (most recent call last): > File "./temporary1.py", line 24, in > f0.close() > AttributeError: 'list' object has no attribute 'close'* > "" > > please help to to find the error. Seeing that, any of us could tell you thought f0 was a file but it was actually a list. Looking back up through the code, people found the definition -- the output of file.readlines, which is a list. > or suggest some simpler or better way Develop more incrementally. If you edit with IDLE, for instance, and hit RUN (F5), it takes about a second to see the result for a small program like this. I would have suggested starting with data = [(1,2), (3,4)] ... print "input: ", a, b, "output: ", tot, ave and fill in ... until the output was correct. Then change data to ['1 2', '3 4'] and revise until correct. At that point, change data to open(....) and the program should otherwise work without change because a list of strings and a file are both iterators that produce a sequence of strings. Now, if you want, worry about formating the output, removing the echo of the input. Very last, send to output to a disk file instead of the screen. For development, that is a nuisance because it takes time to open the file to check results. So only do that when you already know the results are correct. Note that real programs used repeatedly ofter do not hard code an output file. If run from a command line in a comman window, screen output can be redirected to a file with '>': "myprog > outfile". Terry Jan Reedy From piet at cs.uu.nl Fri Jul 10 15:43:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 10 Jul 2009 21:43:21 +0200 Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: >>>>> Chris Rebert (CR) wrote: >CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: >>> I am trying to automate rsync to backup server A from server B. ?I >>> have set up a private/public key between the two servers so I don't >>> have to enter a password when using rsync. ?Running rsync manually >>> with the following command works fine: >>> rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" >>> root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp >>> >>> But when I try to do it with python, the subprocess simply returns the >>> ssh -h output on stderr like I am passing some invalid syntax. ?What >>> is wrong in my translation of rsync's -e command from shell to >>> pythyon? >>> >>> #! /usr/bin/python >>> from subprocess import Popen, PIPE >>> rsyncExec = '/usr/bin/ssh' >>> source = 'root at 10.0.45.67:/home/bry/jquery.lookup' >>> dest = '/home/bry/tmp' >>> rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>> args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] >CR> Like many problems involving the subprocess module, I think you've >CR> tokenized the arguments incorrectly. Try: >CR> rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >CR> args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] >CR> Note that the -e switch and its operand are separate arguments for the >CR> purposes of POSIX shell tokenization. I think you should have only one kind of quotes in rshArg: rshArg = "/usr/bin/ssh -i /home/bry/keys/brybackup.key" I haven't tried it, however, but this is just how Unix works. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From clp2 at rebertia.com Fri Jul 10 15:47:59 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 12:47:59 -0700 Subject: Automate rsync w/ authentication In-Reply-To: References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: <50697b2c0907101247g274c270bocfda4e82d3fb6617@mail.gmail.com> On Fri, Jul 10, 2009 at 12:43 PM, Piet van Oostrum wrote: >>>>>> Chris Rebert (CR) wrote: > >>CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: >>>> I am trying to automate rsync to backup server A from server B. ?I >>>> have set up a private/public key between the two servers so I don't >>>> have to enter a password when using rsync. ?Running rsync manually >>>> with the following command works fine: >>>> rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" >>>> root at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp >>>> >>>> But when I try to do it with python, the subprocess simply returns the >>>> ssh -h output on stderr like I am passing some invalid syntax. ?What >>>> is wrong in my translation of rsync's -e command from shell to >>>> pythyon? >>>> >>>> #! /usr/bin/python >>>> from subprocess import Popen, PIPE >>>> rsyncExec = '/usr/bin/ssh' >>>> source = 'root at 10.0.45.67:/home/bry/jquery.lookup' >>>> dest = '/home/bry/tmp' >>>> rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>>> args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] > >>CR> Like many problems involving the subprocess module, I think you've >>CR> tokenized the arguments incorrectly. Try: > >>CR> rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>CR> args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] > >>CR> Note that the -e switch and its operand are separate arguments for the >>CR> purposes of POSIX shell tokenization. > > I think you should have only one kind of quotes in rshArg: > rshArg = "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > > I haven't tried it, however, but this is just how Unix works. Ah, indeed, I think you're probably right. Just goes to show it's not always easy to get exactly right. Cheers, Chris -- http://blog.rebertia.com From jcd at sdf.lonestar.org Fri Jul 10 16:12:11 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 10 Jul 2009 16:12:11 -0400 Subject: Nested Classes and Instances In-Reply-To: References: Message-ID: <1247256731.11457.76.camel@aalcdl07> On Fri, 2009-07-10 at 19:00 +0200, Manuel Graune wrote: > Hello, > > as an example of what I would like to achieve, think of a street > where each house has a door and a sign with a unique (per house) > number on it. I tried to model this like this: > > class House(object): > class Door(object): > def __init__(self,color): > self.color=color > class Sign(object): > def __init__(self,text): > self.text=text > def __init__(self, doorcolor,housenumber): > self.housenumber=housenumber > self.door=House.Door(doorcolor) > self.sign=House.Sign(housenumber) > > house1=House("red","1") > house2=House("blue","2") > Don't do it like that. Keep your classes independent. Many houses can have doors, and there's no reason to redefine the concept of Doors for each one. Same goes for Signs. class House(object): def __init__(self, doorcolor, housenumber): self.door = Door(doorcolor) self.sign = Sign(housenumber) self.number = housenumber class Door(object): def __init__(self, color): self.color = color class Sign(object): def __init__(self, inscription): self.inscription = str(housenumber) (Something like that) Cheers, Cliff > Well, so far, so good. Now, what I'd like to achive is that the text of > the "sign" changes whenever the variable "housenumber" of the > "parent-instance" changes or that > "house1.sign.text" is a reference/pointer to "house1.housenumber" > > Thanks in advance, > > Manuel > > > > -- > A hundred men did the rational thing. The sum of those rational choices was > called panic. Neal Stephenson -- System of the world > http://www.graune.org/GnuPG_pubkey.asc > Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99 From bryanvick at gmail.com Fri Jul 10 16:16:02 2009 From: bryanvick at gmail.com (Bryan) Date: Fri, 10 Jul 2009 13:16:02 -0700 (PDT) Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: On Jul 10, 12:43?pm, Piet van Oostrum wrote: > >>>>> Chris Rebert (CR) wrote: > >CR> On Fri, Jul 10, 2009 at 9:13 AM, Bryan wrote: > >>> I am trying to automate rsync to backup server A from server B. ?I > >>> have set up a private/public key between the two servers so I don't > >>> have to enter a password when using rsync. ?Running rsync manually > >>> with the following command works fine: > >>> rsync -av --dry-run -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > >>> r... at 10.0.45.67:/home/bry/jquery.lookup /home/bry/tmp > > >>> But when I try to do it with python, the subprocess simply returns the > >>> ssh -h output on stderr like I am passing some invalid syntax. ?What > >>> is wrong in my translation of rsync's -e command from shell to > >>> pythyon? > > >>> #! /usr/bin/python > >>> from subprocess import Popen, PIPE > >>> rsyncExec = '/usr/bin/ssh' > >>> source = 'r... at 10.0.45.67:/home/bry/jquery.lookup' > >>> dest = '/home/bry/tmp' > >>> rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > >>> args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] > >CR> Like many problems involving the subprocess module, I think you've > >CR> tokenized the arguments incorrectly. Try: > >CR> rshArg = '"/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > >CR> args = [rsyncExec, '-av', '--dry-run', '-e', rshArg, source, dest] > >CR> Note that the -e switch and its operand are separate arguments for the > >CR> purposes of POSIX shell tokenization. > > I think you should have only one kind of quotes in rshArg: > rshArg = "/usr/bin/ssh -i /home/bry/keys/brybackup.key" > > I haven't tried it, however, but this is just how Unix works. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org I tried removing the internal quotes, and splitting '-e' from the other arguments. I still get the same ssh -h output however: rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] p = Popen(args, stdout=PIPE, stderr=PIPE) From tjreedy at udel.edu Fri Jul 10 16:27:12 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:27:12 -0400 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Thu, 09 Jul 2009 23:07:34 -0400, Terry Reedy wrote: >> >> The is *not* a bug is Bentley program. This is *not* a bug in Bentley's program. > Wow. That's an impressive set of typos :) 3. Way beneath my usual standards ;-) >> It is a bug in bad, buggy, insane >> integer arithmetic implementations. low + high should either return the >> right answer, as it will in Python, or raise an overflow error. > > Is binary search *supposed* to raise an overflow error if given more than > 2**30 elements? No. it is supposed to work, and Bentley's program will work if lo and hi are actually integers, as his program presupposes, and not residue classes mislabeled 'int'. > Is there something special about OverflowError that is "better" than > ArrayIndexOutOfBoundsException? Wrong comparison. The actual alternative to OverflowError is a negative number as the sum of two positive numbers. If one claims that a and b are positive ints, then returning a negative is a bug. The index exception was a side effect, in Java, of using the negative as an index. In C, the side effect was a segmentation fault. Do you seriously question that OverflowError is better than seg faulting? A different program could go on to silently return a wrong answer. Perhaps it would say to go west instead of east, or to reduce a medical dosage instead of raising it. Note that negative ints are legal indexes in Python, so that if any Python version ever had ints that wrapped instead of overflowing or returning a long, then the program would not necessarily stop. > but there's no reason to imagine that the book will assume -- or even > state as a requirement for binary search -- that addition will never > overflow. Bentley assumed that if (lo+hi)/2 were successfully calculated, then it would be a positive number between lo and hi, and therefore a legal index. The dirty secret of computing is that some languages do not have integer types. They do not even have restricted-range integer types, which would noisily fail when they cannot perform an operation. They only have residue class types, which are something else, and which can give bizarre results if one mindlessly uses them as if they really were restricted-range integers. When one wants integer operations, every operation with two residue-class variables has a potential *silent* bug. Being relieved of the burden of constantly keeping this in mind is one reason I use Python. Float types are usually restricted range types. A huge_float + huge_float may raise overflow, but never returns a negative float. Bentley assumed that indexes hi and lo are integers. If one uses restricted range integers that are too large, then lo+hi could fail gracefully with an appropriate effor message. I would not consider that a bug, bug a limitation due to using limited-range numbers. If one uses residue classes instead of integers, and makes no adjustment, I consider it wrong to blame Bentley. Terry Jan Reedy From vivainio at gmail.com Fri Jul 10 16:30:37 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Fri, 10 Jul 2009 13:30:37 -0700 (PDT) Subject: ANN: Leo 4.6 rc1 released References: Message-ID: <56d676ba-b2fc-4288-a37e-0825291369e0@n11g2000yqb.googlegroups.com> On Jul 10, 9:54?pm, "Edward K Ream" wrote: > The highlights of Leo 4.6: > -------------------------- > - Leo now features a modern Qt interface by default. > ? Leo's legacy Tk interface can also be used. And to drive home this point (Qt ui), some screenshots for the visually oriented: http://imgbin.org/images/625.png http://imgbin.org/images/626.png http://imgbin.org/images/627.png From tjreedy at udel.edu Fri Jul 10 16:32:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:32:17 -0400 Subject: can i write a assemly language programs in python In-Reply-To: <4A572AA1.7010207@ieee.org> References: <4A55F392.3080404@ieee.org> <4A572AA1.7010207@ieee.org> Message-ID: Dave Angel wrote: > Terry Reedy wrote: > If you mean dis.dis() that only gives you byte code. No, dis is a disassembler and it gives readable assembly code, not the raw numerical bytes. Its purpose is to make byte code + other parts of the code object humanly readable. As far as I know, there is no assembler that would turn the exact output of dis back to its input. tjr From Scott.Daniels at Acm.Org Fri Jul 10 16:38:06 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 13:38:06 -0700 Subject: finding most common elements between thousands of multiple arrays. In-Reply-To: <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> Message-ID: Raymond Hettinger wrote: > [Scott David Daniels] >> def most_frequent(arr, N): ... > In Py2.4 and later, see heapq.nlargest(). I should have remembered this one > In Py3.1, see collections.Counter(data).most_common(n) This one is from Py3.2, I think. --Scott David Daniels Scott.Daniels at Acm.Org From tjreedy at udel.edu Fri Jul 10 16:47:06 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:47:06 -0400 Subject: Query regarding set([])? In-Reply-To: References: Message-ID: vox wrote: > Hi, > I'm contsructing a simple compare-script and thought I would use set > ([]) to generate the difference output. But I'm obviosly doing > something wrong. > > file1 contains 410 rows. > file2 contains 386 rows. > I want to know what rows are in file1 but not in file2. > > This is my script: > s1 = set(open("file1")) > s2 = set(open("file2")) > s3 = set([]) > s1temp = set([]) > s2temp = set([]) > > s1temp = set(i.strip() for i in s1) > s2temp = set(i.strip() for i in s2) > s3 = s1temp-s2temp > > print len(s3) > > Output is 119. AFAIK 410-386=24. What am I doing wrong here? Assuming that every line in s2 is in s1. If there are lines in s2 that are not in s1, then the number of lines in s1 not in s2 will be larger than 24. s1 - s2 subtracts the intersection of s1 and s2. From tjreedy at udel.edu Fri Jul 10 16:53:53 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 16:53:53 -0400 Subject: problem with keys combination! In-Reply-To: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Message-ID: Alex wrote: > Hi at all, > I made a simple program that make a screenshot of Desktop and use it > as fullscreen background and then a ball erase image making illusion > that erase Desktop. The program working fine and I succesfully blocked > all keys but I have a problem with hotkey combination Ctrl-Alt- > Del...that bypass my FullScreen Application. Blocking Ctrl-Alt-Del leaves the power switch or maybe the plug as the only way for the user to regain control. Why would you want to do that? From ivlenin at gmail.com Fri Jul 10 16:54:08 2009 From: ivlenin at gmail.com (I V) Date: 10 Jul 2009 22:54:08 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <4a57aa70@news.x-privat.org> On Fri, 10 Jul 2009 16:27:12 -0400, Terry Reedy wrote: > a bug, bug a limitation due to using limited-range numbers. If one uses > residue classes instead of integers, and makes no adjustment, I consider > it wrong to blame Bentley. But it was Bentley himself who used the C int type, so it hardly seems unreasonable to blame him. From Scott.Daniels at Acm.Org Fri Jul 10 17:07:28 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 14:07:28 -0700 Subject: Sorry about that, the Counter class is there. In-Reply-To: References: <6faf39c90907040104v58792755had60b5ac5a873975@mail.gmail.com> <5L6dnWWjU4HF_tLXnZ2dnUVZ_hidnZ2d@pdx.net> <0360b017-72a4-44f9-8779-0299de5b0c04@i8g2000pro.googlegroups.com> Message-ID: Scott David Daniels wrote: > Raymond Hettinger wrote: >> [Scott David Daniels] >>> def most_frequent(arr, N): ... >> In Py2.4 and later, see heapq.nlargest(). > I should have remembered this one > >> In Py3.1, see collections.Counter(data).most_common(n) > This one is from Py3.2, I think. Oops -- egg all over my face. I thought I was checking with 3.1, and it was 2.6.2. I _did_ make an explicit check, just poorly. Again, apologies. --Scott David Daniels Scott.Daniels at Acm.Org From noen Fri Jul 10 17:16:13 2009 From: noen (earthlink) Date: Fri, 10 Jul 2009 17:16:13 -0400 Subject: 2.4 VS 3.1 for simple print Message-ID: Why does print "GarbageCollector: collected %d objects." % (gc.collect()) work in 2.4 but not in 3.1? From Scott.Daniels at Acm.Org Fri Jul 10 17:24:42 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 10 Jul 2009 14:24:42 -0700 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: jacopo mondi wrote: > Roger Binns wrote: >> jacopo mondi wrote: >>> Hi all, I need to patch socketmodule.c (the _socket module) in order to >>> add support to an experimental socket family. >> You may find it considerably easier to use ctypes since that will avoid >> the need for any patching. You'll also be able to control how read and >> write are done (eg read vs recvfrom vs recvmsg vs readv). You can use >> os.fdopen to convert your raw file descriptor into a Python file object >> if appropriate. The typical Python way of dealing with this is an additional module, not a modified module placed back in the library. So, take the sources and edit, but change the module name. Even better is figure out how to use _socket.pyd, to create a smaller _socketexpmodule.c and use that. --Scott David Daniels Scott.Daniels at Acm.Org From clp2 at rebertia.com Fri Jul 10 17:28:07 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 10 Jul 2009 14:28:07 -0700 Subject: 2.4 VS 3.1 for simple print In-Reply-To: References: Message-ID: <50697b2c0907101428q76fcef47me9e389c5aeee66f3@mail.gmail.com> On Fri, Jul 10, 2009 at 2:16 PM, earthlink wrote: > Why does > print "GarbageCollector: collected %d objects." % (gc.collect()) > > work in 2.4 but not in 3.1? Define "works". What error are you getting? Include the exact message and full error traceback. Or if no exception is being raised, explain exactly how is it behaving differently from your expectations. Cheers, Chris -- http://blog.rebertia.com From martin at v.loewis.de Fri Jul 10 17:30:53 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 10 Jul 2009 23:30:53 +0200 Subject: 2.4 VS 3.1 for simple print In-Reply-To: References: Message-ID: <4a57b30e$0$15912$9b622d9e@news.freenet.de> > Why does > print "GarbageCollector: collected %d objects." % (gc.collect()) > > work in 2.4 but not in 3.1? Because print is a function in 3.1, so you have to follow it with a parenthesis. Regards, Martin From lkcl at lkcl.net Fri Jul 10 17:37:46 2009 From: lkcl at lkcl.net (Luke Kenneth Casson Leighton) Date: Fri, 10 Jul 2009 21:37:46 +0000 Subject: [ANN] Pyjamas 0.6pre1 ALPHA release of Pyjamas Widget Set Message-ID: http://pyjs.org - Pyjamas is a port of GWT to Python that can run applications both on the Desktop (like python-gtk2) and in all major Web Browsers (as javascript). This is an alpha release - 0.6pre1 - of the Pyjamas Web Widget Set. It is a significant upgrade, incorporating Pyjamas Desktop which can now use Mozilla XULRunner as well as PyWebKitGtk as the browser engine. Significant enhancements have been made to the javascript compiler, which bring python strict features as well as a relaxed (and faster) compile-time option. The reason for the 0.6 pre-release is due to the number of features and improvements added. Many thanks to Kees, Lovely Systems, and all the people from EuroPython 2009 who have helped contribute and generally make Pyjamas fun to work with. Downloads are available from: http://code.google.com/p/pyjamas http://sourceforge.net/projects/pyjamas http://pypi.python.org/pypi/Pyjamas From matt.dubins at sympatico.ca Fri Jul 10 17:39:14 2009 From: matt.dubins at sympatico.ca (inkhorn) Date: Fri, 10 Jul 2009 14:39:14 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> Message-ID: Thanks all!! I found the following to be most helpful: any(substr in long_string for substr in list_of_strings) This bang-for-your-buck is one of the many many reasons why I love Python programming :) Matt Dubins From piet at cs.uu.nl Fri Jul 10 17:46:23 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 10 Jul 2009 23:46:23 +0200 Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: >>>>> Bryan (B) wrote: >B> I tried removing the internal quotes, and splitting '-e' from the >B> other arguments. I still get the same ssh -h output however: >B> rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' >B> args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, >B> dest] >B> p = Popen(args, stdout=PIPE, stderr=PIPE) For me it works. Of course I substituted local server names, file names etc. I run this on Mac OS X 10.4.11. The server is some Linux system. ------------------------------------------------------------------------ #! /usr/bin/env python from subprocess import Popen, PIPE rsyncExec = '/usr/local/bin/rsync' source = 'xxx.cs.uu.nl:/users/piet/yyyyyy' dest = '/Users/piet/TEMP/test.rsync' rshArg = '/usr/bin/ssh -i /Users/piet/.ssh/id_rsa' args = [rsyncExec, '-a', '-v', '-e', rshArg, source, dest] try: p = Popen(args, stdout=PIPE, stderr=PIPE) print 'rsync running with pid %s' % p.pid out, err = p.communicate() print 'Errors: %s' % err print 'Output: %s' % out except Exception: print 'Error running rsync' ------------------------------------------------------------------------ It just copies the file. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From timlee126 at yahoo.com Fri Jul 10 17:52:09 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 10 Jul 2009 14:52:09 -0700 (PDT) Subject: main in Python Message-ID: <159869.41728.qm@web63104.mail.re1.yahoo.com> Hi, I learned that a Python script is written in this way: def main(): ... if __name__ == "__main__": main() Today, when I read a script, I found it has a different way: def main(): ... main() It can run as well. Can someone explain why and the rules that Python scripts get run? Thanks and regards, Tim From jjposner at optimum.net Fri Jul 10 18:01:06 2009 From: jjposner at optimum.net (John Posner) Date: Fri, 10 Jul 2009 18:01:06 -0400 Subject: Tkinter only: table widget with canvas... In-Reply-To: References: Message-ID: <4A57BA22.5030201@optimum.net> Hi -- > > a) Assume I would have some different widgets to add per row. How > do I get the maximum row height? If you are placing widgets in a table like this: w.grid(row=a, column=b) ... where the *master* of widget "w" is a Tkinter.Frame named "table_frm", then you can determine the height of row N like this: table_frm.grid_bbox(column=0, row=N) Quoth the grid_bbox() documentation (with the object name changed for clarity): Returns a 4-tuple describing the bounding box of some or all of the grid system in widget |/|table_frm|/|. The first two numbers returned are the /|x|/ and /|y|/ coordinates of the upper left corner of the area, and the second two numbers are the width and height. So the final member of the 4-tuple is the height you want. > > b) Assume something like a label in one column. The length of all > texts > in a column will differ. How do I choose the maxium column width? The third member of grid_bbox() 4-tuple is the cell-width. So just find the maximum of these values: table_frm.grid_bbox(column=0, row=0)[2] table_frm.grid_bbox(column=1, row=0)[2] table_frm.grid_bbox(column=2, row=0)[2] ... > > c) Placing headers in a canvas does not look like a good idea because > I don't want to scroll the headers. Am I right? > c.1) How do I place a none scrollable header in a canvas? or > c.2) How do I place all headers outside the canvas correctly above > the relating column? > "c.2" is what you want. Again, if the table is in a Tkinter.Frame named "frm", you want to create an outer frame, then pack a header frame and the table frame into it, vertically: rt = Tk() outer_frm = Frame(rt) outer_frm.pack(expand=True, fill=BOTH) header_frm = Frame(outer_frm, height=25) header_frm.pack(side=TOP) table_frm = Frame(outer_frm) table_frm.pack(side=TOP) (For scrolling, consider making "table_frm" a Pmw.ScrolledFrame instead of a Tkinter.Frame.) As for getting the heading labels correctly placed above the columns, pack correctly-sized subframes (Tkinter.Frame's) into "header_frm". To determine the size of a column, use grid_bbox() again: BBOX_WIDTH_COMPONENT = 2 header_subfrm = Frame(header_frm, relief=GROOVE, bd=2) header_subfrm['width'] = \ table_frm.grid_bbox(column=N, row=0)[BBOX_WIDTH_COMPONENT] Then, use place() to center a label within the subframe: header_label = Label(header_subfrm, text="Heading") header_label.place(relx=0.5, rely=0.5, anchor=CENTER) There's a working app at http://cl1p.net/tkinter_table_headers/ -John From ethan at stoneleaf.us Fri Jul 10 18:11:08 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 10 Jul 2009 15:11:08 -0700 Subject: Clarity vs. code reuse/generality In-Reply-To: <006eb26f$0$9711$c3e8da3@news.astraweb.com> References: <006e795f$0$9711$c3e8da3@news.astraweb.com> <006eb26f$0$9711$c3e8da3@news.astraweb.com> Message-ID: <4A57BC7C.5030705@stoneleaf.us> Steven D'Aprano wrote: > On Mon, 06 Jul 2009 21:02:19 -0700, Aahz wrote: > > >>In article <006e795f$0$9711$c3e8da3 at news.astraweb.com>, Steven D'Aprano >> wrote: >> >>>On Mon, 06 Jul 2009 14:32:10 +0200, Jean-Michel Pichavant wrote: >>> >>>>kj wrote: >>>> >>>>> sense = cmp(func(hi), func(lo)) >>>>> assert sense != 0, "func is not strictly monotonic in [lo, hi]" >>>> >>>>As already said before, unlike other languages, sense in english does >>>>**not** mean direction. You should rewrite this part using a better >>>>name. Wrong informations are far worse than no information at all. >>> >>>Absolutely. >>> >> >>>From Webster's Dictionary: >> >>> 8. (Geom.) One of two opposite directions in which a line, >>> surface, or volume, may be supposed to be described by the motion >>> of a point, line, or surface. >>> [1913 Webster] >>> >>> >>>And from WordNet: >>> >>> 2: the meaning of a word or expression; the way in which a word >>> or expression or situation can be interpreted >>> >>>Both meanings are relevant to the way KJ is using the word. Please take >>>your own advice and stop giving wrong information. As a native English >>>speaker, I had no difficulty understanding the meaning of "sense" in the >>>sense intended by KJ. >> >>As another native English speaker, I agree with Jean-Michel; this is the >>first time I've seen "sense" used to mean direction. > > > > Just goes to show you learn something new all the time. > > http://www.merriam-webster.com/dictionary/sense > > 7: one of two opposite directions especially of motion (as > of a point, line, or surface) > > > http://dictionary.reference.com/browse/sense > > 18. Mathematics. one of two opposite directions in which > a vector may point. > > > > Paraphrasing the Collins Dictionary of Mathematics: > > The sense of a vector is the sign of the measure, contrasted with the > magnitude. Thus the vectors AB and BA have the same direction but > opposite sense. Sense is also used to distinguish clockwise and anti- > clockwise. > > Sense is, if you like, a "signed direction". "Towards north" (say) as > opposed to "along the north-south axis". > This also illustrates the importance of knowing your target audience. I have also not seen "sense" used this way before, and from the placement in the dictionaries I would venture to say it's not common usage outside of mathematics and the sciences. Of course, since kj is teaching biologists, odds are decent they know what he's talking about. ~Ethan~ From usernet at ilthio.net Fri Jul 10 18:13:00 2009 From: usernet at ilthio.net (Tim Harig) Date: Fri, 10 Jul 2009 22:13:00 GMT Subject: main in Python References: Message-ID: On 2009-07-10, Tim wrote: [RE-ORDERED] > It can run as well. Can someone explain why and the rules that Python > scripts get run? Everything gets run by default. The def syntax defines functions but does not run them -- they are only run when they are called > Today, when I read a script, I found it has a different way: > def main(): > ... > > main() This define main and then runs it. > def main(): > ... > if __name__ == "__main__": > main() This defines main but it only runs it if __name__ == "__main" which will only happen if the script is called directly: ./sample.py or python sample.py etc. This form is often preferred because it allows you to import definitions the module without running the code within the main funciton module. That way the same file can be used as a standalone program or as an importable module. From david250 at videotron.ca Fri Jul 10 18:26:36 2009 From: david250 at videotron.ca (David Bernier) Date: Fri, 10 Jul 2009 18:26:36 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: scriber77 at yahoo.com wrote: [...] > community. But perhaps he is trying to see things a bit differently > and is just not getting the feedback he needs, so he is throwing > tantrums apparently across USENET. > > Like I said before, I am just trying to do right by this person who > contacted me, and seemed to be a decent person with a genuine interest > in mathematics. He was very respectful. > > Alas, I am at a loss on what specific feedback to give him on his > paper as though I am a professor of Mathematics, Computer Science is > not my specialty. > > I told him I would try to get him some feedback on the equations on > page 3. Would any of you be so kind to help me? > > Here is the paper: http://MeAmI.org/pversusnp.pdf > > I do thank you for your time. > > Good day, > > Professor X This is what there is at the bottom of page 3: << Conclusion: Binary revisions are allowed given the above formulas. >> So I don't understand the proposed solution for the "P = NP" problem. David Bernier From liukis at usc.edu Fri Jul 10 19:28:06 2009 From: liukis at usc.edu (Maria Liukis) Date: Fri, 10 Jul 2009 16:28:06 -0700 Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception Message-ID: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> Hello, Has anybody seen an exception "OSError: [Errno 39] Directory not empty" raised by shutil.rmtree? The code that raised an exception creates a lot of directories with files, and then removes them. I got an exception when one of such directories was removed. Here is the backtrace: shutil.rmtree(filename) File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree onerror(os.rmdir, path, sys.exc_info()) File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree os.rmdir(path) OSError: [Errno 39] Directory not empty: /path/to/my/dir According to the documentation, shutil.rmtree should not care about directory being not empty. Thanks for the help, Masha -------------------- liukis at usc.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben+python at benfinney.id.au Fri Jul 10 19:35:13 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 11 Jul 2009 09:35:13 +1000 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: <87ljmwm7ri.fsf@benfinney.id.au> walterbyrd writes: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. Yes. That's a ?should? and not a ?must?, even though PEP 8 says it with a simple imperative:: Use 4 spaces per indentation level. it soon afterward takes a more nuanced tone:: The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended! For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do. > Since backward compatibility is being thrown away anyway That implies a level of abandon that was (AIUI) never the intent. Rather, backward compatibility was critically examined for whether it was justified in Python 3. > why not enforce the four space rule? Guido has used his time machine to answer your question a few years ago . (Be sure to read the discussion in the comments on the article.) > At least that way, when I get python code from somebody else, I would > know what I am looking at, without having to do a hex dump, or > something. Any text editor that is good for programming (yes, I'm aware this is perilously close to a ?no true Scotsman? fallacy) will have an option to visibly differentiate whitespace characters, for exactly the reason you point out. -- \ ?Special cocktails for the ladies with nuts.? ?bar, Tokyo | `\ | _o__) | Ben Finney From ben+python at benfinney.id.au Fri Jul 10 19:37:53 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 11 Jul 2009 09:37:53 +1000 Subject: 2.4 VS 3.1 for simple print References: Message-ID: <87hbxkm7n2.fsf@benfinney.id.au> "earthlink" writes: > Why does > > print "GarbageCollector: collected %d objects." % (gc.collect()) > > work in 2.4 but not in 3.1? For this and other differences introduced in the Python 3.x series, see . -- \ ?Please do not feed the animals. If you have any suitable food, | `\ give it to the guard on duty.? ?zoo, Budapest | _o__) | Ben Finney From tjreedy at udel.edu Fri Jul 10 19:48:58 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 10 Jul 2009 19:48:58 -0400 Subject: tough-to-explain Python In-Reply-To: <4a57aa70@news.x-privat.org> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <4a57aa70@news.x-privat.org> Message-ID: I V wrote: > On Fri, 10 Jul 2009 16:27:12 -0400, Terry Reedy wrote: >> a bug, bug a limitation due to using limited-range numbers. If one uses >> residue classes instead of integers, and makes no adjustment, I consider >> it wrong to blame Bentley. > > But it was Bentley himself who used the C int type, so it hardly seems > unreasonable to blame him. The original program that he verified was in pseudocode equivalent to the following (not tested) Python. The odd spec is due to Bentley using 1-based arrays. From the 1986 book Programming Pearls def binsearch(x, t): "If t is in sorted x[1:n], return its index; otherwise 0" # L,U = 1, len(x)-1 while True: if L > U: return 0 m = (L+U)/2 if x[m] < t: L = m+1 elif x[m] == t: return m elif x[m] > t: U = m-1 He then translated into an unspecified dialect of BASIC, but it is consistent with Microsoft GW-BASIC. If so, L and U were float variables, while the largest possible array size for x was 32767. So as near as I can tell with that dialect, there was no possible problem with L+U overflowing or wrapping. Other dialects, I am not sure. So I revise my statement to "I consider it wrong to blame the Bentley that wrote the original program" ;-). If he later translated to C and fell into the residue class trap, then that reinforces my contention that using residue classes as ints is error prone. Terry Jan Reedy From r1chardj0n3s at gmail.com Fri Jul 10 19:52:18 2009 From: r1chardj0n3s at gmail.com (Richard Jones) Date: Sat, 11 Jul 2009 09:52:18 +1000 Subject: Announcing the 9th Pyweek game programming challenge! Message-ID: <7CFED2D8-4C06-4E6F-B25D-C39A510C7686@gmail.com> The date for the ninth PyWeek challenge has been set: Sunday 30th August to Sunday 6th September (00:00UTC to 00:00UTC) The PyWeek challenge invites entrants to write a game in one week from scratch either as an individual or in a team. Entries must be developed in Python, during the challenge, and must incorporate some theme chosen at the start of the challenge. REGISTRATION IS NOT YET OPEN -- Registration will open one month before the start date. See the competition timetable and rules: http://www.pyweek.org/9/ PLANNING FOR THE CHALLENGE -- Make sure you have working versions of the libraries you're going to use. The rules page has a list of libraries and other resources. Make sure you can build packages to submit as your final submission (if you're going to use py2exe, make sure you know how to use it and that it works). If you don't have access to Linux, Windows or a Mac to test on, contact friends, family or other competitors to find someone who is able to test for you. From ben+python at benfinney.id.au Fri Jul 10 19:55:10 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 11 Jul 2009 09:55:10 +1000 Subject: Email provider recommendations for those tired of mangled messages (was: subprocess + python-daemon - bug/problem?) References: <699d22bd-e912-4ea3-92b0-c352a64611fb@n11g2000yqb.googlegroups.com> <828acd8d-a9bd-4506-b695-e537af4587d8@l31g2000yqb.googlegroups.com> <873a94ojvi.fsf@benfinney.id.au> Message-ID: <87d488m6u9.fsf_-_@benfinney.id.au> Ben Finney writes: > Here it is without the unwanted extra line-wrapping that seems to > plague all Google Mail users (seriously, folks: get a real mail > provider that won't mangle your messages) I've been asked off-list whether I have any suggestions for those who want a better email provider. Handling email (transport and mailbox services) is certainly a service that's worth getting someone else to handle, provided you can trust they'll do so reliably and well. For me, ?well? includes ?don't mangle my messages?. My personal solution to this is to manage my own email server machine, but that's an ongoing investment of time that is not something I would recommend to everyone. Nancy McGough maintained (past tense? present tense?) a comprehensive index of email service providers with IMAP access, hence allowing you to use whatever IMAP mail client you like to communicate with your mailboxes remotely. There are feature lists and price indications for each provider. Several of my colleagues swear by Tuffmail as providing excellent uptime, fine control filtering, responsive service, flexible customisation, and good overall value. -- \ ?Visitors are expected to complain at the office between the | `\ hours of 9 and 11 a.m. daily.? ?hotel, Athens | _o__) | Ben Finney From nagle at animats.com Fri Jul 10 20:02:19 2009 From: nagle at animats.com (John Nagle) Date: Fri, 10 Jul 2009 17:02:19 -0700 Subject: tough-to-explain Python In-Reply-To: References: Message-ID: <4a57d630$0$1646$742ec2ed@news.sonic.net> Bearophile wrote: > kj, as Piet van Oostrum as said, that's the difference between mutable > an immutable. It comes from the procedural nature of Python, and > probably an explanation of such topic can't be avoided if you want to > learn/teach Python. The problem with Python's mutable/immutable distinction is that it's not very visible. In a language with no declarations, no named constants, and no parameter attributes like "value", "in", "out", or "const", it's not at all obvious which functions can modify what. Which is usually the place where this causes a problem. The classic, of course, is def additemtolist(item, lst = []) : lst.append(item) return(lst) lista = additemtolist(1) listb = additemtolist(2) print(listb) The general problem is inherent in Python's design. This particular problem, though, results in the occasional suggestion that parameters shouldn't be allowed to have mutable defaults. John Nagle From python.list at tim.thechases.com Fri Jul 10 20:10:03 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 10 Jul 2009 19:10:03 -0500 Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception In-Reply-To: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> References: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> Message-ID: <4A57D85B.8040305@tim.thechases.com> > shutil.rmtree(filename) > File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree > onerror(os.rmdir, path, sys.exc_info()) > File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree > os.rmdir(path) > OSError: [Errno 39] Directory not empty: /path/to/my/dir > > According to the documentation, shutil.rmtree should not care about > directory being not empty. This sounds suspiciously like a permission issue. rmtree() *should* walk the tree removing items *if it can*. If a file can't be deleted, it treats it as an error. rmtree() takes parameters for ignore_errors and an onerror callback function, so you can catch these error conditions. -tkc From liukis at usc.edu Fri Jul 10 20:30:56 2009 From: liukis at usc.edu (Maria Liukis) Date: Fri, 10 Jul 2009 17:30:56 -0700 Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception In-Reply-To: <4A57D85B.8040305@tim.thechases.com> References: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> <4A57D85B.8040305@tim.thechases.com> Message-ID: Thanks for pointing out the parameters for ignore_errors and an onerror callback function. I have been running this code for more than an year, and this is the very first time I see the problem. We switched to the NFS mounted storage though, but there is nothing unusual logged by the system in the /var/log/messages. I checked the permissions - the directory is still there and has exactly the same permissions as other successfully copied and removed directories (the very same process creates the directory and later removes it). The directory is empty though - so the process removed all the entries in it, then some kind of an error occured that triggered the exception. There are lots of similar directories that the process creates and removes, but only one of them raised an exception. Thanks, Masha -------------------- liukis at usc.edu On Jul 10, 2009, at 5:10 PM, Tim Chase wrote: >> shutil.rmtree(filename) >> File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree >> onerror(os.rmdir, path, sys.exc_info()) >> File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree >> os.rmdir(path) >> OSError: [Errno 39] Directory not empty: /path/to/my/dir >> According to the documentation, shutil.rmtree should not care >> about directory being not empty. > > This sounds suspiciously like a permission issue. rmtree() > *should* walk the tree removing items *if it can*. If a file can't > be deleted, it treats it as an error. rmtree() takes parameters > for ignore_errors and an onerror callback function, so you can > catch these error conditions. > > -tkc > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Fri Jul 10 20:44:48 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 10 Jul 2009 20:44:48 -0400 Subject: AP -- MeAmI.org Paces Google In-Reply-To: References: Message-ID: <4A57E080.20008@ieee.org> David Bernier wrote: >> >> >> Good day, >> >> Professor X > > This is what there is at the bottom of page 3: > > << Conclusion: Binary revisions are allowed given the above formulas. >> > > So I don't understand the proposed solution for the "P = NP" > problem. > > David Bernier > > "Professor X" is yet another alias for the hoaxter who started this thread, and presumably who created the website. From benjamin.kaplan at case.edu Fri Jul 10 21:25:19 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 10 Jul 2009 21:25:19 -0400 Subject: main in Python In-Reply-To: <159869.41728.qm@web63104.mail.re1.yahoo.com> References: <159869.41728.qm@web63104.mail.re1.yahoo.com> Message-ID: On Fri, Jul 10, 2009 at 5:52 PM, Tim wrote: > > Hi, > I learned that a Python script is written in this way: > def main(): > ? ?... > if __name__ == "__main__": > ? ?main() > > Today, when I read a script, I found it has a different way: > > def main(): > ? ?... > > main() > > It can run as well. Can someone explain why and the rules that Python scripts get run? > You're thinking like a C or Java programmer. Python scripts are just that - scripts. They aren't compiled with a main() method for an entry point- it's interpreted. When you run a python script, everything that isn't indented is run, from top to bottom, in order. So for instance, when the interpreter sees the line def main() : pass it creates a function called main. If you were to have the statement x= 5 the value 5 would be assigned to x at that time. There are two ways to execute these scripts- by running them directly and by importing them into another module. The second way, with the main method, the script will be run any time it is imported. The first relies on the fact that any module run as a script has a __name__ attribute of "__main__" (usually it's the name of the module). The first program acts more like the C code would- the "main" part of it will only be run if that script is executed. > Thanks and regards, > Tim > > > > -- > http://mail.python.org/mailman/listinfo/python-list > From laurentluce49 at yahoo.com Fri Jul 10 21:52:24 2009 From: laurentluce49 at yahoo.com (Laurent Luce) Date: Fri, 10 Jul 2009 18:52:24 -0700 (PDT) Subject: windows explorer integration Message-ID: <44278.3184.qm@web54207.mail.re2.yahoo.com> Hello, Do you know if it is possible to write a plugin for windows explorer using win32 module ? The idea is to modify the way the folders/files are displayed in the explorer window and also to provide some controls. Laurent From ryankaskel at gmail.com Fri Jul 10 21:57:43 2009 From: ryankaskel at gmail.com (Ryan K) Date: Fri, 10 Jul 2009 18:57:43 -0700 (PDT) Subject: Addind imports to a class namespace Message-ID: Greetings, In order to avoid circular imports, I have a class that I want to improve upon: Class GenerateMenuXhtml(threading.Thread): """ Subclasses a threading.Thread class to generate a menu's XHTML in a separate thread. All Link objects that have this menu associated with it are gathered and combined in an XHTML unordered list. If the sender is of type Link, then all menus associated with that link are iterated through and rebuilt. """ def __init__(self, instance): from asqcom.apps.staticpages.models import Menu, Link self.Link = Link self.Menu = Menu As you can see I just expose these imports by attaching them to members of the class. There must be "prettier" option though where I can just add these imoprts to the class's namespace so all methods of any instance will have access to the imported modules. How would I go about doing this? How can I access the namespace of any class? Through Class.__dict__? Thanks, Ryan From greg at cosc.canterbury.ac.nz Fri Jul 10 22:25:11 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sat, 11 Jul 2009 14:25:11 +1200 Subject: language analysis to enforce code standards In-Reply-To: References: Message-ID: <7bqbeiF1rue32U1@mid.individual.net> Aahz wrote: > Much simpler and *way* more efficient with a set: > > if len(set(s)) < N: > print "Must have at least %s different characters" % N Or you could do a dictionary lookup on the words. I can just see the error message: "Your comment must include at least one verb, one noun, one non-cliched adjective and one Monty Python reference." -- Greg From aahz at pythoncraft.com Fri Jul 10 23:34:25 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 Jul 2009 20:34:25 -0700 Subject: sys.exit() and PyRun_SimpleFileExFlags() References: Message-ID: In article , =?ISO-8859-1?Q?Xavier_B=E9nech?= wrote: > >There is a behaviour I do not understand of PyRun_SimpleFileExFlags(), >normally when it executes a python script containing a sys.exit(), it >results by ending the calling application. If nobody responds on c.l.py, I suggest trying capi-sig. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From marty.musatov at gmail.com Fri Jul 10 23:34:25 2009 From: marty.musatov at gmail.com (Musatov) Date: Fri, 10 Jul 2009 20:34:25 -0700 (PDT) Subject: AP -- MeAmI.org Paces Google References: Message-ID: Martin Musatov wrote: David Bernier wrote: > scriber77 at yahoo.com wrote: > [...] > > > community. But perhaps he is trying to see things a bit differently > > and is just not getting the feedback he needs, so he is throwing > > tantrums apparently across USENET. > > > > Like I said before, I am just trying to do right by this person who > > contacted me, and seemed to be a decent person with a genuine interest > > in mathematics. He was very respectful. > > > > Alas, I am at a loss on what specific feedback to give him on his > > paper as though I am a professor of Mathematics, Computer Science is > > not my specialty. > > > > I told him I would try to get him some feedback on the equations on > > page 3. Would any of you be so kind to help me? > > > > Here is the paper: http://MeAmI.org/pversusnp.pdf > > > > I do thank you for your time. > > > > Good day, > > > > Professor X > > This is what there is at the bottom of page 3: > > << Conclusion: Binary revisions are allowed given the above formulas. >> > > So I don't understand the proposed solution for the "P = NP" > problem. > > David Bernier P can be equal to N and not P when it is in brackets vs. parantheses. The dots ib the equation in "P = [dots] N (dots) P. The difference between the brackets and the paranthesis represents a margin of time in computation. The dots present in the paper (I am not able to render them here, but they are in the .pdf file) represent a non-deterministic symbolic means of representing language. Thank you for your respone. The conclusion means traditional characters and symbols are not ideal for binary computation. From steve at REMOVE-THIS-cybersource.com.au Sat Jul 11 00:01:03 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 11 Jul 2009 04:01:03 GMT Subject: Clarity vs. code reuse/generality References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> Message-ID: <0267ffdf$0$18215$c3e8da3@news.astraweb.com> On Fri, 10 Jul 2009 12:27:25 -0400, Charles Yeomans wrote: >> (3) assert is absolutely unsuitable for enforcing pre-conditions and >> post- >> conditions, unless such conditions are mere "guidelines", because >> assert >> can be switched off at runtime. > > > Unless, of course, you want to switch off such checking at runtime, as > you might when using a design-by-contract approach. So is design-by-contract just another way of saying "let's hope the data is valid, because if it's not, we're screwed"? Perhaps Python should have a new statement, `assume`, used just like `assert` except it never actually performs the test or raises an error. -- Steven From tjreedy at udel.edu Sat Jul 11 01:04:45 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 11 Jul 2009 01:04:45 -0400 Subject: Clarity vs. code reuse/generality In-Reply-To: <0267ffdf$0$18215$c3e8da3@news.astraweb.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <0267ffdf$0$18215$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 10 Jul 2009 12:27:25 -0400, Charles Yeomans wrote: > >>> (3) assert is absolutely unsuitable for enforcing pre-conditions and >>> post- >>> conditions, unless such conditions are mere "guidelines", because >>> assert >>> can be switched off at runtime. >> >> Unless, of course, you want to switch off such checking at runtime, as >> you might when using a design-by-contract approach. > > So is design-by-contract just another way of saying "let's hope the data > is valid, because if it's not, we're screwed"? > > Perhaps Python should have a new statement, `assume`, used just like > `assert` except it never actually performs the test or raises an error. The manual says quite clearly "The simple form, assert expression, is equivalent to if __debug__: if not expression: raise AssertionError" It should be used when and only when one actually wants the double condition check, and not as an abbreviation of the second conditional only. tjr From haematopus at gmail.com Sat Jul 11 01:52:40 2009 From: haematopus at gmail.com (oystercatcher) Date: Fri, 10 Jul 2009 22:52:40 -0700 (PDT) Subject: fedora 3.1 python install Message-ID: <69d90aab-eee3-4fa3-8960-4548e4e706d7@h2g2000yqg.googlegroups.com> had installed python 3.0. on fc11 x64 the only anomalies were a couple of tests failed during the make test. make altinstall was fine and have been using it for a simple math exercise. attempting to install python 3.1 the make test hangs at test_httpservers and I had to kill the task after waiting quite awhile. now probably this is no big deal and I should just go ahead and run the make altinstall but it would be nice to know a bit more about what is going on here. thanks From nobody at nowhere.com Sat Jul 11 02:24:02 2009 From: nobody at nowhere.com (Nobody) Date: Sat, 11 Jul 2009 07:24:02 +0100 Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On Fri, 10 Jul 2009 09:23:54 +0000, garabik-news-2005-05 wrote: >>> I would like to learn a way of changing the colour of a particular >>> part of the output text. I've tried the following > >> On Unix operating systems this would be done through the curses interface: >> >> http://docs.python.org/library/curses.html > > Or using ANSI colour codes: > > colours = { > 'none' : "", > 'default' : "\033[0m", > 'bold' : "\033[1m", [snip] > # non-standard attributes, supported by some terminals This comment should have appeared immediately after "none" ;) Hard-coding control/escape sequences is just lame. Use the curses modules to obtain the correct sequences for the terminal. From research at johnohagan.com Sat Jul 11 02:30:24 2009 From: research at johnohagan.com (John O'Hagan) Date: Sat, 11 Jul 2009 06:30:24 +0000 Subject: tough-to-explain Python In-Reply-To: <001401ca014c$ca414d60$0d00a8c0@Hendrik> References: <00720d76$0$9710$c3e8da3@news.astraweb.com> <001401ca014c$ca414d60$0d00a8c0@Hendrik> Message-ID: <200907110630.24557.research@johnohagan.com> On Fri, 10 Jul 2009, Hendrik van Rooyen wrote: > "Steven D'Aprano" wrote: > >On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: [...] > >> Programming is not like any other human activity. > > > >In practice? In principle? Programming in principle is not the same as it > >is performed in practice. > > > >But in either case, programming requires both the logical reasoning of > >mathematics and the creativity of the arts. Funnily enough, > > I do not buy this arty creativity stuff. - or are you talking about > making a website look pretty? > > >mathematicians will tell you that mathematics requires the same, and so > >will the best artists. I think mathematicians, engineers, artists, even > >great chefs, will pour scorn on your claim that programming is not like > >any other human activity. > > So a chef is now an authority on programming? > > Programming is actually kind of different - almost everything else is > just done, at the time that you do it. > > Programming is creating stuff that is completely useless until it is > fed into something that uses it, to do something else, in conjuction > with the thing it is fed into, at a later time. > > This is a highly significant difference, IMHO. [...] The drawings produced by an architect, the script of a play, the score of a piece of music, and the draft of a piece of legislation are all examples of other things which are "useless" until they are interpreted in some way. There are countless human activities which require a program, i.e. a conscious plan or strategy, formed at least partly by a creative process, and a computer program is just a special case of this. I use Python as a tool for writing music, but I find I need both logical reasoning and creativity to do either. In fact, I find programming very similar to writing music in a rigorous contrapuntal style, where each set of choices constrains each other, and there is a deep aesthetic satisfaction in getting it right. Regards, John -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sat Jul 11 02:51:48 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Jul 2009 08:51:48 +0200 Subject: Addind imports to a class namespace References: Message-ID: Ryan K wrote: > In order to avoid circular imports, I have a class that I want to > improve upon: > > Class GenerateMenuXhtml(threading.Thread): > """ > Subclasses a threading.Thread class to generate a menu's XHTML in > a separate > thread. All Link objects that have this menu associated with it > are gathered > and combined in an XHTML unordered list. > > If the sender is of type Link, then all menus associated with that > link are > iterated through and rebuilt. > """ > def __init__(self, instance): > from asqcom.apps.staticpages.models import Menu, Link > self.Link = Link > self.Menu = Menu > > As you can see I just expose these imports by attaching them to > members of the class. There must be "prettier" option though where I > can just add these imoprts to the class's namespace so all methods of > any instance will have access to the imported modules. > > How would I go about doing this? How can I access the namespace of any > class? Through Class.__dict__? You can write either class A(object): from some.module import Menu, Link or class A(object): @classmethod do_imports(cls): from some.module import Menu, Link cls.Menu = Menu cls.Link = Link A.do_imports() to put the names into the class. The first form performs the import while the containing module is imported and therefore won't help with breaking circles. In the second form you have to defer the A.do_imports() method call until the import from some.module is safe. But I still recommend that you have another look at your package organization to find a way to avoid circles. Peter From piet at cs.uu.nl Sat Jul 11 03:54:19 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 11 Jul 2009 09:54:19 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <4a57aa70@news.x-privat.org> Message-ID: >>>>> I V (IV) wrote: >IV> On Fri, 10 Jul 2009 16:27:12 -0400, Terry Reedy wrote: >>> a bug, bug a limitation due to using limited-range numbers. If one uses >>> residue classes instead of integers, and makes no adjustment, I consider >>> it wrong to blame Bentley. >IV> But it was Bentley himself who used the C int type, so it hardly seems >IV> unreasonable to blame him. If you are on a 32-bit machine, and the array to be searched contains ints, floats or doubles, the the array must be < 2^32 bytes in size, and as each element is at least 4 bytes, the indices are less than 2^30, so l+u < 2^31. Therefore there is no overflow at all. I think the Bentley programs were formulated in terms of arrays of ints. So his implementations were safe. If you are on a 64-bit machine you shouldn't use int for the indices anyway (supposing int is 32 bits) but longs and then the same reasoning shows that there are no overflows. Only when you have an array of shorts or bytes (chars) you get the problem. In that case the alternative formulation l + (u-l)/2 is more robust and therefore preferable. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From Iris-und-Thomas-Lehmann at T-Online.de Sat Jul 11 04:12:27 2009 From: Iris-und-Thomas-Lehmann at T-Online.de (Thomas Lehmann) Date: Sat, 11 Jul 2009 01:12:27 -0700 (PDT) Subject: Tkinter only: table widget with canvas... References: Message-ID: <5db86de3-d0a0-42c7-8924-7ba8948cfd99@32g2000yqj.googlegroups.com> > > There's a working app at http://cl1p.net/tkinter_table_headers/ > > -John Thank you for this example. However, one issue to that... When resizing the window (vertical) then the header moves away from the table. How can I avoid this with the grid? With "pack" I now this... From p.f.moore at gmail.com Sat Jul 11 05:25:26 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Sat, 11 Jul 2009 10:25:26 +0100 Subject: windows explorer integration In-Reply-To: <44278.3184.qm@web54207.mail.re2.yahoo.com> References: <44278.3184.qm@web54207.mail.re2.yahoo.com> Message-ID: <79990c6b0907110225p14dd718bv26a36e5b13d5e51@mail.gmail.com> 2009/7/11 Laurent Luce : > Do you know if it is possible to write a plugin for windows explorer using win32 module ? Yes, I believe it is. There are a number of Python projects (I believe TortoiseHg is one) that do this sort of thing. However, I don't know anything about how to do it - you should check MSDN, the PyWin32 samples, and maybe something like TortoiseHg, for sample code. Paul. From magobin at gmail.com Sat Jul 11 06:39:25 2009 From: magobin at gmail.com (Alex) Date: Sat, 11 Jul 2009 03:39:25 -0700 (PDT) Subject: problem with keys combination! References: <154882be-dfc0-486e-8dc9-1d6807f2c8c9@t13g2000yqt.googlegroups.com> Message-ID: > Blocking Ctrl-Alt-Del leaves the power switch or maybe the plug as the > only way for the user to regain control. Why would you want to do that? ONly for the reason that I explaine above...is for my little joke application ! And I want disable all keys for about 30 seconds (time to erase background) I read about getasynckey() to intercept a key...but I don't know how to change key pressed with another...maybe a solution ? Alex From gslindstrom at gmail.com Sat Jul 11 07:10:17 2009 From: gslindstrom at gmail.com (gslindstrom) Date: Sat, 11 Jul 2009 04:10:17 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: On Jul 10, 2:22?pm, walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. > > Since backward compatibility is being thrown away anyway, why not > enforce the four space rule? > There is a routine in the Scripts directory, reindent.py, that will take your source file(s) and remove tabs, unify the indents to 4- spaces, remove needless characters at the end of lines, etc. IIRC, it was written by Tim Peters. We run all of our source files though before checking them into svn. --greg From mail at microcorp.co.za Sat Jul 11 08:01:25 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 14:01:25 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com><00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <000f01ca021f$5132b5c0$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >On Fri, 10 Jul 2009 12:54:21 +0200, Hendrik van Rooyen wrote: > >> "Steven D'Aprano" wrote: >> >>>On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: >>> >>>>> persistent idea "out there" that programming is a very accessible >>>>> skill, like cooking or gardening, anyone can do it, and even profit >>>>> from it, monetarily or otherwise, etc., and to some extent I am >>>> >>>> Programming is not like any other human activity. >>> >>>In practice? In principle? Programming in principle is not the same as >>>it is performed in practice. >>> >>>But in either case, programming requires both the logical reasoning of >>>mathematics and the creativity of the arts. Funnily enough, >> >> I do not buy this arty creativity stuff. - or are you talking about >> making a website look pretty? > >I must admit, it never crossed my mind that anyone here would claim that >there was no creativity involved in programming, that it was all a >mindless, algorithmic process capable of being done by a simple >mechanical device. "Programming" is the step of going from the "design" to something that tells the machine how to implement the design. The creativity could, arguably, be in the "Design". Not in the translation to python, or assembler. No way. That is just coding. > >This is certainly the accusation made against *bad* programmers -- that >they can't actually solve new, unique problems, but just apply recipes >they learned without any insight or intelligence. The sort of people who >program so poorly that "a trained monkey could do what they do". > >Do you really think that applies to good programmers too? If so, then a >good code generator should be able to replace any programmer. Is that >what you believe? > Should eventually be possible, with sufficient restrictions to start off. UML wants to go this route... But may my eyes be stopped and my bones be heaped with dust ere I see the day... > > >>>mathematicians will tell you that mathematics requires the same, and so >>>will the best artists. I think mathematicians, engineers, artists, even >>>great chefs, will pour scorn on your claim that programming is not like >>>any other human activity. >> >> So a chef is now an authority on programming? > >Did I say that? No. I just read it like that to irritate you. > >Chefs are authorities on OTHER HUMAN ACTIVITIES. > > > >> Programming is actually kind of different - almost everything else is >> just done, at the time that you do it. >> >> Programming is creating stuff that is completely useless until it is fed >> into something that uses it, to do something else, in conjuction with >> the thing it is fed into, at a later time. > >Somebody should teach Hendrik that human beings have been creating TOOLS >for hundreds of thousands of years. People have been creating tools to >build tools for thousands of years. Software is just more of the same. I disagree - I actually own some machine tools, so I am a little bit acquainted with what they can do, and it is not at all like computing at any level I can think of - they are merely extensions of the hand, making the transformation of materials more accurate and faster. The line only becomes blurred when a processor is added, and a STORED PROGRAM is brought into the equation. > >Even *soup stock* fits the same profile as what Hendrik claims is almost >unique to programming. On its own, soup stock is totally useless. But you >make it, now, so you can you feed it into something else later on. > >Or instant coffee. > >No, Henrik, if that's the best you can do, it's not very good. It is >rather sad, but also hilarious, that the most different thing you have >noticed about software is that it's just like instant coffee. > You have a wonderful ability to grab hold of part of a definition and to ignore the rest, just like I can misread what you write. Coffee and soup stay coffee and soup on re hydration. Mixing it in with something else is not at all the same - it does not "DO" anything else in conjunction with the thing it is fed into - how is that like programming, and executing a program? I am sorry if you are confusing the drinking of coffee, which is an ancilliary activity to programming, with the actual programming itself. > > >> This is a highly significant difference, IMHO. > > >>>[...] >>>> He talks about how "when all is said and done, the only thing >>>> computers can do for us is to manipulate symbols and produce results >>>> of such manipulations" and he emphasises the "uninterpreted" nature of >>>> mechanical symbol manipulation, i.e. that the machine is doing it >>>> mindlessly. >>> >>>"Manipulate symbols" is so abstract as to be pointless. By that >>>reasoning, I can build a "computer" consisting of a box open at the top. >>>I represent a symbol by an object (say, a helium-filled balloon, or a >>>stone), instead of a pattern of bits. I manipulate the symbol by holding >>>the object over the box and letting go. If it flies up into the sky, >>>that represents the symbol "Love is War", if it falls into the box, it >>>represents the symbol "Strength is Blue", and if it just floats there, >>>it represents "Cheddar Cheese". This is a deterministic, analog computer >>>which manipulates symbols. Great. >>> >>>And utterly, utterly useless. So what is my computer lacking that real >>>computers have? When you have answered that question, you'll see why >>>Dijkstra's claim is under-specified. >>> >>> >> So if computers do not manipulate symbols, what is it that they do? > >Did I say they don't manipulate symbols? No you wanted someone to tell you how to make your box machine useful, and that was too difficult, so I went this way. > > >> They >> sure cannot think, >> or drink, >> or reason, > >They can't reason? Then what are they doing when they manipulate symbols? They simply manipulate symbols. They really do. There is no reasoning involved. Any reasoning that is done, was done in the mind of the human who designed the system, and the the machine simply manipulates the implementation of the abstract symbols, following the implementation of the abstract rules that were laid down. No reasoning at run time at all. Lots of decision making though - and it is this ability to do this now, and something else later, that tricks the casual observer into thinking that there is somebody at home - a reasoning ghost in the machine. > >Yet again, it didn't even cross my mind that somebody would make this >claim. My entire point is that it's not enough to just "manipulate >symbols", you have to manipulate symbols the correct way, following laws >of logic, so that the computer can *mindlessly* reason. No. There is no requirement for dragging in things like "the laws of logic" or any arbitrarily chosen subset. You can build a machine to do essentially "anything" - and people like Turing and his ilk have spent a lot of skull sweat to try to define what is "general purpose", to enable you to do "anything" > > >> or almost any verb you can think of. > >If you're going to take that argument, then I'll remind you that there >are no symbols inside a computer. There are only bits. And in fact, there >aren't even any bits -- there are only analog voltages, and analog >magnetic fields. > Duh - sorry to hear that - I have only been doing digital electronics now for more years than I care to remember - but maybe there is a difference in how you define "analogue". - maybe something like: "if I can measure it with a multimeter, it is an analogue signal, because a multimeter is capable of measuring analogue signals" - a bit fallacious. More seriously, I again differ on the symbol bit - I am, right now, working on a little processor that mindlessly does I/O, by manipulating bits from one representation to another. In this case, the symbols and the bits are mostly identical - but please do not try to tell me that there are no symbols inside the little processors' memory - I know they are there, because I have arranged for them to be put there, via an RS-232 feed. And my stored program actually arranges for the incoming symbols to do things like activating output relays, and changes on input lines actually cause other symbols to be transmitted out of the RS-232 port. The whole thing is simply rotten with symbology - in fact all it does, is manipulating the implementation of the abstract symbols in my mind. It does so mindlessly. This specific thing is so simple, that it hardly uses the "logical ability" of the processor. And please remember that for a function with two input bits and a single output bit, there can at most be eight possible outcomes, not all of which are useful, for some definition of useful. But they exist, and you can use them, if you want. And you do not even have to be consistent, if you do not want to be, or if it is convenient not to be. I think it is this horrendous freedom that Dijkstra was talking about when he was referring to the "power beyond your wildest dreams", or some such. > >> "Manipulating symbols" is actually an elegant definition. Try coming up >> with a better one and you will see. > >As I said above, it's not enough to just manipulate symbols. Here's a set >of rules to manipulate symbols: > >X => 0 > >or in English, "Any symbol becomes the zero symbol". > >That's symbol manipulation. Utterly useless. This is why it's not enough >to just manipulate symbols, you have to manipulate them THE RIGHT WAY. > No. There is no RIGHT WAY. Really. There are only ways that are useful, or convenient. In my little processor, I can cause any result to occur, and the rules can be as silly as your example - if it is useful for you, under some circumstances, for all symbols to become the symbol for zero, then go for it - The point is that there is nothing prohibiting it to be adopted as a rule for a machine to implement. /dev/null anyone? - Hendrik From mail at microcorp.co.za Sat Jul 11 08:14:55 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 14:14:55 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <001001ca0221$33a04c00$0d00a8c0@Hendrik> "pdpi" wrote; >I've always found cooking an apt metaphor for programming. No this is wrong. Writing a recipe or a cookbook is like programming. Cooking, following a recipe, is like running a program. - Hendrik From jjposner at optimum.net Sat Jul 11 09:31:58 2009 From: jjposner at optimum.net (John Posner) Date: Sat, 11 Jul 2009 09:31:58 -0400 Subject: Tkinter only: table widget with canvas... In-Reply-To: References: Message-ID: <4A58944E.2030009@optimum.net> >> There's a working app at http://cl1p.net/tkinter_table_headers/ > Thank you for this example. However, one issue to that... > When resizing the window (vertical) then the header moves away > from the table. How can I avoid this with the grid? With "pack" > I now this... Oops ... packing can be tricky. Try these modifications: header_frm.pack(side=TOP, anchor=W, expand=False) ... table_frm.pack(side=TOP, expand=True, fill=BOTH) You also might try constraining the top-level (root) window, using these methods: rt.geometry(XXX, YYY) rt.resizable(False, True) -John From ryankaskel at gmail.com Sat Jul 11 09:55:34 2009 From: ryankaskel at gmail.com (Ryan K) Date: Sat, 11 Jul 2009 06:55:34 -0700 (PDT) Subject: Addind imports to a class namespace References: Message-ID: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Thanks for your help Peter. I'm thinking that perhaps this isn't a circular import and that I don't understand importing. Here is a better explanation of my case (I am using Django): I have file x.py that declares classes A, B, C. There is also a file y.py that contains two methods T, U and the class that we are talking about above. x.py uses a dispatcher system to connect a signal to methods T and U in y.py so it does: from y import T, U. y.py needs to use classes A, B, C which is basically Menu and Link (and some other class) above so I am thinking that if in y.py I have from x import A, B, C that will cause a circular import? Is this not correct and if it isn't can you explain why? Does using from ... import X, Y, Z, i.e. explicit imports avoid this problem or does it exacerbate it? Thanks, Ryan From XX.XmcX at XX.XmclaveauX.com Sat Jul 11 10:13:20 2009 From: XX.XmcX at XX.XmclaveauX.com (MC) Date: Sat, 11 Jul 2009 16:13:20 +0200 Subject: windows explorer integration References: Message-ID: Hi! Possible! With Pywin32. I see two ways: - add a "toolBar" ; see the exemple for Internet-Explorer (it run for both, IE & explorer) - add an entry in the context menu (right click) @-salutations -- Michel Claveau -- @-salutations Michel Claveau From __peter__ at web.de Sat Jul 11 10:39:13 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Jul 2009 16:39:13 +0200 Subject: Addind imports to a class namespace References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: Ryan K wrote: > I'm thinking that perhaps this isn't a circular import and that I > don't understand importing. Here is a better explanation of my case (I > am using Django): > > I have file x.py that declares classes A, B, C. Classes in Python are executable code, just like import-statements. That's why there is no Python equivalent to forward declarations in other languages; every name has to be known at the point where it occurs in the module. > There is also a file y.py that contains two methods T, U and the class > that we are talking about above. > > x.py uses a dispatcher system to connect a signal to methods T and U > in y.py so it does: from y import T, U. > > y.py needs to use classes A, B, C which is basically Menu and Link > (and some other class) above so I am thinking that if in y.py I have > from x import A, B, C that will cause a circular import? Yes. Having x import y and y import x creates a cycle. If you cannot avoid this by moving to a simpler design you can always introduce a third module z that imports x and y, and then explicitly resolves the circular references. > Is this not correct and if it isn't can you explain why? Does using > from ... import X, Y, Z, i.e. explicit imports avoid this problem or > does it exacerbate it? It has no effect. from module import X is equivalent to import module X = module.X del module # just the name, not the module itself Peter From mail at microcorp.co.za Sat Jul 11 11:10:04 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 17:10:04 +0200 Subject: tough-to-explain Python References: <00720d76$0$9710$c3e8da3@news.astraweb.com><001401ca014c$ca414d60$0d00a8c0@Hendrik> <200907110630.24557.research@johnohagan.com> Message-ID: <007701ca0240$77f9c2e0$0d00a8c0@Hendrik> John O'Hagan wrote: >The drawings produced by an architect, the script of a play, the score of a piece of music, and the draft of a piece of >legislation are all examples of other things which are "useless" until they are interpreted in some way. Granted. But... >There are countless human activities which require a program, i.e. a conscious plan or strategy, formed at least partly >by a creative process, and a computer program is just a special case of this. The difference is that for a piece of music, or a recipe for "bobotie", there is room for artistry also in the performance or preparation. If the piece of music is reduced to a scroll with holes in it and played on a pianola, then nobody in his right mind would call the mechanically produced sounds "a great performance". And this is the essential difference that sets programming apart from cookbook writing or drawing architectural plans - the one thing that makes the real difference is the mindlessness of the performer. (maybe I should conceed that writing legislation is a form of programming, as the constabulary does not have a reputation for anything other than a plodding consistency) >I use Python as a tool for writing music, but I find I need both logical reasoning and creativity to do either. In fact, I >find programming very similar to writing music in a rigorous contrapuntal style, where each set of choices constrains >each other, and there is a deep aesthetic satisfaction in getting it right. Getting it right has to do with the design, not the programming - Have you ever struggled to get something right, and then one day you take a different tack, and suddenly it is as if you cannot do anything wrong - everything just falls into place? - That is the time that you get the good feeling. The difference between the one and the other is the difference between bad or indifferent design and good design. When you have a good design, the rest follows. If your design is crud, no matter what you do, you struggle and nothing comes out just right, despite heroic effort. Now this stuff is easy to talk about, and immensely difficult to do - it takes experience, some skill, some cunning, and a willingness to experiment in an egoless fashion, amongst other things. And then the stupid compiler completely screws up your intent... :-) - Hendrik From darcy at druid.net Sat Jul 11 11:22:42 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Sat, 11 Jul 2009 11:22:42 -0400 Subject: tough-to-explain Python In-Reply-To: <000f01ca021f$5132b5c0$0d00a8c0@Hendrik> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> <000f01ca021f$5132b5c0$0d00a8c0@Hendrik> Message-ID: <20090711112242.ec01ae8b.darcy@druid.net> On Sat, 11 Jul 2009 14:01:25 +0200 "Hendrik van Rooyen" wrote: > "Programming" is the step of going from the "design" to something > that tells the machine how to implement the design. > > The creativity could, arguably, be in the "Design". > Not in the translation to python, or assembler. > No way. That is just coding. One might also argue that divorcing the design from the code is the problem in a lot of legacy code. See Agile Programming methods. Now you could say that there is a design step still in talking to the client and making a plan in your head or in some notes but that's like saying that Michelangelo was done creating after discussing the Sistine Chapel with Pope Sixtus and that the rest was just a house painting job. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From mail at microcorp.co.za Sat Jul 11 11:58:32 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 11 Jul 2009 17:58:32 +0200 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com><00720d76$0$9710$c3e8da3@news.astraweb.com><007318de$0$9710$c3e8da3@news.astraweb.com><000f01ca021f$5132b5c0$0d00a8c0@Hendrik> <20090711112242.ec01ae8b.darcy@druid.net> Message-ID: <007801ca0240$78a01500$0d00a8c0@Hendrik> "D'Arcy J.M. Cain" > One might also argue that divorcing the design from the code is the > problem in a lot of legacy code. See Agile Programming methods. Now > you could say that there is a design step still in talking to the > client and making a plan in your head or in some notes but that's like > saying that Michelangelo was done creating after discussing the Sistine > Chapel with Pope Sixtus and that the rest was just a house painting job. How do you know that it was not exactly like that - he did, after all, take a much longer time than expected to complete the job - just like a house painter that gets paid by the hour. :-) It is also unreasonable to assume the opposite fallacy - that he was in a creative frenzy from the start to the time at the end when he was cleaning his brush after applying the last spot of paint. But it is a valid point - it is often difficult to draw the line between the design and the implementation - and one of the reasons that we all like to program in python, is that it is almost a language that is a design language that can also be run directly. - I have lately been doing stuff like writing rough prototypes using python syntax to serve as designs for some of the assembler thingies I do. If you muck around a bit at the lower level, you will also be more aware of the dichotomy between the design and the implementation - in python, it is not obvious at all. In assembler, it is glaring. But in either place, if you get it wrong, you suffer. - your programs cripple along, or they hardly do what you thought they would, and they bear no relationship with what the customer wanted. - Hendrik From 2.b7e15163 at googlemail.com Sat Jul 11 13:33:10 2009 From: 2.b7e15163 at googlemail.com (Henry 'Pi' James) Date: Sat, 11 Jul 2009 10:33:10 -0700 (PDT) Subject: asyncore's lack of sendto(), recvfrom() Message-ID: <3cbf56cf-1362-44af-9d9f-4f5f2f298c0a@a26g2000yqn.googlegroups.com> Is there any good reason why sendto() and recvfrom() aren't wrapped by asyncore? Obviously, recvfrom() cannot be replaced by recv(), but even sendto() cannot be replace by connect() and send(), either: I'm writing a traceroute module, and I found out that under the current firewall configuration of my OS, sending an ICMP packet via sendto() doesn't trigger a connection confirmation (the packet is silently and successfully sent), while connect() does (the firewall reports an attempted UDP connection, I'm sure why). I know Python merely wraps send() and sendto() from of the OS, obviously there are some important subtle differences between the two. Now, if I'd want to add sendto() and recvfrom() to asyncore, would it be sufficient to simply copying its send() and recv() wrapper functions and change the names and arguments, or are there more modifications required? From mondi at cs.unibo.it Sat Jul 11 13:55:25 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Sat, 11 Jul 2009 17:55:25 +0000 Subject: hoe to build a patched socketmodule.c In-Reply-To: References: Message-ID: Scott David Daniels wrote: > jacopo mondi wrote: >> Roger Binns wrote: >>> jacopo mondi wrote: >>>> Hi all, I need to patch socketmodule.c (the _socket module) in order to >>>> add support to an experimental socket family. >>> You may find it considerably easier to use ctypes since that will avoid >>> the need for any patching. You'll also be able to control how read and >>> write are done (eg read vs recvfrom vs recvmsg vs readv). You can use >>> os.fdopen to convert your raw file descriptor into a Python file object >>> if appropriate. > > The typical Python way of dealing with this is an additional module, not > a modified module placed back in the library. So, take the sources and > edit, but change the module name. Even better is figure out how to > use _socket.pyd, to create a smaller _socketexpmodule.c and use that. > > --Scott David Daniels > Scott.Daniels at Acm.Org Thanks a lot Scott. If I'll write a new module, with a new name, based on socketmodule.c I will not be able to ditribuite the source code and make users compile it using distutils, instead I have to compile it using the whole python build system (changing setup.py in python sources to let it know about my new module) and distribuite the compiled .so with a script to install it, I suppose. Anyway, that's not too bad... Reusing _socket.so, or the main wrapper socket.pyc I think is not possible, because I need to modify low level functions that are not exposed thanks again jacopo From davea at ieee.org Sat Jul 11 13:56:04 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 11 Jul 2009 13:56:04 -0400 Subject: Addind imports to a class namespace In-Reply-To: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: <4A58D234.4050607@ieee.org> Ryan K wrote: > Thanks for your help Peter. > > I'm thinking that perhaps this isn't a circular import and that I > don't understand importing. Here is a better explanation of my case (I > am using Django): > > I have file x.py that declares classes A, B, C. > > There is also a file y.py that contains two methods T, U and the class > that we are talking about above. > > x.py uses a dispatcher system to connect a signal to methods T and U > in y.py so it does: from y import T, U. > > y.py needs to use classes A, B, C which is basically Menu and Link > (and some other class) above so I am thinking that if in y.py I have > from x import A, B, C that will cause a circular import? > > Is this not correct and if it isn't can you explain why? Does using > from ... import X, Y, Z, i.e. explicit imports avoid this problem or > does it exacerbate it? > > Thanks, > Ryan > > You indeed have described a circular import. In this case, a mutual dependency exists between x.py and y.py. This can and nearly always should be avoided. That's independent of the language involved, in any language, circular dependencies are a code smell Now, in Python in particular, first question is whether either x.py or y.py is your main script module (the one where you do the if __name__ == "__main__" and the answer is yes). If so, you're in big trouble, and you really need to rework it. I could elaborate if needed, but trust me, you don't want to do this, even if it means writing one more module containing a single import line and just two or three more, and letting that be your script. If that's not the case, next question is whether either module refers to the other one at import time, or only from inside def and class definitions. If you have any top-level code going on, other than imports, and this code actually uses a symbol from the other module, you're probably in trouble. Not as bad as first case, but still worth avoiding. Sometimes this can be kludged past, by reordering things a little bit, maybe by moving one of the imports further down in the file. Now back to the code smell. If the modules really depend on each other, you should consider factoring out those dependencies and move them to a third module that both import. Figure out why the modules are broken up in the particular way they are, and how you can eliminate circular references by refactoring. It's hard for me to come up with general principles, but perhaps an example or three will help. If a callee needs configuration information stored in globals of the caller, then you probably need to add that config information as parameters. If an object needs a reference to functions in another module, add them when constructing the object, not statically. Or maybe even have the one module explicitly set global variables in the other module, so they're in one place, instead of being only in the namespace of the code initializing them. That's the best I can do without something concrete to adjust. DaveA From ryankaskel at gmail.com Sat Jul 11 13:56:48 2009 From: ryankaskel at gmail.com (Ryan K) Date: Sat, 11 Jul 2009 10:56:48 -0700 (PDT) Subject: Addind imports to a class namespace References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: Okay so below is the acutal code. I am starting to think there is no reason why I can't install the post_save signal in signals.py itself and thereby avoid this issue entirely. models.py: class Link(CommonAbstractModel): ... class Menu(CommonAbstractModel): .... class StaticPage(CommonAbstractModel): ,,, class CachedMenuXhtml(CommonAbstractModel): ... post_save.connect(signals.build_menu, sender=Link) post_save.connect(signals.build_menu, sender=Menu) -------------------- # Signlas for caching of menu XHTML class GenerateMenuXhtml(threading.Thread): def __init__(self, instance): from asqcom.apps.staticpages.models import Menu, Link, CachedMenuXhtml self.Link = Link self.Menu = Menu self.CachedMenuXhtml = CachedMenuXhtml # Function to run on post_save signal def build_menu(sender, instance, **kwargs): GenerateMenuXhtml(instance).start() From digitig at gmail.com Sat Jul 11 14:33:19 2009 From: digitig at gmail.com (Tim Rowe) Date: Sat, 11 Jul 2009 19:33:19 +0100 Subject: Clarity vs. code reuse/generality In-Reply-To: <0267ffdf$0$18215$c3e8da3@news.astraweb.com> References: <7xzlbkti7z.fsf@ruckus.brouhaha.com> <0267ffdf$0$18215$c3e8da3@news.astraweb.com> Message-ID: 2009/7/11 Steven D'Aprano : > So is design-by-contract just another way of saying "let's hope the data > is valid, because if it's not, we're screwed"? Not at all. Design By Contract is about assigning responsibility for checking. If you don't assign responsibility then a pile of things end up getting checked over and over again, because everybody is assuming responsibility, and some things don't get checked at all because everyone assumes that somebody else is checking. In DbC, the pre-condition on data coming in to a program from outside will usually simply be "true" -- nothing at all is assumed about it. But when you pass it from an input conditioning routine to a processing routine, the input conditioning routine should be able to make guarantees about what it passes, and the processing routine should be able to assume that those guarantees are met without having to check it again (after all, what was the conditioning routine there for?). Assertions might be useful in testing (because they save having to repeat the same set of test cases on absolutely every test run) and they're certainly useful in documenting, but if they're any use at all in the production run-time then there's something wrong with your development and testing processes. -- Tim Rowe From piet at cs.uu.nl Sat Jul 11 17:21:53 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 11 Jul 2009 23:21:53 +0200 Subject: Threading.Condition problem References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Sorry if this appears twice, I sent it once with an attachment and it never >GR> arrived so maybe the attachment is posing problems. I inlined the code this >GR> time (at the bottom), thank you, >GR> Gabriel >GR> ########################## Original message ############################ >GR> Hello everyone, >GR> I wrote a small example that listens for xmpp msgs in a thread. The main >GR> program calls a function that blocks (using Condition.wait) until a msg >GR> has been received and then returns the msg. When a msg arrives, it is >GR> put in a variable in the thread's object, it then calls the notify() >GR> attr on the Condition object. For some reason, this doesn't work, the >GR> thread gets the msg, tries to notify the Condition object, fails because >GR> the lock has not been acquired yet and blocks. I tried ignoring the >GR> failure, thinking that since it has not been acquired yet then when it >GR> is, it will get the msg right away and never call Condition.wait, thus >GR> not causing any problems, but this does not work either. Does someone >GR> know what I am doing wrong? I attached the code to this msg. The code that puts the message in the variable should also acquire the lock: def onMessage(self, conn, msg): with self._cv: self.message = msg self._cv.notify() A couple of remarks: 1. I think the code is neater if all manipulation with the condition is done in the same class (actually in the same instance -- making this instance into a monitor). class Listener(Thread): def __init__(self, ws): Thread.__init__(self) self.interrupt = Event() self.message = None self._cv = Condition() self.client = ws._client self.client.RegisterHandler('message', self.onMessage) def onMessage(self, conn, msg): with self._cv: self.message = msg try: self._cv.notify() except RuntimeError: print "self._cv has not acquired the lock yet" def getMsg(self): with self._cv: while !self.message self._cv.wait() return self.message class WS(object): def __init__(self, username, password, res): self._jid = xmpp.protocol.JID(username) self._client = xmpp.Client(self._jid.getDomain()) # self._cv = Condition() def getMsg(self, mid=None): """ """ return self._listener.getMsg() Of course I haven't tested this code as I don't have the context modules. 2. I don't know if more than one message can be delivered in the same instance. If yes, than your code will not work, and neither will the code above as, the message instance variable is never cleared. So the next getMsg will be happy to deliver the previous one. You would have to clear it when returning this one. def getMsg(self): with self._cv: while !self.message self._cv.wait() msg = self.message self.message = None return msg 3. If the messages come in faster than they can be processed some will be lost as they will overwrite the previous one in the self.message variable. The solution is to use a threading.Queue to transfer the messages from one thread to the other. This also saves you the hassle of doing your own synchronisation like above. If you are not familiar with synchronising multithreaded applications it is very easy to make errors and even if you are it is quite easy to do them wrong. I have been involved in distributed programming courses at university level and I have seen many errors in this area. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mail.to.daniel.platz at googlemail.com Sat Jul 11 18:33:32 2009 From: mail.to.daniel.platz at googlemail.com (Daniel Platz) Date: Sat, 11 Jul 2009 15:33:32 -0700 (PDT) Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: Hi, thanks for your repleys. I have tried matplotlib but it is extremely slow. I think it is more optimized for good looking plots instead of speed. I do not know the Python bindings of gnuplot and Veusz. To clarify the issue again, by 25000 data points I mean 25000 pixels, i.e. corresponding (x,y) pairs. Thus the mapping to one pixel on the screen is not unique. Now, that I have to implement this problem on my own I am very impressed by the plotting capabilities of LabView which brings thounds of data points on screen in an extremely short time. Thanks for your help. Best regards, Daniel From ben+python at benfinney.id.au Sat Jul 11 19:27:23 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 12 Jul 2009 09:27:23 +1000 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <87vdlyls10.fsf@benfinney.id.au> Daniel Platz writes: > I do not know the Python bindings of gnuplot and Veusz. A web search does, though. -- \ ?The problem with television is that the people must sit and | `\ keep their eyes glued on a screen: the average American family | _o__) hasn't time for it.? ?_The New York Times_, 1939 | Ben Finney From ryankaskel at gmail.com Sat Jul 11 19:43:10 2009 From: ryankaskel at gmail.com (Ryan K) Date: Sat, 11 Jul 2009 16:43:10 -0700 (PDT) Subject: Addind imports to a class namespace References: <89bd4a17-8476-41bc-8728-bf840a64390b@s6g2000vbp.googlegroups.com> Message-ID: <3a6a9820-ab17-4ddc-a75b-a6b49b919af3@p15g2000vbl.googlegroups.com> Thank you for your replies. I have factored out the dependency and everything is solved. Cheers, Ryan From ldo at geek-central.gen.new_zealand Sat Jul 11 20:21:12 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 12 Jul 2009 12:21:12 +1200 Subject: 2.4 VS 3.1 for simple print References: <87hbxkm7n2.fsf@benfinney.id.au> Message-ID: In message <87hbxkm7n2.fsf at benfinney.id.au>, Ben Finney wrote: > For this and other differences introduced in the Python 3.x series, see > . People never thank you for an "RTFM" response. From ldo at geek-central.gen.new_zealand Sat Jul 11 20:25:02 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 12 Jul 2009 12:25:02 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , Aahz wrote: > It helps to remember that names and namespaces are in many > ways syntactic sugar for dicts or lists. Interesting, though, that Python insists on maintaining a distinction between c["x"] and c.x, whereas JavaScript doesn't bother. From greg at cosc.canterbury.ac.nz Sat Jul 11 21:28:17 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sun, 12 Jul 2009 13:28:17 +1200 Subject: tough-to-explain Python In-Reply-To: References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com><00720d76$0$9710$c3e8da3@news.astraweb.com> <007318de$0$9710$c3e8da3@news.astraweb.com> Message-ID: <7bssfuF24o01hU1@mid.individual.net> Hendrik van Rooyen wrote: > The creativity could, arguably, be in the "Design". > Not in the translation to python, or assembler. > No way. That is just coding. No, the mechanical part of the process is called compiling, and we have programs to do it for us. By the time you've specified the design so rigorously that not the slightest spark of creativity is needed to implement it, you *have* coded it. -- Greg From greg at cosc.canterbury.ac.nz Sat Jul 11 21:34:27 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sun, 12 Jul 2009 13:34:27 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) In-Reply-To: References: Message-ID: <7bssrgF251u00U1@mid.individual.net> Lawrence D'Oliveiro wrote: > Interesting, though, that Python insists on maintaining a distinction > between c["x"] and c.x, whereas JavaScript doesn't bother. And that distinction is a good thing. It means, for example, that dictionaries can have methods without colliding with the key space of the items put into them. -- Greg From clp2 at rebertia.com Sat Jul 11 21:50:28 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 11 Jul 2009 18:50:28 -0700 Subject: explode() In-Reply-To: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: <50697b2c0907111850sda1fd84k65f017f2553b639a@mail.gmail.com> On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: > ? ? ? ?What is the Python equivalent of the PHP explode() function? some_string.split("separator") Cheers, Chris -- http://blog.rebertia.com From fatkinson at mishmash.com Sat Jul 11 22:41:12 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Sat, 11 Jul 2009 19:41:12 -0700 Subject: explode() Message-ID: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> What is the Python equivalent of the PHP explode() function? Fred From sjmachin at lexicon.net Sat Jul 11 23:01:35 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 12 Jul 2009 03:01:35 +0000 (UTC) Subject: A zlib question References: <80F42625-5FB2-438F-AFF2-913236A6D336@catalogix.se> Message-ID: Roland Hedberg catalogix.se> writes: > I have a problem with zlib and compressing/decompressing according to > RFC 1951. > > It seems like I can decompress, something compressed according to RFC > 1951 by someone else, provided I set wbits to something negative (used > -8 but I guess any negative number would work?). -15 will get you a 32KB window which is the best and is the default in APIs where a default is possible. original_data = zlib.decompress(deflated_data, -15) > But how can I compress using zlib so it doesn't add a gzip header ? You don't need to, because zlib doesn't add a gzip header (RFC 1952) -- it adds a zlib header (RFC 1950) ... without out any frills (i.e. default case) a zlib stream is a 2-byte header plus the RFC 1951 deflate stream plus a 4-byte checksum. deflated_data = zlib.compress(uncompressed_data)[2:-4] Coincidentally this question arose elsewhere very recently: see http://stackoverflow.com/questions/1089662/ python-inflate-and-deflate-implementations concatenate the above two lines -- gmane <= 80-byte line limit 8-P BTW FWIW, one can evidently force zlib to do a gzip wrapper instead of a zlib wrapper by setting wbits to positive > 15 [brilliant API design], but one might be better off using the gzip module directly ... HTH, John From fatkinson at mishmash.com Sat Jul 11 23:20:41 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Sat, 11 Jul 2009 20:20:41 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert wrote: >On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >> ? ? ? ?What is the Python equivalent of the PHP explode() function? > >some_string.split("separator") > >Cheers, >Chris Thanks, Fred From aahz at pythoncraft.com Sun Jul 12 00:23:43 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 Jul 2009 21:23:43 -0700 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In article , Lawrence D'Oliveiro wrote: >In message , Aahz wrote: >> >> It helps to remember that names and namespaces are in many >> ways syntactic sugar for dicts or lists. > >Interesting, though, that Python insists on maintaining a distinction >between c["x"] and c.x, whereas JavaScript doesn't bother. Why do you say "insists"? class AttrDict: def __getitem__(self, key): return getattr(self, key) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "as long as we like the same operating system, things are cool." --piranha From blepctc at free.fr Sun Jul 12 03:47:32 2009 From: blepctc at free.fr (Baptiste Lepilleur) Date: Sun, 12 Jul 2009 09:47:32 +0200 Subject: Looking for a tool to checkfor python script backward compatibility Message-ID: <4a59951a$0$10853$426a74cc@news.free.fr> I'm looking for a tool that could be used in a pre-commit step to check that only features available in a "old" python version are used, say python 2.3 for example. Does any one know of one ? From vs at it.uu.se Sun Jul 12 05:21:51 2009 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 12 Jul 2009 11:21:51 +0200 Subject: PyODE Message-ID: <4A59AB2F.4060708@it.uu.se> Does anyone have PyODE running on Python 2.6.2? --V From ben+python at benfinney.id.au Sun Jul 12 05:34:56 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 12 Jul 2009 19:34:56 +1000 Subject: 2.4 VS 3.1 for simple print References: <87hbxkm7n2.fsf@benfinney.id.au> Message-ID: <87r5wmkzwf.fsf@benfinney.id.au> Lawrence D'Oliveiro writes: > In message <87hbxkm7n2.fsf at benfinney.id.au>, Ben Finney wrote: > > > For this and other differences introduced in the Python 3.x series, see > > . > > People never thank you for an "RTFM" response. It's a good thing I avoid giving such responses, then. (Pointing someone to the specific document isn't ?RTFM?.) -- \ ?Leave nothing to chance. Overlook nothing. Combine | `\ contradictory observations. Allow yourself enough time.? | _o__) ?Hippocrates | Ben Finney From skip at pobox.com Sun Jul 12 09:01:23 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 12 Jul 2009 08:01:23 -0500 Subject: Implementing a cache In-Reply-To: <87ab3cwu3u.fsf@vostro.rath.org> References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: <19033.56995.644542.486528@montanaro.dyndns.org> Nikolaus> I want to implement a caching data structure in Python that Nikolaus> allows me to: Nikolaus> 1. Quickly look up objects using a key Nikolaus> 2. Keep track of the order in which the objects are accessed Nikolaus> (most recently and least recently accessed one, not a Nikolaus> complete history) Nikolaus> 3. Quickly retrieve and remove the least recently accessed Nikolaus> object. My Cache module does #1 and #3. I'm not sure if you want #2 for internal cache maintenance or for as part of the API. http://www.smontanaro.net/python/Cache.py Skip From pdpinheiro at gmail.com Sun Jul 12 09:27:23 2009 From: pdpinheiro at gmail.com (pdpi) Date: Sun, 12 Jul 2009 06:27:23 -0700 (PDT) Subject: Implementing a cache References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: On Jul 12, 2:01?pm, s... at pobox.com wrote: > ? ? Nikolaus> I want to implement a caching data structure in Python that > ? ? Nikolaus> allows me to: > > ? ? Nikolaus> ?1. Quickly look up objects using a key > ? ? Nikolaus> ?2. Keep track of the order in which the objects are accessed > ? ? Nikolaus> ? ? (most recently and least recently accessed one, not a > ? ? Nikolaus> ? ? complete history) > ? ? Nikolaus> ?3. Quickly retrieve and remove the least recently accessed > ? ? Nikolaus> ? ? object. > > My Cache module does #1 and #3. ?I'm not sure if you want #2 for internal > cache maintenance or for as part of the API. > > ? ?http://www.smontanaro.net/python/Cache.py > > Skip I'm not sure whether #2 is doable at all, as written. You _need_ a complete history (at least the full ordering of the items in the cache) to be able to tell what the least recently used item is. From exarkun at divmod.com Sun Jul 12 09:39:43 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Sun, 12 Jul 2009 09:39:43 -0400 Subject: Looking for a tool to checkfor python script backward compatibility In-Reply-To: <4a59951a$0$10853$426a74cc@news.free.fr> Message-ID: <20090712133943.2543.62461196.divmod.quotient.1539@henry.divmod.com> On Sun, 12 Jul 2009 09:47:32 +0200, Baptiste Lepilleur wrote: >I'm looking for a tool that could be used in a pre-commit step to check that >only features available in a "old" python version are used, say python 2.3 >for example. > >Does any one know of one ? > Run your test suite with whichever versions of Python you want to support. Tools like Hudson () and BuildBot () can help with this. Jean-Paul From stef.mientki at gmail.com Sun Jul 12 09:40:27 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 12 Jul 2009 15:40:27 +0200 Subject: how to run the "main" section of another module ? Message-ID: <4A59E7CB.5020908@gmail.com> hello, when I''m working in a library, and want to test some of the library functions, I need to switch to a main application, (which has a normal main-section) and run that. If the library is simply, I add a main section to the library, so no problem. But if the library requires a lot of overhead, I want to run some main program, that has a main section, and uses my library under construction. So how do I call from within the library module, in a main section in that library module, a main in another module ? thanks, Stef Mientki From skip at pobox.com Sun Jul 12 09:40:45 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 12 Jul 2009 08:40:45 -0500 Subject: Implementing a cache In-Reply-To: References: <87ab3cwu3u.fsf@vostro.rath.org> Message-ID: <19033.59357.223527.248051@montanaro.dyndns.org> >> My Cache module does #1 and #3. ?I'm not sure if you want #2 for >> internal cache maintenance or for as part of the API. >> >> ? ?http://www.smontanaro.net/python/Cache.py pdpi> I'm not sure whether #2 is doable at all, as written. You _need_ a pdpi> complete history (at least the full ordering of the items in the pdpi> cache) to be able to tell what the least recently used item is. I should have added that my Cache module does maintain a history. (Clearly it needs that to determine the LRU item.) It is exposed as part of the API via the ftimes method. S From stef.mientki at gmail.com Sun Jul 12 09:47:27 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 12 Jul 2009 15:47:27 +0200 Subject: how to run the "main" section of another module ? In-Reply-To: <4A59E7CB.5020908@gmail.com> References: <4A59E7CB.5020908@gmail.com> Message-ID: <4A59E96F.6080709@gmail.com> Stef Mientki wrote: > hello, > > when I''m working in a library, > and want to test some of the library functions, > I need to switch to a main application, > (which has a normal main-section) > and run that. > > If the library is simply, > I add a main section to the library, > so no problem. > > But if the library requires a lot of overhead, > I want to run some main program, > that has a main section, > and uses my library under construction. > > So how do I call from within the library module, > in a main section in that library module, > a main in another module ? > > thanks, > Stef Mientki btw, I just found this in one of my programs (which works but isn't very beautiful) In the library fill under construction I put this main section: (sb_test.py is the main program, that has a main section and uses the library under construction) if __name__ == '__main__': import db_test new_globals = {} new_globals [ '__name__' ] = '__main__' new_globals [ '__file__' ] = 'not really valuable' execfile ( 'db_test.py', new_globals ) From aahz at pythoncraft.com Sun Jul 12 10:21:55 2009 From: aahz at pythoncraft.com (Aahz) Date: 12 Jul 2009 07:21:55 -0700 Subject: how to run the "main" section of another module ? References: Message-ID: In article , Stef Mientki wrote: > >when I''m working in a library, and want to test some of the library >functions, I need to switch to a main application, (which has a normal >main-section) and run that. if __name__ == '__main__': main() Then you can call main() from another module for testing purposes. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From piet at cs.uu.nl Sun Jul 12 11:54:30 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 12 Jul 2009 17:54:30 +0200 Subject: how to run the "main" section of another module ? References: <4A59E7CB.5020908@gmail.com> Message-ID: >>>>> Stef Mientki (SM) wrote: >SM> Stef Mientki wrote: >>> hello, >>> >>> when I''m working in a library, >>> and want to test some of the library functions, >>> I need to switch to a main application, >>> (which has a normal main-section) >>> and run that. >>> >>> If the library is simply, >>> I add a main section to the library, >>> so no problem. >>> >>> But if the library requires a lot of overhead, >>> I want to run some main program, >>> that has a main section, >>> and uses my library under construction. >>> >>> So how do I call from within the library module, >>> in a main section in that library module, >>> a main in another module ? >>> >>> thanks, >>> Stef Mientki >SM> btw, >SM> I just found this in one of my programs (which works but isn't very >SM> beautiful) >SM> In the library fill under construction I put this main section: >SM> (sb_test.py is the main program, that has a main section and uses the >SM> library under construction) >SM> if __name__ == '__main__': >SM> import db_test >SM> new_globals = {} >SM> new_globals [ '__name__' ] = '__main__' >SM> new_globals [ '__file__' ] = 'not really valuable' >SM> execfile ( 'db_test.py', new_globals ) Why not: import db_test db_test.main() I think that is what Aahz meant. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From bjorn.m.meyer at gmail.com Sun Jul 12 13:16:13 2009 From: bjorn.m.meyer at gmail.com (Bjorn Meyer) Date: Sun, 12 Jul 2009 11:16:13 -0600 Subject: multiprocessing and dictionaries Message-ID: <200907121116.13828.bjorn.m.meyer@gmail.com> I am trying to convert a piece of code that I am using the thread module with to the multiprocessing module. The way that I have it set up is a chunk of code reads a text file and assigns a dictionary key multiple values from the text file. I am using locks to write the values to the dictionary. The way that the values are written is as follows: mydict.setdefault(key, []).append(value) The problem that I have run into is that using multiprocessing, the key gets set, but the values don't get appended. I've even tried the Manager().dict() option, but it doesn't seem to work. Is this not supported at this time or am I missing something? Thanks in advance. Bjorn From stef.mientki at gmail.com Sun Jul 12 14:13:53 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 12 Jul 2009 20:13:53 +0200 Subject: how to run the "main" section of another module ? In-Reply-To: References: <4A59E7CB.5020908@gmail.com> Message-ID: <4A5A27E1.1090502@gmail.com> >> SM> if __name__ == '__main__': >> SM> import db_test >> SM> new_globals = {} >> SM> new_globals [ '__name__' ] = '__main__' >> SM> new_globals [ '__file__' ] = 'not really valuable' >> SM> execfile ( 'db_test.py', new_globals ) >> > > Why not: > > import db_test > db_test.main() > > I think that is what Aahz meant. > > Yes I tried that too, but it gives the following error: "module object not callable" db_text.__main__() gives error: 'module' object has no attribute '__main__' cheers, Stef From aahz at pythoncraft.com Sun Jul 12 14:44:10 2009 From: aahz at pythoncraft.com (Aahz) Date: 12 Jul 2009 11:44:10 -0700 Subject: how to run the "main" section of another module ? References: <4A59E7CB.5020908@gmail.com> Message-ID: In article , Stef Mientki wrote: >Stef deleted an attribution: >> >> Why not: >> >> import db_test >> db_test.main() > >Yes I tried that too, but it gives the following error: "module object >not callable" You need to create a main() function in db_test first. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From emile at fenx.com Sun Jul 12 14:48:25 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 12 Jul 2009 11:48:25 -0700 Subject: how to run the "main" section of another module ? In-Reply-To: <4A5A27E1.1090502@gmail.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: On 7/12/2009 11:13 AM Stef Mientki said... > >>> SM> if __name__ == '__main__': >>> SM> import db_test >>> SM> new_globals = {} >>> SM> new_globals [ '__name__' ] = '__main__' >>> SM> new_globals [ '__file__' ] = 'not really valuable' >>> SM> execfile ( 'db_test.py', new_globals ) >>> >> >> Why not: implied here is that you restructure... >> >> import db_test >> db_test.main() >> >> I think that is what Aahz meant. >> >> > Yes I tried that too, > but it gives the following error: > "module object not callable" > > > db_text.__main__() > gives error: > 'module' object has no attribute '__main__' > You see, we're further assuming you've structured the module for this purpose. IOW, your module should end with: if __name__ == '__main__': main() and nothing more. Then, you can do: import db_test db_test.main() Emile From cameron.pulsford at gmail.com Sun Jul 12 15:11:55 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Sun, 12 Jul 2009 15:11:55 -0400 Subject: Question about generators In-Reply-To: References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Hey everyone, I have this small piece of code that simply finds the factors of a number. import sys def factor(n): primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! = 0) factors = [] for i in [2, 3, 5]: while n % i == 0: n /= i factors.append(i) for i in primes: while n % i == 0: n /= i factors.append(i) print factors factor(int(sys.argv[1])) My question is, is it possible to combine those two loops? The primes generator I wrote finds all primes up to n, except for 2, 3 and 5, so I must check those explicitly. Is there anyway to concatenate the hard coded list of [2,3,5] and the generator I wrote so that I don't need two for loops that do the same thing? I tried writing a primes function using yield statements, but it didn't work like I thought it would. From alan.kesselmann at gmail.com Sun Jul 12 15:16:29 2009 From: alan.kesselmann at gmail.com (zayatzz) Date: Sun, 12 Jul 2009 12:16:29 -0700 (PDT) Subject: Sockets and threading Message-ID: <0b77e7b9-85cd-4a23-b975-5cb8ac59fe7e@j19g2000vbp.googlegroups.com> Im trying to get aquinted to python on bit more basic level and am following socket and threading programming tutorials from these 2 addresses : http://heather.cs.ucdavis.edu/~matloff/Python/PyNet.pdf http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf in this PyThreads file he sets up threaded server app which listens to what clients send to it and echos it back to clients while adding sent info into one string. The problem is that the code does not work for me. class srvr(threading.Thread): v = "" vlock = threading.Lock() id = 0 def __init__(self,clntsock): threading.Thread.__init__(self) self.myid = srvr.id srvr.id += 1 self.myclntsock = clntsock def run(self): while 1: k = self.myclntsock.recv(1) if k == "": break srvr.vlock.acquire() srvr.v += k srvr.vlock.release() self.myclntsock.send(srvr.v) self.myclntsock.close() Instead of sendint back i get this error : File "server3.py", line 31, in serveclient k = self.myclntsock.recv(1) File "/usr/lib/python2.6/socket.py", line 165, in _dummy raise error(EBADF, 'Bad file descriptor') socket.error: [Errno 9] Bad file descriptor As much as i understand this line 31 is supposed to recieve stuff from clients and has buffer size 1. I dont understand what this has to do with file descriptor.. whatever that is anyway. Alan From vodkalove at gmail.com Sun Jul 12 15:18:04 2009 From: vodkalove at gmail.com (Riley Crane) Date: Sun, 12 Jul 2009 12:18:04 -0700 (PDT) Subject: MySQLdb + SSH Tunnel Message-ID: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> OVERVIEW: I am running a script on one machine that connects to a MySQL database on another machine that is outside of our university's domain. According to the administrator, network policies do not allow the compute nodes to access machines outside of our university's domain. COMPUTERS: A = compute node within university (I do not have shell access) B = 2nd machine within university that does not block outside connections (I have root access) C = machine outside of university (I have root access) mysqldb on A cannot connect to C ....but..... mysqldb on A can connect to B WHAT I WOULD LIKE TO DO: Is it possible to set something up where A talks to a port on B, and that port is actually nothing more than 3306 on C? Can I do this with an SSH tunnel? Can anyone please give precise instructions? From emile at fenx.com Sun Jul 12 15:41:31 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 12 Jul 2009 12:41:31 -0700 Subject: MySQLdb + SSH Tunnel In-Reply-To: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: On 7/12/2009 12:18 PM Riley Crane said... > OVERVIEW: > I am running a script on one machine that connects to a MySQL database > on another machine that is outside of our university's domain. > According to the administrator, network policies do not allow the > compute nodes to access machines outside of our university's domain. > > COMPUTERS: > A = compute node within university (I do not have shell access) > B = 2nd machine within university that does not block outside > connections (I have root access) ...so B can talk to C presumably... > C = machine outside of university (I have root access) > mysqldb on A cannot connect to C ....but..... > mysqldb on A can connect to B > > WHAT I WOULD LIKE TO DO: > Is it possible to set something up where A talks to a port on B, and > that port is actually nothing more than 3306 on C? Can I do this with > an SSH tunnel? > > Can anyone please give precise instructions? ssh with something like... ssh -lroot -L3306:C:3306 B Watch out for other instances of mysql on A or B... Emile From mensanator at aol.com Sun Jul 12 15:47:46 2009 From: mensanator at aol.com (Mensanator) Date: Sun, 12 Jul 2009 12:47:46 -0700 (PDT) Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: <173f9127-2846-46a4-8392-6a7f03fde6e1@j19g2000vbp.googlegroups.com> On Jul 12, 2:11?pm, Cameron Pulsford wrote: > Hey everyone, I have this small piece of code that simply finds the ? > factors of a number. > > import sys > > def factor(n): > ? ? ?primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! > = 0) > > ? ? ?factors = [] > > ? ? ?for i in [2, 3, 5]: > ? ? ? ? ?while n % i == 0: > ? ? ? ? ? ? ?n /= i > ? ? ? ? ? ? ?factors.append(i) > ? ? ?for i in primes: > ? ? ? ? ?while n % i == 0: > ? ? ? ? ? ? ?n /= i > ? ? ? ? ? ? ?factors.append(i) > ? ? ?print factors > > factor(int(sys.argv[1])) > > My question is, is it possible to combine those two loops? Yeah, get rid of the first loop. > The primes ? > generator I wrote finds all primes up to n, except for 2, 3 and 5, so ? > I must check those explicitly. Is there anyway to concatenate the hard ? > coded list of [2,3,5] and the generator I wrote so that I don't need ? > two for loops that do the same thing? primes.extend([2,3,5]) > > I tried writing a primes function using yield statements, but it ? > didn't work like I thought it would. From vilya.harvey at gmail.com Sun Jul 12 15:55:49 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Sun, 12 Jul 2009 20:55:49 +0100 Subject: Question about generators In-Reply-To: <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: <6aef848f0907121255n7d5cff37r6b01828cfb0ba6db@mail.gmail.com> 2009/7/12 Cameron Pulsford : > My question is, is it possible to combine those two loops? The primes > generator I wrote finds all primes up to n, except for 2, 3 and 5, so I must > check those explicitly. Is there anyway to concatenate the hard coded list > of [2,3,5] and the generator I wrote so that I don't need two for loops that > do the same thing? itertools.chain([2, 3, 5], primes) is what you're looking for, I think. Vil. From rhodri at wildebst.demon.co.uk Sun Jul 12 16:13:58 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 12 Jul 2009 21:13:58 +0100 Subject: need to write a assembly progrm In-Reply-To: References: Message-ID: On Sat, 11 Jul 2009 05:17:18 +0100, Dennis Lee Bieber wrote: > On Fri, 10 Jul 2009 13:25:49 +0100, "Rhodri James" > declaimed the following in > gmane.comp.python.general: > >> On Thu, 09 Jul 2009 11:52:44 +0100, m.reddy prasad reddy >> wrote: >> >> > my aim is to run the assembly programs in python.by that we can use >> that >> > in >> > the any labs.because we can run the python in the mobiles also.if we >> > write >> > assembly programs in the mobile ,the mobile act as a tool kit for the >> > lab.tell me any other solutions for this >> >> Please define what you mean by "assembly". >> > The poorly punctuated paragraph sounds, to me, as if they mean they > want (Windows assumed) EXE files that can be dropped onto remote/laptop > machines without performing a full installation of Python... > > But yes... "assembly programs" does, to me, also mean things > containing mnemonics for machine level opcodes. Given that to me, "assembly programs" does not mean .EXE files at all, not even a little bit, I'm prefering to refuse to guess :-) -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Sun Jul 12 17:02:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 12 Jul 2009 23:02:46 +0200 Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> Message-ID: >>>>> Cameron Pulsford (CP) wrote: >CP> Hey everyone, I have this small piece of code that simply finds the >CP> factors of a number. Others have already given you advice to add the [2, 3, 5] to the iterator (of which the primes.extend([2,3,5]) will not work). Please allow me to make some other remarks. >CP> import sys >CP> def factor(n): >CP> primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! = 0) It is a bit misleading to call this `primes' as it will also contain non-primes. Of course they don't harm as they will never be considered a factor because all their prime factors will have been successfully removed before this non prime will be considered. >CP> factors = [] >CP> for i in [2, 3, 5]: >CP> while n % i == 0: >CP> n /= i >CP> factors.append(i) >CP> for i in primes: >CP> while n % i == 0: >CP> n /= i >CP> factors.append(i) You can stop the loop when n == 1. Like: if n == 1: break >CP> print factors >CP> factor(int(sys.argv[1])) >CP> My question is, is it possible to combine those two loops? The primes >CP> generator I wrote finds all primes up to n, except for 2, 3 and 5, so I >CP> must check those explicitly. Is there anyway to concatenate the hard coded >CP> list of [2,3,5] and the generator I wrote so that I don't need two for >CP> loops that do the same thing? >CP> I tried writing a primes function using yield statements, but it didn't >CP> work like I thought it would. See the recent thread `Why is my code faster with append() in a loop than with a large list?' http://mail.python.org/pipermail/python-list/2009-July/718931.html -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From gagsl-py2 at yahoo.com.ar Sun Jul 12 17:34:41 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 12 Jul 2009 18:34:41 -0300 Subject: Sockets and threading References: <0b77e7b9-85cd-4a23-b975-5cb8ac59fe7e@j19g2000vbp.googlegroups.com> Message-ID: En Sun, 12 Jul 2009 16:16:29 -0300, zayatzz escribi?: > while 1: > k = self.myclntsock.recv(1) > if k == "": break > srvr.vlock.acquire() > srvr.v += k > srvr.vlock.release() > self.myclntsock.send(srvr.v) > self.myclntsock.close() > > Instead of sendint back i get this error : > File "server3.py", line 31, in serveclient > k = self.myclntsock.recv(1) > File "/usr/lib/python2.6/socket.py", line 165, in _dummy > raise error(EBADF, 'Bad file descriptor') > socket.error: [Errno 9] Bad file descriptor > > As much as i understand this line 31 is supposed to recieve stuff from > clients and has buffer size 1. I dont understand what this has to do > with file descriptor.. whatever that is anyway. That means the clntsock variable isn't an open, available socket. Note that you close the socket right after sending the response - are you sure you don't have an indentation error there? -- Gabriel Genellina From Samnsparky at gmail.com Sun Jul 12 17:50:01 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 14:50:01 -0700 (PDT) Subject: Webcam + GStreamer Message-ID: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> Hello! I need to stream from a webcam in Linux and I need to be able to analyze the video feed frame by frame with PIL. Currently my web- cam (Quickcam Chat) only seems to work with GStreamer so a solution using pygst would be preferred. Thanks for your help, Sam From piet at cs.uu.nl Sun Jul 12 17:52:50 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 12 Jul 2009 23:52:50 +0200 Subject: Sockets and threading References: <0b77e7b9-85cd-4a23-b975-5cb8ac59fe7e@j19g2000vbp.googlegroups.com> Message-ID: >>>>> zayatzz (z) wrote: >z> Im trying to get aquinted to python on bit more basic level and am >z> following socket and threading programming tutorials from these 2 >z> addresses : >z> http://heather.cs.ucdavis.edu/~matloff/Python/PyNet.pdf >z> http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf >z> in this PyThreads file he sets up threaded server app which listens to >z> what clients send to it and echos it back to clients while adding sent >z> info into one string. >z> The problem is that the code does not work for me. >z> class srvr(threading.Thread): >z> v = "" >z> vlock = threading.Lock() >z> id = 0 >z> def __init__(self,clntsock): >z> threading.Thread.__init__(self) >z> self.myid = srvr.id >z> srvr.id += 1 >z> self.myclntsock = clntsock >z> def run(self): >z> while 1: >z> k = self.myclntsock.recv(1) >z> if k == "": break >z> srvr.vlock.acquire() >z> srvr.v += k >z> srvr.vlock.release() >z> self.myclntsock.send(srvr.v) >z> self.myclntsock.close() The last line is wrongly indented. It should be outside the loop. Shift it left so that it lines up with the while. What happens now is that the socket is closed after the first recv(). Then the next round in the loop fails because the myclntsock is closed. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From Samnsparky at gmail.com Sun Jul 12 17:54:13 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 14:54:13 -0700 (PDT) Subject: Webcam + GStreamer References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> Message-ID: <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> On Jul 12, 3:50?pm, Sparky wrote: > Hello! I need to stream from a webcam in Linux and I need to be able > to analyze the video feed frame by frame with PIL. Currently my web- > cam (Quickcam Chat) only seems to work with GStreamer so a solution > using pygst would be preferred. > > Thanks for your help, > Sam Sorry, to clarify I am just having a hard time capturing frames in a way that I can access with PIL. From davoodvahdati2009 at gmail.com Sun Jul 12 18:24:57 2009 From: davoodvahdati2009 at gmail.com (Davood Vahdati) Date: Sun, 12 Jul 2009 15:24:57 -0700 (PDT) Subject: c++ Source Code for acm 2004-2005 problems Message-ID: <03d25e16-97d2-4b09-8113-5011c0e0ded9@p28g2000vbn.googlegroups.com> Dear Sirs And Madams : it is an Acm programming competition Questions in year 2004-2005 . could you please solve problems is question ? I Wan't C++ Source Code program About this questions OR Problems . thank you for your prompt attention to this matter your faithfully. ----------------------------------------- 29th ACM International Collegiate Sponsored by Programming Contest, 2004-2005 Sharif Preliminary Contest Sharif University of Technology, 28 Oct. 2004 Problem B (Program filename: B.CPP, B.DPR, B.PAS or B.java) Parallelogram Counting There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4- element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line. Input (filename: B.IN) The first line of the input file contains a single integer t (1 ? t ? 10), the number of test cases. It is followed by the input data for each test case. The first line of each test case contains an integer n (1 ? n ? 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000. Output (Standard Output) Output should contain t lines. Line i contains an integer showing the number of the parallelograms as described above for test case i. Sample Input 2 6 0 0 2 0 4 0 1 1 3 1 5 1 7 -2 -1 8 9 5 7 1 1 4 8 2 0 9 8 Sample Output 5 6 ---------------------------------------------------------------------------------------------------------------------- Problem B-Page 1 of 1 28th ACM International Collegiate Sponsored by Programming Contest, 2003-2004 Sharif Preliminary Contest Sharif University of Technology, 14 Nov. 2003 Problem C (Program filename: C.CPP or C.PAS or C.DPR or C.java) Toy Storage Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for Reza to find his favorite toys anymore. Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top: We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys. Input (filename: C.IN) The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0< n ? 1000) and the number of toys is given in m (0 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box. Sample Input 4 10 0 10 100 0 20 20 80 80 60 60 40 40 5 10 15 10 95 10 25 10 65 10 75 10 35 10 45 10 55 10 85 10 5 6 0 10 60 0 4 3 15 30 3 1 6 8 10 10 2 1 2 8 1 5 5 5 40 10 7 9 0 Sample Output Box 2: 5 Box 1: 4 2: 1 ---------------------------------------------------------------------- 29th ACM International Collegiate Sponsored by Programming Contest, 2004-2005 Sharif Preliminary Contest Sharif University of Technology, 28 Oct. 2004 Problem D (Program filename: D.CPP, D.DPR, or D.java) Software Company A software developing company has been assigned two programming projects. As both projects are within the same contract, both must be handed in at the same time. It does not help if one is finished earlier. This company has n employees to do the jobs. To manage the two projects more easily, each is divided into m independent subprojects. Only one employee can work on a single subproject at one time, but it is possible for two employees to work on different subprojects of the same project simultaneously. Our goal is to finish the projects as soon as possible. Input (filename: D.IN) The first line of the input file contains a single integer t (1 ? t ? 11), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers n (1 ? n ? 100), and m (1 ? m ? 100). The input for this test case will be followed by n lines. Each line contains two integers which specify how much time in seconds it will take for the specified employee to complete one subproject of each project. So if the line contains x and y, it means that it takes the employee x seconds to complete a subproject from the first project, and y seconds to complete a subproject from the second project. Output (Standard Output) There should be one line per test case containing the minimum amount of time in seconds after which both projects can be completed. Sample Input 1 3 20 1 1 2 4 1 6 Sample Output 18 Problem D -Page 1 of 1 29th ACM International Collegiate Sponsored by Programming Contest, 2004-2005 Sharif Preliminary Contest Sharif University of Technology, 28 Oct. 2004 ---------------------------------------------------------------------- Problem F (Program filename: F.CPP, F.DPR, or F.java) Median Weight Bead There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads: A scale is given to compare the weights of beads. We can determine which one is heavier than the other between two beads. As the result, we now know that some beads are heavier than others. We are going to remove some beads which cannot have the medium weight. For example, the following results show which bead is heavier after M comparisons where M=4 and N=5. 1. Bead 2 is heavier than Bead 1. 2. Bead 4 is heavier than Bead 3. 3. Bead 5 is heavier than Bead 1. 4. Bead 4 is heavier than Bead 2. >From the above results, though we cannot determine exactly which is the median bead, we know that Bead 1 and Bead 4 can never have the median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3 are lighter than Bead 4. Therefore, we can remove these two beads. Write a program to count the number of beads which cannot have the median weight. Input (filename: F.IN) The first line of the input file contains a single integer t (1 ? t ? 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows: The first line of input data contains an integer N (1=N=99) denoting the number of beads, and M denoting the number of pairs of beads compared. In each of the next M lines, two numbers are given where the first bead is heavier than the second bead. Output (Standard Output) There should be one line per test case. Print the number of beads which can never have the medium weight. Sample Input Sample Output 1 2 5 4 2 1 4 3 5 1 4 2 Problem F -Page 1 of 2 From david at pythontoo.com Sun Jul 12 18:30:12 2009 From: david at pythontoo.com (David) Date: Sun, 12 Jul 2009 18:30:12 -0400 Subject: Webcam + GStreamer In-Reply-To: <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> Message-ID: <4A5A63F4.6010301@pythontoo.com> Sparky wrote: > On Jul 12, 3:50 pm, Sparky wrote: >> Hello! I need to stream from a webcam in Linux and I need to be able >> to analyze the video feed frame by frame with PIL. Currently my web- >> cam (Quickcam Chat) only seems to work with GStreamer so a solution >> using pygst would be preferred. >> >> Thanks for your help, >> Sam > > Sorry, to clarify I am just having a hard time capturing frames in a > way that I can access with PIL. Most web cams produce jpeg images so read the note at the bottom here; http://effbot.org/imagingbook/format-jpeg.htm What exactly are you trying to do? What have you tried so far and what happened may help. -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From ptmcg at austin.rr.com Sun Jul 12 18:46:05 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sun, 12 Jul 2009 15:46:05 -0700 (PDT) Subject: c++ Source Code for acm 2004-2005 problems References: <03d25e16-97d2-4b09-8113-5011c0e0ded9@p28g2000vbn.googlegroups.com> Message-ID: On Jul 12, 5:24?pm, Davood Vahdati wrote: > Dear Sirs And Madams : > > it is an Acm programming competition Questions in year 2004-2005 . > could you please solve problems is question ? I ?Wan't C++ Source Code > program About this questions OR Problems . thank you for your prompt > attention to this matter > > 2 1 > 4 3 > 5 1 > 4 2 > looking for the Python content in this post... mmmmm, nope, didn't find any... I guess the OP tried on a C++ newsgroup and got told to do his own homework, so he came here instead? From gagsl-py2 at yahoo.com.ar Sun Jul 12 19:13:43 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 12 Jul 2009 20:13:43 -0300 Subject: c++ Source Code for acm 2004-2005 problems References: <03d25e16-97d2-4b09-8113-5011c0e0ded9@p28g2000vbn.googlegroups.com> Message-ID: En Sun, 12 Jul 2009 19:24:57 -0300, Davood Vahdati escribi?: > it is an Acm programming competition Questions in year 2004-2005 . > could you please solve problems is question ? I Wan't C++ Source Code > program About this questions OR Problems . thank you for your prompt > attention to this matter Not C++ code but Python: A brute force approach to the first problem. > Parallelogram Counting > There are n distinct points in the plane, given by their integer > coordinates. Find the number of parallelograms whose > vertices lie on these points. class Point: def __init__(self, x, y): self.x, self.y = x, y def __repr__(self): return 'Point(%d,%d)' % (self.x, self.y) class Segment: def __init__(self, p0, p1): self.p0, self.p1 = p0, p1 def is_parallel(self, other): return ((self.p1.x-self.p0.x) * (other.p1.y-other.p0.y) - (self.p1.y-self.p0.y) * (other.p1.x-other.p0.x) == 0) points = [ Point(-2,-1), Point(8,9), Point(5,7), Point(1,1), Point(4,8), Point(2,0), Point(9,8)] n = 0 for i,A in enumerate(points): for B in points[i+1:]: AB = Segment(A,B) for C in points: if C in (A,B): continue BC = Segment(B,C) for D in points: if D in (A,B,C): continue CD = Segment(C,D) if AB.is_parallel(CD) and BC.is_parallel(Segment(A,D)): print A,B,C,D n += 1 n /= 4 # ABCD,BCDA,CDAB,DABC ## BACD etc already removed print n -- Gabriel Genellina From clp2 at rebertia.com Sun Jul 12 19:52:06 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 12 Jul 2009 16:52:06 -0700 Subject: multiprocessing and dictionaries In-Reply-To: <200907121116.13828.bjorn.m.meyer@gmail.com> References: <200907121116.13828.bjorn.m.meyer@gmail.com> Message-ID: <50697b2c0907121652j6cf27749xffd6057a3e01f7c@mail.gmail.com> On Sun, Jul 12, 2009 at 10:16 AM, Bjorn Meyer wrote: > I am trying to convert a piece of code that I am using the thread module with > to the multiprocessing module. > > The way that I have it set up is a chunk of code reads a text file and assigns > a dictionary key multiple values from the text file. I am using locks to write > the values to the dictionary. > The way that the values are written is as follows: > ? ? ? ?mydict.setdefault(key, []).append(value) > > The problem that I have run into is that using multiprocessing, the key gets > set, but the values don't get appended. Don't have much concurrency experience, but have you tried using a defaultdict instead? It's possible its implementation might solve the problem. http://docs.python.org/dev/library/collections.html#collections.defaultdict Cheers, Chris -- http://blog.rebertia.com From Samnsparky at gmail.com Sun Jul 12 20:01:55 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 17:01:55 -0700 (PDT) Subject: Webcam + GStreamer References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> Message-ID: <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> On Jul 12, 4:30?pm, David wrote: > Sparky wrote: > > On Jul 12, 3:50 pm, Sparky wrote: > >> Hello! I need to stream from a webcam in Linux and I need to be able > >> to analyze the video feed frame by frame with PIL. Currently my web- > >> cam (Quickcam Chat) only seems to work with GStreamer so a solution > >> using pygst would be preferred. > > >> Thanks for your help, > >> Sam > > > Sorry, to clarify I am just having a hard time capturing frames in a > > way that I can access with PIL. > > Most web cams produce jpeg images so read the note at the bottom here;http://effbot.org/imagingbook/format-jpeg.htm > What exactly are you trying to do? What have you tried so far and what > happened may help. > > -- > Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com Dear David, Thank you for your quick response. I have tried a few things. First of all, I have tried gst-launch-0.10 v4l2src ! ffmpegcolorspace ! pngenc ! filesink location=foo.png. Foo.png comes out sharp enough but it takes around 2 seconds to complete. I have also tried CVTypes but it does not run without LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so and, when it does run, it only displays colored "snow". Here is that code: import pygame import Image from pygame.locals import * import sys import opencv #this is important for capturing/displaying images from opencv import highgui camera = highgui.cvCreateCameraCapture(-1) print "cam:" + str(camera) def get_image(): print "here" im = highgui.cvQueryFrame(camera) #convert Ipl image to PIL image return opencv.adaptors.Ipl2PIL(im) fps = 30.0 pygame.init() window = pygame.display.set_mode((320,240)) pygame.display.set_caption("WebCam Demo") screen = pygame.display.get_surface() while True: events = pygame.event.get() im = get_image() print im.mode pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) screen.blit(pg_img, (0,0)) pygame.display.flip() pygame.time.delay(int(1000 * 1.0/fps)) Finally, I have gotten pygst to stream video with the example at http://pygstdocs.berlios.de/pygst-tutorial/webcam-viewer.html but of course I do not know how to get a hold of that data. Just so you know, I am trying a primitive type of object tracking. I would use some of the libraries already available but the two more popular implementations on Linux (tbeta/ccv and reacTIVision) dont seem to work with my web cam. I have more info on those non-python attempts at http://ubuntuforums.org/showthread.php?p=7596908. Unfortunately no one seemed to respond to that post. Thanks again, Sam From half.italian at gmail.com Sun Jul 12 20:03:32 2009 From: half.italian at gmail.com (Sean DiZazzo) Date: Sun, 12 Jul 2009 17:03:32 -0700 (PDT) Subject: shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception References: <9C99FA09-CEE5-4D05-AD92-6F6D42B0EFE0@usc.edu> Message-ID: On Jul 10, 5:10?pm, Tim Chase wrote: > > ? ? ?shutil.rmtree(filename) > > ? ?File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree > > ? ? ?onerror(os.rmdir, path, sys.exc_info()) > > ? ?File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree > > ? ? ?os.rmdir(path) > > OSError: [Errno 39] Directory not empty: /path/to/my/dir > > > According to the documentation, shutil.rmtree should not care about ? > > directory being not empty. > > This sounds suspiciously like a permission issue. ?rmtree() > *should* walk the tree removing items *if it can*. ?If a file > can't be deleted, it treats it as an error. ?rmtree() takes > parameters for ignore_errors and an onerror callback function, so > you can catch these error conditions. > > -tkc This one took me a long time to find a solution for. Check this page, and see comment #3: http://code.activestate.com/recipes/193736/ I guess if the file is marked as "Read Only" or "Archive", or whatever, it cannot be deleted with shutil.rmtree() The key: win32api.SetFileAttributes(path, win32con.FILE_ATTRIBUTE_NORMAL) It will work! ~Sean From tjreedy at udel.edu Sun Jul 12 20:15:00 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 12 Jul 2009 20:15:00 -0400 Subject: Question about generators In-Reply-To: <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: Cameron Pulsford wrote: When you start a new thread, you should start a new thread and not piggyback on an existing thread. From cameron.pulsford at gmail.com Sun Jul 12 21:24:29 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Sun, 12 Jul 2009 21:24:29 -0400 Subject: Question about generators In-Reply-To: References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: itertools.chain() did it, thanks! As far as the primes generator, it does not generate any non-primes. All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) where is x is [1, 2, ..., n]. The only time it doesn't generate a prime is when x + (1 or 5) % 5 == 0. Which is what that last part is making sure doesn't happen. I'm not a mathematician or anything so correct me if I'm wrong, but that's what I've read. Also sorry if this was piggy backed, I started the thread as a fresh e- mail to python-list at python.org, sorry if I messed something up! On Jul 12, 2009, at 8:15 PM, Terry Reedy wrote: > Cameron Pulsford wrote: > > When you start a new thread, you should start a new thread and not > piggyback on an existing thread. > > -- > http://mail.python.org/mailman/listinfo/python-list From david at pythontoo.com Sun Jul 12 21:38:43 2009 From: david at pythontoo.com (David) Date: Sun, 12 Jul 2009 21:38:43 -0400 Subject: Webcam + GStreamer In-Reply-To: <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> Message-ID: <4A5A9023.8070600@pythontoo.com> Sparky wrote: > On Jul 12, 4:30 pm, David wrote: >> Sparky wrote: >>> On Jul 12, 3:50 pm, Sparky wrote: >>>> Hello! I need to stream from a webcam in Linux and I need to be able >>>> to analyze the video feed frame by frame with PIL. Currently my web- >>>> cam (Quickcam Chat) only seems to work with GStreamer so a solution >>>> using pygst would be preferred. >>>> Thanks for your help, >>>> Sam >>> Sorry, to clarify I am just having a hard time capturing frames in a >>> way that I can access with PIL. >> Most web cams produce jpeg images so read the note at the bottom here;http://effbot.org/imagingbook/format-jpeg.htm >> What exactly are you trying to do? What have you tried so far and what >> happened may help. >> >> -- >> Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com > > Dear David, > > Thank you for your quick response. I have tried a few things. First of > all, I have tried gst-launch-0.10 v4l2src ! ffmpegcolorspace ! > pngenc ! filesink location=foo.png. Foo.png comes out sharp enough but > it takes around 2 seconds to complete. I have also tried CVTypes but > it does not run without LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so and, > when it does run, it only displays colored "snow". Here is that code: > > import pygame > import Image > from pygame.locals import * > import sys > > import opencv > #this is important for capturing/displaying images > from opencv import highgui > > camera = highgui.cvCreateCameraCapture(-1) > print "cam:" + str(camera) > def get_image(): > print "here" > im = highgui.cvQueryFrame(camera) > #convert Ipl image to PIL image > return opencv.adaptors.Ipl2PIL(im) > > fps = 30.0 > pygame.init() > window = pygame.display.set_mode((320,240)) > pygame.display.set_caption("WebCam Demo") > screen = pygame.display.get_surface() > > while True: > events = pygame.event.get() > im = get_image() > print im.mode > pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) > screen.blit(pg_img, (0,0)) > pygame.display.flip() > pygame.time.delay(int(1000 * 1.0/fps)) > > Finally, I have gotten pygst to stream video with the example at > http://pygstdocs.berlios.de/pygst-tutorial/webcam-viewer.html but of > course I do not know how to get a hold of that data. Just so you know, > I am trying a primitive type of object tracking. I would use some of > the libraries already available but the two more popular > implementations on Linux (tbeta/ccv and reacTIVision) dont seem to > work with my web cam. I have more info on those non-python attempts at > http://ubuntuforums.org/showthread.php?p=7596908. Unfortunately no one > seemed to respond to that post. > > Thanks again, > Sam See if this gets you started, I got it working here with a video4linux2 cam. http://code.google.com/p/python-video4linux2/ here is what I have, I think it is the same; http://dwabbott.com/python-video4linux2-read-only/ let me know how you make out. -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From drobinow at gmail.com Sun Jul 12 22:00:15 2009 From: drobinow at gmail.com (David Robinow) Date: Sun, 12 Jul 2009 22:00:15 -0400 Subject: Question about generators In-Reply-To: References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: <4eb0089f0907121900o3f8f3416vd519c1487465fb1a@mail.gmail.com> On Sun, Jul 12, 2009 at 9:24 PM, Cameron Pulsford wrote: > As far as the primes generator, it does not generate any non-primes. All > primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) where is x is > [1, 2, ..., n]. The only time it doesn't generate a prime is when x + (1 or > 5) % 5 == 0. Which is what that last part is making sure doesn't happen. I'm > not a mathematician or anything so correct me if I'm wrong, but that's what > I've read. All you're doing is eliminating numbers divisible by 2,3, and 5. Your generator includes 49 (7 * 7), 77 (7*11), 91 (7*13), 121 (11*11), etc. From sjmachin at lexicon.net Sun Jul 12 22:26:20 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 12 Jul 2009 19:26:20 -0700 (PDT) Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> Message-ID: <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> On Jul 13, 11:24?am, Cameron Pulsford wrote: > As far as the primes generator, it does not generate any non-primes. ? > All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) ? > where is x is [1, 2, ..., n]. The only time it doesn't generate a ? > prime is when x + (1 or 5) % 5 == 0. Which is what that last part is ? > making sure doesn't happen. I'm not a mathematician or anything so > correct me if I'm wrong, but that's what I've read. Where did you read that? Have you tried to verify it, like this: | >>> [6*i+j for i in range(1, 21) for j in (1, 5) if (i+j) % 5] | [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 77, 79, 83, 89, 91, 97, 101, 103, 107, 109, 113, 119, 121] 49, 77, 91, and 121 are not prime. From Samnsparky at gmail.com Sun Jul 12 22:54:21 2009 From: Samnsparky at gmail.com (Sparky) Date: Sun, 12 Jul 2009 19:54:21 -0700 (PDT) Subject: Webcam + GStreamer References: <403834a0-de07-47b2-8529-1a84e26c321a@x6g2000prc.googlegroups.com> <1b905fc1-5066-421e-8f5e-3162b95c1f3c@v15g2000prn.googlegroups.com> <4c9114cd-3a0a-4959-9f21-538115a3a8bc@g7g2000prg.googlegroups.com> Message-ID: <8aac1115-3dcc-4e91-94d6-ac4204879420@d9g2000prh.googlegroups.com> On Jul 12, 7:38?pm, David wrote: > Sparky wrote: > > On Jul 12, 4:30 pm, David wrote: > >> Sparky wrote: > >>> On Jul 12, 3:50 pm, Sparky wrote: > >>>> Hello! I need to stream from a webcam in Linux and I need to be able > >>>> to analyze the video feed frame by frame with PIL. Currently my web- > >>>> cam (Quickcam Chat) only seems to work with GStreamer so a solution > >>>> using pygst would be preferred. > >>>> Thanks for your help, > >>>> Sam > >>> Sorry, to clarify I am just having a hard time capturing frames in a > >>> way that I can access with PIL. > >> Most web cams produce jpeg images so read the note at the bottom here;http://effbot.org/imagingbook/format-jpeg.htm > >> What exactly are you trying to do? What have you tried so far and what > >> happened may help. > > >> -- > >> Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com > > > Dear David, > > > Thank you for your quick response. I have tried a few things. First of > > all, I have tried gst-launch-0.10 v4l2src ! ffmpegcolorspace ! > > pngenc ! filesink location=foo.png. Foo.png comes out sharp enough but > > it takes around 2 seconds to complete. I have also tried CVTypes but > > it does not run without LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so and, > > when it does run, it only displays colored "snow". Here is that code: > > > import pygame > > import Image > > from pygame.locals import * > > import sys > > > import opencv > > #this is important for capturing/displaying images > > from opencv import highgui > > > camera = highgui.cvCreateCameraCapture(-1) > > print "cam:" + str(camera) > > def get_image(): > > ? ?print "here" > > ? ?im = highgui.cvQueryFrame(camera) > > ? ?#convert Ipl image to PIL image > > ? ?return opencv.adaptors.Ipl2PIL(im) > > > fps = 30.0 > > pygame.init() > > window = pygame.display.set_mode((320,240)) > > pygame.display.set_caption("WebCam Demo") > > screen = pygame.display.get_surface() > > > while True: > > ? ?events = pygame.event.get() > > ? ?im = get_image() > > ? ?print im.mode > > ? ?pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) > > ? ?screen.blit(pg_img, (0,0)) > > ? ?pygame.display.flip() > > ? ?pygame.time.delay(int(1000 * 1.0/fps)) > > > Finally, I have gotten pygst to stream video with the example at > >http://pygstdocs.berlios.de/pygst-tutorial/webcam-viewer.htmlbut of > > course I do not know how to get a hold of that data. Just so you know, > > I am trying a primitive type of object tracking. I would use some of > > the libraries already available but the two more popular > > implementations on Linux (tbeta/ccv and reacTIVision) dont seem to > > work with my web cam. I have more info on those non-python attempts at > >http://ubuntuforums.org/showthread.php?p=7596908. Unfortunately no one > > seemed to respond to that post. > > > Thanks again, > > Sam > > See if this gets you started, I got it working here with a video4linux2 cam.http://code.google.com/p/python-video4linux2/ > here is what I have, I think it is the same;http://dwabbott.com/python-video4linux2-read-only/ > let me know how you make out. > -david > > -- > Powered by Gentoo GNU/Linuxhttp://linuxcrazy.com I gave it a shot and here is what I got: sam at sam-laptop:~/python-video4linux2-read-only$ ./pyv4l2.py Available devices: ['/dev/video0'] /dev/video0 Capabilities: Capture ReadWrite Streaming Input 0: Name: zc3xx Type: camera Standards: [] Pixel formats: JPEG JPEG Resolutions: Segmentation fault ------------------------------------- sam at sam-laptop:~/python-video4linux2-read-only$ ./streampics.py /dev/ video0 0 BGR3 640 480 testpics Trying to create directory pics Recording /dev/video0:0 with format JPEG at (640, 480) Traceback (most recent call last): File "./streampics.py", line 94, in Run() File "./streampics.py", line 78, in Run d.SetupStreaming(5, StreamCallback) File "/home/sam/python-video4linux2-read-only/pyv4l2.py", line 682, in SetupStreaming self.StreamOn() File "/home/sam/python-video4linux2-read-only/pyv4l2.py", line 636, in StreamOn lib.Error() Exception: Could not start streaming: 22: Invalid argument *** glibc detected *** python: free(): invalid next size (fast): 0x0a2aeff0 *** Any suggestions? Thanks, Sam From cameron.pulsford at gmail.com Sun Jul 12 23:17:31 2009 From: cameron.pulsford at gmail.com (Cameron Pulsford) Date: Sun, 12 Jul 2009 23:17:31 -0400 Subject: Question about generators In-Reply-To: <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> Message-ID: I read it on the haskell site in their sieves/prime wheel section, I guess I misunderstood something. (east to do over there...) I did verify it against established list of primes and other generators I've written that use more normal methods, but I only hand verified it. It is at least interesting though, I can probably extend it to check for primality by using a more normal sieve method. It might be pretty fast too because generally it does only generate primes, and the few non primes it does generate could be caught quickly using a scratching out technique. When I was initially looking at it there are some interesting patterns I might be able to extend into a generator that would yield only correct sets of numbers for the 6x + n pattern. On Jul 12, 2009, at 10:26 PM, John Machin wrote: > On Jul 13, 11:24 am, Cameron Pulsford > wrote: > >> As far as the primes generator, it does not generate any non-primes. >> All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) >> where is x is [1, 2, ..., n]. The only time it doesn't generate a >> prime is when x + (1 or 5) % 5 == 0. Which is what that last part is >> making sure doesn't happen. I'm not a mathematician or anything so >> correct me if I'm wrong, but that's what I've read. > > Where did you read that? Have you tried to verify it, like this: > > | >>> [6*i+j for i in range(1, 21) for j in (1, 5) if (i+j) % 5] > | [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, > 71, 73, 77, 79, 83, 89, 91, 97, 101, 103, 107, 109, 113, 119, 121] > > 49, 77, 91, and 121 are not prime. > > -- > http://mail.python.org/mailman/listinfo/python-list From vincent.gulinao at gmail.com Mon Jul 13 01:06:11 2009 From: vincent.gulinao at gmail.com (Vincent Gulinao) Date: Mon, 13 Jul 2009 13:06:11 +0800 Subject: Infinite loops and synchronization Message-ID: lst = list() (lst populated by async twisted deferred callbacks) while True: if len(lst) == SOME_NUMBER: return lst Q1: is this a common OK practice? I'm worried infinite loops hogs memory. Q2: operating on list from threads (mostly appends) must be safe, right (synchronization)? From sjmachin at lexicon.net Mon Jul 13 01:08:30 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 12 Jul 2009 22:08:30 -0700 (PDT) Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> Message-ID: On Jul 13, 1:17?pm, Cameron Pulsford wrote: > I read it on the haskell site in their sieves/prime wheel section, I ? > guess I misunderstood something. (east to do over there...) I did ? > verify it against established list of primes and other generators I've ? > written that use more normal methods, but I only hand verified it. "to verify" something means to ensure the truth or correctness, NOT attempt to ensure ;-) From amit.kumar.iitr at gmail.com Mon Jul 13 02:42:02 2009 From: amit.kumar.iitr at gmail.com (Amit) Date: Sun, 12 Jul 2009 23:42:02 -0700 (PDT) Subject: compilation problem of python on AIX 6.1 Message-ID: <51c556a8-7277-474d-821f-190adf155c75@f16g2000vbf.googlegroups.com> Hello I want to intsall python on my AIX 6.1 Box. I couldn't get any rpm for python 2.5.x for AIX 6.1. THen I decided to compile python 2.5.4 on my AIX box. I downloaded the python source code from www.python.org and tried to compile on my AIX both using gcc. step 1 ./configure --with-gcc configuration runs successfully step 2 make it gives me error case $MAKEFLAGS in *-s*) CC='gcc -pthread' LDSHARED='./Modules/ ld_so_aix gcc -pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g - fwrapv -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py -q build;; *) CC='gcc -pthread' LDSHARED='./Modules/ld_so_aix gcc - pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes' ./python -E ./setup.py build;; esac running build running build_ext building 'bz2' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes -I. -I/avre/Python-2.5.4/./Include -I. -IInclude - I./Include -I/avre/Python-2.5.4/Include -I/avre/Python-2.5.4 -c /avre/ Python-2.5.4/Modules/bz2module.c -o build/temp.aix-6.1-2.5/avre/ Python-2.5.4/Modules/bz2module.o building '_tkinter' extension ./Modules/ld_so_aix gcc -pthread -bI:Modules/python.exp build/ temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/_tkinter.o build/ temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/tkappinit.o -L/usr/X11/lib - ltk8.4 -ltcl8.4 -lX11 -o build/lib.aix-6.1-2.5/_tkinter.so *** WARNING: renaming "_tkinter" since importing it failed: 0509-022 Cannot load module build/lib.aix-6.1-2.5. 0509-026 System error: A file or directory in the path name does not exist. plz suggest From piet at cs.uu.nl Mon Jul 13 03:56:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 09:56:08 +0200 Subject: multiprocessing and dictionaries References: Message-ID: >>>>> Bjorn Meyer (BM) wrote: >BM> I am trying to convert a piece of code that I am using the thread module with >BM> to the multiprocessing module. >BM> The way that I have it set up is a chunk of code reads a text file and assigns >BM> a dictionary key multiple values from the text file. I am using locks to write >BM> the values to the dictionary. >BM> The way that the values are written is as follows: >BM> mydict.setdefault(key, []).append(value) >BM> The problem that I have run into is that using multiprocessing, the key gets >BM> set, but the values don't get appended. >BM> I've even tried the Manager().dict() option, but it doesn't seem to work. >BM> Is this not supported at this time or am I missing something? I think you should give more information. Try to make a *minimal* program that shows the problem and include it in your posting or supply a download link. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 04:01:53 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 10:01:53 +0200 Subject: Question about generators References: <4A59E7CB.5020908@gmail.com> <4A5A27E1.1090502@gmail.com> <7EA9A678-E06B-481D-9FAB-34957B5F2D01@gmail.com> <0e1441d0-8250-4646-828d-850c201b6725@d4g2000prc.googlegroups.com> Message-ID: >>>>> Cameron Pulsford (CP) wrote: >CP> I read it on the haskell site in their sieves/prime wheel section, >CP> I guess I misunderstood something. (east to do over there...) I did >CP> verify it against established list of primes and other generators >CP> I've written that use more normal methods, but I only hand verified >CP> it. If it is used in a sieve then the non-primes will be sieved out. >CP> It is at least interesting though, I can probably extend it to >CP> check for primality by using a more normal sieve method. It might >CP> be pretty fast too because generally it does only generate primes, >CP> and the few non primes it does generate could be caught quickly >CP> using a scratching out technique. it does only generate primes => it does generate all primes. >CP> When I was initially looking at it there are some interesting >CP> patterns I might be able to extend into a generator that would >CP> yield only correct sets of numbers for the 6x + n pattern. As I wrote in my previous reply, in your use case the non-primes are harmless. But it is useful to reflect that in the naming of your identifiers. *And please, don't top post.* -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 04:53:36 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 10:53:36 +0200 Subject: Infinite loops and synchronization References: Message-ID: >>>>> Vincent Gulinao (VG) wrote: >VG> lst = list() >VG> (lst populated by async twisted deferred callbacks) >VG> while True: >VG> if len(lst) == SOME_NUMBER: >VG> return lst >VG> Q1: is this a common OK practice? I'm worried infinite loops hogs memory. >VG> Q2: operating on list from threads (mostly appends) must be safe, >VG> right (synchronization)? I am not familiar enough with twisted, but I think the principle is independent from twisted. This loop will not hog memory but it will hog CPU time. You should use a synchronisation construct like threading.Condition or a Semaphore. Here is my suggestion with a Condition: Global somewhere: lst_cond = Condition() In your loop: lst = list() # Why not lst = []? while True: # strange while/if combo if len(lst) == SOME_NUMBER: return lst Make that: with lst_cond: while len(lst) < SOME_NUMBER: lst_cond.wait() return lst In the callback: with lst_cond: lst.append(new_value) lst_cond.notify() In case you don't have a python that supports the with statement (not even `from future') you should use: lst_cond.acquire() try: ..... finally: lst_cond.release() I think the solution with a semaphore is less elegant. global: sem = Semaphore() loop: for i in range(SOME_NUMBER): sem.acquire() return lst In callback: lst.append(new_value) sem.release() *Be careful: I haven't tested this code (not even syntax checked). So consider it pseudo code.* -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From vinay_sajip at yahoo.co.uk Mon Jul 13 05:39:40 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Mon, 13 Jul 2009 02:39:40 -0700 (PDT) Subject: the ultimate logging facility for debugging code References: Message-ID: <69032a2b-0f8e-4174-b996-6203387c4058@l5g2000vbp.googlegroups.com> On Jul 10, 10:39?am, Jean-Michel Pichavant wrote: > Greetings, > > Sorry for the dubious title :o). I was wandering if there is a standard > (or reliable) python module that implements the following feature: > > http://code.activestate.com/recipes/198078/ > ?>>>Recipe 198078: Wrapping method calls (meta-class example) > ?>>> > ?>>>A metaclass is used to wrap all (or just some) methods forlogging > purposes. The underlying mechanism can be used as well to check pre/post > conditions, attribute access,... The basic point is, that the actual > class must not be changed in any way to achive the desired effect. > > I had to adapt the code to some of my needs, but now it's almost > working, by simply defining a metaclass for any of my class, I get the > class methods calls logged with the instance that called the method, the > method name, the call line in the file, the parameter **value** ?passed > to the method and the returned value. > It just saved me from hours of ipython interactive debugging with the > old print way, and to beauty of it: I don't have to write any > "self._log('Please log me')" line. > > As it is so useful, I bet there is a module that is doing this in a more > reliable way that I did. Anyone has heard of it ? > > Jean-Michel Check out the Python Decorator Library: http://wiki.python.org/moin/PythonDecoratorLibrary and you should be able to adapt what you find there to your needs. Regards, Vinay Sajip From pdpinheiro at gmail.com Mon Jul 13 06:12:03 2009 From: pdpinheiro at gmail.com (pdpi) Date: Mon, 13 Jul 2009 03:12:03 -0700 (PDT) Subject: Infinite loops and synchronization References: Message-ID: On Jul 13, 6:06?am, Vincent Gulinao wrote: > lst = list() > > (lst populated by async twisted deferred callbacks) > > while True: > ? ? ? ? if len(lst) == SOME_NUMBER: > ? ? ? ? ? ? ? ? return lst > > Q1: is this a common OK practice? I'm worried infinite loops hogs memory. > Q2: operating on list from threads (mostly appends) must be safe, > right (synchronization)? Q1: I'll answer your question with another. What's so fundamentally different between your infinite loop and this one: while len(lst) != SOME_NUMBER: pass return lst which is not an "infinite loop"[1]. Why would yours be any worse in terms of memory than mine? Are you allocating anything that would hog memory? Of course, like Piet said, it *will* hog your CPU, so you want a time.sleep(.1) in there, at the least. Of course, the question is: why aren't you using a semaphore to let you know you can proceed, and make the provider increment the semaphore? [1] -- well, it can be, if len(lst) == SOME_NUMBER never comes about, and I'd hazard a guess that that's pretty much where your fear of memory hogging comes from: it's easy to allocate stuff and not deallocate it within a cycle, only to find the bounds on that cycle going awry. From ganesh.gbg at gmail.com Mon Jul 13 06:34:19 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Mon, 13 Jul 2009 03:34:19 -0700 (PDT) Subject: simple question about Dictionary type containing List objects Message-ID: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> Hi group, I have a dict object like emails={'mycontacts': [ 'xxx at gmail.com, 'yy at gmail.com', 'zz at gmail.com'], 'myname':['gganesh']} I need to get the lenght of the list mycontacts ,like mycontacts_numbers=3 help me to solve Thanks From contact at xavierho.com Mon Jul 13 06:36:55 2009 From: contact at xavierho.com (Xavier Ho) Date: Mon, 13 Jul 2009 18:36:55 +0800 Subject: simple question about Dictionary type containing List objects In-Reply-To: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> References: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> Message-ID: <2d56febf0907130336x6950051em7903f66b5dfc5349@mail.gmail.com> On Mon, Jul 13, 2009 at 6:34 PM, gganesh wrote: > Hi group, > I have a dict object like > emails={'mycontacts': [ 'xxx at gmail.com, 'yy at gmail.com', > 'zz at gmail.com'], 'myname':['gganesh']} > I need to get the lenght of the list mycontacts ,like > mycontacts_numbers=3 > >>> len(emails['mycontacts']) 3 HTH, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldo at geek-central.gen.new_zealand Mon Jul 13 07:22:36 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:22:36 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , Aahz wrote: > In article , > Lawrence D'Oliveiro wrote: >>In message , Aahz wrote: >>> >>> It helps to remember that names and namespaces are in many >>> ways syntactic sugar for dicts or lists. >> >>Interesting, though, that Python insists on maintaining a distinction >>between c["x"] and c.x, whereas JavaScript doesn't bother. > > Why do you say "insists"? > > class AttrDict: > def __getitem__(self, key): > return getattr(self, key) OK, let's try it: >>> c = {} >>> c["x"] = 3 >>> c.x = 4 Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'x' >>> class AttrDict: ... def __getitem__(self, key): ... return getattr(self, key) ... >>> c.x = 4 Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'x' Nope, still doesn't work... From ldo at geek-central.gen.new_zealand Mon Jul 13 07:24:03 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:24:03 +1200 Subject: Infinite loops and synchronization References: Message-ID: In message , Vincent Gulinao wrote: > Q1: is this a common OK practice? I'm worried infinite loops hogs memory. The problem is not that the loop is infinite, but that it busy-waits, hogging CPU. From eckhardt at satorlaser.com Mon Jul 13 07:25:29 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Mon, 13 Jul 2009 13:25:29 +0200 Subject: simple question about Dictionary type containing List objects References: <2c98a9f3-b84c-4d92-8458-0da061e5b26d@v37g2000prg.googlegroups.com> Message-ID: <96iri6-s13.ln1@satorlaser.homedns.org> gganesh wrote: > I have a dict object like > emails={'mycontacts': [ 'xxx at gmail.com, 'yy at gmail.com', > 'zz at gmail.com'], 'myname':['gganesh']} > I need to get the lenght of the list mycontacts ,like > mycontacts_numbers=3 mycontacts = emails['mycontacts'] mycontacts_number = len(mycontacts) A list doesn't change in behaviour when it is contained in a dictionary. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From ldo at geek-central.gen.new_zealand Mon Jul 13 07:27:33 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:27:33 +1200 Subject: MySQLdb + SSH Tunnel References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: In message , Emile van Sebille wrote: > ssh with something like... > > ssh -lroot -L3306:C:3306 B > > Watch out for other instances of mysql on A or B... You can use a non-default local port and specify that in your local connection parameters. Similarly you can tell the remote server to use a non-default port in its /etc/my.cnf. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:29:39 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:29:39 +1200 Subject: Looking for a tool to checkfor python script backward compatibility References: <4a59951a$0$10853$426a74cc@news.free.fr> Message-ID: In message <4a59951a$0$10853$426a74cc at news.free.fr>, Baptiste Lepilleur wrote: > I'm looking for a tool that could be used in a pre-commit step to check > that only features available in a "old" python version are used, say > python 2.3 for example. The only sure way would be to run the script under an appropriately old version of Python. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:35:43 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:35:43 +1200 Subject: send keys to focused window References: Message-ID: In message , Broken wrote: > I am new to Python, and I'm miserably failing to send specific keys to > (say) notepad. I don't understand why you need to automate a GUI front-end, meant for human use, to a function that can be directly performed without that front-end anyway. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:37:09 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:37:09 +1200 Subject: Check file is locked? References: <652cca82-44a3-473f-b640-c2336a9cf929@v15g2000prn.googlegroups.com> Message-ID: In message <652cca82-44a3-473f-b640- c2336a9cf929 at v15g2000prn.googlegroups.com>, Rajat wrote: > ... my whole idea is to close the wordpad / notepad application so that I > can delete the file and the directory where this file resides. Don't you think the user might have that application open for a reason? From ldo at geek-central.gen.new_zealand Mon Jul 13 07:39:34 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:39:34 +1200 Subject: A Bug By Any Other Name ... References: <4a538a71$0$30236$9b4e6d93@newsspool1.arcor-online.net> Message-ID: In message <4a538a71$0$30236$9b4e6d93 at newsspool1.arcor-online.net>, Stefan Behnel wrote: > Lawrence D'Oliveiro wrote: >> >> I wonder how many people have been tripped up by the fact that >> >> ++n >> >> and >> >> --n >> >> fail silently for numeric-valued n. > > I doubt that there are many. Plus, you misspelled them from the more > obvious > > n++ > > and > > n-- > > which *do* fail with a SyntaxError. Funny, you accuse me of "misspelling", when it's _your_ version that fails with errors! From ldo at geek-central.gen.new_zealand Mon Jul 13 07:44:32 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:44:32 +1200 Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <006da5a2$0$9711$c3e8da3@news.astraweb.com> <4a531512$0$9355$426a74cc@news.free.fr> Message-ID: In message , MRAB wrote: > I wonder whether the problem with assignment in conditionals in C is due > to the assignment operator being "=". If it was ":=", would the error > still occur? One of the original C designers, Kernighan or Ritchie, admitted that he made the assignment operator "=" instead of ":=" just to save typing one character, because he thought assignments were more common than tests for equality. From ldo at geek-central.gen.new_zealand Mon Jul 13 07:47:10 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 13 Jul 2009 23:47:10 +1200 Subject: Blocking XMPP API? References: Message-ID: In message , Gabriel Rossetti wrote: > I am looking for blocking XMPP API. I'm wanting to make a webservice > that uses XMPP, but all the XMPP libs I find are non-blocking (i.e. w/ > callbacks). So why not have the caller sleep or something until the callback is invoked? It's easier to turn an async API into a synchronous one than the other way round. From piet at cs.uu.nl Mon Jul 13 08:15:07 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 14:15:07 +0200 Subject: The meaning of "=" References: Message-ID: >>>>> Lawrence D'Oliveiro (LD) wrote: >LD> In message , Aahz wrote: >Aahz> class AttrDict: >Aahz> def __getitem__(self, key): >Aahz> return getattr(self, key) >LD> OK, let's try it: >LD> >>> c = {} >LD> >>> c["x"] = 3 >LD> >>> c.x = 4 >LD> Traceback (most recent call last): >LD> File "", line 1, in >LD> AttributeError: 'dict' object has no attribute 'x' >LD> >>> class AttrDict: >LD> ... def __getitem__(self, key): >LD> ... return getattr(self, key) >LD> ... >LD> >>> c.x = 4 >LD> Traceback (most recent call last): >LD> File "", line 1, in >LD> AttributeError: 'dict' object has no attribute 'x' >LD> Nope, still doesn't work... Of course you need c = AttrDict() And to get c.x = 4 working you also need a __setitem__. And to get c["x"] working AtrrDict should subclass dict: >>> class AttrDict(dict): but these are only minor details :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From dzizes451 at gmail.com Mon Jul 13 09:07:52 2009 From: dzizes451 at gmail.com (dzizes) Date: Mon, 13 Jul 2009 06:07:52 -0700 (PDT) Subject: how to set timeout while colling a soap method? Message-ID: <24461403.post@talk.nabble.com> Hello! I wrote some some python code for executing a soap method: import SOAPpy from SOAPpy import WSDL _server = WSDL.Proxy(some wsdl) r=_server.generuj(some parameters...) print r.encode('cp1250') It works fine. However, the execution time of this soap method might be long. Therefore, I would like to set a timeout like 1 minute, after which I would post a timeoute message. Any ideas? Greats, -- View this message in context: http://www.nabble.com/how-to-set-timeout-while-colling-a-soap-method--tp24461403p24461403.html Sent from the Python - python-list mailing list archive at Nabble.com. From denis-bz-gg at t-online.de Mon Jul 13 09:11:09 2009 From: denis-bz-gg at t-online.de (denis) Date: Mon, 13 Jul 2009 06:11:09 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> Message-ID: <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Matt, how many words are you looking for, in how long a string ? Were you able to time any( substr in long_string ) against re.compile ( "|".join( list_items )) ? (REs are my method of choice, but different inputs of course give different times -- see google regex speed site:groups.google.com / site:stackoverflow.com .) cheers -- denis From seldan24 at gmail.com Mon Jul 13 09:26:19 2009 From: seldan24 at gmail.com (seldan24) Date: Mon, 13 Jul 2009 06:26:19 -0700 (PDT) Subject: Best Way to Handle All Exceptions Message-ID: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Hello, I'm fairly new at Python so hopefully this question won't be too awful. I am writing some code that will FTP to a host, and want to catch any exception that may occur, take that and print it out (eventually put it into a log file and perform some alerting action). I've figured out two different ways to do this, and am wondering which is the best (i.e. cleanest, 'right' way to proceed). I'm also trying to understand exactly what occurs for each one. The first example: from ftplib import FTP try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except Exception, err: print err This works fine. I read through the documentation, and my understanding is that there is a built-in exceptions module in python, that is automatically available in a built-in namespace. Within that module is an 'Exception' class which would contain whatever exception is thrown. So, I'm passing that to the except, along with err to hold the value and then print it out. The second example: from ftplib import FTP import sys try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except: print sys.exc_info() Here I, for the most part, get the same thing. I'm not passing anything to except and just printing out the exception using a method defined in the sys module. So, I'm new to Python... I've made it this far and am happy, but want to make sure I'm coding correctly from the start. Which method is the better/cleaner/more standard way to continue? Thanks for any help. From dsdale24 at gmail.com Mon Jul 13 09:38:17 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Mon, 13 Jul 2009 06:38:17 -0700 (PDT) Subject: Is it possible to use keyword arguments from C? Message-ID: <94892b60-b4cb-40fc-8c9d-7cfb7d1c00ed@q35g2000vbi.googlegroups.com> I am learning about the python C-api in order to contribute a feature to numpy. I see a discussion (http://docs.python.org/extending/ extending.html#keyword-parameters-for-extension-functions) on how to create a function or method in C that accepts kwargs, but is it possible to call such a method from C using kwargs rather than positional arguments? From dsdale24 at gmail.com Mon Jul 13 09:47:41 2009 From: dsdale24 at gmail.com (Darren Dale) Date: Mon, 13 Jul 2009 06:47:41 -0700 (PDT) Subject: Is it possible to use keyword arguments from C? References: <94892b60-b4cb-40fc-8c9d-7cfb7d1c00ed@q35g2000vbi.googlegroups.com> Message-ID: <29a66e7b-1263-4c06-ab01-017b471d53d1@p36g2000vbn.googlegroups.com> On Jul 13, 9:38?am, Darren Dale wrote: > I am learning about the python C-api in order to contribute a feature > to numpy. I see a discussion (http://docs.python.org/extending/ > extending.html#keyword-parameters-for-extension-functions) on how to > create a function or method ?in C that accepts kwargs, but is it > possible to call such a method from C using kwargs rather than > positional arguments? The answer was yes, this can be done. I found a short discussion at the bottom of section 1.6: http://docs.python.org/extending/extending.html#calling-python-functions-from-c From tonylay at gmail.com Mon Jul 13 09:57:29 2009 From: tonylay at gmail.com (Tony Lay) Date: Mon, 13 Jul 2009 06:57:29 -0700 (PDT) Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory Message-ID: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> Trying to build python-2.6.2 ./configure --prefix=/usr/local --exec-prefix=/usr/local LDFLAGS="-L/ usr/local" (runs through happily, had to make some libs local) make runs most of the way until? building '_tkinter' extension gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/ meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/ include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c / tmp/meld/Python-2.6.2/Modules/_tkinter.c -o build/temp.linux-i686-2.6/ tmp/meld/Python-2.6.2/Modules/_tkinter.o gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - Wstrict-prototypes -DWITH_APPINIT=1 -I/usr/X11R6/include -I. -I/tmp/ meld/Python-2.6.2/./Include -I. -IInclude -I./Include -I/usr/local/ include -I/tmp/meld/Python-2.6.2/Include -I/tmp/meld/Python-2.6.2 -c / tmp/meld/Python-2.6.2/Modules/tkappinit.c -o build/temp.linux-i686-2.6/ tmp/meld/Python-2.6.2/Modules/tkappinit.o gcc -pthread -shared build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/ Modules/_tkinter.o build/temp.linux-i686-2.6/tmp/meld/Python-2.6.2/ Modules/tkappinit.o -L/usr/X11R6/lib64 -L/usr/X11R6/lib -L/usr/local/ lib -ltk8.5 -ltcl8.5 -lX11 -o build/lib.linux-i686-2.6/_tkinter.so *** WARNING: renaming "_tkinter" since importing it failed: libtk8.5.so: cannot open shared object file: No such file or directory Failed to find the necessary bits to build these modules: _sqlite3 bsddb185 sunaudiodev To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: _tkinter running build_scripts # cd /usr/local/lib # ls -la | grep libtk8.5.so -r-xr-xr-x 1 root root 1112606 Jul 10 13:28 libtk8.5.so Am I missing something, it?s there? Regards, -Tony From lists at cheimes.de Mon Jul 13 10:09:47 2009 From: lists at cheimes.de (Christian Heimes) Date: Mon, 13 Jul 2009 16:09:47 +0200 Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory In-Reply-To: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> References: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> Message-ID: Tony Lay wrote: > # cd /usr/local/lib > > # ls -la | grep libtk8.5.so > > -r-xr-xr-x 1 root root 1112606 Jul 10 13:28 libtk8.5.so > > Am I missing something, it?s there? Is /usr/local/lib in your library search path? It looks like it isn't. Check /etc/ld.so.conf and /etc/ld.so.conf.d/. Christian From invalid at invalid Mon Jul 13 10:16:12 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 13 Jul 2009 09:16:12 -0500 Subject: Problem with two instances of PySerial References: Message-ID: > On Thu, 9 Jul 2009 08:40:18 -0500, Shine Jose wrote: > I achieve this by creating a separate worker thread to poll > the serial port for arrival of data and then updating the > required widget in main thread of program. For polling the > serial port, I create a separate instance of pySerial object. > However, I am unable to read any data in the worker thread. > Can the reason for this be 2 instances of pySerial objects > being connected to serial port. Yes. > The reason I had this doubt was because Serial.Connect() in > the worker thread did not throw any exception & isOpen() > method returns true. Under Unix, you are allowed to open a device or file as many times as you want (there are some common cases where there is a very useful thing to do). However, for something like a serial port, there's only one copy of each received data byte. Depending on exactly how you structure your code, the incoming data may be divided up amongst multiple readers, or one particular reader may get all of it. -- Grant Edwards grante Yow! I joined scientology at at a garage sale!! visi.com From deets at nospam.web.de Mon Jul 13 10:17:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 13 Jul 2009 16:17:53 +0200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <7c0toaF25751bU1@mid.uni-berlin.de> seldan24 wrote: > Hello, > > I'm fairly new at Python so hopefully this question won't be too > awful. I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). > I've figured out two different ways to do this, and am wondering which > is the best (i.e. cleanest, 'right' way to proceed). I'm also trying > to understand exactly what occurs for each one. > > The first example: > > from ftplib import FTP > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except Exception, err: > print err > > This works fine. I read through the documentation, and my > understanding is that there is a built-in exceptions module in python, > that is automatically available in a built-in namespace. Within that > module is an 'Exception' class which would contain whatever exception > is thrown. So, I'm passing that to the except, along with err to hold > the value and then print it out. > > The second example: > > from ftplib import FTP > import sys > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except: > print sys.exc_info() > > Here I, for the most part, get the same thing. I'm not passing > anything to except and just printing out the exception using a method > defined in the sys module. > > So, I'm new to Python... I've made it this far and am happy, but want > to make sure I'm coding correctly from the start. Which method is the > better/cleaner/more standard way to continue? Thanks for any help. The latter is - unfortunately - the better. This comes from python allowing all kinds of objects being thrown as exceptions, not only those extending from a common ancestor like Exception. You seem to have a sensible take on this, but anyway I'd like to mention that using these kinds of catch-all code is rarely justified, as it imposes the danger of not handling errors at all. So make sure the program spits out a sensible error-message, and then dies. Unless it's a server-process of course. Diez From invalid at invalid Mon Jul 13 10:19:29 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 13 Jul 2009 09:19:29 -0500 Subject: need to write a assembly progrm References: Message-ID: On 2009-07-12, Rhodri James wrote: >> The poorly punctuated paragraph sounds, to me, as if they mean >> they want (Windows assumed) EXE files that can be dropped onto >> remote/laptop machines without performing a full installation >> of Python... >> >> But yes... "assembly programs" does, to me, also mean things >> containing mnemonics for machine level opcodes. > > Given that to me, "assembly programs" does not mean .EXE files > at all, not even a little bit, I'm prefering to refuse to > guess :-) Ah, but guessing what a poster meant to ask and then answering the "guessed" question is a pretty popular sport here in c.l.p. The discussions resulting from "guessed" questions are often more informative than the answer to the intended question (if the OP ever returns to clarify his intent). -- Grant Edwards grante Yow! Bo Derek ruined at my life! visi.com From tonylay at gmail.com Mon Jul 13 10:24:08 2009 From: tonylay at gmail.com (Tony Lay) Date: Mon, 13 Jul 2009 07:24:08 -0700 (PDT) Subject: python make dies :libtk8.5.so: cannot open shared object file: No such file or directory References: <5594af68-9b4b-45c7-a55d-75020d70173a@f16g2000vbf.googlegroups.com> Message-ID: On Jul 13, 10:09?am, Christian Heimes wrote: > Tony Lay wrote: > > # cd /usr/local/lib > > > # ls -la | grep libtk8.5.so > > > -r-xr-xr-x ? 1 root root ?1112606 Jul 10 13:28 libtk8.5.so > > > Am I missing something, it?s there? > > Is /usr/local/lib in your library search path? It looks like it isn't. > Check /etc/ld.so.conf and /etc/ld.so.conf.d/. > > Christian I added /usr/local/lib to ld.so.conf.d/python26.conf and ran ldconfig (thanks Chrisitan) Ran a make distclean (had to move the files and got a ?file not found? from my previous working directory). Everything compiled and installed like a charm. Thanks! From python at mrabarnett.plus.com Mon Jul 13 10:33:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 Jul 2009 15:33:42 +0100 Subject: Best Way to Handle All Exceptions In-Reply-To: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <4A5B45C6.5060404@mrabarnett.plus.com> seldan24 wrote: > Hello, > > I'm fairly new at Python so hopefully this question won't be too > awful. I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). > I've figured out two different ways to do this, and am wondering which > is the best (i.e. cleanest, 'right' way to proceed). I'm also trying > to understand exactly what occurs for each one. > > The first example: > > from ftplib import FTP > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except Exception, err: > print err > > This works fine. I read through the documentation, and my > understanding is that there is a built-in exceptions module in python, > that is automatically available in a built-in namespace. Within that > module is an 'Exception' class which would contain whatever exception > is thrown. So, I'm passing that to the except, along with err to hold > the value and then print it out. > There isn't an "exceptions module"; exceptions are part of the language. > The second example: > > from ftplib import FTP > import sys > try: > ftp = FTP(ftp_host) > ftp.login(ftp_user, ftp_pass) > except: > print sys.exc_info() > This is called a "bare exception handler". It's virtually never the right way to do it. > Here I, for the most part, get the same thing. I'm not passing > anything to except and just printing out the exception using a method > defined in the sys module. > > So, I'm new to Python... I've made it this far and am happy, but want > to make sure I'm coding correctly from the start. Which method is the > better/cleaner/more standard way to continue? Thanks for any help. You should use the most specific exception possible because at lot of things could raise an exception: >>> foo Traceback (most recent call last): File "", line 1, in foo NameError: name 'foo' is not defined >>> try: foo except Exception, e: print "*** Caught an exception ***" print e *** Caught an exception *** name 'foo' is not defined From bryanvick at gmail.com Mon Jul 13 10:52:51 2009 From: bryanvick at gmail.com (Bryan) Date: Mon, 13 Jul 2009 07:52:51 -0700 (PDT) Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: On Jul 10, 12:03?pm, mgi... at motorola.com (Gary Duzan) wrote: > In article <3af970b1-b454-4d56-a33f-889ecfaca... at l28g2000vba.googlegroups.com>, > > Bryan ? wrote: > > >rsyncExec = '/usr/bin/ssh' > >source = 'r... at 10.0.45.67:/home/bry/jquery.lookup' > >dest = '/home/bry/tmp' > >rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' > >args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] > > ? ?I think you want -e and the ssh command to be separate args. > Something like: > > rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' > args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] > > or: > > rshArgs = [ '-e', '/usr/bin/ssh -i /home/bry/keys/brybackup.key' ] > args = [rsyncExec, '-a', '-v', '--dry-run'] + ?rshArgs + [ source, dest] > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Gary Duzan > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Motorola H&NM Separating the argument parts worked. Strangely though, I don't need to do that for arguments such as --files-from='/path/to/file'. Also, in this example code I had the rsync executable path pointing to the ssh program, so no wonder I was getting the output of ssh! From seldan24 at gmail.com Mon Jul 13 11:03:27 2009 From: seldan24 at gmail.com (seldan24) Date: Mon, 13 Jul 2009 08:03:27 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: On Jul 13, 10:33?am, MRAB wrote: > seldan24 wrote: > > Hello, > > > I'm fairly new at Python so hopefully this question won't be too > > awful. ?I am writing some code that will FTP to a host, and want to > > catch any exception that may occur, take that and print it out > > (eventually put it into a log file and perform some alerting action). > > I've figured out two different ways to do this, and am wondering which > > is the best (i.e. cleanest, 'right' way to proceed). ?I'm also trying > > to understand exactly what occurs for each one. > > > The first example: > > > from ftplib import FTP > > try: > > ? ? ftp = FTP(ftp_host) > > ? ? ftp.login(ftp_user, ftp_pass) > > except Exception, err: > > ? ? print err > > > This works fine. ?I read through the documentation, and my > > understanding is that there is a built-in exceptions module in python, > > that is automatically available in a built-in namespace. ?Within that > > module is an 'Exception' class which would contain whatever exception > > is thrown. ?So, I'm passing that to the except, along with err to hold > > the value and then print it out. > > There isn't an "exceptions module"; exceptions are part of the language. > > > The second example: > > > from ftplib import FTP > > import sys > > try: > > ? ? ftp = FTP(ftp_host) > > ? ? ftp.login(ftp_user, ftp_pass) > > except: > > ? ? print sys.exc_info() > > This is called a "bare exception handler". It's virtually never the > right way to do it. > > > Here I, for the most part, get the same thing. ?I'm not passing > > anything to except and just printing out the exception using a method > > defined in the sys module. > > > So, I'm new to Python... I've made it this far and am happy, but want > > to make sure I'm coding correctly from the start. ?Which method is the > > better/cleaner/more standard way to continue? ?Thanks for any help. > > You should use the most specific exception possible because at lot of > things could raise an exception: > > ?>>> foo > > Traceback (most recent call last): > ? ?File "", line 1, in > ? ? ?foo > NameError: name 'foo' is not defined > ?>>> try: > ? ? ?foo > except Exception, e: > ? ? ?print "*** Caught an exception ***" > ? ? ?print e > > *** Caught an exception *** > name 'foo' is not defined- Hide quoted text - > > - Show quoted text - Thank you both for your input. I want to make sure I get started on the right track. For this particular script, I should have included that I would take the exception contents, and pass those to the logging module. For this particular script, all exceptions are fatal and I would want them to be. I just wanted a way to catch them and log them prior to program termination. I can understand why folks should always specify exact exceptions where possible if they plan on doing something with them, but I just want to log and terminate (and alarm) when any exception, irregardless of what it is, occurs; that's why I'm using the catch-all approach. I most likely should have put in an exit() in my snippet; sorry about that. I did notice that Python allows different objects to be thrown. This threw me off a bit because, at first, I was expecting everything to come through as nice, easy tuples. It turns out that ftplib throws some class from the sockets module (my terminology may be off here). As for terminology, sorry about the 'exceptions' misuse. The Python documentation refers to 'exceptions' as a module, albeit built-in, so I used that language accordingly (link to doc: http://docs.python.org/library/exceptions.html?highlight=exceptions#module-exceptions). Anyway, thanks again for the help and advice, it is greatly appreciated. From floris.bruynooghe at gmail.com Mon Jul 13 11:13:34 2009 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Mon, 13 Jul 2009 08:13:34 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: On Jul 13, 2:26?pm, seldan24 wrote: > The first example: > > from ftplib import FTP > try: > ? ? ftp = FTP(ftp_host) > ? ? ftp.login(ftp_user, ftp_pass) > except Exception, err: > ? ? print err *If* you really do want to catch *all* exceptions (as mentioned already it is usually better to catch specific exceptions) this is the way to do it. To know why you should look at the class hierarchy on http://docs.python.org/library/exceptions.html. The reason is that you almost never want to be catching SystemExit, KeyboardInterrupt etc. catching them will give you trouble at some point (unless you really know what you're doing but then I would suggest you list them explicitly instead of using the bare except statement). While it is true that you could raise an object that is not a subclass from Exception it is very bad practice, you should never do that. And I've haven't seen an external module in the wild that does that in years and the stdlib will always play nice. Regards Floris From hartley79 at gmail.com Mon Jul 13 11:47:03 2009 From: hartley79 at gmail.com (hartley) Date: Mon, 13 Jul 2009 08:47:03 -0700 (PDT) Subject: Passing python list from C to python Message-ID: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> I'm very new at wrapping Python/C, and I have run into some problems. I have one python module that provides me with a list (provideBuffer in provideBuff.py): Py_Initialize(); pName = PyString_FromString("provideBuff"); pModule = PyImport_Import(pName); pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); pValue = PyObject_CallObject(pFunc,NULL); pValue is now a PyList - i've even verified this with: int a = PyList_Check(pValue); printf("%d\n", a); However, I want to send this PyList to another python module, but I don't know how to do this. Initially I though I could just do like above, only swapping NULL with pValue, but that is not working. pName2 = PyString_FromString("C-embedding"); pModule2 = PyImport_Import(pName2); pFunc2 = PyObject_GetAttrString(pModule2,"buff"); pValue2 = PyObject_CallObject(pFunc2,pValue); pValue2 is now False! So i guess i cannot pass pValue as an argument to PyObject_CallObject when i want to pass an python list as an argument. But how must a go about to make this work? From degibb at gmail.com Mon Jul 13 12:21:37 2009 From: degibb at gmail.com (David Gibb) Date: Mon, 13 Jul 2009 12:21:37 -0400 Subject: list of all possible values Message-ID: Hi guys. I was thinking about a problem I had: suppose I have a list of possible values. I want to to have a list of all possible lists of length n whose values are in that original list. For example: if my values are ['a', 'b', 'c'], then all possible lists of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. I created a recursive program to do it, but I was wondering if there was a better way of doing it (possibly with list comprehensions). Here's my recursive version: vals = ['a', 'b', 'c'] def foo(length): if length <=0: return [] if length == 1: return [[x] for x in vals] else: return [x + [y] for x in foo(length - 1) for y in vals] print foo(3) Thanks, David From bjorn.m.meyer at gmail.com Mon Jul 13 12:29:56 2009 From: bjorn.m.meyer at gmail.com (Bjorn Meyer) Date: Mon, 13 Jul 2009 10:29:56 -0600 Subject: multiprocessing and dictionaries In-Reply-To: References: Message-ID: <200907131029.56346.bjorn.m.meyer@gmail.com> On Monday 13 July 2009 01:56:08 Piet van Oostrum wrote: > >>>>> Bjorn Meyer (BM) wrote: > > > >BM> I am trying to convert a piece of code that I am using the thread > > module with BM> to the multiprocessing module. > > > >BM> The way that I have it set up is a chunk of code reads a text file and > > assigns BM> a dictionary key multiple values from the text file. I am > > using locks to write BM> the values to the dictionary. > >BM> The way that the values are written is as follows: > >BM> mydict.setdefault(key, []).append(value) > > > >BM> The problem that I have run into is that using multiprocessing, the > > key gets BM> set, but the values don't get appended. > >BM> I've even tried the Manager().dict() option, but it doesn't seem to > > work. > > > >BM> Is this not supported at this time or am I missing something? > > I think you should give more information. Try to make a *minimal* program > that shows the problem and include it in your posting or supply a > download link. > -- > Piet van Oostrum > URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] > Private email: piet at vanoostrum.org Here is what I have been using as a test. This pretty much mimics what I am trying to do. I put both threading and multiprocessing in the example which shows the output that I am looking for. #!/usr/bin/env python import threading from multiprocessing import Manager, Process name = ('test1','test2','test3') data1 = ('dat1','dat2','dat3') data2 = ('datA','datB','datC') def thread_test(name,data1,data2, d): for nam in name: for num in range(0,3): d.setdefault(nam, []).append(data1[num]) d.setdefault(nam, []).append(data2[num]) print 'Thread test dict:',d def multiprocess_test(name,data1,data2, mydict): for nam in name: for num in range(0,3): mydict.setdefault(nam, []).append(data1[num]) mydict.setdefault(nam, []).append(data2[num]) print 'Multiprocess test dic:',mydict if __name__ == '__main__': mgr = Manager() md = mgr.dict() d = {} m = Process(target=multiprocess_test, args=(name,data1,data2,md)) m.start() t = threading.Thread(target=thread_test, args=(name,data1,data2,d)) t.start() m.join() t.join() print 'Thread test:',d print 'Multiprocess test:',md Thanks Bjorn From python.list at tim.thechases.com Mon Jul 13 12:33:20 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 13 Jul 2009 11:33:20 -0500 Subject: list of all possible values In-Reply-To: References: Message-ID: <4A5B61D0.5070403@tim.thechases.com> > For example: if my values are ['a', 'b', 'c'], then all possible lists > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. > > I created a recursive program to do it, but I was wondering if there > was a better way of doing it (possibly with list comprehensions). > > Here's my recursive version: > > vals = ['a', 'b', 'c'] > > def foo(length): > if length <=0: > return [] > if length == 1: > return [[x] for x in vals] > else: > return [x + [y] for x in foo(length - 1) for y in vals] Sounds like you want one of the combinitoric generators found in itertools[1] -- in this case, the itertools.product() does what you describe. According to the docs, it was added in 2.6 so if you're running an older version, you'd have to back-port it. -tkc [1] http://docs.python.org/library/itertools.html#itertools.product From sjmachin at lexicon.net Mon Jul 13 12:35:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 13 Jul 2009 09:35:16 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> Message-ID: <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> On Jul 14, 1:47?am, hartley wrote: > I'm very new at wrapping Python/C, and I have run into some problems. > > I have one python module that provides me with a list (provideBuffer > in provideBuff.py): > > ?Py_Initialize(); > ? ? ? ? pName = PyString_FromString("provideBuff"); > ? ? ? ? pModule = PyImport_Import(pName); > > ? ? ? ? pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > pValue is now a PyList - i've even verified this with: > > int a = PyList_Check(pValue); > ? ? ? ? printf("%d\n", a); > > However, I want to send this PyList to another python module, Please explain "send" ... do you mean the C equivalent of the Python statement C_embedding.buff = the_pylist ? BTW C-embedding would trigger a syntax error in Python source; best to avoid ... > but I > don't know how to do this. Initially I though I could just do like > above, only swapping NULL with pValue, but that is not working. > > pName2 = PyString_FromString("C-embedding"); > pModule2 = PyImport_Import(pName2); > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); Get?? Do you want Set? Is buff a Python function? Or is it the destination of the "sending"? Any good reason for not checking the return value for an error? [Rhetorical question; answer == "No"] > pValue2 = PyObject_CallObject(pFunc2,pValue); CallObject?? You used this before because you had a function and wanted to call it because it returned you a value .... now you want to do one of (in Python terms) value = amodule.anattr value = getattr(amodule, "anattr") or amodule.anattr = value setattr(amodule, "anattr", value) > pValue2 is now False! False?? Do you mean the C NULL? > So i guess i cannot pass pValue as an argument > to PyObject_CallObject when i want to pass an python list as an > argument. But how must a go about to make this work? It's mainly just a matter of (1) knowing what you want to do (2) picking the API that does what you want (3) checking the returned value for error after every call. HTH, John From bearophileHUGS at lycos.com Mon Jul 13 12:35:49 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Mon, 13 Jul 2009 09:35:49 -0700 (PDT) Subject: list of all possible values References: Message-ID: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> David Gibb: > For example: if my values are ['a', 'b', 'c'], then all possible lists > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. >>> from itertools import product >>> list(product("abc", repeat=2)) [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')] Bye, bearophile From whatnextur at gmail.com Mon Jul 13 12:50:57 2009 From: whatnextur at gmail.com (whatnext@gmail.com) Date: Mon, 13 Jul 2009 09:50:57 -0700 (PDT) Subject: Germany Ups Terrorism Alert Before Election Message-ID: With Germany going to the polls in a general election in three months, authorities are on high alert after detecting an increase in online warnings of terrorist attacks targeting the country. The German government held high-level talks with top security and intelligence chiefs in Berlin on Thursday to discuss the growing threat posed by Islamic extremists and to coordinate counterterrorism measures. Intelligence officials are alarmed by the rising number of videos posted online by militant Islamists who say they are specifically targeting Germany. Up to 13 videos are reported to have appeared on the Web since January, many of them referring to the deployment of German troops in Afghanistan. (See pictures of U.S. Marines opening a new offensive in Afghanistan.) for more http://www.terrorismsearch.blogspot.com/ From andreas.tawn at ubisoft.com Mon Jul 13 12:51:07 2009 From: andreas.tawn at ubisoft.com (Andreas Tawn) Date: Mon, 13 Jul 2009 18:51:07 +0200 Subject: list of all possible values In-Reply-To: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> Message-ID: <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> > David Gibb: > > For example: if my values are ['a', 'b', 'c'], then all possible lists > > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. > > >>> from itertools import product > >>> list(product("abc", repeat=2)) > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', > 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')] > > Bye, > bearophile Certainly possible with list comprehensions. >>> a = "abc" >>> [(x, y) for x in a for y in a] [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')] But I like bearophile's version better. Cheers, Drea From gabriel.rossetti at arimaz.com Mon Jul 13 13:08:12 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Mon, 13 Jul 2009 19:08:12 +0200 Subject: Blocking XMPP API? In-Reply-To: References: Message-ID: <4A5B69FC.5010109@arimaz.com> Lawrence D'Oliveiro wrote: > In message , Gabriel > Rossetti wrote: > > >> I am looking for blocking XMPP API. I'm wanting to make a webservice >> that uses XMPP, but all the XMPP libs I find are non-blocking (i.e. w/ >> callbacks). >> > > So why not have the caller sleep or something until the callback is invoked? > > It's easier to turn an async API into a synchronous one than the other way > round. > > Yes, that is what I ended up doing, thanks for the response though :-) Gabriel From skong1 at gmail.com Mon Jul 13 13:10:00 2009 From: skong1 at gmail.com (sityee kong) Date: Mon, 13 Jul 2009 10:10:00 -0700 Subject: Memory error due to big input file Message-ID: Hi All, I have a similar problem that many new python users might encounter. I would really appreciate if you could help me fix the error. I have a big text file with size more than 2GB. It turned out memory error when reading in this file. Here is my python script, the error occurred at line -- self.fh.readlines(). import math import time class textfile: def __init__(self,fname): self.name=fname self.fh=open(fname) self.fh.readline() self.lines=self.fh.readlines() a=textfile("/home/sservice/nfbc/GenoData/CompareCalls3.diff") lfile=len(a.lines) def myfun(snp,start,end): subdata=a.lines[start:end+1] NEWmiss=0 OLDmiss=0 DIFF=0 for row in subdata: k=row.split() if (k[3]=="0/0") & (k[4]!="0/0"): NEWmiss=NEWmiss+1 elif (k[3]!="0/0") & (k[4]=="0/0"): OLDmiss=OLDmiss+1 elif (k[3]!="0/0") & (k[4]!="0/0"): DIFF=DIFF+1 result.write(snp+" "+str(NEWmiss)+" "+str(OLDmiss)+" "+str(DIFF)+"\n") result=open("Summary_noLoop_diff3.txt","w") result.write("SNP NEWmiss OLDmiss DIFF\n") start=0 snp=0 for i in range(lfile): if (i==0): continue after=a.lines[i].split() before=a.lines[i-1].split() if (before[0]==after[0]): if (i!=(lfile-1)): continue else: end=lfile-1 myfun(before[0],start,end) snp=snp+1 else: end=i-1 myfun(before[0],start,end) snp=snp+1 start=i if (i ==(lfile-1)): myfun(after[0],start,start) snp=snp+1 result.close() sincerely, phoebe -------------- next part -------------- An HTML attachment was scrubbed... URL: From skip at pobox.com Mon Jul 13 13:18:25 2009 From: skip at pobox.com (skip at pobox.com) Date: Mon, 13 Jul 2009 12:18:25 -0500 Subject: Memory error due to big input file In-Reply-To: References: Message-ID: <19035.27745.515643.604146@montanaro.dyndns.org> phoebe> I have a big text file with size more than 2GB. It turned out phoebe> memory error when reading in this file. Here is my python phoebe> script, the error occurred at line -- self.fh.readlines(). phoebe> import math phoebe> import time phoebe> class textfile: phoebe> def __init__(self,fname): phoebe> self.name=fname phoebe> self.fh=open(fname) phoebe> self.fh.readline() phoebe> self.lines=self.fh.readlines() Don't do that. The problem is that you are trying to read the entire file into memory. Learn to operate a line (or a few lines) at a time. Try something like: a = open("/home/sservice/nfbc/GenoData/CompareCalls3.diff") for line in a: do your per-line work here -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ when i wake up with a heart rate below 40, i head right for the espresso machine. -- chaos @ forums.usms.org From python at mrabarnett.plus.com Mon Jul 13 13:20:43 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 13 Jul 2009 18:20:43 +0100 Subject: Memory error due to big input file In-Reply-To: References: Message-ID: <4A5B6CEB.1010507@mrabarnett.plus.com> sityee kong wrote: > Hi All, > > I have a similar problem that many new python users might encounter. I > would really appreciate if you could help me fix the error. > I have a big text file with size more than 2GB. It turned out memory > error when reading in this file. Here is my python script, the error > occurred at line -- self.fh.readlines(). > [snip code] Your 'error' is that you're running it on a computer with insufficient memory. From gabriel.rossetti at arimaz.com Mon Jul 13 13:28:27 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Mon, 13 Jul 2009 19:28:27 +0200 Subject: Threading.Condition problem In-Reply-To: References: Message-ID: <4A5B6EBB.30906@arimaz.com> Piet van Oostrum wrote: >>>>>> Gabriel Rossetti (GR) wrote: >>>>>> > > >> GR> Sorry if this appears twice, I sent it once with an attachment and it never >> GR> arrived so maybe the attachment is posing problems. I inlined the code this >> GR> time (at the bottom), thank you, >> > > >> GR> Gabriel >> > > >> GR> ########################## Original message ############################ >> > > >> GR> Hello everyone, >> > > >> GR> I wrote a small example that listens for xmpp msgs in a thread. The main >> GR> program calls a function that blocks (using Condition.wait) until a msg >> GR> has been received and then returns the msg. When a msg arrives, it is >> GR> put in a variable in the thread's object, it then calls the notify() >> GR> attr on the Condition object. For some reason, this doesn't work, the >> GR> thread gets the msg, tries to notify the Condition object, fails because >> GR> the lock has not been acquired yet and blocks. I tried ignoring the >> GR> failure, thinking that since it has not been acquired yet then when it >> GR> is, it will get the msg right away and never call Condition.wait, thus >> GR> not causing any problems, but this does not work either. Does someone >> GR> know what I am doing wrong? I attached the code to this msg. >> > > The code that puts the message in the variable should also acquire the > lock: > > > def onMessage(self, conn, msg): > with self._cv: > self.message = msg > self._cv.notify() > Thank you, that was the problem, I eventually found that > A couple of remarks: > > 1. I think the code is neater if all manipulation with the condition is > done in the same class (actually in the same instance -- making this > instance into a monitor). > The reason I didn't do that is that I don' t want the Listener to sleep, I maybe over simplified the example, I actually put them in a dictionary as they come in, so in your example, if I have several threads waiting on msgs it wouldn't work. I'm trying to make a webservice api thay will also be turned into a java .jar for people that need java. Now that I think about it, each session will have an instance of the object so msgs shouldn' t get mixed up (one connection per user), so I could block in the thread. I'll try your suggestion as I think it is cleaner. > class Listener(Thread): > def __init__(self, ws): > Thread.__init__(self) > self.interrupt = Event() > self.message = None > self._cv = Condition() > self.client = ws._client > self.client.RegisterHandler('message', self.onMessage) > > def onMessage(self, conn, msg): > with self._cv: > self.message = msg > try: > self._cv.notify() > except RuntimeError: > print "self._cv has not acquired the lock yet" > > def getMsg(self): > with self._cv: > while !self.message > self._cv.wait() > return self.message > > class WS(object): > def __init__(self, username, password, res): > self._jid = xmpp.protocol.JID(username) > self._client = xmpp.Client(self._jid.getDomain()) > # self._cv = Condition() > > def getMsg(self, mid=None): > """ > """ > return self._listener.getMsg() > > Of course I haven't tested this code as I don't have the context > modules. > > 2. I don't know if more than one message can be delivered in the same > instance. If yes, than your code will not work, and neither will the > code above as, the message instance variable is never cleared. So the > next getMsg will be happy to deliver the previous one. > You would have to clear it when returning this one. > > Like I said above, in reality I have a dict not just a simple variable. > def getMsg(self): > with self._cv: > while !self.message > self._cv.wait() > msg = self.message > self.message = None > return msg > > 3. If the messages come in faster than they can be processed some will > be lost as they will overwrite the previous one in the self.message > variable. The solution is to use a threading.Queue to transfer the > messages from one thread to the other. This also saves you the hassle > of doing your own synchronisation like above. If you are not familiar > with synchronising multithreaded applications it is very easy to make > errors and even if you are it is quite easy to do them wrong. I have > been involved in distributed programming courses at university level > and I have seen many errors in this area. > I used a dict because the API can also be setup to be async and use callbacks, so I had to be able to access the msgs directly and quickly. Gabriel From vilya.harvey at gmail.com Mon Jul 13 13:55:50 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Mon, 13 Jul 2009 18:55:50 +0100 Subject: Best Way to Handle All Exceptions In-Reply-To: References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <6aef848f0907131055p76072a24h68ef2636b9676f50@mail.gmail.com> 2009/7/13 seldan24 : > Thank you both for your input. ?I want to make sure I get started on > the right track. ?For this particular script, I should have included > that I would take the exception contents, and pass those to the > logging module. ?For this particular script, all exceptions are fatal > and I would want them to be. ?I just wanted a way to catch them and > log them prior to program termination. The logging module has a function specifically to handle this case: try: # do something except: logging.exception("Uh oh...") The exception() method will automatically add details of the exception to the log message it creates; as per the docs, you should only call it from within an exception handler. Hope that helps, Vil. From emile at fenx.com Mon Jul 13 13:58:25 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 13 Jul 2009 10:58:25 -0700 Subject: list of all possible values In-Reply-To: <4A5B61D0.5070403@tim.thechases.com> References: <4A5B61D0.5070403@tim.thechases.com> Message-ID: On 7/13/2009 9:33 AM Tim Chase said... >> For example: if my values are ['a', 'b', 'c'], then all possible lists >> of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. >> >> I created a recursive program to do it, but I was wondering if there >> was a better way of doing it (possibly with list comprehensions). >> >> Here's my recursive version: >> >> vals = ['a', 'b', 'c'] >> >> def foo(length): >> if length <=0: >> return [] >> if length == 1: >> return [[x] for x in vals] >> else: >> return [x + [y] for x in foo(length - 1) for y in vals] > > Sounds like you want one of the combinitoric generators found in > itertools[1] -- in this case, the itertools.product() does what you > describe. According to the docs, it was added in 2.6 so if you're > running an older version, you'd have to back-port it Or on systems with list comps try: >>> V='abc' >>> ['%s%s'%(ii,jj) for ii in V for jj in V] ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] >>> Emile From mantihor at gmail.com Mon Jul 13 14:08:39 2009 From: mantihor at gmail.com (Bogdan Opanchuk) Date: Mon, 13 Jul 2009 11:08:39 -0700 (PDT) Subject: How to override Marshaller/Unmarshaller in xmlrpc? Message-ID: Is there a way to override marshaller in xmlrpc.client properly? For example, I want to hide bytes -> Binary transformation inside marshaller (i.e., just let user pass a bytes value to function and marshaller dumps it in base64 autmatically). Unfortunately, I cannot just make a derived class with necessary dump_* function and pass it to client and server; marshaller initialization seems to be hidden inside xmlrpc. So, here's the question - is there a nice way to do it (because there are some dirty ways like rewriting getparser() ans so on, but I don't like the idea of copypasting code from library)? Or should I reconsider my design instead? Thanks in advance. From degibb at gmail.com Mon Jul 13 14:15:24 2009 From: degibb at gmail.com (David Gibb) Date: Mon, 13 Jul 2009 14:15:24 -0400 Subject: list of all possible values In-Reply-To: References: <4A5B61D0.5070403@tim.thechases.com> Message-ID: > Or on systems with list comps try: > >>>> V='abc' >>>> ['%s%s'%(ii,jj) for ii in V for jj in V] > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] >>>> Yeah, except that the length here is hard-coded. There's no way (as far as I can tell, at least), to make this generic with respect to list length. Thanks for the itertools suggestion, guys. I was hoping to improve my list-comprehension-fu, but that module was the next best thing. David On Mon, Jul 13, 2009 at 1:58 PM, Emile van Sebille wrote: > On 7/13/2009 9:33 AM Tim Chase said... >>> >>> For example: if my values are ['a', 'b', 'c'], then all possible lists >>> of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc. >>> >>> I created a recursive program to do it, but I was wondering if there >>> was a better way of doing it (possibly with list comprehensions). >>> >>> Here's my recursive version: >>> >>> vals = ['a', 'b', 'c'] >>> >>> def foo(length): >>> ? ?if length <=0: >>> ? ? ? ?return [] >>> ? ?if length == 1: >>> ? ? ? ?return [[x] for x in vals] >>> ? ?else: >>> ? ? ? ?return [x + [y] for x in foo(length - 1) for y in vals] >> >> Sounds like you want one of the combinitoric generators found in >> itertools[1] -- in this case, the itertools.product() does what you >> describe. ?According to the docs, it was added in 2.6 so if you're running >> an older version, you'd have to back-port it > > > Emile > > -- > http://mail.python.org/mailman/listinfo/python-list > From darkwater42 at gmail.com Mon Jul 13 14:19:59 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Mon, 13 Jul 2009 11:19:59 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? Message-ID: I couldn't find a good algorithms forum on the Internet, so I guess I'll ask this question here instead: Is it possible to efficiently maintain a binary search tree in a flat array (i.e., without using pointers), as is typically done for a binary heap? It *is* possible, of course, to keep an ordered list in a flat array, and then do a binary search on the ordered array, but then insertion and deletion are O(n), rather than O(log n). It would also clearly be possible to store a balanced binary tree in a flat array, storing the children of the node at index i at indices 2*i and 2*i + 1, just as one does for a binary heap. But if you do that, I don't know if you could then do insertions and deletions in O(log n) time. One idea that came to mind, is that maybe it is possible using a "treap", which is a combination of a binary heap and a binary search tree. Insertions and deletions in binary heaps can be done in O(log n) in a flat array, but I don't know if this is also true for a treap, since you also have the binary search tree invariants to maintain, in addition to the binary heap invariants. For all I know, this might cause rotations to no longer be O(log n). |>ouglas From piet at cs.uu.nl Mon Jul 13 14:25:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 20:25:58 +0200 Subject: Automate rsync w/ authentication References: <3af970b1-b454-4d56-a33f-889ecfacacc5@l28g2000vba.googlegroups.com> Message-ID: >>>>> Bryan (B) wrote: >B> On Jul 10, 12:03?pm, mgi... at motorola.com (Gary Duzan) wrote: >>> In article <3af970b1-b454-4d56-a33f-889ecfaca... at l28g2000vba.googlegroups.com>, >>> >>> Bryan ? wrote: >>> >>> >rsyncExec = '/usr/bin/ssh' >>> >source = 'r... at 10.0.45.67:/home/bry/jquery.lookup' >>> >dest = '/home/bry/tmp' >>> >rshArg = '-e "/usr/bin/ssh -i /home/bry/keys/brybackup.key"' >>> >args = [rsyncExec, '-a', '-v', '--dry-run', rshArg, source, dest] >>> >>> ? ?I think you want -e and the ssh command to be separate args. >>> Something like: >>> >>> rshArg = '/usr/bin/ssh -i /home/bry/keys/brybackup.key' >>> args = [rsyncExec, '-a', '-v', '--dry-run', '-e', rshArg, source, dest] >>> >>> or: >>> >>> rshArgs = [ '-e', '/usr/bin/ssh -i /home/bry/keys/brybackup.key' ] >>> args = [rsyncExec, '-a', '-v', '--dry-run'] + ?rshArgs + [ source, dest] >>> >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Gary Duzan >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Motorola H&NM >B> Separating the argument parts worked. Strangely though, I don't need >B> to do that for arguments such as --files-from='/path/to/file'. Also, >B> in this example code I had the rsync executable path pointing to the >B> ssh program, so no wonder I was getting the output of ssh! I should have seen that because I changed it in my own copy!!! --files-from='/path/to/file *is* one argument, in contrast to -e "/usr/bin/ssh -i /home/bry/keys/brybackup.key" which is two arguments. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 14:33:17 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 20:33:17 +0200 Subject: Threading.Condition problem References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Piet van Oostrum wrote: ... >GR> I wrote a small example that listens for xmpp msgs in a thread. The main >GR> program calls a function that blocks (using Condition.wait) until a msg >GR> has been received and then returns the msg. When a msg arrives, it is >GR> put in a variable in the thread's object, it then calls the notify() >GR> attr on the Condition object. For some reason, this doesn't work, the >GR> thread gets the msg, tries to notify the Condition object, fails because >GR> the lock has not been acquired yet and blocks. I tried ignoring the >GR> failure, thinking that since it has not been acquired yet then when it >GR> is, it will get the msg right away and never call Condition.wait, thus >GR> not causing any problems, but this does not work either. Does someone >GR> know what I am doing wrong? I attached the code to this msg. >>>> >>> >>> The code that puts the message in the variable should also acquire the >>> lock: >>> >>> >>> def onMessage(self, conn, msg): >>> with self._cv: >>> self.message = msg >>> self._cv.notify() >>> >GR> Thank you, that was the problem, I eventually found that >>> A couple of remarks: >>> >>> 1. I think the code is neater if all manipulation with the condition is >>> done in the same class (actually in the same instance -- making this >>> instance into a monitor). >>> >GR> The reason I didn't do that is that I don' t want the Listener to sleep, I >GR> maybe over simplified the example, I actually put them in a dictionary as >GR> they come in, so in your example, if I have several threads waiting on msgs >GR> it wouldn't work. I'm trying to make a webservice api thay will also be >GR> turned into a java .jar for people that need java. Now that I think about >GR> it, each session will have an instance of the object so msgs shouldn' t get >GR> mixed up (one connection per user), so I could block in the thread. I'll >GR> try your suggestion as I think it is cleaner. Sleeping as you call it is better than busy waiting. You must have some synchronisation to make it efficient. If you put the messages in a dictionary access to the dictionary must be protected. Having several threads waiting for the messages doesn't prevent you from using proper synchronisation. Maybe you must use notify_all instead of notify. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From http Mon Jul 13 14:55:34 2009 From: http (Paul Rubin) Date: 13 Jul 2009 11:55:34 -0700 Subject: Efficient binary search tree stored in a flat array? References: Message-ID: <7x7hycmmzd.fsf@ruckus.brouhaha.com> Douglas Alan writes: > It would also clearly be > possible to store a balanced binary tree in a flat array, storing the > children of the node at index i at indices 2*i and 2*i + 1, just as > one does for a binary heap. But if you do that, I don't know if you > could then do insertions and deletions in O(log n) time. Probably not. Maybe you could organize a 2-3 tree like that, at the expense of some space. From piet at cs.uu.nl Mon Jul 13 15:12:18 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 21:12:18 +0200 Subject: multiprocessing and dictionaries References: Message-ID: >>>>> Bjorn Meyer (BM) wrote: >BM> Here is what I have been using as a test. >BM> This pretty much mimics what I am trying to do. >BM> I put both threading and multiprocessing in the example which shows >BM> the output that I am looking for. >BM> #!/usr/bin/env python >BM> import threading >BM> from multiprocessing import Manager, Process >BM> name = ('test1','test2','test3') >BM> data1 = ('dat1','dat2','dat3') >BM> data2 = ('datA','datB','datC') [snip] >BM> def multiprocess_test(name,data1,data2, mydict): >BM> for nam in name: >BM> for num in range(0,3): >BM> mydict.setdefault(nam, []).append(data1[num]) >BM> mydict.setdefault(nam, []).append(data2[num]) >BM> print 'Multiprocess test dic:',mydict I guess what's happening is this: d.setdefault(nam, []) returns a list, initially an empty list ([]). This list gets appended to. However, this list is a local list in the multi-process_test Process, therefore the result is not reflected in the original list inside the manager. Therefore all your updates get lost. You will have to do operations directly on the dictionary itself, not on any intermediary objects. Of course with the threading the situation is different as all operations are local. This works: def multiprocess_test(name,data1,data2, mydict): print name, data1, data2 for nam in name: for num in range(0,3): mydict.setdefault(nam, []) mydict[nam] += [data1[num]] mydict[nam] += [data2[num]] print 'Multiprocess test dic:',mydict If you have more than one process operating on the dictionary simultaneously you have to beware of race conditions!! -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 13 15:31:28 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 13 Jul 2009 21:31:28 +0200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: >>>>> seldan24 (s) wrote: >s> Hello, >s> I'm fairly new at Python so hopefully this question won't be too >s> awful. I am writing some code that will FTP to a host, and want to >s> catch any exception that may occur, take that and print it out >s> (eventually put it into a log file and perform some alerting action). >s> I've figured out two different ways to do this, and am wondering which >s> is the best (i.e. cleanest, 'right' way to proceed). I'm also trying >s> to understand exactly what occurs for each one. >s> The first example: >s> from ftplib import FTP >s> try: >s> ftp = FTP(ftp_host) >s> ftp.login(ftp_user, ftp_pass) >s> except Exception, err: >s> print err I think you should restrict yourself to those exceptions that are related to ftp. Do you want to catch an exception like a misspelling in one of the variables? from ftplib import FTP, all_errors try: ftp = FTP(ftp_host) ftp.login(ftp_user, ftp_pass) except all_errors, err: print err -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Mon Jul 13 15:57:38 2009 From: aahz at pythoncraft.com (Aahz) Date: 13 Jul 2009 12:57:38 -0700 Subject: Efficient binary search tree stored in a flat array? References: Message-ID: In article , Douglas Alan wrote: > >I couldn't find a good algorithms forum on the Internet, so I guess >I'll ask this question here instead: Is it possible to efficiently >maintain a binary search tree in a flat array (i.e., without using >pointers), as is typically done for a binary heap? > >It *is* possible, of course, to keep an ordered list in a flat array, >and then do a binary search on the ordered array, but then insertion >and deletion are O(n), rather than O(log n). Still, unless your list is large (more than thousands of elements), that's the way you should go. See the bisect module. Thing is, the speed difference between C and Python means the constant for insertion and deletion is very very small relative to bytecode speed. Keep in mind that Python's object/binding model means that you're shuffling pointers in the list rather than items. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From newport at freemail.hu Mon Jul 13 16:17:31 2009 From: newport at freemail.hu (Andras Szabo) Date: Mon, 13 Jul 2009 23:17:31 +0300 Subject: Tkinter / Entry widget problem Message-ID: Hello. I searched the archives but couldn't find a solution to a problem related to the Entry widget in Tkinter. When creating a pop-up window in an app, which contains an Entry widget, I want this widget to contain some default string, to have all this default string selected (as if the user had manually selected everything), and to have the focus transferred to this widget. (The idea is then that if the window pops up, the user won't have to click or press Tab any more before being able to type what is needed in the textbox, overwriting what is written there already.) I thought this might be the way to go: entrybox=Entry(toplevel_parent_window) entrybox.insert(0,"Some default string") entrybox.select_range(0,END) entrybox.focus_set() entrybox.pack() But it doesn't seem to work - the focus is not transferred to the Entry widget, and the text does not appear to be selected (even though after this entrybox.selection_present() returns True). What am I doing wrong? andras From tjreedy at udel.edu Mon Jul 13 16:19:14 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 13 Jul 2009 16:19:14 -0400 Subject: Best Way to Handle All Exceptions In-Reply-To: <7c0toaF25751bU1@mid.uni-berlin.de> References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <7c0toaF25751bU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > The latter is - unfortunately - the better. This comes from python allowing > all kinds of objects being thrown as exceptions, not only those extending > from a common ancestor like Exception. Fixed in Python 3. exceptions, without exceptions, are instances of BaseException, either directly or as instances of subcleasses of BaseException. One of many reasons to switch when possible. tjr From darkwater42 at gmail.com Mon Jul 13 16:47:19 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Mon, 13 Jul 2009 13:47:19 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: Message-ID: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> On Jul 13, 3:57?pm, a... at pythoncraft.com (Aahz) wrote: > Still, unless your list is large (more than thousands of elements), > that's the way you should go. ?See the bisect module. ?Thing is, the > speed difference between C and Python means the constant for insertion > and deletion is very very small relative to bytecode speed. ?Keep in > mind that Python's object/binding model means that you're shuffling > pointers in the list rather than items. Thank you. My question wasn't intended to be Python specific, though. I am just curious for purely academic reasons about whether there is such an algorithm. All the sources I've skimmed only seem to the answer the question via omission. Which is kind of strange, since it seems to me like an obvious question to ask. If I find the free time, I might try to work out myself whether it can be done with a treap. |>ouglas From davea at ieee.org Mon Jul 13 17:01:17 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 13 Jul 2009 17:01:17 -0400 Subject: Memory error due to big input file In-Reply-To: References: Message-ID: <4A5BA09D.9070402@ieee.org> sityee kong wrote: > Hi All, > > I have a similar problem that many new python users might encounter. I would > really appreciate if you could help me fix the error. > I have a big text file with size more than 2GB. It turned out memory error > when reading in this file. Here is my python script, the error occurred at > line -- self.fh.readlines(). > > import math > import time > > class textfile: > def __init__(self,fname): > self.name=fname > self.fh=open(fname) > self.fh.readline() > self.lines=self.fh.readlines() > > a=textfile("/home/sservice/nfbc/GenoData/CompareCalls3.diff") > > lfile=len(a.lines) > > def myfun(snp,start,end): > subdata=a.lines[start:end+1] > NEWmiss=0 > OLDmiss=0 > DIFF=0 > for row in subdata: > k=row.split() > if (k[3]=="0/0") & (k[4]!="0/0"): > NEWmiss=NEWmiss+1 > elif (k[3]!="0/0") & (k[4]=="0/0"): > OLDmiss=OLDmiss+1 > elif (k[3]!="0/0") & (k[4]!="0/0"): > DIFF=DIFF+1 > result.write(snp+" "+str(NEWmiss)+" "+str(OLDmiss)+" "+str(DIFF)+"\n") > > result=open("Summary_noLoop_diff3.txt","w") > result.write("SNP NEWmiss OLDmiss DIFF\n") > > start=0 > snp=0 > for i in range(lfile): > if (i==0): continue > after=a.lines[i].split() > before=a.lines[i-1].split() > if (before[0]==after[0]): > if (i!=(lfile-1)): continue > else: > end=lfile-1 > myfun(before[0],start,end) > snp=snp+1 > else: > end=i-1 > myfun(before[0],start,end) > snp=snp+1 > start=i > if (i ==(lfile-1)): > myfun(after[0],start,start) > snp=snp+1 > > result.close() > > sincerely, phoebe > > Others have pointed out that you have too little memory for a 2gig data structure. If you're running on a 32bit system, chances are it won't matter how much memory you add, a process is limited to 4gb, and the OS typically takes about half of it, your code and other data takes some, and you don't have 2gig left. A 64 bit version of Python, running on a 64bit OS, might be able to "just work." Anyway, loading the whole file into a list is seldom the best answer, except for files under a meg or so. It's usually better to process the file in sequence. It looks like you're also making slices of that data, so they could potentially be pretty big as well. If you can be assured that you only need the current line and the previous two (for example), then you can use a list of just those three, and delete the oldest one, and add a new one to that list each time through the loop. Or, you can add some methods to that 'textfile' class that fetch a line by index. Brute force, you could pre-scan the file, and record all the file offsets for the lines you find, rather than storing the actual line. So you still have just as big a list, but it's a list of integers. Then when somebody calls your method, he passes an integer, and you return the particular line. A little caching for performance, and you're good to go. Anyway, if you organize it that way, you can code the rest of the module to not care whether the whole file is really in memory or not. BTW, you should derive all your classes from something. If nothing else, use object. class textfile(object): From aaron.hildebrandt at gmail.com Mon Jul 13 17:14:27 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Mon, 13 Jul 2009 14:14:27 -0700 (PDT) Subject: Pickling classes -- disappearing lists? Message-ID: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> I'm trying to pickle an instance of a class. It mostly works just fine -- I can save the pickle to a file, restore it from that file, and it's mostly okay. The problem is, some lists seem to disappear. For example (snipped and crunched from the code giving me trouble): --- class InitGame: value = False journal = [] game.InitGame() def Save(): global game import cPickle, gzip, os # Change some data game.journal.append("A value") game.value = True pickles = {'game': game} jar = gzip.open("pickefile", 'wb') cPickle.dump(pickles, jar, 2) jar.close() def Load(): global game import gzip, os, cPickle jar = gzip.open("picklefile", 'r') loaded = cPickle.load(jar) jar.close() game = loaded["game"] --- Now, if I save a pickle, then load it back in, I'll get an instance of InitGame called "game", and game.value will be true, but the list "journal" will be empty. Am I missing something obvious about the pickle spec? Am I doing something wrong? Or should I be hunting for a deeper bug in the code? I've noticed that pretty much anything that's a list isn't saving to the pickle (or loading from the pickle... it's hard to tell which). From aaron.hildebrandt at gmail.com Mon Jul 13 17:20:13 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Mon, 13 Jul 2009 14:20:13 -0700 (PDT) Subject: Memory error due to big input file References: Message-ID: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> > BTW, you should derive all your classes from something. ?If nothing > else, use object. > ? class textfile(object): Just out of curiousity... why is that? I've been coding in Python for a long time, and I never derive my base classes. What's the advantage to deriving them? From l.lensgraf at gmail.com Mon Jul 13 17:44:44 2009 From: l.lensgraf at gmail.com (DrLeif) Date: Mon, 13 Jul 2009 14:44:44 -0700 (PDT) Subject: PDF: finding a blank image Message-ID: I have about 6000 PDF files which have been produced using a scanner with more being produced each day. The PDF files contain old paper records which have been taking up space. The scanner is set to detect when there is information on the backside of the page (duplex scan). The problem of course is it's not the always reliable and we wind up with a number of PDF files containing blank pages. What I would like to do is have python detect a "blank" pages in a PDF file and remove it. Any suggestions? Thanks, DrL From clp2 at rebertia.com Mon Jul 13 17:50:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 14:50:23 -0700 Subject: Pickling classes -- disappearing lists? In-Reply-To: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> References: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> Message-ID: <50697b2c0907131450j5d3f9799l2e9c206b3c749f39@mail.gmail.com> On Mon, Jul 13, 2009 at 2:14 PM, Aaron Scott wrote: > I'm trying to pickle an instance of a class. It mostly works just fine > -- I can save the pickle to a file, restore it from that file, and > it's mostly okay. The problem is, some lists seem to disappear. For > example (snipped and crunched from the code giving me trouble): > > --- > > > class InitGame: > ? ? ? ?value = False > ? ? ? ?journal = [] > > > game.InitGame() That line doesn't make sense with the code you've given... > def Save(): > ? ? ? ?global game > ? ? ? ?import cPickle, gzip, os > > ? ? ? ?# Change some data > ? ? ? ?game.journal.append("A value") > ? ? ? ?game.value = True > > ? ? ? ?pickles = {'game': game} > ? ? ? ?jar = gzip.open("pickefile", 'wb') > ? ? ? ?cPickle.dump(pickles, jar, 2) > ? ? ? ?jar.close() > > > def Load(): > ? ? ? ?global game > ? ? ? ?import gzip, os, cPickle > ? ? ? ?jar = gzip.open("picklefile", 'r') > ? ? ? ?loaded = cPickle.load(jar) > ? ? ? ?jar.close() > ? ? ? ?game = loaded["game"] > > > --- > > Now, if I save a pickle, then load it back in, I'll get an instance of > InitGame called "game", and game.value will be true, but the list > "journal" will be empty. > > Am I missing something obvious about the pickle spec? Am I doing > something wrong? Or should I be hunting for a deeper bug in the code? Your class definition isn't right. It makes 'value' and 'journal' class variables (Java lingo: "static variables") as opposed to the instance variables they should be. Here's a corrected version: class InitGame(object): def __init__(self): #instance variables are created through self.foo assignments in __init__ self.value = False self.journal = [] Cheers, Chris -- http://blog.rebertia.com From vilya.harvey at gmail.com Mon Jul 13 17:51:01 2009 From: vilya.harvey at gmail.com (Vilya Harvey) Date: Mon, 13 Jul 2009 22:51:01 +0100 Subject: Memory error due to big input file In-Reply-To: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> References: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> Message-ID: <6aef848f0907131451h2ae1b83cs1eda1651e6db13@mail.gmail.com> 2009/7/13 Aaron Scott : >> BTW, you should derive all your classes from something. ?If nothing >> else, use object. >> ? class textfile(object): > > Just out of curiousity... why is that? I've been coding in Python for > a long time, and I never derive my base classes. What's the advantage > to deriving them? class Foo: uses the old object model. class Foo(object): uses the new object model. See http://docs.python.org/reference/datamodel.html (specifically section 3.3) for details of the differences. Vil. From clp2 at rebertia.com Mon Jul 13 18:02:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 15:02:37 -0700 Subject: Memory error due to big input file In-Reply-To: <6aef848f0907131451h2ae1b83cs1eda1651e6db13@mail.gmail.com> References: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> <6aef848f0907131451h2ae1b83cs1eda1651e6db13@mail.gmail.com> Message-ID: <50697b2c0907131502p2e262a22p4a6af5a31bfee677@mail.gmail.com> On Mon, Jul 13, 2009 at 2:51 PM, Vilya Harvey wrote: > 2009/7/13 Aaron Scott : >>> BTW, you should derive all your classes from something. ?If nothing >>> else, use object. >>> ? class textfile(object): >> >> Just out of curiousity... why is that? I've been coding in Python for >> a long time, and I never derive my base classes. What's the advantage >> to deriving them? > > ? ?class Foo: > > uses the old object model. > > ? ?class Foo(object): > > uses the new object model. > > See http://docs.python.org/reference/datamodel.html (specifically > section 3.3) for details of the differences. Note that Python 3.0 makes explicitly subclassing `object` unnecessary since it removes old-style classes; a class that doesn't explicitly subclass anything will implicitly subclass `object`. Cheers, Chris -- http://blog.rebertia.com From aaron.hildebrandt at gmail.com Mon Jul 13 19:00:18 2009 From: aaron.hildebrandt at gmail.com (Aaron Scott) Date: Mon, 13 Jul 2009 16:00:18 -0700 (PDT) Subject: Pickling classes -- disappearing lists? References: <2a0cb6a2-558c-4ec4-9f88-21768682c902@n30g2000vba.googlegroups.com> Message-ID: <60e9b0e3-25c7-4d01-a120-b15b33a06b57@m18g2000vbi.googlegroups.com> > Your class definition isn't right. It makes 'value' and 'journal' > class variables (Java lingo: "static variables") as opposed to the > instance variables they should be. Here's a corrected version: > Woah, thanks. I can't believe I made such a stupid mistake. It's not like I'm a newcomer to Python, either. I'm can't believe I never noticed what I was doing. No more 2am coding for me. Thanks, Aaron From db3l.net at gmail.com Mon Jul 13 19:05:05 2009 From: db3l.net at gmail.com (David Bolen) Date: Mon, 13 Jul 2009 19:05:05 -0400 Subject: PDF: finding a blank image References: Message-ID: DrLeif writes: > What I would like to do is have python detect a "blank" pages in a PDF > file and remove it. Any suggestions? The odds are good that even a blank page is being "rendered" within the PDF as having some small bits of data due to scanner resolution, imperfections on the page, etc.. So I suspect you won't be able to just look for a well-defined pattern in the resulting PDF or anything. Unless you're using OCR, the odds are good that the scanner is rendering the PDF as an embedded image. What I'd probably do is extract the image of the page, and then use image processing on it to try to identify blank pages. I haven't had the need to do this myself, and tool availability would depend on platform, but for example, I'd probably try ImageMagick's convert operation to turn the PDF into images (like PNGs). I think Gimp can also do a similar conversion, but you'd probably have to script it yourself. Once you have an image of a page, you could then use something like OpenCV to process the page (perhaps a morphology operation to remove small noise areas, then a threshold or non-zero counter to judge "blankness"), or probably just something like PIL depending on complexity of the processing needed. Once you identify a blank page, removing it could either be with pure Python (there have been other posts recently about PDF libraries) or with external tools (such as pdftk under Linux for example). -- David From Scott.Daniels at Acm.Org Mon Jul 13 19:22:23 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 13 Jul 2009 16:22:23 -0700 Subject: PDF: finding a blank image In-Reply-To: References: Message-ID: DrLeif wrote: > I have about 6000 PDF files which have been produced using a scanner > with more being produced each day. The PDF files contain old paper > records which have been taking up space. The scanner is set to > detect when there is information on the backside of the page (duplex > scan). The problem of course is it's not the always reliable and we > wind up with a number of PDF files containing blank pages. > > What I would like to do is have python detect a "blank" pages in a PDF > file and remove it. Any suggestions? I'd check into ReportLab's commercial product, it may well be easily capable of that. If no success, you might contact PJ at Groklaw, she has dealt with a _lot_ of PDFs (and knows people who deal with PDFs in bulk). --Scott David Daniels Scott.Daniels at Acm.Org From Brian.Mingus at colorado.edu Mon Jul 13 19:26:26 2009 From: Brian.Mingus at colorado.edu (Brian) Date: Mon, 13 Jul 2009 17:26:26 -0600 Subject: PDF: finding a blank image In-Reply-To: References: Message-ID: <9839a05c0907131626w77489e62raf1582a9ff429a8f@mail.gmail.com> Perhaps your blank pages have a characteristic size. Or perhaps if you trim them with `convert' (ImageMagick) there is nothing left. On Mon, Jul 13, 2009 at 3:44 PM, DrLeif wrote: > I have about 6000 PDF files which have been produced using a scanner > with more being produced each day. The PDF files contain old paper > records which have been taking up space. The scanner is set to > detect when there is information on the backside of the page (duplex > scan). The problem of course is it's not the always reliable and we > wind up with a number of PDF files containing blank pages. > > What I would like to do is have python detect a "blank" pages in a PDF > file and remove it. Any suggestions? > > > Thanks, > DrL > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bjorn.m.meyer at gmail.com Mon Jul 13 19:35:00 2009 From: bjorn.m.meyer at gmail.com (Bjorn Meyer) Date: Mon, 13 Jul 2009 17:35:00 -0600 Subject: multiprocessing and dictionaries In-Reply-To: References: Message-ID: <200907131735.00191.bjorn.m.meyer@gmail.com> On Monday 13 July 2009 13:12:18 Piet van Oostrum wrote: > >>>>> Bjorn Meyer (BM) wrote: > > > >BM> Here is what I have been using as a test. > >BM> This pretty much mimics what I am trying to do. > >BM> I put both threading and multiprocessing in the example which shows > >BM> the output that I am looking for. > > > >BM> #!/usr/bin/env python > > > >BM> import threading > >BM> from multiprocessing import Manager, Process > > > >BM> name = ('test1','test2','test3') > >BM> data1 = ('dat1','dat2','dat3') > >BM> data2 = ('datA','datB','datC') > > [snip] > > >BM> def multiprocess_test(name,data1,data2, mydict): > >BM> for nam in name: > >BM> for num in range(0,3): > >BM> mydict.setdefault(nam, []).append(data1[num]) > >BM> mydict.setdefault(nam, []).append(data2[num]) > >BM> print 'Multiprocess test dic:',mydict > > I guess what's happening is this: > > d.setdefault(nam, []) returns a list, initially an empty list ([]). This > list gets appended to. However, this list is a local list in the > multi-process_test Process, therefore the result is not reflected in the > original list inside the manager. Therefore all your updates get lost. > You will have to do operations directly on the dictionary itself, not on > any intermediary objects. Of course with the threading the situation is > different as all operations are local. > > This works: > > def multiprocess_test(name,data1,data2, mydict): > print name, data1, data2 > for nam in name: > for num in range(0,3): > mydict.setdefault(nam, []) > mydict[nam] += [data1[num]] > mydict[nam] += [data2[num]] > print 'Multiprocess test dic:',mydict > > If you have more than one process operating on the dictionary > simultaneously you have to beware of race conditions!! > -- > Piet van Oostrum > URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] > Private email: piet at vanoostrum.org Excellent. That works perfectly. Thank you for your response Piet. Bjorn From ben+python at benfinney.id.au Mon Jul 13 20:09:06 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 14 Jul 2009 10:09:06 +1000 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <87eiskktwd.fsf@benfinney.id.au> seldan24 writes: > I'm fairly new at Python so hopefully this question won't be too > awful. I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). You have been given a lot of advice so far about catching exceptions with the ?try ? except? syntax. But reading your request, I think perhaps you don't want to catch exceptions at all. Instead of catching *all* exceptions at a specific point in your code, and then having nothing to do but end the program (possibly after some pre-exit action like logging), I think you would be better served by installing a custom top-level exception handler. When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function to sys.excepthook. With a custom top-level exception handler in place, you should then restrict your use of ?try: ? except FooException: ?? to be as precise as possible, so that the ?try? block is as small as possible and the ?FooException? is as specific as possible. Any other exceptions that you don't specifically catch will then go through to your default handle-it-just-before-we-exit ?sys.excepthook? exception handler. -- \ ?Nature hath given men one tongue but two ears, that we may | `\ hear from others twice as much as we speak.? ?Epictetus, | _o__) _Fragments_ | Ben Finney From pavlovevidence at gmail.com Mon Jul 13 20:49:23 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 13 Jul 2009 17:49:23 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> On Jul 13, 12:31?pm, Piet van Oostrum wrote: > >>>>> seldan24 (s) wrote: > >s> Hello, > >s> I'm fairly new at Python so hopefully this question won't be too > >s> awful. ?I am writing some code that will FTP to a host, and want to > >s> catch any exception that may occur, take that and print it out > >s> (eventually put it into a log file and perform some alerting action). > >s> I've figured out two different ways to do this, and am wondering which > >s> is the best (i.e. cleanest, 'right' way to proceed). ?I'm also trying > >s> to understand exactly what occurs for each one. > >s> The first example: > >s> from ftplib import FTP > >s> try: > >s> ? ? ftp = FTP(ftp_host) > >s> ? ? ftp.login(ftp_user, ftp_pass) > >s> except Exception, err: > >s> ? ? print err > > I think you should restrict yourself to those exceptions that are > related to ftp. Do you want to catch an exception like a misspelling in > one of the variables? He quite reasonably could want that, such as if the program is designed to be run from a cron job, or in some other non-interactive way. Just because something is usually a bad idea doesn't mean it always is. Carl Banks From albert at spenarnc.xs4all.nl Mon Jul 13 22:28:06 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 14 Jul 2009 02:28:06 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: In article <7x8wj4uxnb.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: > >Anyway, Python's overloading of bool(...) is yet another misfeature >and although it's convenient, the "explicit is better than implicit" >principle indicates to avoid that sort of trick. Well, trading "tricks" (I would call it idiom) for explicitness, made some people (you know ..) hate Pascal. Einstein introduced the summation convention for indices that are used twice. Leaving out summation signs is absolutely hideous, but it has saved generations of physicists of loosing track (and their minds.) Also ... For me there is nothing more clear than a regular expression search that returns None if nothing matches. Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From albert at spenarnc.xs4all.nl Mon Jul 13 22:46:19 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 14 Jul 2009 02:46:19 GMT Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <7xy6r34f3j.fsf@ruckus.brouhaha.com> <0260bf8d$0$20657$c3e8da3@news.astraweb.com> <7x8wj2agma.fsf@ruckus.brouhaha.com> Message-ID: In article <7x8wj2agma.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: >Steven D'Aprano writes: >> > but I don't accept that "somethingness" >> > vs. "nothingness" is the same distinction as truth vs falsehood. >> >> It's the distinction used by Python since the dawn of time. Python only >> grew a bool type a few versions back. > >That's true, part of the situation we have now is an artifact of that >history. > >> I'm not talking about the constants True and False (nouns), but about >> true and false values (adjectives). > >But, it seems to me, the constants True and False are the only values >to which the adjectives "true" and "false" should be applicable to. > >> >> > The idea that the "if" >> > statement selects between "somethingness" and "nothingness" rather than >> > between True and False is a bogus re-imagining of the traditional >> > function of an "if" statement >> >> There's nothing bogus about it. >> >> > and has been an endless source of bugs in Python code. >> I wonder why these "endless" bugs weren't important enough to be >> mentioned in the rationale to PEP 285: > >Because adding the bool type doesn't really fix those bugs. > >> describing `if x` as the "correct form" and calling scrapping such a >> feature as "crippling the language". > >Certainly, changing "if" would have broken an immense amount of code >and been a completely unworkable approach. We are using a fairly >mature language by now; it has a culture and history that carries >certain baggage, as one should expect. > >> > Look how much confusion it causes here in the newsgroup all the time. >> The only confusion is that you're causing me. Would you care to link to >> some? > >This current discussion (about bools) came from such confusion just a >few posts up in this very thread: > > From: upwestdon > Date: Fri, 3 Jul 2009 23:03:39 -0700 (PDT) > How about just: > > if not (self.higher and self.lower): > return self.higher or self.lower > >That test was designed to treat None as a boolean False, without >noticing that numeric 0 is also treated as False and could make the >test do the wrong thing. This is an extremely common type of error. > >> > could see some value to having a generic "is_empty" predicate >> We have that already. It's spelled __bool__ or __nonzero__ > >That's fine, but under the "explicit is better than implicit" >principle, it's preferable to call that predicate explicitly: >"if bool(x): ..." rather than "if x:". Also, after many years of Maybe I'm missing something here, but if self.higher contains 0, wouldn't bool(self.higher) evaluate to False? So how does this help? Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:25:52 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:25:52 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> Message-ID: On Mon, 13 Jul 2009 17:49:23 -0700, Carl Banks wrote: > On Jul 13, 12:31?pm, Piet van Oostrum wrote: >> >>>>> seldan24 (s) wrote: >> >s> Hello, >> >s> I'm fairly new at Python so hopefully this question won't be too s> >> >awful. ?I am writing some code that will FTP to a host, and want to s> >> >catch any exception that may occur, take that and print it out s> >> >(eventually put it into a log file and perform some alerting action). >> >s> I've figured out two different ways to do this, and am wondering >> >which s> is the best (i.e. cleanest, 'right' way to proceed). ?I'm >> >also trying s> to understand exactly what occurs for each one. s> The >> >first example: >> >s> from ftplib import FTP >> >s> try: >> >s> ? ? ftp = FTP(ftp_host) >> >s> ? ? ftp.login(ftp_user, ftp_pass) s> except Exception, err: >> >s> ? ? print err >> >> I think you should restrict yourself to those exceptions that are >> related to ftp. Do you want to catch an exception like a misspelling in >> one of the variables? > > He quite reasonably could want that, such as if the program is designed > to be run from a cron job, or in some other non-interactive way. Why is it okay for non-interactive programs to silently have incorrect behaviour? What's the point of a cron job that doesn't do what it is supposed to, because it has a bug which is silently swallowed? "I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn't crash is a horrible nightmare." http://www.pphsg.org/cdsmith/types.html -- Steven From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:27:30 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:27:30 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <87eiskktwd.fsf@benfinney.id.au> Message-ID: On Tue, 14 Jul 2009 10:09:06 +1000, Ben Finney wrote: > Instead of catching *all* exceptions at a specific point in your code, > and then having nothing to do but end the program (possibly after some > pre-exit action like logging), I think you would be better served by > installing a custom top-level exception handler. ... > Any other exceptions that you don't specifically catch will then go > through to your default handle-it-just-before-we-exit ?sys.excepthook? > exception handler. Isn't that risky though? Won't that potentially change the exception- handling behaviour of functions and classes he imports from other modules? -- Steven From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:45:02 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:45:02 GMT Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: On Mon, 13 Jul 2009 23:22:36 +1200, Lawrence D'Oliveiro wrote: [stupidity omitted] > Nope, still doesn't work... Are we supposed to interpret that post as Dumb Insolence or just Dumb? -- Steven From steven at REMOVE.THIS.cybersource.com.au Mon Jul 13 23:49:30 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 03:49:30 GMT Subject: Memory error due to big input file References: <4f490b14-86b5-45e6-9b27-4d4f82c669bb@r25g2000vbn.googlegroups.com> Message-ID: On Mon, 13 Jul 2009 14:20:13 -0700, Aaron Scott wrote: >> BTW, you should derive all your classes from something. ?If nothing >> else, use object. >> ? class textfile(object): > > Just out of curiousity... why is that? I've been coding in Python for a > long time, and I never derive my base classes. What's the advantage to > deriving them? "Old style" classes (those whose base classes aren't derived from anything) have a few disadvantages: (1) Properties don't work correctly: >>> class Parrot: # old-style class ... def __init__(self): ... self._x = 3 ... def _setter(self, value): ... self._x = value ... def _getter(self): ... print "Processing ..." ... return self._x + 1 ... x = property(_getter, _setter) ... >>> p = Parrot() >>> p.x Processing ... 4 >>> p.x = 2 >>> p.x 2 In general, anything that uses the descriptor protocol, not just property, will fail to work correctly with old-style classes. (2) Classes using multiple inheritance with diamond-shaped inheritance will be broken. (3) __slots__ is just an attribute. (4) super() doesn't work. And, depending on whether you consider this a disadvantage or an advantage: (5) Special methods like __len__ can be over-ridden on the instance, not just the class: >>> class K: ... def __len__(self): ... return 0 ... >>> k = K() >>> len(k) 0 >>> k.__len__ = lambda : 42 >>> len(k) 42 In their favour: (1) Less typing. (2) If you're not using descriptors, including property(), or multiple inheritance with diamond diagrams, they work fine. (3) They're (apparently) a tiny bit faster. -- Steven From Lily.Gao at autodesk.com Tue Jul 14 00:30:03 2009 From: Lily.Gao at autodesk.com (Lily Gao) Date: Mon, 13 Jul 2009 21:30:03 -0700 Subject: How to unbuffer Python's output Message-ID: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> Hi, All I am calling a python program in perl and use redirection, Like : `python x.py > 1.log 2>&1` When x.py crash, I get nothing from 1.log, and if I don?t use redirection, I can get useful log from the screen. How can I do to make x.py ?s output un-buffered when redirection log to files ,just exactly same with print to the screen? When I use perl?s $|=1 to unbuffer output, it take no effect. So I think it may be caused by python. Thanks! Thanks, Lily Gao(??) ACRD PSEB Catapult TD +86-21-38664379 -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Tue Jul 14 01:06:04 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 14 Jul 2009 02:06:04 -0300 Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: En Mon, 13 Jul 2009 10:11:09 -0300, denis escribi?: > Matt, how many words are you looking for, in how long a string ? > Were you able to time any( substr in long_string ) against re.compile > ( "|".join( list_items )) ? There is a known algorithm to solve specifically this problem (Aho-Corasick), a good implementation should perform better than R.E. (and better than the gen.expr. with the advantage of returning WHICH string matched) There is a C extension somewhere implementing Aho-Corasick. -- Gabriel Genellina From clp2 at rebertia.com Tue Jul 14 01:32:55 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 22:32:55 -0700 Subject: explode() In-Reply-To: References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: <50697b2c0907132232g40dce2c3o776a2f07ef28acf0@mail.gmail.com> On Mon, Jul 13, 2009 at 11:28 PM, Fred Atkinson wrote: > On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert > wrote: > >>On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >>> ? ? ? ?What is the Python equivalent of the PHP explode() function? >> >>some_string.split("separator") >> >>Cheers, >>Chris > > ? ? What would the equivalent of the PHP function implode be? "separator".join(a_list_of_strings) I would recommend you read the Python manual's section on string methods: http://docs.python.org/library/stdtypes.html#string-methods Cheers, Chris -- http://blog.rebertia.com From usernet at ilthio.net Tue Jul 14 01:41:40 2009 From: usernet at ilthio.net (Tim Harig) Date: Tue, 14 Jul 2009 05:41:40 GMT Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: On 2009-07-14, Fred Atkinson wrote: > The one thing I really dislike about Python over PHP is that > Python can usually only appear in the cgi directory (unless other > arragements are made with your hosting provider or if you reconfigure > Apache on your own server if you have your own). With PHP, I can put > them in any folder on my Web site without any problem. That is a server configuration and has nothing to do with Python directly. From sanu1267 at gmail.com Tue Jul 14 01:41:52 2009 From: sanu1267 at gmail.com (sanju ps) Date: Tue, 14 Jul 2009 11:11:52 +0530 Subject: python _binary_ code Message-ID: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> Hi Can anyone give me solution to create a python binary file (bytecode) other than pyc file .So my source code be secure.. I am working on ubuntu 9.04 with python2.6.. I Regards Sanju -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Tue Jul 14 02:00:00 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 13 Jul 2009 23:00:00 -0700 Subject: python _binary_ code In-Reply-To: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> References: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> Message-ID: <50697b2c0907132300xd593feepbaf5a2be078c93f4@mail.gmail.com> On Mon, Jul 13, 2009 at 10:41 PM, sanju ps wrote: > Hi > > ?Can anyone give me solution to create a python binary file (bytecode) other > than pyc file .So my source code be secure.. I am working on ubuntu 9.04 > with python2.6.. I PyInstaller (http://www.pyinstaller.org/) will generate a self-contained executable, but it won't hide the bytecode (which can be decompiled w/ decompyle). It's generally accepted to be impossible (or at least more trouble than it's worth) to protect Python source. See http://stackoverflow.com/questions/261638/how-do-i-protect-python-code for excellent and more detailed answers. Cheers, Chris -- http://blog.rebertia.com From fatkinson at mishmash.com Tue Jul 14 02:28:32 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Mon, 13 Jul 2009 23:28:32 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert wrote: >On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >> ? ? ? ?What is the Python equivalent of the PHP explode() function? > >some_string.split("separator") > >Cheers, >Chris What would the equivalent of the PHP function implode be? Thanks, Fred From fatkinson at mishmash.com Tue Jul 14 02:36:26 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Mon, 13 Jul 2009 23:36:26 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> Message-ID: <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> On Mon, 13 Jul 2009 22:32:55 -0700, Chris Rebert wrote: >On Mon, Jul 13, 2009 at 11:28 PM, Fred Atkinson wrote: >> On Sat, 11 Jul 2009 18:50:28 -0700, Chris Rebert >> wrote: >> >>>On Sat, Jul 11, 2009 at 7:41 PM, Fred Atkinson wrote: >>>> ? ? ? ?What is the Python equivalent of the PHP explode() function? >>> >>>some_string.split("separator") >>> >>>Cheers, >>>Chris >> >> ? ? What would the equivalent of the PHP function implode be? > >"separator".join(a_list_of_strings) > >I would recommend you read the Python manual's section on string methods: >http://docs.python.org/library/stdtypes.html#string-methods > >Cheers, >Chris Chris, You always seem to be waiting when I post a question. Do you ever sleep? I wish the Python site was as well written as the PHP site. On the PHP site, I can look up a command and they show not only the docs on that command but a list of all other commands associated with it. Thanks. Python is a whole new animal to me. I'm taking a course in PHP and Python online right now. I've done PHP scripting before though I don't claim to be a whiz on it. But I'd barely heard of Python before I took this course. The one thing I really dislike about Python over PHP is that Python can usually only appear in the cgi directory (unless other arragements are made with your hosting provider or if you reconfigure Apache on your own server if you have your own). With PHP, I can put them in any folder on my Web site without any problem. Regards, Fred From lists at cheimes.de Tue Jul 14 02:50:16 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 14 Jul 2009 08:50:16 +0200 Subject: python _binary_ code In-Reply-To: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> References: <2b76be80907132241q170bb040j827f8c75155188b1@mail.gmail.com> Message-ID: sanju ps schrieb: > Hi > > Can anyone give me solution to create a python binary file (bytecode) other > than pyc file .So my source code be secure.. I am working on ubuntu 9.04 > with python2.6.. I It's impossible to secure your code if it runs on an untrusted computer. This is true for all programming languages but it's even easier to decompile code of byte interpreted languages like Python, Java or .NET. Microsoft has tried to protect its programs, the film industry has tried it with the movies and RIAA, too. All have failed. You won't have more luck, though. :) Christian From wuwei23 at gmail.com Tue Jul 14 02:54:11 2009 From: wuwei23 at gmail.com (alex23) Date: Mon, 13 Jul 2009 23:54:11 -0700 (PDT) Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: Fred Atkinson wrote: > ? ? ? ? I wish the Python site was as well written as the PHP site. On > the PHP site, I can look up a command and they show not only the docs > on that command but a list of all other commands associated with it. ? Hey Fred, My problem is the complete opposite, I wish I could as easily interrogate objects in PHP about their behaviour as I can Python :) In case you're not familiar with this, you can see what methods are attached to an object using the intepreter via dir() and see the associated docstring for them via help(). Try looking at 'dir(str)' for a list of methods that strings recognise, then 'help(str.join)' to see the help on join. This is such a handy ability to have that I miss this casual introspection in everything else in which I'm required to develop. Hope this helps. From amrita at iisermohali.ac.in Tue Jul 14 02:57:29 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Tue, 14 Jul 2009 12:27:29 +0530 (IST) Subject: copy a file Message-ID: <14186.210.212.36.65.1247554649.squirrel@www.iisermohali.ac.in> Dear all, Can anyone tell me that suppose i want to copy few lines from one text file to another then how can i do that.Looking forward for soon reply. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From ben+python at benfinney.id.au Tue Jul 14 02:59:07 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 14 Jul 2009 16:59:07 +1000 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <87eiskktwd.fsf@benfinney.id.au> Message-ID: <873a8zlphg.fsf@benfinney.id.au> Steven D'Aprano writes: > Isn't that risky though? Won't that potentially change the exception- > handling behaviour of functions and classes he imports from other > modules? No, any existing ?except? clause will be unaffected by re-binding ?sys.excepthook?. As I understand the documentation, that function is called only if the exception is uncaught by anything else. >>> 1 / 0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: integer division or modulo by zero >>> import sys >>> def reverse_handler(exc_type, exc_instance, exc_traceback): ... print exc_type.__name__[::-1], str(exc_instance)[::-1] ... >>> sys.excepthook = reverse_handler >>> try: ... 1 / 0 ... except ZeroDivisionError: ... print "You created a black hole." ... You created a black hole. >>> 1 / 0 rorrEnoisiviDoreZ orez yb oludom ro noisivid regetni In other words, it is the function that (unless re-bound) prints the traceback we're all familiar with when an exception is uncaught. It is exposed via the name ?sys.excepthook? precisely for the purpose of changing the default handler for uncaught exceptions -- \ ?I have yet to see any problem, however complicated, which, | `\ when you looked at it in the right way, did not become still | _o__) more complicated.? ?Paul Anderson | Ben Finney From hackingkk at gmail.com Tue Jul 14 03:07:13 2009 From: hackingkk at gmail.com (Krishnakant) Date: Tue, 14 Jul 2009 12:37:13 +0530 Subject: copy a file In-Reply-To: <14186.210.212.36.65.1247554649.squirrel@www.iisermohali.ac.in> References: <14186.210.212.36.65.1247554649.squirrel@www.iisermohali.ac.in> Message-ID: <1247555233.3329.18.camel@krishna-laptop> On Tue, 2009-07-14 at 12:27 +0530, amrita at iisermohali.ac.in wrote: > Dear all, > > Can anyone tell me that suppose i want to copy few lines from one text > file to another then how can i do that.Looking forward for soon reply. > very simple. open one file and open the source file. seek till to the point where u want to start copying and loop till the end with readline function. then write it to the destination file. To find the starting point, keep looping through the content and come out of that loop when you find a match to the word you are looking for. happy hacking. Krishnakant. From pavlovevidence at gmail.com Tue Jul 14 04:30:48 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 01:30:48 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> Message-ID: <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> On Jul 13, 8:25?pm, Steven D'Aprano wrote: > On Mon, 13 Jul 2009 17:49:23 -0700, Carl Banks wrote: > > On Jul 13, 12:31?pm, Piet van Oostrum wrote: > >> >>>>> seldan24 (s) wrote: > >> >s> Hello, > >> >s> I'm fairly new at Python so hopefully this question won't be too s> > >> >awful. ?I am writing some code that will FTP to a host, and want to s> > >> >catch any exception that may occur, take that and print it out s> > >> >(eventually put it into a log file and perform some alerting action). > >> >s> I've figured out two different ways to do this, and am wondering > >> >which s> is the best (i.e. cleanest, 'right' way to proceed). ?I'm > >> >also trying s> to understand exactly what occurs for each one. s> The > >> >first example: > >> >s> from ftplib import FTP > >> >s> try: > >> >s> ? ? ftp = FTP(ftp_host) > >> >s> ? ? ftp.login(ftp_user, ftp_pass) s> except Exception, err: > >> >s> ? ? print err > > >> I think you should restrict yourself to those exceptions that are > >> related to ftp. Do you want to catch an exception like a misspelling in > >> one of the variables? > > > He quite reasonably could want that, such as if the program is designed > > to be run from a cron job, or in some other non-interactive way. > > Why is it okay for non-interactive programs to silently have incorrect > behaviour? > > What's the point of a cron job that doesn't do what it is supposed to, > because it has a bug which is silently swallowed? Seriously, do you *ever* take more than 2 seconds to consider whether you might be missing something obvious before following up with these indignant knee-jerk responses? I never said a thing about swallowing errors. I was talking about catching exceptions, whence you might do all kinds of things besides swallowing (like logging). I was pointing out there are reasons why you might want to catch variable misspelling errors when you're running non-interactively (like for instance, to log the error somewhere). Or would you rather let all unexpected exceptions print to standard error, which is often a black hole in non-interactive sitations? Carl Banks From nelson1977 at gmail.com Tue Jul 14 04:34:10 2009 From: nelson1977 at gmail.com (nelson -) Date: Tue, 14 Jul 2009 10:34:10 +0200 Subject: Remote audio recording in a multiuser environment Message-ID: Hi all! I'm developing an application that will be used in a school. It will allow client connected over ssh to liste to a multimedia file on the server. The server componente will record the audio taken from a specified client microphone, or it would pair two client, allowing them to communicate each other registering the conversation. I think to use pygst to record the microphone, but i can' figure out how to register remote sound and how to create the channel between two client. Do you think i have to use twisted (i don't know this framework at all), or there is a simple solution? I will surely use wxpython for the UI on the client and on the server. Thank for any advice, nelson -- Consulenze Linux e Windows http://nelsonenterprise.net From tedwards at it.dcs.ch Tue Jul 14 05:11:13 2009 From: tedwards at it.dcs.ch (Tim Edwards) Date: Tue, 14 Jul 2009 11:11:13 +0200 Subject: 'unable to execute cc: No such file or directory' with distutils on Solaris Message-ID: <4A5C4BB1.4050204@it.dcs.ch> Hi, I'm trying to compile http://sourceforge.net/projects/astlib/ 0.17.1 on a Solaris 10 SPARC system. This python module uses distutils.ccompiler. The problem seems to be that although I have gcc installed in /usr/sfw/bin/gcc it keeps trying to execute the command 'cc', which doesn't work: cc -c actread.c -o actread.o unable to execute cc: No such file or directory error: command 'cc' failed with exit status 1 I've tried modifying the setup.py file to specify "cc.compiler = '/usr/sfw/bin/gcc'" and setting the $CC envrionment var. But I get the same error everytime. Can someone suggest how to change where distutils is looking for the compiler? Thanks Tim Edwards From rileycrane at gmail.com Tue Jul 14 05:12:15 2009 From: rileycrane at gmail.com (R C) Date: Tue, 14 Jul 2009 02:12:15 -0700 (PDT) Subject: MySQLdb + SSH Tunnel References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: Got it working. Thanks for your help 1) login to B 2) setup a tunnel in the shell machine-B> ssh -L B_ip_address:B_port:C_ip_address:C_port user at C_ip_address for example: machine-B has ip 1.1.1.1 machine-C has ip 2.2.2.2 then I would type: machine-B> ssh -L 1.1.1.1:3307:2.2.2.2:3306 user at 2.2.2.2 now the code that is running on machine-A would use MySQLdb in the following way import MySQLdb connection = MySQLdb.connect (user='myname',passwd='mypass',db='mydb',host='1.1.1.1',port=3307) NOTE: the port is an integer, NOT a string On Jul 12, 9:18?pm, Riley Crane wrote: > OVERVIEW: > I am running a script on one machine that connects to a MySQL database > on another machine that is outside of our university's domain. > According to the administrator, network policies do not allow the > compute nodes to access machines outside of our university's domain. > > COMPUTERS: > A = compute node within university (I do not have shell access) > B = 2nd machine within university that does not block outside > connections (I have root access) > C = machine outside of university (I have root access) > mysqldb on A cannot connect to C ....but..... > mysqldb on A can connect to B > > WHAT I WOULD LIKE TO DO: > Is it possible to set something up where A talks to a port on B, and > that port is actually nothing more than 3306 on C? ?Can I do this with > an SSH tunnel? > > Can anyone please give precise instructions? From vodkalove at gmail.com Tue Jul 14 05:14:19 2009 From: vodkalove at gmail.com (Riley Crane) Date: Tue, 14 Jul 2009 02:14:19 -0700 (PDT) Subject: MySQLdb + SSH Tunnel References: <171b01cc-201e-407c-97c4-34556cfa3035@j21g2000vbn.googlegroups.com> Message-ID: <03687615-f8b2-427f-af2d-4cfcb23604f8@f33g2000vbm.googlegroups.com> Got it working. Thanks for your help! 1) login to B 2) setup a tunnel in the shell machine-B> ssh -L B_ip_address:B_port:C_ip_address:C_port user at C_ip_address for example: machine-B has ip 1.1.1.1 machine-C has ip 2.2.2.2 then I would type: machine-B> ssh -L 1.1.1.1:3307:2.2.2.2:3306 user at 2.2.2.2 now the code that is running on machine-A would use MySQLdb in the following way import MySQLdb connection = MySQLdb.connect (user='myname',passwd='mypass',db='mydb',host='1.1.1.1',port=3307) NOTE: the port is an integer, NOT a string On Jul 12, 9:18?pm, Riley Crane wrote: > OVERVIEW: > I am running a script on one machine that connects to a MySQL database > on another machine that is outside of our university's domain. > According to the administrator, network policies do not allow the > compute nodes to access machines outside of our university's domain. > > COMPUTERS: > A = compute node within university (I do not have shell access) > B = 2nd machine within university that does not block outside > connections (I have root access) > C = machine outside of university (I have root access) > mysqldb on A cannot connect to C ....but..... > mysqldb on A can connect to B > > WHAT I WOULD LIKE TO DO: > Is it possible to set something up where A talks to a port on B, and > that port is actually nothing more than 3306 on C? ?Can I do this with > an SSH tunnel? > > Can anyone please give precise instructions? From steven at REMOVE.THIS.cybersource.com.au Tue Jul 14 05:14:52 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 14 Jul 2009 09:14:52 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 01:30:48 -0700, Carl Banks wrote: > Seriously, do you *ever* take more than 2 seconds to consider whether > you might be missing something obvious before following up with these > indignant knee-jerk responses? Obviously not. [...] > Or would you rather let all unexpected exceptions print to standard > error, which is often a black hole in non-interactive sitations? Fair point. Of course you're right. -- Steven From hartley79 at gmail.com Tue Jul 14 05:22:24 2009 From: hartley79 at gmail.com (hartley) Date: Tue, 14 Jul 2009 02:22:24 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> Message-ID: <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> > > I'm very new at wrapping Python/C, and I have run into some problems. > > > I have one python module that provides me with a list (provideBuffer > > in provideBuff.py): > > > ?Py_Initialize(); > > ? ? ? ? pName = PyString_FromString("provideBuff"); > > ? ? ? ? pModule = PyImport_Import(pName); > > > ? ? ? ? pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > pValue is now a PyList - i've even verified this with: > > > int a = PyList_Check(pValue); > > ? ? ? ? printf("%d\n", a); > > > However, I want to send this PyList to another python module, > > Please explain "send" ... do you mean the C equivalent of the Python > statement C_embedding.buff = the_pylist ? > > BTW C-embedding would trigger a syntax error in Python source; best to > avoid ... I'm sorry I'm not using the proper terms when trying to describe this - i've never really learnt those terms, I guess i should do that one day. Imagine that the python function C-embedding.buff looks like this: def buff(a): if isinstance(a,list): print "success" I want to send, pass, call, whatever a list argument from the C code onto this function. As simple as this - how can you call a function in python from C, and providing a python list as an argument? As I wrote earlier - I already have a PyList, called pValue. But I have not been able to use this as an argument for C-embedding.buff using the code I described last time. > > but I > > don't know how to do this. Initially I though I could just do like > > above, only swapping NULL with pValue, but that is not working. > > > pName2 = PyString_FromString("C-embedding"); > > pModule2 = PyImport_Import(pName2); > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > Get?? Do you want Set? Is buff a Python function? Or is it the > destination of the "sending"? Any good reason for not checking the > return value for an error? [Rhetorical question; answer == "No"] > > > pValue2 = PyObject_CallObject(pFunc2,pValue); > > CallObject?? You used this before because you had a function and > wanted to call it because it returned you a value .... now you want to > do one of (in Python terms) > > value = amodule.anattr > value = getattr(amodule, "anattr") > > or > > amodule.anattr = value > setattr(amodule, "anattr", value) > > > pValue2 is now False! > > False?? Do you mean the C NULL? > > > So i guess i cannot pass pValue as an argument > > to PyObject_CallObject when i want to pass an python list as an > > argument. But how must a go about to make this work? > > It's mainly just a matter of (1) knowing what you want to do (2) > picking the API that does what you want (3) checking the returned > value for error after every call. > > HTH, > John From clp2 at rebertia.com Tue Jul 14 05:25:15 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 02:25:15 -0700 Subject: How to unbuffer Python's output In-Reply-To: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> References: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> Message-ID: <50697b2c0907140225s59df742fpdfb2673d837e4f3d@mail.gmail.com> 2009/7/13 Lily Gao : > Hi, All > > I am calling a python program in perl and use redirection, > > Like : > > `python x.py > 1.log 2>&1` > > When x.py crash, I get nothing from 1.log, and if I don?t use redirection, I > can get useful log from the screen. > > How can I do to make x.py ?s? output un-buffered when redirection log to > files ,just exactly same with print to the screen? Use the -u flag to python: ?u Force stdin, stdout and stderr to be totally unbuffered. python -u x.py > 1.log 2>&1 Cheers, Chris -- http://blog.rebertia.com From amrita at iisermohali.ac.in Tue Jul 14 05:43:17 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Tue, 14 Jul 2009 15:13:17 +0530 (IST) Subject: copy a few lines Message-ID: <23461.210.212.36.65.1247564597.squirrel@www.iisermohali.ac.in> Dear all, I want to know if i want to copy one paragraph from one text file to another then what command i have to use in python.Looking forwaard for soon reply. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From clp2 at rebertia.com Tue Jul 14 05:50:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 02:50:13 -0700 Subject: copy a few lines In-Reply-To: <23461.210.212.36.65.1247564597.squirrel@www.iisermohali.ac.in> References: <23461.210.212.36.65.1247564597.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907140250lb2bdc63y4872c522cf216953@mail.gmail.com> On Tue, Jul 14, 2009 at 2:43 AM, wrote: > > > Dear all, > > I want to know if i want to copy one paragraph from one text file to > another then what command i have to use in python.Looking forwaard for > soon reply. Please don't double-post. It wastes everyone's time, especially when (a) your previous post was less than 3 hours ago, and (b) your previous post got a response. If you're refining your question, it's best to just reply to your own prior post. Also, you're going to need to define what precisely qualifies as a "paragraph" for your purposes. - Chris -- http://blog.rebertia.com From jon at peirce.org.uk Tue Jul 14 06:28:19 2009 From: jon at peirce.org.uk (JonPeirce) Date: Tue, 14 Jul 2009 03:28:19 -0700 (PDT) Subject: renice In-Reply-To: <1193438701.979110.114360@v3g2000hsg.googlegroups.com> References: <13422771.post@talk.nabble.com> <1193417206.734440.268840@o38g2000hse.googlegroups.com> <1193438701.979110.114360@v3g2000hsg.googlegroups.com> Message-ID: <24477000.post@talk.nabble.com> Note that with os.nice you can only *decrease* priority. For increasing priority you need to run; os.system("sudo renice -n %s %s" % (new_nice, os.getpid())) or simply set the nice level when you run python in the first place (you do also need sudo for this): sudo nice -n -15 python myScript.py Jon -- View this message in context: http://www.nabble.com/renice-tp13422771p24477000.html Sent from the Python - python-list mailing list archive at Nabble.com. From hartley79 at gmail.com Tue Jul 14 06:32:08 2009 From: hartley79 at gmail.com (hartley) Date: Tue, 14 Jul 2009 03:32:08 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> Message-ID: <3d3d9f7e-5c57-414b-9363-f1ebfca0f9e2@r25g2000vbn.googlegroups.com> On Jul 13, 6:35?pm, John Machin wrote: > On Jul 14, 1:47?am, hartley wrote: > > > > > I'm very new at wrapping Python/C, and I have run into some problems. > > > I have one python module that provides me with a list (provideBuffer > > in provideBuff.py): > > > ?Py_Initialize(); > > ? ? ? ? pName = PyString_FromString("provideBuff"); > > ? ? ? ? pModule = PyImport_Import(pName); > > > ? ? ? ? pFunc = PyObject_GetAttrString(pModule,"provideBuffer"); > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > pValue is now a PyList - i've even verified this with: > > > int a = PyList_Check(pValue); > > ? ? ? ? printf("%d\n", a); > > > However, I want to send this PyList to another python module, > > Please explain "send" ... do you mean the C equivalent of the Python > statement C_embedding.buff = the_pylist ? > > BTW C-embedding would trigger a syntax error in Python source; best to > avoid ... > > > but I > > don't know how to do this. Initially I though I could just do like > > above, only swapping NULL with pValue, but that is not working. > > > pName2 = PyString_FromString("C-embedding"); > > pModule2 = PyImport_Import(pName2); > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > Get?? Do you want Set? Is buff a Python function? Or is it the > destination of the "sending"? Any good reason for not checking the > return value for an error? [Rhetorical question; answer == "No"] > > > pValue2 = PyObject_CallObject(pFunc2,pValue); > > CallObject?? You used this before because you had a function and > wanted to call it because it returned you a value .... now you want to > do one of (in Python terms) > > value = amodule.anattr > value = getattr(amodule, "anattr") > > or > > amodule.anattr = value > setattr(amodule, "anattr", value) > > > pValue2 is now False! > > False?? Do you mean the C NULL? > > > So i guess i cannot pass pValue as an argument > > to PyObject_CallObject when i want to pass an python list as an > > argument. But how must a go about to make this work? > > It's mainly just a matter of (1) knowing what you want to do (2) > picking the API that does what you want (3) checking the returned > value for error after every call. > > HTH, > John I tried to keep this simple, but I realise that since I'm not using the terminology correctly, it may be hard understand what I mean. I'll do something even simpler: Say you have the function: C-embedding.buff def buff(s): if isinstance(s,list): return s how can i call (use,invoke,?? - sorry, about the terminology again) this function from C so that I call this function with an argument that is (accepted as) a python list? foo.bar def bar(): pName = PyString_FromString("foo"); pModule = PyImport_Import(pName); pFunc = PyObject_GetAttrString(pModule,"bar"); pValue = PyObject_CallObject(pFunc,NULL); From dana_at_work at yahoo.com Tue Jul 14 07:32:35 2009 From: dana_at_work at yahoo.com (dana) Date: Tue, 14 Jul 2009 04:32:35 -0700 (PDT) Subject: DBI module deprecated at Python 2.5--what to use in its place? References: <338f4e72-72c2-4121-86be-fad0af20e47e@h11g2000yqb.googlegroups.com> Message-ID: <9e63df1c-9757-4e94-98a0-f49b383a3ad6@h30g2000vbr.googlegroups.com> On Jul 10, 12:15?pm, "M.-A. Lemburg" wrote: > If you're looking for a stable and maintained ODBC for Python, > have a look at our mxODBC extension or mxODBC Connect package: > http://www.egenix.com/products/python/mxODBC/http://www.egenix.com/products/python/mxODBCConnect/ I'm looking for a free module. Is yours a commercial product? Thanks. Dana From torf at torfbold.com Tue Jul 14 07:38:45 2009 From: torf at torfbold.com (Florian Brucker) Date: Tue, 14 Jul 2009 13:38:45 +0200 Subject: Efficient binary search tree stored in a flat array? In-Reply-To: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: <4a5c5e7f$0$30235$9b4e6d93@newsspool1.arcor-online.net> Douglas Alan wrote: > Thank you. My question wasn't intended to be Python specific, though. > I am just curious for purely academic reasons about whether there is > such an algorithm. All the sources I've skimmed only seem to the > answer the question via omission. Which is kind of strange, since it > seems to me like an obvious question to ask. IIRC comp.programming would be the place to ask such questions. HTH, Florian From __peter__ at web.de Tue Jul 14 07:45:47 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Jul 2009 13:45:47 +0200 Subject: Tkinter / Entry widget problem References: Message-ID: Andras Szabo wrote: > Hello. I searched the archives but couldn't find a solution to a > problem related to the Entry widget in Tkinter. > > When creating a pop-up window in an app, which contains an Entry > widget, I want this widget to contain some default string, to have all > this default string selected (as if the user had manually selected > everything), and to have the focus transferred to this widget. > > (The idea is then that if the window pops up, the user won't have to > click or press Tab any more before being able to type what is needed > in the textbox, overwriting what is written there already.) > > I thought this might be the way to go: > > entrybox=Entry(toplevel_parent_window) > entrybox.insert(0,"Some default string") > entrybox.select_range(0,END) > entrybox.focus_set() > entrybox.pack() > > But it doesn't seem to work - the focus is not transferred to the > Entry widget, and the text does not appear to be selected (even though > after this entrybox.selection_present() returns True). > > What am I doing wrong? Nothing, I would think. Can you post a minimal runnable example? Peter From ldo at geek-central.gen.new_zealand Tue Jul 14 07:48:57 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 14 Jul 2009 23:48:57 +1200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: In message <93f6a517-63d8-4c80- bf19-4614b70999db at m7g2000prd.googlegroups.com>, Carl Banks wrote: > Or would you rather let all unexpected exceptions print to standard > error, which is often a black hole in non-interactive sitations? Since when? Cron, for example, collects standard error and mails it to you. From ldo at geek-central.gen.new_zealand Tue Jul 14 07:50:34 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 14 Jul 2009 23:50:34 +1200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: In message , seldan24 wrote: > For this particular script, all exceptions are fatal > and I would want them to be. I just wanted a way to catch them and > log them prior to program termination. You don't need to. They will be written to standard error anyway. From ldo at geek-central.gen.new_zealand Tue Jul 14 07:52:40 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 14 Jul 2009 23:52:40 +1200 Subject: The meaning of "=" (Was: tough-to-explain Python) References: Message-ID: In message , Steven D'Aprano wrote: > Are we supposed to interpret that post as Dumb Insolence or just Dumb? "Insolence" indeed ... another wanker to plonk, I think. From piet at cs.uu.nl Tue Jul 14 08:10:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 14:10:20 +0200 Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: >>>>> Douglas Alan (DA) wrote: >DA> On Jul 13, 3:57?pm, a... at pythoncraft.com (Aahz) wrote: >>> Still, unless your list is large (more than thousands of elements), >>> that's the way you should go. ?See the bisect module. ?Thing is, the >>> speed difference between C and Python means the constant for insertion >>> and deletion is very very small relative to bytecode speed. ?Keep in >>> mind that Python's object/binding model means that you're shuffling >>> pointers in the list rather than items. >DA> Thank you. My question wasn't intended to be Python specific, though. >DA> I am just curious for purely academic reasons about whether there is >DA> such an algorithm. All the sources I've skimmed only seem to the >DA> answer the question via omission. Which is kind of strange, since it >DA> seems to me like an obvious question to ask. Of course you can take any BST algorithm and replace pointers by indices in the array and allocate new elements in the array. But then you need array elements to contain the indices for the children explicitely. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From sjmachin at lexicon.net Tue Jul 14 08:21:30 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 05:21:30 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> Message-ID: On Jul 14, 7:22?pm, hartley wrote: > > > I'm very new at wrapping Python/C, and I have run into some problems. [snip] > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > > pValue is now a PyList - i've even verified this with: > > > > int a = PyList_Check(pValue); > > > ? ? ? ? printf("%d\n", a); > > > > However, I want to send this PyList to another python module, > > > Please explain "send" ... do you mean the C equivalent of the Python > > statement C_embedding.buff = the_pylist ? > > > BTW C-embedding would trigger a syntax error in Python source; best to > > avoid ... > > I'm sorry I'm not using the proper terms when trying to describe this > - i've never really learnt those terms, I guess i should do that one > day. Today would be a good day to start :-) > > Imagine that the python function C-embedding.buff looks like this: > > def buff(a): > ? ? if isinstance(a,list): > ? ? ? ? print "success" > > I want to send, pass, call, whatever a list argument from the C code > onto this function. As simple as this - how can you call a function in > python from C, and providing a python list as an argument? > > As I wrote earlier - I already have a PyList, called pValue. But I > have not been able to use this as an argument for C-embedding.buff > using the code I described last time. > > > > but I > > > don't know how to do this. Initially I though I could just do like > > > above, only swapping NULL with pValue, but that is not working. > > > > pName2 = PyString_FromString("C-embedding"); > > > pModule2 = PyImport_Import(pName2); > > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > Any good reason for not checking the > > return value for an error? [Rhetorical question; answer == "No"] > > > > pValue2 = PyObject_CallObject(pFunc2,pValue); Your problem is here, in the second arg of PyObject_CallObject. It should be either NULL if no args are being supplied, or a tuple containing the args if 1 or more args are being supplied. pValue is the arg itself; you need to wrap it in a tuple. See http://docs.python.org/c-api/object.html#PyObject_CallObject Actually you might like to use this instead (no tuple hassles): http://docs.python.org/c-api/object.html#PyObject_CallFunctionObjArgs What are you using as a source of information? Have you read this: http://docs.python.org/extending/embedding.html#pure-embedding ? It contains a very thorough example of calling a Python function from C, with all the reference-counting and error-checking stuff and it's quite runnable -- I've just now compiled it and run it (1) as-is (2) changed to pass a list instead of multiple ints (3) changed again to emulate you trying to pass the list directly instead of wrapped in a tuple ... here's the result: TypeError: argument list must be a tuple Call failed Oh by the way, you can get away with C-embedding as a module name if you are importing it from C, but importing it from Python [so that you could test it independently of your C program] presents a difficulty (syntax error) ... better to change it to c_embedding. > > It's mainly just a matter of (1) knowing what you want to do (2) > > picking the API that does what you want (3) checking the returned > > value for error after every call. HTH, John From candide at free.invalid Tue Jul 14 08:37:24 2009 From: candide at free.invalid (candide) Date: Tue, 14 Jul 2009 14:37:24 +0200 Subject: Python code for testing well parenthesized expression Message-ID: <4a5c7c04$0$16808$426a34cc@news.free.fr> Hi, I'm trying to implement in Python a function testing if an expression is well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. My code follows at the end. If you have a better algorithm or a better Python code (I'm a beginner in the Python world), don't hesitate ... #---------------------------------- # The obvious iterative version def i(s): op = 0 # op : open parenthesis for k in range(len(s)): op += (s[k] == '(') - (s[k] == ')') if op < 0: break return op # Recursive implementation of the preceding version def r(s,op=0): # op : open parenthesis if len(s)==0: return op elif s[0]=='(': return r(s[1:],op+1) elif s[0]==')': if op==0: return -1 else : return r(s[1:],op-1) else : return r(s[1:],op) # "functional" style version def f(s): if (len(s)!=0 and s[0]=='('): z=f(s[1:]) if len(z)!=0: return f(z[1:]) else: return None elif len(s)==0 or s[0] == ')': return s else: return f(s[1:]) def test(t,f): for z in t: print (z,f(z)==0) # True True True True True False False t=["zx4er(1(er(Yy)ol)ol)ik", "x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)", "a(ty(y(y(bn)))lokl)kl", "xc(er(tgy(rf(yh)()uj)ki))", "e", "rf(tgt)juj)jkik(jun)", "zx(4er(1(er(Yy)ol)ol)ik"] test(t,i) print test(t,r) print def test(t): for s in t: print (s,f(s)=="") test(t) #------------------------------------------- and the corresponding output : ('zx4er(1(er(Yy)ol)ol)ik', True) ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True) ('a(ty(y(y(bn)))lokl)kl', True) ('xc(er(tgy(rf(yh)()uj)ki))', True) ('e', True) ('rf(tgt)juj)jkik(jun)', False) ('zx(4er(1(er(Yy)ol)ol)ik', False) ('zx4er(1(er(Yy)ol)ol)ik', True) ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True) ('a(ty(y(y(bn)))lokl)kl', True) ('xc(er(tgy(rf(yh)()uj)ki))', True) ('e', True) ('rf(tgt)juj)jkik(jun)', False) ('zx(4er(1(er(Yy)ol)ol)ik', False) ('zx4er(1(er(Yy)ol)ol)ik', True) ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True) ('a(ty(y(y(bn)))lokl)kl', True) ('xc(er(tgy(rf(yh)()uj)ki))', True) ('e', True) ('rf(tgt)juj)jkik(jun)', False) ('zx(4er(1(er(Yy)ol)ol)ik', False) From jeremy+complangpython at jeremysanders.net Tue Jul 14 08:57:21 2009 From: jeremy+complangpython at jeremysanders.net (Jeremy Sanders) Date: Tue, 14 Jul 2009 13:57:21 +0100 Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: candide wrote: > I'm trying to implement in Python a function testing if an expression is > well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" > is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. > > My code follows at the end. > > If you have a better algorithm or a better Python code (I'm a beginner in > the Python world), don't hesitate ... Don't you want to just test that the number of "("s equals the number of ")"s or am I missing the point? >>> a='aAAA(bbb(cc)))' >>> a.count('(') == a.count(')') Jeremy -- Jeremy Sanders http://www.jeremysanders.net/ From adrian.dziubek at gmail.com Tue Jul 14 08:59:51 2009 From: adrian.dziubek at gmail.com (Adrian Dziubek) Date: Tue, 14 Jul 2009 05:59:51 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Strings are immutable, so your method of slicing one letter at time will be building lots of them. That shouldn't hurt you here, but it will when you hit a bigger problem. In the i() there should be "return op == 0" on the end. def well(expr): mapping = {'(':1, ')':-1} count = 0 for s in expr: if s in mapping: count += mapping[s] if s < 0: return False return count == 0 def test_well(): examples = [ ('zx4er(1(er(Yy)ol)ol)ik', True), ('x(x)x(x(x)xx(xx(x)x(x(x)xx)(xxxx))x(x(x)xx)(xxxx)x)(xxxx)', True), ('a(ty(y(y(bn)))lokl)kl', True), ('xc(er(tgy(rf(yh)()uj)ki))', True), ('e', True), ('rf(tgt)juj)jkik(jun)', False), ('zx(4er(1(er(Yy)ol)ol)ik', False), ] for expr, expected in examples: assert well(expr) == expected From adrian.dziubek at gmail.com Tue Jul 14 09:04:06 2009 From: adrian.dziubek at gmail.com (Adrian Dziubek) Date: Tue, 14 Jul 2009 06:04:06 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: <89cdf891-04e2-4c21-9043-cfdf91dd136a@j9g2000vbp.googlegroups.com> > Don't you want to just test that the number of "("s equals the number of > ")"s or am I missing the point? I had this idea too, but there is additional requirement that any beginning must have greater or equal number of '(' than ')'. -- Adrian From martin.hellwig at dcuktec.org Tue Jul 14 09:05:38 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 14 Jul 2009 14:05:38 +0100 Subject: Python code for testing well parenthesized expression In-Reply-To: <4a5c7c04$0$16808$426a34cc@news.free.fr> References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: candide wrote: To add to your implementations; a readable version: +++file parantheses.py+++ """Parentheses Module Test""" def parentheses_are_paired(input_string): "Check if 'input_string' contains paired parentheses, if so return True." parenthesis_count = 0 parenthesis_open = '(' parenthesis_close = ')' for character in input_string: if character == parenthesis_open: parenthesis_count += 1 elif character == parenthesis_close: parenthesis_count -= 1 if parenthesis_count == 0: return(True) else: if parenthesis_count < 0: error_text = 'More closing parantheses than opening ones' elif parenthesis_count > 0: error_text = 'More opening parantheses than closing ones' raise(ValueError(error_text)) if __name__ == '__main__': TEST_TRUE = 'zx4er(1(er(Yy)ol)ol)ik' TEST_ERROR = 'zx(4er(1(er(Yy)ol)ol)ik' print(parentheses_are_paired(TEST_TRUE)) try: print(parentheses_are_paired(TEST_ERROR)) except(ValueError): print(False) ---file parantheses.py--- -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From jeremy+complangpython at jeremysanders.net Tue Jul 14 09:06:42 2009 From: jeremy+complangpython at jeremysanders.net (Jeremy Sanders) Date: Tue, 14 Jul 2009 14:06:42 +0100 Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> <7c3dvuF25o0t6U1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > Yep, you are: > > "())))(((" > > is certainly not "well parenthized". Thanks for that! -- Jeremy Sanders http://www.jeremysanders.net/ From deets at nospam.web.de Tue Jul 14 09:07:20 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 14 Jul 2009 15:07:20 +0200 Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: <7c3dvuF25o0t6U1@mid.uni-berlin.de> Jeremy Sanders wrote: > candide wrote: > >> I'm trying to implement in Python a function testing if an expression is >> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" >> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. >> >> My code follows at the end. >> >> If you have a better algorithm or a better Python code (I'm a beginner in >> the Python world), don't hesitate ... > > Don't you want to just test that the number of "("s equals the number of > ")"s or am I missing the point? Yep, you are: "())))(((" is certainly not "well parenthized". Diez From dns4 at cornell.edu Tue Jul 14 09:08:12 2009 From: dns4 at cornell.edu (David Smith) Date: Tue, 14 Jul 2009 09:08:12 -0400 Subject: Python code for testing well parenthesized expression In-Reply-To: References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Jeremy Sanders wrote: > candide wrote: > >> I'm trying to implement in Python a function testing if an expression is >> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" >> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. >> >> My code follows at the end. >> >> If you have a better algorithm or a better Python code (I'm a beginner in >> the Python world), don't hesitate ... > > Don't you want to just test that the number of "("s equals the number of > ")"s or am I missing the point? > >>>> a='aAAA(bbb(cc)))' >>>> a.count('(') == a.count(')') > > Jeremy > Using straight counting, )z))ab(c(ew( would be well parenthesized. I suspect a better way would be to iterate over the string using a balance counter that increases 1 for every open, decreases 1 for every close. A negative balance at any moment would indicate an error as well as an ending balance greater than 0. --David From martin.hellwig at dcuktec.org Tue Jul 14 09:14:48 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 14 Jul 2009 14:14:48 +0100 Subject: Python code for testing well parenthesized expression In-Reply-To: References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Martin P. Hellwig wrote: > candide wrote: > To add to your implementations; a readable version: > > +++file parantheses.py+++ > """Parentheses Module Test""" > > def parentheses_are_paired(input_string): > "Check if 'input_string' contains paired parentheses, if so return > True." > parenthesis_count = 0 > parenthesis_open = '(' > parenthesis_close = ')' > > for character in input_string: > if character == parenthesis_open: > parenthesis_count += 1 > elif character == parenthesis_close: > parenthesis_count -= 1 > > > if parenthesis_count == 0: > return(True) > else: > if parenthesis_count < 0: > error_text = 'More closing parantheses than opening ones' > > elif parenthesis_count > 0: > error_text = 'More opening parantheses than closing ones' > > raise(ValueError(error_text)) > > if __name__ == '__main__': > TEST_TRUE = 'zx4er(1(er(Yy)ol)ol)ik' > TEST_ERROR = 'zx(4er(1(er(Yy)ol)ol)ik' > > print(parentheses_are_paired(TEST_TRUE)) > > try: > print(parentheses_are_paired(TEST_ERROR)) > except(ValueError): > print(False) > > ---file parantheses.py--- > However as Mr. Roggisch commented this wouldn't check if it is on the right spot, by moving the check that at any time you can not have more closing ones than opening ones within the loop you will get those exceptions too. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From sjmachin at lexicon.net Tue Jul 14 09:14:50 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 13:14:50 +0000 (UTC) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: candide free.invalid> writes: > # The obvious iterative version > def i(s): > op = 0 # op : open parenthesis > for k in range(len(s)): > op += (s[k] == '(') - (s[k] == ')') > if op < 0: break > return op > E: H c, w t P. F: A c, b ? P. Suggested better code: def iterative(string): paren_count = 0 for char in string: paren_count += (char == '(') - (char == ')') if paren_count < 0: break return paren_count You don't need the other versions :-) Try an iterative version of checking that () [] and {} are balanced and nested appropriately. Cheers, John From sjmachin at lexicon.net Tue Jul 14 09:17:48 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 06:17:48 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: On Jul 14, 11:08?pm, David Smith wrote: > Jeremy Sanders wrote: > > candide wrote: > > >> I'm trying to implement in Python a function testing if an expression is > >> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik" > >> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not. > > >> My code follows at the end. > > >> If you have a better algorithm or a better Python code (I'm a beginner in > >> the Python world), don't hesitate ... > > > Don't you want to just test that the number of "("s equals the number of > > ")"s or am I missing the point? > > >>>> a='aAAA(bbb(cc)))' > >>>> a.count('(') == a.count(')') > > > Jeremy > > Using straight counting, )z))ab(c(ew( would be well parenthesized. ?I > suspect a better way would be to iterate over the string using a balance > counter that increases 1 for every open, decreases 1 for every close. ?A > negative balance at any moment would indicate an error as well as an > ending balance greater than 0. > > --David Ummm isn't that the OP's first function? From Scott.Daniels at Acm.Org Tue Jul 14 09:19:37 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 06:19:37 -0700 Subject: Efficient binary search tree stored in a flat array? In-Reply-To: References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: Piet van Oostrum wrote: >>>>>> Douglas Alan (DA) wrote: > >> DA> On Jul 13, 3:57 pm, a... at pythoncraft.com (Aahz) wrote: >>>> Still, unless your list is large (more than thousands of elements), >>>> that's the way you should go. See the bisect module. Thing is, the >>>> speed difference between C and Python means the constant for insertion >>>> and deletion is very very small relative to bytecode speed. Keep in >>>> mind that Python's object/binding model means that you're shuffling >>>> pointers in the list rather than items. > >> DA> Thank you. My question wasn't intended to be Python specific, though. >> DA> I am just curious for purely academic reasons about whether there is >> DA> such an algorithm. All the sources I've skimmed only seem to the >> DA> answer the question via omission. Which is kind of strange, since it >> DA> seems to me like an obvious question to ask. It may well be that there is no good simple solution, and people avoid writing about non-existent algorithms. I certainly cannot imagine trying to write an article that carefully covered ideas which don't have well-studied data structures available, and calling them out only to say, "we don't know how to do this well." If such an algorithm were simple and obvious, I dare say you'd be taught about it around the time you learn binary search. > Of course you can take any BST algorithm and replace pointers by indices > in the array and allocate new elements in the array. But then you need > array elements to contain the indices for the children explicitely. And you loower your locality of reference (cache-friendliness). Note the insert in Python, for example, is quite cache-friendly. --Scott David Daniels Scott.Daniels at Acm.Org From aaron.watters at gmail.com Tue Jul 14 09:23:23 2009 From: aaron.watters at gmail.com (Aaron Watters) Date: Tue, 14 Jul 2009 06:23:23 -0700 (PDT) Subject: WHIFF 0.4 += repoze.who authentication + menus + calculator + docs. Message-ID: <1209aa77-c58c-4daf-a0f7-74f479ca59c8@f30g2000vbf.googlegroups.com> WHIFF (WSGI HTTP Integrated Filesystem Frames) 0.4 released. The new WHIFF 0.4 release linked from http://whiff.sourceforge.net includes the following enhancements: Built in support for repoze.who based authentication (from http://static.repoze.org/whodocs/ ) + tutorial see: http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1500.who . AJAX calculator demo and tutorial: see: http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1400.calc . Built in drop-down menu support and tutorial: see: http://aaron.oirt.rutgers.edu/myapp/docs/W1100_1300.menus . Some subtle but important bug fixes, including a thread race condition bug fix. Auto-reload support. Also documentation improvements: particularly for standard middleware see: http://aaron.oirt.rutgers.edu/myapp/docs/W1200_1400.stdMiddleware What is WHIFF? WHIFF is a collection of support services for Python WSGI applications which allows applications to be composed by "dropping" dynamic pages into container directories. This mode of development will be familiar to developers who have created PHP applications, vanilla CGI scripts, Apache/modpy Publisher applications, JSP pages, or static web content. The WHIFF implementation significantly generalizes the "drop in" paradigm to support WSGI middleware components and application fragments as well as stand-alone pages. WHIFF provides other services in addition to supporting "drop in" components, such as managed application resources. I created WHIFF to address complexity issues I encounter when creating and fixing sophisticated Web applications which include complex database interactions and dynamic features such as AJAX (Asynchronous Javascript And XML). Project home page: http://whiff.sourceforge.net . Documentation index: http://aaron.oirt.rutgers.edu/myapp/docs/W.intro . I hope you like it! -- Aaron Watters === % ping elvis elvis is alive From sjmachin at lexicon.net Tue Jul 14 09:26:38 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 14 Jul 2009 13:26:38 +0000 (UTC) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: Adrian Dziubek gmail.com> writes: > In the i() there should be "return > op == 0" on the end. Hard to tell. In the absence of any docs, we have to resort to divination on the function name :-P ... is "i" short for "iterative" or "imbalanced"? From l.lensgraf at gmail.com Tue Jul 14 09:49:38 2009 From: l.lensgraf at gmail.com (DrLeif) Date: Tue, 14 Jul 2009 06:49:38 -0700 (PDT) Subject: PDF: finding a blank image References: Message-ID: <736570b5-c315-4eb2-99d1-5de7f05c26d0@l28g2000vba.googlegroups.com> On Jul 13, 6:22?pm, Scott David Daniels wrote: > DrLeif wrote: > > I have about 6000 PDF files which have been produced using a scanner > > with more being produced each day. ?The PDF files contain old paper > > records which have been taking up space. ? The scanner is set to > > detect when there is information on the backside of the page (duplex > > scan). ?The problem of course is it's not the always reliable and we > > wind up with a number of PDF files containingblankpages. > > > What I would like to do is have python detect a "blank" pages in a PDF > > file and remove it. ?Any suggestions? > > I'd check into ReportLab's commercial product, it may well be easily > capable of that. ?If no success, you might contact PJ at Groklaw, she > has dealt with a _lot_ of PDFs (and knows people who deal with PDFs > in bulk). > > --Scott David Daniels > Scott.Dani... at Acm.Org From aahz at pythoncraft.com Tue Jul 14 09:54:07 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 06:54:07 -0700 Subject: Infinite loops and synchronization References: Message-ID: In article , Vincent Gulinao wrote: > >lst = list() > >while True: > if len(lst) == SOME_NUMBER: > return lst > >Q2: operating on list from threads (mostly appends) must be safe, >right (synchronization)? What do you mean by "safe"? Python certainly won't crash, but there's no guarantee that the list will be consistent from *your* perspective. Consider what happens if len(lst) == SOME_NUMBER - 1 and some other part of your code adds two elements to lst. You'll skip right over your if condition. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From aahz at pythoncraft.com Tue Jul 14 09:55:37 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 06:55:37 -0700 Subject: compilation problem of python on AIX 6.1 References: <51c556a8-7277-474d-821f-190adf155c75@f16g2000vbf.googlegroups.com> Message-ID: In article <51c556a8-7277-474d-821f-190adf155c75 at f16g2000vbf.googlegroups.com>, Amit wrote: > >THen I decided to compile python 2.5.4 on my AIX box. I downloaded the >python source code from www.python.org and tried to compile on my AIX >both using gcc. > > case $MAKEFLAGS in *-s*) CC='gcc -pthread' LDSHARED='./Modules/ >ld_so_aix gcc -pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g - >fwrapv -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py -q >build;; *) CC='gcc -pthread' LDSHARED='./Modules/ld_so_aix gcc - >pthread -bI:Modules/python.exp' OPT='-DNDEBUG -g -fwrapv -O3 -Wall - >Wstrict-prototypes' ./python -E ./setup.py build;; esac >running build >running build_ext >building 'bz2' extension >gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall - >Wstrict-prototypes -I. -I/avre/Python-2.5.4/./Include -I. -IInclude - >I./Include -I/avre/Python-2.5.4/Include -I/avre/Python-2.5.4 -c /avre/ >Python-2.5.4/Modules/bz2module.c -o build/temp.aix-6.1-2.5/avre/ >Python-2.5.4/Modules/bz2module.o >building '_tkinter' extension >./Modules/ld_so_aix gcc -pthread -bI:Modules/python.exp build/ >temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/_tkinter.o build/ >temp.aix-6.1-2.5/avre/Python-2.5.4/Modules/tkappinit.o -L/usr/X11/lib - >ltk8.4 -ltcl8.4 -lX11 -o build/lib.aix-6.1-2.5/_tkinter.so >*** WARNING: renaming "_tkinter" since importing it failed: 0509-022 >Cannot load module build/lib.aix-6.1-2.5. > 0509-026 System error: A file or directory in the path name does not >exist. This looks like a generic AIX compilation error finding system libraries; you probably will get better results from asking on an AIX forum. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From fatkinson at mishmash.com Tue Jul 14 09:56:01 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Tue, 14 Jul 2009 06:56:01 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: On Tue, 14 Jul 2009 05:41:40 GMT, Tim Harig wrote: >On 2009-07-14, Fred Atkinson wrote: >> The one thing I really dislike about Python over PHP is that >> Python can usually only appear in the cgi directory (unless other >> arragements are made with your hosting provider or if you reconfigure >> Apache on your own server if you have your own). With PHP, I can put >> them in any folder on my Web site without any problem. > >That is a server configuration and has nothing to do with Python directly. Agreed, it doesn't. But if my hosting provider won't change it, I'm stuck with it. Regards, Fred From fatkinson at mishmash.com Tue Jul 14 09:58:58 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Tue, 14 Jul 2009 06:58:58 -0700 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: <5k3p5514u11kldk56vjgpop4jp90hf3o89@4ax.com> On Mon, 13 Jul 2009 23:54:11 -0700 (PDT), alex23 wrote: >Fred Atkinson wrote: >> ? ? ? ? I wish the Python site was as well written as the PHP site. On >> the PHP site, I can look up a command and they show not only the docs >> on that command but a list of all other commands associated with it. ? > >Hey Fred, > >My problem is the complete opposite, I wish I could as easily >interrogate objects in PHP about their behaviour as I can Python :) > >In case you're not familiar with this, you can see what methods are >attached to an object using the intepreter via dir() and see the >associated docstring for them via help(). Try looking at 'dir(str)' >for a list of methods that strings recognise, then 'help(str.join)' to >see the help on join. > >This is such a handy ability to have that I miss this casual >introspection in everything else in which I'm required to develop. > >Hope this helps. I appreciate the information. I'm halfway through a course in PHP and Python. We are given assignments and we have to write the assignment in both languages. I'll take a look at that as I work on this week's assignment (I've only got a month to finish the course but I can probably take some time to look at this. Regards, Fred From phonky at europe.com Tue Jul 14 10:03:16 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 16:03:16 +0200 Subject: one-time factory in python for an experienced java guy Message-ID: Hi I have searched all over and haven't found the solution for my problem yet. I am new to python, and all the time realize I do program python in java, which is not great. Besides being a real-life problem, I want to solve it as elegant as I can, using it to also learn about python (I know I could just hack something easy for now). That's what I want to do. I have an Account class. An Account instance has to get an account number. On instance creation, I want the account number to be generated (I don't want callers to pass the account # to ensure uniqueness): class Account(object): def __init__(self, holder): self.__accountnumber = self.__generate_account_number() Now, I do not know yet how the account number scheme looks like. For now, I just want to have an incremental number; later, when going to production, we'll need the proper one. Furthermore, as we plan to distribute the package, we want to allow custom account numbering schemes. Thus, customers should be able to plug in their own AccountNumberGenerator implementation. For now, I have a generators.py module. I have an AccountNumberGenerator base class, all subclasses should implement "generate". I have an IncrementalGenerator subclass. So for now, I need to instantiate IncrementalGenerator, allowing for a future module to plugin their own generator. Any suggestions on how to do this? Thanks so much!!!! From l.lensgraf at gmail.com Tue Jul 14 10:04:16 2009 From: l.lensgraf at gmail.com (DrLeif) Date: Tue, 14 Jul 2009 07:04:16 -0700 (PDT) Subject: PDF: finding a blank image References: Message-ID: <6ba293b7-bba4-4d7b-ba3b-fb7901a2ea7d@e18g2000vbe.googlegroups.com> On Jul 13, 6:22?pm, Scott David Daniels wrote: > DrLeif wrote: > > I have about 6000 PDF files which have been produced using a scanner > > with more being produced each day. ?The PDF files contain old paper > > records which have been taking up space. ? The scanner is set to > > detect when there is information on the backside of the page (duplex > > scan). ?The problem of course is it's not the always reliable and we > > wind up with a number of PDF files containingblankpages. > > > What I would like to do is have python detect a "blank" pages in a PDF > > file and remove it. ?Any suggestions? > > I'd check into ReportLab's commercial product, it may well be easily > capable of that. ?If no success, you might contact PJ at Groklaw, she > has dealt with a _lot_ of PDFs (and knows people who deal with PDFs > in bulk). > > --Scott David Daniels > Scott.Dani... at Acm.Org Thanks everyone for the quick reply. I had considered using ReportLab however, was uncertain about it's ability to detect a blank page. Scott, I'll drop an email to ReportLab and PJ.... Thanks again, DrLeif From jjposner at optimum.net Tue Jul 14 10:07:52 2009 From: jjposner at optimum.net (John Posner) Date: Tue, 14 Jul 2009 10:07:52 -0400 Subject: Tkinter / Entry widget problem In-Reply-To: References: Message-ID: <4A5C9138.2020502@optimum.net> > Andras Szabo wrote: > > >> Hello. I searched the archives but couldn't find a solution to a >> problem related to the Entry widget in Tkinter. >> >> When creating a pop-up window in an app, which contains an Entry >> widget, I want this widget to contain some default string, to have all >> this default string selected (as if the user had manually selected >> everything), and to have the focus transferred to this widget. >> >> (The idea is then that if the window pops up, the user won't have to >> click or press Tab any more before being able to type what is needed >> in the textbox, overwriting what is written there already.) >> >> I thought this might be the way to go: >> >> entrybox=Entry(toplevel_parent_window) >> entrybox.insert(0,"Some default string") >> entrybox.select_range(0,END) >> entrybox.focus_set() >> entrybox.pack() >> >> But it doesn't seem to work - the focus is not transferred to the >> Entry widget, and the text does not appear to be selected (even though >> after this entrybox.selection_present() returns True). >> >> What am I doing wrong? >> > > Nothing, I would think. Can you post a minimal runnable example? > > Peter This works for me, on Windows and Linux: from Tkinter import * rt = Tk() rt.title("root window") pop = Toplevel() pop.title("second window") entrybox=Entry(pop) entrybox.insert(0,"Some default string") entrybox.select_range(0,END) entrybox.focus_set() entrybox.pack() rt.withdraw() rt.mainloop() On Win/XP SP3: > python Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter >>> Tkinter.TkVersion 8.5 >>> Tkinter.TclVersion 8.5 On Ubuntu Linux (Ubu-SL 2.6.27.14-generic): $ python Python 2.6.1 (r261:67515, Jan 9 2009, 19:06:23) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter >>> Tkinter.TkVersion 8.4000000000000004 >>> Tkinter.TclVersion 8.4000000000000004 -John From piet at cs.uu.nl Tue Jul 14 10:10:06 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 16:10:06 +0200 Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: >>>>> Fred Atkinson (FA) wrote: >FA> On Tue, 14 Jul 2009 05:41:40 GMT, Tim Harig >FA> wrote: >>> On 2009-07-14, Fred Atkinson wrote: >>>> The one thing I really dislike about Python over PHP is that >>>> Python can usually only appear in the cgi directory (unless other >>>> arragements are made with your hosting provider or if you reconfigure >>>> Apache on your own server if you have your own). With PHP, I can put >>>> them in any folder on my Web site without any problem. >>> >>> That is a server configuration and has nothing to do with Python directly. >FA> Agreed, it doesn't. But if my hosting provider won't change it, I'm >FA> stuck with it. That's a good reason to dislike your hosting provider, not a good reason to dislike Python. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From stefan_ml at behnel.de Tue Jul 14 10:27:48 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 14 Jul 2009 16:27:48 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: References: Message-ID: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> phonky wrote: > class Account(object): > def __init__(self, holder): > self.__accountnumber = self.__generate_account_number() > > Now, I do not know yet how the account number scheme looks like. > For now, I just want to have an incremental number; later, > when going to production, we'll need the proper one. Use a global variable in the module. Stefan From http Tue Jul 14 10:33:26 2009 From: http (Paul Rubin) Date: 14 Jul 2009 07:33:26 -0700 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7xzlb7wczt.fsf@ruckus.brouhaha.com> Stefan Behnel writes: > phonky wrote: > > class Account(object): > > def __init__(self, holder): > > self.__accountnumber = self.__generate_account_number() > > > > Now, I do not know yet how the account number scheme looks like. > > For now, I just want to have an incremental number; later, > > when going to production, we'll need the proper one. > > Use a global variable in the module. Yuch! Don't use a global. Try something like: import itertools class Account(object): def __init__(self, holder, gen=itertools.count()): self.__accountnumber = gen.next() From phonky at europe.com Tue Jul 14 10:34:30 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 16:34:30 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> Message-ID: Stefan, thanks first of all > Use a global variable in the module. I have an account_number_generator variable in the module, I got that hint from searching the web. But where my stubborn java mind doesn't release me: what does the variable contain? Do I create the actual IncrementalGenerator object there? Or the super class? Or just a string, which a factory method takes to create the actual object? What I especially don't get: How will an external module overwrite that variable? I fail to see, python being interpreted, how I can ensure that a future module is being executed later, thus overwriting the variable. Thanks again. From newport at freemail.hu Tue Jul 14 10:38:42 2009 From: newport at freemail.hu (Andras Szabo) Date: Tue, 14 Jul 2009 17:38:42 +0300 Subject: Tkinter / Entry widget problem In-Reply-To: <4A5C9138.2020502@optimum.net> References: <4A5C9138.2020502@optimum.net> Message-ID: So it's either that I use Python 2.5.1, or that I use it on a Mac. (John, your code still doesn't work the way it's supposed to here.) I guess I'll upgrade to 2.6.1 and see if it makes a difference. (The Tkinter/Tcl versions are the same for me.) Thanks for your help. andras On Jul 14, 2009, at 5:07 PM, John Posner wrote: > >> Andras Szabo wrote: >> >> >>> Hello. I searched the archives but couldn't find a solution to a >>> problem related to the Entry widget in Tkinter. >>> >>> When creating a pop-up window in an app, which contains an Entry >>> widget, I want this widget to contain some default string, to have >>> all >>> this default string selected (as if the user had manually selected >>> everything), and to have the focus transferred to this widget. >>> >>> (The idea is then that if the window pops up, the user won't have to >>> click or press Tab any more before being able to type what is needed >>> in the textbox, overwriting what is written there already.) >>> >>> I thought this might be the way to go: >>> >>> entrybox=Entry(toplevel_parent_window) >>> entrybox.insert(0,"Some default string") >>> entrybox.select_range(0,END) >>> entrybox.focus_set() >>> entrybox.pack() >>> >>> But it doesn't seem to work - the focus is not transferred to the >>> Entry widget, and the text does not appear to be selected (even >>> though >>> after this entrybox.selection_present() returns True). >>> >>> What am I doing wrong? >>> >> >> Nothing, I would think. Can you post a minimal runnable example? >> >> Peter > > This works for me, on Windows and Linux: > > from Tkinter import * > > rt = Tk() > rt.title("root window") > pop = Toplevel() > pop.title("second window") > > entrybox=Entry(pop) > entrybox.insert(0,"Some default string") > entrybox.select_range(0,END) > entrybox.focus_set() > entrybox.pack() > > rt.withdraw() > rt.mainloop() > > > On Win/XP SP3: > > > python > Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> import Tkinter > >>> Tkinter.TkVersion > 8.5 > >>> Tkinter.TclVersion > 8.5 > > On Ubuntu Linux (Ubu-SL 2.6.27.14-generic): > > $ python > Python 2.6.1 (r261:67515, Jan 9 2009, 19:06:23) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import Tkinter > >>> Tkinter.TkVersion > 8.4000000000000004 > >>> Tkinter.TclVersion > 8.4000000000000004 > > > -John > > From http Tue Jul 14 10:40:52 2009 From: http (Paul Rubin) Date: 14 Jul 2009 07:40:52 -0700 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <7xab37l43v.fsf@ruckus.brouhaha.com> phonky writes: > But where my stubborn java mind doesn't release me: what does the > variable contain? Do I create the actual IncrementalGenerator object > there? Or the super class? Or just a string, which a factory method > takes to create the actual object? Ugh, just forget everything you ever knew about java. Do some Zen exercises to erase your mind. Then read a Python tutorial as if you're starting from nothing. From contact at xavierho.com Tue Jul 14 10:52:55 2009 From: contact at xavierho.com (Xavier Ho) Date: Tue, 14 Jul 2009 22:52:55 +0800 Subject: Why does extend() fail in this case, and what am I doing wrong? Message-ID: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? The code is run at: http://codepad.org/wgLU4JZh class A(): def __init__(self): self.n = [1, 2, 3, 4, 5] a = A() print a.n print a.n.extend([6, 7, 8, 9]) #Output: #[1, 2, 3, 4, 5] #None I really don't know, but I'm probably missing something. Any pointers would be great. Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From phonky at europe.com Tue Jul 14 10:55:56 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 16:55:56 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: <7xab37l43v.fsf@ruckus.brouhaha.com> References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> Message-ID: <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Thanks Paul, > Ugh, just forget everything you ever knew about java. Do some Zen > exercises to erase your mind. Then read a Python tutorial as if > you're starting from nothing. Yeah, surely right, but easier said than done... I'm working on it. Taking your example. import itertools class Account(object): def __init__(self, holder, gen=itertools.count()): self.__accountnumber = gen.next() If you consider my python illiteracy, "itertools.count(): Make an iterator that returns consecutive integers starting with n" to me that sounds like that solves the increment issue, but what about future modules wanting to plug in a different numbering format, e.g. 205434.1234 or whatever? From tn.pablo at gmail.com Tue Jul 14 11:07:29 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Tue, 14 Jul 2009 10:07:29 -0500 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Message-ID: This has been asked extensively before, here and elsewhere. On Tue, Jul 14, 2009 at 09:52, Xavier Ho wrote: > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? > The code is run at: http://codepad.org/wgLU4JZh > > class A(): > ??? def __init__(self): > ??????? self.n = [1, 2, 3, 4, 5] > > a = A() > print a.n > print a.n.extend([6, 7, 8, 9]) > > #Output: > #[1, 2, 3, 4, 5] > #None > > I really don't know, but I'm probably missing something. Any pointers would > be great. > > Best regards, > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Pablo Torres N. From mabdelkader at gmail.com Tue Jul 14 11:07:59 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Tue, 14 Jul 2009 11:07:59 -0400 Subject: one-time factory in python for an experienced java guy In-Reply-To: <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: <148918f0907140807i3b31f9b0u94fecf309eec5cdc@mail.gmail.com> What Paul was trying to elaborate on is that have your customers or whomever will use this implement their own generator protocol to generate whatever number format they need. Paul just gave you an example with itertools.count(), where it is an infinite generator that yields count+1 every time. Read up on the iterator protocol, have your customers pass in a generator to establish their own formats. http://docs.python.org/library/stdtypes.html#iterator-types This is much more elegant than AbstractCustomerRandomFormatGenerator..etc which is what the reference to "erase everything from Java" is attempting to relate. -- mahmoud mack abdelkader python powered http://blog.mahmoudimus.com/ mahmoud at linux.com On Tue, Jul 14, 2009 at 10:55 AM, phonky wrote: > Thanks Paul, > > Ugh, just forget everything you ever knew about java. Do some Zen >> exercises to erase your mind. Then read a Python tutorial as if >> you're starting from nothing. >> > > Yeah, surely right, but easier said than done... > I'm working on it. > > Taking your example. > > import itertools > > class Account(object): > def __init__(self, holder, gen=itertools.count()): > self.__accountnumber = gen.next() > > If you consider my python illiteracy, > > "itertools.count(): Make an iterator that returns consecutive integers > starting with n" > > to me that sounds like that solves the increment issue, but what about > future modules wanting to plug in a different > numbering format, e.g. 205434.1234 or whatever? > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Tue Jul 14 11:13:43 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 16:13:43 +0100 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Message-ID: <4A5CA0A7.7090800@mrabarnett.plus.com> Xavier Ho wrote: > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? > The code is run at: http://codepad.org/wgLU4JZh > > class A(): > def __init__(self): > self.n = [1, 2, 3, 4, 5] > > a = A() > print a.n > print a.n.extend([6, 7, 8, 9]) > > #Output: > #[1, 2, 3, 4, 5] > #None > > I really don't know, but I'm probably missing something. Any pointers > would be great. > The extend() method modifies the list in-place and returns None. Try printing a.n after extending. From http Tue Jul 14 11:14:18 2009 From: http (Paul Rubin) Date: 14 Jul 2009 08:14:18 -0700 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: <7xzlb7fgad.fsf@ruckus.brouhaha.com> phonky writes: > "itertools.count(): Make an iterator that returns consecutive integers > starting with n" > > to me that sounds like that solves the increment issue, but what about > future modules wanting to plug in a different > numbering format, e.g. 205434.1234 or whatever? You'd write a different generator for that, and use it instead of itertools.count. E.g. (untested): def different_gen(): a = 201593.0768 # initial number while True: yield '%.04f'% a a += 123.4567 # increase in increments of this much From __peter__ at web.de Tue Jul 14 11:20:03 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Jul 2009 17:20:03 +0200 Subject: one-time factory in python for an experienced java guy References: <4a5c95e4$0$31340$9b4e6d93@newsspool4.arcor-online.net> <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: phonky wrote: > Thanks Paul, > >> Ugh, just forget everything you ever knew about java. Do some Zen >> exercises to erase your mind. Then read a Python tutorial as if >> you're starting from nothing. > > Yeah, surely right, but easier said than done... > I'm working on it. > > Taking your example. > > import itertools > > class Account(object): > def __init__(self, holder, gen=itertools.count()): > self.__accountnumber = gen.next() > > If you consider my python illiteracy, > > "itertools.count(): Make an iterator that returns consecutive integers > starting with n" > > to me that sounds like that solves the increment issue, but what about > future modules wanting to plug in a different > numbering format, e.g. 205434.1234 or whatever? In that case you may want to stick with the class attribute: >>> class Account(object): ... def __init__(self): ... self.account = self.next_account() ... def __str__(self): ... return "Account(number=%r)" % self.account ... __repr__ = __str__ ... >>> from itertools import count >>> Account.next_account = count(42).next >>> a = Account() >>> b = Account() >>> a, b (Account(number=42), Account(number=43)) >>> from uuid import uuid1 >>> Account.next_account = staticmethod(uuid1) >>> c = Account() >>> d = Account() >>> c, d (Account(number=UUID('b0f8dfc6-7087-11de-be16-001d923f29c5')), Account(number=UUID('b310c90e-7087-11de-be16-001d923f29c5'))) You can plug in arbitrary callables at runtime. The only complication I can see is that you may have to wrap them into a staticmethod to prevent python from passing the self reference. You can avoid that if you just use a global instead: # account.py next_account = ... class Account(object): def __init__(self): self.number = next_account() You can then set the factory elsewhere # main.py import account account.next_account = ... a = Account() In general in python we like to keep simple things simple rather than creating a huge bureaucracy. Peter From ml at well-adjusted.de Tue Jul 14 11:20:13 2009 From: ml at well-adjusted.de (Jochen Schulz) Date: Tue, 14 Jul 2009 17:20:13 +0200 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> Message-ID: <20090714152012.GY31378@wasteland.homelinux.net> Xavier Ho: > > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? -- snip > print a.n.extend([6, 7, 8, 9]) extend doesn't fail. It just returns None and extends the list in place. In [1]: l = [1, 2, 3] In [2]: l.extend([4, 5, 6]) In [3]: l Out[3]: [1, 2, 3, 4, 5, 6] J. -- When I get home from the supermarket I don't know what to do with all the plastic. [Agree] [Disagree] -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From aahz at pythoncraft.com Tue Jul 14 11:24:14 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 08:24:14 -0700 Subject: one-time factory in python for an experienced java guy References: <7xab37l43v.fsf@ruckus.brouhaha.com> <23406$4a5c9c7d$d9a2f023$27926@news.hispeed.ch> Message-ID: In article <23406$4a5c9c7d$d9a2f023$27926 at news.hispeed.ch>, phonky wrote: > >import itertools > > class Account(object): > def __init__(self, holder, gen=itertools.count()): > self.__accountnumber = gen.next() > >If you consider my python illiteracy, > >"itertools.count(): Make an iterator that returns consecutive integers >starting with n" > >to me that sounds like that solves the increment issue, but what about >future modules wanting to plug in a different >numbering format, e.g. 205434.1234 or whatever? Here's what I would do: class Account: gen = itertools.count gen_instance = None def __init__(self, holder): if self.gen_instance is None: self.__class__.gen_instance = self.gen() self._account = self.gen_instance() Notice that I'm using only a single underscore for ``_account`` to make inheritance simpler, and I'm using ``self`` to access class attributes *except* when I need to *set* the class attribute, which requires ``self.__class__``. Now anyone who wants to change the generator can simply do module.Account.gen = other_gen and it will work as long as no Account() instances have been created (you don't want the generator to change mid-stream, right?). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From pdpinheiro at gmail.com Tue Jul 14 11:35:40 2009 From: pdpinheiro at gmail.com (pdpi) Date: Tue, 14 Jul 2009 08:35:40 -0700 (PDT) Subject: one-time factory in python for an experienced java guy References: Message-ID: On Jul 14, 3:03?pm, phonky wrote: > Hi > > I have searched all over and haven't found the solution > for my problem yet. I am new to python, and all the time realize I > do program python in java, which is not great. > > Besides being a real-life problem, I want to > solve it as elegant as I can, using it to > also learn about python (I know I could just > hack something easy for now). > > That's what I want to do. > > I have an Account class. > An Account instance has to get an account number. > On instance creation, I want the account number to > be generated (I don't want callers to pass > the account # to ensure uniqueness): > > class Account(object): > ? ? ? ? def __init__(self, holder): > ? ? ? ? ? ? ? ? self.__accountnumber = self.__generate_account_number() > > Now, I do not know yet how the account number scheme looks like. > For now, I just want to have an incremental number; later, > when going to production, we'll need the proper one. > > Furthermore, as we plan to distribute the package, we want > to allow custom account numbering schemes. Thus, customers > should be able to plug in their own AccountNumberGenerator implementation. > > For now, I have a generators.py module. > > I have an AccountNumberGenerator base class, > all subclasses should implement "generate". > > I have an IncrementalGenerator subclass. > > So for now, I need to instantiate IncrementalGenerator, > allowing for a future module to plugin their own generator. > > Any suggestions on how to do this? > Thanks so much!!!! You don't want an AccountNumberGenerator class and subclassing, all you need is an iterator/generator of some form. These might help: http://docs.python.org/tutorial/classes.html#iterators http://docs.python.org/tutorial/classes.html#generators (in fact, that whole page is pretty relevant) Once your code expects one of those, it's trivially easy to plug something else in there. From Scott.Daniels at Acm.Org Tue Jul 14 11:44:04 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 08:44:04 -0700 Subject: Proposal: Decimal literals in Python. In-Reply-To: References: <4721ab28$0$1238$e4fe514c@dreader28.news.xs4all.nl> <1193438763.190428.134710@q5g2000prf.googlegroups.com> <87tzodii6i.fsf@benfinney.id.au> <1193494184.618613.81090@50g2000hsm.googlegroups.com> Message-ID: Tim Roberts wrote: > My favorite notation for this comes from Ada, which allows arbitrary bases > from 2 to 16, and allows for underscores within numeric literals: > > x23_bin : constant := 2#0001_0111#; > x23_oct : constant := 8#27#; > x23_dec : constant := 10#23#; > x23_hex : constant := 16#17#; And mine is one w/o the base 10 bias: .f.123 == 0x123 .7.123 == 0o123 .1.1101 == 0b1101 That is, .. -- show the base by showing base-1 in the base. I actually built this into "OZ," an interpretter. --Scott David Daniels Scott.Daniels at Acm.Org From pavlovevidence at gmail.com Tue Jul 14 11:53:33 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 08:53:33 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: On Jul 14, 2:14?am, Steven D'Aprano wrote: > On Tue, 14 Jul 2009 01:30:48 -0700, Carl Banks wrote: > > Seriously, do you *ever* take more than 2 seconds to consider whether > > you might be missing something obvious before following up with these > > indignant knee-jerk responses? > > Obviously not. > > [...] > > > Or would you rather let all unexpected exceptions print to standard > > error, which is often a black hole in non-interactive sitations? > > Fair point. Of course you're right. I don't want to be mean or anything. I agree with you on 95% of issues probably, of course I only follow up to disagree.... Carl Banks From contact at xavierho.com Tue Jul 14 11:54:05 2009 From: contact at xavierho.com (Xavier Ho) Date: Tue, 14 Jul 2009 23:54:05 +0800 Subject: Why does extend() fail in this case, and what am I doing wrong? In-Reply-To: <20090714152012.GY31378@wasteland.homelinux.net> References: <2d56febf0907140752x2fe62cf2y9a542821059dd6cc@mail.gmail.com> <20090714152012.GY31378@wasteland.homelinux.net> Message-ID: <2d56febf0907140854s52d20904y90c7ccb6a0262cb9@mail.gmail.com> I see. Thanks! Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Tue, Jul 14, 2009 at 11:20 PM, Jochen Schulz wrote: > Xavier Ho: > > > > Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ? > -- snip > > print a.n.extend([6, 7, 8, 9]) > > extend doesn't fail. It just returns None and extends the list in place. > > In [1]: l = [1, 2, 3] > > In [2]: l.extend([4, 5, 6]) > > In [3]: l > Out[3]: [1, 2, 3, 4, 5, 6] > > > J. > -- > When I get home from the supermarket I don't know what to do with all the > plastic. > [Agree] [Disagree] > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.9 (GNU/Linux) > > iEYEARECAAYFAkpcoiwACgkQ+AfZydWK2zmchgCfZjRCOGTa0OZ1Q045sCLZfpvD > EIEAnjJ8/uNwPYFfCsGNbQIDd5+LnkbA > =fCdV > -----END PGP SIGNATURE----- > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Tue Jul 14 11:55:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 16:55:18 +0100 Subject: Proposal: Decimal literals in Python. In-Reply-To: References: <4721ab28$0$1238$e4fe514c@dreader28.news.xs4all.nl> <1193438763.190428.134710@q5g2000prf.googlegroups.com> <87tzodii6i.fsf@benfinney.id.au> <1193494184.618613.81090@50g2000hsm.googlegroups.com> Message-ID: <4A5CAA66.3080608@mrabarnett.plus.com> Scott David Daniels wrote: > Tim Roberts wrote: >> My favorite notation for this comes from Ada, which allows arbitrary >> bases >> from 2 to 16, and allows for underscores within numeric literals: >> >> x23_bin : constant := 2#0001_0111#; >> x23_oct : constant := 8#27#; >> x23_dec : constant := 10#23#; >> x23_hex : constant := 16#17#; > And mine is one w/o the base 10 bias: > .f.123 == 0x123 > .7.123 == 0o123 > .1.1101 == 0b1101 > That is, .. > -- show the base by showing base-1 in the base. > I actually built this into "OZ," an interpretter. > Smalltalk uses "r" (for "radix"). If we also permit underscores: x23_bin = 2r0001_0111 x23_oct = 8r27 x23_dec = 10r23 x23_hex = 16r17 From doctoresam at gmail.com Tue Jul 14 11:55:36 2009 From: doctoresam at gmail.com (Deep_Feelings) Date: Tue, 14 Jul 2009 08:55:36 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? Message-ID: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> So you have chosen programming language "x" so shall you tell us why you did so , and what negatives or positives it has ? From aahz at pythoncraft.com Tue Jul 14 11:58:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 08:58:30 -0700 Subject: The meaning of "=" References: Message-ID: In article , Piet van Oostrum wrote: >>>>>> Lawrence D'Oliveiro (LD) wrote: > >>LD> In message , Aahz wrote: > >>Aahz> class AttrDict: >>Aahz> def __getitem__(self, key): >>Aahz> return getattr(self, key) > >>LD> OK, let's try it: > >>LD> >>> c = {} >>LD> >>> c["x"] = 3 >>LD> >>> c.x = 4 >>LD> Traceback (most recent call last): >>LD> File "", line 1, in >>LD> AttributeError: 'dict' object has no attribute 'x' >>LD> >>> class AttrDict: >>LD> ... def __getitem__(self, key): >>LD> ... return getattr(self, key) >>LD> ... >>LD> >>> c.x = 4 >>LD> Traceback (most recent call last): >>LD> File "", line 1, in >>LD> AttributeError: 'dict' object has no attribute 'x' > >>LD> Nope, still doesn't work... > >Of course you need c = AttrDict() Absolutely -- Lawrence really needs to learn to do his own debugging. >And to get c.x = 4 working you also need a __setitem__. Nope. You do need __setitem__ so that this works: c['x'] = 4 >And to get c["x"] working AtrrDict should subclass dict: Definitely not. There's a perfectly good dict inside a regular class instance already. The only reason to subclass from dict is so that you get all the dict methods for free; however, the cost is that you get ugly bugs because of e.g. c['update'] = 'foo' Overally, I think it's much better/safer to explicitly pull the dict methods you want to use. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From jfrancis4970 at mailinator.com Tue Jul 14 11:58:44 2009 From: jfrancis4970 at mailinator.com (jfrancis4970 at mailinator.com) Date: Tue, 14 Jul 2009 08:58:44 -0700 (PDT) Subject: how to determine if python is run interactively? (-i) Message-ID: <17280e94-6c15-4225-9d28-3dff4e0b61a5@p15g2000vbl.googlegroups.com> how do you determine, from within a python program, whether the python interpreter was launched in interactive mode? in other words, if i have a program called "test.py", i want to ensure that the program was launched with this command line: python -i test.py (and not just with "python test.py"). the reason is that i'm doing some very expensive and lengthy initialization in my test.py program that will be useless unless the program exits to the python prompt when it is done, where the user can run further python commands. obviously i can't use sys.argv since that is only for parameters passed to the test.py program, not for parameters passed to the python interpreter itself. i haven't found a way to access these options... any help would be greatly appreciated! From pavlovevidence at gmail.com Tue Jul 14 12:01:37 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 09:01:37 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: <7d161f1f-2c09-42ca-9bbd-b99a777044ea@j21g2000vbn.googlegroups.com> On Jul 14, 4:48?am, Lawrence D'Oliveiro wrote: > In message <93f6a517-63d8-4c80- > > bf19-4614b7099... at m7g2000prd.googlegroups.com>, Carl Banks wrote: > > Or would you rather let all unexpected exceptions print to standard > > error, which is often a black hole in non-interactive sitations? > > Since when? > > Cron, for example, collects standard error and mails it to you. 1. Cron is only one way to run programs non-interactively. (Would that work with, say, Windows services?) 2. Many systems don't run MTAs these days 3. In my experience the pipeline from cron to mail delivery is error prone, mainly due to the complexity of configuring MTAs 4. Even if all this works, you might just want to do your logging directly in Python anyway Carl Banks From rylesny at gmail.com Tue Jul 14 12:12:01 2009 From: rylesny at gmail.com (ryles) Date: Tue, 14 Jul 2009 09:12:01 -0700 (PDT) Subject: how to set timeout while colling a soap method? References: Message-ID: On Jul 13, 9:07?am, dzizes wrote: > Hello! > > I wrote some some python code for executing a soap method: > > import SOAPpy > from SOAPpy import WSDL > > _server = WSDL.Proxy(some wsdl) > r=_server.generuj(some parameters...) > print r.encode('cp1250') > > It works fine. However, the execution time of this soap method might be > long. Therefore, I would like to set a timeout like 1 minute, after which I > would post a timeoute message. > > Any ideas? > Greats, > > -- > View this message in context:http://www.nabble.com/how-to-set-timeout-while-colling-a-soap-method-... > Sent from the Python - python-list mailing list archive at Nabble.com. I don't believe SOAPpy supports this directly. You can set the timeout globally before you make your SOAP call: import socket socket.setdefaulttimeout(60) http://docs.python.org/library/socket.html#socket.setdefaulttimeout From python at mrabarnett.plus.com Tue Jul 14 12:14:03 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 17:14:03 +0100 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <4A5CAECB.8080604@mrabarnett.plus.com> Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and what negatives or positives it has ? I've heard of "C" and "D", but not "x", unless you mean XPL (X Programming Language) or PLAN-X. :-) From duncan.booth at invalid.invalid Tue Jul 14 12:26:28 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 14 Jul 2009 16:26:28 GMT Subject: how to determine if python is run interactively? (-i) References: <17280e94-6c15-4225-9d28-3dff4e0b61a5@p15g2000vbl.googlegroups.com> Message-ID: jfrancis4970 at mailinator.com wrote: > how do you determine, from within a python program, whether the python > interpreter was launched in interactive mode? sys.flags.interactive or sys.flags.inspect From duncan.booth at invalid.invalid Tue Jul 14 13:10:17 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 14 Jul 2009 17:10:17 GMT Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: John Machin wrote: > Try an iterative version of checking that () [] and {} > are balanced and nested appropriately. Here's how I might approach the more general case: def balanced(s, parens=("()",)): ''' Example: >>> balanced('aAAA(b[bb(c]c))') True >>> balanced('aAAA(b[bb(c]c))', parens=("()", "[]")) False ''' s = re.sub("[^%s]+" % re.escape("".join(parens)), "", s) for i in range(len(s)/2): for p in parens: s = s.replace(p, "") return not s For short strings this is obviously slower than your 'iterative' function, but the run time mostly depends on the number of pairs of parentheses rather than the total length of the string, so for example it starts winning on a string with 2 pairs of parentheses about 75 characters long. From amrita at iisermohali.ac.in Tue Jul 14 13:33:59 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Tue, 14 Jul 2009 23:03:59 +0530 (IST) Subject: select lines in python Message-ID: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Dear all, Can anyone tell me that suppose i have a file having content like: _Atom_name _Atom_type _Chem_shift_value _Chem_shift_value_error _Chem_shift_ambiguity_code 1 1 PHE H H 8.49 0.02 1 2 1 PHE HA H 4.60 0.02 1 3 1 PHE CA C 57.83 0.3 1 4 2 LEU H H 8.23 0.02 1 5 2 LEU HA H 4.25 0.02 1 6 2 LEU HB2 H 1.54 0.02 1 7 3 ASP H H 8.10 0.02 1 8 3 ASP HA H 4.52 0.02 1 9 3 ASP HB2 H 2.65 0.02 1 stop now what i want that instead of copying all the lines it will just write the information about PHE and ASP then how i acn do that using python programming.Kindly tell me the command for that. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From degibb at gmail.com Tue Jul 14 13:48:06 2009 From: degibb at gmail.com (David Gibb) Date: Tue, 14 Jul 2009 13:48:06 -0400 Subject: select lines in python In-Reply-To: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> References: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Message-ID: try something like: for line in open("filename").readlines(): if (re.search("PHE|ASP",line): print line On Tue, Jul 14, 2009 at 1:33 PM, wrote: > Dear all, > > Can anyone tell me that suppose i have a file having content like: > > ? ?_Atom_name > ? ? ?_Atom_type > ? ? ?_Chem_shift_value > ? ? ?_Chem_shift_value_error > ? ? ?_Chem_shift_ambiguity_code > ? ? 1 ? ? 1 ? PHE ? ? ? H ? ? H ? ? ?8.49 ? ? 0.02 ? ? 1 > ? ? 2 ? ? 1 ? PHE ? ? ? HA ? ?H ? ? ?4.60 ? ? 0.02 ? ? 1 > ? ? 3 ? ? 1 ? PHE ? ? ? CA ? ?C ? ? 57.83 ? ? ?0.3 ? ? 1 > ? ? 4 ? ? 2 ? LEU ? ? ? H ? ? H ? ? ?8.23 ? ? 0.02 ? ? 1 > ? ? 5 ? ? 2 ? LEU ? ? ? HA ? ?H ? ? ?4.25 ? ? 0.02 ? ? 1 > ? ? 6 ? ? 2 ? LEU ? ? ? HB2 ? H ? ? ?1.54 ? ? 0.02 ? ? 1 > ? ? 7 ? ? 3 ? ASP ? ? ? H ? ? H ? ? ?8.10 ? ? 0.02 ? ? 1 > ? ? 8 ? ? 3 ? ASP ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > ? ? 9 ? ? 3 ? ASP ? ? ? HB2 ? H ? ? ?2.65 ? ? 0.02 ? ? 1 > stop > > > now what i want that instead of copying all the lines it will just write > the information about PHE and ASP then how i acn do that using python > programming.Kindly tell me the command for that. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > > -- > http://mail.python.org/mailman/listinfo/python-list > From phonky at europe.com Tue Jul 14 13:50:54 2009 From: phonky at europe.com (phonky) Date: Tue, 14 Jul 2009 19:50:54 +0200 Subject: one-time factory in python for an experienced java guy In-Reply-To: References: Message-ID: Thanks for all replies. I need to practice much more pythonese.... In fact I don't think to understand all of your suggestions, so I'll need to go through them and decide what approach I am going to take. Thanks a lot! From mensanator at aol.com Tue Jul 14 13:52:39 2009 From: mensanator at aol.com (Mensanator) Date: Tue, 14 Jul 2009 10:52:39 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> On Jul 14, 10:55?am, Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and ?what negatives or positives it has ? language must have - unlimited precision integers - easy to program - IDE not required - reasonable speed - math library needs to include number theoretic functions like GCD, LCM, Modular Inverse, etc. - not fucking retarded like F# That leaves Python (along with gympy, the Python wrapper for the GMP library, which could be used with C or C++ if it weren't for the ease of use issue) as the obvious choice. As for negatives, the GMP library doesn't factor. As for positives, you can call a factoring program from Python and capture the output (and have it correct the bug in the factoring program without having to fix the factoring program). From tonylay at gmail.com Tue Jul 14 13:59:24 2009 From: tonylay at gmail.com (Tony Lay) Date: Tue, 14 Jul 2009 10:59:24 -0700 (PDT) Subject: ImportError: No module named _functools Message-ID: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> I'm sooo close to getting this meld program running....had to install a lot of things in /usr/local to get to pygtk2 functional and I'm trying to run the meld program and get... RHEL4 server Traceback (most recent call last): File "/usr/local/bin/meld", line 35, in import gettext File "/usr/local/lib/python2.6/gettext.py", line 49, in import locale, copy, os, re, struct, sys File "/usr/local/lib/python2.6/locale.py", line 15, in import functools File "/usr/local/lib/python2.6/functools.py", line 10, in from _functools import partial, reduce ImportError: No module named _functools This is with me building all of the dependencies as well as python with ./configure --prefix=/usr/local --exec-prefix=/usr/local make distclean make make -i install ldconfig export PYTHONHOME=/usr/local/lib/python2.6 export PYTHONPATH=/usr/local/lib/python2.6:/usr/local/lib/python2.6/ site-packages I thought it might have been an err of the application, but I ran python and received the same error: [root at orlstscts1 meld-1.0.0]# python Python 2.6.2 (r262:71600, Jul 14 2009, 11:47:04) [GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import functools Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.6/functools.py", line 10, in from _functools import partial, reduce ImportError: No module named _functools Kind of a python n00b, but the functools.py is there, it's in the path...everything seems to be lined up right. Problem is that the server this is on is stable (read older), and I am to not touch the older software unless it's as a last resort. For this reason everything is in /usr/local and built from the ground up. I'm hoping that there's some silly dependency I'm missing. I've installed the following: atk-1.26.0 pkg-config-0.23 cairo-1.8.8 libglade-2.6.4 pycairo-1.8.6 fontconfig-2.7.0 libpng-1.2.37 pygobject-2.18.0 freetype-2.1.10 libxml2-2.7.3 pygtk-2.15.2 gettext-0.17 meld-1.0.0 pygtk-2.8.6 glade3-3.6.7 meld-1.1.5 Python-2.6.2 glib-2.20.4 glibc-2.9 meld-1.3.0 (no install) tcl8.5.7 tk8.5.7 gtk+-2.16.4 pango-1.20.5 intltool-0.35.5 pixman-0.15.14 Took me a while to get by fontconfig/freetype junk because (usr/local/ lib/libfontconfig.so: undefined reference to `FT_Select_Size') but that's a little OT. If there are any recommendations of what to attack or further information would help let me know! Regards, -Tony From weafon at lbl.gov Tue Jul 14 14:03:22 2009 From: weafon at lbl.gov (weafon) Date: Tue, 14 Jul 2009 11:03:22 -0700 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? Message-ID: <4A5CC86A.9070907@lbl.gov> Hi guys, I have a question about the usage of yield. As shown in the below example, in general, if there is a code segment commonly used by two or more functions, we may isolate the segment into a function and then call it from other functions if necessary. def func1(): .... while(cond): ..... commoncode() ... def func2(): .... while(cond): ..... commoncode() ... def commoncode() AAAA BBBB CCCC However, if there is a 'yield' operation in the common code segment, the isolation causes that func1 and func2 become a non-generator function!! Although I can prevent such an isolation by just duplicating the segment in func1 and func2 to keep both of them being generator functions, the code may become ugly and hard to maintain particularly when coomoncode() is long. The problem may be resolved if I can define the commoncode() as an inline function or marco. Unfortunately, inline and marco do not seems to be implemented in python. Thus, how can I isolate a common segment into a function when there are yield operations in the common segment? Thanks, Weafon From nagle at animats.com Tue Jul 14 14:06:17 2009 From: nagle at animats.com (John Nagle) Date: Tue, 14 Jul 2009 11:06:17 -0700 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> Message-ID: <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> walterbyrd wrote: > I believe Guido himself has said that all indentions should be four > spaces - no tabs. > > Since backward compatibility is being thrown away anyway, why not > enforce the four space rule? > > At least that way, when I get python code from somebody else, I would > know what I am looking at, without having to do a hex dump, or > something. Python 3 enforces the rule that you can't mix tabs and spaces for indentation in the same file. That (finally) guarantees that the indentation you see is what the Python parser sees. That's enough to prevent non-visible indentation errors. It also means that the Python parser no longer has to have any concept of how many spaces equal a tab. So the problem is now essentially solved. John Nagle From rhodri at wildebst.demon.co.uk Tue Jul 14 14:21:47 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 14 Jul 2009 19:21:47 +0100 Subject: select lines in python In-Reply-To: References: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Message-ID: On Tue, 14 Jul 2009 18:48:06 +0100, David Gibb wrote: [Something top-posted, which I've shuffled down] > On Tue, Jul 14, 2009 at 1:33 PM, wrote: >> Dear all, >> >> Can anyone tell me that suppose i have a file having content like: >> >> ? ?_Atom_name >> ? ? ?_Atom_type >> ? ? ?_Chem_shift_value >> ? ? ?_Chem_shift_value_error >> ? ? ?_Chem_shift_ambiguity_code >> ? ? 1 ? ? 1 ? PHE ? ? ? H ? ? H ? ? ?8.49 ? ? 0.02 ? ? 1 >> ? ? 2 ? ? 1 ? PHE ? ? ? HA ? ?H ? ? ?4.60 ? ? 0.02 ? ? 1 >> ? ? 3 ? ? 1 ? PHE ? ? ? CA ? ?C ? ? 57.83 ? ? ?0.3 ? ? 1 >> ? ? 4 ? ? 2 ? LEU ? ? ? H ? ? H ? ? ?8.23 ? ? 0.02 ? ? 1 >> ? ? 5 ? ? 2 ? LEU ? ? ? HA ? ?H ? ? ?4.25 ? ? 0.02 ? ? 1 >> ? ? 6 ? ? 2 ? LEU ? ? ? HB2 ? H ? ? ?1.54 ? ? 0.02 ? ? 1 >> ? ? 7 ? ? 3 ? ASP ? ? ? H ? ? H ? ? ?8.10 ? ? 0.02 ? ? 1 >> ? ? 8 ? ? 3 ? ASP ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 >> ? ? 9 ? ? 3 ? ASP ? ? ? HB2 ? H ? ? ?2.65 ? ? 0.02 ? ? 1 >> stop >> >> >> now what i want that instead of copying all the lines it will just write >> the information about PHE and ASP then how i acn do that using python >> programming.Kindly tell me the command for that. > try something like: > > for line in open("filename").readlines(): > if (re.search("PHE|ASP",line): > print line > The readlines() is unnecessary, and if your file is at all long you'd be better off precompiling the regular expression. import re expr = re.compile("PHE|ASP") with open("filename") as f: for line in f: if expr.search(line): print line -- Rhodri James *-* Wildebeest Herder to the Masses From milesck at umich.edu Tue Jul 14 14:24:06 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Tue, 14 Jul 2009 14:24:06 -0400 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: <4A5CC86A.9070907@lbl.gov> References: <4A5CC86A.9070907@lbl.gov> Message-ID: <388B397B-40B2-4A4E-857E-49C842708F7F@umich.edu> On Jul 14, 2009, at 2:03 PM, weafon wrote: > Hi guys, > > I have a question about the usage of yield. As shown in the below > example, in general, if there is a code segment commonly used by two > or more functions, we may isolate the segment into a function and > then call it from other functions if necessary. > > def func1(): > .... > while(cond): > ..... > commoncode() > ... > > > def func2(): > .... > while(cond): > ..... > commoncode() > ... > > def commoncode() > AAAA > BBBB > CCCC > > However, if there is a 'yield' operation in the common code segment, > the isolation causes that func1 and func2 become a non-generator > function!! Although I can prevent such an isolation by just > duplicating the segment in func1 and func2 to keep both of them > being generator functions, the code may become ugly and hard to > maintain particularly when coomoncode() is long. > > The problem may be resolved if I can define the commoncode() as an > inline function or marco. Unfortunately, inline and marco do not > seems to be implemented in python. Thus, how can I isolate a common > segment into a function when there are yield operations in the > common segment? def func1(): ... while cond: ... for x in commoncode(): yield x ... See also: - PEP 380: http://www.python.org/dev/peps/pep-0380/ - Stackless: http://www.stackless.com/ -Miles From pfeldman at verizon.net Tue Jul 14 14:25:08 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Tue, 14 Jul 2009 11:25:08 -0700 (PDT) Subject: missing 'xor' Boolean operator Message-ID: <24485116.post@talk.nabble.com> Current Boolean operators are 'and', 'or', and 'not'. It would be nice to have an 'xor' operator as well. -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24485116.html Sent from the Python - python-list mailing list archive at Nabble.com. From stefan_ml at behnel.de Tue Jul 14 14:26:30 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 14 Jul 2009 20:26:30 +0200 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and what negatives or positives it has ? Java, pays a living. *duck* Stefan From http Tue Jul 14 14:47:08 2009 From: http (Paul Rubin) Date: 14 Jul 2009 11:47:08 -0700 Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> Message-ID: <7xab37t843.fsf@ruckus.brouhaha.com> Mensanator writes: > - unlimited precision integers > - easy to program > - IDE not required > - reasonable speed > - math library needs to include number theoretic functions > like GCD, LCM, Modular Inverse, etc. > - not fucking retarded like F# Have you looked at Haskell? > As for negatives, the GMP library doesn't factor. Maybe with some MIRACL bindings... From dickinsm at gmail.com Tue Jul 14 14:47:41 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 14 Jul 2009 11:47:41 -0700 (PDT) Subject: missing 'xor' Boolean operator References: Message-ID: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" wrote: > Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to > have an 'xor' operator as well. Hmm. I don't think 'nice' is sufficient. You'd need to make the case that it's sufficiently useful to justify adding a new keyword 'xor' to the language; I suspect that would be an uphill struggle. :) I'll just note that: (1) It's easy to emulate xor: 'x xor y' <-> bool(x) != bool(y) (2) 'and' and 'or' are special in that they have useful short- circuiting behaviour; xor doesn't have this property (that is, you always need to evaluate *both* operands to determine the result). I'd also guess that 'xor' would be much less used than 'and' or 'or', but maybe that's just a reflection of the sort of code that I tend to write. -- Mark From amrita at iisermohali.ac.in Tue Jul 14 15:10:32 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Wed, 15 Jul 2009 00:40:32 +0530 (IST) Subject: select lines in python Message-ID: <29449.210.212.36.65.1247598632.squirrel@www.iisermohali.ac.in> Can i become more precise like instead of printing all lines for PHE and ASP is it possible that for PHE python will print only those lines which will have information about H and HA and for ASP it will print those lines which will have information about HA and HB. Thanks > On Tue, 14 Jul 2009 18:48:06 +0100, David Gibb wrote: > > [Something top-posted, which I've shuffled down] > >> On Tue, Jul 14, 2009 at 1:33 PM, wrote: >>> Dear all, >>> >>> Can anyone tell me that suppose i have a file having content like: >>> >>> ?? ??_Atom_name >>> ?? ?? ??_Atom_type >>> ?? ?? ??_Chem_shift_value >>> ?? ?? ??_Chem_shift_value_error >>> ?? ?? ??_Chem_shift_ambiguity_code >>> ?? ?? 1 ?? ?? 1 ?? PHE ?? ?? ?? H ?? ?? H ?? ?? ??8.49 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 2 ?? ?? 1 ?? PHE ?? ?? ?? HA ?? ??H ?? ?? ??4.60 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 3 ?? ?? 1 ?? PHE ?? ?? ?? CA ?? ??C ?? ?? 57.83 ?? ?? ??0.3 ?? ?? >>> 1 >>> ?? ?? 4 ?? ?? 2 ?? LEU ?? ?? ?? H ?? ?? H ?? ?? ??8.23 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 5 ?? ?? 2 ?? LEU ?? ?? ?? HA ?? ??H ?? ?? ??4.25 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 6 ?? ?? 2 ?? LEU ?? ?? ?? HB2 ?? H ?? ?? ??1.54 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 7 ?? ?? 3 ?? ASP ?? ?? ?? H ?? ?? H ?? ?? ??8.10 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 8 ?? ?? 3 ?? ASP ?? ?? ?? HA ?? ??H ?? ?? ??4.52 ?? ?? 0.02 ?? ?? >>> 1 >>> ?? ?? 9 ?? ?? 3 ?? ASP ?? ?? ?? HB2 ?? H ?? ?? ??2.65 ?? ?? 0.02 ?? ?? >>> 1 >>> stop >>> >>> >>> now what i want that instead of copying all the lines it will just >>> write >>> the information about PHE and ASP then how i acn do that using python >>> programming.Kindly tell me the command for that. > >> try something like: >> >> for line in open("filename").readlines(): >> if (re.search("PHE|ASP",line): >> print line >> > > The readlines() is unnecessary, and if your file is at all long you'd > be better off precompiling the regular expression. > > import re > > expr = re.compile("PHE|ASP") > with open("filename") as f: > for line in f: > if expr.search(line): > print line > > -- > Rhodri James *-* Wildebeest Herder to the Masses > -- > http://mail.python.org/mailman/listinfo/python-list > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From invalid at invalid Tue Jul 14 15:12:53 2009 From: invalid at invalid (Grant Edwards) Date: Tue, 14 Jul 2009 14:12:53 -0500 Subject: select lines in python References: Message-ID: On 2009-07-14, amrita at iisermohali.ac.in wrote: > Can i become more precise like instead of printing all lines > for PHE and ASP is it possible that for PHE python will print > only those lines which will have information about H and HA > and for ASP it will print those lines which will have > information about HA and HB? Yes. -- Grant Edwards grante Yow! I KAISER ROLL?! at What good is a Kaiser Roll visi.com without a little COLE SLAW on the SIDE? From knny.myer at gmail.com Tue Jul 14 15:15:57 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 12:15:57 -0700 (PDT) Subject: http://tinypic.com/view.php?pic=m78cgp&s=3 Message-ID: Hi, I'm experiencing a strange behaviour of the Python prompt when using the four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of getting the previous and next command respectively I get ugly characters. See it yourself: http://tinypic.com/view.php?pic=m78cgp&s=3 This is not directly Python-specific, but you feel quite handicapped if you must rewrite each command again... so my obvious question is: How can I fix this? Please, let me know if you can tell me something. Additional information: I'm running Ubuntu Linux I've tried the python prompt in several shell environments and got the same issue in csh, dash... all negative. Cheers, Kenny From knny.myer at gmail.com Tue Jul 14 15:20:11 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 12:20:11 -0700 (PDT) Subject: bad behaviour in interactive Python prompt Message-ID: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> Hi, I'm experiencing a strange behaviour of the Python prompt when using the four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of getting the previous and next command respectively I get ugly characters. See it yourself: http://tinypic.com/view.php?pic=m78cgp&s=3 This is not directly Python-specific, but you feel quite handicapped if you must rewrite each command again... so my obvious question is: How can I fix this? Please, let me know if you can tell me something. Additional information: I'm running Ubuntu Linux I've tried the python prompt in several shell environments and got the same issue in csh, dash... all negative. Cheers, Kenny From dickinsm at gmail.com Tue Jul 14 15:26:19 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 14 Jul 2009 12:26:19 -0700 (PDT) Subject: bad behaviour in interactive Python prompt References: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> Message-ID: <722961de-8416-4aff-a1e2-ce0e18c65503@k1g2000yqf.googlegroups.com> On Jul 14, 8:20?pm, "~km" wrote: > I'm experiencing a strange behaviour of the Python prompt when using > the > four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of > getting > the previous and next command respectively I get ugly characters. See > it > yourself:http://tinypic.com/view.php?pic=m78cgp&s=3 > > This is not directly Python-specific, but you feel quite handicapped > if > you must rewrite each command again... so my obvious question is: > > How can I fix this? Please, let me know if you can tell me something. > > Additional information: > I'm running Ubuntu Linux > I've tried the python prompt in several shell environments and got the > same issue in csh, dash... all negative. Where did your version of Python 2.6 come from? If you built your copy of Python 2.6 from source, then the problem is probably that either the readline library is missing, or (much more likely) the include files for the readline library are missing. Look for a package called something like libreadline5-dev or readline-devel and install it, and then try rebuilding Python. Mark From degibb at gmail.com Tue Jul 14 15:26:29 2009 From: degibb at gmail.com (David Gibb) Date: Tue, 14 Jul 2009 15:26:29 -0400 Subject: select lines in python In-Reply-To: References: Message-ID: I think what Grant is saying is that you should read the documentation for the re module. David On Tue, Jul 14, 2009 at 3:12 PM, Grant Edwards wrote: > On 2009-07-14, amrita at iisermohali.ac.in wrote: > >> Can i become more precise like instead of printing all lines >> for PHE and ASP is it possible that for PHE python will print >> only those lines which will have information about H and HA >> and for ASP it will print those lines which will have >> information about HA and HB? > > Yes. > > -- > Grant Edwards ? ? ? ? ? ? ? ? ? grante ? ? ? ? ? ? Yow! I KAISER ROLL?! > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?at ? ? ? ? ? ? ? What good is a Kaiser Roll > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? visi.com ? ? ? ? ? ?without a little COLE SLAW > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? on the SIDE? > -- > http://mail.python.org/mailman/listinfo/python-list > From clp2 at rebertia.com Tue Jul 14 15:43:42 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 12:43:42 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> On Tue, Jul 14, 2009 at 11:47 AM, Mark Dickinson wrote: > On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" > wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to >> have an 'xor' operator as well. > > Hmm. ?I don't think 'nice' is sufficient. ?You'd need to make the case > that it's sufficiently useful to justify adding a new keyword 'xor' to > the language; ?I suspect that would be an uphill struggle. :) > > I'll just note that: > > (1) It's easy to emulate xor: ?'x xor y' <-> bool(x) != bool(y) Using the xor bitwise operator is also an option: bool(x) ^ bool(y) Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Tue Jul 14 15:50:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 12:50:13 -0700 Subject: http://tinypic.com/view.php?pic=m78cgp&s=3 In-Reply-To: References: Message-ID: <50697b2c0907141250k6f44fc8buda78c22a0a1989b@mail.gmail.com> On Tue, Jul 14, 2009 at 12:15 PM, ~km wrote: > Hi, > > I'm experiencing a strange behaviour of the Python prompt when using > the > four arrow keys ( not the VIM' nor Emacs' ones ;-) ). Instead of > getting > the previous and next command respectively I get ugly characters. See > it > yourself: > http://tinypic.com/view.php?pic=m78cgp&s=3 > > This is not directly Python-specific, but you feel quite handicapped > if > you must rewrite each command again... so my obvious question is: > > How can I fix this? Please, let me know if you can tell me something. I would guess that your Python wasn't compiled with GNU readline support. Do you get an error if you `import readline`? Cheers, Chris -- http://blog.rebertia.com From dickinsm at gmail.com Tue Jul 14 15:52:34 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 14 Jul 2009 12:52:34 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Jul 14, 8:43?pm, Chris Rebert wrote: > On Tue, Jul 14, 2009 at 11:47 AM, Mark Dickinson wrote: > > (1) It's easy to emulate xor: ?'x xor y' <-> bool(x) != bool(y) > > Using the xor bitwise operator is also an option: > bool(x) ^ bool(y) Good point. For some reason I expected bitwise operations on bools to return ints rather than bools. Now I know better. :-) Thanks, Mark From pfeldman at verizon.net Tue Jul 14 15:56:02 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Tue, 14 Jul 2009 12:56:02 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: <24486661.post@talk.nabble.com> != does do what I want, except that it doesn't indicate to someone reading the code that the operands are being treated as logicals. (Readability is supposed to be one of the major selling points of Python). But, this is probably good enough. Here's a related issue: I would like to see an option for type checking on operands of logical operators, so that attempting to apply a logical operator to non-Boolean entities generates a warning message. With operand type checking, 'xor' and != would be different. Mark Dickinson wrote: > > On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" > wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice >> to >> have an 'xor' operator as well. > > Hmm. I don't think 'nice' is sufficient. You'd need to make the case > that it's sufficiently useful to justify adding a new keyword 'xor' to > the language; I suspect that would be an uphill struggle. :) > > I'll just note that: > > (1) It's easy to emulate xor: 'x xor y' <-> bool(x) != bool(y) > > (2) 'and' and 'or' are special in that they have useful short- > circuiting behaviour; xor doesn't have this property (that is, you > always need to evaluate *both* operands to determine the result). > > I'd also guess that 'xor' would be much less used than 'and' or 'or', > but maybe that's just a reflection of the sort of code that I tend to > write. > > -- > Mark > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24486661.html Sent from the Python - python-list mailing list archive at Nabble.com. From jgardner at jonathangardner.net Tue Jul 14 16:05:48 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Tue, 14 Jul 2009 13:05:48 -0700 (PDT) Subject: one-time factory in python for an experienced java guy References: Message-ID: On Jul 14, 7:03?am, phonky wrote: > > Now, I do not know yet how the account number scheme looks like. Exactly. The data store knows a lot more than the client (your program) will ever know. The correct answer is to do nothing. Use your data store to generate the IDs for you. The implementations discussed here will not generate unique IDs across invocations, but the data store will persist the sequence for you appropriately. The more correct answer is to abstract away the client to your data store as well. See SQLAlchemy. If you're writing a data store, you're doing it wrong. See PostgreSQL, MySQL, or any other data store out there that are perfectly fine for development and production use. I like to use UUIDs for the IDs. Others like big ints that are a sequence. I've seen people use encrypted big ints, basically random strings that aren't really random. In the end, you only have to change the code that talks to the data store, and the rest of your program only cares about the equality of the id. From clp2 at rebertia.com Tue Jul 14 16:22:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 13:22:20 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <24486661.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: <50697b2c0907141322y3ec27688g4e6bc29d8fc4b7bf@mail.gmail.com> > Mark Dickinson wrote: >> >> On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" >> wrote: >>> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice >>> to >>> have an 'xor' operator as well. >> >> Hmm. ?I don't think 'nice' is sufficient. ?You'd need to make the case >> that it's sufficiently useful to justify adding a new keyword 'xor' to >> the language; ?I suspect that would be an uphill struggle. :) >> >> I'll just note that: >> >> (1) It's easy to emulate xor: ?'x xor y' <-> bool(x) != bool(y) >> >> (2) 'and' and 'or' are special in that they have useful short- >> circuiting behaviour; xor doesn't have this property (that is, you >> always need to evaluate *both* operands to determine the result). >> >> I'd also guess that 'xor' would be much less used than 'and' or 'or', >> but maybe that's just a reflection of the sort of code that I tend to >> write. On Tue, Jul 14, 2009 at 12:56 PM, Dr. Phillip M. Feldman wrote: > Here's a related issue: I would like to see an option for type checking on > operands of logical operators, so that attempting to apply a logical > operator to non-Boolean entities generates a warning message. With operand > type checking, 'xor' and != would be different. That's probably not gonna happen. Python is dynamically and duck typed, and has a sweet coercion system (__bool__ or __nonzero__ depending on your version) to coerce non-booleans to booleans in a sensible and useful way. So, it's not gonna change any time soon. Some illustrative examples: >>> #empty containers considered false >>> bool([] or {} or set() or "") >>> False >>> #zero and null considered false >>> bool(0 or None) >>> False >>> #anything else is, by default, true >>> bool(object()) >>> True And like I said, a class can override a special method to provide whatever boolean semantics are desired. It's surprisingly handy. Cheers, Chris -- http://blog.rebertia.com From jgardner at jonathangardner.net Tue Jul 14 16:24:29 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Tue, 14 Jul 2009 13:24:29 -0700 (PDT) Subject: explode() References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: <681fe49e-7063-47c5-b0c5-d5088ab216c1@g31g2000yqc.googlegroups.com> On Jul 14, 6:56?am, Fred Atkinson wrote: > > Agreed, it doesn't. ?But if my hosting provider won't change it, I'm > stuck with it. ? > Nowadays you can find hosts that allow you to run FastCGI scripts in any language for dirt cheap. (Hostmonster for instance.) If your host doesn't allow it, it's time to upgrade. Any hosting service that supports Ruby on Rails supports Python through the exact same mechanism. From aahz at pythoncraft.com Tue Jul 14 16:30:43 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 13:30:43 -0700 Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, Stefan Behnel wrote: >Deep_Feelings wrote: >> >> So you have chosen programming language "x" so shall you tell us why >> you did so , and what negatives or positives it has ? > >*duck* Where do you get the duck programming language? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From jakub.piotr.nowak at gmail.com Tue Jul 14 16:39:28 2009 From: jakub.piotr.nowak at gmail.com (Jakub P. Nowak) Date: Tue, 14 Jul 2009 13:39:28 -0700 (PDT) Subject: ANN: RuPy '09 Conference Message-ID: RuPy 2009 :: Strongly Dynamic Conference Poznan, Poland November 7-8, 2009 :: Call for speakers RuPy is a conference about dynamically typed programming languages. Held for the first time in April 2007 it gathered enthusiasts from Poland and other countries. The idea behind the conference is to liven up the community and make these technologies more popular in Europe. The first conference was a middle-sized one but came out as a huge success. We believe it was a great experience for both attendees and speakers. RuPy 2009 committee is looking for speakers willing to present a Python, Ruby, Groovy or any other related subject. If you have an interesting talk in mind go ahead and submit a talk proposal. The talk proposal should include a talk title, brief introduction and your short resume/biography. To our invited speakers we offer free accommodation, full board and possibility to interact with a very lively IT community. You are also free to participate in all the extra events - 'Geek party' always turns out great! You can also support us by linking to our site and informing all your friends about the event and the possibility to submit a talk proposal. Potential speakers should submit an abstract using the following form: https://spreadsheets.google.com/viewform?formkey=dEFEYndsQWR2TGFzRFpzcU9IbFlCT2c6MA.. by September 30th, 2009 for consideration. If you have any special presentation needs, let us know. For more details check our site: http://www.rupy.eu Best regards, -- Jakub P. Nowak RuPy Committee From suruti94 at gmail.com Tue Jul 14 16:40:38 2009 From: suruti94 at gmail.com (Mohan Parthasarathy) Date: Tue, 14 Jul 2009 13:40:38 -0700 Subject: Calling functions: Why this complicated ? Message-ID: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> Hi, I am a newbie. I am reading http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html Defining a function with "N" arguments and calling them in "M" different ways. Why does it have to be this complicated ? I like the idea of calling the function by explicitly naming the arguments, but there are so many other ways here that is very confusing. Do people really use all these features ? Perhaps, there is a equivalent book to "Javascript: Good Parts" for Python ? -thanks mohan -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Tue Jul 14 16:45:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 22:45:46 +0200 Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> <7d161f1f-2c09-42ca-9bbd-b99a777044ea@j21g2000vbn.googlegroups.com> Message-ID: >>>>> Carl Banks (CB) wrote: >CB> On Jul 14, 4:48?am, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >>> In message <93f6a517-63d8-4c80- >>> >>> bf19-4614b7099... at m7g2000prd.googlegroups.com>, Carl Banks wrote: >>> > Or would you rather let all unexpected exceptions print to standard >>> > error, which is often a black hole in non-interactive sitations? >>> >>> Since when? >>> >>> Cron, for example, collects standard error and mails it to you. >CB> 1. Cron is only one way to run programs non-interactively. (Would that >CB> work with, say, Windows services?) >CB> 2. Many systems don't run MTAs these days >CB> 3. In my experience the pipeline from cron to mail delivery is error >CB> prone, mainly due to the complexity of configuring MTAs >CB> 4. Even if all this works, you might just want to do your logging >CB> directly in Python anyway Even then, I think the program would get structured better if the FTP code would only catch FTP-specific errors, including socket errors (which is what all_errors does) and to catch programming errors etc on another level, e.g by the mentioned sys.excepthook. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From deets at nospam.web.de Tue Jul 14 16:59:04 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 14 Jul 2009 22:59:04 +0200 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: References: Message-ID: <7c49soF25pgh4U1@mid.uni-berlin.de> weafon schrieb: > Hi guys, > > I have a question about the usage of yield. As shown in the below > example, in general, if there is a code segment commonly used by two or > more functions, we may isolate the segment into a function and then call > it from other functions if necessary. > > def func1(): > .... > while(cond): > ..... > commoncode() > ... > > > def func2(): > .... > while(cond): > ..... > commoncode() > ... > > def commoncode() > AAAA > BBBB > CCCC > > However, if there is a 'yield' operation in the common code segment, the > isolation causes that func1 and func2 become a non-generator function!! No. Not writing them as generators makes them a non-generator-function. You are way to unspecific with your examples. But if func1 and func2 are themselves supposed to be generators, there is nothing preventing you from using them as such. Diez From robert.kern at gmail.com Tue Jul 14 16:59:32 2009 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 14 Jul 2009 15:59:32 -0500 Subject: missing 'xor' Boolean operator In-Reply-To: <24486661.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: > != does do what I want, except that it doesn't indicate to someone reading > the code that the operands are being treated as logicals. (Readability is > supposed to be one of the major selling points of Python). But, this is > probably good enough. In the words of those greater than myself, "Not every one-liner needs to be in the standard library." def xor(a, b): return bool(a) != bool(b) -- 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 piet at cs.uu.nl Tue Jul 14 16:59:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 14 Jul 2009 22:59:34 +0200 Subject: The meaning of "=" References: Message-ID: >>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >A> In article , Piet van Oostrum >A> wrote: >>> And to get c.x = 4 working you also need a __setitem__. >A> Nope. You do need __setitem__ so that this works: >A> c['x'] = 4 Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because that was what the OP wanted (I think). -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From db3l.net at gmail.com Tue Jul 14 17:06:03 2009 From: db3l.net at gmail.com (David Bolen) Date: Tue, 14 Jul 2009 17:06:03 -0400 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: John Nagle writes: > Python 3 enforces the rule that you can't mix tabs and spaces > for indentation in the same file. That (finally) guarantees that > the indentation you see is what the Python parser sees. That's > enough to prevent non-visible indentation errors. Are you sure? It seems to restrict them in the same block, but not in the entire file. At least I was able to use both space and tab indented blocks in the same file with Python 3.0 and 3.1. I suspect precluding any mixture at all at the file level would be more intrusive, for example, when trying to combine multiple code sources in a single file. Not that this really changes your final point, since the major risk of a mismatch between the parser vs. visual display is within a single block. > It also means that the Python parser no longer has to have > any concept of how many spaces equal a tab. So the problem > is now essentially solved. "has to have" being a future possibility at this point, since I'm fairly sure the 3.x parser does technically still have the concept of a tab size of 8, though now it can be an internal implementation detail. -- David From ethan at stoneleaf.us Tue Jul 14 17:38:12 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 14:38:12 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: <4A5CFAC4.7020300@stoneleaf.us> Robert Kern wrote: > On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: > >> != does do what I want, except that it doesn't indicate to someone >> reading >> the code that the operands are being treated as logicals. >> (Readability is >> supposed to be one of the major selling points of Python). But, this is >> probably good enough. > > > In the words of those greater than myself, "Not every one-liner needs to > be in the standard library." > > def xor(a, b): > return bool(a) != bool(b) > Let's see... and returns the last object that is "true" or returns the first object that is "true" so should xor return the only object that is "true", else False/None? def xor(a, b) if a and b: return None elif a: return a elif b: return b else: return None ~Ethan~ From nobody at nowhere.com Tue Jul 14 17:40:27 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 14 Jul 2009 22:40:27 +0100 Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 02:06:04 -0300, Gabriel Genellina wrote: >> Matt, how many words are you looking for, in how long a string ? >> Were you able to time any( substr in long_string ) against re.compile >> ( "|".join( list_items )) ? > > There is a known algorithm to solve specifically this problem > (Aho-Corasick), a good implementation should perform better than R.E. (and > better than the gen.expr. with the advantage of returning WHICH string > matched) Aho-Corasick has the advantage of being linear in the length of the patterns, so the setup may be faster than re.compile(). The actual searching won't necessarily be any faster (assuming optimal implementations; I don't know how safe that assumption is). From ken at seehart.com Tue Jul 14 17:44:51 2009 From: ken at seehart.com (Ken Seehart) Date: Tue, 14 Jul 2009 14:44:51 -0700 Subject: decorators - would be nice if... Message-ID: <4A5CFC53.7050902@seehart.com> Almost every time I use decorators, I find myself wishing I had access to the local namespace of the context from which the decorator is executed. In practice, decorator is being applied to a method, so the namespace in question would be the dictionary of the class being created. Similarly, before decorators were around, I often found myself lamenting the fact that a class is not created until after it's scope is completed, since this makes certain kinds of metaprogramming more difficult. But after digging deeper, I realized why it is the way it is, so I don't complain about that. But it would have been a simple thing to add an argument 'locals' to the decorator specification. It's somewhat more difficult to add that now because of compatibility issues, but perhaps there is a way to get the same effect. I can work around all of this by making my decorators just store data to be processed by a metaclass, but that solution is less elegant. Also, it has the slight disadvantage of requiring the use of a metaclass which would otherwise not be necessary. I prefer to save metaclass implementation as a last resort for various reasons (e.g. there does not seem to be a good way to use multiple metaclasses in a class hierarchy, and more generally you can't stack them on top of each other (you can only have one metaclass per class)). Thoughts? - Ken From knny.myer at gmail.com Tue Jul 14 17:47:04 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 14:47:04 -0700 (PDT) Subject: bad behaviour in interactive Python prompt References: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> <722961de-8416-4aff-a1e2-ce0e18c65503@k1g2000yqf.googlegroups.com> Message-ID: <21a14a9e-2837-4ae1-a55b-c6b3d50c0513@k19g2000yqn.googlegroups.com> On 14 Jul., 15:26, Mark Dickinson wrote: > Where did your version of Python 2.6 come from? > > If you built your copy of Python 2.6 from source, then the problem is > probably that either the readline library is missing, or (much more > likely) the include files for the readline library are missing. ?Look > for a package called something like libreadline5-dev or readline-devel > and install it, and then try rebuilding Python. > > Mark Yes, I built it from source and never thought that there might be such a library. Now I've re-compiled and my problem is solved! Have many thanks Mark, and Chris Rebert, too, who responded to my deleted post, which I've received per mail. Sorry for that incidence. Cheers, Kenny From knny.myer at gmail.com Tue Jul 14 17:47:17 2009 From: knny.myer at gmail.com (~km) Date: Tue, 14 Jul 2009 14:47:17 -0700 (PDT) Subject: bad behaviour in interactive Python prompt References: <0ba40baf-f6fb-4d5f-b2dc-b75cc4e38fb9@f16g2000vbf.googlegroups.com> <722961de-8416-4aff-a1e2-ce0e18c65503@k1g2000yqf.googlegroups.com> Message-ID: On 14 Jul., 15:26, Mark Dickinson wrote: > Where did your version of Python 2.6 come from? > > If you built your copy of Python 2.6 from source, then the problem is > probably that either the readline library is missing, or (much more > likely) the include files for the readline library are missing. ?Look > for a package called something like libreadline5-dev or readline-devel > and install it, and then try rebuilding Python. > > Mark Yes, I built it from source and never thought that there might be such a library. Now I've re-compiled and my problem is solved! Have many thanks Mark, and Chris Rebert, too, who responded to my deleted post, which I've received per mail. Sorry for that incidence. Cheers, Kenny From python at mrabarnett.plus.com Tue Jul 14 17:58:27 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 14 Jul 2009 22:58:27 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5CFAC4.7020300@stoneleaf.us> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> Message-ID: <4A5CFF83.9070301@mrabarnett.plus.com> Ethan Furman wrote: > Robert Kern wrote: >> On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: >> >>> != does do what I want, except that it doesn't indicate to someone >>> reading >>> the code that the operands are being treated as logicals. >>> (Readability is >>> supposed to be one of the major selling points of Python). But, this is >>> probably good enough. >> >> >> In the words of those greater than myself, "Not every one-liner needs >> to be in the standard library." >> >> def xor(a, b): >> return bool(a) != bool(b) >> > > Let's see... > > and returns the last object that is "true" > or returns the first object that is "true" > > so should xor return the only object that is "true", else False/None? > > def xor(a, b) > if a and b: > return None > elif a: > return a > elif b: > return b > else: > return None > How about: def xor(a, b): return not b and a or not a and b From nobody at nowhere.com Tue Jul 14 17:58:28 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 14 Jul 2009 22:58:28 +0100 Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> <7xab37t843.fsf@ruckus.brouhaha.com> Message-ID: On Tue, 14 Jul 2009 11:47:08 -0700, Paul Rubin wrote: >> - unlimited precision integers >> - easy to program >> - IDE not required >> - reasonable speed >> - math library needs to include number theoretic functions >> like GCD, LCM, Modular Inverse, etc. >> - not fucking retarded like F# > > Have you looked at Haskell? Given his comment about F#, I have a suspicion that he might be dogmatically opposed to functional languages generally. From deets at nospam.web.de Tue Jul 14 18:03:03 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 00:03:03 +0200 Subject: decorators - would be nice if... In-Reply-To: References: Message-ID: <7c4dknF253b3tU1@mid.uni-berlin.de> Ken Seehart schrieb: > Almost every time I use decorators, I find myself wishing I had access > to the local namespace of the context from which the decorator is > executed. In practice, decorator is being applied to a method, so the > namespace in question would be the dictionary of the class being created. You can access the instance. def decorator(method): def _d(self, *args, **kwargs): print self.__dict__ return method(self, *args, **kwargs) return _d class Foo(object): @decorator def bar(self, a, b): print "bar" f = Foo() f.bar(1, 2) So what exactly it is you are missing? The method's locals()? And could you explain *why* you are missing this? Diez From ethan at stoneleaf.us Tue Jul 14 18:11:40 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 15:11:40 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5CFF83.9070301@mrabarnett.plus.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> Message-ID: <4A5D029C.5030700@stoneleaf.us> MRAB wrote: > Ethan Furman wrote: > >> Robert Kern wrote: >> >>> On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: >>> >>>> != does do what I want, except that it doesn't indicate to someone >>>> reading >>>> the code that the operands are being treated as logicals. >>>> (Readability is >>>> supposed to be one of the major selling points of Python). But, >>>> this is >>>> probably good enough. >>> >>> >>> >>> In the words of those greater than myself, "Not every one-liner needs >>> to be in the standard library." >>> >>> def xor(a, b): >>> return bool(a) != bool(b) >>> >> >> Let's see... >> >> and returns the last object that is "true" >> or returns the first object that is "true" >> >> so should xor return the only object that is "true", else False/None? >> >> def xor(a, b) >> if a and b: >> return None >> elif a: >> return a >> elif b: >> return b >> else: >> return None >> > How about: > > def xor(a, b): > return not b and a or not a and b In [12]: not 1 and 0 or 1 and not 0 Out[12]: True In [13]: not 0 and 0 or 0 and not 0 Out[13]: 0 In [14]: not 1 and 1 or 1 and not 1 Out[14]: False In [15]: not [] and [] or [] and not [] Out[15]: [] Doesn't produce consistent objects, sometimes bool, sometimes something else. 'Course, it all depends on what you need. ~Ethan~ From pavlovevidence at gmail.com Tue Jul 14 18:26:06 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 14 Jul 2009 15:26:06 -0700 (PDT) Subject: decorators - would be nice if... References: Message-ID: On Jul 14, 2:44?pm, Ken Seehart wrote: [snip] (e.g. there does not > seem to be a good way to use multiple metaclasses in a class hierarchy, > and more generally you can't stack them on top of each other (you can > only have one metaclass per class)). > > Thoughts? If "stacking" metaclasses (whatever that means) is a possible approach to your problem, you might want to ask yourself if you're trying to be too clever. Would you like to give us an example of a decorator you think would benefit from having access to local namespace? We might be able to suggest other possibilities. Carl Banks From darkwater42 at gmail.com Tue Jul 14 18:54:31 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 15:54:31 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <4a5c5e7f$0$30235$9b4e6d93@newsspool1.arcor-online.net> Message-ID: On Jul 14, 7:38?am, Florian Brucker wrote: > Douglas Alan wrote: > > Thank you. My question wasn't intended to be Python specific, though. > > I am just curious for purely academic reasons about whether there is > > such an algorithm. All the sources I've skimmed only seem to the > > answer the question via omission. Which is kind of strange, since it > > seems to me like an obvious question to ask. > IIRC comp.programming would be the place to ask such questions. Thanks, yes that does look like a good place to post such questions. Unfortunately, it also looks to be overrun with stories on "hot girls top and bottom sexy movie", though I'm sure I can ignore those. I'm scared to look at the posting on "tricky bit twiddling", though. |>ouglas From aahz at pythoncraft.com Tue Jul 14 18:58:06 2009 From: aahz at pythoncraft.com (Aahz) Date: 14 Jul 2009 15:58:06 -0700 Subject: The meaning of "=" References: Message-ID: In article , Piet van Oostrum wrote: >>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: > >>A> In article , Piet van Oostrum >>A> wrote: > >>>> And to get c.x = 4 working you also need a __setitem__. > >>A> Nope. You do need __setitem__ so that this works: > >>A> c['x'] = 4 > >Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because >that was what the OP wanted (I think). c.x = 4 already updates the instance dict, so there's no need to change any class methods to support it. That is, IME it's much better to add methods to a regular class to make it more dict-like using the built-in instance dict rather than changing any of the attribute mechanisms. If you're really curious, I recommend trying several approaches yourself to see what works better. ;-) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From Scott.Daniels at Acm.Org Tue Jul 14 19:00:03 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 16:00:03 -0700 Subject: How to check if any item from a list of strings is in a big string? In-Reply-To: References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: <3cCdna9IneyskcDXnZ2dnUVZ_g6dnZ2d@pdx.net> Nobody wrote: > On Tue, 14 Jul 2009 02:06:04 -0300, Gabriel Genellina wrote: > >>> Matt, how many words are you looking for, in how long a string ? >>> Were you able to time any( substr in long_string ) against re.compile >>> ( "|".join( list_items )) ? >> There is a known algorithm to solve specifically this problem >> (Aho-Corasick), a good implementation should perform better than R.E. (and >> better than the gen.expr. with the advantage of returning WHICH string >> matched) > > Aho-Corasick has the advantage of being linear in the length of the > patterns, so the setup may be faster than re.compile(). The actual > searching won't necessarily be any faster (assuming optimal > implementations; I don't know how safe that assumption is). > Having done a fast Aho-Corasick implementation myself, I can assure you that the actual searching can be incredibly fast. RE conversion usually goes to a slightly more general machine than the Aho-Corasick processing requires. --Scott David Daniels Scott.Daniels at Acm.Org From dstanek at dstanek.com Tue Jul 14 19:03:02 2009 From: dstanek at dstanek.com (David Stanek) Date: Tue, 14 Jul 2009 19:03:02 -0400 Subject: How to unbuffer Python's output In-Reply-To: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> References: <082AEC0F05C496449D86020DBFE466156F7C0BF365@ADSK-NAMSG-02.MGDADSK.autodesk.com> Message-ID: 2009/7/14 Lily Gao : > Hi, All > > I am calling a python program in perl and use redirection, > > Like : > > `python x.py > 1.log 2>&1` Try tihs instead: python x.py 2>&1 > 1.log -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From freyr.magnusson at gmail.com Tue Jul 14 19:07:09 2009 From: freyr.magnusson at gmail.com (Freyr) Date: Tue, 14 Jul 2009 16:07:09 -0700 (PDT) Subject: Passing handlers between bound c++ libs Message-ID: <3e7b0717-a665-4f42-b7bd-7a99d59258c4@m11g2000yqh.googlegroups.com> I have a python bound physics library that uses handler to process events. The passing of handlers between c++ and python causes a huge overhead slowing down the process. Can I implement a handler in my custom python bound c++ lib B and pass it to blackbox python bound c++ lib A so that A would call B directly without passing through the python layer? Or to phrase the question differently: Can I pass a handler from B through python to A so that A will invoke B with out calling into python code? From lists at cheimes.de Tue Jul 14 19:08:24 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 15 Jul 2009 01:08:24 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: Chris Rebert wrote: > Using the xor bitwise operator is also an option: > bool(x) ^ bool(y) I prefer something like: bool(a) + bool(b) == 1 It works even for multiple tests (super xor): if bool(a) + bool(b) + bool(c) + bool(d) != 1: raise ValueError("Exactly one of a, b, c and d must be true") Christian From darkwater42 at gmail.com Tue Jul 14 19:08:36 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 16:08:36 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: <95f25380-d71f-41b6-82b8-f991551e8a1a@32g2000yqj.googlegroups.com> On Jul 14, 8:10?am, Piet van Oostrum wrote: > Of course you can take any BST algorithm and replace pointers by indices > in the array and allocate new elements in the array. But then you need > array elements to contain the indices for the children explicitely. And why is this a problem? This is how binary heaps are typically implemented, and it all works swimmingly. The node rotations for keeping a binary heap balanced turn out to be suitable for representation in a flat array. I.e., when you do the node rotations, you only ever have to copy log n array elements. In general, however, you can't move nodes around so easily when represented in a flat array. A node movement in a tree represented with pointers, might involves changing just two pointers, while when represented as a flat array, might involve copying most of the array to maintain the tree invariants. It just so turns out that maintaining the invariants for a binary heap does not have this issue. This is why I was curious about treaps, which are a type of binary heap. The CLRS textbook on algorithms discusses treaps, but doesn't ever mention whether they are as fortunate as less constrained binary heaps. I'm sure I could work out for myself whether the treap rotations are suitable for storage in a flat array, but I might make a mistake somewhere in my reasoning, and then never know the true answer! |>ouglas From Scott.Daniels at Acm.Org Tue Jul 14 19:17:35 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 16:17:35 -0700 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: Aahz wrote: > In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, > Stefan Behnel wrote: >> Deep_Feelings wrote: >>> So you have chosen programming language "x" so shall you tell us why >>> you did so , and what negatives or positives it has ? >> *duck* > > Where do you get the duck programming language? It shares a type system with Python, of course. :-) --Scott David Daniels Scott.Daniels at Acm.Org From jackdied at gmail.com Tue Jul 14 19:23:31 2009 From: jackdied at gmail.com (Jack Diederich) Date: Tue, 14 Jul 2009 19:23:31 -0400 Subject: decorators - would be nice if... In-Reply-To: <4A5CFC53.7050902@seehart.com> References: <4A5CFC53.7050902@seehart.com> Message-ID: On Tue, Jul 14, 2009 at 5:44 PM, Ken Seehart wrote: > Almost every time I use decorators, I find myself wishing I had access > to the local namespace of the context from which the decorator is > executed. ?In practice, decorator is being applied to a method, so the > namespace in question would be the dictionary of the class being created. As other mentioned, an example would make it clearer what you are trying to do. > Similarly, before decorators were around, I often found myself lamenting > the fact that a class is not created until after it's scope is > completed, since this makes certain kinds of metaprogramming more > difficult. ?But after digging deeper, I realized why it is the way it > is, so I don't complain about that. Py3k to the rescue! Because the metaclass is defined outside of the class body it is possible to pass in a metaclass that uses a custom dictionary. This was added to make some common cases easier (like knowing in which order members were defined). This is not backportable to 2.6 because the __metaclass__ is defined inside the class body and possibly anywhere in the class body. -Jack From darkwater42 at gmail.com Tue Jul 14 19:23:58 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 16:23:58 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> Message-ID: <5e8d271a-e3e7-451b-92ca-b5321da9b706@o6g2000yqj.googlegroups.com> On Jul 14, 9:19?am, Scott David Daniels wrote: > It may well be that there is no good simple solution, and people avoid > writing about non-existent algorithms. I can't imagine why that should be the case. The CLRS textbook on algorithms, for instance, goes to some pains to mathematically prove that there is no comparison sort that can operate in faster than O(n log n) time. And any decent discussion of rabies would be sure to mention that there is no known cure. CLRS talks about binary heaps, binary search trees, and treaps, and it shows how to maintain a binary heap in a flat array efficiently (n log n time overall), but it never even seems to bring up the subject as to whether a binary search tree or a treap can also be efficiently maintained in a flat array. Though it may be the case that these questions are left as exercises for the student, and therefore buried in the exercises and problem sets that I didn't read carefully. > Piet van Oostrum wrote: > > Of course you can take any BST algorithm and replace pointers by indices > > in the array and allocate new elements in the array. But then you need > > array elements to contain the indices for the children explicitely. > And you loower your locality of reference (cache-friendliness). > Note the insert in Python, for example, is quite cache-friendly. I can't see that a binary search tree would typically have particularly good cache-friendliness, so I can't see why a flat-array representation, such as is done for a binary heap, would have particularly worse cache-reference. |>ouglas From clp2 at rebertia.com Tue Jul 14 19:31:25 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 16:31:25 -0700 Subject: Calling functions: Why this complicated ? In-Reply-To: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> Message-ID: <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> On Tue, Jul 14, 2009 at 1:40 PM, Mohan Parthasarathy wrote: > Hi, > I am a newbie. I am reading > http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html > Defining a function with "N" arguments and calling them in "M" different > ways. Why does it have to be this complicated ? I like the idea of calling > the function by explicitly naming the arguments, but there are so many other > ways here that is very confusing. Do people really use all these features ? > Perhaps, there is a equivalent book to "Javascript: Good Parts" for Python ? Oh $DEITY, don't even compare Python to JavaScript. At least in Python, when you try to access a non-existent attribute, a proper NameError exception is thrown rather than silently getting back "undefined"... (*has traumatic horror story flashback*) The calling syntax is not so much complicated as it is liberating. Are you a C junkie who has never heard of named arguments? Just use the call sequence like you've always done. Are you a exacting Smalltalk or Objective-C person who likes to name all the arguments all the time for clarity? You can do that. Do you want to call a function with lots of default arguments but only want to override a couple of them? Specifying them by name lets you do that succinctly. Do you not want to have to worry about the order of the parameters because it seems kinda arbitrary? Then specify the arguments by name. etc... And if you try and specify an argument twice, Python will throw an error, so anything truly confusing will get caught right away. And there's only one definition syntax, so while the call could be complicated, figuring out what it means by looking to the definition is fairly easy. There really aren't that many ways it's done in practice. In practice, the following styles cover 90% of cases: - All named arguments: foo(bar=a, baz=b, qux=c) - All sequential arguments: foo(a, b, c) - All sequential arguments, with a few optional arguments given by name: foo(a, b, c, flag=True, style=qux) - Simple pass-through: foo(*args, **kwargs) Granted, there's 4 of them, but each taken by itself seems pretty easy to read, IMHO. Cheers, Chris -- http://blog.rebertia.com From darkwater42 at gmail.com Tue Jul 14 19:33:33 2009 From: darkwater42 at gmail.com (Douglas Alan) Date: Tue, 14 Jul 2009 16:33:33 -0700 (PDT) Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <95f25380-d71f-41b6-82b8-f991551e8a1a@32g2000yqj.googlegroups.com> Message-ID: <01ea9a41-5885-4df3-91ae-24cfa5bfbec4@y17g2000yqn.googlegroups.com> I wrote: > On Jul 14, 8:10?am, Piet van Oostrum wrote: > > > Of course you can take any BST algorithm and replace pointers by indices > > in the array and allocate new elements in the array. But then you need > > array elements to contain the indices for the children explicitely. > And why is this a problem? Oh, I'm sorry -- I see what you are saying now. You're saying you can just implement a normal binary search tree, but store the tree nodes in an array, as if it were a chunk of memory, and use array indices as pointers, rather than using memory addresses as pointers. Fair enough, but I'm not sure what that would buy you. Other than, perhaps some improved locality of reference, and the potential to perhaps get the pointers take up less space if you know the array is never going to grow to be very large. |>ouglas From Scott.Daniels at Acm.Org Tue Jul 14 19:36:35 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 14 Jul 2009 16:36:35 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> Message-ID: <06edna4zh5xcicDXnZ2dnUVZ_tKdnZ2d@pdx.net> Ethan Furman wrote: > and returns the last object that is "true" A little suspect this. _and_ returns the first object that is not "true," or the last object. > or returns the first object that is "true" Similarly: _or_ returns the first object that is "true," or the last object. > so should xor return the only object that is "true", else False/None? Xor has the problem that in two cases it can return neither of its args. Not has behavior similar in those cases, and we see it returns False or True. The Pythonic solution is therefore to use False. > def xor(a, b) > if a and b: > return None > elif a: > return a > elif b: > return b > else: > return None def xor(a, b): if bool(a) == bool(b): return False else: return a or b Side-effect counting in applications of bool(x) is ignored here. If minimizing side-effects is needed: def xor(a, b): if a: if not b: return a elif b: return b return False --Scott David Daniels Scott.Daniels at Acm.Org From ethan at stoneleaf.us Tue Jul 14 19:48:26 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 16:48:26 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <4A5D194A.6050301@stoneleaf.us> Christian Heimes wrote: > Chris Rebert wrote: > >>Using the xor bitwise operator is also an option: >>bool(x) ^ bool(y) > > > I prefer something like: > > bool(a) + bool(b) == 1 > > It works even for multiple tests (super xor): > > if bool(a) + bool(b) + bool(c) + bool(d) != 1: > raise ValueError("Exactly one of a, b, c and d must be true") > > Christian > super_xor! I like it! In [23]: def super_xor(args, failure=False): ....: found_one = False ....: for item in args: ....: if item: ....: if found_one: ....: return failure ....: else: ....: found_one = item ....: return found_one or failure In [25]: super_xor((0, 1, 0, [])) Out[25]: 1 In [26]: super_xor((0, 1, 0, [],('this',))) Out[26]: False In [27]: super_xor((0, {}, 0, [],())) Out[27]: False In [16]: def super_or(args, failure=False): ....: for item in args: ....: if item: ....: return item ....: else: ....: return failure ....: In [17]: super_or((None, [], 0, 3, {})) Out[17]: 3 In [18]: super_or((None, [], 0, (), {})) Out[18]: False In [19]: def super_and(args, failure=False): ....: for item in args: ....: if not item: ....: return failure ....: else: ....: return item ....: In [20]: super_and((1, 2, 3)) Out[20]: 3 In [21]: super_and((1, 2, 3, 4)) Out[21]: 4 In [22]: super_and((1, 0, 3, 4)) Out[22]: False A whole family of supers. :) ~Ethan~ From ethan at stoneleaf.us Tue Jul 14 19:53:37 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 14 Jul 2009 16:53:37 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <06edna4zh5xcicDXnZ2dnUVZ_tKdnZ2d@pdx.net> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <06edna4zh5xcicDXnZ2dnUVZ_tKdnZ2d@pdx.net> Message-ID: <4A5D1A81.5000406@stoneleaf.us> Scott David Daniels wrote: > Ethan Furman wrote: > >> and returns the last object that is "true" > > A little suspect this. > _and_ returns the first object that is not "true," or the last object. > >> or returns the first object that is "true" > > Similarly: > _or_ returns the first object that is "true," or the last object. > Thanks for the correction. ~Ethan~ > > --Scott David Daniels > Scott.Daniels at Acm.Org From magawake at gmail.com Tue Jul 14 20:11:49 2009 From: magawake at gmail.com (Mag Gam) Date: Tue, 14 Jul 2009 20:11:49 -0400 Subject: compiling python Message-ID: <1cbd6f830907141711s4b6dbbe6gd563a3e63fc53b4@mail.gmail.com> At my university we are trying to compile python with --enable-shared however when I do a make many things fail. Is it a good idea to compile python with shared libraries? It seems mod-python needs it this way. TIA From keith at nekotaku.com Tue Jul 14 20:29:43 2009 From: keith at nekotaku.com (Alagalah) Date: Tue, 14 Jul 2009 17:29:43 -0700 (PDT) Subject: Reading floats from Excel using COM Message-ID: <04a50999-3934-4235-b047-5135d8c71c94@m11g2000yqh.googlegroups.com> Hi, I am using the Range function to return a tuple from Excel. The data in the range (nREVENUE) in the excel file is 100.0, 101.0, 102.0, 103.0, 104.0 I can successfully iterate across the tuple and list, but when I try and cast to a float to do some math, I get: File "C:\Python25\lib\site-packages\win32com\client\__init__.py", line 454, in __getattr__ raise AttributeError, "'%s' object has no attribute '%s'" % (repr (self), attr) AttributeError: '' object has no attribute '__float__' **** CODE ***** import win32com.client import os excel=win32com.client.Dispatch("Excel.Application") excel.Visible=0 excel.DisplayAlerts=False #This is the file that contains the macro xlsname=os.path.join(os.getcwd(),"nametest.xlsx") nametest=excel.Workbooks.Open(xlsname) revenue_list=excel.Range("nREVENUE") revenue=[] revenue.extend(revenue_list) for i in range(len(revenue)): rev=revenue[i] print float(rev) excel.Quit() del excel **************** I need to use COM as I will eventually need formula support, otherwise I would use xlutils. Any advice on how to cast the elements to a float would be greatly appreciated! From greg at cosc.canterbury.ac.nz Tue Jul 14 20:32:25 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 15 Jul 2009 12:32:25 +1200 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <7c4mb4F261slsU1@mid.individual.net> Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and what negatives or positives it has ? This summarises my reasons for choosing Python fairly well: http://www1.american.edu/cas/econ/faculty/isaac/choose_python.pdf -- Greg From suruti94 at gmail.com Tue Jul 14 20:42:55 2009 From: suruti94 at gmail.com (Mohan Parthasarathy) Date: Tue, 14 Jul 2009 17:42:55 -0700 Subject: Calling functions: Why this complicated ? In-Reply-To: <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> Message-ID: <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> Chris, Thanks for your clarifications > > I am a newbie. I am reading > > http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html > > Defining a function with "N" arguments and calling them in "M" different > > ways. Why does it have to be this complicated ? I like the idea of > calling > > the function by explicitly naming the arguments, but there are so many > other > > ways here that is very confusing. Do people really use all these features > ? > > Perhaps, there is a equivalent book to "Javascript: Good Parts" for > Python ? > > Oh $DEITY, don't even compare Python to JavaScript. At least in > Python, when you try to access a non-existent attribute, a proper > NameError exception is thrown rather than silently getting back > "undefined"... (*has traumatic horror story flashback*) I did not try to compare python to Javascript. Just because there are ten different ways of doing certain things, not all of them may be used. Over a period of time, people tend to use certain features more and more. Javascript is a different beast where some of the features need to be avoided for writing good programs. I don't know anything about python. But it is possible that there is a subset that people use frequently which may be sufficient to write good programs. Sure, that would not help me the python interview test :-) > > The calling syntax is not so much complicated as it is liberating. > Are you a C junkie who has never heard of named arguments? > Just use the call sequence like you've always done. > Are you a exacting Smalltalk or Objective-C person who likes to name > all the arguments all the time for clarity? > You can do that. That's where I fit. I did not expect to see more than that :-) > Do you want to call a function with lots of default arguments but only > want to override a couple of them? > Specifying them by name lets you do that succinctly. > Do you not want to have to worry about the order of the parameters > because it seems kinda arbitrary? > Then specify the arguments by name. > etc... > > And if you try and specify an argument twice, Python will throw an > error, so anything truly confusing will get caught right away. > And there's only one definition syntax, so while the call could be > complicated, figuring out what it means by looking to the definition > is fairly easy. > > There really aren't that many ways it's done in practice. In practice, > the following styles cover 90% of cases: > - All named arguments: foo(bar=a, baz=b, qux=c) > - All sequential arguments: foo(a, b, c) > - All sequential arguments, with a few optional arguments given by > name: foo(a, b, c, flag=True, style=qux) - Simple pass-through: foo(*args, **kwargs) > > Granted, there's 4 of them, but each taken by itself seems pretty easy > to read, IMHO. So, all four of them above has its use cases in practice i guess. thanks mohan > > Cheers, > Chris > -- > http://blog.rebertia.com > -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Tue Jul 14 20:44:12 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 14 Jul 2009 17:44:12 -0700 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: <7c4mb4F261slsU1@mid.individual.net> References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <7c4mb4F261slsU1@mid.individual.net> Message-ID: <50697b2c0907141744q61489a81t111d4545828d2b09@mail.gmail.com> On Tue, Jul 14, 2009 at 5:32 PM, greg wrote: > Deep_Feelings wrote: >> >> So you have chosen programming language "x" so shall you tell us why >> you did so , and ?what negatives or positives it has ? > > This summarises my reasons for choosing Python > fairly well: > > http://www1.american.edu/cas/econ/faculty/isaac/choose_python.pdf +1 PDF Of The Week - Chris -- http://blog.rebertia.com From martin.hellwig at dcuktec.org Tue Jul 14 20:50:44 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Wed, 15 Jul 2009 01:50:44 +0100 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: Aahz wrote: > In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, > Stefan Behnel wrote: >> Deep_Feelings wrote: >>> So you have chosen programming language "x" so shall you tell us why >>> you did so , and what negatives or positives it has ? >> *duck* > > Where do you get the duck programming language? Probably floated around in the same C. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From sjmachin at lexicon.net Tue Jul 14 21:05:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 15 Jul 2009 01:05:16 +0000 (UTC) Subject: Reading floats from Excel using COM References: <04a50999-3934-4235-b047-5135d8c71c94@m11g2000yqh.googlegroups.com> Message-ID: Alagalah nekotaku.com> writes: > > Hi, I am using the Range function to return a tuple from Excel. [big snip] Exact duplicate of question asked by "KB" on http://groups.google.com/group/python-excel ... see my answer there. From lawson89 at gmail.com Tue Jul 14 21:34:05 2009 From: lawson89 at gmail.com (Rick Lawson) Date: Tue, 14 Jul 2009 18:34:05 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java Message-ID: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> Appreciate any help on this. I am porting an app from Java to python and need generic object pooling with hooks for object initialization / cleanup and be able to specify an object timeout. Thanks ! Rick From cmpython at gmail.com Tue Jul 14 21:52:32 2009 From: cmpython at gmail.com (Che M) Date: Tue, 14 Jul 2009 18:52:32 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> Message-ID: <7ba1ea2b-0886-4972-abb3-5dd77a95aa1e@d4g2000yqa.googlegroups.com> On Jul 14, 11:55?am, Deep_Feelings wrote: > So you have chosen programming language "x" so shall you tell us why > you did so , and ?what negatives or positives it has ? As a hobbyist--and not a real programmer*--I think I chose Python (I don't really recall now) because of things like: - There is a good community, including tutor list, etc. - I wanted something that would run cross-platform (so no Visual Basic). - Preferred something free (so no REALBasic). - I wanted a high level language. - It seemed easiest to read and learn. I like mandatory indents, for example. - There are a lot of libraries for it with good support. - I liked the Zen of Python philosophy (to the extent I understood it). - Some high profile entities were using it, so it must have something going for it. - It was new enough to be a possible improvement over older languages while old enough to be established. - Who can't like a computer language named Python? Drawbacks: - AFAIK, running multiple Python apps all show up in the Windows task manager as "python.exe" (common to all non-compiled languages?) instead of the app's name. - Distributing as an executable is not as straightforward as I might hope. - I wish Python 3 wouldn't break my 2.5 code. - If it could be quicker (compiled), that would be better. (But haven't tried psyco or Shed Skin, etc.) - I've found understanding web programming hard, but that might be just the nature of web programming and not a Python thing. - I wish wxPython had a more complete rich text editor (but overall it is great). CM From http Tue Jul 14 22:36:22 2009 From: http (Paul Rubin) Date: 14 Jul 2009 19:36:22 -0700 Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <5e8d271a-e3e7-451b-92ca-b5321da9b706@o6g2000yqj.googlegroups.com> Message-ID: <7xab36oeop.fsf@ruckus.brouhaha.com> Douglas Alan writes: > I can't see that a binary search tree would typically have > particularly good cache-friendliness, so I can't see why a flat-array > representation, such as is done for a binary heap, would have > particularly worse cache-reference. That is a good point. Maybe we should be paying more attention to cache-oblivious algorithms. http://en.wikipedia.org/wiki/Cache-oblivious_algorithm H. Prokop's masters' thesis cited in the wiki article explains the subject very well. A fair amount of work has been done on it since then, but not as much as one might expect. From davea at dejaviewphoto.com Tue Jul 14 22:51:25 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Tue, 14 Jul 2009 22:51:25 -0400 Subject: explode() In-Reply-To: <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> References: <59ji5593i6nvcs6j69bfk635ofstlgja0e@4ax.com> <6k9o55p6dv04bc3scqompmek9c8rmjdu2m@4ax.com> Message-ID: <4A5D442D.2040600@dejaviewphoto.com> > The one thing I really dislike about Python over PHP is that > Python can usually only appear in the cgi directory (unless other > arragements are made with your hosting provider or if you reconfigure > Apache on your own server if you have your own). With PHP, I can put > them in any folder on my Web site without any problem. > > Regards, > > > > > Fred > > You should talk to your hosting provider. With mine, all you need do differently for directories other than cgi-bin is to do a chmod on the file to make it executable. Have you tried that? Is it hosted on Unix or Windows? DaveA From jmcmonagle at NO.SPAM.velseis.com.au Tue Jul 14 23:06:18 2009 From: jmcmonagle at NO.SPAM.velseis.com.au (John McMonagle) Date: Wed, 15 Jul 2009 13:06:18 +1000 Subject: Tkinter / Entry widget problem In-Reply-To: References: Message-ID: <4A5D47AA.7090007@NO.SPAM.velseis.com.au> Andras Szabo wrote: > Hello. I searched the archives but couldn't find a solution to a problem > related to the Entry widget in Tkinter. > > When creating a pop-up window in an app, which contains an Entry widget, > I want this widget to contain some default string, to have all this > default string selected (as if the user had manually selected > everything), and to have the focus transferred to this widget. > > (The idea is then that if the window pops up, the user won't have to > click or press Tab any more before being able to type what is needed in > the textbox, overwriting what is written there already.) > > I thought this might be the way to go: > > entrybox=Entry(toplevel_parent_window) > entrybox.insert(0,"Some default string") > entrybox.select_range(0,END) > entrybox.focus_set() > entrybox.pack() > > But it doesn't seem to work - the focus is not transferred to the Entry > widget, and the text does not appear to be selected (even though after > this entrybox.selection_present() returns True). > > What am I doing wrong? > > andras You're probably not updating after the focus_set. Try the following: from Tkinter import * r = Tk() def click(): t = Toplevel(r) e = Entry(t) e.pack() b = Button(t, text='Close', command=t.destroy) b.pack() e.insert(0, 'Default') e.select_range(0, END) e.focus_set() r.update() b = Button(r, text='Press', command=click) b.pack() r.mainloop() Regards, John From davea at ieee.org Tue Jul 14 23:32:21 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 14 Jul 2009 23:32:21 -0400 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: <4A5CC86A.9070907@lbl.gov> References: <4A5CC86A.9070907@lbl.gov> Message-ID: <4A5D4DC5.1090202@ieee.org> weafon wrote: >
Hi guys, > > I have a question about the usage of yield. As shown in the below > example, in general, if there is a code segment commonly used by two > or more functions, we may isolate the segment into a function and then > call it from other functions if necessary. > > def func1(): > .... > while(cond): > ..... > commoncode() > ... > > > def func2(): > .... > while(cond): > ..... > commoncode() > ... > > def commoncode() > AAAA > BBBB > CCCC > > However, if there is a 'yield' operation in the common code segment, > the isolation causes that func1 and func2 become a non-generator > function!! Although I can prevent such an isolation by just > duplicating the segment in func1 and func2 to keep both of them being > generator functions, the code may become ugly and hard to maintain > particularly when coomoncode() is long. > > The problem may be resolved if I can define the commoncode() as an > inline function or marco. Unfortunately, inline and marco do not seems > to be implemented in python. Thus, how can I isolate a common segment > into a function when there are yield operations in the common segment? > > Thanks, > Weafon > You are implying there's something special or unique about yield in this. Return has the same problem, and many other flow control constructs. Also, variable definitions and scoping. So you can't just copy any old bunch of adjacent lines out of two functions, put it into a third, and call it factoring. Give us a specific example you're puzzled about, and we can try to solve it. DaveA From pfeldman at verizon.net Wed Jul 15 00:07:14 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Tue, 14 Jul 2009 21:07:14 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <4A5CFF83.9070301@mrabarnett.plus.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> Message-ID: <24491580.post@talk.nabble.com> I appreciate the effort that people have made, but I'm not impressed with any of the answers. For one thing, xor should be able to accept an arbitrary number of input arguments (not just two), and should return True if and only if the number of input arguments that evaluate to True is odd (see www.mathworld.com article on xor). Here's my code: def xor(*args): """xor accepts an arbitrary number of input arguments, returning True if and only if bool() evaluates to True for an odd number of the input arguments.""" result= False for arg in args: if bool(arg): result= not result return result MRAB-2 wrote: > > Ethan Furman wrote: >> Robert Kern wrote: >>> On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote: >>> >>>> != does do what I want, except that it doesn't indicate to someone >>>> reading >>>> the code that the operands are being treated as logicals. >>>> (Readability is >>>> supposed to be one of the major selling points of Python). But, this >>>> is >>>> probably good enough. >>> >>> >>> In the words of those greater than myself, "Not every one-liner needs >>> to be in the standard library." >>> >>> def xor(a, b): >>> return bool(a) != bool(b) >>> >> >> Let's see... >> >> and returns the last object that is "true" >> or returns the first object that is "true" >> >> so should xor return the only object that is "true", else False/None? >> >> def xor(a, b) >> if a and b: >> return None >> elif a: >> return a >> elif b: >> return b >> else: >> return None >> > How about: > > def xor(a, b): > return not b and a or not a and b > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24491580.html Sent from the Python - python-list mailing list archive at Nabble.com. From milesck at umich.edu Wed Jul 15 01:08:42 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 01:08:42 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <24491580.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> <24491580.post@talk.nabble.com> Message-ID: <9F28E417-53B9-4CFE-A880-965B85903B34@umich.edu> On Jul 15, 2009, at 12:07 AM, Dr. Phillip M. Feldman wrote: > I appreciate the effort that people have made, but I'm not impressed > with any > of the answers. For one thing, xor should be able to accept an > arbitrary > number of input arguments (not just two) You originally proposed this in the context of the existing short- circuit boolean operators. Those operators (being infix) take only two operands. > and should return True if and only > if the number of input arguments that evaluate to True is odd The existing and/or operators always return the value of one of the operands--not necessarily True or False--which is another important property, but one that can't be translated fully to xor. Given the lack of context in your original post, hopefully you'll forgive me being unimpressed by your not being impressed. :) > Here's my code: > > def xor(*args): > """xor accepts an arbitrary number of input arguments, returning > True > if and only if bool() evaluates to True for an odd number of the > input > arguments.""" > > result= False > > for arg in args: > if bool(arg): result= not result > > return result If all you want is a True or False result, I'd write it like this: import operator def xor(*args): return reduce(operator.xor, map(bool, args)) # or imap In order to make it act more like the other logical operators, I'd use MRAB's 2-argument xor as the reducer--though I can't really see the utility. def xor2(a, b): return (not b and a) or (not a and b) def xor(*args): return reduce(xor2, args) You may also find this question interesting: http://stackoverflow.com/questions/432842/ -Miles From amrita at iisermohali.ac.in Wed Jul 15 02:13:06 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Wed, 15 Jul 2009 11:43:06 +0530 (IST) Subject: one more question Message-ID: <20300.210.212.36.65.1247638386.squirrel@www.iisermohali.ac.in> Dear all, Just one more thing i want to ask that suppose i have a file like:--- 47 8 ALA H H 7.85 0.02 1 48 8 ALA HA H 2.98 0.02 1 49 8 ALA HB H 1.05 0.02 1 50 8 ALA C C 179.39 0.3 1 51 8 ALA CA C 54.67 0.3 1 52 8 ALA CB C 18.85 0.3 1 53 8 ALA N N 123.95 0.3 1 107 15 ALA H H 8.05 0.02 1 108 15 ALA HA H 4.52 0.02 1 now what i want that i will make another file in which first it will write the position of ALA lets say 8 then its name ALA and then the chemical shift value for only three atoms C,CA and CB. Means it will be someting like: 8 ALA C = 179.39 CA = 54.67 CB = 18.85 I tried but its not coming. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From mensanator at aol.com Wed Jul 15 02:21:21 2009 From: mensanator at aol.com (Mensanator) Date: Tue, 14 Jul 2009 23:21:21 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> <7xab37t843.fsf@ruckus.brouhaha.com> Message-ID: On Jul 14, 1:47?pm, Paul Rubin wrote: > Mensanator writes: > > - unlimited precision integers > > - easy to program > > - IDE not required > > - reasonable speed > > - math library needs to include number theoretic functions > > ? like GCD, LCM, Modular Inverse, etc. > > - not fucking retarded like F# > > Have you looked at Haskell? > > > As for negatives, the GMP library doesn't factor. > > Maybe with some MIRACL bindings... That's the factoring program (factor.exe from the MIRACL package) I made reference to. I don't know how to fix the bug nor how to bind it to Python. What I do (or did at one time) know is how figure out how to recompile it, change the output to be database compatible, consistent messages rather than such useless messages as "this number is prime". The Python program, as it captures the StdOut, can watch for the bug. The bug is that factor.exe occasionally gets stuck on a composite while deep into the factoring process. Sometimes, however, this getting stuck can be resolved if you send the composite back to the beginning and start over. The factor.exe program isn't smart enough to try this, it simply returns COMPOSITE amongst the PRIME_FACTORS. The calling Python program can then collect the unfactored composites and call factor.exe again with each of them sending them back to the start of the factoring. If they successfully factor on the second pass, Python then appends them to the factors from the first pass to achieve a complete factorization that factor.exe can produce in theory but not in practice. From mensanator at aol.com Wed Jul 15 02:35:15 2009 From: mensanator at aol.com (Mensanator) Date: Tue, 14 Jul 2009 23:35:15 -0700 (PDT) Subject: why did you choose the programming language(s)you currently use? References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <70ac8b34-bf03-493b-b0c0-7d85e7a9eb94@d4g2000vbm.googlegroups.com> <7xab37t843.fsf@ruckus.brouhaha.com> Message-ID: <4b2e00a3-d09c-4c9a-bcfb-8c89af262963@i6g2000yqj.googlegroups.com> On Jul 14, 4:58?pm, Nobody wrote: > On Tue, 14 Jul 2009 11:47:08 -0700, Paul Rubin wrote: > >> - unlimited precision integers > >> - easy to program > >> - IDE not required > >> - reasonable speed > >> - math library needs to include number theoretic functions > >> ? like GCD, LCM, Modular Inverse, etc. > >> - not fucking retarded like F# > > > Have you looked at Haskell? > > Given his comment about F#, I have a suspicion that he might be > dogmatically opposed to functional languages generally. Not dogmatically opposed, I installed it because I actually wanted to try functional progrsmming. My mistake was listening to that Harrop dude who didn't bother to explain that F# is part of a government program that provides jobs for retards. To wit: F# has a rational data type, that's cool. But wait, F# has TWO rational data types. Huh? Well, after they made the first, some jackass came up with a second type that stores data more efficiently. And, of course, the second type doesn't have the same functionality as the first, so you need both. And of the myriad data type converion methods, guess which possible conversion methods are conspicuous by their absense? That's right, type1 <==> type2 rational conversions don't exist. At that point, I decided I wasn't going to put up crap like that and threw it away. From timr at probo.com Wed Jul 15 02:43:10 2009 From: timr at probo.com (Tim Roberts) Date: Tue, 14 Jul 2009 23:43:10 -0700 Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: "Dr. Phillip M. Feldman" wrote: > >Here's a related issue: I would like to see an option for type checking on >operands of logical operators, so that attempting to apply a logical >operator to non-Boolean entities generates a warning message. With operand >type checking, 'xor' and != would be different. How would you define "Boolean entities"? Do you mean the True and False values? Such a change would break virtually every Python program ever written. In any case, this idea is dead in the water. It would break a whole bunch of existing code from before the conditional operator: xxx = testme and truevalue or falsevalue -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From wuwei23 at gmail.com Wed Jul 15 02:50:08 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 14 Jul 2009 23:50:08 -0700 (PDT) Subject: one more question References: Message-ID: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> amr... at iisermohali.ac.in wrote: > I tried but its not coming. How much are you prepared to pay for help with this? Or are you just asking us to do all the work for you? From wuwei23 at gmail.com Wed Jul 15 02:56:57 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 14 Jul 2009 23:56:57 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> Message-ID: On Jul 15, 4:50?pm, alex23 wrote: > amr... at iisermohali.ac.in wrote: > > I tried but its not coming. > > How much are you prepared to pay for help with this? Or are you just > asking us to do all the work for you? Or to be a _little_ less blunt: if you want people here to _assist_ you with _your_ code, then post what you've tried here, along with tracebacks if errors are occurring, or an explanation as to what it isn't doing that you require. Spawning a new thread restating your question every time someone suggests you RTFM isn't the best way to show that you're actually trying to solve this issue yourself. From clp2 at rebertia.com Wed Jul 15 03:05:14 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 00:05:14 -0700 Subject: one more question In-Reply-To: <20300.210.212.36.65.1247638386.squirrel@www.iisermohali.ac.in> References: <20300.210.212.36.65.1247638386.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907150005q30917bf1i88cde982b54a3bdf@mail.gmail.com> On Tue, Jul 14, 2009 at 11:13 PM, wrote: > > Dear all, > > Just one more thing i want to ask that suppose i have a file like:--- > > > ?47 ? ? 8 ? ALA ? ? ? H ? ? H ? ? ?7.85 ? ? 0.02 ? ? 1 > ?48 ? ? 8 ? ALA ? ? ? HA ? ?H ? ? ?2.98 ? ? 0.02 ? ? 1 > ?49 ? ? 8 ? ALA ? ? ? HB ? ?H ? ? ?1.05 ? ? 0.02 ? ? 1 > ?50 ? ? 8 ? ALA ? ? ? C ? ? C ? ?179.39 ? ? ?0.3 ? ? 1 > ?51 ? ? 8 ? ALA ? ? ? CA ? ?C ? ? 54.67 ? ? ?0.3 ? ? 1 > ?52 ? ? 8 ? ALA ? ? ? CB ? ?C ? ? 18.85 ? ? ?0.3 ? ? 1 > ?53 ? ? 8 ? ALA ? ? ? N ? ? N ? ?123.95 ? ? ?0.3 ? ? 1 > 107 ? ?15 ? ALA ? ? ? H ? ? H ? ? ?8.05 ? ? 0.02 ? ? 1 > 108 ? ?15 ? ALA ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > > now what i want that i will make another file in which first it will write > the position of ALA lets say 8 then its name ALA and then the chemical > shift value for only three atoms C,CA and CB. > > Means it will be someting like: > > 8 ?ALA ?C = 179.39 ?CA = 54.67 ?CB = 18.85 > > I tried but its not coming. Surely a colleague at IISER Mohali's Computer Science department should be able to help you? Cheers, Chris -- http://blog.rebertia.com From koranthala at gmail.com Wed Jul 15 03:24:52 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 00:24:52 -0700 (PDT) Subject: one more question References: Message-ID: <19b3cb31-643c-47f4-9153-52e15289e933@24g2000yqm.googlegroups.com> On Jul 15, 11:13?am, amr... at iisermohali.ac.in wrote: > Dear all, > > Just one more thing i want to ask that suppose i have a file like:--- > > ?47 ? ? 8 ? ALA ? ? ? H ? ? H ? ? ?7.85 ? ? 0.02 ? ? 1 > ?48 ? ? 8 ? ALA ? ? ? HA ? ?H ? ? ?2.98 ? ? 0.02 ? ? 1 > ?49 ? ? 8 ? ALA ? ? ? HB ? ?H ? ? ?1.05 ? ? 0.02 ? ? 1 > ?50 ? ? 8 ? ALA ? ? ? C ? ? C ? ?179.39 ? ? ?0.3 ? ? 1 > ?51 ? ? 8 ? ALA ? ? ? CA ? ?C ? ? 54.67 ? ? ?0.3 ? ? 1 > ?52 ? ? 8 ? ALA ? ? ? CB ? ?C ? ? 18.85 ? ? ?0.3 ? ? 1 > ?53 ? ? 8 ? ALA ? ? ? N ? ? N ? ?123.95 ? ? ?0.3 ? ? 1 > 107 ? ?15 ? ALA ? ? ? H ? ? H ? ? ?8.05 ? ? 0.02 ? ? 1 > 108 ? ?15 ? ALA ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > > now what i want that i will make another file in which first it will write > the position of ALA lets say 8 then its name ALA and then the chemical > shift value for only three atoms C,CA and CB. > > Means it will be someting like: > > 8 ?ALA ?C = 179.39 ?CA = 54.67 ?CB = 18.85 > > I tried but its not coming. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA This is indeed possible and should be quite easy. One problem is that I do not exactly understand the problem - how do you decide which all to join and print? Is it ALA with 4th field as C or 6th field as 0.3? The issue here is that I am not getting the context of your problem. And I have *no* idea about the Chemical shift etc which you are talking about. If you can explain it a little more, I will try something out. Just for the scenario you explained, this code will suffice - f = open('abcd') d = {} for line in f: fields = line.split() if fields[2] == 'ALA': d.setdefault('ALA', {'position':fields[1], 'values':[]}) if fields[4] == 'C': d['ALA']['values'].append({fields[3]:fields[5]}) print d But i dont think this is what you want From piet at cs.uu.nl Wed Jul 15 03:42:15 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 09:42:15 +0200 Subject: Efficient binary search tree stored in a flat array? References: <5efddf9d-e6c6-47af-b0c2-f6a1be5a1a90@f16g2000vbf.googlegroups.com> <95f25380-d71f-41b6-82b8-f991551e8a1a@32g2000yqj.googlegroups.com> <01ea9a41-5885-4df3-91ae-24cfa5bfbec4@y17g2000yqn.googlegroups.com> Message-ID: >>>>> Douglas Alan (DA) wrote: >DA> I wrote: >>> On Jul 14, 8:10?am, Piet van Oostrum wrote: >>> >>> > Of course you can take any BST algorithm and replace pointers by indices >>> > in the array and allocate new elements in the array. But then you need >>> > array elements to contain the indices for the children explicitely. >>> And why is this a problem? >DA> Oh, I'm sorry -- I see what you are saying now. You're saying you can >DA> just implement a normal binary search tree, but store the tree nodes >DA> in an array, as if it were a chunk of memory, and use array indices as >DA> pointers, rather than using memory addresses as pointers. >DA> Fair enough, but I'm not sure what that would buy you. Other than, >DA> perhaps some improved locality of reference, and the potential to >DA> perhaps get the pointers take up less space if you know the array is >DA> never going to grow to be very large. My second sentence that you quoted more or less means `it doesn't buy you much'. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From dickinsm at gmail.com Wed Jul 15 03:44:48 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Wed, 15 Jul 2009 00:44:48 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> Message-ID: <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> On Jul 15, 5:07?am, "Dr. Phillip M. Feldman" wrote: > I appreciate the effort that people have made, but I'm not impressed with any > of the answers. ?For one thing, xor should be able to accept an arbitrary > number of input arguments (not just two), and should return True if and only > if the number of input arguments that evaluate to True is odd. Well, that's not exactly what you originally asked for. But it's still a one-liner: def xor(*args): return bool(sum(map(bool, args)) % 2) or perhaps def xor(*args): return bool(len(filter(None, args)) & 1) > Here's my code: > > def xor(*args): > ? ?"""xor accepts an arbitrary number of input arguments, returning True > ? ?if and only if bool() evaluates to True for an odd number of the input > ? ?arguments.""" > > ? ?result= False > > ? ?for arg in args: > ? ? ? if bool(arg): result= not result It's more idiomatic to say "if arg: ..." rather than "if bool (arg): ...". > > ? ?return result Mark From koranthala at gmail.com Wed Jul 15 03:51:23 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 00:51:23 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> Message-ID: <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> On Jul 15, 11:56?am, alex23 wrote: > On Jul 15, 4:50?pm, alex23 wrote: > > > amr... at iisermohali.ac.in wrote: > > > I tried but its not coming. > > > How much are you prepared to pay for help with this? Or are you just > > asking us to do all the work for you? > > Or to be a _little_ less blunt: if you want people here to _assist_ > you with _your_ code, then post what you've tried here, along with > tracebacks if errors are occurring, or an explanation as to what it > isn't doing that you require. > > Spawning a new thread restating your question every time someone > suggests you RTFM isn't the best way to show that you're actually > trying to solve this issue yourself. Alex, I am not sure about it. It doesnt look like she(I guess) is a programmer. In the last thread, she asked the question and the reply was to check up the regular expressions. Now, for a non-programmer, re might be a real confusing and tough subject to grasp. I am not saying that what you said was wrong, only that I felt that she got tense looking up regular expressions. So a python reply which basically does the basic checking without going to re etc might be more helpful for her to start her own coding. From piet at cs.uu.nl Wed Jul 15 04:14:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 10:14:20 +0200 Subject: The meaning of "=" References: Message-ID: >>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >A> In article , Piet van Oostrum wrote: >>>>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >>> >A> In article , Piet van Oostrum >A> wrote: >>> >>>>>> And to get c.x = 4 working you also need a __setitem__. >>> >A> Nope. You do need __setitem__ so that this works: >>> >A> c['x'] = 4 >>> >>> Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because >>> that was what the OP wanted (I think). >A> c.x = 4 >A> already updates the instance dict, so there's no need to change any class >A> methods to support it. That is, IME it's much better to add methods to >A> a regular class to make it more dict-like using the built-in instance >A> dict rather than changing any of the attribute mechanisms. If you're >A> really curious, I recommend trying several approaches yourself to see >A> what works better. ;-) Yes, that's why I mentioned __setitem__. I just mixed up the motivation. In [28]: class AttrDict: ....: def __getitem__(self, key): ....: return getattr(self, key) ....: ....: def __setitem__(self, key, value): ....: setattr(self, key, value) ....: ....: In [29]: c = AttrDict() In [30]: c["y"] = 3 In [31]: c.y Out[31]: 3 In [32]: c.x = 4 In [33]: c['x'] Out[33]: 4 -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From hackingkk at gmail.com Wed Jul 15 04:22:24 2009 From: hackingkk at gmail.com (Krishnakant) Date: Wed, 15 Jul 2009 13:52:24 +0530 Subject: select lines in python In-Reply-To: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> References: <18540.210.212.36.65.1247592839.squirrel@www.iisermohali.ac.in> Message-ID: <1247646144.3355.26.camel@krishna-laptop> On Tue, 2009-07-14 at 23:03 +0530, amrita at iisermohali.ac.in wrote: > Dear all, > > Can anyone tell me that suppose i have a file having content like: > > _Atom_name > _Atom_type > _Chem_shift_value > _Chem_shift_value_error > _Chem_shift_ambiguity_code > 1 1 PHE H H 8.49 0.02 1 > 2 1 PHE HA H 4.60 0.02 1 > 3 1 PHE CA C 57.83 0.3 1 > 4 2 LEU H H 8.23 0.02 1 > 5 2 LEU HA H 4.25 0.02 1 > 6 2 LEU HB2 H 1.54 0.02 1 > 7 3 ASP H H 8.10 0.02 1 > 8 3 ASP HA H 4.52 0.02 1 > 9 3 ASP HB2 H 2.65 0.02 1 > stop > > > now what i want that instead of copying all the lines it will just write > the information about PHE and ASP then how i acn do that using python > programming.Kindly tell me the command for that. Dear Amrita, No one will tell you commands for your program. We can give you logic and the way to implement it. It is recommended that you write the code your self. For our problem you keep a list and do a readlines() function and get the content in your list variable. then start looping through that list like for line in lines: where the lines variable is your list. Now keep comparing each line against your expected phrase and the moment you find one, do the actions you need such as writing to a file. happy hacking. Krishnakant. From andi at mozarellasalat.homelinux.net Wed Jul 15 04:24:22 2009 From: andi at mozarellasalat.homelinux.net (Andreas Grommek) Date: Wed, 15 Jul 2009 10:24:22 +0200 Subject: promlems with threading and print Message-ID: <4A5D9236.9060008@mozarellasalat.homelinux.net> Hi Newsgroup, I'm new to python and I am familiarizing myself with threads (haven't done any threading in any other language before...). I was playing around and discovered some weird behavior. Here is my code: import threading from time import sleep from random import random import sys class MyThread(threading.Thread): def __init__(self, t, s): self.threadmarker = t self.sleeptime = s threading.Thread.__init__(self) def run(self): print("Tread", self.threadmarker, "is going to sleep for a while...") sys.stdout.flush() #flush I/O sleep(self.sleeptime) #go to sleep print("Tread", self.threadmarker, "is waking up and terminating") a = 1 b = 20 for n in range(a, b): x = MyThread(n,random()*10.0) x.start() This should create some threads which print messages, go to sleep for a random amount of time (max 10 seconds) and return with a message. When I run the code I get something like this (always different): Tread 1 is going to sleep for a while... Tread 2 is going to sleep for a while... Tread 3 is going to sleep for a while... Tread 4 is going to sleep for a while... Tread 5 is going to sleep for a while... Tread 6 is going to sleep for a while... Tread 6 is going to sleep for a while... Tread 7 is going to sleep for a while... Tread 7 is going to sleep for a while... Tread 7 is going to sleep for a while... Tread 8 is going to sleep for a while... (...) Some "going to sleep" messages appear more than once. If I increase the number of thread the problem gets worse and threads are even started out of order (but this alone would not worry me...). Are some threads startet more than once or is this an issue with print? What else can I do in addition to sys.stdout.flush() after the first print statement? Are print and sleep thread-safe? Or is this a bug (I use python 3.1) Any hints and help for an newbie would be appreciated. Thanks, Andi From p.f.moore at gmail.com Wed Jul 15 04:31:13 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Wed, 15 Jul 2009 09:31:13 +0100 Subject: why did you choose the programming language(s)you currently use? In-Reply-To: References: <0427422e-9f20-4999-a601-9262f6d67632@h30g2000vbr.googlegroups.com> <4a5ccdd6$0$32679$9b4e6d93@newsspool2.arcor-online.net> Message-ID: <79990c6b0907150131x73370fa3iaa095564fe729a51@mail.gmail.com> 2009/7/15 Scott David Daniels : > Aahz wrote: >> >> In article <4a5ccdd6$0$32679$9b4e6d93 at newsspool2.arcor-online.net>, >> Stefan Behnel ? wrote: >>> >>> Deep_Feelings wrote: >>>> >>>> So you have chosen programming language "x" so shall you tell us why >>>> you did so , and ?what negatives or positives it has ? >>> >>> *duck* >> >> Where do you get the duck programming language? > > It shares a type system with Python, of course. ?:-) No, it just has a type system with all the same operations as Python, which all do the same things. :-) Paul. From alessiogiovanni.baroni at gmail.com Wed Jul 15 04:41:49 2009 From: alessiogiovanni.baroni at gmail.com (tuxagb) Date: Wed, 15 Jul 2009 01:41:49 -0700 (PDT) Subject: A question of style ..... Message-ID: <430dc862-7c94-4616-a66c-5ed2c033442f@n11g2000yqb.googlegroups.com> If I have to write an extension module with many objects, how can I organizing the code? I think: every object in a separate file, and a last file with the PyInit_..... function. But is unmenageable ..... Solutions? Thanks! From jgardner at jonathangardner.net Wed Jul 15 04:53:51 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Wed, 15 Jul 2009 01:53:51 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> Message-ID: <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> On Jul 14, 6:34?pm, Rick Lawson wrote: > Appreciate any help on this. I am porting an app from Java to python > and need generic object pooling with hooks for object initialization / > cleanup and be able to specify an object timeout. > Are you looking for something like a thread pool or a connection pool? Such a thing is easy to code in Python. Might as well write one from scratch for your particular need. By the way, a tip for writing Python: Forget Java. If you're porting an app, consider rewriting it from the architecture on up. You'll save yourself time and get a better result. From afriere at yahoo.co.uk Wed Jul 15 04:55:33 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Wed, 15 Jul 2009 01:55:33 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> Message-ID: On Jul 15, 5:44?pm, Mark Dickinson wrote: > On Jul 15, 5:07?am, "Dr. Phillip M. Feldman" > wrote: [snip] > > ? ?for arg in args: > > ? ? ? if bool(arg): result= not result > > It's more idiomatic to say "if arg: ..." rather than "if bool > (arg): ...". > Ah yes, but not once conditional tests, (just like logical operators), type-check to ensure they have been supplied with Boolean entities. ;) From steven at REMOVE.THIS.cybersource.com.au Wed Jul 15 04:58:36 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 15 Jul 2009 08:58:36 GMT Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> <4c2f9fdd-0840-47f3-bb43-5888340cb53b@j12g2000vbl.googlegroups.com> <93f6a517-63d8-4c80-bf19-4614b70999db@m7g2000prd.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 08:53:33 -0700, Carl Banks wrote: > On Jul 14, 2:14?am, Steven D'Aprano > wrote: >> On Tue, 14 Jul 2009 01:30:48 -0700, Carl Banks wrote: >> > Seriously, do you *ever* take more than 2 seconds to consider whether >> > you might be missing something obvious before following up with these >> > indignant knee-jerk responses? >> >> Obviously not. >> >> [...] >> >> > Or would you rather let all unexpected exceptions print to standard >> > error, which is often a black hole in non-interactive sitations? >> >> Fair point. Of course you're right. > > I don't want to be mean or anything. I agree with you on 95% of issues > probably, of course I only follow up to disagree.... No problem Carl. You were right, in this instance I was being a smartarse and ended up being too clever for my own good. There are cases where you want to catch all exceptions, not to hide them, but to process them in some way (possibly logging the error and then exiting). -- Steven From jeremiah.dodds at gmail.com Wed Jul 15 05:01:55 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 15 Jul 2009 10:01:55 +0100 Subject: Calling functions: Why this complicated ? In-Reply-To: <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> Message-ID: <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> On Wed, Jul 15, 2009 at 1:42 AM, Mohan Parthasarathy wrote: > So, all four of them above has its use cases in practice i guess. > > thanks > mohan > As a hopefully semi-informative aside, I've been writing python code for a few years now, and I regularly use all four forms of argument passing listed above. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jgardner at jonathangardner.net Wed Jul 15 05:02:04 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Wed, 15 Jul 2009 02:02:04 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <4c01f2c4-314d-4690-8904-d19dec9f6241@k1g2000yqf.googlegroups.com> On Jul 14, 4:48?pm, Ethan Furman wrote: > > A whole family of supers. ?:) > You should pick up Lisp. The whole concept of a binary operator doesn't exist over there. All the things binary operators can do, Lisp does with 0, 1, 2, or more arguments. [1]> (+) 0 [2]> (+ 1) 1 [3]> (+ 1 2) 3 [4]> (+ 1 2 3) 6 Once you get used to that, binary operators don't seem so useful anymore. The equivalent in Python is dropping the operators and replacing them with built-in functions that take 0, 1, 2, or more arguments. From steven at REMOVE.THIS.cybersource.com.au Wed Jul 15 05:11:44 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 15 Jul 2009 09:11:44 GMT Subject: missing 'xor' Boolean operator References: Message-ID: On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > Current Boolean operators are 'and', 'or', and 'not'. It would be nice > to have an 'xor' operator as well. I've often wished there was too, for the sake of completeness and aesthetics, I'd love to be able to write: a xor b instead of defining a function xor(a, b). Unfortunately, outside of boolean algebra and simulating electrical circuits, I can't think of any use-cases for an xor operator. Do you have any? -- Steven From pdpinheiro at gmail.com Wed Jul 15 05:17:09 2009 From: pdpinheiro at gmail.com (pdpi) Date: Wed, 15 Jul 2009 02:17:09 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> On Jul 15, 12:08?am, Christian Heimes wrote: > Chris Rebert wrote: > > Using the xor bitwise operator is also an option: > > bool(x) ^ bool(y) > > I prefer something like: > > ? ? bool(a) + bool(b) == 1 > > It works even for multiple tests (super xor): > > ? if bool(a) + bool(b) + bool(c) + bool(d) != 1: > ? ? ? raise ValueError("Exactly one of a, b, c and d must be true") > > Christian "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor 1 = 0 xor 1 = 1 if you assicate to the left) From bearophileHUGS at lycos.com Wed Jul 15 05:25:20 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Wed, 15 Jul 2009 02:25:20 -0700 (PDT) Subject: A question of style ..... References: <430dc862-7c94-4616-a66c-5ed2c033442f@n11g2000yqb.googlegroups.com> Message-ID: <07945ef8-769d-4ca5-8142-0112a3a7befc@k6g2000yqn.googlegroups.com> tuxagb: > If I have to write an extension module with many objects, how can I > organizing the code? > I think: every object in a separate file, and a last file with the > PyInit_..... function. But is unmenageable ..... > > Solutions? What do you think about using Cython? Bye, bearophile From tim.wintle at teamrubber.com Wed Jul 15 05:28:34 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Wed, 15 Jul 2009 10:28:34 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: <4c01f2c4-314d-4690-8904-d19dec9f6241@k1g2000yqf.googlegroups.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <4c01f2c4-314d-4690-8904-d19dec9f6241@k1g2000yqf.googlegroups.com> Message-ID: <1247650114.7759.13.camel@tim-laptop> On Wed, 2009-07-15 at 02:02 -0700, Jonathan Gardner wrote: > On Jul 14, 4:48 pm, Ethan Furman wrote: > > > > A whole family of supers. :) > > > All the things binary operators can do, Lisp > does with 0, 1, 2, or more arguments. +1 n-ary operators are great, but binary operators are just syntactic sugar for functions (which are obviously a generalisation of an n-ary operator in python). The fact that lisp-like languages don't expose this sugar is good for making you think about what you're actually doing, but just like mathematicians use binary (and unary) operators in equations rather than working in the lambda calculus, having that sugar is useful to python. > > [1]> (+) > 0 > [2]> (+ 1) > 1 > [3]> (+ 1 2) > 3 > [4]> (+ 1 2 3) > 6 c.f. : >>> sum([]) 0 >>> sum([1]) 1 >>> sum([1,2]) 3 >>> sum([1,2,3]) 6 > Once you get used to that, binary operators don't seem so useful > anymore. > > The equivalent in Python is dropping the operators and replacing them > with built-in functions that take 0, 1, 2, or more arguments. From mail at timgolden.me.uk Wed Jul 15 05:32:55 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 15 Jul 2009 10:32:55 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: References: Message-ID: <4A5DA247.3040606@timgolden.me.uk> Steven D'Aprano wrote: > On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > >> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >> to have an 'xor' operator as well. > > I've often wished there was too, for the sake of completeness and > aesthetics, I'd love to be able to write: > > a xor b > > instead of defining a function xor(a, b). > > Unfortunately, outside of boolean algebra and simulating electrical > circuits, I can't think of any use-cases for an xor operator. Do you have > any? I was pondering on this yesterday, and the only case I've come across in my code -- and it's reasonably common -- is checking that one and only one of two params has been passed. I have code which wants, say, an id or a name but doesn't want both. It's hardly difficult to write the check even now, but an built-in xor would make it fractionally cleaner. TJG From jarausch at igpm.rwth-aachen.de Wed Jul 15 05:53:28 2009 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: Wed, 15 Jul 2009 11:53:28 +0200 Subject: convert Dbase (.dbf) files to SQLite databases Message-ID: <7c5n8kF25pc8bU1@mid.dfncis.de> Hi, I have a lot of old Dbase files (.dbf) and I'll like to convert these to SQLite databases as automatically as possible. Does anybody know a tool/Python script to do so? I know, I could use dbfpy and create the SQLite table and import all data. But is there something easier? Many thanks for a hint, Helmut. -- Helmut Jarausch Lehrstuhl fuer Numerische Mathematik RWTH - Aachen University D 52056 Aachen, Germany From hartley79 at gmail.com Wed Jul 15 05:54:52 2009 From: hartley79 at gmail.com (hartley) Date: Wed, 15 Jul 2009 02:54:52 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> Message-ID: <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> On Jul 14, 2:21?pm, John Machin wrote: > On Jul 14, 7:22?pm, hartley wrote:> > > I'm very new at wrapping Python/C, and I have run into some problems. > > [snip] > > > > > > > ? ? ? ? pValue = PyObject_CallObject(pFunc,NULL); > > > > > pValue is now a PyList - i've even verified this with: > > > > > int a = PyList_Check(pValue); > > > > ? ? ? ? printf("%d\n", a); > > > > > However, I want to send this PyList to another python module, > > > > Please explain "send" ... do you mean the C equivalent of the Python > > > statement C_embedding.buff = the_pylist ? > > > > BTW C-embedding would trigger a syntax error in Python source; best to > > > avoid ... > > > I'm sorry I'm not using the proper terms when trying to describe this > > - i've never really learnt those terms, I guess i should do that one > > day. > > Today would be a good day to start :-) > > > > > > > Imagine that the python function C-embedding.buff looks like this: > > > def buff(a): > > ? ? if isinstance(a,list): > > ? ? ? ? print "success" > > > I want to send, pass, call, whatever a list argument from the C code > > onto this function. As simple as this - how can you call a function in > > python from C, and providing a python list as an argument? > > > As I wrote earlier - I already have a PyList, called pValue. But I > > have not been able to use this as an argument for C-embedding.buff > > using the code I described last time. > > > > > but I > > > > don't know how to do this. Initially I though I could just do like > > > > above, only swapping NULL with pValue, but that is not working. > > > > > pName2 = PyString_FromString("C-embedding"); > > > > pModule2 = PyImport_Import(pName2); > > > > pFunc2 = PyObject_GetAttrString(pModule2,"buff"); > > > ?Any good reason for not checking the > > > return value for an error? [Rhetorical question; answer == "No"] > > > > > pValue2 = PyObject_CallObject(pFunc2,pValue); > > Your problem is here, in the second arg of PyObject_CallObject. It > should be either NULL if no args are being supplied, or a tuple > containing the args if 1 or more args are being supplied. pValue is > the arg itself; you need to wrap it in a tuple. > > Seehttp://docs.python.org/c-api/object.html#PyObject_CallObject > > Actually you might like to use this instead (no tuple hassles): > > http://docs.python.org/c-api/object.html#PyObject_CallFunctionObjArgs > > What are you using as a source of information? Have you read this: > > http://docs.python.org/extending/embedding.html#pure-embedding? > > It contains a very thorough example of calling a Python function from > C, with all the reference-counting and error-checking stuff and it's > quite runnable -- I've just now compiled it and run it (1) as-is (2) > changed to pass a list instead of multiple ints (3) changed again to > emulate you trying to pass the list directly instead of wrapped in a > tuple ... here's the result: > > TypeError: argument list must be a tuple > Call failed > > Oh by the way, you can get away with C-embedding as a module name if > you are importing it from C, but importing it from Python [so that you > could test it independently of your C program] presents a difficulty > (syntax error) ... better to change it to c_embedding. > > > > It's mainly just a matter of (1) knowing what you want to do (2) > > > picking the API that does what you want (3) checking the returned > > > value for error after every call. > > HTH, > John Thank you, that works! You're a genius :) As for what I'm reading, I'm familiar with both http://docs.python.org/c-api/ and http://docs.python.org/extending/embedding.html#pure-embedding but i still find it hard to read this and find the proper API. The Python/C API reference is 162 pages! I have a second issue that I've also struggled with, namely using class object (don't shoot me if my terminology is off again). With the little toy class below, i want to instansiate it, boost it, and return it, similar to: s = ObjectTest() s.boostList() return s.dispList() but in C, of course. so far I have: pName = PyString_FromString("ob-test"); pModule = PyImport_Import(pName); pFunc = PyObject_GetAttrString(pModule,"ObjectTest"); if (pFunc && PyCallable_Check(pFunc)) { printf("Callable\n"); } pObj = PyObject_CallObject(pFunc,NULL); if (pObj != NULL) { printf("WAS NOT NULL!\n"); } At least this seems to work. However, as to how to actually instantiate and use this class the way I described, I have no idea which API to use. Even though I've spent lots of time reading it! Do you have any clue here, as well? (ob-test.py) class ObjectTest: def __init__(self): self.list = [] def boostList(self): self.list.append(1) def dispList(self): return self.list From quentel.pierre at wanadoo.fr Wed Jul 15 06:04:01 2009 From: quentel.pierre at wanadoo.fr (Pierre Quentel) Date: Wed, 15 Jul 2009 03:04:01 -0700 (PDT) Subject: select lines in python References: Message-ID: On 14 juil, 19:33, amr... at iisermohali.ac.in wrote: > Dear all, > > Can anyone tell me that suppose i have a file having content like: > > ? ? _Atom_name > ? ? ? _Atom_type > ? ? ? _Chem_shift_value > ? ? ? _Chem_shift_value_error > ? ? ? _Chem_shift_ambiguity_code > ? ? ?1 ? ? 1 ? PHE ? ? ? H ? ? H ? ? ?8.49 ? ? 0.02 ? ? 1 > ? ? ?2 ? ? 1 ? PHE ? ? ? HA ? ?H ? ? ?4.60 ? ? 0.02 ? ? 1 > ? ? ?3 ? ? 1 ? PHE ? ? ? CA ? ?C ? ? 57.83 ? ? ?0.3 ? ? 1 > ? ? ?4 ? ? 2 ? LEU ? ? ? H ? ? H ? ? ?8.23 ? ? 0.02 ? ? 1 > ? ? ?5 ? ? 2 ? LEU ? ? ? HA ? ?H ? ? ?4.25 ? ? 0.02 ? ? 1 > ? ? ?6 ? ? 2 ? LEU ? ? ? HB2 ? H ? ? ?1.54 ? ? 0.02 ? ? 1 > ? ? ?7 ? ? 3 ? ASP ? ? ? H ? ? H ? ? ?8.10 ? ? 0.02 ? ? 1 > ? ? ?8 ? ? 3 ? ASP ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > ? ? ?9 ? ? 3 ? ASP ? ? ? HB2 ? H ? ? ?2.65 ? ? 0.02 ? ? 1 > stop > > now what i want that instead of copying all the lines it will just write > the information about PHE and ASP then how i acn do that using python > programming.Kindly tell me the command for that. > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA Hi, The problem here is to test the values at specific colums in the file. So I'd rather split each line into a list of values and do a test on relevant items, like : for line in open(filename): items = line.strip().split() if items[1] in ["ASP","PHE"]: (... handle this case ...) Using re here is probably not the best way. It may happen that a sequence appearing in a column also appears in another one ; and when the tests become more complex the regular expression itself might become complex, or even impossible (imagine the OP wants to test that the numeric values are below a certain threshold) - Pierre From ben+python at benfinney.id.au Wed Jul 15 06:08:13 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 15 Jul 2009 20:08:13 +1000 Subject: Teaching pylint about keyword placeholders in string formatting Message-ID: <87bpnmjm2a.fsf@benfinney.id.au> Howdy all, A common idiom I use is:: def frobnicate(warble): foo = complex_computation() bar = long.access.path.leading.to.useful.value baz = (lengthy + expression * with_several_parts) spangulate("%(warble)s: %(foo)s%(bar)s [%(baz)d]" % vars()) This allows the format of the string to be clear, and allows the separate parts of it to have meaningful names. However, this is causing pylint to complain:: W:218:frobnicate: Unused argument 'warble' W:219:frobnicate: Unused variable 'foo' W:220:frobnicate: Unused variable 'bar' W:221:frobnicate: Unused variable 'baz' That is, pylint is not aware that the names used by accessing the values from the dictionary returned by ?vars()?. This warning, when it actually *does* detect unused name bindings, is very useful; I don't want to disable it. Nor do I want to pepper my code with hints to pylint about each one of these, detracting significantly from the readability which is the main point of the above idiom. How can I make pylint aware in the general case that the above idiom does, in fact, constitute a use of the names ?warble?, ?foo?, ?bar?, and ?baz? in the code? -- \ ?Pinky, are you pondering what I'm pondering?? ?I think so, | `\ Brain, but if we give peas a chance, won't the lima beans feel | _o__) left out?? ?_Pinky and The Brain_ | Ben Finney From david.lyon at preisshare.net Wed Jul 15 06:39:21 2009 From: david.lyon at preisshare.net (David Lyon) Date: Wed, 15 Jul 2009 06:39:21 -0400 Subject: convert Dbase (.dbf) files to SQLite databases In-Reply-To: <7c5n8kF25pc8bU1@mid.dfncis.de> References: <7c5n8kF25pc8bU1@mid.dfncis.de> Message-ID: <942d96482771b3b7a0e4294381c5f3dd@preisshare.net> On Wed, 15 Jul 2009 11:53:28 +0200, Helmut Jarausch wrote: > Hi, > > I have a lot of old Dbase files (.dbf) and I'll like to convert these > to SQLite databases as automatically as possible. > Does anybody know a tool/Python script to do so? > > I know, I could use dbfpy and create the SQLite table and import all > data. But is there something easier? yes... Use OpenOffice-Scalc or MS-Office-Excel to open the table... Export to csv.... Use SQLite Manager (https://addons.mozilla.org/en-US/firefox/addon/5817) and use the import wizard to import your data.... It shouldn't take too long... David From piet at cs.uu.nl Wed Jul 15 06:39:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 12:39:26 +0200 Subject: promlems with threading and print References: Message-ID: >>>>> Andreas Grommek (AG) wrote: >AG> Hi Newsgroup, >AG> I'm new to python and I am familiarizing myself with threads >AG> (haven't done any threading in any other language before...). I was >AG> playing around and discovered some weird behavior. Here is my code: When you start programming with threads you should familiarize yourself with (or better: study) the subject of parallel programming. Without that you will make hard to find and debug errors. Fortunately this one you did find. Many of these have to do with 'shared resources', i.e. resources (like objects) that are used in more than one thread at the same time, while at least one thread modifies it/them. For a start read this: http://effbot.org/zone/thread-synchronization.htm and then read some more elaborate literature. >AG> import threading >AG> from time import sleep >AG> from random import random >AG> import sys >AG> class MyThread(threading.Thread): >AG> def __init__(self, t, s): >AG> self.threadmarker = t >AG> self.sleeptime = s >AG> threading.Thread.__init__(self) >AG> def run(self): >AG> print("Tread", self.threadmarker, "is going to sleep for a while...") >AG> sys.stdout.flush() #flush I/O >AG> sleep(self.sleeptime) #go to sleep >AG> print("Tread", self.threadmarker, "is waking up and terminating") What happens here is that stdout is a shared resource that you are modifying in different threads. So you have to protect it. >AG> a = 1 >AG> b = 20 >AG> for n in range(a, b): >AG> x = MyThread(n,random()*10.0) >AG> x.start() >AG> This should create some threads which print messages, go to sleep for a random amount of time (max >AG> 10 seconds) and return with a message. When I run the code I get something like this (always different): You can get even weirder stuff, like: ('Tread', 1, 'is going to sleep for a while...') ('Tread', 2, 'is going to sleep for a while...') ('Tread', 3, 'is going to sleep for a while...') ('Tread', 4, 'is going to sleep for a while...') ('Tread', 5, 'is going to sleep for a while...') ('Tread', 6, 'is going to sleep for a while...') ('Tread', 7, 'is going to sleep for a while...') ('Tread', 8('Tread', , 'is going to sleep for a while...'9, )'is going to sleep for a while...' ('Tread'), 10, 'is going to sleep for a while...') ('Tread'('Tread', 12, 'is going to sleep for a while...') , 11, 'is going to sleep for a while...') ('Tread', 13, 'is going to sleep for a while...') ('Tread', 14, 'is going to sleep for a while...') ('Tread', 15, 'is going to sleep for a while...') ('Tread', 16, 'is going to sleep for a while...') ('Tread', 17, 'is going to sleep for a while...') ('Tread'('Tread', 19, 'is going to sleep for a while...') , 18, 'is going to sleep for a while...') ('Tread', 10, 'is waking up and terminating') ... Here is a solution using locks (and at the same time I corrected Tread->Thread class MyThread(threading.Thread): def __init__(self, t, s, lock): self.lock = lock self.threadmarker = t self.sleeptime = s threading.Thread.__init__(self) def run(self): with lock: print("Thread", self.threadmarker, "is going to sleep for a while...") sys.stdout.flush() #flush I/O sleep(self.sleeptime) #go to sleep with lock: print("Thread", self.threadmarker, "is waking up and terminating") a = 1 b = 20 lock = threading.Lock() for n in range(a, b): x = MyThread(n,random()*10.0, lock) x.start() If you have an older version of Python you have to replace with lock: statement by lock.acquire() try: statement finally: lock.release() or if you want to risk deadlocks: lock.acquire() statement lock.release() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nicolas.chauvat at logilab.fr Wed Jul 15 06:39:48 2009 From: nicolas.chauvat at logilab.fr (Nicolas Chauvat) Date: Wed, 15 Jul 2009 12:39:48 +0200 Subject: [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <87bpnmjm2a.fsf@benfinney.id.au> References: <87bpnmjm2a.fsf@benfinney.id.au> Message-ID: <20090715103948.GA6248@volans.logilab.fr> Hi, On Wed, Jul 15, 2009 at 08:08:13PM +1000, Ben Finney wrote: > def frobnicate(warble): > foo = complex_computation() > bar = long.access.path.leading.to.useful.value > baz = (lengthy + expression * with_several_parts) > spangulate("%(warble)s: %(foo)s%(bar)s [%(baz)d]" % vars()) > > This allows the format of the string to be clear, and allows the > separate parts of it to have meaningful names. > > However, this is causing pylint to complain:: > ... > That is, pylint is not aware that the names used by accessing the values > from the dictionary returned by ?vars()?. > ... You are not the only one: http://lists.logilab.org/pipermail/python-projects/2009-July/thread.html Here is the ticket: https://www.logilab.net/elo/ticket/9634 Thanks for the report. As usual, help and patches are welcome. -- Nicolas Chauvat logilab.fr - services en informatique scientifique et gestion de connaissances From denis-bz-gg at t-online.de Wed Jul 15 06:46:23 2009 From: denis-bz-gg at t-online.de (denis) Date: Wed, 15 Jul 2009 03:46:23 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> Message-ID: <22fa2fe9-899b-407a-8e3c-49f29c86fa59@r33g2000yqn.googlegroups.com> Sure, Aho-Corasick is fast for fixed strings; but without real numbers / a concrete goal > > Matt, how many words are you looking for, in how long a string ? a simple solution is good enough, satisficing. Matt asked "how to make that function look nicer?" but "nice" has many dimensions -- bicycles are nice for some tasks, Ferraris for others. Bytheway http://en.wikipedia.org/wiki/Aho-Corasick_algorithm has a link to a Python implementation, also http://en.wikipedia.org/wiki/Worse_is_Better is fun. From fabiodib at email.it Wed Jul 15 06:49:11 2009 From: fabiodib at email.it (fdb) Date: Wed, 15 Jul 2009 12:49:11 +0200 Subject: dictionary inherit and method overriding Message-ID: Hi all, I need to extend and not replace the __getitem__ method of a dict class. Here is sample the code: >>> class myDict(dict): ... def __getitem__(self, y): ... print("Doing something") ... dict.__getitem__(self, y) ... >>> a=myDict() >>> a["value"] = 1 >>> print a["value"] None As you see i get None instead of 1. Any solutions? Bye -- FabioBD From digitig at gmail.com Wed Jul 15 06:54:06 2009 From: digitig at gmail.com (Tim Rowe) Date: Wed, 15 Jul 2009 11:54:06 +0100 Subject: Calling functions: Why this complicated ? In-Reply-To: <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> Message-ID: 2009/7/15 Jeremiah Dodds : > As a hopefully semi-informative aside, I've been writing python code for a > few years now, and I regularly use all four forms of argument passing listed > above. Curiously, I never use the all-named style in Python, whereas it's my normal style in Ada. I shall now enter a period of self-refelection to try to work out why I am so inconsistent :-) -- Tim Rowe From ben+python at benfinney.id.au Wed Jul 15 07:01:07 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 15 Jul 2009 21:01:07 +1000 Subject: Teaching pylint about keyword placeholders in string formatting References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> Message-ID: <877hyajjm4.fsf@benfinney.id.au> Nicolas Chauvat writes: > On Wed, Jul 15, 2009 at 08:08:13PM +1000, Ben Finney wrote: > > That is, pylint is not aware that the names used by accessing the values > > from the dictionary returned by ?vars()?. > > ... > > You are not the only one: > http://lists.logilab.org/pipermail/python-projects/2009-July/thread.html Thank you. I think the thread Nicolas wanted me to see was this one: . -- \ ?I put contact lenses in my dog's eyes. They had little | `\ pictures of cats on them. Then I took one out and he ran around | _o__) in circles.? ?Steven Wright | Ben Finney From nicolas.chauvat at logilab.fr Wed Jul 15 07:21:26 2009 From: nicolas.chauvat at logilab.fr (Nicolas Chauvat) Date: Wed, 15 Jul 2009 13:21:26 +0200 Subject: [Python-projects] [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <20090715103948.GA6248@volans.logilab.fr> References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> Message-ID: <20090715112126.GA7138@volans.logilab.fr> On Wed, Jul 15, 2009 at 12:39:48PM +0200, Nicolas Chauvat wrote: > Here is the ticket: > https://www.logilab.net/elo/ticket/9634 Apologies: http://www.logilab.org/ticket/9634 -- Nicolas Chauvat logilab.fr - services en informatique scientifique et gestion de connaissances From lists at cheimes.de Wed Jul 15 07:31:44 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 15 Jul 2009 13:31:44 +0200 Subject: dictionary inherit and method overriding In-Reply-To: References: Message-ID: fdb wrote: > Hi all, > > I need to extend and not replace the __getitem__ method of a dict class. > > Here is sample the code: > >>>> class myDict(dict): > .... def __getitem__(self, y): > .... print("Doing something") > .... dict.__getitem__(self, y) > .... >>>> a=myDict() >>>> a["value"] = 1 >>>> print a["value"] > None > > As you see i get None instead of 1. > > Any solutions? How about returning the value? :] Christian From lists at cheimes.de Wed Jul 15 07:37:22 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 15 Jul 2009 13:37:22 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> Message-ID: pdpi wrote: > On Jul 15, 12:08 am, Christian Heimes wrote: >> Chris Rebert wrote: >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >> raise ValueError("Exactly one of a, b, c and d must be true") >> >> Christian > > "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to > xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor > 1 = 0 xor 1 = 1 if you assicate to the left) I'm well aware of the fact that I've described something differently. 'xor' can be explained as 'check if exactly one element of two elements is true'. My algorithms describes a super xor, aka 'check if exactly one element of n elements is true'. Christian From dzizes451 at gmail.com Wed Jul 15 08:07:20 2009 From: dzizes451 at gmail.com (dzizes) Date: Wed, 15 Jul 2009 05:07:20 -0700 (PDT) Subject: how to set timeout while colling a soap method? In-Reply-To: References: <24461403.post@talk.nabble.com> Message-ID: <24496577.post@talk.nabble.com> Yes, that is what I was looking for. Greets!! -- View this message in context: http://www.nabble.com/how-to-set-timeout-while-colling-a-soap-method--tp24461403p24496577.html Sent from the Python - python-list mailing list archive at Nabble.com. From lawson89 at gmail.com Wed Jul 15 08:09:54 2009 From: lawson89 at gmail.com (Rick Lawson) Date: Wed, 15 Jul 2009 05:09:54 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> Message-ID: <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> On Jul 15, 4:53?am, Jonathan Gardner wrote: > On Jul 14, 6:34?pm, Rick Lawson wrote: > > > Appreciate any help on this. I am porting an app from Java to python > > and need generic object pooling with hooks for object initialization / > > cleanup and be able to specify an object timeout. > > Are you looking for something like a thread pool or a connection pool? > Such a thing is easy to code in Python. Might as well write one from > scratch for your particular need. > > By the way, a tip for writing Python: Forget Java. If you're porting > an app, consider rewriting it from the architecture on up. You'll save > yourself time and get a better result. Jonathan, Thanks for the advice but would like to pick your brain a little more. The pool I need is not a thread pool or db pool. I need to pool objects which are a proxy for an external C process that communicates via sockets. The pool is necessary because the initial connection/ authentication is expensive, plus the connection needs to be recycled every so often due to instability on the C side. The existing Java code uses commons-pool which incidentally is the base object pool for a db connection pooling library (DBCP). Anyway, maybe this is just so easy to do in Python that everyone rolls their own - but I have done a lot of googling with no luck. I just hate to re-invent the wheel here - I guess I should look at a db connection pooling library and see if they have a generic object pool somewhere in there. Any you can recommend that are more or less standalone db connection pool libraries ? Thanks, Rick From sorry.no at can.do Wed Jul 15 08:15:53 2009 From: sorry.no at can.do (Borivoj) Date: Wed, 15 Jul 2009 14:15:53 +0200 Subject: Need help with oo design while using TreeWidget Message-ID: I'm looking for objective oriented and pythonic way to solve my problem. I'm using IDLE's TreeWidget, by inheriting TreeItem and overriding some of it's methods. My tree structure is defined inside treedata, which is xml.dom.minidom object. MenuTreeFrame is responsible for displaying the widget inside it's own Frame. As you can see in the source, MyTreeItem is one child in tree, and it's method GetSubList is responsible for creating it's children TreeItems. When creating each child, I have to pass original tree structure (treedata) to each child. Once I implement event firing, I will have to pass Event reference to each child as well. There has to be a better way to make these references available to each child in tree. What would you recommend? My code looks something like this (not executable, because treedata definition is lacking): import Tkinter as tk from TreeWidget import TreeItem, TreeNode, ScrolledCanvas class MenuTreeFrame(tk.Frame): def __init__(self, parent, treedata): tk.Frame.__init__(self, parent) self.treedata = treedata sc = ScrolledCanvas(self, bg='white', width=150, highlightthickness=0, takefocus=1) self.canvas = sc.canvas sc.frame.pack(fill="both", expand=1) def update(self, dom): self.create(dom) # TODO: implement something smarter than recreating whole tree def create(self, dom): self.rootitem = DomTreeItem(dom.documentElement, self.treedata) self.node = TreeNode(self.canvas, None, self.rootitem) self.node.update() self.node.select() self.rootitem.OnSelect() self.node.expand() class MyTreeItem(TreeItem): def __init__(self, treenode, treedata): self.node = treenode self.previously_selected = None self.treedata = treedata def GetText(self): return self.node.firstChild.nodeValue def IsExpandable(self): node = self.node return len(self.node.getElementsByTagName('node')) > 0 def OnSelect(self): self.treedata.current = self.node # TODO: fire off the event def GetSubList(self): parent = self.node children = parent.childNodes itemlist = [MyTreeItem(node, self.treedata) for node in children if node.nodeName == 'node'] return itemlist Thanks for reading Josip From fabiodib at email.it Wed Jul 15 08:19:15 2009 From: fabiodib at email.it (fdb) Date: Wed, 15 Jul 2009 14:19:15 +0200 Subject: dictionary inherit and method overriding In-Reply-To: References: Message-ID: Only this! I'm going crazy! Than you! Code: class myDict(dict): def __getitem__(self, y): print("Doing something") return dict.__getitem__(self, y) a=myDict() a["value"] = 1 print a["value"] Christian Heimes ha scritto: > How about returning the value? :] -- FabioBD From sjmachin at lexicon.net Wed Jul 15 08:34:31 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 15 Jul 2009 05:34:31 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> Message-ID: <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> On Jul 15, 7:54?pm, hartley wrote: > On Jul 14, 2:21?pm, John Machin wrote: > > On Jul 14, 7:22?pm, hartley wrote:> > > I'm very new at wrapping Python/C, and I have run into some problems. [snip] /* the first telling */ > > > > statement C_embedding.buff = the_pylist ? > > > > > BTW C-embedding would trigger a syntax error in Python source; best to > > > > avoid ... /* the second telling */ > > Oh by the way, you can get away with C-embedding as a module name if > > you are importing it from C, but importing it from Python [so that you > > could test it independently of your C program] presents a difficulty > > (syntax error) ... better to change it to c_embedding. > > > As for what I'm reading, I'm familiar with both > > http://docs.python.org/c-api/ > andhttp://docs.python.org/extending/embedding.html#pure-embedding You must have a very strange definition of familiarity! That section has as I pointed out a complete working example of a C program that calls a Python function with 1 or more arguments, complete with error checking and reference counting. To me, familiarity would include reading that program, understanding what it is doing (there are copious explanations), compiling it, running it, then start making changes (like I did) to do things like what you want to do. > but i still find it hard to read this and find the proper API. The > Python/C API reference is 162 pages! It's a reference, not a novel. You needed no API that was not in the sample program. What you should do is when you are going through the example programs, look up each API in the reference manual to see what the arguments are supposed to be, what other possibilities there are (e.g. PyObject_CallObject 2nd arg is NULL or a tuple of your args), what the return value is, what if anything is said about reference counts, what similar APIs are there (e.g. a few called PyObject_Callxxxxxxx) > I have a second issue that I've also struggled with, namely using > class object (don't shoot me if my terminology is off again). With the > little toy class below, i want to instansiate it, boost it, and return > it, similar to: > > s = ObjectTest() > s.boostList() > return s.dispList() Let's break that down: alist = s.dispList() return alist s.dispList (despite its name indicating "display") returns a reference to a list. Your C program is going to return that to whom? The shell? > but in C, of course. > > so far I have: > > pName = PyString_FromString("ob-test"); /* the third telling */ s/-/_/ "What I tell you three times is true". Just do it. > pModule = PyImport_Import(pName); You've imported the module. So far, I'm making the charitable assumption that the lack of error checking is for brevity in your posting, but is there in your actual code. > pFunc = PyObject_GetAttrString(pModule,"ObjectTest"); You've obtained a reference to the class. > if (pFunc && PyCallable_Check(pFunc)) { > > ? ? ? ? ? ? printf("Callable\n"); > ? ? ? ? ? ? ? ? } And if there's a problem, you'll fall through and use the dud reference to the class?? Get into the habit NOW of doing the error checking properly, after each API call; otherwise debugging will become a nightmare. Follow the examples in the sample program. In this case, it is necessary only to test for NULL, and get the correct error message displayed for that. Checking if the returned object is callable in the same if-test clouds the issue. Checking for callability separately is quite unnecessary -- just call the object and you'll find out then if it's not callable. > pObj = PyObject_CallObject(pFunc,NULL); > ? ? ? ? if (pObj != NULL) { > ? ? ? ? printf("WAS NOT NULL!\n"); Again instead of printing congratulatory messages on success, put in proper checking for failure, get the error message printed and bale out. > } > > At least this seems to work. However, as to how to actually > instantiate and use this class the way I described, I have no idea > which API to use. You don't know how to instantiate the class? What do you think was the purpose of the PyObject_CallObject() that you just did?? > Even though I've spent lots of time reading it! Do > you have any clue here, as well? Let's trawl through the Python equivalent of what you want to do: s = amodule.ObjectTest() (1) import the module (2) get the "objectTest" attribute of the module (3) call it, park the result in s s.boostList() (1) get the "boostList" attribute of s (2) call it ... but it's a method, maybe we need to use a different PyObject_Callxxxxxx ... no result to park. x = s.dispList() (1) get the "displist" attribute of s (2) call it (see above), park result in x (3) do something sensible with x > > (ob-test.py) > class ObjectTest: > > ? ? ? ? def __init__(self): > ? ? ? ? ? ? ? ? self.list = [] > > ? ? ? ? def boostList(self): > ? ? ? ? ? ? ? ? self.list.append(1) > > ? ? ? ? def dispList(self): > ? ? ? ? ? ? ? ? return self.list HTH, John From deets at nospam.web.de Wed Jul 15 08:40:05 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 14:40:05 +0200 Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> Message-ID: <7c60ooF25j7lsU1@mid.uni-berlin.de> Rick Lawson wrote: > On Jul 15, 4:53?am, Jonathan Gardner > wrote: >> On Jul 14, 6:34?pm, Rick Lawson wrote: >> >> > Appreciate any help on this. I am porting an app from Java to python >> > and need generic object pooling with hooks for object initialization / >> > cleanup and be able to specify an object timeout. >> >> Are you looking for something like a thread pool or a connection pool? >> Such a thing is easy to code in Python. Might as well write one from >> scratch for your particular need. >> >> By the way, a tip for writing Python: Forget Java. If you're porting >> an app, consider rewriting it from the architecture on up. You'll save >> yourself time and get a better result. > > Jonathan, > > Thanks for the advice but would like to pick your brain a little more. > The pool I need is not a thread pool or db pool. I need to pool > objects which are a proxy for an external C process that communicates > via sockets. The pool is necessary because the initial connection/ > authentication is expensive, plus the connection needs to be recycled > every so often due to instability on the C side. The existing Java > code uses commons-pool which incidentally is the base object pool for > a db connection pooling library (DBCP). Anyway, maybe this is just so > easy to do in Python that everyone rolls their own - but I have done a > lot of googling with no luck. > > I just hate to re-invent the wheel here - I guess I should look at a > db connection pooling library and see if they have a generic object > pool somewhere in there. Any you can recommend that are more or less > standalone db connection pool libraries ? I don't know enough about your domain, e.g. if your pooled objects have state or not. Assuming that - they are objects - they don't have state (like database connections) - you want to recycle them after so many calls one of the simplest solutions would be something like this: class AutoDestructingWrapper(object): MAX_CALL_COUNT = 1000 def __init__(self, object_factory): self._object_factory = object_factory self._call_count = self.MAX_CALL_COUNT def __getattr__(self, name): if self._call_count >= self.MAX_CALL_COUNT: self._delegate = self._object_factory() return getattr(self._delegate, name) Diez From jeremiah.dodds at gmail.com Wed Jul 15 09:04:12 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 15 Jul 2009 14:04:12 +0100 Subject: Calling functions: Why this complicated ? In-Reply-To: References: <89dd3da60907141340i46a6d913mb333788e85507128@mail.gmail.com> <50697b2c0907141631j41779ec4s782e66ddc0b467d0@mail.gmail.com> <89dd3da60907141742t45cd00dfnd3423d23af20c708@mail.gmail.com> <12cbbbfc0907150201r5a043e3cib69e7bc3e67f4a6b@mail.gmail.com> Message-ID: <12cbbbfc0907150604k33a6d468l387742ff0aa0a999@mail.gmail.com> On Wed, Jul 15, 2009 at 11:54 AM, Tim Rowe wrote: > > Curiously, I never use the all-named style in Python, whereas it's my > normal style in Ada. I shall now enter a period of self-refelection to > try to work out why I am so inconsistent :-) > > > I use it for functions that only (or mostly) have behaviour-modifying flags and for functions that take more than 3 or so parameters. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Wed Jul 15 09:05:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 15 Jul 2009 06:05:30 -0700 Subject: The meaning of "=" References: Message-ID: In article , Piet van Oostrum wrote: >>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: > >>A> In article , Piet van Oostrum wrote: >>>>>>>>> aahz at pythoncraft.com (Aahz) (A) wrote: >>>> >>A> In article , Piet van Oostrum >>A> wrote: >>>> >>>>>>> And to get c.x = 4 working you also need a __setitem__. >>>> >>A> Nope. You do need __setitem__ so that this works: >>>> >>A> c['x'] = 4 >>>> >>>> Sorry, I meant such that c.x = 4 does the same as c['x'] = 4 because >>>> that was what the OP wanted (I think). > >>A> c.x = 4 >>A> already updates the instance dict, so there's no need to change any class >>A> methods to support it. That is, IME it's much better to add methods to >>A> a regular class to make it more dict-like using the built-in instance >>A> dict rather than changing any of the attribute mechanisms. If you're >>A> really curious, I recommend trying several approaches yourself to see >>A> what works better. ;-) > >Yes, that's why I mentioned __setitem__. I just mixed up the motivation. Gotcha. Unfortunately, I can only respond to what you've written, not what you intended to write. ;-) (There was enough misinformation earlier in this thread that I felt being really really clear was important.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From pdpinheiro at gmail.com Wed Jul 15 09:12:03 2009 From: pdpinheiro at gmail.com (pdpi) Date: Wed, 15 Jul 2009 06:12:03 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> Message-ID: On Jul 15, 12:37?pm, Christian Heimes wrote: > pdpi wrote: > > On Jul 15, 12:08 am, Christian Heimes wrote: > >> Chris Rebert wrote: > >>> Using the xor bitwise operator is also an option: > >>> bool(x) ^ bool(y) > >> I prefer something like: > > >> ? ? bool(a) + bool(b) == 1 > > >> It works even for multiple tests (super xor): > > >> ? if bool(a) + bool(b) + bool(c) + bool(d) != 1: > >> ? ? ? raise ValueError("Exactly one of a, b, c and d must be true") > > >> Christian > > > "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to > > xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor > > 1 = 0 xor 1 = 1 if you assicate to the left) > > I'm well aware of the fact that I've described something differently. > 'xor' can be explained as 'check if exactly one element of two elements > is true'. My algorithms describes a super xor, aka 'check if exactly one > element of n elements is true'. > > Christian Well, I just wouldn't call it a "super xor" then, when "unicity test" works much better -- especially as an alternative to an actual xor without any specification to the actual intended functionality except the exception text. From peter.fodrek at stuba.sk Wed Jul 15 09:12:52 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Wed, 15 Jul 2009 15:12:52 +0200 Subject: import module unbelieveable behaviour Message-ID: <200907151512.52607.peter.fodrek@stuba.sk> Dear conference! I have test Why python based script for HeeksCNC post-processing does not work... And I've got unbelievable behavior When importing module module manually it works, but same opertaion from script does not work as seen /opt/HeeksCAD8/HeeksCNC> python Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import nc.rez >>> /opt/HeeksCAD8/HeeksCNC> python test.py Traceback (most recent call last): File "test.py", line 7, in import nc.rez ImportError: No module named rez /opt/HeeksCAD8/HeeksCNC> python ./test.py Traceback (most recent call last): File "./test.py", line 7, in import nc.rez ImportError: No module named rez Would anyone be helpful for me to get more information about this problem because pydb does not show anything usable for me,please? I look forward hearing form you Yours faithfully Peter Fodrek From mail at microcorp.co.za Wed Jul 15 09:37:30 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 15 Jul 2009 15:37:30 +0200 Subject: missing 'xor' Boolean operator References: Message-ID: <002101ca0551$66d1dd40$0d00a8c0@Hendrik> "Steven D'Aprano" wrote: >Unfortunately, outside of boolean algebra and simulating electrical >circuits, I can't think of any use-cases for an xor operator. Do you have >any? A bitwise xor is a poor man's comparator - if the result is binary zero, the operands were equal, no matter what they represented. Not much use in python, though. - Hendrik From deets at nospam.web.de Wed Jul 15 09:43:06 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 15:43:06 +0200 Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <7c60ooF25j7lsU1@mid.uni-berlin.de> Message-ID: <7c64etF26e7lsU1@mid.uni-berlin.de> Diez B. Roggisch wrote: > Rick Lawson wrote: > >> On Jul 15, 4:53?am, Jonathan Gardner >> wrote: >>> On Jul 14, 6:34?pm, Rick Lawson wrote: >>> >>> > Appreciate any help on this. I am porting an app from Java to python >>> > and need generic object pooling with hooks for object initialization / >>> > cleanup and be able to specify an object timeout. >>> >>> Are you looking for something like a thread pool or a connection pool? >>> Such a thing is easy to code in Python. Might as well write one from >>> scratch for your particular need. >>> >>> By the way, a tip for writing Python: Forget Java. If you're porting >>> an app, consider rewriting it from the architecture on up. You'll save >>> yourself time and get a better result. >> >> Jonathan, >> >> Thanks for the advice but would like to pick your brain a little more. >> The pool I need is not a thread pool or db pool. I need to pool >> objects which are a proxy for an external C process that communicates >> via sockets. The pool is necessary because the initial connection/ >> authentication is expensive, plus the connection needs to be recycled >> every so often due to instability on the C side. The existing Java >> code uses commons-pool which incidentally is the base object pool for >> a db connection pooling library (DBCP). Anyway, maybe this is just so >> easy to do in Python that everyone rolls their own - but I have done a >> lot of googling with no luck. >> >> I just hate to re-invent the wheel here - I guess I should look at a >> db connection pooling library and see if they have a generic object >> pool somewhere in there. Any you can recommend that are more or less >> standalone db connection pool libraries ? > > I don't know enough about your domain, e.g. if your pooled objects have > state or not. > > Assuming that > > - they are objects > - they don't have state (like database connections) > - you want to recycle them after so many calls > > one of the simplest solutions would be something like this: > > class AutoDestructingWrapper(object): > > MAX_CALL_COUNT = 1000 > > def __init__(self, object_factory): > self._object_factory = object_factory > self._call_count = self.MAX_CALL_COUNT > > This must be def __getattr__(self, name): self._call_count += 1 if self._call_count >= self.MAX_CALL_COUNT: self._delegate = self._object_factory() self._call_count = 0 return getattr(self._delegate, name) of course. Diez From python at mrabarnett.plus.com Wed Jul 15 10:21:27 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 15 Jul 2009 15:21:27 +0100 Subject: missing 'xor' Boolean operator In-Reply-To: References: Message-ID: <4A5DE5E7.5070003@mrabarnett.plus.com> Steven D'Aprano wrote: > On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > >> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >> to have an 'xor' operator as well. > > I've often wished there was too, for the sake of completeness and > aesthetics, I'd love to be able to write: > > a xor b > > instead of defining a function xor(a, b). > > Unfortunately, outside of boolean algebra and simulating electrical > circuits, I can't think of any use-cases for an xor operator. Do you have > any? > The problem is that 'and' and 'or' are not limited to Boolean values: 'and' returns the first false value or the last true value. 'or' returns the first true value or the last false value. What values should 'xor' return? IMHO, if only one of the values is true then it should return that value, otherwise it should return False. 1 xor 0 => 1 0 xor 2 => 2 1 xor 2 => False 0 xor 0 => False This is because it's a Boolean operator, so it should fall back to Boolean values when necessary, like 'not': not 0 => True not 1 => False Also: x and y and z => (x and y) and z x or y or z => (x or y) or z therefore: x xor y xor z => (x xor y) xor z From skip at pobox.com Wed Jul 15 10:22:11 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 15 Jul 2009 09:22:11 -0500 Subject: [Python-projects] [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <20090715103948.GA6248@volans.logilab.fr> References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> Message-ID: <19037.58899.828799.617599@montanaro.dyndns.org> Nicolas> Here is the ticket: Nicolas> https://www.logilab.net/elo/ticket/9634 Is it possible to get read-only access to the tracker? It's prompting me for a login which I don't have. Thx, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ when i wake up with a heart rate below 40, i head right for the espresso machine. -- chaos @ forums.usms.org From piet at cs.uu.nl Wed Jul 15 10:25:14 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 15 Jul 2009 16:25:14 +0200 Subject: promlems with threading and print References: Message-ID: >>>>> Piet van Oostrum (PvO) wrote: >PvO> def run(self): >PvO> with lock: All the 'with lock:' lines should have been 'with self.lock:' but as lock is also a global variable, it did work. Of course you can decide to use only the global variable and get rid of the self.lock altogether. I am not very fond of global variables myself, however, that's why I used the self.lock. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aurelien.campeas at logilab.fr Wed Jul 15 10:33:04 2009 From: aurelien.campeas at logilab.fr (=?utf-8?B?QXVyw6lsaWVuIENhbXDDqWFz?=) Date: Wed, 15 Jul 2009 16:33:04 +0200 Subject: [Python-projects] [TIP] Teaching pylint about keyword placeholders in string formatting In-Reply-To: <19037.58899.828799.617599@montanaro.dyndns.org> References: <87bpnmjm2a.fsf@benfinney.id.au> <20090715103948.GA6248@volans.logilab.fr> <19037.58899.828799.617599@montanaro.dyndns.org> Message-ID: <20090715143304.GJ10884@crater.logilab.fr> On Wed, Jul 15, 2009 at 09:22:11AM -0500, skip at pobox.com wrote: > > Nicolas> Here is the ticket: > Nicolas> https://www.logilab.net/elo/ticket/9634 > > Is it possible to get read-only access to the tracker? It's prompting me > for a login which I don't have. > > Thx, that should be http://www.logilab.org/ticket/9634 From Bill at SynectixLtd.com Wed Jul 15 10:37:54 2009 From: Bill at SynectixLtd.com (Bill Davy) Date: Wed, 15 Jul 2009 15:37:54 +0100 Subject: missing 'xor' Boolean operator References: Message-ID: <6Yidnc7fb6QvdMDXnZ2dnUVZ8h6dnZ2d@bt.com> "MRAB" wrote in message news:mailman.3158.1247667680.8015.python-list at python.org... > Steven D'Aprano wrote: >> On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: >> >>> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >>> to have an 'xor' operator as well. >> >> I've often wished there was too, for the sake of completeness and >> aesthetics, I'd love to be able to write: >> >> a xor b >> >> instead of defining a function xor(a, b). >> >> Unfortunately, outside of boolean algebra and simulating electrical >> circuits, I can't think of any use-cases for an xor operator. Do you have >> any? >> > The problem is that 'and' and 'or' are not limited to Boolean values: > > 'and' returns the first false value or the last true value. > > 'or' returns the first true value or the last false value. > > What values should 'xor' return? IMHO, if only one of the values is true > then it should return that value, otherwise it should return False. > > 1 xor 0 => 1 > 0 xor 2 => 2 > 1 xor 2 => False > 0 xor 0 => False > > This is because it's a Boolean operator, so it should fall back to > Boolean values when necessary, like 'not': > > not 0 => True > not 1 => False > > Also: > > x and y and z => (x and y) and z > x or y or z => (x or y) or z > > therefore: > > x xor y xor z => (x xor y) xor z Gosh, let's all discuss commutation and distribution. And surely in quantum merchanics there is something about non-commuting operatiomns letting in Planck's constant. From tycho at tycho.ws Wed Jul 15 10:58:03 2009 From: tycho at tycho.ws (Tycho Andersen) Date: Wed, 15 Jul 2009 09:58:03 -0500 Subject: import module unbelieveable behaviour In-Reply-To: <200907151512.52607.peter.fodrek@stuba.sk> References: <200907151512.52607.peter.fodrek@stuba.sk> Message-ID: <49b3a7400907150758j77e97e95ra9b806e763eb22da@mail.gmail.com> On Wed, Jul 15, 2009 at 8:12 AM, Peter Fodrek wrote: > > Would anyone be helpful for me to get more information about this problem > because ?pydb does not show anything usable for me,please? What is the directory structure for the HeeksCNC module? Although I'm no expert, I suspect it looks something like: nc/ nc/rez.py nc/foo.py In order for python to look in the nc/ directory for modules, there needs to be a file called __init__.py (even if it's empty). I suspect the nc/ directory is missing this file (although, I'm confused as to why it would work in the interpreter then). Tycho From jeanmichel at sequans.com Wed Jul 15 11:00:39 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 17:00:39 +0200 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: <4A5DEF17.3050107@sequans.com> John Nagle wrote: > walterbyrd wrote: >> I believe Guido himself has said that all indentions should be four >> spaces - no tabs. >> >> Since backward compatibility is being thrown away anyway, why not >> enforce the four space rule? >> >> At least that way, when I get python code from somebody else, I would >> know what I am looking at, without having to do a hex dump, or >> something. > > Python 3 enforces the rule that you can't mix tabs and spaces > for indentation in the same file. That (finally) guarantees that > the indentation you see is what the Python parser sees. That's > enough to prevent non-visible indentation errors. > > It also means that the Python parser no longer has to have > any concept of how many spaces equal a tab. So the problem > is now essentially solved. > > John Nagle By the way why would you prevent us from using tabs for indenting ? If I'm not wrong, from a semantic point of view, that's what tabs are for: indenting. Spaces are meant to separate tokens, aren't they ? I love my tabs, don't take them away from me ! JM From jeanmichel at sequans.com Wed Jul 15 11:03:30 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 17:03:30 +0200 Subject: Colour of output text In-Reply-To: References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: <4A5DEFC2.4030602@sequans.com> Nobody wrote: > On Fri, 10 Jul 2009 09:23:54 +0000, garabik-news-2005-05 wrote: > > >>>> I would like to learn a way of changing the colour of a particular >>>> part of the output text. I've tried the following >>>> >>> On Unix operating systems this would be done through the curses interface: >>> >>> http://docs.python.org/library/curses.html >>> >> Or using ANSI colour codes: >> >> colours = { >> 'none' : "", >> 'default' : "\033[0m", >> 'bold' : "\033[1m", >> > > [snip] > > >> # non-standard attributes, supported by some terminals >> > > This comment should have appeared immediately after "none" ;) > > Hard-coding control/escape sequences is just lame. Use the curses modules > to obtain the correct sequences for the terminal. > > As the OP I'm really interested in doing so. I currently have all my colors hard-coded. Now It may be lame but as soon as I call curses.initscr(), it's just messing up with my terminal, moreover I didn't figure out how to "print 'hello'" using curses color codes. Anyone has an example ? I'm pretty sure it may fit in one line. JM From jeanmichel at sequans.com Wed Jul 15 11:15:47 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 17:15:47 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <4A5DF2A3.2080101@sequans.com> Christian Heimes wrote: > Chris Rebert wrote: > >> Using the xor bitwise operator is also an option: >> bool(x) ^ bool(y) >> > > I prefer something like: > > bool(a) + bool(b) == 1 > > It works even for multiple tests (super xor): > > if bool(a) + bool(b) + bool(c) + bool(d) != 1: > raise ValueError("Exactly one of a, b, c and d must be true") > > Christian > > While everyone's trying to tell the OP how to workaround the missing xor operator, nobody answered the question "why is there no xor operator ?". If the question was "Why is there no 'or' operator ?", would "because A or B <=> not(not A and not B)" be a proper answer ? JM From MrJean1 at gmail.com Wed Jul 15 11:15:55 2009 From: MrJean1 at gmail.com (Jean) Date: Wed, 15 Jul 2009 08:15:55 -0700 (PDT) Subject: Best Way to Handle All Exceptions References: <45090ff9-6f58-4b65-b08b-78189cdbc3c4@g23g2000vbr.googlegroups.com> Message-ID: On Jul 13, 6:26?am, seldan24 wrote: > Hello, > > I'm fairly new at Python so hopefully this question won't be too > awful. ?I am writing some code that will FTP to a host, and want to > catch any exception that may occur, take that and print it out > (eventually put it into a log file and perform some alerting action). > I've figured out two different ways to do this, and am wondering which > is the best (i.e. cleanest, 'right' way to proceed). ?I'm also trying > to understand exactly what occurs for each one. > > The first example: > > from ftplib import FTP > try: > ? ? ftp = FTP(ftp_host) > ? ? ftp.login(ftp_user, ftp_pass) > except Exception, err: > ? ? print err > > This works fine. ?I read through the documentation, and my > understanding is that there is a built-in exceptions module in python, > that is automatically available in a built-in namespace. ?Within that > module is an 'Exception' class which would contain whatever exception > is thrown. ?So, I'm passing that to the except, along with err to hold > the value and then print it out. > > The second example: > > from ftplib import FTP > import sys > try: > ? ? ftp = FTP(ftp_host) > ? ? ftp.login(ftp_user, ftp_pass) > except: > ? ? print sys.exc_info() > > Here I, for the most part, get the same thing. ?I'm not passing > anything to except and just printing out the exception using a method > defined in the sys module. > > So, I'm new to Python... I've made it this far and am happy, but want > to make sure I'm coding correctly from the start. ?Which method is the > better/cleaner/more standard way to continue? ?Thanks for any help. The second example is "better" if you need your code to work in Python 3.0 *and* in 2.X. For Python 3.0, the first example has to be written as: .... except Exception as err: .... /Jean Brouwers From skip at pobox.com Wed Jul 15 11:23:22 2009 From: skip at pobox.com (skip at pobox.com) Date: Wed, 15 Jul 2009 10:23:22 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <4A5DEF17.3050107@sequans.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> <4A5DEF17.3050107@sequans.com> Message-ID: <19037.62570.370235.287125@montanaro.dyndns.org> JM> By the way why would you prevent us from using tabs for indenting ? JM> If I'm not wrong, from a semantic point of view, that's what tabs JM> are for: indenting. Spaces are meant to separate tokens, aren't they JM> ? I love my tabs, don't take them away from me ! I don't think your tabs have been taken away, you just can't mix them with spaces. Skip From python-url at phaseit.net Wed Jul 15 11:36:06 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Wed, 15 Jul 2009 15:36:06 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 15) Message-ID: QOTW: "Everyone gets so caught up in programming via languages that you get, well, people trying to teach 'Computer Programming' as if it were only necessary to grok a language, rather than grokking /symbol manipulation/ itself." - Simon Forman http://groups.google.com/group/comp.lang.python/msg/4bc886a5d5feda14 A C programmer may completely fail to guess the actual meaning of ++n and --n in Python: http://groups.google.com/group/comp.lang.python/browse_thread/thread/fa7281ce6249dec5/ Building the argument list for a subprocess call *right* wasn't obvious: http://groups.google.com/group/comp.lang.python/browse_thread/thread/f02150fbc732f9dc/ Robust way to handle all possible exceptions: http://groups.google.com/group/comp.lang.python/browse_thread/thread/db858ef8dfb51a47/ A related question: how to safely handle Ctrl-C: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a3ef46f47a0eac39/ Started two weeks ago, this thread is now discussing about the correct usage of "assert": http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1a6ca84d3bfc483/ Looking for a robust and safe way of writing a constructor when any step may fail: http://groups.google.com/group/comp.lang.python/browse_thread/thread/1dc1f1cb5fa8177a/ How to define a "class" property (as opposed to normal, "instance" properties): http://groups.google.com/group/comp.lang.python/browse_thread/thread/8a8b8138eada2be5/ How to explain basic concepts like pass-by-object, assignment and mutability to beginners: http://groups.google.com/group/comp.lang.python/browse_thread/thread/4b5c5aa1d8b42b9c/ later: the meaning of '=' in assignment http://groups.google.com/group/comp.lang.python/browse_thread/thread/3c13fd526a4d35df/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From robert.kern at gmail.com Wed Jul 15 11:38:55 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 15 Jul 2009 10:38:55 -0500 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5DF2A3.2080101@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <4A5DF2A3.2080101@sequans.com> Message-ID: On 2009-07-15 10:15, Jean-Michel Pichavant wrote: > Christian Heimes wrote: >> Chris Rebert wrote: >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >> >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >> raise ValueError("Exactly one of a, b, c and d must be true") >> >> Christian >> > While everyone's trying to tell the OP how to workaround the missing xor > operator, nobody answered the question "why is there no xor operator ?". > > If the question was "Why is there no 'or' operator ?", would "because A > or B <=> not(not A and not B)" be a proper answer ? That's not the only answer the OP has been given. The most compelling answer is that an "xor" keyword cannot be implemented with similar semantics to "and" and "or", in particular short-circuiting and returning one of the actual inputs instead of a coerced bool. -- 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 deets at nospam.web.de Wed Jul 15 11:41:54 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 17:41:54 +0200 Subject: import module unbelieveable behaviour References: Message-ID: <7c6bdkF26j00sU1@mid.uni-berlin.de> Peter Fodrek wrote: > Dear conference! > > I have test Why python based script for HeeksCNC post-processing does not > work... And I've got unbelievable behavior When importing module module > manually it works, but same opertaion from script does not > work as seen > > /opt/HeeksCAD8/HeeksCNC> python > Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import nc.rez >>>> > /opt/HeeksCAD8/HeeksCNC> python test.py > Traceback (most recent call last): > File "test.py", line 7, in > import nc.rez > ImportError: No module named rez What does import nc print nc.__file__ tell you, and is that what you'd expect it to be? Diez From hniksic at xemacs.org Wed Jul 15 12:14:10 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 15 Jul 2009 18:14:10 +0200 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <87fxcxj54d.fsf@busola.homelinux.net> Jean-Michel Pichavant writes: > While everyone's trying to tell the OP how to workaround the missing > xor operator, nobody answered the question "why is there no [boolean] > xor operator ?". Probably because there isn't one in C. The bitwise XOR operator, on the other hand, exists in both C and Python. > If the question was "Why is there no 'or' operator ?", would "because > A or B <=> not(not A and not B)" be a proper answer ? Note that in Python A or B is in fact not equivalent to not(not A and not B). From ethan at stoneleaf.us Wed Jul 15 12:25:08 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 15 Jul 2009 09:25:08 -0700 Subject: convert Dbase (.dbf) files to SQLite databases In-Reply-To: <7c5n8kF25pc8bU1@mid.dfncis.de> References: <7c5n8kF25pc8bU1@mid.dfncis.de> Message-ID: <4A5E02E4.9090303@stoneleaf.us> Helmut Jarausch wrote: > Hi, > > I have a lot of old Dbase files (.dbf) and I'll like to convert these > to SQLite databases as automatically as possible. > Does anybody know a tool/Python script to do so? > > I know, I could use dbfpy and create the SQLite table and import all > data. But is there something easier? > > Many thanks for a hint, > > Helmut. > Greetings! If your tables are either dBase III or Visual Foxpro 6 (may work with other VFP versions...) you can try http://groups.google.com/group/python-dbase #open table import dbf table = dbf.Table('/path/to/old/dbf') #generate .csv file table.export() # defaults to same name with csv extension #access records for rec in table: tups = tuple(rec) lst = list(rec) dct = rec.scatter_fields() I haven't used SQlite, so I'm unable to provide samples for that portion of the conversion. Hope this helps. ~Ethan~ From sjmachin at lexicon.net Wed Jul 15 12:30:29 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 15 Jul 2009 09:30:29 -0700 (PDT) Subject: convert Dbase (.dbf) files to SQLite databases References: <7c5n8kF25pc8bU1@mid.dfncis.de> Message-ID: <419cc0e0-3a78-4906-8420-c12ce2684bef@c36g2000yqn.googlegroups.com> On Jul 15, 8:39?pm, David Lyon wrote: > On Wed, 15 Jul 2009 11:53:28 +0200, Helmut Jarausch > > wrote: > > Hi, > > > I have a lot of old Dbase files (.dbf) and I'll like to convert these > > to SQLite databases as automatically as possible. > > Does anybody know a tool/Python script to do so? > > > I know, I could use dbfpy and create the SQLite table and import all > > data. But is there something easier? > > yes... > > Use OpenOffice-Scalc or MS-Office-Excel to open the table... Max 64K rows for Scalc and Excel 2003; 2007 can take 2**20 rows. Only old dBase (not dBase 7). Memo fields not handled. Visual FoxPro DBFs not supported by Excel even tho' VFP is an MS product. > Export to csv.... Yuk. > > Use SQLite Manager (https://addons.mozilla.org/en-US/firefox/addon/5817) > > and use the import wizard to import your data.... > > It shouldn't take too long... ... before you get sick of the error-prone manual tasks. I'd write a script that took a DBF file, analysed the field descriptions, made a CREATE TABLE statement, executed it, and then started doing inserts. Fairly easy to write. Scripts have the great benefit that you can fix them and re-run a whole lot easier than redoing manual steps. If dbfpy can't handle any new-fangled stuff you may have in your files, drop me a line ... I have a soon-to-be released DBF module that should be able to read the "new" stuff up to dBase7 and VFP9, including memo files, conversion from whatever to Unicode if needed, ... Cheers, John From seldan24 at gmail.com Wed Jul 15 12:36:13 2009 From: seldan24 at gmail.com (seldan24) Date: Wed, 15 Jul 2009 09:36:13 -0700 (PDT) Subject: Python Equivalent for dd & fold Message-ID: Hello, I have a shell script, that I'm attempting to convert to Python. It FTP's files down from an AS/400 machine. That part is working fine. Once the files arrive, the script converts them from EBCDIC to ASCII and then formats their line width based on a pre-determined size. For example, if I have the file TESTFILE and I know it should be formatted to 32 characters/line, I run: dd if=TESTFILE,EBCDIC conv=ASCII | fold -w 32 > TESTFILE.ASCII Luckily, the files have the packed decimal format common in COBOL programs converted already prior to reaching me, so all I have to do is the conversion and line width formatting. The above works fine, and I know I could (and may) just embed it using subprocess.Popen. This, I've figured out how to do. But, ideally, I'd like to convert as much shell to native Python as possible, so that I can learn more. I think I can figure out the first part by using the codecs module... found some information on Google related to that. My question is, what can I use as the equivalent for the Unix 'fold' command? I don't really need to know how to do it, just a push in the right direction. Thanks. From rhodri at wildebst.demon.co.uk Wed Jul 15 12:40:20 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 15 Jul 2009 17:40:20 +0100 Subject: select lines in python In-Reply-To: <29449.210.212.36.65.1247598632.squirrel@www.iisermohali.ac.in> References: <29449.210.212.36.65.1247598632.squirrel@www.iisermohali.ac.in> Message-ID: On Tue, 14 Jul 2009 20:10:32 +0100, wrote: > Can i become more precise like instead of printing all lines for PHE and > ASP is it possible that for PHE python will print only those lines which > will have information about H and HA and for ASP it will print those > lines > which will have information about HA and HB. 1) Top-posting is considered unhelpful, since it breaks the flow of information badly. 2) To answer question: yes, you can. However I can sense the start of an unending flood of small changes, so I'm going to direct you back to the Python tutorial at www.python.org rather than tell you exactly how. -- Rhodri James *-* Wildebeest Herder to the Masses From ethan at stoneleaf.us Wed Jul 15 12:44:53 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 15 Jul 2009 09:44:53 -0700 Subject: convert Dbase (.dbf) files to SQLite databases In-Reply-To: <419cc0e0-3a78-4906-8420-c12ce2684bef@c36g2000yqn.googlegroups.com> References: <7c5n8kF25pc8bU1@mid.dfncis.de> <419cc0e0-3a78-4906-8420-c12ce2684bef@c36g2000yqn.googlegroups.com> Message-ID: <4A5E0785.8060307@stoneleaf.us> John Machin wrote: > If dbfpy can't handle any new-fangled stuff you may have in your > files, drop me a line ... I have a soon-to-be released DBF module that > should be able to read the "new" stuff up to dBase7 and VFP9, > including memo files, conversion from whatever to Unicode if > needed, ... > > Cheers, > John Cool! I'm looking forward to it! ~Ethan~ From motoom at xs4all.nl Wed Jul 15 12:47:13 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Wed, 15 Jul 2009 18:47:13 +0200 Subject: Python Equivalent for dd & fold In-Reply-To: References: Message-ID: <4A5E0811.2050800@xs4all.nl> seldan24 wrote: > what can I use as the equivalent for the Unix 'fold' command? def fold(s,len): while s: print s[:len] s=s[len:] s="A very long string indeed. Really that long? Indeed." fold(s,10) Output: A very lon g string i ndeed. Rea lly that l ong? Indee d. Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From weafon at lbl.gov Wed Jul 15 12:48:49 2009 From: weafon at lbl.gov (weafon) Date: Wed, 15 Jul 2009 09:48:49 -0700 Subject: How to keep a function as a generator function when the yield operator is moved into its sub-functions?? In-Reply-To: <4A5D4DC5.1090202@ieee.org> References: <4A5CC86A.9070907@lbl.gov> <4A5D4DC5.1090202@ieee.org> Message-ID: <4A5E0871.90105@lbl.gov> Hi DaveA, Thank for your responses even though my problem has been solved based on Miles' suggestion. I am writing programs by using the SimPy library, which is a discrete-event simulator library. Below is my actual code segment class RAID(Process): def ReqServ(self): while(now() weafon wrote: >>
Hi guys, >> >> I have a question about the usage of yield. As shown in the below >> example, in general, if there is a code segment commonly used by two >> or more functions, we may isolate the segment into a function and >> then call it from other functions if necessary. >> >> def func1(): >> .... >> while(cond): >> ..... >> commoncode() >> ... >> >> >> def func2(): >> .... >> while(cond): >> ..... >> commoncode() >> ... >> >> def commoncode() >> AAAA >> BBBB >> CCCC >> >> However, if there is a 'yield' operation in the common code segment, >> the isolation causes that func1 and func2 become a non-generator >> function!! Although I can prevent such an isolation by just >> duplicating the segment in func1 and func2 to keep both of them being >> generator functions, the code may become ugly and hard to maintain >> particularly when coomoncode() is long. >> >> The problem may be resolved if I can define the commoncode() as an >> inline function or marco. Unfortunately, inline and marco do not >> seems to be implemented in python. Thus, how can I isolate a common >> segment into a function when there are yield operations in the common >> segment? >> >> Thanks, >> Weafon >> > You are implying there's something special or unique about yield in > this. Return has the same problem, and many other flow control > constructs. Also, variable definitions and scoping. > > So you can't just copy any old bunch of adjacent lines out of two > functions, put it into a third, and call it factoring. Give us a > specific example you're puzzled about, and we can try to solve it. > > DaveA > From seldan24 at gmail.com Wed Jul 15 13:14:54 2009 From: seldan24 at gmail.com (seldan24) Date: Wed, 15 Jul 2009 10:14:54 -0700 (PDT) Subject: Python Equivalent for dd & fold References: Message-ID: On Jul 15, 12:47?pm, Michiel Overtoom wrote: > seldan24 wrote: > > what can I use as the equivalent for the Unix 'fold' command? > > def fold(s,len): > ? ? ?while s: > ? ? ? ? ?print s[:len] > ? ? ? ? ?s=s[len:] > > s="A very long string indeed. Really that long? Indeed." > fold(s,10) > > Output: > > A very lon > g string i > ndeed. Rea > lly that l > ong? Indee > d. > > Greetings, > > -- > "The ability of the OSS process to collect and harness > the collective IQ of thousands of individuals across > the Internet is simply amazing." - Vinod Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html Wow, I feel like a dork. I should have done more research prior to posting. Anyway, thanks for the advice. The trouble with Python is that things make 'too much' sense. Loving this language. From python at mrabarnett.plus.com Wed Jul 15 13:23:14 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 15 Jul 2009 18:23:14 +0100 Subject: Python Equivalent for dd & fold In-Reply-To: References: Message-ID: <4A5E1082.8090901@mrabarnett.plus.com> seldan24 wrote: > On Jul 15, 12:47 pm, Michiel Overtoom wrote: >> seldan24 wrote: >>> what can I use as the equivalent for the Unix 'fold' command? >> def fold(s,len): >> while s: >> print s[:len] >> s=s[len:] >> >> s="A very long string indeed. Really that long? Indeed." >> fold(s,10) >> >> Output: >> >> A very lon >> g string i >> ndeed. Rea >> lly that l >> ong? Indee >> d. >> > > Wow, I feel like a dork. I should have done more research prior to > posting. Anyway, thanks for the advice. The trouble with Python is > that things make 'too much' sense. Loving this language. You might still need to tweak the above code as regards how line endings are handled. From http Wed Jul 15 13:25:25 2009 From: http (Paul Rubin) Date: 15 Jul 2009 10:25:25 -0700 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <7x4otdeu4a.fsf@ruckus.brouhaha.com> Hrvoje Niksic writes: > > While everyone's trying to tell the OP how to workaround the missing > > xor operator, nobody answered the question "why is there no [boolean] > > xor operator ?". > > Probably because there isn't one in C. The bitwise XOR operator, on the > other hand, exists in both C and Python. A non-bitwise XOR really sounds almost useless. From wbrehaut at mcsnet.ca Wed Jul 15 13:36:21 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 11:36:21 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <522e004c-77fc-445f-8951-ad54a0e664d4@d32g2000yqh.googlegroups.com> Message-ID: On Wed, 15 Jul 2009 13:37:22 +0200, Christian Heimes wrote: >pdpi wrote: >> On Jul 15, 12:08 am, Christian Heimes wrote: >>> Chris Rebert wrote: >>>> Using the xor bitwise operator is also an option: >>>> bool(x) ^ bool(y) >>> I prefer something like: >>> >>> bool(a) + bool(b) == 1 >>> >>> It works even for multiple tests (super xor): >>> >>> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >>> raise ValueError("Exactly one of a, b, c and d must be true") >>> >>> Christian >> >> "if bool(a) + bool(b) + bool(c) + bool(d) != 1:" is not equivalent to >> xor. 1 xor 1 xor 1 = 1 xor (1 xor 1) = 1 xor 0 = 1 (or = (1 xor 1) xor >> 1 = 0 xor 1 = 1 if you assicate to the left) > >I'm well aware of the fact that I've described something differently. >'xor' can be explained as 'check if exactly one element of two elements >is true'. My algorithms describes a super xor, aka 'check if exactly one >element of n elements is true'. But that's not a "super xor" (commonly known as XOR). Rather than describing xor as: check if exactly one element of two elements is true describe it as: check if an odd number of two elements is true then you'll get the correct definition of "super xor": check if an odd number of n elements is true I.e., XOR determines parity, and: XOR = 0 if v has an even number of 1s and 1 if v has an odd number of 1s wayne >Christian From jeanmichel at sequans.com Wed Jul 15 13:43:20 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 19:43:20 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <87fxcxj54d.fsf@busola.homelinux.net> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <4A5E1538.7030107@sequans.com> Hrvoje Niksic wrote: [snip] > Note that in Python A or B is in fact not equivalent to not(not A and > not B). > >>> l = [(True, True), (True, False), (False, True), (False, False)] >>> for p in l: ... p[0] or p[1] ... True True True False >>> for p in l: ... not(not p[0] and not p[1]) ... True True True False >>> Did I make twice the same obvious error ? JM From emile at fenx.com Wed Jul 15 13:48:42 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Jul 2009 10:48:42 -0700 Subject: Python Equivalent for dd & fold In-Reply-To: <4A5E1082.8090901@mrabarnett.plus.com> References: <4A5E1082.8090901@mrabarnett.plus.com> Message-ID: On 7/15/2009 10:23 AM MRAB said... >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: >>> seldan24 wrote: >>>> what can I use as the equivalent for the Unix 'fold' command? >>> def fold(s,len): >>> while s: >>> print s[:len] >>> s=s[len:] >>> > You might still need to tweak the above code as regards how line endings > are handled. You might also want to tweak it if the strings are _really_ long to simply slice out the substrings as opposed to reassigning the balance to a newly created s on each iteration. Emile From emile at fenx.com Wed Jul 15 13:55:22 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Jul 2009 10:55:22 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5E1538.7030107@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On 7/15/2009 10:43 AM Jean-Michel Pichavant said... > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> > >>> l = [(True, True), (True, False), (False, True), (False, False)] > >>> for p in l: > ... p[0] or p[1] > ... > True > True > True > False > >>> for p in l: > ... not(not p[0] and not p[1]) > ... > True > True > True > False > >>> > Did I make twice the same obvious error ? No -- but in the not(not... example it doesn't short-circuit. Emile From rodriguealcazar at gmail.com Wed Jul 15 13:55:34 2009 From: rodriguealcazar at gmail.com (Rodrigue) Date: Wed, 15 Jul 2009 10:55:34 -0700 (PDT) Subject: python first assignment of a global variable Message-ID: Hi all, I came accross a strange behaviour in python today. Here is a simple example to describe my situation: MY_GLOBAL = '' def a(): print 'global is: ', MY_GLOBAL def b(): try: MY_GLOBAL += 'bla' except Exception, e: print 'b: ', e def c(): try: global MY_GLOBAL MY_GLOBAL += 'bla' except Exception, e: print 'c: ', e def d(): try: if not MY_GLOBAL: print 'not my global' except Exception, e: print 'd: ', e def e(): try: if not MY_GLOBAL: MY_GLOBAL = '' except Exception, e: print 'e: ', e def f(): global MY_GLOBAL if not MY_GLOBAL: MY_GLOBAL = '' def e_raise(): if not MY_GLOBAL: MY_GLOBAL = 'bla' if __name__ == "__main__": a() b() c() d() e() f() e_raise() And here is the output I get: global is: b: local variable 'MY_GLOBAL' referenced before assignment e: local variable 'MY_GLOBAL' referenced before assignment Traceback (most recent call last): File "glo.py", line 49, in e_raise() File "glo.py", line 39, in e_raise if not MY_GLOBAL: UnboundLocalError: local variable 'MY_GLOBAL' referenced before assignment Now, I was reading that page http://stackoverflow.com/questions/370357/python-variable-scope-question and found (understood) only part of the behaviour that could explain the output. Basically, I was very surprised to discover that e() raises an exception, but even more that e_raise() points to if not MY_GLOBAL Is the problem not really when I assign? My assumption is that some reordering is happening behind the scenes that creates a situation similar to the += which assigns hence expects to be at the local level. I would love some enlightenment here Rodrigue From milesck at umich.edu Wed Jul 15 13:57:20 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 13:57:20 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5E1538.7030107@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Jul 15, 2009, at 1:43 PM, Jean-Michel Pichavant wrote: > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> > >>> l = [(True, True), (True, False), (False, True), (False, False)] > >>> for p in l: > ... p[0] or p[1] > [snip] > Did I make twice the same obvious error ? Try again with: l = [('foo','bar'), ('foo', ''), ('', 'bar'), ('', '')] -Miles From milesck at umich.edu Wed Jul 15 14:10:50 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 14:10:50 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Jul 15, 2009, at 1:55 PM, Emile van Sebille wrote: > On 7/15/2009 10:43 AM Jean-Michel Pichavant said... >> Hrvoje Niksic wrote: >> [snip] >>> Note that in Python A or B is in fact not equivalent to not(not A >>> and >>> not B). >>> >> Did I make twice the same obvious error ? > > No -- but in the not(not... example it doesn't short-circuit. No; like 'A or B', 'not (not A and not B)' does in fact short-circuit if A is True. (The 'and' condition does not have to evaluate the right operand when 'not A' is False.) -Miles From tjreedy at udel.edu Wed Jul 15 14:14:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 15 Jul 2009 14:14:55 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5DA247.3040606@timgolden.me.uk> References: <4A5DA247.3040606@timgolden.me.uk> Message-ID: Tim Golden wrote: > I was pondering on this yesterday, and the only case I've > come across in my code -- and it's reasonably common -- > is checking that one and only one of two params has been > passed. I have code which wants, say, an id or a name > but doesn't want both. It's hardly difficult to write the > check even now, but an built-in xor would make it fractionally > cleaner. I think I would just have one parameter id_name or identifier so no check is needed. This is a common idiom in Python where names are not typed. Example: param 'source' is a string (with a file name to be opened) or an already opened file. If you want two local vars inside the function, that should be kept private to the function. tjr From emile at fenx.com Wed Jul 15 14:14:57 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Jul 2009 11:14:57 -0700 Subject: python first assignment of a global variable In-Reply-To: References: Message-ID: On 7/15/2009 10:55 AM Rodrigue said... > I came accross a strange behaviour in python today. Here is a simple > example to describe my situation: > > MY_GLOBAL = '' > def e_raise(): > if not MY_GLOBAL: > MY_GLOBAL = 'bla' > > Traceback (most recent call last): > File "glo.py", line 49, in > e_raise() > File "glo.py", line 39, in e_raise > if not MY_GLOBAL: > UnboundLocalError: local variable 'MY_GLOBAL' referenced before > assignment the def prepares the function for subsequent use. At this prep time, MY_GLOBAL, by virtue of being assigned to later in the function, and the absence of a global statement, is identified as a local variable. Later, at execution time, testing MY_GLOBAL fails as it's not yet been assigned to. HTH, Emile From milesck at umich.edu Wed Jul 15 14:17:27 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 14:17:27 -0400 Subject: python first assignment of a global variable In-Reply-To: References: Message-ID: <4C9A140E-7BAB-49F5-97B7-FAD86DD0E2CF@umich.edu> On Jul 15, 2009, at 1:55 PM, Rodrigue wrote: > Basically, I was very surprised to discover that e() raises an > exception, but even more that e_raise() points to > if not MY_GLOBAL Is the problem not really when I assign? > > My assumption is that some reordering is happening behind the scenes > that creates a situation similar to the += which assigns hence expects > to be at the local level. The determination of whether a name is a reference to a local or global variable is made at compile time. When a function contains a single assignment (or augmented assignment) to a name, the compiler generates bytecode such that all references to that name within the function will be looked up in the local scope only, including those before the assignment statement. -Miles From pfeldman at verizon.net Wed Jul 15 14:19:18 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Wed, 15 Jul 2009 11:19:18 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> References: <24485116.post@talk.nabble.com> <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <24486661.post@talk.nabble.com> <4A5CFAC4.7020300@stoneleaf.us> <4A5CFF83.9070301@mrabarnett.plus.com> <0bcb7d71-6740-497a-bcf2-6139ea0c64ab@o15g2000yqm.googlegroups.com> Message-ID: <24503248.post@talk.nabble.com> I did initially ask for an infix xor operator, but eventually gave up on this. I like the first of your two one-line solutions below; this is clean and easy to understand. Thanks! I'd still like to be able to write an expression like '(a and b) xor (c and d) xor (e and f)', but it looks as though that can't be done. Well, that's not exactly what you originally asked for. But it's still a one-liner: def xor(*args): return bool(sum(map(bool, args)) % 2) or perhaps def xor(*args): return bool(len(filter(None, args)) & 1) -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24503248.html Sent from the Python - python-list mailing list archive at Nabble.com. From milesck at umich.edu Wed Jul 15 14:24:23 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 15 Jul 2009 14:24:23 -0400 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: On Jul 14, 2009, at 5:06 PM, David Bolen wrote: > Are you sure? It seems to restrict them in the same block, but not in > the entire file. At least I was able to use both space and tab > indented blocks in the same file with Python 3.0 and 3.1. It seems to me that, within an indented block, Python 3.1 requires that you are consistent in your use of indentation characters *for that indentation level*. For example, the following code seems to be allowed: def foo(): if True: x = 1 else: x = 2 return x But replacing any of the first tabs on each line with 8 spaces (without replacing them all), which previously would have been allowed, is now an error. -Miles From wbrehaut at mcsnet.ca Wed Jul 15 14:29:54 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 12:29:54 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" >wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to >> have an 'xor' operator as well. > >Hmm. I don't think 'nice' is sufficient. You'd need to make the case >that it's sufficiently useful to justify adding a new keyword 'xor' to >the language; I suspect that would be an uphill struggle. :) > >I'll just note that: > >(1) It's easy to emulate xor: 'x xor y' <-> bool(x) != bool(y) > >(2) 'and' and 'or' are special in that they have useful short- >circuiting behaviour; xor doesn't have this property (that is, you >always need to evaluate *both* operands to determine the result). > >I'd also guess that 'xor' would be much less used than 'and' or 'or', >but maybe that's just a reflection of the sort of code that I tend to >write. You're right about that!. It's used everywhere in: - coding and encryption theory (and practice) (e.g., http://www.mathcs.emory.edu/~whalen/Hash/Hash_Articles/IEEE/XOR-based%20hash%20functions.pdf) - emulation and simulation of hardware (since all but the most trivial logic circuits are likely to include XOR-gates) - hence, for design of new architectures or simulators or virtual machines and simplification of existing ones--(e.g., http://www.date-conference.com/archive/conference/proceedings/PAPERS/1999/DATE99/PDFFILES/05A_6.PDF) and http://bochs.sourceforge.net/Virtualization_Without_Hardware_Final.pdf which includes: "The answer relies on the observation that subtracting an integer value from 0xFFFFFFFF gives the same result as XOR-ing that same value to 0xFFFFFFFF." And, perhaps the most useful use of all, for Bouton's solution of the game of Nim--both for the proof that his strategy "solves" the game and for an easy implementation of a Nim-playing program--and the only operator needed is XOR (e.g., http://www.wordiq.com/definition/Nim). wayne From gerard.blais at gmail.com Wed Jul 15 14:40:32 2009 From: gerard.blais at gmail.com (MCIPERF) Date: Wed, 15 Jul 2009 11:40:32 -0700 (PDT) Subject: Catching control-C References: <0fe9e54d-a8bd-4fca-ac94-addce8c963e9@u16g2000pru.googlegroups.com> Message-ID: On Jul 9, 7:28?pm, Miles Kaufmann wrote: > On Jul 9, 2009, at 9:20 AM, Lie Ryan wrote: > > > Michael Mossey wrote: > >> I want to understand better what the "secret" is to responding to a > >> ctrl-C in any shape or form. > > > Are you asking: "when would the python interpreter process > > KeyboardInterrupt?" > > ... > > In single threaded python program, the currently running thread is > > always the main thread (which can handle KeyboardInterrupt). I believe > > SIGINT is checked at every ticks. But SIGINT cannot interrupt atomic > > operations (i.e. it cannot interrupt long operations that takes a ? > > single > > tick). > > Some otherwise atomic single-bytecode operations (like large integer ? > arithmetic) do manual checks for whether signals were raised (though ? > that won't help at all if the operation isn't on the main thread). > > > I believe a tick in python is equivalent to a single bytecode, but > > please correct me if I'm wrong. > > Not all opcodes qualify as a tick. ?In general, those opcodes that ? > cause control to remain in the eval loop (and not make calls to other ? > Python or C functions) don't qualify as ticks (but there are ? > exceptions, e.g. so that while True: pass is interruptible). ?In ? > Python/ceval.c: PyEval_EvalFrameEx(), those opcodes that don't end in ? > goto fast_next_opcode are ticks. > > Please correct me if _I'm_ wrong! :) > -Miles You don't need to do I/O. This works: try: process_forever() except KeyboardInterrupt: save critical stuff write nice messages I often wrap a large computational task like this, with the idea that the exception can let me exit safely, in my case by writing restart parameters and printing a summary pf progress to date. Gerry From amrita at iisermohali.ac.in Wed Jul 15 14:46:29 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Thu, 16 Jul 2009 00:16:29 +0530 (IST) Subject: mail Message-ID: <7683.210.212.36.65.1247683589.squirrel@www.iisermohali.ac.in> Dear all, Sorry that I am disturbing you all again and again but this is the way I am trying to solve my problem:--- >>> import re >>> exp = re.compile("CA") >>> infile = open("file1.txt") >>> for line in infile: ... values = re.split("\s+", line) ... if exp.search(line): ... print ("%s %s CA = %s" %(values[2], values[3], values[6])) ... with this it is giving the output like:---- 8 ALA CA = 54.67 15 ALA CA = 52.18 21 ALA CA = 54.33 23 ALA CA = 55.84 33 ALA CA = 55.58 38 ALA CA = 54.33 which is all right but i want CB and C value also in each row and it should take value from 5th column infront of them, file is something lookin like:----- 47 8 ALA H H 7.85 0.02 1 48 8 ALA HA H 2.98 0.02 1 49 8 ALA HB H 1.05 0.02 1 50 8 ALA C C 179.39 0.3 1 51 8 ALA CA C 54.67 0.3 1 52 8 ALA CB C 18.85 0.3 1 53 8 ALA N N 123.95 0.3 1 107 15 ALA H H 8.05 0.02 1 108 15 ALA HA H 4.52 0.02 1 109 15 ALA HB H 1.29 0.02 1 110 15 ALA C C 177.18 0.3 1 111 15 ALA CA C 52.18 0.3 1 112 15 ALA CB C 20.64 0.3 1 113 15 ALA N N 119.31 0.3 1 154 21 ALA H H 7.66 0.02 1 155 21 ALA HA H 4.05 0.02 1 156 21 ALA HB H 1.39 0.02 1 157 21 ALA C C 179.35 0.3 1 158 21 ALA CA C 54.33 0.3 1 159 21 ALA CB C 17.87 0.3 1 160 21 ALA N N 123.58 0.3 1 169 23 ALA H H 8.78 0.02 1 170 23 ALA HA H 4.14 0.02 1 171 23 ALA HB H 1.62 0.02 1 172 23 ALA C C 179.93 0.3 1 173 23 ALA CA C 55.84 0.3 1 174 23 ALA CB C 17.55 0.3 1 175 23 ALA N N 120.16 0.3 1 232 33 ALA H H 7.57 0.02 1 233 33 ALA HA H 3.89 0.02 1 234 33 ALA HB H 1.78 0.02 1 235 33 ALA C C 179.24 0.3 1 236 33 ALA CA C 55.58 0.3 1 237 33 ALA CB C 19.75 0.3 1 238 33 ALA N N 121.52 0.3 1 269 38 ALA H H 8.29 0.02 1 270 38 ALA HA H 4.04 0.02 1 271 38 ALA HB H 1.35 0.02 1 272 38 ALA C C 178.95 0.3 1 273 38 ALA CA C 54.33 0.3 1 274 38 ALA CB C 18.30 0.3 1 275 38 ALA N N 120.62 0.3 1 I just want that it will give output something like:----- 8 ALA C = 179.39 CA = 54.67 CB = 18.85 15 ALA C = 177.18 CA = 52.18 CB = 20.64 21 ALA C = 179.35 CA = 54.33 CB = 17.87..... so first it will write the position of the amino acid(given by second column)then amino acid here it is ALA and then the corresponding value of C, CA and CB from 5th colum for each position of ALA. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From robert.kern at gmail.com Wed Jul 15 14:47:28 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 15 Jul 2009 13:47:28 -0500 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On 2009-07-15 13:29, Wayne Brehaut wrote: > On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson > wrote: > >> On Jul 14, 7:25 pm, "Dr. Phillip M. Feldman" >> wrote: >>> Current Boolean operators are 'and', 'or', and 'not'. It would be nice to >>> have an 'xor' operator as well. >> Hmm. I don't think 'nice' is sufficient. You'd need to make the case >> that it's sufficiently useful to justify adding a new keyword 'xor' to >> the language; I suspect that would be an uphill struggle. :) >> >> I'll just note that: >> >> (1) It's easy to emulate xor: 'x xor y'<-> bool(x) != bool(y) >> >> (2) 'and' and 'or' are special in that they have useful short- >> circuiting behaviour; xor doesn't have this property (that is, you >> always need to evaluate *both* operands to determine the result). >> >> I'd also guess that 'xor' would be much less used than 'and' or 'or', >> but maybe that's just a reflection of the sort of code that I tend to >> write. > > You're right about that!. It's used everywhere in: > > - coding and encryption theory (and practice) (e.g., > http://www.mathcs.emory.edu/~whalen/Hash/Hash_Articles/IEEE/XOR-based%20hash%20functions.pdf) > - emulation and simulation of hardware (since all but the most trivial > logic circuits are likely to include XOR-gates) > - hence, for design of new architectures or simulators or virtual > machines and simplification of existing ones--(e.g., > http://www.date-conference.com/archive/conference/proceedings/PAPERS/1999/DATE99/PDFFILES/05A_6.PDF) > and > http://bochs.sourceforge.net/Virtualization_Without_Hardware_Final.pdf > which includes: > > "The answer relies on the observation that subtracting an integer > value from 0xFFFFFFFF gives the same result as XOR-ing that same value > to 0xFFFFFFFF." > > And, perhaps the most useful use of all, for Bouton's solution of the > game of Nim--both for the proof that his strategy "solves" the game > and for an easy implementation of a Nim-playing program--and the only > operator needed is XOR (e.g., http://www.wordiq.com/definition/Nim). All of those can use the bitwise XOR operator, not the boolean XOR. Python already has the ^ operator for those purposes. -- 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 dickinsm at gmail.com Wed Jul 15 14:51:44 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Wed, 15 Jul 2009 11:51:44 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Jul 15, 7:29?pm, Wayne Brehaut wrote: > On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: > >I'd also guess that 'xor' would be much less used than 'and' or 'or', > >but maybe that's just a reflection of the sort of code that I tend to > >write. > > You're right about that!. It's used everywhere in: > [snip examples and references] Those examples are (almost) all about the *bitwise* xor operator, which exists in Python ('^') and, as you point out, has no shortage of good uses. The discussion was about a *logical* xor, to parallel the existing 'and' and 'or' operators. -- Mark From anthony.tolle at gmail.com Wed Jul 15 14:53:23 2009 From: anthony.tolle at gmail.com (Anthony Tolle) Date: Wed, 15 Jul 2009 11:53:23 -0700 (PDT) Subject: missing 'xor' Boolean operator References: Message-ID: On Jul 14, 2:25?pm, "Dr. Phillip M. Feldman" wrote: > Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to > have an 'xor' operator as well. My $0.02 on this discussion: There would be nothing gained by having non-bitwise XOR operator. You can't short-circuit XOR, because you must evaluate all operands to produce a result. Consequently, returning the "true" item also doesn't make much sense. XOR is closer to the the logical NOT operator than it is to AND or OR. Here's my own take on a function that can handle any number of arguments (it should probably raise an exception for 0 or 1 arguments, but I'm lazy): def xor(*operands): if operands: operands = list(operands) a = bool(operands.pop(0)) while operands: b = bool(operands.pop(0)) if a: if b: a = False elif b: a = True return a return False From jeanmichel at sequans.com Wed Jul 15 15:05:16 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 15 Jul 2009 21:05:16 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: <4A5E286C.5060607@sequans.com> Miles Kaufmann wrote: > > On Jul 15, 2009, at 1:43 PM, Jean-Michel Pichavant wrote: > >> Hrvoje Niksic wrote: >> [snip] >>> Note that in Python A or B is in fact not equivalent to not(not A and >>> not B). >>> >> >>> l = [(True, True), (True, False), (False, True), (False, False)] >> >>> for p in l: >> ... p[0] or p[1] >> [snip] >> Did I make twice the same obvious error ? > > > Try again with: > > l = [('foo','bar'), ('foo', ''), ('', 'bar'), ('', '')] > > -Miles > Didn't know that. So if I resume: - not 'foo' => False - 'foo' or 'foo' => 'foo' I may be missing something, but honestly, Guido must have smoked some heavy stuff to write such logic, has he ? Let's play again False or 'foo' => 'foo' False and 'foo' => False So funny. I would have expected boolean operators to return a boolean value. I should have read the f*** manual (this is an anticipated RTFM counter-attack). Anyway Miles, thanks for the update. JM From invalid at invalid Wed Jul 15 15:05:30 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 15 Jul 2009 14:05:30 -0500 Subject: mail References: Message-ID: On 2009-07-15, amrita at iisermohali.ac.in wrote: > Sorry that I am disturbing you all again and again but this is the way I > am trying to solve my problem:--- We could probably be a lot more helpful if you would keep these postings all in a single thread so that people who didn't read the first postings knew what you were talking about in subsequent postings. >>>> import re >>>> exp = re.compile("CA") > [...] > > file is something lookin like:----- > > 47 8 ALA H H 7.85 0.02 1 [...] IMO you're making a mistake using the "re" module for this task. I think you'd be a lot better off if you just used the string type's split method to split each line up into fields and processed the individual fields: for line in inputfile: field = line.split() if field[2] == "CA": <> elif field[2] == "soemthing-else" <> <> -- Grant Edwards grante Yow! If I felt any more at SOPHISTICATED I would DIE visi.com of EMBARRASSMENT! From jcd at sdf.lonestar.org Wed Jul 15 15:40:36 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Wed, 15 Jul 2009 15:40:36 -0400 Subject: mail In-Reply-To: <7683.210.212.36.65.1247683589.squirrel@www.iisermohali.ac.in> References: <7683.210.212.36.65.1247683589.squirrel@www.iisermohali.ac.in> Message-ID: <1247686836.11677.196.camel@aalcdl07> On Thu, 2009-07-16 at 00:16 +0530, amrita at iisermohali.ac.in wrote: > Dear all, > > Sorry that I am disturbing you all again and again but this is the way I > am trying to solve my problem:--- > > >>> import re > >>> exp = re.compile("CA") > >>> infile = open("file1.txt") > >>> for line in infile: > ... values = re.split("\s+", line) > ... if exp.search(line): > ... print ("%s %s CA = %s" %(values[2], values[3], values[6])) > ... > with this it is giving the output like:---- > > 8 ALA CA = 54.67 > 15 ALA CA = 52.18 > 21 ALA CA = 54.33 > 23 ALA CA = 55.84 > 33 ALA CA = 55.58 > 38 ALA CA = 54.33 > > which is all right but i want CB and C value also in each row and it > should take value from 5th column infront of them, file is something > lookin like:----- > > 47 8 ALA H H 7.85 0.02 1 > 48 8 ALA HA H 2.98 0.02 1 > 49 8 ALA HB H 1.05 0.02 1 > 50 8 ALA C C 179.39 0.3 1 > 51 8 ALA CA C 54.67 0.3 1 > 52 8 ALA CB C 18.85 0.3 1 > 53 8 ALA N N 123.95 0.3 1 > 107 15 ALA H H 8.05 0.02 1 > 108 15 ALA HA H 4.52 0.02 1 > 109 15 ALA HB H 1.29 0.02 1 > 110 15 ALA C C 177.18 0.3 1 > 111 15 ALA CA C 52.18 0.3 1 > 112 15 ALA CB C 20.64 0.3 1 > 113 15 ALA N N 119.31 0.3 1 > 154 21 ALA H H 7.66 0.02 1 > 155 21 ALA HA H 4.05 0.02 1 > 156 21 ALA HB H 1.39 0.02 1 > 157 21 ALA C C 179.35 0.3 1 > 158 21 ALA CA C 54.33 0.3 1 > 159 21 ALA CB C 17.87 0.3 1 > 160 21 ALA N N 123.58 0.3 1 > 169 23 ALA H H 8.78 0.02 1 > 170 23 ALA HA H 4.14 0.02 1 > 171 23 ALA HB H 1.62 0.02 1 > 172 23 ALA C C 179.93 0.3 1 > 173 23 ALA CA C 55.84 0.3 1 > 174 23 ALA CB C 17.55 0.3 1 > 175 23 ALA N N 120.16 0.3 1 > 232 33 ALA H H 7.57 0.02 1 > 233 33 ALA HA H 3.89 0.02 1 > 234 33 ALA HB H 1.78 0.02 1 > 235 33 ALA C C 179.24 0.3 1 > 236 33 ALA CA C 55.58 0.3 1 > 237 33 ALA CB C 19.75 0.3 1 > 238 33 ALA N N 121.52 0.3 1 > 269 38 ALA H H 8.29 0.02 1 > 270 38 ALA HA H 4.04 0.02 1 > 271 38 ALA HB H 1.35 0.02 1 > 272 38 ALA C C 178.95 0.3 1 > 273 38 ALA CA C 54.33 0.3 1 > 274 38 ALA CB C 18.30 0.3 1 > 275 38 ALA N N 120.62 0.3 1 > > I just want that it will give output something like:----- > > 8 ALA C = 179.39 CA = 54.67 CB = 18.85 > 15 ALA C = 177.18 CA = 52.18 CB = 20.64 > 21 ALA C = 179.35 CA = 54.33 CB = 17.87..... > > so first it will write the position of the amino acid(given by second > column)then amino acid here it is ALA and then the corresponding value of > C, CA and CB from 5th colum for each position of ALA. > Your program is structured wrong for doing what you want. Right now you are essentially saying, "for each line in the input file, print something", but each line of the input file only has part of the information you want. Instead, you should create a data structure that gathers the pieces you want, and only print once you have all of those pieces in place. Step one, create your data structure, so the data is grouped the way you want it, not by line: >>> thingies = {} # create a dictionary >>> for line in infile: >>> data = line.split() >>> >>> if data[1] not in thingies: >>> # group data by data[1] >>> thingies[data[1]] = {} >>> >>> thingies[data[1]][data[3]] = data[5] Step two, extract the data from the list: >>> for key, data in thingies.items(): >>> print key, >>> for entry in data >>> print '%s = %s' % (entry, data[entry]), This should do what you want, minus some formatting issues. Cheers, Cliff From stefan_ml at behnel.de Wed Jul 15 15:48:13 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 15 Jul 2009 21:48:13 +0200 Subject: compiling python In-Reply-To: References: Message-ID: <4a5e327d$0$31339$9b4e6d93@newsspool4.arcor-online.net> Mag Gam wrote: > At my university we are trying to compile python with --enable-shared Is there a reason why you need to compile the CPython interpreter yourself? > however when I do a make many things fail. Is it a good idea to > compile python with shared libraries? Perfectly fine, Linux distros do that by default, for example. "many things fail" is not a very detailed problem description, though. Could you state more exactly what you do and provide the error messages that you see? Stefan From wbrehaut at mcsnet.ca Wed Jul 15 15:55:08 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 13:55:08 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: On Wed, 15 Jul 2009 11:51:44 -0700 (PDT), Mark Dickinson wrote: >On Jul 15, 7:29?pm, Wayne Brehaut wrote: >> On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >> >I'd also guess that 'xor' would be much less used than 'and' or 'or', >> >but maybe that's just a reflection of the sort of code that I tend to >> >write. >> >> You're right about that!. It's used everywhere in: >> [snip examples and references] > >Those examples are (almost) all about the *bitwise* xor operator, >which exists in Python ('^') and, as you point out, has no shortage of >good uses. The discussion was about a *logical* xor, to parallel the >existing 'and' and 'or' operators. I thought you were saying your program domains didn't include a lot of requirements for xor in general, rather than just no uses for Boolean xor--and I'm used to thinking of xor on binary vectors as "Boolean" anyway so would still have been confused. The most common non-binary-bit-wise xor is the general "symmetric difference" of sets, most likely to be useful in list or dictionary processing or database-like contexts. Please see my suggested use-case for Steven below. wayne From miheavner at gmail.com Wed Jul 15 15:55:56 2009 From: miheavner at gmail.com (mheavner) Date: Wed, 15 Jul 2009 12:55:56 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? Message-ID: I'm using multiprocessing to spawn several subprocesses, each of which uses a very large data structure (making it impractical to pass it via pipes / pickling). I need to allocate this structure once when the process is created and have it remain in memory for the duration of the process. The way the multiprocessing module is set up, only the 'run' method runs within the subprocess - so creating a wrapper class with a constructor that allocates the structure in __init__ will not work, as far as I know, as this will still be within the parent process. If I were working in C/C++, I would declare the variable "static" within the function body - is there any way with the multiprocessing module to have persistent data members within subprocesses? Any ideas?? Thanks, Matt From drozzy at gmail.com Wed Jul 15 16:00:06 2009 From: drozzy at gmail.com (drozzy) Date: Wed, 15 Jul 2009 13:00:06 -0700 (PDT) Subject: Reading a large csv file References: <1cbd6f830906222017m694bc738m40a46e2851381e@mail.gmail.com> <1cbd6f830906222227q7ec2a89ke3d4805c9ace80e8@mail.gmail.com> <4A41D1FD.7080004@simplistix.co.uk> Message-ID: On Jun 26, 6:47?am, Mag Gam wrote: > Thankyou everyone for the responses! I took some of your suggestions > and my loading sped up by 25% what a useless post... From hniksic at xemacs.org Wed Jul 15 16:00:52 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 15 Jul 2009 22:00:52 +0200 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <87bpnliumj.fsf@busola.homelinux.net> Jean-Michel Pichavant writes: > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> >>>> l = [(True, True), (True, False), (False, True), (False, False)] >>>> for p in l: > ... p[0] or p[1] [...] Try with a different data set, for example: >>> 10 or 20 10 >>> not(not 10 and not 20) True From deets at nospam.web.de Wed Jul 15 16:22:46 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 22:22:46 +0200 Subject: Persistent variable in subprocess using multiprocessing? In-Reply-To: References: Message-ID: <7c6s4nF25vs08U1@mid.uni-berlin.de> mheavner schrieb: > I'm using multiprocessing to spawn several subprocesses, each of which > uses a very large data structure (making it impractical to pass it via > pipes / pickling). I need to allocate this structure once when the > process is created and have it remain in memory for the duration of > the process. The way the multiprocessing module is set up, only the > 'run' method runs within the subprocess - so creating a wrapper class > with a constructor that allocates the structure in __init__ will not > work, as far as I know, as this will still be within the parent > process. > > If I were working in C/C++, I would declare the variable "static" > within the function body - is there any way with the multiprocessing > module to have persistent data members within subprocesses? Works for me, at least under OSX (and I presume *nixes in general work.) No idea about Windows. The thing to keep in mind is that forking is used, and that interpreter-state up to the moment of the fork is the same for all subprocesses. from multiprocessing import Process class MyProcess(Process): def __init__(self, huge_shared_state): self.huge_shared_state = huge_shared_state super(MyProcess, self).__init__() def run(self): print self.name, len(self.huge_shared_state) shared_state = range(1000000) processes = [] for i in xrange(10): p = MyProcess(shared_state) p.start() processes.append(p) for p in processes: p.join() Diez From db3l.net at gmail.com Wed Jul 15 16:26:17 2009 From: db3l.net at gmail.com (David Bolen) Date: Wed, 15 Jul 2009 16:26:17 -0400 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: Miles Kaufmann writes: > On Jul 14, 2009, at 5:06 PM, David Bolen wrote: >> Are you sure? It seems to restrict them in the same block, but not in >> the entire file. At least I was able to use both space and tab >> indented blocks in the same file with Python 3.0 and 3.1. > > It seems to me that, within an indented block, Python 3.1 requires > that you are consistent in your use of indentation characters *for > that indentation level*. For example, the following code seems to be > allowed: Um, right - in other words, what I said :-) -- David From rodriguealcazar at gmail.com Wed Jul 15 16:27:11 2009 From: rodriguealcazar at gmail.com (Rodrigue) Date: Wed, 15 Jul 2009 13:27:11 -0700 (PDT) Subject: python first assignment of a global variable References: Message-ID: <47c62f6f-cf9a-4f74-ad71-5f2eccc8c797@h2g2000yqg.googlegroups.com> > MY_GLOBAL, by virtue of being assigned to later in the function, and the > absence of a global statement, is identified as a local variable. > When a function contains a > single assignment (or augmented assignment) to a name, the compiler > generates bytecode such that all references to that name within the > function will be looked up in the local scope only Alright. I didn't know that. I thought the entire scope (local, then global) was considered in every situation. It does explain the observed behaviour then. I'm surprised I never bumped into that before, but I'm glad I learnt something new about python today. Thanks Emile and Miles for the explanation! From georgeoliverGO at gmail.com Wed Jul 15 16:39:37 2009 From: georgeoliverGO at gmail.com (George Oliver) Date: Wed, 15 Jul 2009 13:39:37 -0700 (PDT) Subject: interactive fiction in Python? Message-ID: <5c97224b-99d4-469b-90cb-1c04b1e1e9a0@m11g2000yqh.googlegroups.com> hi, I'm just curious who might be working on interactive fiction modules in the style of Inform or TADS for Python. I've seen a few threads on this list [1] (among many that mention IF tangentially), and there are old projects like PUB and PAWS. There are some newer potential projects such as Curveship as well. In any case I'd like to see what's out there. thanks, George [1]: http://bit.ly/Python_Text_Adventure_Authoring_System http://bit.ly/simple_interactive_fiction_engine http://bit.ly/adventure_engines_in_Python From perfreem at gmail.com Wed Jul 15 16:54:02 2009 From: perfreem at gmail.com (per) Date: Wed, 15 Jul 2009 13:54:02 -0700 (PDT) Subject: allowing output of code that is unittested? Message-ID: hi all, i am using the standard unittest module to unit test my code. my code contains several print statements which i noticed are repressed when i call my unit tests using: if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(TestMyCode) unittest.TextTestRunner(verbosity=2).run(suite) is there a way to allow all the print statements in the code that is being run by the unit test functions to be printed to stdio? i want to be able to see the output of the tested code, in addition to the output of the unit testing framework. thank you. From jkn_gg at nicorp.f9.co.uk Wed Jul 15 17:25:24 2009 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Wed, 15 Jul 2009 14:25:24 -0700 (PDT) Subject: Ann: Google releases Python-based open-source NX server Message-ID: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> Google quietly releases open-source NX server ...written in Python, apparently Neatx can be downloaded from Google's code repository: Regards J^n From magobin at gmail.com Wed Jul 15 17:26:53 2009 From: magobin at gmail.com (Alex) Date: Wed, 15 Jul 2009 14:26:53 -0700 (PDT) Subject: How to Force exiting from program/script Message-ID: hi at all, I have made a script with a while loop and I want that after 30 seconds the program stop and exit . But the code like this doesn't run: In the Console I can see work so that function is correctly called... #Function to exit def exit(): print "work" raise SystemExit() t = threading.Timer(30.0, exit) t.start() # Loop while True: ...many lines.... From deets at nospam.web.de Wed Jul 15 17:39:01 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 23:39:01 +0200 Subject: How to Force exiting from program/script In-Reply-To: References: Message-ID: <7c70jlF25pt2sU1@mid.uni-berlin.de> Alex schrieb: > hi at all, > I have made a script with a while loop and I want that after 30 > seconds the program stop and exit . But the code like this doesn't > run: > In the Console I can see work so that function is correctly called... > > #Function to exit > def exit(): > print "work" > raise SystemExit() > t = threading.Timer(30.0, exit) > t.start() > > # Loop > while True: > ...many lines.... > This works for me: import threading import time def work(): while True: pass t = threading.Thread(target=work) t.setDaemon(True) t.start() time.sleep(10) raise SystemExit The trick is to raise the SystemExit in the main-thread. Diez From deets at nospam.web.de Wed Jul 15 17:46:52 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 15 Jul 2009 23:46:52 +0200 Subject: allowing output of code that is unittested? In-Reply-To: References: Message-ID: <7c712cF26q447U1@mid.uni-berlin.de> per schrieb: > hi all, > > i am using the standard unittest module to unit test my code. my code > contains several print statements which i noticed are repressed when i > call my unit tests using: > > if __name__ == '__main__': > suite = unittest.TestLoader().loadTestsFromTestCase(TestMyCode) > unittest.TextTestRunner(verbosity=2).run(suite) > > is there a way to allow all the print statements in the code that is > being run by the unit test functions to be printed to stdio? i want > to be able to see the output of the tested code, in addition to the > output of the unit testing framework. I use nosetests to discover & run tests, and that has an "-s"-option that does exactly that. Diez From mobiledreamers at gmail.com Wed Jul 15 18:01:15 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Wed, 15 Jul 2009 15:01:15 -0700 Subject: Ann: Google releases Python-based open-source NX server In-Reply-To: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> Message-ID: On Wed, Jul 15, 2009 at 2:25 PM, jkn wrote: > Google quietly releases open-source NX server ...written in Python, > apparently > > Google_quietly_releases_open_source_NX_server?taxonomyId=88> > > Neatx can be downloaded from Google's code repository: > > > > Regards > J^n > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.com Wed Jul 15 18:31:38 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 15 Jul 2009 23:31:38 +0100 Subject: Colour of output text References: <03c4ceed-c49b-4dd0-a585-e6169b02e0eb@26g2000yqk.googlegroups.com> Message-ID: On Wed, 15 Jul 2009 17:03:30 +0200, Jean-Michel Pichavant wrote: >> Hard-coding control/escape sequences is just lame. Use the curses modules >> to obtain the correct sequences for the terminal. >> >> > As the OP I'm really interested in doing so. I currently have all my > colors hard-coded. > Now It may be lame but as soon as I call curses.initscr(), it's just > messing up with my terminal, Use curses.setupterm() to locate and parse the terminfo/termcap entry without entering "curses mode". Most curses functions won't work without calling initscr(), but the terminfo functions will. > moreover I didn't figure out how to "print > 'hello'" using curses color codes. > Anyone has an example ? I'm pretty sure it may fit in one line. #!/usr/bin/env python import curses curses.setupterm() setaf = curses.tigetstr('setaf') if not setaf: setaf = '' print (curses.tparm(setaf,1) + "hello, " + curses.tparm(setaf,2) + "world" + curses.tparm(setaf,0)) From mmanns at gmx.net Wed Jul 15 18:36:51 2009 From: mmanns at gmx.net (mmanns at gmx.net) Date: Thu, 16 Jul 2009 00:36:51 +0200 Subject: Package for fast plotting of many data points in Python? References: <0734dc45-d8a0-4f28-b945-f9e179f30f5c@h11g2000yqb.googlegroups.com> Message-ID: <20090716003651.09bca73c@Knock> On Sat, 11 Jul 2009 15:33:32 -0700 (PDT) Daniel Platz wrote: > thanks for your repleys. I have tried matplotlib but it is extremely > slow. I think it is more optimized for good looking plots instead of > speed. I do not know the Python bindings of gnuplot and Veusz. To > clarify the issue again, by 25000 data points I mean 25000 pixels, > i.e. corresponding (x,y) pairs. Thus the mapping to one pixel on the > screen is not unique. Have you already tried out rpy? Martin From shai at platonix.com Wed Jul 15 18:45:57 2009 From: shai at platonix.com (Shai) Date: Wed, 15 Jul 2009 15:45:57 -0700 (PDT) Subject: Cleaning up after failing to contructing objects References: <5d8aaf63-0072-49a0-8a60-0cd5aff02128@b15g2000yqd.googlegroups.com> <42fbcee1-61b2-4ba8-a50a-39c9c0d2fe9c@k19g2000yqn.googlegroups.com> Message-ID: Since nobody else mentioned this... Python classes have a magic method called __del__ which is usually called just before an object is garbage-collected. Further, Python uses reference-counting to tell when an object is no longer accessible. This means that if your resource classes define __del__ methods, these will be called properly when the object using them is destroyed, and you don't need to write an explicit close() method for this. class Resource(object): def __init__(self, param): # acquire resource def __del__(self): # release resource not_my_responsibility = Resource(1) class Foo(object): def __init__(self): self.ref = not_my_responsibility # self.ref.__del__() will not be called as long as the module exists local = Resource(2) # local.__del__() will be called as soon as __init__ is exited self.held = Resource(3) # self.held.__del__() will be called when the object dies z = 1/0 # The exception in the initializer will kill the object, triggering some Resource.__del__() calls There are two caveats: 1) __del__ methods prevent instances of your class from being collected when they are involved in cyclical structures; this means if your structures start to get complex (sometimes a doubly-linked list is complex enough), you may find yourself leaking memory. 2) The bit about reference counting, which allows predictable destruction, holds for CPython, but not for Jython, and IIRC also not for IronPython (I don't know about PyPy or other implementations). It is a feature of the reference implementation, not the language definition. From wbrehaut at mcsnet.ca Wed Jul 15 18:54:00 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 16:54:00 -0600 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> Message-ID: <7bns55dch1pler31ai2ivdp1u6nk60lnfo@4ax.com> On Tue, 14 Jul 2009 11:47:41 -0700 (PDT), Mark Dickinson wrote: >On Jul 14, 7:25?pm, "Dr. Phillip M. Feldman" >wrote: >> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice to >> have an 'xor' operator as well. > >Hmm. I don't think 'nice' is sufficient. You'd need to make the case >that it's sufficiently useful to justify adding a new keyword 'xor' to >the language; I suspect that would be an uphill struggle. :) === 8< === And for the objects for which it *is* sufficiently useful (sets) the xor operator ^ is available: >>> cheese = set(['cheddar', 'limburger', 'stilton']) >>> stinky = set(['skunk', 'limburger', 'stilton', 'polecat', 'doggy-doo', 'civet']) >>> nasty = set(['doggy-doo', 'polecat', 'limburger', 'Perl']) >>> cheese & stinky # stinky cheese set(['limburger', 'stilton']) >>> cheese ^ stinky # either cheese or stinky but not both set(['doggy-doo', 'civet', 'polecat', 'skunk', 'cheddar']) >>> cheese ^ stinky ^ nasty # in an odd number of these sets (1 or 3) set(['civet', 'cheddar', 'Perl', 'limburger', 'skunk']) wayne From wbrehaut at mcsnet.ca Wed Jul 15 18:57:59 2009 From: wbrehaut at mcsnet.ca (Wayne Brehaut) Date: Wed, 15 Jul 2009 16:57:59 -0600 Subject: missing 'xor' Boolean operator References: Message-ID: On 15 Jul 2009 09:11:44 GMT, Steven D'Aprano wrote: >On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: > >> Current Boolean operators are 'and', 'or', and 'not'. It would be nice >> to have an 'xor' operator as well. > >I've often wished there was too, for the sake of completeness and >aesthetics, I'd love to be able to write: > >a xor b > >instead of defining a function xor(a, b). > >Unfortunately, outside of boolean algebra and simulating electrical >circuits, I can't think of any use-cases for an xor operator. Do you have >any? Since xor in set theory is the symmetric difference, perhaps we'd like to know all items in exactly one of two word lists or dictionaries, or anything else we could easily set-ize: >>> cheese = set(['cheddar', 'limburger', 'stilton']) >>> stinky = set(['skunk', 'limburger', 'stilton', 'polecat', 'doggy-doo', 'civet']) >>> nasty = set(['doggy-doo', 'polecat', 'limburger', 'Perl']) >>> cheese & stinky # stinky cheese set(['limburger', 'stilton']) >>> cheese ^ stinky # either cheese or stinky but not both set(['doggy-doo', 'civet', 'polecat', 'skunk', 'cheddar']) >>> cheese ^ stinky ^ nasty # in an odd number of these sets (1 or 3) set(['civet', 'cheddar', 'Perl', 'limburger', 'skunk']) Who hasn't needed that occasionally? wayne From nobody at nowhere.com Wed Jul 15 19:01:37 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 00:01:37 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > So if I resume: > - not 'foo' => False > - 'foo' or 'foo' => 'foo' > > I may be missing something, but honestly, Guido must have smoked some > heavy stuff to write such logic, has he ? Several languages (e.g. Lisp, Bourne shell) behave the same way, i.e. "or" returns the first element which is considered true while "and" returns the last element provided that all preceding elements are considered true. > Let's play again > False or 'foo' => 'foo' > False and 'foo' => False > > So funny. I would have expected boolean operators to return a boolean > value. In Python, almost everything is a boolean value. Compare with Lisp, where everything is a boolean value: nil (the empty list) is false and everything else (including integer zero) is true. From clp2 at rebertia.com Wed Jul 15 19:02:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 16:02:13 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: Message-ID: <50697b2c0907151602j393935eas4d5e18394baaa639@mail.gmail.com> On Wed, Jul 15, 2009 at 3:57 PM, Wayne Brehaut wrote: > On 15 Jul 2009 09:11:44 GMT, Steven D'Aprano > wrote: > >>On Tue, 14 Jul 2009 11:25:08 -0700, Dr. Phillip M. Feldman wrote: >> >>> Current Boolean operators are 'and', 'or', and 'not'. ?It would be nice >>> to have an 'xor' operator as well. >> >>I've often wished there was too, for the sake of completeness and >>aesthetics, I'd love to be able to write: >> >>a xor b >> >>instead of defining a function xor(a, b). >> >>Unfortunately, outside of boolean algebra and simulating electrical >>circuits, I can't think of any use-cases for an xor operator. Do you have >>any? > > Since xor in set theory is the symmetric difference, ?perhaps we'd > like to know all items in exactly one of two word lists or > dictionaries, or anything else we could easily set-ize: > >>>> cheese = set(['cheddar', 'limburger', 'stilton']) >>>> stinky = set(['skunk', 'limburger', 'stilton', 'polecat', 'doggy-doo', 'civet']) >>>> nasty = set(['doggy-doo', 'polecat', 'limburger', 'Perl']) >>>> cheese & stinky # stinky cheese > set(['limburger', 'stilton']) >>>> cheese ^ stinky # either cheese or stinky but not both > set(['doggy-doo', 'civet', 'polecat', 'skunk', 'cheddar']) >>>> cheese ^ stinky ^ nasty # in an odd number of these sets (1 or 3) > set(['civet', 'cheddar', 'Perl', 'limburger', 'skunk']) > > Who hasn't needed that occasionally? This discussion is about adding a *logical* operator for use in boolean expressions. We obviously already have ^ for non-boolean use. Cheers, Chris -- http://blog.rebertia.com From pdlemper at earthlink.net Wed Jul 15 20:07:18 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Wed, 15 Jul 2009 19:07:18 -0500 Subject: Can module tell if running from interpreter vs Windows command line ? Message-ID: The WConio console module produces different colors, sometimes quite different, when run from Windows command line vs from Python interpreter >>> . A good foregnd/backgnd combination under one may be unreadable under the other : ( I'm using Python 3.0 with the corresponding WConio on XP. Is there any way for the module to detect under which it has been started ? Thanks From pavlovevidence at gmail.com Wed Jul 15 20:18:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 15 Jul 2009 17:18:52 -0700 (PDT) Subject: import module unbelieveable behaviour References: Message-ID: On Jul 15, 6:12?am, Peter Fodrek wrote: > Dear conference! > > I have test Why python based script for HeeksCNC post-processing does not > work... ?And I've got unbelievable behavior ?When importing module module > manually it works, but same opertaion from script does not > ?work as seen > > /opt/HeeksCAD8/HeeksCNC> python > Python 2.6 (r26:66714, Feb ?3 2009, 20:49:49) > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > Type "help", "copyright", "credits" or "license" for more information.>>> import nc.rez > > /opt/HeeksCAD8/HeeksCNC> python test.py > Traceback (most recent call last): > ? File "test.py", line 7, in > ? ? import nc.rez > ImportError: No module named rez > > /opt/HeeksCAD8/HeeksCNC> python ./test.py > Traceback (most recent call last): > ? File "./test.py", line 7, in > ? ? import nc.rez > ImportError: No module named rez > > Would anyone be helpful for me to get more information about this problem > because ?pydb does not show anything usable for me,please? That's a tricky one, indeed. Here's my guess: test.py is a symlink to a file in another directory. A major difference between interactive and script mode is the value of sys.path[0], which is the directory Python uses for local modules and packages. Python uses the current directory when running in interactive mode, but it uses the directory where the script is located when it is called on a script. Normally, when you run python on a script in the current directory, the behavior is the same as interactive mode, because the directory where the script is located is the current directory. But there's the gotcha: when you run python on symlinked file, Python sets sys.path[0] to the directory containing the actual file, not the directory containing the symlink. Thus even if the symlink is in the current directory, sys.path[0] is set to a different one. As a workaround, test.py should explicitly add '' to sys.path: sys.path.insert(0,'') There is another notable difference in behavior between the two modes. In interactive mode, sys.path[0] is ''. If you were to change the current directory from within Python, using os.chdir(), then future imports will occur relative to the new current directory. However, if you run Python from a script, sys.path[0] is an actual directory name, so that even if you chdir, any future imports will still occur relative to the original directory. Pretty confusing if you ask me. Unfortunately Python is very unPythonic when it comes to importing. :( Carl Banks From http Wed Jul 15 20:32:34 2009 From: http (Paul Rubin) Date: 15 Jul 2009 17:32:34 -0700 Subject: missing 'xor' Boolean operator References: Message-ID: <7x3a8xwjq5.fsf@ruckus.brouhaha.com> Anthony Tolle writes: > def xor(*operands): > if operands: > operands = list(operands) > a = bool(operands.pop(0)) > while operands: > b = bool(operands.pop(0)) > if a: > if b: > a = False > elif b: > a = True > return a > return False Among other things, that uses quadratic time! Why do you want to keep popping items from that list instead of iterating through it anyway? Anyway, I think you wrote something close to this: def xor(*operands): r = False for x in operands: r = (r != bool(x)) return r or in map-reduce style: from operator import ne def xor(*operands): return reduce(ne, map(bool, operands), False) From wuwei23 at gmail.com Wed Jul 15 20:41:41 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 17:41:41 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: Message-ID: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> On Jul 16, 10:07?am, pdlem... at earthlink.net wrote: > The WConio console module produces different colors, sometimes quite > different, when run from Windows command line vs from Python > interpreter >>> . ?A good foregnd/backgnd combination under one > may be unreadable under the other ?: ( > I'm using Python 3.0 with the corresponding WConio on XP. > Is there any way for the module to detect under which it has been > started ? ? ?Thanks It's recommended that you search through the list for similar questions before posting. This was covered just yesterday: http://groups.google.com/group/comp.lang.python/browse_frm/thread/fb2f075ccc796b63# From wuwei23 at gmail.com Wed Jul 15 20:47:39 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 17:47:39 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> Message-ID: <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> On Jul 16, 10:41?am, alex23 wrote: > It's recommended that you search through the list for similar > questions before posting. Of course, it's even MORE important that one actually ensures they're responding to the _same_ question before pointing at an answer... This older post should help: http://groups.google.com/group/comp.lang.python/browse_frm/thread/6c587ba377ae045a/0df10b077f5ee5d4 But the quick answer is to import sys into your program, and do a test on hasattr(sys, 'ps1'), which is only created when running the interactive prompt. From pdlemper at earthlink.net Wed Jul 15 21:36:57 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Wed, 15 Jul 2009 20:36:57 -0500 Subject: Can module tell if running from interpreter vs Windows command line ? References: Message-ID: On Wed, 15 Jul 2009 19:07:18 -0500, pdlemper at earthlink.net wrote: >The WConio console module produces different colors, sometimes quite >different, when run from Windows command line vs from Python >interpreter >>> . A good foregnd/backgnd combination under one >may be unreadable under the other : ( >I'm using Python 3.0 with the corresponding WConio on XP. >Is there any way for the module to detect under which it has been >started ? Thanks Thanks Alex It worked : import sys hasattr(sys, 'ps1') yields True when started from interpreter >>> and False run from Windows command line Dave WB3DWE From wuwei23 at gmail.com Wed Jul 15 22:30:11 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 19:30:11 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> Message-ID: <30a6179e-f08a-4809-8c3d-50e083114d50@y10g2000prg.googlegroups.com> On Jul 15, 5:51?pm, koranthala wrote: > ? ?I am not saying that what you said was wrong, only that I felt that > she got tense looking up regular expressions. So a python reply which > basically does the basic checking without going to re etc might be > more helpful for her to start her own coding. Using regexps wasn't the only advice given, and string manipulation is covered well in pretty much every tutorial I've taken a look at. The issue is that no evidence was shown that the OP was even trying to resolve this themself, and the new posting of the question every time an unsatisfactory answer was provided doesn't give me any faith they were endeavouring to do this at all. It's annoying seeing these "stone soup" attempts at coding come through here, whether its to answer someone's homework or to do someone's job for them. If the end result has value to the poster, I expect to see either effort on their behalf or at least a nominal attempt to reward others for their time. But if you want to do their work for them, I don't think anyone here would object, especially if you both took it to private correspondence. From afriere at yahoo.co.uk Wed Jul 15 22:56:38 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Wed, 15 Jul 2009 19:56:38 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> Message-ID: <0fc81395-7787-48a9-93de-cf7a274b691e@u38g2000pro.googlegroups.com> On Jul 16, 10:47?am, alex23 wrote: ... > This older post should help:http://groups.google.com/group/comp.lang.python/browse_frm/thread/6c5... > > But the quick answer is to import sys into your program, and do a test > on hasattr(sys, 'ps1'), which is only created when running the > interactive prompt. As you note there, this will work when running the vanilla shell (ie running it from the command line), but not (potentially) in other interactive environments (IronPython being the example you give). Another instance: there is not sys.ps1 when running a python shell under idle. Since this solution differs whether the interactive session is taking place from the cmd line, idle, IronPython etc. it seems to me not terribly robust. Depending on the use case, it is of course easy to tell whether the module was executed on the command line, or imported (from an interactive shell or another script) using the __name__ trick. (eg. is_imported = __name__ == '__main__') From koranthala at gmail.com Wed Jul 15 23:42:51 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 20:42:51 -0700 (PDT) Subject: mail References: Message-ID: <55d3cd26-44a2-4b87-8bb4-988e8c397833@v15g2000prn.googlegroups.com> On Jul 15, 11:46?pm, amr... at iisermohali.ac.in wrote: > Dear all, > > Sorry that I am disturbing you all again and again but this is the way I > am trying to solve my problem:--- > > >>> import re > >>> exp = re.compile("CA") > >>> infile = open("file1.txt") > >>> for line in infile: > > ... ? ? values = re.split("\s+", line) > ... ? ? if exp.search(line): > ... ? ? ? ?print ("%s %s CA = %s" %(values[2], values[3], values[6])) > ... > ?with this it is giving the output like:---- > > 8 ALA CA = 54.67 > 15 ALA CA = 52.18 > 21 ALA CA = 54.33 > 23 ALA CA = 55.84 > 33 ALA CA = 55.58 > 38 ALA CA = 54.33 > > which is all right but i want CB and C value also in each row and it > should take value from 5th column infront of them, file is something > lookin like:----- > > ?47 ? ? 8 ? ALA ? ? ? H ? ? H ? ? ?7.85 ? ? 0.02 ? ? 1 > ?48 ? ? 8 ? ALA ? ? ? HA ? ?H ? ? ?2.98 ? ? 0.02 ? ? 1 > ?49 ? ? 8 ? ALA ? ? ? HB ? ?H ? ? ?1.05 ? ? 0.02 ? ? 1 > ?50 ? ? 8 ? ALA ? ? ? C ? ? C ? ?179.39 ? ? ?0.3 ? ? 1 > ?51 ? ? 8 ? ALA ? ? ? CA ? ?C ? ? 54.67 ? ? ?0.3 ? ? 1 > ?52 ? ? 8 ? ALA ? ? ? CB ? ?C ? ? 18.85 ? ? ?0.3 ? ? 1 > ?53 ? ? 8 ? ALA ? ? ? N ? ? N ? ?123.95 ? ? ?0.3 ? ? 1 > 107 ? ?15 ? ALA ? ? ? H ? ? H ? ? ?8.05 ? ? 0.02 ? ? 1 > 108 ? ?15 ? ALA ? ? ? HA ? ?H ? ? ?4.52 ? ? 0.02 ? ? 1 > 109 ? ?15 ? ALA ? ? ? HB ? ?H ? ? ?1.29 ? ? 0.02 ? ? 1 > 110 ? ?15 ? ALA ? ? ? C ? ? C ? ?177.18 ? ? ?0.3 ? ? 1 > 111 ? ?15 ? ALA ? ? ? CA ? ?C ? ? 52.18 ? ? ?0.3 ? ? 1 > 112 ? ?15 ? ALA ? ? ? CB ? ?C ? ? 20.64 ? ? ?0.3 ? ? 1 > 113 ? ?15 ? ALA ? ? ? N ? ? N ? ?119.31 ? ? ?0.3 ? ? 1 > 154 ? ?21 ? ALA ? ? ? H ? ? H ? ? ?7.66 ? ? 0.02 ? ? 1 > 155 ? ?21 ? ALA ? ? ? HA ? ?H ? ? ?4.05 ? ? 0.02 ? ? 1 > 156 ? ?21 ? ALA ? ? ? HB ? ?H ? ? ?1.39 ? ? 0.02 ? ? 1 > 157 ? ?21 ? ALA ? ? ? C ? ? C ? ?179.35 ? ? ?0.3 ? ? 1 > 158 ? ?21 ? ALA ? ? ? CA ? ?C ? ? 54.33 ? ? ?0.3 ? ? 1 > 159 ? ?21 ? ALA ? ? ? CB ? ?C ? ? 17.87 ? ? ?0.3 ? ? 1 > 160 ? ?21 ? ALA ? ? ? N ? ? N ? ?123.58 ? ? ?0.3 ? ? 1 > 169 ? ?23 ? ALA ? ? ? H ? ? H ? ? ?8.78 ? ? 0.02 ? ? 1 > 170 ? ?23 ? ALA ? ? ? HA ? ?H ? ? ?4.14 ? ? 0.02 ? ? 1 > 171 ? ?23 ? ALA ? ? ? HB ? ?H ? ? ?1.62 ? ? 0.02 ? ? 1 > 172 ? ?23 ? ALA ? ? ? C ? ? C ? ?179.93 ? ? ?0.3 ? ? 1 > 173 ? ?23 ? ALA ? ? ? CA ? ?C ? ? 55.84 ? ? ?0.3 ? ? 1 > 174 ? ?23 ? ALA ? ? ? CB ? ?C ? ? 17.55 ? ? ?0.3 ? ? 1 > 175 ? ?23 ? ALA ? ? ? N ? ? N ? ?120.16 ? ? ?0.3 ? ? 1 > 232 ? ?33 ? ALA ? ? ? H ? ? H ? ? ?7.57 ? ? 0.02 ? ? 1 > 233 ? ?33 ? ALA ? ? ? HA ? ?H ? ? ?3.89 ? ? 0.02 ? ? 1 > 234 ? ?33 ? ALA ? ? ? HB ? ?H ? ? ?1.78 ? ? 0.02 ? ? 1 > 235 ? ?33 ? ALA ? ? ? C ? ? C ? ?179.24 ? ? ?0.3 ? ? 1 > 236 ? ?33 ? ALA ? ? ? CA ? ?C ? ? 55.58 ? ? ?0.3 ? ? 1 > 237 ? ?33 ? ALA ? ? ? CB ? ?C ? ? 19.75 ? ? ?0.3 ? ? 1 > 238 ? ?33 ? ALA ? ? ? N ? ? N ? ?121.52 ? ? ?0.3 ? ? 1 > 269 ? ?38 ? ALA ? ? ? H ? ? H ? ? ?8.29 ? ? 0.02 ? ? 1 > 270 ? ?38 ? ALA ? ? ? HA ? ?H ? ? ?4.04 ? ? 0.02 ? ? 1 > 271 ? ?38 ? ALA ? ? ? HB ? ?H ? ? ?1.35 ? ? 0.02 ? ? 1 > 272 ? ?38 ? ALA ? ? ? C ? ? C ? ?178.95 ? ? ?0.3 ? ? 1 > 273 ? ?38 ? ALA ? ? ? CA ? ?C ? ? 54.33 ? ? ?0.3 ? ? 1 > 274 ? ?38 ? ALA ? ? ? CB ? ?C ? ? 18.30 ? ? ?0.3 ? ? 1 > 275 ? ?38 ? ALA ? ? ? N ? ? N ? ?120.62 ? ? ?0.3 ? ? 1 > > I just want that it will give output something like:----- > > 8 ?ALA ?C = 179.39 ?CA = 54.67 ?CB = 18.85 > 15 ALA ?C = 177.18 ?CA = 52.18 ?CB = 20.64 > 21 ALA ?C = 179.35 ?CA = 54.33 ?CB = 17.87..... > > so first it will write the position of the amino acid(given by second > column)then amino acid here it is ALA and then the corresponding value of > C, CA and CB from 5th colum for each position of ALA. > > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA Hi, Can you try using the code which I suggested in the earlier thread? If you cannot use it to get the output, just point it out here, and I will try to solve the issue. From nagle at animats.com Thu Jul 16 00:03:58 2009 From: nagle at animats.com (John Nagle) Date: Wed, 15 Jul 2009 21:03:58 -0700 Subject: does python have a generic object pool like commons-pool in Java In-Reply-To: <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> Message-ID: <4a5ea61b$0$1682$742ec2ed@news.sonic.net> Rick Lawson wrote: > On Jul 15, 4:53 am, Jonathan Gardner > wrote: >> On Jul 14, 6:34 pm, Rick Lawson wrote: >> >>> Appreciate any help on this. I am porting an app from Java to python >>> and need generic object pooling with hooks for object initialization / >>> cleanup and be able to specify an object timeout. >> Are you looking for something like a thread pool or a connection pool? >> Such a thing is easy to code in Python. Might as well write one from >> scratch for your particular need. >> >> By the way, a tip for writing Python: Forget Java. If you're porting >> an app, consider rewriting it from the architecture on up. You'll save >> yourself time and get a better result. > > Jonathan, > > Thanks for the advice but would like to pick your brain a little more. > The pool I need is not a thread pool or db pool. I need to pool > objects which are a proxy for an external C process that communicates > via sockets. "fcgi" is an option for this sort of thing. With "mod_fcgi" installed in Apache, and "fcgi.py" used to manage the Python side of the problem, you can have semi-persistent programs started up and shut down for you on the server side. I use this for "sitetruth.com". It's useful when CGI is too slow because the cost of launching Python and loading all the modules is far greater than the cost of processing the request. Once you find and install a working Apache "mod_fcgi" module, and a working "fcgi.py" module (I suggest "http://svn.saddi.com/py-lib/trunk/fcgi.py") writing the server side app is no harder than with CGI. But the performance is far better. John Nagle From wuwei23 at gmail.com Thu Jul 16 00:14:37 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 21:14:37 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> <0fc81395-7787-48a9-93de-cf7a274b691e@u38g2000pro.googlegroups.com> Message-ID: <35b0b936-6bc1-4acf-aa27-6cc0db84292b@y10g2000prf.googlegroups.com> On Jul 16, 12:56?pm, Asun Friere wrote: > As you note there, this will work when running the vanilla shell (ie > running ?it from the command line), but not (potentially) in other > interactive environments (IronPython being the example you give). Actually, that was IPython, which is an enhanced interactive prompt, and a totally different beastie altogether :) > Another instance: ?there is not sys.ps1 when running a python shell > under idle. ?Since this solution differs whether the interactive > session is taking place from the cmd line, idle, IronPython etc. it > seems to me not terribly robust. Well, none of {idle, IronPython, IPython} were specified by the OP AND you're citing back what I myself wrote in the link to which I also referred the OP - whom has subsequently posted his success with this technique - so I'm not really sure what the point is here other than "if you have a different use case, you'll need a different solution"... > Depending on the use case, it is of course easy to tell whether the > module was executed on the command line, or imported (from an > interactive shell or another script) using the __name__ trick. (eg. > is_imported = __name__ == '__main__') That should be: is_imported = __name__ != '__main__' And such a test is all well and good if the main code body is the one that needs to know about the execution mode, but if you need to know under which conditions the program is being run within a module imported by the main body, well, that check is _always_ going to be true... From phostu at gmail.com Thu Jul 16 00:18:39 2009 From: phostu at gmail.com (Vincent) Date: Wed, 15 Jul 2009 21:18:39 -0700 (PDT) Subject: problem about cx_Oracle Message-ID: <957ab843-92ac-4072-ace4-441a2da0d130@m3g2000pri.googlegroups.com> hi, all: i am using cx_oracle now. i write code as below: def __getfields_by_tbname(self,tbname): cursor = self.ora_db.cursor() print tbname sql = 'select * from %s where rownum <=2' % tbname print sql cursor = cursor.execute(sql) return self.getfields(cursor) and i got a error, it's message is : JRYZCFZB_X_ZQY select * from JRYZCFZB_X_ZQY where rownum <=2 Traceback (most recent call last): File "", line 1, in File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools \data_convert.py", line 107, in convert self.convert_table(tbname) File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools \data_convert.py", line 94, in convert_table field_list = self.__getfields_by_tbname(tbname) File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools \data_convert.py", line 38, in __getfields_by_tbname cursor = cursor.execute(sql) TypeError: expecting None or a string i'm sure the cursor instance is not None. could anybody give me sussgestion? i will apreciate it. vincent From koranthala at gmail.com Thu Jul 16 00:29:04 2009 From: koranthala at gmail.com (koranthala) Date: Wed, 15 Jul 2009 21:29:04 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> <30a6179e-f08a-4809-8c3d-50e083114d50@y10g2000prg.googlegroups.com> Message-ID: On Jul 16, 7:30?am, alex23 wrote: > On Jul 15, 5:51?pm, koranthala wrote: > > > ? ?I am not saying that what you said was wrong, only that I felt that > > she got tense looking up regular expressions. So a python reply which > > basically does the basic checking without going to re etc might be > > more helpful for her to start her own coding. > > Using regexps wasn't the only advice given, and string manipulation is > covered well in pretty much every tutorial I've taken a look at. The > issue is that no evidence was shown that the OP was even trying to > resolve this themself, and the new posting of the question every time > an unsatisfactory answer was provided doesn't give me any faith they > were endeavouring to do this at all. > > It's annoying seeing these "stone soup" attempts at coding come > through here, whether its to answer someone's homework or to do > someone's job for them. If the end result has value to the poster, I > expect to see either effort on their behalf or at least a nominal > attempt to reward others for their time. > > But if you want to do their work for them, I don't think anyone here > would object, especially if you both took it to private correspondence. It is not that I do want to do the work for them. It is just that I was in the same position just 6 months back. My first pieces of code were very poor - full of errors and quite horrible indeed. I was even afraid to post it anywhere. I came here, and I found so much helpful people who helped me out so much, that I think my coding has improved a little bit. So, I just thought that we should always give people the benefit of doubt. From wuwei23 at gmail.com Thu Jul 16 00:30:40 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 21:30:40 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <4a5ea61b$0$1682$742ec2ed@news.sonic.net> Message-ID: <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> On Jul 16, 2:03?pm, John Nagle wrote: > ? ? ?"fcgi" is an option for this sort of thing. ?With "mod_fcgi" installed > in Apache, and "fcgi.py" used to manage the Python side of the problem, you > can have semi-persistent programs started up and shut down for you on the > server side. Hey John, The environments in which I've been asked to develop webs apps using Python have all utilised mod_wsgi. Do you have any experience with mod_wsgi vs mod_fcgi, and if so, can you comment on any relevant performance / capability / ease-of-use differences? Cheers, alex23 From akhilanger at gmail.com Thu Jul 16 00:34:06 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 21:34:06 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) Message-ID: <24509879.post@talk.nabble.com> Hi! Can anyone please help me getting rid of this error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) I am not a python programmer (though intend to start learning this wonderful language), I am just using a python script. After doing some search, I found that 0xb7 is a 'middle dot character' that is not interpreted by the python. Even after inserting text = text.replace('\u00b7', '') in the script, the problem still persists. Can anyone please tell me the easiest way to get rid of this? --Thanks, Akhil -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24509879.html Sent from the Python - python-list mailing list archive at Nabble.com. From clp2 at rebertia.com Thu Jul 16 00:39:19 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 21:39:19 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24509879.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> Message-ID: <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: > > Hi! > > Can anyone please help me getting rid of this error: > UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position > 13: ordinal not in range(128) > > I am not a python programmer (though intend to start learning this wonderful > language), I am just using a python script. > > After doing some search, I found that 0xb7 is a 'middle dot character' that > is not interpreted by the python. > Even after inserting text = text.replace('\u00b7', '') in the script, the > problem still persists. > > Can anyone please tell me the easiest way to get rid of this? We'll need the full error traceback. The error message at the end is just not enough information. As to fixing it, google for "UnicodeEncodeError". You should find about a million mailinglist threads on it. Cheers, Chris -- http://blog.rebertia.com From wuwei23 at gmail.com Thu Jul 16 00:41:31 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 15 Jul 2009 21:41:31 -0700 (PDT) Subject: one more question References: <045a6d36-8b65-44bf-909a-8831e6bf1bbf@t13g2000yqt.googlegroups.com> <56406040-baec-4dae-9b04-5f1472028bdc@k1g2000yqf.googlegroups.com> <30a6179e-f08a-4809-8c3d-50e083114d50@y10g2000prg.googlegroups.com> Message-ID: <0f28ec7c-f0cc-4cfb-a518-e65dba6e094c@12g2000pri.googlegroups.com> On Jul 16, 2:29?pm, koranthala wrote: > It is not that I do want to do the work for them. It is just that I > was in the same position just 6 months back. My first pieces of code > were very poor - full of errors and quite horrible indeed. I was even > afraid to post it anywhere. I came here, and I found so much helpful > people who helped me out so much, that I think my coding has improved > a little bit. So, I just thought that we should always give people the > benefit of doubt. Which is why I waited until the 5th repeated post on the topic before commenting. From nagle at animats.com Thu Jul 16 01:05:01 2009 From: nagle at animats.com (John Nagle) Date: Wed, 15 Jul 2009 22:05:01 -0700 Subject: does python have a generic object pool like commons-pool in Java In-Reply-To: <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <4a5ea61b$0$1682$742ec2ed@news.sonic.net> <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> Message-ID: <4a5eb46a$0$1673$742ec2ed@news.sonic.net> alex23 wrote: > On Jul 16, 2:03 pm, John Nagle wrote: >> "fcgi" is an option for this sort of thing. With "mod_fcgi" installed >> in Apache, and "fcgi.py" used to manage the Python side of the problem, you >> can have semi-persistent programs started up and shut down for you on the >> server side. > > Hey John, > > The environments in which I've been asked to develop webs apps using > Python have all utilised mod_wsgi. Do you have any experience with > mod_wsgi vs mod_fcgi, and if so, can you comment on any relevant > performance / capability / ease-of-use differences? > > Cheers, > alex23 FCGI seems to be somewhat out of favor, perhaps because it isn't complicated enough. It's a mature technology and works reasonably well. It's been a puzzle to me that FCGI was taken out of the main Apache code base, because it gives production-level performance with CGI-type simplicity. WSGI has a mode for running Python inside the Apache process, which is less secure and doesn't allow multiple Python processes. That complicates mod_wsgi considerably, and ties it very closely to specific versions of Python and Python modules. As a result, the WSGI developers are patching at a great rate. I think the thing has too many "l33t features". I'd avoid "embedded mode". "Daemon mode" is the way to go if you use WSGI. I haven't tried WSGI; I don't need the grief of a package under heavy development. John Nagle From akhilanger at gmail.com Thu Jul 16 01:05:20 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 22:05:20 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> Message-ID: <24510222.post@talk.nabble.com> Well, All I get is this traceback: File "./customWikiExtractor.py", line 492, in ? main() File "./customWikiExtractor.py", line 480, in main print >> sys.stdout, 'line: %s' % line UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) I am giving a string to the python code as input, and python processes it like this: line = line.decode('utf-8').strip() After this when I do, print >> sys.stdout, 'line: %s' % line I get this Unicode error. I tried a few repairs, but they did not work like changing: in sgmmlib.py (/usr/lib64/python2.4/sgmmlib.py) if not 0 < n <= 255 to if not 0 < n <= 127 But since this did not work, I have changed it back to it's original form. --Thanks, Akhil Chris Rebert-6 wrote: > > On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >> >> Hi! >> >> Can anyone please help me getting rid of this error: >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >> position >> 13: ordinal not in range(128) >> >> I am not a python programmer (though intend to start learning this >> wonderful >> language), I am just using a python script. >> >> After doing some search, I found that 0xb7 is a 'middle dot character' >> that >> is not interpreted by the python. >> Even after inserting text = text.replace('\u00b7', '') in the script, the >> problem still persists. >> >> Can anyone please tell me the easiest way to get rid of this? > > We'll need the full error traceback. The error message at the end is > just not enough information. > As to fixing it, google for "UnicodeEncodeError". You should find > about a million mailinglist threads on it. > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24510222.html Sent from the Python - python-list mailing list archive at Nabble.com. From akhilanger at gmail.com Thu Jul 16 01:09:38 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 22:09:38 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24510222.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> Message-ID: <24510252.post@talk.nabble.com> Sorry, it is sgmllib.py and not sgmmlib.py -- Akhil akhil1988 wrote: > > Well, > All I get is this traceback: > > File "./customWikiExtractor.py", line 492, in ? > main() > File "./customWikiExtractor.py", line 480, in main > print >> sys.stdout, 'line: %s' % line > UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in > position 13: ordinal not in range(128) > > I am giving a string to the python code as input, and python processes it > like this: > > line = line.decode('utf-8').strip() > > After this when I do, > print >> sys.stdout, 'line: %s' % line > I get this Unicode error. > > I tried a few repairs, but they did not work like > changing: in sgmmlib.py (/usr/lib64/python2.4/sgmmlib.py) > if not 0 < n <= 255 > to > if not 0 < n <= 127 > > But since this did not work, I have changed it back to it's original form. > > --Thanks, > Akhil > > > Chris Rebert-6 wrote: >> >> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >>> >>> Hi! >>> >>> Can anyone please help me getting rid of this error: >>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >>> position >>> 13: ordinal not in range(128) >>> >>> I am not a python programmer (though intend to start learning this >>> wonderful >>> language), I am just using a python script. >>> >>> After doing some search, I found that 0xb7 is a 'middle dot character' >>> that >>> is not interpreted by the python. >>> Even after inserting text = text.replace('\u00b7', '') in the script, >>> the >>> problem still persists. >>> >>> Can anyone please tell me the easiest way to get rid of this? >> >> We'll need the full error traceback. The error message at the end is >> just not enough information. >> As to fixing it, google for "UnicodeEncodeError". You should find >> about a million mailinglist threads on it. >> >> Cheers, >> Chris >> -- >> http://blog.rebertia.com >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24510252.html Sent from the Python - python-list mailing list archive at Nabble.com. From clp2 at rebertia.com Thu Jul 16 01:12:08 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 15 Jul 2009 22:12:08 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24510222.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> Message-ID: <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> > Chris Rebert-6 wrote: >> >> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >>> >>> Hi! >>> >>> Can anyone please help me getting rid of this error: >>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >>> position >>> 13: ordinal not in range(128) >>> >>> I am not a python programmer (though intend to start learning this >>> wonderful >>> language), I am just using a python script. >>> >>> After doing some search, I found that 0xb7 is a 'middle dot character' >>> that >>> is not interpreted by the python. >>> Even after inserting text = text.replace('\u00b7', '') in the script, the >>> problem still persists. >>> >>> Can anyone please tell me the easiest way to get rid of this? >> >> We'll need the full error traceback. The error message at the end is >> just not enough information. >> As to fixing it, google for "UnicodeEncodeError". You should find >> about a million mailinglist threads on it. On Wed, Jul 15, 2009 at 10:05 PM, akhil1988 wrote: > > Well, > All I get is this traceback: > > File "./customWikiExtractor.py", line 492, in ? > main() > File "./customWikiExtractor.py", line 480, in main > print >> sys.stdout, 'line: %s' % line > UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position > 13: ordinal not in range(128) > > I am giving a string to the python code as input, and python processes it > like this: > > line = line.decode('utf-8').strip() > > After this when I do, > print >> sys.stdout, 'line: %s' % line > I get this Unicode error. Try this instead (the ">> sys.stdout" part is redundant): print (u'line: %s' % line).encode('utf8') #if your system doesn't use UTF-8, change as necessary Cheers, Chris -- http://blog.rebertia.com From peter.fodrek at stuba.sk Thu Jul 16 01:16:57 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 07:16:57 +0200 Subject: import module unbelieveable behaviour In-Reply-To: <7c6bdkF26j00sU1@mid.uni-berlin.de> References: <7c6bdkF26j00sU1@mid.uni-berlin.de> Message-ID: <200907160716.57648.peter.fodrek@stuba.sk> On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > Peter Fodrek wrote: ...... > What does > > import nc > print nc.__file__ python Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import nc >>> print nc.__file__ nc/__init__.pyc and as well ls nc attach.py hpgl2dv_read.py iso_codes.pyc nc_read.py emc2.py hpgl3d.py iso.py nc_read.pyc rez.pyc emc2_read.py hpgl3d_read.py iso_read.py num_reader.py rez_read.py hpgl2d.py __init__.py machines.txt rez_codes.py siegkx1.py hpgl2d_read.py __init__.pyc nc.py siegkx1_read.py hpgl2dv.py iso_codes.py nc.pyc rez.py and module is compiled well ls -la nc/rez* -rwxr-xr-x 1 peto users 1391 2009-07-15 13:18 nc/rez_codes.py -rwxrwxrwx 1 peto users 255 2009-07-15 14:28 nc/rez.py -rwxr-xr-x 1 peto users 222 2009-07-15 14:28 nc/rez.pyc -rwxr-xr-x 1 peto users 5920 2009-07-15 13:17 nc/rez_read.py Thank you Peter From peter.fodrek at stuba.sk Thu Jul 16 01:20:53 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 07:20:53 +0200 Subject: import module unbelieveable behaviour In-Reply-To: References: Message-ID: <200907160720.53656.peter.fodrek@stuba.sk> On Thursday 16 July 2009 02:18:52 Carl Banks wrote: > On Jul 15, 6:12 am, Peter Fodrek wrote: > That's a tricky one, indeed. > > Here's my guess: test.py is a symlink to a file in another directory. It is not true guess ls -la test.py -rw-r--r-- 1 peto users 1990 2009-07-15 14:19 test.py But maybe access rights needed to be corrected... Thank you Peter From mensanator at aol.com Thu Jul 16 01:28:31 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 15 Jul 2009 22:28:31 -0700 (PDT) Subject: Why does Python not stop when it's supposed to? Message-ID: <539781e9-1096-499e-851a-8d3e8247e52d@f10g2000vbf.googlegroups.com> So, I'm playing around with Python (running 2.6 and 3.1 versions of IDLE). After I shut everything down and close the windows, I notice the Task Manager's activity light is still glowing. In looking at the Process window I see this: Image... User Name CPU Memory(... Description pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 216 K pythonw.exe pythonw.exe mensanator 00 220 K pythonw.exe pythonw.exe mensanator 00 220 K pythonw.exe pythonw.exe mensanator 00 408 K pythonw.exe pythonw.exe mensanator 00 180 K pythonw.exe pythonw.exe mensanator 50 66,156 K pythonw.exe pythonw.exe mensanator 00 600 K pythonw.exe pythonw.exe mensanator 00 228 K pythonw.exe pythonw.exe mensanator 00 208 K pythonw.exe pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 192 K pythonw.exe pythonw.exe mensanator 00 224 K pythonw.exe pythonw.exe mensanator 00 180 K pythonw.exe pythonw.exe mensanator 00 8,672 K pythonw.exe I can't seem to reproduce this. Opening IDLE causes two copies to appear. When RUN, a third appears for a few moments and drops back to two. Doing a RESTART SHELL also launches another copy which also goes away momentarily. So, with a 2.6 and a 3.1 IDLE running simultaneously, there could be 6 copies at once. And they eventually all go away when the windows are closed. How is it I end up with 16 copies, one of them still running after all the windows are closed? I had to manually do an [end process] on all of them. If I was doing something wrong, i sure would like to know what it is. From steven at REMOVE.THIS.cybersource.com.au Thu Jul 16 01:37:02 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 16 Jul 2009 05:37:02 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > Didn't know that. > So if I resume: > - not 'foo' => False > - 'foo' or 'foo' => 'foo' > > I may be missing something, but honestly, Guido must have smoked some > heavy stuff to write such logic, has he ? No, it's perfectly reasonable, and not at all the product of mind- altering drugs. Other languages do the same thing. for x in alist or blist or clist: print x will select the first non-empty list and iterate over that. Every object in Python has a truth value. By convention, we say that objects are Something or Nothing. 0 None [] '' are examples of Nothing, or false values. 1 2.5 [x, y, z] 'foo' are examples of Something, or true values. Note the distinction between lower-case "false" and "true" (adjectives), and title-case False and True (nouns). `if x: ... else:` branches according to whether x is Something or Nothing, not whether it is True or False. The operators `and` and `or` also return Something or Nothing, short- circuiting as appropriate. -- Steven From phostu at gmail.com Thu Jul 16 01:38:10 2009 From: phostu at gmail.com (Vincent) Date: Wed, 15 Jul 2009 22:38:10 -0700 (PDT) Subject: problem about cx_Oracle References: <957ab843-92ac-4072-ace4-441a2da0d130@m3g2000pri.googlegroups.com> Message-ID: <717d5451-ef6d-45df-9e77-2135c75f7a12@u38g2000pro.googlegroups.com> On Jul 16, 12:18?pm, Vincent wrote: > hi, all: > > i am using cx_oracle now. > > i write code as below: > > def __getfields_by_tbname(self,tbname): > ? ? ? ? cursor = self.ora_db.cursor() > ? ? ? ? print tbname > ? ? ? ? sql = 'select * from %s where rownum <=2' % tbname > ? ? ? ? print sql > ? ? ? ? cursor = cursor.execute(sql) > ? ? ? ? return self.getfields(cursor) > > and i got a error, it's message is : > JRYZCFZB_X_ZQY > select * from JRYZCFZB_X_ZQY where rownum <=2 > Traceback (most recent call last): > ? File "", line 1, in > ? File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools > \data_convert.py", line 107, in convert > ? ? self.convert_table(tbname) > ? File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools > \data_convert.py", line 94, in convert_table > ? ? field_list = self.__getfields_by_tbname(tbname) > ? File "c:/vincent/wd/django/mysite/mysite\..\mysite\tools > \data_convert.py", line 38, in __getfields_by_tbname > ? ? cursor = cursor.execute(sql) > TypeError: expecting None or a string > > i'm sure the cursor instance is not None. > could anybody give me sussgestion? i will apreciate it. > > vincent i have the answer now. the variant sql is unicode. i neet to convert it to string. From pavlovevidence at gmail.com Thu Jul 16 01:38:23 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 15 Jul 2009 22:38:23 -0700 (PDT) Subject: import module unbelieveable behaviour References: <7c6bdkF26j00sU1@mid.uni-berlin.de> Message-ID: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> On Jul 15, 10:16?pm, Peter Fodrek wrote: > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > > > Peter Fodrek wrote: > ...... > > What does > > > import nc > > print nc.__file__ > > python > Python 2.6 (r26:66714, Feb ?3 2009, 20:49:49) > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > Type "help", "copyright", "credits" or "license" for more information.>>> import nc > >>> print nc.__file__ > > nc/__init__.pyc What does it do if you run this from a script? Also, what is sys.path[0] in both interactive and script? Inside test.py, right before the line that imports nc.rez, add the code "import sys; print sys.modules.get('nc')"; what does that output? Is there an old nc.py or nc.pyc file anywhere? Carl Banks Carl Banks From afriere at yahoo.co.uk Thu Jul 16 01:44:40 2009 From: afriere at yahoo.co.uk (Asun Friere) Date: Wed, 15 Jul 2009 22:44:40 -0700 (PDT) Subject: Can module tell if running from interpreter vs Windows command line ? References: <02dbb097-c0b2-46b9-9ea7-f1212518b283@12g2000pri.googlegroups.com> <52883754-fd57-450e-a6f0-00b55e510b08@y10g2000prg.googlegroups.com> <0fc81395-7787-48a9-93de-cf7a274b691e@u38g2000pro.googlegroups.com> <35b0b936-6bc1-4acf-aa27-6cc0db84292b@y10g2000prf.googlegroups.com> Message-ID: <6c572443-cf8e-403c-aa66-086007dca540@d15g2000prc.googlegroups.com> On Jul 16, 2:14?pm, alex23 wrote: ... > AND > you're citing back what I myself wrote in the link to which I also > referred the OP - whom (sic) has subsequently posted his success with this > technique - so I'm not really sure what the point is here other than > "if you have a different use case, you'll need a different > solution"... > Please don't take offence where none was intended. I know that I was citing you and that I was highlighting the caveat you raised, explicitly so. No criticism of your post was intended nor implied. Moreover the fact that it worked for OP on a single occasion does not speak for its robustness. > > Depending on the use case, it is of course easy to tell whether the > > module was executed on the command line, or imported (from an > > interactive shell or another script) using the __name__ trick. ?(eg. > > is_imported = __name__ == '__main__') > > That should be: > > is_imported = __name__ != '__main__' > Doh! ... Yup I actually used that when I tried it out, my bad. > And such a test is all well and good if the main code body is the one > that needs to know about the execution mode, but if you need to know > under which conditions the program is being run within a module > imported by the main body, well, that check is _always_ going to be > true... Which is what the paragraph you just quoted says. Hence the attribute is called 'is_imported' rather that 'running_non_interactively'. From akhilanger at gmail.com Thu Jul 16 01:52:24 2009 From: akhilanger at gmail.com (akhil1988) Date: Wed, 15 Jul 2009 22:52:24 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> Message-ID: <24510519.post@talk.nabble.com> Chris, Using print (u'line: %s' % line).encode('utf-8') the 'line' gets printed, but actually this print statement I was using just for testing, actually my code operates on 'line', on which I use line = line.decode('utf-8') as 'line' is read as bytes from a stream. And if I use line = line.encode('utf-8'), I start getting other error like UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4561: ordinal not in range(128) at line = line.replace('<<', u'?').replace('>>', u'?') --Akhil Chris Rebert-6 wrote: > >> Chris Rebert-6 wrote: >>> >>> On Wed, Jul 15, 2009 at 9:34 PM, akhil1988 wrote: >>>> >>>> Hi! >>>> >>>> Can anyone please help me getting rid of this error: >>>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >>>> position >>>> 13: ordinal not in range(128) >>>> >>>> I am not a python programmer (though intend to start learning this >>>> wonderful >>>> language), I am just using a python script. >>>> >>>> After doing some search, I found that 0xb7 is a 'middle dot character' >>>> that >>>> is not interpreted by the python. >>>> Even after inserting text = text.replace('\u00b7', '') in the script, >>>> the >>>> problem still persists. >>>> >>>> Can anyone please tell me the easiest way to get rid of this? >>> >>> We'll need the full error traceback. The error message at the end is >>> just not enough information. >>> As to fixing it, google for "UnicodeEncodeError". You should find >>> about a million mailinglist threads on it. > On Wed, Jul 15, 2009 at 10:05 PM, akhil1988 wrote: >> >> Well, >> All I get is this traceback: >> >> File "./customWikiExtractor.py", line 492, in ? >> main() >> File "./customWikiExtractor.py", line 480, in main >> print >> sys.stdout, 'line: %s' % line >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in >> position >> 13: ordinal not in range(128) >> >> I am giving a string to the python code as input, and python processes it >> like this: >> >> line = line.decode('utf-8').strip() >> >> After this when I do, >> print >> sys.stdout, 'line: %s' % line >> I get this Unicode error. > > Try this instead (the ">> sys.stdout" part is redundant): > print (u'line: %s' % line).encode('utf8') > #if your system doesn't use UTF-8, change as necessary > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24510519.html Sent from the Python - python-list mailing list archive at Nabble.com. From milesck at umich.edu Thu Jul 16 01:59:54 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Thu, 16 Jul 2009 01:59:54 -0400 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <4a5cc8c0$0$1655$742ec2ed@news.sonic.net> Message-ID: <1EAE7F84-0FA1-4688-8F2B-B5E6869E69B8@umich.edu> On Jul 15, 2009, at 4:26 PM, David Bolen wrote: > Miles Kaufmann writes: > >> On Jul 14, 2009, at 5:06 PM, David Bolen wrote: >>> Are you sure? It seems to restrict them in the same block, but >>> not in >>> the entire file. At least I was able to use both space and tab >>> indented blocks in the same file with Python 3.0 and 3.1. >> >> It seems to me that, within an indented block, Python 3.1 requires >> that you are consistent in your use of indentation characters *for >> that indentation level*. For example, the following code seems to be >> allowed: > > Um, right - in other words, what I said :-) I wasn't trying to correct you, just being more explicit. :) After reading your post, I still wasn't sure if the restriction on mixing spaces and tabs applied to nested blocks--I was surprised that the code sample I included was allowed. -Miles From list at qtrac.plus.com Thu Jul 16 02:21:48 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Wed, 15 Jul 2009 23:21:48 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? Message-ID: Hi, I'm just wondering why <, <=, >=, and > are not supported by collections.OrderedDict: >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) >>> d2 = d1.copy() >>> d2["z"] = 4 >>> d1 == d2 False >>> d1 < d2 Traceback (most recent call last): File "", line 1, in d1 < d2 TypeError: unorderable types: OrderedDict() < OrderedDict() It just seems to me that since the items in ordered dictionaries are ordered, it would make sense to do an item by item comparison from first to last item in exactly the same way that Python compares lists or tuples? From peter.fodrek at stuba.sk Thu Jul 16 02:38:57 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 08:38:57 +0200 Subject: import module unbelieveable behaviour In-Reply-To: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> References: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> Message-ID: <200907160838.57658.peter.fodrek@stuba.sk> On Thursday 16 July 2009 07:38:23 Carl Banks wrote: > On Jul 15, 10:16 pm, Peter Fodrek wrote: > > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > > > Peter Fodrek wrote: > > > > ...... > > > > > What does > > > > > > import nc > > > print nc.__file__ > > > > python > > Python 2.6 (r26:66714, Feb 3 2009, 20:49:49) > > [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2 > > Type "help", "copyright", "credits" or "license" for more information.>>> > > import nc > > > > >>> print nc.__file__ > > > > nc/__init__.pyc > > What does it do if you run this from a script? Ouch, I am and python rookie, that was needed to try to edit undocumented script from German author this answers everything /home/opt/HeeksCAD8/HeeksCNC> python ./test.py /usr/local/lib/heekscnc/nc/__init__.py and it is because begining of the script # -*- coding: utf-8 -*- import sys sys.path.insert(0,'/usr/local/lib/heekscnc/') import kurve import kurve_funcs from nc.nc import * import nc print nc.__file__ import nc.rez and ls -a /usr/local/lib/heekscnc/nc . emc2_read.py hpgl2dv_read.py iso_codes.py nc_read.py .. hpgl2d.py hpgl3d.py iso.py num_reader.py attach.py hpgl2d_read.py hpgl3d_read.py iso_read.py siegkx1.py emc2.py hpgl2dv.py __init__.py nc.py siegkx1_read.py Thank you for your help, once more. Peter From peter.fodrek at stuba.sk Thu Jul 16 02:48:55 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Thu, 16 Jul 2009 08:48:55 +0200 Subject: import module unbelieveable behaviour In-Reply-To: <200907160838.57658.peter.fodrek@stuba.sk> References: <2c30659f-0e89-4d1e-bfa0-e96032a813d0@n11g2000yqb.googlegroups.com> <200907160838.57658.peter.fodrek@stuba.sk> Message-ID: <200907160848.55069.peter.fodrek@stuba.sk> On Thursday 16 July 2009 08:38:57 Peter Fodrek wrote: > On Thursday 16 July 2009 07:38:23 Carl Banks wrote: > > On Jul 15, 10:16 pm, Peter Fodrek wrote: > > > On Wednesday 15 July 2009 17:41:54 Diez B. Roggisch wrote: > sys.path.insert(0,'/usr/local/lib/heekscnc/') I was mentioned that this adds system path to multipath list(As $PATH) and it replaces path instead. That was problem.. Peter From jackdied at gmail.com Thu Jul 16 03:12:20 2009 From: jackdied at gmail.com (Jack Diederich) Date: Thu, 16 Jul 2009 03:12:20 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: Message-ID: On Thu, Jul 16, 2009 at 2:21 AM, Mark Summerfield wrote: > Hi, > > I'm just wondering why <, <=, >=, and > are not supported by > collections.OrderedDict: > > ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) > ? ?>>> d2 = d1.copy() > ? ?>>> d2["z"] = 4 > ? ?>>> d1 == d2 > ? ?False > ? ?>>> d1 < d2 > ? ?Traceback (most recent call last): > ? ?File "", line 1, in > ? ? ? ?d1 < d2 > ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > It just seems to me that since the items in ordered dictionaries are > ordered, it would make sense to do an item by item comparison from > first to last item in exactly the same way that Python compares lists > or tuples? >>> import this In the face of ambiguity, refuse the temptation to guess. It isn't an OrderedDict thing, it is a comparison thing. Two regular dicts also raise an error if you try to LT them. What does it mean for a dict to be greater than or less than its peer? Nothing, so we refuse to guess. -Jack From list at qtrac.plus.com Thu Jul 16 03:22:20 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 00:22:20 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> On 16 July, 08:12, Jack Diederich wrote: > On Thu, Jul 16, 2009 at 2:21 AM, Mark Summerfield wrote: > > Hi, > > > I'm just wondering why <, <=, >=, and > are not supported by > > collections.OrderedDict: > > > ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) > > ? ?>>> d2 = d1.copy() > > ? ?>>> d2["z"] = 4 > > ? ?>>> d1 == d2 > > ? ?False > > ? ?>>> d1 < d2 > > ? ?Traceback (most recent call last): > > ? ?File "", line 1, in > > ? ? ? ?d1 < d2 > > ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > > It just seems to me that since the items in ordered dictionaries are > > ordered, it would make sense to do an item by item comparison from > > first to last item in exactly the same way that Python compares lists > > or tuples? > >>> import this > > In the face of ambiguity, refuse the temptation to guess. > > It isn't an OrderedDict thing, it is a comparison thing. ?Two regular > dicts also raise an error if you try to LT them. ?What does it mean > for a dict to be greater than or less than its peer? ?Nothing, so we > refuse to guess. > > -Jack You are right that it doesn't make sense to compare two dicts. But OrderedDicts can be viewed logically as lists of (key,value) tuples so they are much more like lists or tuples when it comes to comparisons. For example: >>> l = [("a", 1), ("z", 2), ("k", 3)] >>> l1 = l[:] >>> l1[1] = ("z", 5) >>> l < l1 True But if you do: >>> d = collections.OrderedDict(l) >>> d1 = collections.OrderedDict(l1) You can't use <, <=, =>, or >, even though ordered dictionaries preserve the order and their items are just as comparable as those in a list or tuple of tuples. From nagle at animats.com Thu Jul 16 03:26:18 2009 From: nagle at animats.com (John Nagle) Date: Thu, 16 Jul 2009 00:26:18 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> Message-ID: <4a5ed587$0$1627$742ec2ed@news.sonic.net> akhil1988 wrote: > Sorry, it is sgmllib.py and not sgmmlib.py Oh, that bug again. See http://bugs.python.org/issue1651995 It's a bug in SGMLParser. When Python 2.5 restricted ASCII to 0..127, SGMLParser needed to be modified, but wasn't. I reported that bug in February 2007. It was fixed in Python 2.6 and 3.0 on March 31, 2009. John Nagle From clp2 at rebertia.com Thu Jul 16 03:30:26 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 00:30:26 -0700 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: Message-ID: <50697b2c0907160030x5abbf7a4i99d397236297c850@mail.gmail.com> On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield wrote: > Hi, > > I'm just wondering why <, <=, >=, and > are not supported by > collections.OrderedDict: > > ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) > ? ?>>> d2 = d1.copy() > ? ?>>> d2["z"] = 4 > ? ?>>> d1 == d2 > ? ?False > ? ?>>> d1 < d2 > ? ?Traceback (most recent call last): > ? ?File "", line 1, in > ? ? ? ?d1 < d2 > ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > It just seems to me that since the items in ordered dictionaries are > ordered, it would make sense to do an item by item comparison from > first to last item in exactly the same way that Python compares lists > or tuples? Use case? I'm curious. Cheers, Chris -- http://blog.rebertia.com From mail at microcorp.co.za Thu Jul 16 03:43:53 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 16 Jul 2009 09:43:53 +0200 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com><50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: <001201ca05e9$2b0a61e0$0d00a8c0@Hendrik> "Hrvoje Niksic" wrote: > Note that in Python A or B is in fact not equivalent to not(not A and > not B). De Morgan would turn in his grave. - Hendrik From steven at REMOVE.THIS.cybersource.com.au Thu Jul 16 03:51:29 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 16 Jul 2009 07:51:29 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote: > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield > wrote: >> Hi, >> >> I'm just wondering why <, <=, >=, and > are not supported by >> collections.OrderedDict: >> >> ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 = >> ? ?>>> d1.copy() >> ? ?>>> d2["z"] = 4 >> ? ?>>> d1 == d2 >> ? ?False >> ? ?>>> d1 < d2 >> ? ?Traceback (most recent call last): >> ? ?File "", line 1, in >> ? ? ? ?d1 < d2 >> ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() >> >> It just seems to me that since the items in ordered dictionaries are >> ordered, it would make sense to do an item by item comparison from >> first to last item in exactly the same way that Python compares lists >> or tuples? > > Use case? I'm curious. Surely it would be the same use case as for comparing two lists? There doesn't need to be a special "OrderedDict use case" beyond "an OrderedDict is just like a list of (key,value) tuples, but searches are faster". Or maybe not. If OrderedDicts are sequences as well as mappings, then we should be able to sort them. And that seems a bit much even for me. -- Steven From eckhardt at satorlaser.com Thu Jul 16 03:56:13 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Thu, 16 Jul 2009 09:56:13 +0200 Subject: allowing output of code that is unittested? References: Message-ID: per wrote: > i am using the standard unittest module to unit test my code. my code > contains several print statements which i noticed are repressed when i > call my unit tests using: > > if __name__ == '__main__': > suite = unittest.TestLoader().loadTestsFromTestCase(TestMyCode) > unittest.TextTestRunner(verbosity=2).run(suite) I have here if __name__ == '__main__': unittest.main() preceded by classes which derive from unittest.TestCase. Running this allows me to write to stdout using print as usual, even though it's ugly because it clutters the output of the testcases. Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From steven at REMOVE.THIS.cybersource.com.au Thu Jul 16 04:19:21 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 16 Jul 2009 08:19:21 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On Thu, 16 Jul 2009 09:43:53 +0200, Hendrik van Rooyen wrote: > "Hrvoje Niksic" wrote: > > >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > De Morgan would turn in his grave. No he wouldn't. Python isn't Boolean algebra, and there is no requirement to limit Python's operators to the semantics of Boolean algebra. -- Steven From info at egenix.com Thu Jul 16 04:28:10 2009 From: info at egenix.com (eGenix Team: M.-A. Lemburg) Date: Thu, 16 Jul 2009 10:28:10 +0200 Subject: EuroPython 2009: Making 50 Mio. EUR per year using Python Message-ID: <4A5EE49A.8000207@egenix.com> Now available as video... http://www.egenix.com/company/news/EuroPython-2009-Lightning-Talk.html Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 16 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From vmalloc at gmail.com Thu Jul 16 04:43:32 2009 From: vmalloc at gmail.com (Rotem) Date: Thu, 16 Jul 2009 01:43:32 -0700 (PDT) Subject: Memory leak involving traceback objects Message-ID: Hi, I'm debugging a nasty memory leak in a framework written in Python (v2.6.2). After much digging around I found that the entire object group that is leaking is held by a frame object which is subsequently held by a traceback object. Traversing the get_referrers() of each traceback frame leads eventually to a root traceback frame which has no referrers (gc.get_referrers returns an empty list). However, this traceback object seems not to be picked by the garbage collector, and is still there even after many iterations and calls to gc.collect(). The code location to which the traceback frame points doesn't do anything special - it just catches an exception, without saving the exception itself and/or traceback anywhere. Before I dive into the Python code itself - does anyone have any idea what can cause this? Thanks, Rotem From piet at cs.uu.nl Thu Jul 16 04:48:57 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 10:48:57 +0200 Subject: How to Force exiting from program/script References: Message-ID: >>>>> Alex (A) a ?crit: >A> hi at all, >A> I have made a script with a while loop and I want that after 30 >A> seconds the program stop and exit . But the code like this doesn't >A> run: >A> In the Console I can see work so that function is correctly called... >A> #Function to exit >A> def exit(): >A> print "work" >A> raise SystemExit() >A> t = threading.Timer(30.0, exit) >A> t.start() >A> # Loop >A> while True: >A> ...many lines.... This code gives you a bit more control as it doesn't just force a system exit, but allows you to continue some other work after aborting the loop: import signal, os from threading import Timer signalcode = signal.SIGALRM class AlarmError(Exception): pass def handler(signum, frame): raise AlarmError, "command lasts too long" signal.signal(signalcode, handler) def interrupt(): os.kill(os.getpid(), signalcode) def execute(function, timeout): Timer(timeout, interrupt).start() try: function() except AlarmError, e: print e print 'Execution aborted' def long_function(): while True: pass print "The everlasting command" execute(long_function, 10) print "The End" -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From l.mastrodomenico at gmail.com Thu Jul 16 05:02:55 2009 From: l.mastrodomenico at gmail.com (Lino Mastrodomenico) Date: Thu, 16 Jul 2009 11:02:55 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <001201ca05e9$2b0a61e0$0d00a8c0@Hendrik> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <001201ca05e9$2b0a61e0$0d00a8c0@Hendrik> Message-ID: 2009/7/16 Hendrik van Rooyen : > "Hrvoje Niksic" wrote: > > >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > De Morgan would turn in his grave. If this can make him happier, in Python (not (not a and not b)) *is* equivalent to bool(a or b). (Modulo crazy things like redefining "bool" or having a __bool__ with side effects.) In the first expression you implicitly request a bool because you use "not", in the second one you do this with an explicit "bool". -- Lino Mastrodomenico From jeanmichel at sequans.com Thu Jul 16 05:06:54 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 16 Jul 2009 11:06:54 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: <4A5EEDAE.3040605@sequans.com> Nobody wrote: > On Wed, 15 Jul 2009 21:05:16 +0200, Jean-Michel Pichavant wrote: > > >> So if I resume: >> - not 'foo' => False >> - 'foo' or 'foo' => 'foo' >> >> I may be missing something, but honestly, Guido must have smoked some >> heavy stuff to write such logic, has he ? >> > > Several languages (e.g. Lisp, Bourne shell) behave the same way, i.e. "or" > returns the first element which is considered true while "and" returns the > last element provided that all preceding elements are considered true. > > [snip] > Ok then, why "or" does not return True, if the first element is considered True ? Why returning the element itself. Any reason for that ? Because it's confusing, maybe people used to that logic find it obvious, but I really don't. JM From piet at cs.uu.nl Thu Jul 16 05:09:51 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 11:09:51 +0200 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <50697b2c0907152212s5c198cdaqeb7cd97b5295163@mail.gmail.com> Message-ID: >>>>> akhil1988 (a) wrote: >a> Chris, >a> Using >a> print (u'line: %s' % line).encode('utf-8') >a> the 'line' gets printed, but actually this print statement I was using just >a> for testing, actually my code operates on 'line', on which I use line = >a> line.decode('utf-8') as 'line' is read as bytes from a stream. >a> And if I use line = line.encode('utf-8'), >a> I start getting other error like >a> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4561: >a> ordinal not in range(128) >a> at line = line.replace('<<', u'?').replace('>>', u'?') You do a Unicode replace here, so line should be a unicode string. Therefore you have to do this before the line.encode('utf-8'), but after the decode('utf-8'). It might be better to use different variables for Unicode strings and byte code strings to prevent confusion, like: 'line' is read as bytes from a stream uline = line.decode('utf-8') uline = uline.replace('<<', u'?').replace('>>', u'?') line = uline.encode('utf-8') -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jarausch at igpm.rwth-aachen.de Thu Jul 16 05:12:25 2009 From: jarausch at igpm.rwth-aachen.de (Helmut Jarausch) Date: Thu, 16 Jul 2009 11:12:25 +0200 Subject: How to search this newsgroup by a python script. Message-ID: <7c897lF26dea4U1@mid.dfncis.de> Hi, I haven't found anything with Google's group search, so let me ask it (again?). How can I search this newsgroup from within a Python script. (Perhaps by searching Google Groups or Gmane by some Python code.) Many thanks for a hint, Helmut. -- Helmut Jarausch Lehrstuhl fuer Numerische Mathematik RWTH - Aachen University D 52056 Aachen, Germany From piet at cs.uu.nl Thu Jul 16 05:21:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 11:21:54 +0200 Subject: Why aren't OrderedDicts comparable with < etc? References: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> Message-ID: >>>>> Mark (M) wrote: >M> You are right that it doesn't make sense to compare two dicts. >M> But OrderedDicts can be viewed logically as lists of (key,value) >M> tuples so they are much more like lists or tuples when it comes to >M> comparisons. >M> For example: >>>>> l = [("a", 1), ("z", 2), ("k", 3)] >>>>> l1 = l[:] >>>>> l1[1] = ("z", 5) >>>>> l < l1 >M> True >M> But if you do: >>>>> d = collections.OrderedDict(l) >>>>> d1 = collections.OrderedDict(l1) >M> You can't use <, <=, =>, or >, even though ordered dictionaries >M> preserve the order and their items are just as comparable as those in >M> a list or tuple of tuples. But why should the order be as if the OrderedDict was a list of tuples. A dict can be considered as a mapping and then you might want to treat either the key or the value as contravariant (the key I guess). So there is ambiguity. Why would the view as a list of tuples for the ordering be the `natural' view? Maybe you may expect some kind of monotonicity such that d1 URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rob.hooft at gmail.com Thu Jul 16 05:25:27 2009 From: rob.hooft at gmail.com (rwwh) Date: Thu, 16 Jul 2009 02:25:27 -0700 (PDT) Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <4A519AC0.1050905@islandtraining.com> <006eb461$0$9711$c3e8da3@news.astraweb.com> <006f13c9$0$9711$c3e8da3@news.astraweb.com> Message-ID: <1712a4da-fb9f-416e-9190-4da55fcc40ea@b14g2000yqd.googlegroups.com> On Jul 7, 2:00?pm, Steven D'Aprano wrote: > On Mon, 06 Jul 2009 22:18:20 -0700, Chris Rebert wrote: > >> Not so rare. Decimal uses unary plus. Don't assume +x is a no-op. > [...] > > Well, yes, but when would you apply it twice in a row? > > My point was that unary + isn't a no-op, and therefore neither is ++. For > Decimal, I can't think why you'd want to apply ++x, but for other > objects, who knows? > > Here's a toy example: > > >>> class Spam(str): > > ... ? ? def __pos__(self): > ... ? ? ? ? return self.__class__("spam " + self) > ...>>> s = Spam("") > >>> ++++s > > 'spam spam spam spam ' Here's another toy example: class Toy(int): def __init__(self, value): self._incrd = False int.__init__(self, value) def incrHalf(self): self._incrd = True def __pos__(self): if self._incrd: return self.__class__(self+1) else: p = self.__class__(self) p.incrHalf() return p def __add__(self, other): return self.__class__(int(self)+other) nows122[126]~% python -i toy.py >>> i=Toy(5) >>> +i 5 >>> ++i 6 >>> +++i 6 >>> +i++i 10 >>> +(+i++i) 10 >>> (++i)++i 11 From clp2 at rebertia.com Thu Jul 16 05:33:24 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 02:33:24 -0700 Subject: How to search this newsgroup by a python script. In-Reply-To: <7c897lF26dea4U1@mid.dfncis.de> References: <7c897lF26dea4U1@mid.dfncis.de> Message-ID: <50697b2c0907160233j70ba360fv69f6f48e509b55aa@mail.gmail.com> On Thu, Jul 16, 2009 at 2:12 AM, Helmut Jarausch wrote: > Hi, > > I haven't found anything with Google's group search, so let me > ask it (again?). > > How can I search this newsgroup from within a Python script. > (Perhaps by searching Google Groups or Gmane by some Python code.) 1. Generate URL of the form: http://search.gmane.org/?query=foo&group=gmane.comp.python.general where "foo" is the search terms, with proper URL escaping applied. 2. Fetch URL using urllib - http://docs.python.org/library/urllib.html 3. Parse resulting HTML page (e.g. using BeautifulSoup) 4. Extract desired information from search results using the parse tree. 5. ??? 6. Profit! Cheers, Chris -- http://blog.rebertia.com From mail at timgolden.me.uk Thu Jul 16 05:38:05 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 16 Jul 2009 10:38:05 +0100 Subject: How to search this newsgroup by a python script. In-Reply-To: <50697b2c0907160233j70ba360fv69f6f48e509b55aa@mail.gmail.com> References: <7c897lF26dea4U1@mid.dfncis.de> <50697b2c0907160233j70ba360fv69f6f48e509b55aa@mail.gmail.com> Message-ID: <4A5EF4FD.7080708@timgolden.me.uk> Chris Rebert wrote: > On Thu, Jul 16, 2009 at 2:12 AM, Helmut > Jarausch wrote: >> Hi, >> >> I haven't found anything with Google's group search, so let me >> ask it (again?). >> >> How can I search this newsgroup from within a Python script. >> (Perhaps by searching Google Groups or Gmane by some Python code.) > > 1. Generate URL of the form: > http://search.gmane.org/?query=foo&group=gmane.comp.python.general > where "foo" is the search terms, with proper URL escaping applied. > 2. Fetch URL using urllib - http://docs.python.org/library/urllib.html > 3. Parse resulting HTML page (e.g. using BeautifulSoup) > 4. Extract desired information from search results using the parse tree. > 5. ??? > 6. Profit! Alternatively, you could do something with the mailing list archive: http://mail.python.org/pipermail/python-list/ The .gz files are gzipped mbox format so can be dropped into, eg, Thunderbird for offline browsing, or searched with the email package from the stdlib. TJG From user at example.net Thu Jul 16 06:34:20 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 12:34:20 +0200 Subject: using timers to force an execution time Message-ID: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> hello. based upon previuos suggestions, i tried to write a program in which i would like to have a certain code fragment to execute only for a specified amount of time (say 3 seconds), then bail out. but i get the following: $ cat tmr004.py #!/usr/bin/python -u import threading , time e = threading.Event() t = threading.Timer(3.0, e.set) t.start() print time.asctime(time.localtime(time.time())) while not e.isSet(): for repeat in range(10): print time.time() time.sleep(0.66) print time.asctime(time.localtime(time.time())) $ ./tmr004.py Thu Jul 16 12:31:27 2009 1247740287.44 1247740288.1 1247740288.76 1247740289.42 1247740290.08 1247740290.74 1247740291.4 1247740292.06 1247740292.72 1247740293.38 Thu Jul 16 12:31:34 2009 $ see? the while body ran for about 7 seconds... i bet it has to do with the fact that the timer does not control inner loops... any suggestion? $ python -V Python 2.3.4 $ uname -a Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux $ bye From contact at xavierho.com Thu Jul 16 06:49:07 2009 From: contact at xavierho.com (Xavier Ho) Date: Thu, 16 Jul 2009 18:49:07 +0800 Subject: using timers to force an execution time In-Reply-To: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <2d56febf0907160349k7c4eaa50t42216ce85bcb1689@mail.gmail.com> On Thu, Jul 16, 2009 at 6:34 PM, superpollo wrote: > hello. > > based upon previuos suggestions, i tried to write a program in which i > would like to have a certain code fragment to execute only for a specified > amount of time (say 3 seconds), then bail out. > > > see? the while body ran for about 7 seconds... i bet it has to do with the > fact that the timer does not control inner loops... any suggestion? > > I'm guessing it has to do with a sleep of 0.66 seconds x 10 times = sleep for 6.6 seconds, and then it checks for e.isSet(). Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From list at qtrac.plus.com Thu Jul 16 06:50:25 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 03:50:25 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> Message-ID: On 16 July, 10:21, Piet van Oostrum wrote: > >>>>> Mark (M) wrote: > >M> You are right that it doesn't make sense to compare two dicts. > >M> But OrderedDicts can be viewed logically as lists of (key,value) > >M> tuples so they are much more like lists or tuples when it comes to > >M> comparisons. > >M> For example: > >>>>> l = [("a", 1), ("z", 2), ("k", 3)] > >>>>> l1 = l[:] > >>>>> l1[1] = ("z", 5) > >>>>> l < l1 > >M> True > >M> But if you do: > >>>>> d = collections.OrderedDict(l) > >>>>> d1 = collections.OrderedDict(l1) > >M> You can't use <, <=, =>, or >, even though ordered dictionaries > >M> preserve the order and their items are just as comparable as those in > >M> a list or tuple of tuples. > > But why should the order be as if the OrderedDict was a list of tuples. > A dict can be considered as a mapping and then you might want to treat > either the key or the value as contravariant (the key I guess). So there > is ambiguity. Why would the view as a list of tuples for the ordering be > the `natural' view? > > Maybe you may expect some kind of monotonicity such that d1 d1[x] 2:5}. So maybe there is only a partial ordering? OK, that seems to me to be a convincing argument against supporting ordering. From user at example.net Thu Jul 16 06:56:50 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 12:56:50 +0200 Subject: turtle dump Message-ID: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> hi there. is there a way to dump the content of a turtle window to a file or a file object? bye From list at qtrac.plus.com Thu Jul 16 06:58:08 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 03:58:08 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: <401967fb-c12e-4f63-8b05-6dffa0688028@a26g2000yqn.googlegroups.com> On 16 July, 08:51, Steven D'Aprano wrote: > On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote: > > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield > > wrote: > >> Hi, > > >> I'm just wondering why <, <=, >=, and > are not supported by > >> collections.OrderedDict: > > >> ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 = > >> ? ?>>> d1.copy() > >> ? ?>>> d2["z"] = 4 > >> ? ?>>> d1 == d2 > >> ? ?False > >> ? ?>>> d1 < d2 > >> ? ?Traceback (most recent call last): > >> ? ?File "", line 1, in > >> ? ? ? ?d1 < d2 > >> ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > >> It just seems to me that since the items in ordered dictionaries are > >> ordered, it would make sense to do an item by item comparison from > >> first to last item in exactly the same way that Python compares lists > >> or tuples? > > > Use case? I'm curious. > > Surely it would be the same use case as for comparing two lists? There > doesn't need to be a special "OrderedDict use case" beyond "an > OrderedDict is just like a list of (key,value) tuples, but searches are > faster". > > Or maybe not. If OrderedDicts are sequences as well as mappings, then we > should be able to sort them. And that seems a bit much even for me. > > -- > Steven One thing that I've just noticed is that you can use <, <=, >=, and > with sets: >>> s1 = {1,2,3} >>> s2 = {1,2,4} >>> s1 == s2 False >>> s1 < s2 False >>> s1 <= s2 False >>> s2 < s1 False >>> s2 <= s1 False >>> s1 != s2 True It seems a bit inconsistent that with sets you always get False when using an ordering operator but with an ordered dict you get an exception? From list at qtrac.plus.com Thu Jul 16 06:59:47 2009 From: list at qtrac.plus.com (Mark) Date: Thu, 16 Jul 2009 03:59:47 -0700 (PDT) Subject: Why aren't OrderedDicts comparable with < etc? References: <401967fb-c12e-4f63-8b05-6dffa0688028@a26g2000yqn.googlegroups.com> Message-ID: <028e48c2-4a3a-4920-9a67-d9bb95714b4f@c36g2000yqn.googlegroups.com> On 16 July, 11:58, Mark wrote: > On 16 July, 08:51, Steven D'Aprano > > > > wrote: > > On Thu, 16 Jul 2009 00:30:26 -0700, Chris Rebert wrote: > > > On Wed, Jul 15, 2009 at 11:21 PM, Mark Summerfield > > > wrote: > > >> Hi, > > > >> I'm just wondering why <, <=, >=, and > are not supported by > > >> collections.OrderedDict: > > > >> ? ?>>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) d2 = > > >> ? ?>>> d1.copy() > > >> ? ?>>> d2["z"] = 4 > > >> ? ?>>> d1 == d2 > > >> ? ?False > > >> ? ?>>> d1 < d2 > > >> ? ?Traceback (most recent call last): > > >> ? ?File "", line 1, in > > >> ? ? ? ?d1 < d2 > > >> ? ?TypeError: unorderable types: OrderedDict() < OrderedDict() > > > >> It just seems to me that since the items in ordered dictionaries are > > >> ordered, it would make sense to do an item by item comparison from > > >> first to last item in exactly the same way that Python compares lists > > >> or tuples? > > > > Use case? I'm curious. > > > Surely it would be the same use case as for comparing two lists? There > > doesn't need to be a special "OrderedDict use case" beyond "an > > OrderedDict is just like a list of (key,value) tuples, but searches are > > faster". > > > Or maybe not. If OrderedDicts are sequences as well as mappings, then we > > should be able to sort them. And that seems a bit much even for me. > > > -- > > Steven > > One thing that I've just noticed is that you can use <, <=, >=, and > > with sets: > > >>> s1 = {1,2,3} > >>> s2 = {1,2,4} > >>> s1 == s2 > False > >>> s1 < s2 > False > >>> s1 <= s2 > False > >>> s2 < s1 > False > >>> s2 <= s1 > False > >>> s1 != s2 > > True > > It seems a bit inconsistent that with sets you always get False when > using an ordering operator but with an ordered dict you get an > exception? Ooops---disregard the above---I forgot that these do subset and superset comparisions! From akhilanger at gmail.com Thu Jul 16 07:00:00 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 04:00:00 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <4a5ed587$0$1627$742ec2ed@news.sonic.net> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> Message-ID: <24514309.post@talk.nabble.com> I have switched to python 3.1 , but now I am getting some syntax errors in the code: File "./customWikiExtractor.py", line 81 __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', '¢' :u'\u00A2', ^ SyntaxError: invalid syntax line 81 is: __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', '¢' :u'\u00A2', '£' :u'\u00A3', '¤':u'\u00A4', '¥' :u'\u00A5', '¦' :u'\u00A6', '§' :u'\u00A7', '¨' :u'\u00A8', '©' :u'\u00A9', 'ª' :u'\u00AA', '«' :u'\u00AB', '¬' :u'\u00AC', '­' :u'\u00AD', '®' :u'\u00AE', '¯' :u'\u00AF', '°' :u'\u00B0', '±' :u'\u00B1', '²' :u'\u00B2', '³' :u'\u00B3', '´' :u'\u00B4', 'µ' :u'\u00B5', '¶' :u'\u00B6', '·' :u'\u00B7', '¸' :u'\u00B8', '¹' :u'\u00B9', 'º' :u'\u00BA',} --Akhil John Nagle-2 wrote: > > akhil1988 wrote: >> Sorry, it is sgmllib.py and not sgmmlib.py > > Oh, that bug again. See > > http://bugs.python.org/issue1651995 > > It's a bug in SGMLParser. When Python 2.5 restricted ASCII to 0..127, > SGMLParser needed to be modified, but wasn't. > > I reported that bug in February 2007. It was fixed in > Python 2.6 and 3.0 on March 31, 2009. > > John Nagle > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24514309.html Sent from the Python - python-list mailing list archive at Nabble.com. From user at example.net Thu Jul 16 07:01:34 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:01:34 +0200 Subject: using timers to force an execution time In-Reply-To: <7c8fgbF26vgl2U1@mid.uni-berlin.de> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> Message-ID: <4a5f088f$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > What you can't achieve in python without major black magic hackery that is > very dangerous is to terminate a thread while it is executing. Termination > has to be co-operative. i see. thanks a lot. bye From deets at nospam.web.de Thu Jul 16 07:03:55 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:03:55 +0200 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <7c8fgbF26vgl2U1@mid.uni-berlin.de> superpollo wrote: > hello. > > based upon previuos suggestions, i tried to write a program in which i > would like to have a certain code fragment to execute only for a > specified amount of time (say 3 seconds), then bail out. > > but i get the following: > > $ cat tmr004.py > #!/usr/bin/python -u > > import threading , time > > e = threading.Event() > t = threading.Timer(3.0, e.set) > t.start() > print time.asctime(time.localtime(time.time())) > while not e.isSet(): > for repeat in range(10): > print time.time() > time.sleep(0.66) > print time.asctime(time.localtime(time.time())) > $ ./tmr004.py > Thu Jul 16 12:31:27 2009 > 1247740287.44 > 1247740288.1 > 1247740288.76 > 1247740289.42 > 1247740290.08 > 1247740290.74 > 1247740291.4 > 1247740292.06 > 1247740292.72 > 1247740293.38 > Thu Jul 16 12:31:34 2009 > $ > > see? the while body ran for about 7 seconds... i bet it has to do with > the fact that the timer does not control inner loops... any suggestion? Of course the inner loop isn't affected by the set event - how should it be, if you don't check it. if you rewrite it as this: while True: for repeat in range(10): if e.isSet(): break print time.time() time.sleep(.66) it should terminate earlier. What you can't achieve in python without major black magic hackery that is very dangerous is to terminate a thread while it is executing. Termination has to be co-operative. Diez From user at example.net Thu Jul 16 07:04:08 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:04:08 +0200 Subject: using timers to force an execution time In-Reply-To: <7c8fgbF26vgl2U1@mid.uni-berlin.de> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> Message-ID: <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: >>see? the while body ran for about 7 seconds... i bet it has to do with >>the fact that the timer does not control inner loops... any suggestion? > > > Of course the inner loop isn't affected by the set event - how should it be, > if you don't check it. > > if you rewrite it as this: > > while True: > for repeat in range(10): > if e.isSet(): > break > print time.time() > time.sleep(.66) > > it should terminate earlier. so it seems almost impossible to allocate a certain amount of time to a code fragment *without* somehow rewriting its logic? am i wrong? bye From akhilanger at gmail.com Thu Jul 16 07:04:24 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 04:04:24 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24514309.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <24514309.post@talk.nabble.com> Message-ID: <24514367.post@talk.nabble.com> Please click reply on the post and then read this reply in the editor. Actually, some sequences have been replaced to their graphical form when this post is published. So the python code is being displayed, what actually it is not. --Akhil akhil1988 wrote: > > I have switched to python 3.1 , but now I am getting some syntax errors in > the code: > > File "./customWikiExtractor.py", line 81 > __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', > '¢' :u'\u00A2', > ^ > SyntaxError: invalid syntax > > line 81 is: > __char_entities = {' ' :u'\u00A0', '¡' :u'\u00A1', '¢' > :u'\u00A2', > '£' :u'\u00A3', '¤':u'\u00A4', > '¥' :u'\u00A5', > '¦' :u'\u00A6', '§' :u'\u00A7', > '¨' :u'\u00A8', > '©' :u'\u00A9', 'ª' :u'\u00AA', > '«' :u'\u00AB', > '¬' :u'\u00AC', '­' :u'\u00AD', > '®' :u'\u00AE', > '¯' :u'\u00AF', '°' :u'\u00B0', > '±' :u'\u00B1', > '²' :u'\u00B2', '³' :u'\u00B3', > '´' :u'\u00B4', > 'µ' :u'\u00B5', '¶' :u'\u00B6', > '·' :u'\u00B7', > '¸' :u'\u00B8', '¹' :u'\u00B9', > 'º' :u'\u00BA',} > > --Akhil > > > John Nagle-2 wrote: >> >> akhil1988 wrote: >>> Sorry, it is sgmllib.py and not sgmmlib.py >> >> Oh, that bug again. See >> >> http://bugs.python.org/issue1651995 >> >> It's a bug in SGMLParser. When Python 2.5 restricted ASCII to 0..127, >> SGMLParser needed to be modified, but wasn't. >> >> I reported that bug in February 2007. It was fixed in >> Python 2.6 and 3.0 on March 31, 2009. >> >> John Nagle >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24514367.html Sent from the Python - python-list mailing list archive at Nabble.com. From deets at nospam.web.de Thu Jul 16 07:08:40 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:08:40 +0200 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> Message-ID: <7c8fp8F26vgl2U2@mid.uni-berlin.de> Diez B. Roggisch wrote: > superpollo wrote: > >> hello. >> >> based upon previuos suggestions, i tried to write a program in which i >> would like to have a certain code fragment to execute only for a >> specified amount of time (say 3 seconds), then bail out. >> >> but i get the following: >> >> $ cat tmr004.py >> #!/usr/bin/python -u >> >> import threading , time >> >> e = threading.Event() >> t = threading.Timer(3.0, e.set) >> t.start() >> print time.asctime(time.localtime(time.time())) >> while not e.isSet(): >> for repeat in range(10): >> print time.time() >> time.sleep(0.66) >> print time.asctime(time.localtime(time.time())) >> $ ./tmr004.py >> Thu Jul 16 12:31:27 2009 >> 1247740287.44 >> 1247740288.1 >> 1247740288.76 >> 1247740289.42 >> 1247740290.08 >> 1247740290.74 >> 1247740291.4 >> 1247740292.06 >> 1247740292.72 >> 1247740293.38 >> Thu Jul 16 12:31:34 2009 >> $ >> >> see? the while body ran for about 7 seconds... i bet it has to do with >> the fact that the timer does not control inner loops... any suggestion? > > Of course the inner loop isn't affected by the set event - how should it > be, if you don't check it. > > if you rewrite it as this: > > while True: > for repeat in range(10): > if e.isSet(): > break > print time.time() > time.sleep(.66) > > it should terminate earlier. This is of course wrong, remove the outer "while" Diez From user at example.net Thu Jul 16 07:09:24 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:09:24 +0200 Subject: using timers to force an execution time In-Reply-To: <7c8futF26vgl2U3@mid.uni-berlin.de> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> Message-ID: <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: >>am i wrong? > > > No, you are right, for threads that is. You can try & trick around with the > trace-functionality of python, and some ctypes-based > system-thread-module-tricks that are, as mentioned before, black-magic & a > spinning gatling gun pointed to your very own lower extremities. > OUCH. > The only reliable way of terminating an asynchronous computation is to use > processes, since python2.6 that's rather convenient with the > multiprocessing-module. However, that imposes some limits to what you can > do. fair enough. very helpful indeed. bye From deets at nospam.web.de Thu Jul 16 07:11:41 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:11:41 +0200 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <7c8futF26vgl2U3@mid.uni-berlin.de> superpollo wrote: > Diez B. Roggisch wrote: >> superpollo wrote: >>>see? the while body ran for about 7 seconds... i bet it has to do with >>>the fact that the timer does not control inner loops... any suggestion? >> >> >> Of course the inner loop isn't affected by the set event - how should it >> be, if you don't check it. >> >> if you rewrite it as this: >> >> while True: >> for repeat in range(10): >> if e.isSet(): >> break >> print time.time() >> time.sleep(.66) >> >> it should terminate earlier. > > so it seems almost impossible to allocate a certain amount of time to a > code fragment *without* somehow rewriting its logic? > > am i wrong? No, you are right, for threads that is. You can try & trick around with the trace-functionality of python, and some ctypes-based system-thread-module-tricks that are, as mentioned before, black-magic & a spinning gatling gun pointed to your very own lower extremities. The only reliable way of terminating an asynchronous computation is to use processes, since python2.6 that's rather convenient with the multiprocessing-module. However, that imposes some limits to what you can do. Diez From deets at nospam.web.de Thu Jul 16 07:15:04 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 16 Jul 2009 13:15:04 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <7c8g58F26vgl2U4@mid.uni-berlin.de> superpollo wrote: > hi there. > > is there a way to dump the content of a turtle window to a file or a > file object? Why should I want to dump a turtle? They are very benign creatures, dumping them is going to hurt them needlessly. Without a cheek-in-tongue: how are we supposed to know what a"turtle window" is, how you create it, what features it uses? You need to give more details, such as the module you are using, on what system that runs and so forth. Diez From user at example.net Thu Jul 16 07:18:51 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 13:18:51 +0200 Subject: turtle dump In-Reply-To: <7c8g58F26vgl2U4@mid.uni-berlin.de> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> Message-ID: <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: > > >>hi there. >> >>is there a way to dump the content of a turtle window to a file or a >>file object? > > > Why should I want to dump a turtle? They are very benign creatures, dumping > them is going to hurt them needlessly. > lol. ;-) the title was indeed supposed to stir a bit of curiosity upon the reader... > Without a cheek-in-tongue: how are we supposed to know what a"turtle window" > is, how you create it, what features it uses? You need to give more > details, such as the module you are using, on what system that runs and so > forth. in fact i was looking for a *platform independent* way to draw into a graphics file (say, a postscript or a png) using the turtle module. so i understand that "dumping a window" is not a good expression... maybe "redirecting graphics commands to a file instead of a window"? bye From wuwei23 at gmail.com Thu Jul 16 07:24:30 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 04:24:30 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> Message-ID: <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> On Jul 16, 9:00?pm, akhil1988 wrote: > I have switched to python 3.1 , but now I am getting some syntax errors in > the code: Python 3.x was a major release that endeavoured to clean up a number of lingering issues with the language, the upshot being that it isn't entirely backwards compatible with past versions. Unicode became the default string type, which is what is causing the error here: the u- prefix is no longer required (or even allowed). However, Py3.x _does_ come with a handy tool for automatically converting Python 2.x code to 3.x, called 2to3. One of the things it should do is convert Py2.x unicode values into their correct representation in 3.x. With any luck, it should be able to convert the code you're using entirely. Let us know how it goes. From wuwei23 at gmail.com Thu Jul 16 07:50:29 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 04:50:29 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> On Jul 16, 9:18?pm, superpollo wrote: > lol. ;-) the title was indeed supposed to stir a bit of curiosity upon > the reader... Which isn't really useful when trying to obtain assistance... you want certainty, not curiosity. > in fact i was looking for a *platform independent* way to draw into a > graphics file (say, a postscript or a png) using the turtle module. so i > understand that "dumping a window" is not a good expression... maybe > "redirecting graphics commands to a file instead of a window"? You didn't actually answer Diez question. The turtle module is only a recent (2.6/3.0) addition to Python and is probably obscure enough not to have tracked across everyone's radar. So _if_ you're talking about the standard lib turtle module, you're not looking to 'dump a window', you're looking to write the contents of turtle.Canvas to file. Canvas only supports writing to postscript, so you'll have to do something like this (untested): import turtle screen = turtle.Screen() # go go gadget turtle... screen._canvas.postscript(file='turtle.ps') Or it may be open('turtle.ps','w').write(screen._canvas.postscript ())... I couldn't find a definitive answer. Try looking through the Tk docs, it should be covered there. Note that this is a postscript _text_ file, so you'll also need to find something to render it with. Hope this helps point you in the right direction. From user at example.net Thu Jul 16 08:11:07 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 14:11:07 +0200 Subject: turtle dump In-Reply-To: <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> Message-ID: <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> alex23 wrote: > On Jul 16, 9:18 pm, superpollo wrote: > >>lol. ;-) the title was indeed supposed to stir a bit of curiosity upon >>the reader... > > > Which isn't really useful when trying to obtain assistance... you want > certainty, not curiosity. > ok. my bad. > >>in fact i was looking for a *platform independent* way to draw into a >>graphics file (say, a postscript or a png) using the turtle module. so i >>understand that "dumping a window" is not a good expression... maybe >>"redirecting graphics commands to a file instead of a window"? > > > You didn't actually answer Diez question. The turtle module is only a > recent (2.6/3.0) addition to Python and is probably obscure enough not > to have tracked across everyone's radar. > actually i am still using 2.3.4, which means that... > screen = turtle.Screen() ... is not possible > > Hope this helps point you in the right direction. it certainly did, tahnks. bye From bearophileHUGS at lycos.com Thu Jul 16 08:22:22 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Thu, 16 Jul 2009 05:22:22 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <267d74e2-97ce-477c-a39b-022fc4f82bd3@i6g2000yqj.googlegroups.com> Superchicken: > is there a way to dump the content of a turtle window to a file or a file object? A possible low-tech solution is to append to a list the sequence of your plotting commands (using a decorator too, even, something like the logging decorator), and then save all of them at the end into a text file :-) Bye, bearophile From piet at cs.uu.nl Thu Jul 16 08:39:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 14:39:54 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: Message-ID: >>>>> mheavner (m) wrote: >m> I'm using multiprocessing to spawn several subprocesses, each of which >m> uses a very large data structure (making it impractical to pass it via >m> pipes / pickling). I need to allocate this structure once when the >m> process is created and have it remain in memory for the duration of >m> the process. The way the multiprocessing module is set up, only the >m> 'run' method runs within the subprocess - so creating a wrapper class >m> with a constructor that allocates the structure in __init__ will not >m> work, as far as I know, as this will still be within the parent >m> process. >m> If I were working in C/C++, I would declare the variable "static" >m> within the function body - is there any way with the multiprocessing >m> module to have persistent data members within subprocesses? >m> Any ideas?? Your post is not entirely clear. Is `the process' the same as `the subprocess'? Assuming it is, what is the problem? You can create the datastructure first thing in the run method can't you? Like this: from multiprocessing import Process from time import sleep from random import random class MyProcess(Process): def __init__(self, number): self.number = number Process.__init__(self) def run(self): print "Process %s started" % self.number self.data = range(self.number * 100000, (self.number + 1) * 100000) self.doit() def doit(self): for i in range(5): sleep(3 * random()) self.data[i] += i print self.data[i] processes = [] for k in range(10): p = MyProcess(k) p.start() processes.append(p) for p in processes: p.join() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From albert at spenarnc.xs4all.nl Thu Jul 16 08:45:27 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 16 Jul 2009 12:45:27 GMT Subject: Clarity vs. code reuse/generality References: Message-ID: In article , kj wrote: > > >I'm will be teaching a programming class to novices, and I've run >into a clear conflict between two of the principles I'd like to >teach: code clarity vs. code reuse. I'd love your opinion about >it. > > >This seemed straightforward enough, until I realized that, to be >useful to my students in their homework, this _binary_search function >had to handle the case in which the passed function was monotonically >decreasing in the specified interval... > > >Here's the rub: the code above is more general (hence more reusable) >by virtue of this trick with the sense parameter, but it is also >a bit harder to understand. > >This not an unusual situation. I find that the processing of >abstracting out common logic often results in code that is harder >to read, at least for the uninitiated... Yes, of course. You're teaching, say, green belt programmers. Good reusable code requires a fifth dan. You may not be up to it yourself (no offense intended), let alone your students. Writing a reusable binary search is a very different assignment from being able to code/use it in a concrete example. Their choice must be between *writing* a one-off binary search, versus *using* a brilliantly code, brilliantly documented binary search that has been given beforehand. Even assuming the reusable code is perfect in all sense, reusing may be more effort than writing from scratch. Then *you* have to explain them about the benefits of reuse. (The other benefits, of course.) > >I'd love to know your opinions on this. You're welcome. >kj Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From __peter__ at web.de Thu Jul 16 08:46:40 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 16 Jul 2009 14:46:40 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: superpollo wrote: > alex23 wrote: >> On Jul 16, 9:18 pm, superpollo wrote: >> >>>lol. ;-) the title was indeed supposed to stir a bit of curiosity upon >>>the reader... >> >> >> Which isn't really useful when trying to obtain assistance... you want >> certainty, not curiosity. >> > > ok. my bad. > >> >>>in fact i was looking for a *platform independent* way to draw into a >>>graphics file (say, a postscript or a png) using the turtle module. so i >>>understand that "dumping a window" is not a good expression... maybe >>>"redirecting graphics commands to a file instead of a window"? >> >> >> You didn't actually answer Diez question. The turtle module is only a >> recent (2.6/3.0) addition to Python and is probably obscure enough not >> to have tracked across everyone's radar. >> > > actually i am still using 2.3.4, which means that... > >> screen = turtle.Screen() > > ... is not possible Tested on 2.4: >>> import turtle >>> turtle.reset() >>> for i in range(4): ... turtle.forward(50) ... turtle.right(90) ... >>> turtle._canvas.postscript(file="tmp.ps") '' I think the big rewrite has happened in 2.6, so the above should also work in 2.3. Peter From user at example.net Thu Jul 16 09:07:26 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 15:07:26 +0200 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: <4a5f260f$0$18931$4fafbaef@reader2.news.tin.it> Peter Otten wrote: > Tested on 2.4: > > >>>>import turtle >>>>turtle.reset() >>>>for i in range(4): > > ... turtle.forward(50) > ... turtle.right(90) > ... > >>>>turtle._canvas.postscript(file="tmp.ps") > > '' > > I think the big rewrite has happened in 2.6, so the above should also work > in 2.3. > > Peter > mr otten, you are great. bye From hartley79 at gmail.com Thu Jul 16 09:10:35 2009 From: hartley79 at gmail.com (hartley) Date: Thu, 16 Jul 2009 06:10:35 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> Message-ID: /* the first telling */ (...) /* the third telling */ (...) /* the third telling */ - If you had loosened up on the sarcasm I would probably have read what you wrote more thoroughly instead of just skimming through it. Thanks for the help, but you should seriously consider doing something with your patronizing attitude. HTH, Hartley From hartley79 at gmail.com Thu Jul 16 09:13:09 2009 From: hartley79 at gmail.com (hartley) Date: Thu, 16 Jul 2009 06:13:09 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> Message-ID: <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> /* the first telling */ (...) /* the second telling */ (...) /* the third telling */ - If you had loosened up on the sarcasm I would probably have read what you wrote more thoroughly instead of just skimming through it. Thanks for the help, but you should seriously consider doing something with your patronizing attitude. HTH, Hartley From albert at spenarnc.xs4all.nl Thu Jul 16 09:15:59 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 16 Jul 2009 13:15:59 GMT Subject: Clarity vs. code reuse/generality References: <006eb26f$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Dennis Lee Bieber wrote: >On 07 Jul 2009 05:05:12 GMT, Steven D'Aprano > declaimed the following in >gmane.comp.python.general: > >> Paraphrasing the Collins Dictionary of Mathematics: >> >> opposite sense. Sense is also used to distinguish clockwise and anti- >> clockwise. >> > Which, I believe, is the only usage I've encountered of it... In >regards to quantum spin states in such things as Scientific American >(though that magazine has either gone down hill in the last 30 years, or >my expectations have gone up... Current issues read like the first years >of Discover magazine) I dropped my subscription when power was expressed in multiples of US hair dryers. (I could calculate that back, and was appalled by the energy wastage of US hair dryers. ;-) ) >-- > Wulfraed Dennis Lee Bieber KD6MOG Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From matt.dubins at sympatico.ca Thu Jul 16 09:17:27 2009 From: matt.dubins at sympatico.ca (inkhorn) Date: Thu, 16 Jul 2009 06:17:27 -0700 (PDT) Subject: How to check if any item from a list of strings is in a big string? References: <7x63e1kynt.fsf@ruckus.brouhaha.com> <07f46fcd-a3ee-414e-a6d0-ce541f61be39@f33g2000vbm.googlegroups.com> <22fa2fe9-899b-407a-8e3c-49f29c86fa59@r33g2000yqn.googlegroups.com> Message-ID: Hi all, This was more a question of programming aesthetics for me than one of great practical significance. I was looking to perform a certain function on files in a directory so long as those files weren't found in certain standard directories. In other words, I was using os.walk () to get multiple root directory strings, and the lists of files in each directory. The function was to be performed on those files, so long as certain terms weren't in the root directory string. In actuality, I could have stuck with the helper function I created, but I'm always curious to see how well multiple lines of code can turn into fewer lines of code in python and retain the same functional value :) Matt On Jul 15, 6:46?am, denis wrote: > Sure, Aho-Corasick is fast for fixedstrings; but without real > numbers / a concrete goal > > > > Matt, how many words are you looking for, in how long a string ? > > a simple solution is good enough, satisficing. ?Matt asked "how to > make that function look nicer?" > but "nice" has many dimensions -- bicycles are nice for some tasks, > Ferraris for others. > > Bythewayhttp://en.wikipedia.org/wiki/Aho-Corasick_algorithmhas a > link to a Python implementation, > alsohttp://en.wikipedia.org/wiki/Worse_is_Betteris fun. From motoom at xs4all.nl Thu Jul 16 09:17:47 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Thu, 16 Jul 2009 15:17:47 +0200 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: <4A5F287B.2000503@xs4all.nl> I got success with the following code (python 2.6.2): import turtle turtle.reset() for i in range(4): turtle.forward(50) turtle.right(90) can=turtle.getscreen().getcanvas() can.postscript(file="tmp.ps") -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From miheavner at gmail.com Thu Jul 16 09:18:43 2009 From: miheavner at gmail.com (mheavner) Date: Thu, 16 Jul 2009 06:18:43 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: Message-ID: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> On Jul 16, 8:39?am, Piet van Oostrum wrote: > >>>>> mheavner (m) wrote: > >m> I'm using multiprocessing to spawn several subprocesses, each of which > >m> uses a very large data structure (making it impractical to pass it via > >m> pipes / pickling). I need to allocate this structure once when the > >m> process is created and have it remain in memory for the duration of > >m> the process. The way the multiprocessing module is set up, only the > >m> 'run' method runs within the subprocess - so creating a wrapper class > >m> with a constructor that allocates the structure in __init__ will not > >m> work, as far as I know, as this will still be within the parent > >m> process. > >m> If I were working in C/C++, I would declare the variable "static" > >m> within the function body - is there any way with the multiprocessing > >m> module to have persistent data members within subprocesses? > >m> Any ideas?? > > Your post is not entirely clear. Is `the process' the same as `the > subprocess'? > > Assuming it is, what is the problem? You can create the datastructure > first thing in the run method can't you? > > Like this: > > from multiprocessing import Process > from time import sleep > from random import random > > class MyProcess(Process): > > ? ? def __init__(self, number): > ? ? ? ? self.number = number > ? ? ? ? Process.__init__(self) > > ? ? def run(self): > ? ? ? ? print "Process %s started" % self.number > ? ? ? ? self.data = range(self.number * 100000, (self.number + 1) * 100000) > ? ? ? ? self.doit() > > ? ? def doit(self): > ? ? ? ? for i in range(5): > ? ? ? ? ? ? sleep(3 * random()) > ? ? ? ? ? ? self.data[i] += i > ? ? ? ? ? ? print self.data[i] > > processes = [] > for k in range(10): > ? ? p = MyProcess(k) > ? ? p.start() > ? ? processes.append(p) > > for p in processes: > ? ? p.join() > > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org 'The process' refers to the subprocess. I could do as you say, load the data structure each time, but the problem is that takes a considerable amount of time compared to the the actual computation with the data it contains. I'm using these processes within a loop as follows: # Don't recreate processes or Queues pop1 = Queue() pop2 = Queue() pop_out = Queue() p1 = CudaProcess(0, args=(costf,pop1,pop_out)) p2 = CudaProcess(1, args=(costf,pop2,pop_out)) # Main loop for i in range(maxiter): print 'ITERATION: '+str(i) if log != None: l = open(log,'a') l.write('Iteration: '+str(i)+'\n') l.close() # Split population in two pop1.putmany(pop[0:len(pop)/2]) pop2.putmany(pop[len(pop)/2:len(pop)]) # Start two processes if not p1.isAlive(): p1.start() print 'started %s'%str(p1.getPid()) else: p1.run() if not p2.isAlive(): p2.start() print 'started %s'%str(p2.getPid()) else: p2.run() . . . So I'd like to load that data into memory once and keep there as long as the process is alive (ideally when the subprocess is created, storing some sort of pointer to it), rather than loading it each time run is called for a process within the loop. Could be my CudaProcess class - I'll check out what Diez suggested and post back. From albert at spenarnc.xs4all.nl Thu Jul 16 09:19:08 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 16 Jul 2009 13:19:08 GMT Subject: Clarity vs. code reuse/generality References: <006ebea2$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Lie Ryan wrote: >Steven D'Aprano wrote: >> On Tue, 07 Jul 2009 05:13:28 +0000, Lie Ryan wrote: >> >>> When people are fighting over things like `sense`, although sense may >>> not be strictly wrong dictionary-wise, it smells of something burning... >> >> That would be my patience. >> >> I can't believe the direction this discussion has taken. > >Me neither. > >> Anybody sensible >> would be saying "Oh wow, I've just learned a new meaning to the word, >> that's great, I'm now less ignorant than I was a minute ago". But oh no, >> we mustn't use a standard meaning to a word, heaven forbid we disturb >> people's ignorance by teaching them something new. > >A meaning of a word is meaningless if nobody apart the writer >understands it. The purpose of code is 1) to communicate with the Exactly. And the OP teaches to scientist. They know sense in that meaning. Maybe you don't, but that is irrelevant. Groetjes Albert. -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From anthony.tolle at gmail.com Thu Jul 16 09:23:57 2009 From: anthony.tolle at gmail.com (Anthony Tolle) Date: Thu, 16 Jul 2009 06:23:57 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <7x3a8xwjq5.fsf@ruckus.brouhaha.com> Message-ID: On Jul 15, 8:32?pm, Paul Rubin wrote: > Among other things, that uses quadratic time! ?Why do you want to keep > popping items from that list instead of iterating through it anyway? > > Anyway, I think you wrote something close to this: > ... Very true! I didn't think about the problems with pop(). I was using it as a shortcut for pulling off the first operand. I forgot that if you start with an initial operand of "False", the result will be the same (0 xor X = X) While I'm not sure how useful it would be, here's a version of the first function that returns one of the operands (ala AND and OR), except in the case where there is an even number of True elements, where it returns False: def xor(*operands): r, rprime = False, False for x in operands: xprime = bool(x) if rprime: if xprime: r, rprime = False, False else: r, rprime = x, xprime return r >>> xor(0, 0) 0 >>> xor(0, 1) 1 >>> xor(1, 0) 1 >>> xor(1, 1) False >>> xor(0, 1, 2) False >>> xor(0, 1, 2, 3) 3 >>> xor(None, []) [] From nick at craig-wood.com Thu Jul 16 09:29:59 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 16 Jul 2009 08:29:59 -0500 Subject: using timers to force an execution time References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: superpollo wrote: > Diez B. Roggisch wrote: > > superpollo wrote: > >>am i wrong? > > > > > > No, you are right, for threads that is. You can try & trick around with the > > trace-functionality of python, and some ctypes-based > > system-thread-module-tricks that are, as mentioned before, black-magic & a > > spinning gatling gun pointed to your very own lower extremities. > > > > OUCH. > > > The only reliable way of terminating an asynchronous computation is to use > > processes, since python2.6 that's rather convenient with the > > multiprocessing-module. However, that imposes some limits to what you can > > do. > > fair enough. Or if you are on unix you can use signals... It is probably just about acceptable to raise an exception in a signal handler like this code does. import signal, os, time class TimeOut(Exception): """Thrown on alarm""" pass def sig_alarm(signum, frame): raise TimeOut() def time_out(t, fn, *args, **kwargs): """Calls fn with the args and kwargs returning its result or raising a TimeOut exception if it doesn't complete within t seconds""" # Turn alarm off and read old value old_alarm = signal.alarm(0) # Install new handler remembering old old_handler = signal.signal(signal.SIGALRM, sig_alarm) # Set the timer going signal.alarm(t) try: rc = fn(*args, **kwargs) finally: # Restore the old handler signal.signal(signal.SIGALRM, old_handler) signal.alarm(0) def test(): for repeat in range(10): print time.time() time.sleep(0.66) if __name__ == "__main__": try: time_out(3, test) except TimeOut: print "timed out" -- Nick Craig-Wood -- http://www.craig-wood.com/nick From inky788 at gmail.com Thu Jul 16 09:32:34 2009 From: inky788 at gmail.com (Inky 788) Date: Thu, 16 Jul 2009 06:32:34 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> Message-ID: <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> On Jul 10, 7:35?pm, Ben Finney wrote: > walterbyrd writes: > > I believe Guido himself has said that all indentions should be four > > spaces - no tabs. > > Yes. That's a ?should? and not a ?must?, even though PEP 8 says it > with a simple imperative:: > > ? ? Use 4 spaces per indentation level. That actually sounds pretty weird. "Don't do {foo}. Really, I mean *really* don't do {foo}. Oh, also, the interpreter allows you to do {foo}. But don't do it! I mean it!". Very ... perlish, if you ask me. I realize that a small portion of the community likes the tabs. They're sold on the tabs. They like the tabs. But tabs are an archaic holdover from an era when typewriters had physical tabstops on them. Actually, after *that* they're a holdover from a program called `make` whose designers unfortunately decided to use tab "characters" to prefix commands. The tab is vestigial. Your text editor can treat 4 consecutive spaces as a unit if you wish. Let tabs go away. Stop using them. If your editor keeps irrationally inserting them, make it stop. From miheavner at gmail.com Thu Jul 16 09:32:47 2009 From: miheavner at gmail.com (mheavner) Date: Thu, 16 Jul 2009 06:32:47 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: On Jul 16, 9:18?am, mheavner wrote: > On Jul 16, 8:39?am, Piet van Oostrum wrote: > > > > > >>>>> mheavner (m) wrote: > > >m> I'm using multiprocessing to spawn several subprocesses, each of which > > >m> uses a very large data structure (making it impractical to pass it via > > >m> pipes / pickling). I need to allocate this structure once when the > > >m> process is created and have it remain in memory for the duration of > > >m> the process. The way the multiprocessing module is set up, only the > > >m> 'run' method runs within the subprocess - so creating a wrapper class > > >m> with a constructor that allocates the structure in __init__ will not > > >m> work, as far as I know, as this will still be within the parent > > >m> process. > > >m> If I were working in C/C++, I would declare the variable "static" > > >m> within the function body - is there any way with the multiprocessing > > >m> module to have persistent data members within subprocesses? > > >m> Any ideas?? > > > Your post is not entirely clear. Is `the process' the same as `the > > subprocess'? > > > Assuming it is, what is the problem? You can create the datastructure > > first thing in the run method can't you? > > > Like this: > > > from multiprocessing import Process > > from time import sleep > > from random import random > > > class MyProcess(Process): > > > ? ? def __init__(self, number): > > ? ? ? ? self.number = number > > ? ? ? ? Process.__init__(self) > > > ? ? def run(self): > > ? ? ? ? print "Process %s started" % self.number > > ? ? ? ? self.data = range(self.number * 100000, (self.number + 1) * 100000) > > ? ? ? ? self.doit() > > > ? ? def doit(self): > > ? ? ? ? for i in range(5): > > ? ? ? ? ? ? sleep(3 * random()) > > ? ? ? ? ? ? self.data[i] += i > > ? ? ? ? ? ? print self.data[i] > > > processes = [] > > for k in range(10): > > ? ? p = MyProcess(k) > > ? ? p.start() > > ? ? processes.append(p) > > > for p in processes: > > ? ? p.join() > > > -- > > Piet van Oostrum > > URL:http://pietvanoostrum.com[PGP8DAE142BE17999C4] > > Private email: p... at vanoostrum.org > > 'The process' refers to the subprocess. I could do as you say, load > the data structure each time, but the problem is that takes a > considerable amount of time compared to the the actual computation > with the data it contains. I'm using these processes within a loop as > follows: > > ? ? ? ? ?# Don't recreate processes or Queues > ? ? ? ? ?pop1 = Queue() > ? ? ? ? ?pop2 = Queue() > ? ? ? ? ?pop_out = Queue() > ? ? ? ? ?p1 = CudaProcess(0, args=(costf,pop1,pop_out)) > ? ? ? ? ?p2 = CudaProcess(1, args=(costf,pop2,pop_out)) > > ? ? ? ? ?# Main loop > ? ? ? ? ?for i in range(maxiter): > ? ? ? ? ? ? ? ? ?print 'ITERATION: '+str(i) > ? ? ? ? ? ? ? ? ?if log != None: > ? ? ? ? ? ? ? ? ? ? ? ? ?l = open(log,'a') > ? ? ? ? ? ? ? ? ?l.write('Iteration: '+str(i)+'\n') > ? ? ? ? ? ? ? ? ?l.close() > > ? ? ? ? ? ? ? ? ?# Split population in two > ? ? ? ? ? ? ? ? ?pop1.putmany(pop[0:len(pop)/2]) > ? ? ? ? ? ? ? ? ?pop2.putmany(pop[len(pop)/2:len(pop)]) > > ? ? ? ? ? ? ? ? ?# Start two processes > ? ? ? ? ? ? ? ? ?if not p1.isAlive(): > ? ? ? ? ? ? ? ? ? ? ? ? ?p1.start() > ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p1.getPid()) > ? ? ? ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ? ? ? ? ? ?p1.run() > ? ? ? ? ? ? ? ? ?if not p2.isAlive(): > ? ? ? ? ? ? ? ? ? ? ? ? ?p2.start() > ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p2.getPid()) > ? ? ? ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ? ? ? ? ? ?p2.run() > ? ? ? ? ? ? ? ? ?. > ? ? ? ? ? ? ? ? ?. > ? ? ? ? ? ? ? ? ?. > > So I'd like to load that data into memory once and keep there as long > as the process is alive (ideally when the subprocess is created, > storing some sort of pointer to it), rather than loading it each time > run is called for a process within the loop. Could be my CudaProcess > class - I'll check out what Diez suggested and post back. Essentially, I'd like to "sneak" that allocation in somewhere after the fork is done (in start()) in the context of the subprocess, holding a pointer to that structure, but before all of the run() calls are done From emile at fenx.com Thu Jul 16 09:34:34 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 06:34:34 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5EEDAE.3040605@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: On 7/16/2009 2:06 AM Jean-Michel Pichavant said... > Ok then, why "or" does not return True, if the first element is > considered True ? Why returning the element itself. Any reason for that > ? Because it's confusing, maybe people used to that logic find it > obvious, but I really don't. For example, I sometimes use it to set defaults: daysInAdvance = int(inputVar) or 25 Emile From wuwei23 at gmail.com Thu Jul 16 09:43:20 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 06:43:20 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> Message-ID: On Jul 16, 10:11?pm, superpollo wrote: > actually i am still using 2.3.4, which means that... > > > screen = turtle.Screen() > > ... is not possible Ah, sorry about that. My belief that turtle was a new module was based on a line from http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle-PyCon2007.pdf Since Python 2.6/3.0, Python has had a new turtle module. At which point I stopped reading and missed the following line: Its development was based entirely on the previous one. In my defence, I _had_ been drinking. Thankfully Peter stepped up with a more appropriate solution, and Michiel pointed out the more suitable API calls over dealing directly with the underlying implementation :) Good work guys! From p.f.moore at gmail.com Thu Jul 16 09:46:13 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Thu, 16 Jul 2009 14:46:13 +0100 Subject: using timers to force an execution time In-Reply-To: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <79990c6b0907160646n58163a3fx1577951dab64215d@mail.gmail.com> 2009/7/16 superpollo : > hello. > > based upon previuos suggestions, i tried to write a program in which i would > like to have a certain code fragment to execute only for a specified amount > of time (say 3 seconds), then bail out. > > but i get the following: > > $ cat tmr004.py > #!/usr/bin/python -u > > import threading , time > > e = threading.Event() > t = threading.Timer(3.0, e.set) > t.start() > print time.asctime(time.localtime(time.time())) > while not e.isSet(): > ? ?for repeat in range(10): > ? ? ? ?print time.time() > ? ? ? ?time.sleep(0.66) > print time.asctime(time.localtime(time.time())) > $ ./tmr004.py > Thu Jul 16 12:31:27 2009 > 1247740287.44 > 1247740288.1 > 1247740288.76 > 1247740289.42 > 1247740290.08 > 1247740290.74 > 1247740291.4 > 1247740292.06 > 1247740292.72 > 1247740293.38 > Thu Jul 16 12:31:34 2009 > $ > > see? the while body ran for about 7 seconds... i bet it has to do with the > fact that the timer does not control inner loops... any suggestion? Clearly, this isn't what you are actually trying to do. For this case, your event is getting set when you expect, but your main thread does not check the event often enough to let it stop in a timely manner. To fix this, remove the inner "for repeat in range(10)" loop. But I assume that your "real" code isn't something that you can fix this easily. If you describe your real requirement, it may be possible to give you a better answer. (But that answer will almost certainly not be a way of interrupting one thread from another - that's not really possible with Python - but rather a way of achieving your goal without *needing* to interrupt one thread from another). Paul. PS If you really must interrupt one thread from another, you can use C code (or ctypes) to call the PyThreadState_SetAsyncExc API. But I'm not going to tell you how, as it's almost certainly *not* what you want to do in practice :-) From user at example.net Thu Jul 16 09:46:41 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 15:46:41 +0200 Subject: using timers to force an execution time In-Reply-To: References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <4a5f2f43$0$18933$4fafbaef@reader2.news.tin.it> Nick Craig-Wood wrote: > superpollo wrote: > ... > Or if you are on unix you can use signals... > > It is probably just about acceptable to raise an exception in a signal > handler like this code does. > > ... neat! From icanbob at gmail.com Thu Jul 16 09:50:52 2009 From: icanbob at gmail.com (bobicanprogram) Date: Thu, 16 Jul 2009 06:50:52 -0700 (PDT) Subject: Passing handlers between bound c++ libs References: <3e7b0717-a665-4f42-b7bd-7a99d59258c4@m11g2000yqh.googlegroups.com> Message-ID: On Jul 14, 7:07 pm, Freyr wrote: > I have a python bound physics library that uses handler to process > events. The passing of handlers between c++ and python causes a huge > overhead slowing down the process. Can I implement a handler in my > custom python bound c++ lib B and pass it to blackbox python bound c++ > lib A so that A would call B directly without passing through the > python layer? Or to phrase the question differently: Can I pass a > handler from B through python to A so that A will invoke B with out > calling into python code? Depending on your exact configuration you may be able to wrap that C++ library using the SIMPL toolkit and get at your data with a message. There is some "hello world" demo code here: http://www.icanprogram.com/06py/main.html bob From user at example.net Thu Jul 16 09:51:59 2009 From: user at example.net (superpollo) Date: Thu, 16 Jul 2009 15:51:59 +0200 Subject: using timers to force an execution time In-Reply-To: References: <4a5f022d$0$18927$4fafbaef@reader2.news.tin.it> <7c8fgbF26vgl2U1@mid.uni-berlin.de> <4a5f0929$0$18927$4fafbaef@reader2.news.tin.it> <7c8futF26vgl2U3@mid.uni-berlin.de> <4a5f0a64$0$18927$4fafbaef@reader2.news.tin.it> Message-ID: <4a5f3080$0$18933$4fafbaef@reader2.news.tin.it> Nick Craig-Wood wrote: > ... > import signal, os, time > ... importing os is useless of course... From jeanmichel at sequans.com Thu Jul 16 09:53:45 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 16 Jul 2009 15:53:45 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: <4A5F30E9.1000404@sequans.com> Emile van Sebille wrote: > On 7/16/2009 2:06 AM Jean-Michel Pichavant said... >> Ok then, why "or" does not return True, if the first element is >> considered True ? Why returning the element itself. Any reason for >> that ? Because it's confusing, maybe people used to that logic find >> it obvious, but I really don't. > > For example, I sometimes use it to set defaults: > > daysInAdvance = int(inputVar) or 25 > > Emile > Sure this looks like an elegant way to set default values and I will use this form , but I'm not sure this justifies by itself the trickery. Python has extended the algebra definition of "or" and "and" top any type, but it is so unintuitive (I'm no LISP programmer). I think than using the short-circuiting mechanism of bool operators along with the python tricks is just error prone and may result in bug difficult to spot, unless you are very aware of all python boolean mechanisms. JM From invalid at invalid Thu Jul 16 10:04:34 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 16 Jul 2009 09:04:34 -0500 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: On 2009-07-16, Emile van Sebille wrote: > On 7/16/2009 2:06 AM Jean-Michel Pichavant said... >> Ok then, why "or" does not return True, if the first element is >> considered True ? Why returning the element itself. Any reason for that >> ? Because it's confusing, maybe people used to that logic find it >> obvious, but I really don't. > > For example, I sometimes use it to set defaults: > > daysInAdvance = int(inputVar) or 25 I don't get it. That doesn't work right when inputVar == "0". -- Grant Edwards grante Yow! What I want to find at out is -- do parrots know visi.com much about Astro-Turf? From invalid at invalid Thu Jul 16 10:05:22 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 16 Jul 2009 09:05:22 -0500 Subject: How to search this newsgroup by a python script. References: <7c897lF26dea4U1@mid.dfncis.de> Message-ID: On 2009-07-16, Chris Rebert wrote: > On Thu, Jul 16, 2009 at 2:12 AM, Helmut > Jarausch wrote: >> Hi, >> >> I haven't found anything with Google's group search, so let me >> ask it (again?). >> >> How can I search this newsgroup from within a Python script. >> (Perhaps by searching Google Groups or Gmane by some Python code.) > > 1. Generate URL of the form: > http://search.gmane.org/?query=foo&group=gmane.comp.python.general > where "foo" is the search terms, with proper URL escaping applied. > 2. Fetch URL using urllib - http://docs.python.org/library/urllib.html > 3. Parse resulting HTML page (e.g. using BeautifulSoup) > 4. Extract desired information from search results using the parse tree. > 5. ??? > 6. Profit! The underpants gnomes would be proud. -- Grant Edwards grante Yow! I'm encased in the at lining of a pure pork visi.com sausage!! From nleioatt at gmail.com Thu Jul 16 10:07:58 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 16 Jul 2009 07:07:58 -0700 (PDT) Subject: This is a mess... Message-ID: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> I've been coding python for about a week now, and i'm trying to make an object oriented version of a program i just wrote. in this post is the original program. the next post will include the new programs and the traceback. i'm sure there are many underlying problems, but have been stuck on this for 2 days now... email me for input files since they are big and i don't see a way to upload a file here(~5 mB) this program works, and inputing the same file twice you should get a "Q" values of ~1 (.99...) ##############CODE##################### #9July09 #Compute the covariance overlap of two results #Read in _U.asc files for both ensembles to find e-vectors #Read in _ .asc files for both ensembles to find sqrt e-values #strip out matrix info #compute corvariance import sys import math from math import sqrt if len(sys.argv) != 3: print " " print "The corrent usage is 'python covariance.py file1 file2'" print "where the _U.asc and _s.asc will be appended when needed" print " " exit(1) ####Input the first prefix1_U.asc file: ####this is the first eigen vector matrix prefix1 = sys.argv[1] + "_U.asc" fileholder = open(prefix1) text = fileholder.readlines() fields = text[1].split() num_rows = int(fields[1]) num_cols = int(fields[2]) U1_matrix = [] for line in text[2: num_rows+2]: fields = line.split() for i in range(len(fields)): fields[i] = float(fields[i]) U1_matrix.append(fields) ####Input the second prefix2_U.asc file: ####this is the 2nd eigen vector matrix prefix2 = sys.argv[2] + "_U.asc" fileholder = open(prefix2) text = fileholder.readlines() fields = text[1].split() num_rows = int(fields[1]) num_cols = int(fields[2]) #print "look here nick:", fields U2_matrix = [] for line in text[2: num_rows+2]: fields = line.split() for i in range(len(fields)): fields[i] = float(fields[i]) U2_matrix.append(fields) ####Then Read in the first eigen values ####1st 2 lines are header and need to be stripped away prefix3 = sys.argv[1] + "_s.asc" fileholder = open(prefix3) text = fileholder.readlines() fields = text[1].split() num_vals1 = int(fields[1]) #add check to see if correct # of values added print "square if", len(U1_matrix), "=", len(U2_matrix), "=", len (U1_matrix[1]), "=", len(U2_matrix[1]) #the list of sqrt e vals sqrt_s1 = [float(line) for line in text[2: num_vals1+2]] s1 = [float(line) * float(line) for line in text[2: num_vals1+2]] ####Finally, read in the 2nd set of eigen vals prefix4 = sys.argv[2] + "_s.asc" fileholder = open(prefix4) text = fileholder.readlines() fields = text[1].split() num_vals2 = int(fields[1]) #add check to see if correct # of values added #the list of sqrt e vals sqrt_s2 = [float(line) for line in text[2: num_vals1+2]] s2 = [float(line) * float(line) for line in text[2: num_vals1+2]] #============================================================= ####double summation (the 2nd term) doublesum = 0.0 for i in range(len(U1_matrix[1])): #print "i = ", i v1 = U1_matrix[i] sum = 0 for j in range(len(U2_matrix)): v2 = U2_matrix[j] dot = 0 for k in range(len(U1_matrix)): dot += v1[k] * v2[k] root = sqrt_s1[i] * sqrt_s2[j] sum += root * dot * dot doublesum += sum print "double sum: ", doublesum ####single summation (the 1st term and denominator) singsum = 0.0 for k in range(len(s1)): singsum += s1[k] + s2[k] print "sing sum:", singsum ####Put it all together Q = 1 - sqrt(abs((singsum - (2.0*doublesum)) / singsum)) print "your Q:" print Q ############################# end of code covariance.py From seldan24 at gmail.com Thu Jul 16 10:12:24 2009 From: seldan24 at gmail.com (seldan24) Date: Thu, 16 Jul 2009 07:12:24 -0700 (PDT) Subject: Python Equivalent for dd & fold References: <4A5E1082.8090901@mrabarnett.plus.com> Message-ID: <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> On Jul 15, 1:48?pm, Emile van Sebille wrote: > On 7/15/2009 10:23 AM MRAB said... > > >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: > >>> seldan24 wrote: > >>>> what can I use as the equivalent for the Unix 'fold' command? > >>> def fold(s,len): > >>> ? ? ?while s: > >>> ? ? ? ? ?print s[:len] > >>> ? ? ? ? ?s=s[len:] > > > > You might still need to tweak the above code as regards how line endings > > are handled. > > You might also want to tweak it if the strings are _really_ long to > simply slice out the substrings as opposed to reassigning the balance to > a newly created s on each iteration. > > Emile Thanks for all of the help. I'm almost there. I have it working now, but the 'fold' piece is very slow. When I use the 'fold' command in shell it is almost instantaneous. I was able to do the EBCDIC->ASCII conversion usng the decode method in the built-in str type. I didn't have to import the codecs module. I just decoded the data to cp037 which works fine. So now, I'm left with a large file, consisting of one extremely long line of ASCII data that needs to be sliced up into 35 character lines. I did the following, which works but takes a very long time: f = open(ascii_file, 'w') while ascii_data: f.write(ascii_data[:len]) ascii_data = ascii_data[len:] f.close() I know that Emile suggested that I can slice out the substrings rather than do the gradual trimming of the string variable as is being done by moving around the length. So, I'm going to give that a try... I'm a bit confused by what that means, am guessing that slice can break up a string based on characters; will research. Thanks for the help thus far. I'll post again when all is working fine. From aahz at pythoncraft.com Thu Jul 16 10:14:40 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 07:14:40 -0700 Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: In article <45228cf4-0b37-4c52-bf6f-d7bd124b0f82 at l32g2000vbp.googlegroups.com>, Tony Lay wrote: > >Traceback (most recent call last): > File "/usr/local/bin/meld", line 35, in > import gettext > File "/usr/local/lib/python2.6/gettext.py", line 49, in > import locale, copy, os, re, struct, sys > File "/usr/local/lib/python2.6/locale.py", line 15, in > import functools > File "/usr/local/lib/python2.6/functools.py", line 10, in > from _functools import partial, reduce >ImportError: No module named _functools Where is _functools.so? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From nleioatt at gmail.com Thu Jul 16 10:15:52 2009 From: nleioatt at gmail.com (Nick) Date: Thu, 16 Jul 2009 07:15:52 -0700 (PDT) Subject: This is a mess... References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: this is the new oop version, its pretty messy currently, and i do understand it is a simple routine, but i'm using it as an exercise to learn oop python... first the (current) traceback: [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test Traceback (most recent call last): File "oop_covariance.py", line 24, in cov = set1.covariance(set2, Eigen_vect.dot) File "/home/nleioatts/python/Eigen.py", line 66, in covariance print self.vectors[i][i] AttributeError: Eigen_vect instance has no attribute '__getitem__' and a quick explaination: the file structures aare a 2d list of lists of numbers corresponding to eginvectors and a list of numbers corresponding to eigenvalues #########################33##Here is the main body #!/usr/bin/env python import sys import Eigen from Eigen import * if len(sys.argv) != 3: print " " print "The corrent usage is 'python covariance.py file1 file2'" print "where the _U.asc and _s.asc will be appended when needed" print " " exit(1) file1 = sys.argv[1] file2 = sys.argv[2] set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc") set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc") cov = set1.covariance(set2, Eigen_vect.dot) print cov ###############and here are the classes: #!/usr/bin/env python import sys import math from math import sqrt class Eigen_vect: def __init__(self, e_val, e_vect): self.e_val = e_val self.e_vect = e_vect def length(self): return len(self.e_vect) def dot(self, other): d = 0.0 if other.length() != self.length(): raise ValueError, "Eigen Vectors not same Length" for k in range(self.length()): # print "HI NICK", self.e_vect[k], other.e_vect[k] d += float(self.e_vect[k]) * float(other.e_vect[k]) return d class Eigen_set: def __init__(self, vec_filename, val_filename): self.vec_filename = vec_filename self.val_filename = val_filename # open two files # loop through them, skipping lines that begin with # # for each row, extract eigen vector and eigen values fileholder = open(self.vec_filename) text = fileholder.readlines() fields = text[2].split() # print "len of fields", len(fields) self.vectors = [] for line in text[2: len(fields)]: fields = line.split() # print "len of fields", len(fields) for i in range(len(fields)): fields[i] = float(fields[i]) e_vect = fields fileholder = open(self.val_filename) text = fileholder.readlines() e_val = [float(line) for line in text[2: self.length()]] self.vectors.append(Eigen_vect(e_val, e_vect)) # print "this is self.vectors" # print self.vectors(e_val) def length(self): return len(self.vectors) def covariance(self, other, dot): newdot = 0.0 # do a length check to make sure we're consistent if other.length() != self.length(): raise ValueError, "Eigen Vectors not same Length" #double loop over all my vectors and all of other's vectors doublesum = 0.0 for i in range(self.length()): sum = 0.0 v1 = self.vectors[i] for j in range(self.length()): newdot += v1.dot(self.vectors[j]) # root = self.e_val[i] * other.e_val[j] print self.vectors[i] print self.vectors[j] print self.vectors[i][i] #####################<<------------------------This is line 66, I'm trying to figure out how to call "e_val" from the Eigen_set class root = self.vectors[i][i] * other.vectors[i][j] sum += newdot * newdot * root doublesum += sum ######### singsum = 0.0 for k in range(self.length()): singsum += self.e_val[k] * self.e_val[k] + other.e_val[k] * other.e_val[k] Q = 1 - sqrt(abs((singsum - (2.0*doublesum)) / singsum)) print "your Q:" print Q and any additional help is great. thanks in advance, like the title says this is really a mess.... From python.list at tim.thechases.com Thu Jul 16 10:18:47 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 16 Jul 2009 09:18:47 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: <4A5F36C7.5010307@tim.thechases.com> > I realize that a small portion of the community likes the tabs. > They're sold on the tabs. They like the tabs. But tabs are an archaic > holdover from an era when typewriters had physical tabstops on them. However, they are a single logical level of indentation -- I come down fairly solidly on the "tabs" side of the "tabs vs. spaces" argument. I can set my editor (vim in this case) to show tabs as as many spaces as I want. I usually have this set to 4, but sometimes 1 or 2. In Vim, using tabs I can just switch up my tab-stop setting (":set ts=2") and the presentation of my code reindents the way I expect. If I wanted to switch from 4-real-spaces to 2-real-spaces, I'd have to concoct a perlish regexp to handle the munging. The same "separation of content and presentation" that is all the rage with N-tier programmers and web developers, my content is "logical levels of indentation indicated by a tab character in the source-code" and my presentation is "N spaces as presented by my editor". Yes, the dictatorial "a tab always equals 8 spaces" is a vestigial holdover for which I hold no love. I'll code on other people's projects with spaces if that's the project convention (as Vim lets me switch around fairly easily). But as for my own code, it's tabs all the way. -tkc From sjmachin at lexicon.net Thu Jul 16 10:20:25 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 16 Jul 2009 07:20:25 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <9ad398aa-c68e-48a0-951c-fb10a0e27da1@l35g2000pra.googlegroups.com> <51203b20-f38e-4629-94b8-2427cd453652@f30g2000vbf.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> Message-ID: On Jul 16, 11:13?pm, hartley wrote: > /* the first telling */ > (...) > /* the second telling */ > (...) > /* the third telling */ > > - > If you had loosened up on the sarcasm That wasn't sarcasm, it was an attempt at humourous watering-down the expression of exasperation at continued ignoring of advice and wonder that I was continuing to bother ... > HTH, It didn't. From aahz at pythoncraft.com Thu Jul 16 10:22:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 07:22:16 -0700 Subject: Memory leak involving traceback objects References: Message-ID: In article , Rotem wrote: > >I'm debugging a nasty memory leak in a framework written in Python >(v2.6.2). >After much digging around I found that the entire object group that is >leaking is held by a frame object which is subsequently held by a >traceback object. > >Traversing the get_referrers() of each traceback frame leads >eventually to a root traceback frame which has no referrers >(gc.get_referrers returns an empty list). > >However, this traceback object seems not to be picked by the garbage >collector, and is still there even after many iterations and calls to >gc.collect(). The code location to which the traceback frame points >doesn't do anything special - it just catches an exception, without >saving the exception itself and/or traceback anywhere. What *does* it do? Does it re-raise? This sounds like you're still in block scope of an exception. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From ifl2009 at shu.edu Thu Jul 16 10:28:44 2009 From: ifl2009 at shu.edu (IFL 2009) Date: Thu, 16 Jul 2009 10:28:44 -0400 Subject: IFL 2009: Third Call for Papers Message-ID: An HTML attachment was scrubbed... URL: From james at agentultra.com Thu Jul 16 10:49:54 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 16 Jul 2009 10:49:54 -0400 Subject: interactive fiction in Python? References: <5c97224b-99d4-469b-90cb-1c04b1e1e9a0@m11g2000yqh.googlegroups.com> Message-ID: <853a8wlm25.fsf@agentultra.com> George Oliver writes: > hi, I'm just curious who might be working on interactive fiction > modules in the style of Inform or TADS for Python. I've seen a few > threads on this list [1] (among many that mention IF tangentially), > and there are old projects like PUB and PAWS. There are some newer > potential projects such as Curveship as well. In any case I'd like to > see what's out there. I'd be interested in checking out such a project, but I don't really see the point in starting one. There are already several great IF tools out there that are already freely available! The only reason I would see to start one is either for pure intellectual curiosity or because you have some feature ideas that no one else has implemented (and you'd rather cash in on the potential glory than submit a patch!). Best of luck finding what you're looking for... but maybe you should start writing one yourself and post here about it! :) Cheers > > > thanks, George > > > [1]: > > http://bit.ly/Python_Text_Adventure_Authoring_System > > http://bit.ly/simple_interactive_fiction_engine > > http://bit.ly/adventure_engines_in_Python From james at agentultra.com Thu Jul 16 10:51:47 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 16 Jul 2009 10:51:47 -0400 Subject: Ann: Google releases Python-based open-source NX server References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> Message-ID: <85y6qok7ek.fsf@agentultra.com> jkn writes: > Google quietly releases open-source NX server ...written in Python, > apparently > > Google_quietly_releases_open_source_NX_server?taxonomyId=88> > > Neatx can be downloaded from Google's code repository: > > > > Regards > J^n It's pretty cool, but not PEP8! Probably because they just bought the source off of another smaller proprietary project. Makes me sad seeing Google, proud supporter of all things Python, release non-PEP8 code. Either way, I did check out the project and it looks pretty exciting. Hopefully I'll get some time to contribute a few patches. From gabriel.rossetti at arimaz.com Thu Jul 16 11:12:23 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Thu, 16 Jul 2009 17:12:23 +0200 Subject: no return value for threading.Condition.wait(timeout)? Message-ID: <4A5F4357.5060402@arimaz.com> Hello everyone, I am using threading.Condition.wait(timeout) and was surprised to see that there is no return value nor an exception when wait() is used w/ a timeout. How am I supposed to know if it was notified or if it timed out? cheers, Gabriel From maxerickson at gmail.com Thu Jul 16 11:31:36 2009 From: maxerickson at gmail.com (Max Erickson) Date: Thu, 16 Jul 2009 15:31:36 +0000 (UTC) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <24514309.post@talk.nabble.com> <24514367.post@talk.nabble.com> Message-ID: akhil1988 wrote: > > akhil1988 wrote: >> >> I have switched to python 3.1 , but now I am getting some syntax >> errors in the code: >> >> File "./customWikiExtractor.py", line 81 >> __char_entities = {' ' :u'\u00A0', '¡' >> :u'\u00A1', >> '¢' :u'\u00A2', >> ^ You may want to try 2.6. Python 3.1 is not syntax compatible with 2.5 (so the u'' stuff won't work in 3.1): http://docs.python.org/dev/py3k/whatsnew/3.0.html#removed-syntax max From motoom at xs4all.nl Thu Jul 16 11:34:25 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Thu, 16 Jul 2009 17:34:25 +0200 Subject: Python Equivalent for dd & fold In-Reply-To: <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <4A5F4881.9090501@xs4all.nl> seldan24 wrote: > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. An excellent idea. def fold(s,chunklength): offset=0 while offset <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <24514309.post@talk.nabble.com> Message-ID: On Jul 16, 9:04?pm, akhil1988 wrote: > Please click reply on the post and then read this reply in the editor. > Actually, some sequences have been replaced to their graphical form when > this post is published. So the python code is being displayed, what actually > it is not. What editor? I guess you mean that somebody's browser may be interpreting the & blah ; thingies .... but this is not relevant; your syntax error problem is that 2.x unicode literals have a u in front but 3.x str literals don't IOW you need to lose the first u in u'\u00A0' From Caseyweb at gmail.com Thu Jul 16 11:44:18 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Thu, 16 Jul 2009 08:44:18 -0700 (PDT) Subject: Python Equivalent for dd & fold References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <26e55904-58b6-482f-a2b6-c6edfb5bf32e@y4g2000prf.googlegroups.com> On Jul 16, 10:12?am, seldan24 wrote: > On Jul 15, 1:48?pm, Emile van Sebille wrote: > > > > > On 7/15/2009 10:23 AM MRAB said... > > > >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: > > >>> seldan24 wrote: > > >>>> what can I use as the equivalent for the Unix 'fold' command? > > >>> def fold(s,len): > > >>> ? ? ?while s: > > >>> ? ? ? ? ?print s[:len] > > >>> ? ? ? ? ?s=s[len:] > > > > > > You might still need to tweak the above code as regards how line endings > > > are handled. > > > You might also want to tweak it if the strings are _really_ long to > > simply slice out the substrings as opposed to reassigning the balance to > > a newly created s on each iteration. > > > Emile > > Thanks for all of the help. ?I'm almost there. ?I have it working now, > but the 'fold' piece is very slow. ?When I use the 'fold' command in > shell it is almost instantaneous. ?I was able to do the EBCDIC->ASCII > conversion usng the decode method in the built-in str type. ?I didn't > have to import the codecs module. ?I just decoded the data to cp037 > which works fine. > > So now, I'm left with a large file, consisting of one extremely long > line of ASCII data that needs to be sliced up into 35 character > lines. ?I did the following, which works but takes a very long time: > > f = open(ascii_file, 'w') > while ascii_data: > ? ? f.write(ascii_data[:len]) > ? ? ascii_data = ascii_data[len:] > f.close() > > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. ?So, I'm going to give that a try... I'm > a bit confused by what that means, am guessing that slice can break up > a string based on characters; will research. ?Thanks for the help thus > far. ?I'll post again when all is working fine. The problem is that it creates a new string every time you iterate through the "ascii_data = ascii_data[len:]". I believe Emile was suggesting that you just keep moving the starting index through the same string, something like (warning - untested code!): >>> i = 0 >>> str_len = len(ascii_data) >>> while i < str_len: >>> j = min(i + length, str_len) >>> print ascii_data[i:j] >>> i = j From pdpinheiro at gmail.com Thu Jul 16 12:02:25 2009 From: pdpinheiro at gmail.com (pdpi) Date: Thu, 16 Jul 2009 09:02:25 -0700 (PDT) Subject: Python Equivalent for dd & fold References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <4592d512-592b-4e58-829e-e643be34bcec@r33g2000yqn.googlegroups.com> On Jul 16, 3:12?pm, seldan24 wrote: > On Jul 15, 1:48?pm, Emile van Sebille wrote: > > > > > > > On 7/15/2009 10:23 AM MRAB said... > > > >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: > > >>> seldan24 wrote: > > >>>> what can I use as the equivalent for the Unix 'fold' command? > > >>> def fold(s,len): > > >>> ? ? ?while s: > > >>> ? ? ? ? ?print s[:len] > > >>> ? ? ? ? ?s=s[len:] > > > > > > You might still need to tweak the above code as regards how line endings > > > are handled. > > > You might also want to tweak it if the strings are _really_ long to > > simply slice out the substrings as opposed to reassigning the balance to > > a newly created s on each iteration. > > > Emile > > Thanks for all of the help. ?I'm almost there. ?I have it working now, > but the 'fold' piece is very slow. ?When I use the 'fold' command in > shell it is almost instantaneous. ?I was able to do the EBCDIC->ASCII > conversion usng the decode method in the built-in str type. ?I didn't > have to import the codecs module. ?I just decoded the data to cp037 > which works fine. > > So now, I'm left with a large file, consisting of one extremely long > line of ASCII data that needs to be sliced up into 35 character > lines. ?I did the following, which works but takes a very long time: > > f = open(ascii_file, 'w') > while ascii_data: > ? ? f.write(ascii_data[:len]) > ? ? ascii_data = ascii_data[len:] > f.close() > > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. ?So, I'm going to give that a try... I'm > a bit confused by what that means, am guessing that slice can break up > a string based on characters; will research. ?Thanks for the help thus > far. ?I'll post again when all is working fine. Assuming your rather large text file is 1 meg long, you have 1 million characters in there. 1000000/35 = ~29k lines. The size remaining string decreases linearly, so the average size is (1000000 + 0) / 2 or 500k. All said and done, you're allocating and copying a 500K string -- not once, but 29 thousand times. That's where your slowdown resides. From tn.pablo at gmail.com Thu Jul 16 12:02:57 2009 From: tn.pablo at gmail.com (Pablo Torres N.) Date: Thu, 16 Jul 2009 11:02:57 -0500 Subject: How to check if any item from a list of strings is in a big string? In-Reply-To: References: Message-ID: On Thu, Jul 9, 2009 at 22:07, Steven D'Aprano wrote: > On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: > >> def list_items_in_string(list_items, string): >> ? ? for item in list_items: >> ? ? ? ? if item in string: >> ? ? ? ? ? ? return True >> ? ? return False > ... >> Any ideas how to make that function look nicer? :) > > Change the names. Reverse the order of the arguments. Add a docstring. > Why reverse the order of the arguments? Is there a design principle there? I always make a mess out of the order of my arguments... -- Pablo Torres N. From degibb at gmail.com Thu Jul 16 12:15:46 2009 From: degibb at gmail.com (David Gibb) Date: Thu, 16 Jul 2009 12:15:46 -0400 Subject: list of all possible values In-Reply-To: <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> Message-ID: > Certainly possible with list comprehensions. > >>>> a = "abc" >>>> [(x, y) for x in a for y in a] > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), > ('c', 'a'), ('c', 'b'), ('c', 'c')] > > But I like bearophile's version better. > Andreas, Thanks, but I think you were missing my point. I should have explained better. The advantage that bearophile's version is generic with respect to the number of elements in each combination. To go from 2 element pairs (e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e')) requires only a change in a parameter passed to itertools. I don't see how you would do that with list comprehensions. You're example works nicely with 2 element pairs, but it seems to me like you'd need to recode it if you wanted to change it to 5 element pairs. Am I wrong about that? Can you think of a way to write a function that, using list comprehensions, takes a list of values and the size of each combination, and returns the len(list)**(combination size) possible combinations using those values? Thanks again, David From rylesny at gmail.com Thu Jul 16 12:23:25 2009 From: rylesny at gmail.com (ryles) Date: Thu, 16 Jul 2009 09:23:25 -0700 (PDT) Subject: Python Equivalent for dd & fold References: Message-ID: On Jul 15, 1:14?pm, seldan24 wrote: > On Jul 15, 12:47?pm, Michiel Overtoom wrote: > > > > > seldan24 wrote: > > > what can I use as the equivalent for the Unix 'fold' command? > > > def fold(s,len): > > ? ? ?while s: > > ? ? ? ? ?print s[:len] > > ? ? ? ? ?s=s[len:] > > > s="A very long string indeed. Really that long? Indeed." > > fold(s,10) > > > Output: > > > A very lon > > g string i > > ndeed. Rea > > lly that l > > ong? Indee > > d. > > > Greetings, > > > -- > > "The ability of the OSS process to collect and harness > > the collective IQ of thousands of individuals across > > the Internet is simply amazing." - Vinod Valloppillilhttp://www.catb.org/~esr/halloween/halloween4.html > > Wow, I feel like a dork. ?I should have done more research prior to > posting. ?Anyway, thanks for the advice. ?The trouble with Python is > that things make 'too much' sense. ?Loving this language. You might also find the textwrap module useful: http://docs.python.org/library/textwrap.html From emile at fenx.com Thu Jul 16 12:23:43 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 09:23:43 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: On 7/16/2009 7:04 AM Unknown said... > On 2009-07-16, Emile van Sebille wrote: >> daysInAdvance = int(inputVar) or 25 > > I don't get it. That doesn't work right when inputVar == "0". > Aah, but you didn't get to define right. :) For that particular example 0 is not a valid response. Emile From jeanmichel at sequans.com Thu Jul 16 12:37:45 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 16 Jul 2009 18:37:45 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: <4A5F5759.4010600@sequans.com> Emile van Sebille wrote: > On 7/16/2009 7:04 AM Unknown said... >> On 2009-07-16, Emile van Sebille wrote: >>> daysInAdvance = int(inputVar) or 25 >> >> I don't get it. That doesn't work right when inputVar == "0". >> > Aah, but you didn't get to define right. :) For that particular > example 0 is not a valid response. > > Emile > When I was talking about such error prone form of boolean operations, I didn't expect to be right so quickly :p Steven explained the truth notion with the Something/Nothing. "0" is Something, 0 would be Nothing. I'm not sure it makes sens anyway. I mean, I could easily argue that the number 0 is something. In the end I wonder if I shouldn't just acknowledge the python mechanism without trying to find any intuitive/natural/obvious logic in it, knowing that sometimes the Truth lies far away from the Evidence. JM From andreas.tawn at ubisoft.com Thu Jul 16 12:49:11 2009 From: andreas.tawn at ubisoft.com (Andreas Tawn) Date: Thu, 16 Jul 2009 18:49:11 +0200 Subject: list of all possible values In-Reply-To: References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> Message-ID: <8AEDA5E3386EA742B8C24C95FF0C7580079FCD7C@PDC-MAIL3.ubisoft.org> > > Certainly possible with list comprehensions. > > > >>>> a = "abc" > >>>> [(x, y) for x in a for y in a] > > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), > > ('c', 'a'), ('c', 'b'), ('c', 'c')] > > > > But I like bearophile's version better. > > > > Andreas, > > Thanks, but I think you were missing my point. I should have explained better. > > The advantage that bearophile's version is generic with respect to the > number of elements in each combination. To go from 2 element pairs > (e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e')) > requires only a change in a parameter passed to itertools. > > I don't see how you would do that with list comprehensions. You're > example works nicely with 2 element pairs, but it seems to me like > you'd need to recode it if you wanted to change it to 5 element pairs. > > Am I wrong about that? Can you think of a way to write a function > that, using list comprehensions, takes a list of values and the size > of each combination, and returns the len(list)**(combination size) > possible combinations using those values? > > Thanks again, > David David, I think my post got caught in the nebulous email eddys and seems to have taken 16 hours to arrive on the list. It was meant to be a reply to your first post, not your second. Having said that, I think I missed the point of that post too ;o) Maybe someone smarter than me can come up with a way to dynamically nest the fors in a list comprehension, but I certainly can't do it. Sorry for the confusion. Cheers, Drea From python at mrabarnett.plus.com Thu Jul 16 13:06:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 16 Jul 2009 18:06:09 +0100 Subject: Python Equivalent for dd & fold In-Reply-To: <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> Message-ID: <4A5F5E01.1060505@mrabarnett.plus.com> seldan24 wrote: > On Jul 15, 1:48 pm, Emile van Sebille wrote: >> On 7/15/2009 10:23 AM MRAB said... >> >>>> On Jul 15, 12:47 pm, Michiel Overtoom wrote: >>>>> seldan24 wrote: >>>>>> what can I use as the equivalent for the Unix 'fold' command? >>>>> def fold(s,len): >>>>> while s: >>>>> print s[:len] >>>>> s=s[len:] >> >>> You might still need to tweak the above code as regards how line endings >>> are handled. >> You might also want to tweak it if the strings are _really_ long to >> simply slice out the substrings as opposed to reassigning the balance to >> a newly created s on each iteration. >> >> Emile > > Thanks for all of the help. I'm almost there. I have it working now, > but the 'fold' piece is very slow. When I use the 'fold' command in > shell it is almost instantaneous. I was able to do the EBCDIC->ASCII > conversion usng the decode method in the built-in str type. I didn't > have to import the codecs module. I just decoded the data to cp037 > which works fine. > > So now, I'm left with a large file, consisting of one extremely long > line of ASCII data that needs to be sliced up into 35 character > lines. I did the following, which works but takes a very long time: > > f = open(ascii_file, 'w') > while ascii_data: > f.write(ascii_data[:len]) > ascii_data = ascii_data[len:] > f.close() > The 'write' method doesn't append any line ending, so that code gives the same output as f.write(ascii_data). > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. So, I'm going to give that a try... I'm > a bit confused by what that means, am guessing that slice can break up > a string based on characters; will research. Thanks for the help thus > far. I'll post again when all is working fine. From python at mrabarnett.plus.com Thu Jul 16 13:07:54 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 16 Jul 2009 18:07:54 +0100 Subject: Python Equivalent for dd & fold In-Reply-To: <4A5F4881.9090501@xs4all.nl> References: <4A5E1082.8090901@mrabarnett.plus.com> <27f11d7d-2724-4108-b005-04b2fc8cf030@f10g2000vbf.googlegroups.com> <4A5F4881.9090501@xs4all.nl> Message-ID: <4A5F5E6A.60104@mrabarnett.plus.com> Michiel Overtoom wrote: > seldan24 wrote: > >> I know that Emile suggested that I can slice out the substrings rather >> than do the gradual trimming of the string variable as is being done >> by moving around the length. > > An excellent idea. > > def fold(s,chunklength): > offset=0 > while offset print s[offset:offset+chunklength] > offset+=chunklength > More Pythonic: for offset in range(0, len(s), chunklength): print s[offset : offset + chunklength] > s="A very long string indeed. Really that long? Indeed." > fold(s,10) > From piet at cs.uu.nl Thu Jul 16 13:18:04 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 19:18:04 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: >>>>> mheavner (m) wrote: >m> 'The process' refers to the subprocess. I could do as you say, load >m> the data structure each time, but the problem is that takes a >m> considerable amount of time compared to the the actual computation >m> with the data it contains. I'm using these processes within a loop as >m> follows: >m> # Don't recreate processes or Queues >m> pop1 = Queue() >m> pop2 = Queue() >m> pop_out = Queue() >m> p1 = CudaProcess(0, args=(costf,pop1,pop_out)) >m> p2 = CudaProcess(1, args=(costf,pop2,pop_out)) >m> # Main loop >m> for i in range(maxiter): >m> print 'ITERATION: '+str(i) >m> if log != None: >m> l = open(log,'a') >m> l.write('Iteration: '+str(i)+'\n') >m> l.close() >m> # Split population in two >m> pop1.putmany(pop[0:len(pop)/2]) >m> pop2.putmany(pop[len(pop)/2:len(pop)]) >m> # Start two processes >m> if not p1.isAlive(): >m> p1.start() >m> print 'started %s'%str(p1.getPid()) >m> else: >m> p1.run() That won't work. p1.run() will execute the run method in the Master process, not in the subprocess. And if it would your could would have a race condition: between the p1.isAlive() (which must be is_alive btw), and the p1.run() the process can have stopped. The proper way to do is to put the work in a Queue and let the processes get work out of the Queue. The datastructure will remain in the process then. >m> if not p2.isAlive(): >m> p2.start() >m> print 'started %s'%str(p2.getPid()) >m> else: >m> p2.run() >m> . >m> . >m> . >m> So I'd like to load that data into memory once and keep there as long >m> as the process is alive (ideally when the subprocess is created, >m> storing some sort of pointer to it), rather than loading it each time >m> run is called for a process within the loop. Could be my CudaProcess >m> class - I'll check out what Diez suggested and post back. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From miheavner at gmail.com Thu Jul 16 13:37:45 2009 From: miheavner at gmail.com (mheavner) Date: Thu, 16 Jul 2009 10:37:45 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: <2b61ef2d-4bf6-47ba-824d-6b490a342edb@n11g2000yqb.googlegroups.com> I realize that the Queue would be the best way of doing this, however that involves transferring the huge amount of data for each call - my hope was to transfer it once and have it remain in memory for the subprocess across run() calls. On Jul 16, 1:18?pm, Piet van Oostrum wrote: > >>>>> mheavner (m) wrote: > >m> 'The process' refers to the subprocess. I could do as you say, load > >m> the data structure each time, but the problem is that takes a > >m> considerable amount of time compared to the the actual computation > >m> with the data it contains. I'm using these processes within a loop as > >m> follows: > >m> ? ? ? ? ?# Don't recreate processes or Queues > >m> ? ? ? ? ?pop1 = Queue() > >m> ? ? ? ? ?pop2 = Queue() > >m> ? ? ? ? ?pop_out = Queue() > >m> ? ? ? ? ?p1 = CudaProcess(0, args=(costf,pop1,pop_out)) > >m> ? ? ? ? ?p2 = CudaProcess(1, args=(costf,pop2,pop_out)) > >m> ? ? ? ? ?# Main loop > >m> ? ? ? ? ?for i in range(maxiter): > >m> ? ? ? ? ? ? ? ? ?print 'ITERATION: '+str(i) > >m> ? ? ? ? ? ? ? ? ?if log != None: > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?l = open(log,'a') > >m> ? ? ? ? ? ? ? ? ?l.write('Iteration: '+str(i)+'\n') > >m> ? ? ? ? ? ? ? ? ?l.close() > >m> ? ? ? ? ? ? ? ? ?# Split population in two > >m> ? ? ? ? ? ? ? ? ?pop1.putmany(pop[0:len(pop)/2]) > >m> ? ? ? ? ? ? ? ? ?pop2.putmany(pop[len(pop)/2:len(pop)]) > >m> ? ? ? ? ? ? ? ? ?# Start two processes > >m> ? ? ? ? ? ? ? ? ?if not p1.isAlive(): > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p1.start() > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p1.getPid()) > >m> ? ? ? ? ? ? ? ? ?else: > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p1.run() > > That won't work. p1.run() will execute the run method in the Master > process, not in the subprocess. And if it would your could would have a > race condition: between the p1.isAlive() (which must be is_alive btw), and > the p1.run() the process can have stopped. > > The proper way to do is to put the work in a Queue and let the processes > get work out of the Queue. The datastructure will remain in the process > then. > > >m> ? ? ? ? ? ? ? ? ?if not p2.isAlive(): > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p2.start() > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?print 'started %s'%str(p2.getPid()) > >m> ? ? ? ? ? ? ? ? ?else: > >m> ? ? ? ? ? ? ? ? ? ? ? ? ?p2.run() > >m> ? ? ? ? ? ? ? ? ?. > >m> ? ? ? ? ? ? ? ? ?. > >m> ? ? ? ? ? ? ? ? ?. > >m> So I'd like to load that data into memory once and keep there as long > >m> as the process is alive (ideally when the subprocess is created, > >m> storing some sort of pointer to it), rather than loading it each time > >m> run is called for a process within the loop. Could be my CudaProcess > >m> class - I'll check out what Diez suggested and post back. > > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org From inky788 at gmail.com Thu Jul 16 13:47:07 2009 From: inky788 at gmail.com (Inky 788) Date: Thu, 16 Jul 2009 10:47:07 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: <68dbe005-64bc-42f8-99b7-e73c41d0dbd4@r33g2000yqn.googlegroups.com> On Jul 16, 10:18?am, Tim Chase wrote: > > I realize that a small portion of the community likes the tabs. > > They're sold on the tabs. They like the tabs. But tabs are an archaic > > holdover from an era when typewriters had physical tabstops on them. > > However, they are a single logical level of indentation -- I come > down fairly solidly on the "tabs" side of the "tabs vs. spaces" > argument. My bet is that the problem is this: some people like to format their code in ways that don't work well when you're using tabs. For example, they might want to call a function like this (note spaces): some_func(foo=1, bar=2, baz=3) instead of: some_func( foo=1, bar=2, baz=3) The first one requires 10 spaces in front of bar and baz. If you're using tabs, that means one or two tabs plus some space characters. So, people who do that would probably never use tabs. The 2nd example is fine for tabs or spaces. I'm sure there are a bunch of similar examples for things besides function calls. Don't you ever find cases where you'd like to add in an extra space or two to make things line up nicely? >?I can set my editor (vim in this case) to show tabs as > as many spaces as I want. ?I usually have this set to 4, but > sometimes 1 or 2. Just curious: why would you want to do that? In my experience, once my eyes got used to 4-space indents, everything else looks either too little or too much. :) From jkn_gg at nicorp.f9.co.uk Thu Jul 16 13:59:12 2009 From: jkn_gg at nicorp.f9.co.uk (jkn) Date: Thu, 16 Jul 2009 10:59:12 -0700 (PDT) Subject: Ann: Google releases Python-based open-source NX server References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> Message-ID: <4086976f-11da-4da2-869a-7720510f2bae@m11g2000yqh.googlegroups.com> On Jul 16, 3:51?pm, J Kenneth King wrote: > It's pretty cool, but not PEP8! Probably because they just bought the > source off of another smaller proprietary project. ?Makes me sad seeing > Google, proud supporter of all things Python, release non-PEP8 code. Personally, I don't follow PEP8 either. Note: PEP-8 gives 'coding conventions for the Python code **comprising the standard library in the main Python distribution**' (my emphasis). My coding conventions are similar to, but not exactly the same as, PEP-8. It would be ridiculous IMO (and PEP-8 acknowledges this) for such a permissive language as Python to put a 'coding style' straitjacket around itself. YMMV, of course. J^n From emile at fenx.com Thu Jul 16 14:27:55 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 11:27:55 -0700 Subject: Ann: Google releases Python-based open-source NX server In-Reply-To: <4086976f-11da-4da2-869a-7720510f2bae@m11g2000yqh.googlegroups.com> References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> <4086976f-11da-4da2-869a-7720510f2bae@m11g2000yqh.googlegroups.com> Message-ID: On 7/16/2009 10:59 AM jkn said... > Personally, I don't follow PEP8 either. Note: PEP-8 gives 'coding > conventions for the Python code **comprising the standard library in > the main Python distribution**' (my emphasis). My coding conventions > are similar to, but not exactly the same as, PEP-8. Mine too, and my objection to PEP-8 is that the coding style doesn't allow for copy-n-paste into the interpreter, and I like to be able to test that way. Perhaps if I configured my editor not to strip trailing whitespace from lines it would work, but then I don't like trailing whitespace. Of course, I _like_ leading whitespace. :) Emile From robert.kern at gmail.com Thu Jul 16 14:29:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 16 Jul 2009 13:29:31 -0500 Subject: Ann: Google releases Python-based open-source NX server In-Reply-To: <85y6qok7ek.fsf@agentultra.com> References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> Message-ID: On 2009-07-16 09:51, J Kenneth King wrote: > jkn writes: > >> Google quietly releases open-source NX server ...written in Python, >> apparently >> >> > Google_quietly_releases_open_source_NX_server?taxonomyId=88> >> >> Neatx can be downloaded from Google's code repository: >> >> >> >> Regards >> J^n > > It's pretty cool, but not PEP8! Probably because they just bought the > source off of another smaller proprietary project. No, it' just that Google does not use PEP8 for its internal style standard. -- 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 python.list at tim.thechases.com Thu Jul 16 14:40:09 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 16 Jul 2009 13:40:09 -0500 Subject: Why not enforce four space indentations in version 3.x? In-Reply-To: <68dbe005-64bc-42f8-99b7-e73c41d0dbd4@r33g2000yqn.googlegroups.com> References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> <68dbe005-64bc-42f8-99b7-e73c41d0dbd4@r33g2000yqn.googlegroups.com> Message-ID: <4A5F7409.8000502@tim.thechases.com> >> However, they are a single logical level of indentation -- I come >> down fairly solidly on the "tabs" side of the "tabs vs. spaces" >> argument. > > My bet is that the problem is this: some people like to format their > code in ways that don't work well when you're using tabs. For example, > they might want to call a function like this (note spaces): > > some_func(foo=1, > bar=2, > baz=3) > > instead of: > > some_func( > foo=1, > bar=2, > baz=3) For continued indenting statements such as this, I tend to use the coding convention used at my first job out of college (Computer Sciences Corp...for better or worse) which just indents two levels: def some_func(foo=1, bar=2, baz=3): do_something(foo) do_other_stuff(bar) > examples for things besides function calls. Don't you ever find cases > where you'd like to add in an extra space or two to make things line > up nicely? I have occasionally (okay, "very rarely") use the "mixed tab+space" method of indentation for continued lines if I want them lined up nicely: if (foo == bar and baz > frob and fred != barney): do_something() do_more() This scheme insures that the visual alignment for the continued-line matches up, even if the tab-stop is changed. The positioning of the indented block (the do_* bit) floats inconveniently with relation to the continued text, with pessimal cases being indistinguishable from the continued lines (which is why I generally opt not to use this unless it has great benefits in code clarity). By regularly indenting continued lines for containing blocks (if, while, etc) by two tabs, the continuation stands out from the contained code regardless of my tab stops. >> I can set my editor (vim in this case) to show tabs as >> as many spaces as I want. I usually have this set to 4, but >> sometimes 1 or 2. > > Just curious: why would you want to do that? In my experience, once my > eyes got used to 4-space indents, everything else looks either too > little or too much. :) It totally depends on the project -- I like the condensed nature of 2sp/tab for code sharing on mailing lists (and tend to copy out of vim with tabs expanded to 2 spaces for pasting into emails) and for my own visual preference. If it's code that I'd expect anybody else to view, I tend to use 4sp/tab to keep my lines below 80 chars per line with the tabs taken into consideration. I guess I change up my indent enough that sometimes 2 seems just right or too small, and sometimes 4 seems just right or too large. -tkc From pavlovevidence at gmail.com Thu Jul 16 14:45:38 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 16 Jul 2009 11:45:38 -0700 (PDT) Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: On Jul 16, 6:32?am, Inky 788 wrote: > On Jul 10, 7:35?pm, Ben Finney wrote: > > > walterbyrd writes: > > > I believe Guido himself has said that all indentions should be four > > > spaces - no tabs. > > > Yes. That's a ?should? and not a ?must?, even though PEP 8 says it > > with a simple imperative:: > > > ? ? Use 4 spaces per indentation level. > > That actually sounds pretty weird. "Don't do {foo}. Really, I mean > *really* don't do {foo}. Oh, also, the interpreter allows you to do > {foo}. But don't do it! I mean it!". > > Very ... perlish, if you ask me. Not Perlish at all. (Perl would never want you not to use something.) Carl Banks From tjreedy at udel.edu Thu Jul 16 14:50:41 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 14:50:41 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: <9fc93aa9-13dd-4e9a-9e93-820c65cdb592@o15g2000yqm.googlegroups.com> Message-ID: Mark wrote: > On 16 July, 10:21, Piet van Oostrum wrote: >> But why should the order be as if the OrderedDict was a list of tuples. >> A dict can be considered as a mapping and then you might want to treat >> either the key or the value as contravariant (the key I guess). So there >> is ambiguity. Why would the view as a list of tuples for the ordering be >> the `natural' view? >> >> Maybe you may expect some kind of monotonicity such that d1> d1[x]> 2:5}. So maybe there is only a partial ordering? > > OK, that seems to me to be a convincing argument against supporting > ordering. To put the above in a slightly different way. OrderedDicts are a recently added niche class that Raymond added to what is mostly his collections module because there are enough few use cases. There was pydev discussion which included the idea, I believe, that they should fundamentally be dicts, not lists. Regardless, the justifying use cases did not include a need to compare OrderedDicts. The small fraction of the few use cases for OrderedDicts that do require comparision can be met by extracting the needed sequences and comparing *them* in the appropriate manner. The 'appropriate manner' is not likely to always be the same. This point may have come up in the discussion, but I would let you check for sure if curious. 'Consistency' is a Python design principle, but it is balanced with others, so that it is not sufficient reason to add nearly useless features. There is a cost to every addition. Terry Jan Reedy From akhilanger at gmail.com Thu Jul 16 14:57:09 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 11:57:09 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> Message-ID: <24522412.post@talk.nabble.com> Hi, Thanks all for the replies. I am working on a cluster of 15 nodes and I have now installed python 3.1 on all of them. I tried installing python2.6 but there was some make error. So, I do not want to give more time in installing 2.4 and rather use 3.1 but for that I need to convert my 2.4 code to 3.1. I used 2to3 tool, and it did make many changes in the 2.4 code, but still there are some indentation errors that I am unable to resolve being new to python. I have attached my python code, can anyone please fix the indentation error in the code. I am using vi editor. --Thanks a lot, Akhil http://www.nabble.com/file/p24522412/temp.py temp.py alex23 wrote: > > On Jul 16, 9:00?pm, akhil1988 wrote: >> I have switched to python 3.1 , but now I am getting some syntax errors >> in >> the code: > > Python 3.x was a major release that endeavoured to clean up a number > of lingering issues with the language, the upshot being that it isn't > entirely backwards compatible with past versions. Unicode became the > default string type, which is what is causing the error here: the u- > prefix is no longer required (or even allowed). > > However, Py3.x _does_ come with a handy tool for automatically > converting Python 2.x code to 3.x, called 2to3. One of the things it > should do is convert Py2.x unicode values into their correct > representation in 3.x. > > With any luck, it should be able to convert the code you're using > entirely. Let us know how it goes. > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24522412.html Sent from the Python - python-list mailing list archive at Nabble.com. From 1x7y2z9 at gmail.com Thu Jul 16 15:02:52 2009 From: 1x7y2z9 at gmail.com (1x7y2z9) Date: Thu, 16 Jul 2009 12:02:52 -0700 (PDT) Subject: URLError errno and strerror not set Message-ID: <5bbbf0b7-743d-48bc-bd0d-67a6626a58cd@z4g2000prh.googlegroups.com> python 2.5.2 errno, strerror and message do not appear to be set in the following two cases (at least). Is this to be expected? (as an aside, arg[0] is set) # case 1 > print exception, exception.errno, exception.strerror, exception.message == '' None None True # case 2 > print exception, exception.errno, exception.strerror, exception.message == '' None None True Thanks. From piet at cs.uu.nl Thu Jul 16 15:10:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 21:10:08 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> <2b61ef2d-4bf6-47ba-824d-6b490a342edb@n11g2000yqb.googlegroups.com> Message-ID: >>>>> mheavner (m) wrote: >m> I realize that the Queue would be the best way of doing this, however >m> that involves transferring the huge amount of data for each call - my >m> hope was to transfer it once and have it remain in memory for the >m> subprocess across run() calls. Which huge amount of data? The datastructure you talked about can remain in the process. You only have to transfer the input for your calculation in the queue but you have to do that anyway. And there is only one run call per process. When run has terminated the process exits, so you would have a loop in the run(0 method getting work from the queue. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Thu Jul 16 15:26:02 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 12:26:02 -0700 Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> Message-ID: In article <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4 at m11g2000yqh.googlegroups.com>, hartley wrote: > >If you had loosened up on the sarcasm I would probably have read what >you wrote more thoroughly instead of just skimming through it. Thanks >for the help, but you should seriously consider doing something with >your patronizing attitude. http://www.mikeash.com/getting_answers.html http://www.catb.org/~esr/faqs/smart-questions.html -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From james at agentultra.com Thu Jul 16 15:36:11 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 16 Jul 2009 15:36:11 -0400 Subject: Ann: Google releases Python-based open-source NX server References: <42cd7043-58b7-4472-a149-9dc03bc169d3@y7g2000yqa.googlegroups.com> <85y6qok7ek.fsf@agentultra.com> Message-ID: <85r5wgju8k.fsf@agentultra.com> Robert Kern writes: > On 2009-07-16 09:51, J Kenneth King wrote: >> jkn writes: >> >>> Google quietly releases open-source NX server ...written in Python, >>> apparently >>> >>> >> Google_quietly_releases_open_source_NX_server?taxonomyId=88> >>> >>> Neatx can be downloaded from Google's code repository: >>> >>> >>> >>> Regards >>> J^n >> >> It's pretty cool, but not PEP8! Probably because they just bought the >> source off of another smaller proprietary project. > > No, it' just that Google does not use PEP8 for its internal style standard. Yeah probably. I'm just a little OCD sometimes. From akhilanger at gmail.com Thu Jul 16 15:41:40 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 12:41:40 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: <24522412.post@talk.nabble.com> References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24523113.post@talk.nabble.com> ok! I got the indentation errors fixed. Bu I get another error: Traceback (most recent call last): File "./temp.py", line 484, in main() File "./temp.py", line 476, in main line.decode('utf-8').strip() AttributeError: 'str' object has no attribute 'decode' I am using Python3.1 Thanks Akhil akhil1988 wrote: > > Hi, > > Thanks all for the replies. > > I am working on a cluster of 15 nodes and I have now installed python 3.1 > on all of them. I tried installing python2.6 but there was some make > error. So, I do not want to give more time in installing 2.4 and rather > use 3.1 but for that I need to convert my 2.4 code to 3.1. > > I used 2to3 tool, and it did make many changes in the 2.4 code, but still > there are some indentation errors that I am unable to resolve being new to > python. I have attached my python code, can anyone please fix the > indentation error in the code. I am using vi editor. > > --Thanks a lot, > Akhil http://www.nabble.com/file/p24522412/temp.py temp.py > > > alex23 wrote: >> >> On Jul 16, 9:00?pm, akhil1988 wrote: >>> I have switched to python 3.1 , but now I am getting some syntax errors >>> in >>> the code: >> >> Python 3.x was a major release that endeavoured to clean up a number >> of lingering issues with the language, the upshot being that it isn't >> entirely backwards compatible with past versions. Unicode became the >> default string type, which is what is causing the error here: the u- >> prefix is no longer required (or even allowed). >> >> However, Py3.x _does_ come with a handy tool for automatically >> converting Python 2.x code to 3.x, called 2to3. One of the things it >> should do is convert Py2.x unicode values into their correct >> representation in 3.x. >> >> With any luck, it should be able to convert the code you're using >> entirely. Let us know how it goes. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24523113.html Sent from the Python - python-list mailing list archive at Nabble.com. From tonylay at gmail.com Thu Jul 16 15:45:27 2009 From: tonylay at gmail.com (Tony Lay) Date: Thu, 16 Jul 2009 12:45:27 -0700 (PDT) Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: On Jul 16, 10:14?am, a... at pythoncraft.com (Aahz) wrote: > In article <45228cf4-0b37-4c52-bf6f-d7bd124b0... at l32g2000vbp.googlegroups.com>, > Tony ?Lay ? wrote: > > > > >Traceback (most recent call last): > > ?File "/usr/local/bin/meld", line 35, in > > ? ?import gettext > > ?File "/usr/local/lib/python2.6/gettext.py", line 49, in > > ? ?import locale, copy, os, re, struct, sys > > ?File "/usr/local/lib/python2.6/locale.py", line 15, in > > ? ?import functools > > ?File "/usr/local/lib/python2.6/functools.py", line 10, in > > ? ?from _functools import partial, reduce > >ImportError: No module named _functools > > Where is _functools.so? > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "If you think it's expensive to hire a professional to do the job, wait > until you hire an amateur." ?--Red Adair /usr/local/lib/python2.6/lib-dynload/ It's in the path... export PYTHONHOME=/usr/local/lib/python2.6 export PYTHONPATH=/usr/local/lib/python2.6:/usr/local/lib/python2.6/ site-packages:/usr/local/lib/python2.6/lib-dynload:/usr/local/etc/ pango From tonylay at gmail.com Thu Jul 16 15:56:31 2009 From: tonylay at gmail.com (Tony Lay) Date: Thu, 16 Jul 2009 12:56:31 -0700 (PDT) Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: On Jul 16, 10:14?am, a... at pythoncraft.com (Aahz) wrote: > In article <45228cf4-0b37-4c52-bf6f-d7bd124b0... at l32g2000vbp.googlegroups.com>, > Tony ?Lay ? wrote: > > > > >Traceback (most recent call last): > > ?File "/usr/local/bin/meld", line 35, in > > ? ?import gettext > > ?File "/usr/local/lib/python2.6/gettext.py", line 49, in > > ? ?import locale, copy, os, re, struct, sys > > ?File "/usr/local/lib/python2.6/locale.py", line 15, in > > ? ?import functools > > ?File "/usr/local/lib/python2.6/functools.py", line 10, in > > ? ?from _functools import partial, reduce > >ImportError: No module named _functools > > Where is _functools.so? > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "If you think it's expensive to hire a professional to do the job, wait > until you hire an amateur." ?--Red Adair /usr/local/lib/python2.6/lib-dynload/ It's in the path... export PYTHONHOME=/usr/local/lib/python2.6 export PYTHONPATH=/usr/local/lib/python2.6:/usr/local/lib/python2.6/ site-packages:/usr/local/lib/python2.6/lib-dynload:/usr/local/etc/ pango From kyrie at uh.cu Thu Jul 16 15:57:02 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Thu, 16 Jul 2009 15:57:02 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A5F5759.4010600@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <4A5F5759.4010600@sequans.com> Message-ID: <1247774222.4a5f860ec3cfa@mail.uh.cu> Quoting Jean-Michel Pichavant : > Emile van Sebille wrote: > > On 7/16/2009 7:04 AM Unknown said... > >> On 2009-07-16, Emile van Sebille wrote: > >>> daysInAdvance = int(inputVar) or 25 > >> > >> I don't get it. That doesn't work right when inputVar == "0". > >> > > Aah, but you didn't get to define right. :) For that particular > > example 0 is not a valid response. > > When I was talking about such error prone form of boolean operations, I > didn't expect to be right so quickly :p What do you mean by being "right so quickly", and "error prone" in this context? I would also ask "Unknown" why he believes that "int(intputVar) or 25" doesn't work right when inputVar == "0". The only false value that int() may return is zero, so the "or 25" clause is there only for that case. I can't see then how you think that is an error. > I'm not sure it makes sens anyway. I > mean, I could easily argue that the number 0 is something. In the end I > wonder if I shouldn't just acknowledge the python mechanism Then look it another way. The "Empty/Nothing" is just standard practice, there is nothing in python that forces you to be "false" if you are empty, or true otherwise. Instead, answer this: why do you need a /boolean/ value? Is there any case where you need to be certain that the object's type is 'bool'? If you think the answer is "yes", you may want to get more familiar with the "duck typing" concept. (That doesn't mean that there are no legitimate cases where duck typing is inappropriate, but that there are many cases where people, specially if they come from statically typed languages, may believe that it is inappropriate when it isn't). In the python world, one should care more about how an object /behaves/ than from what clase it came. If something quacks like a duck, then assume that it is a duck, at least for the quacking part. Most python objects carry a truth value. Sometimes it feels natural (None is "false", boolean True and False are "true" and "false", empty containers are expected to be false, 0 and '' are "false"). Sometimes, it is everything but natural, but that's a problem with the object's implementation (datetime.time comes to mind). So, you shouldn't care if you have a bool instance - it should be enough that it behaves like a bool (though, if you need a bool, you can always construct one). The "True or False" expression could return Giraffes, as long as Giraffes behave like the bool "True" in boolean context. If you care about the class of the result, you can ask for its truth value, and if you don't care about it, you can just ignore it, and use it as you would use a bool. And then, if you can return any object as long as it behaves properly, what would be better to return? A new bool? Why not new Giraffe, if they will have the same behaviour? Guido chose to return the a value that will say more about the result of the operation than just a boolean. It acts as a boolean - if you don't need anything else, treat it as such -, but it will be, whenever is possible, one of the objects in the sequence, in case you need more info. > without > trying to find any intuitive/natural/obvious logic in it, knowing that > sometimes the Truth lies far away from the Evidence. Don't do that. Many of python's decisions are very well thought. You may disagree with them, as I do with some, but they are rarely taken lightly. And this is one that I find very agreeable and in line with the rest of python's philosophy. -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From piet at cs.uu.nl Thu Jul 16 16:01:22 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 16 Jul 2009 22:01:22 +0200 Subject: This is a mess... References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: >>>>> Nick (N) wrote: >N> this is the new oop version, its pretty messy currently, and i do >N> understand it is a simple routine, but i'm using it as an exercise to >N> learn oop python... >N> first the (current) traceback: >N> [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test >N> >N> >N> Traceback (most recent call last): >N> File "oop_covariance.py", line 24, in >N> cov = set1.covariance(set2, Eigen_vect.dot) >N> File "/home/nleioatts/python/Eigen.py", line 66, in covariance >N> print self.vectors[i][i] >N> AttributeError: Eigen_vect instance has no attribute '__getitem__' self.vectors is a list of Eigen_vect objects. So self.vectors[i] is an Eigen_vect object. Now you do a subscript on this Eigen_vect object but you have not defined what that means. Presumably you want it to do the subscript on the self.e_vect in the Eigen_vect instance. Therefore you have to define the __getitem__ method in the Eigen_vect class. class Eigen_vect: def __init__(self, e_val, e_vect): self.e_val = e_val self.e_vect = e_vect def length(self): return len(self.e_vect) def __getitem__(self, indx): return self.e_vect[indx] I hope I did not make a mistake, I didn't check it because I don't want to make test input files. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nobody at nowhere.com Thu Jul 16 16:17:05 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 21:17:05 +0100 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: On Thu, 16 Jul 2009 09:18:47 -0500, Tim Chase wrote: > Yes, the dictatorial "a tab always equals 8 spaces" Saying "always" is incorrect; it is more accurate to say that tab stops are every 8 columns unless proven otherwise, with the burden of proof falling on whoever wants to use something different. From mensanator at aol.com Thu Jul 16 16:23:38 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 16 Jul 2009 13:23:38 -0700 (PDT) Subject: list of all possible values References: <907717ce-a3b1-46a3-80c1-7f2e59dbc501@q35g2000vbi.googlegroups.com> <8AEDA5E3386EA742B8C24C95FF0C7580078FF522@PDC-MAIL3.ubisoft.org> Message-ID: On Jul 16, 11:49 am, "Andreas Tawn" wrote: > > > Certainly possible with list comprehensions. > > > >>>> a = "abc" > > >>>> [(x, y) for x in a for y in a] > > > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', > 'c'), > > > ('c', 'a'), ('c', 'b'), ('c', 'c')] > > > > But I like bearophile's version better. > > > Andreas, > > > Thanks, but I think you were missing my point. I should have explained > better. > > > The advantage that bearophile's version is generic with respect to the > > number of elements in each combination. To go from 2 element pairs > > (e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e')) > > requires only a change in a parameter passed to itertools. > > > I don't see how you would do that with list comprehensions. You're > > example works nicely with 2 element pairs, but it seems to me like > > you'd need to recode it if you wanted to change it to 5 element pairs. > > > Am I wrong about that? Can you think of a way to write a function > > that, using list comprehensions, takes a list of values and the size > > of each combination, and returns the len(list)**(combination size) > > possible combinations using those values? > > > Thanks again, > > David > > David, > > I think my post got caught in the nebulous email eddys and seems to have > taken 16 hours to arrive on the list. It was meant to be a reply to your > first post, not your second. > > Having said that, I think I missed the point of that post too ;o) > > Maybe someone smarter than me can come up with a way to dynamically nest > the fors in a list comprehension, but I certainly can't do it. Well, I don't use list comprehension, but you can certainly make dynamic for loops. Not that this is a replacement for itertools (I wrote it before itertools had the ability to do all this). def ooloop6(a, n, perm=True, repl=True): if (not repl) and (n>len(a)): return r0 = range(n) r1 = r0[1:] if perm and repl: # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) e = ''.join(["p = [''.join((",v,")) ",f,"]"]) exec e return p if (not perm) and repl: # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) i = ' and '.join(['(c%s>=c%s)' % (j,j-1) for j in r1]) e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"]) exec e return p if perm and (not repl): # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) i = ' and '.join([' and '.join(['(c%s!=c%s)' % (j,k) for k in range(j)]) for j in r1]) e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"]) exec e return p if (not perm) and (not repl): # ok v = ','.join(['c%s' % i for i in r0]) f = ' '.join(['for c%s in a' % i for i in r0]) i = ' and '.join(['(c%s>c%s)' % (j,j-1) for j in r1]) e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"]) exec e return p print '0123456789 taken 2 at a time' p = ooloop6('0123456789',2,True,True) print print 'Permutations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',2,True,False) print print 'Permutations without Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',2,False,True) print print 'Combinations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',2,False,False) print print 'Combinations without Replacement: %6d' % (len(p)) print p print print '0123456789 taken 3 at a time' p = ooloop6('0123456789',3,True,True) print print 'Permutations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',3,True,False) print print 'Permutations without Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',3,False,True) print print 'Combinations with Replacement: %6d' % (len(p)) print p p = ooloop6('0123456789',3,False,False) print print 'Combinations without Replacement: %6d' % (len(p)) print p 0123456789 taken 2 at a time Permutations with Replacement: 100 ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99'] Permutations without Replacement: 90 ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98'] Combinations with Replacement: 55 ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '11', '12', '13', '14', '15', '16', '17', '18', '19', '22', '23', '24', '25', '26', '27', '28', '29', '33', '34', '35', '36', '37', '38', '39', '44', '45', '46', '47', '48', '49', '55', '56', '57', '58', '59', '66', '67', '68', '69', '77', '78', '79', '88', '89', '99'] Combinations without Replacement: 45 ['01', '02', '03', '04', '05', '06', '07', '08', '09', '12', '13', '14', '15', '16', '17', '18', '19', '23', '24', '25', '26', '27', '28', '29', '34', '35', '36', '37', '38', '39', '45', '46', '47', '48', '49', '56', '57', '58', '59', '67', '68', '69', '78', '79', '89'] 0123456789 taken 3 at a time Permutations with Replacement: 1000 ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012', '013', '014', '015', '016', '017', '018', '019', '020', '021', '022', '023', '024', '025', '026', '027', '028', '029', '030', '031', '032', '033', '034', '035', '036', '037', '038', '039', '040', '041', '042', '043', '044', '045', '046', '047', '048', '049', '050', '051', '052', '053', '054', '055', '056', '057', '058', '059', '060', '061', '062', '063', '064', '065', '066', '067', '068', '069', '070', '071', '072', '073', '074', '075', '076', '077', '078', '079', '080', '081', '082', '083', '084', '085', '086', '087', '088', '089', '090', '091', '092', '093', '094', '095', '096', '097', '098', '099', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '220', '221', '222', '223', '224', '225', '226', '227', '228', '229', '230', '231', '232', '233', '234', '235', '236', '237', '238', '239', '240', '241', '242', '243', '244', '245', '246', '247', '248', '249', '250', '251', '252', '253', '254', '255', '256', '257', '258', '259', '260', '261', '262', '263', '264', '265', '266', '267', '268', '269', '270', '271', '272', '273', '274', '275', '276', '277', '278', '279', '280', '281', '282', '283', '284', '285', '286', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '302', '303', '304', '305', '306', '307', '308', '309', '310', '311', '312', '313', '314', '315', '316', '317', '318', '319', '320', '321', '322', '323', '324', '325', '326', '327', '328', '329', '330', '331', '332', '333', '334', '335', '336', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '357', '358', '359', '360', '361', '362', '363', '364', '365', '366', '367', '368', '369', '370', '371', '372', '373', '374', '375', '376', '377', '378', '379', '380', '381', '382', '383', '384', '385', '386', '387', '388', '389', '390', '391', '392', '393', '394', '395', '396', '397', '398', '399', '400', '401', '402', '403', '404', '405', '406', '407', '408', '409', '410', '411', '412', '413', '414', '415', '416', '417', '418', '419', '420', '421', '422', '423', '424', '425', '426', '427', '428', '429', '430', '431', '432', '433', '434', '435', '436', '437', '438', '439', '440', '441', '442', '443', '444', '445', '446', '447', '448', '449', '450', '451', '452', '453', '454', '455', '456', '457', '458', '459', '460', '461', '462', '463', '464', '465', '466', '467', '468', '469', '470', '471', '472', '473', '474', '475', '476', '477', '478', '479', '480', '481', '482', '483', '484', '485', '486', '487', '488', '489', '490', '491', '492', '493', '494', '495', '496', '497', '498', '499', '500', '501', '502', '503', '504', '505', '506', '507', '508', '509', '510', '511', '512', '513', '514', '515', '516', '517', '518', '519', '520', '521', '522', '523', '524', '525', '526', '527', '528', '529', '530', '531', '532', '533', '534', '535', '536', '537', '538', '539', '540', '541', '542', '543', '544', '545', '546', '547', '548', '549', '550', '551', '552', '553', '554', '555', '556', '557', '558', '559', '560', '561', '562', '563', '564', '565', '566', '567', '568', '569', '570', '571', '572', '573', '574', '575', '576', '577', '578', '579', '580', '581', '582', '583', '584', '585', '586', '587', '588', '589', '590', '591', '592', '593', '594', '595', '596', '597', '598', '599', '600', '601', '602', '603', '604', '605', '606', '607', '608', '609', '610', '611', '612', '613', '614', '615', '616', '617', '618', '619', '620', '621', '622', '623', '624', '625', '626', '627', '628', '629', '630', '631', '632', '633', '634', '635', '636', '637', '638', '639', '640', '641', '642', '643', '644', '645', '646', '647', '648', '649', '650', '651', '652', '653', '654', '655', '656', '657', '658', '659', '660', '661', '662', '663', '664', '665', '666', '667', '668', '669', '670', '671', '672', '673', '674', '675', '676', '677', '678', '679', '680', '681', '682', '683', '684', '685', '686', '687', '688', '689', '690', '691', '692', '693', '694', '695', '696', '697', '698', '699', '700', '701', '702', '703', '704', '705', '706', '707', '708', '709', '710', '711', '712', '713', '714', '715', '716', '717', '718', '719', '720', '721', '722', '723', '724', '725', '726', '727', '728', '729', '730', '731', '732', '733', '734', '735', '736', '737', '738', '739', '740', '741', '742', '743', '744', '745', '746', '747', '748', '749', '750', '751', '752', '753', '754', '755', '756', '757', '758', '759', '760', '761', '762', '763', '764', '765', '766', '767', '768', '769', '770', '771', '772', '773', '774', '775', '776', '777', '778', '779', '780', '781', '782', '783', '784', '785', '786', '787', '788', '789', '790', '791', '792', '793', '794', '795', '796', '797', '798', '799', '800', '801', '802', '803', '804', '805', '806', '807', '808', '809', '810', '811', '812', '813', '814', '815', '816', '817', '818', '819', '820', '821', '822', '823', '824', '825', '826', '827', '828', '829', '830', '831', '832', '833', '834', '835', '836', '837', '838', '839', '840', '841', '842', '843', '844', '845', '846', '847', '848', '849', '850', '851', '852', '853', '854', '855', '856', '857', '858', '859', '860', '861', '862', '863', '864', '865', '866', '867', '868', '869', '870', '871', '872', '873', '874', '875', '876', '877', '878', '879', '880', '881', '882', '883', '884', '885', '886', '887', '888', '889', '890', '891', '892', '893', '894', '895', '896', '897', '898', '899', '900', '901', '902', '903', '904', '905', '906', '907', '908', '909', '910', '911', '912', '913', '914', '915', '916', '917', '918', '919', '920', '921', '922', '923', '924', '925', '926', '927', '928', '929', '930', '931', '932', '933', '934', '935', '936', '937', '938', '939', '940', '941', '942', '943', '944', '945', '946', '947', '948', '949', '950', '951', '952', '953', '954', '955', '956', '957', '958', '959', '960', '961', '962', '963', '964', '965', '966', '967', '968', '969', '970', '971', '972', '973', '974', '975', '976', '977', '978', '979', '980', '981', '982', '983', '984', '985', '986', '987', '988', '989', '990', '991', '992', '993', '994', '995', '996', '997', '998', '999'] Permutations without Replacement: 720 ['012', '013', '014', '015', '016', '017', '018', '019', '021', '023', '024', '025', '026', '027', '028', '029', '031', '032', '034', '035', '036', '037', '038', '039', '041', '042', '043', '045', '046', '047', '048', '049', '051', '052', '053', '054', '056', '057', '058', '059', '061', '062', '063', '064', '065', '067', '068', '069', '071', '072', '073', '074', '075', '076', '078', '079', '081', '082', '083', '084', '085', '086', '087', '089', '091', '092', '093', '094', '095', '096', '097', '098', '102', '103', '104', '105', '106', '107', '108', '109', '120', '123', '124', '125', '126', '127', '128', '129', '130', '132', '134', '135', '136', '137', '138', '139', '140', '142', '143', '145', '146', '147', '148', '149', '150', '152', '153', '154', '156', '157', '158', '159', '160', '162', '163', '164', '165', '167', '168', '169', '170', '172', '173', '174', '175', '176', '178', '179', '180', '182', '183', '184', '185', '186', '187', '189', '190', '192', '193', '194', '195', '196', '197', '198', '201', '203', '204', '205', '206', '207', '208', '209', '210', '213', '214', '215', '216', '217', '218', '219', '230', '231', '234', '235', '236', '237', '238', '239', '240', '241', '243', '245', '246', '247', '248', '249', '250', '251', '253', '254', '256', '257', '258', '259', '260', '261', '263', '264', '265', '267', '268', '269', '270', '271', '273', '274', '275', '276', '278', '279', '280', '281', '283', '284', '285', '286', '287', '289', '290', '291', '293', '294', '295', '296', '297', '298', '301', '302', '304', '305', '306', '307', '308', '309', '310', '312', '314', '315', '316', '317', '318', '319', '320', '321', '324', '325', '326', '327', '328', '329', '340', '341', '342', '345', '346', '347', '348', '349', '350', '351', '352', '354', '356', '357', '358', '359', '360', '361', '362', '364', '365', '367', '368', '369', '370', '371', '372', '374', '375', '376', '378', '379', '380', '381', '382', '384', '385', '386', '387', '389', '390', '391', '392', '394', '395', '396', '397', '398', '401', '402', '403', '405', '406', '407', '408', '409', '410', '412', '413', '415', '416', '417', '418', '419', '420', '421', '423', '425', '426', '427', '428', '429', '430', '431', '432', '435', '436', '437', '438', '439', '450', '451', '452', '453', '456', '457', '458', '459', '460', '461', '462', '463', '465', '467', '468', '469', '470', '471', '472', '473', '475', '476', '478', '479', '480', '481', '482', '483', '485', '486', '487', '489', '490', '491', '492', '493', '495', '496', '497', '498', '501', '502', '503', '504', '506', '507', '508', '509', '510', '512', '513', '514', '516', '517', '518', '519', '520', '521', '523', '524', '526', '527', '528', '529', '530', '531', '532', '534', '536', '537', '538', '539', '540', '541', '542', '543', '546', '547', '548', '549', '560', '561', '562', '563', '564', '567', '568', '569', '570', '571', '572', '573', '574', '576', '578', '579', '580', '581', '582', '583', '584', '586', '587', '589', '590', '591', '592', '593', '594', '596', '597', '598', '601', '602', '603', '604', '605', '607', '608', '609', '610', '612', '613', '614', '615', '617', '618', '619', '620', '621', '623', '624', '625', '627', '628', '629', '630', '631', '632', '634', '635', '637', '638', '639', '640', '641', '642', '643', '645', '647', '648', '649', '650', '651', '652', '653', '654', '657', '658', '659', '670', '671', '672', '673', '674', '675', '678', '679', '680', '681', '682', '683', '684', '685', '687', '689', '690', '691', '692', '693', '694', '695', '697', '698', '701', '702', '703', '704', '705', '706', '708', '709', '710', '712', '713', '714', '715', '716', '718', '719', '720', '721', '723', '724', '725', '726', '728', '729', '730', '731', '732', '734', '735', '736', '738', '739', '740', '741', '742', '743', '745', '746', '748', '749', '750', '751', '752', '753', '754', '756', '758', '759', '760', '761', '762', '763', '764', '765', '768', '769', '780', '781', '782', '783', '784', '785', '786', '789', '790', '791', '792', '793', '794', '795', '796', '798', '801', '802', '803', '804', '805', '806', '807', '809', '810', '812', '813', '814', '815', '816', '817', '819', '820', '821', '823', '824', '825', '826', '827', '829', '830', '831', '832', '834', '835', '836', '837', '839', '840', '841', '842', '843', '845', '846', '847', '849', '850', '851', '852', '853', '854', '856', '857', '859', '860', '861', '862', '863', '864', '865', '867', '869', '870', '871', '872', '873', '874', '875', '876', '879', '890', '891', '892', '893', '894', '895', '896', '897', '901', '902', '903', '904', '905', '906', '907', '908', '910', '912', '913', '914', '915', '916', '917', '918', '920', '921', '923', '924', '925', '926', '927', '928', '930', '931', '932', '934', '935', '936', '937', '938', '940', '941', '942', '943', '945', '946', '947', '948', '950', '951', '952', '953', '954', '956', '957', '958', '960', '961', '962', '963', '964', '965', '967', '968', '970', '971', '972', '973', '974', '975', '976', '978', '980', '981', '982', '983', '984', '985', '986', '987'] Combinations with Replacement: 220 ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '011', '012', '013', '014', '015', '016', '017', '018', '019', '022', '023', '024', '025', '026', '027', '028', '029', '033', '034', '035', '036', '037', '038', '039', '044', '045', '046', '047', '048', '049', '055', '056', '057', '058', '059', '066', '067', '068', '069', '077', '078', '079', '088', '089', '099', '111', '112', '113', '114', '115', '116', '117', '118', '119', '122', '123', '124', '125', '126', '127', '128', '129', '133', '134', '135', '136', '137', '138', '139', '144', '145', '146', '147', '148', '149', '155', '156', '157', '158', '159', '166', '167', '168', '169', '177', '178', '179', '188', '189', '199', '222', '223', '224', '225', '226', '227', '228', '229', '233', '234', '235', '236', '237', '238', '239', '244', '245', '246', '247', '248', '249', '255', '256', '257', '258', '259', '266', '267', '268', '269', '277', '278', '279', '288', '289', '299', '333', '334', '335', '336', '337', '338', '339', '344', '345', '346', '347', '348', '349', '355', '356', '357', '358', '359', '366', '367', '368', '369', '377', '378', '379', '388', '389', '399', '444', '445', '446', '447', '448', '449', '455', '456', '457', '458', '459', '466', '467', '468', '469', '477', '478', '479', '488', '489', '499', '555', '556', '557', '558', '559', '566', '567', '568', '569', '577', '578', '579', '588', '589', '599', '666', '667', '668', '669', '677', '678', '679', '688', '689', '699', '777', '778', '779', '788', '789', '799', '888', '889', '899', '999'] Combinations without Replacement: 120 ['012', '013', '014', '015', '016', '017', '018', '019', '023', '024', '025', '026', '027', '028', '029', '034', '035', '036', '037', '038', '039', '045', '046', '047', '048', '049', '056', '057', '058', '059', '067', '068', '069', '078', '079', '089', '123', '124', '125', '126', '127', '128', '129', '134', '135', '136', '137', '138', '139', '145', '146', '147', '148', '149', '156', '157', '158', '159', '167', '168', '169', '178', '179', '189', '234', '235', '236', '237', '238', '239', '245', '246', '247', '248', '249', '256', '257', '258', '259', '267', '268', '269', '278', '279', '289', '345', '346', '347', '348', '349', '356', '357', '358', '359', '367', '368', '369', '378', '379', '389', '456', '457', '458', '459', '467', '468', '469', '478', '479', '489', '567', '568', '569', '578', '579', '589', '678', '679', '689', '789'] >>> > > Sorry for the confusion. > > Cheers, > > Drea From nobody at nowhere.com Thu Jul 16 16:25:50 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 21:25:50 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> Message-ID: On Thu, 16 Jul 2009 11:06:54 +0200, Jean-Michel Pichavant wrote: >>> So if I resume: >>> - not 'foo' => False >>> - 'foo' or 'foo' => 'foo' >>> >>> I may be missing something, but honestly, Guido must have smoked some >>> heavy stuff to write such logic, has he ? >> >> Several languages (e.g. Lisp, Bourne shell) behave the same way, i.e. "or" >> returns the first element which is considered true while "and" returns the >> last element provided that all preceding elements are considered true. >> >> [snip] >> > > Ok then, why "or" does not return True, if the first element is > considered True ? If the first element is true, returning the first element is returning true. > Why returning the element itself. Any reason for that ? Why not? Where is the benefit in collapsing all true values to True? You can convert values to True/False with bool(), but the conversion cannot be reversed. It only makes a difference if you are interested in the representation rather than the value. Do you normally test for equality with "is" or "=="? From nobody at nowhere.com Thu Jul 16 16:29:07 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 21:29:07 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On Wed, 15 Jul 2009 18:14:10 +0200, Hrvoje Niksic wrote: >> If the question was "Why is there no 'or' operator ?", would "because >> A or B <=> not(not A and not B)" be a proper answer ? > > Note that in Python A or B is in fact not equivalent to not(not A and > not B). Ah, but it *is* "equivalent"; it isn't "identical", but that's not the point. From nick at craig-wood.com Thu Jul 16 16:29:59 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 16 Jul 2009 15:29:59 -0500 Subject: This is a mess... References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: Nick wrote: > this is the new oop version, its pretty messy currently, and i do > understand it is a simple routine, but i'm using it as an exercise to > learn oop python... > > first the (current) traceback: > [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test > > > Traceback (most recent call last): > File "oop_covariance.py", line 24, in > cov = set1.covariance(set2, Eigen_vect.dot) > File "/home/nleioatts/python/Eigen.py", line 66, in covariance > print self.vectors[i][i] > AttributeError: Eigen_vect instance has no attribute '__getitem__' You are trying to use your Eigen_vect class as an array. In order for an object to act as an array it needs a __getitem__ method. > class Eigen_vect: > def __init__(self, e_val, e_vect): > self.e_val = e_val > self.e_vect = e_vect > def length(self): > return len(self.e_vect) > > def dot(self, other): > d = 0.0 > if other.length() != self.length(): > raise ValueError, "Eigen Vectors not same Length" > for k in range(self.length()): > # print "HI NICK", self.e_vect[k], other.e_vect[k] > d += float(self.e_vect[k]) * float(other.e_vect[k]) > return d Either add a __getitem__ method like this def __getitem__(self, n): return self.e_vect[n] Or you might want to subclass list if you are going to do a lot of list like operations on Eigen_vects, eg class Eigen_vect(list): def __init__(self, e_val, e_vect): self.e_val = e_val self[:] = e_vect def dot(self, other): d = 0.0 if len(other) != len(self): raise ValueError("Eigen Vectors not same Length") for a, b in zip(self, other): d += float(a) * float(b) return d Notice that now these things are lists, you can use len, zip etc on them which makes the code much neater. e = Eigen_vect(3, range(10)) f = Eigen_vect(4, range(1,11)) print e print f print e[2] print e.dot(f) Which prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 2 330.0 -- Nick Craig-Wood -- http://www.craig-wood.com/nick From robertrobert93 at yahoo.com Thu Jul 16 16:41:39 2009 From: robertrobert93 at yahoo.com (Robert Robert) Date: Thu, 16 Jul 2009 13:41:39 -0700 (PDT) Subject: file write IOError Invalid argument Message-ID: <505868.24184.qm@web59308.mail.re1.yahoo.com> Hello All, I am trying to write a binary string to file on a windows network share. I get an IOError. I've read that it is because the file size is too large. I did a type( binaryString) and saw that it was type "str". So I loop through it one by one element and use f.write to write to file, and it worked without any error. However it took a long while to write to file. I was wondering how I could split this binary string into N number of chunks efficiently and write them to file. thanks, rh -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Jul 16 16:42:48 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 16:42:48 -0400 Subject: turtle dump In-Reply-To: <4A5F287B.2000503@xs4all.nl> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Michiel Overtoom wrote: > > I got success with the following code (python 2.6.2): > > import turtle > turtle.reset() > for i in range(4): > turtle.forward(50) > turtle.right(90) > can=turtle.getscreen().getcanvas() > can.postscript(file="tmp.ps") Is raw postscript (.ps) the only thing tk can write from canvas? I would like to be able to import into OpenOffice document (drawing object) and it can import encapsulated postscript (.eps) and about 20 other things but not, apparently, .ps. Help on method postscript: postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance is spectacularly useless. I did not see anything similar in the list of about 100 attributes. The image_type of the canvas is 'photo bitmap' but I see no method to write that. From tjreedy at udel.edu Thu Jul 16 16:55:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 16:55:08 -0400 Subject: This is a mess... In-Reply-To: References: <8c7ffaf5-67a6-4866-9c9d-271be8b8d8c6@c1g2000yqi.googlegroups.com> Message-ID: Nick wrote: > this is the new oop version, its pretty messy currently, and i do > understand it is a simple routine, but i'm using it as an exercise to > learn oop python... You neglected to specify Python version. > > first the (current) traceback: > [:~/python]$ python oop_covariance.py b2ar_all_test b2ar_all_test > > > Traceback (most recent call last): > File "oop_covariance.py", line 24, in > cov = set1.covariance(set2, Eigen_vect.dot) > File "/home/nleioatts/python/Eigen.py", line 66, in covariance > print self.vectors[i][i] > AttributeError: Eigen_vect instance has no attribute '__getitem__' self.vectors[i] is apparently an Eigen_vect instance and you try to subscript it, but, as the message said, there is no __getitem__ method. So add one. > > and a quick explaination: the file structures aare a 2d list of lists > of numbers corresponding to eginvectors and a list of numbers > corresponding to eigenvalues > > #########################33##Here is the main body > #!/usr/bin/env python > import sys > import Eigen > from Eigen import * > > if len(sys.argv) != 3: > print " " > print "The corrent usage is 'python covariance.py file1 file2'" > print "where the _U.asc and _s.asc will be appended when needed" > print " " > exit(1) > file1 = sys.argv[1] > file2 = sys.argv[2] > set1 = Eigen_set(file1+"_U.asc", file1+"_s.asc") > set2 = Eigen_set(file2+"_U.asc", file2+"_s.asc") > cov = set1.covariance(set2, Eigen_vect.dot) > print cov > > > ###############and here are the classes: > > #!/usr/bin/env python > import sys > import math > from math import sqrt > > class Eigen_vect: > def __init__(self, e_val, e_vect): > self.e_val = e_val > self.e_vect = e_vect > def length(self): > return len(self.e_vect) You should really call this __len__ so len(Eigen_vect()) will work. I presume you need something like def __getitem__(self, i): return self.e_vect[i] Before continuing, read the Language Reference / Data Model / Special method names to understand how to write classes to integrate into syntax. Terry Jan Reedy From pavlovevidence at gmail.com Thu Jul 16 17:00:43 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 16 Jul 2009 14:00:43 -0700 (PDT) Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: On Jul 16, 8:12?am, Gabriel Rossetti wrote: > Hello everyone, > > I am using threading.Condition.wait(timeout) and was surprised to see > that there is no return value nor an exception when wait() is used w/ a > timeout. How am I supposed to know if it was notified or if it timed out? That's a good question. Condition.wait seems to violate an invariant if it can return without having acquired the underlying lock. And if it does the least it could do is to return a status so you know if you have to skeddadle. Workaround is to use the _is_owned() method of Condition to see if you are the owner of the condition, but this method is undocumented. cond = Condition() ... cond.acquire() while not ok_to_proceed(): cond.wait() if not cond._is_owned(): # must've timed out raise TimeOutException operate() cond.release() Carl Banks From tjreedy at udel.edu Thu Jul 16 17:10:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 17:10:15 -0400 Subject: interactive fiction in Python? In-Reply-To: <853a8wlm25.fsf@agentultra.com> References: <5c97224b-99d4-469b-90cb-1c04b1e1e9a0@m11g2000yqh.googlegroups.com> <853a8wlm25.fsf@agentultra.com> Message-ID: J Kenneth King wrote: > George Oliver writes: > >> hi, I'm just curious who might be working on interactive fiction >> modules in the style of Inform or TADS for Python. I've seen a few >> threads on this list [1] (among many that mention IF tangentially), >> and there are old projects like PUB and PAWS. There are some newer >> potential projects such as Curveship as well. In any case I'd like to >> see what's out there. www.renpy.org Visual novel engine written in Python, with full access to Python as needed. Download includes a tutorial written with renpy. You can look as source code in a separate window as you go (at least earlier version I used did). Also has 150+ free games/novels to download. "Ren'Py is a free and cross-platform engine that helps you use words, pictures, and sounds to tell stories with the computer." Terry Jan Reedy From nobody at nowhere.com Thu Jul 16 17:12:32 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 16 Jul 2009 22:12:32 +0100 Subject: Why aren't OrderedDicts comparable with < etc? References: <401967fb-c12e-4f63-8b05-6dffa0688028@a26g2000yqn.googlegroups.com> <028e48c2-4a3a-4920-9a67-d9bb95714b4f@c36g2000yqn.googlegroups.com> Message-ID: On Thu, 16 Jul 2009 03:59:47 -0700, Mark wrote: >> > Or maybe not. If OrderedDicts are sequences as well as mappings, then we >> > should be able to sort them. And that seems a bit much even for me. >> One thing that I've just noticed is that you can use <, <=, >=, and > >> with sets: >> It seems a bit inconsistent that with sets you always get False when >> using an ordering operator but with an ordered dict you get an >> exception? > > Ooops---disregard the above---I forgot that these do subset and > superset comparisions! Which is an argument for dictionaries (ordered or not) doing likewise, except that the comparison would be subfunction rather than subset, i.e. d1 References: Message-ID: <26ddd1750907161422n5b640a48pbbf9325dd05a699d@mail.gmail.com> On Thu, Jul 16, 2009 at 5:00 PM, Carl Banks wrote: > On Jul 16, 8:12?am, Gabriel Rossetti > wrote: >> Hello everyone, >> >> I am using threading.Condition.wait(timeout) and was surprised to see >> that there is no return value nor an exception when wait() is used w/ a >> timeout. How am I supposed to know if it was notified or if it timed out? > > That's a good question. ?Condition.wait seems to violate an invariant > if it can return without having acquired the underlying lock. ?And if > it does the least it could do is to return a status so you know if you > have to skeddadle. > > Workaround is to use the _is_owned() method of Condition to see if you > are the owner of the condition, but this method is undocumented. > > cond = Condition() > ... > cond.acquire() > while not ok_to_proceed(): > ? ?cond.wait() > ? ?if not cond._is_owned(): > ? ? ? ?# must've timed out > ? ? ? ?raise TimeOutException > operate() > cond.release() You will always be the owner of the condition whether it times out or your thread is notified. This doesn't solve the problem. As an aside, the implementation of wait() is rather poor in any case; the sleep function should not be used for the purpose of waiting for an event. Depending on what platform you are using, I can offer two solutions. The simplest one is to subclass Condition and override the wait method. Just copy the code as it is, but modify the end of the function to look like this: if not gotit: if __debug__: self._note("%s.wait(%s): timed out", self, timeout) try: self.__waiters.remove(waiter) except ValueError: pass return False else: if __debug__: self._note("%s.wait(%s): got it", self, timeout) return True The two return statements will tell you whether you timed out ("if not gotit" condition), or were notified ("else"). The better way to solve this and the other problem that I mentioned is to use native OS events. This is what I had to do for a recent project. I used pywin32 extension to completely rewrite Event, Condition, and Timer classes to use a true event-based approach. In the process of the rewrite I also modified some functionality, such as returning True of False from wait(). Unfortunately, I'm not able to post this code right now, though I can try and get permission to do so a bit later. If you are writing software for Windows, then simply take a look at win32event module. CreateEvent, SetEvent, ResetEvent, WaitForSingleObject are all the Windows functions that you need to reimplement threading.Event. Using the new event class you can implement a proper Condition.wait() method (hint: use new events rather than locks to wait for notification). If you are on Linux or BSD, I can't help you. I'm sure that there is some equivalent functionality that you may be able to access using ctypes. Exactly how to do it, however, is not something that I can help you with right now. - Max From __peter__ at web.de Thu Jul 16 17:37:06 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 16 Jul 2009 23:37:06 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Terry Reedy wrote: > Michiel Overtoom wrote: >> >> I got success with the following code (python 2.6.2): >> >> import turtle >> turtle.reset() >> for i in range(4): >> turtle.forward(50) >> turtle.right(90) >> can=turtle.getscreen().getcanvas() >> can.postscript(file="tmp.ps") > > Is raw postscript (.ps) the only thing tk can write from canvas? > I would like to be able to import into OpenOffice document (drawing > object) and it can import encapsulated postscript (.eps) and about 20 > other things but not, apparently, .ps. > > Help on method postscript: > postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance > > is spectacularly useless. http://docs.python.org/library/turtle.html#turtle.getcanvas The module is a bit messy, but the accompanying documentation seems OK to me. > I did not see anything similar in the list of > about 100 attributes. The image_type of the canvas is 'photo bitmap' but > I see no method to write that. That's probably a bitmap that you can draw on the canvas. The Canvas is actually that of Tkinter, and according to http://www.tcl.tk/man/tcl8.5/TkCmd/canvas.htm#M59 it writes Encapsulated Postscript 3.0. Also: $ file tmp.ps tmp.ps: PostScript document text conforming DSC level 3.0, type EPS Try changing the file extension from .ps to .eps. Peter From emile at fenx.com Thu Jul 16 17:59:22 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Jul 2009 14:59:22 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On 7/16/2009 1:29 PM Nobody said... > On Wed, 15 Jul 2009 18:14:10 +0200, Hrvoje Niksic wrote: > >>> If the question was "Why is there no 'or' operator ?", would "because >>> A or B <=> not(not A and not B)" be a proper answer ? >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). > > Ah, but it *is* "equivalent"; it isn't "identical", but that's not the > point. > I'm not sure I'd call it equivalent. A or B returns either unaltered, and not(not A and not B) always returns a boolean. The equivalent would be not(not( A or B )). Emile From piet at cs.uu.nl Thu Jul 16 18:03:52 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 00:03:52 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Hello everyone, >GR> I am using threading.Condition.wait(timeout) and was surprised to see that >GR> there is no return value nor an exception when wait() is used w/ a timeout. >GR> How am I supposed to know if it was notified or if it timed out? Normally you wouldn't have to know. The logic of your program should be such that you wait until a certain condition is satisfied. After each wait you usually check that condition anyway, like: while (some_condition_satisfied): cond_var.wait() The only exception could be if there is a 1-1 relation between a single waiter and a single notifier. I that case you can usually use if instead of while (but it does depend of the logic of the notifier). So in case of using a timeout you can check the desired condition after the wait and decide what to do. If the condition was not satisfied you probably want to do something special, like error handling, or do something else first and then retry the wait. There could be situations where this doesn't work, however. For example if you wait until a queue is not empty, and the notify is done when something is inserted in the queue, another thread may sneak in while you are waiting and snatch the inserted item just before your thread continues after the wait. In that case you can't distinguish between a timeout and a snatcher. So it all depends on what your use case is. (Java's wait doesn't return anything either.) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From thelists at optusnet.com.au Thu Jul 16 18:07:30 2009 From: thelists at optusnet.com.au (Leo List) Date: 16 Jul 2009 22:07:30 GMT Subject: Python 3 xmlrpc client HTTPS problem Message-ID: <4a5fa4a2$0$3321$afc38c87@news.optusnet.com.au> I have just built and installed version 3.1 and all's good, but when I try to connect to a server via HTTPS I get the following: File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1029, in __call__ return self.__send(self.__name, args) File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1271, in __request verbose=self.__verbose File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1058, in request http_conn = self.send_request(host, handler, request_body, verbose) File "/usr/local/lib/python3.1/xmlrpc/client.py", line 1183, in send_request "your version of http.client doesn't support HTTPS") NotImplementedError: your version of http.client doesn't support HTTPS I am on Ubuntu and have followed the instructions on how to build a release with SSL support: ./configure CPPFLAGS=-I/usr/include/openssl LDFLAGS=-L/usr/lib/ssl Any idea what I'm missing? Thanks From piet at cs.uu.nl Thu Jul 16 18:08:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 00:08:08 +0200 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: >>>>> akhil1988 (a) wrote: >a> ok! >a> I got the indentation errors fixed. Bu I get another error: >a> Traceback (most recent call last): >a> File "./temp.py", line 484, in >a> main() >a> File "./temp.py", line 476, in main >a> line.decode('utf-8').strip() >a> AttributeError: 'str' object has no attribute 'decode' >a> I am using Python3.1 In Python 3 you can't decode strings because they are Unicode strings and it doesn't make sense to decode a Unicode string. You can only decode encoded things which are byte strings. So you are mixing up byte strings and Unicode strings. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pavlovevidence at gmail.com Thu Jul 16 18:15:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 16 Jul 2009 15:15:52 -0700 (PDT) Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: On Jul 16, 2:22?pm, Maxim Khitrov wrote: > On Thu, Jul 16, 2009 at 5:00 PM, Carl Banks wrote: > > On Jul 16, 8:12?am, Gabriel Rossetti > > wrote: > >> Hello everyone, > > >> I am using threading.Condition.wait(timeout) and was surprised to see > >> that there is no return value nor an exception when wait() is used w/ a > >> timeout. How am I supposed to know if it was notified or if it timed out? > > > That's a good question. ?Condition.wait seems to violate an invariant > > if it can return without having acquired the underlying lock. ?And if > > it does the least it could do is to return a status so you know if you > > have to skeddadle. > > > Workaround is to use the _is_owned() method of Condition to see if you > > are the owner of the condition, but this method is undocumented. > > > cond = Condition() > > ... > > cond.acquire() > > while not ok_to_proceed(): > > ? ?cond.wait() > > ? ?if not cond._is_owned(): > > ? ? ? ?# must've timed out > > ? ? ? ?raise TimeOutException > > operate() > > cond.release() > > You will always be the owner of the condition whether it times out or > your thread is notified. This doesn't solve the problem. You are correct, I misread the code. So scratch what I said. Carl Banks From akhilanger at gmail.com Thu Jul 16 18:43:37 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 15:43:37 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24525761.post@talk.nabble.com> Then, how should I do it? I read a byte string from sys.stdin which needs to converted to unicode string for further processing. I cannot just remove the decode statement and proceed? This is it what it looks like: for line in sys.stdin: line = line.decode('utf-8').strip() if line == '': #do something here elsif #do something here If I remove the decode statement, line == '' never gets true. --Akhil Piet van Oostrum wrote: > >>>>>> akhil1988 (a) wrote: > >>a> ok! >>a> I got the indentation errors fixed. Bu I get another error: > >>a> Traceback (most recent call last): >>a> File "./temp.py", line 484, in >>a> main() >>a> File "./temp.py", line 476, in main >>a> line.decode('utf-8').strip() >>a> AttributeError: 'str' object has no attribute 'decode' > >>a> I am using Python3.1 > > In Python 3 you can't decode strings because they are Unicode strings > and it doesn't make sense to decode a Unicode string. You can only > decode encoded things which are byte strings. So you are mixing up byte > strings and Unicode strings. > -- > Piet van Oostrum > URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] > Private email: piet at vanoostrum.org > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24525761.html Sent from the Python - python-list mailing list archive at Nabble.com. From tjreedy at udel.edu Thu Jul 16 19:06:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 19:06:55 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Peter Otten wrote: > Terry Reedy wrote: >> Help on method postscript: >> postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance >> >> is spectacularly useless. This is from interactive help. > http://docs.python.org/library/turtle.html#turtle.getcanvas That method, following your lead, is how I got to .postscript. The doc says " Return the Canvas of this TurtleScreen. Useful for insiders who know what to do with a Tkinter Canvas." > The module is a bit messy, but the accompanying documentation seems OK to > me. ?? I am not an 'insider' ;-). >> I did not see anything similar in the list of >> about 100 attributes. Of Canvas, mostly methods. I can understand that no one who could write decent doc strings for the 100 methods has actually volunteered to do so. >The image_type of the canvas is 'photo bitmap' but >> I see no method to write that. > > That's probably a bitmap that you can draw on the canvas. The Canvas is > actually that of Tkinter, and according to > > http://www.tcl.tk/man/tcl8.5/TkCmd/canvas.htm#M59 > > it writes Encapsulated Postscript 3.0. Also: > > $ file tmp.ps > tmp.ps: PostScript document text conforming DSC level 3.0, type EPS > > Try changing the file extension from .ps to .eps. I will. Thank you. tjr From graham.dumpleton at gmail.com Thu Jul 16 19:25:37 2009 From: graham.dumpleton at gmail.com (Graham Dumpleton) Date: Thu, 16 Jul 2009 16:25:37 -0700 (PDT) Subject: does python have a generic object pool like commons-pool in Java References: <4f9a4244-3bae-4a6d-92a8-cd622ffccad2@m11g2000yqh.googlegroups.com> <38a7cf0b-fc70-4dee-a2c9-fcd999686dc8@j32g2000yqh.googlegroups.com> <12811e51-86a9-42b4-be61-b4e02961ab87@q11g2000yqi.googlegroups.com> <4a5ea61b$0$1682$742ec2ed@news.sonic.net> <0450b7d2-b9f6-4dc9-adf7-67e94b8f8b9b@v37g2000prg.googlegroups.com> <4a5eb46a$0$1673$742ec2ed@news.sonic.net> Message-ID: On Jul 16, 3:05?pm, John Nagle wrote: > alex23 wrote: > > On Jul 16, 2:03 pm, John Nagle wrote: > >> ? ? ?"fcgi" is an option for this sort of thing. ?With "mod_fcgi" installed > >> in Apache, and "fcgi.py" used to manage the Python side of the problem, you > >> can have semi-persistent programs started up and shut down for you on the > >> server side. > > > Hey John, > > > The environments in which I've been asked to develop webs apps using > > Python have all utilisedmod_wsgi. Do you have any experience with > >mod_wsgivs mod_fcgi, and if so, can you comment on any relevant > > performance / capability / ease-of-use differences? > > > Cheers, > > alex23 > > ? ? FCGI seems to be somewhat out of favor, perhaps because it isn't > complicated enough. I doubt that is the reason. It is out of favour for Python web hosting at least, because web hosting companies provide really crappy support for using Python with it and also don't like long lived processes. Most FASTCGI deployments are configured in a way more appropriate for PHP because that is what the default settings favour. This works to the detriment of Python. Even when people setup FASTCGI themselves, they still don't play with the configuration to optimise it for their specific Python application. Thus it can still run poorly. > It's a mature technology and works reasonably well. But is showing its age. It really needs a refresh. Various of its design decisions were from when network speeds and bandwidth were much lower and this complicates the design. Making the protocol format simpler in some areas would make it easier to implement. The protocol could also be built on to make process management more flexible, rather than relying on each implementation to come up with adhoc ways of managing it. Part of the problem with FASTCGI, or hosting of dynamic applications in general, is that web servers primary focus is serving of static files. Any support for dynamic web applications is more of a bolt on feature. Thus, web applications using Python will always be at a disadvantage. PHP manages okay because their model of operation is closer to the one shot processing of static files. Python requires persistent to work adequately with modern fat applications. > It's been a puzzle to me that FCGI was taken out of the > main Apache code base, because it gives production-level performance > with CGI-type simplicity. As far as I know FASTCGI support has in the past never been a part of the Apache code base. Both mod_fastcgi and mod_fcgid were developed by independent people and not under the ASF. In Apache 2.3 development versions (to become 2.4 when released), there will however be a mod_proxy_fcgi. The ASF has also taken over management of mod_fcgid and working out how that may be incorporated into future Apache versions. > ? ? WSGI has a mode for running Python inside the Apache process, > which is less secure and doesn't allow multiple Python processes. Depending on the requirements it is more than adequate and if configured correctly gives better performance and scalability. Not everyone runs in a shared hosting environment. The different modes of mod_wsgi therefore give choice. > That complicates mod_wsgi considerably, No it doesn't. It is actual daemon mode that complicates things, not embedded mode. > and ties it very closely > to specific versions of Python and Python modules. ?As a result, > the WSGI developers are patching at a great rate. What are you talking about when you say 'As a result, the WSGI developers are patching at a great rate'? As with some of your other comments, this is leaning towards being FUD and nothing more. There is no 'patching at a great rate' going on. >?I think the thing has too many "l33t features". It is all about choice and providing flexibility. You may not see a need for certain features, but it doesn't mean other people don't have a need for it. > I'd avoid "embedded mode". ?"Daemon mode" is the way to go if you use WSGI. I'd agree, but not sure you really understand the reasons for why it would be preferred. > I haven't tried WSGI; Then why comment on it as if you are knowledgeable on it. > I don't need the grief of a package under heavy development. More FUD. Graham From ben+python at benfinney.id.au Thu Jul 16 19:56:33 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 09:56:33 +1000 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: <87zlb4gp1q.fsf@benfinney.id.au> Inky 788 writes: > On Jul 10, 7:35?pm, Ben Finney wrote: > > Yes. That's a ?should? and not a ?must?, even though PEP 8 says it > > with a simple imperative:: > > > > ? ? Use 4 spaces per indentation level. > > That actually sounds pretty weird. "Don't do {foo}. Really, I mean > *really* don't do {foo}. Oh, also, the interpreter allows you to do > {foo}. But don't do it! I mean it!". Not weird at all, especially when you realise that your interpretation isn't there in the document. The allowance for both types of indentation is a compromise made for backward compatibility with code written before this convention was quite so strong. All *new* code is strongly advised to follow the convention, while the interpreter will still allow old code that doesn't follow the convention. > Very ... perlish, if you ask me. Hah! Perl doesn't have the ?don't do {foo}? part, so I don't see what you're seeing. -- \ ?I like to skate on the other side of the ice.? ?Steven Wright | `\ | _o__) | Ben Finney From lhughes42 at gmail.com Thu Jul 16 19:58:36 2009 From: lhughes42 at gmail.com (lh) Date: Thu, 16 Jul 2009 16:58:36 -0700 (PDT) Subject: Visualization of Python Class Hierarchy Message-ID: I would like to automatically generate this for my program. I am running python in an eclipse context (pydev). I am not familiar with the best current tools. Thank you From wuwei23 at gmail.com Thu Jul 16 20:37:00 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 16 Jul 2009 17:37:00 -0700 (PDT) Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> > >> Help on method postscript: > >> postscript(self, *args, **kw) method of turtle.ScrolledCanvas instance > >> is spectacularly useless. > > This is from interactive help. The help in iPython says the same, but also mentions that it's a dynamically generated function, so it may not be picking up the docstring that way. turtle.ScrolledCanvas.postscript is similarly terse, but you can find more info in turtle.Canvas.postscript: Print the contents of the canvas to a postscript file. Valid options: colormap, colormode, file, fontmap, height, pageanchor, pageheight, pagewidth, pagex, pagey, rotate, witdh, x, y. > The doc says > > " Return the Canvas of this TurtleScreen. Useful for insiders who know > what to do with a Tkinter Canvas." > > > The module is a bit messy, but the accompanying documentation seems OK to > > me. > > ?? I am not an 'insider' ;-). I don't think the intent of the turtle module is to be a primer in Tk, but more of an educational tool. The lack of image export niceties has a lot to do with the limitations of Tk, but was probably (and I'm conjecturing here) felt to be outside the scope of what the module aims to achieve. > Of Canvas, mostly methods. I can understand that no one who could write > decent doc strings for the 100 methods has actually volunteered to do so. I think they all _have_ doc strings, or at least the code they eventually call does, but I'd have to look into the module itself to see if they could be brought over dynamically. You may be interested in the final page of Greg Lingl's PyCon talk, Seven Ways to use Turtle[1] which states: The turtle module is designed in a way so that essentially all of the turtle graphics machinery is based on a class TurtleScreenBase, which provides the interface to the underlying graphics toolkit Tkinter. So it?s easy to port turtle.py to different graphics toolkits/ libraries, simply by replacing this Tkinter base class with an appropriate different one. I?ve done two ports: Pygame & Jython If you're after bitmap, I'd suggest contacting Greg and asking about the Pygame port. His contact info can be found at the original site for the revised turtle module[2], hopefully it's still up to date. 1: us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle- PyCon2007.pdf 2: http://xturtle.rg16.at/download.html From tjreedy at udel.edu Thu Jul 16 20:55:54 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 16 Jul 2009 20:55:54 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Terry Reedy wrote: >> $ file tmp.ps >> tmp.ps: PostScript document text conforming DSC level 3.0, type EPS >> >> Try changing the file extension from .ps to .eps. > > I will. Thank you. I tried it. Unfortunately, OOo does not open it correctly. It just displays the first three lines of metadate - Title, Creator, Date -- as image text. Photoshop does read the image, and does an ok job of conversion once anti-aliasing is turned off. Hmmm. Crazy idea... in http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle-PyCon2007.pdf Gregor Lingl says that turtle.py has been ported to pygame and jython as back ends. It should be possible to instead send output to a file instead of an on-screen canvas. Run a program to screen. When output looks right, return with a different parameter to send to file. For instance, an OpenDocumentDrawing file (.odd) using odfpy. This assumes that there are elements corresponding to each turtle command. Or at least that the fit is close enough. Terry Jan Reedy From ronn.ross at gmail.com Thu Jul 16 20:57:03 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Thu, 16 Jul 2009 20:57:03 -0400 Subject: TypeError: unbound method Message-ID: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> Hello all, Created a python file that has a class and three methods. When I jump into interactive mode I import like so: from import Now when I try to use a method from the class: var1 = class.method() It give me this error: TypeError: unbound method must be called with instance as first argument (got int instance instead) I'm not sure what is going on event after consulting with google -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben+python at benfinney.id.au Thu Jul 16 21:01:49 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 11:01:49 +1000 Subject: Override a method but inherit the docstring Message-ID: <87r5wggm0y.fsf@benfinney.id.au> Howdy all, The following is a common idiom:: class FooGonk(object): def frobnicate(self): """ Frobnicate this gonk. """ basic_implementation(self.wobble) class BarGonk(FooGonk): def frobnicate(self): special_implementation(self.warble) The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly applicable to the ?BarGonk.frobnicate? method also. Yet in overriding the method, the original docstring is not associated with it. Ideally there would be a way to specify that the docstring should be inherited. The best I can come up with is:: class BarGonk(FooGonk): def frobnicate(self): special_implementation(self.warble) frobnicate.__doc__ = FooGonk.frobnicate.__doc__ but that violates DRY (the association between BarGonk and FooGonk is being repeated), puts the docstring assignment awkwardly after the end of the method instead of at the beginning where docstrings normally go, and reads poorly besides. What is the most Pythonic, DRY-adherent, and preferably least-ugly approach to override a method, but have the same docstring on both methods? -- \ ?Why, I'd horse-whip you if I had a horse.? ?Groucho Marx | `\ | _o__) | Ben Finney From clp2 at rebertia.com Thu Jul 16 21:08:45 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 18:08:45 -0700 Subject: TypeError: unbound method In-Reply-To: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> References: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> Message-ID: <50697b2c0907161808i3bcc55eej333ad83660897313@mail.gmail.com> On Thu, Jul 16, 2009 at 5:57 PM, Ronn Ross wrote: > Hello all, > > Created a python file that has a class and three methods. When I jump into > interactive mode I import like so: > > from import > > Now when I try to use a method from the class: > > var1 = class.method() > > It give me this error: > TypeError: unbound method must be called with > instance as first argument (got int instance instead) > > I'm not sure what is going on event after consulting with google You're trying to call an instance method on the class itself, which doesn't make sense. You need to first create an instance of the class to invoke the method on. i.e.: instance = TheClass(constuctor_arguments_here) var1 = instance.method() Cheers, Chris -- http://blog.rebertia.com From exarkun at divmod.com Thu Jul 16 21:13:34 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Thu, 16 Jul 2009 21:13:34 -0400 Subject: Override a method but inherit the docstring In-Reply-To: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <20090717011334.2543.775231474.divmod.quotient.2982@henry.divmod.com> On Fri, 17 Jul 2009 11:01:49 +1000, Ben Finney wrote: >Howdy all, > >The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > >The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >the method, the original docstring is not associated with it. > >Ideally there would be a way to specify that the docstring should be >inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > >but that violates DRY (the association between BarGonk and FooGonk is >being repeated), puts the docstring assignment awkwardly after the end >of the method instead of at the beginning where docstrings normally go, >and reads poorly besides. > >What is the most Pythonic, DRY-adherent, and preferably least-ugly >approach to override a method, but have the same docstring on both >methods? > How about this? class BarGonk(FooGonk): @inherit_docstring def frobnicate(self): special_implementation(self.warble) The implementation of "inherit_docstring" is left as an exercise for the reader (it's not utterly trivial, I admit, as "FooGonk" will not readily be at hand, but it is still possible). By the way, I don't think this is a particularly good idea. Presumably there is a reason your implementation is special. It would probably be better if this were reflected in the docstring somehow. Perhaps this idea is a better one: class BarGonk(FooGonk): @append_to_docstring def frobnicate(self): """ This implementation takes the warble into consideration. """ special_implementation(self.warble) With the result of BarGonk.frobnicate.__doc__ being set to: Frobnicate this gonk. This implementation takes the warble into consideration. Jean-Paul From nobody at nowhere.com Thu Jul 16 21:16:53 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 17 Jul 2009 02:16:53 +0100 Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> Message-ID: On Thu, 16 Jul 2009 14:59:22 -0700, Emile van Sebille wrote: >>>> If the question was "Why is there no 'or' operator ?", would "because >>>> A or B <=> not(not A and not B)" be a proper answer ? >>> Note that in Python A or B is in fact not equivalent to not(not A and >>> not B). >> >> Ah, but it *is* "equivalent"; it isn't "identical", but that's not the >> point. > > I'm not sure I'd call it equivalent. That depends upon what definition of "equivalent" you are using. Apart from all manner of vague, informal definitions, there is a formal definition: A relation "=" is an equivalence relation if the following hold for all x, y, and z: x = x x = y => y = x x = y and y = z => x = z An equivalence relation partitions its domain into equivalence classes, such that an element is "=" (equivalent) to every element in the same class, and not "=" to every element in every other class. This is a lesser notion of equality to "identity", which also requires that x = y => f(x) = f(y) for all f. Python's notion of boolean values treats x and y as equivalent if bool(x) == bool(y). On this basis, the result of "A or B" is *equivalent* to the result of "not(not A and not B)". If you use either result as a boolean value (e.g. the test of an "if" or "while" statement, as an operand to "and" or "or", etc), the effect is the same. The results aren't *identical*, as there exist ways to distinguish the two. As for the utility of this behaviour, returning an actual value rather than True/False allows the operators to be used as "gates". The term "gate" in digital logic follows from the axioms: 0 and x = 0 1 and x = x 0 or x = x 1 or x = 1 [0 = False, 1 = True] If you consider the left operand as the "control" and the right operand as the "data", the control determines whether or not the data can pass through the gate to the output (i.e. whether the gate is open or closed). In Python: and: False and 7 => False False and 0 => False True and 7 => 7 True and 0 => 0 or: False or 7 => 7 False or 0 => 0 True or 7 => True True or 0 => True From max at alcyone.com Thu Jul 16 21:18:56 2009 From: max at alcyone.com (Erik Max Francis) Date: Thu, 16 Jul 2009 18:18:56 -0700 Subject: Einstein summation notation (was: question of style) In-Reply-To: References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: Albert van der Horst wrote: > Einstein introduced the summation convention for indices that > are used twice. Leaving out summation signs is absolutely hideous, > but it has saved generations of physicists of loosing track (and > their minds.) There is a joke among mathematicians that if Einstein hadn't been remembered for a couple of other fairly contributions things in physics :-), he would have been remembered in mathematics for that convention, since it has saved everyone -- mathematicians included -- a lot of time and anguish. When using tensor calculus, there's even very easy ways to verify you have a valid tensor equation using it. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis If the sky should fall, hold up your hands. -- (a Spanish proverb) From mkhitrov at gmail.com Thu Jul 16 21:21:16 2009 From: mkhitrov at gmail.com (Maxim Khitrov) Date: Thu, 16 Jul 2009 21:21:16 -0400 Subject: Override a method but inherit the docstring In-Reply-To: <20090717011334.2543.775231474.divmod.quotient.2982@henry.divmod.com> References: <87r5wggm0y.fsf@benfinney.id.au> <20090717011334.2543.775231474.divmod.quotient.2982@henry.divmod.com> Message-ID: <26ddd1750907161821r1a5feeebr1c7c63e0130bf6e7@mail.gmail.com> On Thu, Jul 16, 2009 at 9:13 PM, Jean-Paul Calderone wrote: > On Fri, 17 Jul 2009 11:01:49 +1000, Ben Finney > wrote: >> >> Howdy all, >> >> The following is a common idiom:: >> >> ? class FooGonk(object): >> ? ? ? def frobnicate(self): >> ? ? ? ? ? """ Frobnicate this gonk. """ >> ? ? ? ? ? basic_implementation(self.wobble) >> >> ? class BarGonk(FooGonk): >> ? ? ? def frobnicate(self): >> ? ? ? ? ? special_implementation(self.warble) >> >> The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >> applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >> the method, the original docstring is not associated with it. >> >> Ideally there would be a way to specify that the docstring should be >> inherited. The best I can come up with is:: >> >> ? class BarGonk(FooGonk): >> ? ? ? def frobnicate(self): >> ? ? ? ? ? special_implementation(self.warble) >> ? ? ? frobnicate.__doc__ = FooGonk.frobnicate.__doc__ >> >> but that violates DRY (the association between BarGonk and FooGonk is >> being repeated), puts the docstring assignment awkwardly after the end >> of the method instead of at the beginning where docstrings normally go, >> and reads poorly besides. >> >> What is the most Pythonic, DRY-adherent, and preferably least-ugly >> approach to override a method, but have the same docstring on both >> methods? >> > > How about this? > > ? class BarGonk(FooGonk): > ? ? ? @inherit_docstring > ? ? ? def frobnicate(self): > ? ? ? ? ? special_implementation(self.warble) > > The implementation of "inherit_docstring" is left as an exercise for the > reader (it's not utterly trivial, I admit, as "FooGonk" will not readily > be at hand, but it is still possible). > > By the way, I don't think this is a particularly good idea. ?Presumably > there is a reason your implementation is special. ?It would probably be > better if this were reflected in the docstring somehow. ?Perhaps this > idea is a better one: > > ? class BarGonk(FooGonk): > ? ? ? @append_to_docstring > ? ? ? def frobnicate(self): > ? ? ? ? ? """ > ? ? ? ? ? This implementation takes the warble into consideration. > ? ? ? ? ? """ > ? ? ? ? ? special_implementation(self.warble) > > With the result of BarGonk.frobnicate.__doc__ being set to: > > > ? ?Frobnicate this gonk. > > ? ?This implementation takes the warble into consideration. Another way is to use a metaclass. Have its __new__ method loop through all attributes and compare those with what is already defined in bases. If you find a match, copy the __doc__ attribute. The advantage here is that it will work for all methods without any additional code, not counting the "__metaclass__ = ..." line. If you define a metaclass for the base, then no modifications are required for any subclasses. I do agree, however, that the best thing to do is to write a very short explanation for what the override is for. - Max From nobody at nowhere.com Thu Jul 16 21:23:41 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 17 Jul 2009 02:23:41 +0100 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: >> In Python 3 you can't decode strings because they are Unicode strings >> and it doesn't make sense to decode a Unicode string. You can only >> decode encoded things which are byte strings. So you are mixing up byte >> strings and Unicode strings. > > Then, how should I do it? > I read a byte string from sys.stdin which needs to converted to unicode > string for further processing. In 3.x, sys.stdin (stdout, stderr) are text streams, which means that they read and write Unicode strings, not byte strings. > I cannot just remove the decode statement and > proceed? > > This is it what it looks like: > > for line in sys.stdin: > line = line.decode('utf-8').strip() > if line == '': #do something here > elsif #do something here > > If I remove the decode statement, line == '' never gets true. Did you inadvertently remove the strip() as well? From rhodri at wildebst.demon.co.uk Thu Jul 16 21:31:43 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 17 Jul 2009 02:31:43 +0100 Subject: Override a method but inherit the docstring In-Reply-To: <87r5wggm0y.fsf@benfinney.id.au> References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: On Fri, 17 Jul 2009 02:01:49 +0100, Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > Ideally there would be a way to specify that the docstring should be > inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > > but that violates DRY (the association between BarGonk and FooGonk is > being repeated), Not really. Consider the case of BarGonk being a subclass of FooGonk and BazGonk; which docstring would you wish to inherit? > puts the docstring assignment awkwardly after the end > of the method instead of at the beginning where docstrings normally go, > and reads poorly besides. Sounds like a job for a decorator! (This is probably unbelievably ugly and unwise, since I don't use decorators at all often.) def copydoc(cls): def _fn(fn): if fn.__name__ in cls.__dict__: fn.__doc__ = cls.__dict__[fn.__name__].__doc__ return fn return _fn class BarGonk(FooGonk): @copydoc(FooGonk) def frobnicate(self): special_implementation(self.warble) -- Rhodri James *-* Wildebeest Herder to the Masses From ptmcg at austin.rr.com Thu Jul 16 21:35:05 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 16 Jul 2009 18:35:05 -0700 (PDT) Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> On Jul 16, 8:01?pm, Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > ? ? class FooGonk(object): > ? ? ? ? def frobnicate(self): > ? ? ? ? ? ? """ Frobnicate this gonk. """ > ? ? ? ? ? ? basic_implementation(self.wobble) > > ? ? class BarGonk(FooGonk): > ? ? ? ? def frobnicate(self): > ? ? ? ? ? ? special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > What is the most Pythonic, DRY-adherent, and preferably least-ugly > approach to override a method, but have the same docstring on both > methods? > Two ideas come to mind, the decorator way and the metaclass way. I am not a guru at either, but these two examples work: # the decorator way def inherit_docstring_from(cls): def docstring_inheriting_decorator(fn): fn.__doc__ = getattr(cls,fn.__name__).__doc__ return fn return docstring_inheriting_decorator class FooGonk(object): def frobnicate(self): """ Frobnicate this gonk. """ basic_implementation(self.wobble) class BarGonk(FooGonk): @inherit_docstring_from(FooGonk) def frobnicate(self): special_implementation(self.warble) bg = BarGonk() help(bg.frobnicate) Prints: Help on method frobnicate in module __main__: frobnicate(self) method of __main__.BarGonk instance Frobnicate this gonk. Using a decorator in this manner requires repeating the super class name. Perhaps there is a way to get the bases of BarGonk, but I don't think so, because at the time that the decorator is called, BarGonk is not yet fully defined. # The metaclass way from types import FunctionType class DocStringInheritor(type): def __new__(meta, classname, bases, classDict): newClassDict = {} for attributeName, attribute in classDict.items(): if type(attribute) == FunctionType: # look through bases for matching function by name for baseclass in bases: if hasattr(baseclass, attributeName): basefn = getattr(baseclass,attributeName) if basefn.__doc__: attribute.__doc__ = basefn.__doc__ break newClassDict[attributeName] = attribute return type.__new__(meta, classname, bases, newClassDict) class FooGonk2(object): def frobnicate(self): """ Frobnicate this gonk. """ basic_implementation(self.wobble) class BarGonk2(FooGonk2): __metaclass__ = DocStringInheritor def frobnicate(self): special_implementation(self.warble) bg = BarGonk2() help(bg.frobnicate) Prints: Help on method frobnicate in module __main__: frobnicate(self) method of __main__.BarGonk2 instance Frobnicate this gonk. This metaclass will walk the list of bases until the desired superclass method is found AND if that method has a docstring and only THEN does it attach the superdocstring to the derived class method. Please use carefully, I just did the metaclass thing by following Michael Foord's Metaclass tutorial (http://www.voidspace.org.uk/python/ articles/metaclasses.shtml), I may have missed a step or two. -- Paul From vinay_sajip at yahoo.co.uk Thu Jul 16 21:56:51 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Thu, 16 Jul 2009 18:56:51 -0700 (PDT) Subject: ANN: gnupg v0.2.0 released Message-ID: A new version of the Python module which wraps GnuPG has been released. What Changed? ============= The module was refactored slightly to support Python 3.0. The current version now passes all tests on Windows (Python 2.4, 2.5) and Ubuntu (Python 2.4, 2.5, 2.6 and 3.0). What Does It Do? ================ The gnupg module allows Python programs to make use of the functionality provided by the Gnu Privacy Guard (abbreviated GPG or GnuPG). Using this module, Python programs can encrypt and decrypt data, digitally sign documents and verify digital signatures, manage (generate, list and delete) encryption keys, using proven Public Key Infrastructure (PKI) encryption technology based on OpenPGP. This module is expected to be used with Python versions >= 2.4, as it makes use of the subprocess module which appeared in that version of Python. This module is a newer version derived from earlier work by Andrew Kuchling, Richard Jones and Steve Traugott. A test suite using unittest is included with the source distribution. Simple usage: >>> import gnupg >>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory') >>> gpg.list_keys() [{ ... 'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2', 'keyid': '197D5DAC68F1AAB2', 'length': '1024', 'type': 'pub', 'uids': ['', 'Gary Gross (A test user) ']}, { ... 'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A', 'keyid': '0C5FEFA7A921FC4A', 'length': '1024', ... 'uids': ['', 'Danny Davis (A test user) ']}] >>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A']) >>> str(encrypted) '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n \nhQIOA/6NHMDTXUwcEAf ... -----END PGP MESSAGE-----\n' >>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret') >>> str(decrypted) 'Hello, world!' >>> signed = gpg.sign("Goodbye, world!", passphrase='secret') >>> verified = verified = gpg.verify(str(signed)) >>> print "Verified" if verified else "Not verified" 'Verified' For more information, visit http://code.google.com/p/python-gnupg/ - as always, your feedback is most welcome (especially bug reports, patches and suggestions for improvement). Enjoy! Cheers Vinay Sajip Red Dove Consultants Ltd. From tismer at stackless.com Thu Jul 16 22:26:02 2009 From: tismer at stackless.com (Christian Tismer) Date: Fri, 17 Jul 2009 04:26:02 +0200 Subject: ANN: psyco V2 Message-ID: <4A5FE13A.3080708@stackless.com> Announcing Psyco V2 source release ---------------------------------- This is the long awaited announcement of Psyco V2. Psyco V2 is a continuation of the well-known psyco project, which was called finished and was dis-continued by its author Armin Rigo in 2005, in favor of the PyPy project. This is a new project, using Psyco's code base with permission of Armin. Questions aqnd complaints should go to me (tismer at stackless.com) or the mailing list (psyco-devel at lists.sourceforge.net); Armin is explicitly not in charge of (t)his project any longer! As one of the founders and an active member of the PyPy project, I was very happy to be invited to work on Psyco V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco approach "an extension module that just makes Python faster". Psyco is a just-in-time compiler that accelerates arbitrary Python code by specialization. We believe that Psyco's approach can be carried out much further than it was tried so far, when it's first version was abandoned. This first V2 release is source-only. There is no web-site, yet, and there are no binaries for download. These will be available in a few days on http://www.psyco.org . For the time being, please stick with subversion access, building the extension module from source code. The repository is here: http://codespeak.net/svn/psyco/v2/dist Check-out the repository, and run the setup.py script, given that you have access to a C compiler. Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, and Mac OS X. Psyco is not supporting 64 bit, yet. But it is well being considered. The current improvements are, shortly: - Support for Python 2.4, 2.5 and 2.6 - a lot of new builtins - generators, fast and fully supported. More information is coming soon on http://www.psyco.org . This is the beginning of a series of new Psyco versions. Many more improvements are prepared and about to be published, soon, starting with the current version 2.0.0 . Stay tuned, this is just the beginning of psyco's re-birth! For questions about Psyco V2, please join the mailing list psyco-devel at lists.sourceforge.net or contact me on IRC: #psyco on irc.freenode.net . Psyco V2 is fundamentally supported by FATTOC, LLC. See http://www.fattoc.com . Without their continuous support, this work would not have been possible at all. I wish to express my deepest thanks to FATTOC, for allowing me to continue on Psyco with all the energy that this ambitious project needs, and will need. Further special thanks are going to Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, Raymond Hettinger, Fabrizio Milo, Michael Foord, Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, for all the support and discussions. Looking forward to a great future of Psyco! July 17, 2009 -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From tismer at stackless.com Thu Jul 16 22:48:18 2009 From: tismer at stackless.com (Christian Tismer) Date: Fri, 17 Jul 2009 04:48:18 +0200 Subject: ANN: psyco V2 Message-ID: <4A5FE672.3000401@stackless.com> Announcing Psyco V2 source release ---------------------------------- This is the long awaited announcement of Psyco V2. Psyco V2 is a continuation of the well-known psyco project, which was called finished and was dis-continued by its author Armin Rigo in 2005, in favor of the PyPy project. This is a new project, using Psyco's code base with permission of Armin. Questions and complaints should go to me (tismer at stackless.com) or the mailing list (psyco-devel at lists.sourceforge.net); Armin is explicitly not in charge of (t)his project any longer! As one of the founders and an active member of the PyPy project, I was very happy to be invited to work on Psyco V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco approach "an extension module that just makes Python faster". Psyco is a just-in-time compiler that accelerates arbitrary Python code by specialization. We believe that Psyco's approach can be carried out much further than it was tried so far, when it's first version was abandoned. This first V2 release is source-only. There is no web-site, yet, and there are no binaries for download. These will be available in a few days on http://www.psyco.org . For the time being, please stick with subversion access, building the extension module from source code. The repository is here: http://codespeak.net/svn/psyco/v2/dist Check-out the repository, and run the setup.py script, given that you have access to a C compiler. Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, and Mac OS X. Psyco is not supporting 64 bit, yet. But it is well being considered. The current improvements are, shortly: - Support for Python 2.4, 2.5 and 2.6 - a lot of new builtins - generators, fast and fully supported. More information is coming soon on http://www.psyco.org . This is the beginning of a series of new Psyco versions. Many more improvements are prepared and about to be published, soon, starting with the current version 2.0.0 . Stay tuned, this is just the beginning of psyco's re-birth! For questions about Psyco V2, please join the mailing list psyco-devel at lists.sourceforge.net or contact me on IRC: #psyco on irc.freenode.net . Psyco V2 is fundamentally supported by FATTOC, LLC. See http://www.fattoc.com . Without their continuous support, this work would not have been possible at all. I wish to express my deepest thanks to FATTOC, for allowing me to continue on Psyco with all the energy that this ambitious project needs, and will need. Further special thanks are going to Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, Raymond Hettinger, Fabrizio Milo, Michael Foord, Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, for all the support and discussions. Looking forward to a great future of Psyco! July 17, 2009 -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From ben+python at benfinney.id.au Thu Jul 16 22:58:48 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 12:58:48 +1000 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> Message-ID: <87ljmogglz.fsf@benfinney.id.au> Paul McGuire writes: > Two ideas come to mind, the decorator way and the metaclass way. I am > not a guru at either, but these two examples work: I think the decorator idea is most attractive to me, since it can be applied per method. > # the decorator way > def inherit_docstring_from(cls): > def docstring_inheriting_decorator(fn): > fn.__doc__ = getattr(cls,fn.__name__).__doc__ > return fn > return docstring_inheriting_decorator That works, thank you. > Using a decorator in this manner requires repeating the super class > name. Perhaps there is a way to get the bases of BarGonk, but I don't > think so, because at the time that the decorator is called, BarGonk is > not yet fully defined. Yes, I tried a few different ways, but within the decorator it seems the function object is quite unaware of what class it is destined for. -- \ ?We are human only to the extent that our ideas remain humane.? | `\ ?_Breakfast of Champions_, Kurt Vonnegut | _o__) | Ben Finney From akhilanger at gmail.com Thu Jul 16 23:26:39 2009 From: akhilanger at gmail.com (akhil1988) Date: Thu, 16 Jul 2009 20:26:39 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24528030.post@talk.nabble.com> Well, you were write: unintentionally I removed strip(). But the problem does not ends here: I get this error now: File "./temp.py", line 488, in main() File "./temp.py", line 475, in main for line in sys.stdin: File "/usr/local/lib/python3.1/codecs.py", line 300, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid data for this line: ? --Akhil Nobody-38 wrote: > > On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: > >>> In Python 3 you can't decode strings because they are Unicode strings >>> and it doesn't make sense to decode a Unicode string. You can only >>> decode encoded things which are byte strings. So you are mixing up byte >>> strings and Unicode strings. >> >> Then, how should I do it? >> I read a byte string from sys.stdin which needs to converted to unicode >> string for further processing. > > In 3.x, sys.stdin (stdout, stderr) are text streams, which means that they > read and write Unicode strings, not byte strings. > >> I cannot just remove the decode statement and >> proceed? >> >> This is it what it looks like: >> >> for line in sys.stdin: >> line = line.decode('utf-8').strip() >> if line == '': #do something here >> elsif #do something here >> >> If I remove the decode statement, line == '' never gets true. > > Did you inadvertently remove the strip() as well? > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24528030.html Sent from the Python - python-list mailing list archive at Nabble.com. From aahz at pythoncraft.com Thu Jul 16 23:39:39 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 20:39:39 -0700 Subject: ImportError: No module named _functools References: <45228cf4-0b37-4c52-bf6f-d7bd124b0f82@l32g2000vbp.googlegroups.com> Message-ID: In article , Tony Lay wrote: >On Jul 16, 10:14=A0am, a... at pythoncraft.com (Aahz) wrote: >> In article <45228cf4-0b37-4c52-bf6f-d7bd124b0... at l32g2000vbp.googlegroups= >.com>, >> Tony =A0Lay =A0 wrote: >>> >>>Traceback (most recent call last): >>> =A0File "/usr/local/bin/meld", line 35, in >>> =A0 =A0import gettext >>> =A0File "/usr/local/lib/python2.6/gettext.py", line 49, in >>> =A0 =A0import locale, copy, os, re, struct, sys >>> =A0File "/usr/local/lib/python2.6/locale.py", line 15, in >>> =A0 =A0import functools >>> =A0File "/usr/local/lib/python2.6/functools.py", line 10, in >>> =A0 =A0from _functools import partial, reduce >>>ImportError: No module named _functools >> >> Where is _functools.so? > >/usr/local/lib/python2.6/lib-dynload/ > >It's in the path... >export PYTHONHOME=3D/usr/local/lib/python2.6 >export PYTHONPATH=3D/usr/local/lib/python2.6:/usr/local/lib/python2.6/ >site-packages:/usr/local/lib/python2.6/lib-dynload:/usr/local/etc/ What's in sys.path? You might also get somewhere with "python -vv". -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From aurora00 at gmail.com Thu Jul 16 23:55:11 2009 From: aurora00 at gmail.com (aurora00 at gmail.com) Date: Thu, 16 Jul 2009 20:55:11 -0700 (PDT) Subject: ctype performance benchmark Message-ID: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> I have done some performance benchmarking for Python's ctypes library. I think you may find it interesting. I am planning to use ctypes as an alternative to writing C extension module for performance enhancement. Therefore my use case is slight different from the typical use case for accessing existing third party C libraries. In this case I am both the user and the implementer of the C library. In order to determine what is the right granularity for context switching between Python and C, I have done some benchmarking. I mainly want to measure the function call overhead. So the test functions are trivial function like returning the first character of a string. I compare a pure Python function versus C module function versus ctypes function. The tests are ran under Python 2.6 on Windows XP with Intel 2.33Ghz Core Duo. First of all I want to compare the function to get the first character of a string. The most basic case is to reference it as the 0th element of a sequence without calling any function. The produce the fastest result at 0.0659 usec per loop. $ timeit "'abc'[0]" 10000000 loops, best of 3: 0.0659 usec per loop As soon as I build a function around it, the cost goes up substantially. Both pure Python and C extension method shows similar performance at around 0.5 usec. ctypes function takes about 2.5 times as long at 1.37 usec. $ timeit -s "f=lambda s: s[0]" "f('abc')" 1000000 loops, best of 3: 0.506 usec per loop $ timeit -s "import mylib" "mylib.py_first('abc')" 1000000 loops, best of 3: 0.545 usec per loop $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')" "dll.first('abc')" 1000000 loops, best of 3: 1.37 usec per loop I repeated the test with a long string (1MB). There are not much difference in performance. So I can be quite confident that the parameter is passed by reference (of the internal buffer). $ timeit -s "f=lambda s: s[0]; lstr='abcde'*200000" "f(lstr)" 1000000 loops, best of 3: 0.465 usec per loop $ timeit -s "import mylib; lstr='abcde'*200000" "mylib.py_first(lstr)" 1000000 loops, best of 3: 0.539 usec per loop $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')" -s "lstr='abcde'*200000" "dll.first(lstr)" 1000000 loops, best of 3: 1.4 usec per loop Next I have make some attempts to speed up ctypes performance. A measurable improvement can be attained by eliminating the attribute look up for the function. Curiously this shows no improvement in the similar case for C extension. $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd'); -s "f=dll.first" "f('abcde')" 1000000 loops, best of 3: 1.18 usec per loop Secondary I have tried to specify the ctypes function prototype. This actually decrease the performance significantly. $ timeit -s "import ctypes; dll = ctypes.CDLL('mylib.pyd')" -s "f=dll.first" -s "f.argtypes=[ctypes.c_char_p]" -s "f.restype=ctypes.c_int" "f('abcde')" 1000000 loops, best of 3: 1.57 usec per loop Finally I have tested passing multiple parameters into the function. One of the parameter is passed by reference in order to return a value. Performance decrease as the number of parameter increase. $ timeit -s "charAt = lambda s, size, pos: s[pos]" -s "s='this is a test'" "charAt(s, len(s), 1)" 1000000 loops, best of 3: 0.758 usec per loop $ timeit -s "import mylib; s='this is a test'" "mylib.py_charAt(s, len(s), 1)" 1000000 loops, best of 3: 0.929 usec per loop $ timeit -s "import ctypes" -s "dll = ctypes.CDLL('mylib.pyd')" -s "s='this is a test'" -s "ch = ctypes.c_char()" "dll.charAt(s, len(s), 1, ctypes.byref(ch))" 100000 loops, best of 3: 2.5 usec per loop One style of coding that improve the performance somewhat is to build a C struct to hold all the parameters. $ timeit -s "from test_mylib import dll, charAt_param" -s "s='this is a test'" -s "obj = charAt_param(s=s, size=len(s), pos=3, ch='')" "dll.charAt_struct(obj)" 1000000 loops, best of 3: 1.71 usec per loop This may work because most of the fields in the charAt_param struct are invariant in the loop. Having them in the same struct object save them from getting rebuilt each time. My overall observation is that ctypes function has an overhead that is 2 to 3 times to a similar C extension function. This may become a limiting factor if the function calls are fine grained. Using ctypes for performance enhancement is a lot more productive if the interface can be made to medium or coarse grained. A snapshot of the source code used for testing is available for download http://tungwaiyip.info/blog/2009/07/16/ctype_performance_benchmark Wai Yip Tung From aurora00 at gmail.com Fri Jul 17 00:03:14 2009 From: aurora00 at gmail.com (Wai Yip) Date: Thu, 16 Jul 2009 21:03:14 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: I think Diez' example show this work automatically in Unix. In my case I use Windows. I use the multiprocessing.Array to share data in shared memory. multiprocessing.Array has a limitation that it can only reference simple C data types, not Python objects though. Wai Yip Tung From crazypangolin at gmail.com Fri Jul 17 00:44:26 2009 From: crazypangolin at gmail.com (aj) Date: Thu, 16 Jul 2009 21:44:26 -0700 (PDT) Subject: error when compiling source on linux Message-ID: <1c8d7530-873b-4f52-8d0f-21a6b79de339@p9g2000vbl.googlegroups.com> when I try to compile Python 2.6 from source code on ubuntu, I get the message /usr/bin/ld: cannot open output file python: Is a directory collect2: ld returned 1 exit status make: *** [python] Error 1 PLEASE HELP! From aahz at pythoncraft.com Fri Jul 17 01:08:31 2009 From: aahz at pythoncraft.com (Aahz) Date: 16 Jul 2009 22:08:31 -0700 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: In article , Gabriel Rossetti wrote: > >I am using threading.Condition.wait(timeout) and was surprised to see >that there is no return value nor an exception when wait() is used w/ a >timeout. How am I supposed to know if it was notified or if it timed out? What are you trying to do? Maybe your purpose would be better suited to using a Queue? (Yes, Queue is a hammer in search of a nail, but most threading problems are easier if you treat them as nails.) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From alan.isaac at gmail.com Fri Jul 17 01:19:50 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Fri, 17 Jul 2009 05:19:50 GMT Subject: invoke method on many instances Message-ID: As a recurrent situation, I need to invoke the same method on many instances. Speed matters, but the solution should be pure Python. Is the following convenience function a reasonable approach? def apply2(itr, methodname, *args, **kwargs): f = operator.methodcaller(methodname, *args, **kwargs) for item in itr: f(item) (Comment: in the case at hand, the methods return None.) Thank you, Alan Isaac From contact at xavierho.com Fri Jul 17 01:31:28 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 13:31:28 +0800 Subject: Try... except....Try again? Message-ID: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> I have a simple class that generates prime numbers, using memorisation and iteration to generate the next prime number. In the class, I have defined a function that gives, say, the 5th prime number. Or the 1000th, it's still very fast. But the code isn't what I really like. def nPrime(self, n): "Returns nth prime number, the first one being 2, where n = 0. When n = 1, it returns 3." while True: try: return self.primes[n] except: self.next() The next() function generates the next prime, and appends it into primes. This way, it keeps trying to append until the nth prime requested exist, and returns it. My problem is that it's a "While True" loop, which I get a lot of "Don't do it!" In the light of making the code better, what could I do? Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From gabriel.rossetti at arimaz.com Fri Jul 17 02:12:38 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 17 Jul 2009 08:12:38 +0200 Subject: no return value for threading.Condition.wait(timeout)? In-Reply-To: <26ddd1750907161422n5b640a48pbbf9325dd05a699d@mail.gmail.com> References: <26ddd1750907161422n5b640a48pbbf9325dd05a699d@mail.gmail.com> Message-ID: <4A601656.1070802@arimaz.com> Maxim Khitrov wrote: > On Thu, Jul 16, 2009 at 5:00 PM, Carl Banks wrote: > >> On Jul 16, 8:12 am, Gabriel Rossetti >> wrote: >> >>> Hello everyone, >>> >>> I am using threading.Condition.wait(timeout) and was surprised to see >>> that there is no return value nor an exception when wait() is used w/ a >>> timeout. How am I supposed to know if it was notified or if it timed out? >>> >> That's a good question. Condition.wait seems to violate an invariant >> if it can return without having acquired the underlying lock. And if >> it does the least it could do is to return a status so you know if you >> have to skeddadle. >> >> Workaround is to use the _is_owned() method of Condition to see if you >> are the owner of the condition, but this method is undocumented. >> >> cond = Condition() >> ... >> cond.acquire() >> while not ok_to_proceed(): >> cond.wait() >> if not cond._is_owned(): >> # must've timed out >> raise TimeOutException >> operate() >> cond.release() >> > > You will always be the owner of the condition whether it times out or > your thread is notified. This doesn't solve the problem. As an aside, > the implementation of wait() is rather poor in any case; the sleep > function should not be used for the purpose of waiting for an event. > > Depending on what platform you are using, I can offer two solutions. > The simplest one is to subclass Condition and override the wait > method. Just copy the code as it is, but modify the end of the > function to look like this: > > if not gotit: > if __debug__: > self._note("%s.wait(%s): timed out", self, timeout) > try: > self.__waiters.remove(waiter) > except ValueError: > pass > return False > else: > if __debug__: > self._note("%s.wait(%s): got it", self, timeout) > return True > > The two return statements will tell you whether you timed out ("if not > gotit" condition), or were notified ("else"). > > The better way to solve this and the other problem that I mentioned is > to use native OS events. This is what I had to do for a recent > project. I used pywin32 extension to completely rewrite Event, > Condition, and Timer classes to use a true event-based approach. In > the process of the rewrite I also modified some functionality, such as > returning True of False from wait(). > > Unfortunately, I'm not able to post this code right now, though I can > try and get permission to do so a bit later. If you are writing > software for Windows, then simply take a look at win32event module. > CreateEvent, SetEvent, ResetEvent, WaitForSingleObject are all the > Windows functions that you need to reimplement threading.Event. Using > the new event class you can implement a proper Condition.wait() method > (hint: use new events rather than locks to wait for notification). > > If you are on Linux or BSD, I can't help you. I'm sure that there is > some equivalent functionality that you may be able to access using > ctypes. Exactly how to do it, however, is not something that I can > help you with right now. > > - Max > Thank you, I will try that. I am coding for Linux, Mac & Windows (and any POSIX platform, but right now I support only those three). Gabriel From koranthala at gmail.com Fri Jul 17 02:13:51 2009 From: koranthala at gmail.com (koranthala) Date: Thu, 16 Jul 2009 23:13:51 -0700 (PDT) Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> Message-ID: <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> > That test was designed to treat None as a boolean False, without > noticing that numeric 0 is also treated as False and could make the > test do the wrong thing. This is an extremely common type of error. Actually, I felt that 0 not being considered False would be a better option. I had lot of instances where 0 is a valid value and always I had to put checks specifically for that. For me, None and False should be equal to False in if checks, every thing else being considered True. Can someone let me know why 0 is considered equal to False? There should be real good reasons for it, only that I cannot think of one. From gabriel.rossetti at arimaz.com Fri Jul 17 02:25:23 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 17 Jul 2009 08:25:23 +0200 Subject: no return value for threading.Condition.wait(timeout)? In-Reply-To: References: Message-ID: <4A601953.5000603@arimaz.com> Piet van Oostrum wrote: >>>>>> Gabriel Rossetti (GR) wrote: >>>>>> > > >> GR> Hello everyone, >> GR> I am using threading.Condition.wait(timeout) and was surprised to see that >> GR> there is no return value nor an exception when wait() is used w/ a timeout. >> GR> How am I supposed to know if it was notified or if it timed out? >> > > Normally you wouldn't have to know. The logic of your program should be > such that you wait until a certain condition is satisfied. After each > wait you usually check that condition anyway, like: > > while (some_condition_satisfied): > cond_var.wait() > > The only exception could be if there is a 1-1 relation between a single > waiter and a single notifier. I that case you can usually use if instead > of while (but it does depend of the logic of the notifier). > I have a 1-1 relation, I have a thread reading msgs from a network socket and a method waiting for an answer to arrive (see my thread "Threading.Condition problem"). I would like to be able to have a timeout as to not block forever, so the idea is to check if I returned because of a timeout or not. > So in case of using a timeout you can check the desired condition after > the wait and decide what to do. If the condition was not satisfied you > probably want to do something special, like error handling, or do > something else first and then retry the wait. > > There could be situations where this doesn't work, however. For example > if you wait until a queue is not empty, and the notify is done when > something is inserted in the queue, another thread may sneak in while > you are waiting and snatch the inserted item just before your thread > continues after the wait. In that case you can't distinguish between a > timeout and a snatcher. > > So it all depends on what your use case is. > > (Java's wait doesn't return anything either.) > From __peter__ at web.de Fri Jul 17 02:28:46 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 17 Jul 2009 08:28:46 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Terry Reedy wrote: > Terry Reedy wrote: > >>> $ file tmp.ps >>> tmp.ps: PostScript document text conforming DSC level 3.0, type EPS >>> >>> Try changing the file extension from .ps to .eps. >> >> I will. Thank you. > > I tried it. Unfortunately, OOo does not open it correctly. It just > displays the first three lines of metadate - Title, Creator, Date -- as > image text. Photoshop does read the image, and does an ok job of > conversion once anti-aliasing is turned off. I snatched some code from the module and modified it to save a sample image: from turtle import * def switchpen(): if isdown(): pu() else: pd() def demo2(): """Demo of some new features.""" speed(1) st() pensize(3) setheading(towards(0, 0)) radius = distance(0, 0)/2.0 rt(90) for _ in range(18): switchpen() circle(radius, 10) write("wait a moment...") while undobufferentries(): undo() reset() lt(90) colormode(255) laenge = 10 pencolor("green") pensize(3) lt(180) for i in range(-2, 16): if i > 0: begin_fill() fillcolor(255-15*i, 0, 15*i) for _ in range(3): fd(laenge) lt(120) laenge += 10 lt(15) speed((speed()+1)%12) end_fill() lt(120) pu() fd(70) rt(30) pd() color("red","yellow") speed(0) fill(1) for _ in range(4): circle(50, 90) rt(90) fd(30) rt(90) fill(0) lt(90) pu() fd(30) pd() shape("turtle") tri = getturtle() tri.resizemode("auto") turtle = Turtle() turtle.resizemode("auto") turtle.shape("turtle") turtle.reset() turtle.left(90) turtle.speed(0) turtle.up() turtle.goto(280, 40) turtle.lt(30) turtle.down() turtle.speed(6) turtle.color("blue","orange") turtle.pensize(2) tri.speed(6) setheading(towards(turtle)) count = 1 while tri.distance(turtle) > 4: turtle.fd(3.5) turtle.lt(0.6) tri.setheading(tri.towards(turtle)) tri.fd(4) if count % 20 == 0: turtle.stamp() tri.stamp() switchpen() count += 1 tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right") if __name__ == "__main__": demo2() Screen().getcanvas().postscript(file="demo2.eps") I could successfully insert the picture into Writer. I'm on Kubuntu 9.04 with Python 2.6.2 and OpenOffice 3.0.1. > Hmmm. Crazy idea... in > http://us.pycon.org/media/2009/talkdata/PyCon2009/065/SevenWaysToUseTurtle- PyCon2007.pdf > Gregor Lingl says that turtle.py has been ported to pygame and jython as > back ends. It should be possible to instead send output to a file > instead of an on-screen canvas. Run a program to screen. When output > looks right, return with a different parameter to send to file. > > For instance, an OpenDocumentDrawing file (.odd) using odfpy. > This assumes that there are elements corresponding to each turtle > command. Or at least that the fit is close enough. I can see nothing crazy about that. But still, turtle is an educational tool, its main advantage is that it can show beginners how the image is generated. If you want to generate high-quality graphics easily you need different primitives. http://cairographics.org has Python bindings, but I don't know if it's available on Windows. Peter From clp2 at rebertia.com Fri Jul 17 02:30:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 16 Jul 2009 23:30:39 -0700 Subject: Einstein summation notation (was: question of style) In-Reply-To: <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <50697b2c0907162330m60327179j6f7fcf0427beac3c@mail.gmail.com> On Thu, Jul 16, 2009 at 11:13 PM, koranthala wrote: >> That test was designed to treat None as a boolean False, without >> noticing that numeric 0 is also treated as False and could make the >> test do the wrong thing. ?This is an extremely common type of error. > > Actually, I felt that 0 not being considered False would be a better > option. > I had lot of instances where 0 is a valid value and always I had to > put checks specifically for that. > For me, None and False should be equal to False in if checks, every > thing else being considered True. > > Can someone let me know why 0 is considered equal to False? > There should be real good reasons for it, only that I cannot think of > one. * Because that's how C does/did it (primary motivation) - Because if Boolean algebra is implemented using numbers, False is 0 - Because philosophically, 0 is the "empty" or null value for numbers, and empty values are by convention considered false in Python - Because it's the behavior people want 75% of the time. FWIW, your paradigm of true/false is used by Lisp and Lisp wannabes such as Ruby. Cheers, Chris -- http://blog.rebertia.com From __peter__ at web.de Fri Jul 17 02:58:40 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 17 Jul 2009 08:58:40 +0200 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > Ideally there would be a way to specify that the docstring should be > inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > > but that violates DRY (the association between BarGonk and FooGonk is > being repeated), puts the docstring assignment awkwardly after the end > of the method instead of at the beginning where docstrings normally go, > and reads poorly besides. > > What is the most Pythonic, DRY-adherent, and preferably least-ugly > approach to override a method, but have the same docstring on both > methods? Just thinking aloud: Write a patch for pydoc that looks up the base-class documentation. B.f.__doc__ will continue to return None, but help(B.f) will show something like No documentation available for B.f. Help for A.f: yadda yadda Of course that might be misleading when A.f and B.f are up to something completely different... Peter From nobody at nowhere.com Fri Jul 17 03:20:15 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 17 Jul 2009 08:20:15 +0100 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: On Thu, 16 Jul 2009 20:26:39 -0700, akhil1988 wrote: > Well, you were write: unintentionally I removed strip(). But the problem does > not ends here: > > I get this error now: > > File "./temp.py", line 488, in > main() > File "./temp.py", line 475, in main > for line in sys.stdin: > File "/usr/local/lib/python3.1/codecs.py", line 300, in decode > (result, consumed) = self._buffer_decode(data, self.errors, final) > UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid > data > > for this line: > ? Right. You're running in a locale whose encoding is UTF-8, but feeding data which isn't valid UTF-8 to stdin. If you want to use data with a different encoding, you need to replace sys.stdin, e.g.: import sys import io sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding = 'iso-8859-1') From Lacrima.Maxim at gmail.com Fri Jul 17 03:24:48 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Fri, 17 Jul 2009 00:24:48 -0700 (PDT) Subject: urllib with x509 certs References: <19cb6bd9-b9cc-489e-8983-5c0ab08fae71@b15g2000yqd.googlegroups.com> <4a4f2307$0$8384$9b622d9e@news.freenet.de> <4A4F387F.7050804@v.loewis.de> Message-ID: Hello! I've solved this problem, using pyCurl. Here is sample code. import pycurl import StringIO b = StringIO.StringIO() c = pycurl.Curl() url = 'https://example.com/' c.setopt(pycurl.URL, url) c.setopt(pycurl.WRITEFUNCTION, b.write) c.setopt(pycurl.CAINFO, 'cert.crt') c.setopt(pycurl.SSLKEY, 'mykey.key') c.setopt(pycurl.SSLCERT, 'mycert.cer') c.setopt(pycurl.SSLKEYPASSWD , 'pass phrase') c.perform() This also allow to specify CA, so your requests are more secure then with urllib. With regards, Max. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:28:32 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:28:32 GMT Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <02701951$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 23:13:51 -0700, koranthala wrote: >> That test was designed to treat None as a boolean False, without >> noticing that numeric 0 is also treated as False and could make the >> test do the wrong thing. This is an extremely common type of error. > > Actually, I felt that 0 not being considered False would be a better > option. > I had lot of instances where 0 is a valid value and always I had to put > checks specifically for that. > For me, None and False should be equal to False in if checks, every > thing else being considered True. I believe it would be a mistake for None == False to return True. As far as having only None and False to be considered equivalent in truth contexts, that's awfully limiting. It is very useful to be able to write e.g.: if header or body or footer: print assemble_page(header, body, footer) and have empty strings to be equivalent to False. Lisp and (I think) Ruby behaves as you want. Python doesn't. We think it's Lisp and Ruby that got it wrong :) > Can someone let me know why 0 is considered equal to False? There should > be real good reasons for it, only that I cannot think of one. Having 0 considered false goes back to branching in machine language, which I understand are typically. The Null value (e.g. nil in Pascal, nul in C) are usually implemented as pointer values equal to an address of zero. It essentially boils down to this: the behaviour of if...else is a convention. The language designer might choose to have an explicit Boolean type, and prohibit "if None" (e.g. Pascal does this). It might choose to treat flags as integers, and branch according to the integer being zero (false) or non-zero (true) (e.g. Forth does this). Python allows any object to be tested by if, and branches according to whether it is Something or Nothing. It's a logical dichotomy, and treating 0 and [] as Something instead of Nothing would make it arbitrary and strange. -- Steven From http Fri Jul 17 03:34:00 2009 From: http (Paul Rubin) Date: 17 Jul 2009 00:34:00 -0700 Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> Message-ID: <7xljmnahlj.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > It is very useful to be able to write e.g.: > if header or body or footer: > print assemble_page(header, body, footer) > and have empty strings to be equivalent to False. Why doesn't assemble_page properly handle the case where header, body, and footer are all empty? That would let you eliminate the if. "Make sure your code 'does nothing' gracefully" (Kernighan and Plauger). From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:45:08 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:45:08 GMT Subject: invoke method on many instances References: Message-ID: <02701d35$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: > As a recurrent situation, I need to invoke the same method on many > instances. Speed matters, but the solution should be pure Python. Is > the following convenience function a reasonable approach? > > def apply2(itr, methodname, *args, **kwargs): > f = operator.methodcaller(methodname, *args, **kwargs) > for item in itr: > f(item) I don't particularly like your naming conventions, but never mind. You could also try this: for obj in objects: getattr(obj, methodname)(*args, **kwargs) See also these recipes from the "Python Cookbook": http://code.activestate.com/recipes/52289/ http://code.activestate.com/recipes/87370/ -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:52:19 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:52:19 GMT Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> <87ljmogglz.fsf@benfinney.id.au> Message-ID: <02701ee5$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 12:58:48 +1000, Ben Finney wrote: >> Using a decorator in this manner requires repeating the super class >> name. Perhaps there is a way to get the bases of BarGonk, but I don't >> think so, because at the time that the decorator is called, BarGonk is >> not yet fully defined. > > Yes, I tried a few different ways, but within the decorator it seems the > function object is quite unaware of what class it is destined for. When the decorator is called, the function object is just a function object, not a method, so there is no concept of "what class it is destined for". >>> def dec(func): ... print type(func) ... try: ... print func.im_class ... except: ... print "no im_class" ... return func ... >>> class Test(object): ... @dec ... def spam(self): ... pass ... no im_class >>> type(Test.spam) >>> Test.spam.im_class I suppose you could try to determine what namespace you're currently when the class is created, but that's surely going to be fragile and messy. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 03:56:07 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 07:56:07 GMT Subject: TypeError: unbound method References: <9c8c445f0907161757u38925c8w889807f42278122f@mail.gmail.com> Message-ID: <02701fc9$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 18:08:45 -0700, Chris Rebert wrote: > You're trying to call an instance method on the class itself, which > doesn't make sense. Oh I don't know, people do it all the time -- just not the way the OP did :) > You need to first create an instance of the class to invoke the method > on. i.e.: > > instance = TheClass(constuctor_arguments_here) > var1 = instance.method() Or explicitly pass an instance to the method when calling from the class: TheClass.method(TheClass(constuctor_arguments_here)) Which strangely enough, is useful at times. class K(parent): def method(self, args): x = parent.method(self, args) return x + 1 -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 04:06:22 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 08:06:22 GMT Subject: How to check if any item from a list of strings is in a big string? References: Message-ID: <0270222f$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 11:02:57 -0500, Pablo Torres N. wrote: > On Thu, Jul 9, 2009 at 22:07, Steven > D'Aprano wrote: >> On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote: >> >>> def list_items_in_string(list_items, string): >>> ? ? for item in list_items: >>> ? ? ? ? if item in string: >>> ? ? ? ? ? ? return True >>> ? ? return False >> ... >>> Any ideas how to make that function look nicer? :) >> >> Change the names. Reverse the order of the arguments. Add a docstring. >> >> > Why reverse the order of the arguments? Is there a design principle > there? It's just a convention. Before strings had methods, you used the string module, e.g.: string.find(source, target) => find target in source This became source.find(target). In your function: list_items_in_string(list_items, string) "list_items" is equivalent to target, and "string" is equivalent to source. It's conventional to write the source first. -- Steven From piet at cs.uu.nl Fri Jul 17 04:12:52 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:12:52 +0200 Subject: question of style References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: >>>>> koranthala (k) wrote: >>> That test was designed to treat None as a boolean False, without >>> noticing that numeric 0 is also treated as False and could make the >>> test do the wrong thing. This is an extremely common type of error. >k> Actually, I felt that 0 not being considered False would be a better >k> option. >k> I had lot of instances where 0 is a valid value and always I had to >k> put checks specifically for that. >k> For me, None and False should be equal to False in if checks, every >k> thing else being considered True. >k> Can someone let me know why 0 is considered equal to False? >k> There should be real good reasons for it, only that I cannot think of >k> one. Originally Python didn't have booleans. Integers were used instead (but not exclusively). 0 was False, everything else True (with a slight preference for 1 as True). Same as in C. Nowadays this is reflected in bool being a subtype of int with False == 0 and True == 1. Actually it is even closer: False and True are just 0 and 1 cast to bool, so to say. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From oisin.mulvihill at gmail.com Fri Jul 17 04:14:45 2009 From: oisin.mulvihill at gmail.com (Oisin Mulvihill) Date: Fri, 17 Jul 2009 09:14:45 +0100 Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... Message-ID: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> Hi There, I gave a lightning talk about my ServiceStation project at Europython 2009. While it isn't written in Python (C++/Windows), I did write it with python in mind. Its released under the CDDL license. I'm hoping it will be of use to other Python users who will need to run their services on windows, but don't want to learn much/anything about windows service programming. The ServiceStation blurb is: ServiceStation allows you to run arbitrary programs as a service on the Windows platform. The program you wish to run does not need to be changed to allow it to work with ServiceStation or windows services. This project was developed with an eye to running Python web services on Windows, without the need to use and include Pywin32. This meant we could take services running on Linux/Mac and run them unmodified on Windows." If your interested in this check out the projects trac page for docs and download http://www.foldingsoftware.com/servicestation All the best, Oisin From piet at cs.uu.nl Fri Jul 17 04:26:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:26:27 +0200 Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: There is stil something not clear in your description. >m> I'm using multiprocessing to spawn several subprocesses, each of which >m> uses a very large data structure (making it impractical to pass it via >m> pipes / pickling). I need to allocate this structure once when the >m> process is created and have it remain in memory for the duration of >m> the process. I have read this as that every subprocess has its own large data structure and that there is no connection between these. But seeing where the discussion is going I guess there might be different interpretations. So can you enlighten us how the situation is? 1. Each subprocess has a copy of a data structure that is prepared by the master process. Therefore you want it to be passed by the fork 1a. the data structure is constant i.e. the subprocess doesn't change it 1b. the subprocess makes changes in its copy 2. Each subprocess has a seperate data structure not equal to the others 3. Something else. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mail at timgolden.me.uk Fri Jul 17 04:34:27 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 17 Jul 2009 09:34:27 +0100 Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... In-Reply-To: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> References: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> Message-ID: <4A603793.3070803@timgolden.me.uk> Oisin Mulvihill wrote: > Hi There, > > I gave a lightning talk about my ServiceStation project at Europython > 2009. While it > isn't written in Python (C++/Windows), I did write it with python in > mind. Its > released under the CDDL license. > > I'm hoping it will be of use to other Python users who will need to run > their services > on windows, but don't want to learn much/anything about windows service > programming. > If your interested in this check out the projects trac page for docs and download > http://www.foldingsoftware.com/servicestation It's not clear at a glance how this differs from, say, the venerable SRVANY.EXE and a slew of other "Run-things-as-a-service" programs. Absolutely not wishing to be disparaging, but what is servicestation offering to distinguish itself? TJG From stefan_ml at behnel.de Fri Jul 17 04:35:54 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 17 Jul 2009 10:35:54 +0200 Subject: ctype performance benchmark In-Reply-To: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> Message-ID: <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> aurora00 at gmail.com wrote: > My overall observation is that ctypes function has an overhead that is > 2 to 3 times to a similar C extension function. This may become a > limiting factor if the function calls are fine grained. Using ctypes > for performance enhancement is a lot more productive if the interface > can be made to medium or coarse grained. I think ctypes is ok for its niche: easy interfacing with C stuff from straight Python code, without further dependencies. There will always (and necessarily) be a call overhead involved. Anyone who needs to care about per-call performance would use something else anyway (do I need to mention Cython here?) Stefan From piet at cs.uu.nl Fri Jul 17 04:37:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:37:27 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> I have a 1-1 relation, I have a thread reading msgs from a network socket >GR> and a method waiting for an answer to arrive (see my thread >GR> "Threading.Condition problem"). I would like to be able to have a timeout >GR> as to not block forever, so the idea is to check if I returned because of a >GR> timeout or not. So that case is easy I think. After the wait just check if the answer has arrived. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Fri Jul 17 04:47:36 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 10:47:36 +0200 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: >>>>> akhil1988 (a) wrote: >a> Well, you were write: unintentionally I removed strip(). But the problem does >a> not ends here: >a> I get this error now: >a> File "./temp.py", line 488, in >a> main() >a> File "./temp.py", line 475, in main >a> for line in sys.stdin: >a> File "/usr/local/lib/python3.1/codecs.py", line 300, in decode >a> (result, consumed) = self._buffer_decode(data, self.errors, final) >a> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid >a> data >a> for this line: >a> ? Your Python assumes stdin uses utf-8 encoding, probably because your locale says so. But it seems the input is not really utf-8 but some other encoding. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From oisin.mulvihill at gmail.com Fri Jul 17 05:13:49 2009 From: oisin.mulvihill at gmail.com (Oisin Mulvihill) Date: Fri, 17 Jul 2009 02:13:49 -0700 (PDT) Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... References: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> Message-ID: <42b910db-7e71-4f26-81ed-ee6a2f29ca56@26g2000yqk.googlegroups.com> On Jul 17, 9:34?am, Tim Golden wrote: > Oisin Mulvihill wrote: > > Hi There, > > > I gave a lightning talk about my ServiceStation project at Europython > > 2009. While it > > isn't written in Python (C++/Windows), I did write it with python in > > mind. Its > > released under the CDDL license. > > > I'm hoping it will be of use to other Python users who will need to run > > their services > > on windows, but don't want to learn much/anything about windows service > > programming. > > If your interested in this check out the projects trac page for docs and download > > ?http://www.foldingsoftware.com/servicestation > > It's not clear at a glance how this differs from, say, the > venerable SRVANY.EXE and a slew of other "Run-things-as-a-service" > programs. Absolutely not wishing to be disparaging, but what > is servicestation offering to distinguish itself? > > TJG Hi There, I have to admit I didn't come across srvany before. I may not have used the right google fu the day I decided to do this ;) So maybe it does all the following, however servicestation distingusihes itself by: * Properly tracking all child processes launched by the command it runs and closes them on stop/restart. * Monitoring the command its running and keeping it alive. * Allows you to set the description / name from the config file. * It logs useful information to the event viewer so you can see why it couldn't run the command under its care. * Can interact with the desktop or not so you can run programs with a gui but not actually see it. * Does not disconnect the service if you log-out as some runners do. Thats all I can think of. I should probably add this to this wiki too, Thanks for prompting me on this. Oisin From peter.fodrek at stuba.sk Fri Jul 17 05:24:59 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Fri, 17 Jul 2009 11:24:59 +0200 Subject: error when compiling source on linux In-Reply-To: <1c8d7530-873b-4f52-8d0f-21a6b79de339@p9g2000vbl.googlegroups.com> References: <1c8d7530-873b-4f52-8d0f-21a6b79de339@p9g2000vbl.googlegroups.com> Message-ID: <200907171124.59610.peter.fodrek@stuba.sk> On Friday 17 July 2009 06:44:26 aj wrote: > when I try to compile Python 2.6 from source code on ubuntu, I get the > message > /usr/bin/ld: cannot open output file python: Is a directory This says that there is directory in the sources with same name as will be named output It can only occur when you are not compiling software correctly via INSTALL or README file I think you made ./configure make as compiling process if this is true try mkdir bulid cd build ../configure make make install Kind regards Peter From mail at timgolden.me.uk Fri Jul 17 05:26:09 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 17 Jul 2009 10:26:09 +0100 Subject: ann: servicestation - run your linux/mac services on windows without needing pywin... In-Reply-To: <42b910db-7e71-4f26-81ed-ee6a2f29ca56@26g2000yqk.googlegroups.com> References: <11B9E5DD-3E9A-4284-AD26-07CBBFF03F02@gmail.com> <42b910db-7e71-4f26-81ed-ee6a2f29ca56@26g2000yqk.googlegroups.com> Message-ID: <4A6043B1.8010500@timgolden.me.uk> Oisin Mulvihill wrote: > I have to admit I didn't come across srvany before. It's been part of the Windows Resource Kit since way back, but like many such things is probably known more by word-of-mouth than by Internet Searchability > * Properly tracking all child processes launched by the command it > runs and closes them on stop/restart. > > * Monitoring the command its running and keeping it alive. > > * Allows you to set the description / name from the config file. > > * It logs useful information to the event viewer so you can see why > it couldn't run the command under its care. > > * Can interact with the desktop or not so you can run programs with a > gui but not actually see it. > > * Does not disconnect the service if you log-out as some runners do. Looks very interesting. Definitely put this on the front page, otherwise people may think as I did that this is just another srvany clone. TJG From hartley79 at gmail.com Fri Jul 17 05:59:46 2009 From: hartley79 at gmail.com (hartley) Date: Fri, 17 Jul 2009 02:59:46 -0700 (PDT) Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <768d13a5-e49c-4d43-8707-df19781e28ea@s15g2000yqs.googlegroups.com> <802e3a33-b4f8-4b7e-b14a-67229979f7e7@k6g2000yqn.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> Message-ID: <94c1adf3-a25d-4ae5-9f38-7ca8680d022d@b14g2000yqd.googlegroups.com> On Jul 16, 9:26?pm, a... at pythoncraft.com (Aahz) wrote: > In article <0afc5c4d-1af5-4d0e-9442-26b51b12e... at m11g2000yqh.googlegroups.com>, > > hartley ? wrote: > > >If you had loosened up on the sarcasm I would probably have read what > >you wrote more thoroughly instead of just skimming through it. Thanks > >for the help, but you should seriously consider doing something with > >your patronizing attitude. > > http://www.mikeash.com/getting_answers.htmlhttp://www.catb.org/~esr/faqs/smart-questions.html >From "http://www.mikeash.com/getting_answers.html": How To Ask Questions The Smart Way is pretty condescending, especially if you already feel insulted by somebody telling you that you need help asking questions. If you're pointed at a guide with a filename of smart-questions, that means this person thinks you have stupid questions, and who needs that? Cheers mate :) -Hartley From ben+python at benfinney.id.au Fri Jul 17 06:03:29 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 20:03:29 +1000 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <87zlb3fwy6.fsf@benfinney.id.au> Peter Otten <__peter__ at web.de> writes: > Just thinking aloud: Write a patch for pydoc that looks up the > base-class documentation. That doesn't scale; I want the docstring to be discovered by the normal interface (the ?__doc__? attribute) by *every* tool that gathers docstrings from methods. Also, I want to have this behaviour not for every method missing a docstring, but only for selected methods. > Of course that might be misleading when A.f and B.f are up to > something completely different... Exactly. -- \ ?Facts do not cease to exist because they are ignored.? ?Aldous | `\ Huxley | _o__) | Ben Finney From sion at viridian.paintbox Fri Jul 17 06:20:34 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Fri, 17 Jul 2009 10:20:34 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: Jack Diederich wrote: >It isn't an OrderedDict thing, it is a comparison thing. Two regular >dicts also raise an error if you try to LT them. Since when? Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = dict((str(i), i) for i in range (10)) >>> d2 = dict((str(i), i) for i in range (20)) >>> d1 < d2 True >>> (Don't have a 2.6 or 3 to hand.) Mind you, it makes even less sense for a regular dict than an OrderedDict. And it's not like d1.items() < d2.items() is a huge burden, if that's what you want dictionary comparison to mean. -- \S under construction From jeanmichel at sequans.com Fri Jul 17 07:06:26 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 17 Jul 2009 13:06:26 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <1247774222.4a5f860ec3cfa@mail.uh.cu> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <4A5F5759.4010600@sequans.com> <1247774222.4a5f860ec3cfa@mail.uh.cu> Message-ID: <4A605B32.5080306@sequans.com> Luis Alberto Zarrabeitia Gomez wrote: > Quoting Jean-Michel Pichavant : > > >> Emile van Sebille wrote: >> >>> On 7/16/2009 7:04 AM Unknown said... >>> >>>> On 2009-07-16, Emile van Sebille wrote: >>>> >>>>> daysInAdvance = int(inputVar) or 25 >>>>> >>>> I don't get it. That doesn't work right when inputVar == "0". >>>> >>>> >>> Aah, but you didn't get to define right. :) For that particular >>> example 0 is not a valid response. >>> >> When I was talking about such error prone form of boolean operations, I >> didn't expect to be right so quickly :p >> > > What do you mean by being "right so quickly", and "error prone" in this context? > I would also ask "Unknown" why he believes that "int(intputVar) or 25" doesn't > work right when inputVar == "0". The only false value that int() may return is > zero, so the "or 25" clause is there only for that case. I can't see then how > you think that is an error. > I was saying that using boolean operators with object instead of boolean values is error prone, cause no language behaves he same way, and all behaviors are conventions difficult to figure out without diving deeply into the documentation (or being explained as it happened to me). I think the initialization trick is an error, because I don't want foo(0) to set daysInAdvance to 25. I'll want it to set the attribute to 0, cause 0 is a valid integer. 0 is a valid integer content, None wouldn't be a valid integer content. JM From bearophileHUGS at lycos.com Fri Jul 17 07:11:10 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Fri, 17 Jul 2009 04:11:10 -0700 (PDT) Subject: ANN: psyco V2 References: Message-ID: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> Very good, thank you. I'll try it when I can. Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow? The problem I have with Psyco1.6 is that you can't use the normal profilers to know how much seconds of running time is taken by each function/method of your code. Psyco1.6 has a profile() function, but I am not much able to use it yet. Can you tell me how to find a sorted list of the running time of all functions/methods of a Psyco-digested program? Bye and thank you, bearophile From andreengels at gmail.com Fri Jul 17 07:33:03 2009 From: andreengels at gmail.com (Andre Engels) Date: Fri, 17 Jul 2009 13:33:03 +0200 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> Message-ID: <6faf39c90907170433y7258eebcyeab03ff3021aaa8a@mail.gmail.com> On Fri, Jul 17, 2009 at 7:31 AM, Xavier Ho wrote: > I have a simple class that generates prime numbers, using memorisation and > iteration to generate the next prime number. > > In the class, I have defined a function that gives, say, the 5th prime > number. Or the 1000th, it's still very fast. But the code isn't what I > really like. [...] > My problem is that it's a "While True" loop, which I get a lot of "Don't do > it!" In the light of making the code better, what could I do? Where do you get these "Don't do it!"s? As far as I know, "while true" loops are a very acceptable idiom for programming in Python (and several other languages). -- Andr? Engels, andreengels at gmail.com From contact at xavierho.com Fri Jul 17 07:38:55 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 19:38:55 +0800 Subject: Try... except....Try again? In-Reply-To: <6faf39c90907170433y7258eebcyeab03ff3021aaa8a@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <6faf39c90907170433y7258eebcyeab03ff3021aaa8a@mail.gmail.com> Message-ID: <2d56febf0907170438l6e38a503g74c3e0256404bb58@mail.gmail.com> On Fri, Jul 17, 2009 at 7:33 PM, Andre Engels wrote: > > Where do you get these "Don't do it!"s? As far as I know, "while true" > loops are a very acceptable idiom for programming in Python (and > several other languages). > Hey Andre, Friends, internet, common sense. Also... "while True" has a bad reputation. But mostly outside influences. But if it's okay, it's okay by me.. I can't think of an easier way to code the "try loop" above, anyhow. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From Larry.Martell at gmail.com Fri Jul 17 08:07:04 2009 From: Larry.Martell at gmail.com (Larry.Martell at gmail.com) Date: Fri, 17 Jul 2009 05:07:04 -0700 (PDT) Subject: setup.py not found Message-ID: <3be2bdce-680d-4b32-ad0c-ef46caf556e9@f10g2000vbf.googlegroups.com> I'm trying to install a package (cx_Oracle) on a mac running 10.5.7. I've done this on other platforms, but never on a mac. I followed the instructions given, but when I try and run setup I get: Apollo:instantclient_10_2 user$ python setup.py build /System/Library/Frameworks/Python.framework/Versions/2.5/Resources/ Python.app/Contents/MacOS/Python: can't open file 'setup.py': [Errno 2] No such file or directory Is there something else I have to install first to have this? Thanks! From dstanek at dstanek.com Fri Jul 17 08:08:48 2009 From: dstanek at dstanek.com (David Stanek) Date: Fri, 17 Jul 2009 08:08:48 -0400 Subject: Override a method but inherit the docstring In-Reply-To: References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: On Fri, Jul 17, 2009 at 2:58 AM, Peter Otten<__peter__ at web.de> wrote: > Ben Finney wrote: > >> Howdy all, >> >> The following is a common idiom:: >> >> ? ? class FooGonk(object): >> ? ? ? ? def frobnicate(self): >> ? ? ? ? ? ? """ Frobnicate this gonk. """ >> ? ? ? ? ? ? basic_implementation(self.wobble) >> >> ? ? class BarGonk(FooGonk): >> ? ? ? ? def frobnicate(self): >> ? ? ? ? ? ? special_implementation(self.warble) >> >> The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >> applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >> the method, the original docstring is not associated with it. >> >> Ideally there would be a way to specify that the docstring should be >> inherited. The best I can come up with is:: >> >> ? ? class BarGonk(FooGonk): >> ? ? ? ? def frobnicate(self): >> ? ? ? ? ? ? special_implementation(self.warble) >> ? ? ? ? frobnicate.__doc__ = FooGonk.frobnicate.__doc__ >> >> but that violates DRY (the association between BarGonk and FooGonk is >> being repeated), puts the docstring assignment awkwardly after the end >> of the method instead of at the beginning where docstrings normally go, >> and reads poorly besides. >> >> What is the most Pythonic, DRY-adherent, and preferably least-ugly >> approach to override a method, but have the same docstring on both >> methods? > > Just thinking aloud: Write a patch for pydoc that looks up the base-class > documentation. > > B.f.__doc__ will continue to return None, but > > help(B.f) will show something like > > ? ?No documentation available for B.f. > > ? ?Help for A.f: > ? ?yadda yadda > > > Of course that might be misleading when A.f and B.f are up to something > completely different... > This should never be the case. It violates LSP and would be very confusing to readers of the code. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From dstanek at dstanek.com Fri Jul 17 08:21:39 2009 From: dstanek at dstanek.com (David Stanek) Date: Fri, 17 Jul 2009 08:21:39 -0400 Subject: Override a method but inherit the docstring In-Reply-To: <02701ee5$0$5185$c3e8da3@news.astraweb.com> References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> <87ljmogglz.fsf@benfinney.id.au> <02701ee5$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Fri, Jul 17, 2009 at 3:52 AM, Steven D'Aprano wrote: > On Fri, 17 Jul 2009 12:58:48 +1000, Ben Finney wrote: > >>> Using a decorator in this manner requires repeating the super class >>> name. ?Perhaps there is a way to get the bases of BarGonk, but I don't >>> think so, because at the time that the decorator is called, BarGonk is >>> not yet fully defined. >> >> Yes, I tried a few different ways, but within the decorator it seems the >> function object is quite unaware of what class it is destined for. > > > When the decorator is called, the function object is just a function > object, not a method, so there is no concept of "what class it is > destined for". > >>>> def dec(func): > ... ? ? print type(func) > ... ? ? try: > ... ? ? ? ? ? ? print func.im_class > ... ? ? except: > ... ? ? ? ? ? ? print "no im_class" > ... ? ? return func > ... >>>> class Test(object): > ... ? ? @dec > ... ? ? def spam(self): > ... ? ? ? ? ? ? pass > ... > > no im_class >>>> type(Test.spam) > >>>> Test.spam.im_class > > > > I suppose you could try to determine what namespace you're currently when > the class is created, but that's surely going to be fragile and messy. > It isn't too bad. I got the idea to use the method's enclosing scope[1] in my decorator[2] from DecoratorTools. I am working to remove it because it make me sad, but it does work. [1] http://code.google.com/p/snake-guice/source/browse/snakeguice/decorators.py#51 [2] http://code.google.com/p/snake-guice/source/browse/snakeguice/decorators.py#58 -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From seldon at katamail.it Fri Jul 17 08:23:45 2009 From: seldon at katamail.it (Seldon) Date: Fri, 17 Jul 2009 14:23:45 +0200 Subject: guessing file type Message-ID: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Hello, I need to determine programmatically a file type from its content/extension (much like the "file" UNIX command line utility) I searched for a suitable Python library module, with little luck. Do you know something useful ? Thanks in advance. -- Seldon From ben+python at benfinney.id.au Fri Jul 17 08:28:41 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 17 Jul 2009 22:28:41 +1000 Subject: guessing file type References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: <87iqhrfq86.fsf@benfinney.id.au> Seldon writes: > Hello, I need to determine programmatically a file type from its > content/extension (much like the "file" UNIX command line utility) The Unix ?file(1)? program does its magic with a library called ?magic? and a corresponding database. > I searched for a suitable Python library module, with little luck. Do > you know something useful ? This package provides Python bindings to libmagic. -- \ ?Unix is an operating system, OS/2 is half an operating system, | `\ Windows is a shell, and DOS is a boot partition virus.? ?Peter | _o__) H. Coffin | Ben Finney From python.list at tim.thechases.com Fri Jul 17 08:36:29 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 17 Jul 2009 07:36:29 -0500 Subject: guessing file type In-Reply-To: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: <4A60704D.8080803@tim.thechases.com> > Hello, I need to determine programmatically a file type from its > content/extension (much like the "file" UNIX command line utility) > > I searched for a suitable Python library module, with little luck. Do > you know something useful ? Are you looking for something comprehensive? Or are you just looking for particular file-types that your application can handle? I'd start with the python mimetypes library[1] which does detection based on extensions (to which you can add your own mappings). For sniffing by content, there are a wide variety of document types, I don't know of any pre-existing library. The first couple bytes can often tell you something, but you'd have to go digging into the source for "file" to see what it does. -tkc [1] http://docs.python.org/library/mimetypes.html From van.lindberg at gmail.com Fri Jul 17 08:39:22 2009 From: van.lindberg at gmail.com (VanL) Date: Fri, 17 Jul 2009 07:39:22 -0500 Subject: guessing file type In-Reply-To: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: Seldon wrote: > Hello, I need to determine programmatically a file type from its > content/extension (much like the "file" UNIX command line utility) > > I searched for a suitable Python library module, with little luck. Do > you know something useful ? Python-magic (http://pypi.python.org/pypi/python-magic/0.1) wraps libmagic -- the same thing that "file" uses -- but getting it on windows is a pain. I have never been able to do it, anyway. A more cross-platform library with a similar scope is hachoir. Still seems to be available from PyPI, but the website seems to have fallen off the 'net. From victorsubervi at gmail.com Fri Jul 17 09:02:25 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Fri, 17 Jul 2009 10:02:25 -0300 Subject: Auto Send URL Message-ID: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> Hi; I am trying to script code that automatically sends a Web site visitor to an URL. Specifically, when they enter a value in a search box, I have that form sent to a script that writes an URL acceptable to Google, then I want to send the visitor off without him having to click another button. How do I do this? TIA, Victor -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Fri Jul 17 09:17:20 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 14:17:20 +0100 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> Message-ID: <4A6079E0.8040003@mrabarnett.plus.com> Xavier Ho wrote: > I have a simple class that generates prime numbers, using memorisation > and iteration to generate the next prime number. > > In the class, I have defined a function that gives, say, the 5th prime > number. Or the 1000th, it's still very fast. But the code isn't what I > really like. > > def nPrime(self, n): > "Returns nth prime number, the first one being 2, where n = 0. > When n = 1, it returns 3." > while True: > try: > return self.primes[n] > except: > self.next() > > The next() function generates the next prime, and appends it into > primes. This way, it keeps trying to append until the nth prime > requested exist, and returns it. > > My problem is that it's a "While True" loop, which I get a lot of "Don't > do it!" In the light of making the code better, what could I do? > I wouldn't use a bare "except". What if you had written "prims" instead of "primes"? It would catch AttributeError and produce the next prime, repeating until it ran out of memory. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 09:28:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 13:28:00 GMT Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> Message-ID: <02706d8f$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 00:34:00 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> It is very useful to be able to write e.g.: >> >> if header or body or footer: >> print assemble_page(header, body, footer) >> >> and have empty strings to be equivalent to False. > > Why doesn't assemble_page properly handle the case where header, body, > and footer are all empty? That would let you eliminate the if. "Make > sure your code 'does nothing' gracefully" (Kernighan and Plauger). You mean something like this? def assemble_page(header, body, footer): if header or body or footer: do_lots_of_expensive_processing() else: do_nothing_gracefully() Sure, why not? Whatever way you do it, my point is that it is very useful to perform branching tests on non-Boolean objects. Wherever you put the test, being able to write: if header or body or footer: is a huge improvement over: if (header != '') or (body != '') or (footer != ''): -- Steven From edreamleo at charter.net Fri Jul 17 09:36:59 2009 From: edreamleo at charter.net (Edward K Ream) Date: Fri, 17 Jul 2009 08:36:59 -0500 Subject: ANN: Leo 4.6 final released Message-ID: Leo 4.6 final is now available at: http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106 Leo is a text editor, data organizer, project manager and much more. See: http://webpages.charter.net/edreamleo/intro.html The highlights of Leo 4.6: -------------------------- - Cached external files *greatly* reduces the time to load .leo files. - Leo now features a modern Qt interface by default. Leo's legacy Tk interface can also be used. - New --config, --file and --gui command-line options. - Leo tests syntax of .py files when saving them. - Leo can now open any kind of file into @edit nodes. - @auto-rst nodes allow easy editing of reStructuredText files. - Properties of commanders, positions and nodes simplify programming. - Improved Leo's unit testing framework. - Leo now requires Python 2.5 or later. - Dozens of small improvements and bug fixes. Links: ------ Leo: http://webpages.charter.net/edreamleo/front.html Forum: http://groups.google.com/group/leo-editor Download: http://sourceforge.net/project/showfiles.php?group_id=3458 Bzr: http://code.launchpad.net/leo-editor/ Quotes: http://webpages.charter.net/edreamleo/testimonials.html -------------------------------------------------------------------- Edward K. Ream email: edreamleo at yahoo.com Leo: http://webpages.charter.net/edreamleo/front.html -------------------------------------------------------------------- From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 09:42:44 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 13:42:44 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> Message-ID: <02707103$0$5185$c3e8da3@news.astraweb.com> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: > Python has extended the algebra definition of "or" and "and" top any > type, but it is so unintuitive (I'm no LISP programmer). I disagree. The Something/Nothing dichotomy is so intuitive to me that I would hate to go back to a language that only accepted booleans as arguments to `if`. > I think than > using the short-circuiting mechanism of bool operators along with the > python tricks is just error prone and may result in bug difficult to > spot, unless you are very aware of all python boolean mechanisms. In other words, if you don't know how Python behaves, you will make mistakes. Of course you will. That applies to *anything* -- if you don't know how it works, you will make mistakes. Given three result codes, where 0 means "no error" and an arbitrary non- zero integer means some error, it is simple and easy to write: failed = result_1 or result_2 or result_3 The equivalent: failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) # or if you prefer: succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) are longer and more difficult to read and easier to get wrong. Even worse are tricks like this: failed = (result_1 + result_2 + result_3) != 0 This obscures the fact that the result codes are flags and makes it seem like (flag + flag) is meaningful. -- Steven From justin.mailinglists at gmail.com Fri Jul 17 09:58:13 2009 From: justin.mailinglists at gmail.com (Justin Ezequiel) Date: Fri, 17 Jul 2009 06:58:13 -0700 (PDT) Subject: guessing file type References: <4a606b28$0$18938$4fafbaef@reader2.news.tin.it> Message-ID: <7c7ab0ce-ffea-46da-b495-8ebf8e63ecf6@32g2000yqj.googlegroups.com> On Jul 17, 8:39?pm, VanL wrote: > Seldon wrote: > > Hello, ?I need to determine programmatically a file type from its > > content/extension (much like the "file" UNIX command line utility) > > > I searched for a suitable Python library module, with little luck. Do > > you know something useful ? > I've used the magic.py module by "Jason Petrone" before. http://tinyurl.com/ldho8m From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 10:12:37 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 14:12:37 GMT Subject: Try... except....Try again? References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> Message-ID: <02707804$0$5185$c3e8da3@news.astraweb.com> Apologies for breaking threading, but the original post hasn't showed up on my ISP's news server. Xavier Ho wrote: > I have a simple class that generates prime numbers, using memorisation > and iteration to generate the next prime number. [...] > The next() function generates the next prime, and appends it into > primes. This way, it keeps trying to append until the nth prime > requested exist, and returns it. > > My problem is that it's a "While True" loop, which I get a lot of >> "Don't do it!" There's nothing wrong with "while True" loops. They are useful in two situations: (1) Where you want to loop forever. (2) Where you want to loop until some complex event takes place, which can't easily be written using "while condition". Neither of these cases holds for your code, so although there's nothing wrong with "while True", in your case I'd avoid it. > In the light of making the code better, what could I do? (1) Explicitly loop the correct number of times. (2) Only catch IndexError. def nPrime(self, n): for i in xrange(len(self.primes), n+1): self.next() return self.primes[n] -- Steven From http Fri Jul 17 10:12:51 2009 From: http (Paul Rubin) Date: 17 Jul 2009 07:12:51 -0700 Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> Message-ID: <7xab33s8ik.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > def assemble_page(header, body, footer): > if header or body or footer: > do_lots_of_expensive_processing() > else: > do_nothing_gracefully() Why should the processing be expensive if all three fields are empty? > if header or body or footer: > is a huge improvement over: > if (header != '') or (body != '') or (footer != ''): Doesn't really seem any better. There's always something like if any(p != '' for p in [header, body, footer, otherthing1, ...]) if the number of components gets larger. From kyrie at uh.cu Fri Jul 17 10:19:51 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Fri, 17 Jul 2009 10:19:51 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <4A605B32.5080306@sequans.com> References: <1247774222.4a5f860ec3cfa@mail.uh.cu> <4A605B32.5080306@sequans.com> Message-ID: <200907171019.52471.kyrie@uh.cu> On Friday 17 July 2009 07:06:26 am Jean-Michel Pichavant wrote: > > I was saying that using boolean operators with object instead of boolean > values is error prone, cause no language behaves he same way, I don't know of many languages that actively promote the duck typing concept, are as highly dynamic as python, have the "metaclass" concept, treats almost all language concepts as first class citizens (including functions and classes), and so on. And don't get me started on assignment (which I consider very natural, by the way, but apparently most of the popular languages have pretty unnatural assignments). It is error prone if you are expecting the result to be a bool instead of just behaving like one (it is certainly unexpected, but you shouldn't be expecting to get an instance of certain class anyway, for most of python's operations). And it is confusing if you are reading the "int(inputVar) or 25" line and have no idea of what it means (but once you know it, it becomes very readable, almost plain english). > and all > behaviors are conventions difficult to figure out without diving deeply > into the documentation (or being explained as it happened to me). That happens with many new concepts. Welcome to python, I guess... if you are willing to shake some of your expectations from previous programming languages, you will enjoy it. My [little] experience teaching python tells me that "duck typing", "non-enforced encapsulation" and "everything is an object" are the hardest to accept for the C# folk at my faculty, but once past it, they (the few of them who don't leave the course after that) really enjoy the language. > I think the initialization trick is an error, because I don't want > foo(0) to set daysInAdvance to 25. I'll want it to set the attribute to > 0, cause 0 is a valid integer. 0 is a valid integer content, None > wouldn't be a valid integer content. Well, that wouldn't be a problem with "or", but with the programmer. The exact same behaviour could be obtained with if int(inputValue) == 0: inputValue = 25 and no "or" involved. However, using only inputValue = inputValue or 25 could have been an error if you only wanted 25 in case inputValue is None. (the "or trick" implies certain trust in that the object you have in hand has a reasonable definition of truth value). -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From jeanmichel at sequans.com Fri Jul 17 10:34:57 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 17 Jul 2009 16:34:57 +0200 Subject: missing 'xor' Boolean operator In-Reply-To: <02707103$0$5185$c3e8da3@news.astraweb.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A608C11.1020901@sequans.com> Steven D'Aprano wrote: > On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: > > > Given three result codes, where 0 means "no error" and an arbitrary non- > zero integer means some error, it is simple and easy to write: > > failed = result_1 or result_2 or result_3 > > The equivalent: > > failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) > # or if you prefer: > succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) > > [snip] This is, I guess, where we disagree. I find the second proposal less error prone, and universally understandable unlike the first one. It may be verbose, it may look even lame to some people, but in the end this is perfectly reliable, because you manipulate only False or True within the boolean operations. The first form does not clearly show what is the failed criteria. It just happens by coincidence that in this case the failed criteria matches the Nothingness of result_1, result_2, result_3. What if results may be 'OK' or 'KO'. failed = result_1 or result_2 or result_3 won't work. failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is lame but reliable. JM From contact at xavierho.com Fri Jul 17 10:35:24 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 22:35:24 +0800 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> Message-ID: <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> oops, wrong address. When will reply-to tag appear on the Python mailing list? =/ Good idea, Steven and MRAB. I changed my code to the following: def nPrime(self, n): "Returns nth prime number, the first one being 2, where n = 0. When n = 1, it returns 3." for x in range(n+2): try: return self.primes[n] except IndexError: self.next() And it's definitely better. Thanks a ton guys. (n+2 because after looping the maximum number of times, it needs to try returning the value again.) It works just as well as the previous version, but slightly safer on my part... and I can adapt this in my future coding habits. Thanks again. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Fri Jul 17 10:51:05 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 17 Jul 2009 07:51:05 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A608C11.1020901@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> <4A608C11.1020901@sequans.com> Message-ID: <4A608FD9.908@stoneleaf.us> Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: > >> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: >> >> >> Given three result codes, where 0 means "no error" and an arbitrary non- >> zero integer means some error, it is simple and easy to write: >> >> failed = result_1 or result_2 or result_3 >> >> The equivalent: >> >> failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) >> # or if you prefer: >> succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) >> >> > > [snip] > > This is, I guess, where we disagree. I find the second proposal less > error prone, and universally understandable unlike the first one. Careful! The very few (if any) things in this world that can be considered "universally understandable" do *not* include any programming language! :) > It may > be verbose, it may look even lame to some people, but in the end this is > perfectly reliable, because you manipulate only False or True within the > boolean operations. > > The first form does not clearly show what is the failed criteria. It > just happens by coincidence that in this case the failed criteria > matches the Nothingness of result_1, result_2, result_3. What if results > may be 'OK' or 'KO'. As the programmer, particularly a Python programmer, you should be taking advantage of Python's strengths, of which this is one. If, as you say, some function uses a "something" value to indicate an undesirable result, then you have to use something akin to your last statement. ~Ethan~ > > failed = result_1 or result_2 or result_3 > won't work. > > failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is > lame but reliable. > > > JM From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 10:54:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 14:54:53 GMT Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> Message-ID: <027081ec$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 07:12:51 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> def assemble_page(header, body, footer): >> if header or body or footer: >> do_lots_of_expensive_processing() >> else: >> do_nothing_gracefully() > > Why should the processing be expensive if all three fields are empty? Since I haven't specified an implementation for assemble_page, it could be doing *anything*. Perhaps it has to talk to a remote database over a slow link, perhaps it generates 300 lines of really inefficient HTML code with no content, perhaps it sends a print job to a printer which then warms up, cleans the print heads, and ejects a blank piece of paper. Why does it matter? The example is about the `if` test, not about the function assemble_page(). -- Steven From jackdied at gmail.com Fri Jul 17 11:02:09 2009 From: jackdied at gmail.com (Jack Diederich) Date: Fri, 17 Jul 2009 11:02:09 -0400 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: On Fri, Jul 17, 2009 at 10:35 AM, Xavier Ho wrote: > I changed my code to the following: > > ??? def nPrime(self, n): > ??????? "Returns nth prime number, the first one being 2, where n = 0. When > n = 1, it returns 3." > ??????? for x in range(n+2): > ??????????? try: > ??????????????? return self.primes[n] > ??????????? except IndexError: > ??????????????? self.next() > > And it's definitely better. Thanks a ton guys. > > (n+2 because after looping the maximum number of times, it needs to try > returning the value again.) def nPrime(self, n): while len(self.primes) < n: self.next() return self.primes[n] If you use an off-the-shelf prime number generator fucntion[1] that returns consecutive primes the method collapses into a simple function. def nPrime(n, primes=[], next_prime=eratosthenes().next): while len(primes) < n: primes.append(next_prime()) return primes[n] -Jack [1] http://www.catonmat.net/blog/solving-google-treasure-hunt-prime-number-problem-four/#comment-3075 There is a deque implementation that is faster still but I can't find a link. From python at mrabarnett.plus.com Fri Jul 17 11:09:03 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 16:09:03 +0100 Subject: Einstein summation notation In-Reply-To: <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <4A60940F.6090604@mrabarnett.plus.com> koranthala wrote: >> That test was designed to treat None as a boolean False, without >> noticing that numeric 0 is also treated as False and could make the >> test do the wrong thing. This is an extremely common type of error. > > Actually, I felt that 0 not being considered False would be a better > option. > I had lot of instances where 0 is a valid value and always I had to > put checks specifically for that. > For me, None and False should be equal to False in if checks, every > thing else being considered True. > > Can someone let me know why 0 is considered equal to False? > There should be real good reasons for it, only that I cannot think of > one. Python did always have True and False. From gabriel.rossetti at arimaz.com Fri Jul 17 11:19:36 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 17 Jul 2009 17:19:36 +0200 Subject: no return value for threading.Condition.wait(timeout)? In-Reply-To: References: Message-ID: <4A609688.9080201@arimaz.com> Piet van Oostrum wrote: >>>>>> Gabriel Rossetti (GR) wrote: >>>>>> > > >> GR> I have a 1-1 relation, I have a thread reading msgs from a network socket >> GR> and a method waiting for an answer to arrive (see my thread >> GR> "Threading.Condition problem"). I would like to be able to have a timeout >> GR> as to not block forever, so the idea is to check if I returned because of a >> GR> timeout or not. >> > > So that case is easy I think. After the wait just check if the answer > has arrived. > The problem is other answers could arrive that are not the one I want. Maybe I could check if I got the answer, if not then check how much time has elapsed, and if it's grater or equal to the timeout consider it a timeout. I still think a return value would be better though. From ethan at stoneleaf.us Fri Jul 17 11:20:41 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 17 Jul 2009 08:20:41 -0700 Subject: Einstein summation notation In-Reply-To: <7xab33s8ik.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> Message-ID: <4A6096C9.4070703@stoneleaf.us> Paul Rubin wrote: > Steven D'Aprano writes: > >>def assemble_page(header, body, footer): >> if header or body or footer: >> do_lots_of_expensive_processing() >> else: >> do_nothing_gracefully() > > > Why should the processing be expensive if all three fields are empty? > > >> if header or body or footer: >>is a huge improvement over: >> if (header != '') or (body != '') or (footer != ''): > > > Doesn't really seem any better. There's always something like > > if any(p != '' for p in [header, body, footer, otherthing1, ...]) > > if the number of components gets larger. Or if any(p for p in [header, body, footer, whatever, ...]) Which is even nicer! :) Particularly if header, et al, are some more complicated objects which don't easily generate strings, but do easily know whether they are Something or Nothing. I suppose some of these discussions are based more on one's personal requirements (using several languages, just a couple, or just Python), and whether one wants to achieve Mastery of Python, or just be Proficient. Neither choice is wrong, and wanting Mastery, of course, does not mean not wanting improvements, etc. It does, however, require a deep understanding of not just the mechanics, but the philosophy of the language. Mind you, I am nowhere near Mastery just yet (okay, okay, I have a _long_ way to go! ;), but I love the philosophy of Python, it's simplicity, and the *entirety* of the Zen. ~Ethan~ From malaclypse2 at gmail.com Fri Jul 17 11:26:18 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 17 Jul 2009 11:26:18 -0400 Subject: Einstein summation notation In-Reply-To: <4A60940F.6090604@mrabarnett.plus.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <4A60940F.6090604@mrabarnett.plus.com> Message-ID: <16651e80907170826r13856d5ag48eb299b70574450@mail.gmail.com> On Fri, Jul 17, 2009 at 11:09 AM, MRAB wrote: > Python did always have True and False. Only if "always" means "since python 2.2.1". See: http://www.python.org/doc/2.3/whatsnew/section-bool.html and http://www.python.org/dev/peps/pep-0285/ for details. -- Jerry From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 11:30:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 15:30:11 GMT Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> Message-ID: <02708a32$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 16:34:57 +0200, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: >> >> >> Given three result codes, where 0 means "no error" and an arbitrary >> non- zero integer means some error, it is simple and easy to write: >> >> failed = result_1 or result_2 or result_3 >> >> The equivalent: >> >> failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) # or if >> you prefer: >> succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) >> >> > [snip] > > This is, I guess, where we disagree. I find the second proposal less > error prone, and universally understandable unlike the first one. It may > be verbose, it may look even lame to some people, but in the end this is > perfectly reliable, because you manipulate only False or True within the > boolean operations. Because it is verbose, it is more error-prone. The more code you have to write, the more opportunities you have to make mistakes. (This holds up to a point, beyond which being more and more terse also leads to more errors.) Boolean algebra is notorious for programmer errors. The more complicated the boolean expression, the more often programmers get it wrong. The more book-keeping they have to do, the easier it is to get it wrong. All those (result != 0) comparisons are mere book-keeping, they don't add anything to the code except to force the flag to be True or False. > The first form does not clearly show what is the failed criteria. Of course it does: at least one of the three result codes is non-zero. As a bonus, failed will be assigned the first non-zero result code (if any). Your preferred form, on the other hand, folds all the possible error codes into True, throwing away useful information: >>> result_1 = 0 # success >>> result_2 = 0 >>> result_3 = 15 # failure >>> failure = result_1 or result_2 or result_3 >>> failure 15 >>> failure = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) >>> failure True > It > just happens by coincidence that in this case the failed criteria > matches the Nothingness of result_1, result_2, result_3. What if results > may be 'OK' or 'KO'. Obviously the test you write depends on the data you have to work with. Do you think that's surprising? > failed = result_1 or result_2 or result_3 won't work. Not in the example you gave, no. Although if I had to work with lots and lots of strings of the form 'OK' and 'KO', I'd consider sub-classing string: >>> class OKString(str): ... def __nonzero__(self): ... if self == 'OK': return True ... elif self == 'KO': return False ... else: ... raise ValueError('invalid result code') ... >>> >>> a = OKString('OK') >>> b = OKString('KO') >>> c = OKString('KO') >>> if a and b and c: ... print "Success!" ... else: print "Failed!" ... Failed! (I wouldn't bother if I only needed one or two such tests, but if I had lots of them, I'd consider it.) > failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is > lame but reliable. Yes, it's reliably complicated, reliably easy to get wrong, reliably harder to read, and it reliably throws away information. But apart from those disadvantages, it does the job you want it to do. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 17 11:32:41 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 17 Jul 2009 15:32:41 GMT Subject: Einstein summation notation References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> Message-ID: <02708ac8$0$5185$c3e8da3@news.astraweb.com> On Fri, 17 Jul 2009 16:09:03 +0100, MRAB wrote: > Python did always have True and False. $ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> True, False Traceback (innermost last): File "", line 1, in ? NameError: True -- Steven From mooniitk at gmail.com Fri Jul 17 11:42:19 2009 From: mooniitk at gmail.com (mayank gupta) Date: Fri, 17 Jul 2009 17:42:19 +0200 Subject: 'del' function for Dictionary Message-ID: Hi all, I wanted to know whether there is a more efficient way to delete an entry from a dictionary (instead of using the 'del' function), because after analyzing the time taken by the code, it seems to me that the 'del' function takes most of the time. I might be incorrect as well. Kindly help me in this regard. Cheers, Mayank -- I luv to walk in rain bcoz no one can see me crying -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 17 11:50:14 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 17 Jul 2009 23:50:14 +0800 Subject: Try... except....Try again? In-Reply-To: References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: <2d56febf0907170850w5941170fpa527571f7fc241ef@mail.gmail.com> On Fri, Jul 17, 2009 at 11:02 PM, Jack Diederich wrote: > > If you use an off-the-shelf prime number generator fucntion[1] that > returns consecutive primes the method collapses into a simple > function. > > def nPrime(n, primes=[], next_prime=eratosthenes().next): > while len(primes) < n: > primes.append(next_prime()) > return primes[n] > > -Jack > > [1] > http://www.catonmat.net/blog/solving-google-treasure-hunt-prime-number-problem-four/#comment-3075 > There is a deque implementation that is faster still but I can't > find a link. Jack, thanks for the link, I'll definitely look into it. The function is currently implemented in a class I wrote myself, which is a generator class that does exactly the same thing in the next() function. I didn't post the code here because it wasn't in the scope of this thread, but I'm sure others have done a lot better and much more efficient algorithms. The nPrime method uses the nature of the iteration and checks if a value already exists (ie generated before) and return that value; otherwise it iterates again. This way, I don't have to iterate from n=2 and on every time, which makes generation time linear. However, I just gave the link's code a quick try. It was at least 10x times faster than my code to generate 100000 prime numbers - I'll have a closer look. Thanks a great deal. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From motoom at xs4all.nl Fri Jul 17 11:54:47 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Fri, 17 Jul 2009 17:54:47 +0200 Subject: 'del' function for Dictionary In-Reply-To: References: Message-ID: <4A609EC7.6060007@xs4all.nl> mayank gupta wrote: > after analyzing the time taken by the code, What code? From dan.eloff at gmail.com Fri Jul 17 12:11:18 2009 From: dan.eloff at gmail.com (Eloff) Date: Fri, 17 Jul 2009 09:11:18 -0700 (PDT) Subject: Find first matching substring Message-ID: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> Almost every time I've had to do parsing of text over the last 5 years I've needed this function: def find_first(s, subs, start=None, end=None): results = [s.find(sub, start, end) for sub in subs] results = [r for r in results if r != -1] if results: return min(results) return -1 It finds the first matching substring in the target string, if there is more than one match, it returns the position of the match that occurs at the lowest index in the string. Has anyone else had problems where they could have applied this function? It seems to me that python's find (and rfind, index, rindex) could be modified (the same way that startswith and endswith have been) to behave this way if a tuple were passed. Do other's agree that this would be desirable? Thanks, Dan From http Fri Jul 17 12:15:17 2009 From: http (Paul Rubin) Date: 17 Jul 2009 09:15:17 -0700 Subject: Einstein summation notation (was: question of style) References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> <027081ec$0$5185$c3e8da3@news.astraweb.com> Message-ID: <7xljmn5lre.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Since I haven't specified an implementation for assemble_page, it could > be doing *anything*. Perhaps it has to talk to a remote database over a > slow link, perhaps it generates 300 lines of really inefficient HTML code > with no content, perhaps it sends a print job to a printer which then > warms up, cleans the print heads, and ejects a blank piece of paper. That sounds like a code smell to me. If the function is supposed to print a piece of paper when any of those strings are nonempty, it should also print one when they are all empty. Of the different values a string can have, 'foo', 'bar', and '' are all perfectly good values and should all be treated the same way. If you want an absent string to work differently than a present one, that's separate from empty vs nonempty, and overloading the empty string to mean "absent" is another antipattern. Maybe what you really want is a list of page constituents that are present: page_parts = [header, body, footer] If all the constituents are absent, then page_parts is the empty list, so you would just test for page_parts being empty. > Why does it matter? The example is about the `if` test, not about the > function assemble_page(). Because of the notion that the code should "do nothing" gracefully. The presence of that 'if' test is ungraceful, so its better to look for ways to eliminate it than slickify it. From wells at submute.net Fri Jul 17 12:31:10 2009 From: wells at submute.net (Wells Oliver) Date: Fri, 17 Jul 2009 11:31:10 -0500 Subject: Question regarding style/design.. Message-ID: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> Sometimes I see relatively small application, generally task scripts, written as essentially a list of statements. Other times, I see them neatly divided into functions and then the "if __name__ == '__main__':" convention. Is there a preference? Is there an... application scope such that the preference shifts from the former to the latter? I understand the use of the __name__ == 'main' convention for building unit tests, but I'm mixed on using it in scripts/small applications. Thanks for any thoughts! -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From mabdelkader at gmail.com Fri Jul 17 12:32:12 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Fri, 17 Jul 2009 12:32:12 -0400 Subject: A multi-threaded file searcher for processing large text files Message-ID: <148918f0907170932w7f1a7f3br78a70845e9050566@mail.gmail.com> I'm building a threaded file searcher that uses some of Fredrik Lundh's ( http://effbot.org/zone/wide-finder.htm) suggestions for parsing text very quickly in pure python, as I have about a 10GB log file to parse every day. A naiive approach would be to just parse the 1MB chunks, add the results into a list, and just traverse that list. I want to take this idea a bit further. I want to process results as they're being found. A great way to implement this is to use the Queue class that python provides. My idea is to exploit the iterator protocol to have it block until a result is found, if any, and return the result until we're finished parsing the file then we can raise StopIteration. My idea is sort of similar to a producer / consumer, but it follows something of this idiom: producer produces the file chunks consumer consumes the file chunks -> consumer parsers the file chunks and produces results class waits on the production of the original consumer and processes it as they come. I am having a bit of trouble with the concurrency, but I'm using this as an exercise to understand how concurrency works from a broader scale. I am not trying to get into a debate of whether this is really needed or a python-concurrency debate:) Without further ado, my class is as follows: class ThreadedFileSearcher(object): def __init__(self, filename, rx_pat, blocking_timeout = 10): self.name = filename self.pattern = rx_pat self.blocking_timeout = blocking_timeout #need to find a better way to do this with more threads that can return #stable results (aka chunks are in order) self._thread_count = 1 #the queues self._results = Queue.Queue() self._queue = Queue.Queue() #returns the get_chunk() implementation self._engine = LogParsingEngine(filename) #start acquiring file offsets for the file #as daemon threads self._initialize_worker_threads(self._prime_queue) #experimental...should probably be some type of conditional variable self._stop_processing = False def __iter__(self): #start the worker threads self._initialize_worker_threads(self._target_worker) return self.next() def _initialize_worker_threads(self, callback): #should really use just one thread for i in xrange(self._thread_count): t = threading.Thread(target=callback) t.setDaemon(True) t.start() def _prime_queue(self): """put code chunk offsets on the queue""" #get_chunks() just returns 1MB offsets in the file for chunk in self._engine.get_chunks(): self._queue.put(chunk) def _target_worker(self): """code chunk to parse queue""" #loop infinitely while True: try: #get next chunk offset from the queue start_pos, bytes_to_read = self._queue.get( timeout=self.blocking_timeout ) except (TypeError, Queue.Empty): #None was returned from the .get() #this will throw a TypeError as it tries to unpack None #or the Queue was empty self._stop_processing = True #exit loop break #process the cunk here f = open(self.name, 'r') f.seek(start_pos) #find all matching lines in the chunk for chunk in self.pattern.findall(f.read(bytes_to_read)): #an non-overlapping matches of self.pattern #placed on the queue as a string self._results.put(chunk) f.close() #done! self._queue.task_done() def next(self): while True: try: #wait for the results to be put on matchedlist = self._results.get(timeout=self.blocking_timeout) except Queue.Empty: #if the worker thread finished if self._stop_processing: raise StopIteration else: self._results.task_done() yield matchedlist To use the following class, I wanted to have some kind of interface like this: regex = re.compile("-{3}Processing..-{3}") #---Processing..--- f = ThreadedFileSearcher("LogFile.log", regex) for line in f: #guaranteed to be a line that matches regex #process something... print line I am looking for some suggestions, comments, and better ways to modify this. One thing someone will realize when using this class is that the initial parsing will be incredibly quick, but if the blocking_timeout is set to 10, then there will be a 10second wait at the end to test if the worker threads should set the stop conditions. A glaring hole leading from this is if the blocking_timeout is set to something like 1second and by the time a user attempts to iterate over the results, the worker threads will prematurely stop processing. Speaking of stop processing, should self._stop_processing be a conditional variable. Right now it's a boolean, and I think that's pretty hacky. I don't like the StopIteration stop condition, maybe someone has a better suggestion? A future modification I'm looking for is to launch multiple threads that process different parts of the file (different chunks) and return their results, probably indexed by their chunk offset. Then I can iterate over that sequentially. I think that would be a trivial parallel optimization. Thoughts? Comments? Thanks very much, Mahmoud Abdelkader mahmoud at linux.com http://blog.mahmoudimus.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Jul 17 12:55:39 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 17 Jul 2009 09:55:39 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <4A608C11.1020901@sequans.com> References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <02707103$0$5185$c3e8da3@news.astraweb.com> <4A608C11.1020901@sequans.com> Message-ID: On 7/17/2009 7:34 AM Jean-Michel Pichavant said... > Steven D'Aprano wrote: >> On Thu, 16 Jul 2009 15:53:45 +0200, Jean-Michel Pichavant wrote: >> >> >> Given three result codes, where 0 means "no error" and an arbitrary non- >> zero integer means some error, it is simple and easy to write: >> >> failed = result_1 or result_2 or result_3 >> >> The equivalent: >> >> failed = (result_1 != 0) or (result_2 != 0) or (result_3 != 0) >> # or if you prefer: >> succeeded = (result_1 == 0) and (result_2 == 0) and (result_3 == 0) >> >> > [snip] > > This is, I guess, where we disagree. I find the second proposal less > error prone, and universally understandable unlike the first one. It may > be verbose, it may look even lame to some people, but in the end this is > perfectly reliable, because you manipulate only False or True within the > boolean operations. > > The first form does not clearly show what is the failed criteria. It > just happens by coincidence No -- it happens by design because the premise is 'where 0 means "no error" and an arbitrary non-zero integer means some error'. > that in this case the failed criteria > matches the Nothingness of result_1, result_2, result_3. What if results > may be 'OK' or 'KO'. Which by definition won't happen for the example cited... > > failed = result_1 or result_2 or result_3 > won't work. ... so you certainly wouldn't write a test that couldn't properly determine success or failure. > > failed = (result_1 =='KO') or (result_2 =='KO') or (result_3 =='KO') is > lame but reliable. In this case I'd write something that meets the specs of the problem your addressing... failed = 'KO' in (result_1,result_2,result_3) Emile From nohics at gmail.com Fri Jul 17 12:59:47 2009 From: nohics at gmail.com (nohics nohics) Date: Fri, 17 Jul 2009 17:59:47 +0100 Subject: Regular exprssion for IRC messages Message-ID: Hello, When an IRC client receive a messages, this message in in a special format defined here: Message format in pseudo BNF( http://www.irchelp.org/irchelp/rfc/chapter2.html#c2_3_1 ). For example we can have: :MyNickName!n=MyUserName at 41.238.129.121 JOIN :#AnIrcChannel :MyNickName!n=MyUserName at 41.238.129.121 PRIVMSG #AnIrcChannel :Hello here :MyNickName!n=MyUserName at 41.238.129.121 NICK :TheNewNickName :holmes.freenode.net 372 UnNickName :- a message here ... I want to transforme the message format defined in the irc protocole in the last link, to a regular expression to get the nickname, the username, the host, the commad, it's arguments if exist, and the message after ":" I'm programming in python and I have tried this regular expression: ^(:(\w+)(![^ \r\n]+)?(\@[^ \r\n]+)? )?([a-zA-Z]+|\d\d\d)(( [^: \r\n][^ \r\n]*)+)( :[^\r\n]*)? Python code: nicknamePattern = re.compile(r'^(:(\w+)(![^ \r\n]+)?(\@[^ \r\n]+)? )?([a-zA-Z]+|\d\d\d)(( [^: \r\n][^ \r\n]*)+)( :[^\r\n]*)?') matches = nicknamePattern.search(data) if matches != None: print matches.groups() But it doesn't seems to work all the time. So can you help me to make the correct regular expression please ? You can see some example of messages working or not with this pattren (when I try to connect to an irc server): http://www.developpez.net/forums/d779736/php/langage/regex/format-message-pseudo-bnf-expression-reguliere/#post4494010 Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.list at tim.thechases.com Fri Jul 17 13:11:49 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 17 Jul 2009 12:11:49 -0500 Subject: Question regarding style/design.. In-Reply-To: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> Message-ID: <4A60B0D5.6030806@tim.thechases.com> > Sometimes I see relatively small application, generally task scripts, > written as essentially a list of statements. Other times, I see them neatly > divided into functions and then the "if __name__ == '__main__':" convention. > Is there a preference? Is there an... application scope such that the > preference shifts from the former to the latter? I understand the use of the > __name__ == 'main' convention for building unit tests, but I'm mixed on > using it in scripts/small applications. This may circle around to the "is Python a scripting language" bruhaha a while back. I tend to skip the __name__ == '__main__' in what I'd consider scripts (one-file hacks to solve an immediate problem, often disposable or one-use wonders). However for non-scripts (which I'd define as code that has more than one source-code file I've written or that I'd have to maintain) and for modules, I tend to put in the __name__ guard. -tkc From aurora00 at gmail.com Fri Jul 17 13:15:59 2009 From: aurora00 at gmail.com (Wai Yip) Date: Fri, 17 Jul 2009 10:15:59 -0700 (PDT) Subject: ctype performance benchmark References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> Message-ID: <48c15bac-8af2-472d-9bc0-1f19c4bf29cb@g7g2000prg.googlegroups.com> I started with ctypes because it is the battery included with the Python standard library. My code is very fluid and I'm looking for easy opportunity to optimize it. One example is to find the longest common prefix among two strings. Right now I am comparing it character by character with pure Python. It seems like an ideal low hanging fruit. With ctype I can rewriting a few lines of Python into a few lines of C. All the tools are available and no third party library is needed. It turned out the performance fail to match pure Python. This function is called million of times in an inner loop. The overhead overwhelm any performance gain with C. Eventually I have found success grouping the data into larger chunk for each API call. This is what I originally planned to do anyway. I am only sharing my experience here that doing fine grained ctype function call has its limitation. I have looked into Pyrex before and I was quite confused by the fusion of C and Python langauage. Perhaps it is time for me to give it a second look. I just heard of Cython now and it also look interesting. I think it is helpful for both project to articulate more clearly what they are, how they achieve the performance gain and to give more guidance on good use cases. On the other hand, perhaps it is just me are confused because I don't know enough of the Python internal. Wai Yip From mydevforums at gmail.com Fri Jul 17 13:17:19 2009 From: mydevforums at gmail.com (IronyOfLife) Date: Fri, 17 Jul 2009 10:17:19 -0700 (PDT) Subject: Installing python-gnutls on Windows Message-ID: <58825f8b-9665-4a3d-9f0a-7bb559ae195e@p23g2000vbl.googlegroups.com> Hi, I'm new to python and would like to request your help installing python-gnutls on windows. Are there binaries available which can be installed ? Looks like the setup.py file which came with the package is for Debian. I tried modifying it for windows but am still getting errors. get_options() is not getting the include directories and libraries so I harcoded the include directories. I'm getting the following error now: //----------------------------------------------------------------------------------- C:\Development\python\python-gnutls-1.1.8>python setup.py build -- compiler=mingw32 running build running build_py running build_ext building 'gnutls.library._gnutls_init' extension Traceback (most recent call last): File "setup.py", line 86, in libraries='-lgnutls -lgnutls-extra')]) File "C:\Python26\lib\distutils\core.py", line 152, in setup dist.run_commands() File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands self.run_command(cmd) .... File "C:\Python26\lib\distutils\command\build_ext.py", line 471, in build_exte nsions self.build_extension(ext) ... depends=ext.depends) File "C:\Python26\lib\distutils\ccompiler.py", line 689, in compile depends, extra_postargs) File "C:\Python26\lib\distutils\ccompiler.py", line 363, in _setup_compile "'include_dirs' (if supplied) must be a list of strings" TypeError: 'include_dirs' (if supplied) must be a list of strings //------------------------------------------------------------------------------------- I would really appreciate your help Thanks Ramg From lodmod.dod at gmail.com Fri Jul 17 13:20:43 2009 From: lodmod.dod at gmail.com (LoD MoD) Date: Fri, 17 Jul 2009 10:20:43 -0700 Subject: getopt code NameError exception on logTail.py Message-ID: I am having trouble extending my option parsing. Any help is appreciated: import sys import getopt import time def tail(file): while 1: where = file.tell() line = file.readline() if not line: time.sleep(1) file.seek(where) else: print line, # already has newline def main(): # parse command line options try: opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", "filename=" ]) except getopt.error, msg: print msg print "for help use --help" sys.exit(2) # process options for o, a in opts: if o in ("-h", "--help"): print __doc__ sys.exit(0) if o in ("-f", "--filename"): print "Parsing F argument" file = open(filename, 'r') print file # process arguments for arg in args: process(arg) # process() is defined elsewhere if __name__ == "__main__": main() Yields this error: localhost:src gsery$ python logTail.py /var/log/system.log Traceback (most recent call last): File "logTail.py", line 52, in main() File "logTail.py", line 49, in main process(arg) # process() is defined elsewhere NameError: global name 'process' is not defined -------------- next part -------------- An HTML attachment was scrubbed... URL: From mrabarnett at mrabarnett.plus.com Fri Jul 17 13:24:42 2009 From: mrabarnett at mrabarnett.plus.com (Matthew Barnett) Date: Fri, 17 Jul 2009 18:24:42 +0100 Subject: Einstein summation notation In-Reply-To: <02708ac8$0$5185$c3e8da3@news.astraweb.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02708ac8$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A60B3DA.7010506@mrabarnett.plus.com> Steven D'Aprano wrote: > On Fri, 17 Jul 2009 16:09:03 +0100, MRAB wrote: > >> Python did always have True and False. Oops! I meant "didn't", of course. > > $ python1.5 > Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat > 4.1.2-27)] on linux2 > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>> True, False > Traceback (innermost last): > File "", line 1, in ? > NameError: True > From phily05 at gmail.com Fri Jul 17 13:28:13 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 10:28:13 -0700 (PDT) Subject: Propagate import for all modules in a package. Message-ID: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> I'm really new to Python and I am absolutely stumped trying to figure this out. I have searched plenty, but I am either searching for the wrong keywords or this isn't possible. What I want to do is have one import be global for the entire package. Here is an example... __init__.py module1.py module2.py ... moduleN.py I was thinking that I could just, for example, 'from datetime import datetime' in __init__.py and have the ability to use 'datetime' anywhere in any of the modules in 'package'. This didn't work for me. Did I just do something wrong? Is what I am trying to do possible? Thanks in advance. From Scott.Daniels at Acm.Org Fri Jul 17 13:37:25 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 17 Jul 2009 10:37:25 -0700 Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: akhil1988 wrote: > > Nobody-38 wrote: >> On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: ... >>>> In Python 3 you can't decode strings because they are Unicode strings >>>> and it doesn't make sense to decode a Unicode string. You can only >>>> decode encoded things which are byte strings. So you are mixing up byte >>>> strings and Unicode strings. >>> ... I read a byte string from sys.stdin which needs to converted to unicode >>> string for further processing. >> In 3.x, sys.stdin (stdout, stderr) are text streams, which means that they >> read and write Unicode strings, not byte strings. >> >>> I cannot just remove the decode statement and proceed? >>> This is it what it looks like: >>> for line in sys.stdin: >>> line = line.decode('utf-8').strip() >>> if line == '': #do something here >>> .... >>> If I remove the decode statement, line == '' never gets true. >> Did you inadvertently remove the strip() as well? > ... unintentionally I removed strip().... > I get this error now: > File "./temp.py", line 488, in > main() > File "./temp.py", line 475, in main > for line in sys.stdin: > File "/usr/local/lib/python3.1/codecs.py", line 300, in decode > (result, consumed) = self._buffer_decode(data, self.errors, final) > UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid > data (1) Do not top post. (2) Try to fully understand the problem and proposed solution, rather than trying to get people to tell you just enough to get your code going. (3) The only way sys.stdin can possibly return unicode is to do some decoding of its own. your job is to make sure it uses the correct decoding. So, if you know your source is always utf-8, try something like: import sys import io sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf8') for line in sys.stdin: line = line.strip() if line == '': #do something here .... --Scott David Daniels Scott.Daniels at Acm.Org From robert.kern at gmail.com Fri Jul 17 13:45:44 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 17 Jul 2009 12:45:44 -0500 Subject: Question regarding style/design.. In-Reply-To: <4A60B0D5.6030806@tim.thechases.com> References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> <4A60B0D5.6030806@tim.thechases.com> Message-ID: On 2009-07-17 12:11, Tim Chase wrote: >> Sometimes I see relatively small application, generally task scripts, >> written as essentially a list of statements. Other times, I see them >> neatly >> divided into functions and then the "if __name__ == '__main__':" >> convention. >> Is there a preference? Is there an... application scope such that the >> preference shifts from the former to the latter? I understand the use >> of the >> __name__ == 'main' convention for building unit tests, but I'm mixed on >> using it in scripts/small applications. > > This may circle around to the "is Python a scripting language" bruhaha a > while back. I tend to skip the __name__ == '__main__' in what I'd > consider scripts (one-file hacks to solve an immediate problem, often > disposable or one-use wonders). However for non-scripts (which I'd > define as code that has more than one source-code file I've written or > that I'd have to maintain) and for modules, I tend to put in the > __name__ guard. I definitely suggest always using the __name__ guard no matter what. Habits are important, and it's usually more of a waste of time trying to determine if you are writing a "script" or a "program" than it is to just write the clause all the time. One of the ex-DivMod guys (Glyph?) has a good blog post on the subject of writing docstrings and the Suzuki Method of violin; however, I cannot find it. With a good programmer's editor that can do macros or "snippets", this particular habit can be basically cost-free. There are also technical limitations to be aware of. Even if you don't think you will ever import the script, other tools might. For example, on Windows, multiprocessing needs to import the main module of the parent process in order to do some of its operations. If you do not have a __name__ guard, you will go into an infinite regress as your subprocesses execute the main code and create subsubprocesses until you finally manage to kill them all. Extra fun if it is a GUI and you have to click all of the windows closed. Don't waste half a day trying to figure out why your script mysteriously doesn't work. Learn from my mistakes. :-) -- 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 deets at nospam.web.de Fri Jul 17 13:52:30 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 17 Jul 2009 19:52:30 +0200 Subject: Propagate import for all modules in a package. In-Reply-To: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <7cbs2uF275m7mU1@mid.uni-berlin.de> Phil schrieb: > I'm really new to Python and I am absolutely stumped trying to figure > this out. I have searched plenty, but I am either searching for the > wrong keywords or this isn't possible. > > What I want to do is have one import be global for the entire package. > Here is an example... > > > __init__.py > module1.py > module2.py > ... > moduleN.py > > I was thinking that I could just, for example, 'from datetime import > datetime' in __init__.py and have the ability to use 'datetime' > anywhere in any of the modules in 'package'. > > This didn't work for me. Did I just do something wrong? Is what I am > trying to do possible? Each module has it's own namespace, there is no real global one. So there is no obvious and recommended way to do what you want. You can do it through, by stuffing names into the __builtins__-module, which acts as "namespace of last resort" from datetime import datetime __builtins__['datetime'] = datetime However, this is strongly discouraged - you can easily get name-conflicts, and undefined behavior in your modules if you rely on this. Diez From zaki.rahaman at gmail.com Fri Jul 17 13:58:20 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 10:58:20 -0700 (PDT) Subject: Generator Expressions and CSV Message-ID: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Hey all, I'm really new to Python and this may seem like a really dumb question, but basically, I wrote a script to do the following, however the processing time/memory usage is not what I'd like it to be. Any suggestions? Outline: 1. Read tab delim files from a directory, files are of 3 types: install, update, and q. All 3 types contain ID values that are the only part of interest. 2. Using set() and set.add(), generate a list of unique IDs from install and update files. 3. Using the set created in (2), check the q files to see if there are matches for IDs. Keep all matches, and add any non matches (which only occur once in the q file) to a queue of lines to be removed from teh q files. 4. Remove the lines in the q for each file. (I haven't quite written the code for this, but I was going to implement this using csv.writer and rewriting all the lines in the file except for the ones in the removal queue). Now, I've tried running this and it takes much longer than I'd like. I was wondering if there might be a better way to do things (I thought generator expressions might be a good way to attack this problem, as you could generate the set, and then check to see if there's a match, and write each line that way). From python at mrabarnett.plus.com Fri Jul 17 13:59:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 18:59:09 +0100 Subject: Find first matching substring In-Reply-To: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> References: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> Message-ID: <4A60BBED.7040501@mrabarnett.plus.com> Eloff wrote: > Almost every time I've had to do parsing of text over the last 5 years > I've needed this function: > > def find_first(s, subs, start=None, end=None): > results = [s.find(sub, start, end) for sub in subs] > results = [r for r in results if r != -1] > if results: > return min(results) > > return -1 > > It finds the first matching substring in the target string, if there > is more than one match, it returns the position of the match that > occurs at the lowest index in the string. > One possible optimisation for your code is to note that if you find that one of the substrings starts at a certain position then you're not interested in any subsequent substring which might start at or after that position, so you could reduce the search space for each substring found. > Has anyone else had problems where they could have applied this > function? > > It seems to me that python's find (and rfind, index, rindex) could be > modified (the same way that startswith and endswith have been) to > behave this way if a tuple were passed. Do other's agree that this > would be desirable? > Possibly. I think that allowing a tuple in the partition and rpartition methods might also be useful. From marduk at letterboxes.org Fri Jul 17 13:59:39 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Fri, 17 Jul 2009 13:59:39 -0400 Subject: Propagate import for all modules in a package. In-Reply-To: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <1247853579.17425.10.camel@centar> On Fri, 2009-07-17 at 10:28 -0700, Phil wrote: > I'm really new to Python and I am absolutely stumped trying to figure > this out. I have searched plenty, but I am either searching for the > wrong keywords or this isn't possible. > > What I want to do is have one import be global for the entire package. > Here is an example... > > > __init__.py > module1.py > module2.py > ... > moduleN.py > > I was thinking that I could just, for example, 'from datetime import > datetime' in __init__.py and have the ability to use 'datetime' > anywhere in any of the modules in 'package'. > > This didn't work for me. Did I just do something wrong? Is what I am > trying to do possible? > That's not how packages (were designed to) work. A package is basically a namespace. It doesn't do anything special outside of providing a namespace for common modules. There are some special features (e.g. __init__.py which is basically the "body" of the package and '__all__' which handles wildcard imports of a package) but other than that it's just a namespace. From python.list at tim.thechases.com Fri Jul 17 14:01:44 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 17 Jul 2009 13:01:44 -0500 Subject: Question regarding style/design.. In-Reply-To: References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> <4A60B0D5.6030806@tim.thechases.com> Message-ID: <4A60BC88.2070703@tim.thechases.com> Robert Kern wrote: [sage advice snipped] > Don't waste half a day trying to figure out why your script > mysteriously doesn't work. Learn from my mistakes. :-) I find that's the best type of mistake to learn from: other people's ;-) -tkc From hannarosie at gmail.com Fri Jul 17 14:06:27 2009 From: hannarosie at gmail.com (Hanna Michelsen) Date: Fri, 17 Jul 2009 11:06:27 -0700 Subject: Writing a single list of numbers to a .m (matlab) file Message-ID: <564970db0907171106q2be0852dg7209a4177f0b265a@mail.gmail.com> Hi, I was wondering if I could get some suggestions on how to write a single list of numbers to a .m file (for matlab) so that I can create a matlab vector out of the list of numbers from my python program. I have been using a csv writer to create .m files from lists of lists, but I'm not sure how to write just a single list to the file. (if I use the same code I've been using, I get an error: csv.Error sequence expected) Thanks for the help! -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Fri Jul 17 14:08:16 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 19:08:16 +0100 Subject: getopt code NameError exception on logTail.py In-Reply-To: References: Message-ID: <4A60BE10.5050905@mrabarnett.plus.com> LoD MoD wrote: > I am having trouble extending my option parsing. > Any help is appreciated: > > import sys > import getopt > import time > > def tail(file): > while 1: > where = file.tell() > line = file.readline() > if not line: > time.sleep(1) > file.seek(where) > else: > print line, # already has newline > > def main(): > > # parse command line options > try: > opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", > "filename="]) > except getopt.error, msg: > print msg > print "for help use --help" > sys.exit(2) > # process options > for o, a in opts: > if o in ("-h", "--help"): > print __doc__ > sys.exit(0) > if o in ("-f", "--filename"): > print "Parsing F argument" > file = open(filename, 'r') > print file > > > > # process arguments > for arg in args: > process(arg) # process() is defined elsewhere > > if __name__ == "__main__": > main() > > Yields this error: > > localhost:src gsery$ python logTail.py /var/log/system.log > Traceback (most recent call last): > File "logTail.py", line 52, in > main() > File "logTail.py", line 49, in main > process(arg) # process() is defined elsewhere > NameError: global name 'process' is not defined > The trackback tells you what's wrong: you haven't defined 'process'. The comment says it's defined elsewhere, but neither I nor Python can see it! :-) From emile at fenx.com Fri Jul 17 14:26:22 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 17 Jul 2009 11:26:22 -0700 Subject: Generator Expressions and CSV In-Reply-To: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Message-ID: On 7/17/2009 10:58 AM Zaki said... > Now, I've tried running this and it takes much longer than I'd like. I > was wondering if there might be a better way to do things Suppose, for the sake of argument, that you've written highly efficient code. Then the processing time would already be entirely optimized and no improvements possible. It's running as fast as it can. We can't help. On the other hand, maybe you didn't. In that case, you'll need to profile your code to determine where the time is consumed. At a minimum, you'll need to post the slow parts so we can see the implementation and suggest improvements. Emile From nick at craig-wood.com Fri Jul 17 14:29:59 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Fri, 17 Jul 2009 13:29:59 -0500 Subject: ctype performance benchmark References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> <48c15bac-8af2-472d-9bc0-1f19c4bf29cb@g7g2000prg.googlegroups.com> Message-ID: Wai Yip wrote: > I started with ctypes because it is the battery included with the > Python standard library. My code is very fluid and I'm looking for > easy opportunity to optimize it. One example is to find the longest > common prefix among two strings. Right now I am comparing it character > by character with pure Python. It seems like an ideal low hanging > fruit. With ctype I can rewriting a few lines of Python into a few > lines of C. All the tools are available and no third party library is > needed. Yes ctypes is very easy for this and I've used it like this in the past too. It makes interfacing C code very easy. You might consider just writing a plain bit of C code. If you make a setup.py too (not hard) then it is very easy to distribute and use on other platforms. The Python C API is very easy to use - documentation is extensive. You need to be very careful with references and the error checking is boring and tedious! > It turned out the performance fail to match pure Python. This function > is called million of times in an inner loop. The overhead overwhelm > any performance gain with C. Eventually I have found success grouping > the data into larger chunk for each API call. This is what I > originally planned to do anyway. I am only sharing my experience here > that doing fine grained ctype function call has its limitation. Interesting but not too unexpected - that translation of types is non-trivial. > I have looked into Pyrex before and I was quite confused by the fusion > of C and Python langauage. Perhaps it is time for me to give it a > second look. I just heard of Cython now and it also look interesting. > I think it is helpful for both project to articulate more clearly what > they are, how they achieve the performance gain and to give more > guidance on good use cases. On the other hand, perhaps it is just me > are confused because I don't know enough of the Python internal. Pyrex evolved/was forked into Cython. Cython allows you to write a subset of python code that is compiled into C. You can also optionally provide some type annotations to get it to produce better C code at the cost of making your code look less like python. I find Cython alternatively brilliant and frustrating! Sometimes it works really well, and sometimes I spend hours working out why my program doesn't compile, or to find out the exact syntax I need to interface with such and such a library. I did a C library interfacing project with both ctypes and cython recently as a comparison. It came out to be about the same number of lines of code for both. I decided to run with the ctypes version as it was part python. Here is a short Cython example which interfaces with opendir / closedir / readdir which python doesn't do natively. Note the way you interface with C structures (the real ones are used in the C code - the annotations just tell cython how to use them). Note also that Directory is a class defined in C which I don't think you can't do with ctypes without a wrapper class. This compiles into 1320 lines of C, which in turn compile into 11380 bytes of shared object (when stripped). import cython cdef extern from "dirent.h": struct dirent: char d_name[0] ctypedef struct DIR DIR *opendir(char *name) int closedir(DIR *dirp) dirent *readdir(DIR *dirp) cdef extern from "errno.h": int errno cdef extern from "string.h": char *strerror(int errnum) cdef class Directory: """Represents an open directory""" cdef DIR *handle def __init__(self, path): self.handle = opendir(path) if self.handle is NULL: raise OSError(errno, "Failed to open directory: %s" % strerror(errno)) def readdir(self): """Read the next name in the directory""" cdef dirent *p p = readdir(self.handle) if p is NULL: return None return p.d_name def close(self): """Close the directory""" if self.handle is not NULL: closedir(self.handle) self.handle = NULL def __dealloc__(self): self.close() -- Nick Craig-Wood -- http://www.craig-wood.com/nick From lodmod.dod at gmail.com Fri Jul 17 14:32:50 2009 From: lodmod.dod at gmail.com (LoD MoD) Date: Fri, 17 Jul 2009 11:32:50 -0700 Subject: getopt code NameError exception on logTail.py In-Reply-To: <4A60BE10.5050905@mrabarnett.plus.com> References: <4A60BE10.5050905@mrabarnett.plus.com> Message-ID: In this instance the trackback was somewhat unhelpful.There problem was here: file = open(filename, 'r') should be file = open(a, 'r') args should be passed within the getopt riff On Fri, Jul 17, 2009 at 11:08 AM, MRAB wrote: > LoD MoD wrote: > >> I am having trouble extending my option parsing. >> Any help is appreciated: >> >> import sys >> import getopt >> import time >> >> def tail(file): >> while 1: >> where = file.tell() >> line = file.readline() >> if not line: >> time.sleep(1) >> file.seek(where) >> else: >> print line, # already has newline >> >> def main(): >> >> # parse command line options >> try: >> opts, args = getopt.getopt(sys.argv[1:], "hf:", ["help", >> "filename="]) >> except getopt.error, msg: >> print msg >> print "for help use --help" >> sys.exit(2) >> # process options >> for o, a in opts: >> if o in ("-h", "--help"): >> print __doc__ >> sys.exit(0) >> if o in ("-f", "--filename"): >> print "Parsing F argument" >> file = open(filename, 'r') >> print file >> >> # process arguments >> for arg in args: >> process(arg) # process() is defined elsewhere >> >> if __name__ == "__main__": >> main() >> >> Yields this error: >> >> localhost:src gsery$ python logTail.py /var/log/system.log >> Traceback (most recent call last): >> File "logTail.py", line 52, in >> main() >> File "logTail.py", line 49, in main >> process(arg) # process() is defined elsewhere >> NameError: global name 'process' is not defined >> >> The trackback tells you what's wrong: you haven't defined 'process'. The > comment says it's defined elsewhere, but neither I nor Python can see > it! :-) > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From phily05 at gmail.com Fri Jul 17 14:42:28 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 11:42:28 -0700 (PDT) Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <663ab878-2ba2-45f1-a38e-2cff94a6bb00@k30g2000yqf.googlegroups.com> Thanks to both of you for the fast and detailed responses. I will just treat the package for what it is, a namespace. From phily05 at gmail.com Fri Jul 17 14:43:49 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 11:43:49 -0700 (PDT) Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <698e45d2-204b-46c0-ab80-8023e4b8ff15@y19g2000yqy.googlegroups.com> Thanks to both of you for the fast and detailed responses. I will just use the package for what it is, a namespace. From phily05 at gmail.com Fri Jul 17 14:44:42 2009 From: phily05 at gmail.com (Phil) Date: Fri, 17 Jul 2009 11:44:42 -0700 (PDT) Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <9314b787-558b-4891-b519-23228c6d8fb7@m11g2000yqh.googlegroups.com> Thanks to both of you for the fast and detailed responses. I will just use the package for what it is, a namespace. From python at mrabarnett.plus.com Fri Jul 17 14:49:32 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 19:49:32 +0100 Subject: Generator Expressions and CSV In-Reply-To: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Message-ID: <4A60C7BC.4080904@mrabarnett.plus.com> Zaki wrote: > Hey all, > > I'm really new to Python and this may seem like a really dumb > question, but basically, I wrote a script to do the following, however > the processing time/memory usage is not what I'd like it to be. Any > suggestions? > > > Outline: > 1. Read tab delim files from a directory, files are of 3 types: > install, update, and q. All 3 types contain ID values that are the > only part of interest. > 2. Using set() and set.add(), generate a list of unique IDs from > install and update files. > 3. Using the set created in (2), check the q files to see if there are > matches for IDs. Keep all matches, and add any non matches (which only > occur once in the q file) to a queue of lines to be removed from teh q > files. > 4. Remove the lines in the q for each file. (I haven't quite written > the code for this, but I was going to implement this using csv.writer > and rewriting all the lines in the file except for the ones in the > removal queue). > > Now, I've tried running this and it takes much longer than I'd like. I > was wondering if there might be a better way to do things (I thought > generator expressions might be a good way to attack this problem, as > you could generate the set, and then check to see if there's a match, > and write each line that way). > Why are you checking and removing lines in 2 steps? Why not copy the matching lines to a new q file and then replace the old file with the new one (or, maybe, delete the new q file if no lines were removed)? From smolds at comcast.net Fri Jul 17 14:51:11 2009 From: smolds at comcast.net (Stephen M. Olds) Date: Fri, 17 Jul 2009 11:51:11 -0700 Subject: Mechanize not recognized by py2exe Message-ID: <4A60C81F.7080707@comcast.net> An HTML attachment was scrubbed... URL: From duncan.booth at invalid.invalid Fri Jul 17 15:49:54 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 17 Jul 2009 19:49:54 GMT Subject: ctype performance benchmark References: <2b1ec74d-4e47-4eec-b128-bb00431ea651@y10g2000prg.googlegroups.com> <4a6037ea$0$31869$9b4e6d93@newsspool3.arcor-online.net> <48c15bac-8af2-472d-9bc0-1f19c4bf29cb@g7g2000prg.googlegroups.com> Message-ID: Wai Yip wrote: > I started with ctypes because it is the battery included with the > Python standard library. My code is very fluid and I'm looking for > easy opportunity to optimize it. One example is to find the longest > common prefix among two strings. Right now I am comparing it character > by character with pure Python. It seems like an ideal low hanging > fruit. With ctype I can rewriting a few lines of Python into a few > lines of C. All the tools are available and no third party library is > needed. Alternatively you could just use os.path.commonprefix(). From travis+ml-python at subspacefield.org Fri Jul 17 16:01:41 2009 From: travis+ml-python at subspacefield.org (travis+ml-python at subspacefield.org) Date: Fri, 17 Jul 2009 15:01:41 -0500 Subject: proposal: add setresuid() system call to python Message-ID: <20090717200141.GI6065@subspacefield.org> Hello, Historically, I have used scripting languages like python for typical uses, but they tend to not fare very well at system programming; for close interfacing with the operating system, I'm often forced to use a language like C. This is undesirable to me. I do not think this has to be the case; I see no reason why a scripting language can't implement more of the system call API, at the risk of having some OS-dependent modules. I would actually like to see more network servers written in scripting languages, as they neatly avoid buffer overflow and integer overflow issues with no extra effort. One BIG roadblock to doing this is when they can't manage to drop permissions properly. I am suggesting that the setresuid function be added to python, perhaps in the OS module, because it has the clearest semantics for manipulating user ids. The reason why is best described in the following paper: http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf One argument against this is that it is not specified by POSIX, and thus might be dismissed as "implementation dependent". However, as the paper above demonstrates, even though the setuid system call is defined by POSIX, it already has system-dependent behavior. POSIX provides for at least two different behaviors of the setuid call, and even more if you consider that it leaves what constitutes "appropriate privileges" up to the OS kernel. I humbly propose that python implement all the routines necessary to securely drop privileges, to enable construction of network daemons that might need to drop privileges from root to some non-root userid (e.g. mail transfer agents, or POP/IMAP servers). Furthermore, where there are multiple system calls to achieve this effect, it should implement the ones with the clearest semantics, and setresuid fits that bill. To see what an utter mess the uid-manipulation routines are in, I refer you once again to this paper, as the situation is too complicated to describe in this email: http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf Opinions? Best, Travis -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john at subspacefield.org to get blacklisted. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 850 bytes Desc: not available URL: From zaki.rahaman at gmail.com Fri Jul 17 16:08:05 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 13:08:05 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> Message-ID: <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> On Jul 17, 2:49?pm, MRAB wrote: > Zaki wrote: > > Hey all, > > > I'm really new to Python and this may seem like a really dumb > > question, but basically, I wrote a script to do the following, however > > the processing time/memory usage is not what I'd like it to be. Any > > suggestions? > > > Outline: > > 1. Read tab delim files from a directory, files are of 3 types: > > install, update, and q. All 3 types contain ID values that are the > > only part of interest. > > 2. Using set() and set.add(), generate a list of unique IDs from > > install and update files. > > 3. Using the set created in (2), check the q files to see if there are > > matches for IDs. Keep all matches, and add any non matches (which only > > occur once in the q file) to a queue of lines to be removed from teh q > > files. > > 4. Remove the lines in the q for each file. (I haven't quite written > > the code for this, but I was going to implement this using csv.writer > > and rewriting all the lines in the file except for the ones in the > > removal queue). > > > Now, I've tried running this and it takes much longer than I'd like. I > > was wondering if there might be a better way to do things (I thought > > generator expressions might be a good way to attack this problem, as > > you could generate the set, and then check to see if there's a match, > > and write each line that way). > > Why are you checking and removing lines in 2 steps? Why not copy the > matching lines to a new q file and then replace the old file with the > new one (or, maybe, delete the new q file if no lines were removed)? That's what I've done now. Here is the final code that I have running. It's very much 'hack' type code and not at all efficient or optimized and any help in optimizing it would be greatly appreciated. import csv import sys import os import time begin = time.time() #Check minutes elapsed def timeElapsed(): current = time.time() elapsed = current-begin return round(elapsed/60) #USAGE: python logcleaner.py inputdir = sys.argv[1] outputdir = sys.argv[2] logfilenames = os.listdir(inputdir) IDs = set() #IDs from update and install logs foundOnceInQuery = set() #foundTwiceInQuery = set() #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; Queue of IDs to remove from query logs (IDs found only once in query logs) #Generate Filename Queues For Install/Update Logs, Query Logs iNuQ = [] queryQ = [] for filename in logfilenames: if filename.startswith("par1.install") or filename.startswith ("par1.update"): iNuQ.append(filename) elif filename.startswith("par1.query"): queryQ.append(filename) totalfiles = len(iNuQ) + len(queryQ) print "Total # of Files to be Processed:" , totalfiles print "Install/Update Logs to be processed:" , len(iNuQ) print "Query logs to be processed:" , len(queryQ) #Process install/update queue to generate list of valid IDs currentfile = 1 for file in iNuQ: print "Processing", currentfile, "install/update log out of", len (iNuQ) print timeElapsed() reader = csv.reader(open(inputdir+file),delimiter = '\t') for row in reader: IDs.add(row[2]) currentfile+=1 print "Finished processing install/update logs" print "Unique IDs found:" , len(IDs) print "Total Time Elapsed:", timeElapsed() currentfile = 1 for file in queryQ: print "Processing", currentfile, "query log out of", len(queryQ) print timeElapsed() reader = csv.reader(open(inputdir+file), delimiter = '\t') outputfile = csv.writer(open(outputdir+file), 'w') for row in reader: if row[2] in IDs: ouputfile.writerow(row) else: if row[2] in foundOnceInQuery: foundOnceInQuery.remove(row[2]) outputfile.writerow(row) #IDremovalQ.remove(row[2]) #foundTwiceInQuery.add(row[2]) else: foundOnceInQuery.add(row[2]) #IDremovalQ.add(row[2]) currentfile+=1 print "Finished processing query logs and writing new files" print "# of Query log entries removed:" , len(foundOnceInQuery) print "Total Time Elapsed:", timeElapsed() From jcd at sdf.lonestar.org Fri Jul 17 16:08:26 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 17 Jul 2009 16:08:26 -0400 Subject: A Bug By Any Other Name ... In-Reply-To: References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: <1247861306.6362.6.camel@aalcdl07> On Fri, 2009-07-17 at 20:53 +0000, Albert van der Horst wrote: > Because unlike in algol 68 in python whitespace is relevant, > we could get by with requiring whitespace: > x= -q # okay > a 8 ** -2 # okay This is actually quite thoroughly untrue. In python, *indentation* is significant. Whitespace (internal to a line) is not. You can even call methods like this if you want: >>> s = 'abc' >>> s . upper() ABC Obviously, that's A Bad Idea(tm), but python's parser won't stop you. The ++ operator gotcha is so minor that I can't remember anyone actually asking about it on the list (who was actually facing it as a problem--this thread was started by idle speculation). Can we not change the language syntax to address non-issues? Practicality beats purity, a.k.a. don't you have something better to do? Cheers, Cliff From mabdelkader at gmail.com Fri Jul 17 16:15:00 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Fri, 17 Jul 2009 16:15:00 -0400 Subject: proposal: add setresuid() system call to python In-Reply-To: <20090717200141.GI6065@subspacefield.org> References: <20090717200141.GI6065@subspacefield.org> Message-ID: <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> Why don't you write a python extension module? This is a perfect opportunity for that. -- mahmoud mack abdelkader http://blog.mahmoudimus.com/ On Fri, Jul 17, 2009 at 4:01 PM, > wrote: > Hello, > > Historically, I have used scripting languages like python for typical > uses, but they tend to not fare very well at system programming; for > close interfacing with the operating system, I'm often forced to use a > language like C. This is undesirable to me. > > I do not think this has to be the case; I see no reason why a > scripting language can't implement more of the system call API, at the > risk of having some OS-dependent modules. I would actually like to > see more network servers written in scripting languages, as they > neatly avoid buffer overflow and integer overflow issues with no extra > effort. > > One BIG roadblock to doing this is when they can't manage to drop > permissions properly. > > I am suggesting that the setresuid function be added to python, > perhaps in the OS module, because it has the clearest semantics for > manipulating user ids. The reason why is best described in the > following paper: > > http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf > > One argument against this is that it is not specified by POSIX, and > thus might be dismissed as "implementation dependent". > > However, as the paper above demonstrates, even though the setuid > system call is defined by POSIX, it already has system-dependent > behavior. POSIX provides for at least two different behaviors of the > setuid call, and even more if you consider that it leaves what > constitutes "appropriate privileges" up to the OS kernel. > > I humbly propose that python implement all the routines necessary to > securely drop privileges, to enable construction of network daemons > that might need to drop privileges from root to some non-root userid > (e.g. mail transfer agents, or POP/IMAP servers). > > Furthermore, where there are multiple system calls to achieve this > effect, it should implement the ones with the clearest semantics, and > setresuid fits that bill. To see what an utter mess the uid-manipulation > routines are in, I refer you once again to this paper, as the situation > is too complicated to describe in this email: > > http://www.eecs.berkeley.edu/~daw/papers/setuid-usenix02.pdf > > Opinions? > > Best, > Travis > -- > Obama Nation | My emails do not have attachments; it's a digital signature > that your mail program doesn't understand. | > http://www.subspacefield.org/~travis/ > If you are a spammer, please email john at subspacefield.org to get > blacklisted. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.kern at gmail.com Fri Jul 17 16:15:35 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 17 Jul 2009 15:15:35 -0500 Subject: proposal: add setresuid() system call to python In-Reply-To: <20090717200141.GI6065@subspacefield.org> References: <20090717200141.GI6065@subspacefield.org> Message-ID: On 2009-07-17 15:01, travis+ml-python at subspacefield.org wrote: > Hello, > > Historically, I have used scripting languages like python for typical > uses, but they tend to not fare very well at system programming; for > close interfacing with the operating system, I'm often forced to use a > language like C. This is undesirable to me. You can use ctypes for most of these use cases. -- 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 tjreedy at udel.edu Fri Jul 17 16:32:51 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 16:32:51 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: Peter Otten wrote: > Terry Reedy wrote: > >> I tried it. Unfortunately, OOo does not open it correctly. It just >> displays the first three lines of metadate - Title, Creator, Date -- as >> image text. Photoshop does read the image, and does an ok job of >> conversion once anti-aliasing is turned off. > > I snatched some code from the module and modified it to save a sample image: Thank you for the example. > > from turtle import * > > def switchpen(): > if isdown(): > pu() > else: > pd() > > def demo2(): > """Demo of some new features.""" > speed(1) > st() > pensize(3) > setheading(towards(0, 0)) > radius = distance(0, 0)/2.0 > rt(90) > for _ in range(18): > switchpen() > circle(radius, 10) > write("wait a moment...") > while undobufferentries(): > undo() > reset() > lt(90) > colormode(255) > laenge = 10 > pencolor("green") > pensize(3) > lt(180) > for i in range(-2, 16): > if i > 0: > begin_fill() > fillcolor(255-15*i, 0, 15*i) > for _ in range(3): > fd(laenge) > lt(120) > laenge += 10 > lt(15) > speed((speed()+1)%12) > end_fill() > > lt(120) > pu() > fd(70) > rt(30) > pd() > color("red","yellow") > speed(0) > fill(1) begin_fill() in 3.x > for _ in range(4): > circle(50, 90) > rt(90) > fd(30) > rt(90) > fill(0) end_fill() in 3.x > lt(90) > pu() > fd(30) > pd() > shape("turtle") > > tri = getturtle() > tri.resizemode("auto") > turtle = Turtle() > turtle.resizemode("auto") > turtle.shape("turtle") > turtle.reset() > turtle.left(90) > turtle.speed(0) > turtle.up() > turtle.goto(280, 40) > turtle.lt(30) > turtle.down() > turtle.speed(6) > turtle.color("blue","orange") > turtle.pensize(2) > tri.speed(6) > setheading(towards(turtle)) > count = 1 > while tri.distance(turtle) > 4: > turtle.fd(3.5) > turtle.lt(0.6) > tri.setheading(tri.towards(turtle)) > tri.fd(4) > if count % 20 == 0: > turtle.stamp() > tri.stamp() > switchpen() > count += 1 > tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right") > > if __name__ == "__main__": > demo2() > Screen().getcanvas().postscript(file="demo2.eps") > > I could successfully insert the picture into Writer. Whereas I still get just three lines of metadata. The size of my demo2.eps is given as 65,628 bytes. 'Size on disk is given as a bit more but I presume that counts to the end of the last 4k block. > I'm on Kubuntu 9.04 with Python 2.6.2 and OpenOffice 3.0.1. WinXP with updates, Python 3.1, and OO 3.1.0 So either tk 8.5(?) on windows is producing something different or OO3.1 on windows is reading differently. If your file size is different, could you email it so I could try to import it here? > But still, turtle is an educational tool, its main advantage is that it can > show beginners how the image is generated. It is also available with Python as installed. And I want to do simple animations (of algoritms) as well as static pictures. > If you want to generate high-quality graphics easily you need different > primitives. > > http://cairographics.org > > has Python bindings, but I don't know if it's available on Windows. Effectively not, as far as I can tell. PyCairo appears to be *nix and require later binaries than those available from gtk site referenced from cairo site. Terry Jan Reedy From tjreedy at udel.edu Fri Jul 17 16:36:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 16:36:17 -0400 Subject: turtle dump In-Reply-To: <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> Message-ID: alex23 wrote: > The help in iPython says the same, but also mentions that it's a > dynamically generated function, so it may not be picking up the > docstring that way. turtle.ScrolledCanvas.postscript is similarly > terse, but you can find more info in turtle.Canvas.postscript: > > Print the contents of the canvas to a postscript > file. Valid options: colormap, colormode, file, fontmap, > height, pageanchor, pageheight, pagewidth, pagex, pagey, > rotate, witdh, x, y. How, exactly, did you get that list? tjr From piet at cs.uu.nl Fri Jul 17 16:48:37 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 22:48:37 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: >>>>> Gabriel Rossetti (GR) wrote: >GR> Piet van Oostrum wrote: >>>>>>>> Gabriel Rossetti (GR) wrote: >>>>>>>> >>> >>> >GR> I have a 1-1 relation, I have a thread reading msgs from a network socket >GR> and a method waiting for an answer to arrive (see my thread >GR> "Threading.Condition problem"). I would like to be able to have a timeout >GR> as to not block forever, so the idea is to check if I returned because of a >GR> timeout or not. >>>> >>> >>> So that case is easy I think. After the wait just check if the answer >>> has arrived. >>> >GR> The problem is other answers could arrive that are not the one I >GR> want. Maybe I could check if I got the answer, if not then check >GR> how much time has elapsed, and if it's grater or equal to the >GR> timeout consider it a timeout. I still think a return value would >GR> be better though. The wait will not get a return value in Python. There was an issue posted in the tracker to request this in 2005. Recently (after 4 years) it was rejected by the BDFL. See http://bugs.python.org/issue1175933 OK, your use case might be a bit more difficult, but I still think it can be solved without a return value. I can't tell exactly how because of lack of information. So (as I already mentioned in one of my previous messages) being notified doesn't imply that everything is OK. Now suppose that your CV is notified one microsecond before it would time out but the required answer did not arrive. What's the difference with a real timeout except for that microsecond? Reimplementing Condition yourself is not to be recommended I think. Using these synchronisation primitives is hard enough let alone implementing them. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From albert at spenarnc.xs4all.nl Fri Jul 17 16:53:36 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 17 Jul 2009 20:53:36 GMT Subject: A Bug By Any Other Name ... References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> Message-ID: In article , Gabriel Genellina wrote: >En Mon, 06 Jul 2009 00:28:43 -0300, Steven D'Aprano > escribi?: >> On Mon, 06 Jul 2009 14:32:46 +1200, Lawrence D'Oliveiro wrote: >> >>> I wonder how many people have been tripped up by the fact that >>> >>> ++n >>> >>> and >>> >>> --n >>> >>> fail silently for numeric-valued n. >> >> What do you mean, "fail silently"? They do exactly what you should >> expect: >>>>> ++5 # positive of a positive number is positive >> >> I'm not sure what "bug" you're seeing. Perhaps it's your expectations >> that are buggy, not Python. > >Well, those expectations are taken seriously when new features are >introduced into the language - and sometimes the feature is dismissed just >because it would be confusing for some. >If a += 1 works, expecting ++a to have the same meaning is very reasonable >(for those coming from languages with a ++ operator, like C or Java) - >more when ++a is a perfectly valid expression. >If this issue isn't listed under the various "Python gotchas" articles, it >should... In algol 68 there was one thing strictly forbidden: putting two operators behind each other: x := y** -b .comment must be x := y**(-b) .comment This is quite a sensible rule, especially when, like in Python, two special characters can be run together to denote a different operator. Examples abound : +:= ** A consequence is that 'a*-b' would be illegal. It would become 'a*(-b)' Worse is that x=-q would be illegal. Because unlike in algol 68 in python whitespace is relevant, we could get by with requiring whitespace: x= -q # okay a-- >Gabriel Genellina Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From clp2 at rebertia.com Fri Jul 17 16:55:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 17 Jul 2009 13:55:20 -0700 Subject: Writing a single list of numbers to a .m (matlab) file In-Reply-To: <564970db0907171106q2be0852dg7209a4177f0b265a@mail.gmail.com> References: <564970db0907171106q2be0852dg7209a4177f0b265a@mail.gmail.com> Message-ID: <50697b2c0907171355q76f7c538le319b2240bb4210e@mail.gmail.com> On Fri, Jul 17, 2009 at 11:06 AM, Hanna Michelsen wrote: > Hi, > > I was wondering if I could get some suggestions on how to write a single > list of numbers to a .m file (for matlab) so that I can create a matlab > vector out of the list of numbers from my python program. I have been using > a csv writer to create .m files from lists of lists, but I'm not sure how to > write just a single list to the file. (if I use the same code I've been > using, I get an error: csv.Error sequence expected) You didn't provide any code, but since csv is probably overkill anyway: m_file.write("[" + " ".join(str(num) for num in numbers) + "]\n") Cheers, Chris -- http://blog.rebertia.com From python at mrabarnett.plus.com Fri Jul 17 16:57:46 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 21:57:46 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: <1247861306.6362.6.camel@aalcdl07> References: <006d4a50$0$9711$c3e8da3@news.astraweb.com> <1247861306.6362.6.camel@aalcdl07> Message-ID: <4A60E5CA.1000106@mrabarnett.plus.com> J. Cliff Dyer wrote: > On Fri, 2009-07-17 at 20:53 +0000, Albert van der Horst wrote: >> Because unlike in algol 68 in python whitespace is relevant, >> we could get by with requiring whitespace: >> x= -q # okay >> a> 8 ** -2 # okay > > This is actually quite thoroughly untrue. In python, *indentation* is > significant. Whitespace (internal to a line) is not. [snip] Whitespace (internal to a line) _mostly_ is not. It's not allowed within names, and it is needed in the second example above, before the 'and's. From exarkun at divmod.com Fri Jul 17 16:59:53 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Fri, 17 Jul 2009 16:59:53 -0400 Subject: proposal: add setresuid() system call to python In-Reply-To: <20090717200141.GI6065@subspacefield.org> Message-ID: <20090717205953.2543.1223422516.divmod.quotient.3129@henry.divmod.com> On Fri, 17 Jul 2009 15:01:41 -0500, travis+ml-python at subspacefield.org wrote: >Hello, > > [snip] > >I am suggesting that the setresuid function be added to python, >perhaps in the OS module, because it has the clearest semantics for >manipulating user ids. The reason why is best described in the >following paper: Yes, it'd be good for Python to expose setresuid. The best course of action is to file a ticket in the issue tracker. Things will be sped along if you also attach a patch implementing the change. :) Jean-Paul From piet at cs.uu.nl Fri Jul 17 16:59:54 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 17 Jul 2009 22:59:54 +0200 Subject: no return value for threading.Condition.wait(timeout)? References: Message-ID: I should add that *if the wait times out* it still could be the case that your answer has arrived but that the notify occured a microsecond after the timeout. So knowing if the timeout occurred really doesn't tell you much. It's just a way to prevent infinite waiting. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From tjreedy at udel.edu Fri Jul 17 16:59:59 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 16:59:59 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: References: Message-ID: Sion Arrowsmith wrote: > Jack Diederich wrote: >> It isn't an OrderedDict thing, it is a comparison thing. Two regular >> dicts also raise an error if you try to LT them. > > Since when? > > Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> d1 = dict((str(i), i) for i in range (10)) >>>> d2 = dict((str(i), i) for i in range (20)) >>>> d1 < d2 > True Try reversing the definitions of d1 and d2. The dicts are probably being compared by id (address), which is the 2.x CPython default. > (Don't have a 2.6 or 3 to hand.) 2.6 same. 3.1 >>> d1,d2 = {},{} >>> d1 < d2 Traceback (most recent call last): File "", line 1, in d1 < d2 TypeError: unorderable types: dict() < dict() From aahz at pythoncraft.com Fri Jul 17 17:03:59 2009 From: aahz at pythoncraft.com (Aahz) Date: 17 Jul 2009 14:03:59 -0700 Subject: Passing python list from C to python References: <68dd04fa-6f62-4cb9-807d-ec12d7216fd7@n4g2000vba.googlegroups.com> <0afc5c4d-1af5-4d0e-9442-26b51b12e5b4@m11g2000yqh.googlegroups.com> <94c1adf3-a25d-4ae5-9f38-7ca8680d022d@b14g2000yqd.googlegroups.com> Message-ID: In article <94c1adf3-a25d-4ae5-9f38-7ca8680d022d at b14g2000yqd.googlegroups.com>, hartley wrote: >On Jul 16, 9:26=A0pm, a... at pythoncraft.com (Aahz) wrote: >> In article <0afc5c4d-1af5-4d0e-9442-26b51b12e... at m11g2000yqh.googlegroups= >.com>, >> hartley =A0 wrote: >>> >>>If you had loosened up on the sarcasm I would probably have read what >>>you wrote more thoroughly instead of just skimming through it. Thanks >>>for the help, but you should seriously consider doing something with >>>your patronizing attitude. >> >> http://www.mikeash.com/getting_answers.htmlhttp://www.catb.org/~esr/faqs/= >smart-questions.html > >From "http://www.mikeash.com/getting_answers.html": > >How To Ask Questions The Smart Way is pretty condescending, especially >if you already feel insulted by somebody telling you that you need >help asking questions. If you're pointed at a guide with a filename of >smart-questions, that means this person thinks you have stupid >questions, and who needs that? > >Cheers mate :) Not really. Feel free to chide people trying to help you, eventually people won't care. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair From tjreedy at udel.edu Fri Jul 17 17:09:40 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 17 Jul 2009 17:09:40 -0400 Subject: 'del' function for Dictionary In-Reply-To: References: Message-ID: mayank gupta wrote: > Hi all, > > I wanted to know whether there is a more efficient way to delete an > entry from a dictionary (instead of using the 'del' function), because > after analyzing the time taken by the code, it seems to me that the > 'del' function takes most of the time. That is hard to imagine. del ob[sub] has the same effect as ob.__delitem__(sub) but I suspect this would take longer since it requires a runtime attribute lookup and Python level function call. So I am pretty sure the answer is 'no'. tjr From python at mrabarnett.plus.com Fri Jul 17 17:31:30 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 17 Jul 2009 22:31:30 +0100 Subject: Generator Expressions and CSV In-Reply-To: <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: <4A60EDB2.5050202@mrabarnett.plus.com> Zaki wrote: > On Jul 17, 2:49 pm, MRAB wrote: >> Zaki wrote: >>> Hey all, >>> I'm really new to Python and this may seem like a really dumb >>> question, but basically, I wrote a script to do the following, however >>> the processing time/memory usage is not what I'd like it to be. Any >>> suggestions? >>> Outline: >>> 1. Read tab delim files from a directory, files are of 3 types: >>> install, update, and q. All 3 types contain ID values that are the >>> only part of interest. >>> 2. Using set() and set.add(), generate a list of unique IDs from >>> install and update files. >>> 3. Using the set created in (2), check the q files to see if there are >>> matches for IDs. Keep all matches, and add any non matches (which only >>> occur once in the q file) to a queue of lines to be removed from teh q >>> files. >>> 4. Remove the lines in the q for each file. (I haven't quite written >>> the code for this, but I was going to implement this using csv.writer >>> and rewriting all the lines in the file except for the ones in the >>> removal queue). >>> Now, I've tried running this and it takes much longer than I'd like. I >>> was wondering if there might be a better way to do things (I thought >>> generator expressions might be a good way to attack this problem, as >>> you could generate the set, and then check to see if there's a match, >>> and write each line that way). >> Why are you checking and removing lines in 2 steps? Why not copy the >> matching lines to a new q file and then replace the old file with the >> new one (or, maybe, delete the new q file if no lines were removed)? > > That's what I've done now. > > Here is the final code that I have running. It's very much 'hack' type > code and not at all efficient or optimized and any help in optimizing > it would be greatly appreciated. > > import csv > import sys > import os > import time > > begin = time.time() > > #Check minutes elapsed > def timeElapsed(): > current = time.time() > elapsed = current-begin > return round(elapsed/60) > > > #USAGE: python logcleaner.py > > inputdir = sys.argv[1] > outputdir = sys.argv[2] > > logfilenames = os.listdir(inputdir) > > > > IDs = set() #IDs from update and install logs > foundOnceInQuery = set() > #foundTwiceInQuery = set() > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > Queue of IDs to remove from query logs (IDs found only once in query > logs) > > #Generate Filename Queues For Install/Update Logs, Query Logs > iNuQ = [] > queryQ = [] > > for filename in logfilenames: > if filename.startswith("par1.install") or filename.startswith > ("par1.update"): if filename.startswith(("par1.install", "par1.update")): > iNuQ.append(filename) > elif filename.startswith("par1.query"): > queryQ.append(filename) > > totalfiles = len(iNuQ) + len(queryQ) > print "Total # of Files to be Processed:" , totalfiles > print "Install/Update Logs to be processed:" , len(iNuQ) > print "Query logs to be processed:" , len(queryQ) > > #Process install/update queue to generate list of valid IDs > currentfile = 1 > for file in iNuQ: > print "Processing", currentfile, "install/update log out of", len > (iNuQ) > print timeElapsed() > reader = csv.reader(open(inputdir+file),delimiter = '\t') > for row in reader: > IDs.add(row[2]) > currentfile+=1 Best not to call it 'file'; that's a built-in name. Also you could use 'enumerate', and joining filepaths is safer with os.path.join(). for currentfile, filename in enumerate(iNuQ, start=1): print "Processing", currentfile, "install/update log out of", len(iNuQ) print timeElapsed() current_path = os.path.join(inputdir, filename) reader = csv.reader(open(current_path), delimiter = '\t') for row in reader: IDs.add(row[2]) > > print "Finished processing install/update logs" > print "Unique IDs found:" , len(IDs) > print "Total Time Elapsed:", timeElapsed() > > currentfile = 1 > for file in queryQ: Similar remarks to above ... > print "Processing", currentfile, "query log out of", len(queryQ) > print timeElapsed() > reader = csv.reader(open(inputdir+file), delimiter = '\t') > outputfile = csv.writer(open(outputdir+file), 'w') ... and also here. > for row in reader: > if row[2] in IDs: > ouputfile.writerow(row) Should be 'outputfile'. > else: > if row[2] in foundOnceInQuery: > foundOnceInQuery.remove(row[2]) You're removing the ID here ... > outputfile.writerow(row) > #IDremovalQ.remove(row[2]) > #foundTwiceInQuery.add(row[2]) > > else: > foundOnceInQuery.add(row[2]) ... and adding it again here! > #IDremovalQ.add(row[2]) > > > currentfile+=1 > For safety you should close the files after use. > print "Finished processing query logs and writing new files" > print "# of Query log entries removed:" , len(foundOnceInQuery) > print "Total Time Elapsed:", timeElapsed() > Apart from that, it looks OK. How big are the q files? If they're not too big and most of the time you're not removing rows, you could put the output rows into a list and then create the output file only if rows have been removed, otherwise just copy the input file, which might be faster. From emile at fenx.com Fri Jul 17 17:56:12 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 17 Jul 2009 14:56:12 -0700 Subject: Generator Expressions and CSV In-Reply-To: <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: On 7/17/2009 1:08 PM Zaki said... > Here is the final code that I have running. It's very much 'hack' type > code and not at all efficient or optimized and any help in optimizing > it would be greatly appreciated. There are some things I'd approach differently , eg I might prefer glob to build iNuQ and queryQ [1], and although glob is generally fast, I'm not sure it'd be faster. But overall it looks like most of the time is spent in your 'for row in' loops, and as you're reading each file only once, and would have to anyway, there's not much that'll improve overall timing. I don't know what csvreader is doing under the covers, but if your files are reasonably sized for your system you might try timing something that reads in the full file and splits: for each in filelist: for row in open(filelist).readlines(): if row.split()[2] in .... -----[1]----- import glob iNuQ = glob.glob(os.sep.join(inputdir,"par1.install*") queryQ = glob.glob(os.sep.join(inputdir,"par1.query*") Emile From lie.1296 at gmail.com Fri Jul 17 17:58:21 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 18 Jul 2009 07:58:21 +1000 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: Xavier Ho wrote: > oops, wrong address. > > When will reply-to tag appear on the Python mailing list? =/ > > Good idea, Steven and MRAB. > > I changed my code to the following: > > > def nPrime(self, n): > "Returns nth prime number, the first one being 2, where n = 0. > When n = 1, it returns 3." > for x in range(n+2): > > try: > return self.primes[n] > except IndexError: > self.next() If you're looking for speed, you should be aware that a failed try-clause is quite expensive in python and often (as in this case) codes using exception can be less readable than the one without since a program's flow in the case of exception is not linear (the non-linear execution is useful for quickly breaking out from nests of clauses, but not necessary in this case). Semantically, it is also wrong. Not finding the n'th prime in the cache is not really an exceptional case. If you insists on using a try-except clause, use it this way: # untested def nPrime(self, n): try: return self.primes[n] except IndexError: for _ in xrange(len(self.primes), n+1): self.next() return self.primes[n] From lodmod.dod at gmail.com Fri Jul 17 17:58:46 2009 From: lodmod.dod at gmail.com (LoD MoD) Date: Fri, 17 Jul 2009 14:58:46 -0700 Subject: creating my own logwatch in python Message-ID: I am trying to fabricate a logwatch-type script in Python and I'm having some trouble.What happens with the following code is that it prints to the screen but the if condition on line 22 never gets executed. http://pastie.org/549975 Any suggestions are appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 17 18:07:13 2009 From: contact at xavierho.com (Xavier Ho) Date: Sat, 18 Jul 2009 06:07:13 +0800 Subject: Try... except....Try again? In-Reply-To: References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> Message-ID: <2d56febf0907171507y75f6436ete75cb3f748ee838d@mail.gmail.com> On Sat, Jul 18, 2009 at 5:58 AM, Lie Ryan wrote: > > > If you're looking for speed, you should be aware that a failed > try-clause is quite expensive in python and often (as in this case) > codes using exception can be less readable than the one without since a > program's flow in the case of exception is not linear (the non-linear > execution is useful for quickly breaking out from nests of clauses, but > not necessary in this case). Was not aware that myself. Noted. > Semantically, it is also wrong. Not finding the n'th prime in the cache > is not really an exceptional case. > > If you insists on using a try-except clause, use it this way: > > Well, I don't have to use try-except. I think I got another idea though. I could use an internal counter to keep track of how many prime numbers have generated, and if not enough, generate more. Thanks. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From mabdelkader at gmail.com Fri Jul 17 18:33:55 2009 From: mabdelkader at gmail.com (Mahmoud Abdelkader) Date: Fri, 17 Jul 2009 18:33:55 -0400 Subject: creating my own logwatch in python In-Reply-To: References: Message-ID: <148918f0907171533y4044b171h54e91c142a21b6cf@mail.gmail.com> A few code critiques: - Your code is not very help friendly because it can not be run, so I have to deadlist debug it. - There are various syntax errors in the code: - for example: file is not defined when you invoke tail(file) - You are overshadowing a built-in type 'file' by that name, which is confusing. PEP 8 states that if you want to use a keyword, the practice is to put an underscore after it. So, it should become file_ - Getopt is very old and should not be used. You should use the better, readable, optparse - That while loop will consume 99% CPU because of the infinite loop - line = file.readline() is guaranteed to return a newline OR an empty string ONLY when an EOF is encountered since you're not specifying the optional maximum read byte size. This means that all your characters are strings. So, why do you explicitly convert each character to a string and separate each character by a space? (line 18) - Line 19 will always return 0 because of line 18 - Line 19->22 are completely redundant, since readline is guaranteed to give you ONE line. You should just do if "uccess" in logString: ... Hope this helps, mahmoud mack abdelkader http://blog.mahmoudimus.com/ On Fri, Jul 17, 2009 at 5:58 PM, LoD MoD wrote: > I am trying to fabricate a logwatch-type script in Python and I'm having > some trouble.What happens with the following code is that it prints to the > screen but the if condition on line 22 never gets executed. > > http://pastie.org/549975 > > Any suggestions are appreciated. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zaki.rahaman at gmail.com Fri Jul 17 18:39:33 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 15:39:33 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: <1e8faacc-f4bc-420a-8028-88e405abb81f@d4g2000yqa.googlegroups.com> On Jul 17, 5:31?pm, MRAB wrote: > Zaki wrote: > > On Jul 17, 2:49 pm, MRAB wrote: > >> Zaki wrote: > >>> Hey all, > >>> I'm really new to Python and this may seem like a really dumb > >>> question, but basically, I wrote a script to do the following, however > >>> the processing time/memory usage is not what I'd like it to be. Any > >>> suggestions? > >>> Outline: > >>> 1. Read tab delim files from a directory, files are of 3 types: > >>> install, update, and q. All 3 types contain ID values that are the > >>> only part of interest. > >>> 2. Using set() and set.add(), generate a list of unique IDs from > >>> install and update files. > >>> 3. Using the set created in (2), check the q files to see if there are > >>> matches for IDs. Keep all matches, and add any non matches (which only > >>> occur once in the q file) to a queue of lines to be removed from teh q > >>> files. > >>> 4. Remove the lines in the q for each file. (I haven't quite written > >>> the code for this, but I was going to implement this using csv.writer > >>> and rewriting all the lines in the file except for the ones in the > >>> removal queue). > >>> Now, I've tried running this and it takes much longer than I'd like. I > >>> was wondering if there might be a better way to do things (I thought > >>> generator expressions might be a good way to attack this problem, as > >>> you could generate the set, and then check to see if there's a match, > >>> and write each line that way). > >> Why are you checking and removing lines in 2 steps? Why not copy the > >> matching lines to a new q file and then replace the old file with the > >> new one (or, maybe, delete the new q file if no lines were removed)? > > > That's what I've done now. > > > Here is the final code that I have running. It's very much 'hack' type > > code and not at all efficient or optimized and any help in optimizing > > it would be greatly appreciated. > > > import csv > > import sys > > import os > > import time > > > begin = time.time() > > > #Check minutes elapsed > > def timeElapsed(): > > ? ? current = time.time() > > ? ? elapsed = current-begin > > ? ? return round(elapsed/60) > > > #USAGE: python logcleaner.py > > > inputdir = sys.argv[1] > > outputdir = sys.argv[2] > > > logfilenames = os.listdir(inputdir) > > > IDs = set() #IDs from update and install logs > > foundOnceInQuery = set() > > #foundTwiceInQuery = set() > > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > > Queue of IDs to remove from query logs (IDs found only once in query > > logs) > > > #Generate Filename Queues For Install/Update Logs, Query Logs > > iNuQ = [] > > queryQ = [] > > > for filename in logfilenames: > > ? ? if filename.startswith("par1.install") or filename.startswith > > ("par1.update"): > > ? ? ? if filename.startswith(("par1.install", "par1.update")): > > > ? ? ? ? iNuQ.append(filename) > > ? ? elif filename.startswith("par1.query"): > > ? ? ? ? queryQ.append(filename) > > > totalfiles = len(iNuQ) + len(queryQ) > > print "Total # of Files to be Processed:" , totalfiles > > print "Install/Update Logs to be processed:" , len(iNuQ) > > print "Query logs to be processed:" , len(queryQ) > > > #Process install/update queue to generate list of valid IDs > > currentfile = 1 > > for file in iNuQ: > > ?> ? ? print "Processing", currentfile, "install/update log out of", len > ?> (iNuQ) > ?> ? ? print timeElapsed() > ?> ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > ?> ? ? for row in reader: > ?> ? ? ? ? IDs.add(row[2]) > ?> ? ? currentfile+=1 > > Best not to call it 'file'; that's a built-in name. > > Also you could use 'enumerate', and joining filepaths is safer with > os.path.join(). > > for currentfile, filename in enumerate(iNuQ, start=1): > ? ? ?print "Processing", currentfile, "install/update log out of", len(iNuQ) > ? ? ?print timeElapsed() > ? ? ?current_path = os.path.join(inputdir, filename) > ? ? ?reader = csv.reader(open(current_path), delimiter = '\t') > ? ? ?for row in reader: > ? ? ? ? ?IDs.add(row[2]) > > > > > print "Finished processing install/update logs" > > print "Unique IDs found:" , len(IDs) > > print "Total Time Elapsed:", timeElapsed() > > > currentfile = 1 > > for file in queryQ: > > Similar remarks to above ... > > > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > > ? ? print timeElapsed() > > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > > ... and also here. > > > ? ? for row in reader: > > ? ? ? ? if row[2] in IDs: > > ? ? ? ? ? ? ouputfile.writerow(row) > > Should be 'outputfile'. > > > ? ? ? ? else: > > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > > You're removing the ID here ... > > > ? ? ? ? ? ? ? ? outputfile.writerow(row) > > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > > ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > > ... and adding it again here! > > > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > > ? ? currentfile+=1 > > For safety you should close the files after use. > > > print "Finished processing query logs and writing new files" > > print "# of Query log entries removed:" , len(foundOnceInQuery) > > print "Total Time Elapsed:", timeElapsed() > > Apart from that, it looks OK. > > How big are the q files? If they're not too big and most of the time > you're not removing rows, you could put the output rows into a list and > then create the output file only if rows have been removed, otherwise > just copy the input file, which might be faster. MRAB, could you please repost what I sent to you here as I meant to post it in the main discussion. From joncle at googlemail.com Fri Jul 17 18:40:27 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 17 Jul 2009 15:40:27 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> Message-ID: <51ce01d3-4620-4e5f-b5b5-bd0e9fd6fd2c@w41g2000yqb.googlegroups.com> On 17 July, 21:08, Zaki wrote: > On Jul 17, 2:49?pm, MRAB wrote: > > > > > Zaki wrote: > > > Hey all, > > > > I'm really new to Python and this may seem like a really dumb > > > question, but basically, I wrote a script to do the following, however > > > the processing time/memory usage is not what I'd like it to be. Any > > > suggestions? > > > > Outline: > > > 1. Read tab delim files from a directory, files are of 3 types: > > > install, update, and q. All 3 types contain ID values that are the > > > only part of interest. > > > 2. Using set() and set.add(), generate a list of unique IDs from > > > install and update files. > > > 3. Using the set created in (2), check the q files to see if there are > > > matches for IDs. Keep all matches, and add any non matches (which only > > > occur once in the q file) to a queue of lines to be removed from teh q > > > files. > > > 4. Remove the lines in the q for each file. (I haven't quite written > > > the code for this, but I was going to implement this using csv.writer > > > and rewriting all the lines in the file except for the ones in the > > > removal queue). > > > > Now, I've tried running this and it takes much longer than I'd like. I > > > was wondering if there might be a better way to do things (I thought > > > generator expressions might be a good way to attack this problem, as > > > you could generate the set, and then check to see if there's a match, > > > and write each line that way). > > > Why are you checking and removing lines in 2 steps? Why not copy the > > matching lines to a new q file and then replace the old file with the > > new one (or, maybe, delete the new q file if no lines were removed)? > > That's what I've done now. > > Here is the final code that I have running. It's very much 'hack' type > code and not at all efficient or optimized and any help in optimizing > it would be greatly appreciated. > > import csv > import sys > import os > import time > > begin = time.time() > > #Check minutes elapsed > def timeElapsed(): > ? ? current = time.time() > ? ? elapsed = current-begin > ? ? return round(elapsed/60) > > #USAGE: python logcleaner.py > > inputdir = sys.argv[1] > outputdir = sys.argv[2] > > logfilenames = os.listdir(inputdir) > > IDs = set() #IDs from update and install logs > foundOnceInQuery = set() > #foundTwiceInQuery = set() > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > Queue of IDs to remove from query logs (IDs found only once in query > logs) > > #Generate Filename Queues For Install/Update Logs, Query Logs > iNuQ = [] > queryQ = [] > > for filename in logfilenames: > ? ? if filename.startswith("par1.install") or filename.startswith > ("par1.update"): > ? ? ? ? iNuQ.append(filename) > ? ? elif filename.startswith("par1.query"): > ? ? ? ? queryQ.append(filename) > > totalfiles = len(iNuQ) + len(queryQ) > print "Total # of Files to be Processed:" , totalfiles > print "Install/Update Logs to be processed:" , len(iNuQ) > print "Query logs to be processed:" , len(queryQ) > > #Process install/update queue to generate list of valid IDs > currentfile = 1 > for file in iNuQ: > ? ? print "Processing", currentfile, "install/update log out of", len > (iNuQ) > ? ? print timeElapsed() > ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > ? ? for row in reader: > ? ? ? ? IDs.add(row[2]) > ? ? currentfile+=1 > > print "Finished processing install/update logs" > print "Unique IDs found:" , len(IDs) > print "Total Time Elapsed:", timeElapsed() > > currentfile = 1 > for file in queryQ: > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > ? ? print timeElapsed() > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > ? ? for row in reader: > ? ? ? ? if row[2] in IDs: > ? ? ? ? ? ? ouputfile.writerow(row) > ? ? ? ? else: > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > ? ? ? ? ? ? ? ? outputfile.writerow(row) > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > ? ? currentfile+=1 > > print "Finished processing query logs and writing new files" > print "# of Query log entries removed:" , len(foundOnceInQuery) > print "Total Time Elapsed:", timeElapsed() Just a couple of ideas: 1) load the data into a sqlite3 database and use an SQL query to extract your results (has the potential of doing what you want without you coding it, plus if your requirements change, maybe somewhat more flexible) 2) Pre-sort your input files via ID, then match-merge (may add some time/space required to sort, but then the merge should be fairly quick, plus you'll have access to the entire row in the process, not just the ID) Jon. From zaki.rahaman at gmail.com Fri Jul 17 18:57:48 2009 From: zaki.rahaman at gmail.com (Zaki) Date: Fri, 17 Jul 2009 15:57:48 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> <51ce01d3-4620-4e5f-b5b5-bd0e9fd6fd2c@w41g2000yqb.googlegroups.com> Message-ID: <8298385a-6f4b-44a5-b171-4871833f8dd2@c36g2000yqn.googlegroups.com> On Jul 17, 6:40?pm, Jon Clements wrote: > On 17 July, 21:08, Zaki wrote: > > > > > On Jul 17, 2:49?pm, MRAB wrote: > > > > Zaki wrote: > > > > Hey all, > > > > > I'm really new to Python and this may seem like a really dumb > > > > question, but basically, I wrote a script to do the following, however > > > > the processing time/memory usage is not what I'd like it to be. Any > > > > suggestions? > > > > > Outline: > > > > 1. Read tab delim files from a directory, files are of 3 types: > > > > install, update, and q. All 3 types contain ID values that are the > > > > only part of interest. > > > > 2. Using set() and set.add(), generate a list of unique IDs from > > > > install and update files. > > > > 3. Using the set created in (2), check the q files to see if there are > > > > matches for IDs. Keep all matches, and add any non matches (which only > > > > occur once in the q file) to a queue of lines to be removed from teh q > > > > files. > > > > 4. Remove the lines in the q for each file. (I haven't quite written > > > > the code for this, but I was going to implement this using csv.writer > > > > and rewriting all the lines in the file except for the ones in the > > > > removal queue). > > > > > Now, I've tried running this and it takes much longer than I'd like. I > > > > was wondering if there might be a better way to do things (I thought > > > > generator expressions might be a good way to attack this problem, as > > > > you could generate the set, and then check to see if there's a match, > > > > and write each line that way). > > > > Why are you checking and removing lines in 2 steps? Why not copy the > > > matching lines to a new q file and then replace the old file with the > > > new one (or, maybe, delete the new q file if no lines were removed)? > > > That's what I've done now. > > > Here is the final code that I have running. It's very much 'hack' type > > code and not at all efficient or optimized and any help in optimizing > > it would be greatly appreciated. > > > import csv > > import sys > > import os > > import time > > > begin = time.time() > > > #Check minutes elapsed > > def timeElapsed(): > > ? ? current = time.time() > > ? ? elapsed = current-begin > > ? ? return round(elapsed/60) > > > #USAGE: python logcleaner.py > > > inputdir = sys.argv[1] > > outputdir = sys.argv[2] > > > logfilenames = os.listdir(inputdir) > > > IDs = set() #IDs from update and install logs > > foundOnceInQuery = set() > > #foundTwiceInQuery = set() > > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > > Queue of IDs to remove from query logs (IDs found only once in query > > logs) > > > #Generate Filename Queues For Install/Update Logs, Query Logs > > iNuQ = [] > > queryQ = [] > > > for filename in logfilenames: > > ? ? if filename.startswith("par1.install") or filename.startswith > > ("par1.update"): > > ? ? ? ? iNuQ.append(filename) > > ? ? elif filename.startswith("par1.query"): > > ? ? ? ? queryQ.append(filename) > > > totalfiles = len(iNuQ) + len(queryQ) > > print "Total # of Files to be Processed:" , totalfiles > > print "Install/Update Logs to be processed:" , len(iNuQ) > > print "Query logs to be processed:" , len(queryQ) > > > #Process install/update queue to generate list of valid IDs > > currentfile = 1 > > for file in iNuQ: > > ? ? print "Processing", currentfile, "install/update log out of", len > > (iNuQ) > > ? ? print timeElapsed() > > ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > > ? ? for row in reader: > > ? ? ? ? IDs.add(row[2]) > > ? ? currentfile+=1 > > > print "Finished processing install/update logs" > > print "Unique IDs found:" , len(IDs) > > print "Total Time Elapsed:", timeElapsed() > > > currentfile = 1 > > for file in queryQ: > > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > > ? ? print timeElapsed() > > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > > ? ? for row in reader: > > ? ? ? ? if row[2] in IDs: > > ? ? ? ? ? ? ouputfile.writerow(row) > > ? ? ? ? else: > > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > > ? ? ? ? ? ? ? ? outputfile.writerow(row) > > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > > ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > > ? ? currentfile+=1 > > > print "Finished processing query logs and writing new files" > > print "# of Query log entries removed:" , len(foundOnceInQuery) > > print "Total Time Elapsed:", timeElapsed() > > Just a couple of ideas: > > 1) load the data into a sqlite3 database and use an SQL query to > extract your results (has the potential of doing what you want without > you coding it, plus if your requirements change, maybe somewhat more > flexible) > > 2) Pre-sort your input files via ID, then match-merge (may add some > time/space required to sort, but then the merge should be fairly > quick, plus you'll have access to the entire row in the process, not > just the ID) > > Jon. Thanks Jon for the ideas, and yeah I might look into the SQLite solution especially since I might need to do other sub selections. I was also considering constructing a set for both install/update logs and then another set for query logs and then do an intersection/other set manipulations. What I was really interested in was seeing if I could use generators to try and accomplish what I'm doing especially since I'm uisng a lot of for loops with conditionals and at this point it wouldn't matter if the data is consumed in the intermediate steps before producing output. Any help with getting this code in generator form would be greatly appreciated. From db3l.net at gmail.com Fri Jul 17 19:09:35 2009 From: db3l.net at gmail.com (David Bolen) Date: Fri, 17 Jul 2009 19:09:35 -0400 Subject: Why not enforce four space indentations in version 3.x? References: <260f0f1f-1115-4db8-a955-74c9f459ecc3@h30g2000vbr.googlegroups.com> <87ljmwm7ri.fsf@benfinney.id.au> <34f9992c-92a4-4043-ac27-93119d68460c@b15g2000yqd.googlegroups.com> Message-ID: Nobody writes: > On Thu, 16 Jul 2009 09:18:47 -0500, Tim Chase wrote: > >> Yes, the dictatorial "a tab always equals 8 spaces" > > Saying "always" is incorrect; it is more accurate to say that tab stops > are every 8 columns unless proven otherwise, with the burden of proof > falling on whoever wants to use something different. I suspect Tim was referring to the Python tokenizer. Internally, barring the existence of one of a few Emacs/vi tab setting commands in the file, Python always assigns the logical indentation level for a tab to align with the next multiple-of-8 column. This is unrelated to how someone might choose to display such a file. So mixing tabs and spaces and using a visual display setting of something other than 8 for the tab size (other than one consistent with an instruction embedded in the file) can yield a discrepancy between what is shown on the screen and how the same code is perceived by the Python compiler. This in turn may cause errors or code to execute at different indent levels than expected. Thus, in general, such a mixture is a bad idea, and as per this thread, no longer permitted in a single block in Python 3.x. -- David From dresdek at gmail.com Fri Jul 17 19:13:16 2009 From: dresdek at gmail.com (Matt) Date: Fri, 17 Jul 2009 16:13:16 -0700 (PDT) Subject: Unable to get Tkinter menubar to appear under OS X References: <3737fd18-4973-4cc3-914d-fbdefdbbb8a7@t10g2000vbg.googlegroups.com> <6a9b0831-ce2e-417b-ab4b-419629d0a25d@u16g2000pru.googlegroups.com> Message-ID: same problem on OS 10.4, menu doesn't show up on either from tkinter or wxpython, continuing search.. From ben+python at benfinney.id.au Fri Jul 17 19:25:47 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 18 Jul 2009 09:25:47 +1000 Subject: Propagate import for all modules in a package. References: <9781e3be-9b58-43ee-be64-3b57572d5a0d@c29g2000yqd.googlegroups.com> Message-ID: <87ws66evt0.fsf@benfinney.id.au> Phil writes: > I was thinking that I could just, for example, 'from datetime import > datetime' in __init__.py and have the ability to use 'datetime' > anywhere in any of the modules in 'package'. That would break one of the very nice features of Python's namespace system: that, because nothing[0] comes into a module without an explicit ?import foo?, you can always trace where ?foo? came from in the source code. > This didn't work for me. Did I just do something wrong? Is what I am > trying to do possible? If it were possible, we'd have the nightmare of not knowing where the names in a module were coming from, without having to search all over the package just to find where these implicit imports were taking place. No, thanks; I'm glad it's not possible. > Thanks in advance. [0] Two exceptions: There are a certain number of built-in names, strictly limited and well documented in the standard Python documentation. There are also those who use a namespace-clobbering ?from foo import *?, to which the solution is a clue-bat applied to the back of the skull. -- \ ?Pinky, are you pondering what I'm pondering?? ?Wuh, I think | `\ so, Brain, but how will we get three pink flamingos into one | _o__) pair of Capri pants?? ?_Pinky and The Brain_ | Ben Finney From joncle at googlemail.com Fri Jul 17 19:31:43 2009 From: joncle at googlemail.com (Jon Clements) Date: Fri, 17 Jul 2009 16:31:43 -0700 (PDT) Subject: Generator Expressions and CSV References: <8b0b135b-7bb5-45e4-b437-9308e787884d@t13g2000yqt.googlegroups.com> <71cfe652-0784-4612-82f8-26c2bf882980@v20g2000yqm.googlegroups.com> <51ce01d3-4620-4e5f-b5b5-bd0e9fd6fd2c@w41g2000yqb.googlegroups.com> <8298385a-6f4b-44a5-b171-4871833f8dd2@c36g2000yqn.googlegroups.com> Message-ID: <3ba0cc18-2cd0-416c-ad69-c7be138607e0@k1g2000yqf.googlegroups.com> On 17 July, 23:57, Zaki wrote: > On Jul 17, 6:40?pm, Jon Clements wrote: > > > > > On 17 July, 21:08, Zaki wrote: > > > > On Jul 17, 2:49?pm, MRAB wrote: > > > > > Zaki wrote: > > > > > Hey all, > > > > > > I'm really new to Python and this may seem like a really dumb > > > > > question, but basically, I wrote a script to do the following, however > > > > > the processing time/memory usage is not what I'd like it to be. Any > > > > > suggestions? > > > > > > Outline: > > > > > 1. Read tab delim files from a directory, files are of 3 types: > > > > > install, update, and q. All 3 types contain ID values that are the > > > > > only part of interest. > > > > > 2. Using set() and set.add(), generate a list of unique IDs from > > > > > install and update files. > > > > > 3. Using the set created in (2), check the q files to see if there are > > > > > matches for IDs. Keep all matches, and add any non matches (which only > > > > > occur once in the q file) to a queue of lines to be removed from teh q > > > > > files. > > > > > 4. Remove the lines in the q for each file. (I haven't quite written > > > > > the code for this, but I was going to implement this using csv.writer > > > > > and rewriting all the lines in the file except for the ones in the > > > > > removal queue). > > > > > > Now, I've tried running this and it takes much longer than I'd like. I > > > > > was wondering if there might be a better way to do things (I thought > > > > > generator expressions might be a good way to attack this problem, as > > > > > you could generate the set, and then check to see if there's a match, > > > > > and write each line that way). > > > > > Why are you checking and removing lines in 2 steps? Why not copy the > > > > matching lines to a new q file and then replace the old file with the > > > > new one (or, maybe, delete the new q file if no lines were removed)? > > > > That's what I've done now. > > > > Here is the final code that I have running. It's very much 'hack' type > > > code and not at all efficient or optimized and any help in optimizing > > > it would be greatly appreciated. > > > > import csv > > > import sys > > > import os > > > import time > > > > begin = time.time() > > > > #Check minutes elapsed > > > def timeElapsed(): > > > ? ? current = time.time() > > > ? ? elapsed = current-begin > > > ? ? return round(elapsed/60) > > > > #USAGE: python logcleaner.py > > > > inputdir = sys.argv[1] > > > outputdir = sys.argv[2] > > > > logfilenames = os.listdir(inputdir) > > > > IDs = set() #IDs from update and install logs > > > foundOnceInQuery = set() > > > #foundTwiceInQuery = set() > > > #IDremovalQ = set() Note: Unnecessary, duplicate of foundOnceInQuery; > > > Queue of IDs to remove from query logs (IDs found only once in query > > > logs) > > > > #Generate Filename Queues For Install/Update Logs, Query Logs > > > iNuQ = [] > > > queryQ = [] > > > > for filename in logfilenames: > > > ? ? if filename.startswith("par1.install") or filename.startswith > > > ("par1.update"): > > > ? ? ? ? iNuQ.append(filename) > > > ? ? elif filename.startswith("par1.query"): > > > ? ? ? ? queryQ.append(filename) > > > > totalfiles = len(iNuQ) + len(queryQ) > > > print "Total # of Files to be Processed:" , totalfiles > > > print "Install/Update Logs to be processed:" , len(iNuQ) > > > print "Query logs to be processed:" , len(queryQ) > > > > #Process install/update queue to generate list of valid IDs > > > currentfile = 1 > > > for file in iNuQ: > > > ? ? print "Processing", currentfile, "install/update log out of", len > > > (iNuQ) > > > ? ? print timeElapsed() > > > ? ? reader = csv.reader(open(inputdir+file),delimiter = '\t') > > > ? ? for row in reader: > > > ? ? ? ? IDs.add(row[2]) > > > ? ? currentfile+=1 > > > > print "Finished processing install/update logs" > > > print "Unique IDs found:" , len(IDs) > > > print "Total Time Elapsed:", timeElapsed() > > > > currentfile = 1 > > > for file in queryQ: > > > ? ? print "Processing", currentfile, "query log out of", len(queryQ) > > > ? ? print timeElapsed() > > > ? ? reader = csv.reader(open(inputdir+file), delimiter = '\t') > > > ? ? outputfile = csv.writer(open(outputdir+file), 'w') > > > ? ? for row in reader: > > > ? ? ? ? if row[2] in IDs: > > > ? ? ? ? ? ? ouputfile.writerow(row) > > > ? ? ? ? else: > > > ? ? ? ? ? ? if row[2] in foundOnceInQuery: > > > ? ? ? ? ? ? ? ? foundOnceInQuery.remove(row[2]) > > > ? ? ? ? ? ? ? ? outputfile.writerow(row) > > > ? ? ? ? ? ? ? ? #IDremovalQ.remove(row[2]) > > > ? ? ? ? ? ? ? ? #foundTwiceInQuery.add(row[2]) > > > > ? ? ? ? ? ? else: > > > ? ? ? ? ? ? ? ? foundOnceInQuery.add(row[2]) > > > ? ? ? ? ? ? ? ? #IDremovalQ.add(row[2]) > > > > ? ? currentfile+=1 > > > > print "Finished processing query logs and writing new files" > > > print "# of Query log entries removed:" , len(foundOnceInQuery) > > > print "Total Time Elapsed:", timeElapsed() > > > Just a couple of ideas: > > > 1) load the data into a sqlite3 database and use an SQL query to > > extract your results (has the potential of doing what you want without > > you coding it, plus if your requirements change, maybe somewhat more > > flexible) > > > 2) Pre-sort your input files via ID, then match-merge (may add some > > time/space required to sort, but then the merge should be fairly > > quick, plus you'll have access to the entire row in the process, not > > just the ID) > > > Jon. > > Thanks Jon for the ideas, and yeah I might look into the SQLite > solution especially since I might need to do other sub selections. I > was also considering constructing a set for both install/update logs > and then another set for query logs and then do an intersection/other > set manipulations. > > What I was really interested in was seeing if I could use generators > to try and accomplish what I'm doing especially since I'm uisng a lot > of for loops with conditionals and at this point it wouldn't matter if > the data is consumed in the intermediate steps before producing > output. Any help with getting this code in generator form would be > greatly appreciated. Well effectively, you are using generators. A very small optimisation might be to load the smallest set of data first (if you are already, I apologise!). Read the others and see if it's a matching ID, where it is, remove it from the smallest set, and those are the records to be added on the next parse. Posting when tired, so hope that makes some sense! Otherwise, I'd be tempted to pre-sort or use sqlite. From zac256 at gmail.com Fri Jul 17 20:31:44 2009 From: zac256 at gmail.com (Zac Burns) Date: Fri, 17 Jul 2009 17:31:44 -0700 Subject: Self optimizing iterable Message-ID: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> Greetings, I would like a set like object that when iterated maintains a count of where iteration stopped and then re-orders itself based on that count so that the iteration stopped on the most bubble to the top. An example use case for this would be for something like a large table of regular expressions that would be iterated over trying to match in some string. If some regular expressions are more statistically more successful then the iteration will generally be short. Does anyone know of a pre-existing recipe for this or feel like taking on the challenge? Bonus points for: Best possible BigO notation on switching order and iteration Threadsafety Extend to also include a mapping version -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games From http Fri Jul 17 20:40:32 2009 From: http (Paul Rubin) Date: 17 Jul 2009 17:40:32 -0700 Subject: Self optimizing iterable References: Message-ID: <7xbpniombj.fsf@ruckus.brouhaha.com> Zac Burns writes: > An example use case for this would be for something like a large table > of regular expressions that would be iterated over trying to match in > some string. If some regular expressions are more statistically more > successful then the iteration will generally be short. Generally if you are matching against a bunch of regexps, they will tend to match overlapping sets, so you want to control precisely what order they are tested in. Having stuff bubble around based on hit counts doesn't sound good. From zac256 at gmail.com Fri Jul 17 20:43:21 2009 From: zac256 at gmail.com (Zac Burns) Date: Fri, 17 Jul 2009 17:43:21 -0700 Subject: Retrieving a partial pickle Message-ID: <333edbe80907171743n6e72f5b8p7c1c6ab668cff7df@mail.gmail.com> I have a large pickle file, which happens to be a list with lots of objects in it. What sort of things can I do without unpickling the whole object? I would particularly like to retrieve one of the elements in the list without unpicking the whole object. If the answer is not specific to lists that would be useful (works on getting items in a dictionary, or doing introspection on the objects or whatever) -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games From python at mrabarnett.plus.com Fri Jul 17 20:54:22 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 18 Jul 2009 01:54:22 +0100 Subject: Self optimizing iterable In-Reply-To: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> References: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> Message-ID: <4A611D3E.90806@mrabarnett.plus.com> Zac Burns wrote: > Greetings, > > I would like a set like object that when iterated maintains a count of > where iteration stopped and then re-orders itself based on that count > so that the iteration stopped on the most bubble to the top. > > An example use case for this would be for something like a large table > of regular expressions that would be iterated over trying to match in > some string. If some regular expressions are more statistically more > successful then the iteration will generally be short. > > Does anyone know of a pre-existing recipe for this or feel like taking > on the challenge? > > Bonus points for: > Best possible BigO notation on switching order and iteration > Threadsafety > Extend to also include a mapping version > That's not a set, but a Most Recently Used list. From gagsl-py2 at yahoo.com.ar Fri Jul 17 21:09:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 17 Jul 2009 22:09:22 -0300 Subject: Self optimizing iterable References: <333edbe80907171731m8e8baf7i26d8b7f17275149d@mail.gmail.com> Message-ID: En Fri, 17 Jul 2009 21:31:44 -0300, Zac Burns escribi?: > I would like a set like object that when iterated maintains a count of > where iteration stopped and then re-orders itself based on that count > so that the iteration stopped on the most bubble to the top. > > An example use case for this would be for something like a large table > of regular expressions that would be iterated over trying to match in > some string. If some regular expressions are more statistically more > successful then the iteration will generally be short. If you require a certain order, it's not a set; better to use a list. The code below extends the builtin list type to add a "promote" method; lst.promote(3) "bubbles" lst[3] up (exchanging lst[3] with lst[2]). It's thread safe, and O(1). import threading class XList(list): # @property def lock(self): lock = getattr(self, '_lock', None) if lock is None: lock = self._lock = threading.Lock() return lock # def promote(self, index): if index<0: index += len(self) if index>0: with self.lock: self[index], self[index-1] = self[index-1], self[index] py> a = XList('python') py> a ['p', 'y', 't', 'h', 'o', 'n'] py> a.promote(3) py> a ['p', 'y', 'h', 't', 'o', 'n'] py> a.promote(2) py> a ['p', 'h', 'y', 't', 'o', 'n'] py> a.promote(-1) py> a ['p', 'h', 'y', 't', 'n', 'o'] py> a.promote(0) py> a ['p', 'h', 'y', 't', 'n', 'o'] An example, looking for a matching regular expression: for index,expr in enumerate(exprlist): m = expr.match(txt) if m: dosomethingwith(m) exprlist.promote(index) break After many calls, the most frequently matched expressions appear towards the front of the list. > Extend to also include a mapping version I'm unsure if this point is still applicable. I'm using a list here, not a set as in your original proposal. -- Gabriel Genellina From pfeldman at verizon.net Fri Jul 17 21:09:37 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 17 Jul 2009 18:09:37 -0700 (PDT) Subject: missing 'xor' Boolean operator In-Reply-To: <4A5DE5E7.5070003@mrabarnett.plus.com> References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> Message-ID: <24543805.post@talk.nabble.com> Suppose that 'xor' returns the value that is true when only one value is true, and False otherwise. This definition of xor doesn't have the standard associative property, that is, (a xor b) xor c will not necessarily equal a xor (b xor c) To see that this is the case, let a= 1, b= 2, and c= 3. (a xor b) xor c yields 3, while a xor (b xor c) yields 1. So, I'd prefer an xor operator that simply returns True or False. Phillip MRAB-2 wrote: > > > > > What values should 'xor' return? IMHO, if only one of the values is true > then it should return that value, otherwise it should return False. > > 1 xor 0 => 1 > 0 xor 2 => 2 > 1 xor 2 => False > 0 xor 0 => False > > This is because it's a Boolean operator, so it should fall back to > Boolean values when necessary, like 'not': > > not 0 => True > not 1 => False > > Also: > > x and y and z => (x and y) and z > x or y or z => (x or y) or z > > therefore: > > x xor y xor z => (x xor y) xor z > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24543805.html Sent from the Python - python-list mailing list archive at Nabble.com. From pfeldman at verizon.net Fri Jul 17 21:12:31 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 17 Jul 2009 18:12:31 -0700 (PDT) Subject: PDF version of Python Tutorial? Message-ID: <24543817.post@talk.nabble.com> Does anyone know if there is a PDF version of the Python Tutorial (URL= http://www.python.org/doc/current/tutorial/)? -- View this message in context: http://www.nabble.com/PDF-version-of-Python-Tutorial--tp24543817p24543817.html Sent from the Python - python-list mailing list archive at Nabble.com. From pavlovevidence at gmail.com Fri Jul 17 21:35:33 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 17 Jul 2009 18:35:33 -0700 (PDT) Subject: Self optimizing iterable References: <7xbpniombj.fsf@ruckus.brouhaha.com> Message-ID: <97c6fdf0-0364-4669-bdd5-35e8b567a38c@o36g2000vbl.googlegroups.com> On Jul 17, 5:40?pm, Paul Rubin wrote: > Zac Burns writes: > > An example use case for this would be for something like a large table > > of regular expressions that would be iterated over trying to match in > > some string. If some regular expressions are more statistically more > > successful then the iteration will generally be short. > > Generally if you are matching against a bunch of regexps, they will > tend to match overlapping sets, so you want to control precisely what > order they are tested in. ?Having stuff bubble around based on hit > counts doesn't sound good. As a corrollary, if you happen to be matching non-overlapping sets, then it is a red flag that there could be some other (better) way to dispatch than a linear regexp search. For instance, if all your regexps start with a different keyword, then you should dispatch on that keyword using a dictionary. A regexp can be used after dispatching to extract parameters if necessary. It's possible to have disjoint regexps without a simple dispatch criterion, but I'd guess that's less common. Carl Banks From ronn.ross at gmail.com Fri Jul 17 21:42:43 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Fri, 17 Jul 2009 21:42:43 -0400 Subject: Beginners question Message-ID: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> How do you define a global variable in a class. I tried this with do success: class ClassName: global_var = 1 def some_methos(): print global_var This doesn't work. What am I doing wrong? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjmachin at lexicon.net Fri Jul 17 21:54:11 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 17 Jul 2009 18:54:11 -0700 (PDT) Subject: Find first matching substring References: <2ac9b562-897e-450b-8d92-ef33c7fcc5e7@g23g2000vbr.googlegroups.com> Message-ID: <470ebc06-3e84-41c8-bd8e-0a342bb4ea28@y10g2000prg.googlegroups.com> On Jul 18, 2:11?am, Eloff wrote: > Almost every time I've had to do parsing of text over the last 5 years > I've needed this function: [snip] > It finds the first matching substring in the target string, if there > is more than one match, it returns the position of the match that > occurs at the lowest index in the string. Alternatives: (1) re.search(r"sub0|sub1|...", ...) (2) google "Aho Corasick Python" (one result should be a thread in this newsgroup within the last week) From gabrielmonnerat at gmail.com Fri Jul 17 22:03:32 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Fri, 17 Jul 2009 23:03:32 -0300 Subject: Beginners question In-Reply-To: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: <4A612D74.80908@gmail.com> Ronn Ross wrote: > How do you define a global variable in a class. I tried this with do > success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? You need pass "self" to the function and "global_var" needs be called with self. class ClassName: global_var = 1 def some_methos(self): print self.global_var Gabriel M. Monnerat From marduk at letterboxes.org Fri Jul 17 22:13:24 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Fri, 17 Jul 2009 22:13:24 -0400 Subject: Beginners question In-Reply-To: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: <1247883209.28511.8.camel@centar> On Fri, 2009-07-17 at 21:42 -0400, Ronn Ross wrote: > How do you define a global variable in a class. I bit of a mix-up with words here. A variable can be a class variable or a global variable (wrt the module).. not both. > I tried this with do success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? You could have posted the error message... Two things are wrong here. The first, if some_methos() is a method of ClassName then it should have at least one parameter, which is the instance of the class (i.e. "self"), so def some_methos(self): The second problem is that global_var is not local so you need to specify the scope. You can either use print ClassName.global_var or print self.global_var I'm not sure which one is better though I prefer the former because it makes it clearer to the reader that it's a class variable and not an instance variable. HTH, -a From nohics at gmail.com Fri Jul 17 22:40:32 2009 From: nohics at gmail.com (nohics nohics) Date: Sat, 18 Jul 2009 03:40:32 +0100 Subject: Beginners question In-Reply-To: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: When defining your class methods, you *must* explicitly list self as the first argument for each method, including __init__. When you call a method of an ancestor class from within your class, you *must* include the selfargument. But when you call your class method from outside, you do not specify anything for the self argument; you skip it entirely, and Pythonautomatically adds the instance reference for you. I am aware that this is confusing at first; it's not really inconsistent, but it may appear inconsistent because it relies on a distinction (between bound and unbound methods) that you don't know about yet. So, you have to do: class ClassName: self.global_var = 1 def some_methos(self): print self.global_var 2009/7/18 Ronn Ross > How do you define a global variable in a class. I tried this with do > success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From contact at xavierho.com Fri Jul 17 22:48:28 2009 From: contact at xavierho.com (Xavier Ho) Date: Sat, 18 Jul 2009 10:48:28 +0800 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907171947s6a4a010fp124146d54d560bf4@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> <2d56febf0907171507y75f6436ete75cb3f748ee838d@mail.gmail.com> <4A611D89.7040603@ieee.org> <2d56febf0907171947s6a4a010fp124146d54d560bf4@mail.gmail.com> Message-ID: <2d56febf0907171948l35e73c14gf02bc3c6ef11d493@mail.gmail.com> Darn it. On Sat, Jul 18, 2009 at 8:55 AM, Dave Angel wrote: > > You don't need a counter. len() will tell you the size of the list of > primes. Does len() go through and count by itself, or does it actually keep track of the size and just return the memory? I always thought it would go through and count, which may take some time. I could be wrong. > > And, just in case you're still interested, that > for i in range() > > earlier in the thread has the overhead of building a (possibly) large list > of integers, just in case we might need to loop. Right, so for i in xrange() would be a better choice? Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From gabrielmonnerat at gmail.com Fri Jul 17 23:06:31 2009 From: gabrielmonnerat at gmail.com (gabrielmonnerat) Date: Sat, 18 Jul 2009 00:06:31 -0300 Subject: Beginners question In-Reply-To: References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: <4A613C37.8090808@gmail.com> nohics nohics wrote: > When defining your class methods, you /must/ explicitly list self as > the first argument for each method, including __init__. When you call > a method of an ancestor class from within your class, you /must/ > include the self argument. But when you call your class method from > outside, you do not specify anything for the self argument; you skip > it entirely, and Python automatically adds the instance reference for > you. I am aware that this is confusing at first; it's not really > inconsistent, but it may appear inconsistent because it relies on a > distinction (between bound and unbound methods) that you don't know > about yet. > > So, you have to do: > > class ClassName: > self.global_var = 1 self isn't exists in this context. >>> class ClassName: ... self.global_var = 1 ... def some_methods(self): ... print self.global_var ... Traceback (most recent call last): File "", line 1, in File "", line 2, in ClassName NameError: name 'self' is not defined If you want a variable of instance you can use __init__ >>> class ClassName: ... def __init__(self): ... self.global_var = 1 ... def some_methods(self): ... print self.global_var Now "global_var" is created when ClassName is instantiated >>> ClassName.global_var Traceback (most recent call last): File "", line 1, in AttributeError: class ClassName has no attribute 'global_var' >>> c = ClassName() >>> c.global_var 1 > def some_methos(self): > print self.global_var > > 2009/7/18 Ronn Ross > > > How do you define a global variable in a class. I tried this with > do success: > class ClassName: > global_var = 1 > > def some_methos(): > print global_var > > This doesn't work. What am I doing wrong? > > -- > http://mail.python.org/mailman/listinfo/python-list > > From drobinow at gmail.com Fri Jul 17 23:09:44 2009 From: drobinow at gmail.com (David Robinow) Date: Fri, 17 Jul 2009 23:09:44 -0400 Subject: PDF version of Python Tutorial? In-Reply-To: <24543817.post@talk.nabble.com> References: <24543817.post@talk.nabble.com> Message-ID: <4eb0089f0907172009t1ba9331bx24042c497b1d2b96@mail.gmail.com> On Fri, Jul 17, 2009 at 9:12 PM, Dr. Phillip M. Feldman wrote: > > Does anyone know if there is a PDF version of the Python Tutorial (URL= > http://www.python.org/doc/current/tutorial/)? http://docs.python.org/download.html From gagsl-py2 at yahoo.com.ar Fri Jul 17 23:34:35 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 18 Jul 2009 00:34:35 -0300 Subject: PDF version of Python Tutorial? References: <24543817.post@talk.nabble.com> Message-ID: En Fri, 17 Jul 2009 22:12:31 -0300, Dr. Phillip M. Feldman escribi?: > Does anyone know if there is a PDF version of the Python Tutorial (URL= > http://www.python.org/doc/current/tutorial/)? From that page, click on the top left corner to go up one level, and you'll see a download link. Or, starting on http://www.python.org/doc/ you can navigate to the desired document. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 17 23:34:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 18 Jul 2009 00:34:39 -0300 Subject: Beginners question References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> Message-ID: En Fri, 17 Jul 2009 22:42:43 -0300, Ronn Ross escribi?: > How do you define a global variable in a class. I tried this with do > success: > class ClassName: > global_var = 1 > > def some_method(self): > print global_var > > This doesn't work. What am I doing wrong? [some typos fixed] In Python, a "variable" is just a name in a namespace that references another object. There are five possible namespaces here: - The local namespace, that is, the namespace inside some_method. All assigned-to names that appear inside the function (there is none in this case) are in the local namespace. But you're not interested on these local names, I presume, because you said "global" - The global namespace: the namespace of the module containing the code (NOT a "super-global" namespace shared by all modules). Names in this namespace are "global" in the sense that all code in the same module can reference them. But they're not restricted to any class, as you requested. To use a global name in an expression, just write it. To assign a new value to a global name, you must use the "global" statement if you're inside a function (else, it's considered a local name, see above). - The built-in namespace: like a super-global namespace, it is searched last when looking for an unqualified name; by example, the "len" function. For most purposes, you should consider the built-in namespace read-only. - The instance namespace: each object has its own namespace; you access its names using dotted attributes: obj.name -- or self.attribute when inside a method. The instance namespace is not determined by the object type or class; any object may contain any attribute name (in general). - The class namespace: classes may contain attributes too (like any other object, because classes are objects too). In your code above, you may use ClassName.global_var to refer to such class attribute. To resolve a dotted attribute name, Python searches the instance first -- and if no match is found, then the class namespace is searched too: this way, class attributes effectively are "default values" for instance attributes. Since all existing instances of a certain class share the same class, this is like a "shared attribute" between all instances (like "static members" in other languages). Inside some_method above, you may use self.global_var or ClassName.global_var -- but once you *assign* something to the instance attribute (self.global_var = xxx), the class attribute is shadowed and you may reference it using ClassName.global_var only. -- Gabriel Genellina From drobinow at gmail.com Fri Jul 17 23:51:46 2009 From: drobinow at gmail.com (David Robinow) Date: Fri, 17 Jul 2009 23:51:46 -0400 Subject: turtle dump In-Reply-To: References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> Message-ID: <4eb0089f0907172051v1b206a47r4326e7447f3d0637@mail.gmail.com> >> If you want to generate high-quality graphics easily you need different >> primitives. >> >> http://cairographics.org >> >> has Python bindings, but I don't know if it's available on Windows. > > Effectively not, as far as I can tell. PyCairo appears to be *nix and > require later binaries than those available from gtk site referenced from > cairo site. http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/1.4/ Works for me. From gagsl-py2 at yahoo.com.ar Sat Jul 18 00:13:18 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 18 Jul 2009 01:13:18 -0300 Subject: Retrieving a partial pickle References: <333edbe80907171743n6e72f5b8p7c1c6ab668cff7df@mail.gmail.com> Message-ID: En Fri, 17 Jul 2009 21:43:21 -0300, Zac Burns escribi?: > I have a large pickle file, which happens to be a list with lots of > objects in it. > > What sort of things can I do without unpickling the whole object? > > I would particularly like to retrieve one of the elements in the list > without unpicking the whole object. I'm afraid you can't do that, in general. A pickle is actually *code* for a stack-based machine; you have to execute all the previous instructions to have a sane stack contents; it's not possible (at least, not easily) just to jump in the middle of the pickle and execute just a section. > If the answer is not specific to lists that would be useful (works on > getting items in a dictionary, or doing introspection on the objects > or whatever) The pickletools module might be a starting point: http://docs.python.org/library/pickletools.html -- Try the Python Cookbook too http://code.activestate.com/recipes/langs/python/ -- Gabriel Genellina From http Sat Jul 18 00:47:03 2009 From: http (Paul Rubin) Date: 17 Jul 2009 21:47:03 -0700 Subject: Einstein summation notation References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> Message-ID: <7xprbybnso.fsf@ruckus.brouhaha.com> Ethan Furman writes: > Or if any(p for p in [header, body, footer, whatever, ...]) No need for the genexp: if any([header, body, footer, whatever, ...]) But, you are using the built-in bool cast either way. From http Sat Jul 18 00:49:02 2009 From: http (Paul Rubin) Date: 17 Jul 2009 21:49:02 -0700 Subject: Retrieving a partial pickle References: Message-ID: <7xljmmbnpd.fsf@ruckus.brouhaha.com> Zac Burns writes: > I have a large pickle file, which happens to be a list with lots of > objects in it. > What sort of things can I do without unpickling the whole object? If it's for some kind of data recovery of a busted file, you could code some awful hack to pull stuff out of the middle of the pickle, but that's not the idea. You may want the shelve module. From torriem at gmail.com Sat Jul 18 00:49:52 2009 From: torriem at gmail.com (Michael Torrie) Date: Fri, 17 Jul 2009 22:49:52 -0600 Subject: getopt code NameError exception on logTail.py In-Reply-To: References: <4A60BE10.5050905@mrabarnett.plus.com> Message-ID: <4A615470.9080503@gmail.com> LoD MoD wrote: > In this instance the trackback was somewhat unhelpful.There problem was > here: > > file = open(filename, 'r') > > should be > > file = open(a, 'r') > > args should be passed within the getopt riff Well given the code you posted, this little "problem" you mention has nothing to do with the traceback. The code (as you posted it) still does not define "process" anywhere. Seems helpful enough to me. If you're not importing it from somewhere, then saying "it's defined elsewhere" is still an error! From torriem at gmail.com Sat Jul 18 00:55:26 2009 From: torriem at gmail.com (Michael Torrie) Date: Fri, 17 Jul 2009 22:55:26 -0600 Subject: Question regarding style/design.. In-Reply-To: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> References: <3f1a902d0907170931v19a0c37ax398e782f604d788e@mail.gmail.com> Message-ID: <4A6155BE.80500@gmail.com> Wells Oliver wrote: > Sometimes I see relatively small application, generally task scripts, > written as essentially a list of statements. Other times, I see them neatly > divided into functions and then the "if __name__ == '__main__':" convention. > Is there a preference? Is there an... application scope such that the > preference shifts from the former to the latter? I understand the use of the > __name__ == 'main' convention for building unit tests, but I'm mixed on > using it in scripts/small applications. > Thanks for any thoughts! I always use the name guard thing for the simple reason that it lets me reuse code by importing it as a module into other things. My programs often serve double purposes. One is to be a standalone utility. The other is to be able to be used as a library from other code. So the name guard is essential. For code that is intended to be in modules, the name guard thing is indispensable for running unit tests. For very short, one-off things, I may leave it off. In any case I much prefer having a name guard, then running various functions than the Java style of writing a stupid static class with a static main method. Always seemed like a paradigm hack to me. From electronixtar at gmail.com Sat Jul 18 01:03:05 2009 From: electronixtar at gmail.com (est) Date: Fri, 17 Jul 2009 22:03:05 -0700 (PDT) Subject: ANN: psyco V2 References: Message-ID: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> On Jul 17, 10:48?am, Christian Tismer wrote: > Announcing Psyco V2 source release > ---------------------------------- > > This is the long awaited announcement of Psyco V2. > > Psyco V2 is a continuation of the well-known psyco project, > which was called finished and was dis-continued by its author > Armin Rigo in 2005, in favor of the PyPy project. > > This is a new project, using Psyco's code base with permission > of Armin. Questions and complaints should go to me > (tis... at stackless.com) or the mailing list > (psyco-de... at lists.sourceforge.net); > Armin is explicitly not in charge of (t)his project any longer! > > As one of the founders and an active member of the PyPy > project, I was very happy to be invited to work on Psyco > V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco > approach "an extension module that just makes Python faster". > > Psyco is a just-in-time compiler that accelerates arbitrary > Python code by specialization. We believe that Psyco's approach > can be carried out much further than it was tried so far, when > it's first version was abandoned. > > This first V2 release is source-only. There is no web-site, yet, > and there are no binaries for download. These will be available > in a few days onhttp://www.psyco.org. > > For the time being, please stick with subversion access, > building the extension module from source code. The repository > is here: > > ? ? ?http://codespeak.net/svn/psyco/v2/dist > > Check-out the repository, and run the setup.py script, > given that you have access to a C compiler. > > Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, > and Mac OS X. Psyco is not supporting 64 bit, yet. But it > is well being considered. > > The current improvements are, shortly: > > ? ?- Support for Python 2.4, 2.5 and 2.6 > ? ?- a lot of new builtins > ? ?- generators, fast and fully supported. > > More information is coming soon onhttp://www.psyco.org. > > This is the beginning of a series of new Psyco versions. > Many more improvements are prepared and about to be published, > soon, starting with the current version 2.0.0 . > > Stay tuned, this is just the beginning of psyco's re-birth! > > For questions about Psyco V2, please join the mailing list > > ? ? ?psyco-de... at lists.sourceforge.net > > or contact me on IRC: > > ? ? ?#psyco on irc.freenode.net . > > Psyco V2 is fundamentally supported by FATTOC, LLC. > Seehttp://www.fattoc.com. > > Without their continuous support, this work would not have > been possible at all. I wish to express my deepest thanks > to FATTOC, for allowing me to continue on Psyco with all the > energy that this ambitious project needs, and will need. > > Further special thanks are going to > Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, > Raymond Hettinger, Fabrizio Milo, Michael Foord, > Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, > for all the support and discussions. > > Looking forward to a great future of Psyco! > > July 17, 2009 > -- > Christian Tismer ? ? ? ? ? ? :^) ? > tismerysoft GmbH ? ? ? ? ? ? : ? ? Have a break! Take a ride on Python's > Johannes-Niemeyer-Weg 9A ? ? : ? ?*Starship*http://starship.python.net/ > 14109 Berlin ? ? ? ? ? ? ? ? : ? ? PGP key ->http://wwwkeys.pgp.net/ > work +49 30 802 86 56 ?mobile +49 173 24 18 776 ?fax +49 30 80 90 57 05 > PGP 0x57F3BF04 ? ? ? 9064 F4E1 D754 C2FF 1619 ?305B C09C 5A3B 57F3 BF04 > ? ? ? ?whom do you want to sponsor today? ?http://www.stackless.com/ congrats! btw is psyco.org owned? From tjreedy at udel.edu Sat Jul 18 01:44:58 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 18 Jul 2009 01:44:58 -0400 Subject: turtle dump In-Reply-To: <4eb0089f0907172051v1b206a47r4326e7447f3d0637@mail.gmail.com> References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> <4eb0089f0907172051v1b206a47r4326e7447f3d0637@mail.gmail.com> Message-ID: David Robinow wrote: >>> If you want to generate high-quality graphics easily you need different >>> primitives. >>> >>> http://cairographics.org >>> >>> has Python bindings, but I don't know if it's available on Windows. >> Effectively not, as far as I can tell. PyCairo appears to be *nix and >> require later binaries than those available from gtk site referenced from >> cairo site. > > http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/1.4/ > Works for me. Thanks. I did not see that referenced from the cairo site. Now if only there were a 3.1 version.... tjr From __peter__ at web.de Sat Jul 18 02:42:15 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 08:42:15 +0200 Subject: turtle dump References: <4a5f0773$0$18927$4fafbaef@reader2.news.tin.it> <7c8g58F26vgl2U4@mid.uni-berlin.de> <4a5f0c9c$0$18927$4fafbaef@reader2.news.tin.it> <77e991d3-bb94-42b9-9d78-b61db536569a@d15g2000prc.googlegroups.com> <4a5f18dd$0$18933$4fafbaef@reader2.news.tin.it> <4A5F287B.2000503@xs4all.nl> <364d422c-118b-4654-bfeb-9ed1ae468426@12g2000pri.googlegroups.com> Message-ID: Terry Reedy wrote: > alex23 wrote: > >> The help in iPython says the same, but also mentions that it's a >> dynamically generated function, so it may not be picking up the >> docstring that way. turtle.ScrolledCanvas.postscript is similarly >> terse, but you can find more info in turtle.Canvas.postscript: >> >> Print the contents of the canvas to a postscript >> file. Valid options: colormap, colormode, file, fontmap, >> height, pageanchor, pageheight, pagewidth, pagex, pagey, >> rotate, witdh, x, y. > > How, exactly, did you get that list? > > tjr [Python 3.1] >>> import tkinter >>> print(tkinter.Canvas.postscript.__doc__) Print the contents of the canvas to a postscript file. Valid options: colormap, colormode, file, fontmap, height, pageanchor, pageheight, pagewidth, pagex, pagey, rotate, witdh, x, y. Peter From tismer at stackless.com Sat Jul 18 02:50:07 2009 From: tismer at stackless.com (Christian Tismer) Date: Sat, 18 Jul 2009 08:50:07 +0200 Subject: ANN: psyco V2 In-Reply-To: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> References: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> Message-ID: <5661F6B6-371B-46C3-BAE6-FFE280DC78A3@stackless.com> It is just being transferred Von meinem iTouch gesendet On Jul 18, 2009, at 7:03, est wrote: > On Jul 17, 10:48 am, Christian Tismer wrote: >> Announcing Psyco V2 source release >> ---------------------------------- >> >> This is the long awaited announcement of Psyco V2. >> >> Psyco V2 is a continuation of the well-known psyco project, >> which was called finished and was dis-continued by its author >> Armin Rigo in 2005, in favor of the PyPy project. >> >> This is a new project, using Psyco's code base with permission >> of Armin. Questions and complaints should go to me >> (tis... at stackless.com) or the mailing list >> (psyco-de... at lists.sourceforge.net); >> Armin is explicitly not in charge of (t)his project any longer! >> >> As one of the founders and an active member of the PyPy >> project, I was very happy to be invited to work on Psyco >> V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco >> approach "an extension module that just makes Python faster". >> >> Psyco is a just-in-time compiler that accelerates arbitrary >> Python code by specialization. We believe that Psyco's approach >> can be carried out much further than it was tried so far, when >> it's first version was abandoned. >> >> This first V2 release is source-only. There is no web-site, yet, >> and there are no binaries for download. These will be available >> in a few days onhttp://www.psyco.org. >> >> For the time being, please stick with subversion access, >> building the extension module from source code. The repository >> is here: >> >> http://codespeak.net/svn/psyco/v2/dist >> >> Check-out the repository, and run the setup.py script, >> given that you have access to a C compiler. >> >> Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, >> and Mac OS X. Psyco is not supporting 64 bit, yet. But it >> is well being considered. >> >> The current improvements are, shortly: >> >> - Support for Python 2.4, 2.5 and 2.6 >> - a lot of new builtins >> - generators, fast and fully supported. >> >> More information is coming soon onhttp://www.psyco.org. >> >> This is the beginning of a series of new Psyco versions. >> Many more improvements are prepared and about to be published, >> soon, starting with the current version 2.0.0 . >> >> Stay tuned, this is just the beginning of psyco's re-birth! >> >> For questions about Psyco V2, please join the mailing list >> >> psyco-de... at lists.sourceforge.net >> >> or contact me on IRC: >> >> #psyco on irc.freenode.net . >> >> Psyco V2 is fundamentally supported by FATTOC, LLC. >> Seehttp://www.fattoc.com. >> >> Without their continuous support, this work would not have >> been possible at all. I wish to express my deepest thanks >> to FATTOC, for allowing me to continue on Psyco with all the >> energy that this ambitious project needs, and will need. >> >> Further special thanks are going to >> Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, >> Raymond Hettinger, Fabrizio Milo, Michael Foord, >> Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, >> for all the support and discussions. >> >> Looking forward to a great future of Psyco! >> >> July 17, 2009 >> -- >> Christian Tismer :^) >> tismerysoft GmbH : Have a break! Take a ride on >> Python's >> Johannes-Niemeyer-Weg 9A : *Starship*http://starship.python.net/ >> 14109 Berlin : PGP key ->http://wwwkeys.pgp.net/ >> work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 >> PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 >> BF04 >> whom do you want to sponsor today? http://www.stackless.com/ > > congrats! > > btw is psyco.org owned? > -- > http://mail.python.org/mailman/listinfo/python-list From amrita at iisermohali.ac.in Sat Jul 18 03:09:21 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 18 Jul 2009 12:39:21 +0530 (IST) Subject: how two join and arrange two files together Message-ID: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> Hi, I have two files having entries like:-- fileA 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 and fileB ChainA: ALA8 -67.217297 -37.131330 ChainA: ALA21 -69.822977 -48.871282 ChainA: ALA23 -59.148095 -46.540043 ChainA: ALA33 -65.459303 -43.269718 i want to join thses two files in such a way that the output file will contain column of both the files and the enties of similar position of ALA will be together.so the output file should look something like:---- fileC 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA: ALA8 -67.217297 -37.131330 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 ChainA: ALA21 -69.822977 -48.871282 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA: ALA23 -59.148095 -46.540043 ChainA: ALA33 -65.459303 -43.269718 Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From kalyanchakravarthy at hyit.com Sat Jul 18 03:14:23 2009 From: kalyanchakravarthy at hyit.com (Kalyan Chakravarthy) Date: Sat, 18 Jul 2009 12:44:23 +0530 Subject: Python import Error Message-ID: <8ca278430907180014k460f3798mafef6fc59379677b@mail.gmail.com> Hi All, I am using* Python 2.6, MySQL 4.0* , I have successfully Instaled MySQLdb (*MySQL-python-1.2.3c1.win32-py2.6*) in my system. I tested through command prompt with "import MySQLdb ", its not shwing any errors (means its instaled successfully), I set Eneceranment variable for Python I am running simple application with import "MySQLdb" to get the connection to MySQL, But its throwing "No module named MySQLdb", Please any one tell me what couled be the reasion is there any version miss match with python and MySQL ? Since last 3days I am strugling on this -- Regards Kalyan -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Sat Jul 18 03:19:23 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 00:19:23 -0700 Subject: how two join and arrange two files together In-Reply-To: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> On Sat, Jul 18, 2009 at 12:09 AM, wrote: > > Hi, > > I have two files having entries like:-- > fileA > 8 ?ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 > and > fileB > ChainA: ALA8 ? ?-67.217297 ? ? ?-37.131330 > ChainA: ALA21 ? -69.822977 ? ? ?-48.871282 > ChainA: ALA23 ? -59.148095 ? ? ?-46.540043 > ChainA: ALA33 ? -65.459303 ? ? ?-43.269718 > > i want to join thses two files in such a way that the output file will > contain column of both the files and the enties of similar position of ALA > will be together.so the output file should look something like:---- > fileC > 8 ?ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA: > ALA8 ? ?-67.217297 ? ? ?-37.131330 > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ChainA: > ALA21 ? -69.822977 ? ? ?-48.871282 > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA: > ALA23 ? -59.148095 ? ? ?-46.540043 > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ChainA: > ALA33 ? -65.459303 ? ? ?-43.269718 This mailinglist is not a collection of free code monkeys. Show us you've at least /tried/ to write this yourself, and tell us where you're running into problems or what error you're getting. See also http://catb.org/esr/faqs/smart-questions.html Additionally, you might consider asking on the Indian Python mailinglist instead: http://mail.python.org/mailman/listinfo/bangpypers - Chris From amrita at iisermohali.ac.in Sat Jul 18 03:39:09 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 18 Jul 2009 13:09:09 +0530 (IST) Subject: how two join and arrange two files together In-Reply-To: <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: <23421.210.212.36.65.1247902749.squirrel@www.iisermohali.ac.in> I tried to join these two files together using command....... from itertools import izip from os.path import exists def parafiles(*files): vec = (open(f) for f in files if exists(f)) data = izip(*vec) [f.close() for f in vec] return data for data in parafiles('/home/amrita/alachems/chem1.txt', '/home/amrita/secstr/secstr.txt'): print ' '.join(d.strip() for d in data) it just joined the column of two files. > On Sat, Jul 18, 2009 at 12:09 AM, wrote: >> >> Hi, >> >> I have two files having entries like:-- >> fileA >> 8 ??ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 >> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 >> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 >> and >> fileB >> ChainA: ALA8 ?? ??-67.217297 ?? ?? ??-37.131330 >> ChainA: ALA21 ?? -69.822977 ?? ?? ??-48.871282 >> ChainA: ALA23 ?? -59.148095 ?? ?? ??-46.540043 >> ChainA: ALA33 ?? -65.459303 ?? ?? ??-43.269718 >> >> i want to join thses two files in such a way that the output file will >> contain column of both the files and the enties of similar position of >> ALA >> will be together.so the output file should look something like:---- >> fileC >> 8 ??ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 ChainA: >> ALA8 ?? ??-67.217297 ?? ?? ??-37.131330 >> 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 >> ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? >> ?? ?? ?? ?? ?? ChainA: >> ALA21 ?? -69.822977 ?? ?? ??-48.871282 >> 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 ChainA: >> ALA23 ?? -59.148095 ?? ?? ??-46.540043 >> ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? >> ?? ?? ?? ?? ?? ChainA: >> ALA33 ?? -65.459303 ?? ?? ??-43.269718 > > This mailinglist is not a collection of free code monkeys. Show us > you've at least /tried/ to write this yourself, and tell us where > you're running into problems or what error you're getting. > > See also http://catb.org/esr/faqs/smart-questions.html > > Additionally, you might consider asking on the Indian Python > mailinglist instead: > http://mail.python.org/mailman/listinfo/bangpypers > > - Chris > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From __peter__ at web.de Sat Jul 18 05:17:33 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 11:17:33 +0200 Subject: how two join and arrange two files together References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: amrita at iisermohali.ac.in wrote: > I tried to join these two files together using command....... > > from itertools import izip > from os.path import exists > > def parafiles(*files): > vec = (open(f) for f in files if exists(f)) > data = izip(*vec) > [f.close() for f in vec] > return data > > for data in parafiles('/home/amrita/alachems/chem1.txt', > '/home/amrita/secstr/secstr.txt'): > print ' '.join(d.strip() for d in data) parafiles has a bug: vec is a generator and hence cannot be run twice. Therefore the odd [f.close()...] list comprehension (don't use a list comp if you don't care about the result!) has no effect. If you change vec into a list you will be hit by another problem -- the for loop trying to operate on closed files. While the correct approach probably involves a contextlib.contextmanager I recommend that you concentrate on your real problem and keep parafiles() simple: def parafiles(*files): open_files = (open(f) for f in files if exits(f)) return izip(*open_files) > it just joined the column of two files. Can you make an effort to express clearly what you want, preferrably with a simple and unambiguous example? Please keep the line widths below the threshold of 78 characters to avoid messing it up on its way to the reader. Peter From amrita at iisermohali.ac.in Sat Jul 18 05:29:28 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Sat, 18 Jul 2009 14:59:28 +0530 (IST) Subject: how two join and arrange two files together In-Reply-To: References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: <2569.210.212.36.65.1247909368.squirrel@www.iisermohali.ac.in> I want to join column of two different data file but i want that the entries will match (example i mentioned in my first mail, the position of ALA eill match) if its not matching then it will get printed as such. > amrita at iisermohali.ac.in wrote: > >> I tried to join these two files together using command....... >> >> from itertools import izip >> from os.path import exists >> >> def parafiles(*files): >> vec = (open(f) for f in files if exists(f)) >> data = izip(*vec) >> [f.close() for f in vec] >> return data >> >> for data in parafiles('/home/amrita/alachems/chem1.txt', >> '/home/amrita/secstr/secstr.txt'): >> print ' '.join(d.strip() for d in data) > > parafiles has a bug: vec is a generator and hence cannot be run twice. > Therefore the odd [f.close()...] list comprehension (don't use a list comp > if you don't care about the result!) has no effect. > > If you change vec into a list you will be hit by another problem -- the > for > loop trying to operate on closed files. While the correct approach > probably > involves a contextlib.contextmanager I recommend that you concentrate on > your real problem and keep parafiles() simple: > > def parafiles(*files): > open_files = (open(f) for f in files if exits(f)) > return izip(*open_files) > >> it just joined the column of two files. > > Can you make an effort to express clearly what you want, preferrably with > a > simple and unambiguous example? > > Please keep the line widths below the threshold of 78 characters to avoid > messing it up on its way to the reader. > > Peter > > -- > http://mail.python.org/mailman/listinfo/python-list > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From dickinsm at gmail.com Sat Jul 18 05:50:06 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sat, 18 Jul 2009 02:50:06 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <057cc5d0-cbf4-4f13-b57c-406d36ce2649@k1g2000yqf.googlegroups.com> <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <87fxcxj54d.fsf@busola.homelinux.net> <4A5E1538.7030107@sequans.com> <4A5EEDAE.3040605@sequans.com> <4A5F5759.4010600@sequans.com> <1247774222.4a5f860ec3cfa@mail.uh.cu> Message-ID: <9a623957-aece-413f-817a-88c76b9e25f4@y17g2000yqn.googlegroups.com> On Jul 17, 12:06?pm, Jean-Michel Pichavant wrote: > I was saying that using boolean operators with object instead of boolean > values is error prone, I agree with this to some extent. After all, Python conditional expressions were eventually introduced in response to buggy uses of the 'a and b or c' idiom. See PEP 308, and: http://mail.python.org/pipermail/python-dev/2005-September/056546.html In my own code, I'm finding myself increasingly using conditional expressions where I would once have used 'and' or 'or': daysInAdvance = int(inputVar) if inputVar is not None else 0 -- Mark From markbak at gmail.com Sat Jul 18 06:21:57 2009 From: markbak at gmail.com (Mark Bakker) Date: Sat, 18 Jul 2009 12:21:57 +0200 Subject: difference in printing to screen Mac / Windows Message-ID: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> Hello list I notice a difference between running the following script on my Mac and on a PC: from time import sleep for i in range(10): print i, sleep(2) On my PC this prints a number every 2 seconds. This is the behavior I want. On my Mac Python waits 10*2 = 20 seconds, and then prints 0 1 2 3 4 5 6 7 8 9 Any thoughts on how I can make Python behave to get the Python behavior of my PC on my Mac? Thanks, Mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sat Jul 18 06:40:01 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 12:40:01 +0200 Subject: how two join and arrange two files together References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: amrita at iisermohali.ac.in wrote: >> Can you make an effort to express clearly what you want, preferrably with >> a simple and unambiguous example? > I want to join column of two different data file but i want that the > entries will match (example i mentioned in my first mail, the position of > ALA eill match) if its not matching then it will get printed as such. Just say "No" if you mean it. My best guess: from collections import defaultdict def merge(sources): blanks = [blank for items, blank, keyfunc in sources] d = defaultdict(lambda: blanks[:]) for index, (items, blank, keyfunc) in enumerate(sources): for item in items: d[keyfunc(item)][index] = item for key in sorted(d): yield d[key] if __name__ == "__main__": from StringIO import StringIO a = StringIO("""\ a alpha c beta d gamma """) b = StringIO("""\ a one b two d three e four """) c = StringIO("""\ a 111 b 222 f 333 """) def key(line): return line[:1] def source(stream, blank="", key=key): return (line.strip() for line in stream), blank, key for m in merge([source(x) for x in [a,b,c]]): print "|".join(c.ljust(10) for c in m) From python.list at tim.thechases.com Sat Jul 18 07:03:11 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 18 Jul 2009 06:03:11 -0500 Subject: difference in printing to screen Mac / Windows In-Reply-To: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> References: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> Message-ID: <4A61ABEF.6020002@tim.thechases.com> > I notice a difference between running the following script on my Mac and on > a PC: > > from time import sleep > for i in range(10): > print i, > sleep(2) > > On my PC this prints a number every 2 seconds. This is the behavior I want. > > On my Mac Python waits 10*2 = 20 seconds, and then prints 0 1 2 3 4 5 6 7 8 > 9 This sounds like a buffered vs. non-buffered output issue. My guess would be that if you increased 10 to something larger, the output buffer would flush at various intervals. The solution on the Mac (or other *nix-like OSes) would be to either start python in unbuffered mode: python -u mycode.py Alternatively, you can force a flush of the output buffer at each iteration: import sys for i in range(10): print i sys.stdout.flush() sleep(2) Lastly, you can force all standard-output in your program to be unbuffered without the "-u" parameter: sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) Hope this helps, -tkc From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 07:21:32 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 12:21:32 +0100 Subject: Python graphics / imaging library Message-ID: Chaps, what's the most appropriate (maintained) graphics library to use? PIL seems to have last been updated in 2006 http://www.pythonware.com/products/pil/ and GD seems to be even older. Don't want to go down a dead end. Pete -- http://www.petezilla.co.uk From motoom at xs4all.nl Sat Jul 18 07:38:14 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Sat, 18 Jul 2009 13:38:14 +0200 Subject: Python graphics / imaging library In-Reply-To: References: Message-ID: <4A61B426.2090404@xs4all.nl> Peter Chant wrote: > what's the most appropriate (maintained) graphics library to use? PIL seems > to have last been updated in 2006 http://www.pythonware.com/products/pil/ > and GD seems to be even older. Don't want to go down a dead end. Contrary to organic material, software doesn't rot when it gets older. PIL is pretty complete for the task it was designed to do, pretty debugged during the past years, and pretty much 'finished' -- it doesn't need frequent updates anymore. Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 07:50:01 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 12:50:01 +0100 Subject: Python graphics / imaging library References: Message-ID: <9gp8j6xuh6.ln2@phoenix.fire> Michiel Overtoom wrote: > Peter Chant wrote: > >> what's the most appropriate (maintained) graphics library to use? PIL >> seems to have last been updated in 2006 >> http://www.pythonware.com/products/pil/ >> and GD seems to be even older. Don't want to go down a dead end. > > Contrary to organic material, software doesn't rot when it gets older. > > PIL is pretty complete for the task it was designed to do, pretty > debugged during the past years, and pretty much 'finished' -- it doesn't > need frequent updates anymore. > > Greetings, > No, it does not. However, if PIL was updated last in 2006. Python in 2009 has gone to version 3.1. If PIL is compatible with 3.1 then I'm fine. But I don't want to have to stick with Python 2.5 as the rest of the world moves on. Pete -- http://www.petezilla.co.uk From benjamin.kaplan at case.edu Sat Jul 18 08:26:22 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 18 Jul 2009 08:26:22 -0400 Subject: Python graphics / imaging library In-Reply-To: <9gp8j6xuh6.ln2@phoenix.fire> References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: On Sat, Jul 18, 2009 at 7:50 AM, Peter Chant < REMpeteOVE at cappetezilla.italsco.uk> wrote: > Michiel Overtoom wrote: > > > Peter Chant wrote: > > > >> what's the most appropriate (maintained) graphics library to use? PIL > >> seems to have last been updated in 2006 > >> http://www.pythonware.com/products/pil/ > >> and GD seems to be even older. Don't want to go down a dead end. > > > > Contrary to organic material, software doesn't rot when it gets older. > > > > PIL is pretty complete for the task it was designed to do, pretty > > debugged during the past years, and pretty much 'finished' -- it doesn't > > need frequent updates anymore. > > > > Greetings, > > > > No, it does not. However, if PIL was updated last in 2006. Python in 2009 > has gone to version 3.1. If PIL is compatible with 3.1 then I'm fine. But > I don't want to have to stick with Python 2.5 as the rest of the world > moves on. > The rest of the world hasn't moved on yet. Most people are still using Python 2.6 and the 2.x series will continue to be actively developed for another couple years. > Pete > > > -- > http://www.petezilla.co.uk > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 08:30:02 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 13:30:02 +0100 Subject: Python graphics / imaging library References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: Peter Chant wrote: > > No, it does not. However, if PIL was updated last in 2006. Python in > 2009 > has gone to version 3.1. If PIL is compatible with 3.1 then I'm fine. > But I don't want to have to stick with Python 2.5 as the rest of the world > moves on. BTW, this was not a critisism of PIL or GD, rather what do people generally use now? -- http://www.petezilla.co.uk From motoom at xs4all.nl Sat Jul 18 08:41:36 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Sat, 18 Jul 2009 14:41:36 +0200 Subject: Python graphics / imaging library In-Reply-To: References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: <4A61C300.9070503@xs4all.nl> Peter Chant wrote: > what do people generally use now? I can only speak for myself... I use PIL ;-) Greetings, From maxerickson at gmail.com Sat Jul 18 08:54:50 2009 From: maxerickson at gmail.com (Max Erickson) Date: Sat, 18 Jul 2009 12:54:50 +0000 (UTC) Subject: Python graphics / imaging library References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: Peter Chant wrote: > No, it does not. However, if PIL was updated last in 2006. > Python in 2009 has gone to version 3.1. If PIL is compatible > with 3.1 then I'm fine. But I don't want to have to stick with > Python 2.5 as the rest of the world moves on. > > Pete > > Various messages to the Image-SIG mailing list indicate that support for 3.x is coming after a release of 1.1.7: http://mail.python.org/pipermail/image-sig/2009-March/005498.html More here: http://mail.python.org/pipermail/image-sig/2009-March/thread.html More recent months contain updates to the status of 1.1.7, it is headed towards a release. Preliminary tarballs and binaries are available on effbot.org: http://effbot.org/downloads/#imaging http://effbot.org/downloads/#pil max From rhodri at wildebst.demon.co.uk Sat Jul 18 09:17:12 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sat, 18 Jul 2009 14:17:12 +0100 Subject: Beginners question In-Reply-To: <4A612D74.80908@gmail.com> References: <9c8c445f0907171842p2b1ee910y3338450881ac7ad2@mail.gmail.com> <4A612D74.80908@gmail.com> Message-ID: On Sat, 18 Jul 2009 03:03:32 +0100, gabrielmonnerat wrote: > Ronn Ross wrote: >> How do you define a global variable in a class. I tried this with do >> success: >> class ClassName: >> global_var = 1 >> def some_methos(): >> print global_var >> >> This doesn't work. What am I doing wrong? > You need pass "self" to the function and "global_var" needs be called > with self. > > class ClassName: > global_var = 1 > def some_methos(self): > print self.global_var If the OP really wants global_var to be global to the class, this approach will only work as long as he never assigns to self.global_var. The terminology is a bit confusing here. "Global variables" refers to variables which are global to an entire module, ones that you would use "global" to declare inside a function. Something like this: x = 5 def set_x(y): global x x = y set_x(3) print x ...gives the result "3" The idea of defining a global variable in a class is a bit of a nonsense, since globals exist for the entire module. There are, however, variables that exist and are the same for every instance of a class, or "class attributes" as they are normally called. You use them either by assignment in the class definition, or in a method by prefixing them with the class name. class ClassName: global_var = 1 def some_method(self): print ClassName.global_var ClassName.global_var = 2 "Instance attributes", the ones prefixed by "self", are what get used for most purposes. These are separate for each different instance of the class. class ClassName: def __init__(self, val): self.var = val a = ClassName(1) b = ClassName(2) print a.var, b.var ...gives the result "1 2" The wrinkle is that if you read from an instance attribute that doesn't exist, Python will use a class attribute of the same name if there is one. This is often exploited for setting default values. Beware, though: the moment you assign, an instance attribute is created -- the underlying class attribute is *not* changed. class ClassName: global_var = 1 def some_method(self): self.global_var = 2 a = ClassName() b = ClassName() b.some_method() print a.global_var, b.global_var ...gives the result "1 2" again! -- Rhodri James *-* Wildebeest Herder to the Masses From dstanek at dstanek.com Sat Jul 18 09:41:13 2009 From: dstanek at dstanek.com (David Stanek) Date: Sat, 18 Jul 2009 09:41:13 -0400 Subject: Python import Error In-Reply-To: <8ca278430907180014k460f3798mafef6fc59379677b@mail.gmail.com> References: <8ca278430907180014k460f3798mafef6fc59379677b@mail.gmail.com> Message-ID: On Sat, Jul 18, 2009 at 3:14 AM, Kalyan Chakravarthy wrote: > Hi All, > ?????????????????? I am using Python 2.6, MySQL 4.0 , I have successfully > Instaled MySQLdb (MySQL-python-1.2.3c1.win32-py2.6) in my system. I tested > through command prompt with "import MySQLdb ", its not shwing any errors > (means its instaled successfully), I set Eneceranment variable for Python > > I? am running simple application with import "MySQLdb" to get the connection > to MySQL, But its throwing "No module named MySQLdb", Please any one tell me > what couled be the reasion > > is there any version miss match with python and MySQL ? > > Since last 3days I am strugling on this > Off the top of my head I would say that maybe you are using one python binary from the command-line and another in your script or possibly your python path is foobar. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From tjcrone at gmail.com Sat Jul 18 10:34:11 2009 From: tjcrone at gmail.com (Timothy Crone) Date: Sat, 18 Jul 2009 10:34:11 -0400 Subject: Unpack Expects the Wrong Number of Bytes Message-ID: <78926b920907180734p7363af24v7f81f425cdd6d972@mail.gmail.com> Hello, I have noticed that for certain format strings, struct.unpack expects the wrong number of bytes. For example, this works fine header = "4si4s4si2h2i3h4s" data = list(unpack(header,f.read(42))) however, this header = "4si4s4si2h2i3h4si" data = list(unpack(header,f.read(46))) returns the following error: struct.error: unpack requires a string argument of length 48 So even though my format string includes an additional 4-byte integer, unpack is expecting 6 additional bytes. Here's a simpler example: This works: header = "s" data = list(unpack(header,f.read(1))) however this: header = "si" data = list(unpack(header,f.read(5))) throws struct.error: unpack requires a string argument of length 8 So unpack expects 7 additional bytes when an integer is added to the format string. Does anyone know what is going on? I am using Debian stable, so my Python version is 2.5.2. But I have replicated this with 2.6.2. Here's my proc/version: Linux version 2.6.30-bpo.1-amd64 (Debian 2.6.30-1~bpo50+1) (nobse at debian.org) (gcc version 4.3.2 (Debian 4.3.2-1.1) ) #1 SMP Fri Jun 26 09:41:55 UTC 2009 Any help would be greatly appreciated. Cheers, Tim From aahz at pythoncraft.com Sat Jul 18 10:44:29 2009 From: aahz at pythoncraft.com (Aahz) Date: 18 Jul 2009 07:44:29 -0700 Subject: setup.py not found References: <3be2bdce-680d-4b32-ad0c-ef46caf556e9@f10g2000vbf.googlegroups.com> Message-ID: In article <3be2bdce-680d-4b32-ad0c-ef46caf556e9 at f10g2000vbf.googlegroups.com>, Larry.Martell at gmail.com wrote: > >I'm trying to install a package (cx_Oracle) on a mac running 10.5.7. >I've done this on other platforms, but never on a mac. I followed the >instructions given, but when I try and run setup I get: > >Apollo:instantclient_10_2 user$ python setup.py build >/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/ >Python.app/Contents/MacOS/Python: can't open file 'setup.py': [Errno >2] No such file or directory > >Is there something else I have to install first to have this? Do you have a setup.py in the current working directory? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From sjmachin at lexicon.net Sat Jul 18 10:47:15 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 18 Jul 2009 07:47:15 -0700 (PDT) Subject: Unpack Expects the Wrong Number of Bytes References: Message-ID: <34a7b901-4d33-469a-8b35-5ac1f30304fa@i18g2000pro.googlegroups.com> On Jul 19, 12:34?am, Timothy Crone wrote: > Hello, > > I have noticed that for certain format strings, struct.unpack expects > the wrong number of bytes. [snip] > header = "si" > data = list(unpack(header,f.read(5))) > > throws > > struct.error: unpack requires a string argument of length 8 > > So unpack expects 7 additional bytes when an integer is added to the > format string. Does anyone know what is going on? Alignment. 1 byte for the "s", 3 pad bytes to get to the next "i" boundary, 4 bytes for the "i". http://docs.python.org/library/struct.html Then Ctrl-F search for "align" Choose a prefix character whose MO matches the struct that you need to work with. Cheers, John From python at mrabarnett.plus.com Sat Jul 18 10:53:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 18 Jul 2009 15:53:18 +0100 Subject: Try... except....Try again? In-Reply-To: <2d56febf0907171948l35e73c14gf02bc3c6ef11d493@mail.gmail.com> References: <2d56febf0907162231s2134f32dm7d31727087a8b215@mail.gmail.com> <02707804$0$5185$c3e8da3@news.astraweb.com> <2d56febf0907170734r21254251pc62a70601cf8f043@mail.gmail.com> <2d56febf0907170735k6e123cd9n16727acacc3b046a@mail.gmail.com> <2d56febf0907171507y75f6436ete75cb3f748ee838d@mail.gmail.com> <4A611D89.7040603@ieee.org> <2d56febf0907171947s6a4a010fp124146d54d560bf4@mail.gmail.com> <2d56febf0907171948l35e73c14gf02bc3c6ef11d493@mail.gmail.com> Message-ID: <4A61E1DE.8010300@mrabarnett.plus.com> Xavier Ho wrote: > Darn it. > > On Sat, Jul 18, 2009 at 8:55 AM, Dave Angel > wrote: > > > You don't need a counter. len() will tell you the size of the list > of primes. > > > Does len() go through and count by itself, or does it actually keep > track of the size and just return the memory? I always thought it would > go through and count, which may take some time. I could be wrong. > [snip] Lists (and strings, tuples, dicts and sets) know how many items they contain. From rridge at csclub.uwaterloo.ca Sat Jul 18 10:55:53 2009 From: rridge at csclub.uwaterloo.ca (Ross Ridge) Date: Sat, 18 Jul 2009 10:55:53 -0400 Subject: Unpack Expects the Wrong Number of Bytes References: Message-ID: Timothy Crone wrote: >header = "s" >data = list(unpack(header,f.read(1))) > >however this: > >header = "si" >data = list(unpack(header,f.read(5))) > >throws > >struct.error: unpack requires a string argument of length 8 > >So unpack expects 7 additional bytes when an integer is added to the >format string. Does anyone know what is going on? It's adding pad bytes so that the format matches the equivilent C structure on your machine. You should use either the "<" little-endian or the ">" big-endian prefix depending on the byte order used in the file you're trying to unpack. You can also use the "=" native byte-order flag if endianness of the file format changes according to the machine your Python program runs on. If you use any of these flags, no extra padding will be inserted. Ross Ridge -- l/ // Ross Ridge -- The Great HTMU [oo][oo] rridge at csclub.uwaterloo.ca -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/ db // From alan.isaac at gmail.com Sat Jul 18 11:31:46 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sat, 18 Jul 2009 15:31:46 GMT Subject: invoke method on many instances In-Reply-To: <02701d35$0$5185$c3e8da3@news.astraweb.com> References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: > On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >> def apply2(itr, methodname, *args, **kwargs): >> f = operator.methodcaller(methodname, *args, **kwargs) >> for item in itr: >> f(item) On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: > for obj in objects: > getattr(obj, methodname)(*args, **kwargs) Are there any obvious considerations in choosing between those two? > See also these recipes from the "Python Cookbook": > http://code.activestate.com/recipes/52289/ > http://code.activestate.com/recipes/87370/ Interesting. Thanks, Alan From REMpeteOVE at CAPpetezilla.ITALSco.uk Sat Jul 18 11:41:33 2009 From: REMpeteOVE at CAPpetezilla.ITALSco.uk (Peter Chant) Date: Sat, 18 Jul 2009 16:41:33 +0100 Subject: Python graphics / imaging library References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: Max Erickson wrote: > More recent months contain updates to the status of 1.1.7, it is > headed towards a release. Preliminary tarballs and binaries are > available on effbot.org: > > http://effbot.org/downloads/#imaging > http://effbot.org/downloads/#pil Excellent. From a very brief look it seems like it will be quite simple to use. Pete -- http://www.petezilla.co.uk From tkermode at gmail.com Sat Jul 18 11:50:44 2009 From: tkermode at gmail.com (Tom Kermode) Date: Sat, 18 Jul 2009 16:50:44 +0100 Subject: A Bug By Any Other Name ... In-Reply-To: References: Message-ID: Maybe the IDE is the best place to warn you of something like that. You could have an IDE where you specify which language you're more familiar with and then have it display warnings likely to be relevant to you. People could collaborate to add support for gradually more niche languages. Python could continue to work the way it was designed to and people documenting python wouldn't have to worry about this kind of issue. :) 2009/7/6 Lawrence D'Oliveiro : > I wonder how many people have been tripped up by the fact that > > ? ?++n > > and > > ? ?--n > > fail silently for numeric-valued n. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.kiloday.com http://www.fourstopspast.com From marcusw at cox.net Sat Jul 18 11:58:47 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 18 Jul 2009 11:58:47 -0400 Subject: Python graphics / imaging library In-Reply-To: References: <9gp8j6xuh6.ln2@phoenix.fire> Message-ID: On 7/18/2009 11:41 AM, Peter Chant wrote: > Max Erickson wrote: > >> More recent months contain updates to the status of 1.1.7, it is >> headed towards a release. Preliminary tarballs and binaries are >> available on effbot.org: >> >> http://effbot.org/downloads/#imaging >> http://effbot.org/downloads/#pil > > Excellent. From a very brief look it seems like it will be quite simple to > use. > > Pete > > Yes, it was fun to work with (for me at least). Marcus From gnarlodious at gmail.com Sat Jul 18 12:32:18 2009 From: gnarlodious at gmail.com (Gnarlodious) Date: Sat, 18 Jul 2009 09:32:18 -0700 (PDT) Subject: Rus Python script interactively Message-ID: In an interactive session (I am using iPython), what is the most elegant way to run a Python script from Terminal? Right now I am saying: import subprocess subprocess.call("python /path/to/scriptname.py", shell=True) But I am calling a shell process and I'd rather not. The script just runs, no inputs are needed. Is there a more Pythonesque method? -- Gnarlie http://Gnarlodious.com/ From deets at nospam.web.de Sat Jul 18 12:39:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 18 Jul 2009 18:39:53 +0200 Subject: Rus Python script interactively In-Reply-To: References: Message-ID: <7cec6pF26munvU1@mid.uni-berlin.de> Gnarlodious schrieb: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? If it's in the path, you can just import it. That will of course only be possible once per ipython-session. Diez From lie.1296 at gmail.com Sat Jul 18 12:55:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 19 Jul 2009 02:55:49 +1000 Subject: Rus Python script interactively In-Reply-To: References: Message-ID: Gnarlodious wrote: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? > > -- Gnarlie > http://Gnarlodious.com/ IMHO, starting a new terminal and runs the script from there. Or quitting/pausing the current interactive session and run it like normal python scripts. Alternatively, you may also want to look at `execfile` built-in function. >>> execfile('myscript.py', {}, {}) From marcusw at cox.net Sat Jul 18 13:04:07 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 18 Jul 2009 13:04:07 -0400 Subject: Rus Python script interactively In-Reply-To: References: Message-ID: On 7/18/2009 12:32 PM, Gnarlodious wrote: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? > > -- Gnarlie > http://Gnarlodious.com/ You could put the code of the script in a main() function and have an if __name__ == '__main__': around the internal call. That way, it will still run the code if you run it normally, and you can also run it several times from the interactive session, ie: file you want to run: {{{ def somefunction(): print 'hello world' somefunction() }}} file after modifications: {{{ def somefunction(): print 'hello world' def main(): somefunction() if __name__ == '__main__': #only true if the file is being run normally, not imported. main() }}} Both of those, if run normally, will print "hello world". If the first one is imported, it will run once and not me runnable in that session again. If the second one is imported, it will not do anything until you call the main() function, and then you can call it again as many times as you want: {{{ >>> import thefile >>> for i in range(5): ... thefile.main() ... hello world hello world hello world hello world hello world >>> exit() }}} Hope this helps you! Marcus From __peter__ at web.de Sat Jul 18 13:13:17 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Jul 2009 19:13:17 +0200 Subject: Rus Python script interactively References: Message-ID: Gnarlodious wrote: > In an interactive session (I am using iPython), what is the most > elegant way to run a Python script from Terminal? Right now I am > saying: > > import subprocess > subprocess.call("python /path/to/scriptname.py", shell=True) > > But I am calling a shell process and I'd rather not. The script just > runs, no inputs are needed. Is there a more Pythonesque method? After a quick glance over ipython's %quickref: In [1]: !mkdir -p path/to In [2]: !echo 'print "works!"' > path/to/scriptname.py In [3]: %run path/to/scriptname.py works! From timphotoman at gmail.com Sat Jul 18 13:25:11 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 13:25:11 -0400 Subject: python command running old version Message-ID: My brain is running in n00b mode this morning...must find coffee. I upgraded python this morning and entering python from the command line runs the old version. Just looked and it appears the old version is in /usr/bin while the new one is in /usr/local/bin/ Besides changing the path order, how do I ensure it runs the new version? I'm sure I'll bang my head on the desk in shame as soon as I'm reminded. Thanks. From clp2 at rebertia.com Sat Jul 18 13:26:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 10:26:40 -0700 Subject: python command running old version In-Reply-To: References: Message-ID: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> On Sat, Jul 18, 2009 at 10:25 AM, Tim Edwards wrote: > My brain is running in n00b mode this morning...must find coffee. > > I upgraded python this morning and entering python from the command > line runs the old version. Which OS? How did you install it? Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Sat Jul 18 13:32:25 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 10:32:25 -0700 Subject: Auto Send URL In-Reply-To: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> References: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> Message-ID: <50697b2c0907181032x6f0029dfs304a345ca2835690@mail.gmail.com> On Fri, Jul 17, 2009 at 6:02 AM, Victor Subervi wrote: > Hi; > I am trying to script code that automatically sends a Web site visitor to an > URL. Specifically, when they enter a value in a search box, I have that form > sent to a script that writes an URL acceptable to Google, then I want to > send the visitor off without him having to click another button. How do I do > this? Send back a webpage with a Refresh meta tag: Cheers, Chris -- http://blog.rebertia.com From lists at cheimes.de Sat Jul 18 13:33:48 2009 From: lists at cheimes.de (Christian Heimes) Date: Sat, 18 Jul 2009 19:33:48 +0200 Subject: python command running old version In-Reply-To: References: Message-ID: Tim Edwards wrote: > Besides changing the path order, how do I ensure it runs the new > version? I'm sure I'll bang my head on the desk in shame as soon as > I'm reminded. hash -r Christian From timphotoman at gmail.com Sat Jul 18 13:37:50 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 13:37:50 -0400 Subject: python command running old version In-Reply-To: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> References: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> Message-ID: > Which OS? How did you install it? Sorry (see I need that coffee) Installed on redhat from source From timphotoman at gmail.com Sat Jul 18 13:54:31 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 13:54:31 -0400 Subject: python command running old version In-Reply-To: References: Message-ID: >> Besides changing the path order, how do I ensure it runs the new >> version? I'm sure I'll bang my head on the desk in shame as soon as >> I'm reminded. > > hash -r Thanks. From lie.1296 at gmail.com Sat Jul 18 13:58:13 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 19 Jul 2009 03:58:13 +1000 Subject: python command running old version In-Reply-To: References: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> Message-ID: Tim Edwards wrote: >> Which OS? How did you install it? > > Sorry (see I need that coffee) > > Installed on redhat from source You should consult the distro's (i.e. RedHat's) documentation/mailing list about changing the default python interpreter. In most cases, you will need to make sure all python-related packages are compatible with the new python version. In popular distros, there should be a script that will do that for you. From akhilanger at gmail.com Sat Jul 18 14:20:20 2009 From: akhilanger at gmail.com (akhil1988) Date: Sat, 18 Jul 2009 11:20:20 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24550497.post@talk.nabble.com> Thanks Nobody-38, it solved my problem immediately. --Thanks Again, Akhil Nobody-38 wrote: > > On Thu, 16 Jul 2009 20:26:39 -0700, akhil1988 wrote: > >> Well, you were write: unintentionally I removed strip(). But the problem >> does >> not ends here: >> >> I get this error now: >> >> File "./temp.py", line 488, in >> main() >> File "./temp.py", line 475, in main >> for line in sys.stdin: >> File "/usr/local/lib/python3.1/codecs.py", line 300, in decode >> (result, consumed) = self._buffer_decode(data, self.errors, final) >> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: >> invalid >> data >> >> for this line: >> ? > > Right. You're running in a locale whose encoding is UTF-8, but feeding > data which isn't valid UTF-8 to stdin. If you want to use data with a > different encoding, you need to replace sys.stdin, e.g.: > > import sys > import io > sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding = 'iso-8859-1') > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24550497.html Sent from the Python - python-list mailing list archive at Nabble.com. From akhilanger at gmail.com Sat Jul 18 14:25:53 2009 From: akhilanger at gmail.com (akhil1988) Date: Sat, 18 Jul 2009 11:25:53 -0700 (PDT) Subject: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) In-Reply-To: References: <24509879.post@talk.nabble.com> <50697b2c0907152139x13b501b0w24a6d84135923f61@mail.gmail.com> <24510222.post@talk.nabble.com> <4a5ed587$0$1627$742ec2ed@news.sonic.net> <63e1a8d1-b9ad-4275-95e6-54fad122fd05@l35g2000pra.googlegroups.com> <24522412.post@talk.nabble.com> Message-ID: <24550540.post@talk.nabble.com> Thanks David, it solved my problem immediately. I will follow your advise from next time but honestly I am new to python with not much knowledge about text formats. And the main portion of my project was not to deal with these, so I just wanted to get this solved as I was already struck at this for 2 days. If you think I am wrong in my approach to getting problems solved, please let me know. Your advise would be helpful in future for me. --Thanks Again, Akhil Scott David Daniels wrote: > > akhil1988 wrote: > > >> Nobody-38 wrote: >>> On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote: > ... >>>>> In Python 3 you can't decode strings because they are Unicode strings >>>>> and it doesn't make sense to decode a Unicode string. You can only >>>>> decode encoded things which are byte strings. So you are mixing up >>>>> byte >>>>> strings and Unicode strings. >>>> ... I read a byte string from sys.stdin which needs to converted to >>>> unicode >>>> string for further processing. >>> In 3.x, sys.stdin (stdout, stderr) are text streams, which means that >>> they >>> read and write Unicode strings, not byte strings. >>> >>>> I cannot just remove the decode statement and proceed? >>>> This is it what it looks like: >>>> for line in sys.stdin: >>>> line = line.decode('utf-8').strip() >>>> if line == '': #do something here >>>> .... >>>> If I remove the decode statement, line == '' never gets true. >>> Did you inadvertently remove the strip() as well? >> ... unintentionally I removed strip().... >> I get this error now: >> File "./temp.py", line 488, in >> main() >> File "./temp.py", line 475, in main >> for line in sys.stdin: >> File "/usr/local/lib/python3.1/codecs.py", line 300, in decode >> (result, consumed) = self._buffer_decode(data, self.errors, final) >> UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: >> invalid >> data > > (1) Do not top post. > (2) Try to fully understand the problem and proposed solution, rather > than trying to get people to tell you just enough to get your code > going. > (3) The only way sys.stdin can possibly return unicode is to do some > decoding of its own. your job is to make sure it uses the correct > decoding. So, if you know your source is always utf-8, try > something like: > > import sys > import io > > sys.stdin = io.TextIOWrapper(sys.stdin.detach(), encoding='utf8') > > for line in sys.stdin: > line = line.strip() > if line == '': > #do something here > .... > > --Scott David Daniels > Scott.Daniels at Acm.Org > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24550540.html Sent from the Python - python-list mailing list archive at Nabble.com. From timphotoman at gmail.com Sat Jul 18 15:35:19 2009 From: timphotoman at gmail.com (Tim Edwards) Date: Sat, 18 Jul 2009 15:35:19 -0400 Subject: python command running old version In-Reply-To: References: <50697b2c0907181026p2ad3fb44mba3570ed44e17aeb@mail.gmail.com> Message-ID: > You should consult the distro's (i.e. RedHat's) documentation/mailing > list about changing the default python interpreter. In most cases, you > will need to make sure all python-related packages are compatible with > the new python version. In popular distros, there should be a script > that will do that for you. Thanks, I'll look into that. From sgm at objexx.com Sat Jul 18 16:49:42 2009 From: sgm at objexx.com (SK) Date: Sat, 18 Jul 2009 13:49:42 -0700 (PDT) Subject: multiprocessing and freezing on Windows References: <20282f53-a848-4f44-bad7-c7d781294369@c36g2000yqn.googlegroups.com> Message-ID: <015ab41f-7433-4136-8b9c-809f96ae6384@o13g2000vbl.googlegroups.com> Thanks Gabriel. Posted as: http://bugs.python.org/issue6461 The multiprocessing author has tentatively confirmed the bug. From twgray2007 at gmail.com Sat Jul 18 17:33:48 2009 From: twgray2007 at gmail.com (twgray) Date: Sat, 18 Jul 2009 14:33:48 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? Message-ID: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> I am attempting to send a jpeg image file created on an embedded device over a wifi socket to a Python client running on a Linux pc (Ubuntu). All works well, except I don't know, on the pc client side, what the file size is? The following is a snippet: [code] f = open("frame.jpg",mode = 'wb') while True: data = self.s.recv(MAXPACKETLEN) if len(data) == 0: break recvd += len(data) f.write(data) f.close() [end] It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on the final packet, which will always be less than MAXPACKETLEN. I guess my question is, how do I detect end of data on the client side? From irmen.NOSPAM at xs4all.nl Sat Jul 18 17:43:47 2009 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sat, 18 Jul 2009 23:43:47 +0200 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> Message-ID: <4a624251$0$191$e4fe514c@news.xs4all.nl> twgray wrote: > I am attempting to send a jpeg image file created on an embedded > device over a wifi socket to a Python client running on a Linux pc > (Ubuntu). All works well, except I don't know, on the pc client side, > what the file size is? You don't. Sockets are just endless streams of bytes. You will have to design some form of 'wire protocol' that includes the length of the message that is to be read. For instance a minimalistic protocol could be the following: Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 bytes, decodes it into the integer that tells it the length, and then reads the correct amount of bytes from the socket. --irmen From gnarlodious at gmail.com Sat Jul 18 17:48:24 2009 From: gnarlodious at gmail.com (Gnarlodious) Date: Sat, 18 Jul 2009 14:48:24 -0700 (PDT) Subject: Rus Python script interactively References: Message-ID: <0814d92c-46bf-4421-88ea-499cb82a0be8@w41g2000yqb.googlegroups.com> Thanks for all the suggestions! The last solution is the one I was ooking for, I am really starting to like iPython. Learning all kinds of new tricks!. -- Gnarlie From tycho at tycho.ws Sat Jul 18 17:52:41 2009 From: tycho at tycho.ws (Tycho Andersen) Date: Sat, 18 Jul 2009 16:52:41 -0500 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <4a624251$0$191$e4fe514c@news.xs4all.nl> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> Message-ID: <49b3a7400907181452t3f87ee39n17b3dc15c3426d34@mail.gmail.com> On Sat, Jul 18, 2009 at 4:43 PM, Irmen de Jong wrote: > twgray wrote: >> >> I am attempting to send a jpeg image file created on an embedded >> device over a wifi socket to a Python client running on a Linux pc >> (Ubuntu). ?All works well, except I don't know, on the pc client side, >> what the file size is? > > You don't. Sockets are just endless streams of bytes. You will have to > design some form of 'wire protocol' that includes the length of the message > that is to be read. > For instance a minimalistic protocol could be the following: > Send 4 bytes that contain the length (an int) then the data itself. The > client reads 4 bytes, decodes it into the integer that tells it the length, > and then reads the correct amount of bytes from the socket. Exactly, sending the length first is the only way to know ahead of time. Alternatively, if you know what the end of the data looks like, you can look for that 'flag' as well, and stop trying to recv() after that. Some things that may be useful, though, are socket.settimeout() and socket.setblocking(). More information is availible in the docs: http://docs.python.org/library/socket.html. You need to be careful with this, though, since network latency may cause problems. Using these methods will keep your program from sitting in recv() forever, though. \t -- http://tycho.ws From twgray2007 at gmail.com Sat Jul 18 18:04:41 2009 From: twgray2007 at gmail.com (twgray) Date: Sat, 18 Jul 2009 15:04:41 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> Message-ID: <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> On Jul 18, 4:43?pm, Irmen de Jong wrote: > twgray wrote: > > I am attempting to send a jpeg image file created on an embedded > > device over a wifi socket to a Python client running on a Linux pc > > (Ubuntu). ?All works well, except I don't know, on the pc client side, > > what the file size is? ? > > You don't. Sockets are just endless streams of bytes. You will have to design some form > of 'wire protocol' that includes the length of the message that is to be read. > For instance a minimalistic protocol could be the following: > Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 > bytes, decodes it into the integer that tells it the length, and then reads the correct > amount of bytes from the socket. > > --irmen Thanks for the reply. But, now I have a newbie Python question. If I send a 4 byte address from the embedded device, how do I convert that, in Python, to a 4 byte, or long, number? From tjreedy at udel.edu Sat Jul 18 18:30:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 18 Jul 2009 18:30:08 -0400 Subject: invoke method on many instances In-Reply-To: References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: Alan G Isaac wrote: >> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>> def apply2(itr, methodname, *args, **kwargs): >>> f = operator.methodcaller(methodname, *args, **kwargs) >>> for item in itr: >>> f(item) > > > On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >> for obj in objects: >> getattr(obj, methodname)(*args, **kwargs) > > > Are there any obvious considerations in choosing > between those two? I would use the straightforward getattr idiom. From rickbking at comcast.net Sat Jul 18 18:30:52 2009 From: rickbking at comcast.net (Rick King) Date: Sat, 18 Jul 2009 18:30:52 -0400 Subject: uniicode and executing a process with subprocess.call, or os.system Message-ID: <4A624D1C.5010503@comcast.net> Hello, I want to copy files using subprocess.call or os.system where the file names are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. Windows stores all the file names in unicode so they are displayed ok in explorer, and I can read them into my program with listdir(u'.'), etc. and work with the names in the program. os.rename() can be used to rename such files successfully. But I want to be able to copy files using: cmdstr = u'copy' +u' /Y "'+pair[0]+u'" "'+pair[1]+u'"\n' cmdstr = cmdstr.encode(sys.getfilesystemencoding()) try: retcode = sp.call(cmdstr, shell=True) #SP=SUBPROCESS but the encoding can't handle all the characters and so the file isn't found to be copied. sp.call() returns 1. 'mbcs' encoding doesn't work either. 'utf-8' doesn't work. I am very confused about unicode. Can someone point me in the right direction? windows xp sp2 python 2.6.2 unicode Thanks! Rick King Southfield MI From python at mrabarnett.plus.com Sat Jul 18 19:05:48 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 00:05:48 +0100 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> Message-ID: <4A62554C.3040100@mrabarnett.plus.com> twgray wrote: > On Jul 18, 4:43 pm, Irmen de Jong wrote: >> twgray wrote: >>> I am attempting to send a jpeg image file created on an embedded >>> device over a wifi socket to a Python client running on a Linux pc >>> (Ubuntu). All works well, except I don't know, on the pc client side, >>> what the file size is? >> You don't. Sockets are just endless streams of bytes. You will have to design some form >> of 'wire protocol' that includes the length of the message that is to be read. >> For instance a minimalistic protocol could be the following: >> Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 >> bytes, decodes it into the integer that tells it the length, and then reads the correct >> amount of bytes from the socket. >> > Thanks for the reply. But, now I have a newbie Python question. If I > send a 4 byte address from the embedded device, how do I convert that, > in Python, to a 4 byte, or long, number? If you send the length as 4 bytes then you'll have to decide whether it's big-endian or little-endian. An alternative is to send the length as characters, terminated by, say, '\n' or chr(0). From clp2 at rebertia.com Sat Jul 18 19:06:51 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 18 Jul 2009 16:06:51 -0700 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: <4A624D1C.5010503@comcast.net> References: <4A624D1C.5010503@comcast.net> Message-ID: <50697b2c0907181606n2af8577ew3ad178f0463fa2d8@mail.gmail.com> On Sat, Jul 18, 2009 at 3:30 PM, Rick King wrote: > Hello, > I want to copy files using subprocess.call or os.system where the file names > are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. Windows > stores all the file names in unicode so they are displayed ok in explorer, > and I can read them into my program with listdir(u'.'), etc. and work with > the names in the program. You should try one of the copying functions in the shutil module instead, it'll be much simpler than using subprocess: http://docs.python.org/library/shutil.html Cheers, Chris -- http://blog.rebertia.com From python at mrabarnett.plus.com Sat Jul 18 19:07:12 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 00:07:12 +0100 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: <4A624D1C.5010503@comcast.net> References: <4A624D1C.5010503@comcast.net> Message-ID: <4A6255A0.20804@mrabarnett.plus.com> Rick King wrote: > Hello, > I want to copy files using subprocess.call or os.system where the file > names are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. > Windows stores all the file names in unicode so they are displayed ok in > explorer, and I can read them into my program with listdir(u'.'), etc. > and work with the names in the program. > > os.rename() > > can be used to rename such files successfully. > > But I want to be able to copy files using: > > cmdstr = u'copy' +u' /Y "'+pair[0]+u'" "'+pair[1]+u'"\n' > cmdstr = cmdstr.encode(sys.getfilesystemencoding()) > try: retcode = sp.call(cmdstr, shell=True) #SP=SUBPROCESS > > but the encoding can't handle all the characters and so the file isn't > found to be copied. sp.call() returns 1. 'mbcs' encoding doesn't work > either. 'utf-8' doesn't work. > > I am very confused about unicode. Can someone point me in the right > direction? > > windows xp sp2 > python 2.6.2 unicode > Use the shutil module. From aahz at pythoncraft.com Sat Jul 18 19:18:22 2009 From: aahz at pythoncraft.com (Aahz) Date: 18 Jul 2009 16:18:22 -0700 Subject: python command running old version References: Message-ID: In article , Tim Edwards wrote: > >I upgraded python this morning and entering python from the command >line runs the old version. > >Just looked and it appears the old version is in /usr/bin while the >new one is in /usr/local/bin/ > >Besides changing the path order, how do I ensure it runs the new >version? I'm sure I'll bang my head on the desk in shame as soon as >I'm reminded. You probably want to create an alias for command-line usage; scripts should use the full path to the interpreter you want. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From sjmachin at lexicon.net Sat Jul 18 19:26:21 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 18 Jul 2009 16:26:21 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> Message-ID: On Jul 19, 8:04?am, twgray wrote: > send a 4 byte address from the embedded device, how do I convert that, > in Python, to a 4 byte, or long, number? struct.unpack() is your friend. Presuming the embedded device is little-endian, you do: the_int = struct.unpack(' Message-ID: On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote: > It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on > the final packet, which will always be less than MAXPACKETLEN. > > I guess my question is, how do I detect end of data on the client side? recv() should return zero when the sender closes its end of the connection. Is the sender actually closing its end? If you are unsure, use a packet sniffer such as tcpdump to look for a packet with the FIN flag. If you need to keep the connection open for further transfers, you need to incorporate some mechanism for identifying the end of the data into the protocol. As others have suggested, prefixing the data by its length is one option. Another is to use an end-of-data marker, but then you need a mechanism to "escape" the marker if it occurs in the data. A length prefix is probably simpler to implement, but has the disadvantage that you can't start sending the data until you know how long it is going to be. From foo at coo.com Sat Jul 18 19:59:45 2009 From: foo at coo.com (Dave) Date: Sun, 19 Jul 2009 00:59:45 +0100 Subject: Newbie question - running a command and looking at output Message-ID: <4a6261ff@212.67.96.135> I'm trying to run a command (arch -k) and check if the value returned is 'sun4v' or not. kirkby at t2:[~] $ arch -k sun4v In fact, I want to do 3 three things 1) Check if the system is Solaris. 2) If it is Solaris, check if 'arch -k' prints 'sun4v' 3) If both 1 and 2 are true, copy a file. Since arch -k is not supported on all systems, the test for Solaris must be made first. I have a test for Solaris (aka SunOS), which copies a file if the systems is running Solaris: import os, shutil if os.uname()[0] == 'SunOS': shutil.copy2('foo.c','bar.c') How can I change that, so the copy is only performed if 'arch -k' prints 'sun4v'? -- I respectfully request that this message is not archived by companies as unscrupulous as 'Experts Exchange' . In case you are unaware, 'Experts Exchange' take questions posted on the web and try to find idiots stupid enough to pay for the answers, which were posted freely by others. They are leeches. From foo at coo.com Sat Jul 18 20:08:05 2009 From: foo at coo.com (Dave) Date: Sun, 19 Jul 2009 01:08:05 +0100 Subject: Newbie question - running a command and looking at output In-Reply-To: <4a6261ff@212.67.96.135> References: <4a6261ff@212.67.96.135> Message-ID: <4a6263f3@212.67.96.135> Dave wrote: > I'm trying to run a command (arch -k) and check if the value returned is > 'sun4v' or not. > > > kirkby at t2:[~] $ arch -k > sun4v > > In fact, I want to do 3 three things > > 1) Check if the system is Solaris. > 2) If it is Solaris, check if 'arch -k' prints 'sun4v' > 3) If both 1 and 2 are true, copy a file. > > > Since arch -k is not supported on all systems, the test for Solaris must > be made first. > > I have a test for Solaris (aka SunOS), which copies a file if the > systems is running Solaris: > > import os, shutil > > if os.uname()[0] == 'SunOS': > shutil.copy2('foo.c','bar.c') > > How can I change that, so the copy is only performed if 'arch -k' prints > 'sun4v'? > > > Actually, when I look at oluname, it does print sunnv: >>> import os >>> os.uname() ('SunOS', 't2', '5.10', 'Generic_141414-02', 'sun4v') I guess all I need to do is check for the value of os.uname()[4] too. -- I respectfully request that this message is not archived by companies as unscrupulous as 'Experts Exchange' . In case you are unaware, 'Experts Exchange' take questions posted on the web and try to find idiots stupid enough to pay for the answers, which were posted freely by others. They are leeches. From sjmachin at lexicon.net Sat Jul 18 20:12:32 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 18 Jul 2009 17:12:32 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> Message-ID: <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> On Jul 19, 7:43?am, Irmen de Jong wrote: > twgray wrote: > > I am attempting to send a jpeg image file created on an embedded > > device over a wifi socket to a Python client running on a Linux pc > > (Ubuntu). ?All works well, except I don't know, on the pc client side, > > what the file size is? ? > > You don't. Sockets are just endless streams of bytes. You will have to design some form > of 'wire protocol' that includes the length of the message that is to be read. Apologies in advance for my ignorance -- the last time I dipped my toe in that kind of water, protocols like zmodem and Kermit were all the rage -- but I would have thought there would have been an off-the- shelf library for peer-to-peer file transfer over a socket interface ... not so? From python at mrabarnett.plus.com Sat Jul 18 20:33:32 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 01:33:32 +0100 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> Message-ID: <4A6269DC.1010409@mrabarnett.plus.com> Nobody wrote: > On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote: > >> It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on >> the final packet, which will always be less than MAXPACKETLEN. >> >> I guess my question is, how do I detect end of data on the client side? > > recv() should return zero when the sender closes its end of the connection. > > Is the sender actually closing its end? If you are unsure, use a packet > sniffer such as tcpdump to look for a packet with the FIN flag. > > If you need to keep the connection open for further transfers, you need to > incorporate some mechanism for identifying the end of the data into the > protocol. As others have suggested, prefixing the data by its length is > one option. Another is to use an end-of-data marker, but then you need a > mechanism to "escape" the marker if it occurs in the data. A length prefix > is probably simpler to implement, but has the disadvantage that you can't > start sending the data until you know how long it is going to be. > You could send it in chunks, ending with a chunk length of zero. From twgray2007 at gmail.com Sat Jul 18 22:02:05 2009 From: twgray2007 at gmail.com (twgray) Date: Sat, 18 Jul 2009 19:02:05 -0700 (PDT) Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> Message-ID: <2b0e3c3c-2beb-46c3-acf6-86aee8caa2ed@b15g2000yqd.googlegroups.com> On Jul 18, 7:33?pm, MRAB wrote: > Nobody wrote: > > On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote: > > >> It appears to be locking up in ?'data=self.s.recv(MAXPACKETLEN)' on > >> the final packet, which will always be less than MAXPACKETLEN. > > >> I guess my question is, how do I detect end of data on the client side? > > > recv() should return zero when the sender closes its end of the connection. > > > Is the sender actually closing its end? If you are unsure, use a packet > > sniffer such as tcpdump to look for a packet with the FIN flag. > > > If you need to keep the connection open for further transfers, you need to > > incorporate some mechanism for identifying the end of the data into the > > protocol. As others have suggested, prefixing the data by its length is > > one option. Another is to use an end-of-data marker, but then you need a > > mechanism to "escape" the marker if it occurs in the data. A length prefix > > is probably simpler to implement, but has the disadvantage that you can't > > start sending the data until you know how long it is going to be. > > You could send it in chunks, ending with a chunk length of zero. Thanks for the help! From aahz at pythoncraft.com Sat Jul 18 22:46:01 2009 From: aahz at pythoncraft.com (Aahz) Date: 18 Jul 2009 19:46:01 -0700 Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <243077b4-2f96-4b60-afe3-cd0605a70f11@c14g2000yqm.googlegroups.com> Message-ID: In article , MRAB wrote: > >If you send the length as 4 bytes then you'll have to decide whether >it's big-endian or little-endian. An alternative is to send the length >as characters, terminated by, say, '\n' or chr(0). Alternatively, make it a fixed-length string of bytes, zero-padded in front. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From piet at cs.uu.nl Sun Jul 19 02:55:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 08:55:20 +0200 Subject: How to receive a data file of unknown length using a python socket? References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> Message-ID: >>>>> John Machin (JM) wrote: >JM> On Jul 19, 7:43?am, Irmen de Jong wrote: >>> twgray wrote: >>> > I am attempting to send a jpeg image file created on an embedded >>> > device over a wifi socket to a Python client running on a Linux pc >>> > (Ubuntu). ?All works well, except I don't know, on the pc client side, >>> > what the file size is? ? >>> >>> You don't. Sockets are just endless streams of bytes. You will have to design some form >>> of 'wire protocol' that includes the length of the message that is to be read. >JM> Apologies in advance for my ignorance -- the last time I dipped my toe >JM> in that kind of water, protocols like zmodem and Kermit were all the >JM> rage -- but I would have thought there would have been an off-the- >JM> shelf library for peer-to-peer file transfer over a socket >JM> interface ... not so? Yes, many of them, for example HTTP or FTP. But I suppose they are overkill in this situation. There are also remote procedure call protocols which can do much more, like XMLRPC. By the way if the image file is the only thing you send, the client should close the socket after sending and then the receiver will detect end of file which will be detected by your `if len(data) == 0:' -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jeremy.cowles at gmail.com Sun Jul 19 03:12:47 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Sun, 19 Jul 2009 00:12:47 -0700 Subject: Import hashlib fails, embedded Message-ID: <373cf0740907190012q74da1d22le6b1bd03930acd78@mail.gmail.com> I'm working on an embeddded Python interpreter (using the c-api) where we are loading a custom, zipped copy of the standard Python library (./lib/python25.zip). Everything is working fine, but when I try to "import hashlib", i get the following error: Traceback (most recent call last): File "tryrun.py", line 2, in import hashlib File "~/workspace/pyboinc/lib/python25.zip/hashlib.py", line 133, in File "~/workspace/pyboinc/lib/python25.zip/hashlib.py", line 60, in __get_builtin_constructor ImportError: No module named _md5 I assume this is related to a builtin or dynamically loaded C module, but I'm not sure exactly what the problem is. Can anyone explain to me what is going wrong? Other modules (like sys, os) are working fine, which may be because they are pure-Python modules. Here is the main function: int main(int argc, const char* argv[]) { if (argc < 2) { cerr << "Module name missing\n"; cerr << "usage: " << argv[0] << " module" << endl; return 1; } // get the python library name char* libname = get_lib_name(); // try to copy the library from ./libname to ./lib/libname if (!copy_lib(libname)) { return 1; } Py_SetProgramName((char*)argv[0]); Py_SetPythonHome("./"); Py_Initialize(); if (argc > 0){ PySys_SetArgv(argc-1, (char**)(argv+1)); } { PyObject* syspath = PySys_GetObject("path"); if (!syspath) { cerr << "Couldn't get sys.path" << endl; return 1; } if (!PyList_Check(syspath)) { cerr << "sys.path not a list" << endl; return 1; } PyObject* str = PyString_FromString("."); PyList_Append(syspath, str); Py_DECREF(str); } Py_InitModule("boinc", BoincMethods); PyObject* name = PyString_FromString(argv[1]); PyObject* mod = PyImport_Import(name); Py_DECREF(name); if (!mod) { cerr << "Error loading module" << endl; PyErr_Print(); return 1; } Py_DECREF(mod); Py_Finalize(); } -- Jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Jul 19 03:21:15 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 19 Jul 2009 09:21:15 +0200 Subject: how two join and arrange two files together References: <6098.210.212.36.65.1247900961.squirrel@www.iisermohali.ac.in> <50697b2c0907180019l1efd087dv40f6a29b797e42b5@mail.gmail.com> Message-ID: amrita at iisermohali.ac.in wrote: [please keep the correspondence on the mailing list/newsgroup] > It is working sir, but my datas are on file when i replaced StringIO("") > with open("filename.txt") then it is not printing the result properly, > like in one file i have data like:--- > 33 ALA H = 7.57 N = 121.52 CA = 55.58 HA = 3.89 C = 179.24 > 38 ALA H = 8.29 N = 120.62 CA = 54.33 HA = 4.04 C = 178.95 > 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = 4.05 C = 179.35 > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 > in other:--- > 8 ALA helix (helix_alpha, helix1) > 21 ALA helix (helix_alpha, helix2) > 23 ALA helix (helix_alpha, helix2) > 33 ALA helix (helix_alpha, helix3) > 38 ALA helix (helix_alpha, helix3) > 49 ALA bend > and it is giving the result:- > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18| > 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93|23 ALA helix > (helix_alpha, helix2) > 38 ALA H = 8.29 N = 120.62 CA = 54.33 HA = 4.04 C = 178.95|38 ALA helix > (helix_alpha, helix3) > |49 ALA bend > 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39|8 ALA helix > (helix_alpha, helix1) > it is not printing the result for 33 and 21. Hint: you have to adapt the key() function from def key(line): return line[:1] to something that returns the same key for lines in the two files that belong together, and different keys for lines that don't. Peter From vs at it.uu.se Sun Jul 19 04:04:10 2009 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 19 Jul 2009 10:04:10 +0200 Subject: On out-of-date Python Applications Message-ID: <4A62D37A.5080105@it.uu.se> I am not a heavy user of Python; but, I do work with it and some of its application packages (e.g. PyODE), in an academic setting. Many of these applications packages have a Windows installer which usually works fine. However, I also try to keep up with the latest release of Python, and this is where I often have problems. That is, the latest Windows installer provided for some of these applications will not install on the latest version of Python. I do understand that there can be a time lag between the release of Python applications and the latest Python. I also appreciate the work of the people that are responsible for these applications. My question is --- Is there anything I, as a user of an application package that is out-of-date with respect to the latest Python, can do to help in this process of bringing an application up-to-date? --V. Stokes From motoom at xs4all.nl Sun Jul 19 04:38:46 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Sun, 19 Jul 2009 10:38:46 +0200 Subject: On out-of-date Python Applications In-Reply-To: <4A62D37A.5080105@it.uu.se> References: <4A62D37A.5080105@it.uu.se> Message-ID: <4A62DB96.8000302@xs4all.nl> Virgil Stokes wrote: > some of these applications will > not install on the latest version of Python. Which version of Python precisely? -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From mail at microcorp.co.za Sun Jul 19 05:47:07 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 19 Jul 2009 11:47:07 +0200 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> Message-ID: <200907191147.07949.mail@microcorp.co.za> On Sunday 19 July 2009 02:12:32 John Machin wrote: > > Apologies in advance for my ignorance -- the last time I dipped my toe > in that kind of water, protocols like zmodem and Kermit were all the > rage -- but I would have thought there would have been an off-the- > shelf library for peer-to-peer file transfer over a socket > interface ... not so? *Grins at the references to Kermit and zmodem, and remembers Laplink and PC Anywhere* If there is such a transfer beast in Python, I have not found it. (There is an FTP module but that is not quite the same thing) I think it is because the network stuff is all done in the OS or NFS and SAMBA now - with drag and drop support and other nice goodies. I have ended up writing a netstring thingy, that addresses the string transfer problem by having a start sentinel, a four byte ASCII length (so you can see it with a packet sniffer/displayer) and the rest of the data escaped to take out the start sentinel and the escape character. It works, but the four byte ASCII limits the size of what can be sent and received. It guarantees to deliver either the whole string, or fail, or timeout. If anybody is interested I will attach the code here. It is not a big module. This question seems to come up periodically in different guises. To the OP: There are really very few valid ways of solving the string transfer problem, given a featureless stream of bytes like a socket. The first thing that must be addressed is to sync up - you have to somehow find the start of the thing as it comes past. And the second is to find the end of the slug of data that you are transferring. So the simplest way is to designate a byte as a start and end sentinel, and to make sure that such a byte does not occur in the data stream, other than as a start and end marker. This process is called escaping, and the reverse is called unescaping. (SDLC/HDLC does this at a bit pattern level) Another way is to use time, namely to rely on there being some minimum time between slugs of data. This does not work well on TCP/IP sockets, as retries at the lower protocol levels can give you false breaks in the stream. It works well on direct connections like RS-232 or RS-485/422 lines. Classic netstrings send length, then data. They rely on the lower level protocols and the length sent for demarcation of the slug, and work well if you connect, send a slug or two, and disconnect. They are not so hot for long running processes, where processors can drop out while sending - there is no reliable way for a stable receiver to sync up again if it is waiting for a slug that will not finish. Adapting the netstring by adding a sync character and time out is a compromise that I have found works well in practice. - Hendrik From sjmachin at lexicon.net Sun Jul 19 06:17:49 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 19 Jul 2009 03:17:49 -0700 (PDT) Subject: On out-of-date Python Applications References: Message-ID: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> On Jul 19, 6:04?pm, Virgil Stokes wrote: > I am not a heavy user of Python; but, I do work with it and some of its > application packages (e.g. PyODE), in an academic setting. > Many of these applications packages have a Windows installer which > usually works fine. However, I also try to keep up with the latest > release of Python, and this is where I often have problems. That is, the > latest Windows installer provided for some of these applications will > not install on the latest version of Python. If the package was written for 2.x and "the latest version of Python" means 3.X, this is not surprising; your options are (1) wait until the package maintainer releases a 3.x-compatible version (2) port it to 3.x yourself (3) use 2.6 Otherwise: What does "will not install" mean? Are these pure Python packages or do they include C extensions (binary (pyd) or source?)? At what stage of the installation does the installation fail? With what error message(s)? With what versions of Python? > > I do understand that there can be a time lag between the release of > Python applications and the latest Python. I also appreciate the work of > the people that are responsible for these applications. > > My question is --- Is there anything I, as a user of an application > package that is out-of-date with respect to the latest Python, can do to > help in this process of bringing an application ?up-to-date? > > --V. Stokes From r.grimm at science-computing.de Sun Jul 19 07:08:00 2009 From: r.grimm at science-computing.de (Rainer Grimm) Date: Sun, 19 Jul 2009 04:08:00 -0700 (PDT) Subject: invoke method on many instances References: Message-ID: Hallo Alan, > def apply2(itr, methodname, *args, **kwargs): > ? ? f = operator.methodcaller(methodname, *args, **kwargs) > ? ? for item in itr: > ? ? ? ? f(item) you can do it in a functional way. >>> class A(object): ... def hello(self): return "hello: " + str ( self.__class__.__name__ ) ... >>> class B(A):pass ... >>> class C(A):pass ... >>> a=A() >>> b=B() >>> c=C() >>> a.hello() 'hello: A' >>> b.hello() 'hello: B' >>> c.hello() 'hello: C' >>> >>> map( (lambda obj : getattr(obj,"hello")()),(a,b,c)) ['hello: A', 'hello: B', 'hello: C'] >>> [ getattr(obj,"hello")() for obj in (a,b,c)] ['hello: A', 'hello: B', 'hello: C'] Greetings from Rottenburg, Rainer From hubaghdadi at gmail.com Sun Jul 19 07:22:57 2009 From: hubaghdadi at gmail.com (Hussein B) Date: Sun, 19 Jul 2009 04:22:57 -0700 (PDT) Subject: A little help with pexpect Message-ID: Hey, I'm trying to execute a command over a remore server using pexpect +++++++++++++++++ url = 'ssh internalserver' res = pexpect.spawn(url) print '1' res.expect('.*ssword:') print '2' res.sendline('mypasswd') print '3' res.sendline('ls -aslh') +++++++++++++++++ What I want to do is to send a couple of commands and get the response. How to do this? Thanks. From python at bdurham.com Sun Jul 19 09:18:21 2009 From: python at bdurham.com (python at bdurham.com) Date: Sun, 19 Jul 2009 09:18:21 -0400 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <200907191147.07949.mail@microcorp.co.za> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <4a624251$0$191$e4fe514c@news.xs4all.nl> <3960a105-f6cd-47c8-857c-d305b6c69b5e@v37g2000prg.googlegroups.com> <200907191147.07949.mail@microcorp.co.za> Message-ID: <1248009501.29158.1325749595@webmail.messagingengine.com> Hi Hendrik, > I have ended up writing a netstring thingy, that addresses the string transfer problem by having a start sentinel, a four byte ASCII length (so you can see it with a packet sniffer/displayer) and the rest of the data escaped to take out the start sentinel and the escape character. It works, but the four byte ASCII limits the size of what can be sent and received. It guarantees to deliver either the whole string, or fail, or timeout. > If anybody is interested I will attach the code here. It is not a big module. I am interested in seeing your code and would be grateful if you shared it with this list. Thank you, Malcolm From piet at cs.uu.nl Sun Jul 19 09:24:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 15:24:26 +0200 Subject: invoke method on many instances References: Message-ID: >>>>> Rainer Grimm (RG) a ?crit: >RG> Hallo Alan, >>> def apply2(itr, methodname, *args, **kwargs): >>> ? ? f = operator.methodcaller(methodname, *args, **kwargs) >>> ? ? for item in itr: >>> ? ? ? ? f(item) >RG> you can do it in a functional way. >>>>> class A(object): >RG> ... def hello(self): return "hello: " + str >RG> ( self.__class__.__name__ ) >RG> ... >>>>> class B(A):pass >RG> ... >>>>> class C(A):pass >RG> ... >>>>> a=A() >>>>> b=B() >>>>> c=C() >>>>> a.hello() >RG> 'hello: A' >>>>> b.hello() >RG> 'hello: B' >>>>> c.hello() >RG> 'hello: C' >>>>> >>>>> map( (lambda obj : getattr(obj,"hello")()),(a,b,c)) >RG> ['hello: A', 'hello: B', 'hello: C'] >>>>> [ getattr(obj,"hello")() for obj in (a,b,c)] >RG> ['hello: A', 'hello: B', 'hello: C'] But that creates an unnecessary list. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From victorsubervi at gmail.com Sun Jul 19 09:24:43 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 19 Jul 2009 15:24:43 +0200 Subject: Auto Send URL In-Reply-To: <50697b2c0907181032x6f0029dfs304a345ca2835690@mail.gmail.com> References: <4dc0cfea0907170602u2da58ab1l3ad38fc457efa49a@mail.gmail.com> <50697b2c0907181032x6f0029dfs304a345ca2835690@mail.gmail.com> Message-ID: <4dc0cfea0907190624g691688cdlef20acd77b88746b@mail.gmail.com> Ah. How easy! Thank you. On Sat, Jul 18, 2009 at 7:32 PM, Chris Rebert wrote: > On Fri, Jul 17, 2009 at 6:02 AM, Victor Subervi > wrote: > > Hi; > > I am trying to script code that automatically sends a Web site visitor to > an > > URL. Specifically, when they enter a value in a search box, I have that > form > > sent to a script that writes an URL acceptable to Google, then I want to > > send the visitor off without him having to click another button. How do I > do > > this? > > Send back a webpage with a Refresh meta tag: > > > > > > > > Cheers, > Chris > -- > http://blog.rebertia.com > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.isaac at gmail.com Sun Jul 19 09:48:16 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sun, 19 Jul 2009 13:48:16 GMT Subject: are user defined classes hashable? Message-ID: Are user defined classes hashable? (The classes; *not* the instances!) I want to use some classes as dictionary keys. Python is not objecting, but I'm not sure how to think about whether this could be dangerous. I'm inclined to guess it will be hashed by id and this is OK. Thanks for any insights, Alan Isaac From Nicolas.Dandrimont at crans.org Sun Jul 19 10:07:57 2009 From: Nicolas.Dandrimont at crans.org (Nicolas Dandrimont) Date: Sun, 19 Jul 2009 10:07:57 -0400 Subject: are user defined classes hashable? In-Reply-To: References: Message-ID: <20090719140757.GB11029@dirichlet.crans.org> * Alan G Isaac [2009-07-19 13:48:16 +0000]: > Are user defined classes hashable? > (The classes; *not* the instances!) > > I want to use some classes as dictionary keys. > Python is not objecting, > but I'm not sure how to think about > whether this could be dangerous. > I'm inclined to guess it will be hashed by id > and this is OK. You can check for yourself: In [1]: class Foo(object): ...: pass ...: In [2]: foo = Foo() In [3]: hash hash In [3]: hash(foo) Out[3]: 15294992 In [4]: id(foo) Out[4]: 15294992 So yes, by default, user-defined classes are hashable, by id. You can override this behaviour by defining the __hash__ special method on your object. HTH, -- Nicolas Dandrimont -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From mark.dufour at gmail.com Sun Jul 19 10:30:57 2009 From: mark.dufour at gmail.com (Mark Dufour) Date: Sun, 19 Jul 2009 16:30:57 +0200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler Message-ID: <8180ef690907190730t50ccc2f4sb7f489e7c9bb9fb8@mail.gmail.com> Hi all, I have just released version 0.2 of Shed Skin, an experimental (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). It comes with 7 new example programs (for a total of 40 example programs, at over 12,000 lines) and several important improvements/bug fixes. See http://code.google.com/p/shedskin/wiki/ReleaseNotes for the full changelog. The new example programs consist of Disco, an elegant go player (see http://shed-skin.blogspot.com/2009/07/disco-elegant-python-go-player.html), a larger Voronoi implementation at 800 lines, a TSP algorithm simulating ant colonies, a nicer neural network algorithm and three compressors (Lempel-Ziv, huffman block, and arithmetic). Other than bug fixes for these programs, this release adds some important optimizations. First and foremost, inlining was greatly improved, resulting in potential speedups across the board. Second, loops such as 'for a, b in enumerate/zip(sequence[, sequence])' should now be dramatically faster (also inside list comprehensions), by avoiding allocation of intermediate tuples. Finally, basic list slicing should now be much faster. Please try it out! Mark Dufour. -- "One of my most productive days was throwing away 1000 lines of code" - Ken Thompson From alan.isaac at gmail.com Sun Jul 19 10:46:12 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Sun, 19 Jul 2009 14:46:12 GMT Subject: are user defined classes hashable? In-Reply-To: References: Message-ID: > * Alan G Isaac [2009-07-19 13:48:16 +0000]: >> Are user defined classes hashable? >> (The classes; *not* the instances!) >> I'm inclined to guess it will be hashed by id and this is >> OK. On 7/19/2009 10:07 AM Nicolas Dandrimont apparently wrote: > You can check for yourself: > In [1]: class Foo(object): > ...: pass > ...: > In [2]: foo = Foo() > In [3]: hash(foo) > Out[3]: 15294992 > In [4]: id(foo) > Out[4]: 15294992 Again, my question is about the class not its instances, but still, checking as you suggest gives the same answer. Thanks, Alan From piet at cs.uu.nl Sun Jul 19 10:49:09 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 16:49:09 +0200 Subject: A little help with pexpect References: Message-ID: >>>>> Hussein B (HB) wrote: >HB> Hey, >HB> I'm trying to execute a command over a remore server using pexpect >HB> +++++++++++++++++ >HB> url = 'ssh internalserver' >HB> res = pexpect.spawn(url) >HB> print '1' >HB> res.expect('.*ssword:') >HB> print '2' >HB> res.sendline('mypasswd') >HB> print '3' >HB> res.sendline('ls -aslh') >HB> +++++++++++++++++ >HB> What I want to do is to send a couple of commands and get the >HB> response. >HB> How to do this? >HB> Thanks. You can read the output with res.readline() in a loop or similar. The problem is when to stop. You could look in the returned string for the prompt and hope the prompt doesn't accidentally occur in the output. You can also do res.expect(prompt) and get res.before() Same problem with accidentally occurrence of the prompt. Another way is to use a timeout, e.g. with read_nonblocking or specifying a timeout at the spawn call. You can also consider using paramiko instead of pexpect. It gives you considerably more control. For each command you van open a `channel' and that acts more or less similar to a socket, i.e. you get a decent EOF at the end of the command's output. This implies that you have to create a new channel for each command, but it stays within the same SSH connection (SSH allows subconnections). Example: >>> import paramiko ...Mumbles about deprecated modules... >>> hostname='server.example.net' >>> port = 22 >>> t = paramiko.Transport((hostname, port)) >>> username = 'user' >>> password = '********' >>> t.connect(username=username, password=password) Open a channel for a command >>> chan = t.open_session() >>> chan.exec_command('ls -l') >>> chan.recv(999999) 'total 0\ndrwxr-xr-x 2 user group 60 Apr 2 2009 Mail\ndrwx------ 2 user group 6 Dec 27 2008 tmp\n' >>> chan.recv(999999) '' That was end of file. Open a new channel for a new command >>> chan = t.open_session() >>> chan.exec_command('cat') >>> chan.send('abcdefghijklmn\n') 15 >>> chan.recv(99) 'abcdefghijklmn\n' >>> -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From resurtm at gmail.com Sun Jul 19 11:03:21 2009 From: resurtm at gmail.com (resurtm) Date: Sun, 19 Jul 2009 08:03:21 -0700 (PDT) Subject: python, ctypes, callbacks -- access violation when calling callback Message-ID: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> Hello. I'm trying to pass to the C function pointer to callback function from python. But when i'm trying to do this i get access violation within the DLL file when calling python callback. Here is the python side code: from ctypes import * # ... class NewtonBody(Structure): def __init__(self, pointer = 0): self.pointer = pointer # ... class Newton: def __init__(self): self._cdll = CDLL('newton.dll') self.world = NewtonWorld() # ... # NewtonBodySetForceAndTorqueCallback def bodySetForceAndTorqueCallback(self, body): CALLBACK = CFUNCTYPE(c_int, POINTER(NewtonBody), c_float, c_int) def callback(a, b, c): print '1' return 0 self._cdll.NewtonBodySetForceAndTorqueCallback(body.pointer, CALLBACK(callback)) return None Traceback: Traceback (most recent call last): File "Newton.py", line 119, in newton.update(10.5) File "Newton.py", line 42, in update self._cdll.NewtonUpdate(self.world.pointer, c_float(timestep)) WindowsError: exception: access violation reading 0x3C888899 And finally prototype of function which i'm trying to call and callback function type declarations: typedef void (*NewtonApplyForceAndTorque) (const NewtonBody* body, dFloat timestep, int threadIndex); // ... NEWTON_API void NewtonBodySetForceAndTorqueCallback (const NewtonBody* body, NewtonApplyForceAndTorque callback); Can anybody explain my errors when trying to pass callback to DLL function? Thanks for advices and solutions! From lists at cheimes.de Sun Jul 19 11:09:37 2009 From: lists at cheimes.de (Christian Heimes) Date: Sun, 19 Jul 2009 17:09:37 +0200 Subject: python, ctypes, callbacks -- access violation when calling callback In-Reply-To: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> References: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> Message-ID: resurtm wrote: > Can anybody explain my errors when trying to pass callback to DLL > function? > > Thanks for advices and solutions! You have to keep a reference to the callback alive yourself. ctypes doesn't increase the refernece counter of the function when you define a callback. As soon as the ref counter reaches 0, the function object is collected and the pointer to the callback is invalid. You could assign it to a module global or instance variable. Christian From resurtm at gmail.com Sun Jul 19 11:15:33 2009 From: resurtm at gmail.com (resurtm) Date: Sun, 19 Jul 2009 08:15:33 -0700 (PDT) Subject: python, ctypes, callbacks -- access violation when calling callback References: <690ea5a4-1c35-42b4-a259-a3471415668a@m11g2000yqh.googlegroups.com> Message-ID: <8df80a1c-ada2-47a8-86ba-a9ed804c57f6@q11g2000yqi.googlegroups.com> On 19 ???, 21:09, Christian Heimes wrote: > resurtm wrote: > > Can anybody explain my errors when trying to pass callback to DLL > > function? > > > Thanks for advices and solutions! > > You have to keep a reference to the callback alive yourself. ctypes > doesn't increase the refernece counter of the function when you define a > callback. As soon as the ref counter reaches 0, the function object is > collected and the pointer to the callback is invalid. > > You could assign it to a module global or instance variable. > > Christian Thanks for help Christian! It's working fine now! ;-) From forman.simon at gmail.com Sun Jul 19 11:21:01 2009 From: forman.simon at gmail.com (Calroc) Date: Sun, 19 Jul 2009 08:21:01 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> Message-ID: <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> On Jul 9, 1:20 pm, Steven D'Aprano wrote: [...] > You'll excuse my skepticism about all these claims about how anyone can > program, how easy it is to teach the fundamentals of Turing Machines and > functional programming to anybody at all. Prove it. Where are your peer- > reviewed studies demonstrating such successes on randomly chosen people, > not self-selected motivated, higher-than-average intelligence students? I'm engaged presently in starting a school to teach programming from the ground up, based roughly on the curriculum outlined in the article I mentioned. I can't randomly select students but I do intend to gather data about who does how well from what starting conditions. I'm excited about formal methods because one, I just heard about them, and two, they provide me with a much more well-fleshed-out way to bridge rigorously from "raw" Turing machine models to higher order abstractions. I'm confident that anyone who can play Sudoku can learn programming and formal methods. > In the real world, human beings are prone to serious cognitive biases and > errors. Our brains are buggy. Take any list of reasoning fallacies, and > you'll find the majority of people have difficulty avoid them. The first > struggle is to get them to even accept that they *are* fallacies, but > even once they have intellectually accepted it, getting them to live up > to that knowledge in practice is much, much harder. > > In fact I'd go further and say that *every single person*, no matter how > intelligent and well-educated, will fall for cognitive biases and > fallacious reasoning on a regular basis. I agree completely. One of my chief goals is to "debug" human reasoning to whatever extent possible. This is precisely where I expect training in computer programming to come in handy. The machine symbol manipulator is not going to be fooled by reasoning fallacies. Fluency in rigorous symbol manipulation can only help us cope with the vagaries of our too-often sub-rational wetware. I think the real win starts when computers can be used as a communication medium, not for blogs and tweets and SMS txt and IM and VoIP, but for reasoned symbolically coded discussion. On Jul 9, 2:10 pm, Steven D'Aprano wrote: > On Wed, 08 Jul 2009 22:05:57 -0700, Simon Forman wrote: > >> persistent idea "out there" that programming is a very accessible > >> skill, like cooking or gardening, anyone can do it, and even profit > >> from it, monetarily or otherwise, etc., and to some extent I am > > > Programming is not like any other human activity. > > In practice? In principle? Programming in principle is not the same as it > is performed in practice. > > But in either case, programming requires both the logical reasoning of > mathematics and the creativity of the arts. Funnily enough, > mathematicians will tell you that mathematics requires the same, and so > will the best artists. I think mathematicians, engineers, artists, even > great chefs, will pour scorn on your claim that programming is not like > any other human activity. Well it's actually Dijkstra's claim, and I find it reasonable, that programming has two novel or unique aspects not present in other areas of human endeavor: vast scale and digital mechanism. http://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1036.html > [...] > > > He talks about how "when all is said and done, the only thing computers > > can do for us is to manipulate symbols and produce results of such > > manipulations" and he emphasises the "uninterpreted" nature of > > mechanical symbol manipulation, i.e. that the machine is doing it > > mindlessly. > > "Manipulate symbols" is so abstract as to be pointless. By that That's the "koan form", longhand he means the Boolean domain {true, false}, the logical operations and their algebraic relationships, and the means of using same to deduce new formulae. > reasoning, I can build a "computer" consisting of a box open at the top. > I represent a symbol by an object (say, a helium-filled balloon, or a > stone), instead of a pattern of bits. I manipulate the symbol by holding > the object over the box and letting go. If it flies up into the sky, that > represents the symbol "Love is War", if it falls into the box, it > represents the symbol "Strength is Blue", and if it just floats there, it > represents "Cheddar Cheese". This is a deterministic, analog computer > which manipulates symbols. Great. > > And utterly, utterly useless. So what is my computer lacking that real > computers have? When you have answered that question, you'll see why > Dijkstra's claim is under-specified. Well, you said it yourself: your computer is lacking utility. [...] > > What is an uninterpreted formula? If you don't interpret it, how can you > distinguish it from random noise? Uninterpreted formula means that you manipulate the symbols without thinking about what they mean. You just use the rules on the formulae as such. To be useful, once you're "done" with them you can consider their meaning. You can distinguish it at any time, but Dijktra's saying that we should pay attention to the [mathematical, logical] reasoning process as it's own thing, without reference to the meaning of the symbols, at least temporarily. [...] > > > If you're never exposed to that constellation of concepts that underpins > > "mechanical symbol manipulation" you are adrift in a sea ("c", ha ha) of > > abstractions. > > > However, if you /are/ exposed to the "so few and simple" rules of > > manipulation the gates (no pun intended) to the kingdom are thrown wide. > > What nonsense. The keys to the kingdom are the abstractions. > > Here's an exercise for you, if you dare. It's not that difficult to > remove the abstraction from integer addition, to explain it in terms of > bit manipulation. If you prefer, you can write a Turing Machine instead. > > Now try doing the same thing for Quicksort. Don't worry, we'll wait. > > Once you've done that, let's see you implement a practical, scalable, non- > toy webserver as a Turing Machine. > > No, it's not the fundamental operations that open the doors to the > kingdom. It's the abstractions. The history of computing is to gain more > and more power by leveraging better and better abstractions. I agree that "better and better abstractions" are the key(s), but I strongly disagree that the fundamental operations are not, uh, key to those keys. There are two intertwined phenomenon: 1) formulae, written in symbols 2) deriving new formulae by applying [in]formal methods to existing formulae The rules, and the application of those rules, are two separate but compatible phenomenon. (Compatible in that you can encode the latter in terms of the former. This is the essential "trick" of mechanized thought.) Now (1) is constantly being elaborated, these elaborations are the "higher" abstractions we're talking about. But (2) stays the same(-ish) no matter how high you go, and (2) is the process by which all the higher (1)'s come from, so if we get it right (and by right I mean of course by using rigorous proofs, formal methods et. al.) from the get go and keep it right, we're golden. I do literally intend to teach e.g. "integer addition ... in terms of bit manipulation" and a bit of parsing too. But I intend to rapidly bring students to a Forth or Forth-like stage, and then immediately create simple parsers that implement, for example, C for loops, and other "higher" abstractions. Highly abridged, this is the curriculum: Enough about bits to bring them to bytes, enough bytes to bring them to RAM, enough RAM to implement a crude CPU, enough CPU to implement a bit of Forth, enough Forth to implement parsers, and enough parsing (and compilation and interpreting, etc.) to let them grok languages in general. Formal methods gives me a wide straight road from each stage to the next. [...] > > Quoting Dijkstra again [1]: "Before we part, I would like to invite you > > to consider the following way of doing justice to computing's radical > > novelty in an introductory programming course. > > > "On the one hand, we teach what looks like the predicate calculus, but > > we do it very differently from the philosophers. In order to train the > > novice programmer in the manipulation of uninterpreted formulae, we > > teach it more as boolean algebra, familiarizing the student with all > > algebraic properties of the logical connectives. To further sever the > > links to intuition, we rename the values {true, false} of the boolean > > domain as {black, white}. > > This is supposed to be a good thing? > > Oh my. It must be nice in that ivory tower, never having to deal with > anyone who hasn't already been winnowed by years of study and self- > selection before taking on a comp sci course. > > I don't think Dijkstra would have taken that suggestion seriously if he > had to teach school kids instead of college adults. I'm not going to teach it exactly like he outlines there, but I really am going to try the curriculum I outlined. I'll post videos. :] > ... > > > (Did you read that last paragraph and think, "Well how the heck else are > > you supposed to program a computer if not in a computer language?"? If > > so, well, that is kind of my point.) > > No. What I thought was, how the hell are you supposed to manipulate > symbols without a language to describe what sort of manipulations you can > do? Direct-manipulation graphical Abstract Syntax Trees. [...] > > tables and arrays. He didn't even know that they were different > > "things". > > I shall manfully resist the urge to make sarcastic comments about > ignorant PHP developers, and instead refer you to my earlier reply to you > about cognitive biases. For extra points, can you identify all the > fallacious reasoning in that paragraph of yours? I'll pass. I should have resisted the urge to whinge about him in the first place. > > But those abstractions just get in the way, according to you. He should > be programming in the bare metal, doing pure symbol manipulation without > language. No, the abstractions are useful, but their utility is tied to their reliability, which in turn derives from the formal rigour of the proofs of the abstractions' correctness. My issues with modern art are, that too many of these abstractions are not /proven/, and they are both frozen and fractured by the existing milieu of "static" mutually incompatible programming languages. I see a possibility for something better. > If programming is symbol manipulation, then you should remember that the > user interface is also symbol manipulation, and it is a MUCH harder > problem than databases, sorting, searching, and all the other problems > you learn about in academia. The user interface has to communicate over a > rich but noisy channel using multiple under-specified protocols to a > couple of pounds of meat which processes information using buggy > heuristics evolved over millions of years to find the ripe fruit, avoid > being eaten, and have sex. If you think getting XML was hard, that's > *nothing* compared to user interfaces. > > The fact that even bad UIs work at all is a credit to the heuristics, > bugs and all, in the meat. Ok, yes, but have you read Jef Raskin's "the Humane Interface"? > > As for "a minimum of bugs"... sigh. The "minimum of bugs" is zero, if > > you derive your "uninterpreted formulae" /correctly/. Sorry. I can't believe that I was so melodramatic. > And the secret of getting rich on the stock market is to buy low, sell > high. Very true, and very pointless. How do you derive the formula > correctly? > > Do you have an algorithm to do so? If so, how do you know that algorithm > is correct? Well we know logic works, we can trust that. The algorithm of proving the formula is logic. > > Deriving provably > > correct "programs" should be what computer science and computer > > education are all about > > That's impossible. Not just impractical, or hard, or subject to hardware > failures, but impossible. > > I really shouldn't need to raise this to anyone with pretensions of being > a Computer Scientist and not just a programmer, but... try proving an > arbitrary program will *halt*. > > If you can't do that, then what makes you think you can prove all useful, > necessary programs are correct? It may well be that there are useful, necessary programs that are impossible to prove correct. I'm not trying to claim there aren't such. I think that wherever that's not the case, wherever useful necessary programs can be proven, they should be. It may be that flawless software is an unreachable asymptote, like the speed of light for matter, but I'm (recently!) convinced that it's a goal worthy of exploration and effort. Just because it's unattainable doesn't make it undesirable. And who knows? Maybe the horse will learn to sing. > > Again with Dijkstra[3]: "The prime paradigma of the pragmatic designer > > is known as "poor man's induction", i.e. he believes in his design as > > long as "it works", i.e. until faced with evidence to the contrary. (He > > will then "fix the design".) > > Yes, that is the only way it can be. > > > The scientific designer, however, believes > > in his design because he understands why it will work under all > > circumstances. > > Really? Under *all* circumstances? > > Memory failures? Stray cosmic radiation flipping bits? Noise in the power > supply? > > What's that you say? "That's cheating, the software designer can't be > expected to deal with the actual reality of computers!!! We work in an > abstract realm where computers never run out of memory, swapping never > occurs, and the floating point unit is always correct!" First, yes, that could be considered cheating. We don't expect automotive engineers to design for resistance to meteor strikes. Second, those issues plague all programs, formally derived or not. Formal methods certainly can't hurt. Third, in cases where you do want reliability in the face of things like cosmic rays you're going to want to use formal methods to achieve it. And last but not least, it may be possible to "parameterize" the abstractions we derive with information about their susceptibility to stability of their underlying abstractions. > Fail enough. I don't want to make it too hard for you. > > Okay, so let's compare the mere "pragmatic designer" with his provisional > expectation that his code is correct, and Dijkstra's "scientific > designer". He understands his code is correct. Wonderful! > > Er, *how exactly* does he reach that understanding? > > He reasons about the logic of the program. Great! I can do that. See, > here's a proof that binary search is correct. How easy this is. > > Now try it for some non-trivial piece of code. Say, the Linux kernel. > This may take a while... Well, I'd start with Minix 3... But really the method would be to build up provably correct compounds out of proven sub-assemblies. Even if it takes a long time is it time wasted? A provably correct OS is kind of a neat idea IMO. > > What guarantee do we have that the scientific designer's reasoning was > correct? I mean, sure he is convinced he has reasoned correctly, but he > would, wouldn't he? If he was unsure, he'd keep reasoning (or ask for > help), until he was sure. But being sure doesn't make him correct. It > just makes him sure. > > So how do you verify the verification? Peer review and run it through a mechanical verifier. There is a self- reflexive loop there but it's not infinite. If it was we couldn't implement any computer. > And speaking of binary search: [Bentley's binary search bug] This is a perfect example of leaky abstraction. The C "integer" doesn't behave like a "real" integer but part of the algorithm's implementation relied on the tacit overlap between C ints and integers up to a certain value. This is the sort of thing that could be annotated onto the symbolic representation of the algorithm and used to compile provably correct machine code (for various platforms.) If the same proven algorithm had been implemented on top of a correct integer abstraction (like python's long int) it wouldn't have had that bug. (And yes, there would still be limits, truly vast arrays could conceivably result in memory exhaustion during execution of the sort. I think the answer there lies in being able to represent or calculate the limits of an algorithm explicitly, the better to use it correctly in other code.) > Perhaps the "scientific designer" should be a little less arrogant, and > take note of the lesson of real science: all knowledge is provisional and > subject to correction and falsification. (I apologize for my arrogance on behalf of formal methods, I just found out about them and I'm all giddy and irritating.) > Which really makes the "scientific designer" just a slightly more clever > and effective "pragmatic designer", doesn't it? Um, er, well, scientists are ultimate pragmatists, and Dijkstra's description of "pragmatic designer" above could well be applied to "scientific designers". The difference turns on /why/ each person has confidence is his design: one can prove mathematically that his design is correct while the other is ... sure. Whether or not the delivered software is actually flawless, the difference seems to me more than just "slightly more clever and effective". > > > The transition from pragmatic to scientific design would > > indeed be a drastic change within the computer industry." > > Given Dijkstra's definition of "scientific design", it certainly would. > It would be as drastic a change as the discovery of Immovable Objects and > Irresistible Forces would be to engineering, or the invention of a > Universal Solvent for chemistry, or any other impossibility. > > > "Obviously no errors" is the goal to strive for, and I am comfortable > > calling anyone an amateur who prefers "no obvious errors." > > It's not a matter of preferring no obvious errors, as understanding the > limitations of reality. You simply can't do better than no obvious errors > (although of course you can do worse) -- the only question is what you > call "obvious". It may be expensive, it may be difficult, and it may certainly be impossible, but I feel the shift from ad hoc software creation to rigorous scientific software production is obvious. To me it's like the shift from astrology to astronomy, or from alchemy to chemistry. ~Simon From victorsubervi at gmail.com Sun Jul 19 11:35:58 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 19 Jul 2009 17:35:58 +0200 Subject: Getting a Form To Work Message-ID: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> Hi; I have the following in a *.py page for the Web: from primeNumbers import primeNumbers try: num = form.getfirst('num') except: num = '' msg = "Oops" print "Content-Type: text/html" print print """ """ if num != '': try: num = round(float(num)) roots = primeNumbers(num) print roots except: msg += "We're sorry! You entered something that is not a number. Please try again.
\n" num = '' if num == '': print msg print "Num:", num print """
""" print '\n' Now, "Oops" never gets printed, nor does the variable num (although a blank space appears to print). I am calling the very page to which the form sends its values. Assuming the imported module works, why don?t I get anything printed? TIA, Victor -------------- next part -------------- An HTML attachment was scrubbed... URL: From Nicolas.Dandrimont at crans.org Sun Jul 19 11:39:31 2009 From: Nicolas.Dandrimont at crans.org (Nicolas Dandrimont) Date: Sun, 19 Jul 2009 11:39:31 -0400 Subject: are user defined classes hashable? In-Reply-To: References: Message-ID: <20090719153931.GA14960@dirichlet.crans.org> * Alan G Isaac [2009-07-19 14:46:12 +0000]: > Again, my question is about the class not its instances, > but still, checking as you suggest gives the same answer. That's what I get for answering before my coffee! Cheers, -- Nicolas Dandrimont "Linux poses a real challenge for those with a taste for late-night hacking (and/or conversations with God)." (By Matt Welsh) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From sajmikins at gmail.com Sun Jul 19 11:45:20 2009 From: sajmikins at gmail.com (Simon Forman) Date: Sun, 19 Jul 2009 08:45:20 -0700 (PDT) Subject: Python code for testing well parenthesized expression References: <4a5c7c04$0$16808$426a34cc@news.free.fr> Message-ID: On Jul 14, 1:10?pm, Duncan Booth wrote: > John Machin wrote: > > Try an iterative version of checking that () [] and {} > > are balanced and nested appropriately. > > Here's how I might approach the more general case: > > def balanced(s, parens=("()",)): > ? ? ''' > ? ? Example: > ? ? >>> balanced('aAAA(b[bb(c]c))') > ? ? True > ? ? >>> balanced('aAAA(b[bb(c]c))', parens=("()", "[]")) > ? ? False > ? ? ''' > ? ? s = re.sub("[^%s]+" % re.escape("".join(parens)), "", s) > ? ? for i in range(len(s)/2): > ? ? ? ? for p in parens: > ? ? ? ? ? ? s = s.replace(p, "") > ? ? return not s > > For short strings this is obviously slower than your 'iterative' function, > but the run time mostly depends on the number of pairs of parentheses > rather than the total length of the string, so for example it starts > winning on a string with 2 pairs of parentheses about 75 characters long. def balanced(s, pairs=('()', '[]', '{}')): opening = {} closing = {} for open_, close in pairs: opening[open_] = closing[close] = object() stack = [] for char in s: marker = opening.get(char) if marker: stack.append(marker) continue marker = closing.get(char) if marker: try: if stack.pop() is not marker: return False except IndexError: return False # All markers should be popped. return not stack Can parse sequences other than strings. :] From sjmachin at lexicon.net Sun Jul 19 11:47:17 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 01:47:17 +1000 Subject: On out-of-date Python Applications In-Reply-To: <4A632C9D.2010303@it.uu.se> References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> Message-ID: <4A634005.6080505@lexicon.net> On 20/07/2009 12:24 AM, Virgil Stokes wrote: > John Machin wrote: >> On Jul 19, 6:04 pm, Virgil Stokes wrote: >> >>> I am not a heavy user of Python; but, I do work with it and some of its >>> application packages (e.g. PyODE), in an academic setting. >>> Many of these applications packages have a Windows installer which >>> usually works fine. However, I also try to keep up with the latest >>> release of Python, and this is where I often have problems. That is, the >>> latest Windows installer provided for some of these applications will >>> not install on the latest version of Python. >>> >> >> If the package was written for 2.x and "the latest version of Python" >> means 3.X, this is not surprising; your options are (1) wait until the >> package maintainer releases a 3.x-compatible version (2) port it to >> 3.x yourself (3) use 2.6 >> > Actually John, > My question was about the more general case --- how to help with > upgrading applications; but, I will try to address your email. >> Otherwise: What does "will not install" mean? > This means that when the Windows installer is executed it informs you > that you do not have a match between the application and the Python that > it finds on your Windows PC. You can easily find this out for yourself > by trying to install PyODE on a Windows platform with a Python version > later than 2.5.x. >> Are these pure Python >> packages or do they include C extensions (binary (pyd) or source?)? > Obviously, this depends on the application. In the particular case that > I mentioned (PyODE) I believe that there is a mix of C binaries and > Python code. >> At >> what stage of the installation does the installation fail? > In the initial stages. >> With what >> error message(s)? > In one specific case, the message is (paraphrasing) "You do not have > Python 2.5 installed" --- the installation is then aborted. >> With what versions of Python? >> > I have only Python 2.6.2 on my home Windows Vista PC and PyODE does not > have a Windows Installer for Python 2.6. >> >> >> >>> I do understand that there can be a time lag between the release of >>> Python applications and the latest Python. I also appreciate the work of >>> the people that are responsible for these applications. >>> >>> My question is --- Is there anything I, as a user of an application >>> package that is out-of-date with respect to the latest Python, can do to >>> help in this process of bringing an application up-to-date? >>> >>> --V. Stokes >>> >> >> > Note, again John, my question was about how I might assist (help) with > the updating of applications; Your answers have been very helpful in determining exactly what your problem is; thank you. > but, thank you for your interest in this. > Perhaps, only the owner/responsible programmer for the application can > create a Windows installer, Given a source distribution, a suitable C++ compiler, and the suspension of Murphy's Law, anyone should be able to (a) install it on their own machine (b) make a Windows installer. > or perhaps others could assist with this --- Perhaps indeed ... > this is what my question was about. I would be glad to assist if > possible :-) Suggestions: (1) contact the pyODE maintainer and ask, or check out the project's forums on sourceforge (2) if you have a C++ compiler on your Windows box: get the source distribution, unpack it, ensure that the ODE_BASE in the setup.py points to your (existing) ODE 0.7 installation, do \python26\python setup.py install and stand well back. I have just tried this and got a ton of warnings from the compile and 3 errors from the link: build\lib.win32-2.6\ode.pyd : fatal error LNK1120: 3 unresolved externals error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"' failed with exit status 1120 C++ is not my bag and it's way past bedtime here, so I'll just paste in the link output in the hope that someone can spot the problem: C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\ode\ode-0.7\lib\releaselib /LIBPATH:C:\python26\libs /LIBPATH:C:\python26\PCbuild ode.lib user32.lib /EXPORT:initode build\temp.win32-2.6\Release\ode_trimesh.obj /OUT:build\lib.win32-2.6\ode.pyd /IMPLIB:build\temp.win32-2.6\Release\ode.lib /MANIFESTFILE:build\temp.win32-2.6\Release\ode.pyd.manifest /NODEFAULTLIB:LIBCMT Creating library build\temp.win32-2.6\Release\ode.lib and object build\temp.win32-2.6\Release\ode.exp ode.lib(error.obj) : error LNK2019: unresolved external symbol __iob referenced in function "void __cdecl printMessage(int,char const *,char const *,char *)" (?printMessage@@YAXHPBD0PAD at Z) ode.lib(convex.obj) : error LNK2001: unresolved external symbol __iob ode.lib(convex.obj) : error LNK2019: unresolved external symbol "public: void __thiscall std::_String_base::_Xran(void)const " (?_Xran at _String_base@std@@QBEXXZ) referenced in function "public: class std::basic_string,class std::allocator > & __thiscall std::basic_string,class std::allocator >::erase(unsigned int,unsigned int)" (?erase@?$basic_string at DU?$char_traits at D@std@@V?$allocator at D@2@@std@@QAEAAV12 at II@Z) ode.lib(convex.obj) : error LNK2019: unresolved external symbol "public: void __thiscall std::_String_base::_Xlen(void)const " (?_Xlen at _String_base@std@@QBEXXZ) referenced in function "protected: bool __thiscall std::basic_string,class std::allocator >::_Grow(unsigned int,bool)" (?_Grow@?$basic_string at DU?$char_traits at D@std@@V?$allocator at D@2@@std@@IAE_NI_N at Z) build\lib.win32-2.6\ode.pyd : fatal error LNK1120: 3 unresolved externals Cheers, John From python at mrabarnett.plus.com Sun Jul 19 11:52:45 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 16:52:45 +0100 Subject: Getting a Form To Work In-Reply-To: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> References: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> Message-ID: <4A63414D.20408@mrabarnett.plus.com> Victor Subervi wrote: > Hi; > I have the following in a *.py page for the Web: > > from primeNumbers import primeNumbers > > try: > num = form.getfirst('num') > except: > num = '' > msg = "Oops" > > print "Content-Type: text/html" > print > print """ > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> > > > """ > if num != '': > try: > num = round(float(num)) > roots = primeNumbers(num) > print roots > except: > msg += "We're sorry! You entered something that is not a number. > Please try again.
\n" > num = '' > if num == '': > print msg > print "Num:", num You get here if num == '', so you're printing out an empty string! BTW, try not to use bare excepts. > print """ >
> > name="search" id="search" /> >
> """ > print '\n' > > Now, "Oops" never gets printed, nor does the variable num (although a > blank space appears to print). I am calling the very page to which the > form sends its values. Assuming the imported module works, why don?t I > get anything printed? > From hendrik at microcorp.co.za Sun Jul 19 12:07:54 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 19 Jul 2009 18:07:54 +0200 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <1248009501.29158.1325749595@webmail.messagingengine.com> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <200907191147.07949.mail@microcorp.co.za> <1248009501.29158.1325749595@webmail.messagingengine.com> Message-ID: <200907191807.55071.hendrik@microcorp.co.za> On Sunday 19 July 2009 15:18:21 python at bdurham.com wrote: > Hi Hendrik, > > If anybody is interested I will attach the code here. It is not a big > > module. > > I am interested in seeing your code and would be grateful if you shared > it with this list. All right here it is. Hope it helps - Hendrik -------------- next part -------------- A non-text attachment was scrubbed... Name: netstring.py Type: application/x-python Size: 3474 bytes Desc: not available URL: From python at bdurham.com Sun Jul 19 12:09:12 2009 From: python at bdurham.com (python at bdurham.com) Date: Sun, 19 Jul 2009 12:09:12 -0400 Subject: How to receive a data file of unknown length using a python socket? In-Reply-To: <200907191807.55071.hendrik@microcorp.co.za> References: <831d97bc-2323-42c3-9aeb-d62e4010c882@m11g2000yqh.googlegroups.com> <200907191147.07949.mail@microcorp.co.za> <1248009501.29158.1325749595@webmail.messagingengine.com> <200907191807.55071.hendrik@microcorp.co.za> Message-ID: <1248019752.28310.1325763043@webmail.messagingengine.com> >> I am interested in seeing your code and would be grateful if you shared it with this list. > All right here it is. Hope it helps. Hendrik, Thank you very much!! (I'm not the OP, but found this thread interesting) Best regards, Malcolm From piet at cs.uu.nl Sun Jul 19 13:08:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 19 Jul 2009 19:08:58 +0200 Subject: A little help with pexpect References: Message-ID: >>>>> Piet van Oostrum (PvO) wrote: [snip] >PvO> You can also consider using paramiko instead of pexpect. [snip] >>>>> chan = t.open_session() >>>>> chan.exec_command('cat') >>>>> chan.send('abcdefghijklmn\n') In a real program it is better to use sendall here, as send may decide to send only part of its argument (the result will tell how many bytes have been sent). -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From fft1976 at gmail.com Sun Jul 19 13:33:39 2009 From: fft1976 at gmail.com (fft1976) Date: Sun, 19 Jul 2009 10:33:39 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> Message-ID: <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> On Jul 19, 9:55?am, Frank Buss wrote: > E.g. the number system: In many Lisp > implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > it returns "0.6666666666", which will result in lots of fun for porting > applications written for Python <= 2.6. How do you explain that something as inferior as Python beat Lisp in the market place despite starting 40 years later. From python at mrabarnett.plus.com Sun Jul 19 14:06:35 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 19:06:35 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <4A6360AB.4060003@mrabarnett.plus.com> fft1976 wrote: > On Jul 19, 9:55 am, Frank Buss wrote: > >> E.g. the number system: In many Lisp >> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 >> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now >> it returns "0.6666666666", which will result in lots of fun for porting >> applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. It's all part of a Dutch conspiracy to take over the world! :-) From python at mrabarnett.plus.com Sun Jul 19 14:13:13 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 19 Jul 2009 19:13:13 +0100 Subject: Getting a Form To Work In-Reply-To: <4dc0cfea0907191034h2146a185o5691b0ab705f3c8d@mail.gmail.com> References: <4dc0cfea0907190835i1db3fa1es26b962c6b91610a3@mail.gmail.com> <4A63414D.20408@mrabarnett.plus.com> <4dc0cfea0907191034h2146a185o5691b0ab705f3c8d@mail.gmail.com> Message-ID: <4A636239.2010208@mrabarnett.plus.com> Victor Subervi wrote: > When the form comes up the first time, there is the default value for > num. When I fill in a number in the form and press send, even though the > form sends to itself (same page name), I would think it would read the > number sent. Here again is the code: > > from primeNumbers import primeNumbers > > try: > lang = form.getfirst('lang', 'en') > browser = form.getfirst('browser', 'all') > site = form.getfirst('site', 'bridge') > num = form.getfirst('num','') > except: > pass > > ourFile = string.split(__file__, "/") > p = ourFile[len(ourFile) - 1] > p = p[: - 9] > site = ourFile[4][:-10] > if site != '': > site = site[:-1] > > print "Content-Type: text/html" > print > print """ > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> > > > """ > if num != '': > num = round(float(num)) > roots = primeNumbers(num) > print roots > if num == '': > print """ >
> > name="search" id="search" /> >
> """ > print '\n' > > And why not use bare excepts? What is better? > [snip] An exception could be raised for a number of reasons, including misspelling a name. You should catch only those exceptions you expect so that others you don't expect, caused by bugs, will still be shown; you'll then know there's a bug and can fix it. From egarrulo at gmail.com Sun Jul 19 14:22:30 2009 From: egarrulo at gmail.com (Elena) Date: Sun, 19 Jul 2009 11:22:30 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <1542e10e-6ff0-43db-ab31-b73c0942ecf6@d32g2000yqh.googlegroups.com> On Jul 19, 7:33?pm, fft1976 wrote: > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. To be mainstream a language has to fit in most programmers' minds. From victorsubervi at gmail.com Sun Jul 19 14:28:10 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 19 Jul 2009 20:28:10 +0200 Subject: Trouble With a Form Message-ID: <4dc0cfea0907191128j4ed4e8det11d244769222b57e@mail.gmail.com> Hi: When the form comes up the first time, there is the default value for num. When I fill in a number in the form and press send, even though the form sends to itself (same page name), I would think it would read the number sent. Here again is the code: from primeNumbers import primeNumbers try: lang = form.getfirst('lang', 'en') browser = form.getfirst('browser', 'all') site = form.getfirst('site', 'bridge') num = form.getfirst('num','') except: pass ourFile = string.split(__file__, "/") p = ourFile[len(ourFile) - 1] p = p[: - 9] site = ourFile[4][:-10] if site != '': site = site[:-1] print "Content-Type: text/html" print print """ """ if num != '': num = round(float(num)) roots = primeNumbers(num) print roots if num == '': print """
""" print '\n' -------------- next part -------------- An HTML attachment was scrubbed... URL: From fb at frank-buss.de Sun Jul 19 14:31:36 2009 From: fb at frank-buss.de (Frank Buss) Date: Sun, 19 Jul 2009 20:31:36 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> fft1976 wrote: > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. Python is not that bad. Unlike Lisp, there is much less undefined behavior, there is one free unique implementation on the 3 major platforms Linux, Windows and MacOS X, which is stable, support multithreading and has a default GUI library binding, which is difficult to find for Lisp (e.g. I don't know of a free modern and stable Lisp implemenation with mulithreading support for Windows, with a licence with which you can use it in closed source commercial programs, like you can do with Python). Many problems in the Lispbuilder mailing list are related to problems due to different operating systems and Lisp implementations. But maybe the most important point: The syntax looks simple compared to Common Lisp (much less parentheses) and if you program in Python, it feels easier for programmer newbies. As Sussman says: "undergraduate?s initial experiences maximally productive". And this holds even for more experienced programmers. If you know already a bit of C, it is easy to use Python, but without the ability to do silly errors like writing out of array bounds (of course, you can do this in Lisp, too, if you remember to set the safe mode and if you use the right implementation). GC helps, too, to make the programming task easier than in C. Some more arguments, e.g. 5 times less program size than Java or C and more productive programmers: http://www.artima.com/intv/speedP.html (of course, an interview with Van Rossum might be a bit biased :-) -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From http Sun Jul 19 14:51:17 2009 From: http (Paul Rubin) Date: 19 Jul 2009 11:51:17 -0700 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> Message-ID: <7xljmk8q1m.fsf@ruckus.brouhaha.com> Calroc writes: > I'm engaged presently in starting a school to teach programming from > the ground up, based roughly on the curriculum outlined in the article > I mentioned. ... > I'm excited about formal methods because one, I just heard about them, Formal methods are a big and complicated subject. Right after you heard about them is probably not the best time to start teaching them. Better get to understand them yourself first. From roy at panix.com Sun Jul 19 15:09:28 2009 From: roy at panix.com (Roy Smith) Date: Sun, 19 Jul 2009 15:09:28 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, Frank Buss wrote: > there is one free unique implementation on the 3 major platforms Linux, > Windows and MacOS X Most people would still consider Solaris to be a "major platform". From rickbking at comcast.net Sun Jul 19 15:32:16 2009 From: rickbking at comcast.net (Rick King) Date: Sun, 19 Jul 2009 15:32:16 -0400 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: <50697b2c0907181606n2af8577ew3ad178f0463fa2d8@mail.gmail.com> References: <4A624D1C.5010503@comcast.net> <50697b2c0907181606n2af8577ew3ad178f0463fa2d8@mail.gmail.com> Message-ID: <4A6374C0.3000701@comcast.net> Thanks. I looked around for alternatives but didn't find this one. Rick Chris Rebert wrote: > On Sat, Jul 18, 2009 at 3:30 PM, Rick King wrote: > >> Hello, >> I want to copy files using subprocess.call or os.system where the file names >> are non-ascii, e.g. Serbian(latin), c's and s's with hacheks,etc. Windows >> stores all the file names in unicode so they are displayed ok in explorer, >> and I can read them into my program with listdir(u'.'), etc. and work with >> the names in the program. >> > > You should try one of the copying functions in the shutil module > instead, it'll be much simpler than using subprocess: > http://docs.python.org/library/shutil.html > > Cheers, > Chris > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Sun Jul 19 16:01:26 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 19 Jul 2009 16:01:26 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: Roy Smith wrote: > In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, > Frank Buss wrote: > >> there is one free unique implementation on the 3 major platforms Linux, >> Windows and MacOS X > > Most people would still consider Solaris to be a "major platform". ?? I do not, but I have no idea what comes in 4th after the other three by whatever metric. From stef.mientki at gmail.com Sun Jul 19 16:15:01 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Sun, 19 Jul 2009 22:15:01 +0200 Subject: any suggestions to synchronize typed text and speech ? Message-ID: <4A637EC5.4010701@gmail.com> hello, I'm using Scintilla as a wxPython widget with great pleasure. I now have an application where I want to make notes during a conversation, but also want to record the speech during that conversation. I'm using Scintilla as a wxPython widget for editing and PyAudio for the speech recording, until so far everything works fine. Here the problem part: I need to synchronize the typed text with the sound during playback. So if I click somewhere in the sound recording, the line of text, typed that moment should be highlighted. And vise versa, if the cursor in the text is moved and some special key is pressed, the sound starting 10 or 20seconds earlier should be playbacked. I though of adding bookmarks (because these are fixed in the text), and keep a list of bookmarks and sound pointers. This idea should work, but there are only 31 bookmarks. Any other suggestions ? thanks, Stef Mientki From emile at fenx.com Sun Jul 19 17:16:38 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 19 Jul 2009 14:16:38 -0700 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: On 7/19/2009 1:01 PM Terry Reedy said... > Roy Smith wrote: >> In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, >> Frank Buss wrote: >> >>> there is one free unique implementation on the 3 major platforms Linux, >>> Windows and MacOS X >> >> Most people would still consider Solaris to be a "major platform". > > ?? I do not, but I have no idea what comes in 4th after the other three > by whatever metric. > one metric calls fourth as the iPhone OS... http://marketshare.hitslink.com/report.aspx?qprid=8&qptimeframe=Y&qpsp=2009&qpmr=100&qpdt=1&qpct=0&qpob=UV%20DESC Emile From pavlovevidence at gmail.com Sun Jul 19 17:46:42 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sun, 19 Jul 2009 14:46:42 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: On Jul 19, 10:33?am, fft1976 wrote: > On Jul 19, 9:55?am, Frank Buss wrote: > > > E.g. the number system: In many Lisp > > implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > > "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > > it returns "0.6666666666", which will result in lots of fun for porting > > applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. There was no reason to crosspost this here--looking at the original thread on comp.lang.lisp it seems they were doing a surprisingly good job discussing the issue. I'm guessing it's because the fanboy Lispers like Ken Tifton were busy with a flamewar in another thread (LISP vs PROLOG vs HASKELL). Carl Banks From casevh at gmail.com Sun Jul 19 18:05:33 2009 From: casevh at gmail.com (casevh) Date: Sun, 19 Jul 2009 15:05:33 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> <4ae1e16f-124b-49c8-8364-36c59419be9b@d32g2000yqh.googlegroups.com> Message-ID: GMPY 1.10 beta is now available. This version fixes an issue where very large objects would be cached for reuse instead of being freed. Source code and Windows installers may be found at http://code.google.com/p/gmpy/downloads/list casevh From vippstar at gmail.com Sun Jul 19 18:54:17 2009 From: vippstar at gmail.com (vippstar) Date: Sun, 19 Jul 2009 15:54:17 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: On Jul 19, 9:31?pm, Frank Buss wrote: > fft1976 wrote: > > How do you explain that something as inferior as Python beat Lisp in > > the market place despite starting 40 years later. > But maybe the most important point: The syntax looks simple compared to > Common Lisp (much less parentheses) hahaha. From tundra at tundraware.com Sun Jul 19 19:29:07 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Sun, 19 Jul 2009 18:29:07 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: Carl Banks wrote: > On Jul 19, 10:33 am, fft1976 wrote: >> On Jul 19, 9:55 am, Frank Buss wrote: >> >>> E.g. the number system: In many Lisp >>> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 >>> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now >>> it returns "0.6666666666", which will result in lots of fun for porting >>> applications written for Python <= 2.6. >> How do you explain that something as inferior as Python beat Lisp in >> the market place despite starting 40 years later. > > There was no reason to crosspost this here--looking at the original > thread on comp.lang.lisp it seems they were doing a surprisingly good > job discussing the issue. > > I'm guessing it's because the fanboy Lispers like Ken Tifton were busy > with a flamewar in another thread (LISP vs PROLOG vs HASKELL). > > > Carl Banks This is an incredibly important discussion and is much weaker because it does not also include Pascal, BASIC, Ada, Oberon and Forth. In fact, picking a computer language is the most important discussion in Computer Science and eclipses even P=NP? in significance. I sure hope we can keep this thread going for a few months. For guidance, see: http://www.tundraware.com/Technology/How-To-Pick-A-Programming-Language/ -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From marcusw at cox.net Sun Jul 19 19:32:02 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sun, 19 Jul 2009 19:32:02 -0400 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: On 7/19/2009 4:15 PM, Stef Mientki wrote: > hello, > > I'm using Scintilla as a wxPython widget with great pleasure. > I now have an application where I want to make notes during a conversation, > but also want to record the speech during that conversation. > I'm using Scintilla as a wxPython widget for editing and PyAudio for the > speech recording, > until so far everything works fine. > > Here the problem part: > I need to synchronize the typed text with the sound during playback. > So if I click somewhere in the sound recording, > the line of text, typed that moment should be highlighted. > And vise versa, if the cursor in the text is moved and some special key > is pressed, > the sound starting 10 or 20seconds earlier should be playbacked. > > I though of adding bookmarks (because these are fixed in the text), and > keep a list of bookmarks and sound pointers. > This idea should work, but there are only 31 bookmarks. > > Any other suggestions ? > > thanks, > Stef Mientki That sounds like a very specialized type of thing, which only the few people with experience with wxPython, PyAudio, and Scintilla could help you with... But you might try having a dictionary with notes and associated times, or just give each note a four-digit ID number at the beginning of it when it's entered and use that in the dictionary (to keep keys shorter). Or you could just do a little hack and increase the number of bookmarks allowed (seeing as source is available) :p Marcus From http Sun Jul 19 19:35:01 2009 From: http (Paul Rubin) Date: 19 Jul 2009 16:35:01 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <7xiqho1c2i.fsf@ruckus.brouhaha.com> Emile van Sebille writes: > >> Most people would still consider Solaris to be a "major platform". > > ?? I do not, but I have no idea what comes in 4th after the other > > three by whatever metric. > one metric calls fourth as the iPhone OS... > http://marketshare.hitslink.com/report.aspx?qprid=8... That metric is clearly wrong. The #1 platform OS platform in terms of number of units shipped is Javacard, which is in the SIM cards of billions of GSM phones. From pavlovevidence at gmail.com Sun Jul 19 20:45:01 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sun, 19 Jul 2009 17:45:01 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> On Jul 19, 4:29?pm, Tim Daneliuk wrote: > Carl Banks wrote: > > On Jul 19, 10:33 am, fft1976 wrote: > >> On Jul 19, 9:55 am, Frank Buss wrote: > > >>> E.g. the number system: In many Lisp > >>> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > >>> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > >>> it returns "0.6666666666", which will result in lots of fun for porting > >>> applications written for Python <= 2.6. > >> How do you explain that something as inferior as Python beat Lisp in > >> the market place despite starting 40 years later. > > > There was no reason to crosspost this here--looking at the original > > thread on comp.lang.lisp it seems they were doing a surprisingly good > > job discussing the issue. > > > I'm guessing it's because the fanboy Lispers like Ken Tifton were busy > > with a flamewar in another thread (LISP vs PROLOG vs HASKELL). > > > Carl Banks > > This is an incredibly important discussion It might be an important question but a discussion on Usenet about it is utterly useless. > and is much weaker because > it does not also include Pascal, BASIC, Ada, Oberon and Forth. In the same way that a movie is weaker because the director edited out the bad scenes. > In fact, > picking a computer language is the most important discussion in > Computer Science and eclipses even P=NP? in significance. I sure hope > we can keep this thread going for a few months. Please feel free to extend this flame-war along for a few months on comp.lang.lisp. Not here. Carl Banks From marek at xivilization.net Sun Jul 19 20:51:49 2009 From: marek at xivilization.net (Marek Kubica) Date: Mon, 20 Jul 2009 02:51:49 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <20090720025149.326f0cfc@halmanfloyd.lan.local> On Sun, 19 Jul 2009 15:09:28 -0400 Roy Smith wrote: > In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, > Frank Buss wrote: > > > there is one free unique implementation on the 3 major platforms > > Linux, Windows and MacOS X > > Most people would still consider Solaris to be a "major platform". Depends on who you ask. On the desktop it doesn't matter at all (fortunately, since everytime I work on Solaris I'm entering a world of pain which is just slowly getting better with OpenSolaris), on the server it (and other propietary Unices) is losing ground compared to the free Unices. But ok, let's say 3 major platforms: Unix, Windows and Mac OS X. Mac OS X is formally a Unix but everything with GUI is non-Unix'y so it can be considered a separate platform. regards, Marek From marek at xivilization.net Sun Jul 19 20:54:13 2009 From: marek at xivilization.net (Marek Kubica) Date: Mon, 20 Jul 2009 02:54:13 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1542e10e-6ff0-43db-ab31-b73c0942ecf6@d32g2000yqh.googlegroups.com> Message-ID: <20090720025413.12eb7fbf@halmanfloyd.lan.local> On Sun, 19 Jul 2009 11:22:30 -0700 (PDT) Elena wrote: > On Jul 19, 7:33?pm, fft1976 wrote: > > How do you explain that something as inferior as Python beat Lisp in > > the market place despite starting 40 years later. > > To be mainstream a language has to fit in most programmers' minds. Sometimes I wonder whether Python is not too complex for "most programmers", since "most programmers" use Java, which is heavily mainstream. Interesting whether it is in spite of the ridiculous limitations or because of these. Probably a mix of both. regards, Marek From research at johnohagan.com Sun Jul 19 21:44:44 2009 From: research at johnohagan.com (John O'Hagan) Date: Mon, 20 Jul 2009 01:44:44 +0000 Subject: question on input function In-Reply-To: <346922.15108.qm@web56908.mail.re3.yahoo.com> References: <346922.15108.qm@web56908.mail.re3.yahoo.com> Message-ID: <200907200144.44417.research@johnohagan.com> On Mon, 20 Jul 2009, Richel Satumbaga wrote: > I am just learning python then I encountered an certain point in terms of > using the input function of python. the source code: > eq = input("enter an equation:"); > print " the result is : "; > > > the output seen in the command window: > > enter an equation:[m*x+b for m in (0,10,1),for x in (1,11,1), for b in > (2,12,1)] > > Traceback (most recent call last): > File "C:/Python26/try", line 1, in > eq = input("enter an equation:"); > File "", line 1 > [m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] > ^ > SyntaxError: invalid syntax [...] It's the extra commas in your list comprehension.This should work: [m*x+b for m in (0,10,1) for x in (1,11,1) for b in (2,12,1)] HTH, John -------------- next part -------------- An HTML attachment was scrubbed... URL: From rlsatumbaga at yahoo.com Sun Jul 19 22:07:07 2009 From: rlsatumbaga at yahoo.com (Richel Satumbaga) Date: Sun, 19 Jul 2009 19:07:07 -0700 (PDT) Subject: question on input function Message-ID: <346922.15108.qm@web56908.mail.re3.yahoo.com> I am just learning python then I encountered an certain point in terms of using the input function of python. the source code: ?????????????????????? eq = input("enter an equation:"); ?????????????????????? print " the result is : "; the output seen in the command window: >>> enter an equation:[m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] Traceback (most recent call last): ? File "C:/Python26/try", line 1, in ??? eq = input("enter an equation:"); ? File "", line 1 ??? [m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] ?????????????????????????????? ^ SyntaxError: invalid syntax ????????????????? Is there any other way to enter those? or it must be coded in the source code? If in source code is it like in matlab where one can type: x = [0:1:10]; to define the range of x which is form 0 to 10 with the increment of? 1. Thanks Ann -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Sun Jul 19 22:07:34 2009 From: roy at panix.com (Roy Smith) Date: Sun, 19 Jul 2009 22:07:34 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <20090720025149.326f0cfc@halmanfloyd.lan.local> Message-ID: In article <20090720025149.326f0cfc at halmanfloyd.lan.local>, Marek Kubica wrote: > > > there is one free unique implementation on the 3 major platforms > > > Linux, Windows and MacOS X > > > > Most people would still consider Solaris to be a "major platform". > > Depends on who you ask. On the desktop it doesn't matter at all > (fortunately, since everytime I work on Solaris I'm entering a world of > pain which is just slowly getting better with OpenSolaris), on the > server it (and other propietary Unices) is losing ground compared to > the free Unices. Clearly, there's not much (if any) Solaris on the desktop. But it's sure alive and kicking in the server world. One of the things I like about Python is not just how portable is it is, but how easy it is to build and deploy. We build our product on Windows, N different Linux distros, Solaris, HPUX, AIX, and OSX. I've built Python for all of those platforms. Then, I checked the install areas into Perforce. Deploying is as simple as checking out the right sandbox. We've gone through the same exercise for Perl. That was a lot more painful. From fatkinson at mishmash.com Sun Jul 19 22:22:37 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Sun, 19 Jul 2009 19:22:37 -0700 Subject: Final Project Message-ID: I'm looking for some ideas here. I think I've mentioned I am taking a course in Python and PHP. The professor wants each of us to pick a project to write in both languages. It has to be something fairly complex and I'd like for it to be something that would be useful on my Web site(s). I would be grateful for any and all suggestions. Regards, Fred From clp2 at rebertia.com Sun Jul 19 22:44:45 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 19 Jul 2009 19:44:45 -0700 Subject: question on input function In-Reply-To: <346922.15108.qm@web56908.mail.re3.yahoo.com> References: <346922.15108.qm@web56908.mail.re3.yahoo.com> Message-ID: <50697b2c0907191944i43d14c85m270bd391e366ec21@mail.gmail.com> On Sun, Jul 19, 2009 at 7:07 PM, Richel Satumbaga wrote: > I am just learning python then I encountered an certain point in terms of > using the input function of python. > the source code: > ?????????????????????? eq = input("enter an equation:"); > ?????????????????????? print " the result is : "; > > > the output seen in the command window: >>>> > enter an equation:[m*x+b for m in (0,10,1),for x in (1,11,1), for b in > (2,12,1)] > > Traceback (most recent call last): > ? File "C:/Python26/try", line 1, in > ??? eq = input("enter an equation:"); > ? File "", line 1 > ??? [m*x+b for m in (0,10,1),for x in (1,11,1), for b in (2,12,1)] > ?????????????????????????????? ^ > SyntaxError: invalid syntax > > ????????????????? Is there any other way to enter those? or it must be coded > in the source code? > If in source code is it like in matlab where one can type: > > x = [0:1:10]; > > to define the range of x which is form 0 to 10 with the increment of? 1. First, commas between the "for"s are not permitted, which gives us: [m*x+b for m in (0,10,1) for x in (1,11,1) for b in (2,12,1)] However, in Python, (i, j, k) is just a tuple (MATLAB: 1D matrix) of the items i, j, and k. What you want is a range, which is understandably created using the range() function. A step of 1 and a start of 0 are the defaults, and the stop value is always excluded, so the code becomes: [m*x+b for m in range(11) for x in range(1,12) for b in range(2,13)] Which should be what you want. For further explanation of the range() function: range(b) - the numbers from 0 to b-1 with a step of 1 range(a,b) - the numbers from a to b-1 with a step of 1 range(a,b,c) - the numbers from a to b-1 with a step of c Hope that helps. Cheers, Chris -- http://blog.rebertia.com From denize.paul at gmail.com Sun Jul 19 22:57:50 2009 From: denize.paul at gmail.com (pdenize) Date: Sun, 19 Jul 2009 19:57:50 -0700 (PDT) Subject: Managing non-ascii filenames in python Message-ID: <60a305de-0552-4f77-9051-d87149d308c7@y10g2000prg.googlegroups.com> I created the following filename in windows just as a test - ?D?n?ld?s? N?ph?ws? deg?.txt The quotes are non -ascii, many non english characters, long hyphen etc. Now in DOS I can do a directory and it translates them all to something close. "D?n?ld'sT N?ph?ws" deg?.txt I thought the correct way to do this in python would be to scan the dir files=os.listdir(os.path.dirname( os.path.realpath( __file__ ) )) then print the filenames for filename in files: print filename but as expected teh filename is not correct - so correct it using the file sysytems encoding print filename.decode(sys.getfilesystemencoding()) But I get UnicodeEncodeError: 'charmap' codec can't encode character u'\u2014' in position 6: character maps to All was working well till these characters came along I need to be able to write (a representation) to the screen (and I don't see why I should not get something as good as DOS shows). Write it to an XML file in UTF-8 and write it to a text file and be able to read it back in. Again I was supprised that this was also difficult - it appears that the file also wanted ascii. Should I have to open the file in binary for write (I expect so) but then what encoding should I write in? I have been beating myself up with this for weeks as I get it working then come across some outher character that causes it all to stop again. Please help. From mensanator at aol.com Mon Jul 20 00:12:35 2009 From: mensanator at aol.com (Mensanator) Date: Sun, 19 Jul 2009 21:12:35 -0700 (PDT) Subject: ANN: GMPY 1.10 alpha with support for Python 3 References: <4acb279c-2d90-4976-8e45-648624ecf542@18g2000yqa.googlegroups.com> <4ae1e16f-124b-49c8-8364-36c59419be9b@d32g2000yqh.googlegroups.com> Message-ID: <2c3e8fc0-e814-4020-bcfd-d3d02cc62dea@t13g2000yqt.googlegroups.com> On Jul 19, 5:05?pm, casevh wrote: > GMPY 1.10 beta is now available. This version fixes an issue where > very large objects would be cached for reuse instead of being freed. Excellent! That explains the funny memory usage graph. > > Source code and Windows installers may be found athttp://code.google.com/p/gmpy/downloads/list > Test results: import collatz_functions as cf # imports gmpy # the cf.Type12MH function does the equivalent # of the pure Python version of the function # using gmpy import time #3.1 gmpy 1.10 beta # t0 = time.time() n=10 for k in range(1,n): for i in range(1,n-2): print((str(cf.gmpy.numdigits(cf.Type12MH(k,i))).zfill(n)),end=' ') print() print() t1 = time.time() print(t1-t0) # 3.1 pure Python # ##t0 = time.time() ##n=10 ##for k in range(1,n): ## for i in range(1,n-2): ## print((str(cf.gmpy.numdigits( \ ## 2**(6*((i-1)*9**(k-1)+(9**(k-1)-1)//2+1)-1)-1 \ ## )).zfill(n)),end=' ') ## print() ##print() ##t1 = time.time() ## ##print(t1-t0) ## 3.1 gmpy 1.10 alpha ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##3.32299995422 ## funny memory usage display, different between 2.6 & 3.1, but funny regardless, ## memory remains allocated (~700 MB) when program halts ## 3.1 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##338.832000017 ## memory usage seems normal, runs slow enough to observe memory being allocated ## and freed and remains freed when program halts ## 2.6 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##aborted after ONE FULL WEEK of processing (only 8 of 9 rows completed) ## Something funny happened here. By the time I ran this test, there ## were 14 orphaned copies of pythonw.exe present, at least one of which ## was still running along with the real pair executing the program. Yet ## the Performance only registered ~50%. Normally, this would be 100% if ## two programs were running. Pure Python is a LOT slower than gmpy, ## but not THAT slow. These orphaned processes may be a Python 3.1 issue. ##=================================================================== ## 3.1 gmpy 1.10 beta ## ## 0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ## 0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ## 0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ## 0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ## 0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ## 0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ## 0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ## 0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ## 0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ## 2.41799998283 ## memory usage now appears normal, no more allocated memory when program halts ## and it's so fast all we see on Performance graph is a small blip, much better ## than the 700-1400 MB it was allocating before. ## 3.1 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##339.611999989 ## looks like gmpy about a 100 times faster. I knew there was a reason I ## won't use Python without gmpy. ## 2.6 gmpy 1.10 beta ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##2.68400001526 ## 2.6 pure Python ## ##0000000002 0000000004 0000000006 0000000007 0000000009 0000000011 0000000013 ##0000000009 0000000025 0000000042 0000000058 0000000074 0000000091 0000000107 ##0000000074 0000000221 0000000367 0000000513 0000000659 0000000806 0000000952 ##0000000659 0000001976 0000003293 0000004610 0000005926 0000007243 0000008560 ##0000005926 0000017777 0000029627 0000041477 0000053328 0000065178 0000077028 ##0000053328 0000159981 0000266634 0000373287 0000479940 0000586593 0000693246 ##0000479940 0001439818 0002399696 0003359574 0004319453 0005279331 0006239209 ##0004319453 0012958355 0021597258 0030236161 0038875064 0047513967 0056152869 ##0038875064 0116625189 0194375315 0272125440 0349875565 0427625691 0505375816 ## ##338.987999916 ## ah, that's more like it. no more pythonw.exe thrashing. I was almost ready ## to drop version 2.6 like a live grenade, but that was based on some other ## fault apparently. it may be 3.1 I have to drop. I still got a single instance of an orphaned pythonw.exe, but this happened during a quick attempt at provoking it during a 3.1 pure Python test, so I have no reason to suspect gmpy (previously, I thought it may be related to the memory leak). I've got an idea on how to reliably provoke this which I'll try to work up later (and won't even load gmpy to make sure it's not involved). > casevh From steven at REMOVE.THIS.cybersource.com.au Mon Jul 20 01:11:17 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 20 Jul 2009 05:11:17 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: On Sun, 19 Jul 2009 10:33:39 -0700, fft1976 wrote: > On Jul 19, 9:55?am, Frank Buss wrote: > >> E.g. the number system: In many Lisp >> implementations (/ 2 3) results in the fractional object 2/3. In Python >> 2.6 "2 / 3" results in "0". Looks like with Python 3.1 they have fixed >> it, now it returns "0.6666666666", which will result in lots of fun for >> porting applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in the > market place despite starting 40 years later. http://www.jwz.org/doc/worse-is-better.html Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing to do. It's the right thing to do if you're doing integer maths. -- Steven From fb at frank-buss.de Mon Jul 20 01:18:01 2009 From: fb at frank-buss.de (Frank Buss) Date: Mon, 20 Jul 2009 07:18:01 +0200 Subject: Final Project References: Message-ID: <1ctywmarsqqzd.y7fg69mkbshy$.dlg@40tude.net> Fred Atkinson wrote: > I'm looking for some ideas here. > > I think I've mentioned I am taking a course in Python and PHP. > The professor wants each of us to pick a project to write in both > languages. It has to be something fairly complex and I'd like for it > to be something that would be useful on my Web site(s). > > I would be grateful for any and all suggestions. Useful for your webpage depends very much on what you think what you need and it is difficult to answer by others who don't know your needs. E.g. useful for me was a Python Blender script to create wireframe and solid models of platonic solids and a program for geodesic domes of parametrized detail level: http://www.frank-buss.de/shapeways/dome.py http://www.shapeways.com/mydesign?user_id=19157 It is my first bigger Python program, so ideas how it could be improved are welcome. Would be an interesting project to create a renderer for it, which can calculate this and other geometric figures as a CGI script to an image and display the result in the browser. Try this in PHP and Python and write about the experiences you made. Maybe don't forget to limit the access to the webpage or the size of the output image, to avoid trouble with your hosting provider, if you don't have your own server and anything slows down because of your rendering task :-) -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From tundra at tundraware.com Mon Jul 20 01:18:58 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Mon, 20 Jul 2009 00:18:58 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> Message-ID: Carl Banks wrote: > On Jul 19, 4:29 pm, Tim Daneliuk wrote: >> Carl Banks wrote: >>> On Jul 19, 10:33 am, fft1976 wrote: >>>> On Jul 19, 9:55 am, Frank Buss wrote: >>>>> E.g. the number system: In many Lisp >>>>> implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 >>>>> "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now >>>>> it returns "0.6666666666", which will result in lots of fun for porting >>>>> applications written for Python <= 2.6. >>>> How do you explain that something as inferior as Python beat Lisp in >>>> the market place despite starting 40 years later. >>> There was no reason to crosspost this here--looking at the original >>> thread on comp.lang.lisp it seems they were doing a surprisingly good >>> job discussing the issue. >>> I'm guessing it's because the fanboy Lispers like Ken Tifton were busy >>> with a flamewar in another thread (LISP vs PROLOG vs HASKELL). >>> Carl Banks >> This is an incredibly important discussion > > It might be an important question but a discussion on Usenet about it > is utterly useless. > > >> and is much weaker because >> it does not also include Pascal, BASIC, Ada, Oberon and Forth. > > In the same way that a movie is weaker because the director edited out > the bad scenes. > > >> In fact, >> picking a computer language is the most important discussion in >> Computer Science and eclipses even P=NP? in significance. I sure hope >> we can keep this thread going for a few months. > > Please feel free to extend this flame-war along for a few months on > comp.lang.lisp. Not here. > > > Carl Banks Uh Carl ... are you familiar with the concept of mocking humor? -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From gagsl-py2 at yahoo.com.ar Mon Jul 20 01:54:57 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 02:54:57 -0300 Subject: file write IOError Invalid argument References: <505868.24184.qm@web59308.mail.re1.yahoo.com> Message-ID: En Thu, 16 Jul 2009 17:41:39 -0300, Robert Robert escribi?: > I am trying to write a binary string to file on a windows network share. > I get an IOError. I've read that it is because the file size is too > large. I did a type( binaryString) and saw that it was type "str". So I > loop through it one by one element and use f.write to write to file, and > it worked without any error. However it took a long while to write to > file. I was wondering how I could split this binary string into N number > of chunks efficiently and write them to file. some_string[i:i+BLOCK_SIZE] returns a slice of the original string, starting at position i, with length BLOCK_SIZE. -- Gabriel Genellina From bob.martin at excite.com Mon Jul 20 02:12:45 2009 From: bob.martin at excite.com (Bob Martin) Date: Mon, 20 Jul 2009 06:12:45 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: in 121683 20090719 210126 Terry Reedy wrote: >Roy Smith wrote: >> In article <1cethsrrw8h6k$.9ty7j7u7zovn.dlg at 40tude.net>, >> Frank Buss wrote: >> >>> there is one free unique implementation on the 3 major platforms Linux, >>> Windows and MacOS X >> >> Most people would still consider Solaris to be a "major platform". > >?? I do not, but I have no idea what comes in 4th after the other three >by whatever metric. I think the OP means "major PC operating systems". Those with a wider knowledge of the computer world would consider IBM's mainframe operating systems to be deserving of the description "major". From http Mon Jul 20 02:13:15 2009 From: http (Paul Rubin) Date: 19 Jul 2009 23:13:15 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <7x4ot7rif8.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing > to do. It's the right thing to do if you're doing integer maths. I wonder whether 2/3 => ValueError is preferable. From martin at v.loewis.de Mon Jul 20 02:27:20 2009 From: martin at v.loewis.de (=?windows-1252?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Mon, 20 Jul 2009 08:27:20 +0200 Subject: Managing non-ascii filenames in python In-Reply-To: <60a305de-0552-4f77-9051-d87149d308c7@y10g2000prg.googlegroups.com> References: <60a305de-0552-4f77-9051-d87149d308c7@y10g2000prg.googlegroups.com> Message-ID: <4a640e48$0$7445$9b622d9e@news.freenet.de> > I thought the correct way to do this in python would be to scan the > dir > files=os.listdir(os.path.dirname( os.path.realpath( __file__ ) )) > > then print the filenames > for filename in files: > print filename > > but as expected teh filename is not correct - so correct it using the > file sysytems encoding > > print filename.decode(sys.getfilesystemencoding()) > > But I get > UnicodeEncodeError: 'charmap' codec can't encode character u'\u2014' > in position 6: character maps to As a starting point, you shouldn't be using byte-oriented APIs to access files on Windows; the specific byte-oriented API is os.listdir, when passed a directory represented as a byte string. So try: dirname = os.path.dirname(os.path.realpath(__file__)) dirname = dirname.decode(sys.getfilesystemencoding() files = os.listdir(dirname) This should give you the files as Unicode strings. > I need to be able to write (a representation) to the screen (and I > don't see why I should not get something as good as DOS shows). The command window (it's not really DOS anymore) uses the CP_OEMCP encoding, which is not available in Python. This does all the transliteration also, so you would have to write an extension module if you want to get the same transliteration (or try to get to the OEMCP encoding through ctypes). If you can live with a simpler transliteration, try print filename.encode(sys.stdout.encoding, "replace") > Write it to an XML file in UTF-8 > > and write it to a text file and be able to read it back in. > Again I was supprised that this was also difficult - it appears that > the file also wanted ascii. Should I have to open the file in binary > for write (I expect so) but then what encoding should I write in? You need to tell us how precisely you tried to do this. My guess is: if you now try again, with the filenames being Unicode strings, it will work fairly well. Regards, Martin From fb at frank-buss.de Mon Jul 20 02:28:58 2009 From: fb at frank-buss.de (Frank Buss) Date: Mon, 20 Jul 2009 08:28:58 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <1pk5i4u29maii.vcsqz0mmsyy5$.dlg@40tude.net> Bob Martin wrote: > I think the OP means "major PC operating systems". Those with a wider > knowledge of the computer world would consider IBM's mainframe operating > systems to be deserving of the description "major". Maybe you are right, if you mean big machines. I know mainframes a bit and there are interesting concepts, like hot-swapping of CPU modules and mainframes are very reliable. But expensive, too. I know at least one client, who wants to change it to some cheap Linux boxes, like Google demonstrates it. If you take care (e.g. Xen virtualization for easier computer changing and RAID harddisks, if a downtime of some hours might be ok), it doesn't matter if one PC goes out of order. But even on IBM mainframes you can install Linux or other Unix systems in parallel to the usual operating systems for this machines, so except for special cases, like embedded systems, the most installed and used operating systems might be Unix-like systems and Windows. But looks like Python even runs on more native operating systems for mainframes. -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From bob.martin at excite.com Mon Jul 20 02:40:21 2009 From: bob.martin at excite.com (Bob Martin) Date: Mon, 20 Jul 2009 06:40:21 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <1pk5i4u29maii.vcsqz0mmsyy5$.dlg@40tude.net> Message-ID: in 121708 20090720 072858 Frank Buss wrote: >Bob Martin wrote: > >> I think the OP means "major PC operating systems". Those with a wider >> knowledge of the computer world would consider IBM's mainframe operating >> systems to be deserving of the description "major". > >Maybe you are right, if you mean big machines. I know mainframes a bit and >there are interesting concepts, like hot-swapping of CPU modules and >mainframes are very reliable. But expensive, too. I know at least one >client, who wants to change it to some cheap Linux boxes, like Google >demonstrates it. If you take care (e.g. Xen virtualization for easier >computer changing and RAID harddisks, if a downtime of some hours might be >ok), it doesn't matter if one PC goes out of order. > >But even on IBM mainframes you can install Linux or other Unix systems in >parallel to the usual operating systems for this machines, so except for >special cases, like embedded systems, the most installed and used operating >systems might be Unix-like systems and Windows. But looks like Python even >runs on more native operating systems for mainframes. Yes, a "platform" is really the combination of hardware architecture and operating system, so Linux on Intel and Linux on 390 are different platforms. From pavlovevidence at gmail.com Mon Jul 20 03:08:31 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 20 Jul 2009 00:08:31 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> Message-ID: <77e91c9e-5487-427d-9300-3ad9015adf51@2g2000prl.googlegroups.com> On Jul 19, 10:18?pm, Tim Daneliuk wrote: > Uh Carl ... are you familiar with the concept of mocking humor? You got me, lip hurts bad. :) Carl Banks From gagsl-py2 at yahoo.com.ar Mon Jul 20 03:29:17 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 04:29:17 -0300 Subject: invoke method on many instances References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac escribi?: >> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>> def apply2(itr, methodname, *args, **kwargs): >>> f = operator.methodcaller(methodname, *args, **kwargs) >>> for item in itr: >>> f(item) > > > On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >> for obj in objects: >> getattr(obj, methodname)(*args, **kwargs) > > > Are there any obvious considerations in choosing > between those two? The operator.methodcaller version is faster in my tests for large collections, but slightly slower when you have very few elements. import operator import timeit class X: def method(self, x, y, **kw): pass def apply1(itr, methodname, *args, **kwargs): for item in itr: getattr(item, methodname)(*args, **kwargs) def apply2(itr, methodname, *args, **kwargs): f = operator.methodcaller(methodname, *args, **kwargs) for item in itr: f(item) L=[X() for _ in range(3000)] apply1(L,'method', 1, 2, foo=3) apply2(L,'method', 1, 2, foo=3) timeit.timeit(setup="from __main__ import apply1,apply2,L", stmt="apply1(L,'method', 1, 2, foo=3)", number=1000) timeit.timeit(setup="from __main__ import apply1,apply2,L", stmt="apply2(L,'method', 1, 2, foo=3)", number=1000) -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 20 03:50:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 04:50:15 -0300 Subject: Mechanize not recognized by py2exe References: <4A60C81F.7080707@comcast.net> Message-ID: En Fri, 17 Jul 2009 15:51:11 -0300, Stephen M. Olds escribi?: > I have a Python script getData.py that uses Mechanize, and runs fine > under the > interpreter. It was installed using easy_install - and the install > seemed to > indicate it was completed. > > The problem is, when I try to compile it using py2exe while in the > folder of the > script, and using the run line command: > > python getData.py py2exe > > I get the warning: "Import error: No Module named mechanize"... > > I checked the environmental path and find the following: > %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program > Files (x86)\Python;C:\Python25\Lib\site-packages;C:\Program > Files (x86)\Common Files\Adobe\AGL The system PATH is irrelevant here - it's only used by Windows to locate the Python executable. I don't see a reason to list C:\Python25\Lib\site-packages here. You're mostly interested in sys.path instead (that is, the 'path' attribute in the 'sys' Python module) > I did a search for mechanize and find an egg file in > C:\Python25\Lib\site-packages > named mechanize-0.1.7b-py2.5. > > Not really understanding the "egg" thing, what do I have here that needs > to be > done? I'm not an egg expert either, and I try to avoid them as a plague. But I *think* you can delete the .egg and re-install it using easy_install with the -Z option. -- Gabriel Genellina From greg at cosc.canterbury.ac.nz Mon Jul 20 04:00:14 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Mon, 20 Jul 2009 20:00:14 +1200 Subject: tough-to-explain Python In-Reply-To: <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> Message-ID: <7cimf0F27u23eU1@mid.individual.net> Calroc wrote: > It may be that flawless software is an unreachable asymptote, like the > speed of light for matter, but I'm (recently!) convinced that it's a > goal worthy of exploration and effort. Seems to me that once you get beyond the toy program stage and try to apply correctness proving to real world programming problems, you run up against the problem of what exactly you mean by "correct". Once the requirements get beyond a certain level of complexity, deciding whether the specifications themselves are correct becomes just as difficult as deciding whether the program meets them. Then there's the fact that very often we don't even know what the exact requirements are, and it's only by trying to write and use the program that we discover what they really are -- or at least, get a better idea of what they are, because the process is usually iterative, with no clear end point. So in theory, correctness proofs are a fine idea, and might even be doble on a small scale, but the real world is huge and messy! > Just because it's unattainable doesn't make it undesirable. And who > knows? Maybe the horse will learn to sing. Striving to find ways of writing less bugs is a worthy goal, but I think of it more in terms of adopting patterns of thought and programming that make it more likely you will write code that does what you had in mind, rather than a separate "proof" process that you go through afterwards. -- Greg From stef.mientki at gmail.com Mon Jul 20 05:34:09 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Mon, 20 Jul 2009 11:34:09 +0200 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: <4A643A11.6030109@gmail.com> thanks Marcus, Marcus Wanner wrote: > On 7/19/2009 4:15 PM, Stef Mientki wrote: >> hello, >> >> I'm using Scintilla as a wxPython widget with great pleasure. >> I now have an application where I want to make notes during a >> conversation, >> but also want to record the speech during that conversation. >> I'm using Scintilla as a wxPython widget for editing and PyAudio for >> the speech recording, >> until so far everything works fine. >> >> Here the problem part: >> I need to synchronize the typed text with the sound during playback. >> So if I click somewhere in the sound recording, >> the line of text, typed that moment should be highlighted. >> And vise versa, if the cursor in the text is moved and some special >> key is pressed, >> the sound starting 10 or 20seconds earlier should be playbacked. >> >> I though of adding bookmarks (because these are fixed in the text), >> and keep a list of bookmarks and sound pointers. >> This idea should work, but there are only 31 bookmarks. >> >> Any other suggestions ? >> >> thanks, >> Stef Mientki > That sounds like a very specialized type of thing, Well from an application point of view, with the current netbooks, this looks like a perfect tool for any conversation or meeting. > which only the few people with experience with wxPython, PyAudio, and > Scintilla could help you with... > I was afraid of that too, so I dropped the question in several places, and the writer of Scintilla himself came with the perfect answer. cheers,Stef > But you might try having a dictionary with notes and associated times, > or just give each note a four-digit ID number at the beginning of it > when it's entered and use that in the dictionary (to keep keys > shorter). Or you could just do a little hack and increase the number > of bookmarks allowed (seeing as source is available) :p > > Marcus From sion at viridian.paintbox Mon Jul 20 05:34:24 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Mon, 20 Jul 2009 09:34:24 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: Terry Reedy wrote: >Sion Arrowsmith wrote: >> Jack Diederich wrote: >>> It isn't an OrderedDict thing, it is a comparison thing. Two regular >>> dicts also raise an error if you try to LT them. >> Python 2.5.2 >>>>> d1 = dict((str(i), i) for i in range (10)) >>>>> d2 = dict((str(i), i) for i in range (20)) >>>>> d1 < d2 >> True >Try reversing the definitions of d1 and d2. The dicts are probably being >compared by id (address), which is the 2.x CPython default. Like this? >>> d1 = dict((str(i), i) for i in range (20)) >>> d2 = dict((str(i), i) for i in range (10)) >>> d1 < d2 False >>> id(d1) < id(d2) True I didn't know that comparison for anything other than equality defaulted to using id. That really is rather broken, and I'm glad 3.0 fixed it. -- \S under construction From walkraft at gmail.com Mon Jul 20 05:57:36 2009 From: walkraft at gmail.com (casebash) Date: Mon, 20 Jul 2009 02:57:36 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? Message-ID: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Hi, I have searched this list and found out that Python doesn't have a mutable string class (it had an inefficient one, but this was removed in 3.0). Are there any libraries outside the core that offer this? Thanks, Chris From vivainio at gmail.com Mon Jul 20 06:12:25 2009 From: vivainio at gmail.com (Ville Vainio) Date: Mon, 20 Jul 2009 03:12:25 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops Message-ID: Has anyone implementing something like what the subject line indicates? The idea: To run functions that execute a series of system commands without blocking the ui, *and* without adding state machine logic. The syntax would be something like: def work(): showstatus("building") r = yield runshell("make") showstatus("installing") r = yield runshell("make install") showstatus("Success") mygui.startwork(work) # returns immediately, runs work() gradually in the background. The catch is that showstatus() would need to be run in the mainloop, so running the whole thing in a thread is a no-go. I imagine runshell() would be implemented in terms of QProcess, or subprocess.Popen/os.system and a worker thread. Anyone done this already, or do I have to roll my own? From vivainio at gmail.com Mon Jul 20 06:14:35 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Mon, 20 Jul 2009 03:14:35 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: <9600897a-100c-4907-826d-0d700cef6880@r2g2000yqm.googlegroups.com> On Jul 20, 1:12?pm, Ville Vainio wrote: > Has anyone implementing something like what the subject line ImplentED. I don't think this is that hard to do in the first place, but a "generic" solution that can be easily tuned for different gui mainloops would be nice. From pavlovevidence at gmail.com Mon Jul 20 06:26:09 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 20 Jul 2009 03:26:09 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: On Jul 20, 2:57?am, casebash wrote: > Hi, > > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? I just did a brief search on Python Packaging Index (pypi.python.org) and saw nothing. You might have better luck. I suspect there is not a mutable string class partly because there is little demand, partly because it'd be hard to make an object that is usable everywhere a string is. For instance, a third-party string object might not work with the re module. The core does have some classes that are kind of like strings but mutable. The array module can create mutable arrays of characters which are somewhat like strings. Depending on your use case some other things might suffice (such as mmap, io.StringIO, or even a list of characters). Carl Banks From vivainio at gmail.com Mon Jul 20 06:28:33 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Mon, 20 Jul 2009 03:28:33 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: On Jul 20, 1:12?pm, Ville Vainio wrote: > I imagine runshell() would be implemented in terms of QProcess, or > subprocess.Popen/os.system and a worker thread. Actually, the problem is that of general serialization of worker thread operations. That is, it could be something akin to: res = yield run_in_thread(lambda : os.system('make')) run_in_thread would basically grab a worked thread, execute the callable, and allow the generator to proceed after the thread is done. Now, the only thing to do is the "generator dispatcher" in gui mainloop that finds the correct generator and makes it proceed... From ben+python at benfinney.id.au Mon Jul 20 07:08:22 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 20 Jul 2009 21:08:22 +1000 Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: <87r5wba9y1.fsf@benfinney.id.au> casebash writes: > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? A mutable string would not (AFAICT) be usefully implementable as a subclass of the built-in string types. So even if such a type existed, it would not be useable with all the functionality that works with strings. What is it you're trying to do that makes you search for a mutable string type? It's likely that a better approach can be found. -- \ ?As scarce as truth is, the supply has always been in excess of | `\ the demand.? ?Josh Billings | _o__) | Ben Finney From sjmachin at lexicon.net Mon Jul 20 07:24:06 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 04:24:06 -0700 (PDT) Subject: On out-of-date Python Applications References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> Message-ID: On Jul 20, 1:47?am, John Machin wrote: > On 20/07/2009 12:24 AM, Virgil Stokes wrote: > > John Machin wrote: > >> On Jul 19, 6:04 pm, Virgil Stokes wrote: > >>> I am not a heavy user of Python; but, I do work with it and some of its > >>> application packages (e.g. PyODE), in an academic setting. > >>> Many of these applications packages have a Windows installer which > >>> usually works fine. However, I also try to keep up with the latest > >>> release of Python, and this is where I often have problems. That is, the > >>> latest Windows installer provided for some of these applications will > >>> not install on the latest version of Python. > > Actually John, > > My question was about the more general case --- how to help with > > upgrading applications; but, I will try to address your email. > >> Otherwise: What does "will not install" mean? > > This means that when the Windows installer is executed it informs you > > that you do not have a match between the application and the Python that > > it finds on your Windows PC. You can easily find this out for yourself > > by trying to install PyODE on a Windows platform with a Python version > > later than 2.5.x. > >> Are these pure Python > >> packages or do they include C extensions (binary (pyd) or source?)? > > Obviously, this depends on the application. In the particular case that > > I mentioned (PyODE) I believe that there is a mix of C binaries and > > Python code. > >> At > >> what stage of the installation does the installation fail? > > In the initial stages. > >> With what > >> error message(s)? > > In one specific case, the message is (paraphrasing) "You do not have > > Python 2.5 installed" --- the installation is then aborted. > >> With what versions of Python? > > > I have only Python 2.6.2 on my home Windows Vista PC and PyODE does not > > have a Windows Installer for Python 2.6. > > >>> I do understand that there can be a time lag between the release of > >>> Python applications and the latest Python. I also appreciate the work of > >>> the people that are responsible for these applications. > > >>> My question is --- Is there anything I, as a user of an application > >>> package that is out-of-date with respect to the latest Python, can do to > >>> help in this process of bringing an application ?up-to-date? > > Note, again John, my question was about how I might assist (help) with > > the updating of applications; > > Your answers have been very helpful in determining exactly what your > problem is; thank you. > > > but, thank you for your interest in this. > > Perhaps, only the owner/responsible programmer for the application can > > create a Windows installer, > > Given a source distribution, a suitable C++ compiler, and the suspension > of Murphy's Law, anyone should be able to (a) install it on their own > machine (b) make a Windows installer. > > > or perhaps others could assist with this --- > > Perhaps indeed ... > > > this is what my question was about. I would be glad to assist if > > possible :-) > > Suggestions: > > (1) contact the pyODE maintainer and ask, or check out the project's > forums on sourceforge > > (2) if you have a C++ compiler on your Windows box: get the source > distribution, unpack it, ensure that the ODE_BASE in the setup.py points > to your (existing) ODE 0.7 installation, do > ? ? ?\python26\python setup.py install > and stand well back. > > I have just tried this and got a ton of warnings from the compile and 3 > errors from the link: > > build\lib.win32-2.6\ode.pyd : fatal error LNK1120: 3 unresolved externals > error: command '"C:\Program Files\Microsoft Visual Studio > 9.0\VC\BIN\link.exe"' failed with exit status 1120 Update: The problem is evidently with the lib(s) available with ODE 0.7 (or 0.8 -- there are conflicting stories about which one to use with PyODE); they were compiled with VS2003 AFAICT and the complained- of symbols are not supplied by VS2009 which is what I'm using to try and build PyODE for Python 2.6 . The next step would be to try to compile ODE 0.7 or 0.8 with VS9 -- however this would require "project files" for ODE for VS9, and there aren't any on the ODE website; it has only those for VS3 and VS5. As all I know about VS9 is that somewhere inside the ferschlugginer 100 Mb download there's a C compiler that does the right thing when distutils pushes its button, I'm going to have to punt this back to you to follow up on suggestion (1) ... skip asking the pyODE maintainer (last heard saying little time available and he wasn't even /using/ pyODE any more); try asking on the pyODE and ODE mailing- lists has anyone contemplated a Python 2.6 / win32 / VS 2009 build of pyODE. Cheers, John From sjmachin at lexicon.net Mon Jul 20 07:30:12 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 04:30:12 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> Message-ID: <47ab4ab6-d8c8-4997-8963-ac41b00dd10f@y10g2000prg.googlegroups.com> On Jul 20, 9:08?pm, Ben Finney wrote: > casebash writes: > > I have searched this list and found out that Python doesn't have a > > mutable string class (it had an inefficient one, but this was removed > > in 3.0). Are there any libraries outside the core that offer this? > > A mutable string would not (AFAICT) be usefully implementable as a > subclass of the built-in string types. So even if such a type existed, > it would not be useable with all the functionality that works with > strings. > > What is it you're trying to do that makes you search for a mutable > string type? It's likely that a better approach can be found. > OK, I'll bite: where does the Python 3.x bytearray type fit into your taxonomy? At first glance it appears to be mutable and have a fair swag of functionality. From drobinow at gmail.com Mon Jul 20 07:46:12 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 20 Jul 2009 07:46:12 -0400 Subject: On out-of-date Python Applications In-Reply-To: References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> Message-ID: <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> On Mon, Jul 20, 2009 at 7:24 AM, John Machin wrote: ... > The next step would be to try to compile ODE 0.7 or 0.8 with VS9 -- > however this would require "project files" for ODE for VS9, and there > aren't any on the ODE website; it has only those for VS3 and VS5. > The ODE site is a mess. Go to http://www.ode.org/svn.html and click on: Instructions for accessing the repository Scroll down to the section "Building with Premake" (Note that there is no directory "ode/build" -- you want the "build" directory, which contains premake4.exe) I used "premake4 --with-demos --with-tests vs2008" I have successfully compiled ode-0.11.1 using these instructions. I have not yet run the tests or demos or tried to compile PyODE. From wilk at flibuste.net Mon Jul 20 07:56:49 2009 From: wilk at flibuste.net (William Dode) Date: 20 Jul 2009 11:56:49 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: <4a645b81$0$12974$426a74cc@news.free.fr> On 19-07-2009, Mark Dufour wrote: > Hi all, > > I have just released version 0.2 of Shed Skin, an experimental > (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). I just tested it with a litle game, to find the places of horse on a board 5x5. The result is : c 5s gcj 7s java 7s shedskin 8s python + psyco 18s cython avec malloc *int 18s cython 55s avec [] python python 303s (5m3s) -- William Dod? - http://flibuste.net Informaticien Ind?pendant From Lacrima.Maxim at gmail.com Mon Jul 20 08:20:26 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Mon, 20 Jul 2009 05:20:26 -0700 (PDT) Subject: Design question. Message-ID: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Hello! I am newbie in python and I have really simple question, but I need your advice to know how to do best. I need to store a number of dictionaries in certain place. I've decided to store them in a separate module. Like this: dicts.py ----------------------- dict1 = {....} dict2 = {....} dict3 = {....} Then, when I need any dictionary, I can access it: import dicts dicts.dict1 Is it a good practice? Or should I store them as class attributes or anything else? Thanks in advance. With regards, Max (sorry if my English isn't very proper) From magobin at gmail.com Mon Jul 20 08:28:27 2009 From: magobin at gmail.com (Alex) Date: Mon, 20 Jul 2009 05:28:27 -0700 (PDT) Subject: A lot of problem with pygame.mixer module! Message-ID: hi at all, As email object I 'm not be able to run my program when compiled with py2exe. Everythink works fine until I try to make an exe. After that, it crash and in the log: C:\dist\sponge.exe:97: RuntimeWarning: use mixer: DLL load failed: The specified module could not be found. Traceback (most recent call last): File "sponge.py", line 97, in File "pygame\__init__.pyo", line 70, in __getattr__ NotImplementedError: mixer module not available The code about pygame.mixer is only: pygame.mixer.init() music = pygame.mixer.Sound("popcorn.ogg") music.play() in the dist folder there is SDL_mixer.dll so...HOW can I solve this problem ?? thanks in advance Alex Below my setup.py # py2exe setup program from distutils.core import setup import py2exe import pygame from modulefinder import Module #from pygame import mixer import ImageGrab import sys import os import Tkinter import glob, shutil sys.argv.append("py2exe") VERSION = '1.0' AUTHOR_NAME = 'Your Name' AUTHOR_EMAIL = 'your_email at somewhere.com' AUTHOR_URL = "http://www.urlofyourgamesite.com/" PRODUCT_NAME = "Sponge" SCRIPT_MAIN = 'sponge.py' VERSIONSTRING = PRODUCT_NAME + " ALPHA " + VERSION ICONFILE = 'icon.ico' # Remove the build tree on exit automatically REMOVE_BUILD_ON_EXIT = True if os.path.exists('dist/'): shutil.rmtree('dist/') extra_files = [ ("",[ICONFILE,'WinLockDll.dll','popcorn.ogg']), #("data",glob.glob(os.path.join('data','*.dat'))), #("gfx",glob.glob(os.path.join('gfx','*.jpg'))), #("gfx",glob.glob(os.path.join('gfx','*.png'))), ("fonts",glob.glob(os.path.join('fonts','*.ttf'))), ("music",glob.glob(os.path.join('music','*.ogg')))] #("snd",glob.glob(os.path.join('snd','*.wav')))] # List of all modules to automatically exclude from distribution build # This gets rid of extra modules that aren't necessary for proper functioning of app # You should only put things in this list if you know exactly what you DON'T need # This has the benefit of drastically reducing the size of your dist MODULE_EXCLUDES =[ 'email', 'AppKit', 'Foundation', 'bdb', 'difflib', 'tcl', #'Tkinter', #'Tkconstants', 'curses', 'distutils', 'setuptools', 'urllib', 'urllib2', 'urlparse', 'BaseHTTPServer', '_LWPCookieJar', '_MozillaCookieJar', 'ftplib', 'gopherlib', '_ssl', 'htmllib', 'httplib', 'mimetools', 'mimetypes', 'rfc822', 'tty', 'webbrowser', 'socket', 'hashlib', #'base64', 'compiler', 'pydoc' ] INCLUDE_STUFF = ['encodings',"encodings.latin_1"] setup(windows=[ {'script': SCRIPT_MAIN, 'other_resources': [(u"VERSIONTAG",1,VERSIONSTRING)], 'icon_resources': [(1,ICONFILE)]}], options = {"py2exe": { "optimize": 2, "includes": INCLUDE_STUFF, "compressed": 1, "ascii": 1, #"bundle_files": 1, "ignores": ['tcl','AppKit','Numeric','Foundation'], "excludes": MODULE_EXCLUDES} }, name = PRODUCT_NAME, version = VERSION, data_files = extra_files, #zipfile = None, author = AUTHOR_NAME, author_email = AUTHOR_EMAIL, url = AUTHOR_URL) # Create the /save folder for inclusion with the installer #shutil.copytree('save','dist/save') #if os.path.exists('dist/tcl'): shutil.rmtree('dist/tcl') # Remove the build tree if REMOVE_BUILD_ON_EXIT: shutil.rmtree('build/') if os.path.exists('dist/tcl84.dll'): os.unlink('dist/tcl84.dll') if os.path.exists('dist/tk84.dll'): os.unlink('dist/tk84.dll') From vippstar at gmail.com Mon Jul 20 08:29:40 2009 From: vippstar at gmail.com (vippstar) Date: Mon, 20 Jul 2009 05:29:40 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: On Jul 20, 9:13?am, Paul Rubin wrote: > Steven D'Aprano writes: > > Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing > > to do. It's the right thing to do if you're doing integer maths. > > I wonder whether 2/3 => ValueError is preferable. Not all software wants this. It shouldn't be part of the language but rather part of your code if you need such a feature. (for instance, to distinguish between 2/3 and divisions with 0 dividend). From deets at nospam.web.de Mon Jul 20 08:31:15 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 20 Jul 2009 14:31:15 +0200 Subject: Design question. References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Message-ID: <7cj63mF285spnU1@mid.uni-berlin.de> Lacrima wrote: > Hello! > > I am newbie in python and I have really simple question, but I need > your advice to know how to do best. > I need to store a number of dictionaries in certain place. I've > decided to store them in a separate module. > Like this: > dicts.py > ----------------------- > dict1 = {....} > dict2 = {....} > dict3 = {....} > > Then, when I need any dictionary, I can access it: > import dicts > dicts.dict1 > > Is it a good practice? Or should I store them as class attributes or > anything else? It's perfectly fine to use dictionaries as module-level objects for storing e.g. configuration-data. OTOH it's also fine to do so as class-attributes. Choosing one over the other has a lot to do with the actual problem you are working on. Diez From ndbecker2 at gmail.com Mon Jul 20 08:39:28 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 20 Jul 2009 08:39:28 -0400 Subject: proposal: add setresuid() system call to python References: <20090717200141.GI6065@subspacefield.org> <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> Message-ID: Mahmoud Abdelkader wrote: > Why don't you write a python extension module? This is a perfect > opportunity for that. > I think having a module just for one system call is a bit silly. Why not add to os module? From miheavner at gmail.com Mon Jul 20 08:41:02 2009 From: miheavner at gmail.com (mheavner) Date: Mon, 20 Jul 2009 05:41:02 -0700 (PDT) Subject: Persistent variable in subprocess using multiprocessing? References: <93ef749c-fd54-4ab9-9a67-fda5068051d1@k19g2000yqn.googlegroups.com> Message-ID: Piet, The situation is 1a of your listed options, however my issue was solved. I was stopping the subprocesses from consuming more data at each iteration which led to the data being lost since the subprocess worker function would then end - I now keep them alive across iterations. Thanks for your help, I'm new to the multiprocessing module and this was very helpful! On Jul 17, 4:26?am, Piet van Oostrum wrote: > There is stil something not clear in your description. > > >m> I'm using multiprocessing to spawn several subprocesses, each of which > >m> uses a very large data structure (making it impractical to pass it via > >m> pipes / pickling). I need to allocate this structure once when the > >m> process is created and have it remain in memory for the duration of > >m> the process. > > I have read this as that every subprocess has its own large > data structure and that there is no connection between these. > > But seeing where the discussion is going I guess there might be > different interpretations. So can you enlighten us how the situation is? > > 1. Each subprocess has a copy of a data structure that is prepared by the > ? ?master process. Therefore you want it to be passed by the fork > ? ?1a. the data structure is constant i.e. the subprocess doesn't change it > ? ?1b. the subprocess makes changes in its copy > 2. Each subprocess has a seperate data structure not equal to the others > 3. Something else. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org From Lacrima.Maxim at gmail.com Mon Jul 20 08:44:22 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Mon, 20 Jul 2009 05:44:22 -0700 (PDT) Subject: Design question. References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> <7cj63mF285spnU1@mid.uni-berlin.de> Message-ID: <825d0aae-3147-4f81-8c82-fa86e4b777df@c29g2000yqd.googlegroups.com> On Jul 20, 3:31?pm, "Diez B. Roggisch" wrote: > Lacrima wrote: > > Hello! > > > I am newbie in python and I have really simple question, but I need > > your advice to know how to do best. > > I need to store a number of dictionaries in certain place. I've > > decided to store them in a separate module. > > Like this: > > dicts.py > > ----------------------- > > dict1 = {....} > > dict2 = {....} > > dict3 = {....} > > > Then, when I need any dictionary, I can access it: > > import dicts > > dicts.dict1 > > > Is it a good practice? Or should I store them as class attributes or > > anything else? > > It's perfectly fine to use dictionaries as module-level objects for storing > e.g. configuration-data. > > OTOH it's also fine to do so as class-attributes. Choosing one over the > other has a lot to do with the actual problem you are working on. > > Diez Hi! Thank you very much for your so soon reply! With regards, Max From jeanmichel at sequans.com Mon Jul 20 09:05:13 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 20 Jul 2009 15:05:13 +0200 Subject: Design question. In-Reply-To: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Message-ID: <4A646B89.7060103@sequans.com> Lacrima wrote: > Hello! > > I am newbie in python and I have really simple question, but I need > your advice to know how to do best. > I need to store a number of dictionaries in certain place. I've > decided to store them in a separate module. > Like this: > dicts.py > ----------------------- > dict1 = {....} > dict2 = {....} > dict3 = {....} > > Then, when I need any dictionary, I can access it: > import dicts > dicts.dict1 > > Is it a good practice? Or should I store them as class attributes or > anything else? > > Thanks in advance. > > With regards, Max > (sorry if my English isn't very proper) > Defining dict as a module attribute ic correct, but try to answer the following question: Is dict1 an attribute/property/declension of the object/entity defined by the module dicts ? If yes, then your design is correct. An correct example: fruits.py ------------ apple = {} banana = {} An incorrect one: fruit.py ----------- apple={} bicycle={} Basically, the rule is very straightforward, set your dict as a module attribute only if its one of its attribute (very nice paraphrase !) Most of people (including me) tend to have a module, where they put everything they have no idea about their location. This is a bad habit and result from a uncontrolled/undocumented design. Usually documenting objects in such modules is really painful. Your proposal is fine from a python syntax point of view. I can't tell of your design with names like (dicts.py and dict1,dict2) hoping you do not intend to name them that way. JM From deets at nospam.web.de Mon Jul 20 09:10:17 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 20 Jul 2009 15:10:17 +0200 Subject: proposal: add setresuid() system call to python References: <20090717200141.GI6065@subspacefield.org> <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> Message-ID: <7cj89qF27mfrjU1@mid.uni-berlin.de> Neal Becker wrote: again with his notorious gmane.comp.python.general group that doesn't work for any decent news-reader..... > Mahmoud Abdelkader wrote: > >> Why don't you write a python extension module? This is a perfect >> opportunity for that. >> > I think having a module just for one system call is a bit silly. Why not > add to os module? While I do agree that it's an omission, it is easy enough to get it yourself using ctypes: from ctypes import * from ctypes.util import find_library clib = CDLL(find_library("c")) # this might be different on 64bit __uid_t = c_uint _setresuid = clib.setresuid _setresuid.argtypes = [__uid_t, __uid_t, __uid_t] _setresuid.restype = c_int def setresuid(ruid, euid, suid): return _setresuid(__uid_t(ruid), __uid_t(euid), __uid_t(suid)) print setresuid(1000, 1000, 1000) # returns 0 for me print setresuid(1001, 1001, 1001) # fails. Diez From stefan_ml at behnel.de Mon Jul 20 09:22:02 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 20 Jul 2009 15:22:02 +0200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a645b81$0$12974$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <4a646f7b$0$31869$9b4e6d93@newsspool3.arcor-online.net> William Dode wrote: > On 19-07-2009, Mark Dufour wrote: >> I have just released version 0.2 of Shed Skin, an experimental >> (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). > > I just tested it with a litle game, to find the places of horse on > a board 5x5. The result is : > > [...] > shedskin 8s > python + psyco 18s > cython avec malloc *int 18s > cython 55s avec [] python > python 303s (5m3s) Note that both Psyco and Cython make a lot less assumptions about Python code than Shed Skin does. Psyco has the advantage of just needing to jump in when it finds out that it can help, so it's the most broadly compatible of the three. But Cython also supports quite a large corpus of dynamic Python code by now. Shed Skin has a lot of restrictions, many of which are by design. It's not intended to compile dynamic code, and I think that's a good thing, because that's what makes it fast for the code that it supports. Getting the same speed in Cython requires a bit more explicit typing, simply because Cython does not assume these restrictions. I think that all three have their raison d'?tre, and it currently looks like all three are there to stay and to keep growing better. And I'm also happy to read that some optimisations jump from one to the other. ;) Stefan From Lacrima.Maxim at gmail.com Mon Jul 20 09:42:23 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Mon, 20 Jul 2009 06:42:23 -0700 (PDT) Subject: Design question. References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> Message-ID: <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> On Jul 20, 4:05?pm, Jean-Michel Pichavant wrote: > Lacrima wrote: > > Hello! > > > I am newbie in python and I have really simple question, but I need > > your advice to know how to do best. > > I need to store a number of dictionaries in certain place. I've > > decided to store them in a separate module. > > Like this: > > dicts.py > > ----------------------- > > dict1 = {....} > > dict2 = {....} > > dict3 = {....} > > > Then, when I need any dictionary, I can access it: > > import dicts > > dicts.dict1 > > > Is it a good practice? Or should I store them as class attributes or > > anything else? > > > Thanks in advance. > > > With regards, Max > > (sorry if my English isn't very proper) > > Defining dict as a module attribute ic correct, but try to answer the > following question: > > Is dict1 an attribute/property/declension of the object/entity defined > by the module dicts ? > If yes, then your design is correct. > > An correct example: > fruits.py > ------------ > apple = {} > banana = {} > > An incorrect one: > fruit.py > ----------- > apple={} > bicycle={} > > Basically, the rule is very straightforward, set your dict as a module > attribute only if its one of its attribute (very nice paraphrase !) > Most of people (including me) tend to have a ?module, where they put > everything they have no idea about their location. This is a bad habit > and result from a uncontrolled/undocumented design. Usually documenting > objects in such modules is really painful. > > Your proposal is fine from a python syntax point of view. I can't tell > of your design with names like (dicts.py and dict1,dict2) hoping you do > not intend to name them that way. > > JM Hi, Jean-Michel! Thanks for your answer. I am not going to have names like dicts.py and dict1,dict2. That was just example. I wanted to know if it is correct to have dictionaries on a module level. Now I know that this is correct. I want to collect in one module a number of dictionaries, every of which describes a separate web service. And my function in another module will import required dictionary, depending on what web service should be used. Thanks again for the help. With regards, Max. (sorry if my English isn't very proper) From gerard.blais at gmail.com Mon Jul 20 09:47:54 2009 From: gerard.blais at gmail.com (Gerry) Date: Mon, 20 Jul 2009 06:47:54 -0700 (PDT) Subject: win32api install problem Message-ID: I'm running Python 2.6 under XP. I've installed Windows 32 extensions for Python 2.6 version 1.4 (pywin32-214.win32-py2.6.exe). But If I try to import win32api, I get: File "C:\python_projects\euler\driveletters.py", line 1, in import win32api ImportError: DLL load failed: The specified module could not be found. \Python26\Lib\site-packages has: 03/23/2009 08:35 AM win32 07/20/2009 09:08 AM win32com 02/18/2009 01:21 PM win32comext Can anyone offer a suggestion? Thanks, Gerry From mail at timgolden.me.uk Mon Jul 20 09:57:56 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 20 Jul 2009 14:57:56 +0100 Subject: win32api install problem In-Reply-To: References: Message-ID: <4A6477E4.3030808@timgolden.me.uk> Gerry wrote: > I'm running Python 2.6 under XP. > > I've installed Windows 32 extensions for Python 2.6 version 1.4 > (pywin32-214.win32-py2.6.exe). > > But If I try to import win32api, I get: > > File "C:\python_projects\euler\driveletters.py", line 1, in > import win32api > ImportError: DLL load failed: The specified module could not be found. Used to be you'd get this error if you installed as a non-admin user. Don't know if that's still an issue. Possibility? TJG From dtgeadamo at yahoo.com Mon Jul 20 09:57:57 2009 From: dtgeadamo at yahoo.com (mistersexy) Date: Mon, 20 Jul 2009 06:57:57 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account Message-ID: I am trying to create a Windows service in Python using pywin32. I do not want this service to run under a user account. I want this service to be able to run as a LocalService, NetworkService and the like. How do I specify this using the win32 library? Thanks, everyone. From steve at REMOVE-THIS-cybersource.com.au Mon Jul 20 10:00:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 20 Jul 2009 14:00:30 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: Message-ID: <02746997$0$5185$c3e8da3@news.astraweb.com> On Mon, 20 Jul 2009 09:34:24 +0000, Sion Arrowsmith wrote: > Terry Reedy wrote: >>Sion Arrowsmith wrote: >>> Jack Diederich wrote: >>>> It isn't an OrderedDict thing, it is a comparison thing. Two regular >>>> dicts also raise an error if you try to LT them. >>> Python 2.5.2 >>>>>> d1 = dict((str(i), i) for i in range (10)) d2 = dict((str(i), i) >>>>>> for i in range (20)) d1 < d2 >>> True >>Try reversing the definitions of d1 and d2. The dicts are probably being >>compared by id (address), which is the 2.x CPython default. > > Like this? > >>>> d1 = dict((str(i), i) for i in range (20)) >>>> d2 = dict((str(i), i) for i in range (10)) >>>> d1 < d2 > False >>>> id(d1) < id(d2) > True > > I didn't know that comparison for anything other than equality defaulted > to using id. That really is rather broken, and I'm glad 3.0 fixed it. I don't think comparisons other than equality use id. That would be rather insane. If anyone can demonstrate such comparisons in a built-in or standard library class, I'd like to see it. For the record, dicts have been comparable with < and > since at least Python 1.5: $ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> >>> {1: 'a', 2: 'b'} < {2: 'b', 3: 'a'} 1 Reading the docs: http://docs.python.org/library/stdtypes.html#comparisons http://docs.python.org/library/stdtypes.html#mapping-types-dict I can only suggest that dicts compare in an arbitrary but consistent fashion. What that is based on, I don't know, but based on some very limited testing, I'd guess it is like this: If the two dicts have unequal length, the longer dict compares greater than the shorter dict. If the two dicts are equal in length, (key, item) pairs are compared, probably in lexicographic order, because the order of insertion of keys doesn't appear to matter. -- Steven From mail at timgolden.me.uk Mon Jul 20 10:03:14 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 20 Jul 2009 15:03:14 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: References: Message-ID: <4A647922.4080908@timgolden.me.uk> mistersexy wrote: > I am trying to create a Windows service in Python using pywin32. I do > not want this service to run under a user account. I want this service > to be able to run as a LocalService, NetworkService and the like. How > do I specify this using the win32 library? Thanks, everyone. When you "install" the service, using the HandleCommandLine option, specify --username= and --password options. TJG From nyamatongwe+thunder at gmail.com Mon Jul 20 10:05:33 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Mon, 20 Jul 2009 14:05:33 GMT Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: casebash: > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? I wrote a gap buffer implementation for Python 2.5 allowing character, unicode character and integer elements. http://code.google.com/p/gapbuffer/ Its not seen much use or any maintenance so is unlikely to work with Python 3.x. Neil From hniksic at xemacs.org Mon Jul 20 10:10:35 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Mon, 20 Jul 2009 16:10:35 +0200 Subject: proposal: add setresuid() system call to python References: <20090717200141.GI6065@subspacefield.org> <148918f0907171315i4ed5b274v77581a43c4464f8f@mail.gmail.com> <7cj89qF27mfrjU1@mid.uni-berlin.de> Message-ID: <877hy38mxw.fsf@busola.homelinux.net> "Diez B. Roggisch" writes: To emulate the os-module-type calls, it's better to raise exceptions than return negative values: > def setresuid(ruid, euid, suid): > return _setresuid(__uid_t(ruid), __uid_t(euid), __uid_t(suid)) def setresuid(ruid, euid, suid): res = _setresuid(__uid_t(ruid), __uid_t(euid), __uid_t(suid)) if res < 0: raise OSError('[Errno %d] %s' % (os.errno, errno.strerror(os.errno))) From steve at REMOVE-THIS-cybersource.com.au Mon Jul 20 10:21:15 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 20 Jul 2009 14:21:15 GMT Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> Message-ID: <02746e73$0$5185$c3e8da3@news.astraweb.com> On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote: > casebash writes: > >> I have searched this list and found out that Python doesn't have a >> mutable string class (it had an inefficient one, but this was removed >> in 3.0). Are there any libraries outside the core that offer this? > > A mutable string would not (AFAICT) be usefully implementable as a > subclass of the built-in string types. So even if such a type existed, > it would not be useable with all the functionality that works with > strings. If applications ignore duck-typing and do isinstance(value, str), it's arguably the application and not the value that is broken. Besides, with the new __isinstance__ method, surely such a mutable string could claim to be an instance of string without needing to inherit from string? > What is it you're trying to do that makes you search for a mutable > string type? It's likely that a better approach can be found. When dealing with very large strings, it is wasteful to have to duplicate the entire string just to mutate a single character. However, when dealing with very large strings, it's arguably better to use the rope data structure instead. http://en.wikipedia.org/wiki/Rope_(computer_science) -- Steven From invalid at invalid Mon Jul 20 10:59:17 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 20 Jul 2009 09:59:17 -0500 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1410bb2c-e37f-445b-9fe4-9c4443ad5592@g1g2000pra.googlegroups.com> Message-ID: On 2009-07-20, Tim Daneliuk wrote: >>> In fact, picking a computer language is the most important >>> discussion in Computer Science and eclipses even P=NP? in >>> significance. I sure hope we can keep this thread going for a >>> few months. >> >> Please feel free to extend this flame-war along for a few >> months on comp.lang.lisp. Not here. > > Uh Carl ... are you familiar with the concept of mocking humor? Irony on Usenet: always a bit of a gamble... -- Grant Edwards grante Yow! Vote for ME -- I'm at well-tapered, half-cocked, visi.com ill-conceived and TAX-DEFERRED! From craig1.boyd at citi.com Mon Jul 20 11:09:47 2009 From: craig1.boyd at citi.com (Boyd, Craig1 ) Date: Mon, 20 Jul 2009 10:09:47 -0500 Subject: Auto-generate GUI from a database table structure Message-ID: <55660F9659966544BBA3AE14CE5D278D01243B8E04@exgtmb08.nam.nsroot.net> Hello All, I am REALLY new to python and still trying to figure a lot of this stuff out. I am trying to write a couple screens to update three or four database tables on Oracle 10g and I was wondering if there was a way to automatically generate some kind of GUI shell based on the tables that I could then fill in with the logic / functionality I need. I have coded and run some of the python / tcl examples that I could find so I kind of get that, but it appears to be a bit labor intensive. Is there an easier way? Thanks, Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: From tvashtar at gmail.com Mon Jul 20 11:11:34 2009 From: tvashtar at gmail.com (tvashtar) Date: Mon, 20 Jul 2009 08:11:34 -0700 (PDT) Subject: Problem when applying Patch from issue1424152 to get https over authenticating proxies working with urllib2 in Python 2.5 Message-ID: <8fe2ad78-0bd4-40d8-909c-1d2784441e3c@s31g2000yqs.googlegroups.com> Hi, I'm trying to get https requests working through an authenticating proxy with urllib2 in Python 2.5, I'm aware that this isn't supported "out of the box", so applied the patch http://bugs.python.org/file9753/http-tunnel-urllib linked from http://bugs.python.org/issue1424152 , my baseline test program is the following: p = "user:pass at proxy:port" proxy_handler = urllib2.ProxyHandler({"http": p, "https": p}) urllib2.install_opener( urllib2.build_opener( urllib2.HTTPHandler, urllib2.HTTPSHandler, proxy_handler)) request = urllib2.Request( "https://groups.google.com") response = urllib2.urlopen(request) Unfortunately this doesn't work, the call stack being: Traceback (most recent call last): File "", line 1, in File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 121, in urlopen return _opener.open(url, data) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 374, in open response = self._open(req, data) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 392, in _open '_open', req) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 353, in _call_chain result = func(*args) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 1108, in https_open return self.do_open(httplib.HTTPSConnection, req) File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \urllib2.py", line 1075, in do_open raise URLError(err) urllib2.URLError: Does anyone know why I might be hitting this issue? Any help is greatly appreciated, thanks From nikekoo at gmail.com Mon Jul 20 11:42:15 2009 From: nikekoo at gmail.com (Nike) Date: Mon, 20 Jul 2009 08:42:15 -0700 (PDT) Subject: Problem when applying Patch from issue1424152 to get https over authenticating proxies working with urllib2 in Python 2.5 References: <8fe2ad78-0bd4-40d8-909c-1d2784441e3c@s31g2000yqs.googlegroups.com> Message-ID: <58f2558c-33fe-4ee0-9a0a-2563374be47c@x6g2000prc.googlegroups.com> On Jul 20, 11:11?pm, tvashtar wrote: > Hi, > I'm trying to get https requests working through an authenticating > proxy with urllib2 in Python 2.5, I'm aware that this isn't supported > "out of the box", so applied the patchhttp://bugs.python.org/file9753/http-tunnel-urllib > linked fromhttp://bugs.python.org/issue1424152, my baseline test > program is the following: > > p = "user:pass at proxy:port" > proxy_handler = urllib2.ProxyHandler({"http": p, "https": p}) > urllib2.install_opener( urllib2.build_opener( urllib2.HTTPHandler, > > urllib2.HTTPSHandler, > > proxy_handler)) > > request = urllib2.Request( "https://groups.google.com") > response = urllib2.urlopen(request) > > Unfortunately this doesn't work, the call stack being: > > Traceback (most recent call last): > ? File "", line 1, in > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 121, in urlopen > ? ? return _opener.open(url, data) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 374, in open > ? ? response = self._open(req, data) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 392, in _open > ? ? '_open', req) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 353, in _call_chain > ? ? result = func(*args) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 1108, in https_open > ? ? return self.do_open(httplib.HTTPSConnection, req) > ? File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem > \urllib2.py", line 1075, in do_open > ? ? raise URLError(err) > urllib2.URLError: routines:SSL23_GET_SERVER_HELLO:unknown protocol')> > > Does anyone know why I might be hitting this issue? > Any help is greatly appreciated, > thanks hi! It's looks like a ssl error . Under the following step to help u : 1. takes a simple code to confirm your pupose without ssl protocol. 2. to confirm python version and extended libs work well 3. to confirm ssl work well. goog luck! nikekoo From hyugaricdeau at gmail.com Mon Jul 20 11:52:39 2009 From: hyugaricdeau at gmail.com (Hyuga) Date: Mon, 20 Jul 2009 08:52:39 -0700 (PDT) Subject: are user defined classes hashable? References: Message-ID: <373d6c69-6965-4a88-bdd2-8028ef850bf8@k6g2000yqn.googlegroups.com> On Jul 19, 11:39?am, Nicolas Dandrimont wrote: > * Alan G Isaac [2009-07-19 14:46:12 +0000]: > > > Again, my question is about the class not its instances, > > but still, checking as you suggest gives the same answer. > > That's what I get for answering before my coffee! Regardless, Nicolas's example can be applied to the class too: >>> class Foo(object): pass >>> hash(Foo) 11443104 >>> id(Foo) 11443104 class objects are just objects of type 'type'. From dtgeadamo at yahoo.com Mon Jul 20 11:55:35 2009 From: dtgeadamo at yahoo.com (mistersexy) Date: Mon, 20 Jul 2009 08:55:35 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: Message-ID: On Jul 20, 3:03?pm, Tim Golden wrote: > mistersexy wrote: > > I am trying to create a Windows service in Python using pywin32. I do > > not want this service to run under a user account. I want this service > > to be able to run as a LocalService, NetworkService and the like. How > > do I specify this using the win32 library? Thanks, everyone. > > When you "install" the service, using the HandleCommandLine > option, specify --username= and --password options. > > TJG That's exactly my point. I do not want to have to specify username and password options. For instance, when creating a Windows Service in .NET, it is possible to specify that the service should run using the LocalService or NetworkService account. Doing this, you would not need to specify username and password options. Is there a way to achieve this in Python? From aahz at pythoncraft.com Mon Jul 20 12:00:33 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 09:00:33 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: In article , tkouts wrote: > >I'm pleased to announce the new version of Porcupine Web Application >Server, a Python based framework that provides front-end and back-end >technologies for building modern data-centric Web 2.0 applications. > > [...] > >QuiX, the server's integrated JavaScript toolkit, has reached the >major milestone of supporting all the popular browsers including >Opera, Safari 4 and IE8. [...] Out of curiosity, are there any JavaScript toolkits that generate code that degrades gracefully when JavaScript is disabled? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From vs at it.uu.se Mon Jul 20 12:09:42 2009 From: vs at it.uu.se (Virgil Stokes) Date: Mon, 20 Jul 2009 18:09:42 +0200 Subject: On out-of-date Python Applications In-Reply-To: <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> Message-ID: <4A6496C6.4040606@it.uu.se> An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Mon Jul 20 12:14:31 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 20 Jul 2009 17:14:31 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: References: Message-ID: <4A6497E7.7090908@timgolden.me.uk> mistersexy wrote: > On Jul 20, 3:03 pm, Tim Golden wrote: >> mistersexy wrote: >>> I am trying to create a Windows service in Python using pywin32. I do >>> not want this service to run under a user account. I want this service >>> to be able to run as a LocalService, NetworkService and the like. How >>> do I specify this using the win32 library? Thanks, everyone. >> When you "install" the service, using the HandleCommandLine >> option, specify --username= and --password options. >> >> TJG > > That's exactly my point. I do not want to have to specify username and > password options. For instance, when creating a Windows Service > in .NET, it is possible to specify that the service should run using > the LocalService or NetworkService account. Doing this, you would not > need to specify username and password options. Is there a way to > achieve this in Python? Sorry, I misread: I mentally removed the "not" in your 'I do not want this service to run under a user account' and reinserted it further on! By default, the service will run as LocalSystem: you only specify a username to override that default. The value in Username is passed straight through to the CreateService Win32 API, and the docs for that: http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx say: """ lpServiceStartName [in, optional] The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName. If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account. If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account. A shared process can run as any user. If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system. A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide. Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2. """ So, although I haven't tried it, it looks as though you can pass "LocalService" or "NetworkService" and so on if you want to override the default LocalSystem but don't want to specify a username/password. TJG From ebonak at hotmail.com Mon Jul 20 12:22:47 2009 From: ebonak at hotmail.com (Esmail) Date: Mon, 20 Jul 2009 12:22:47 -0400 Subject: comments? storing a function in an object Message-ID: Hello all, I am trying to store a function and some associated information in an object so that I can later have series of functions in a list that I can evaluate one at a time. Right now I am only storing the function itself, the number of arguments it expects and its string representation. I may add other information such as the acceptable range for the parameters or other characteristics such as maximum or minimum. I wonder if anyone could comment on my implementation and offer suggestions or constructive criticisms? The 'loading' of the functions is a bit tedious and of course care has to be taken to make sure the string representation corresponds to the actual function computed. It would be nice if there was an automatic way to convert the function to its string representation. Comments or problems with the approach I have taken? Thanks, Esmail -- #!/usr/bin/env python # # store and represent functions # import math class FunctionException(Exception): """ custom exception """ def __init__(self, value): self.parameter = value def __str__(self): return repr(self.parameter) class Function(object): """ class to represent a function """ def __init__(self, fn, fn_str, num_vars): """ the function, its string representation, and the number of variables """ self.fn = fn self.fn_str = fn_str self.num_vars = num_vars def eval_fn(self, variables): """ size of variables should equal num_vars .. else problem """ if len(variables) == self.num_vars: result = self.fn(*variables) return result else: raise FunctionException('invalid number of args provided - '+ 'received %d, expected %d' %(len(variables), self.num_vars)) def str(self): """ return string representation of function """ return self.fn_str def funct1(x): ''' small test function ''' return x * x def funct2(x, y): ''' small test function ''' return x + y def funct3(x): ''' small test function ''' return 1000 + (x*x + x) * math.cos(x) def main(): """ main method """ print 'in main' fn1 = Function(funct1, 'x * x', 1) fn2 = Function(funct2, 'x + y', 2) fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) for i in range(-10, 10): try: print 'f(', [i],') =', print fn3.str(), ' => ', print fn3.eval_fn([i]) except FunctionException, (ex): print ex.parameter if __name__ == '__main__': main() From dtgeadamo at yahoo.com Mon Jul 20 12:25:44 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Mon, 20 Jul 2009 09:25:44 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: Message-ID: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> On Jul 20, 5:14?pm, Tim Golden wrote: > mistersexy wrote: > > On Jul 20, 3:03 pm, Tim Golden wrote: > >> mistersexy wrote: > >>> I am trying to create a Windows service in Python using pywin32. I do > >>> not want this service to run under a user account. I want this service > >>> to be able to run as a LocalService, NetworkService and the like. How > >>> do I specify this using the win32 library? Thanks, everyone. > >> When you "install" the service, using the HandleCommandLine > >> option, specify --username= and --password options. > > >> TJG > > > That's exactly my point. I do not want to have to specify username and > > password options. For instance, when creating a Windows Service > > in .NET, it is possible to specify that the service should run using > > the LocalService or NetworkService account. Doing this, you would not > > need to specify username and password options. Is there a way to > > achieve this in Python? > > Sorry, I misread: I mentally removed the "not" in your 'I do not want > this service to run under a user account' and reinserted it > further on! > > By default, the service will run as LocalSystem: you > only specify a username to override that default. The value > in Username is passed straight through to the CreateService > Win32 API, and the docs for that: > > ?http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx > > say: > > """ > lpServiceStartName [in, optional] > > ? ? The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName. > > ? ? If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account. > > ? ? If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account. > > ? ? A shared process can run as any user. > > ? ? If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system. > > ? ? A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide. > > ? ? ? ? Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: ?Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2. > """ > > So, although I haven't tried it, it looks as though you can pass > "LocalService" or "NetworkService" and so on if you want to > override the default LocalSystem but don't want to specify a > username/password. > > TJG I'll try this stuff. Thanks a million...I'll let everyone know how it goes. From bearophileHUGS at lycos.com Mon Jul 20 12:26:08 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Mon, 20 Jul 2009 09:26:08 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> William Dode': > I just tested it with a litle game, to find the places of horse on > a board 5x5. The result is : > > c 5s > gcj 7s > java 7s > shedskin 8s > python + psyco 18s > cython avec malloc *int 18s > cython 55s avec [] python > python 303s (5m3s) Nice timings, can you please show me the Python, Java and C code versions? I may do more tests. The purpose of all those "example programs" in ShedSkin is to find bugs and to find details to speedup. Bye, bearophile From phillip.oldham at gmail.com Mon Jul 20 12:27:05 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Mon, 20 Jul 2009 17:27:05 +0100 Subject: Help understanding the decisions *behind* python? Message-ID: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> My colleagues and I have been working with python for around 6 months now, and while we love a lot of what python has done for us and what it enables us to do some of the decisions behind such certain data-types and their related methods baffle us slightly (when compared to the decisions made in other, similarly powerful languages). Specifically the "differences" between lists and tuples have us confused and have caused many "discussions" in the office. We understand that lists are mutable and tuples are not, but we're a little lost as to why the two were kept separate from the start. They both perform a very similar job as far as we can tell. Consider the following: >>> x = [2,1,3] >>> x.sort() >>> print x [1, 2, 3] Now, if the sort operations were unable to affect the original structure of the list (as in JavaScript) you'd effectively have a tuple which you could add/remove from, and the example above would look more like: >>> x = [2,1,3] >>> print x.sort() [1, 2, 3] >>> print x [2,1,3] This make a lot more sense to us, and follows the convention from other languages. It would also mean chaining methods to manipulate lists would be easier: >>> x = [2,1,3] >>> print x.sort()[0] 3 >>> print x [2,1,3] We often find we need to do manipulations like the above without changing the order of the original list, and languages like JS allow this. We can't work out how to do this in python though, other than duplicating the list, sorting, reversing, then discarding. We're not looking to start any arguments or religious wars and we're not asking that python be changed into something its not. We'd simply like to understand the decision behind the lists and tuple structures. We feel that in not "getting" the difference between the two types we may be missing out on using these data structures to their full potential. From tycho at tycho.ws Mon Jul 20 12:43:16 2009 From: tycho at tycho.ws (Tycho Andersen) Date: Mon, 20 Jul 2009 11:43:16 -0500 Subject: Help understanding the decisions *behind* python? In-Reply-To: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: <49b3a7400907200943s2ed382c7qa0a38aa67db79885@mail.gmail.com> On Mon, Jul 20, 2009 at 11:27 AM, Phillip B Oldham wrote: > > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. > I have no idea about why the design decisions were made. You might take a look at the sorted() function: http://docs.python.org/library/functions.html#sorted It will do what you want. \t -- http://tycho.ws From http Mon Jul 20 12:50:56 2009 From: http (Paul Rubin) Date: 20 Jul 2009 09:50:56 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: <7xljmjguxb.fsf@ruckus.brouhaha.com> vippstar writes: > > I wonder whether 2/3 => ValueError is preferable. > > Not all software wants this. It shouldn't be part of the language but > rather part of your code if you need such a feature. (for instance, to > distinguish between 2/3 and divisions with 0 dividend). I don't see how to implement such a thing in my code, if I believe that the ring of integers doesn't have any concept of division and so attempts to divide integers should be treated as errors. Yes of course the present / operator is useful, but I could do just as well with the divmod function which I think is more explicit. From marcusw at cox.net Mon Jul 20 13:06:39 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 13:06:39 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <7x4ot7rif8.fsf@ruckus.brouhaha.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: On 7/20/2009 2:13 AM, Paul Rubin wrote: > Steven D'Aprano writes: >> Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing >> to do. It's the right thing to do if you're doing integer maths. > > I wonder whether 2/3 => ValueError is preferable. Not for me :( From duncan.booth at invalid.invalid Mon Jul 20 13:08:38 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 20 Jul 2009 17:08:38 GMT Subject: Help understanding the decisions *behind* python? References: Message-ID: Phillip B Oldham wrote: > This make a lot more sense to us, and follows the convention from > other languages. It would also mean chaining methods to manipulate > lists would be easier: > >>>> x = [2,1,3] >>>> print x.sort()[0] > 3 >>>> print x > [2,1,3] You already have a way to do what you want: >>> x = [2,1,3] >>> print sorted(x)[0] 3 >>> print x [2,1,3] as a bonus you can use 'sorted' to sort any sequence including generators or tuples, but the result will always be a list: if it was a list method then you would have to convert the sequence to a list first. The main reason why you need both lists and tuples is that because a tuple of immutable objects is itself immutable you can use it as a dictionary key. You can't use a list as a dictionary key because if something were to mutate a key the dictionary structure would behave very strangely indeed. The types 'set' and 'frozenset' both exist for the same reason. From darcy at druid.net Mon Jul 20 13:08:56 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Mon, 20 Jul 2009 13:08:56 -0400 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) In-Reply-To: References: Message-ID: <20090720130856.fdee4c6d.darcy@druid.net> On 20 Jul 2009 09:00:33 -0700 aahz at pythoncraft.com (Aahz) wrote: > Out of curiosity, are there any JavaScript toolkits that generate code > that degrades gracefully when JavaScript is disabled? I understand what you want but I can't see how a toolkit can do that. How do you program "graceful?" It seems pretty application specific. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From marcusw at cox.net Mon Jul 20 13:11:20 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 13:11:20 -0400 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: On 7/20/2009 5:34 AM, Stef Mientki wrote: > thanks Marcus, > > Marcus Wanner wrote: >> On 7/19/2009 4:15 PM, Stef Mientki wrote: >>> hello, >>> >>> I'm using Scintilla as a wxPython widget with great pleasure. >>> I now have an application where I want to make notes during a >>> conversation, >>> but also want to record the speech during that conversation. >>> I'm using Scintilla as a wxPython widget for editing and PyAudio for >>> the speech recording, >>> until so far everything works fine. >>> >>> Here the problem part: >>> I need to synchronize the typed text with the sound during playback. >>> So if I click somewhere in the sound recording, >>> the line of text, typed that moment should be highlighted. >>> And vise versa, if the cursor in the text is moved and some special >>> key is pressed, >>> the sound starting 10 or 20seconds earlier should be playbacked. >>> >>> I though of adding bookmarks (because these are fixed in the text), >>> and keep a list of bookmarks and sound pointers. >>> This idea should work, but there are only 31 bookmarks. >>> >>> Any other suggestions ? >>> >>> thanks, >>> Stef Mientki >> That sounds like a very specialized type of thing, > Well from an application point of view, > with the current netbooks, > this looks like a perfect tool for any conversation or meeting. >> which only the few people with experience with wxPython, PyAudio, and >> Scintilla could help you with... >> > I was afraid of that too, so I dropped the question in several places, > and the writer of Scintilla himself came with the perfect answer. > > cheers,Stef >> But you might try having a dictionary with notes and associated times, >> or just give each note a four-digit ID number at the beginning of it >> when it's entered and use that in the dictionary (to keep keys >> shorter). Or you could just do a little hack and increase the number >> of bookmarks allowed (seeing as source is available) :p >> >> Marcus > Glad you got a good answer from somebody. Sounds like an interesting project. About when would this be headed for a release? Could you post a link to a googlecode or sourceforge project or something so I can follow and/or help with development? Marcus From motoom at xs4all.nl Mon Jul 20 13:17:19 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Mon, 20 Jul 2009 19:17:19 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: <4A64A69F.2030400@xs4all.nl> Phillip wrote: > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. Yes, but because of their immutability you can use tuples as dictionary keys (only if they contain immutable objects). Lists can't be used as dictionary keys. Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From gerard.blais at gmail.com Mon Jul 20 13:28:09 2009 From: gerard.blais at gmail.com (MCIPERF) Date: Mon, 20 Jul 2009 10:28:09 -0700 (PDT) Subject: win32api install problem References: Message-ID: On Jul 20, 9:57?am, Tim Golden wrote: > Gerry wrote: > > I'm running Python 2.6 under XP. > > > I've installed Windows 32 extensions for Python 2.6 version 1.4 > > (pywin32-214.win32-py2.6.exe). > > > But If I try to import win32api, I get: > > > ? File "C:\python_projects\euler\driveletters.py", line 1, in > > ? ? import win32api > > ImportError: DLL load failed: The specified module could not be found. > > Used to be you'd get this error if you installed as a > non-admin user. Don't know if that's still an issue. > Possibility? > > TJG Not a possibility (afaict) -- I am an administrator, according to the control panel. From marcusw at cox.net Mon Jul 20 13:42:55 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 13:42:55 -0400 Subject: Design question. In-Reply-To: <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> Message-ID: On 7/20/2009 9:42 AM, Lacrima wrote: > On Jul 20, 4:05 pm, Jean-Michel Pichavant > wrote: >> Lacrima wrote: >>> Hello! >>> I am newbie in python and I have really simple question, but I need >>> your advice to know how to do best. >>> I need to store a number of dictionaries in certain place. I've >>> decided to store them in a separate module. >>> Like this: >>> dicts.py >>> ----------------------- >>> dict1 = {....} >>> dict2 = {....} >>> dict3 = {....} >>> Then, when I need any dictionary, I can access it: >>> import dicts >>> dicts.dict1 >>> Is it a good practice? Or should I store them as class attributes or >>> anything else? >>> Thanks in advance. >>> With regards, Max >>> (sorry if my English isn't very proper) >> Defining dict as a module attribute ic correct, but try to answer the >> following question: >> >> Is dict1 an attribute/property/declension of the object/entity defined >> by the module dicts ? >> If yes, then your design is correct. >> >> An correct example: >> fruits.py >> ------------ >> apple = {} >> banana = {} >> >> An incorrect one: >> fruit.py >> ----------- >> apple={} >> bicycle={} >> >> Basically, the rule is very straightforward, set your dict as a module >> attribute only if its one of its attribute (very nice paraphrase !) >> Most of people (including me) tend to have a module, where they put >> everything they have no idea about their location. This is a bad habit >> and result from a uncontrolled/undocumented design. Usually documenting >> objects in such modules is really painful. >> >> Your proposal is fine from a python syntax point of view. I can't tell >> of your design with names like (dicts.py and dict1,dict2) hoping you do >> not intend to name them that way. >> >> JM > > Hi, Jean-Michel! > > Thanks for your answer. > I am not going to have names like dicts.py and dict1,dict2. That was > just example. I wanted to know if it is correct to have dictionaries > on a module level. Now I know that this is correct. I want to collect > in one module a number of dictionaries, every of which describes a > separate web service. And my function in another module will import > required dictionary, depending on what web service should be used. > Thanks again for the help. > > With regards, > Max. > (sorry if my English isn't very proper) Yeah, you can put just about anything in a separate module/file, but putting unrelated things in the same module is bad... For example, if you have apple and banana in a module, and had a function called slicefruit() to do something to those variables in there with them, then that would be good. But if you put bicycle and car and adjustbrakes() in the module with the fruits, or mixed the two groups in several modules, that would be bad. Marcus From bieffe62 at gmail.com Mon Jul 20 13:44:16 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Mon, 20 Jul 2009 10:44:16 -0700 (PDT) Subject: comments? storing a function in an object References: Message-ID: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> On Jul 20, 6:22?pm, Esmail wrote: > Hello all, > > I am trying to store a function and some associated information in an > object so that I can later have series of functions in a list that I can > evaluate one at a time. > > Right now I am only storing the function itself, the number of > arguments it expects and its string representation. I may add other > information such as the acceptable range for the parameters or other > characteristics such as maximum or minimum. > > I wonder if anyone could comment on my implementation and offer > suggestions or constructive criticisms? > > The 'loading' of the functions is a bit tedious and of course care > has to be taken to make sure the string representation corresponds to > the actual function computed. It would be nice if there was an > automatic way to convert the function to its string representation. > > Comments or problems with the approach I have taken? > > Thanks, > Esmail > > -- > > #!/usr/bin/env python > > # > # store and represent functions > # > > import math > > class FunctionException(Exception): > ? ? ?""" custom exception """ > ? ? ?def __init__(self, value): > ? ? ? ? ?self.parameter = value > > ? ? ?def __str__(self): > ? ? ? ? ?return repr(self.parameter) > > class Function(object): > ? ? ?""" class to represent a function """ > > ? ? ?def __init__(self, fn, fn_str, num_vars): > ? ? ? ? ?""" > ? ? ? ? ?the function, its string representation, and the number of variables > ? ? ? ? ?""" > ? ? ? ? ?self.fn = fn > ? ? ? ? ?self.fn_str = fn_str > ? ? ? ? ?self.num_vars = num_vars > > ? ? ?def eval_fn(self, variables): > ? ? ? ? ?""" size of variables should equal num_vars .. else problem """ > ? ? ? ? ?if len(variables) == self.num_vars: > ? ? ? ? ? ? ?result = self.fn(*variables) > ? ? ? ? ? ? ?return result > ? ? ? ? ?else: > ? ? ? ? ? ? ?raise FunctionException('invalid number of args provided - '+ > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'received %d, expected %d' > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?%(len(variables), self.num_vars)) > > ? ? ?def str(self): > ? ? ? ? ?""" return string representation of function """ > ? ? ? ? ?return self.fn_str > > def funct1(x): > ? ? ?''' small test function ''' > ? ? ?return x * x > > def funct2(x, y): > ? ? ?''' small test function ''' > ? ? ?return x + y > > def funct3(x): > ? ? ?''' small test function ''' > ? ? ?return 1000 + (x*x + x) * math.cos(x) > > def main(): > ? ? ?""" main method """ > ? ? ?print 'in main' > > ? ? ?fn1 = Function(funct1, 'x * x', 1) > ? ? ?fn2 = Function(funct2, 'x + y', 2) > ? ? ?fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) > > ? ? ?for i in range(-10, 10): > ? ? ? ? ?try: > > ? ? ? ? ? ? ?print 'f(', [i],') =', > ? ? ? ? ? ? ?print fn3.str(), ' => ', > ? ? ? ? ? ? ?print fn3.eval_fn([i]) > > ? ? ? ? ?except FunctionException, (ex): > ? ? ? ? ? ? ?print ex.parameter > > if __name__ == '__main__': > ? ? ?main() I can offer some small suggestions: it is up to you to evaluate if they make sense for your app: 1. use __doc__ function standard attribute for function description (your fn_str); this meanst that you do not need fm_str in the constructor and you have to do e.g. : def funct2(x, y): ''' x + y ''' return x + y then funct2.__doc__ becomes the string you want to associate to the function 2. Use __call__ instead of eval_fn : this way, the instance of your Function became a 'callable' and can be used everywhere a function is needed. You can do: f = Function(...) f( some_args ) 3. If you call a python function with wrong number of arguments, it already raises a TypeError exception which contains - with different wording - the same information of your FunctionException : consider removing the check and using the python error instead HTH Ciao ------ FB From drobinow at gmail.com Mon Jul 20 13:46:26 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 20 Jul 2009 13:46:26 -0400 Subject: On out-of-date Python Applications In-Reply-To: <4A6496C6.4040606@it.uu.se> References: <63767354-1f6a-44fe-8c57-05873503a90e@b25g2000prb.googlegroups.com> <4A632C9D.2010303@it.uu.se> <4eb0089f0907200446p23d452fes1c5727676e92bf81@mail.gmail.com> <4A6496C6.4040606@it.uu.se> Message-ID: <4eb0089f0907201046x4bdc3b10t3ad56a888526eff2@mail.gmail.com> On Mon, Jul 20, 2009 at 12:09 PM, Virgil Stokes wrote: > David Robinow wrote: > > On Mon, Jul 20, 2009 at 7:24 AM, John Machin wrote: > ... > > > The next step would be to try to compile ODE 0.7 or 0.8 with VS9 -- > however this would require "project files" for ODE for VS9, and there > aren't any on the ODE website; it has only those for VS3 and VS5. > > > > The ODE site is a mess. > Go to http://www.ode.org/svn.html and click on: Instructions for > accessing the repository > Scroll down to the section "Building with Premake" > (Note that there is no directory "ode/build" -- you want the "build" > directory, which contains premake4.exe) > I used "premake4 --with-demos --with-tests vs2008" > I have successfully compiled ode-0.11.1 using these instructions. I > have not yet run the tests or demos or tried to compile PyODE. > > > Thanks for this information David. > Now that you have successfully compiled ode-0.11.1, how can one finish this > process --- compile PyODE? > > --V > Edit setup.py Windows specific settings: ODE_BASE to location of your ode distribution create releaselib directory under lib and copy the .lib file that you built in Visual Studio. This will be: ReleaseSingleLib\ode_single.lib or ReleaseDoubleLib\ode_double.lib or one of the Debug versions Change the name to ode.lib. i.e. in CMD-speak: %ODE_BASE%\lib\releaselib\ode.lib I was then able to build PyODE-1.2.0 However, all the examples died with: ODE Message 2: mass must be > 0 <..\..\ode\src\mass.cpp:49) I don't have time right now to debug this. Maybe somebody else can take it a bit further. From tvashtar at gmail.com Mon Jul 20 13:58:53 2009 From: tvashtar at gmail.com (tvashtar) Date: Mon, 20 Jul 2009 10:58:53 -0700 (PDT) Subject: Problem when applying Patch from issue1424152 to get https over authenticating proxies working with urllib2 in Python 2.5 References: <8fe2ad78-0bd4-40d8-909c-1d2784441e3c@s31g2000yqs.googlegroups.com> <58f2558c-33fe-4ee0-9a0a-2563374be47c@x6g2000prc.googlegroups.com> Message-ID: <446a3c42-6d86-4003-85be-3e5d8a23b268@c14g2000yqm.googlegroups.com> On Jul 20, 4:42?pm, Nike wrote: > hi! > ?It's looks like a ssl error . Under the following step to help u : > ? 1. takes a simple code to confirm your pupose without ssl protocol. > ? 2. to confirm python version and extended libs work well > ? 3. to confirm ssl work well. > > goog luck! > > nikekoo I've reduced my code to the following: import urllib2 p = "https://user:pass at myproxy:port" proxy_handler = urllib2.ProxyHandler({"https": p}) urllib2.install_opener(urllib2.build_opener(proxy_handler)) request = urllib2.Request( "https://groups.google.com") response = urllib2.urlopen(request) and it is now failing with: Traceback (most recent call last): File "D:\p4\depot\Development\HEAD\Build\ReleaseSystem\DownloadSystem \test.py", line 12, in response = urllib2.urlopen(request) File "C:\Python25\lib\urllib2.py", line 121, in urlopen return _opener.open(url, data) File "C:\Python25\lib\urllib2.py", line 379, in open response = self._open(req, data) File "C:\Python25\lib\urllib2.py", line 397, in _open '_open', req) File "C:\Python25\lib\urllib2.py", line 358, in _call_chain result = func(*args) File "C:\Python25\lib\urllib2.py", line 1115, in https_open return self.do_open(httplib.HTTPSConnection, req) File "C:\Python25\lib\urllib2.py", line 1082, in do_open raise URLError(err) urllib2.URLError: I thought the proxy_handler should take care of the authentication? Thanks for your help From kiorky at cryptelium.net Mon Jul 20 14:19:43 2009 From: kiorky at cryptelium.net (kiorky) Date: Mon, 20 Jul 2009 20:19:43 +0200 Subject: Compilation problem with Python < 2.6 and db >= 4.7 Message-ID: <4A64B53F.8060502@cryptelium.net> Is there a way to make the bsddb module compile against db>=4.7 for python < 2.6 (2.4.6, 2.5.4)? A patch ? A tip ? For the moment, i have a known failure as: gcc -pthread -fno-strict-aliasing -DNDEBUG -I/usr/home/kiorky/minitage/dependencies/readline-5.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/bzip2-1.0/parts/part/include -I/usr/home/kiorky/minitage/dependencies/zlib-1.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/db-4.7/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/expat-2.0/parts/part/include -I. -IInclude -I./Include -I/usr/home/kiorky/minitage/dependencies/readline-5.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/bzip2-1.0/parts/part/include -I/usr/home/kiorky/minitage/dependencies/zlib-1.2/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/db-4.7/parts/part/include -I/usr/home/kiorky/minitage/dependencies/openssl-0.9/parts/part/include -I/usr/home/kiorky/minitage/dependencies/expat-2.0/parts/part/include -fPIC -DPy_BUILD_CORE -I/usr/home/kiorky/minitage/dependencies/db-4.7/parts/part/include -c ./Modules/_bsddb.c -o Modules/_bsddb.o ./Modules/_bsddb.c: In function 'DBEnv_getattr': ./Modules/_bsddb.c:5337: error: 'DB_ENV' has no member named 'db_home' ./Modules/_bsddb.c:5340: error: 'DB_ENV' has no member named 'db_home' ./Modules/_bsddb.c: In function 'init_bsddb': ./Modules/_bsddb.c:5962: error: 'DB_LOG_AUTOREMOVE' undeclared (first use in this function) ./Modules/_bsddb.c:5962: error: (Each undeclared identifier is reported only once ./Modules/_bsddb.c:5962: error: for each function it appears in.) ./Modules/_bsddb.c:5963: error: 'DB_DIRECT_LOG' undeclared (first use in this function) ./Modules/_bsddb.c:5971: error: 'DB_LOG_INMEMORY' undeclared (first use in this function) *** Error code 1 I looked for the one inside python >= 2.6 and it seems to have changed a lot so backporting it seems difficult and maybe break the old API. I didn't find something on it. -- -- Cordialement, KiOrKY GPG Key FingerPrint: 0x1A1194B7681112AF From anthony.tolle at gmail.com Mon Jul 20 14:22:52 2009 From: anthony.tolle at gmail.com (Anthony Tolle) Date: Mon, 20 Jul 2009 11:22:52 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: On Jul 20, 12:27?pm, Phillip B Oldham wrote: > ... > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > ... There's no hard-set rules, but tuples are typically used to hold collections of heterogeneous data, e.g. (10, "spam", 3.21). As has been mentioned, such a tuple can be used as a dictionary key, whereas a list cannot be used as a dictionary key, because it is mutable. Lists, on the other hand, typically hold collections of homogeneous data, e.g. [1, 2, 5] or ["spam", "eggs", "sausage"]. Of course, you'll also see plenty of examples of tuples containing homogeneous data and lists containing heterogeneous data :) From lenz at joinville.udesc.br Mon Jul 20 14:26:50 2009 From: lenz at joinville.udesc.br (Eduardo Lenz) Date: Mon, 20 Jul 2009 11:26:50 -0700 Subject: ANN: psyco V2 In-Reply-To: <4A5FE672.3000401@stackless.com> References: <4A5FE672.3000401@stackless.com> Message-ID: <200907201126.51878.lenz@joinville.udesc.br> Em Qui 16 Jul 2009, ??s 19:48:18, Christian Tismer escreveu: > Announcing Psyco V2 source release > ---------------------------------- > > This is the long awaited announcement of Psyco V2. > > Psyco V2 is a continuation of the well-known psyco project, > which was called finished and was dis-continued by its author > Armin Rigo in 2005, in favor of the PyPy project. > > This is a new project, using Psyco's code base with permission > of Armin. Questions and complaints should go to me > (tismer at stackless.com) or the mailing list > (psyco-devel at lists.sourceforge.net); > Armin is explicitly not in charge of (t)his project any longer! > > As one of the founders and an active member of the PyPy > project, I was very happy to be invited to work on Psyco > V2, by FATTOC, LLC. Psyco V2 tries to extend on the original Psyco > approach "an extension module that just makes Python faster". > > Psyco is a just-in-time compiler that accelerates arbitrary > Python code by specialization. We believe that Psyco's approach > can be carried out much further than it was tried so far, when > it's first version was abandoned. > > This first V2 release is source-only. There is no web-site, yet, > and there are no binaries for download. These will be available > in a few days on http://www.psyco.org . > > For the time being, please stick with subversion access, > building the extension module from source code. The repository > is here: > > http://codespeak.net/svn/psyco/v2/dist > > Check-out the repository, and run the setup.py script, > given that you have access to a C compiler. > > Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, > and Mac OS X. Psyco is not supporting 64 bit, yet. But it > is well being considered. > > The current improvements are, shortly: > > - Support for Python 2.4, 2.5 and 2.6 > - a lot of new builtins > - generators, fast and fully supported. > > More information is coming soon on http://www.psyco.org . > > This is the beginning of a series of new Psyco versions. > Many more improvements are prepared and about to be published, > soon, starting with the current version 2.0.0 . > > Stay tuned, this is just the beginning of psyco's re-birth! > > For questions about Psyco V2, please join the mailing list > > psyco-devel at lists.sourceforge.net > > or contact me on IRC: > > #psyco on irc.freenode.net . > > Psyco V2 is fundamentally supported by FATTOC, LLC. > See http://www.fattoc.com . > > Without their continuous support, this work would not have > been possible at all. I wish to express my deepest thanks > to FATTOC, for allowing me to continue on Psyco with all the > energy that this ambitious project needs, and will need. > > Further special thanks are going to > Armin Rigo, John Benediktsson, David Salomon, Miki Tebeka, > Raymond Hettinger, Fabrizio Milo, Michael Foord, > Dinu Gherman, Stephan Diehl, Laura Creighton and Andrea Tismer, > for all the support and discussions. > > Looking forward to a great future of Psyco! > > July 17, 2009 I know its not the proper list but.... gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -DNDEBUG - g -O3 -fPIC -Ic/i386 -Ic -I/usr/include/python2.6 -c c/mergepoints.c -o build/temp.linux-i686-2.6/c/mergepoints.o c/mergepoints.c:250: error: ???RAISE_VARARGS??? undeclared here (not in a function) c/mergepoints.c:250: error: ???JUMP_IF_FALSE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???JUMP_IF_TRUE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???DUP_TOPX??? undeclared here (not in a function) c/mergepoints.c:250: error: ???IMPORT_STAR??? undeclared here (not in a function) c/mergepoints.c:250: error: ???SLICE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???STORE_SLICE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???DELETE_SLICE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_EXPR??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_ITEM??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_ITEM_TO??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_NEWLINE??? undeclared here (not in a function) c/mergepoints.c:250: error: ???PRINT_NEWLINE_TO??? undeclared here (not in a function) c/mergepoints.c:250: error: ???BUILD_CLASS??? undeclared here (not in a function) c/mergepoints.c:250: error: ???IMPORT_NAME??? undeclared here (not in a function) c/mergepoints.c:250: error: ???IMPORT_FROM??? undeclared here (not in a function) c/mergepoints.c:250: error: ???MAKE_FUNCTION??? undeclared here (not in a function) c/mergepoints.c:250: error: ???BUILD_SLICE??? undeclared here (not in a function) error: command 'gcc' failed with exit status 1 any clue ? []'s Eduardo Tel: +55 47 4009-7971 - Fax: +55 47 4009-7940 E-mail: lenz at Joinville.udesc.br --------------------------------------------- -- Esta mensagem foi verificada pelo sistema de antiv?rus e acredita-se estar livre de perigo. From skip at pobox.com Mon Jul 20 14:26:56 2009 From: skip at pobox.com (skip at pobox.com) Date: Mon, 20 Jul 2009 13:26:56 -0500 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a645b81$0$12974$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <19044.46832.524925.930667@montanaro.dyndns.org> William> c 5s William> gcj 7s William> java 7s William> shedskin 8s William> python + psyco 18s William> cython avec malloc *int 18s William> cython 55s avec [] python William> python 303s (5m3s) I read just enough French to know that "avec" means "with", but I don't understand the difference between "avec malloc *int" and "avec []". Can you explain please? Thx, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire From vippstar at gmail.com Mon Jul 20 14:31:08 2009 From: vippstar at gmail.com (vippstar) Date: Mon, 20 Jul 2009 11:31:08 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> <7xljmjguxb.fsf@ruckus.brouhaha.com> Message-ID: <7a04b07d-2dea-455f-bafd-33461ce25a1a@q11g2000yqi.googlegroups.com> On Jul 20, 7:50?pm, Paul Rubin wrote: > vippstar writes: > > > I wonder whether 2/3 => ValueError is preferable. > > > Not all software wants this. It shouldn't be part of the language but > > rather part of your code if you need such a feature. (for instance, to > > distinguish between 2/3 and divisions with 0 dividend). > > I don't see how to implement such a thing in my code, Write a function: (if (< x y) ValueError (/ x y)) This will return 0 only if the dividend = 0, not in integer division x/ y with y > x, which will return ValueError. Of course, ValueError must not be an integer, because that could be the result of an integer division. If it's not possible to return multiple types, then the function can make use of some error handling mechanism. > if I believe that the ring of integers doesn't have any concept > of division and so attempts to divide integers should be treated > as errors. Wouldn't that mean 3/2 would also evaluate to ValueError? But 3/2 = 1 in integer division, not 0, like 2/3. Regardless, it's a specialized requirement, and thus should either be implemented by the programmer or the language could provide it if it's specialized, (for instance, I wouldn't require a language to provide text manipulation features, but I expect them from perl because it's not a general all purpose language [the name designates that, however it can be used as one - like lisp]) > course the present / operator is useful, but I could do just as well > with the divmod function which I think is more explicit. What? Python? Aww. From bearophileHUGS at lycos.com Mon Jul 20 14:51:18 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Mon, 20 Jul 2009 11:51:18 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <04f03d96-46b7-4761-8498-9149f19ecba6@k6g2000yqn.googlegroups.com> Skip Montanaro: > I read just enough French to know that "avec" means "with", but I don't > understand the difference between "avec malloc *int" and "avec []". ?Can you > explain please? Maybe it's the time difference between using a Python list from Cython and using a C "array" allocated with a malloc from Cython. Bye, bearophile From mark.dufour at gmail.com Mon Jul 20 15:20:55 2009 From: mark.dufour at gmail.com (srepmub) Date: Mon, 20 Jul 2009 12:20:55 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> Message-ID: > Nice timings, can you please show me the Python, Java and C code > versions? I may do more tests. also, which shedskin options did you use? did you use -bw, to disable bounds and wrap-around checking? this can make quite a difference for code that does a lot of indexing. if the code uses random numbers, then -r can also make a big difference, to use C rand(), instead of Python compatible random numbers. and which C++ compiler flags did you use? the default -O2, or something like -O3 -fomit-frame-pointer -msse2..? thanks, mark. From phillip.oldham at gmail.com Mon Jul 20 15:26:07 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Mon, 20 Jul 2009 12:26:07 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> On Jul 20, 6:08?pm, Duncan Booth wrote: > The main reason why you need both lists and tuples is that because a tuple > of immutable objects is itself immutable you can use it as a dictionary > key. Really? That sounds interesting, although I can't think of any real- world cases where you'd use something like that. From duncan.booth at invalid.invalid Mon Jul 20 15:54:02 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 20 Jul 2009 19:54:02 GMT Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: Phillip B Oldham wrote: > On Jul 20, 6:08?pm, Duncan Booth wrote: >> The main reason why you need both lists and tuples is that because a >> tuple of immutable objects is itself immutable you can use it as a >> dictionary key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. > How about a multi-dimensional sparse array? >>> d = {} >>> d[1,2] = 'a' >>> d[5,7] = 'b' From marcusw at cox.net Mon Jul 20 16:12:45 2009 From: marcusw at cox.net (Marcus Wanner) Date: Mon, 20 Jul 2009 16:12:45 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: On 7/20/2009 3:26 PM, Phillip B Oldham wrote: > On Jul 20, 6:08 pm, Duncan Booth wrote: >> The main reason why you need both lists and tuples is that because a tuple >> of immutable objects is itself immutable you can use it as a dictionary >> key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. Actually, that would be very useful in the program from "any suggestions to synchronize typed text and speech ?"...i.e. have a dictionary key of (hour, minute, second). Marcus From jcd at sdf.lonestar.org Mon Jul 20 16:15:16 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Mon, 20 Jul 2009 16:15:16 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: <1248120917.5967.5.camel@aalcdl07> On Mon, 2009-07-20 at 12:26 -0700, Phillip B Oldham wrote: > On Jul 20, 6:08 pm, Duncan Booth wrote: > > The main reason why you need both lists and tuples is that because a tuple > > of immutable objects is itself immutable you can use it as a dictionary > > key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. Well, if you wanted to index a dictionary by coordinates, you might do something like this: fleet = {} fleet[9,4] = 'destroyer' fleet[8,4] = 'destroyer' fleet[3,5] = 'aircraftcarrier' fleet[4,5] = 'aircraftcarrier' fleet[5,5] = 'aircraftcarrier' fleet[6,5] = 'aircraftcarrier' fleet[8,0] = 'battleship' fleet[8,1] = 'battleship' fleet[8,2] = 'battleship' def checkattack(x, y, fleet): if x,y in fleet: return "You hit my %s' % fleet[x,y] Maybe not the best implementation of Battleship, but you get the idea. From mrstevegross at gmail.com Mon Jul 20 16:38:57 2009 From: mrstevegross at gmail.com (mrstevegross) Date: Mon, 20 Jul 2009 13:38:57 -0700 (PDT) Subject: How to import pydoc and then use it? Message-ID: I know how to use pydoc from the command line. However, because of complicated environmental setup, it would be preferable to run it within a python script as a native API call. That is, my python runner looks a bit like this: import pydoc pydoc.generate_html_docs_for(someFile) However, it's not clear to me from the pydoc documentation which function calls I need to use to make this behavior work. Any ideas? Thanks, --Steve From nytrokiss at gmail.com Mon Jul 20 17:01:13 2009 From: nytrokiss at gmail.com (James Matthews) Date: Tue, 21 Jul 2009 00:01:13 +0300 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> Message-ID: <8a6b8e350907201401k383b0982ma7aeaaf8bf7d7f8a@mail.gmail.com> I like this, I am going to run this as a test. I also want to see the source code on how they compile the dynamic variables. On Mon, Jul 20, 2009 at 10:20 PM, srepmub wrote: > > > Nice timings, can you please show me the Python, Java and C code > > versions? I may do more tests. > > also, which shedskin options did you use? did you use -bw, to disable > bounds and wrap-around checking? this can make quite a difference for > code that does a lot of indexing. if the code uses random numbers, > then -r can also make a big difference, to use C rand(), instead of > Python compatible random numbers. > > and which C++ compiler flags did you use? the default -O2, or > something like -O3 -fomit-frame-pointer -msse2..? > > > thanks, > mark. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.jewelerslounge.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From egon.frerich at nord-com.net Mon Jul 20 17:07:38 2009 From: egon.frerich at nord-com.net (Egon Frerich) Date: Mon, 20 Jul 2009 23:07:38 +0200 Subject: locale doesn' format Message-ID: <4A64DC9A.3060606@nord-com.net> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I want to format values to the german form eg. 1.034,56 but locale.format() doesn't work for me. Here is a little test program: import decimal, locale print locale.getdefaultlocale() print locale.localeconv() locale.setlocale(locale.LC_ALL, ('de_DE', 'UTF-8')) print locale.localeconv() k = decimal.Decimal('1034.56') #k = 1034.56 print 'k', k, type(k) k1 = locale.format_string('%12s',k,True) print 'k1', k1, type(k1) k1a = locale.format('%12s',k,True,True) print 'k1a', k1a, type(k1a) k2 = unicode(k1) print 'k2',k2,type(k2) print k1.find(',') print k1a.find(',') print k2.find(',') ab = locale.localeconv()['decimal_point'] print 'ab', ab print locale.localeconv()['decimal_point'].find(',') print locale.localeconv()['mon_decimal_point'].find(',') ac = locale.localeconv()['mon_decimal_point'] print 'ac', ac, len(ac) print locale.localeconv() k1, k1a, k2 are printed as 1034.56 Egon -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFKZNyaZRiDo9Iq4qIRAuqhAKCW3x7iDbrxUDp3M/qHabwbw92osQCgi6N2 mmgb54LACmrd/Wi4BRi2iZo= =psal -----END PGP SIGNATURE----- From piet at cs.uu.nl Mon Jul 20 17:23:25 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 20 Jul 2009 23:23:25 +0200 Subject: Help understanding the decisions *behind* python? References: Message-ID: >>>>> Duncan Booth (DB) wrote: >DB> Phillip B Oldham wrote: >>> This make a lot more sense to us, and follows the convention from >>> other languages. It would also mean chaining methods to manipulate >>> lists would be easier: >>> >>>>>> x = [2,1,3] >>>>>> print x.sort()[0] >>> 3 >>>>>> print x >>> [2,1,3] >DB> You already have a way to do what you want: >>>>> x = [2,1,3] >>>>> print sorted(x)[0] >DB> 3 What kind of Python produces that? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From clp2 at rebertia.com Mon Jul 20 17:30:16 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 20 Jul 2009 14:30:16 -0700 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: Message-ID: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum wrote: >>>>>> Duncan Booth (DB) wrote: > >>DB> Phillip B Oldham wrote: >>>> This make a lot more sense to us, and follows the convention from >>>> other languages. It would also mean chaining methods to manipulate >>>> lists would be easier: >>>> >>>>>>> x = [2,1,3] >>>>>>> print x.sort()[0] >>>> 3 >>>>>>> print x >>>> [2,1,3] > >>DB> You already have a way to do what you want: > >>>>>> x = [2,1,3] >>>>>> print sorted(x)[0] >>DB> 3 > > What kind of Python produces that? Assuming you're referring to the latter example, it was added in version 2.4 If you meant the former example, I think that's purely pseudo-Python. Cheers, Chris -- http://blog.rebertia.com From gagsl-py2 at yahoo.com.ar Mon Jul 20 17:38:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 18:38:22 -0300 Subject: comments? storing a function in an object References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: En Mon, 20 Jul 2009 14:44:16 -0300, Francesco Bochicchio escribi?: > On Jul 20, 6:22?pm, Esmail wrote: >> Hello all, >> >> I am trying to store a function and some associated information in an >> object so that I can later have series of functions in a list that I can >> evaluate one at a time. >> >> Right now I am only storing the function itself, the number of >> arguments it expects and its string representation. I may add other >> information such as the acceptable range for the parameters or other >> characteristics such as maximum or minimum. >> >> I wonder if anyone could comment on my implementation and offer >> suggestions or constructive criticisms? >> >> The 'loading' of the functions is a bit tedious and of course care >> has to be taken to make sure the string representation corresponds to >> the actual function computed. It would be nice if there was an >> automatic way to convert the function to its string representation. >> >> Comments or problems with the approach I have taken? >> >> Thanks, >> Esmail >> >> -- >> >> #!/usr/bin/env python >> >> # >> # store and represent functions >> # >> >> import math >> >> class FunctionException(Exception): >> ? ? ?""" custom exception """ >> ? ? ?def __init__(self, value): >> ? ? ? ? ?self.parameter = value >> >> ? ? ?def __str__(self): >> ? ? ? ? ?return repr(self.parameter) >> >> class Function(object): >> ? ? ?""" class to represent a function """ >> >> ? ? ?def __init__(self, fn, fn_str, num_vars): >> ? ? ? ? ?""" >> ? ? ? ? ?the function, its string representation, and the number of >> variables >> ? ? ? ? ?""" >> ? ? ? ? ?self.fn = fn >> ? ? ? ? ?self.fn_str = fn_str >> ? ? ? ? ?self.num_vars = num_vars >> >> ? ? ?def eval_fn(self, variables): >> ? ? ? ? ?""" size of variables should equal num_vars .. else problem """ >> ? ? ? ? ?if len(variables) == self.num_vars: >> ? ? ? ? ? ? ?result = self.fn(*variables) >> ? ? ? ? ? ? ?return result >> ? ? ? ? ?else: >> ? ? ? ? ? ? ?raise FunctionException('invalid number of args provided - >> '+ >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'received %d, expected %d' >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?%(len(variables), self.num_vars)) >> >> ? ? ?def str(self): >> ? ? ? ? ?""" return string representation of function """ >> ? ? ? ? ?return self.fn_str >> >> def funct1(x): >> ? ? ?''' small test function ''' >> ? ? ?return x * x >> >> def funct2(x, y): >> ? ? ?''' small test function ''' >> ? ? ?return x + y >> >> def funct3(x): >> ? ? ?''' small test function ''' >> ? ? ?return 1000 + (x*x + x) * math.cos(x) >> >> def main(): >> ? ? ?""" main method """ >> ? ? ?print 'in main' >> >> ? ? ?fn1 = Function(funct1, 'x * x', 1) >> ? ? ?fn2 = Function(funct2, 'x + y', 2) >> ? ? ?fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) >> >> ? ? ?for i in range(-10, 10): >> ? ? ? ? ?try: >> >> ? ? ? ? ? ? ?print 'f(', [i],') =', >> ? ? ? ? ? ? ?print fn3.str(), ' => ', >> ? ? ? ? ? ? ?print fn3.eval_fn([i]) >> >> ? ? ? ? ?except FunctionException, (ex): >> ? ? ? ? ? ? ?print ex.parameter >> >> if __name__ == '__main__': >> ? ? ?main() > > > I can offer some small suggestions: it is up to you to evaluate if > they make sense for your app: > > 1. use __doc__ function standard attribute for function description > (your fn_str); this meanst that > you do not need fm_str in the constructor and you have to do e.g. : > > def funct2(x, y): > ''' x + y ''' > return x + y > > then funct2.__doc__ becomes the string you want to associate to the > function > > 2. Use __call__ instead of eval_fn : this way, the instance of your > Function became a 'callable' and can be used > everywhere a function is needed. You can do: > > f = Function(...) > f( some_args ) > > 3. If you call a python function with wrong number of arguments, it > already raises a TypeError exception which contains > - with different wording - the same information of your > FunctionException : consider removing the check and using the > python error instead If you follow the above suggestions, you'll see that your Function class becomes almost useless: a normal function already IS an object, so you don't have to wrap it inside ANOTHER object unless you need very special features. -- Gabriel Genellina From aahz at pythoncraft.com Mon Jul 20 17:40:05 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 14:40:05 -0700 Subject: any suggestions to synchronize typed text and speech ? References: Message-ID: In article , Stef Mientki wrote: > >I was afraid of that too, so I dropped the question in several places, >and the writer of Scintilla himself came with the perfect answer. ...which was?... -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From gagsl-py2 at yahoo.com.ar Mon Jul 20 17:40:14 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 20 Jul 2009 18:40:14 -0300 Subject: Auto-generate GUI from a database table structure References: <55660F9659966544BBA3AE14CE5D278D01243B8E04@exgtmb08.nam.nsroot.net> Message-ID: En Mon, 20 Jul 2009 12:09:47 -0300, Boyd, Craig1 escribi?: > I am trying to write a couple screens to update three or four database > tables on Oracle 10g and I was wondering if there was a way to > automatically generate some kind of GUI shell based on the tables that I > could then fill in with the logic / functionality I need. I've never actually used it, but the DABO project seems to provide what you want: http://dabodev.com/ -- Gabriel Genellina From marduk at letterboxes.org Mon Jul 20 17:40:19 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Mon, 20 Jul 2009 17:40:19 -0400 Subject: How to import pydoc and then use it? In-Reply-To: References: Message-ID: <1248126024.9281.3.camel@centar> On Mon, 2009-07-20 at 13:38 -0700, mrstevegross wrote: > I know how to use pydoc from the command line. However, because of > complicated environmental setup, it would be preferable to run it > within a python script as a native API call. That is, my python runner > looks a bit like this: > > import pydoc > pydoc.generate_html_docs_for(someFile) > > However, it's not clear to me from the pydoc documentation which > function calls I need to use to make this behavior work. Any ideas? Did you try 'pydoc pydoc'? ;) >>> import pydoc >>> htmldoc = pydoc.HTMLDoc() >>> htmldoc_for_pydoc = htmldoc(pydoc) >>> print htmldoc_for_pydoc From p.f.moore at gmail.com Mon Jul 20 17:40:58 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 20 Jul 2009 22:40:58 +0100 Subject: Help understanding the decisions *behind* python? In-Reply-To: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> References: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> Message-ID: <79990c6b0907201440r5470c486j207440183514ba9@mail.gmail.com> 2009/7/20 Chris Rebert : > On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum wrote: >>>>>>> x = [2,1,3] >>>>>>> print sorted(x)[0] >>>DB> 3 >> >> What kind of Python produces that? > > Assuming you're referring to the latter example, it was added in version 2.4 > If you meant the former example, I think that's purely pseudo-Python. I think he was referring to the fact that the result should be 1, not 3. Paul. From malaclypse2 at gmail.com Mon Jul 20 17:59:08 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Mon, 20 Jul 2009 17:59:08 -0400 Subject: locale doesn' format In-Reply-To: <4A64DC9A.3060606@nord-com.net> References: <4A64DC9A.3060606@nord-com.net> Message-ID: <16651e80907201459w5060ad3ex9706a1f4ec12c@mail.gmail.com> On Mon, Jul 20, 2009 at 5:07 PM, Egon Frerich wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I want to format values to the german form eg. 1.034,56 but > locale.format() doesn't work for me. > > Here is a little test program: ... > k1 = locale.format_string('%12s',k,True) I don't think you want to be using '%s' to format numbers in your format strings. Instead, use '%f' to format floating point values. As I understand it, a %s in a format string actually calls str() on the object that you pass into the formatting function, and I don't think that floats (or Decimal.Decimal objects) take locale into account when you do that. -- Jerry From FSet.SLB at gmail.com Mon Jul 20 18:07:58 2009 From: FSet.SLB at gmail.com (Scott Burson) Date: Mon, 20 Jul 2009 15:07:58 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> On Jul 19, 11:31?am, Frank Buss wrote: > I don't know of a free modern and stable Lisp implementation with > mulithreading support for Windows, with a licence with which you can use it > in closed source commercial programs Have you looked at ECL? http://ecls.sourceforge.net/ I've used it only a little, so I can't vouch for its stability, but it fits the threading and license requirements (well, some corporate lawyers have trouble with the LGPL, but I think it's usable). -- Scott From corbet at lwn.net Mon Jul 20 18:16:59 2009 From: corbet at lwn.net (Jonathan Corbet) Date: Mon, 20 Jul 2009 16:16:59 -0600 Subject: Python Essential Reference, 4th Edition - Now Available! In-Reply-To: References: Message-ID: <20090720161659.78cbf90a@bike.lwn.net> On Sun, 19 Jul 2009 11:03:05 -0500 David Beazley wrote: > I'm pleased to announce the release of the Python Essential Reference, > 4th edition, now available at a bookstore near you. More than a year > in development, this edition covers Python 2.6, Python 3, and a wide > variety of new library modules. And here I am still using my old, dog-eared second edition... Any chance of getting a copy of the 4th for an LWN review? Thanks, jon -- Jonathan Corbet / LWN.net / corbet at lwn.net From hniksic at xemacs.org Mon Jul 20 18:19:36 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 21 Jul 2009 00:19:36 +0200 Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: <87ljmjyp3b.fsf@busola.homelinux.net> Phillip B Oldham writes: > On Jul 20, 6:08?pm, Duncan Booth wrote: >> The main reason why you need both lists and tuples is that because a tuple >> of immutable objects is itself immutable you can use it as a dictionary >> key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. An application visiting files on a filesystem recursively needs a dictionary or set keyed by (st_dev, st_ino) to make sure it doesn't visit the same file twice. Memoization implementation (of a cache for results of function application) might need to use a dictionary to map function arguments, a tuple, to the function result. From hniksic at xemacs.org Mon Jul 20 18:20:53 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 21 Jul 2009 00:20:53 +0200 Subject: Help understanding the decisions *behind* python? References: Message-ID: <87hbx7yp16.fsf@busola.homelinux.net> Chris Rebert writes: >>>>>>> x = [2,1,3] >>>>>>> print sorted(x)[0] >>>DB> 3 >> >> What kind of Python produces that? > > Assuming you're referring to the latter example, it was added in version 2.4 > If you meant the former example, I think that's purely pseudo-Python. sorted([2, 1, 3])[0] evaluates to 1, not 3. From http Mon Jul 20 18:22:20 2009 From: http (Paul Rubin) Date: 20 Jul 2009 15:22:20 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> <7xljmjguxb.fsf@ruckus.brouhaha.com> <7a04b07d-2dea-455f-bafd-33461ce25a1a@q11g2000yqi.googlegroups.com> Message-ID: <7xiqhn0zc3.fsf@ruckus.brouhaha.com> vippstar writes: > > I don't see how to implement such a thing in my code, > Write a function: > > (if (< x y) > ValueError > (/ x y)) I meant changing the behavior of integer division in python. > Wouldn't that mean 3/2 would also evaluate to ValueError? Yes, the idea was that one can take the view that integer division should not be allowed except through a 'div' function or some such. From ethan at stoneleaf.us Mon Jul 20 18:34:28 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 20 Jul 2009 15:34:28 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <24543805.post@talk.nabble.com> References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> <24543805.post@talk.nabble.com> Message-ID: <4A64F0F4.9060201@stoneleaf.us> [fixed for bottom-posting] Dr. Phillip M. Feldman wrote: > MRAB-2 wrote: > >> >> >> >>What values should 'xor' return? IMHO, if only one of the values is true >>then it should return that value, otherwise it should return False. >> >> 1 xor 0 => 1 >> 0 xor 2 => 2 >> 1 xor 2 => False >> 0 xor 0 => False >> >>This is because it's a Boolean operator, so it should fall back to >>Boolean values when necessary, like 'not': >> >> not 0 => True >> not 1 => False >> >>Also: >> >> x and y and z => (x and y) and z >> x or y or z => (x or y) or z >> >>therefore: >> >> x xor y xor z => (x xor y) xor z >> > Suppose that 'xor' returns the value that is true when only one value is > true, and False otherwise. This definition of xor doesn't have the standard > associative property, that is, > > (a xor b) xor c > > will not necessarily equal > > a xor (b xor c) > > To see that this is the case, let a= 1, b= 2, and c= 3. > > (a xor b) xor c > > yields 3, while > > a xor (b xor c) > > yields 1. So, I'd prefer an xor operator that simply returns True or False. > > Phillip > You are, of course, free to write your version however it makes sense to you and your team. :) Since the 'and' and 'or' already return objects (and objects evaluate to true or false), then 'xor' should behave likewise, IMO. I expect that would be the case if it were ever added to the language. ~Ethan~ From martin at v.loewis.de Mon Jul 20 18:39:11 2009 From: martin at v.loewis.de (=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=) Date: Tue, 21 Jul 2009 00:39:11 +0200 Subject: Compilation problem with Python < 2.6 and db >= 4.7 In-Reply-To: References: Message-ID: <4a64f210$0$8379$9b622d9e@news.freenet.de> > Is there a way to make the bsddb module compile against db>=4.7 for python < 2.6 > (2.4.6, 2.5.4)? I don't think so, no. > I didn't find something on it. If you don't want to use pybsddb either, for fear of incompatible API, your only choice is to port _bsddb.c to the newer db versions. BSDDB is in the tradition of both breaking the data format and the API frequently, so we (python-dev) have given up on it. Regards, Martin From niels.ellegaard at gmail.com Mon Jul 20 18:39:24 2009 From: niels.ellegaard at gmail.com (Niels L. Ellegaard) Date: Tue, 21 Jul 2009 00:39:24 +0200 Subject: Help understanding the decisions *behind* python? References: Message-ID: <86skgr0yjn.fsf@gmail.com> Phillip B Oldham writes: > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. If you just want a one-liner, and you don't care about speed you can do the following (but I don't think this is considered best practice) >>> x = [2,1,3] >>> print list(sorted(x)) [1, 2, 3] >>> print x [2, 1, 3] Niels From vippstar at gmail.com Mon Jul 20 18:48:51 2009 From: vippstar at gmail.com (vippstar) Date: Mon, 20 Jul 2009 15:48:51 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> <7xljmjguxb.fsf@ruckus.brouhaha.com> <7a04b07d-2dea-455f-bafd-33461ce25a1a@q11g2000yqi.googlegroups.com> <7xiqhn0zc3.fsf@ruckus.brouhaha.com> Message-ID: On Jul 21, 1:22?am, Paul Rubin wrote: > vippstar writes: > > > I don't see how to implement such a thing in my code, > > Write a function: > > > ? (if (< x y) > > ? ? ? ValueError > > ? ? ? (/ x y)) > > I meant changing the behavior of integer division in python. You'd either have to hack an implementation or change the standard (I just noticed python doesn't have one). > > Wouldn't that mean 3/2 would also evaluate to ValueError? > > Yes, the idea was that one can take the view that integer division > should not be allowed except through a 'div' function or some such. You brought up 3/2 == ValueError as a more appropriate value for the integer division to evaluate, rather than 0. I thought you meant specifically those kinds of divisions. From kiorky at cryptelium.net Mon Jul 20 18:51:42 2009 From: kiorky at cryptelium.net (kiorky) Date: Tue, 21 Jul 2009 00:51:42 +0200 Subject: Compilation problem with Python < 2.6 and db >= 4.7 In-Reply-To: <4a64f210$0$8379$9b622d9e@news.freenet.de> References: <4a64f210$0$8379$9b622d9e@news.freenet.de> Message-ID: <4A64F4FE.805@cryptelium.net> Martin v. L?wis a ?crit : >> Is there a way to make the bsddb module compile against db>=4.7 for python < 2.6 >> (2.4.6, 2.5.4)? > > I don't think so, no. > >> I didn't find something on it. > > If you don't want to use pybsddb either, for fear of incompatible API, > your only choice is to port _bsddb.c to the newer db versions. BSDDB > is in the tradition of both breaking the data format and the API > frequently, so we (python-dev) have given up on it. Yup, i understand. I will be lazy and fall back as all packaging systems do, and keep in the minitage packages tree both db versions. Thanks for confirmation. > > Regards, > Martin -- -- Cordialement, KiOrKY GPG Key FingerPrint: 0x1A1194B7681112AF From lists at cheimes.de Mon Jul 20 18:56:12 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 21 Jul 2009 00:56:12 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <86skgr0yjn.fsf@gmail.com> References: <86skgr0yjn.fsf@gmail.com> Message-ID: Niels L. Ellegaard wrote: > Phillip B Oldham writes: > >> We often find we need to do manipulations like the above without >> changing the order of the original list, and languages like JS allow >> this. We can't work out how to do this in python though, other than >> duplicating the list, sorting, reversing, then discarding. > > If you just want a one-liner, and you don't care about speed you can > do the following (but I don't think this is considered best practice) sorted() already returns a new list not a generator as you might assume. Christian From piet at cs.uu.nl Mon Jul 20 19:41:16 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 01:41:16 +0200 Subject: Help understanding the decisions *behind* python? References: <50697b2c0907201430x5fd6cb0ak6cbc75babcc293fe@mail.gmail.com> Message-ID: >>>>> Paul Moore (PM) wrote: >PM> 2009/7/20 Chris Rebert : >>> On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum wrote: >>>>>>>>> x = [2,1,3] >>>>>>>>> print sorted(x)[0] >DB> 3 >>>> >>>> What kind of Python produces that? >>> >>> Assuming you're referring to the latter example, it was added in version 2.4 >>> If you meant the former example, I think that's purely pseudo-Python. >PM> I think he was referring to the fact that the result should be 1, not 3. The underlying thought was that you should copy and paste examples from a real Python interpreter. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Mon Jul 20 20:10:55 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 17:10:55 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: In article , D'Arcy J.M. Cain wrote: >On 20 Jul 2009 09:00:33 -0700 >aahz at pythoncraft.com (Aahz) wrote: >> >> Out of curiosity, are there any JavaScript toolkits that generate code >> that degrades gracefully when JavaScript is disabled? > >I understand what you want but I can't see how a toolkit can do that. >How do you program "graceful?" It seems pretty application specific. Presumably the JS toolkit generates both code and HTML. Actions that normally get executed by JS should degrade to plain HTML links and form submits (or possibly not display at all if it's a pure convenience). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From gdamjan at gmail.com Mon Jul 20 20:33:39 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Tue, 21 Jul 2009 02:33:39 +0200 Subject: locale doesn' format References: Message-ID: <30ffj6-qd6.ln1@archaeopteryx.softver.org.mk> > I want to format values to the german form eg. 1.034,56 but > locale.format() doesn't work for me. seems to work here >>> import decimal, locale >>> locale.setlocale(locale.LC_ALL, 'mk_MK.UTF-8') 'mk_MK.UTF-8' >>> locale.localeconv()['grouping'] [3, 3, 0] >>> locale.localeconv()['thousands_sep'] ' ' >>> locale.localeconv()['decimal_point'] ',' >>> locale.format('%f', 1034.56,True) '1 034,560000' >>> locale.format('%d', 1034.56,True) '1 034' ps. you can copy/paste the above in .txt file and then run/test it with: python -m doctest -v check.txt -- ?????? ( http://softver.org.mk/damjan/ ) Begin...the rest is easy. From gdamjan at gmail.com Mon Jul 20 20:44:38 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Tue, 21 Jul 2009 02:44:38 +0200 Subject: locale doesn' format References: <30ffj6-qd6.ln1@archaeopteryx.softver.org.mk> Message-ID: >> I want to format values to the german form eg. 1.034,56 but >> locale.format() doesn't work for me. > > seems to work here In 2.6 this is good too: >>> import decimal, locale >>> locale.setlocale(locale.LC_ALL, 'mk_MK.UTF-8') >>> '{0:n}'.format(1234.56) '1 234,56' -- ?????? ( http://softver.org.mk/damjan/ ) A: Because it reverses the logical flow of conversation. Q: Why is top posting frowned upon? From davea at ieee.org Mon Jul 20 20:47:56 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 20 Jul 2009 20:47:56 -0400 Subject: Design question. In-Reply-To: <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> References: <4dce44e2-3911-4313-8cf1-2f14634426eb@k30g2000yqf.googlegroups.com> <68149452-d139-4866-bf1b-5e1c49fdd479@w41g2000yqb.googlegroups.com> Message-ID: <4A65103C.4010401@ieee.org> Lacrima wrote: > On Jul 20, 4:05 pm, Jean-Michel Pichavant > wrote: > >> Lacrima wrote: >> >>> Hello! >>> >>> I am newbie in python and I have really simple question, but I need >>> your advice to know how to do best. >>> I need to store a number of dictionaries in certain place. I've >>> decided to store them in a separate module. >>> Like this: >>> dicts.py >>> ----------------------- >>> dict1 =....} >>> dict2 =....} >>> dict3 =....} >>> >>> Then, when I need any dictionary, I can access it: >>> import dicts >>> dicts.dict1 >>> >> > Hi, Jean-Michel! > > Thanks for your answer. > I am not going to have names like dicts.py and dict1,dict2. That was > just example. I wanted to know if it is correct to have dictionaries > on a module level. Now I know that this is correct. I want to collect > in one module a number of dictionaries, every of which describes a > separate web service. And my function in another module will import > required dictionary, depending on what web service should be used. > Thanks again for the help. > > With regards, > Max. > (sorry if my English isn't very proper) > > It makes a lot of sense to store several dictionaries in a module, if they describe parallel things, or alternate ways of accessing something (like web services). However, you might want to consider how they'll be accessed. If another module is going to "import required dictionary," then you have some means of selection. Perhaps that selection should be in the same module, or at least the data structure to select it should be in the same module. Let's say you have four choices right now. And you add a fifth later. Do you want to have to edit both the "function in the other module" as well as this module? This is similar to a feature that some programs have, "plug-ins." There a customer can add an additional runtime just by dropping a new .py file in the appropriate directory, and the program will find it and add it to its "list of features." So I'm just suggesting you figure out how the program might evolve, and set up the data so changes may be localized, if practical. This turns out usually to be parallel to the notion of naming things and grouping them by similarity of functionality. DaveA From sajmikins at gmail.com Mon Jul 20 20:56:26 2009 From: sajmikins at gmail.com (Simon Forman) Date: Mon, 20 Jul 2009 17:56:26 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7xljmk8q1m.fsf@ruckus.brouhaha.com> Message-ID: <6348dcb8-bfc1-446e-b7bf-28657e2b607e@j32g2000yqh.googlegroups.com> On Jul 19, 2:51 pm, Paul Rubin wrote: > Calroc writes: > > I'm engaged presently in starting a school to teach programming from > > the ground up, based roughly on the curriculum outlined in the article > > I mentioned. ... > > I'm excited about formal methods because one, I just heard about them, > > Formal methods are a big and complicated subject. Right after you > heard about them is probably not the best time to start teaching them. > Better get to understand them yourself first. Very good point. But I'm glad it's there to study, these are wheels I don't have to invent for myself. ~Simon From sajmikins at gmail.com Mon Jul 20 20:57:05 2009 From: sajmikins at gmail.com (Simon Forman) Date: Mon, 20 Jul 2009 17:57:05 -0700 (PDT) Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7cimf0F27u23eU1@mid.individual.net> Message-ID: <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> On Jul 20, 4:00 am, greg wrote: > Calroc wrote: > > It may be that flawless software is an unreachable asymptote, like the > > speed of light for matter, but I'm (recently!) convinced that it's a > > goal worthy of exploration and effort. > > Seems to me that once you get beyond the toy program > stage and try to apply correctness proving to real > world programming problems, you run up against the > problem of what exactly you mean by "correct". > > Once the requirements get beyond a certain level of > complexity, deciding whether the specifications > themselves are correct becomes just as difficult as > deciding whether the program meets them. I agree. You could prove that a game renders geometry without crashing, but not that it's fun. Or in an accounting package you could prove that it never loses a cent in tracking accounts and payments, or that its graph-rendering code does correctly render data (without introducing artifacts, for instance), but not that it will let you notice trends. > Then there's the fact that very often we don't > even know what the exact requirements are, and it's > only by trying to write and use the program that > we discover what they really are -- or at least, > get a better idea of what they are, because the > process is usually iterative, with no clear end > point. Aye, if you're still exploring your solution space your requirements are perforce somewhat nebulous. > So in theory, correctness proofs are a fine idea, > and might even be doble on a small scale, but the > real world is huge and messy! Very true, but I think it is still worthy to build larger systems from proven components combined in provably correct ways, at least to the extent possible. > > Just because it's unattainable doesn't make it undesirable. And who > > knows? Maybe the horse will learn to sing. > > Striving to find ways of writing less bugs is a > worthy goal, but I think of it more in terms of > adopting patterns of thought and programming that > make it more likely you will write code that does > what you had in mind, rather than a separate > "proof" process that you go through afterwards. I would consider formal methods exactly "patterns of thought and programming that make it more likely you will write code that does what you had in mind", in fact that's why I'm excited about them. My understanding (so far) is that you (hope to) /derive/ correct code using formal logic, rather than writing code and then proving its soundness. The real win, IMO, is teaching computers as a logical, mathematical activity (that can certainly depart for realms less rigorously defined.) I think anyone who can spontaneously solve a Sudoku puzzle has enough native grasp of "formal" methods of reasoning to learn how to derive working programs logically. Making the formal methods explicit gives you the strongest available confidence that you are reasoning, and programming, correctly. This is not to say that "incorrect" (or unproven or unprovable) programs are never useful or necessary. However, I do feel that it's better to learn the "correct" way, and then introduce ambiguity, than to simply throw e.g. Java at some poor student and let them find solid ground "catch as catch can". ~Simon From darcy at druid.net Mon Jul 20 21:18:28 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Mon, 20 Jul 2009 21:18:28 -0400 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) In-Reply-To: References: Message-ID: <20090720211828.88876711.darcy@druid.net> On 20 Jul 2009 17:10:55 -0700 aahz at pythoncraft.com (Aahz) wrote: > >I understand what you want but I can't see how a toolkit can do that. > >How do you program "graceful?" It seems pretty application specific. > > Presumably the JS toolkit generates both code and HTML. Actions that > normally get executed by JS should degrade to plain HTML links and form > submits (or possibly not display at all if it's a pure convenience). As I said, I do understand what you are after but I still think it is a judgement call. What defines "convenience?" How does the library know what is essential and what is pretty? -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From ebonak at hotmail.com Mon Jul 20 21:53:59 2009 From: ebonak at hotmail.com (Esmail) Date: Mon, 20 Jul 2009 21:53:59 -0400 Subject: comments? storing a function in an object In-Reply-To: References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: <4A651FB7.1030602@hotmail.com> Gabriel Genellina wrote: > > If you follow the above suggestions, you'll see that your Function class > becomes almost useless: a normal function already IS an object, so you > don't have to wrap it inside ANOTHER object unless you need very special > features. Hello Gabriel, In general I would agree with you, but in my specific case I want so store some additional meta-data with each function, such as the valid range for input values, where the max or minimum are located, the name/source for the function etc. I am creating list of functions for use in testing optimization code, so it seems best to store this data along with the function I am going to optimize in order to verify the results for a given range (for instance). Esmail From http Mon Jul 20 21:54:54 2009 From: http (Paul Rubin) Date: 20 Jul 2009 18:54:54 -0700 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7xljmk8q1m.fsf@ruckus.brouhaha.com> <6348dcb8-bfc1-446e-b7bf-28657e2b607e@j32g2000yqh.googlegroups.com> Message-ID: <7xws627qc1.fsf@ruckus.brouhaha.com> Simon Forman writes: > But I'm glad it's there to study, these are wheels I don't have to > invent for myself. http://dwheeler.com/essays/high-assurance-floss.html might be an interesting place to start. From ebonak at hotmail.com Mon Jul 20 22:01:45 2009 From: ebonak at hotmail.com (Esmail) Date: Mon, 20 Jul 2009 22:01:45 -0400 Subject: comments? storing a function in an object In-Reply-To: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: <4A652189.2000607@hotmail.com> Hi Francesco, Those were great suggestions! Re 1: I used the __doc__ attribute to eliminate the parameter in the constructor as you suggested. Also, much easier to specify the character string with the actual function than later to match it up like I was. class Function(object): """ class to represent a function """ def __init__(self, fn, num_vars): """ the function, its string representation, and the number of variables """ self.fn = fn self.fn_str = fn.__doc__ self.num_vars = num_vars Re 2: I will need to read up on __call__ to see how to use it here .. Re 3: I think I still need to have my own exception handler as I am using the 'extended call syntax' now (I just learned about this yesterday) and I will always just supply one argument, the list. Or perhaps my custom exception will be superfluous once I figure out __call__ ..? Thank you for taking the time to help, always good to learn new things. Esmail From aahz at pythoncraft.com Mon Jul 20 22:53:08 2009 From: aahz at pythoncraft.com (Aahz) Date: 20 Jul 2009 19:53:08 -0700 Subject: are user defined classes hashable? References: <373d6c69-6965-4a88-bdd2-8028ef850bf8@k6g2000yqn.googlegroups.com> Message-ID: In article <373d6c69-6965-4a88-bdd2-8028ef850bf8 at k6g2000yqn.googlegroups.com>, Hyuga wrote: > >Regardless, Nicolas's example can be applied to the class too: > >>>> class Foo(object): > pass > >>>> hash(Foo) >11443104 >>>> id(Foo) >11443104 > >class objects are just objects of type 'type'. Not quite. They certainly default that way, but changing the metaclass changes a class's type:: class M(type): pass class C(object): pass class C2(object): __metaclass__ = M print type(C) print type(C2) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From pavlovevidence at gmail.com Mon Jul 20 23:53:53 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 20 Jul 2009 20:53:53 -0700 (PDT) Subject: comments? storing a function in an object References: Message-ID: On Jul 20, 9:22?am, Esmail wrote: > def funct1(x): > ? ? ?''' small test function ''' > ? ? ?return x * x > > def funct2(x, y): > ? ? ?''' small test function ''' > ? ? ?return x + y > > def funct3(x): > ? ? ?''' small test function ''' > ? ? ?return 1000 + (x*x + x) * math.cos(x) > > def main(): > ? ? ?""" main method """ > ? ? ?print 'in main' > > ? ? ?fn1 = Function(funct1, 'x * x', 1) > ? ? ?fn2 = Function(funct2, 'x + y', 2) > ? ? ?fn3 = Function(funct3, '1000 + (x*x + x) * cos(x)', 1) If you are defining all of the functions are in-line like this (and I assume you are because you seem to need a function object), I'd just exec the string representation. This is to support the DRY principle. You could do something like this: class Function(object): def __init__(self,args,body): ns = {} exec '''def func(%s): return %s''' in ns self.fn = ns['func'] self.fn_str = body self.num_vars = args.count(',')+1 You have to be REALLY REALLY careful not to pass any user-supplied data to it if this is a server running on your computer, of course. (If it's an application running on the user's computer it doesn't matter.) Still wouldn't be a bad idea to pass it through some kind of validator for extra protection. Carl Banks From boyd.blackwell at gmail.com Mon Jul 20 23:56:51 2009 From: boyd.blackwell at gmail.com (bdb112) Date: Mon, 20 Jul 2009 20:56:51 -0700 (PDT) Subject: clean way prepend an element to a numpy array Message-ID: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> If I want to add an element at the beginning of an array, it seems like I must make a list, insert in place, then make an array again. Of course, lists can be efficient too, and the insert() funtion works nicely in that case, but sometimes arrays are the best choice e.g. x=array([1,2,3]) # to put the element 0 at the head of the array listx=list(x) listx.insert(0,0) x=array(listx) # this extra effort distracts from the clarity of the code, and must slow things a little. # While I appreciate that array operations that cause memory re- allocation and copying are to be # avoided if at all possible, sometimes this is the best compromise between clarity and efficiency # A nicer piece of code would be x.insert(0,0) #or x.prepend(0) # It is a little easier to grow an array at the end (if it is not referenced) x.resize(4) I saw this interest syntax on this site x[:0]=0 I guess that is cute, but could be confusing....(and doesn't work) From ben+python at benfinney.id.au Tue Jul 21 00:13:55 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 21 Jul 2009 14:13:55 +1000 Subject: clean way prepend an element to a numpy array References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> Message-ID: <87eisaad18.fsf@benfinney.id.au> bdb112 writes: > If I want to add an element at the beginning of an array, it seems > like I must make a list, insert in place, then make an array again. The NumPy ?ndarray? type (which is what you get by default from the ?array? factory function) is a far more complex type than (and is not derived from) the Python list. For manipulating them, you'll want to study the NumPy documentation . -- \ ?Are you pondering what I'm pondering?? ?I think so, Brain, but | `\ I don't think Kay Ballard's in the union.? ?_Pinky and The | _o__) Brain_ | Ben Finney From gagsl-py2 at yahoo.com.ar Tue Jul 21 00:31:04 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 01:31:04 -0300 Subject: comments? storing a function in an object References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> <4A651FB7.1030602@hotmail.com> Message-ID: En Mon, 20 Jul 2009 22:53:59 -0300, Esmail escribi?: > Gabriel Genellina wrote: > > >> If you follow the above suggestions, you'll see that your Function >> class becomes almost useless: a normal function already IS an object, >> so you don't have to wrap it inside ANOTHER object unless you need very >> special features. > > Hello Gabriel, > > In general I would agree with you, but in my specific case > I want so store some additional meta-data with each function, such > as the valid range for input values, where the max or minimum are > located, > the name/source for the function etc. I am creating list of functions > for use in testing optimization code, so it seems best to store this > data along with the function I am going to optimize in order to verify > the results for a given range (for instance). You can store all meta-data in the function itself: py> def triangle(b, h): ... "area of triangle: b*h/2" ... return b*h/2 ... py> triangle.foo = "some value" py> triangle.foo 'some value' Python already knows some properties: py> triangle.func_code.co_argcount 2 py> triangle.func_doc 'area of triangle: b*h/2' py> triangle.__doc__ 'area of triangle: b*h/2' You may use this variant of Carl Banks proposal - this is a factory function that returns new function objects: def inline_function_factory(name, args, expr): ns = {} exec '''def %s(%s): %r return %s''' % ( name, args, expr, expr) in ns function = ns[name] return function py> sqr = inline_function_factory("square", "x", "x*x") py> sqr(3) 9 py> sqr.__doc__ 'x*x' py> sqr Wrapping a function object isn't necesarily bad, but perhaps you're doing things more complicated than should be. -- Gabriel Genellina From smolds at orcasoul.com Tue Jul 21 00:33:25 2009 From: smolds at orcasoul.com (OrcaSoul) Date: Mon, 20 Jul 2009 21:33:25 -0700 (PDT) Subject: Mechanize not recognized by py2exe In-Reply-To: References: <4A60C81F.7080707@comcast.net> Message-ID: <24581545.post@talk.nabble.com> Gabriel Genellina-7 wrote: > > En Fri, 17 Jul 2009 15:51:11 -0300, Stephen M. Olds > escribi?: > >> I have a Python script getData.py that uses Mechanize, and runs fine >> under the >> interpreter. It was installed using easy_install - and the install >> seemed to >> indicate it was completed. >> >> The problem is, when I try to compile it using py2exe while in the >> folder of the >> script, and using the run line command: >> >> python getData.py py2exe >> >> I get the warning: "Import error: No Module named mechanize"... >> >> I checked the environmental path and find the following: >> %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program >> Files (x86)\Python;C:\Python25\Lib\site-packages;C:\Program >> Files (x86)\Common Files\Adobe\AGL > > The system PATH is irrelevant here - it's only used by Windows to locate > the Python executable. I don't see a reason to list > C:\Python25\Lib\site-packages here. > You're mostly interested in sys.path instead (that is, the 'path' > attribute in the 'sys' Python module) > >> I did a search for mechanize and find an egg file in >> C:\Python25\Lib\site-packages >> named mechanize-0.1.7b-py2.5. >> >> Not really understanding the "egg" thing, what do I have here that needs >> to be >> done? > > I'm not an egg expert either, and I try to avoid them as a plague. But I > *think* you can delete the .egg and re-install it using easy_install with > the -Z option. > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > > Gabriel, that was the kick in the pants I needed! Thanks - after over a week searching through the same 15 web pages these old eyes simply blurred over and I missed that one...I had tried the -a option, but that's what got me the eggs... So, thanks for the help...it's too late to name my first born after you, but I did have a cat named Gabriel - she was a great cat! Stephen -- View this message in context: http://www.nabble.com/Mechanize-not-recognized-by-py2exe-tp24540012p24581545.html Sent from the Python - python-list mailing list archive at Nabble.com. From fb at frank-buss.de Tue Jul 21 00:57:39 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 06:57:39 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> Message-ID: <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Scott Burson wrote: > Have you looked at ECL? > > http://ecls.sourceforge.net/ > > I've used it only a little, so I can't vouch for its stability, but it > fits the threading and license requirements (well, some corporate > lawyers have trouble with the LGPL, but I think it's usable). I didn't tried it myself, but looks like it is not very stable: http://www.mail-archive.com/application-builder at lispniks.com/msg01069.html -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From astan.chee at al.com.au Tue Jul 21 01:02:57 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 21 Jul 2009 15:02:57 +1000 Subject: ignore special characters in python regex Message-ID: <4A654C01.2000508@al.com.au> Hi, I'm reading text from a file (per line) and I want to do a regex using these lines but I want the regex to ignore any special characters and treat them like normal strings. Is there a regex function that can do this? Here is what I have so far: fp = open('file.txt','r') notes = fp.readlines() fp.close() strin = "this is what I want" for note in notes: if re.search(r""+ str(note) + "",strin): print "Found " + str(note) + " in " + strin Thanks for any help From fb at frank-buss.de Tue Jul 21 01:08:41 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 07:08:41 +0200 Subject: any suggestions to synchronize typed text and speech ? References: Message-ID: Stef Mientki wrote: > Here the problem part: > I need to synchronize the typed text with the sound during playback. > So if I click somewhere in the sound recording, > the line of text, typed that moment should be highlighted. > And vise versa, if the cursor in the text is moved and some special key > is pressed, > the sound starting 10 or 20seconds earlier should be playbacked. You could use some screen recording tools, e.g. http://www.techsmith.com/camtasia.asp on Windows. Most tools allows you to record speech while recording the screen. -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From doomster at knuut.de Tue Jul 21 01:16:10 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Tue, 21 Jul 2009 07:16:10 +0200 Subject: C-API, tp_dictoffset vs tp_members Message-ID: <7cl194F28ojoqU1@mid.uni-berlin.de> Hi! When would I use PyObject_SetAttrString/tp_dictoffset instead of tp_members? I have a predefined set of members, some of which are optional. The problem I had with an embedded dictionary was that I can't see its elements using "dir()". Now I just converted to using tp_members, and it still seems to work correctly for the cases I tested. Even better, I can declare the fields as read-only then and add doc-strings. So, I wonder, what made the original author[2] use tp_dictoffset instead? In case you wonder, I'm looking at the Python bindings for GDB[1], file gdb/python/python-type.c, struct field_object. Thanks! Uli [1] http://sourceware.org/gdb/wiki/PythonGdb [2] Yes I could ask them, but I'd like to get an understanding of the pros and cons. From fb at frank-buss.de Tue Jul 21 01:18:01 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 07:18:01 +0200 Subject: ignore special characters in python regex References: Message-ID: <13459wwzhbzoo$.jei131n3o527.dlg@40tude.net> Astan Chee wrote: > I'm reading text from a file (per line) and I want to do a regex using > these lines but I want the regex to ignore any special characters and > treat them like normal strings. > Is there a regex function that can do this? Maybe re.escape helps? -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From boyd.blackwell at gmail.com Tue Jul 21 01:41:13 2009 From: boyd.blackwell at gmail.com (bdb112) Date: Mon, 20 Jul 2009 22:41:13 -0700 (PDT) Subject: clean way prepend an element to a numpy array References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <87eisaad18.fsf@benfinney.id.au> Message-ID: <608f857f-3925-44db-9e5e-8969b6a116af@b25g2000prb.googlegroups.com> On Jul 21, 2:13?pm, Ben Finney wrote: > bdb112 writes: > > If I want to add an element at the beginning of an array, it seems > > like I must make a list, insert in place, then make an array again. > > The NumPy ?ndarray? type (which is what you get by default from the > ?array? factory function) is a far more complex type than (and is not > derived from) the Python list. > > For manipulating them, you'll want to study the NumPy documentation > . > Yes, I had had a look through that - nothing there that allows writing a succint clear statement (except for my .resize example in the original post), although if efficiency was a concern, then maybe resize and shift right, but that would really obscure the code. From sjmachin at lexicon.net Tue Jul 21 02:01:38 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 20 Jul 2009 23:01:38 -0700 (PDT) Subject: ignore special characters in python regex References: Message-ID: On Jul 21, 3:02?pm, Astan Chee wrote: > Hi, > I'm reading text from a file (per line) and I want to do a regex using > these lines but I want the regex to ignore any special characters and > treat them like normal strings. > Is there a regex function that can do this? It would help if you were to say (1) what "ignore ... characters" means -- pretend they don't exist? (2) what are "special chararacters" -- non-alphanumeric? (3) what "treat them like normal strings" means (4) how you expect these special characters to be (a) ignored and (b) "treated like normal strings" /at the same time/. Cheers, John From cmpython at gmail.com Tue Jul 21 02:11:33 2009 From: cmpython at gmail.com (Che M) Date: Mon, 20 Jul 2009 23:11:33 -0700 (PDT) Subject: any suggestions to synchronize typed text and speech ? References: Message-ID: <9eb00b5c-f51b-45ab-9e1a-1b1691a7e538@h18g2000yqj.googlegroups.com> On Jul 19, 4:15?pm, Stef Mientki wrote: > hello, > > I'm using Scintilla as a wxPython widget with great pleasure. > I now have an application where I want to make notes during a conversation, > but also want to record the speech during that conversation. > I'm using Scintilla as a wxPython widget for editing and PyAudio for the > speech recording, > until so far everything works fine. > > Here the problem part: > I need to synchronize the typed text with the sound during playback. > So if I click somewhere in the sound recording, > the line of text, typed that moment should be highlighted. > And vise versa, if the cursor in the text is moved and some special key > is pressed, > the sound starting 10 or 20seconds earlier should be playbacked. > > I though of adding bookmarks (because these are fixed in the text), and > keep a list of bookmarks and sound pointers. > This idea should work, but there are only 31 bookmarks. > > Any other suggestions ? > > thanks, > Stef Mientki Stef, it occurs to me now that you might be useful to look at Transana: http://www.transana.org/ >From their site: Transana is software for professional researchers who want to analyze digital video or audio data. Transana lets you analyze and manage your data in very sophisticated ways. Transcribe it, identify analytically interesting clips, assign keywords to clips, arrange and rearrange clips, create complex collections of interrelated clips, explore relationships between applied keywords, and share your analysis with colleagues. It is written in Python, uses Scintilla, and is open source (GPL): http://www.transana.org/developers/index.htm Che From ivlenin at gmail.com Tue Jul 21 02:24:05 2009 From: ivlenin at gmail.com (I V) Date: 21 Jul 2009 08:24:05 +0200 Subject: comments? storing a function in an object References: <3a2b1cd7-f337-4a99-8a51-004897ac2247@v20g2000yqm.googlegroups.com> Message-ID: <4a655f05$1@news.x-privat.org> On Mon, 20 Jul 2009 21:53:59 -0400, Esmail wrote: > In general I would agree with you, but in my specific case I want so > store some additional meta-data with each function, such as the valid > range for input values, where the max or minimum are located, the > name/source for the function etc. I am creating list of functions for > use in testing optimization code, so it seems best to store this data > along with the function I am going to optimize in order to verify the > results for a given range (for instance). You can set attributes on functions, so if your class is just storing this data, rather than altering the behavior of the function, you may be able to just use a function: def f(x, y): "x * y" return x * y f.arguments_max = [1000, 255] A function also already knows how many arguments it takes, which you can access as f.func_code.co_argcount From jayshree06comp at gmail.com Tue Jul 21 02:29:06 2009 From: jayshree06comp at gmail.com (jayshree) Date: Mon, 20 Jul 2009 23:29:06 -0700 (PDT) Subject: python function for retrieving key and encryption Message-ID: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> M2Crypto package not showing the 'recipient_public_key.pem' file at linux terminal .how do i get/connect with recipient public key. exactly i need to check how can i open this file through linux commands. import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open ('recipient_public_key.pem','rb').read()) print recip; plaintext = whatever i need to encrypt msg = recip.public_encrypt (plaintext,RSA.pkcs1_padding) print msg; after calling the function its not giving any output and even any error i also tried as 'Will' said pk = open('public_key.pem','rb').read() print pk; rsa = M2Crypto.RSA.load_pub_key(pk) whats the mistake i am not getting .will somebody help me out. is something wrong in opening 'recipient_public_key.pem'. is M2Crypto contain this file inbuilt .from where this file taking public key of the recipient i'. what this file contain and how it will work .should i need to create such a file for my purpose.if it is then how can i create this and how it will retrieve the key,where i recognize my recipient to get his public key .is something like database at server. please give me a quick response...... looking for your answer. From jackdied at gmail.com Tue Jul 21 02:42:02 2009 From: jackdied at gmail.com (Jack Diederich) Date: Tue, 21 Jul 2009 02:42:02 -0400 Subject: Why aren't OrderedDicts comparable with < etc? In-Reply-To: <02746997$0$5185$c3e8da3@news.astraweb.com> References: <02746997$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Mon, Jul 20, 2009 at 10:00 AM, Steven D'Aprano wrote: > On Mon, 20 Jul 2009 09:34:24 +0000, Sion Arrowsmith wrote: > >> Terry Reedy ? wrote: >>>Sion Arrowsmith wrote: >>>> Jack Diederich ? wrote: >>>>> It isn't an OrderedDict thing, it is a comparison thing. ?Two regular >>>>> dicts also raise an error if you try to LT them. >>>> Python 2.5.2 >>>>>>> d1 = dict((str(i), i) for i in range (10)) d2 = dict((str(i), i) >>>>>>> for i in range (20)) d1 < d2 >>>> True >>>Try reversing the definitions of d1 and d2. The dicts are probably being >>>compared by id (address), which is the 2.x CPython default. >> >> Like this? >> >>>>> d1 = dict((str(i), i) for i in range (20)) >>>>> d2 = dict((str(i), i) for i in range (10)) >>>>> d1 < d2 >> False >>>>> id(d1) < id(d2) >> True >> >> I didn't know that comparison for anything other than equality defaulted >> to using id. That really is rather broken, and I'm glad 3.0 fixed it. > > > I don't think comparisons other than equality use id. That would be > rather insane. If anyone can demonstrate such comparisons in a built-in > or standard library class, I'd like to see it. > > > For the record, dicts have been comparable with < and > since at least > Python 1.5: > > $ python1.5 > Python 1.5.2 (#1, Apr ?1 2009, 22:55:54) ?[GCC 4.1.2 20070925 (Red Hat > 4.1.2-27)] on linux2 > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>> >>>> {1: 'a', 2: 'b'} < {2: 'b', 3: 'a'} > 1 > > > Reading the docs: > > http://docs.python.org/library/stdtypes.html#comparisons > http://docs.python.org/library/stdtypes.html#mapping-types-dict > > I can only suggest that dicts compare in an arbitrary but consistent > fashion. I should have specified 3.x but since we were talking about OrderedDicts (only in 3.1) I didn't say it explicitly. Earlier versions of python were very permissive with comparisons -- it was rare that any cmp() raised an error and the ultimate fallback was on the object's id. This is one of the non-backwards compatible changes in 3k. Now comparing two of the same thing that don't have an obvious ordering is an error. Is a dict "greater than" if it has a larger size? if its max key is larger? what does "max key" mean when the keys aren't even comparable?. Comparing things that aren't extremely similar is an error now also. -Jack From astan.chee at al.com.au Tue Jul 21 02:59:16 2009 From: astan.chee at al.com.au (Astan Chee) Date: Tue, 21 Jul 2009 16:59:16 +1000 Subject: ignore special characters in python regex In-Reply-To: References: Message-ID: <4A656744.7050908@al.com.au> I think the re.escape did the trick. to answer your questions: By "ignore" i meant instead of using non-alphanumeric characters that have special significance in regular expression (e.g. [|\]) and treat them as normal strings (i.e preceded by \), but since I don't know all the characters in regular expression that have special significance, I don't know which ones to add a '\' infront of. Thanks anyway John Machin wrote: > On Jul 21, 3:02 pm, Astan Chee wrote: > >> Hi, >> I'm reading text from a file (per line) and I want to do a regex using >> these lines but I want the regex to ignore any special characters and >> treat them like normal strings. >> Is there a regex function that can do this? >> > > It would help if you were to say > > (1) what "ignore ... characters" means -- pretend they don't exist? > (2) what are "special chararacters" -- non-alphanumeric? > (3) what "treat them like normal strings" means > (4) how you expect these special characters to be (a) ignored and (b) > "treated like normal strings" /at the same time/. > > Cheers, > John > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joswig at lisp.de Tue Jul 21 03:00:16 2009 From: joswig at lisp.de (Rainer Joswig) Date: Tue, 21 Jul 2009 00:00:16 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Message-ID: On 21 Jul., 06:57, Frank Buss wrote: > Scott Burson wrote: > > Have you looked at ECL? > > >http://ecls.sourceforge.net/ > > > I've used it only a little, so I can't vouch for its stability, but it > > fits the threading and license requirements (well, some corporate > > lawyers have trouble with the LGPL, but I think it's usable). > > I didn't tried it myself, but looks like it is not very stable: > > http://www.mail-archive.com/application-buil... at lispniks.com/msg01069.html I'm not sure if it is fair to post a reference to a single post by someone without context and without having used ECL. If there are stability problems, people can report to the ECL mailing list. The maintainers are very active. Frank, I have seen you constructive and posting code a year ago. What happened? Several messages of yours I've seen here are now going in the direction of been mostly useless. I thought you could do better. If you are no longer interested in Lisp with no first hand usage, why not switch to comp.lang.misc or some other random place? From hv at tbz-pariv.de Tue Jul 21 03:03:23 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Tue, 21 Jul 2009 09:03:23 +0200 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: <7cl7hrF28lsb2U1@mid.individual.net> casebash schrieb: > Hi, > > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? Hi, you could use a list of characters. It would not be difficult to implement this as a class with all fancy methods like startswith() ... Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From googler.1.webmaster at spamgourmet.com Tue Jul 21 03:22:27 2009 From: googler.1.webmaster at spamgourmet.com (moerchendiser2k3) Date: Tue, 21 Jul 2009 00:22:27 -0700 (PDT) Subject: PIL for OSX 64bit Message-ID: Hi, I have a problem with Python and the Python Image Library to get it work on OSX 64 bit. Maybe anyone can help me? I started Python in 64 bit mode and called Image.open(...).load() and got: ImportError: The _imaging C module is not installed Yes, it seems PIL is not available in 64 bit on my system, but I can't compile it in any way. Is there a tutorial or something like that which explains how I can compile it in 64 bit? I found a workaround where I added this function to the setup.py OrigExtension = Extension def Extension(*args, **kwargs): extra_args = ['-arch', 'x86_64'] kwargs['extra_compile_args'] = extra_args + kwargs.get ('extra_compile_args', []) kwargs['extra_link_args'] = extra_args + kwargs.get('extra_link_args', []) return OrigExtension(*args, **kwargs) without success. Can anyone help me? Thanks... Cheers, Googler From dtgeadamo at yahoo.com Tue Jul 21 03:27:45 2009 From: dtgeadamo at yahoo.com (sightseer) Date: Tue, 21 Jul 2009 00:27:45 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> Message-ID: <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> On Jul 20, 5:25?pm, "David Adamo Jr." wrote: > On Jul 20, 5:14?pm, Tim Golden wrote: > > > > > mistersexy wrote: > > > On Jul 20, 3:03 pm, Tim Golden wrote: > > >> mistersexy wrote: > > >>> I am trying to create a Windows service in Python using pywin32. I do > > >>> not want this service to run under a user account. I want this service > > >>> to be able to run as a LocalService, NetworkService and the like. How > > >>> do I specify this using the win32 library? Thanks, everyone. > > >> When you "install" the service, using the HandleCommandLine > > >> option, specify --username= and --password options. > > > >> TJG > > > > That's exactly my point. I do not want to have to specify username and > > > password options. For instance, when creating a Windows Service > > > in .NET, it is possible to specify that the service should run using > > > the LocalService or NetworkService account. Doing this, you would not > > > need to specify username and password options. Is there a way to > > > achieve this in Python? > > > Sorry, I misread: I mentally removed the "not" in your 'I do not want > > this service to run under a user account' and reinserted it > > further on! > > > By default, the service will run as LocalSystem: you > > only specify a username to override that default. The value > > in Username is passed straight through to the CreateService > > Win32 API, and the docs for that: > > > ?http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx > > > say: > > > """ > > lpServiceStartName [in, optional] > > > ? ? The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName. > > > ? ? If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account. > > > ? ? If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account. > > > ? ? A shared process can run as any user. > > > ? ? If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system. > > > ? ? A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide. > > > ? ? ? ? Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: ?Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2. > > """ > > > So, although I haven't tried it, it looks as though you can pass > > "LocalService" or "NetworkService" and so on if you want to > > override the default LocalSystem but don't want to specify a > > username/password. > > > TJG > > I'll try this stuff. Thanks a million...I'll let everyone know how it > goes. I'm finding it hard to install my Python service. When I try to install the service, this is what I get: Error Installing Service: Access is Denied. (5) Event with a correct username and password (I think). Also, inheriting from the win32serviceutil.Framework class, I do not see a way to explicitly specify the serviceType. I know some of the parameters such as the startupType can be specified when installing the service in the command line, but how are these things specified in the service code when inheriting from win32serviceutil.Framework (especially the serviceType). Does this mean I would have to code my service from scratch without using win32serviceutil.Framework? Basically, I still can't install my service using a NetworkService or LocalSystem Account because it still requires me to specify a username and password. Following Tim Golden's advice, I'm trying to specify the service type in order to determine what account to run the service under. Please help! From hendrik at microcorp.co.za Tue Jul 21 04:03:46 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Tue, 21 Jul 2009 10:03:46 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: <200907211003.46253.hendrik@microcorp.co.za> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: > On Jul 20, 6:08?pm, Duncan Booth wrote: > > The main reason why you need both lists and tuples is that because a > > tuple of immutable objects is itself immutable you can use it as a > > dictionary key. > > Really? That sounds interesting, although I can't think of any real- > world cases where you'd use something like that. simplest is something like a point in the cartesian plane with an associated attribute like colour. - Hendrik From wilk at flibuste.net Tue Jul 21 04:19:29 2009 From: wilk at flibuste.net (William Dode) Date: 21 Jul 2009 08:19:29 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> Message-ID: <4a657a11$0$417$426a74cc@news.free.fr> On 20-07-2009, srepmub wrote: > >> Nice timings, can you please show me the Python, Java and C code >> versions? I may do more tests. Of course, the codes are here : http://hg.flibuste.net/libre/games/cheval Like you'll see, i tried to use exactly the same code for each langage. > > also, which shedskin options did you use? did you use -bw, to disable > bounds and wrap-around checking? this can make quite a difference for > code that does a lot of indexing. if the code uses random numbers, > then -r can also make a big difference, to use C rand(), instead of > Python compatible random numbers. > > and which C++ compiler flags did you use? the default -O2, or > something like -O3 -fomit-frame-pointer -msse2..? I used the default, shedksin cheval.py; make shedskin 0.2 With -bw and -O3 -fomit-frame-pointer -msse2 i have 5.5s (instead of 8) Let me know if you find better. -- William Dod? - http://flibuste.net Informaticien Ind?pendant From wilk at flibuste.net Tue Jul 21 04:20:29 2009 From: wilk at flibuste.net (William Dode) Date: 21 Jul 2009 08:20:29 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <04f03d96-46b7-4761-8498-9149f19ecba6@k6g2000yqn.googlegroups.com> Message-ID: <4a657a4d$0$417$426a74cc@news.free.fr> On 20-07-2009, Bearophile wrote: > Skip Montanaro: >> I read just enough French to know that "avec" means "with", but I don't >> understand the difference between "avec malloc *int" and "avec []". ?Can you >> explain please? > > Maybe it's the time difference between using a Python list from Cython > and using a C "array" allocated with a malloc from Cython. yes, it's this -- William Dod? - http://flibuste.net Informaticien Ind?pendant From ben+python at benfinney.id.au Tue Jul 21 04:36:14 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 21 Jul 2009 18:36:14 +1000 Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <47ab4ab6-d8c8-4997-8963-ac41b00dd10f@y10g2000prg.googlegroups.com> Message-ID: <87ws628mbl.fsf@benfinney.id.au> John Machin writes: > OK, I'll bite: where does the Python 3.x bytearray type I wasn't previously aware of it, thanks for bringing it to my attention . > fit into your taxonomy? At first glance it appears to be mutable and > have a fair swag of functionality. Yes. I'd classify it as a mutable array of bytes :-) but, as such, not to be used as a text string. -- \ ?Science doesn't work by vote and it doesn't work by | `\ authority.? ?Richard Dawkins, _Big Mistake_ (The Guardian, | _o__) 2006-12-27) | Ben Finney From ben+python at benfinney.id.au Tue Jul 21 04:38:31 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 21 Jul 2009 18:38:31 +1000 Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> Message-ID: <87skgq8m7s.fsf@benfinney.id.au> Steven D'Aprano writes: > On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote: > > A mutable string would not (AFAICT) be usefully implementable as a > > subclass of the built-in string types. So even if such a type > > existed, it would not be useable with all the functionality that > > works with strings. > > If applications ignore duck-typing and do isinstance(value, str), it's > arguably the application and not the value that is broken. Agreed, I wouldn't advise that. My point was rather meant to imply that subclassing the built-in (immutable) string types was the best way to usefully get all their functionality, rather than re-implementing most of it. -- \ ?I went to a restaurant that serves ?breakfast at any time?. So | `\ I ordered French Toast during the Renaissance.? ?Steven Wright | _o__) | Ben Finney From dickinsm at gmail.com Tue Jul 21 04:51:50 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Tue, 21 Jul 2009 01:51:50 -0700 (PDT) Subject: missing 'xor' Boolean operator References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> <24543805.post@talk.nabble.com> Message-ID: <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> On Jul 20, 11:34?pm, Ethan Furman wrote: > Dr. Phillip M. Feldman wrote: > ?> Suppose that 'xor' returns the value that is true when only one value is > ?> true, and False otherwise. ?This definition of xor doesn't have the > standard > ?> associative property, that is, > ?> > ?> (a xor b) xor c > ?> > ?> will not necessarily equal > ?> > ?> a xor (b xor c) > ?> > ?> To see that this is the case, let a= 1, b= 2, and c= 3. > ?> > ?> (a xor b) xor c > ?> > ?> yields 3, while > ?> > ?> a xor (b xor c) > ?> > ?> yields 1. ?So, I'd prefer an xor operator that simply returns True or > False. > ?> > ?> Phillip > ?> > > You are, of course, free to write your version however it makes sense to > you and your team. ?:) > > Since the 'and' and 'or' already return objects (and objects evaluate to > true or false), then 'xor' should behave likewise, IMO. ?I expect that > would be the case if it were ever added to the language. I'm not so sure. Did you ever wonder why the any() and all() functions introduced in 2.5 return a boolean rather than returning one of their arguments? (I did, and I'm still not sure what the answer is.) Mark From juanjose.garciaripoll at googlemail.com Tue Jul 21 05:23:59 2009 From: juanjose.garciaripoll at googlemail.com (Juanjo) Date: Tue, 21 Jul 2009 02:23:59 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Message-ID: <56721e57-e0b4-4d7d-8693-a96d1dca061b@j32g2000yqh.googlegroups.com> On Jul 21, 6:57?am, Frank Buss wrote: > Scott Burson wrote: > > Have you looked atECL? > > >http://ecls.sourceforge.net/ > > > I've used it only a little, so I can't vouch for its stability, but it > > fits the threading and license requirements (well, some corporate > > lawyers have trouble with the LGPL, but I think it's usable). > > I didn't tried it myself, but looks like it is not very stable: > > http://www.mail-archive.com/application-builder at lispniks.com/msg01069... ECL suffers from fast development problems if this is what you mean. People are advised to stay with certain releases and we announce when some ports are broken due to progress along certain lines. For instance, if I find that the generational garbage collector is needed for Linux, FreeBSD, OpenBSD and OS X, and it works, I do not mind dropping temporarily the mingw port until the garbage collector library catches up. People who wanted to stay with mingw produced a branch (see Samium's posts) with the old garbage collector. Now this is not so dramatic. ECL now has a release cycle of ONE MONTH. If you find that this month's release does not work on your platform (and this is normally explicit in the announcement), then do not upgrade and wait one or two months until the problem is solved. OTOH, people only seem to notice problems when their petty platform is temporarily broken, but nobody seems to notice the overall stability and portability of the platform. See the list http://ecls.sourceforge.net/logs.html which will soon expand including Solaris and regular builds on Windows using the free Microsoft compiler. And once the ARM computer I have been gifted by a happy arrives, then Debian-ARM as well. Juanjo From milanj at gmail.com Tue Jul 21 05:37:27 2009 From: milanj at gmail.com (milanj) Date: Tue, 21 Jul 2009 02:37:27 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> On Jul 19, 8:31?pm, Frank Buss wrote: > Python is not that bad. Unlike Lisp, there is much less undefined behavior, > there is one free unique implementation on the 3 major platforms Linux, > Windows and MacOS X, which is stable, support multithreading and has a > default GUI library binding, which is difficult to find for Lisp (e.g. I > don't know of a free modern and stable Lisp implemenation with > mulithreading support for Windows, with a licence with which you can use it > in closed source commercial programs, like you can do with Python). Many > problems in the Lispbuilder mailing list are related to problems due to > different operating systems and Lisp implementations. > Frank Buss, f... at frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de Someone should mention Clozure CL - http://trac.clozure.com/openmcl As you can see there is os x, freebsd, linux, solaris and windows port and all of them use native threads (python still use green threads ?) and development is pretty alive, they did planty of developing last year[s], ccl licence permits you to deliver closed source programs ... CCL is promising bright feature to CL since looks like the insist of building stable implementation across most arch in use today From martin.hellwig at dcuktec.org Tue Jul 21 05:40:54 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 21 Jul 2009 10:40:54 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> Message-ID: sightseer wrote: > > Error Installing Service: Access is Denied. (5) > Are you trying to do this on windows vista? -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From gagsl-py2 at yahoo.com.ar Tue Jul 21 05:56:33 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 06:56:33 -0300 Subject: ignore special characters in python regex References: <4A654C01.2000508@al.com.au> Message-ID: En Tue, 21 Jul 2009 02:02:57 -0300, Astan Chee escribi?: > I'm reading text from a file (per line) and I want to do a regex using > these lines but I want the regex to ignore any special characters and > treat them like normal strings. > Is there a regex function that can do this? > Here is what I have so far: > fp = open('file.txt','r') > notes = fp.readlines() > fp.close() > strin = "this is what I want" > for note in notes: > if re.search(r""+ str(note) + "",strin): > print "Found " + str(note) + " in " + strin You don't even need a regex for that. py> "fragil" in "supercalifragilisticexpialidocious" True Note that: r""+ str(note) + "" is the same as: str(note) which in turn is the same as: note Remember that each line keeps its '\n' final! for note in notes: if note.rstrip('\n') in strin: print "Found %s in %s" % (note, strin) -- Gabriel Genellina From duncan.booth at invalid.invalid Tue Jul 21 06:28:16 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 21 Jul 2009 10:28:16 GMT Subject: Help understanding the decisions *behind* python? References: <87hbx7yp16.fsf@busola.homelinux.net> Message-ID: Hrvoje Niksic wrote: > Chris Rebert writes: > >>>>>>>> x = [2,1,3] >>>>>>>> print sorted(x)[0] >>>>DB> 3 >>> >>> What kind of Python produces that? >> >> Assuming you're referring to the latter example, it was added in >> version 2.4 If you meant the former example, I think that's purely >> pseudo-Python. > > sorted([2, 1, 3])[0] evaluates to 1, not 3. > I guess you can tell I broke my unbreakable rule to never post untested code without labelling it as such. :-) -- Duncan Booth http://kupuguy.blogspot.com From jeanmichel at sequans.com Tue Jul 21 06:36:14 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 21 Jul 2009 12:36:14 +0200 Subject: How to import pydoc and then use it? In-Reply-To: <1248126024.9281.3.camel@centar> References: <1248126024.9281.3.camel@centar> Message-ID: <4A659A1E.9090509@sequans.com> Albert Hopkins wrote: > On Mon, 2009-07-20 at 13:38 -0700, mrstevegross wrote: > >> I know how to use pydoc from the command line. However, because of >> complicated environmental setup, it would be preferable to run it >> within a python script as a native API call. That is, my python runner >> looks a bit like this: >> >> import pydoc >> pydoc.generate_html_docs_for(someFile) >> >> However, it's not clear to me from the pydoc documentation which >> function calls I need to use to make this behavior work. Any ideas? >> > > Did you try 'pydoc pydoc'? ;) > > >>>> import pydoc >>>> htmldoc = pydoc.HTMLDoc() >>>> htmldoc_for_pydoc = htmldoc(pydoc) >>>> print htmldoc_for_pydoc >>>> > > By the way, just in case the OP didn't know, there is the epydoc module (google it) which is doing basically the same things than pydoc, but with a much better look and feel to my opinion. JM From mark.dufour at gmail.com Tue Jul 21 08:25:01 2009 From: mark.dufour at gmail.com (srepmub) Date: Tue, 21 Jul 2009 05:25:01 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> Message-ID: <60222088-2059-49b1-a442-a56fc85d6838@g31g2000yqc.googlegroups.com> > With -bw and -O3 -fomit-frame-pointer -msse2 i have 5.5s (instead of 8) > > Let me know if you find better. thanks. now I'm wondering how fast does the C version become with these flags..? :-) mark. From nick at craig-wood.com Tue Jul 21 08:29:58 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Tue, 21 Jul 2009 07:29:58 -0500 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: Mark Dufour wrote: > I have just released version 0.2 of Shed Skin, an experimental > (restricted) Python-to-C++ compiler (http://shedskin.googlecode.com). > It comes with 7 new example programs (for a total of 40 example > programs, at over 12,000 lines) and several important improvements/bug > fixes. See http://code.google.com/p/shedskin/wiki/ReleaseNotes for the > full changelog. Cool! I tried it on a program I wrote to solve a puzzle (Rush Hour). (Actually it solves the meta-puzzle - trying to make the hardest possible Rush Hour puzzle.) After a bit of twiddling (remove psyco and profiling) I got it to start compiling, but unfortunately it compiled for about an hour (in iterative type analysis..) used up 600 MB of RAM printing an '*' every 10 minutes or so then gave an error message and gave up. Unfortunately I shut the window by accident, but the error message was something about not being able to resolve types I think with a list of 20 or so unresolved types. Can you give a hint as to how I debug this? I presume my program has some instances of non static types which is causing the problem, but it is going to be a very long debugging session if it takes me an hour each cycle ;-) The program is about 700 lines of python (excluding comments). Thanks Nick -- Nick Craig-Wood -- http://www.craig-wood.com/nick From wilk at flibuste.net Tue Jul 21 08:44:30 2009 From: wilk at flibuste.net (William Dode) Date: 21 Jul 2009 12:44:30 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <60222088-2059-49b1-a442-a56fc85d6838@g31g2000yqc.googlegroups.com> Message-ID: <4a65b82e$0$11946$426a74cc@news.free.fr> On 21-07-2009, srepmub wrote: > >> With -bw and -O3 -fomit-frame-pointer -msse2 i have 5.5s (instead of 8) >> >> Let me know if you find better. > > thanks. now I'm wondering how fast does the C version become with > these flags..? :-) I don't see any difference... -- William Dod? - http://flibuste.net Informaticien Ind?pendant From dtgeadamo at yahoo.com Tue Jul 21 08:49:45 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Tue, 21 Jul 2009 05:49:45 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> Message-ID: <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> On Jul 21, 10:40?am, "Martin P. Hellwig" wrote: > sightseer wrote: > > > > > Error Installing Service: Access is Denied. (5) > > > Are you trying to do this on windows vista? > > -- > MPHhttp://blog.dcuktec.com > 'If consumed, best digested with added seasoning to own preference.' Yeah, I was trying to do it on Vista. Someone has just helped me out. I had to deactivate User Account Control on Windows Vista...and now everything is rosy. Thanks guys. From craig1.boyd at citi.com Tue Jul 21 08:52:19 2009 From: craig1.boyd at citi.com (Boyd, Craig1 ) Date: Tue, 21 Jul 2009 07:52:19 -0500 Subject: Auto-generate GUI from a database table structure In-Reply-To: References: <55660F9659966544BBA3AE14CE5D278D01243B8E04@exgtmb08.nam.nsroot.net> Message-ID: <55660F9659966544BBA3AE14CE5D278D01243B8E11@exgtmb08.nam.nsroot.net> Gabriel, Thanks for pointing this out. It does not look like it supports Oracle at this time, which is one of my requirements, but that is based on doing a quick read of the wiki. I will dig in further and see. Perhaps it supports ODBC which would be an acceptable work-around. Thanks again, Craig -----Original Message----- From: python-list-bounces+craig1.boyd=citi.com at python.org [mailto:python-list-bounces+craig1.boyd=citi.com at python.org] On Behalf Of Gabriel Genellina Sent: Monday, July 20, 2009 4:40 PM To: python-list at python.org Subject: Re: Auto-generate GUI from a database table structure En Mon, 20 Jul 2009 12:09:47 -0300, Boyd, Craig1 escribi?: > I am trying to write a couple screens to update three or four database > tables on Oracle 10g and I was wondering if there was a way to > automatically generate some kind of GUI shell based on the tables that > I could then fill in with the logic / functionality I need. I've never actually used it, but the DABO project seems to provide what you want: http://dabodev.com/ -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list From paul at boddie.org.uk Tue Jul 21 08:55:12 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Tue, 21 Jul 2009 05:55:12 -0700 (PDT) Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: <1c994086-8c58-488f-b3b3-6161c4b2ba05@k30g2000yqf.googlegroups.com> On 20 Jul, 18:00, a... at pythoncraft.com (Aahz) wrote: > > Out of curiosity, are there any JavaScript toolkits that generate code > that degrades gracefully when JavaScript is disabled? You mean "Web toolkits which use JavaScript", I presume. I have written (and use myself) a toolkit/framework called XSLForms (part of the XSLTools distribution) which supports in-page updates (AJAX, effectively) that degrade to vanilla whole-page requests if JavaScript is switched off: http://www.boddie.org.uk/python/XSLTools.html I've been meaning to have a few example applications running on that site to illustrate the point. Some people complain that by constraining the JavaScript functionality to merely enhance vanilla HTML/HTTP functionality, one is not using the full potential of the medium, but having used sites whose JavaScript has failed, thus preventing the use of those sites almost completely, I think that stacking AJAX-like stuff on top of normal stuff helps the majority of developers offer an accessible solution. Paul From rashidsdpk at gmail.com Tue Jul 21 08:56:21 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Tue, 21 Jul 2009 05:56:21 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: <2ea811b0-8135-4df2-b3c1-964414291341@g31g2000yqc.googlegroups.com> Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10.more link http://0nanotechnology0.blogspot.com/ From c.c.wood at gmail.com Tue Jul 21 09:22:30 2009 From: c.c.wood at gmail.com (ChrisW) Date: Tue, 21 Jul 2009 06:22:30 -0700 (PDT) Subject: Multiple versions of python Message-ID: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Hi, I have installed 2 versions of python on my Windows XP computer - I originally had 3.0.1, but then found that the MySQL module only supported 2.*, so I've now installed that. I have found that if I change the Windows Environment Variable path, then I can change the version of python called when I type 'python' into a command line. However, I'd like to be able to choose which version I use. I know that if I change C:\Python26\python.exe to C:\Python26\python2.exe and C:\Python30\python.exe to C: \Python26\python3.exe, then typing 'python2' or 'python3' will invoke the correct interpreter. However, is it safe just to rename the executable files? Is there a more elegant way to achieve the same task? Thanks, Chris From peter.fodrek at stuba.sk Tue Jul 21 09:30:47 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Tue, 21 Jul 2009 15:30:47 +0200 Subject: Regular expression Message-ID: <200907211530.47704.peter.fodrek@stuba.sk> Dear conference! I have third party regular expression self.pattern_main = re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| \#\d+\=(?:[+])?\d*(?:\.\d*)?)') with code def readline(self): self.line = self.file_in.readline().rstrip() if (len(self.line)) : return True else : return False while (self.readline()): words = self.pattern_main.findall(self.line) This handles text file like // remark PL_OFF PARK FS MA 52.8806 , 18.0914 SS AD_W PL_ON C 52.3955 , -16.1511 , -90.0000 It handles file correctly with two exceptions 1) omits ',' in the a output 2) omits minus sign in the numbers... Would anyone recommend me what to change regular expression to add thesee two think to the output,please? Thank you for any help. I look forward hearing form you Yours faithfully Peter Fodrek From Scott.Daniels at Acm.Org Tue Jul 21 09:46:12 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 21 Jul 2009 06:46:12 -0700 Subject: Mechanize not recognized by py2exe In-Reply-To: References: <4A60C81F.7080707@comcast.net> Message-ID: OrcaSoul wrote: > ...it's too late to name my first born after you, but > I did have a cat named Gabriel - she was a great cat! Remember Gabriel in English uses a hard A, as in the horn player, not Gabrielle. I know because when I first read his posts I made the same trick in my head, and hence imagined a woman. I suspect it would come to irk one almost enough to become a Gabe. --Scott David Daniels Scott.Daniels at Acm.Org From inky788 at gmail.com Tue Jul 21 09:49:59 2009 From: inky788 at gmail.com (Inky 788) Date: Tue, 21 Jul 2009 06:49:59 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> On Jul 20, 12:27?pm, Phillip B Oldham wrote: > [snip] We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. My guess is that it was probably for optimization reasons long ago. I've never heard a *good* reason why Python needs both. From Scott.Daniels at Acm.Org Tue Jul 21 10:03:29 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 21 Jul 2009 07:03:29 -0700 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <02746e73$0$5185$c3e8da3@news.astraweb.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote: >> What is it you're trying to do that makes you search for a mutable >> string type? It's likely that a better approach can be found. > > When dealing with very large strings, it is wasteful to have to duplicate > the entire string just to mutate a single character. > > However, when dealing with very large strings, it's arguably better to > use the rope data structure instead. The general problem is that whether strings are mutable or not is an early language design decision, and few languages provide both. Mutable strings need lots of data copying to be safe passing args to unknown functions; immutable strings need lots of copying for ncremental changes. The rope is a great idea for some cases. I'd argue Python works better with immutable strings, because Python is too slow at per-character operations to be running up and down strings a character at a time, changing here and there. So it becomes more natural to deal with strings as chunks to pass around, and it is nice not to have to copy the strings when doing that passing around. --Scott David Daniels Scott.Daniels at Acm.Org From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Tue Jul 21 10:13:00 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Tue, 21 Jul 2009 10:13:00 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: On 2009-07-21 05:37:27 -0400, milanj said: > Someone should mention Clozure CL - http://trac.clozure.com/openmcl > As you can see there is os x, freebsd, linux, solaris and windows port > and all of them use native threads (python still use green threads ?) > and development is pretty alive, they did planty of developing last > year[s], ccl licence permits you to deliver closed source programs ... > CCL is promising bright feature to CL since looks like the insist of > building stable implementation across most arch in use today Hear, hear! -- Raffael Cavallaro From Scott.Daniels at Acm.Org Tue Jul 21 10:17:13 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 21 Jul 2009 07:17:13 -0700 Subject: Multiple versions of python In-Reply-To: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: ChrisW wrote: > Hi, > > I have installed 2 versions of python on my Windows XP computer - I > originally had 3.0.1, but then found that the MySQL module only > supported 2.*, so I've now installed that. I have found that if I > change the Windows Environment Variable path, then I can change the > version of python called when I type 'python' into a command line. > However, I'd like to be able to choose which version I use. I know > that if I change C:\Python26\python.exe to > C:\Python26\python2.exe and C:\Python30\python.exe to C: > \Python26\python3.exe, then typing 'python2' or 'python3' will invoke > the correct interpreter. However, is it safe just to rename the > executable files? Is there a more elegant way to achieve the same > task? I wouldn't rename them. You can, of course, copy them (so you have two executables), or you can pick a somedir on your path (I made a directory "C:\cmds" that I add to my path, but tastes vary). C:> copy con \py25.cmd C:\Python25\python\python.exe %* ^Z C:> copy con \py31.cmd C:\Python31\python\python.exe %* ^Z I'd use the two-digit form, as that is where interface changes happen; trying code with py24, py25, py26 can be convenient. By the way, install Python 3.1 rather than 3.0; think of 3.0 as the alpha of the 3.X branches (it will get no love at all now that 3.1 is out). --Scott David Daniels Scott.Daniels at Acm.Org From davea at dejaviewphoto.com Tue Jul 21 10:19:42 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Tue, 21 Jul 2009 10:19:42 -0400 Subject: Multiple versions of python In-Reply-To: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: <4A65CE7E.6090800@dejaviewphoto.com> ChrisW wrote: > Hi, > > I have installed 2 versions of python on my Windows XP computer - I > originally had 3.0.1, but then found that the MySQL module only > supported 2.*, so I've now installed that. I have found that if I > change the Windows Environment Variable path, then I can change the > version of python called when I type 'python' into a command line. > However, I'd like to be able to choose which version I use. I know > that if I change C:\Python26\python.exe to > C:\Python26\python2.exe and C:\Python30\python.exe to C: > \Python26\python3.exe, then typing 'python2' or 'python3' will invoke > the correct interpreter. However, is it safe just to rename the > executable files? Is there a more elegant way to achieve the same > task? > > Thanks, > Chris > > The elegant way is to have a batch directory on your PATH ( I use m:\t\bat ) and put your *.bat files there. I do NOT put any python installations on the PATH. For example, I have a batch file called: m:\t\bat\python26.bat c:\progfiles\python26\python.exe %* The %* syntax means pass all arguments through to the program. Once it all works, you can add an "@" in front of the c: in order to suppress echoing the command. At that point, it'll look and work the same way as what you did, but without modifying anything in the install directory. (You may want python26.bat, pythonw26.bat, python31.bat and pythonw31.bat) The other thing you may want to do in a batch file is to change the file associations so that you can run the .py file directly, without typing "python" or "pythonw" in front of it. The relevant Windows commands are: assoc and ftype And on a related note, you may want to edit the PATHEXT environment variable, to add .PY and .PYW From hv at tbz-pariv.de Tue Jul 21 10:27:27 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Tue, 21 Jul 2009 16:27:27 +0200 Subject: Workflow Libraries (DB vs Code) Message-ID: <7cm1ifF28jmnhU1@mid.individual.net> Hi, I need to write some simple workflows with web interface. For the web stuff I will use django, but I am not sure how to do the workflow part. Here are ideas: Do it myself. I don't think it will be difficult, I did things like this before. But they were not reusable. Use spiff Workflow library http://code.google.com/p/spiff-workflow/ Use goflow for django (port of OpenFlow of Zope). It does not look like a active project. Has anyone worked with spiff, goflow or other workflow software? What do you like and dislike? Somehow I like it the way goflow does it: The process definitions are stored in the database. But on the other hand I prefer to have things like this in a SVN-Repository. Creating the workflow definitions will be done by python programmers. I don't need a GUI to build them. There are many languages to define processes (BPML, XPDL, BPEL or YAWL). But I prefer python. Feedback welcome, Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From sajmikins at gmail.com Tue Jul 21 10:30:56 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 07:30:56 -0700 (PDT) Subject: invoke method on many instances References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Jul 20, 3:29?am, "Gabriel Genellina" wrote: > En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac ? > escribi?: > > >> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: > >>> def apply2(itr, methodname, *args, **kwargs): > >>> ? ? f = operator.methodcaller(methodname, *args, **kwargs) > >>> ? ? for item in itr: > >>> ? ? ? ? f(item) > > > On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: > >> for obj in objects: > >> ? ? getattr(obj, methodname)(*args, **kwargs) > > > Are there any obvious considerations in choosing > > between those two? > > The operator.methodcaller version is faster in my tests for large ? > collections, but slightly slower when you have very few elements. > > > import operator > import timeit > > class X: > ? ?def method(self, x, y, **kw): pass > > def apply1(itr, methodname, *args, **kwargs): > ? ? ?for item in itr: > ? ? ? ? ?getattr(item, methodname)(*args, **kwargs) > > def apply2(itr, methodname, *args, **kwargs): > ? ? ?f = operator.methodcaller(methodname, *args, **kwargs) > ? ? ?for item in itr: > ? ? ? ? ?f(item) > > L=[X() for _ in range(3000)] > apply1(L,'method', 1, 2, foo=3) > apply2(L,'method', 1, 2, foo=3) > > timeit.timeit(setup="from __main__ import apply1,apply2,L", ? > stmt="apply1(L,'method', 1, 2, foo=3)", number=1000) > timeit.timeit(setup="from __main__ import apply1,apply2,L", ? > stmt="apply2(L,'method', 1, 2, foo=3)", number=1000) > > > -- > Gabriel Genellina If your instances are all of the same class you can pass in the method directly like so: def apply3(iterator, method, *args, **kwargs): for item in iterator: method(item, *args, **kwargs) class X: def method(self, x, y, **kw): pass items = [X() for _ in range(10)] apply3(items, X.method, 1, 2, foo=3) HTH, ~Simon From sajmikins at gmail.com Tue Jul 21 10:42:08 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 10:42:08 -0400 Subject: tough-to-explain Python In-Reply-To: <7xws627qc1.fsf@ruckus.brouhaha.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7xljmk8q1m.fsf@ruckus.brouhaha.com> <6348dcb8-bfc1-446e-b7bf-28657e2b607e@j32g2000yqh.googlegroups.com> <7xws627qc1.fsf@ruckus.brouhaha.com> Message-ID: <50f98a4c0907210742q47aa3e21s9a410e833ab78718@mail.gmail.com> On Mon, Jul 20, 2009 at 9:54 PM, Paul Rubin wrote: > Simon Forman writes: >> But I'm glad it's there to study, these are wheels I don't have to >> invent for myself. > > http://dwheeler.com/essays/high-assurance-floss.html > > might be an interesting place to start. OO la la! Thank you! From slobodan.blazeski at gmail.com Tue Jul 21 10:42:18 2009 From: slobodan.blazeski at gmail.com (Slobodan Blazeski) Date: Tue, 21 Jul 2009 07:42:18 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <03fef26b-b8ed-4863-ba55-309624c97eff@o6g2000yqj.googlegroups.com> On Jul 19, 7:33?pm, fft1976 wrote: > On Jul 19, 9:55?am, Frank Buss wrote: > > > E.g. the number system: In many Lisp > > implementations (/ 2 3) results in the fractional object 2/3. In Python 2.6 > > "2 / 3" results in "0". Looks like with Python 3.1 they have fixed it, now > > it returns "0.6666666666", which will result in lots of fun for porting > > applications written for Python <= 2.6. > > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. Worse is better? Bobi http://www.linkedin.com/in/slobodanblazeski From bearophileHUGS at lycos.com Tue Jul 21 10:46:26 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 21 Jul 2009 07:46:26 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: Nick Craig-Wood: > Can you give a hint as to how I debug this? ?I presume my program has > some instances of non static types which is causing the problem, but > it is going to be a very long debugging session if it takes me an hour > each cycle ;-) > > The program is about 700 lines of python (excluding comments). You can show us the Python (SSPython) code, and we can try to find the problem. Sometimes there's no simple ways to solve such problems. Generally for not very large progrograms if SS doesn't compile in about a minute or so then it's gone in infinite loop (there's a compilation flag that avoids some infinite loops, try it). Bye, bearophile From python at mrabarnett.plus.com Tue Jul 21 10:50:15 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 21 Jul 2009 15:50:15 +0100 Subject: Regular expression In-Reply-To: <200907211530.47704.peter.fodrek@stuba.sk> References: <200907211530.47704.peter.fodrek@stuba.sk> Message-ID: <4A65D5A7.9060908@mrabarnett.plus.com> Peter Fodrek wrote: > Dear conference! > > I have third party regular expression > > self.pattern_main = re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| > \#\d+\=(?:[+])?\d*(?:\.\d*)?)') > [snip] > It handles file correctly with two exceptions > > 1) omits ',' in the a output > 2) omits minus sign in the numbers... > > Would anyone recommend me what to change regular expression to add thesee two > think to the output,please? > self.pattern_main = re.compile(r'(\s+|,|-?\w\+?\d*(?:\.\d*)?|\w#\d+|\(.*?\)|#\d+=\+?\d*(?:\.\d*)?)') From piet at cs.uu.nl Tue Jul 21 11:05:36 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 17:05:36 +0200 Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: >>>>> Hendrik van Rooyen (HvR) wrote: >HvR> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: >>> On Jul 20, 6:08?pm, Duncan Booth wrote: >>> > The main reason why you need both lists and tuples is that because a >>> > tuple of immutable objects is itself immutable you can use it as a >>> > dictionary key. >>> >>> Really? That sounds interesting, although I can't think of any real- >>> world cases where you'd use something like that. >HvR> simplest is something like a point in the cartesian plane with >HvR> an associated attribute like colour. There are numerous other examples. Anytime you need a key that is not a single object but composed of more than one: Name + birthday Street + number + City Student + Course etc. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From bearophileHUGS at lycos.com Tue Jul 21 11:08:37 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 21 Jul 2009 08:08:37 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> Message-ID: <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> William Dode': > http://hg.flibuste.net/libre/games/cheval > Like you'll see, i tried to use exactly the same code for each langage. It's a cute solver. Few more versions of mine: #1, a Psyco version of mine: http://codepad.org/9m5rf7kX #2, unrolled Psyco version: http://codepad.org/gKFLu34M #3, a quick D (D1) version: http://codepad.org/Tk9FL7Xk Timings (no printing), seconds, best of 3: #1: 4.79 #2: 3.67 #3: 1.10 Your Psyco version: 13.37 Your C version, compiled with GCC 4.3.2, -s -O3 -fomit-frame-pointer: 3.79 I have timed the whole running time of the programs, with an external timer, so my timings include the start of the PythonVM and the final cleanup of the GC. Please, feel free to time my code again, so we can compare with your other timings (Java, etc) better. I have used Psyco 1.6 final and Python 2.6.2 on a Core2 CPU at 2 GHz. To compile the D1 code you can use the free DMD compiler for D1, I have used version 1.042, www.digitalmars.com/d/download.html ). In such benchmarks it's often better to start from the fastest low level code (written in an imperative language as C/D/C++, or a version in a functional language like Clean or OCaML) and then use it to create higher level versions (in Python, etc). This offers you a baseline timing, and usually the small differences of the higher level code compared to the original C/Clean code don't invalidate the test. I have changed only small things in the program, as you can see the Psyco program #2 is faster than your C code :-) If you learn how to use it well, Psyco1.6 can often lead to very good performance. But lot of people don't know how to use Psyco well (I too am ignorant: I don't know how to profile Python programs yet). Bye, bearophile From dns4 at cornell.edu Tue Jul 21 11:14:37 2009 From: dns4 at cornell.edu (David Smith) Date: Tue, 21 Jul 2009 11:14:37 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: Piet van Oostrum wrote: >>>>>> Hendrik van Rooyen (HvR) wrote: > >> HvR> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: >>>> On Jul 20, 6:08 pm, Duncan Booth wrote: >>>>> The main reason why you need both lists and tuples is that because a >>>>> tuple of immutable objects is itself immutable you can use it as a >>>>> dictionary key. >>>> Really? That sounds interesting, although I can't think of any real- >>>> world cases where you'd use something like that. > >> HvR> simplest is something like a point in the cartesian plane with >> HvR> an associated attribute like colour. > > There are numerous other examples. Anytime you need a key that is not a > single object but composed of more than one: > Name + birthday > Street + number + City > Student + Course > etc. Compound keys (like what's listed above) can also be used for sorting lists of dictionaries using DSU style sorting. Something I believe (and I could be wrong) won't work with mutable types like lists. --David From piet at cs.uu.nl Tue Jul 21 11:32:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 17:32:26 +0200 Subject: Help understanding the decisions *behind* python? References: <4f467d53-b121-430f-b153-8e4d4fc8476a@o15g2000yqm.googlegroups.com> Message-ID: >>>>> David Smith (DS) wrote: >DS> Piet van Oostrum wrote: >>>>>>>> Hendrik van Rooyen (HvR) wrote: >>> >HvR> On Monday 20 July 2009 21:26:07 Phillip B Oldham wrote: >>>>>> On Jul 20, 6:08 pm, Duncan Booth wrote: >>>>> The main reason why you need both lists and tuples is that because a >>>>> tuple of immutable objects is itself immutable you can use it as a >>>>> dictionary key. >>>>>> Really? That sounds interesting, although I can't think of any real- >>>>>> world cases where you'd use something like that. >>> >HvR> simplest is something like a point in the cartesian plane with >HvR> an associated attribute like colour. >>> >>> There are numerous other examples. Anytime you need a key that is not a >>> single object but composed of more than one: >>> Name + birthday >>> Street + number + City >>> Student + Course >>> etc. >DS> Compound keys (like what's listed above) can also be used for sorting >DS> lists of dictionaries using DSU style sorting. Something I believe (and >DS> I could be wrong) won't work with mutable types like lists. For sorting there is no problem with mutable arrays as long as you don't mutate them during the sorting process (for example in the comparison routine). Doing that would be extremely perverse. And there is no enforcement of that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From ethan at stoneleaf.us Tue Jul 21 11:34:19 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 21 Jul 2009 08:34:19 -0700 Subject: missing 'xor' Boolean operator In-Reply-To: <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> References: <24485116.post@talk.nabble.com> <4A5DE5E7.5070003@mrabarnett.plus.com> <24543805.post@talk.nabble.com> <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> Message-ID: <4A65DFFB.3090004@stoneleaf.us> Mark Dickinson wrote: > On Jul 20, 11:34 pm, Ethan Furman wrote: > >>Dr. Phillip M. Feldman wrote: >> > Suppose that 'xor' returns the value that is true when only one value is >> > true, and False otherwise. This definition of xor doesn't have the >>standard >> > associative property, that is, >> > >> > (a xor b) xor c >> > >> > will not necessarily equal >> > >> > a xor (b xor c) >> > >> > To see that this is the case, let a= 1, b= 2, and c= 3. >> > >> > (a xor b) xor c >> > >> > yields 3, while >> > >> > a xor (b xor c) >> > >> > yields 1. So, I'd prefer an xor operator that simply returns True or >>False. >> > >> > Phillip >> > >> >>You are, of course, free to write your version however it makes sense to >>you and your team. :) >> >>Since the 'and' and 'or' already return objects (and objects evaluate to >>true or false), then 'xor' should behave likewise, IMO. I expect that >>would be the case if it were ever added to the language. > > > I'm not so sure. Did you ever wonder why the any() and all() > functions introduced in 2.5 return a boolean rather than returning > one of their arguments? (I did, and I'm still not sure what the > answer is.) > > Mark Very good question -- I likewise do not know the answer. I will only observe that any() and all() are functions, while 'and' and 'or' are not. If one wanted the object-returning behavior one could string together 'or's or 'and's instead. ~Ethan~ -- Thinking out loud here -- YMMV. :) From rhodri at wildebst.demon.co.uk Tue Jul 21 11:39:51 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 21 Jul 2009 16:39:51 +0100 Subject: Regular expression In-Reply-To: <200907211530.47704.peter.fodrek@stuba.sk> References: <200907211530.47704.peter.fodrek@stuba.sk> Message-ID: On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek wrote: > Dear conference! > > I have third party regular expression > > self.pattern_main = > re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| > \#\d+\=(?:[+])?\d*(?:\.\d*)?)') Always use raw strings (r"...") when you're working with regular expressions. *Always*. It may happen that you don't need them, as in this case, because Python fails to throw an exception when it meets unknown escape sequences in string literals, but when you do need them you will spend hours before realising it. Also, whoever wrote this regular expression should be taken out and slapped with a wet fish, for it is abominable and unlikely to look much like what you actually want. > with code > > def readline(self): > self.line = self.file_in.readline().rstrip() > if (len(self.line)) : return True > else : return False > > while (self.readline()): > words = self.pattern_main.findall(self.line) Um. Yes. Well. > This handles text file like > > // remark > PL_OFF > PARK > FS > MA 52.8806 , 18.0914 > SS > AD_W > PL_ON > C 52.3955 , -16.1511 , -90.0000 > > It handles file correctly with two exceptions > > 1) omits ',' in the a output You don't do any output, so it's hard to correct that! > 2) omits minus sign in the numbers... > > Would anyone recommend me what to change regular expression to add > thesee two > think to the output,please? It would help considerably if you explained what "it handles file correctly" means, since the regular expresion makes little sense with the input you give. What are you trying to extract from the input, and what do you think you are getting at the moment? -- Rhodri James *-* Wildebeest Herder to the Masses From c.c.wood at gmail.com Tue Jul 21 11:51:08 2009 From: c.c.wood at gmail.com (CCW) Date: Tue, 21 Jul 2009 08:51:08 -0700 (PDT) Subject: Multiple versions of python References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> On 21 July, 15:19, Dave Angel wrote: > ChrisW wrote: > > Hi, > > > I have installed 2 versions of python on my Windows XP computer - I > > originally had 3.0.1, but then found that the MySQL module only > > supported 2.*, so I've now installed that. ?I have found that if I > > change the Windows Environment Variable path, then I can change the > > version of python called when I type 'python' into a command line. > > However, I'd like to be able to choose which version I use. ?I know > > that if I change C:\Python26\python.exe to > > C:\Python26\python2.exe and C:\Python30\python.exe to C: > > \Python26\python3.exe, then typing 'python2' or 'python3' will invoke > > the correct interpreter. ?However, is it safe just to rename the > > executable files? Is there a more elegant way to achieve the same > > task? > > > Thanks, > > Chris > > The elegant way is to have a batch directory on your PATH ( I use ? > m:\t\bat ) ?and put your *.bat files there. ? I do NOT put any python > installations on the PATH. > > For example, I have a batch file called: ? ? m:\t\bat\python26.bat > > c:\progfiles\python26\python.exe %* > > The %* syntax means pass all arguments through to the program. > > Once it all works, you can add an "@" in front of the c: ? in order to > suppress echoing the command. ?At that point, it'll look and work the > same way as what you did, but without modifying anything in the install > directory. ? ?(You may want ?python26.bat, pythonw26.bat, python31.bat > and pythonw31.bat) > > The other thing you may want to do in a batch file is to change the file > associations so that you can run the .py file directly, without typing > "python" or "pythonw" in front of it. > > The relevant Windows commands are: ? ? assoc and ftype ? ? ?And on a > related note, you may want to edit the PATHEXT environment variable, to > add .PY and .PYW Thanks for this - this way made a bit more sense to me. I've now got C:\commands with the 4 .bat files in, and C:\commands in my path. It all seems to work :) I think I've missed the point of the @ though - it doesn't seem to make any difference.. I'm also a bit confused with the associations - since I've got python 2.6 and 3.1, surely the command I type (python26 or python31) is the only way to force a script to be run using a specific interpreter at runtime without having to change the .bat file every time I want to run a script using 3.1 instead of 2.6? From peter.fodrek at stuba.sk Tue Jul 21 11:51:32 2009 From: peter.fodrek at stuba.sk (Peter Fodrek) Date: Tue, 21 Jul 2009 17:51:32 +0200 Subject: Regular expression In-Reply-To: <4A65D5A7.9060908@mrabarnett.plus.com> References: <200907211530.47704.peter.fodrek@stuba.sk> <4A65D5A7.9060908@mrabarnett.plus.com> Message-ID: <86771222-4E27-4E0C-BC56-948169A277BF@stuba.sk> 21.7.2009 v 16:50, MRAB: > Peter Fodrek wrote: >> Dear conference! >> I have third party regular expression >> self.pattern_main = re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d >> +|\(.*?\)| >> \#\d+\=(?:[+])?\d*(?:\.\d*)?)') > [snip] >> It handles file correctly with two exceptions >> 1) omits ',' in the a output >> 2) omits minus sign in the numbers... >> Would anyone recommend me what to change regular expression to add >> thesee two think to the output,please? > > self.pattern_main = re.compile(r'(\s+|,|-?\w\+?\d*(?:\.\d*)?|\w#\d+|\ > (.*?\)|#\d+=\+?\d*(?:\.\d*)?)') Thank you I will try yesterday... Peter From deets at nospam.web.de Tue Jul 21 11:58:47 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 21 Jul 2009 17:58:47 +0200 Subject: Workflow Libraries (DB vs Code) References: <7cm1ifF28jmnhU1@mid.individual.net> Message-ID: <7cm6kmF26g9srU1@mid.uni-berlin.de> Thomas Guettler wrote: > Hi, > > I need to write some simple workflows with web interface. > > For the web stuff I will use django, but I am not sure how > to do the workflow part. Did you consider using OpenERP? It comes with a web-frontend (TG1.0.8-based), but also offers a XMLRPC-server to connect to, so using it from within django shouldn't be any difficult. Workflow-definitions are stored in the DB, and can be generated visually, not sure if they have to though. I'm not very familiar with the system myself, but the EuroPython presentation was truly impressive. Diez From piet at cs.uu.nl Tue Jul 21 11:59:47 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 17:59:47 +0200 Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> M2Crypto package not showing the 'recipient_public_key.pem' file at >j> linux terminal .how do i get/connect with recipient public key. >j> exactly i need to check how can i open this file through linux >j> commands. >j> import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open >j> ('recipient_public_key.pem','rb').read()) print recip; plaintext = >j> whatever i need to encrypt msg = recip.public_encrypt >j> (plaintext,RSA.pkcs1_padding) print msg; >j> after calling the function its not giving any output and even any >j> error >j> i also tried as 'Will' said >j> pk = open('public_key.pem','rb').read() print pk; rsa = >j> M2Crypto.RSA.load_pub_key(pk) >j> whats the mistake i am not getting .will somebody help me out. >j> is something wrong in opening 'recipient_public_key.pem'. is M2Crypto >j> contain this file inbuilt .from where this file taking public key of >j> the recipient i'. what this file contain and how it will work .should >j> i need to create such a file for my purpose.if it is then how can i >j> create this and how it will retrieve the key,where i recognize my >j> recipient to get his public key .is something like database at server. >j> please give me a quick response...... >j> looking for your answer. Please: 1. Type your python code with newlines and proper indentation. 2. Show the error messages that your code gives when you run it. 3. Use proper capital letters at the beginning of your sentences. 4. Don't fire so many questions in rapid succession. The recipient_public_key.pem file is the public key of the recipient which means the person that is going to receive the encrypted message. You should get it from the recipient him/herself or from some key store where s/he has deposited it. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From iwan at reahl.org Tue Jul 21 12:05:31 2009 From: iwan at reahl.org (Iwan Vosloo) Date: Tue, 21 Jul 2009 18:05:31 +0200 Subject: Issue combining gzip and subprocess Message-ID: <1248192331.30167.38.camel@easymoney> Hi there, We tried to gzip the output of a shell command, but this results in a strange error: the resulting file seems to be the concatenation of the plaintext file with the zipped content. For example: f = gzip.open(filename, 'w') subprocess.check_call(['ls','-la'], stdout=f) f.close() Using a normal file works as expected, but a GzipFile results in a file containing what looks like the unzipped data, followed by the zipped data. I suspect this may have something to do with limitations of GzipFile such as it not implementing truncate(). Does anyone have an explanation / can you suggest a nice solution for doing what we are trying to do? Regards - Iwan From nad at acm.org Tue Jul 21 12:08:48 2009 From: nad at acm.org (Ned Deily) Date: Tue, 21 Jul 2009 09:08:48 -0700 Subject: PIL for OSX 64bit References: Message-ID: In article , moerchendiser2k3 wrote: > I have a problem with Python and the Python Image Library to get it > work on OSX 64 bit. Maybe anyone can help me? I started Python in 64 > bit mode and called Image.open(...).load() and got: > > ImportError: The _imaging C module is not installed [...] It's hard to know what your problem is without more information. One potential stumbling block: PIL has dependencies on other open-source libraries, such as libjpeg and freetype. You'll need to have 64-bit versions of them available when building and when running, as well. There are a number of ways to do that. The simplest might be to download pre-built frameworks containing those libraries from here: http://www.kyngchaos.com/software:frameworks There are also build scripts there for building your own versions from scratch. -- Ned Deily, nad at acm.org From sajmikins at gmail.com Tue Jul 21 12:10:57 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 09:10:57 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: On Jul 20, 12:27?pm, Phillip B Oldham wrote: > My colleagues and I have been working with python for around 6 months > now, and while we love a lot of what python has done for us and what > it enables us to do some of the decisions behind such certain > data-types and their related methods baffle us slightly (when compared > to the decisions made in other, similarly powerful languages). > > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > > Consider the following: > > >>> x = [2,1,3] > >>> x.sort() > >>> print x > > [1, 2, 3] > > Now, if the sort operations were unable to affect the original > structure of the list (as in JavaScript) you'd effectively have a > tuple which you could add/remove from, and the example above would > look more like: > > >>> x = [2,1,3] > >>> print x.sort() > [1, 2, 3] > >>> print x > > [2,1,3] > > This make a lot more sense to us, and follows the convention from > other languages. It would also mean chaining methods to manipulate > lists would be easier: > > >>> x = [2,1,3] > >>> print x.sort()[0] > 3 (As others have already pointed out this would print 1, not 3.) > >>> print x > > [2,1,3] > > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. In this case you can use: >>> x = [2,1,3] >>> print min(x) 1 (There's also a max() function.) > We're not looking to start any arguments or religious wars and we're > not asking that python be changed into something its not. We'd simply > like to understand the decision behind the lists and tuple structures. > We feel that in not "getting" the difference between the two types we > may be missing out on using these data structures to their full > potential. One way to think about tuples (as distinct from lists) is as a short- hand version of Pascal's 'record' type, or C's 'struct' (with the caveats that the fields are not named, only indexed, and the types of the fields are implied by use, not explicitly declared.) (FWIW: http://en.wikipedia.org/wiki/Record_(computer_science) ) Broadly speaking, lists are useful for things like stacks and queues, and sorting, while tuples are useful for aggregating heterogeneous data into coherent units, and you can hash them (provided their contents are also hashable.) HTH, ~Simon From stef.mientki at gmail.com Tue Jul 21 12:13:30 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Tue, 21 Jul 2009 18:13:30 +0200 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: <4A65E92A.3050608@gmail.com> hi Marcus, >>> That sounds like a very specialized type of thing, >> Well from an application point of view, >> with the current netbooks, >> this looks like a perfect tool for any conversation or meeting. >>> which only the few people with experience with wxPython, PyAudio, >>> and Scintilla could help you with... >>> >> I was afraid of that too, so I dropped the question in several places, >> and the writer of Scintilla himself came with the perfect answer. >> >> cheers,Stef >>> But you might try having a dictionary with notes and associated >>> times, or just give each note a four-digit ID number at the >>> beginning of it when it's entered and use that in the dictionary (to >>> keep keys shorter). Or you could just do a little hack and increase >>> the number of bookmarks allowed (seeing as source is available) :p >>> >>> Marcus >> > Glad you got a good answer from somebody. Sounds like an interesting > project. About when would this be headed for a release? Could you post > a link to a googlecode or sourceforge project or something so I can > follow and/or help with development? > For the moment it's just an idea, so no line of code yet. I first like to tackle all the problems, at least to the level I think I can handle them. So first solve the next problem, before I start coding: automatic synchronization (file uploading and deleting) between EEE-pc and desktop PC over bluetooth. And another problem, as my customers are physicians, both the text and audio need to be stored encrypted. cheers, Stef > Marcus From fb at frank-buss.de Tue Jul 21 12:44:23 2009 From: fb at frank-buss.de (Frank Buss) Date: Tue, 21 Jul 2009 18:44:23 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <3f1f6d1c-2fb5-44dc-9caa-dc1f80d39e94@b25g2000prb.googlegroups.com> <61cu7a1ra9hf$.1r69rle8f92yp.dlg@40tude.net> Message-ID: <1t9jyo7lzm3ke.hh1blip7i2yp.dlg@40tude.net> Rainer Joswig wrote: > I'm not sure if it is fair to post a reference to a single > post by someone without context and without having used ECL. > If there are stability problems, people can report to the > ECL mailing list. The maintainers are very active. This was just one example. Another one: http://www.mail-archive.com/application-builder at lispniks.com/msg01024.html Luke and Elliott are no beginners, so it is at least difficult to use ECL: On Windows it doesn't work with MinGW and on MacOS X there was at least one case were it freezed. But as I have written, I didn't tried it myself, so this is only some second hand experience. Maybe it is all wrong setups in combination with Lispbuilder and ECL itself is stable, I don't know. But I know that it is much easier to get a full featured running Python system on Windows, MacOS X and Linux, so this is something where Lisp distributions could be improved. -- Frank Buss, fb at frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de From nagle at animats.com Tue Jul 21 12:48:38 2009 From: nagle at animats.com (John Nagle) Date: Tue, 21 Jul 2009 09:48:38 -0700 Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops In-Reply-To: References: Message-ID: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> Ville Vainio wrote: > Has anyone implementing something like what the subject line > indicates? > > The idea: > > To run functions that execute a series of system commands without > blocking the ui, *and* without adding state machine logic. At some level, there's going to be state machine logic. You need interlocking so that the user can't initiate simultaneous conflicting background operations from the GUI. The usual solution is to run the background operations in another thread, and have them put events on a queue read by the GUI task. You also have to work out how to cancel background operations, if they're going to be as big as a "make". Here's the very first application which did this sort of thing, running commands in multiple threads without blocking the user interface thread. This is from 1972. http://www.fourmilab.ch/documents/univac/fang/ And yes, it did properly interlock everything it was doing in the background. John Nagle From piet at cs.uu.nl Tue Jul 21 12:50:00 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 18:50:00 +0200 Subject: Issue combining gzip and subprocess References: Message-ID: >>>>> Iwan Vosloo (IV) wrote: >IV> Hi there, >IV> We tried to gzip the output of a shell command, but this results in a >IV> strange error: the resulting file seems to be the concatenation of the >IV> plaintext file with the zipped content. >IV> For example: >IV> f = gzip.open(filename, 'w') >IV> subprocess.check_call(['ls','-la'], stdout=f) >IV> f.close() >IV> Using a normal file works as expected, but a GzipFile results in a file >IV> containing what looks like the unzipped data, followed by the zipped >IV> data. >IV> I suspect this may have something to do with limitations of GzipFile >IV> such as it not implementing truncate(). >IV> Does anyone have an explanation / can you suggest a nice solution for >IV> doing what we are trying to do? stdout (and the others) must be None, PIPE or a real file object or file descriptor, not a file like object. In your case the solution would be to use PIPE, and then read the output and write in to the GzipFile yourself. f = gzip.open(filename, 'w') proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) while True: line = proc.stdout.readline() if not line: break f.write(line) f.close() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mail at timgolden.me.uk Tue Jul 21 13:07:49 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 21 Jul 2009 18:07:49 +0100 Subject: win32api install problem In-Reply-To: References: Message-ID: <4A65F5E5.3060903@timgolden.me.uk> MCIPERF wrote: > On Jul 20, 9:57 am, Tim Golden wrote: >> Gerry wrote: >>> I'm running Python 2.6 under XP. >>> I've installed Windows 32 extensions for Python 2.6 version 1.4 >>> (pywin32-214.win32-py2.6.exe). >>> But If I try to import win32api, I get: >>> File "C:\python_projects\euler\driveletters.py", line 1, in >>> import win32api >>> ImportError: DLL load failed: The specified module could not be found. >> Used to be you'd get this error if you installed as a >> non-admin user. Don't know if that's still an issue. >> Possibility? >> >> TJG > > Not a possibility (afaict) -- I am an administrator, according to the > control panel. Not too many ideas, then, I'm afraid. Apart from the obvious -- uninstall and try again -- how about running Python with the -vv option: python -vv to get more information about what it's trying to get, and/or running sysinternal's Process Explorer at the same time to spot the DLL Dependencies. I have seen a (not too similar) problem when running TortoiseHg and the pywin32 extensions, since the shell import's TortoiseHg's version of various pywin32 DLLs before I try to import them myself for some other purpose, and the two versions clashed dramatically (crashed Explorer hard, I seem to remember). Just in case that rings any bells.. TJG From jeanmichel at sequans.com Tue Jul 21 13:22:05 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 21 Jul 2009 19:22:05 +0200 Subject: Regular expression In-Reply-To: References: <200907211530.47704.peter.fodrek@stuba.sk> Message-ID: <4A65F93D.8000908@sequans.com> Rhodri James wrote: > On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek > wrote: > >> Dear conference! >> >> I have third party regular expression >> >> self.pattern_main = >> re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)| >> \#\d+\=(?:[+])?\d*(?:\.\d*)?)') > > Also, whoever wrote this regular expression should be taken > out and slapped with a wet fish, for it is abominable and > unlikely to look much like what you actually want. Here, take that fish and please, hit really hard ! ** ;'-. `;-._ ) '---.._ > `-.__.-' `'.__ /_.-'-._ _, ^ ---) ` `'------/_.'----`` JM ** From tfb at cley.com Tue Jul 21 13:41:59 2009 From: tfb at cley.com (Tim Bradshaw) Date: Tue, 21 Jul 2009 18:41:59 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> Message-ID: <2009072118415916807-tfb@cleycom> On 2009-07-19 19:31:36 +0100, Frank Buss said: > (e.g. I > don't know of a free modern and stable Lisp implemenation with > mulithreading support for Windows, with a licence with which you can use it > in closed source commercial programs, like you can do with Python). Openmcl seems reasonably stable to me, is LLGPL-licensed, and runs on these platforms and Solaris x86. It's kind of tragic, of course, that this kind of parasitic behaviour (will not consider a commercial product to use in your commercial system) has now become so common. From robert.kern at gmail.com Tue Jul 21 13:52:23 2009 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 21 Jul 2009 12:52:23 -0500 Subject: clean way prepend an element to a numpy array In-Reply-To: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> Message-ID: On 2009-07-20 22:56, bdb112 wrote: > If I want to add an element at the beginning of an array, it seems > like I must make a list, insert in place, then make an array again. the_array = numpy.concatenate([new_value, the_array]) You will want to ask numpy questions on the numpy mailing list. http://www.scipy.org/Mailing_Lists -- 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 rhodri at wildebst.demon.co.uk Tue Jul 21 13:59:32 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 21 Jul 2009 18:59:32 +0100 Subject: Regular expression In-Reply-To: <1AE4BD36-9955-4F80-9FBC-6DF62F529CC3@stuba.sk> References: <200907211530.47704.peter.fodrek@stuba.sk> <1AE4BD36-9955-4F80-9FBC-6DF62F529CC3@stuba.sk> Message-ID: On Tue, 21 Jul 2009 17:12:53 +0100, Peter Fodrek wrote: > 21.7.2009 v 17:39, Rhodri James: > >> On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek > > wrote: [snipped for space] >>> This handles text file like >>> >>> // remark >>> PL_OFF >>> PARK >>> FS >>> MA 52.8806 , 18.0914 >>> SS >>> AD_W >>> PL_ON >>> C 52.3955 , -16.1511 , -90.0000 >>> >>> It handles file correctly with two exceptions >>> >>> 1) omits ',' in the a output >> >> You don't do any output, so it's hard to correct that! > > for line > > C 52.3955 , -16.1511 , -90.0000 > > it returns (as I remember) > > ('C', ' ', '52.3955', ' ', ' ', ' 16.1511', ' ', ' ','90.0') > > I was to get (number and positions of empty strings are not related to > me. I can add, remove then as you wish) > > ('C', ' ', '52.3955', ' ', ' ,', ' -16.1511', ' ', ' ,','-90.0') If whitespace isn't an issue and the format really is as simple as your example, you don't need the full power of a regular expression here. split() would do the trick: words = self.line.split() will return what you want for that example. It will produce different (but more consistent) results than the regular expression for other lines, though: for example, MA 52.8806 , 18.0914 when put through the regular expression (if fixed for commas) would give you ('M', 'A', ' ', '52.8806', ' ', ',', ' ', '18.0914') as against ['MA', '52.8806', ',', '18.0914'] > I use eval call in python to do format conversion, so just parsing is > problem float() might be better than eval(), if you know for sure that you are expecting floats. -- Rhodri James *-* Wildebeest Herder to the Masses From fred at bsdhost.net Tue Jul 21 14:12:01 2009 From: fred at bsdhost.net (Fred C) Date: Tue, 21 Jul 2009 11:12:01 -0700 Subject: setuptools question. Message-ID: I have a python program and when I install this program from the module home directory using setup.py everything works fine. But easy_install fails with the following error, because I am trying to install some start up shell scripts into /etc/init.d > The package setup script has attempted to modify files on your system >that are not within the EasyInstall build area, and has been aborted. Is there a way to use easy_install to install the start scripts into / etc/init.d. If not, what is the best way to install these scripts. Thanks -fred- From hyugaricdeau at gmail.com Tue Jul 21 14:13:35 2009 From: hyugaricdeau at gmail.com (Hyuga) Date: Tue, 21 Jul 2009 11:13:35 -0700 (PDT) Subject: are user defined classes hashable? References: <373d6c69-6965-4a88-bdd2-8028ef850bf8@k6g2000yqn.googlegroups.com> Message-ID: On Jul 20, 10:53?pm, a... at pythoncraft.com (Aahz) wrote: > In article <373d6c69-6965-4a88-bdd2-8028ef850... at k6g2000yqn.googlegroups.com>, > > Hyuga ? wrote: > > >Regardless, Nicolas's example can be applied to the class too: > > >>>> class Foo(object): > > ? ?pass > > >>>> hash(Foo) > >11443104 > >>>> id(Foo) > >11443104 > > >class objects are just objects of type 'type'. > > Not quite. ?They certainly default that way, but changing the metaclass > changes a class's type:: > > class M(type): > ? ? pass > > class C(object): > ? ? pass > > class C2(object): > ? ? __metaclass__ = M > > print type(C) > print type(C2) Well, okay, you got me there. But the OP wasn't asking about classes with different metaclasses. And besides, type(type(C2)) is still type ;) From tjreedy at udel.edu Tue Jul 21 14:26:39 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 21 Jul 2009 14:26:39 -0400 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: References: Message-ID: Nick Craig-Wood wrote: > > I tried it on a program I wrote to solve a puzzle (Rush Hour). > (Actually it solves the meta-puzzle - trying to make the hardest > possible Rush Hour puzzle.) > > After a bit of twiddling (remove psyco and profiling) I got it to > start compiling, but unfortunately it compiled for about an hour (in > iterative type analysis..) used up 600 MB of RAM printing an '*' every > 10 minutes or so then gave an error message and gave up. > > Unfortunately I shut the window by accident, but the error message was > something about not being able to resolve types I think with a list of > 20 or so unresolved types. > > Can you give a hint as to how I debug this? I presume my program has > some instances of non static types which is causing the problem, but > it is going to be a very long debugging session if it takes me an hour > each cycle ;-) > > The program is about 700 lines of python (excluding comments). Split it into pieces and compile each separately. Recurse. tjr From tjreedy at udel.edu Tue Jul 21 14:57:57 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 21 Jul 2009 14:57:57 -0400 Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops In-Reply-To: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> References: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> Message-ID: > Ville Vainio wrote: >> Has anyone implementing something like what the subject line >> indicates? Your subject line is so long that it is cut off even on my wide screen. Better to repeat the question in the body. From martin.hellwig at dcuktec.org Tue Jul 21 15:05:55 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Tue, 21 Jul 2009 20:05:55 +0100 Subject: Running a Python Service under the LocalService or NetworkService Account In-Reply-To: <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> Message-ID: <-vadnaHpJMoIjPvXnZ2dnUVZ8tli4p2d@bt.com> David Adamo Jr. wrote: > On Jul 21, 10:40 am, "Martin P. Hellwig" > wrote: >> sightseer wrote: >> >> >> >>> Error Installing Service: Access is Denied. (5) >> >> Are you trying to do this on windows vista? >> >> -- >> MPHhttp://blog.dcuktec.com >> 'If consumed, best digested with added seasoning to own preference.' > > Yeah, I was trying to do it on Vista. Someone has just helped me out. > I had to deactivate User Account Control on Windows Vista...and now > everything is rosy. Thanks guys. No need to deactivate it, just right click on the command shell program and say run as administrator, than you can install the service via the command line. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From lukepadawan at gmail.com Tue Jul 21 15:21:50 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Tue, 21 Jul 2009 16:21:50 -0300 Subject: List insertion cost Message-ID: <4A66154E.1050200@gmail.com> Hello, I would like to know how much it costs to insert an element into a list using this operation: a[2:2] = [ 1 ] i. e, what is the complexity of the operation above (given that len(a) = n)? Thanks. From daniel at stutzbachenterprises.com Tue Jul 21 15:35:55 2009 From: daniel at stutzbachenterprises.com (Daniel Stutzbach) Date: Tue, 21 Jul 2009 14:35:55 -0500 Subject: List insertion cost In-Reply-To: <4A66154E.1050200@gmail.com> References: <4A66154E.1050200@gmail.com> Message-ID: On Tue, Jul 21, 2009 at 2:21 PM, Lucas P Melo wrote: > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? > O(n) If you want O(log n), you can use the blist extension type from http://pypi.python.org/pypi/blist/ -- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC -------------- next part -------------- An HTML attachment was scrubbed... URL: From king.seth at gmail.com Tue Jul 21 15:37:12 2009 From: king.seth at gmail.com (Seth) Date: Tue, 21 Jul 2009 12:37:12 -0700 (PDT) Subject: Pyserial and pyQt Message-ID: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> I have used pyserial in the past but this is my first experience with pyQt. I am using the Python xy package for windows current but might move to linux. I have a small device that is outputting a basic text string. I want to be able to read this string(from the comm port) and update a text box and eventually a graph in pyQt. I can't find any documentation or tutorials on how to do this. If anyone can point me in the right direction or give me some tips I would be grateful. Thanks, Seth From robert.kern at gmail.com Tue Jul 21 15:43:41 2009 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 21 Jul 2009 14:43:41 -0500 Subject: List insertion cost In-Reply-To: <4A66154E.1050200@gmail.com> References: <4A66154E.1050200@gmail.com> Message-ID: On 2009-07-21 14:21, Lucas P Melo wrote: > Hello, > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? O(n). Python lists are contiguous arrays in memory, and everything after the insertion point needs to be moved. Raymond Hettinger has a good talk about the implementation of Python lists and other container objects. http://www.youtube.com/watch?v=hYUsssClE94 http://www.pycon.it/static/stuff/slides/core-python-containers-under-hood.ppt -- 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 lukepadawan at gmail.com Tue Jul 21 16:47:26 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Tue, 21 Jul 2009 17:47:26 -0300 Subject: List insertion cost In-Reply-To: References: <4A66154E.1050200@gmail.com> Message-ID: <4A66295E.9070004@gmail.com> Robert Kern wrote: > O(n). Python lists are contiguous arrays in memory, and everything > after the insertion point needs to be moved. Raymond Hettinger has a > good talk about the implementation of Python lists and other container > objects. > > http://www.youtube.com/watch?v=hYUsssClE94 > http://www.pycon.it/static/stuff/slides/core-python-containers-under-hood.ppt > Thanks. :) From ryniek90 at gmail.com Tue Jul 21 16:55:18 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Tue, 21 Jul 2009 22:55:18 +0200 Subject: Changing the private variables content In-Reply-To: References: Message-ID: <4A662B36.3010708@gmail.com> Hi. I'm writing some class, and decided to use inside private method and some private variables. While with method i haven't got any problem's with variables i have. Maybe some example. A class with private method and private variable: " >>> class Secret(object): # def __init__(self): self._number = 1 # def _secret(self): print self._number def showit(self): print "Secret number is:\n" self._secret() >>> sec = Secret() >>> sec.showit() Secret number is: 1 >>> sec._number 1 >>> sec._number = 2 >>> sec._number 2 >>> sec._number += 3 >>> sec._number 5 >>> " As You can see, i made class with private method and private variable inside __init__ constructor. I've changed also the variable value, but outside class. I've got problem with changing some variable value _inside__my_ class, which i'm writing. I've searched over google, some tuts with topics about operations on private variables, but didn't found anything - only how to make them, but no how to assign new objects/override them with new content (and other advanced and helpful options). If someone could help me, it would be great. Thanks. From davidj411 at gmail.com Tue Jul 21 17:00:51 2009 From: davidj411 at gmail.com (davidj411) Date: Tue, 21 Jul 2009 14:00:51 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order Message-ID: I am using a recursive function to print the time and a few other things on each pass. ( the function calculates size of file that is being transferred and if not 100 % copied, it waits 20 secs and checks again). i would expect the time to be correct anytime it is used: <--code below -->> print time.strftime('%m-%d-%Y %H:%m:%S') <--code above -->> here is an example of what i am seeing: 16:07:16 16:07:36 16:07:56 16:07:16 16:07:36 16:07:56 16:07:16 16:07:36 16:07:56 From lukepadawan at gmail.com Tue Jul 21 17:08:07 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Tue, 21 Jul 2009 18:08:07 -0300 Subject: Balanced binary tree implementation Message-ID: <4A662E37.6090406@gmail.com> Hello, I would like to use a balanced binary tree implementation (preferably within some API). Any hints about where I could find it? I am looking for something that implements insertion, deletion, search and a special search that returns the lesser element bigger than a given key [1]. A nice possibility would be an extensible API that allows me to inherit its classes and to add operations myself. Thanks in advance. [1] Ex: 1 2 3 4 5 6 are elements of the bbt. If I use this operation given 4 as the parameter, the value returned would be 5. From joncle at googlemail.com Tue Jul 21 17:09:31 2009 From: joncle at googlemail.com (Jon Clements) Date: Tue, 21 Jul 2009 14:09:31 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: <025efda8-0289-4d7d-a9e8-c90faa445c22@s31g2000yqs.googlegroups.com> On 21 July, 22:00, davidj411 wrote: > I am using a recursive function to print the time and a few other > things on each pass. ( the function calculates size of file that is > being transferred and if not 100 % copied, it waits 20 secs and checks > again). > > i would expect the time to be correct anytime it is used: > > <--code below -->> > print time.strftime('%m-%d-%Y %H:%m:%S') > <--code above -->> > > here is an example of what i am seeing: > > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 I assume month, day and year are actually being output and that you've removed it from your post. Err, what else do you expect to happen if you're doing this recursively? From gherron at islandtraining.com Tue Jul 21 17:14:44 2009 From: gherron at islandtraining.com (Gary Herron) Date: Tue, 21 Jul 2009 14:14:44 -0700 Subject: Changing the private variables content In-Reply-To: <4A662B36.3010708@gmail.com> References: <4A662B36.3010708@gmail.com> Message-ID: <4A662FC4.2050804@islandtraining.com> Ryniek90 wrote: > Hi. > I'm writing some class, and decided to use inside private method and > some private variables. While with method i haven't got any problem's > with variables i have. > Maybe some example. > A class with private method and private variable: > > " > >>> class Secret(object): > # > def __init__(self): > self._number = 1 > # > def _secret(self): > print self._number > def showit(self): > print "Secret number is:\n" > self._secret() > > >>> sec = Secret() > >>> sec.showit() > Secret number is: > > 1 > >>> sec._number > 1 > >>> sec._number = 2 > >>> sec._number > 2 > >>> sec._number += 3 > >>> sec._number > 5 > >>> > " > > As You can see, i made class with private method and private variable > inside __init__ constructor. I've changed also the variable value, but > outside class. > I've got problem with changing some variable value _inside__my_ class, > which i'm writing. Not sure this is what you are asking, but a method (which is how I interpret "_inside__my_ class") changes the value by normal assignment like this: class Secret(object): ... def SetNumber(self,value): self._number = value Is that what you were asking? Gary Herron > I've searched over google, some tuts with topics about operations on > private variables, but didn't found anything - only how to make them, > but no how to assign new objects/override them with new content (and > other advanced and helpful options). > > If someone could help me, it would be great. > > Thanks. From sajmikins at gmail.com Tue Jul 21 17:29:14 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 14:29:14 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> On Jul 21, 5:00?pm, davidj411 wrote: > I am using a recursive function to print the time and a few other > things on each pass. ( the function calculates size of file that is > being transferred and if not 100 % copied, it waits 20 secs and checks > again). > > i would expect the time to be correct anytime it is used: > > <--code below -->> > print time.strftime('%m-%d-%Y %H:%m:%S') > <--code above -->> > > here is an example of what i am seeing: > > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 > 16:07:16 > 16:07:36 > 16:07:56 Your output doesn't match your format string: In [1]: import time In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') 07-21-2009 17:07:16 There's no way to tell why your output times seem to repeat without seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: %m:%S')" line. From piet at cs.uu.nl Tue Jul 21 17:38:43 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 21 Jul 2009 23:38:43 +0200 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: >>>>> davidj411 (d) wrote: >d> I am using a recursive function to print the time and a few other >d> things on each pass. ( the function calculates size of file that is >d> being transferred and if not 100 % copied, it waits 20 secs and checks >d> again). >d> i would expect the time to be correct anytime it is used: >d> <--code below -->> >d> print time.strftime('%m-%d-%Y %H:%m:%S') >d> <--code above -->> >d> here is an example of what i am seeing: >d> 16:07:16 >d> 16:07:36 >d> 16:07:56 >d> 16:07:16 >d> 16:07:36 >d> 16:07:56 >d> 16:07:16 >d> 16:07:36 >d> 16:07:56 You probably meant: print time.strftime('%m-%d-%Y %H:%M:%S') -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jeanmichel at sequans.com Tue Jul 21 17:42:41 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 21 Jul 2009 23:42:41 +0200 Subject: doted filenames in import statements Message-ID: <4A663651.5020006@sequans.com> Hi fellows, I'd like to use the dynamic __import__ statement. It works pretty well with non dotted names, but I cannot figure how to make it work with dotted file paths. example: file = "/home/dsp/test.py" test = __import__(file) works like a charm file = "/home/dsp/4.6.0.0/test.py" test = __import__(file) => no module name blalalal found. Any suggestion ? I tried multiple escape technics without any success. JM From joncle at googlemail.com Tue Jul 21 17:52:57 2009 From: joncle at googlemail.com (Jon Clements) Date: Tue, 21 Jul 2009 14:52:57 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: Message-ID: <577639e9-b64a-4a8e-8d90-b70ca491596b@18g2000yqa.googlegroups.com> On 21 July, 22:38, Piet van Oostrum wrote: > >>>>> davidj411 (d) wrote: > >d> I am using a recursive function to print the time and a few other > >d> things on each pass. ( the function calculates size of file that is > >d> being transferred and if not 100 % copied, it waits 20 secs and checks > >d> again). > >d> i would expect the time to be correct anytime it is used: > >d> <--code below -->> > >d> print time.strftime('%m-%d-%Y %H:%m:%S') > >d> <--code above -->> > >d> here is an example of what i am seeing: > >d> 16:07:16 > >d> 16:07:36 > >d> 16:07:56 > >d> 16:07:16 > >d> 16:07:36 > >d> 16:07:56 > >d> 16:07:16 > >d> 16:07:36 > >d> 16:07:56 > > You probably meant: print time.strftime('%m-%d-%Y %H:%M:%S') > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Good spot! From davidj411 at gmail.com Tue Jul 21 17:53:29 2009 From: davidj411 at gmail.com (davidj411) Date: Tue, 21 Jul 2009 14:53:29 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: On Jul 21, 5:29?pm, Simon Forman wrote: > On Jul 21, 5:00?pm, davidj411 wrote: > > > > > > > I am using a recursive function to print the time and a few other > > things on each pass. ( the function calculates size of file that is > > being transferred and if not 100 % copied, it waits 20 secs and checks > > again). > > > i would expect the time to be correct anytime it is used: > > > <--code below -->> > > print time.strftime('%m-%d-%Y %H:%m:%S') > > <--code above -->> > > > here is an example of what i am seeing: > > > 16:07:16 > > 16:07:36 > > 16:07:56 > > 16:07:16 > > 16:07:36 > > 16:07:56 > > 16:07:16 > > 16:07:36 > > 16:07:56 > > Your output doesn't match your format string: > > In [1]: import time > > In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') > 07-21-2009 17:07:16 > > There's no way to tell why your output times seem to repeat without > seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: > %m:%S')" line. sorry, yes, i did manually filter the output. here is the function: def log_out(msg,servername='std.out'): print msg open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') on each pass, it should output the newer time (whether recursive or not, right) ? From clp2 at rebertia.com Tue Jul 21 17:59:17 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 21 Jul 2009 14:59:17 -0700 Subject: doted filenames in import statements In-Reply-To: <4A663651.5020006@sequans.com> References: <4A663651.5020006@sequans.com> Message-ID: <50697b2c0907211459w88c3e28y2a96811bd307233c@mail.gmail.com> On Tue, Jul 21, 2009 at 2:42 PM, Jean-Michel Pichavant wrote: > Hi fellows, > > I'd like to use the dynamic __import__ statement. It works pretty well with > non dotted names, but I cannot figure how to make it work with dotted file > paths. > > example: > > file = "/home/dsp/test.py" > test = __import__(file) > > works like a charm > > file = "/home/dsp/4.6.0.0/test.py" > test = __import__(file) > => no module name blalalal found. > > Any suggestion ? I tried multiple escape technics without any success. You want the imp.load_module() function: http://docs.python.org/library/imp.html#imp.load_module __import__() only operates on module/package names. I'm not sure how you even got it to work with a filename... Cheers, Chris -- http://blog.rebertia.com From rhodri at wildebst.demon.co.uk Tue Jul 21 18:00:39 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 21 Jul 2009 23:00:39 +0100 Subject: Changing the private variables content In-Reply-To: <4A662B36.3010708@gmail.com> References: <4A662B36.3010708@gmail.com> Message-ID: On Tue, 21 Jul 2009 21:55:18 +0100, Ryniek90 wrote: > Hi. > I'm writing some class, and decided to use inside private method and > some private variables. While with method i haven't got any problem's > with variables i have. There is no mechanism in Python that makes attributes truly private. self._number is an attribute just like any other, the whole business with _leading_underscores is purely a matter of convention. If you have an instance attribute or method with a leading underscore, you know that using it or calling it isn't something you're supposed to do outside its class, but nothing will stop you doing exactly that if you're rude enough to try. Does that help? -- Rhodri James *-* Wildebeest Herder to the Masses From piet at cs.uu.nl Tue Jul 21 18:01:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 00:01:35 +0200 Subject: Balanced binary tree implementation References: Message-ID: >>>>> Lucas P Melo (LPM) wrote: >LPM> Hello, >LPM> I would like to use a balanced binary tree implementation (preferably >LPM> within some API). >LPM> Any hints about where I could find it? >LPM> I am looking for something that implements insertion, deletion, search and >LPM> a special search that returns the lesser element bigger than a given key >LPM> [1]. >LPM> A nice possibility would be an extensible API that allows me to inherit its >LPM> classes and to add operations myself. >LPM> Thanks in advance. >LPM> [1] Ex: 1 2 3 4 5 6 are elements of the bbt. If I use this operation given >LPM> 4 as the parameter, the value returned would be 5. http://newcenturycomputers.net/projects/rbtree.html (warning: see end of page!) http://pypi.python.org/pypi/rbtree/ http://pyavl.sourceforge.net/ http://code.activestate.com/recipes/576817/ -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at mrabarnett.plus.com Tue Jul 21 18:04:39 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 21 Jul 2009 23:04:39 +0100 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order In-Reply-To: References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: <4A663B77.3020408@mrabarnett.plus.com> davidj411 wrote: > On Jul 21, 5:29 pm, Simon Forman wrote: >> On Jul 21, 5:00 pm, davidj411 wrote: >> >> >> >> >> >>> I am using a recursive function to print the time and a few other >>> things on each pass. ( the function calculates size of file that is >>> being transferred and if not 100 % copied, it waits 20 secs and checks >>> again). >>> i would expect the time to be correct anytime it is used: >>> <--code below -->> >>> print time.strftime('%m-%d-%Y %H:%m:%S') >>> <--code above -->> >>> here is an example of what i am seeing: >>> 16:07:16 >>> 16:07:36 >>> 16:07:56 >>> 16:07:16 >>> 16:07:36 >>> 16:07:56 >>> 16:07:16 >>> 16:07:36 >>> 16:07:56 >> Your output doesn't match your format string: >> >> In [1]: import time >> >> In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') >> 07-21-2009 17:07:16 >> >> There's no way to tell why your output times seem to repeat without >> seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: >> %m:%S')" line. > > sorry, yes, i did manually filter the output. > > here is the function: > > def log_out(msg,servername='std.out'): > print msg > open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" > + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') > > on each pass, it should output the newer time (whether recursive or > not, right) ? Maybe it does, but you were outputting the month ("07") instead of the minutes; the seconds were changing. From piet at cs.uu.nl Tue Jul 21 18:11:38 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 00:11:38 +0200 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: >>>>> davidj411 (d) wrote: >>> >>> > <--code below -->> >>> > print time.strftime('%m-%d-%Y %H:%m:%S') >>> > <--code above -->> >d> here is the function: >d> def log_out(msg,servername='std.out'): >d> print msg >d> open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" >d> + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') >d> on each pass, it should output the newer time (whether recursive or >d> not, right) ? How come your strftime is now different from the first posting? Are you cheating? If you want to ask a question about why your code is not working properly there are a few important rules: 1. Copy and paste your code, and at least all relevant stuff (preferably a minimal example that shows the problem). *DO NOT RETYPE THE CODE* 2. Copy and paste the output. *DO NOT RETYPE THE OUTPUT* 3. Tell what the expected or desired output is. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Tue Jul 21 18:29:52 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 00:29:52 +0200 Subject: doted filenames in import statements References: Message-ID: >>>>> Jean-Michel Pichavant (JP) wrote: >JP> Hi fellows, >JP> I'd like to use the dynamic __import__ statement. It works pretty well with >JP> non dotted names, but I cannot figure how to make it work with dotted file >JP> paths. What is a dotted file path? Is that 4.6.0.0? >JP> example: >JP> file = "/home/dsp/test.py" >JP> test = __import__(file) >JP> works like a charm That's not supposed to work. In older pythons it did work but that's a bug. In newer pythons it doesn't. __import__ works on module names, not on file names. Python 2.6: >>> spam = __import__('/Users/piet/TEMP/test.py', globals(), locals(), [], -1) Traceback (most recent call last): File "", line 1, in ImportError: Import by filename is not supported. >>> >JP> file = "/home/dsp/4.6.0.0/test.py" >JP> test = __import__(file) >JP> => no module name blalalal found. >JP> Any suggestion ? I tried multiple escape technics without any success. Rightly so. I think the best would be to add the directory to sys.path sys.path.add('/home/dsp/4.6.0.0') and then __import__('test', ... ) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From emile at fenx.com Tue Jul 21 18:37:40 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 21 Jul 2009 15:37:40 -0700 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <7xiqho1c2i.fsf@ruckus.brouhaha.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <7xiqho1c2i.fsf@ruckus.brouhaha.com> Message-ID: On 7/19/2009 4:35 PM Paul Rubin said... > Emile van Sebille writes: >>>> Most people would still consider Solaris to be a "major platform". >>> ?? I do not, but I have no idea what comes in 4th after the other >>> three by whatever metric. >> one metric calls fourth as the iPhone OS... >> http://marketshare.hitslink.com/report.aspx?qprid=8... > > That metric is clearly wrong. The #1 platform OS platform in terms of > number of units shipped is Javacard, which is in the SIM cards of > billions of GSM phones. Yes -- that location includes disclaimers on how they reach their conclusions. From lists at cheimes.de Tue Jul 21 18:48:40 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 22 Jul 2009 00:48:40 +0200 Subject: doted filenames in import statements In-Reply-To: <50697b2c0907211459w88c3e28y2a96811bd307233c@mail.gmail.com> References: <4A663651.5020006@sequans.com> <50697b2c0907211459w88c3e28y2a96811bd307233c@mail.gmail.com> Message-ID: Chris Rebert wrote: > You want the imp.load_module() function: > http://docs.python.org/library/imp.html#imp.load_module > > __import__() only operates on module/package names. I'm not sure how > you even got it to work with a filename... It used to work with filenames but it was a bug. I guess several people are swearing curses on me for removing this 'feature'. *g* Christian From ebonak at hotmail.com Tue Jul 21 19:06:01 2009 From: ebonak at hotmail.com (Esmail) Date: Tue, 21 Jul 2009 19:06:01 -0400 Subject: comments? storing a function in an object In-Reply-To: References: Message-ID: Thanks *everyone* for your help, and sharing your knowledge. I had no idea that Python functions could have user defined attributes, and I may now not even need the object to wrap my functions in. Amazing what one can learn here .. thanks again! Esmail From nyamatongwe+thunder at gmail.com Tue Jul 21 19:06:02 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Tue, 21 Jul 2009 23:06:02 GMT Subject: If Scheme is so good why MIT drops it? In-Reply-To: <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: milanj: > and all of them use native threads (python still use green threads ?) Python uses native threads. Neil From david at boddie.org.uk Tue Jul 21 19:24:32 2009 From: david at boddie.org.uk (David Boddie) Date: Wed, 22 Jul 2009 01:24:32 +0200 Subject: Pyserial and pyQt References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> Message-ID: On Tuesday 21 July 2009 21:37, Seth wrote: > I have used pyserial in the past but this is my first experience with > pyQt. I am using the Python xy package for windows current but might > move to linux. I have a small device that is outputting a basic text > string. I want to be able to read this string(from the comm port) and > update a text box and eventually a graph in pyQt. I can't find any > documentation or tutorials on how to do this. If anyone can point me > in the right direction or give me some tips I would be grateful. It seems that someone has already asked a similar question on Stack Overflow, though perhaps you should start with a simpler solution and look at more advanced ones later: http://stackoverflow.com/questions/771988/pyqt4-and-pyserial One starting point is this list of tutorials on the PyQt and PyKDE Wiki: http://www.diotavelli.net/PyQtWiki/Tutorials Later, when you want to draw graphs, you might find PyQwt useful: http://pyqwt.sourceforge.net/ You may already be aware that there's also a mailing list for PyQt and PyKDE: http://www.riverbankcomputing.com/pipermail/pyqt/ Another way to get answers to questions is to join the #pyqt IRC channel at freenode.net: irc://irc.freenode.net/ David From david.lyon at preisshare.net Tue Jul 21 19:48:24 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 21 Jul 2009 19:48:24 -0400 Subject: setuptools question. In-Reply-To: References: Message-ID: <21107bd58153b0a10d52ce5d3d9b70db@preisshare.net> On Tue, 21 Jul 2009 11:12:01 -0700, Fred C wrote: > I have a python program and when I install this program from the > module home directory using setup.py everything works fine. > > But easy_install fails with the following error, because I am trying > to install some start up shell scripts into /etc/init.d > > > The package setup script has attempted to modify files on your system > >that are not within the EasyInstall build area, and has been aborted. > > Is there a way to use easy_install to install the start scripts into / > etc/init.d. If not, what is the best way to install these scripts. What package is it? what platform ? From sajmikins at gmail.com Tue Jul 21 19:59:21 2009 From: sajmikins at gmail.com (Simon Forman) Date: Tue, 21 Jul 2009 16:59:21 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> Message-ID: <16e1d810-bdf0-46b8-aef1-7e57eeb30547@k19g2000yqn.googlegroups.com> On Jul 21, 5:53?pm, davidj411 wrote: > On Jul 21, 5:29?pm, Simon Forman wrote: > > > > > On Jul 21, 5:00?pm, davidj411 wrote: > > > > I am using a recursive function to print the time and a few other > > > things on each pass. ( the function calculates size of file that is > > > being transferred and if not 100 % copied, it waits 20 secs and checks > > > again). > > > > i would expect the time to be correct anytime it is used: > > > > <--code below -->> > > > print time.strftime('%m-%d-%Y %H:%m:%S') > > > <--code above -->> > > > > here is an example of what i am seeing: > > > > 16:07:16 > > > 16:07:36 > > > 16:07:56 > > > 16:07:16 > > > 16:07:36 > > > 16:07:56 > > > 16:07:16 > > > 16:07:36 > > > 16:07:56 > > > Your output doesn't match your format string: > > > In [1]: import time > > > In [2]: print time.strftime('%m-%d-%Y %H:%m:%S') > > 07-21-2009 17:07:16 > > > There's no way to tell why your output times seem to repeat without > > seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H: > > %m:%S')" line. > > sorry, yes, i did manually filter the output. > > here is the function: > > def log_out(msg,servername='std.out'): > ? ? ? ? print msg > ? ? ? ? open(log_dir + '\\' + servername + ".log",'a').write(servername + "-" > + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n') > > on each pass, it should output the newer time (whether recursive or > not, right) ? Well, as Piet van Oostrum pointed out, your problem in the first code you posted was that you used '%m' rather than '%M' for the minutes. (Good eye Van Oostrum!) But now in this function you seem to have the correct '%M' field. Are you still having the same output after changing that? In any event, here's a rewritten version of that function that's a little cleaner, FWIW. from os.path import join from time import strftime format = '%m-%d-%Y %H:%M:%S' def log_out(msg, servername='std.out'): print msg msg = "%s - %s %s\n" % (servername, strftime(format), msg) log_file = open(join(log_dir, servername + ".log"), 'a') try: log_file.write(msg) finally: log_file.close() But why not just use the logging module? From mail.to.daniel.platz at googlemail.com Tue Jul 21 20:00:13 2009 From: mail.to.daniel.platz at googlemail.com (Daniel Platz) Date: Tue, 21 Jul 2009 17:00:13 -0700 (PDT) Subject: Fast reading and unpacking of binary data (struct module) Message-ID: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Hi, I have a Python newbie question about reading data from a binary file. I have an huge binary file from an external program. I want to read and process the data in this file in a reasonable time. It turns out that the reading of the data itself and the processing do not need most of the time. However, when using the read(bytes) method Python returns a string representing the binary information in hex. This string I have to "cast/translate" into a number (in my case a signed short). For this I am using the method struct.unpack from the struct module. This unpacking part of the program takes by far the most time. Is there a way to speed this up or to do it the unpacking more cleverly than with the struct module? Thanks in advance. With kind regards, Daniel From ronn.ross at gmail.com Tue Jul 21 20:08:57 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Tue, 21 Jul 2009 19:08:57 -0500 Subject: trouble with minidom Message-ID: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Hello I'm trying to read an xml file using minidome. The xml looks like: myProj /here/ My code looks like so: from xml.dom.minidom import parse dom = parse("myfile.xml") for node in dom.getElementsByTagName("project'): print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, node.childNodes[1]) Unfortunately, it returns 'nodeValue as none. I'm trying to read the value out of the node fir example name: myProj. I haven't found much help in the documentation. Can someone point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Tue Jul 21 20:18:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 21:18:42 -0300 Subject: Fast reading and unpacking of binary data (struct module) References: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Message-ID: En Tue, 21 Jul 2009 21:00:13 -0300, Daniel Platz escribi?: > I have an huge binary file from an external program. I want to read > and process the data in this file in a reasonable time. It turns out > that the reading of the data itself and the processing do not need > most of the time. However, when using the read(bytes) method Python > returns a string representing the binary information in hex. This > string I have to "cast/translate" into a number (in my case a signed > short). For this I am using the method struct.unpack from the struct > module. This unpacking part of the program takes by far the most time. > Is there a way to speed this up or to do it the unpacking more > cleverly than with the struct module? Try creating a Struct object with your format and use its unpack() method. http://docs.python.org/library/struct.html#struct-objects If your format consists of just integers, probably an array is more efficient: http://docs.python.org/library/array.html#array.array.fromfile -- Gabriel Genellina From greg at cosc.canterbury.ac.nz Tue Jul 21 20:18:44 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:18:44 +1200 Subject: tough-to-explain Python In-Reply-To: <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7cimf0F27u23eU1@mid.individual.net> <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> Message-ID: <7cn45nF28j1d4U1@mid.individual.net> Simon Forman wrote: > My understanding (so far) is that you (hope to) /derive/ correct code > using formal logic, rather than writing code and then proving its > soundness. But to the extent you can rigorously formalise it, all you've done is create Yet Another Programming Language. Which is fine, if you can come up with one that works at a high enough level in some domain that you can see just by looking that the program will do what you want. However, I suspect that you can't improve much on what we've already got without restricting the domain of applicability of the language. Otherwise, you just shift the intellectual bottleneck from writing a correct program to writing correct specifications. In the realm of programming language design, it's been said that you can't eliminate complexity, all you can do is push it around from one place to another. I suspect something similar applies to the difficulty of writing programs. -- Greg From greg at cosc.canterbury.ac.nz Tue Jul 21 20:28:39 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:28:39 +1200 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <87skgq8m7s.fsf@benfinney.id.au> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> <87skgq8m7s.fsf@benfinney.id.au> Message-ID: <7cn4obF286u4kU1@mid.individual.net> Ben Finney wrote: > My point was rather meant to imply that > subclassing the built-in (immutable) string types was the best way to > usefully get all their functionality However, it would be difficult to do that without changing all C code that deals with strings, including that in extension modules. That's because the existing string type stores the characters in the string object itself. A mutable variant would have to contain a pointer to a resizable memory block, and therefore couldn't be used as a drop-in replacement by existing C code that expects a string. -- Greg From gagsl-py2 at yahoo.com.ar Tue Jul 21 20:32:03 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 21:32:03 -0300 Subject: trouble with minidom References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Message-ID: En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross escribi?: > Hello I'm trying to read an xml file using minidome. The xml looks like: > > > myProj > /here/ > > > > My code looks like so: > from xml.dom.minidom import parse > > dom = parse("myfile.xml") > > for node in dom.getElementsByTagName("project'): > print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, > node.childNodes[1]) > > Unfortunately, it returns 'nodeValue as none. I'm trying to read the > value > out of the node fir example name: myProj. I haven't found much help in > the > documentation. Can someone point me in the right direction? Unless you have a specific reason to use the DOM interface (like having a masochistic mind), working with ElementTree usually is a lot easier: py> import xml.etree.ElementTree as ET py> xml = """ ... ... myProj ... /here/ ... ... """ py> doc = ET.fromstring(xml) py> for project in doc.findall('project'): ... for child in project.getchildren(): ... print child.tag, child.text ... name myProj path /here/ -- Gabriel Genellina From khanhqh2008i at gmail.com Tue Jul 21 20:32:22 2009 From: khanhqh2008i at gmail.com (khanh le) Date: Wed, 22 Jul 2009 07:32:22 +0700 Subject: Python Message-ID: do you have any materials about Python? can you show me the link of Python or some books? thanks a lots! -- Regard! Khanh -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg at cosc.canterbury.ac.nz Tue Jul 21 20:35:18 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:35:18 +1200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a645b81$0$12974$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> Message-ID: <7cn54pF28nv72U1@mid.individual.net> William Dode wrote: > I just tested it with a litle game, to find the places of horse on > a board 5x5. The result is : > > cython avec malloc *int 18s Posting benchmark times for Pyrex or Cython is pretty meaningless without showing the exact code that was used, since times can vary enormously depending on how much you C-ify things. -- Greg From david.lyon at preisshare.net Tue Jul 21 20:37:50 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 21 Jul 2009 20:37:50 -0400 Subject: Python In-Reply-To: References: Message-ID: <00e687679e63af535bea6a4046f1df08@preisshare.net> On Wed, 22 Jul 2009 07:32:22 +0700, khanh le wrote: > do you have any materials about Python? > can you show me the link of Python or some books? http://www.python.org/ > thanks a lots! No problem. Have a nice day. From casey.mcginty at gmail.com Tue Jul 21 20:43:27 2009 From: casey.mcginty at gmail.com (Casey McGinty) Date: Tue, 21 Jul 2009 14:43:27 -1000 Subject: SMTPlib and SMTPd Performance Issues Message-ID: Hi, I just wanted mention a few workarounds I've come up with for the Python SMTP modules in regards to performance. Before I started, I was getting about 15MB/s while sending e-mail from smtplib to smptd over a local connection. (i.e. both client/server running on the same machine). After the following changes, I'm seeing around*220+MB/s * (increase of 14x) The source can be found here: http://svn.python.org/view/python/trunk/Lib/smtplib.py?view=markup http://svn.python.org/view/python/trunk/Lib/smtpd.py?view=markup When sending e-mail through *smtpdlib*, the following method is called. def quotedata(data): """Quote data for email. Double leading '.', and change Unix newline '\\n', or Mac '\\r' into Internet CRLF end-of-line. """ return re.sub(r'(?m)^\.', '..', re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)) As you can see there are two regular expressions parsing the data. If you know that your message is formatted correctly to start with, then this step is unnecessary. When receiving e-mail through *smtpd*, the SMTPChannel class inherits from * asynchat.async_chat*. The default recv buffer size for asynchat is 4K. This can be too much overhead for high data throughput. The buffer size can be increased with this code: import asynchat asynchat.async_chat.ac_in_buffer_size = 1024*128 asynchat.async_chat.ac_out_buffer_size = 1024*128 The *smtpd.SMTP* class prcoesses data through the *smtpd.SMTPChannel* class. There are a lot of debug statements in the module, like so: print >> DEBUGSTREAM, 'Data:', repr(line) By default, DEBUGSTREAM is a no-op, but that that doesn't prevent repr(line) from being called. When variable, line, contains a large email (multiple megabytes), this debug step will really kill performance. Secondly, the method *found_terminator* will also perform expensive strings ops on the e-mail. Maybe its not possible to disable this step, in all cases, but for testing performance, you can override the method like so: class QuickSMTPChannel( smtpd.SMTPChannel, object): def found_terminator(self): if (self._SMTPChannel__state == self.COMMAND or self._SMTPChannel__state != self.DATA): super(QuickSMTPChannel,self).found_terminator() else: data = smtpd.EMPTYSTRING.join(self._SMTPChannel__line) self._SMTPChannel__line = [] status = self._SMTPChannel__server.process_message( self._SMTPChannel__peer, self._SMTPChannel__mailfrom, self._SMTPChannel__rcpttos, data) self._SMTPChannel__rcpttos = [] self._SMTPChannel__mailfrom = None self._SMTPChannel__state = self.COMMAND self.set_terminator('\r\n') if not status: self.push('250 Ok') else: self.push(status Thanks, - Casey -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg at cosc.canterbury.ac.nz Tue Jul 21 20:47:35 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 12:47:35 +1200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <7x4ot7rif8.fsf@ruckus.brouhaha.com> Message-ID: <7cn5rqF28emnnU1@mid.individual.net> >>Steven D'Aprano writes: >> >>>Besides, one can legitimately disagree that 2/3 => 0 is the wrong thing >>>to do. It's the right thing to do if you're doing integer maths. True, but the question is how best to decide whether the programmer wants to do integer maths. Deciding based on the types of the operands is okay in a statically typed language. But it's asking for trouble in a dynamically-typed language, especially where it's common practice to use ints as a substitute for floats that happen to have integer values. EIBTI in this case. -- Greg From http Tue Jul 21 21:07:24 2009 From: http (Paul Rubin) Date: 21 Jul 2009 18:07:24 -0700 Subject: tough-to-explain Python References: <5f0a2722-45eb-468c-b6b2-b7bb80ae5f19@q11g2000yqi.googlegroups.com> <00720d76$0$9710$c3e8da3@news.astraweb.com> <6973dc43-4d3d-41fa-99d0-67aed42e5bd6@g31g2000yqc.googlegroups.com> <7cimf0F27u23eU1@mid.individual.net> <11d09c87-1cd0-45be-b921-ef1811c97723@k6g2000yqn.googlegroups.com> <7cn45nF28j1d4U1@mid.individual.net> Message-ID: <7x4ot5bk4z.fsf@ruckus.brouhaha.com> greg writes: > However, I suspect that you can't improve much on what we've already > got without restricting the domain of applicability of the > language. Otherwise, you just shift the intellectual bottleneck from > writing a correct program to writing correct specifications. Really, it's easier to write specifications. They're a level of abstraction away from the implementation. Think of a sorting function that operates on a list a1,a2...a_n. The sorting algorithm might be some fiendishly clever, hyper-optimized hybrid of quicksort, heapsort, timsort, burstsort, and 52-pickup. It might be thousands of lines of intricate code that looks like it could break at the slightest glance. The specification simply says the output has the property a1<=a2<=...<=a_n. That is a lot easier to say. If you can then prove that the program satisfies the specification, you are far better off than if you have some super-complex pile of code that appears to sort when you test it, but has any number of possible edge cases that you didn't figure out needed testing. It's like the difference between stating a mathematical theorem (like the four-color theorem) and proving it. From pjb at informatimago.com Tue Jul 21 21:19:19 2009 From: pjb at informatimago.com (Pascal J. Bourguignon) Date: Wed, 22 Jul 2009 03:19:19 +0200 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <2009072118415916807-tfb@cleycom> Message-ID: <87my6xa50o.fsf@galatea.local> Tim Bradshaw writes: > On 2009-07-19 19:31:36 +0100, Frank Buss said: > >> (e.g. I >> don't know of a free modern and stable Lisp implemenation with >> mulithreading support for Windows, with a licence with which you can use it >> in closed source commercial programs, like you can do with Python). > > Openmcl seems reasonably stable to me, is LLGPL-licensed, and runs on > these platforms and Solaris x86. > > It's kind of tragic, of course, that this kind of parasitic behaviour > (will not consider a commercial product to use in your commercial > system) has now become so common. GPL. -- __Pascal Bourguignon__ From dstanek at dstanek.com Tue Jul 21 22:05:46 2009 From: dstanek at dstanek.com (David Stanek) Date: Tue, 21 Jul 2009 22:05:46 -0400 Subject: Changing the private variables content In-Reply-To: References: <4A662B36.3010708@gmail.com> Message-ID: On Tue, Jul 21, 2009 at 6:00 PM, Rhodri James wrote: > On Tue, 21 Jul 2009 21:55:18 +0100, Ryniek90 wrote: > >> Hi. >> I'm writing some class, and decided to use inside private method and some >> private variables. While with method i haven't got any problem's with >> variables i have. > > There is no mechanism in Python that makes attributes truly private. > self._number is an attribute just like any other, the whole business > with _leading_underscores is purely a matter of convention. ?If you > have an instance attribute or method with a leading underscore, you > know that using it or calling it isn't something you're supposed > to do outside its class, but nothing will stop you doing exactly that > if you're rude enough to try. > Doubling the _ will give you a little more privacy. It really just mangles the attribute name, but it close to what you want. I just use a _single_under to tell other programmers that they shouldn't be accessing that attribute directly. To my knowledge nothing bad has happened because things are public and using the __double_under makes testing a little harder. >>> class C(object): ... def __init__(self): ... self.__x = 0 ... >>> c = C() >>> c.__x Traceback (most recent call last): File "", line 1, in AttributeError: 'C' object has no attribute '__x' >>> c._C__x 0 -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From gagsl-py2 at yahoo.com.ar Tue Jul 21 22:18:26 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 21 Jul 2009 23:18:26 -0300 Subject: Python References: <00e687679e63af535bea6a4046f1df08@preisshare.net> Message-ID: En Tue, 21 Jul 2009 21:37:50 -0300, David Lyon escribi?: > On Wed, 22 Jul 2009 07:32:22 +0700, khanh le > wrote: >> do you have any materials about Python? >> can you show me the link of Python or some books? > > http://www.python.org/ Also see http://wiki.python.org/moin/BeginnersGuide -- Gabriel Genellina From prateekkakirwar at gmail.com Wed Jul 22 00:42:39 2009 From: prateekkakirwar at gmail.com (Prateek) Date: Tue, 21 Jul 2009 21:42:39 -0700 (PDT) Subject: Python References: <00e687679e63af535bea6a4046f1df08@preisshare.net> Message-ID: <6281cf33-8ab6-4ff6-a2c5-a9fa1e66d368@m7g2000prd.googlegroups.com> Chk dis out ... http://www.swaroopch.com/notes/Python It's gr8 guide for beginners Cheers!!! Prateek From gagsl-py2 at yahoo.com.ar Wed Jul 22 01:02:55 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 02:02:55 -0300 Subject: Detect target name in descriptor __set__ method Message-ID: I have a class attribute 'foo' which is a data descriptor. I create an instance of such class. When I say instance.foo = value, the descriptor __set__ method is called. Is there any way to obtain the name being assigned to? ('foo' in this example). That is, I want to know the target name for the assignment that triggered the __set__ method call. class descriptor(object): def __get__(self, instance, owner): print 'descriptor.__get__ self=%r instance=%r owner=%r' % ( self, instance, owner) return self def __set__(self, instance, value): # I want to know the *name* this value is being assigned to print 'descriptor.__set__ self=%r instance=%r value=%r' % ( self, instance, value) def __delete__(self, instance): print 'descriptor.__delete__ self=%r instance=%r' % ( self, instance) class X(object): foo = descriptor() x = X() x.foo = "value" I can obtain the name descriptor() was assigned to at the time the X class was defined, using a custom metaclass: class Meta(type): def __new__(meta, name, bases, dict): for key,value in dict.iteritems(): if isinstance(value, descriptor): value.name = key return type.__new__(meta, name, bases, dict) class Y(object): __metaclass__ = Meta foo = descriptor() y = Y() print y.foo.name # prints 'foo' This is good enough for me (assuming no one modifies the class after defining it, no two names refer to the same descriptor, no two classes share the same descriptor instance...). But I would like to use a more direct/robust approach, if available. Any ideas? -- Gabriel Genellina From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Wed Jul 22 01:09:35 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Wed, 22 Jul 2009 01:09:35 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: On 2009-07-21 19:06:02 -0400, Neil Hodgson said: > Python uses native threads. So it can be teh-slowness on all ur cores! The global interpreter lock doesn't help much either. -- Raffael Cavallaro From wolfgang at rohdewald.de Wed Jul 22 01:24:21 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Wed, 22 Jul 2009 07:24:21 +0200 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: <200907220724.21185.wolfgang@rohdewald.de> On Wednesday 22 July 2009, Gabriel Genellina wrote: > x = X() > x.foo = "value" del x.foo -- Wolfgang From wolfgang at rohdewald.de Wed Jul 22 01:28:11 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Wed, 22 Jul 2009 07:28:11 +0200 Subject: Detect target name in descriptor __set__ method In-Reply-To: <200907220724.21185.wolfgang@rohdewald.de> References: <200907220724.21185.wolfgang@rohdewald.de> Message-ID: <200907220728.11536.wolfgang@rohdewald.de> On Wednesday 22 July 2009, Wolfgang Rohdewald wrote: > On Wednesday 22 July 2009, Gabriel Genellina wrote: > > x = X() > > x.foo = "value" > > del x.foo sorry, was not yet quite awaken - I read "delete target name" instead of "detect target name" -- Wolfgang From gagsl-py2 at yahoo.com.ar Wed Jul 22 01:43:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 02:43:07 -0300 Subject: Regular exprssion for IRC messages References: Message-ID: En Fri, 17 Jul 2009 13:59:47 -0300, nohics nohics escribi?: > When an IRC client receive a messages, this message in in a special > format > defined here: Message format in pseudo > BNF( > > I want to transforme the message format defined in the irc protocole in > the > last link, to a regular expression to get the nickname, the username, the > host, the commad, it's arguments if exist, and the message after ":" You may want to use the pyparsing module instead of a monstruous and unmantainable regular expression: http://pyparsing.wikispaces.com/ -- Gabriel Genellina From jhinak.sen at gmail.com Wed Jul 22 02:28:27 2009 From: jhinak.sen at gmail.com (jhinak sen) Date: Wed, 22 Jul 2009 11:58:27 +0530 Subject: plotting graph from another file Message-ID: <3d4ff96c0907212328q211801b4tc7a86986a9c35c25@mail.gmail.com> hello.. i want to plot a distribution graph for following data: *elements: number of occurance: a 2 b 4 c 1 d 9 e 3* ( this a simple version) the data is in a file , say abc.txt how can i call/import the text file and draw an appropriate distribution graph in python ? can it be done using matplotlib ? if yes , please give me the code for plotting the importing the text file and plotting the graph. thanx jhinak -------------- next part -------------- An HTML attachment was scrubbed... URL: From hendrik at microcorp.co.za Wed Jul 22 02:36:29 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 22 Jul 2009 08:36:29 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: <200907220836.30064.hendrik@microcorp.co.za> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > On Jul 20, 12:27?pm, Phillip B Oldham > > wrote: > > [snip] We > > understand that lists are mutable and tuples are not, but we're a > > little lost as to why the two were kept separate from the start. They > > both perform a very similar job as far as we can tell. > > My guess is that it was probably for optimization reasons long ago. > I've never heard a *good* reason why Python needs both. The good reason is the immutability, which lets you use a tuple as a dict key. - Hendrik From steven at REMOVE.THIS.cybersource.com.au Wed Jul 22 02:40:16 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 22 Jul 2009 06:40:16 GMT Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: On Tue, 21 Jul 2009 00:39:24 +0200, Niels L. Ellegaard wrote: > Phillip B Oldham writes: > >> We often find we need to do manipulations like the above without >> changing the order of the original list, and languages like JS allow >> this. We can't work out how to do this in python though, other than >> duplicating the list, sorting, reversing, then discarding. I think Phillip is confused if he thinks that other languages can sort a list without modifying or duplicating the original. If you don't duplicate the list, how can you sort it without modifying the original? The only difference is whether the language makes the duplicate for you, or expects you to do it yourself. > If you just want a one-liner, and you don't care about speed you can do > the following (but I don't think this is considered best practice) > >>>> x = [2,1,3] >>>> print list(sorted(x)) The call to list is redundant, as sorted() already creates a list. But that's the wrong solution to the problem. The OP wants the largest (or smallest) item, which he expects to get by sorting, then grabbing the first element: sorted(alist)[0] That requires sorting the entire list, only to throw away everything except the first item. A better solution is to use min() and max(), neither of which sort the list, so they are much faster. -- Steven From steven at REMOVE.THIS.cybersource.com.au Wed Jul 22 02:45:54 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 22 Jul 2009 06:45:54 GMT Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: On Tue, 21 Jul 2009 06:49:59 -0700, Inky 788 wrote: > On Jul 20, 12:27?pm, Phillip B Oldham wrote: >> [snip] We >> understand that lists are mutable and tuples are not, but we're a >> little lost as to why the two were kept separate from the start. They >> both perform a very similar job as far as we can tell. > > My guess is that it was probably for optimization reasons long ago. I've > never heard a *good* reason why Python needs both. Suppose you could do this: key = [1, 2] adict = {key: 'x', [1, 1]: 'y'} This would work as expected: adict[ [1, 2] ] => returns 'x' adict[ [1, 1] ] => returns 'y' But what happens if you mutate the key? key[1] = 0 adict[ [1, 2] ] => raises KeyError Okay, that's bad. What's even worse is this: key[1] = 1 adict[ [1, 1] ] => should it return 'x' or 'y'? The solution is to disallow mutable objects like lists from being used as keys in dicts, and to allow immutable list-like tuples instead. -- Steven From clp2 at rebertia.com Wed Jul 22 02:46:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 21 Jul 2009 23:46:13 -0700 Subject: plotting graph from another file In-Reply-To: <3d4ff96c0907212328q211801b4tc7a86986a9c35c25@mail.gmail.com> References: <3d4ff96c0907212328q211801b4tc7a86986a9c35c25@mail.gmail.com> Message-ID: <50697b2c0907212346t3673b2a0rc5669bf7663bc4c6@mail.gmail.com> On Tue, Jul 21, 2009 at 11:28 PM, jhinak sen wrote: > hello.. > > i want to plot a distribution graph for following data: > elements:?????????????? number of occurance: > ?? a ?? ? ? ? ? ? ? ? ? ? ? ? ? 2 > ?? b??????????????????????????? 4 > ?? c??????????????????????????? 1 > ?? d??????????????????????????? 9 > ?? e??????????????????????????? 3 > > ( this a simple version) > > the data is in a file , say abc.txt > > how can i call/import the text file and draw an appropriate distribution > graph in python? ? > can it be done using matplotlib ? > > if yes , please give me the code for plotting the? importing the text file > and plotting the graph. Given matplot's versatility, I would presume with high likelihood that matplot could make such a chart. However, the purpose of this mailinglist is *not* to do people's job/work/homework for them for free. I would suggest you first *attempt* coding a solution yourself. If you run into trouble, read http://catb.org/~esr/faqs/smart-questions.html Then ask a more specific question on the mailinglist. Best of luck. Regards, Chris From bearophileHUGS at lycos.com Wed Jul 22 02:48:55 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 21 Jul 2009 23:48:55 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> Message-ID: <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> greg: > Posting benchmark times for Pyrex or Cython is pretty > meaningless without showing the exact code that was > used, since times can vary enormously depending on > how much you C-ify things. Was this link, shown by William, not enough? http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 Bye, bearophile From greg at cosc.canterbury.ac.nz Wed Jul 22 02:51:22 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Wed, 22 Jul 2009 18:51:22 +1200 Subject: clean way prepend an element to a numpy array In-Reply-To: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> Message-ID: <7cnr5tF28183hU1@mid.individual.net> bdb112 wrote: > I saw this interest syntax on this site > x[:0]=0 > > I guess that is cute, but could be confusing....(and doesn't work) It does if you use an array of the appropriate type on the right hand side: a[:0] = array('i', [0]) -- Greg From walkraft at gmail.com Wed Jul 22 02:59:34 2009 From: walkraft at gmail.com (casebash) Date: Tue, 21 Jul 2009 23:59:34 -0700 (PDT) Subject: Mutable Strings - Any libraries that offer this? References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> <87r5wba9y1.fsf@benfinney.id.au> <02746e73$0$5185$c3e8da3@news.astraweb.com> <87skgq8m7s.fsf@benfinney.id.au> <7cn4obF286u4kU1@mid.individual.net> Message-ID: Thanks all for your advice. I'm not actually going to use the mutable string right at the moment, but I thought I might in the future and I was just curious if it existed. I suppose a list of characters is close enough for most purposes. On Jul 22, 10:28?am, greg wrote: > Ben Finney wrote: > > My point was rather meant to imply that > > subclassing the built-in (immutable)stringtypes was the best way to > > usefully get all their functionality > > However, it would be difficult to do that without changing > all C code that deals with strings, including that in extension > modules. > > That's because the existingstringtype stores the characters > in thestringobject itself. Amutablevariant would have to > contain a pointer to a resizable memory block, and therefore > couldn't be used as a drop-in replacement by existing C > code that expects astring. > > -- > Greg From timr at probo.com Wed Jul 22 03:09:07 2009 From: timr at probo.com (Tim Roberts) Date: Wed, 22 Jul 2009 00:09:07 -0700 Subject: win32api install problem References: Message-ID: <8led65544idaf3630nk21rjij030aj4s4k@4ax.com> Gerry wrote: >I'm running Python 2.6 under XP. > >I've installed Windows 32 extensions for Python 2.6 version 1.4 >(pywin32-214.win32-py2.6.exe). > >But If I try to import win32api, I get: > > File "C:\python_projects\euler\driveletters.py", line 1, in > import win32api >ImportError: DLL load failed: The specified module could not be found. > >\Python26\Lib\site-packages has: > >03/23/2009 08:35 AM win32 >07/20/2009 09:08 AM win32com >02/18/2009 01:21 PM win32comext > >Can anyone offer a suggestion? It is very unusual that those three directories should have different creation dates. Normally, all three would have the same date and time, from whenever you ran the installer. I would suggest that you go into Add and Remove Programs, uninstall pywin32, and run the installer again. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From dtgeadamo at yahoo.com Wed Jul 22 03:13:49 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Wed, 22 Jul 2009 00:13:49 -0700 (PDT) Subject: Running a Python Service under the LocalService or NetworkService Account References: <1d5e8a37-bc84-430f-93f4-685d9b073d63@c2g2000yqi.googlegroups.com> <2857576c-6695-431e-8d5f-62e9585d5fdf@q11g2000yqi.googlegroups.com> <3d45333b-3a3f-4672-a499-8bb4cafd4595@o7g2000yqb.googlegroups.com> <-vadnaHpJMoIjPvXnZ2dnUVZ8tli4p2d@bt.com> Message-ID: On Jul 21, 8:05?pm, "Martin P. Hellwig" wrote: > David Adamo Jr. wrote: > > On Jul 21, 10:40 am, "Martin P. Hellwig" > > wrote: > >> sightseer wrote: > > >> > > >>> Error Installing Service: Access is Denied. (5) > >> > >> Are you trying to do this on windows vista? > > >> -- > >> MPHhttp://blog.dcuktec.com > >> 'If consumed, best digested with added seasoning to own preference.' > > > Yeah, I was trying to do it on Vista. Someone has just helped me out. > > I had to deactivate User Account Control on Windows Vista...and now > > everything is rosy. Thanks guys. > > No need to deactivate it, just right click on the command shell program > and say run as administrator, than you can install the service via the > command line. > > -- > MPHhttp://blog.dcuktec.com > 'If consumed, best digested with added seasoning to own preference.' Thanks MPH From milesck at umich.edu Wed Jul 22 03:21:06 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 22 Jul 2009 03:21:06 -0400 Subject: trouble with minidom In-Reply-To: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Message-ID: On Jul 21, 2009, at 8:08 PM, Ronn Ross wrote: > Hello I'm trying to read an xml file using minidome. The xml looks > like: > > > myProj > /here/ > > > > My code looks like so: > from xml.dom.minidom import parse > > dom = parse("myfile.xml") > > for node in dom.getElementsByTagName("project'): > print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, > node.childNodes[1]) > > Unfortunately, it returns 'nodeValue as none. I'm trying to read the > value out of the node fir example name: myProj. I haven't found much > help in the documentation. Can someone point me in the right > direction? Two issues: In your example XML file, the first child node of the project Element is the Text node containing the whitespace between the and tags. node.childNodes[0] will select that whitespace node. The nodeValue of an Element is null (None). In order to get the text contents of an element, you must get the nodeValue of the Text child node of the Element. Like Gabriel, I would recommend using an XML library with a more concise API than the W3C DOM (I like lxml.objectify). But if you stick with xml.dom, use the specification as a reference: http://www.w3.org/TR/REC-DOM-Level-1/ From hv at tbz-pariv.de Wed Jul 22 04:05:19 2009 From: hv at tbz-pariv.de (Thomas Guettler) Date: Wed, 22 Jul 2009 10:05:19 +0200 Subject: Workflow Libraries (DB vs Code) In-Reply-To: <7cm6kmF26g9srU1@mid.uni-berlin.de> References: <7cm1ifF28jmnhU1@mid.individual.net> <7cm6kmF26g9srU1@mid.uni-berlin.de> Message-ID: <7cnvi0F28qk9aU1@mid.individual.net> Diez B. Roggisch schrieb: > Thomas Guettler wrote: > >> Hi, >> >> I need to write some simple workflows with web interface. >> >> For the web stuff I will use django, but I am not sure how >> to do the workflow part. > > Did you consider using OpenERP? It comes with a web-frontend > (TG1.0.8-based), but also offers a XMLRPC-server to connect to, so using it > from within django shouldn't be any difficult. > > Workflow-definitions are stored in the DB, and can be generated visually, > not sure if they have to though. Thank you for this hint. OpenERP seems well done. But the licence is GPL and the dependency is too big. But I will have a look at how they did it. Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From joncle at googlemail.com Wed Jul 22 04:21:49 2009 From: joncle at googlemail.com (Jon Clements) Date: Wed, 22 Jul 2009 01:21:49 -0700 (PDT) Subject: Regular exprssion for IRC messages References: Message-ID: <4e59a9a3-f565-4042-9016-5192b8ff8247@c29g2000yqd.googlegroups.com> On 22 July, 06:43, "Gabriel Genellina" wrote: > En Fri, 17 Jul 2009 13:59:47 -0300, nohics nohics ? > escribi?: > > > When an IRC client receive a messages, this message in in a special ? > > format > > defined here: Message format in pseudo > > BNF( > > > I want to transforme the message format defined in the irc protocole in ? > > the > > last link, to a regular expression to get the nickname, the username, the > > host, the commad, it's arguments if exist, and the message after ":" > > You may want to use the pyparsing module instead of a monstruous and ? > unmantainable regular expression: > > http://pyparsing.wikispaces.com/ > > -- > Gabriel Genellina Alternatively, if IIRC, the twisted framework has an IRC client, which automatically parses server messages. From dtgeadamo at yahoo.com Wed Jul 22 04:27:52 2009 From: dtgeadamo at yahoo.com (David Adamo Jr.) Date: Wed, 22 Jul 2009 01:27:52 -0700 (PDT) Subject: Ideas for problem with chat server application! Message-ID: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> I developed a chat application with an attendant chat server. Everything is working fine. The issue now is the fact that whenever the chat server goes down (for instance, the server system shuts down as a result of power failure or some other problem), by the time the server system come back on, the chat server would have to be restarted manually. I believe (and I know) it is more appropriate for the chat server application to restart itself when the computer comes back on (and of course regardless of who is logged in and of course, even before anyone logs in). I have a batch file that executes the chat server. My attempt was to create a windows service that start automatically and runs this batch file using a Network Service account on the server system. Although, I'm having a hard time with this (temporarily), I would love to ask if there are any alternatives to using a windows service. Suggestions are highly appreciated. From joncle at googlemail.com Wed Jul 22 04:31:13 2009 From: joncle at googlemail.com (Jon Clements) Date: Wed, 22 Jul 2009 01:31:13 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: Message-ID: <3cb5f960-16f7-4fc6-841f-d0706f0e1d0a@s31g2000yqs.googlegroups.com> On 22 July, 06:02, "Gabriel Genellina" wrote: > I have a class attribute 'foo' which is a data descriptor. I create an ? > instance of such class. When I say instance.foo = value, the descriptor ? > __set__ method is called. Is there any way to obtain the name being ? > assigned to? ('foo' in this example). That is, I want to know the target ? > name for the assignment that triggered the __set__ method call. > > class descriptor(object): > ? ?def __get__(self, instance, owner): > ? ? ?print 'descriptor.__get__ self=%r instance=%r owner=%r' % ( > ? ? ? ?self, instance, owner) > ? ? ?return self > > ? ?def __set__(self, instance, value): > ? ? ?# I want to know the *name* this value is being assigned to > ? ? ?print 'descriptor.__set__ self=%r instance=%r value=%r' % ( > ? ? ? ?self, instance, value) > > ? ?def __delete__(self, instance): > ? ? ?print 'descriptor.__delete__ self=%r instance=%r' % ( > ? ? ? ?self, instance) > > class X(object): > ? ?foo = descriptor() > > x = X() > x.foo = "value" > > I can obtain the name descriptor() was assigned to at the time the X class ? > was defined, using a custom metaclass: > > class Meta(type): > ? ?def __new__(meta, name, bases, dict): > ? ? ?for key,value in dict.iteritems(): > ? ? ? ?if isinstance(value, descriptor): > ? ? ? ? ?value.name = key > ? ? ?return type.__new__(meta, name, bases, dict) > > class Y(object): > ? ?__metaclass__ = Meta > ? ?foo = descriptor() > > y = Y() > print y.foo.name # prints 'foo' > > This is good enough for me (assuming no one modifies the class after ? > defining it, no two names refer to the same descriptor, no two classes ? > share the same descriptor instance...). But I would like to use a more ? > direct/robust approach, if available. > > Any ideas? > > -- > Gabriel Genellina >>> class Test: def __setattr__(self, what, value): print what, value >>> t = Test() >>> t.foo = 'x' foo x Is that what you're after? Jon. From phillip.oldham at gmail.com Wed Jul 22 05:27:56 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Wed, 22 Jul 2009 02:27:56 -0700 (PDT) Subject: Suggestions for Python MapReduce? Message-ID: I understand that there are a number of MapReduce frameworks/tools that play nicely with Python (Disco, Dumbo/Hadoop), however these have "large" dependencies (Erlang/Java). Are there any MapReduce frameworks/ tools which are either pure-Python, or play nicely with Python but don't require the Java/Erlang runtime environments? From user at example.net Wed Jul 22 06:03:44 2009 From: user at example.net (superpollo) Date: Wed, 22 Jul 2009 12:03:44 +0200 Subject: binary literal Message-ID: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> hello clp. i can insert a hex value for a character literal in a string: >>> stuff = "\x45" >>> print stuff E >>> can i do something like the above, but using a *binary* number? (e.g. 00101101 instead of 45) ? bye From pmarti at warp.es Wed Jul 22 06:24:22 2009 From: pmarti at warp.es (=?ISO-8859-1?Q?Pablo_Mart=ED_Gamboa?=) Date: Wed, 22 Jul 2009 12:24:22 +0200 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <53a2f8770907220324m445c3004s9c844176255eaf46@mail.gmail.com> 2009/7/22 superpollo > hello clp. > > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? (Python 3) >>> bin(45) '0b101101' >>> int(_, 2) 45 -- Pablo Mart? http://www.linkedin.com/in/pmarti || http://www.warp.es python -c "print '706d6172746940776172702e6573'.decode('hex')" -------------- next part -------------- An HTML attachment was scrubbed... URL: From eckhardt at satorlaser.com Wed Jul 22 06:25:32 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Wed, 22 Jul 2009 12:25:32 +0200 Subject: binary literal References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? There are binary number literals since 2.6 and there is the chr() function. >>> print chr(0b1000101) E Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From theajodharan.vfx at gmail.com Wed Jul 22 06:29:59 2009 From: theajodharan.vfx at gmail.com (TNR) Date: Wed, 22 Jul 2009 11:29:59 +0100 Subject: Python In-Reply-To: <6281cf33-8ab6-4ff6-a2c5-a9fa1e66d368@m7g2000prd.googlegroups.com> References: <00e687679e63af535bea6a4046f1df08@preisshare.net> <6281cf33-8ab6-4ff6-a2c5-a9fa1e66d368@m7g2000prd.googlegroups.com> Message-ID: Hi, Check the below links.. http://www.tutorialspoint.com/python/ http://www.java2s.com/Tutorial/Python/CatalogPython.htm Hope that helps. Cheers -theajo On Wed, Jul 22, 2009 at 5:42 AM, Prateek wrote: > Chk dis out ... > > http://www.swaroopch.com/notes/Python > > It's gr8 guide for beginners > > Cheers!!! > Prateek > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjmachin at lexicon.net Wed Jul 22 06:34:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 22 Jul 2009 10:34:16 +0000 (UTC) Subject: Fast reading and unpacking of binary data (struct module) References: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Message-ID: Daniel Platz googlemail.com> writes: > > Hi, > > I have a Python newbie question about reading data from a binary file. > I have an huge binary file from an external program. Please translate "huge" into Mb. Also, how much free real memory do you have? > I want to read > and process the data in this file in a reasonable time. It turns out > that the reading of the data itself and the processing do not need > most of the time. However, when using the read(bytes) method Python > returns a string representing the binary information in hex. "In hex" is not part of the string's nature. If you look at it through hexadecimal/decimal/octal/binary spectacles, you'll see respectively hex/decimal/octal/binary. > This > string I have to "cast/translate" into a number (in my case a signed > short). For this I am using the method struct.unpack from the struct > module. This unpacking part of the program takes by far the most time. > Is there a way to speed this up or to do it the unpacking more > cleverly than with the struct module? Are you reading the file (a) two bytes at a time (b) several Kb at a time (c) whole file? Are you unpacking it ... [same options as above but the two answers may differ]? What are you planning to do with this large list/array? The array option that Gabriel mentioned should be fastest to load, and take up least memory, but working with the elements will require making int objects out of the shorts (and the reverse if you are updating the array). Is numpy (http://numpy.scipy.org/) a possibility? Cheers, John From drunkard at thepub.co.za Wed Jul 22 07:05:31 2009 From: drunkard at thepub.co.za (Mark du Preez) Date: Wed, 22 Jul 2009 13:05:31 +0200 Subject: Documentation Problems Message-ID: Hi Can anyone tell me where to go to suggest changes to the Python documentation? Thanks. From ndbecker2 at gmail.com Wed Jul 22 07:12:07 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 22 Jul 2009 07:12:07 -0400 Subject: Fast reading and unpacking of binary data (struct module) References: <76f9e536-dffd-4fd8-8fdc-7231603813b5@a26g2000yqn.googlegroups.com> Message-ID: Daniel Platz wrote: > Hi, > > I have a Python newbie question about reading data from a binary file. > I have an huge binary file from an external program. I want to read > and process the data in this file in a reasonable time. It turns out > that the reading of the data itself and the processing do not need > most of the time. However, when using the read(bytes) method Python > returns a string representing the binary information in hex. This > string I have to "cast/translate" into a number (in my case a signed > short). For this I am using the method struct.unpack from the struct > module. This unpacking part of the program takes by far the most time. > Is there a way to speed this up or to do it the unpacking more > cleverly than with the struct module? > > Thanks in advance. > > With kind regards, > > Daniel Consider mmap Consider numpy Consider numpy+mmap From mail at timgolden.me.uk Wed Jul 22 07:15:22 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 22 Jul 2009 12:15:22 +0100 Subject: Documentation Problems In-Reply-To: References: Message-ID: <4A66F4CA.2070506@timgolden.me.uk> Mark du Preez wrote: > Hi > > Can anyone tell me where to go to suggest changes to the Python > documentation? Drop an entry in the tracker: http://bugs.python.org Patches are always welcome, or at least suggested wording rather than vague "I don't think it should say that" -- although that's better than nothing! TJG From nipunreddevil at gmail.com Wed Jul 22 07:15:33 2009 From: nipunreddevil at gmail.com (nipun batra) Date: Wed, 22 Jul 2009 16:45:33 +0530 Subject: GTK+ ques Message-ID: <1e61b3260907220415g6edb24sb7fe07c83fe346c8@mail.gmail.com> I need to develop a GUI preferably in GTK using c or PYGTK.I have little idea about Python. Following are requiremnts of the application: should be GUI Sould be real time Should interface serial port should display moving maps,like google earth It is basically for Ground control station of a UAV. How would Python+Pygtk combo work? is python slower than c/c++ ,if then by how much? any suggestions for the applcation? -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Wed Jul 22 07:15:45 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 22 Jul 2009 13:15:45 +0200 Subject: Documentation Problems References: Message-ID: <7coan1F28i1q2U1@mid.uni-berlin.de> Mark du Preez wrote: > Hi > > Can anyone tell me where to go to suggest changes to the Python > documentation? > > Thanks. http://docs.python.org/ has a "bugs"-section that leads to http://docs.python.org/bugs.html You can file doc-bugs in the tracker, and check if they are already fixed in the dev-docs. Diez From wilk at flibuste.net Wed Jul 22 07:38:08 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 11:38:08 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> Message-ID: <4a66fa20$0$11368$426a74cc@news.free.fr> I updated the script (python, c and java) with your unrolled version + somes litle thinks. I also tried with python3.1, unladen Q2, ironpython1.1.1 Unfortunately it doesn't work more with shedskin, i'll see on the shedskin group... c 1.85s gcj 2.15s java 2.8s python2.5 + psyco 3.1s unladen-2009Q2 145s (2m45) python2.5 254s (4m14s) python3.1 300s (5m) ironpython1.1.1 680s (11m20) -- William Dod? - http://flibuste.net Informaticien Ind?pendant From wfluro at gmail.com Wed Jul 22 07:38:43 2009 From: wfluro at gmail.com (ulf) Date: Wed, 22 Jul 2009 04:38:43 -0700 (PDT) Subject: Python ctypes on win64 Message-ID: Hi, Does anybody know if python includes a win64 version of ctypes? According to the documentation it should be included - at least there's no special remarks for win64, and I haven't found any recent notes saying that it shouldn't work on win64. Yet it looks like both the installer from Python.org and from ActiveState.com have disabled it. regards /ulfw From duncan.booth at invalid.invalid Wed Jul 22 07:55:13 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 22 Jul 2009 11:55:13 GMT Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: Steven D'Aprano wrote: > But that's the wrong solution to the problem. The OP wants the largest > (or smallest) item, which he expects to get by sorting, then grabbing > the first element: > > sorted(alist)[0] > > That requires sorting the entire list, only to throw away everything > except the first item. A better solution is to use min() and max(), > neither of which sort the list, so they are much faster. > And if they want the n smallest or largest items (where n is significantly less than the length of the list[*]) they might consider using heapq.nsmallest() and heapq.nlargest() I find it interesting that the heapq functions tell you in the documentation that they aren't suitable for use where n==1 or where n is near the total size of the sequence whereas random.sample() chooses what it thinks is the best algorithm based on the input. At the very least I would have thought the heapq functions could check for n==1 and call min/max when it is. [*] Some quick playing with timeit seems to indicate that with a list of 200 integers nsmallest is fastest (by a very small margin) when n==2 and n==3 but is beaten by sorted[:n] when n==4, so I guess you need a reasonably long list to make it worth not sorting it: with list of 20,000 integers it isn't worth sorting unless you want more than about 700 items. -- Duncan Booth http://kupuguy.blogspot.com From deets at nospam.web.de Wed Jul 22 07:55:29 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 22 Jul 2009 13:55:29 +0200 Subject: List insertion cost References: Message-ID: <7cod1hF28nhhlU1@mid.uni-berlin.de> Lucas P Melo wrote: > Hello, > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? O(n) I'd say. You need to copy nearly the whole list, and sometimes re-allocate it's storage (they are internally stored as arrays of object references) Diez From jeanmichel at sequans.com Wed Jul 22 08:16:19 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 22 Jul 2009 14:16:19 +0200 Subject: doted filenames in import statements In-Reply-To: References: Message-ID: <4A670313.8040204@sequans.com> Piet van Oostrum wrote: > > [snip] > >> JP> file = "/home/dsp/4.6.0.0/test.py" >> JP> test = __import__(file) >> JP> => no module name blalalal found. >> > > >> JP> Any suggestion ? I tried multiple escape technics without any success. >> > > Rightly so. > > I think the best would be to add the directory to sys.path > sys.path.add('/home/dsp/4.6.0.0') > and then > __import__('test', ... ) > I see. My problem is that a have to import 2 different files having de same name. In the same name space I have 2 objects from 2 different software branches, let's say 4.6.0 and 4.6.1. The first object shall import 4.6.0/orb.py and the second one 4.6.1/orb.py. If I add 4.6.1 to sys.path, the import statement will look like: self._orb = __import__('orb') The problem is, python wil assume orb is already imported and will assign the module from the 4.6.0 branch to my 4.6.1 object. Do I have to mess up with sys.modules keys to make python import the correct file ? Is there a standard/proper way to do that ? JM From ryniek90 at gmail.com Wed Jul 22 08:29:20 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 22 Jul 2009 14:29:20 +0200 Subject: Changing the private variables content In-Reply-To: References: Message-ID: <4A670620.1090501@gmail.com> > > ------------------------------------------------------------------------ > > Temat: > Re: Changing the private variables content > Od: > Gary Herron > Data: > Tue, 21 Jul 2009 14:14:44 -0700 > Do: > Ryniek90 > > Do: > Ryniek90 > Kopia: > python-list at python.org > > > Ryniek90 wrote: >> Hi. >> I'm writing some class, and decided to use inside private method and >> some private variables. While with method i haven't got any problem's >> with variables i have. >> Maybe some example. >> A class with private method and private variable: >> >> " >> >>> class Secret(object): >> # >> def __init__(self): >> self._number = 1 >> # >> def _secret(self): >> print self._number >> def showit(self): >> print "Secret number is:\n" >> self._secret() >> >> >>> sec = Secret() >> >>> sec.showit() >> Secret number is: >> >> 1 >> >>> sec._number >> 1 >> >>> sec._number = 2 >> >>> sec._number >> 2 >> >>> sec._number += 3 >> >>> sec._number >> 5 >> >>> >> " >> >> As You can see, i made class with private method and private variable >> inside __init__ constructor. I've changed also the variable value, >> but outside class. >> I've got problem with changing some variable value _inside__my_ >> class, which i'm writing. > > Not sure this is what you are asking, but a method (which is how I > interpret "_inside__my_ class") changes the value by normal assignment > like this: > > class Secret(object): > ... > def SetNumber(self,value): > self._number = value > > Is that what you were asking? > > > Gary Herron > > > >> I've searched over google, some tuts with topics about operations on >> private variables, but didn't found anything - only how to make them, >> but no how to assign new objects/override them with new content (and >> other advanced and helpful options). >> >> If someone could help me, it would be great. >> >> Thanks. > > Thanks for hint, but looks like i can't do what i want. Maybe i show You my class: " class ModPrint(object): u""" This will be the doc. """ def __init__(self): #Assign the Python installation directory - sys.exec_prefix - to variable self._default_search_path=sys.exec_prefix self._chosen_module = '' self._user_search_path = '' self._this_module = '' def _SetVar(self, attr, val): self.attr = val def _search_for_module(self, *args): """Private method which walks through default Python installation directories, and search for prefered module.""" #walking thru all directories available in path '_user_search_path' for root, dirs, files in os.walk(self._user_search_path): for f in files: #if found file 'f' is THAT module, #full path to THAT file is assigned to variable if f == ("%s.py" % self._chosen_module): self._SetVar(self._this_module, os.path.join(root, f)) def print_module(self, _chosen_module='', _user_search_path='', _default_search_path=sys.exec_prefix,): """Reads module chosen by user, and returns full content of this module, as it is.""" #if custom search path hasn't been assigned, #default path is being assigned as custom path if self._user_search_path == '': self._user_search_path = self._default_search_path #using private method '_search_for_module' with 'self.' preffix #to search for prefered module self._search_for_module(_chosen_module, _user_search_path) #opening prefered module with read-only binary mode #and assigning it to 'module_open' variable module_open = open(self._this_module, 'rb') #reading this file and assigning it to variable module_text = module_open.read() #closing read file; the read content is still available #it's stored in variable 'module_text' module_open.close() #returning the read content return module_text " When i use this class in Python IDLE, i've got this error: " >>> mod = ModPrint() >>> import socket >>> mod.print_module('socket') Traceback (most recent call last): File "", line 1, in mod.print_module('socket') File "", line 48, in print_module module_open = open(self._this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " As You can see, i can't assign the new value "os.path.join(root, f)" to the 'self._this_module variable'. So for sure i've made some mistake in method: " def _SetVar(self, attr, val): self.attr = val " When i've changed private variables to normal, stored in class (in __init__ method), it was the same situation - i couldn't change this variable value. " >>> mod = ModPrint() >>> mod.print_module('os') Traceback (most recent call last): File "", line 1, in mod.print_module('os') File "", line 48, in print_module module_open = open(self.this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " Thanks i someone could help me, give some hint. From contact at xavierho.com Wed Jul 22 08:45:37 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 22:45:37 +1000 Subject: Changing the private variables content In-Reply-To: <4A670620.1090501@gmail.com> References: <4A670620.1090501@gmail.com> Message-ID: <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> On Wed, Jul 22, 2009 at 10:29 PM, Ryniek90 wrote: > Thanks for hint, but looks like i can't do what i want. That's because > > def _SetVar(self, attr, val): > self.attr = val doesn't work as you might think. However, I tried to replace it with: val('self.' + attr + '=\'' + val + '\'') and it gives me a syntax error! File "test.py", line 7, in _setVar eval('self.' + attr + '=\'' + val + '\'') File "", line 1 self.var='Mrra' ^ SyntaxError: invalid syntax So, uh, I'm wondering if this is a bug. And also the solution to his problem. Good luck. Any ideas appreciated. Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From python-url at phaseit.net Wed Jul 22 08:48:25 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Wed, 22 Jul 2009 12:48:25 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 22) Message-ID: QOTW: "If programming is symbol manipulation, then you should remember that the user interface is also symbol manipulation, and it is a MUCH harder problem than databases, sorting, searching, and all the other problems you learn about in academia. The user interface has to communicate over a rich but noisy channel using multiple under-specified protocols to a couple of pounds of meat which processes information using buggy heuristics evolved over millions of years to find the ripe fruit, avoid being eaten, and have sex. If you think getting XML was hard, that's *nothing* compared to user interfaces. The fact that even bad UIs work at all is a credit to the heuristics, bugs and all, in the meat." - Steven D'Aprano http://groups.google.com/group/comp.lang.python/msg/8c65eacbd76e79cf Invoke the same method over every object in a collection: http://groups.google.com/group/comp.lang.python/browse_thread/thread/8fd293a9b39c8733/ There is no 'xor' boolean operator, why? What should be its outcome? http://groups.google.com/group/comp.lang.python/browse_thread/thread/16eec722310e75e8/ A thread-safe method to create unique identifiers (not necesarily increasing integers): http://groups.google.com/group/comp.lang.python/browse_thread/thread/dfe22a6446c057df/ Tim Chase tells us three ways to turn standard output into unbuffered mode: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a65f71444bd4bb53/ How to receive data of unknown length using sockets: http://groups.google.com/group/comp.lang.python/browse_thread/thread/9e4a9f8b6e83320/ Override a method, but keeping the inherited docstring: http://groups.google.com/group/comp.lang.python/browse_thread/thread/1e4075ba10dcbdd9/ Limiting the execution time of a code fragment: http://groups.google.com/group/comp.lang.python/browse_thread/thread/734054de170e2904/ Sharing a big object between processes using the multiprocessing module: http://groups.google.com/group/comp.lang.python/browse_thread/thread/833d4988b7af6353/ Why aren't OrderedDicts [3.1] comparable using <, >, <=, >=? http://groups.google.com/group/comp.lang.python/browse_thread/thread/c3c6f4fe7b6d487d/ So many ways to call a function - why is that? Beginners get confused: http://groups.google.com/group/comp.lang.python/browse_thread/thread/3fd57f5ee4850530/ The reasons to choose your favorite programming language (Python, I presume!): http://groups.google.com/group/comp.lang.python/browse_thread/thread/5d87008e328efcf9/ Tuples, lists, their differences and motivation: http://groups.google.com/group/comp.lang.python/browse_thread/thread/cb0cf56c52321ccc/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From contact at xavierho.com Wed Jul 22 08:52:45 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 22:52:45 +1000 Subject: Changing the private variables content In-Reply-To: <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> References: <4A670620.1090501@gmail.com> <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> Message-ID: <2d56febf0907220552q6a96107ds7b60ba46ac82ef6a@mail.gmail.com> > > val('self.' + attr + '=\'' + val + '\'') > Obviously that was eval, not val. Also it doesn't work without the escaped single quotes, either. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark.dufour at gmail.com Wed Jul 22 08:55:57 2009 From: mark.dufour at gmail.com (srepmub) Date: Wed, 22 Jul 2009 05:55:57 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: please send any program that doesn't work with shedskin (it still is experimental after all) to me, or create an issue at shedskin.googlecode.com, and I will have a look at the problem. thanks, mark. From wilk at flibuste.net Wed Jul 22 08:59:37 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 12:59:37 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: <4a670d39$0$11286$426a74cc@news.free.fr> On 22-07-2009, srepmub wrote: > > please send any program that doesn't work with shedskin (it still is > experimental after all) to me, or create an issue at > shedskin.googlecode.com, and I will have a look at the problem. I did it, on the discussion group http://groups.google.com/group/shedskin-discuss/browse_thread/thread/c1f47a7c21897b44 -- William Dod? - http://flibuste.net Informaticien Ind?pendant From disappearedng at gmail.com Wed Jul 22 09:04:18 2009 From: disappearedng at gmail.com (disappearedng) Date: Wed, 22 Jul 2009 21:04:18 +0800 Subject: Finding all cycles within an undirected graph Message-ID: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> Hey everyone, I am interested to find out all the cycles within an undirected graph. I don't have a particular implementation of nodes and vertices yet, so would someone kindly point out a good graph library for this ( I looked at pygraph and python-graph, but not really satisfied ), in addition to good algorithm for solving this issue. From contact at xavierho.com Wed Jul 22 09:06:22 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 23:06:22 +1000 Subject: Changing the private variables content In-Reply-To: <2d56febf0907220552q6a96107ds7b60ba46ac82ef6a@mail.gmail.com> References: <4A670620.1090501@gmail.com> <2d56febf0907220545o25077f01u4682bd35b922d22a@mail.gmail.com> <2d56febf0907220552q6a96107ds7b60ba46ac82ef6a@mail.gmail.com> Message-ID: <2d56febf0907220606q19e343f2mdd276d4e734ee048@mail.gmail.com> Got it: exec('self.' + attr + '=\'' + val + '\'') That worked. I think it'll do what you want now ;) Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Jul 22 09:11:26 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 09:11:26 -0400 Subject: Multiple versions of python In-Reply-To: <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> Message-ID: <4A670FFE.80405@ieee.org> CCW wrote: > On 21 July, 15:19, Dave Angel wrote: > >> ChrisW wrote: >> >>> Hi, >>> >>> I have installed 2 versions of python on my Windows XP computer - I >>> originally had 3.0.1, but then found that the MySQL module only >>> supported 2.*, so I've now installed that. I have found that if I >>> change the Windows Environment Variable path, then I can change the >>> version of python called when I type 'python' into a command line. >>> However, I'd like to be able to choose which version I use. I know >>> that if I change C:\Python26\python.exe to >>> C:\Python26\python2.exe and C:\Python30\python.exe to C: >>> \Python26\python3.exe, then typing 'python2' or 'python3' will invoke >>> the correct interpreter. However, is it safe just to rename the >>> executable files? Is there a more elegant way to achieve the same >>> task? >>> >>> Thanks, >>> Chris >>> >> The elegant way is to have a batch directory on your PATH ( I use >> m:\t\bat ) and put your *.bat files there. I do NOT put any python >> installations on the PATH. >> >> For example, I have a batch file called: m:\t\bat\python26.bat >> >> c:\progfiles\python26\python.exe %* >> >> The %* syntax means pass all arguments through to the program. >> >> Once it all works, you can add an "@" in front of the c: in order to >> suppress echoing the command. At that point, it'll look and work the >> same way as what you did, but without modifying anything in the install >> directory. (You may want python26.bat, pythonw26.bat, python31.bat >> and pythonw31.bat) >> >> The other thing you may want to do in a batch file is to change the file >> associations so that you can run the .py file directly, without typing >> "python" or "pythonw" in front of it. >> >> The relevant Windows commands are: assoc and ftype And on a >> related note, you may want to edit the PATHEXT environment variable, to >> add .PY and .PYW >> > > Thanks for this - this way made a bit more sense to me. I've now got > C:\commands with the 4 .bat files in, and C:\commands in my path. It > all seems to work :) I think I've missed the point of the @ though - > it doesn't seem to make any difference.. > > I'm also a bit confused with the associations - since I've got python > 2.6 and 3.1, surely the command I type (python26 or python31) is the > only way to force a script to be run using a specific interpreter at > runtime without having to change the .bat file every time I want to > run a script using 3.1 instead of 2.6? > > The @ symbol was used in older versions of CMD and COMMAND to suppress echoing of the command line. I think if you're using XP or later, it doesn't matter. As for file associations, I don't know just what you already know about. When you type data.doc at a command line, the system looks for the data file, then if it's found, it looks up the program associated with that extension. On my machine, that's a particular version of Word for Windows. Similarly, if I type digest.py and I'm in the directory containing that file, the system looks up my associations, and runs Python 2.6 on that file. If I wanted it to run Python 3.1 on that file, I'd have to change the associations, temporarily. So, first approximation, it's a way to avoid having to type PYTHON each time I want to run a script, as long as I don't need any other arguments on the (implied) command line. Further, the system will then look for digest.py everywhere on the PATH, so I don't even have to be in the same directory. And that means I can be in the data directory that digest.py is going to work on. Effectively, it raises digest.py to feeling like an executable, for most purposes. In fact, I put all my scripts in a directory, along with other simple executables. I use m:\t\bin for that purpose. These associations (to .py and .pyw) are established by the installer program for Python. But when you had multiple installs, you could choose whether the second one installed overrides the first. My point is that if you need to change them back and forth, you could use assoc and ftype to do it. On my system: M:\LabOrders>assoc .py .py=Python.File M:\LabOrders>ftype Python.File Python.File="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* M:\LabOrders> So my .py files are associated with the 2.6.2 version of ActivePython installation. Note the similarity of the ftype to your python26.bat file? One more little trick is that I could just type digest instead of digest.py, if I have added the .py and .pyw extensions to my PATHEXT environment variable. So my PATHEXT looks like: PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY;.PYW Note that the association changes made by assoc and ftype are global. They apply to all cmd.exe windows, even those already running, and they survive a reboot. But when you set environment variables like PATHEXT, you can choose to do it in a single window (with SET), or globally (using control-panel). If I found myself switching often, I'd make separate ftype entries for each interpreter, something like Python.File26="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* Python.File31="C:\PROGFI~1\..... and just change assoc .py to point at the one I needed today. As it is, I'm content leaving the associations pointing at 2.6, and explicitly entering PYTHON31 in front of any script name I want to run on that interpreter. DaveA From python at mrabarnett.plus.com Wed Jul 22 09:12:18 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 22 Jul 2009 14:12:18 +0100 Subject: Changing the private variables content In-Reply-To: <4A670620.1090501@gmail.com> References: <4A670620.1090501@gmail.com> Message-ID: <4A671032.10304@mrabarnett.plus.com> Ryniek90 wrote: >> >> ------------------------------------------------------------------------ >> >> Temat: >> Re: Changing the private variables content >> Od: >> Gary Herron >> Data: >> Tue, 21 Jul 2009 14:14:44 -0700 >> Do: >> Ryniek90 >> >> Do: >> Ryniek90 >> Kopia: >> python-list at python.org >> >> >> Ryniek90 wrote: >>> Hi. >>> I'm writing some class, and decided to use inside private method and >>> some private variables. While with method i haven't got any problem's >>> with variables i have. >>> Maybe some example. >>> A class with private method and private variable: >>> >>> " >>> >>> class Secret(object): >>> # >>> def __init__(self): >>> self._number = 1 >>> # >>> def _secret(self): >>> print self._number >>> def showit(self): >>> print "Secret number is:\n" >>> self._secret() >>> >>> >>> sec = Secret() >>> >>> sec.showit() >>> Secret number is: >>> >>> 1 >>> >>> sec._number >>> 1 >>> >>> sec._number = 2 >>> >>> sec._number >>> 2 >>> >>> sec._number += 3 >>> >>> sec._number >>> 5 >>> >>> >>> " >>> >>> As You can see, i made class with private method and private variable >>> inside __init__ constructor. I've changed also the variable value, >>> but outside class. >>> I've got problem with changing some variable value _inside__my_ >>> class, which i'm writing. >> >> Not sure this is what you are asking, but a method (which is how I >> interpret "_inside__my_ class") changes the value by normal assignment >> like this: >> >> class Secret(object): >> ... >> def SetNumber(self,value): >> self._number = value >> >> Is that what you were asking? >> >> >> Gary Herron >> >> >> >>> I've searched over google, some tuts with topics about operations on >>> private variables, but didn't found anything - only how to make them, >>> but no how to assign new objects/override them with new content (and >>> other advanced and helpful options). >>> >>> If someone could help me, it would be great. >>> >>> Thanks. >> >> > > > Thanks for hint, but looks like i can't do what i want. > > Maybe i show You my class: > > " > > class ModPrint(object): > u""" > This will be the doc. > """ > def __init__(self): > #Assign the Python installation directory - sys.exec_prefix > - to variable > self._default_search_path=sys.exec_prefix > self._chosen_module = '' > self._user_search_path = '' > self._this_module = '' > def _SetVar(self, attr, val): > self.attr = val def _search_for_module(self, *args): > """Private method which walks through default Python > installation directories, and search for prefered module.""" > #walking thru all directories available in path > '_user_search_path' > for root, dirs, files in os.walk(self._user_search_path): > for f in files: > #if found file 'f' is THAT module, > #full path to THAT file is assigned to variable > if f == ("%s.py" % self._chosen_module): > self._SetVar(self._this_module, os.path.join(root, f)) > def print_module(self, > _chosen_module='', _user_search_path='', > _default_search_path=sys.exec_prefix,): > """Reads module chosen by user, and returns full content of > this module, as it is.""" > #if custom search path hasn't been assigned, > #default path is being assigned as custom path > if self._user_search_path == '': > self._user_search_path = self._default_search_path > #using private method '_search_for_module' with 'self.' > preffix > #to search for prefered module > self._search_for_module(_chosen_module, _user_search_path) > #opening prefered module with read-only binary mode > #and assigning it to 'module_open' variable > module_open = open(self._this_module, 'rb') > #reading this file and assigning it to variable > module_text = module_open.read() > #closing read file; the read content is still available > #it's stored in variable 'module_text' > module_open.close() > #returning the read content > return module_text > > " > > When i use this class in Python IDLE, i've got this error: > " > >>> mod = ModPrint() > >>> import socket > >>> mod.print_module('socket') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 48, in print_module > module_open = open(self._this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > As You can see, i can't assign the new value "os.path.join(root, f)" to > the 'self._this_module variable'. > So for sure i've made some mistake in method: > > " > def _SetVar(self, attr, val): > self.attr = val " > When i've changed private variables to normal, stored in class (in > __init__ method), it was the same situation - i couldn't change this > variable value. > > " > >>> mod = ModPrint() > >>> mod.print_module('os') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('os') > File "", line 48, in print_module > module_open = open(self.this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > Thanks i someone could help me, give some hint. What is the point of the _SetVar method? Instead of: self._SetVar(self._this_module, os.path.join(root, f)) just do: self._this_module = os.path.join(root, f) (unless I'm misunderstanding what you're trying to do!) From alan.isaac at gmail.com Wed Jul 22 09:14:12 2009 From: alan.isaac at gmail.com (Alan G Isaac) Date: Wed, 22 Jul 2009 13:14:12 GMT Subject: invoke method on many instances In-Reply-To: References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>>> def apply2(itr, methodname, *args, **kwargs): >>>> f = operator.methodcaller(methodname, *args, **kwargs) >>>> for item in itr: >>>> f(item) >> On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >>> for obj in objects: >>> getattr(obj, methodname)(*args, **kwargs) > En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac: >> Are there any obvious considerations in choosing >> between those two? On 7/20/2009 3:29 AM Gabriel Genellina apparently wrote: > The operator.methodcaller version is faster in my tests for large > collections, but slightly slower when you have very few elements. So it seems. Is this easily explained? Thanks, Alan Isaac From breamoreboy at yahoo.co.uk Wed Jul 22 09:18:55 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 Jul 2009 14:18:55 +0100 Subject: Python In-Reply-To: References: Message-ID: khanh le wrote: > do you have any materials about Python? > can you show me the link of Python or some books? > thanks a lots! > > -- > Regard! > Khanh > This comes highly recommeended http://diveintopython.org/ Regards. From Caseyweb at gmail.com Wed Jul 22 09:23:33 2009 From: Caseyweb at gmail.com (Casey Webster) Date: Wed, 22 Jul 2009 06:23:33 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: Message-ID: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> On Jul 22, 5:27?am, Phillip B Oldham wrote: > I understand that there are a number of MapReduce frameworks/tools > that play nicely with Python (Disco, Dumbo/Hadoop), however these have > "large" dependencies (Erlang/Java). Are there any MapReduce frameworks/ > tools which are either pure-Python, or play nicely with Python but > don't require the Java/Erlang runtime environments? I can't answer your question, but I would like to better understand the problem you are trying to solve. The Apache Hadoop/MapReduce java application isn't really that "large" by modern standards, although it is generally run with large heap sizes for performance (-Xmx1024m or larger for the mapred.child.java.opts parameter). MapReduce is designed to do extremely fast parallel data set processing on terabytes of data over hundreds of physical nodes; what advantage would a pure Python approach have here? From contact at xavierho.com Wed Jul 22 09:24:05 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 22 Jul 2009 23:24:05 +1000 Subject: Changing the private variables content In-Reply-To: <4A671032.10304@mrabarnett.plus.com> References: <4A670620.1090501@gmail.com> <4A671032.10304@mrabarnett.plus.com> Message-ID: <2d56febf0907220624u7401550ei24461dd9ca7d6934@mail.gmail.com> > What is the point of the _SetVar method? > > So you can set any variable in that class, I guess? -------------- next part -------------- An HTML attachment was scrubbed... URL: From brendon.wickham at gmail.com Wed Jul 22 09:28:16 2009 From: brendon.wickham at gmail.com (Brendon Wickham) Date: Wed, 22 Jul 2009 23:28:16 +1000 Subject: Getting cookie "expires" value Message-ID: Hi there, My web application uses a cookie to set/get a session id. This is working fine. However, I'm unable to work out how to get the cookie "expires" value. Below is test code that replicates the problem. It creates a simple web page and shows a cookie value if it's found, or creates a new one (correctly setting the "expires" value) and indicates that it was created: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #!/usr/bin/env python import Cookie,os def printHeader(): ??? print "Content-type: text/html\n" ??? print "Cookie test" ??? print "" cookies=Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE')) if cookies.has_key('test'): ??? #If cookie was found: ??? printHeader() ??? print "Found cookie:

" ??? #Get cookie: ??? c = cookies.get('test') ??? #Print its value: ??? print "
Value: %s
" % c.value ??? #Iterate through its items and print name/value: ??? for k,v in c.items(): ??? ??? ?print "
%s=%s
" % (k,v) else: ??? #Define cookie: ??? cookies['test'] = "1234" ??? cookies['test']['expires'] = 3600 ??? #Save cookie: ??? print cookies['test'] ??? #Now print HTML: ??? printHeader() ??? print "Cookie created." print "" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This is what I get if the cookie exists: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Found cookie: Value: 1234 comment= domain= secure= expires= max-age= version= path= <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< As you can see, the cookie attributes are valueless. Can anyone help? Am I missing something fundamental? Cheers, Brendon From steve at REMOVE-THIS-cybersource.com.au Wed Jul 22 09:28:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 22 Jul 2009 13:28:23 GMT Subject: Changing the private variables content References: Message-ID: <02770500$0$5185$c3e8da3@news.astraweb.com> On Wed, 22 Jul 2009 14:29:20 +0200, Ryniek90 wrote: > When i use this class in Python IDLE, i've got this error: " ... > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 48, in print_module > module_open = open(self._this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > As You can see, i can't assign the new value "os.path.join(root, f)" to > the 'self._this_module variable'. In the __init__ method, you successfully set the `_this_module` attribute: self._this_module = '' That works, so it proves that you *can* set a private attribute. Later on, you call the `_SetVar` method, which does this: def _SetVar(self, attr, val): self.attr = val In English, this method does this: (1) Take two arguments, and store them in variables called "attr" and "val". (2) Assign the value stored in "val" to an attribute named exactly "attr". What you actually want is: (1) Take two arguments, and store them in variables called "attr" and "val". (2) Assign the value stored in "val" to an attribute with the name stored in the variable "attr". Can you see the difference? What you actually want is: def _SetVar(self, attr, val): setattr(self, attr, val) Your code is terribly complicated. This will probably do what you want. (Warning -- I haven't tested it, so there may be a few errors.) class ModPrint(object): u""" This will be the doc. """ def __init__(self, default_search_path=''): self.default_search_path = '' def find_module(self, modulename, search_path=''): if search_path == '': # Try the default search path instead. search_path = self.default_search_path if search_path == '': # still blank. # Try the Python search path instead. search_path = sys.exec_prefix for root, dirs, files in os.walk(search_path): for f in files: if f == "%s.py" % modulename: return os.path.join(root, f) def get_module_text(self, modulename, search_path=''): filename = self.find_module(modulename, search_path) module_open = open(filename, 'rb') module_text = module_open.read() module_open.close() return module_text I changed the name of print_module_text because it doesn't actually print anything. -- Steven From robin at reportlab.com Wed Jul 22 09:37:35 2009 From: robin at reportlab.com (Robin Becker) Date: Wed, 22 Jul 2009 14:37:35 +0100 Subject: Finding all cycles within an undirected graph In-Reply-To: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> References: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> Message-ID: <4A67161F.8090908@chamonix.reportlab.co.uk> disappearedng wrote: > Hey everyone, > I am interested to find out all the cycles within an undirected graph. > I don't have a particular implementation of nodes and vertices yet, so > would someone kindly point out a good graph library for this ( I > looked at pygraph and python-graph, but not really satisfied ), in > addition to good algorithm for solving this issue. this one has been updated recently http://code.google.com/p/python-graph/ also this page has many links http://wiki.python.org/moin/PythonGraphApi -- Robin Becker From jjkk73 at gmail.com Wed Jul 22 09:39:09 2009 From: jjkk73 at gmail.com (jorma kala) Date: Wed, 22 Jul 2009 15:39:09 +0200 Subject: How to document Python code properly for Pydoc Message-ID: Hi, Do you know where I can find the rules for documenting Python code, so that automatic document generation with Pydoc makes the most of the comments inserted in the code? I know about documenting class and method through triple quote just under the class definition. But how do you comment a specific field or variable, or how do you document function arguments so that they are extracted like in javadoc? Thanks very much -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhodri at wildebst.demon.co.uk Wed Jul 22 10:01:09 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 22 Jul 2009 15:01:09 +0100 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina wrote: > class X(object): > foo = descriptor() > > x = X() > x.foo = "value" Isn't this going to create a brand new instance attribute x.foo that has nothing to do with the descriptor anyway? -- Rhodri James *-* Wildebeest Herder to the Masses From ryniek90 at gmail.com Wed Jul 22 10:01:19 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 22 Jul 2009 16:01:19 +0200 Subject: Changing the private variables content In-Reply-To: References: Message-ID: <4A671BAF.1060204@gmail.com> > > Got it: > > exec('self.' + attr + '=\'' + val + '\'') > > That worked. I think it'll do what you want now ;) > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ To bad, that didn't worked in my class. Still the same error: " >>> mod.print_module('socket') Traceback (most recent call last): File "", line 1, in mod.print_module('socket') File "", line 51, in print_module module_open = open(self._this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " :-/ > What is the point of the _SetVar method? > > Instead of: > > self._SetVar(self._this_module, os.path.join(root, f)) > > just do: > > self._this_module = os.path.join(root, f) > > (unless I'm misunderstanding what you're trying to do!) > Of course i;ve tried, but still get the same error: " >>> mod.print_module('socket') Traceback (most recent call last): File "", line 1, in mod.print_module('socket') File "", line 51, in print_module module_open = open(self.this_module, 'rb') IOError: [Errno 2] No such file or directory: '' >>> " It looks like private variable have specific naure, that prevent from traditional editing them. Still searching for some tuts about private methods and variables. From Scott.Daniels at Acm.Org Wed Jul 22 10:06:45 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 22 Jul 2009 07:06:45 -0700 Subject: Multiple versions of python In-Reply-To: <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> <98c92c97-9e4c-4528-94e2-58ca6474e681@m11g2000yqh.googlegroups.com> Message-ID: CCW wrote: > On 21 July, 15:19, Dave Angel wrote: >> The other thing you may want to do in a batch file is to change the file >> associations so that you can run the .py file directly, without typing >> "python" or "pythonw" in front of it. >> >> The relevant Windows commands are: assoc and ftype And on a >> related note, you may want to edit the PATHEXT environment variable, to >> add .PY and .PYW > > Thanks for this - this way made a bit more sense to me. I've now got > C:\commands with the 4 .bat files in, and C:\commands in my path. It > all seems to work :) I think I've missed the point of the @ though - > it doesn't seem to make any difference.. > > I'm also a bit confused with the associations - since I've got python > 2.6 and 3.1, surely the command I type (python26 or python31) is the > only way to force a script to be run using a specific interpreter at > runtime without having to change the .bat file every time I want to > run a script using 3.1 instead of 2.6? OK, for me currently: C:\> assoc .py .py=Python.File C:\> assoc .pyw .pyw=Python.NoConFile C:\> ftype Python.File Python.File="C:\Python31\python.exe" "%1" %* C:\> ftype Python.NoConFile Python.NoConFile="C:\Python31\pythonw.exe" "%1" %* C:\> ftype Python.File Python.File="C:\Python31\python.exe" "%1" %* Now imagine instead that you've added: C:\> ftype Python31.File="C:\Python31\python.exe" "%1" %* C:\> ftype Python31.NoConFile="C:\Python31\pythonw.exe" "%1" %* C:\> ftype Python26.File="C:\Python26\python.exe" "%1" %* C:\> ftype Python26.NoConFile="C:\Python26\pythonw.exe" "%1" %* Then you can do the following: C:\> assoc .py=Python26.File C:\> fumble.py C:\> assoc .py=Python31.File C:\> fumble.py That is the basic idea, but at the moment, I don't see a simple demo working for me. SO, if you want to pursue this, you can probably get it to work. --Scott David Daniels Scott.Daniels at Acm.Org From Scott.Daniels at Acm.Org Wed Jul 22 10:12:18 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 22 Jul 2009 07:12:18 -0700 Subject: Issue combining gzip and subprocess In-Reply-To: References: Message-ID: <2YednUGQxvntgfrXnZ2dnUVZ_hWdnZ2d@pdx.net> Piet van Oostrum wrote: > ... > f = gzip.open(filename, 'w') > proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) > while True: > line = proc.stdout.readline() > if not line: break > f.write(line) > f.close() Or even: proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) with gzip.open(filename, 'w') as dest: for line in iter(proc.stdout, ''): f.write(line) --Scott David Daniels Scott.Daniels at Acm.Org From invalid at invalid Wed Jul 22 10:13:28 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 22 Jul 2009 09:13:28 -0500 Subject: plotting graph from another file References: Message-ID: <096dnaQneZwVg_rXnZ2dnUVZ_hZi4p2d@posted.visi> On 2009-07-22, jhinak sen wrote: > i want to plot a distribution graph for following data: [...] > can it be done using matplotlib ? > > if yes , please give me the code for plotting the importing > the text file and plotting the graph. As soon as you're done cleaning my house, washing my car, and running a few errands. -- Grant Edwards grante Yow! Of course, you at UNDERSTAND about the PLAIDS visi.com in the SPIN CYCLE -- From disappearedng at gmail.com Wed Jul 22 10:16:31 2009 From: disappearedng at gmail.com (disappearedng) Date: Wed, 22 Jul 2009 22:16:31 +0800 Subject: Finding all cycles within an undirected graph (disappearedng) Message-ID: <15dd54ec0907220716g34a806e1w5c2a93176172db20@mail.gmail.com> Actually, I have realized that using the bit vector XOR method by http://www.me.utexas.edu/~bard/IP/Handouts/cycles.pdf gives some possibility of doing so. However this method is still experimental and doesn't really have a definitive end to the algorithm (the base number of vectors can change). Any ideas anyone? On Wed, Jul 22, 2009 at 9:12 PM, wrote: > 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. Finding all cycles within an undirected graph (disappearedng) > ? 2. Re: Re: Changing the private variables content (Xavier Ho) > ? 3. Python-URL! - weekly Python news and links (Jul 22) > ? ? ?(Gabriel Genellina) > ? 4. Re: Multiple versions of python (Dave Angel) > ? 5. Re: Changing the private variables content (MRAB) > > > ---------- Forwarded message ---------- > From:?disappearedng > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 21:04:18 +0800 > Subject:?Finding all cycles within an undirected graph > Hey everyone, > I am interested to find out all the cycles within an undirected graph. > I don't have a particular implementation of nodes and vertices yet, so > would someone kindly point out a good graph library for this ( I > looked at pygraph and python-graph, but not really satisfied ), in > addition to good algorithm for solving this issue. > > > > ---------- Forwarded message ---------- > From:?Xavier Ho > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 23:06:22 +1000 > Subject:?Re: Re: Changing the private variables content > Got it: > > exec('self.' + attr + '=\'' + val + '\'') > > That worked. I think it'll do what you want now ;) > > Ching-Yun "Xavier" Ho, Technical Artist > > Contact Information > Mobile: (+61) 04 3335 4748 > Skype ID: SpaXe85 > Email: contact at xavierho.com > Website: http://xavierho.com/ > > > ---------- Forwarded message ---------- > From:?"Gabriel Genellina" > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 12:48:25 +0000 (UTC) > Subject:?Python-URL! - weekly Python news and links (Jul 22) > QOTW: ?"If programming is symbol manipulation, then you should remember that > the user interface is also symbol manipulation, and it is a MUCH harder > problem than databases, sorting, searching, and all the other problems you > learn about in academia. The user interface has to communicate over a rich > but noisy channel using multiple under-specified protocols to a couple of > pounds of meat which processes information using buggy heuristics evolved > over millions of years to find the ripe fruit, avoid being eaten, and have > sex. If you think getting XML was hard, that's *nothing* compared to user > interfaces. > > The fact that even bad UIs work at all is a credit to the heuristics, bugs > and all, in the meat." - Steven D'Aprano > ? ? http://groups.google.com/group/comp.lang.python/msg/8c65eacbd76e79cf > > > ? ?Invoke the same method over every object in a collection: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/8fd293a9b39c8733/ > > ? ?There is no 'xor' boolean operator, why? What should be its outcome? > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/16eec722310e75e8/ > > ? ?A thread-safe method to create unique identifiers (not necesarily > ? ?increasing integers): > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/dfe22a6446c057df/ > > ? ?Tim Chase tells us three ways to turn standard output into unbuffered mode: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/a65f71444bd4bb53/ > > ? ?How to receive data of unknown length using sockets: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/9e4a9f8b6e83320/ > > ? ?Override a method, but keeping the inherited docstring: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/1e4075ba10dcbdd9/ > > ? ?Limiting the execution time of a code fragment: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/734054de170e2904/ > > ? ?Sharing a big object between processes using the multiprocessing module: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/833d4988b7af6353/ > > ? ?Why aren't OrderedDicts [3.1] comparable using <, >, <=, >=? > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/c3c6f4fe7b6d487d/ > > ? ?So many ways to call a function - why is that? Beginners get confused: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/3fd57f5ee4850530/ > > ? ?The reasons to choose your favorite programming language (Python, I > ? ?presume!): > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/5d87008e328efcf9/ > > ? ?Tuples, lists, their differences and motivation: > ? ? ? ?http://groups.google.com/group/comp.lang.python/browse_thread/thread/cb0cf56c52321ccc/ > > > ======================================================================== > Everything Python-related you want is probably one or two clicks away in > these pages: > > ? ?Python.org's Python Language Website is the traditional > ? ?center of Pythonia > ? ? ? ?http://www.python.org > ? ?Notice especially the master FAQ > ? ? ? ?http://www.python.org/doc/FAQ.html > > ? ?PythonWare complements the digest you're reading with the > ? ?marvelous daily python url > ? ? ? ? http://www.pythonware.com/daily > > ? ?Just beginning with Python? ?This page is a great place to start: > ? ? ? ?http://wiki.python.org/moin/BeginnersGuide/Programmers > > ? ?The Python Papers aims to publish "the efforts of Python enthusiasts": > ? ? ? ?http://pythonpapers.org/ > ? ?The Python Magazine is a technical monthly devoted to Python: > ? ? ? ?http://pythonmagazine.com > > ? ?Readers have recommended the "Planet" sites: > ? ? ? ?http://planetpython.org > ? ? ? ?http://planet.python.org > > ? ?comp.lang.python.announce announces new Python software. ?Be > ? ?sure to scan this newsgroup weekly. > ? ? ? ?http://groups.google.com/group/comp.lang.python.announce/topics > > ? ?Python411 indexes "podcasts ... to help people learn Python ..." > ? ?Updates appear more-than-weekly: > ? ? ? ?http://www.awaretek.com/python/index.html > > ? ?The Python Package Index catalogues packages. > ? ? ? ?http://www.python.org/pypi/ > > ? ?Much of Python's real work takes place on Special-Interest Group > ? ?mailing lists > ? ? ? ?http://www.python.org/sigs/ > > ? ?Python Success Stories--from air-traffic control to on-line > ? ?match-making--can inspire you or decision-makers to whom you're > ? ?subject with a vision of what the language makes practical. > ? ? ? ?http://www.pythonology.com/success > > ? ?The Python Software Foundation (PSF) has replaced the Python > ? ?Consortium as an independent nexus of activity. ?It has official > ? ?responsibility for Python's development and maintenance. > ? ? ? ?http://www.python.org/psf/ > ? ?Among the ways you can support PSF is with a donation. > ? ? ? ?http://www.python.org/psf/donations/ > > ? ?The Summary of Python Tracker Issues is an automatically generated > ? ?report summarizing new bugs, closed ones, and patch submissions. > ? ? ? ?http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date > > ? ?Although unmaintained since 2002, the Cetus collection of Python > ? ?hyperlinks retains a few gems. > ? ? ? ?http://www.cetus-links.org/oo_python.html > > ? ?Python FAQTS > ? ? ? ?http://python.faqts.com/ > > ? ?The Cookbook is a collaborative effort to capture useful and > ? ?interesting recipes. > ? ? ? ?http://code.activestate.com/recipes/langs/python/ > > ? ?Many Python conferences around the world are in preparation. > ? ?Watch this space for links to them. > > ? ?Among several Python-oriented RSS/RDF feeds available, see: > ? ? ? ?http://www.python.org/channews.rdf > ? ?For more, see: > ? ? ? ?http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all > ? ?The old Python "To-Do List" now lives principally in a > ? ?SourceForge reincarnation. > ? ? ? ?http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse > ? ? ? ?http://www.python.org/dev/peps/pep-0042/ > > ? ?del.icio.us presents an intriguing approach to reference commentary. > ? ?It already aggregates quite a bit of Python intelligence. > ? ? ? ?http://del.icio.us/tag/python > > ? ?Enjoy the *Python Magazine*. > ? ? ? ?http://pymag.phparch.com/ > > ? ?*Py: the Journal of the Python Language* > ? ? ? ?http://www.pyzine.com > > ? ?Dr.Dobb's Portal is another source of Python news and articles: > ? ? ? ?http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python > ? ?and Python articles regularly appear at IBM DeveloperWorks: > ? ? ? ?http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 > > Previous - (U)se the (R)esource, (L)uke! - messages are listed here: > ?http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date > ?http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& > ?http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes > > There is *not* an RSS for "Python-URL!"--at least not yet. ?Arguments > for and against are occasionally entertained. > > > Suggestions/corrections for next week's posting are always welcome. > E-mail to should get through. > > To receive a new issue of this posting in e-mail each Monday morning > (approximately), ask to subscribe. ?Mention > "Python-URL!". ?Write to the same address to unsubscribe. > > > -- The Python-URL! Team-- > > Phaseit, Inc. (http://phaseit.net) is pleased to participate in and > sponsor the "Python-URL!" project. ?Watch this space for upcoming > news about posting archives. > > > > ---------- Forwarded message ---------- > From:?Dave Angel > To:?CCW > Date:?Wed, 22 Jul 2009 09:11:26 -0400 > Subject:?Re: Multiple versions of python > CCW wrote: >> >> On 21 July, 15:19, Dave Angel wrote: >> >>> >>> ChrisW wrote: >>> >>>> >>>> Hi, >>>> ? ? ?I have installed 2 versions of python on my Windows XP computer - I >>>> originally had 3.0.1, but then found that the MySQL module only >>>> supported 2.*, so I've now installed that. ?I have found that if I >>>> change the Windows Environment Variable path, then I can change the >>>> version of python called when I type 'python' into a command line. >>>> However, I'd like to be able to choose which version I use. ?I know >>>> that if I change C:\Python26\python.exe to >>>> C:\Python26\python2.exe and C:\Python30\python.exe to C: >>>> \Python26\python3.exe, then typing 'python2' or 'python3' will invoke >>>> the correct interpreter. ?However, is it safe just to rename the >>>> executable files? Is there a more elegant way to achieve the same >>>> task? >>>> ? ? ?Thanks, >>>> Chris >>>> >>> >>> The elegant way is to have a batch directory on your PATH ( I use ?m:\t\bat ) ?and put your *.bat files there. ? I do NOT put any python >>> installations on the PATH. >>> >>> For example, I have a batch file called: ? ? m:\t\bat\python26.bat >>> >>> c:\progfiles\python26\python.exe %* >>> >>> The %* syntax means pass all arguments through to the program. >>> >>> Once it all works, you can add an "@" in front of the c: ? in order to >>> suppress echoing the command. ?At that point, it'll look and work the >>> same way as what you did, but without modifying anything in the install >>> directory. ? ?(You may want ?python26.bat, pythonw26.bat, python31.bat >>> and pythonw31.bat) >>> >>> The other thing you may want to do in a batch file is to change the file >>> associations so that you can run the .py file directly, without typing >>> "python" or "pythonw" in front of it. >>> >>> The relevant Windows commands are: ? ? assoc and ftype ? ? ?And on a >>> related note, you may want to edit the PATHEXT environment variable, to >>> add .PY and .PYW >>> >> >> Thanks for this - this way made a bit more sense to me. ?I've now got >> C:\commands with the 4 .bat files in, and C:\commands in my path. ?It >> all seems to work :) I think I've missed the point of the @ though - >> it doesn't seem to make any difference.. >> >> I'm also a bit confused with the associations - since I've got python >> 2.6 and 3.1, surely the command I type (python26 or python31) is the >> only way to force a script to be run using a specific interpreter at >> runtime without having to change the .bat file every time I want to >> run a script using 3.1 instead of 2.6? >> >> > > The @ symbol was used in older versions of CMD and COMMAND to suppress echoing of the command line. ?I think if you're using XP or later, it doesn't matter. > > As for file associations, I don't know just what you already know about. ?When you type ? ?data.doc ? ?at a command line, the system looks for the data file, then if it's found, it looks up the program associated with that extension. ?On my machine, that's a particular version of Word for Windows. ?Similarly, if I type ? ?digest.py ? and I'm in the directory containing that file, the system looks up my associations, and runs Python 2.6 on that file. ?If I wanted it to run Python 3.1 on that file, I'd have to change the associations, temporarily. > > So, first approximation, it's a way to avoid having to type PYTHON each time I want to run a script, as long as I don't need any other arguments on the (implied) command line. ?Further, the system will then look for digest.py everywhere on the PATH, so I don't even have to be in the same directory. ?And that means I can be in the data directory that digest.py is going to work on. ?Effectively, it raises digest.py to feeling like an executable, for most purposes. ?In fact, I put all my scripts in a directory, along with other simple executables. ? I use ?m:\t\bin ?for that purpose. > > These associations (to .py and .pyw) are established by the installer program for Python. ?But when you had multiple installs, you could choose whether the second one installed overrides the first. ?My point is that if you need to change them back and forth, you could use ?assoc and ftype to do it. > > On my system: > > M:\LabOrders>assoc .py > .py=Python.File > > M:\LabOrders>ftype Python.File > Python.File="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* > > M:\LabOrders> > > > So my .py files are associated with the 2.6.2 version of ActivePython installation. ? Note the similarity of the ftype to your python26.bat file? > > One more little trick is that I could just type ? digest ? ?instead of ? ?digest.py, ? if I have added the .py and .pyw extensions to my ? PATHEXT environment variable. ? ?So my PATHEXT looks like: > ?PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY;.PYW > > > > Note that the association changes made by ?assoc and ftype are global. ?They apply to all cmd.exe windows, even those already running, and they survive a reboot. ?But when you set environment variables like PATHEXT, ?you can choose to do it in a single window (with SET), or globally (using control-panel). > > If I found myself switching often, I'd make separate ftype entries for each interpreter, something like > ?Python.File26="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %* > ?Python.File31="C:\PROGFI~1\..... > and just change assoc .py ?to point at the one I needed today. > > As it is, I'm content leaving the associations pointing at 2.6, and explicitly entering PYTHON31 in front of any script name I want to run on that interpreter. > > DaveA > > > > > ---------- Forwarded message ---------- > From:?MRAB > To:?python-list at python.org > Date:?Wed, 22 Jul 2009 14:12:18 +0100 > Subject:?Re: Changing the private variables content > Ryniek90 wrote: >>> >>> ------------------------------------------------------------------------ >>> >>> Temat: >>> Re: Changing the private variables content >>> Od: >>> Gary Herron >>> Data: >>> Tue, 21 Jul 2009 14:14:44 -0700 >>> Do: >>> Ryniek90 >>> >>> Do: >>> Ryniek90 >>> Kopia: >>> python-list at python.org >>> >>> >>> Ryniek90 wrote: >>>> >>>> Hi. >>>> I'm writing some class, and decided to use inside private method and some private variables. While with method i haven't got any problem's with variables i have. >>>> Maybe some example. >>>> A class with private method and private variable: >>>> >>>> " >>>> >>> class Secret(object): >>>> ? # >>>> ? def __init__(self): >>>> ? ? ? self._number = 1 >>>> ? # >>>> ? def _secret(self): >>>> ? ? ? print self._number >>>> ? def showit(self): >>>> ? ? ? print "Secret number is:\n" >>>> ? ? ? self._secret() >>>> >>>> >>> sec = Secret() >>>> >>> sec.showit() >>>> Secret number is: >>>> >>>> 1 >>>> >>> sec._number >>>> 1 >>>> >>> sec._number = 2 >>>> >>> sec._number >>>> 2 >>>> >>> sec._number += 3 >>>> >>> sec._number >>>> 5 >>>> >>> >>>> " >>>> >>>> As You can see, i made class with private method and private variable inside __init__ constructor. I've changed also the variable value, but outside class. >>>> I've got problem with changing some variable value _inside__my_ class, which i'm writing. >>> >>> Not sure this is what you are asking, but a method (which is how I interpret "_inside__my_ class") changes the value by normal assignment like this: >>> >>> class Secret(object): >>> ?... >>> ?def SetNumber(self,value): >>> ? self._number = value >>> >>> Is that what you were asking? >>> >>> >>> Gary Herron >>> >>> >>> >>>> I've searched over google, some tuts with topics about operations on private variables, but didn't found anything - only how to make them, but no how to assign new objects/override them with new content (and other advanced and helpful options). >>>> >>>> If someone could help me, it would be great. >>>> >>>> Thanks. >>> >>> >> >> >> Thanks for hint, but looks like i can't do what i want. >> >> Maybe i show You my class: >> >> " >> >> class ModPrint(object): >> ? ? u""" >> ? This will be the doc. >> ? """ >> ? ? def __init__(self): >> ? ? ? ? ? ? #Assign the Python installation directory - sys.exec_prefix - to variable >> ? ? ? self._default_search_path=sys.exec_prefix >> ? ? ? self._chosen_module = '' >> ? ? ? self._user_search_path = '' >> ? ? ? self._this_module = '' >> ? ? ? ? def _SetVar(self, attr, val): >> ? ? ? self.attr = val ? ? ? ? ? ?def _search_for_module(self, *args): >> ? ? ? ? ? ? """Private method which walks through default Python installation directories, and search for prefered module.""" >> ? ? ? ? ? ? #walking thru all directories available in path '_user_search_path' >> ? ? ? for root, dirs, files in os.walk(self._user_search_path): >> ? ? ? ? ? for f in files: >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? #if found file 'f' is THAT module, >> ? ? ? ? ? ? ? #full path to THAT file is assigned to variable >> ? ? ? ? ? ? ? if f == ("%s.py" % self._chosen_module): >> ? ? ? ? ? ? ? ? ? self._SetVar(self._this_module, os.path.join(root, f)) >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? def print_module(self, _chosen_module='', _user_search_path='', _default_search_path=sys.exec_prefix,): >> ? ? ? ? ? ? """Reads module chosen by user, and returns full content of this module, as it is.""" >> ? ? ? ? ? ? ? ? ? #if custom search path hasn't been assigned, >> ? ? ? #default path is being assigned as custom path >> ? ? ? if self._user_search_path == '': >> ? ? ? ? ? self._user_search_path = self._default_search_path >> ? ? ? ? ? ? ? ? #using private method '_search_for_module' with 'self.' preffix >> ? ? ? #to search for prefered module >> ? ? ? self._search_for_module(_chosen_module, _user_search_path) >> ? ? ? ? ? ? #opening prefered module with read-only binary mode >> ? ? ? #and assigning it to 'module_open' variable >> ? ? ? module_open = open(self._this_module, 'rb') >> ? ? ? ? ? ? #reading this file and assigning it to variable >> ? ? ? module_text = module_open.read() >> ? ? ? ? ? ? #closing read file; the read content is still available >> ? ? ? #it's stored in variable 'module_text' >> ? ? ? module_open.close() >> ? ? ? ? ? ? #returning the read content >> ? ? ? return module_text >> >> " >> >> When i use this class in Python IDLE, i've got this error: >> " >> ?>>> mod = ModPrint() >> ?>>> import socket >> ?>>> mod.print_module('socket') >> >> Traceback (most recent call last): >> ?File "", line 1, in >> ? mod.print_module('socket') >> ?File "", line 48, in print_module >> ? module_open = open(self._this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> ?>>> >> " >> >> As You can see, i can't assign the new value "os.path.join(root, f)" to the 'self._this_module variable'. >> So for sure i've made some mistake in method: >> >> " >> def _SetVar(self, attr, val): >> ? ? ? self.attr = val ? " >> When i've changed private variables to normal, stored in class (in __init__ method), it was the same situation - i couldn't change this variable value. >> >> " >> ?>>> mod = ModPrint() >> ?>>> mod.print_module('os') >> >> Traceback (most recent call last): >> ?File "", line 1, in >> ? mod.print_module('os') >> ?File "", line 48, in print_module >> ? module_open = open(self.this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> ?>>> >> " >> >> Thanks i someone could help me, give some hint. > > What is the point of the _SetVar method? > > Instead of: > > ? ?self._SetVar(self._this_module, os.path.join(root, f)) > > just do: > > ? ?self._this_module = os.path.join(root, f) > > (unless I'm misunderstanding what you're trying to do!) > > > -- > http://mail.python.org/mailman/listinfo/python-list > From inky788 at gmail.com Wed Jul 22 10:36:51 2009 From: inky788 at gmail.com (Inky 788) Date: Wed, 22 Jul 2009 07:36:51 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> On Jul 22, 2:36?am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > On Jul 20, 12:27?pm, Phillip B Oldham > > > wrote: > > > [snip] We > > > understand that lists are mutable and tuples are not, but we're a > > > little lost as to why the two were kept separate from the start. They > > > both perform a very similar job as far as we can tell. > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. ? Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm just not sophisticated enough, but I've never wanted to use a list/ tuple as a dict key. This sounds like obscure usage, and a bit contrived as a reason for having *both* lists and tuples. From davea at ieee.org Wed Jul 22 10:38:14 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 10:38:14 -0400 Subject: Ideas for problem with chat server application! In-Reply-To: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> References: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> Message-ID: <4A672456.9010604@ieee.org> David Adamo Jr. wrote: > I developed a chat application with an attendant chat server. > Everything is working fine. The issue now is the fact that whenever > the chat server goes down (for instance, the server system shuts down > as a result of power failure or some other problem), by the time the > server system come back on, the chat server would have to be restarted > manually. > > I believe (and I know) it is more appropriate for the chat server > application to restart itself when the computer comes back on (and of > course regardless of who is logged in and of course, even before > anyone logs in). I have a batch file that executes the chat server. My > attempt was to create a windows service that start automatically and > runs this batch file using a Network Service account on the server > system. Although, I'm having a hard time with this (temporarily), I > would love to ask if there are any alternatives to using a windows > service. Suggestions are highly appreciated. > > As a starting place, take a look at the Windows Scheduler. One of the options is to schedule something at "boot." That happens before anybody logs on to the system. I don't know whether you can specify what user account will be used, but you can certainly launch a python program from there. DaveA From inky788 at gmail.com Wed Jul 22 10:46:06 2009 From: inky788 at gmail.com (Inky 788) Date: Wed, 22 Jul 2009 07:46:06 -0700 (PDT) Subject: Documentation Problems References: Message-ID: On Jul 22, 7:15?am, Tim Golden wrote: > Mark du Preez wrote: > > Hi > > > Can anyone tell me where to go to suggest changes to the Python > > documentation? > > Drop an entry in the tracker: > > ?http://bugs.python.org > > Patches are always welcome, Could you please provide brief instructions on exactly how to go about creating a suitable patch file? I mean, starting from the beginning (as in, "check out code from {here} using {this command}", "cd to {this} directory", "edit the file in place", "run {this} command to create a patch file"). Or perhaps this procedure is already outlined elsewhere? From f.orndorffplunkett at gmail.com Wed Jul 22 10:46:09 2009 From: f.orndorffplunkett at gmail.com (F.O.P.) Date: Wed, 22 Jul 2009 07:46:09 -0700 (PDT) Subject: Not understanding this documentation Message-ID: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> http://bugs.python.org/issue5358 I simply installed 3.0 on my ubuntubox brought my project from a windows machine and made necessary changes to what I had used from os and os.path. I have an error at the end of this line: for k in range(I): and the error: SyntaxError: invalid character in identifier I've already declared I way above as the lenth of an array (I = len (IA)) There shouldn't be a problem there aren't any issues with other declarations identical to this. From george.sakkis at gmail.com Wed Jul 22 10:55:38 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Wed, 22 Jul 2009 07:55:38 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> Message-ID: <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> On Jul 22, 7:38?am, William Dode wrote: > I updated the script (python, c and java) with your unrolled version > + somes litle thinks. > > I also tried with python3.1, unladen Q2, ironpython1.1.1 > > Unfortunately it doesn't work more with shedskin, i'll see on the > shedskin group... > > c 1.85s > gcj 2.15s > java 2.8s > python2.5 + psyco 3.1s > unladen-2009Q2 145s (2m45) > python2.5 254s (4m14s) > python3.1 300s (5m) > ironpython1.1.1 680s (11m20) Cool; it would be interesting to see the numbers for Jython and Boo as well if it's not too much effort. George From kyrie at uh.cu Wed Jul 22 11:01:20 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Wed, 22 Jul 2009 11:01:20 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: <1248274880.4a6729c036a68@comuh.uh.cu> Quoting Inky 788 : > > The good reason is the immutability, which lets you use > > a tuple as a dict key. ? > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. I don't seem to understand your definition of obscure and contrived. It seems that you got late to this thread, and you missed the examples. I'd suggest you to go back on this thread and look at them. heights = {} heights[1,2] = 5 heights[1,3] = 7 heights[3,5] = 1 addresses[lastname, firstname] = my_home_address census[location, age] = census.get((location, age), 0) + 1 All those are using tuples as dict keys. Regards, -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From rhodri at wildebst.demon.co.uk Wed Jul 22 11:07:53 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 22 Jul 2009 16:07:53 +0100 Subject: Not understanding this documentation In-Reply-To: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: On Wed, 22 Jul 2009 15:46:09 +0100, F.O.P. wrote: > http://bugs.python.org/issue5358 > I simply installed 3.0 on my ubuntubox brought my project from a > windows machine and made necessary changes to what I had used from os > and os.path. I have an error at the end of this line: > for k in range(I): > and the error: > SyntaxError: invalid character in identifier > > I've already declared I way above as the lenth of an array (I = len > (IA)) > There shouldn't be a problem there aren't any issues with other > declarations identical to this. Could you show us slightly more of your code and the rest of the error message, please? I have a suspicion that the error actually lies in the "necessary changes" you made. -- Rhodri James *-* Wildebeest Herder to the Masses From phillip.oldham at gmail.com Wed Jul 22 11:09:31 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Wed, 22 Jul 2009 08:09:31 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> Message-ID: <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> On Jul 22, 2:23?pm, Casey Webster wrote: > I can't answer your question, but I would like to better understand > the > problem you are trying to solve. ?The Apache Hadoop/MapReduce java > application isn't really that "large" by modern standards, although it > is generally run with large heap sizes for performance (-Xmx1024m or > larger for the mapred.child.java.opts parameter). > > MapReduce is designed to do extremely fast parallel data set > processing > on terabytes of data over hundreds of physical nodes; what advantage > would a pure Python approach have here? We're always taught that it is a good idea to reduce the number of dependencies for a project. So you could use Disco, or Dumbo, or even Skynet. But then you've introduced another system you have to manage. Each new system will have a learning curve, which is lessened if the system is written in an environment/language you already work with/ understand. And since a large cost with environments like erlang and java is in understanding them any issues that are out of the ordinary can be killer; changing the heap size as you mentioned above for Java could be one of these issues that a non-java dev trying to use Hadoop might come across. With the advent of cloud computing and the new semi-structured/ document databases that are coming to the fore sometimes you need to use MapReduce on smaller/fewer systems to the same effect. Also, you may need only to ensure that a job is done in a timely fashion without taking up too many resources, rather than lightening-fast. Dumbo/disco in these situations may be considered overkill. Implementations like BashReduce are perfect for such scenarios. I'm simply wondering if there's another simpler/smaller implementation of MapReduce that plays nicely with Python but doesn't require the setup/ knowledge overhead of more "robust" implementations such as hadoop and disco... maybe similar to Ruby's Skynet. From davea at ieee.org Wed Jul 22 11:10:35 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 11:10:35 -0400 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <4A672BEB.6010008@ieee.org> superpollo wrote: >
hello clp. > > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? > > bye > >
> There's no way to get a binary value directly into a literal, but you can convert one into a string value, as follows: x = 0b00101101 print x print chr(x) output is: 45 - Note that this is decimal 45, which is different than the hex 45 you used in your first example. For doing multiple characters, I can't see anything better than: lis = [0b00101101, 0b01000101, 0b01100001] print lis s= "".join(chr(x) for x in lis) print s DaveA From hackingkk at gmail.com Wed Jul 22 11:35:19 2009 From: hackingkk at gmail.com (Krishnakant) Date: Wed, 22 Jul 2009 21:05:19 +0530 Subject: challenging problem for changing to a dedicated non-privileged user within a script. Message-ID: <1248276919.8402.6.camel@krishna-laptop> hello all, This is a real challenge and I don't know if a solution even exists for this or not. I am writing an application which I run as my usual user on ubuntu. the usernake is let's say kk and it has sudo permission (meaning the user is in the sudoers list ). now when i do python myscript.py, the script has to change to another non-privileged user for some tasks. let's say for example switch to the postgres user which is dedicated for postgres and has no other privileges. I have tryed doing os.setuid(112) where 112 could be the uid of the user I want the script to swith over. but I got opperation not permitted. I tryed using subprocess but that did not help me either. I tryed sudo su into the Popen command but it throws me into the terminal (shell) with postgres as the user. But that's now my desired result. what I exactly want is that the script now continues to execute under postgres user till the end. I don't know how to achieve this iffect. Infact I will need this during a serious deployment because i would have my application run as a demon as a dedicated user. I am finding some code for deamonising a python application but don't know how to tell the script to change user. happy hacking. Krishnakant. From davea at ieee.org Wed Jul 22 11:39:51 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 22 Jul 2009 11:39:51 -0400 Subject: Changing the private variables content In-Reply-To: <4A671BAF.1060204@gmail.com> References: <4A671BAF.1060204@gmail.com> Message-ID: <4A6732C7.7070909@ieee.org> Ryniek90 wrote: > > > > It looks like private variable have specific naure, that prevent from > traditional editing them. > Still searching for some tuts about private methods and variables. > > There's no tutorial on private variables for good reason: Private variables have no special characteristics. The compiler and runtime library don't treat them specially (except for the highly controversial from xx import *). It's strictly a programming convention. You've got some other bugs in your code, which I can't help you with because your code comes across formatted so strangely that it cannot compile here. Some things you might do to make it easier to get help: 1) make sure you're not mixing tabs and spaces in your source code 2) post using text messages, so they don't get reformatted. 3) copy/paste the code and error messages into your message, don't retype. 4) whenever you've made more than trivial changes to the code, repost it all, since it's very difficult to tell which suggestions you've followed, and which ones you haven't gotten yet, or maybe dismissed because they didn't work. DaveA From python at bdurham.com Wed Jul 22 11:42:07 2009 From: python at bdurham.com (python at bdurham.com) Date: Wed, 22 Jul 2009 11:42:07 -0400 Subject: Suggestions for Python MapReduce? In-Reply-To: References: Message-ID: <1248277327.15700.1326268767@webmail.messagingengine.com> Phillip, We've had great success writing simple, project specific algorithms to split content into chunks appropriate for ETL type, Python based processing in a hosted cloud environment like Amazon EC2 or the recently launched Rackspace Cloud Servers. Since we're purchasing our cloud hosting time in 1 hour blocks, we divide our data into much larger chunks than what a traditional map-reduce technique might use. For many of our projects, the data transfer time to and from the cloud takes the majority of clock time. Malcolm From jeanmichel at sequans.com Wed Jul 22 12:07:07 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 22 Jul 2009 18:07:07 +0200 Subject: Changing the private variables content In-Reply-To: <02770500$0$5185$c3e8da3@news.astraweb.com> References: <02770500$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A67392B.3090602@sequans.com> Steven D'Aprano wrote: > On Wed, 22 Jul 2009 14:29:20 +0200, Ryniek90 wrote: > > >> When i use this class in Python IDLE, i've got this error: " >> > ... > >> Traceback (most recent call last): >> File "", line 1, in >> mod.print_module('socket') >> File "", line 48, in print_module >> module_open = open(self._this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> >>> >> " >> >> As You can see, i can't assign the new value "os.path.join(root, f)" to >> the 'self._this_module variable'. >> > > In the __init__ method, you successfully set the `_this_module` attribute: > > self._this_module = '' > > That works, so it proves that you *can* set a private attribute. > > Later on, you call the `_SetVar` method, which does this: > > def _SetVar(self, attr, val): > self.attr = val > > In English, this method does this: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute named exactly "attr". > > What you actually want is: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute with the name stored > in the variable "attr". > > Can you see the difference? > > What you actually want is: > > def _SetVar(self, attr, val): > setattr(self, attr, val) > > > Your code is terribly complicated. This will probably do what you want. > (Warning -- I haven't tested it, so there may be a few errors.) > > > class ModPrint(object): > u""" > This will be the doc. > """ > def __init__(self, default_search_path=''): > self.default_search_path = '' > > def find_module(self, modulename, search_path=''): > if search_path == '': > # Try the default search path instead. > search_path = self.default_search_path > if search_path == '': # still blank. > # Try the Python search path instead. > search_path = sys.exec_prefix > for root, dirs, files in os.walk(search_path): > for f in files: > if f == "%s.py" % modulename: > return os.path.join(root, f) > > def get_module_text(self, modulename, search_path=''): > filename = self.find_module(modulename, search_path) > module_open = open(filename, 'rb') > module_text = module_open.read() > module_open.close() > return module_text > > > I changed the name of print_module_text because it doesn't actually print > anything. > > > > Moreover, if I'm not wrong there is absolutely no difference in the way python is handling private and public attributes, for python, both are the same kind of attributes. This is purely conventional (some doc processing tool will take into account the private prefix). So there is absolutely no restriction in writing self._myVar = 0 in any of the instance methods. JM From ryniek90 at gmail.com Wed Jul 22 12:10:52 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 22 Jul 2009 18:10:52 +0200 Subject: Python-list Digest, Vol 70, Issue 282 In-Reply-To: References: Message-ID: <4A673A0C.2000807@gmail.com> > >> When i use this class in Python IDLE, i've got this error: " >> > ... > >> Traceback (most recent call last): >> File "", line 1, in >> mod.print_module('socket') >> File "", line 48, in print_module >> module_open = open(self._this_module, 'rb') >> IOError: [Errno 2] No such file or directory: '' >> >>> >> " >> >> As You can see, i can't assign the new value "os.path.join(root, f)" to >> the 'self._this_module variable'. >> > > In the __init__ method, you successfully set the `_this_module` attribute: > > self._this_module = '' > > That works, so it proves that you *can* set a private attribute. > > Later on, you call the `_SetVar` method, which does this: > > def _SetVar(self, attr, val): > self.attr = val > > In English, this method does this: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute named exactly "attr". > > What you actually want is: > > (1) Take two arguments, and store them in variables called "attr" and > "val". > > (2) Assign the value stored in "val" to an attribute with the name stored > in the variable "attr". > > Can you see the difference? > > What you actually want is: > > def _SetVar(self, attr, val): > setattr(self, attr, val) > > > Your code is terribly complicated. This will probably do what you want. > (Warning -- I haven't tested it, so there may be a few errors.) > > > class ModPrint(object): > u""" > This will be the doc. > """ > def __init__(self, default_search_path=''): > self.default_search_path = '' > > def find_module(self, modulename, search_path=''): > if search_path == '': > # Try the default search path instead. > search_path = self.default_search_path > if search_path == '': # still blank. > # Try the Python search path instead. > search_path = sys.exec_prefix > for root, dirs, files in os.walk(search_path): > for f in files: > if f == "%s.py" % modulename: > return os.path.join(root, f) > > def get_module_text(self, modulename, search_path=''): > filename = self.find_module(modulename, search_path) > module_open = open(filename, 'rb') > module_text = module_open.read() > module_open.close() > return module_text > > > I changed the name of print_module_text because it doesn't actually print > anything. > Thanks for ready class - it works great, but that code: " What you actually want is: def _SetVar(self, attr, val): setattr(self, attr, val) " Didn't worked, so i wil reuse your class, and modify it later. Now i see that working on private objects complex. From junhufr at gmail.com Wed Jul 22 12:13:37 2009 From: junhufr at gmail.com (Jun) Date: Wed, 22 Jul 2009 09:13:37 -0700 (PDT) Subject: Copy/paste through LAN Message-ID: Hello, I've a client/server application which uses Pyro to communicate each other. Now i try to implement copy/paste through LAN between server controlled filesystem to my local windows machine (I could list files in client's window). How can i pass the url information through Pyro ? Thank you in advance. Jun From tjreedy at udel.edu Wed Jul 22 12:28:04 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:28:04 -0400 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: superpollo wrote: > hello clp. > > i can insert a hex value for a character literal in a string: > > >>> stuff = "\x45" > >>> print stuff > E > >>> > > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? Language Ref 2.4. Literals From rNOSPAMon at flownet.com Wed Jul 22 12:36:28 2009 From: rNOSPAMon at flownet.com (Ron Garret) Date: Wed, 22 Jul 2009 09:36:28 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: In article , Neil Hodgson wrote: > milanj: > > > and all of them use native threads (python still use green threads ?) > > Python uses native threads. But then it adds the global interpreter lock, which completely undermines the utility of native threads. So yes, it uses native threads, but it does not actually realize the benefits of that use. rg From sjmachin at lexicon.net Wed Jul 22 12:37:16 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 22 Jul 2009 09:37:16 -0700 (PDT) Subject: Not understanding this documentation References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: On Jul 23, 12:46?am, "F.O.P." wrote: > http://bugs.python.org/issue5358 > I simply installed 3.0 on my ubuntubox brought my project from a > windows machine and made necessary changes to what I had used from os > and os.path. ?I have an error at the end of this line: > for k in range(I): > and the error: > SyntaxError: invalid character in identifier > > I've already declared I way above as the lenth of an array (I = len > (IA)) > There shouldn't be a problem there aren't any issues with other > declarations identical to this. You are have used the --ftniv and --strict options and hence it's objecting to the lower-case "k". From tjreedy at udel.edu Wed Jul 22 12:38:22 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:38:22 -0400 Subject: Documentation Problems In-Reply-To: <7coan1F28i1q2U1@mid.uni-berlin.de> References: <7coan1F28i1q2U1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > Mark du Preez wrote: > >> Hi >> >> Can anyone tell me where to go to suggest changes to the Python >> documentation? >> >> Thanks. > > http://docs.python.org/ has a "bugs"-section that leads to > > http://docs.python.org/bugs.html > > You can file doc-bugs in the tracker, and check if they are already fixed in > the dev-docs. Actually, check first at http://docs.python.org/dev/py3k/ for 3.2a http://docs.python.org/dev/ for 2.7a These are updated often, perhaps nightly. Then file if you still think a change is needed. For small changes, a specific suggested rewording is sufficient. "Change 'tihs' to 'this'." "Between 'Sentence 1' and 'sentence 2', I suggest adding 'sentence 3'." The doc maintainers are happy to do the rst formatting themselves. From tjreedy at udel.edu Wed Jul 22 12:44:08 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:44:08 -0400 Subject: doted filenames in import statements In-Reply-To: <4A670313.8040204@sequans.com> References: <4A670313.8040204@sequans.com> Message-ID: Jean-Michel Pichavant wrote: > Piet van Oostrum wrote: >> >> [snip] >> >>> JP> file = "/home/dsp/4.6.0.0/test.py" >>> JP> test = __import__(file) >>> JP> => no module name blalalal found. >>> >> >> >>> JP> Any suggestion ? I tried multiple escape technics without any >>> success. >>> >> >> Rightly so. >> >> I think the best would be to add the directory to sys.path >> sys.path.add('/home/dsp/4.6.0.0') >> and then >> __import__('test', ... ) > > I see. My problem is that a have to import 2 different files having de > same name. In the same name space I have 2 objects from 2 different > software branches, let's say 4.6.0 and 4.6.1. > The first object shall import 4.6.0/orb.py and the second one 4.6.1/orb.py. > > If I add 4.6.1 to sys.path, the import statement will look like: > self._orb = __import__('orb') > The problem is, python wil assume orb is already imported and will > assign the module from the 4.6.0 branch to my 4.6.1 object. > > Do I have to mess up with sys.modules keys to make python import the > correct file ? Is there a standard/proper way to do that ? If you make the directory names into proper identifiers like v460 and v461 and add __init__.py to each to make them packages and have both on search path, then import v460.orb #or import v460.orb as orb460 import v461.orb #or import v460.orb as orb461 will get you both. One way or another, they have to get different names within Python. Terry Jan Reedy From wilk at flibuste.net Wed Jul 22 12:45:19 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 16:45:19 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> Message-ID: <4a67421e$0$31038$426a74cc@news.free.fr> On 22-07-2009, George Sakkis wrote: > On Jul 22, 7:38?am, William Dode wrote: > >> I updated the script (python, c and java) with your unrolled version >> + somes litle thinks. >> >> I also tried with python3.1, unladen Q2, ironpython1.1.1 >> >> Unfortunately it doesn't work more with shedskin, i'll see on the >> shedskin group... >> >> c 1.85s >> gcj 2.15s >> java 2.8s >> python2.5 + psyco 3.1s >> unladen-2009Q2 145s (2m45) >> python2.5 254s (4m14s) >> python3.1 300s (5m) >> ironpython1.1.1 680s (11m20) > > Cool; it would be interesting to see the numbers for Jython and Boo as > well if it's not too much effort. I just tried with jython, but oddly it's faster without array. Thanks to Mark, i could compile to shedskin again. And add somes little improvements... c 1.65s gcj 1.9s java 2.4s python2.5 + psyco 2.9s shedskin 3.4s unladen-2009Q2 125s (2m05) Jython 2.2.1 on java1.6.0_12 176s (without array, like shedskin) Jython 2.2.1 on java1.6.0_12 334s (with array) python2.5 215s (3m35s) python3.1 246s (4m06s) ironpython1.1.1 512 (8m32s) -- William Dod? - http://flibuste.net Informaticien Ind?pendant From f.orndorffplunkett at gmail.com Wed Jul 22 12:45:58 2009 From: f.orndorffplunkett at gmail.com (F.O.P.) Date: Wed, 22 Jul 2009 09:45:58 -0700 (PDT) Subject: Not understanding this documentation References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: <081d1a50-fd7f-4f87-889e-262bc7e2debd@x5g2000prf.googlegroups.com> On Jul 22, 9:07?am, "Rhodri James" wrote: > On Wed, 22 Jul 2009 15:46:09 +0100, F.O.P. ? > wrote: > > >http://bugs.python.org/issue5358 > > I simply installed 3.0 on my ubuntubox brought my project from a > > windows machine and made necessary changes to what I had used from os > > and os.path. ?I have an error at the end of this line: > > for k in range(I): > > and the error: > > SyntaxError: invalid character in identifier > > > I've already declared I way above as the lenth of an array (I = len > > (IA)) > > There shouldn't be a problem there aren't any issues with other > > declarations identical to this. > > Could you show us slightly more of your code and the rest of the > error message, please? ?I have a suspicion that the error actually > lies in the "necessary changes" you made. > > -- > Rhodri James *-* Wildebeest Herder to the Masses I spoke to a friend about it and he found that I had put in a unicode "semicolon" rather than ASCII. I feel silly for asking a question like this but there's really no harm done in asking. Thank you for the quick response Rhodri! Much appreciated! Cheers From tjreedy at udel.edu Wed Jul 22 12:47:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:47:15 -0400 Subject: Finding all cycles within an undirected graph In-Reply-To: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> References: <15dd54ec0907220604gf7f760w98cad7d6f042ebac@mail.gmail.com> Message-ID: disappearedng wrote: > Hey everyone, > I am interested to find out all the cycles within an undirected graph. I think you have to qualify that as 'minimal cycles' to have a well-defined problem. From http Wed Jul 22 12:55:51 2009 From: http (Paul Rubin) Date: 22 Jul 2009 09:55:51 -0700 Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> Message-ID: <7xws60u06g.fsf@ruckus.brouhaha.com> Phillip B Oldham writes: > Implementations like BashReduce mapreduce-bash-script> are perfect for such scenarios. I'm simply > wondering if there's another simpler/smaller implementation of > MapReduce that plays nicely with Python but doesn't require the setup/ > knowledge overhead of more "robust" implementations such as hadoop and > disco... maybe similar to Ruby's Skynet. I usually just spew ssh tasks across whatever computing nodes I can get my hands on. It's less organized than something like mapreduce, but I tend to run one-off tasks that I have to keep an eye on anyway. I've done stuff like that across up to 100 or so machines and I think it wouldn't be really worse if the number were a few times higher. I don't think it would scale to really large (10,000's of nodes) clusters. From tjreedy at udel.edu Wed Jul 22 12:59:01 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 12:59:01 -0400 Subject: Not understanding this documentation In-Reply-To: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> Message-ID: F.O.P. wrote: Your subject line should have been about "SyntaxError message" > http://bugs.python.org/issue5358 Why reference this? Unless you added zero-width joiners in the move, this is irrelevant noise. > I simply installed 3.0 on my ubuntubox Upgrade to 3.1 as soon as you can. Works better. > brought my project from a > windows machine and made necessary changes to what I had used from os > and os.path. I have an error at the end of this line: > for k in range(I): > and the error: > SyntaxError: invalid character in identifier > > I've already declared I way above as the lenth of an array (I = len > (IA)) > There shouldn't be a problem there aren't any issues with other > declarations identical to this. I otherwise concur with R. James as there is no syntax error posted. To make sure there is nothing invisible in that line, do print(repr("for k in range(I):")) # should be no escaped chars and print(len("for k in range(I):")) # should be 18, I believe tjr From pavlovevidence at gmail.com Wed Jul 22 13:01:31 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 10:01:31 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> On Jul 22, 9:36?am, Ron Garret wrote: > In article , > ?Neil Hodgson wrote: > > > milanj: > > > > and all of them use native threads (python still use green threads ?) > > > ? ?Python uses native threads. > > But then it adds the global interpreter lock, which completely > undermines the utility of native threads. ?So yes, it uses native > threads, but it does not actually realize the benefits of that use. Wrong. It only partially undermines the utility of native threads, not completely. Native threading allows some threads to run while others are blocked in a system call (as well as in a few other minor cases), which can't be done with green threads. Carl Banks From lie.1296 at gmail.com Wed Jul 22 13:19:02 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 23 Jul 2009 03:19:02 +1000 Subject: Documentation Problems In-Reply-To: References: Message-ID: Inky 788 wrote: > On Jul 22, 7:15 am, Tim Golden wrote: >> Mark du Preez wrote: >>> Hi >>> Can anyone tell me where to go to suggest changes to the Python >>> documentation? >> Drop an entry in the tracker: >> >> http://bugs.python.org >> >> Patches are always welcome, > > Could you please provide brief instructions on exactly how to go about > creating a suitable patch file? I mean, starting from the beginning > (as in, "check out code from {here} using {this command}", "cd to > {this} directory", "edit the file in place", "run {this} command to > create a patch file"). Or perhaps this procedure is already outlined > elsewhere? It's the typical svn-based development, "svn checkout" from the repository (see list of svn urls here: http://www.python.org/dev/faq/#how-do-i-get-a-checkout-of-the-repository-read-only-and-read-write), modify the files, then "svn diff" (or "diff -u") And last but not least, check out the dev-faq: http://www.python.org/dev/faq/ From http Wed Jul 22 13:20:20 2009 From: http (Paul Rubin) Date: 22 Jul 2009 10:20:20 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> Message-ID: <7xskgobpnv.fsf@ruckus.brouhaha.com> Carl Banks writes: > Wrong. It only partially undermines the utility of native threads, > not completely. Native threading allows some threads to run while > others are blocked in a system call (as well as in a few other minor > cases), which can't be done with green threads. Why is that such an advantage? Green threads work fine if you just organize the i/o system to never block. From king.seth at gmail.com Wed Jul 22 13:32:51 2009 From: king.seth at gmail.com (Seth) Date: Wed, 22 Jul 2009 10:32:51 -0700 (PDT) Subject: Pyserial and pyQt References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> Message-ID: <70e4ebae-908f-4f3a-a96a-6fa0a7a7887a@y19g2000yqy.googlegroups.com> On Jul 21, 7:24?pm, David Boddie wrote: > On Tuesday 21 July 2009 21:37, Seth wrote: > > > I have used pyserial in the past but this is my first experience with > > pyQt. ?I am using the Python xy package for windows current but might > > move to linux. ?I have a small device that is outputting a basic text > > string. ?I want to be able to read this string(from the comm port) and > > update a text box and eventually a graph in pyQt. ?I can't find any > > documentation or tutorials on how to do this. ?If anyone can point me > > in the right direction or give me some tips I would be grateful. > > It seems that someone has already asked a similar question on Stack > Overflow, though perhaps you should start with a simpler solution > and look at more advanced ones later: > > ?http://stackoverflow.com/questions/771988/pyqt4-and-pyserial > > One starting point is this list of tutorials on the PyQt and PyKDE Wiki: > > ?http://www.diotavelli.net/PyQtWiki/Tutorials > > Later, when you want to draw graphs, you might find PyQwt useful: > > ?http://pyqwt.sourceforge.net/ > > You may already be aware that there's also a mailing list for PyQt and > PyKDE: > > ?http://www.riverbankcomputing.com/pipermail/pyqt/ > > Another way to get answers to questions is to join the #pyqt IRC channel > at freenode.net: > > ? irc://irc.freenode.net/ > > David Thanks for the response. I have gone through a lot of the tutorials. All of them(that I saw) seem to just deal will event-based applications ie calculator, image viewer, etc. How do I run pyserial in the background and pass the information to PyQT and refresh the screen? Is there a way to have pyserial run in another thread and pass the information to the UI? Thanks, Seth From lie.1296 at gmail.com Wed Jul 22 13:43:14 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 23 Jul 2009 03:43:14 +1000 Subject: How to document Python code properly for Pydoc In-Reply-To: References: Message-ID: jorma kala wrote: > Hi, > Do you know where I can find the rules for documenting Python code, so > that automatic document generation with Pydoc makes the most of the > comments inserted in the code? > I know about documenting class and method through triple quote just > under the class definition. But how do you comment a specific field or > variable, or how do you document function arguments so that they are > extracted like in javadoc? > Thanks very much pydoc is a simple tool, and doesn't do much. You write in freeform, although generally you'll do something like this: def myfunc(a, b): ''' short description of myfunc longer description of myfunc, if necessary, and typically includes description of the arguments and the behaviors. Also includes the description of the return value. ''' pass pydoc doesn't recognize any special markups. If you want to get more from the docstring, you need other documentation generator such as epydoc, Doxygen, or Sphinx. From phillip.oldham at gmail.com Wed Jul 22 13:51:17 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Wed, 22 Jul 2009 10:51:17 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> <7xws60u06g.fsf@ruckus.brouhaha.com> Message-ID: <83889f6f-1e40-435e-b947-af17964e90af@d32g2000yqh.googlegroups.com> On Jul 22, 5:55?pm, Paul Rubin wrote: > Phillip B Oldham writes: > I usually just spew ssh tasks across whatever computing nodes I can > get my hands on. ?It's less organized than something like mapreduce, > but I tend to run one-off tasks that I have to keep an eye on anyway. I'm thinking of repeated tasks; things you run regularly, but don't need to be blisteringly fast. I'll more than likely use Disco, but if I were to find something more light-weight I'd take a serious look. From wilk at flibuste.net Wed Jul 22 13:55:34 2009 From: wilk at flibuste.net (William Dode) Date: 22 Jul 2009 17:55:34 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> <4a67421e$0$31038$426a74cc@news.free.fr> Message-ID: <4a675296$0$1549$426a74cc@news.free.fr> On 22-07-2009, William Dode wrote: > c 1.65s > gcj 1.9s > java 2.4s > python2.5 + psyco 2.9s > shedskin 3.4s with -bw i have 2.6s > unladen-2009Q2 125s (2m05) > Jython 2.2.1 on java1.6.0_12 176s (without array, like shedskin) > Jython 2.2.1 on java1.6.0_12 334s (with array) > python2.5 215s (3m35s) > python3.1 246s (4m06s) > ironpython1.1.1 512 (8m32s) somebody can test with ironpython on windows ? Anyway, it's very impressive. I wonder if unladen will be so close in the futur. -- William Dod? - http://flibuste.net Informaticien Ind?pendant From pavlovevidence at gmail.com Wed Jul 22 13:59:50 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 10:59:50 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> Message-ID: <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> On Jul 22, 10:20?am, Paul Rubin wrote: > Carl Banks writes: > > Wrong. ?It only partially undermines the utility of native threads, > > not completely. ?Native threading allows some threads to run while > > others are blocked in a system call (as well as in a few other minor > > cases), which can't be done with green threads. > > Why is that such an advantage? ?Green threads work fine if you just > organize the i/o system to never block. ? Because then I don't have to organize the I/O system never to block. Carl Banks From davidj411 at gmail.com Wed Jul 22 14:04:36 2009 From: davidj411 at gmail.com (davidj411) Date: Wed, 22 Jul 2009 11:04:36 -0700 (PDT) Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> <16e1d810-bdf0-46b8-aef1-7e57eeb30547@k19g2000yqn.googlegroups.com> Message-ID: <524a57a9-ba45-459b-b62d-81f527888a4e@j19g2000vbp.googlegroups.com> i never heard of the logging module, but this function seemed simple enough. i assume this link is what you refering to: http://docs.python.org/library/logging.html thanks for the helpful info. i think "Piet van Oostrum" has resolved my issue. good eyes! From dangets at gmail.com Wed Jul 22 14:15:55 2009 From: dangets at gmail.com (DG) Date: Wed, 22 Jul 2009 11:15:55 -0700 (PDT) Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output Message-ID: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> There is probably a better way to do this (please enlighten me, if you know), but what I want to do is get a list of a class' attributes minus whatever the 'builtin' methods are. I would also like to do this for instances of classes as well. I don't want to use __dict__ because I want to get all of the attributes that have been added throughout the inheritance tree, just not the builtin ones. I was wondering if there is a function to return a list of these, so I could perform a 'dir' on the object (class or instance) and filter out the builtin attributes. I know I could create my own list something like ['__class__', '__delattr__', ..., '__weakref__'] and use that. I would see this being fragile to Python version differences as new ones are added or taken away. A more flexible way is to have the list be filled out dynamically by creating a 'junk' class that inherits only from object and storing it's dir() output into the filter list. This is probably what I'll do unless someone knows of a builtin function that will give me a (version-safe) list. Thanks! From dangets at gmail.com Wed Jul 22 14:24:21 2009 From: dangets at gmail.com (DG) Date: Wed, 22 Jul 2009 11:24:21 -0700 (PDT) Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output References: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> Message-ID: On Jul 22, 12:15?pm, DG wrote: > There is probably a better way to do this (please enlighten me, if you > know), but what I want to do is get a list of a class' attributes > minus whatever the 'builtin' methods are. ?I would also like to do > this for instances of classes as well. ?I don't want to use __dict__ > because I want to get all of the attributes that have been added > throughout the inheritance tree, just not the builtin ones. > > I was wondering if there is a function to return a list of these, so I > could perform a 'dir' on the object (class or instance) and filter out > the builtin attributes. ?I know I could create my own list something > like > ['__class__', '__delattr__', ..., '__weakref__'] and use that. ?I > would see this being fragile to Python version differences as new ones > are added or taken away. > > A more flexible way is to have the list be filled out dynamically by > creating a 'junk' class that inherits only from object and storing > it's dir() output into the filter list. ?This is probably what I'll do > unless someone knows of a builtin function that will give me a > (version-safe) list. > > Thanks! Haha, replying to my own post. Here is the implementation of my idea of dynamically creating the filter lists from a 'junk' object. As of now it appears that the dir output of a class is the same as the dir output of an instance, but if that ever changes, this should future proof it. This is only a 4-liner, so it is probably good enough for what I want to do, but I would rather use a builtin/standard way if there is one. class A(object): pass class_filter_methods = dir(A) instance_filter_methods = dir(A()) From dangets at gmail.com Wed Jul 22 14:28:58 2009 From: dangets at gmail.com (DG) Date: Wed, 22 Jul 2009 11:28:58 -0700 (PDT) Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output References: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> Message-ID: <48e4e3e1-7356-451e-845b-46516ead98b7@v37g2000prg.googlegroups.com> On Jul 22, 12:24?pm, DG wrote: > On Jul 22, 12:15?pm, DG wrote: > > > > > There is probably a better way to do this (please enlighten me, if you > > know), but what I want to do is get a list of a class' attributes > > minus whatever the 'builtin' methods are. ?I would also like to do > > this for instances of classes as well. ?I don't want to use __dict__ > > because I want to get all of the attributes that have been added > > throughout the inheritance tree, just not the builtin ones. > > > I was wondering if there is a function to return a list of these, so I > > could perform a 'dir' on the object (class or instance) and filter out > > the builtin attributes. ?I know I could create my own list something > > like > > ['__class__', '__delattr__', ..., '__weakref__'] and use that. ?I > > would see this being fragile to Python version differences as new ones > > are added or taken away. > > > A more flexible way is to have the list be filled out dynamically by > > creating a 'junk' class that inherits only from object and storing > > it's dir() output into the filter list. ?This is probably what I'll do > > unless someone knows of a builtin function that will give me a > > (version-safe) list. > > > Thanks! > > Haha, replying to my own post. ?Here is the implementation of my idea > of dynamically creating the filter lists from a 'junk' object. ?As of > now it appears that the dir output of a class is the same as the dir > output of an instance, but if that ever changes, this should future > proof it. ?This is only a 4-liner, so it is probably good enough for > what I want to do, but I would rather use a builtin/standard way if > there is one. > > class A(object): > ? ? pass > class_filter_methods = dir(A) > instance_filter_methods = dir(A()) Wow, I'm a quick one. Sharp as a marble. 2-liner: class_filter_methods = dir(object) instance_filter_methods = dir(object()) From invalid at invalid Wed Jul 22 14:49:58 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 22 Jul 2009 13:49:58 -0500 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: On 2009-07-22, Ron Garret wrote: > In article , > Neil Hodgson wrote: > >> milanj: >> >> > and all of them use native threads (python still use green threads ?) >> >> Python uses native threads. > > But then it adds the global interpreter lock, which completely > undermines the utility of native threads. So yes, it uses native > threads, but it does not actually realize the benefits of that use. Not all of the time. For library/extension calls that release the GIL, it does. -- Grant Edwards grante Yow! I was making donuts at and now I'm on a bus! visi.com From http Wed Jul 22 15:04:07 2009 From: http (Paul Rubin) Date: 22 Jul 2009 12:04:07 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> Message-ID: <7xfxco35g8.fsf@ruckus.brouhaha.com> Carl Banks writes: > > Why is that such an advantage? ?Green threads work fine if you just > > organize the i/o system to never block. ? > > Because then I don't have to organize the I/O system never to block. We're talking about what a language implementation does behind the scenes, I thought. From ethan at stoneleaf.us Wed Jul 22 15:21:04 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 22 Jul 2009 12:21:04 -0700 Subject: Einstein summation notation In-Reply-To: <7xprbybnso.fsf@ruckus.brouhaha.com> References: <8bfce41e-719d-4d8b-bec8-bf341ea87e38@s9g2000yqd.googlegroups.com> <687a86ae-244e-4c38-a11c-91e4d8eb92f6@g31g2000yqc.googlegroups.com> <069f16b0-8c88-4b10-8fd0-59b35ea49a9c@i6g2000yqj.googlegroups.com> <7x8wj4uxnb.fsf@ruckus.brouhaha.com> <10acd98a-e1c9-4aca-bd20-0131277beec1@t11g2000prh.googlegroups.com> <02701951$0$5185$c3e8da3@news.astraweb.com> <7xljmnahlj.fsf@ruckus.brouhaha.com> <02706d8f$0$5185$c3e8da3@news.astraweb.com> <7xab33s8ik.fsf@ruckus.brouhaha.com> <7xprbybnso.fsf@ruckus.brouhaha.com> Message-ID: <4A6766A0.4000809@stoneleaf.us> Paul Rubin wrote: > Ethan Furman writes: > >>Or if any(p for p in [header, body, footer, whatever, ...]) > > > No need for the genexp: > > if any([header, body, footer, whatever, ...]) > > But, you are using the built-in bool cast either way. Right you are -- and if any([header, body, footer, whatever, ...]) sure looks nicer to me than your original code of if any(p != '' for p in [header, body, footer, otherthing1, ...]) But there a are a lot of decisions and factors to writing these things one way or the other. I'm just glad Python supports the Something vs. Nothing model. ~Ethan~ From tjreedy at udel.edu Wed Jul 22 15:24:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 22 Jul 2009 15:24:19 -0400 Subject: list of 'magic methods' or builtin class methods... want to exclude those from dir output In-Reply-To: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> References: <7959c03f-d2d3-471e-a687-4695a09b3aa0@k13g2000prh.googlegroups.com> Message-ID: DG wrote: > There is probably a better way to do this (please enlighten me, if you > know), but what I want to do is get a list of a class' attributes > minus whatever the 'builtin' methods are. I would also like to do > this for instances of classes as well. I don't want to use __dict__ > because I want to get all of the attributes that have been added > throughout the inheritance tree, just not the builtin ones. > > I was wondering if there is a function to return a list of these, so I > could perform a 'dir' on the object (class or instance) and filter out > the builtin attributes. I know I could create my own list something > like > ['__class__', '__delattr__', ..., '__weakref__'] and use that. I > would see this being fragile to Python version differences as new ones > are added or taken away. > > A more flexible way is to have the list be filled out dynamically by > creating a 'junk' class that inherits only from object and storing > it's dir() output into the filter list. This is probably what I'll do > unless someone knows of a builtin function that will give me a > (version-safe) list. > > Thanks! 3.1 >>> list(filter(lambda s: s[0:2]!='__', dir(object))) [] Now substitute your object for lambda. This assumes of course that you are doing no name-mangling __xxx names. tjr From pavlovevidence at gmail.com Wed Jul 22 15:35:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 12:35:52 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> Message-ID: On Jul 22, 12:04?pm, Paul Rubin wrote: > Carl Banks writes: > > > Why is that such an advantage? ?Green threads work fine if you just > > > organize the i/o system to never block. ? > > > Because then I don't have to organize the I/O system never to block. > > We're talking about what a language implementation does behind the > scenes, I thought. No we're not, we are talking about the whether GIL completely or only partially undermines the use of native threads on Python. I don't think your fantasy async-only all-green-thread langauge implementation is possible anyway. How would you wait on a pipe in one thread, a socket in another, a semaphore in a third? (Are there any popular OSes that offer a unified polling interface to all possible synchronizations?) And what do you do about drivers or libraries that make underlying blocking calls? What if you have a busy calculation going on in the background? Carl Banks From robert.kern at gmail.com Wed Jul 22 15:45:41 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 22 Jul 2009 14:45:41 -0500 Subject: clean way prepend an element to a numpy array In-Reply-To: <7cnr5tF28183hU1@mid.individual.net> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <7cnr5tF28183hU1@mid.individual.net> Message-ID: On 2009-07-22 01:51, greg wrote: > bdb112 wrote: > >> I saw this interest syntax on this site >> x[:0]=0 >> >> I guess that is cute, but could be confusing....(and doesn't work) > > It does if you use an array of the appropriate > type on the right hand side: > > a[:0] = array('i', [0]) Not when 'a' is a numpy array rather than an array.array. -- 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 torriem at gmail.com Wed Jul 22 16:26:04 2009 From: torriem at gmail.com (Michael Torrie) Date: Wed, 22 Jul 2009 14:26:04 -0600 Subject: GTK+ ques In-Reply-To: <1e61b3260907220415g6edb24sb7fe07c83fe346c8@mail.gmail.com> References: <1e61b3260907220415g6edb24sb7fe07c83fe346c8@mail.gmail.com> Message-ID: <4A6775DC.1050003@gmail.com> nipun batra wrote: > I need to develop a GUI preferably in GTK using c or PYGTK.I have little > idea about Python. > Following are requiremnts of the application: > should be GUI > Sould be real time > Should interface serial port > should display moving maps,like google earth > It is basically for Ground control station of a UAV. > How would Python+Pygtk combo work? > is python slower than c/c++ ,if then by how much? > any suggestions for the applcation? All of these things can be done in Python. Using nice math libraries like numpy, math-specific things can be very fast. Whether python is fast enough only you could say. PyGTK is nice, but doesn't have built-in primitives for doing super high level things like moving maps. But there are libraries out there that can do this, one of which is call libchamplain. A blog post about it is here: http://blog.pierlux.com/2009/01/10/libchamplain-and-libchamplain-gtk-028-released/en/ libchamplain does have python bindings I think. From george.sakkis at gmail.com Wed Jul 22 16:27:13 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Wed, 22 Jul 2009 13:27:13 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> <4a67421e$0$31038$426a74cc@news.free.fr> Message-ID: <36aeca4d-0609-4da2-83a4-376c88869667@p23g2000vbl.googlegroups.com> On Jul 22, 12:45?pm, William Dode wrote: > On 22-07-2009, George Sakkis wrote: > > > > > On Jul 22, 7:38?am, William Dode wrote: > > >> I updated the script (python, c and java) with your unrolled version > >> + somes litle thinks. > > >> I also tried with python3.1, unladen Q2, ironpython1.1.1 > > >> Unfortunately it doesn't work more with shedskin, i'll see on the > >> shedskin group... > > >> c 1.85s > >> gcj 2.15s > >> java 2.8s > >> python2.5 + psyco 3.1s > >> unladen-2009Q2 145s (2m45) > >> python2.5 254s (4m14s) > >> python3.1 300s (5m) > >> ironpython1.1.1 680s (11m20) > > > Cool; it would be interesting to see the numbers for Jython and Boo as > > well if it's not too much effort. > > I just tried with jython, but oddly it's faster without array. FYI Jython 2.5 was released last month, you may want to try this instead of 2.2. George From scriptlearner at gmail.com Wed Jul 22 16:51:18 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Wed, 22 Jul 2009 13:51:18 -0700 (PDT) Subject: logging outgoing HTTP POST message and incoming response message Message-ID: I am sending a HTTP POST by using the following codes: opener = urllib2.build_opener(proxyHandler, MultipartPostHandler) params = { "audio" : "http://sample.com/my.wav", "data" : open(file, "rb") } print opener.open(myURL, params).read() How do I log or print out the actual POST message (including headers) that is sent out? How do I log or print out the response headers as well? Thanks From exarkun at divmod.com Wed Jul 22 16:53:17 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Wed, 22 Jul 2009 16:53:17 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: Message-ID: <20090722205317.2543.659910647.divmod.quotient.4286@henry.divmod.com> On Wed, 22 Jul 2009 12:35:52 -0700 (PDT), Carl Banks wrote: >On Jul 22, 12:04?pm, Paul Rubin wrote: >> Carl Banks writes: >> > > Why is that such an advantage? ?Green threads work fine if you just >> > > organize the i/o system to never block. >> >> > Because then I don't have to organize the I/O system never to block. >> >> We're talking about what a language implementation does behind the >> scenes, I thought. > >No we're not, we are talking about the whether GIL completely or only >partially undermines the use of native threads on Python. > >I don't think your fantasy async-only all-green-thread langauge >implementation is possible anyway. How would you wait on a pipe in >one thread, a socket in another, a semaphore in a third? (Are there >any popular OSes that offer a unified polling interface to all >possible synchronizations?) Every OS I can think of can support the three examples you gave here. >And what do you do about drivers or >libraries that make underlying blocking calls? Certainly a point to consider. >What if you have a busy calculation going on in the background? What if you do? Are you suggesting this would somehow prevent I/O from being serviced? I'm not sure why, as long as the implementation pays attention to I/O events. Jean-Paul From deets at nospam.web.de Wed Jul 22 16:54:02 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 22 Jul 2009 22:54:02 +0200 Subject: logging outgoing HTTP POST message and incoming response message In-Reply-To: References: Message-ID: <7cpcjaF2915u5U1@mid.uni-berlin.de> scriptlearner at gmail.com schrieb: > I am sending a HTTP POST by using the following codes: > > opener = urllib2.build_opener(proxyHandler, MultipartPostHandler) > params = { "audio" : "http://sample.com/my.wav", > "data" : open(file, "rb") } > print opener.open(myURL, params).read() > > How do I log or print out the actual POST message (including headers) > that is sent out? > How do I log or print out the response headers as well? You can use proxy-tools such as tcpmon or sniff traffic using wireshark. Diez From scriptlearner at gmail.com Wed Jul 22 17:18:37 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Wed, 22 Jul 2009 14:18:37 -0700 (PDT) Subject: logging outgoing HTTP POST message and incoming response message References: <7cpcjaF2915u5U1@mid.uni-berlin.de> Message-ID: <0b403bd0-759f-49b6-9eac-3d585d877fe3@k13g2000prh.googlegroups.com> On Jul 22, 1:54?pm, "Diez B. Roggisch" wrote: > You can use proxy-tools such as tcpmon or sniff traffic using wireshark. > > Diez Thanks, but I am trying to enable some debug mode to log all outgoing and incoming messages for certain period of time, and running another proxy-tool is not very ideal. It would be great to log it in some log file. From piet at cs.uu.nl Wed Jul 22 17:24:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 23:24:34 +0200 Subject: time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order References: <146e1535-268c-42ff-8d86-09c2fa4bbd1c@h21g2000yqa.googlegroups.com> <16e1d810-bdf0-46b8-aef1-7e57eeb30547@k19g2000yqn.googlegroups.com> <524a57a9-ba45-459b-b62d-81f527888a4e@j19g2000vbp.googlegroups.com> Message-ID: >>>>> davidj411 (d) wrote: >d> i never heard of the logging module, but this function seemed simple >d> enough. >d> i assume this link is what you refering to: >d> http://docs.python.org/library/logging.html >d> thanks for the helpful info. i think "Piet van Oostrum" has resolved >d> my issue. good eyes! Without glasses I wouldn't have noticed it :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 22 17:50:03 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 22 Jul 2009 23:50:03 +0200 Subject: Issue combining gzip and subprocess References: <2YednUGQxvntgfrXnZ2dnUVZ_hWdnZ2d@pdx.net> Message-ID: >>>>> Scott David Daniels (SDD) schreef: >SDD> Piet van Oostrum wrote: >>> ... >>> f = gzip.open(filename, 'w') >>> proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) >>> while True: >>> line = proc.stdout.readline() >>> if not line: break >>> f.write(line) >>> f.close() >SDD> Or even: >SDD> proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) >SDD> with gzip.open(filename, 'w') as dest: >SDD> for line in iter(proc.stdout, ''): >SDD> f.write(line) If it would work. 1) with gzip... is not supported in Python < 3.1 2) for line in iter(proc.stdout), i.e. no second argument. 3) dest <==> f should be the same identifier. Lesson: if you post code either: - test it and copy verbatim from your test, or - write a disclaimer -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 22 18:17:12 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 00:17:12 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. References: Message-ID: >>>>> Krishnakant (K) wrote: >K> hello all, >K> This is a real challenge and I don't know if a solution even exists for >K> this or not. >K> I am writing an application which I run as my usual user on ubuntu. >K> the usernake is let's say kk and it has sudo permission (meaning the >K> user is in the sudoers list ). >K> now when i do python myscript.py, the script has to change to another >K> non-privileged user for some tasks. >K> let's say for example switch to the postgres user which is dedicated for >K> postgres and has no other privileges. >K> I have tryed doing os.setuid(112) where 112 could be the uid of the user >K> I want the script to swith over. >K> but I got opperation not permitted. Being a sudoer is not a privilege to issue the os.setuid system call. It is only a permission to use the sudo command. >K> I tryed using subprocess but that did not help me either. I tryed sudo >K> su into the Popen command but it throws me into the terminal (shell) >K> with postgres as the user. You could execute the command: sudo -u postgres required_command with subprocess. You have another problem then: your password must be supplied unless the NOPASSWD flag is set in the sudoers file. >K> But that's now my desired result. >K> what I exactly want is that the script now continues to execute under >K> postgres user till the end. I don't think that's possible if you start as the user kk. >K> I don't know how to achieve this iffect. >K> Infact I will need this during a serious deployment because i would have >K> my application run as a demon as a dedicated user. >K> I am finding some code for deamonising a python application but don't >K> know how to tell the script to change user. >K> happy hacking. >K> Krishnakant. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pavlovevidence at gmail.com Wed Jul 22 18:17:52 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 22 Jul 2009 15:17:52 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: Message-ID: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> On Jul 22, 1:53?pm, Jean-Paul Calderone wrote: > On Wed, 22 Jul 2009 12:35:52 -0700 (PDT), Carl Banks wrote: > >On Jul 22, 12:04?pm, Paul Rubin wrote: > >> Carl Banks writes: > >> > > Why is that such an advantage? ?Green threads work fine if you just > >> > > organize the i/o system to never block. > > >> > Because then I don't have to organize the I/O system never to block. > > >> We're talking about what a language implementation does behind the > >> scenes, I thought. > > >No we're not, we are talking about the whether GIL completely or only > >partially undermines the use of native threads on Python. > > >I don't think your fantasy async-only all-green-thread langauge > >implementation is possible anyway. ?How would you wait on a pipe in > >one thread, a socket in another, a semaphore in a third? ?(Are there > >any popular OSes that offer a unified polling interface to all > >possible synchronizations?) > > Every OS I can think of can support the three examples you gave here. I guess you would know, but polling on all of these at once has got use more obscure calls than I'm familiar with. On Linux I can use select to wait for pipes and sockets, but not SysV semaphores AFAIK. On Windows it's well known that select only works for sockets. So do all these OSes have some kind of __mega_unifying_poll system call that works for anything that might possibly block, that you can exploit from a user process? > >And what do you do about drivers or > >libraries that make underlying blocking calls? > > Certainly a point to consider. > > >What if you have a busy calculation going on in the background? > > What if you do? ?Are you suggesting this would somehow prevent I/O from > being serviced? ?I'm not sure why, as long as the implementation pays > attention to I/O events. Using native theads with blocking allows one to run a background calculation without burdening it to yield often enough to provide sufficient response times for other operations (which may or may not be convenient to do). Even if it's possible to accomplish arbitrary background processing without native threading (and it is not, because the background calculations could be performed by a library you don't control whose author didn't bother yielding at any point), you cannot reasonably claim native threads have no advantage in this case. Carl Banks From ethan at stoneleaf.us Wed Jul 22 18:35:01 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 22 Jul 2009 15:35:01 -0700 Subject: Changing the private variables content In-Reply-To: <4A671BAF.1060204@gmail.com> References: <4A671BAF.1060204@gmail.com> Message-ID: <4A679415.90407@stoneleaf.us> Ryniek90 wrote: >> >> Got it: >> >> exec('self.' + attr + '=\'' + val + '\'') >> >> That worked. I think it'll do what you want now ;) >> >> Ching-Yun "Xavier" Ho, Technical Artist >> >> Contact Information >> Mobile: (+61) 04 3335 4748 >> Skype ID: SpaXe85 >> Email: contact at xavierho.com >> Website: http://xavierho.com/ > > > To bad, that didn't worked in my class. Still the same error: > " > >>> mod.print_module('socket') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 51, in print_module > module_open = open(self._this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > :-/ > >> What is the point of the _SetVar method? >> >> Instead of: >> >> self._SetVar(self._this_module, os.path.join(root, f)) >> >> just do: >> >> self._this_module = os.path.join(root, f) >> >> (unless I'm misunderstanding what you're trying to do!) >> > > Of course i;ve tried, but still get the same error: > > " > >>> mod.print_module('socket') > > Traceback (most recent call last): > File "", line 1, in > mod.print_module('socket') > File "", line 51, in print_module > module_open = open(self.this_module, 'rb') > IOError: [Errno 2] No such file or directory: '' > >>> > " > > It looks like private variable have specific naure, that prevent from > traditional editing them. > Still searching for some tuts about private methods and variables. No. There is nothing special about variables with a leading underscore. _number is treated by Python *exactly* the same as number . The specific problem in your code above is your _SetVar function, among the more general problem of not yet having a good understanding of classes in Python. Keep studying, Python is an awesome language. I was able to make this work -- hope it helps. 8<----------------------------------------------------------------------- import os import sys class ModPrint(object): u"""This will be the doc.""" def __init__(self): self._default_search_path = sys.exec_prefix def _search_for_module(self, chosen_module, search_path): for root, dirs, files in os.walk(search_path): for f in files: if f == ("%s.py" % chosen_module): return os.path.join(root, f) def print_module(self, chosen_module, user_search_path=''): search_path = user_search_path or self._default_search_path module = self._search_for_module(chosen_module, search_path) if module is not None: module_open = open(module, 'rb') module_text = module_open.read() module_open.close() return module_text return 'Module not found...' 8<----------------------------------------------------------------------- From pinkeen at gmail.com Wed Jul 22 18:43:08 2009 From: pinkeen at gmail.com (Filip) Date: Wed, 22 Jul 2009 15:43:08 -0700 (PDT) Subject: python fast HTML data extraction library Message-ID: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> Hello, Sometime ago I was searching for a library that would simplify mass data scraping/extraction from webpages. Python XPath implementation seemed like the way to go. The problem was that most of the HTML on the net doesn't conform to XML standards, even the XHTML (those advertised as valid XHTML too) pages. I tried to fix that with BeautifulSoup + regexp filtering of some particular cases I encountered. That was slow and after running my data scraper for some time a lot of new problems (exceptions from xpath parser) were showing up. Not to mention that BeautifulSoup stripped almost all of the content from some heavily broken pages (50+KiB page stripped down to some few hundred bytes). Character encoding conversion was a hell too - even UTF-8 pages had some non- standard characters causing issues. Cutting to the chase - that's when I decided to take the matter into my own hands. I hacked together a solution sporting completely new approach overnight. It's called htxpath - a small, lightweight (also without dependencies) python library which lets you to extract specific tag(s) from a HTML document using a path string which has very similar syntax to xpath (but is more convenient in some cases). It did a very good job for me. My library, rather than parsing the whole input into a tree, processes it like a char stream with regular expressions. I decided to share it with everyone so there it is: http://code.google.com/p/htxpath/ I am aware that it is not beautifully coded as my experience with python is rather brief, but I am curious if it will be useful to anyone (also it's my first potentially [real-world ;)] useful project gone public). In that case I promise to continue developing it. It's probably full of bugs, but I can't catch them all by myself. regards, Filip Sobalski From ben+python at benfinney.id.au Wed Jul 22 19:26:37 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 23 Jul 2009 09:26:37 +1000 Subject: Documentation Problems References: Message-ID: <87ocrc7102.fsf@benfinney.id.au> Inky 788 writes: > Could you please provide brief instructions on exactly how to go about > creating a suitable patch file? I mean, starting from the beginning > (as in, "check out code from {here} using {this command}", "cd to > {this} directory", "edit the file in place", "run {this} command to > create a patch file"). Or perhaps this procedure is already outlined > elsewhere? Yes. The documentation is built from source documents under version control, like the rest of Python. So the procedure for creating and submitting patches is the same, and is covered by the Developer's FAQ . -- \ ?If you ever fall off the Sears Tower, just go real limp, | `\ because maybe you'll look like a dummy and people will try to | _o__) catch you because, hey, free dummy.? ?Jack Handey | Ben Finney From piet at cs.uu.nl Wed Jul 22 19:34:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 01:34:34 +0200 Subject: Changing the private variables content References: Message-ID: >>>>> Ryniek90 (R) wrote: >R> It looks like private variable have specific naure, that prevent from >R> traditional editing them. >R> Still searching for some tuts about private methods and variables. Why do you stubbornly keep saying that, while you have been told that it is not the case. Instead you would do better debugging your own case instead of making false claims. Sprinkle a few print statements in your code to see what is happening. Here are a few thing for you to look at: #using private method '_search_for_module' with 'self.' prefix #to search for prefered module self._search_for_module(_chosen_module, _user_search_path) You call it with 2 parameters: _chosen_module, _user_search_path Look at the definition: def _search_for_module(self, *args): It has *args to pick up the arguments, but they are not used in the body. Instead you use self._user_search_path and self._chosen_module. The latter one is always '', never changed to anything else. So it is not surprising that the module is not found. In print_module you have the same kind of confusion. Do you think that magically self._chosen_module gets the value of _chosen_module? Is that you idea of `private variables'? Or do you think that *args does magical things? """Private method which walks through default Python installation directories, and search for prefered module.""" #walking thru all directories available in path _user_search_path' for root, dirs, files in os.walk(self._user_search_path): for f in files: #if found file 'f' is THAT module, #full path to THAT file is assigned to variable print "Trying %s" % f if f == ("%s.py" % self._chosen_module): self._this_module = os.path.join(root, f) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Wed Jul 22 19:36:38 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 01:36:38 +0200 Subject: Python-list Digest, Vol 70, Issue 282 References: Message-ID: >>>>> Ryniek90 (R) wrote: >R> " >R> Didn't worked, so i wil reuse your class, and modify it later. Now i see >R> that working on private objects complex. No it's not complex. *You* make it complex. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From Brian.Mingus at colorado.edu Wed Jul 22 19:51:52 2009 From: Brian.Mingus at colorado.edu (Brian) Date: Wed, 22 Jul 2009 17:51:52 -0600 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <4a675296$0$1549$426a74cc@news.free.fr> References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> <10ef7849-a1ea-4770-b8c8-6f7bd2594579@g23g2000vbr.googlegroups.com> <4a67421e$0$31038$426a74cc@news.free.fr> <4a675296$0$1549$426a74cc@news.free.fr> Message-ID: <9839a05c0907221651o6afaa80xc25f37ff9bcbeea5@mail.gmail.com> On Wed, Jul 22, 2009 at 11:55 AM, William Dode wrote: > On 22-07-2009, William Dode wrote: > > > c 1.65s > > gcj 1.9s > > java 2.4s > > python2.5 + psyco 2.9s > > shedskin 3.4s > > with -bw i have 2.6s > > > unladen-2009Q2 125s (2m05) > > Jython 2.2.1 on java1.6.0_12 176s (without array, like shedskin) > > Jython 2.2.1 on java1.6.0_12 334s (with array) > > python2.5 215s (3m35s) > > python3.1 246s (4m06s) > > ironpython1.1.1 512 (8m32s) > > somebody can test with ironpython on windows ? > > Anyway, it's very impressive. I wonder if unladen will be so close in > the futur. > > -- > William Dod? - http://flibuste.net > Informaticien Ind?pendant > I had time to run a few tests. Since psyco doesn't work on 64 bit and the developer now works on pypy I used that. Intel Core i7 920 @ 2.66GHz Kubuntu Jaunty Jackal c gcc 4.3.3 0.77s gcj 4.3.3 0.81s java 1.6 0.99s shedskin 1.63s jython 2.2.1 85.37s cpython 2.6.2 93.26s pypy 1.1.0 1612.15s -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Wed Jul 22 19:52:06 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 20:52:06 -0300 Subject: Detect target name in descriptor __set__ method References: <3cb5f960-16f7-4fc6-841f-d0706f0e1d0a@s31g2000yqs.googlegroups.com> Message-ID: En Wed, 22 Jul 2009 05:31:13 -0300, Jon Clements escribi?: > On 22 July, 06:02, "Gabriel Genellina" wrote: >> I have a class attribute 'foo' which is a data descriptor. I create an ? >> instance of such class. When I say instance.foo = value, the descriptor >> ? >> __set__ method is called. Is there any way to obtain the name being ? >> assigned to? ('foo' in this example). That is, I want to know the >> target ? >> name for the assignment that triggered the __set__ method call. >>>> class Test: > def __setattr__(self, what, value): > print what, value > > >>>> t = Test() >>>> t.foo = 'x' > foo x > > Is that what you're after? Yes and no. __setattr__ has a terrible overhead as it is invoked for each and every attribute being set, special or not. Using a custom descriptor seems better, because it allows to intercept just the desired attribute. If only I had access to the name being used... -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Wed Jul 22 20:05:55 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 21:05:55 -0300 Subject: Detect target name in descriptor __set__ method References: Message-ID: En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James escribi?: > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina > wrote: > > >> class X(object): >> foo = descriptor() >> >> x = X() >> x.foo = "value" > > Isn't this going to create a brand new instance attribute x.foo that has > nothing to do with the descriptor anyway? No, it's up to the descriptor __set__ method what happens in this case. Think of the standard 'property' descriptor, the fset function can do whatever it wants. Also, a data descriptor takes precedence over any instance attribute of the same name that might exist. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Wed Jul 22 20:10:54 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 22 Jul 2009 21:10:54 -0300 Subject: invoke method on many instances References: <02701d35$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Wed, 22 Jul 2009 10:14:12 -0300, Alan G Isaac escribi?: >>>> On Fri, 17 Jul 2009 05:19:50 +0000, Alan G Isaac wrote: >>>>> def apply2(itr, methodname, *args, **kwargs): >>>>> f = operator.methodcaller(methodname, *args, **kwargs) >>>>> for item in itr: >>>>> f(item) > > >>> On 7/17/2009 3:45 AM Steven D'Aprano apparently wrote: >>>> for obj in objects: >>>> getattr(obj, methodname)(*args, **kwargs) > > >> En Sat, 18 Jul 2009 12:31:46 -0300, Alan G Isaac: >>> Are there any obvious considerations in choosing >>> between those two? > > > On 7/20/2009 3:29 AM Gabriel Genellina apparently wrote: >> The operator.methodcaller version is faster in my tests for large >> collections, but slightly slower when you have very few elements. > > > So it seems. Is this easily explained? NLMPI -- Gabriel Genellina From martin.hellwig at dcuktec.org Wed Jul 22 20:22:14 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 23 Jul 2009 01:22:14 +0100 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: References: Message-ID: Krishnakant wrote: I've seen a method before in a MS cmd script (MakeMeAdmin.cmd) for the purpose of temporarily elevating your rights but remaining the same user. There was a need trick in that the script checks itself on what credentials it runs, if it is not the appropriate one it will call itself again with runas (about the same as sudo) and exits. If it has the right credentials it will perform the actual task. That might be a way to solve your problem. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From http Wed Jul 22 20:27:23 2009 From: http (Paul Rubin) Date: 22 Jul 2009 17:27:23 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> Message-ID: <7xws60p7kk.fsf@ruckus.brouhaha.com> Carl Banks writes: > I don't think your fantasy async-only all-green-thread langauge > implementation is possible anyway. Erlang and GHC both work like that, quite successfully: http://shootout.alioth.debian.org/gp4/benchmark.php?test=threadring&lang=all > How would you wait on a pipe in one thread, a socket in another, a > semaphore in a third? You can select on pipes and sockets, I think. Not sure about semaphores. > (Are there any popular OSes that offer a unified polling interface to > all possible synchronizations?) And what do you do about drivers or > libraries that make underlying blocking calls? What if you have a > busy calculation going on in the background? I don't think the concept of "drivers" applies to user-mode programs. For FFI calls you would use an OS thread. The language runtime switches between busy computation threads on a timer tick. From wells at submute.net Wed Jul 22 20:40:11 2009 From: wells at submute.net (Wells Oliver) Date: Wed, 22 Jul 2009 19:40:11 -0500 Subject: RPY2 examples? Message-ID: <3f1a902d0907221740i62487600xc2210ca9fede5791@mail.gmail.com> I am trying to find examples using RPY2 to render R graphs to PNG/PDF/etc. The only things I can find use rpy 1.x. Any references? Thanks! -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwmsmith at gmail.com Wed Jul 22 21:25:17 2009 From: kwmsmith at gmail.com (Kurt Smith) Date: Wed, 22 Jul 2009 21:25:17 -0400 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> Message-ID: On Wed, Jul 22, 2009 at 2:48 AM, Bearophile wrote: > greg: >> Posting benchmark times for Pyrex or Cython is pretty >> meaningless without showing the exact code that was >> used, since times can vary enormously depending on >> how much you C-ify things. > > Was this link, shown by William, not enough? > http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 I took a stab at converting the recent psyco-optimized code to cython, and got a speedup. gcj 4.3.3 1.39s gcc 4.3.3 1.55s cython 11.2 1.91s psyco 1.94s javac 1.5.0_19 2.00s python 2.5.4 168.37s It was just a matter of cdef-ing all the arrays & integers -- bearophile already did the hard work :-) Here's the cython code; all the others are from the repo. ################################################# DEF NMOVES = 8 DEF SIDE = 5 DEF SQR_SIDE = SIDE * SIDE cdef int circuit[SQR_SIDE] cdef int nsolutions = 0 cdef int movex[NMOVES] cdef int movey[NMOVES] py_movex = [-1,-2,-2,-1,+1,+2,+2,+1] py_movey = [-2,-1,+1,+2,+2,+1,-1,-2] for i in range(NMOVES): movex[i] = py_movex[i] movey[i] = py_movey[i] shift = [x * SIDE + y for x,y in zip(py_movex, py_movey)] cdef int shift_0 = shift[0] cdef int shift_1 = shift[1] cdef int shift_2 = shift[2] cdef int shift_3 = shift[3] cdef int shift_4 = shift[4] cdef int shift_5 = shift[5] cdef int shift_6 = shift[6] cdef int shift_7 = shift[7] def showCircuit(): print for x in xrange(SIDE): x_SIDE = x * SIDE for y in xrange(SIDE): if SQR_SIDE < 100: print "%02d " % circuit[x_SIDE + y], else: print "%03d " % circuit[x_SIDE + y], print cdef void solve(int nb, int x, int y, int SIDE=SIDE, int SQR_SIDE=SQR_SIDE, int *circuit=circuit, int shift_0=shift_0, int shift_1=shift_1, int shift_2=shift_2, int shift_3=shift_3, int shift_4=shift_4, int shift_5=shift_5, int shift_6=shift_6, int shift_7=shift_7, ): global nsolutions cdef int newx, newy cdef int pos = x * SIDE + y circuit[pos] = nb if nb == SQR_SIDE: #showCircuit() nsolutions += 1 circuit[pos] = 0 return newx = x + -1 if newx >= 0 and newx < SIDE: newy = y + -2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_0]: solve(nb+1, newx, newy) newx = x + -2 if newx >= 0 and newx < SIDE: newy = y + -1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_1]: solve(nb+1, newx, newy) newx = x + -2 if newx >= 0 and newx < SIDE: newy = y + 1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_2]: solve(nb+1, newx, newy) newx = x + -1 if newx >= 0 and newx < SIDE: newy = y + 2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_3]: solve(nb+1, newx, newy) newx = x + 1 if newx >= 0 and newx < SIDE: newy = y + 2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_4]: solve(nb+1, newx, newy) newx = x + 2 if newx >= 0 and newx < SIDE: newy = y + 1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_5]: solve(nb+1, newx, newy) newx = x + 2 if newx >= 0 and newx < SIDE: newy = y + -1 if newy >= 0 and newy < SIDE and not circuit[pos + shift_6]: solve(nb+1, newx, newy) newx = x + 1 if newx >= 0 and newx < SIDE: newy = y + -2 if newy >= 0 and newy < SIDE and not circuit[pos + shift_7]: solve(nb+1, newx, newy) circuit[pos] = 0 def main(): print "Search for side=%d" % SIDE cdef int x,y for x in range(SIDE): for y in range(SIDE): solve(1, x, y); print "\n%dx%d case, %d solutions." % (SIDE, SIDE, nsolutions) def run(): import time s=time.time() main() print time.time()-s ################################################# From scriptlearner at gmail.com Wed Jul 22 21:45:45 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Wed, 22 Jul 2009 18:45:45 -0700 (PDT) Subject: regex: multiple matching for one string Message-ID: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I will like to take out the values (valuea, valueb, and valuec). How do I do that in Python? The group method will only return the matched part. Thanks. p = re.compile('#a=*;b=*;c=*;') m = p.match(line) if m: print m.group(), From ptmcg at austin.rr.com Wed Jul 22 21:53:33 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Wed, 22 Jul 2009 18:53:33 -0700 (PDT) Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> Message-ID: <88e8ea19-7106-406a-a531-88c2b6893987@y7g2000yqa.googlegroups.com> On Jul 22, 5:43?pm, Filip wrote: > > My library, rather than parsing the whole input into a tree, processes > it like a char stream with regular expressions. > Filip - In general, parsing HTML with re's is fraught with easily-overlooked deviations from the norm. But since you have stepped up to the task, here are some comments on your re's: # You should use raw string literals throughout, as in: # blah_re = re.compile(r'sljdflsflds') # (note the leading r before the string literal). raw string literals # really help keep your re expressions clean, so that you don't ever # have to double up any '\' characters. # Attributes might be enclosed in single quotes, or not enclosed in any quotes at all. attr_re = re.compile('([\da-z]+?)\s*=\s*\"(.*?)\"', re.DOTALL | re.UNICODE | re.IGNORECASE) # Needs re.IGNORECASE, and can have tag attributes, such as
line_break_re = re.compile('', re.UNICODE) # what about HTML entities defined using hex syntax, such as &#xxxx; amp_re = re.compile('\&(?![a-z]+?\;)', re.UNICODE | re.IGNORECASE) How would you extract data from a table? For instance, how would you extract the data entries from the table at this URL: http://tf.nist.gov/tf-cgi/servers.cgi ? This would be a good example snippet for your module documentation. Try extracting all of the sldjlsfjd links from yahoo.com, and see how much of what you expect actually gets matched. Good luck! -- Paul From rurpy at yahoo.com Wed Jul 22 22:00:42 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Wed, 22 Jul 2009 19:00:42 -0700 (PDT) Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: On Jul 22, 7:45 pm, "scriptlear... at gmail.com" wrote: > For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I > will like to take out the values (valuea, valueb, and valuec). How do > I do that in Python? The group method will only return the matched > part. Thanks. > > p = re.compile('#a=*;b=*;c=*;') > m = p.match(line) > if m: > print m.group(), p = re.compile('#a=([^;]*);b=([^;]*);c=([^;]*);') m = p.match(line) if m: print m.group(1),m.group(2),m.group(3), Note that "=*;" in your regex will match zero or more "=" characters -- probably not what you intended. "[^;]* will match any string up to the next ";" character which will be a value (assuming you don't have or care about embedded whitespace.) You might also want to consider using a r'...' string for the regex, which will make including backslash characters easier if you need them at some future time. From wuwei23 at gmail.com Wed Jul 22 22:04:34 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 22 Jul 2009 19:04:34 -0700 (PDT) Subject: Ideas for problem with chat server application! References: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> Message-ID: <40c22282-7526-4064-8885-62a87107bb3d@p10g2000prm.googlegroups.com> On Jul 22, 6:27?pm, "David Adamo Jr." wrote: > Suggestions are highly appreciated. I haven't used it but I've heard good things about FireDaemon, which can run any script or program as a Windows service. Apparently the Lite version is free: http://www.download-freeware-shareware.com/Freeware-System-Utilities.php?Type=14387 Hope this helps. From cindy at speed-voip.com Wed Jul 22 22:36:31 2009 From: cindy at speed-voip.com (cindy) Date: Wed, 22 Jul 2009 22:36:31 -0400 Subject: [Unblocking VoIP in UAE] for freedom! Message-ID: Please view this newsletter online at: http://www.mynewsletterbuilder.com/tools/view_newsletter.php?newsletter_id=1409985533 SpeedVoIP Communication Technology Co., LTD. - 600, 6th Avenue S.W. - Calgary - Alberta - T2P 0S5 Subscribe to this newsletter: https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=s&newsletter_id=1409985533 Unsubscribe python-list at python.org: https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=u&email=python-list at python.org Change your preferences: https://www.mynewsletterbuilder.com/tools/subscription.php?username=svsales&send_id=712043215&l=p&email=python-list at python.org Forward to a friend: http://www.mynewsletterbuilder.com/tools/forward.php?username=svsales&newsletter_id=1409985533&email=python-list at python.org&send_id=712043215 Report this email as spam: https://www.mynewsletterbuilder.com/tools/abuse_report.php?username=svsales&send_id=712043215&email=python-list at python.org This email was sent using MyNewsletterBuilder.com. -------------- next part -------------- An HTML attachment was scrubbed... URL: From songofacandy at gmail.com Wed Jul 22 23:06:56 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Wed, 22 Jul 2009 20:06:56 -0700 (PDT) Subject: What is file.encoding convention? Message-ID: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> In document : >> The encoding that this file uses. When Unicode strings are written to a file, >> they will be converted to byte strings using this encoding. In addition, >> when the file is connected to a terminal, the attribute gives the encoding >> that the terminal is likely to use But in logging.StreamHandler.emit() :: try: if (isinstance(msg, unicode) and getattr(stream, 'encoding', None)): #fs = fs.decode(stream.encoding) try: stream.write(fs % msg) except UnicodeEncodeError: #Printing to terminals sometimes fails. For example, #with an encoding of 'cp1251', the above write will #work if written to a stream opened or wrapped by #the codecs module, but fail when writing to a #terminal even when the codepage is set to cp1251. #An extra encoding step seems to be needed. stream.write((fs % msg).encode (stream.encoding)) else: stream.write(fs % msg) except UnicodeError: stream.write(fs % msg.encode("UTF-8")) And behavior of sys.stdout in Windows:: >>> import sys >>> sys.stdout.encoding 'cp932' >>> u = u"???" >>> u u'\u3042\u3044\u3046' >>> print >>sys.stdout, u ??? >>> sys.stderr.write(u) Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) What is file.encoding convention? If I want to write a unicode string to a file(-like) that have encoding attribute, I should do (1) try: file.write(unicode_str), (2) except UnicodeEncodeError: file.write(unicode_str.encode (file.encoding)) like logging? It seems agly. From sjmachin at lexicon.net Wed Jul 22 23:24:47 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 22 Jul 2009 20:24:47 -0700 (PDT) Subject: Not understanding this documentation References: <25704c70-b9c6-4435-9ff0-9659bd340847@x25g2000prf.googlegroups.com> <081d1a50-fd7f-4f87-889e-262bc7e2debd@x5g2000prf.googlegroups.com> Message-ID: On Jul 23, 2:45?am, "F.O.P." wrote: > On Jul 22, 9:07?am, "Rhodri James" > wrote: > > > > > > > > > On Wed, 22 Jul 2009 15:46:09 +0100, F.O.P. ? > > wrote: > > > >http://bugs.python.org/issue5358 > > > I simply installed 3.0 on my ubuntubox brought my project from a > > > windows machine and made necessary changes to what I had used from os > > > and os.path. ?I have an error at the end of this line: > > > for k in range(I): > > > and the error: > > > SyntaxError: invalid character in identifier > > > > I've already declared I way above as the lenth of an array (I = len > > > (IA)) > > > There shouldn't be a problem there aren't any issues with other > > > declarations identical to this. > > > Could you show us slightly more of your code and the rest of the > > error message, please? ?I have a suspicion that the error actually > > lies in the "necessary changes" you made. > > > -- > > Rhodri James *-* Wildebeest Herder to the Masses > > I spoke to a friend about it and he found that I had put in a unicode > "semicolon" rather than ASCII. This is rather puzzling. What are you describing as a "unicode semicolon"? Possibilities include: (a) U+003B SEMICOLON (which is exactly what you need) (b) U+037E GREEK QUESTION MARK (which looks like (a)) (c) U+061B ARABIC SEMICOLON (which looks like (a) rotated 180 degrees) How did you get it into your source file? What was in your # coding: ????? or similar line at the top of your source file? From aahz at pythoncraft.com Wed Jul 22 23:38:30 2009 From: aahz at pythoncraft.com (Aahz) Date: 22 Jul 2009 20:38:30 -0700 Subject: comments? storing a function in an object References: Message-ID: In article , Carl Banks wrote: > >You have to be REALLY REALLY careful not to pass any user-supplied >data to it if this is a server running on your computer, of course. Unless, of course, your users are paying for this service. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From aahz at pythoncraft.com Wed Jul 22 23:54:11 2009 From: aahz at pythoncraft.com (Aahz) Date: 22 Jul 2009 20:54:11 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: Message-ID: In article , D'Arcy J.M. Cain wrote: >On 20 Jul 2009 17:10:55 -0700 >aahz at pythoncraft.com (Aahz) wrote: >>>I understand what you want but I can't see how a toolkit can do that. >>>How do you program "graceful?" It seems pretty application specific. >> >> Presumably the JS toolkit generates both code and HTML. Actions that >> normally get executed by JS should degrade to plain HTML links and form >> submits (or possibly not display at all if it's a pure convenience). > >As I said, I do understand what you are after but I still think it is a >judgement call. What defines "convenience?" How does the library know >what is essential and what is pretty? Presumably a JS toolkit designed for this purpose would have an API to specify the handling of such issues. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From aahz at pythoncraft.com Wed Jul 22 23:55:20 2009 From: aahz at pythoncraft.com (Aahz) Date: 22 Jul 2009 20:55:20 -0700 Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: <1c994086-8c58-488f-b3b3-6161c4b2ba05@k30g2000yqf.googlegroups.com> Message-ID: In article <1c994086-8c58-488f-b3b3-6161c4b2ba05 at k30g2000yqf.googlegroups.com>, Paul Boddie wrote: >On 20 Jul, 18:00, a... at pythoncraft.com (Aahz) wrote: >> >> Out of curiosity, are there any JavaScript toolkits that generate code >> that degrades gracefully when JavaScript is disabled? > >You mean "Web toolkits which use JavaScript", I presume. I have >written (and use myself) a toolkit/framework called XSLForms (part of >the XSLTools distribution) which supports in-page updates (AJAX, >effectively) that degrade to vanilla whole-page requests if JavaScript >is switched off: > >http://www.boddie.org.uk/python/XSLTools.html Thanks! I'll take a look after OSCON. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "The volume of a pizza of thickness 'a' and radius 'z' is given by pi*z*z*a" From gagsl-py2 at yahoo.com.ar Thu Jul 23 00:27:12 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 23 Jul 2009 01:27:12 -0300 Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: En Wed, 22 Jul 2009 11:36:51 -0300, Inky 788 escribi?: > On Jul 22, 2:36?am, Hendrik van Rooyen > wrote: >> The good reason is the immutability, which lets you use >> a tuple as a dict key. ? > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. Many people posted useful examples of tuples as dictionary keys in this thread. Just to add another one (emulate SQL GROUP BY): ope_by_dept = defaultdict(int) total_times = defaultdict(float) for dept_name, ope_name, ope_date, engineer in list_of_operations: ope_by_dept[dept_name, ope_start.month] += 1 total_times[dept_name, engineer] += ope_end - ope_start print "Operations per department per month" for dept_name, month in sorted(ope_by_dept): print dept_name, month, ope_by_dept[dept_name, month] -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Thu Jul 23 00:27:15 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 23 Jul 2009 01:27:15 -0300 Subject: logging outgoing HTTP POST message and incoming response message References: <7cpcjaF2915u5U1@mid.uni-berlin.de> <0b403bd0-759f-49b6-9eac-3d585d877fe3@k13g2000prh.googlegroups.com> Message-ID: En Wed, 22 Jul 2009 18:18:37 -0300, scriptlearner at gmail.com escribi?: > On Jul 22, 1:54?pm, "Diez B. Roggisch" wrote: >> You can use proxy-tools such as tcpmon or sniff traffic using wireshark. > > Thanks, > but I am trying to enable some debug mode to log all outgoing and > incoming messages for certain period of time, and running another > proxy-tool is not very ideal. It would be great to log it in some log > file. You may install a custom HTTPHandler: class LoggingHTTPConnection(httplib.HTTPConnection): def request(self, method, url, body=None, headers={}): print method, url, headers httplib.HTTPConnection.request(self, method, url, body, headers) def getresponse(self): response = httplib.HTTPConnection.getresponse(self) print response.status, response.msg return response class LoggingHTTPHandler(urllib2.HTTPHandler): def http_open(self, req): return self.do_open(LoggingHTTPConnection, req) opener = urllib2.build_opener(LoggingHTTPHandler, ...) -- Gabriel Genellina From nagle at animats.com Thu Jul 23 00:40:38 2009 From: nagle at animats.com (John Nagle) Date: Wed, 22 Jul 2009 21:40:38 -0700 Subject: MySQLdb for Python 3.1 getting close? Message-ID: <4a67e912$0$1603$742ec2ed@news.sonic.net> Is MySQLdb available for Python 3.1 yet? http://sourceforge.net/projects/mysql-python/ says the last supported Python version is 2.5. Any progress in sight? John Nagle From martin.hellwig at dcuktec.org Thu Jul 23 02:16:22 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 23 Jul 2009 07:16:22 +0100 Subject: Ideas for problem with chat server application! In-Reply-To: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> References: <02ec3d2a-fcbd-49f6-8fae-32725cc4c0cc@k6g2000yqn.googlegroups.com> Message-ID: David Adamo Jr. wrote: > My > attempt was to create a windows service that start automatically and > runs this batch file using a Network Service account on the server > system. Although, I'm having a hard time with this (temporarily), I > would love to ask if there are any alternatives to using a windows > service. Suggestions are highly appreciated. This is definitely the right way to go, perhaps reviewing a template which I use for windows services may help you: http://blog.dcuktec.com/2009/07/python-windows-service-template.html -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From jayshree06comp at gmail.com Thu Jul 23 02:37:17 2009 From: jayshree06comp at gmail.com (jayshree) Date: Wed, 22 Jul 2009 23:37:17 -0700 (PDT) Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: On Jul 21, 8:59?pm, Piet van Oostrum wrote: > >>>>> jayshree (j) wrote: > >j> M2Crypto package not showing the 'recipient_public_key.pem' file at > >j> linux terminal .how do i get/connect with recipient public key. > >j> exactly i need to check how can i open this file through linux > >j> commands. > >j> import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open > >j> ('recipient_public_key.pem','rb').read()) print recip; plaintext = > >j> whatever i need to encrypt msg = recip.public_encrypt > >j> (plaintext,RSA.pkcs1_padding) print msg; > >j> after calling the function its not giving any output and even any > >j> error > >j> i also tried as 'Will' said > >j> pk = open('public_key.pem','rb').read() print pk; rsa = > >j> M2Crypto.RSA.load_pub_key(pk) > >j> whats the mistake i am not getting .will somebody help me out. > >j> is something wrong in opening 'recipient_public_key.pem'. is M2Crypto > >j> contain this file inbuilt .from where this file taking public key of > >j> the recipient i'. what this file contain and how it will work .should > >j> i need to create such a file for my purpose.if it is then how can i > >j> create this ?and how it will retrieve the key,where i recognize my > >j> recipient to get his public key .is something like database at server. > >j> please give me a quick response...... > >j> looking for your answer. > > Please: > 1. Type your python code with newlines and proper indentation. > 2. Show the error messages that your code gives when you run it. > 3. Use proper capital letters at the beginning of your sentences. > 4. Don't fire so many questions in rapid succession. > > The recipient_public_key.pem file is the public key of the recipient > which means the person that is going to receive the encrypted message. > You should get it from the recipient him/herself or from some key store > where s/he has deposited it. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org- Hide quoted text - > > - Show quoted text - Here is the code that i used import M2Crypto from M2Crypto import RSA def encrypt(): recip = M2Crypto.RSA.load_pub_key(open ('recipient_public_key.pem','rb').read()) #recip = M2Crypto.RSA.load_pub_key('recipient_public_key.pem') print recip; plaintext =" whatever i need to encrypt" msg = recip.public_encrypt(plaintext,RSA.pkcs1_padding) print msg; encrypt() error coming like - IOError: [Errno 2] No such file or directory: 'recipient_public_key.pem' Is this not the inbuilt file. How should i create such type of file. From wilk at flibuste.net Thu Jul 23 02:44:56 2009 From: wilk at flibuste.net (William Dode) Date: 23 Jul 2009 06:44:56 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> Message-ID: <4a6806e8$0$22900$426a74cc@news.free.fr> On 23-07-2009, Kurt Smith wrote: > On Wed, Jul 22, 2009 at 2:48 AM, Bearophile wrote: >> greg: >>> Posting benchmark times for Pyrex or Cython is pretty >>> meaningless without showing the exact code that was >>> used, since times can vary enormously depending on >>> how much you C-ify things. >> >> Was this link, shown by William, not enough? >> http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 > > I took a stab at converting the recent psyco-optimized code to cython, > and got a speedup. thanks, i updated my repo with litle more tips. -- William Dod? - http://flibuste.net Informaticien Ind?pendant From amrita at iisermohali.ac.in Thu Jul 23 03:22:15 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Thu, 23 Jul 2009 12:52:15 +0530 (IST) Subject: how two join and arrange two files together Message-ID: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> Hi, I have two large files: FileA 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 23 ALA H = 8.78 N = CA = HA = C = 179.93................. and FileB 21 ALA helix (helix_alpha, helix2) 23 ALA helix (helix_alpha, helix3) 38 ALA helix (helix_alpha, helix3)........... now what i want that i will make another file in which i will join the two file in such a way that only matching entries will come like here 21 and 23 ALA is in both files, so the output will be something like:- 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA helix (helix_alpha, helix2) 23 ALA H = 8.78 N = CA = HA = C = 179.93|23 ALA helix (helix_alpha, helix3) and further i will make another file in which i will be able to put those lines form this file based on the missing atom value, like for 21 ALA HA is not defined so i will put it another file based on its HA missing value similarly i will put 23 ALA on another file based on its missing N,CA and HA value. I tried to join the two file based on their matching entries by:--- >>>from collections import defaultdict >>> >>> if __name__ == "__main__": ... a = open("/home/amrita/alachems/chem100.txt") ... c = open("/home/amrita/secstr/secstr100.txt") ... >>> def source(stream): ... return (line.strip() for line in stream) ... ... >>> def merge(sources): ... for m in merge([source(a),source(c)]): ... print "|".join(c.ljust(10) for c in m) ... but it is not giving any value. Thanks, Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From hendrik at microcorp.co.za Thu Jul 23 03:25:22 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 23 Jul 2009 09:25:22 +0200 Subject: binary literal In-Reply-To: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <200907230925.22747.hendrik@microcorp.co.za> On Wednesday 22 July 2009 12:03:44 superpollo wrote: > can i do something like the above, but using a *binary* number? (e.g. > 00101101 instead of 45) ? 00101101 is not hex 45. hex 45 is 01000101 >>> chr(int('01000101',2)) 'E' >>> - Hendrik From hendrik at microcorp.co.za Thu Jul 23 03:42:27 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Thu, 23 Jul 2009 09:42:27 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> References: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: <200907230942.27558.hendrik@microcorp.co.za> On Wednesday 22 July 2009 16:36:51 Inky 788 wrote: > On Jul 22, 2:36?am, Hendrik van Rooyen > > wrote: > > The good reason is the immutability, which lets you use > > a tuple as a dict key. ? > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. Steven showed why you cannot have a mutable thing as a key in a dict. if you think it is contrived, then please consider how you would keep track of say the colour of a pixel on a screen at position (x,y) - this is about the simplest "natural" tuple format and example. There are other equally valid examples, as has been pointed out. (may have been in another thread - am a bit confused about that right now) - Hendrik From clp2 at rebertia.com Thu Jul 23 04:16:04 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 23 Jul 2009 01:16:04 -0700 Subject: how two join and arrange two files together In-Reply-To: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> References: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> Message-ID: <50697b2c0907230116m67a73918gbd344b9a5289b45@mail.gmail.com> On Thu, Jul 23, 2009 at 12:22 AM, wrote: > > Hi, > > I have two large files: > > FileA > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 > 23 ALA H = 8.78 N = ?CA = ?HA = C = 179.93................. > > and > > FileB > 21 ALA ?helix (helix_alpha, helix2) > 23 ALA ?helix (helix_alpha, helix3) > 38 ALA ?helix (helix_alpha, helix3)........... > > now what i want that i will make another file in which i will join the two > file in such a way that only matching entries will come like here 21 and > 23 ALA is in both files, so the output will be something like:- > > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA ?helix > (helix_alpha, helix2) > 23 ALA H = 8.78 N = ?CA = ?HA = C = 179.93|23 ALA ?helix (helix_alpha, > helix3) > > and further i will make another file in which i will be able to put those > lines form this file based on the missing atom value, like for 21 ALA HA > is not defined so i will put it another file based on its HA missing value > similarly i will put 23 ALA on another file based on its missing N,CA and > HA value. > > I tried to join the two file based on their matching entries by:--- >>>>from collections import defaultdict >>>> >>>> if __name__ == "__main__": > ... ? ? ?a = open("/home/amrita/alachems/chem100.txt") > ... ? ? ?c = open("/home/amrita/secstr/secstr100.txt") > ... >>>> def source(stream): > ... ? ? return (line.strip() for line in stream) > ... > ... >>>> def merge(sources): > ... ? ? for m in merge([source(a),source(c)]): > ... ? ? ? ? print "|".join(c.ljust(10) for c in m) > ... > > but it is not giving any value. You never actually called any of your functions. Slightly corrected version: from collections import defaultdict def source(stream): return (line.strip() for line in stream) def merge(sources): for m in sources: print "|".join(c.ljust(10) for c in m) if __name__ == "__main__": a = open("/home/amrita/alachems/chem100.txt") c = open("/home/amrita/secstr/secstr100.txt") merge([source(a), source(c)]) It's still not sophisticated enough to give the exact output you're looking for, but it is a step in the right direction. You really should try asking someone from your CS Dept to help you. It would seriously take a couple hours, at most. - Chris -- Still brandishing a cluestick a vain... http://blog.rebertia.com From pavlovevidence at gmail.com Thu Jul 23 04:47:03 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 01:47:03 -0700 (PDT) Subject: comments? storing a function in an object References: Message-ID: <20efdb6a-c1a5-47dc-8546-7c4ae548e22d@g1g2000pra.googlegroups.com> On Jul 22, 8:38?pm, a... at pythoncraft.com (Aahz) wrote: > In article , > Carl Banks ? wrote: > > >You have to be REALLY REALLY careful not to pass any user-supplied > >data to it if this is a server running on your computer, of course. > > Unless, of course, your users are paying for this service. Well, yes, but I assume that by the time you're deliberately letting users pay to run their programs on your server, you will already have deployed a full-blown, multi-tiered security strategy that includes validation by the server process. That was sort of beyond the scope of the OP's question. Carl Banks From helvinlui at gmail.com Thu Jul 23 04:55:51 2009 From: helvinlui at gmail.com (Helvin) Date: Thu, 23 Jul 2009 01:55:51 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> Message-ID: <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> I believe I now have vtkpython.exe. However, my 'import vtk' statement in my python code is not working. The error says something like "no module named vtk". Where do I find modules for vtk in pyqt? Do they exist? They must, right? Because there are people using vtk in pyqt? Or do I have to use OpenGL? Confused... Helvin From jjkk73 at gmail.com Thu Jul 23 05:01:45 2009 From: jjkk73 at gmail.com (jorma kala) Date: Thu, 23 Jul 2009 10:01:45 +0100 Subject: How to document Python code properly for Pydoc In-Reply-To: References: Message-ID: Thanks very much for your help On Wed, Jul 22, 2009 at 6:43 PM, Lie Ryan wrote: > jorma kala wrote: > > Hi, > > Do you know where I can find the rules for documenting Python code, so > > that automatic document generation with Pydoc makes the most of the > > comments inserted in the code? > > I know about documenting class and method through triple quote just > > under the class definition. But how do you comment a specific field or > > variable, or how do you document function arguments so that they are > > extracted like in javadoc? > > Thanks very much > > pydoc is a simple tool, and doesn't do much. You write in freeform, > although generally you'll do something like this: > > def myfunc(a, b): > ''' > short description of myfunc > > longer description of myfunc, if necessary, and typically includes > description of the arguments and the behaviors. Also includes the > description of the return value. > ''' > > pass > > pydoc doesn't recognize any special markups. If you want to get more > from the docstring, you need other documentation generator such as > epydoc, Doxygen, or Sphinx. > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From user at example.net Thu Jul 23 05:04:24 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 11:04:24 +0200 Subject: binary literal In-Reply-To: References: <4a66e400$0$553$4fafbaef@reader4.news.tin.it> Message-ID: <4a682799$0$28504$4fafbaef@reader2.news.tin.it> Hendrik van Rooyen wrote: > On Wednesday 22 July 2009 12:03:44 superpollo wrote: > > >>can i do something like the above, but using a *binary* number? (e.g. >>00101101 instead of 45) ? > > > 00101101 is not hex 45. > hex 45 is 01000101 > whoopsie daisie! > >>>>chr(int('01000101',2)) > > 'E' > much obliged. bye From ryszard.szopa at gmail.com Thu Jul 23 05:08:39 2009 From: ryszard.szopa at gmail.com (Ryszard Szopa) Date: Thu, 23 Jul 2009 02:08:39 -0700 (PDT) Subject: strange error when trying to log something Message-ID: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Hi, I've recently reinstalled Python 2.6 (from DMG) on my Mac, and I am running into very strage errors. Namely, logging seems to be badly broken. When I open the interpreter through Django's manage.py shell and try to use logging, I get the following error: >>> logging.critical('ala') ------------------------------------------------------------ Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1416, in critical root.critical(*((msg,)+args), **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1074, in critical self._log(CRITICAL, msg, args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1142, in _log record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 1117, in makeRecord rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/logging/__init__.py", line 272, in __init__ from multiprocessing import current_process File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/multiprocessing/__init__.py", line 64, in from multiprocessing.util import SUBDEBUG, SUBWARNING File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/multiprocessing/util.py", line 121, in _afterfork_registry = weakref.WeakValueDictionary() File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/weakref.py", line 51, in __init__ UserDict.UserDict.__init__(self, *args, **kw) TypeError: unbound method __init__() must be called with UserDict instance as first argument (got WeakValueDictionary instance instead) I was able to silence the error (and be able to work normally) by making UserDict.UserDict inherit from object. Any ideas what is causing the error? Before I updated Python everything was fine. Am I breaking a lot of things by making UserDict.UserDict a new style class? Thanks in advance for any insight. -- Ryszard Szopa From songofacandy at gmail.com Thu Jul 23 05:24:42 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Thu, 23 Jul 2009 02:24:42 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> Message-ID: <697fa41f-9c6a-438f-9c47-9566a79bdf82@p36g2000prn.googlegroups.com> > What is file.encoding convention? > If I want to write a unicode string to a file(-like) that have > encoding attribute, I should do > (1) try: file.write(unicode_str), > (2) except UnicodeEncodeError: file.write(unicode_str.encode > (file.encoding)) > like logging? > It seems agly. s/agly/ugly/ From __peter__ at web.de Thu Jul 23 05:29:13 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 11:29:13 +0200 Subject: strange error when trying to log something References: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Message-ID: Ryszard Szopa wrote: > Hi, > > I've recently reinstalled Python 2.6 (from DMG) on my Mac, and I am > running into very strage errors. Namely, logging seems to be badly > broken. When I open the interpreter through Django's manage.py shell > and try to use logging, I get the following error: > >>>> logging.critical('ala') > ------------------------------------------------------------ > Traceback (most recent call last): > File "", line 1, in > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1416, in critical > root.critical(*((msg,)+args), **kwargs) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1074, in critical > self._log(CRITICAL, msg, args, **kwargs) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1142, in _log > record = self.makeRecord(self.name, level, fn, lno, msg, args, > exc_info, func, extra) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 1117, in makeRecord > rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func) > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/logging/__init__.py", line 272, in __init__ > from multiprocessing import current_process > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/multiprocessing/__init__.py", line 64, in > from multiprocessing.util import SUBDEBUG, SUBWARNING > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/multiprocessing/util.py", line 121, in > _afterfork_registry = weakref.WeakValueDictionary() > File "/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/weakref.py", line 51, in __init__ > UserDict.UserDict.__init__(self, *args, **kw) > TypeError: unbound method __init__() must be called with UserDict > instance as first argument (got WeakValueDictionary instance instead) > > I was able to silence the error (and be able to work normally) by > making UserDict.UserDict inherit from object. > > Any ideas what is causing the error? Before I updated Python > everything was fine. Am I breaking a lot of things by making > UserDict.UserDict a new style class? > > Thanks in advance for any insight. > > -- Ryszard Szopa I have a hunch that you are triggering a reload() somewhere. Example: Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import weakref >>> weakref.WeakValueDictionary() >>> import UserDict >>> reload(UserDict) >>> weakref.WeakValueDictionary() Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/weakref.py", line 51, in __init__ UserDict.UserDict.__init__(self, *args, **kw) TypeError: unbound method __init__() must be called with UserDict instance as first argument (got WeakValueDictionary instance instead) Try restarting the interpreter after any change to source files. Peter From nobody at nowhere.com Thu Jul 23 05:37:55 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 23 Jul 2009 10:37:55 +0100 Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: On Wed, 22 Jul 2009 15:17:52 -0700, Carl Banks wrote: > So do all these OSes have some kind of __mega_unifying_poll system > call that works for anything that might possibly block, that you can > exploit from a user process? Threads ;) They also have the advantage that one thread can run while another is waiting on disk I/O, which isn't something which can be done with a select/poll interface (even if select/poll worked for files, it doesn't help for mapped files). From hackingkk at gmail.com Thu Jul 23 05:50:10 2009 From: hackingkk at gmail.com (Krishnakant) Date: Thu, 23 Jul 2009 15:20:10 +0530 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: References: Message-ID: <1248342610.3698.7.camel@krishna-laptop> On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote: > Being a sudoer is not a privilege to issue the os.setuid system call. It > is only a permission to use the sudo command. > Yes, So I would like to know if python can change the user to some other non-privileged user during the script execution? > >K> I tryed using subprocess but that did not help me either. I tryed sudo > >K> su into the Popen command but it throws me into the terminal (shell) > >K> with postgres as the user. > > You could execute the command: > sudo -u postgres required_command > with subprocess. > Ok, but the problem is much more complex. What if I want to do the following. 1, change the user for a particular script to the postgres user. 2. now execute the python code for connecting to the postgresql database. In the second point I actually want to execute python code not shell level command so will the sudo -u in the subprocess.Popen change the user in the script? In short I would just like to have the script run under another user let's say postgres as long as a certain action is going on, for example connecting to the postgresql database. > You have another problem then: your password must be supplied unless the > NOPASSWD flag is set in the sudoers file. > That is clear, the only problem is that I want the script to run as postgres user although it was started by the user kk. happy hacking. Krishnakant. From news123 at free.fr Thu Jul 23 05:56:45 2009 From: news123 at free.fr (News123) Date: Thu, 23 Jul 2009 11:56:45 +0200 Subject: available formats and params for Image.save() Message-ID: <4a6833dd$0$22599$426a34cc@news.free.fr> Hi, Somehow I have difficulties reading the documentation for PIL (Image) Is there an easy way to know which formats are supported and what their names are? Is there an easy way to know which parameters are supported by Image.save(). How can I list them where are they documented? Somehow I consider the documentation to be a little weak on this points. all the documentation says (at least where I looked) is: > class Image > | Methods defined here: > . . . > save(self, fp, format=None, **params) > | Save image to file or stream In order to find out how the format name for a .jpg file I did following: import Image img = Image.open("afile.jpg") print img.format Then I saw, that the result was 'JPEG' and not 'JPG' as I tried first I'm at a complete loss at finding out what parameters the save function accepts for saving a JPG file or a PNG file Is there an easy way of finding this in any doc or do I have to dive into the sources. If there's no easy way: Wouldn't a better documentation increase the 'user experience'? Thanks in advance for any pointers N From nobody at nowhere.com Thu Jul 23 06:03:58 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 23 Jul 2009 11:03:58 +0100 Subject: Multiple versions of python References: <0b1dafb3-6725-4418-9d4e-4c55d8c213e3@c29g2000yqd.googlegroups.com> Message-ID: On Tue, 21 Jul 2009 10:19:42 -0400, Dave Angel wrote: > The other thing you may want to do in a batch file is to change the file > associations so that you can run the .py file directly, without typing > "python" or "pythonw" in front of it. > > The relevant Windows commands are: assoc and ftype However: assoc and ftype modify the registry keys under HKLM\Software\Classes. This works fine if these are the only relevant keys, but if you also have settings under HKCU\Software\Classes, those will take precedence, so assoc and/or ftype won't have any effect. Also, you typically need at least "Power User" status in order to modify the keys under HKLM, while any user should be able to modify those under HKCU (HKCU is a "virtual" key, corresponding to HKU\). From user at example.net Thu Jul 23 06:23:13 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 12:23:13 +0200 Subject: raster (PIL) Message-ID: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> hi. i wrote a program which transforms a string of zeroes ando ones into a png file. #!/usr/bin/env python import Image import sys bits_in_a_byte = 8 raster_string = """\ 00000000000000000000000000000000 00100100111100100000100000111100 00100100100000100000100000100100 00111100111000100000100000100100 00100100100000100000100000100100 00100100111100111100111100111100 00000000000000000000000000000000 00000000000000000000000000000000 00000000111100100000111100000000 00000000100000100000100100000000 00000000100000100000111100000000 00000000100000100000100000000000 00000000111100111100100000000000 00000000000000000000000000000000 """ raster_lines = raster_string.splitlines() high = len(raster_lines) wide = len(raster_lines[0]) bytes_in_a_row = wide/bits_in_a_byte bitmap = "" for raster_line in raster_lines: for byte_count in range(bytes_in_a_row): first_bit = byte_count*bits_in_a_byte bitmap += chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) im = Image.fromstring("1", (wide , high) , bitmap) im.save(sys.stdout , "PNG") any suggestions for improvement? bye From digitig at gmail.com Thu Jul 23 06:28:22 2009 From: digitig at gmail.com (Tim Rowe) Date: Thu, 23 Jul 2009 11:28:22 +0100 Subject: Help understanding the decisions *behind* python? In-Reply-To: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: 2009/7/22 Inky 788 : > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > just not sophisticated enough, but I've never wanted to use a list/ > tuple as a dict key. This sounds like obscure usage, and a bit > contrived as a reason for having *both* lists and tuples. If you are used to working in a language that doesn't allow it then you'll probably carry on using the work-arounds that you have always used. It almost certainly only seems obscure because you're not considering it when it would be a natural solution. In a language that builds *very* heavily on the concept of dictionaries it's not obscure at all! -- Tim Rowe From nick at craig-wood.com Thu Jul 23 07:30:08 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 06:30:08 -0500 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: Message-ID: srepmub wrote: > please send any program that doesn't work with shedskin (it still is > experimental after all) to me, or create an issue at > shedskin.googlecode.com, and I will have a look at the problem. I divided and conquered the program as suggested and eventually I got it to compile and run correctly :-) I learnt that if you have lots of variables with indeterminate type then shedskin takes a very long time indeed before blowing up! I also learnt that shedskin doesn't support the idiom I'd been using for creating shallow copies, namely the Board.__new__(Board) below. shedskin compiles it ok, but the C++ won't compile complaning about not being able to find __init__ methods Producing these warnings *WARNING* rush_hour_solver_cut_down.py:71: class 'Vehicle' has no method '__new__' *WARNING* rush_hour_solver_cut_down.py:72: variable 'new' has no type *WARNING* rush_hour_solver_cut_down.py:236: variable 'new_vehicle' has no type And these compile errors rush_hour_solver_cut_down.cpp:94: error: ?__new__? is not a member of ?__rush_hour_solver_cut_down__::Vehicle? rush_hour_solver_cut_down.cpp:95: error: expected type-specifier before ?;? token rush_hour_solver_cut_down.cpp: In member function ?void* __rush_hour_solver_cut_down__::Board::move(int, int)?: rush_hour_solver_cut_down.cpp:276: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:276: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:279: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:279: error: ?void*? is not a pointer-to-object type rush_hour_solver_cut_down.cpp:281: error: invalid conversion from ?void*? to ?__rush_hour_solver_cut_down__::Vehicle*? def copy(self): new = Board.__new__(Board) new.me_x = self.me_x new.me_y = self.me_y new.depth = self.depth new.parent = self new.best_child = None new.board = [self.board[i][:] for i in range(WIDTH)] new.rep = self.rep[:] new.vehicles = self.vehicles[:] return new I changed to using copy.copy which did work, but I couldn't name my copy methods "copy" otherwise I got this error from the C++ compile rush_hour_solver_cut_down.cpp: In member function '__rush_hour_solver_cut_down__::Vehicle* __rush_hour_solver_cut_down__::Vehicle::copy()': rush_hour_solver_cut_down.cpp:94: error: no matching function for call to '__rush_hour_solver_cut_down__::Vehicle::copy(__rush_hour_solver_cut_down__::Vehicle* const)' rush_hour_solver_cut_down.cpp:89: note: candidates are: __rush_hour_solver_cut_down__::Vehicle* __rush_hour_solver_cut_down__::Vehicle::copy() rush_hour_solver_cut_down.cpp: In member function '__rush_hour_solver_cut_down__::Board* __rush_hour_solver_cut_down__::Board::copy()': rush_hour_solver_cut_down.cpp:135: error: no matching function for call to '__rush_hour_solver_cut_down__::Board::copy(__rush_hour_solver_cut_down__::Board* const)' rush_hour_solver_cut_down.cpp:129: note: candidates are: __rush_hour_solver_cut_down__::Board* __rush_hour_solver_cut_down__::Board::copy() So I renamed them to pycopy, and they ended up looking like def pycopy(self): new = copy(self) new.parent = self new.best_child = None new.board = [self.board[i][:] for i in range(WIDTH)] new.rep = self.rep[:] new.vehicles = self.vehicles[:] return new After all that - some timing results! Python: 9.3 seconds Psyco: 5.8 seconds ShedSkin: 1.0 seconds Impressive! I put the code http://www.craig-wood.com/nick/pub/rush_hour_solver_cut_down.py I left in the commented out bits of code I had to change. This is only part of the project (375 lines) - it solves Rush Hour boards. There is another part which I haven't attempted to compile yet which finds the most difficult possible boards using a combination of back tracking and a genetic algorithm. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From stef.mientki at gmail.com Thu Jul 23 07:36:54 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 13:36:54 +0200 Subject: sqlite3 performance problems only in python Message-ID: <4A684B56.3020906@gmail.com> hello, until now I used only small / simple databases in Python with sqlite3. Now I've a large and rather complex database. The most simple query (with just a result of 100 rows), takes about 70 seconds. And all that time is consumed in "cursor.fetchall" Using the same database in Delphi, using the same query, takes less than 5 seconds (including displaying the full table in a grid). Are there in Python faster ways to get the query results ? Would it be faster if I used an ODBC coupling and PyODBC to interface the database ? thanks, Stef Mientki From deets at nospam.web.de Thu Jul 23 07:47:45 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 13:47:45 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <7cr0v1F28dhltU1@mid.uni-berlin.de> superpollo wrote: > hi. > > i wrote a program which transforms a string of zeroes ando ones into a > png file. > > #!/usr/bin/env python > import Image > import sys > bits_in_a_byte = 8 > raster_string = """\ > 00000000000000000000000000000000 > 00100100111100100000100000111100 > 00100100100000100000100000100100 > 00111100111000100000100000100100 > 00100100100000100000100000100100 > 00100100111100111100111100111100 > 00000000000000000000000000000000 > 00000000000000000000000000000000 > 00000000111100100000111100000000 > 00000000100000100000100100000000 > 00000000100000100000111100000000 > 00000000100000100000100000000000 > 00000000111100111100100000000000 > 00000000000000000000000000000000 > """ > raster_lines = raster_string.splitlines() > high = len(raster_lines) > wide = len(raster_lines[0]) > bytes_in_a_row = wide/bits_in_a_byte This will give you the wrong result if not divideable by bits_in_a_byte. > bitmap = "" > for raster_line in raster_lines: > for byte_count in range(bytes_in_a_row): > first_bit = byte_count*bits_in_a_byte > bitmap += > chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) > im = Image.fromstring("1", (wide , high) , bitmap) > im.save(sys.stdout , "PNG") > > any suggestions for improvement? Instead of res = "" for ...: res += ... use res = [] for ...: res.append(...) "".join(res) There are some optimizations for the +=-op on strings, but I'm not sure how far they go, and the other form is safer. Diez From paul at subsignal.org Thu Jul 23 07:50:47 2009 From: paul at subsignal.org (paul) Date: Thu, 23 Jul 2009 13:50:47 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: <1248342610.3698.7.camel@krishna-laptop> References: <1248342610.3698.7.camel@krishna-laptop> Message-ID: Krishnakant schrieb: > On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote: >> Being a sudoer is not a privilege to issue the os.setuid system call. It >> is only a permission to use the sudo command. >> > Yes, So I would like to know if python can change the user to some other > non-privileged user during the script execution? If the user running python program is allowed to call setuid() then yes. > >>> K> I tryed using subprocess but that did not help me either. I tryed sudo >>> K> su into the Popen command but it throws me into the terminal (shell) >>> K> with postgres as the user. >> You could execute the command: >> sudo -u postgres required_command >> with subprocess. >> > Ok, but the problem is much more complex. No. > What if I want to do the following. > 1, change the user for a particular script to the postgres user. Did you try running "sudo -u postgres blabla" with subprocess? > 2. now execute the python code for connecting to the postgresql > database. > In the second point I actually want to execute python code not shell > level command so will the sudo -u in the subprocess.Popen change the > user in the script? No, as the name "subprocess" suggests you are spawning a new process which gets another uid through sudo. This does not affect the parent process. hth Paul From piet at cs.uu.nl Thu Jul 23 08:06:14 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 14:06:14 +0200 Subject: Copy/paste through LAN References: Message-ID: >>>>> Jun (J) wrote: >J> Hello, >J> I've a client/server application which uses Pyro to communicate each >J> other. >J> Now i try to implement copy/paste through LAN between server >J> controlled >J> filesystem to my local windows machine (I could list files in client's >J> window). >J> How can i pass the url information through Pyro ? What do you mean? Do you want to copy a file or copy file names? Copying a file between two machines can best be done by a specialized protocol, like HTTP or scp. Pasting a file doesn't make sense IMHO. And URLs are just strings. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Thu Jul 23 08:23:24 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 14:23:24 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. References: Message-ID: >>>>> Krishnakant (K) wrote: >K> On Thu, 2009-07-23 at 00:17 +0200, Piet van Oostrum wrote: >>> Being a sudoer is not a privilege to issue the os.setuid system call. It >>> is only a permission to use the sudo command. >>> >K> Yes, So I would like to know if python can change the user to some other >K> non-privileged user during the script execution? As I said you can't (unless you are root). It would be a security leak if an arbitrary user could suddenly run as another user. Sudo is the escape mechanism but it runs commands, and is not for changing the uid in the middle of a process. >>> >K> I tryed using subprocess but that did not help me either. I tryed sudo >>> >K> su into the Popen command but it throws me into the terminal (shell) >>> >K> with postgres as the user. >>> >>> You could execute the command: >>> sudo -u postgres required_command >>> with subprocess. >>> >K> Ok, but the problem is much more complex. >K> What if I want to do the following. >K> 1, change the user for a particular script to the postgres user. >K> 2. now execute the python code for connecting to the postgresql >K> database. >K> In the second point I actually want to execute python code not shell >K> level command so will the sudo -u in the subprocess.Popen change the >K> user in the script? You can run another python script as the other user (o even the same python script). You said you tried subprocess. If that is acceptable then running another python script should also be acceptable, becaus eit is basically the same. >K> In short I would just like to have the script run under another user >K> let's say postgres as long as a certain action is going on, for example >K> connecting to the postgresql database. Why would you have to be another user for connecting to a postgres database? The DBMS takes care of the permissions at the DB level. Otherwise you would have to do the DB access in another script. The script could even communicate withe the original script, e.g by pipes or some protocol like XMLRPC. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jyoti.mickey at gmail.com Thu Jul 23 08:30:34 2009 From: jyoti.mickey at gmail.com (Jyoti Sharma) Date: Thu, 23 Jul 2009 18:00:34 +0530 Subject: how two join and arrange two files together References: Message-ID: On Thu, 23 Jul 2009 12:52:15 +0530, wrote: > > Hi, > > I have two large files: > > FileA > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 > 23 ALA H = 8.78 N = CA = HA = C = 179.93................. > > and > > FileB > 21 ALA helix (helix_alpha, helix2) > 23 ALA helix (helix_alpha, helix3) > 38 ALA helix (helix_alpha, helix3)........... > > now what i want that i will make another file in which i will join the > two > file in such a way that only matching entries will come like here 21 and > 23 ALA is in both files, so the output will be something like:- > > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA helix > (helix_alpha, helix2) > 23 ALA H = 8.78 N = CA = HA = C = 179.93|23 ALA helix (helix_alpha, > helix3) > > and further i will make another file in which i will be able to put those > lines form this file based on the missing atom value, like for 21 ALA HA > is not defined so i will put it another file based on its HA missing > value > similarly i will put 23 ALA on another file based on its missing N,CA and > HA value. > > I tried to join the two file based on their matching entries by:--- (snip) I believe there are packages available for doing such things mostly written in perl. But if the aim is not to develop suitable applications but only to obtain the desired formatted file then doing this with the help of something like Excel would be easiest. This is my opinion from the experience I have from my bioinfo programming. Regards, Jyoti From hackingkk at gmail.com Thu Jul 23 08:30:53 2009 From: hackingkk at gmail.com (Krishnakant) Date: Thu, 23 Jul 2009 18:00:53 +0530 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: References: <1248342610.3698.7.camel@krishna-laptop> Message-ID: <1248352253.5100.37.camel@krishna-laptop> On Thu, 2009-07-23 at 13:50 +0200, paul wrote: > If the user running python program is allowed to call setuid() then yes. > NO, i don't think i can do that. I am getting opperation not permitted. Any ways I think probably subprocess will have to sort it out. > Did you try running "sudo -u postgres blabla" with subprocess? > Yes, but still not got the intended result which is now obvious. > > 2. now execute the python code for connecting to the postgresql > > database. > > In the second point I actually want to execute python code not shell > > level command so will the sudo -u in the subprocess.Popen change the > > user in the script? > No, as the name "subprocess" suggests you are spawning a new process > which gets another uid through sudo. This does not affect the parent > process. > Ok then here is the work-around which I am thinking to try, Plese tell me if it is correct. I will let that subprocess start python inthe background and execute the connecting code to postgresql including importing the pygresql library. Then I will create the connection and cursor objcts in that subprocess. But my concern is, will the connection object in the child process (subprocess) be available to the parrent process? happy hacking. Krishnakant. From __peter__ at web.de Thu Jul 23 08:35:37 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 14:35:37 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > i wrote a program which transforms a string of zeroes ando ones into a > png file. > > #!/usr/bin/env python > import Image > import sys > bits_in_a_byte = 8 > raster_string = """\ > 00000000000000000000000000000000 > 00100100111100100000100000111100 > 00100100100000100000100000100100 > 00111100111000100000100000100100 > 00100100100000100000100000100100 > 00100100111100111100111100111100 > 00000000000000000000000000000000 > 00000000000000000000000000000000 > 00000000111100100000111100000000 > 00000000100000100000100100000000 > 00000000100000100000111100000000 > 00000000100000100000100000000000 > 00000000111100111100100000000000 > 00000000000000000000000000000000 > """ > raster_lines = raster_string.splitlines() > high = len(raster_lines) > wide = len(raster_lines[0]) > bytes_in_a_row = wide/bits_in_a_byte > bitmap = "" > for raster_line in raster_lines: > for byte_count in range(bytes_in_a_row): > first_bit = byte_count*bits_in_a_byte > bitmap += > chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) > im = Image.fromstring("1", (wide , high) , bitmap) > im.save(sys.stdout , "PNG") > > any suggestions for improvement? You can simplify the inner loop: for first_bit in range(0, wide, bits_in_a_byte): bitmap += ... and get rid of a few helper variables. Here's a different approach: #!/usr/bin/env python import Image import string import sys mapping = string.maketrans("01", "\x00\xff") raster_string = ... width = raster_string.index("\n") height = raster_string.count("\n") raster_string = raster_string.translate(mapping, "\n") im = Image.fromstring("L", (width, height), raster_string) im.convert("1").save(sys.stdout, "PNG") The idea is to move the bit-twiddling from python to code written in C, pointless for such a tiny picture but crucial for the performance when you want to manipulate larger images. Peter From lists at cheimes.de Thu Jul 23 08:37:07 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 14:37:07 +0200 Subject: Looking for os.listdir() generator Message-ID: Hello, I'm looking for a generator version of os.listdir() for Python 2.5 and newer. I know somebody has worked on it because I've seen a generator version in a posting on some list or blog a while ago. I can't find it anymore. It seems my Google fu is lacking today. All I can find is a very old version of xlistdir. A Cython based solution is appreciated but please no ctypes version. Christian From python.list at tim.thechases.com Thu Jul 23 08:39:46 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 23 Jul 2009 07:39:46 -0500 Subject: sqlite3 performance problems only in python In-Reply-To: <4A684B56.3020906@gmail.com> References: <4A684B56.3020906@gmail.com> Message-ID: <4A685A12.4080800@tim.thechases.com> > until now I used only small / simple databases in Python with sqlite3. > Now I've a large and rather complex database. > > The most simple query (with just a result of 100 rows), > takes about 70 seconds. > And all that time is consumed in "cursor.fetchall" > > Using the same database in Delphi, > using the same query, > takes less than 5 seconds (including displaying the full table in a grid). While it may seem obvious, are you doing anything time-consuming with those results? Or have you tested just doing the fetchall() without doing any further processing? I'm curious on the timing of sql = "..." start = time() cursor.execute(sql) rows = cursor.fetchall() end = time() print end-start with no other processing. I regularly write sql that's fairly complex and brings back somewhat large datasets (sometimes in sqlite), and have never experienced problems with "simple quer[ies] (with just a result of 100 rows" taking such extrordinary times The answer from the above code will help determine whether it's the sqlite portion that's crazy (and might need some well-placed index statements; though if your Delphi code is fine, I suspect not), or if it's your application code that goes off into left field with the resulting data. -tkc From user at example.net Thu Jul 23 08:41:23 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 14:41:23 +0200 Subject: raster (PIL) In-Reply-To: <7cr0v1F28dhltU1@mid.uni-berlin.de> References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <7cr0v1F28dhltU1@mid.uni-berlin.de> Message-ID: <4a685a75$0$40009$4fafbaef@reader3.news.tin.it> Diez B. Roggisch wrote: > superpollo wrote: ... >>high = len(raster_lines) >>wide = len(raster_lines[0]) >>bytes_in_a_row = wide/bits_in_a_byte > > > This will give you the wrong result if not divideable by bits_in_a_byte. > then maybe: #!/usr/bin/env python import Image import sys bits_in_a_byte = 8 raster_string = """\ 0000000000000000000000 00100100111100100000100000111100 00100100100000100000100000100100 00111100111000100000100000100100 00100100100000100000100000100100 00100100111100111100111100111100 00000000000000000000000000000000 00000000000000000000000000000000 00000000111100100000111100000000 00000000100000100000100100000000 00000000100000100000111100000000 00000000100000100000100000000000 00000000111100111100100000000000 00000000000000000000000000000000 """ raster_lines = raster_string.splitlines() high = len(raster_lines) wide = len(raster_lines[0]) bytes_in_a_row = wide/bits_in_a_byte wide_for_real = bytes_in_a_row*bits_in_a_byte if wide_for_real: bitmap = "" for raster_line in raster_lines: for byte_count in range(bytes_in_a_row): first_bit = byte_count*bits_in_a_byte bitmap += chr(int(raster_line[first_bit:first_bit+bits_in_a_byte] , 2)) im = Image.fromstring("1", (wide_for_real , high) , bitmap) im.save(sys.stdout , "PNG") bye From piet at cs.uu.nl Thu Jul 23 08:48:29 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 14:48:29 +0200 Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> On Jul 21, 8:59?pm, Piet van Oostrum wrote: >>> The recipient_public_key.pem file is the public key of the recipient >>> which means the person that is going to receive the encrypted message. >>> You should get it from the recipient him/herself or from some key store >>> where s/he has deposited it. [...] >j> error coming like - IOError: [Errno 2] No such file or directory: >j> 'recipient_public_key.pem' >j> Is this not the inbuilt file. >j> How should i create such type of file. You don't create it. See above. If you understand what is is you know why it can't be builtin. If you don't understand it is better if you first learn about OpenSSL, otherwise you run the risk to make serious errors. If you are just experimenting to create a message for yourself then you have to create the public key because you are then the recipient yourself. That has been answered on http://stackoverflow.com/questions/1169798/m2crypto-package -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From stephen.cuppett at sas.com Thu Jul 23 08:48:44 2009 From: stephen.cuppett at sas.com (Stephen Cuppett) Date: Thu, 23 Jul 2009 08:48:44 -0400 Subject: Predefined Variables References: Message-ID: os.environment('QUERY_STRING') "Fred Atkinson" wrote in message news:p2qg65d0t4kki1sh0t3v6ileamhkvl9hgj at 4ax.com... > Is there a pre-defined variable that returns the GET line > (http://www.php.net/index.php?everythingafterthequestionmark) as a > single variable (rather than individual variables)? > > Regards, > > > > Fred From stephen.cuppett at sas.com Thu Jul 23 08:50:27 2009 From: stephen.cuppett at sas.com (Stephen Cuppett) Date: Thu, 23 Jul 2009 08:50:27 -0400 Subject: Predefined Variables References: Message-ID: $_SERVER['QUERY_STRING']; (if it's PHP) "Fred Atkinson" wrote in message news:p2qg65d0t4kki1sh0t3v6ileamhkvl9hgj at 4ax.com... > Is there a pre-defined variable that returns the GET line > (http://www.php.net/index.php?everythingafterthequestionmark) as a > single variable (rather than individual variables)? > > Regards, > > > > Fred From user at example.net Thu Jul 23 08:58:45 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 14:58:45 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <4a685e86$0$40011$4fafbaef@reader3.news.tin.it> Peter Otten wrote: > superpollo wrote: > > >>i wrote a program which transforms a string of zeroes ando ones into a >>png file. ... >>any suggestions for improvement? ... > Here's a different approach: ... > The idea is to move the bit-twiddling from python to code written in C, > pointless for such a tiny picture but crucial for the performance when you > want to manipulate larger images. very very interesting... you know i come from a lower level language approach (C/Pascal) so i find it difficult (but full of fascination) to adapt to such a different and much simpler way of thinking. anyways, thanks a lot. bye From stef.mientki at gmail.com Thu Jul 23 09:02:45 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 15:02:45 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: <4A685A12.4080800@tim.thechases.com> References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: <4A685F75.10701@gmail.com> Tim Chase wrote: >> until now I used only small / simple databases in Python with sqlite3. >> Now I've a large and rather complex database. >> >> The most simple query (with just a result of 100 rows), >> takes about 70 seconds. >> And all that time is consumed in "cursor.fetchall" >> >> Using the same database in Delphi, >> using the same query, >> takes less than 5 seconds (including displaying the full table in a >> grid). > > While it may seem obvious, are you doing anything time-consuming with > those results? Or have you tested just doing the fetchall() without > doing any further processing? I'm curious on the timing of > > sql = "..." > start = time() > cursor.execute(sql) > rows = cursor.fetchall() > end = time() > print end-start No this is exactly what I did, I timed the execute and fetchall seperatly: execute: 125 msec fetchall: 71000 msec (returning 100 rows and 25 columns) pysqlite: version 2.3.2 btw, I don't know if it's of any importance, the SQL-statement I perform is select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* from OPNAMEN inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID inner join POID_SSID_SCID on ( OPNAMEN.POID = POID_SSID_SCID.POID ) and ( SUBSCHAAL_GEGEVENS.SSID = POID_SSID_SCID.SSID ) inner join SCORES on SCORES.SCID = POID_SSID_SCID.SCID inner join PID_POID on OPNAMEN.POID = PID_POID.POID inner join PATIENT on PATIENT.PID = PID_POID.PID where substr ( lower( NAME) , 1, 6) = 'cis20r' and lower ( NAME_ ) = 'fatigue' and TEST_COUNT in (3,4) and DATETIME > 39814.0 and SCORE < 30 cheers, Stef > > with no other processing. I regularly write sql that's fairly complex > and brings back somewhat large datasets (sometimes in sqlite), and > have never experienced problems with "simple quer[ies] (with just a > result of 100 rows" taking such extrordinary times > > The answer from the above code will help determine whether it's the > sqlite portion that's crazy (and might need some well-placed index > statements; though if your Delphi code is fine, I suspect not), or if > it's your application code that goes off into left field with the > resulting data. > > -tkc > > > > From user at example.net Thu Jul 23 09:02:55 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 15:02:55 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <4a685f81$0$40008$4fafbaef@reader3.news.tin.it> Peter Otten wrote: ... > Here's a different approach: ... > raster_string = ... > > width = raster_string.index("\n") > height = raster_string.count("\n") your approach has a funny side-effect: try to remove just one zero from the first line of the raster ;-) bye From franke.rob at googlemail.com Thu Jul 23 09:03:06 2009 From: franke.rob at googlemail.com (Robert Franke) Date: Thu, 23 Jul 2009 15:03:06 +0200 Subject: Pyserial and pyQt In-Reply-To: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> Message-ID: On Tue, Jul 21, 2009 at 9:37 PM, Seth wrote: > > I have used pyserial in the past but this is my first experience with > pyQt. I am using the Python xy package for windows current but might > move to linux. I have a small device that is outputting a basic text > string. I want to be able to read this string(from the comm port) and > update a text box and eventually a graph in pyQt. I can't find any > documentation or tutorials on how to do this. If anyone can point me > in the right direction or give me some tips I would be grateful. > As I need to do something similar I looked around and found the following recipe at activestate: http://code.activestate.com/recipes/82965/ In the comments there is an example for PyQt. Cheers, Robert -------------- next part -------------- An HTML attachment was scrubbed... URL: From user at example.net Thu Jul 23 09:16:51 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 15:16:51 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> Message-ID: <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> Peter Otten wrote: ... > im.convert("1").save(sys.stdout, "PNG") ... a q about pil: im.convert("1") is different from: im2 = im.convert("1") right? in the former im is changed (the method applies to im) but in the latter im is unchanged (first im is copied unto im2 and then the method is applied to im2)... am i right? bye From dangets at gmail.com Thu Jul 23 09:19:33 2009 From: dangets at gmail.com (DG) Date: Thu, 23 Jul 2009 06:19:33 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: Message-ID: <74cd3059-cd88-418b-8444-766a125d93c3@y4g2000prf.googlegroups.com> On Jul 22, 6:05?pm, "Gabriel Genellina" wrote: > En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James ? > escribi?: > > > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina ? > > wrote: > > >> class X(object): > >> ? ?foo = descriptor() > > >> x = X() > >> x.foo = "value" > > > Isn't this going to create a brand new instance attribute x.foo that has ? > > nothing to do with the descriptor anyway? > > No, it's up to the descriptor __set__ method what happens in this case. ? > Think of the standard 'property' descriptor, the fset function can do ? > whatever it wants. > Also, a data descriptor takes precedence over any instance attribute of ? > the same name that might exist. > > -- > Gabriel Genellin You might've already thought of this (and it is annoying), but you could pass the name through the descriptor's init method. I believe this is the only way besides assigning a metaclass that will look for that type of descriptor upon class creation and set the descriptor's name at that time. class A(object): def __init__(self, attr_name): self._name = attr_name def __set__(self, instance, value): self.instance.__dict__[self._name] = value # or something like that... class B(object): foo = A('foo') From breamoreboy at yahoo.co.uk Thu Jul 23 09:23:27 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 Jul 2009 14:23:27 +0100 Subject: regex: multiple matching for one string In-Reply-To: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: scriptlearner at gmail.com wrote: > For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I > will like to take out the values (valuea, valueb, and valuec). How do > I do that in Python? The group method will only return the matched > part. Thanks. > > p = re.compile('#a=*;b=*;c=*;') > m = p.match(line) > if m: > print m.group(), IMHO a regex for this is overkill, a combination of string methods such as split and find should suffice. Regards. From deets at nospam.web.de Thu Jul 23 09:27:48 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 15:27:48 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <7cr0v1F28dhltU1@mid.uni-berlin.de> <4a685a75$0$40009$4fafbaef@reader3.news.tin.it> Message-ID: <7cr6qkF28oinaU1@mid.uni-berlin.de> superpollo wrote: > Diez B. Roggisch wrote: >> superpollo wrote: > ... >>>high = len(raster_lines) >>>wide = len(raster_lines[0]) >>>bytes_in_a_row = wide/bits_in_a_byte >> >> >> This will give you the wrong result if not divideable by bits_in_a_byte. >> > > then maybe: > > #!/usr/bin/env python > import Image > import sys > bits_in_a_byte = 8 > raster_string = """\ > 0000000000000000000000 > 00100100111100100000100000111100 > 00100100100000100000100000100100 > 00111100111000100000100000100100 > 00100100100000100000100000100100 > 00100100111100111100111100111100 > 00000000000000000000000000000000 > 00000000000000000000000000000000 > 00000000111100100000111100000000 > 00000000100000100000100100000000 > 00000000100000100000111100000000 > 00000000100000100000100000000000 > 00000000111100111100100000000000 > 00000000000000000000000000000000 > """ > raster_lines = raster_string.splitlines() > high = len(raster_lines) > wide = len(raster_lines[0]) > bytes_in_a_row = wide/bits_in_a_byte > wide_for_real = bytes_in_a_row*bits_in_a_byte No. Because that would skip up to 7 bits. Instead, do bytes_in_a_row = wide/bits_in_a_byte if wide % bits_in_a_byte: bytes_in_a_row += 1 Diez From deets at nospam.web.de Thu Jul 23 09:28:50 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 15:28:50 +0200 Subject: Predefined Variables References: Message-ID: <7cr6siF28oinaU2@mid.uni-berlin.de> Fred Atkinson wrote: > Is there a pre-defined variable that returns the GET line > (http://www.php.net/index.php?everythingafterthequestionmark) as a > single variable (rather than individual variables)? Variables don't return things. Functions do. And additionally the answer depends on what and with what you actually do. Diez From paul at subsignal.org Thu Jul 23 09:32:10 2009 From: paul at subsignal.org (paul) Date: Thu, 23 Jul 2009 15:32:10 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. In-Reply-To: <1248352253.5100.37.camel@krishna-laptop> References: <1248342610.3698.7.camel@krishna-laptop> <1248352253.5100.37.camel@krishna-laptop> Message-ID: Krishnakant schrieb: > On Thu, 2009-07-23 at 13:50 +0200, paul wrote: > >> If the user running python program is allowed to call setuid() then yes. >> > NO, i don't think i can do that. I am getting opperation not permitted. > > Any ways I think probably subprocess will have to sort it out. > >> Did you try running "sudo -u postgres blabla" with subprocess? >> > Yes, but still not got the intended result which is now obvious. Why is that obvious? Works for me: ---- test.py --------- #!/usr/bin/python from subprocess import Popen, PIPE cmd = Popen('sudo -u vboxadd /home/pkoelle/Documents/whoami.sh', shell=True, stdout=PIPE, stderr=PIPE) print "OUT: "+cmd.stdout.read() print "ERR: "+cmd.stderr.read() ---- whoami.sh ----- #!/bin/bash echo $UID logger "whoami script called for $UID" Of course, you need to adapt path and user values to your situation. The user you use in your 'sudo -u ...' call needs execute permissions for whoami.sh. The relevant entry in /etc/sudoers: pkoelle ALL=NOPASSWD: /home/pkoelle/Documents/whoami.sh hth Paul PS: This has absolutely nothing to do with "connecting to postgresql". A "postgres user" is not a "system user" (Piet already asked the right questions here ;) >>> 2. now execute the python code for connecting to the postgresql >>> database. >>> In the second point I actually want to execute python code not shell >>> level command so will the sudo -u in the subprocess.Popen change the >>> user in the script? >> No, as the name "subprocess" suggests you are spawning a new process >> which gets another uid through sudo. This does not affect the parent >> process. >> > Ok then here is the work-around which I am thinking to try, Plese tell > me if it is correct. > I will let that subprocess start python inthe background and execute the > connecting code to postgresql including importing the pygresql library. > Then I will create the connection and cursor objcts in that subprocess. > But my concern is, will the connection object in the child process > (subprocess) be available to the parrent process? > > > happy hacking. > Krishnakant. > > From fatkinson at mishmash.com Thu Jul 23 09:41:30 2009 From: fatkinson at mishmash.com (Fred Atkinson) Date: Thu, 23 Jul 2009 06:41:30 -0700 Subject: Predefined Variables Message-ID: Is there a pre-defined variable that returns the GET line (http://www.php.net/index.php?everythingafterthequestionmark) as a single variable (rather than individual variables)? Regards, Fred From __peter__ at web.de Thu Jul 23 09:43:36 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 15:43:36 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Peter Otten wrote: > ... >> im.convert("1").save(sys.stdout, "PNG") > ... > > a q about pil: > > im.convert("1") > > is different from: > > im2 = im.convert("1") > > right? > > in the former im is changed (the method applies to im) but in the latter > im is unchanged (first im is copied unto im2 and then the method is > applied to im2)... am i right? No. A method has no clue whether its result is used or discarded. Therefore im.convert("1") creates a new image in the specified mode, too, which is discarded immediately. If you don't need the result you probably shouldn't call the method at all. Peter From user at example.net Thu Jul 23 09:57:03 2009 From: user at example.net (superpollo) Date: Thu, 23 Jul 2009 15:57:03 +0200 Subject: raster (PIL) In-Reply-To: References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4a686c30$0$40012$4fafbaef@reader3.news.tin.it> Peter Otten wrote: > superpollo wrote: > > >>Peter Otten wrote: >>... >> >>>im.convert("1").save(sys.stdout, "PNG") >> >>... >> >>a q about pil: >> >>im.convert("1") >> >>is different from: >> >>im2 = im.convert("1") >> >>right? >> >>in the former im is changed (the method applies to im) but in the latter >>im is unchanged (first im is copied unto im2 and then the method is >>applied to im2)... am i right? > > > No. A method has no clue whether its result is used or discarded. Therefore > > im.convert("1") > > creates a new image in the specified mode, too, which is discarded > immediately. but in: im.convert("1").save(sys.stdout, "PNG") the new (anonymous) image created by .convert is not discarded *immediately*, i mean *before* the .save method is called on it, right? bye From paul at boddie.org.uk Thu Jul 23 10:13:02 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Thu, 23 Jul 2009 07:13:02 -0700 (PDT) Subject: JavaScript toolkits (was Re: ANN: Porcupine Web Application Server 0.6 is released!) References: <1c994086-8c58-488f-b3b3-6161c4b2ba05@k30g2000yqf.googlegroups.com> Message-ID: On 23 Jul, 05:55, a... at pythoncraft.com (Aahz) wrote: > In article <1c994086-8c58-488f-b3b3-6161c4b2b... at k30g2000yqf.googlegroups.com>, > Paul Boddie ? wrote: > > >http://www.boddie.org.uk/python/XSLTools.html > > Thanks! ?I'll take a look after OSCON. The JavaScript parts of the framework are a bit complicated, I'll admit: you have to write some nasty-looking function calls with awkward arguments to send AJAX-style requests to the server. I've been meaning to employ a more declarative "signals and slots" approach so that you don't have to write in the page templates where the in-page updates should be expected: it's really the server code that determines this kind of thing, and so the server code should be able to say where it wants the page to be updated. Again, I'll try and put up some examples in the relatively near future that will make it easier for you to see if it's your kind of thing. Paul From __peter__ at web.de Thu Jul 23 10:19:38 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 16:19:38 +0200 Subject: raster (PIL) References: <4a683a13$0$40007$4fafbaef@reader3.news.tin.it> <4a6862c5$0$40016$4fafbaef@reader3.news.tin.it> <4a686c30$0$40012$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > Peter Otten wrote: >> superpollo wrote: >> >> >>>Peter Otten wrote: >>>... >>> >>>>im.convert("1").save(sys.stdout, "PNG") >>> >>>... >>> >>>a q about pil: >>> >>>im.convert("1") >>> >>>is different from: >>> >>>im2 = im.convert("1") >>> >>>right? >>> >>>in the former im is changed (the method applies to im) but in the latter >>>im is unchanged (first im is copied unto im2 and then the method is >>>applied to im2)... am i right? >> >> >> No. A method has no clue whether its result is used or discarded. >> Therefore >> >> im.convert("1") >> >> creates a new image in the specified mode, too, which is discarded >> immediately. > > but in: > > im.convert("1").save(sys.stdout, "PNG") > > the new (anonymous) image created by .convert is not discarded > *immediately*, i mean *before* the .save method is called on it, right? Of course. Think of it as a shortcut for tmp = im.convert(...) tmp.save(...) del tmp Peter From dangets at gmail.com Thu Jul 23 10:33:55 2009 From: dangets at gmail.com (DG) Date: Thu, 23 Jul 2009 07:33:55 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: <74cd3059-cd88-418b-8444-766a125d93c3@y4g2000prf.googlegroups.com> Message-ID: On Jul 23, 7:19?am, DG wrote: > On Jul 22, 6:05?pm, "Gabriel Genellina" > wrote: > > > > > En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James ? > > escribi?: > > > > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina ? > > > wrote: > > > >> class X(object): > > >> ? ?foo = descriptor() > > > >> x = X() > > >> x.foo = "value" > > > > Isn't this going to create a brand new instance attribute x.foo that has ? > > > nothing to do with the descriptor anyway? > > > No, it's up to the descriptor __set__ method what happens in this case. ? > > Think of the standard 'property' descriptor, the fset function can do ? > > whatever it wants. > > Also, a data descriptor takes precedence over any instance attribute of ? > > the same name that might exist. > > > -- > > Gabriel Genellin > > You might've already thought of this (and it is annoying), but you > could pass the name through the descriptor's init method. ?I believe > this is the only way besides assigning a metaclass that will look for > that type of descriptor upon class creation and set the descriptor's > name at that time. > > class A(object): > ? ? def __init__(self, attr_name): > ? ? ? ? self._name = attr_name > ? ? def __set__(self, instance, value): > ? ? ? ? self.instance.__dict__[self._name] = value > ? ? ? ? # or something like that... > > class B(object): > ? ? foo = A('foo') Well of course I think of more alternatives after I post. 1) still annoying... pass the class through a 'registering' function that will examine all of it's attrs and find the ones that are of your descriptor type, and assign the attr_name upon that descriptor.name attribute. 2) I just thought of, and it's kind of a hack is to do this examination within the '__set__' method. e.g. class A(object): def __get__(self, instance, owner): if instance is None: return self else: # do something different here for instances' access return self def __set__(self, instance, value): cls = instance.__class__ name = None for attr in dir(cls): if getattr(cls, attr) is self: name = attr print name From amrita at iisermohali.ac.in Thu Jul 23 10:34:15 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Thu, 23 Jul 2009 20:04:15 +0530 (IST) Subject: how two join and arrange two files together In-Reply-To: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> References: <1741.210.212.36.65.1248333735.squirrel@www.iisermohali.ac.in> Message-ID: <20373.210.212.36.65.1248359655.squirrel@www.iisermohali.ac.in> I tried to print those lines having C value missing by: import re expr = re.compile("C = None") f = open("/home/amrita/helix.dat") for line in f: if expr.search(line): print line but it is not giving any value. > > Hi, > > I have two large files: > > FileA > 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35 > 23 ALA H = 8.78 N = CA = HA = C = 179.93................. > > and > > FileB > 21 ALA helix (helix_alpha, helix2) > 23 ALA helix (helix_alpha, helix3) > 38 ALA helix (helix_alpha, helix3)........... > > now what i want that i will make another file in which i will join the two > file in such a way that only matching entries will come like here 21 and > 23 ALA is in both files, so the output will be something like:- > > 21 ALA H = 7.66 N = 123.58 CA = 54.33 HA = C = 179.35| 21 ALA helix > (helix_alpha, helix2) > 23 ALA H = 8.78 N = CA = HA = C = 179.93|23 ALA helix (helix_alpha, > helix3) > > and further i will make another file in which i will be able to put those > lines form this file based on the missing atom value, like for 21 ALA HA > is not defined so i will put it another file based on its HA missing value > similarly i will put 23 ALA on another file based on its missing N,CA and > HA value. > > I tried to join the two file based on their matching entries by:--- >>>>from collections import defaultdict >>>> >>>> if __name__ == "__main__": > ... a = open("/home/amrita/alachems/chem100.txt") > ... c = open("/home/amrita/secstr/secstr100.txt") > ... >>>> def source(stream): > ... return (line.strip() for line in stream) > ... > ... >>>> def merge(sources): > ... for m in merge([source(a),source(c)]): > ... print "|".join(c.ljust(10) for c in m) > ... > > but it is not giving any value. > > > > > > > Thanks, > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From rhodri at wildebst.demon.co.uk Thu Jul 23 10:39:15 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 23 Jul 2009 15:39:15 +0100 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: On Thu, 23 Jul 2009 01:05:55 +0100, Gabriel Genellina wrote: > En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James > escribi?: >> On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina >> wrote: >> >> >>> class X(object): >>> foo = descriptor() >>> >>> x = X() >>> x.foo = "value" >> >> Isn't this going to create a brand new instance attribute x.foo that >> has nothing to do with the descriptor anyway? > > No, it's up to the descriptor __set__ method what happens in this case. > Think of the standard 'property' descriptor, the fset function can do > whatever it wants. If it gets called, that is. > Also, a data descriptor takes precedence over any instance attribute of > the same name that might exist. This was the bit I wasn't clear on. Thanks! -- Rhodri James *-* Wildebeest Herder to the Masses From manu3d at gmail.com Thu Jul 23 10:44:50 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Thu, 23 Jul 2009 07:44:50 -0700 (PDT) Subject: import vs imp and friends. Message-ID: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Greetings, I was looking in the archive of this newsgroup and I found this snippet: import imp sourcecode = 'def foo(x): return 11*x' mod = imp.new_module('foo') exec sourcecode in mod.__dict__ mod.foo(16) Together with similar and sometimes more complete snippets available they show how a module can be created out of string, plain text files and compiled files. Neat! Now the question. Apart from checking sys.module first and eventually adding the new module to it if it isn't there already, and apart from setting __file__, is there anything else that import does and this snippet doesn't? Manu From MLists at romulo.de Thu Jul 23 10:44:56 2009 From: MLists at romulo.de (Rainer Mansfeld) Date: Thu, 23 Jul 2009 16:44:56 +0200 Subject: Detect target name in descriptor __set__ method In-Reply-To: References: Message-ID: <4A687768.2050804@romulo.de> Gabriel Genellina schrieb: > I have a class attribute 'foo' which is a data descriptor. I create an > instance of such class. When I say instance.foo = value, the descriptor > __set__ method is called. Is there any way to obtain the name being > assigned to? ('foo' in this example). That is, I want to know the target > name for the assignment that triggered the __set__ method call. > class descriptor(object): def __get__(self, instance, owner): return self def __set__(self, instance, value): # I want to know the *name* this value is being assigned to for name in instance.__class__.__dict__: if getattr(instance, name) is self: print "assigning to %s" % name break class X(object): foo = descriptor() bar = descriptor() class Y(object): foo = descriptor() baz = descriptor() x = X() y = Y() x.foo = "value" x.bar = "value" y.foo = "value" y.baz = "value" Does this work for you? Rainer From luismgz at gmail.com Thu Jul 23 10:53:48 2009 From: luismgz at gmail.com (Neuruss) Date: Thu, 23 Jul 2009 07:53:48 -0700 (PDT) Subject: ANN: psyco V2 References: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> Message-ID: <649b38ef-fced-457e-953a-ffaf2fd9012e@c29g2000yqd.googlegroups.com> It seems psyco.org is still in the transfer process... Is there any charitable soul with a link to a Windows binary? :-) From lists at cheimes.de Thu Jul 23 11:04:05 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 17:04:05 +0200 Subject: import vs imp and friends. In-Reply-To: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> References: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Message-ID: Emanuele D'Arrigo wrote: > Now the question. Apart from checking sys.module first and eventually > adding the new module to it if it isn't there already, and apart from > setting __file__, is there anything else that import does and this > snippet doesn't? The import statement does several things. For instance it holds the import lock to stop other threads from importing the same module again. It also does lots of additional work for packages like relative imports, checking __path__, setting attributes on parent packages and so on. The import system also does a lot of work in order to find and load a module, too. Christian From lists at cheimes.de Thu Jul 23 11:17:42 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 17:17:42 +0200 Subject: ANN: psyco V2 In-Reply-To: <649b38ef-fced-457e-953a-ffaf2fd9012e@c29g2000yqd.googlegroups.com> References: <0cdc63fe-4a40-47b5-ab9c-715a8a9b0e70@d15g2000prc.googlegroups.com> <649b38ef-fced-457e-953a-ffaf2fd9012e@c29g2000yqd.googlegroups.com> Message-ID: <4A687F16.3030501@cheimes.de> Neuruss wrote: > It seems psyco.org is still in the transfer process... > Is there any charitable soul with a link to a Windows binary? :-) It seems like Christian is already working on Windows binaries. We are having a discussing about an obscure MinGW bug on the Python developer list. It looks like MinGW isn't fully ABI compatible with MSVC. Christian From lists at cheimes.de Thu Jul 23 11:22:55 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 17:22:55 +0200 Subject: ANN: psyco V2 In-Reply-To: <4A5FE13A.3080708__12565.7702716582$1247820318$gmane$org@stackless.com> References: <4A5FE13A.3080708__12565.7702716582$1247820318$gmane$org@stackless.com> Message-ID: <4A68804F.4030802@cheimes.de> Christian Tismer wrote: > Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, > and Mac OS X. Psyco is not supporting 64 bit, yet. But it > is well being considered. Can you estimate how much work needs to be done in order to get Psyco working on 64bit POSIX (Linux) systems? Christian From Aaron.Dushku at ma.usda.gov Thu Jul 23 11:27:34 2009 From: Aaron.Dushku at ma.usda.gov (Dushku, Aaron - Amherst, MA) Date: Thu, 23 Jul 2009 10:27:34 -0500 Subject: Office COM automatisation - calling python from VBA Message-ID: <7B11445876E166488E821C88E5A18778032F136CA3@mostlouis7s302.agent.one.usda.gov> I'd like a copy of that code. Thanks for taking the time for all of us. Sincerely, Aaron Dushku ****************************** Aaron Dushku GIS Specialist USDA-NRCS Amherst, Massachusetts (413) 253-4379 Email: aaron.dushku at ma.usda.gov -------------- next part -------------- An HTML attachment was scrubbed... URL: From drakonik at gmail.com Thu Jul 23 11:29:01 2009 From: drakonik at gmail.com (Nick Dumas) Date: Thu, 23 Jul 2009 11:29:01 -0400 Subject: regex: multiple matching for one string In-Reply-To: References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Agreed. Two string.split()s, first at the semi-colon and then at the equal sign, will yield you your value, without having to fool around with regexes. On 7/23/2009 9:23 AM, Mark Lawrence wrote: > scriptlearner at gmail.com wrote: >> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >> will like to take out the values (valuea, valueb, and valuec). How do >> I do that in Python? The group method will only return the matched >> part. Thanks. >> >> p = re.compile('#a=*;b=*;c=*;') >> m = p.match(line) >> if m: >> print m.group(), > > IMHO a regex for this is overkill, a combination of string methods such > as split and find should suffice. > > Regards. > -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkpogb0ACgkQLMI5fndAv9jtOwCgj3+YOLfKGvAdyCMOhh4NGgfy x5YAn1ydhUYxGlvC4Z4WlWKaa1gwviSh =jnp1 -----END PGP SIGNATURE----- From leerisq at gmail.com Thu Jul 23 11:33:39 2009 From: leerisq at gmail.com (LeeRisq) Date: Thu, 23 Jul 2009 08:33:39 -0700 (PDT) Subject: win32clipboard operation Message-ID: Hi all, Newbie question. I've written a script that outputs to a text file. Now, I just want to copy the content to win32clipboard for access to other applications. Here's the mess I've come up with so far:) import xlrd import win32clipboard def program_uno(): ofile = open(r"C:\Query\DQL.txt", "w") book = xlrd.open_workbook("C:\DocLoader\MCL_Drawing and Legacy Docloader Sheet.xls") sh = book.sheet_by_index(0) e = sh.cell_value(1, 0) a = sh.col_values(0, start_rowx=2, end_rowx=200) b = r'%' + e c = r'%Draft%' y = r"some text..." %(b, c) w = r"some more text..." ofile.writelines(y) for x in a: d = r'%' + x z = r" loop text..." %(d, c) f = ofile.writelines(z) ofile.writelines(w) def copy_text(): ifile = open(r"C:\Query\DQL.txt", "r") win32clipboard.OpenClipboard(0) win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText() win32clipboard.CloseClipboard program_uno() copy_text() From vinay_sajip at yahoo.co.uk Thu Jul 23 11:39:50 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Thu, 23 Jul 2009 08:39:50 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> Message-ID: <716ee910-452b-4872-b4eb-5f280344c990@n11g2000yqb.googlegroups.com> On Jul 23, 4:06 am, Naoki INADA wrote: > In document stdtypes.html#file.encoding>: > > >> The encoding that this file uses. When Unicode strings are written to a file, > >> they will be converted to byte strings using this encoding. In addition, > >> when the file is connected to a terminal, the attribute gives the encoding > >> that the terminal is likely to use > > But inlogging.StreamHandler.emit() :: > > try: > if (isinstance(msg, unicode) and > getattr(stream, 'encoding', None)): > #fs = fs.decode(stream.encoding) > try: > stream.write(fs % msg) > except UnicodeEncodeError: > #Printing to terminals sometimes fails. > For example, > #with an encoding of 'cp1251', the above > write will > #work if written to a stream opened or > wrapped by > #the codecs module, but fail when writing > to a > #terminal even when the codepage is set to > cp1251. > #An extra encoding step seems to be > needed. > stream.write((fs % msg).encode > (stream.encoding)) > else: > stream.write(fs % msg) > except UnicodeError: > stream.write(fs % msg.encode("UTF-8")) > > And behavior of sys.stdout in Windows::>>> import sys > >>> sys.stdout.encoding > 'cp932' > >>> u = u"???" > >>> u > > u'\u3042\u3044\u3046'>>> print >>sys.stdout, u > ??? > >>> sys.stderr.write(u) > > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 0-2: ordinal not in range(128) > > What is file.encoding convention? > If I want to write a unicode string to a file(-like) that have > encoding attribute, I should do > (1) try: file.write(unicode_str), > (2) except UnicodeEncodeError: file.write(unicode_str.encode > (file.encoding)) > likelogging? > It seems agly. If you are writing a Unicode string to a stream which has been opened with e.g. codecs.open with a specific encoding, then the stream is actually a wrapper. You can write Unicode strings directly to it, and the wrapper stream will encode the Unicode to bytes using the specific encoding and write those bytes to the underlyting stream. In your example you didn't show sys.stderr.encoding - you showed sys.stdout.encoding and printed out something to it which seemed to give the correct result, but then wrote to sys.stderr which gave a UnicodeEncodeError. What is the encoding of sys.stderr in your example? Also note that logging had to handle what appeared to be an oddity with terminals - they (at least sometimes) have an encoding attribute but appear to expect to have bytes written to them, and not Unicode. Hence the logging kludge, which should not be needed and so has been carefully commented. Regards, Vinay Sajip From Bill at SynectixLtd.com Thu Jul 23 11:44:50 2009 From: Bill at SynectixLtd.com (Bill Davy) Date: Thu, 23 Jul 2009 16:44:50 +0100 Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: <7creq2F29npujU1@mid.individual.net> "Mark Lawrence" wrote in message news:mailman.3588.1248355389.8015.python-list at python.org... > scriptlearner at gmail.com wrote: >> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >> will like to take out the values (valuea, valueb, and valuec). How do >> I do that in Python? The group method will only return the matched >> part. Thanks. >> >> p = re.compile('#a=*;b=*;c=*;') >> m = p.match(line) >> if m: >> print m.group(), > > IMHO a regex for this is overkill, a combination of string methods such as > split and find should suffice. > > Regards. > For the OP, it can be done with regex by grouping: p = re.compile(r'#a=(*);b=(*);c=(*);') m = p.match(line) if m: print m.group(1), m.group(1) has valuea in it, etc. But this may not be the best way, but it is reasonably terse. From icebergwtf at gmail.com Thu Jul 23 11:46:04 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Thu, 23 Jul 2009 23:46:04 +0800 Subject: extract c/cpp include file with regular expression Message-ID: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> Hi all! I need to parse c/cpp source files, one requirement is to extract included header file name. here is my solution: >>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")') >>> m = re.search(p, '#include ') >>> m.group(3) 'header.h' >>> m = re.search(p, '#include "header.h"') >>> m.group(3) 'header.h' >>> m = re.search(p, '#include >> print(m) None >>> m = re.search(p, '#include "header.h>') >>> print(m) None Pretty ugly! And I know for a valid c/cpp source file, it will be not necessary to check and match '<' with '>' and " with ", but I'm wondering to see more elegant way to do such thing. tiefeng wu 2009-07-23 From victorsubervi at gmail.com Thu Jul 23 11:46:16 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Thu, 23 Jul 2009 12:46:16 -0300 Subject: Cannot Get Form To Work Message-ID: <4dc0cfea0907230846g4f593219t308c64eec4943f93@mail.gmail.com> Hi: When the form comes up the first time, there is the default value for num. When I fill in a number in the form and press send, even though the form sends to itself (same page name), I would think it would read the number sent. Here again is the code: from primeNumbers import primeNumbers try: lang = form.getfirst('lang', 'en') browser = form.getfirst('browser', 'all') site = form.getfirst('site', 'bridge') num = form.getfirst('num','') except: pass ourFile = string.split(__file__, "/") p = ourFile[len(ourFile) - 1] p = p[: - 9] site = ourFile[4][:-10] if site != '': site = site[:-1] print "Content-Type: text/html" print print """ """ if num != '': num = round(float(num)) roots = primeNumbers(num) print roots if num == '': print """
""" print '\n' -------------- next part -------------- An HTML attachment was scrubbed... URL: From dr.mtarver at ukonline.co.uk Thu Jul 23 11:48:46 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Thu, 23 Jul 2009 08:48:46 -0700 (PDT) Subject: strange python scripting error Message-ID: I have a very strange error. I have two test python files test.py and python.py which contain the following code #!/usr/bin/python print "Content-type: text/html" print print "" print "
Hello, Linux.com!
" print "" One file (test.py) works; you call it up and it shows a web page with Hello, Linux.com The other fails with a server configuration error. Both are running under Linux, same server, same permissions. Running a character scan shows that both files contain the same printable characters and are therefore typographically identical. They are absolutely the same. The only hint at a difference I can see is that my ftp program says the files are of unequal lengths. test.py is 129 bytes long. python.py 134 bytes long. A zipped folder containing both files is at www.lambdassociates.org/weird.zip Any ideas welcome. Mark From icebergwtf at gmail.com Thu Jul 23 11:54:01 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Thu, 23 Jul 2009 23:54:01 +0800 Subject: regex: multiple matching for one string In-Reply-To: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: <4314c1f70907230854qcb85dc1vde5fede76b0c80ff@mail.gmail.com> 2009/7/23 scriptlearner at gmail.com : > For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I > will like to take out the values (valuea, valueb, and valuec). ?How do > I do that in Python? ?The group method will only return the matched > part. ?Thanks. > > p = re.compile('#a=*;b=*;c=*;') > m = p.match(line) > ? ? ? ?if m: > ? ? ? ? ? ? print m.group(), > -- > http://mail.python.org/mailman/listinfo/python-list > maybe like this: >>> p = re.compile(r'#?\w+=(\w+);') >>> l = re.findall(p, '#a=valuea;b=valueb;c=valuec;') >>> for r in l: print(r) ... valuea valueb valuec tiefeng wu 2009-07-23 From deets at nospam.web.de Thu Jul 23 11:57:44 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 17:57:44 +0200 Subject: strange python scripting error References: Message-ID: <7crfjoF29e4gtU1@mid.uni-berlin.de> Mark Tarver wrote: > I have a very strange error. I have two test python files test.py and > python.py which contain the following code > > #!/usr/bin/python > print "Content-type: text/html" > print > print "" > print "
Hello, Linux.com!
" > print "" > > One file (test.py) works; you call it up and it shows a web page with > > Hello, Linux.com > > The other fails with a server configuration error. Both are running > under Linux, same server, same permissions. Running a character scan > shows that both files contain the same printable characters and are > therefore typographically identical. They are absolutely the same. > > The only hint at a difference I can see is that my ftp program says > the files are of unequal lengths. test.py is 129 bytes long. > python.py 134 bytes long. > > A zipped folder containing both files is at > > www.lambdassociates.org/weird.zip > > Any ideas welcome. They have different line-ending-conventions. Not sure if and why that makes a difference. Diez From philip at semanchuk.com Thu Jul 23 11:58:13 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 11:58:13 -0400 Subject: extract c/cpp include file with regular expression In-Reply-To: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> Message-ID: On Jul 23, 2009, at 11:46 AM, tiefeng wu wrote: > Hi all! > I need to parse c/cpp source files, one requirement is to extract > included header file name. > here is my solution: >>>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")') >>>> m = re.search(p, '#include ') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include "header.h"') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include >>> print(m) > None >>>> m = re.search(p, '#include "header.h>') >>>> print(m) > None > > Pretty ugly! And I know for a valid c/cpp source file, it will be not > necessary to check and match '<' with '>' and " with ", > but I'm wondering to see more elegant way to do such thing. Hi tiefeng, Regexes are always a little ugly IMO. =) A side note -- does your parser need to handle /* comments like this one*/? If so, then regular expressions are not going be sufficient. Good luck Philip From python at mrabarnett.plus.com Thu Jul 23 12:05:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 17:05:09 +0100 Subject: win32clipboard operation In-Reply-To: References: Message-ID: <4A688A35.3080107@mrabarnett.plus.com> LeeRisq wrote: > Hi all, > > Newbie question. I've written a script that outputs to a text file. > > Now, I just want to copy the content to win32clipboard for access to > other applications. Here's the mess I've come up with so far:) > [snip] def copy_text(): ifile = open(r"C:\Query\DQL.txt", "r") text = ifile.read() ifile.close() win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(text) win32clipboard.CloseClipboard() From dstanek at dstanek.com Thu Jul 23 12:09:55 2009 From: dstanek at dstanek.com (David Stanek) Date: Thu, 23 Jul 2009 12:09:55 -0400 Subject: sqlite3 performance problems only in python In-Reply-To: <4A685F75.10701@gmail.com> References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote: > > btw, I don't know if it's of any importance, the SQL-statement I perform is > select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* > ?from OPNAMEN > ? inner join POID_VLID ? ? ? ? ?on OPNAMEN.POID ? ? ? ? ? ?= POID_VLID.POID > ? inner join VRAAGLST ? ? ? ? ? on VRAAGLST.VLID ? ? ? ? ? = POID_VLID.VLID > ? inner join VLID_SSID ? ? ? ? ?on VRAAGLST.VLID ? ? ? ? ? = VLID_SSID.VLID > ? inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID > ? inner join POID_SSID_SCID ? ? on ( OPNAMEN.POID ? ? ? ? ? ?= > POID_SSID_SCID.POID ) and > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?( SUBSCHAAL_GEGEVENS.SSID = > POID_SSID_SCID.SSID ) > ? inner join SCORES ? ? ? ? ? ? on SCORES.SCID ? ? ? ? ? ? = > POID_SSID_SCID.SCID > ? inner join PID_POID ? ? ? ? ? on OPNAMEN.POID ? ? ? ? ? ?= PID_POID.POID > ? inner join PATIENT ? ? ? ? ? ?on PATIENT.PID ? ? ? ? ? ? = PID_POID.PID > ?where substr ( lower( NAME) , 1, 6) ?= 'cis20r' > ? and lower ( NAME_ ) = 'fatigue' > ? and TEST_COUNT in (3,4) > ? and DATETIME > 39814.0 > ? and SCORE < 30 Warning: I suck at SQL and hate it with a passion... By using lower() on the left side of the where expressions I believe that you are table scanning. So it is not the size of the data returned, but the size of the data that needs to be scanned. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From R.Brodie at rl.ac.uk Thu Jul 23 12:11:21 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Thu, 23 Jul 2009 17:11:21 +0100 Subject: strange python scripting error References: <7crfjoF29e4gtU1@mid.uni-berlin.de> Message-ID: "Diez B. Roggisch" wrote in message news:7crfjoF29e4gtU1 at mid.uni-berlin.de... > They have different line-ending-conventions. Not sure if and why that makes > a difference. Depends on your setup. Shells can be a bit dumb about it, so it will likely break simple cgi-style hosting. -bash: ./python.py: /usr/bin/python^M: bad interpreter: From python at mrabarnett.plus.com Thu Jul 23 12:13:02 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 17:13:02 +0100 Subject: extract c/cpp include file with regular expression In-Reply-To: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> Message-ID: <4A688C0E.6040705@mrabarnett.plus.com> tiefeng wu wrote: > Hi all! > I need to parse c/cpp source files, one requirement is to extract > included header file name. > here is my solution: >>>> p = re.compile(r'#\s*include\s+(?:(<)|("))(.*)(?(1)>)(?(2)")') >>>> m = re.search(p, '#include ') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include "header.h"') >>>> m.group(3) > 'header.h' >>>> m = re.search(p, '#include >>> print(m) > None >>>> m = re.search(p, '#include "header.h>') >>>> print(m) > None > > Pretty ugly! And I know for a valid c/cpp source file, it will be not > necessary to check and match '<' with '>' and " with ", > but I'm wondering to see more elegant way to do such thing. > I'd probably do: >>> p = re.compile(r'#\s*include\s+(?:<([^>]*)>|"([^"]*)")') >>> m = p.search('#include ') >>> m.group(1) or m.group(2) 'header.h' From __peter__ at web.de Thu Jul 23 12:14:03 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 18:14:03 +0200 Subject: strange python scripting error References: <7crfjoF29e4gtU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > Mark Tarver wrote: > >> I have a very strange error. I have two test python files test.py and >> python.py which contain the following code >> >> #!/usr/bin/python >> print "Content-type: text/html" >> print >> print "" >> print "
Hello, Linux.com!
" >> print "" >> >> One file (test.py) works; you call it up and it shows a web page with >> >> Hello, Linux.com >> >> The other fails with a server configuration error. Both are running >> under Linux, same server, same permissions. Running a character scan >> shows that both files contain the same printable characters and are >> therefore typographically identical. They are absolutely the same. >> >> The only hint at a difference I can see is that my ftp program says >> the files are of unequal lengths. test.py is 129 bytes long. >> python.py 134 bytes long. >> >> A zipped folder containing both files is at >> >> www.lambdassociates.org/weird.zip >> >> Any ideas welcome. > > They have different line-ending-conventions. Not sure if and why that > makes a difference. Looks like the shell treats the CR as part of the interpreter name: $ cat python.py #!/usr/bin/python print "Content-type: text/html" print print "" print "
Hello, Linux.com!
" print ""$ $ python python.py Content-type: text/html
Hello, Linux.com!
$ chmod u+x python.py $ ./python.py bash: ./python.py: /usr/bin/python^M: bad interpreter: No such file or directory Peter From dangets at gmail.com Thu Jul 23 12:21:15 2009 From: dangets at gmail.com (DG) Date: Thu, 23 Jul 2009 09:21:15 -0700 (PDT) Subject: Detect target name in descriptor __set__ method References: Message-ID: On Jul 23, 8:44?am, Rainer Mansfeld wrote: > Gabriel Genellina schrieb: > > > I have a class attribute 'foo' which is a data descriptor. I create an > > instance of such class. When I say instance.foo = value, the descriptor > > __set__ method is called. Is there any way to obtain the name being > > assigned to? ('foo' in this example). That is, I want to know the target > > name for the assignment that triggered the __set__ method call. > > class descriptor(object): > ? ? ?def __get__(self, instance, owner): > ? ? ? ?return self > > ? ? ?def __set__(self, instance, value): > ? ? ? ? ?# I want to know the *name* this value is being assigned to > ? ? ? ? ?for name in instance.__class__.__dict__: > ? ? ? ? ? ? ?if getattr(instance, name) is self: > ? ? ? ? ? ? ? ? ?print "assigning to %s" % name > ? ? ? ? ? ? ? ? ?break > > class X(object): > ? ? ?foo = descriptor() > ? ? ?bar = descriptor() > > class Y(object): > ? ? ?foo = descriptor() > ? ? ?baz = descriptor() > > x = X() > y = Y() > > x.foo = "value" > x.bar = "value" > y.foo = "value" > y.baz = "value" > > Does this work for you? > > ? ? Rainer The reason I wasn't checking the class' '__dict__' attribute in my solution was because this won't show any descriptors that were inherited from base classes. Example with some optimizations below (sorry for the long code): builtin_methods = dir(object) class descriptor(object): def __init__(self): self.name = None def __get__(self, instance, owner): # if you want a 'useful' data descriptor only return self upon # non-instance access if instance is None: return self else: # do something besides the below for the usefulness return self def __set__(self, instance, value): if self.name is None: cls = instance.__class__ dir_attrs = [m for m in dir(cls) if m not in builtin_methods] # 'foo' is printed here print 'dir(cls): ', dir_attrs # 'foo' is not printed here print 'cls.__dict__:', cls.__dict__ for name in dir_attrs: if getattr(cls, name) is self: self.name = name print "setting %s to %s" % (self.name, value) class baseX(object): foo = descriptor() class X(baseX): pass x = X() x.foo = 'bar' From icebergwtf at gmail.com Thu Jul 23 12:21:41 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Fri, 24 Jul 2009 00:21:41 +0800 Subject: extract c/cpp include file with regular expression In-Reply-To: <4A688C0E.6040705@mrabarnett.plus.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> <4A688C0E.6040705@mrabarnett.plus.com> Message-ID: <4314c1f70907230921j2a5eacd0n1fdac763765b922f@mail.gmail.com> MRAB wrote: > I'd probably do: > >>>> p = re.compile(r'#\s*include\s+(?:<([^>]*)>|"([^"]*)")') >>>> m = p.search('#include ') >>>> m.group(1) or m.group(2) > 'header.h' > yes, it's easier to understand. thanks, MRAB! I always make things complicated :P tiefeng wu 2009-07-23 From icebergwtf at gmail.com Thu Jul 23 12:36:53 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Fri, 24 Jul 2009 00:36:53 +0800 Subject: extract c/cpp include file with regular expression In-Reply-To: <35D1D3C1-ECC3-4FEC-A21E-C6A17D9E22D7@semanchuk.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> <4314c1f70907230913t76567a7cm3b3e4649f5bc6e4e@mail.gmail.com> <35D1D3C1-ECC3-4FEC-A21E-C6A17D9E22D7@semanchuk.com> Message-ID: <4314c1f70907230936w51881daaja30e1abf73742ad2@mail.gmail.com> 2009/7/24 Philip Semanchuk : > > I know this will sound like a sarcastic comment, but it is sincere: my > suggestion is that if you want to parse C/C++ (or Python, or Perl, or > Fortran, etc.), use a real parser, not regexes unless you're willing to > sacrifice some accuracy. Sooner or later you'll come across some code that > your regexes won't handle, like this -- > > #ifdef FOO_BAR > #include > /* #else */ > #include > #endif > > > Parsing code is difficult... > I understand your point, thanks for your suggestion, Philip. And I've met the problem like in your example The reason I choose regex because I barely know about "real parser", for me it still in some "dark area" :) But I'll find something to learn. tiefeng wu 2009-07-23 From tismer at stackless.com Thu Jul 23 12:41:36 2009 From: tismer at stackless.com (Christian Tismer) Date: Thu, 23 Jul 2009 09:41:36 -0700 Subject: ANN: psyco V2 In-Reply-To: <4A68804F.4030802@cheimes.de> References: <4A5FE13A.3080708__12565.7702716582$1247820318$gmane$org@stackless.com> <4A68804F.4030802@cheimes.de> Message-ID: <4A6892C0.40104@stackless.com> On 7/23/09 8:22 AM, Christian Heimes wrote: > Christian Tismer wrote: >> Psyco V2 will run on X86 based 32 bit Linux, 32 bit Windows, >> and Mac OS X. Psyco is not supporting 64 bit, yet. But it >> is well being considered. > > Can you estimate how much work needs to be done in order to get Psyco > working on 64bit POSIX (Linux) systems? This is not easy to tell. I'm in the process of estimating this, because my sponsor wants to know as well. They are very interested, but it has to be somehow affordable in time and money. There are different paths that can be taken. Simply hacking away, trying to go straight to 64 bit is obvious, but probably a bad approach. Half of the system needs to be rewritten and augmented with extra size info, and this goes very deep. I think this way I would produce a nightmare of even more complicated code, and would kill myself debugging-wise. I believe I need to simplify psyco and make many parts more abstract and more general, to become able to make it flexible. This also means slowing the compiler down quite a lot. Slowing it down will again become no problem, when my new compiler strategy is ready. The number of compilations will reduce so drastically, that the slowdown is neglectible. Yes, I did not give an answer. I have the vague feeling of three months full-time work. My problem right now is to ensure that it will become less and not more :-) cheers - chris -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From ethan at stoneleaf.us Thu Jul 23 12:42:17 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 23 Jul 2009 09:42:17 -0700 Subject: Changing the private variables content In-Reply-To: <4A679415.90407@stoneleaf.us> References: <4A671BAF.1060204@gmail.com> <4A679415.90407@stoneleaf.us> Message-ID: <4A6892E9.20602@stoneleaf.us> Or, in other words, what Steven D'Aprano had already said. Guess I should read the whole thread before madly posting! :) ~Ethan~ From mal at egenix.com Thu Jul 23 12:56:24 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Thu, 23 Jul 2009 18:56:24 +0200 Subject: Balanced binary tree implementation In-Reply-To: <4A662E37.6090406@gmail.com> References: <4A662E37.6090406@gmail.com> Message-ID: <4A689638.6050303@egenix.com> Lucas P Melo wrote: > Hello, > > I would like to use a balanced binary tree implementation (preferably > within some API). > Any hints about where I could find it? > > I am looking for something that implements insertion, deletion, search > and a special search that returns the lesser element bigger than a given > key [1]. > > A nice possibility would be an extensible API that allows me to inherit > its classes and to add operations myself. > > Thanks in advance. > > [1] Ex: 1 2 3 4 5 6 are elements of the bbt. If I use this operation > given 4 as the parameter, the value returned would be 5. You might want to have a look at the btree implementation we have in mxBeeBase: http://www.egenix.com/products/python/mxBase/mxBeeBase/ It's written in C and optimized for on-disk operations. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 23 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From philip at semanchuk.com Thu Jul 23 13:01:45 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 13:01:45 -0400 Subject: extract c/cpp include file with regular expression In-Reply-To: <4314c1f70907230936w51881daaja30e1abf73742ad2@mail.gmail.com> References: <4314c1f70907230846q1c038e96w87a5f8f2abac1e4d@mail.gmail.com> <4314c1f70907230913t76567a7cm3b3e4649f5bc6e4e@mail.gmail.com> <35D1D3C1-ECC3-4FEC-A21E-C6A17D9E22D7@semanchuk.com> <4314c1f70907230936w51881daaja30e1abf73742ad2@mail.gmail.com> Message-ID: <6835BCF7-0B3C-4E5D-B737-57B288D786D3@semanchuk.com> On Jul 23, 2009, at 12:36 PM, tiefeng wu wrote: > 2009/7/24 Philip Semanchuk : >> >> I know this will sound like a sarcastic comment, but it is sincere: >> my >> suggestion is that if you want to parse C/C++ (or Python, or Perl, or >> Fortran, etc.), use a real parser, not regexes unless you're >> willing to >> sacrifice some accuracy. Sooner or later you'll come across some >> code that >> your regexes won't handle, like this -- >> >> #ifdef FOO_BAR >> #include >> /* #else */ >> #include >> #endif >> >> >> Parsing code is difficult... >> > I understand your point, thanks for your suggestion, Philip. And I've > met the problem like in your example > The reason I choose regex because I barely know about "real parser", > for me it still in some "dark area" :) > But I'll find something to learn. Yes! Learning is always good. And as I said, if you don't mind missing some unusual cases, regexes are fine. I don't know how accurate you want your results to be. As for real parsers, there's lots of them out there, although they may be overkill for what you want to do. Here's one written entirely in Python: http://www.dabeaz.com/ply/ Whatever you choose, good luck with it. Cheers Philip From robert.kern at gmail.com Thu Jul 23 13:03:03 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 23 Jul 2009 12:03:03 -0500 Subject: PyQt GUI In-Reply-To: <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> Message-ID: On 2009-07-23 03:55, Helvin wrote: > I believe I now have vtkpython.exe. However, my 'import vtk' statement > in my python code is not working. The error says something like "no > module named vtk". > Where do I find modules for vtk in pyqt? Do they exist? There are no VTK modules in PyQt itself. The PyQt support is in the vtk package which can be built with VTK itself. You will need to install VTK and the vtk package correctly in order to achieve this. vtkpython.exe is not going to help you. Ignore it. After you have built VTK, you need to do an extra step to install the vtk package to where your Python interpreter will be able to find it. Let's say that your build directory is c:\vtkbuild. cd \vtkbuild\Wrapping\Python python setup.py install cd \ Now you should be able to import vtk from your normal Python interpreter. If you are still having problems, you will need to copy-and-paste exactly what you did and what error messages you got. Do not paraphrase error messages. -- 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 dwbear75 at gmail.com Thu Jul 23 13:06:35 2009 From: dwbear75 at gmail.com (DwBear75) Date: Thu, 23 Jul 2009 10:06:35 -0700 (PDT) Subject: installing 2.6 on vista64 Message-ID: I just downloaded and attempted to install python 2.6.2. The installer proceeds to do its work then dies, leaving an entry in the eventlog: Windows Installer installed the product. Product Name: Python 2.6.2. Product Version: 2.6.2150. Product Language: 1033. Installation success or error status: 1602. Googling for this I wasn't able to narrow the results down to something usable. Anyone know of issues and how to fix them installing on vista 64 (yes, I have 8 gigs of ram) From tismer at stackless.com Thu Jul 23 13:06:43 2009 From: tismer at stackless.com (Christian Tismer) Date: Thu, 23 Jul 2009 10:06:43 -0700 Subject: ANN: psyco V2 In-Reply-To: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> Message-ID: <4A6898A3.3060609@stackless.com> On 7/17/09 4:11 AM, Bearophile wrote: > Very good, thank you. I'll try it when I can. > > Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow? Psyco3: nice typo! :-) Well, I haven't so far found a new idea there that I'd want to borrow and did not know from PyPy, before. Wasn't the project plan saying the opposite, borrowing some ideas from psyco? :-) http://code.google.com/p/unladen-swallow/wiki/ProjectPlan > The problem I have with Psyco1.6 is that you can't use the normal > profilers to know how much seconds of running time is taken by each > function/method of your code. Yes, the profiler hooks are not useful with psyco. You need to write extra functions for timing, as we do in the benchmark directory. > Psyco1.6 has a profile() function, but I am not much able to use it > yet. The profile() function is used for profile driven compilation, as opposed to psyco.full(). This will go away, pretty soon. Psyco will only be switched on or off. Maybe I will add an option for profiling the compiled code. Interesting idea! cheers - chris -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From leerisq at gmail.com Thu Jul 23 13:07:05 2009 From: leerisq at gmail.com (LeeRisq) Date: Thu, 23 Jul 2009 10:07:05 -0700 (PDT) Subject: win32clipboard operation References: Message-ID: On Jul 23, 9:05?am, MRAB wrote: > LeeRisq wrote: > > Hi all, > > > Newbie question. I've written a script that outputs to a text file. > > > Now, I just want to copy the content to win32clipboard for access to > > other applications. Here's the mess I've come up with so far:) > > [snip] > > def copy_text(): > ? ? ?ifile = open(r"C:\Query\DQL.txt", "r") > ? ? ?text = ifile.read() > ? ? ?ifile.close() > > ? ? ?win32clipboard.OpenClipboard() > ? ? ?win32clipboard.EmptyClipboard() > ? ? ?win32clipboard.SetClipboardText(text) > ? ? ?win32clipboard.CloseClipboard() I've actually tried this configuration, but I did it again just to be sure. The program executes without exception, but the text still isn't copied to the clipboard. Any ideas? From robert.kern at gmail.com Thu Jul 23 13:09:55 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 23 Jul 2009 12:09:55 -0500 Subject: import vs imp and friends. In-Reply-To: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> References: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Message-ID: On 2009-07-23 09:44, Emanuele D'Arrigo wrote: > Greetings, > > I was looking in the archive of this newsgroup and I found this > snippet: > > import imp > sourcecode = 'def foo(x): return 11*x' > mod = imp.new_module('foo') > exec sourcecode in mod.__dict__ > mod.foo(16) > > Together with similar and sometimes more complete snippets available > they show how a module can be created out of string, plain text files > and compiled files. Neat! > > Now the question. Apart from checking sys.module first and eventually > adding the new module to it if it isn't there already, and apart from > setting __file__, is there anything else that import does and this > snippet doesn't? Brett Cannon has a good presentation that covers basically the entirety of the import mechanism: http://us.pycon.org/2008/conference/schedule/event/12/ -- 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 igouy2 at yahoo.com Thu Jul 23 13:15:00 2009 From: igouy2 at yahoo.com (Isaac Gouy) Date: Thu, 23 Jul 2009 10:15:00 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> Message-ID: <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> On Jul 21, 10:09?pm, Raffael Cavallaro wrote: > On 2009-07-21 19:06:02 -0400, Neil Hodgson > said: > > > ? ?Python uses native threads. > > So it can be teh-slowness on all ur cores! > > > > The global interpreter lock doesn't help much either. As you've linked to programs that /have not/ been written to use threading or multiple cores (look at the "~ CPU Load" column) I get the feeling I'm missing the joke? From breamoreboy at yahoo.co.uk Thu Jul 23 13:21:59 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 Jul 2009 18:21:59 +0100 Subject: Help understanding the decisions *behind* python? In-Reply-To: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: Phillip B Oldham wrote: > My colleagues and I have been working with python for around 6 months > now, and while we love a lot of what python has done for us and what > it enables us to do some of the decisions behind such certain > data-types and their related methods baffle us slightly (when compared > to the decisions made in other, similarly powerful languages). > > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > [rest of original snipped as already discussed] Sorry if this has been discussed and I've missed it, but how about memory allocation. An immutable tuple has a fixed memory allocation whereas that for the mutable list must be liable to change. You might like to look at the recent thread on this ng 'List insertion cost' and follow the links to Raymond Hettinger's power point presentation. Kindest regards. Mark Lawrence. From piet at cs.uu.nl Thu Jul 23 13:25:48 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 19:25:48 +0200 Subject: challenging problem for changing to a dedicated non-privileged user within a script. References: <1248342610.3698.7.camel@krishna-laptop> Message-ID: >>>>> Krishnakant (K) wrote: >K> On Thu, 2009-07-23 at 13:50 +0200, paul wrote: >>> If the user running python program is allowed to call setuid() then yes. >>> >K> NO, i don't think i can do that. I am getting opperation not permitted. >K> Any ways I think probably subprocess will have to sort it out. >>> Did you try running "sudo -u postgres blabla" with subprocess? >>> >K> Yes, but still not got the intended result which is now obvious. >>> > 2. now execute the python code for connecting to the postgresql >>> > database. >>> > In the second point I actually want to execute python code not shell >>> > level command so will the sudo -u in the subprocess.Popen change the >>> > user in the script? >>> No, as the name "subprocess" suggests you are spawning a new process >>> which gets another uid through sudo. This does not affect the parent >>> process. >>> >K> Ok then here is the work-around which I am thinking to try, Plese tell >K> me if it is correct. >K> I will let that subprocess start python inthe background and execute the >K> connecting code to postgresql including importing the pygresql library. >K> Then I will create the connection and cursor objcts in that subprocess. >K> But my concern is, will the connection object in the child process >K> (subprocess) be available to the parrent process? No. However it is still not clear why you want to run under the postgres user id. Why can't the original process not do the postgres connection? If that is really impossible, then you might start the new process with sudo and let it do a socket tunnelling to postgress, i.e. make a connection to the postgres server and a socket connection to the original Python script, and copy everything from one socket to the other - in both directions. However this can also be done with a ssh tunnel which might be simpler. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From python at mrabarnett.plus.com Thu Jul 23 13:42:21 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 18:42:21 +0100 Subject: win32clipboard operation In-Reply-To: References: Message-ID: <4A68A0FD.4010204@mrabarnett.plus.com> LeeRisq wrote: > On Jul 23, 9:05 am, MRAB wrote: >> LeeRisq wrote: >>> Hi all, >>> Newbie question. I've written a script that outputs to a text file. >>> Now, I just want to copy the content to win32clipboard for access to >>> other applications. Here's the mess I've come up with so far:) >> [snip] >> >> def copy_text(): >> ifile = open(r"C:\Query\DQL.txt", "r") >> text = ifile.read() >> ifile.close() >> >> win32clipboard.OpenClipboard() >> win32clipboard.EmptyClipboard() >> win32clipboard.SetClipboardText(text) >> win32clipboard.CloseClipboard() > > I've actually tried this configuration, but I did it again just to be > sure. The program executes without exception, but the text still isn't > copied to the clipboard. Any ideas? All I can say is that it works for me (Windows XP Pro, service pack 3). From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Thu Jul 23 13:45:52 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Thu, 23 Jul 2009 13:45:52 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: On 2009-07-23 13:15:00 -0400, Isaac Gouy said: > I get > the feeling I'm missing the joke? Yes, you are missing the joke. The point is that if python is 60x slower than C, even if there were not a GIL, it would require running the python program on a 60 core machine just reach parity with C. The existence of the GIL means that in reality you'd probably need a several hundred core machine running python just to equal what C can do on one core. Hence the 13375p34k pseudo quote - "teh slowness on all ur cores!" -- Raffael Cavallaro From alex.repenning at gmail.com Thu Jul 23 13:54:26 2009 From: alex.repenning at gmail.com (game_designer) Date: Thu, 23 Jul 2009 10:54:26 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: Perhaps like Xah Lee I find, after many years of Lisp programming, these discussions increasingly frustrating and even, in some sense, amazing. We can speculate all we want about syntax and semantics of programing languages. What counts in the end are really the PRAGMATICS of programming languages. How can I do something with a language that is USEFUL to me? Will the result be good looking, and snappy or some ugly, dated looking, crashing application? For instance, last time I played with Scheme (drScheme) to explore some OpenGL 3D issue I was not impressed at all. One can debate the syntax and semantics of Scheme but in that particular instance all that was important to me was the fact that the Scheme example performed terrible and the threading fell completely apart when running more that a single OpenGL window. Perhaps this was coded poorly but I don't care. Scheme left a pretty bad impression. alex Prof. Alexander Repenning University of Colorado Computer Science Department Boulder, CO 80309-430 From piet at cs.uu.nl Thu Jul 23 14:20:50 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 20:20:50 +0200 Subject: sqlite3 performance problems only in python References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: >>>>> Stef Mientki (SM) wrote: >SM> btw, I don't know if it's of any importance, the SQL-statement I perform is >SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >SM> from OPNAMEN >SM> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >SM> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >SM> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >SM> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >SM> inner join POID_SSID_SCID on ( OPNAMEN.POID = >SM> POID_SSID_SCID.POID ) and >SM> ( SUBSCHAAL_GEGEVENS.SSID = >SM> POID_SSID_SCID.SSID ) >SM> inner join SCORES on SCORES.SCID = >SM> POID_SSID_SCID.SCID >SM> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >SM> inner join PATIENT on PATIENT.PID = PID_POID.PID >SM> where substr ( lower( NAME) , 1, 6) = 'cis20r' >SM> and lower ( NAME_ ) = 'fatigue' >SM> and TEST_COUNT in (3,4) >SM> and DATETIME > 39814.0 >SM> and SCORE < 30 1) Do you have indices on the join fields? 2) Look at the ANALYZE command 3) Look at the EXPLAIN command -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From paul-donnelly at sbcglobal.net Thu Jul 23 15:09:31 2009 From: paul-donnelly at sbcglobal.net (Paul Donnelly) Date: Thu, 23 Jul 2009 14:09:31 -0500 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: <87r5w72p3o.fsf@plap.localdomain> game_designer writes: > Perhaps like Xah Lee I find, after many years of Lisp programming, > these discussions increasingly frustrating and even, in some sense, > amazing. We can speculate all we want about syntax and semantics of > programing languages. What counts in the end are really the PRAGMATICS > of programming languages. How can I do something with a language that > is USEFUL to me? Will the result be good looking, and snappy or some > ugly, dated looking, crashing application? For instance, last time I > played with Scheme (drScheme) to explore some OpenGL 3D issue I was > not impressed at all. One can debate the syntax and semantics of > Scheme but in that particular instance all that was important to me > was the fact that the Scheme example performed terrible and the > threading fell completely apart when running more that a single OpenGL > window. Perhaps this was coded poorly but I don't care. Scheme left a > pretty bad impression. One implementation of one dialect of Lisp worked poorly for one particular project some unspecified number of years ago, judging by code (written by you, no less) that may or may not have been terrible? I appreciate that this was a frustrating experience, but I don't see what lesson about Lisp programming we're supposed to be getting from this. From bbarbero at inescporto.pt Thu Jul 23 15:23:54 2009 From: bbarbero at inescporto.pt (bbarbero at inescporto.pt) Date: Thu, 23 Jul 2009 21:23:54 +0200 Subject: Problems in commands.getoutput(cmd) with sox Message-ID: <20090723212354.5378695qs4beje8a@horde.inescporto.pt> Hello to all! I am a new researcher, new to Python as well, and I have a problem, when I try to get commands from sox in a python script. I am going crazy, because this code has been working before.. so I dont really know whats going on.. Ive already seen some solutions that match with what I have. I am running this: if os.path.splitext(file_au)[1] == ".au": resampled_file_path = os.path.join(resampled_path, file_au) cmd = "sox " + dir_file + " -r 44100 " + resampled_file_path print cmd output = commands.getoutput(cmd) print output What i do, is just to resample a song from dir_file to resampled_file_path. As a result of "cmd" I get: .... sox /Volumes/HAL/Datasets/Audio/gtzan_genres/rock/rock.00097.au -r 44100 /Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resampled/rock/rock.00097.au ..... If I run this on the command line, it will work perfectly! But The program stops at File "/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resamplingto44.py", line 35 output = commands.getoutput(cmd) ^ IndentationError: unexpected indent I dont have any idea, whats going on, I am trying lot of things, but I can understand where is the error as it has been running perfectly before. Any suggestions will be more than welcome! Thanks in advance for your help! Best regards, Beatriz Mora. ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program. From nick at craig-wood.com Thu Jul 23 15:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 14:29:57 -0500 Subject: extract c/cpp include file with regular expression References: Message-ID: tiefeng wu wrote: > I need to parse c/cpp source files, one requirement is to extract > included header file name. If you are serious about parsing C code then you'll want to investigate gcc-xml http://www.gccxml.org/HTML/Index.html This runs the gcc frontend over the code but instead of producing an object file, it produces a machine readable xml file describing the source. It is used by h2xml.py / xml2py.py to make ctypes header file automatically. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From gslindstrom at gmail.com Thu Jul 23 15:30:09 2009 From: gslindstrom at gmail.com (Greg Lindstrom) Date: Thu, 23 Jul 2009 14:30:09 -0500 Subject: Creating xml Message-ID: It's been a while since I've played with XML using Python but I've been asked to create XML using data from our postgres database. Currently we are generating XML directly from the database using a set of stored procedures but it is too slow (yes, I have numbers). I have been asked to create a routine to generate the XML to see if it will be faster that the sprocs (which I think it will be, based on other routines we have running). But I digress... Using my favorite search engine I see that there is a lot of material on Python and XML. At the risk of starting something (which is not my intention), what are my options if I want to create xml? Ceratinly writing my own routine is an option, but I bet there are better ones :-) How about if I need/want to parse or process an XML file? Thanks! --greg -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Thu Jul 23 15:30:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 23 Jul 2009 21:30:56 +0200 Subject: Looking for os.listdir() generator References: Message-ID: >>>>> Christian Heimes (CH) wrote: >CH> Hello, >CH> I'm looking for a generator version of os.listdir() for Python 2.5 and >CH> newer. I know somebody has worked on it because I've seen a generator >CH> version in a posting on some list or blog a while ago. I can't find it >CH> anymore. It seems my Google fu is lacking today. All I can find is a >CH> very old version of xlistdir. A Cython based solution is appreciated but >CH> please no ctypes version. Nick Craig-Wood posted these about 5 weeks ago in the thread `waling a directory with very many files' [sic!] Message id's: (ctypes) (Cython) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From leerisq at gmail.com Thu Jul 23 15:31:14 2009 From: leerisq at gmail.com (LeeRisq) Date: Thu, 23 Jul 2009 12:31:14 -0700 (PDT) Subject: win32clipboard operation References: Message-ID: > I've actually tried this configuration, but I did it again just to be > sure. The program executes without exception, but the text still isn't > copied to the clipboard. Any ideas? So, I think I've figured out the issue. Which brings me to another question...why is it that I can manually copy and paste the text without issue, but when I run a python script that copies the same text to the clipboard, it won't accept the amount of text I am placing on the clipboard. Is there some kind of setting I am missing? Buffer size maybe? From clp2 at rebertia.com Thu Jul 23 15:32:58 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 23 Jul 2009 12:32:58 -0700 Subject: Creating xml In-Reply-To: References: Message-ID: <50697b2c0907231232o4e5614dame0adee88d4a15cf@mail.gmail.com> On Thu, Jul 23, 2009 at 12:30 PM, Greg Lindstrom wrote: > How about if I need/want to parse or process an XML file? xml.etree.ElementTree in the standard library: http://docs.python.org/library/xml.etree.elementtree.html Cheers, Chris -- http://blog.rebertia.com From vinay_sajip at yahoo.co.uk Thu Jul 23 15:42:29 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Thu, 23 Jul 2009 12:42:29 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> Message-ID: <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> On Jul 23, 4:06 am, Naoki INADA wrote: > In document stdtypes.html#file.encoding>: > > >> The encoding that this file uses. When Unicode strings are written to a file, > >> they will be converted to byte strings using this encoding. In addition, > >> when the file is connected to a terminal, the attribute gives the encoding > >> that the terminal is likely to use > > But inlogging.StreamHandler.emit() :: > > try: > if (isinstance(msg, unicode) and > getattr(stream, 'encoding', None)): > #fs = fs.decode(stream.encoding) > try: > stream.write(fs % msg) > except UnicodeEncodeError: > #Printing to terminals sometimes fails. > For example, > #with an encoding of 'cp1251', the above > write will > #work if written to a stream opened or > wrapped by > #the codecs module, but fail when writing > to a > #terminal even when the codepage is set to > cp1251. > #An extra encoding step seems to be > needed. > stream.write((fs % msg).encode > (stream.encoding)) > else: > stream.write(fs % msg) > except UnicodeError: > stream.write(fs % msg.encode("UTF-8")) > > And behavior of sys.stdout in Windows::>>> import sys > >>> sys.stdout.encoding > 'cp932' > >>> u = u"???" > >>> u > > u'\u3042\u3044\u3046'>>> print >>sys.stdout, u > ??? > >>> sys.stderr.write(u) > > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 0-2: ordinal not in range(128) > > What is file.encoding convention? > If I want to write a unicode string to a file(-like) that have > encoding attribute, I should do > (1) try: file.write(unicode_str), > (2) except UnicodeEncodeError: file.write(unicode_str.encode > (file.encoding)) > likelogging? > It seems agly. Further to my earlier mail, please have a look at the following screenshot: http://imgur.com/FtAi0.png As you can see, the codepage is set to 1251 (Cyrillic) at the beginning. A Unicode string is initialised with Cyrillic code points. Then sys.stdout.encoding shows 'cp1251', but writing the string to it gives a UnicodeEncodeError. Explicitly encoding the string and writing it works. Next, we get a wrapper from the codecs module for the same encoding and use it to wrap sys.stdout. Writing the Unicode string to the wrapped string works, too. So the problem is essentially this: if a stream has an encoding attribute, sometimes it is a wrapped stream which encodes Unicode (e.g. a stream obtained via the codecs module) and sometimes it is not (e.g. sys.stdout, sys.stderr). Regards, Vinay From clp2 at rebertia.com Thu Jul 23 15:52:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 23 Jul 2009 12:52:48 -0700 Subject: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox) Message-ID: <50697b2c0907231252w4715c94di7876b8580cddf170@mail.gmail.com> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: > You can use tabnanny to help diagnose the problem: > http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a Anyone know what's the deal with effbot.org? It seems to be broken at present, forcing me to use Google's cached version. It's a real shame since it has lots of handy Python info. Cheers, Chris -- http://blog.rebertia.com From stef.mientki at gmail.com Thu Jul 23 15:58:30 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 21:58:30 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: <4A68C0E6.90905@gmail.com> Piet van Oostrum wrote: >>>>>> Stef Mientki (SM) wrote: >>>>>> > > >> SM> btw, I don't know if it's of any importance, the SQL-statement I perform is >> SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >> SM> from OPNAMEN >> SM> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >> SM> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >> SM> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >> SM> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >> SM> inner join POID_SSID_SCID on ( OPNAMEN.POID = >> SM> POID_SSID_SCID.POID ) and >> SM> ( SUBSCHAAL_GEGEVENS.SSID = >> SM> POID_SSID_SCID.SSID ) >> SM> inner join SCORES on SCORES.SCID = >> SM> POID_SSID_SCID.SCID >> SM> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >> SM> inner join PATIENT on PATIENT.PID = PID_POID.PID >> SM> where substr ( lower( NAME) , 1, 6) = 'cis20r' >> SM> and lower ( NAME_ ) = 'fatigue' >> SM> and TEST_COUNT in (3,4) >> SM> and DATETIME > 39814.0 >> SM> and SCORE < 30 >> > > 1) Do you have indices on the join fields? > well I'm happily surprised, you came up with this suggestion - I thought that sqlite created indexes on all primairy key and unique fields - but after explicitly creating the indices, a gained a speed of about a factor 10 After checking the database creation, it seemed I forgot to make these fields the primary key so thanks very much. I gained another factor of 10 speed by updating to version 2.5.5 of pysqlite. cheers, Stef > 2) Look at the ANALYZE command > 3) Look at the EXPLAIN command > From stef.mientki at gmail.com Thu Jul 23 16:01:16 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Thu, 23 Jul 2009 22:01:16 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: <4A68C18C.3090605@gmail.com> David Stanek wrote: > On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote: > >> btw, I don't know if it's of any importance, the SQL-statement I perform is >> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >> from OPNAMEN >> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >> inner join POID_SSID_SCID on ( OPNAMEN.POID = >> POID_SSID_SCID.POID ) and >> ( SUBSCHAAL_GEGEVENS.SSID = >> POID_SSID_SCID.SSID ) >> inner join SCORES on SCORES.SCID = >> POID_SSID_SCID.SCID >> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >> inner join PATIENT on PATIENT.PID = PID_POID.PID >> where substr ( lower( NAME) , 1, 6) = 'cis20r' >> and lower ( NAME_ ) = 'fatigue' >> and TEST_COUNT in (3,4) >> and DATETIME > 39814.0 >> and SCORE < 30 >> > > Warning: I suck at SQL and hate it with a passion... > +1, but I couldn't find anything better. I guess you have some better suggestions ? (I looked at Dee, but from my first view, it looks a bit premature) thanks, Stef From nick at craig-wood.com Thu Jul 23 16:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 15:29:57 -0500 Subject: sqlite3 performance problems only in python References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: David Stanek wrote: > On Thu, Jul 23, 2009 at 9:02 AM, Stef Mientki wrote: > > > > btw, I don't know if it's of any importance, the SQL-statement I perform is > > select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* > > ?from OPNAMEN > > ? inner join POID_VLID ? ? ? ? ?on OPNAMEN.POID ? ? ? ? ? ?= POID_VLID.POID > > ? inner join VRAAGLST ? ? ? ? ? on VRAAGLST.VLID ? ? ? ? ? = POID_VLID.VLID > > ? inner join VLID_SSID ? ? ? ? ?on VRAAGLST.VLID ? ? ? ? ? = VLID_SSID.VLID > > ? inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID > > ? inner join POID_SSID_SCID ? ? on ( OPNAMEN.POID ? ? ? ? ? ?= > > POID_SSID_SCID.POID ) and > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?( SUBSCHAAL_GEGEVENS.SSID = > > POID_SSID_SCID.SSID ) > > ? inner join SCORES ? ? ? ? ? ? on SCORES.SCID ? ? ? ? ? ? = > > POID_SSID_SCID.SCID > > ? inner join PID_POID ? ? ? ? ? on OPNAMEN.POID ? ? ? ? ? ?= PID_POID.POID > > ? inner join PATIENT ? ? ? ? ? ?on PATIENT.PID ? ? ? ? ? ? = PID_POID.PID > > ?where substr ( lower( NAME) , 1, 6) ?= 'cis20r' > > ? and lower ( NAME_ ) = 'fatigue' > > ? and TEST_COUNT in (3,4) > > ? and DATETIME > 39814.0 > > ? and SCORE < 30 > > Warning: I suck at SQL and hate it with a passion... > > By using lower() on the left side of the where expressions I believe > that you are table scanning. So it is not the size of the data > returned, but the size of the data that needs to be scanned. In all the databases I've used, the like operator has been case insensitive, so if that is the problem you could use NAME like '%cis20r%' -- not quite the same, but close! and NAME_ like 'fatigue' instead which might be quicker. Or not ;-) -- Nick Craig-Wood -- http://www.craig-wood.com/nick From nick at craig-wood.com Thu Jul 23 16:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 15:29:57 -0500 Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: Ville Vainio wrote: > Has anyone implementing something like what the subject line > indicates? > > The idea: > > To run functions that execute a series of system commands without > blocking the ui, *and* without adding state machine logic. > > The syntax would be something like: > > def work(): > > showstatus("building") > r = yield runshell("make") > showstatus("installing") > r = yield runshell("make install") > showstatus("Success") > > mygui.startwork(work) > # returns immediately, runs work() gradually in the background. > > The catch is that showstatus() would need to be run in the mainloop, > so running the whole thing in a thread is a no-go. > > I imagine runshell() would be implemented in terms of QProcess, or > subprocess.Popen/os.system and a worker thread. > > Anyone done this already, or do I have to roll my own? You might want to look at twisted, in particular http://twistedmatrix.com/trac/wiki/DeferredGenerator -- Nick Craig-Wood -- http://www.craig-wood.com/nick From nick at craig-wood.com Thu Jul 23 16:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 23 Jul 2009 15:29:57 -0500 Subject: Looking for os.listdir() generator References: Message-ID: Christian Heimes wrote: > I'm looking for a generator version of os.listdir() for Python 2.5 and > newer. I know somebody has worked on it because I've seen a generator > version in a posting on some list or blog a while ago. I can't find it > anymore. It seems my Google fu is lacking today. All I can find is a > very old version of xlistdir. A Cython based solution is appreciated but > please no ctypes version. I posted exactly that yesterday I think ;-) Note that this has a python part as well as a ctypes part because the version of ctypes I used doesn't support generators. I think the development version does though. Here is it --setup.py---------------------------------------------------------- from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( cmdclass = {'build_ext': build_ext}, ext_modules = [Extension("directory", ["directory.pyx"])] ) --directory.pyx---------------------------------------------------------- # Cython interface for listdir # python setup.py build_ext --inplace import cython cdef extern from "dirent.h": struct dirent: char d_name[0] ctypedef struct DIR DIR *opendir(char *name) int closedir(DIR *dirp) dirent *readdir(DIR *dirp) cdef extern from "errno.h": int errno cdef extern from "string.h": char *strerror(int errnum) cdef class Directory: """Represents an open directory""" cdef DIR *handle def __init__(self, path): self.handle = opendir(path) if self.handle is NULL: raise OSError(errno, "Failed to open directory: %s" % strerror(errno)) def readdir(self): """Read the next name in the directory""" cdef dirent *p p = readdir(self.handle) if p is NULL: return None return p.d_name def close(self): """Close the directory""" if self.handle is not NULL: closedir(self.handle) self.handle = NULL def __dealloc__(self): self.close() --listdir.py---------------------------------------------------------- from directory import Directory def listdir(path): """ A generator to return the names of files in the directory passed in """ d = Directory(path) while True: name = d.readdir() if not name: break if name not in (".", ".."): yield name d.close() del d if __name__ == "__main__": import sys paths = sys.argv[1:] if not paths: paths = ["."] for path in paths: print "*** %s ***" % path for name in listdir(path): print name ------------------------------------------------------------ -- Nick Craig-Wood -- http://www.craig-wood.com/nick From jaime.frio at gmail.com Thu Jul 23 16:34:21 2009 From: jaime.frio at gmail.com (Jaime Fernandez del Rio) Date: Thu, 23 Jul 2009 22:34:21 +0200 Subject: installing 2.6 on vista64 In-Reply-To: References: Message-ID: <97a8f1a70907231334y1fd64890g9c8cee11b1e41f8f@mail.gmail.com> I have installed the 32 bit 2.6 and the 64 bit 3.1 on my machine running Vista 64 without any issues. Which of course has nothing to do with your problem.... On Thu, Jul 23, 2009 at 7:06 PM, DwBear75 wrote: > I just downloaded and attempted to install python 2.6.2. ?The > installer proceeds to do its work then dies, leaving an entry in the > eventlog: > > Windows Installer installed the product. Product Name: Python 2.6.2. > Product Version: 2.6.2150. Product Language: 1033. Installation > success or error status: 1602. > > Googling for this I wasn't able to narrow the results down to > something usable. Anyone know of issues and how to fix them installing > on vista 64 (yes, I have 8 gigs of ram) > -- > http://mail.python.org/mailman/listinfo/python-list > -- (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ay?dale en sus planes de dominaci?n mundial. From malaclypse2 at gmail.com Thu Jul 23 16:50:56 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 23 Jul 2009 16:50:56 -0400 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A685F75.10701@gmail.com> Message-ID: <16651e80907231350t554f8603i7e6bb82172169b52@mail.gmail.com> On Thu, Jul 23, 2009 at 4:29 PM, Nick Craig-Wood wrote: > In all the databases I've used, the like operator has been case > insensitive, so if that is the problem you could use This is not true in all databases! ?Many times, this is something that is configurable when setting up the database server. ?I'm not sure what the defaults are, but here at work we have DB/2 and Oracle servers that are case sensitive, and at least one MSSQL server that is not. -- Jerry From laran.evans at gmail.com Thu Jul 23 16:53:19 2009 From: laran.evans at gmail.com (Laran Evans) Date: Thu, 23 Jul 2009 13:53:19 -0700 (PDT) Subject: 2.3 missing on OSX Message-ID: I just tried to run MacVim on OSX 10.5. It crashed with this: Dyld Error Message: Library not loaded: /System/Library/Frameworks/Python.framework/ Versions/2.3/Python Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ MacVim.app/Contents/MacOS/Vim Reason: image not found I posted to the MacVim mailing list and was told that that version of 2.3 should always be available because it comes with the OS. I don't remember if I removed it for whatever reason at some point or something. In any case, I have python installed now via MacPorts. I have version 2.3 and 2.5 both installed. So my question is, is there a way for me to fix the issue by re- instating the default 2.3 location or something? I'm not much of a Python person. So I don't really know where to start with this one. Thanks. From deets at nospam.web.de Thu Jul 23 17:01:21 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 23 Jul 2009 23:01:21 +0200 Subject: 2.3 missing on OSX In-Reply-To: References: Message-ID: <7cs1d1F1tbs10U1@mid.uni-berlin.de> Laran Evans schrieb: > I just tried to run MacVim on OSX 10.5. It crashed with this: > > Dyld Error Message: > Library not loaded: /System/Library/Frameworks/Python.framework/ > Versions/2.3/Python > Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ > MacVim.app/Contents/MacOS/Vim > Reason: image not found > > I posted to the MacVim mailing list and was told that that version of > 2.3 should always be available because it comes with the OS. On my 10.5 machine, it is under the above given path. > > I don't remember if I removed it for whatever reason at some point or > something. In any case, I have python installed now via MacPorts. I > have version 2.3 and 2.5 both installed. > > So my question is, is there a way for me to fix the issue by re- > instating the default 2.3 location or something? Most probably yes, but not using MacPorts. Those aren't framework-builds, and not in the proper location anyway. I suggest you try & get hold of a copy from another OSX-machine (match teh architecture), and just copy it into the needed location. Diez From philip at semanchuk.com Thu Jul 23 17:03:41 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 17:03:41 -0400 Subject: 2.3 missing on OSX In-Reply-To: References: Message-ID: On Jul 23, 2009, at 4:53 PM, Laran Evans wrote: > I just tried to run MacVim on OSX 10.5. It crashed with this: > > Dyld Error Message: > Library not loaded: /System/Library/Frameworks/Python.framework/ > Versions/2.3/Python > Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ > MacVim.app/Contents/MacOS/Vim > Reason: image not found > > I posted to the MacVim mailing list and was told that that version of > 2.3 should always be available because it comes with the OS. OS X 10.4 shipped with Python 2.3 as the system Python. OS X 10.5 ships with Python 2.5 as the system Python. 2.3 is not available unless you install it yourself. And if you install it yourself, I wouldn't put it where MacVim is looking for it because the System folder is OS X stuff. If MacVim (or any app) contains a hardcoded reference to /System/.../2.3/Python, then it will break on OS X 10.5. From philip at semanchuk.com Thu Jul 23 17:14:37 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 17:14:37 -0400 Subject: 2.3 missing on OSX In-Reply-To: <7cs1d1F1tbs10U1@mid.uni-berlin.de> References: <7cs1d1F1tbs10U1@mid.uni-berlin.de> Message-ID: On Jul 23, 2009, at 5:01 PM, Diez B. Roggisch wrote: > Laran Evans schrieb: >> I just tried to run MacVim on OSX 10.5. It crashed with this: >> Dyld Error Message: >> Library not loaded: /System/Library/Frameworks/Python.framework/ >> Versions/2.3/Python >> Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ >> MacVim.app/Contents/MacOS/Vim >> Reason: image not found >> I posted to the MacVim mailing list and was told that that version of >> 2.3 should always be available because it comes with the OS. > > On my 10.5 machine, it is under the above given path. Diez is correct, please ignore my earlier post on this topic. I have 2.3 on my machine in that location as well; I didn't look closely enough. Apologies for the misinformation. I'm not answering any more Mac Python questions as I seem to get them wrong every time. :-/ From nad at acm.org Thu Jul 23 17:14:50 2009 From: nad at acm.org (Ned Deily) Date: Thu, 23 Jul 2009 14:14:50 -0700 Subject: 2.3 missing on OSX References: Message-ID: In article , Laran Evans wrote: > I just tried to run MacVim on OSX 10.5. It crashed with this: > > Dyld Error Message: > Library not loaded: /System/Library/Frameworks/Python.framework/ > Versions/2.3/Python > Referenced from: /Users/laran/Downloads/MacVim-7_2-stable-1_2/ > MacVim.app/Contents/MacOS/Vim > Reason: image not found > > I posted to the MacVim mailing list and was told that that version of > 2.3 should always be available because it comes with the OS. > > I don't remember if I removed it for whatever reason at some point or > something. In any case, I have python installed now via MacPorts. I > have version 2.3 and 2.5 both installed. > > So my question is, is there a way for me to fix the issue by re- > instating the default 2.3 location or something? If, in fact, /System/Library/Frameworks/Python.framework/Versions/2.3 is empty or missing, you should restore it. Rule of thumb: everything under /System is installed and managed by Apple. Don't remove things from there! I don't know of a particularly easy way to restore it, short of hacking a Leopard installer image or re-installing OS X. If you have an unused external drive or partition, it might be easiest to do a vanilla install of Leopard and all software updates, then, from the terminal, do a ditto of that directory subtree back to your regular /. -- Ned Deily, nad at acm.org From __peter__ at web.de Thu Jul 23 17:20:42 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Jul 2009 23:20:42 +0200 Subject: Problems in commands.getoutput(cmd) with sox References: Message-ID: bbarbero at inescporto.pt wrote: > Hello to all! > > I am a new researcher, new to Python as well, and I have a problem, > when I try to get commands from sox in a python script. I am going > crazy, because this code has been working before.. so I dont really > know whats going on.. > > Ive already seen some solutions that match with what I have. I am > running this: > > > > if os.path.splitext(file_au)[1] == ".au": > resampled_file_path = os.path.join(resampled_path, file_au) > cmd = "sox " + dir_file + " -r 44100 " + resampled_file_path > print cmd > output = commands.getoutput(cmd) > print output > > What i do, is just to resample a song from dir_file to > resampled_file_path. As a result of "cmd" I get: > > .... > sox /Volumes/HAL/Datasets/Audio/gtzan_genres/rock/rock.00097.au -r > 44100 > /Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resampled/rock/rock.00097.au > ..... > > If I run this on the command line, it will work perfectly! > > But The program stops at > > File > "/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resamplingto44.py", > line 35 > output = commands.getoutput(cmd) > ^ > IndentationError: unexpected indent > > I dont have any idea, whats going on, I am trying lot of things, but I > can understand where is the error as it has been running perfectly > before. > > Any suggestions will be more than welcome! Thanks in advance for your > help! You are probably mixing tabs and spaces and have set your editor to display a tabsize != 8 spaces. When you change the tabsize to 8 you will see the inconsistent indentation. In the future you can avoid such problems by configuring your editor to replace one tab with four spaces. Recommended reading: http://www.python.org/dev/peps/pep-0008/ Peter From python at mrabarnett.plus.com Thu Jul 23 17:25:39 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 23 Jul 2009 22:25:39 +0100 Subject: Problems in commands.getoutput(cmd) with sox In-Reply-To: <20090723212354.5378695qs4beje8a@horde.inescporto.pt> References: <20090723212354.5378695qs4beje8a@horde.inescporto.pt> Message-ID: <4A68D553.6090203@mrabarnett.plus.com> bbarbero at inescporto.pt wrote: > Hello to all! > > I am a new researcher, new to Python as well, and I have a problem, when > I try to get commands from sox in a python script. I am going crazy, > because this code has been working before.. so I dont really know whats > going on.. > > Ive already seen some solutions that match with what I have. I am > running this: > > > > if os.path.splitext(file_au)[1] == ".au": > resampled_file_path = os.path.join(resampled_path, file_au) > cmd = "sox " + dir_file + " -r 44100 " + resampled_file_path > print cmd ^ extra indent > output = commands.getoutput(cmd) > print output > > What i do, is just to resample a song from dir_file to > resampled_file_path. As a result of "cmd" I get: > > .... > sox /Volumes/HAL/Datasets/Audio/gtzan_genres/rock/rock.00097.au -r 44100 > /Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resampled/rock/rock.00097.au > > ..... > > If I run this on the command line, it will work perfectly! > > But The program stops at > > File > "/Volumes/bmorab/Audio_Projecto/Data/gtzan_genres/resamplingto44.py", > line 35 > output = commands.getoutput(cmd) > ^ > IndentationError: unexpected indent > > I dont have any idea, whats going on, I am trying lot of things, but I > can understand where is the error as it has been running perfectly before. > > Any suggestions will be more than welcome! Thanks in advance for your help! > You need to double-check the indentation. From gh at ghaering.de Thu Jul 23 17:27:08 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Thu, 23 Jul 2009 23:27:08 +0200 Subject: Creating xml In-Reply-To: References: Message-ID: Greg Lindstrom wrote: > It's been a while since I've played with XML using Python but I've been > asked to create XML using data from our postgres database. Currently we > are generating XML directly from the database using a set of stored > procedures but it is too slow (yes, I have numbers). I have been asked > to create a routine to generate the XML to see if it will be faster that > the sprocs (which I think it will be, based on other routines we have > running). But I digress... I write to a cStringIO.StringIO instance to create XML. Like this (pseudo-code): class Foo: def toxml(self, stream): print >> stream, "" If you're concerned about producing valid XML, you can write tests ;-) > Using my favorite search engine I see that there is a lot of material on > Python and XML. At the risk of starting something (which is not my > intention), what are my options if I want to create xml? Ceratinly > writing my own routine is an option, but I bet there are better ones :-) > > How about if I need/want to parse or process an XML file? Use lxml. It's blazingly fast and its XPath support makes parsing XML a snap. If you don't want to depend on anything not in the standard library, use the etree module. Code will be slightly longer because you don't have XPath support and performance will be only "ok". Not "super-fast", like with lxml. HTH -- Gerhard From suruti94 at gmail.com Thu Jul 23 17:45:41 2009 From: suruti94 at gmail.com (Mohan Parthasarathy) Date: Thu, 23 Jul 2009 14:45:41 -0700 Subject: Bridging Python and C Message-ID: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Hi, I am a newbie. It looks like there are quite a few ways to bridge Python and C. I have a bunch of C code and I just need Python wrappers for it. If i google for this I get SWIG, Boost etc. And I also see http://www.python.org/doc/2.5.2/ext/intro.html What is the recommended way for doing this ? thanks mohan -------------- next part -------------- An HTML attachment was scrubbed... URL: From jon at ffconsultancy.com Thu Jul 23 17:49:13 2009 From: jon at ffconsultancy.com (Jon Harrop) Date: Thu, 23 Jul 2009 22:49:13 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <72fb4a8d-b160-43c0-87ba-22a0d3768d5f@a37g2000prf.googlegroups.com> Message-ID: Raffael Cavallaro wrote: > Yes, you are missing the joke. The point is that if python is 60x > slower than C, even if there were not a GIL, it would require running > the python program on a 60 core machine just reach parity with C. The > existence of the GIL means that in reality you'd probably need a > several hundred core machine... No, OCaml is in the same boat as Python. Spawning a parallel work item takes 20,000x longer in OCaml than in F#. Gathering the results is asymptotically slower in general. Consequently, for most problems, Python or OCaml running on an infinite number of cores cannot beat F# running on 2 or more (and I already have 8) because the overheads are far too high for parallelism to pay off. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u From lists at cheimes.de Thu Jul 23 17:53:22 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 23 Jul 2009 23:53:22 +0200 Subject: Bridging Python and C In-Reply-To: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> References: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Message-ID: Mohan Parthasarathy wrote: > Hi, > I am a newbie. It looks like there are quite a few ways to bridge Python and > C. I have a bunch of C code and I just need Python wrappers for it. If i > google for this I get SWIG, Boost etc. And I also see > > http://www.python.org/doc/2.5.2/ext/intro.html > > What is the recommended way for doing this ? I recommend Cython (http://cython.org/). I'm using it on a daily bases to wrap C libraries. Christian From philip at semanchuk.com Thu Jul 23 17:54:33 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Thu, 23 Jul 2009 17:54:33 -0400 Subject: Bridging Python and C In-Reply-To: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> References: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Message-ID: <12D9D99B-6730-4534-BA61-631A4B9B27FE@semanchuk.com> On Jul 23, 2009, at 5:45 PM, Mohan Parthasarathy wrote: > Hi, > I am a newbie. It looks like there are quite a few ways to bridge > Python and > C. I have a bunch of C code and I just need Python wrappers for it. > If i > google for this I get SWIG, Boost etc. And I also see > > http://www.python.org/doc/2.5.2/ext/intro.html > > What is the recommended way for doing this ? "Recommended" depends on what you want to accomplish. SWIG, Boost, etc. all have pros and cons. One option you didn't mention is ctypes which is part of the Python standard library. It allows you to pass params to C functions. Works great for me! HTH Philip From cmpython at gmail.com Thu Jul 23 17:54:34 2009 From: cmpython at gmail.com (Che M) Date: Thu, 23 Jul 2009 14:54:34 -0700 (PDT) Subject: sqlite3 performance problems only in python References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: On Jul 23, 3:58?pm, Stef Mientki wrote: > Piet van Oostrum wrote: > >>>>>> Stef Mientki (SM) wrote: > > >> SM> btw, I don't know if it's of any importance, the SQL-statement I perform is > >> SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* > >> SM> ?from OPNAMEN > >> SM> ? ?inner join POID_VLID ? ? ? ? ?on OPNAMEN.POID ? ? ? ? ? ?= POID_VLID.POID > >> SM> ? ?inner join VRAAGLST ? ? ? ? ? on VRAAGLST.VLID ? ? ? ? ? = POID_VLID.VLID > >> SM> ? ?inner join VLID_SSID ? ? ? ? ?on VRAAGLST.VLID ? ? ? ? ? = VLID_SSID.VLID > >> SM> ? ?inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID > >> SM> ? ?inner join POID_SSID_SCID ? ? on ( OPNAMEN.POID ? ? ? ? ? ?= > >> SM> POID_SSID_SCID.POID ) and > >> SM> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ( SUBSCHAAL_GEGEVENS.SSID = > >> SM> POID_SSID_SCID.SSID ) > >> SM> ? ?inner join SCORES ? ? ? ? ? ? on SCORES.SCID ? ? ? ? ? ? = > >> SM> POID_SSID_SCID.SCID > >> SM> ? ?inner join PID_POID ? ? ? ? ? on OPNAMEN.POID ? ? ? ? ? ?= PID_POID.POID > >> SM> ? ?inner join PATIENT ? ? ? ? ? ?on PATIENT.PID ? ? ? ? ? ? = PID_POID.PID > >> SM> ?where substr ( lower( NAME) , 1, 6) ?= 'cis20r' > >> SM> ? ?and lower ( NAME_ ) = 'fatigue' > >> SM> ? ?and TEST_COUNT in (3,4) > >> SM> ? ?and DATETIME > 39814.0 > >> SM> ? ?and SCORE < 30 > > > 1) Do you have indices on the join fields? > > well I'm happily surprised, you came up with this suggestion > - I thought that sqlite created indexes on all primairy key and unique > fields > - but after explicitly creating the indices, a gained a speed of about a > factor 10 > After checking the database creation, it seemed I forgot to make these > fields the primary key > so thanks very much. > > I gained another factor of 10 speed by updating to version 2.5.5 of > pysqlite. > > cheers, > Stef > > > 2) Look at the ANALYZE command > > 3) Look at the EXPLAIN command > > You might want to consult the SQLite list for questions like this. Why do you use pysqlite? I just import sqlite3 in Python 2.5. What is the advantage of pysqlite? Che From longbotham at gmail.com Thu Jul 23 18:05:34 2009 From: longbotham at gmail.com (Nathan) Date: Thu, 23 Jul 2009 15:05:34 -0700 (PDT) Subject: netCDF4 usage issues Message-ID: <7c26c9b2-4560-4c16-a01a-68db6c206177@i6g2000yqj.googlegroups.com> I am having issues correctly implementing the multi-file read functionality in the Python module netCDF4 (whitaker - http://code.google.com/p/netcdf4-python/). I am a relative beginner to Python, so I may be missing something simple. I've done my best to follow the example in the documentation at the website referenced above (reprinted): >>> from netCDF4 import MFDataset >>> f = MFDataset('mftest*nc') >>> print f.variables['x'][:] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] >>> Where I attempt to follow the same flow, I don't get the full data set returned. I only get a data set for the first file in my list (see notes in the code below). >from netCDF4 import MFDataset >f = MFDataset('E*nc') # All files I want to read are .nc files in a single directory, each file starting with E >temp = f.variables['TEMP'][:] >temp.shape Out[17]: (8940, 150) #This is the exact shape of the TEMP variables array in the first file I need to read verified by an external netCDF reader application >f.file_format #There are two files I am trying to read in this example Out[33]: ['NETCDF3_CLASSIC', 'NETCDF3_CLASSIC'] Does the module only support netcdf4 files? If so, why is it getting data out of one of the files? I'm unsure how to trouble shoot this. Any suggestions would be appreciated. If this should be posted elsewhere, please direct me to another list/forum as I didn't find any directly related to netCDF or this specific netCDF module. Thank you! From http Thu Jul 23 18:06:22 2009 From: http (Paul Rubin) Date: 23 Jul 2009 15:06:22 -0700 Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: <7xk51zm4v5.fsf@ruckus.brouhaha.com> Nobody writes: > They also have the advantage that one thread can run while another is > waiting on disk I/O, which isn't something which can be done with a > select/poll interface (even if select/poll worked for files, it doesn't > help for mapped files). AIO can help with this, but I don't know any language runtimes that use it right now. Really, this (and the similar issue of ram cache misses) is basically why hardware hyperthreading exists too. To get processor parallelism you do have to use OS threads, but green threads are lighter weight and switch faster, so you want to use a combination of both. Basically, use about as many OS threads as you have hardware threads (i.e. CPU cores or hyperthreads), and then assign green threads to OS threads in your application runtime. From lists at cheimes.de Thu Jul 23 18:08:56 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 24 Jul 2009 00:08:56 +0200 Subject: Looking for os.listdir() generator In-Reply-To: References: Message-ID: Nick Craig-Wood wrote: > Christian Heimes wrote: >> I'm looking for a generator version of os.listdir() for Python 2.5 and >> newer. I know somebody has worked on it because I've seen a generator >> version in a posting on some list or blog a while ago. I can't find it >> anymore. It seems my Google fu is lacking today. All I can find is a >> very old version of xlistdir. A Cython based solution is appreciated but >> please no ctypes version. > > I posted exactly that yesterday I think ;-) > > Note that this has a python part as well as a ctypes part because the > version of ctypes I used doesn't support generators. I think the > development version does though. Thanks Nick! ctypes? I'm sure you wanted to say Cython :) If you don't mind I'm going to wrap it up into a nice package and release it on PyPI. Christian From myzoku at gmail.com Thu Jul 23 18:14:19 2009 From: myzoku at gmail.com (Gordon) Date: Thu, 23 Jul 2009 15:14:19 -0700 (PDT) Subject: Gedcom and Genealogy Message-ID: <5222a601-a128-4892-9a8d-e1942ac9ce4d@e27g2000yqm.googlegroups.com> We have many small libraries in JAVA or Ruby that need to be ported to Python. Actually it's so simple a rewrite is possible too. From dr.mtarver at ukonline.co.uk Thu Jul 23 18:20:04 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Thu, 23 Jul 2009 15:20:04 -0700 (PDT) Subject: strange python scripting error References: Message-ID: On 23 July, 18:01, Dennis Lee Bieber wrote: > On Thu, 23 Jul 2009 08:48:46 -0700 (PDT), Mark Tarver > declaimed the following in > gmane.comp.python.general: > > > I have a very strange error. ?I have two test python files test.py and > > python.py which contain the following code > > ? ? ? ? > > > The other fails with a server configuration error. ?Both are running > > ? ? ? ? Way out from left field... Have you tried renaming "python.py" to > some other name? > > ? ? ? ? If the server is configured to treat .py as executable files, it > might be getting confused over "python.py" trying to invoke "python". > -- > ? ? ? ? Wulfraed ? ? ? ?Dennis Lee Bieber ? ? ? ? ? ? ? KD6MOG > ? ? ? ? wlfr... at ix.netcom.com ? ? ? ? ? ? wulfr... at bestiaria.com > ? ? ? ? ? ? ? ? HTTP://wlfraed.home.netcom.com/ > ? ? ? ? (Bestiaria Support Staff: ? ? ? ? ? ? ? web-a... at bestiaria.com) > ? ? ? ? ? ? ? ? HTTP://www.bestiaria.com/ Yes; tried pyth.py - still failed. Mark From dr.mtarver at ukonline.co.uk Thu Jul 23 18:27:51 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Thu, 23 Jul 2009 15:27:51 -0700 (PDT) Subject: strange python scripting error References: Message-ID: <99dbbe65-adcd-4f06-aa8c-2d7fc25bf94c@s15g2000yqs.googlegroups.com> On 23 July, 18:01, Dennis Lee Bieber wrote: > On Thu, 23 Jul 2009 08:48:46 -0700 (PDT), Mark Tarver > declaimed the following in > gmane.comp.python.general: > > > The only hint at a difference I can see is that my ftp program says > > the files are of unequal lengths. ?test.py is 129 bytes long. > > python.py 134 bytes long. > > ? ? ? ? Just a guess... > > ? ? ? ? Line endings... vs > > -- > ? ? ? ? Wulfraed ? ? ? ?Dennis Lee Bieber ? ? ? ? ? ? ? KD6MOG > ? ? ? ? wlfr... at ix.netcom.com ? ? ? ? ? ? wulfr... at bestiaria.com > ? ? ? ? ? ? ? ? HTTP://wlfraed.home.netcom.com/ > ? ? ? ? (Bestiaria Support Staff: ? ? ? ? ? ? ? web-a... at bestiaria.com) > ? ? ? ? ? ? ? ? HTTP://www.bestiaria.com/ Is that linefeed + ctrl or what? I can't pick up any difference reading the files char by char in Lisp. How do you find the difference? Mark From stef.mientki at gmail.com Thu Jul 23 18:29:40 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Fri, 24 Jul 2009 00:29:40 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> Message-ID: <4A68E454.8040909@gmail.com> Che M wrote: > On Jul 23, 3:58 pm, Stef Mientki wrote: > >> Piet van Oostrum wrote: >> >>>>>>>> Stef Mientki (SM) wrote: >>>>>>>> >>>> SM> btw, I don't know if it's of any importance, the SQL-statement I perform is >>>> SM> select OPNAMEN.*, NAME, NAME_, SCORES.SCORE, PATIENT.* >>>> SM> from OPNAMEN >>>> SM> inner join POID_VLID on OPNAMEN.POID = POID_VLID.POID >>>> SM> inner join VRAAGLST on VRAAGLST.VLID = POID_VLID.VLID >>>> SM> inner join VLID_SSID on VRAAGLST.VLID = VLID_SSID.VLID >>>> SM> inner join SUBSCHAAL_GEGEVENS on SUBSCHAAL_GEGEVENS.SSID = VLID_SSID.SSID >>>> SM> inner join POID_SSID_SCID on ( OPNAMEN.POID = >>>> SM> POID_SSID_SCID.POID ) and >>>> SM> ( SUBSCHAAL_GEGEVENS.SSID = >>>> SM> POID_SSID_SCID.SSID ) >>>> SM> inner join SCORES on SCORES.SCID = >>>> SM> POID_SSID_SCID.SCID >>>> SM> inner join PID_POID on OPNAMEN.POID = PID_POID.POID >>>> SM> inner join PATIENT on PATIENT.PID = PID_POID.PID >>>> SM> where substr ( lower( NAME) , 1, 6) = 'cis20r' >>>> SM> and lower ( NAME_ ) = 'fatigue' >>>> SM> and TEST_COUNT in (3,4) >>>> SM> and DATETIME > 39814.0 >>>> SM> and SCORE < 30 >>>> >>> 1) Do you have indices on the join fields? >>> >> well I'm happily surprised, you came up with this suggestion >> - I thought that sqlite created indexes on all primairy key and unique >> fields >> - but after explicitly creating the indices, a gained a speed of about a >> factor 10 >> After checking the database creation, it seemed I forgot to make these >> fields the primary key >> so thanks very much. >> >> I gained another factor of 10 speed by updating to version 2.5.5 of >> pysqlite. >> >> cheers, >> Stef >> >> >>> 2) Look at the ANALYZE command >>> 3) Look at the EXPLAIN command >>> >> > > You might want to consult the SQLite list for questions like this. > thanks, but because the same SQL-statement in Delphi performed well, I thought it was a problem with the Python implementation. > Why do you use pysqlite? I just import sqlite3 in Python 2.5. > What is the advantage of pysqlite? > it's 10 .. 15 times faster then sqlite3 delivered with pyton 2.5. AFAIK it's nothing different, just a newer version. cheers, Stef > Che > From pavlovevidence at gmail.com Thu Jul 23 18:39:35 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 15:39:35 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: <36db2930-b4c9-4308-a267-a41703160580@h21g2000yqa.googlegroups.com> On Jul 23, 2:37?am, Nobody wrote: > On Wed, 22 Jul 2009 15:17:52 -0700, Carl Banks wrote: > > So do all these OSes have some kind of __mega_unifying_poll system > > call that works for anything that might possibly block, that you can > > exploit from a user process? > > Threads ;) Yeah, well that was kind of my point, you'd need native threads to do some of this. Jean-Paul Calderone seemed to be suggesting that it was possible to wait for events on sockets, pipes, and IPC semaphores at the same. I pointed out that it isn't possible with well-known I/O polling calls (like select), so there must be some other way, and I was wondering what it was. He's a proponent of Twisted, which is a major async framework, and I'd have to figure that once or twice they've encounted issues like "how to poll both a semaphore and a socket at the same time", so maybe they've found a good solution to it. Carl Banks From __peter__ at web.de Thu Jul 23 18:49:47 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 00:49:47 +0200 Subject: strange python scripting error References: <99dbbe65-adcd-4f06-aa8c-2d7fc25bf94c@s15g2000yqs.googlegroups.com> Message-ID: Mark Tarver wrote: > On 23 July, 18:01, Dennis Lee Bieber wrote: >> On Thu, 23 Jul 2009 08:48:46 -0700 (PDT), Mark Tarver >> declaimed the following in >> gmane.comp.python.general: >> >> > The only hint at a difference I can see is that my ftp program says >> > the files are of unequal lengths. test.py is 129 bytes long. >> > python.py 134 bytes long. >> >> Just a guess... >> >> Line endings... vs >> >> -- >> Wulfraed Dennis Lee Bieber KD6MOG >> wlfr... at ix.netcom.com wulfr... at bestiaria.com >> HTTP://wlfraed.home.netcom.com/ >> (Bestiaria Support Staff: web-a... at bestiaria.com) >> HTTP://www.bestiaria.com/ > > Is that linefeed + ctrl or what? I can't pick up any difference > reading the files char by char in Lisp. How do you find the > difference? carriage-return + linefeed That's the Windows convention for end-of-line markers. Unix uses linefeed only. If you are on Windows you have to open the file in binary mode to see the difference: >>> open("python.py", "rb").read() '#!/usr/bin/python\r\nprint "Content-type: text/html"\r\nprint\r\nprint""\r\nprint "
Hello, Linux.com!
"\r\nprint ""' >>> open("test.py", "rb").read() '#!/usr/bin/python\nprint "Content-type: text/html"\nprint\nprint ""\nprint "
Hello, Linux.com!
"\nprint ""' >>> \n denotes a newline (chr(10)) \r denotes a cr (chr(13)) You can fix the line endings with open(outfile, "wb").writelines(open(infile, "rU")) Here "U" denotes "universal newline" mode which recognizes "\r", "\n", and "\r\n" as line endings and translates all of these into "\n". "wb" means "write binary" which does no conversion. Therefore you'll end up with outfile following the Unix convention. Peter From lists at cheimes.de Thu Jul 23 18:54:40 2009 From: lists at cheimes.de (Christian Heimes) Date: Fri, 24 Jul 2009 00:54:40 +0200 Subject: sqlite3 performance problems only in python In-Reply-To: <4A684B56.3020906@gmail.com> References: <4A684B56.3020906@gmail.com> Message-ID: <4A68EA30.5060006@cheimes.de> Stef Mientki schrieb: > hello, > > until now I used only small / simple databases in Python with sqlite3. > Now I've a large and rather complex database. > > The most simple query (with just a result of 100 rows), > takes about 70 seconds. > And all that time is consumed in "cursor.fetchall" > > Using the same database in Delphi, > using the same query, > takes less than 5 seconds (including displaying the full table in a grid). Side note: Since you are coming from Delphi you are probably glad to hear that Firebird (formally known as Borland InterBase) is well supported. The kinterbasdb DBA provides a DBA 2.0 compatible interface plus lots of additional Firebird specific features. You might be better of using Firebird if you need large and complex databases. With Firebird you can then create a lower case index: CREATE INDEX idx_lower_name ON table COMPUTED BY (LOWER(name)); and use it to query the table for lower case names with: SELECT ... FROM table t WHERE LOWER(t.name) STARTING WITH LOWER(:query) or maybe: SELECT ... FROM table t WHERE t.name STARTING WITH LOWER(:query) PLAN JOIN (t INDEX(idx_lower_name)); Firebird 2.1 supports even more useful features to join on columns without falling back to a full table scan like UNICODE_CI. Christian From david at boddie.org.uk Thu Jul 23 18:58:19 2009 From: david at boddie.org.uk (David Boddie) Date: Fri, 24 Jul 2009 00:58:19 +0200 Subject: Pyserial and pyQt References: <11eb9b7a-3bf2-4f1e-92f1-fa9ba0cfe859@k30g2000yqf.googlegroups.com> <70e4ebae-908f-4f3a-a96a-6fa0a7a7887a@y19g2000yqy.googlegroups.com> Message-ID: On Thursday 23 July 2009 10:13, Dennis Lee Bieber wrote: > On Wed, 22 Jul 2009 10:32:51 -0700 (PDT), Seth > declaimed the following in gmane.comp.python.general: > >> Thanks for the response. I have gone through a lot of the tutorials. >> All of them(that I saw) seem to just deal will event-based >> applications ie calculator, image viewer, etc. How do I run pyserial >> in the background and pass the information to PyQT and refresh the >> screen? Is there a way to have pyserial run in another thread and >> pass the information to the UI? >> > So far as I've experienced (which isn't all that much), most all GUI > frameworks support some method of defining either: 1) a work routine > which will be called whenever the input event queue is empty -- work > routines should be coded to do a very short, fast, bit of processing and > return, so the event dispatcher can continue (collect one character at a > time, or one line using a non-blocking I/O operation; or use a thread to > collect and post via a Queue) (wx.EVT_IDLE [bind to your idle handler] & > wx.WakeUpIdle() [call from thread after posting to queue]); I think I can say that idle processing isn't a common idiom in PyQt applications. After all, you can't guarantee that your application will be idle or be able to use up idle time in a way that you can rely on, and perhaps it isn't a good situation to be in if your framework is using up CPU time by idling. > 2) a timed-event which the dispatcher can call at periodic intervals (use > a thread to collect lines from the serial port, post the lines via a > Queue, and have the timed event check for queued data) (wx.Timer() ); In PyQt, the easiest way to do this is with a timer, connecting its timeout() signal to a slot (method) to perform some processing. A widget class might be adapted to do this in the following way: class MyWindow(QWidget): def __init__(self): QWidget.__init__(self) # ... other initialisation ... timer = QTimer(self) timer.timeout.connect(self.processData) # PyQt 4.5 signal notation timer.start(100) # every 100 milliseconds def processData(self): # Do your processing here. There are various ways to use timers with PyQt, but this is a fairly simple pattern to use. > 3) a callback interface to the GUI event dispatcher which can be invoked > from a separate thread (wx.CallAfter() ) I think at this point I would recommend using a separate thread. The idiom for using threads with PyQt is quite simple, though it helps to go with the framework and use QThread instead of Python's thread class. > You'll have to track down the equivalent QT functions (I have to > confess, I found the above from "wxPython in Action"; my last real GUI > coding that needed such was nearly 20 years ago, using xt and DECWindows > for the interface, and GKS on X for the data drawing, emulating an > archaic Ramtek graphics engine command set. The idle/work routine > collected commands sent via VMS mailbox to the "display" and generated > GKS operations for them). The people demand screenshots! ;-) David From timro21 at gmail.com Thu Jul 23 19:02:23 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 16:02:23 -0700 (PDT) Subject: integer square roots Message-ID: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> I wish to process billions of 100-digit numbers and test if each has an integer square root. What's the most efficient way to do this? From davea at ieee.org Thu Jul 23 19:03:14 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 23 Jul 2009 19:03:14 -0400 Subject: strange python scripting error In-Reply-To: References: Message-ID: <4A68EC32.4090907@ieee.org> Mark Tarver wrote: > I have a very strange error. I have two test python files test.py and > python.py which contain the following code > > #!/usr/bin/python > print "Content-type: text/html" > print > print "" > print "
Hello, Linux.com!
" > print "" > > One file (test.py) works; you call it up and it shows a web page with > > Hello, Linux.com > > The other fails with a server configuration error. Both are running > under Linux, same server, same permissions. Running a character scan > shows that both files contain the same printable characters and are > therefore typographically identical. They are absolutely the same. > > The only hint at a difference I can see is that my ftp program says > the files are of unequal lengths. test.py is 129 bytes long. > python.py 134 bytes long. > > A zipped folder containing both files is at > > www.lambdassociates.org/weird.zip > > Any ideas welcome. > > Mark > > Easiest explanation is that python.py has Windows-style newlines. In other words, each line ends with 0d0a, rather than the Unix convention of 0a. If your server is Unix-based, it can't handle that first line, since it has an illegal character (0d) following the #!/usr/bin/python line. Convert it to Unix line-endings. DaveA From gagsl-py2 at yahoo.com.ar Thu Jul 23 19:14:07 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 23 Jul 2009 20:14:07 -0300 Subject: Detect target name in descriptor __set__ method References: <74cd3059-cd88-418b-8444-766a125d93c3@y4g2000prf.googlegroups.com> Message-ID: En Thu, 23 Jul 2009 10:19:33 -0300, DG escribi?: > On Jul 22, 6:05?pm, "Gabriel Genellina" > wrote: >> En Wed, 22 Jul 2009 11:01:09 -0300, Rhodri James ? >> escribi?: >> >> > On Wed, 22 Jul 2009 06:02:55 +0100, Gabriel Genellina ? >> > wrote: >> >> >> class X(object): >> >> ? ?foo = descriptor() >> >> >> x = X() >> >> x.foo = "value" >> > You might've already thought of this (and it is annoying), but you > could pass the name through the descriptor's init method. I believe > this is the only way besides assigning a metaclass that will look for > that type of descriptor upon class creation and set the descriptor's > name at that time. > > class A(object): > def __init__(self, attr_name): > self._name = attr_name > def __set__(self, instance, value): > self.instance.__dict__[self._name] = value > # or something like that... > > class B(object): > foo = A('foo') Thanks, this seems to be the less "magical" solution. I don't like having to repeat the attribute name, but nothing is perfect... And thanks to Rainer Mansfeld too; looking up the matching attribute may be useful in other cases. -- Gabriel Genellina From pavlovevidence at gmail.com Thu Jul 23 19:30:54 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 16:30:54 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> <7xws60p7kk.fsf@ruckus.brouhaha.com> Message-ID: <6a3f1211-b551-4012-904c-0ed0a2c9f6f8@o32g2000yqm.googlegroups.com> On Jul 22, 5:27?pm, Paul Rubin wrote: > Carl Banks writes: > > I don't think your fantasy async-only all-green-thread langauge > > implementation is possible anyway. > > Erlang and GHC both work like that, quite successfully: > > ?http://shootout.alioth.debian.org/gp4/benchmark.php?test=threadring&l... I am not suggesting that you can't do a lot of things with async I/O. But can you do *everything* that native threads can do? I don't think so. So when you asked: "Why is that [native threading] such an advantage? Green threads work fine if you just organize the i/o system to never block." Well, the answer is they don't always work fine. Just organizing the I/O system never to block won't cut it, since things other than I/O block. For green threads to work fine all the time you have to be able organize your entire system never to block, and I don't think it's possible on the common OSes in use today. Until that happens, there will always be advantages to being able to use native threads on a single core. > > How would you wait on a pipe in one thread, a socket in another, a > > semaphore in a third? ? > > You can select on pipes and sockets, I think. ?Not sure about > semaphores. ? So you made a claim that "green threads work fine", and rhetorically suggested that native threads had no advantages, yet you're not even 100% sure you can select on both pipes and sockets (both common forms of I/O), let alone semaphores? [You can select on sockets and pipes in Unix, not in Windows, although on Windows I think there are other ways to do it besides the select call.] > > (Are there any popular OSes that offer a unified polling interface to > > all possible synchronizations?) ?And what do you do about drivers or > > libraries that make underlying blocking calls? ?What if you have a > > busy calculation going on in the background? > > I don't think the concept of "drivers" applies to user-mode programs. > For FFI calls you would use an OS thread. That's contrary to the hypothesis, isn't it? >?The language runtime > switches between busy computation threads on a timer tick. This would seem to result in a tradeoff between performance and low- latency. Well that happens at the OS-level too but the OS can respond to interrupts and task switch between timer ticks at finer intervals. Back in my DOS days I wrote user programs that responded to I/O interrupts, but it doesn't seem to be common practice in modern OSes to easily allow this. And Paul, if I'm being a little hard on you here, it's not that I'm taking issue with your own claim so much as with your dismissal of mine. Carl Banks From scriptlearner at gmail.com Thu Jul 23 19:39:29 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Thu, 23 Jul 2009 16:39:29 -0700 (PDT) Subject: passing data to a liburl2 opener object Message-ID: <89de6563-1016-461b-87b6-090754ca224b@r2g2000yqm.googlegroups.com> I have prepared my headers and data for a HTTP POST message; however, I am not sure how to pass the data to the opener. Can you guys provide some suggestions? Thanks. proxy_handler = urllib2.ProxyHandler({'http': 'http://my.proxy.com: 3128/'}) opener = urllib2.build_opener(proxy_handler) url = "http://sample.company.com:9999/service?" headers['Content-Type'] = 'multipart/form-data; boundary=%s' % myBoundar headers['Cookie'] = 'somevalidcookiecontents' #there is an opener.addheaders method for adding headers data = multipart_encode(myDict) #how to pass data to the opener??? With the codes above, I need to send out a HTTP POST like the following one: POST http://sample.company.com:9999/service? Content-Type: multipart/form-data; boundary=---------------------------1234 ---------------------------1234 Content-Disposition: form-data; name="picture" PICTURE contents here ---------------------------1234 Content-Disposition: form-data; name="id" whateverid ---------------------------1234-- From shai at platonix.com Thu Jul 23 19:51:43 2009 From: shai at platonix.com (Shai) Date: Thu, 23 Jul 2009 16:51:43 -0700 (PDT) Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <8e71092f-d57b-4784-8874-3dd19bd5c06b@d32g2000yqh.googlegroups.com> <87ljmogglz.fsf@benfinney.id.au> <02701ee5$0$5185$c3e8da3@news.astraweb.com> Message-ID: <056f629b-aa63-458a-ae16-ac40a759e446@h11g2000yqb.googlegroups.com> On Jul 17, 10:52?am, Steven D'Aprano wrote: > > When the decorator is called, the function object is just a function > object, not a method, so there is no concept of "what class it is > destined for". > ... which points to the better solution: use a descriptor. With the doc_inherit decorator defined below, one may write class Foo(object): def foo(self): "Frobber" pass class Bar(Foo): @doc_inherit def foo(self): pass and it appears to work. The code below is a little longish because we need to do slightly different things when called for a class and for an instance. But there's no need to repeat the parent name, no need to look into namespaces (which, as you said, is probably messy and fragile), and it seems pretty readable, too. from functools import wraps class DocInherit(object): """ Docstring inheriting method descriptor The class itself is also used as a decorator """ def __init__(self, mthd): self.mthd = mthd self.name = mthd.__name__ def __get__(self, obj, cls): if obj: return self.get_with_inst(obj, cls) else: return self.get_no_inst(cls) def get_with_inst(self, obj, cls): overridden = getattr(super(cls, obj), self.name, None) @wraps(self.mthd, assigned=('__name__','__module__')) def f(*args, **kwargs): return self.mthd(obj, *args, **kwargs) return self.use_parent_doc(f, overridden) def get_no_inst(self, cls): for parent in cls.__mro__[1:]: overridden = getattr(parent, self.name, None) if overridden: break @wraps(self.mthd, assigned=('__name__','__module__')) def f(*args, **kwargs): return self.mthd(*args, **kwargs) return self.use_parent_doc(f, overridden) def use_parent_doc(self, func, source): if source is None: raise NameError, ("Can't find '%s' in parents"%self.name) func.__doc__ = source.__doc__ return func doc_inherit = DocInherit Combining docstrings (as suggested by Jean-Paul Calderone), and taking proper care of classmethods and staticmethods, are left as an exercise to the reader. Have fun, Shai. From davea at ieee.org Thu Jul 23 19:52:22 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 23 Jul 2009 19:52:22 -0400 Subject: Bridging Python and C In-Reply-To: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> References: <89dd3da60907231445r9dc4bd5jad1dbbdcfb490032@mail.gmail.com> Message-ID: <4A68F7B6.2020101@ieee.org> Mohan Parthasarathy wrote: > Hi, > I am a newbie. It looks like there are quite a few ways to bridge Python and > C. I have a bunch of C code and I just need Python wrappers for it. If i > google for this I get SWIG, Boost etc. And I also see > > http://www.python.org/doc/2.5.2/ext/intro.html > > What is the recommended way for doing this ? > > thanks > mohan > > I think the real question to ask is what form is your C code in. If you're in Windows, and it's compiled into DLL's with documented exports, then I'd start by using ctypes to access a few of the exports. If that seems too hard, you might try SWIG or another automated tool, to parse the headers and generate wrappers. But if you haven't done it first by hand, you might find the automated tools very hard to figure out. DaveA From http Thu Jul 23 19:57:10 2009 From: http (Paul Rubin) Date: 23 Jul 2009 16:57:10 -0700 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> <7xws60p7kk.fsf@ruckus.brouhaha.com> <6a3f1211-b551-4012-904c-0ed0a2c9f6f8@o32g2000yqm.googlegroups.com> Message-ID: <7x8wifymuh.fsf@ruckus.brouhaha.com> Carl Banks writes: > > I don't think the concept of "drivers" applies to user-mode programs. > > For FFI calls you would use an OS thread. > That's contrary to the hypothesis, isn't it? Yeah, it would have been better to say, green threads are ok for most typical forms of i/o concurrency, but OS threads are still necessary for some things. An FFI call in particular isn't exactly under the control of your language's runtime system; it has to be treated more like an external program. OS thread?switching is much more expensive than green thread switching, so it's still preferable to use green threads when possible. > >?The language runtime switches between busy computation threads on > >?a timer tick. > This would seem to result in a tradeoff between performance and low- > latency. GHC appears to use a 100 hz timer, which I'd expect to be fast enough for interactive applications while still not causing too much cpu load. I don't know if it has a way to assign priorities to microthreads (e.g. if you have one running your program's GUI) though. I don't think either Python or GHC can implement hard-real-time latency because they both use stop-the-world garbage collection. E.g. in Python, releasing the last reference to a large list or dict can take a long time. > And Paul, if I'm being a little hard on you here, it's not that I'm > taking issue with your own claim so much as with your dismissal of > mine. Well, the issue was why Python uses native threads pervasively. As far as I can tell, it's just an implementation artifact that has no really compelling justification. From mensanator at aol.com Thu Jul 23 20:11:54 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 23 Jul 2009 17:11:54 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> Message-ID: <73b8529d-23f4-41b4-a6cd-9f191f74bcd4@y19g2000yqy.googlegroups.com> On Jul 23, 6:02?pm, timro21 wrote: > I wish to process billions of 100-digit numbers and test if each has > an integer square root. ?What's the most efficient way to do this? Use gmpy. >>> import gmpy >>> help(gmpy.sqrt) Help on built-in function sqrt in module gmpy: sqrt(...) sqrt(x): returns the integer, truncated square root of x, i.e. the largest y such that x>=y*y. x must be an mpz, or else gets coerced to one; further, x must be >= 0. >>> p = 10**100 >>> p1 = gmpy.next_prime(p) >>> p1 mpz (10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000267) >>> gmpy.sqrt(p1) mpz(100000000000000000000000000000000000000000000000000) From mensanator at aol.com Thu Jul 23 20:17:53 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 23 Jul 2009 17:17:53 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <73b8529d-23f4-41b4-a6cd-9f191f74bcd4@y19g2000yqy.googlegroups.com> Message-ID: On Jul 23, 7:11?pm, Mensanator wrote: > On Jul 23, 6:02?pm, timro21 wrote: > > > I wish to process billions of 100-digit numbers and test if each has > > an integer square root. ?What's the most efficient way to do this? > > Use gmpy. > > >>> import gmpy > >>> help(gmpy.sqrt) > > Help on built-in function sqrt in module gmpy: > > sqrt(...) > ? ? sqrt(x): returns the integer, truncated square root of x, i.e. the > ? ? largest y such that x>=y*y. x must be an mpz, or else gets coerced > ? ? to one; further, x must be >= 0. > > >>> p = 10**100 > >>> p1 = gmpy.next_prime(p) > >>> p1 > > mpz > (10000000000000000000000000000000000000000000000000000000000000000000000000?000000000000000000000000267) > > >>> gmpy.sqrt(p1) > > mpz(100000000000000000000000000000000000000000000000000) Duh. I completely misread this. I thought you wanted the square root, not IF it had a square root that's an integer. Sorry about that. Try this, instead: >>> gmpy.sqrtrem(p1) (mpz(100000000000000000000000000000000000000000000000000), mpz(267)) It has a non-zero remainder, so it's not an integer square root. From aahz at pythoncraft.com Thu Jul 23 20:25:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 23 Jul 2009 17:25:16 -0700 Subject: Help understanding the decisions *behind* python? References: <534b1c540907200927g4cb7011bpe58249d2517d1b5d@mail.gmail.com> Message-ID: In article , Mark Lawrence wrote: > >Sorry if this has been discussed and I've missed it, but how about >memory allocation. An immutable tuple has a fixed memory allocation >whereas that for the mutable list must be liable to change. You might >like to look at the recent thread on this ng 'List insertion cost' and >follow the links to Raymond Hettinger's power point presentation. Not only that, but lists use more memory, period, for any given number of elements because the overallocation permits amortized constant time for appends. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From aahz at pythoncraft.com Thu Jul 23 20:27:35 2009 From: aahz at pythoncraft.com (Aahz) Date: 23 Jul 2009 17:27:35 -0700 Subject: invoke method on many instances References: Message-ID: In article , Gabriel Genellina wrote: > >NLMPI What? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From mensanator at aol.com Thu Jul 23 20:30:34 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 23 Jul 2009 17:30:34 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <73b8529d-23f4-41b4-a6cd-9f191f74bcd4@y19g2000yqy.googlegroups.com> Message-ID: On Jul 23, 7:17?pm, Mensanator wrote: > On Jul 23, 7:11?pm, Mensanator wrote: > > > > > > > On Jul 23, 6:02?pm, timro21 wrote: > > > > I wish to process billions of 100-digit numbers and test if each has > > > an integer square root. ?What's the most efficient way to do this? > > > Use gmpy. > > > >>> import gmpy > > >>> help(gmpy.sqrt) > > > Help on built-in function sqrt in module gmpy: > > > sqrt(...) > > ? ? sqrt(x): returns the integer, truncated square root of x, i.e. the > > ? ? largest y such that x>=y*y. x must be an mpz, or else gets coerced > > ? ? to one; further, x must be >= 0. > > > >>> p = 10**100 > > >>> p1 = gmpy.next_prime(p) > > >>> p1 > > > mpz > > (10000000000000000000000000000000000000000000000000000000000000000000000000??000000000000000000000000267) > > > >>> gmpy.sqrt(p1) > > > mpz(100000000000000000000000000000000000000000000000000) > > Duh. I completely misread this. I thought you wanted the square root, > not IF it had a square root that's an integer. > > Sorry about that. > > Try this, instead: > > >>> gmpy.sqrtrem(p1) > > (mpz(100000000000000000000000000000000000000000000000000), mpz(267)) > > It has a non-zero remainder, so it's not an integer square root. There's also is_square(...) is_square(x): returns 1 if x is a perfect square, else 0. x must be an mpz, or else gets coerced to one. From http Thu Jul 23 20:35:27 2009 From: http (Paul Rubin) Date: 23 Jul 2009 17:35:27 -0700 Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> Message-ID: <7x4ot2zzn4.fsf@ruckus.brouhaha.com> timro21 writes: > I wish to process billions of 100-digit numbers and test if each has > an integer square root. What's the most efficient way to do this? Is this a homework problem? If not, may I ask what the application is? I think the basic answer is 1) use the law of quadratic reciprocity to eliminate a lot of the inputs quickly (for example, just looking at the low 8 bits of an input number should eliminate about 5/6th of them); 2) use GMP and Newton's method to check the rest. http://cr.yp.to/papers/powers.pdf has some advice about how to do that. sci.crypt might be another good place to ask this question. From songofacandy at gmail.com Thu Jul 23 20:49:26 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Thu, 23 Jul 2009 17:49:26 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> Message-ID: <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> > What is the encoding of sys.stderr in your example? Sorry, I missed. It is cp932 > So the problem is essentially this: if a stream has an encoding > attribute, sometimes it is a wrapped stream which encodes Unicode > (e.g. a stream obtained via the codecs module) and sometimes it is not > (e.g. sys.stdout, sys.stderr). Yes! I confused by it. >> The encoding that this file uses. When Unicode strings are written to a file, >> they will be converted to byte strings using this encoding. In addition, >> when the file is connected to a terminal, the attribute gives the encoding >> that the terminal is likely to use I feel this doc means "file object with encoding attribute encodes unicode regardless it is tty or not" and sys.stdout/stderr defies convention. If this doc means "file object encodes unicode if it isn't tty.", I should write like below:: if not afile.isatty(): if getattr(afile, "encoding") is not None: afile.write(unicode_str) elif getattr(afile, "encoding") is not None: afile.write(unicode_str.encode(afile.encoding)) else: afile.write(unicode_str.encode(fallback_encoding)) # utf8, defaultencoding, preferedencoding, ... "Writing unicode to a file(-like)" is a simple requirement. Does python have any simple resolution for it? From rui.maciel at gmail.com Thu Jul 23 20:52:00 2009 From: rui.maciel at gmail.com (Rui Maciel) Date: Fri, 24 Jul 2009 01:52 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> Message-ID: <4a6905b0$0$27110$a729d347@news.telepac.pt> fft1976 wrote: > How do you explain that something as inferior as Python beat Lisp in > the market place despite starting 40 years later. Probably due to similar reasons that lead php to become remotely relevant. Rui Maciel From timro21 at gmail.com Thu Jul 23 21:00:20 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 18:00:20 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> Message-ID: On Jul 24, 10:35?am, Paul Rubin wrote: > timro21 writes: > > I wish to process billions of 100-digit numbers and test if each has > > an integer square root. ?What's the most efficient way to do this? > > Is this a homework problem? ?If not, may I ask what the application is? > > I think the basic answer is 1) use the law of quadratic reciprocity to > eliminate a lot of the inputs quickly (for example, just looking at > the low 8 bits of an input number should eliminate about 5/6th of > them); 2) use GMP and Newton's method to check the rest.http://cr.yp.to/papers/powers.pdfhas some advice about how to do > that. > > sci.crypt might be another good place to ask this question. Homework? Gosh no. I have several number theory applications which need to know (quickly) whether particular very large numbers are perfect squares. Since I asked this in this newsgroup I guess I assumed that responses wuld relate specifically to how to do this efficiently *and accurately* in Python. Sorry if I wasn't clear. From dstanek at dstanek.com Thu Jul 23 21:23:06 2009 From: dstanek at dstanek.com (David Stanek) Date: Thu, 23 Jul 2009 21:23:06 -0400 Subject: sqlite3 performance problems only in python In-Reply-To: <4A68E454.8040909@gmail.com> References: <4A684B56.3020906@gmail.com> <4A685A12.4080800@tim.thechases.com> <4A68E454.8040909@gmail.com> Message-ID: On Thu, Jul 23, 2009 at 6:29 PM, Stef Mientki wrote: > > but because the same SQL-statement in Delphi performed well, > I thought it was a problem with the Python implementation. Same SQL, but were you also using Sqlite in Delphi? -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From http Thu Jul 23 21:51:01 2009 From: http (Paul Rubin) Date: 23 Jul 2009 18:51:01 -0700 Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> Message-ID: <7xzlaudf22.fsf@ruckus.brouhaha.com> timro21 writes: > Homework? Gosh no. I have several number theory applications which > need to know (quickly) whether particular very large numbers are > perfect squares. Since I asked this in this newsgroup I guess I > assumed that responses wuld relate specifically to how to do this > efficiently *and accurately* in Python. Sorry if I wasn't clear. comp.lang.python is a good place to get answers about Python. It's probably not such a good source of answers about computational number theory. Also, Python is more about productivity than speed, so answers involving Python may not be the most efficient possible answers. One obvious point though is that GMP/gmpy is pretty simple to use from Python and will be several times faster than Python's built in longs. Also, as Mensanator pointed out, GMP has built-in functions that will help you with precise checks (I'd forgotten or not known about them). I still think you'd get a speedup from first filtering out obviously non-square candidates before resorting to multi-precision arithmetic. Some other fairly simple optimization may be possible too. I asked about the application mainly because I wondered if you had realtime requirements, whether you were going to have to keep doing this problem with new numbers, etc. If you just have a 1-off task of checking a few billion numbers, you could probably do it in a few days with Python on a workstation using some fairly simple code. If you want to check billions of numbers per minute, or if what you really want is to check trillions of numbers rather than billions, then it's worth pursuing fancier methods. Another issue is the source of the numbers. E.g. maybe there is a way to program a graphics accelerator to turn a number into a list of residues mod a bunch of small primes in parallel and get a fast, near-certain test if the inputs are random, but that approach could be fooled if the numbers were concocted by a possible adversary. From casevh at gmail.com Thu Jul 23 22:04:50 2009 From: casevh at gmail.com (casevh) Date: Thu, 23 Jul 2009 19:04:50 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> Message-ID: > comp.lang.python is a good place to get answers about Python. ?It's > probably not such a good source of answers about computational number > theory. ?Also, Python is more about productivity than speed, so > answers involving Python may not be the most efficient possible > answers. ?One obvious point though is that GMP/gmpy is pretty simple > to use from Python and will be several times faster than Python's > built in longs. ?Also, as Mensanator pointed out, GMP has built-in > functions that will help you with precise checks (I'd forgotten or not > known about them). ?I still think you'd get a speedup from first > filtering out obviously non-square candidates before resorting to > multi-precision arithmetic. ?Some other fairly simple optimization may > be possible too. > gmpy.is_square() is quite fast. On a older 32-bit Linux box, it can test approximately 400,000 100-digits numbers per second. The time includes conversion from a string. If the numbers are already Python longs, it can check 800,000 per second. Checking a billion is not unreasonable. casevh From http Thu Jul 23 22:48:12 2009 From: http (Paul Rubin) Date: 23 Jul 2009 19:48:12 -0700 Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> Message-ID: <7xocra7q4z.fsf@ruckus.brouhaha.com> casevh writes: > gmpy.is_square() is quite fast. On a older 32-bit Linux box, it can > test approximately 400,000 100-digits numbers per second. The time > includes conversion from a string. If the numbers are already Python > longs, it can check 800,000 per second. Checking a billion is not > unreasonable. Wow, that is pretty impressive. A look at the code shows it uses quite a few optimizations: http://sage.math.washington.edu/home/novoselt/gmp-4.2.4/mpn/perfsqr.c From songofacandy at gmail.com Thu Jul 23 23:10:10 2009 From: songofacandy at gmail.com (Naoki INADA) Date: Thu, 23 Jul 2009 20:10:10 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: > > Yes! I confused by it. s/I confused/I am confused/ > "Writing unicode to a file(-like)" is a simple requirement. > Does python have any simple resolution for it? s/resolution/solution/ Sorry about my poor English. From pavlovevidence at gmail.com Thu Jul 23 23:11:23 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 20:11:23 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <1cethsrrw8h6k$.9ty7j7u7zovn.dlg@40tude.net> <8bbba0b8-073b-4a43-8954-d5fe7b2f40ee@o15g2000yqm.googlegroups.com> <4e05ee59-db96-4bfc-9828-b11374446ac6@e18g2000vbe.googlegroups.com> <7xskgobpnv.fsf@ruckus.brouhaha.com> <5993d986-cda2-4dd3-b288-69467a68f949@o6g2000yqj.googlegroups.com> <7xfxco35g8.fsf@ruckus.brouhaha.com> <7xws60p7kk.fsf@ruckus.brouhaha.com> <6a3f1211-b551-4012-904c-0ed0a2c9f6f8@o32g2000yqm.googlegroups.com> <7x8wifymuh.fsf@ruckus.brouhaha.com> Message-ID: On Jul 23, 4:57?pm, Paul Rubin wrote: > Carl Banks writes: > > > I don't think the concept of "drivers" applies to user-mode programs. > > > For FFI calls you would use an OS thread. > > That's contrary to the hypothesis, isn't it? > > Yeah, it would have been better to say, green threads are ok for most > typical forms of i/o concurrency, but OS threads are still necessary > for some things. ?An FFI call in particular isn't exactly under the > control of your language's runtime system; it has to be treated > more like an external program. > > OS thread?switching is much more expensive than green thread > switching, so it's still preferable to use green threads when > possible. That is reasonable. Thank you. > > And Paul, if I'm being a little hard on you here, it's not that I'm > > taking issue with your own claim so much as with your dismissal of > > mine. > > Well, the issue was why Python uses native threads pervasively. ?As > far as I can tell, it's just an implementation artifact that has no > really compelling justification. Probably for this reason: "Because then they didn't have to arrange for the I/O to never block." Carl Banks From timro21 at gmail.com Thu Jul 23 23:30:25 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 20:30:25 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> Message-ID: <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Thanks to all! Tim From pavlovevidence at gmail.com Thu Jul 23 23:51:02 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 23 Jul 2009 20:51:02 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On Jul 23, 5:52?pm, Rui Maciel wrote: > fft1976 wrote: > > How do you explain that something as inferior as Python beat Lisp in > > the market place despite starting 40 years later. > > Probably due to similar reasons that lead php to become remotely relevant. Well, the only reason PHP became relevant because it was an easy to deploy solution in a single application domain, the web, that happened to explode. The same thing happened to Python. Oh, wait, no it didn't. Carl Banks From tundra at tundraware.com Thu Jul 23 23:51:26 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Thu, 23 Jul 2009 22:51:26 -0500 Subject: integer square roots In-Reply-To: <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Message-ID: timro21 wrote: > Thanks to all! > > Tim While we're at it, would you mind saying more about what exactly you're doing - Inquiring Math Dorks (tm) want to know ... -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From icebergwtf at gmail.com Thu Jul 23 23:58:04 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Fri, 24 Jul 2009 11:58:04 +0800 Subject: extract c/cpp include file with regular expression In-Reply-To: References: Message-ID: <4314c1f70907232058t4d295a25t86ba11d90e15001d@mail.gmail.com> Thanks, Philip Semanchuk and Nick Craig-Wood! My goal is gather informations from cpp sources and generate cross referenced html files (as more detail as fine) So I think " missing some unusual cases" might be acceptable, but informations you introduced make me more interests to dig things out :) I know some programs like doxygen do such things, but I can learn much by doing this than simply write configuration files. cheers! tiefeng wu 2009-07-24 From rurpy at yahoo.com Fri Jul 24 00:19:52 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 23 Jul 2009 21:19:52 -0700 (PDT) Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: Nick Dumas wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Agreed. Two string.split()s, first at the semi-colon and then at the > equal sign, will yield you your value, without having to fool around > with regexes. > > On 7/23/2009 9:23 AM, Mark Lawrence wrote: >> scriptlearner at gmail.com wrote: >>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >>> will like to take out the values (valuea, valueb, and valuec). How do >>> I do that in Python? The group method will only return the matched >>> part. Thanks. >>> >>> p = re.compile('#a=*;b=*;c=*;') >>> m = p.match(line) >>> if m: >>> print m.group(), >> >> IMHO a regex for this is overkill, a combination of string methods such >> as split and find should suffice. You're saying that something like the following is better than the simple regex used by the OP? [untested] values = [] parts = line.split(';') if len(parts) != 4: raise SomeError() for p, expected in zip (parts[-1], ('#a','b','c')): name, x, value = p.partition ('=') if name != expected or x != '=': raise SomeError() values.append (value) print values[0], values[1], values[2] Blech, not in my book. The regex checks the format of the string, extracts the values, and does so very clearly. Further, it is easily adapted to other similar formats, or evolutionary changes in format. It is also (once one is familiar with regexes -- a useful skill outside of Python too) easier to get right (at least in a simple case like this.) The only reason I can think of to prefer a split-based solution is if this code were performance-critical in that I would expect the split code to be faster (although I don't know that for sure.) This is a perfectly fine use of a regex. From d at vidr.cc Fri Jul 24 00:27:10 2009 From: d at vidr.cc (David Roberts) Date: Fri, 24 Jul 2009 14:27:10 +1000 Subject: OverflowError in RLock.acquire() Message-ID: Hi, I'm trying to port a Python application to Windows, and I'm getting the following error (which did not occur when running on Linux): Exception in thread Thread-4: Traceback (most recent call last): File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner self.run() File "C:\Documents and Settings\David\My Documents\pyzui\pyzui\tileprovider.py", line 97, in run self.__tilecache[tile_id] = Tile(tile) File "C:\Documents and Settings\David\My Documents\pyzui\pyzui\tilecache.py", line 165, in __setitem__ with self.__lock: File "C:\Python26\lib\threading.py", line 115, in acquire me = current_thread() File "C:\Python26\lib\threading.py", line 803, in currentThread return _active[_get_ident()] OverflowError: can't convert negative value to unsigned long Where __lock is an RLock object. The error only occurs for a single class (which is derived from the TileProvider class in tileprovider.py, which in turn is derived from threading.Thread), which would lead me to believe that there's an error in my code, but the traceback doesn't help much, and I haven't been able to find any similar problems with google. Any help would be appreciated. -- David Roberts http://da.vidr.cc/ From d at vidr.cc Fri Jul 24 00:41:01 2009 From: d at vidr.cc (David Roberts) Date: Fri, 24 Jul 2009 14:41:01 +1000 Subject: OverflowError in RLock.acquire() In-Reply-To: References: Message-ID: I forgot to mention, Python version is 2.6.2. -- David Roberts http://da.vidr.cc/ On Fri, Jul 24, 2009 at 14:27, David Roberts wrote: > Hi, > > I'm trying to port a Python application to Windows, and I'm getting > the following error (which did not occur when running on Linux): > > Exception in thread Thread-4: > Traceback (most recent call last): > ?File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner > ? ?self.run() > ?File "C:\Documents and Settings\David\My > Documents\pyzui\pyzui\tileprovider.py", line 97, in run > ? ?self.__tilecache[tile_id] = Tile(tile) > ?File "C:\Documents and Settings\David\My > Documents\pyzui\pyzui\tilecache.py", line 165, in __setitem__ > ? ?with self.__lock: > ?File "C:\Python26\lib\threading.py", line 115, in acquire > ? ?me = current_thread() > ?File "C:\Python26\lib\threading.py", line 803, in currentThread > ? ?return _active[_get_ident()] > OverflowError: can't convert negative value to unsigned long > > Where __lock is an RLock object. > > The error only occurs for a single class (which is derived from the > TileProvider class in tileprovider.py, which in turn is derived from > threading.Thread), which would lead me to believe that there's an > error in my code, but the traceback doesn't help much, and I haven't > been able to find any similar problems with google. > > Any help would be appreciated. > > -- > David Roberts > http://da.vidr.cc/ > From timro21 at gmail.com Fri Jul 24 00:43:48 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 21:43:48 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Message-ID: Hi Tim, sure, I'm looking at the perfect cuboid problem. I've just spent a couple of very frustrating hours. Given that I'm in my 50's but have the brain of a retarded 3-year-old, can someone please explain what I have to do to download gmpy to use with ActivePython 2.6 on a Windows system? I downloaded (or so I thought) GMPY 1.04 from http://code.google.com/p/gmpy/ but all the files look way too small...and then how do I import it into a Python program...I'm sure I could do this if it wasn't for my extreme stupidity... From casevh at gmail.com Fri Jul 24 01:12:23 2009 From: casevh at gmail.com (casevh) Date: Thu, 23 Jul 2009 22:12:23 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> Message-ID: <0fc47aab-eb53-4e0a-be6d-9736883db888@i8g2000pro.googlegroups.com> On Jul 23, 9:43?pm, timro21 wrote: > Hi Tim, sure, I'm looking at the perfect cuboid problem. > > I've just spent a couple of very frustrating hours. ?Given that I'm in > my 50's but have the brain of a retarded 3-year-old, can someone > please explain what I have to do to download gmpy to use with > ActivePython 2.6 on a Windows system? ?I downloaded (or so I thought) > GMPY 1.04 fromhttp://code.google.com/p/gmpy/but all the files look > way too small...and then how do I import it into a Python > program...I'm sure I could do this if it wasn't for my extreme > stupidity... You should just download the Windows installer from the Downloads tab. I would recommend version 1.10 since it should be faster and fixes some bugs in 1.04. The direct link is http://gmpy.googlecode.com/files/gmpy-1.10-beta.win32-py2.6.exe Just run the installer and it should automatically detect your existing Python installation. The executable is small. ;-) casevh p.s. If you're using a 64-bit Windows system, let me know and I'll step you through the manual process. From Scott.Daniels at Acm.Org Fri Jul 24 01:20:27 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 23 Jul 2009 22:20:27 -0700 Subject: Issue combining gzip and subprocess In-Reply-To: References: <2YednUGQxvntgfrXnZ2dnUVZ_hWdnZ2d@pdx.net> Message-ID: Piet van Oostrum wrote: >>>>>> Scott David Daniels (SDD) schreef: > ... >> SDD> Or even: >> SDD> proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE) >> SDD> with gzip.open(filename, 'w') as dest: >> SDD> for line in iter(proc.stdout, ''): >> SDD> f.write(line) > > If it would work. > > 1) with gzip... is not supported in Python < 3.1 > 2) for line in iter(proc.stdout), i.e. no second argument. > 3) dest <==> f should be the same identifier. > > Lesson: if you post code either: > - test it and copy verbatim from your test, or > - write a disclaimer Totally chagrined and embarassed. Chastisement absorbed and acknowledged. --Scott David Daniels Scott.Daniels at Acm.Org From timro21 at gmail.com Fri Jul 24 01:36:42 2009 From: timro21 at gmail.com (timro21) Date: Thu, 23 Jul 2009 22:36:42 -0700 (PDT) Subject: integer square roots References: <1646c39f-67a5-45e9-a9ca-f54e4cd7996f@c2g2000yqi.googlegroups.com> <7x4ot2zzn4.fsf@ruckus.brouhaha.com> <7xzlaudf22.fsf@ruckus.brouhaha.com> <7xocra7q4z.fsf@ruckus.brouhaha.com> <2b4f9c35-f429-4488-89e5-aaa05bc9d4b7@k13g2000prh.googlegroups.com> <0fc47aab-eb53-4e0a-be6d-9736883db888@i8g2000pro.googlegroups.com> Message-ID: <4132e60e-f586-42d1-b0fc-460aecf6a474@d9g2000prh.googlegroups.com> Bloody 'ell - thanks. :-) From girish.cfc at gmail.com Fri Jul 24 02:11:11 2009 From: girish.cfc at gmail.com (Girish) Date: Thu, 23 Jul 2009 23:11:11 -0700 (PDT) Subject: Registering DLL Message-ID: <071d3f78-a14b-4ea1-96fd-0f10d9dc2b80@d36g2000prb.googlegroups.com> Hello, I am facing a problem while registering DLL(Dspace.dll).. I am getting an error saying: "DLLRegisterServer inDspace.dll Failed. Return Code: 0x80040201" I am using following piece of code to generate DLL: ######################################################## # This setup script builds a single-file Python inprocess COM server. from distutils.core import setup import py2exe import sys # If run without args, build executables, in quiet mode. if len(sys.argv) == 1: sys.argv.append("py2exe") sys.argv.append("-q") class Target: def __init__(self, **kw): self.__dict__.update(kw) # for the versioninfo resources self.version = "1.0" self.company_name = "None" self.copyright = "None" self.name = "API" interp = Target( description = "COM server module", # what to build. For COM servers, the module name (not the # filename) must be specified! modules = ["Dspace"], #script = ["Dspace"], # we only want the inproc server. create_exe = True, ) excludes = ["pywin", "pywin.debugger", "pywin.debugger.dbgcon", "pywin.dialogs", "pywin.dialogs.list", 'email.Generator', 'email.Iterators', 'email.Utils'] options = { "bundle_files": 1, "ascii": 1, # to make a smaller executable, don't include the encodings "compressed": 1, # compress the library archive "excludes": excludes, # COM stuff we don't want } setup( options = {"py2exe": options}, zipfile = None, # append zip-archive to the executable. com_server = [interp], ) ################################################################ I am trying to generate a DLL from Python COM server module. I am able to register the module by running the python code, but when I generate a DLL and then try to register it using Regsvr32 command, I am getting above mentioned error Please help me resolve this issue. -Girish From pfeldman at verizon.net Fri Jul 24 02:35:43 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Thu, 23 Jul 2009 23:35:43 -0700 (PDT) Subject: len() should always return something Message-ID: <24639361.post@talk.nabble.com> Some aspects of the Python design are remarkably clever, while others leave me perplexed. Here's an example of the latter: Why does len() give an error when applied to an int or float? len() should always return something; in particular, when applied to a scalar, it should return a value of 1. Of course, I can define my own function like this: def mylen(x): if isinstance(x,int) or isinstance(x,float): return 1 return len(x) But, this shouldn't be necessary. -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24639361.html Sent from the Python - python-list mailing list archive at Nabble.com. From deets at nospam.web.de Fri Jul 24 02:57:44 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 08:57:44 +0200 Subject: len() should always return something In-Reply-To: References: Message-ID: <7ct4b8F29mednU1@mid.uni-berlin.de> Dr. Phillip M. Feldman schrieb: > Some aspects of the Python design are remarkably clever, while others leave > me perplexed. Here's an example of the latter: Why does len() give an error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. Of > course, I can define my own function like this: > > def mylen(x): > if isinstance(x,int) or isinstance(x,float): return 1 > return len(x) > > But, this shouldn't be necessary. Can you show some example of where that is actually making a piece of code more elegant? Diez From clp2 at rebertia.com Fri Jul 24 03:02:28 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 00:02:28 -0700 Subject: len() should always return something In-Reply-To: <24639361.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> Message-ID: <50697b2c0907240002i1721d7a4y54d618782ca1a071@mail.gmail.com> On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. Feldman wrote: > > Some aspects of the Python design are remarkably clever, while others leave > me perplexed. Here's an example of the latter: Why does len() give an error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. Of > course, I can define my own function like this: > > def mylen(x): > ? if isinstance(x,int) or isinstance(x,float): return 1 > ? return len(x) > > But, this shouldn't be necessary. The problem is that redefining len()/length/size that way would violate several principles of Python's design (The "Zen" of Python - http://www.python.org/dev/peps/pep-0020/). Specifically: - Explicit is better than implicit. - Special cases aren't special enough to break the rules. - Errors should never pass silently. - In the face of ambiguity, refuse the temptation to guess. If you'd explain the situation that prompts you to find this redefinition necessary, I'm sure someone can suggest a better approach. Cheers, Chris -- http://blog.rebertia.com From __peter__ at web.de Fri Jul 24 03:07:03 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 09:07:03 +0200 Subject: len() should always return something References: Message-ID: Dr. Phillip M. Feldman wrote: > Some aspects of the Python design are remarkably clever, while others > leave me perplexed. Here's an example of the latter: Why does len() give > an error when applied to an int or float? len() should always return > something; in particular, when applied to a scalar, it should return a > value of 1. Of course, I can define my own function like this: > > def mylen(x): > if isinstance(x,int) or isinstance(x,float): return 1 > return len(x) > > But, this shouldn't be necessary. Python should not blur the distinction between vectors an scalars like that. Instead of trying to be clever you should pass a vector with a single item and send mylen() to /dev/null. On a general note, I think one of Python's strengths is that it consistently /avoids/ this kind of cleverness. A prominent example is the handling of "1" + 1. Peter From nick at craig-wood.com Fri Jul 24 03:29:57 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Fri, 24 Jul 2009 02:29:57 -0500 Subject: Looking for os.listdir() generator References: Message-ID: Christian Heimes wrote: > Nick Craig-Wood wrote: > > Christian Heimes wrote: > >> I'm looking for a generator version of os.listdir() for Python 2.5 and > >> newer. I know somebody has worked on it because I've seen a generator > >> version in a posting on some list or blog a while ago. I can't find it > >> anymore. It seems my Google fu is lacking today. All I can find is a > >> very old version of xlistdir. A Cython based solution is appreciated but > >> please no ctypes version. > > > > I posted exactly that yesterday I think ;-) > > > > Note that this has a python part as well as a ctypes part because the > > version of ctypes I used doesn't support generators. I think the > > development version does though. > > Thanks Nick! > > ctypes? I'm sure you wanted to say Cython :) Er, yes! > If you don't mind I'm going to wrap it up into a nice package and > release it on PyPI. Yes thats fine with me! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From wilk at flibuste.net Fri Jul 24 04:04:03 2009 From: wilk at flibuste.net (William Dode) Date: 24 Jul 2009 08:04:03 GMT Subject: ANN: psyco V2 References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> Message-ID: <4a696af3$0$442$426a74cc@news.free.fr> On 23-07-2009, Christian Tismer wrote: > On 7/17/09 4:11 AM, Bearophile wrote: >> Very good, thank you. I'll try it when I can. >> >> Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow? > > Psyco3: nice typo! :-) > > Well, I haven't so far found a new idea there that I'd want > to borrow and did not know from PyPy, before. > Wasn't the project plan saying the opposite, borrowing > some ideas from psyco? :-) > http://code.google.com/p/unladen-swallow/wiki/ProjectPlan How do you see the futur of psyco when unladen-swallow will grab the best of psyco (if they can !) ? Wait and see ? Anyway, thanks a lot for your work that we can use NOW ! -- William Dod? - http://flibuste.net Informaticien Ind?pendant From hendrik at microcorp.co.za Fri Jul 24 04:13:02 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 24 Jul 2009 10:13:02 +0200 Subject: Gedcom and Genealogy In-Reply-To: <5222a601-a128-4892-9a8d-e1942ac9ce4d@e27g2000yqm.googlegroups.com> References: <5222a601-a128-4892-9a8d-e1942ac9ce4d@e27g2000yqm.googlegroups.com> Message-ID: <200907241013.02795.hendrik@microcorp.co.za> On Friday 24 July 2009 00:14:19 Gordon wrote: > We have many small libraries in JAVA or Ruby that need to be ported to > Python. Actually it's so simple a rewrite is possible too. Is this: 1 - A question? 2 - A job offer? 3 - A piece of random news? - Hendrik From mwawrzyczek at gmail.com Fri Jul 24 04:35:52 2009 From: mwawrzyczek at gmail.com (marekw2143) Date: Fri, 24 Jul 2009 01:35:52 -0700 (PDT) Subject: Adding method from one class to another class or to instance of another class Message-ID: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Hi, I have one class (A) that has defined method createVars. I would like to add that method to class B The code looks like this: class A(object): def createVars(self): self.v1 = 1 self.v2 = 3 pass class B(object): pass I don't want to use inheritance (because class A has many methods defined that class B doesn't need). When I try the folloowing: B.createVars = C.createVars B().createVars() then the following error occurs: Traceback (most recent call last): File "", line 1, in TypeError: unbound method createVars() must be called with A instance as first argument (got nothing instead) When I try to add the createVars method to instance of B: >>> b=B() >>> b.createVars = new.instancemethod(A.createVars, b, B) >>> b.createVars > >>> b.createVars() Then the following error raises: Traceback (most recent call last): File "", line 1, in TypeError: unbound method createVars() must be called with A instance as first argument (got B instance instead) How can I solve this problem? Regards, Marek From Slaunger at gmail.com Fri Jul 24 04:38:37 2009 From: Slaunger at gmail.com (Slaunger) Date: Fri, 24 Jul 2009 01:38:37 -0700 (PDT) Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 Message-ID: OS: Win XP SP3, 32 bit Python 2.5.4 Hi I have run into some problems with allocating numpy.memmaps exceeding and accumulated size of about 2 GB. I have found out that the real problem relates to numpy.memmap using mmap.mmap I've written a small test program to illustrate it: import itertools import mmap import os files = [] mmaps = [] file_names= [] mmap_cap=0 bytes_per_mmap = 100 * 1024 ** 2 try: for i in itertools.count(1): file_name = "d:/%d.tst" % i file_names.append(file_name) f = open(file_name, "w+b") files.append(f) mm = mmap.mmap(f.fileno(), bytes_per_mmap) mmaps.append(mm) mmap_cap += bytes_per_mmap print "Created %d writeable mmaps containing %d MB" % (i, mmap_cap/(1024**2)) #Clean up finally: print "Removing mmaps..." for mm, f, file_name in zip(mmaps, files, file_names): mm.close() f.close() os.remove(file_name) print "Done..." which creates this output Created 1 writeable mmaps containing 100 MB Created 2 writeable mmaps containing 200 MB .... Created 17 writeable mmaps containing 1700 MB Created 18 writeable mmaps containing 1800 MB Removing mmaps... Done... Traceback (most recent call last): File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha \mmaptest.py", line 16, in mm = mmap.mmap(f.fileno(), bytes_per_mmap) WindowsError: [Error 8] Not enough storage is available to process this command There is more than 25 GB of free space on drive d: at this stage. Is it a bug or a "feature" of the 32 bit OS? I am surprised about it as I have not found any notes about these kinds of limitations in the documentation. I am in dire need of these large memmaps for my task, and it is not an option to change OS due to other constraints in the system. Is there anything I can do about it? Best wishes, Kim From manu3d at gmail.com Fri Jul 24 04:55:03 2009 From: manu3d at gmail.com (Emanuele D'Arrigo) Date: Fri, 24 Jul 2009 01:55:03 -0700 (PDT) Subject: import vs imp and friends. References: <9ee16eda-403c-4d62-b9a4-59b5d387c39a@h21g2000yqa.googlegroups.com> Message-ID: Christian, Robert, thank you both for the replies, much appreciated. Manu From vinay_sajip at yahoo.co.uk Fri Jul 24 05:14:13 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Fri, 24 Jul 2009 02:14:13 -0700 (PDT) Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: <61b029e2-494f-4c87-871f-2469a88aaf91@o6g2000yqj.googlegroups.com> On Jul 24, 4:10?am, Naoki INADA wrote: > > Yes! I confused by it. > > s/I confused/I am confused/ > > > "Writing unicode to a file(-like)" is a simple requirement. > > Does python have any simple resolution for it? > > s/resolution/solution/ > Of course, Python 3 has much better Unicode support: --------------------------------------------------------------------- C:\Users\Vinay>chcp 1251 Active code page: 1251 C:\Users\Vinay>\python31\python ActivePython 3.1.0.1 (ActiveState Software Inc.) based on Python 3.1 (r31:73572, Jun 28 2009, 19:55:39) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.stdout.encoding 'cp1251' >>> u = '\u0434\u043e \u0441\u0432\u0438\u0434\u0430\u043d\u0438\u044f' >>> print(u) ?? ???????? >>> n = sys.stdout.write(u) ?? ????????>>> ^Z --------------------------------------------------------------------- Regards, Vinay Sajip From piet at cs.uu.nl Fri Jul 24 05:37:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 11:37:21 +0200 Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: >>>>> Naoki INADA (NI) wrote: >NI> "Writing unicode to a file(-like)" is a simple requirement. >NI> Does python have any simple resolution for it? Yes, Python 3 will do this. For Python < 3.0 you will have to use a codecs wrapper or explicitely do the encoding. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From deets at nospam.web.de Fri Jul 24 05:38:05 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 11:38:05 +0200 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 In-Reply-To: References: Message-ID: <7ctdo0F28litnU1@mid.uni-berlin.de> Slaunger schrieb: > OS: Win XP SP3, 32 bit > Python 2.5.4 > > Hi I have run into some problems with allocating numpy.memmaps > exceeding and accumulated size of about 2 GB. I have found out that > the real problem relates to numpy.memmap using mmap.mmap > > I've written a small test program to illustrate it: > > import itertools > import mmap > import os > > files = [] > mmaps = [] > file_names= [] > mmap_cap=0 > bytes_per_mmap = 100 * 1024 ** 2 > try: > for i in itertools.count(1): > file_name = "d:/%d.tst" % i > file_names.append(file_name) > f = open(file_name, "w+b") > files.append(f) > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > mmaps.append(mm) > mmap_cap += bytes_per_mmap > print "Created %d writeable mmaps containing %d MB" % (i, > mmap_cap/(1024**2)) > > #Clean up > finally: > print "Removing mmaps..." > for mm, f, file_name in zip(mmaps, files, file_names): > mm.close() > f.close() > os.remove(file_name) > print "Done..." > > > which creates this output > > Created 1 writeable mmaps containing 100 MB > Created 2 writeable mmaps containing 200 MB > .... > Created 17 writeable mmaps containing 1700 MB > Created 18 writeable mmaps containing 1800 MB > Removing mmaps... > Done... > Traceback (most recent call last): > File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha > \mmaptest.py", line 16, in > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > WindowsError: [Error 8] Not enough storage is available to process > this command > > There is more than 25 GB of free space on drive d: at this stage. > > Is it a bug or a "feature" of the 32 bit OS? It's a limitation, yes. That's what 64-bit-OSes are for. > I am surprised about it as I have not found any notes about these > kinds of limitations in the documentation. > > I am in dire need of these large memmaps for my task, and it is not an > option to change OS due to other constraints in the system. > > Is there anything I can do about it? Only by partitioning data yourself, and accessing these partitions. Like in the good old days of DOS-programming. Diez From piet at cs.uu.nl Fri Jul 24 05:38:51 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 11:38:51 +0200 Subject: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox) References: Message-ID: >>>>> Chris Rebert (CR) wrote: >CR> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: >>> You can use tabnanny to help diagnose the problem: >>> http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a >CR> Anyone know what's the deal with effbot.org? It seems to be broken at >CR> present, forcing me to use Google's cached version. >CR> It's a real shame since it has lots of handy Python info. Just try again. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From __peter__ at web.de Fri Jul 24 05:38:57 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 11:38:57 +0200 Subject: Adding method from one class to another class or to instance of another class References: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Message-ID: marekw2143 wrote: > Hi, > > I have one class (A) that has defined method createVars. I would like > to add that method to class B > The code looks like this: > > > class A(object): > def createVars(self): > self.v1 = 1 > self.v2 = 3 > pass > > class B(object): > pass > > > I don't want to use inheritance (because class A has many methods > defined that class B doesn't need). You can move createVars() into a mixin or common base class: class M(object): def createVars(self): ... class A(M): ... class B(M) ... > When I try the folloowing: > > > B.createVars = C.createVars > B().createVars() > > > then the following error occurs: > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got nothing instead) > > When I try to add the createVars method to instance of B: > >>>> b=B() >>>> b.createVars = new.instancemethod(A.createVars, b, B) >>>> b.createVars > > >>>> b.createVars() > > > > Then the following error raises: > > > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got B instance instead) > > > > How can I solve this problem? >>> class A(object): ... def create_vars(self): ... self.x = 42 ... >>> class B(object): pass ... >>> B.create_vars = A.create_vars.im_func >>> b = B() >>> b.create_vars() >>> b.x 42 An alternative I find a bit cleaner: >>> def create_vars(self): self.x = 42 ... >>> class A(object): ... create_vars = create_vars ... >>> class B(object): ... create_vars = create_vars ... >>> b = B() >>> b.create_vars() >>> b.x 42 Peter From gagsl-py2 at yahoo.com.ar Fri Jul 24 05:47:37 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 24 Jul 2009 06:47:37 -0300 Subject: available formats and params for Image.save() References: <4a6833dd$0$22599$426a34cc@news.free.fr> Message-ID: En Thu, 23 Jul 2009 06:56:45 -0300, News123 escribi?: > Somehow I have difficulties reading the documentation for PIL (Image) > > Is there an easy way to know which formats are supported and what their > names are? py> import PIL py> from PIL import Image py> Image.ID [] py> Image.init() py> Image.ID ['PNG', 'ARG', 'BMP', 'BUFR', 'CUR', 'PCX', 'DCX', 'EPS', 'FITS', 'FLI', 'FPX', 'GBR', 'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM', 'IMT', 'IPTC', 'JPEG', 'MCIDA S', 'TIFF', 'MIC', 'MPEG', 'MSP', 'PCD', 'PIXAR', 'PPM', 'PSD', 'SGI', 'SPIDER', 'SUN', 'TGA', 'WBMP', 'WMF', 'XBM', 'XPM', 'XVTHUMB'] py> Image.OPEN.keys() ['PCX', 'ICNS', 'HDF5', 'SUN', 'MIC', 'EPS', 'MSP', 'FLI', 'FITS', 'GBR', 'WBMP' , 'PCD', 'PIXAR', 'BUFR', 'PPM', 'WMF', 'SGI', 'BMP', 'TGA', 'DCX', 'ICO', 'CUR' , 'XPM', 'TIFF', 'JPEG', 'SPIDER', 'GIF', 'GRIB', 'IM', 'IMT', 'IPTC', 'FPX', 'X BM', 'MPEG', 'PSD', 'ARG', 'XVTHUMB', 'PNG', 'MCIDAS'] py> Image.SAVE.keys() ['XBM', 'PCX', 'SPIDER', 'HDF5', 'TIFF', 'BUFR', 'EPS', 'JPEG', 'MSP', 'GRIB', ' GIF', 'BMP', 'IM', 'PPM', 'PDF', 'FITS', 'PALM', 'WBMP', 'WMF', 'PNG'] > Is there an easy way to know which parameters are supported by > Image.save(). How can I list them where are they documented? That depends on the format being used. The PIL handbook lists the standard formats used and their parameters: http://www.pythonware.com/library/pil/handbook/index.htm > I'm at a complete loss at finding out what parameters the save function > accepts for saving a JPG file or a PNG file http://www.pythonware.com/library/pil/handbook/format-jpeg.htm -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 24 06:06:29 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 24 Jul 2009 07:06:29 -0300 Subject: invoke method on many instances References: Message-ID: En Thu, 23 Jul 2009 21:27:35 -0300, Aahz escribi?: > In article , > Gabriel Genellina wrote: >> >> NLMPI > > What? IHNFI -- Gabriel Genellina From amrita at iisermohali.ac.in Fri Jul 24 06:20:11 2009 From: amrita at iisermohali.ac.in (amrita at iisermohali.ac.in) Date: Fri, 24 Jul 2009 15:50:11 +0530 (IST) Subject: how to get no value Message-ID: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> Hi, I have a file having lines:- 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = 85 ALA H = 8.60 N = CA = HA = 4.65 C = Now i want to make two another file in which i want to put those lines for which C is missing and another one for which N,CA and C all are missing, I tried in this way: import re f = open('chem.txt') for line in f: if re.search('C = ',''): print line but i am not getting the desired output. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA From lpmelo at ualg.pt Fri Jul 24 06:21:30 2009 From: lpmelo at ualg.pt (Luis Pedro Almeida) Date: Fri, 24 Jul 2009 11:21:30 +0100 Subject: Convert points to polygon shapefile Message-ID: <20090724101638.E93B414B2A@smtp3.ualg.pt> Dear all, I would like to know how to convert a list of points into a polygon shapefile (esri). Thanks! Best regards, Luis Pedro Almeida From gagsl-py2 at yahoo.com.ar Fri Jul 24 06:24:52 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 24 Jul 2009 07:24:52 -0300 Subject: OverflowError in RLock.acquire() References: Message-ID: En Fri, 24 Jul 2009 01:27:10 -0300, David Roberts escribi?: > I'm trying to port a Python application to Windows, and I'm getting > the following error (which did not occur when running on Linux): > > Exception in thread Thread-4: > File "C:\Python26\lib\threading.py", line 803, in currentThread > return _active[_get_ident()] > OverflowError: can't convert negative value to unsigned long Looks like a bug in the thread module - you should report it at http://bugs.python.org -- Gabriel Genellina From piet at cs.uu.nl Fri Jul 24 06:46:14 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 12:46:14 +0200 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 References: Message-ID: >>>>> Slaunger (S) wrote: >S> OS: Win XP SP3, 32 bit >S> Python 2.5.4 >S> Hi I have run into some problems with allocating numpy.memmaps >S> exceeding and accumulated size of about 2 GB. I have found out that >S> the real problem relates to numpy.memmap using mmap.mmap On Windows XP the virtual address space of a process is limited to 2 GB unless the /3GB switch is used in the Boot.ini file. http://www.microsoft.com/whdc/system/platform/server/PAE/PAEmem.mspx -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From darcy at druid.net Fri Jul 24 06:56:03 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 24 Jul 2009 06:56:03 -0400 Subject: how to get no value In-Reply-To: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> References: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> Message-ID: <20090724065603.cd758a39.darcy@druid.net> On Fri, 24 Jul 2009 15:50:11 +0530 (IST) amrita at iisermohali.ac.in wrote: > but i am not getting the desired output. Show us what output you got and what you desired. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From deets at nospam.web.de Fri Jul 24 06:56:16 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 12:56:16 +0200 Subject: how to get no value In-Reply-To: References: Message-ID: <7ctiaiF1vrcvpU1@mid.uni-berlin.de> amrita at iisermohali.ac.in schrieb: > Hi, > > I have a file having lines:- > > 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 > 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = > 85 ALA H = 8.60 N = CA = HA = 4.65 C = > > Now i want to make two another file in which i want to put those lines for > which C is missing and another one for which N,CA and C all are missing, > > I tried in this way: > import re > f = open('chem.txt') > for line in f: > if re.search('C = ',''): > print line > > but i am not getting the desired output. Gosh. Must be groundhog-day. Again. And there is me thinking that my job could be endangered by cheap & qualified indian soft-workers - can't be to many of them around if the OP doesn't get a hold of one for the better part of a month now. Must be one of those management myths they tell you to scare you into a less well paid contract... Diez From beni.cherniavsky at gmail.com Fri Jul 24 07:13:09 2009 From: beni.cherniavsky at gmail.com (Beni Cherniavsky) Date: Fri, 24 Jul 2009 04:13:09 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> Message-ID: <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> On Jul 22, 9:36?am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. ? > On Jul 22, 9:36 am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. > The *technical* reason is immutability for dict keys. Dict could allow mutable objects as keys by comparing *by value*, making a copy on insertion and hashing the current value on lookup. Prior art: the 2.3 sets module allows mutable Sets as elements in Sets, by making ImmutableSet copies on insertion, and hashing Sets as if they are temporarily immutable on lookup. This inspired PEP 351 and ambitious proposals to expand the approach to all Python with a copy-on-write scheme. But these ideas were rejected, and the 2.4 builtin sets only allow frozenset elements. Half the reason is technical: copy-on-write and harder than it sounds, and without it you pay a performance price. But the deeper reason is style: immutable types are convenient! The allow a pure-functional style of code, which can be simpler. Of course, sometimes an imperative style is simpler. Depends on the problem. My rule of thumb: - Use mutable lists when you are likely to modify individual elements. - Use immutable tuples when you are likely to replace the whole thing. (In practice, this boils down to similar usage to the "official" rule of thumb that lists are for homogenous data, and tuples for heterogenous records.) From d at vidr.cc Fri Jul 24 07:29:44 2009 From: d at vidr.cc (David Roberts) Date: Fri, 24 Jul 2009 21:29:44 +1000 Subject: OverflowError in RLock.acquire() In-Reply-To: References: Message-ID: Done: http://bugs.python.org/issue6562 -- David Roberts http://da.vidr.cc/ On Fri, Jul 24, 2009 at 20:24, Gabriel Genellina wrote: > En Fri, 24 Jul 2009 01:27:10 -0300, David Roberts escribi?: > >> I'm trying to port a Python application to Windows, and I'm getting >> the following error (which did not occur when running on Linux): >> >> Exception in thread Thread-4: >> ?File "C:\Python26\lib\threading.py", line 803, in currentThread >> ? ?return _active[_get_ident()] >> OverflowError: can't convert negative value to unsigned long > > Looks like a bug in the thread module - you should report it at > http://bugs.python.org > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > From piet at cs.uu.nl Fri Jul 24 07:48:09 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 13:48:09 +0200 Subject: how to get no value References: Message-ID: >>>>> amrita at iisermohali.ac.in (a) a ?crit: >a> Hi, >a> I have a file having lines:- >a> 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 >a> 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = >a> 85 ALA H = 8.60 N = CA = HA = 4.65 C = >a> Now i want to make two another file in which i want to put those lines for >a> which C is missing and another one for which N,CA and C all are missing, >a> I tried in this way: >a> import re >a> f = open('chem.txt') >a> for line in f: >a> if re.search('C = ',''): >a> print line >a> but i am not getting the desired output. You never look in the lines you read. if re.search('C = ',''): should be if re.search('C = ', line): Do you really think before you write your code? Or look at it after you have written it? Sorry if I offend you but you give the impression of just trying some more or less random stuff and then asking here if it doesn't give the required result. That's not the attitude of a researcher, unless your definition of research is 'asking on Usenet'. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pdpinheiro at gmail.com Fri Jul 24 08:08:21 2009 From: pdpinheiro at gmail.com (pdpi) Date: Fri, 24 Jul 2009 05:08:21 -0700 (PDT) Subject: Convert points to polygon shapefile References: Message-ID: On Jul 24, 11:21?am, Luis Pedro Almeida wrote: > Dear all, > > I would like to know how to convert a list of points into a polygon > shapefile (esri). > > Thanks! > > Best regards, > > Luis Pedro Almeida I think you'd be better served by asking this question in a newsgroup dedicated to GIS software (I'm guessing that's the ESRI you meant). Still, what you want is to find the file format specification, and implement it. From piet at cs.uu.nl Fri Jul 24 08:14:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 14:14:27 +0200 Subject: Convert points to polygon shapefile References: Message-ID: >>>>> Luis Pedro Almeida (LPA) wrote: >LPA> Dear all, >LPA> I would like to know how to convert a list of points into a >LPA> polygon shapefile (esri). http://lmgtfy.com/?q=esri+shapefile+Python -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Fri Jul 24 08:25:17 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 24 Jul 2009 08:25:17 -0400 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 In-Reply-To: References: Message-ID: <4A69A82D.8090301@ieee.org> Slaunger wrote: > OS: Win XP SP3, 32 bit > Python 2.5.4 > > Hi I have run into some problems with allocating numpy.memmaps > exceeding and accumulated size of about 2 GB. I have found out that > the real problem relates to numpy.memmap using mmap.mmap > > I've written a small test program to illustrate it: > > import itertools > import mmap > import os > > files = [] > mmaps = [] > file_names= [] > mmap_cap=0 > bytes_per_mmap = 100 * 1024 ** 2 > try: > for i in itertools.count(1): > file_name = "d:/%d.tst" % i > file_names.append(file_name) > f = open(file_name, "w+b") > files.append(f) > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > mmaps.append(mm) > mmap_cap += bytes_per_mmap > print "Created %d writeable mmaps containing %d MB" % (i, > mmap_cap/(1024**2)) > > #Clean up > finally: > print "Removing mmaps..." > for mm, f, file_name in zip(mmaps, files, file_names): > mm.close() > f.close() > os.remove(file_name) > print "Done..." > > > which creates this output > > Created 1 writeable mmaps containing 100 MB > Created 2 writeable mmaps containing 200 MB > .... > Created 17 writeable mmaps containing 1700 MB > Created 18 writeable mmaps containing 1800 MB > Removing mmaps... > Done... > Traceback (most recent call last): > File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha > \mmaptest.py", line 16, in > mm = mmap.mmap(f.fileno(), bytes_per_mmap) > WindowsError: [Error 8] Not enough storage is available to process > this command > > There is more than 25 GB of free space on drive d: at this stage. > > Is it a bug or a "feature" of the 32 bit OS? > > I am surprised about it as I have not found any notes about these > kinds of limitations in the documentation. > > I am in dire need of these large memmaps for my task, and it is not an > option to change OS due to other constraints in the system. > > Is there anything I can do about it? > > Best wishes, > Kim > > It's not a question of how much disk space there is, but how much virtual space 32 bits can address. 2**32 is about 4 gig, and Windows XP reserves about half of that for system use. Presumably a 64 bit OS would have a much larger limit. Years ago I worked on Sun Sparc system which had much more limited shared memory access, due to hardware limitations. So 2gig seems pretty good to me. There is supposed to be a way to tell the Windows OS to only use 1 gb of virtual space, leaving 3gb for application use. But there are some limitations, and I don't recall what they are. I believe it has to be done globally (probably in Boot.ini), rather than per process. And some things didn't work in that configuration. DaveA From jeanmichel at sequans.com Fri Jul 24 08:34:23 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 24 Jul 2009 14:34:23 +0200 Subject: doted filenames in import statements In-Reply-To: References: <4A670313.8040204@sequans.com> Message-ID: <4A69AA4F.9060707@sequans.com> Terry Reedy wrote: > Jean-Michel Pichavant wrote: >> Piet van Oostrum wrote: >>> >>> [snip] >>> >>>> JP> file = "/home/dsp/4.6.0.0/test.py" >>>> JP> test = __import__(file) >>>> JP> => no module name blalalal found. >>> >>> >>>> JP> Any suggestion ? I tried multiple escape technics without any >>>> success. >>> >>> Rightly so. >>> >>> I think the best would be to add the directory to sys.path >>> sys.path.add('/home/dsp/4.6.0.0') >>> and then >>> __import__('test', ... ) >> >> I see. My problem is that a have to import 2 different files having >> de same name. In the same name space I have 2 objects from 2 >> different software branches, let's say 4.6.0 and 4.6.1. >> The first object shall import 4.6.0/orb.py and the second one >> 4.6.1/orb.py. >> >> If I add 4.6.1 to sys.path, the import statement will look like: >> self._orb = __import__('orb') >> The problem is, python wil assume orb is already imported and will >> assign the module from the 4.6.0 branch to my 4.6.1 object. >> >> Do I have to mess up with sys.modules keys to make python import the >> correct file ? Is there a standard/proper way to do that ? > > If you make the directory names into proper identifiers like v460 and > v461 and add __init__.py to each to make them packages and have both > on search path, then > > import v460.orb #or import v460.orb as orb460 > import v461.orb #or import v460.orb as orb461 > > will get you both. One way or another, they have to get different > names within Python. > > Terry Jan Reedy > I finally had to write my own import statement as I prefered to manipulate the python objects instead of manipulating third party files. Basically when importing 'file.py' it records it in sys.modules as sys.modules['__magic_word_file''] and then I remove the standard reference. This allows me to import file.py again, but with a totally different path. (path is temporarily added to sys.path) JM From sanne at kortec.nl Fri Jul 24 08:34:53 2009 From: sanne at kortec.nl (Sanne Korzec) Date: Fri, 24 Jul 2009 14:34:53 +0200 Subject: trouble with wrapping a c program Message-ID: <20090724123458.TOWM14607.viefep17-int.chello.at@edge02.upc.biz> Hi Mailing, I am using a c program, which first initializes for some seconds and then waits for user input (keyboard) to type something. When enter is pressed the c program continues. I have wrapped this program in a python script, which starts the c program. To start the c program, there are many options in python e.g. Os.system os.popen or subprocess.popen To me there does not seem to be a difference for starting the program. They all work well. However the problem occurs when I want to input data during this c program. Using the keyboard and then enter in the c program prompt works, but I wish to do this from the python script by sending the string from the python script. I am able to print the string from python with a print command or with a stdout.write command. But this simply prints it to the prompt, the c program does nothing with this printed string. Is there a way to pipe, stream, or send this string to the running c program? All tutorials about piping I have read, seem to have to wait for the process to finish. Or need the stdin string before starting the program. Note that the c program is not finished when the input is needed, so I cannot use subprocess.call or wait. It is possible to give the user input in advance, but I do not want this, because this makes everything much slower! I really would like to do this during/while the c program is running. I have created a thread and a small xmlrpc server for this and this works fine. However I cannot seem to pass the string. Here is a small fragment of my code: #initialization cmd = [a list of my program and arguments] subprocess.Popen(cmd) #starts the c program #somewhere along the way Send_string(str): #Sys.stdin = str #subprocess.stdin = str I have spent the entire night trying to get this to work, but I can't seem to get it right. Any help is much appreciated. Also, if anybody could explain me where the fuck-up in my brain is, I would be very happy.. Sanne -------------- next part -------------- An HTML attachment was scrubbed... URL: From doetoe at gmail.com Fri Jul 24 08:38:00 2009 From: doetoe at gmail.com (Utpal Sarkar) Date: Fri, 24 Jul 2009 05:38:00 -0700 (PDT) Subject: non-owning references? Message-ID: Hi, I'm not sure the subject describes what I'm looking for, but the question is the following: Is there a way I can tell a variable that the object it is pointing too is not owned by it, in the sense that if it is the only reference to the object it can be garbage collected? I want this for what is essentially a singleton class, so that on first instantiation the object is created and a reference is kept in the class, that is used to return the same object in subsequent instantiations. When all instances go out of scope, the reference in the class is still there, preventing it from being garbage collected, but since the instance can be huge, I would like it to be. Thanks, Utpal From __peter__ at web.de Fri Jul 24 08:52:07 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Jul 2009 14:52:07 +0200 Subject: non-owning references? References: Message-ID: Utpal Sarkar wrote: > Is there a way I can tell a variable that the object it is pointing > too is not owned by it, in the sense that if it is the only reference > to the object it can be garbage collected? http://docs.python.org/library/weakref.html From gh at ghaering.de Fri Jul 24 09:00:43 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Fri, 24 Jul 2009 15:00:43 +0200 Subject: non-owning references? In-Reply-To: References: Message-ID: Utpal Sarkar wrote: > Hi, > [...] You're looking for the weakref module. What you're describing there sounds like a nice exercise, but I cannot imagine why you'd really need to clean it up, if it really is a singleton. -- Gerhard From ben+python at benfinney.id.au Fri Jul 24 09:06:34 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 24 Jul 2009 23:06:34 +1000 Subject: non-owning references? References: Message-ID: <87d47q5ixx.fsf@benfinney.id.au> Utpal Sarkar writes: > Is there a way I can tell a variable that the object it is pointing > too is not owned by it, in the sense that if it is the only reference > to the object it can be garbage collected? Python doesn't have ?pointers?, and doesn't really have ?variables? either, at least not how many other languages use that term. What it does have is references to objects . > on first instantiation the object is created and a reference is kept > in the class, that is used to return the same object in subsequent > instantiations. When all instances go out of scope, the reference in > the class is still there, preventing it from being garbage collected, > but since the instance can be huge, I would like it to be. What you are asking for is called a ?weak reference? and is provided by the ?weakref? module . -- \ ?Patience, n. A minor form of despair, disguised as a virtue.? | `\ ?Ambrose Bierce, _The Devil's Dictionary_, 1906 | _o__) | Ben Finney From doetoex at gmail.com Fri Jul 24 09:18:48 2009 From: doetoex at gmail.com (Doetoe) Date: Fri, 24 Jul 2009 06:18:48 -0700 (PDT) Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> Message-ID: <14f1729b-9035-417f-a725-3deaa9408a69@c2g2000yqi.googlegroups.com> On Jul 24, 3:06?pm, Ben Finney wrote: > Utpal Sarkar writes: > > Is there a way I can tell a variable that the object it is pointing > > too is not owned by it, in the sense that if it is the only reference > > to the object it can be garbage collected? > > Python doesn't have ?pointers?, and doesn't really have ?variables? > either, at least not how many other languages use that term. > > What it does have is references to objects > . > > > on first instantiation the object is created and a reference is kept > > in the class, that is used to return the same object in subsequent > > instantiations. When all instances go out of scope, the reference in > > the class is still there, preventing it from being garbage collected, > > but since the instance can be huge, I would like it to be. > > What you are asking for is called a ?weak reference? and is provided > by the ?weakref? module . > > -- > ?\ ? ? ??Patience, n. A minor form of despair, disguised as a virtue.? | > ? `\ ? ? ? ? ? ? ? ? ? ?Ambrose Bierce, _The Devil's Dictionary_, 1906 | > _o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| > Ben Finney Thanks to the three of you. This is precisely what I needed! Gerhard, the reason I need to clean it up is that it is a lazy data structure that can grow to arbitrary size. When it is not needed anymore it would still remain in memory. Utpal From breamoreboy at yahoo.co.uk Fri Jul 24 09:50:33 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 24 Jul 2009 14:50:33 +0100 Subject: how to get no value In-Reply-To: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> References: <23321.210.212.36.65.1248430811.squirrel@www.iisermohali.ac.in> Message-ID: amrita at iisermohali.ac.in wrote: > Hi, > > I have a file having lines:- > > 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 > 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = > 85 ALA H = 8.60 N = CA = HA = 4.65 C = > > Now i want to make two another file in which i want to put those lines for > which C is missing and another one for which N,CA and C all are missing, > > I tried in this way: > import re > f = open('chem.txt') > for line in f: > if re.search('C = ',''): > print line > > but i am not getting the desired output. > > > > > Amrita Kumari > Research Fellow > IISER Mohali > Chandigarh > INDIA > Try writing correct rather than incorrect code. Or as has been repeatedly stated get somone from your CS department to help. -- Kindest regards. Mark Lawrence. From hniksic at xemacs.org Fri Jul 24 09:55:45 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 24 Jul 2009 15:55:45 +0200 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> Message-ID: <87fxcm5gny.fsf@busola.homelinux.net> Ben Finney writes: > Utpal Sarkar writes: > >> Is there a way I can tell a variable that the object it is pointing >> too is not owned by it, in the sense that if it is the only reference >> to the object it can be garbage collected? > > Python doesn't have ?pointers?, and doesn't really have ?variables? > either, at least not how many other languages use that term. The OP didn't use the term "pointer", but the word "pointing", which makes sense in the context. The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it. Please stop complaining about valid terminology; it is not helpful. From invalid at invalid Fri Jul 24 09:57:02 2009 From: invalid at invalid (Grant Edwards) Date: Fri, 24 Jul 2009 08:57:02 -0500 Subject: len() should always return something References: Message-ID: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> On 2009-07-24, Dr. Phillip M. Feldman wrote: > > Some aspects of the Python design are remarkably clever, while > others leave me perplexed. Here's an example of the latter: > Why does len() give an error when applied to an int or float? > len() should always return something; in particular, when > applied to a scalar, it should return a value of 1. If len(7) returned a value of 1, then wouldn't one expect 7[0] to be valid? It isn't, so you'd then have to redefine all types so that they are sequences that can be indexed. Sounds like a big mess to me... [Are there types for which len() returns a value that can't be indexed?] -- Grant Edwards grante Yow! It's the RINSE CYCLE!! at They've ALL IGNORED the visi.com RINSE CYCLE!! From timlee126 at yahoo.com Fri Jul 24 10:03:22 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 24 Jul 2009 07:03:22 -0700 (PDT) Subject: Popen Message-ID: <896055.35192.qm@web63101.mail.re1.yahoo.com> Hi, I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? Thanks and regards! From invalid at invalid Fri Jul 24 10:10:29 2009 From: invalid at invalid (Grant Edwards) Date: Fri, 24 Jul 2009 09:10:29 -0500 Subject: how to get no value References: Message-ID: On 2009-07-24, amrita at iisermohali.ac.in wrote: > > Hi, > > I have a file having lines:- > > 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 > 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = > 85 ALA H = 8.60 N = CA = HA = 4.65 C = > > Now i want to make two another file in which i want to put > those lines for which C is missing and another one for which > N,CA and C all are missing, > > I tried in this way: > import re > f = open('chem.txt') > for line in f: > if re.search('C = ',''): > print line > > but i am not getting the desired output. I've told you before: don't use regular expressions (e.g. the "re" module). Stop using regular expressions now. Regular expressions are way beyond your capabilities. Use simple operations like split() and "in": f = open('chem.txt') for line in f: if "C = " in line: print line You really need to work through a Python tutorial or two: http://docs.python.org/tutorial/ http://www.greenteapress.com/thinkpython/thinkpython.html Better yet, take an couple introductory programming courses. I'm a bit surprised that one could become a "Research Fellow" in a scientific field without taking any programming courses. -- Grant Edwards grante Yow! I'm also against at BODY-SURFING!! visi.com From rhodri at wildebst.demon.co.uk Fri Jul 24 10:11:40 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 15:11:40 +0100 Subject: len() should always return something In-Reply-To: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards wrote: > On 2009-07-24, Dr. Phillip M. Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while >> others leave me perplexed. Here's an example of the latter: >> Why does len() give an error when applied to an int or float? >> len() should always return something; in particular, when >> applied to a scalar, it should return a value of 1. > > If len(7) returned a value of 1, then wouldn't one expect 7[0] > to be valid? It isn't, so you'd then have to redefine all > types so that they are sequences that can be indexed. Sounds > like a big mess to me... > > [Are there types for which len() returns a value that can't be > indexed?] > Dictionaries. Which doesn't make your point less valid. In fact I'd go so far as to argue that what len() gives you is the number of items in a container, so len(7) should return 0. -- Rhodri James *-* Wildebeest Herder to the Masses From rhodri at wildebst.demon.co.uk Fri Jul 24 10:21:36 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 15:21:36 +0100 Subject: non-owning references? In-Reply-To: <87fxcm5gny.fsf@busola.homelinux.net> References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic wrote: > Ben Finney writes: > >> Utpal Sarkar writes: >> >>> Is there a way I can tell a variable that the object it is pointing >>> too is not owned by it, in the sense that if it is the only reference >>> to the object it can be garbage collected? >> >> Python doesn't have ?pointers?, and doesn't really have ?variables? >> either, at least not how many other languages use that term. > > The OP didn't use the term "pointer", but the word "pointing", which > makes sense in the context. The term "variable" is used in the Python > language reference and elsewhere, and is quite compatible with how other > popular languages (Java, PHP, Lisp, ...) use it. Only superficially. Treating Python variables the same as C variables (say) is one of the classic ways that newbies come unstuck when mutable objects appear on the scene. While the OP appears to have the right idea, your "correction" here could be quite misleading. -- Rhodri James *-* Wildebeest Herder to the Masses From pruebauno at latinmail.com Fri Jul 24 10:45:21 2009 From: pruebauno at latinmail.com (nn) Date: Fri, 24 Jul 2009 07:45:21 -0700 (PDT) Subject: strange python scripting error References: Message-ID: On Jul 23, 7:03?pm, Dave Angel wrote: > Mark Tarver wrote: > > I have a very strange error. ?I have two test python files test.py and > > python.py which contain the following code > > > #!/usr/bin/python > > print "Content-type: text/html" > > print > > print "" > > print "
Hello, Linux.com!
" > > print "" > > > One file (test.py) works; you call it up and it shows a web page with > > > Hello, Linux.com > > > The other fails with a server configuration error. ?Both are running > > under Linux, same server, same permissions. ?Running a character scan > > shows that both files contain the same printable characters and are > > therefore typographically identical. ? They are absolutely the same. > > > The only hint at a difference I can see is that my ftp program says > > the files are of unequal lengths. ?test.py is 129 bytes long. > > python.py 134 bytes long. > > > A zipped folder containing both files is at > > >www.lambdassociates.org/weird.zip > > > Any ideas welcome. > > > Mark > > Easiest explanation is that python.py has Windows-style newlines. ?In > other words, each line ends with 0d0a, rather than the Unix convention > of 0a. > > If your server is Unix-based, it can't handle that first line, since it > has an illegal character (0d) following the > > #!/usr/bin/python > > line. ?Convert it to Unix line-endings. > > DaveA Use dos2unix for conversion of the longer file and try again: http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm From dickinsm at gmail.com Fri Jul 24 10:45:40 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Fri, 24 Jul 2009 07:45:40 -0700 (PDT) Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Jul 24, 3:11?pm, "Rhodri James" wrote: > Which doesn't make your point less valid. ?In fact I'd go so > far as to argue that what len() gives you is the number of > items in a container, so len(7) should return 0. Nah. 7 contains three bits, so len(7) should *clearly* return 3. Mark From user at example.net Fri Jul 24 10:50:03 2009 From: user at example.net (superpollo) Date: Fri, 24 Jul 2009 16:50:03 +0200 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> Mark Dickinson wrote: > On Jul 24, 3:11 pm, "Rhodri James" > wrote: > >>Which doesn't make your point less valid. In fact I'd go so >>far as to argue that what len() gives you is the number of >>items in a container, so len(7) should return 0. > > > Nah. 7 contains three bits, so len(7) should *clearly* return 3. and len("7") must return 8, by the same token... but wait! >>> len("7") 1 >>> my python installation must me outdated ;-) bye From deets at nospam.web.de Fri Jul 24 10:51:12 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 16:51:12 +0200 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: <7cu030F295totU1@mid.uni-berlin.de> Mark Dickinson schrieb: > On Jul 24, 3:11 pm, "Rhodri James" > wrote: >> Which doesn't make your point less valid. In fact I'd go so >> far as to argue that what len() gives you is the number of >> items in a container, so len(7) should return 0. > > Nah. 7 contains three bits, so len(7) should *clearly* return 3. But it contains a minimum of 32 bits! And why are you treating ones as special over zeros? I thought the times of BitRacism were finally over... Diez From kushal.kumaran+python at gmail.com Fri Jul 24 10:58:49 2009 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Fri, 24 Jul 2009 20:28:49 +0530 Subject: Popen In-Reply-To: <896055.35192.qm@web63101.mail.re1.yahoo.com> References: <896055.35192.qm@web63101.mail.re1.yahoo.com> Message-ID: <1e364c4e0907240758k753f8653o225ac97e92fd2eea@mail.gmail.com> On Fri, Jul 24, 2009 at 7:33 PM, Tim wrote: > > Hi, > I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? > Thanks and regards! > Assuming you mean subprocess.Popen, the child is executed asynchronously. You can use the wait() method on the Popen object if you want the parent to wait for the child to finish. -- kushal From piet at cs.uu.nl Fri Jul 24 11:03:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:03:58 +0200 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: >>>>> "Rhodri James" (RJ) wrote: >RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic wrote: >>> Ben Finney writes: >>> >>>> Utpal Sarkar writes: >>>> >>>>> Is there a way I can tell a variable that the object it is pointing >>>>> too is not owned by it, in the sense that if it is the only reference >>>>> to the object it can be garbage collected? >>>> >>>> Python doesn't have ?pointers?, and doesn't really have ?variables? >>>> either, at least not how many other languages use that term. >>> >>> The OP didn't use the term "pointer", but the word "pointing", which >>> makes sense in the context. The term "variable" is used in the Python >>> language reference and elsewhere, and is quite compatible with how other >>> popular languages (Java, PHP, Lisp, ...) use it. >RJ> Only superficially. Treating Python variables the same as C variables >RJ> (say) is one of the classic ways that newbies come unstuck when mutable >RJ> objects appear on the scene. While the OP appears to have the right idea, >RJ> your "correction" here could be quite misleading. If you read the OP, it is clear that he talked about a class variable, which is a perfectly legal notion in Python, and is mentioned as such in the language reference manual: `Variables defined in the class definition are class variables' And who was talking about C variables? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Fri Jul 24 11:06:23 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:06:23 +0200 Subject: how to get no value References: Message-ID: Well actually your subject is `how to get no value'. Your code does that perfectly. :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From inky788 at gmail.com Fri Jul 24 11:07:30 2009 From: inky788 at gmail.com (Inky 788) Date: Fri, 24 Jul 2009 08:07:30 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <1b78798e-9d5b-4037-91f1-5fad600514f0@d32g2000yqh.googlegroups.com> Message-ID: On Jul 23, 3:42?am, Hendrik van Rooyen wrote: > On Wednesday 22 July 2009 16:36:51 Inky 788 wrote: > > > On Jul 22, 2:36?am, Hendrik van Rooyen > > > wrote: > > > The good reason is the immutability, which lets you use > > > a tuple as a dict key. ? > > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > > just not sophisticated enough, but I've never wanted to use a list/ > > tuple as a dict key. This sounds like obscure usage, and a bit > > contrived as a reason for having *both* lists and tuples. > > Steven showed why you cannot have a mutable thing > as a key in a dict. > > if you think it is contrived, then please consider how you would > keep track of say the colour of a pixel on a screen at position > (x,y) - this is about the simplest "natural" tuple format and > example. My guess is that this is probably the way most people do it: ~~~~ #!/usr/bin/env python import sys import random if len( sys.argv ) != 3: print "Please pass exactly 2 ints. Exiting." sys.exit(1) NUM_COLUMNS = int( sys.argv[1] ) NUM_ROWS = int( sys.argv[2] ) print "Making array of %s columns by %s rows." % (NUM_COLUMNS, NUM_ROWS) def rand(): return int( 255 * random.random()) def make_a_pixel(): # red green blue return [rand(), rand(), rand()] def make_a_row(num_columns): temp_row = [] for i in range(num_columns): temp_row.append( make_a_pixel() ) return temp_row def make_array_of_pixels(num_columns, num_rows): rows = [] for i in range(num_rows): rows.append( make_a_row(num_columns) ) return rows def show_pixels(pixel_array): for row in pixel_array: for pixel in row: print pixel, ' ', print rows_of_pixels = make_array_of_pixels(NUM_COLUMNS, NUM_ROWS) show_pixels(rows_of_pixels) ~~~~ From piet at cs.uu.nl Fri Jul 24 11:10:07 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:10:07 +0200 Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: >>>>> "Rhodri James" (RJ) wrote: >RJ> On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards wrote: >>> On 2009-07-24, Dr. Phillip M. Feldman wrote: >>>> >>>> Some aspects of the Python design are remarkably clever, while >>>> others leave me perplexed. Here's an example of the latter: >>>> Why does len() give an error when applied to an int or float? >>>> len() should always return something; in particular, when >>>> applied to a scalar, it should return a value of 1. >>> >>> If len(7) returned a value of 1, then wouldn't one expect 7[0] >>> to be valid? It isn't, so you'd then have to redefine all >>> types so that they are sequences that can be indexed. Sounds >>> like a big mess to me... >>> >>> [Are there types for which len() returns a value that can't be >>> indexed?] >>> >RJ> Dictionaries. >RJ> Which doesn't make your point less valid. In fact I'd go so >RJ> far as to argue that what len() gives you is the number of >RJ> items in a container, so len(7) should return 0. But len(7) could as well be defined as 3, 1, 32, or 64 (depending on the implementation). Therefore it doesn't make much sense. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From ben+python at benfinney.id.au Fri Jul 24 11:14:06 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 25 Jul 2009 01:14:06 +1000 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: <8763di5d1d.fsf@benfinney.id.au> Hrvoje Niksic writes: > The term "variable" is used in the Python language reference and > elsewhere Yes. It should also be abundantly clear from the constant stream of confused newbies on this point that its usage of that term is different to what many expect from usage elsewhere. > and is quite compatible with how other popular languages (Java, PHP, > Lisp, ...) use it. Please stop complaining about valid terminology; it > is not helpful. I disagree with your assertions. Rather than yet another round of this tedious debate, I merely point interested readers to and ask them to draw their own conclusion on how compatible their pre-existing ?variable? concept is with Python's object reference model. -- \ ?Too many pieces of music finish too long after the end.? ?Igor | `\ Stravinskey | _o__) | Ben Finney From piet at cs.uu.nl Fri Jul 24 11:17:56 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 17:17:56 +0200 Subject: Popen References: Message-ID: >>>>> Tim (T) wrote: >T> Hi, >T> I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? >T> Thanks and regards! Only if you use Popen.wait(), Popen.communicate() or something similar like os.waitpid(), subprocess.call() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rylesny at gmail.com Fri Jul 24 11:22:55 2009 From: rylesny at gmail.com (ryles) Date: Fri, 24 Jul 2009 08:22:55 -0700 (PDT) Subject: difference in printing to screen Mac / Windows References: <6946b9500907180321j1ee7b254ua00d520599745ec1@mail.gmail.com> Message-ID: On Jul 18, 7:03?am, Tim Chase wrote: > Lastly, you can force all standard-output in your program to be > unbuffered without the "-u" parameter: And if you're using -u a lot, the PYTHONUNBUFFERED environment variable can also be set (but not empty), so that python adds the option automatically. From Scott.Daniels at Acm.Org Fri Jul 24 11:54:32 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 24 Jul 2009 08:54:32 -0700 Subject: regex: multiple matching for one string In-Reply-To: References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: rurpy at yahoo.com wrote: > Nick Dumas wrote: >> On 7/23/2009 9:23 AM, Mark Lawrence wrote: >>> scriptlearner at gmail.com wrote: >>>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >>>> will like to take out the values (valuea, valueb, and valuec). How do >>>> I do that in Python? The group method will only return the matched >>>> part. Thanks. >>>> >>>> p = re.compile('#a=*;b=*;c=*;') >>>> m = p.match(line) >>>> if m: >>>> print m.group(), >>> IMHO a regex for this is overkill, a combination of string methods such >>> as split and find should suffice. > > You're saying that something like the following > is better than the simple regex used by the OP? > [untested] > values = [] > parts = line.split(';') > if len(parts) != 4: raise SomeError() > for p, expected in zip (parts[-1], ('#a','b','c')): > name, x, value = p.partition ('=') > if name != expected or x != '=': > raise SomeError() > values.append (value) > print values[0], values[1], values[2] I call straw man: [tested] line = "#a=valuea;b=valueb;c=valuec;" d = dict(single.split('=', 1) for single in line.split(';') if single) d['#a'], d['b'], d['c'] If you want checking code, add: if len(d) != 3: raise ValueError('Too many keys: %s in %r)' % ( sorted(d), line)) > Blech, not in my book. The regex checks the > format of the string, extracts the values, and > does so very clearly. Further, it is easily > adapted to other similar formats, or evolutionary > changes in format. It is also (once one is > familiar with regexes -- a useful skill outside > of Python too) easier to get right (at least in > a simple case like this.) The posted regex doesn't work; this might be homework, so I'll not fix the two problems. The fact that you did not see the failure weakens your claim of "does so very clearly." --Scott David Daniels Scott.Daniels at Acm.Org From pfeldman at verizon.net Fri Jul 24 11:58:36 2009 From: pfeldman at verizon.net (Phillip M. Feldman) Date: Fri, 24 Jul 2009 10:58:36 -0500 (CDT) Subject: len() should always return something Message-ID: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> An HTML attachment was scrubbed... URL: From Scott.Daniels at Acm.Org Fri Jul 24 12:06:54 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 24 Jul 2009 09:06:54 -0700 Subject: Predefined Variables In-Reply-To: References: Message-ID: Stephen Cuppett (should have written in this order): > "Fred Atkinson" wrote ... >> Is there a pre-defined variable that returns the GET line >> >> (http://www.php.net/index.php?everythingafterthequestionmark) as a >> single variable (rather than individual variables)? > os.environment('QUERY_STRING') Maybe you mean: os.environ['USER'] --Scott David Daniels Scott.Daniels at Acm.Org From timlee126 at yahoo.com Fri Jul 24 12:24:44 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 24 Jul 2009 09:24:44 -0700 (PDT) Subject: Popen Message-ID: <987525.41038.qm@web63102.mail.re1.yahoo.com> Thanks! Yes I mean subprocess.Popen. I was wondering the meaning of "asynchronously" Here is some code I am reading recently: " result = Popen(cmdline,shell=True,stdout=PIPE).stdout for line in result.readlines(): if find(line,"Cross") != -1: return float(split(line)[-1][0:-1]) " The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. So how to understand the meaning of "asynchronous"? Thanks and regards! --- On Fri, 7/24/09, Kushal Kumaran wrote: > From: Kushal Kumaran > Subject: Re: Popen > To: "Tim" > Cc: python-list at python.org > Date: Friday, July 24, 2009, 10:58 AM > On Fri, Jul 24, 2009 at 7:33 PM, > Tim > wrote: > > > > Hi, > > I wonder if I use Popen, the parent process will wait > for the child process to finish or continue without > waiting? > > Thanks and regards! > > > > Assuming you mean subprocess.Popen, the child is executed > asynchronously.? You can use the wait() method on the > Popen object if > you want the parent to wait for the child to finish. > > -- > kushal > From deets at nospam.web.de Fri Jul 24 12:35:56 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 18:35:56 +0200 Subject: Popen In-Reply-To: References: Message-ID: <7cu67cF29g680U1@mid.uni-berlin.de> Tim schrieb: > Thanks! > Yes I mean subprocess.Popen. > > I was wondering the meaning of "asynchronously" > Here is some code I am reading recently: > " > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > if find(line,"Cross") != -1: > return float(split(line)[-1][0:-1]) > " > The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. > > "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. > However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. > > So how to understand the meaning of "asynchronous"? "Asynchronous" means asynchronous - the parent is *not* waiting. Which is the reason that there is (amongst other things) the "wait"-method you can call to wait for the child to be terminated. Diez From bieffe62 at gmail.com Fri Jul 24 12:40:22 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 24 Jul 2009 09:40:22 -0700 (PDT) Subject: Popen References: Message-ID: <7df4ca4e-476a-4b3c-99d4-e710c75ab80b@o15g2000yqm.googlegroups.com> On Jul 24, 6:24?pm, Tim wrote: > Thanks! > Yes I mean subprocess.Popen. > > I was wondering the meaning of "asynchronously" > Here is some code I am reading recently: > " > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > ? ? if find(line,"Cross") != -1: > ? ? ? ? return float(split(line)[-1][0:-1]) > " > The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. > > "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. This is correct > However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. > Calling result.readlines() the parent process attempts to read from stdout until end of file. For a pipe, end of file means that the other end is closing its connection, which - unless the child process closes stdout explicitely - means that the child process is terminated. So the end effect is the almost like using 'communicate' on the result of Popen, except that communicates returns both standard output and standard error contents at the same time. > So how to understand the meaning of "asynchronous"? > > Thanks and regards! HTH Ciao ----- FB From mathieu.malaterre at gmail.com Fri Jul 24 12:48:22 2009 From: mathieu.malaterre at gmail.com (mathieu) Date: Fri, 24 Jul 2009 09:48:22 -0700 (PDT) Subject: SONAME for python modules is bad? (aka multiple module version) Message-ID: <2a2a36ca-72a0-470f-907c-2be43104617a@h18g2000yqj.googlegroups.com> As far as I know there has not been any consensus on how to install multiple version of a same module in python ? What are the recommended mechanism ? I could not find any documentation on the subject. Does anyone sees any issue with using standard SONAME mechanism when installing a python module ? Thanks, ref: http://mail.python.org/pipermail/pythonmac-sig/2009-January/020936.html From timlee126 at yahoo.com Fri Jul 24 12:52:01 2009 From: timlee126 at yahoo.com (Tim) Date: Fri, 24 Jul 2009 09:52:01 -0700 (PDT) Subject: Popen Message-ID: <414673.50429.qm@web63108.mail.re1.yahoo.com> Thanks! If that is the case, i.e. the parent doesn't wait, is the code in my last post wrong? "result" could be nothing. --- On Fri, 7/24/09, Diez B. Roggisch wrote: > From: Diez B. Roggisch > Subject: Re: Popen > To: python-list at python.org > Date: Friday, July 24, 2009, 12:35 PM > Tim schrieb: > > Thanks! Yes I mean subprocess.Popen. > > > > I was wondering the meaning of "asynchronously" > > Here is some code I am reading recently: > > " > > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > >? ???if find(line,"Cross") != -1: > >? ? ? ???return > float(split(line)[-1][0:-1]) " > > The computation in the program "cmdline" takes a long > time, at the end of which the results will be output to > stdout. > > > > "asynchronous" seems to mean Popen returns to the > parent process immediately and the parent and child > processes continue to be executed. > > However, if Popen returns immediately to the parent > process, then there will be nothing in "result", not to > mention extracting information from the output. Thus it > seems to me the parent process has to wait till the child > process finish. > > > > So how to understand the meaning of "asynchronous"? > > "Asynchronous" means asynchronous - the parent is *not* > waiting. > > Which is the reason that there is (amongst other things) > the "wait"-method you can call to wait for the child to be > terminated. > > Diez > -- http://mail.python.org/mailman/listinfo/python-list > From marcusw at cox.net Fri Jul 24 12:57:57 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 24 Jul 2009 12:57:57 -0400 Subject: any suggestions to synchronize typed text and speech ? In-Reply-To: References: Message-ID: On 7/21/2009 12:13 PM, Stef Mientki wrote: > hi Marcus, >>>> That sounds like a very specialized type of thing, >>> Well from an application point of view, >>> with the current netbooks, >>> this looks like a perfect tool for any conversation or meeting. >>>> which only the few people with experience with wxPython, PyAudio, >>>> and Scintilla could help you with... >>>> >>> I was afraid of that too, so I dropped the question in several places, >>> and the writer of Scintilla himself came with the perfect answer. >>> >>> cheers,Stef >>>> But you might try having a dictionary with notes and associated >>>> times, or just give each note a four-digit ID number at the >>>> beginning of it when it's entered and use that in the dictionary (to >>>> keep keys shorter). Or you could just do a little hack and increase >>>> the number of bookmarks allowed (seeing as source is available) :p >>>> >>>> Marcus >>> >> Glad you got a good answer from somebody. Sounds like an interesting >> project. About when would this be headed for a release? Could you post >> a link to a googlecode or sourceforge project or something so I can >> follow and/or help with development? >> > For the moment it's just an idea, so no line of code yet. > I first like to tackle all the problems, > at least to the level I think I can handle them. > So first solve the next problem, > before I start coding: > automatic synchronization (file uploading and deleting) between EEE-pc > and desktop PC over bluetooth. > And another problem, as my customers are physicians, > both the text and audio need to be stored encrypted. > > cheers, > Stef >> Marcus > I would recommend pybluez and http://www.google.com/search?q=python+aes+encryption Marcus From kyrie at uh.cu Fri Jul 24 13:00:38 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Fri, 24 Jul 2009 13:00:38 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: Message-ID: <200907241300.38738.kyrie@uh.cu> On Friday 24 July 2009 11:07:30 am Inky 788 wrote: > On Jul 23, 3:42?am, Hendrik van Rooyen > > if you think it is contrived, then please consider how you would > > keep track of say the colour of a pixel on a screen at position > > (x,y) - this is about the simplest "natural" tuple format and > > example. > > My guess is that this is probably the way most people do it: [...] > def make_array_of_pixels(num_columns, num_rows): [...] If you need to hold /every/ pixel on the screen, it makes sense to have a bidimentional array of points and colors. But if you need to hold a non-rectangular subset, or even a subset that doesn't need to be rectangular, you can either use dicts, or ... do what you did. Most people will use the less contrived version of: screen = {} screen[1,2] = (rand(), rand(), rand()) (btw, in your make_a_pixel function, unless you want to make the pixels updatable in place, you should also be using a tuple). Sparse datasets are extremely common, and dicts are a good way to [temporarily] store them. As someone mentioned before, dictionaries are essential to python. Don't go out of your way to avoid them, unless you have a /reason/ to do it. Btw, def get_color(point): return screen[point] is way more readable (and less obscure) than def get_color(point): return rows_of_pixels[point[0]][point[1]] Regards, -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From clp2 at rebertia.com Fri Jul 24 13:02:30 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 10:02:30 -0700 Subject: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox) In-Reply-To: References: Message-ID: <50697b2c0907241002o1cd9f262h853c2abe2ce534f6@mail.gmail.com> On Fri, Jul 24, 2009 at 2:38 AM, Piet van Oostrum wrote: >>>>>> Chris Rebert (CR) wrote: > >>CR> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: >>>> You can use tabnanny to help diagnose the problem: >>>> http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a > >>CR> Anyone know what's the deal with effbot.org? It seems to be broken at >>CR> present, forcing me to use Google's cached version. >>CR> It's a real shame since it has lots of handy Python info. > > Just try again. Yup, whatever the problem was, seems to be fixed now. Interesting. - Chris From roastie57 at gmail.com Fri Jul 24 13:14:31 2009 From: roastie57 at gmail.com (Uncle Roastie) Date: Fri, 24 Jul 2009 10:14:31 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <0505d29f-edeb-40a4-b513-6117ecf4de33@m18g2000vbi.googlegroups.com> On Jul 20, 12:27?pm, Phillip B Oldham wrote: > My colleagues and I have been working with python for around 6 months > now, and while we love a lot of what python has done for us and what > it enables us to do some of the decisions behind such certain > data-types and their related methods baffle us slightly (when compared > to the decisions made in other, similarly powerful languages). > > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > > Consider the following: > > >>> x = [2,1,3] > >>> x.sort() > >>> print x > > [1, 2, 3] > > Now, if the sort operations were unable to affect the original > structure of the list (as in JavaScript) you'd effectively have a > tuple which you could add/remove from, and the example above would > look more like: > > >>> x = [2,1,3] > >>> print x.sort() > [1, 2, 3] > >>> print x > > [2,1,3] > > This make a lot more sense to us, and follows the convention from > other languages. It would also mean chaining methods to manipulate > lists would be easier: > > >>> x = [2,1,3] > >>> print x.sort()[0] > 3 > >>> print x > > [2,1,3] > > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. > > We're not looking to start any arguments or religious wars and we're > not asking that python be changed into something its not. We'd simply > like to understand the decision behind the lists and tuple structures. > We feel that in not "getting" the difference between the two types we > may be missing out on using these data structures to their full > potential. A tuple can be used like a struct in C - the number of fields is meant to be fixed and should not be dynamically changed. From gert.cuykens at gmail.com Fri Jul 24 13:22:36 2009 From: gert.cuykens at gmail.com (gert) Date: Fri, 24 Jul 2009 10:22:36 -0700 (PDT) Subject: cgi.fieldstorage() Message-ID: this is a non standard way to store multi part post data on disk def application(environ, response): with open('/usr/httpd/var/wsgiTemp','w') as f: while True: chunk = environ['wsgi.input'].read(8192).decode('latin1') if not chunk: break f.write(chunk) response('200 OK',[]) return ['complete'] my question is how do i handle the file, so i can shuffle it into a db using small chunks of memorie ? From deets at nospam.web.de Fri Jul 24 13:27:16 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 19:27:16 +0200 Subject: SONAME for python modules is bad? (aka multiple module version) In-Reply-To: <2a2a36ca-72a0-470f-907c-2be43104617a@h18g2000yqj.googlegroups.com> References: <2a2a36ca-72a0-470f-907c-2be43104617a@h18g2000yqj.googlegroups.com> Message-ID: <7cu97kF29dnlbU1@mid.uni-berlin.de> mathieu schrieb: > As far as I know there has not been any consensus on how to install > multiple version of a same module in python ? What are the recommended > mechanism ? I use virtualenvs for everything. Especially on unixish OSes this usually works without problems. On windows, things are a bit different, as sometimes you only get binary installers that insist on installing into the base-installation. > I could not find any documentation on the subject. Does anyone sees > any issue with using standard SONAME mechanism when installing a > python module ? I don't understand that. You mean the .so.X.Y.Z-thingy under *ix? That would essentially be the pkg_resources.require-road, yes. But as it's not widely adopted, it will cause you troubles because some packages won't declare their dependencies properly. Diez From clp2 at rebertia.com Fri Jul 24 13:28:10 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 10:28:10 -0700 Subject: Popen In-Reply-To: <414673.50429.qm@web63108.mail.re1.yahoo.com> References: <414673.50429.qm@web63108.mail.re1.yahoo.com> Message-ID: <50697b2c0907241028s4f3ff1e6ma4f78ae2b61b2341@mail.gmail.com> > --- On Fri, 7/24/09, Diez B. Roggisch wrote: > >> From: Diez B. Roggisch >> Subject: Re: Popen >> To: python-list at python.org >> Date: Friday, July 24, 2009, 12:35 PM >> Tim schrieb: >> > Thanks! Yes I mean subprocess.Popen. >> > >> > I was wondering the meaning of "asynchronously" >> > Here is some code I am reading recently: >> > " >> > result = Popen(cmdline,shell=True,stdout=PIPE).stdout >> for line in result.readlines(): >> >? ???if find(line,"Cross") != -1: >> >? ? ? ???return >> float(split(line)[-1][0:-1]) " >> > The computation in the program "cmdline" takes a long >> time, at the end of which the results will be output to >> stdout. >> > >> > "asynchronous" seems to mean Popen returns to the >> parent process immediately and the parent and child >> processes continue to be executed. >> > However, if Popen returns immediately to the parent >> process, then there will be nothing in "result", not to >> mention extracting information from the output. Thus it >> seems to me the parent process has to wait till the child >> process finish. >> > >> > So how to understand the meaning of "asynchronous"? >> >> "Asynchronous" means asynchronous - the parent is *not* >> waiting. >> >> Which is the reason that there is (amongst other things) >> the "wait"-method you can call to wait for the child to be >> terminated. On Fri, Jul 24, 2009 at 9:52 AM, Tim wrote: > > Thanks! If that is the case, i.e. the parent doesn't wait, is the code in my last post wrong? "result" could be nothing. No, it will be a subprocess.Popen object representing the spawned process. Both the child process and the parent Python process will then be running simultaneously.This allows the Python program to interact with the subprocess via the Popen object while the subprocess is executing in parallel with Python. The asynchronicity means that the call to the Popen constructor does not wait for the spawned subprocess to terminate before returning the new Popen object to the parent Python program. Cheers, Chris -- http://blog.rebertia.com From deets at nospam.web.de Fri Jul 24 13:32:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 24 Jul 2009 19:32:31 +0200 Subject: cgi.fieldstorage() In-Reply-To: References: Message-ID: <7cu9hhF29gt65U1@mid.uni-berlin.de> gert schrieb: > this is a non standard way to store multi part post data on disk > > def application(environ, response): > with open('/usr/httpd/var/wsgiTemp','w') as f: > while True: > chunk = environ['wsgi.input'].read(8192).decode('latin1') > if not chunk: break > f.write(chunk) > response('200 OK',[]) > return ['complete'] > > my question is how do i handle the file, so i can shuffle it into a db > using small chunks of memorie ? I don't think that's possible with the current DB-API. There is no stream-based BLOB-interface (as e.g. JDBC offers). So the answer certainly depends on your used RDBMS. For oracle, you would be lucky: http://cx-oracle.sourceforge.net/html/lob.html Other adapters I don't know about. Diez From clp2 at rebertia.com Fri Jul 24 13:41:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 10:41:13 -0700 Subject: trouble with wrapping a c program In-Reply-To: <20090724123458.TOWM14607.viefep17-int.chello.at@edge02.upc.biz> References: <20090724123458.TOWM14607.viefep17-int.chello.at@edge02.upc.biz> Message-ID: <50697b2c0907241041l10d13f09pa01abb505461e737@mail.gmail.com> On Fri, Jul 24, 2009 at 5:34 AM, Sanne Korzec wrote: > Hi Mailing, > > I am using a c program, which first initializes for some seconds and then > waits for user input (keyboard) to type something. When enter is pressed the > c program continues. > Using the keyboard and then enter in the c program prompt works, but I wish > to do this from the python script by sending the string from the python > script. > > I am able to print the string from python with a print command or with a > stdout.write command. But this simply prints it to the prompt, the c program > does nothing with this printed string. > > Is there a way to pipe, stream, or send this string to the running c > program? > Here is a small fragment of my code: > > #initialization > > cmd = [a list of my program and arguments] > > subprocess.Popen(cmd)?? #starts the c program import subprocess cmd = [a list of my program and arguments] process = subprocess.Popen(cmd, stdin=subprocess.PIPE) #starts the c program line = raw_input("Please enter a line of input for the C program:") process.stdin.write(line) process.stdin.write("\n") You might want to study the docs for the subprocess module: http://docs.python.org/library/subprocess.html Cheers, Chris -- http://blog.rebertia.com From kyrie at uh.cu Fri Jul 24 13:46:10 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Fri, 24 Jul 2009 13:46:10 -0400 Subject: len() should always return something In-Reply-To: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <200907241346.10966.kyrie@uh.cu> On Friday 24 July 2009 11:58:36 am Phillip M. Feldman wrote: > I've been converting Matlab codes to Python.? In Matlab, a scalar is just a > one-by-one matrix and has a length of 1.? This convention seems no less > arbitrary to me than Python's convention that the concept of length is not > applicable to ints and floats. Are you sure it isn't? (as opposed to being tainted by matlab?). Almost everywhere, a scalar and a tuple are different things. How many elements are "inside" the number 7? If you assume that 7 is a matrix, why should it be a two-dimensional matrix? Why not a vector, or a three dimensional matrix instead? (I don't use matlab, but in octave, size(7) returns [1,1], not [1] or [1,1,1,1]). That seems even more arbitrary to me. Of course, in a language like Matlab, that assumes that everything is a matrix, it makes sense for scalars to be mapped to a useful matrices. But that assumption is not natural. Even pure math makes a difference between scalars and vectors. Matrix x Matrix multiplication is not always defined (even when the second matrix contains exactly one element), while Matrix x Scalar always is. (Octave, btw, will demote a two-dimensional 1x1 matrix to a scalar for Matrix multiplication, which may hide errors) If you are converting from matlab, I'd say you have biggest things to worry about. As you said, you can just replace the len function (even safer may be to do [1]). Assignment, for instance, is /very/ different. > If there is only a single measurement, it is reasonable to allow the calling > program to pass a scalar without wrapping it up into a list or array. I try to avoid those automatic conversions, on the long run, I believe that most of them make the code more confusing for the caller. If you feel that you must allow that, my suggestion would be to create a method that will ensure that what you receive is a list (or whatever) and coerce its argument if not, and call that one at the beginning of your methods. Regards, [1] this will define len for any object, not only int and floats. === def size(x): try: return len(x) except TypeError: return 1,1 === -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From rhodri at wildebst.demon.co.uk Fri Jul 24 13:52:41 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 18:52:41 +0100 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Fri, 24 Jul 2009 16:10:07 +0100, Piet van Oostrum wrote: >>>>>> "Rhodri James" (RJ) wrote: > >> RJ> On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards >> wrote: >>>> On 2009-07-24, Dr. Phillip M. Feldman wrote: >>>>> >>>>> Some aspects of the Python design are remarkably clever, while >>>>> others leave me perplexed. Here's an example of the latter: >>>>> Why does len() give an error when applied to an int or float? >>>>> len() should always return something; in particular, when >>>>> applied to a scalar, it should return a value of 1. >>>> >>>> If len(7) returned a value of 1, then wouldn't one expect 7[0] >>>> to be valid? It isn't, so you'd then have to redefine all >>>> types so that they are sequences that can be indexed. Sounds >>>> like a big mess to me... >>>> >>>> [Are there types for which len() returns a value that can't be >>>> indexed?] >>>> > >> RJ> Dictionaries. > >> RJ> Which doesn't make your point less valid. In fact I'd go so >> RJ> far as to argue that what len() gives you is the number of >> RJ> items in a container, so len(7) should return 0. > > But len(7) could as well be defined as 3, 1, 32, or 64 (depending on the > implementation). Therefore it doesn't make much sense. Quite. -- Rhodri James *-* Wildebeest Herder to the Masses From andre.roberge at gmail.com Fri Jul 24 13:54:21 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Fri, 24 Jul 2009 10:54:21 -0700 (PDT) Subject: ElementTree's Element substitution in Python 3 Message-ID: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> I have a function to replace the content of an ElementTree Element by that of another one which works using Python 2 but not with Python 3. I get an assertion error. The function is as follows: def replace_element(elem, replacement): '''replace the content of an ElementTree Element by that of another one. ''' elem.clear() elem.text = replacement.text elem.tail = replacement.tail elem.tag = replacement.tag elem.attrib = replacement.attrib elem[:] = replacement[:] The last line is problematic. For example, if I do the following program with Python2.5 ### from xml.etree import ElementTree as et a = et.Element('a') b = et.SubElement(a, 'b') c = et.Element('c') a[:] = c[:] ### nothing of note happens - however, doing the same with Python 3.1, I get the following traceback: Traceback (most recent call last): File "test.py", line 7, in a[:] = c[:] File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line 210, in __setitem__ assert iselement(element) AssertionError ====== I would gladly welcome any suggestion for writing a replace_element() function that works with Python 3.1 Andr? From ronn.ross at gmail.com Fri Jul 24 13:56:29 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Fri, 24 Jul 2009 12:56:29 -0500 Subject: trouble with minidom In-Reply-To: References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> Message-ID: <9c8c445f0907241056x11ec5316s2ea6310872cd342@mail.gmail.com> On Tue, Jul 21, 2009 at 7:32 PM, Gabriel Genellina wrote: > En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross > escribi?: > > > Hello I'm trying to read an xml file using minidome. The xml looks like: >> >> >> myProj >> /here/ >> >> >> >> My code looks like so: >> from xml.dom.minidom import parse >> >> dom = parse("myfile.xml") >> >> for node in dom.getElementsByTagName("project'): >> print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue, >> node.childNodes[1]) >> >> Unfortunately, it returns 'nodeValue as none. I'm trying to read the value >> out of the node fir example name: myProj. I haven't found much help in the >> documentation. Can someone point me in the right direction? >> > > Unless you have a specific reason to use the DOM interface (like having a > masochistic mind), working with ElementTree usually is a lot easier: > > py> import xml.etree.ElementTree as ET > py> xml = """ > ... > ... myProj > ... /here/ > ... > ... """ > py> doc = ET.fromstring(xml) > py> for project in doc.findall('project'): > ... for child in project.getchildren(): > ... print child.tag, child.text > ... > name myProj > path /here/ > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > I have used the loop below and it works great, but I need get both child elements or 'project' per iteration. I want to build a dictionary that resemble this: my_dict = {'myProj':'/here/', 'anothername':'anotherpath'} I couldn't find how to do with in the element tree docs. Can you point me in the right direction? -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Fri Jul 24 14:03:59 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 11:03:59 -0700 Subject: len() should always return something In-Reply-To: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <50697b2c0907241103v5ad12d48re1857532c58903e8@mail.gmail.com> > Jul 24, 2009 07:02:29 AM, clp2 at rebertia.com wrote: > > On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. > Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while others >> leave >> me perplexed. Here's an example of the latter: Why does len() give an >> error >> when applied to an int or float? len() should always return something; in >> particular, when applied to a scalar, it should return a value of 1. Of >> course, I can define my own function like this: >> >> def mylen(x): >> ? if isinstance(x,int) or isinstance(x,float): return 1 >> ? return len(x) >> >> But, this shouldn't be necessary. > > The problem is that redefining len()/length/size that way would > violate several principles of Python's design (The "Zen" of Python - > http://www.python.org/dev/peps/pep-0020/). > > Specifically: > - Explicit is better than implicit. > - Special cases aren't special enough to break the rules. > - Errors should never pass silently. > - In the face of ambiguity, refuse the temptation to guess. > > If you'd explain the situation that prompts you to find this > redefinition necessary, I'm sure someone can suggest a better > approach. On Fri, Jul 24, 2009 at 8:58 AM, Phillip M. Feldman wrote: > I've read the "Zen of Python", but most of these aphorisms are vague and > could be understood differently by different readers. In particular, I > don't understand the statement that "explicit is better than implicit". > Some examples of this would be helpful. > > I've been converting Matlab codes to Python. In Matlab, a scalar is just a > one-by-one matrix and has a length of 1. This convention seems no less > arbitrary to me than Python's convention that the concept of length is not > applicable to ints and floats. My workaround was to write the following > function: > > def is_scalar(x): > """Return True if x is an instance of int, float, or complex. > Otherwise, return False. Note: If x is a length-1 list or array > containing an int, float, or complex value, False is returned.""" > if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex): > return True > return False > > The application is the following: In various types of scientific > applications, one operates on a list of measurements. If there is only a > single measurement, it is reasonable to allow the calling program to pass a > scalar without wrapping it up into a list or array. You could use Python's extended call syntax when defining your function: def average(*args): return sum(args) / len(args) average(7) #==> 7 average(2,3,4,5,6) #==> 4 average(*[2,3,4,5,6]) #==> 4 average([2,3,4,5,6]) #==> error Cheers, Chris -- http://blog.rebertia.com From rhodri at wildebst.demon.co.uk Fri Jul 24 14:05:58 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 24 Jul 2009 19:05:58 +0100 Subject: non-owning references? In-Reply-To: References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: On Fri, 24 Jul 2009 16:03:58 +0100, Piet van Oostrum wrote: >>>>>> "Rhodri James" (RJ) wrote: > >> RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic >> wrote: >>>> Ben Finney writes: >>>> >>>>> Utpal Sarkar writes: >>>>> >>>>>> Is there a way I can tell a variable that the object it is pointing >>>>>> too is not owned by it, in the sense that if it is the only >>>>>> reference >>>>>> to the object it can be garbage collected? >>>>> >>>>> Python doesn't have ?pointers?, and doesn't really have ?variables? >>>>> either, at least not how many other languages use that term. >>>> >>>> The OP didn't use the term "pointer", but the word "pointing", which >>>> makes sense in the context. The term "variable" is used in the Python >>>> language reference and elsewhere, and is quite compatible with how >>>> other >>>> popular languages (Java, PHP, Lisp, ...) use it. > >> RJ> Only superficially. Treating Python variables the same as C >> variables >> RJ> (say) is one of the classic ways that newbies come unstuck when >> mutable >> RJ> objects appear on the scene. While the OP appears to have the >> right idea, >> RJ> your "correction" here could be quite misleading. > > If you read the OP, it is clear that he talked about a class variable, > which is a perfectly legal notion in Python, and is mentioned as such in > the language reference manual: > `Variables defined in the class definition are class variables' Yes. I didn't think I needed to say that explicitly. > And who was talking about C variables? Hrvoje, implicitly. 'The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it.' I listed C as another example of a popular language because I am very familiar with how C's variables work; I don't know Java, I've never programmed PHP in anger and it's twenty years since I last touched Lisp. The point was, and remains, that this newsgroup gets regular traffic from people who expect Python's variables to act like C's variables, demonstrating that describing them as "quite compatible" is somewhat misleading. -- Rhodri James *-* Wildebeest Herder to the Masses From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Fri Jul 24 14:06:32 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Fri, 24 Jul 2009 14:06:32 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-23 23:51:02 -0400, Carl Banks said: > On Jul 23, 5:52?pm, Rui Maciel wrote: >> fft1976 wrote: >>> How do you explain that something as inferior as Python beat Lisp in >>> the market place despite starting 40 years later. >> >> Probably due to similar reasons that lead php to become remotely relevant > . > > Well, the only reason PHP became relevant because it was an > easy ^^^^^^^^ ^^^^^^^^ (emphasis added) > to > deploy solution in a single application domain, the web, that happened > to explode. i.e., Python "beat" lisp because it is ~70% of lisp in a form that is much more palatable to the average programmer, just as php became popular because it is powerful enough to do websites and, most importantly, apprehensible to mediocre programmers and even some non-programmers. -- Raffael Cavallaro From gert.cuykens at gmail.com Fri Jul 24 14:10:17 2009 From: gert.cuykens at gmail.com (gert) Date: Fri, 24 Jul 2009 11:10:17 -0700 (PDT) Subject: cgi.fieldstorage() References: <7cu9hhF29gt65U1@mid.uni-berlin.de> Message-ID: On Jul 24, 7:32?pm, "Diez B. Roggisch" wrote: > gert schrieb: > > > this is a non standard way to store multi part post data on disk > > > def application(environ, response): > > ? ? with open('/usr/httpd/var/wsgiTemp','w') as f: > > ? ? ? ? while True: > > ? ? ? ? ? ? chunk = environ['wsgi.input'].read(8192).decode('latin1') > > ? ? ? ? ? ? if not chunk: break > > ? ? ? ? ? ? f.write(chunk) > > ? ? response('200 OK',[]) > > ? ? return ['complete'] > > > my question is how do i handle the file, so i can shuffle it into a db > > using small chunks of memorie ? > > I don't think that's possible with the current DB-API. There is no > stream-based BLOB-interface (as e.g. JDBC offers). > > So the answer certainly depends on your used RDBMS. For oracle, you > would be lucky: > > http://cx-oracle.sourceforge.net/html/lob.html > > Other adapters I don't know about. sqlite :) ok let say for now it would be impossible on a db level, but before i reach the impossible, i still need to parse the file to prepare the chunks. How do i do that ? How do i get the chunks without loading the hole file into memorie ? b = environ['CONTENT_TYPE'].split('boundary=')[1] data = search(b+r'.*?Content-Type: application/octet-stream\r\n\r \n (.*?)\r\n--'+b,t,DOTALL).group(1) data = data.encode('latin1') From jakecjacobson at gmail.com Fri Jul 24 14:24:58 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Fri, 24 Jul 2009 11:24:58 -0700 (PDT) Subject: exceptions.TypeError an integer is required Message-ID: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> I am trying to do a post to a REST API over HTTPS and requires the script to pass a cert to the server. I am getting "exceptions.TypeError an integer is required" error and can't find the reason. I commenting out the lines of code, it is happening on the connection.request() line. Here is the problem code. Would love some help if possible. head = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"} parameters = urlencode({"collection" : collection, "entryxml" : open (file,'r').read()}) try: connection = httplib.HTTPSConnection(host, port, key_file, cert_file) connection.request('POST', path, parameters, head) response = connection.getresponse() print response.status, response.reason except: print sys.exc_type, sys.exc_value connection.close() From piet at cs.uu.nl Fri Jul 24 14:39:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 20:39:46 +0200 Subject: Predefined Variables References: Message-ID: >>>>> Scott David Daniels (SDD) wrote: >SDD> Stephen Cuppett (should have written in this order): >>> "Fred Atkinson" wrote ... >>>> Is there a pre-defined variable that returns the GET line >>>> >>>> (http://www.php.net/index.php?everythingafterthequestionmark) as a >>>> single variable (rather than individual variables)? >>> os.environment('QUERY_STRING') >SDD> Maybe you mean: >SDD> os.environ['USER'] Let's take the best of both: os.environ['QUERY_STRING'] -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From maymunbeyin at gmail.com Fri Jul 24 14:51:54 2009 From: maymunbeyin at gmail.com (kk) Date: Fri, 24 Jul 2009 11:51:54 -0700 (PDT) Subject: How can I get the line number ? Message-ID: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> Hello I am writing some Python code that runs in another application(has wrapper functions). Due to lack of debugging I am printing out alot of outputs and manual messages. I want to be able to create a function that would let me print the current line number that is called from. This is not for debugging exceptions it is rather to simplify my debug messages, at least I can trace my debug messages. thanks From tjreedy at udel.edu Fri Jul 24 14:52:38 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 14:52:38 -0400 Subject: len() should always return something In-Reply-To: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: Phillip M. Feldman wrote: > I've been converting Matlab codes to Python. In Matlab, a scalar is > just a one-by-one matrix and has a length of 1. This convention seems > no less arbitrary to me than Python's convention that the concept of > length is not applicable to ints and floats. Multiplication of a vector/matrix by a scalar always defined and commutative. Multiplication of a vector/matrix by a 1x1 matrix is not always even defined. So not having scalars in a matrix package strikes me as a bit odd. > My workaround was to write > the following function: > > def is_scalar(x): > """Return True if x is an instance of int, float, or complex. > Otherwise, return False. Note: If x is a length-1 list or array > containing an int, float, or complex value, False is returned.""" > if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex): Better: if isinstance(x, (int, float, complex)): but you forgot decimals and fractions and any other possible number modules. In 3.1, >>> from numbers import Number >>> from decimal import Decimal >>> from fractions import Fraction >>> for x in 1, 1.0, (1+0j), Decimal(1), Fraction(1,1): isinstance(x, Number) True True True True True and the same for any other module that registers a class as a Number > return True > return False > > The application is the following: In various types of scientific > applications, one operates on a list of measurements. If there is only > a single measurement, it is reasonable to allow the calling program to > pass a scalar without wrapping it up into a list or array. If you want to do that, start with def f(x): try: len(x) except TypeError: x = x, or in 3.1 use Number test above. Terry Jan Reedy From mkhitrov at gmail.com Fri Jul 24 14:59:04 2009 From: mkhitrov at gmail.com (Maxim Khitrov) Date: Fri, 24 Jul 2009 14:59:04 -0400 Subject: How can I get the line number ? In-Reply-To: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> References: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> Message-ID: <26ddd1750907241159q3799e4c7k5a883d3b45feb4a5@mail.gmail.com> On Fri, Jul 24, 2009 at 2:51 PM, kk wrote: > Hello > > I am writing some Python code that runs in another application(has > wrapper functions). Due to lack of debugging I am printing out alot of > outputs and manual messages. I want to be able to create a function > that would let me print the current line number that is called from. > This is not for debugging exceptions it is rather to simplify my debug > messages, at least I can trace my debug messages. > > thanks Modify the following as needed: from inspect import currentframe, getframeinfo def caller_info(depth=0): """ Get file, line number, and name of the calling function. """ if depth < 0: raise ValueError('invalid stack depth') caller = frame = currentframe() try: for i in xrange(-1, depth): caller = caller.f_back if caller is None: return (None, None, None) return getframeinfo(caller, 0)[:3] finally: del caller, frame - Max From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 14:59:40 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 18:59:40 GMT Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <0279f596$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >> Nah. 7 contains three bits, so len(7) should *clearly* return 3. > > and len("7") must return 8, by the same token... but wait! > > >>> len("7") > 1 > >>> > >>> > my python installation must me outdated ;-) No no no, you're obviously using an awesome version of Python that can compress single-character strings to a single bit! -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:00:14 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:00:14 GMT Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: <0279f5b9$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 15:55:45 +0200, Hrvoje Niksic wrote: > The term "variable" is used in the Python > language reference and elsewhere, and is quite compatible with how other > popular languages (Java, PHP, Lisp, ...) use it. Please stop > complaining about valid terminology; it is not helpful. No, the use of the single term "variable" to describe two distinct program models is not helpful. Whether other languages muddy the water between memory-location based variables and name-binding is irrelevant to whether we should do so. And quite a few of us are disappointed that the Python language reference should confuse the issue by using misleading terminology. Unfortunately, the use of "variable" is so ingrained, and so simple compared to name binding terminology, that I fear we'll never eradicate it. I know sometimes I use it myself, but always with a little shiver of shame that I'm misusing terminology. -- Steven From robert.kern at gmail.com Fri Jul 24 15:01:47 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 24 Jul 2009 14:01:47 -0500 Subject: Convert points to polygon shapefile In-Reply-To: <20090724101638.E93B414B2A@smtp3.ualg.pt> References: <20090724101638.E93B414B2A@smtp3.ualg.pt> Message-ID: On 2009-07-24 05:21, Luis Pedro Almeida wrote: > Dear all, > > I would like to know how to convert a list of points into a polygon > shapefile (esri). shapelib has Python bindings. http://shapelib.maptools.org/ -- 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 roy at panix.com Fri Jul 24 15:03:29 2009 From: roy at panix.com (Roy Smith) Date: Fri, 24 Jul 2009 15:03:29 -0400 Subject: list vs. tuple [Re: len() should always return something] References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: In article , Terry Reedy wrote: > Better: if isinstance(x, (int, float, complex)): I never noticed this before, but it seems odd that the second argument to isinstance() should be a tuple. Using the normal arguments made about tuples vs. lists, it seems like a list would be the right data structure here. I suppose a set would be even more right, but (I'm pretty sure) isinstance() predates sets. I'm curious why a tuple was chosen. From roy at panix.com Fri Jul 24 15:04:55 2009 From: roy at panix.com (Roy Smith) Date: Fri, 24 Jul 2009 15:04:55 -0400 Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, Steven D'Aprano wrote: > On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: > > >> Nah. 7 contains three bits, so len(7) should *clearly* return 3. > > > > and len("7") must return 8, by the same token... but wait! > > > > >>> len("7") > > 1 > > >>> > > >>> > > my python installation must me outdated ;-) > > No no no, you're obviously using an awesome version of Python that can > compress single-character strings to a single bit! Compressing strings to a single bit is easy. It's the uncompressing that's tricky. From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:05:11 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:05:11 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> Message-ID: <0279f6df$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 00:02:28 -0700, Chris Rebert wrote: > On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. > Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while others >> leave me perplexed. Here's an example of the latter: Why does len() >> give an error when applied to an int or float? len() should always >> return something; in particular, when applied to a scalar, it should >> return a value of 1. Of course, I can define my own function like this: >> >> def mylen(x): >> ? if isinstance(x,int) or isinstance(x,float): return 1 return len(x) >> >> But, this shouldn't be necessary. > > The problem is that redefining len()/length/size that way would violate > several principles of Python's design (The "Zen" of Python - > http://www.python.org/dev/peps/pep-0020/). > > Specifically: > - Explicit is better than implicit. > - Special cases aren't special enough to break the rules. > - Errors should never pass silently. > - In the face of ambiguity, refuse the temptation to guess. Chris, I'm curious why you think that these Zen are relevant to the OP's complaint. Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). Arguably (I wouldn't argue this, but some people might) ints aren't "special enough" to break the rule that len(obj) should always return something. (I don't actually agree, but some people might be able to produce a coherent argument why len() should apply equally to all objects.) Re errors passing silently, the OP doesn't believe that len(42) should be an error, so that's not relevant. And there's nothing ambiguous about len(42). I agree with the current Python behaviour, but I don't think there's anything in the Zen to support it. As far as I know, there is no programming language which treats scalars like ints as if they were vectors of length 1, which makes Python's choice to make ints unlengthed a no-brainer. -- Steven From andre.roberge at gmail.com Fri Jul 24 15:07:32 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Fri, 24 Jul 2009 12:07:32 -0700 (PDT) Subject: ElementTree's Element substitution in Python 3 References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: <9923e1c2-aabe-40d3-9c55-830991ad2dc9@q11g2000yqi.googlegroups.com> Sorry for replying to myself ... the following seems to be a working solution to my original problem. On Jul 24, 2:54?pm, Andr? wrote: > I have a function to replace the content of an ElementTree Element by > that of another one which works using Python 2 but not with Python 3. > I get an assertion error. ?The function is as follows: > > def replace_element(elem, replacement): > ? ? '''replace the content of an ElementTree Element by that of > another > ? ? ? ?one. > ? ? ''' > ? ? elem.clear() > ? ? elem.text = replacement.text > ? ? elem.tail = replacement.tail > ? ? elem.tag = replacement.tag > ? ? elem.attrib = replacement.attrib > ? ? elem[:] = replacement[:] > Use instead: def replace_element(elem, replacement): '''replace the content of an ElementTree Element by that of another one. ''' elem.clear() elem.text = replacement.text elem.tail = replacement.tail elem.tag = replacement.tag elem.attrib = replacement.attrib try: elem[:] = replacement[:] # works with Python 2.x (fast) but not 3.x except AssertionError: del elem[:] for child in replacement: elem.append(child) Andr? From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:11:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:11:33 GMT Subject: exceptions.TypeError an integer is required References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> Message-ID: <0279f85d$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 11:24:58 -0700, jakecjacobson wrote: > I am trying to do a post to a REST API over HTTPS and requires the > script to pass a cert to the server. I am getting "exceptions.TypeError > an integer is required" error and can't find the reason. I commenting > out the lines of code, it is happening on the connection.request() line. > Here is the problem code. Would love some help if possible. Please post the traceback that you get. My guess is that you are passing a string instead of an integer, probably for the port. [...] > except: > print sys.exc_type, sys.exc_value As a general rule, a bare except of that fashion is bad practice. Unless you can explain why it is normally bad practice, *and* why your case is an exception (no pun intended) to the rule "never use bare except clauses", I suggest you either: * replace "except:" with "except Exception:" instead. * better still, re-write the entire try block as: try: [code goes here] finally: connection.close() and use the Python error-reporting mechanism instead of defeating it. -- Steven From piet at cs.uu.nl Fri Jul 24 15:17:10 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 24 Jul 2009 21:17:10 +0200 Subject: ElementTree's Element substitution in Python 3 References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: >>>>> Andr? (A) a ?crit: >A> I have a function to replace the content of an ElementTree Element by >A> that of another one which works using Python 2 but not with Python 3. >A> I get an assertion error. The function is as follows: >A> def replace_element(elem, replacement): >A> '''replace the content of an ElementTree Element by that of >A> another >A> one. >A> ''' >A> elem.clear() >A> elem.text = replacement.text >A> elem.tail = replacement.tail >A> elem.tag = replacement.tag >A> elem.attrib = replacement.attrib >A> elem[:] = replacement[:] >A> The last line is problematic. For example, if I do the following >A> program with Python2.5 >A> ### >A> from xml.etree import ElementTree as et >A> a = et.Element('a') >A> b = et.SubElement(a, 'b') >A> c = et.Element('c') >A> a[:] = c[:] >A> ### >A> nothing of note happens - however, doing the same with Python 3.1, I >A> get the following traceback: >A> Traceback (most recent call last): >A> File "test.py", line 7, in >A> a[:] = c[:] >A> File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line >A> 210, in __setitem__ >A> assert iselement(element) >A> AssertionError This is a Python bug. Please report it. The problem is that in Python 3 slice assignments are done with __setitem__ rather than __setslice__ but ElementTree has not been adapted to that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From tjreedy at udel.edu Fri Jul 24 15:17:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 15:17:15 -0400 Subject: Adding method from one class to another class or to instance of another class In-Reply-To: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> References: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Message-ID: marekw2143 wrote: > Hi, > > I have one class (A) that has defined method createVars. I would like > to add that method to class B > The code looks like this: > > > class A(object): > def createVars(self): > self.v1 = 1 > self.v2 = 3 > pass > > class B(object): > pass > > > I don't want to use inheritance (because class A has many methods > defined that class B doesn't need). > When I try the folloowing: > > > B.createVars = C.createVars you meant A.createVars > B().createVars() > > > then the following error occurs: > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got nothing instead) In 3.1, your example works fine. The difference is that in 2.x, B.createVars is a method wrapperthat wraps the function, whereas in 3.1, it is the function itself. For 2.x, you need to extract the function from the wrapper. It is im_func or something like that. Use dir(B.createVars) to check for sure. > How can I solve this problem? Terry Jan Reedy From linuxguy123 at gmail.com Fri Jul 24 15:17:24 2009 From: linuxguy123 at gmail.com (Linuxguy123) Date: Fri, 24 Jul 2009 13:17:24 -0600 Subject: Eclipse Pydev update error ? Message-ID: <1248463044.8697.50.camel@localhost.localdomain> Does anyone know why this error is occurring in my Eclipse Pydev update ? An error occurred while collecting items to be installed No repository found containing: org.python.pydev/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.ast/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.core/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.debug/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 No repository found containing: org.python.pydev.help/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.jython/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.parser/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.templates/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.customizations/osgi.bundle/1.4.7.2843 Thanks From steve at REMOVE-THIS-cybersource.com.au Fri Jul 24 15:18:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 24 Jul 2009 19:18:09 GMT Subject: list vs. tuple [Re: len() should always return something] References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <0279f9e9$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: > In article , > Terry Reedy wrote: > >> Better: if isinstance(x, (int, float, complex)): > > I never noticed this before, but it seems odd that the second argument > to isinstance() should be a tuple. Using the normal arguments made > about tuples vs. lists, it seems like a list would be the right data > structure here. What would be the point of using a list? You're never going to sort it, or append items to it, or otherwise mutate it. You build it, pass it to a function which doesn't modify it in any fashion, then it gets garbage collected. > I suppose a set would be even more right, but (I'm > pretty sure) isinstance() predates sets. Yes. [steve at sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> isinstance >>> set Traceback (innermost last): File "", line 1, in ? NameError: set > I'm curious why a tuple was chosen. Tuples are smaller and faster to build than lists -- they're the most lightweight sequence type in Python. You don't need all the extra functionality of lists, so why go to the time and effort of building a list? -- Steven From maymunbeyin at gmail.com Fri Jul 24 15:23:58 2009 From: maymunbeyin at gmail.com (kk) Date: Fri, 24 Jul 2009 12:23:58 -0700 (PDT) Subject: How can I get the line number ? References: <60183aee-c4e4-4200-8449-198fd2b08258@p23g2000vbl.googlegroups.com> Message-ID: <3ce2fe1a-c769-4f92-b4b0-559229158297@33g2000vbe.googlegroups.com> Maxim, Thank you so much. I will try right now. From fabiofz at gmail.com Fri Jul 24 15:43:36 2009 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Fri, 24 Jul 2009 16:43:36 -0300 Subject: Eclipse Pydev update error ? In-Reply-To: <1248463044.8697.50.camel@localhost.localdomain> References: <1248463044.8697.50.camel@localhost.localdomain> Message-ID: On Fri, Jul 24, 2009 at 4:17 PM, Linuxguy123 wrote: > Does anyone know why this error is occurring in my Eclipse Pydev > update ? > > An error occurred while collecting items to be installed > ?No repository found containing: > org.python.pydev/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.ast/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.core/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.debug/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 > ?No repository found containing: > org.python.pydev.help/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.jython/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.parser/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.templates/osgi.bundle/1.4.7.2843 > ?No repository found containing: > org.python.pydev.customizations/osgi.bundle/1.4.7.2843 > This usually happens if there was some connection error during the update. You can try other mirrors (see: http://pydev.blogspot.com/2009/07/pydev-147-released.html for the new added mirrors). Cheers, Fabio From tjreedy at udel.edu Fri Jul 24 15:45:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 15:45:36 -0400 Subject: ElementTree's Element substitution in Python 3 In-Reply-To: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: Andr? wrote: > I have a function to replace the content of an ElementTree Element by > that of another one which works using Python 2 but not with Python 3. > I get an assertion error. The function is as follows: > > def replace_element(elem, replacement): > '''replace the content of an ElementTree Element by that of > another > one. > ''' > elem.clear() > elem.text = replacement.text > elem.tail = replacement.tail > elem.tag = replacement.tag > elem.attrib = replacement.attrib > elem[:] = replacement[:] > > The last line is problematic. For example, if I do the following > program with Python2.5 > ### > from xml.etree import ElementTree as et > > a = et.Element('a') > b = et.SubElement(a, 'b') > c = et.Element('c') > > a[:] = c[:] > ### > nothing of note happens - however, doing the same with Python 3.1, I > get the following traceback: > > Traceback (most recent call last): > File "test.py", line 7, in > a[:] = c[:] > File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line > 210, in __setitem__ > assert iselement(element) > AssertionError > > ====== > I would gladly welcome any suggestion for writing a replace_element() > function that works with Python 3.1 My guess is that you found a subtle bug in the 3.1 version of ElementTree, perhap a result of conversion. I would take a look at the noted line 210 to see whether it is 'a' or 'c' that is not passing as an element. Assuming that it is 'c', I would look at __getitem__ to see why not. Is [:] special-cased? Compare the codes for 2.5 and 3.1. Assuming it still looks like a bug, report it on the tracker using your minimal example, leaving out your function. tjr From tjreedy at udel.edu Fri Jul 24 15:47:36 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 15:47:36 -0400 Subject: exceptions.TypeError an integer is required In-Reply-To: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> Message-ID: jakecjacobson wrote: > I am trying to do a post to a REST API over HTTPS and requires the > script to pass a cert to the server. I am getting > "exceptions.TypeError an integer is required" error and can't find the > reason. I commenting out the lines of code, it is happening on the > connection.request() line. Here is the problem code. Would love some > help if possible. > > head = {"Content-Type" : "application/x-www-form-urlencoded", > "Accept" : "text/plain"} > parameters = urlencode({"collection" : collection, "entryxml" : open > (file,'r').read()}) > try: > connection = httplib.HTTPSConnection(host, port, key_file, > cert_file) > connection.request('POST', path, parameters, head) > response = connection.getresponse() > print response.status, response.reason > except: > print sys.exc_type, sys.exc_value > > connection.close() Help us help you by posting the full actual traceback. From andre.roberge at gmail.com Fri Jul 24 15:48:02 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Fri, 24 Jul 2009 12:48:02 -0700 (PDT) Subject: ElementTree's Element substitution in Python 3 References: <72c9fbf8-88a0-4d62-86fa-44e450ef7272@k1g2000yqf.googlegroups.com> Message-ID: <7ea510e0-ceb8-4425-9af2-b30154ad6e6d@b14g2000yqd.googlegroups.com> On Jul 24, 4:17?pm, Piet van Oostrum wrote: > >>>>> Andr? (A) a ?crit: > >A> I have a function to replace the content of an ElementTree Element by > >A> that of another one which works using Python 2 but not with Python 3. > >A> I get an assertion error. [SNIP] > >A> Traceback (most recent call last): > >A> ? File "test.py", line 7, in > >A> ? ? a[:] = c[:] > >A> ? File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line > >A> 210, in __setitem__ > >A> ? ? assert iselement(element) > >A> AssertionError > > This is a Python bug. Please report it. Done. > The problem is that in Python 3 > slice assignments are done with __setitem__ rather than __setslice__ but > ElementTree has not been adapted to that. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org From linuxguy123 at gmail.com Fri Jul 24 15:57:40 2009 From: linuxguy123 at gmail.com (Linuxguy123) Date: Fri, 24 Jul 2009 13:57:40 -0600 Subject: Eclipse Pydev update error ? In-Reply-To: References: <1248463044.8697.50.camel@localhost.localdomain> Message-ID: <1248465460.8697.51.camel@localhost.localdomain> On Fri, 2009-07-24 at 16:43 -0300, Fabio Zadrozny wrote: > On Fri, Jul 24, 2009 at 4:17 PM, Linuxguy123 wrote: > > Does anyone know why this error is occurring in my Eclipse Pydev > > update ? > > > > An error occurred while collecting items to be installed > > No repository found containing: > > org.python.pydev/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.ast/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.core/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.debug/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 > > No repository found containing: > > org.python.pydev.help/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.jython/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.parser/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.templates/osgi.bundle/1.4.7.2843 > > No repository found containing: > > org.python.pydev.customizations/osgi.bundle/1.4.7.2843 > > > > This usually happens if there was some connection error during the > update. You can try other mirrors (see: > http://pydev.blogspot.com/2009/07/pydev-147-released.html for the new > added mirrors). Excellent reply. Thanks. I added http://fabioz.com/pydev/updates to my site list and the update completed immediately. I'll bookmark the blog as well. Thanks again. From clp2 at rebertia.com Fri Jul 24 16:02:24 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 13:02:24 -0700 Subject: len() should always return something In-Reply-To: <0279f6df$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: <50697b2c0907241302m166d0c61y4912a29aaa4caaf1@mail.gmail.com> On Fri, Jul 24, 2009 at 12:05 PM, Steven D'Aprano wrote: > On Fri, 24 Jul 2009 00:02:28 -0700, Chris Rebert wrote: > >> On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. >> Feldman wrote: >>> >>> Some aspects of the Python design are remarkably clever, while others >>> leave me perplexed. Here's an example of the latter: Why does len() >>> give an error when applied to an int or float? len() should always >>> return something; in particular, when applied to a scalar, it should >>> return a value of 1. Of course, I can define my own function like this: >>> >>> def mylen(x): >>> ? if isinstance(x,int) or isinstance(x,float): return 1 return len(x) >>> >>> But, this shouldn't be necessary. >> >> The problem is that redefining len()/length/size that way would violate >> several principles of Python's design (The "Zen" of Python - >> http://www.python.org/dev/peps/pep-0020/). >> >> Specifically: >> - Explicit is better than implicit. >> - Special cases aren't special enough to break the rules. >> - Errors should never pass silently. >> - In the face of ambiguity, refuse the temptation to guess. > > > Chris, I'm curious why you think that these Zen are relevant to the OP's > complaint. To explain in more detail: > Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). If you want a collection (something that has length), then one should explicitly create one, not implicitly have a singleton value act like it's a pseudo-collection. I admit I'm somewhat conflating this principle with the anti-ambiguity principle, but the two are related, imho. > Arguably (I wouldn't argue this, but some people might) ints aren't > "special enough" to break the rule that len(obj) should always return > something. Except that's not the current rule. The current rule is that it's defined only for collections. One would instead have to argue why ints are special enough to have len() defined despite not being collections. I think the point made by Grant Edwards is instructive. len(x) = 1 typically implies list(x)[0] and similar should be valid. Altering the behavior would invalidate that theorem and cause quite a bit of code upheaval, all just to save the OP from typing one pair of []s. > (I don't actually agree, but some people might be able to produce a > coherent argument why len() should apply equally to all objects.) Well, yes, this /whole/ "argument" is entirely academic; the behavior is extremely unlikely to change, we're just trying to give ex post facto rationales for pedagogical purposes. :) > Re errors passing silently, the OP doesn't believe that len(42) should be > an error, so that's not relevant. True, it would not directly silence an error, but defining len() on scalars would tend towards obscuring errors in code that incorrectly treats scalars as collections. > And there's nothing ambiguous about len(42). Really? What is its value then? I think arguments of varying quality can be made for: 1 - as the OP and those from array programming languages would suggest 2 - the number of decimal digits in 42, if one was feeling Perlish 6 - the minimum number of bits necessary to represent 42 in binary 32 (or 64, depending on your CPU) - the number of bits necessary to represent an int (obviously breaks down a bit with arbitrary-magnitude ints) undefined - i.e. it causes an error, the current behavior; asking for the length of a non-collection is "nonsensical" or "absurd" > I agree with the current Python behaviour, but I don't think there's > anything in the Zen to support it. As far as I know, there is no The problem and strength of the Zen is that it's all about how you interpret it. :-) Cheers, Chris -- http://blog.rebertia.com From marcusw at cox.net Fri Jul 24 16:09:15 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 24 Jul 2009 16:09:15 -0400 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 7/24/2009 3:04 PM, Roy Smith wrote: > In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, > Steven D'Aprano wrote: > >> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >> >>>> Nah. 7 contains three bits, so len(7) should *clearly* return 3. >>> and len("7") must return 8, by the same token... but wait! >>> >>> >>> len("7") >>> 1 >>> >>> >>> >>> >>> my python installation must me outdated ;-) >> No no no, you're obviously using an awesome version of Python that can >> compress single-character strings to a single bit! > > Compressing strings to a single bit is easy. It's the uncompressing that's > tricky. I assume you mean ord("7")%2? First one to correctly decompress the value 0 into an ASCII character wins the title of the world's most capable hacker :p Marcus From breamoreboy at yahoo.co.uk Fri Jul 24 16:18:26 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 24 Jul 2009 21:18:26 +0100 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: Marcus Wanner wrote: > On 7/24/2009 3:04 PM, Roy Smith wrote: >> In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, >> Steven D'Aprano wrote: >> >>> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >>> >>>>> Nah. 7 contains three bits, so len(7) should *clearly* return 3. >>>> and len("7") must return 8, by the same token... but wait! >>>> >>>> >>> len("7") >>>> 1 >>>> >>> >>>> >>> >>>> my python installation must me outdated ;-) >>> No no no, you're obviously using an awesome version of Python that >>> can compress single-character strings to a single bit! >> >> Compressing strings to a single bit is easy. It's the uncompressing >> that's tricky. > I assume you mean ord("7")%2? > > First one to correctly decompress the value 0 into an ASCII character > wins the title of the world's most capable hacker :p > > Marcus asciichar = chr(len(0)) if the OP's wishes come true? -- Kindest regards. Mark Lawrence. From marcusw at cox.net Fri Jul 24 16:25:57 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 24 Jul 2009 16:25:57 -0400 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 7/24/2009 4:18 PM, Mark Lawrence wrote: > Marcus Wanner wrote: >> On 7/24/2009 3:04 PM, Roy Smith wrote: >>> In article <0279f596$0$5185$c3e8da3 at news.astraweb.com>, >>> Steven D'Aprano wrote: >>> >>>> On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: >>>> >>>>>> Nah. 7 contains three bits, so len(7) should *clearly* return 3. >>>>> and len("7") must return 8, by the same token... but wait! >>>>> >>>>> >>> len("7") >>>>> 1 >>>>> >>> >>>>> >>> >>>>> my python installation must me outdated ;-) >>>> No no no, you're obviously using an awesome version of Python that >>>> can compress single-character strings to a single bit! >>> >>> Compressing strings to a single bit is easy. It's the uncompressing >>> that's tricky. >> I assume you mean ord("7")%2? >> >> First one to correctly decompress the value 0 into an ASCII character >> wins the title of the world's most capable hacker :p >> >> Marcus > asciichar = chr(len(0)) if the OP's wishes come true? > Nope, wasn't "?"... Marcus From python.list at tim.thechases.com Fri Jul 24 16:30:10 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 24 Jul 2009 15:30:10 -0500 Subject: len() should always return something In-Reply-To: References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A6A19D2.2020204@tim.thechases.com> Marcus Wanner wrote: > First one to correctly decompress the value 0 into an ASCII > character wins the title of the world's most capable hacker :p Bah...uncompressing the value 0 into *an* ASCII character is easy. Uncompressing it into the *original* ASCII character from which it was compressed (with the aforementioned compression method) becomes a bit trickier ;-) -tkc From clp2 at rebertia.com Fri Jul 24 16:34:11 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 13:34:11 -0700 Subject: len() should always return something In-Reply-To: <4A6A19D2.2020204@tim.thechases.com> References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> <4a69ca1d$0$6161$4fafbaef@reader5.news.tin.it> <0279f596$0$5185$c3e8da3@news.astraweb.com> <4A6A19D2.2020204@tim.thechases.com> Message-ID: <50697b2c0907241334t660fce21l3ae20e7772561cef@mail.gmail.com> On Fri, Jul 24, 2009 at 1:30 PM, Tim Chase wrote: > Marcus Wanner wrote: >> >> First one to correctly decompress the value 0 into an ASCII >> character wins the title of the world's most capable hacker :p > > Bah...uncompressing the value 0 into *an* ASCII character is easy. > ?Uncompressing it into the *original* ASCII character from which it was > compressed (with the aforementioned compression method) becomes a bit > trickier ;-) Deja vu: http://www.hackles.org/cgi-bin/archives.pl?request=310 - Chris -- http://blog.rebertia.com From tismer at stackless.com Fri Jul 24 16:39:36 2009 From: tismer at stackless.com (Christian Tismer) Date: Fri, 24 Jul 2009 13:39:36 -0700 Subject: ANN: psyco V2 In-Reply-To: <4a696af3$0$442$426a74cc@news.free.fr> References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> <4a696af3$0$442$426a74cc@news.free.fr> Message-ID: <4A6A1C08.6000202@stackless.com> On 7/24/09 1:04 AM, William Dode wrote: > On 23-07-2009, Christian Tismer wrote: ... >> Wasn't the project plan saying the opposite, borrowing >> some ideas from psyco? :-) >> http://code.google.com/p/unladen-swallow/wiki/ProjectPlan > > How do you see the future of psyco when unladen-swallow will grab the > best of psyco (if they can !) ? I have no objections, but also no idea so far how that could work. Sounded like an unreflected idea to me, without seriously checking the possibilities and/or implications. The same kind of research apparently did not happen concerning PyPy, which IMHO is much more suitable to take advantage from. This is for the current status of psyco, of course. It will change dramatically in the next months, slowly adopting some of PyPy's ideas. Then it might start to make more sense. > Wait and see ? Wait and see, not holding breath, I'd say. > Anyway, thanks a lot for your work that we can use NOW ! Thanks for so much encouraging reactions! cheers - chris -- Christian Tismer :^) tismerysoft GmbH : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/ From tjreedy at udel.edu Fri Jul 24 16:43:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 16:43:17 -0400 Subject: list vs. tuple [Re: len() should always return something] In-Reply-To: <0279f9e9$0$5185$c3e8da3@news.astraweb.com> References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> <0279f9e9$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: > >> In article , >> Terry Reedy wrote: >> >>> Better: if isinstance(x, (int, float, complex)): >> I never noticed this before, but it seems odd that the second argument >> to isinstance() should be a tuple. Using the normal arguments made >> about tuples vs. lists, it seems like a list would be the right data >> structure here. I ignore the 'normal arguments' and it seems that Guido or the designer of isinstance did so here too. Fortunately. Practicality beats 'purity', especially misguided purity. > What would be the point of using a list? You're never going to sort it, > or append items to it, or otherwise mutate it. You build it, pass it to a > function which doesn't modify it in any fashion, then it gets garbage > collected. > > >> I suppose a set would be even more right, but (I'm >> pretty sure) isinstance() predates sets. > > Yes. > > [steve at sylar ~]$ python1.5 > Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat > 4.1.2-27)] on linux2 > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>> isinstance > >>>> set > Traceback (innermost last): > File "", line 1, in ? > NameError: set > > > >> I'm curious why a tuple was chosen. > > Tuples are smaller and faster to build than lists -- they're the most > lightweight sequence type in Python. You don't need all the extra > functionality of lists, so why go to the time and effort of building a > list? In fact, constant tuples can be and now are compiled as constants: >>> dis(compile('(1,2,3)','','eval')) 1 0 LOAD_CONST 3 ((1, 2, 3)) 3 RETURN_VALUE >>> dis(compile('[1,2,3]','','eval')) 1 0 LOAD_CONST 0 (1) 3 LOAD_CONST 1 (2) 6 LOAD_CONST 2 (3) 9 BUILD_LIST 3 12 RETURN_VALUE Internally, even a frozenset is more complicated than a tuple since it still needs a hash table, which is overkill for something that will be linearly scanned exactly once. tjr From qauzzix at gmail.com Fri Jul 24 16:52:47 2009 From: qauzzix at gmail.com (Qauzzix) Date: Fri, 24 Jul 2009 13:52:47 -0700 (PDT) Subject: How do I generate dia diagrams from python source code? Message-ID: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Greetings. Since I have been using dia to make my UML diagrams. I also found an util named dia2code that generates python code from dia diagram. Now that I have that option I really want to find a way to generate dia diagram from existing code and/or maintain my diagrams. I have been googling like crazy trying to find a way but with no luck. Anyone here know how to do this? Peace, - Jakob Sv. Bjarnason From tjreedy at udel.edu Fri Jul 24 16:52:53 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 16:52:53 -0400 Subject: len() should always return something In-Reply-To: <50697b2c0907241302m166d0c61y4912a29aaa4caaf1@mail.gmail.com> References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <50697b2c0907241302m166d0c61y4912a29aaa4caaf1@mail.gmail.com> Message-ID: Chris Rebert wrote: > I think the point made by Grant Edwards is instructive. len(x) = 1 > typically implies list(x)[0] and similar should be valid. At least, one should be able to iterate with x and get len(x) items. See below. >> And there's nothing ambiguous about len(42). > > Really? What is its value then? I think arguments of varying quality > can be made for: > 1 - as the OP and those from array programming languages would suggest > 2 - the number of decimal digits in 42, if one was feeling Perlish > 6 - the minimum number of bits necessary to represent 42 in binary > 32 (or 64, depending on your CPU) - the number of bits necessary to > represent an int (obviously breaks down a bit with arbitrary-magnitude > ints) > undefined - i.e. it causes an error, the current behavior; asking for > the length of a non-collection is "nonsensical" or "absurd" In set theory, one can define counts recursively as follows 0 = {} (set, not dict) n+1 = n | {n} so len(n) = n, and in particular, len(42) = 42. On the same basis, it has been proposed (and rejected) that iter(n) == range(n), do that we could write 'for i in 10' for instance. Guido thought this too esoteric. Terry Jan Reedy From tjreedy at udel.edu Fri Jul 24 16:57:13 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 24 Jul 2009 16:57:13 -0400 Subject: non-owning references? In-Reply-To: <0279f5b9$0$5185$c3e8da3@news.astraweb.com> References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> <0279f5b9$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 24 Jul 2009 15:55:45 +0200, Hrvoje Niksic wrote: > >> The term "variable" is used in the Python >> language reference and elsewhere, and is quite compatible with how other >> popular languages (Java, PHP, Lisp, ...) use it. Please stop >> complaining about valid terminology; it is not helpful. > > No, the use of the single term "variable" to describe two distinct > program models is not helpful. Whether other languages muddy the water > between memory-location based variables and name-binding is irrelevant to > whether we should do so. And quite a few of us are disappointed that the > Python language reference should confuse the issue by using misleading > terminology. > > Unfortunately, the use of "variable" is so ingrained, and so simple > compared to name binding terminology, that I fear we'll never eradicate > it. I know sometimes I use it myself, but always with a little shiver of > shame that I'm misusing terminology. Some years ago, I read a claim that 'variable' has about 15 shades of meaning in math (some referring to non-variable constants), making it probably the most overloaded term in math. I am trying to mostly avoid it in the book I am writing. tjr From bryanvick at gmail.com Fri Jul 24 17:11:02 2009 From: bryanvick at gmail.com (Bryan) Date: Fri, 24 Jul 2009 14:11:02 -0700 (PDT) Subject: Script runs manually, but cron fails Message-ID: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> I have a backup script that runs fine when I run it manually from the command line. When I run it with cron, the script stops running at random points in the source code. The script calls rsync with the subprocess module, which in turn uses ssh to backup files from a box on my lan. It also uses the atexit module to always run a certain piece of cleanup code when the script exits. However, this piece of code is never called when I use cron to run the script, but a ps -A command also does not show the python process so I know the script is dead. The log files generated by the script all show rsync as completing, but then the logging gets cutoff at random places when the script dies after that point. I am not getting any email from cron complaining of an error either. The script runs fine in my bash shell, what could cron be doing to interfere? From duncan.booth at invalid.invalid Fri Jul 24 17:14:06 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 24 Jul 2009 21:14:06 GMT Subject: list vs. tuple [Re: len() should always return something] References: <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: Roy Smith wrote: > In article , > Terry Reedy wrote: > >> Better: if isinstance(x, (int, float, complex)): > > I never noticed this before, but it seems odd that the second argument > to isinstance() should be a tuple. Using the normal arguments made > about tuples vs. lists, it seems like a list would be the right data > structure here. I suppose a set would be even more right, but (I'm > pretty sure) isinstance() predates sets. > > I'm curious why a tuple was chosen. There's a very good reason[*]. The second argument to isinstance() is a classinfo where a classinfo is a type or a tuple of classinfo. That means you can have an arbitrarily complex set of nested tuples e.g. (int, (float, complex)), but the immutable nature of tuples means the implementation of isinstance can walk the tuple tree without ever having to worry about infinite loops. If classinfo could be a list of types then someone somewhere would feed it a recursive list and then complain that the interpreter crashed. Exception specifications are tuples for the same reason. [*] For some definition of 'very good'. Would Python be significantly impaired if you couldn't combine classinfos into a tree structure? Probably not. From anonymous.c.lisper at gmail.com Fri Jul 24 17:58:57 2009 From: anonymous.c.lisper at gmail.com (ACL) Date: Fri, 24 Jul 2009 14:58:57 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <12ce2d31-5028-4b60-a5b1-5d5bf1fc06d7@f20g2000prn.googlegroups.com> On Jul 24, 2:06?pm, Raffael Cavallaro wrote: > On 2009-07-23 23:51:02 -0400, Carl Banks said: > > > On Jul 23, 5:52?pm, Rui Maciel wrote: > >> fft1976 wrote: > >>> How do you explain that something as inferior as Python beat Lisp in > >>> the market place despite starting 40 years later. > > >> Probably due to similar reasons that lead php to become remotely relevant > > . > > > Well, the only reason PHP became relevant because it was an > > easy > > ^^^^^^^^ > ^^^^^^^^ (emphasis added) > > > to > > deploy solution in a single application domain, the web, that happened > > to explode. > > i.e., Python "beat" lisp because it is ~70% of lisp in a form that is > much more palatable to the average programmer, just as php became > popular because it is powerful enough to do websites and, most > importantly, apprehensible to mediocre programmers and even some > non-programmers. > > -- > Raffael Cavallaro I actually think that the thing holding lisp back is 'bus factor'. Lets assume I have a java project and a lisp project: Java project: I have maybe 10 or 12 people on my team working on various subsystems of my project. There are probably one or two 'technical leader' types in the group, and a bunch of others who are sort of 'serfs', banging out java classes. Lets say one of my important guys gets totally splattered by a bus... I've still got another one left! I can rely on the other guy to keep things afloat while I train up a new leader type. Lisp project: I don't need as many people. I have 3 or 4 people, and one person is my technical leader and lisp guru. Guru is probably in charge of more difficult macros and (because of that), also in charge of the overall design (macros are design patterns embodied in code). Lets say he gets totally annihilated by the bus. What do I do now? I had all my eggs in one basket and my project is now stalled. I think that (for example) when people are saying that lisp macros make projects difficult to understand, or that lisp hackers are much harder to find than other programmers, what they are really showing is that because of the nature of the language, lisp projects get organized in a different way. This different way of organization is especially hard on corporate projects, because managers are expected to plan for when some important guy drops dead. The problem with lisp is that if you hire too many extra hackers, you end up with a bunch of people sitting around twiddling their thumbs. This may also explain why it gets touted as an academic language. In academia if a professor who is leading some important research drops dead, who cares? Most likely no one had interest as to whether the university would make a profit on this particular research endeavor (except maybe the deceased, as he would like to get grant funding). So I guess then we can give some tips for making lisp more acceptable for 'paying gigs'. 1.) If you are a lisp hacker, it is your duty to be lazier and write less code. We need to make it so that these projects need sizable teams of people to complete. 2.) Write down everything that you do, everything you are going to do. Maybe draw some object diagrams of your important/fancy macros, make sure these are stored with the source. (You'll note that an iterative approach to programming isn't really conducive to making a lot of diagrams of stuff...) I don't know what this has to do with python or scheme or people dumping scheme for python. Honestly, I seriously doubt that any undergraduate programming course will give you enough actual programming experience to make you seriously proficient in the language (research projects not counted). The language used to teach the material is a cursory detail. From user at example.net Fri Jul 24 18:28:09 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 00:28:09 +0200 Subject: pack an integer into a string Message-ID: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> is there a pythonic and synthetic way (maybe some standard module) to "pack" an integer (maybe a *VERY* big one) into a string? like this: >>> number = 252509952 >>> hex(number) '0xf0cff00' >>> so i would like a string like '\xf0\xcf\xf0\x00' i wrote some code to do it, so ugly i am ashamed to post :-( i tried xdrlib, but does not quite do what i mean... bye From martin at v.loewis.de Fri Jul 24 18:28:24 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 25 Jul 2009 00:28:24 +0200 Subject: installing 2.6 on vista64 In-Reply-To: References: Message-ID: <4a6a3588$0$11346$9b622d9e@news.freenet.de> > I just downloaded and attempted to install python 2.6.2. The > installer proceeds to do its work then dies, leaving an entry in the > eventlog: > > Windows Installer installed the product. Product Name: Python 2.6.2. > Product Version: 2.6.2150. Product Language: 1033. Installation > success or error status: 1602. Are you using subst'ed drives by any chance? > Googling for this I wasn't able to narrow the results down to > something usable. Anyone know of issues and how to fix them installing > on vista 64 (yes, I have 8 gigs of ram) Run "msiexec /i /l*v python.log", and search the log file for 1602. This should give a clue. Regards, Martin From emile at fenx.com Fri Jul 24 18:41:59 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 24 Jul 2009 15:41:59 -0700 Subject: Script runs manually, but cron fails In-Reply-To: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> References: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> Message-ID: On 7/24/2009 2:11 PM Bryan said... > The script runs fine in my bash shell, what could cron be doing to > interfere? It's likely environmental -- make sure you're starting with the same path, etc. Sometimes I'll create a sell script by env'ing to a bash script file and append the actual command to execute, chmod a+x the script and put that in the cron file. Then strip out variables until you find the missing culprit. HTH, Emile From sbassi at clubdelarazon.org Fri Jul 24 18:43:58 2009 From: sbassi at clubdelarazon.org (Sebastian Bassi) Date: Fri, 24 Jul 2009 19:43:58 -0300 Subject: pack an integer into a string In-Reply-To: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <9e2f512b0907241543o5b4606agf80a87af74b4116@mail.gmail.com> On Fri, Jul 24, 2009 at 7:28 PM, superpollo wrote: > is there a pythonic and synthetic way (maybe some standard module) to "pack" > an integer (maybe a *VERY* big one) into a string? like this: What do you mean to pack? Maybe Pickle is what you want. import cPickle variable = 124348654333577698 cPickle.dump(variable,open('filename', 'w')) To load it: import cPickle vaariable = cPickle.load(open('filename')) It is not "into a string", but instead of a filename, use a string with stringio From 9372966370 at sms.mycricket.com Fri Jul 24 18:51:49 2009 From: 9372966370 at sms.mycricket.com (9372966370 at sms.mycricket.com) Date: 24 Jul 09 16:51:49 -0600 Subject: No subject Message-ID: <200907242251.n6OLv34O026362@mta2-den.mycricket.com> (Re: urllib2.URLError: error using twill with python) how can i change url/http://... --------------------------------------------- Sent by a Cricket mobile device --------------------------------------------- From 9372966370 at sms.mycricket.com Fri Jul 24 18:53:15 2009 From: 9372966370 at sms.mycricket.com (9372966370 at sms.mycricket.com) Date: 24 Jul 09 16:53:15 -0600 Subject: No subject Message-ID: <200907242253.n6OLv34f026362@mta2-den.mycricket.com> (Re: urllib2.URLError: error using twill with python) --------------------------------------------- Sent by a Cricket mobile device --------------------------------------------- From user at example.net Fri Jul 24 18:58:07 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 00:58:07 +0200 Subject: pack an integer into a string In-Reply-To: References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <4a6a3c82$0$6157$4fafbaef@reader5.news.tin.it> Sebastian Bassi wrote: > On Fri, Jul 24, 2009 at 7:28 PM, superpollo wrote: > >>is there a pythonic and synthetic way (maybe some standard module) to "pack" >>an integer (maybe a *VERY* big one) into a string? like this: > > > What do you mean to pack? Maybe Pickle is what you want. > > import cPickle > variable = 124348654333577698 > cPickle.dump(variable,open('filename', 'w')) > > To load it: > > import cPickle > vaariable = cPickle.load(open('filename')) > > It is not "into a string", but instead of a filename, use a string with stringio >>> number 252509952 >>> f = cStringIO.StringIO() >>> cPickle.dump(number , f) >>> f.getvalue() 'I252509952\n.' >>> as you can see, the pickled representation is not what i wanted... thanks anyway bye From piet at cs.uu.nl Fri Jul 24 19:03:25 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 01:03:25 +0200 Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: >>>>> superpollo (s) wrote: >s> is there a pythonic and synthetic way (maybe some standard module) to >s> "pack" an integer (maybe a *VERY* big one) into a string? like this: >>>>> number = 252509952 >>>>> hex(number) >s> '0xf0cff00' >>>>> >s> so i would like a string like '\xf0\xcf\xf0\x00' You have the string wrong. But the correct one you get with: In [67]: import struct In [68]: number = 252509952 In [69]: struct.pack('>I', number) Out[69]: '\x0f\x0c\xff\x00' (Please note that this is big endian) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From user at example.net Fri Jul 24 19:11:08 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 01:11:08 +0200 Subject: pack an integer into a string In-Reply-To: <4a6a3c82$0$6157$4fafbaef@reader5.news.tin.it> References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> <4a6a3c82$0$6157$4fafbaef@reader5.news.tin.it> Message-ID: <4a6a3f8f$0$6157$4fafbaef@reader5.news.tin.it> superpollo wrote: > Sebastian Bassi wrote: > >> On Fri, Jul 24, 2009 at 7:28 PM, superpollo wrote: >> >>> is there a pythonic and synthetic way (maybe some standard module) to >>> "pack" >>> an integer (maybe a *VERY* big one) into a string? like this: >> >> >> >> What do you mean to pack? Maybe Pickle is what you want. >> >> import cPickle >> variable = 124348654333577698 >> cPickle.dump(variable,open('filename', 'w')) >> >> To load it: >> >> import cPickle >> vaariable = cPickle.load(open('filename')) >> >> It is not "into a string", but instead of a filename, use a string >> with stringio > > > > >>> number > 252509952 > >>> f = cStringIO.StringIO() > >>> cPickle.dump(number , f) > >>> f.getvalue() > 'I252509952\n.' > >>> > > as you can see, the pickled representation is not what i wanted... > OTOH, using a higher protocol: >>> cPickle.dump(number , f , 1) >>> f.getvalue() 'J\x00\xff\x0c\x0f.' which is very close to '\xf0\xcf\xf0\x00', that i meant originally... so i i change my spec as; '\x0f\x0c\xff\x00' (i.e. left- intead of right- zeropadding) i am almost there... bye From bartc at freeuk.com Fri Jul 24 19:15:32 2009 From: bartc at freeuk.com (bartc) Date: Fri, 24 Jul 2009 23:15:32 GMT Subject: len() should always return something In-Reply-To: References: Message-ID: "Dr. Phillip M. Feldman" wrote in message news:mailman.3644.1248417347.8015.python-list at python.org... > > Some aspects of the Python design are remarkably clever, while others > leave > me perplexed. Here's an example of the latter: Why does len() give an > error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. So you want len() to treat 123 as though it could potentially be a list containing a single element? In that case there could be an ambiguity here: print len([10,20,30]) Should it print 3 (the elements in [10,20,30]), or 1 (treating [10,20,30] as a potential list containing the single element [10,20,30])? -- bartc From user at example.net Fri Jul 24 19:16:07 2009 From: user at example.net (superpollo) Date: Sat, 25 Jul 2009 01:16:07 +0200 Subject: pack an integer into a string In-Reply-To: References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <4a6a40ba$0$6157$4fafbaef@reader5.news.tin.it> Piet van Oostrum wrote: ... > You have the string wrong. oops yea. > But the correct one you get with: > > In [67]: import struct > > In [68]: number = 252509952 > > In [69]: struct.pack('>I', number) > Out[69]: '\x0f\x0c\xff\x00' > > (Please note that this is big endian) thanks a lot, but it does not work for large integers: >>> number 283691163101781L >>> struct.pack('>I', number) Traceback (most recent call last): File "", line 1, in ? OverflowError: long int too large to convert >>> bye From scriptlearner at gmail.com Fri Jul 24 19:20:00 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Fri, 24 Jul 2009 16:20:00 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? Message-ID: My parent thread keeps a counter for the number of free child workers (say 100) and initializes some child threads and call child.start(). Once the number of free child workers reach 0, the parent thread will wait until some at least one child thread finishes and then it will initialize another child thread. My question is, how can a child thread notify the parent that it's done so that the parent can call join() on it? I am not sure how a child thread can send a signal to its parent while it may not even know anything about it's parent. Can you guys please provide some suggestions? Some code samples will be nice. Thanks. From greg at cosc.canterbury.ac.nz Fri Jul 24 19:24:13 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sat, 25 Jul 2009 11:24:13 +1200 Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler In-Reply-To: <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> References: <4a645b81$0$12974$426a74cc@news.free.fr> <7cn54pF28nv72U1@mid.individual.net> <861f18de-ceb9-46d9-92f1-13a95dcf047e@h31g2000yqd.googlegroups.com> Message-ID: <7cuu3eF29jmmuU2@mid.individual.net> Bearophile wrote: > Was this link, shown by William, not enough? > http://hg.flibuste.net/libre/games/cheval/file/46797c3a5136/chevalx.pyx#l1 Yes, sorry, I posted too soon. -- Greg From greg at cosc.canterbury.ac.nz Fri Jul 24 19:26:44 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Sat, 25 Jul 2009 11:26:44 +1200 Subject: clean way prepend an element to a numpy array In-Reply-To: References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <7cnr5tF28183hU1@mid.individual.net> Message-ID: <7cuu86F2945qmU1@mid.individual.net> Robert Kern wrote: >> a[:0] = array('i', [0]) > > Not when 'a' is a numpy array rather than an array.array. That's true, but I got the impression that the OP was talking about array.array, not numpy.array. It's very confusing having two widely-used types both called 'array'. :-( -- Greg From robert.kern at gmail.com Fri Jul 24 19:35:01 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 24 Jul 2009 18:35:01 -0500 Subject: clean way prepend an element to a numpy array In-Reply-To: <7cuu86F2945qmU1@mid.individual.net> References: <493c4132-fb88-4fcc-9b1c-327f4dc3d905@l35g2000pra.googlegroups.com> <7cnr5tF28183hU1@mid.individual.net> <7cuu86F2945qmU1@mid.individual.net> Message-ID: On 2009-07-24 18:26, greg wrote: > Robert Kern wrote: > >>> a[:0] = array('i', [0]) >> >> Not when 'a' is a numpy array rather than an array.array. > > That's true, but I got the impression that the OP was > talking about array.array, not numpy.array. Did you read the Subject: line? :-) -- 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 python at mrabarnett.plus.com Fri Jul 24 19:44:36 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 25 Jul 2009 00:44:36 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: References: Message-ID: <4A6A4764.1080206@mrabarnett.plus.com> scriptlearner at gmail.com wrote: > My parent thread keeps a counter for the number of free child workers > (say 100) and initializes some child threads and call child.start(). > Once the number of free child workers reach 0, the parent thread will > wait until some at least one child thread finishes and then it will > initialize another child thread. > My question is, how can a child thread notify the parent that it's > done so that the parent can call join() on it? I am not sure how a > child thread can send a signal to its parent while it may not even > know anything about it's parent. Can you guys please provide some > suggestions? Some code samples will be nice. Thanks. I would create a queue ("Queue" module) and pass it to each child; the child would put something (eg a reference to itself) in the queue when it had finished. From lists at cheimes.de Fri Jul 24 19:53:19 2009 From: lists at cheimes.de (Christian Heimes) Date: Sat, 25 Jul 2009 01:53:19 +0200 Subject: how can a child thread notify a parent thread its status? In-Reply-To: References: Message-ID: scriptlearner at gmail.com wrote: > My parent thread keeps a counter for the number of free child workers > (say 100) and initializes some child threads and call child.start(). > Once the number of free child workers reach 0, the parent thread will > wait until some at least one child thread finishes and then it will > initialize another child thread. > My question is, how can a child thread notify the parent that it's > done so that the parent can call join() on it? I am not sure how a > child thread can send a signal to its parent while it may not even > know anything about it's parent. Can you guys please provide some > suggestions? Some code samples will be nice. Thanks. You are using the wrong approach here. There is a much easier solution to your problem although it's not obvious in the first place. In this approach the worker threads don't have to notify the parent thread that they are ready. The main thread creates a queue and a bunch of worker threads that are pulling tasks from the queue. As long as the queue is empty all worker threads block and do nothing. When a task is put into the queue a random worker thread acquires the task, does it job and sleeps as soon as its ready. That way you can reuse a thread over and over again without creating a new worker threads. When you need to stop the threads you put a kill object into the queue that tells the thread to shut down instead of blocking on the queue. Christian From python at mrabarnett.plus.com Fri Jul 24 20:05:22 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 25 Jul 2009 01:05:22 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: References: Message-ID: <4A6A4C42.40004@mrabarnett.plus.com> Christian Heimes wrote: > scriptlearner at gmail.com wrote: >> My parent thread keeps a counter for the number of free child workers >> (say 100) and initializes some child threads and call child.start(). >> Once the number of free child workers reach 0, the parent thread will >> wait until some at least one child thread finishes and then it will >> initialize another child thread. >> My question is, how can a child thread notify the parent that it's >> done so that the parent can call join() on it? I am not sure how a >> child thread can send a signal to its parent while it may not even >> know anything about it's parent. Can you guys please provide some >> suggestions? Some code samples will be nice. Thanks. > > You are using the wrong approach here. There is a much easier solution > to your problem although it's not obvious in the first place. In this > approach the worker threads don't have to notify the parent thread that > they are ready. > > The main thread creates a queue and a bunch of worker threads that are > pulling tasks from the queue. As long as the queue is empty all worker > threads block and do nothing. When a task is put into the queue a random > worker thread acquires the task, does it job and sleeps as soon as its > ready. That way you can reuse a thread over and over again without > creating a new worker threads. > > When you need to stop the threads you put a kill object into the queue > that tells the thread to shut down instead of blocking on the queue. > The extra trick is that when a thread gets the kill object it puts it back before terminating so that another thread will also get it. From googler.1.webmaster at spamgourmet.com Fri Jul 24 20:12:13 2009 From: googler.1.webmaster at spamgourmet.com (moerchendiser2k3) Date: Fri, 24 Jul 2009 17:12:13 -0700 (PDT) Subject: Freetype2 in PIL Message-ID: <95f321b4-d7e8-4515-8743-dad86298024d@a26g2000yqn.googlegroups.com> Hi, I have a problem with Freetype2 in the Python Image Library. I compiled it as 64bit and I got a file named freetype239.lib. So I added this file to a folder called lib. I added the parent folder of the lib folder to the setup.py settings (like all the others: libjpeg, zlib). LIbjpeg and Zlib works fine on Windows 64 bit and the installer can find them. But I just get a "Freetype2 not found". Even renaming it to freetype.lib and freetype2.lib doesnt work. Any suggestions why PIL cant find the library? Bye and thanks in advance :) From googler.1.webmaster at spamgourmet.com Fri Jul 24 20:12:13 2009 From: googler.1.webmaster at spamgourmet.com (moerchendiser2k3) Date: Fri, 24 Jul 2009 17:12:13 -0700 (PDT) Subject: Freetype2 in PIL Message-ID: Hi, I have a problem with Freetype2 in the Python Image Library. I compiled it as 64bit and I got a file named freetype239.lib. So I added this file to a folder called lib. I added the parent folder of the lib folder to the setup.py settings (like all the others: libjpeg, zlib). LIbjpeg and Zlib works fine on Windows 64 bit and the installer can find them. But I just get a "Freetype2 not found". Even renaming it to freetype.lib and freetype2.lib doesnt work. Any suggestions why PIL cant find the library? Bye and thanks in advance :) From gallium.arsenide at gmail.com Fri Jul 24 20:14:59 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 24 Jul 2009 17:14:59 -0700 (PDT) Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> <4a6a40ba$0$6157$4fafbaef@reader5.news.tin.it> Message-ID: <669833bd-7d70-4cec-a9a3-9de50d77d8e7@c14g2000yqm.googlegroups.com> On Jul 24, 7:16?pm, superpollo wrote: > thanks a lot, but [struct] does not work for large integers: Since the struct module is designed specifically for C-style structs, it's definitely not going to handle arbitrary-length integers on its own. You could chop up your Python (long) integer into C-sized pieces and then pack the pieces; but if you need to do that, the advantage of using struct at all may be minimal or even completely gone. > OTOH, using a higher [pickle] protocol: If you are sure you are going to keep using the same version of Python, then this could work. I would personally not depend on this to keep working in future versions. I'm not aware of any existing package that does what you want. Maybe someone else does. How I personally would tackle your problem is to write the whole thing from scratch (as you've done). John From deets at nospam.web.de Fri Jul 24 20:33:11 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 25 Jul 2009 02:33:11 +0200 Subject: cgi.fieldstorage() In-Reply-To: References: <7cu9hhF29gt65U1@mid.uni-berlin.de> Message-ID: <7cv268F29q0j5U1@mid.uni-berlin.de> gert schrieb: > On Jul 24, 7:32 pm, "Diez B. Roggisch" wrote: >> gert schrieb: >> >>> this is a non standard way to store multi part post data on disk >>> def application(environ, response): >>> with open('/usr/httpd/var/wsgiTemp','w') as f: >>> while True: >>> chunk = environ['wsgi.input'].read(8192).decode('latin1') >>> if not chunk: break >>> f.write(chunk) >>> response('200 OK',[]) >>> return ['complete'] >>> my question is how do i handle the file, so i can shuffle it into a db >>> using small chunks of memorie ? >> I don't think that's possible with the current DB-API. There is no >> stream-based BLOB-interface (as e.g. JDBC offers). >> >> So the answer certainly depends on your used RDBMS. For oracle, you >> would be lucky: >> >> http://cx-oracle.sourceforge.net/html/lob.html >> >> Other adapters I don't know about. > > sqlite :) ok let say for now it would be impossible on a db level, but > before i reach the impossible, i still need to parse the file to > prepare the chunks. How do i do that ? How do i get the chunks without > loading the hole file into memorie ? It's memory - memorie might be some nice dutch girl you know :) Apart from that, your code above does exactly that - reads the data chunkwise. If the WSGI-implementation works proper, this will be the socket's input stream, so there is no memory overhead involved. Now of course if you want to have a multi-pass processing of the file without putting it into memory, then you need to save it to the harddisk befor. But honestly - we are talking about a web-application here. My DSL-connection has 1 MBit upstream, the average server has at least 2GB of memory available - so we are talking 20000 seconds uploading time to fill that memory. Which is about 5 hours. And you are decoding the data to a certain decoding, so we are not talking about binary data here - are you really sure memory is an issue? Diez Diez From casevh at gmail.com Fri Jul 24 20:35:59 2009 From: casevh at gmail.com (casevh) Date: Fri, 24 Jul 2009 17:35:59 -0700 (PDT) Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <7728c70f-4d1d-4e5c-ae8b-175491fc1d31@f18g2000prf.googlegroups.com> On Jul 24, 3:28?pm, superpollo wrote: > is there a pythonic and synthetic way (maybe some standard module) to > "pack" an integer (maybe a *VERY* big one) into a string? like this: > > ? >>> number = 252509952 > ? >>> hex(number) > ? '0xf0cff00' > ? >>> > > so i would like a string like '\xf0\xcf\xf0\x00' > > i wrote some code to do it, so ugly i am ashamed to post :-( > > i tried xdrlib, but does not quite do what i mean... > > bye This works, I think. >>> def hex_string(n): ... a = hex(n) ... ch_list = [] ... if a.startswith('0x'): a = a[2:] ... if a.endswith('L'): a = a[:-1] ... for i in range(0, len(a)): ... ch_list.append(chr(int(a[i:i+2],16))) ... return ''.join(ch_list) ... >>> hex_string(252509952) '\xf0\x0c\xcf\xff\xf0\x00\x00' >>> hex_string(283691163101781L) '\x10\x02 \x03?\xff\xf0\x00\n\xaa\xa5U\x05' >>> From deets at nospam.web.de Fri Jul 24 20:38:28 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 25 Jul 2009 02:38:28 +0200 Subject: Computation/E-mail Expert opinions? In-Reply-To: <9e520bf6-bc15-4942-a668-d18c2dbaf80f@o32g2000yqm.googlegroups.com> References: <9e520bf6-bc15-4942-a668-d18c2dbaf80f@o32g2000yqm.googlegroups.com> Message-ID: <7cv2g5F29q0j5U2@mid.uni-berlin.de> scribio_vide schrieb: > MAIL From: > From: "tommy02" > To: "MeAmI" > Subject: I AM LOOKING FORWARD TO YOUR ANSWER ON THIS ISSUE > Date: Fri, 24 Jul 09 11:52:27 Pacific Daylight Time > MIME-Version: 1.0 > Content-Type: multipart/mixed;boundary= "---- > =_NextPart_000_008C_8F228C8E.684AAFEA" > X-Priority: 3 > X-MSMail-Priority: Normal > X-Mailer: Microsoft Outlook Express 6.00.2462.0000 > X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2462.0000 > > ------=_NextPart_000_008C_8F228C8E.684AAFEA > Content-Type: text/plain > Content-Transfer-Encoding: base64 > > ZXRtYWlsLmNvbQ0KICAgIA== > ------=_NextPart_000_008C_8F228C8E.684AAFEA-- > > Any ides why I this came to be in my inbox? It is either an extremely dangerous virus - or an utterly harmless, but annoying base64-encoded spam-mail, given the subject. None of the possibilities justifies a post to *three* newsgroups. Diez From scriptlearner at gmail.com Fri Jul 24 20:39:04 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Fri, 24 Jul 2009 17:39:04 -0700 (PDT) Subject: time in milliseconds by calling time.time() Message-ID: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> I am trying to measure some system response time by using the time.time () or time.clock() in my script. However, the numbers I get are in 10s of milliseconds. For example, 1248481670.34 #from time.time() 0.08 #from time.clock() That won't work for me, since the response time may be only a few milliseconds. My environment is Solaris 10 with Python 2.4.4 (#7, Feb 9 2007, 22:10:21). SunOS 5.10 Generic_137112-07 i86pc i386 i86pc The tricky thing is, if I run the python interpreter and import the time module, I can get a time floating number in better precision by calling time.time(). Do you guys have any suggestion on debugging this problem? Or, is there any other module I can try? Thanks. $ python Python 2.4.4 (#7, Feb 9 2007, 22:10:21) [GCC 3.4.6] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> time.time() 1248481930.8023829 <--I like this! >>> time.clock() 0.0 >>> From aahz at pythoncraft.com Fri Jul 24 21:09:08 2009 From: aahz at pythoncraft.com (Aahz) Date: 24 Jul 2009 18:09:08 -0700 Subject: Python ctypes on win64 References: Message-ID: In article , ulf wrote: > >Does anybody know if python includes a win64 version of ctypes? > >According to the documentation it should be included - at least >there's no special remarks for win64, and I haven't found any recent >notes saying that it shouldn't work on win64. Yet it looks like both >the installer from Python.org and from ActiveState.com have disabled >it. Nobody seems to have followed up to this, you may have better luck on the capi-sig mailing list. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From rylesny at gmail.com Fri Jul 24 21:11:29 2009 From: rylesny at gmail.com (ryles) Date: Fri, 24 Jul 2009 18:11:29 -0700 (PDT) Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? Message-ID: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> According to http://www.python.org/doc/essays/packages.html: "The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it." However, I've noticed that once a module is imported using the `from pkg import mod' syntax, if its name is deleted from the package namespace then subsequent imports with `from' will fail. Here is an example of this type of scenario: $ ls -l pkg total 8.0K -rw-rw-r-- 1 ? general 0 Jul 24 20:21 _impl.py -rw-rw-r-- 1 ? general 147 Jul 24 20:28 __init__.py -rw-rw-r-- 1 ? general 0 Jul 24 20:33 public2.py -rw-rw-r-- 1 ? general 208 Jul 24 20:32 public.py $ cat pkg/__init__.py from pkg import _impl # Add functions which refer to objects in _impl # ... # Don't "currently" require this in the namespace anymore. del _impl $ cat pkg/public.py # Implement something with the aid of _impl. from pkg import public2 # OK, fine. from pkg import _impl # The module exists, but this will fail. # Could do import pkg._impl (or a relative import) $ python Python 2.6.2 (r262:71600, Jul 16 2009, 14:04:28) [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from pkg import public Traceback (most recent call last): File "", line 1, in File "pkg/public.py", line 4, in from pkg import _impl # The module exists, but this will fail. ImportError: cannot import name _impl >>> import sys >>> sys.modules["pkg._impl"] >>> from pkg import _impl Traceback (most recent call last): File "", line 1, in ImportError: cannot import name _impl >>> import pkg._impl # Giving up! >>> I had previously noted that once a module is imported from a package it is automatically added to the package namespace: >>> import pkg >>> dir(pkg) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__'] >>> from pkg import public2 >>> dir(pkg) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'public2'] >>> However, I didn't expect python to give up on importing those names again once removed. Can anyone provide more details on what's occurring here? Is this behavior intentional (a feature), or, is it in your opinion, a defect? From roy at panix.com Fri Jul 24 21:27:10 2009 From: roy at panix.com (Roy Smith) Date: Fri, 24 Jul 2009 21:27:10 -0400 Subject: time in milliseconds by calling time.time() References: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> Message-ID: In article <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50 at m7g2000prd.googlegroups.com>, "scriptlearner at gmail.com" wrote: > I am trying to measure some system response time by using the time.time > () or time.clock() in my script. However, the numbers I get are in > 10s of milliseconds. > [...] > The tricky thing is, if I run the python interpreter and import the > time module, I can get a time floating number in better precision by > calling time.time(). > [...] > >>> time.time() > 1248481930.8023829 <--I like this! time.time() is returning a float in either case. The difference you are seeing is purely related to how you are printing it; executing a "print" statement as opposed to the interactive interpreter printing a value. Notice: Roy-Smiths-MacBook-Pro:play$ python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> print time.time() 1248484949.75 >>> time.time() 1248484957.151274 and further notice: >>> x = time.time() >>> str(x) '1248485028.58' >>> repr(x) '1248485028.5814769' Keep in mind that while a float may have a large apparent precision, there's no promise that the actual value returned by the OS has that much precision. You should be fine if all you're looking for is ms, but I wouldn't count on much more than that. From http Fri Jul 24 21:30:46 2009 From: http (Paul Rubin) Date: 24 Jul 2009 18:30:46 -0700 Subject: pack an integer into a string References: <4a6a357c$0$6161$4fafbaef@reader5.news.tin.it> Message-ID: <7xvdlh8s6x.fsf@ruckus.brouhaha.com> superpollo writes: > >>> number = 252509952 > >>> hex(number) > '0xf0cff00' > >>> > > so i would like a string like '\xf0\xcf\xf0\x00' def encode(number): h = '%x' % number if len(h) % 2 == 1: h = '0' + h return h.decode('hex') From pfeldman at verizon.net Fri Jul 24 22:30:12 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 24 Jul 2009 19:30:12 -0700 (PDT) Subject: list vs. tuple [Re: len() should always return something] In-Reply-To: References: <24639361.post@talk.nabble.com> <1078183501.1917310.1248451116880.JavaMail.root@vms244.mailsrvcs.net> Message-ID: <24654347.post@talk.nabble.com> isinstance(x, (int, float, complex)) is certainly very compact, and does what I want. Thanks! -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654347.html Sent from the Python - python-list mailing list archive at Nabble.com. From pfeldman at verizon.net Fri Jul 24 22:33:30 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 24 Jul 2009 19:33:30 -0700 (PDT) Subject: len() should always return something In-Reply-To: <0279f6df$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: <24654358.post@talk.nabble.com> "As far as I know, there is no programming language which treats scalars like ints as if they were vectors of length 1" Actually, Matlab does: >> length(5) ans = 1 >> -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654358.html Sent from the Python - python-list mailing list archive at Nabble.com. From rurpy at yahoo.com Fri Jul 24 22:41:50 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 24 Jul 2009 19:41:50 -0700 (PDT) Subject: regex: multiple matching for one string References: <3559c849-b650-4284-ae05-96db5da1d5f2@y10g2000prf.googlegroups.com> Message-ID: Scott David Daniels wrote: > rurpy at yahoo.com wrote: >> Nick Dumas wrote: >>> On 7/23/2009 9:23 AM, Mark Lawrence wrote: >>>> scriptlearner at gmail.com wrote: >>>>> For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I >>>>> will like to take out the values (valuea, valueb, and valuec). How do >>>>> I do that in Python? The group method will only return the matched >>>>> part. Thanks. >>>>> >>>>> p = re.compile('#a=*;b=*;c=*;') >>>>> m = p.match(line) >>>>> if m: >>>>> print m.group(), >>>> IMHO a regex for this is overkill, a combination of string methods such >>>> as split and find should suffice. >> >> You're saying that something like the following >> is better than the simple regex used by the OP? >> [untested] >> values = [] >> parts = line.split(';') >> if len(parts) != 4: raise SomeError() >> for p, expected in zip (parts[-1], ('#a','b','c')): >> name, x, value = p.partition ('=') >> if name != expected or x != '=': >> raise SomeError() >> values.append (value) >> print values[0], values[1], values[2] > > I call straw man: [tested] > line = "#a=valuea;b=valueb;c=valuec;" > d = dict(single.split('=', 1) > for single in line.split(';') if single) > d['#a'], d['b'], d['c'] > If you want checking code, add: > if len(d) != 3: > raise ValueError('Too many keys: %s in %r)' % ( > sorted(d), line)) OK, that seems like a good solution. It certainly wasn't an obvious solution to me. I still have no problem maintaining that [tested] line = "#a=valuea;b=valueb;c=valuec;" m = re.match ('#a=(.*);b=(.*);c=(.*);', line) m.groups((1,2,3)) (If you want checking code, nothing else required.) is still simpler and clearer (with the obvious caveat that one is familiar with regexes.) >> Blech, not in my book. The regex checks the >> format of the string, extracts the values, and >> does so very clearly. Further, it is easily >> adapted to other similar formats, or evolutionary >> changes in format. It is also (once one is >> familiar with regexes -- a useful skill outside >> of Python too) easier to get right (at least in >> a simple case like this.) > The posted regex doesn't work; this might be homework, so > I'll not fix the two problems. The fact that you did not > see the failure weakens your claim of "does so very clearly." Fact? Maybe you should have read the whole thread before spewing claims that I did not see the regex problem. The fact that you did not bother to weakens any claims you make in this thread. (Of course this line of argumentation is stupid anyway -- even had I not noticed the problem, it would say nothing about the general case. My advice to you is not to try to extrapolate when the sample size is one.) From pfeldman at verizon.net Fri Jul 24 22:50:54 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 24 Jul 2009 19:50:54 -0700 (PDT) Subject: len() should always return something In-Reply-To: <7ct4b8F29mednU1@mid.uni-berlin.de> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: <24654439.post@talk.nabble.com> Here's a simple-minded example: def dumbfunc(xs): for x in xs: print x This function works fine if xs is a list of floats, but not if it is single float. It can be made to work as follows: def dumbfunc(xs): if isinstance(xs,(int,float,complex)): xs= [xs] for x in xs: print x Having to put such extra logic into practically every function is one of the annoying things about Python. Phillip Diez B. Roggisch-2 wrote: > > Dr. Phillip M. Feldman schrieb: >> Some aspects of the Python design are remarkably clever, while others >> leave >> me perplexed. Here's an example of the latter: Why does len() give an >> error >> when applied to an int or float? len() should always return something; in >> particular, when applied to a scalar, it should return a value of 1. Of >> course, I can define my own function like this: >> >> def mylen(x): >> if isinstance(x,int) or isinstance(x,float): return 1 >> return len(x) >> >> But, this shouldn't be necessary. > > Can you show some example of where that is actually making a piece of > code more elegant? > > Diez > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654439.html Sent from the Python - python-list mailing list archive at Nabble.com. From clp2 at rebertia.com Fri Jul 24 23:02:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 24 Jul 2009 20:02:40 -0700 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: <50697b2c0907242002s1772783jed0a4a68177616cf@mail.gmail.com> On Fri, Jul 24, 2009 at 7:50 PM, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): > ? for x in xs: > ? ? ?print x > > This function works fine if xs is a list of floats, but not if it is single > float. ?It can be made to work as follows: > > def dumbfunc(xs): > ? if isinstance(xs,(int,float,complex)): xs= [xs] > ? for x in xs: > ? ? ?print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. You can easily fix that using decorators: (disclaimer: untested, except mentally) #bit of setup; pays off later def list_or_single(func): def wrapper(arg): try: iter(arg) except TypeError: arg = [arg] return func(arg) return wrapper #now just prefix functions with @list_or_single @list_or_single def dumbfunc(xs): for x in xs: print x Using the extended call syntax as I explained earlier is another option. Cheers, Chris -- http://blog.rebertia.com From davidj411 at gmail.com Sat Jul 25 00:21:48 2009 From: davidj411 at gmail.com (davidj411) Date: Fri, 24 Jul 2009 21:21:48 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? References: Message-ID: could i see an example of this maybe? From pavlovevidence at gmail.com Sat Jul 25 00:55:26 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 24 Jul 2009 21:55:26 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On Jul 24, 11:06?am, Raffael Cavallaro wrote: > On 2009-07-23 23:51:02 -0400, Carl Banks said: > > > On Jul 23, 5:52?pm, Rui Maciel wrote: > >> fft1976 wrote: > >>> How do you explain that something as inferior as Python beat Lisp in > >>> the market place despite starting 40 years later. > > >> Probably due to similar reasons that lead php to become remotely relevant > > . > > > Well, the only reason PHP became relevant because it was an > > easy > > ^^^^^^^^ > ^^^^^^^^ (emphasis added) > > > to > > deploy solution in a single application domain, the web, that happened > > to explode. > > i.e., Python "beat" lisp because it is ~70% of lisp in a form that is > much more palatable to the average programmer, just as php became > popular because it is powerful enough to do websites and, most > importantly, apprehensible to mediocre programmers and even some > non-programmers. That the two languages made something easier is the beginning and end of the similarities between them. PHP made hacking together a security-hole-ridden website easier. Python made actual programming easier. PHP became what it is because it rode on the coattails of a technology that grew in spite of it (kind of like everything Microsoft has shipped since Windows 3.1). Python became what it is because it earned the respect of programmers, who contributed to it and spread the word about it. That it is easy to use is only a part of why it earned that respect. Two languages could not have come to prominence by more different means. I can handle Python being called inferior to Lisp or a language for stupid people or a single-core-using dinosaur of a language or worse than Perl. But please don't put it on the same level as PHP. Their situations have almost nothing in common. Carl Banks From pavlovevidence at gmail.com Sat Jul 25 01:44:04 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 24 Jul 2009 22:44:04 -0700 (PDT) Subject: len() should always return something References: <4YGdnQ-7p7gzIPTXnZ2dnUVZ_oJi4p2d@posted.visi> Message-ID: On Jul 24, 6:57?am, Grant Edwards wrote: > On 2009-07-24, Dr. Phillip M. Feldman wrote: > > > > > Some aspects of the Python design are remarkably clever, while > > others leave me perplexed. Here's an example of the latter: > > Why does len() give an error when applied to an int or float? > > len() should always return something; in particular, when > > applied to a scalar, it should return a value of 1. > > If len(7) returned a value of 1, then wouldn't one expect 7[0] > to be valid? ?It isn't, so you'd then have to redefine all > types so that they are sequences that can be indexed. ?Sounds > like a big mess to me... You can call the big mess "Matlab". Carl Banks From andreengels at gmail.com Sat Jul 25 01:47:14 2009 From: andreengels at gmail.com (Andre Engels) Date: Sat, 25 Jul 2009 07:47:14 +0200 Subject: time in milliseconds by calling time.time() In-Reply-To: References: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> Message-ID: <6faf39c90907242247o4e6d50b6m5871faa7f08b447f@mail.gmail.com> On Sat, Jul 25, 2009 at 3:27 AM, Roy Smith wrote: > Keep in mind that while a float may have a large apparent precision, > there's no promise that the actual value returned by the OS has that much > precision. ?You should be fine if all you're looking for is ms, but I > wouldn't count on much more than that. Even stronger: I wouldn't count on _anything_ more than that. On my machine, time.time() changes value once per millisecond. I tested this by looking at a loop that recorded time.time() 100000 times. The total time in the loop was 61 ms; out of the 100000 numbers, 61 were higher than the previous one, with the highest difference being 1.00017 ms, the lowest 0.999928 ms. -- Andr? Engels, andreengels at gmail.com From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 02:18:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 06:18:23 GMT Subject: time in milliseconds by calling time.time() References: <9c600f0c-f4a0-4e8c-bbb9-27f128aecc50@m7g2000prd.googlegroups.com> Message-ID: <027a94a5$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 07:47:14 +0200, Andre Engels wrote: > On Sat, Jul 25, 2009 at 3:27 AM, Roy Smith wrote: > >> Keep in mind that while a float may have a large apparent precision, >> there's no promise that the actual value returned by the OS has that >> much precision. ?You should be fine if all you're looking for is ms, >> but I wouldn't count on much more than that. > > Even stronger: I wouldn't count on _anything_ more than that. On my > machine, time.time() changes value once per millisecond. I tested this > by looking at a loop that recorded time.time() 100000 times. The total > time in the loop was 61 ms; out of the 100000 numbers, 61 were higher > than the previous one, with the highest difference being 1.00017 ms, the > lowest 0.999928 ms. I'm guessing you're running Windows, because for Windows time.time() has a low resolution and time.clock() is the higher resolution timer. I'm running Linux, which is the opposite: >>> from time import time, clock >>> def diffs(alist): ... deltas = [] ... for i in xrange(1, len(alist)): ... deltas.append( alist[i] - alist[i-1] ) ... return deltas ... >>> d = [time() for i in xrange(10000)] # grab raw times >>> dt = diffs(d) # calculate the difference between each call >>> max(dt), min(dt) (0.00060892105102539062, 1.9073486328125e-06) >>> >>> d = [clock() for i in xrange(10000)] # and again using clock() >>> dc = diffs(d) >>> max(dc), min(dc) (0.010000000000000009, 0.0) More important than the maximum and minimum time deltas is the resolution of ticks in each timer. Under Linux, clock() hardly ever gets updated: >>> len(dc) # how many time deltas? 9999 >>> len(filter(None, dc)) # how many non-zero deltas? 2 while time() gets updated frequently: >>> len(dt) 9999 >>> len(filter(None, dt)) 9999 See also the comments in the timeit module. -- Steven From gagsl-py2 at yahoo.com.ar Sat Jul 25 02:44:14 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 25 Jul 2009 03:44:14 -0300 Subject: trouble with minidom References: <9c8c445f0907211708qc187b53xe5085df6e894f8c9@mail.gmail.com> <9c8c445f0907241056x11ec5316s2ea6310872cd342@mail.gmail.com> Message-ID: En Fri, 24 Jul 2009 14:56:29 -0300, Ronn Ross escribi?: > On Tue, Jul 21, 2009 at 7:32 PM, Gabriel Genellina > wrote: >> En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross >> escribi?: >> >> Hello I'm trying to read an xml file using minidome. The xml looks >> like: >>> >>> >>> myProj >>> /here/ >>> >>> > > I have used the loop below and it works great, but I need get both child > elements or 'project' per iteration. I want to build a dictionary that > resemble this: > my_dict = {'myProj':'/here/', 'anothername':'anotherpath'} > > I couldn't find how to do with in the element tree docs. Can you point > me in > the right direction? py> import xml.etree.ElementTree as ET py> xml = """ ... ... myProj ... /here/ ... ... ... anothername ... anotherpath ... ... """ py> doc = ET.fromstring(xml) py> result = {} py> for project in doc.findall('project'): ... name = project.find("name").text ... path = project.find("path").text ... result[name] = path ... py> result {'anothername': 'anotherpath', 'myProj': '/here/'} The Element interface is documented here: http://docs.python.org/library/xml.etree.elementtree.html#the-element-interface More info in the author's site: http://www.effbot.org/zone/element-index.htm (Perhaps you're using Python 2.5? That part of the documentation was omited in the 2.5 doc set by mistake. Read the 2.6 docs instead, there was no changes) -- Gabriel Genellina From xahlee at gmail.com Sat Jul 25 02:54:22 2009 From: xahlee at gmail.com (Xah Lee) Date: Fri, 24 Jul 2009 23:54:22 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: PHP is popular because it is geared for the server-side web scripting lang, and simpler and easy to use, than popular free alternatives at the time (such as Perl and Java's JSP). Python became popular primarily because its ease-to-read syntax. Btween the two, PHP is much easier to use, and much a pleasure to program in. Python is a pain in the ass. PHP is functional. The language is not elegant, lots of inconsistancies. However, it's a joy to use for any practical task in its web scripting field. PHP has one of the best documentation among open source computer languages, i'd say top 5. Python is a twist, with half-assed lambda, a culty community thinking "computer science R us", and it has this OOP obsession. The Guido guy do not understand functional programing, but has the pleasure to actitively badmouth it. References: ? Language, Purity, Cult, and Deception http://xahlee.org/UnixResource_dir/writ/lang_purity_cult_deception.html ? What Languages to Hate http://xahlee.org/UnixResource_dir/writ/language_to_hate.html ? Lambda in Python 3000 http://xahlee.org/perl-python/python_3000.html ? Python Documentation Problems http://xahlee.org/perl-python/python_doc_index.html ? Examples Of Quality Documentation In The Computing Industry http://xahlee.org/perl-python/quality_docs.html ? Xah's Perl and Python Tutorial http://xahlee.org/perl-python/index.html ? Xah's PHP Tutorial http://xahlee.org/php/index.html Xah ? http://xahlee.org/ ? From andreengels at gmail.com Sat Jul 25 02:55:40 2009 From: andreengels at gmail.com (Andre Engels) Date: Sat, 25 Jul 2009 08:55:40 +0200 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: <6faf39c90907242355m1de6d4e2s6a575a6c7469b67f@mail.gmail.com> On Sat, Jul 25, 2009 at 4:50 AM, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): > ? for x in xs: > ? ? ?print x > > This function works fine if xs is a list of floats, but not if it is single > float. ?It can be made to work as follows: > > def dumbfunc(xs): > ? if isinstance(xs,(int,float,complex)): xs= [xs] > ? for x in xs: > ? ? ?print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. I don't have this problem - when I have a list of one element, that's what I use - a list with one element. I think things would get much simpler if you do such a conversion right at the Matlab-Python interface, rather than changing all kind of things in Python so that its types are more matlab-like. Just like you shouldn't program Python by simply translating Java or C into Python idiom, you also shouldn't program Python by trying to mimic Matlab. Each language has its own strengths and weaknesses, and moving things over too much in a simplistic fashion will usually mean that you get the weaknesses of both and the strengths of neither. -- Andr? Engels, andreengels at gmail.com From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 03:03:02 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 07:03:02 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: <027a9f1a$0$5185$c3e8da3@news.astraweb.com> On Fri, 24 Jul 2009 19:50:54 -0700, Dr. Phillip M. Feldman wrote: > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is > single float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): > xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of > the annoying things about Python. But it's not "practically every function". It's hardly any function at all -- in my code, I don't think I've ever wanted this behavior. I would consider it an error for function(42) and function([42]) to behave the same way. One is a scalar, and the other is a vector -- they're different things, it's poor programming practice to treat them identically. (If Matlab does this, so much the worse for Matlab, in my opinion.) However, if you want this behaviour, it's easy enough to get it with a decorator: from functools import wraps def matlab(func): """Decorate func so that it behaves like Matlab, that is, it considers a single scalar argument to be the same as a vector of length one.""" @wraps(func) def inner(arg, **kwargs): # If arg is already a vector, don't touch it. try: # If this succeeds, it's a vector of some type. iter(arg) except TypeError: # It's a scalar. arg = [arg] # Now call the decorated function (the original). return func(arg, **kwargs) return inner With this decorator in hand, you can now easily get the behaviour you want with a single extra line per function: >>> @matlab ... def mean(numbers): ... return sum(numbers)/len(numbers) ... >>> mean([4.5]) 4.5 >>> mean(4.5) 4.5 >>> mean([4.5, 3.6]) 4.0499999999999998 Decorators are extremely powerful constructs, and well worth learning. As an example, here's a similar decorator that will accept multiple arguments, like the built-in functions min() and max(): def one_or_many(func): """Decorate func so that it behaves like the built-ins min() and max().""" @wraps(func) def inner(*args, **kwargs): # If we're given a single argument, and it's a vector, # use it as args. if len(args) == 1: try: iter(args[0]) except TypeError: pass else: # No exception was raised, so it's a vector. args = args[0] # Now call the decorated function (the original). return func(args, **kwargs) return inner And then use it: >>> @one_or_many ... def minmax(numbers): ... """Return the minimum and maximum element of numbers, ... making a single pass.""" ... # the following will fail if given no arguments ... smallest = biggest = numbers[0] ... for x in numbers[1:]: ... if x < smallest: ... smallest = x ... elif x > biggest: ... biggest = x ... return (smallest, biggest) ... >>> >>> minmax([2, 4, 6, 8, 1, 3, 5, 7]) (1, 8) >>> minmax(2, 4, 6, 8, 1, 3, 5, 7) (1, 8) -- Steven From tayssir.john at googlemail.com Sat Jul 25 03:23:45 2009 From: tayssir.john at googlemail.com (Tayssir John Gabbour) Date: Sat, 25 Jul 2009 00:23:45 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <12ce2d31-5028-4b60-a5b1-5d5bf1fc06d7@f20g2000prn.googlegroups.com> Message-ID: On Jul 24, 11:58 pm, ACL wrote: > I actually think that the thing holding lisp back is 'bus factor'. > > Lets assume I have a java project and a lisp project: > > Java project: > I have maybe 10 or 12 people on my team working on various subsystems > of my project. There are probably one or two 'technical leader' types > in the group, and a bunch of others who are sort of 'serfs', banging > out java classes. Lets say one of my important guys gets totally > splattered by a bus... I've still got another one left! I can rely on > the other guy to keep things afloat while I train up a new leader > type. > > Lisp project: > I don't need as many people. I have 3 or 4 people, and one person is > my technical leader and lisp guru. Guru is probably in charge of more > difficult macros and (because of that), also in charge of the overall > design (macros are design patterns embodied in code). Lets say he gets > totally annihilated by the bus. What do I do now? I had all my eggs in > one basket and my project is now stalled. A Clojure programmer mentioned interesting solutions to this used by his company, such as partnerships with similarly Agile companies. http://programmingtour.blogspot.com/2009/07/conversation-with-stuart-halloway.html I agree that the bus factor -- as well as some other problems -- works against Lisp's acceptance. All the best, Tayssir From pavlovevidence at gmail.com Sat Jul 25 03:49:12 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 25 Jul 2009 00:49:12 -0700 (PDT) Subject: len() should always return something References: Message-ID: On Jul 23, 11:35?pm, "Dr. Phillip M. Feldman" wrote: > Some aspects of the Python design are remarkably clever, while others leave > me perplexed. Here's an example of the latter: Why does len() give an error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. Of > course, I can define my own function like this: > > def mylen(x): > ? ?if isinstance(x,int) or isinstance(x,float): return 1 > ? ?return len(x) > > But, this shouldn't be necessary. I knew that you were coming from Matlab as soon as I saw the subject line. I use both Matlab and Python very often, and I understand the design decisions behind both (insofar as Matlab can be considered a "design"). The thing to keep in mind about Python is that it's a general purpose language, not just for mathematical computation, and there are some things in Python that are a bit suboptimal for math calculations. This is one of them: when the types of objects you are dealing with most often are numbers, then there's not much chance for ambiguity when treating a scalar as a 1x1 matrix. However, when doing general purpose programming, you often use lots of types besides numbers, some of which are containers themselves. This creates the possibility of ambiguity and inconsistent behavior. Let's consider your example function. def dumbfunc(xs): for x in xs: print x You can use it on an list, like this, and there is no ambiguity: dumbfunc([1,2,3]) Now suppose Python allowed numbers to be treated as degenerate sequences of length 1. Then you could pass a number, and it would work as you expect: dumbfunc(1) However, because Python is general purpose, you might also want to pass other types around, such as strings. Say you wanted to print a list of string, you would do this, and it would work as expected: dumbfunc(["abc","def","ghi"]) Now, the problem. In the numbers example, you were able to treat a single number as a degenerate list, and by our supposition, it worked. By analogy, you would expect to be able to do the same with a list of strings, like this: dumbfunc("abc") Whoops: doesn't work. Instead of printing one string "abc" it prints three strings, "a", "b", and "c". By allowing scalar numbers to act as degenerate lists, you've introduced a very bad inconsistency into the language, you've made it difficult to learn the language by analogy. For a general purpose language there is really no second-guessing this design decision. It would be unequivocally bad to have atomic types act like sequences. The reason it isn't so bad in Matlab is that you can rely on the elements of an array to be scalar. Matlab, for its part, does contain some types (cell arrays and structures) that can contain non-scalars for cases when you want to do that. However, you will notice that no scalar will ever be treated as a degenerate cell array or structure. Even a poorly designed, ad hoc language like Matlab won't try to treat scalar numbers as cell arrays, because ambiguity like I described above will appear. Nope, it takes an abomination like Perl to do that. As for how I would recommend you translate the Matlab function that rely on this: you need to decide whether that scalar number you are passing in is REALLY a scalar, or a degenerate vector. If it's the latter, just get over it and pass in a single-item list. I have done these kinds of conversions myself before, and that's the way to do it. Trying to be cute and writing functions that can work with both types will lead to trouble and inconsistency, especially if you are a Python newbie. Just pass in a list if your function expects a list. Carl Banks From hendrik at microcorp.co.za Sat Jul 25 03:50:30 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 09:50:30 +0200 Subject: len() should always return something In-Reply-To: References: Message-ID: <200907250950.30999.hendrik@microcorp.co.za> On Friday 24 July 2009 16:45:40 Mark Dickinson wrote: > On Jul 24, 3:11?pm, "Rhodri James" > > wrote: > > Which doesn't make your point less valid. ?In fact I'd go so > > far as to argue that what len() gives you is the number of > > items in a container, so len(7) should return 0. > > Nah. 7 contains three bits, so len(7) should *clearly* return 3. Almost right - however the first seven that you typed was a string seven and not an int - so the hex is 37 and that takes at least six bits... :-) Now if only someone can figure out a reason why it should return seven, then it would be perfect! - Hendrik From deets at nospam.web.de Sat Jul 25 03:55:26 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 25 Jul 2009 09:55:26 +0200 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: <7cvs3fF24hjh7U1@mid.uni-berlin.de> Dr. Phillip M. Feldman schrieb: > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is single > float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. And where comes "len(xs)" into play here? What you want is iteration over scalars. I do think that if you frequently have to write code like that, you are writing errorneous code. But might that as it is, a simple def iterable(i): if isinstance(i, (int, float, complex)): return [i] return i is all you need. So you can write for x in iterable(xs): wherever you expect values to be either scalar or iterable. Diez From piet at cs.uu.nl Sat Jul 25 04:03:58 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 10:03:58 +0200 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>>>> Steven D'Aprano (S) wrote: >S> Chris, I'm curious why you think that these Zen are relevant to the OP's >S> complaint. >S> Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). >S> Arguably (I wouldn't argue this, but some people might) ints aren't >S> "special enough" to break the rule that len(obj) should always return >S> something. >S> (I don't actually agree, but some people might be able to produce a >S> coherent argument why len() should apply equally to all objects.) >S> Re errors passing silently, the OP doesn't believe that len(42) should be >S> an error, so that's not relevant. >S> And there's nothing ambiguous about len(42). len(42) should be 7.5 million. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From hendrik at microcorp.co.za Sat Jul 25 04:10:32 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 10:10:32 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: Message-ID: <200907251010.32720.hendrik@microcorp.co.za> On Friday 24 July 2009 17:07:30 Inky 788 wrote: > On Jul 23, 3:42?am, Hendrik van Rooyen > > wrote: 8<------------------------ > > Steven showed why you cannot have a mutable thing > > as a key in a dict. > > > > if you think it is contrived, then please consider how you would > > keep track of say the colour of a pixel on a screen at position > > (x,y) - this is about the simplest "natural" tuple format and > > example. > > My guess is that this is probably the way most people do it: > > ~~~~ > #!/usr/bin/env python > > import sys > import random > > if len( sys.argv ) != 3: > print "Please pass exactly 2 ints. Exiting." > sys.exit(1) > > NUM_COLUMNS = int( sys.argv[1] ) > NUM_ROWS = int( sys.argv[2] ) > > print "Making array of %s columns by %s rows." % (NUM_COLUMNS, > NUM_ROWS) > > def rand(): > return int( 255 * random.random()) > > def make_a_pixel(): > # red green blue > return [rand(), rand(), rand()] > > def make_a_row(num_columns): > temp_row = [] > for i in range(num_columns): > temp_row.append( make_a_pixel() ) > return temp_row > > def make_array_of_pixels(num_columns, num_rows): > rows = [] > for i in range(num_rows): > rows.append( make_a_row(num_columns) ) > return rows > > def show_pixels(pixel_array): > for row in pixel_array: > for pixel in row: > print pixel, ' ', > print > > > rows_of_pixels = make_array_of_pixels(NUM_COLUMNS, NUM_ROWS) > > show_pixels(rows_of_pixels) > ~~~~ Good grief! - Not a dictionary in sight! I had something in mind where point was an (x,y) tuple, and point_colours was a dict of possibly named points with a list of RGB values. (The colour can change, the point is fixed) If I were doing it your way, I would use the Array module's Array. But then, strictly speaking it would not be your way any more, would it? - Hendrik From gagsl-py2 at yahoo.com.ar Sat Jul 25 04:31:20 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 25 Jul 2009 05:31:20 -0300 Subject: How do I generate dia diagrams from python source code? References: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Message-ID: En Fri, 24 Jul 2009 17:52:47 -0300, Qauzzix escribi?: > Since I have been using dia to make my UML diagrams. I also found an > util named dia2code that generates python code from dia diagram. Now > that I have that option I really want to find a way to generate dia > diagram from existing code and/or maintain my diagrams. > > I have been googling like crazy trying to find a way but with no > luck. Anyone here know how to do this? SPE does that: http://pythonide.stani.be/ -- Gabriel Genellina From hendrik at microcorp.co.za Sat Jul 25 05:01:46 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 11:01:46 +0200 Subject: len() should always return something In-Reply-To: References: <0279f596$0$5185$c3e8da3@news.astraweb.com> Message-ID: <200907251101.46672.hendrik@microcorp.co.za> On Friday 24 July 2009 21:04:55 Roy Smith wrote: > Compressing strings to a single bit is easy. It's the uncompressing that's > tricky. Not really - all you have to do is to apply the EXACT same sequence of operations that compressed it, in reverse. The unfortunate part is that this information is in almost all cases larger than the original, uncompressed string, so that it kind of defeats the object of compression. So yes - tricky! :-) - Hendrik From dr.mtarver at ukonline.co.uk Sat Jul 25 05:30:16 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Sat, 25 Jul 2009 02:30:16 -0700 (PDT) Subject: strange python scripting error References: Message-ID: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> On 24 July, 15:45, nn wrote: > On Jul 23, 7:03?pm, Dave Angel wrote: > > > > > > > Mark Tarver wrote: > > > I have a very strange error. ?I have two test python files test.py and > > > python.py which contain the following code > > > > #!/usr/bin/python > > > print "Content-type: text/html" > > > print > > > print "" > > > print "
Hello, Linux.com!
" > > > print "" > > > > One file (test.py) works; you call it up and it shows a web page with > > > > Hello, Linux.com > > > > The other fails with a server configuration error. ?Both are running > > > under Linux, same server, same permissions. ?Running a character scan > > > shows that both files contain the same printable characters and are > > > therefore typographically identical. ? They are absolutely the same. > > > > The only hint at a difference I can see is that my ftp program says > > > the files are of unequal lengths. ?test.py is 129 bytes long. > > > python.py 134 bytes long. > > > > A zipped folder containing both files is at > > > >www.lambdassociates.org/weird.zip > > > > Any ideas welcome. > > > > Mark > > > Easiest explanation is that python.py has Windows-style newlines. ?In > > other words, each line ends with 0d0a, rather than the Unix convention > > of 0a. > > > If your server is Unix-based, it can't handle that first line, since it > > has an illegal character (0d) following the > > > #!/usr/bin/python > > > line. ?Convert it to Unix line-endings. > > > DaveA > > Use dos2unix for conversion of the longer file and try again: > > http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm- Hide quoted text - > > - Show quoted text - That sounds the ticket - but is there anything that runs under Windows to do the trick? Mark From hendrik at microcorp.co.za Sat Jul 25 05:34:13 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 11:34:13 +0200 Subject: len() should always return something In-Reply-To: References: Message-ID: <200907251134.13221.hendrik@microcorp.co.za> On Friday 24 July 2009 22:09:15 Marcus Wanner wrote: > First one to correctly decompress the value 0 into an ASCII character > wins the title of the world's most capable hacker :p that is easy. the xor of 0 and 1 is 1, which is ASCII soh, if I remember right. soh is start of header. Burroughs poll select, anyone? - Hendrik From dr.mtarver at ukonline.co.uk Sat Jul 25 05:46:14 2009 From: dr.mtarver at ukonline.co.uk (Mark Tarver) Date: Sat, 25 Jul 2009 02:46:14 -0700 (PDT) Subject: strange python scripting error References: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> Message-ID: <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> On 25 July, 10:30, Mark Tarver wrote: > On 24 July, 15:45, nn wrote: > > > > > > > On Jul 23, 7:03?pm, Dave Angel wrote: > > > > Mark Tarver wrote: > > > > I have a very strange error. ?I have two test python files test.py and > > > > python.py which contain the following code > > > > > #!/usr/bin/python > > > > print "Content-type: text/html" > > > > print > > > > print "" > > > > print "
Hello, Linux.com!
" > > > > print "" > > > > > One file (test.py) works; you call it up and it shows a web page with > > > > > Hello, Linux.com > > > > > The other fails with a server configuration error. ?Both are running > > > > under Linux, same server, same permissions. ?Running a character scan > > > > shows that both files contain the same printable characters and are > > > > therefore typographically identical. ? They are absolutely the same. > > > > > The only hint at a difference I can see is that my ftp program says > > > > the files are of unequal lengths. ?test.py is 129 bytes long. > > > > python.py 134 bytes long. > > > > > A zipped folder containing both files is at > > > > >www.lambdassociates.org/weird.zip > > > > > Any ideas welcome. > > > > > Mark > > > > Easiest explanation is that python.py has Windows-style newlines. ?In > > > other words, each line ends with 0d0a, rather than the Unix convention > > > of 0a. > > > > If your server is Unix-based, it can't handle that first line, since it > > > has an illegal character (0d) following the > > > > #!/usr/bin/python > > > > line. ?Convert it to Unix line-endings. > > > > DaveA > > > Use dos2unix for conversion of the longer file and try again: > > >http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm-Hide quoted text - > > > - Show quoted text - > > That sounds the ticket - but is there anything that runs under Windows > to do the trick? > > Mark- Hide quoted text - > > - Show quoted text - OK, got a version http://www.bastet.com/ has dos2unix.exe for Windows. And it solves the problem. Many thanks all for help on this baffling error Mark From piet at cs.uu.nl Sat Jul 25 06:20:37 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 12:20:37 +0200 Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> Message-ID: >>>>> "Rhodri James" (RJ) wrote: >RJ> The point was, and remains, that this newsgroup gets regular traffic >RJ> from people who expect Python's variables to act like C's variables, >RJ> demonstrating that describing them as "quite compatible" is somewhat >RJ> misleading. So let' study these postings carefully to see what the actual confusion is. I myself think that the confusion is not so much the variable concept but the vale vs. reference semantics of objects. As such the same thing plays a role in Java. Although the binding mechanisms in Java and Python are different I don't think the actual semantics are so much difference as to cause much confusion (with C it could be more). But such a study could reveal that. BTW. Which postings from (say) the last month did you have in mind? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jon at ffconsultancy.com Sat Jul 25 07:02:08 2009 From: jon at ffconsultancy.com (Jon Harrop) Date: Sat, 25 Jul 2009 12:02:08 +0100 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <12ce2d31-5028-4b60-a5b1-5d5bf1fc06d7@f20g2000prn.googlegroups.com> Message-ID: ACL wrote: > Lisp project: > I don't need as many people... Is there any actual evidence of that? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u From piet at cs.uu.nl Sat Jul 25 07:14:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 13:14:08 +0200 Subject: how can a child thread notify a parent thread its status? References: Message-ID: >>>>> davidj411 (d) wrote: >d> could i see an example of this maybe? queue is a shared Queue instance. The parent puts the work in the queue with queue.put(work_object). After all work has been put in the queue it puts a sentinel queue.put(None) Each child thread (consumer) has a loop getting things out of the queue: while True: work = queue.get() if work is None: break #do real stuff using work queue.put(None) #put back the sentinel for other worker threads. #finish (Thanks to Dennis Lee Bieber) Instead of None you can also create a special sentinel object and use that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Sat Jul 25 07:18:58 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 25 Jul 2009 07:18:58 -0400 Subject: strange python scripting error In-Reply-To: <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> References: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> Message-ID: <4A6AEA22.1030207@ieee.org> Mark Tarver wrote: > > >> >>> Use dos2unix for conversion of the longer file and try again: >>> >>> http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm-Hide quoted text - >>> >>> - Show quoted text - >>> >> That sounds the ticket - but is there anything that runs under Windows >> to do the trick? >> >> Mark- Hide quoted text - >> >> - Show quoted text - >> > > OK, got a version > > http://www.bastet.com/ > > has dos2unix.exe for Windows. And it solves the problem. > > Many thanks all for help on this baffling error > > Mark > > I use metapad for simple text editing, see http://en.wikipedia.org/wiki/Metapad It's free, loads very quickly, and has a number of features I like. One of its trivial features is to read/write files in four different formats. In the file menu, you just choose what file-format you need now. Another thing I'd point out is that some ftp programs will do this conversion as the file is being sent between a local DOS machine and a Unix machine on the internet. DaveA From gert.cuykens at gmail.com Sat Jul 25 07:45:31 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 25 Jul 2009 04:45:31 -0700 (PDT) Subject: cgi.fieldstorage() References: <7cu9hhF29gt65U1@mid.uni-berlin.de> <7cv268F29q0j5U1@mid.uni-berlin.de> Message-ID: <13ed9818-aacd-4260-800b-a04177e13681@n11g2000yqb.googlegroups.com> On Jul 25, 2:33?am, "Diez B. Roggisch" wrote: > gert schrieb: > > > On Jul 24, 7:32 pm, "Diez B. Roggisch" wrote: > >> gert schrieb: > > >>> this is a non standard way to store multi part post data on disk > >>> def application(environ, response): > >>> ? ? with open('/usr/httpd/var/wsgiTemp','w') as f: > >>> ? ? ? ? while True: > >>> ? ? ? ? ? ? chunk = environ['wsgi.input'].read(8192).decode('latin1') > >>> ? ? ? ? ? ? if not chunk: break > >>> ? ? ? ? ? ? f.write(chunk) > >>> ? ? response('200 OK',[]) > >>> ? ? return ['complete'] > >>> my question is how do i handle the file, so i can shuffle it into a db > >>> using small chunks of memorie ? > >> I don't think that's possible with the current DB-API. There is no > >> stream-based BLOB-interface (as e.g. JDBC offers). > > >> So the answer certainly depends on your used RDBMS. For oracle, you > >> would be lucky: > > >>http://cx-oracle.sourceforge.net/html/lob.html > > >> Other adapters I don't know about. > > > sqlite :) ok let say for now it would be impossible on a db level, but > > before i reach the impossible, i still need to parse the file to > > prepare the chunks. How do i do that ? How do i get the chunks without > > loading the hole file into memorie ? > > It's memory - memorie might be some nice dutch girl you know :) > > Apart from that, your code above does exactly that - reads the data > chunkwise. If the WSGI-implementation works proper, this will be the > socket's input stream, so there is no memory overhead involved. > > Now of course if you want to have a multi-pass processing of the file > without putting it into memory, then you need to save it to the harddisk > befor. > > But honestly - we are talking about a web-application here. My > DSL-connection has 1 MBit upstream, the average server has at least 2GB > of memory available - so we are talking 20000 seconds uploading time to > fill that memory. Which is about 5 hours. And you are decoding the data > to a certain decoding, so we are not talking about binary data here - > are you really sure memory is an issue? > What about some http upload resume features ? From pavlovevidence at gmail.com Sat Jul 25 07:45:48 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 25 Jul 2009 04:45:48 -0700 (PDT) Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> <8763di5d1d.fsf@benfinney.id.au> Message-ID: <2977b59f-6843-4c2e-86c2-c0fa5dfdcf72@u38g2000pro.googlegroups.com> On Jul 24, 8:14?am, Ben Finney wrote: > Hrvoje Niksic writes: > > The term "variable" is used in the Python language reference and > > elsewhere > > Yes. It should also be abundantly clear from the constant stream of > confused newbies on this point that its usage of that term is different > to what many expect from usage elsewhere. Personally, I haven't noticed a constant stream, just a few here and there. I expect that someone used to C variables will expect Python variables to behave the same way even if you call them something different, so what really is the point? Carl Banks From pavlovevidence at gmail.com Sat Jul 25 07:49:09 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 25 Jul 2009 04:49:09 -0700 (PDT) Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <7fa7aa37-fd9e-4702-85f8-1f9b9a05c54d@i18g2000pro.googlegroups.com> On Jul 24, 11:54?pm, Xah Lee wrote: [snip] > References: > > ? Language, Purity, Cult, and Deception > ?http://xahlee.org/UnixResource_dir/writ/lang_purity_cult_deception.html > > ? What Languages to Hate > ?http://xahlee.org/UnixResource_dir/writ/language_to_hate.html > > ? Lambda in Python 3000 > ?http://xahlee.org/perl-python/python_3000.html > > ? Python Documentation Problems > ?http://xahlee.org/perl-python/python_doc_index.html > > ? Examples Of Quality Documentation In The Computing Industry > ?http://xahlee.org/perl-python/quality_docs.html > > ? Xah's Perl and Python Tutorial > ?http://xahlee.org/perl-python/index.html > > ? Xah's PHP Tutorial > ?http://xahlee.org/php/index.html > > ? Xah > ?http://xahlee.org/ Wow, you leave no stone unturned. > "computer science R us" I'm stealing this. Carl BAnks From marcusw at cox.net Sat Jul 25 08:55:47 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 25 Jul 2009 08:55:47 -0400 Subject: len() should always return something In-Reply-To: References: Message-ID: On 7/25/2009 5:34 AM, Hendrik van Rooyen wrote: > On Friday 24 July 2009 22:09:15 Marcus Wanner wrote: > >> First one to correctly decompress the value 0 into an ASCII character >> wins the title of the world's most capable hacker :p > > that is easy. > > the xor of 0 and 1 is 1, which is ASCII soh, if I remember right. > > soh is start of header. > > Burroughs poll select, anyone? > > - Hendrik > nope, not soh. Marcus From piet at cs.uu.nl Sat Jul 25 08:57:19 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 14:57:19 +0200 Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? References: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> Message-ID: >>>>> ryles (r) wrote: >r> According to http://www.python.org/doc/essays/packages.html: >r> "The import statement first tests whether the item is defined in the >r> package; if not, it assumes it is a module and attempts to load it." >r> However, I've noticed that once a module is imported using the >r> `from pkg import mod' syntax, if its name is deleted from the >r> package namespace then subsequent imports with `from' will fail. This is incorrectly stated. Also on the initial import it will fail, not just on subsequent imports. piet$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pkg import _impl _impl # this is from a print statement in _impl to show that it did import Traceback (most recent call last): File "", line 1, in ImportError: cannot import name _impl >>> According to the documentation the statement from pkg import _impl _temp = __import__('pkg', globals(), locals(), ['_impl'], -1) _impl = _temp._impl This fails because _temp (the imported module) doesn't have a binding for _impl because you deleted it. By the way, if you have a `normal' package that doesn't do anything with the name _impl, after the import the pkg module namespace will have got a binding for _impl although it is not in your code. It will have been put there by the import code. In the following example pkg2/__init__.py just contains the line A = 2 piet$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pkg2 import _impl _impl >>> import sys >>> dir(sys.modules['pkg2']) ['A', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '_impl'] >>> It is not clear to me, however what the order is of the statements in __init__.py and the insertion of _impl in the module namespace. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 08:59:43 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 12:59:43 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> Message-ID: <027af2b3$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 10:03:58 +0200, Piet van Oostrum wrote: >>S> And there's nothing ambiguous about len(42). > > len(42) should be 7.5 million. And "I don't understand your reasoning".upper() should be "Millennium Hand and Shrimp!". Every function would be ambiguous if we treat it as returning an arbitrary result. But we don't -- semantics do matter. We expect that string.upper() should return the string converted to uppercase, and that's not ambiguous because we already know what uppercase means. Even if somebody didn't know what uppercase meant, they would expect that it had a meaning, and it was just a matter of learning what it meant: for example, one might take string.upper() as any of: * convert every letter a..z to A..Z * convert the first letter of each word to A..Z * throw away the argument and return the literal "upper" * count how many characters are in A..Z and so forth. These are some of the infinite number of *possible* meanings to string.upper(), but only one of them is the *actual* meaning. The method isn't ambiguous, because the language designers have chosen one meaning, and having learned what that meaning is, it's the *only* meaning you can legitimately use. If you (generic you) are uncertain what that meaning is, that's uncertainty, not ambiguity. Similarly, we might be uncertain about the meaning of len(42) -- reasonable values it might return are 0, 1, 2, the number of bits in 42, and possibly even 7.5 million as you suggest. But that's our ignorance, due to the fact that we haven't yet agreed on a meaning to len(42), and not ambiguity: having made up our mind what len(42) should mean, it could only mean one thing. However, mixed string/integer addition is a good example of ambiguity. We can add strings: "1" + "2" => "12" and we can add integers: 1 + 2 => 3 but it is ambiguous as to what "1" + 2 should return. Guido might have made Python behave like Perl, and define addition of a string and an int, but there would still be ambiguity in the expression, because we can't tell if the coder intended "1"+"2" and forgot to quote the two, or intended 1+2 and forgot to convert the string "1" to an int. Ambiguity essentially boils down to being unable to reasonably predict the expectation of the coder. I say "reasonably", because if you allow unreasonable situations, everything is "ambiguous": I typed: x = 42 + y but maybe I intended to import the math module, therefore the + operator is ambiguous. Reasonable? No, because we're allowed to assume the coder is rational, and mistakes are small. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sat Jul 25 09:37:51 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 25 Jul 2009 13:37:51 GMT Subject: non-owning references? References: <87d47q5ixx.fsf@benfinney.id.au> <87fxcm5gny.fsf@busola.homelinux.net> <8763di5d1d.fsf@benfinney.id.au> <2977b59f-6843-4c2e-86c2-c0fa5dfdcf72@u38g2000pro.googlegroups.com> Message-ID: <027afba2$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 04:45:48 -0700, Carl Banks wrote: > On Jul 24, 8:14?am, Ben Finney wrote: >> Hrvoje Niksic writes: >> > The term "variable" is used in the Python language reference and >> > elsewhere >> >> Yes. It should also be abundantly clear from the constant stream of >> confused newbies on this point that its usage of that term is different >> to what many expect from usage elsewhere. > > Personally, I haven't noticed a constant stream, just a few here and > there. > > I expect that someone used to C variables will expect Python variables > to behave the same way even if you call them something different, so > what really is the point? >From time to time we get newbies to Python assuming that len(list) is O(N), because the lists they learned about in Comp Sci 101 (or Lisp) are linked lists and traversing a linked list to count the items is O(N). Do you believe that if Python lists were called "arrays", or "waskilators", they would make the same mistake? [Generalisation] C programmers have a mental model for how "variables" behave. They have no such mental model for how "objects bound to names" behave, and so they are less likely to jump to conclusions about the behaviour of name- bindings. So I think you are mostly wrong. However, only mostly -- human beings are generalisers par excellence, or rather, *over*-generalisers par excellence. Experienced coders are likely to rapidly realise that name-binding is very similar to the C variable model, and some percentage of them will over-generalise to assume that name-binding is the same as the C model. But if they do, at least that will be their fault for jumping to conclusions, not our fault for misleading them. -- Steven From piet at cs.uu.nl Sat Jul 25 10:08:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 16:08:27 +0200 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <027af2b3$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>>>> Steven D'Aprano (SD) wrote: >SD> Ambiguity essentially boils down to being unable to reasonably predict >SD> the expectation of the coder. I say "reasonably", because if you allow >SD> unreasonable situations, everything is "ambiguous": That's for me the reason that len(42) is ambiguous. The OP apparently had 1 as expectation, whereas my first thought was the minimal number of bits to represent the number and 7.5 million came later :=). The number of bits I certainly find reasonable, and I would find the number of decimal digits equally reasonable. More so than 1, actually. 1 as the length of an int doesn't give any information. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Sat Jul 25 10:33:52 2009 From: aahz at pythoncraft.com (Aahz) Date: 25 Jul 2009 07:33:52 -0700 Subject: comments? storing a function in an object References: <20efdb6a-c1a5-47dc-8546-7c4ae548e22d@g1g2000pra.googlegroups.com> Message-ID: In article <20efdb6a-c1a5-47dc-8546-7c4ae548e22d at g1g2000pra.googlegroups.com>, Carl Banks wrote: >On Jul 22, 8:38=A0pm, a... at pythoncraft.com (Aahz) wrote: >> In article .com>, >> Carl Banks =A0 wrote: >>> >>>You have to be REALLY REALLY careful not to pass any user-supplied >>>data to it if this is a server running on your computer, of course. >> >> Unless, of course, your users are paying for this service. > >Well, yes, but I assume that by the time you're deliberately letting >users pay to run their programs on your server, you will already have >deployed a full-blown, multi-tiered security strategy that includes >validation by the server process. That was sort of beyond the scope >of the OP's question. That's not necessarily a good assumption. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From aahz at pythoncraft.com Sat Jul 25 11:20:51 2009 From: aahz at pythoncraft.com (Aahz) Date: 25 Jul 2009 08:20:51 -0700 Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> Message-ID: In article <37da38d2-09a8-4fd2-94b4-5feae9675dcd at k1g2000yqf.googlegroups.com>, Filip wrote: > >I tried to fix that with BeautifulSoup + regexp filtering of some >particular cases I encountered. That was slow and after running my >data scraper for some time a lot of new problems (exceptions from >xpath parser) were showing up. Not to mention that BeautifulSoup >stripped almost all of the content from some heavily broken pages >(50+KiB page stripped down to some few hundred bytes). Character >encoding conversion was a hell too - even UTF-8 pages had some non- >standard characters causing issues. Have you tried lxml? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From aahz at pythoncraft.com Sat Jul 25 12:12:49 2009 From: aahz at pythoncraft.com (Aahz) Date: 25 Jul 2009 09:12:49 -0700 Subject: strange error when trying to log something References: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Message-ID: In article , Peter Otten <__peter__ at web.de> wrote: > >I have a hunch that you are triggering a reload() somewhere. Example: > >Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) >[GCC 4.3.3] on linux2 >Type "help", "copyright", "credits" or "license" for more information. >>>> import weakref >>>> weakref.WeakValueDictionary() > >>>> import UserDict >>>> reload(UserDict) > >>>> weakref.WeakValueDictionary() >Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.6/weakref.py", line 51, in __init__ > UserDict.UserDict.__init__(self, *args, **kw) >TypeError: unbound method __init__() must be called with UserDict instance >as first argument (got WeakValueDictionary instance instead) Nice sleuthing! How did you figure that out? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From hendrik at microcorp.co.za Sat Jul 25 12:15:37 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 25 Jul 2009 18:15:37 +0200 Subject: len() should always return something In-Reply-To: <027af2b3$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <027af2b3$0$5185$c3e8da3@news.astraweb.com> Message-ID: <200907251815.37762.hendrik@microcorp.co.za> On Saturday 25 July 2009 14:59:43 Steven D'Aprano wrote: > On Sat, 25 Jul 2009 10:03:58 +0200, Piet van Oostrum wrote: > >>S> And there's nothing ambiguous about len(42). > > > > len(42) should be 7.5 million. > > And "I don't understand your reasoning".upper() should be "Millennium > Hand and Shrimp!". That does it. I will be kinder to you in future. Anybody who quotes foul old Ron cannot be all bad. - Hendrik From albert at spenarnc.xs4all.nl Sat Jul 25 13:27:52 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 25 Jul 2009 17:27:52 GMT Subject: missing 'xor' Boolean operator References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: In article , Jean-Michel Pichavant wrote: >Christian Heimes wrote: >> Chris Rebert wrote: >> >>> Using the xor bitwise operator is also an option: >>> bool(x) ^ bool(y) >>> >> >> I prefer something like: >> >> bool(a) + bool(b) == 1 >> >> It works even for multiple tests (super xor): >> >> if bool(a) + bool(b) + bool(c) + bool(d) != 1: >> raise ValueError("Exactly one of a, b, c and d must be true") >> >> Christian >> >> >While everyone's trying to tell the OP how to workaround the missing xor >operator, nobody answered the question "why is there no xor operator ?". > >If the question was "Why is there no 'or' operator ?", would "because A >or B <=> not(not A and not B)" be a proper answer ? No. I think it is because and/or can be extended to be sensible in a context where objects can be used. (What others have expressed as having short-circuit evaluation. So sce indeed is the underlying reason that and/or can be extended sensibly to objects.) Remains whether we need an xor that only works and requires that both operands are booleans. That one we have already! It is called != . (a!=b)!=c and a!=(b!=c) are the same for booleans, so can indeed be expressed a!=b!=c (associativy of xor) > >JM > Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From tjreedy at udel.edu Sat Jul 25 13:30:54 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 25 Jul 2009 13:30:54 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: Albert van der Horst wrote: > Remains whether we need an xor that only works and requires that > both operands are booleans. That one we have already! > It is called != . > > (a!=b)!=c > and > a!=(b!=c) > > are the same for booleans, so can indeed be expressed > a!=b!=c (associativy of xor) Not in Python >>> a,b,c = True, False, True >>> (a!=b)!=c False >>> a!=(b!=c) False >>> a!=b!=c True >>> (a!=b) and (b!=c) True In Math and Python, a>> (a^b)^c False >>> a^b^c False Terry Jan Reedy From scriptlearner at gmail.com Sat Jul 25 13:32:06 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Sat, 25 Jul 2009 10:32:06 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? References: Message-ID: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> First of all, let me say thank you to all of you. I have asked many questions (some of them are dump questions), and you have kindly helped me. I am not going to reply every message to say thank-you since that would be annoying for such group with such high daily traffics. Thank you very much. Let's get back to topic of this message. Here's how I have implemented it so far, and I am taking the queue of work load items approach. In my child thread, I will keep checking for available work load item until a duration is reached. #inside the child# while endTime > time.time(): try: item = self.q.get(True, 3) except Queue.Empty: #what's wrong? AttributeError: class Queue has no attribute 'Empty' print 'cant find any work load item, so lets wait and try again later' time.sleep(1) #wait and then check again continue except: print "Unexpected error:", sys.exc_info()[0] raise #do the real work with load item In my parent thread, I will initialize X (depending on a cfg file) child threads and keep adding load items to a shared q until the duration is reached. #inside the parent# callCounter = 0 workers = [] #a list of child threads totalWorkers = 250 endTime = time.time() + duration for i in range(totalWorkers): w = Worker(q, duration, i) w.start() #worker, do your job now! workers.append(w) while endTime > time.time(): time.sleep(1) q.put(getWorkloadItem()) #add workload itmes callCounter += 1 #actually can we guarantee that the call will be sent?? #should we ask each child to report the number of calls they make? for i in range(totalWorkers): workers[i].join() # Wait for the child threads to finish Overall, it seems to be working now. Though, I still have a couple of problems to resolve. 1. I got the following error for the codes that attempt to catch Empty Queue exception. What's the right way to use it? except Queue.Empty: AttributeError: class Queue has no attribute 'Empty' 2. What's the best way to have each child thread to report the number of requests they send when they are done? To add the numbers to another queue? 3. I will need to do some logging for response time as well as some response contents. I have two choices, one big log file for all threads (both child and parent), and one log file for each thread. Given the fact that I may have to log tons of data, I think opening and maintaining a bunch of smaller logs may be better than dealing with a big one (it may grow very fast). Is there any best prastice for logging in Python? If I change my mind and go with one big log file (pass it to each thread), is there anything I should be aware of for multi-thread access (writting) to the same log file? Again, thank you. From albert at spenarnc.xs4all.nl Sat Jul 25 14:01:33 2009 From: albert at spenarnc.xs4all.nl (Albert van der Horst) Date: 25 Jul 2009 18:01:33 GMT Subject: Why aren't OrderedDicts comparable with < etc? References: <02746997$0$5185$c3e8da3@news.astraweb.com> Message-ID: In article , Jack Diederich wrote: >On Mon, Jul 20, 2009 at 10:00 AM, Steven >D'Aprano wrote: >> On Mon, 20 Jul 2009 09:34:24 +0000, Sion Arrowsmith wrote: >> >>> Terry Reedy =A0 wrote: >>>>Sion Arrowsmith wrote: >>>>> Jack Diederich =A0 wrote: >>>>>> It isn't an OrderedDict thing, it is a comparison thing. =A0Two regul= >ar >>>>>> dicts also raise an error if you try to LT them. >>>>> Python 2.5.2 >>>>>>>> d1 =3D dict((str(i), i) for i in range (10)) d2 =3D dict((str(i), i= >) >>>>>>>> for i in range (20)) d1 < d2 >>>>> True >>>>Try reversing the definitions of d1 and d2. The dicts are probably being >>>>compared by id (address), which is the 2.x CPython default. >>> >>> Like this? >>> >>>>>> d1 =3D dict((str(i), i) for i in range (20)) >>>>>> d2 =3D dict((str(i), i) for i in range (10)) >>>>>> d1 < d2 >>> False >>>>>> id(d1) < id(d2) >>> True >>> >>> I didn't know that comparison for anything other than equality defaulted >>> to using id. That really is rather broken, and I'm glad 3.0 fixed it. >> >> >> I don't think comparisons other than equality use id. That would be >> rather insane. If anyone can demonstrate such comparisons in a built-in >> or standard library class, I'd like to see it. >> >> >> For the record, dicts have been comparable with < and > since at least >> Python 1.5: >> >> $ python1.5 >> Python 1.5.2 (#1, Apr =A01 2009, 22:55:54) =A0[GCC 4.1.2 20070925 (Red Ha= >t >> 4.1.2-27)] on linux2 >> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>>> >>>>> {1: 'a', 2: 'b'} < {2: 'b', 3: 'a'} >> 1 >> >> >> Reading the docs: >> >> http://docs.python.org/library/stdtypes.html#comparisons >> http://docs.python.org/library/stdtypes.html#mapping-types-dict >> >> I can only suggest that dicts compare in an arbitrary but consistent >> fashion. > >I should have specified 3.x but since we were talking about >OrderedDicts (only in 3.1) I didn't say it explicitly. Earlier >versions of python were very permissive with comparisons -- it was >rare that any cmp() raised an error and the ultimate fallback was on >the object's id. This is one of the non-backwards compatible changes >in 3k. Now comparing two of the same thing that don't have an obvious >ordering is an error. Is a dict "greater than" if it has a larger >size? if its max key is larger? what does "max key" mean when the >keys aren't even comparable?. Comparing things that aren't extremely >similar is an error now also. With regard to < and > you are right. But I think there is a sensible == w.r.t. dict's. It is to mean that for each key dict1(key) == dict2(key) (implying that their key set must be the same) [I could have used that for one of the euler problems. You have a 4 by 4 field containing a red or blue square. That is naturally a mapping of (1,1) ..(4,4) tuples to one of the objects `blue' `red'. After moving a square you want to know whether this is a map you already have encountered.] > >-Jack -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst From kawenks at gmail.com Sat Jul 25 14:04:45 2009 From: kawenks at gmail.com (allan) Date: Sat, 25 Jul 2009 11:04:45 -0700 (PDT) Subject: Form/Template Fill-in the blanks Message-ID: <4b77a992-370d-4879-88a0-fdd6a23f7c5d@p10g2000prm.googlegroups.com> I'm attempting to create a generic python script that will take an EDI document specification and a database of transactions and be able to generate a raw EDI file (ansi X12). I'm looking for ideas on how best to use the strengths of Python to implement this. I've initially tackled the EDI 812 specifications and narrowed this down to a specific implementation by a company. This narrowed down version is what I want to achieve for output. But I want the script to be generic enough such that I can plug-in another EDI specification + a database transaction set and it will output the proper raw file accordingly. My initial thought was to use: 1. .ini files to declare the EDI configuration 2. Use SQLAlchemy as ORB to simplify access to database objects. INI file configuration: * A "root" INI file indicates other INI files that define each segment of the EDI document. * Each segment INI defines the data elements of each segment and the behavior of the segment (one instance or multiple-instance as in a loop, sequence order, segment code, etc.) * Specify data elements as either constant, system function (like datetime), database field or object method (object being the segment's) * Load all the ini configuration into a "template" object. Each segment ini maps to its own segment object. DB using SQLAlchemy Gather a Trading Partner data and Credit Transaction (EDI 812 remember?) into one dictionary object Gather Credit Transaction details into another dictionary object where it can generate the multiple instance segments The heart of the matter is how to fuse together the template and the data from the dictionary objects efficiently. It should be generic enough to take another set of data dictionary and another template to generate a completely new EDI document. I'm stuck at this fusing together thing. Is this a good approach? Is there an easier to implement approach? Comments, suggestions, questions please. Allan From piet at cs.uu.nl Sat Jul 25 14:18:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 25 Jul 2009 20:18:46 +0200 Subject: Why aren't OrderedDicts comparable with < etc? References: <02746997$0$5185$c3e8da3@news.astraweb.com> Message-ID: >>>>> Albert van der Horst (AvdH) wrote: >AvdH> With regard to < and > you are right. >AvdH> But I think there is a sensible == w.r.t. dict's. >AvdH> It is to mean that for each key dict1(key) == dict2(key) >AvdH> (implying that their key set must be the same) >AvdH> [I could have used that for one of the euler problems. >AvdH> You have a 4 by 4 field containing a red or blue square. >AvdH> That is naturally a mapping of (1,1) ..(4,4) tuples to one >AvdH> of the objects `blue' `red'. After moving a square you >AvdH> want to know whether this is a map you already have encountered.] So what's the problem? piet$ python3 Python 3.1 (r31:73578, Jun 27 2009, 21:49:46) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from collections import OrderedDict >>> a = OrderedDict() >>> b = OrderedDict() >>> a[1]=2 >>> b[1]=2 >>> a[3]=4 >>> b[3]=4 >>> a==b True >>> b[5]=6 >>> a==b False >>> d1 = dict((str(i), i) for i in range (10)) >>> d2 = dict((str(i), i) for i in range (10)) >>> d1 == d2 True -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From constant.beta at gmail.com Sat Jul 25 14:30:54 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Sat, 25 Jul 2009 11:30:54 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones Message-ID: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Hi, Is there a way to tell if a generator has been exhausted using pure Python code? I've looked at CPython sources and it seems that something like "active"/"exhausted" attribute on genobject is missing from the API. For the time being I am using a simple C extension to look at f_stacktop pointer of the generator frame, which seems to differentiate active generators from exhausted ones. See http://bazaar.launchpad.net/~ruby/pythoscope/support-python2.3/annotate/286/pythoscope/_util.c#L16 for complete source code. I may be missing something obvious here. Is there a better way to tell if a given generator object is still active or not? Cheers, mk From python at mrabarnett.plus.com Sat Jul 25 14:39:24 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 25 Jul 2009 19:39:24 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> Message-ID: <4A6B515C.9040803@mrabarnett.plus.com> scriptlearner at gmail.com wrote: > First of all, let me say thank you to all of you. I have asked many > questions (some of them are dump questions), and you have kindly > helped me. I am not going to reply every message to say thank-you > since that would be annoying for such group with such high daily > traffics. Thank you very much. > > Let's get back to topic of this message. > Here's how I have implemented it so far, and I am taking the queue of > work load items approach. > In my child thread, I will keep checking for available work load item > until a duration is reached. > #inside the child# > while endTime > time.time(): > try: > item = self.q.get(True, 3) > except Queue.Empty: #what's wrong? AttributeError: class > Queue has no attribute 'Empty' > print 'cant find any work load item, so lets wait and > try again later' > time.sleep(1) #wait and then check again > continue > except: > print "Unexpected error:", sys.exc_info()[0] > raise > #do the real work with load item > > In my parent thread, I will initialize X (depending on a cfg file) > child threads and keep adding load items to a shared q until the > duration is reached. > #inside the parent# > callCounter = 0 > workers = [] #a list of child threads > totalWorkers = 250 > endTime = time.time() + duration > for i in range(totalWorkers): > w = Worker(q, duration, i) > w.start() #worker, do your job now! > workers.append(w) > > while endTime > time.time(): > time.sleep(1) > q.put(getWorkloadItem()) #add workload itmes > callCounter += 1 #actually can we guarantee that the > call will be sent?? > #should we ask each child to report > the number of calls they make? > > for i in range(totalWorkers): > workers[i].join() # Wait for the child threads to > finish > > > Overall, it seems to be working now. Though, I still have a couple of > problems to resolve. > 1. I got the following error for the codes that attempt to catch Empty > Queue exception. What's the right way to use it? > except Queue.Empty: > AttributeError: class Queue has no attribute 'Empty' > The exception 'Empty' belongs to the module, not the class. Try importing as: from Queue import Queue, Empty > 2. What's the best way to have each child thread to report the number > of requests they send when they are done? To add the numbers to > another queue? > Why not? :-) > 3. I will need to do some logging for response time as well as some > response contents. I have two choices, one big log file for all > threads (both child and parent), and one log file for each thread. > Given the fact that I may have to log tons of data, I think opening > and maintaining a bunch of smaller logs may be better than dealing > with a big one (it may grow very fast). Is there any best prastice > for logging in Python? If I change my mind and go with one big log > file (pass it to each thread), is there anything I should be aware of > for multi-thread access (writting) to the same log file? > > Again, thank you. If you like threads then you could put the log items into a queue and have another thread writing them to the logfile. :-) BTW, do you really need 250 threads? Seems like a lot. I notice that you stop putting items into the queue when endTime is reached and also the threads terminate when endTime is reached. If items are put into the queue faster than they're taken out (or an item is put in just before endTime) then there might still be unprocessed items in the queue at endTime. From rknop at pobox.com Sat Jul 25 14:57:55 2009 From: rknop at pobox.com (Rob Knop) Date: Sat, 25 Jul 2009 13:57:55 -0500 Subject: RSA cryptography between Python and Java Message-ID: I've created an RSA key in Java. I have exported the public key by making it into a X509EncodedKeySpec and spitting out the result of getEncoded(). I want to use this public key to encode something in python that I will send to Java, and then decode in Java with the corresponding private key. Are there any python libraries that will take a public key in this format and do RSA encoding on it? -- --Rob Knop E-mail: rknop at pobox.com Home Page: http://www.pobox.com/~rknop/ Blog: http://www.sonic.net/~rknop/blog/ From http Sat Jul 25 15:01:07 2009 From: http (Paul Rubin) Date: 25 Jul 2009 12:01:07 -0700 Subject: RSA cryptography between Python and Java References: Message-ID: <7xocr8si30.fsf@ruckus.brouhaha.com> Rob Knop writes: > Are there any python libraries that will take a public key in this > format and do RSA encoding on it? Try www.trevp.com/tlslite From rylesny at gmail.com Sat Jul 25 15:11:23 2009 From: rylesny at gmail.com (ryles) Date: Sat, 25 Jul 2009 12:11:23 -0700 (PDT) Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? References: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> Message-ID: On Jul 25, 8:57?am, Piet van Oostrum wrote: > >>>>> ryles (r) wrote: > >r> According tohttp://www.python.org/doc/essays/packages.html: > >r> "The import statement first tests whether the item is defined in the > >r> package; if not, it assumes it is a module and attempts to load it." > >r> However, I've noticed that once a module is imported using the > >r> `from pkg import mod' syntax, if its name is deleted from the > >r> package namespace then subsequent imports with `from' will fail. > > This is incorrectly stated. Also on the initial import it will fail, not > just on subsequent imports. > > piet$ python > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > Type "help", "copyright", "credits" or "license" for more information.>>> from pkg import _impl > > _impl # this is from a print statement in _impl to show that it did import > Traceback (most recent call last): > ? File "", line 1, in > ImportError: cannot import name _impl > Well, since __init__.py is executed first, I would regard it as causing the initial (successful) import, and yours being the subsequent one, so that the "initial" import does not fail, the subsequent one does. Wording aside, though, I think we are on the same page here. > > According to the documentation > the statement > from pkg import _impl > > _temp = __import__('pkg', globals(), locals(), ['_impl'], -1) > _impl = _temp._impl > > This fails because _temp (the imported module) doesn't have a binding > for _impl because you deleted it. But supposing we hadn't deleted it, and __init__.py didn't import _impl, there would still be no binding. Yet the importer would still import and make the module available (presumably it "knows" to do this since it was not already in sys.modules). The question really is that since in our example pkg._impl is still in sys.modules, why won't the importer add it to the pkg namespace again as it previous had? I would imagine this is either by design, or is a case that was never considered. > for _impl because you deleted it. By the way, if you have a `normal' > package that doesn't do anything with the name _impl, after the import > the pkg module namespace will have got a binding for _impl although it is not > in your code. It will have been put there by the import code. Yes, this was noted further down in the original post with an example. From tom.sully at gmx.com Sat Jul 25 15:27:37 2009 From: tom.sully at gmx.com (Tom) Date: Sat, 25 Jul 2009 20:27:37 +0100 Subject: Removing newlines from string on windows (without replacing) Message-ID: <1248550057.5509.86.camel@ubuntu.ubuntu-domain> This is my first post to this mailing list, so hi :) I have an annoying problem. While I mainly use Linux when I distribute this program to friends and on the internet, it'll get used on Windows. So, I tested my python program on my Windows Vista dual boot, running the same version of python (2.6) as my Linux, and got an error at the following code. s = sauce.replace("\n", "") Sauce is a string, read from a file, that I need to remove newlines from. This code works fine in Linux, but not in Windows. rstrip("\n") won't work for me, so anybody know how to get this working on Windows? Thanks! :D --print "Tom" From rhodri at wildebst.demon.co.uk Sat Jul 25 15:53:51 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sat, 25 Jul 2009 20:53:51 +0100 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: On Sat, 25 Jul 2009 03:50:54 +0100, Dr. Phillip M. Feldman wrote: > > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is > single > float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of > the > annoying things about Python. If you persist in treating as if it was , then your code will always be ugly, and often buggy. Unless we're talking natural languages, in which case Yoda-like you will sound. Fundamentally, your problem is your assertion that it is reasonable to allow users to treat a single object as if it were wrapped in a list. In Python, it is not reasonable, for the reasons that you are spending so much time complaining about. -- Rhodri James *-* Wildebeest Herder to the Masses From tack at urandom.ca Sat Jul 25 16:00:02 2009 From: tack at urandom.ca (Jason Tackaberry) Date: Sat, 25 Jul 2009 16:00:02 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <1248552002.4390.2.camel@willow> On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: > Is there a way to tell if a generator has been exhausted using pure > Python code? I've looked at CPython sources and it seems that Upon a cursory look, after a generator 'gen' is exhausted (meaning gen.next() has raised StopIteration), it seems that gen.gi_frame will be None. Cheers, Jason. From rknop at pobox.com Sat Jul 25 16:08:17 2009 From: rknop at pobox.com (Rob Knop) Date: Sat, 25 Jul 2009 15:08:17 -0500 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: Paul Rubin writes: > www.trevp.com/tlslite Thanks, but that looks like a library for setting up a secure connection between two ends. What I need is the ability to encrypt with a public key in Python, where that public key was generated in Java as described, and where the cipertext can later be decrypted with the corresponding secret key in Java. -- --Rob Knop E-mail: rknop at pobox.com Home Page: http://www.pobox.com/~rknop/ Blog: http://www.sonic.net/~rknop/blog/ From rhodri at wildebst.demon.co.uk Sat Jul 25 16:08:54 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sat, 25 Jul 2009 21:08:54 +0100 Subject: Removing newlines from string on windows (without replacing) In-Reply-To: <1248550057.5509.86.camel@ubuntu.ubuntu-domain> References: <1248550057.5509.86.camel@ubuntu.ubuntu-domain> Message-ID: On Sat, 25 Jul 2009 20:27:37 +0100, Tom wrote: > This is my first post to this mailing list, so hi :) > > I have an annoying problem. While I mainly use Linux when I distribute > this program to friends and on the internet, it'll get used on Windows. > So, I tested my python program on my Windows Vista dual boot, running > the same version of python (2.6) as my Linux, and got an error at the > following code. > > s = sauce.replace("\n", "") What error? (I don't let Vista in the house.) > Sauce is a string, read from a file, that I need to remove newlines > from. This code works fine in Linux, but not in Windows. rstrip("\n") > won't work for me, so anybody know how to get this working on Windows? Why won't rstrip("\n") work? Is rstrip() OK instead, or does trailing whitespace matter to you? To provide a partial answer, your problem probably lies in the different ways Windows and Linux treat ends of lines. Under Linux, "\n" is the line terminator. Under Windows, it's "\r\n". If you opened your file in "byte mode" with open("myfile.txt", "rb"), you will be given all the bytes in the file, including those extra "\r" characters on Windows. If you open your file in text mode with open("myfile.txt, "r") instead, the line endings are converted to "\n" on both Windows and Linux. If my crystal ball has proved faulty, giving us more details will help get a more sensible answer. -- Rhodri James *-* Wildebeest Herder to the Masses From http Sat Jul 25 16:10:43 2009 From: http (Paul Rubin) Date: 25 Jul 2009 13:10:43 -0700 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: <7xiqhgmsl8.fsf@ruckus.brouhaha.com> Rob Knop writes: > > www.trevp.com/tlslite > > Thanks, but that looks like a library for setting up a secure connection > between two ends. What I need is the ability to encrypt with a public > key in Python, where that public key was generated in Java as described, > and where the cipertext can later be decrypted with the corresponding > secret key in Java. Yes, I think that library has the function that you want. It's just X509 DER encoding. From constant.beta at gmail.com Sat Jul 25 16:20:11 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Sat, 25 Jul 2009 13:20:11 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> On Jul 25, 10:00?pm, Jason Tackaberry wrote: > On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: > > Is there a way to tell if a generator has been exhausted using pure > > Python code? I've looked at CPython sources and it seems that > > Upon a cursory look, after a generator 'gen' is exhausted (meaning > gen.next() has raised StopIteration), it seems that gen.gi_frame will be > None. Only in Python 2.5 or higher though. I need to support Python 2.3 and 2.4 as well, sorry for not making that clear in the original post. Cheers, mk From news123 at free.fr Sat Jul 25 16:29:29 2009 From: news123 at free.fr (News123) Date: Sat, 25 Jul 2009 22:29:29 +0200 Subject: available formats and params for Image.save() In-Reply-To: References: <4a6833dd$0$22599$426a34cc@news.free.fr> Message-ID: <4a6b6b29$0$435$426a74cc@news.free.fr> Thanks a lot Gabriel, Your answer is perfect. I was only looking in part II of the PIL manual and overlooked the Appendix :-(. I also apreciate Image.ID , Image.SAVE , Image.OPEN I saw them when typing dir Image but I never had the idea of calling Image.init() first bye N Gabriel Genellina wrote: > En Thu, 23 Jul 2009 06:56:45 -0300, News123 escribi?: > >> Somehow I have difficulties reading the documentation for PIL (Image) >> >> Is there an easy way to know which formats are supported and what their >> names are? > > py> import PIL > py> from PIL import Image > py> Image.ID > [] > py> Image.init() > py> Image.ID > ['PNG', 'ARG', 'BMP', 'BUFR', 'CUR', 'PCX', 'DCX', 'EPS', 'FITS', 'FLI', > 'FPX', > 'GBR', 'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM', 'IMT', 'IPTC', > 'JPEG', 'MCIDA > S', 'TIFF', 'MIC', 'MPEG', 'MSP', 'PCD', 'PIXAR', 'PPM', 'PSD', 'SGI', > 'SPIDER', > 'SUN', 'TGA', 'WBMP', 'WMF', 'XBM', 'XPM', 'XVTHUMB'] > py> Image.OPEN.keys() > ['PCX', 'ICNS', 'HDF5', 'SUN', 'MIC', 'EPS', 'MSP', 'FLI', 'FITS', > 'GBR', 'WBMP' > , 'PCD', 'PIXAR', 'BUFR', 'PPM', 'WMF', 'SGI', 'BMP', 'TGA', 'DCX', > 'ICO', 'CUR' > , 'XPM', 'TIFF', 'JPEG', 'SPIDER', 'GIF', 'GRIB', 'IM', 'IMT', 'IPTC', > 'FPX', 'X > BM', 'MPEG', 'PSD', 'ARG', 'XVTHUMB', 'PNG', 'MCIDAS'] > py> Image.SAVE.keys() > ['XBM', 'PCX', 'SPIDER', 'HDF5', 'TIFF', 'BUFR', 'EPS', 'JPEG', 'MSP', > 'GRIB', ' > GIF', 'BMP', 'IM', 'PPM', 'PDF', 'FITS', 'PALM', 'WBMP', 'WMF', 'PNG'] > >> Is there an easy way to know which parameters are supported by >> Image.save(). How can I list them where are they documented? > > That depends on the format being used. The PIL handbook lists the > standard formats used and their parameters: > http://www.pythonware.com/library/pil/handbook/index.htm > >> I'm at a complete loss at finding out what parameters the save function >> accepts for saving a JPG file or a PNG file > > http://www.pythonware.com/library/pil/handbook/format-jpeg.htm > From mwawrzyczek at gmail.com Sat Jul 25 16:33:01 2009 From: mwawrzyczek at gmail.com (marekw2143) Date: Sat, 25 Jul 2009 13:33:01 -0700 (PDT) Subject: Adding method from one class to another class or to instance of another class References: <6d9d960e-7f05-4fd8-a0a8-56bea4be0c45@e27g2000yqm.googlegroups.com> Message-ID: <953308c3-671f-4604-9562-d37767e600a4@g6g2000vbr.googlegroups.com> Thanks for your responses. im_func is all I need. I considered subclassing, wchih is more easy to extend, but I needed some quick way to add a method to another class. Regards, Marek From robert.kern at gmail.com Sat Jul 25 16:57:23 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 25 Jul 2009 15:57:23 -0500 Subject: len() should always return something In-Reply-To: <24654439.post@talk.nabble.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <24654439.post@talk.nabble.com> Message-ID: On 2009-07-24 21:50, Dr. Phillip M. Feldman wrote: > Here's a simple-minded example: > > def dumbfunc(xs): > for x in xs: > print x > > This function works fine if xs is a list of floats, but not if it is single > float. It can be made to work as follows: > > def dumbfunc(xs): > if isinstance(xs,(int,float,complex)): xs= [xs] > for x in xs: > print x > > Having to put such extra logic into practically every function is one of the > annoying things about Python. I have spent the last ten years writing scientific code in Python (i.e. that which otherwise might be written in Matlab), and I can guarantee you that you do not need to put such extra logic in practically every function. Even when naively translating code from Matlab, it's not often necessary. By the way, are you familiar with numpy? If you are converting code from Matlab, you will almost certainly need it. We have a number of functions that make these kinds of operations easy when they are in fact necessary. For example, we have isscalar() and atleast_1d(). def dumbfunc(xs): xs = numpy.atleast_1d(xs) for x in xs: print x http://numpy.scipy.org/ -- 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 Sat Jul 25 16:59:30 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 25 Jul 2009 15:59:30 -0500 Subject: len() should always return something In-Reply-To: <7cvs3fF24hjh7U1@mid.uni-berlin.de> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <7cvs3fF24hjh7U1@mid.uni-berlin.de> Message-ID: On 2009-07-25 02:55, Diez B. Roggisch wrote: > Dr. Phillip M. Feldman schrieb: >> Here's a simple-minded example: >> >> def dumbfunc(xs): >> for x in xs: >> print x >> >> This function works fine if xs is a list of floats, but not if it is >> single >> float. It can be made to work as follows: >> >> def dumbfunc(xs): >> if isinstance(xs,(int,float,complex)): xs= [xs] >> for x in xs: >> print x >> >> Having to put such extra logic into practically every function is one >> of the >> annoying things about Python. > > And where comes "len(xs)" into play here? What you want is iteration > over scalars. He explained in another post that iteration is another feature along the same lines that he would want for scalars. -- 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 lie.1296 at gmail.com Sat Jul 25 18:30:30 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 26 Jul 2009 08:30:30 +1000 Subject: Failed Regression Test: What socket.gethostname() is supposed to return? Message-ID: In my laptop, socket.gethostname() returned my username, and causing one of python's "make test" regression test to error (test_socket): ====================================================================== ERROR: testSockName (test.test_socket.GeneralModuleTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/var/tmp/portage/dev-lang/python-2.5.4-r3/work/Python-2.5.4/Lib/test/test_socket.py", line 456, in testSockName my_ip_addr = socket.gethostbyname(socket.gethostname()) gaierror: (-2, 'Name or service not known') ---------- since on my system socket.gethostname() returns 'lieryan', and since socket.gethostbyname('lieryan') does not resolve to anything; the test becomes an error. My system is Gentoo, but I think this also happened on Ubuntu (still on this laptop). The trunk failed in similar manner. Do I have a misconfigured system or is the test faulty? For convenience, the relevant test code (taken from trunk): ======================= def testSockName(self): # Testing getsockname() port = self._get_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(("0.0.0.0", port)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. # At least for eCos. This is required for the S/390 to pass. my_ip_addr = socket.gethostbyname(socket.gethostname()) self.assertTrue(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) self.assertEqual(name[1], port) ======================= lieryan at lieryan ~/Desktop/pythontrunk/trunk $ ./python -m test.regrtest test_socket Could not find '/home/lieryan/Desktop/pythontrunk/trunk/Lib/test' in sys.path to remove it test_socket test test_socket failed -- Traceback (most recent call last): File "/home/lieryan/Desktop/pythontrunk/trunk/Lib/test/test_socket.py", line 493, in testSockName my_ip_addr = socket.gethostbyname(socket.gethostname()) gaierror: [Errno -2] Name or service not known 1 test failed: test_socket I tracked the code for socket.gethostname() and socket.gethostbyname() and found that they are simply a wrapper for gethostname() from #import (http://linux.die.net/man/2/gethostname) and gethostbyname() from #import (http://linux.die.net/man/3/gethostbyname). A simple test in C found that the C's equivalent to gethostbyname(gethostname()) returns a null pointer (used to indicate error, per documentation). So, the question is: what is socket.gethostname() is supposed to return that will be a valid argument for socket.gethostbyname()? PS: I found an MSDN article by Microsoft stating that gethostbyname(gethostname) is guaranteed to always succeed (http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this guarantee also true in linux? From marcusw at cox.net Sat Jul 25 18:42:26 2009 From: marcusw at cox.net (Marcus Wanner) Date: Sat, 25 Jul 2009 18:42:26 -0400 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <027af2b3$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 7/25/2009 10:08 AM, Piet van Oostrum wrote: >>>>>> Steven D'Aprano (SD) wrote: > >> SD> Ambiguity essentially boils down to being unable to reasonably predict >> SD> the expectation of the coder. I say "reasonably", because if you allow >> SD> unreasonable situations, everything is "ambiguous": > > That's for me the reason that len(42) is ambiguous. The OP apparently > had 1 as expectation, whereas my first thought was the minimal number of > bits to represent the number and 7.5 million came later :=). The number > of bits I certainly find reasonable, and I would find the number of > decimal digits equally reasonable. More so than 1, actually. 1 as the > length of an int doesn't give any information. Well, for my two cents, I will say that the number of binary bits or decimal digits is certainly the most sensible return value, and that the former is the most useful, because the latter can be got with len(str(42)). However, the former can also be (/slightly/ less)easily got with len(bin(42))-2... I also think that "Explicit is better than implicit." says that there should be no return value in this case, as any return value would be implicit. Marcus From ben+python at benfinney.id.au Sat Jul 25 19:10:40 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 26 Jul 2009 09:10:40 +1000 Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <877hxw4avj.fsf@benfinney.id.au> Michal Kwiatkowski writes: > I may be missing something obvious here. Is there a better way to tell > if a given generator object is still active or not? foo = the_generator_object try: do_interesting_thing_that_needs(foo.next()) except StopIteration: generator_is_exhausted() In other words, don't LBYL, because it's EAFP. Whatever you need to do that requires the next item from the generator, do that; you'll get a specific exception if the generator is exhausted. -- \ ?Courteous and efficient self-service.? ?caf?, southern France | `\ | _o__) | Ben Finney From max at alcyone.com Sat Jul 25 19:21:39 2009 From: max at alcyone.com (Erik Max Francis) Date: Sat, 25 Jul 2009 16:21:39 -0700 Subject: len() should always return something In-Reply-To: <027a9f1a$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> Message-ID: <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Steven D'Aprano wrote: > But it's not "practically every function". It's hardly any function at > all -- in my code, I don't think I've ever wanted this behavior. I would > consider it an error for function(42) and function([42]) to behave the > same way. One is a scalar, and the other is a vector -- they're different > things, it's poor programming practice to treat them identically. > > (If Matlab does this, so much the worse for Matlab, in my opinion.) There's actually good reason to do this in heavily matrix-oriented specialized languages; there are numerous applications where scalars and 1x1 matrices are mathematically equivalent. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Gods are born and die, but the atom endures. -- Alexander Chase From clp2 at rebertia.com Sat Jul 25 19:30:44 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 25 Jul 2009 16:30:44 -0700 Subject: len() should always return something In-Reply-To: <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: <50697b2c0907251630s6e4f7e0en6876b3609eb4fa5@mail.gmail.com> On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: > Steven D'Aprano wrote: >> >> But it's not "practically every function". It's hardly any function at all >> -- in my code, I don't think I've ever wanted this behavior. I would >> consider it an error for function(42) and function([42]) to behave the same >> way. One is a scalar, and the other is a vector -- they're different things, >> it's poor programming practice to treat them identically. >> >> (If Matlab does this, so much the worse for Matlab, in my opinion.) > > There's actually good reason to do this in heavily matrix-oriented > specialized languages; there are numerous applications where scalars and 1x1 > matrices are mathematically equivalent. The pertinent issue here being that Python, as a language, is neither matrix-oriented nor special-purpose. :) Cheers, Chris -- http://blog.rebertia.com From davea at ieee.org Sat Jul 25 20:35:05 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 25 Jul 2009 20:35:05 -0400 Subject: strange python scripting error In-Reply-To: References: <948a21b4-864a-4c9c-9a60-15afd6e8e5b8@r2g2000yqm.googlegroups.com> <0e4e4eb7-59e3-461b-9d41-9704a88f1d6b@18g2000yqa.googlegroups.com> <4A6AEA22.1030207@ieee.org> Message-ID: <4A6BA4B9.5090800@ieee.org> Dennis Lee Bieber wrote: > On Sat, 25 Jul 2009 07:18:58 -0400, Dave Angel > declaimed the following in gmane.comp.python.general: > > >> Another thing I'd point out is that some ftp programs will do this >> conversion as the file is being sent between a local DOS machine and a >> Unix machine on the internet. >> >> > "some ftp programs"? > > Line end conversion is the whole reason, to my knowledge, for text > mode transfer, vs binary mode transfer. > > > > Perhaps I should have said "most ftp programs," or even "all ftp programs," but I don't feel qualified to make such a generalization. In any case, I don't currently use such automatic conversion when I'm doing transfers because I like to be able to see exactly what's there, and I don't have another way to directly examine the remote end. The FTP I use is an add-on for Firefox (FireFTP), and although it has text and "auto" modes, it's buried in the Tools->options menu, and the main panel just does a transfer using whichever mode was selected. Rather than risking corrupting the (mostly binary) files, I choose to use binary mode all the time. DaveA From scriptlearner at gmail.com Sat Jul 25 21:03:37 2009 From: scriptlearner at gmail.com (scriptlearner at gmail.com) Date: Sat, 25 Jul 2009 18:03:37 -0700 (PDT) Subject: how can a child thread notify a parent thread its status? References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> Message-ID: <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> I decided to go with one big log file, which will be shared by all threads (child and parent). A log message Queue is used to store all log entries, and a customized logger thread will get log entries from the Queue. #from the logger thread# def run(self): while self.flag == 1: #if the flag is set to 0, the logger thread should exit try: entry = self.q.get() except Empty: self.logger.debug('cant find any log entry') continue except: self.logger.error("Unexpected error:", sys.exc_info() [0]) raise #do whatever that should be done self.logger.info("logger thread done") #should see this message in log file as well def off(self): self.logger.info('turning off flag') self.flag = 0 #in parent thread# logItemQ.put('We are done, lets stop the logger now.') time.sleep(1) #it seems that the logger thread cannot exit if I put a sleep here myLog.off() #off is called successfully myLog.join() I put an off method to turn off a flag so the logger thread knows it should exit. However, the last log message (the one 'We are done, lets stop the logger now.') won't be logged if I call myLog.off() and myLog.join() immediately. So, I put a time.sleep(1) to make sure the logger thread has enough time to finish it's job. Unfortunately, now the logger thread simply won't exit, and I don't see the message 'logger thread done'. I can't figure out at which point it hangs, since I don't any new log entry but the thread simply won't exit. Am I taking a right approach by using a flag? Should I lock the flag? From python at mrabarnett.plus.com Sat Jul 25 21:30:00 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 26 Jul 2009 02:30:00 +0100 Subject: how can a child thread notify a parent thread its status? In-Reply-To: <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> Message-ID: <4A6BB198.4040209@mrabarnett.plus.com> scriptlearner at gmail.com wrote: > I decided to go with one big log file, which will be shared by all > threads (child and parent). A log message Queue is used to store all > log entries, and a customized logger thread will get log entries from > the Queue. > > #from the logger thread# > def run(self): > while self.flag == 1: #if the flag is set to 0, the logger > thread should exit > try: > entry = self.q.get() > except Empty: > self.logger.debug('cant find any log entry') > continue > except: > self.logger.error("Unexpected error:", sys.exc_info() > [0]) > raise > #do whatever that should be done > self.logger.info("logger thread done") #should see this > message in log file as well > def off(self): > self.logger.info('turning off flag') > self.flag = 0 > > > #in parent thread# > logItemQ.put('We are done, lets stop the logger now.') > time.sleep(1) #it seems that the logger thread cannot exit if > I put a sleep here > myLog.off() #off is called successfully > myLog.join() > > > I put an off method to turn off a flag so the logger thread knows it > should exit. However, the last log message (the one 'We are done, > lets stop the logger now.') won't be logged if I call myLog.off() and > myLog.join() immediately. So, I put a time.sleep(1) to make sure the > logger thread has enough time to finish it's job. Unfortunately, now > the logger thread simply won't exit, and I don't see the message > 'logger thread done'. I can't figure out at which point it hangs, > since I don't any new log entry but the thread simply won't exit. > Am I taking a right approach by using a flag? Should I lock the flag? self.q.get() will block if the queue is empty, so the Empty exception is never raised. What's happening is that the parent thread puts the final message into the queue, sleeps, and then clears the flag; meanwhile, the logging thread gets the message, writes it out, checks the flag, which is still set, and then tries to get the next message. The queue is empty, so the .get() blocks. The simplest solution is not to use a flag, but the sentinel trick. The parent thread can put, say, None into the queue after the last message; when the logging thread gets None, it knows it should terminate. From max at alcyone.com Sat Jul 25 21:47:07 2009 From: max at alcyone.com (Erik Max Francis) Date: Sat, 25 Jul 2009 18:47:07 -0700 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: Chris Rebert wrote: > On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: >> Steven D'Aprano wrote: >>> But it's not "practically every function". It's hardly any function at all >>> -- in my code, I don't think I've ever wanted this behavior. I would >>> consider it an error for function(42) and function([42]) to behave the same >>> way. One is a scalar, and the other is a vector -- they're different things, >>> it's poor programming practice to treat them identically. >>> >>> (If Matlab does this, so much the worse for Matlab, in my opinion.) >> There's actually good reason to do this in heavily matrix-oriented >> specialized languages; there are numerous applications where scalars and 1x1 >> matrices are mathematically equivalent. > > The pertinent issue here being that Python, as a language, is neither > matrix-oriented nor special-purpose. :) Yes. And I was responding to the comment that such a feature of a language would a priori be poor design. It _isn't_ poor design for special purpose languages. Python isn't one of them, but Matlab _is_. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis More fodder for the new lost generation -- Nik Kershaw From clp2 at rebertia.com Sat Jul 25 22:39:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 25 Jul 2009 19:39:13 -0700 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: <50697b2c0907251939u4c159290qf700640721e88e3a@mail.gmail.com> On Sat, Jul 25, 2009 at 6:47 PM, Erik Max Francis wrote: > Chris Rebert wrote: >> >> On Sat, Jul 25, 2009 at 4:21 PM, Erik Max Francis wrote: >>> >>> Steven D'Aprano wrote: >>>> >>>> But it's not "practically every function". It's hardly any function at >>>> all >>>> -- in my code, I don't think I've ever wanted this behavior. I would >>>> consider it an error for function(42) and function([42]) to behave the >>>> same >>>> way. One is a scalar, and the other is a vector -- they're different >>>> things, >>>> it's poor programming practice to treat them identically. >>>> >>>> (If Matlab does this, so much the worse for Matlab, in my opinion.) >>> >>> There's actually good reason to do this in heavily matrix-oriented >>> specialized languages; there are numerous applications where scalars and >>> 1x1 >>> matrices are mathematically equivalent. >> >> The pertinent issue here being that Python, as a language, is neither >> matrix-oriented nor special-purpose. :) > > Yes. ?And I was responding to the comment that such a feature of a language > would a priori be poor design. ?It _isn't_ poor design for special purpose > languages. ?Python isn't one of them, but Matlab _is_. I was agreeing with your point actually. That was what I was trying to convey in my post. Apparently I wasn't as successful in that regard as I'd hoped. - Chris -- http://blog.rebertia.com From martin at v.loewis.de Sun Jul 26 00:07:21 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 06:07:21 +0200 Subject: uniicode and executing a process with subprocess.call, or os.system In-Reply-To: References: Message-ID: <4a6bd67a$0$7423$9b622d9e@news.freenet.de> > I am very confused about unicode. Can someone point me in the right > direction? Try Python 3.1. This should accept Unicode strings directly for sp.call, so you wouldn't need to encode first. Regards, Martin From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Sun Jul 26 00:56:59 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Sun, 26 Jul 2009 00:56:59 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-25 00:55:26 -0400, Carl Banks said: > But please don't put it on the same level as PHP. Their situations > have almost nothing in common. Their situations have much in common; Python attracted programmers away from (for example) C++, becuse python is easier to master; Then php came along and attracted programmers away from (for example) python, because php is easier to master. This doesn't mean they're on the same level - in fact, if you read carefully you'll see my original post said as much: python attracted average programmers; php attracted mediocre programmers and even some non-programmers, which means that php is clearly a lesser language than python. -- Raffael Cavallaro From nagle at animats.com Sun Jul 26 01:47:37 2009 From: nagle at animats.com (John Nagle) Date: Sat, 25 Jul 2009 22:47:37 -0700 Subject: MySQLdb for Python 3.1 getting close? In-Reply-To: <4a67e912$0$1603$742ec2ed@news.sonic.net> References: <4a67e912$0$1603$742ec2ed@news.sonic.net> Message-ID: <4a6bed48$0$1648$742ec2ed@news.sonic.net> (This is actually a repost; outgoing netnews server was down for a while.) John Nagle wrote: > Is MySQLdb available for Python 3.1 yet? > > http://sourceforge.net/projects/mysql-python/ > > says the last supported Python version is 2.5. Any progress in sight? > > John Nagle From nagle at animats.com Sun Jul 26 01:52:18 2009 From: nagle at animats.com (John Nagle) Date: Sat, 25 Jul 2009 22:52:18 -0700 Subject: non-owning references? In-Reply-To: References: Message-ID: <4a6bee61$0$1648$742ec2ed@news.sonic.net> Peter Otten wrote: > Utpal Sarkar wrote: > >> Is there a way I can tell a variable that the object it is pointing >> too is not owned by it, in the sense that if it is the only reference >> to the object it can be garbage collected? > > http://docs.python.org/library/weakref.html Yes. Weak references can be quite useful. I have a version of BeautifulSoup in which all the "upward" and "backwards" links are weak references, but the "downward" and "forwards" links are strong references. This breaks the loops in the data structure, and thus trees and subtrees will go away when no longer required, without requiring a GC cycle. John Nagle From israel.unterman at gmail.com Sun Jul 26 01:52:36 2009 From: israel.unterman at gmail.com (is un) Date: Sat, 25 Jul 2009 22:52:36 -0700 (PDT) Subject: multiprocessing and __main__ Message-ID: Hi, Trying the multiprocessing module for the first time, I spent quite a bit on making this code run: from multiprocessing import Process import time def my_process(): i = 0 while 1: print i i += 1 time.sleep(0.5) p = Process(target=my_process, args=()) p.start() print 'Process started' The result was not what I expected, it seems like the process restarts all the time, and the message 'Process started' keeps getting printed... Going back to the documentation, I realized that the doc was really serious about the line: if __name__ == '__main__' .. which I always ignore, and by changing the code to if __name__ == '__main__': p = Process(target=my_process, args=()) p.start() print 'Process started' the problem was solved. So my question is what actually happens when I call p.start()? Is the entry file reloaded under a different module name? Thanks iu2 From israel.unterman at gmail.com Sun Jul 26 01:55:24 2009 From: israel.unterman at gmail.com (is un) Date: Sat, 25 Jul 2009 22:55:24 -0700 (PDT) Subject: multiprocessing and __main__ References: Message-ID: <88e64585-e719-4a1a-ade9-7d54b3884503@r2g2000yqm.googlegroups.com> Me again, fix a type in my question: So my question is what actually happens when I call p.start()? Is the *entire* file reloaded under a different module name? > > Thanks > iu2 From bhardwajjayesh7 at gmail.com Sun Jul 26 02:23:11 2009 From: bhardwajjayesh7 at gmail.com (golu) Date: Sat, 25 Jul 2009 23:23:11 -0700 (PDT) Subject: code debugging Message-ID: here is a code which crawls links sent to it. theres some problem with the retrieve_url function ,plz help me out in debugging the fuction retrive_url. This function retrives pages and saves them in file #TODO:Visited dict grows in size it needs to be handled smartly #Moreover server program needs to be in sync with the client eg. Myrobot #Take care of tag - 'if modified since',repeated links,hash links #This is the client side of the distributed crawling framework #It gets the list of urls to be crawled #Then crawls the urls and stores the pages in a temporary archive #which is then transferred to the server or grey_matter import httplib import os import sys import urlparse import urllib2 import urllib import zipfile import threading from socket import * PAGE_DIR="C:/users/jayesh/ pages/" # directory where the web pages are stored temporarily # before transfer to the grey_matter visited= {} # a dict to remember visited urls ROBOT_COUNT=4 def fget(): """ This function retrieves the zipped file containing the list of urls from the grey_matter and saves them in a local file 'list.txt'. """ httplib.HTTPConnection.debuglevel=1 request=urllib2.Request('http://192.168.153.57/list.zip') #Requesting the zipped file request.add_header('Accept-encoding','gzip') #containing the list of urls opener=urllib2.build_opener() flag=1 s='Waiting for server' while flag==1: try: op=opener.open(request) flag=0 except: s=s+'*' print s f=open('list.zip',"wb") f.write(op.read()) f.close() z=zipfile.ZipFile('list.zip') p=z.namelist() g=open('list.txt',"wb") g.write(z.read(p[0])) g.close() print 'got zipped file' def compress(): """ This function compresses the crawled pages and stores them in a single compressed file ready to be sent to the grey_matter.""" zfile=zipfile.ZipFile('C:/xampp/htdocs/pages.zip',mode='w') for fil in os.listdir(PAGE_DIR): full=os.path.join(PAGE_DIR,fil) zfile.write(full,fil) os.remove(full) os.rmdir(PAGE_DIR) #Removing the directory after transfer to grey_matter x=0 class robot(threading.Thread): """ The main robot class which does the crawling of listed urls it recieves from the grey matter. It uses 3 threads which crawl the listed urls synchronously.""" def __init__(self,urllist,urllistlock,dblock): threading.Thread.__init__(self) self.urllist=urllist self.urllistlock=urllistlock self.dblock=dblock def popurl(self): """ This method pops out urls from the urls file one by one and sends them for retrieval.""" self.urllistlock.acquire(1) if(len(self.urllist)<1): Nexturl=None else: Nexturl=self.urllist[0] if Nexturl[-1]=='\n':Nexturl=Nexturl[:-1] del self.urllist[0] self.urllistlock.release() return Nexturl def retrieve_url(self,url): """ The main method of the robot class and is called run method to retrieve the given urls from the web.""" global x if url is not None: try: if visited.has_key(url): return pieces=urlparse.urlparse(url) filepath=pieces[2] if filepath != '': filepath=filepath[1:] filename=filepath.split("/")[-1] else: filename=x+'.htm' x+=1 path=os.path.join(PAGE_DIR,filename) url=urlparse.urlunparse(pieces) p=url.rfind('#') #temporary if p!=-1: url=url[:p] visited[url]=1 m=urllib2.urlopen(url) fopen=open(path,'wb') fopen.seek(0) fopen.write(url+'|') fopen.write(m.read()) fopen.close() print url ,'retrieved' except IOError: print url print "ERROR:OOPS! THE URL CAN'T BE RETRIEVED" return def run(self): while(1): url=self.popurl() if url is None: break try: self.retrieve_url(url) except:sys.exit() if __name__=='__main__': s=socket(AF_INET,SOCK_STREAM) s.bind(('',444)) s.listen(5) q,v=s.accept() count=1 print 'Connecting...' while 1: print 'Phase: %s' %(count) message=q.recv(3) if(message!='yes'):continue print 'Connected' count=count+1 fget() # Calling the fget method to get the url list from # grey_matter(server). try: os.mkdir(PAGE_DIR) except: print 'Cant make dir' try: f=open('list.txt','r') urllist=f.readlines() f.close() except: print 'Error opening urls file' sys.exit() print 'startting threads' urllistlock=threading.Lock() dblock=threading.Lock() botlist=[] for X in range(0,ROBOT_COUNT): newbot=robot(urllist,urllistlock,dblock) newbot.setName('X') botlist.append(newbot) newbot.start() for X in range(0,ROBOT_COUNT): botlist[X].join() compress() try: q.send('yes') except: print 'socket disconnected' print sys.exit() From clp2 at rebertia.com Sun Jul 26 02:28:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 25 Jul 2009 23:28:48 -0700 Subject: code debugging In-Reply-To: References: Message-ID: <50697b2c0907252328t70ecd0ddp20898ddf497b5190@mail.gmail.com> On Sat, Jul 25, 2009 at 11:23 PM, golu wrote: > here is a code which crawls links sent to it. theres some problem with > the retrieve_url function ,plz help me out in debugging the fuction > retrive_url. This function retrives pages and saves them in file Please specify exactly what the problem is that you are experiencing. If you are getting an error, please provide the error message and full traceback. Cheers, Chris -- http://blog.rebertia.com From bhardwajjayesh7 at gmail.com Sun Jul 26 02:33:27 2009 From: bhardwajjayesh7 at gmail.com (golu) Date: Sat, 25 Jul 2009 23:33:27 -0700 (PDT) Subject: code debugging References: Message-ID: On Jul 26, 11:28?am, Chris Rebert wrote: > On Sat, Jul 25, 2009 at 11:23 PM, golu wrote: > > here is a code which crawls links sent to it. theres some problem with > > the retrieve_url function ,plz help me out in debugging the fuction > > retrive_url. This function retrives pages and saves them in file > > Please specify exactly what the problem is that you are experiencing. > If you are getting an error, please provide the error message and full > traceback. > > Cheers, > Chris > --http://blog.rebertia.com i want to save pages in a directory and i m using the urls to get filenames. The program gets stuck in the saving step.can u suggest me a way to save a page e.g google.com as a file google.html From bhardwajjayesh7 at gmail.com Sun Jul 26 02:57:35 2009 From: bhardwajjayesh7 at gmail.com (golu) Date: Sat, 25 Jul 2009 23:57:35 -0700 (PDT) Subject: web page retrieve problems Message-ID: <16343e3d-1be4-422b-8bff-c07fa4aa4c54@j9g2000prh.googlegroups.com> the following function retrieves pages from the web and saves them in a specified dir. i want to extract the respective filenames from the urls e.g the page code.google.com shud be saved as code-google.htm or something similar. can u suggest me a way to do it def retrieve_url(self,url): """ The main method of the robot class and is called run method to retrieve the given urls from the web.""" if url is not None: try: if visited.has_key(url): return pieces=urlparse.urlparse(url) filepath=pieces[2] if filepath != '': filepath=filepath[1:] filename=filepath.split("/")[-1] else: filename='home.htm' path=os.path.join(PAGE_DIR,filename) url=urlparse.urlunparse(pieces) p=url.rfind('#') #temporary if p!=-1: url=url[:p] visited[url]=1 m=urllib2.urlopen(url) fopen=open(path,'wb') fopen.seek(0) fopen.write(url+'|') fopen.write(m.read()) fopen.close() print url ,'retrieved' except IOError: print url print "ERROR:OOPS! THE URL CAN'T BE RETRIEVED" return From emmanuel.surleau at gmail.com Sun Jul 26 03:42:33 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sun, 26 Jul 2009 09:42:33 +0200 Subject: len() should always return something In-Reply-To: References: <24639361.post@talk.nabble.com> Message-ID: <200907260942.33841.emmanuel.surleau@gmail.com> On Sunday 26 July 2009 00:42:26 Marcus Wanner wrote: > On 7/25/2009 10:08 AM, Piet van Oostrum wrote: > >>>>>> Steven D'Aprano (SD) wrote: > >> > >> SD> Ambiguity essentially boils down to being unable to reasonably > >> predict SD> the expectation of the coder. I say "reasonably", because if > >> you allow SD> unreasonable situations, everything is "ambiguous": > > > > That's for me the reason that len(42) is ambiguous. The OP apparently > > had 1 as expectation, whereas my first thought was the minimal number of > > bits to represent the number and 7.5 million came later :=). The number > > of bits I certainly find reasonable, and I would find the number of > > decimal digits equally reasonable. More so than 1, actually. 1 as the > > length of an int doesn't give any information. > > Well, for my two cents, I will say that the number of binary bits or > decimal digits is certainly the most sensible return value, and that the > former is the most useful, because the latter can be got with > len(str(42)). However, the former can also be (/slightly/ less)easily > got with len(bin(42))-2... For my two eurocents, I'd expect such an operator to be called "sizeof" and when applied to a collection, to also return the number of bits it uses. I don't think a 'len' function has meaning when applied to something else than a collection, really... > I also think that "Explicit is better than implicit." says that there > should be no return value in this case, as any return value would be > implicit. Agreed. Cheers, Emm From hendrik at microcorp.co.za Sun Jul 26 03:48:49 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Sun, 26 Jul 2009 09:48:49 +0200 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> Message-ID: <200907260948.49293.hendrik@microcorp.co.za> On Saturday 25 July 2009 20:30:54 Michal Kwiatkowski wrote: > Hi, > > Is there a way to tell if a generator has been exhausted using pure > Python code? I've looked at CPython sources and it seems that > something like "active"/"exhausted" attribute on genobject is missing > from the API. For the time being I am using a simple C extension to > look at f_stacktop pointer of the generator frame, which seems to > differentiate active generators from exhausted ones. See > http://bazaar.launchpad.net/~ruby/pythoscope/support-python2.3/annotate/286 >/pythoscope/_util.c#L16 for complete source code. > > I may be missing something obvious here. Is there a better way to tell > if a given generator object is still active or not? Is there a reason why you cannot just call the next method and handle the StopIteration when it happens? - Hendrik From emmanuel.surleau at gmail.com Sun Jul 26 03:50:42 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Sun, 26 Jul 2009 09:50:42 +0200 Subject: non-owning references? In-Reply-To: <8763di5d1d.fsf@benfinney.id.au> References: <87fxcm5gny.fsf@busola.homelinux.net> <8763di5d1d.fsf@benfinney.id.au> Message-ID: <200907260950.42765.emmanuel.surleau@gmail.com> On Friday 24 July 2009 17:14:06 you wrote: > Hrvoje Niksic writes: > > The term "variable" is used in the Python language reference and > > elsewhere > > Yes. It should also be abundantly clear from the constant stream of > confused newbies on this point that its usage of that term is different > to what many expect from usage elsewhere. > > > and is quite compatible with how other popular languages (Java, PHP, > > Lisp, ...) use it. Please stop complaining about valid terminology; it > > is not helpful. > > I disagree with your assertions. > > Rather than yet another round of this tedious debate, I merely point > interested readers to > and ask them to draw their own conclusion on how compatible their > pre-existing ?variable? concept is with Python's object reference > model. Then I must be one of these "confused newbies". After a cursory look at your article, I don't see a difference with Java variables (as long as we speak about non-primitive types). Cheers, Emm From martin at v.loewis.de Sun Jul 26 04:04:12 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 10:04:12 +0200 Subject: C-API, tp_dictoffset vs tp_members In-Reply-To: <7cl194F28ojoqU1@mid.uni-berlin.de> References: <7cl194F28ojoqU1@mid.uni-berlin.de> Message-ID: <4A6C0DFC.4030702@v.loewis.de> > When would I use PyObject_SetAttrString/tp_dictoffset instead of tp_members? When I have a variable list of attributes, and cannot statically know what those attributes might be. > I have a predefined set of members, some of which are optional. Having optional fields is also a good reason. > The problem > I had with an embedded dictionary was that I can't see its elements using > "dir()". How so? That should work fine. > Now I just converted to using tp_members, and it still seems to > work correctly for the cases I tested. So how do you do optional fields now? In particular, how would you do optional integers? > Even better, I can declare the fields > as read-only then and add doc-strings. So, I wonder, what made the original > author[2] use tp_dictoffset instead? Most likely, it was the simplest approach. This code only wants to set the attributes, and never read them. It's easier to maintain: if you want to add a field with tp_members, you have to change multiple places, and you have to consider garbage collection (assuming you have embedded objects). With tp_dictoffset, adding another attribute is easy. Regards, Martin From jessica.1980.smith at gmail.com Sun Jul 26 04:12:47 2009 From: jessica.1980.smith at gmail.com (Jessica R Smith) Date: Sun, 26 Jul 2009 01:12:47 -0700 (PDT) Subject: I am trying to compile python 2.6.2 on my Mac Message-ID: Hello, I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 I downloaded python 2.6.2 from here: - http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 I unpacked it. I ran these shell commands: - ./configure --prefix=/pt/p - make Near the end of the make output I see this message: . Failed to find the necessary bits to build these modules: . _bsddb gdbm linuxaudiodev . ossaudiodev readline spwd . sunaudiodev . To find the necessary bits, look in setup.py in detect_modules() for the module's name. I looked in setup.py in detect_modules() It is not clear to me how to proceed. I think that I want the module: "readline" I doubt I need the other modules like linuxaudiodev, etc. If you have any clues or opinions on how I can build the module "readline", please feel free to share. Thanks, JRS From nobody at nowhere.com Sun Jul 26 04:13:30 2009 From: nobody at nowhere.com (Nobody) Date: Sun, 26 Jul 2009 09:13:30 +0100 Subject: Failed Regression Test: What socket.gethostname() is supposed to return? References: Message-ID: On Sun, 26 Jul 2009 08:30:30 +1000, Lie Ryan wrote: > since on my system socket.gethostname() returns 'lieryan', and since > socket.gethostbyname('lieryan') does not resolve to anything; the test > becomes an error. > > My system is Gentoo, but I think this also happened on Ubuntu (still on > this laptop). The trunk failed in similar manner. > > Do I have a misconfigured system or is the test faulty? Misconfigured system. > I tracked the code for socket.gethostname() and socket.gethostbyname() > and found that they are simply a wrapper for gethostname() from #import > (http://linux.die.net/man/2/gethostname) and gethostbyname() > from #import (http://linux.die.net/man/3/gethostbyname). A > simple test in C found that the C's equivalent to > gethostbyname(gethostname()) returns a null pointer (used to indicate > error, per documentation). > > So, the question is: what is socket.gethostname() is supposed to return > that will be a valid argument for socket.gethostbyname()? gethostname() returns the hostname. gethostbyname() returns some information about the host (specifically, aliases and IP addresses) obtained by platform-specific means (which normally includes /etc/hosts and DNS, and may also include NIS, LDAP, and others; see the nsswitch.conf manpage for more details). Python's socket.gethostname() simply returns the first IP address; socket.gethostbyname_ex() returns all of the information which libc's gethostbyname() provides. In order for gethostbyname() to work, the host must be listed in one of the databases which it uses. The simplest way to achieve this is to add an entry to /etc/hosts, e.g.: 192.168.0.2 lieryan.yourdomain.com lieryan or just: 192.168.0.2 lieryan > PS: I found an MSDN article by Microsoft stating that > gethostbyname(gethostname) is guaranteed to always succeed > (http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this > guarantee also true in linux? No. From gagsl-py2 at yahoo.com.ar Sun Jul 26 04:14:34 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 26 Jul 2009 05:14:34 -0300 Subject: multiprocessing and __main__ References: Message-ID: En Sun, 26 Jul 2009 02:52:36 -0300, is un escribi?: > Trying the multiprocessing module for the first time, I spent quite a > bit on making this code run: > [...] > The result was not what I expected, it seems like the process restarts > all the time, and the message 'Process started' keeps getting > printed... > > Going back to the documentation, I realized that the doc was really > serious about the line: > if __name__ == '__main__' .. which I always ignore, and by changing > the code to > > if __name__ == '__main__': > p = Process(target=my_process, args=()) > p.start() > > print 'Process started' > > the problem was solved. > So my question is what actually happens when I call p.start()? Is the > entry file reloaded under a different module name? The multiprocessing module handles multiple (separate) processes. On Windows, a new Python interpreter is launched; on other OSs a different approach is used. In any case, you get distinct and separate processes, each one with its own set of loaded modules, etc. -- Gabriel Genellina From clp2 at rebertia.com Sun Jul 26 04:37:43 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 26 Jul 2009 01:37:43 -0700 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: References: Message-ID: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> On Sun, Jul 26, 2009 at 1:12 AM, Jessica R Smith wrote: > Hello, > I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 > > I downloaded python 2.6.2 from here: > ?- http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 > > I unpacked it. > > I ran these shell commands: > ?- ./configure --prefix=/pt/p > ?- make > > Near the end of the make output I see this message: > > . Failed to find the necessary bits to build these modules: > . _bsddb ? ? ? ? ? ? gdbm ? ? ? ? ? ? ? linuxaudiodev > . ossaudiodev ? ? ? ?readline ? ? ? ? ? spwd > . sunaudiodev > . To find the necessary bits, look in setup.py in detect_modules() for > the module's name. > > I looked in setup.py in detect_modules() > > It is not clear to me how to proceed. > > I think that I want the module: "readline" > > I doubt I need the other modules like linuxaudiodev, etc. > > If you have any clues or opinions on how I can build the module > "readline", > please feel free to share. You need to install the GNU Readline library (http://tiswww.case.edu/php/chet/readline/rltop.html), which the module depends on. You might consider installing Python through MacPorts or Fink instead, as they automate the compilation and installation process and take care of dependencies (such as GNU Readline) for you: http://www.macports.org/ http://www.finkproject.org/ Cheers, Chris -- http://blog.rebertia.com From constant.beta at gmail.com Sun Jul 26 04:45:21 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Sun, 26 Jul 2009 01:45:21 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> Message-ID: <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> On Jul 26, 1:10?am, Ben Finney wrote: > Michal Kwiatkowski writes: > > I may be missing something obvious here. Is there a better way to tell > > if a given generator object is still active or not? > > ? ? foo = the_generator_object > ? ? try: > ? ? ? ? do_interesting_thing_that_needs(foo.next()) > ? ? except StopIteration: > ? ? ? ? generator_is_exhausted() > > In other words, don't LBYL, because it's EAFP. Whatever you need to do > that requires the next item from the generator, do that; you'll get a > specific exception if the generator is exhausted. The thing is I don't need the next item. I need to know if the generator has stopped without invoking it. Why - you may ask. Well, the answer needs some explaining. I'm working on the Pythoscope project (http://pythoscope.org) and I use tracing mechanisms of CPython (sys.settrace) to capture function calls and values passed to and from them. Now, the problem with generators is that when they are ending (i.e. returning instead of yielding) they return a None, which is in fact indistinguishable from "yield None". That means I can't tell if the last captured None was in fact yielded or is a bogus value which should be rejected. Let me show you on an example. import sys def trace(frame, event, arg): if event != 'line': print frame, event, arg return trace def gen1(): yield 1 yield None def gen2(): yield 1 sys.settrace(trace) print "gen1" g1 = gen1() g1.next() g1.next() print "gen2" g2 = gen2() [x for x in g2] sys.settrace(None) The first generator isn't finished, it yielded 1 and None. Second one is exhausted after yielding a single value (1). The problem is that, under Python 2.4 or 2.3 both invocations will generate the same trace output. So, to know whether the last None was actually a yielded value I need to know if a generator is active or not. Your solution, while gives me an answer, is not acceptable because generators can cause side effects (imagine call to launch_rockets() before the next yield statement ;). Cheers, mk From jessica.1980.smith at gmail.com Sun Jul 26 05:01:31 2009 From: jessica.1980.smith at gmail.com (Jessica Smith) Date: Sun, 26 Jul 2009 02:01:31 -0700 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> References: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> Message-ID: Chris thanks!! other members: I read this: - http://chrismiles.livejournal.com/25648.html I did this: - install mac ports - port install readline - vi setup.py def detect_modules(self): # Ensure that /usr/local is always used add_dir_to_list(self.compiler.library_dirs, '/opt/local/lib') add_dir_to_list(self.compiler.include_dirs, '/opt/local/include') - configure --prefix=/pt/p - make - make install I'm happy now! JRS On 7/26/09, Chris Rebert wrote: > On Sun, Jul 26, 2009 at 1:12 AM, Jessica R > Smith wrote: >> Hello, >> I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 >> >> I downloaded python 2.6.2 from here: >> ?- http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 >> >> I unpacked it. >> >> I ran these shell commands: >> ?- ./configure --prefix=/pt/p >> ?- make >> >> Near the end of the make output I see this message: >> >> . Failed to find the necessary bits to build these modules: >> . _bsddb ? ? ? ? ? ? gdbm ? ? ? ? ? ? ? linuxaudiodev >> . ossaudiodev ? ? ? ?readline ? ? ? ? ? spwd >> . sunaudiodev >> . To find the necessary bits, look in setup.py in detect_modules() for >> the module's name. >> >> I looked in setup.py in detect_modules() >> >> It is not clear to me how to proceed. >> >> I think that I want the module: "readline" >> >> I doubt I need the other modules like linuxaudiodev, etc. >> >> If you have any clues or opinions on how I can build the module >> "readline", >> please feel free to share. > > You need to install the GNU Readline library > (http://tiswww.case.edu/php/chet/readline/rltop.html), which the > module depends on. > > You might consider installing Python through MacPorts or Fink instead, > as they automate the compilation and installation process and take > care of dependencies (such as GNU Readline) for you: > http://www.macports.org/ > http://www.finkproject.org/ > > Cheers, > Chris > -- > http://blog.rebertia.com > From __peter__ at web.de Sun Jul 26 05:08:10 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 26 Jul 2009 11:08:10 +0200 Subject: strange error when trying to log something References: <0aef181f-b002-432d-b046-dbbbd713d1dd@d32g2000yqh.googlegroups.com> Message-ID: Aahz wrote: > In article , > Peter Otten <__peter__ at web.de> wrote: >> >>I have a hunch that you are triggering a reload() somewhere. Example: >> >>Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) >>[GCC 4.3.3] on linux2 >>Type "help", "copyright", "credits" or "license" for more information. >>>>> import weakref >>>>> weakref.WeakValueDictionary() >> >>>>> import UserDict >>>>> reload(UserDict) >> >>>>> weakref.WeakValueDictionary() >>Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib/python2.6/weakref.py", line 51, in __init__ >> UserDict.UserDict.__init__(self, *args, **kw) >>TypeError: unbound method __init__() must be called with UserDict instance >>as first argument (got WeakValueDictionary instance instead) > > Nice sleuthing! How did you figure that out? Roughly: - The poster's description/remedy makes no sense (to me); have a look at the traceback. - Nothing suspicious except the actual error message. Isn't WeakValueDictionary a UserDict? Verify. - There must be two different UserDict classes. This can happen to an unsuspecting user by importing the main script or by doing a reload(). UserDict is unlikely to be the main script, and web frameworks (django was mentioned) often use a reload mechanism to avoid frequent server restarts. - Reproduce the error. Peter From gagsl-py2 at yahoo.com.ar Sun Jul 26 05:19:52 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 26 Jul 2009 06:19:52 -0300 Subject: code debugging References: Message-ID: En Sun, 26 Jul 2009 03:33:27 -0300, golu escribi?: > i want to save pages in a directory and i m using the urls to get > filenames. The program gets stuck in the saving step.can u suggest me > a way to save a page e.g google.com as a file google.html You may use str.translate to replace/remove all undesired characters: py> import string py> valid = string.ascii_letters+string.digits+'.' py> invalid = ''.join(chr(x) for x in range(256) if chr(x) not in valid) py> table = string.maketrans(invalid, '_'*len(invalid)) py> x = 'http://docs.python.org/library/string.html' py> x.translate(table) 'http___docs.python.org_library_string.html' See http://docs.python.org/library/stdtypes.html#str.translate -- Gabriel Genellina From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 05:45:03 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 09:45:03 GMT Subject: len() should always return something References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> Message-ID: <027c168c$0$5185$c3e8da3@news.astraweb.com> On Sat, 25 Jul 2009 16:21:39 -0700, Erik Max Francis wrote: > Steven D'Aprano wrote: >> But it's not "practically every function". It's hardly any function at >> all -- in my code, I don't think I've ever wanted this behavior. I >> would consider it an error for function(42) and function([42]) to >> behave the same way. One is a scalar, and the other is a vector -- >> they're different things, it's poor programming practice to treat them >> identically. >> >> (If Matlab does this, so much the worse for Matlab, in my opinion.) > > There's actually good reason to do this in heavily matrix-oriented > specialized languages; there are numerous applications where scalars and > 1x1 matrices are mathematically equivalent. I'm curious what those applications are, because regular multiplication behaves differently depending on whether you have a 1x1 matrix or a scalar: [[2]]*[[1, 2, 3], [2, 3, 4]] is not defined 2*[[1, 2, 3], [2, 3, 4]] = [[2, 4, 6], [2, 6, 8]] I'm curious as to what these applications are, and what they're actually doing. Kronecker multiplication perhaps? Do you have some examples of those applications? -- Steven From doomster at knuut.de Sun Jul 26 05:45:37 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sun, 26 Jul 2009 11:45:37 +0200 Subject: C-API, tp_dictoffset vs tp_members References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> Message-ID: <7d2mu7F27a9tbU1@mid.uni-berlin.de> "Martin v. L?wis" wrote: >> I have a predefined set of members, some of which are optional. > > Having optional fields is also a good reason. What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? I would have though that the former describes an optional field, because the behaviour of accessing it when it is NULL is the same as accessing a nonexistent field. However, I see that it still appears in the dir() output even if it is NULL, so it seems I'm misunderstanding this. >> The problem >> I had with an embedded dictionary was that I can't see its elements using >> "dir()". > > How so? That should work fine. >>> import example >>> x = example.Example() >>> dir(x) ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__'] >>> x.example_attribute 'example value' The 'example_attribute' can be accessed, but it is not visible in the dir() output. The code for this is below. >> Now I just converted to using tp_members, and it still seems to >> work correctly for the cases I tested. > > So how do you do optional fields now? In particular, how would you do > optional integers? See above, I would expect T_OBJECT_EX to do that. Thanks! Uli /* example.c gcc -Wall example.c -shared -I /usr/include/python2.5 -o example.so */ #include #include typedef struct { PyObject_HEAD PyObject* dict; } Example; static void Example_dealloc(PyObject* self) { Example* ex = (Example*)self; Py_XDECREF(ex->dict); self->ob_type->tp_free(self); } static PyObject * Example_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { Example* self; PyObject* attr; self = (Example*)type->tp_alloc(type, 0); if(self==NULL) return NULL; self->dict = PyDict_New(); if(self->dict==NULL) { Py_DECREF(self); return NULL; } attr = PyString_FromString("example value"); if(PyObject_SetAttrString((PyObject*)self, "example_attribute", attr)<0) { Py_DECREF(self); return NULL; } return (PyObject*)self; } static PyTypeObject ExampleType = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ "example.Example", /*tp_name*/ sizeof(Example), /*tp_basicsize*/ 0, /*tp_itemsize*/ Example_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT, /*tp_flags*/ 0, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ offsetof(Example, dict), /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ Example_new, /* tp_new */ }; void initexample(void) { PyObject* m; if (PyType_Ready(&ExampleType) < 0) return; m = Py_InitModule3("example", 0, "Example module that creates an extension type."); if (m == NULL) return; Py_INCREF(&ExampleType); PyModule_AddObject(m, "Example", (PyObject*)&ExampleType); } From israelu at elbit.co.il Sun Jul 26 05:50:15 2009 From: israelu at elbit.co.il (iu2) Date: Sun, 26 Jul 2009 02:50:15 -0700 (PDT) Subject: multiprocessing and __main__ References: Message-ID: On Jul 26, 8:52?am, is un wrote: > Hi, > > Trying the multiprocessing module for the first time, I spent quite a > bit on making this code run: > > from multiprocessing import Process > import time > > def my_process(): > ? ? i = 0 > ? ? while 1: > ? ? ? ? print i > ? ? ? ? i += 1 > ? ? ? ? time.sleep(0.5) > > p = Process(target=my_process, args=()) > p.start() > > print 'Process started' > > The result was not what I expected, it seems like the process restarts > all the time, and the message 'Process started' keeps getting > printed... > > Going back to the documentation, I realized that the doc was really > serious about the line: > if __name__ == '__main__' .. which I always ignore, and by changing > the code to > > if __name__ == '__main__': > ? ? p = Process(target=my_process, args=()) > ? ? p.start() > > ? ? print 'Process started' > > the problem was solved. > So my question is what actually happens when I call p.start()? Is the > entry file reloaded under a different module name? > > Thanks > iu2 Ok, I found it, it is nicely documented, I missed it before. Case closed. From doomster at knuut.de Sun Jul 26 05:54:55 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sun, 26 Jul 2009 11:54:55 +0200 Subject: Removing newlines from string on windows (without replacing) References: Message-ID: <7d2nfnF29tk6fU1@mid.uni-berlin.de> Tom wrote: > s = sauce.replace("\n", "") > > Sauce is a string, read from a file, that I need to remove newlines > from. This code works fine in Linux, but not in Windows. rstrip("\n") > won't work for me, so anybody know how to get this working on Windows? I'm pretty sure this works regardless of the system. What might change is the way that a line is terminated: MS Windows by default uses a CR/LF ('\r\n') pair, while most other systems just an LF ('\n'). I'd adapt the code to accept either line ending on either system. If that's not what you want, you should improve your "doesn't work" description. ;) Uli From max at alcyone.com Sun Jul 26 06:11:02 2009 From: max at alcyone.com (Erik Max Francis) Date: Sun, 26 Jul 2009 03:11:02 -0700 Subject: len() should always return something In-Reply-To: <027c168c$0$5185$c3e8da3@news.astraweb.com> References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> <027a9f1a$0$5185$c3e8da3@news.astraweb.com> <04-dnQ5L6JkeDvbXnZ2dnUVZ_sOdnZ2d@giganews.com> <027c168c$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I'm curious what those applications are, because regular multiplication > behaves differently depending on whether you have a 1x1 matrix or a > scalar: > > [[2]]*[[1, 2, 3], [2, 3, 4]] is not defined > > 2*[[1, 2, 3], [2, 3, 4]] = [[2, 4, 6], [2, 6, 8]] > > I'm curious as to what these applications are, and what they're actually > doing. Kronecker multiplication perhaps? Do you have some examples of > those applications? The most obvious example would be the matrix algebra equivalent of the calculation of the dot product, the equivalent of a dot product in normal vector algebra. If you have two 3-vectors (say), u = (u_1, u_2, u_3) and v = (v_1, v_2, v_3), then the dot product is the sum of the pairwise products of these terms, u dot v = sum_i u_i v_i = u_1 v_1 + u_2 v_2 + u_3 v_3. Nothing revolutionary there. The matrix way of writing this wouldn't obviously work, since multiplying two nx1 or (1xn) matrixes by each other is invalid. But you _can_ multiply a nx1 matrix by an 1xn matrix, and you get the equivalent of the dot product: u v^T = ( u dot v ), where the right hand side of the equation is formally a 1x1 matrix (intended to be indicated by the parentheses), but you can see how it is useful to think of it as just a scalar, because it's really just a matrix version of the same thing as a dot product. This stretches out to other kinds of applications, where, say, in tensor calculus, you can think of a contravariant vector as being transformed into a covariant vector by the application of the matrix tensor, which is written as lowering an index. The components of the contravariant vector can be thought of as a column vector, while the components of a covariant vector can be represented with a row vector. The application of the metric tensor to a contravariant vector turns it into a row vector, and then contracting over the two vectors gives you the inner product as above. The applications and contractions can all be visualized as matrix multiplications. (Contrary to a popularization, tensors _aren't_ generalizations of matrices, but their components can be.) It's a bunch of other examples like that where you end up with a 1x1 matrix that really represents a scalar, and since that was intended there really isn't a lot of efficacy to treat it as a separate entity. Especially if you're dealing with a special-purpose language where everything is really a form of an generalized array representation of something _anyway_. -- Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Scars are like memories. We do not have them removed. -- Chmeee From wilk at flibuste.net Sun Jul 26 06:22:34 2009 From: wilk at flibuste.net (William Dode) Date: 26 Jul 2009 10:22:34 GMT Subject: ANN: psyco V2 References: <639d8155-ab27-462b-9401-73448a3c9575@b15g2000yqd.googlegroups.com> <4a696af3$0$442$426a74cc@news.free.fr> Message-ID: <4a6c2e6a$0$424$426a34cc@news.free.fr> On 24-07-2009, Christian Tismer wrote: > On 7/24/09 1:04 AM, William Dode wrote: >> On 23-07-2009, Christian Tismer wrote: > ... > >>> Wasn't the project plan saying the opposite, borrowing >>> some ideas from psyco? :-) >>> http://code.google.com/p/unladen-swallow/wiki/ProjectPlan >> >> How do you see the future of psyco when unladen-swallow will grab the >> best of psyco (if they can !) ? > > I have no objections, but also no idea so far how that could work. > Sounded like an unreflected idea to me, without seriously > checking the possibilities and/or implications. The same > kind of research apparently did not happen concerning PyPy, > which IMHO is much more suitable to take advantage from. > > This is for the current status of psyco, of course. > It will change dramatically in the next months, slowly > adopting some of PyPy's ideas. Then it might start to > make more sense. I didn't understand how psyco could use somes PyPy's ideas, i thought it's only the opposite... So i was going to read the PyPy's documentation : it's fascinating !!! But how to test it ? The doc says : "When PyPy is translated into an executable like pypy-c, the executable contains a full virtual machine that can optionally include a Just-In-Time compiler." Where is this 'option', is there somethings to activate it ? thx -- William Dod? - http://flibuste.net Informaticien Ind?pendant From deets at nospam.web.de Sun Jul 26 06:51:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 26 Jul 2009 12:51:31 +0200 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: References: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> Message-ID: <7d2qpkF29u2uiU1@mid.uni-berlin.de> Jessica Smith schrieb: > Chris thanks!! > > other members: > > I read this: > - http://chrismiles.livejournal.com/25648.html > > I did this: > - install mac ports > - port install readline > - vi setup.py > > def detect_modules(self): > # Ensure that /usr/local is always used > add_dir_to_list(self.compiler.library_dirs, '/opt/local/lib') > add_dir_to_list(self.compiler.include_dirs, '/opt/local/include') > > - configure --prefix=/pt/p > - make > - make install > > I'm happy now! You are aware that under OSX one usually wants a so-called framework-build? It's an configure-option. You don't need the --prefix then (unless you have another python2.6 installed), and without the FW-build no UI-packages are useable. Diez From piet at cs.uu.nl Sun Jul 26 07:50:57 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 26 Jul 2009 13:50:57 +0200 Subject: how can a child thread notify a parent thread its status? References: <923a982d-5cd6-41ba-b4f9-80b4e81b601f@y10g2000prf.googlegroups.com> <96ec583d-afc6-4c8e-b53f-d875611a9c55@t11g2000prh.googlegroups.com> Message-ID: >>>>> MRAB (M) wrote: >M> scriptlearner at gmail.com wrote: >>> I decided to go with one big log file, which will be shared by all >>> threads (child and parent). A log message Queue is used to store all >>> log entries, and a customized logger thread will get log entries from >>> the Queue. >>> >>> #from the logger thread# >>> def run(self): >>> while self.flag == 1: #if the flag is set to 0, the logger >>> thread should exit >>> try: >>> entry = self.q.get() >>> except Empty: >>> self.logger.debug('cant find any log entry') >>> continue >>> except: >>> self.logger.error("Unexpected error:", sys.exc_info() >>> [0]) >>> raise >>> #do whatever that should be done >>> self.logger.info("logger thread done") #should see this >>> message in log file as well >>> def off(self): >>> self.logger.info('turning off flag') >>> self.flag = 0 >>> >>> >>> #in parent thread# >>> logItemQ.put('We are done, lets stop the logger now.') >>> time.sleep(1) #it seems that the logger thread cannot exit if >>> I put a sleep here >>> myLog.off() #off is called successfully >>> myLog.join() >>> >>> >>> I put an off method to turn off a flag so the logger thread knows it >>> should exit. However, the last log message (the one 'We are done, >>> lets stop the logger now.') won't be logged if I call myLog.off() and >>> myLog.join() immediately. So, I put a time.sleep(1) to make sure the >>> logger thread has enough time to finish it's job. Unfortunately, now >>> the logger thread simply won't exit, and I don't see the message >>> 'logger thread done'. I can't figure out at which point it hangs, >>> since I don't any new log entry but the thread simply won't exit. >>> Am I taking a right approach by using a flag? Should I lock the flag? >M> self.q.get() will block if the queue is empty, so the Empty exception is >M> never raised. >M> What's happening is that the parent thread puts the final message into >M> the queue, sleeps, and then clears the flag; meanwhile, the logging >M> thread gets the message, writes it out, checks the flag, which is still >M> set, and then tries to get the next message. The queue is empty, so the >M> .get() blocks. >M> The simplest solution is not to use a flag, but the sentinel trick. The >M> parent thread can put, say, None into the queue after the last message; >M> when the logging thread gets None, it knows it should terminate. To the OP: a sleep will never do it because you can't be sure how long the logging thread will lag behind. Most multithreaded `solutions' which depend on timings are buggy. If you have more than one thread writing to the log queue one sentinel won't solve the problem. You would need as many sentinels as there are threads, and the logger should count the sentinels which can have normal messages between them. It may be a bit fragile to depend on the number of threads especially when this number is dynamic. One solution would be to have special StartLogging and StopLogging objects that are put in the queue when a thread starts and finishes respectively. The logger can stop then when the number of StopLogging objects equals the number of StartLogging objects received and that number is greater than 0. This presupposes that no new thread will be started after all the others have finished. The StartLogging objects can also be put in the queue by the parent when it starts up the threads, which may be more robust because it has fewer timing dependencies. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From breamoreboy at yahoo.co.uk Sun Jul 26 07:53:20 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 Jul 2009 12:53:20 +0100 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: Neil Hodgson wrote: > casebash: > >> I have searched this list and found out that Python doesn't have a >> mutable string class (it had an inefficient one, but this was removed >> in 3.0). Are there any libraries outside the core that offer this? > > I wrote a gap buffer implementation for Python 2.5 allowing > character, unicode character and integer elements. > > http://code.google.com/p/gapbuffer/ > > Its not seen much use or any maintenance so is unlikely to work with > Python 3.x. > > Neil I tried this as a learning exercise and found slicing doesn't work correctly. import gapbuffer print gapbuffer.GapBuffer(range(10))[:] GapBuffer('i')] If my sleuthing is correct the problem is with these lines ilow *= self->itemSize; ihigh *= self->itemSize; in GapBuffer_slice being computed before ilow and ihigh are compared to anything. Python 2.6.2 32 bit Windows. -- Kindest regards. Mark Lawrence. From helvinlui at gmail.com Sun Jul 26 08:04:11 2009 From: helvinlui at gmail.com (Helvin) Date: Sun, 26 Jul 2009 05:04:11 -0700 (PDT) Subject: PyQt GUI References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> Message-ID: <4ee78bf1-476d-4e41-b9da-c20297aad861@13g2000prl.googlegroups.com> On Jul 24, 5:03?am, Robert Kern wrote: > On 2009-07-23 03:55, Helvin wrote: > > > I believe I now have vtkpython.exe. However, my 'import vtk' statement > > in my python code is not working. The error says something like "no > > module named vtk". > > Where do I find modules for vtk inpyqt? Do they exist? > > There are no VTK modules inPyQtitself. ThePyQtsupport is in the vtk package > which can be built with VTK itself. You will need to install VTK and the vtk > package correctly in order to achieve this. vtkpython.exe is not going to help > you. Ignore it. After you have built VTK, you need to do an extra step to > install the vtk package to where your Python interpreter will be able to find it. > > Let's say that your build directory is c:\vtkbuild. > > ? ?cd \vtkbuild\Wrapping\Python > ? ?python setup.py install > ? ?cd \ > > Now you should be able to import vtk from your normal Python interpreter. If you > are still having problems, you will need to copy-and-paste exactly what you did > and what error messages you got. Do not paraphrase error messages. > > -- > 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 Hi Robert, I realised that vtkpython.exe works, if I drag and drop my .py file on it. Ie, running my .py file using vtkpython.exe But my python interpreter does not work with the python file. I have tried what you suggested. Below is what I typed in command line, as well as the error that it produces. C:\Qt\VTKbin7\Wrapping\Python>python setup.py install Traceback (most recent call last): File "setup.py", line 138, in raise "ERROR: Must specify BUILD_TYPE= on command line." TypeError: exceptions must be classes or instances, not str Is it just a small error with input format or something? Thanks so much! Helvin From aahz at pythoncraft.com Sun Jul 26 09:16:39 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 06:16:39 -0700 Subject: If Scheme is so good why MIT drops it? References: Message-ID: In article , Raffael Cavallaro wrote: >On 2009-07-25 00:55:26 -0400, Carl Banks said: >> >> But please don't put it on the same level as PHP. Their situations >> have almost nothing in common. > >Their situations have much in common; Python attracted programmers away >from (for example) C++, becuse python is easier to master; Then php >came along and attracted programmers away from (for example) python, >because php is easier to master. No, Python attracted programmers away from C++ because it's easier to write good programs in it, in less time. There are plenty of expert C++ programmers who switched to Python; your thesis only applies to the legions of people who found it difficult to learn C++ in the first place. Moreover, AFAIK PHP never attracted people away from Python; Python was not particularly popular when PHP was in its heyday. PHP attracted people away from Perl and Java and C++. I'm curious, do you actually know Python at all? Have you used it for a serious project? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Sun Jul 26 09:31:06 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Sun, 26 Jul 2009 09:31:06 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-26 09:16:39 -0400, aahz at pythoncraft.com (Aahz) said: > There are plenty of expert C++ > programmers who switched to Python; "plenty" is an absolute term, not a relative term. I sincerely doubt that the majority of python users were formerly *expert* C++ programmers. > your thesis only applies to the > legions of people who found it difficult to learn C++ in the first place. No, my thesis applies to the overwhelming majority of programmers who found it more difficult to *master* (i.e., not merely use) C++ as opposed to mastering python. BTW, this is a *complement* not a dis; python is a better language than C++ precisely because it is more sensibly and elegantly designed than C++ and therefore easier to master. php represents the same process but farther down the ladder, as it were. There's often a tradeoff between ease of mastery and power. python hits a sweet spot for many tasks and many programmers, especially as compared to C++ (or even lisp, which though more powerful than python is more difficult to master. lisp beats C++ on both counts imho - more powerful *and* easier to master). php hits a sweet spot only in a very restricted domain. Beyond that, it is clearly inferior to python which has greater power, but is more difficult to master. -- Raffael Cavallaro From pauljbarry3 at gmail.com Sun Jul 26 10:29:44 2009 From: pauljbarry3 at gmail.com (Paul Barry) Date: Sun, 26 Jul 2009 07:29:44 -0700 (PDT) Subject: Can't get UDP example to work Message-ID: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> I'm trying to get one of the examples from Foundation of Python Network Programming to work. Specifically this is the UDP example from Ch 3. First there is the server: #!/usr/bin/env python # UDP Echo Server - Chapter 3 - udpechoserver.py import socket, traceback, time host = '127.0.0.1' # Bind to all interfaces port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) while 1: try: message, address = s.recvfrom(8192) print "Got message '%s' from %s" % (message, address) # Echo it back s.sendto(message, address) print "Sent response to '%s'" % (address,) except (KeyboardInterrupt, SystemExit): raise except: traceback.print_exc() Next I have a client written in Ruby, which works. I am posting thing not to start a Ruby/Python flame war, but to simply prove that the server works and there are no weird networking issues that would prevent the Python client from working. The Ruby code is: #!/usr/bin/env ruby require 'socket' socket = UDPSocket.new socket.connect ARGV[0], ARGV[1] puts "Enter data to transmit: " data = STDIN.gets.chomp socket.send data, 0 puts "Looking for replies; press Ctrl-C or Ctrl-Break to stop." loop do buf = socket.recvfrom(2048) puts buf.first end When I start the server and run that, the output looks like this: $ ch02/udp.rb 127.0.0.1 51423 Enter data to transmit: foobar Looking for replies; press Ctrl-C or Ctrl-Break to stop. foobar Now, when I try the python example: #!/usr/bin/env python # UDP Example - Chapter 2 - udp.py import socket, sys host = sys.argv[1] textport = sys.argv[2] s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: port = int(textport) except ValueError: # That didn't work. Look it up instead. port = socket.getservbyname(textport, 'udp') s.connect((host, port)) print "Enter data to transmit: " data = sys.stdin.readline().strip() s.sendall(data) print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." while 1: buf = s.recvfrom(2048) sys.stdout.write(buf[0]) I don't ever get a response: $ ch02/udp.py 127.0.0.1 51423 Enter data to transmit: foobar Looking for replies; press Ctrl-C or Ctrl-Break to stop. The server sees the message and says it has sent a reply: Got message 'foobar' from ('127.0.0.1', 49623) Sent response to '('127.0.0.1', 49623)' Any ideas as to why this doesn't work? From michael at stroeder.com Sun Jul 26 10:59:42 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Sun, 26 Jul 2009 16:59:42 +0200 Subject: ANN: python-ldap-2.3.9 Message-ID: Find a new release of python-ldap: http://www.python-ldap.org/ python-ldap provides an object-oriented API to access LDAP directory servers from Python programs. It mainly wraps the OpenLDAP 2.x libs for that purpose. Additionally it contains modules for other LDAP-related stuff (e.g. processing LDIF, LDAPURLs and LDAPv3 schema). Ciao, Michael. -- Michael Str?der E-Mail: michael at stroeder.com http://www.stroeder.com ---------------------------------------------------------------- Released 2.3.9 2009-07-26 Changes since 2.3.8: Lib/ * All modules (ldap, ldif, dsml and ldapurl) have common version number now * Non-exported function ldif.needs_base64() was abandoned and is now implemented as method LDIFWriter._needs_base64_encoding(). This allows sub-classes of LDIFWriter to implement determining whether attribute values have to be base64-encoded in a different manner and is the same approach like in class dsml.DSMLWriter. * LDAPUrlExtension._parse() now gracefully handles LDAP URL extensions without explicit exvalue as being set with implicit value None. Modules/ * New LDAP option constant ldap.OPT_X_SASL_NOCANON supported in LDAPObject.get/set_option() From python at mrabarnett.plus.com Sun Jul 26 11:07:40 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 26 Jul 2009 16:07:40 +0100 Subject: Can't get UDP example to work In-Reply-To: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: <4A6C713C.8050806@mrabarnett.plus.com> Paul Barry wrote: > I'm trying to get one of the examples from Foundation of Python > Network Programming to work. Specifically this is the UDP example > from Ch 3. First there is the server: > > #!/usr/bin/env python > # UDP Echo Server - Chapter 3 - udpechoserver.py > import socket, traceback, time > > host = '127.0.0.1' # Bind to all > interfaces > port = 51423 > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) > s.bind((host, port)) > > while 1: > try: > message, address = s.recvfrom(8192) > print "Got message '%s' from %s" % (message, address) > > # Echo it back > s.sendto(message, address) > print "Sent response to '%s'" % (address,) > > except (KeyboardInterrupt, SystemExit): > raise > except: > traceback.print_exc() > > Next I have a client written in Ruby, which works. I am posting thing > not to start a Ruby/Python flame war, but to simply prove that the > server works and there are no weird networking issues that would > prevent the Python client from working. The Ruby code is: > > #!/usr/bin/env ruby > require 'socket' > > socket = UDPSocket.new > socket.connect ARGV[0], ARGV[1] > > puts "Enter data to transmit: " > data = STDIN.gets.chomp > > socket.send data, 0 > puts "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > loop do > buf = socket.recvfrom(2048) > puts buf.first > end > > When I start the server and run that, the output looks like this: > > $ ch02/udp.rb 127.0.0.1 51423 > Enter data to transmit: > foobar > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > foobar > > Now, when I try the python example: > > #!/usr/bin/env python > # UDP Example - Chapter 2 - udp.py > > import socket, sys > > host = sys.argv[1] > textport = sys.argv[2] > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > try: > port = int(textport) > except ValueError: > # That didn't work. Look it up instead. > port = socket.getservbyname(textport, 'udp') > > s.connect((host, port)) > print "Enter data to transmit: " > data = sys.stdin.readline().strip() > s.sendall(data) > > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > while 1: > buf = s.recvfrom(2048) > sys.stdout.write(buf[0]) > > I don't ever get a response: > > $ ch02/udp.py 127.0.0.1 51423 > Enter data to transmit: > foobar > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > > The server sees the message and says it has sent a reply: > > Got message 'foobar' from ('127.0.0.1', 49623) > Sent response to '('127.0.0.1', 49623)' > > Any ideas as to why this doesn't work? > It works on my PC (Python 2.6.2, Windows XP Pro, service pack 3). From robert.avery at gmail.com Sun Jul 26 11:09:02 2009 From: robert.avery at gmail.com (Robert Avery) Date: Sun, 26 Jul 2009 08:09:02 -0700 (PDT) Subject: 'Registry' or 'Preference' settings in Mac Python Message-ID: <35258dc9-941d-42ec-9c35-9361d0c70da7@n11g2000yqb.googlegroups.com> Is there any way for me to save some sort of global preference for python that I can read/write at runtime that doesn't involve me explicitly creating a file? It may create a file underneath, but I don't want some path hard coded to my routine. In Windows, the Registry serves this purpose. Is there something similar for Mac? From deets at nospam.web.de Sun Jul 26 11:09:26 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 26 Jul 2009 17:09:26 +0200 Subject: cgi.fieldstorage() In-Reply-To: <13ed9818-aacd-4260-800b-a04177e13681@n11g2000yqb.googlegroups.com> References: <7cu9hhF29gt65U1@mid.uni-berlin.de> <7cv268F29q0j5U1@mid.uni-berlin.de> <13ed9818-aacd-4260-800b-a04177e13681@n11g2000yqb.googlegroups.com> Message-ID: <7d39t9F2963rqU1@mid.uni-berlin.de> gert schrieb: > On Jul 25, 2:33 am, "Diez B. Roggisch" wrote: >> gert schrieb: >> >>> On Jul 24, 7:32 pm, "Diez B. Roggisch" wrote: >>>> gert schrieb: >>>>> this is a non standard way to store multi part post data on disk >>>>> def application(environ, response): >>>>> with open('/usr/httpd/var/wsgiTemp','w') as f: >>>>> while True: >>>>> chunk = environ['wsgi.input'].read(8192).decode('latin1') >>>>> if not chunk: break >>>>> f.write(chunk) >>>>> response('200 OK',[]) >>>>> return ['complete'] >>>>> my question is how do i handle the file, so i can shuffle it into a db >>>>> using small chunks of memorie ? >>>> I don't think that's possible with the current DB-API. There is no >>>> stream-based BLOB-interface (as e.g. JDBC offers). >>>> So the answer certainly depends on your used RDBMS. For oracle, you >>>> would be lucky: >>>> http://cx-oracle.sourceforge.net/html/lob.html >>>> Other adapters I don't know about. >>> sqlite :) ok let say for now it would be impossible on a db level, but >>> before i reach the impossible, i still need to parse the file to >>> prepare the chunks. How do i do that ? How do i get the chunks without >>> loading the hole file into memorie ? >> It's memory - memorie might be some nice dutch girl you know :) >> >> Apart from that, your code above does exactly that - reads the data >> chunkwise. If the WSGI-implementation works proper, this will be the >> socket's input stream, so there is no memory overhead involved. >> >> Now of course if you want to have a multi-pass processing of the file >> without putting it into memory, then you need to save it to the harddisk >> befor. >> >> But honestly - we are talking about a web-application here. My >> DSL-connection has 1 MBit upstream, the average server has at least 2GB >> of memory available - so we are talking 20000 seconds uploading time to >> fill that memory. Which is about 5 hours. And you are decoding the data >> to a certain decoding, so we are not talking about binary data here - >> are you really sure memory is an issue? >> > > What about some http upload resume features ? What has that todo with reading data into memory or not? Of course you can create continous uploads with HTTP, but a single HTTP-request is a single HTTP-request. You can store the requests into individual blobs, and then when the overall file upload is finished, concatenate these. If you like. Or not, if you don't care, as you can as easily serve them even as one stream from those various chunks. But all of this has nothing todo with the need to not load POST-data into memory. This only becomes a problem if you are expecting uploads in the hundreds of megabytes. Diez From petite.abeille at gmail.com Sun Jul 26 11:18:17 2009 From: petite.abeille at gmail.com (Petite Abeille) Date: Sun, 26 Jul 2009 17:18:17 +0200 Subject: 'Registry' or 'Preference' settings in Mac Python In-Reply-To: <35258dc9-941d-42ec-9c35-9361d0c70da7@n11g2000yqb.googlegroups.com> References: <35258dc9-941d-42ec-9c35-9361d0c70da7@n11g2000yqb.googlegroups.com> Message-ID: <68879AA8-CC01-4CA1-815F-88C0DC0F0181@gmail.com> On Jul 26, 2009, at 5:09 PM, Robert Avery wrote: > In Windows, the Registry serves this purpose. Is there something > similar for Mac? http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html http://developer.apple.com/documentation/CoreFoundation/Reference/CFPreferencesUtils/Reference/reference.html http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFPreferences/CFPreferences.html -- PA. http://alt.textdrive.com/nanoki/ From aahz at pythoncraft.com Sun Jul 26 11:44:02 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 08:44:02 -0700 Subject: invoke method on many instances References: Message-ID: In article , Gabriel Genellina wrote: >En Thu, 23 Jul 2009 21:27:35 -0300, Aahz escribi?: >> In article , >> Gabriel Genellina wrote: >>> >>> NLMPI >> >> What? > >IHNFI What? Abbreviations are fine, but if someone asks you about one, it would be nice to just expand it instead of replying with another abbreviation. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From victorsubervi at gmail.com Sun Jul 26 11:48:29 2009 From: victorsubervi at gmail.com (Victor Subervi) Date: Sun, 26 Jul 2009 12:48:29 -0300 Subject: Cannot Get Form To Work In-Reply-To: References: <4dc0cfea0907230846g4f593219t308c64eec4943f93@mail.gmail.com> Message-ID: <4dc0cfea0907260848n1c59793bwd9f2dc1be46e66f1@mail.gmail.com> Oops. My bad. I did in fact have this line of code before calls to form: form = cgi.FieldStorage() I changed the except command from "pass" to "print sys.exc_info()" as per your suggestion. Still, however, the form just renews itself without offering any information whatsoever. You can see this here, if you like: http://13gems.com/test-Calculators_frame.py TIA, beno On Thu, Jul 23, 2009 at 2:01 PM, Dennis Lee Bieber wrote: > On Thu, 23 Jul 2009 12:46:16 -0300, Victor Subervi > declaimed the following in > gmane.comp.python.general: > > > sent. Here again is the code: > > > > from primeNumbers import primeNumbers > > > > try: > > lang = form.getfirst('lang', 'en') > > This code is incomplete -- where is "form" created? I see no > "import"s that might define such... > > > browser = form.getfirst('browser', 'all') > > site = form.getfirst('site', 'bridge') > > num = form.getfirst('num','') > > except: > > pass > > Since there is no such thing as "form" in the code you posted, the > first "form.getfirst()" operation will raise an exception. That > exception is then being ignored by this "pass". > > I'd suggest moving the code that outputs the HTTP response header > and the HTML prelude material to the top, then replace "pass" with > something that prints actual exception information (the stuff from > sys.exc_info() ) > -- > Wulfraed Dennis Lee Bieber KD6MOG > wlfraed at ix.netcom.com wulfraed at bestiaria.com > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: web-asst at bestiaria.com) > HTTP://www.bestiaria.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjmachin at lexicon.net Sun Jul 26 11:51:39 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 26 Jul 2009 08:51:39 -0700 (PDT) Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> <88e8ea19-7106-406a-a531-88c2b6893987@y7g2000yqa.googlegroups.com> Message-ID: On Jul 23, 11:53?am, Paul McGuire wrote: > On Jul 22, 5:43?pm, Filip wrote: > > # Needs re.IGNORECASE, and can have tag attributes, such as
CLEAR="ALL"> > line_break_re = re.compile('', re.UNICODE) Just in case somebody actually uses valid XHTML :-) it might be a good idea to allow for
> # what about HTML entities defined using hex syntax, such as &#xxxx; > amp_re = re.compile('\&(?![a-z]+?\;)', re.UNICODE | re.IGNORECASE) What about the decimal syntax ones? E.g. not only   and   but also   Also, entity names can contain digits e.g. ¹ ¾ From aahz at pythoncraft.com Sun Jul 26 11:55:18 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 08:55:18 -0700 Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> <87ljmogglz.fsf@benfinney.id.au> <02701ee5$0$5185$c3e8da3@news.astraweb.com> <056f629b-aa63-458a-ae16-ac40a759e446@h11g2000yqb.googlegroups.com> Message-ID: In article <056f629b-aa63-458a-ae16-ac40a759e446 at h11g2000yqb.googlegroups.com>, Shai wrote: > >class DocInherit(object): > """ > Docstring inheriting method descriptor > > The class itself is also used as a decorator > """ Nice! Maybe stick this on the Cookbook? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From piet at cs.uu.nl Sun Jul 26 12:13:22 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 26 Jul 2009 18:13:22 +0200 Subject: Why doesn't `from pkg import mod' work after `del pkg.mod'? References: <2a32df38-3b3e-4c50-b5c7-accd1ae978c4@z34g2000vbl.googlegroups.com> Message-ID: >>>>> ryles (r) wrote: >r> On Jul 25, 8:57?am, Piet van Oostrum wrote: >>> >>>>> ryles (r) wrote: >>> >r> According tohttp://www.python.org/doc/essays/packages.html: >>> >r> "The import statement first tests whether the item is defined in the >>> >r> package; if not, it assumes it is a module and attempts to load it." >>> >r> However, I've noticed that once a module is imported using the >>> >r> `from pkg import mod' syntax, if its name is deleted from the >>> >r> package namespace then subsequent imports with `from' will fail. >>> >>> This is incorrectly stated. Also on the initial import it will fail, not >>> just on subsequent imports. >>> >>> piet$ python >>> Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) >>> [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin >>> Type "help", "copyright", "credits" or "license" for more information. >>> from pkg import _impl >>> >>> _impl # this is from a print statement in _impl to show that it did import >>> Traceback (most recent call last): >>> ? File "", line 1, in >>> ImportError: cannot import name _impl >>> >r> Well, since __init__.py is executed first, I would regard it as >r> causing the initial (successful) import, and yours being the >r> subsequent one, so that the "initial" import does not fail, the >r> subsequent one does. Wording aside, though, I think we are on the same >r> page here. I was talking about the 'from pkg import _impl' statements because I thought that's what we were talking about. Of course this does an implicit 'import pkg' first, but that doesn't fail. In your OP you were also talking about `from pkg import mod' imports and said that the subsequent ones of these fails. I noticed that also the first one of these would fail. >>> >>> According to the documentation >>> the statement >>> from pkg import _impl >>> >>> _temp = __import__('pkg', globals(), locals(), ['_impl'], -1) >>> _impl = _temp._impl >>> >>> This fails because _temp (the imported module) doesn't have a binding >>> for _impl because you deleted it. >r> But supposing we hadn't deleted it, and __init__.py didn't import >r> _impl, there would still be no binding. Yet the importer would still >r> import and make the module available (presumably it "knows" to do this >r> since it was not already in sys.modules). The question really is that >r> since in our example pkg._impl is still in sys.modules, why won't the >r> importer add it to the pkg namespace again as it previous had? I would >r> imagine this is either by design, or is a case that was never >r> considered. Yes, that is also something I don't understand. >>> for _impl because you deleted it. By the way, if you have a `normal' >>> package that doesn't do anything with the name _impl, after the import >>> the pkg module namespace will have got a binding for _impl although it is not >>> in your code. It will have been put there by the import code. >r> Yes, this was noted further down in the original post with an example. Sorry, I missed that. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From bazwal at ftml.net Sun Jul 26 12:22:53 2009 From: bazwal at ftml.net (Baz Walter) Date: Sun, 26 Jul 2009 17:22:53 +0100 Subject: pyc files not automatically compiled on import Message-ID: <4A6C82DD.8020005@ftml.net> hello i thought that python automatically compiled pyc files after a module is successfully imported. what could prevent this happening? Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.mkdir('/home/baz/tmp/foo') >>> os.chdir('/home/baz/tmp/foo') >>> f = open('foo.py', 'w') >>> f.write('print "hello world"\n') >>> f.close() >>> os.listdir('.') ['foo.py'] >>> import foo hello world >>> os.listdir('.') # why no pyc file? ['foo.py'] >>> import py_compile >>> py_compile.compile('foo.py') >>> os.listdir('.') ['foo.py', 'foo.pyc'] From fuzzyman at gmail.com Sun Jul 26 12:44:14 2009 From: fuzzyman at gmail.com (Fuzzyman) Date: Sun, 26 Jul 2009 09:44:14 -0700 (PDT) Subject: pyc files not automatically compiled on import References: Message-ID: On Jul 26, 5:22?pm, Baz Walter wrote: > hello > > i thought that python automatically compiled pyc files after a module is > successfully imported. what could prevent this happening? > > Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > ?>>> import os > ?>>> os.mkdir('/home/baz/tmp/foo') > ?>>> os.chdir('/home/baz/tmp/foo') > ?>>> f = open('foo.py', 'w') > ?>>> f.write('print "hello world"\n') > ?>>> f.close() > ?>>> os.listdir('.') > ['foo.py'] > ?>>> import foo > hello world > ?>>> os.listdir('.') # why no pyc file? > ['foo.py'] > ?>>> import py_compile > ?>>> py_compile.compile('foo.py') > ?>>> os.listdir('.') > ['foo.py', 'foo.pyc'] Works for me I'm afraid (Mac OS X and Python 2.6). Michael Foord -- http://www.ironpythoninaction.com/ From roy at panix.com Sun Jul 26 12:53:59 2009 From: roy at panix.com (Roy Smith) Date: Sun, 26 Jul 2009 12:53:59 -0400 Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: In article <148abf0f-c9e4-4156-8f16-e4e5615d350b at s6g2000vbp.googlegroups.com>, Paul Barry wrote: > host = '127.0.0.1' # Bind to all interfaces This threw me off the track for a little while. The comment is wrong! You're not binding to all interfaces, you're binding specifically to the loopback interface. For what you're doing, it's fine, but the comment is still wrong. It's also a little weird that the example uses connect() and sendall(). Those are TCP-ish type constructs. They work with UDP sockets, but using them sort of obscures the fundamental datagram nature of UDP. Well, enough of that. The real problem seems to be right here: > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > while 1: > buf = s.recvfrom(2048) > sys.stdout.write(buf[0]) Try adding sys.stdout.flush(), and it should work. Or change it to print. I've never seen this book, but if this code really is verbatim from the book, I'm not very impressed. From __peter__ at web.de Sun Jul 26 13:07:30 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 26 Jul 2009 19:07:30 +0200 Subject: pyc files not automatically compiled on import References: Message-ID: Baz Walter wrote: > i thought that python automatically compiled pyc files after a module is > successfully imported. what could prevent this happening? > > > Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import os > >>> os.mkdir('/home/baz/tmp/foo') > >>> os.chdir('/home/baz/tmp/foo') > >>> f = open('foo.py', 'w') > >>> f.write('print "hello world"\n') > >>> f.close() > >>> os.listdir('.') > ['foo.py'] > >>> import foo > hello world > >>> os.listdir('.') # why no pyc file? > ['foo.py'] > >>> import py_compile > >>> py_compile.compile('foo.py') > >>> os.listdir('.') > ['foo.py', 'foo.pyc'] You did not set the PYTHONDONTWRITEBYTECODE environment variable in a former life, or did you? From Tom.sully at gmx.com Sun Jul 26 13:13:44 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 10:13:44 -0700 (PDT) Subject: Removing newlines from string on windows (without replacing) References: <7d2nfnF29tk6fU1@mid.uni-berlin.de> Message-ID: Sorry for not providing enough information - and Rhodri, for sending my second message I used the GMX webmail client rather than my usual Linux client as I was testing it on windows. That must have screwed it up, I need to download Thunderbird on Mozzila. The thing that was messing it up was that the endlines are handled differently on each each OS, so I changed the code to strip the endlines to be: if os.name == "nt": s = sauce.rstrip("\r\n") else: s = sauce.replace("\n", "") This works fine now, (os.name returns nt on windows and posix on linux) Thanks for the help, and sorry for slow response and lack of detail, I'm still trying to get used to this usenet thing :P -- print "Tom" From qauzzix at gmail.com Sun Jul 26 13:48:27 2009 From: qauzzix at gmail.com (Qauzzix) Date: Sun, 26 Jul 2009 10:48:27 -0700 (PDT) Subject: How do I generate dia diagrams from python source code? References: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Message-ID: On Jul 25, 8:31?am, "Gabriel Genellina" wrote: > En Fri, 24 Jul 2009 17:52:47 -0300, Qauzzix escribi?: > > > Since I have been usingdiato make my UML diagrams. I also found an > > util named dia2code that generates python code fromdiadiagram. Now > > that I have that option I really want to find a way to generatedia > > diagram from existing code and/or maintain my diagrams. > > > I have been googling ?like crazy trying to find a way but with no > > luck. Anyone here know how to do this? > > SPE does that:http://pythonide.stani.be/ > > -- > Gabriel Genellina I think You are misunderstanding my question. I downloaded SPE and I have been trying it out. It has an UML featus but in no way I can see can I export it to be used by DIA the diagram editor. I could export it as an image, nothing more. There exists many apps for extracting UML diagrams from Python source but I need it to conform with DIA. From gallium.arsenide at gmail.com Sun Jul 26 13:54:52 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Sun, 26 Jul 2009 10:54:52 -0700 (PDT) Subject: Removing newlines from string on windows (without replacing) References: <7d2nfnF29tk6fU1@mid.uni-berlin.de> Message-ID: <2abd5ecb-6fed-4763-9563-2caf63ca3c15@r25g2000vbn.googlegroups.com> On Jul 26, 1:13?pm, Tom wrote: > The thing that was messing it up was that the endlines are handled > differently on each each OS, so I changed the code to strip the > endlines to be: > > ? ? if os.name == "nt": > ? ? ? ? s = sauce.rstrip("\r\n") > ? ? else: > ? ? ? ? s = sauce.replace("\n", "") Well, this is doing two different things. When os.name is 'nt', you are getting rid of all *trailing* CRLFs. Otherwise, you are getting rid of all LFs *anywhere in the string*. (Even if it so happens sauce will only ever have LFs at the end, it's still better to use the method that is closest to your intended meaning.) Personally, I prefer using sauce.rstrip() with no parameters (one of the suggestions Rhodri mentioned) if it's not important to preserve trailing spaces, since this will work regardless of end-of-line style (and typically, trailing spaces, tabs, etc. are undesirable anyway). John From aahz at pythoncraft.com Sun Jul 26 13:56:54 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 10:56:54 -0700 Subject: Removing newlines from string on windows (without replacing) References: Message-ID: In article , Tom wrote: > >I have an annoying problem. While I mainly use Linux when I distribute >this program to friends and on the internet, it'll get used on Windows. >So, I tested my python program on my Windows Vista dual boot, running >the same version of python (2.6) as my Linux, and got an error at the >following code. > >s = sauce.replace("\n", "") > >Sauce is a string, read from a file, that I need to remove newlines >from. This code works fine in Linux, but not in Windows. rstrip("\n") >won't work for me, so anybody know how to get this working on Windows? Use open(fname, 'U') -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. :-)" --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From martin at v.loewis.de Sun Jul 26 14:03:39 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 20:03:39 +0200 Subject: C-API, tp_dictoffset vs tp_members In-Reply-To: <7d2mu7F27a9tbU1@mid.uni-berlin.de> References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> <7d2mu7F27a9tbU1@mid.uni-berlin.de> Message-ID: <4a6c9a7c$0$12532$9b622d9e@news.freenet.de> >>> I have a predefined set of members, some of which are optional. >> Having optional fields is also a good reason. > > What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? Right - this works for optional objects. However, it can't possibly work for any of the other fields. > I would > have though that the former describes an optional field, because the > behaviour of accessing it when it is NULL is the same as accessing a > nonexistent field. However, I see that it still appears in the dir() > output even if it is NULL, so it seems I'm misunderstanding this. I suppose that's because there will still be a descriptor for the field in the class. > The 'example_attribute' can be accessed, but it is not visible in the > dir() output. The code for this is below. I see. So you do need a tp_members list: static PyMemberDef example_members[] = { {"__dict__", T_OBJECT, offsetof(Example, dict), READONLY}, {0} }; Perhaps dir() should know about tp_dictoffset, but alas, it doesn't. Regards, Martin From vivainio at gmail.com Sun Jul 26 14:20:59 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Sun, 26 Jul 2009 11:20:59 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: Message-ID: <69c8f3be-cc84-48e5-9969-ef9e87836094@e27g2000yqm.googlegroups.com> On Jul 23, 11:29?pm, Nick Craig-Wood wrote: > > ?The syntax would be something like: > > > ?def work(): > > > ? ?showstatus("building") > > ? ?r = yield runshell("make") > > ? ?showstatus("installing") > > ? ?r = yield runshell("make install") > > ? ?showstatus("Success") > > > ?mygui.startwork(work) > > ?# returns immediately, runs work() gradually in the background. > > > ?The catch is that showstatus() would need to be run in the mainloop, > > ?so running the whole thing in a thread is a no-go. > > > ?I imagine runshell() would be implemented in terms of QProcess, or > > ?subprocess.Popen/os.system and a worker thread. > > > ?Anyone done this already, or do I have to roll my own? > > You might want to look at twisted, in particular > > ?http://twistedmatrix.com/trac/wiki/DeferredGenerator Indeed, DeferredGenerator looks pretty much what I'm interested in, thanks for the pointer. Unfortunately, it has the big Twisted dependency. That's probably not a showstopper, but Twisted requires a degree of buy-in when something smaller would do. From santhosh.vkumar at gmail.com Sun Jul 26 14:22:42 2009 From: santhosh.vkumar at gmail.com (Santhosh Kumar) Date: Sun, 26 Jul 2009 23:52:42 +0530 Subject: Itext for Python Message-ID: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> Hi all, One of my cousin suggested me to do a IText PDF converter for python. Actually I heard that there is no separate IText converter either we have to go for jython or GCJ with wrapper. Instead of wrapping, my plan is to create a separate module with Python and I am thinking of doing this in as my final year project also. Kindly give me your suggestion. With anticipation, SanthoshVKumar. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nagle at animats.com Sun Jul 26 14:24:48 2009 From: nagle at animats.com (John Nagle) Date: Sun, 26 Jul 2009 11:24:48 -0700 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> Message-ID: <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Beni Cherniavsky wrote: > On Jul 22, 9:36 am, Hendrik van Rooyen > wrote: >> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >> >>> My guess is that it was probably for optimization reasons long ago. >>> I've never heard a *good* reason why Python needs both. >> The good reason is the immutability, which lets you use >> a tuple as a dict key. >> > On Jul 22, 9:36 am, Hendrik van Rooyen > wrote: >> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >> >>> My guess is that it was probably for optimization reasons long ago. >>> I've never heard a *good* reason why Python needs both. >> The good reason is the immutability, which lets you use >> a tuple as a dict key. >> > The *technical* reason is immutability for dict keys. > Dict could allow mutable objects as keys by comparing *by value*, > making a copy on insertion and hashing the current value on lookup. > Prior art: the 2.3 sets module allows mutable Sets as elements in > Sets, by making ImmutableSet copies on insertion, and hashing Sets as > if they are temporarily immutable on lookup. > > This inspired PEP 351 and ambitious proposals to expand the approach > to all Python with a copy-on-write scheme. But these ideas were > rejected, and the 2.4 builtin sets only allow frozenset elements. > Half the reason is technical: copy-on-write and harder than it sounds, > and without it you pay a performance price. > > But the deeper reason is style: immutable types are convenient! > The allow a pure-functional style of code, which can be simpler. > Of course, sometimes an imperative style is simpler. Depends on the > problem. An interesting issue is Python objects, which are always mutable. A "dict" of Python objects is allowed, but doesn't consider the contents of the objects, just their identity (address). Only built-in types are immutable; one cannot create a class of immutable objects. So things like "frozenset" had to be built into the language. A tuple is really a frozen list. Arguably, frozen objects should have been a general concept. Conceptually, they're simple - once "__init__" has run, there can be no more changes to fields of the object. Compare the C++ approach, where you can have a "map" of any object that defines the "<" operator. Python has "__cmp__" for objects, but built-in dictionaries don't use it. Of course, one could create a "map" class in Python that uses "__cmp__", which would allow dictionary-type operations on arbitrary objects. There are some interesting uses for immutable objects in multithreaded programs. They can be shared between threads without locking. If it weren't for the infamous Global Interpreter Lock, which basically limits Python programs to using one CPU at a time, Python would have to deal with locking in a more advanced way. A system where immutable objects can be shared and passed between threads, and mutable objects must be "synchronized" (in the Java sense) to be shared would be useful. John Nagle From vivainio at gmail.com Sun Jul 26 14:32:11 2009 From: vivainio at gmail.com (Ville M. Vainio) Date: Sun, 26 Jul 2009 11:32:11 -0700 (PDT) Subject: Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops References: <4a65f0d8$0$1599$742ec2ed@news.sonic.net> Message-ID: Apologies for the long subject line, here it is again: "Pep 342 (val = yield MyGenerator(foo)), synchronous os.system() that doesn't block gui event loops" On Jul 21, 7:48?pm, John Nagle wrote: > > The idea: > > > To run functions that execute a series of system commands without > > blocking the ui, *and* without adding state machine logic. > > ? ? At some level, there's going to be state machine logic. > You need interlocking so that the user can't initiate > simultaneous conflicting background operations from the GUI. This belongs to the "I don't care" category, as you'd expect to have only one operation like this on the flight at one time. The idea is to allow making simple scripts that you execute from the gui, that *may* need to access the gui objects at one point or another (so execution has to happen in the main thread most of the time - apart from blocking, long operations). > ? ? The usual solution is to run the background operations > in another thread, and have them put events on a queue > read by the GUI task. Yeah, and the receiving end of the "queue" here would be the same block of statements that launched the request - that is, I want to avoid having to create handler functions, and keep the program from linear. > ? ? You also have to work out how to cancel background operations, > if they're going to be as big as a "make". I think that is easy to accomplish with generators, provided that you can let the long-lasting operation run to completion - just close() the generator. If the long lasting computation was in a subprocess, rather than a thread, it could be interrupted in a cleaner fashion even. From drobinow at gmail.com Sun Jul 26 15:26:46 2009 From: drobinow at gmail.com (David Robinow) Date: Sun, 26 Jul 2009 14:26:46 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> > This doesn't mean they're on the same level - in fact, if you read carefully > you'll see my original post said as much: python attracted average > programmers; php attracted mediocre programmers and even some > non-programmers, which means that php is clearly a lesser language than > python. I'm a mediocre programmer. Does this mean I should switch to PHP? From bazwal at ftml.net Sun Jul 26 15:30:12 2009 From: bazwal at ftml.net (Baz Walter) Date: Sun, 26 Jul 2009 20:30:12 +0100 Subject: pyc files not automatically compiled on import In-Reply-To: References: Message-ID: <4A6CAEC4.7020300@ftml.net> Peter Otten wrote: > You did not set the PYTHONDONTWRITEBYTECODE environment variable in a former > life, or did you? thanks peter no i didn't, but i've just discovered a script in /etc/profile.d that did. now i'll have to try to find out how that script got in there :-| From dotancohen at gmail.com Sun Jul 26 15:34:52 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sun, 26 Jul 2009 22:34:52 +0300 Subject: Looking for a dream language: sounds like Python to me. Message-ID: <880dece00907261234x684547f4g5272b211379a7ba1@mail.gmail.com> Referring to this article: http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ The author, who is specifically looking for math-related functions, writes: """ The dream algorithm R&D tool would be similar to Matlab or Mathematica but could be compiled to fast, efficient binaries similar to ANSI C and would be available for all platforms. An integrated GUI builder similar to Visual Basic and integrated network support would be helpful. """ It looks to me like he is looking for Python. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From doomster at knuut.de Sun Jul 26 15:39:17 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sun, 26 Jul 2009 21:39:17 +0200 Subject: C-API, tp_dictoffset vs tp_members References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> <7d2mu7F27a9tbU1@mid.uni-berlin.de> <4a6c9a7c$0$12532$9b622d9e@news.freenet.de> Message-ID: <7d3pnaF29o57gU1@mid.uni-berlin.de> "Martin v. L?wis" wrote: >>>> I have a predefined set of members, some of which are optional. >>> Having optional fields is also a good reason. >> >> What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? > > Right - this works for optional objects. However, it can't possibly > work for any of the other fields. I have two members, one T_OBJECT and one T_OBJECT_EX. Both are NULL and both still appear in the dir() output. For one, accessing it returns 'None', for the other it raises an exception. I would have expected an element that is not accessible, not even 'None', to also not be visible in dir(). >> I would have thought that the former describes an optional field, >> because the behaviour of accessing it when it is NULL is the same >> as accessing a nonexistent field. However, I see that it still >> appears in the dir() output even if it is NULL, so it seems I'm >> misunderstanding this. > > I suppose that's because there will still be a descriptor for the > field in the class. So is that intentional or is it dir() that could be improved there? Should I file a bugreport? Thanks for the tip with __dict__ in tp_members, that does the job! Uli From pauljbarry3 at gmail.com Sun Jul 26 15:40:39 2009 From: pauljbarry3 at gmail.com (Paul Barry) Date: Sun, 26 Jul 2009 12:40:39 -0700 (PDT) Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a@p23g2000vbl.googlegroups.com> On Jul 26, 12:53?pm, Roy Smith wrote: > In article > <148abf0f-c9e4-4156-8f16-e4e5615d3... at s6g2000vbp.googlegroups.com>, > ?Paul Barry wrote: > > > host = '127.0.0.1' ? ? ? ? ? # Bind to all interfaces > > This threw me off the track for a little while. ?The comment is wrong! ? > You're not binding to all interfaces, you're binding specifically to the > loopback interface. ?For what you're doing, it's fine, but the comment is > still wrong. You are right. The example in the book actually has host = '' So the comment is correct. In trying to debug what was going on, I put 127.0.0.1 to see if that had something to do with it, which it didn't. > > It's also a little weird that the example uses connect() and sendall(). ? > Those are TCP-ish type constructs. ?They work with UDP sockets, but using > them sort of obscures the fundamental datagram nature of UDP. > > Well, enough of that. ?The real problem seems to be right here: > > > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > while 1: > > ? ? buf = s.recvfrom(2048) > > ? ? sys.stdout.write(buf[0]) > > Try adding sys.stdout.flush(), and it should work. ?Or change it to print. > Yes, that did the trick, thanks. I should have thought of that, if I would have just printed something after the recvfrom line I would have realized it was no longer blocking and the problem was with getting the client to print the data to stdout, not with getting a response from the server. > I've never seen this book, but if this code really is verbatim from the > book, I'm not very impressed. I like the book so far. It's a little verbose, but I think that's because he's trying to teach the concepts, rather than the write it how you actually would in production. In this case, I think he's trying to illustrate how the UDP example compares to the TCP example from the previous section, which is why he choose to keep the methods the same. But why not use print buf[0] instead of sys.stdin.write(buf[0]) that much, I don't know. From pauljbarry3 at gmail.com Sun Jul 26 15:42:32 2009 From: pauljbarry3 at gmail.com (Paul Barry) Date: Sun, 26 Jul 2009 12:42:32 -0700 (PDT) Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> Message-ID: <87ff9f36-30cb-4982-9730-7dcee491c0de@l2g2000vba.googlegroups.com> On Jul 26, 11:07?am, MRAB wrote: > Paul Barry wrote: > > I'm trying to get one of the examples from Foundation of Python > > Network Programming to work. ?Specifically this is the UDP example > > from Ch 3. ?First there is the server: > > > #!/usr/bin/env python > > # UDP Echo Server - Chapter 3 - udpechoserver.py > > import socket, traceback, time > > > host = '127.0.0.1' ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Bind to all > > interfaces > > port = 51423 > > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > > s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) > > s.bind((host, port)) > > > while 1: > > ? ? try: > > ? ? ? ? message, address = s.recvfrom(8192) > > ? ? ? ? print "Got message '%s' from %s" % (message, address) > > > ? ? ? ? # Echo it back > > ? ? ? ? s.sendto(message, address) > > ? ? ? ? print "Sent response to '%s'" % (address,) > > > ? ? except (KeyboardInterrupt, SystemExit): > > ? ? ? ? raise > > ? ? except: > > ? ? ? ? traceback.print_exc() > > > Next I have a client written in Ruby, which works. ?I am posting thing > > not to start a Ruby/Python flame war, but to simply prove that the > > server works and there are no weird networking issues that would > > prevent the Python client from working. ?The Ruby code is: > > > #!/usr/bin/env ruby > > require 'socket' > > > socket = UDPSocket.new > > socket.connect ARGV[0], ARGV[1] > > > puts "Enter data to transmit: " > > data = STDIN.gets.chomp > > > socket.send data, 0 > > puts "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > > loop do > > ? buf = socket.recvfrom(2048) > > ? puts buf.first > > end > > > When I start the server and run that, the output looks like this: > > > $ ch02/udp.rb 127.0.0.1 51423 > > Enter data to transmit: > > foobar > > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > > foobar > > > Now, when I try the python example: > > > #!/usr/bin/env python > > # UDP Example - Chapter 2 - udp.py > > > import socket, sys > > > host = sys.argv[1] > > textport = sys.argv[2] > > > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > > try: > > ? ? port = int(textport) > > except ValueError: > > ? ? # That didn't work. ?Look it up instead. > > ? ? port = socket.getservbyname(textport, 'udp') > > > s.connect((host, port)) > > print "Enter data to transmit: " > > data = sys.stdin.readline().strip() > > s.sendall(data) > > > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." > > while 1: > > ? ? buf = s.recvfrom(2048) > > ? ? sys.stdout.write(buf[0]) > > > I don't ever get a response: > > > $ ch02/udp.py 127.0.0.1 51423 > > Enter data to transmit: > > foobar > > Looking for replies; press Ctrl-C or Ctrl-Break to stop. > > > The server sees the message and says it has sent a reply: > > > Got message 'foobar' from ('127.0.0.1', 49623) > > Sent response to '('127.0.0.1', 49623)' > > > Any ideas as to why this doesn't work? > > It works on my PC (Python 2.6.2, Windows XP Pro, service pack 3). Doesn't work on a Mac with Python 2.5.1 or 2.6.2 unless you flush stdout or change it to print. Not sure why it would work on one platform and not the other. From Tom.sully at gmx.com Sun Jul 26 15:45:10 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 12:45:10 -0700 (PDT) Subject: Globalize all variables in function without knowing names. Message-ID: Does anyknow know how to do this? The reason I'm asking is because I'm trying to make a mini-programming language for fun, and need to work with variables. The user types 'set NAME "DATA" ' or whatever else the variable is going to be called/contain. I have: def variablework(varname, varset): exec("global "+varname) #DEFUNCT vars()[varname] = varset exec("print "+varname) #Debug purposes only This sets the variable (varname is the name of the variable, and varset is the data for it to contain) all fine and dandy, but it is local, and after some googling I realised that you can't globalised variables using exec. So I think the next best thing would be to find a way to globalize every variable in the function, but I don't know how to do this. If there isn't a way of doing this, can someone suggest another way of globalizing variable without knowing their names? From roy at panix.com Sun Jul 26 16:31:43 2009 From: roy at panix.com (Roy Smith) Date: Sun, 26 Jul 2009 16:31:43 -0400 Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a@p23g2000vbl.googlegroups.com> Message-ID: In article <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a at p23g2000vbl.googlegroups.com>, Paul Barry wrote: > In this case, I think he's trying to illustrate how the UDP example > compares to the TCP example from the previous section, which is why he > choose to keep the methods the same. I suppose, but I still think that's a confusing way to teach the differences between UDP and TCP. The fundamental differences are: 1) TCP makes sure what you send gets there (for reasonably large values of "makes sure"). UDP just tosses stuff out onto the network and hopes for the best. 2) TCP is connection-oriented. Connections must be explicitly set up before any data can flow, and once two sockets are connected, they can never be disconnected (short of tearing down the connection and starting again with two new sockets). With UPD, every time you send data, you have to specify where it's going, and a given socket can be talking to many different remote endpoints on a packet-by-packet basis. 3) TCP is a stream protocol, which hides record boundaries; it is free to combine and/or split up your data into whatever packet boundaries it wants, and there is no way for the receiving end to discover where the boundaries are. UDP, on the other hand, honors record boundaries; there is a one-to-one correspondence between each write (send, sendto) call at one and each read (recv, recvfrom) call at the other. It's difficult to demonstrate #1 in a simple example, so I'll skip that. But, #2 and #3 are easy. The example you gave uses connect() on a UDP socket. That's not pointing out the differences between UDP and TCP, that's hiding them behind a strange corner of the API. There really is no connection set up when you do connect() on a UDP socket; it's just a way of saying, "From now on, I'm going to skip telling you the destination of each individual datagram. Just use this destination from now on". It saves moving a bit of data around, but it's not fundamentally a connection. As for the stream/datagram distinction, sendall() is very much a stream creature. It says, "Just keep banging out packets until you've managed to transmit the whole buffer". That only makes sense in an environment where packet boundaries are meaningless. Using it with UDP is sort of like saying, "Use as many packets as it takes, as long as that number is one". It's not not a concept that makes sense in a datagram world. That it works at all on UDP is (IHMO) a mis-guided attempt at interface uniformity. If I were writing an example on how to use UDP, I would leave out the connect() call, and use sendto() instead of sendall(). Once the student has those down, that's the time to talk about weird corner cases like how connect() and sendall() can be twisted to work with UDP sockets. Or maybe not even bother. From Tom.sully at gmx.com Sun Jul 26 16:57:02 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 13:57:02 -0700 (PDT) Subject: Removing newlines from string on windows (without replacing) References: <7d2nfnF29tk6fU1@mid.uni-berlin.de> <2abd5ecb-6fed-4763-9563-2caf63ca3c15@r25g2000vbn.googlegroups.com> Message-ID: > (Even if it so happens sauce > will only ever have LFs at the end, it's still better to use the > method that is closest to your intended meaning.) Oh, you're right. I'll change that now. I suppose removing all endlines will be better, incase, somehow, endlines in the middle of the line arrises. > if it's not important to preserve trailing spaces The way I parse the file currently splits away all whitespace later on. While I could change this, I have a nice system worked out and my code is working, so it's not top priority. From python at mrabarnett.plus.com Sun Jul 26 16:59:42 2009 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 26 Jul 2009 21:59:42 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: <4A6CC3BE.6060806@mrabarnett.plus.com> David Robinow wrote: >> This doesn't mean they're on the same level - in fact, if you read carefully >> you'll see my original post said as much: python attracted average >> programmers; php attracted mediocre programmers and even some >> non-programmers, which means that php is clearly a lesser language than >> python. > I'm a mediocre programmer. Does this mean I should switch to PHP? If you're a mediocre programmer who knows you're mediocre, but wants to improve and is willing to learn, then stick with Python. You'll get better. :-) From roy at panix.com Sun Jul 26 17:04:23 2009 From: roy at panix.com (Roy Smith) Date: Sun, 26 Jul 2009 17:04:23 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: In article , Raffael Cavallaro wrote: > php is clearly a lesser language than python. I'm as much of a Python bigot as anybody. Likewise, I put down php for all the sorts of theoretical reasons people have been putting it down. Not to mention that it looks like Perl, which is enough to make anybody go screaming in the other direction. However, it's hard to argue with success. A ton of very useful software has been written in php (including MediaWiki, which drives Wikipedia). One needs to have a very highly developed sense of theoretical purity to look down their noses at the language that drives one of the highest volume web sites on the planet. From gagsl-py2 at yahoo.com.ar Sun Jul 26 17:06:48 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 26 Jul 2009 18:06:48 -0300 Subject: invoke method on many instances References: Message-ID: En Sun, 26 Jul 2009 12:44:02 -0300, Aahz escribi?: > In article , > Gabriel Genellina wrote: >> En Thu, 23 Jul 2009 21:27:35 -0300, Aahz >> escribi?: >>> In article , >>> Gabriel Genellina wrote: >>>> >>>> NLMPI >>> >>> What? >> >> IHNFI > > What? Abbreviations are fine, but if someone asks you about one, it > would be nice to just expand it instead of replying with another > abbreviation. Ok, if you insist... NLMPI = Ni La M?s Puta Idea. IHNFI = I Have No Fucking Idea. -- Gabriel Genellina From martin at v.loewis.de Sun Jul 26 17:24:43 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 26 Jul 2009 23:24:43 +0200 Subject: C-API, tp_dictoffset vs tp_members In-Reply-To: <7d3pnaF29o57gU1@mid.uni-berlin.de> References: <7cl194F28ojoqU1@mid.uni-berlin.de> <4A6C0DFC.4030702@v.loewis.de> <7d2mu7F27a9tbU1@mid.uni-berlin.de> <4a6c9a7c$0$12532$9b622d9e@news.freenet.de> <7d3pnaF29o57gU1@mid.uni-berlin.de> Message-ID: <4A6CC99B.4070406@v.loewis.de> >>>>> I have a predefined set of members, some of which are optional. >>>> Having optional fields is also a good reason. >>> What is the use of T_OBJECT_EX vs T_OBJECT in PyMemberDef then? >> Right - this works for optional objects. However, it can't possibly >> work for any of the other fields. > > I have two members, one T_OBJECT and one T_OBJECT_EX. Both are NULL and both > still appear in the dir() output. For one, accessing it returns 'None', for > the other it raises an exception. I would have expected an element that is > not accessible, not even 'None', to also not be visible in dir(). I understood your expectation already. I maintain my theory that it shows up because there is a descriptor on the class. >> I suppose that's because there will still be a descriptor for the >> field in the class. > > So is that intentional or is it dir() that could be improved there? It's probably both. > Should I file a bugreport? Only if you can provide a patch also. My guess is that when you have the patch completed, you might realize that it is not an improvement, despite achieving what you wanted to achieve. Regards, Martin From raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com Sun Jul 26 17:41:31 2009 From: raffaelcavallaro at pas.espam.s.il.vous.plait.mac.com (Raffael Cavallaro) Date: Sun, 26 Jul 2009 17:41:31 -0400 Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On 2009-07-26 17:04:23 -0400, Roy Smith said: > One > needs to have a very highly developed sense of theoretical purity to look > down their noses at the language that drives one of the highest volume web > sites on the planet. It's nothing to do with theoretical purity and everything to do with practicality. php has a limited range of utility. Within that range, it's clearly quite useful. Python is useful for a greater range of tasks which makes it a more generally useful (and in this sense, better) language. -- Raffael Cavallaro From devent at deventm.org Sun Jul 26 17:49:58 2009 From: devent at deventm.org (Erwin Mueller) Date: Mon, 27 Jul 2009 07:49:58 +1000 Subject: How to comment constant values? Message-ID: <200907270749.58845.devent@deventm.org> Hello, I'm new to Python (using it for two months) and I wonder how can I comment the const. values with the doc-strings. I.e. if I have code: >FRACTION_MIN = 1 >FRACTION_MAX = 10 > >class Fraction(collections.MutableSequence): > '''Model a fraction with denominators. > > It contains one ore more denomintors in a list for quick access. > The class is thread save, it uses a lock for all operations. > ''' > > def __init__(self): > # ... The class and the methods can have doc-strings, but what about the const. values, FRACTION_MIN and FRACTION_MAX? Thank you, Erwin. From clp2 at rebertia.com Sun Jul 26 18:06:29 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 26 Jul 2009 15:06:29 -0700 Subject: How to comment constant values? In-Reply-To: <200907270749.58845.devent@deventm.org> References: <200907270749.58845.devent@deventm.org> Message-ID: <50697b2c0907261506o4c7b0a04t2e5155833f62be7e@mail.gmail.com> On Sun, Jul 26, 2009 at 2:49 PM, Erwin Mueller wrote: > Hello, I'm new to Python (using it for two months) and I wonder how can I > comment the const. values with the doc-strings. I.e. if I have code: > >>FRACTION_MIN = 1 >>FRACTION_MAX = 10 >> >>class Fraction(collections.MutableSequence): >> ? ?'''Model a fraction with denominators. >> >> ? ?It contains one ore more denomintors in a list for quick access. >> ? ?The class is thread save, it uses a lock for all operations. >> ? ?''' >> >> ? ?def __init__(self): >> ? ? ? ? ? ? ? # ... > > The class and the methods can have doc-strings, but what about the const. > values, FRACTION_MIN and FRACTION_MAX? Only modules, classes, and functions/methods can have docstrings associated with them. For anything else, you have to use comments; or you can mention them in the docstrings of related things. Cheers, Chris -- http://blog.rebertia.com From Tom.sully at gmx.com Sun Jul 26 18:07:08 2009 From: Tom.sully at gmx.com (Tom) Date: Sun, 26 Jul 2009 15:07:08 -0700 (PDT) Subject: Globalize all variables in function without knowing names. References: Message-ID: <5649dfb8-01a6-490d-bf30-b4a5206e644e@j21g2000yqe.googlegroups.com> Ah, thanks for that Dennis. I have a system already figured out where the user manually pre-enters all the variables into a .py file, which obviously isn't very efficient. The language isn't going to be used much except by me, so I suppose I'll just work with that until I look more into dictionaries, but I'm still beginning Python, and before that the only coding I did was a little bit of C++, so I'll wait up for that :P From hekevintran at gmail.com Sun Jul 26 18:24:25 2009 From: hekevintran at gmail.com (Kevin) Date: Sun, 26 Jul 2009 15:24:25 -0700 (PDT) Subject: Error in compiling Python on OS X Message-ID: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> I am trying to compile Python 2.6.2 on Mac OS X 10.5.7. I have Xcode 3.1.3 installed. The error I got is below. $ ./configure --prefix=/Users/me/python checking for --with-universal-archs... 32-bit checking MACHDEP... darwin checking EXTRAPLATDIR... $(PLATMACDIRS) checking machine type as reported by uname -m... i386 checking for --without-gcc... no checking for gcc... gcc checking for C compiler default output file name... configure: error: C compiler cannot create executables See `config.log' for more details. Please send any ideas. From rhodri at wildebst.demon.co.uk Sun Jul 26 18:32:30 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 26 Jul 2009 23:32:30 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: On Sun, 26 Jul 2009 14:31:06 +0100, Raffael Cavallaro wrote: > On 2009-07-26 09:16:39 -0400, aahz at pythoncraft.com (Aahz) said: > >> There are plenty of expert C++ >> programmers who switched to Python; > > "plenty" is an absolute term, not a relative term. I sincerely doubt > that the majority of python users were formerly *expert* C++ programmers. Nitpicking like this doesn't help your case. >> your thesis only applies to the >> legions of people who found it difficult to learn C++ in the first >> place. > > No, my thesis applies to the overwhelming majority of programmers who > found it more difficult to *master* (i.e., not merely use) C++ as > opposed to mastering python. BTW, this is a *complement* not a dis; > python is a better language than C++ precisely because it is more > sensibly and elegantly designed than C++ and therefore easier to master. It is perhaps more accurate to say that Python was designed where C++ aggregated. C was fundamentally the wrong place to have started from, making C++ (as distinct from the subset of C++ that is really C) harder to learn, never mind master, than it really needed to be. > php represents the same process but farther down the ladder, as it were. > There's often a tradeoff between ease of mastery and power. python hits > a sweet spot for many tasks and many programmers, especially as compared > to C++ (or even lisp, which though more powerful than python is more > difficult to master. lisp beats C++ on both counts imho - more powerful > *and* easier to master). php hits a sweet spot only in a very restricted > domain. Beyond that, it is clearly inferior to python which has greater > power, but is more difficult to master. Fundamentally incorrect. PHP attracted many people because of where it lives in the web application structure -- the part of the language that was thought about very hard. Beyond that there's nothing much to master, so the whole ease vs power debate is rather pointless. -- Rhodri James *-* Wildebeest Herder to the Masses From bearophileHUGS at lycos.com Sun Jul 26 18:42:22 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Sun, 26 Jul 2009 15:42:22 -0700 (PDT) Subject: How to comment constant values? References: <200907270749.58845.devent@deventm.org> Message-ID: <5c1b5c1e-7677-4786-a026-b4825568cf5b@j32g2000yqh.googlegroups.com> Chris Rebert: > Only modules, classes, and functions/methods can have docstrings > associated with them. > For anything else, you have to use comments; or you can mention them > in the docstrings of related things. What about adding docstrings to other Python things, especially to variables? Bye, bearophile From deets at nospam.web.de Sun Jul 26 18:46:01 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 00:46:01 +0200 Subject: Error in compiling Python on OS X In-Reply-To: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> References: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> Message-ID: <7d44lbF29p3p7U1@mid.uni-berlin.de> Kevin schrieb: > I am trying to compile Python 2.6.2 on Mac OS X 10.5.7. I have Xcode > 3.1.3 installed. > > The error I got is below. > > $ ./configure --prefix=/Users/me/python Use a framework-build. > checking for --with-universal-archs... 32-bit > checking MACHDEP... darwin > checking EXTRAPLATDIR... $(PLATMACDIRS) > checking machine type as reported by uname -m... i386 > checking for --without-gcc... no > checking for gcc... gcc > checking for C compiler default output file name... > configure: error: C compiler cannot create executables > See `config.log' for more details. > > Please send any ideas. What happens if you write a simple test.c like this: int main() { return 0; } and compile it with gcc on the commandline? There must be a file called "a.out" afterwards. diez From deets at nospam.web.de Sun Jul 26 18:47:08 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 00:47:08 +0200 Subject: How to comment constant values? In-Reply-To: References: <200907270749.58845.devent@deventm.org> Message-ID: <7d44ndF29p3p7U2@mid.uni-berlin.de> Chris Rebert schrieb: > On Sun, Jul 26, 2009 at 2:49 PM, Erwin Mueller wrote: >> Hello, I'm new to Python (using it for two months) and I wonder how can I >> comment the const. values with the doc-strings. I.e. if I have code: >> >>> FRACTION_MIN = 1 >>> FRACTION_MAX = 10 >>> >>> class Fraction(collections.MutableSequence): >>> '''Model a fraction with denominators. >>> >>> It contains one ore more denomintors in a list for quick access. >>> The class is thread save, it uses a lock for all operations. >>> ''' >>> >>> def __init__(self): >>> # ... >> The class and the methods can have doc-strings, but what about the const. >> values, FRACTION_MIN and FRACTION_MAX? > > Only modules, classes, and functions/methods can have docstrings > associated with them. > For anything else, you have to use comments; or you can mention them > in the docstrings of related things. While this is technically true, writing docstrings to constants (module or classlevel) works when one uses tools such as epydoc to generate documentation. Diez From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 18:49:33 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 22:49:33 GMT Subject: Globalize all variables in function without knowing names. References: <5649dfb8-01a6-490d-bf30-b4a5206e644e@j21g2000yqe.googlegroups.com> Message-ID: <027cce66$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 15:07:08 -0700, Tom wrote: > Ah, thanks for that Dennis. I have a system already figured out where > the user manually pre-enters all the variables into a .py file, which > obviously isn't very efficient. The language isn't going to be used much > except by me, so I suppose I'll just work with that until I look more > into dictionaries, but I'm still beginning Python, and before that the > only coding I did was a little bit of C++, so I'll wait up for that :P Dictionaries are fundamental to Python and very useful. Not learning about them before starting to write code is like not learning about the accelerator pedal before starting to drive a car. You might be able to get somewhere by getting out and pushing, but it will be slow and exhausting. Have you done the tutorial? http://www.python.org/doc/current/tutorial/ -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 18:49:47 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 22:49:47 GMT Subject: Can't get UDP example to work References: <148abf0f-c9e4-4156-8f16-e4e5615d350b@s6g2000vbp.googlegroups.com> <2f578124-1ae3-45a5-a0c9-b8b05c0b5d3a@p23g2000vbl.googlegroups.com> Message-ID: <027cce74$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 12:40:39 -0700, Paul Barry wrote: > On Jul 26, 12:53?pm, Roy Smith wrote: >> In article >> <148abf0f-c9e4-4156-8f16-e4e5615d3... at s6g2000vbp.googlegroups.com>, >> ?Paul Barry wrote: >> >> > host = '127.0.0.1' ? ? ? ? ? # Bind to all interfaces >> >> This threw me off the track for a little while. ?The comment is wrong! >> You're not binding to all interfaces, you're binding specifically to >> the loopback interface. ?For what you're doing, it's fine, but the >> comment is still wrong. > > You are right. The example in the book actually has > > host = '' > > So the comment is correct. In trying to debug what was going on, I put > 127.0.0.1 to see if that had something to do with it, which it didn't. Now I have to quote Aahz's email sig: "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. " --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 > But why not use > > print buf[0] > > instead of > > sys.stdin.write(buf[0]) > > that much, I don't know. print adds a trailing newline to the output, so the two lines aren't quite equivalent. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 18:58:34 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 22:58:34 GMT Subject: Help understanding the decisions *behind* python? - immutable objects References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: <027cd083$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 11:24:48 -0700, John Nagle wrote: > An interesting issue is Python objects, which are always mutable. > A "dict" of Python objects is allowed, but doesn't consider the contents > of the objects, just their identity (address). Only built-in types are > immutable; one cannot create a class of immutable objects. Yes you can, for some definition of "can": http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html Admittedly pure Python objects are only "cooperatively immutable". The immutability relies on the caller not going out of its way to break the instance, so you can mutate it if you work at it. One could, I suppose, try putting in complicated tricks to prevent that, but anything written in Python can ultimately be modified in Python. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 19:20:58 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 23:20:58 GMT Subject: How to comment constant values? References: <200907270749.58845.devent@deventm.org> <5c1b5c1e-7677-4786-a026-b4825568cf5b@j32g2000yqh.googlegroups.com> Message-ID: <027cd5c2$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 15:42:22 -0700, Bearophile wrote: > Chris Rebert: >> Only modules, classes, and functions/methods can have docstrings >> associated with them. >> For anything else, you have to use comments; or you can mention them in >> the docstrings of related things. > > What about adding docstrings to other Python things, especially to > variables? Python doesn't have variables, it has objects bound to names. Because __doc__ is looked up on the class, not the instance, there's not much point in adding a docstring to the instance (even for instances which allow attributes to be added). And because names are internally stored as strings, you can't add a docstring to the name. So what you ask for is impossible. But of course that could be changed, if there was consensus that what you ask for would be useful. But consider the consequences: # A thought-experiment >>> margin = 3 # allow a little bit of whitespace >>> margin.__doc__ = "Extra space in pixels." >>> help(x) => displays "Extra space in pixels." So far so good. But now consider two equally unacceptable situations: (1) If the docstring sticks to the object, then you get the docstring inappropriately showing up in places where it should: >>> number_of_widgets = len( [a, b, c] ) >>> help(number_of_widgets) => displays "Extra space in pixels." (2) If the docstring sticks to the name, then you get the docstring hanging around when you recycle the name to mean something else: >>> margin = 15/100.0 # our profit margin is 15% >>> help(margin) => displays "Extra space in pixels." Now, in a large program, one shouldn't recycle names like that, but in a large program, you're unlikely to programmatically look up docstrings. Docstrings, and the help() function, are particularly useful in an interactive session, which is precisely the time that you're likely to recycle names. In other words, it's currently impossible to bind docstrings to "variables" in Python, but even more fundamentally, the request is semantically incompatible with the Python name/object binding model. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 19:23:30 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 26 Jul 2009 23:23:30 GMT Subject: How to comment constant values? References: <200907270749.58845.devent@deventm.org> <7d44ndF29p3p7U2@mid.uni-berlin.de> Message-ID: <027cd65b$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote: >> Only modules, classes, and functions/methods can have docstrings >> associated with them. >> For anything else, you have to use comments; or you can mention them in >> the docstrings of related things. > > While this is technically true, writing docstrings to constants (module > or classlevel) works when one uses tools such as epydoc to generate > documentation. I've never used epydoc, so I'm not sure what you mean. Presumably it uses source code analysis to detect: CONSTANT = 42 """This is a constant.""" even though the string is ignored by the compiler. Is that correct? -- Steven From andrew-newspost at areilly.bpc-users.org Sun Jul 26 19:28:53 2009 From: andrew-newspost at areilly.bpc-users.org (Andrew Reilly) Date: 26 Jul 2009 23:28:53 GMT Subject: If Scheme is so good why MIT drops it? References: <17pco71asfk7d.1kd4oiidz6nqy.dlg@40tude.net> <5gi665hk6ggn0a48o7qd4h3p16phe959hc@4ax.com> <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> Message-ID: <7d475kF29aga9U1@mid.individual.net> On Sun, 26 Jul 2009 09:31:06 -0400, Raffael Cavallaro wrote: > On 2009-07-26 09:16:39 -0400, aahz at pythoncraft.com (Aahz) said: > >> There are plenty of expert C++ >> programmers who switched to Python; > > "plenty" is an absolute term, not a relative term. I sincerely doubt > that the majority of python users were formerly *expert* C++ > programmers. > >> your thesis only applies to the >> legions of people who found it difficult to learn C++ in the first >> place. > > No, my thesis applies to the overwhelming majority of programmers who > found it more difficult to *master* (i.e., not merely use) C++ as > opposed to mastering python. BTW, this is a *complement* not a dis; > python is a better language than C++ precisely because it is more > sensibly and elegantly designed than C++ and therefore easier to master. Isn't it widely accepted that the number of people who have mastered C++ is about five? All of the rest of us just struggle... [I know enough of C++ to avoid it whenever I can, and to not use it for my own projects. I'm happy with a mix of C, python and lisp(or scheme).] -- Andrew From pinkeen at gmail.com Sun Jul 26 19:44:54 2009 From: pinkeen at gmail.com (Filip) Date: Sun, 26 Jul 2009 16:44:54 -0700 (PDT) Subject: python fast HTML data extraction library References: <37da38d2-09a8-4fd2-94b4-5feae9675dcd@k1g2000yqf.googlegroups.com> <88e8ea19-7106-406a-a531-88c2b6893987@y7g2000yqa.googlegroups.com> Message-ID: <0643e7ab-3136-4352-a849-a19882c3f9b2@c2g2000yqi.googlegroups.com> On Jul 23, 3:53?am, Paul McGuire wrote: > # You should use raw string literals throughout, as in: > # blah_re = re.compile(r'sljdflsflds') > # (note the leading r before the string literal). ?raw string > literals > # really help keep your re expressions clean, so that you don't ever > # have to double up any '\' characters. Thanks, I didn't know about that, updated my code. > # Attributes might be enclosed in single quotes, or not enclosed in > any quotes at all. > attr_re = re.compile('([\da-z]+?)\s*=\s*\"(.*?)\"', re.DOTALL | > re.UNICODE | re.IGNORECASE) Of course, you mean attribute's *value* can be enclosed in single/ double quotes? To be true, I haven't seen single quote variant in HTML lately but I checked it and it seems to be in the specs and it can be even quite useful (man learns something every day). Thank you for pointing that one out, I updated the code accordingly (just realized that condition check REs need an update too :/). As far as the lack of value quoting is concerned, I am not so sure I need this - It would significanly obfuscate my REs and this practice is rather deprecated, considered unsafe and I've seen it only in very old websites. > How would you extract data from a table? ?For instance, how would you > extract the data entries from the table at this URL:http://tf.nist.gov/tf-cgi/servers.cgi? ?This would be a good example > snippet for your module documentation. This really seems like a nice example. I'll surely explain it in my docs (examples are surely needed there ;)). > Try extracting all of the sldjlsfjd links from > yahoo.com, and see how much of what you expect actually gets matched. The library was used in my humble production environment, processing a few hundred thousand+ of pages and spitting out about 10000 SQL records so it does work quite good with a simple task like extracting all links. However, I can't really say that the task introduced enough diversity (there were only 9 different page templates) to say that the library is 'tested'... On Jul 26, 5:51 pm, John Machin wrote: > On Jul 23, 11:53 am, Paul McGuire wrote: > > > On Jul 22, 5:43 pm, Filip wrote: > > > # Needs re.IGNORECASE, and can have tag attributes, such as
> CLEAR="ALL"> > > line_break_re = re.compile('', re.UNICODE) > > Just in case somebody actually uses valid XHTML :-) it might be a good > idea to allow for
> > > # what about HTML entities defined using hex syntax, such as &#xxxx; > > amp_re = re.compile('\&(?![a-z]+?\;)', re.UNICODE | re.IGNORECASE) > > What about the decimal syntax ones? E.g. not only   and   > but also   > > Also, entity names can contain digits e.g. ¹ ¾ Thanks for pointing this out, I fixed that. Although it has very little impact on how the library performs its main task (I'd like to see some comments on that ;)). From aahz at pythoncraft.com Sun Jul 26 19:56:59 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 16:56:59 -0700 Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> Message-ID: In article <2a408da6-af57-45d0-a75f-4cbe384bb7f9 at s15g2000yqs.googlegroups.com>, Michal Kwiatkowski wrote: >On Jul 25, 10:00=A0pm, Jason Tackaberry wrote: >> On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: >>> >>> Is there a way to tell if a generator has been exhausted using pure >>> Python code? I've looked at CPython sources and it seems that >> >> Upon a cursory look, after a generator 'gen' is exhausted (meaning >> gen.next() has raised StopIteration), it seems that gen.gi_frame will be >> None. > >Only in Python 2.5 or higher though. I need to support Python 2.3 and >2.4 as well, sorry for not making that clear in the original post. Are you sure? It appears to work in Python 2.4; I don't have time to check 2.3. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From tjreedy at udel.edu Sun Jul 26 20:10:00 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 26 Jul 2009 20:10:00 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> Message-ID: Michal Kwiatkowski wrote: > The thing is I don't need the next item. I need to know if the > generator has stopped without invoking it. Write a one-ahead iterator class, which I have posted before, that sets .exhausted to True when next fails. tjr From nyamatongwe+thunder at gmail.com Sun Jul 26 20:25:46 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Mon, 27 Jul 2009 00:25:46 GMT Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: Mark Lawrence: > If my sleuthing is correct the problem is with these lines > > ilow *= self->itemSize; > ihigh *= self->itemSize; > > in GapBuffer_slice being computed before ilow and ihigh are compared to > anything. This particular bug was because ihigh is the maximum 32 bit integer 2147483647 so multiplying it by the integer item size (4) caused overflow. Adding an extra check fixes this: if (ihigh > self->lengthBody / self->itemSize) ihigh = self->lengthBody / self->itemSize; Committed a new version 1.02 and new downloads are available from Google code. http://code.google.com/p/gapbuffer/downloads/list Neil From jackdied at gmail.com Sun Jul 26 20:36:17 2009 From: jackdied at gmail.com (Jack Diederich) Date: Sun, 26 Jul 2009 20:36:17 -0400 Subject: Mutable Strings - Any libraries that offer this? In-Reply-To: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> References: <44e28fa9-af98-47da-a83b-5ec538b73d10@d9g2000prh.googlegroups.com> Message-ID: On Mon, Jul 20, 2009 at 5:57 AM, casebash wrote: > Hi, > > I have searched this list and found out that Python doesn't have a > mutable string class (it had an inefficient one, but this was removed > in 3.0). Are there any libraries outside the core that offer this? It depends on how mutable you want your strings. There have been several patches proposed on python-dev over the years that do things like lazy concatenation and ropes. They were always shot down because the implementation or the semantics were very complicated. -Jack From tdelaney at avaya.com Sun Jul 26 20:53:33 2009 From: tdelaney at avaya.com (Delaney, Timothy (Tim)) Date: Mon, 27 Jul 2009 08:53:33 +0800 Subject: missing 'xor' Boolean operator In-Reply-To: <8d46fe89-9e91-45c7-9958-1b06e7f7b693@k19g2000yqn.googlegroups.com> Message-ID: Mark Dickinson wrote: >> Since the 'and' and 'or' already return objects (and objects >> evaluate to true or false), then 'xor' should behave likewise, IMO. >> I expect that would be the case if it were ever added to the >> language. > > I'm not so sure. Did you ever wonder why the any() and all() > functions introduced in 2.5 return a boolean rather than returning > one of their arguments? (I did, and I'm still not sure what the > answer is.) Consider the case of any() and all() operating on an empty iterable. What type should they return? It is impossible in the case of any() and all() to always return one of the elements due to this edge case. Similarly, it is impossible in all cases for a boolean xor to return one of the arguments - if both arguments evaluate to a true (Something) value, xor must return a false (Nothing) result. Tim Delaney From bearophileHUGS at lycos.com Sun Jul 26 21:09:47 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Sun, 26 Jul 2009 18:09:47 -0700 (PDT) Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> Message-ID: William Dode': > I updated the script (python, c and java) with your unrolled version > + somes litle thinks. [...] > c 1.85s > gcj 2.15s > java 2.8s > python2.5 + psyco 3.1s > unladen-2009Q2 145s (2m45) > python2.5 254s (4m14s) > python3.1 300s (5m) > ironpython1.1.1 680s (11m20) Sorry for being late, I was away. In your last C version this code is useless because the C compiler is able to perform such simple optimization by itself (but probably Python isn't able, so if you want the code to be the similar in all versions it may be better to keep it): shift_0=shift[0]; shift_1=shift[1]; shift_2=shift[2]; shift_3=shift[3]; shift_4=shift[4]; shift_5=shift[5]; shift_6=shift[6]; shift_7=shift[7]; This part in the Python code is useless: shift_0 = shift[0] shift_1 = shift[1] shift_2 = shift[2] shift_3 = shift[3] shift_4 = shift[4] shift_5 = shift[5] shift_6 = shift[6] shift_7 = shift[7] Because later you copy values locally anyway: def solve(nb, x, y, SIDE=SIDE, SQR_SIDE=SQR_SIDE, circuit=circuit, shift_0=shift_0, shift_1=shift_1, shift_2=shift_2, shift_3=shift_3, shift_4=shift_4, shift_5=shift_5, shift_6=shift_6, shift_7=shift_7, ): So doing something like this is probably enough: def solve(nb, x, y, SIDE=SIDE, SQR_SIDE=SQR_SIDE, circuit=circuit, shift_0=shift[0], shift_1=shift[1], shift_2=shift[2], shift_3=shift[3], shift_4=shift[4], shift_5=shift[5], shift_6=shift[6], shift_7=shift[7], ): In low-level languages like C unrolling has to be done with care, to avoid slowing down the code. I have tried your latest C version using your compiler options, my MinGW based on GCC 4.3.2 produces a crash at runtime. Using LLVM-GCC it runs in 1.31 seconds. The D version is a bit less optimized than your last C versions, yet using DMD it runs in 1.08-1.10 seconds. Let's see if someone is able to write a C version faster than that D code :-) Have you have compiled/read my D version? In the D version you may have missed that I did use an extra trick: unsigned integers, so it needs just two tests to see if a number is in the 0-5, 0-5 square :-) Note that Pyd, the Python-D bridge, may work with the latest DMD version still (and it works if you use a bit older DMD compiler): http://pyd.dsource.org/ Bye, bearophile From greg at cosc.canterbury.ac.nz Sun Jul 26 21:12:26 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Mon, 27 Jul 2009 13:12:26 +1200 Subject: missing 'xor' Boolean operator In-Reply-To: References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> Message-ID: <7d4d6gF28qk7kU1@mid.individual.net> Terry Reedy wrote: > In Math and Python, a != is a comparison operator like <, Although Python extends the chaining principle to !=, this is somewhat questionable, because a < b and b < c implies a < c, but a != b and b != c does not imply a != c. I'm not sure I've ever seen a mathematician write a != b != c, but if I did, I would tend to think he meant to say that none of a, b, c are equal to any other. That's not what it means in Python, though. -- Greg From greg at cosc.canterbury.ac.nz Sun Jul 26 21:30:41 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Mon, 27 Jul 2009 13:30:41 +1200 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> Message-ID: <7d4e8nF2a9i83U1@mid.individual.net> Michal Kwiatkowski wrote: > The first generator isn't finished, it yielded 1 and None. Second one > is exhausted after yielding a single value (1). The problem is that, > under Python 2.4 or 2.3 both invocations will generate the same trace > output. This seems to be a deficiency in the trace mechanism. There really ought to be a 'yield' event to distinguish yields from returns. You could put in a feature request on python-dev concerning this. -- Greg From aahz at pythoncraft.com Sun Jul 26 22:44:24 2009 From: aahz at pythoncraft.com (Aahz) Date: 26 Jul 2009 19:44:24 -0700 Subject: invoke method on many instances References: Message-ID: In article , Gabriel Genellina wrote: > >Ok, if you insist... > >NLMPI = Ni La M?s Puta Idea. >IHNFI = I Have No Fucking Idea. Thanks! -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From nidlobm at hotmail.com Sun Jul 26 23:14:52 2009 From: nidlobm at hotmail.com (Michael Boldin) Date: Sun, 26 Jul 2009 23:14:52 -0400 Subject: SEC doc parsing Message-ID: I am looking for any parsing routines (written in python), for Security Exchange Commision (SEC) documents. These documents are on the SEC's EDGAR system (at ftp://ftp.sec.gov ) and I am especially interested in insider trading files known as Forms 3,4 and 5. _________________________________________________________________ NEW mobile Hotmail. Optimized for YOUR phone. Click here. http://windowslive.com/Mobile?ocid=TXT_TAGLM_WL_CS_MB_new_hotmail_072009 -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Sun Jul 26 23:18:53 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 27 Jul 2009 03:18:53 GMT Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> Message-ID: <027d0d84$0$5185$c3e8da3@news.astraweb.com> On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: > Michal Kwiatkowski wrote: > >> The thing is I don't need the next item. I need to know if the >> generator has stopped without invoking it. > > Write a one-ahead iterator class, which I have posted before, that sets > .exhausted to True when next fails. And hope that the generator doesn't have side-effects... -- Steven From robert.kern at gmail.com Mon Jul 27 00:11:12 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sun, 26 Jul 2009 23:11:12 -0500 Subject: PyQt GUI In-Reply-To: <4ee78bf1-476d-4e41-b9da-c20297aad861@13g2000prl.googlegroups.com> References: <7bj5ulF22b4ktU1@mid.uni-berlin.de> <60ff3276-0570-4222-9055-4e1a40538e61@r15g2000pra.googlegroups.com> <5131c895-b155-486f-aff2-7587a114e60b@o18g2000pra.googlegroups.com> <88b09175-9167-4903-9524-2725a9ab9819@j9g2000prh.googlegroups.com> <16422b45-a58b-451f-88d8-d85b70808d31@i4g2000prm.googlegroups.com> <4ee78bf1-476d-4e41-b9da-c20297aad861@13g2000prl.googlegroups.com> Message-ID: On 2009-07-26 07:04, Helvin wrote: > C:\Qt\VTKbin7\Wrapping\Python>python setup.py install > Traceback (most recent call last): > File "setup.py", line 138, in > raise "ERROR: Must specify BUILD_TYPE= on command > line." > TypeError: exceptions must be classes or instances, not str > > Is it just a small error with input format or something? Read the source, please: # The build type ('Release', 'Debug' etc.). If vtk_has_configuration_types # is true this must be set. It may be set on the command line by something # like 'BUILD_TYPE=Release'. For example:: # python setup.py install --prefix=D:\\Python23 BUILD_TYPE=Release -- 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 Mon Jul 27 00:16:24 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sun, 26 Jul 2009 23:16:24 -0500 Subject: How to comment constant values? In-Reply-To: <027cd65b$0$5185$c3e8da3@news.astraweb.com> References: <200907270749.58845.devent@deventm.org> <7d44ndF29p3p7U2@mid.uni-berlin.de> <027cd65b$0$5185$c3e8da3@news.astraweb.com> Message-ID: On 2009-07-26 18:23, Steven D'Aprano wrote: > On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote: > >>> Only modules, classes, and functions/methods can have docstrings >>> associated with them. >>> For anything else, you have to use comments; or you can mention them in >>> the docstrings of related things. >> While this is technically true, writing docstrings to constants (module >> or classlevel) works when one uses tools such as epydoc to generate >> documentation. > > I've never used epydoc, so I'm not sure what you mean. Presumably it uses > source code analysis to detect: > > CONSTANT = 42 > """This is a constant.""" > > even though the string is ignored by the compiler. > > Is that correct? epydoc 3 can actually parse the file to grab comments with a "#:" marker: #: This is a constant. CONSTANT = 42 -- 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 dpresley at midiowa.net Mon Jul 27 00:20:51 2009 From: dpresley at midiowa.net (drednot57) Date: Sun, 26 Jul 2009 21:20:51 -0700 (PDT) Subject: len() should always return something References: Message-ID: <6ac8501e-b3ca-403b-8edb-c055b65bbff1@e11g2000yqo.googlegroups.com> To the best of my recollection, the len() function only applies to container objects; i. e. tuples, lists, strings, etc. an integer object is not a container, thus one receives an error when sending an int as an argument for len(). From tjreedy at udel.edu Mon Jul 27 02:02:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 27 Jul 2009 02:02:19 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <027d0d84$0$5185$c3e8da3@news.astraweb.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: > >> Michal Kwiatkowski wrote: >> >>> The thing is I don't need the next item. I need to know if the >>> generator has stopped without invoking it. >> Write a one-ahead iterator class, which I have posted before, that sets >> .exhausted to True when next fails. > > > And hope that the generator doesn't have side-effects... If run to exhastion, the same number of side-effects happen. The only difference is that they each happen once step happpen sooner. For reading a file that is irrelevant. Much else, and the iterator is not just an iterator. tjr From tjreedy at udel.edu Mon Jul 27 02:05:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 27 Jul 2009 02:05:55 -0400 Subject: missing 'xor' Boolean operator In-Reply-To: <7d4d6gF28qk7kU1@mid.individual.net> References: <50697b2c0907141243p2f36db1fmefb276c21d95a8af@mail.gmail.com> <7d4d6gF28qk7kU1@mid.individual.net> Message-ID: greg wrote: > Terry Reedy wrote: > >> In Math and Python, a> != is a comparison operator like <, > > Although Python extends the chaining principle to > !=, this is somewhat questionable, because > a < b and b < c implies a < c, but a != b and > b != c does not imply a != c. > > I'm not sure I've ever seen a mathematician > write a != b != c, but if I did, I would tend > to think he meant to say that none of a, b, > c are equal to any other. That's not what it > means in Python, though. However, == is transitive, and a == b == c is quite common. It would hardly do to have different rules for !=. Either we have a uniform rule for a compare_op b compare_ob c, as we do, or we have several fussy rules that would be hard to remember. From steve at REMOVE-THIS-cybersource.com.au Mon Jul 27 02:22:54 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 27 Jul 2009 06:22:54 GMT Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> Message-ID: <027d38a5$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 02:02:19 -0400, Terry Reedy wrote: > Steven D'Aprano wrote: >> On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: >> >>> Michal Kwiatkowski wrote: >>> >>>> The thing is I don't need the next item. I need to know if the >>>> generator has stopped without invoking it. >>> Write a one-ahead iterator class, which I have posted before, that >>> sets .exhausted to True when next fails. >> >> >> And hope that the generator doesn't have side-effects... > > If run to exhastion, the same number of side-effects happen. The only > difference is that they each happen once step happpen sooner. For > reading a file that is irrelevant. Much else, and the iterator is not > just an iterator. I believe the OP specifically said he needs to detect whether or not an iterator is exhausted, without running it to exhaustion, so you shouldn't assume that the generator has been exhausted. When it comes to side-effects, timing matters. For example, a generator which cleans up after it has run (deleting temporary files, closing sockets, etc.) will leave the environment in a different state if run to exhaustion than just before exhaustion. Even if you store the final result in a one-ahead class, you haven't saved the environment, and that may be significant. (Of course, it's possible that it isn't significant. Not all differences make a difference.) The best advice is, try to avoid side-effects, especially in generators. -- Steven From jayshree06comp at gmail.com Mon Jul 27 02:47:00 2009 From: jayshree06comp at gmail.com (jayshree) Date: Sun, 26 Jul 2009 23:47:00 -0700 (PDT) Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: On Jul 23, 5:48?pm, Piet van Oostrum wrote: > >>>>> jayshree (j) wrote: > >j> On Jul 21, 8:59?pm, Piet van Oostrum wrote: > >>> The recipient_public_key.pem file is the public key of the recipient > >>> which means the person that is going to receive the encrypted message. > >>> You should get it from the recipient him/herself or from some key store > >>> where s/he has deposited it. > > [...] > > >j> error coming like - IOError: [Errno 2] No such file or directory: > >j> 'recipient_public_key.pem' > >j> Is this not the inbuilt file. > >j> How should i create such type of file. > > You don't create it. See above. If you understand what is is you know > why it can't be builtin. If you don't understand it is better if you > first learn about OpenSSL, otherwise you run the risk to make serious > errors. > > If you are just experimenting to create a message for yourself then you > have to create the public key because you are then the recipient > yourself. That has been answered onhttp://stackoverflow.com/questions/1169798/m2crypto-package > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org import M2Crypto from M2Crypto import RSA,SSL def encrypt(): pk = open('my_key.public.pem', 'rb').read() rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub object. plaintext = 4545479545655576767767686688782344 msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding) print msg; encrypt() This is code i am refering. The Problem is coming with .pem file. I also asked this questions at http://stackoverflow.com/questions/1176864/problem-with-the-pem-file-closed which has not been answered . please help me out.... thanks From nagle at animats.com Mon Jul 27 03:06:36 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 00:06:36 -0700 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: <027cd083$0$5185$c3e8da3@news.astraweb.com> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> <027cd083$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4a6d514d$0$1641$742ec2ed@news.sonic.net> Steven D'Aprano wrote: > On Sun, 26 Jul 2009 11:24:48 -0700, John Nagle wrote: > >> An interesting issue is Python objects, which are always mutable. >> A "dict" of Python objects is allowed, but doesn't consider the contents >> of the objects, just their identity (address). Only built-in types are >> immutable; one cannot create a class of immutable objects. > > Yes you can, for some definition of "can": > > http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html > > > Admittedly pure Python objects are only "cooperatively immutable". Right. I've been thinking about this as a way of improving concurrency handling. The general idea is that objects shared across threads would have to be either be immutable or synchronized. Regular objects would be limited to a single thread. It's a path to multithread programs without a global lock. Needs more work to become a serious proposal. (It may not be worth the trouble; a few years ago we were hearing about how 64-core CPUs were going to become mainstream, but instead, the industry is going in the direction of the same compute power for less money and with less power consumption.) John Nagle From abhigyan_agrawal at in.ibm.com Mon Jul 27 03:11:42 2009 From: abhigyan_agrawal at in.ibm.com (abhi) Date: Mon, 27 Jul 2009 00:11:42 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX Message-ID: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> Hi, I am facing a problem using PyArg_ParseTuple() in my C-API extension. Here is a small repro of the function: static PyObject *parsetuple_test(PyObject *self, PyObject *args) { SQLUSMALLINT param_no = 0; PyObject *py_obj = NULL; if (!PyArg_ParseTuple(args, "Oi", &py_obj, ¶m_no)){ return NULL; } printf("%d\n", param_no); return NULL; } This function works fine and prints the correct value passed (param_no) when I test it on Linux or Windows. However, it always prints 0 (initial value defined in function) when I run it on AIX. I think the problem is with "Oi" argument, if I replace that with "OO" and specifically convert it integer/sqlsmallint. It works fine on AIX, but I want to use "i" and not "O". Any ideas on what is the reason behind the problem and how to fix this? Thanks, Abhigyan From jayshree06comp at gmail.com Mon Jul 27 03:28:50 2009 From: jayshree06comp at gmail.com (jayshree) Date: Mon, 27 Jul 2009 00:28:50 -0700 (PDT) Subject: open a file in python Message-ID: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() Please tell me how to open a file placed in any directory or in same directory. After opening this file i want to use the contain (public key ) for encryption thanks From metallourlante at gmail.com Mon Jul 27 03:40:09 2009 From: metallourlante at gmail.com (Alex) Date: Mon, 27 Jul 2009 00:40:09 -0700 (PDT) Subject: web page retrieve problems References: <16343e3d-1be4-422b-8bff-c07fa4aa4c54@j9g2000prh.googlegroups.com> Message-ID: <7dd16ce7-8442-4e67-a949-442b287e00dd@j21g2000yqe.googlegroups.com> On Jul 26, 8:57?am, golu wrote: > the following function retrieves pages from the web and saves them in > a specified dir. i want to extract the respective filenames from the > urls e.g the page code.google.com shud be saved as code-google.htm ?or > something similar. can u suggest me a way to do it Try with urllib.urlretrieve from standard lib: urllib.urlretrieve(url[, filename[, reporthook[, data]]])? Copy a network object denoted by a URL to a local file, if necessary. If the URL points to a local file, or a valid cached copy of the object exists, the object is not copied. Return a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object, possibly cached). Exceptions are the same as for urlopen(). From kushal.kumaran+python at gmail.com Mon Jul 27 04:09:26 2009 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Mon, 27 Jul 2009 13:39:26 +0530 Subject: open a file in python In-Reply-To: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: <1e364c4e0907270109u2aa8e702oa3ebaaab065f5e90@mail.gmail.com> On Mon, Jul 27, 2009 at 12:58 PM, jayshree wrote: > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > Please tell me how to open a file placed in any directory or in same > directory. > > After opening this file i want to use the contain (public key ) for > encryption > Does the code you've put into your message not read that file? If you get an exception, copy-paste in the traceback message you get into your mail. -- kushal From piet at cs.uu.nl Mon Jul 27 04:18:20 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 10:18:20 +0200 Subject: Looking for a dream language: sounds like Python to me. References: Message-ID: >>>>> Dotan Cohen (DC) wrote: >DC> Referring to this article: >DC> http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ >DC> The author, who is specifically looking for math-related functions, writes: >DC> """ >DC> The dream algorithm R&D tool would be similar to Matlab or Mathematica >DC> but could be compiled to fast, efficient binaries similar to ANSI C >DC> and would be available for all platforms. An integrated GUI builder >DC> similar to Visual Basic and integrated network support would be >DC> helpful. >DC> """ >DC> It looks to me like he is looking for Python. No fast, efficient binaries yet, and no integrated GUI builder. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From vesa.koppa at gmail.com Mon Jul 27 04:51:51 2009 From: vesa.koppa at gmail.com (=?ISO-8859-1?Q?Vesa_K=F6pp=E4?=) Date: Mon, 27 Jul 2009 11:51:51 +0300 Subject: How do I generate dia diagrams from python source code? In-Reply-To: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> References: <8d6fc359-7a02-4978-a9d3-93a1271a50db@e27g2000yqm.googlegroups.com> Message-ID: <4a6d6aa6$0$24756$4f793bc4@news.tdc.fi> Qauzzix wrote: > Greetings. > > Since I have been using dia to make my UML diagrams. I also found an > util named dia2code that generates python code from dia diagram. Now > that I have that option I really want to find a way to generate dia > diagram from existing code and/or maintain my diagrams. > > I have been googling like crazy trying to find a way but with no > luck. Anyone here know how to do this? > > Peace, > - Jakob Sv. Bjarnason Hi. There is this utility called autodia. http://www.aarontrevena.co.uk/opensource/autodia/ I think it does what you want. It is written in perl. It can read several languages and outputs dia diagrams. BTW, I found it by going to dia2code homepage and then to its links page. It is listed in there. -- Vesa From s.selvamsiva at gmail.com Mon Jul 27 04:53:33 2009 From: s.selvamsiva at gmail.com (S.Selvam) Date: Mon, 27 Jul 2009 14:23:33 +0530 Subject: Itext for Python In-Reply-To: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> References: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> Message-ID: On Sun, Jul 26, 2009 at 11:52 PM, Santhosh Kumar wrote: > Hi all, One of my cousin suggested me to do a IText > PDF converter for python. Actually I heard that there is > no separate IText converter either we have to go for jython or GCJ with > wrapper. Instead of wrapping, my plan is to create a separate module with > Python and I am thinking of doing this in as my final year project also. > Kindly give me your suggestion. > As for as i know, no pure python module exists for iText PDF generator, where iText seems to be a standard one for PDF related works.There are some other modules for pdf work but might not be as standard as iText.Surely some senior programmers will suggest you better way to proceed further. > With anticipation, > > SanthoshVKumar. > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Yours, S.Selvam -------------- next part -------------- An HTML attachment was scrubbed... URL: From Tom.sully at gmx.com Mon Jul 27 04:58:48 2009 From: Tom.sully at gmx.com (Tom) Date: Mon, 27 Jul 2009 01:58:48 -0700 (PDT) Subject: Globalize all variables in function without knowing names. References: <5649dfb8-01a6-490d-bf30-b4a5206e644e@j21g2000yqe.googlegroups.com> <027cce66$0$5185$c3e8da3@news.astraweb.com> Message-ID: <50a401f0-b203-4dcd-baa9-22895a068f3b@r27g2000vbn.googlegroups.com> > Dictionaries are fundamental to Python and very useful. Not learning > about them before starting to write code is like not learning about the > accelerator pedal before starting to drive a car. Heh, I just looked at the actual tutorial for Dictionaries and I have to say they are very useful. Basically just arrays with strings(or any other data type, except lists) instead of numbers. I'll work on trying to integrate them into my language code as, like you said, user data... Then when a user requests a variable it just accessess the dictionary, that should work. Thanks! This is easier than I thought. > Have you done the tutorial? I did the first few sections, and then flipped back to it later on to find out what I needed to know as I coded applications. A bad idea, in retrospect, particilary as it slowed down my coding time alot. From hendrik at microcorp.co.za Mon Jul 27 05:05:06 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 27 Jul 2009 11:05:06 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: <200907271105.06692.hendrik@microcorp.co.za> On Sunday 26 July 2009 21:26:46 David Robinow wrote: > > I'm a mediocre programmer. Does this mean I should switch to PHP? I have searched, but I can find nothing about this mediocre language. Could you tell us more? - Hendrik From dickinsm at gmail.com Mon Jul 27 05:12:16 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Mon, 27 Jul 2009 02:12:16 -0700 (PDT) Subject: missing 'xor' Boolean operator References: Message-ID: <668280c4-6c52-4c2e-ab6f-9b3701399134@s15g2000yqs.googlegroups.com> On Jul 27, 1:53?am, "Delaney, Timothy (Tim)" wrote: > Mark Dickinson wrote: > >> Since the 'and' and 'or' already return objects (and objects > >> evaluate to true or false), then 'xor' should behave likewise, IMO. > >> I expect that would be the case if it were ever added to the > >> language. > > > I'm not so sure. ?Did you ever wonder why the any() and all() > > functions introduced in 2.5 return a boolean rather than returning > > one of their arguments? ?(I did, and I'm still not sure what the > > answer is.) > > Consider the case of any() and all() operating on an empty iterable. > What type should they return? > > It is impossible in the case of any() and all() to always return one of > the elements due to this edge case. Yes, of course; the alternative implementation I was thinking of was the one that I implemented eons ago for my own pre-2.5 code, where I defined any and all roughly as: any([x1, x2, x3, ...]) <-> False or x1 or x2 or x3 or ... all([x1, x2, x3, ...]) <-> True and x1 and x2 and x3 and ... At the time this seemed like the obvious choice, so I was a bit surprised when it was chosen to always return a bool instead in the official versions. Now that I'm older and wise^H^H^H^H, well, maybe just older, the pure bool version seems cleaner and less error-prone, even if it's mildly inconsistent with the behaviour of and and or. Mark From sjmachin at lexicon.net Mon Jul 27 05:25:19 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 27 Jul 2009 02:25:19 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX References: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> Message-ID: <635a7311-62e1-4da5-98ad-b62cfa150287@k30g2000yqf.googlegroups.com> On Jul 27, 5:11?pm, abhi wrote: > Hi, > ? ? I am facing a problem using PyArg_ParseTuple() in my C-API > extension. Here is a small repro of the function: > > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > { > ? ? ? ? SQLUSMALLINT param_no = 0; Sorry, my crystal ball is on the fritz. What is SQLUSMALLINT on Windows/Linux/AIX? What does printf("%d %d %d\n", sizeof(SQLUSMALLINT), sizeof(int), sizeof (long)); give you on each platform? > ? ? ? ? PyObject *py_obj = NULL; > > ? ? ? ? if (!PyArg_ParseTuple(args, "Oi", &py_obj, ¶m_no)){ > > ? ? ? ? ? ? ? ? return NULL; > ? ? ? ? } > ? ? ? ? printf("%d\n", param_no); > ? ? ? ? return NULL; > > } > > This function works fine and prints the correct value passed > (param_no) when I test it on Linux or Windows. However, it always > prints 0 (initial value defined in function) when I run it on AIX. Consider that 0 could have been plucked out of the air and has nothing to do with the initial value of param_no. Try initialising it to something distinctive, like 0xfedcba98. Use %08x format in the printf rather than %d. > > I think the problem is with "Oi" argument, if I replace that with "OO" > and specifically convert it integer/sqlsmallint. It works fine on AIX, > but I want to use "i" and not "O". > > Any ideas on what is the reason behind the problem and how to fix > this? I jump-started the crystal ball from the over-head tram wire and saw this: """SQLSMALLINT is 16 bits on all platforms, int/long is 32 bits on all platforms, first 2 are little-endian, AIX is bigendian, the "i" format expects SIGNED int /long, so param_no is getting (win:x, linux:x, aix:0) and the next 16 bits in memory are getting trashed with (win:0, linux:0, aix:x) (when x is your input smallish integer) and luckily this trashing didn't cause a crash. You can receive the result from the function in an int/long then have code to reject it if it won't fit into a SQLUSMALLINT, and cast it otherwise. If you don't care about overflow (NOT recommended), just use "H" format instead if "i" format""" Could be wrong, really shouldn't subject precision instruments to 415V DC, only OPs :-) N.B. to detect endianness : use print sys.byteorder HTH, John From jayshree06comp at gmail.com Mon Jul 27 05:37:47 2009 From: jayshree06comp at gmail.com (jayshree) Date: Mon, 27 Jul 2009 02:37:47 -0700 (PDT) Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: On Jul 27, 1:09?pm, Kushal Kumaran wrote: > On Mon, Jul 27, 2009 at 12:58 PM, jayshree wrote: > > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > > Please tell me how to open a file placed in any directory or in same > > directory. > > > After opening this file i want to use the contain (public key ) for > > encryption > > Does the code you've put into your message not read that file? ?If you > get an exception, copy-paste in the traceback message you get into > your mail. > > -- > kushal try: pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() except IOError: print "Error: can\'t find file or read data" else: print "reading from file successfully" >Still no error it gives .what to do? From m_tayseer82 at yahoo.com Mon Jul 27 05:39:30 2009 From: m_tayseer82 at yahoo.com (Mohammad Tayseer) Date: Mon, 27 Jul 2009 02:39:30 -0700 (PDT) Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: References: Message-ID: <468250.83203.qm@web31106.mail.mud.yahoo.com> You can generate binaries using py2exe, and you can create UI using Tkinter (which is very easy) or wxPython (which have GUI builders) Mohammad Tayseer http://spellcoder.com/blogs/tayseer ________________________________ From: Piet van Oostrum To: python-list at python.org Sent: Monday, July 27, 2009 11:18:20 AM Subject: Re: Looking for a dream language: sounds like Python to me. >>>>> Dotan Cohen (DC) wrote: >DC> Referring to this article: >DC> http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ >DC> The author, who is specifically looking for math-related functions, writes: >DC> """ >DC> The dream algorithm R&D tool would be similar to Matlab or Mathematica >DC> but could be compiled to fast, efficient binaries similar to ANSI C >DC> and would be available for all platforms. An integrated GUI builder >DC> similar to Visual Basic and integrated network support would be >DC> helpful. >DC> """ >DC> It looks to me like he is looking for Python. No fast, efficient binaries yet, and no integrated GUI builder. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Mon Jul 27 05:55:02 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 11:55:02 +0200 Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: <7d5brmF2a1alcU1@mid.uni-berlin.de> jayshree wrote: > On Jul 27, 1:09?pm, Kushal Kumaran > wrote: >> On Mon, Jul 27, 2009 at 12:58 PM, jayshree >> wrote: >> > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() >> >> > Please tell me how to open a file placed in any directory or in same >> > directory. >> >> > After opening this file i want to use the contain (public key ) for >> > encryption >> >> Does the code you've put into your message not read that file? ?If you >> get an exception, copy-paste in the traceback message you get into >> your mail. >> >> -- >> kushal > > > try: > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > except IOError: > print "Error: can\'t find file or read data" > else: > print "reading from file successfully" > >>Still no error it gives .what to do? Erm - so it doesn't give an error - which means you have successfully opened a file, and read it's contents. So what exactly is your problem? Diez From nogetfx at gmail.com Mon Jul 27 06:18:55 2009 From: nogetfx at gmail.com (Lars) Date: Mon, 27 Jul 2009 03:18:55 -0700 (PDT) Subject: Call function from another class Message-ID: <4b6b0e4b-b85b-43cf-a695-e3a722de01cc@e11g2000yqo.googlegroups.com> Hi I'm trying to make an simple image viewer in wxPython and rotate an image with a slider. The code at Pastebin is striped down at bit. The class Frame(wx.Frame) is the main window, the function "def CreateMenuBar" (l. 39) creates a menu, where the function "def onRotate (self,event):" (l. 43) is called. The slider appear in at 2nd window, class rotationSlider(wx.Frame). My problem is what to do, when the Okay button is pressed in the "rotationSlider" frame. How to get the slider value to my main window and how to call a function to do the rotation. I tried something with a global variable, but it would make no difference regarding the function problem. Amongst the other things I've tried is to bind the event of the 2nd/slider window closing (l. 46), but no. Python code at Pastebin: http://pastebin.com/m7c24ec34 So some help on Classes and etc., would be appreciated. /Lars From himanshu.garg at gmail.com Mon Jul 27 06:38:04 2009 From: himanshu.garg at gmail.com (++imanshu) Date: Mon, 27 Jul 2009 03:38:04 -0700 (PDT) Subject: _msi.Record object has no attribute 'GetString' Message-ID: <32e0b242-8635-40b5-a366-2c9967e64e1c@d4g2000yqa.googlegroups.com> The documentation (http://docs.python.org/library/msilib.html#record- objects) for msilib mentions the GetString() method on Record objects. However, the following snippet :- db = msilib.OpenDatabase(os.path.join(root, file), msilib.MSIDBOPEN_READONLY) view = db.OpenView('SELECT * FROM Property') view.Execute(None) r = view.Fetch() print dir(r) print r.GetString(0) gives me this error :- c:\>python msi.py ['ClearData', 'GetFieldCount', 'SetInteger', 'SetStream', 'SetString', __class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', __reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__'] Traceback (most recent call last): File "msi.py", line 18, in print r.GetString(0) AttributeError: '_msi.Record' object has no attribute 'GetString' Am I missing something? Thank You, Himanshu From deets at nospam.web.de Mon Jul 27 06:43:14 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 12:43:14 +0200 Subject: Call function from another class References: <4b6b0e4b-b85b-43cf-a695-e3a722de01cc@e11g2000yqo.googlegroups.com> Message-ID: <7d5em2F2anm94U1@mid.uni-berlin.de> Lars wrote: > Hi > I'm trying to make an simple image viewer in wxPython and rotate an > image with a slider. The code at Pastebin is striped down at bit. The > class Frame(wx.Frame) is the main window, the function "def > CreateMenuBar" (l. 39) creates a menu, where the function "def onRotate > (self,event):" (l. 43) is called. The slider appear in at 2nd window, > class rotationSlider(wx.Frame). > My problem is what to do, when the Okay button is pressed in the > "rotationSlider" frame. How to get the slider value to my main window > and how to call a function to do the rotation. I tried something with > a global variable, but it would make no difference regarding the > function problem. Amongst the other things I've tried is to bind the > event of the 2nd/slider window closing (l. 46), but no. > Python code at Pastebin: http://pastebin.com/m7c24ec34 > > So some help on Classes and etc., would be appreciated. If I'm not mistaken, in "doRotate" you should be able to refer to the to-be-closed dialog via self.frameRotate Now if you change the "sliderUpdate"-code to store that angle instead of just letting it fall out of scope, you could access that value through frameRotate: def sliderUpdate(self, event): angle=self.slider.GetValue() ROTATION_ANGLE=angle self.angle = angle self.Destroy() def doRotate(self,event): """Rotating image""" print self.frameRotate.angle Diez From abhigyan_agrawal at in.ibm.com Mon Jul 27 06:49:04 2009 From: abhigyan_agrawal at in.ibm.com (abhi) Date: Mon, 27 Jul 2009 03:49:04 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX References: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> <635a7311-62e1-4da5-98ad-b62cfa150287@k30g2000yqf.googlegroups.com> Message-ID: <43b1acda-b518-40ef-acde-145e28379775@r25g2000vbn.googlegroups.com> On Jul 27, 2:25?pm, John Machin wrote: > On Jul 27, 5:11?pm, abhi wrote: > > > Hi, > > ? ? I am facing a problem using PyArg_ParseTuple() in my C-API > > extension. Here is a small repro of the function: > > > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > > { > > ? ? ? ? SQLUSMALLINT param_no = 0; > > Sorry, my crystal ball is on the fritz. What is SQLUSMALLINT on > Windows/Linux/AIX? > What does > ? ? printf("%d %d %d\n", sizeof(SQLUSMALLINT), sizeof(int), sizeof > (long)); > give you on each platform? > > > ? ? ? ? PyObject *py_obj = NULL; > > > ? ? ? ? if (!PyArg_ParseTuple(args, "Oi", &py_obj, ¶m_no)){ > > > ? ? ? ? ? ? ? ? return NULL; > > ? ? ? ? } > > ? ? ? ? printf("%d\n", param_no); > > ? ? ? ? return NULL; > > > } > > > This function works fine and prints the correct value passed > > (param_no) when I test it on Linux or Windows. However, it always > > prints 0 (initial value defined in function) when I run it on AIX. > > Consider that 0 could have been plucked out of the air and has nothing > to do with the initial value of param_no. Try initialising it to > something distinctive, like 0xfedcba98. Use %08x format in the printf > rather than %d. > > > > > I think the problem is with "Oi" argument, if I replace that with "OO" > > and specifically convert it integer/sqlsmallint. It works fine on AIX, > > but I want to use "i" and not "O". > > > Any ideas on what is the reason behind the problem and how to fix > > this? > > I jump-started the crystal ball from the over-head tram wire and saw > this: """SQLSMALLINT is 16 bits on all platforms, int/long is 32 bits > on all platforms, first 2 are little-endian, AIX is bigendian, the "i" > format expects SIGNED int /long, so param_no is getting (win:x, > linux:x, aix:0) and the next 16 bits in memory are getting trashed > with (win:0, linux:0, aix:x) (when x is your input smallish integer) > and luckily this trashing didn't cause a crash. You can receive the > result from the function in an int/long then have code to reject it if > it won't fit into a SQLUSMALLINT, and cast it otherwise. If you don't > care about overflow (NOT recommended), just use "H" format instead if > "i" format""" > > Could be wrong, really shouldn't subject precision instruments to 415V > DC, only OPs :-) > > N.B. to detect endianness : use print sys.byteorder > > HTH, > John Hi, Thanks for the response. If I don't use SQLUSMALLINT and use int instead, then also I see the same problem. static PyObject *parsetuple_test(PyObject *self, PyObject *args) { int param_no = 0; .... .... } This also prints out 0 for AIX and actual value passed in other platforms. If I initialize param_no with some other value (e.g. 90), it prints out that value. - Abhigyan From piet at cs.uu.nl Mon Jul 27 07:08:27 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 13:08:27 +0200 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: >>>>> Paul Rubin (PR) wrote: >PR> Rob Knop writes: >>> Are there any python libraries that will take a public key in this >>> format and do RSA encoding on it? >PR> Try www.trevp.com/tlslite I have looked into tlslite and found no easy way to do this. Maybe I overlooked it. On the other hand with M2Crypto it isn't that hard. However, M2Crypto wants to load keys in PEM format rather than the DER format that the Java getEncoded() produces. It is not hard to convert this to PEM format: just Base64 encode it and put a header and trailer line around it. The conversion can be done with openssl or just on the fly in the Python code. Please note that the text to be encrypted must be smaller than the key size (at least 11 bytes smaller). You shouldn't encrypt large data with RSA anyway: it is too slow. Normally you would encrypt a session key with RSA and encrypt the data with the session key using a symmetric algorithms like AES. Here is an example script: If your OS doesn't have /dev/urandom you should probably seed OpenSSL's PRNG to be more secure. ------------------------------------------------------------------------ from M2Crypto import BIO, RSA pubkey = open("pubkey.der", 'rb').read() import base64 pubkey = base64.encodestring(pubkey) pubkey = '-----BEGIN PUBLIC KEY-----\n' + pubkey + '-----END PUBLIC KEY-----' bio = BIO.MemoryBuffer(pubkey) rsa = RSA.load_pub_key_bio(bio) plaintext = "This is my secret text." codetext = rsa.public_encrypt(plaintext, RSA.pkcs1_padding) with open("codetext", 'wb') as out: out.write(codetext) ------------------------------------------------------------------------ The following Java program decodes this with the corresponding private key: ------------------------------------------------------------------------ import java.security.*; import javax.crypto.*; import java.security.spec.*; import java.security.interfaces.*; import java.io.*; class Decrypt { public static void main(String[] args) { byte[] key = null; try { File file = new File("privkey.der"); FileInputStream keyfile = new FileInputStream(file); key = new byte[(int)file.length()]; DataInputStream dis= new DataInputStream(keyfile); dis.readFully(key); dis.close(); } catch(FileNotFoundException e) { System.out.println("Key file not found" + e); } catch (IOException e) { System.out.println("Can't read key file" + e); } byte[] codetext = null; try { File file = new File("codetext"); FileInputStream codefile = new FileInputStream(file); codetext = new byte[(int)file.length()]; DataInputStream dis = new DataInputStream(codefile); dis.readFully(codetext); dis.close(); } catch(FileNotFoundException e) { System.out.println("Code file not found" + e); } catch (IOException e) { System.out.println("Can't read code file" + e); } try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(key); RSAPrivateKey rsakey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec); Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); c.init(Cipher.DECRYPT_MODE, rsakey); byte[] decrypted = c.doFinal(codetext); System.out.println("Decrypted text:"); System.out.println(new String(decrypted)); } catch (NoSuchAlgorithmException e) { System.out.println("Wrong Algorithm " + e); } catch (InvalidKeyException e) { System.out.println("Invalid key " + e); } catch (IllegalBlockSizeException e) { System.out.println("Illegal block size" + e); } catch (NoSuchPaddingException e) { System.out.println("Illegal padding " + e); } catch (BadPaddingException e) { System.out.println("Bad padding " + e); } catch (InvalidKeySpecException e) { System.out.println("Invalid keyspec " + e); } } } ------------------------------------------------------------------------ -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From sean.kemplay at gmail.com Mon Jul 27 07:14:10 2009 From: sean.kemplay at gmail.com (Sean Kemplay) Date: Mon, 27 Jul 2009 04:14:10 -0700 (PDT) Subject: Free hosting for open source Python projects Message-ID: <97378723-7e67-415b-84bb-06d1675f2e2b@n11g2000yqb.googlegroups.com> Hi All, If anyone is looking for somewhere to host their Python project / modules etc. for free, please get in touch with me. There of course are some restraints on disk space and bandwidth but they should be sufficient for most projects. Your project must be open source. You will have access to both Django or Plone/Zope(shared). No PHP etc - this is a Python only host. Unfortunately I can't host everyone but if I can't host your particular project now, I may be able to at a later stage. Please contact me at "postmaster (at) pythonicity.co.uk". Our website is located at www.pythonicity.co.uk if you want to see what we do. Best Regards, Sean Kemplay From wilk at flibuste.net Mon Jul 27 07:16:15 2009 From: wilk at flibuste.net (William Dode) Date: 27 Jul 2009 11:16:15 GMT Subject: ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler References: <4a645b81$0$12974$426a74cc@news.free.fr> <29b7919a-ff4b-44eb-bad3-697274b66b6b@j32g2000yqh.googlegroups.com> <4a657a11$0$417$426a74cc@news.free.fr> <4e6df236-bbcd-4c1f-becf-3f23c0ba35bb@b15g2000yqd.googlegroups.com> <4a66fa20$0$11368$426a74cc@news.free.fr> Message-ID: <4a6d8c7f$0$19614$426a74cc@news.free.fr> On 27-07-2009, Bearophile wrote: > William Dode': >> I updated the script (python, c and java) with your unrolled version >> + somes litle thinks. > [...] >> c 1.85s >> gcj 2.15s >> java 2.8s >> python2.5 + psyco 3.1s >> unladen-2009Q2 145s (2m45) >> python2.5 254s (4m14s) >> python3.1 300s (5m) >> ironpython1.1.1 680s (11m20) > > Sorry for being late, I was away. > > In your last C version this code is useless because the C compiler is > able to perform such simple optimization by itself (but probably > Python isn't able, so if you want the code to be the similar in all > versions it may be better to keep it): I wanted so, specialy if it doesn't change a lot of the result (the difference is so small that i cannot see it)... ... > I have tried your latest C version using your compiler options, my > MinGW based on GCC 4.3.2 produces a crash at runtime. Maybe because of -msse2 ? > Using LLVM-GCC > it runs in 1.31 seconds. The D version is a bit less optimized than > your last C versions, yet using DMD it runs in 1.08-1.10 seconds. > Let's see if someone is able to write a C version faster than that D > code :-) > > Have you have compiled/read my D version? In the D version you may > have missed that I did use an extra trick: unsigned integers, so it > needs just two tests to see if a number is in the 0-5, 0-5 square :-) I didn't see, fine ! But the difference is also too small to see... > Note that Pyd, the Python-D bridge, may work with the latest DMD > version still (and it works if you use a bit older DMD compiler): > http://pyd.dsource.org/ I completly don't know anything about D... When i see the result of psyco or shedskin, i'm affraid i'll not look somewhere else soon ! However, i'd like to see a lisp implementation of this... bye -- William Dod? - http://flibuste.net Informaticien Ind?pendant From davea at ieee.org Mon Jul 27 07:21:44 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 07:21:44 -0400 Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 In-Reply-To: References: <4A69A82D.8090301@ieee.org> Message-ID: <4A6D8DC8.40304@ieee.org> (forwarding this message, as the reply was off-list) Kim Hansen wrote: > 2009/7/24 Dave Angel : > >> It's not a question of how much disk space there is, but how much virtual >> space 32 bits can address. 2**32 is about 4 gig, and Windows XP reserves >> about half of that for system use. Presumably a 64 bit OS would have a much >> larger limit. >> >> Years ago I worked on Sun Sparc system which had much more limited shared >> memory access, due to hardware limitations. So 2gig seems pretty good to >> me. >> >> There is supposed to be a way to tell the Windows OS to only use 1 gb of >> virtual space, leaving 3gb for application use. But there are some >> limitations, and I don't recall what they are. I believe it has to be done >> globally (probably in Boot.ini), rather than per process. And some things >> didn't work in that configuration. >> >> DaveA >> >> >> > Hi Dave, > > In the related post I did on the numpy discussions: > > http://article.gmane.org/gmane.comp.python.numeric.general/31748 > > another user was kind enough to run my test program on both 32 bit and > 64 bit machines. On the 64 bit machine, there was no such limit, very > much in line with what you wrote. Adding the /3GB option in boot.ini > did not increase the available memory as well. Apparently, Python > needs to have been compiled in a way, which makes it possible to take > advantage of that switch and that is either not the case or I did > something else wrong as well. > > I acknowledge the explanation concerning the address space available. > Being an ignorant of the inner details of the implementation of mmap, > it seems like somewhat an "implementation detail" to me that such an > address wall is hit. There may be some good arguments from a > programming point of view and it may be a relative high limit as > compared to other systems but it is certainly at the low side for my > application: I work with data files typically 200 GB in size > consisting of datapackets each having a fixed size frame and a > variable size payload. To handle these large files, I generate an > "index" file consisting of just the frames (which has all the metadata > I need for finding the payloads I am interested in) and "pointers" to > where in the large data file each payload begins. This index file can > be up to 1 GB in size and at times I need to have access to two of > those at the same time (and then i hit the address wall). I would > really really like to be able to access these index files in a > read-only manner as an array of records on a file for which I use > numpy.memmap (which wraps mmap.mmap) such that I can pick a single > element, extract, e.g., every thousand value of a specific field in > the record using the convenient indexing available in Python/numpy. > Now it seems like I have to resort to making my own encapsulation > layer, which seeks to the relevant place in the file, reads sections > as bytestrings into recarrays, etc. Well, I must just get on with > it... > > I think it would be worthwhile specifying this 32 bit OS limitation in > the documentation of mmap.mmap, as I doubt I am the only one being > surprised about this address space limitation. > > Cheers, > Kim > > I agree that some description of system limitations should be included in a system-specific document. There probably is one, I haven't looked recently. But I don't think it belongs in mmap documentation. Perhaps you still don't recognize what the limit is. 32 bits can only address 4 gigabytes of things as first-class addresses. So roughly the same limit that's on mmap is also on list, dict, bytearray, or anything else. If you had 20 lists taking 100 meg each, you would fill up memory. If you had 10 of them, you might have enough room for a 1gb mmap area. And your code takes up some of that space, as well as the Python interpreter, the standard library, and all the data structures that are normally ignored by the application developer. BTW, there is one difference between mmap and most of the other allocations. Most data is allocated out of the swapfile, while mmap is allocated from the specified file (unless you use -1 for fileno). Consequently, if the swapfile is already clogged with all the other running applications, you can still take your 1.8gb or whatever of your virtual space, when much less than that might be available for other kinds of allocations. Executables and dlls are also (mostly) mapped into memory just the same as mmap. So they tend not to take up much space from the swapfile. In fact, with planning, a DLL needn't take up any swapfile space (well, a few K is always needed, realistically).. But that's a linking issue for compiled languages. DaveA From mdekauwe at gmail.com Mon Jul 27 07:24:31 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 04:24:31 -0700 (PDT) Subject: quickly looping over a 2D array? Message-ID: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Hi, I am new to python and I was wondering if there was a way to speed up the way I index 2D arrays when I need to check two arrays simultaneously? My current implementations is (using numpy) something like the following... for i in range(numrows): for j in range(numcols): if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, j] * some_other_value array_1[i, j] = some_new_value Many Thanks, Martin From michael at stroeder.com Mon Jul 27 07:35:15 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Mon, 27 Jul 2009 13:35:15 +0200 Subject: RSA cryptography between Python and Java In-Reply-To: References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: Piet van Oostrum wrote: > Please note that the text to be encrypted must be smaller than the key > size (at least 11 bytes smaller). You shouldn't encrypt large data with > RSA anyway: it is too slow. Normally you would encrypt a session key > with RSA and encrypt the data with the session key using a symmetric > algorithms like AES. I'd use CMS (AKA PKCS#7) for encrypted/signed data with X.509 certs. One should not invent another message format. Ciao, Michael. -- Michael Str?der E-Mail: michael at stroeder.com http://www.stroeder.com From __peter__ at web.de Mon Jul 27 07:42:48 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 13:42:48 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Message-ID: Martin wrote: > I am new to python and I was wondering if there was a way to speed up > the way I index 2D arrays when I need to check two arrays > simultaneously? My current implementations is (using numpy) something > like the following... > > for i in range(numrows): > for j in range(numcols): > > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > j] * some_other_value > array_1[i, j] = some_new_value array_1[(array_1 == some_value) | (array_2 >= array_1 * some_other_value)] = some_new_value maybe? From nogetfx at gmail.com Mon Jul 27 07:48:30 2009 From: nogetfx at gmail.com (Lars) Date: Mon, 27 Jul 2009 04:48:30 -0700 (PDT) Subject: Call function from another class References: <4b6b0e4b-b85b-43cf-a695-e3a722de01cc@e11g2000yqo.googlegroups.com> <7d5em2F2anm94U1@mid.uni-berlin.de> Message-ID: > If I'm not mistaken, in "doRotate" you should be able to refer to the > to-be-closed dialog via > self.frameRotate > > Now if you change the "sliderUpdate"-code to store that angle instead of > just letting it fall out of scope, you could access that value through > frameRotate: > > def sliderUpdate(self, event): > ? ? ? ? angle=self.slider.GetValue() > ? ? ? ? ROTATION_ANGLE=angle > ? ? ? ? self.angle = angle > ? ? ? ? self.Destroy() > > def doRotate(self,event): > ? ? """Rotating image""" > ? ? print self.frameRotate.angle Thanks for replying. If Bind is used in "onRotate": def onRotate(self,event): """Rotate image""" self.frameRotate = rotationSlider(parent=None, id=-1) self.Bind(wx.EVT_CLOSE,self.doRotate, self.frameRotate.Destroy) self.frameRotate.Show() Then i get: Traceback (most recent call last): File "viewer_sep.py", line 136, in onRotate self.Bind(wx.EVT_CLOSE,self.doRotate, self.frameRotate.Destroy) File "/usr/lib/python2.5/site-packages/wx-2.6-gtk2-unicode/wx/ _core.py", line 3629, in Bind id = source.GetId() AttributeError: 'function' object has no attribute 'GetId' My experience with ID is setting it to "-1". So I'm not sure if it is a wrong use of Bind or an wxPython error. /Lars From Slaunger at gmail.com Mon Jul 27 07:50:52 2009 From: Slaunger at gmail.com (Slaunger) Date: Mon, 27 Jul 2009 04:50:52 -0700 (PDT) Subject: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4 References: <4A69A82D.8090301@ieee.org> Message-ID: On 27 Jul., 13:21, Dave Angel wrote: > (forwarding this message, as the reply was off-list) > > > > Kim Hansen wrote: > > 2009/7/24 Dave Angel : > > >> It's not a question of how much disk space there is, but how much virtual > >> space 32 bits can address. ?2**32 is about 4 gig, and Windows XP reserves > >> about half of that for system use. ?Presumably a 64 bit OS would have a much > >> larger limit. > > >> Years ago I worked on Sun Sparc system which had much more limited shared > >> memory access, due to hardware limitations. ?So 2gig seems pretty good to > >> me. > > >> There is supposed to be a way to tell the Windows OS to only use 1 gb of > >> virtual space, leaving 3gb for application use. ?But there are some > >> limitations, and I don't recall what they are. ?I believe it has to be done > >> globally (probably in Boot.ini), rather than per process. ?And some things > >> didn't work in that configuration. > > >> DaveA > > > Hi Dave, > > > In the related post I did on the numpy discussions: > > >http://article.gmane.org/gmane.comp.python.numeric.general/31748 > > > another user was kind enough to run my test program on both 32 bit and > > 64 bit machines. On the 64 bit machine, there was no such limit, very > > much in line with what you wrote. Adding the /3GB option in boot.ini > > did not increase the available memory as well. Apparently, Python > > needs to have been compiled in a way, which makes it possible to take > > advantage of that switch and that is either not the case or I did > > something else wrong as well. > > > I acknowledge the explanation concerning the address space available. > > Being an ignorant of the inner details of the implementation of mmap, > > it seems like somewhat an "implementation detail" to me that such an > > address wall is hit. There may be some good arguments from a > > programming point of view and it may be a relative high limit as > > compared to other systems but it is certainly at the low side for my > > application: I work with data files typically 200 GB in size > > consisting of datapackets each having a fixed size frame and a > > variable size payload. To handle these large files, I generate an > > "index" file consisting of just the frames (which has all the metadata > > I need for finding the payloads I am interested in) and "pointers" to > > where in the large data file each payload begins. This index file can > > be up to 1 GB in size and at times I need to have access to two of > > those at the same time (and then i hit the address wall). I would > > really really like to be able to access these index files in a > > read-only manner as an array of records on a file for which I use > > numpy.memmap (which wraps mmap.mmap) such that I can pick a single > > element, extract, e.g., every thousand value of a specific field in > > the record using the convenient indexing available in Python/numpy. > > Now it seems like I have to resort to making my own encapsulation > > layer, which seeks to the relevant place in the file, reads sections > > as bytestrings into recarrays, etc. Well, I must just get on with > > it... > > > I think it would be worthwhile specifying this 32 bit OS limitation in > > the documentation of mmap.mmap, as I doubt I am the only one being > > surprised about this address space limitation. > > > Cheers, > > Kim > > I agree that some description of system limitations should be included > in a system-specific document. ?There probably is one, I haven't looked > recently. ?But I don't think it belongs in mmap documentation. > > Perhaps you still don't recognize what the limit is. ?32 bits can only > address 4 gigabytes of things as first-class addresses. ?So roughly the > same limit that's on mmap is also on list, dict, bytearray, or anything > else. ?If you had 20 lists taking 100 meg each, you would fill up > memory. ?If you had 10 of them, you might have enough room for a 1gb > mmap area. ?And your code takes up some of that space, as well as the > Python interpreter, the standard library, and all the data structures > that are normally ignored by the application developer. > > BTW, ?there is one difference between mmap and most of the other > allocations. ?Most data is allocated out of the swapfile, while mmap is > allocated from the specified file (unless you use -1 for fileno). ? > Consequently, if the swapfile is already clogged with all the other > running applications, you can still take your 1.8gb or whatever of your > virtual space, when much less than that might be available for other > kinds of allocations. > > Executables and dlls are also (mostly) mapped into memory just the same > as mmap. ?So they tend not to take up much space from the swapfile. ?In > fact, with planning, a DLL needn't take up any swapfile space (well, a > few K is always needed, realistically).. ?But that's a linking issue for > compiled languages. > > DaveA- Skjul tekst i anf?rselstegn - > > - Vis tekst i anf?rselstegn - I do understand the 2 GB address space limitation. However, I think I have found a solution to my original numpy.memmap problem (which spun off to this problem), and that is PyTables, where I can address 2^64 data on a 32 bit machine using hd5 files and thus circumventing the "implementation detail" of the intermedia 2^32 memory address problem in the numpy.memmap/mmap.mmap implementation. http://www.pytables.org/moin I just watched the first tutorial video, and that seems like just what I am after (if it works as well in practise at it appears to do). http://showmedo.com/videos/video?name=1780000&fromSeriesID=178 Cheers, Kim From piet at cs.uu.nl Mon Jul 27 07:51:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 13:51:34 +0200 Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() By the way, a PEM file is a text file, no reason to open it in binary mode. Just replace the 'rb' with 'r' or leave it out. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Mon Jul 27 08:19:49 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 14:19:49 +0200 Subject: RSA cryptography between Python and Java References: <7xocr8si30.fsf@ruckus.brouhaha.com> Message-ID: >>>>> Michael Str?der (MS) wrote: >MS> Piet van Oostrum wrote: >>> Please note that the text to be encrypted must be smaller than the key >>> size (at least 11 bytes smaller). You shouldn't encrypt large data with >>> RSA anyway: it is too slow. Normally you would encrypt a session key >>> with RSA and encrypt the data with the session key using a symmetric >>> algorithms like AES. >MS> I'd use CMS (AKA PKCS#7) for encrypted/signed data with X.509 certs. One >MS> should not invent another message format. Yes but the original question was not about X.509 certs. Just the RSA public key. I don't know what the OP wants to do with it. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From drobinow at gmail.com Mon Jul 27 08:20:48 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 27 Jul 2009 07:20:48 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4A6CC3BE.6060806@mrabarnett.plus.com> References: <19d4rlsscvrgw.xu0laoyvg0$.dlg@40tude.net> <361bee02-b7aa-4475-968b-db8d88deb8d8@x25g2000prf.googlegroups.com> <4a6905b0$0$27110$a729d347@news.telepac.pt> <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> <4A6CC3BE.6060806@mrabarnett.plus.com> Message-ID: <4eb0089f0907270520n782503b1ndc34bcb1cf14a47e@mail.gmail.com> On 7/26/09, MRAB wrote: > David Robinow wrote: > > > This doesn't mean they're on the same level - in fact, if you read > carefully > > > you'll see my original post said as much: python attracted average > > > programmers; php attracted mediocre programmers and even some > > > non-programmers, which means that php is clearly a lesser language than > > > python. > > I'm a mediocre programmer. Does this mean I should switch to PHP? > If you're a mediocre programmer who knows you're mediocre, but wants to > improve and is willing to learn, then stick with Python. You'll get > better. :-) I doubt it. I remember when Guido moved to the U.S. and I've even used stdwin. I've actually gotten worse over the years. Old age does that. Nevertheless, I question the idea that a language that is easy to use (I don't know if PHP qualifies) is somehow inferior. From mdekauwe at gmail.com Mon Jul 27 08:27:11 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 05:27:11 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Message-ID: <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > I am new to python and I was wondering if there was a way to speed up > > the way I index 2D arrays when I need to check two arrays > > simultaneously? My current implementations is (using numpy) something > > like the following... > > > for i in range(numrows): > > for j in range(numcols): > > > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > > j] * some_other_value > > array_1[i, j] = some_new_value > > array_1[(array_1 == some_value) | (array_2 >= array_1 * some_other_value)] = > some_new_value > > maybe? So I tried... band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 which led to ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() so not sure that works? From rlichlighter at gmail.com Mon Jul 27 08:29:23 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 05:29:23 -0700 (PDT) Subject: Convert raw binary file to ascii Message-ID: I have a memory dump from a machine I am trying to analyze. I can view the file in a hex editor to see text strings in the binary code. I don't see a way to save these ascii representations of the binary, so I went digging into Python to see if there were any modules to help. I found one I think might do what I want it to do - the binascii module. Can anyone describe to me how to convert a raw binary file to an ascii file using this module. I've tried? Boy, I've tried. Am I correct in assuming I can get the converted binary to ascii text I see in a hex editor using this module? I'm new to this forensics thing and it's quite possible I am mixing technical terms. I am not new to Python, however. Thanks for your help. From electriclightheads at gmail.com Mon Jul 27 08:29:58 2009 From: electriclightheads at gmail.com ('2+) Date: Mon, 27 Jul 2009 21:29:58 +0900 Subject: wave.setparams((2, 2, 44100, 44100 * 2 * 10, "NONE", "not compressed")) became more than 30secs Message-ID: <1078018b0907270529l676fdb75k4c1f42ef0a2e7d0a@mail.gmail.com> thanx to rob .. who gave me an example of how to use the WAVE lib now am on my way to use it as simple as i can i wrote an oil.py the instance of which will behave like an oscillator which constantly generates 7 different wave forms (non fixed) i thought this code might produce 10secs of wave file but the result was longer than 30secs was testing if i could use the WAVE lib withount taking advantage of StringIO nor struct but maybe am doing totally wrong? --8<--- import oil import wave a = oil.Sa() w = wave.open("current.wav", "w") w.setparams((2, 2, 44100, 44100 * 2 * 10, "NONE", "not compressed")) for gas in range(44100 * 10): a.breath() r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) w.writeframes(hex(r)) w.writeframes(hex(l)) w.close() -- SaRiGaMa's Oil Vending Orchestra is podcasting: http://sarigama.namaste.jp/podcast/rss.xml and supplying oil.py for free: http://oilpy.blogspot.com/ From benjamin.kaplan at case.edu Mon Jul 27 08:41:23 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 27 Jul 2009 08:41:23 -0400 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: On Sun, Jul 26, 2009 at 2:24 PM, John Nagle wrote: > Beni Cherniavsky wrote: >> >> On Jul 22, 9:36 am, Hendrik van Rooyen >> wrote: >>> >>> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >>> >>>> My guess is that it was probably for optimization reasons long ago. >>>> I've never heard a *good* reason why Python needs both. >>> >>> The good reason is the immutability, which lets you use >>> a tuple as a dict key. >> >> On Jul 22, 9:36 am, Hendrik van Rooyen >> wrote: >>> >>> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >>> >>>> My guess is that it was probably for optimization reasons long ago. >>>> I've never heard a *good* reason why Python needs both. >>> >>> The good reason is the immutability, which lets you use >>> a tuple as a dict key. >>> >> The *technical* reason is immutability for dict keys. >> Dict could allow mutable objects as keys by comparing *by value*, >> making a copy on insertion and hashing the current value on lookup. >> Prior art: the 2.3 sets module allows mutable Sets as elements in >> Sets, by making ImmutableSet copies on insertion, and hashing Sets as >> if they are temporarily immutable on lookup. >> >> This inspired PEP 351 and ambitious proposals to expand the approach >> to all Python with a copy-on-write scheme. ?But these ideas were >> rejected, and the 2.4 builtin sets only allow frozenset elements. >> Half the reason is technical: copy-on-write and harder than it sounds, >> and without it you pay a performance price. >> >> But the deeper reason is style: immutable types are convenient! >> The allow a pure-functional style of code, which can be simpler. >> Of course, sometimes an imperative style is simpler. ?Depends on the >> problem. > > ? ?An interesting issue is Python objects, which are always mutable. > A "dict" of Python objects is allowed, but doesn't consider > the contents of the objects, just their identity (address). Only > built-in types are immutable; one cannot create a class of immutable > objects. > So things like "frozenset" had to be built into the language. > A tuple is really a frozen list. ?Arguably, frozen objects > should have been a general concept. ?Conceptually, they're > simple - once "__init__" has run, there can be no more changes > to fields of the object. > > ? ?Compare the C++ approach, where you can have a "map" of > any object that defines the "<" operator. ?Python has > "__cmp__" for objects, but built-in dictionaries don't use it. > Of course, one could create a "map" class in Python that > uses "__cmp__", which would allow dictionary-type operations > on arbitrary objects. Python dictionaries are hash maps so they use hashes, not comparisons, to store objects. By default the hash is equal to the object's identity but all you need to do to change it is define your own __hash__ method. If you were to make a C++ hash map, it wouldn't use comparisons either. > > ? ?There are some interesting uses for immutable objects in > multithreaded programs. ?They can be shared between threads > without locking. ?If it weren't for the infamous Global > Interpreter Lock, which basically limits Python programs to using > one CPU at a time, Python would have to deal with locking > in a more advanced way. ?A system where immutable objects > can be shared and passed between threads, and mutable objects > must be "synchronized" (in the Java sense) to be shared would > be useful. > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?John Nagle > -- > http://mail.python.org/mailman/listinfo/python-list > From rashidsdpk at gmail.com Mon Jul 27 08:45:33 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Mon, 27 Jul 2009 05:45:33 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10.more http://0nanotechnology0.blogspot.com/ From __peter__ at web.de Mon Jul 27 08:46:54 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 14:46:54 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> Message-ID: Martin wrote: > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: >> Martin wrote: >> > I am new to python and I was wondering if there was a way to speed up >> > the way I index 2D arrays when I need to check two arrays >> > simultaneously? My current implementations is (using numpy) something >> > like the following... >> >> > for i in range(numrows): >> > for j in range(numcols): >> >> > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, >> > j] * some_other_value >> > array_1[i, j] = some_new_value >> >> array_1[(array_1 == some_value) | (array_2 >= array_1 * >> some_other_value)] = some_new_value >> >> maybe? > > So I tried... > > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > which led to > > ValueError: The truth value of an array with more than one element is > ambiguous. Use a.any() or a.all() > > so not sure that works? Copy and paste -- or replace "or" with "|". From mdekauwe at gmail.com Mon Jul 27 08:55:15 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 05:55:15 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> Message-ID: <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> On Jul 27, 1:46 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: > >> Martin wrote: > >> > I am new to python and I was wondering if there was a way to speed up > >> > the way I index 2D arrays when I need to check two arrays > >> > simultaneously? My current implementations is (using numpy) something > >> > like the following... > > >> > for i in range(numrows): > >> > for j in range(numcols): > > >> > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > >> > j] * some_other_value > >> > array_1[i, j] = some_new_value > > >> array_1[(array_1 == some_value) | (array_2 >= array_1 * > >> some_other_value)] = some_new_value > > >> maybe? > > > So I tried... > > > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > > which led to > > > ValueError: The truth value of an array with more than one element is > > ambiguous. Use a.any() or a.all() > > > so not sure that works? > > Copy and paste -- or replace "or" with "|". apologies - I mistook that for a type for "or" I now get the following error... ValueError: shape mismatch: objects cannot be broadcast to a single shape From __peter__ at web.de Mon Jul 27 09:06:17 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 15:06:17 +0200 Subject: Convert raw binary file to ascii References: Message-ID: r2 wrote: > I have a memory dump from a machine I am trying to analyze. I can view > the file in a hex editor to see text strings in the binary code. I > don't see a way to save these ascii representations of the binary, so > I went digging into Python to see if there were any modules to help. > > I found one I think might do what I want it to do - the binascii > module. Can anyone describe to me how to convert a raw binary file to > an ascii file using this module. I've tried? Boy, I've tried. That won't work because a text editor doesn't need any help to convert the bytes into characters. If it expects ascii it just will be puzzled by bytes that are not valid ascii. Also, it will happily display byte sequences that are valid ascii, but that you as a user will see as gibberish because they were meant to be binary data by the program that wrote them. > Am I correct in assuming I can get the converted binary to ascii text > I see in a hex editor using this module? I'm new to this forensics > thing and it's quite possible I am mixing technical terms. I am not > new to Python, however. Thanks for your help. Unix has the "strings" commandline tool to extract text from a binary. Get hold of a copy of the MinGW tools if you are on windows. Peter From nemelis at gmail.com Mon Jul 27 09:12:39 2009 From: nemelis at gmail.com (Harry Ebbers) Date: Mon, 27 Jul 2009 06:12:39 -0700 (PDT) Subject: Testoob: How do you use testoob.collector_from_globals / collector_from_modules? Message-ID: <1a8582cc-a7fc-4b1e-b1b2-d23e20ad6022@18g2000yqa.googlegroups.com> Hi, For a project I'm creating unittests using testoob. When all tests are in a single file there is no problem if __name__ == '__main__': testoob.main() does the trick as usual. But for a number of python-applications they have asked me to group unittests in different files, based on common functionality (e.g. GUI, main-application, etcetera). Ican get it to work by importing all unittestclasses in the following way and than use the normal if __name__ == '__main__' construction (from GUI import GUItests from APP import APPtests if __name__ == '__main__': testoob.main()) But looking at the future I would like to use testsuites. On the internet I found the functions testoob.collector_from_globals and collector_from_modules which one can use to 'easily' create testsuites which can be used with testoob. But no examples on how to use them can be found. Can anybody supply me with a working example? TIA Harry Ebbers From __peter__ at web.de Mon Jul 27 09:17:58 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 15:17:58 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> Message-ID: Martin wrote: > On Jul 27, 1:46 pm, Peter Otten <__pete... at web.de> wrote: >> Martin wrote: >> > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: >> >> Martin wrote: >> >> > I am new to python and I was wondering if there was a way to speed >> >> > up the way I index 2D arrays when I need to check two arrays >> >> > simultaneously? My current implementations is (using numpy) >> >> > something like the following... >> >> >> > for i in range(numrows): >> >> > for j in range(numcols): >> >> >> > if array_1[i, j] == some_value or array_2[i, j] >= >> >> > array_1[i, >> >> > j] * some_other_value >> >> > array_1[i, j] = some_new_value >> >> >> array_1[(array_1 == some_value) | (array_2 >= array_1 * >> >> some_other_value)] = some_new_value >> >> >> maybe? >> >> > So I tried... >> >> > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 >> >> > which led to >> >> > ValueError: The truth value of an array with more than one element is >> > ambiguous. Use a.any() or a.all() >> >> > so not sure that works? >> >> Copy and paste -- or replace "or" with "|". > > apologies - I mistook that for a type for "or" > > I now get the following error... > > ValueError: shape mismatch: objects cannot be broadcast to a single > shape It seems array_1 and array_2 have a -- dada! -- different shape. Assuming array_1 is the smaller one: numrows, numcols = array_1.shape array_1[(array_1 == some_value) | (array_2[:numrows,:numcols] >= array_1 * some_other_value)] = some_new_value There may be other options, but I'm not a numpy expert. Peter From constant.beta at gmail.com Mon Jul 27 09:46:01 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Mon, 27 Jul 2009 06:46:01 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> Message-ID: <1c8ae01e-2e9c-497c-9f8d-408f56f9c0bd@g31g2000yqc.googlegroups.com> On Jul 27, 1:56 am, a... at pythoncraft.com (Aahz) wrote: > >> Upon a cursory look, after a generator 'gen' is exhausted (meaning > >> gen.next() has raised StopIteration), it seems that gen.gi_frame will be > >> None. > > >Only in Python 2.5 or higher though. I need to support Python 2.3 and > >2.4 as well, sorry for not making that clear in the original post. > > Are you sure? It appears to work in Python 2.4; I don't have time to > check 2.3. No, it does not work in Python 2.4. gi_frame can be None only in Python 2.5 and higher. Via "What?s New in Python 2.5" (http://docs.python.org/whatsnew/ 2.5.html): """ Another even more esoteric effect of this change: previously, the gi_frame attribute of a generator was always a frame object. It?s now possible for gi_frame to be None once the generator has been exhausted. """ Cheers, mk From sjmachin at lexicon.net Mon Jul 27 09:57:26 2009 From: sjmachin at lexicon.net (John Machin) Date: Mon, 27 Jul 2009 06:57:26 -0700 (PDT) Subject: Problem in PyArg_ParseTuple on python 2.5.2 with AIX References: <370cfa30-697f-4a82-a577-1eb951424f5a@e11g2000yqo.googlegroups.com> <635a7311-62e1-4da5-98ad-b62cfa150287@k30g2000yqf.googlegroups.com> <43b1acda-b518-40ef-acde-145e28379775@r25g2000vbn.googlegroups.com> Message-ID: <281196a8-c392-4818-b2fb-6b60c84aba2a@j21g2000vbn.googlegroups.com> On Jul 27, 8:49?pm, abhi wrote: > On Jul 27, 2:25?pm, John Machin wrote: > > > > > On Jul 27, 5:11?pm, abhi wrote: > > > > Hi, > > > ? ? I am facing a problem using PyArg_ParseTuple() in my C-API > > > extension. Here is a small repro of the function: > > > > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > > > { > > > ? ? ? ? SQLUSMALLINT param_no = 0; > > > Sorry, my crystal ball is on the fritz. What is SQLUSMALLINT on > > Windows/Linux/AIX? > > What does > > ? ? printf("%d %d %d\n", sizeof(SQLUSMALLINT), sizeof(int), sizeof > > (long)); > > give you on each platform? And the answer was: what? > ? ? Thanks for the response. If I don't use SQLUSMALLINT and use int > instead, then also I see the same problem. > static PyObject *parsetuple_test(PyObject *self, PyObject *args) > ?{ > ? ? ? ? int param_no = 0; > .... > ....} > > This also prints out 0 for AIX and actual value passed in other > platforms. If I initialize param_no with some other value (e.g. 90), > it prints out that value. Please run the following on AIX with Python 2.5: import sys print sys.byteorder, sys.maxint print sys.version and tell us the output. Does the C compiler that you are using for the extension correspond with whatever is mentioned in sys.version about the C compiler used to compile Python 2.5? C code: Please add/change as follows: int dummy1 = 0xaaaabbbb; int param_no = 0xccccdddd; int dummy2 = 0xeeeeffff; ... printf("%08x %08x %08x\n", dummy1, param_no, dummy2); printf("%d %d\n", sizeof(int), sizeof(long)); Try this on AIX (a) with no optimisation (b) whatever O level you have been using. Try it with (i) small positive input (i) integer > 65535 Take a copy of your extension and cut out everything that is not part of executing this function i.e. apart from this function (with the dummies and printfs) you should have only a few includes, a table defining just 1 callable function, and the minimal module initialisation code. Check that this cut down version works on Windows and Linux and still fails on AIX. Then try it without the first ("O") arg. If it still fails on AIX, lose the first arg. Publish the minimal failing code, the instructions to build the .so, and a transcript of the Python interactive session showing calling the function and the results. Cheers, John From mdekauwe at gmail.com Mon Jul 27 10:01:18 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 07:01:18 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> Message-ID: <8b78b47f-799e-4d37-bc28-d1ed349362c9@k6g2000yqn.googlegroups.com> On Jul 27, 2:17 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > On Jul 27, 1:46 pm, Peter Otten <__pete... at web.de> wrote: > >> Martin wrote: > >> > On Jul 27, 12:42 pm, Peter Otten <__pete... at web.de> wrote: > >> >> Martin wrote: > >> >> > I am new to python and I was wondering if there was a way to speed > >> >> > up the way I index 2D arrays when I need to check two arrays > >> >> > simultaneously? My current implementations is (using numpy) > >> >> > something like the following... > > >> >> > for i in range(numrows): > >> >> > for j in range(numcols): > > >> >> > if array_1[i, j] == some_value or array_2[i, j] >= > >> >> > array_1[i, > >> >> > j] * some_other_value > >> >> > array_1[i, j] = some_new_value > > >> >> array_1[(array_1 == some_value) | (array_2 >= array_1 * > >> >> some_other_value)] = some_new_value > > >> >> maybe? > > >> > So I tried... > > >> > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > >> > which led to > > >> > ValueError: The truth value of an array with more than one element is > >> > ambiguous. Use a.any() or a.all() > > >> > so not sure that works? > > >> Copy and paste -- or replace "or" with "|". > > > apologies - I mistook that for a type for "or" > > > I now get the following error... > > > ValueError: shape mismatch: objects cannot be broadcast to a single > > shape > > It seems array_1 and array_2 have a -- dada! -- different shape. > Assuming array_1 is the smaller one: > > numrows, numcols = array_1.shape > array_1[(array_1 == some_value) | (array_2[:numrows,:numcols] >= array_1 * > some_other_value)] = some_new_value > > There may be other options, but I'm not a numpy expert. > > Peter My mistake - the incorrect size in the arrays was my error. The statement works now, but it doesn't give the same results as my original logic, strangely!? in my logic: data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') for i in range(numrows): for j in range(numcols): if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: data[i,j] = 0 else: data[i,j] = 1 to do the same with what you suggested... data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') data[(band3 == 255) | (band6 >= band3 * factor)] = 0 Thanks From santhosh.vkumar at gmail.com Mon Jul 27 10:04:48 2009 From: santhosh.vkumar at gmail.com (santhoshvkumar) Date: Mon, 27 Jul 2009 07:04:48 -0700 (PDT) Subject: iText for Python Message-ID: <505f2bc1-cbfb-49a1-a7ef-0b6c357750a7@q35g2000vbi.googlegroups.com> Hi all, One of my cousin suggested me to do a IText PDF converter for python. Actually I heard that there is no separate IText converter either we have to go for jython or GCJ with wrapper. Instead of wrapping, my plan is to create a separate module with Python and I am thinking of doing this in as my final year project also. Kindly give me your suggestion. With anticipation, SanthoshVKumar. From jeanmichel at sequans.com Mon Jul 27 10:05:11 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 27 Jul 2009 16:05:11 +0200 Subject: Override a method but inherit the docstring In-Reply-To: <87r5wggm0y.fsf@benfinney.id.au> References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <4A6DB417.5080601@sequans.com> Ben Finney wrote: > Howdy all, > > The following is a common idiom:: > > class FooGonk(object): > def frobnicate(self): > """ Frobnicate this gonk. """ > basic_implementation(self.wobble) > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > the method, the original docstring is not associated with it. > > Ideally there would be a way to specify that the docstring should be > inherited. The best I can come up with is:: > > class BarGonk(FooGonk): > def frobnicate(self): > special_implementation(self.warble) > frobnicate.__doc__ = FooGonk.frobnicate.__doc__ > > but that violates DRY (the association between BarGonk and FooGonk is > being repeated), puts the docstring assignment awkwardly after the end > of the method instead of at the beginning where docstrings normally go, > and reads poorly besides. > > What is the most Pythonic, DRY-adherent, and preferably least-ugly > approach to override a method, but have the same docstring on both > methods? > > I am using epydoc and if the docstring is present only in the baseclass method, it will repeat the docstring for the child methods. So basically, there's nothing to do. I've also tried within the python interpreter, and it can perfectly solve docstring inheritance. So why would you explicitly assign docstring to child methods ? JM From invalid at invalid Mon Jul 27 10:11:21 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 27 Jul 2009 09:11:21 -0500 Subject: Convert raw binary file to ascii References: Message-ID: On 2009-07-27, r2 wrote: > I have a memory dump from a machine I am trying to analyze. I can view > the file in a hex editor to see text strings in the binary code. I > don't see a way to save these ascii representations of the binary, $ strings memdump.binary >memdump.strings $ hexdump -C memdump.binary >memdump.hex+ascii -- Grant Edwards grante Yow! I'm ZIPPY the PINHEAD at and I'm totally committed visi.com to the festive mode. From jeanmichel at sequans.com Mon Jul 27 10:17:13 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 27 Jul 2009 16:17:13 +0200 Subject: How to comment constant values? In-Reply-To: <027cd65b$0$5185$c3e8da3@news.astraweb.com> References: <200907270749.58845.devent@deventm.org> <7d44ndF29p3p7U2@mid.uni-berlin.de> <027cd65b$0$5185$c3e8da3@news.astraweb.com> Message-ID: <4A6DB6E9.3020308@sequans.com> Steven D'Aprano wrote: > On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote: > > >>> Only modules, classes, and functions/methods can have docstrings >>> associated with them. >>> For anything else, you have to use comments; or you can mention them in >>> the docstrings of related things. >>> >> While this is technically true, writing docstrings to constants (module >> or classlevel) works when one uses tools such as epydoc to generate >> documentation. >> > > I've never used epydoc, so I'm not sure what you mean. Presumably it uses > source code analysis to detect: > > CONSTANT = 42 > """This is a constant.""" > > even though the string is ignored by the compiler. > > Is that correct? > > > Yes, and because it is perfectly ignored by the compiler there's no harm using this feature. I would add that even if you're not using epydoc, having a way top discriminate comments from documentation is recommended, their purpose are definitely not the same. JM From piet at cs.uu.nl Mon Jul 27 10:23:04 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 27 Jul 2009 16:23:04 +0200 Subject: python function for retrieving key and encryption References: <19aa84c7-2268-41c4-809e-88e6dabd656f@h18g2000yqj.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >j> import M2Crypto >j> from M2Crypto import RSA,SSL >j> def encrypt(): >j> pk = open('my_key.public.pem', 'rb').read() >j> rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub >j> object. >j> plaintext = 4545479545655576767767686688782344 >j> msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding) >j> print msg; >j> encrypt() >j> This is code i am refering. >j> The Problem is coming with .pem file. You never tell what the problem is. Which error message do you get? Why can't you just copy the error messages in your post instead of letting us guess what is happening? There are indeed problems with the code above. One warning: If you are going to write cryptographic code while you really don't understand the subject enough there is a big chance that your programs will have security problems. One more question: Do you have now a valid my_key.public.pem file? And is it in the same directory where you run this program? Here are the problems in your code (and there may be more!) import M2Crypto from M2Crypto import RSA,SSL You never use the imports from the line above so you can leave it out. SSL isn't used at all, and RSA is used as M2Crypto.RSA so it uses the first import. Cleanliness is next to godliness. def encrypt(): pk = open('my_key.public.pem', 'rb').read() rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub object. load_pub_key requires a file name, not the contents of the file. So use rsa = M2Crypto.RSA.load_pub_key('my_key.public.pem') and leave the open line out. plaintext = 4545479545655576767767686688782344 That's not text, it is a number. rsa.public_encrypt will not accept a number. So you would have to use plaintext = "4545479545655576767767686688782344" or convert the number to a byte array. msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding) print msg; encrypt() The above line should be shifted to the left. It is not part of the function otherwise you get an endless recursion. And to answer another question from stackoverflow.com: RSA.pkcs1_padding is a good parameter, if the decryption side will also use it. See also my posting on the subject `RSA cryptography between Python and Java'. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From skip at pobox.com Mon Jul 27 10:26:42 2009 From: skip at pobox.com (skip at pobox.com) Date: Mon, 27 Jul 2009 09:26:42 -0500 (CDT) Subject: Wrapping prstat on Solaris Message-ID: <20090727142642.947081188353@montanaro.dyndns.org> At work we currently use top to monitor ongoing system utilization on our Solaris systems. As time has moved on though, use of top has become problematic. Our admins want us to switch to prstat, Sun's top-like command. It works fine however doesn't emit a timestamp at each display interval, so it's essentially impossible looking at a prstat output file to determine when the particular "screen" was emitted. If figured, "No problem. I'll just write a little wrapper." Alas, that is proving harder than I thought. I can run prstat directing output to a file and it seems to blast out a block of lines every N seconds, but when run from inside a Python script I can't seem to make the damn thing not massively buffer its output. Accordingly, my script doesn't really see the output as its emitted, so the timestamp line it prepends to a block of output is off. I'm currently using subprocess.Popen like so: os.environ["TERM"] = "dumb" cmd = "prstat -c %s" % " ".join(sys.argv[1:]) pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout I've tried these other variants as well: * prefacing the prstat command with unbuffer (the tcl/expect thingamabob) * explicitly redirect the prstat output to /dev/stdout * setting bufsize to 0 * used os.popen instead of Subprocess.Popen Nothing seems to help. I always seem to see about 30 seconds of data at once (I'm currently using a 5-second interval and 10 lines of output). I would have expected that prstat would simply flush stdout after each block of output. Any ideas about how to get prstat to cooperate better? Thanks, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire From simon at brunningonline.net Mon Jul 27 10:31:11 2009 From: simon at brunningonline.net (Simon Brunning) Date: Mon, 27 Jul 2009 15:31:11 +0100 Subject: iText for Python In-Reply-To: <505f2bc1-cbfb-49a1-a7ef-0b6c357750a7@q35g2000vbi.googlegroups.com> References: <505f2bc1-cbfb-49a1-a7ef-0b6c357750a7@q35g2000vbi.googlegroups.com> Message-ID: <8c7f10c60907270731x1d416e64vde1907d8c4dd42d1@mail.gmail.com> 2009/7/27 santhoshvkumar : > ? ? ? ? ? One of my cousin ?suggested me to do a IText PDF converter > for python. Actually I heard that there is no separate IText converter > either we have to go for jython or GCJ with wrapper. Instead of > wrapping, my plan is to create a separate module with Python and I am > thinking of doing this in as my final year project also. Kindly give > me your suggestion. What would this give us that ReportLab does not? I wouldn't want to see you spend a lot of time reinventing the wheel... -- Cheers, Simon B. From aahz at pythoncraft.com Mon Jul 27 10:32:53 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:32:53 -0700 Subject: Form/Template Fill-in the blanks References: <4b77a992-370d-4879-88a0-fdd6a23f7c5d@p10g2000prm.googlegroups.com> Message-ID: In article <4b77a992-370d-4879-88a0-fdd6a23f7c5d at p10g2000prm.googlegroups.com>, allan wrote: > >My initial thought was to use: >1. .ini files to declare the EDI configuration > >INI file configuration: >* A "root" INI file indicates other INI files that define each segment >of the EDI document. >* Each segment INI defines the data elements of each segment and the >behavior of the segment (one instance or multiple-instance as in a >loop, sequence order, segment code, etc.) >* Specify data elements as either constant, system function (like >datetime), database field or object method (object being the >segment's) >* Load all the ini configuration into a "template" object. Each >segment ini maps to its own segment object. This sounds like something you want to use XML for. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From bryanvick at gmail.com Mon Jul 27 10:37:53 2009 From: bryanvick at gmail.com (Bryan) Date: Mon, 27 Jul 2009 07:37:53 -0700 (PDT) Subject: Script runs manually, but cron fails References: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> Message-ID: On Jul 24, 2:11?pm, Bryan wrote: > I have a backup script that runs fine when I run it manually from the > command line. ?When I run it with cron, the script stops running at > random points in the source code. > > The script calls rsync with the subprocess module, which in turn uses > ssh to backup files from a box on my lan. ?It also uses the atexit > module to always run a certain piece of cleanup code when the script > exits. ?However, this piece of code is never called when I use cron to > run the script, but a ps -A command also does not show the python > process so I know the script is dead. ?The log files generated by the > script all show rsync as completing, but then the logging gets cutoff > at random places when the script dies after that point. ?I am not > getting any email from cron complaining of an error either. > > The script runs fine in my bash shell, what could cron be doing to > interfere? This script works when I run it at home, and I think it is because I don't use rsync's ability to backup over ssh. The working script at home simply backs up the local machine to a mounted external hard drive. So when I run a python script that calls rsync to work over ssh, cron is screwing things up. Can anyone recommend where I should report this behavior? Should I email the author of cron directly, I don't see a group/address specifically for cron bugs. From kushal.kumaran+python at gmail.com Mon Jul 27 10:40:40 2009 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Mon, 27 Jul 2009 20:10:40 +0530 Subject: Itext for Python In-Reply-To: References: <404c076d0907261122r35a1cca9kf11940d972d2b691@mail.gmail.com> Message-ID: <1e364c4e0907270740sdc0e6ceye49f78b342820096@mail.gmail.com> On Mon, Jul 27, 2009 at 2:23 PM, S.Selvam wrote: > > > On Sun, Jul 26, 2009 at 11:52 PM, Santhosh Kumar > wrote: >> >> Hi all, >> ?? ? ? ? ??One of my cousin ?suggested me to do a IText PDF?converter?for >> python. Actually I heard that there is no?separate?IText?converter?either we >> have to go for jython or GCJ with wrapper. Instead of wrapping, my plan is >> to?create a separate module with Python and I am thinking of doing this in >> as my final year project also. Kindly give me your?suggestion. > > As for as i know, no pure python module exists for iText PDF generator, > where iText seems to be a standard one for PDF related works.There are some > other modules for pdf work but might not be as standard as iText.Surely some > senior programmers? will suggest you better way to proceed further. > AFAIK, iText is just a java library for generating PDF files. You could have a look at http://www.reportlab.org/rl_toolkit.html -- kushal From james at agentultra.com Mon Jul 27 10:40:53 2009 From: james at agentultra.com (J Kenneth King) Date: Mon, 27 Jul 2009 10:40:53 -0400 Subject: Script runs manually, but cron fails References: <4cba6362-a25c-4bf1-b27b-4ddf48feb473@d9g2000prh.googlegroups.com> Message-ID: <858wia6vey.fsf@agentultra.com> Bryan writes: > I have a backup script that runs fine when I run it manually from the > command line. When I run it with cron, the script stops running at > random points in the source code. > > The script calls rsync with the subprocess module, which in turn uses > ssh to backup files from a box on my lan. It also uses the atexit > module to always run a certain piece of cleanup code when the script > exits. However, this piece of code is never called when I use cron to > run the script, but a ps -A command also does not show the python > process so I know the script is dead. The log files generated by the > script all show rsync as completing, but then the logging gets cutoff > at random places when the script dies after that point. I am not > getting any email from cron complaining of an error either. > > The script runs fine in my bash shell, what could cron be doing to > interfere? Double check nothing is writing to stdout/stdin Some cron's don't mind, but I always squelch output because some do. From aahz at pythoncraft.com Mon Jul 27 10:41:45 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:41:45 -0700 Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <2a408da6-af57-45d0-a75f-4cbe384bb7f9@s15g2000yqs.googlegroups.com> <1c8ae01e-2e9c-497c-9f8d-408f56f9c0bd@g31g2000yqc.googlegroups.com> Message-ID: In article <1c8ae01e-2e9c-497c-9f8d-408f56f9c0bd at g31g2000yqc.googlegroups.com>, Michal Kwiatkowski wrote: >On Jul 27, 1:56 am, a... at pythoncraft.com (Aahz) wrote: >>>> Upon a cursory look, after a generator 'gen' is exhausted (meaning >>>> gen.next() has raised StopIteration), it seems that gen.gi_frame will be >>>> None. >>> >>>Only in Python 2.5 or higher though. I need to support Python 2.3 and >>>2.4 as well, sorry for not making that clear in the original post. >> >> Are you sure? It appears to work in Python 2.4; I don't have time to >> check 2.3. > >No, it does not work in Python 2.4. gi_frame can be None only in >Python 2.5 and higher. You're right, I guess I must have made a boo-boo when I was switching versions. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From aahz at pythoncraft.com Mon Jul 27 10:45:50 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:45:50 -0700 Subject: pyc files not automatically compiled on import References: Message-ID: In article , Baz Walter wrote: > >i thought that python automatically compiled pyc files after a module is >successfully imported. what could prevent this happening? Looks like you got your problem fixed, but for the record, not having write permission on a directory also causes this. It's even uglier when the .pyc already exists but does not have write perms. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From aahz at pythoncraft.com Mon Jul 27 10:49:25 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 07:49:25 -0700 Subject: If Scheme is so good why MIT drops it? References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: In article , Hendrik van Rooyen wrote: >On Sunday 26 July 2009 21:26:46 David Robinow wrote: >> >> I'm a mediocre programmer. Does this mean I should switch to PHP? > >I have searched, but I can find nothing about this mediocre language. > >Could you tell us more? :-P (For anyone who is confused by Hendrik's humor, he is saying that David was referring to a programming language named "mediocre". English grammar is confusing!) -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From ethan at stoneleaf.us Mon Jul 27 11:00:12 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 27 Jul 2009 08:00:12 -0700 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <468250.83203.qm@web31106.mail.mud.yahoo.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> Message-ID: <4A6DC0FC.90704@stoneleaf.us> [corrected top posting] Mohammad Tayseer wrote: >> *From:* Piet van Oostrum >> *To:* python-list at python.org >> *Sent:* Monday, July 27, 2009 11:18:20 AM >> *Subject:* Re: Looking for a dream language: sounds like Python to me. >> >> >>>>> Dotan Cohen > >> (DC) wrote: >> >> >DC> Referring to this article: >>>DC> >> http://math-blog.com/2009/07/20/complex-algorithm-research-and-development-harder-than-many-think/ >> >> >DC> The author, who is specifically looking for math-related >> functions, writes: >> >DC> """ >> >DC> The dream algorithm R&D tool would be similar to Matlab or Mathematica >> >DC> but could be compiled to fast, efficient binaries similar to ANSI C >> >DC> and would be available for all platforms. An integrated GUI builder >> >DC> similar to Visual Basic and integrated network support would be >> >DC> helpful. >> >DC> """ >> >> >DC> It looks to me like he is looking for Python. >> >> No fast, efficient binaries yet, and no integrated GUI builder. >> -- >> Piet van Oostrum > >> URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] >> Private email: piet at vanoostrum.org >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > You can generate binaries using py2exe, and you can create UI using > Tkinter (which is very easy) or wxPython (which have GUI builders) > > Mohammad Tayseer > http://spellcoder.com/blogs/tayseer Creating binaries is not the same as creating /fast, efficient/ binaries. Py2Exe bundles it all together, but does not make it any faster. ~Ethan~ From __peter__ at web.de Mon Jul 27 11:12:02 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 17:12:02 +0200 Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> <8b78b47f-799e-4d37-bc28-d1ed349362c9@k6g2000yqn.googlegroups.com> Message-ID: Martin wrote: > The statement works now, but it doesn't give the same results as my > original logic, strangely!? > > in my logic: > > data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') > > for i in range(numrows): > for j in range(numcols): > if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: > data[i,j] = 0 > else: > data[i,j] = 1 > > to do the same with what you suggested... > > data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') > data[(band3 == 255) | (band6 >= band3 * factor)] = 0 Did you swap band3 and band6? If that's the case, it is an error you should be able to find yourself. Please be a bit more careful. Also, it is good practice to write a few test cases where you have precalculated the result. How would you otherwise know which version is correct? Writing a third one to break the tie? Peter From dotancohen at gmail.com Mon Jul 27 11:12:09 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Mon, 27 Jul 2009 18:12:09 +0300 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <4A6DC0FC.90704@stoneleaf.us> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> Message-ID: <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> > Creating binaries is not the same as creating /fast, efficient/ binaries. > ?Py2Exe bundles it all together, but does not make it any faster. > How inefficient is py2exe. I was under the impression that it's really not that bad. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From dns4 at cornell.edu Mon Jul 27 11:14:15 2009 From: dns4 at cornell.edu (David Smith) Date: Mon, 27 Jul 2009 11:14:15 -0400 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: Aahz wrote: > In article , > Hendrik van Rooyen wrote: >> On Sunday 26 July 2009 21:26:46 David Robinow wrote: >>> I'm a mediocre programmer. Does this mean I should switch to PHP? >> I have searched, but I can find nothing about this mediocre language. >> >> Could you tell us more? > > :-P > > (For anyone who is confused by Hendrik's humor, he is saying that David > was referring to a programming language named "mediocre". English > grammar is confusing!) LOL ... I'm an American and that wasn't all that clear :-) From exarkun at divmod.com Mon Jul 27 11:18:41 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Mon, 27 Jul 2009 11:18:41 -0400 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> Message-ID: <20090727151841.388.143506053.divmod.quotient.793@henry.divmod.com> On Mon, 27 Jul 2009 18:12:09 +0300, Dotan Cohen wrote: >> Creating binaries is not the same as creating /fast, efficient/ binaries. >> ?Py2Exe bundles it all together, but does not make it any faster. >> > >How inefficient is py2exe. I was under the impression that it's really >not that bad. > py2exe doesn't really change the performance characteristics of a Python application at all - for better or for worse. Jean-Paul From gmweezel at gmail.com Mon Jul 27 11:21:29 2009 From: gmweezel at gmail.com (Eric) Date: Mon, 27 Jul 2009 08:21:29 -0700 (PDT) Subject: CRLF Newlines and libc Message-ID: <6bcb4820-11be-4053-8e3b-849b00aba55a@c2g2000yqi.googlegroups.com> I am working on the subprocess.Popen module for Google Summer of Code. Right now, I am having difficulty trying to work out how to deal with my '\n' newlines being converted to '\r\n' newlines when reading from a pipe on windows; see this blog post (http://subdev.blogspot.com/ 2009/07/stdout.html) and this chain on Python-Dev (http:// mail.python.org/pipermail/python-dev/2009-July/090720.html). Right now, unless there is way to disable the newline conversions, I am thinking I will just have to document the caveat and call it a day. Is there a better way to handle this? Eric From cournape at gmail.com Mon Jul 27 11:22:16 2009 From: cournape at gmail.com (David Cournapeau) Date: Tue, 28 Jul 2009 00:22:16 +0900 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> Message-ID: <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> On Tue, Jul 28, 2009 at 12:12 AM, Dotan Cohen wrote: >> Creating binaries is not the same as creating /fast, efficient/ binaries. >> ?Py2Exe bundles it all together, but does not make it any faster. >> > > How inefficient is py2exe. It is neither efficient or inefficient: it is just a distribution tool, to deploy python software in a form familiar to most windows users. It does not make it any faster than running the software under a python prompt. As much as I like python for scientific programming, I would say python is pretty far from the stated requirements in the posted blog post. It is difficult to deploy software written with python (much better than the alternatives, though), and it is slow if you can't leverage numpy/scipy (where vectorization does not apply). It remains to be seen whether it will be true in practice, but something like F#, with its integration in VS 2010, seems much closer IMHO. It is compiled, high level language, and backed by the biggest software vendor in the world. David From invalid at invalid Mon Jul 27 11:24:27 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 27 Jul 2009 10:24:27 -0500 Subject: Looking for a dream language: sounds like Python to me. References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> Message-ID: On 2009-07-27, Dotan Cohen wrote: >> Creating binaries is not the same as creating /fast, efficient/ binaries. >> ??Py2Exe bundles it all together, but does not make it any faster. > > How inefficient is py2exe. [Assuming that was a question.] py2exe just bundles up the files needed to run the program. The result runs pretty much the same as it did when it was "unbundled". By default, py2exe combines some stuff in a zip file, so that might slow down startup by a tiny amount. If you don't like that, you can tell py2exe not to "zip" things up, and then the program runs pretty much identically as did the unbundled version. > I was under the impression that it's really not that bad. py2exe doesn't really have any impact on the program's perfromance. It's still the same Python VM running the same bytecode using the same library files. -- Grant Edwards grante Yow! I'm totally DESPONDENT at over the LIBYAN situation visi.com and the price of CHICKEN ... From dotancohen at gmail.com Mon Jul 27 11:28:47 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Mon, 27 Jul 2009 18:28:47 +0300 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> Message-ID: <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> > It is neither efficient or inefficient: it is just a distribution > tool, to deploy python software in a form familiar to most windows > users. It does not make it any faster than running the software under > a python prompt. > > As much as I like python for scientific programming, I would say > python is pretty far from the stated requirements in the posted blog > post. It is difficult to deploy software written with python (much > better than the alternatives, though), and it is slow if you can't > leverage numpy/scipy (where vectorization does not apply). > > It remains to be seen whether it will be true in practice, but > something like F#, with its integration in VS 2010, seems much closer > IMHO. It is compiled, high level language, and backed by the biggest > software vendor in the world. > The blog post is not looking to distribute his code, but he would like it to be cross platform for his own reasons. VB is not cross platform. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From cournape at gmail.com Mon Jul 27 11:39:14 2009 From: cournape at gmail.com (David Cournapeau) Date: Tue, 28 Jul 2009 00:39:14 +0900 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: <5b8d13220907270839p3f65e1f8k7c1bb022bc922d6e@mail.gmail.com> On Tue, Jul 28, 2009 at 12:28 AM, Dotan Cohen wrote: >> It is neither efficient or inefficient: it is just a distribution >> tool, to deploy python software in a form familiar to most windows >> users. It does not make it any faster than running the software under >> a python prompt. >> >> As much as I like python for scientific programming, I would say >> python is pretty far from the stated requirements in the posted blog >> post. It is difficult to deploy software written with python (much >> better than the alternatives, though), and it is slow if you can't >> leverage numpy/scipy (where vectorization does not apply). >> >> It remains to be seen whether it will be true in practice, but >> something like F#, with its integration in VS 2010, seems much closer >> IMHO. It is compiled, high level language, and backed by the biggest >> software vendor in the world. >> > > The blog post is not looking to distribute his code, but he would like > it to be cross platform for his own reasons. VB is not cross platform. I understand his "efficient binary as Ansi C" partially as a deployment requirement, and independent of cross-platform issues. As a scientist, being able to share my software with colleagues is a non trivial matter. Python does not make this easy today. F# has nothing to do with VB: F# is a ML-language inspired from OCAML, and run on top of the CLR. It can thus leverage the huge .net framework (lack of non numerical API is one of the biggest matlab hindrance, and comparatively big advantage of python + numpy/scipy), and benefits from the much more efficient implementation compared to python (under the currently CPython implementation at least). Some recent F# versions are compatible with mono, making it compatible on most platforms that matter today for research (but of course, you lose the IDE integration outside windows). David From jakecjacobson at gmail.com Mon Jul 27 11:44:40 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Mon, 27 Jul 2009 08:44:40 -0700 (PDT) Subject: exceptions.TypeError an integer is required References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> <0279f85d$0$5185$c3e8da3@news.astraweb.com> Message-ID: <8d03a739-7e45-49e1-b93d-6a396beed877@a37g2000prf.googlegroups.com> On Jul 24, 3:11?pm, Steven D'Aprano wrote: > On Fri, 24 Jul 2009 11:24:58 -0700, jakecjacobson wrote: > > I am trying to do a post to a REST API over HTTPS and requires the > > script to pass a cert to the server. ?I am getting "exceptions.TypeError > > an integer is required" error and can't find the reason. ?I commenting > > out the lines of code, it is happening on the connection.request() line. > > ?Here is the problem code. ?Would love some help if possible. > > Please post the traceback that you get. > > My guess is that you are passing a string instead of an integer, probably > for the port. > > [...] > > > ? ?except: > > ? ? ? ? ? ?print sys.exc_type, sys.exc_value > > As a general rule, a bare except of that fashion is bad practice. Unless > you can explain why it is normally bad practice, *and* why your case is > an exception (no pun intended) to the rule "never use bare except > clauses", I suggest you either: > > * replace "except:" with "except Exception:" instead. > > * better still, re-write the entire try block as: > > ? ? try: > ? ? ? ? [code goes here] > ? ? finally: > ? ? ? ? connection.close() > > and use the Python error-reporting mechanism instead of defeating it. > > -- > Steven Steven, You are quite correct in your statements. My goal was not to make great code but something that I could quickly test. My assumption was that the httplib.HTTPSConnection() would do the cast to int for me. As soon as I cast it to an int, I was able to get past that issue. Still not able to post because I am getting a bad cert error. Jake Jacobson From jakecjacobson at gmail.com Mon Jul 27 11:57:40 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Mon, 27 Jul 2009 08:57:40 -0700 (PDT) Subject: bad certificate error Message-ID: Hi, I am getting the following error when doing a post to REST API, Enter PEM pass phrase: Traceback (most recent call last): File "./ices_catalog_feeder.py", line 193, in ? main(sys.argv[1]) File "./ices_catalog_feeder.py", line 60, in main post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join (input_dir, file), collection_name, key_file, cert_file) File "./ices_catalog_feeder.py", line 125, in post2Catalog connection.request('POST', path, parameters, head) File "/usr/lib/python2.4/httplib.py", line 810, in request self._send_request(method, url, body, headers) File "/usr/lib/python2.4/httplib.py", line 833, in _send_request self.endheaders() File "/usr/lib/python2.4/httplib.py", line 804, in endheaders self._send_output() File "/usr/lib/python2.4/httplib.py", line 685, in _send_output self.send(msg) File "/usr/lib/python2.4/httplib.py", line 652, in send self.connect() File "/usr/lib/python2.4/httplib.py", line 1079, in connect ssl = socket.ssl(sock, self.key_file, self.cert_file) File "/usr/lib/python2.4/socket.py", line 74, in ssl return _realssl(sock, keyfile, certfile) socket.sslerror: (1, 'error:14094412:SSL routines:SSL3_READ_BYTES:sslv3 alert bad certificate') My code where this error occurs is: head = {"Content-Type" : "application/x-www-form-urlencoded", "Accept" : "text/plain"} parameters = urlencode({"collection" : collection, "entryxml" : open (file,'r').read()}) print "Sending the file to: " + host try: try: # Default port is 443. # key_file is the name of a PEM formatted file that contains your private key. # cert_file is a PEM formatted certificate chain file. connection = httplib.HTTPSConnection(host, int(port), key_file, cert_file) connection.request('POST', path, parameters, head) response = connection.getresponse() print response.status, response.reason except httplib.error, (value,message): print value + ':' + message finally: connection.close() I was wondering if this is due to the server having a invalid server cert? If I go to this server in my browser, I get a "This server tried to identify itself with invalid information". Is there a way to ignore this issue with Python? Can I setup a trust store and add this server to the trust store? From mdekauwe at gmail.com Mon Jul 27 12:00:41 2009 From: mdekauwe at gmail.com (Martin) Date: Mon, 27 Jul 2009 09:00:41 -0700 (PDT) Subject: quickly looping over a 2D array? References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> <503adfe2-bf70-422e-b22f-8befcae577b9@r2g2000yqm.googlegroups.com> <266d5f19-6a9d-4733-9cd4-f40f03dcb1fa@k1g2000yqf.googlegroups.com> <8b78b47f-799e-4d37-bc28-d1ed349362c9@k6g2000yqn.googlegroups.com> Message-ID: On Jul 27, 4:12 pm, Peter Otten <__pete... at web.de> wrote: > Martin wrote: > > The statement works now, but it doesn't give the same results as my > > original logic, strangely!? > > > in my logic: > > > data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') > > > for i in range(numrows): > > for j in range(numcols): > > if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: > > data[i,j] = 0 > > else: > > data[i,j] = 1 > > > to do the same with what you suggested... > > > data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') > > data[(band3 == 255) | (band6 >= band3 * factor)] = 0 > > Did you swap band3 and band6? If that's the case, it is an error you should > be able to find yourself. > > Please be a bit more careful. > > Also, it is good practice to write a few test cases where you have > precalculated the result. How would you otherwise know which version is > correct? Writing a third one to break the tie? > > Peter apologies... it was the way I typed it up, I wasn't copying and pasting from my text editor! Thanks Peter. From davea at ieee.org Mon Jul 27 12:09:38 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 12:09:38 -0400 Subject: [python-win32] subprocess and stdout In-Reply-To: References: <4A6D9D83.6050505@ieee.org> Message-ID: <4A6DD142.7040809@ieee.org> (This message was top-posted, and sent off-list. So I'm copying it back to the list, with my response at the end) Chris Chapman wrote: > Thanks Dave. You know after trying your suggestion on the command prompt it doesn't as a matter of fact. Not sure why I didn't just try that in windows to begin with. Guess I got stuck more on the python issue then it being the other program. > > There is an option for it to create an HTML log file, but it's formatted sort of strangely. I'm really not looking to parse through it just for a summary like that presented on the terminal. I'm not sure if you know but I'm guessing parsing the HTML would be a lot easier the trying to capture the console output from the buffer, huh? Thanks again for the help. > > Chris > > >> Date: Mon, 27 Jul 2009 08:28:51 -0400 >> From: davea at ieee.org >> To: oxydol010 at hotmail.com >> CC: python-win32 at python.org >> Subject: Re: [python-win32] subprocess and stdout >> >> Christopher Chapman wrote: >> >>> I'm trying to write a "module" to a larger program. This module eventually >>> needs to reference a config file with setup information in it, but I'm not >>> there yet. I'm using the subprocess module to run an external antivirus >>> program and trying to get the output written to a log file, but I keep >>> getting a weird return. My code is listed below. I'm a noob to Python so >>> I'm not sure if this is a windows specific issue or not, but I'm programming >>> on windows and more then likely what I'm working on won't be on anything >>> other then a windows machine. I'm also writing my code for 3.1 if that >>> makes any difference. >>> >>> # Pass target path to scanners command line and write output to a file >>> # Currently "scan" only targets the McAfee Command Line Scanner >>> print() >>> print ("Begining scan of " + target) >>> print() >>> scan = "scan /all " + target >>> s = subprocess.Popen(scan, >>> stdout=subprocess.PIPE) >>> out = s.communicate()[0] >>> chgout = str(out) >>> s.wait() >>> scanlog.write(chgout) >>> scanlog.close >>> " >>> If I change my stdout in s subprocess to None then everything gets written >>> to the terminal as if I had just run the program straight from the command >>> line, but when I try piping it to my file "scanlog" then literally the only >>> return I get in the file is '' or two single quotes. I've even tried piping >>> the output and then printing it instead of writing it to a file and I get >>> the same result. I've experimented with standard windows command line >>> commands and using the same syntax was able to pipe directory listings and >>> other things to my file. Any ideas what I'm missing here? Thanks in >>> advance >>> >>> Chris Chapman >>> >>> >>> >>> >> I suspect it's because of the way the antivirus program is written. I >> can't be sure what you tried at the command prompt, but have you tried this: >> >> c:\>scan /all > scanlog.txt >> >> >> If this does not capture to the file, then scan.exe isn't written in the >> way you're expecting. There are several ways to write directly to a >> console that do not redirect or pipe. >> >> You might also look to see whether scan has any other commandline >> options. One of them might be to create a log file. >> >> DaveA >> > > I wouldn't know where to begin trying to capture output that another process sends to its console window. While I'm sure it's possible, I do suspect that parsing the html would be easier. Remember that even if it looks ugly, it is generated by program, and it's quite possible that you could throw away the fluff and end up with a simple summary. There are libraries to make this easier, but I've never used them. If it's valid xhtml, which is by definition valid xml, you might find it useful to use elementree. But most html is buggy, and parsing that takes a more tolerant parser. DaveA From robert.kern at gmail.com Mon Jul 27 12:11:02 2009 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 27 Jul 2009 11:11:02 -0500 Subject: quickly looping over a 2D array? In-Reply-To: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> References: <1b8747d1-571a-4d79-8849-b918fefeaf4d@r2g2000yqm.googlegroups.com> Message-ID: On 2009-07-27 06:24, Martin wrote: > Hi, > > I am new to python and I was wondering if there was a way to speed up > the way I index 2D arrays when I need to check two arrays > simultaneously? My current implementations is (using numpy) something > like the following... > > for i in range(numrows): > for j in range(numcols): > > if array_1[i, j] == some_value or array_2[i, j]>= array_1[i, > j] * some_other_value > array_1[i, j] = some_new_value Peter has given you good answers, but you will probably want to ask future numpy questions on the numpy mailing list. http://www.scipy.org/Mailing_Lists -- 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 python at mrabarnett.plus.com Mon Jul 27 12:34:03 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 27 Jul 2009 17:34:03 +0100 Subject: New implementation of re module Message-ID: <4A6DD6FB.8040609@mrabarnett.plus.com> Hi all, I've been working on a new implementation of the re module. The details are at http://bugs.python.org/issue2636, specifically from http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for Python 2.6 on Windows if you want to try it out. I'm interested in how fast it is generally, compared with the current re module, but especially when faced with those 'pathological' regular expressions which seem to take a long time to finish, for example: re.search(r"^(.+|D)*A$", "x" * 25 + "B") which on my PC (1.8GHz) takes 18.98secs with the re module but <0.01secs with this new implementation. TIA From Ray.Joseph at CDICorp.com Mon Jul 27 12:42:06 2009 From: Ray.Joseph at CDICorp.com (ray) Date: Mon, 27 Jul 2009 09:42:06 -0700 (PDT) Subject: Using easy_install, reduncant? Message-ID: I am working on a Trac installation. I am new to Python. To install packages, it is suggested to use setuptools. I have not understood the directions. I execute ez_install.py. Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. There response that setuptools is already the active version in easy- install.pth. Then: Installing easy_install.exe script to C:\Python26\Scripts error: C: \Python26\Scripts\Easy_install.exe: Permission denied. I have compared the file entries before and after this attempt and there are no new files. Is there any problems here? What did I miss? Regards, Ray From nagle at animats.com Mon Jul 27 12:58:57 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 09:58:57 -0700 Subject: Using easy_install, reduncant? In-Reply-To: References: Message-ID: <4a6ddc22$0$1667$742ec2ed@news.sonic.net> ray wrote: > I am working on a Trac installation. I am new to Python. To install > packages, it is suggested to use setuptools. I have not understood > the directions. > > I execute ez_install.py. > > Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. > There response that setuptools is already the active version in easy- > install.pth. Then: > Installing easy_install.exe script to C:\Python26\Scripts error: C: > \Python26\Scripts\Easy_install.exe: Permission denied. "Easy Install" usually isn't "easy". If it works, it's great. Most of the time, it doesn't. It's not very good at finding all the files it needs to update. Hence problems like the above. John Nagle From deets at nospam.web.de Mon Jul 27 13:00:52 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 19:00:52 +0200 Subject: Using easy_install, reduncant? References: <4a6ddc22$0$1667$742ec2ed@news.sonic.net> Message-ID: <7d64q4F2an2drU1@mid.uni-berlin.de> John Nagle wrote: > ray wrote: >> I am working on a Trac installation. I am new to Python. To install >> packages, it is suggested to use setuptools. I have not understood >> the directions. >> >> I execute ez_install.py. >> >> Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. >> There response that setuptools is already the active version in easy- >> install.pth. Then: >> Installing easy_install.exe script to C:\Python26\Scripts error: C: >> \Python26\Scripts\Easy_install.exe: Permission denied. > > "Easy Install" usually isn't "easy". If it works, it's great. > Most of the time, it doesn't. It's not very good at finding all the > files it needs to update. Hence problems like the above. Which is the usual BS from you. It works most of the times as advertised, and in the concrete case the OP misunderstood what ez_setup.py does (already installing setuptools), and thus the complaint about "already active version". Which btw is just a warning, and setuptools would continue, but as the OP works on Windows which prohibits a running executable to be replaced, it fails. Again, not setuptools fault. Diez From nagle at animats.com Mon Jul 27 13:23:43 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 10:23:43 -0700 Subject: M2Crypto hangs on this URL Message-ID: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> There's something strange about this URL: "https://sagar310.pontins.com/sraep/" It hangs Firefox 2; there's no short timeout, the web page just gets stuck in initial load for about ten minutes. Then "The connection to sagar310.pontins.com was interrupted while the page was loading." It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. What does the new SSL implementation do? (Haven't tried that yet; waiting for MySQL support for Python 3.x before converting web apps.) John Nagle From rlichlighter at gmail.com Mon Jul 27 13:25:18 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 10:25:18 -0700 (PDT) Subject: Convert raw binary file to ascii References: Message-ID: On Jul 27, 9:06?am, Peter Otten <__pete... at web.de> wrote: > r2 wrote: > > I have a memory dump from a machine I am trying to analyze. I can view > > the file in a hex editor to see text strings in the binary code. I > > don't see a way to save these ascii representations of the binary, so > > I went digging into Python to see if there were any modules to help. > > > I found one I think might do what I want it to do - the binascii > > module. Can anyone describe to me how to convert a raw binary file to > > an ascii file using this module. I've tried? Boy, I've tried. > > That won't work because a text editor doesn't need any help to convert the > bytes into characters. If it expects ascii it just will be puzzled by bytes > that are not valid ascii. Also, it will happily display byte sequences that > are valid ascii, but that you as a user will see as gibberish because they > were meant to be binary data by the program that wrote them. > > > Am I correct in assuming I can get the converted binary to ascii text > > I see in a hex editor using this module? I'm new to this forensics > > thing and it's quite possible I am mixing technical terms. I am not > > new to Python, however. Thanks for your help. > > Unix has the "strings" commandline tool to extract text from a binary. > Get hold of a copy of the MinGW tools if you are on windows. > > Peter Okay. Thanks for the guidance. I have a machine with Linux, so I should be able to do what you describe above. Could Python extract the strings from the binary as well? Just wondering. From rlichlighter at gmail.com Mon Jul 27 13:25:44 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 10:25:44 -0700 (PDT) Subject: Convert raw binary file to ascii References: Message-ID: On Jul 27, 10:11?am, Grant Edwards wrote: > On 2009-07-27, r2 wrote: > > > I have a memory dump from a machine I am trying to analyze. I can view > > the file in a hex editor to see text strings in the binary code. I > > don't see a way to save these ascii representations of the binary, > > $ strings memdump.binary >memdump.strings > > $ hexdump -C memdump.binary >memdump.hex+ascii > > -- > Grant Edwards ? ? ? ? ? ? ? ? ? grante ? ? ? ? ? ? Yow! I'm ZIPPY the PINHEAD > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? at ? ? ? ? ? ? ? and I'm totally committed > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?visi.com ? ? ? ? ? ?to the festive mode. Grant, Thanks for the commands! From reckoner at gmail.com Mon Jul 27 13:40:03 2009 From: reckoner at gmail.com (Reckoner) Date: Mon, 27 Jul 2009 10:40:03 -0700 (PDT) Subject: initializing with empty list as default causes freaky problems Message-ID: Hi, Observe the following: In [202]: class Foo(): .....: def __init__(self,h=[]): .....: self.h=h .....: .....: In [203]: f=Foo() In [204]: g=Foo() In [205]: g.h Out[205]: [] In [206]: f.h Out[206]: [] In [207]: f.h.append(10) In [208]: f.h Out[208]: [10] In [209]: g.h Out[209]: [10] The question is: why is g.h updated when I append to f.h? Shouldn't g.h stay []? What am I missing here? I'm using Python 2.5.1. Thanks in advance. From python at mrabarnett.plus.com Mon Jul 27 13:55:57 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 27 Jul 2009 18:55:57 +0100 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: <4A6DEA2D.6000408@mrabarnett.plus.com> Reckoner wrote: > Hi, > X-Antispam: NO; Spamcatcher 5.2.1. Score 50 > > Observe the following: > > In [202]: class Foo(): > .....: def __init__(self,h=[]): > .....: self.h=h > .....: > .....: > > In [203]: f=Foo() > > In [204]: g=Foo() > > In [205]: g.h > Out[205]: [] > > In [206]: f.h > Out[206]: [] > > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? Shouldn't > g.h stay []? > > What am I missing here? > > I'm using Python 2.5.1. > Default arguments are evaluated once and then shared, so don't use them with mutable objects like lists. Do this instead: class Foo(): def __init__(self, h=None): if h is None: self.h = [] else: self.h = h From __peter__ at web.de Mon Jul 27 14:07:37 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 27 Jul 2009 20:07:37 +0200 Subject: Convert raw binary file to ascii References: Message-ID: r2 wrote: > On Jul 27, 9:06 am, Peter Otten <__pete... at web.de> wrote: >> r2 wrote: >> > I have a memory dump from a machine I am trying to analyze. I can view >> > the file in a hex editor to see text strings in the binary code. I >> > don't see a way to save these ascii representations of the binary, so >> > I went digging into Python to see if there were any modules to help. >> >> > I found one I think might do what I want it to do - the binascii >> > module. Can anyone describe to me how to convert a raw binary file to >> > an ascii file using this module. I've tried? Boy, I've tried. >> >> That won't work because a text editor doesn't need any help to convert >> the bytes into characters. If it expects ascii it just will be puzzled by >> bytes that are not valid ascii. Also, it will happily display byte >> sequences that are valid ascii, but that you as a user will see as >> gibberish because they were meant to be binary data by the program that >> wrote them. >> >> > Am I correct in assuming I can get the converted binary to ascii text >> > I see in a hex editor using this module? I'm new to this forensics >> > thing and it's quite possible I am mixing technical terms. I am not >> > new to Python, however. Thanks for your help. >> >> Unix has the "strings" commandline tool to extract text from a binary. >> Get hold of a copy of the MinGW tools if you are on windows. >> >> Peter > > Okay. Thanks for the guidance. I have a machine with Linux, so I > should be able to do what you describe above. Could Python extract the > strings from the binary as well? Just wondering. As a special service for you here is a naive implementation to build upon: #!/usr/bin/env python import sys wanted_chars = ["\0"]*256 for i in range(32, 127): wanted_chars[i] = chr(i) wanted_chars[ord("\t")] = "\t" wanted_chars = "".join(wanted_chars) THRESHOLD = 4 for s in sys.stdin.read().translate(wanted_chars).split("\0"): if len(s) >= THRESHOLD: print s Peter From nagle at animats.com Mon Jul 27 14:13:40 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 11:13:40 -0700 Subject: M2Crypto hangs on this URL In-Reply-To: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> Message-ID: <4a6deda5$0$1639$742ec2ed@news.sonic.net> John Nagle wrote: > There's something strange about this URL: > > "https://sagar310.pontins.com/sraep/" > > It hangs Firefox 2; there's no short timeout, the web page just gets > stuck in initial load for about ten minutes. Then > "The connection to sagar310.pontins.com was interrupted while the page > was loading." > > It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. Correction: Linux is still hung at 5 hours, but Windows timed out after about 15 minutes. John Nagle From gagsl-py2 at yahoo.com.ar Mon Jul 27 14:14:38 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 15:14:38 -0300 Subject: CRLF Newlines and libc References: <6bcb4820-11be-4053-8e3b-849b00aba55a@c2g2000yqi.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 12:21:29 -0300, Eric escribi?: > I am working on the subprocess.Popen module for Google Summer of Code. > Right now, I am having difficulty trying to work out how to deal with > my '\n' newlines being converted to '\r\n' newlines when reading from > a pipe on windows; see this blog post (http://subdev.blogspot.com/ > 2009/07/stdout.html) and this chain on Python-Dev (http:// > mail.python.org/pipermail/python-dev/2009-July/090720.html). > > Right now, unless there is way to disable the newline conversions, I > am thinking I will just have to document the caveat and call it a day. > Is there a better way to handle this? It isn't clear to me what exactly your problem is. What are you reading, using which functions, how did you open the file? On the writing side, how did you open the file, which functions are used to write? The Windows API functions like CreateFile, ReadFile, WriteFile... don't perform any kind of '\n' conversion, neither reading nor writing. On the other hand, the C library does differentiate between files opened in text mode or binary mode (this comes from the ANSI C standard, it's not Windows specific). The default is "text" mode (unless you add "b" to the fopen call, or O_BINARY to the open call). In that mode, '\n' is translated to '\r\n' on writing, and '\r\n' is translated to '\n' on reading. Python files are implemented on top of the C library (on 2.x) so they inherit that behaviour. Console programs written in C and using printf/fwrite/write to stdout share that behaviour too (standard handles are opened in text mode by default). Note that """'\n' newlines being converted to '\r\n' newlines when reading""" cannot happen -- if you see '\r\n' when reading, it's because a) you're reading the file in binary mode, and b) the file was written in text mode. b) is true for process output redirected to a pipe, so you'll have to fix a). How to do that, depends on what exactly you're doing. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 27 14:23:41 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 15:23:41 -0300 Subject: bad certificate error References: Message-ID: En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson escribi?: > I was wondering if this is due to the server having a invalid server > cert? If I go to this server in my browser, I get a "This server > tried to identify itself with invalid information". Is there a way to > ignore this issue with Python? Can I setup a trust store and add this > server to the trust store? I don't see the point in trusting someone that you know is telling lies about itself. -- Gabriel Genellina From kyrie at uh.cu Mon Jul 27 14:25:47 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Mon, 27 Jul 2009 14:25:47 -0400 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: <1248719147.4a6df12b61835@mail.uh.cu> Quoting Reckoner : > Hi, > > Observe the following: > > In [202]: class Foo(): > .....: def __init__(self,h=[]): > .....: self.h=h [...] > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? Shouldn't > g.h stay []? What you are seeing is basically the same that happens here: === In [1]: def f(l=[]): ...: l.append(1) ...: print l ...: In [2]: f() [1] In [3]: f() [1, 1] In [4]: f() [1, 1, 1] === The problem is that the default value "[]" is evaluated only once, at function creation time, and not at invocation. Thus, every call to the function shares the same default object. That is consistent with python's function type: the "def" construct just creates a function object and initializes it with the code, argument list and default values. That means that the default value are part of the function object itself, regardless of when/if it is called: === In [5]: f.func_defaults Out[5]: ([1, 1, 1],) === The recommended way of dealing with this case (mutable default arguments) is: def f(l = None): if l is None: l = [] # the code goes here. (in your case, your g.__init__ and f.__init__ methods share the same Foo.__init__ function, and thus, share the same default value []) That is a very common python gotcha, and I think it is well documented in the standard doc (but I can't find it right now, sorry). Unfortunately, it doesn't become intuitive until you've spent a while understanding python's execution model (and then you suddenly realize that it just makes sense). [And now I wonder... how other languages do it? I've spent so much time with python that reevaluating the default argument on invocation feels clumsy, but I'm obviously tainted now...] Regards, -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From niedziala at gazeta.pl Mon Jul 27 14:43:36 2009 From: niedziala at gazeta.pl (Piotrek) Date: Mon, 27 Jul 2009 20:43:36 +0200 Subject: where do I put resources (images, audio files) when I wrote Python program? Message-ID: Hello, I write a Python program. It will contain some images (in .png format), some audio files (as .ogg) etc. Now I think where should my installer put these files and how should I access them. What is the normal Python way of doing that? I think about puting these files in /usr/share/myprogram and then reading it the normal way (so the path "/usr/share/myprogram" would be just hardwired in my program). Is it the way one usually does it in Python program or is there any more sofisticated way? From jakecjacobson at gmail.com Mon Jul 27 14:52:08 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Mon, 27 Jul 2009 11:52:08 -0700 (PDT) Subject: bad certificate error References: Message-ID: <157f16f9-2172-4fd5-8ec8-8d9569a3f186@f20g2000prn.googlegroups.com> On Jul 27, 2:23?pm, "Gabriel Genellina" wrote: > En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson ? > escribi?: > > > I was wondering if this is due to the server having a invalid server > > cert? ?If I go to this server in my browser, I get a "This server > > tried to identify itself with invalid information". ?Is there a way to > > ignore this issue with Python? ?Can I setup a trust store and add this > > server to the trust store? > > I don't see the point in trusting someone that you know is telling lies ? > about itself. > > -- > Gabriel Genellina It is a test box that the team I am on runs. That is why I would trust it. From mhearne808 at gmail.com Mon Jul 27 14:57:28 2009 From: mhearne808 at gmail.com (mhearne808) Date: Mon, 27 Jul 2009 11:57:28 -0700 (PDT) Subject: where do I put resources (images, audio files) when I wrote Python program? References: Message-ID: On Jul 27, 12:43?pm, Piotrek wrote: > Hello, > > I write a Python program. It will contain some images (in .png format), some > audio files (as .ogg) etc. Now I think where should my installer put these > files and how should I access them. What is the normal Python way of doing > that? I think about puting these files in /usr/share/myprogram and then > reading it the normal way (so the path "/usr/share/myprogram" would be just > hardwired in my program). Is it the way one usually does it in Python > program or is there any more sofisticated way? Usually the preferred method is either distutils (http:// docs.python.org/library/distutils.html#module-distutils) or setuptools (http://peak.telecommunity.com/DevCenter/setuptools). Either of these will allow you to create source (".tar.gz") or binary distributions that can be installed relatively easily on a target machine. Good luck, Mike From wilk at flibuste.net Mon Jul 27 15:04:59 2009 From: wilk at flibuste.net (William Dode) Date: 27 Jul 2009 19:04:59 GMT Subject: New implementation of re module References: Message-ID: <4a6dfa5b$0$10245$426a74cc@news.free.fr> On 27-07-2009, MRAB wrote: > Hi all, > > I've been working on a new implementation of the re module. The details > are at http://bugs.python.org/issue2636, specifically from > http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for > Python 2.6 on Windows if you want to try it out. > Someone can remember me how to compile it (on debian lenny), if possible with python2.5. I've also python3.1 that i build alone... I could test it with pytextile, i've a bunch of texts to bench and compare. Did you announce it on the unladen-swallow list ? They wanted to hack on RE also... -- William Dod? - http://flibuste.net Informaticien Ind?pendant From joshua at joshuakugler.com Mon Jul 27 15:09:47 2009 From: joshua at joshuakugler.com (Joshua Kugler) Date: Mon, 27 Jul 2009 11:09:47 -0800 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <24654358.post@talk.nabble.com> Message-ID: Dr. Phillip M. Feldman wrote: > > "As far as I know, there is no programming language which treats scalars > like ints as if they were > vectors of length 1" > > Actually, Matlab does: > >>> length(5) > ans = > 1 >>> Oddly enough, so does Perl: $ print length(44) 2 (that's in the Zoidberg shell) j From deets at nospam.web.de Mon Jul 27 15:26:46 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 27 Jul 2009 21:26:46 +0200 Subject: Wrapping prstat on Solaris In-Reply-To: References: Message-ID: <7d6dbmF2ahp61U1@mid.uni-berlin.de> skip at pobox.com schrieb: > At work we currently use top to monitor ongoing system utilization on our > Solaris systems. As time has moved on though, use of top has become > problematic. Our admins want us to switch to prstat, Sun's top-like > command. It works fine however doesn't emit a timestamp at each display > interval, so it's essentially impossible looking at a prstat output > file to determine when the particular "screen" was emitted. > > If figured, "No problem. I'll just write a little wrapper." Alas, that is > proving harder than I thought. I can run prstat directing output to a file > and it seems to blast out a block of lines every N seconds, but when run > from inside a Python script I can't seem to make the damn thing not > massively buffer its output. Accordingly, my script doesn't really see the > output as its emitted, so the timestamp line it prepends to a block of > output is off. > > I'm currently using subprocess.Popen like so: > > os.environ["TERM"] = "dumb" > cmd = "prstat -c %s" % " ".join(sys.argv[1:]) > pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout > > I've tried these other variants as well: > > * prefacing the prstat command with unbuffer (the tcl/expect thingamabob) > > * explicitly redirect the prstat output to /dev/stdout > > * setting bufsize to 0 > > * used os.popen instead of Subprocess.Popen > > Nothing seems to help. I always seem to see about 30 seconds of data at > once (I'm currently using a 5-second interval and 10 lines of output). I > would have expected that prstat would simply flush stdout after each block > of output. > > Any ideas about how to get prstat to cooperate better? Use pexpect, which will make the program believe it runs in a terminal, and not buffer the output. Diez From invalid at invalid Mon Jul 27 15:27:47 2009 From: invalid at invalid (Grant Edwards) Date: Mon, 27 Jul 2009 14:27:47 -0500 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <0279f6df$0$5185$c3e8da3@news.astraweb.com> <24654358.post@talk.nabble.com> Message-ID: On 2009-07-27, Joshua Kugler wrote: > Dr. Phillip M. Feldman wrote: > >> "As far as I know, there is no programming language which >> treats scalars like ints as if they were vectors of length 1" >> >> Actually, Matlab does: >> >>>> length(5) >> ans = >> 1 >>>> > > Oddly enough, so does Perl: > > $ print length(44) > 2 No, that's not really treating scalars as vectors of length 1. Were it doing so, length(44) would be 1 rather than 2. What Perl does is treat everything as a string. -- Grant Edwards grante Yow! I was making donuts at and now I'm on a bus! visi.com From wolfgang at rohdewald.de Mon Jul 27 15:27:55 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Mon, 27 Jul 2009 21:27:55 +0200 Subject: New implementation of re module In-Reply-To: <4A6DD6FB.8040609@mrabarnett.plus.com> References: <4A6DD6FB.8040609@mrabarnett.plus.com> Message-ID: <200907272127.55328.wolfgang@rohdewald.de> On Monday 27 July 2009, MRAB wrote: > I've been working on a new implementation of the re module. The > details are at http://bugs.python.org/issue2636, specifically from > http://bugs.python.org/issue2636#msg90954. I've included a .pyd > file for Python 2.6 on Windows if you want to try it out. how do I compile _regex.c on Linux? -- Wolfgang From benjamin.kaplan at case.edu Mon Jul 27 15:30:31 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 27 Jul 2009 15:30:31 -0400 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: On Mon, Jul 27, 2009 at 1:40 PM, Reckoner wrote: > Hi, > > Observe the following: > > In [202]: class Foo(): > ? .....: ? ? def __init__(self,h=[]): > ? .....: ? ? ? ? self.h=h > ? .....: > ? .....: > > In [203]: f=Foo() > > In [204]: g=Foo() > > In [205]: g.h > Out[205]: [] > > In [206]: f.h > Out[206]: [] > > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? ?Shouldn't > g.h stay []? > > What am I missing here? > > I'm using Python 2.5.1. > > Thanks in advance. . It has to do with the way Python handles functions and class methods. Basically, when the parser creates the class Foo, it also creates all of Foo's class variables and functions. It's at this point, and only at this point, that the default parameter is evaluated. When you make an instance of Foo, just wraps the function __init__ by filling in the initial parameter. Because both f and g are really using the Foo.__init__ function (as opposed to separate f.__init__ and g.__init__ methods), they share the same default parameter, a list that was created at the very beginning. Since f.h and g.h both refer to the same list, updating f.h will also update g.h (and the default parameter). That's why it's a bad idea to use mutable default parameters- use either None class Foo(object) : def __init__(self, h=None) : if h is None: h = [] or, if None is an acceptable value, make a sentinel value sentinel = object() class Foo(object) : def __init__(self, h = sentinel) : if h is sentinel: h = [] > -- > http://mail.python.org/mailman/listinfo/python-list > From rlichlighter at gmail.com Mon Jul 27 15:33:21 2009 From: rlichlighter at gmail.com (r2) Date: Mon, 27 Jul 2009 12:33:21 -0700 (PDT) Subject: Convert raw binary file to ascii References: Message-ID: On Jul 27, 2:07?pm, Peter Otten <__pete... at web.de> wrote: > r2 wrote: > > On Jul 27, 9:06 am, Peter Otten <__pete... at web.de> wrote: > >> r2 wrote: > >> > I have a memory dump from a machine I am trying to analyze. I can view > >> > the file in a hex editor to see text strings in the binary code. I > >> > don't see a way to save these ascii representations of the binary, so > >> > I went digging into Python to see if there were any modules to help. > > >> > I found one I think might do what I want it to do - the binascii > >> > module. Can anyone describe to me how to convert a raw binary file to > >> > an ascii file using this module. I've tried? Boy, I've tried. > > >> That won't work because a text editor doesn't need any help to convert > >> the bytes into characters. If it expects ascii it just will be puzzled by > >> bytes that are not valid ascii. Also, it will happily display byte > >> sequences that are valid ascii, but that you as a user will see as > >> gibberish because they were meant to be binary data by the program that > >> wrote them. > > >> > Am I correct in assuming I can get the converted binary to ascii text > >> > I see in a hex editor using this module? I'm new to this forensics > >> > thing and it's quite possible I am mixing technical terms. I am not > >> > new to Python, however. Thanks for your help. > > >> Unix has the "strings" commandline tool to extract text from a binary. > >> Get hold of a copy of the MinGW tools if you are on windows. > > >> Peter > > > Okay. Thanks for the guidance. I have a machine with Linux, so I > > should be able to do what you describe above. Could Python extract the > > strings from the binary as well? Just wondering. > > As a special service for you here is a naive implementation to build upon: > > #!/usr/bin/env python > import sys > > wanted_chars = ["\0"]*256 > for i in range(32, 127): > ? ? wanted_chars[i] = chr(i) > wanted_chars[ord("\t")] = "\t" > wanted_chars = "".join(wanted_chars) > > THRESHOLD = 4 > > for s in sys.stdin.read().translate(wanted_chars).split("\0"): > ? ? if len(s) >= THRESHOLD: > ? ? ? ? print s > > Peter- Hide quoted text - > > - Show quoted text - Perfect! Thanks. From mondi at cs.unibo.it Mon Jul 27 15:35:51 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Mon, 27 Jul 2009 19:35:51 +0000 Subject: ioctl on socket Message-ID: Is there a reason why there is no ioctl interface for socket either then for windows platform? It's technical issues or what else?? thank in advance jacopo From python at mrabarnett.plus.com Mon Jul 27 16:00:48 2009 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 27 Jul 2009 21:00:48 +0100 Subject: New implementation of re module In-Reply-To: <4a6dfa5b$0$10245$426a74cc@news.free.fr> References: <4a6dfa5b$0$10245$426a74cc@news.free.fr> Message-ID: <4A6E0770.4000908@mrabarnett.plus.com> William Dode wrote: > On 27-07-2009, MRAB wrote: >> Hi all, >> >> I've been working on a new implementation of the re module. The details >> are at http://bugs.python.org/issue2636, specifically from >> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >> Python 2.6 on Windows if you want to try it out. >> > > Someone can remember me how to compile it (on debian lenny), if possible > with python2.5. I've also python3.1 that i build alone... > > I could test it with pytextile, i've a bunch of texts to bench and > compare. > All I can do is point you to http://docs.python.org/extending/extending.html. For Linux (which I don't have) you'll need to use _regex.h and _regex.c to compile to _regex.so instead of _regex.pyd. > Did you announce it on the unladen-swallow list ? They wanted to hack on > RE also... > No. I haven't subscribed to it. From davea at ieee.org Mon Jul 27 16:07:16 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 16:07:16 -0400 Subject: Convert raw binary file to ascii In-Reply-To: References: Message-ID: <4A6E08F4.8080203@ieee.org> r2 wrote: > On Jul 27, 9:06 am, Peter Otten <__pete... at web.de> wrote: > >> r2 wrote: >> >>> I have a memory dump from a machine I am trying to analyze. I can view >>> the file in a hex editor to see text strings in the binary code. I >>> don't see a way to save these ascii representations of the binary, so >>> I went digging into Python to see if there were any modules to help. >>> >>> I found one I think might do what I want it to do - the binascii >>> module. Can anyone describe to me how to convert a raw binary file to >>> an ascii file using this module. I've tried? Boy, I've tried. >>> >> That won't work because a text editor doesn't need any help to convert the >> bytes into characters. If it expects ascii it just will be puzzled by bytes >> that are not valid ascii. Also, it will happily display byte sequences that >> are valid ascii, but that you as a user will see as gibberish because they >> were meant to be binary data by the program that wrote them. >> >> >>> Am I correct in assuming I can get the converted binary to ascii text >>> I see in a hex editor using this module? I'm new to this forensics >>> thing and it's quite possible I am mixing technical terms. I am not >>> new to Python, however. Thanks for your help. >>> >> Unix has the "strings" commandline tool to extract text from a binary. >> Get hold of a copy of the MinGW tools if you are on windows. >> >> Peter >> > > Okay. Thanks for the guidance. I have a machine with Linux, so I > should be able to do what you describe above. Could Python extract the > strings from the binary as well? Just wondering. > > Yes, you could do the same thing in Python easily enough. And with the advantage that you could define your own meanings for "characters." The memory dump could be storing characters that are strictly ASCII. Or it could have EBCDIC, or UTF-8. And it could be Unicode, 16 bit or 32 bits, and big-endian or little-endian. Or the characters could be in some other format specific to a particular program. However, it's probably very useful to see what a "strings" program might look like, because you can quickly code variations on it, to suit your particular data. Something like the following (totally untested) def isprintable(char): return 0x20 <= char <= 0x7f def string(filename): data = open(filename, "rb").read() count = 0 line = "" for ch in data: if isprintable(ch): count += 1 line = line + ch else: if count > 4 : #cutoff, don't print strings smaller than this because they're probably just coincidence print line count = 0 line= "" print line Now you can change the definition of what's "printable", you can change the min-length that you care about. And of course you can fine-tune things like max-length lines and such. DaveA From davea at dejaviewphoto.com Mon Jul 27 16:14:14 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Mon, 27 Jul 2009 16:14:14 -0400 Subject: Help understanding the decisions *behind* python? - immutable objects In-Reply-To: References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: <4A6E0A96.60001@dejaviewphoto.com> Benjamin Kaplan wrote: > On Sun, Jul 26, 2009 at 2:24 PM, John Nagle wrote: > >> Beni Cherniavsky wrote: >> >>> On Jul 22, 9:36 am, Hendrik van Rooyen >>> wrote: >>> >>>> On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: >>>> >>> problem. >>> >> An interesting issue is Python objects, which are always mutable. >> A "dict" of Python objects is allowed, but doesn't consider >> the contents of the objects, just their identity (address). Only >> built-in types are immutable; one cannot create a class of immutable >> objects. >> So things like "frozenset" had to be built into the language. >> A tuple is really a frozen list. Arguably, frozen objects >> should have been a general concept. Conceptually, they're >> simple - once "__init__" has run, there can be no more changes >> to fields of the object. >> >> Compare the C++ approach, where you can have a "map" of >> any object that defines the "<" operator. Python has >> "__cmp__" for objects, but built-in dictionaries don't use it. >> Of course, one could create a "map" class in Python that >> uses "__cmp__", which would allow dictionary-type operations >> on arbitrary objects. >> > > > Python dictionaries are hash maps so they use hashes, not comparisons, > to store objects. By default the hash is equal to the object's > identity but all you need to do to change it is define your own > __hash__ method. If you were to make a C++ hash map, it wouldn't use > comparisons either. > > > > I think you'll find that to make a proper dict of user-objects, you need to define not only __hash__(), but also __eq__() methods. A hash is only a starting place. In case of collision, the dict class calls another method to make sure. And of course, for the dict to function reasonably, you have to make sure that the hash and eq functions return consistent results, at least pretending that the objects involved are immutable. DaveA From ccurvey at gmail.com Mon Jul 27 16:16:28 2009 From: ccurvey at gmail.com (Chris Curvey) Date: Mon, 27 Jul 2009 13:16:28 -0700 (PDT) Subject: PyPDF and print restrictions Message-ID: <90034475-cf07-4a57-b8cf-ec2949e0e544@d23g2000vbm.googlegroups.com> Has anyone out there been able to enforce print restrictions on a PDF document by using PyPDF? The documentation for "encrypt" states: # @param user_pwd The "user password", which allows for opening and reading # the PDF file with the restrictions provided. But there is no parameter for providing a restriction, and I can't find a reference to any kind of restriction besides this comment in the docs. Thanks in advance! From nagle at animats.com Mon Jul 27 16:24:34 2009 From: nagle at animats.com (John Nagle) Date: Mon, 27 Jul 2009 13:24:34 -0700 Subject: M2Crypto hangs on this URL In-Reply-To: <4a6deda5$0$1639$742ec2ed@news.sonic.net> References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> <4a6deda5$0$1639$742ec2ed@news.sonic.net> Message-ID: <4a6e0c53$0$1644$742ec2ed@news.sonic.net> John Nagle wrote: > John Nagle wrote: >> There's something strange about this URL: >> >> "https://sagar310.pontins.com/sraep/" >> >> It hangs Firefox 2; there's no short timeout, the web page just gets >> stuck in initial load for about ten minutes. Then >> "The connection to sagar310.pontins.com was interrupted while the page >> was loading." >> >> It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. > > Correction: Linux is still hung at 5 hours, but Windows timed out > after about 15 minutes. > Further update: M2Crypto on Windows now hung on that URL for hours. Last time, the connection apparently failed and the program got unstuck. It's waiting for something to happen inside SSL connection setup. John Nagle From djames.suhanko at gmail.com Mon Jul 27 16:25:32 2009 From: djames.suhanko at gmail.com (Djames Suhanko) Date: Mon, 27 Jul 2009 17:25:32 -0300 Subject: Discovery IP in connection Message-ID: Hello,all! I wrote a little programa that listening UDP packets. A can receive the messages, but I can't see the the origin IP. How can I to get the remote IP address in connection? My source: #!/usr/bin/env python import socket mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM ) mySocket.bind ( ( '', 514 ) ) data, addr = mySocket.recvfrom(100) print data -- Djames Suhanko LinuxUser 158.760 From 71david at libero.it Mon Jul 27 16:35:01 2009 From: 71david at libero.it (David) Date: Mon, 27 Jul 2009 22:35:01 +0200 Subject: Gracefully exiting CLI application Message-ID: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Greetings, I am writing a command line application, and I need to perform some cleaning on exit even if the process is killed. How can I do that with python? Thank you. David. From erikwickstrom at gmail.com Mon Jul 27 16:38:25 2009 From: erikwickstrom at gmail.com (erikcw) Date: Mon, 27 Jul 2009 13:38:25 -0700 (PDT) Subject: Download the "head" of a large file? Message-ID: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> I'm trying to figure out how to download just the first few lines of a large (50mb) text file form a server to save bandwidth. Can Python do this? Something like the Python equivalent of curl http://url.com/file.xml | head -c 2048 Thanks! Erik From tjreedy at udel.edu Mon Jul 27 16:47:34 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 27 Jul 2009 16:47:34 -0400 Subject: Distinguishing active generators from exhausted ones In-Reply-To: <027d38a5$0$5185$c3e8da3@news.astraweb.com> References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> <027d38a5$0$5185$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Mon, 27 Jul 2009 02:02:19 -0400, Terry Reedy wrote: > >> Steven D'Aprano wrote: >>> On Sun, 26 Jul 2009 20:10:00 -0400, Terry Reedy wrote: >>> >>>> Michal Kwiatkowski wrote: >>>> >>>>> The thing is I don't need the next item. I need to know if the >>>>> generator has stopped without invoking it. >>>> Write a one-ahead iterator class, which I have posted before, that >>>> sets .exhausted to True when next fails. >>> >>> And hope that the generator doesn't have side-effects... >> If run to exhastion, the same number of side-effects happen. The only >> difference is that they each happen once step happpen sooner. For >> reading a file that is irrelevant. Much else, and the iterator is not >> just an iterator. > > I believe the OP specifically said he needs to detect whether or not an > iterator is exhausted, without running it to exhaustion, so you shouldn't > assume that the generator has been exhausted. I believe the OP said he needs to determine whether or not an iterator (specifically generator) is exhausted without consuming an item when it is not. That is slightly different. The wrapper I suggested makes that easy. I am obviously not assuming exhaustion when there is a .exhausted True/False flag to check. There are two possible definition of 'exhausted': 1) will raise StopIteration on the next next() call; 2) has raised StopIteration at least once. The wrapper converts 2) to 1), which is to say, it obeys definition 1 once the underlying iteration has obeyed definition 2. Since it is trivial to set 'exhausted=True' in the generator user code once StopIteration has been raised (meaning 2), I presume the OP wants the predictive meaning 1). Without a iterator class wrapper, I see no way to predict what a generator will do (expecially, raise StopIteration the first time) without analyzing its code and local variable state. I said in response to YOU that once exhaustion has occurred, then the same number of side effects would have occurred. > When it comes to side-effects, timing matters. Sometimes. And I admitted that possibility (slight garbled). For example, a generator > which cleans up after it has run (deleting temporary files, closing > sockets, etc.) will leave the environment in a different state if run to > exhaustion than just before exhaustion. Even if you store the final > result in a one-ahead class, you haven't saved the environment, and that > may be significant. Of course, an eager-beaver generator written to be a good citizen might well close resources as soon as it knows *they* are exhausted, long before *it* yields the last items from the in-memory last block read. For all I know, file.readlines could do such. Assuming that is not the case, the cleanup will not happen until the what turns out to be the final item is requested from the wrapper. Once cleanup has happened, .exhausted will be set to True. If proper processing of even the last item requires that cleanup not have happened, then that and prediction of exhaustion are incompatible. One who wants both should write an iterator class instead of generator function. > (Of course, it's possible that it isn't significant. Not all differences > make a difference.) > > The best advice is, try to avoid side-effects, especially in generators. Agreed. Terry Jan Reedy From davea at ieee.org Mon Jul 27 16:53:12 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 27 Jul 2009 16:53:12 -0400 Subject: where do I put resources (images, audio files) when I wrote Python program? In-Reply-To: References: Message-ID: <4A6E13B8.4080806@ieee.org> Piotrek wrote: > Hello, > > I write a Python program. It will contain some images (in .png format), some > audio files (as .ogg) etc. Now I think where should my installer put these > files and how should I access them. What is the normal Python way of doing > that? I think about puting these files in /usr/share/myprogram and then > reading it the normal way (so the path "/usr/share/myprogram" would be just > hardwired in my program). Is it the way one usually does it in Python > program or is there any more sofisticated way? > > > I think this question is mostly independent of the issue of what's used to actually install the files. My answer is to put read-only files right with the py* files, and writable files in the user's space. In the former case, you can find the files by parsing __file__ for one of your modules. In the latter case, it's system dependent. But I'd start with a single file which is put in a conventional place (for your target OS). That will be a configuration file ("preferences files"), which specifies, among other things, where everything else will be written. Somebody else will have to tell you what the Unix convention would be. In the case of Windows, the first "file" would just be a registry entry. And I wouldn't write any of the actual files till the user had configured things, either during installation or during the first run (with a File->Preferences menu setup). From zapwireDASHgroups at yahoo.com Mon Jul 27 17:04:20 2009 From: zapwireDASHgroups at yahoo.com (Joel Koltner) Date: Mon, 27 Jul 2009 14:04:20 -0700 Subject: len() should always return something References: <24639361.post@talk.nabble.com> <7ct4b8F29mednU1@mid.uni-berlin.de> Message-ID: "Dr. Phillip M. Feldman" wrote in message news:mailman.3699.1248490256.8015.python-list at python.org... > Here's a simple-minded example: ... > This function works fine if xs is a list of floats, but not if it is single > float. It can be made to work as follows: Wow, you could substitute "Matlab" for "Python" in your post and it'd still be just as good. :-) From zuo at chopin.edu.pl Mon Jul 27 17:09:14 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Mon, 27 Jul 2009 23:09:14 +0200 Subject: Convert raw binary file to ascii In-Reply-To: References: Message-ID: Hello Friends, It's my first post to python-list, so first let me introduce myself... * my name is Jan Kaliszewski, * country -- Poland, * occupation -- composer (studied in F. Chopin Academy of Music @Warsaw) and programmer (currently in Record System company, working on Anakonda -- ERP system for big companies [developed in Python + WX + Postgres]). Now, to the matter... 27-07-2009 Grant Edwards wrote: > On 2009-07-27, r2 wrote: > >> I have a memory dump from a machine I am trying to analyze. I can view >> the file in a hex editor to see text strings in the binary code. I >> don't see a way to save these ascii representations of the binary, > > $ strings memdump.binary >memdump.strings > > $ hexdump -C memdump.binary >memdump.hex+as Do You (r2) want to do get ASCII substrings (i.e. extract only those pieces of file that consist of ASCII codes -- i.e. 7-bit values -- i.e in range 0...127), or rather "possibly readable ascii representation" of the whole file, with printable ascii characters preserved 'as is' and not-printable/non-ascii characters being replaced with their codes (e.g. with '\x...' notation). If the latter, you probably want something like this: import codecs with open('memdump.binary', 'rb') as source: with open('memdump.txt', 'w') as target: for quasiline in codecs.iterencode(source, 'string_escape'): target.write(quasiline) -- Jan Kaliszewski (zuo) From martin.hellwig at dcuktec.org Mon Jul 27 17:12:23 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Mon, 27 Jul 2009 22:12:23 +0100 Subject: M2Crypto hangs on this URL In-Reply-To: <4a6e0c53$0$1644$742ec2ed@news.sonic.net> References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> <4a6deda5$0$1639$742ec2ed@news.sonic.net> <4a6e0c53$0$1644$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > John Nagle wrote: >> John Nagle wrote: >>> There's something strange about this URL: >>> >>> "https://sagar310.pontins.com/sraep/" >>> >>> It hangs Firefox 2; there's no short timeout, the web page just gets >>> stuck in initial load for about ten minutes. Then >>> "The connection to sagar310.pontins.com was interrupted while the >>> page was loading." >>> >>> It hangs M2Crypto 0.17 on both Linux and Windows, for at least 4 hours. >> >> Correction: Linux is still hung at 5 hours, but Windows timed out >> after about 15 minutes. >> > Further update: M2Crypto on Windows now hung on that URL for hours. > Last time, > the connection apparently failed and the program got unstuck. > > It's waiting for something to happen inside SSL connection setup. > > John Nagle It looks to me like the SSL handshake is not done properly from the server side. Compare the output of: openssl s_client -host sagar310.pontins.com -port 443 -debug -showcerts -msg With (for example): openssl s_client -host www.google.com -port 443 -debug -showcerts -msg -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From shai at platonix.com Mon Jul 27 17:15:00 2009 From: shai at platonix.com (Shai) Date: Mon, 27 Jul 2009 14:15:00 -0700 (PDT) Subject: Override a method but inherit the docstring References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: On Jul 27, 5:05?pm, Jean-Michel Pichavant wrote: > Ben Finney wrote: > > > > The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly > > applicable to the ?BarGonk.frobnicate? method also. Yet in overriding > > the method, the original docstring is not associated with it. > > I've also tried within the python interpreter, and it can perfectly > solve docstring inheritance. So why would you explicitly assign > docstring to child methods ? > What do you mean by "can perfectly solve docstring inheritance" ? After the following, class Foo(object): def foo(self): "Frobber" pass class Bar(Foo): def foo(self): pass help(Bar.foo) does not display "Frobber" on my interpreter. From zuo at chopin.edu.pl Mon Jul 27 17:35:23 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Mon, 27 Jul 2009 23:35:23 +0200 Subject: Gracefully exiting CLI application In-Reply-To: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Message-ID: 27-07-2009 o 22:35:01 David <71david at libero.it> wrote: > I am writing a command line application, and I need to perform some > cleaning > on exit even if the process is killed. > How can I do that with python? See: http://docs.python.org/library/signal.html Cheers, *j -- Jan Kaliszewski (zuo) From constant.beta at gmail.com Mon Jul 27 17:37:57 2009 From: constant.beta at gmail.com (Michal Kwiatkowski) Date: Mon, 27 Jul 2009 14:37:57 -0700 (PDT) Subject: Distinguishing active generators from exhausted ones References: <736219d7-cf95-456a-a5d9-2e12acfd6635@k1g2000yqf.googlegroups.com> <877hxw4avj.fsf@benfinney.id.au> <2986f530-2195-405a-8d40-1ba15b7de6cf@d4g2000yqa.googlegroups.com> <027d0d84$0$5185$c3e8da3@news.astraweb.com> <027d38a5$0$5185$c3e8da3@news.astraweb.com> Message-ID: <47bfd80f-2f61-462e-8789-811a2c836b5e@n2g2000vba.googlegroups.com> On Jul 27, 10:47?pm, Terry Reedy wrote: > There are two possible definition of 'exhausted': 1) will raise > StopIteration on the next next() call; 2) has raised StopIteration at > least once. The wrapper converts 2) to 1), which is to say, it obeys > definition 1 once the underlying iteration has obeyed definition 2. > > Since it is trivial to set 'exhausted=True' in the generator user code > once StopIteration has been raised (meaning 2), I presume the OP wants > the predictive meaning 1). No, I meant the second meaning (i.e. generator is exhausted when it has returned instead of yielding). While, as you showed, it is trivial to create a generator that will have the "exhausted" flag, in my specific case I have no control over the user code. I have to use what the Python genobject API gives me plus the context of the trace function. Cheers, mk From ben+python at benfinney.id.au Mon Jul 27 17:40:32 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 28 Jul 2009 07:40:32 +1000 Subject: Gracefully exiting CLI application References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Message-ID: <873a8h4xf3.fsf@benfinney.id.au> David <71david at libero.it> writes: > I am writing a command line application, and I need to perform some > cleaning on exit even if the process is killed. How can I do that with > python? Write an ?exit handler? function, then use ?atexit.register? to register yours for processing whenever the program exits. -- \ ?Pinky, are you pondering what I'm pondering?? ?Wuh, I think | `\ so, Brain, but will they let the Cranberry Duchess stay in the | _o__) Lincoln Bedroom?? ?_Pinky and The Brain_ | Ben Finney From zuo at chopin.edu.pl Mon Jul 27 17:52:27 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Mon, 27 Jul 2009 23:52:27 +0200 Subject: Gracefully exiting CLI application In-Reply-To: <873a8h4xf3.fsf@benfinney.id.au> References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> <873a8h4xf3.fsf@benfinney.id.au> Message-ID: 27-07-2009 Ben Finney wrote: > David <71david at libero.it> writes: > >> I am writing a command line application, and I need to perform some >> cleaning on exit even if the process is killed. How can I do that with >> python? > > Write an ?exit handler? function, then use ?atexit.register? > to register yours for > processing whenever the program exits. Unfortunately neither sys.exitfunc nor function registered with atexit.register() are called when process is killed with signal. As I wrote, you must use signals. Though sometimes it's a good idea to combine these two techniques (i.e. signal handlers call sys.exit(), then sys.exitfunc/or function registered with atexit does the actual cleaning actions). *j -- Jan Kaliszewski (zuo) From bcharrow at csail.mit.edu Mon Jul 27 18:33:51 2009 From: bcharrow at csail.mit.edu (Ben Charrow) Date: Mon, 27 Jul 2009 18:33:51 -0400 Subject: Download the "head" of a large file? In-Reply-To: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> Message-ID: <4A6E2B4F.70102@csail.mit.edu> erikcw wrote: > ...download just the first few lines of a large (50mb) text file form a > server to save bandwidth..... Something like the Python equivalent of curl > http://url.com/file.xml | head -c 2048 If you're OK calling curl and head from within python: from subprocess import Popen, PIPE url = "http://docs.python.org/" p1 = Popen(["curl", url], stdout = PIPE, stderr = PIPE) p2 = Popen(["head", "-c", "1024"], stdin = p1.stdout, stdout = PIPE) p2.communicate()[0] If you want a pure python approach: import urllib2 url = "http://docs.python.org/" req = urllib2.Request(url) f = urllib2.urlopen(req) f.read(1024) HTH, Ben From gallium.arsenide at gmail.com Mon Jul 27 18:40:25 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Mon, 27 Jul 2009 15:40:25 -0700 (PDT) Subject: Download the "head" of a large file? References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> Message-ID: <5579245f-b8cb-44cc-9b5f-1e7009422a58@g19g2000vbi.googlegroups.com> On Jul 27, 4:38?pm, erikcw wrote: > I'm trying to figure out how to download just the first few lines of a > large (50mb) text file form a server to save bandwidth. ?Can Python do > this? > > Something like the Python equivalent of curlhttp://url.com/file.xml| > head -c 2048 urllib.urlopen gives you a file-like object, which you can then read line by line or in fixed-size chunks. For example: import urllib chunk = urllib.urlopen('http://url.com/file.xml').read(2048) At that point, chunk is just bytes, which you can write to a local file, print, or whatever it is you want. John From steve at REMOVE-THIS-cybersource.com.au Mon Jul 27 19:00:27 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 27 Jul 2009 23:00:27 GMT Subject: bad certificate error References: Message-ID: <027e226c$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 15:23:41 -0300, Gabriel Genellina wrote: > En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson > escribi?: > >> I was wondering if this is due to the server having a invalid server >> cert? If I go to this server in my browser, I get a "This server tried >> to identify itself with invalid information". Is there a way to ignore >> this issue with Python? Can I setup a trust store and add this server >> to the trust store? > > I don't see the point in trusting someone that you know is telling lies > about itself. Don't you? It's just commonsense risk assessment. It's far more likely that the server has an incorrectly setup certificate managed by an incompetent sys admin than it is that somebody is eaves- dropping on my connection to https://somerandom.site.com/something-trivial Particularly when the connection is on my own intranet and I know the sys admin in question has made a mistake and the new certificate is scheduled to be installed "some time next week". *wink* Of course, if there was sensitive information involved, or money, then I'd be more concerned. -- Steven From gherron at islandtraining.com Mon Jul 27 19:06:36 2009 From: gherron at islandtraining.com (Gary Herron) Date: Mon, 27 Jul 2009 16:06:36 -0700 Subject: initializing with empty list as default causes freaky problems In-Reply-To: References: Message-ID: <4A6E32FC.4080202@islandtraining.com> Reckoner wrote: > Hi, > > Observe the following: > > In [202]: class Foo(): > .....: def __init__(self,h=[]): > .....: self.h=h > .....: > .....: > > In [203]: f=Foo() > > In [204]: g=Foo() > > In [205]: g.h > Out[205]: [] > > In [206]: f.h > Out[206]: [] > > In [207]: f.h.append(10) > > In [208]: f.h > Out[208]: [10] > > In [209]: g.h > Out[209]: [10] > > The question is: why is g.h updated when I append to f.h? Shouldn't > g.h stay []? > > What am I missing here? > > I'm using Python 2.5.1. > > Thanks in advance. > This is a FAQ (and *very* common newbie trap)! See: http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects Gary Herron From drobinow at gmail.com Mon Jul 27 19:07:06 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 27 Jul 2009 18:07:06 -0500 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <4eb0089f0907261226q13cbfa8am9a52f35c471e44db@mail.gmail.com> Message-ID: <4eb0089f0907271607m1dd34f39qaec04a650afdd31f@mail.gmail.com> On Mon, Jul 27, 2009 at 9:49 AM, Aahz wrote: > In article , > Hendrik van Rooyen ? wrote: >>On Sunday 26 July 2009 21:26:46 David Robinow wrote: >>> >>> ?I'm a mediocre programmer. Does this mean I should switch to PHP? >> >>I have searched, but I can find nothing about this mediocre language. >> >>Could you tell us more? > > (For anyone who is confused by Hendrik's humor, he is saying that David > was referring to a programming language named "mediocre". ?English > grammar is confusing!) Well, Aahz, I think it rather rude to spoil the fun with your explanation. Just for that, I'm not going to post any mediocre code. You'll have to figure it out yourself. (Q: should the name of my favorite language be capitalized?) From roy at panix.com Mon Jul 27 19:21:43 2009 From: roy at panix.com (Roy Smith) Date: Mon, 27 Jul 2009 19:21:43 -0400 Subject: Discovery IP in connection References: Message-ID: In article , Djames Suhanko wrote: > Hello,all! > I wrote a little programa that listening UDP packets. A can receive > the messages, but I can't see the the origin IP. > How can I to get the remote IP address in connection? > > My source: > > #!/usr/bin/env python > import socket > mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM ) > mySocket.bind ( ( '', 514 ) ) > > data, addr = mySocket.recvfrom(100) > print data What happens when you print addr? From piet at cs.uu.nl Mon Jul 27 19:23:13 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 28 Jul 2009 01:23:13 +0200 Subject: Discovery IP in connection References: Message-ID: >>>>> Djames Suhanko (DS) wrote: >DS> Hello,all! >DS> I wrote a little programa that listening UDP packets. A can receive >DS> the messages, but I can't see the the origin IP. >DS> How can I to get the remote IP address in connection? What do you think the addr is for in data, addr = mySocket.recvfrom(100)? >DS> My source: >DS> #!/usr/bin/env python >DS> import socket >DS> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM ) >DS> mySocket.bind ( ( '', 514 ) ) >DS> data, addr = mySocket.recvfrom(100) >DS> print data -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From david.lyon at preisshare.net Mon Jul 27 19:53:00 2009 From: david.lyon at preisshare.net (David Lyon) Date: Mon, 27 Jul 2009 19:53:00 -0400 Subject: Using =?UTF-8?Q?easy=5Finstall=2C=20reduncant=3F?= In-Reply-To: References: Message-ID: <8ae7c63db1dfcc3bbc1619b67939a5d5@preisshare.net> On Mon, 27 Jul 2009 09:42:06 -0700 (PDT), ray wrote: > I am working on a Trac installation. I am new to Python. To install > packages, it is suggested to use setuptools. I have not understood > the directions. > > I execute ez_install.py. > > Then I attempt to execute easy_install.py setuptools-0.6c9-py2.6.egg. > There response that setuptools is already the active version in easy- > install.pth. Then: > Installing easy_install.exe script to C:\Python26\Scripts error: C: > \Python26\Scripts\Easy_install.exe: Permission denied. > > I have compared the file entries before and after this attempt and > there are no new files. Is there any problems here? What did I miss? Try using python package manager : http://sourceforge.net/projects/pythonpkgmgr/ You might find it a lot simpler. It will download and install setuptools for you if you are still having problems. David From r1chardj0n3s at gmail.com Mon Jul 27 20:34:52 2009 From: r1chardj0n3s at gmail.com (Richard Jones) Date: Tue, 28 Jul 2009 10:34:52 +1000 Subject: Next meeting: Tuesday 11th August Message-ID: <1680A969-5DD9-4A2C-BACB-9B342C44123C@gmail.com> The next meeting of the Melbourne Python Users Group will be on Tuesday the 11th of August starting at 6:30pm. We'll be meeting at Horse Bazaar again but this time we'll have use of their projector. We'll have time for several short presentations or lightning talks. Meeting details, location and talks list are at: http://wiki.python.org/moin/MelbournePUG If you've seen something cool or are doing something cool then we'd like you to tell everyone about it! Presentations could be 5 minutes or up to 15 minutes if you'd like to ramble for a bit longer. I'll be getting up to talk a bit about my experiences playing with IronPython - what's cool and what's downright odd :) If you've got an idea for a talk just add it to the wiki page. Richard From gagsl-py2 at yahoo.com.ar Mon Jul 27 20:46:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 21:46:51 -0300 Subject: bad certificate error References: <157f16f9-2172-4fd5-8ec8-8d9569a3f186@f20g2000prn.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 15:52:08 -0300, jakecjacobson escribi?: > On Jul 27, 2:23?pm, "Gabriel Genellina" > wrote: >> En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson ? >> escribi?: >> >> > I was wondering if this is due to the server having a invalid server >> > cert? ?If I go to this server in my browser, I get a "This server >> > tried to identify itself with invalid information". ?Is there a way to >> > ignore this issue with Python? ?Can I setup a trust store and add this >> > server to the trust store? >> >> I don't see the point in trusting someone that you know is telling lies >> about itself. > > It is a test box that the team I am on runs. That is why I would > trust it. I'd fix the certificate issue on the server. Or use plain HTTP if it's just for testing. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 27 22:16:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 23:16:39 -0300 Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Mon, 27 Jul 2009 20:00:27 -0300, Steven D'Aprano escribi?: > On Mon, 27 Jul 2009 15:23:41 -0300, Gabriel Genellina wrote: >> En Mon, 27 Jul 2009 12:57:40 -0300, jakecjacobson >> escribi?: >> >>> I was wondering if this is due to the server having a invalid server >>> cert? If I go to this server in my browser, I get a "This server tried >>> to identify itself with invalid information". Is there a way to ignore >>> this issue with Python? Can I setup a trust store and add this server >>> to the trust store? >> >> I don't see the point in trusting someone that you know is telling lies >> about itself. > > Don't you? It's just commonsense risk assessment. > > It's far more likely that the server has an incorrectly setup certificate > managed by an incompetent sys admin than it is that somebody is eaves- > dropping on my connection to > https://somerandom.site.com/something-trivial Fire the sys admin then :) I don't see the point on "fixing" either the Python script or httplib to accomodate for an invalid server certificate... If it's just for internal testing, I'd use HTTP instead (at least until the certificate is fixed). -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Mon Jul 27 22:16:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 27 Jul 2009 23:16:42 -0300 Subject: Download the "head" of a large file? References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> <5579245f-b8cb-44cc-9b5f-1e7009422a58@g19g2000vbi.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 19:40:25 -0300, John Yeung escribi?: > On Jul 27, 4:38?pm, erikcw wrote: >> I'm trying to figure out how to download just the first few lines of a >> large (50mb) text file form a server to save bandwidth. ?Can Python do >> this? >> >> Something like the Python equivalent of curlhttp://url.com/file.xml| >> head -c 2048 > > urllib.urlopen gives you a file-like object, which you can then read > line by line or in fixed-size chunks. For example: > > import urllib > chunk = urllib.urlopen('http://url.com/file.xml').read(2048) > > At that point, chunk is just bytes, which you can write to a local > file, print, or whatever it is you want. As the OP wants to save bandwidth, it's better to ask exactly the amount of data to read. That is, add a Range header field [1] to the request, and inspect the response for a corresponding Content-Range header [2]. py> import urllib2 py> url = "http://www.python.org/" py> req = urllib2.Request(url) py> req.add_header('Range', 'bytes=0-10239') # first 10K py> f = urllib2.urlopen(req) py> data = f.read() py> print repr(data[-30:]), len(data) '\t > just >> hardwired in my program). Is it the way one usually does it in Python >> program or is there any more sofisticated way? >> >> > My answer is to put read-only files right with the py* files, and > writable files in the user's space. In the former case, you can find > the files by parsing __file__ for one of your modules. In the latter > case, it's system dependent. For those read-only resources I'd use pkgutil.get_data (instead of manually parsing __file__): http://docs.python.org/library/pkgutil.html#pkgutil.get_data It's easier to manage when someone later decide to install the package as an egg file, or distribute it using py2exe. Quoting the documentation: """The function returns a binary string that is the contents of the specified resource. For packages located in the filesystem, which have already been imported, this is the rough equivalent of: d = os.path.dirname(sys.modules[package].__file__) data = open(os.path.join(d, resource), 'rb').read() return data """ -- Gabriel Genellina From doug.farrell at gmail.com Mon Jul 27 22:52:01 2009 From: doug.farrell at gmail.com (writeson) Date: Mon, 27 Jul 2009 19:52:01 -0700 (PDT) Subject: Extract images from PDF files Message-ID: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Hi all, I've looked around with Google quite a bit, but haven't found anything like what I'm looking for. Is there a Python library that will extract images from PDF files? My ultimate goal is to pull the images out, use the PIL library to reduce the size of the images and rebuild another PDF file that's an essentially "thumbnail" version of the original PDF file, smaller in size. We've been using imagick to extract the images, but it's difficult to script and slow to process the input PDF. Can someone suggest something better? Thanks in advance, Doug From aahz at pythoncraft.com Mon Jul 27 22:52:26 2009 From: aahz at pythoncraft.com (Aahz) Date: 27 Jul 2009 19:52:26 -0700 Subject: New implementation of re module References: Message-ID: In article , MRAB wrote: > >I've been working on a new implementation of the re module. The details >are at http://bugs.python.org/issue2636, specifically from >http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >Python 2.6 on Windows if you want to try it out. How does it handle the re module's unit tests? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From dmw at coder.cl Mon Jul 27 23:02:47 2009 From: dmw at coder.cl (Daniel Molina Wegener) Date: Mon, 27 Jul 2009 23:02:47 -0400 Subject: [ANN] pyxser-1.1r --- python xml serialization Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 Hello, I'm pleased to announce pyxser-1.1r, a Python-Object to XML serializer and deserializer. This package it's completly written in C and licensed under LGPLv3. The tested Python versions are 2.5.X and 2.7.X. * home page: http://coder.cl/software/pyxser * hosted at: http://sourceforge.net/projects/pyxser/ The current ChangeLog is as follows: - -----8<----------8<----------8<----------8<----- 1.1r (2009.05.09): Daniel Molina Wegener * Removed bugs concerning XML schema declaration for output documents. * Removed bugs concerning XML schema design for C14N serialization. * Removed exc argument from C14N serialization functions, because they have no sense. * Code cleanup with function arguments (C code). * All changes seems to be transparent for previous versions. - -----8<----------8<----------8<----------8<----- Also, I need a little bit of feedback. I want it to be able to serialize files and buffers: I've planned to allow the user to have three options: ignore files, serialize them in base64 encoding and just put the file name as string. But my question concerns how the files and buffers are deserialized: I must allow the user to dump the files on a directory, put the complete file on a buffer object, or just extract the base64 encoded block? Best regards and thanks in advance... - -- .O. | Daniel Molina Wegener | FreeBSD & Linux ..O | dmw [at] coder [dot] cl | Open Standards OOO | http://coder.cl/ | FOSS Developer -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iQIcBAEBCgAGBQJKbmpXAAoJEHxqfq6Y4O5NkEEQAJd6dXtHRWY7dqajArrNLIOn RqQhzAvzmdWHSsGe6qW3F8f56q88B2Zd4U/5d53rzgR3d+bUlhddj07VpuxWjiIs 2IbJSXIeHzwwuvcB0N236DLNtKKnN4LaB29Ua+WofPK5QZ+NXX8SQMVyjgYNqV7X F98+PXQ7BLAZ96zzdxEqnvkOww1YNbxkpIYWhqWleOmE+kPTrrGP6H5yhUdqz+W9 VJRvy5FOJAyBlJSlA7HoFULvVjUx0q2EdoIY/PmLGBj0pIcMKJ0IM6sUhSTN/bjq nwJRvcOmn1LwE8TpK9ZiOiNp3/BgRV87v8buPPx4C1Mj5e1SJ3M2pziQftRdXtIp f8fI/IDA8+ekCEvC9Rkzt+iRRxknDXe5JqpukH4uSLidKCD4GbdCGVmxC5rnIodk JcnbfTdPduV94CH2OxBjPOcuBAYEeUZ3RnQRT6LPJRxm+EnOtZ2tTnY+ByVpTN7G kGD0MYI3yPa3nX9IYK07Icp/4hC8w7EitQhhxRQD/y2NdfQELdu2IQUxag7x3G/8 bX65CUBHUxMwKoT+/HzeTRi0lGHopa6ptRxIbHg2wjHBLh7HI/s7khZtzJN8CZZE AFuxsrraUzeP2ZQ/uk0cfGPC3QBlKCYL3gksnoN4Fa5sCSX5Z+bDiKSg/xEhjC7U +HMeDE431oIJthqDqfdK =7sXN -----END PGP SIGNATURE----- From huzaigang at cn.panasonic.com Mon Jul 27 23:06:06 2009 From: huzaigang at cn.panasonic.com (jammy) Date: Tue, 28 Jul 2009 11:06:06 +0800 Subject: How to get the output struct parameter by extending C function? Message-ID: <006a01ca0f30$59849b30$af97c20a@psdcd.local> Hello, I an just starting to use the ctypes module. Now I want to expentd C in python. In the function of C, I want to get the output struct parameter which is chaged in the function. following is my code: ----------- 1.Header file of C?information.h?----------- typedef struct { char name[20]; int height; int weight; } Infor; int getInfor( char* name, int height, int weight, Infor* infor); ----------- 2.Source file of C?information.c?----------- #include #include #include #include #include "Extest.h" int getInfor( char* name, int height, int weight, Infor* infor) { if(name == NULL || infor == NULL) return (-1); printf(" [%s] get params: name[%s] height[%d] weight[%d]\n", __FUNCTION__, name, height, weight); strcpy(infor->name, name); infor->height = height; infor->weight = weight; return (1); } ----------- 3.information.c's warp file for Python calling?information_wrap.c?----------- #include "Python.h" #include "ceval.h" #include "Extest.h" static PyObject * Extest_getInfor(PyObject *self, PyObject *args) { char* name; int height; int weight; Infor* infor; int res; if(!PyArg_ParseTuple(args, "siiO:infor", &name, &height, &weight, &infor)){ return NULL; } printf(" ***** Before Call: infor.name[%s] infor.height[%d] infor.weight[%d] *****\n",infor->name, infor->height, infor->weight);//[1]initial value res = getInfor(name, height, weight, infor); printf(" ***** After Call: infor.name[%s] infor.height[%d] infor.weight[%d] *****\n", infor->name, infor->height, infor->weight);//[2]changed value return (PyObject*)Py_BuildValue("i", res); } static PyMethodDef ExtestMethods[] = { { "getInfor", Extest_getInfor, METH_VARARGS }, { NULL, NULL }, }; void initExtest(void) { PyObject *m; m = Py_InitModule("Extest", ExtestMethods); } ----------- 4.Makefile----------- # Makefile of Extend C for Python OBJS = Extest.o ExtestWrappers.o TARGET = Extest.so INCLUDES = -I /usr/local/include/python2.6/ MAKEFILE = Makefile CC = gcc CFLAGS = -fPIC -Wall -Wstrict-prototypes -Winline \ ${INCLUDES} -O2 all : ${TARGET} ${TARGET} : ${OBJS} ${LIBS} ${CC} -shared -o ${TARGET} ${OBJS} rm -f ${OBJS} clean : rm -f *.o *.Z* *~ ${TARGET} ----------- 5.test file of Python?test.py?----------- #!/usr/bin/env python2.6 #coding=utf8 import Extest from ctypes import * class Infor(Structure): _fields_= [('name', c_char*20), ('height', c_int), ('weight', c_int)] def main(): infor = Infor() infor.name = 'NoName' infor.height = -1 infor.weight = -1 name = 'TestName' height = 175 weight = 160 print 'Before call getInfor(), Infor.name=%s Infor.height=%d Infor.weight=%d' % (infor.name, infor.height, infor.weight) Extest.getInfor(name, height, weight, byref(infor)) print 'After call getInfor(), Infor.name=%s Infor.height=%d Infor.weight=%d' % (infor.name, infor.height, infor.weight) [3]want to get value of infor changed print ' ----- all DONE -----' if __name__ == '__main__': main() ----------- 6.result of running test.py ----------- [root at localhost obj_struct_test]# ./test.py Before call getInfor(), Infor.name=NoName Infor.height=-1 Infor.weight=-1 sizeof(*infor)=28, sizeof(Infor)=28 ***** [Extest_getInfor] Before Call: infor.name[] infor.height[0] infor.weight[0] ***** [getInfor] get params: name[TestName] height[175] weight[160] ***** [Extest_getInfor] After Call: infor.name[TestName] infor.height[175] infor.weight[160] ***** After call getInfor(), Infor.name=NoName Infor.height=-1 Infor.weight=-1 ----- all DONE ----- Looking for the printed information, in the file of information_wrap.c(setp 3), In the comment 's tag[2], printed value is changed valuse. Why is the printed Information initial value atfer calling getInfor() in test.py? Thanks for your help! yours: jammy From mobiledreamers at gmail.com Mon Jul 27 23:31:25 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Mon, 27 Jul 2009 20:31:25 -0700 Subject: [Baypiggies] Egnyte: hiring In-Reply-To: <20090728024611.GA4649@panix.com> References: <20090728024611.GA4649@panix.com> Message-ID: Seems interesting http://fotoroll.com/searchvideo?q=vineet%20jain,%20ceo%20of%20egnyte&id=0&type=video On Mon, Jul 27, 2009 at 7:46 PM, Aahz wrote: > My new company (I just started today) closed a big venture capital round > (also today) and is looking to hire more people, preferably with Python > experience: > > Senior Linux Engineer: > http://sfbay.craigslist.org/pen/sof/1292008176.html > > Senior Platforms Engineer: > http://sfbay.craigslist.org/pen/sof/1291776907.html > > Venture announcement: > http://blogs.zdnet.com/BTL/?p=21777 > > I can't say a lot about the company myself yet, but I've gotten a good > impression of the team and the business plan. Find out more here: > http://www.egnyte.com/ > -- > Aahz (aahz at pythoncraft.com) <*> > http://www.pythoncraft.com/ > > "Many customs in this life persist because they ease friction and promote > productivity as a result of universal agreement, and whether they are > precisely the optimal choices is much less important." --Henry Spencer > _______________________________________________ > Baypiggies mailing list > Baypiggies at python.org > To change your subscription options or unsubscribe: > http://mail.python.org/mailman/listinfo/baypiggies > -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From david.lyon at preisshare.net Mon Jul 27 23:37:39 2009 From: david.lyon at preisshare.net (David Lyon) Date: Mon, 27 Jul 2009 23:37:39 -0400 Subject: Extract images from PDF files In-Reply-To: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> References: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Message-ID: pdftohtml on sourceforge may help... On Mon, 27 Jul 2009 19:52:01 -0700 (PDT), writeson wrote: > Hi all, > > I've looked around with Google quite a bit, but haven't found anything > like what I'm looking for. Is there a Python library that will extract > images from PDF files? My ultimate goal is to pull the images out, use > the PIL library to reduce the size of the images and rebuild another > PDF file that's an essentially "thumbnail" version of the original PDF > file, smaller in size. > > We've been using imagick to extract the images, but it's difficult to > script and slow to process the input PDF. Can someone suggest > something better? > > Thanks in advance, > Doug From himanshu.garg at gmail.com Tue Jul 28 00:22:29 2009 From: himanshu.garg at gmail.com (++imanshu) Date: Mon, 27 Jul 2009 21:22:29 -0700 (PDT) Subject: _msi.Record object has no attribute 'GetString' References: <32e0b242-8635-40b5-a366-2c9967e64e1c@d4g2000yqa.googlegroups.com> Message-ID: <2392afa6-59fe-4c98-8c12-403222a8ee24@a37g2000prf.googlegroups.com> On Jul 27, 4:38?pm, "++imanshu" wrote: > The documentation (http://docs.python.org/library/msilib.html#record- > objects) for msilib mentions the GetString() method on Record objects. > However, the following snippet :- > > db = msilib.OpenDatabase(os.path.join(root, file), > msilib.MSIDBOPEN_READONLY) > view = db.OpenView('SELECT * FROM Property') > view.Execute(None) > r = view.Fetch() > print dir(r) > print r.GetString(0) > > gives me this error :- > > c:\>python msi.py > ['ClearData', 'GetFieldCount', 'SetInteger', 'SetStream', 'SetString', > __class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', > '__init__', '__new__', __reduce__', '__reduce_ex__', '__repr__', > '__setattr__', '__str__'] > Traceback (most recent call last): > ? File "msi.py", line 18, in > ? ? print r.GetString(0) > AttributeError: '_msi.Record' object has no attribute 'GetString' > > Am I missing something? Known Issue. Already fixed here http://mail.python.org/pipermail/python-bugs-list/2008-February/047136.html From gagsl-py2 at yahoo.com.ar Tue Jul 28 00:34:42 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 01:34:42 -0300 Subject: How to get the output struct parameter by extending C function? References: <006a01ca0f30$59849b30$af97c20a@psdcd.local> Message-ID: En Tue, 28 Jul 2009 00:06:06 -0300, jammy escribi?: > I an just starting to use the ctypes module. > Now I want to expentd C in python. > In the function of C, I want to get the output struct parameter which > is chaged in the function. You're mixing a C extension with a ctypes call - why is that? ctypes is mainly used when you *don't* want to actually write a C extension - if you actually write one, you don't need ctypes at all. > ----------- 1.Header file of C(information.h)----------- > typedef struct { > char name[20]; > int height; > int weight; > } Infor; > int getInfor( char* name, int height, int weight, Infor* infor); > ----------- 5.test file of Python(test.py)----------- > Extest.getInfor(name, height, weight, byref(infor)) All arguments for a function intended to be called from Python must be PyObject*; byref is used to call a ctypes-wrapped function, not a function from an extension module. (You're lucky you didn't crash the interpreter process) Either use ctypes to directly call the getInfor function in information.c (and drop the extension module), or write a complete wrapper in the extension module and forget ctypes. Don't mix both ways. -- Gabriel Genellina From doron.tal.list at gmail.com Tue Jul 28 00:37:35 2009 From: doron.tal.list at gmail.com (Doron Tal) Date: Tue, 28 Jul 2009 07:37:35 +0300 Subject: Gracefully exiting CLI application In-Reply-To: References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> <873a8h4xf3.fsf@benfinney.id.au> Message-ID: <2a8674c60907272137u6df107b7v25c73d0e4a8a9821@mail.gmail.com> On Tue, Jul 28, 2009 at 12:52 AM, Jan Kaliszewski wrote: > As I wrote, you must use signals. Though sometimes it's a good idea > to combine these two techniques (i.e. signal handlers call sys.exit(), > then sys.exitfunc/or function registered with atexit does the actual > cleaning actions). Another way of combining signals and atexit functions is to write a signal handlers which update some QUIT_APP flag, which the application polls on occasionally. This way you'll avoid deadlocks which may happen if you have a multi-threaded application, and your main thread had just acquired a lock. --doron -------------- next part -------------- An HTML attachment was scrubbed... URL: From brenNOSPAMbarn at NObrenSPAMbarn.net Tue Jul 28 00:41:34 2009 From: brenNOSPAMbarn at NObrenSPAMbarn.net (OKB (not okblacke)) Date: Tue, 28 Jul 2009 04:41:34 GMT Subject: New implementation of re module References: Message-ID: MRAB wrote: > http://bugs.python.org/issue2636#msg90954 Variable-length lookbehind! My hero! -- --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 janssen at parc.com Tue Jul 28 01:45:23 2009 From: janssen at parc.com (Bill Janssen) Date: Mon, 27 Jul 2009 22:45:23 PDT Subject: new version of SSL module on PyPI Message-ID: <2029.1248759923@parc.com> I've uploaded ssl-1.15.tgz, the backport of the 2.6/3.x SSL module to Python 2.3-2.5. It provides an option for not using SSLv2, and also fixes a bug with write retries. Bill From hekevintran at gmail.com Tue Jul 28 02:02:30 2009 From: hekevintran at gmail.com (Kevin) Date: Mon, 27 Jul 2009 23:02:30 -0700 (PDT) Subject: Error in compiling Python on OS X References: <2a083a20-ec25-4533-aa47-8a4330655ba4@o9g2000prg.googlegroups.com> <7d44lbF29p3p7U1@mid.uni-berlin.de> Message-ID: <5535080f-30ba-473c-8710-8752bf745e70@p36g2000prn.googlegroups.com> On Jul 26, 3:46?pm, "Diez B. Roggisch" wrote: > Kevin schrieb: > > > I am trying to compile Python 2.6.2 on Mac OS X 10.5.7. ?I have Xcode > > 3.1.3 installed. > > > The error I got is below. > > > $ ./configure --prefix=/Users/me/python > > Use a framework-build. > > > checking for --with-universal-archs... 32-bit > > checking MACHDEP... darwin > > checking EXTRAPLATDIR... $(PLATMACDIRS) > > checking machine type as reported by uname -m... i386 > > checking for --without-gcc... no > > checking for gcc... gcc > > checking for C compiler default output file name... > > configure: error: C compiler cannot create executables > > See `config.log' for more details. > > > Please send any ideas. > > What happens if you write a simple test.c like this: > > int main() { > ? ? return 0; > > } > > and compile it with gcc on the commandline? There must be a file called > "a.out" afterwards. > > diez I have solved my problem. I think something was wrong with my compiler. After I installed Xcode again, I was able to compile with no problems. From bcharrow at csail.mit.edu Tue Jul 28 02:18:37 2009 From: bcharrow at csail.mit.edu (Ben Charrow) Date: Tue, 28 Jul 2009 02:18:37 -0400 Subject: Download the "head" of a large file? In-Reply-To: References: <8f80931b-eb54-434f-9f80-a8eea90f3c67@y28g2000prd.googlegroups.com> Message-ID: <4A6E983D.20707@csail.mit.edu> Dennis Lee Bieber wrote: > On Mon, 27 Jul 2009 13:38:25 -0700 (PDT), erikcw > declaimed the following in > gmane.comp.python.general: >> Something like the Python equivalent of curl http://url.com/file.xml | >> head -c 2048 >> > Presuming that | is a shell pipe operation, then doesn't that > command line use "curl" to download the entire file, and "head" to > display just the first 2k? No, the entire file is not downloaded. My understanding of why this is (which could be wrong) is that the output of curl is piped to head, and once head gets the first 2k it closes the pipe. Then, when curl tries to write to the pipe again, it gets sent the SIGPIPE signal at which point it exits. Cheers, Ben From paul.paj at gmail.com Tue Jul 28 02:21:10 2009 From: paul.paj at gmail.com (Paul Johnston) Date: Mon, 27 Jul 2009 23:21:10 -0700 (PDT) Subject: Determining __name__ from the code that called a function Message-ID: Hi, In ToscaWidgets 2 experimental, when defining resources you often do something like this: CSSLink(modname=__name__, filename='static/mycss.css') Now, what I'd like to do is make the "modname=__name__" optional, to make code more concise. I figure there must be some way (using inspect or something) to determine what __name__ would be in the code that just called my function. Couldn't immediately see how to do this - any suggestions? Thanks, Paul From gagsl-py2 at yahoo.com.ar Tue Jul 28 02:45:52 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 03:45:52 -0300 Subject: What is file.encoding convention? References: <4468e223-564d-4c1f-8cd9-5338230f649a@12g2000pri.googlegroups.com> <2373a886-d577-4eab-8c8f-59acb92966e8@s31g2000yqs.googlegroups.com> <784cbf1c-db1b-4ced-8a80-8633ae943755@b15g2000yqd.googlegroups.com> Message-ID: En Thu, 23 Jul 2009 21:49:26 -0300, Naoki INADA escribi?: >>> The encoding that this file uses. When Unicode strings are written to >>> a file, >>> they will be converted to byte strings using this encoding. In >>> addition, >>> when the file is connected to a terminal, the attribute gives the >>> encoding >>> that the terminal is likely to use > > I feel this doc means "file object with encoding attribute encodes > unicode > regardless it is tty or not" and sys.stdout/stderr defies convention. The 'encoding' file attribute is very confusing and mostly unsupported (or there is a bug in the documentation, at least in Python 2.x). See http://bugs.python.org/issue4947 -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Tue Jul 28 02:46:00 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 03:46:00 -0300 Subject: exceptions.TypeError an integer is required References: <2b77d02c-e219-49c2-9929-70099ba88872@f33g2000vbm.googlegroups.com> <0279f85d$0$5185$c3e8da3@news.astraweb.com> <8d03a739-7e45-49e1-b93d-6a396beed877@a37g2000prf.googlegroups.com> Message-ID: En Mon, 27 Jul 2009 12:44:40 -0300, jakecjacobson escribi?: > > You are quite correct in your statements. My goal was not to make > great code but something that I could quickly test. My assumption was > that the httplib.HTTPSConnection() would do the cast to int for me. > As soon as I cast it to an int, I was able to get past that issue. A few remarks that may help learning the language: Note that Python is a strongly typed (and dynamic) language. All objects have a defined type: "443" is not the same thing as 443, and "2" + 2 raises a TypeError. If a function expects an integer, you must provide an integer (or something that at least "acts" as an integer; a string isn't "integer-alike" at all from Python's POV) Also, you don't "cast" an object into another: the expression int("443") is a constructor, and it returns a new object (an integer) based upon its argument. (so it's quite different from, say, casting "short" to "unsigned short" in C, that only changes the way the compiler treats the same bytes in memory). -- Gabriel Genellina From guitarzero at gmail.com Tue Jul 28 03:18:43 2009 From: guitarzero at gmail.com (guitarzero) Date: Tue, 28 Jul 2009 00:18:43 -0700 (PDT) Subject: WSDL and XML generation Message-ID: <3be39c48-6a89-4542-b08a-eaf00933b184@c1g2000yqi.googlegroups.com> Hi all Newbie in Python, i am looking for some pointers (or better existing modules) to do the followings: - generate (correct) XML messages from a WSDL file - make some modifications inside XML messages *before* sending them to the server hosting the Web services (described by previously mentionned WSDL file) - parse the answer. Some ideas ? Thanks in advance Stephane From oripel at gmail.com Tue Jul 28 03:23:10 2009 From: oripel at gmail.com (orip) Date: Tue, 28 Jul 2009 00:23:10 -0700 (PDT) Subject: Testoob: How do you use testoob.collector_from_globals / collector_from_modules? References: <1a8582cc-a7fc-4b1e-b1b2-d23e20ad6022@18g2000yqa.googlegroups.com> Message-ID: <0972bcba-a18c-4e33-9605-8955f723bd51@d32g2000yqh.googlegroups.com> Hi Harry, I forwarded this to the Testoob list at http://groups.google.com/group/testoob Ori. On Jul 27, 4:12?pm, Harry Ebbers wrote: > Hi, > > For a project I'm creating unittests using testoob. When all tests are > in a single file there is no problem > > if __name__ == '__main__': > ? ? testoob.main() > ?does the trick as usual. > > But for a number of python-applications they have asked me to group > unittests in different files, based on common functionality (e.g. GUI, > main-application, etcetera). > > Ican get it to work by importing all unittestclasses in the following > way and than use the normal if __name__ == '__main__' construction > > (from GUI import GUItests > from APP import APPtests > > if __name__ == '__main__': > ? ? testoob.main()) > > But looking at the future I would like to use testsuites. > > On the internet I found the functions testoob.collector_from_globals > and collector_from_modules which one can use to 'easily' create > testsuites which can be used with testoob. But no examples on how to > use them can be found. > > Can anybody supply me with a working example? > > TIA > > Harry Ebbers From nick at craig-wood.com Tue Jul 28 03:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Tue, 28 Jul 2009 02:29:56 -0500 Subject: bad certificate error References: Message-ID: jakecjacobson wrote: > I am getting the following error when doing a post to REST API, > > Enter PEM pass phrase: > Traceback (most recent call last): > File "./ices_catalog_feeder.py", line 193, in ? > main(sys.argv[1]) > File "./ices_catalog_feeder.py", line 60, in main > post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join > (input_dir, file), collection_name, key_file, cert_file) > File "./ices_catalog_feeder.py", line 125, in post2Catalog > connection.request('POST', path, parameters, head) > File "/usr/lib/python2.4/httplib.py", line 810, in request > self._send_request(method, url, body, headers) > File "/usr/lib/python2.4/httplib.py", line 833, in _send_request > self.endheaders() > File "/usr/lib/python2.4/httplib.py", line 804, in endheaders > self._send_output() > File "/usr/lib/python2.4/httplib.py", line 685, in _send_output > self.send(msg) > File "/usr/lib/python2.4/httplib.py", line 652, in send > self.connect() > File "/usr/lib/python2.4/httplib.py", line 1079, in connect > ssl = socket.ssl(sock, self.key_file, self.cert_file) > File "/usr/lib/python2.4/socket.py", line 74, in ssl > return _realssl(sock, keyfile, certfile) > socket.sslerror: (1, 'error:14094412:SSL > routines:SSL3_READ_BYTES:sslv3 alert bad certificate') > > > My code where this error occurs is: > > head = {"Content-Type" : "application/x-www-form-urlencoded", > "Accept" : "text/plain"} > parameters = urlencode({"collection" : collection, "entryxml" : open > (file,'r').read()}) > print "Sending the file to: " + host > > try: > try: > # Default port is 443. > # key_file is the name of a PEM formatted file that contains your > private key. > # cert_file is a PEM formatted certificate chain file. > connection = httplib.HTTPSConnection(host, int(port), key_file, > cert_file) > connection.request('POST', path, parameters, head) > response = connection.getresponse() > print response.status, response.reason > except httplib.error, (value,message): > print value + ':' + message > finally: > connection.close() > > I was wondering if this is due to the server having a invalid server > cert? I'd say judging from the traceback you messed up key_file or cert_file somehow. Try using the openssl binary on them (read the man page to see how!) to check them out. > If I go to this server in my browser, I get a "This server tried to > identify itself with invalid information". Is there a way to > ignore this issue with Python? Can I setup a trust store and add > this server to the trust store? Invalid how? Self signed certificate? Domain mismatch? Expired certificate? -- Nick Craig-Wood -- http://www.craig-wood.com/nick From mal at egenix.com Tue Jul 28 03:30:23 2009 From: mal at egenix.com (M.-A. Lemburg) Date: Tue, 28 Jul 2009 09:30:23 +0200 Subject: RSA cryptography between Python and Java In-Reply-To: References: Message-ID: <4A6EA90F.1030000@egenix.com> Rob Knop wrote: > I've created an RSA key in Java. I have exported the public key by > making it into a X509EncodedKeySpec and spitting out the result of > getEncoded(). > > I want to use this public key to encode something in python that I will > send to Java, and then decode in Java with the corresponding private > key. > > Are there any python libraries that will take a public key in this > format and do RSA encoding on it? M2Crypto will let you do this: http://chandlerproject.org/bin/view/Projects/MeTooCrypto If you have access to the raw key data (instead of having it encoded in a X509 certificate), then you can also use PyCrypto, which does require setting up OpenSSL first: http://www.dlitz.net/software/pycrypto/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Jul 28 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From gagsl-py2 at yahoo.com.ar Tue Jul 28 03:40:03 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 04:40:03 -0300 Subject: Determining __name__ from the code that called a function References: Message-ID: En Tue, 28 Jul 2009 03:21:10 -0300, Paul Johnston escribi?: > In ToscaWidgets 2 experimental, when defining resources you often do > something like this: > CSSLink(modname=__name__, filename='static/mycss.css') > > Now, what I'd like to do is make the "modname=__name__" optional, to > make code more concise. I figure there must be some way (using inspect > or something) to determine what __name__ would be in the code that > just called my function. Couldn't immediately see how to do this - any > suggestions? In CPython, you may use sys._getframe(1).f_globals['__name__'] but I don't know how portable is that across Python implementations. -- Gabriel Genellina From marco at sferacarta.com Tue Jul 28 04:18:57 2009 From: marco at sferacarta.com (Marco Mariani) Date: Tue, 28 Jul 2009 10:18:57 +0200 Subject: where do I put resources (images, audio files) when I wrote Python program? In-Reply-To: References: Message-ID: Piotrek wrote: > that? I think about puting these files in /usr/share/myprogram and then > reading it the normal way (so the path "/usr/share/myprogram" would be just > hardwired in my program). Is it the way one usually does it in Python > program or is there any more sofisticated way? Just keep them in your sources, and create an empty __init__.py file in the images directory. Then install setuptools and use the pkg_resources module. It will work even if your application is installed as an egg through easy_install, possibly zipped. To open a resource file: f = pkg_resources.resource_stream('path.to.package', 'resource.png') f.read() """ Return a readable file-like object for the specified resource; it may be an actual file, a StringIO, or some similar object. The stream is in "binary mode", in the sense that whatever bytes are in the resource will be read as-is. """ From niklasro at gmail.com Tue Jul 28 04:26:57 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 01:26:57 -0700 (PDT) Subject: The longest word Message-ID: Newbie hello onwards to the .py manual asks meantime how the longest word gets determined? First word, ok 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] 'a' rfind is another subset >>> 'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)] 'a aa aaa' One row should be able. It's a direct word 'a aa aaa...' and can be a variable, it's no big deal it occurs twice still naturally easier if also occured just once. Sincere thanks & regards Niklas From hendrik at microcorp.co.za Tue Jul 28 04:35:48 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Tue, 28 Jul 2009 10:35:48 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: Message-ID: <200907281035.48713.hendrik@microcorp.co.za> On Monday 27 July 2009 16:49:25 Aahz wrote: > In article , > > Hendrik van Rooyen wrote: > >On Sunday 26 July 2009 21:26:46 David Robinow wrote: > >> I'm a mediocre programmer. Does this mean I should switch to PHP? > > > >I have searched, but I can find nothing about this mediocre language. > > > >Could you tell us more? > > > :-P > > (For anyone who is confused by Hendrik's humor, he is saying that David > was referring to a programming language named "mediocre". English > grammar is confusing!) This is true - I intended, when I started the post, to make a crack about how he knew that he was mediocre - If there were some exam or test that you have to pass or fail to be able to make the claim to mediocrity. I was imagining a sort of devil's rating scale for programmers, that could cause one to say things like: "I am studying hard so that I can get my mediocre certificate, and one day I hope to reach hacker rank". And then the similarity to "I am a COBOL programmer" struck me, and I abandoned the ratings. - Hendrik From bearophileHUGS at lycos.com Tue Jul 28 05:02:23 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Tue, 28 Jul 2009 02:02:23 -0700 (PDT) Subject: The longest word References: Message-ID: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> On Jul 28, 10:26?am, NiklasRTZ wrote: > Newbie hello onwards to the .py manual asks meantime how the longest > word gets determined? > First word, ok > ?'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] Your language is not easy to understand, but I think you want to find the longest word in a string. If this is the input string: txt = "a aa aaa aa" There are several ways to do it, I'll show a simple one. You can split it into its parts (not having Python a built-in lazy split yet, you can split it all at once). You can do it with the string split method. It produces a list of the words, more or less (but you may have words like "gone,", you may have to take care of them too, this requires some code. Once you have a list of words, you have to take the longest. A simple way is to replace each word with a tuple that contains the length of the word and the word itself, then pick the tuple with the highest length value. But Python allows you to do such operation in a faster way: you can use the max() function, it has a "key" function that allows you to remap on the fly what you mean by "max". So you just have to give it a function (reference) that given the word spits its length, such function is "len" itself. If you instead want to find the position of the longest word the program gets a little longer. Ask if you need something different. Bye, bearophile From niklasro at gmail.com Tue Jul 28 05:22:53 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 02:22:53 -0700 (PDT) Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: On Jul 28, 5:02?am, Bearophile wrote: > On Jul 28, 10:26?am, NiklasRTZ wrote: > > > Newbie hello onwards to the .py manual asks meantime how the longest > > word gets determined? > > First word, ok > > ?'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] > > Your language is not easy to understand, but I think you want to find > the longest word in a string. > If this is the input string: > txt = "a aa aaa aa" > > There are several ways to do it, I'll show a simple one. > > You can split it into its parts (not having Python a built-in lazy > split yet, you can split it all at once). You can do it with the > string split method. It produces a list of the words, more or less > (but you may have words like "gone,", you may have to take care of > them too, this requires some code. > > Once you have a list of words, you have to take the longest. A simple > way is to replace each word with a tuple that contains the length of > the word and the word itself, then pick the tuple with the highest > length value. But Python allows you to do such operation in a faster > way: you can use the max() function, it has a "key" function that > allows you to remap on the fly what you mean by "max". So you just > have to give it a function (reference) that given the word spits its > length, such function is "len" itself. > > If you instead want to find the position of the longest word the > program gets a little longer. Ask if you need something different. > > Bye, > bearophile Thank you. This seems to work: sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- len(b))[-1] 'sdfsdfsdfsdf' From piet at cs.uu.nl Tue Jul 28 05:24:37 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 28 Jul 2009 11:24:37 +0200 Subject: The longest word References: Message-ID: >>>>> NiklasRTZ (N) wrote: >N> Newbie hello onwards to the .py manual asks meantime how the longest >N> word gets determined? >N> First word, ok >N> 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] >N> 'a' >N> rfind is another subset >>>>> 'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)] >N> 'a aa aaa' >N> One row should be able. It's a direct word 'a aa aaa...' and can be a >N> variable, it's no big deal it occurs twice still naturally easier if >N> also occured just once. >N> Sincere thanks & regards >N> Niklas 1. Is this homework? 2. It sounds very confusing. Are you trying to find the longest word in a series of words seperated by spaces? find and rfind will not help you much. You should start with var.split() if var contains the text. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From jeanmichel at sequans.com Tue Jul 28 05:33:43 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 28 Jul 2009 11:33:43 +0200 Subject: Override a method but inherit the docstring In-Reply-To: References: <87r5wggm0y.fsf@benfinney.id.au> Message-ID: <4A6EC5F7.9090402@sequans.com> Shai wrote: > On Jul 27, 5:05 pm, Jean-Michel Pichavant > wrote: > >> Ben Finney wrote: >> >>> The docstring for ?FooGonk.frobnicate? is, intentionally, perfectly >>> applicable to the ?BarGonk.frobnicate? method also. Yet in overriding >>> the method, the original docstring is not associated with it. >>> >> I've also tried within the python interpreter, and it can perfectly >> solve docstring inheritance. So why would you explicitly assign >> docstring to child methods ? >> >> > > What do you mean by "can perfectly solve docstring inheritance" ? > > After the following, > > class Foo(object): > def foo(self): > "Frobber" > pass > > class Bar(Foo): > def foo(self): > pass > > help(Bar.foo) does not display "Frobber" on my interpreter. > > You're right. I must have made some dumb mistake. So interpreters do not solve docstring inheritance, Epydoc does. JM From piet at cs.uu.nl Tue Jul 28 05:36:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 28 Jul 2009 11:36:46 +0200 Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: >>>>> NiklasRTZ (N) wrote: >N> Thank you. This seems to work: >N> sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- >N> len(b))[-1] >N> 'sdfsdfsdfsdf' simpler: sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len)[-1] -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From mondi at cs.unibo.it Tue Jul 28 06:05:43 2009 From: mondi at cs.unibo.it (jacopo mondi) Date: Tue, 28 Jul 2009 10:05:43 +0000 Subject: ioctl on socket In-Reply-To: References: Message-ID: Gabriel Genellina wrote: > En Mon, 27 Jul 2009 16:35:51 -0300, jacopo mondi > escribi?: > >> Is there a reason why there is no ioctl interface for socket either then >> for windows platform? It's technical issues or what else?? > > I don't completely understand your question. On Windows, you can use > socket.ioctl with sockets, and DeviceIoControl (from the pywin32 package) > with other files. On Linux, you have fcntl.ioctl that works with any kind > of file. > Ok, thanks a lot, my question was because I didn't know about fcntl.ioct, I thought that ioctl wrapping was implemented inside socketmodule.c as 'bind', 'connect', 'accept' etc. are, and I was disoriented because no documentation for socket usage on Linux nor the code have references about ioctl, except for windows. I hope the reason why you didn't completely understood my question is not my english, if it be so I'm sorry and I ask anyone to correct me, it's the only way to improve ;) thanks a lot jacopo From user at example.net Tue Jul 28 06:07:46 2009 From: user at example.net (superpollo) Date: Tue, 28 Jul 2009 12:07:46 +0200 Subject: Extract images from PDF files In-Reply-To: References: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Message-ID: <4a6ecdf3$0$40015$4fafbaef@reader3.news.tin.it> David Lyon wrote: > pdftohtml on sourceforge may help... > also see: http://linuxcommand.org/man_pages/pdfimages1.html bye From __peter__ at web.de Tue Jul 28 06:14:34 2009 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 Jul 2009 12:14:34 +0200 Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: NiklasRTZ wrote: > On Jul 28, 5:02 am, Bearophile wrote: >> On Jul 28, 10:26 am, NiklasRTZ wrote: >> >> > Newbie hello onwards to the .py manual asks meantime how the longest >> > word gets determined? >> > First word, ok >> > 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)] >> >> Your language is not easy to understand, but I think you want to find >> the longest word in a string. >> If this is the input string: >> txt = "a aa aaa aa" >> >> There are several ways to do it, I'll show a simple one. >> >> You can split it into its parts (not having Python a built-in lazy >> split yet, you can split it all at once). You can do it with the >> string split method. It produces a list of the words, more or less >> (but you may have words like "gone,", you may have to take care of >> them too, this requires some code. >> >> Once you have a list of words, you have to take the longest. A simple >> way is to replace each word with a tuple that contains the length of >> the word and the word itself, then pick the tuple with the highest >> length value. But Python allows you to do such operation in a faster >> way: you can use the max() function, it has a "key" function that >> allows you to remap on the fly what you mean by "max". So you just >> have to give it a function (reference) that given the word spits its >> length, such function is "len" itself. >> >> If you instead want to find the position of the longest word the >> program gets a little longer. Ask if you need something different. >> >> Bye, >> bearophile > > Thank you. This seems to work: > sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- > len(b))[-1] > 'sdfsdfsdfsdf' To spell out bearophile's more efficient suggestion: >>> max("a AAA aa aaaaa sdfsdfsdfsdf vv".split(), key=len) 'sdfsdfsdfsdf' From niklasro at gmail.com Tue Jul 28 06:23:58 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 03:23:58 -0700 (PDT) Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: <8a6313a3-5575-4195-a318-b823af0682fc@e11g2000yqo.googlegroups.com> There were 3 solutions. Everybody's homework is someone's "what's this?" max('asdasfd faop29v8 asejrhjhw awg5w35g aw3g5aw3g kajhsdkfhlskjdhfakljsdhf awg3 aw3'.split(), key=len) sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),key=len)[-1] sorted("a AAA aa aaaaa vv".split(' '),lambda a,b: len(a)-len(b))[-1] From jakecjacobson at gmail.com Tue Jul 28 06:35:55 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Tue, 28 Jul 2009 03:35:55 -0700 (PDT) Subject: bad certificate error References: Message-ID: On Jul 28, 3:29?am, Nick Craig-Wood wrote: > jakecjacobson wrote: > > ?I am getting the following error when doing a post to REST API, > > > ?Enter PEM pass phrase: > > ?Traceback (most recent call last): > > ? ?File "./ices_catalog_feeder.py", line 193, in ? > > ? ? ?main(sys.argv[1]) > > ? ?File "./ices_catalog_feeder.py", line 60, in main > > ? ? ?post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join > > ?(input_dir, file), collection_name, key_file, cert_file) > > ? ?File "./ices_catalog_feeder.py", line 125, in post2Catalog > > ? ? ?connection.request('POST', path, parameters, head) > > ? ?File "/usr/lib/python2.4/httplib.py", line 810, in request > > ? ? ?self._send_request(method, url, body, headers) > > ? ?File "/usr/lib/python2.4/httplib.py", line 833, in _send_request > > ? ? ?self.endheaders() > > ? ?File "/usr/lib/python2.4/httplib.py", line 804, in endheaders > > ? ? ?self._send_output() > > ? ?File "/usr/lib/python2.4/httplib.py", line 685, in _send_output > > ? ? ?self.send(msg) > > ? ?File "/usr/lib/python2.4/httplib.py", line 652, in send > > ? ? ?self.connect() > > ? ?File "/usr/lib/python2.4/httplib.py", line 1079, in connect > > ? ? ?ssl = socket.ssl(sock, self.key_file, self.cert_file) > > ? ?File "/usr/lib/python2.4/socket.py", line 74, in ssl > > ? ? ?return _realssl(sock, keyfile, certfile) > > ?socket.sslerror: (1, 'error:14094412:SSL > > ?routines:SSL3_READ_BYTES:sslv3 alert bad certificate') > > > ?My code where this error occurs is: > > > ?head = {"Content-Type" : "application/x-www-form-urlencoded", > > ?"Accept" : "text/plain"} > > ?parameters = urlencode({"collection" : collection, "entryxml" : open > > ?(file,'r').read()}) > > ?print "Sending the file to: " + host > > > ?try: > > ? ?try: > > ? ? ? ? ? ?# Default port is 443. > > ? ? ? ? ? ?# key_file is the name of a PEM formatted file that contains your > > ?private key. > > ? ? ? ? ? ?# cert_file is a PEM formatted certificate chain file. > > ? ? ? ? ? ?connection = httplib.HTTPSConnection(host, int(port), key_file, > > ?cert_file) > > ? ? ? ? ? ?connection.request('POST', path, parameters, head) > > ? ? ? ? ? ?response = connection.getresponse() > > ? ? ? ? ? ?print response.status, response.reason > > ? ?except httplib.error, (value,message): > > ? ? ? ? ? ?print value + ':' + message > > ?finally: > > ? ?connection.close() > > > ?I was wondering if this is due to the server having a invalid server > > ?cert? > > I'd say judging from the traceback you messed up key_file or cert_file > somehow. > > Try using the openssl binary on them (read the man page to see how!) > to check them out. > > > ?If I go to this server in my browser, I get a "This server tried to > > ?identify itself with invalid information". ?Is there a way to > > ?ignore this issue with Python? ?Can I setup a trust store and add > > ?this server to the trust store? > > Invalid how? ?Self signed certificate? Domain mismatch? Expired certificate? > > -- > Nick Craig-Wood --http://www.craig-wood.com/nick Nick, Thanks for the help on this. I will check my steps on openssl again and see if I messed up. What I tried to do was: 1. Save my PKI cert to disk. It was saved as a P12 file 2. Use openssl to convert it to the needed .pem file type 3. Saved the CA that my cert was signed by as a .crt file These are the 2 files that I was using for key_file and * cert_file -> CA * key_file -> my PKI cert converted to a .pem file "Invalid how? Self signed certificate? Domain mismatch? Expired certificate?" It is a server name mismatch. For everyone that wants to discuss why we shouldn't do this, great but I can't change the fact that I need to do this. I can't use http or even get a correct cert at this time. This is a quick a dirty project to demonstrate capability. I need something more than slide show briefs. From davea at ieee.org Tue Jul 28 06:43:16 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 28 Jul 2009 06:43:16 -0400 Subject: where do I put resources (images, audio files) when I wrote Python program? In-Reply-To: References: <4A6E13B8.4080806@ieee.org> Message-ID: <4A6ED644.7000805@ieee.org> Gabriel Genellina wrote: > For those read-only resources I'd use pkgutil.get_data (instead of > manually parsing __file__): > > http://docs.python.org/library/pkgutil.html#pkgutil.get_data > > It's easier to manage when someone later decide to install the package as > an egg file, or distribute it using py2exe. Quoting the documentation: > > """The function returns a binary string that is the contents of the > specified resource. > > For packages located in the filesystem, which have already been imported, > this is the rough equivalent of: > > d = os.path.dirname(sys.modules[package].__file__) > data = open(os.path.join(d, resource), 'rb').read() > return data > """ > Thanks Gabriel, I hadn't come across get_data() yet. DaveA From gagsl-py2 at yahoo.com.ar Tue Jul 28 06:56:27 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 28 Jul 2009 07:56:27 -0300 Subject: ioctl on socket References: Message-ID: En Tue, 28 Jul 2009 07:05:43 -0300, jacopo mondi escribi?: > Gabriel Genellina wrote: >> En Mon, 27 Jul 2009 16:35:51 -0300, jacopo mondi >> escribi?: >> >>> Is there a reason why there is no ioctl interface for socket either >>> then >>> for windows platform? It's technical issues or what else?? >> >> I don't completely understand your question. On Windows, you can use >> socket.ioctl with sockets, and DeviceIoControl (from the pywin32 >> package) >> with other files. On Linux, you have fcntl.ioctl that works with any >> kind >> of file. >> > Ok, thanks a lot, my question was because I didn't know about > fcntl.ioct, I thought that ioctl wrapping was implemented inside > socketmodule.c as 'bind', 'connect', 'accept' etc. are, and I was > disoriented because no documentation for socket usage on Linux nor the > code have references about ioctl, except for windows. I see; socket.ioctl should menction fcntl.ioctl; I'll submit a documentation patch. -- Gabriel Genellina From hniksic at xemacs.org Tue Jul 28 07:03:54 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 28 Jul 2009 13:03:54 +0200 Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> Message-ID: <87prblul0l.fsf@busola.homelinux.net> Piet van Oostrum writes: >>>>>> NiklasRTZ (N) wrote: > >>N> Thank you. This seems to work: >>N> sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- >>N> len(b))[-1] >>N> 'sdfsdfsdfsdf' > > simpler: > > sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len)[-1] There is no need to sort the sequence to obtain the largest element. The max function is designed to do exactly that, and also supports the key argument: >>> max("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len) 'sdfsdfsdfsdf' From Ray.Joseph at CDICorp.com Tue Jul 28 07:54:00 2009 From: Ray.Joseph at CDICorp.com (ray) Date: Tue, 28 Jul 2009 04:54:00 -0700 (PDT) Subject: Looking for a dream language: sounds like Python to me. References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: On Jul 27, 10:39?am, David Cournapeau wrote: > On Tue, Jul 28, 2009 at 12:28 AM, Dotan Cohen wrote: > >> It is neither efficient or inefficient: it is just a distribution > >> tool, to deploy python software in a form familiar to most windows > >> users. It does not make it any faster than running the software under > >> a python prompt. > > >> As much as I like python for scientific programming, I would say > >> python is pretty far from the stated requirements in the posted blog > >> post. It is difficult to deploy software written with python (much > >> better than the alternatives, though), and it is slow if you can't > >> leverage numpy/scipy (where vectorization does not apply). > > >> It remains to be seen whether it will be true in practice, but > >> something like F#, with its integration in VS 2010, seems much closer > >> IMHO. It is compiled, high level language, and backed by the biggest > >> software vendor in the world. > > > The blog post is not looking to distribute his code, but he would like > > it to be cross platform for his own reasons. VB is not cross platform. > > I understand his "efficient binary as Ansi C" partially as a > deployment requirement, and independent of cross-platform issues. As a > scientist, being able to share my software with colleagues is a non > trivial matter. Python does not make this easy today. > > F# has nothing to do with VB: F# is a ML-language inspired from OCAML, > and run on top of the CLR. It can thus leverage the huge .net > framework (lack of non numerical API is one of the biggest matlab > hindrance, and comparatively big advantage of python + numpy/scipy), > and benefits from the much more efficient implementation compared to > python (under the currently CPython implementation at least). > > Some recent F# versions are compatible with mono, making it compatible > on most platforms that matter today for research (but of course, you > lose the IDE integration outside windows). > > David- Hide quoted text - > > - Show quoted text - I wish . . . For comparisons, Mathcad has the symbolic notation appropriate for mathematical communications. I like features of Mathematica and Maple but Mathcad provides for the user to 'stay' with mathematical symbolism longer. I prefer Matlab execution environment. So I develop concepts in Mathcad, prove them in Matlab and then compile to through C where direct performance is required. Maple and Matlab have this type of relation. Matlab, from The Mathworks, has a companion product called Simulink. This allows the user to graphically build ?algorithms? in block form. There is a similar Python function. Each of these components would best be served if allowed to exist independently but supported with transparent integration. I would like to develop in a stable user environment - a stable GUI. And then allow efficient algorithms behind the scenes. By separating the functionality of the workspace, the user can be given (or create at will) a GUI that suites her desires and provides for the creativity and productivity she chooses. The functionality under the GUI should then be pluggable. Developers can provide solutions from many directions, compete for varying performance requirements, enhance functional features technology changes, and still not disturb the fragile user interface. Allow the user the comfort of home. Let them keep whatever GUI suits them and provide for their deployment (if any) needs behind the scenes. Ray From niklasro at gmail.com Tue Jul 28 07:55:26 2009 From: niklasro at gmail.com (NiklasRTZ) Date: Tue, 28 Jul 2009 04:55:26 -0700 (PDT) Subject: The longest word References: <99e6020c-766e-46bc-bd16-fac09112288b@c1g2000yqi.googlegroups.com> <87prblul0l.fsf@busola.homelinux.net> Message-ID: On Jul 28, 7:03?am, Hrvoje Niksic wrote: > Piet van Oostrum writes: > > >>>>>> NiklasRTZ (N) wrote: > > >>N> Thank you. This seems to work: > >>N> sorted("a AAA aa aaaaa ?sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- > >>N> len(b))[-1] > >>N> 'sdfsdfsdfsdf' > > > simpler: > > > sorted("a AAA aa aaaaa ?sdfsdfsdfsdf vv".split(' '), key=len)[-1] > > There is no need to sort the sequence to obtain the largest element. > The max function is designed to do exactly that, and also supports the > key argument: > > >>> max("a AAA aa aaaaa ?sdfsdfsdfsdf vv".split(' '), key=len) > > 'sdfsdfsdfsdf' Sincere thanks for strengthening python's superior flexibility. Same function also works around an exploding index problem returning results for longest word where otherwise a word with whitespace crashes the index: all().search(max(q.split(), key=len)).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch(PAGESIZE+1) Line below crashes the index for words with whitespace (unknown researchable, only occurs live, works with development) all().search(q).filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified").fetch (PAGESIZE+1) best regards, Niklas From tommy.nordgren at comhem.se Tue Jul 28 07:58:32 2009 From: tommy.nordgren at comhem.se (Tommy Nordgren) Date: Tue, 28 Jul 2009 13:58:32 +0200 Subject: I am trying to compile python 2.6.2 on my Mac In-Reply-To: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> References: <50697b2c0907260137m16aff26djd91bd598eea10913@mail.gmail.com> Message-ID: On Jul 26, 2009, at 10:37 AM, Chris Rebert wrote: > On Sun, Jul 26, 2009 at 1:12 AM, Jessica R > Smith wrote: >> Hello, >> I am trying to compile Python 2.6.2 on my Mac which has os/x 10.5.7 >> >> I downloaded python 2.6.2 from here: >> - http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tar.bz2 >> >> I unpacked it. >> >> I ran these shell commands: >> - ./configure --prefix=/pt/p >> - make >> >> Near the end of the make output I see this message: >> >> . Failed to find the necessary bits to build these modules: >> . _bsddb gdbm linuxaudiodev >> . ossaudiodev readline spwd >> . sunaudiodev >> . To find the necessary bits, look in setup.py in detect_modules() >> for >> the module's name. >> >> I looked in setup.py in detect_modules() >> >> It is not clear to me how to proceed. >> >> I think that I want the module: "readline" >> >> I doubt I need the other modules like linuxaudiodev, etc. >> >> If you have any clues or opinions on how I can build the module >> "readline", >> please feel free to share. > > You need to install the GNU Readline library > (http://tiswww.case.edu/php/chet/readline/rltop.html), which the > module depends on. > A better solution would be to configure the modules in question to use the libreadline.dylib library already installed on every Mac OS X computer. > You might consider installing Python through MacPorts or Fink instead, > as they automate the compilation and installation process and take > care of dependencies (such as GNU Readline) for you: > http://www.macports.org/ > http://www.finkproject.org/ > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list ----------------------------------- See the amazing new SF reel: Invasion of the man eating cucumbers from outer space. On congratulations for a fantastic parody, the producer replies : "What parody?" Tommy Nordgren tommy.nordgren at comhem.se From steve at REMOVE-THIS-cybersource.com.au Tue Jul 28 08:02:40 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 28 Jul 2009 12:02:40 GMT Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> Message-ID: <027ed9be$0$5185$c3e8da3@news.astraweb.com> On Mon, 27 Jul 2009 23:16:39 -0300, Gabriel Genellina wrote: > I don't see the point on "fixing" either the Python script or httplib to > accomodate for an invalid server certificate... If it's just for > internal testing, I'd use HTTP instead (at least until the certificate > is fixed). In real life, sometimes you need to drive with bad brakes on your car, walk down dark alleys in the bad part of town, climb a tree without a safety line, and use a hammer without wearing goggles. We can do all these things. The OP has said that, for whatever reason, he needs to ignore a bad server certificate when connecting to HTTPS. Python is a language where developers are allowed to shoot themselves in the foot, so long as they do so in full knowledge of what they're doing. So, putting aside all the millions of reasons why the OP shouldn't accept an invalid certificate, how can he accept an invalid certificate? -- Steven From nick at craig-wood.com Tue Jul 28 08:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Tue, 28 Jul 2009 07:29:56 -0500 Subject: bad certificate error References: Message-ID: jakecjacobson wrote: > > > ?If I go to this server in my browser, I get a "This server tried to > > > ?identify itself with invalid information". ?Is there a way to > > > ?ignore this issue with Python? ?Can I setup a trust store and add > > > ?this server to the trust store? > > > > Invalid how? ?Self signed certificate? Domain mismatch? Expired > > certificate? > > For everyone that wants to discuss why we shouldn't do this, great but > I can't change the fact that I need to do this. I can't use http or > even get a correct cert at this time. This is a quick a dirty project > to demonstrate capability. I need something more than slide show > briefs. I wasn't making a value judgement - I was trying to help! If you can get a bit more information out of the browser as to why the certificate is invalid it may help your python code. Real certificates cost real money. Usually a correctly set up self-signed certificate is fine for dev stuff. I'm certainly too cheap to by real certificates for dev or internal stuff! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From magobin at gmail.com Tue Jul 28 08:50:35 2009 From: magobin at gmail.com (Alex) Date: Tue, 28 Jul 2009 05:50:35 -0700 (PDT) Subject: index nested lists Message-ID: hi at all, If I have this list: >>> lista ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] if I want enumerate elements...I can see: >>> for parola in lista: print lista[i] i = i + 1 ciao 1 ['mela', 'pera', 'banana'] [1, 2, 3] >>> but, if I want to enumerate elements about nested lists ??, something like: ciao 1 mela pera banana 1 2 3 ...How can I do ?? Alex From dudekksoft at gmail.com Tue Jul 28 08:54:26 2009 From: dudekksoft at gmail.com (redhot) Date: Tue, 28 Jul 2009 05:54:26 -0700 (PDT) Subject: File and TagPy Message-ID: <11a3ce55-6ba5-4438-9b05-490686e258e6@k1g2000yqf.googlegroups.com> Hello, I have question, is it possible to read tag in tagpy module from file stream? For example I have earlier read file: file = open('some.mp3', 'rb') and next get tags from it? Regards, Konrad From bakes at ymail.com Tue Jul 28 08:59:02 2009 From: bakes at ymail.com (Bakes) Date: Tue, 28 Jul 2009 05:59:02 -0700 (PDT) Subject: FTP Offset larger than file. Message-ID: I am writing a python script that performs an identical function to the 'tail' unix utility, except that it connects to its files over FTP, rather than the local hard disk. I am currently using this python script to generate an increasing 'logfile' of garbage. > import time > for i in range(1, 20000): > time.sleep(0.2) > print i > f = open("data1.log","a") > f.write('%s: This logfile is being automatically generated to help Bakes test his python ftptail. \n' % i) > f.close() and use this script to actually download it. >import time >import os.path >from ftplib import FTP > >#Empty the file >filename = 'data1.log' >file = open(filename, 'w') >file.write('') >file.close() > >def handleDownload(block): > file.write(block) > print ".", > ># Create an instance of the FTP object ># Optionally, you could specify username and password: >ftp=FTP(host, user, pass) > >directory = '/temp' >ftp.cwd(directory) > >file = open(filename, 'a') > >for i in range(1,20000): > size=os.path.getsize('data1.log') > ftp.retrbinary('RETR ' + filename, handleDownload, rest=size) > >file.close() > >print ftp.close() Now, my problem is that I get a very strange error. What should be happening is the script gets the size of the local file before downloading all of the external file after that offset. The error I get is: ftplib.error_temp: 451-Restart offset 24576 is too large for file size 22852. 451 Restart offset reset to 0 which tells me that the local file is larger than the external file, by about a kilobyte. Certainly, the local file is indeed that size, so my local script is doing the right things. I do wonder what is going wrong, can anyone enlighten me? From npuloski at gmail.com Tue Jul 28 09:09:18 2009 From: npuloski at gmail.com (Nanime Puloski) Date: Tue, 28 Jul 2009 09:09:18 -0400 Subject: Internal Math Library of Numpy Message-ID: Does Numpy use Python's standard math library when calculating elementary functions such as exp(x) and acos(x)? Also, what is the internal library of Numpy and Python's standard math library? Is it platform independent? -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreas.tawn at ubisoft.com Tue Jul 28 09:12:41 2009 From: andreas.tawn at ubisoft.com (Andreas Tawn) Date: Tue, 28 Jul 2009 15:12:41 +0200 Subject: index nested lists In-Reply-To: References: Message-ID: <8AEDA5E3386EA742B8C24C95FF0C758007B66A65@PDC-MAIL3.ubisoft.org> > hi at all, > If I have this list: > > >>> lista > ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] > > if I want enumerate elements...I can see: > > >>> for parola in lista: > print lista[i] > i = i + 1 > > ciao > 1 > ['mela', 'pera', 'banana'] > [1, 2, 3] > >>> > > but, if I want to enumerate elements about nested lists ??, something > like: > > ciao > 1 > mela > pera > banana > 1 > 2 > 3 > > ...How can I do ?? > > Alex > -- > http://mail.python.org/mailman/listinfo/python-list You could do something like this. def printNestedList(lst): if isinstance(lst, list): for element in lst: printNestedList(element) else: print lst myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] printNestedList(myList) >>> ciao 1 mela pera banana 1 2 3 Cheers, Drea From magobin at gmail.com Tue Jul 28 09:19:03 2009 From: magobin at gmail.com (Alex) Date: Tue, 28 Jul 2009 06:19:03 -0700 (PDT) Subject: index nested lists References: Message-ID: On 28 Lug, 15:12, "Andreas Tawn" wrote: > > hi at all, > > ?If I have this list: > > > >>> lista > > ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] > > > if I want enumerate elements...I can see: > > > >>> for parola in lista: > > ? ?print lista[i] > > ? ?i = i + 1 > > > ciao > > 1 > > ['mela', 'pera', 'banana'] > > [1, 2, 3] > > > but, if I want to enumerate elements about nested lists ??, something > > like: > > > ciao > > 1 > > mela > > pera > > banana > > 1 > > 2 > > 3 > > > ...How can I do ?? > > > Alex > > -- > >http://mail.python.org/mailman/listinfo/python-list > > You could do something like this. > > def printNestedList(lst): > ? ? if isinstance(lst, list): > ? ? ? ? for element in lst: > ? ? ? ? ? ? printNestedList(element) > ? ? else: > ? ? ? ? print lst > > myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] > printNestedList(myList) > thanks a lot ! Alex From michele.petrazzo at REMOVE_me_unipex.it Tue Jul 28 09:30:49 2009 From: michele.petrazzo at REMOVE_me_unipex.it (Michele Petrazzo) Date: Tue, 28 Jul 2009 15:30:49 +0200 Subject: python 3 and stringio.seek Message-ID: Hi list, I'm trying to port a my library to python 3, but I have a problem with a the stringio.seek: the method not accept anymore a value like pos=-6 mode=1, but the "old" (2.X) version yes... The error: File "/home/devel/Py3/lib/python3.0/io.py", line 2031, in seek return self._seek(pos, whence) IOError: Can't do nonzero cur-relative seeks How solve this? Thanks, MIchele From nobody at nowhere.com Tue Jul 28 09:31:56 2009 From: nobody at nowhere.com (Nobody) Date: Tue, 28 Jul 2009 14:31:56 +0100 Subject: Gracefully exiting CLI application References: <1jh923agd88bc$.3z8qrldkivi8$.dlg@40tude.net> Message-ID: On Mon, 27 Jul 2009 22:35:01 +0200, David wrote: > I am writing a command line application, and I need to perform some > cleaning on exit even if the process is killed. How can I do that with > python? Killed by what means? Ctrl-C sends SIGINT which is converted to a KeyboardInterrupt exception. This can be caught, or if it's allowed to terminate the process, any exit handlers registered via atexit.register() will be used. For other signals, you can install a handler with signal.signal(). This can call sys.exit() or raise an exception (e.g. KeyboardInterrupt). OTOH, if the process is terminated by SIGKILL, there's nothing you can do about it. And although it's possible to trap SIGSEGV, you shouldn't assume that the Python interpreter is still functional at this point. From jeanmichel at sequans.com Tue Jul 28 09:39:56 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 28 Jul 2009 15:39:56 +0200 Subject: index nested lists In-Reply-To: References: Message-ID: <4A6EFFAC.30106@sequans.com> Alex wrote: > On 28 Lug, 15:12, "Andreas Tawn" wrote: > >>> hi at all, >>> If I have this list: >>> >>>>>> lista >>>>>> >>> ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] >>> >>> if I want enumerate elements...I can see: >>> >>>>>> for parola in lista: >>>>>> >>> print lista[i] >>> i = i + 1 >>> >>> ciao >>> 1 >>> ['mela', 'pera', 'banana'] >>> [1, 2, 3] >>> >>> but, if I want to enumerate elements about nested lists ??, something >>> like: >>> >>> ciao >>> 1 >>> mela >>> pera >>> banana >>> 1 >>> 2 >>> 3 >>> >>> ...How can I do ?? >>> >>> Alex >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >> You could do something like this. >> >> def printNestedList(lst): >> if isinstance(lst, list): >> for element in lst: >> printNestedList(element) >> else: >> print lst >> >> myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]] >> printNestedList(myList) >> >> > > > thanks a lot ! > > Alex > One hidden suggestion in Andreas answer is to write your code in english, if you can :o) JM From exarkun at divmod.com Tue Jul 28 09:48:02 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Tue, 28 Jul 2009 09:48:02 -0400 Subject: bad certificate error In-Reply-To: Message-ID: <20090728134802.11822.1316828413.divmod.quotient.0@henry.divmod.com> On Tue, 28 Jul 2009 03:35:55 -0700 (PDT), jakecjacobson wrote: > [snip] > >"Invalid how? Self signed certificate? Domain mismatch? Expired >certificate?" It is a server name mismatch. Python 2.4 is not capable of allowing you to customize this verification behavior. It is hard coded to let OpenSSL make the decision about whether to accept the certificate or not. Either M2Crypto or pyOpenSSL will let you ignore verification errors. The new ssl module in Python 2.6 may also as well. Jean-Paul From jakecjacobson at gmail.com Tue Jul 28 09:52:37 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Tue, 28 Jul 2009 06:52:37 -0700 (PDT) Subject: bad certificate error References: Message-ID: <332a5bba-48d5-4c8b-9c1e-288790d85dbb@o32g2000yqm.googlegroups.com> On Jul 28, 9:48?am, Jean-Paul Calderone wrote: > On Tue, 28 Jul 2009 03:35:55 -0700 (PDT), jakecjacobson wrote: > > [snip] > > >"Invalid how? ?Self signed certificate? Domain mismatch? Expired > >certificate?" ?It is a server name mismatch. > > Python 2.4 is not capable of allowing you to customize this verification > behavior. ?It is hard coded to let OpenSSL make the decision about whether > to accept the certificate or not. > > Either M2Crypto or pyOpenSSL will let you ignore verification errors. ?The > new ssl module in Python 2.6 may also as well. > > Jean-Paul Thanks, I will look into these suggestions. From li.weidong at gmail.com Tue Jul 28 09:54:21 2009 From: li.weidong at gmail.com (Weidong) Date: Tue, 28 Jul 2009 06:54:21 -0700 (PDT) Subject: Questions about unicodedata in python 2.6.2 Message-ID: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> I am trying to build python 2.6.2 from the source by following the instructions in README that comes with the source. The configure and make steps are fine, but there is an error in "make install" step. This "make install" attempts to build a lot of lib, and it complains about the lack of "unicodedata" shared object, I looked at the source tree and the build result, unicodedata.so does not exist. I further notice that in Modules/Setup.dist, the line for unicodedata.c is commented out by default. So I uncomment it, and experiment with rebuilding everything. This time, with or without re-configure, the "make" step failed with a link-time error when linking for libpython2.6.a due to "undefined reference to 'initunicodedata'. This symbol is defined in unicodedata.c, but unicodedata.o is not used in linking for libpython2.6.a, hence the error. So this is a problem in the generated Makefile. Does anyone know what special things I have to do to avoid such error? Is there any known problems in the configure/make files in the python 2.6.2 source code that require special patch? Is unicodedata.c really needed in python 2.6.2? If it is needed, why it is commented out by default in Modules/Setup.dist? If it is not needed, why "make install" attempts to use it in compiling the libraries? Why those libraries are not designed to be compiled in the make step, but in the install step? Thanks for your help! - Weidong From li.weidong at gmail.com Tue Jul 28 09:56:15 2009 From: li.weidong at gmail.com (Weidong) Date: Tue, 28 Jul 2009 06:56:15 -0700 (PDT) Subject: Questions about unicodedata in python 2.6.2 References: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> Message-ID: <01581b5a-3ddc-4ed8-abf4-ad41a542812c@l35g2000vba.googlegroups.com> On Jul 28, 9:54?am, Weidong wrote: > I am trying to build python 2.6.2 from the source by following the > instructions in README that comes with the source. ?The configure and > make steps are fine, but there is an error in "make install" step. > This "make install" attempts to build a lot of lib, and it complains > about the lack of "unicodedata" shared object, > > I looked at the source tree and the build result, unicodedata.so does > not exist. ?I further notice that in Modules/Setup.dist, the line for > unicodedata.c is commented out by default. ?So I uncomment it, and > experiment with rebuilding everything. > > This time, with or without re-configure, the "make" step failed with a > link-time error when linking for libpython2.6.a due to "undefined > reference to 'initunicodedata'. ?This symbol is defined in > unicodedata.c, but unicodedata.o is not used in linking for > libpython2.6.a, hence the error. ?So this is a problem in the > generated Makefile. > > Does anyone know what special things I have to do to avoid such > error? ?Is there any known problems in the configure/make files in the > python 2.6.2 source code that require special patch? > > Is unicodedata.c really needed in python 2.6.2? ?If it is needed, why > it is commented out by default in Modules/Setup.dist? ?If it is not > needed, why "make install" attempts to use it in compiling the > libraries? ?Why those libraries are not designed to be compiled in the > make step, but in the install step? > > Thanks for your help! > > - Weidong To add some info: This experiment was done on Linux, Ubuntu 8.x. From hniksic at xemacs.org Tue Jul 28 10:01:52 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 28 Jul 2009 16:01:52 +0200 Subject: FTP Offset larger than file. References: Message-ID: <87ljm8vrcf.fsf@busola.homelinux.net> Bakes writes: > The error I get is: > ftplib.error_temp: 451-Restart offset 24576 is too large for file size > 22852. > 451 Restart offset reset to 0 > which tells me that the local file is larger than the external file, > by about a kilobyte. Certainly, the local file is indeed that size, so > my local script is doing the right things. I do wonder what is going > wrong, can anyone enlighten me? I'd say you failed to take buffering into account. You write into a buffered file, yet you use os.path.getsize() to find out the current file size. If the data is not yet flushed, you keep re-reading the same stuff from the remote file, and writing it out. Once the buffer is flushed, your file will contain more data than was retrieved from the remote side, and eventually this will result in the error you see. As a quick fix, you can add a file.flush() line after the file.write(...) line, and the problem should go away. From python-url at phaseit.net Tue Jul 28 10:07:28 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Tue, 28 Jul 2009 14:07:28 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (Jul 28) Message-ID: QOTW: "But there's another principle at work here that's less well known, and that was first articulated to me by Robert Dewar: You can remove linear factors by profiling, but it's much harder to undo bad algorithmic decisions. In particular, whether a program runs in O(n) or O(n^2) sometimes depends on decisions that have to be frozen fairly early in the design." - Andrew Koenig Comparing the performance of the same algorithm using many compilers: CPython, psyco, Cython, ShedSkin, Unladen Swallow, Java, C and D: http://groups.google.com/group/comp.lang.python/browse_thread/thread/8d793c46903cc0b6/ MRAB has written a new implementation of re module with many new features, and he's looking for feedback: http://groups.google.com/group/comp.lang.python/browse_thread/thread/16c139b0a52ab023/ Importing two modules with the same name from different directories: http://groups.google.com/group/comp.lang.python/browse_thread/thread/3cb83a30e1b2c202/ Cleanly exiting an application, even if it gets killed by a signal: http://groups.google.com/group/comp.lang.python/browse_thread/thread/d60b8e0d93aeaaf9/ How can a child thread notify a parent thread of its status? http://groups.google.com/group/comp.lang.python/browse_thread/thread/d1d7f55716aacedc/ How to attach a docstring to global constants/variables? http://groups.google.com/group/comp.lang.python/browse_thread/thread/ac54186ad873036a/ In Python -unlike other languages- it does not make sense to treat numbers (scalars) as vectors of length 1: http://groups.google.com/group/comp.lang.python/browse_thread/thread/3ef498c906bd7e2d/ isinstance may take a tuple of types as its second argument: why a tuple, and not a list, or a set? http://mail.python.org/pipermail/python-list/2009-July/721205.html How to overcome the normal 2GB allocation limit of Windows XP, 32bits: http://groups.google.com/group/comp.lang.python/browse_thread/thread/59851df6e54f9ef0/ Distinguishing active generators from exhausted ones: http://groups.google.com/group/comp.lang.python/browse_thread/thread/9ff179e8cb5e9bc/ Peter Otten must be, undoubtedly, Sherlock Holmes reincarnated: http://groups.google.com/group/comp.lang.python/browse_thread/thread/838177c8a37d2b7c/ And Piet van Oostrum is not much behind him: http://groups.google.com/group/comp.lang.python/browse_thread/thread/d1f8627413cd3c4e/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiasts": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From lists at cheimes.de Tue Jul 28 10:17:20 2009 From: lists at cheimes.de (Christian Heimes) Date: Tue, 28 Jul 2009 16:17:20 +0200 Subject: Questions about unicodedata in python 2.6.2 In-Reply-To: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> References: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> Message-ID: Weidong schrieb: > I am trying to build python 2.6.2 from the source by following the > instructions in README that comes with the source. The configure and > make steps are fine, but there is an error in "make install" step. > This "make install" attempts to build a lot of lib, and it complains > about the lack of "unicodedata" shared object, unicodedata is usually build as a shared library and not linked into the Python core. How did you configure Python? The usual prodecure is: ./configure make sudo make install On Unix the preferred option for ./configure is "--enable-unicode=ucs4". Christian From gdamjan at gmail.com Tue Jul 28 10:17:45 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Tue, 28 Jul 2009 16:17:45 +0200 Subject: If Scheme is so good why MIT drops it? References: <64cf5074-485f-4e16-87a7-e7929dc346d8@v15g2000prn.googlegroups.com> Message-ID: <9td3k6-eid.ln1@archaeopteryx.softver.org.mk> > So do all these OSes have some kind of __mega_unifying_poll system > call that works for anything that might possibly block, that you can > exploit from a user process? On Linux at least, the select/poll/epoll is that system, the trick is to use eventfd, timerfd and signalfd which are Linux specific system calls. I think that covers everything needed. (eventfd is used for semaphores) -- ?????? ( http://softver.org.mk/damjan/ ) Give me the knowledge to change the code I do not accept, the wisdom not to accept the code I cannot change, and the freedom to choose my preference. From bakes at ymail.com Tue Jul 28 10:18:48 2009 From: bakes at ymail.com (Bakes) Date: Tue, 28 Jul 2009 07:18:48 -0700 (PDT) Subject: FTP Offset larger than file. References: <87ljm8vrcf.fsf@busola.homelinux.net> Message-ID: On 28 July, 15:01, Hrvoje Niksic wrote: > Bakes writes: > > The error I get is: > > ftplib.error_temp: 451-Restart offset 24576 is too large for file size > > 22852. > > 451 Restart offset reset to 0 > > which tells me that the local file is larger than the external file, > > by about a kilobyte. Certainly, the local file is indeed that size, so > > my local script is doing the right things. I do wonder what is going > > wrong, can anyone enlighten me? > > I'd say you failed to take buffering into account. ?You write into a > buffered file, yet you use os.path.getsize() to find out the current > file size. ?If the data is not yet flushed, you keep re-reading the same > stuff from the remote file, and writing it out. ?Once the buffer is > flushed, your file will contain more data than was retrieved from the > remote side, and eventually this will result in the error you see. > > As a quick fix, you can add a file.flush() line after the > file.write(...) line, and the problem should go away. Thank you very much, that worked perfectly. From bakes at ymail.com Tue Jul 28 10:33:05 2009 From: bakes at ymail.com (Bakes) Date: Tue, 28 Jul 2009 07:33:05 -0700 (PDT) Subject: FTP Offset larger than file. References: <87ljm8vrcf.fsf@busola.homelinux.net> Message-ID: On 28 July, 15:18, Bakes wrote: > On 28 July, 15:01, Hrvoje Niksic wrote: > > > > > Bakes writes: > > > The error I get is: > > > ftplib.error_temp: 451-Restart offset 24576 is too large for file size > > > 22852. > > > 451 Restart offset reset to 0 > > > which tells me that the local file is larger than the external file, > > > by about a kilobyte. Certainly, the local file is indeed that size, so > > > my local script is doing the right things. I do wonder what is going > > > wrong, can anyone enlighten me? > > > I'd say you failed to take buffering into account. ?You write into a > > buffered file, yet you use os.path.getsize() to find out the current > > file size. ?If the data is not yet flushed, you keep re-reading the same > > stuff from the remote file, and writing it out. ?Once the buffer is > > flushed, your file will contain more data than was retrieved from the > > remote side, and eventually this will result in the error you see. > > > As a quick fix, you can add a file.flush() line after the > > file.write(...) line, and the problem should go away. > > Thank you very much, that worked perfectly. Actually, no it didn't. That fix works seamlessly in Linux, but gave the same error in a Windows environment. Is that expected? From stef.mientki at gmail.com Tue Jul 28 10:35:39 2009 From: stef.mientki at gmail.com (Stef Mientki) Date: Tue, 28 Jul 2009 16:35:39 +0200 Subject: Looking for a dream language: sounds like Python to me. In-Reply-To: References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: <4A6F0CBB.8050609@gmail.com> > Matlab, from The Mathworks, has a companion product called Simulink. > This allows the user to graphically build ?algorithms? in block form. > There is a similar Python function. > Where can I find a Python functionality like simulink ? thanks, Stef Mientki From cournape at gmail.com Tue Jul 28 10:37:22 2009 From: cournape at gmail.com (David Cournapeau) Date: Tue, 28 Jul 2009 23:37:22 +0900 Subject: Internal Math Library of Numpy In-Reply-To: References: Message-ID: <5b8d13220907280737k43d2fad1k479f597e062176d2@mail.gmail.com> On Tue, Jul 28, 2009 at 10:09 PM, Nanime Puloski wrote: > Does Numpy use Python's standard math library when calculating > elementary functions such as exp(x) and acos(x)? It depends on the dtype: for fundamental types (float, int, etc...), the underlying implementation is whatever the (C) math library provides. If the functionality is not there, we have our own custom implementation (but exp and acos happen to be mandatory - if they are not detected, the build fail). > Also, what is the > internal library of Numpy and Python's standard math library? Is it > platform independent? I don't know for python, but for numpy, we have our own math library on top of whatever the platform provides (which is not much on windows, for example). There is an ongoing effort in numpy to provides a portable, pure C (independent of the python runtime) math library. The goal is to have a portable C99 math library, and more later hopefully. cheers, David From woakas at gfc.edu.co Tue Jul 28 10:50:27 2009 From: woakas at gfc.edu.co (Gustavo =?iso-8859-1?Q?Andr=E9s?= Angulo) Date: Tue, 28 Jul 2009 09:50:27 -0500 Subject: WSDL and XML generation In-Reply-To: <3be39c48-6a89-4542-b08a-eaf00933b184@c1g2000yqi.googlegroups.com> References: <3be39c48-6a89-4542-b08a-eaf00933b184@c1g2000yqi.googlegroups.com> Message-ID: <20090728145027.GA11013@gfc.edu.co> Hi you can generate the WSDl with soaplib [1], and you can view web.py or django for this: for web.py -> http://webpy.org/cookbook/webservice for django -> http://www.djangosnippets.org/snippets/979/ [1]=> http://trac.optio.webfactional.com/ > Hi all > Newbie in Python, i am looking for some pointers (or better existing > modules) to do the followings: > > - generate (correct) XML messages from a WSDL file > - make some modifications inside XML messages *before* sending them to > the server hosting the Web services (described by previously > mentionned WSDL file) > - parse the answer. > > Some ideas ? > Thanks in advance > Stephane > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: Digital signature URL: From python at mrabarnett.plus.com Tue Jul 28 10:59:06 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 28 Jul 2009 15:59:06 +0100 Subject: New implementation of re module In-Reply-To: References: Message-ID: <4A6F123A.7030002@mrabarnett.plus.com> Aahz wrote: > In article , > MRAB wrote: >> I've been working on a new implementation of the re module. The details >> are at http://bugs.python.org/issue2636, specifically from >> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >> Python 2.6 on Windows if you want to try it out. > > How does it handle the re module's unit tests? Basically, it passes all those tests I expect it to pass. :-) It fails those where the intended behaviour has changed, such as re.sub treating unmatched groups as empty strings, as requested in http://bugs.python.org/issue1519638. From python at mrabarnett.plus.com Tue Jul 28 11:11:02 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 28 Jul 2009 16:11:02 +0100 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <200907281035.48713.hendrik@microcorp.co.za> References: <200907281035.48713.hendrik@microcorp.co.za> Message-ID: <4A6F1506.9030207@mrabarnett.plus.com> Hendrik van Rooyen wrote: > On Monday 27 July 2009 16:49:25 Aahz wrote: >> In article , >> >> Hendrik van Rooyen wrote: >>> On Sunday 26 July 2009 21:26:46 David Robinow wrote: >>>> I'm a mediocre programmer. Does this mean I should switch to PHP? >>> I have searched, but I can find nothing about this mediocre language. >>> >>> Could you tell us more? >>> >> :-P >> >> (For anyone who is confused by Hendrik's humor, he is saying that David >> was referring to a programming language named "mediocre". English >> grammar is confusing!) > > This is true - I intended, when I started the post, to make a crack about > how he knew that he was mediocre - If there were some exam or test > that you have to pass or fail to be able to make the claim to mediocrity. > I was imagining a sort of devil's rating scale for programmers, that > could cause one to say things like: "I am studying hard so that I can > get my mediocre certificate, and one day I hope to reach hacker rank". > > And then the similarity to "I am a COBOL programmer" struck me, > and I abandoned the ratings. > If you were a "COBOL" programmer, would you want to shout about it? :-) From phillip.oldham at gmail.com Tue Jul 28 11:15:49 2009 From: phillip.oldham at gmail.com (Phillip B Oldham) Date: Tue, 28 Jul 2009 08:15:49 -0700 (PDT) Subject: Suggestions for Python MapReduce? References: <9786752b-4e1b-4ca2-8790-5547ba601647@t11g2000prh.googlegroups.com> <123edb2e-882d-46b7-8081-0ca5d8798aab@k6g2000yqn.googlegroups.com> <7xws60u06g.fsf@ruckus.brouhaha.com> <83889f6f-1e40-435e-b947-af17964e90af@d32g2000yqh.googlegroups.com> Message-ID: <62d0b6e4-5356-4b53-b08b-9c99933d2c0f@s31g2000yqs.googlegroups.com> In answer to my own question, I came across Octo.py[1] today. Not tested/played-with/used it yet, however, though it seems simple enough. Still welcoming any suggestions for other/similar tools. [1] http://code.google.com/p/octopy/ From dearmanzur at gmail.com Tue Jul 28 11:17:25 2009 From: dearmanzur at gmail.com (Manzur Ahmed) Date: Tue, 28 Jul 2009 11:17:25 -0400 Subject: need help using Tkinter Message-ID: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> i wanted to ask you if one of you could help me to use Tkinter. i'm trying to just create a simple GUI for which I have provided the code for below. # Simple GUI # Demonstrates creating a window from Tkinter import * # create the root window root = Tk () # modify the window root.title("Simple GUI") root.geometry("200x100") # kick off the window's event loop root.mainloop() i saved the file as both simple_gui.py and simple_gui.pyw. when i try to run simple_gui.pyw i don't get anything and when i try to run simple_gui.py i get the following: Traceback (most recent call last): File "C:\Python-Study\Chapter10\simple_gui.py", line 4, in ImportError: No module named Tkinter i tried to google search this and there were some threads that were saying to install tcl but that does not work. I did check the Lib directory under the Python directory and I did not see a Tkinter module anywhere which I think is the problem. Do you know how I would go about with getting this module and if that is not the problem or if you think it might be caused by something else, can you please give me some guidance? i even reinstalled python 3.1 which i got from www.python.org. i still do not see the Tkinter module in the lib directory. thank you for all your help. manzur -------------- next part -------------- An HTML attachment was scrubbed... URL: From hniksic at xemacs.org Tue Jul 28 11:22:38 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Tue, 28 Jul 2009 17:22:38 +0200 Subject: FTP Offset larger than file. References: <87ljm8vrcf.fsf@busola.homelinux.net> Message-ID: <873a8gg7cx.fsf@busola.homelinux.net> Bakes writes: >> > As a quick fix, you can add a file.flush() line after the >> > file.write(...) line, and the problem should go away. >> >> Thank you very much, that worked perfectly. > > Actually, no it didn't. That fix works seamlessly in Linux, but gave > the same error in a Windows environment. Is that expected? Consider opening the file in binary mode, by passing the 'wb' and 'ab' modes to open instead of 'w' and 'a' respectively. On Windows, python (and other languages) will convert '\n' to '\r\n' on write. From python at mrabarnett.plus.com Tue Jul 28 11:32:45 2009 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 28 Jul 2009 16:32:45 +0100 Subject: need help using Tkinter In-Reply-To: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> References: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> Message-ID: <4A6F1A1D.7090604@mrabarnett.plus.com> Manzur Ahmed wrote: > i wanted to ask you if one of you could help me to use Tkinter. i'm > trying to just create a simple GUI for which I have provided the code > for below. > > # Simple GUI > # Demonstrates creating a window > > from Tkinter import * > > # create the root window > root = Tk () > > # modify the window > root.title("Simple GUI") > root.geometry("200x100") > > # kick off the window's event loop > root.mainloop() > > i saved the file as both simple_gui.py and simple_gui.pyw. when i try to > run simple_gui.pyw i don't get anything and when i try to run > simple_gui.py i get the following: > Traceback (most recent call last): > File "C:\Python-Study\Chapter10\simple_gui.py", line 4, in > ImportError: No module named Tkinter > > i tried to google search this and there were some threads that were > saying to install tcl but that does not work. I did check the Lib > directory under the Python directory and I did not see a Tkinter module > anywhere which I think is the problem. Do you know how I would go about > with getting this module and if that is not the problem or if you think > it might be caused by something else, can you please give me some > guidance? i even reinstalled python 3.1 which i got from www.python.org > . i still do not see the Tkinter module in the > lib directory. thank you for all your help. > There are some differences between Python 3.x and Python 2.x. In Python 3.1 the module is called "tkinter", not "Tkinter" (names are case-sensitive). From nagle at animats.com Tue Jul 28 12:02:29 2009 From: nagle at animats.com (John Nagle) Date: Tue, 28 Jul 2009 09:02:29 -0700 Subject: M2Crypto hangs on this URL In-Reply-To: References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> <4a6deda5$0$1639$742ec2ed@news.sonic.net> <4a6e0c53$0$1644$742ec2ed@news.sonic.net> Message-ID: <4a6f2067$0$1649$742ec2ed@news.sonic.net> Martin P. Hellwig wrote: > John Nagle wrote: >> John Nagle wrote: >>> John Nagle wrote: >>>> There's something strange about this URL: >>>> >>>> "https://sagar310.pontins.com/sraep/" ... > It looks to me like the SSL handshake is not done properly from the > server side. > > Compare the output of: > openssl s_client -host sagar310.pontins.com -port 443 -debug -showcerts > -msg > > With (for example): > openssl s_client -host www.google.com -port 443 -debug -showcerts -msg OpenSSL is clearly not happy with that site. But it doesn't hang. openssl s_client -host sagar310.pontins.com -port 443 -debug -showcerts -msg eventually prints "Verify return code: 19 (self signed certificate in certificate chain)" That's weird, because there's a Verisign certificate in the chain. That site is somehow mishandling its certs. My problem, though, is that M2Crypto 0.17 is hanging for hours to days on those connections. John Nagle From li.weidong at gmail.com Tue Jul 28 12:16:57 2009 From: li.weidong at gmail.com (Weidong) Date: Tue, 28 Jul 2009 09:16:57 -0700 (PDT) Subject: Questions about unicodedata in python 2.6.2 References: <42274f5b-22a6-4449-9f8f-f4fd355c1aea@r25g2000vbn.googlegroups.com> Message-ID: <1d728013-fc77-469f-97bb-5769092377a7@p36g2000vbn.googlegroups.com> On Jul 28, 10:17?am, Christian Heimes wrote: > unicodedatais usually build as a shared library and not linked into the > Python core. How did you configure Python? The usual prodecure is: > > ? ?./configure > ? ?make > ? ?sudo make install > > On Unix the preferred option for ./configure is "--enable-unicode=ucs4". > > Christian Thanks for your response! I configured it in the same way as you said, but without "--enable-unicode=ucs4". The ocnfig.log shows that checking for UCS-4 was failed. So I assume that by default UCS-2 was used. There was no other problme in the "make" step. The problem was in the "sudo make install" step, where there were errors in building libraries / test libraries that need unicodedata.so which did not exist. I also tried to use "make -i" to let it complete the building to ignore that error. In the end, I still did not see unicodedata.so in the build result. It seems that the Makefile did not even try to build unicodedata.so. Maybe something went wrong in my configuration? - Weidong From rylesny at gmail.com Tue Jul 28 12:21:39 2009 From: rylesny at gmail.com (ryles) Date: Tue, 28 Jul 2009 09:21:39 -0700 (PDT) Subject: Wrapping prstat on Solaris References: Message-ID: <1bce2ffd-85cb-4860-8b3e-92d8e1619a46@k19g2000yqn.googlegroups.com> On Jul 27, 10:26?am, s... at pobox.com wrote: > At work we currently use top to monitor ongoing system utilization on our > Solaris systems. ?As time has moved on though, use of top has become > problematic. ?Our admins want us to switch to prstat, Sun's top-like > command. ?It works fine however doesn't emit a timestamp at each display > interval, so it's essentially impossible looking at a prstat output > file to determine when the particular "screen" was emitted. > > If figured, "No problem. ?I'll just write a little wrapper." ?Alas, that is > proving harder than I thought. ?I can run prstat directing output to a file > and it seems to blast out a block of lines every N seconds, but when run > from inside a Python script I can't seem to make the damn thing not > massively buffer its output. ?Accordingly, my script doesn't really see the > output as its emitted, so the timestamp line it prepends to a block of > output is off. > > I'm currently using subprocess.Popen like so: > > ? ? os.environ["TERM"] = "dumb" > ? ? cmd = "prstat -c %s" % " ".join(sys.argv[1:]) > ? ? pipe = Popen(cmd, shell=True, bufsize=1, stdout=PIPE).stdout > > I've tried these other variants as well: > > ? * prefacing the prstat command with unbuffer (the tcl/expect thingamabob) > > ? * explicitly redirect the prstat output to /dev/stdout > > ? * setting bufsize to 0 > > ? * used os.popen instead of Subprocess.Popen > > Nothing seems to help. ?I always seem to see about 30 seconds of data at > once (I'm currently using a 5-second interval and 10 lines of output). ?I > would have expected that prstat would simply flush stdout after each block > of output. > > Any ideas about how to get prstat to cooperate better? > > Thanks, > > -- > Skip Montanaro - s... at pobox.com -http://www.smontanaro.net/ > ? ? That's more than a dress. That's an Audrey Hepburn movie. -- Jerry Maguire Hey Skip, You haven't told us how you are actually reading from prstat's output pipe, which may be the cause. For instance, if you are doing for line in pipe: print line ... then this could cause your buffering issue. Instead try using readline(): while True: line = pipe.readline() ... From daniel.harjanto at gmail.com Tue Jul 28 12:30:26 2009 From: daniel.harjanto at gmail.com (misterdi) Date: Tue, 28 Jul 2009 09:30:26 -0700 (PDT) Subject: Python as active script The output should be something like I have tried and failed. I am reading http://www.amk.ca/python/howto/regex/ and hope to get somewhere with this soon. If this is something simple, can someone please help me out. Thanks in advance for any help. From contact at xavierho.com Tue Jul 28 19:15:34 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 09:15:34 +1000 Subject: Extract images from PDF files In-Reply-To: References: <4de4e1e5-510d-43fe-a143-8b63413df923@w6g2000yqw.googlegroups.com> Message-ID: <2d56febf0907281615g798087dcrb98b3b283befee71@mail.gmail.com> I've got a non-Python solution if you have Acrobat 6 or up. >From the menu, Advanced -> Document Processing -> Extract All Images... If you need multiple PDFs done, Batch Processing would be a great start. Then you can run another script to make the thumbnails, or use Photoshop. Either way works! Best regards, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ On Wed, Jul 29, 2009 at 6:21 AM, writeson wrote: > David, > > Thanks for your reply, I'll take a look at pdftohtml and see if it > suits my needs. > > Thanks! > Doug > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Tue Jul 28 19:19:41 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 29 Jul 2009 01:19:41 +0200 Subject: Semaphore Techniques In-Reply-To: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: <4A6F878D.6010403@cheimes.de> John D Giotta schrieb: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? Since you are talking about crontab I assume that you are on an os that supports pthreads. You problem can easily be solved with a named semaphore (see sem_open(3) and sem_overview(7)). Unfortunately Python doesn't expose named semaphores. The multiprocessing library uses named semaphores but you can't set the name yourself. You have to write your own C wrapper or search on pypi and through Google. If you are going to write your own semaphore I highly recommend Cython. Christian From philip at semanchuk.com Tue Jul 28 19:26:53 2009 From: philip at semanchuk.com (Philip Semanchuk) Date: Tue, 28 Jul 2009 19:26:53 -0400 Subject: Semaphore Techniques In-Reply-To: <4A6F878D.6010403@cheimes.de> References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> <4A6F878D.6010403@cheimes.de> Message-ID: On Jul 28, 2009, at 7:19 PM, Christian Heimes wrote: > John D Giotta schrieb: >> I'm looking to run a process with a limit of 3 instances, but each >> execution is over a crontab interval. I've been investigating the >> threading module and using daemons to limit active thread objects, >> but >> I'm not very successful at grasping the documentation. >> >> Is it possible to do what I'm trying to do and if so anyone know of a >> useful example to get started? > > Since you are talking about crontab I assume that you are on an os > that > supports pthreads. You problem can easily be solved with a named > semaphore (see sem_open(3) and sem_overview(7)). Unfortunately Python > doesn't expose named semaphores. The multiprocessing library uses > named > semaphores but you can't set the name yourself. > > You have to write your own C wrapper or search on pypi and through > Google. If you are going to write your own semaphore I highly > recommend > Cython. My POSIX IPC extension permits manipulation of interprocess semaphores: http://semanchuk.com/philip/posix_ipc/ There's also one for SysV IPC: http://semanchuk.com/philip/sysv_ipc/ Enjoy P From db3l.net at gmail.com Tue Jul 28 19:30:21 2009 From: db3l.net at gmail.com (David Bolen) Date: Tue, 28 Jul 2009 19:30:21 -0400 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: John D Giotta writes: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? Does it have to be built into the tool, or are you open to handling the restriction right in the crontab entry? For example, a crontab entry like: * * * * * test `pidof -x script.py | wc -w` -ge 4 || /script.py should attempt to run script.py every minute (adjust period as required) unless there are already four of them running. And if pidof isn't precise enough you can put anything in there that would accurately check your processes (grep a ps listing or whatever). This works because if the test expression is true it returns 0 which terminates the logical or (||) expression. There may be some variations based on cron implementation (the above was tested against Vixie cron), but some similar mechanism should be available. If you wanted to build it into the tool, it can be tricky in terms of managing shared state (the count) amongst purely sibling/cooperative processes. It's much easier to ensure no overlap (1 instance), but once you want 'n' instances you need an accurate process-wide counter. I'm not positive, but don't think Python's built-in semaphores or shared memory objects are cross-process. (Maybe something in multiprocessing in recent Python versions would work, though they may need the sharing processes to all have been executed from a parent script) I do believe there are some third party interfaces (posix_ipc, shm/shm_wrapper) that would provide access to posix shared-process objects. A semaphore may still not work as I'm not sure you can obtain the current count. But you could probably do something with a shared memory counter in conjunction with a mutex of some sort, as long as you were careful to clean it up on exit. Or, you could stick PIDs into the shared memory and count PIDs on a new startup (double checking against running processes to help protect against process failures without cleanup). You could also use the filesystem - have a shared directory where each process dumps its PID, after first counting how many other PIDs are in the directory and exiting if too many. Of course all of these (even with a PID check) are risky in the presence of unexpected failures. It would be worse with something like C code, but it should be reasonably easy to ensure that your script has cleanup code even on an unexpected termination, and it's not that likely the Python interpreter itself would crash. Then again, something external could kill the process. Ensuring accuracy and cleanup of shared state can be non-trivial. You don't mention if you can support a single master daemon, but if you could, then it can get a little easier as it can maintain and protect access to the state - you could have each worker process maintain a socket connection of some sort with the master daemon so it could detect when they terminate for the count, and it could just reject such connections from new processes if too many are running already. Of course, if the master daemon goes away then nobody would run, which may or may not be an acceptable failure mode. All in all, unless you need the scripts to enforce this behavior even in the presence of arbitrary use, I'd just use an appropriate crontab entry and move on to other problems :-) -- David From pavlovevidence at gmail.com Tue Jul 28 19:36:49 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 28 Jul 2009 16:36:49 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: On Jul 28, 3:15?pm, John D Giotta wrote: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? It seems like you want to limit the number of processes to three; the threading module won't help you there because it deals with threads within a single process. What I'd do is to simply run the system ps to see how many processes are running (ps is pretty versatile on most systems and can find specifically targeted processes like you program), and exit if there are already three. If you really are talking about multiple threads on a single server process, then you want to use a thread pool (create three threads, and give them tasks as necessary). But you'll have to have a way for the process started by crontab to communicate with the server. Carl Banks From zuo at chopin.edu.pl Tue Jul 28 20:06:32 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Wed, 29 Jul 2009 02:06:32 +0200 Subject: need help using Tkinter In-Reply-To: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> References: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> Message-ID: 28-07-2009 o 17:17 Manzur Ahmed wrote: > i wanted to ask you if one of you could help me to use Tkinter. i'm > trying > to just create a simple GUI for which I have provided the code for below. [snip] > i tried to google search this and there were some threads that were > saying > to install tcl but that does not work. I did check the Lib directory > under > the Python directory and I did not see a Tkinter module anywhere which I > think is the problem. Do you know how I would go about with getting this > module and if that is not the problem or if you think it might be caused > by > something else, can you please give me some guidance? What operating system do you use? *j -- Jan Kaliszewski (zuo) From sjmachin at lexicon.net Tue Jul 28 20:26:40 2009 From: sjmachin at lexicon.net (John Machin) Date: Tue, 28 Jul 2009 17:26:40 -0700 (PDT) Subject: xlrd - insert column? References: <75fb7538-5079-4c99-83b7-cf3afa1e222c@c29g2000yqd.googlegroups.com> Message-ID: On Jul 29, 6:05?am, LeeRisq wrote: > Does xlrd have the capability to insert a column? No. If there was anything in the PyPI description, the README, or the docs that gave you the faintest hope that xlrd does anything other than read Excel files, please raise a documentation bug-fix request. Start here: http://www.python-excel.org and read the tutorial. Cheers, John From nobody at nowhere.com Tue Jul 28 20:45:08 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 01:45:08 +0100 Subject: bad certificate error References: <332a5bba-48d5-4c8b-9c1e-288790d85dbb@o32g2000yqm.googlegroups.com> Message-ID: On Tue, 28 Jul 2009 21:44:05 +0200, Piet van Oostrum wrote: >>j> # cert_file is a PEM formatted certificate chain file. >>j> connection = httplib.HTTPSConnection(host, int(port), key_file, >>j> cert_file) > > What happens if you set cert_file to None? This would indicate that you > are not interested in the server's certificate. > > By the way, is the cert_file you supply the certificate of the CA that > signed the server's cert (in contrast to yours)? The cert_file parameter is the client certificate. You must provide either both key_file and cert_file, or neither. From nobody at nowhere.com Tue Jul 28 20:54:23 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 01:54:23 +0100 Subject: Regular expression solution References: Message-ID: On Tue, 28 Jul 2009 16:12:19 -0700, nickname wrote: > I'm a newbie with python. I am looking to write a regex > program which can take in a piece of text and remove some parts of it > and supply the remaining parts back. > > The input can be something like: > > > The output should be something like > import re r = re.compile(r'"\s*\+\s*"') s = r'''''' r.sub('', s) From python at mrabarnett.plus.com Tue Jul 28 20:58:50 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 01:58:50 +0100 Subject: New implementation of re module In-Reply-To: <4a6f5a0f$0$25500$426a74cc@news.free.fr> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <4a6f5a0f$0$25500$426a74cc@news.free.fr> Message-ID: <4A6F9ECA.3010205@mrabarnett.plus.com> William Dode wrote: > On 28-07-2009, MRAB wrote: > >> With the official Python 2.6 distribution for Mac OS X it works. >> The source code is intended to replace the current 're' module in Python >> 2.7 (and I'll be porting it to Python 3.2), so I'm not that worried >> about Python versions earlier than 2.6 for testing, although if there's >> sufficient need then I could tweak the sources for 2.5. > > I understand now why i could'nt compile it ! > > So, i would like if it's not too much work for you. > There's a new version which should compile with Python 2.5 now. From cocobear.cn at gmail.com Tue Jul 28 21:20:29 2009 From: cocobear.cn at gmail.com (cocobear) Date: Tue, 28 Jul 2009 18:20:29 -0700 (PDT) Subject: merge two png pic Message-ID: This two png file has their own palette >>> im1.mode 'P' >>> im.mode 'P' >>> im.getpalette == im1.getpalette False I can use this code to merge two png pic together: Map = Image.new("RGB", (x,y)) Map.paste(im, box) Map.paste(im1,box) Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) But if the two png pic is too big , or if I have to merge more pic together, I will get MemoryError: >>> Image.new("RGB",(44544,38656)) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in new return Image()._new(core.fill(mode, size, color)) MemoryError How can I directly merge two pic to a ?P' mode png with palette. From contact at xavierho.com Tue Jul 28 21:29:15 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 11:29:15 +1000 Subject: merge two png pic In-Reply-To: References: Message-ID: <2d56febf0907281829x4d43a1abse2c271ad857174c1@mail.gmail.com> On Wed, Jul 29, 2009 at 11:20 AM, cocobear wrote: > > >>> Image.new("RGB",(44544,38656)) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in > new > return Image()._new(core.fill(mode, size, color)) > MemoryError a 44544x38656 pixel PNG file? I don't mean to be nosy, but what is this for, if you could share? > How can I directly merge two pic to a ?P' mode png with palette. Save to disk incrementally is one way I can think of. It's a lot slower, but definitely more space than your RAM. Good luck, Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From icebergwtf at gmail.com Tue Jul 28 23:11:28 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Wed, 29 Jul 2009 11:11:28 +0800 Subject: Regular expression solution In-Reply-To: References: Message-ID: <4314c1f70907282011g799ccbf2tf44a4d4dd95dc8a7@mail.gmail.com> 2009/7/29 Nobody : >> The output should be something like >> > > import re > r = re.compile(r'"\s*\+\s*"') > s = r'''''' > r.sub('', s) > Nobody's solution is good. here is more complicated solution, just for regex practice :-) >>> s = '' >>> new_s = re.sub(r'(?<=")(.+)(?=")', ''.join(re.findall(r'"([^"]+)"', s)), s) >>> new_s '' From Brian.Mingus at colorado.edu Tue Jul 28 23:42:31 2009 From: Brian.Mingus at colorado.edu (Brian) Date: Tue, 28 Jul 2009 21:42:31 -0600 Subject: Regular expression solution In-Reply-To: <4314c1f70907282011g799ccbf2tf44a4d4dd95dc8a7@mail.gmail.com> References: <4314c1f70907282011g799ccbf2tf44a4d4dd95dc8a7@mail.gmail.com> Message-ID: <9839a05c0907282042w778caa87je53b3d34e4b00049@mail.gmail.com> On Tue, Jul 28, 2009 at 9:11 PM, tiefeng wu wrote: > 2009/7/29 Nobody : > >> The output should be something like > >> > > > > import re > > r = re.compile(r'"\s*\+\s*"') > > s = r'''''' > > r.sub('', s) > > > > Nobody's solution is good. > here is more complicated solution, just for regex practice :-) > > >>> s = '' > >>> new_s = re.sub(r'(?<=")(.+)(?=")', ''.join(re.findall(r'"([^"]+)"', > s)), s) > >>> new_s > '' > -- > http://mail.python.org/mailman/listinfo/python-list > >>> from time import time>>> import re>>> s=''>>> *# my version*>>> start=time();null=s.replace(' +','+').replace('+ ','+').replace('+','').replace('"','').replace('(','("').replace(';)','";)');*print* time()-start2.8133392334e-05>>> *# nobody's version*>>> r = re.compile(r'"\s*\+\s*"')>>> start = time();r.sub('',s);*print* time()-start3.40938568115e-05>>> *# tiefeng's version*>>> start=time();re.sub(r'(?<=")(.+)(?=")', ''.join(re.findall(r'"([^"]+)"', s)), s);*print* time()-start0.000455141067505>>> 3.40938568115e-05/2.8133392334e-051.2118644067781548>>> 0.000455141067505/2.8133392334e-0516.177966101690096>>> 0.000455141067505/3.40938568115e-0513.349650349662966 -------------- next part -------------- An HTML attachment was scrubbed... URL: From darkneter at gmail.com Tue Jul 28 23:58:53 2009 From: darkneter at gmail.com (NighterNet) Date: Tue, 28 Jul 2009 20:58:53 -0700 (PDT) Subject: simple splash screen? Message-ID: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> I am trying to make a simple splash screen from python 3.1.Not sure where to start looking for it. Can any one help? From hjtoi-better-remove-before-reply at comcast.net Wed Jul 29 00:16:38 2009 From: hjtoi-better-remove-before-reply at comcast.net (Heikki Toivonen) Date: Tue, 28 Jul 2009 21:16:38 -0700 Subject: M2Crypto hangs on this URL References: <4a6de1ef$0$1612$742ec2ed@news.sonic.net> Message-ID: John Nagle wrote: > There's something strange about this URL: > > "https://sagar310.pontins.com/sraep/" The following program finishes fine for me using M2Crypto 0.20beta1 + openssl 0.9.8g. Like Martin mentioned in another message, maybe someone fixed the site. from M2Crypto import SSL ctx = SSL.Context() # If you comment out the next 2 lines, the connection won't be secure #ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, depth=9) #if ctx.load_verify_locations('ca.pem') != 1: raise Exception('No CA certs') c = SSL.Connection(ctx) c.connect(('sagar310.pontins.com', 443)) c.send('GET /sraep/ \n') c.close() -- Heikki Toivonen - http://heikkitoivonen.net From dearmanzur at gmail.com Wed Jul 29 00:35:44 2009 From: dearmanzur at gmail.com (Manzur Ahmed) Date: Wed, 29 Jul 2009 00:35:44 -0400 Subject: need help using Tkinter In-Reply-To: References: <60972b200907280817q208e8117we76cde8f66fdf999@mail.gmail.com> Message-ID: <60972b200907282135s7fd125b4h5dc118a60027ff64@mail.gmail.com> i figured it out. its actually because i'm using python 3.1. for python 3.0+, many of the packages are lowercased, tkinter being one of them and so thats why i wan getting a module not found error . . . thanks for getting back to me. manzur On Tue, Jul 28, 2009 at 8:06 PM, Jan Kaliszewski wrote: > 28-07-2009 o 17:17 Manzur Ahmed wrote: > > i wanted to ask you if one of you could help me to use Tkinter. i'm trying >> to just create a simple GUI for which I have provided the code for below. >> > [snip] > >> i tried to google search this and there were some threads that were saying >> to install tcl but that does not work. I did check the Lib directory under >> the Python directory and I did not see a Tkinter module anywhere which I >> think is the problem. Do you know how I would go about with getting this >> module and if that is not the problem or if you think it might be caused >> by >> something else, can you please give me some guidance? >> > > What operating system do you use? > > *j > > -- > Jan Kaliszewski (zuo) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Wed Jul 29 02:08:19 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 29 Jul 2009 03:08:19 -0300 Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> <027ed9be$0$5185$c3e8da3@news.astraweb.com> Message-ID: En Tue, 28 Jul 2009 09:02:40 -0300, Steven D'Aprano escribi?: > On Mon, 27 Jul 2009 23:16:39 -0300, Gabriel Genellina wrote: > >> I don't see the point on "fixing" either the Python script or httplib to >> accomodate for an invalid server certificate... If it's just for >> internal testing, I'd use HTTP instead (at least until the certificate >> is fixed). > > In real life, sometimes you need to drive with bad brakes on your car, > walk down dark alleys in the bad part of town, climb a tree without a > safety line, and use a hammer without wearing goggles. We can do all > these things. > > The OP has said that, for whatever reason, he needs to ignore a bad > server certificate when connecting to HTTPS. Python is a language where > developers are allowed to shoot themselves in the foot, so long as they > do so in full knowledge of what they're doing. > > So, putting aside all the millions of reasons why the OP shouldn't accept > an invalid certificate, how can he accept an invalid certificate? Yes, I understand the situation, but I'm afraid there is no way (that I know of). At least not without patching _ssl.c; all the SSL negotiation is handled by the OpenSSL library itself. I vaguely remember a pure Python SSL implementation somewhere that perhaps could be hacked to bypass all controls. But making it work properly will probably require a lot more effort than installing a self signed certificate in the server... -- Gabriel Genellina From jayshree06comp at gmail.com Wed Jul 29 02:58:36 2009 From: jayshree06comp at gmail.com (jayshree) Date: Tue, 28 Jul 2009 23:58:36 -0700 (PDT) Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> <7d5brmF2a1alcU1@mid.uni-berlin.de> Message-ID: <2b5b00b8-f9ba-4d16-b950-681aa6fa010a@24g2000yqm.googlegroups.com> On Jul 27, 2:55?pm, "Diez B. Roggisch" wrote: > jayshree wrote: > > On Jul 27, 1:09?pm, Kushal Kumaran > > wrote: > >> On Mon, Jul 27, 2009 at 12:58 PM, jayshree > >> wrote: > >> > pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > >> > Please tell me how to open a file placed in any directory or in same > >> > directory. > > >> > After opening this file i want to use the contain (public key ) for > >> > encryption > > >> Does the code you've put into your message not read that file? ?If you > >> get an exception, copy-paste in the traceback message you get into > >> your mail. > > >> -- > >> kushal > > > ? ? try: > > ? ? ? ? pk = open('/home/jayshree/my_key.public.pem' , 'rb').read() > > ? ? except IOError: > > ? ? ? ? print "Error: can\'t find file or read data" > > ? ? else: > > ? ? ? ? print "reading from file successfully" > > >>Still no error it gives .what to do? > > Erm - so it doesn't give an error - which means you have successfully opened > a file, and read it's contents. > > So what exactly is your problem? > > Diez- Hide quoted text - > > - Show quoted text - > The Exact Problem is how to use the key containing by .pem file for 'encryption' . >can i print the contents of the .pem file see http://stackoverflow.com/questions/1176864/problem-with-the-pem-file From gagsl-py2 at yahoo.com.ar Wed Jul 29 02:58:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 29 Jul 2009 03:58:39 -0300 Subject: "Deprecated sets module" with Python 2.6 References: <4A6F5F59.4080507@it.uu.se> Message-ID: En Tue, 28 Jul 2009 17:28:09 -0300, Virgil Stokes escribi?: > I would appreciate help on correcting a problem when trying to create an > *.exe file using py2exe via GUI2exe with Python 2.6.2. > > When using GUI2exe to create an *.exe I always get the following warning > during the compile process: > > C:\Python26\lib\site-packages\py2exe\build_exe.py:16: > DeprecationWarning: the sets module is deprecated > import sets Note that build_exe.py is part of the py2exe package - the warning says that py2exe itself is importing a deprecated module, it's not in your code. > and this results in the creation of an *.exe file that can not be > executed. I don't think the warning above is related to your problem. Are you using the latest py2exe version? You may get more help in the py2exe mailing list; see http://www.py2exe.org/ -- Gabriel Genellina From huangchonggnu at gmail.com Wed Jul 29 03:29:35 2009 From: huangchonggnu at gmail.com (hch) Date: Wed, 29 Jul 2009 15:29:35 +0800 Subject: No subject Message-ID: Hi, everyone Is there a python script can get a list of how much stack space each function in your program uses? ( the program is compiled by gcc) Any advice will be appreciate. From vs at it.uu.se Wed Jul 29 03:46:50 2009 From: vs at it.uu.se (Virgil Stokes) Date: Wed, 29 Jul 2009 09:46:50 +0200 Subject: "Deprecated sets module" with Python 2.6 In-Reply-To: <7d99s9F28m0aaU1@mid.uni-berlin.de> References: <7d99s9F28m0aaU1@mid.uni-berlin.de> Message-ID: <4A6FFE6A.8060503@it.uu.se> Diez B. Roggisch wrote: > Virgil Stokes schrieb: >> I would appreciate help on correcting a problem when trying to create >> an *.exe file using py2exe via GUI2exe with Python 2.6.2. >> >> When using GUI2exe to create an *.exe I always get the following >> warning during the compile process: >> >> C:\Python26\lib\site-packages\py2exe\build_exe.py:16: >> DeprecationWarning: the sets module is deprecated >> import sets >> >> and this results in the creation of an *.exe file that can not be >> executed. >> >> On the other hand, if I use the same procedure (on the same Python >> code) with >> Python 2.5, there are no warnings and the *.exe works fine. >> >> The procedure used is that given in the example "Less simpler one" at >> >> http://code.google.com/p/gui2exe/wiki/GUI2ExeExamplesin >> >> Any suggestions, help, ... would be greatly appreciated. > > If you don't need your app running on python2.3 and earlier, just > remove the sets-module and replace it with the builtin "set". Of course Diez, this is a good suggestion. However, in this case the Python code that I am trying to convert into an *.exe file does not refer to sets directly; i.e, the use of this module is buried within a package that is imported (wxPython) which is not under my control. --V > > Diez From huangchonggnu at gmail.com Wed Jul 29 03:51:26 2009 From: huangchonggnu at gmail.com (hch) Date: Wed, 29 Jul 2009 15:51:26 +0800 Subject: about analyze object's stack usage Message-ID: Hi, everyone Is there a python script can get a list of how much stack space each function in your program uses? ( the program is compiled by gcc) Any advice will be appreciate. From vs at it.uu.se Wed Jul 29 03:52:57 2009 From: vs at it.uu.se (Virgil Stokes) Date: Wed, 29 Jul 2009 09:52:57 +0200 Subject: "Deprecated sets module" with Python 2.6 In-Reply-To: <4A6FFE6A.8060503@it.uu.se> References: <7d99s9F28m0aaU1@mid.uni-berlin.de> <4A6FFE6A.8060503@it.uu.se> Message-ID: <4A6FFFD9.4050508@it.uu.se> Virgil Stokes wrote: > Diez B. Roggisch wrote: >> Virgil Stokes schrieb: >>> I would appreciate help on correcting a problem when trying to >>> create an *.exe file using py2exe via GUI2exe with Python 2.6.2. >>> >>> When using GUI2exe to create an *.exe I always get the following >>> warning during the compile process: >>> >>> C:\Python26\lib\site-packages\py2exe\build_exe.py:16: >>> DeprecationWarning: the sets module is deprecated >>> import sets >>> >>> and this results in the creation of an *.exe file that can not be >>> executed. >>> >>> On the other hand, if I use the same procedure (on the same Python >>> code) with >>> Python 2.5, there are no warnings and the *.exe works fine. >>> >>> The procedure used is that given in the example "Less simpler one" at >>> >>> http://code.google.com/p/gui2exe/wiki/GUI2ExeExamplesin >>> >>> Any suggestions, help, ... would be greatly appreciated. >> >> If you don't need your app running on python2.3 and earlier, just >> remove the sets-module and replace it with the builtin "set". > Of course Diez, this is a good suggestion. However, in this case the > Python code that I am trying to convert into an *.exe file does not > refer to sets directly; i.e, the use of this module is buried within a > package that is imported (wxPython) which is not under my control. > > --V >> >> Diez > > Whoops, the reference to the module sets is in py2exe not wxPython. --V From hendrik at microcorp.co.za Wed Jul 29 04:08:24 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Wed, 29 Jul 2009 10:08:24 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <4A6F1506.9030207@mrabarnett.plus.com> References: <200907281035.48713.hendrik@microcorp.co.za> <4A6F1506.9030207@mrabarnett.plus.com> Message-ID: <200907291008.24572.hendrik@microcorp.co.za> On Tuesday 28 July 2009 17:11:02 MRAB wrote: > If you were a "COBOL" programmer, would you want to shout about it? :-) Hey don't knock it! - at the time, it was either COBOL or FORTRAN or some assembler or coding in hex or octal. And if code is data, where is Pythons ALTER statement? *Ducks* :-) Hendrik From milesck at umich.edu Wed Jul 29 04:14:03 2009 From: milesck at umich.edu (Miles Kaufmann) Date: Wed, 29 Jul 2009 01:14:03 -0700 Subject: python 3 and stringio.seek In-Reply-To: References: Message-ID: On Jul 28, 2009, at 6:30 AM, Michele Petrazzo wrote: > Hi list, > I'm trying to port a my library to python 3, but I have a problem > with a > the stringio.seek: > the method not accept anymore a value like pos=-6 mode=1, but the > "old" > (2.X) version yes... > > The error: > File "/home/devel/Py3/lib/python3.0/io.py", line 2031, in seek > return self._seek(pos, whence) > IOError: Can't do nonzero cur-relative seeks > > > How solve this? In Python 2, StringIO is a stream of bytes (non-Unicode characters). In Python 3, StringIO is a stream of text (Unicode characters). In the early development of Python 3 (and 3.1's _pyio), it was implemented as a TextIOWrapper over a BytesIO buffer. TextIOWrapper does not support relative seeks because it is difficult to map the concept of a "current position" between bytes and the text that it encodes, especially with variable-width encodings and other considerations. Furthermore, the value returned from TextIOWrapper.tell isn't just a file position but a "cookie" that contains other data necessary to restore the decoding mechanism to the same state. However, for the default encoding (utf-8), the current position is equivalent to that of the underlying bytes buffer. In Python 3, StringIO is implemented using an internal buffer of Unicode characters. There is no technical reason why it can't support relative seeks; I assume it does not for compatibility with the original Python TextIOWrapper implementation (which is present in 3.1's _pyio, but not in 3.0). Note that because of the different implementations, StringIO.tell() (and seek) behaves differently for the C and Python implementations: $ python3.1 >>> import io, _pyio >>> s = io.StringIO('\u263A'); s.read(1), s.tell() ('?', 1) >>> s = _pyio.StringIO('\u263A'); s.read(1), s.tell() ('?', 3) The end result seems to be that, for text streams (including StreamIO), you *should* treat the value returned by tell() as an opaque magic cookie, and *only* pass values to seek() that you have obtained from a previous tell() call. However, in practice, it appears that you *may* seek StringIO objects relatively by characters using s.seek(s.tell() + n), so long as you do not use the _pyio.StringIO implementation. If what you actually want is a stream of bytes, use BytesIO, which may be seeked (sought?) however you please. I'm basing this all on my reading of the Python source (and svn history), since it doesn't seem to be documented, so take it with a grain of salt. -Miles From gregor.thalhammer at gmail.com Wed Jul 29 04:14:31 2009 From: gregor.thalhammer at gmail.com (gregorth) Date: Wed, 29 Jul 2009 01:14:31 -0700 (PDT) Subject: fast video encoding Message-ID: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Hi all, for a scientific application I need to save a video stream to disc for further post processing. My cam can deliver 8bit grayscale images with resolution 640x480 with a framerate up to 100Hz, this is a data rate of 30MB/s. Writing the data uncompressed to disc hits the data transfer limits of my current system and creates huge files. Therefore I would like to use video compression, preferably fast and high quality to lossless encoding. Final file size is not that important. Because of the hardware I am bound to WinXP. I already tried pymedia for encoding to mpeg2, however I only managed to get a framerate of about 30-40fps (on a 1.8GHz dual core). There is still room for improvements in my code, but before trying hard I want to ask for advices or other possibilities. I also found gstreamer with pygst python bindings, which seems to be more modern (and performant?) package than pymedia. I did not yet try it, especially since I didn't find a simple code example of how to use it for my use case. Can somebody give me a hint? I also found huffyuv or lagarith which is provided as a directshow codec for Windows. Can somebody give me a hint how to use a directshow codec with python? I am a novice with video encoding. I found that few codecs support gray scale images. Any hints to take advantage of the fact that I only have gray scale images? Thanks for any help Gregor From __peter__ at web.de Wed Jul 29 04:21:05 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jul 2009 10:21:05 +0200 Subject: instead of depending on data = array('h') .. write samples 1 by 1 to w = wave.open("wav.wav", "w") References: Message-ID: '2+ wrote: > it says > Wave_write.writeframes(data) > will that mean > "from array import array" > is a must? > > this does the job: > > import oil > import wave > from array import array > > a = oil.Sa() > > w = wave.open("current.wav", "w") > w.setnchannels(2) > w.setsampwidth(2) > w.setframerate(44100) > > data = array('h') > > for gas in range(44100 * 5): > a.breath() > r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > data.append(r) > data.append(l) > > w.writeframes(data.tostring()) > w.close() > > don't like array becoming so huge so tested this and it was also okay: > > for gas in range(44100 * 5): > a.breath() > data = array('h') > r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > data.append(r) > data.append(l) > w.writeframes(data.tostring()) > > but without array .. it becomes 15secs(3 times longer than was > intended to be) of wav file: > > for gas in range(44100 * 5): > a.breath() > r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > w.writeframes(hex(r)) > w.writeframes(hex(l)) Doesn't it sound strange, too? You are writing bogus bytes to the file. Compare: >>> import array >>> array.array("h", [42]).tostring() '*\x00' >>> hex(42) '0x2a' Not only do they differ in length, the contents are different, too. If you're unfamiliar with string escape codes, here's a way to see the bytes: >>> map(ord, array.array("h", [42]).tostring()) [42, 0] >>> map(ord, hex(42)) [48, 120, 50, 97] > should i just be happy with depennding on using array? > or is there a solution to make the last one work properly? There is a way to make the last one work: use struct.pack("h", r) instead of hex(r). I'm of course assuming that the first version does give you the desired result. You should not continue before you have verified that. I still recommend array for performance reasons. If memory usage is an issue you can adopt a hybrid approach: # untested from itertools import islice from array import array def gen_data(): for gas in xrange(44100 * 5): # range --> xrange a.breath() r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) yield r yield l data = gen_data() N = 2**20 while True: chunk = array('h') chunk.extend(islice(data, N)) if not chunk: break w.writeframes(chunk.tostring()) This will limit the array size to 4N bytes. Peter From michael at stroeder.com Wed Jul 29 04:45:05 2009 From: michael at stroeder.com (=?ISO-8859-1?Q?Michael_Str=F6der?=) Date: Wed, 29 Jul 2009 10:45:05 +0200 Subject: 64-bit issues with dictionaries in Python 2.6 Message-ID: HI! Are there any known issues with dictionaries in Python 2.6 (not 2.6.2) when running on a 64-bit platform? A friend of mine experiences various strange problems with my web2ldap running with Python 2.6 shipped with openSUSE 11.1 x64. For me it seems some dictionary-based internal registries of web2ldap are not working. Yes, the next test would be to compile Python 2.6.2 from source... Any hint is appreciated. Ciao, Michael. From user at example.net Wed Jul 29 05:02:25 2009 From: user at example.net (superpollo) Date: Wed, 29 Jul 2009 11:02:25 +0200 Subject: idiom for list looping Message-ID: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> hi clp. i want to get from here: nomi = ["one", "two", "three"] to here: 0 - one 1 - two 2 - three i found two ways: first way: for i in range(len(nomi)): print i, "-", nomi[i] or second way: for (i, e) in enumerate(nomi): print i, "-", e which one is "better"? is there a more pythonic way to do it? bye ps: max2 at 192.168.1.102:~/test$ python -V Python 2.3.4 max2 at 192.168.1.102:~/test$ uname -a Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux max2 at 192.168.1.102:~/test$ From d at vidr.cc Wed Jul 29 05:31:47 2009 From: d at vidr.cc (David Roberts) Date: Wed, 29 Jul 2009 19:31:47 +1000 Subject: idiom for list looping In-Reply-To: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> Message-ID: To the best of my knowledge the second way is more pythonic - the first is a little too reminiscent of C. A couple of notes: - you don't need the parentheses around "i, e" - if you were going to use the first way it's better to use xrange instead of range for iteration -- David Roberts http://da.vidr.cc/ On Wed, Jul 29, 2009 at 19:02, superpollo wrote: > hi clp. > > i want to get from here: > > nomi = ["one", "two", "three"] > > to here: > > 0 - one > 1 - two > 2 - three > > i found two ways: > > first way: > > for i in range(len(nomi)): > ? ?print i, "-", nomi[i] > > or second way: > > for (i, e) in enumerate(nomi): > ? ?print i, "-", e > > which one is "better"? is there a more pythonic way to do it? > > bye > > ps: > > max2 at 192.168.1.102:~/test$ python -V > Python 2.3.4 > max2 at 192.168.1.102:~/test$ uname -a > Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux > max2 at 192.168.1.102:~/test$ > -- > http://mail.python.org/mailman/listinfo/python-list > From python at mrabarnett.plus.com Wed Jul 29 05:38:29 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 10:38:29 +0100 Subject: idiom for list looping In-Reply-To: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> Message-ID: <4A701895.5000003@mrabarnett.plus.com> superpollo wrote: > hi clp. > > i want to get from here: > > nomi = ["one", "two", "three"] > > to here: > > 0 - one > 1 - two > 2 - three > > i found two ways: > > first way: > > for i in range(len(nomi)): > print i, "-", nomi[i] > > or second way: > > for (i, e) in enumerate(nomi): > print i, "-", e > > which one is "better"? is there a more pythonic way to do it? > 'enumerate' is the Pythonic solution. From gnewsg at gmail.com Wed Jul 29 06:25:19 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Wed, 29 Jul 2009 03:25:19 -0700 (PDT) Subject: "Deprecated sets module" with Python 2.6 References: <4A6F5F59.4080507@it.uu.se> Message-ID: What about this? import warnings warnings.simplefilter('ignore', DeprecationWarning) import the_module_causing_the_warning warnings.resetwarnings() --- Giampaolo http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil From contact at xavierho.com Wed Jul 29 06:37:44 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 20:37:44 +1000 Subject: idiom for list looping In-Reply-To: <4A701895.5000003@mrabarnett.plus.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> Message-ID: <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> > > superpollo wrote: > >> >> for (i, e) in enumerate(nomi): >> print i, "-", e >> >> Just to be random: print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) This has one advantage: only print once. So it's slightly faster if you have a list of a large amount. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Wed Jul 29 06:52:08 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 11:52:08 +0100 Subject: idiom for list looping In-Reply-To: <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> Message-ID: <4A7029D8.3010700@mrabarnett.plus.com> Xavier Ho wrote: > superpollo wrote: > > > for (i, e) in enumerate(nomi): > print i, "-", e > > Just to be random: > > print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) > > This has one advantage: only print once. So it's slightly faster if you > have a list of a large amount. > Slightly shorter: print '\n'.join("%s - %s" % p for p in enumerate(nomi)) :-) From contact at xavierho.com Wed Jul 29 07:00:37 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 21:00:37 +1000 Subject: idiom for list looping In-Reply-To: <4A7029D8.3010700@mrabarnett.plus.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4A7029D8.3010700@mrabarnett.plus.com> Message-ID: <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> On Wed, Jul 29, 2009 at 8:52 PM, MRAB wrote: > Slightly shorter: > > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > > :-) > That's cool. Does that make the "list" a tuple? (not that it matters, I'm just curious!) I just tested for a list of 1000 elements (number in letters from 1 to 1000 code I wrote for doing project euler problem 17), and the join method is 0.1 seconds faster than the for loop! Woo!? ;p -------------- next part -------------- An HTML attachment was scrubbed... URL: From user at example.net Wed Jul 29 07:02:14 2009 From: user at example.net (superpollo) Date: Wed, 29 Jul 2009 13:02:14 +0200 Subject: idiom for list looping In-Reply-To: References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> Message-ID: <4a702c37$0$34617$4fafbaef@reader4.news.tin.it> MRAB wrote: > Xavier Ho wrote: > >> superpollo wrote: >> >> >> for (i, e) in enumerate(nomi): >> print i, "-", e >> >> Just to be random: >> >> print '\n'.join(["%s - %s" % (i, e) for i, e in enumerate(nomi)]) >> >> This has one advantage: only print once. So it's slightly faster if >> you have a list of a large amount. >> > Slightly shorter: > > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > > :-) >>> print '\n'.join("%s - %s" % p for p in enumerate(nomi)) File "", line 1 print '\n'.join("%s - %s" % p for p in enumerate(nomi)) ^ SyntaxError: invalid syntax >>> print '\n'.join(["%s - %s" % p for p in enumerate(nomi)]) 0 - one 1 - two 2 - three >>> help() Welcome to Python 2.3! This is the online help utility. ... >>> ;-) bye From contact at xavierho.com Wed Jul 29 07:16:41 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 21:16:41 +1000 Subject: idiom for list looping In-Reply-To: <2d56febf0907290416x75d2edb3i88ad452ba18894f1@mail.gmail.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4a702c37$0$34617$4fafbaef@reader4.news.tin.it> <2d56febf0907290416x75d2edb3i88ad452ba18894f1@mail.gmail.com> Message-ID: <2d56febf0907290416x389a1b72w64f583d1d229bbf9@mail.gmail.com> Ack, sent to the wrong email again. On Wed, Jul 29, 2009 at 9:02 PM, superpollo wrote: > > >>> print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > File "", line 1 > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > ^ > SyntaxError: invalid syntax lol, I knew I shouldn't have trusted untested code! > >>> print '\n'.join(["%s - %s" % p for p in enumerate(nomi)]) > 0 - one > 1 - two > 2 - three > Yup.. but using a variable for the tuple itself was a good move though =P. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Wed Jul 29 07:17:14 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 12:17:14 +0100 Subject: idiom for list looping In-Reply-To: <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4A7029D8.3010700@mrabarnett.plus.com> <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> Message-ID: <4A702FBA.8060303@mrabarnett.plus.com> Xavier Ho wrote: > On Wed, Jul 29, 2009 at 8:52 PM, MRAB > wrote: > > Slightly shorter: > > print '\n'.join("%s - %s" % p for p in enumerate(nomi)) > > :-) > > That's cool. Does that make the "list" a tuple? (not that it matters, > I'm just curious!) > I've just replaced the list comprehension with a generator expression. From contact at xavierho.com Wed Jul 29 07:20:50 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 21:20:50 +1000 Subject: idiom for list looping In-Reply-To: <4A702FBA.8060303@mrabarnett.plus.com> References: <4a701022$0$34623$4fafbaef@reader4.news.tin.it> <4A701895.5000003@mrabarnett.plus.com> <2d56febf0907290337j688607bfhb0f171f5a982abfd@mail.gmail.com> <4A7029D8.3010700@mrabarnett.plus.com> <2d56febf0907290400s5f31b8bcs549df9cdaa72bc28@mail.gmail.com> <4A702FBA.8060303@mrabarnett.plus.com> Message-ID: <2d56febf0907290420m7543ee8bs3c6ad9914fb51d39@mail.gmail.com> On Wed, Jul 29, 2009 at 9:17 PM, MRAB wrote: > I've just replaced the list comprehension with a generator expression. > > Oh, and that isn't in Python 2.3.... I see. Generators are slightly newer, eh. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From nick at craig-wood.com Wed Jul 29 07:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 29 Jul 2009 06:29:56 -0500 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: John D Giotta wrote: > I'm looking to run a process with a limit of 3 instances, but each > execution is over a crontab interval. I've been investigating the > threading module and using daemons to limit active thread objects, but > I'm not very successful at grasping the documentation. > > Is it possible to do what I'm trying to do and if so anyone know of a > useful example to get started? If you want a simple, cross platform way of doing it, then bind each process to a different local tcp port. Make a list of 3 ports, and try binding to each port in turn. If you can't find a port to bind to then there are already 3 instances running. Something like this import socket PORTS = range(10000,10003) lock_sock = None def lock_process(_locks = []): for port in PORTS: sock = socket.socket() try: sock.bind(("localhost", port)) except socket.error, e: sock = None else: _locks.append(sock) break else: raise Exception("Too many instances of me running") for i in range(5): print "Trying",i+1 lock_process() Which prints Trying 1 Trying 2 Trying 3 Trying 4 Traceback (most recent call last): File "", line 20, in File "", line 16, in lock_process Exception: Too many instances of me running You could do the same thing with lock files also very easily... -- Nick Craig-Wood -- http://www.craig-wood.com/nick From spookyk at gmail.com Wed Jul 29 07:35:42 2009 From: spookyk at gmail.com (Rincewind) Date: Wed, 29 Jul 2009 04:35:42 -0700 (PDT) Subject: QFileDialog setFileMode blues Message-ID: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> Heya, I am fairly new to Python and even newer to Qt. The problem is opening a Qt file dialog to select folders only. QFileDialog has a nice and dandy setFileMode() function just for that. The only trouble is that I cannot make it work. Last thing I've tried was something like this: self.fd = QtGui.QFileDialog() self.fd.setFileMode(self.fd.FileMode(4)) filename = self.fd.getOpenFileName() ..but it was fruitless. Any help? Tons of internets in return! From Ron.Barak at lsi.com Wed Jul 29 07:36:05 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Wed, 29 Jul 2009 12:36:05 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: References: Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> Hi, I wanted to make a python byte-code file executable, expecting to be able to run it without specifying "python" on the (Linux bash) command line. So, I wrote the following: [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python print "hello" [root at VMLinux1 python]# and made its pyc file executable: [root at VMLinux1 python]# ls -ls test_pyc.pyc 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc [root at VMLinux1 python]# So, I see: [root at VMLinux1 python]# file test_pyc.py* test_pyc.py: a python script text executable test_pyc.pyc: python 2.3 byte-compiled [root at VMLinux1 python]# If I try to do the following, no problem: [root at VMLinux1 python]# python test_pyc.pyc hello [root at VMLinux1 python]# However, the following fails: [root at VMLinux1 python]# ./test_pyc.pyc -bash: ./test_pyc.pyc: cannot execute binary file [root at VMLinux1 python]# Is there a way to run a pyc file without specifying the python path ? Bye, Ron. -------------- next part -------------- An HTML attachment was scrubbed... URL: From phil at riverbankcomputing.com Wed Jul 29 07:45:03 2009 From: phil at riverbankcomputing.com (Phil Thompson) Date: Wed, 29 Jul 2009 12:45:03 +0100 Subject: QFileDialog setFileMode blues In-Reply-To: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> References: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> Message-ID: <8477fc1d11b68f7e3e11237d957890b1@localhost> On Wed, 29 Jul 2009 04:35:42 -0700 (PDT), Rincewind wrote: > Heya, > > I am fairly new to Python and even newer to Qt. > The problem is opening a Qt file dialog to select folders only. > QFileDialog has a nice and dandy setFileMode() function just for that. > The only trouble is that I cannot make it work. > Last thing I've tried was something like this: > > self.fd = QtGui.QFileDialog() > self.fd.setFileMode(self.fd.FileMode(4)) > filename = self.fd.getOpenFileName() > > ..but it was fruitless. > > Any help? Tons of internets in return! QFileDialog.getOpenFileName() is a static method that creates its own QFileDialog internally. It's not using the one you have created and configured. Phil From marcusw at cox.net Wed Jul 29 08:23:14 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 08:23:14 -0400 Subject: fast video encoding In-Reply-To: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> References: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Message-ID: On 7/29/2009 4:14 AM, gregorth wrote: > Hi all, > > for a scientific application I need to save a video stream to disc for > further post processing. My cam can deliver 8bit grayscale images with > resolution 640x480 with a framerate up to 100Hz, this is a data rate > of 30MB/s. Writing the data uncompressed to disc hits the data > transfer limits of my current system and creates huge files. Therefore > I would like to use video compression, preferably fast and high > quality to lossless encoding. Final file size is not that important. Try googling realtime greyscale video codec... > Because of the hardware I am bound to WinXP. There's always a way to run linux :p > > I already tried pymedia for encoding to mpeg2, however I only managed > to get a framerate of about 30-40fps (on a 1.8GHz dual core). There is > still room for improvements in my code, but before trying hard I want > to ask for advices or other possibilities. I also found gstreamer with > pygst python bindings, which seems to be more modern (and performant?) > package than pymedia. I did not yet try it, especially since I didn't > find a simple code example of how to use it for my use case. Can > somebody give me a hint? Video encoding is not my specialty, but my recommendation here is to drop python because of its slow speed and work in c as much as possible. > > I also found huffyuv or lagarith which is provided as a directshow > codec for Windows. Can somebody give me a hint how to use a directshow > codec with python? Not me, sorry :( Never worked directly with directshow (no pun intended). > > I am a novice with video encoding. I found that few codecs support > gray scale images. Any hints to take advantage of the fact that I only > have gray scale images? Greyscale PNG or BMP compression. > > Thanks for any help Don't know if this counts as help, but you're welcome! > > Gregor > Marcus From marcusw at cox.net Wed Jul 29 08:24:33 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 08:24:33 -0400 Subject: simple splash screen? In-Reply-To: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: On 7/28/2009 11:58 PM, NighterNet wrote: > I am trying to make a simple splash screen from python 3.1.Not sure > where to start looking for it. Can any one help? Trying to make a splash screen for python? Or trying to do image processing in python? Marcus From sibtey.mehndi at genpact.com Wed Jul 29 08:25:22 2009 From: sibtey.mehndi at genpact.com (Mehndi, Sibtey) Date: Wed, 29 Jul 2009 05:25:22 -0700 Subject: how to embed the python interpreter into web App Message-ID: <1A343A98C8DAE44A8D9D1BF310F90D5E023D54C3@GCPWINGGN2EVS11.IND.CORP.AD> Hi All I am trying to embed the python interpreter in to a web app but could not get the way, any one can suggest me how to do this. Thanks, Sibtey Mehdi This e-mail (and any attachments), is confidential and may be privileged. It may be read, copied and used only by intended recipients. Unauthorized access to this e-mail (or attachments) and disclosure or copying of its contents or any action taken in reliance on it is unlawful. Unintended recipients must notify the sender immediately by e-mail/phone & delete it from their system without making any copies or disclosing it to a third person. -------------- next part -------------- An HTML attachment was scrubbed... URL: From duncan.booth at invalid.invalid Wed Jul 29 08:26:11 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 29 Jul 2009 12:26:11 GMT Subject: Need help in passing a "-c command" argument to the interactive shell. References: <230bcf11-2227-4d7f-9048-862c1bdb1912@13g2000prl.googlegroups.com> Message-ID: Bill wrote: > On my windows box I type => c:\x>python -c "import re" > > The result is => c:\x> > > In other words, the Python interactive shell doesn't even open. What > am I doing wrong? > > I did RTFM at http://www.python.org/doc/1.5.2p2/tut/node4.html on > argument passing, but to no avail. Why are you reading documentation for Python 1.5? There have been just a few releases since then. Try reading http://docs.python.org/using/cmdline.html instead, specifically the part about the -i command line option. Or you could just try "python -?" at a command prompt. -- Duncan Booth http://kupuguy.blogspot.com From marcusw at cox.net Wed Jul 29 08:34:09 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 08:34:09 -0400 Subject: about analyze object's stack usage In-Reply-To: References: Message-ID: On 7/29/2009 3:51 AM, hch wrote: > Is there a python script can get a list of how much stack space each > function in your program uses? I don't think so. You could try raw reading of the memory from another thread using ctypes and pointers, but that would be madness. > ( the program is compiled by gcc) If you're talking about a c program, almost certainly not. What you should do is just use gdb and disas each function and look at what it subtracts off of %esp at the third instruction in the function. I can explain it to you if you are not very experienced in gdb and assembly... Marcus From rashidsdpk at gmail.com Wed Jul 29 08:54:24 2009 From: rashidsdpk at gmail.com (Rashid Ali Soomro) Date: Wed, 29 Jul 2009 05:54:24 -0700 (PDT) Subject: Particle research opens door for new technology Message-ID: Big uses for small particles will be explored at the annual Particle Technology Research Centre Conference at The University of Western Ontario July 9 and 10.more http://0nanotechnology0.blogspot.com/ From catalinfest at gmail.com Wed Jul 29 09:03:06 2009 From: catalinfest at gmail.com (catalinfest at gmail.com) Date: Wed, 29 Jul 2009 06:03:06 -0700 (PDT) Subject: Help with sql and python Message-ID: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> Hello ! I have accont on myphpadmin on my server. I want to create one script in python. This script manage sql task from my sql server . How i make this in a simple way ? Thank you ! From contact at xavierho.com Wed Jul 29 09:08:12 2009 From: contact at xavierho.com (Xavier Ho) Date: Wed, 29 Jul 2009 23:08:12 +1000 Subject: Particle research opens door for new technology In-Reply-To: References: Message-ID: <2d56febf0907290608v4fe62d32n804e43008713572b@mail.gmail.com> Is this a spam? Why did you have to send it 4 times, and it's already in the past (July 9 and 10) ? Ching-Yun "Xavier" Ho, Technical Artist Contact Information Mobile: (+61) 04 3335 4748 Skype ID: SpaXe85 Email: contact at xavierho.com Website: http://xavierho.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From v1d4l0k4 at gmail.com Wed Jul 29 09:10:47 2009 From: v1d4l0k4 at gmail.com (v1d4l0k4) Date: Wed, 29 Jul 2009 13:10:47 +0000 Subject: Working with platform.system() Message-ID: <9a5b5be90907290610t1868ef07p20a5c2bdc3404ab8@mail.gmail.com> Hi! I want to know how I can get different expected return values by platform.system() method. I want to sniff some systems (Linux, Windows, Mac, BSD, Solaris) and I don't know how I can check them correctly. Could you give me some expected return values that you know? Thanks in advance, Paulo Ricardo -------------- next part -------------- An HTML attachment was scrubbed... URL: From james at agentultra.com Wed Jul 29 09:29:55 2009 From: james at agentultra.com (J Kenneth King) Date: Wed, 29 Jul 2009 09:29:55 -0400 Subject: escaping characters in filenames Message-ID: <854osv7h2k.fsf@agentultra.com> I wrote a script to process some files using another program. One thing I noticed was that both os.listdir() and os.path.walk() will return unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a module or recipe that escapes file names and was wondering if anyone could point me in the right direction. As an aside, the script is using subprocess.call() with the "shell=True" parameter. There isn't really a reason for doing it this way (was just the fastest way to write it and get a prototype working). I was wondering if Popen objects were sensitive to unescaped names like the shell. I intend to refactor the function to use Popen objects at some point and thought perhaps escaping file names may not be entirely necessary. Cheers From nospam at nospam.com Wed Jul 29 09:55:32 2009 From: nospam at nospam.com (Gilles Ganault) Date: Wed, 29 Jul 2009 15:55:32 +0200 Subject: Compiling Python for uCLinux appliance? Message-ID: <7sk075l5p7eq8mek9ooqj5kr0ds10hrvfq@4ax.com> Hello I just got a small appliance based on a Blackfin CPU with 64MB RAM and 258MB NAND flash. Using the stock software, there's about 30MB of RAM left. Besides C/C++ and shel scripts, I was wondering if it were realistic to upload a few Python scripts in such a small appliance? Thank you. From piet at cs.uu.nl Wed Jul 29 10:14:45 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 29 Jul 2009 16:14:45 +0200 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: >>>>> Carl Banks (CB) wrote: >CB> On Jul 28, 3:15?pm, John D Giotta wrote: >>> I'm looking to run a process with a limit of 3 instances, but each >>> execution is over a crontab interval. I've been investigating the >>> threading module and using daemons to limit active thread objects, but >>> I'm not very successful at grasping the documentation. >>> >>> Is it possible to do what I'm trying to do and if so anyone know of a >>> useful example to get started? >CB> It seems like you want to limit the number of processes to three; the >CB> threading module won't help you there because it deals with threads >CB> within a single process. >CB> What I'd do is to simply run the system ps to see how many processes >CB> are running (ps is pretty versatile on most systems and can find >CB> specifically targeted processes like you program), and exit if there >CB> are already three. That will surely run into some race conditions. If the limit of 3 processes is soft then that wouldn't be a big deal, however. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From spookyk at gmail.com Wed Jul 29 10:15:53 2009 From: spookyk at gmail.com (Rincewind) Date: Wed, 29 Jul 2009 07:15:53 -0700 (PDT) Subject: QFileDialog setFileMode blues References: <13e9a34a-d92a-49f0-96da-3abc17b0f280@c14g2000yqm.googlegroups.com> Message-ID: On Jul 29, 12:45 pm, Phil Thompson wrote: > On Wed, 29 Jul 2009 04:35:42 -0700 (PDT), Rincewind > wrote: > > > Heya, > > > I am fairly new to Python and even newer to Qt. > > The problem is opening a Qt file dialog to select folders only. > > QFileDialog has a nice and dandy setFileMode() function just for that. > > The only trouble is that I cannot make it work. > > Last thing I've tried was something like this: > > > self.fd = QtGui.QFileDialog() > > self.fd.setFileMode(self.fd.FileMode(4)) > > filename = self.fd.getOpenFileName() > > > ..but it was fruitless. > > > Any help? Tons of internets in return! > > QFileDialog.getOpenFileName() is a static method that creates its own > QFileDialog internally. It's not using the one you have created and > configured. > > Phil You are awesome, thanks! From python at mrabarnett.plus.com Wed Jul 29 10:22:34 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 15:22:34 +0100 Subject: escaping characters in filenames In-Reply-To: <854osv7h2k.fsf@agentultra.com> References: <854osv7h2k.fsf@agentultra.com> Message-ID: <4A705B2A.2050807@mrabarnett.plus.com> J Kenneth King wrote: > I wrote a script to process some files using another program. One thing > I noticed was that both os.listdir() and os.path.walk() will return > unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ > File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a > module or recipe that escapes file names and was wondering if anyone > could point me in the right direction. > That's only necessary if you're building a command line and passing it as a string. > As an aside, the script is using subprocess.call() with the "shell=True" > parameter. There isn't really a reason for doing it this way (was just > the fastest way to write it and get a prototype working). I was > wondering if Popen objects were sensitive to unescaped names like the > shell. I intend to refactor the function to use Popen objects at some > point and thought perhaps escaping file names may not be entirely > necessary. > Pass the command line to Popen as a list of strings. From deets at nospam.web.de Wed Jul 29 10:46:47 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 29 Jul 2009 16:46:47 +0200 Subject: Compiling Python for uCLinux appliance? References: <7sk075l5p7eq8mek9ooqj5kr0ds10hrvfq@4ax.com> Message-ID: <7db5mnF2bf9i2U1@mid.uni-berlin.de> Gilles Ganault wrote: > Hello > > I just got a small appliance based on a Blackfin CPU with 64MB RAM and > 258MB NAND flash. Using the stock software, there's about 30MB of RAM > left. > > Besides C/C++ and shel scripts, I was wondering if it were realistic > to upload a few Python scripts in such a small appliance? Try & check out the gumstix software repo. It comes with python, and thus some crosscompiling/build-instructions should be in there. Diez From jakecjacobson at gmail.com Wed Jul 29 11:07:34 2009 From: jakecjacobson at gmail.com (jakecjacobson) Date: Wed, 29 Jul 2009 08:07:34 -0700 (PDT) Subject: bad certificate error References: <027e226c$0$5185$c3e8da3@news.astraweb.com> <027ed9be$0$5185$c3e8da3@news.astraweb.com> Message-ID: On Jul 29, 2:08?am, "Gabriel Genellina" wrote: > En Tue, 28 Jul 2009 09:02:40 -0300, Steven D'Aprano ? > escribi?: > > > > > On Mon, 27 Jul 2009 23:16:39 -0300, Gabriel Genellina wrote: > > >> I don't see the point on "fixing" either the Python script or httplib to > >> accomodate for an invalid server certificate... If it's just for > >> internal testing, I'd use HTTP instead (at least until the certificate > >> is fixed). > > > In real life, sometimes you need to drive with bad brakes on your car, > > walk down dark alleys in the bad part of town, climb a tree without a > > safety line, and use a hammer without wearing goggles. We can do all > > these things. > > > The OP has said that, for whatever reason, he needs to ignore a bad > > server certificate when connecting to HTTPS. Python is a language where > > developers are allowed to shoot themselves in the foot, so long as they > > do so in full knowledge of what they're doing. > > > So, putting aside all the millions of reasons why the OP shouldn't accept > > an invalid certificate, how can he accept an invalid certificate? > > Yes, I understand the situation, but I'm afraid there is no way (that I ? > know of). At least not without patching _ssl.c; all the SSL negotiation is ? > handled by the OpenSSL library itself. > > I vaguely remember a pure Python SSL implementation somewhere that perhaps ? > could be hacked to bypass all controls. But making it work properly will ? > probably require a lot more effort than installing a self signed ? > certificate in the server... > > -- > Gabriel Genellina I have it working and I want to thank everyone for their efforts and very helpful hints. The error was with me and not understanding the documentation about the cert_file & key_file. After using openssl to divide up my p12 file into a cert file and a key file using the instructions http://security.ncsa.uiuc.edu/research/grid-howtos/usefulopenssl.php. I got everything working. Again, much thanks. Jake From darkneter at gmail.com Wed Jul 29 11:20:45 2009 From: darkneter at gmail.com (NighterNet) Date: Wed, 29 Jul 2009 08:20:45 -0700 (PDT) Subject: simple splash screen? References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: <68539c2a-e2f6-4956-b779-17d21ca422b1@2g2000prl.googlegroups.com> On Jul 29, 5:24?am, Marcus Wanner wrote: > On 7/28/2009 11:58 PM, NighterNet wrote:> I am trying to make a simple splash screen from python 3.1.Not sure > > where to start looking for it. Can any one help? > > Trying to make a splash screen for python? > Or trying to do image processing in python? > > Marcus trying to use jpg,gif,png for the splash screen From wwn1 at sfu.ca Wed Jul 29 11:24:25 2009 From: wwn1 at sfu.ca (WilsonOfCanada) Date: Wed, 29 Jul 2009 08:24:25 -0700 (PDT) Subject: Clearing an array Message-ID: Hellos, I was wondering if there is any built-in function that clears the array. I was also wondering if this works: arrMoo = ['33', '342', .... '342'] arrMoo = [] From tutufan at gmail.com Wed Jul 29 11:24:52 2009 From: tutufan at gmail.com (Mike) Date: Wed, 29 Jul 2009 08:24:52 -0700 (PDT) Subject: New implementation of re module References: Message-ID: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> On Jul 27, 11:34?am, MRAB wrote: > I've been working on a new implementation of the re module. Fabulous! If you're extending/changing the interface, there are a couple of sore points in the current implementation I'd love to see addressed: - findall/finditer doesn't find overlapping matches. Sometimes you really *do* want to know all possible matches, even if they overlap. This comes up in bioinformatics, for example. - split won't split on empty patterns, e.g. empty lookahead patterns. This means that it can't be used for a whole class of interesting cases. This has been discussed previously: http://bugs.python.org/issue3262 http://bugs.python.org/issue852532 http://bugs.python.org/issue988761 - It'd be nice to have a version of split that generates the parts (one by one) rather than returning the whole list. - Repeated subgroup match information is not available. That is, for a match like this re.match('(.){3}', 'xyz') there's no way to discover that the subgroup first matched 'x', then matched 'y', and finally matched 'z'. Here is one past proposal (mine), perhaps over-complex, to address this problem: http://mail.python.org/pipermail/python-dev/2004-August/047238.html Mike From Omer.Khalid at cern.ch Wed Jul 29 11:26:55 2009 From: Omer.Khalid at cern.ch (Omer Khalid) Date: Wed, 29 Jul 2009 17:26:55 +0200 Subject: Very Strange Problem Message-ID: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Hi, I am having a very strange problem with modifying a variable in a list in my program. Here is the code: # a list that contains dictionary objects jobs = [] index=5 for each in range(index): jobs.append({'v':0}) some_function(index): if jobs[index]['v'] == 0: # set it to 1 jobs[index]['v'] = 1 print "Set to 1" else: print "Already set to 1" loop(): index=0 for each in range(len(jobs)): some_function(index) index +=1 Apparently, the jobs[index]['v'] never get updated in the some_function but the print statement afterwards get printed... What's really surprising is that there are no errors or exceptions and my my program runs in a single thread...so i have been unable to explain this behavior. Any insight would be much appreciated! Cheers Omer -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Wed Jul 29 11:32:36 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 29 Jul 2009 17:32:36 +0200 Subject: Clearing an array References: Message-ID: <7db8ckF28m03sU1@mid.uni-berlin.de> WilsonOfCanada wrote: > Hellos, > > I was wondering if there is any built-in function that clears the > array. I was also wondering if this works: > > arrMoo = ['33', '342', .... '342'] > arrMoo = [] Most of the time, yes. Unless you have something like this, then it won't work: foo = [10] bar = foo # an alias foo = [] assert foo == bar Then what you need to to is this: foo[:] = [] Diez From tack at urandom.ca Wed Jul 29 11:39:00 2009 From: tack at urandom.ca (Jason Tackaberry) Date: Wed, 29 Jul 2009 11:39:00 -0400 Subject: Clearing an array In-Reply-To: References: Message-ID: <1248881940.7696.28.camel@willow> On Wed, 2009-07-29 at 08:24 -0700, WilsonOfCanada wrote: > I was wondering if there is any built-in function that clears the > array. The proper python term would be "list." You can remove all elements of a list 'l' like so: del l[:] > I was also wondering if this works: > > arrMoo = ['33', '342', .... '342'] > arrMoo = [] That doesn't clear the list as such, but rather creates a new list, and reassigns the new list to the 'arrMoo' name in the local scope. Consider: >>> l1 = [1,2,3] >>> l2 = l1 >>> l1 = [] >>> print l2 [1, 2, 3] So the original list 'l1' lives on. However: >>> l1 = [1,2,3] >>> l2 = l1 >>> del l1[:] >>> print l1, l2 [] [] Cheers, Jason. From tjreedy at udel.edu Wed Jul 29 11:43:12 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 29 Jul 2009 11:43:12 -0400 Subject: python 3 and stringio.seek In-Reply-To: References: Message-ID: Miles Kaufmann wrote: > > On Jul 28, 2009, at 6:30 AM, Michele Petrazzo wrote: > >> Hi list, >> I'm trying to port a my library to python 3, but I have a problem with a >> the stringio.seek: >> the method not accept anymore a value like pos=-6 mode=1, but the "old" >> (2.X) version yes... >> >> The error: >> File "/home/devel/Py3/lib/python3.0/io.py", line 2031, in seek >> return self._seek(pos, whence) >> IOError: Can't do nonzero cur-relative seeks >> >> >> How solve this? > > In Python 2, StringIO is a stream of bytes (non-Unicode characters). In > Python 3, StringIO is a stream of text (Unicode characters). In the > early development of Python 3 (and 3.1's _pyio), it was implemented as a > TextIOWrapper over a BytesIO buffer. TextIOWrapper does not support > relative seeks because it is difficult to map the concept of a "current > position" between bytes and the text that it encodes, especially with > variable-width encodings and other considerations. Furthermore, the > value returned from TextIOWrapper.tell isn't just a file position but a > "cookie" that contains other data necessary to restore the decoding > mechanism to the same state. However, for the default encoding (utf-8), > the current position is equivalent to that of the underlying bytes buffer. > > In Python 3, StringIO is implemented using an internal buffer of Unicode > characters. There is no technical reason why it can't support relative > seeks; I assume it does not for compatibility with the original Python > TextIOWrapper implementation (which is present in 3.1's _pyio, but not > in 3.0). > > Note that because of the different implementations, StringIO.tell() (and > seek) behaves differently for the C and Python implementations: > > $ python3.1 > >>> import io, _pyio > >>> s = io.StringIO('\u263A'); s.read(1), s.tell() > ('?', 1) > >>> s = _pyio.StringIO('\u263A'); s.read(1), s.tell() > ('?', 3) It seems to me that this discrepancy might be worth a tracker item. I wonder why the second implementation is even there if not used. Two different commiters? > The end result seems to be that, for text streams (including StreamIO), > you *should* treat the value returned by tell() as an opaque magic > cookie, and *only* pass values to seek() that you have obtained from a > previous tell() call. However, in practice, it appears that you *may* > seek StringIO objects relatively by characters using s.seek(s.tell() + > n), so long as you do not use the _pyio.StringIO implementation. A tracker item could include a request that relative seek be restored if possible. tjr From python at mrabarnett.plus.com Wed Jul 29 11:45:55 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 16:45:55 +0100 Subject: New implementation of re module In-Reply-To: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> References: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> Message-ID: <4A706EB3.1040501@mrabarnett.plus.com> Mike wrote: > On Jul 27, 11:34 am, MRAB wrote: >> I've been working on a new implementation of the re module. > > Fabulous! > > If you're extending/changing the interface, there are a couple of sore > points in the current implementation I'd love to see addressed: > > - findall/finditer doesn't find overlapping matches. Sometimes you > really *do* want to know all possible matches, even if they overlap. > This comes up in bioinformatics, for example. > Perhaps by adding "overlapped=True"? > - split won't split on empty patterns, e.g. empty lookahead patterns. > This means that it can't be used for a whole class of interesting > cases. This has been discussed previously: > > http://bugs.python.org/issue3262 > http://bugs.python.org/issue852532 > http://bugs.python.org/issue988761 > Already addressed (see issue2636 for the full details). > - It'd be nice to have a version of split that generates the parts > (one by one) rather than returning the whole list. > Hmm, re.splititer() perhaps. > - Repeated subgroup match information is not available. That is, for > a match like this > > re.match('(.){3}', 'xyz') > > there's no way to discover that the subgroup first matched 'x', then > matched 'y', and finally matched 'z'. Here is one past proposal > (mine), perhaps over-complex, to address this problem: > > http://mail.python.org/pipermail/python-dev/2004-August/047238.html > Yikes! I think I'll let you code that... :-) From mdboldin at gmail.com Wed Jul 29 11:49:49 2009 From: mdboldin at gmail.com (mpython) Date: Wed, 29 Jul 2009 08:49:49 -0700 (PDT) Subject: SEC forms parsing Message-ID: I am looking for any python examples that have written to parse SEC filings (Security Exchange Commission's EDGAR system),or just a pointer to best modules to use. From python at mrabarnett.plus.com Wed Jul 29 11:53:47 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 16:53:47 +0100 Subject: Very Strange Problem In-Reply-To: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Message-ID: <4A70708B.4030402@mrabarnett.plus.com> Omer Khalid wrote: > Hi, > > I am having a very strange problem with modifying a variable in a list > in my program. Here is the code: > > # a list that contains dictionary objects > jobs = [] > > index=5 > for each in range(index): > jobs.append({'v':0}) > > some_function(index): > if jobs[index]['v'] == 0: > # set it to 1 > jobs[index]['v'] = 1 > print "Set to 1" > else: > print "Already set to 1" > > loop(): > index=0 > for each in range(len(jobs)): > some_function(index) > index +=1 > > > Apparently, the jobs[index]['v'] never get updated in the some_function > but the print statement afterwards get printed... > > What's really surprising is that there are no errors or exceptions and > my my program runs in a single thread...so i have been unable to explain > this behavior. > > Any insight would be much appreciated! > Well, when I insert the missing 'def's in the function definitions, it works for me. From ronn.ross at gmail.com Wed Jul 29 11:57:18 2009 From: ronn.ross at gmail.com (Ronn Ross) Date: Wed, 29 Jul 2009 10:57:18 -0500 Subject: Can you easy_install from your localhost? Message-ID: <9c8c445f0907290857x27930acbvc5067a242e18ed17@mail.gmail.com> I have a package i would like to store locally. If it is stored locally can I do something like ' easy_install http://localhost/package ' ? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid Wed Jul 29 12:02:53 2009 From: invalid at invalid (Grant Edwards) Date: Wed, 29 Jul 2009 11:02:53 -0500 Subject: Compiling Python for uCLinux appliance? References: <7sk075l5p7eq8mek9ooqj5kr0ds10hrvfq@4ax.com> <7db5mnF2bf9i2U1@mid.uni-berlin.de> Message-ID: On 2009-07-29, Diez B. Roggisch wrote: > Gilles Ganault wrote: > >> Hello >> >> I just got a small appliance based on a Blackfin CPU with 64MB RAM and >> 258MB NAND flash. Using the stock software, there's about 30MB of RAM >> left. >> >> Besides C/C++ and shel scripts, I was wondering if it were realistic >> to upload a few Python scripts in such a small appliance? The standard uclinux-dist comes with python, so it's pretty much just a question of memory. Asking on the uclinux mailing list will probably be more useful, but I would guess that 64MB would be plenty -- especially if use JFFS to put your root filesystem in flash instead of in RAM. Of course it depends entirely on what else you want to do with that RAM at the same time... > Try & check out the gumstix software repo. It comes with > python, and thus some crosscompiling/build-instructions should > be in there. IIRC, all you have to do is "make menuconfig", go to the user apps page and check the "Python" box, and then "make". It should "just work". -- Grant Edwards grante Yow! I haven't been married at in over six years, but we visi.com had sexual counseling every day from Oral Roberts!! From ryniek90 at gmail.com Wed Jul 29 12:11:32 2009 From: ryniek90 at gmail.com (Ryniek90) Date: Wed, 29 Jul 2009 18:11:32 +0200 Subject: how to embed the python interpreter into web App (Mehndi, Sibtey) In-Reply-To: References: Message-ID: <4A7074B4.2070204@gmail.com> > > > Hi All > > I am trying to embed the python interpreter in to a web app but could > not get the way, any one can suggest me how to do this. > > > > Thanks, > > Sibtey Mehdi > > > > This e-mail (and any attachments), is confidential and may be privileged. It may be read, copied and used only > by intended recipients. Unauthorized access to this e-mail (or attachments) and disclosure or copying of its > contents or any action taken in reliance on it is unlawful. Unintended recipients must notify the sender immediately > by e-mail/phone & delete it from their system without making any copies or disclosing it to a third person. > > > Here's example: http://try-python.mired.org/ Contact with author of that website/webapp. Good luck. From python at mrabarnett.plus.com Wed Jul 29 12:53:33 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 17:53:33 +0100 Subject: Very Strange Problem In-Reply-To: <4A7074E6.9030100@gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A70708B.4030402@mrabarnett.plus.com> <4A7074E6.9030100@gmail.com> Message-ID: <4A707E8D.4020306@mrabarnett.plus.com> Ricardo Ar?oz wrote: > MRAB wrote: >> Omer Khalid wrote: >>> Hi, >>> >>> I am having a very strange problem with modifying a variable in a >>> list in my program. Here is the code: >>> >>> # a list that contains dictionary objects >>> jobs = [] >>> >>> index=5 >>> for each in range(index): >>> jobs.append({'v':0}) >>> >>> some_function(index): >>> if jobs[index]['v'] == 0: >>> # set it to 1 >>> jobs[index]['v'] = 1 >>> print "Set to 1" >>> else: >>> print "Already set to 1" >>> >>> loop(): >>> index=0 >>> for each in range(len(jobs)): >>> some_function(index) >>> index +=1 >>> >>> >>> Apparently, the jobs[index]['v'] never get updated in the >>> some_function but the print statement afterwards get printed... >>> >>> What's really surprising is that there are no errors or exceptions >>> and my my program runs in a single thread...so i have been unable to >>> explain this behavior. >>> >>> Any insight would be much appreciated! >>> >> Well, when I insert the missing 'def's in the function definitions, it >> works for me. > Hi Omer, what he is trying to convey in his rude manner is that you are > missing "def" in your function definitions. It is probably a beginners > mistake. > > That is : > from "some_function(index): " > to "def some_function(index): " > > from "loop(): " > to "def loop(): " > > I have not tried your code so you should believe him when he states he > has actually run the code. > HTH > Omer says "the print statement afterwards get printed", but the code provided would have raised a SyntaxError, so omitting the 'def's can't be the cause of the actual problem reported. From nagle at animats.com Wed Jul 29 13:10:15 2009 From: nagle at animats.com (John Nagle) Date: Wed, 29 Jul 2009 10:10:15 -0700 Subject: bad certificate error In-Reply-To: References: Message-ID: <4a7081c9$0$1600$742ec2ed@news.sonic.net> jakecjacobson wrote: > Hi, > > I am getting the following error when doing a post to REST API, > > Enter PEM pass phrase: > Traceback (most recent call last): > File "./ices_catalog_feeder.py", line 193, in ? > main(sys.argv[1]) > File "./ices_catalog_feeder.py", line 60, in main > post2Catalog(catalog_host, catalog_port, catalog_path, os.path.join > (input_dir, file), collection_name, key_file, cert_file) > File "./ices_catalog_feeder.py", line 125, in post2Catalog > connection.request('POST', path, parameters, head) > File "/usr/lib/python2.4/httplib.py", line 810, in request > self._send_request(method, url, body, headers) > File "/usr/lib/python2.4/httplib.py", line 833, in _send_request > self.endheaders() > File "/usr/lib/python2.4/httplib.py", line 804, in endheaders > self._send_output() > File "/usr/lib/python2.4/httplib.py", line 685, in _send_output > self.send(msg) > File "/usr/lib/python2.4/httplib.py", line 652, in send > self.connect() > File "/usr/lib/python2.4/httplib.py", line 1079, in connect > ssl = socket.ssl(sock, self.key_file, self.cert_file) > File "/usr/lib/python2.4/socket.py", line 74, in ssl > return _realssl(sock, keyfile, certfile) > socket.sslerror: (1, 'error:14094412:SSL > routines:SSL3_READ_BYTES:sslv3 alert bad certificate') What SSL implementation are you using? The old one from Python 2.4 doesn't even check the certificate chain. M2Crypto does, and the new SSL module does, but in each case you have to provide a root certificate file. (The one from Mozilla is available.) John Nagle From nagle at animats.com Wed Jul 29 13:12:27 2009 From: nagle at animats.com (John Nagle) Date: Wed, 29 Jul 2009 10:12:27 -0700 Subject: SEC forms parsing In-Reply-To: References: Message-ID: <4a70824d$0$1600$742ec2ed@news.sonic.net> mpython wrote: > I am looking for any python examples that have written to parse SEC > filings (Security Exchange Commission's EDGAR system),or just a > pointer to best modules to use. I've been doing that in Perl for years. See "www.downside.com". Actually extracting financial statements is possible, but you run into a patent problem with a Price-Waterhouse patent if you try to do a good job. What are you trying to do? John Nagle From piet at cs.uu.nl Wed Jul 29 13:17:51 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 29 Jul 2009 19:17:51 +0200 Subject: Help with sql and python References: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> Message-ID: >>>>> "catalinfest at gmail.com" (cc) wrote: >cc> Hello ! >cc> I have accont on myphpadmin on my server. >cc> I want to create one script in python. >cc> This script manage sql task from my sql server . >cc> How i make this in a simple way ? See http://mysql-python.sourceforge.net/MySQLdb.html This really has nothing to do with myphpadmin (you probably mean phpMyAdmin but even then it has nothing to do with it except that both acess a MySQL database). And if you give yourself a name than we might have an idea if we are talking to a human being or a robot. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From tutufan at gmail.com Wed Jul 29 13:21:39 2009 From: tutufan at gmail.com (Mike) Date: Wed, 29 Jul 2009 10:21:39 -0700 (PDT) Subject: New implementation of re module References: <215e5098-9181-4fec-b563-f8851d3bb491@v36g2000yqv.googlegroups.com> Message-ID: <19a1cbfa-6594-4956-bc3c-f1fd3108091a@c29g2000yqd.googlegroups.com> On Jul 29, 10:45?am, MRAB wrote: > Mike wrote: > > - findall/finditer doesn't find overlapping matches. ?Sometimes you > > really *do* want to know all possible matches, even if they overlap. > > Perhaps by adding "overlapped=True"? Something like that would be great, yes. > > - split won't split on empty patterns, e.g. empty lookahead patterns. > Already addressed (see issue2636 for the full details). Glad to hear it. > > - Repeated subgroup match information is not available. ?That is, for > > a match like this > > > ? ? re.match('(.){3}', 'xyz') > > > there's no way to discover that the subgroup first matched 'x', then > > matched 'y', and finally matched 'z'. ?Here is one past proposal > > (mine), perhaps over-complex, to address this problem: > > > ? ?http://mail.python.org/pipermail/python-dev/2004-August/047238.html > > Yikes! I think I'll let you code that... :-) I agree that that document looks a little scary--maybe I was trying to bite off too much at once. My intuition, though, is that the basic idea should be fairly simple to implement, at least for a depth-first matcher. The repeated match subgroups are already being discovered, it's just that they're not being saved, so there's no way to report them out once a complete match is found. If some trail of breadcrumbs were pushed onto a stack during the DFS, it could be traced at the end. And the whole thing might not even been that expensive to do. The hardest parts about this, in my mind, are figuring out how to report the repeated matches out in a useful form (hence all that detail in the proposal), and getting users to understand that using this feature *could* suck up a lot of memory, if they're not careful. As always, it's possible that my intuition is totally wrong. Plus I'm not sure how this would work out in the breadth-first case. Details aside, I would really, really, really like to have a way to get at the repeated subgroup matches. I write a lot of code that would be one-liners if this capability existed. Plus, it just plain burns me that Python is discovering this information but impudently refuses to tell me what it's found! ;-) From piet at cs.uu.nl Wed Jul 29 13:26:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 29 Jul 2009 19:26:26 +0200 Subject: open a file in python References: <5f84b536-dbaf-4758-b78e-ed9d10c57daa@h31g2000yqd.googlegroups.com> <7d5brmF2a1alcU1@mid.uni-berlin.de> <2b5b00b8-f9ba-4d16-b950-681aa6fa010a@24g2000yqm.googlegroups.com> Message-ID: >>>>> jayshree (j) wrote: >>> The Exact Problem is how to use the key containing by .pem file for >>> 'encryption' . can i print the contents of the .pem file I have already answered this question in a previous thread. It is not very helpful if you repeat asking the same or similar questions in different threads, especially if you don't take notice of the answers given. I will repeat it once here: load_pub_key requires a file name, not the contents of the file. So use rsa = M2Crypto.RSA.load_pub_key('my_key.public.pem') and leave the open line out. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From dandi.kain at gmail.com Wed Jul 29 13:59:55 2009 From: dandi.kain at gmail.com (dandi kain) Date: Wed, 29 Jul 2009 10:59:55 -0700 (PDT) Subject: Does underscore has any special built-in meaningin Python ? Message-ID: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Hello everybody, I have just started learning Python.I heard its simple so I pick a presentation [1] and tried to work on it.But when it comes to underscores leading and trailing an object I dont understand any.I look through the python manual also but that was not helping .I searched some forums and I still dont have a clear picture. What is the functionality of __ or _ , leading or trailing an object , class ot function ? Is it just a naming convention to note special functions and objects , or it really mean someting to Python ? Thanks ahead , [1] http://www.aleax.it/goo_py4prog.pdf From lemmath at gmail.com Wed Jul 29 14:04:49 2009 From: lemmath at gmail.com (Vincent Vega) Date: Wed, 29 Jul 2009 11:04:49 -0700 (PDT) Subject: Print value CLOB via cx_Oracle References: <6de8842b-f1c6-460e-a705-4f6dbd81492c@a26g2000yqn.googlegroups.com> Message-ID: <372c160b-eee2-46f3-913c-f75d84f45e11@c14g2000yqm.googlegroups.com> On 28 Lip, 20:02, Vincent Vega wrote: > Hi, > > I call function in Oracle database use cx_Oracle. > In standard I define: > > db ? ? ? = cx_Oracle.connect(username, password, tnsentry) > cursor ?= db.cursor() > > I create variable 'y' (result function 'fun_name') and call function > 'fun_name': > > y ? = cursor.var(cx_Oracle.CLOB) > cursor.callfunc(fun_name, cx_Oracle.CLOB, y) > > When I print variable 'y' I receive: > > > > How can I get value variable 'y'??? > How read ??? > > Vincent Vega > > Note: > Off course y.read() does not work, cause 'y'is CLOB not LOB. > (see datatypes table in site:http://www.oracle.com/technology/pub/articles/prez-python-queries.html) The solution is simple: print y.getvalue() (see: http://cx-oracle.sourceforge.net/html/variable.html) Thanks! :) Vincent Vega From davea at ieee.org Wed Jul 29 14:05:28 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:05:28 -0400 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> Message-ID: <4A708F68.1010505@ieee.org> Barak, Ron wrote: > Hi, > > I wanted to make a python byte-code file executable, expecting to be able to run it without specifying "python" on the (Linux bash) command line. > > So, I wrote the following: > > [root at VMLinux1 python]# cat test_pyc.py > #!/usr/bin/env python > > print "hello" > [root at VMLinux1 python]# > > and made its pyc file executable: > > [root at VMLinux1 python]# ls -ls test_pyc.pyc > 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > [root at VMLinux1 python]# > > So, I see: > > [root at VMLinux1 python]# file test_pyc.py* > test_pyc.py: a python script text executable > test_pyc.pyc: python 2.3 byte-compiled > [root at VMLinux1 python]# > > If I try to do the following, no problem: > > [root at VMLinux1 python]# python test_pyc.pyc > hello > [root at VMLinux1 python]# > > However, the following fails: > > [root at VMLinux1 python]# ./test_pyc.pyc > -bash: ./test_pyc.pyc: cannot execute binary file > [root at VMLinux1 python]# > > Is there a way to run a pyc file without specifying the python path ? > > Bye, > Ron. > > I don't currently run Unix, but I think I know the problem. In a text file, the shell examines the first line, and if it begins #! it's assumed to point to the executable of an interpreter for that text file. Presumably the same trick doesn't work for a .pyc file. Why not write a trivial wrapper.py file, don't compile it, and let that invoke the main code in the .pyc file? Then make wrapper.py executable, and you're ready to go. DaveA From benjamin.kaplan at case.edu Wed Jul 29 14:08:07 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 29 Jul 2009 14:08:07 -0400 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: On Wed, Jul 29, 2009 at 1:59 PM, dandi kain wrote: > > Hello everybody, > I have just started learning Python.I heard its simple so I pick a > presentation [1] and tried to work on it.But when it comes to > underscores leading and trailing an object I dont understand any.I > look through the python manual also but that was not helping .I > searched some forums and I still dont have a clear picture. > > What is the functionality of __ or _ , leading or trailing an object , > class ot function ? Is it just a naming convention to note special > functions and objects , or it really mean someting to Python ? It's just a convention for the most part. A single leading underscore is used for "private" attributes. Two leading underscores will affect the code- it mangles the variable name so that you don't have to worry about the value being overwritten by a subclass. For instance """ class Foo(object) : def __init__(self) : self.__bar = '' foo = Foo() "" will store the attribute as foo._Foo__bar. Also, the "magic methods"- the ones that are used for operations and built-in stuff, all have two leading and two trailing underscores. These are things like __add__ (+), __eq__ (=), __cmp__ (old way for comparisons), __len__ (len), __str__ (str), and so on. > > Thanks ahead , > > [1] http://www.aleax.it/goo_py4prog.pdf > -- > http://mail.python.org/mailman/listinfo/python-list From martin.hellwig at dcuktec.org Wed Jul 29 14:16:31 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Wed, 29 Jul 2009 19:16:31 +0100 Subject: simple splash screen? In-Reply-To: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: NighterNet wrote: > I am trying to make a simple splash screen from python 3.1.Not sure > where to start looking for it. Can any one help? Sure, almost the same as with Python 2 :-) But to be a bit more specific: ++++ """Only works if you got Python 3 installed with tkinter""" import tkinter IMAGE_PATH = "/path/to/image" class Splash(object): "Splash Screen GUI" def __init__(self, root): self.root = root # No window borders and decoration self.root.overrideredirect(True) # Get the size of the screen screen_width = self.root.winfo_screenwidth() screen_height = self.root.winfo_screenheight() # Full screen geometry_text = "%dx%d+0+0" % (screen_width, screen_height) self.root.geometry(geometry_text) # Display an image self.label = tkinter.Label(self.root) # Only GIF and PGM/PPM supported, for more information see: self.label._image = tkinter.PhotoImage(file=IMAGE_PATH) # http://effbot.org/tkinterbook/photoimage.htm self.label.configure(image = self.label._image) self.label.pack() # This will quit the screen after about 5 seconds self.root.after(5000, self.root.quit) if __name__ == '__main__': ROOT = tkinter.Tk() APPL = Splash(ROOT) ROOT.mainloop() ++++ -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From davea at ieee.org Wed Jul 29 14:19:32 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:19:32 -0400 Subject: Help with sql and python In-Reply-To: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> References: <3e53d632-0e8a-4d15-8409-8d0dd8ce3b47@o6g2000yqj.googlegroups.com> Message-ID: <4A7092B4.8090609@ieee.org> catalinfest at gmail.com wrote: > Hello ! > > I have accont on myphpadmin on my server. > I want to create one script in python. > This script manage sql task from my sql server . > How i make this in a simple way ? > > Thank you ! > > It's seldom simple. I'm guessing it's not your server, but is actually a webserver, located remotely, and to which you have only ftp access. Next questions are do you have access to the admins of the machine, what kind of machine is it, what OS is it running, does it have Python installed, what version(s)? Next questions are dependent on the answers to those questions. For example, on Unix systems you'll generally need to be able to chmod the file. But before you ask your admins those questions, you need to ask how you *want* to run the script. Creating it isn't enough, if you don't run it. You might be running it over the web, via URL, perhaps using CGI. You might be activating it via some remote console, or rsh. Or it might be a cron job. It might need various privileges to do what you expect. And if it's going to run continuously, or if it uses lots of resources, the admins may refuse it entirely. DaveA From electriclightheads at gmail.com Wed Jul 29 14:26:47 2009 From: electriclightheads at gmail.com ('2+) Date: Thu, 30 Jul 2009 03:26:47 +0900 Subject: instead of depending on data = array('h') .. write samples 1 by 1 to w = wave.open("wav.wav", "w") In-Reply-To: References: Message-ID: <1078018b0907291126t32c453a7h1f2a86fc270d3701@mail.gmail.com> o wow .. there's still a lot to learn .. okay .. if i get stucked with the memory usage issue will try this hybrid .. thanx for the example!! it took about 40 min to render 1min of wav so i'll just keep this project like 15 sec oriented and maybe that'll keep me away from the mem trouble and my oil.py is still so cheap.. sad thing is that "the result sound strange" is true to all versions :s thanx again anyway! On Wed, Jul 29, 2009 at 5:21 PM, Peter Otten<__peter__ at web.de> wrote: > '2+ wrote: > >> it says >> Wave_write.writeframes(data) >> will that mean >> "from array import array" >> is a must? >> >> this does the job: >> >> import oil >> import wave >> from array import array >> >> a = oil.Sa() >> >> w = wave.open("current.wav", "w") >> w.setnchannels(2) >> w.setsampwidth(2) >> w.setframerate(44100) >> >> data = array('h') >> >> for gas in range(44100 * 5): >> ? ? a.breath() >> ? ? r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) >> ? ? l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) >> ? ? data.append(r) >> ? ? data.append(l) >> >> w.writeframes(data.tostring()) >> w.close() >> >> don't like array becoming so huge so tested this and it was also okay: >> >> for gas in range(44100 * 5): >> ? ? a.breath() >> ? ? data = array('h') >> ? ? r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) >> ? ? l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) >> ? ? data.append(r) >> ? ? data.append(l) >> ? ? w.writeframes(data.tostring()) >> >> but without array .. it becomes 15secs(3 times longer than was >> intended to be) of wav file: >> >> for gas in range(44100 * 5): >> ? ? a.breath() >> ? ? r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) >> ? ? l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) >> ? ? w.writeframes(hex(r)) >> ? ? w.writeframes(hex(l)) > > Doesn't it sound strange, too? You are writing bogus bytes to the file. > Compare: > >>>> import array >>>> array.array("h", [42]).tostring() > '*\x00' >>>> hex(42) > '0x2a' > > Not only do they differ in length, the contents are different, too. If > you're unfamiliar with string escape codes, here's a way to see the bytes: > >>>> map(ord, array.array("h", [42]).tostring()) > [42, 0] >>>> map(ord, hex(42)) > [48, 120, 50, 97] > >> should i just be happy with depennding on using array? >> or is there a solution to make the last one work properly? > > There is a way to make the last one work: use struct.pack("h", r) instead of > hex(r). I'm of course assuming that the first version does give you the > desired result. You should not continue before you have verified that. > > I still recommend array for performance reasons. If memory usage is an issue > you can adopt a hybrid approach: > > # untested > from itertools import islice > from array import array > > def gen_data(): > ? ?for gas in xrange(44100 * 5): # range --> xrange > ? ? ? ?a.breath() > ? ? ? ?r = int(32767 * (a.pulse(1) + a.pulse(2) + a.pulse(3)) / 3.0) > ? ? ? ?l = int(32767 * (a.pulse(4) + a.pulse(5) + a.pulse(6)) / 3.0) > ? ? ? ?yield r > ? ? ? ?yield l > > data = gen_data() > N = 2**20 > while True: > ? ?chunk = array('h') > ? ?chunk.extend(islice(data, N)) > ? ?if not chunk: > ? ? ? ?break > ? ?w.writeframes(chunk.tostring()) > > This will limit the array size to 4N bytes. > > Peter > > -- > http://mail.python.org/mailman/listinfo/python-list > -- SaRiGaMa's Oil Vending Orchestra is podcasting: http://sarigama.namaste.jp/podcast/rss.xml and supplying oil.py for free: http://oilpy.blogspot.com/ From davea at ieee.org Wed Jul 29 14:33:25 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:33:25 -0400 Subject: escaping characters in filenames In-Reply-To: <854osv7h2k.fsf@agentultra.com> References: <854osv7h2k.fsf@agentultra.com> Message-ID: <4A7095F5.6090100@ieee.org> J Kenneth King wrote: > I wrote a script to process some files using another program. One thing > I noticed was that both os.listdir() and os.path.walk() will return > unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ > File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a > module or recipe that escapes file names and was wondering if anyone > could point me in the right direction. > > As an aside, the script is using subprocess.call() with the "shell=True" > parameter. There isn't really a reason for doing it this way (was just > the fastest way to write it and get a prototype working). I was > wondering if Popen objects were sensitive to unescaped names like the > shell. I intend to refactor the function to use Popen objects at some > point and thought perhaps escaping file names may not be entirely > necessary. > > Cheers > > There are dozens of meanings for escaping characters in strings. Without some context, we're wasting our time. For example, if the filename is to be interpreted as part of a URL, then spaces are escaped by using %20. Exactly who is going to be using this string you think you have to modify? I don't know of any environment which expects spaces to be escaped with backslashes. Be very specific. For example, if a Windows application is parsing its own command line, you need to know what that particular application is expecting -- Windows passes the entire command line as a single string. But of course you may be invoking that application using subprocess.Popen(), in which case some transformations happen to your arguments before the single string is built. Then some more transformations may happen in the shell. Then some more in the C runtime library of the new process (if it happens to be in C, and if it happens to use those libraries). I'm probably not the one with the answer. But until you narrow down your case, you probably won't attract the attention of whichever person has the particular combination of experience that you're hoping for. DaveA From jdgiotta at gmail.com Wed Jul 29 14:39:55 2009 From: jdgiotta at gmail.com (John D Giotta) Date: Wed, 29 Jul 2009 11:39:55 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: I'm working with up to 3 process "session" per server, each process running three threads. I was wishing to tie back the 3 "session"/server to a semaphore, but everything (and everyone) say semaphores are only good per process. From jdgiotta at gmail.com Wed Jul 29 14:43:07 2009 From: jdgiotta at gmail.com (John D Giotta) Date: Wed, 29 Jul 2009 11:43:07 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: <171f69c0-44ae-4624-bd03-a47032e82c21@n11g2000yqb.googlegroups.com> That was my original idea. Restricting each process by pid: #bash procs=`ps aux | grep script.pl | grep -v grep | wc -l` if [ $procs -lt 3 ]; then python2.4 script.py config.xml else exit 0 fi From lists at cheimes.de Wed Jul 29 14:53:49 2009 From: lists at cheimes.de (Christian Heimes) Date: Wed, 29 Jul 2009 20:53:49 +0200 Subject: Semaphore Techniques In-Reply-To: References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: John D Giotta schrieb: > I'm working with up to 3 process "session" per server, each process > running three threads. > I was wishing to tie back the 3 "session"/server to a semaphore, but > everything (and everyone) say semaphores are only good per process. That's not true. Named semaphores are the best solution for your problem and I said so yesterday. Christian From nobody at nowhere.com Wed Jul 29 14:55:16 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 19:55:16 +0100 Subject: escaping characters in filenames References: <854osv7h2k.fsf@agentultra.com> Message-ID: On Wed, 29 Jul 2009 09:29:55 -0400, J Kenneth King wrote: > I wrote a script to process some files using another program. One thing > I noticed was that both os.listdir() and os.path.walk() will return > unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ > File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a > module or recipe that escapes file names and was wondering if anyone > could point me in the right direction. > > As an aside, the script is using subprocess.call() with the "shell=True" > parameter. There isn't really a reason for doing it this way (was just > the fastest way to write it and get a prototype working). I was > wondering if Popen objects were sensitive to unescaped names like the > shell. I intend to refactor the function to use Popen objects at some > point and thought perhaps escaping file names may not be entirely > necessary. Note that subprocess.call() is nothing more than: def call(*popenargs, **kwargs): return Popen(*popenargs, **kwargs).wait() plus a docstring. It accepts exactly the same arguments as Popen(), with the same semantics. If you want to run a command given a program and arguments, you should pass the command and arguments as a list, rather than trying to construct a string. On Windows the value of shell= is unrelated to whether the command is a list or a string; a list is always converted to string using the list2cmdline() function. Using shell=True simply prepends "cmd.exe /c " to the string (this allows you to omit the .exe/.bat/etc extension for extensions which are in %PATHEXT%). On Unix, a string is first converted to a single-element list, so if you use a string with shell=False, it will be treated as the name of an executable to be run without arguments, even if contains spaces, shell metacharacters etc. The most portable approach seems to be to always pass the command as a list, and to set shell=True on Windows and shell=False on Unix. The only reason to pass a command as a string is if you're getting a string from the user and you want it to be interpreted using the platform's standard shell (i.e. cmd.exe or /bin/sh). If you want it to be interpreted the same way regardless of platform, parse it into a list using shlex.split(). From mdekauwe at gmail.com Wed Jul 29 14:56:28 2009 From: mdekauwe at gmail.com (Martin) Date: Wed, 29 Jul 2009 11:56:28 -0700 (PDT) Subject: set variable to looping index? Message-ID: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Hi, I am trying to set the return value from a function to a name which I grab from the for loop. I can't work out how I can do this without using an if statement... for f in var1_fn, var2_fn, var3_fn: if f.split('.')[0] == 'var1': var1 = call_some_function(f) . . . etc Really I would like to remove the need for this if loop and I am sure there is a simple way I am missing? Many thanks Martin From davea at ieee.org Wed Jul 29 14:56:55 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 14:56:55 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Message-ID: <4A709B77.9080209@ieee.org> Omer Khalid wrote: > Hi, > > I am having a very strange problem with modifying a variable in a list in my > program. Here is the code: > > # a list that contains dictionary objects > jobs = [] > > index=5 > for each in range(index): > jobs.append({'v':0}) > > some_function(index): > if jobs[index]['v'] == 0: > # set it to 1 > jobs[index]['v'] = 1 > print "Set to 1" > else: > print "Already set to 1" > > loop(): > index=0 > for each in range(len(jobs)): > some_function(index) > index +=1 > > > Apparently, the jobs[index]['v'] never get updated in the some_function but > the print statement afterwards get printed... > > What's really surprising is that there are no errors or exceptions and my my > program runs in a single thread...so i have been unable to explain this > behavior. > > Any insight would be much appreciated! > > Cheers > Omer > > There are four things to fix before the program does anything much at all. Two places you're missing the def, indentation is inconsistent, and you never actually call either of the functions. The first three are syntax errors, so presumably your cut/paste in your computer is broken. Once I make those four corrections, I get the following output: Set to 1 Set to 1 Set to 1 Set to 1 Set to 1 But you never said what you got, nor what you expected. That's certainly what I'd expect. And if you make a second call to loop() in your outer code, you get five copies of "Already set to 1" BTW, there are a number of things that could be done better. The main one I'll point out is that you shouldn't re-use a global variable 'index' as a local with different meaning. As someone else pointed out, since the global is a constant, making it all uppercase is the convention. DaveA From martin at v.loewis.de Wed Jul 29 15:06:31 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Wed, 29 Jul 2009 21:06:31 +0200 Subject: 64-bit issues with dictionaries in Python 2.6 In-Reply-To: References: Message-ID: <4a709db7$0$15886$9b622d9e@news.freenet.de> > Are there any known issues with dictionaries in Python 2.6 (not 2.6.2) > when running on a 64-bit platform? No, none. Regards, Martin From Omer.Khalid at cern.ch Wed Jul 29 15:10:44 2009 From: Omer.Khalid at cern.ch (Omer Khalid) Date: Wed, 29 Jul 2009 21:10:44 +0200 Subject: Very Strange Problem In-Reply-To: <4A709B77.9080209@ieee.org> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A709B77.9080209@ieee.org> Message-ID: <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> Hi Dave, Thanks for your reply. I actually didn't cut and paste my code as it was dispersed in different places, i typed the logic behind my code in the email (and obiviously made some typos, indentations is some thing else) but my real code does not have these problems as my application runs fine with out errors... Except that the line where i want to update the value doesn't get updated and no exception is thrown. What's surprising for me is that i am doing the same thing in hundreds of places in my 3k+ line code but by some reason this part doesn't work... As far as the global variables are concerned, i am using them in other places too and didn't see any problems. I think some thing else is going on here as the statement above and below my modified lines get executed. Is there a way in Python to debug memory address or to see where in memory this object is stored, and is there a lock on it or else? Thanks, Omer ************************************** On Wed, Jul 29, 2009 at 8:56 PM, Dave Angel wrote: > Omer Khalid wrote: > >> Hi, >> >> I am having a very strange problem with modifying a variable in a list in >> my >> program. Here is the code: >> >> # a list that contains dictionary objects >> jobs = [] >> >> index=5 >> for each in range(index): >> jobs.append({'v':0}) >> >> some_function(index): >> if jobs[index]['v'] == 0: >> # set it to 1 >> jobs[index]['v'] = 1 >> print "Set to 1" >> else: >> print "Already set to 1" >> >> loop(): >> index=0 >> for each in range(len(jobs)): >> some_function(index) >> index +=1 >> >> >> Apparently, the jobs[index]['v'] never get updated in the some_function >> but >> the print statement afterwards get printed... >> >> What's really surprising is that there are no errors or exceptions and my >> my >> program runs in a single thread...so i have been unable to explain this >> behavior. >> >> Any insight would be much appreciated! >> >> Cheers >> Omer >> >> >> > There are four things to fix before the program does anything much at all. > Two places you're missing the def, indentation is inconsistent, and you > never actually call either of the functions. The first three are syntax > errors, so presumably your cut/paste in your computer is broken. > > Once I make those four corrections, I get the following output: > > Set to 1 > Set to 1 > Set to 1 > Set to 1 > Set to 1 > > But you never said what you got, nor what you expected. That's certainly > what I'd expect. And if you make a second call to loop() in your outer > code, you get five copies of "Already set to 1" > > BTW, there are a number of things that could be done better. The main one > I'll point out is that you shouldn't re-use a global variable 'index' as a > local with different meaning. As someone else pointed out, since the global > is a constant, making it all uppercase is the convention. > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From darkneter at gmail.com Wed Jul 29 15:32:10 2009 From: darkneter at gmail.com (NighterNet) Date: Wed, 29 Jul 2009 12:32:10 -0700 (PDT) Subject: simple splash screen? References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> Message-ID: <79954da5-a437-48a1-985b-88abe601d021@a37g2000prf.googlegroups.com> On Jul 29, 11:16?am, "Martin P. Hellwig" wrote: > NighterNet wrote: > > I am trying to make a simple splash screen from python 3.1.Not sure > > where to start looking for it. Can any one help? > > Sure, almost the same as with Python 2 :-) > But to be a bit more specific: > ++++ > """Only works if you got Python 3 installed with tkinter""" > import tkinter > > IMAGE_PATH = "/path/to/image" > > class Splash(object): > ? ? ?"Splash Screen GUI" > ? ? ?def __init__(self, root): > ? ? ? ? ?self.root = root > ? ? ? ? ?# No window borders and decoration > ? ? ? ? ?self.root.overrideredirect(True) > ? ? ? ? ?# Get the size of the screen > ? ? ? ? ?screen_width = self.root.winfo_screenwidth() > ? ? ? ? ?screen_height = self.root.winfo_screenheight() > ? ? ? ? ?# Full screen > ? ? ? ? ?geometry_text = "%dx%d+0+0" % (screen_width, screen_height) > ? ? ? ? ?self.root.geometry(geometry_text) > ? ? ? ? ?# Display an image > ? ? ? ? ?self.label = tkinter.Label(self.root) > ? ? ? ? ?# Only GIF and PGM/PPM supported, for more information see: > ? ? ? ? ?self.label._image = tkinter.PhotoImage(file=IMAGE_PATH) > ? ? ? ? ?#http://effbot.org/tkinterbook/photoimage.htm > ? ? ? ? ?self.label.configure(image = self.label._image) > ? ? ? ? ?self.label.pack() > ? ? ? ? ?# This will quit the screen after about 5 seconds > ? ? ? ? ?self.root.after(5000, self.root.quit) > > if __name__ == '__main__': > ? ? ?ROOT = tkinter.Tk() > ? ? ?APPL = Splash(ROOT) > ? ? ?ROOT.mainloop() > ++++ > > -- > MPHhttp://blog.dcuktec.com > 'If consumed, best digested with added seasoning to own preference.' Thanks it help. Sorry about that, I was just wander what kind of answer and if there are other methods to learn it. Is there a way to position image to the center screen? From tjreedy at udel.edu Wed Jul 29 15:41:34 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 29 Jul 2009 15:41:34 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> Message-ID: Omer Khalid wrote: > Hi, > > I am having a very strange problem with modifying a variable in a list > in my program. Here is the code: To me, this sentence clearly implies that the code that follows is the code that had the problem. Since the posted code cannot run, it clearly is not. People should test code to be posted before posting unless they clearly label it as 'untested'. Original posters, of course, should run the code first. tjr From davea at ieee.org Wed Jul 29 15:42:39 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 15:42:39 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A709B77.9080209@ieee.org> <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> Message-ID: <4A70A62F.6080403@ieee.org> Omer Khalid wrote: > Hi Dave, > > Thanks for your reply. I actually didn't cut and paste my code as it was > dispersed in different places, i typed the logic behind my code in the email > (and obiviously made some typos, indentations is some thing else) but my > real code does not have these problems as my application runs fine with out > errors... > > Except that the line where i want to update the value doesn't get updated > and no exception is thrown. What's surprising for me is that i am doing the > same thing in hundreds of places in my 3k+ line code but by some reason this > part doesn't work... > > As far as the global variables are concerned, i am using them in other > places too and didn't see any problems. > > I think some thing else is going on here as the statement above and below my > modified lines get executed. > > Is there a way in Python to debug memory address or to see where in memory > this object is stored, and is there a lock on it or else? > > Thanks, > Omer > > > ************************************** > > > On Wed, Jul 29, 2009 at 8:56 PM, Dave Angel wrote: > > >> Omer Khalid wrote: >> >> >>> Hi, >>> >>> I am having a very strange problem with modifying a variable in a list in >>> my >>> program. Here is the code: >>> >>> # a list that contains dictionary objects >>> jobs = [] >>> >>> index=5 >>> for each in range(index): >>> jobs.append({'v':0}) >>> >>> some_function(index): >>> if jobs[index]['v'] == 0: >>> # set it to 1 >>> jobs[index]['v'] = 1 >>> print "Set to 1" >>> else: >>> print "Already set to 1" >>> >>> loop(): >>> index=0 >>> for each in range(len(jobs)): >>> some_function(index) >>> index +=1 >>> >>> >>> Apparently, the jobs[index]['v'] never get updated in the some_function >>> but >>> the print statement afterwards get printed... >>> >>> What's really surprising is that there are no errors or exceptions and my >>> my >>> program runs in a single thread...so i have been unable to explain this >>> behavior. >>> >>> Any insight would be much appreciated! >>> >>> Cheers >>> Omer >>> >>> >>> >>> >> There are four things to fix before the program does anything much at all. >> Two places you're missing the def, indentation is inconsistent, and you >> never actually call either of the functions. The first three are syntax >> errors, so presumably your cut/paste in your computer is broken. >> >> Once I make those four corrections, I get the following output: >> >> Set to 1 >> Set to 1 >> Set to 1 >> Set to 1 >> Set to 1 >> >> But you never said what you got, nor what you expected. That's certainly >> what I'd expect. And if you make a second call to loop() in your outer >> code, you get five copies of "Already set to 1" >> >> BTW, there are a number of things that could be done better. The main one >> I'll point out is that you shouldn't re-use a global variable 'index' as a >> local with different meaning. As someone else pointed out, since the global >> is a constant, making it all uppercase is the convention. >> >> DaveA >> >> (You top-posted, so your ,message is out of sequence. More and more people are doing that in this list.) ++++ ...Except that the line where i want to update the value doesn't get updated... And what makes you think that? You never answered my question. What did you expect for output, and what did you get? I got exactly what I expected, when I ran it. ++++ ... Is there a way in Python to debug memory address or ++++ to see where in memory this object is stored, and ++++ is there a lock on it or else? If there really were a bug in the language, you might need such a tool. I use Komodo IDE as a debugger, but there was no need in this case. Adding a few print statements might clear up your confusion, but since you haven't spelled out what it is, I can't suggest where. How about if you just add a print jobs at the beginning of some_function() ? Then you could see things getting updated perfectly. DaveA From zuo at chopin.edu.pl Wed Jul 29 15:54:51 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Wed, 29 Jul 2009 21:54:51 +0200 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: 29-07-2009 Benjamin Kaplan wrote: > On Wed, Jul 29, 2009 at 1:59 PM, dandi kain wrote: [snip >> What is the functionality of __ or _ , leading or trailing an object , >> class ot function ? Is it just a naming convention to note special >> functions and objects , or it really mean someting to Python ? > > It's just a convention for the most part. A single leading underscore > is used for "private" attributes. Two leading underscores will affect > the code- Single leading underscore in some situations also affect the code... See: * http://docs.python.org/reference/lexical_analysis.html#reserved-classes-of-identifiers * http://docs.python.org/reference/datamodel.html#object.__del__ (in the the red "Warning" frame) -- Jan Kaliszewski (zuo) From tjreedy at udel.edu Wed Jul 29 15:57:25 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 29 Jul 2009 15:57:25 -0400 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: Benjamin Kaplan wrote: > On Wed, Jul 29, 2009 at 1:59 PM, dandi kain wrote: >> Hello everybody, >> I have just started learning Python.I heard its simple so I pick a >> presentation [1] and tried to work on it.But when it comes to >> underscores leading and trailing an object I dont understand any.I >> look through the python manual also but that was not helping .I >> searched some forums and I still dont have a clear picture. >> >> What is the functionality of __ or _ , leading or trailing an object , >> class ot function ? Is it just a naming convention to note special >> functions and objects , or it really mean someting to Python ? > > It's just a convention for the most part. A single leading underscore > is used for "private" attributes. Two leading underscores will affect > the code- it mangles the variable name so that you don't have to worry > about the value being overwritten by a subclass. For instance > """ > class Foo(object) : > def __init__(self) : > self.__bar = '' > > foo = Foo() > "" > will store the attribute as foo._Foo__bar. > > Also, the "magic methods"- the ones that are used for operations and > built-in stuff, all have two leading and two trailing underscores. > These are things like __add__ (+), __eq__ (=), __cmp__ (old way for > comparisons), __len__ (len), __str__ (str), and so on. For this last, see http://docs.python.org/dev/py3k/reference/datamodel.html#special-method-names From no.email at please.post Wed Jul 29 16:05:36 2009 From: no.email at please.post (kj) Date: Wed, 29 Jul 2009 20:05:36 +0000 (UTC) Subject: How to "gunzip-iterate" over a file? Message-ID: I need to iterate over the lines of *very* large (>1 GB) gzipped files. I would like to do this without having to read the full compressed contents into memory so that I can apply zlib.decompress to these contents. I also would like to avoid having to gunzip the file (i.e. creating an uncompressed version of the file in the filesystem) prior to iterating over it. Basically I'm looking for something that will give me the same functionality as Perl's gzip IO layer, which looks like this (from the documentation): use PerlIO::gzip; open FOO, "<:gzip", "file.gz" or die $!; print while ; # And it will be uncompressed... What's the best way to achieve the same functionality in Python? TIA! kynn From malc0de.encrypt at gmail.com Wed Jul 29 16:21:56 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Wed, 29 Jul 2009 13:21:56 -0700 (PDT) Subject: Does python have the capability for driver development ? Message-ID: hello there, I've a question : I want to know does python have any capability for using Ring0 and kernel functions for driver and device development stuff . if there's such a feature it is very good, and if there something for this kind that you know please refer me to some reference and show me some snippet . thanks - Malc0de From robert.kern at gmail.com Wed Jul 29 16:22:43 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 29 Jul 2009 15:22:43 -0500 Subject: How to "gunzip-iterate" over a file? In-Reply-To: References: Message-ID: On 2009-07-29 15:05, kj wrote: > > > I need to iterate over the lines of *very* large (>1 GB) gzipped > files. I would like to do this without having to read the full > compressed contents into memory so that I can apply zlib.decompress > to these contents. I also would like to avoid having to gunzip > the file (i.e. creating an uncompressed version of the file in the > filesystem) prior to iterating over it. > > Basically I'm looking for something that will give me the same > functionality as Perl's gzip IO layer, which looks like this (from > the documentation): > > use PerlIO::gzip; > open FOO, "<:gzip", "file.gz" or die $!; > print while; # And it will be uncompressed... > > What's the best way to achieve the same functionality in Python? http://docs.python.org/library/gzip import gzip f = gzip.open('filename.gz') for line in f: print line f.close() -- 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 npuloski at gmail.com Wed Jul 29 16:23:33 2009 From: npuloski at gmail.com (Nanime Puloski) Date: Wed, 29 Jul 2009 16:23:33 -0400 Subject: Differences Between Arrays and Matrices in Numpy Message-ID: What are some differences between arrays and matrices using the Numpy library? When would I want to use arrays instead of matrices and vice versa? From pfeldman at verizon.net Wed Jul 29 16:26:23 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Wed, 29 Jul 2009 13:26:23 -0700 (PDT) Subject: delayed sys.exit? Message-ID: <24726902.post@talk.nabble.com> In the attached http://www.nabble.com/file/p24726902/test.py test.py code, it appears that additional statements execute after the call to sys.exit(0). I'll be grateful if anyone can shed light on why this is happening. Below is a copy of some sample I/O. Note that in the last case I get additional output after what should be the final error message. In [126]: run test Input a string: 1,2 [1, 2] In [127]: run test Input a string: 1-3 [1, 2, 3] In [128]: run test Input a string: 1,4,5-12 [1, 4, 5, 6, 7, 8, 9, 10, 11, 12] In [129]: run test Input a string: 0 ERROR: 0 is invalid; run numbers must be positive. ERROR: '0' is not a valid run number. -- View this message in context: http://www.nabble.com/delayed-sys.exit--tp24726902p24726902.html Sent from the Python - python-list mailing list archive at Nabble.com. From james at agentultra.com Wed Jul 29 16:33:11 2009 From: james at agentultra.com (J Kenneth King) Date: Wed, 29 Jul 2009 16:33:11 -0400 Subject: escaping characters in filenames References: <854osv7h2k.fsf@agentultra.com> Message-ID: <85ws5r5iwo.fsf@agentultra.com> Nobody writes: > On Wed, 29 Jul 2009 09:29:55 -0400, J Kenneth King wrote: > >> I wrote a script to process some files using another program. One thing >> I noticed was that both os.listdir() and os.path.walk() will return >> unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\ >> File\ With\ Spaces\ \&\ Stuff"). I haven't had much success finding a >> module or recipe that escapes file names and was wondering if anyone >> could point me in the right direction. >> >> As an aside, the script is using subprocess.call() with the "shell=True" >> parameter. There isn't really a reason for doing it this way (was just >> the fastest way to write it and get a prototype working). I was >> wondering if Popen objects were sensitive to unescaped names like the >> shell. I intend to refactor the function to use Popen objects at some >> point and thought perhaps escaping file names may not be entirely >> necessary. > > Note that subprocess.call() is nothing more than: > > def call(*popenargs, **kwargs): > return Popen(*popenargs, **kwargs).wait() > > plus a docstring. It accepts exactly the same arguments as Popen(), with > the same semantics. > > If you want to run a command given a program and arguments, you > should pass the command and arguments as a list, rather than trying to > construct a string. > > On Windows the value of shell= is unrelated to whether the command is > a list or a string; a list is always converted to string using the > list2cmdline() function. Using shell=True simply prepends "cmd.exe /c " to > the string (this allows you to omit the .exe/.bat/etc extension for > extensions which are in %PATHEXT%). > > On Unix, a string is first converted to a single-element list, so if you > use a string with shell=False, it will be treated as the name of an > executable to be run without arguments, even if contains spaces, shell > metacharacters etc. > > The most portable approach seems to be to always pass the command as a > list, and to set shell=True on Windows and shell=False on Unix. > > The only reason to pass a command as a string is if you're getting a > string from the user and you want it to be interpreted using the > platform's standard shell (i.e. cmd.exe or /bin/sh). If you want it to be > interpreted the same way regardless of platform, parse it into a > list using shlex.split(). I understand; I think I was headed towards subprocess.Popen() either way. It seems to handle the problem I posted about. And I got to learn a little something on the way. Thanks! Only now there's a new problem in that the output of the program is different if I run it from Popen than if I run it from the command line. The program in question is 'pdftotext'. More investigation to ensue. Thanks again for the helpful post. From robert.kern at gmail.com Wed Jul 29 16:36:03 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 29 Jul 2009 15:36:03 -0500 Subject: Differences Between Arrays and Matrices in Numpy In-Reply-To: References: Message-ID: On 2009-07-29 15:23, Nanime Puloski wrote: > What are some differences between arrays and matrices using the Numpy > library? When would I want to use arrays instead of matrices and vice > versa? You will want to ask numpy questions on the numpy mailing list: http://www.scipy.org/Mailing_Lists An overview of how the matrix subclass differs from ndarray, see the documentation: http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects Basically, I suggest that you just use regular arrays always. There is a syntactical convenience to matrix objects, but it does cause incompatibilities with the majority of code that is written for regular arrays. The convenience is usually not worth the cost. -- 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 apt.shansen at gmail.com Wed Jul 29 16:36:32 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Wed, 29 Jul 2009 13:36:32 -0700 Subject: delayed sys.exit? In-Reply-To: <24726902.post@talk.nabble.com> References: <24726902.post@talk.nabble.com> Message-ID: <7a9c25c20907291336k5b4686c9sb410710967307907@mail.gmail.com> > > In the attached http://www.nabble.com/file/p24726902/test.py test.py > code, > it appears that additional statements execute after the call to > sys.exit(0). > I'll be grateful if anyone can shed light on why this is happening. Below > is a copy of some sample I/O. Note that in the last case I get additional > output after what should be the final error message. > A bare "except:" catches ALL exceptions; including SystemExit which is generated by sys.exit. And KeyboardInterrupt, too. That's why its basically a bad idea to use bare excepts unless you really, really, really need to. Try 'except Exception' instead. SystemExit and such do not inherit from Exception. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Jul 29 16:40:13 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jul 2009 22:40:13 +0200 Subject: delayed sys.exit? References: Message-ID: Dr. Phillip M. Feldman wrote: > In the attached http://www.nabble.com/file/p24726902/test.py test.py > code, it appears that additional statements execute after the call to > sys.exit(0). > try: > # If the conversion to int fails, nothing is appended to the list: > Runs.append(int(strs[i])) > if Runs[-1] <= 0: > print 'ERROR: ' + str(Runs[-i]) + \ > ' is invalid; run numbers must be positive.' > sys.exit(0) > except: sys.exit() works by raising a SystemExit exception which is caught by the bare except. http://docs.python.org/library/sys.html#sys.exit http://docs.python.org/library/exceptions.html#exceptions.SystemExit As a general rule try to be as specific as possible when catching exceptions. Peter From python at mrabarnett.plus.com Wed Jul 29 16:41:37 2009 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 29 Jul 2009 21:41:37 +0100 Subject: delayed sys.exit? In-Reply-To: <24726902.post@talk.nabble.com> References: <24726902.post@talk.nabble.com> Message-ID: <4A70B401.9040700@mrabarnett.plus.com> Dr. Phillip M. Feldman wrote: > In the attached http://www.nabble.com/file/p24726902/test.py test.py code, > it appears that additional statements execute after the call to sys.exit(0). > I'll be grateful if anyone can shed light on why this is happening. Below > is a copy of some sample I/O. Note that in the last case I get additional > output after what should be the final error message. > > In [126]: run test > Input a string: 1,2 > [1, 2] > > In [127]: run test > Input a string: 1-3 > [1, 2, 3] > > In [128]: run test > Input a string: 1,4,5-12 > [1, 4, 5, 6, 7, 8, 9, 10, 11, 12] > > In [129]: run test > Input a string: 0 > ERROR: 0 is invalid; run numbers must be positive. > ERROR: '0' is not a valid run number. > sys.exit raises an SystemExit exception, which then gets caught by the bare 'except' of the enclosing try...except... statement. A lot of things can raise an exception, which is why bare 'except's are a bad idea; catch only those you expect. From Russ.Davis at njpines.state.nj.us Wed Jul 29 16:55:27 2009 From: Russ.Davis at njpines.state.nj.us (Russ Davis) Date: Wed, 29 Jul 2009 16:55:27 -0400 Subject: IDLE Config Problems Message-ID: <4A70801E.DEDA.009D.0@njpines.state.nj.us> I am just getting started with Python and have installed v. 2.5.4 Idle version 1.2.4 I can't seem to get the idle to display text. It seems as if the install went fine. I start up the idle and the screen is blank. No text. It seems as if I can type on the screen but I just can't see the characters. I go to the config menu and it bombs and brings up the Visual Studio debugger. The computer that I am trying to install it on is a windows xp laptop serv pack 2. I have a workstation here that I am using also with the same os and the idle works fine. Any hints as to what might be interfering with the idle config. Thanks Russ Russell Davis PP, AICP, GISP GIS Administrator State of New Jersey Pinelands Commission Office of Land Use and Technology GIS Laboratory Po Box 7 New Lisbon, NJ 08064 Phone 609-894-7300 Fax 609-894-7330 russ.davis at njpines.state.nj.us From __peter__ at web.de Wed Jul 29 17:06:30 2009 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Jul 2009 23:06:30 +0200 Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: Martin wrote: > I am trying to set the return value from a function to a name which I > grab from the for loop. I can't work out how I can do this without > using an if statement... > > for f in var1_fn, var2_fn, var3_fn: > if f.split('.')[0] == 'var1': > var1 = call_some_function(f) > . > . > . > etc > > Really I would like to remove the need for this if loop and I am sure > there is a simple way I am missing? Use dictionaries: functions = {"var1": some_function, "var2": some_other_function, ...} return_values = {} for arg in var1_fn, var2_fn, var3_fn: key = arg.split(".")[0] return_values[key] = functions[key](arg) Peter From martin.hellwig at dcuktec.org Wed Jul 29 17:08:23 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Wed, 29 Jul 2009 22:08:23 +0100 Subject: simple splash screen? In-Reply-To: <79954da5-a437-48a1-985b-88abe601d021@a37g2000prf.googlegroups.com> References: <79a6c64f-af43-455f-a989-16dd6bb9e30e@a39g2000pre.googlegroups.com> <79954da5-a437-48a1-985b-88abe601d021@a37g2000prf.googlegroups.com> Message-ID: NighterNet wrote: > Thanks it help. Sorry about that, I was just wander what kind of > answer and if there are other methods to learn it. > > Is there a way to position image to the center screen? Yes there is, just start reading from here: http://effbot.org/tkinterbook/ Though because Python 3 has done some module reorganisation/renaming, Tkinter is now called tkinter. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From deets at nospam.web.de Wed Jul 29 17:11:49 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 29 Jul 2009 23:11:49 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: References: Message-ID: <7dbs8nF2br07nU1@mid.uni-berlin.de> MalC0de schrieb: > hello there, I've a question : > I want to know does python have any capability for using Ring0 and > kernel functions for driver and device development stuff . > if there's such a feature it is very good, and if there something for > this kind that you know please refer me to some reference and show me > some snippet . No, it can't do such things. At least it isn't embedded in the kernel - in theory that might be possible, but given the timing-constraints and concurrency-requirements, it's not really feasible. Diez From bearophileHUGS at lycos.com Wed Jul 29 17:12:34 2009 From: bearophileHUGS at lycos.com (Bearophile) Date: Wed, 29 Jul 2009 14:12:34 -0700 (PDT) Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: Martin: > for f in var1_fn, var2_fn, var3_fn: > ? ? if f.split('.')[0] == 'var1': > ? ? ? ? var1 = call_some_function(f) > ? ? ? ? . > ? ? ? ? . > ? ? ? ? . > ? ? ? etc As usual I have big problems in understanding what you want to do. Please, show an example of the contents of var1_fn, var2_fn, etc. Generally you can create a dict: results = {} And then fill it with name-result pairs: name = value.split('_')[0] results[name] = some_function(value) Bye, bearophile From andre.roberge at gmail.com Wed Jul 29 17:18:52 2009 From: andre.roberge at gmail.com (=?ISO-8859-1?Q?Andr=E9?=) Date: Wed, 29 Jul 2009 14:18:52 -0700 (PDT) Subject: how to embed the python interpreter into web App (Mehndi, Sibtey) References: Message-ID: <4c2f2ee0-1009-4dc7-8cd7-b38db43d83c7@o32g2000yqm.googlegroups.com> On Jul 29, 1:11?pm, Ryniek90 wrote: > > Hi All > > > I am trying to embed the python interpreter in to a web app but could > > not get the way, any one can suggest me how to do this. > > > Thanks, > > > Sibtey Mehdi > > > This e-mail (and any attachments), is confidential and may be privileged. It may be read, copied and used only > > by intended recipients. Unauthorized access to this e-mail (or attachments) and disclosure or copying of its > > contents or any action taken in reliance on it is unlawful. Unintended recipients must notify the sender immediately > > by e-mail/phone & delete it from their system without making any copies or disclosing it to a third person. > > Here's example:http://try-python.mired.org/ > > Contact with author of that website/webapp. > > Good luck. Or you can look at the code for Crunchy: http://code.google.com/p/crunchy Note however that this will result in something that is not secure... To quote the try-python site: "My ISP (idiom.com) provides a sandbox inside a FreeBSD Jail..." Andr? From davea at ieee.org Wed Jul 29 17:47:43 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Jul 2009 17:47:43 -0400 Subject: set variable to looping index? In-Reply-To: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: <4A70C37F.2030709@ieee.org> Martin wrote: > Hi, > > I am trying to set the return value from a function to a name which I > grab from the for loop. I can't work out how I can do this without > using an if statement... > > for f in var1_fn, var2_fn, var3_fn: > if f.split('.')[0] == 'var1': > var1 = call_some_function(f) > . > . > . > etc > > Really I would like to remove the need for this if loop and I am sure > there is a simple way I am missing? > > Many thanks > > Martin > > Is this a real problem, or is it a "programming puzzle"? If it's the latter, maybe someone else can help. But if it's a real problem, give us some context, and maybe we can figure out how to help. If I took this fragment at face value, I'd simply replace it by: var1 = call_some_function(var1_fn) var2 = call_some_function(var2_fn) var3 = call_some_function(var3_fn) Is this fragment part of a function definition, or is it top-level? Are there really exactly three var*_fn objects, or might there be an arbitrary number of them? Do you want to tell us the types of these three objects? Is there content really tied directly to their name? Did they get their values from literals, or were they computed at some other point? DaveA From rhodri at wildebst.demon.co.uk Wed Jul 29 18:02:00 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 29 Jul 2009 23:02:00 +0100 Subject: set variable to looping index? In-Reply-To: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: On Wed, 29 Jul 2009 19:56:28 +0100, Martin wrote: > Hi, > > I am trying to set the return value from a function to a name which I > grab from the for loop. I can't work out how I can do this without > using an if statement... > > for f in var1_fn, var2_fn, var3_fn: > if f.split('.')[0] == 'var1': > var1 = call_some_function(f) > . > . > . > etc > > Really I would like to remove the need for this if loop and I am sure > there is a simple way I am missing? It's a little hard to tell what you actually want from your description, but it looks like you're fighting the language unnecessarily here. If you have a sequence of functions that you want a sequence of results out of, you should be thinking in terms of a sequence type. A list, in other words. results = [] for f in fn1, fn2, fn3: results.append(f()) -- Rhodri James *-* Wildebeest Herder to the Masses From nobody at nowhere.com Wed Jul 29 18:21:27 2009 From: nobody at nowhere.com (Nobody) Date: Wed, 29 Jul 2009 23:21:27 +0100 Subject: Differences Between Arrays and Matrices in Numpy References: Message-ID: On Wed, 29 Jul 2009 16:23:33 -0400, Nanime Puloski wrote: > What are some differences between arrays and matrices using the Numpy > library? Matrices are always two-dimensional, as are slices of them. Matrices override mulitplication and exponentiation to use matrix multiplication rather than element-wise multiplication. > When would I want to use arrays instead of matrices and vice > versa? Use a matrix if you want a matrix, i.e. a linear transformation. Otherwise, use an array. From mdekauwe at gmail.com Wed Jul 29 18:29:30 2009 From: mdekauwe at gmail.com (Martin) Date: Wed, 29 Jul 2009 15:29:30 -0700 (PDT) Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> Message-ID: <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> On Jul 29, 11:02?pm, "Rhodri James" wrote: > On Wed, 29 Jul 2009 19:56:28 +0100, Martin wrote: > > Hi, > > > I am trying to set the return value from a function to a name which I > > grab from the for loop. I can't work out how I can do this without > > using an if statement... > > > for f in var1_fn, var2_fn, var3_fn: > > ? ? if f.split('.')[0] == 'var1': > > ? ? ? ? var1 = call_some_function(f) > > ? ?. > > ? ? ? ? . > > ? ? ? ? . > > ? ? ? etc > > > ?Really I would like to remove the need for this if loop and I am sure > > there is a simple way I am missing? > > It's a little hard to tell what you actually want from your description, > but it looks like you're fighting the language unnecessarily here. ?If > you have a sequence of functions that you want a sequence of results > out of, you should be thinking in terms of a sequence type. ?A list, > in other words. > > results = [] > for f in fn1, fn2, fn3: > ? ? ?results.append(f()) > > -- > Rhodri James *-* Wildebeest Herder to the Masses Hi all, Thanks and apologises I wasn't trying to be cryptic, like all things when I wrote it I thought it was quite transparent. All I was trying to do was call a function and return the result to an a variable. I could admittedly of just done... var1 = some_function(var1_fn) var2 = some_function(var2_fn) var3 = some_function(var3_fn) where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn = 'x.txt'. But I figured I would try and make it slightly more generic whilst I was at it, hence my attempt to use the filenames to create the variable names (hence the loop). I will as suggested try the dictionary option. I appreciate all the suggestions. Thanks Martin From nick at craig-wood.com Wed Jul 29 18:29:56 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Wed, 29 Jul 2009 17:29:56 -0500 Subject: Does python have the capability for driver development ? References: <7dbs8nF2br07nU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > MalC0de schrieb: > > hello there, I've a question : > > I want to know does python have any capability for using Ring0 and > > kernel functions for driver and device development stuff . > > if there's such a feature it is very good, and if there something for > > this kind that you know please refer me to some reference and show me > > some snippet . > > No, it can't do such things. At least it isn't embedded in the kernel - > in theory that might be possible, but given the timing-constraints and > concurrency-requirements, it's not really feasible. You can write FUSE (file systems in userspace) drivers in python I believe. Not the same as running in ring0 but in most senses a kernel driver... -- Nick Craig-Wood -- http://www.craig-wood.com/nick From pavlovevidence at gmail.com Wed Jul 29 18:55:26 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 29 Jul 2009 15:55:26 -0700 (PDT) Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> Message-ID: <8cb1ba65-be71-49b7-8955-54c17f351a40@w6g2000yqw.googlegroups.com> On Jul 29, 7:14?am, Piet van Oostrum wrote: > >>>>> Carl Banks (CB) wrote: > >CB> On Jul 28, 3:15?pm, John D Giotta wrote: > >>> I'm looking to run a process with a limit of 3 instances, but each > >>> execution is over a crontab interval. I've been investigating the > >>> threading module and using daemons to limit active thread objects, but > >>> I'm not very successful at grasping the documentation. > > >>> Is it possible to do what I'm trying to do and if so anyone know of a > >>> useful example to get started? > >CB> It seems like you want to limit the number of processes to three; the > >CB> threading module won't help you there because it deals with threads > >CB> within a single process. > >CB> What I'd do is to simply run the system ps to see how many processes > >CB> are running (ps is pretty versatile on most systems and can find > >CB> specifically targeted processes like you program), and exit if there > >CB> are already three. > > That will surely run into some race conditions. What, the OS might not have gotten around to update the process table to include a process started minutes ago? (He said he was starting the processes over crontab intervals, not that he communicated what he wanted well.) Carl Banks From zuo at chopin.edu.pl Wed Jul 29 18:57:02 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 00:57:02 +0200 Subject: set variable to looping index? In-Reply-To: <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> Message-ID: 30-07-2009 wrote: > All I was trying to do was call a function and return the result to an > a variable. I could admittedly of just done... > > var1 = some_function(var1_fn) > var2 = some_function(var2_fn) > var3 = some_function(var3_fn) > > where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn = > 'x.txt'. But I figured I would try and make it slightly more generic > whilst I was at it, hence my attempt to use the filenames to create > the variable names (hence the loop). Hi, Then you could also consider using simply lists: filenames = p'foo', 'bar', baz'] results = [] for name in filenames: results.append(some_function(name)) If filenames were generated according to a particular pattern, you can mimic that pattern and generate filenames list using list-comprehension, e.g.: filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)] Chreers, *j -- Jan Kaliszewski (zuo) From zuo at chopin.edu.pl Wed Jul 29 19:03:33 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 01:03:33 +0200 Subject: set variable to looping index? In-Reply-To: References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> Message-ID: Me wrote: > filenames = p'foo', 'bar', baz'] Sorry, should be of course: filenames = ['foo', 'bar', baz'] *j -- Jan Kaliszewski (zuo) From mdekauwe at gmail.com Wed Jul 29 19:04:59 2009 From: mdekauwe at gmail.com (Martin) Date: Wed, 29 Jul 2009 16:04:59 -0700 (PDT) Subject: set variable to looping index? References: <7ba88464-d33a-49e3-8263-459edfc5f9aa@c14g2000yqm.googlegroups.com> <0832103a-f0b4-4bd7-a423-a40884b973dc@o32g2000yqm.googlegroups.com> Message-ID: On Jul 29, 11:57?pm, "Jan Kaliszewski" wrote: > 30-07-2009 wrote: > > All I was trying to do was call a function and return the result to an > > a variable. I could admittedly of just done... > > > var1 = some_function(var1_fn) > > var2 = some_function(var2_fn) > > var3 = some_function(var3_fn) > > > where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn = > > 'x.txt'. But I figured I would try and make it slightly more generic > > whilst I was at it, hence my attempt to use the filenames to create > > the variable names (hence the loop). > > Hi, > > Then you could also consider using simply lists: > > filenames = p'foo', 'bar', baz'] > results = [] > for name in filenames: > ? ? ?results.append(some_function(name)) > > If filenames were generated according to a particular pattern, you can > mimic that pattern and generate filenames list using > list-comprehension, e.g.: > > filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)] > > Chreers, > > *j > > -- > Jan Kaliszewski (zuo) I guess I wanted to keep the function returns in separate arrays in this case, hence my attempt to make variable names based on the filenames. Thanks Martin From martin.hellwig at dcuktec.org Wed Jul 29 19:05:04 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 00:05:04 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: References: Message-ID: MalC0de wrote: > hello there, I've a question : > I want to know does python have any capability for using Ring0 and > kernel functions for driver and device development stuff . > if there's such a feature it is very good, and if there something for > this kind that you know please refer me to some reference and show me > some snippet . > > thanks > > - Malc0de Python is interpreted, so the first requirement would be that the interpreter (the python VM to be more precise) would run in the kernel or that there is a way for the interpreter to delegate operations to kernel restricted operations. Most notably access to the memory location of the hardware you want to write a driver for and possibly also a way to pass through a callback function for triggered interrupt coming from the hardware. So technically speaking it shouldn't be impossible. And there is perhaps something to say for being able to write drivers in a readable/easy programming language however I am afraid that if such a beast would be made that it would remain an academical exercise only due to performance constraints. Though I would love to play around with a system where the kernel is essentially only a python interpreter, with full raw access to the hardware. But creating such a thing requires more talent and understanding than currently (and probably ever) in my possession. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From cjw at ncf.ca Wed Jul 29 19:27:41 2009 From: cjw at ncf.ca (Colin J. Williams) Date: Wed, 29 Jul 2009 19:27:41 -0400 Subject: Differences Between Arrays and Matrices in Numpy In-Reply-To: References: Message-ID: Robert Kern wrote: > On 2009-07-29 15:23, Nanime Puloski wrote: >> What are some differences between arrays and matrices using the Numpy >> library? When would I want to use arrays instead of matrices and vice >> versa? > > You will want to ask numpy questions on the numpy mailing list: > > http://www.scipy.org/Mailing_Lists > > An overview of how the matrix subclass differs from ndarray, see the > documentation: > > > http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects > > > Basically, I suggest that you just use regular arrays always. There is a > syntactical convenience to matrix objects, but it does cause > incompatibilities with the majority of code that is written for regular > arrays. The convenience is usually not worth the cost. > Numpy's arrays can have any dimensionality, whereas matrices[http://en.wikipedia.org/wiki/Matrix_%28mathematics%29], typically have two. A single column can represent a vector or a single row can represent a transposed vector. Does the additional cost arise because the commonly used procedures are accessed through numpy's array? Colin W. From rwanderley at rsw.digi.com.br Wed Jul 29 19:30:21 2009 From: rwanderley at rsw.digi.com.br (Rodrigo S Wanderley) Date: Wed, 29 Jul 2009 20:30:21 -0300 Subject: Does python have the capability for driver development ? In-Reply-To: (Martin P. Hellwig's message of "Thu, 30 Jul 2009 00:05:04 +0100") References: Message-ID: <87skgf83ua.fsf@rsw.digi.com.br> "Martin P. Hellwig" writes: > Python is interpreted, so the first requirement would be that the > interpreter (the python VM to be more precise) would run in the kernel > or that there is a way for the interpreter to delegate operations to > kernel restricted operations. Most notably access to the memory > location of the hardware you want to write a driver for and possibly > also a way to pass through a callback function for triggered > interrupt coming from the hardware. > > So technically speaking it shouldn't be impossible. And there is > perhaps something to say for being able to write drivers in a > readable/easy programming language however I am afraid that if such a > beast would be made that it would remain an academical exercise only > due to performance constraints. What about user level device drivers? Think the Python VM could communicate with the driver through the user space API. Is there a Python module for that? -- Rodrigo S. Wanderley -- Blog: http://rsw.digi.com.br From david.lyon at preisshare.net Wed Jul 29 19:33:01 2009 From: david.lyon at preisshare.net (David Lyon) Date: Wed, 29 Jul 2009 19:33:01 -0400 Subject: Does python have the capability for driver development =?UTF-8?Q?=3F?= In-Reply-To: References: Message-ID: MalC0de wrote: > hello there, I've a question : > I want to know does python have any capability for using Ring0 and > kernel functions for driver and device development stuff . > if there's such a feature it is very good, and if there something for > this kind that you know please refer me to some reference and show me > some snippet . What operating system are you talking about? Most device drivers run at ring 3 (or lower) and not zero. This way if there is a driver crash the whole operating system doesn't freeze. Python is generally considered a high-level language. If you want to play around with drivers.. usb serial.. etc do it at a python level through the existing device drivers. imho the performance of interpreted python isn't compatible with writing block-mode device drivers (hard-disks) and so forth. What hardware do you have that you need to write a device driver for ? Isn't there a device driver available already? or do you mean just a device controller? David From robert.kern at gmail.com Wed Jul 29 19:42:05 2009 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 29 Jul 2009 18:42:05 -0500 Subject: Differences Between Arrays and Matrices in Numpy In-Reply-To: References: Message-ID: On 2009-07-29 18:27, Colin J. Williams wrote: > Robert Kern wrote: >> On 2009-07-29 15:23, Nanime Puloski wrote: >>> What are some differences between arrays and matrices using the Numpy >>> library? When would I want to use arrays instead of matrices and vice >>> versa? >> >> You will want to ask numpy questions on the numpy mailing list: >> >> http://www.scipy.org/Mailing_Lists >> >> An overview of how the matrix subclass differs from ndarray, see the >> documentation: >> >> http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects >> >> >> Basically, I suggest that you just use regular arrays always. There is >> a syntactical convenience to matrix objects, but it does cause >> incompatibilities with the majority of code that is written for >> regular arrays. The convenience is usually not worth the cost. > > Numpy's arrays can have any dimensionality, whereas > matrices[http://en.wikipedia.org/wiki/Matrix_%28mathematics%29], > typically have two. A single column can represent a vector or a single > row can represent a transposed vector. > > Does the additional cost arise because the commonly used procedures are > accessed through numpy's array? Most functions are written to expect that its inputs behave like ndarrays; e.g. a*b is elementwise multiplication rather than matrix multiplication. When you use the matrix subclass, you are basically confining yourself to a smallish ghetto of functions that knows how to deal with matrix semantics. That's a huge cost compared to the relatively small syntactic cost of having to write dot(a,b) instead of (a*b). -- 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 ben+python at benfinney.id.au Wed Jul 29 19:42:35 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 30 Jul 2009 09:42:35 +1000 Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: <87ljm72h04.fsf@benfinney.id.au> dandi kain writes: > What is the functionality of __ or _ , leading or trailing an object , > class ot function ? There is no change in functionality. It has some slight effects on import, but as a beginner you shouldn't need to worry about that. > Is it just a naming convention to note special functions and objects , > or it really mean someting to Python ? Both. It's a strongly-adhered-to naming convention, as an indicator to the reader how that name should be used. foo Ordinary name, part of public interface _foo Ordinary name, part of internal-only interface __foo Ordinary name, but will be mangled (this style used rarely) __foo__ Name which is used in a special way by Python There's no change in the objects themselves; the underscores rather indicate how those names are expected to be accessed. -- \ ?Pinky, are you pondering what I'm pondering?? ?I think so, | `\ Brain, but Tuesday Weld isn't a complete sentence.? ?_Pinky and | _o__) The Brain_ | Ben Finney From martin.hellwig at dcuktec.org Wed Jul 29 19:44:41 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 00:44:41 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <87skgf83ua.fsf@rsw.digi.com.br> References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: <4A70DEE9.1060105@dcuktec.org> Rodrigo S Wanderley wrote: > > What about user level device drivers? Think the Python VM could > communicate with the driver through the user space API. Is there a > Python module for that? > > Sure why not? Look for example to libusb, which provides a userspace environment and pyusb which uses that and provides an interface to Python. -- MPH From nyamatongwe+thunder at gmail.com Wed Jul 29 19:55:34 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Wed, 29 Jul 2009 23:55:34 GMT Subject: No PyPI search for 3.x compatible packages Message-ID: There appears to be no way to search PyPI for packages that are compatible with Python 3.x. There are classifiers for 'Programming Language' including 'Programming Language :: Python :: 3' but that seems to be for implementation language since there are so many packages that specify C. There are a total of 109 packages classified with Python :: [3, 3.0, 3.1] out of a total of 4829 packages. http://pypi.python.org/pypi?:action=browse&show=all&c=214&c=533 The search box appears to search for any word entered so a search like "xml 3.0" or "xml AND 3.0" does not help. Some packages include version information in the Py Version column of their download lists or embedded in the download file names. Projects are often constrained to a particular set of Python versions so need to choose packages that will work with those versions. It would be helpful if PyPI made this information more visible and searchable. Neil From malc0de.encrypt at gmail.com Wed Jul 29 20:19:42 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Wed, 29 Jul 2009 17:19:42 -0700 (PDT) Subject: Does python have the capability for driver development ? References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: <94abbddc-dac7-4215-84d1-d4eb3f82724d@h21g2000yqa.googlegroups.com> actually I mean driver programming under Windows operating system, if you know, there's A kit name DDK available at microsoft's website for developing device drivers under C / C++ environment, now i'm working with this kind of toolkit, but when I started programming with Python, I saw this is very good and easy to develop application in all aspects of a programmer or customer . I love python cuze I can write every application i want, it's easy and it's very robust, But i thought if there's any available toolkit like Microsoft DDK which can program driver under windows but based on python it's very good and well, I don't know it's the lack of python or interpreter concept but cuze of the extensibility of python if such a good feature can be added to python it will be good enough. actually I want to write device drivers in Ring0 mode or kernel mode ... please introduce me some resource for system programming, I know that python is very well at system programming level. thanks - Malc0de From wuwei23 at gmail.com Wed Jul 29 20:24:03 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 29 Jul 2009 17:24:03 -0700 (PDT) Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: <11942b01-07b4-44d9-9be5-92868493b706@g1g2000pra.googlegroups.com> On Jul 30, 3:59?am, dandi kain wrote: > What is the functionality of __ or _ , leading or trailing an object , > class ot function ? Is it just a naming convention to note special > functions and objects , or it really mean someting to Python ? I think everyone else has covered what you need to know, but there's still one remaining pseudo-convention you might see in Python code, and that's using _ as a label to indicate that you don't care about the value it holds. Overly simplistic example: data_set = [('gold','junk'),('gold','junk'),...] for keep, _ in data_set: ... It's a convenient way of not having to come up with a label for something you're not going to use, although arguably you get the same effect with names like 'dummy', 'ignore' etc. Not everyone agrees with this usage but you _will_ see it in use in other people's code, so it helps to have a heads up. From marcusw at cox.net Wed Jul 29 20:24:16 2009 From: marcusw at cox.net (Marcus Wanner) Date: Wed, 29 Jul 2009 20:24:16 -0400 Subject: Does python have the capability for driver development ? In-Reply-To: References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: On 7/29/2009 7:44 PM, Martin P. Hellwig wrote: > Rodrigo S Wanderley wrote: >> >> What about user level device drivers? Think the Python VM could >> communicate with the driver through the user space API. Is there a >> Python module for that? >> >> > Sure why not? > Look for example to libusb, which provides a userspace environment and > pyusb which uses that and provides an interface to Python. > iicr pyusb uses a c interface to libusb, not python... Marcus From http Wed Jul 29 21:04:47 2009 From: http (Paul Rubin) Date: 29 Jul 2009 18:04:47 -0700 Subject: How to "gunzip-iterate" over a file? References: Message-ID: <7x8wi7j80g.fsf@ruckus.brouhaha.com> Robert Kern writes: > f = gzip.open('filename.gz') > for line in f: > print line or use f.read(nbytes) to read n uncompressed bytes from f. Note that the standard iterator (which iterates over lines) can potentially consume an unbounded amount of memory if the file contains no newlines. From greg at cosc.canterbury.ac.nz Wed Jul 29 21:09:14 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Thu, 30 Jul 2009 13:09:14 +1200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <200907281035.48713.hendrik@microcorp.co.za> <4A6F1506.9030207@mrabarnett.plus.com> Message-ID: <7dca4iF2bgbn5U1@mid.individual.net> Hendrik van Rooyen wrote: > And if code is data, where is Pythons ALTER statement? class Duck: def quack(self): print "Quack!" def moo(): print "Moo!" def ALTER(obj, name, TO_PROCEED_TO): setattr(obj, name, TO_PROCEED_TO) d = Duck() ALTER(d, 'quack', TO_PROCEED_TO = moo) d.quack() -- Greg From clp2 at rebertia.com Wed Jul 29 21:21:32 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 18:21:32 -0700 Subject: Working with platform.system() In-Reply-To: <9a5b5be90907290610t1868ef07p20a5c2bdc3404ab8@mail.gmail.com> References: <9a5b5be90907290610t1868ef07p20a5c2bdc3404ab8@mail.gmail.com> Message-ID: <50697b2c0907291821l54f30d99q44df2a9188a917f0@mail.gmail.com> On Wed, Jul 29, 2009 at 6:10 AM, v1d4l0k4 wrote: > Hi! > > I want to know how I can get different expected return values by > platform.system() method. I want to sniff some systems (Linux, Windows, Mac, > BSD, Solaris) and I don't know how I can check them correctly. Could you > give me some expected return values that you know? On my Mac, I get "Darwin". You should file a bug in the docs after compiling the results, to help future users. Cheers, Chris -- http://blog.rebertia.com From draghuram at gmail.com Wed Jul 29 23:00:57 2009 From: draghuram at gmail.com (Raghuram Devarakonda) Date: Wed, 29 Jul 2009 20:00:57 -0700 (PDT) Subject: Connecting multiple test cases in a sort of pipe Message-ID: Hi, I have three functions - create(), list(), and delete(). create() creates a new name in the file system hierarchy while list() displays all such created names and delete() removes them. I would like to write test cases for each of these functions, say, test_create() , test_list(), and test_delete() using the unittest module. It easily follows that test_list() and test_delete() will need to know the exact name that was created in test_create() in order to properly validate list() and delete(). One option is for test_create() to always use a hard coded name but unfortunately this is not feasible. Another way would be for test_create() to pass the new name down to the next test case test_list() which would do the same and pass the name down to test_delete(). In other words, the requirement is to connect all three test cases in a kind of pipe. This facility is obviously not currently present in the unittest module and nor does it seem to be available in other frameworks (such as py.test and nose, I only skimmed the feature list). I am thinking of implementing this feature on top of unittest.TestCase and would like to know what others think of the idea. Is there a framework that has similar feature? Or perhaps, given my requirement as stated above, is there some alternate way of implementing it? I greatly appreciate any feed back. Thanks, Raghu From rt8396 at gmail.com Wed Jul 29 23:06:33 2009 From: rt8396 at gmail.com (r) Date: Wed, 29 Jul 2009 20:06:33 -0700 (PDT) Subject: Confessions of a Python fanboy Message-ID: Hello fellow Pythonista's it's nice to be back after such a long hiatus. I have recently realized my blind love affair with Python was just that. I must admit there are better ways to program. Like your first lay, your first programing language can leave an indelible mark on you, but like all young chaps, the passion for programming has sweep me away into vast new orgies of programming bliss. (ig ,little head guides the big one.) My adventures in Ruby. an autobyography by rt8396 After breaking free from the wild love affair i had with Python, a new day had dawned. I dried my eyes and soon discovered the Ruby language (not by choice however) --now don't worry fellow Pythonistas, i am still on your side (mostly) -- but i now realize Ruby has some good things going for it. For instance... 1.) No need to use "()" to call a function with no arguments. Python --> "obj.m2().m3()" --ugly Ruby --> "obj.m1.m2.m3" -- sweeet! Man, i must admit i really like this, and your code will look so much cleaner. 2.) the .each method container.each{|localVar| block} This method can really cleanup some ugly for loops, although i really like the readability of for loops. 3.) true OOP Now before you go and get all "huffy" over this statement, hear me out. Python is the best language in the world. But it damn sure has some warts! "len(this)" instead of "obj.length" max(that) instead of [1,2,3,4,5].max(). You know what i am talking about here people. We all get complacent and It seems easier to just cope with these problems instead of fighting for change. But look at the French, WHAT THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! As for the rest of Ruby, i am not impressed. The redundant usage of "end" over indention perplexes me. The Perlish feel of "require" and the horrifically cryptic idioms of Ruby regular expressions. The "puts" and "gets" seem childish and the math class does not even have a degrees or radians function! Anyway, i thought i would get this off my chest and catch up with old friends in the meantime. We can all make Python the perfect language it needs to be, but it ain't gonna be easy! Thank you all PS stay tuned for more from this series.... From rt8396 at gmail.com Wed Jul 29 23:43:58 2009 From: rt8396 at gmail.com (r) Date: Wed, 29 Jul 2009 20:43:58 -0700 (PDT) Subject: IDLE Config Problems References: Message-ID: <37c1f7fa-8008-41b0-b2c4-d9c1e5288e2e@v20g2000yqm.googlegroups.com> On Jul 29, 3:55?pm, "Russ Davis" wrote: > I am just getting started with Python and have installed v. 2.5.4 ?Idle version 1.2.4 ?I can't seem to get the idle to display text. ?It seems as if the install went fine. ?I start up the idle and the screen is blank. ?No text. ?It seems as if I can type on the screen but I just can't see the characters. ? I go to the config menu and it bombs and brings up the Visual Studio debugger. ?The computer that I am trying to install it on is a windows xp laptop serv pack 2. ? I have a workstation here that I am using also with the same os and the idle works fine. ?Any hints as to what might be interfering with the idle config. > just spit balling here, but have you tried a un-install, re-install """ Bad things can happen to good installers """ From darkneter at gmail.com Wed Jul 29 23:52:06 2009 From: darkneter at gmail.com (NighterNet) Date: Wed, 29 Jul 2009 20:52:06 -0700 (PDT) Subject: socket send Message-ID: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> I am trying to figure out how to send text or byte in python 3.1. I am trying to send data to flash socket to get there. I am not sure how to work it. buff= 'id=' , self.id , ':balive=False\n' clientSock.send(buff); From wuwei23 at gmail.com Thu Jul 30 00:04:27 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 29 Jul 2009 21:04:27 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: On Jul 30, 1:06?pm, r wrote: > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. How do you distinguish between calling a method with no arguments, and getting access to the method object itself (because it _is_ an object, y'know, it's OO all the way down...)? > 2.) the .each method > container.each{|localVar| block} > This method can really cleanup some ugly for loops, although i really > like the readability of for loops. map(lambda localVar: , sequence) or: def usefully_named_func(var): return var transformed = [usefully_named_func(v) for v in sequence] > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). As the Zen says: '[P]racticality beats purity'. Personally, I'm not sure how a handful of convenient built-in functions make a language in which _everything is an object_ somehow "false" OO. If you're really that concerned with writing "true" OO (for some wildly variable value of "true"), there's nothing stopping you from doing so now: obj.__len__() With max(), this is a built-in that takes _any_ iterable and an optional key function, and returns the highest value as per the key. This means that _every_ iterable object - as _well_ as every object that supports enough of the iterator protocol - can be handed to max() and a result obtained. So at best, I just need to make sure my new sequence-type provides the iterator protocol and viola, it works with max() without me having to hand-code a .max() that's specialised for my new type, and without Python forcing me to have a complex inheritance chain just to make sure I include the right MaxableSequence ancestor to inherit the right .max(). > PS stay tuned for more from this series.... Is this going to be more of you telling us - without any apparent irony whatsoever - how Ruby has some valid points after all your vilification last year when we said the same to you? If so, where can I sign up?! (You should consider trading guest spot posts with Xah Lee on your respective blogs. You both have a very similar approach to programming & programming languages and I think the synergy would be amazing to see.) From pdlemper at earthlink.net Thu Jul 30 00:24:11 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Wed, 29 Jul 2009 23:24:11 -0500 Subject: gamma approximation : what is module cmath and where is it located ? Message-ID: The following numerical approximation for Euler's Gamma function is found in http://en.wikipedia.org/wiki/Lanczos_approximation from cmath import * # Coefficients used by the GNU Scientific Library g = 7 p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7] def gamma(z): z = complex(z) # Reflection formula if z.real < 0.5: return pi / (sin(pi*z)*gamma(1-z)) else: z -= 1 x = p[0] for i in range(1, g+2): x += p[i]/(z+i) t = z + g + 0.5 return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x This works in Python 3.0 But I can't figure out where it gets "cmath". Searching the Python directory reveals no more than a test_cmath. The only cmath I can find is a header file in another directory turboc++\Borland\include\ dir(cmath) reveals 23 functions overlapping the 37 functions of the math module. What is cmath, where did it come from and how does it differ from the standard math module ? Dave WB3DWE I saw the number 4 in silver, Guido (apologies to Wm Carlos Williams) From clp2 at rebertia.com Thu Jul 30 00:27:20 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 21:27:20 -0700 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <50697b2c0907292127l98ff61aief7f7205b5b5ca9@mail.gmail.com> On Wed, Jul 29, 2009 at 9:04 PM, alex23 wrote: > On Jul 30, 1:06?pm, r wrote: >> 1.) No need to use "()" to call a function with no arguments. >> Python --> "obj.m2().m3()" --ugly >> ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! >> Man, i must admit i really like this, and your code will look so much >> cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? IIRC from the Pickaxe, to get at a method object, you call a method on the object whose specific job it is to return method objects. Unsurprisingly, this method is itself named "method". So: foo.bar #calls the "bar" method on foo foo.method(:bar) #returns the method "bar" of foo, as an object Cheers, Chris -- http://blog.rebertia.com From clp2 at rebertia.com Thu Jul 30 00:34:07 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 21:34:07 -0700 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: <50697b2c0907292134p594105fag2bf99e0b5e14e35c@mail.gmail.com> On Wed, Jul 29, 2009 at 9:24 PM, wrote: > But I can't figure out where it gets "cmath". ? ?Searching the Python > directory reveals no more than a test_cmath. ?The only cmath I can find > is a header file in another directory ?turboc++\Borland\include\ > > dir(cmath) reveals 23 functions overlapping the 37 functions of > the math module. > > What is cmath, where did it come from and how does it differ from > the standard math module ?? It's a module in the standard library: http://docs.python.org/library/cmath.html The difference is that it handles complex numbers, whereas the plain "math" module doesn't. I would guess the reason there are separate modules is for performance, so as to avoid having to dispatch on type at runtime. But this is only a guess. Cheers, Chris -- http://blog.rebertia.com From pavlovevidence at gmail.com Thu Jul 30 00:36:31 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 29 Jul 2009 21:36:31 -0700 (PDT) Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: On Jul 29, 9:24?pm, pdlem... at earthlink.net wrote: > What is cmath, where did it come from and how does it differ from > the standard math module ?? What happened when you did a keyword search for "cmath" in the Python library documentation? What happened when you entered "python cmath" in Google? Carl Banks From pavlovevidence at gmail.com Thu Jul 30 00:43:47 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 29 Jul 2009 21:43:47 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: On Jul 29, 9:04?pm, alex23 wrote: > On Jul 30, 1:06?pm, r wrote: > > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? > > > 2.) the .each method > > container.each{|localVar| block} > > This method can really cleanup some ugly for loops, although i really > > like the readability of for loops. > > map(lambda localVar: , sequence) > > or: > > def usefully_named_func(var): > ? ? > ? ? return var > > transformed = [usefully_named_func(v) for v in sequence] > > > 3.) true OOP > > Now before you go and get all "huffy" over this statement, hear me > > out. Python is the best language in the world. But it damn sure has > > some warts! "len(this)" instead of "obj.length" max(that) instead of > > [1,2,3,4,5].max(). > > As the Zen says: '[P]racticality beats purity'. Personally, I'm not > sure how a handful of convenient built-in functions make a language in > which _everything is an object_ somehow "false" OO. > > If you're really that concerned with writing "true" OO (for some > wildly variable value of "true"), there's nothing stopping you from > doing so now: > > ? ? obj.__len__() > > With max(), this is a built-in that takes _any_ iterable and an > optional key function, and returns the highest value as per the key. > This means that _every_ iterable object - as _well_ as every object > that supports enough of the iterator protocol - can be handed to max() > and a result obtained. So at best, I just need to make sure my new > sequence-type provides the iterator protocol and viola, it works with > max() without me having to hand-code a .max() that's specialised for > my new type, and without Python forcing me to have a complex > inheritance chain just to make sure I include the right > MaxableSequence ancestor to inherit the right .max(). > > > PS stay tuned for more from this series.... > > Is this going to be more of you telling us - without any apparent > irony whatsoever - how Ruby has some valid points after all your > vilification last year when we said the same to you? ?If so, where can > I sign up?! > > (You should consider trading guest spot posts with Xah Lee on your > respective blogs. You both have a very similar approach to programming > & programming languages and I think the synergy would be amazing to > see.) Here is some wisdom from Guido van Rossum on the matter: http://mail.python.org/pipermail/python-dev/2006-May/064841.html Carl Banks From pdlemper at earthlink.net Thu Jul 30 01:25:11 2009 From: pdlemper at earthlink.net (pdlemper at earthlink.net) Date: Thu, 30 Jul 2009 00:25:11 -0500 Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> On Wed, 29 Jul 2009 21:36:31 -0700 (PDT), Carl Banks wrote: >On Jul 29, 9:24?pm, pdlem... at earthlink.net wrote: >> What is cmath, where did it come from and how does it differ from >> the standard math module ?? > >What happened when you did a keyword search for "cmath" in the Python >library documentation? > >What happened when you entered "python cmath" in Google? > > >Carl Banks Thanks Carl & Chris. I've read the Python doc 10.3 and now understand its for complex arithmetic. I've used python a few months and expected to find cmath seperately sort of as a " header file". A text search failed. I now understand its in the Python Standard Library, which I assume is that big file Python30\LIBS\libpython30.a And this is not readable as text. Dave From wuwei23 at gmail.com Thu Jul 30 01:28:06 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 29 Jul 2009 22:28:06 -0700 (PDT) Subject: IDLE Config Problems References: Message-ID: <89553ecc-a30e-4280-8a42-17a18af50ba9@v37g2000prg.googlegroups.com> On Jul 30, 6:55?am, "Russ Davis" wrote: > I am just getting started with Python and have installed v. 2.5.4 ? > Idle version 1.2.4 ?I can't seem to get the idle to display text. ?It > seems as if the install went fine. ?I start up the idle and the screen > is blank. ?No text. ?It seems as if I can type on the screen but I just > can't see the characters. Is this the official python.org distribution you're trying? I can't speak from experience, but I'm wondering if it doesn't include the pyreadline module by default, which you'll require under Windows (although I would've expected to hear more problems like your's if this is the case). You can easily check this by going to C: \Python25\Lib\site-packages and looking for a folder or a file called pyreadline. If it's not, you'll have to install it separately. If you're not already familiar with it, I do recommend checking out the ActiveState ActivePython distribution instead, especially if you're working under Windows. It includes a lot of Windows-specific 3rd party modules, such as the win32all modules, and it definitely includes pyreadline. Is there any particular reason why you're opting for Python 2.5.x? 2.6 should be pretty compatible with most things in the 2.x stream and it's a handy stepping stone towards using 3.x (which, admittedly, is probably the last of your concerns when you're first setting up...) From tjreedy at udel.edu Thu Jul 30 01:35:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 01:35:19 -0400 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: pdlemper at earthlink.net wrote: > The following numerical approximation for Euler's Gamma function > is found in http://en.wikipedia.org/wiki/Lanczos_approximation > > from cmath import * > But I can't figure out where it gets "cmath". The Python documentation set has a module index. Really handy. I use it all the time. cmath is under 'c'. To answer some of your question: >>> math.sqrt(-1) Traceback (most recent call last): File "", line 1, in math.sqrt(-1) ValueError: math domain error >>> cmath.sqrt(-1) 1j Which to use depends on the behavior you want. tjr From Ron.Barak at lsi.com Thu Jul 30 02:01:28 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 07:01:28 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <4A708F68.1010505@ieee.org> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> > -----Original Message----- > From: Dave Angel [mailto:davea at ieee.org] > Sent: Wednesday, July 29, 2009 21:05 > To: Barak, Ron > Cc: 'python-list at python.org' > Subject: Re: Run pyc file without specifying python path ? > > Barak, Ron wrote: > > Hi, > > > > I wanted to make a python byte-code file executable, > expecting to be able to run it without specifying "python" on > the (Linux bash) command line. > > > > So, I wrote the following: > > > > [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > > > > print "hello" > > [root at VMLinux1 python]# > > > > and made its pyc file executable: > > > > [root at VMLinux1 python]# ls -ls test_pyc.pyc > > 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > > [root at VMLinux1 python]# > > > > So, I see: > > > > [root at VMLinux1 python]# file test_pyc.py* > > test_pyc.py: a python script text executable > > test_pyc.pyc: python 2.3 byte-compiled > > [root at VMLinux1 python]# > > > > If I try to do the following, no problem: > > > > [root at VMLinux1 python]# python test_pyc.pyc hello > > [root at VMLinux1 python]# > > > > However, the following fails: > > > > [root at VMLinux1 python]# ./test_pyc.pyc > > -bash: ./test_pyc.pyc: cannot execute binary file > > [root at VMLinux1 python]# > > > > Is there a way to run a pyc file without specifying the > python path ? > > > > Bye, > > Ron. > > > > > I don't currently run Unix, but I think I know the problem. > > In a text file, the shell examines the first line, and if it > begins #! > it's assumed to point to the executable of an interpreter for > that text file. Presumably the same trick doesn't work for a > .pyc file. > > Why not write a trivial wrapper.py file, don't compile it, > and let that invoke the main code in the .pyc file? > > Then make wrapper.py executable, and you're ready to go. > > DaveA > > Hi Dave, Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. Bye, Ron. From deets at nospam.web.de Thu Jul 30 02:33:54 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 08:33:54 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: References: <7dbs8nF2br07nU1@mid.uni-berlin.de> Message-ID: <7dct6iF2bhhshU1@mid.uni-berlin.de> Nick Craig-Wood schrieb: > Diez B. Roggisch wrote: >> MalC0de schrieb: >>> hello there, I've a question : >>> I want to know does python have any capability for using Ring0 and >>> kernel functions for driver and device development stuff . >>> if there's such a feature it is very good, and if there something for >>> this kind that you know please refer me to some reference and show me >>> some snippet . >> No, it can't do such things. At least it isn't embedded in the kernel - >> in theory that might be possible, but given the timing-constraints and >> concurrency-requirements, it's not really feasible. > > You can write FUSE (file systems in userspace) drivers in python I believe. > Not the same as running in ring0 but in most senses a kernel driver... No. That's why it is called "userspace". The kernel just hooks into a running program. Diez From __peter__ at web.de Thu Jul 30 02:41:00 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 30 Jul 2009 08:41 +0200 Subject: gamma approximation : what is module cmath and where is it located ? References: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> Message-ID: pdlemper at earthlink.net wrote: > I've used python a few months and expected to find cmath seperately > sort of as a " header file". A text search failed. I now understand > its in the Python Standard Library, which I assume is that big file > Python30\LIBS\libpython30.a Python 3.1 is no longer maintained. I recommend that you upgrade to Python 3.1. > And this is not readable as text. The module is written in C. The source code for the 3.1 version is here: http://svn.python.org/view/python/branches/release31-maint/Modules/cmathmodule.c?view=markup If you want the source for all parts of python written in C you can download the sourcecode at http://www.python.org/download/releases/3.1/ Peter From deets at nospam.web.de Thu Jul 30 02:43:27 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 08:43:27 +0200 Subject: Connecting multiple test cases in a sort of pipe In-Reply-To: References: Message-ID: <7dctogF29q4c6U1@mid.uni-berlin.de> Raghuram Devarakonda schrieb: > Hi, > > I have three functions - create(), list(), and delete(). create() > creates a new name in the file system hierarchy while list() displays > all such created names and delete() removes them. I would like to > write test cases for each of these functions, say, test_create() , > test_list(), and test_delete() using the unittest module. It easily > follows that test_list() and test_delete() will need to know the exact > name that was created in test_create() in order to properly validate > list() and delete(). One option is for test_create() to always use a > hard coded name but unfortunately this is not feasible. > > Another way would be for test_create() to pass the new name down to > the next test case test_list() which would do the same and pass the > name down to test_delete(). In other words, the requirement is to > connect all three test cases in a kind of pipe. This facility is > obviously not currently present in the unittest module and nor does it > seem to be available in other frameworks (such as py.test and nose, I > only skimmed the feature list). > > I am thinking of implementing this feature on top of unittest.TestCase > and would like to know what others think of the idea. Is there a > framework that has similar feature? Or perhaps, given my requirement > as stated above, is there some alternate way of implementing it? I > greatly appreciate any feed back. Write a TestCase-subclass that simply works like this: class FileTests(TestCase): def test_create(self): self.created_file = create() assert something def test_list(self): self.test_create() delete(self.created_file) Passing state around is one of the reasons objects have been invented :) And nobody ever said that you can't invoke a test-method from somewhere else. Diez From clp2 at rebertia.com Thu Jul 30 02:53:36 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 29 Jul 2009 23:53:36 -0700 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> Message-ID: <50697b2c0907292353i479f5d70ke59a91c330b6d8f7@mail.gmail.com> On Wed, Jul 29, 2009 at 11:41 PM, Peter Otten<__peter__ at web.de> wrote: > pdlemper at earthlink.net wrote: > >> I've used python a few months and expected to find cmath seperately >> sort of as a " header file". ?A text search failed. ?I now understand >> its in the Python Standard Library, which I assume is that big file >> Python30\LIBS\libpython30.a > > Python 3.1 is no longer maintained. I recommend that you upgrade to Python 3.1. (He meant to say 3.0.x is no longer maintained.) - Chris -- http://blog.rebertia.com From __peter__ at web.de Thu Jul 30 03:06:31 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 30 Jul 2009 09:06:31 +0200 Subject: gamma approximation : what is module cmath and where is it located ? References: <72b2751isp5ro2hmq5ordm2nhf3ohjrtjk@4ax.com> Message-ID: Chris Rebert wrote: > On Wed, Jul 29, 2009 at 11:41 PM, Peter Otten<__peter__ at web.de> wrote: >> pdlemper at earthlink.net wrote: >> >>> I've used python a few months and expected to find cmath seperately >>> sort of as a " header file". A text search failed. I now understand >>> its in the Python Standard Library, which I assume is that big file >>> Python30\LIBS\libpython30.a >> >> Python 3.1 is no longer maintained. I recommend that you upgrade to >> Python 3.1. > > (He meant to say 3.0.x is no longer maintained.) Ouch. Thanks for the correction. From xubin.cz at gmail.com Thu Jul 30 03:12:33 2009 From: xubin.cz at gmail.com (xubin.cz) Date: Thu, 30 Jul 2009 00:12:33 -0700 (PDT) Subject: beautiful soup Message-ID: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> hi, everyone Is there any pakage or module handling html document like beautiful soup? thanks a lot From deets at nospam.web.de Thu Jul 30 03:30:48 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 09:30:48 +0200 Subject: beautiful soup In-Reply-To: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> References: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> Message-ID: <7dd0h8F2a2he0U1@mid.uni-berlin.de> xubin.cz schrieb: > hi, everyone > > Is there any pakage or module handling html document like beautiful > soup? why don't you *use* beautiful soup? It is a module... Diez From enleverLesX_XXmcX at XmclavXeauX.com Thu Jul 30 03:54:21 2009 From: enleverLesX_XXmcX at XmclavXeauX.com (Michel Claveau - MVP) Date: Thu, 30 Jul 2009 09:54:21 +0200 Subject: Does python have the capability for driver development ? References: Message-ID: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Hi! > Python is interpreted No. Python is compiled (--> .pyc) But the term "to compile" is not always unambiguous... And the notion of "compiler" is not attached to Python (the language), but is attached to the implementation. @+ MCI From massi_srb at msn.com Thu Jul 30 03:54:31 2009 From: massi_srb at msn.com (Massi) Date: Thu, 30 Jul 2009 00:54:31 -0700 (PDT) Subject: Bug in IEHtmlWindow? Message-ID: <8a5ed753-d437-45a6-8fee-300eea1207ca@c2g2000yqi.googlegroups.com> Hi everyone, I'm trying to use IEHtmlWindow in my application (python 2.5, wxpython 2.8 on win xp). Everything is ok, but I encountered a problem which also affects the demo. If I make a research on google a strange message appears, saying (I'll try to translate from italian): Error during execution. Do you want to run the debug? line 29: error: 'r' is NULL or it is not an object Did anybody have the same problem? is it a bug? Thanks in advance for your help. From masklinn at masklinn.net Thu Jul 30 03:55:24 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 09:55:24 +0200 Subject: beautiful soup In-Reply-To: <7dd0h8F2a2he0U1@mid.uni-berlin.de> References: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> <7dd0h8F2a2he0U1@mid.uni-berlin.de> Message-ID: On 30 Jul 2009, at 09:30 , Diez B. Roggisch wrote: > xubin.cz schrieb: >> hi, everyone >> Is there any pakage or module handling html document like beautiful >> soup? > > why don't you *use* beautiful soup? It is a module... Or lxml, which works a bit better than BF 3.1 (post parser change) nowadays. From martin.hellwig at dcuktec.org Thu Jul 30 04:03:26 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 09:03:26 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: References: <87skgf83ua.fsf@rsw.digi.com.br> Message-ID: Marcus Wanner wrote: >> Look for example to libusb, which provides a userspace environment and >> pyusb which uses that and provides an interface to Python. >> > iicr pyusb uses a c interface to libusb, not python... > According to them they use ctypes indeed. Sorry if I was misleading in my explanation. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From martin.hellwig at dcuktec.org Thu Jul 30 04:05:48 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 09:05:48 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <4a7151b2$0$17782$ba4acef3@news.orange.fr> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: Michel Claveau - MVP wrote: > Hi! > >> Python is interpreted > > No. Python is compiled (--> .pyc) > But the term "to compile" is not always unambiguous... > And the notion of "compiler" is not attached to Python (the language), but is attached to the implementation. > > @+ > > MCI Well the pyc, which I thought was the Python bytecode, is then interpreted by the VM. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From javier.collado at gmail.com Thu Jul 30 04:17:24 2009 From: javier.collado at gmail.com (Javier Collado) Date: Thu, 30 Jul 2009 10:17:24 +0200 Subject: string.Template issue Message-ID: Hello, In the string.Template documentation (http://docs.python.org/library/string.html) it's explained that if a custom regular expression for pattern substitution is needed, it's possible to override idpattern class attribute (whose default value is [_a-z][_a-z0-9]*). However, if the custom pattern that is needed is just uppercase letters something like [A-Z]+ won't work because of the following line in the _TemplateMetaclass class __init__ method: cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE) I would say that this is an error (IGNORECASE just shouldn't be there) and that the line above should be: cls.pattern = _re.compile(pattern, _re.VERBOSE) and the default value for idpattern: [_a-zA-Z][_a-zA-Z0-9]* Do you agree on this? Is there any reason for the IGNORECASE option to be passed to re.compile? Best regards, Javier From kunguz at gmail.com Thu Jul 30 05:02:04 2009 From: kunguz at gmail.com (=?ISO-8859-9?Q?Kaan_AK=DE=DDT?=) Date: Thu, 30 Jul 2009 11:02:04 +0200 Subject: Pyplot Message-ID: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> I am a newbee in Python. I have some problem with usage of Pyplot. I have a loop that creates plot in every end of it. Problem starts here: The first plot is fine but the second one is plotted onto the first plot. I use: plt.cla() plt.draw() plt.hold(False) plt.close() plt.clf() but nothing works. Perhaps I am having a problem with basics, please help me if you know something about it. Kaan -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Thu Jul 30 05:10:32 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 30 Jul 2009 11:10:32 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: Martin P. Hellwig wrote: > Well the pyc, which I thought was the Python bytecode, is then > interpreted by the VM. Python is often referred as byte-code interpreted language. Most modern languages are interpreted languages. The list [1] is rather long. Technically speaking even native code is interpreted by the micro code of most CPUs. [1] http://en.wikipedia.org/wiki/Interpreted_language [2] http://en.wikipedia.org/wiki/Microcode From ben+python at benfinney.id.au Thu Jul 30 05:14:54 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 30 Jul 2009 19:14:54 +1000 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <878wi6v8fl.fsf@benfinney.id.au> "Michel Claveau - MVP" writes: > Hi! > > > Python is interpreted > > No. Yes. The same Python code is both interpreted and compiled so as to run it. > Python is compiled (--> .pyc) The Python bytecode (the contents of the compiled ?foo.pyc? file) is then interpreted by the run-time Python interpreter, to actually run the program. -- \ ?Value your freedom or you will lose it, teaches history. | `\ ?Don't bother us with politics,? respond those who don't want | _o__) to learn.? ?Richard Stallman, 2002 | Ben Finney From python at rgbaz.eu Thu Jul 30 05:17:51 2009 From: python at rgbaz.eu (PythonAB) Date: Thu, 30 Jul 2009 11:17:51 +0200 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> >> > Hi Dave, > Your solution sort of defeats my intended purpose (sorry for not > divulging my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python > script, and look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want > a cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) > version of the application, because it'll grow the resulting file > significantly, and I don't have the experience to know how it will > run on different Linuxes. > Bye, > Ron. Hey Ron, What i usually do to accomplish this is compile the script to a .pyc just like you did and then call that pyc from another script that's not compiled. so in your case the not compiled script looks like: #!/usr/bin/env python import test_pyc.pyc then run that script... hope this helps... alternatively you might have a look at: http://www.pyinstaller.org/ gr Arno -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Thu Jul 30 05:27:51 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 30 Jul 2009 11:27:51 +0200 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: Barak, Ron wrote: > Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. Ask Google for binfmt-support. Hint: Each Python version has its own magic header. Christian From kunguz at gmail.com Thu Jul 30 05:29:28 2009 From: kunguz at gmail.com (=?ISO-8859-9?Q?Kaan_AK=DE=DDT?=) Date: Thu, 30 Jul 2009 11:29:28 +0200 Subject: Pyplot In-Reply-To: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> References: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> Message-ID: <8fa7fe790907300229i496f7886w3c92676e51e0d9fa@mail.gmail.com> Please help me I have been dealing with this little piece of code for 2 days. I am stuck and loosing time with it. plt.figure(count) plt.xlabel("Time steps") plt.ylabel("Values") plt.title("Output of ADCs") plt.plot(timing,adc0) plt.plot(timing,adc1) plt.plot(timing,adc3) plt.show() plt.clf() plt.cla() plt.close() 30 Temmuz 2009 11:02 tarihinde Kaan AK??T yazd?: > I am a newbee in Python. I have some problem with usage of Pyplot. I have a > loop that creates plot in every end of it. Problem starts here: The first > plot is fine but the second one is plotted onto the first plot. I use: > > plt.cla() > plt.draw() > plt.hold(False) > plt.close() > plt.clf() > > but nothing works. Perhaps I am having a problem with basics, please help > me if you know something about it. > > Kaan > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Thu Jul 30 05:53:03 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 30 Jul 2009 11:53:03 +0200 Subject: Can you easy_install from your localhost? In-Reply-To: <9c8c445f0907290857x27930acbvc5067a242e18ed17@mail.gmail.com> References: <9c8c445f0907290857x27930acbvc5067a242e18ed17@mail.gmail.com> Message-ID: Ronn Ross schrieb: > I have a package i would like to store locally. If it is stored locally can > I do something like ' easy_install http://localhost/package ' ? Thanks You can do: easy_install /path/to/package-0.1.tar.gz or you can put your packages into /some/directory/simple/packagename/package-0.1.tar.gz and do: easy_install -i file:///some/directory packagename Christian From steven at REMOVE.THIS.cybersource.com.au Thu Jul 30 06:10:36 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 30 Jul 2009 10:10:36 GMT Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: On Wed, 29 Jul 2009 21:34:07 -0700, Chris Rebert wrote: > The difference is that it handles complex numbers, whereas the plain > "math" module doesn't. > I would guess the reason there are separate modules is for performance, > so as to avoid having to dispatch on type at runtime. But this is only a > guess. My understanding is that it is to avoid people who don't care about complex numbers being confused when math.sqrt(-1) returns 1j instead of raising an error. -- Steven From egcalso at gmail.com Thu Jul 30 06:13:52 2009 From: egcalso at gmail.com (eman) Date: Thu, 30 Jul 2009 03:13:52 -0700 (PDT) Subject: Help on Reading Attached Image on Excel Message-ID: <7a651be8-e653-448e-91a2-84af71c5f64d@p36g2000prn.googlegroups.com> Anyone know how to read included images in Excel using Python? I've tried xlrd and pyexcelerator, but i couldn't figure out how to do it. If anyone has experience in this, please help. Thanks. From steven at REMOVE.THIS.cybersource.com.au Thu Jul 30 06:20:24 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 30 Jul 2009 10:20:24 GMT Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> Message-ID: On Thu, 30 Jul 2009 07:01:28 +0100, Barak, Ron wrote: > I wanted my application to "hide" the fact that it's a python script, > and look as much as possible like it's a compiled program. What do you think the "c" in .pyc stands for? What it's not is a compiled to machine-code stand alone executable. > The reason I don't just give my user a py file, is that I don't want > a cleaver user to change the innards of the script. It's not the clever users you need to worry about, because they'll fix your bugs. It's the not-as-clever-as-they-think-they-are users you have to worry about. But frankly, the best way to deal with them is to make it absolutely clear that you don't provide free support for modified versions of the script, and as soon as they report a problem, you diff their version with the official version. If they vary, then you start charging them. Before you go spending vast amounts of time and energy creating a fragile solution, it's worth you working out what it actually is you're trying to prevent. If it's just "I don't want anybody touching my precious, precious code!" then I have no sympathy and have no interest in helping you. If it's "The damn users have mucked up the script AGAIN and now they expect me to fix it for free!!!", then that's a social problem, not a technical one, and a social solution might save you a lot of wasted effort. -- Steven From clp2 at rebertia.com Thu Jul 30 06:20:41 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 30 Jul 2009 03:20:41 -0700 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: <50697b2c0907300320s35658775tfacc182f16ce9711@mail.gmail.com> On Thu, Jul 30, 2009 at 3:10 AM, Steven D'Aprano wrote: > On Wed, 29 Jul 2009 21:34:07 -0700, Chris Rebert wrote: > >> The difference is that it handles complex numbers, whereas the plain >> "math" module doesn't. >> I would guess the reason there are separate modules is for performance, >> so as to avoid having to dispatch on type at runtime. But this is only a >> guess. > > My understanding is that it is to avoid people who don't care about > complex numbers being confused when math.sqrt(-1) returns 1j instead of > raising an error. That seems a better and more plausible rationale than my guess. Kudos. - Chris -- http://blog.rebertia.com From bieffe62 at gmail.com Thu Jul 30 06:29:24 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 03:29:24 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> On Jul 30, 5:52?am, NighterNet wrote: > I am trying to figure out how to send text or byte in python 3.1. I am > trying to send data to flash socket to get there. I am not sure how to > work it. > > buff= 'id=' , self.id , ':balive=False\n' > clientSock.send(buff); Try putting a 'b' before the constant string that you want to send: >>> type(b'123') or use something like this to convert non constant strings (with only ASCII characters) into bytes: >>> s = "A non-constant string : %d " % n >>> s 'A non-constant string : 12 ' >>> type(s) >>> b = bytes ( ord(c) for c in s ) >>> b b'A non-constant string : 12 ' You could also use struct.pack , that in python 3.x returns bytes and not strings. In this case you need to specify the size of the string (extra bytes are zero-filled): import struct >>> struct.pack( "30s", "A non-constant string : %d " % n ) b'A non-constant string : 12 \x00\x00\x00' Ciao ----- FB From marco at sferacarta.com Thu Jul 30 06:29:50 2009 From: marco at sferacarta.com (Marco Mariani) Date: Thu, 30 Jul 2009 12:29:50 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: r wrote: > My adventures in Ruby. Oh, it's you. Good boy. Now, why don't you have a look at javascript and come back in six months? Or better yet, haskell and twelve months. thanks From wolfgang at rohdewald.de Thu Jul 30 06:35:22 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 12:35:22 +0200 Subject: New implementation of re module In-Reply-To: References: <4A6DD6FB.8040609@mrabarnett.plus.com> Message-ID: <200907301235.22563.wolfgang@rohdewald.de> On Tuesday 28 July 2009, Christopher Arndt wrote: > setup(name='regex', > version='1.0', > py_modules = ['regex'], > ext_modules=[Extension('_regex', ['_regex.c'])], > ) > > Also, you need to copy "unicodedata_db.h" from the "Modules" > directory of the Python source tree to your working directory, > since this file apparently is not installed into the include > directory of a Python installation. using issue2636-20090729.zip I have Python 2.6.2 on ubuntu 9.04 with ggc-4.3: gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o build/temp.linux-i686-2.6/_regex.o _regex.c: In Funktion ?bmatch_context?: _regex.c:1462: Fehler: Als Erh?hungsoperand wird L-Wert erfordert _regex.c:1470: Fehler: Als Erh?hungsoperand wird L-Wert erfordert _regex.c:1478: Fehler: Als Verringerungsoperand wird L-Wert erfordert with gcc-4.4: gcc-4.4 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict- prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o build/temp.linux-i686-2.6/_regex.o _regex.c: In function ?bmatch_context?: _regex.c:1462: error: lvalue required as increment operand _regex.c:1470: error: lvalue required as increment operand _regex.c:1478: error: lvalue required as decrement operand -- Wolfgang From Ron.Barak at lsi.com Thu Jul 30 06:42:52 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 11:42:52 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> ________________________________ From: PythonAB [mailto:python at rgbaz.eu] Sent: Thursday, July 30, 2009 12:18 To: Barak, Ron Cc: 'Dave Angel'; 'python-list at python.org' Subject: Re: Run pyc file without specifying python path ? Hi Dave, Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. Bye, Ron. Hey Ron, What i usually do to accomplish this is compile the script to a .pyc just like you did and then call that pyc from another script that's not compiled. so in your case the not compiled script looks like: #!/usr/bin/env python import test_pyc.pyc then run that script... hope this helps... alternatively you might have a look at: http://www.pyinstaller.org/ gr Arno [BR] Thanks for the answer Arno. But, I wonder - How is your suggestion, which to the user would look like: python wrapper.py different (from the user's point-of-view) than the following: python test_pyc.pyc ? I'm trying to avoide having to write python on the command line. Of course, I could write the wrapper is bourne shell, but that interoduces other problems (especially when considering that my real application needs to parse many command line options). . Bye, Ron. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bieffe62 at gmail.com Thu Jul 30 06:49:36 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 03:49:36 -0700 (PDT) Subject: Python processors? : WAS Re: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <2d8298b9-b857-4b3d-ae63-2056d80cdf7f@d4g2000yqa.googlegroups.com> On Jul 30, 11:10?am, Christian Heimes wrote: > Martin P. Hellwig wrote: > > Well the pyc, which I thought was the Python bytecode, is then > > interpreted by the VM. > > Python is often referred as byte-code interpreted language. Most modern > languages are interpreted languages. The list [1] is rather long. > Technically speaking even native code is interpreted by the micro code > of most CPUs. > > [1]http://en.wikipedia.org/wiki/Interpreted_language > [2]http://en.wikipedia.org/wiki/Microcode Once upon a time there where lisp machines, whith processors designed to fastly execute lisp code ... I worked with one of them for 2 years. I wonder: has anybody thought of making a python-machine, or at least a processor able to directly execute high-level bytecode (LLVM-like?). I think the main feature of such a machine should be hyper-fast hash lookup. Then dynamic memory management hardware ... Ciao ----- FB From clp2 at rebertia.com Thu Jul 30 06:57:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 30 Jul 2009 03:57:40 -0700 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> Message-ID: <50697b2c0907300357ld78846ch257547666a453829@mail.gmail.com> On Thu, Jul 30, 2009 at 3:42 AM, Barak, Ron wrote: > > > ________________________________ > From: PythonAB [mailto:python at rgbaz.eu] > Sent: Thursday, July 30, 2009 12:18 > To: Barak, Ron > Cc: 'Dave Angel'; 'python-list at python.org' > Subject: Re: Run pyc file without specifying python path ? > > > Hi Dave, > Your solution sort of defeats my intended purpose (sorry for not divulging > my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python script, and > look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want a > cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) version of the > application, because it'll grow the resulting file significantly, and I > don't have the experience to know how it will run on different Linuxes. > Bye, > Ron. > > Hey Ron, > What i usually do to accomplish this is compile the script to a .pyc just > like you > did and then call that pyc from another script that's not compiled. > so in your case the not compiled script looks like: > #!/usr/bin/env python > import test_pyc.pyc > > > then run that script... > hope this helps... > > > alternatively you might have a look at: > http://www.pyinstaller.org/ > > > gr > Arno > [BR]?Thanks for the answer Arno. But, I wonder?- > > How is your suggestion, which to the user would look like: > python wrapper.py No, with the shebang line (and assuming execute permissions on the file), it would look like: ./wrapper.py (or just `wrapper.py` depending on whether the file is placed in the $PATH) Cheers, Chris -- http://blog.rebertia.com From python at mrabarnett.plus.com Thu Jul 30 07:05:58 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 12:05:58 +0100 Subject: New implementation of re module In-Reply-To: <200907301235.22563.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> Message-ID: <4A717E96.3020900@mrabarnett.plus.com> Wolfgang Rohdewald wrote: > On Tuesday 28 July 2009, Christopher Arndt wrote: >> setup(name='regex', >> version='1.0', >> py_modules = ['regex'], >> ext_modules=[Extension('_regex', ['_regex.c'])], >> ) >> >> Also, you need to copy "unicodedata_db.h" from the "Modules" >> directory of the Python source tree to your working directory, >> since this file apparently is not installed into the include >> directory of a Python installation. > > using issue2636-20090729.zip > > I have Python 2.6.2 on ubuntu 9.04 > > with ggc-4.3: > gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - > Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o > build/temp.linux-i686-2.6/_regex.o > _regex.c: In Funktion ?bmatch_context?: > _regex.c:1462: Fehler: Als Erh?hungsoperand wird L-Wert erfordert > _regex.c:1470: Fehler: Als Erh?hungsoperand wird L-Wert erfordert > _regex.c:1478: Fehler: Als Verringerungsoperand wird L-Wert erfordert > > with gcc-4.4: > gcc-4.4 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict- > prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o > build/temp.linux-i686-2.6/_regex.o > _regex.c: In function ?bmatch_context?: > _regex.c:1462: error: lvalue required as increment operand > _regex.c:1470: error: lvalue required as increment operand > _regex.c:1478: error: lvalue required as decrement operand > There are other lines which are similar, eg line 1487. Do they all give the same/similar error with your compiler? From masklinn at masklinn.net Thu Jul 30 07:36:49 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 13:36:49 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> On 30 Jul 2009, at 06:04 , alex23 wrote: > On Jul 30, 1:06 pm, r wrote: >> 1.) No need to use "()" to call a function with no arguments. >> Python --> "obj.m2().m3()" --ugly >> Ruby --> "obj.m1.m2.m3" -- sweeet! >> Man, i must admit i really like this, and your code will look so much >> cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? > As chris pointed out, there's a method for that, to which a symbol is provided: `Object#method(methodname)`, which is basically equivalent to `getattr(object, methodname)` in Python. >> 2.) the .each method >> container.each{|localVar| block} >> This method can really cleanup some ugly for loops, although i really >> like the readability of for loops. > > map(lambda localVar: , sequence) > > or: > > def usefully_named_func(var): > > return var > > transformed = [usefully_named_func(v) for v in sequence] > The issue here is of course that `map` and comprehensions are transformations. `#each` exists for effectful iterations (Ruby has `#map` for the map operation). So the intent expressed by `#each` and `map` isn't the same. Furthermore and this is the most problematic limitation of Python here, `lambda` doesn't allow complex transformations due to its restrictions, so one has to switch to named functions which works but isn't sexy (and tends to lower readability imo). >> 3.) true OOP >> Now before you go and get all "huffy" over this statement, hear me >> out. Python is the best language in the world. But it damn sure has >> some warts! "len(this)" instead of "obj.length" max(that) instead of >> [1,2,3,4,5].max(). > > As the Zen says: '[P]racticality beats purity'. Personally, I'm not > sure how a handful of convenient built-in functions make a language in > which _everything is an object_ somehow "false" OO. > That's an interesting point, but not relevant at the end of the day: `foo.length` and `length(foo)` have the same "practicality". On the other hand Ruby can be praised for the coherence: everything's a method period end of the story; while Python does have a dichotomy between methods and functions/generic methods (whether or not that dichotomy bothers users is another issue). > With max(), this is a built-in that takes _any_ iterable and an > optional key function, and returns the highest value as per the key. > This means that _every_ iterable object - as _well_ as every object > that supports enough of the iterator protocol - can be handed to max() > and a result obtained. So at best, I just need to make sure my new > sequence-type provides the iterator protocol and viola, it works with > max() without me having to hand-code a .max() that's specialised for > my new type, and without Python forcing me to have a complex > inheritance chain just to make sure I include the right > MaxableSequence ancestor to inherit the right .max(). > Well interestingly, Ruby works pretty much the same way. Where Python asks that you implement the iterator protocol to have `max` work, Ruby asks that you implement the `each` method (also called the Enumerable protocol) and mixin `Enumerable` (http://ruby-doc.org/core/classes/Enumerable.html ). Then you get max (and min, map, etc?) "for free". Of course you're free to re-implement all of them manually if you wish to do so (or if you need those operations to return your custom collection rather than arrays). Enumerable is merely a convenience mixin. So there's no big difference here (and note that mixins aren't necessarily part of the inheritance chain, though they're implemented that way in Ruby). Also, it's "voil?" not "viola" (a viola is a bowed string instrument, slightly bigger than a violin, whereas voil? is a french interjection used to call attention or suggest the appearance of something, as if by magic). >> PS stay tuned for more from this series.... > > Is this going to be more of you telling us - without any apparent > irony whatsoever - how Ruby has some valid points after all your > vilification last year when we said the same to you? If so, where can > I sign up?! I fear I'll have to agree with that sentiment. From bruno.42.desthuilliers at websiteburo.invalid Thu Jul 30 07:40:05 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Thu, 30 Jul 2009 13:40:05 +0200 Subject: Does underscore has any special built-in meaningin Python ? In-Reply-To: <87ljm72h04.fsf@benfinney.id.au> References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> <87ljm72h04.fsf@benfinney.id.au> Message-ID: <4a718695$0$2760$426a34cc@news.free.fr> Ben Finney a ?crit : > dandi kain writes: > >> What is the functionality of __ or _ , leading or trailing an object , >> class ot function ? Please note that in Python, classes and functions are objects too. But anyway, these underscores are part of *identifiers*, not objects themselves !-) > foo Ordinary name, part of public interface > _foo Ordinary name, part of internal-only interface > __foo Ordinary name, but will be mangled (this style used rarely) > __foo__ Name which is used in a special way by Python And FWIW: foo_ When you want to use a reserved name for identifier (ie: 'class_' , 'or_', 'and_' etc) From no.email at please.post Thu Jul 30 08:02:06 2009 From: no.email at please.post (kj) Date: Thu, 30 Jul 2009 12:02:06 +0000 (UTC) Subject: How to "gunzip-iterate" over a file? References: <7x8wi7j80g.fsf@ruckus.brouhaha.com> Message-ID: Robert, Paul, thanks. That was just what I was looking for. kynn From user at example.net Thu Jul 30 08:03:38 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 14:03:38 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> Masklinn wrote: ... > That's an interesting point, but not relevant at the end of the day: > `foo.length` and `length(foo)` have the same "practicality". On the > other hand Ruby can be praised for the coherence: everything's a method > period end of the story; while Python does have a dichotomy between > methods and functions/generic methods (whether or not that dichotomy > bothers users is another issue). ... how would you correct a newbie (like me) who says: "well, the main difference between a function and a method (from the caller's pow) is a syntactic one: fun(obj, arguments) as opposed to: obj.met(arguments) but the effect is just about the same." ? bye From Ray.Joseph at CDICorp.com Thu Jul 30 08:17:05 2009 From: Ray.Joseph at CDICorp.com (ray) Date: Thu, 30 Jul 2009 05:17:05 -0700 (PDT) Subject: Looking for a dream language: sounds like Python to me. References: <468250.83203.qm@web31106.mail.mud.yahoo.com> <4A6DC0FC.90704@stoneleaf.us> <880dece00907270812g1a4bcd25l63754cb7a26f7900@mail.gmail.com> <5b8d13220907270822i7e641387ob4b0a7503d21284@mail.gmail.com> <880dece00907270828y1ad33407re4c52e6b81f8abfc@mail.gmail.com> Message-ID: <976e1fea-4f40-49be-81e1-493456fcf52d@o36g2000vbl.googlegroups.com> > Where can I find a Python functionality like simulink ?? Stef, I saw this at: http://showmedo.com/videotutorials/video?name=7430000&fromSeriesID=743 Ray From jeanmichel at sequans.com Thu Jul 30 08:24:39 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 30 Jul 2009 14:24:39 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> References: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> Message-ID: <4A719107.9020500@sequans.com> superpollo wrote: > Masklinn wrote: > ... >> That's an interesting point, but not relevant at the end of the day: >> `foo.length` and `length(foo)` have the same "practicality". On the >> other hand Ruby can be praised for the coherence: everything's a >> method period end of the story; while Python does have a dichotomy >> between methods and functions/generic methods (whether or not that >> dichotomy bothers users is another issue). > ... > how would you correct a newbie (like me) who says: > > "well, the main difference between a function and a method (from the > caller's pow) is a syntactic one: > > fun(obj, arguments) > > as opposed to: > > obj.met(arguments) > > but the effect is just about the same." > > ? > > bye My suggestion - OO programming: length(foo) calls the foo instance(or class for some language) length method length(bar) calls the bar instance length method, it is **not** the same than the foo method - non OO programming: length(foo) calls the length function with foo as parameter length(bar) calls the **same** length function with the bar parameter (so you better make sure length is handling both foo and bar) So the difference is definitely **not** a syntactic one, but it is true that the syntax obj.method() better illustrates the bind mechanism of the OO programming. JM From clp2 at rebertia.com Thu Jul 30 08:27:12 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 30 Jul 2009 05:27:12 -0700 Subject: Python processors? : WAS Re: Does python have the capability for driver development ? In-Reply-To: <2d8298b9-b857-4b3d-ae63-2056d80cdf7f@d4g2000yqa.googlegroups.com> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <2d8298b9-b857-4b3d-ae63-2056d80cdf7f@d4g2000yqa.googlegroups.com> Message-ID: <50697b2c0907300527o37e2353vb2fa7d62e2f9ebdd@mail.gmail.com> On Thu, Jul 30, 2009 at 3:49 AM, Francesco Bochicchio wrote: > On Jul 30, 11:10?am, Christian Heimes wrote: >> Martin P. Hellwig wrote: >> > Well the pyc, which I thought was the Python bytecode, is then >> > interpreted by the VM. >> >> Python is often referred as byte-code interpreted language. Most modern >> languages are interpreted languages. The list [1] is rather long. >> Technically speaking even native code is interpreted by the micro code >> of most CPUs. >> >> [1]http://en.wikipedia.org/wiki/Interpreted_language >> [2]http://en.wikipedia.org/wiki/Microcode > > Once upon a time there where lisp machines, whith processors designed > to fastly execute lisp code ?... I worked with one of them for 2 > years. > I wonder: has anybody thought of making a python-machine, or at least > a processor able to directly execute high-level bytecode (LLVM-like?). > I think the main feature of such a machine should be hyper-fast hash > lookup. Then dynamic memory management hardware ... Yes, it was considered a decade ago: http://mail.python.org/pipermail/python-list/1999-June/004451.html Cheers, Chris -- http://blog.rebertia.com From wolfgang at rohdewald.de Thu Jul 30 08:29:07 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 14:29:07 +0200 Subject: New implementation of re module In-Reply-To: <4A717E96.3020900@mrabarnett.plus.com> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> <4A717E96.3020900@mrabarnett.plus.com> Message-ID: <200907301429.07850.wolfgang@rohdewald.de> On Thursday 30 July 2009, MRAB wrote: > There are other lines which are similar, eg line 1487. Do they all > give the same/similar error with your compiler? yes. The full output with gcc-4.3: notebook:~/kmj/src$ LANG=C python setup.py build running build running build_py running build_ext building '_regex' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o build/temp.linux-i686-2.6/_regex.o _regex.c: In function 'bmatch_context': _regex.c:1462: error: lvalue required as increment operand _regex.c:1470: error: lvalue required as increment operand _regex.c:1478: error: lvalue required as decrement operand _regex.c:1487: error: lvalue required as decrement operand _regex.c:1593: error: lvalue required as increment operand _regex.c:1606: error: lvalue required as decrement operand _regex.c:1616: error: lvalue required as increment operand _regex.c:1625: error: lvalue required as increment operand _regex.c:1634: error: lvalue required as decrement operand _regex.c:1643: error: lvalue required as decrement operand _regex.c:2036: error: lvalue required as increment operand _regex.c:2047: error: lvalue required as increment operand _regex.c:2059: error: lvalue required as decrement operand _regex.c:2070: error: lvalue required as decrement operand _regex.c:2316: error: lvalue required as increment operand In file included from _regex.c:2431: _regex.c: In function 'umatch_context': _regex.c:1462: error: lvalue required as increment operand _regex.c:1470: error: lvalue required as increment operand _regex.c:1478: error: lvalue required as decrement operand _regex.c:1487: error: lvalue required as decrement operand _regex.c:1593: error: lvalue required as increment operand _regex.c:1606: error: lvalue required as decrement operand _regex.c:1616: error: lvalue required as increment operand _regex.c:1625: error: lvalue required as increment operand _regex.c:1634: error: lvalue required as decrement operand _regex.c:1643: error: lvalue required as decrement operand _regex.c:2036: error: lvalue required as increment operand _regex.c:2047: error: lvalue required as increment operand _regex.c:2059: error: lvalue required as decrement operand _regex.c:2070: error: lvalue required as decrement operand _regex.c:2316: error: lvalue required as increment operand error: command 'gcc' failed with exit status 1 -- Wolfgang From masklinn at masklinn.net Thu Jul 30 08:30:48 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 14:30:48 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> References: <4a718c1a$0$6163$4fafbaef@reader5.news.tin.it> Message-ID: On 30 Jul 2009, at 14:03 , superpollo wrote: > Masklinn wrote: > ... >> That's an interesting point, but not relevant at the end of the >> day: `foo.length` and `length(foo)` have the same "practicality". >> On the other hand Ruby can be praised for the coherence: >> everything's a method period end of the story; while Python does >> have a dichotomy between methods and functions/generic methods >> (whether or not that dichotomy bothers users is another issue). > ... > how would you correct a newbie (like me) who says: > > "well, the main difference between a function and a method (from the > caller's pow) is a syntactic one: > > fun(obj, arguments) > > as opposed to: > > obj.met(arguments) > > but the effect is just about the same." > > ? Depending on the way things are implemented, it can be very similar (see CLOS' generic functions, which have the same look as functions but dispatch based on parameter types). In Python, the difference would be that functions don't automatically dispatch (you have a function for everybody, and any dispatch you perform based on argument types or other qualifiers has to be done manually) whereas methods do dispatch on the first [implied] argument (you execute a precise method corresponding to the object it's being called on). So fun(obj, *arguments) will call the same `fun` whether `obj` is an int, a string or an HTTPFactory whereas obj.met(*arguments) will call a different `met` each time (they all have the same "unqualified" name [and signature if it's part of a protocol], but might very well be implemented completely differently). Therefore -- in python -- the effect isn't anywhere "just about the same". From beldar.cat at gmail.com Thu Jul 30 08:40:41 2009 From: beldar.cat at gmail.com (Beldar) Date: Thu, 30 Jul 2009 05:40:41 -0700 (PDT) Subject: Regexp problem Message-ID: Hi there! I have a problem and i'm not very good at regular expressions. I have a text like "lalala lalala tiruri beldar-is-listening tiruri lalala" I need a regexp to get the 'beldar' part, the format is 'something-is-listening', i need to get the something part, use it in my code, and then replace the whole 'something-is-listening' for another string. Someone can help me please? Thank you! From nobody at nowhere.com Thu Jul 30 08:43:29 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 30 Jul 2009 13:43:29 +0100 Subject: gamma approximation : what is module cmath and where is it located ? References: Message-ID: On Wed, 29 Jul 2009 23:24:11 -0500, pdlemper wrote: > The following numerical approximation for Euler's Gamma function > is found in http://en.wikipedia.org/wiki/Lanczos_approximation > > from cmath import * > This works in Python 3.0 > > But I can't figure out where it gets "cmath". Searching the Python > directory reveals no more than a test_cmath. The only cmath I can find > is a header file in another directory turboc++\Borland\include\ > > dir(cmath) reveals 23 functions overlapping the 37 functions of > the math module. > > What is cmath, where did it come from and how does it differ from > the standard math module ? If you're looking for the file, it's written in C, so it's typically a dynamic library, e.g.: /usr/lib/python2.5/lib-dynload/cmath.so You won't find a cmath.py file. From nytrokiss at gmail.com Thu Jul 30 08:45:09 2009 From: nytrokiss at gmail.com (James Matthews) Date: Thu, 30 Jul 2009 15:45:09 +0300 Subject: Image merging Message-ID: <8a6b8e350907300545i48de1284h82e3c4d9d249ff8@mail.gmail.com> Hi, I need to create an app that takes to images and mergers them. After googling around I have found 4 graphic library's that I can use. However since I am new to "image programming" can you please tell me which one you think would be the best. The ones I found were cairographics PIL Imagemagick GraphicsMagick Any tips? Thanks James -- http://www.goldwatches.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at rgbaz.eu Thu Jul 30 08:54:21 2009 From: python at rgbaz.eu (PythonAB) Date: Thu, 30 Jul 2009 14:54:21 +0200 Subject: Run pyc file without specifying python path ? In-Reply-To: <50697b2c0907300357ld78846ch257547666a453829@mail.gmail.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> <50697b2c0907300357ld78846ch257547666a453829@mail.gmail.com> Message-ID: <736F147B-47A0-4559-8FC3-4134068B5F09@rgbaz.eu> On 30 jul 2009, at 12:57, Chris Rebert wrote: > On Thu, Jul 30, 2009 at 3:42 AM, Barak, Ron wrote: >> >> >> ________________________________ >> From: PythonAB [mailto:python at rgbaz.eu] >> Sent: Thursday, July 30, 2009 12:18 >> To: Barak, Ron >> Cc: 'Dave Angel'; 'python-list at python.org' >> Subject: Re: Run pyc file without specifying python path ? >> >> >> Hi Dave, >> Your solution sort of defeats my intended purpose (sorry for not >> divulging >> my 'hidden agenda'). >> I wanted my application to "hide" the fact that it's a python >> script, and >> look as much as possible like it's a compiled program. >> The reason I don't just give my user a py file, is that I don't >> want a >> cleaver user to change the innards of the script. >> On the other hand, I don't want to make a compiled (freezed?) >> version of the >> application, because it'll grow the resulting file significantly, >> and I >> don't have the experience to know how it will run on different >> Linuxes. >> Bye, >> Ron. >> >> Hey Ron, >> What i usually do to accomplish this is compile the script to >> a .pyc just >> like you >> did and then call that pyc from another script that's not compiled. >> so in your case the not compiled script looks like: >> #!/usr/bin/env python >> import test_pyc.pyc >> >> >> then run that script... >> hope this helps... >> >> >> alternatively you might have a look at: >> http://www.pyinstaller.org/ >> >> >> gr >> Arno >> [BR] Thanks for the answer Arno. But, I wonder - >> >> How is your suggestion, which to the user would look like: >> python wrapper.py > > No, with the shebang line (and assuming execute permissions on the > file), it would look like: > > ./wrapper.py > > (or just `wrapper.py` depending on whether the file is placed in the > $PATH) > > Cheers, > Chris > yes exactly, chris you can distribute a pyc containing your program, and have the wrapper.py execute it gr Arno From python at mrabarnett.plus.com Thu Jul 30 08:56:32 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 13:56:32 +0100 Subject: New implementation of re module In-Reply-To: <200907301429.07850.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> <4A717E96.3020900@mrabarnett.plus.com> <200907301429.07850.wolfgang@rohdewald.de> Message-ID: <4A719880.3070300@mrabarnett.plus.com> Wolfgang Rohdewald wrote: > On Thursday 30 July 2009, MRAB wrote: >> There are other lines which are similar, eg line 1487. Do they all >> give the same/similar error with your compiler? > > yes. The full output with gcc-4.3: > > > notebook:~/kmj/src$ LANG=C python setup.py build > running build > running build_py > running build_ext > building '_regex' extension > gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall - > Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c _regex.c -o > build/temp.linux-i686-2.6/_regex.o > _regex.c: In function 'bmatch_context': > _regex.c:1462: error: lvalue required as increment operand [snip] > error: command 'gcc' failed with exit status 1 > So it complains about: ++(RE_CHAR*)context->text_ptr but not about: ++info->repeat.count Does this mean that the gcc compiler thinks that the cast makes it an rvalue? I'm using Visual C++ 2008 Express Edition, which doesn't complain. What does the C standard say? From davea at dejaviewphoto.com Thu Jul 30 09:02:55 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Thu, 30 Jul 2009 09:02:55 -0400 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: <4A7199FF.8010202@dejaviewphoto.com> Barak, Ron wrote: >> -----Original Message----- >> From: Dave Angel [mailto:davea at ieee.org] >> Sent: Wednesday, July 29, 2009 21:05 >> To: Barak, Ron >> Cc: 'python-list at python.org' >> Subject: Re: Run pyc file without specifying python path ? >> >> Barak, Ron wrote: >> >>> Hi, >>> >>> I wanted to make a python byte-code file executable, >>> >> expecting to be able to run it without specifying "python" on >> the (Linux bash) command line. >> >>> So, I wrote the following: >>> >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python >>> >>> print "hello" >>> [root at VMLinux1 python]# >>> >>> and made its pyc file executable: >>> >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc >>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc >>> [root at VMLinux1 python]# >>> >>> So, I see: >>> >>> [root at VMLinux1 python]# file test_pyc.py* >>> test_pyc.py: a python script text executable >>> test_pyc.pyc: python 2.3 byte-compiled >>> [root at VMLinux1 python]# >>> >>> If I try to do the following, no problem: >>> >>> [root at VMLinux1 python]# python test_pyc.pyc hello >>> [root at VMLinux1 python]# >>> >>> However, the following fails: >>> >>> [root at VMLinux1 python]# ./test_pyc.pyc >>> -bash: ./test_pyc.pyc: cannot execute binary file >>> [root at VMLinux1 python]# >>> >>> Is there a way to run a pyc file without specifying the >>> >> python path ? >> >>> Bye, >>> Ron. >>> >>> >>> >> I don't currently run Unix, but I think I know the problem. >> >> In a text file, the shell examines the first line, and if it >> begins #! >> it's assumed to point to the executable of an interpreter for >> that text file. Presumably the same trick doesn't work for a >> .pyc file. >> >> Why not write a trivial wrapper.py file, don't compile it, >> and let that invoke the main code in the .pyc file? >> >> Then make wrapper.py executable, and you're ready to go. >> >> DaveA >> >> >> > > Hi Dave, > Your solution sort of defeats my intended purpose (sorry for not divulging my 'hidden agenda'). > I wanted my application to "hide" the fact that it's a python script, and look as much as possible like it's a compiled program. > The reason I don't just give my user a py file, is that I don't want a cleaver user to change the innards of the script. > On the other hand, I don't want to make a compiled (freezed?) version of the application, because it'll grow the resulting file significantly, and I don't have the experience to know how it will run on different Linuxes. > Bye, > Ron. > Most of the other answers basically paraphrased my suggestion of making a wrapper file, not compiling it, and making it executable. With that approach, the user just types "wrapper.py" on his command line. And wrapper.py only needs two lines, a shebang, and an import, no big deal if the user modifies it. The rest of your code can be .pyc files. Steven makes some good points. You have to define what level of clever you're protecting from. A determined hacker will get in no matter what you do, unless you want to ship the program in a proprietary embedded system, encased in epoxy. Further, if you have an extension of .py or .pyc, a knowledgeable hacker will know it's probably python. You imply you want it to run unmodifed on multiple unknown Linux versions. I think that lets out binfmt solutions. That means you need to test and support not only multiple Linux implementations, but multiple Python versions, because who knows what the user may have installed. I think you need to rethink your support strategy. And maybe concentrate on being able to detect change, rather than prevent it. DaveA From python.list at tim.thechases.com Thu Jul 30 09:04:24 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 30 Jul 2009 08:04:24 -0500 Subject: Regexp problem In-Reply-To: References: Message-ID: <4A719A58.4020509@tim.thechases.com> > I have a problem and i'm not very good at regular expressions. > I have a text like "lalala lalala tiruri beldar-is-listening tiruri > lalala" I need a regexp to get the 'beldar' part, the format is > 'something-is-listening', i need to get the something part, use it in > my code, and then replace the whole 'something-is-listening' for > another string. Pretty easy: >>> import re >>> s = "lalala lalala tiruri beldar-is-listening tiruri lalala" >>> r = re.compile(r'(\w+)-is-listening') >>> r.search(s).group(1) 'beldar' >>> r.sub('this is a replacement', s) 'lalala lalala tiruri this is a replacement tiruri lalala' -tkc From python at mrabarnett.plus.com Thu Jul 30 09:07:51 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 14:07:51 +0100 Subject: Regexp problem In-Reply-To: References: Message-ID: <4A719B27.8000404@mrabarnett.plus.com> Beldar wrote: > Hi there! > > I have a problem and i'm not very good at regular expressions. > I have a text like "lalala lalala tiruri beldar-is-listening tiruri > lalala" I need a regexp to get the 'beldar' part, the format is > 'something-is-listening', i need to get the something part, use it in > my code, and then replace the whole 'something-is-listening' for > another string. > \w+ will match a word and enclosing it in (...) will capture what was matched: m = re.search(r"(\w+)-is-listening", text) print "Captured '%s'" % m.group(1) print "Matched from %d to %d" % (m.start(), m.end()) From wolfgang at rohdewald.de Thu Jul 30 09:18:27 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 15:18:27 +0200 Subject: New implementation of re module In-Reply-To: <4A719880.3070300@mrabarnett.plus.com> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301429.07850.wolfgang@rohdewald.de> <4A719880.3070300@mrabarnett.plus.com> Message-ID: <200907301518.28122.wolfgang@rohdewald.de> On Thursday 30 July 2009, MRAB wrote: > So it complains about: > > ++(RE_CHAR*)context->text_ptr > > but not about: > > ++info->repeat.count > > Does this mean that the gcc compiler thinks that the cast makes it > an rvalue? I'm using Visual C++ 2008 Express Edition, which doesn't > complain. What does the C standard say? I am not really a C expert but I found some links. Most helpful: http://developer.apple.com/DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/C-Dialect-Options.html (search -fnon-lvalue-assign) so I did the conversion mentioned there. This works: --- _regex.c 2009-07-29 11:34:00.000000000 +0200 +++ n 2009-07-30 15:15:22.000000000 +0200 @@ -1459,7 +1459,7 @@ if (text_ptr < (RE_CHAR*)context->slice_end && text_ptr[0] != '\n') { context->node = node->next_1; - ++(RE_CHAR*)context->text_ptr; + ++*(RE_CHAR**)&context->text_ptr; } else context = reject_context(state, context); break; -- Wolfgang From wfluro at gmail.com Thu Jul 30 09:19:29 2009 From: wfluro at gmail.com (ulf) Date: Thu, 30 Jul 2009 06:19:29 -0700 (PDT) Subject: Python ctypes on win64 References: Message-ID: <433d4958-81e4-4d5f-be51-9d1b2170d1eb@c2g2000yqi.googlegroups.com> On Jul 25, 3:09?am, a... at pythoncraft.com (Aahz) wrote: > In article , Thanks, I'll try there. > > ulf ? wrote: > > >Does anybody know if python includes a win64 version ofctypes? > > >According to the documentation it should be included - at least > >there's no special remarks for win64, and I haven't found any recent > >notes saying that it shouldn't work on win64. Yet it looks like both > >the installer from Python.org and from ActiveState.com have disabled > >it. > > Nobody seems to have followed up to this, you may have better luck on > the capi-sig mailing list. > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "At Resolver we've found it useful to short-circuit any doubt and just ? ? ? ? > refer to comments in code as 'lies'. :-)" > --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 From davea at ieee.org Thu Jul 30 09:20:45 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 09:20:45 -0400 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <4A719E2D.708@ieee.org> Martin P. Hellwig wrote: >
Michel > Claveau - MVP wrote: >> Hi! >> >>> Python is interpreted >> >> No. Python is compiled (--> .pyc) >> But the term "to compile" is not always unambiguous... >> And the notion of "compiler" is not attached to Python (the >> language), but is attached to the implementation. >> >> @+ >> >> MCI > > Well the pyc, which I thought was the Python bytecode, is then > interpreted by the VM. > As Michel says, "to compile" is not always unambiguous. My definition includes a one-way transformation from human-readable source text into something that can be more efficiently interpreted by other code, or by hardware. The compiler really doesn't care whether the machine it's targeting is real or virtual. The CPython implementation of Python compiles the source text into a bytecode file, with extension .pyc. That certainly is a compilation step. Followed (much) later by an interpreted one. To pick a specific implementation of C++, Microsoft C++ compiles C++ source text into an "executable file," with extension .exe (I'm ignoring little details, like the linker). That's a compilation step. Then the exe file is (later) interpreted by the microcode on the Pentium chip. As far as I know, nobody has yet built a microcode implementation of a Python VM (Virtual Machine). Nor have I seen one for the Java VM. However, in the early 80's there was a microcode implementation of the P-system VM. It was never a commercial success, but it existed. And there have been at least three Forth machines, where the hardware itself was designed to support the language's VM. No microcode at all. DaveA From wolfgang at rohdewald.de Thu Jul 30 09:24:28 2009 From: wolfgang at rohdewald.de (Wolfgang Rohdewald) Date: Thu, 30 Jul 2009 15:24:28 +0200 Subject: New implementation of re module In-Reply-To: <200907301518.28122.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <4A719880.3070300@mrabarnett.plus.com> <200907301518.28122.wolfgang@rohdewald.de> Message-ID: <200907301524.28703.wolfgang@rohdewald.de> On Thursday 30 July 2009, Wolfgang Rohdewald wrote: > so I did the conversion mentioned there. This works: I actually do not know if it works - but it compiles. -- Wolfgang From beldar.cat at gmail.com Thu Jul 30 09:32:59 2009 From: beldar.cat at gmail.com (Beldar) Date: Thu, 30 Jul 2009 06:32:59 -0700 (PDT) Subject: Regexp problem References: Message-ID: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> On 30 jul, 15:07, MRAB wrote: > Beldar wrote: > > Hi there! > > > I have a problem and i'm not very good at regular expressions. > > I have a text like "lalala lalala tiruri beldar-is-listening tiruri > > lalala" I need a regexp to get the 'beldar' part, the format is > > 'something-is-listening', i need to get the something part, use it in > > my code, and then replace the whole 'something-is-listening' for > > another string. > > \w+ will match a word and enclosing it in (...) will capture what was > matched: > > ? ? ?m = re.search(r"(\w+)-is-listening", text) > ? ? ?print "Captured '%s'" % m.group(1) > ? ? ?print "Matched from %d to %d" % (m.start(), m.end()) Ok, thank you all, it was very helpful! From ccurvey at gmail.com Thu Jul 30 09:35:46 2009 From: ccurvey at gmail.com (Chris Curvey) Date: Thu, 30 Jul 2009 06:35:46 -0700 (PDT) Subject: PyPDF and print restrictions References: <90034475-cf07-4a57-b8cf-ec2949e0e544@d23g2000vbm.googlegroups.com> Message-ID: <13cc96b6-7c1b-4835-b711-eabedb4d6442@f10g2000vbf.googlegroups.com> On Jul 27, 4:16?pm, Chris Curvey wrote: > Has anyone out there been able to enforce print restrictions on a PDF > document by usingPyPDF? The documentation for "encrypt" states: > > ?# @param user_pwd The "user password", which allows for opening and > reading > ?# the PDF file with the restrictions provided. > > But there is no parameter for providing a restriction, and I can't > find a reference to any kind of restriction besides this comment in > the docs. > > Thanks in advance! I found some (I think old-ish) documentation on encryption and permissions (of course, I've lost the link). I think there are additional permissions that have been implemented since the doc that I found, but this patch works for me. Index: C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py =================================================================== --- C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py (revision 1) +++ C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py (revision 2) @@ -118,7 +118,10 @@ # @param use_128bit Boolean argument as to whether to use 128bit # encryption. When false, 40bit encryption will be used. By default, this # flag is on. - def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True): + # @param perm_mask bitmask of the permissions that should be granted. + # Defaults to -1, which is "everything permitted" + def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True, + perm_mask=-1): import md5, time, random if owner_pwd == None: owner_pwd = user_pwd @@ -130,8 +133,8 @@ V = 1 rev = 2 keylen = 40 / 8 - # permit everything: - P = -1 + + P = perm_mask O = ByteStringObject(_alg33(owner_pwd, user_pwd, rev, keylen)) ID_1 = md5.new(repr(time.time())).digest() ID_2 = md5.new(repr(random.random())).digest() Index: C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py =================================================================== --- C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py (revision 1) +++ C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py (revision 2) @@ -1,2 +1,11 @@ from pdf import PdfFileReader, PdfFileWriter + +PERM_NONE = 0 +PERM_PRINT = 2 +PERM_MODIFY = 4 +PERM_COPY_TEXT = 8 +PERM_ANNOTATE = 16 + +PERM_ALL = PERM_PRINT | PERM_MODIFY | PERM_COPY_TEXT | PERM_ANNOTATE + __all__ = ["pdf"] From marcusw at cox.net Thu Jul 30 09:38:49 2009 From: marcusw at cox.net (Marcus Wanner) Date: Thu, 30 Jul 2009 09:38:49 -0400 Subject: Regexp problem In-Reply-To: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> Message-ID: On 7/30/2009 9:32 AM, Beldar wrote: > On 30 jul, 15:07, MRAB wrote: >> Beldar wrote: >>> Hi there! >>> I have a problem and i'm not very good at regular expressions. >>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>> lalala" I need a regexp to get the 'beldar' part, the format is >>> 'something-is-listening', i need to get the something part, use it in >>> my code, and then replace the whole 'something-is-listening' for >>> another string. >> \w+ will match a word and enclosing it in (...) will capture what was >> matched: >> >> m = re.search(r"(\w+)-is-listening", text) >> print "Captured '%s'" % m.group(1) >> print "Matched from %d to %d" % (m.start(), m.end()) > > Ok, thank you all, it was very helpful! Wow, I really need to learn more about regexp... Any tutorials you guys can recommend? Marcus From piet at cs.uu.nl Thu Jul 30 09:39:35 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 30 Jul 2009 15:39:35 +0200 Subject: New implementation of re module References: Message-ID: >>>>> MRAB (M) wrote: >M> Hi all, >M> I've been working on a new implementation of the re module. The details >M> are at http://bugs.python.org/issue2636, specifically from >M> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >M> Python 2.6 on Windows if you want to try it out. >M> I'm interested in how fast it is generally, compared with the current re >M> module, but especially when faced with those 'pathological' regular >M> expressions which seem to take a long time to finish, for example: >M> re.search(r"^(.+|D)*A$", "x" * 25 + "B") >M> which on my PC (1.8GHz) takes 18.98secs with the re module but <0.01secs >M> with this new implementation. Is this version also going to use the Thompson approach? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From martin.hellwig at dcuktec.org Thu Jul 30 09:43:26 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 14:43:26 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: Dave Angel wrote: Ah yes, we thread on the territory of word definition and difference in interpretation. Any argument is doomed to fail if not agreed or at least taken in perspective of the terminology used by users. I could be (well it is quite likely) wrong in my interpretation of the terminology, but here goes it anyway: Machine Code: Whatever the machine executes, it could be that the CPU uses an abstraction of microcode to do this but from the perspective of the user, this is all done in the same 'black box' Compiling: Translate words/symbols/mnemonics to machine code, which than can be either loaded, linked and executed by an OS or read and executed by the BIOS. Interpreted: Instructions which can be fed to a previous compiled program that is able to dynamically change its execution and flow without the need to recompile itself. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From piet at cs.uu.nl Thu Jul 30 09:45:44 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 30 Jul 2009 15:45:44 +0200 Subject: Semaphore Techniques References: <004e308e-2621-4411-bb1d-8c0f23259367@d4g2000yqa.googlegroups.com> <8cb1ba65-be71-49b7-8955-54c17f351a40@w6g2000yqw.googlegroups.com> Message-ID: >>>>> Carl Banks (CB) wrote: >CB> On Jul 29, 7:14?am, Piet van Oostrum wrote: >>> >>>>> Carl Banks (CB) wrote: >>> >CB> On Jul 28, 3:15?pm, John D Giotta wrote: >>> >>> I'm looking to run a process with a limit of 3 instances, but each >>> >>> execution is over a crontab interval. I've been investigating the >>> >>> threading module and using daemons to limit active thread objects, but >>> >>> I'm not very successful at grasping the documentation. >>> >>> >>> Is it possible to do what I'm trying to do and if so anyone know of a >>> >>> useful example to get started? >>> >CB> It seems like you want to limit the number of processes to three; the >>> >CB> threading module won't help you there because it deals with threads >>> >CB> within a single process. >>> >CB> What I'd do is to simply run the system ps to see how many processes >>> >CB> are running (ps is pretty versatile on most systems and can find >>> >CB> specifically targeted processes like you program), and exit if there >>> >CB> are already three. >>> >>> That will surely run into some race conditions. >CB> What, the OS might not have gotten around to update the process table >CB> to include a process started minutes ago? (He said he was starting >CB> the processes over crontab intervals, not that he communicated what he >CB> wanted well.) No but between the time you get the ps output and decide not to start a new process one of the processes might have exited. As I said it probably is not a big deal, but you (he) should be aware of it I think. The other possible race condition: two processes starting at approximately the same time and both not detecting the other will probably not occur because of the time distance between starting the processes by cron. Unless the system is so busy that ps takes a loooong time. The problem is similar to the sleeping barber problem (3 sleeping barbers actually). -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Thu Jul 30 09:53:31 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 09:53:31 -0400 Subject: gamma approximation : what is module cmath and where is it located ? In-Reply-To: References: Message-ID: <4A71A5DB.60409@ieee.org> pdlemper at earthlink.net wrote: > from cmath import * > > > What is cmath, where did it come from and how does it differ from > the standard math module ? > > Dave WB3DWE > I saw the number 4 in silver, Guido > (apologies to Wm Carlos Williams) > > I'm surprised that with all the responses, nobody has pointed out the dangers involved in using from cmath import * In general, this form of import is discouraged. I presume there's no problem if you're not also importing 'math' and all your calls are intending to be for the complex versions. But if you're mixing things up, consider using the more conventional import cmath and using cmath. as a prefix before the function calls. DaveA From metolone+gmane at gmail.com Thu Jul 30 09:56:07 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Thu, 30 Jul 2009 06:56:07 -0700 Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: "NighterNet" wrote in message news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com... >I am trying to figure out how to send text or byte in python 3.1. I am > trying to send data to flash socket to get there. I am not sure how to > work it. > > buff= 'id=' , self.id , ':balive=False\n' > clientSock.send(buff); > -- > http://mail.python.org/mailman/listinfo/python-list > Python 3.1 strings are Unicode (think characters not bytes). When writing to files, sockets, etc. bytes must be used. Unicode strings have an encode() method, where you can specify an appropriate encoding (ascii, latin-1, windows-1252, utf-8, etc.): clientSock.send(buff.encode('ascii')) When reading from the socket, you can decode() the byte strings back into Unicode strings. data = clientSock.recv(1024).decode('ascii') -Mark From bieffe62 at gmail.com Thu Jul 30 10:11:24 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 07:11:24 -0700 (PDT) Subject: No PyPI search for 3.x compatible packages References: Message-ID: On 30 Lug, 01:55, Neil Hodgson wrote: > ? ?There appears to be no way to search PyPI for packages that are > compatible with Python 3.x. There are classifiers for 'Programming > Language' including 'Programming Language :: Python :: 3' but that seems > to be for implementation language since there are so many packages that > specify C. There are a total of 109 packages classified with Python :: > [3, 3.0, 3.1] out of a total of 4829 packages.http://pypi.python.org/pypi?:action=browse&show=all&c=214&c=533 > > ? ?The search box appears to search for any word entered so a search > like "xml 3.0" or "xml AND 3.0" does not help. > > ? ?Some packages include version information in the Py Version column of > their download lists or embedded in the download file names. Projects > are often constrained to a particular set of Python versions so need to > choose packages that will work with those versions. It would be helpful > if PyPI made this information more visible and searchable. > > ? ?Neil Are you sure? I note that for example pygtk has as language tags both C and python. So maybe a C extension for python3 would have both C and python 3 as language tags. I suspect that the 109 packages you found are the only ones obf the 4829 which works with python3 (but I hope to be wrong ). Ciao ----- FB From peter at peter-b.co.uk Thu Jul 30 10:17:52 2009 From: peter at peter-b.co.uk (Peter Brett) Date: Thu, 30 Jul 2009 15:17:52 +0100 Subject: Regexp problem References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> Message-ID: Marcus Wanner writes: > On 7/30/2009 9:32 AM, Beldar wrote: >> On 30 jul, 15:07, MRAB wrote: >>> Beldar wrote: >>>> Hi there! >>>> I have a problem and i'm not very good at regular expressions. >>>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>>> lalala" I need a regexp to get the 'beldar' part, the format is >>>> 'something-is-listening', i need to get the something part, use it in >>>> my code, and then replace the whole 'something-is-listening' for >>>> another string. >>> \w+ will match a word and enclosing it in (...) will capture what was >>> matched: >>> >>> m = re.search(r"(\w+)-is-listening", text) >>> print "Captured '%s'" % m.group(1) >>> print "Matched from %d to %d" % (m.start(), m.end()) >> >> Ok, thank you all, it was very helpful! > Wow, I really need to learn more about regexp... > Any tutorials you guys can recommend? I have to confess that after fiddling with regexps for quite a while with no great success, I learnt the hard (and best) way, i.e. using them to write something vile and horrible. [*] I commend this path to you also. ;-) Cheers, Peter [*] http://git.gpleda.org/?p=gaf.git;a=blob;f=libgeda/desktop-i18n;h=6fab9b85b -- Peter Brett Remote Sensing Research Group Surrey Space Centre From p.f.moore at gmail.com Thu Jul 30 10:20:10 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Thu, 30 Jul 2009 15:20:10 +0100 Subject: No PyPI search for 3.x compatible packages In-Reply-To: References: Message-ID: <79990c6b0907300720s3a2ad4a8lf49bc265b653f55f@mail.gmail.com> 2009/7/30 Francesco Bochicchio : > On 30 Lug, 01:55, Neil Hodgson wrote: >> ? ?There appears to be no way to search PyPI for packages that are >> compatible with Python 3.x. There are classifiers for 'Programming >> Language' including 'Programming Language :: Python :: 3' but that seems >> to be for implementation language since there are so many packages that >> specify C. There are a total of 109 packages classified with Python :: >> [3, 3.0, 3.1] out of a total of 4829 packages.http://pypi.python.org/pypi?:action=browse&show=all&c=214&c=533 >> >> ? ?The search box appears to search for any word entered so a search >> like "xml 3.0" or "xml AND 3.0" does not help. >> >> ? ?Some packages include version information in the Py Version column of >> their download lists or embedded in the download file names. Projects >> are often constrained to a particular set of Python versions so need to >> choose packages that will work with those versions. It would be helpful >> if PyPI made this information more visible and searchable. >> >> ? ?Neil > > Are you sure? I note that for example pygtk has as language tags both > C and python. So maybe a C extension > for python3 would have both C and python 3 as language tags. > > I suspect that the 109 packages you found are the only ones obf the > 4829 which works with python3 (but I hope > to be wrong ). Also, of course, they may not be the only ones that work, but merely the only ones where the author has checked they work and tagged the entry. It's quite possible that some packages will work unmodified... Paul. From kwatch at gmail.com Thu Jul 30 10:30:25 2009 From: kwatch at gmail.com (kwatch) Date: Thu, 30 Jul 2009 07:30:25 -0700 (PDT) Subject: [ANN] pyKook 0.0.2 - a simple build tool similar to Make or Ant Message-ID: Hi, I have released pyKook 0.0.2. http://pypi.python.org/pypi/Kook/0.0.2 http://www.kuwata-lab.com/kook/ http://www.kuwata-lab.com/kook/pykook-users-guide.html Overview -------- pyKook is a simple build tool similar to Make, Ant, Rake, or SCons. pyKook regards software project as cooking. Terms used in pyKook are cooking terms. For example: cookbook - Makefile product - target file ingredient - source file recipe - how to create target from source spices - command-line options for recipe Cookbook (= Makefile) is written in pure Python. You can write any statements or expressions in cookbook. NOTICE: pyKook is under alpha release. Spec and features may be changed in the future without previous announcement. Example ------- Example of cookbook (Kookbook.py): -------------------------------------------------- ## ## properties ## cc = prop('cc', 'gcc') cflags = prop('cflags', '-g -Wall') ## ## recipes ## @ingreds("hello") def task_all(c): pass @product("hello") @ingreds("hello.o") def file_command(c): """generates hello command""" system(c%"$(cc) $(cflags) -o $(product) $(ingred)") @product("*.o") @ingreds("$(1).c", if_exists("$(1).h")) def file_ext_o(c): """compile '*.c' and '*.h'""" system(c%"$(cc) $(cflags) -c $(1).c") def task_clean(c): rm_f("*.o") -------------------------------------------------- Exampe of result: ================================================== bash> ls Kookbook.py hello.c hello.h bash> pykook -l Properties: cc : 'gcc' cflags : '-g -Wall' Task recipes: all : cook all products clean : remove by-products File recipes: hello : generates hello command *.o : compile '*.c' and '*.h' (Tips: you can set 'kook_default_product' variable in your kookbook.) bash> pykook all # or, pykook --cc=gcc4 all ### *** hello.o (func=file_ext_o) $ gcc -g -Wall -c hello.c ### ** hello (func=file_command) $ gcc -g -Wall -o hello hello.o ### * all (func=task_all) ================================================== See users-guide for more details. http://www.kuwata-lab.com/kook/pykook-users-guide.html Enhancements, Changes, Bug fixes sice 0.0.1 ------------------------------------------- Enhancements: - Python 3 support. - Add 'kk' script which is shortcat for kk command. Changes: - Decorator '@cmdopts()' is renamed to '@spices()', and there is no need to call parse_cmdopts(). ### prev version @cmdopts('-v: verbose', '-f file: filename') def task_example(c, *args): opts, rests = c.parse_cmdopts(args) verbose = opts.get('v', False): fileame = opts.get('f', None) ### since this release (0.0.2) @spices('-v: verbose', '-f file: filename') def task_example(c, *args, *kwargs): opts, rests = kwarts, args verbose = opts.get('v', False): fileame = opts.get('f', None) - Remove 'pyk' script - Testing library is changed from Python's unittest library into 'test/oktest.py'. Bugfixes: - glob2() now recognizes files on current directory. Have fun! -- regards, makoto kuwata From davea at ieee.org Thu Jul 30 10:30:55 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 10:30:55 -0400 Subject: Does python have the capability for driver development ? In-Reply-To: References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <4A71AE9F.2000900@ieee.org> Martin P. Hellwig wrote: >
Dave > Angel wrote: > > Ah yes, we thread on the territory of word definition and difference > in interpretation. Any argument is doomed to fail if not agreed or at > least taken in perspective of the terminology used by users. > > I could be (well it is quite likely) wrong in my interpretation of the > terminology, but here goes it anyway: > > Machine Code: > Whatever the machine executes, it could be that the CPU uses an > abstraction of microcode to do this but from the perspective of the > user, this is all done in the same 'black box' > > Compiling: > Translate words/symbols/mnemonics to machine code, which than can be > either loaded, linked and executed by an OS or read and executed by > the BIOS. > > Interpreted: > Instructions which can be fed to a previous compiled program that is > able to dynamically change its execution and flow without the need to > recompile itself. > Depending on the level of understanding of the user, plus his history and his biases, he will include more or less in his "black box." In the old days, microcode was not "on-chip" but stored separately in control memory. And on many machines, it was changeable at will. To many users these days, the entire system including software is a black box, which gradually breaks down (gets slow, runs out of space, crashes a lot) and must be replaced. They don't distinguish operating system from application, real memory from virtual, or viruses from bugs. But of course those users wouldn't be participating in this discussion. My background includes specifying hardware instruction sets and architecture. And writing microcode for multiple machines. And writing parts of compilers, interpreters, assemblers, and so on. And microcoding interpreters. And hooking into compilers to modify how they would generate code. And hooking into runtimes to test running code in realtime. So I tend to have very flexible definition of compiler and interpreter. Probably the only reason I jumped in here was the unmentioned bias that somehow a compiler is superior to an interpreter. I think I'd better extend my definition of compilation. It's a step that's statically taken over a series of instructions (not necessarily text source), that transforms it into a form closer to the targeted enviromment, real or virtual. Most C++ compilers have two compilers operating serially, the first to turn the source code into an intermediate form (like byte code), and the second to generate what is commonly called "machine code." The second part of course is duplicated for each different target processor. So Java is compiled into byte code, and the typical java VM then compiles that piecewise into machine code (so called JIT compiling, for just in time). BTW, interpreters don't have to be written in a compiled language. Anyway, I don't object to your definitions. We all have different perspectives. DaveA From Ron.Barak at lsi.com Thu Jul 30 10:47:37 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 15:47:37 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <4A7199FF.8010202@dejaviewphoto.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26D1@enbmail01.lsi.com> > -----Original Message----- > From: Dave Angel [mailto:davea at dejaviewphoto.com] > Sent: Thursday, July 30, 2009 16:03 > To: Barak, Ron > Cc: 'Dave Angel'; 'python-list at python.org' > Subject: RE: Run pyc file without specifying python path ? > > Barak, Ron wrote: > >> -----Original Message----- > >> From: Dave Angel [mailto:davea at ieee.org] > >> Sent: Wednesday, July 29, 2009 21:05 > >> To: Barak, Ron > >> Cc: 'python-list at python.org' > >> Subject: Re: Run pyc file without specifying python path ? > >> > >> Barak, Ron wrote: > >> > >>> Hi, > >>> > >>> I wanted to make a python byte-code file executable, > >>> > >> expecting to be able to run it without specifying "python" on the > >> (Linux bash) command line. > >> > >>> So, I wrote the following: > >>> > >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > >>> > >>> print "hello" > >>> [root at VMLinux1 python]# > >>> > >>> and made its pyc file executable: > >>> > >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc > >>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > >>> [root at VMLinux1 python]# > >>> > >>> So, I see: > >>> > >>> [root at VMLinux1 python]# file test_pyc.py* > >>> test_pyc.py: a python script text executable > >>> test_pyc.pyc: python 2.3 byte-compiled > >>> [root at VMLinux1 python]# > >>> > >>> If I try to do the following, no problem: > >>> > >>> [root at VMLinux1 python]# python test_pyc.pyc hello > >>> [root at VMLinux1 python]# > >>> > >>> However, the following fails: > >>> > >>> [root at VMLinux1 python]# ./test_pyc.pyc > >>> -bash: ./test_pyc.pyc: cannot execute binary file > >>> [root at VMLinux1 python]# > >>> > >>> Is there a way to run a pyc file without specifying the > >>> > >> python path ? > >> > >>> Bye, > >>> Ron. > >>> > >>> > >>> > >> I don't currently run Unix, but I think I know the problem. > >> > >> In a text file, the shell examines the first line, and if > it begins > >> #! > >> it's assumed to point to the executable of an interpreter for that > >> text file. Presumably the same trick doesn't work for a .pyc file. > >> > >> Why not write a trivial wrapper.py file, don't compile it, and let > >> that invoke the main code in the .pyc file? > >> > >> Then make wrapper.py executable, and you're ready to go. > >> > >> DaveA > >> > >> > >> > > > > Hi Dave, > > Your solution sort of defeats my intended purpose (sorry > for not divulging my 'hidden agenda'). > > I wanted my application to "hide" the fact that it's a > python script, and look as much as possible like it's a > compiled program. > > The reason I don't just give my user a py file, is that I > don't want a cleaver user to change the innards of the script. > > On the other hand, I don't want to make a compiled > (freezed?) version of the application, because it'll grow the > resulting file significantly, and I don't have the experience > to know how it will run on different Linuxes. > > Bye, > > Ron. > > Hi Dave, > Most of the other answers basically paraphrased my suggestion > of making a wrapper file, not compiling it, and making it > executable. With that > approach, the user just types "wrapper.py" on his command line. > > And wrapper.py only needs two lines, a shebang, and an > import, no big deal if the user modifies it. The rest of > your code can be .pyc files. > > Steven makes some good points. You have to define what level > of clever you're protecting from. A determined hacker will The clever I try to guard against is not at hacker level, just the curios worker. > get in no matter what you do, unless you want to ship the > program in a proprietary embedded system, encased in epoxy. > Further, if you have an extension of .py or .pyc, a > knowledgeable hacker will know it's probably python. Granted, but the user will know that from the user manual anyway :-) > > You imply you want it to run unmodifed on multiple unknown > Linux versions. I think that lets out binfmt solutions. Actually, no: I know the set of Linuxes my users are likely to have. > That means you need to test and support not only multiple > Linux implementations, but multiple Python versions, because > who knows what the user may have installed. I think you need > to rethink your support strategy. And maybe concentrate on > being able to detect change, rather than prevent it. > > DaveA > > Thanks for the helpful suggestions. Ron. From invalid at invalid Thu Jul 30 10:50:47 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 09:50:47 -0500 Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> Message-ID: On 2009-07-30, Dave Angel wrote: > Steven makes some good points. You have to define what level > of clever you're protecting from. A determined hacker will > get in no matter what you do, unless you want to ship the > program in a proprietary embedded system, encased in epoxy. That won't stop a dedicated reverse-engineering effort. It might take an X-ray machine, machine tools, various nasty chemicals, and quite a bit of skill and patience -- but it can be done. > Further, if you have an extension of .py or .pyc, a > knowledgeable hacker will know it's probably python. > > You imply you want it to run unmodifed on multiple unknown > Linux versions. I think that lets out binfmt solutions. That > means you need to test and support not only multiple Linux > implementations, but multiple Python versions, because who > knows what the user may have installed. I think you need to > rethink your support strategy. And maybe concentrate on being > able to detect change, rather than prevent it. Or even being able to benefit from change. :) -- Grant Edwards grante Yow! FROZEN ENTREES may at be flung by members of visi.com opposing SWANSON SECTS ... From sajmikins at gmail.com Thu Jul 30 10:52:46 2009 From: sajmikins at gmail.com (Simon Forman) Date: Thu, 30 Jul 2009 10:52:46 -0400 Subject: Very Strange Problem In-Reply-To: <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> References: <77e5896b0907290826m2366e205ia933a9339dc70231@mail.gmail.com> <4A709B77.9080209@ieee.org> <77e5896b0907291210m26ecf5adlb292b895f649d7f3@mail.gmail.com> Message-ID: <50f98a4c0907300752mbabd920ma208ca6a0b8f69f2@mail.gmail.com> On Wed, Jul 29, 2009 at 3:10 PM, Omer Khalid wrote: > Hi Dave, > > Thanks for your reply. I actually didn't cut and paste my code as it was > dispersed in different places, i typed the logic behind my code in the email > (and obiviously made some typos, indentations is some thing else) but my Please, do not do that. It's very difficult to debug code that hasn't been seen. The code you posted has numerous problems (that likely have nothing to do with your actual problem.) If you're going to post code, try to recreate the issue with a small runnable script. If you can't do that, as it sounds like it would be difficult in this case as your code is dispersed in different places, at least post the relevant portions of the actual code. Don't re-type. > real code does not have these problems as my application runs fine with out > errors... > > Except that the line where i want to update the value doesn't get updated > and no exception is thrown. What's surprising for me is that i am doing the > same thing in hundreds of places in my 3k+ line code but by some reason this > part doesn't work... > > As far as the global variables are concerned, i am using them in other > places too and didn't see any problems. > > I think some thing else is going on here as the statement above and below my > modified lines get executed. If the statements above and below that line(s) are executed, then that line is certainly being executed as well. Try introducing some additional print statements to verify your mental model of what's happening: # set it to 1 print jobs print jobs[index] print jobs[index]['v'] jobs[index]['v'] = 1 print jobs print jobs[index] print jobs[index]['v'] print "Set to 1" > Is there a way in Python to debug memory address or to see where in memory > this object is stored, and is there a lock on it or else? You are barking up the wrong tree. Somewhere in your code you're doing something silly that's causing your issue. From ben+python at benfinney.id.au Thu Jul 30 10:53:35 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 31 Jul 2009 00:53:35 +1000 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: <87vdlate6o.fsf@benfinney.id.au> "Martin P. Hellwig" writes: > Machine Code: > Whatever the machine executes, it could be that the CPU uses an > abstraction of microcode to do this but from the perspective of the > user, this is all done in the same 'black box' This requires, of course, defining what is the machine. Python bytecode targets a virtual machine that is implemented differently for each hardware platform. > Compiling: > Translate words/symbols/mnemonics to machine code, which than can be > either loaded, linked and executed by an OS or read and executed by > the BIOS. Related to the above point, the ?machine code? can just as easily be codes for a virtual machine specification. This is the case for the bytecode instructions Python gets compiled to. > Interpreted: > Instructions which can be fed to a previous compiled program that is > able to dynamically change its execution and flow without the need to > recompile itself. This doesn't make much sense to me, I must say. I'd say, instead, that a program is interpreted if its instruction are dynamically translated to underlying platform instructions at execution time. This is the case for the bytecode instructions interpreted by the Python virtual machine. -- \ ?Often, the surest way to convey misinformation is to tell the | `\ strict truth.? ?Mark Twain, _Following the Equator_ | _o__) | Ben Finney From invalid at invalid Thu Jul 30 10:55:26 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 09:55:26 -0500 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: On 2009-07-30, Martin P. Hellwig wrote: > Michel Claveau - MVP wrote: > >>> Python is interpreted >> >> No. Python is compiled (--> .pyc) >> But the term "to compile" is not always unambiguous... >> And the notion of "compiler" is not attached to Python (the >> language), but is attached to the implementation. > > Well the pyc, which I thought was the Python bytecode, It is, but that's just one particular implementation you're talking about (though by far the most widely used one). > is then interpreted by the VM. Yup. Just like .exe files are interpreted by the microcode in the processor that implements the IA32 VM. It would be quite possible to put a Python VM into hardware. Alternatevly, you can compiler Python into Java bytecode and run that "directly" on hardware. -- Grant Edwards grante Yow! It don't mean a at THING if you ain't got visi.com that SWING!! From draghuram at gmail.com Thu Jul 30 10:59:40 2009 From: draghuram at gmail.com (Raghuram Devarakonda) Date: Thu, 30 Jul 2009 07:59:40 -0700 (PDT) Subject: Connecting multiple test cases in a sort of pipe References: <7dctogF29q4c6U1@mid.uni-berlin.de> Message-ID: <63715814-ff14-4b95-9160-d98325640a8b@v20g2000yqm.googlegroups.com> On Jul 30, 2:43 am, "Diez B. Roggisch" wrote: > Write a TestCase-subclass that simply works like this: > > class FileTests(TestCase): > > def test_create(self): > self.created_file = create() > assert something > > def test_list(self): > self.test_create() > delete(self.created_file) > > Passing state around is one of the reasons objects have been invented :) > And nobody ever said that you can't invoke a test-method from somewhere > else. Just to be clear, are you suggesting that these tests be run by a custom runnner rather than by the default runner from unittest? Thanks, Raghu From invalid at invalid Thu Jul 30 11:03:56 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 10:03:56 -0500 Subject: Does python have the capability for driver development ? References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> Message-ID: On 2009-07-30, Dave Angel wrote: > As far as I know, nobody has yet built a microcode implementation of a > Python VM (Virtual Machine). Nor have I seen one for the Java VM. Didn't Sun or somebody do one 10-12 years ago? Or did I misinterpret some press release or something? Off to google... > However, in the early 80's there was a microcode implementation of the > P-system VM. Ah yes. I remember sitting at an Intel MDS-80 "blue box" CP/M system entering assembly language by hand for a simple Pascal -> P-code compiler. IIRC, I typed it from a listing in the "Byte Big Book of Pascal". That machine was pretty high-tech, since I could save my work on an 8" floppy rather than a spool of paper tape. The floppy disks didn't leave big oil stains in your backpack! I got the compiler working, but I don't remember ever having a VM and run-time system. > It was never a commercial success, but it existed. And there > have been at least three Forth machines, where the hardware > itself was designed to support the language's VM. No > microcode at all. -- Grant Edwards grante Yow! When you get your at PH.D. will you get able to visi.com work at BURGER KING? From keith at nekotaku.com Thu Jul 30 11:09:20 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 08:09:20 -0700 (PDT) Subject: Use existing IE cookie Message-ID: Hi there, Relevant versions: Python 2.5, Vista Home, IE7 I am trying to scrape a website I have browsed manually in the past, and also manually selected my options, and now want python to use my existing cookie from the manual browse when downloading data. Using: http://code.activestate.com/recipes/80443/ I have found the "name" of the relevant cookie, just after reading urllib2 docs, I can't see how to "send" or have my python instance use "MY" existing cookie. Using the following: *** import re import urllib2, cookielib # set things up for cookies opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) urllib2.install_opener(opener) reply = urllib2.urlopen('foo.html').read() print reply *** This does return data, just default data, not the data from the options I set up when manually browsing. My sense is that I need "something" in the () part of HTTPCookieProcessor() but I have no idea as to what... the docs say "cookiejar" but the only code examples I have found are to create a cookiejar for the existing Python session, not to use the cookies from my prior manual meanderings. Any help greatly appreciated. From Ron.Barak at lsi.com Thu Jul 30 11:10:17 2009 From: Ron.Barak at lsi.com (Barak, Ron) Date: Thu, 30 Jul 2009 16:10:17 +0100 Subject: Run pyc file without specifying python path ? In-Reply-To: <4A7199FF.8010202@dejaviewphoto.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <7F0503CD69378F49BE0DC30661C6CCF6701A26D2@enbmail01.lsi.com> Hi Dave, On second thoughts, I may have a problem implementing the wrapper solution, because my actual test_pyc.pyc, needs to parse its command line. Namely, the actual call to test_pyc.pyc looks something like this: $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t tvn -p pool1 -l -c And I don't know of a way to add these parameters to the "import test_pyc" in wrapper Is there a way to pass information to an imported module ? (Sorry if there's an obvious answer, I just cannot figure it out). Bye, Ron. > -----Original Message----- > From: Dave Angel [mailto:davea at dejaviewphoto.com] > Sent: Thursday, July 30, 2009 16:03 > To: Barak, Ron > Cc: 'Dave Angel'; 'python-list at python.org' > Subject: RE: Run pyc file without specifying python path ? > > Barak, Ron wrote: > >> -----Original Message----- > >> From: Dave Angel [mailto:davea at ieee.org] > >> Sent: Wednesday, July 29, 2009 21:05 > >> To: Barak, Ron > >> Cc: 'python-list at python.org' > >> Subject: Re: Run pyc file without specifying python path ? > >> > >> Barak, Ron wrote: > >> > >>> Hi, > >>> > >>> I wanted to make a python byte-code file executable, > >>> > >> expecting to be able to run it without specifying "python" on the > >> (Linux bash) command line. > >> > >>> So, I wrote the following: > >>> > >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > >>> > >>> print "hello" > >>> [root at VMLinux1 python]# > >>> > >>> and made its pyc file executable: > >>> > >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc > >>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc > >>> [root at VMLinux1 python]# > >>> > >>> So, I see: > >>> > >>> [root at VMLinux1 python]# file test_pyc.py* > >>> test_pyc.py: a python script text executable > >>> test_pyc.pyc: python 2.3 byte-compiled > >>> [root at VMLinux1 python]# > >>> > >>> If I try to do the following, no problem: > >>> > >>> [root at VMLinux1 python]# python test_pyc.pyc hello > >>> [root at VMLinux1 python]# > >>> > >>> However, the following fails: > >>> > >>> [root at VMLinux1 python]# ./test_pyc.pyc > >>> -bash: ./test_pyc.pyc: cannot execute binary file > >>> [root at VMLinux1 python]# > >>> > >>> Is there a way to run a pyc file without specifying the > >>> > >> python path ? > >> > >>> Bye, > >>> Ron. > >>> > >>> > >>> > >> I don't currently run Unix, but I think I know the problem. > >> > >> In a text file, the shell examines the first line, and if > it begins > >> #! > >> it's assumed to point to the executable of an interpreter for that > >> text file. Presumably the same trick doesn't work for a .pyc file. > >> > >> Why not write a trivial wrapper.py file, don't compile it, and let > >> that invoke the main code in the .pyc file? > >> > >> Then make wrapper.py executable, and you're ready to go. > >> > >> DaveA > >> > >> > >> > > > > Hi Dave, > > Your solution sort of defeats my intended purpose (sorry > for not divulging my 'hidden agenda'). > > I wanted my application to "hide" the fact that it's a > python script, and look as much as possible like it's a > compiled program. > > The reason I don't just give my user a py file, is that I > don't want a cleaver user to change the innards of the script. > > On the other hand, I don't want to make a compiled > (freezed?) version of the application, because it'll grow the > resulting file significantly, and I don't have the experience > to know how it will run on different Linuxes. > > Bye, > > Ron. > > > Most of the other answers basically paraphrased my suggestion > of making a wrapper file, not compiling it, and making it > executable. With that > approach, the user just types "wrapper.py" on his command line. > > And wrapper.py only needs two lines, a shebang, and an > import, no big deal if the user modifies it. The rest of > your code can be .pyc files. > > Steven makes some good points. You have to define what level > of clever you're protecting from. A determined hacker will > get in no matter what you do, unless you want to ship the > program in a proprietary embedded system, encased in epoxy. > Further, if you have an extension of .py or .pyc, a > knowledgeable hacker will know it's probably python. > > You imply you want it to run unmodifed on multiple unknown > Linux versions. I think that lets out binfmt solutions. > That means you need to test and support not only multiple > Linux implementations, but multiple Python versions, because > who knows what the user may have installed. I think you need > to rethink your support strategy. And maybe concentrate on > being able to detect change, rather than prevent it. > > DaveA > > > From deets at nospam.web.de Thu Jul 30 11:16:44 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 17:16:44 +0200 Subject: Use existing IE cookie References: Message-ID: <7ddrqsF2b5mblU1@mid.uni-berlin.de> KB wrote: > Hi there, > > Relevant versions: Python 2.5, Vista Home, IE7 > > I am trying to scrape a website I have browsed manually in the past, > and also manually selected my options, and now want python to use my > existing cookie from the manual browse when downloading data. > > Using: http://code.activestate.com/recipes/80443/ I have found the > "name" of the relevant cookie, just after reading urllib2 docs, I > can't see how to "send" or have my python instance use "MY" existing > cookie. > > Using the following: > > *** > import re > import urllib2, cookielib > > # set things up for cookies > > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) > urllib2.install_opener(opener) > > reply = urllib2.urlopen('foo.html').read() > > print reply > > *** > > This does return data, just default data, not the data from the > options I set up when manually browsing. > > My sense is that I need "something" in the () part of > HTTPCookieProcessor() but I have no idea as to what... the docs say > "cookiejar" but the only code examples I have found are to create a > cookiejar for the existing Python session, not to use the cookies from > my prior manual meanderings. Because this is a completely different beast. You need to find out if and how to access IE-cookies from python - I guess some win32-road is to be walked down for that. Once you get a hold on them, you can build up whatever cookiejar urllib2 needs. Diez From invalid at invalid Thu Jul 30 11:16:46 2009 From: invalid at invalid (Grant Edwards) Date: Thu, 30 Jul 2009 10:16:46 -0500 Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <27mdnb-NxM3DJOzXnZ2dnUVZ_hxi4p2d@posted.visi> On 2009-07-30, Barak, Ron wrote: > Hi Dave, > > On second thoughts, I may have a problem implementing the > wrapper solution, because my actual test_pyc.pyc, needs to > parse its command line. Namely, the actual call to > test_pyc.pyc looks something like this: > > $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t tvn -p pool1 -l -c > > And I don't know of a way to add these parameters to the > "import test_pyc" in wrapper > > Is there a way to pass information to an imported module ? > (Sorry if there's an obvious answer, I just cannot figure it > out). I don't understand your problem. The module would parse sys.argv just like always. If you don't like that for some reason, you could define an entry point in the module and call it: #!/usr/bin/python import sys,test_pyc test_pyc.main(*sys.argv) -- Grant Edwards grante Yow! We just joined the at civil hair patrol! visi.com From keith at nekotaku.com Thu Jul 30 11:19:08 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 08:19:08 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> Message-ID: <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> > > Using:http://code.activestate.com/recipes/80443/ > Thanks for the prompt reply, Diez! Using the above I have found the name of the cookie (I did google how to use IE cookies in python and that was the best match) but it only tells me the name of the cookie, not how to use it. Any clues? TIA! From dkatkowski at gmail.com Thu Jul 30 11:25:58 2009 From: dkatkowski at gmail.com (Feyo) Date: Thu, 30 Jul 2009 08:25:58 -0700 (PDT) Subject: Help with Regex for domain names Message-ID: I'm trying to figure out how to write efficiently write a regex for domain names with a particular top level domain. Let's say, I want to grab all domain names with country codes .us, .au, and .de. I could create three different regexs that would work: regex = re.compile(r'[\w\-\.]+\.us) regex = re.compile(r'[\w\-\.]+\.au) regex = re.compile(r'[\w\-\.]+\.de) How would I write one to accommodate all three, or, better yet, to accommodate a list of them that I can pass into a method call? Thanks! From martin.hellwig at dcuktec.org Thu Jul 30 11:41:56 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 30 Jul 2009 16:41:56 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <87vdlate6o.fsf@benfinney.id.au> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <87vdlate6o.fsf@benfinney.id.au> Message-ID: Ben Finney wrote: > "Martin P. Hellwig" writes: > >> Machine Code: >> Whatever the machine executes, it could be that the CPU uses an >> abstraction of microcode to do this but from the perspective of the >> user, this is all done in the same 'black box' > > This requires, of course, defining what is the machine. Python bytecode > targets a virtual machine that is implemented differently for each > hardware platform. > I would further define 'black box' as the hardware a kernel programmer writes to. >> Interpreted: >> Instructions which can be fed to a previous compiled program that is >> able to dynamically change its execution and flow without the need to >> recompile itself. > > This doesn't make much sense to me, I must say. > > I'd say, instead, that a program is interpreted if its instruction are > dynamically translated to underlying platform instructions at execution > time. This is the case for the bytecode instructions interpreted by the > Python virtual machine. > Yes that is indeed a much better description, I'll steal that from you :-) -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From deets at nospam.web.de Thu Jul 30 11:43:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 17:43:31 +0200 Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> Message-ID: <7ddtd3F297bafU1@mid.uni-berlin.de> KB wrote: > >> > Using:http://code.activestate.com/recipes/80443/ >> > > Thanks for the prompt reply, Diez! Using the above I have found the > name of the cookie (I did google how to use IE cookies in python and > that was the best match) but it only tells me the name of the cookie, > not how to use it. Ah, sorry, should have read the recipe also. For me it looks as if findIECookie from that recipe is to be called with the name. Then it should return the value, or None What does you full example look like, including the cookie-acquisition-stuff? Diez From tundra at tundraware.com Thu Jul 30 11:47:06 2009 From: tundra at tundraware.com (Tim Daneliuk) Date: Thu, 30 Jul 2009 10:47:06 -0500 Subject: Help with Regex for domain names In-Reply-To: References: Message-ID: Feyo wrote: > I'm trying to figure out how to write efficiently write a regex for > domain names with a particular top level domain. Let's say, I want to > grab all domain names with country codes .us, .au, and .de. > > I could create three different regexs that would work: > regex = re.compile(r'[\w\-\.]+\.us) > regex = re.compile(r'[\w\-\.]+\.au) > regex = re.compile(r'[\w\-\.]+\.de) > > How would I write one to accommodate all three, or, better yet, to > accommodate a list of them that I can pass into a method call? Thanks! Just a point of interest: A correctly formed domain name may have a trailing period at the end of the TLD [1]. Example: foo.bar.com. Though you do not often see this, it's worth accommodating "just in case"... [1] http://homepages.tesco.net/J.deBoynePollard/FGA/web-fully-qualified-domain-name.html -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From python at mrabarnett.plus.com Thu Jul 30 11:50:37 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 16:50:37 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <4A719E2D.708@ieee.org> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <4A719E2D.708@ieee.org> Message-ID: <4A71C14D.7030805@mrabarnett.plus.com> Dave Angel wrote: [snip] > As far as I know, nobody has yet built a microcode implementation of a > Python VM (Virtual Machine). Nor have I seen one for the Java VM. > However, in the early 80's there was a microcode implementation of the > P-system VM. It was never a commercial success, but it existed. And > there have been at least three Forth machines, where the hardware itself > was designed to support the language's VM. No microcode at all. > There's Jazelle: http://en.wikipedia.org/wiki/Jazelle. From python at mrabarnett.plus.com Thu Jul 30 11:53:35 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 16:53:35 +0100 Subject: Does python have the capability for driver development ? In-Reply-To: <87vdlate6o.fsf@benfinney.id.au> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <87vdlate6o.fsf@benfinney.id.au> Message-ID: <4A71C1FF.2070702@mrabarnett.plus.com> Ben Finney wrote: > "Martin P. Hellwig" writes: > >> Machine Code: >> Whatever the machine executes, it could be that the CPU uses an >> abstraction of microcode to do this but from the perspective of the >> user, this is all done in the same 'black box' > > This requires, of course, defining what is the machine. Python bytecode > targets a virtual machine that is implemented differently for each > hardware platform. > >> Compiling: >> Translate words/symbols/mnemonics to machine code, which than can be >> either loaded, linked and executed by an OS or read and executed by >> the BIOS. > > Related to the above point, the ?machine code? can just as easily be > codes for a virtual machine specification. This is the case for the > bytecode instructions Python gets compiled to. > >> Interpreted: >> Instructions which can be fed to a previous compiled program that is >> able to dynamically change its execution and flow without the need to >> recompile itself. > > This doesn't make much sense to me, I must say. > > I'd say, instead, that a program is interpreted if its instruction are > dynamically translated to underlying platform instructions at execution > time. This is the case for the bytecode instructions interpreted by the > Python virtual machine. > Interpretation doesn't necessarily mean translating to machine code at execution time. What you're describing is more like JIT. From python at mrabarnett.plus.com Thu Jul 30 11:56:23 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 16:56:23 +0100 Subject: Help with Regex for domain names In-Reply-To: References: Message-ID: <4A71C2A7.90900@mrabarnett.plus.com> Feyo wrote: > I'm trying to figure out how to write efficiently write a regex for > domain names with a particular top level domain. Let's say, I want to > grab all domain names with country codes .us, .au, and .de. > > I could create three different regexs that would work: > regex = re.compile(r'[\w\-\.]+\.us) > regex = re.compile(r'[\w\-\.]+\.au) > regex = re.compile(r'[\w\-\.]+\.de) > > How would I write one to accommodate all three, or, better yet, to > accommodate a list of them that I can pass into a method call? Thanks! > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') If you have a list of country codes ["us", "au", "de"] then you can build the regular expression from it: regex = re.compile(r'[\w\-\.]+\.(?:%s)' % '|'.join(domains)) From keith at nekotaku.com Thu Jul 30 11:58:56 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 08:58:56 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> Message-ID: <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> > What does you full example look like, including the > cookie-acquisition-stuff? > > Diez I ran them seperately, hoping for a clue as to what my "cookiejar" was. The cookie-acquisition stuff returns "screener.ashx?v=151" when I search with my domain I am interested in. I have tried urllib2.HTTPCookieProcessor('screener.ashx?v=151') but that failed with attr has no cookie header. >From the HTTPCookieProcessor doco, it appears that non-IE browsers have a cookie file (and example code) but from what I can tell IE uses a hidden folder. (you can set your location in IE but it appends a folder "\Temporary Internet Files" - From: http://docs.python.org/dev/library/cookielib.html *** This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx cookies (assumes Unix/Netscape convention for location of the cookies file): import os, cookielib, urllib2 cj = cookielib.MozillaCookieJar() cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt")) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) r = opener.open("http://example.com/") *** Not sure how to adapt this for IE. From darkneter at gmail.com Thu Jul 30 12:06:11 2009 From: darkneter at gmail.com (NighterNet) Date: Thu, 30 Jul 2009 09:06:11 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: <15017646-6e20-40ff-af70-5717598f26bb@a39g2000pre.googlegroups.com> On Jul 30, 6:56?am, "Mark Tolonen" wrote: > "NighterNet" wrote in message > > news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com... > > >I am trying to figure out how to send text or byte in python3.1. I am > > trying to send data to flashsocketto get there. I am not sure how to > > work it. > > > buff= 'id=' , self.id , ':balive=False\n' > > clientSock.send(buff); > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Python3.1strings are Unicode (think characters not bytes). ?When writing > to files, sockets, etc. bytes must be used. ?Unicode strings have an > encode() method, where you can specify an appropriate encoding (ascii, > latin-1, windows-1252, utf-8, etc.): > > ? ? clientSock.send(buff.encode('ascii')) > > When reading from thesocket, you can decode() the byte strings back into > Unicode strings. > > ? ? data = clientSock.recv(1024).decode('ascii') > > -Mark I am not sure how to use struct package. Here an example for the input: {id:1,playername:guest,x:100,y:50} {id:2,playername:tester,x:100,y:50} struct.pack(? ) From cjw at ncf.ca Thu Jul 30 12:18:07 2009 From: cjw at ncf.ca (Colin J. Williams) Date: Thu, 30 Jul 2009 12:18:07 -0400 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: Some have treated this as a troll. I don't. r wrote: [snip] > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > Ruby --> "obj.m1.m2.m3" -- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. > +1 > 2.) the .each method > container.each{|localVar| block} > This method can really cleanup some ugly for loops, although i really > like the readability of for loops. > Not clear. > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). You know what i am talking about here people. We > all get complacent and It seems easier to just cope with these > problems instead of fighting for change. But look at the French, WHAT > THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! > +0.6 or better [1, 2, 3, 4, 5].len or [1, 2, 3, 4, 5].max What has this got to do with "true OOP"? [snip] From deets at nospam.web.de Thu Jul 30 12:23:59 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 30 Jul 2009 18:23:59 +0200 Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> Message-ID: <7ddvp0F2brmgrU1@mid.uni-berlin.de> KB wrote: > >> What does you full example look like, including the >> cookie-acquisition-stuff? >> >> Diez > > I ran them seperately, hoping for a clue as to what my "cookiejar" > was. > > The cookie-acquisition stuff returns "screener.ashx?v=151" when I > search with my domain I am interested in. I have tried > urllib2.HTTPCookieProcessor('screener.ashx?v=151') but that failed > with attr has no cookie header. > > From the HTTPCookieProcessor doco, it appears that non-IE browsers > have a cookie file (and example code) but from what I can tell IE uses > a hidden folder. (you can set your location in IE but it appends a > folder "\Temporary Internet Files" - > > From: http://docs.python.org/dev/library/cookielib.html > > *** > This example illustrates how to open a URL using your Netscape, > Mozilla, or Lynx cookies (assumes Unix/Netscape convention for > location of the cookies file): > > import os, cookielib, urllib2 > cj = cookielib.MozillaCookieJar() > cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt")) > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) > r = opener.open("http://example.com/") > *** > > Not sure how to adapt this for IE. You could create a file that resembles the cookies.txt - no idea how that looks, but I guess it's pretty simple. Diez From ethan at stoneleaf.us Thu Jul 30 12:27:32 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 30 Jul 2009 09:27:32 -0700 Subject: Does python have the capability for driver development ? In-Reply-To: <87vdlate6o.fsf@benfinney.id.au> References: <4a7151b2$0$17782$ba4acef3@news.orange.fr> <87vdlate6o.fsf@benfinney.id.au> Message-ID: <4A71C9F4.8040006@stoneleaf.us> Ben Finney wrote: > "Martin P. Hellwig" writes: > > >>Machine Code: >>Whatever the machine executes, it could be that the CPU uses an >>abstraction of microcode to do this but from the perspective of the >>user, this is all done in the same 'black box' > > > This requires, of course, defining what is the machine. Python bytecode > targets a virtual machine that is implemented differently for each > hardware platform. > > >>Compiling: >>Translate words/symbols/mnemonics to machine code, which than can be >>either loaded, linked and executed by an OS or read and executed by >>the BIOS. > > > Related to the above point, the ?machine code? can just as easily be > codes for a virtual machine specification. This is the case for the > bytecode instructions Python gets compiled to. > > >>Interpreted: >>Instructions which can be fed to a previous compiled program that is >>able to dynamically change its execution and flow without the need to >>recompile itself. > > > This doesn't make much sense to me, I must say. > > I'd say, instead, that a program is interpreted if its instruction are > dynamically translated to underlying platform instructions at execution > time. This is the case for the bytecode instructions interpreted by the > Python virtual machine. > I would have said compiled is executed by hardware, interpreted is executed by software -- but I like your definition better. :) ~Ethan~ From garrickp at gmail.com Thu Jul 30 12:31:22 2009 From: garrickp at gmail.com (Falcolas) Date: Thu, 30 Jul 2009 09:31:22 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> On Jul 29, 9:06?pm, r wrote: > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. I personally would not prefer this, and would likely continue to use (), precisely for code clarity - let me explain: foo.nextval foo.val foo.previousval Which of the calls above referenced instance variables, and which ones called functions which changed the internal state of foo? I would have trouble saying, just based on the calls above. I would have to go back to the definition or documentation of foo to identify which is doing what. On the other hand, the following gives a better clue as to what is happening (granted not perfect, but better): foo.nextval() foo.val foo.previousval() ~G From keith at nekotaku.com Thu Jul 30 12:31:33 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 09:31:33 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> <7ddvp0F2brmgrU1@mid.uni-berlin.de> Message-ID: <33b7685b-054a-442c-8e97-46a5b77f7900@z4g2000prh.googlegroups.com> On Jul 30, 9:23?am, "Diez B. Roggisch" wrote: > KB wrote: > > >> What does you full example look like, including the > >> cookie-acquisition-stuff? > > >> Diez > > > I ran them seperately, hoping for a clue as to what my "cookiejar" > > was. > > > The cookie-acquisition stuff returns "screener.ashx?v=151" when I > > search with my domain I am interested in. I have tried > > urllib2.HTTPCookieProcessor('screener.ashx?v=151') but that failed > > with attr has no cookie header. > > > From the HTTPCookieProcessor doco, it appears that non-IE browsers > > have a cookie file (and example code) but from what I can tell IE uses > > a hidden folder. (you can set your location in IE but it appends a > > folder "\Temporary Internet Files" ?- > > > From:http://docs.python.org/dev/library/cookielib.html > > > *** > > This example illustrates how to open a URL using your Netscape, > > Mozilla, or Lynx cookies (assumes Unix/Netscape convention for > > location of the cookies file): > > > import os, cookielib, urllib2 > > cj = cookielib.MozillaCookieJar() > > cj.load(os.path.join(os.environ["HOME"], ".netscape/cookies.txt")) > > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) > > r = opener.open("http://example.com/") > > *** > > > Not sure how to adapt this for IE. > > You could create a file that resembles the cookies.txt - no idea how that > looks, but I guess it's pretty simple. > > Diez- Hide quoted text - > > - Show quoted text - Yeah unfortunately I just tried Firefox and it uses cookies.sqlite now... more dead ends :) From edreamleo at charter.net Thu Jul 30 12:45:32 2009 From: edreamleo at charter.net (Edward K Ream) Date: Thu, 30 Jul 2009 11:45:32 -0500 Subject: ANN: Leo 4.6.1 final released Message-ID: Leo 4.6.1 final is now available at: http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106 Leo is a text editor, data organizer, project manager and much more. See: http://webpages.charter.net/edreamleo/intro.html Leo 4.6.1 fixes several minor bugs in Leo 4.6. The highlights of Leo 4.6 -------------------------- - Cached external files *greatly* reduces the time to load .leo files. - Leo now features a modern Qt interface by default. Leo's legacy Tk interface can also be used. - New --config, --file and --gui command-line options. - Leo tests syntax of .py files when saving them. - Leo can now open any kind of file into @edit nodes. - @auto-rst nodes allow easy editing of reStructuredText files. - Properties of commanders, positions and nodes simplify programming. - Improved Leo's unit testing framework. - Leo now requires Python 2.5 or later. - Dozens of small improvements and bug fixes. Links: ------ Leo: http://webpages.charter.net/edreamleo/front.html Forum: http://groups.google.com/group/leo-editor Download: http://sourceforge.net/project/showfiles.php?group_id=3458 Bzr: http://code.launchpad.net/leo-editor/ Quotes: http://webpages.charter.net/edreamleo/testimonials.html -------------------------------------------------------------------- Edward K. Ream email: edreamleo at yahoo.com Leo: http://webpages.charter.net/edreamleo/front.html -------------------------------------------------------------------- From inky788 at gmail.com Thu Jul 30 13:01:40 2009 From: inky788 at gmail.com (Inky 788) Date: Thu, 30 Jul 2009 10:01:40 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> On Jul 30, 12:04?am, alex23 wrote: > On Jul 30, 1:06?pm, r wrote: > > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > How do you distinguish between calling a method with no arguments, and > getting access to the method object itself (because it _is_ an object, > y'know, it's OO all the way down...)? I agree with alex here. Will take the explicit syntax over the extra cognitive load of figuring out exactly what's going on with `obj.m1.m2.m3`. Python has its warts, but requiring ()'s on function calls isn't one of them. :) From bieffe62 at gmail.com Thu Jul 30 13:03:42 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Thu, 30 Jul 2009 10:03:42 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> <15017646-6e20-40ff-af70-5717598f26bb@a39g2000pre.googlegroups.com> Message-ID: <3b14ad80-2e0e-4d4f-92e7-684dc75a7a05@w41g2000yqb.googlegroups.com> On 30 Lug, 18:06, NighterNet wrote: > On Jul 30, 6:56?am, "Mark Tolonen" wrote: > > > > > > > "NighterNet" wrote in message > > >news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com... > > > >I am trying to figure out how to send text or byte in python3.1. I am > > > trying to send data to flashsocketto get there. I am not sure how to > > > work it. > > > > buff= 'id=' , self.id , ':balive=False\n' > > > clientSock.send(buff); > > > -- > > >http://mail.python.org/mailman/listinfo/python-list > > > Python3.1strings are Unicode (think characters not bytes). ?When writing > > to files, sockets, etc. bytes must be used. ?Unicode strings have an > > encode() method, where you can specify an appropriate encoding (ascii, > > latin-1, windows-1252, utf-8, etc.): > > > ? ? clientSock.send(buff.encode('ascii')) > > > When reading from thesocket, you can decode() the byte strings back into > > Unicode strings. > > > ? ? data = clientSock.recv(1024).decode('ascii') > > > -Mark > > I am not sure how to use struct package. > Here an example for the input: > {id:1,playername:guest,x:100,y:50} > {id:2,playername:tester,x:100,y:50} > > struct.pack(? ) If your messages are ASCII, like it seems, forget about struct, which is for 'binary' message format. Format the string as you would in 2.6 ( using % or string.format for instance ) and then use encode as instructed. From davea at dejaviewphoto.com Thu Jul 30 13:07:43 2009 From: davea at dejaviewphoto.com (Dave Angel) Date: Thu, 30 Jul 2009 13:07:43 -0400 Subject: Run pyc file without specifying python path ? In-Reply-To: <7F0503CD69378F49BE0DC30661C6CCF6701A26D2@enbmail01.lsi.com> References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> <7F0503CD69378F49BE0DC30661C6CCF6701A26D2@enbmail01.lsi.com> Message-ID: <4A71D35F.3090800@dejaviewphoto.com> Barak, Ron wrote: > Hi Dave, > > On second thoughts, I may have a problem implementing the wrapper solution, because my actual test_pyc.pyc, needs to parse its command line. > Namely, the actual call to test_pyc.pyc looks something like this: > > $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t tvn -p pool1 -l -c > > And I don't know of a way to add these parameters to the "import test_pyc" in wrapper > > Is there a way to pass information to an imported module ? (Sorry if there's an obvious answer, I just cannot figure it out). > > Bye, > Ron. > > >> -----Original Message----- >> From: Dave Angel [mailto:davea at dejaviewphoto.com] >> Sent: Thursday, July 30, 2009 16:03 >> To: Barak, Ron >> Cc: 'Dave Angel'; 'python-list at python.org' >> Subject: RE: Run pyc file without specifying python path ? >> >> Barak, Ron wrote: >> >>>> -----Original Message----- >>>> From: Dave Angel [mailto:davea at ieee.org] >>>> Sent: Wednesday, July 29, 2009 21:05 >>>> To: Barak, Ron >>>> Cc: 'python-list at python.org' >>>> Subject: Re: Run pyc file without specifying python path ? >>>> >>>> Barak, Ron wrote: >>>> >>>> >>>>> Hi, >>>>> >>>>> I wanted to make a python byte-code file executable, >>>>> >>>>> >>>> expecting to be able to run it without specifying "python" on the >>>> (Linux bash) command line. >>>> >>>> >>>>> So, I wrote the following: >>>>> >>>>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python >>>>> >>>>> print "hello" >>>>> [root at VMLinux1 python]# >>>>> >>>>> and made its pyc file executable: >>>>> >>>>> [root at VMLinux1 python]# ls -ls test_pyc.pyc >>>>> 4 -rwxr-xr-x 1 root root 106 Jul 29 14:22 test_pyc.pyc >>>>> [root at VMLinux1 python]# >>>>> >>>>> So, I see: >>>>> >>>>> [root at VMLinux1 python]# file test_pyc.py* >>>>> test_pyc.py: a python script text executable >>>>> test_pyc.pyc: python 2.3 byte-compiled >>>>> [root at VMLinux1 python]# >>>>> >>>>> If I try to do the following, no problem: >>>>> >>>>> [root at VMLinux1 python]# python test_pyc.pyc hello >>>>> [root at VMLinux1 python]# >>>>> >>>>> However, the following fails: >>>>> >>>>> [root at VMLinux1 python]# ./test_pyc.pyc >>>>> -bash: ./test_pyc.pyc: cannot execute binary file >>>>> [root at VMLinux1 python]# >>>>> >>>>> Is there a way to run a pyc file without specifying the >>>>> >>>>> >>>> python path ? >>>> >>>> >>>>> Bye, >>>>> Ron. >>>>> >>>>> >>>>> >>>>> >>>> I don't currently run Unix, but I think I know the problem. >>>> >>>> In a text file, the shell examines the first line, and if >>>> >> it begins >> >>>> #! >>>> it's assumed to point to the executable of an interpreter for that >>>> text file. Presumably the same trick doesn't work for a .pyc file. >>>> >>>> Why not write a trivial wrapper.py file, don't compile it, and let >>>> that invoke the main code in the .pyc file? >>>> >>>> Then make wrapper.py executable, and you're ready to go. >>>> >>>> DaveA >>>> >>>> >>>> >>>> >>> Hi Dave, >>> Your solution sort of defeats my intended purpose (sorry >>> >> for not divulging my 'hidden agenda'). >> >>> I wanted my application to "hide" the fact that it's a >>> >> python script, and look as much as possible like it's a >> compiled program. >> >>> The reason I don't just give my user a py file, is that I >>> >> don't want a cleaver user to change the innards of the script. >> >>> On the other hand, I don't want to make a compiled >>> >> (freezed?) version of the application, because it'll grow the >> resulting file significantly, and I don't have the experience >> to know how it will run on different Linuxes. >> >>> Bye, >>> Ron. >>> >>> >> Most of the other answers basically paraphrased my suggestion >> of making a wrapper file, not compiling it, and making it >> executable. With that >> approach, the user just types "wrapper.py" on his command line. >> >> And wrapper.py only needs two lines, a shebang, and an >> import, no big deal if the user modifies it. The rest of >> your code can be .pyc files. >> >> Steven makes some good points. You have to define what level >> of clever you're protecting from. A determined hacker will >> get in no matter what you do, unless you want to ship the >> program in a proprietary embedded system, encased in epoxy. >> Further, if you have an extension of .py or .pyc, a >> knowledgeable hacker will know it's probably python. >> >> You imply you want it to run unmodifed on multiple unknown >> Linux versions. I think that lets out binfmt solutions. >> That means you need to test and support not only multiple >> Linux implementations, but multiple Python versions, because >> who knows what the user may have installed. I think you need >> to rethink your support strategy. And maybe concentrate on >> being able to detect change, rather than prevent it. >> >> DaveA >> >> >> >> (Please don't top-post. It puts responses out of order) You don't have to do anything special. Any module can import sys, and parse sys.argv, as long as it wasn't yet modified. DaveA From rt8396 at gmail.com Thu Jul 30 13:09:02 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:09:02 -0700 (PDT) Subject: Confessions of a Python fanboy References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: On Jul 30, 11:31?am, Falcolas wrote: > On Jul 29, 9:06?pm, r wrote: > > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > I personally would not prefer this, and would likely continue to use > (), precisely for code clarity - let me explain: > > foo.nextval > foo.val > foo.previousval > > Which of the calls above referenced instance variables, and which ones > called functions which changed the internal state of foo? I would have > trouble saying, just based on the calls above. I would have to go back > to the definition or documentation of foo to identify which is doing > what. On the other hand, the following gives a better clue as to what > is happening (granted not perfect, but better): > > foo.nextval() > foo.val > foo.previousval() > > ~G I held your exact same view before i learned the Ruby language. And your veiw makes some good points, however, naming conventions with eliminate this problem all together. All "method names" should use the underscore to separate words, "variable names" should use camelCase, "constants" in all caps, and "class defs" in titlecase. def go_and_do_this_for_me_now(self, *args) self.variableNameHere MyClassNameHere THISISACONSTANT -or- THIS_IS_A_CONSTANT in your example i would have used the following foo.next_value foo.value foo.prev_value good naming conventions will make your life (and everybody else's) much easier when debugging code. From masklinn at masklinn.net Thu Jul 30 13:10:35 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:10:35 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: On 30 Jul 2009, at 18:31 , Falcolas wrote: > On Jul 29, 9:06 pm, r wrote: > >> 1.) No need to use "()" to call a function with no arguments. >> Python --> "obj.m2().m3()" --ugly >> Ruby --> "obj.m1.m2.m3" -- sweeet! >> Man, i must admit i really like this, and your code will look so much >> cleaner. > > I personally would not prefer this, and would likely continue to use > (), precisely for code clarity - let me explain: > > foo.nextval > foo.val > foo.previousval > > Which of the calls above referenced instance variables Well, that's very simple: none of them. In Ruby (as in Smalltalk), public instance variables simply don't exist. > and which ones called functions which changed the internal state of > foo? That you can't say, but neither can you say in Python as they could all be properties. And of course just because it's a method doesn't mean it mutates the object. > I would have > trouble saying, just based on the calls above. I would have to go back > to the definition or documentation of foo to identify which is doing > what. On the other hand, the following gives a better clue as to what > is happening (granted not perfect, but better): Well... it doesn't give much of a clue no really. From masklinn at masklinn.net Thu Jul 30 13:15:14 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:15:14 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> Message-ID: On 30 Jul 2009, at 19:01 , Inky 788 wrote: > On Jul 30, 12:04 am, alex23 wrote: >> On Jul 30, 1:06 pm, r wrote: >> >>> 1.) No need to use "()" to call a function with no arguments. >>> Python --> "obj.m2().m3()" --ugly >>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>> Man, i must admit i really like this, and your code will look so >>> much >>> cleaner. >> >> How do you distinguish between calling a method with no arguments, >> and >> getting access to the method object itself (because it _is_ an >> object, >> y'know, it's OO all the way down...)? > > I agree with alex here. Will take the explicit syntax over the extra > cognitive load of figuring out exactly what's going on with > `obj.m1.m2.m3`. There's no cognitive load whatsoever: it's calling methods. Always. Ruby simply gives you no other option. Now it could be very simple methods to instance attributes, akin to a java getter, but it's still only methods. Furthermore Ruby has a pretty nice convention (sadly not used enough I think) taken from Scheme where it's possible to postfix a method name with "!" (note: the "!" is part of the name, there's no magic) to indicate that this method modifies the object it's called on rather than simply returning stuff. From keith at nekotaku.com Thu Jul 30 13:16:45 2009 From: keith at nekotaku.com (KB) Date: Thu, 30 Jul 2009 10:16:45 -0700 (PDT) Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> <7ddvp0F2brmgrU1@mid.uni-berlin.de> <33b7685b-054a-442c-8e97-46a5b77f7900@z4g2000prh.googlegroups.com> Message-ID: Winner, winner, chicken dinner... resolved. http://wwwsearch.sourceforge.net/mechanize/doc.html import mechanize cj = mechanize.MSIECookieJar(delayload=True) cj.load_from_registry() # finds cookie index file from registry Thanks Mr Lee! From mhanbali at teksystems.com Thu Jul 30 13:22:20 2009 From: mhanbali at teksystems.com (Mehdi H) Date: Thu, 30 Jul 2009 10:22:20 -0700 (PDT) Subject: NASA using Python / Django for NEBULA (cloud comp.) Message-ID: <57859e1d-ff69-49fc-b758-1331912b4d0e@v36g2000yqv.googlegroups.com> Hello Python devs! I'm supporting NASA on their NEBULA project and they're looking for Python / Django experts to join their team here in Mountain View, CA. If you're interested, let me know and we can talk more on how they're using Django in their environment! Thanks, Mehdi From robert.kern at gmail.com Thu Jul 30 13:24:52 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 30 Jul 2009 12:24:52 -0500 Subject: Pyplot In-Reply-To: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> References: <8fa7fe790907300202s66bde801k254c3fab10515f50@mail.gmail.com> Message-ID: On 2009-07-30 04:02, Kaan AK??T wrote: > I am a newbee in Python. I have some problem with usage of Pyplot. I > have a loop that creates plot in every end of it. Problem starts here: > The first plot is fine but the second one is plotted onto the first > plot. I use: > > plt.cla() > plt.draw() > plt.hold(False) > plt.close() > plt.clf() > > but nothing works. Perhaps I am having a problem with basics, please > help me if you know something about it. Create a new window by using figure(). You will want to ask matplotlib questions on the matplotlib mailing list: https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- 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 dkatkowski at gmail.com Thu Jul 30 13:25:58 2009 From: dkatkowski at gmail.com (Feyo) Date: Thu, 30 Jul 2009 10:25:58 -0700 (PDT) Subject: Help with Regex for domain names References: Message-ID: <78504224-b114-45f3-a302-95fbd8393443@o6g2000yqj.googlegroups.com> On Jul 30, 11:56?am, MRAB wrote: > Feyo wrote: > > I'm trying to figure out how to write efficiently write a regex for > > domain names with a particular top level domain. Let's say, I want to > > grab all domain names with country codes .us, .au, and .de. > > > I could create three different regexs that would work: > > regex = re.compile(r'[\w\-\.]+\.us) > > regex = re.compile(r'[\w\-\.]+\.au) > > regex = re.compile(r'[\w\-\.]+\.de) > > > How would I write one to accommodate all three, or, better yet, to > > accommodate a list of them that I can pass into a method call? Thanks! > > ?> > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') > > If you have a list of country codes ["us", "au", "de"] then you can > build the regular expression from it: > > regex = re.compile(r'[\w\-\.]+\.(?:%s)' % '|'.join(domains)) Perfect! Thanks. From rt8396 at gmail.com Thu Jul 30 13:28:44 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:28:44 -0700 (PDT) Subject: Confessions of a Python fanboy References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> Message-ID: <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> On Jul 30, 12:15?pm, Masklinn wrote: [snip] > Furthermore Ruby has a pretty nice convention (sadly not used enough I ? > think) taken from Scheme where it's possible to postfix a method name ? > with "!" (note: the "!" is part of the name, there's no magic) to ? > indicate that this method modifies the object it's called on rather ? > than simply returning stuff. Another oddity i did not like at first but must admit is growing on me vector.reverse --> returns a new reversed vector vector.reverse! --> modifies the instance vector in-place Of course in python you would do... vector.reverse --> in-place vector.reversed --> in-place The above example works pretty good, but this doesn't always "sound" good. Take for example this... point3d.offset --> return a new pt point3d.offseted --> What did you say!?!?! Now try Ruby's way point3d.offset! point3d.offset a symbol works better for these things From rurpy at yahoo.com Thu Jul 30 13:29:09 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Thu, 30 Jul 2009 10:29:09 -0700 (PDT) Subject: Help with Regex for domain names References: Message-ID: <0422bf8f-ce59-4de2-a1a4-b16a32e5f152@o15g2000yqm.googlegroups.com> On Jul 30, 9:56 am, MRAB wrote: > Feyo wrote: > > I'm trying to figure out how to write efficiently write a regex for > > domain names with a particular top level domain. Let's say, I want to > > grab all domain names with country codes .us, .au, and .de. > > > I could create three different regexs that would work: > > regex = re.compile(r'[\w\-\.]+\.us) > > regex = re.compile(r'[\w\-\.]+\.au) > > regex = re.compile(r'[\w\-\.]+\.de) > > > How would I write one to accommodate all three, or, better yet, to > > accommodate a list of them that I can pass into a method call? Thanks! > > > > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') You might also want to consider that some country codes such as "co" for Columbia might match more than you want, for example: re.match(r'[\w\-\.]+\.(?:us|au|de|co)', 'foo.boo.com') will match. From torriem at gmail.com Thu Jul 30 13:30:03 2009 From: torriem at gmail.com (Michael Torrie) Date: Thu, 30 Jul 2009 11:30:03 -0600 Subject: Does python have the capability for driver development ? In-Reply-To: <94abbddc-dac7-4215-84d1-d4eb3f82724d@h21g2000yqa.googlegroups.com> References: <87skgf83ua.fsf@rsw.digi.com.br> <94abbddc-dac7-4215-84d1-d4eb3f82724d@h21g2000yqa.googlegroups.com> Message-ID: <4A71D89B.6030301@gmail.com> MalC0de wrote: > please introduce me some resource for system programming, I know that > python is very well at system programming level. > thanks Although it is possible (as others have said) to embed Python the interpreter into a driver, no one has done that that I know of. You'd have to write the driver in C or C++, though, and then provide embed a python interpreter and then provide a python interface (wrapper code written in C++) that would expose binary primitives and structures to python that the OS and driver use. Sounds like quite an undertaking. I embedded a python interpreter in a gina dll once (to write login code in python). That's a form of system programming, but not what you're asking about. Someone years ago someone embedded the perl interpreter into a Linux driver allowing some kinds of device drivers to be written in perl. It was made more as a curiosity though. Python is not well-suited to system programming. It takes extra work (via the ctypes library or other wrappers) to interface with C structs, plus calls all have to be marshalled to and from the native C APIs. Might be cool, though. System programming in Win32 is very complicated and messy. What little system programming I've done in Linux was a dream compared. Definitely you'll have to master win32 system programming in C and C++ before you try to embed python in anything. I'm sure there are a number of books on the subject. That's where I'd start. From rt8396 at gmail.com Thu Jul 30 13:33:38 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:33:38 -0700 (PDT) Subject: Confessions of a Python fanboy References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: Traceback (most recent post last): File "", lines (13,14), in vector.reverse --> in-place vector.reversed --> in-place DumbMistakeError: Of course in python you would do... vector.reverse --> in-place vector.reversed --> new vector From jeanmichel at sequans.com Thu Jul 30 13:37:00 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 30 Jul 2009 19:37:00 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: <4A71DA3C.7060502@sequans.com> r wrote: > On Jul 30, 11:31 am, Falcolas wrote: > >> On Jul 29, 9:06 pm, r wrote: >> >> >>> 1.) No need to use "()" to call a function with no arguments. >>> Python --> "obj.m2().m3()" --ugly >>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>> Man, i must admit i really like this, and your code will look so much >>> cleaner. >>> >> I personally would not prefer this, and would likely continue to use >> (), precisely for code clarity - let me explain: >> >> foo.nextval >> foo.val >> foo.previousval >> >> Which of the calls above referenced instance variables, and which ones >> called functions which changed the internal state of foo? I would have >> trouble saying, just based on the calls above. I would have to go back >> to the definition or documentation of foo to identify which is doing >> what. On the other hand, the following gives a better clue as to what >> is happening (granted not perfect, but better): >> >> foo.nextval() >> foo.val >> foo.previousval() >> >> ~G >> > > I held your exact same view before i learned the Ruby language. And > your veiw makes some good points, however, naming conventions with > eliminate this problem all together. All "method names" should use the > underscore to separate words, "variable names" should use camelCase, > "constants" in all caps, and "class defs" in titlecase. > > def go_and_do_this_for_me_now(self, *args) > self.variableNameHere > MyClassNameHere > THISISACONSTANT -or- THIS_IS_A_CONSTANT > > > in your example i would have used the following > > foo.next_value > foo.value > foo.prev_value > > good naming conventions will make your life (and everybody else's) > much easier when debugging code. > > > > How do I know if foo.value is an attribute or if it is a method that returns the foo value ? It seems you can make the difference only when tokens are composed of more than one word, not very handy don't you think ? Just feeding the troll. JM From carsten.haese at gmail.com Thu Jul 30 13:42:30 2009 From: carsten.haese at gmail.com (Carsten Haese) Date: Thu, 30 Jul 2009 13:42:30 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: r wrote: > Of course in python you would do... > vector.reverse --> in-place > vector.reversed --> in-place You do know that only one of those works in-place, right? > The above example works pretty good, but this doesn't always "sound" > good. Take for example this... > point3d.offset --> return a new pt > point3d.offseted --> What did you say!?!?! > > Now try Ruby's way > point3d.offset! > point3d.offset > > a symbol works better for these things Or you could use a better verb: point3d.move -> modifies in place point3d.moved -> returns a new point -Carsten From rt8396 at gmail.com Thu Jul 30 13:45:49 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 10:45:49 -0700 (PDT) Subject: Confessions of a Python fanboy References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> Message-ID: <91508ea6-a9dd-4d80-81e6-c056c26aae5c@b15g2000yqd.googlegroups.com> On Jul 30, 12:37?pm, Jean-Michel Pichavant wrote: > r wrote: > > On Jul 30, 11:31 am, Falcolas wrote: > > >> On Jul 29, 9:06 pm, r wrote: > > >>> 1.) No need to use "()" to call a function with no arguments. > >>> Python --> "obj.m2().m3()" --ugly > >>> ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > >>> Man, i must admit i really like this, and your code will look so much > >>> cleaner. > > >> I personally would not prefer this, and would likely continue to use > >> (), precisely for code clarity - let me explain: > > >> foo.nextval > >> foo.val > >> foo.previousval > > >> Which of the calls above referenced instance variables, and which ones > >> called functions which changed the internal state of foo? I would have > >> trouble saying, just based on the calls above. I would have to go back > >> to the definition or documentation of foo to identify which is doing > >> what. On the other hand, the following gives a better clue as to what > >> is happening (granted not perfect, but better): > > >> foo.nextval() > >> foo.val > >> foo.previousval() > > >> ~G > > > I held your exact same view before i learned the Ruby language. And > > your veiw makes some good points, however, naming conventions with > > eliminate this problem all together. All "method names" should use the > > underscore to separate words, "variable names" should use camelCase, > > "constants" in all caps, and "class defs" in titlecase. > > > def go_and_do_this_for_me_now(self, *args) > > self.variableNameHere > > MyClassNameHere > > THISISACONSTANT -or- THIS_IS_A_CONSTANT > > > in your example i would have used the following > > > foo.next_value > > foo.value > > foo.prev_value > > > good naming conventions will make your life (and everybody else's) > > much easier when debugging code. > > How do I know if foo.value is an attribute or if it is a method that > returns the foo value ? It seems you can make the difference only when > tokens are composed of more than one word, not very handy don't you think ? > > Just feeding the troll. > > JM You can still use empty parenthesis in Ruby if that floats your boat. obj.method() <-- note the empty parenthesis I am just saying don't make them mandatory, thats all. In any case where ambiguity abounds, by all means use the damn parenthisis. Now Ruby will even let you call a method *that has arguments* with no parenthisis -- however, i do not think this is a good idea. Consider the following Geom::Transformation.rotation pt, vec, ang bad coder!, bad! ...and leads to worn out eyeball parsers in a hurry Geom::Transformation.rotation(pt, vec, ang) yes, good boy! From kyosohma at gmail.com Thu Jul 30 13:45:57 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Thu, 30 Jul 2009 10:45:57 -0700 (PDT) Subject: Bug in IEHtmlWindow? References: <8a5ed753-d437-45a6-8fee-300eea1207ca@c2g2000yqi.googlegroups.com> Message-ID: On Jul 30, 2:54?am, Massi wrote: > Hi everyone, I'm trying to use IEHtmlWindow in my application (python > 2.5, wxpython 2.8 on win xp). Everything is ok, but I encountered a > problem which also affects the demo. If I make a research on google a > strange message appears, saying (I'll try to translate from italian): > > Error during execution. > Do you want to run the debug? > > line 29: > error: 'r' is NULL or it is not an object > > Did anybody have the same problem? is it a bug? > Thanks in advance for your help. I've never seen that error. Could you re-post to the wxPython user's list? They can probably help you there: http://groups.google.com/group/wxPython Mike From digitig at gmail.com Thu Jul 30 13:47:04 2009 From: digitig at gmail.com (Tim Rowe) Date: Thu, 30 Jul 2009 18:47:04 +0100 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: 2009/7/30 r : > > Like your > first lay, your first programing language can leave an indelible mark > on you That's true. FOCAL scarred me for life. > but i now realize Ruby has some good > things going for it. Any language that gets any sort of real use has to have. For instance, I love Ada's numeric types (you can specify either the minimum number of significant figures or the maximum delta for a real type, and it will give you a type that satisfies that -- or a compilation error if it can't. That matches genuine problem domains far better than having to remember how many bits in a double on this particular system, and reduces portability bugs). > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). You know what i am talking about here people. We > all get complacent and It seems easier to just cope with these > problems instead of fighting for change. But look at the French, ?WHAT > THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! I seem to recall recent studies showing that the French were on average happier than Brits or Americans. Don't knock it! > As for the rest of Ruby, i am not impressed. The redundant usage of > "end" over indention perplexes me. The Perlish feel of "require" and > the horrifically cryptic idioms of Ruby regular expressions. The > "puts" and "gets" seem childish and the math class does not even have > a degrees or radians function! The operating system dependency built into the language did it for me. That and the fact that I couldn't stop laughing for long enough to learn any more when I read in the Pragmatic Programmer's Guide that "Ruby, unlike less flexible languages, lets you alter the value of a constant." Yep, as they say "Bug" = "Undocumented feature"! -- Tim Rowe From masklinn at masklinn.net Thu Jul 30 13:56:07 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:56:07 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4A71DA3C.7060502@sequans.com> References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> <4A71DA3C.7060502@sequans.com> Message-ID: On 30 Jul 2009, at 19:37 , Jean-Michel Pichavant wrote: > r wrote: >> On Jul 30, 11:31 am, Falcolas wrote: >> >>> On Jul 29, 9:06 pm, r wrote: >>> >>> >>>> 1.) No need to use "()" to call a function with no arguments. >>>> Python --> "obj.m2().m3()" --ugly >>>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>>> Man, i must admit i really like this, and your code will look so >>>> much >>>> cleaner. >>>> >>> I personally would not prefer this, and would likely continue to use >>> (), precisely for code clarity - let me explain: >>> >>> foo.nextval >>> foo.val >>> foo.previousval >>> >>> Which of the calls above referenced instance variables, and which >>> ones >>> called functions which changed the internal state of foo? I would >>> have >>> trouble saying, just based on the calls above. I would have to go >>> back >>> to the definition or documentation of foo to identify which is doing >>> what. On the other hand, the following gives a better clue as to >>> what >>> is happening (granted not perfect, but better): >>> >>> foo.nextval() >>> foo.val >>> foo.previousval() >>> >>> ~G >>> >> >> I held your exact same view before i learned the Ruby language. And >> your veiw makes some good points, however, naming conventions with >> eliminate this problem all together. All "method names" should use >> the >> underscore to separate words, "variable names" should use camelCase, >> "constants" in all caps, and "class defs" in titlecase. >> >> def go_and_do_this_for_me_now(self, *args) >> self.variableNameHere >> MyClassNameHere >> THISISACONSTANT -or- THIS_IS_A_CONSTANT >> >> >> in your example i would have used the following >> >> foo.next_value >> foo.value >> foo.prev_value >> >> good naming conventions will make your life (and everybody else's) >> much easier when debugging code. >> > How do I know if foo.value is an attribute or if it is a method that > returns the foo value ? It cannot be an attribute. Ruby doesn't give access to attributes, they're always solely private (as in Smalltalk). Also you shouldn't reply to r, he has no idea about what he's talking about. From masklinn at masklinn.net Thu Jul 30 13:57:54 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 19:57:54 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: <92229A0F-EC80-43C7-97B8-8C3636581EDE@masklinn.net> On 30 Jul 2009, at 19:42 , Carsten Haese wrote: > r wrote: >> Of course in python you would do... >> vector.reverse --> in-place >> vector.reversed --> in-place > > You do know that only one of those works in-place, right? > Well mostly because the other one doesn't exist (as python has `lst.reverse()` but `reversed(lst)`) but he was probably talking hypothetically. Or something. From user at example.net Thu Jul 30 14:05:53 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 20:05:53 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> Message-ID: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> r wrote: > On Jul 30, 12:15 pm, Masklinn wrote: > [snip] > >>Furthermore Ruby has a pretty nice convention (sadly not used enough I >>think) taken from Scheme where it's possible to postfix a method name >>with "!" (note: the "!" is part of the name, there's no magic) to >>indicate that this method modifies the object it's called on rather >>than simply returning stuff. > > > Another oddity i did not like at first but must admit is growing on me > vector.reverse --> returns a new reversed vector > vector.reverse! --> modifies the instance vector in-place > > Of course in python you would do... > vector.reverse --> in-place > vector.reversed --> in-place > how to reverse a string in python? must i usa a temp? like: >>> s = "ciccio" >>> l = list(s) >>> l.reverse() >>> s = "".join(l) >>> s 'oiccic' >>> ??? bye From garrickp at gmail.com Thu Jul 30 14:06:33 2009 From: garrickp at gmail.com (Falcolas) Date: Thu, 30 Jul 2009 11:06:33 -0700 (PDT) Subject: Confessions of a Python fanboy References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> <4A71DA3C.7060502@sequans.com> Message-ID: <776cafad-cfe8-442d-8d40-cab4e5c71d9b@c14g2000yqm.googlegroups.com> On Jul 30, 11:56?am, Masklinn wrote: > On 30 Jul 2009, at 19:37 , Jean-Michel Pichavant wrote: > > > r wrote: > > How do I know if foo.value is an attribute or if it is a method that ? > > returns the foo value ? > > It cannot be an attribute. Ruby doesn't give access to attributes, ? > they're always solely private (as in Smalltalk). Also you shouldn't ? > reply to r, he has no idea about what he's talking about. That doesn't sound like a feature I would appreciate much, though I am a big fan of "we're all adults here" programming. Though if they are all functions, then adding parentheses wouldn't make much difference, so offering the option of getting rid of them makes sense. >> I would have >> trouble saying, just based on the calls above. I would have to go back >> to the definition or documentation of foo to identify which is doing >> what. On the other hand, the following gives a better clue as to what >> is happening (granted not perfect, but better): > Well... it doesn't give much of a clue no really. It, with few exceptions, tells me that fetching the value foo.val will not modify the state of foo. Important stuff, at least to me. From user at example.net Thu Jul 30 14:08:36 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 20:08:36 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <4a71e1a5$0$40016$4fafbaef@reader3.news.tin.it> Tim Rowe wrote: > 2009/7/30 r : > >>Like your >>first lay, your first programing language can leave an indelible mark >>on you > > > That's true. FOCAL scarred me for life. > > >>but i now realize Ruby has some good >>things going for it. > > > Any language that gets any sort of real use has to have. For instance, > I love Ada's numeric types (you can specify either the minimum number > of significant figures or the maximum delta for a real type, and it > will give you a type that satisfies that -- or a compilation error if > it can't. That matches genuine problem domains far better doesn't the decimal module do some of that too? bye From darkneter at gmail.com Thu Jul 30 14:13:44 2009 From: darkneter at gmail.com (NighterNet) Date: Thu, 30 Jul 2009 11:13:44 -0700 (PDT) Subject: class or thread id count Message-ID: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> Need some help on doing some bit simple id count. It like every time it create a class or thread there is id++. Python 3.1 example: class ClientThread (threading.Thread): def __init__(self,clientSocket): threading.Thread.__init__(self) self.id += 1; Not sure it how it works. From python at mrabarnett.plus.com Thu Jul 30 14:20:11 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 19:20:11 +0100 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4A71E45B.3070009@mrabarnett.plus.com> superpollo wrote: > r wrote: >> On Jul 30, 12:15 pm, Masklinn wrote: >> [snip] >> >>> Furthermore Ruby has a pretty nice convention (sadly not used enough >>> I think) taken from Scheme where it's possible to postfix a method >>> name with "!" (note: the "!" is part of the name, there's no magic) >>> to indicate that this method modifies the object it's called on >>> rather than simply returning stuff. >> >> >> Another oddity i did not like at first but must admit is growing on me >> vector.reverse --> returns a new reversed vector >> vector.reverse! --> modifies the instance vector in-place >> >> Of course in python you would do... >> vector.reverse --> in-place >> vector.reversed --> in-place >> > > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? > Use slicing with a step of -1: >>> s = "ciccio" >>> s[::-1] 'oiccic' From masklinn at masklinn.net Thu Jul 30 14:22:21 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 20:22:21 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <772E43D2-61B0-4C26-AAA3-C1E9785176AF@masklinn.net> On 30 Jul 2009, at 20:05 , superpollo wrote: > r wrote: >> On Jul 30, 12:15 pm, Masklinn wrote: >> [snip] >>> Furthermore Ruby has a pretty nice convention (sadly not used >>> enough I think) taken from Scheme where it's possible to postfix >>> a method name with "!" (note: the "!" is part of the name, >>> there's no magic) to indicate that this method modifies the >>> object it's called on rather than simply returning stuff. >> Another oddity i did not like at first but must admit is growing on >> me >> vector.reverse --> returns a new reversed vector >> vector.reverse! --> modifies the instance vector in-place >> Of course in python you would do... >> vector.reverse --> in-place >> vector.reversed --> in-place > > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? > > bye s = s[::-1] From masklinn at masklinn.net Thu Jul 30 14:24:23 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 20:24:23 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <776cafad-cfe8-442d-8d40-cab4e5c71d9b@c14g2000yqm.googlegroups.com> References: <56bb9325-babc-478b-a98c-03d00c48ac5d@24g2000yqm.googlegroups.com> <4A71DA3C.7060502@sequans.com> <776cafad-cfe8-442d-8d40-cab4e5c71d9b@c14g2000yqm.googlegroups.com> Message-ID: On 30 Jul 2009, at 20:06 , Falcolas wrote: > On Jul 30, 11:56 am, Masklinn wrote: >> On 30 Jul 2009, at 19:37 , Jean-Michel Pichavant wrote: >> >>> r wrote: > >>> How do I know if foo.value is an attribute or if it is a method that >>> returns the foo value ? >> >> It cannot be an attribute. Ruby doesn't give access to attributes, >> they're always solely private (as in Smalltalk). Also you shouldn't >> reply to r, he has no idea about what he's talking about. > > That doesn't sound like a feature I would appreciate much, though I am > a big fan of "we're all adults here" programming. Though if they are > all functions, then adding parentheses wouldn't make much difference, > so offering the option of getting rid of them makes sense. > That's pretty much it. >>> I would have >>> trouble saying, just based on the calls above. I would have to go >>> back >>> to the definition or documentation of foo to identify which is doing >>> what. On the other hand, the following gives a better clue as to >>> what >>> is happening (granted not perfect, but better): > >> Well... it doesn't give much of a clue no really. > > It, with few exceptions, tells me that fetching the value foo.val will > not modify the state of foo. Important stuff, at least to me. But because of those few exceptions, it really tells you that foo.val *probably* doesn't modify the state of foo. Unless you go source- diving, you can't know for sure. Only Haskell gives you that kind of guarantees (and even then, the code you call could have an unsafe* somewhere) From davea at ieee.org Thu Jul 30 14:28:11 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Jul 2009 14:28:11 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4A71E63B.6080200@ieee.org> superpollo wrote: >> > > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? > > bye > > >
> simply s = s[::-1] DaveA From user at example.net Thu Jul 30 14:30:45 2009 From: user at example.net (superpollo) Date: Thu, 30 Jul 2009 20:30:45 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <4a71e6d6$0$40004$4fafbaef@reader3.news.tin.it> MRAB wrote: > superpollo wrote: ... >> how to reverse a string in python? must i usa a temp? like: >> >> >>> s = "ciccio" >> >>> l = list(s) >> >>> l.reverse() >> >>> s = "".join(l) >> >>> s >> 'oiccic' >> >>> >> >> ??? >> > Use slicing with a step of -1: > > >>> s = "ciccio" > >>> s[::-1] > 'oiccic' lol bye From hniksic at xemacs.org Thu Jul 30 14:32:10 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 30 Jul 2009 20:32:10 +0200 Subject: New implementation of re module References: <4A6DD6FB.8040609@mrabarnett.plus.com> <200907301235.22563.wolfgang@rohdewald.de> <4A717E96.3020900@mrabarnett.plus.com> <200907301429.07850.wolfgang@rohdewald.de> Message-ID: <87y6q6dnth.fsf@busola.homelinux.net> MRAB writes: > So it complains about: > > ++(RE_CHAR*)context->text_ptr > > but not about: > > ++info->repeat.count > > Does this mean that the gcc compiler thinks that the cast makes it an > rvalue? The cast operator does return an rvalue, treating it otherwise used to be an extension to popular compilers, including ironically gcc. The standard-compliant way of writing the above would be: ++ *(RE_CHAR **) &context->text_ptr From wells at submute.net Thu Jul 30 14:45:53 2009 From: wells at submute.net (Wells Oliver) Date: Thu, 30 Jul 2009 13:45:53 -0500 Subject: Sorting dict by value w/ operator.itemgetter- using key name? Message-ID: <3f1a902d0907301145t2aa559b7ifcb3dc45d9b7a363@mail.gmail.com> Bit of code: print sorted(results.items(), key=operator.itemgetter(1)) Would rather use 'H9', which is the name of the key in position 1 like: print sorted(results.items(), key=operator.itemgetter('H9')) Obviously that ain't work else I wouldn't be sending this email. Any tips? -- Wells Oliver wells at submute.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From nobody at nowhere.com Thu Jul 30 14:51:23 2009 From: nobody at nowhere.com (Nobody) Date: Thu, 30 Jul 2009 19:51:23 +0100 Subject: Help with Regex for domain names References: <0422bf8f-ce59-4de2-a1a4-b16a32e5f152@o15g2000yqm.googlegroups.com> Message-ID: On Thu, 30 Jul 2009 10:29:09 -0700, rurpy wrote: >> regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') > > You might also want to consider that some country > codes such as "co" for Columbia might match more than > you want, for example: > > re.match(r'[\w\-\.]+\.(?:us|au|de|co)', 'foo.boo.com') > > will match. ... so put \b at the end, i.e.: regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)\b') From sri_annauni at yahoo.co.in Thu Jul 30 15:04:09 2009 From: sri_annauni at yahoo.co.in (srinivasan srinivas) Date: Fri, 31 Jul 2009 00:34:09 +0530 (IST) Subject: calling obect method Message-ID: <960855.56660.qm@web7906.mail.in.yahoo.com> Hi, I would like to know if i have an object and one of its methods as a string. How to call the method using this string? For ex If object is x and string y has one of its method names. x.value(y)() ---. Is there a way to do this? Thanks, Srini See the Web's breaking stories, chosen by people like you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/ From rt8396 at gmail.com Thu Jul 30 15:08:25 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 12:08:25 -0700 (PDT) Subject: calling obect method References: Message-ID: <2e8c5c91-be27-430b-9cad-46a5bff3ee37@26g2000yqk.googlegroups.com> >>> import math >>> getattr(math, 'hypot')(3,4) 5.0 From rt8396 at gmail.com Thu Jul 30 15:14:12 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 12:14:12 -0700 (PDT) Subject: class or thread id count References: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> Message-ID: On Jul 30, 1:13?pm, NighterNet wrote: > Need some help on doing some bit simple id count. It like every time > it create a class or thread there is id++. Python 3.1 > > example: > class ClientThread (threading.Thread): > ? ?def __init__(self,clientSocket): > ? ? ? threading.Thread.__init__(self) > ? ? ? ? ?self.id += 1; > > Not sure it how it works. use a class atrribute >>> class Person(): count = 0 def __init__(self, name, info): self.name = name self.info = info Person.count+=1 >>> p1 = Person('alex23', 'very annoying') >>> p2 = Person('Guido', 'very intelligent') >>> p2.count 2 >>> p1.count 2 >>> Person.count 2 From zuo at chopin.edu.pl Thu Jul 30 15:20:21 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 21:20:21 +0200 Subject: socket send In-Reply-To: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> Message-ID: 30-07-2009 o 05:52:06 NighterNet wrote: > I am trying to figure out how to send text or byte in python 3.1. I am > trying to send data to flash socket to get there. I am not sure how to > work it. > > buff= 'id=' , self.id , ':balive=False\n' > clientSock.send(buff); And what is the problem? *j -- Jan Kaliszewski (zuo) From bdezonia at wisc.edu Thu Jul 30 15:36:18 2009 From: bdezonia at wisc.edu (BDZ) Date: Thu, 30 Jul 2009 12:36:18 -0700 (PDT) Subject: os.path.exists() and Samba shares Message-ID: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> Hello. I have written a Python 3.1 script running on Windows that uses os.path.exists() to connect to network shares. If the various network shares require different user account and password combos than the account the script is running under the routine returns false. I need something like os.samba.path.exists(username,password,path). Does anyone have a suggestion on how I can accomplish what I need to do in Python? From martin at v.loewis.de Thu Jul 30 15:56:24 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Thu, 30 Jul 2009 21:56:24 +0200 Subject: No PyPI search for 3.x compatible packages In-Reply-To: References: Message-ID: <4a71fae9$0$10471$9b622d9e@news.freenet.de> Neil Hodgson wrote: > There appears to be no way to search PyPI for packages that are > compatible with Python 3.x. There are classifiers for 'Programming > Language' including 'Programming Language :: Python :: 3' but that seems > to be for implementation language since there are so many packages that > specify C. Not at all. The Trove classifiers *always* mean what the package author wants them to mean. In the specific case of Python :: 3, they should mean what makes most sense. For an application, it might make most sense to specify the language it is written in. For a Python library, it surely would make most sense to specify the Python version it supports - notice, however, that this is *also* the Python version the library is written in. Regards, Martin From darkneter at gmail.com Thu Jul 30 16:09:36 2009 From: darkneter at gmail.com (NighterNet) Date: Thu, 30 Jul 2009 13:09:36 -0700 (PDT) Subject: class or thread id count References: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> Message-ID: <68b09353-7e30-4df2-8e05-32159b03414a@f20g2000prn.googlegroups.com> On Jul 30, 12:14?pm, r wrote: > On Jul 30, 1:13?pm, NighterNet wrote: > > > Need some help on doing some bit simple id count. It like every time > > it create a class orthreadthere is id++. Python 3.1 > > > example: > > class ClientThread (threading.Thread): > > ? ?def __init__(self,clientSocket): > > ? ? ? threading.Thread.__init__(self) > > ? ? ? ? ?self.id += 1; > > > Not sure it how it works. > > use a class atrribute > > >>> class Person(): > > ? ? ? ? count = 0 > ? ? ? ? def __init__(self, name, info): > ? ? ? ? ? ? ? ? self.name = name > ? ? ? ? ? ? ? ? self.info = info > ? ? ? ? ? ? ? ? Person.count+=1 > > >>> p1 = Person('alex23', 'very annoying') > >>> p2 = Person('Guido', 'very intelligent') > >>> p2.count > 2 > >>> p1.count > 2 > >>> Person.count > > 2 Thanks it work. class Person(): count = 0 def __init__(self, name, info): self.name = name self.info = info Person.count+=1 self.count = Person.count From zuo at chopin.edu.pl Thu Jul 30 16:16:13 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 22:16:13 +0200 Subject: socket send In-Reply-To: <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> Message-ID: 30-07-2009 o 12:29:24 Francesco Bochicchio wrote: > On Jul 30, 5:52?am, NighterNet wrote: >> I am trying to figure out how to send text or byte in python 3.1. I am >> trying to send data to flash socket to get there. I am not sure how to >> work it. >> >> buff= 'id=' , self.id , ':balive=False\n' >> clientSock.send(buff); > > Try putting a 'b' before the constant string that you want to send: > > >>> type(b'123') > It's true. In python '...' literals are for strings ('str' type) and b'...' literals are for binary data (bytes type). Sockets' send() and recv() need binary data ('bytes' objects). > or use something like this to convert non constant strings (with only > ASCII characters) into bytes: > >>> s = "A non-constant string : %d " % n > >>> s > 'A non-constant string : 12 ' > >>> type(s) > What??? 'str' type in Python 3.x IS NOT a type of "non-constant" strings and IS NOT a type of strings "with only ASCII characters"! 'str' type in Python 3.x *is* the type of immutable ('constant') and Unicode character (Unicode, not only ASCII) strings. It's the same what 'unicode' type in Python 2.x. 'bytes' type objects in Python 3.x are also immutable. They are good for binary data (also text data encoded to binary data; see below). 'bytearray' type objects are like 'bytes', but mutable (like e. g. lists). >>>> b = bytes ( ord(c) for c in s ) >>>> b > b'A non-constant string : 12 ' > > You could also use struct.pack , that in python 3.x returns bytes and > not strings. In this case you need to specify the size of the string > (extra bytes are zero-filled): > > import struct What for all that weird actions??? * Simply do: bytes_object = str_object.encode() or bytes_object = bytes(str_object, ) -- where could be e.g. 'ascii', 'utf-8', 'iso-8859-1' etc.; using the former method, you can omit this argument -> then default encoding will be used (usualy 'utf-8'). * As easily as that, you can convert bytes to str: str_object = bytes_object.decode() or str_object = str(bytes_object, ) * Some examples: >>> 'What makes you think she is a witch?' # it's str 'What makes you think she is a witch?' >>> 'What makes you think she is a witch?'.encode() # to bytes b'What makes you think she is a witch?' >>> 'What makes you think she is a witch?'.encode('utf-8') # to bytes b'What makes you think she is a witch?' >>> bytes('What makes you think she is a witch?') # to bytes??? Traceback (most recent call last): File "", line 1, in TypeError: string argument without an encoding >>> bytes('What makes you think she is a witch?', 'ascii') # to bytes b'What makes you think she is a witch?' >>> b'What makes you think she is a witch?'.decode() # to str 'What makes you think she is a witch?' >>> b'What makes you think she is a witch?'.decode('utf-8') # to str 'What makes you think she is a witch?' >>> str(b'What makes you think she is a witch?', 'utf-8') # to str 'What makes you think she is a witch?' >>> str(b'What makes you think she is a witch?') # but not this way "b'What makes you think she is a witch?'" [^ note this] Cheers, *j -- Jan Kaliszewski (zuo) From zuo at chopin.edu.pl Thu Jul 30 16:23:32 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 22:23:32 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> Message-ID: 30-07-2009 o 13:36:49 Masklinn wrote: > On 30 Jul 2009, at 06:04 , alex23 wrote: >> On Jul 30, 1:06 pm, r wrote: >>> 2.) the .each method >>> container.each{|localVar| block} >>> This method can really cleanup some ugly for loops, although i really >>> like the readability of for loops. >> >> map(lambda localVar: , sequence) >> >> or: >> >> def usefully_named_func(var): >> >> return var >> >> transformed = [usefully_named_func(v) for v in sequence] >> > The issue here is of course that `map` and comprehensions are > transformations. `#each` exists for effectful iterations (Ruby has > `#map` for the map operation). So the intent expressed by `#each` and > `map` isn't the same. Furthermore and this is the most problematic > limitation of Python here, `lambda` doesn't allow complex > transformations due to its restrictions, so one has to switch to named > functions which works but isn't sexy (and tends to lower readability > imo). I don't see any real limitation. What's wrong in: for localVar in container: block And ruby's container.each is very similar to Python's iter() *j -- Jan Kaliszewski (zuo) From masklinn at masklinn.net Thu Jul 30 16:41:57 2009 From: masklinn at masklinn.net (Masklinn) Date: Thu, 30 Jul 2009 22:41:57 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> Message-ID: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: > 30-07-2009 o 13:36:49 Masklinn wrote: > >> On 30 Jul 2009, at 06:04 , alex23 wrote: >>> On Jul 30, 1:06 pm, r wrote: >>>> 2.) the .each method >>>> container.each{|localVar| block} >>>> This method can really cleanup some ugly for loops, although i >>>> really >>>> like the readability of for loops. >>> >>> map(lambda localVar: , sequence) >>> >>> or: >>> >>> def usefully_named_func(var): >>> >>> return var >>> >>> transformed = [usefully_named_func(v) for v in sequence] >>> >> The issue here is of course that `map` and comprehensions are >> transformations. `#each` exists for effectful iterations (Ruby has >> `#map` for the map operation). So the intent expressed by `#each` >> and `map` isn't the same. Furthermore and this is the most >> problematic limitation of Python here, `lambda` doesn't allow >> complex transformations due to its restrictions, so one has to >> switch to named functions which works but isn't sexy (and tends to >> lower readability imo). > > I don't see any real limitation. What's wrong in: > > for localVar in container: > block > Well what's wrong with using that rather than `map`, `filter` or a list comprehension? (and if you don't see what the limitations of `lambda` are, you probably very rarely use it) > And ruby's container.each is very similar to Python's iter() > Uh? not at all? From tjreedy at udel.edu Thu Jul 30 16:55:17 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 16:55:17 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: superpollo wrote: > r wrote: > how to reverse a string in python? must i usa a temp? like: > > >>> s = "ciccio" > >>> l = list(s) > >>> l.reverse() > >>> s = "".join(l) > >>> s > 'oiccic' > >>> > > ??? No. >>> ''.join(list(reversed('abc'))) 'cba' >>> 'abc'[2::-1] 'cba' >>> 'abc'[1000000000::-1] 'cba' Any int >= len(string)-1 will do, so the call to len will usually not be necessary. Needing strings reversed is not common. More common might be for char in reversed(somestring): process(char) Terry Jan Reedy From rt8396 at gmail.com Thu Jul 30 17:02:03 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 14:02:03 -0700 (PDT) Subject: Confessions of a Python fanboy References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: <39013d06-e281-4459-bdb2-19831c9526ea@k1g2000yqf.googlegroups.com> On Jul 30, 3:55?pm, Terry Reedy wrote: > superpollo wrote: > > r wrote: > > how to reverse a string in python? must i usa a temp? like: [snip] > Terry Jan Reedy No "r" never wrote anything like that. reversing a string is RTFM material, this is basic stuff here! Stop quoting me as saying things i did not say! I never asked how to reverse a string, "superpollo" did. From tjreedy at udel.edu Thu Jul 30 17:02:20 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 17:02:20 -0400 Subject: Sorting dict by value w/ operator.itemgetter- using key name? In-Reply-To: <3f1a902d0907301145t2aa559b7ifcb3dc45d9b7a363@mail.gmail.com> References: <3f1a902d0907301145t2aa559b7ifcb3dc45d9b7a363@mail.gmail.com> Message-ID: Wells Oliver wrote: > Bit of code: > > print sorted(results.items(), key=operator.itemgetter(1)) > > Would rather use 'H9', which is the name of the key in position 1 like: > > print sorted(results.items(), key=operator.itemgetter('H9')) > > Obviously that ain't work else I wouldn't be sending this email. Any tips? Suppose namepos=['H9':1,} etc, then itemgetter(namepos['H9']) From ethan at stoneleaf.us Thu Jul 30 17:04:21 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 30 Jul 2009 14:04:21 -0700 Subject: Regexp problem In-Reply-To: References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> Message-ID: <4A720AD5.1030202@stoneleaf.us> Marcus Wanner wrote: > On 7/30/2009 9:32 AM, Beldar wrote: > >> On 30 jul, 15:07, MRAB wrote: >> >>> Beldar wrote: >>> >>>> Hi there! >>>> I have a problem and i'm not very good at regular expressions. >>>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>>> lalala" I need a regexp to get the 'beldar' part, the format is >>>> 'something-is-listening', i need to get the something part, use it in >>>> my code, and then replace the whole 'something-is-listening' for >>>> another string. >>> >>> \w+ will match a word and enclosing it in (...) will capture what was >>> matched: >>> >>> m = re.search(r"(\w+)-is-listening", text) >>> print "Captured '%s'" % m.group(1) >>> print "Matched from %d to %d" % (m.start(), m.end()) >> >> >> Ok, thank you all, it was very helpful! > > Wow, I really need to learn more about regexp... > Any tutorials you guys can recommend? > > Marcus Mastering Regular Expressions Powerful Techniques for Perl and Other Tools By Jeffrey E. F. Friedl Great book! ~Ethan~ From python at mrabarnett.plus.com Thu Jul 30 17:28:55 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 22:28:55 +0100 Subject: Help with Regex for domain names In-Reply-To: References: <0422bf8f-ce59-4de2-a1a4-b16a32e5f152@o15g2000yqm.googlegroups.com> Message-ID: <4A721097.50201@mrabarnett.plus.com> Nobody wrote: > On Thu, 30 Jul 2009 10:29:09 -0700, rurpy wrote: > >>> regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)') >> You might also want to consider that some country >> codes such as "co" for Columbia might match more than >> you want, for example: >> >> re.match(r'[\w\-\.]+\.(?:us|au|de|co)', 'foo.boo.com') >> >> will match. > > ... so put \b at the end, i.e.: > > regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)\b') > It would still match "www.bbc.co.uk", so you might need: regex = re.compile(r'[\w\-\.]+\.(?:us|au|de)\b(?!\.\b)') From emmanuel.surleau at gmail.com Thu Jul 30 17:29:21 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Thu, 30 Jul 2009 23:29:21 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: Message-ID: <200907302329.21286.emmanuel.surleau@gmail.com> > 1.) No need to use "()" to call a function with no arguments. > Python --> "obj.m2().m3()" --ugly > Ruby --> "obj.m1.m2.m3" -- sweeet! > Man, i must admit i really like this, and your code will look so much > cleaner. It has benefits - code does look better. It has also significant cons - it is ambiguous. For instance: a = b Is b a variable or a method called without parameter? > 2.) the .each method > container.each{|localVar| block} > This method can really cleanup some ugly for loops, although i really > like the readability of for loops. It's not only the 'each' method. It's "anonymous callbacks everywhere", which is pretty cool. Certainly one of my favourite features of Ruby. > 3.) true OOP > Now before you go and get all "huffy" over this statement, hear me > out. Python is the best language in the world. But it damn sure has > some warts! "len(this)" instead of "obj.length" max(that) instead of > [1,2,3,4,5].max(). You know what i am talking about here people. We > all get complacent and It seems easier to just cope with these > problems instead of fighting for change. While ob.length() is more clear to read, it follows the same "operator" logic as __iter__. Which is OK, I suppose. > But look at the French, WHAT > THE HELL HAS THAT DONE FOR THEM, *NOTHING*!!!! I assume this was supposed to be funny? > As for the rest of Ruby, i am not impressed. The redundant usage of > "end" over indention perplexes me. You have the choice to use {}. If you are confused over languages which do not use indentation to delimit code blocks, you should probably try out a few languages outside Python. > The Perlish feel of "require" and > the horrifically cryptic idioms of Ruby regular expressions. Cryptic idioms of Ruby regular expressions? They are Perl-like regular expressions, not exactly a far cry from what re does. Also, you can chain them, which is extremely nice. > The "puts" and "gets" seem childish Well, they're short and easy. Writing "gets" is more intuitive than using raw_input() > and the math class does not even have > a degrees or radians function! > > Anyway, i thought i would get this off my chest and catch up with old > friends in the meantime. We can all make Python the perfect language > it needs to be, but it ain't gonna be easy! > Thank you all > > PS stay tuned for more from this series.... Cheers, Emm From python at mrabarnett.plus.com Thu Jul 30 17:40:46 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 22:40:46 +0100 Subject: Regexp problem In-Reply-To: <4A720AD5.1030202@stoneleaf.us> References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> <4A720AD5.1030202@stoneleaf.us> Message-ID: <4A72135E.7060205@mrabarnett.plus.com> Ethan Furman wrote: > Marcus Wanner wrote: >> On 7/30/2009 9:32 AM, Beldar wrote: >> >>> On 30 jul, 15:07, MRAB wrote: >>> >>>> Beldar wrote: >>>> >>>>> Hi there! >>>>> I have a problem and i'm not very good at regular expressions. >>>>> I have a text like "lalala lalala tiruri beldar-is-listening tiruri >>>>> lalala" I need a regexp to get the 'beldar' part, the format is >>>>> 'something-is-listening', i need to get the something part, use it in >>>>> my code, and then replace the whole 'something-is-listening' for >>>>> another string. >>>> >>>> \w+ will match a word and enclosing it in (...) will capture what was >>>> matched: >>>> >>>> m = re.search(r"(\w+)-is-listening", text) >>>> print "Captured '%s'" % m.group(1) >>>> print "Matched from %d to %d" % (m.start(), m.end()) >>> >>> >>> Ok, thank you all, it was very helpful! >> >> Wow, I really need to learn more about regexp... >> Any tutorials you guys can recommend? >> >> Marcus > > Mastering Regular Expressions > Powerful Techniques for Perl and Other Tools > By Jeffrey E. F. Friedl > > Great book! > +1 I have the first edition, seventh printing (December 1998). It refers to the 'regex' module of Python 1.4b1, which was subsequently replaced by the current 're' module and then removed from the standard library. I hope it's been updated since then. :-) From loic.domaigne at googlemail.com Thu Jul 30 17:41:50 2009 From: loic.domaigne at googlemail.com (=?ISO-8859-1?Q?Lo=EFc_Domaign=E9?=) Date: Thu, 30 Jul 2009 14:41:50 -0700 (PDT) Subject: os.path.exists() and Samba shares References: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> Message-ID: <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> Hi, > Hello. I have written a Python 3.1 script running on Windows that uses > os.path.exists() to connect to network shares. If the various network > shares require different user account and password combos than the > account the script is running under the routine returns false. I need > something like os.samba.path.exists(username,password,path). Does > anyone have a suggestion on how I can accomplish what I need to do in > Python? Could the Python Samba module PySamba be interesting for you? http://sourceforge.net/projects/pysamba/ HTH, Lo?c -- My blog: http://www.domaigne.com/blog From python at mrabarnett.plus.com Thu Jul 30 17:44:31 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 22:44:31 +0100 Subject: New implementation of re module In-Reply-To: <200907301524.28703.wolfgang@rohdewald.de> References: <4A6DD6FB.8040609@mrabarnett.plus.com> <4A719880.3070300@mrabarnett.plus.com> <200907301518.28122.wolfgang@rohdewald.de> <200907301524.28703.wolfgang@rohdewald.de> Message-ID: <4A72143F.6070506@mrabarnett.plus.com> Wolfgang Rohdewald wrote: > On Thursday 30 July 2009, Wolfgang Rohdewald wrote: >> so I did the conversion mentioned there. This works: > > I actually do not know if it works - but it compiles. > Yes, it works. I've updated my code accordingly and it'll be in the next release. From rt8396 at gmail.com Thu Jul 30 17:44:34 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 14:44:34 -0700 (PDT) Subject: Confessions of a Python fanboy References: Message-ID: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> On Jul 30, 4:29?pm, Emmanuel Surleau wrote: > > 1.) No need to use "()" to call a function with no arguments. > > Python --> "obj.m2().m3()" --ugly > > ? Ruby --> "obj.m1.m2.m3" ?-- sweeet! > > Man, i must admit i really like this, and your code will look so much > > cleaner. > > It has benefits - code does look better. It has also significant cons - it is > ambiguous. > For instance: > > a = b > > Is b a variable or a method called without parameter? Hello Emanuel, Again, who so ever names a method with such a non-descriptive name will get whats coming to him. And if you did for some reason use such a cryptic name as "b", do yourself (and everyone else) a favor and follow it with "()" to denote the method call. Remember when something is optional that means you have an option to use it OR not use it. optional (http://definr.com/optional) adj: possible but not necessary; left to personal choice [ant: obligatory] simple solutions for simple problems. From zuo at chopin.edu.pl Thu Jul 30 17:52:45 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Thu, 30 Jul 2009 23:52:45 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: Dnia 30-07-2009 o 22:41:57 Masklinn napisa?(a): > On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: >> 30-07-2009 o 13:36:49 Masklinn wrote: >> >>> On 30 Jul 2009, at 06:04 , alex23 wrote: >>>> On Jul 30, 1:06 pm, r wrote: >>>>> 2.) the .each method >>>>> container.each{|localVar| block} >>>>> This method can really cleanup some ugly for loops, although i really >>>>> like the readability of for loops. >>>> >>>> map(lambda localVar: , sequence) >>>> >>>> or: >>>> >>>> def usefully_named_func(var): >>>> >>>> return var >>>> >>>> transformed = [usefully_named_func(v) for v in sequence] >>>> >>> The issue here is of course that `map` and comprehensions are >>> transformations. `#each` exists for effectful iterations (Ruby has >>> `#map` for the map operation). So the intent expressed by `#each` and >>> `map` isn't the same. Furthermore and this is the most problematic >>> limitation of Python here, `lambda` doesn't allow complex >>> transformations due to its restrictions, so one has to switch to named >>> functions which works but isn't sexy (and tends to lower readability >>> imo). >> >> I don't see any real limitation. What's wrong in: >> >> for localVar in container: >> block >> > Well what's wrong with using that rather than `map`, `filter` or a list > comprehension? (and if you don't see what the limitations of `lambda` > are, you probably very rarely use it) I know well about the expression-only-limitation of lambda, fortnately there is the 'for' loop construct (or, alternatively, you can define a named function). And then, what's wrong with using 'for' rather than 'map', 'filter' etc.? Agree, that 'anonymous block syntax' would be nice in some cases, but I don't see any real limitation caused by lack of it i.e. something you can't (all you can only with unreasonable effort). >> And ruby's container.each is very similar to Python's iter() > Uh? not at all? OK, .each is like Python's iter() + some form of iterating over it ('for' loop or '[i]map'...). Still, in this matter there is no real functionality in Ruby that you can't reach in Python. All that confessions of a (former?) Python fanboy are about sintactic sugars, which importance is incomparable which such issues as e.g. particular mechanism of classes, their inheritance etc. or even such small helpers like function annotations. Cheers, *j -- Jan Kaliszewski (zuo) From kyrie at uh.cu Thu Jul 30 17:57:48 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Thu, 30 Jul 2009 17:57:48 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <200907301757.48761.kyrie@uh.cu> On Thursday 30 July 2009 04:41:57 pm Masklinn wrote: > On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: > > 30-07-2009 o 13:36:49 Masklinn wrote: > > > > I don't see any real limitation. What's wrong in: > > > > for localVar in container: > > block > > Well what's wrong with using that rather than `map`, `filter` or a > list comprehension? As I understood the question, it was "was wrong in 'for var in container' in comparison with ruby's container.each?" What's the (semantic) difference between for localVar in container: block and container.each{|localVar| block} ? (the sintactic difference is quite clear... if there is no semantic difference, I'd rather use the first version!) Map, filter and comprehensions have little to do with that (syntactic sugar that may allow some optimizations, but that could otherwise be replaced by the for loop). > (and if you don't see what the limitations of > `lambda` are, you probably very rarely use it) I'm not the poster, but: * I use lambdas, a lot. * I "know" python lambdas are limited, compared with theoretical lambdas. * I don't [usually] see that as a "limitation". Most of the time, the limitation is the lack of multiline/multiexpression lambdas. But I've never seen a case where I would /prefer/ to have a multiline function definition inside a comprehension or method invocation, instead of a named function defined two lines before it. (The second famous limitation is the inability to use statements inside lambdas... luckily, now that we have a ternary operator and a print function, it is less limiting) > > And ruby's container.each is very similar to Python's iter() > > Uh? not at all? I agree with this. It should have said that they are equivalent in the sense that they represent the iterator protocols of both languages, and nothing more. I'd like to ask, what "container.each" is, exactly? It looks like a function call (as I've learned a few posts ago), but, what are its arguments? How the looping "works"? Does it receive a "code" object that it has to execute? Is .each some kind of magic keyword? (This has little to do with python or the current thread, so feel free to reply off-list if you want to...) Regards, Luis. -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From python at mrabarnett.plus.com Thu Jul 30 18:06:05 2009 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 30 Jul 2009 23:06:05 +0100 Subject: New implementation of re module In-Reply-To: References: Message-ID: <4A72194D.4050604@mrabarnett.plus.com> Piet van Oostrum wrote: >>>>>> MRAB (M) wrote: > >> M> Hi all, >> M> I've been working on a new implementation of the re module. The details >> M> are at http://bugs.python.org/issue2636, specifically from >> M> http://bugs.python.org/issue2636#msg90954. I've included a .pyd file for >> M> Python 2.6 on Windows if you want to try it out. > >> M> I'm interested in how fast it is generally, compared with the current re >> M> module, but especially when faced with those 'pathological' regular >> M> expressions which seem to take a long time to finish, for example: > >> M> re.search(r"^(.+|D)*A$", "x" * 25 + "B") > >> M> which on my PC (1.8GHz) takes 18.98secs with the re module but <0.01secs >> M> with this new implementation. > > Is this version also going to use the Thompson approach? That approach is what inspired the method in the new implementation, but its speed comes from asking _whether_ there's a match, not _where_ there's a match, so it can ignore the route taken to get the match. "grep", for example, selects those lines which match a pattern, whereas the typical Python (and Perl) usage is to extract part of a string or which part of the string matches. Trying to support lazy and possessive quantifiers in Thompson's approach is tricky (at least, I haven't been able to figure it out), and back-references are right out! :-) There's always the chance that I could come up with some sort of hybrid scheme, applying different approaches depending on certain conditions. It already switches from depth-first to breadth-first when it's taken too many iterations without advancing and merges similar states, which is why it's so much better with 'pathological' patterns; I just have to get the depth-first phase to be as fast as the current 're' module. From benjamin.kaplan at case.edu Thu Jul 30 18:08:21 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 30 Jul 2009 18:08:21 -0400 Subject: calling obect method In-Reply-To: <960855.56660.qm@web7906.mail.in.yahoo.com> References: <960855.56660.qm@web7906.mail.in.yahoo.com> Message-ID: On Thu, Jul 30, 2009 at 3:04 PM, srinivasan srinivas < sri_annauni at yahoo.co.in> wrote: > Hi, > I would like to know if i have an object and one of its methods as a > string. How to call the method using this string? > For ex > If object is x and string y has one of its method names. > x.value(y)() ---. Is there a way to do this? > > Thanks, > Srini getattr(x,y)() > > > > See the Web's breaking stories, chosen by people like you. Check out > Yahoo! Buzz. http://in.buzz.yahoo.com/ > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rt8396 at gmail.com Thu Jul 30 18:13:41 2009 From: rt8396 at gmail.com (r) Date: Thu, 30 Jul 2009 15:13:41 -0700 (PDT) Subject: Confessions of a Python fanboy References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <80839e72-9df4-4590-b22d-4c5c829e37b6@h11g2000yqb.googlegroups.com> On Jul 30, 4:57?pm, Luis Zarrabeitia wrote: [snip] > I'd like to ask, what "container.each" is, exactly? It looks like a function > call (as I've learned a few posts ago), but, what are its arguments? How the > looping "works"? Does it receive a "code" object that it has to execute? > Is .each some kind of magic keyword? (This has little to do with python or > the current thread, so feel free to reply off-list if you want to...) > > Regards, > > Luis. Hello Luis, I think your question is completely valid within the context of this thread. The purpose of his thread was to get feedback on how Python and Ruby ideas could be cumulated into the best high level language. And being that i am the BDFL of the "Confessions of a Python Fanboy" thread, you have my personal permission to continue on with this subject matter..., From rcdailey at gmail.com Thu Jul 30 18:40:37 2009 From: rcdailey at gmail.com (Robert Dailey) Date: Thu, 30 Jul 2009 15:40:37 -0700 (PDT) Subject: Printing with colors in a portable way Message-ID: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Anyone know of a way to print text in Python 3.1 with colors in a portable way? In other words, I should be able to do something like this: print_color( "This is my text", COLOR_BLUE ) And this should be portable (i.e. it should work on Linux, Mac, Windows). From nyamatongwe+thunder at gmail.com Thu Jul 30 18:47:35 2009 From: nyamatongwe+thunder at gmail.com (Neil Hodgson) Date: Thu, 30 Jul 2009 22:47:35 GMT Subject: No PyPI search for 3.x compatible packages In-Reply-To: References: Message-ID: Francesco Bochicchio : > Are you sure? I note that for example pygtk has as language tags both > C and python. So maybe a C extension > for python3 would have both C and python 3 as language tags. > > I suspect that the 109 packages you found are the only ones obf the > 4829 which works with python3 (but I hope > to be wrong ). There are 523 packages marked with Python :: [2, 2.1, ...] and I hope more than that work with Python 2.x. What I would like to encourages is adding Python :: 3 or a more specific minimum supported version classifier to packages that support Python 3. I wouldn't want to go to the level of adding a classifier for each version of Python supported since a package will often stay valid when a new Python is released. Neil From gdamjan at gmail.com Thu Jul 30 18:59:31 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Fri, 31 Jul 2009 00:59:31 +0200 Subject: [ANN] pyKook 0.0.2 - a simple build tool similar to Make or Ant References: Message-ID: > I have released pyKook 0.0.2. > http://pypi.python.org/pypi/Kook/0.0.2 > http://www.kuwata-lab.com/kook/ > http://www.kuwata-lab.com/kook/pykook-users-guide.html How does it compare to http://code.google.com/p/fabricate/ and why is this problem (solution) reoccurring all the time -- ?????? ( http://softver.org.mk/damjan/ ) Teaching a pig to sing wastes your time & annoys the pig. From robert.kern at gmail.com Thu Jul 30 19:06:31 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 30 Jul 2009 18:06:31 -0500 Subject: Confessions of a Python fanboy In-Reply-To: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> Message-ID: On 2009-07-30 16:44, r wrote: > On Jul 30, 4:29 pm, Emmanuel Surleau > wrote: >>> 1.) No need to use "()" to call a function with no arguments. >>> Python --> "obj.m2().m3()" --ugly >>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>> Man, i must admit i really like this, and your code will look so much >>> cleaner. >> It has benefits - code does look better. It has also significant cons - it is >> ambiguous. >> For instance: >> >> a = b >> >> Is b a variable or a method called without parameter? > > Hello Emanuel, > Again, who so ever names a method with such a non-descriptive name > will get whats coming to him. And if you did for some reason use such > a cryptic name as "b", do yourself (and everyone else) a favor and > follow it with "()" to denote the method call. Remember when something > is optional that means you have an option to use it OR not use it. I believe his point is that it is ambiguous to the compiler, not humans reading the code. Python functions and methods are first class objects. They can be passed around. If they were auto-called, then you could not do this. -- 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 rhodri at wildebst.demon.co.uk Thu Jul 30 19:29:50 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 31 Jul 2009 00:29:50 +0100 Subject: Printing with colors in a portable way In-Reply-To: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: On Thu, 30 Jul 2009 23:40:37 +0100, Robert Dailey wrote: > Anyone know of a way to print text in Python 3.1 with colors in a > portable way? In other words, I should be able to do something like > this: > > print_color( "This is my text", COLOR_BLUE ) > > And this should be portable (i.e. it should work on Linux, Mac, > Windows). ...in an xterm, over a telnet connection, via VNC... There's no portable way of doing this in any language, since it's up to the terminal emulator exactly what it does with what incoming bytes. Some respect the ANSI colour codes, some don't; that's about all that you can say. -- Rhodri James *-* Wildebeest Herder to the Masses From zuo at chopin.edu.pl Thu Jul 30 19:54:20 2009 From: zuo at chopin.edu.pl (Jan Kaliszewski) Date: Fri, 31 Jul 2009 01:54:20 +0200 Subject: Printing with colors in a portable way In-Reply-To: References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: 31-07-2009 o 01:29:50 Rhodri James wrote: > On Thu, 30 Jul 2009 23:40:37 +0100, Robert Dailey > wrote: > >> Anyone know of a way to print text in Python 3.1 with colors in a >> portable way? In other words, I should be able to do something like >> this: >> >> print_color( "This is my text", COLOR_BLUE ) >> >> And this should be portable (i.e. it should work on Linux, Mac, >> Windows). > > ...in an xterm, over a telnet connection, via VNC... > > There's no portable way of doing this in any language, since it's > up to the terminal emulator exactly what it does with what incoming > bytes. Some respect the ANSI colour codes, some don't; that's about > all that you can say. Yeah. Although you could try to provide "term-sensitive" mapping of colour names -> codes, e.g.: # color_codes.py class NoColors: blue = '' red = '' class XtermColors: blue = '
' red = '' class FooBarColors: blue = '' red = '' COLORS = { 'nocolors': NoColors, 'xterm': XtermColors, 'another term': FooBarColors, } # a_program.py import color_codes # e.g. with used_term == 'xterm'... colors = color_codes.COLORS[used_term] print('Some text, {colors.blue}Something in blue, ' '{colors.red}And now in red.').format(colors=colors) Regards, *j -- Jan Kaliszewski (zuo) From gagsl-py2 at yahoo.com.ar Thu Jul 30 20:04:01 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Thu, 30 Jul 2009 21:04:01 -0300 Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> <27mdnb-NxM3DJOzXnZ2dnUVZ_hxi4p2d@posted.visi> Message-ID: En Thu, 30 Jul 2009 12:16:46 -0300, Unknown escribi?: > On 2009-07-30, Barak, Ron wrote: >> On second thoughts, I may have a problem implementing the >> wrapper solution, because my actual test_pyc.pyc, needs to >> parse its command line. Namely, the actual call to >> test_pyc.pyc looks something like this: >> >> $ python test_pyc.py -U dave -PpasswoRD -C CreateMcGroupOnVolume >> --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n "host1,host2" -g gn -j jn -s svn -t >> tvn -p pool1 -l -c >> >> And I don't know of a way to add these parameters to the >> "import test_pyc" in wrapper >> >> Is there a way to pass information to an imported module ? >> (Sorry if there's an obvious answer, I just cannot figure it >> out). > > I don't understand your problem. The module would parse > sys.argv just like always. > > If you don't like that for some reason, you could define an > entry point in the module and call it: > > #!/usr/bin/python > import sys,test_pyc > test_pyc.main(*sys.argv) And you *should* do it that way, instead of just importing the other module and executing ALL the program as a side-effect of the import statement. In the first case, the import lock will prevent other threads to run. -- Gabriel Genellina From digitig at gmail.com Thu Jul 30 20:05:29 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 31 Jul 2009 01:05:29 +0100 Subject: Confessions of a Python fanboy In-Reply-To: <4a71e1a5$0$40016$4fafbaef@reader3.news.tin.it> References: <4a71e1a5$0$40016$4fafbaef@reader3.news.tin.it> Message-ID: 2009/7/30 superpollo : > Tim Rowe wrote: >> Any language that gets any sort of real use has to have. For instance, >> I love Ada's numeric types (you can specify either the minimum number >> of significant figures or the maximum delta for a real type, and it >> will give you a type that satisfies that -- or a compilation error if >> it can't. ?That matches genuine problem domains far better > > doesn't the decimal module do some of that too? Not as far as I can tell, no. -- Tim Rowe From j.sundo at cs.ucl.ac.uk Thu Jul 30 20:17:04 2009 From: j.sundo at cs.ucl.ac.uk (=?ISO-8859-1?Q?Jannik_Sund=F8?=) Date: Fri, 31 Jul 2009 01:17:04 +0100 Subject: Confused About Python Loggin Message-ID: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> Dear all, I am quite confused about the Python logging. I have read and re-read the Python documentation for the Python logging module and googled, but to no avail. I simply want one logger to log to a file and another logger to log to the console. Neither should log the other's messages. The code below causes fileLogger to log to BOTH a file and the console, how come? It seems to me that a call such as "consoleHandler = logging.StreamHandler()" affects other loggers, which I do not want. Also, if I do not do logging.basicConfig(level=logging.INFO) in one of the classes of my application, the logging does not work. Any ideas how come? Thanks a lot for any help! :) fileHandler = logging.FileHandler('../../logs/log.txt') fileLogger = logging.getLogger('TESTHARNESSFILE') fileLogger.addHandler(fileHandler) fileLogger.setLevel(logging.INFO) consoleHandler = logging.StreamHandler() logger = logging.getLogger('TESTHARNESS') logger.addHandler(consoleHandler) logger.setLevel(logging.INFO) From dhaneshpai at gmail.com Thu Jul 30 20:24:10 2009 From: dhaneshpai at gmail.com (Dhanesh) Date: Thu, 30 Jul 2009 17:24:10 -0700 (PDT) Subject: Non-blocking read with popen subprocess Message-ID: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> Hi , I am trying to use subprocess popen on a windows command line executable with spits messages on STDOUT as well as STDIN. Code snippet is as below :- ########################################################################## sOut="" sErr="" javaLoaderPath = os.path.join("c:\\","Program Files","Research In Motion","BlackBerry JDE 4.7.0","bin","Javaloader.exe") cmd = [javaLoaderPath,'-u','load','helloworld.jad'] popen = subprocess.Popen (cmd,bufsize=256,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: sErr = sErr + popen.stderr.read(64) sOut = sOut + popen.stdout.read(64)------------------> Blocks here if None != popen.poll(): break; ########################################################################## I observed that python scripts blocks at stdout read on the other hand when I do not create a child stdout PIPE say " stdout=None" , things seems to be working fine. how can I we have a non blocking read ? And why does stdout block even where data is available to be read ( which seem to be apparent when stdout=None, and logs appear on parents STDOUT) ? From tjreedy at udel.edu Thu Jul 30 20:38:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 30 Jul 2009 20:38:15 -0400 Subject: Confessions of a Python fanboy In-Reply-To: <39013d06-e281-4459-bdb2-19831c9526ea@k1g2000yqf.googlegroups.com> References: <196929f0-9b3f-4210-aa34-d7ab64bc934d@n11g2000yqb.googlegroups.com> <19f8b41f-27ec-4563-90db-8a984d1f88e5@26g2000yqk.googlegroups.com> <4a71e102$0$40016$4fafbaef@reader3.news.tin.it> <39013d06-e281-4459-bdb2-19831c9526ea@k1g2000yqf.googlegroups.com> Message-ID: r wrote: > On Jul 30, 3:55 pm, Terry Reedy wrote: >> superpollo wrote: >>> r wrote: >>> how to reverse a string in python? must i usa a temp? like: > [snip] >> Terry Jan Reedy > > No "r" never wrote anything like that. reversing a string is RTFM > material, this is basic stuff here! Stop quoting me as saying things i > did not say! I never asked how to reverse a string, "superpollo" > did. Nor did what I posted say that you did. The above says that superpollo wrote "how to reverse..., as he did. Quotes are indented once more than the author. The above says 'r wrote' with no indent and then "No...", which you did write, indented once. I agree that since I did not quote anything that you wrote, I should also have snipped your name. But I least I do attempt to snip material irrelavant to my comments. Terry Jan Reedy From brochu121 at gmail.com Thu Jul 30 20:57:56 2009 From: brochu121 at gmail.com (David Brochu) Date: Thu, 30 Jul 2009 20:57:56 -0400 Subject: httplib headers Message-ID: <9583ed900907301757q50e85b42o830820980386ff57@mail.gmail.com> Using the following code I am trying to pass a header to my POST request. (Note: urls masked) file = sys.argv[0] url = sys.argv[1] conn = httplib.HTTPConnection("XX.XXX.XX.XXX",XXXX) headers = {'content-type':'application/xml'} conn.request("POST",url,file,headers) response = conn.getresponse() head = response.getheaders() status = response.status response = response.read() print response, status, head When I run this script and view the headers that get printed the following displays: [('content-length', '691'), ('proxy-connection', 'Keep-Alive'), ('connection', 'Keep-Alive'), ('pragma', 'no-cache'), ('cache-control', 'no-cache'), ('content-type', 'text/html; charset=utf-8')] Why isn't my content-type header using the value I pass in from headers? -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Thu Jul 30 21:13:53 2009 From: aahz at pythoncraft.com (Aahz) Date: 30 Jul 2009 18:13:53 -0700 Subject: Run pyc file without specifying python path ? References: <157EBE1D-3ACF-4A3D-BDCD-F094695D0707@rgbaz.eu> <7F0503CD69378F49BE0DC30661C6CCF6701A26CA@enbmail01.lsi.com> Message-ID: In article , Chris Rebert wrote: > >No, with the shebang line (and assuming execute permissions on the >file), it would look like: > >./wrapper.py There's no reason to name it ".py"; you can have a perfectly useful "shell script" that just happens to list python on the shebang line instead of the usual /bin/sh. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From python at mrabarnett.plus.com Thu Jul 30 21:20:40 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 02:20:40 +0100 Subject: httplib headers In-Reply-To: <9583ed900907301757q50e85b42o830820980386ff57@mail.gmail.com> References: <9583ed900907301757q50e85b42o830820980386ff57@mail.gmail.com> Message-ID: <4A7246E8.3010400@mrabarnett.plus.com> David Brochu wrote: > Using the following code I am trying to pass a header to my POST > request. (Note: urls masked) > > file = sys.argv[0] > url = sys.argv[1] > conn = httplib.HTTPConnection("XX.XXX.XX.XXX",XXXX) > headers = {'content-type':'application/xml'} > conn.request("POST",url,file,headers) > response = conn.getresponse() > head = response.getheaders() > status = response.status > response = response.read() > print response, status, head > > When I run this script and view the headers that get printed the > following displays: > > [('content-length', '691'), ('proxy-connection', 'Keep-Alive'), > ('connection', 'Keep-Alive'), ('pragma', 'no-cache'), ('cache-control', > 'no-cache'), ('content-type', 'text/html; charset=utf-8')] > Why isn't my content-type header using the value I pass in from headers? > Aren't you just setting the headers for the "POST" you're sending (you're posting a file whose content type is 'application/xml'), but printing the headers of the response you receive (the content type of the response is 'text/html; charset=utf-8')? From jgardner at jonathangardner.net Thu Jul 30 22:30:46 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Thu, 30 Jul 2009 19:30:46 -0700 (PDT) Subject: Non-blocking read with popen subprocess References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> Message-ID: <824f8092-55b1-4df2-a6b5-74a4b1b1d9ea@i18g2000pro.googlegroups.com> On Jul 30, 5:24?pm, Dhanesh wrote: > > how can I we have a non blocking read ? See http://docs.python.org/library/popen2.html#flow-control-issues Note well: In the non-blocking world, you have to use select() or poll () to get your job done. You may want to look at "communicate" (http://docs.python.org/library/ subprocess.html#popen-objects) which may be what you need. From lukepadawan at gmail.com Thu Jul 30 22:55:40 2009 From: lukepadawan at gmail.com (Lucas P Melo) Date: Thu, 30 Jul 2009 23:55:40 -0300 Subject: Generators through the C API Message-ID: <4A725D2C.4030705@gmail.com> Hello, I'm a total noob about the C API. Is there any way to create a generator function using the C API? I couldn't find anything like the 'yield' keyword in it. Thanks in advance. From grante at visi.com Thu Jul 30 23:02:17 2009 From: grante at visi.com (Grant Edwards) Date: Thu, 30 Jul 2009 22:02:17 -0500 Subject: Printing with colors in a portable way References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: On 2009-07-30, Robert Dailey wrote: > Anyone know of a way to print text in Python 3.1 with colors in a > portable way? In other words, I should be able to do something like > this: > > print_color( "This is my text", COLOR_BLUE ) > > And this should be portable (i.e. it should work on Linux, Mac, > Windows). Generating postscript or PDF is the only remotely portable way to print anything. -- Grant From cocobear.cn at gmail.com Fri Jul 31 00:18:25 2009 From: cocobear.cn at gmail.com (cocobear) Date: Thu, 30 Jul 2009 21:18:25 -0700 (PDT) Subject: merge two png pic References: Message-ID: On Jul 29, 9:20?am, cocobear wrote: > Thistwopngfile has their own palette > > >>> im1.mode > 'P' > >>> im.mode > 'P' > >>> im.getpalette == im1.getpalette > > False > > I can use this code tomergetwopngpictogether: > > Map = Image.new("RGB", (x,y)) > Map.paste(im, box) > Map.paste(im1,box) > > Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) > > But if thetwopngpicis too big , or if I have tomergemorepic > together, I will get MemoryError: > > >>> Image.new("RGB",(44544,38656)) > > Traceback (most recent call last): > ? File "", line 1, in > ? File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in > new > ? ? return Image()._new(core.fill(mode, size, color)) > MemoryError > > How can I directlymergetwopicto a ?P' modepngwith palette. Nobody replied. From ivlenin at gmail.com Fri Jul 31 01:27:22 2009 From: ivlenin at gmail.com (I V) Date: 31 Jul 2009 07:27:22 +0200 Subject: Confessions of a Python fanboy References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <4a7280ba$1@news.x-privat.org> On Thu, 30 Jul 2009 17:57:48 -0400, Luis Zarrabeitia wrote: > As I understood the question, it was "was wrong in 'for var in > container' in comparison with ruby's container.each?" > > What's the (semantic) difference between > > for localVar in container: > block > > and > > container.each{|localVar| block} I don't think "each" is a particularly compelling example of Ruby's blocks - as you say, it's easily replaceable with a Python for-loop. The advantage of Ruby's syntax is that it allows you to define further functions that are sort-of like new control structures. So you have things like: File.open('myfile', 'r') do |file| while line = file.gets puts line end end Which is like a python's: with open('myfile', 'r) as f: for line in f: print f However, with Ruby, this kind of scoping construct can be added without adding new syntax. Another interesting example is the Sinatra web framework, which allows you to write a web app with something like: get '/' do "Index page" end post '/:name' do "Posted to " + params[:name] end Which looks something like a DSL for web apps, but is accomplished solely with normal ruby functions. More: http://www.sinatrarb.com/intro.html Ruby blocks are, fundamentally, syntactic sugar, but they are kind of neat. From emmanuel.surleau at gmail.com Fri Jul 31 01:46:49 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 07:46:49 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> Message-ID: <200907310746.49392.emmanuel.surleau@gmail.com> On Friday 31 July 2009 01:06:31 Robert Kern wrote: > On 2009-07-30 16:44, r wrote: > > On Jul 30, 4:29 pm, Emmanuel Surleau > > > > wrote: > >>> 1.) No need to use "()" to call a function with no arguments. > >>> Python --> "obj.m2().m3()" --ugly > >>> Ruby --> "obj.m1.m2.m3" -- sweeet! > >>> Man, i must admit i really like this, and your code will look so much > >>> cleaner. > >> > >> It has benefits - code does look better. It has also significant cons - > >> it is ambiguous. > >> For instance: > >> > >> a = b > >> > >> Is b a variable or a method called without parameter? > > > > Hello Emanuel, > > Again, who so ever names a method with such a non-descriptive name > > will get whats coming to him. And if you did for some reason use such > > a cryptic name as "b", do yourself (and everyone else) a favor and > > follow it with "()" to denote the method call. Remember when something > > is optional that means you have an option to use it OR not use it. > > I believe his point is that it is ambiguous to the compiler, not humans > reading the code. Actually, both. Use a variable you didn't initialize? This is what you get: NameError: undefined local variable or method `b' for main:Object from (irb):1 The compiler has no idea if b is a variable or a method. It also makes it very easy to shadow an existing method by declaring a variable of the same name, which is problematic. I suppose it is a side-effect of Ruby's Perl philosophical inheritance - except that Perl uses sigils to prefix its variables, negating this issue. Cheers, Emm From steve at nospam.au Fri Jul 31 02:09:48 2009 From: steve at nospam.au (steve) Date: Fri, 31 Jul 2009 16:09:48 +1000 Subject: Test for Pythonwin? Message-ID: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> Is there a good way to check if a script is running inside Pythonwin? Perhaps a property or method that is exposed by that environment? or, alternatively, is there a better place to ask :-) Steven From __peter__ at web.de Fri Jul 31 02:52:01 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 08:52:01 +0200 Subject: merge two png pic References: Message-ID: cocobear wrote: > On Jul 29, 9:20 am, cocobear wrote: >> Thistwopngfile has their own palette >> >> >>> im1.mode >> 'P' >> >>> im.mode >> 'P' >> >>> im.getpalette == im1.getpalette >> >> False >> >> I can use this code tomergetwopngpictogether: >> >> Map = Image.new("RGB", (x,y)) >> Map.paste(im, box) >> Map.paste(im1,box) >> >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) >> >> But if thetwopngpicis too big , or if I have tomergemorepic >> together, I will get MemoryError: >> >> >>> Image.new("RGB",(44544,38656)) As a workaround you could split the image into tiles that fit into your machine's RAM. Or you could try to do the rendering on a 64-bit system. You'll need at least >>> 3*44544*38656./(2**30) 4.8109130859375 5 gigabytes for the target image plus memory for the biggest source image. Alternatively there are probably tools that can keep parts of the image on disk (imagemagick, maybe? you'll have to check yourself or ask in a specialized forum). As a last resort you should be able to write such a tool yourself. I don't know how hard it would be to generate the PNG, but the RGB pasting should be trivial. >> Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in >> new >> return Image()._new(core.fill(mode, size, color)) >> MemoryError >> >> How can I directlymergetwopicto a ?P' modepngwith palette. > > > Nobody replied. What do you want to do with such a big image? You will run into the same limitation when you are trying to display it. Peter From tjreedy at udel.edu Fri Jul 31 02:52:56 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 02:52:56 -0400 Subject: merge two png pic In-Reply-To: References: Message-ID: cocobear wrote: > On Jul 29, 9:20 am, cocobear wrote: >> Thistwopngfile has their own palette >> >>>>> im1.mode >> 'P' >>>>> im.mode >> 'P' >>>>> im.getpalette == im1.getpalette >> False >> >> I can use this code tomergetwopngpictogether: >> >> Map = Image.new("RGB", (x,y)) >> Map.paste(im, box) >> Map.paste(im1,box) >> >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) >> >> But if thetwopngpicis too big , or if I have tomergemorepic >> together, I will get MemoryError: >> >>>>> Image.new("RGB",(44544,38656)) >> Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in >> new >> return Image()._new(core.fill(mode, size, color)) >> MemoryError >> >> How can I directlymergetwopicto a ?P' modepngwith palette. > > > Nobody replied. A. Leavingoutspacesmakesyourposthardtoread. B. Nobody knows how to avoid mem error. tjr From shearichard at gmail.com Fri Jul 31 03:09:48 2009 From: shearichard at gmail.com (northof40) Date: Fri, 31 Jul 2009 00:09:48 -0700 (PDT) Subject: Python 2.6 in shared/VPS environment Message-ID: <6babf860-0161-4c1a-82f8-69e03dc53da0@p36g2000prn.googlegroups.com> Hi - I'd really like to have access to Python 2.6 to try something out. It needs to be on Linux/Unix machine. I don't mind paying but whichever way I turn I find Python 2.4 is the standard installation. Anyone know of anyone who offers this out of the box ? Or got any smart way of achieving it ? thanks R. From __peter__ at web.de Fri Jul 31 03:14:59 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 09:14:59 +0200 Subject: Confused About Python Loggin References: Message-ID: Jannik Sund? wrote: > Dear all, I am quite confused about the Python logging. I have read > and re-read the Python documentation for the Python logging module and > googled, but to no avail. I simply want one logger to log to a file > and another logger to log to the console. Neither should log the > other's messages. The code below causes fileLogger to log to BOTH a > file and the console, how come? It seems to me that a call such as > "consoleHandler = logging.StreamHandler()" affects other loggers, > which I do not want. Also, if I do not do > logging.basicConfig(level=logging.INFO) in one of the classes of my > application, the logging does not work. Any ideas how come? > > Thanks a lot for any help! :) > > fileHandler = logging.FileHandler('../../logs/log.txt') > fileLogger = logging.getLogger('TESTHARNESSFILE') > fileLogger.addHandler(fileHandler) > fileLogger.setLevel(logging.INFO) > consoleHandler = logging.StreamHandler() > logger = logging.getLogger('TESTHARNESS') > logger.addHandler(consoleHandler) > logger.setLevel(logging.INFO) The code you present should work as you expect: $ cat tmp.py import logging fileHandler = logging.FileHandler('tmp.txt') fileLogger = logging.getLogger('TESTHARNESSFILE') fileLogger.addHandler(fileHandler) fileLogger.setLevel(logging.INFO) consoleHandler = logging.StreamHandler() logger = logging.getLogger('TESTHARNESS') logger.addHandler(consoleHandler) logger.setLevel(logging.INFO) logger.info("to console") fileLogger.info("to file") $ python tmp.py to console $ cat tmp.txt to file What problems exactly are you running into if you omit the basicConfig() call? Peter From malc0de.encrypt at gmail.com Fri Jul 31 03:25:09 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 00:25:09 -0700 (PDT) Subject: problem on socket programming Message-ID: hello there, I'm using Learning Python 3rd edition as my core reference, the book is great. here's some snippet for demonstrating echo-server program, but I can't interpret it in both windows / Gnu linux ... #!/usr/bin/env python from socket import * myHost = '' myPort = 50007 sockobj = socket(AF_INET,SOCK_STREAM) sockobj.bind((myHost,myPort)) sockobj.listen(5) while true : connection,address = sockobj.accept() print 'server connected by :',address while True: data = connection.recv(1024) if not data: break connection.send('echo : '+ data) connection.close() where's the problem, beforehand appreciate ... From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 03:28:29 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 07:28:29 GMT Subject: Confessions of a Python fanboy References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> Message-ID: <008e7846$0$9756$c3e8da3@news.astraweb.com> On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote: > On 2009-07-30 16:44, r wrote: >> On Jul 30, 4:29 pm, Emmanuel Surleau wrote: >>>> 1.) No need to use "()" to call a function with no arguments. Python >>>> --> "obj.m2().m3()" --ugly >>>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>>> Man, i must admit i really like this, and your code will look so much >>>> cleaner. >>> It has benefits - code does look better. It has also significant cons >>> - it is ambiguous. >>> For instance: >>> >>> a = b >>> >>> Is b a variable or a method called without parameter? >> >> Hello Emanuel, >> Again, who so ever names a method with such a non-descriptive name will >> get whats coming to him. And if you did for some reason use such a >> cryptic name as "b", do yourself (and everyone else) a favor and follow >> it with "()" to denote the method call. Remember when something is >> optional that means you have an option to use it OR not use it. > > I believe his point is that it is ambiguous to the compiler, not humans > reading the code. Python functions and methods are first class objects. > They can be passed around. If they were auto-called, then you could not > do this. Oh my, "r" is still around is he??? And now he's singing the praises of Ruby, the language which he treated as the Devil's Spawn when he first arrived. That's hilarious. But back on topic... "r" has missed the point. It's not that a=b is hard to understand because b is a poor name. The example could have been: def factory_function(): magic = time.time() # or whatever def inner(): return magic return inner my_function = factory_function It's still ambiguous. Does the programmer intend my_function to become factory_function itself, or the output of factory_function? In Python, it's not ambiguous at all -- my_function is set to factory_function, and *not* the output of factory_function, because you haven't called the function. Python's model is consistent and simple: given a function "func", you *always* refer to the function object itself as func and you *always* call it with func(). This applies no matter how many arguments the function takes, or what it returns, or where it is defined. I think it's telling that "r" the fanboy has rejected Python's advantages (simplicity, consistency, unambiguity) in favour of lazily saving two keystrokes. -- Steven From lkrzysiak at gmail.com Fri Jul 31 03:28:54 2009 From: lkrzysiak at gmail.com (=?UTF-8?Q?=C5=81ukasz?=) Date: Fri, 31 Jul 2009 00:28:54 -0700 (PDT) Subject: How to force SAX parser to ignore encoding problems Message-ID: <89c214a4-e846-4101-b0b3-8078eec15114@w41g2000yqb.googlegroups.com> Hi, I have a problem with my XML parser (created with libraries from xml.sax package). When parser finds a invalid character (in CDATA section) for example ?, throws an exception SAXParseException. Is there any way to just ignore this kind of problem. Maybe there is a way to set up parser in less strict mode? I know that I can catch this exception and determine if this is this kind of problem and then ignore this, but I am asking about any global setting. From contact at xavierho.com Fri Jul 31 03:34:32 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 17:34:32 +1000 Subject: problem on socket programming In-Reply-To: <2d56febf0907310033n66afa69ch3a43bcbe078396c5@mail.gmail.com> References: <2d56febf0907310033n66afa69ch3a43bcbe078396c5@mail.gmail.com> Message-ID: <2d56febf0907310034kf012f4br2b8ef56688e2883c@mail.gmail.com> Whoops, posted to the wrong address.... yet again. I deserve some punishment. On Fri, Jul 31, 2009 at 5:33 PM, Xavier Ho wrote: > On Fri, Jul 31, 2009 at 5:25 PM, MalC0de wrote: > >> >> while true : >> >> where's the problem, > > > There. =] > (Btw, it's True, not true.) -------------- next part -------------- An HTML attachment was scrubbed... URL: From efotinis at oohay.com Fri Jul 31 03:38:41 2009 From: efotinis at oohay.com (eliasf) Date: Fri, 31 Jul 2009 10:38:41 +0300 Subject: Test for Pythonwin? In-Reply-To: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> References: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> Message-ID: <1249025993.67838@athprx04> "steve" wrote: > Is there a good way to check if a script is running inside Pythonwin? > Perhaps a property or method that is exposed by that environment? I don't know how foolproof it is, but this works: 'pywin' in sys.modules From piet at cs.uu.nl Fri Jul 31 03:47:34 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 31 Jul 2009 09:47:34 +0200 Subject: Non-blocking read with popen subprocess References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> <824f8092-55b1-4df2-a6b5-74a4b1b1d9ea@i18g2000pro.googlegroups.com> Message-ID: >>>>> Jonathan Gardner (JG) wrote: >JG> On Jul 30, 5:24?pm, Dhanesh wrote: >>> >>> how can I we have a non blocking read ? >JG> See http://docs.python.org/library/popen2.html#flow-control-issues >JG> Note well: In the non-blocking world, you have to use select() or poll >JG> () to get your job done. AFAIK, on Windows these work only on sockets, not on files. I think pipes are in the files camp. Alternatively you can read one of the two in a different thread. >JG> You may want to look at "communicate" (http://docs.python.org/library/ >JG> subprocess.html#popen-objects) which may be what you need. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From masklinn at masklinn.net Fri Jul 31 03:48:24 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 09:48:24 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <3B112998-E96D-40E0-9F2B-1ECC270BB9F5@masklinn.net> <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> Message-ID: <27BA9FDF-8295-41F5-A4E7-170B0D755159@masklinn.net> On 30 Jul 2009, at 23:52 , Jan Kaliszewski wrote: > Dnia 30-07-2009 o 22:41:57 Masklinn > napisa?(a): > On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote: >>> 30-07-2009 o 13:36:49 Masklinn wrote: >>>> On 30 Jul 2009, at 06:04 , alex23 wrote: >>>>> On Jul 30, 1:06 pm, r wrote: >>>>>> 2.) the .each method >>>>>> container.each{|localVar| block} >>>>>> This method can really cleanup some ugly for loops, although i >>>>>> really >>>>>> like the readability of for loops. >>>>> >>>>> map(lambda localVar: , sequence) >>>>> >>>>> or: >>>>> >>>>> def usefully_named_func(var): >>>>> >>>>> return var >>>>> >>>>> transformed = [usefully_named_func(v) for v in sequence] >>>>> >>>> The issue here is of course that `map` and comprehensions are >>>> transformations. `#each` exists for effectful iterations (Ruby >>>> has `#map` for the map operation). So the intent expressed by >>>> `#each` and `map` isn't the same. Furthermore and this is the >>>> most problematic limitation of Python here, `lambda` doesn't >>>> allow complex transformations due to its restrictions, so one has >>>> to switch to named functions which works but isn't sexy (and >>>> tends to lower readability imo). >>> >>> I don't see any real limitation. What's wrong in: >>> >>> for localVar in container: >>> block >>> >> Well what's wrong with using that rather than `map`, `filter` or a >> list comprehension? (and if you don't see what the limitations of >> `lambda` are, you probably very rarely use it) > > I know well about the expression-only-limitation of lambda, fortnately > there is the 'for' loop construct (or, alternatively, you can define > a named function). > > And then, what's wrong with using 'for' rather than 'map', 'filter' > etc.? Nothing, but then again nothing's wrong using C's for either. But I do think that using higher-order functions: * tends to be terser while not going overly terse, it only removes boilerplate * is more composable (it's pretty easy to tack another transformer in your chain, the same way defining iterator/generator transformers and chaining them is simpler than doing the same thing using explicit `for?in`) * it clearly spells the intent of the programmer, while `for` can be anything and everything, and one has to dive into the code to know even the high-level operations performed (is it a basic transformation? A filtering or partitioning? A reduction? An application of side effects?) > Agree, that 'anonymous block syntax' would be nice in some cases, but > I don't see any real limitation caused by lack of it i.e. something > you > can't (all you can only with unreasonable effort). > There are no limitations, but once again there never are. There are no actual limitations to using conditional jumps over iterators either. >>> And ruby's container.each is very similar to Python's iter() > >> Uh? not at all? > > OK, .each is like Python's iter() + some form of iterating over it > ('for' loop or '[i]map'...). Well of course Enumerable#each is similar to map/imap, it's the same core principle. From malc0de.encrypt at gmail.com Fri Jul 31 03:50:19 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 00:50:19 -0700 (PDT) Subject: problem on socket programming References: Message-ID: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> problem with this code solved in win32, but I didn't test it after solved problem on gnu/linux . From bieffe62 at gmail.com Fri Jul 31 03:51:45 2009 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 31 Jul 2009 00:51:45 -0700 (PDT) Subject: socket send References: <55aba832-df6d-455f-bf34-04d37eb061cd@i4g2000prm.googlegroups.com> <41b3e874-e10e-4760-8974-cc4988ba9803@w41g2000yqb.googlegroups.com> Message-ID: <45056c13-9ff6-4a0f-b357-1893d7d03144@s15g2000yqs.googlegroups.com> On Jul 30, 10:16?pm, "Jan Kaliszewski" wrote: > 30-07-2009 o 12:29:24 Francesco Bochicchio wrote: > > > On Jul 30, 5:52?am, NighterNet wrote: > >> I am trying to figure out how to send text or byte in python 3.1. I am > >> trying to send data to flash socket to get there. I am not sure how to > >> work it. > > >> buff= 'id=' , self.id , ':balive=False\n' > >> clientSock.send(buff); > > > Try putting a 'b' before the constant string that you want to send: > > > >>> type(b'123') > > > > It's true. In python '...' literals are for strings ('str' type) and > b'...' literals are for binary data (bytes type). > > Sockets' send() and recv() need binary data ('bytes' objects). > > > or use something like this to convert non constant strings (with only > > ASCII characters) into bytes: > > >>> s = "A non-constant string : %d " % n > > >>> s > > 'A non-constant string : 12 ' > > >>> type(s) > > > > What??? > > 'str' type in Python 3.x IS NOT a type of "non-constant" strings and > IS NOT a type of strings "with only ASCII characters"! > > 'str' type in Python 3.x *is* the type of immutable ('constant') and > Unicode character (Unicode, not only ASCII) strings. It's the same what > 'unicode' type in Python 2.x. > ... unfortunate choice of words and not enough research on my part here. WHat I meant was: if you want to send via socket a constant string, use b"..."; if you want to send via socket a string that you made out of variables (the "non-constant string" ) then you have to convert it in bytes. Since I did not now of the encode method, I tried other solutions, like the one-liner using ord or using the struct module. Obviously, encode is better. My bad :-) Ciao ------- FB From malc0de.encrypt at gmail.com Fri Jul 31 04:02:33 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 01:02:33 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> Message-ID: now I've been encountered to some errors with this snippet : import sys from socket import * serverHost = 'localhost' serverPort = 50007 message = ['Hello network world'] if len(sys.argv) > 1: serverHost = sys.argv[1] if len(sys.argv) > 2: message = sys.argv[2:] sockobj = socket(AF_INET,SOCK_STREAM) sockobj.connect((serverHost,serverPort)) for line in message: sockobj.send(line) data = sockobj.recv(1024) print 'Client recieved :',repr(data) sockobj.close() thanks if someone can solve my problem . - malc0de. From matt.urry at googlemail.com Fri Jul 31 04:04:08 2009 From: matt.urry at googlemail.com (ma3mju) Date: Fri, 31 Jul 2009 01:04:08 -0700 (PDT) Subject: Processes not exiting Message-ID: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Hi all, I'm having trouble with multiprocessing I'm using it to speed up some simulations, I find for large queues when the process reaches the poison pill it does not exit whereas for smaller queues it works without any problems. Has anyone else had this trouble? Can anyone tell me a way around it? The code is in two files below. Thanks Matt parallel.py =================================================== import GaussianProcessRegression as GP import numpy as np import networkx as nx import pickle import multiprocessing ############################################################################################ # Things You Can Change ############################################################################################ #savefiles savefile = "wattsdata2" graphfile = "wattsgraphs2" #sample sizes num_graphs = 5 num_sets_of_data = 10 #other things... intervals = np.ceil(np.logspace(-2,1,50)*500) noise = [np.sqrt(0.1),np.sqrt(0.01),np.sqrt(0.001),np.sqrt(0.0001)] ############################################################################################ #generate graphs graphs = [] for i in range(0,num_graphs): graphs.append(nx.watts_strogatz_graph(500,5,0.01)) #save them for later reference filehandler = open(graphfile,'w') pickle.dump(graphs,filehandler,-1) filehandler.close() #queues easy_work_queue = multiprocessing.Queue() hard_work_queue = multiprocessing.Queue() result_queue = multiprocessing.Queue() #construct the items in the hard queue l=0 for j in range(0,len(intervals)): for i in range(0,len(noise)): for k in range(0,num_graphs): if int(intervals[j]) <=4000: easy_work_queue.put({'datapt': l,'graph': graphs [k],'noise': noise[i],'number_of_sets_of_data': num_sets_of_data,'number_of_data_points':int(intervals[j])}) else: hard_work_queue.put({'datapt': l,'graph': graphs [k],'noise': noise[i],'number_of_sets_of_data': num_sets_of_data,'number_of_data_points':int(intervals[j])}) l+=1 #get number of cores and set the number on concurrent processes num_hard_workers = 2 num_workers = multiprocessing.cpu_count()*1.5 easy_workers = [] hard_workers = [] #add poison pill for each worker and create the worker for i in range(0,num_workers-num_hard_workers): easy_work_queue.put(None) easy_workers.append(multiprocessing.Process (target=GP.RandomWalkGeneralizationErrorParallel,args= (easy_work_queue,result_queue,))) for i in range(0,num_hard_workers): hard_work_queue.put(None) hard_workers.append(multiprocessing.Process (target=GP.RandomWalkGeneralizationErrorParallel,args= (hard_work_queue,result_queue,))) #run all workers for worker in hard_workers: worker.start() for worker in easy_workers: worker.start() #wait for easy workers to finish for worker in easy_workers: worker.join() print('worker joined') #set off some of the easy workers on the hard work (maybe double number of hard) for i in range(0,num_hard_workers): hard_work_queue.put(None) hard_workers.append(multiprocessing.Process (target=GP.RandomWalkGeneralizationErrorParallel,args= (hard_work_queue,result_queue,))) #wait for all hard workers to finish for worker in hard_workers: worker.join() #construct data from the mess in the result queue tempdata = np.zeros(l) while not result_queue.empty(): data = result_queue.get() tempdata[data[0]] = data[1] finaldata = tempdata.reshape((len(intervals),len(noise),num_graphs)) np.save(savefile,finaldata) ======================================================= GaussianProcessRegression.py ======================================================= import CovarianceFunction as CF import networkx as nx import numpy as np import scipy.linalg as sp #fortran code from lapack-blas (hopefully when scipy updated this wont be needed) import dtrsv #to use more than one core import multiprocessing #Currently we assume Gaussian noise TODO change to general noise #Assume 0 mean TODO change to general mean Gaussian Process class GaussianProcessRegression: def __init__(self,covariance_function,sigma): #a covariance function object defined in CovarianceFunction class #note this uses the parent class but any children can be used self.C = covariance_function #a list of pts that are known and their values self.pts = [] self.vals = [] #the inverse of K as defined in #@book{coolen05:theoryofneural, #ISBN = {0-19-853024-2}, #publisher = {Oxford University Press, USA}, #author = {Coolen, A. C. C. and K{\"{u}}hn, R. and Sollich, P.}, #title = {Theory of neural information processing systems}, #year = {2005}, #} self.K = np.array([]) #gaussian noise variable self.sigma = float(sigma) self.cholL = np.array([]) def add_data_points(self,points,vals): #add all points to list self.pts += points self.vals += vals arraysize = len(self.pts) #construct K K = np.zeros((arraysize,arraysize)) #for speed pts = self.pts between_points = self.C.between_points if len(self.K): K[:-1,:-1] = self.K for i in xrange(0,arraysize): for j in xrange(arraysize-len(points),arraysize): K[i,j] = between_points(pts[i],pts[j]) K[j,i] = K[i,j] K[arraysize-len(points):arraysize,arraysize-len (points):arraysize] = K[arraysize-len(points):arraysize,arraysize-len (points):arraysize] + self.sigma**2 * np.eye(len(points)) self.K = K #calculate the prediction of a point based on data previously given def point_prediction(self,points): mean = [] variance =[] arraysize = len(self.pts) #cholesky #if self.cholL.shape[0] < arraysize: L=np.linalg.cholesky(self.K) # self.cholL = L #else: # L = self.cholL alpha = sp.cho_solve((L,1),self.vals) #create L in banded form k=np.zeros((arraysize,len(points))) ################################################################## #for speed get ref to functions im going to use and save them between_points = self.C.between_points pts = self.pts dot = np.dot ################################################################## for j in xrange(0,len(points)): #create k for i in xrange(0,arraysize): k[i,j] = between_points(pts[i],points[j]) #calculate mean and variance #call the command for forward substitution ###############fortran call####################################### v = dtrsv.dtrsv('L','N',arraysize,L,k) ################################################################## #result mean=dot(alpha,k) for i in xrange(0,len(points)): variance.append(between_points(points[i],points[i]) - dot(v [:,i],v[:,i])) #return it in dictionary form return {'mean':mean,'variance':variance} # calculate the error for data given, where function is a vector # of the function evaluated at a sufficiently large number of points # that the GPregression has been trying to learn def error(self,function): total = 0 #sum up variances result = self.point_prediction(function[::2]) total = np.sum(result['variance']) total = (1/float(len(function)/2))*total return total #clear what has been learnt so far def clear(self): self.pts = [] self.vals = [] self.K = np.array([]) #calculate the average error for a function defined in function when give #number_of_examples examples def average_error_over_samples(self,function, sample_size, number_of_examples): avg = 0 numberofpoints = len(function)/2 for i in range(0,sample_size): self.clear() #generate points of the function permpts = np.random.randint (0,numberofpoints,number_of_examples) #create the vectors pts = [] vals = [] for j in range(0,number_of_examples): pts.append(function[permpts[j]*2]) vals.append(function[permpts[j]*2+1]) #learn these points self.add_data_points(pts,vals) #print("points added") avg = avg + self.error(function) avg = avg/sample_size return avg #calculate the average error over functions over data of size number_of_data_points for MOST cases this is #also the generalization error a summary of which and approximations to can be found in: #@inproceedings{Sollich99learningcurves, #booktitle = {Neural Computation}, #author = {Sollich, P.}, #title = {Learning curves for Gaussian process regression: Approximations and bounds}, #pages = {200-2}, #year = {1999}, #} def emprical_average_error_over_functions (self,number_of_functions,number_of_sets_of_data,number_of_data_points,function_detail =0,progress=0): avg = 0 step = float(100)/number_of_functions for i in range(0,number_of_functions): if progress: print step*float(i),"%" if function_detail: fx = self.C.generate_function (self.sigma,function_detail) else: fx = self.C.generate_function(self.sigma) avg = self.average_error_over_samples (fx,number_of_sets_of_data,number_of_data_points)+avg avg = avg / number_of_functions return avg def average_error_over_functions (self,number_of_sets_of_data,number_of_data_points,function_detail=0): if function_detail: fx = self.C.generate_function (self.sigma,function_detail) else: fx = self.C.generate_function(self.sigma) avg = self.average_error_over_samples (fx,number_of_sets_of_data,number_of_data_points) return(avg) def function_prediction(self,pts): temp = self.point_prediction(pts) return {'func':temp['mean'],'varpos':temp ['variance'],'varneg':-temp['variance']} ######################################################################################################################################################### #Functions not contained in a class ######################################################################################################################################################### #function to calculate the generalization error for a RandomWalk kernel averaging over graphs graphs def RandomWalkGeneralizationError (noise,graphs,number_of_sets_of_data,number_of_data_points,a=2,p=10): graph_specific = np.zeros(len(graphs)) avg = 0 for i in range(0,len(graphs)): rw = CF.RandomWalk(a,p,graphs[i]) GP = GaussianProcessRegression(rw,noise) graph_specific[i] = GP.average_error_over_functions (number_of_sets_of_data,number_of_data_points) avg = np.sum(graph_specific)/len(graphs) return avg, graph_specific #as above but using queues to create parallel architechture def RandomWalkGeneralizationErrorParallel (work_queue,result_queue,a=2,p=10): while True: input = work_queue.get() if input is None: print "poison" break print 'this should not appear' print input['datapt'], ' ', input['number_of_data_points'] rw=CF.RandomWalk(a,p,input['graph']) GP = GaussianProcessRegression(rw,input['noise']) err = GP.average_error_over_functions(input ['number_of_sets_of_data'],input['number_of_data_points']) result_queue.put([input['datapt'],err]) print 'here' return From matt.urry at googlemail.com Fri Jul 31 04:06:25 2009 From: matt.urry at googlemail.com (ma3mju) Date: Fri, 31 Jul 2009 01:06:25 -0700 (PDT) Subject: Processes not exiting References: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Message-ID: <28791ddd-5270-4a74-9c86-8fe719b34069@d4g2000yqa.googlegroups.com> Sorry ###############fortran call####################################### is meant to be ###############fortran call####################################### Matt From clp2 at rebertia.com Fri Jul 31 04:07:41 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 01:07:41 -0700 Subject: problem on socket programming In-Reply-To: References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> Message-ID: <50697b2c0907310107v40f20d51ic5852303e545cd77@mail.gmail.com> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: > now I've been encountered to some errors with this snippet : And these errors are? Provide error messages and full tracebacks. Cheers, Chris -- http://blog.rebertia.com From masklinn at masklinn.net Fri Jul 31 04:08:33 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 10:08:33 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <200907301757.48761.kyrie@uh.cu> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: On 30 Jul 2009, at 23:57 , Luis Zarrabeitia wrote: > I'd like to ask, what "container.each" is, exactly? It looks like a > function > call (as I've learned a few posts ago), but, what are its arguments? > How the > looping "works"? Does it receive a "code" object that it has to > execute? > Is .each some kind of magic keyword? (This has little to do with > python or > the current thread, so feel free to reply off-list if you want to...) #each is simply a method that takes a function (called blocks in ruby). One could call it a higher-order method I guess. It's an implementation of the concept of internal iteration: instead of collections yielding iterator objects, and programmers using those through specially-built iteration constructs (e.g. `for?in`), collections control iteration over themselves (the iteration is performed "inside" the collection, thus the "internal" part) and the programmer provides the operations to perform at each iterative step through (usually) a function. In Python (assuming we had anonymous defs and an each method on lists), the following loop: for item in some_list: do_something(item) do_something_else(item) some_list.each((def (item): do_something(item) do_something_else(item) )) There's absolutely nothing magic there, it's simply using anonymous functions and method calls (SmallTalk initiated this approach in the 70s, and actually went much, much further than Ruby as it did away not only with `for?in` but also with `if?else` and `while` and a bunch of other stuff). Now as IV pointed out, #each isn't the most interesting usage of blocks/anonymous functions (though I do like it, because it gets rid of no less than two keywords? even if Ruby reintroduced them as syntactic sugar), higher-order functions (functions which act on other functions) are (among other things) ways to create new control structures without having to extend the core language (so are lisp- style macros, by the way). Of course Python does have higher-order functions (functions are first- class objects, so it's possible and frequent to have functions act on other functions), but since Python doesn't have anonymous functions that usage tends to be a bit too verbose beyond simple cases (also, of course, this kind of usages is neither in the "genes" nor in the stdlib). In closing, IV has an example of how blocks make `with` unnecessary in Ruby (the functionality can be implemented at the library level rather than the language one). Generally, anonymous functions in OO languages are a way to inject behavior into third-party methods/objects. Kind-of a first-class Visitor support. -m PS: there's actually a bit of syntactic magic in Ruby's blocks, and it's one of the things I hate in the language, but it's not relevant to the role of blocks, and unnecessary to it: SmallTalk has no magical syntax). From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 04:13:50 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 08:13:50 GMT Subject: Confessions of a Python fanboy References: Message-ID: <008e82e8$0$9756$c3e8da3@news.astraweb.com> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > That and the fact that I couldn't stop laughing for long enough to learn > any more when I read in the Pragmatic Programmer's Guide that "Ruby, > unlike less flexible languages, lets you alter the value of a constant." > Yep, as they say "Bug" = "Undocumented feature"! That's no different from Python's "constant by convention". We don't even get a compiler warning! On the other hand, we don't have to prefix names with @ and @@, and we don't have the compiler trying to *guess* whether we're calling a function or referring to a variable. Somebody who knows more Ruby than me should try writing the Zen of Ruby. Something like: Line noise is beautiful. Simplicity is for the simple. Complicated just proves we're smart. Readability only matters to the schmuck who has to maintain our code. Special cases require breaking the rules. In the face of ambiguity, try to guess. Go on, what could go wrong? The more ways to do it, the better. Although I'm sure Ruby has its good points. I'm not convinced anonymous code blocks are one of them though. -- Steven From hendrik at microcorp.co.za Fri Jul 31 04:18:15 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 31 Jul 2009 10:18:15 +0200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: <7dca4iF2bgbn5U1@mid.individual.net> References: <7dca4iF2bgbn5U1@mid.individual.net> Message-ID: <200907311018.15784.hendrik@microcorp.co.za> On Thursday 30 July 2009 03:09:14 greg wrote: > Hendrik van Rooyen wrote: > > And if code is data, where is Pythons ALTER statement? > > class Duck: > > def quack(self): > print "Quack!" > > def moo(): > print "Moo!" > > def ALTER(obj, name, TO_PROCEED_TO): > setattr(obj, name, TO_PROCEED_TO) > > d = Duck() > ALTER(d, 'quack', TO_PROCEED_TO = moo) > d.quack() Nice, and I really appreciate the Duck, but (there is always a but): 1) That is a function, not a statement. 2) The original changed a jump target address from one address to another, and did not need the heavy machinery of object oriented code. So what you are doing is not quite the same. It is actually not really needed in python, as one can pass functions around, so that you can call different things instead of jumping to them: thing_to_call = moo thing_to_call() Does the equivalent without user level OO. The original ALTER statement found use in building fast state machines, where the next thing to do was set up by the current state. In python one simply returns the next state. The equivalent python code is easier to read as it is obvious what is happening - The COBOL source was more obscure, as any jump could have been altered, and you could not see that until you have read further on in the program, where the ALTER statement was. - Hendrik From contact at xavierho.com Fri Jul 31 04:21:45 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 18:21:45 +1000 Subject: Confessions of a Python fanboy In-Reply-To: References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn wrote: > ... but since Python doesn't have anonymous functions that usage > tends to be a bit too verbose ... > Sorry to interrupt, but wouldn't lambda in Python be considered as 'anonymous functions'? -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Fri Jul 31 04:25:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 01:25:37 -0700 Subject: Confessions of a Python fanboy In-Reply-To: <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> Message-ID: <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho wrote: > On Fri, Jul 31, 2009 at 6:08 PM, Masklinn wrote: >> >> ... but since Python doesn't have anonymous functions that usage >> tends to be a bit too verbose ... > > Sorry to interrupt, but wouldn't lambda in Python be considered as > 'anonymous functions'? I believe "full" anonymous functions was intended by the author. lambdas are limited to a single expression. "Full" anonymous functions would be allowed to contain multiple statements. Cheers, Chris -- http://blog.rebertia.com From javier.collado at gmail.com Fri Jul 31 04:27:01 2009 From: javier.collado at gmail.com (Javier Collado) Date: Fri, 31 Jul 2009 10:27:01 +0200 Subject: Non-blocking read with popen subprocess In-Reply-To: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> Message-ID: Hello, According to my experience and from what I've read in other threads, subprocess isn't easy to use for interactive tasks. I don't really know, but maybe it wasn't even designed for that at all. On the other hand, pexpect seems to work fine for interactive use and even provides a method for nonblocking reads, so I think that you should consider to take a look at it: http://pexpect.sourceforge.net/pexpect.html#spawn-read_nonblocking Best regards, Javier 2009/7/31 Dhanesh : > Hi , > > I am trying to use subprocess popen on a windows command line > executable with spits messages on STDOUT as well as STDIN. Code > snippet is as below :- > ########################################################################## > sOut="" > ? ?sErr="" > ? ?javaLoaderPath = os.path.join("c:\\","Program Files","Research In > Motion","BlackBerry JDE 4.7.0","bin","Javaloader.exe") > ? ?cmd = [javaLoaderPath,'-u','load','helloworld.jad'] > ? ?popen = subprocess.Popen > (cmd,bufsize=256,shell=False,stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > ? ?while True: > ? ? ? ?sErr = sErr + popen.stderr.read(64) > ? ? ? ?sOut = sOut + popen.stdout.read(64)------------------> Blocks > here > ? ? ? ?if None != popen.poll(): > ? ? ? ? ? ?break; > ########################################################################## > > I observed that python scripts blocks at stdout read on the other hand > when I do not create a child stdout PIPE ?say ?" stdout=None" , things > seems to be working fine. > > how can I we have a non blocking read ? > And why does stdout block even where data is available to be read > ( which seem to be apparent when stdout=None, and logs appear on > parents STDOUT) ? > > -- > http://mail.python.org/mailman/listinfo/python-list > From contact at xavierho.com Fri Jul 31 04:31:43 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 18:31:43 +1000 Subject: Confessions of a Python fanboy In-Reply-To: <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> Message-ID: <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> On Fri, Jul 31, 2009 at 6:25 PM, Chris Rebert wrote: > I believe "full" anonymous functions was intended by the author. > lambdas are limited to a single expression. "Full" anonymous functions > would be allowed to contain multiple statements. > > Cheers, but what about this: def goBig(x): while True: x = x ** 2 yield x for result in goBig(10): if result > 10 ** 100: break print result It's a silly example, but wouldn't goBig(10) in this example be a "full anonymous function"? Just being curious. -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Fri Jul 31 04:38:16 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 01:38:16 -0700 Subject: Confessions of a Python fanboy In-Reply-To: <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> Message-ID: <50697b2c0907310138u1ea485f9yf9bac0095e09927d@mail.gmail.com> On Fri, Jul 31, 2009 at 1:31 AM, Xavier Ho wrote: > On Fri, Jul 31, 2009 at 6:25 PM, Chris Rebert wrote: >> >> I believe "full" anonymous functions was intended by the author. >> lambdas are limited to a single expression. "Full" anonymous functions >> would be allowed to contain multiple statements. >> > Cheers, but what about this: > > ?def goBig(x): > ??? while True: > ??????? x = x ** 2 > ??????? yield x > > for result in goBig(10): > ??? if result > 10 ** 100: > ??????? break > ??? print result > > It's a silly example, but wouldn't goBig(10) in this example be a "full > anonymous function"? No, because it has a name, namely "goBig"; this obviously prevents it from being "anonymous". For comparison, note how the function in the following example is never given a name, and is thus anonymous: >>> (lambda x: x+5)(6) 11 Cheers, Chris -- http://blog.rebertia.com From malc0de.encrypt at gmail.com Fri Jul 31 04:38:21 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 01:38:21 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> Message-ID: <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> On Jul 31, 12:07?pm, Chris Rebert wrote: > On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: > > now I've been encountered to some errors with this snippet : > > And these errors are? Provide error messages and full tracebacks. > > Cheers, > Chris > --http://blog.rebertia.com these are errors : Traceback (most recent call last): File "echo-client.py", line 11, in ", line 1, in connect socket.error: (10061, 'Connection refused') From contact at xavierho.com Fri Jul 31 04:41:17 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 18:41:17 +1000 Subject: Confessions of a Python fanboy In-Reply-To: <50697b2c0907310138u1ea485f9yf9bac0095e09927d@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> <2d56febf0907310131x6d83f647i173f084c240a5ca3@mail.gmail.com> <50697b2c0907310138u1ea485f9yf9bac0095e09927d@mail.gmail.com> Message-ID: <2d56febf0907310141m15bfc526i3c916f5a2b48f544@mail.gmail.com> On Fri, Jul 31, 2009 at 6:38 PM, Chris Rebert wrote: > No, because it has a name, namely "goBig"; this obviously prevents it > from being "anonymous". > > For comparison, note how the function in the following example is > never given a name, and is thus anonymous: > >>> (lambda x: x+5)(6) > 11 > Oh, I see. I was thinking in terms of using a variable to "give the function a name". In that case, I understand. Thanks again for bearing me with my random thoughts. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hendrik at microcorp.co.za Fri Jul 31 04:47:24 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 31 Jul 2009 10:47:24 +0200 Subject: Does python have the capability for driver development ? In-Reply-To: <4A719E2D.708@ieee.org> References: <4A719E2D.708@ieee.org> Message-ID: <200907311047.25029.hendrik@microcorp.co.za> On Thursday 30 July 2009 15:20:45 Dave Angel wrote: > As far as I know, nobody has yet built a microcode implementation of a > Python VM (Virtual Machine). Nor have I seen one for the Java VM. Atmel has made an ARM that has support for Java Bytecode. AT91SAM9X512 and friends (smaller versions) - Hendrik From greg at cosc.canterbury.ac.nz Fri Jul 31 05:02:08 2009 From: greg at cosc.canterbury.ac.nz (greg) Date: Fri, 31 Jul 2009 21:02:08 +1200 Subject: If Scheme is so good why MIT drops it? In-Reply-To: References: <7dca4iF2bgbn5U1@mid.individual.net> Message-ID: <7dfq78F2c8bolU1@mid.individual.net> Hendrik van Rooyen wrote: > The COBOL source was > more obscure, as any jump could have been altered, > and you could not see that until you have read further > on in the program, where the ALTER statement was. Well, in Python you can pretty much replace any function with any other function, so you can obfuscate things just as much if you really want! -- Greg From jeanmichel at sequans.com Fri Jul 31 05:20:47 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 31 Jul 2009 11:20:47 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <80839e72-9df4-4590-b22d-4c5c829e37b6@h11g2000yqb.googlegroups.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <80839e72-9df4-4590-b22d-4c5c829e37b6@h11g2000yqb.googlegroups.com> Message-ID: <4A72B76F.4020805@sequans.com> r wrote: > The purpose of his thread was to get feedback on how Python > and Ruby ideas could be cumulated into the best high level language. > And being that i am the BDFL of the "Confessions of a Python Fanboy" > thread, you have my personal permission to continue on with this > subject matter..., > > Challenging even the most accepted mechanisms of a language is a proof of intelligence. Descartes's doubt method : "The basic strategy of /Descartes/ 's method of doubt is to defeat skepticism on its own ground. Begin by doubting the truth of everything?not only the evidence of the senses and the more extravagant cultural presuppositions, but even the fundamental process of reasoning itself. If any particular truth about the world can survive this extreme skeptical challenge, then it must be truly indubitable and therefore a perfectly certain foundation for knowledge" So let's make the method call parenthesis a "truly indubitable and therefore a perfectly certain foundation". Those who want to remove the parenthesis on python method calls raise the hand ! JM From pyvault2.6 at gmail.com Fri Jul 31 05:25:17 2009 From: pyvault2.6 at gmail.com (learner learner) Date: Fri, 31 Jul 2009 14:55:17 +0530 Subject: file comparison Message-ID: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Hi all, I want to compare two text files line by line and eliminate the matching/repeated line and store the unmatched/leftout lines into a third file or overwrite into one of them. regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From lkrzysiak at gmail.com Fri Jul 31 05:29:57 2009 From: lkrzysiak at gmail.com (=?UTF-8?Q?=C5=81ukasz?=) Date: Fri, 31 Jul 2009 02:29:57 -0700 (PDT) Subject: How to force SAX parser to ignore encoding problems References: <89c214a4-e846-4101-b0b3-8078eec15114@w41g2000yqb.googlegroups.com> Message-ID: <5587c712-1a15-4666-a5d9-5c99ace648f6@a13g2000yqc.googlegroups.com> On 31 Lip, 09:28, ?ukasz wrote: > Hi, > I have a problem with my XML parser (created with libraries from > xml.sax package). When parser finds a invalid character (in CDATA > section) for example , After sending this message I noticed that example invalid characters are not displaying on some platforms :) From shearichard at gmail.com Fri Jul 31 05:30:36 2009 From: shearichard at gmail.com (northof40) Date: Fri, 31 Jul 2009 02:30:36 -0700 (PDT) Subject: Python 2.6 in shared/VPS environment References: <6babf860-0161-4c1a-82f8-69e03dc53da0@p36g2000prn.googlegroups.com> Message-ID: <923e59f8-638c-4c2e-8b6a-b6e13d415424@j9g2000prh.googlegroups.com> Ignore this question. Managed to get Amazon Web Services going and have installed Python 2.6 on there. Thanks for your eyeballs time. On Jul 31, 7:09?pm, northof40 wrote: > Hi - I'd really like to have access to Python 2.6 to try something > out. It needs to be on Linux/Unix machine. I don't mind paying but > whichever way I turn I find Python 2.4 is the standard installation. > > Anyone know of anyone who offers this out of the box ? Or got any > smart way of achieving it ? > > thanks > > R. From contact at xavierho.com Fri Jul 31 05:34:58 2009 From: contact at xavierho.com (Xavier Ho) Date: Fri, 31 Jul 2009 19:34:58 +1000 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: <2d56febf0907310234h5bda3a66m66f71201efb92fe8@mail.gmail.com> On Fri, Jul 31, 2009 at 7:25 PM, learner learner wrote: > Hi all, > > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a third > file or overwrite into one of them. > Sounds like homework to me. Why not look into: - the file() function and its methods. It's in the documentation. -------------- next part -------------- An HTML attachment was scrubbed... URL: From gh at ghaering.de Fri Jul 31 05:35:03 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Fri, 31 Jul 2009 11:35:03 +0200 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: learner learner wrote: > Hi all, > > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a > third file or overwrite into one of them. gl & hf! From clp2 at rebertia.com Fri Jul 31 05:37:25 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 31 Jul 2009 02:37:25 -0700 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: <50697b2c0907310237y2c79e938g70952e966b74ce87@mail.gmail.com> On Fri, Jul 31, 2009 at 2:25 AM, learner learner wrote: > Hi all, > > I?want to compare two text files line by line and eliminate the > matching/repeated?line and store the unmatched/leftout lines into?a third > file or overwrite into one of them. See the `difflib` module: http://docs.python.org/library/difflib.html Cheers, Chris -- http://blog.rebertia.com From duncan.booth at invalid.invalid Fri Jul 31 05:41:34 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 31 Jul 2009 09:41:34 GMT Subject: Generators through the C API References: Message-ID: Lucas P Melo wrote: > Hello, I'm a total noob about the C API. Is there any way to create a > generator function using the C API? I couldn't find anything like the > 'yield' keyword in it. > > Thanks in advance. You define a new type with tp_flags including Py_TPFLAGS_HAVE_ITER. Anything that would be a local variable in your generator needs to become an attribute in the type. The tp_init initialization function should contain all the code up to the first yield, tp_iter should return self and tp_iternext should execute code up to the next yield. -- Duncan Booth http://kupuguy.blogspot.com From masklinn at masklinn.net Fri Jul 31 05:49:31 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 11:49:31 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> Message-ID: On 31 Jul 2009, at 10:25 , Chris Rebert wrote: > On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho > wrote: >> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn >> wrote: >>> >>> ... but since Python doesn't have anonymous functions that >>> usage >>> tends to be a bit too verbose ... >> >> Sorry to interrupt, but wouldn't lambda in Python be considered as >> 'anonymous functions'? > > I believe "full" anonymous functions was intended by the author. > lambdas are limited to a single expression. Yes, and they're limited to a single *expression*, so before Python 3, lambda: print "foo" is out. Likewise, it isn't possible to have an if/ else statement within a lambda (though a ternary is ok), or a with, ? since Python statements aren't expressions. From iainking at gmail.com Fri Jul 31 05:54:50 2009 From: iainking at gmail.com (Iain King) Date: Fri, 31 Jul 2009 02:54:50 -0700 (PDT) Subject: Confessions of a Python fanboy References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> <008e7846$0$9756$c3e8da3@news.astraweb.com> Message-ID: On Jul 31, 8:28?am, Steven D'Aprano wrote: > On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote: > > On 2009-07-30 16:44, r wrote: > >> On Jul 30, 4:29 pm, Emmanuel Surleau wrote: > >>>> 1.) No need to use "()" to call a function with no arguments. Python > >>>> --> ?"obj.m2().m3()" --ugly > >>>> ? ?Ruby --> ?"obj.m1.m2.m3" ?-- sweeet! > >>>> Man, i must admit i really like this, and your code will look so much > >>>> cleaner. > >>> It has benefits - code does look better. It has also significant cons > >>> - it is ambiguous. > >>> For instance: > > >>> a = b > > >>> Is b a variable or a method called without parameter? > > >> Hello Emanuel, > >> Again, who so ever names a method with such a non-descriptive name will > >> get whats coming to him. And if you did for some reason use such a > >> cryptic name as "b", do yourself (and everyone else) a favor and follow > >> it with "()" to denote the method call. Remember when something is > >> optional that means you have an option to use it OR not use it. > > > I believe his point is that it is ambiguous to the compiler, not humans > > reading the code. Python functions and methods are first class objects. > > They can be passed around. If they were auto-called, then you could not > > do this. > > Oh my, "r" is still around is he??? And now he's singing the praises of > Ruby, the language which he treated as the Devil's Spawn when he first > arrived. That's hilarious. > > But back on topic... "r" has missed the point. It's not that a=b is hard > to understand because b is a poor name. The example could have been: > > def factory_function(): > ? ? magic = time.time() ?# or whatever > ? ? def inner(): > ? ? ? ? return magic > ? ? return inner > > my_function = factory_function > > It's still ambiguous. Does the programmer intend my_function to become > factory_function itself, or the output of factory_function? Not only that - does 'return inner' return the function inner or the result of function inner? How does ruby pass a function as an object? Iain From piet at cs.uu.nl Fri Jul 31 06:01:09 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 31 Jul 2009 12:01:09 +0200 Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> Message-ID: >>>>> MalC0de (M) wrote: >M> On Jul 31, 12:07?pm, Chris Rebert wrote: >>> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: >>> > now I've been encountered to some errors with this snippet : >>> >>> And these errors are? Provide error messages and full tracebacks. >>> >>> Cheers, >>> Chris >>> --http://blog.rebertia.com >M> these are errors : >M> Traceback (most recent call last): >M> File "echo-client.py", line 11, in M> sockobj.connect((serverHost,serverPort)) >M> File "", line 1, in connect >M> socket.error: (10061, 'Connection refused') Is your server running? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Fri Jul 31 06:27:31 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 31 Jul 2009 12:27:31 +0200 Subject: Processes not exiting References: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Message-ID: >>>>> ma3mju (m) wrote: >m> Hi all, >m> I'm having trouble with multiprocessing I'm using it to speed up some >m> simulations, I find for large queues when the process reaches the >m> poison pill it does not exit whereas for smaller queues it works >m> without any problems. Has anyone else had this trouble? Can anyone >m> tell me a way around it? The code is in two files below. How do you know it doesn't exit. You haven't shown any of your output. I did discover a problem in your code, but it should cause an exception: >m> #set off some of the easy workers on the hard work (maybe double >m> number of hard) >m> for i in range(0,num_hard_workers): >m> hard_work_queue.put(None) >m> hard_workers.append(multiprocessing.Process >m> (target=GP.RandomWalkGeneralizationErrorParallel,args= >m> (hard_work_queue,result_queue,))) >m> #wait for all hard workers to finish >m> for worker in hard_workers: >m> worker.join() Here you create new hard workers, but you never start them. The join should then give an exception when it reaches these. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From masklinn at masklinn.net Fri Jul 31 06:32:56 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 12:32:56 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <23d07b28-8cb9-4297-bd02-af4f59f99334@n11g2000yqb.googlegroups.com> <008e7846$0$9756$c3e8da3@news.astraweb.com> Message-ID: <1AD26B34-17FA-4315-8A4F-B3F3D09F7D29@masklinn.net> On 31 Jul 2009, at 11:54 , Iain King wrote: > On Jul 31, 8:28 am, Steven D'Aprano cybersource.com.au> wrote: >> On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote: >>> On 2009-07-30 16:44, r wrote: >>>> On Jul 30, 4:29 pm, Emmanuel Surleau >>>> wrote: >>>>>> 1.) No need to use "()" to call a function with no arguments. >>>>>> Python >>>>>> --> "obj.m2().m3()" --ugly >>>>>> Ruby --> "obj.m1.m2.m3" -- sweeet! >>>>>> Man, i must admit i really like this, and your code will look >>>>>> so much >>>>>> cleaner. >>>>> It has benefits - code does look better. It has also significant >>>>> cons >>>>> - it is ambiguous. >>>>> For instance: >> >>>>> a = b >> >>>>> Is b a variable or a method called without parameter? >> >>>> Hello Emanuel, >>>> Again, who so ever names a method with such a non-descriptive >>>> name will >>>> get whats coming to him. And if you did for some reason use such a >>>> cryptic name as "b", do yourself (and everyone else) a favor and >>>> follow >>>> it with "()" to denote the method call. Remember when something is >>>> optional that means you have an option to use it OR not use it. >> >>> I believe his point is that it is ambiguous to the compiler, not >>> humans >>> reading the code. Python functions and methods are first class >>> objects. >>> They can be passed around. If they were auto-called, then you >>> could not >>> do this. >> >> Oh my, "r" is still around is he??? And now he's singing the >> praises of >> Ruby, the language which he treated as the Devil's Spawn when he >> first >> arrived. That's hilarious. >> >> But back on topic... "r" has missed the point. It's not that a=b is >> hard >> to understand because b is a poor name. The example could have been: >> >> def factory_function(): >> magic = time.time() # or whatever >> def inner(): >> return magic >> return inner >> >> my_function = factory_function >> >> It's still ambiguous. Does the programmer intend my_function to >> become >> factory_function itself, or the output of factory_function? > > Not only that - does 'return inner' return the function inner or the > result of function inner? > > How does ruby pass a function as an object? Ruby doesn't have functions as such. It has methods and blocks (which would be anonymous functions). `def` always creates methods. To get a (bound) method object, you simply call `method` on an instance e.g. `foo.method(:bar)` is equivalent to `foo.bar` in Python (it returns a bound method object without calling it). Blocks are usually created as part of the calls: `foo.bar {do_something}` the part between braces (braces included) is a block, which will be passed to the method `bar`. Sadly, Ruby's blocks are magical syntax (doesn't mean they have to be, in Smalltalk there's nothing magical about blocks for instance) so you can't just do `foo = {do_something}`, you have to turn them into `Proc` objects with the `proc` constructor (or `lambda`, it's equivalent and looks better so I'll use that): `foo = lambda {do_something}`. If you use the magical syntax previously shown, Ruby handles the turning of a block into an actual `Proc` instance. And since Ruby doesn't have a `()` operator, it uses a method instead (`#call`), so you simply do `foo.call` to execute the proc and get its value. All in all, much like Smalltalk, Ruby tends not to favor raw functions the way Python does, so a direct translation of the Python code doesn't make much sense. From python at mrabarnett.plus.com Fri Jul 31 06:34:29 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 11:34:29 +0100 Subject: Processes not exiting In-Reply-To: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> References: <6cfb2f39-c6d6-4d2a-882c-18f21addf206@a13g2000yqc.googlegroups.com> Message-ID: <4A72C8B5.1030309@mrabarnett.plus.com> ma3mju wrote: > Hi all, > > I'm having trouble with multiprocessing I'm using it to speed up some > simulations, I find for large queues when the process reaches the > poison pill it does not exit whereas for smaller queues it works > without any problems. Has anyone else had this trouble? Can anyone > tell me a way around it? The code is in two files below. > [snip] > > #get number of cores and set the number on concurrent processes > num_hard_workers = 2 > num_workers = multiprocessing.cpu_count()*1.5 > easy_workers = [] > hard_workers = [] > #add poison pill for each worker and create the worker > for i in range(0,num_workers-num_hard_workers): > easy_work_queue.put(None) > easy_workers.append(multiprocessing.Process > (target=GP.RandomWalkGeneralizationErrorParallel,args= > (easy_work_queue,result_queue,))) > for i in range(0,num_hard_workers): > hard_work_queue.put(None) > hard_workers.append(multiprocessing.Process > (target=GP.RandomWalkGeneralizationErrorParallel,args= > (hard_work_queue,result_queue,))) > You have 2 hard workers and ceil(CPU_count * 1.5) - 2 easy workers. What if the number of CPUs was 1? That would give 2 hard and 0 easy! Also, I recommend that you put only 1 'poison pill' in each queue and have the workers put it back when they see it. From hendrik at microcorp.co.za Fri Jul 31 06:47:30 2009 From: hendrik at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 31 Jul 2009 12:47:30 +0200 Subject: file comparison In-Reply-To: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: <200907311247.30793.hendrik@microcorp.co.za> On Friday 31 July 2009 11:25:17 learner learner wrote: > Hi all, > > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a third > file or overwrite into one of them. This is not as simple as it seems. You will probably be better off using diff, or, if it is a learning exercise, studying its source. - Hendrik From deets at nospam.web.de Fri Jul 31 07:26:06 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 31 Jul 2009 13:26:06 +0200 Subject: problem on socket programming In-Reply-To: <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> Message-ID: <4A72D4CE.6030501@nospam.web.de> MalC0de schrieb: > On Jul 31, 12:07 pm, Chris Rebert wrote: >> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: >>> now I've been encountered to some errors with this snippet : >> And these errors are? Provide error messages and full tracebacks. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > these are errors : > > Traceback (most recent call last): > File "echo-client.py", line 11, in sockobj.connect((serverHost,serverPort)) > File "", line 1, in connect > socket.error: (10061, 'Connection refused') Can you ping and telnet to the port on the desired server? Firewalls can be an issue here. Diez From deets at nospam.web.de Fri Jul 31 07:26:41 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 31 Jul 2009 13:26:41 +0200 Subject: problem on socket programming In-Reply-To: <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> Message-ID: <7dg2niF2bue0tU2@mid.uni-berlin.de> MalC0de schrieb: > On Jul 31, 12:07 pm, Chris Rebert wrote: >> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote: >>> now I've been encountered to some errors with this snippet : >> And these errors are? Provide error messages and full tracebacks. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > these are errors : > > Traceback (most recent call last): > File "echo-client.py", line 11, in sockobj.connect((serverHost,serverPort)) > File "", line 1, in connect > socket.error: (10061, 'Connection refused') Can you ping and telnet to the port on the desired server? Firewalls can be an issue here. Diez From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 31 07:38:56 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 31 Jul 2009 13:38:56 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <008e82e8$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <4a72d7cf$0$25239$426a34cc@news.free.fr> Steven D'Aprano a ?crit : > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > >> That and the fact that I couldn't stop laughing for long enough to learn >> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >> unlike less flexible languages, lets you alter the value of a constant." >> Yep, as they say "Bug" = "Undocumented feature"! > > That's no different from Python's "constant by convention". Well, at least Python doesn't pretend to have real symbolic constants - we all know it's only a convention !-) > We don't even > get a compiler warning! Of course - from the compiler's POV, it's only usual rebinding. > On the other hand, we don't have to prefix names with @ and @@, Nope, we have to prefix them with 'self' or 'cls' (or even 'self.__class__'). > and we > don't have the compiler trying to *guess* whether we're calling a > function or referring to a variable. Please re-read a bit more carefully - it's *all* method call. Python is 'uniform' in obj.name is always an attribute lookup (methods being attributes), Ruby is uniform in that 'obj.name' is always a method call. > Somebody who knows more Ruby than me should try writing the Zen of Ruby. > Something like: (snip childish parody of Python Zen) Steven, is that any useful ? > > Although I'm sure Ruby has its good points. I'm not convinced anonymous > code blocks are one of them though. Ruby's code blocks come from Smalltalk, where they are an absolute necessity since message passing (which code blocks are part of) is the *only* builtin control flow in Smalltalk - so you just *need* this construction to provide branching and iterations. Wether it makes sense to have code blocks in Ruby is another question since Ruby does provide traditional control flow features, but then one could question Python's "lambda" (hem...) too. From j.sundo at cs.ucl.ac.uk Fri Jul 31 07:41:49 2009 From: j.sundo at cs.ucl.ac.uk (=?ISO-8859-1?Q?Jannik_Sund=F8?=) Date: Fri, 31 Jul 2009 12:41:49 +0100 Subject: Confused About Python Loggin In-Reply-To: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> References: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> Message-ID: <14CCA71D-F575-4B8A-8C99-67BDAAC2C6E0@cs.ucl.ac.uk> Hi all. I think I fixed this problem by setting fileLogger.propagate = 0. Otherwise it will propagate up to the root logger, which outputs to stdout, as far as I can understand. On 31 Jul 2009, at 01:17, Jannik Sund? wrote: > Dear all, I am quite confused about the Python logging. I have read > and re-read the Python documentation for the Python logging module > and googled, but to no avail. I simply want one logger to log to a > file and another logger to log to the console. Neither should log > the other's messages. The code below causes fileLogger to log to > BOTH a file and the console, how come? It seems to me that a call > such as "consoleHandler = logging.StreamHandler()" affects other > loggers, which I do not want. Also, if I do not do > logging.basicConfig(level=logging.INFO) in one of the classes of my > application, the logging does not work. Any ideas how come? > > Thanks a lot for any help! :) > > fileHandler = logging.FileHandler('../../logs/log.txt') > fileLogger = logging.getLogger('TESTHARNESSFILE') > fileLogger.addHandler(fileHandler) > fileLogger.setLevel(logging.INFO) > consoleHandler = logging.StreamHandler() > logger = logging.getLogger('TESTHARNESS') > logger.addHandler(consoleHandler) > logger.setLevel(logging.INFO) From malc0de.encrypt at gmail.com Fri Jul 31 07:49:09 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 04:49:09 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> Message-ID: <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> you want to say shall i disable the firewall, there's no specific firewall or IPS/IDS system out there, this is just windows xp firewall From deets at nospam.web.de Fri Jul 31 08:08:25 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 31 Jul 2009 14:08:25 +0200 Subject: problem on socket programming In-Reply-To: <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> Message-ID: <7dg55pF2bfubpU1@mid.uni-berlin.de> MalC0de schrieb: > you want to say shall i disable the firewall, there's no specific > firewall or IPS/IDS system out there, this is just windows xp firewall Which is a firewall, don't you agree? So it could well be the reason for the problems you see. Diez From masklinn at masklinn.net Fri Jul 31 08:18:56 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 14:18:56 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a72d7cf$0$25239$426a34cc@news.free.fr> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> Message-ID: <23AB2CC9-71D1-4498-8D23-D3648C00DD4C@masklinn.net> On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote: > Steven D'Aprano a ?crit : >> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >>> That and the fact that I couldn't stop laughing for long enough to >>> learn >>> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >>> unlike less flexible languages, lets you alter the value of a >>> constant." >>> Yep, as they say "Bug" = "Undocumented feature"! >> That's no different from Python's "constant by convention". > Ruby's code blocks come from Smalltalk, where they are an absolute > necessity since message passing (which code blocks are part of) is > the *only* builtin control flow in Smalltalk - so you just *need* > this construction to provide branching and iterations. > I'm not so sure about the way you say it. I'm pretty sure "traditional" flow control structures preceded Smalltalk by a pair of decades so it's not that Smalltalk's designers found it necessary to use blocks & messages, but that they understood blocks & messages could trivially replace most control structures (making *those* unnecessary), making the core language simpler and more flexible. In other words, I see it the other way around. > Wether it makes sense to have code blocks in Ruby is another > question since Ruby does provide traditional control flow features Well it does at least allow for the creation of new flow control structures in library land when the existing ones aren't enough (e.g. allows Ruby not to require the introduction of a `with` statement). Though Ruby's blocks are nowhere near as flexible as Smalltalk's. From vinay_sajip at yahoo.co.uk Fri Jul 31 08:24:58 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Fri, 31 Jul 2009 05:24:58 -0700 (PDT) Subject: Confused About Python Loggin References: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> Message-ID: <21effe68-2202-4f58-a2d9-f1ea3968149a@d32g2000yqh.googlegroups.com> On Jul 31, 12:41?pm, Jannik Sund? wrote: > Hi all. I think I fixed this problem by setting fileLogger.propagate = ? > 0. Otherwise it will propagate up to the root logger, which outputs to ? > stdout, as far as I can understand. > Only if you specifically configure it to do so - for example, calling basicConfig() configures the root logger (if it has no handlers) by adding a StreamHandler which outputs to sys.stderr. You shouldn't need to set propagate - the script in Peter's reply appears to do what you need. Regards, Vinay Sajip From malc0de.encrypt at gmail.com Fri Jul 31 08:37:21 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 05:37:21 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> Message-ID: no, I disabled my firewall, no problem with even enabled firewall ... no one can help !? From davea at ieee.org Fri Jul 31 08:55:27 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 31 Jul 2009 08:55:27 -0400 Subject: file comparison In-Reply-To: <200907311247.30793.hendrik@microcorp.co.za> References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> <200907311247.30793.hendrik@microcorp.co.za> Message-ID: <4A72E9BF.3080808@ieee.org> Hendrik van Rooyen wrote: > On Friday 31 July 2009 11:25:17 learner learner wrote: > >> Hi all, >> >> I want to compare two text files line by line and eliminate the >> matching/repeated line and store the unmatched/leftout lines into a third >> file or overwrite into one of them. >> > > This is not as simple as it seems. > > You will probably be better off using diff, or, > if it is a learning exercise, studying its source. > > - Hendrik > > > It is not only "not simple," it's totally underspecified. Learner: If this is a homework assignment, you should read the whole assignment, and see what it says. Without knowing something about the text files, I couldn't even guess what the real goal is. For example, if the text files are really text (e.g. a letter to grandma), then we want a context-sensitive comparison, the kind that Windiff, RCSDiff, DiffMerge, and others can do. If the text files are each a list of items, with order and duplication irrelevant, then your best bet is probably to build a set from each, and difference the sets. DaveA From jeanmichel at sequans.com Fri Jul 31 09:04:59 2009 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 31 Jul 2009 15:04:59 +0200 Subject: Confused About Python Loggin In-Reply-To: <14CCA71D-F575-4B8A-8C99-67BDAAC2C6E0@cs.ucl.ac.uk> References: <534A6695-37D9-4200-9872-9E844AE50681@cs.ucl.ac.uk> <14CCA71D-F575-4B8A-8C99-67BDAAC2C6E0@cs.ucl.ac.uk> Message-ID: <4A72EBFB.4080306@sequans.com> Jannik Sund? wrote: > Hi all. I think I fixed this problem by setting fileLogger.propagate = > 0. Otherwise it will propagate up to the root logger, which outputs to > stdout, as far as I can understand. > > > On 31 Jul 2009, at 01:17, Jannik Sund? wrote: > >> Dear all, I am quite confused about the Python logging. I have read >> and re-read the Python documentation for the Python logging module >> and googled, but to no avail. I simply want one logger to log to a >> file and another logger to log to the console. Neither should log the >> other's messages. The code below causes fileLogger to log to BOTH a >> file and the console, how come? It seems to me that a call such as >> "consoleHandler = logging.StreamHandler()" affects other loggers, >> which I do not want. Also, if I do not do >> logging.basicConfig(level=logging.INFO) in one of the classes of my >> application, the logging does not work. Any ideas how come? >> >> Thanks a lot for any help! :) >> >> fileHandler = logging.FileHandler('../../logs/log.txt') >> fileLogger = logging.getLogger('TESTHARNESSFILE') >> fileLogger.addHandler(fileHandler) >> fileLogger.setLevel(logging.INFO) >> consoleHandler = logging.StreamHandler() >> logger = logging.getLogger('TESTHARNESS') >> logger.addHandler(consoleHandler) >> logger.setLevel(logging.INFO) > > --http://mail.python.org/mailman/listinfo/python-list > Please do not top post :o) Are sure you have used the code provided above ? Loggers only propagate events to its parent. fileLogger = logging.getLogger('TESTHARNESSFILE" logger = logging.getLogger('TESTHARNESS') Those one have no parent relationship. However these one have parent relationship: fileLogger = logging.getLogger('TESTHARNESS.FILE") # note the dotted name logger = logging.getLogger('TESTHARNESS') Or maybe you have added a handler to the root logger... Using the propagate attribute is correct only between 2 loggers that are meant to be parents. JM From python at mrabarnett.plus.com Fri Jul 31 09:05:35 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 14:05:35 +0100 Subject: problem on socket programming In-Reply-To: References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> Message-ID: <4A72EC1F.7040709@mrabarnett.plus.com> MalC0de wrote: > no, I disabled my firewall, no problem with even enabled firewall ... > no one can help !? I hope you disconnected your computer from the internet before disabling the firewall... From bruno.42.desthuilliers at websiteburo.invalid Fri Jul 31 09:12:42 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Fri, 31 Jul 2009 15:12:42 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> Message-ID: <4a72edca$0$32755$426a74cc@news.free.fr> Masklinn a ?crit : > On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote: >> Steven D'Aprano a ?crit : >>> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >>>> That and the fact that I couldn't stop laughing for long enough to >>>> learn >>>> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >>>> unlike less flexible languages, lets you alter the value of a >>>> constant." >>>> Yep, as they say "Bug" = "Undocumented feature"! >>> That's no different from Python's "constant by convention". >> Ruby's code blocks come from Smalltalk, where they are an absolute >> necessity since message passing (which code blocks are part of) is the >> *only* builtin control flow in Smalltalk - so you just *need* this >> construction to provide branching and iterations. >> > I'm not so sure about the way you say it. I'm not sure about the way you understand it !-) > I'm pretty sure "traditional" > flow control structures preceded Smalltalk by a pair of decades Yes, of course - and that's not the point. What's important is that: > so it's > not that Smalltalk's designers found it necessary to use blocks & > messages, but that they understood blocks & messages could trivially > replace most control structures (making *those* unnecessary), making the > core language simpler and more flexible. Exactly. > In other words, I see it the other way around. My wording may have been a bit confusing, indeed. It was implied (but perhaps a bit unclear) that restricting control structures to messages and blocks was a design choice. >> Wether it makes sense to have code blocks in Ruby is another question >> since Ruby does provide traditional control flow features > > Well it does at least allow for the creation of new flow control > structures in library land when the existing ones aren't enough (e.g. > allows Ruby not to require the introduction of a `with` statement). Yeps. But then other "traditionnal" control flow features become redundant. > Though Ruby's blocks are nowhere near as flexible as Smalltalk's. From processor.dev1l at gmail.com Fri Jul 31 09:15:00 2009 From: processor.dev1l at gmail.com (Processor-Dev1l) Date: Fri, 31 Jul 2009 06:15:00 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> Message-ID: <72f851f9-a2d4-4448-900d-3fe20e4ebeb0@w41g2000yqb.googlegroups.com> On Jul 31, 2:37?pm, MalC0de wrote: > no, I disabled my firewall, no problem with even enabled firewall ... > no one can help !? Connection refused... connection was refused by the listening socket, it could also be due to the settings of your service, if it accepts only from localhost to localhost (127.0.0.1 to 127.0.0.1) then other connections will not be granted. Try to find some socket samples on python.org (there are plenty of tip and tricks for using sockets) From processor.dev1l at gmail.com Fri Jul 31 09:21:14 2009 From: processor.dev1l at gmail.com (Processor-Dev1l) Date: Fri, 31 Jul 2009 06:21:14 -0700 (PDT) Subject: Run pyc file without specifying python path ? References: <7F0503CD69378F49BE0DC30661C6CCF6701A26C4@enbmail01.lsi.com> <4A708F68.1010505@ieee.org> <7F0503CD69378F49BE0DC30661C6CCF6701A26C7@enbmail01.lsi.com> <4A7199FF.8010202@dejaviewphoto.com> Message-ID: <52bdb49f-935f-459c-bc64-76e5ccb16f36@k6g2000yqn.googlegroups.com> On Jul 30, 4:47?pm, "Barak, Ron" wrote: > > -----Original Message----- > > From: Dave Angel [mailto:da... at dejaviewphoto.com] > > Sent: Thursday, July 30, 2009 16:03 > > To: Barak, Ron > > Cc: 'Dave Angel'; 'python-l... at python.org' > > Subject: RE: Run pyc file without specifying python path ? > > > Barak, Ron wrote: > > >> -----Original Message----- > > >> From: Dave Angel [mailto:da... at ieee.org] > > >> Sent: Wednesday, July 29, 2009 21:05 > > >> To: Barak, Ron > > >> Cc: 'python-l... at python.org' > > >> Subject: Re: Run pyc file without specifying python path ? > > > >> Barak, Ron wrote: > > > >>> Hi, > > > >>> I wanted to make a python byte-code file executable, > > > >> expecting to be able to run it without specifying "python" on the > > >> (Linux bash) command line. > > > >>> So, I wrote the following: > > > >>> [root at VMLinux1 python]# cat test_pyc.py #!/usr/bin/env python > > > >>> print "hello" > > >>> [root at VMLinux1 python]# > > > >>> and made its pyc file executable: > > > >>> [root at VMLinux1 python]# ls -ls test_pyc.pyc > > >>> 4 -rwxr-xr-x ?1 root root 106 Jul 29 14:22 test_pyc.pyc > > >>> [root at VMLinux1 python]# > > > >>> So, I see: > > > >>> [root at VMLinux1 python]# file test_pyc.py* > > >>> test_pyc.py: ?a python script text executable > > >>> test_pyc.pyc: python 2.3 byte-compiled > > >>> [root at VMLinux1 python]# > > > >>> If I try to do the following, no problem: > > > >>> [root at VMLinux1 python]# python test_pyc.pyc hello > > >>> [root at VMLinux1 python]# > > > >>> However, the following fails: > > > >>> [root at VMLinux1 python]# ./test_pyc.pyc > > >>> -bash: ./test_pyc.pyc: cannot execute binary file > > >>> [root at VMLinux1 python]# > > > >>> Is there a way to run a pyc file without specifying the > > > >> python path ? > > > >>> Bye, > > >>> Ron. > > > >> I don't currently run Unix, but I think I know the problem. > > > >> In a text file, the shell examines the first line, and if > > it begins > > >> #! > > >> it's assumed to point to the executable of an interpreter for that > > >> text file. ?Presumably the same trick doesn't work for a .pyc file. > > > >> Why not write a trivial wrapper.py file, don't compile it, and let > > >> that invoke the main code in the .pyc file? > > > >> Then make wrapper.py executable, and you're ready to go. > > > >> DaveA > > > > Hi Dave, > > > Your solution sort of defeats my intended purpose (sorry > > for not divulging my 'hidden agenda'). > > > I wanted my application to "hide" the fact that it's a > > python script, and look as much as possible like it's a > > compiled program. > > > The reason I don't just give my user a py file, is that I > > don't want a cleaver user to change the innards of the script. > > > On the other hand, I don't want to make a compiled > > (freezed?) version of the application, because it'll grow the > > resulting file significantly, and I don't have the experience > > to know how it will run on different Linuxes. > > > Bye, > > > Ron. > > Hi Dave, > > > Most of the other answers basically paraphrased my suggestion > > of making a wrapper file, not compiling it, and making it > > executable. ?With that > > approach, the user just types ? "wrapper.py" on his command line. > > > And wrapper.py only needs two lines, a shebang, and an > > import, no big deal if the user modifies it. ?The rest of > > your code can be .pyc files. > > > Steven makes some good points. ?You have to define what level > > of clever you're protecting from. ?A determined hacker will > > The clever I try to guard against is not at hacker level, just the curios worker. > > > get in no matter what you do, unless you want to ship the > > program in a proprietary embedded system, encased in epoxy. ? > > Further, if you have an extension of .py or .pyc, a > > knowledgeable hacker will know it's probably python. > > Granted, but the user will know that from the user manual anyway :-) > > > > > You imply you want it to run unmodifed on multiple unknown > > Linux versions. ?I think that lets out binfmt solutions. ? > > Actually, no: I know the set of Linuxes my users are likely to have. > > > That means you need to test and support not only multiple > > Linux implementations, but multiple Python versions, because > > who knows what the user may have installed. ?I think you need > > to rethink your support strategy. ?And maybe concentrate on > > being able to detect change, rather than prevent it. > > > DaveA > > Thanks for the helpful suggestions. > Ron. how can a compiled python file (pyc) can defend your code against clever-user? Try command cat "yourcode.pyc" and you will is how "hardly" it can be read or modified :). The most easy way how to distribute such file is with some shell batch containing #!/usr/bin/sh python ./lib/myscript.pyc the topology of the app would be a shell script called simply run, or name of your app and something like that and a folder called lib where your pyc file is stored, just make the script executable (chmod a+x shellfile or chmod 755 shellfile) and force ppl to use it instead of the pyc file in the lib folder :) From masklinn at masklinn.net Fri Jul 31 09:39:47 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 15:39:47 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <4a72edca$0$32755$426a74cc@news.free.fr> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <4a72edca$0$32755$426a74cc@news.free.fr> Message-ID: On 31 Jul 2009, at 15:12 , Bruno Desthuilliers wrote: > Masklinn a ?crit : >> On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote: >>> Steven D'Aprano a ?crit : >>>> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >>>>> That and the fact that I couldn't stop laughing for long enough >>>>> to learn >>>>> any more when I read in the Pragmatic Programmer's Guide that >>>>> "Ruby, >>>>> unlike less flexible languages, lets you alter the value of a >>>>> constant." >>>>> Yep, as they say "Bug" = "Undocumented feature"! >>>> That's no different from Python's "constant by convention". >>> Ruby's code blocks come from Smalltalk, where they are an absolute >>> necessity since message passing (which code blocks are part of) is >>> the *only* builtin control flow in Smalltalk - so you just *need* >>> this construction to provide branching and iterations. > [misunderstandings on my part/clarifications on yours] >> Well it does at least allow for the creation of new flow control >> structures in library land when the existing ones aren't enough >> (e.g. allows Ruby not to require the introduction of a `with` >> statement). > > Yeps. But then other "traditionnal" control flow features become > redundant. They can be anyway: Ruby doesn't deprecate most control flows as the actual usages of blocks are a bit restricted (cannot be used for `while` as it would require the serialization to a Proc, and Ruby's syntax doesn't allow sending multiple blocks to a method so `if?else` is out as well). And I assume they reintroduced the for?in sugar to ease the transition from more traditional languages (using #each and others seems the suggested collection iterators across the community). From malc0de.encrypt at gmail.com Fri Jul 31 09:43:49 2009 From: malc0de.encrypt at gmail.com (MalC0de) Date: Fri, 31 Jul 2009 06:43:49 -0700 (PDT) Subject: problem on socket programming References: <3de0863c-15ef-4153-8d0f-6b7a438b98f5@c2g2000yqi.googlegroups.com> <67ec3e6d-0995-459a-a7a5-3bc296729664@b15g2000yqd.googlegroups.com> <7dg2niF2bue0tU2@mid.uni-berlin.de> <5dbf451d-a289-40d1-83db-ce655d47f698@r38g2000yqn.googlegroups.com> <7dg55pF2bfubpU1@mid.uni-berlin.de> <72f851f9-a2d4-4448-900d-3fe20e4ebeb0@w41g2000yqb.googlegroups.com> Message-ID: none of you can't interpret it ? do you have errors like me !? From gary.wilson at gmail.com Fri Jul 31 09:51:05 2009 From: gary.wilson at gmail.com (Gary Wilson) Date: Fri, 31 Jul 2009 06:51:05 -0700 (PDT) Subject: supported versions policy? Message-ID: <07526890-6264-43c0-bd01-29ffdb3eedc2@d9g2000prh.googlegroups.com> Does Python have a formal policy on the support lifetime (bug fixes, security fixes, etc.) for major and minor versions? I did a bit of searching on the Python web site and this group, but didn't find anything. If there is a policy posted somewhere (and I just didn't dig deep enough), would someone please point me in the right direction. If there is no posted policy, would someone be kind enough to describe the practiced policy here. I'm looking for is something like this: http://docs.djangoproject.com/en/dev/internals/release-process/#supported-versions Thanks, Gary From Scott.Daniels at Acm.Org Fri Jul 31 09:51:17 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 31 Jul 2009 06:51:17 -0700 Subject: fast video encoding In-Reply-To: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> References: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Message-ID: gregorth wrote: > for a scientific application I need to save a video stream to disc for > further post processing. My cam can deliver 8bit grayscale images with > resolution 640x480 with a framerate up to 100Hz, this is a data rate > of 30MB/s. Writing the data uncompressed to disc hits the data > transfer limits of my current system and creates huge files. Therefore > I would like to use video compression, preferably fast and high > quality to lossless encoding. Final file size is not that important. Well, it sounds like it better be enough to affect bandwidth. > I am a novice with video encoding. I found that few codecs support > gray scale images. Any hints to take advantage of the fact that I only > have gray scale images? You might try to see if there is a primitive .MNG encoder around. That could give you lossless with perhaps enough compression to make you happy, and I'm sure it will handle the grayscale. .MNG is pictures only, but that doesn't hurt you in the least. --Scott David Daniels Scott.Daniels at Acm.Org From roger.miller at nova-sol.com Fri Jul 31 10:22:56 2009 From: roger.miller at nova-sol.com (Roger Miller) Date: Fri, 31 Jul 2009 07:22:56 -0700 (PDT) Subject: Test for Pythonwin? References: <4a728aac$0$9744$5a62ac22@per-qv1-newsreader-01.iinet.net.au> Message-ID: <7be1b318-851d-4197-afd4-48de38c878e6@d34g2000vbm.googlegroups.com> On Jul 31, 2:09?am, "steve" wrote: > Is there a good way to check if a script is running inside Pythonwin? > Perhaps a property or method that is exposed by that environment? I've used if sys.stdin.fileno() < 0: This is not precisely a test for pythonwin, but indicates whether standard streams are available, which is usually what I really want to know. From ethan at stoneleaf.us Fri Jul 31 11:08:29 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 31 Jul 2009 08:08:29 -0700 Subject: Confessions of a Python fanboy In-Reply-To: <008e82e8$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <4A7308ED.7040108@stoneleaf.us> Steven D'Aprano wrote: > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > > >>That and the fact that I couldn't stop laughing for long enough to learn >>any more when I read in the Pragmatic Programmer's Guide that "Ruby, >>unlike less flexible languages, lets you alter the value of a constant." >>Yep, as they say "Bug" = "Undocumented feature"! > > > That's no different from Python's "constant by convention". We don't even > get a compiler warning! > That's quite different, actually. Python doesn't claim to have constants! Can't misinterpret what you can't have, can you? [Holds breath while awaiting counter-example... :] ~Ethan~ From ethan at stoneleaf.us Fri Jul 31 11:15:14 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 31 Jul 2009 08:15:14 -0700 Subject: Regexp problem In-Reply-To: <4A72135E.7060205@mrabarnett.plus.com> References: <30761c97-aeb0-474d-8047-c7fa38c04a86@w41g2000yqb.googlegroups.com> <4A720AD5.1030202@stoneleaf.us> <4A72135E.7060205@mrabarnett.plus.com> Message-ID: <4A730A82.3050502@stoneleaf.us> MRAB wrote: > Ethan Furman wrote: > >> Marcus Wanner wrote: >>> Wow, I really need to learn more about regexp... >>> Any tutorials you guys can recommend? >>> >>> Marcus >> >> >> Mastering Regular Expressions >> Powerful Techniques for Perl and Other Tools >> By Jeffrey E. F. Friedl >> >> Great book! >> > +1 > > I have the first edition, seventh printing (December 1998). It refers to > the 'regex' module of Python 1.4b1, which was subsequently replaced by > the current 're' module and then removed from the standard library. I > hope it's been updated since then. :-) I have the second edition (no idea which printing ;), and according to his preface it has indeed been much updated. Most examples are in perl, the few in python are decent. The knowledge embodied seems very thorough. Since I've had the book (two weeks now?) I've been able to solve two otherwise thorny issues using regular expressions. Yay! ~Ethan~ From iainking at gmail.com Fri Jul 31 11:35:45 2009 From: iainking at gmail.com (Iain King) Date: Fri, 31 Jul 2009 08:35:45 -0700 (PDT) Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: On Jul 31, 4:08?pm, Ethan Furman wrote: > Steven D'Aprano wrote: > > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > > >>That and the fact that I couldn't stop laughing for long enough to learn > >>any more when I read in the Pragmatic Programmer's Guide that "Ruby, > >>unlike less flexible languages, lets you alter the value of a constant." > >>Yep, as they say "Bug" = "Undocumented feature"! > > > That's no different from Python's "constant by convention". We don't even > > get a compiler warning! > > That's quite different, actually. ?Python doesn't claim to have > constants! ?Can't misinterpret what you can't have, can you? > > [Holds breath while awaiting counter-example... :] > > ~Ethan~ The convention being detailed in PEP8: http://www.python.org/dev/peps/pep-0008/ basically, anything in ALL_CAPS is a constant, assuming you follow those style guidelines. Iain From benjamin.kaplan at case.edu Fri Jul 31 11:52:37 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Fri, 31 Jul 2009 11:52:37 -0400 Subject: Confessions of a Python fanboy In-Reply-To: References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: On Fri, Jul 31, 2009 at 11:35 AM, Iain King wrote: > On Jul 31, 4:08?pm, Ethan Furman wrote: >> Steven D'Aprano wrote: >> > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: >> >> >>That and the fact that I couldn't stop laughing for long enough to learn >> >>any more when I read in the Pragmatic Programmer's Guide that "Ruby, >> >>unlike less flexible languages, lets you alter the value of a constant." >> >>Yep, as they say "Bug" = "Undocumented feature"! >> >> > That's no different from Python's "constant by convention". We don't even >> > get a compiler warning! >> >> That's quite different, actually. ?Python doesn't claim to have >> constants! ?Can't misinterpret what you can't have, can you? >> >> [Holds breath while awaiting counter-example... :] >> >> ~Ethan~ > > The convention being detailed in PEP8: http://www.python.org/dev/peps/pep-0008/ > basically, anything in ALL_CAPS is a constant, assuming you follow > those style guidelines. > Right, but that's a style guide and not a language definition. Nobody claims that anything in there is a part of the language. > Iain > -- > http://mail.python.org/mailman/listinfo/python-list > From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 11:55:43 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 15:55:43 GMT Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> Message-ID: <008eef29$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 13:38:56 +0200, Bruno Desthuilliers wrote: >> On the other hand, we don't have to prefix names with @ and @@, > > Nope, we have to prefix them with 'self' or 'cls' (or even > 'self.__class__'). Incorrect. >>> class K: ... class_attribute = 'No @@ required.' ... >>> K().class_attribute 'No @@ required.' No 'self' or 'cls' in sight. I can see a vague advantage to using @ to create instance attributes and @@ to creating class attributes -- it makes it easy to create class attributes inside a method without needing to do a double lookup. In Python terms: def K: def method(self): self.__class__.attr = "Class attribute" would be: def K: def method(self): @@attr = "Class attribute" Advantages: you save a few characters and (possibly!) a couple of runtime lookups. Disadvantages: your code is filled with line noise. It's an arbitrary choice between @@ meaning instance attribute and @@ meaning class attribute -- there's no logical reason for choosing one over the other, so you have to memorise which is which. It's easy to get it wrong. 'self' and 'cls' at least are words, even if 'cls' is badly misspelled :) >> and we >> don't have the compiler trying to *guess* whether we're calling a >> function or referring to a variable. > > Please re-read a bit more carefully - it's *all* method call. What did I misread from here? [quote] When Ruby sees a name such as ``a'' in an expression, it needs to determine if it is a local variable reference or a call to a method with no parameters. To decide which is the case, Ruby uses a heuristic. As Ruby reads a source file, it keeps track of symbols that have been assigned to. It assumes that these symbols are variables. When it subsequently comes across a symbol that might be either a variable or a method call, it checks to see if it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable; otherwise it treats it as a method call. [end quote] And see the example "pathological case" comparing a function call (not method) and a variable. http://ruby-doc.org/docs/ProgrammingRuby/html/language.html > Python is > 'uniform' in obj.name is always an attribute lookup (methods being > attributes), Ruby is uniform in that 'obj.name' is always a method call. Which would be relevant if I was talking about method calls, but I wasn't. >> Somebody who knows more Ruby than me should try writing the Zen of >> Ruby. Something like: > > (snip childish parody of Python Zen) > > Steven, is that any useful ? It made me feel good. But seriously, while I admit that I have very little Ruby experience, and so aren't in a great position to judge, it seems to me that Ruby doesn't have anything like Python's over-riding design principles (the Zen). If there is a design principle to Ruby, I can't see what it is. I'm the first to admit that I'm far too inexperienced with the language to make this a fair judgement. Unfair it might be, but it doesn't necessarily mean I'm wrong! *wink* Ruby just seems to be far more complicated than Python: things which Python does at runtime, with a function call, Ruby has special syntax for: "a bunch of words".split() %w(a bunch of words) ord('a') ?a That makes the barrier to entry far higher: it's easier to leverage existing knowledge to interpret unfamiliar Python code than unfamiliar Ruby code. Or so it seems to me. Oh, and I admit that Python decorators are a conspicuous counter-example. I wouldn't do without them, but neither would I expect somebody to intuit what they do. >> Although I'm sure Ruby has its good points. I'm not convinced anonymous >> code blocks are one of them though. > > Ruby's code blocks come from Smalltalk, where they are an absolute > necessity since message passing (which code blocks are part of) is the > *only* builtin control flow in Smalltalk - so you just *need* this > construction to provide branching and iterations. Just because Smalltalk had a particular (mis?)feature doesn't mean that other languages should copy it. I know, I know, Ruby people swear by anonymous code blocks, and I've read Paul Graham too. But I'm really not so sure that the benefits of anonymous code blocks are great enough to overcome the disadvantages of anonymous code blocks. > Wether it makes sense to have code blocks in Ruby is another question > since Ruby does provide traditional control flow features, but then one > could question Python's "lambda" (hem...) too. lambda, by allowing the function to be only a single expression, doesn't suffer the disadvantages of anonymous code blocks. lambda, by allowing the function to be only a single expression, also has fewer advantages than anonymous code blocks. I think lambda ends up on the "more advantages than disadvantages" side. I'm keeping my mind open regarding Ruby code blocks. -- Steven From masklinn at masklinn.net Fri Jul 31 12:15:15 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 18:15:15 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <008eef29$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <008eef29$0$9756$c3e8da3@news.astraweb.com> Message-ID: <11938EF7-4FC3-4F1A-A665-D9360442456F@masklinn.net> On 31 Jul 2009, at 17:55 , Steven D'Aprano wrote: > But seriously, while I admit that I have very little Ruby > experience, and > so aren't in a great position to judge, it seems to me that Ruby > doesn't > have anything like Python's over-riding design principles (the Zen). > If > there is a design principle to Ruby, I can't see what it is. > As far as I know, Ruby doesn't have anything like the Zen no. But then again, I don't know any language other than Python with such a document. > Ruby just seems to be far more complicated than Python: things which > Python does at runtime, with a function call, Ruby has special > syntax for: > > "a bunch of words".split() > %w(a bunch of words) > > ord('a') > ?a > That's the Perl heritage. And since it inherits from Perl, TIMTOWTDI: >> "a bunch of words".split => ["a", "bunch", "of", "words"] >> "a"[0] => 97 (yes, the indexing operator returns a character code if provided a single index. With two, it returns as string slice) Oh and %w() isn't really equivalent to split(): you don't use it to split a string but to create a list of strings, so the equivalent expression in Python would be `["a", "bunch", "of", "words"]`. > That makes the barrier to entry far higher: it's easier to leverage > existing knowledge to interpret unfamiliar Python code than unfamiliar > Ruby code. Or so it seems to me. > That's probable. >>> Although I'm sure Ruby has its good points. I'm not convinced >>> anonymous >>> code blocks are one of them though. >> >> Ruby's code blocks come from Smalltalk, where they are an absolute >> necessity since message passing (which code blocks are part of) is >> the >> *only* builtin control flow in Smalltalk - so you just *need* this >> construction to provide branching and iterations. > > Just because Smalltalk had a particular (mis?)feature doesn't mean > that > other languages should copy it. I know, I know, Ruby people swear by > anonymous code blocks, and I've read Paul Graham too. But I'm really > not > so sure that the benefits of anonymous code blocks are great enough to > overcome the disadvantages of anonymous code blocks. > What are the disadvantages of anonymous functions? From tjreedy at udel.edu Fri Jul 31 12:24:28 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 12:24:28 -0400 Subject: Confessions of a Python fanboy In-Reply-To: References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: Masklinn wrote: > #each is simply a method that takes a function (called blocks in ruby). > One could call it a higher-order method I guess. > > It's an implementation of the concept of internal iteration: instead of > collections yielding iterator objects, and programmers using those > through specially-built iteration constructs (e.g. `for?in`), > collections control iteration over themselves (the iteration is > performed "inside" the collection, thus the "internal" part) and the > programmer provides the operations to perform at each iterative step > through (usually) a function. Python's iterator protocol was developed in part to avoid the (inside-out) callback style of programming. Writing virtual collections as generator functions instead of iterator or iterable classes saves a lot of boilerplate code. The itertools modules shows how nicely iterators can be composed in a way that is much more awkward with callbacks. > In Python (assuming we had anonymous defs and an each method on lists), > the following loop: > > for item in some_list: > do_something(item) > do_something_else(item) > > some_list.each((def (item): > do_something(item) > do_something_else(item) > )) And how does Ruby do the equivalent of def double(it): for i in it: yield 2*i for i,j in zip(double(some_list), some_gen_func(args)): print(do_something(i+j,i-j)) Terry Jan Reedy From bdezonia at wisc.edu Fri Jul 31 12:33:45 2009 From: bdezonia at wisc.edu (BDZ) Date: Fri, 31 Jul 2009 09:33:45 -0700 (PDT) Subject: os.path.exists() and Samba shares References: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> Message-ID: <337482f9-8ad9-4600-9cf4-39911f5368ac@h31g2000yqd.googlegroups.com> On Jul 30, 4:41?pm, Lo?c Domaign? wrote: > Hi, > > > Hello. I have written a Python 3.1 script running on Windows that uses > > os.path.exists() to connect to network shares. If the various network > > shares require different user account and password combos than the > > account the script is running under the routine returns false. I need > > something like os.samba.path.exists(username,password,path). Does > > anyone have a suggestion on how I can accomplish what I need to do in > > Python? > > Could the Python Samba module PySamba be interesting for you?http://sourceforge.net/projects/pysamba/ > > HTH, > Lo?c > -- > My blog:http://www.domaigne.com/blog Unfortunately, although it has the calls I'd want, pysamba appears to be *nix only. I need something that will work under Windows. Is there a set of Python Windows functions (official or contributed) that might do what I need? (I'm new to Python) From darkneter at gmail.com Fri Jul 31 12:35:10 2009 From: darkneter at gmail.com (NighterNet) Date: Fri, 31 Jul 2009 09:35:10 -0700 (PDT) Subject: threads check socket Message-ID: I been trying to find a way to check the socket is open or not. The thread are in a group and loop if the sockets are open. If they are not open delete the thread and remove the group. I need on this. From tjreedy at udel.edu Fri Jul 31 12:39:21 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 12:39:21 -0400 Subject: supported versions policy? In-Reply-To: <07526890-6264-43c0-bd01-29ffdb3eedc2@d9g2000prh.googlegroups.com> References: <07526890-6264-43c0-bd01-29ffdb3eedc2@d9g2000prh.googlegroups.com> Message-ID: Gary Wilson wrote: > Does Python have a formal policy on the support lifetime (bug fixes, > security fixes, etc.) for major and minor versions? I did a bit of > searching on the Python web site and this group, but didn't find > anything. If there is a policy posted somewhere (and I just didn't > dig deep enough), would someone please point me in the right > direction. If there is no posted policy, would someone be kind enough > to describe the practiced policy here. Defining Pyx.y.z as Pymajor.minor.bugfix, the last normal bugfix comes out about the same time as the next minor. True security fixes (very rare) come out for a couple of years thereafter. (Exception, 3.0 support ended with 3.0.1 -- it should be replaced with 3.1.) Currently Py2 and Py3 are running in parallel. The future 2.7.z's will probably be the end of the Py2 series. Everything is contingent on voluteer time to do the above. tjr From db3l.net at gmail.com Fri Jul 31 12:40:34 2009 From: db3l.net at gmail.com (David Bolen) Date: Fri, 31 Jul 2009 12:40:34 -0400 Subject: fast video encoding References: <570097d0-2b2a-4705-892f-0d0154d69d63@b14g2000yqd.googlegroups.com> Message-ID: gregorth writes: > I am a novice with video encoding. I found that few codecs support > gray scale images. Any hints to take advantage of the fact that I only > have gray scale images? I don't know that there's any good way around the fact that video encoding is simply one of the heavier CPU-bound activities you're likely to encounter. So I suspect that codec choice (barring letting quality drop a bit) is going to move the bar less than picking the beefiest CPU you can. If I were looking to do this, I'd probably include investigating pumping the raw camera images into an ffmpeg encoding subprocess and let it handle all the encoding. There's about a gazillion options and a variety of codec options to play with for performance. You could grab and store a short video sample from the camera and use it as a benchmark to compare encoding options. ffmpeg does have a -pix_fmt option that can be used to indicate the input pixel type - "gray" would be 8-bit, and result in a 4:0:0 image with a YUV-based encoder, for example. Not sure how much, if any, impact it would have on encoding speed though. To be honest, with your data rate, I might even consider getting Python out of the pure encoding path once it starts - depending on the raw network format delivered by the camera you might be able to have ffmpeg read directly from it. Might not be worth it, since the data transfer is probably I/O bound, but a 640x480x1 stream at 100Hz is still nothing to sniff at, and even managing the raw data flow in Python might eat up some CPU that you could better allocate to the encoding process, or require extra data copies along the way that would be best to avoid. -- David From garrickp at gmail.com Fri Jul 31 12:53:44 2009 From: garrickp at gmail.com (Falcolas) Date: Fri, 31 Jul 2009 09:53:44 -0700 (PDT) Subject: Confessions of a Python fanboy References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> <2d56febf0907310121g36bb6e54yae84cb82a22b58e7@mail.gmail.com> <50697b2c0907310125r5eebbdcewfec4ff2337c53140@mail.gmail.com> Message-ID: <5028a8b0-21b6-4052-8483-7236b9142bd9@h21g2000yqa.googlegroups.com> On Jul 31, 3:49?am, Masklinn wrote: > On 31 Jul 2009, at 10:25 , Chris Rebert wrote:> On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho ? > > wrote: > >> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn ? > >> wrote: > > >>> ... but since Python doesn't have anonymous functions that ? > >>> usage > >>> tends to be a bit too verbose ... > > >> Sorry to interrupt, but wouldn't lambda in Python be considered as > >> 'anonymous functions'? > > > I believe "full" anonymous functions was intended by the author. > > lambdas are limited to a single expression. > > Yes, and they're limited to a single *expression*, so before Python 3, ? > lambda: print "foo" is out. Likewise, it isn't possible to have an if/ > else statement within a lambda (though a ternary is ok), or a with, ? ? > since Python statements aren't expressions. Perhaps you can't do lambda foo: print foo, but you *can* do lambda x: sys.stdout.write(x). Combined with the ternary if, it seem sufficient if you want to stick to the ideal "Simple is better than Complex". Sure, with statements allowed in anonymous functions, you can save on a line of typing (def blargh(x):), but I don't feel the obfuscation is worth the cost. ~G From digitig at gmail.com Fri Jul 31 12:54:23 2009 From: digitig at gmail.com (Tim Rowe) Date: Fri, 31 Jul 2009 17:54:23 +0100 Subject: Confessions of a Python fanboy In-Reply-To: <008e82e8$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: 2009/7/31 Steven D'Aprano : > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > >> That and the fact that I couldn't stop laughing for long enough to learn >> any more when I read in the Pragmatic Programmer's Guide that "Ruby, >> unlike less flexible languages, lets you alter the value of a constant." >> Yep, as they say "Bug" = "Undocumented feature"! > > That's no different from Python's "constant by convention". We don't even > get a compiler warning! We don't actually *declare* that something is constant and then have that declaration ignored. Python doesn't lie to us, although (as in any language) a programmer might. -- Tim Rowe From jabronson at gmail.com Fri Jul 31 13:02:55 2009 From: jabronson at gmail.com (Joshua Bronson) Date: Fri, 31 Jul 2009 10:02:55 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: On Jul 22, 7:55?am, Duncan Booth wrote: > I find it interesting that the heapq functions tell you in the > documentation that they aren't suitable for use where n==1 or where n is > near the total size of the sequence whereas random.sample() chooses what it > thinks is the best algorithm based on the input. At the very least I would > have thought the heapq functions could check for n==1 and call min/max when > it is. I've had the same thought myself a number of times, so I filed a bug: http://bugs.python.org/issue6614 From marcusw at cox.net Fri Jul 31 13:22:24 2009 From: marcusw at cox.net (Marcus Wanner) Date: Fri, 31 Jul 2009 13:22:24 -0400 Subject: threads check socket In-Reply-To: References: Message-ID: On 7/31/2009 12:35 PM, NighterNet wrote: > I been trying to find a way to check the socket is open or not. The > thread are in a group and loop if the sockets are open. If they are > not open delete the thread and remove the group. I need on this. Being a bit more specific would help. Are you using tcp or udp? Is it a local socket or a remote one? What is controlling the socket? Define "open". Marcus From gagsl-py2 at yahoo.com.ar Fri Jul 31 13:23:02 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 14:23:02 -0300 Subject: threads check socket References: Message-ID: En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet escribi?: > I been trying to find a way to check the socket is open or not. The > thread are in a group and loop if the sockets are open. If they are > not open delete the thread and remove the group. I need on this. What means an "open socket"? Do you want to detect whether the other side closed the connection? In that case reading from the socket returns an empty string. Regarding the "threads in a group", I don't understand what you mean. But in general, you don't delete a thread, you just return from its run() method and the thread finishes execution. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 13:32:13 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 14:32:13 -0300 Subject: file comparison References: <29a34bc20907310225n50b35fe2m322872af2058a465@mail.gmail.com> Message-ID: En Fri, 31 Jul 2009 06:25:17 -0300, learner learner escribi?: > I want to compare two text files line by line and eliminate the > matching/repeated line and store the unmatched/leftout lines into a third > file or overwrite into one of them. Look at the difflib module: http://docs.python.org/library/difflib.html If that's not what you want, you have to provide much more details. -- Gabriel Genellina From python at rcn.com Fri Jul 31 13:32:46 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 10:32:46 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <86skgr0yjn.fsf@gmail.com> Message-ID: On Jul 22, 4:55?am, Duncan Booth wrote: > Steven D'Aprano wrote: > > But that's the wrong solution to the problem. The OP wants the largest > > (or smallest) item, which he expects to get by sorting, then grabbing > > the first element: > > > sorted(alist)[0] > > > That requires sorting the entire list, only to throw away everything > > except the first item. A better solution is to use min() and max(), > > neither of which sort the list, so they are much faster. > > And if they want the n smallest or largest items (where n is significantly > less than the length of the list[*]) they might consider using > heapq.nsmallest() and heapq.nlargest() > > I find it interesting that the heapq functions tell you in the > documentation that they aren't suitable for use where n==1 or where n is > near the total size of the sequence whereas random.sample() chooses what it > thinks is the best algorithm based on the input. At the very least I would > have thought the heapq functions could check for n==1 and call min/max when > it is. The heapq.py code in Py2.7 and Py3.1 already does some automatic algorithm selection: http://svn.python.org/view/python/branches/release31-maint/Lib/heapq.py?revision=73579&view=markup The automatic seletion of alternatives only occurs in clear-cut cases (such as n==1 or where n==len(iterable) when the iterable has a known length). For the remaining cases, the switchover point from heapq to sorted needs a programmer's judgment based on whether the input iterable has a known length, the cost of comparison function, and whether input is already partially ordered. The advice in the docs helps the reader understand the relationships between min, max, nsmallest, nlargest, and sorted. Raymond From ethan at stoneleaf.us Fri Jul 31 13:41:15 2009 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 31 Jul 2009 10:41:15 -0700 Subject: .dbf tables and Null Message-ID: <4A732CBB.9090400@stoneleaf.us> Mornin'! and a good one, too, I hope. Question for you... First part of the question: What is the general value in having Null capability for fields? Second part: Is there a tangible difference between Null, and the nothing of 0, '', False, etc, in python? Third part: If there is a tangible difference, do those of us who use python and these old, refuse-to-die, .dbf files actually have need of, or have tables including, Null values? P.S. part (for John Machin, if he sees this ;) Will the dbf package you are working on support Null values? Any and all information appreciated! ~Ethan~ From darkneter at gmail.com Fri Jul 31 13:42:29 2009 From: darkneter at gmail.com (NighterNet) Date: Fri, 31 Jul 2009 10:42:29 -0700 (PDT) Subject: threads check socket References: Message-ID: <7b3247b0-4c85-4290-9de2-41fc45348d3d@z4g2000prh.googlegroups.com> On Jul 31, 10:23?am, "Gabriel Genellina" wrote: > En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet ? > escribi?: > > > I been trying to find a way to check the socket is open or not. The > > thread are in a group and loop if the sockets are open. If they are > > not open delete the thread and remove the group. I need on this. > > What means an "open socket"? Do you want to detect whether the other side ? > closed the connection? In that case reading from the socket returns an ? > empty string. > > Regarding the "threads in a group", I don't understand what you mean. But ? > in general, you don't delete a thread, you just return from its run() ? > method and the thread finishes execution. > > -- > Gabriel Genellina Thread added into the array. Well this thread when user leave the server want to make sure it not in used so that I can reuse it some how or move into another array. Need to know the thread speed that need for game server timer to control it. Don't want it too fast or slow for the game server. fps = 30 def run(self): while True: self.gametime += 1 if self.gametime > 10000000/fps: self.gametime = 0; From jabronson at gmail.com Fri Jul 31 13:44:33 2009 From: jabronson at gmail.com (Joshua Bronson) Date: Fri, 31 Jul 2009 10:44:33 -0700 (PDT) Subject: heapq "key" arguments Message-ID: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> According to http://docs.python.org/library/heapq.html, Python 2.5 added an optional "key" argument to heapq.nsmallest and heapq.nlargest. I could never understand why they didn't also add a "key" argument to the other relevant functions (heapify, heappush, etc). Say I want to maintain a heap of (x, y) pairs sorted only by first coordinate. Without being able to pass key=itemgetter(0), won't heapifying a list of such pairs unnecessarily compare both coordinates? And worse, if the second coordinate is actually an object with no ordering defined for it, heapifying will cause an error even though all I care about is sorting by the first coordinate, which does have an ordering. Am I missing something? From python at rcn.com Fri Jul 31 13:49:04 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 10:49:04 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: Message-ID: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> On Jul 20, 9:27?am, Phillip B Oldham wrote: > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. The underlying C code for the two is substantially the same. In terms of code, tuples are in effect just immutable lists that have the added feature of being hashable (and therefore usable as dictionary keys or elements of sets). Beyond the mutable/hashable distinction, there is an important philosophical distinction articulated by Guido. He deems tuples to be useful for struct like groupings of non-homogenous fields and lists to be useful for sequences of homogenous data suitable for looping. While nothing in the list/tuple code requires you to make that distinction, it is important because that philosophy pervades the language. If you follow Guido's direction, you'll find that the various parts of the language fit together better. Do otherwise and you'll be going against the grain. Raymond P.S. The C code for lists and tuples have a slightly difference internal structure that makes tuples a little more space efficient (since their size is known at the outset and doesn't change during use). From jgardner at jonathangardner.net Fri Jul 31 14:02:48 2009 From: jgardner at jonathangardner.net (Jonathan Gardner) Date: Fri, 31 Jul 2009 11:02:48 -0700 (PDT) Subject: heapq "key" arguments References: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> Message-ID: <1582f82e-34aa-4b66-b989-c9f8351a5f1c@o9g2000prg.googlegroups.com> On Jul 31, 10:44?am, Joshua Bronson wrote: > Say I want to maintain a heap of (x, y) pairs sorted only by > first coordinate. Without being able to pass key=itemgetter(0), won't > heapifying a list of such pairs unnecessarily compare both > coordinates? It will compare the second value only if the first values are equal. From bfrederi at library.unt.edu Fri Jul 31 14:08:44 2009 From: bfrederi at library.unt.edu (Brandon Fredericks) Date: Fri, 31 Jul 2009 11:08:44 -0700 (PDT) Subject: base64 Incorrect Padding Message-ID: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> I did a search within this group, but couldn't find any information on this. I am sending base64 encoded data as the content over http using urllib2 urlopen. When I receive the data and attempt to decode it, I get an "Incorrect Padding" error. Is there a simple way to fix this? A better way to send and receive the data possibly (using base64 of course)? From masklinn at masklinn.net Fri Jul 31 14:15:09 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 20:15:09 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <293A960D-EFB7-4A09-917A-7ECD3D769B46@masklinn.net> <200907301757.48761.kyrie@uh.cu> Message-ID: <003A7A2F-0FF1-42D4-96ED-1EBBA605B475@masklinn.net> On 31 Jul 2009, at 18:24 , Terry Reedy wrote: > Masklinn wrote: > >> #each is simply a method that takes a function (called blocks in >> ruby). One could call it a higher-order method I guess. >> It's an implementation of the concept of internal iteration: >> instead of collections yielding iterator objects, and programmers >> using those through specially-built iteration constructs (e.g. `for? >> in`), collections control iteration over themselves (the iteration >> is performed "inside" the collection, thus the "internal" part) and >> the programmer provides the operations to perform at each iterative >> step through (usually) a function. > > Python's iterator protocol was developed in part to avoid the > (inside-out) callback style of programming. Writing virtual > collections as generator functions instead of iterator or iterable > classes saves a lot of boilerplate code. The itertools modules shows > how nicely iterators can be composed in a way that is much more > awkward with callbacks. > >> In Python (assuming we had anonymous defs and an each method on >> lists), the following loop: >> for item in some_list: >> do_something(item) >> do_something_else(item) >> some_list.each((def (item): >> do_something(item) >> do_something_else(item) >> )) > > And how does Ruby do the equivalent of > > def double(it): > for i in it: > yield 2*i > > for i,j in zip(double(some_list), some_gen_func(args)): > print(do_something(i+j,i-j)) Somethign along the lines of some_list.map{|e| 2*e}.zip(some_gen_func(args)).each {|i, j| puts(do_something(i+j, i-j)) } The `#each` call after `#zip` is not actually necessary, but I find it clearer. Oh, and some_gen_func and do_something probably wouldn't work that way (as I said above, Ruby isn't big on named functions and doesn't actually have them) From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 14:17:05 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 31 Jul 2009 18:17:05 GMT Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <008eef29$0$9756$c3e8da3@news.astraweb.com> Message-ID: <008f104b$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 18:15:15 +0200, Masklinn wrote: > > I know, I know, Ruby people swear by > > anonymous code blocks, and I've read Paul Graham too. But I'm really > > not so sure that the benefits of anonymous code blocks are great > > enough to overcome the disadvantages of anonymous code blocks. > > > What are the disadvantages of anonymous functions? In no particular order, and not necessarily exhaustive: * The risk of obfuscation in your code. That's fairly minimal for lambdas, because they're just a single expression, but for a large anonymous code block (ACB) defined inside a named function, it may be difficult for the reader to easily distinguish which bits are the outer function and which are the ACB. * Loss of useful debugging information. Take this example from Python: >>> def main(f): ... return f(3) ... >>> main(lambda n: 2/(n-3)) Traceback (most recent call last): File "", line 1, in File "", line 2, in main File "", line 1, in ZeroDivisionError: integer division or modulo by zero >>> >>> def my_special_function(n): ... return 2/(n-3) ... >>> main(my_special_function) Traceback (most recent call last): File "", line 1, in File "", line 2, in main File "", line 2, in my_special_function ZeroDivisionError: integer division or modulo by zero If your code has only one anonymous function (whether a lambda or a full multi-line block), then it's easy to identify which lambda raised the exception: there is only one it could be. But if your code uses lots of lambdas, the lack of a function name makes it hard to distinguish one from another . Anonymity makes identification harder. * Risk of code-duplication and breaking the principle of Once And Only Once. Anonymous functions are generally created, used, then immediately thrown away -- or at least made more-or-less inaccessible for reuse. An anonymous function stored in a callback still exists, but the coder isn't able to easily re-use it for another callback somewhere else in the code. Consequently, there's a temptation for the coder to write the same function multiple times: add_button("Parrot", colour=blue, callback=lambda x: x.stuff('a')) add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) add_button("Canary", colour=yellow, callback=lambda x: x.stuff('a')) instead of: def bird_callback(x): return x.stuff('a') add_button("Parrot", colour=blue, callback=bird_callback) add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) add_button("Canary", colour=yellow, callback=bird_callback) * Recursion is more or less impossible without fragile tricks. (At least for Python. I don't know how recursion operates in Ruby.) -- Steven From dciphercomputing at gmail.com Fri Jul 31 14:22:26 2009 From: dciphercomputing at gmail.com (Simon) Date: Fri, 31 Jul 2009 11:22:26 -0700 (PDT) Subject: Newbie Question regarding __init__() Message-ID: Hi I want to create an instance of dcCursor which inherits from dcObject. When I run the following code it gives the error shown. Can some explain to me what is wrong? I have included the dcObject.py and dcCursor.py below. >>>import dcObject >>> import dcCursor >>> x = dcCursor.dcCursor() Traceback (most recent call last): File "", line 1, in TypeError: __init__() takes no arguments (1 given) # -*- coding: utf-8 -*- # dcCursor.py - The base Cursor Class from dcObject import dcObject class dcCursor(dcObject): """The base Cursor Class""" def OpenCursor(tcTemp,tcTable): """Opens the specified cursor Parameters: tcTemp,tcTable""" pass def CreateCursor(tcTable): """Creates the specified cursor Parameters: tcTable""" pass def init_Exec(): print("init_Exec called") # -*- coding: utf-8 -*- # dcObject.py - Base Object class dcObject(object): """Base Object""" def __init__(): """default init method has the form of init_Pre() and init_Exec init_Post()""" self.init_Pre() and self.init_Exec() self.init_Post() def init_Pre(): """Always called before init_Exec() if it returns false init_Exec is not executed""" return True def init_Exec(): """Base __init__ code goes here and this is only executed if init_Pre() returns true""" return True def init_Post(): """Always called after the init_Pre() and init_Exec()""" return True From nobody at nowhere.com Fri Jul 31 14:22:33 2009 From: nobody at nowhere.com (Nobody) Date: Fri, 31 Jul 2009 19:22:33 +0100 Subject: Printing with colors in a portable way References: <33e19169-19b4-497a-b262-2bcf7b5637d5@r38g2000yqn.googlegroups.com> Message-ID: On Thu, 30 Jul 2009 15:40:37 -0700, Robert Dailey wrote: > Anyone know of a way to print text in Python 3.1 with colors in a > portable way? In other words, I should be able to do something like > this: > > print_color( "This is my text", COLOR_BLUE ) > > And this should be portable (i.e. it should work on Linux, Mac, > Windows). The way that terminals (and emulators) handle colour is fundamentally different from the DOS/Windows console. If you want something which will work on both, you will have write separate implementations for terminals and the DOS/Windows console. For terminals, you can use the "curses" package, e.g.: import curses curses.setupterm() setaf = curses.tigetstr('setaf') setab = curses.tigetstr('setab') def foreground(num): if setaf: sys.stdout.write(curses.tparm(setaf, num)) def background(num): if setab: sys.stdout.write(curses.tparm(setab, num)) For the Windows console, you'll need to use ctypes to interface to the SetConsoleTextAttribute() function from Kernel32.dll. From python at mrabarnett.plus.com Fri Jul 31 14:33:57 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 19:33:57 +0100 Subject: Newbie Question regarding __init__() In-Reply-To: References: Message-ID: <4A733915.9020203@mrabarnett.plus.com> Simon wrote: > Hi > > I want to create an instance of dcCursor which inherits from > dcObject. When I run the following code it gives the error shown. > Can some explain to me what is wrong? I have included the dcObject.py > and dcCursor.py below. > >>>> import dcObject >>>> import dcCursor >>>> x = dcCursor.dcCursor() > Traceback (most recent call last): > File "", line 1, in > TypeError: __init__() takes no arguments (1 given) > All instance methods must have the instance as the first argument, by convention called 'self': > # -*- coding: utf-8 -*- > # dcCursor.py - The base Cursor Class > > from dcObject import dcObject > > class dcCursor(dcObject): > """The base Cursor Class""" > def OpenCursor(tcTemp,tcTable): def OpenCursor(self, tcTemp, tcTable): > """Opens the specified cursor > Parameters: tcTemp,tcTable""" > pass > > def CreateCursor(tcTable): def CreateCursor(self, tcTable): ... and so on, including the '__init__' method. From emmanuel.surleau at gmail.com Fri Jul 31 14:41:12 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 20:41:12 +0200 Subject: Confessions of a Python fanboy In-Reply-To: References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <200907312041.12241.emmanuel.surleau@gmail.com> On Friday 31 July 2009 18:54:23 Tim Rowe wrote: > 2009/7/31 Steven D'Aprano : > > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote: > >> That and the fact that I couldn't stop laughing for long enough to learn > >> any more when I read in the Pragmatic Programmer's Guide that "Ruby, > >> unlike less flexible languages, lets you alter the value of a constant." > >> Yep, as they say "Bug" = "Undocumented feature"! > > > > That's no different from Python's "constant by convention". We don't even > > get a compiler warning! > > We don't actually *declare* that something is constant and then have > that declaration ignored. Python doesn't lie to us, although (as in > any language) a programmer might. You could say that Ruby doesn't either, you just need to read the documentation. Ruby's unwritten motto is "flexibility ?ber alles". In this regard, it is consistent (1). Not much is really bolted down in Ruby. You get encapsulation, but it's so easy to break that it's mostly symbolic. It's a language which gives great power to the programmer. Whether Ruby programmers handle this power responsibly is another debate. Cheers, Emm (1) I find Ruby to be pretty consistent in general, which is not always the case of Python. From emmanuel.surleau at gmail.com Fri Jul 31 14:48:03 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 20:48:03 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> Message-ID: <200907312048.03922.emmanuel.surleau@gmail.com> On Friday 31 July 2009 19:49:04 Raymond Hettinger wrote: > On Jul 20, 9:27 am, Phillip B Oldham wrote: > > Specifically the "differences" between lists and tuples have us > > confused and have caused many "discussions" in the office. We > > understand that lists are mutable and tuples are not, but we're a > > little lost as to why the two were kept separate from the start. They > > both perform a very similar job as far as we can tell. > > The underlying C code for the two is substantially the same. In terms > of code, tuples are in effect just immutable lists that have the > added > feature of being hashable (and therefore usable as dictionary keys or > elements of sets). > > Beyond the mutable/hashable distinction, there is an important > philosophical distinction articulated by Guido. He deems tuples > to be useful for struct like groupings of non-homogenous fields > and lists to be useful for sequences of homogenous data suitable > for looping. > > While nothing in the list/tuple code requires you to make that > distinction, > it is important because that philosophy pervades the language. If you > follow Guido's direction, you'll find that the various parts of the > language fit together better. Do otherwise and you'll be going > against > the grain. I might be wrong, but I get the impression that many people don't indeed "get it" and use tuples as static arrays and lists when the array needs to be dynamically resized. This is certainly how I use them as well. This would tend to show that Guido's notion here was not particularly intuitive. Cheers, Emm From __peter__ at web.de Fri Jul 31 14:56:15 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 20:56:15 +0200 Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: Brandon Fredericks wrote: > I did a search within this group, but couldn't find any information on > this. > > I am sending base64 encoded data as the content over http using > urllib2 urlopen. When I receive the data and attempt to decode it, I > get an "Incorrect Padding" error. Is there a simple way to fix this? A > better way to send and receive the data possibly (using base64 of > course)? Have the server send some known data. Read it with urllib2.urlopen(). Compare with that same data that you encoded locally. What are the differences? Peter From masklinn at masklinn.net Fri Jul 31 15:24:33 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 21:24:33 +0200 Subject: Confessions of a Python fanboy In-Reply-To: <008f104b$0$9756$c3e8da3@news.astraweb.com> References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> <4a72d7cf$0$25239$426a34cc@news.free.fr> <008eef29$0$9756$c3e8da3@news.astraweb.com> <008f104b$0$9756$c3e8da3@news.astraweb.com> Message-ID: On 31 Jul 2009, at 20:17 , Steven D'Aprano wrote: > On Fri, 31 Jul 2009 18:15:15 +0200, Masklinn wrote: > >>> I know, I know, Ruby people swear by >>> anonymous code blocks, and I've read Paul Graham too. But I'm really >>> not so sure that the benefits of anonymous code blocks are great >>> enough to overcome the disadvantages of anonymous code blocks. >>> >> What are the disadvantages of anonymous functions? > > In no particular order, and not necessarily exhaustive: > > * The risk of obfuscation in your code. That's fairly minimal for > lambdas, because they're just a single expression, but for a large > anonymous code block (ACB) defined inside a named function, it may be > difficult for the reader to easily distinguish which bits are the > outer > function and which are the ACB. > I believe that one's unadulterated BS. > * Loss of useful debugging information. Take this example from Python: > >>>> def main(f): > ... return f(3) > ... >>>> main(lambda n: 2/(n-3)) > Traceback (most recent call last): > File "", line 1, in > File "", line 2, in main > File "", line 1, in > ZeroDivisionError: integer division or modulo by zero >>>> >>>> def my_special_function(n): > ... return 2/(n-3) > ... >>>> main(my_special_function) > Traceback (most recent call last): > File "", line 1, in > File "", line 2, in main > File "", line 2, in my_special_function > ZeroDivisionError: integer division or modulo by zero > > If your code has only one anonymous function (whether a lambda or a > full > multi-line block), then it's easy to identify which lambda raised the > exception: there is only one it could be. But if your code uses lots > of > lambdas, the lack of a function name makes it hard to distinguish one > from another . Anonymity makes identification harder. > The traceback gives you the line of the anonymous function (even in python) so unless you have several anonymous functions on the same line, there's no reason why that would be much of an issue. Furthermore, Python doesn't provide any more information when the error happens out of a function (in a `for` or a `with`), so it's not like there's much of a difference here between Ruby's block-based approach and Python's statements-based approach. > * Risk of code-duplication and breaking the principle of Once And Only > Once. Anonymous functions are generally created, used, then > immediately > thrown away -- or at least made more-or-less inaccessible for reuse. > An > anonymous function stored in a callback still exists, but the coder > isn't > able to easily re-use it for another callback somewhere else in the > code. > Consequently, there's a temptation for the coder to write the same > function multiple times: > > add_button("Parrot", colour=blue, callback=lambda x: x.stuff('a')) > add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) > add_button("Canary", colour=yellow, callback=lambda x: x.stuff('a')) > > instead of: > > def bird_callback(x): > return x.stuff('a') > > add_button("Parrot", colour=blue, callback=bird_callback) > add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b')) > add_button("Canary", colour=yellow, callback=bird_callback) > Yes, that one I can give you though I don't think that's a big issue. And it's not like it's hard to extract the anonymous function into a named one and then use that on the third strike, so I really don't believe that point holds much water. > * Recursion is more or less impossible without fragile tricks. > > (At least for Python. I don't know how recursion operates in Ruby.) Code blocks are rarely if ever used recursively. If an operation is using anonymous functions recursively, then there's often something very wrong with the idea leading to that code. So I find this objection irrelevant. From masklinn at masklinn.net Fri Jul 31 15:31:21 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 21:31:21 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: <200907312048.03922.emmanuel.surleau@gmail.com> References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> <200907312048.03922.emmanuel.surleau@gmail.com> Message-ID: <3F279D7F-68C2-4523-B01D-7576F3DFAF3C@masklinn.net> On 31 Jul 2009, at 20:48 , Emmanuel Surleau wrote: > On Friday 31 July 2009 19:49:04 Raymond Hettinger wrote: >> On Jul 20, 9:27 am, Phillip B Oldham >> wrote: >>> Specifically the "differences" between lists and tuples have us >>> confused and have caused many "discussions" in the office. We >>> understand that lists are mutable and tuples are not, but we're a >>> little lost as to why the two were kept separate from the start. >>> They >>> both perform a very similar job as far as we can tell. >> >> The underlying C code for the two is substantially the same. In >> terms >> of code, tuples are in effect just immutable lists that have the >> added >> feature of being hashable (and therefore usable as dictionary keys or >> elements of sets). >> >> Beyond the mutable/hashable distinction, there is an important >> philosophical distinction articulated by Guido. He deems tuples >> to be useful for struct like groupings of non-homogenous fields >> and lists to be useful for sequences of homogenous data suitable >> for looping. >> >> While nothing in the list/tuple code requires you to make that >> distinction, >> it is important because that philosophy pervades the language. If >> you >> follow Guido's direction, you'll find that the various parts of the >> language fit together better. Do otherwise and you'll be going >> against >> the grain. > > I might be wrong, but I get the impression that many people don't > indeed "get > it" and use tuples as static arrays and lists when the array needs > to be > dynamically resized. This is certainly how I use them as well. > > This would tend to show that Guido's notion here was not particularly > intuitive. It's intuitive if you come to Python knowing other languages with tuples (which are mostly functional, and in which tuples are *never* sequences/iterables). At the end of the day, and if Guido's intention truly was what Raymond says, implementing tuples as immutable sequence was a mistake. And the documentation is to blame as well: in it, tuples are clearly described as a *sequence* type, not a *structure* type. From jabronson at gmail.com Fri Jul 31 15:38:01 2009 From: jabronson at gmail.com (Joshua Bronson) Date: Fri, 31 Jul 2009 12:38:01 -0700 (PDT) Subject: heapq "key" arguments References: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> <1582f82e-34aa-4b66-b989-c9f8351a5f1c@o9g2000prg.googlegroups.com> Message-ID: <4f096546-36a1-4633-9a24-1265ce0929ce@q14g2000vbi.googlegroups.com> On Jul 31, 2:02?pm, Jonathan Gardner wrote: > On Jul 31, 10:44?am, Joshua Bronson wrote: > > > Say I want to maintain a heap of (x, y) pairs sorted only by > > first coordinate. Without being able to pass key=itemgetter(0), won't > > heapifying a list of such pairs unnecessarily compare both > > coordinates? > > It will compare the second value only if the first values are equal. I don't see how that helps. That's not at all the same thing as being able to pass key=itemgetter(0). From gagsl-py2 at yahoo.com.ar Fri Jul 31 15:45:53 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 16:45:53 -0300 Subject: class or thread id count References: <585e07cc-8b0e-47e4-b6f6-cde964ed8d13@d9g2000prh.googlegroups.com> <68b09353-7e30-4df2-8e05-32159b03414a@f20g2000prn.googlegroups.com> Message-ID: En Thu, 30 Jul 2009 17:09:36 -0300, NighterNet escribi?: > On Jul 30, 12:14?pm, r wrote: >> On Jul 30, 1:13?pm, NighterNet wrote: >> >> > Need some help on doing some bit simple id count. It like every time >> > it create a class orthreadthere is id++. Python 3.1 >> >> use a class atrribute > > class Person(): > count = 0 > def __init__(self, name, info): > self.name = name > self.info = info > Person.count+=1 > self.count = Person.count Since you menctioned "thread" in the subject, note that the above isn't thread-safe. You'll need a lock around the last two statements. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 15:46:45 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 16:46:45 -0300 Subject: Use existing IE cookie References: <7ddrqsF2b5mblU1@mid.uni-berlin.de> <3d78acf8-594b-4e8d-b138-fa02a77d9432@x25g2000prf.googlegroups.com> <7ddtd3F297bafU1@mid.uni-berlin.de> <22afbc37-f396-404f-8639-6191d258e6be@a39g2000pre.googlegroups.com> <7ddvp0F2brmgrU1@mid.uni-berlin.de> <33b7685b-054a-442c-8e97-46a5b77f7900@z4g2000prh.googlegroups.com> Message-ID: En Thu, 30 Jul 2009 13:31:33 -0300, KB escribi?: > On Jul 30, 9:23?am, "Diez B. Roggisch" wrote: >> KB wrote: >> >> > From the HTTPCookieProcessor doco, it appears that non-IE browsers >> > have a cookie file (and example code) but from what I can tell IE uses >> > a hidden folder. (you can set your location in IE but it appends a >> > folder "\Temporary Internet Files" ?- >> >> > Not sure how to adapt this for IE. For IE you may use the pywin32 package: py> import win32inet py> win32inet.InternetGetCookie("http://sldm/", None) '__ac_name="softlab"' Or use ctypes to call the function of the same name in wininet.dll; see http://msdn.microsoft.com/en-us/library/aa384710(VS.85).aspx -- Gabriel Genellina From tjreedy at udel.edu Fri Jul 31 15:55:11 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 15:55:11 -0400 Subject: Help understanding the decisions *behind* python? In-Reply-To: <200907312048.03922.emmanuel.surleau@gmail.com> References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> <200907312048.03922.emmanuel.surleau@gmail.com> Message-ID: Emmanuel Surleau wrote: > >> Beyond the mutable/hashable distinction, there is an important >> philosophical distinction articulated by Guido. He deems tuples to >> be useful for struct like groupings of non-homogenous fields and >> lists to be useful for sequences of homogenous data suitable for >> looping. I think the use of the with 'homegeneous' in this context is wrong because 1) it is ambiguous or even sometimes not the case, and 2) it misleads. Everything is an object, so all collections are in that sense homogeneous. If the meaning is restricted to type(ob), than a list 'mixing' ints and floats would be non-homogeneous and not 'legitimate', but Guido does not really mean that. The word tuple comes from relational databases as a generalization of single, double, triple, quadruple, quintuple, sextuple, sestuple, octuple, etc. A tuple is a data record with a fixed number of fields with individual meaning. There is nothing about homogeneity of data type in that definition. A triple of floats is legitimately a tuple when each is a coordinate (the individual meanings). In other contexts, the same triple might properly be a list (such as of heights of people arbitrarily ordered). >> While nothing in the list/tuple code requires you to make that >> distinction, which needs to be properly articulated... >> it is important because that philosophy pervades the language. If >> you follow Guido's direction, you'll find that the various parts of >> the language fit together better. Do otherwise and you'll be going >> against the grain. Understanding the idea certainly helps understanding the design. For instance, it explains why no tuple comprehensions. > I might be wrong, but I get the impression that many people don't > indeed "get it" and use tuples as static arrays and lists when the > array needs to be dynamically resized. This is certainly how I use > them as well. You also *need* lists if you want to dynamically re-arrange or replace. If you *must not* re-arrange or edit, then a tuple will prevent that. I would also refer back to the idea of tuple as database record. But a list is needed to edit without copying, so lists can be used for writable database records. I believe tuple(iterable) for indefinite-length iterables is somewhat equivalent to tuple(list(iterable)), so there is usually no point to constructing a tuple that way from such an input. For collections defined by constants, there is a real advantage to using tuples, since tuple constants are compiled when the bytecode is compiled, rather than being dynamically constructed. Inside an inner loop, this might make enough time difference that "Practicality beats purity" would apply. > This would tend to show that Guido's notion here was not particularly > intuitive. The problem is that it *is* intuitive, on his part, and usually not well explained rationally. Terry Jan Reedy From python at mrabarnett.plus.com Fri Jul 31 16:03:53 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 21:03:53 +0100 Subject: base64 Incorrect Padding In-Reply-To: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: <4A734E29.9080708@mrabarnett.plus.com> Brandon Fredericks wrote: > I did a search within this group, but couldn't find any information on > this. > > I am sending base64 encoded data as the content over http using > urllib2 urlopen. When I receive the data and attempt to decode it, I > get an "Incorrect Padding" error. Is there a simple way to fix this? A > better way to send and receive the data possibly (using base64 of > course)? Base-64 encodes 3 bytes (echo in the range 0-255) to 4 bytes (each in a more restricted range). The length of the output is _always_ a multiple of 4 (ignoring whitespace); the method adds extra padding bytes (ASCII '=') to ensure that this is the case if the length of the input isn't a multiple of 3. So, if decoding raises an "Incorrect Padding" error then strip out any whitespace and append extra ASCII '=' to make its length a multiple of 3, then try decoding again. (Or you could just repeatedly add one pad character and retry, up to 3 times.) From no.email at please.post Fri Jul 31 16:10:45 2009 From: no.email at please.post (kj) Date: Fri, 31 Jul 2009 20:10:45 +0000 (UTC) Subject: Python docs disappointing Message-ID: I'm pretty new to Python, and I like a lot overall, but I find the documentation for Python rather poor, overall. I'm sure that Python experts don't have this problem: they have internalized some good ways to access the documentation, are productive with it, and therefore have lost the ability to see why the Python documentations is deficient for beginners. This explains why a suboptimal situation can persist like this: those who are most able fix it are also the least able to perceive it. I've heard similar complaints from other experienced programmers who are trying out Python for the first time: poor documentation. Here is an *entirely typical* example: on some Unix, try % pydoc urllib The displayed documentation mention the optional parameter "data" in practically every function listed (a few dozen of them). This parameter is not documented *anywhere* on that page. All that we are told is that its default value is always None. I'm sure that I can find a full description of this parameter if I fire up Google, and search online. In fact, more likely than not, I'll find far more documentation than I want. But my point is that a programmer should not need to do this. The full documentation should be readily accessible directly through a few keystrokes. I would love to know how experienced Python programmers quickly zero in on the Python documentation they need. TIA! kynn From jstroud at mbi.ucla.edu Fri Jul 31 16:11:49 2009 From: jstroud at mbi.ucla.edu (James Stroud) Date: Fri, 31 Jul 2009 13:11:49 -0700 Subject: Python quirk in evaluation order Message-ID: Python 2.5: mbi136-176 211% python *** Pasting of code with ">>>" or "..." has been enabled. ######################################################################## ## ipython ## ######################################################################## py> b = 4 if True else b py> b 4 Isn't the right side supposed to be evaluated first? From emmanuel.surleau at gmail.com Fri Jul 31 16:26:58 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 22:26:58 +0200 Subject: Help understanding the decisions *behind* python? In-Reply-To: References: <200907312048.03922.emmanuel.surleau@gmail.com> Message-ID: <200907312226.59027.emmanuel.surleau@gmail.com> On Friday 31 July 2009 21:55:11 Terry Reedy wrote: > Emmanuel Surleau wrote: > >> Beyond the mutable/hashable distinction, there is an important > >> philosophical distinction articulated by Guido. He deems tuples to > >> be useful for struct like groupings of non-homogenous fields and > >> lists to be useful for sequences of homogenous data suitable for > >> looping. > > I think the use of the with 'homegeneous' in this context is wrong > because 1) it is ambiguous or even sometimes not the case, and 2) it > misleads. Everything is an object, so all collections are in that sense > homogeneous. If the meaning is restricted to type(ob), than a list > 'mixing' ints and floats would be non-homogeneous and not 'legitimate', > but Guido does not really mean that. > > The word tuple comes from relational databases as a generalization of > single, double, triple, quadruple, quintuple, sextuple, sestuple, > octuple, etc. A tuple is a data record with a fixed number of fields > with individual meaning. There is nothing about homogeneity of data type > in that definition. A triple of floats is legitimately a tuple when each > is a coordinate (the individual meanings). In other contexts, the same > triple might properly be a list (such as of heights of people > arbitrarily ordered). My understanding is that, in this context, it's not so much data types which are heterogeneous, but the semantic meaning of the data. For instance, a tuple containing (first_name, last_name, address) would be a "legitimate" tuple, but not a tuple containing (address, address, address), which, if we follow Guido's philosophy, ought to be represented as a list. Whether including the distinction in the language offers any practical benefit is questionable. [snip] > > This would tend to show that Guido's notion here was not particularly > > intuitive. > > The problem is that it *is* intuitive, on his part, and usually not well > explained rationally. Excellent point. Cheers, Emm From robert.kern at gmail.com Fri Jul 31 16:28:01 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 31 Jul 2009 15:28:01 -0500 Subject: Python docs disappointing In-Reply-To: References: Message-ID: On 2009-07-31 15:10, kj wrote: > I would love to know how experienced Python programmers quickly > zero in on the Python documentation they need. http://docs.python.org/library/urllib I use Firefox's "Quick Searches" feature to make getting this URL as fast as possible: "m urllib" -- 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 rt8396 at gmail.com Fri Jul 31 16:29:36 2009 From: rt8396 at gmail.com (r) Date: Fri, 31 Jul 2009 13:29:36 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: On Jul 31, 3:10?pm, kj wrote: > I'm pretty new to Python, and I like a lot overall, but I find the > documentation for Python rather poor, overall. [snip] If you mean the built-in docs i *highly agree* with you. if you mean docs/tutorials available across the WWW i *highly disagree* with you, and if you mean the command line "help" i also *highly disagree*, of which python has the-best-in the-world! I am using 2.6.2 and the help docs are a joke. trying to find even the simplest thing (built-in functions, etc...) is like pulling teeth. I do not look at them anymore, or do i plan to in the future. Luckily i know python pretty well, but i can't image the horrors a new Python Programmer must face now... Kj, you could not be more right. Doc maintainers need to get some feedback from Python noobies when writing this stuff. Does anybody remember CP4E? I know Guido put a lot of effort into this project and very much believed in this philosophy(circa 1999~2005), although it never really went very far, i still believe in Guido's heart the CP4E exists -- if only barley clinging to life(...maybe?) any thoughts on this Pythonistas? From robert.kern at gmail.com Fri Jul 31 16:31:21 2009 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 31 Jul 2009 15:31:21 -0500 Subject: Python quirk in evaluation order In-Reply-To: References: Message-ID: On 2009-07-31 15:11, James Stroud wrote: > Python 2.5: > > mbi136-176 211% python > *** Pasting of code with ">>>" or "..." has been enabled. > ######################################################################## > ## ipython ## > ######################################################################## > py> b = 4 if True else b > py> b > 4 > > > Isn't the right side supposed to be evaluated first? The right side of the assignment or the if-else expression? The "expr1 if condition else expr2" expression evaluates condition, then either expr1 or expr2 depending on the result. The other expression is unevaluated. >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> b Traceback (most recent call last): File "", line 1, in NameError: name 'b' is not defined >>> 5 if True else b 5 >>> a if False else 5 5 -- 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 marduk at letterboxes.org Fri Jul 31 16:33:00 2009 From: marduk at letterboxes.org (Albert Hopkins) Date: Fri, 31 Jul 2009 16:33:00 -0400 Subject: Python quirk in evaluation order In-Reply-To: References: Message-ID: <1249072381.24514.4.camel@centar> On Fri, 2009-07-31 at 13:11 -0700, James Stroud wrote: > Python 2.5: > > mbi136-176 211% python > *** Pasting of code with ">>>" or "..." has been enabled. > ######################################################################## > ## ipython ## > ######################################################################## > py> b = 4 if True else b > py> b > 4 > > > Isn't the right side supposed to be evaluated first? Yes, the right hand side of the '=', which is evaluated from LtoR. >From the Language Reference: Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. So the right-hand side of the '=' is evaluated first: 4 if True else b And that's evaluated from left to right: 4 Therefore: b = 4 From emmanuel.surleau at gmail.com Fri Jul 31 16:34:06 2009 From: emmanuel.surleau at gmail.com (Emmanuel Surleau) Date: Fri, 31 Jul 2009 22:34:06 +0200 Subject: Python docs disappointing In-Reply-To: References: Message-ID: <200907312234.06374.emmanuel.surleau@gmail.com> On Friday 31 July 2009 22:10:45 kj wrote: > I'm pretty new to Python, and I like a lot overall, but I find the > documentation for Python rather poor, overall. > > I'm sure that Python experts don't have this problem: they have > internalized some good ways to access the documentation, are > productive with it, and therefore have lost the ability to see why > the Python documentations is deficient for beginners. This explains > why a suboptimal situation can persist like this: those who are > most able fix it are also the least able to perceive it. > > I've heard similar complaints from other experienced programmers > who are trying out Python for the first time: poor documentation. The documentation on python.org is quite extensive. In particular, you may want to look at the PEPs, which offer fascinating insights for the reasoning behind particular features of Python. > Here is an *entirely typical* example: on some Unix, try > > % pydoc urllib > > The displayed documentation mention the optional parameter "data" > in practically every function listed (a few dozen of them). This > parameter is not documented *anywhere* on that page. All that we > are told is that its default value is always None. > > I'm sure that I can find a full description of this parameter if > I fire up Google, and search online. In fact, more likely than > not, I'll find far more documentation than I want. But my point > is that a programmer should not need to do this. The full > documentation should be readily accessible directly through a few > keystrokes. You have first-grade documentation on the Python website: http://docs.python.org/library/urllib.html I'm not really using pydoc, but I'd wager it's more used as a quick lookup than anything else. > I would love to know how experienced Python programmers quickly > zero in on the Python documentation they need. I wouldn't count myself as an 'experienced' Python programmer, but I rely on docs.python.org for most things with regards to the standard library. Cheers, Emm From __peter__ at web.de Fri Jul 31 16:39:25 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Jul 2009 22:39:25 +0200 Subject: Python quirk in evaluation order References: Message-ID: James Stroud wrote: > py> b = 4 if True else b > py> b > 4 > Isn't the right side supposed to be evaluated first? Perhaps it becomes clearer if you change it a bit: >>> b = 4 if True else whatever >>> whatever Traceback (most recent call last): File "", line 1, in NameError: name 'whatever' is not defined I. e. the else clause is never evaluated at all. Peter From breamoreboy at yahoo.co.uk Fri Jul 31 16:55:35 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Jul 2009 21:55:35 +0100 Subject: Python docs disappointing In-Reply-To: References: Message-ID: r wrote: > On Jul 31, 3:10 pm, kj wrote: >> I'm pretty new to Python, and I like a lot overall, but I find the >> documentation for Python rather poor, overall. > [snip] > > If you mean the built-in docs i *highly agree* with you. if you mean > docs/tutorials available across the WWW i *highly disagree* with you, > and if you mean the command line "help" i also *highly disagree*, of > which python has the-best-in the-world! > > I am using 2.6.2 and the help docs are a joke. trying to find even the > simplest thing (built-in functions, etc...) is like pulling teeth. I > do not look at them anymore, or do i plan to in the future. Luckily i > know python pretty well, but i can't image the horrors a new Python > Programmer must face now... > > Kj, you could not be more right. Doc maintainers need to get some > feedback from Python noobies when writing this stuff. Does anybody > remember CP4E? I know Guido put a lot of effort into this project and > very much believed in this philosophy(circa 1999~2005), although it > never really went very far, i still believe in Guido's heart the CP4E > exists -- if only barley clinging to life(...maybe?) > > any thoughts on this Pythonistas? I entirely agree with these comments. Looking at home on the compiled help file on windows for python 2.6.2 I can't see anything except for some icons at the top, the Contents, Index, Search and Favorites tabs on the left and the following on the right. What's new in Python 2.6? or all "What's new" documents since 2.0 Tutorial start here Using Python how to use Python on different platforms Language Reference describes syntax and language elements Library Reference keep this under your pillow Python HOWTOs in-depth documents on specific topics Extending and Embedding tutorial for C/C++ programmers Python/C API reference for C/C++ programmers Installing Python Modules information for installers & sys-admins Distributing Python Modules sharing modules with others Documenting Python guide for documentation authors Indices and tables: Global Module Index quick access to all modules General Index all functions, classes, terms Glossary the most important terms explained Search page search this documentation Complete Table of Contents lists all sections and subsections Meta information: Reporting bugs About the documentation History and License of Python Copyright Apart from that what have the Pythonistas ever done for us? Nothing!:) -- Kindest regards. Mark Lawrence. From nagle at animats.com Fri Jul 31 16:56:25 2009 From: nagle at animats.com (John Nagle) Date: Fri, 31 Jul 2009 13:56:25 -0700 Subject: beautiful soup In-Reply-To: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> References: <87ae223b-7201-4fad-b10d-63bc839dbedd@v37g2000prg.googlegroups.com> Message-ID: <4a7359ce$0$1629$742ec2ed@news.sonic.net> xubin.cz wrote: > hi, everyone > > Is there any pakage or module handling html document like beautiful > soup? Try "http://code.google.com/p/html5lib/". That's supposed to be a parser which complies with the HTML 5 specification, including its rules for handling bad HTML. John Nagle From maxerickson at gmail.com Fri Jul 31 17:04:10 2009 From: maxerickson at gmail.com (Max Erickson) Date: Fri, 31 Jul 2009 21:04:10 +0000 (UTC) Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> <4A734E29.9080708@mrabarnett.plus.com> Message-ID: MRAB wrote: > Brandon Fredericks wrote: >> I did a search within this group, but couldn't find any >> information on this. >> >> I am sending base64 encoded data as the content over http using >> urllib2 urlopen. When I receive the data and attempt to decode >> it, I get an "Incorrect Padding" error. Is there a simple way to >> fix this? A better way to send and receive the data possibly >> (using base64 of course)? > > Base-64 encodes 3 bytes (echo in the range 0-255) to 4 bytes > (each in a more restricted range). The length of the output is > _always_ a multiple of 4 (ignoring whitespace); the method adds > extra padding bytes (ASCII '=') to ensure that this is the case > if the length of the input isn't a multiple of 3. > > So, if decoding raises an "Incorrect Padding" error then strip > out any whitespace and append extra ASCII '=' to make its length > a multiple of 3, then try decoding again. (Or you could just > repeatedly add one pad character and retry, up to 3 times.) The length of the encoded string should be a multiple of 4 (as you state in the second sentence of your post), not a multiple of 3. max From davea at ieee.org Fri Jul 31 17:07:01 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 31 Jul 2009 17:07:01 -0400 Subject: Python quirk in evaluation order In-Reply-To: References: Message-ID: <4A735CF5.1070304@ieee.org> James Stroud wrote: >
Python 2.5: > > mbi136-176 211% python > *** Pasting of code with ">>>" or "..." has been enabled. > ######################################################################## > ## ipython ## > ######################################################################## > py> b = 4 if True else b > py> b > 4 > > > Isn't the right side supposed to be evaluated first? > I don't have a clue what value you expected b to have. The if/else ternary expression is roughly equivalent to: if True: b = 4 else b = b The first part to be evaluated is the if expression, which is hard-coded to True. Then the part to the left will be used for the expression result, and the part on the right ignored. This is because of short-circuit rules. Try this one for size: b = 22 if True else I.am.Not.Being.Evaluated(4.4) The else clause does need to be a syntactically valid expression, but it's not evaluated unless the if-expression is false. DaveA From pavlovevidence at gmail.com Fri Jul 31 17:16:48 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 14:16:48 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: On Jul 31, 1:10?pm, kj wrote: > I'm pretty new to Python, and I like a lot overall, but I find the > documentation for Python rather poor, overall. > > I'm sure that Python experts don't have this problem: they have > internalized some good ways to access the documentation, are > productive with it, and therefore have lost the ability to see why > the Python documentations is deficient for beginners. That may be so, but I do remember when I was a beginner myself and I had no issue with the documentation. [snip] > I'm sure that I can find a full description of this parameter if > I fire up Google, and search online. Are you aware that Python ships with a full set of documentation, where (for instance) the meaning of "data" in urllib is defined? You don't need Google. > In fact, more likely than > not, I'll find far more documentation than I want. ?But my point > is that a programmer should not need to do this. ?The full > documentation should be readily accessible directly through a few > keystrokes. Well, no one volunteered to do that. Oh well. > I would love to know how experienced Python programmers quickly > zero in on the Python documentation they need. Choose bookmark in web browser for Python documentation -> keyword search for correct module (easy with firefox) -> scroll. This might not be the smoothest method ever (omg you have to use a *mouse*) but it should be usable enough. Carl Banks From python at mrabarnett.plus.com Fri Jul 31 17:17:30 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 22:17:30 +0100 Subject: base64 Incorrect Padding In-Reply-To: References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> <4A734E29.9080708@mrabarnett.plus.com> Message-ID: <4A735F6A.1010405@mrabarnett.plus.com> Max Erickson wrote: > MRAB wrote: > >> Brandon Fredericks wrote: >>> I did a search within this group, but couldn't find any >>> information on this. >>> >>> I am sending base64 encoded data as the content over http using >>> urllib2 urlopen. When I receive the data and attempt to decode >>> it, I get an "Incorrect Padding" error. Is there a simple way to >>> fix this? A better way to send and receive the data possibly >>> (using base64 of course)? >> Base-64 encodes 3 bytes (echo in the range 0-255) to 4 bytes >> (each in a more restricted range). The length of the output is >> _always_ a multiple of 4 (ignoring whitespace); the method adds >> extra padding bytes (ASCII '=') to ensure that this is the case >> if the length of the input isn't a multiple of 3. >> >> So, if decoding raises an "Incorrect Padding" error then strip >> out any whitespace and append extra ASCII '=' to make its length >> a multiple of 3, then try decoding again. (Or you could just >> repeatedly add one pad character and retry, up to 3 times.) > > The length of the encoded string should be a multiple of 4 (as you > state in the second sentence of your post), not a multiple of 3. > Oops, correct! :-) From masklinn at masklinn.net Fri Jul 31 17:19:07 2009 From: masklinn at masklinn.net (Masklinn) Date: Fri, 31 Jul 2009 23:19:07 +0200 Subject: Python docs disappointing In-Reply-To: <200907312234.06374.emmanuel.surleau@gmail.com> References: <200907312234.06374.emmanuel.surleau@gmail.com> Message-ID: On 31 Jul 2009, at 22:34 , Emmanuel Surleau wrote: > You have first-grade documentation on the Python website: > http://docs.python.org/library/urllib.html > I'm not really using pydoc, but I'd wager it's more used as a quick > lookup > than anything else. Another important documentary resource for the stdlib is Doug Hellmann's awesome Python Module of the Day series (http://www.doughellmann.com/PyMOTW/ ) From pavlovevidence at gmail.com Fri Jul 31 17:23:02 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 14:23:02 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: <7bb4df03-f09a-40b0-83f8-1c6d0c9800a8@j21g2000vbn.googlegroups.com> On Jul 31, 1:55?pm, Mark Lawrence wrote: > Apart from that what have the Pythonistas ever done for us? Nothing!:) Please don't feed the trolls. And if you do feed the trolls don't smile at them. Carl Banks From no.email at please.post Fri Jul 31 17:28:36 2009 From: no.email at please.post (kj) Date: Fri, 31 Jul 2009 21:28:36 +0000 (UTC) Subject: Python docs disappointing References: Message-ID: In Carl Banks writes: >(omg you have to use a >*mouse*) That's precisely the point. There's a huge number of programmers out there who, like me, *hate* to use the mouse while they're coding. It is truly disappointing to us that the developers of Python chose to completely disregard this constituency. This is one area in which Perl still whips Python... kynn From rt8396 at gmail.com Fri Jul 31 17:30:47 2009 From: rt8396 at gmail.com (r) Date: Fri, 31 Jul 2009 14:30:47 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> On Jul 31, 4:16?pm, Carl Banks wrote: > On Jul 31, 1:10?pm, kj wrote: > > > I'm pretty new to Python, and I like a lot overall, but I find the > > documentation for Python rather poor, overall. > > > I'm sure that Python experts don't have this problem: they have > > internalized some good ways to access the documentation, are > > productive with it, and therefore have lost the ability to see why > > the Python documentations is deficient for beginners. > > That may be so, but I do remember when I was a beginner myself and I > had no issue with the documentation. have you tried the new docs (>= 2.6) The interface has changed drastically as to render itself completely useless. The old docs (<= 2.5) --the ones i learned from-- where flawless. @ Mark Lawrence Have you clicked any of those links? try the "Tutorial start here" and then try to find a menu of sorts. It seems you have to click "next" to navigate. Thats pretty counter intuitive if you need to get to page 589!! Upon clicking the tutorial link in the OLD DOCS, you where presented with a nice menu for concise navigation. From python at rcn.com Fri Jul 31 17:43:17 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 14:43:17 -0700 (PDT) Subject: Help understanding the decisions *behind* python? References: <86b332b3-2da0-44e0-b29f-d930136635df@u16g2000pru.googlegroups.com> Message-ID: <4677d753-364f-4d29-9091-e55008daec83@12g2000pri.googlegroups.com> > > While nothing in the list/tuple code requires you to make that > > distinction, > > it is important because that philosophy pervades the language. ?If you > > follow Guido's direction, you'll find that the various parts of the > > language fit together better. ?Do otherwise and you'll be going > > against > > the grain. > > I might be wrong, but I get the impression that many people don't indeed "get > it" and use tuples as static arrays and lists when the array needs to be > dynamically resized. This is certainly how I use them as well. > > This would tend to show that Guido's notion here was not particularly > intuitive. No, it just shows that they are not Dutch ;-) IIRC, Aahz once made an eloquent statement about how languages aren't intuitive because different people have different intuitions. The fact is that every language has its own set of ways. People usually pick- up on what is Pythonic in a short order. The problem comes when people decide-in-advance how the language ought to be -- it takes them longer to pick-up the culture and idioms. More than one person here has observed that the time to learn to program Pythonically is inversely proportional to their experience in Java. It is perfectly okay for you to use tuples for statically sized arrays. That isn't wrong. It is against then grain though. Guido articulated his concept early and many of his minions (me included) have designed the APIs and optimizations to flow nicely with his preferred way of doing things. Raymond From breamoreboy at yahoo.co.uk Fri Jul 31 17:53:02 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Jul 2009 22:53:02 +0100 Subject: Python docs disappointing In-Reply-To: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> References: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> Message-ID: r wrote: > On Jul 31, 4:16 pm, Carl Banks wrote: >> On Jul 31, 1:10 pm, kj wrote: >> >>> I'm pretty new to Python, and I like a lot overall, but I find the >>> documentation for Python rather poor, overall. >>> I'm sure that Python experts don't have this problem: they have >>> internalized some good ways to access the documentation, are >>> productive with it, and therefore have lost the ability to see why >>> the Python documentations is deficient for beginners. >> That may be so, but I do remember when I was a beginner myself and I >> had no issue with the documentation. > > have you tried the new docs (>= 2.6) The interface has changed > drastically as to render itself completely useless. The old docs (<= > 2.5) --the ones i learned from-- where flawless. > > > @ Mark Lawrence > Have you clicked any of those links? try the "Tutorial start here" and > then try to find a menu of sorts. It seems you have to click "next" to > navigate. Thats pretty counter intuitive if you need to get to page > 589!! Upon clicking the tutorial link in the OLD DOCS, you where > presented with a nice menu for concise navigation. Yes. Works perfectly as evidenced by the fact that this evening I've checked data on the cProfile, pstats and array modules. -- Kindest regards. Mark Lawrence. From python at rcn.com Fri Jul 31 17:55:16 2009 From: python at rcn.com (Raymond Hettinger) Date: Fri, 31 Jul 2009 14:55:16 -0700 (PDT) Subject: Help understanding the decisions *behind* python? - immutable objects References: <54411136-ffe1-49c7-b102-f99c5890ce21@k6g2000yqn.googlegroups.com> <9c6c98d6-fced-4ab6-ba65-245b1c7714eb@g31g2000yqc.googlegroups.com> <4a6c9ec0$0$1636$742ec2ed@news.sonic.net> Message-ID: <6b343b95-27d0-4167-bef2-d3e0a5fd87dc@u38g2000pro.googlegroups.com> On Jul 26, 11:24?am, John Nagle wrote: > A tuple is really a frozen list. ?Arguably, frozen objects > should have been a general concept. ?Conceptually, they're > simple - once "__init__" has run, there can be no more changes > to fields of the object. I would argue that freezing and thawing (making the container statefull) is the proverbial road to hell paved with good intentions. It is roughly in the same category as dynamic scoping -- programs become harder to analyze for correctness -- code that used to work can break because something froze it along the way (perhaps to use it as a key for dict or set in a caching function) or because something unfroze it (so it could mutate a value). Raymond From brfredericks at gmail.com Fri Jul 31 17:58:43 2009 From: brfredericks at gmail.com (bfrederi) Date: Fri, 31 Jul 2009 14:58:43 -0700 (PDT) Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: So what if I used a different encoding that isn't ASCII? Like UTF-8? Would that give me lengths that are multiples of 4 based on how the characters are represented? Or would I still need to pad with '='? From pavlovevidence at gmail.com Fri Jul 31 18:03:25 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 15:03:25 -0700 (PDT) Subject: Python docs disappointing References: Message-ID: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> On Jul 31, 2:28?pm, kj wrote: > In Carl Banks writes: > > >(omg you have to use a > >*mouse*) > > That's precisely the point. ?There's a huge number of programmers > out there who, like me, *hate* to use the mouse while they're > coding. You would have figured one of them would have written a script or something if there were so many. Maybe check the Python Cookbook for someone's pet trick, I bet there is one or two there. Or figure out a way to use elinks, the text mode web browser. You know, text mode browsers have come a long way since the lynx days. >?It is truly disappointing to us that the developers of > Python chose to completely disregard this constituency. Python is a volunteer effort, chief. If all these programmers have this itch, as you claim, they've all disregarded themselves by failing to contribute something. > This is one area in which Perl still whips Python... No way. Perl's man pages are organized so poorly there is no ergonomic pit deep enough to offset them. Quick, what man page is the "do" statement documented in? Carl Banks From no.email at please.post Fri Jul 31 18:09:42 2009 From: no.email at please.post (kj) Date: Fri, 31 Jul 2009 22:09:42 +0000 (UTC) Subject: Python docs disappointing References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: In <09bf4f17-40a5-4bad-81d3-1950545b7570 at g6g2000vbr.googlegroups.com> Carl Banks writes: Thanks. Your remarks at least confirm that my impression was not simply due to my noob ignorance: the keyboard-accessible docs are indeed as poor as they look. kynn From pfeldman at verizon.net Fri Jul 31 18:17:56 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 31 Jul 2009 15:17:56 -0700 (PDT) Subject: possible to round number and convert to string? Message-ID: <24763821.post@talk.nabble.com> I'd like to be able to convert a float to a string representation in which the number is rounded to a specified number of digits. If num2str is a hypothetical function that does this, then num2str(pi,3) would be '3.142' (not '3.141'). I've been told that there is no such function in Python. I tried to write this myself, but wasn't successful. Any suggestions will be appreciated. -- View this message in context: http://www.nabble.com/possible-to-round-number-and-convert-to-string--tp24763821p24763821.html Sent from the Python - python-list mailing list archive at Nabble.com. From python at mrabarnett.plus.com Fri Jul 31 18:24:09 2009 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 31 Jul 2009 23:24:09 +0100 Subject: base64 Incorrect Padding In-Reply-To: References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: <4A736F09.6090505@mrabarnett.plus.com> bfrederi wrote: > So what if I used a different encoding that isn't ASCII? Like UTF-8? > Would that give me lengths that are multiples of 4 based on how the > characters are represented? Or would I still need to pad with '='? I think that when it says Base-64 it's talking about the byte values used for the encoding. From david.birdsong at gmail.com Fri Jul 31 18:27:19 2009 From: david.birdsong at gmail.com (birdsong) Date: Fri, 31 Jul 2009 15:27:19 -0700 (PDT) Subject: Non-blocking read with popen subprocess References: <77a55a40-79b5-45c1-a72e-af42528c6e3e@l5g2000pra.googlegroups.com> <824f8092-55b1-4df2-a6b5-74a4b1b1d9ea@i18g2000pro.googlegroups.com> Message-ID: On Jul 30, 7:30?pm, Jonathan Gardner wrote: > On Jul 30, 5:24?pm, Dhanesh wrote: > > > > > how can I we have a non blocking read ? > > Seehttp://docs.python.org/library/popen2.html#flow-control-issues > > Note well: In the non-blocking world, you have to use select() or poll > () to get your job done. I solved a problem just yesterday regarding select on a Popen.stdout file descriptor that was created with a 100Mb buffer. I'd love it if anybody could critique my solution and my understanding of my findings. In my case, select would return with my Popen object ready for read, I'd go off and try to read and then my read would block. After a few strace's I found that Popen.stdout.read(), which I am assuming is the same as the read builtin, would make multiple read system calls, in many cases blocking until it returned my default buffer size. Sure enough, it's in the docs: http://docs.python.org/library/stdtypes.html?highlight=fread """ Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given. """ I couldn't find a way to tell subprocess to return my stdout fd in non- blocking mode, but then I thought about it not being a problem with the fd and how it was opened as much as a problem with the read call. A non-blocking fd will have a read call throw EWOULDBLOCK when asked for more bytes than is in buffer, a blocking fd would have read just return fewer bytes ...do I have this this right? I can handle assembling the data fine, I just don't want to block. I wrote a little test program to spit out random stdout to test a Popen object's stdout in os.read. The len() of the result of os.read was all over the place depending on how quickly i re-ran read() on the fd. There were slight delays from time to time, but I attributed that to the interpreter being context switched in, ssh delays ...or the weather...in general the return was pretty fast. I switched my program to use os.read and now it's snappy and spends most of it's time blocking on select exactly where I want it to. (this is on linux btw, I guess os.read is different everywhere) > You may want to look at "communicate" (http://docs.python.org/library/subprocess.html#popen-objects) which may be what you need. From Nikolaus at rath.org Fri Jul 31 18:38:25 2009 From: Nikolaus at rath.org (Nikolaus Rath) Date: Fri, 31 Jul 2009 18:38:25 -0400 Subject: Python docs disappointing References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: <87prbgscke.fsf@vostro.rath.org> Carl Banks writes: >> This is one area in which Perl still whips Python... > > No way. Perl's man pages are organized so poorly there is no > ergonomic pit deep enough to offset them. Quick, what man page is the > "do" statement documented in? Of course there is: $ perldoc -f do | head do BLOCK Not really a function. Returns the value of the last command in the sequence of commands indicated by BLOCK. When modified by the "while" or "until" loop modifier, executes the BLOCK once before testing the loop condition. (On other statements the loop modifiers test the conditional first.) "do BLOCK" does not count as a loop, so the loop control statements "next", "last", or "redo" cannot be used to leave or restart the block. See perlsyn for alternative strategies. $ Best, -Nikolaus -- ?Time flies like an arrow, fruit flies like a Banana.? PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C From malaclypse2 at gmail.com Fri Jul 31 18:42:03 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 31 Jul 2009 18:42:03 -0400 Subject: possible to round number and convert to string? In-Reply-To: <24763821.post@talk.nabble.com> References: <24763821.post@talk.nabble.com> Message-ID: <16651e80907311542w1a94168egd173641cdfb002ce@mail.gmail.com> On Fri, Jul 31, 2009 at 6:17 PM, Dr. Phillip M. Feldman wrote: > > I'd like to be able to convert a float to a string representation in which > the number is rounded to a specified number of digits. ?If num2str is a > hypothetical function that does this, then num2str(pi,3) would be '3.142' > (not '3.141'). ?I've been told that there is no such function in Python. ?I > tried to write this myself, but wasn't successful. ?Any suggestions will be > appreciated. You should be able to do this with standard string formatting. ( http://docs.python.org/library/stdtypes.html#string-formatting ) For example: >>> from math import pi >>> pi 3.1415926535897931 >>> "%0.3f" % pi '3.142' If you need it as a function, you could do something like this: >>> def num2str(num, precision): return "%0.*f" % (precision, num) >>> num2str(pi, 4) '3.1416' Does that do what you need? -- Jerry From user at example.net Fri Jul 31 18:42:21 2009 From: user at example.net (superpollo) Date: Sat, 01 Aug 2009 00:42:21 +0200 Subject: possible to round number and convert to string? In-Reply-To: References: Message-ID: <4a737350$0$28514$4fafbaef@reader2.news.tin.it> Dr. Phillip M. Feldman wrote: > I'd like to be able to convert a float to a string representation in which > the number is rounded to a specified number of digits. If num2str is a > hypothetical function that does this, then num2str(pi,3) would be '3.142' > (not '3.141'). I've been told that there is no such function in Python. I > tried to write this myself, but wasn't successful. Any suggestions will be > appreciated. >>> import math >>> s = str(round(math.pi, 3)) >>> s '3.142' >>> From gallium.arsenide at gmail.com Fri Jul 31 18:46:22 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 31 Jul 2009 15:46:22 -0700 (PDT) Subject: possible to round number and convert to string? References: Message-ID: <3b719e5e-0cce-4059-86e1-623a0ad7777a@f33g2000vbm.googlegroups.com> On Jul 31, 6:17?pm, "Dr. Phillip M. Feldman" wrote: > I'd like to be able to convert a float to a > string representation in which the number is > rounded to a specified number of digits. ?If > num2str is a hypothetical function that does > this, then num2str(pi,3) would be '3.142' > (not '3.141'). ?I've been told that there is > no such function in Python. ?I tried to write > this myself, but wasn't successful. ?Any > suggestions will be appreciated. This is what I get in Python 2.6.1 on Windows: >>> from math import pi >>> pi 3.1415926535897931 >>> round(pi, 3) 3.1419999999999999 >>> str(round(pi, 3)) '3.142' John From cjns1989 at gmail.com Fri Jul 31 18:54:52 2009 From: cjns1989 at gmail.com (Chris Jones) Date: Fri, 31 Jul 2009 18:54:52 -0400 Subject: Python docs disappointing In-Reply-To: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: <20090731225452.GK25977@turki.gavron.org> On Fri, Jul 31, 2009 at 06:03:25PM EDT, Carl Banks wrote: > On Jul 31, 2:28?pm, kj wrote: > > In Carl Banks writes: > > > > >(omg you have to use a > > >*mouse*) > > > > That's precisely the point. ?There's a huge number of programmers > > out there who, like me, *hate* to use the mouse while they're > > coding. > > You would have figured one of them would have written a script or > something if there were so many. Maybe check the Python Cookbook for > someone's pet trick, I bet there is one or two there. Or figure out a > way to use elinks, the text mode web browser. You know, text mode > browsers have come a long way since the lynx days. So true.. I would add that one of the unplanned merits of a well-thought out text mode browser such as ELinks is to remove all the fluff and leave you, the reader, face to face with the site's content - or lack thereof. CJ From pavlovevidence at gmail.com Fri Jul 31 18:55:19 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 31 Jul 2009 15:55:19 -0700 (PDT) Subject: Python docs disappointing References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> Message-ID: <109f1ff7-8fa9-40d3-80b4-1e90b5fc669c@d34g2000vbm.googlegroups.com> On Jul 31, 3:09?pm, kj wrote: > In <09bf4f17-40a5-4bad-81d3-1950545b7... at g6g2000vbr.googlegroups.com> > > Carl Banks writes: > > > > Thanks. ?Your remarks at least confirm that my impression was not > simply due to my noob ignorance: the keyboard-accessible docs are > indeed as poor as they look. In the standard library, docstrings (which is what pydoc prints) are intended to be a brief explanation/reminder of usage, not an exhaustive description. Where docstrings are present, they are more or less adequate for their intended use (although the quality of docstrings does vary highly throughout the library). If you are trying to use pydoc in a way not intended, then yes you would likely find that they are poor when used in that unintended way. Docstrings take up memory in a running process, so they are not ever likely to be converted to an exhaustive description. Your best bet is to figure out a way to automate lookup in the html documentation. Carl Banks From aahz at pythoncraft.com Fri Jul 31 19:09:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 31 Jul 2009 16:09:16 -0700 Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: In article <062813eb-7eec-4504-b32c-abadf02c3e38 at 12g2000pri.googlegroups.com>, dandi kain wrote: > >What is the functionality of __ or _ , leading or trailing an object , >class ot function ? Is it just a naming convention to note special >functions and objects , or it really mean someting to Python ? One more thing: if you have global module names with a single leading underscore (e.g. "_foo"), they will not be loaded when you use from import * However, "import *" is strongly discouraged for the most part. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From jeremy.cowles at gmail.com Fri Jul 31 19:57:23 2009 From: jeremy.cowles at gmail.com (Jeremy Cowles) Date: Fri, 31 Jul 2009 16:57:23 -0700 Subject: Bug with Python, Cygwin and /dev/urandom? Message-ID: <373cf0740907311657j3e65d94mbbb2eda9e0f94fb7@mail.gmail.com> urllib2.py is crashes when calling randombytes(n). I run the following under Cygwin (mimicking how randombytes works): $ python Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14) [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import sys,os >>> os.path.exists("/dev/urandom") True >>> f = open("/dev/urandom") >>> s = f.read(2) >>> f.close() Traceback (most recent call last): File "", line 1, in IOError: [Errno 0] Error Errno 0 is supposed to be impossible according to the following thread. Apparently this same issue also causes Mercurial to crash: http://www.nabble.com/hg-1.0-exits-with-abort:-Error-td19021833.html Is this a bug? -- Jeremy -------------- next part -------------- An HTML attachment was scrubbed... URL: From rt8396 at gmail.com Fri Jul 31 20:00:28 2009 From: rt8396 at gmail.com (r) Date: Fri, 31 Jul 2009 17:00:28 -0700 (PDT) Subject: Python docs disappointing References: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> Message-ID: On Jul 31, 4:53?pm, Mark Lawrence wrote: > r wrote: > > On Jul 31, 4:16 pm, Carl Banks wrote: > >> On Jul 31, 1:10 pm, kj wrote: > > >>> I'm pretty new to Python, and I like a lot overall, but I find the > >>> documentation for Python rather poor, overall. > >>> I'm sure that Python experts don't have this problem: they have > >>> internalized some good ways to access the documentation, are > >>> productive with it, and therefore have lost the ability to see why > >>> the Python documentations is deficient for beginners. > >> That may be so, but I do remember when I was a beginner myself and I > >> had no issue with the documentation. > > > have you tried the new docs (>= 2.6) The interface has changed > > drastically as to render itself completely useless. The old docs (<= > > 2.5) --the ones i learned from-- where flawless. > > > @ Mark Lawrence > > Have you clicked any of those links? try the "Tutorial start here" and > > then try to find a menu of sorts. It seems you have to click "next" to > > navigate. Thats pretty counter intuitive if you need to get to page > > 589!! Upon clicking the tutorial link in the OLD DOCS, you where > > presented with a nice menu for concise navigation. > > Yes. ?Works perfectly as evidenced by the fact that this evening I've > checked data on the cProfile, pstats and array modules. > > -- > Kindest regards. > > Mark Lawrence. Hold the phone... You checked data on modules using the "Tutorial Start Here" link? Would not the "Global Module Index" be more, shall we say, "informative"? From kee at kagi.com Fri Jul 31 20:12:43 2009 From: kee at kagi.com (Kee Nethery) Date: Fri, 31 Jul 2009 17:12:43 -0700 Subject: Python docs disappointing - group effort to hire writers? In-Reply-To: <109f1ff7-8fa9-40d3-80b4-1e90b5fc669c@d34g2000vbm.googlegroups.com> References: <09bf4f17-40a5-4bad-81d3-1950545b7570@g6g2000vbr.googlegroups.com> <109f1ff7-8fa9-40d3-80b4-1e90b5fc669c@d34g2000vbm.googlegroups.com> Message-ID: <756580E3-CFFA-404C-B66A-9F4281760C8E@kagi.com> I too find the Python docs not very useful and it really slows down my learning curve. I wonder if it would make sense to find good tech writers, get a quotes, and get some professionally written documentation WITH LOTS OF EXAMPLES added to the standard Python documentation tree. I'd chip in money for that task. I've certainly spent enough buying Python books to where it would be very reasonable to chip in the cost of one book towards this project. Get enough people ... could be a great thing. Even though it is not the version I use, I would suggest that the really detailed docs with lots of examples be written against the latest python version. Just a thought. Kee Nethery From martin.hellwig at dcuktec.org Fri Jul 31 20:14:33 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Sat, 01 Aug 2009 01:14:33 +0100 Subject: Python docs disappointing In-Reply-To: References: Message-ID: kj wrote: Well to a level I agree with you. If you are totally new to programming _and_ you won't/can't invest in educational material _and_ have an adversity for looking up resources using a web browser _and_ don't have the patience for trial and error *then* getting proficient with the language is almost impossible. Sometimes some modules are completely incomprehensible for me and that is after I have looked at the docstrings, the python doc and examples found using Google. However usually after combining all three with a fair bit of experimenting I figure it out, to me that is fun I like that kind of puzzles but I am aware that this isn't the best possible situation. Overall I would say a better situation would be if the standard python documentation would have an *extensive* 'pattern' section for each module much like, for example, Frederik Lundh did this with his tkinter documentation. Incorporating these patterns in the standard documentation would save you some jumping around sites and it is perhaps more 'trustworthy' coming from the source (pun intended). I believe that the documentation is all ReST format, so there should be a way (if there isn't already) to display the extensive documentation from the interpreter directly for the module you are working with. As you probably noticed, I actually never bothered to look if this isn't already the case. Which is a good indication to how much it has actually bothered me. Furthermore I like to add to my reply, which is already longer then I anticipated at first, the thing I don't agree with you is the following (and comparative in nature): Compared to all other 'popular' languages I find Python actually very, very good documented and if you can't learn Python using the available documentation and on-line resources I think it is unlikely you can learn any other language, whether this is a programming language or a natural language. Actually, I should have replaced 'any' with 'anything' followed by a full stop. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From breamoreboy at yahoo.co.uk Fri Jul 31 20:20:32 2009 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 01 Aug 2009 01:20:32 +0100 Subject: Python docs disappointing In-Reply-To: References: <4679ca44-492d-457c-a1cb-1dd7e6181d24@b15g2000yqd.googlegroups.com> Message-ID: r wrote: > On Jul 31, 4:53 pm, Mark Lawrence wrote: >> r wrote: >>> On Jul 31, 4:16 pm, Carl Banks wrote: >>>> On Jul 31, 1:10 pm, kj wrote: >>>>> I'm pretty new to Python, and I like a lot overall, but I find the >>>>> documentation for Python rather poor, overall. >>>>> I'm sure that Python experts don't have this problem: they have >>>>> internalized some good ways to access the documentation, are >>>>> productive with it, and therefore have lost the ability to see why >>>>> the Python documentations is deficient for beginners. >>>> That may be so, but I do remember when I was a beginner myself and I >>>> had no issue with the documentation. >>> have you tried the new docs (>= 2.6) The interface has changed >>> drastically as to render itself completely useless. The old docs (<= >>> 2.5) --the ones i learned from-- where flawless. >>> @ Mark Lawrence >>> Have you clicked any of those links? try the "Tutorial start here" and >>> then try to find a menu of sorts. It seems you have to click "next" to >>> navigate. Thats pretty counter intuitive if you need to get to page >>> 589!! Upon clicking the tutorial link in the OLD DOCS, you where >>> presented with a nice menu for concise navigation. >> Yes. Works perfectly as evidenced by the fact that this evening I've >> checked data on the cProfile, pstats and array modules. >> >> -- >> Kindest regards. >> >> Mark Lawrence. > > Hold the phone... You checked data on modules using the "Tutorial > Start Here" link? Would not the "Global Module Index" be more, shall > we say, "informative"? You asked "Have you clicked any of those links?". I answered yes since they all work. I usually navigate to module data via the index tab as I find this the easiest way. I'm sure other people prefer the global module index. Six of one, half a dozen of the other? -- Kindest regards. Mark Lawrence. From aahz at pythoncraft.com Fri Jul 31 20:39:20 2009 From: aahz at pythoncraft.com (Aahz) Date: 31 Jul 2009 17:39:20 -0700 Subject: Python docs disappointing References: Message-ID: In article , kj wrote: >In Carl Banks writes: >> >>(omg you have to use a *mouse*) > >That's precisely the point. There's a huge number of programmers out >there who, like me, *hate* to use the mouse while they're coding. It >is truly disappointing to us that the developers of Python chose to >completely disregard this constituency. That's why I use Lynx on a local copy of the Python docs. Next question? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Many customs in this life persist because they ease friction and promote productivity as a result of universal agreement, and whether they are precisely the optimal choices is much less important." --Henry Spencer From pfeldman at verizon.net Fri Jul 31 20:42:57 2009 From: pfeldman at verizon.net (Dr. Phillip M. Feldman) Date: Fri, 31 Jul 2009 17:42:57 -0700 (PDT) Subject: possible to round number and convert to string? In-Reply-To: <4a737350$0$28514$4fafbaef@reader2.news.tin.it> References: <24763821.post@talk.nabble.com> <4a737350$0$28514$4fafbaef@reader2.news.tin.it> Message-ID: <24764948.post@talk.nabble.com> This was very close to what I wanted. Thanks! My final code looks like this: def num2str(x,f=4): """Convert x (int or float) to a string with f digits to right of the decimal point. f may be zero or negative, in which case the decimal point is suppressed.""" s= str(round(x,f)) if f<=0: # Delete decimal point: i= s.find('.') if i>0: s= s[:i] return s -- View this message in context: http://www.nabble.com/possible-to-round-number-and-convert-to-string--tp24763821p24764948.html Sent from the Python - python-list mailing list archive at Nabble.com. From gagsl-py2 at yahoo.com.ar Fri Jul 31 20:46:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 21:46:22 -0300 Subject: threads check socket References: <7b3247b0-4c85-4290-9de2-41fc45348d3d@z4g2000prh.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 14:42:29 -0300, NighterNet escribi?: > On Jul 31, 10:23?am, "Gabriel Genellina" > wrote: >> En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet >> escribi?: >> > I been trying to find a way to check the socket is open or not. The >> > thread are in a group and loop if the sockets are open. If they are >> > not open delete the thread and remove the group. I need on this. >> >> What means an "open socket"? Do you want to detect whether the other >> side ? >> closed the connection? In that case reading from the socket returns an ? >> empty string. >> >> Regarding the "threads in a group", I don't understand what you mean. >> But ? >> in general, you don't delete a thread, you just return from its run() ? >> method and the thread finishes execution. > > Thread added into the array. Which array? > Well this thread when user leave the > server want to make sure it not in used so that I can reuse it some > how or move into another array. Sorry but it's hard to help if you don't provide enough background. Looks like you're developing a game server; maybe this story is relevant to you: http://comments.gmane.org/gmane.comp.python.general/627319 Assuming you're talking about TCP: when the client closes the connection gracefully, you get an empty string '' when reading from the socket. If the client loses the connection abruptly (the power goes down, the network is broken, the process is killed...) you get no indication of that. You may need to implement some mechanism (a PING? query/response, or perhaps the client must send an "I'm alive!" message once each N minutes; no answer means it's down and the server closes the connection). > Need to know the thread speed that need for game server timer to > control it. Don't want it too fast or slow for the game server. > fps = 30 > def run(self): > while True: > self.gametime += 1 > if self.gametime > 10000000/fps: > self.gametime = 0; What's this code used for, apart from sucking lots of CPU power? -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 21:02:56 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 22:02:56 -0300 Subject: base64 Incorrect Padding References: <0fda9d64-ddfd-40c4-83a1-e3a1ea68ad03@s31g2000yqs.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 18:58:43 -0300, bfrederi escribi?: > So what if I used a different encoding that isn't ASCII? Like UTF-8? > Would that give me lengths that are multiples of 4 based on how the > characters are represented? Or would I still need to pad with '='? It doesn't matter, a base64-encoded string contains only ASCII characters. How are you encoding the text? base64.b64encode does the padding for you, so you don't have to worry about that. Do as P. Otten suggests, try to send some known text and see what you actually get on the server. Most likely you'll find another problem (like trying to decode the complete HTTP response instead of just the entity body, or some other stupid mistake :) ). -- Gabriel Genellina From nagle at animats.com Fri Jul 31 21:32:49 2009 From: nagle at animats.com (John Nagle) Date: Fri, 31 Jul 2009 18:32:49 -0700 Subject: Module updating plans for Python 3.1: feedparser, MySQLdb Message-ID: <4a739a96$0$1615$742ec2ed@news.sonic.net> Any progress on updating feedparser and MySQLdb for Python 3.x in the foreseeable future? Feedparser shouldn't be that hard; it's just that nobody is working on it. MySQLdb is known to be hard, and that may be a while. John Nagle From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 21:46:12 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Aug 2009 01:46:12 GMT Subject: Confessions of a Python fanboy References: <008e82e8$0$9756$c3e8da3@news.astraweb.com> Message-ID: <008f7990$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 20:41:12 +0200, Emmanuel Surleau wrote: >> We don't actually *declare* that something is constant and then have >> that declaration ignored. Python doesn't lie to us, although (as in any >> language) a programmer might. > > You could say that Ruby doesn't either, Well you could say a lot of things. Admittedly you don't need a separate "constant" declaration, but creating a variable name with an initial uppercase letter is sufficient to make it a (supposed) constant: irb(main):049:0* Thing = 5 => 5 irb(main):050:0> Thing = 7 (irb):50: warning: already initialized constant Thing => 7 As you can see, Ruby (well, irb at least) considers that Thing is a constant, and then goes ahead and changes it anyway. Apart from the use of an arbitrary naming convention instead of an explicit "make this constant" declaration, and the feeble way Ruby capitulates when you change it, I think having actual write-once constants is actually a plus. > you just need to read the > documentation. Ruby's unwritten motto is "flexibility ?ber alles". In > this regard, it is consistent (1). "It's consistent in its inconsistency" sort of thing perhaps? > Not much is really bolted down in > Ruby. You get encapsulation, but it's so easy to break that it's mostly > symbolic. Out of curiosity, can you read/write class and instance attributes from outside the class without using a getter/setter? -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri Jul 31 21:48:09 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 Aug 2009 01:48:09 GMT Subject: Python docs disappointing References: Message-ID: <008f7a05$0$9756$c3e8da3@news.astraweb.com> On Fri, 31 Jul 2009 21:28:36 +0000, kj wrote: > In > Carl Banks writes: > >>(omg you have to use a >>*mouse*) > > That's precisely the point. There's a huge number of programmers out > there who, like me, *hate* to use the mouse while they're coding. Complain to the developers of your web browser then. Or use a different web browser. > It is > truly disappointing to us that the developers of Python chose to > completely disregard this constituency. Python is open source software developed by a community of volunteers, so *you* are one of the developers of Python. You have an itch that needs scratching. Solve it yourself, and release it to the wider community, don't expect somebody else to do so. Nobody is entitled to any functionality in Python. You can ask for it nicely, and if somebody thinks it's a good idea, and have nothing better to do, they may code it out of the goodness of their heart. Or you can do it yourself, or you can pay somebody to do it if you don't have the technical skill yourself. Or you can do without. > This is one area in which Perl still whips Python... If so, it's because Perl has more people who understand that the way to get something done is to do it, rather than whining about it. -- Steven From dciphercomputing at gmail.com Fri Jul 31 21:53:47 2009 From: dciphercomputing at gmail.com (Simon) Date: Fri, 31 Jul 2009 18:53:47 -0700 (PDT) Subject: Newbie Question regarding __init__() References: Message-ID: Hi So should the dcObject class include the "self" as well since I have not defined an __init__ method in dcCursor? Simon From gagsl-py2 at yahoo.com.ar Fri Jul 31 22:06:39 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 23:06:39 -0300 Subject: Bug with Python, Cygwin and /dev/urandom? References: <373cf0740907311657j3e65d94mbbb2eda9e0f94fb7@mail.gmail.com> Message-ID: En Fri, 31 Jul 2009 20:57:23 -0300, Jeremy Cowles escribi?: > urllib2.py is crashes when calling randombytes(n). I run the following > under > Cygwin (mimicking how randombytes works): > > $ python > Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14) > [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin > Type "help", "copyright", "credits" or "license" for more information. >>>> import sys,os >>>> os.path.exists("/dev/urandom") > True >>>> f = open("/dev/urandom") >>>> s = f.read(2) >>>> f.close() > Traceback (most recent call last): > File "", line 1, in > IOError: [Errno 0] Error > > > Errno 0 is supposed to be impossible according to the following thread. > Apparently this same issue also causes Mercurial to crash: > http://www.nabble.com/hg-1.0-exits-with-abort:-Error-td19021833.html > > > Is this a bug? It seems so, but not necesarily a Python one; it might be a bug in cygwin instead. If not already reported, file a bug at http://bugs.python.org/ -- Gabriel Genellina From python.list at tim.thechases.com Fri Jul 31 22:39:31 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 31 Jul 2009 21:39:31 -0500 Subject: Python docs disappointing In-Reply-To: References: Message-ID: <4A73AAE3.1050209@tim.thechases.com> Martin P. Hellwig wrote: > kj wrote: [excerpt of previously snipped content restored] >> I'm sure that I can find a full description of this parameter if >> I fire up Google, and search online. In fact, more likely than >> not, I'll find far more documentation than I want. But my point >> is that a programmer should not need to do this. The full >> documentation should be readily accessible directly through a few >> keystrokes. > > Well to a level I agree with you. [snip] > have an adversity for looking up resources using a web browser It may not be an adversity for looking things up using a web-browser, but rather the need to access documentation offline. Whether on an airplane or simply away from a wifi/landline connection, there are plenty of times I'm coding offline (another reason I'm happy to have DVCS tools). This thread prompted me to go digging a little further, and all the online docs are available in downloadable format, but you have to start by going to the base docs and chasing down the "downloads" rabbit-hole. Perhaps it would be easier if there were some sort of back-link from individual pages, referencing the downloadable format package. Combined with the downloaded HTML version of the docs (there are PDFs too if you prefer), using a lightweight browser like lynx/links/dillo, and optionally adding htdig for local search/indexing, you can access (and search) the full docs offline. -tkc From sjmachin at lexicon.net Fri Jul 31 22:41:30 2009 From: sjmachin at lexicon.net (John Machin) Date: Fri, 31 Jul 2009 19:41:30 -0700 (PDT) Subject: .dbf tables and Null References: Message-ID: <2f4d5719-6ca8-4ef2-8734-420b3044b1dc@l35g2000pra.googlegroups.com> On Aug 1, 3:41?am, Ethan Furman wrote: > Mornin'! ?and a good one, too, I hope. > > Question for you... > > First part of the question: ?What is the general value in having Null > capability for fields? In general, in any database system, so that one can distinguish between "the customer has no 'middle name'" ('') and "the customer's 'middle name' is unknown" (NULL). > > Second part: ?Is there a tangible difference between Null, and the > nothing of 0, '', False, etc, in python? 0 is the zero thing, it is not nothing. False is not nothing. > > Third part: ?If there is a tangible difference, do those of us who use > python and these old, refuse-to-die, .dbf files actually have need of, > or have tables including, Null values? > > P.S. part (for John Machin, if he sees this ?;) > Will the dbf package you are working on support Null values? My philosophy when digging stuff out of arcane storages is to expose what is found and leave any kludging/sanitising to the next layer. For example, None is returned for an N (Number) field that's all spaces; it's up to the caller to decide whether to treat None as zero, raise an exception, pop up a data-collection dialogue box, ... If you mean specifically the Visual Foxpro v3 _NullFlags hack, yes, it already supports that, as well as the VFP9 abuse of that hack for Varchar and Varbinary fields :-) From gagsl-py2 at yahoo.com.ar Fri Jul 31 22:58:40 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 31 Jul 2009 23:58:40 -0300 Subject: heapq "key" arguments References: <97932d10-8601-401f-bb69-a98db9036929@p15g2000vbl.googlegroups.com> <1582f82e-34aa-4b66-b989-c9f8351a5f1c@o9g2000prg.googlegroups.com> <4f096546-36a1-4633-9a24-1265ce0929ce@q14g2000vbi.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 16:38:01 -0300, Joshua Bronson escribi?: > On Jul 31, 2:02?pm, Jonathan Gardner > wrote: >> On Jul 31, 10:44?am, Joshua Bronson wrote: >> > Say I want to maintain a heap of (x, y) pairs sorted only by >> > first coordinate. Without being able to pass key=itemgetter(0), won't >> > heapifying a list of such pairs unnecessarily compare both >> > coordinates? >> >> It will compare the second value only if the first values are equal. > > I don't see how that helps. That's not at all the same thing as being > able to pass key=itemgetter(0). Ok, it's not strictly the same, but usually it doesn't hurt. The heaqp module doesn't promise anything about equal elements: it may keep the original order, rearrange them at will, reverse them, whatever. So the element-wise comparison of tuples is as good as comparing only the first element - *except* when comparing the second element isn't cheap or has side effects or something like that. In that case, use a custom class to redefine comparison operators: from heapq import heapify, heappop class tuplebyfirst(tuple): "tuple that sorts only on first element" def __lt__(self, other): return self[0] <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> <337482f9-8ad9-4600-9cf4-39911f5368ac@h31g2000yqd.googlegroups.com> Message-ID: <87ab2ktexg.fsf@benfinney.id.au> BDZ writes: > Unfortunately, although it has the calls I'd want, pysamba appears to > be *nix only. That's because Samba is *nix-only. If you want to use something knowing that it's Samba, you are by definition working on *nix. > I need something that will work under Windows. In that case, forget the term ?samba?. What you want is the ability to connect, *over a network*, to shares served via the SMB protocol suite. Those shares might be implemented via Samba, MS Windows, or aything else that can speak the SMB protocol suite. Your program, running on Windows, should not need to care about Samba, only about the various SMB protocols. A search for ?site:pypi.python.org smb client? gets me directly to . That's totally useless to you though, unfortunately, since it depends on Samba installed ? which you can't, by definition, on MS Windows. > Is there a set of Python Windows functions (official or contributed) > that might do what I need? (I'm new to Python) Not that I know of. It would probably involve a Python wrapper around the native MS Windows networking API, something which I'm not sure has been done. -- \ ?Whenever you read a good book, it's like the author is right | `\ there, in the room talking to you, which is why I don't like to | _o__) read good books.? ?Jack Handey | Ben Finney From nat.williams at gmail.com Fri Jul 31 23:13:05 2009 From: nat.williams at gmail.com (Nat Williams) Date: Fri, 31 Jul 2009 22:13:05 -0500 Subject: Newbie Question regarding __init__() In-Reply-To: References: Message-ID: As MRAB described, ALL instance methods need to accept 'self' as a first parameter, as that will be passed to them implicitly when they are called. This includes __init__. The name 'self' is just a commonly accepted convention for the name of the instance object passed to methods. You don't have to call it that, but you really should. Take a look at http://docs.python.org/tutorial/classes.html#class-objects It might help shed some light on how methods and instances work. One other thing. I'm a little confused by the first line of dcObject.__init__: self.init_Pre() and self.init_Exec() I suspect this does not do what you think it does. init_Pre and init_Exec will both be called by this expression (unless init_Pre throws an exception, of course). You're not getting anything here that you wouldn't by just calling each method on a separate line, except just making it harder to read. Nat On Fri, Jul 31, 2009 at 8:53 PM, Simon wrote: > Hi > > So should the dcObject class include the "self" as well since I have > not defined an __init__ method in dcCursor? > > Simon > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From darkneter at gmail.com Fri Jul 31 23:14:13 2009 From: darkneter at gmail.com (NighterNet) Date: Fri, 31 Jul 2009 20:14:13 -0700 (PDT) Subject: socket policy flash help Message-ID: <04e26f53-5f51-476d-823e-47bfeed9b2a8@m7g2000prd.googlegroups.com> I need help on the policy to able to let access to user to the server once the policy access is finish. I been trying to find a good example, but I got no luck. Using python version 3.1. Here the code I tested but it not working. if str(buff) == str("b\'\\x00\'"): print ('policy FOUND >>> sending...') rawinput = str('') print (rawinput) b = bytes ( ord(c) for c in rawinput) self.sockfd.send(b); From tjreedy at udel.edu Fri Jul 31 23:18:35 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 31 Jul 2009 23:18:35 -0400 Subject: Python docs disappointing In-Reply-To: <4A73AAE3.1050209@tim.thechases.com> References: <4A73AAE3.1050209@tim.thechases.com> Message-ID: Tim Chase wrote: > It may not be an adversity for looking things up using a web-browser, > but rather the need to access documentation offline. Whether on an > airplane or simply away from a wifi/landline connection, there are > plenty of times I'm coding offline (another reason I'm happy to have > DVCS tools). This is one area where Windows users seems to have an advantage. The standard installer includes the doc set as a Windows help file. I often keep that open in one window while programming in others. I only later discovered that this was a copy of the online docs ;-), which I only use to check the in-development version before submitting a doc bug. From ben+python at benfinney.id.au Fri Jul 31 23:21:05 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 01 Aug 2009 13:21:05 +1000 Subject: Does underscore has any special built-in meaningin Python ? References: <062813eb-7eec-4504-b32c-abadf02c3e38@12g2000pri.googlegroups.com> Message-ID: <873a8cte1q.fsf@benfinney.id.au> aahz at pythoncraft.com (Aahz) writes: > One more thing: if you have global module names with a single leading > underscore (e.g. "_foo"), they will not be loaded when you use > > from import * > > However, "import *" is strongly discouraged for the most part. Dude, that's exactly the ?underscore has an effect on import but as a beginner don't worry about it? that I avoided drawing attention to. The explanation was just fine without that. But no, you have to find a way to *contribute*, don't you? Things were different before all these new-fangled changes, I say. Get off my lawn. -- \ ?I love and treasure individuals as I meet them, I loathe and | `\ despise the groups they identify with and belong to.? ?George | _o__) Carlin, 2007 | Ben Finney From gagsl-py2 at yahoo.com.ar Fri Jul 31 23:22:35 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 01 Aug 2009 00:22:35 -0300 Subject: Help understanding the decisions *behind* python? References: <200907312048.03922.emmanuel.surleau@gmail.com> <200907312226.59027.emmanuel.surleau@gmail.com> Message-ID: En Fri, 31 Jul 2009 17:26:58 -0300, Emmanuel Surleau escribi?: > On Friday 31 July 2009 21:55:11 Terry Reedy wrote: >> The word tuple comes from relational databases as a generalization of >> single, double, triple, quadruple, quintuple, sextuple, sestuple, >> octuple, etc. A tuple is a data record with a fixed number of fields >> with individual meaning. There is nothing about homogeneity of data type >> in that definition. A triple of floats is legitimately a tuple when each >> is a coordinate (the individual meanings). In other contexts, the same >> triple might properly be a list (such as of heights of people >> arbitrarily ordered). > > My understanding is that, in this context, it's not so much data types > which > are heterogeneous, but the semantic meaning of the data. For instance, a > tuple > containing (first_name, last_name, address) would be a "legitimate" > tuple, but > not a tuple containing (address, address, address), which, if we follow > Guido's philosophy, ought to be represented as a list. Note that years ago the distinction was much stronger: tuples had NO methods at all. All you could do with a tuple was: len(t), t[i], t1+t2, t*n, e in t, for e in t Being so "crippled", thinking of tuples just as immutable lists probably wasn't so natural. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 23:31:25 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 01 Aug 2009 00:31:25 -0300 Subject: Newbie Question regarding __init__() References: Message-ID: En Fri, 31 Jul 2009 22:53:47 -0300, Simon escribi?: > So should the dcObject class include the "self" as well since I have > not defined an __init__ method in dcCursor? Every method that you define takes "self" as its first argument. Every method that you want to call on the current instance must be qualified by self.methodname(argu, ments) -- it is not different from anotherobject.methodname(...) Every attribute that you want to access from the current instance must be qualified too: self.attributename There is no implicit self/this in Python as in other languages. -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri Jul 31 23:56:43 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 01 Aug 2009 00:56:43 -0300 Subject: os.path.exists() and Samba shares References: <42f0c423-657f-45fb-bec5-67dcdda9f4ad@a13g2000yqc.googlegroups.com> <3fdc924a-75ad-477a-be2e-6e42ce7bff0c@s15g2000yqs.googlegroups.com> <337482f9-8ad9-4600-9cf4-39911f5368ac@h31g2000yqd.googlegroups.com> Message-ID: En Fri, 31 Jul 2009 13:33:45 -0300, BDZ escribi?: > On Jul 30, 4:41?pm, Lo?c Domaign? > wrote: >> > Hello. I have written a Python 3.1 script running on Windows that uses >> > os.path.exists() to connect to network shares. If the various network >> > shares require different user account and password combos than the >> > account the script is running under the routine returns false. I need >> > something like os.samba.path.exists(username,password,path). Does >> > anyone have a suggestion on how I can accomplish what I need to do in >> > Python? >> >> Could the Python Samba module PySamba be interesting for >> you?http://sourceforge.net/projects/pysamba/ > > Unfortunately, although it has the calls I'd want, pysamba appears to > be *nix only. I need something that will work under Windows. Is there > a set of Python Windows functions (official or contributed) that might > do what I need? (I'm new to Python) SAMBA is a Linux implementation of the SMB protocol, natively supported on Windows. You may use the pywin32 package (available on sourceforge.net) to call the WNetAddConnection2 Windows function: http://msdn.microsoft.com/en-us/library/aa385413(VS.85).aspx -- Gabriel Genellina